Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.h
39653 views
//===-- ClangASTImporter.h --------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGASTIMPORTER_H9#define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGASTIMPORTER_H1011#include <map>12#include <memory>13#include <set>14#include <vector>1516#include "clang/AST/ASTContext.h"17#include "clang/AST/ASTImporter.h"18#include "clang/AST/CharUnits.h"19#include "clang/AST/Decl.h"20#include "clang/AST/DeclCXX.h"21#include "clang/Basic/FileManager.h"22#include "clang/Basic/FileSystemOptions.h"2324#include "lldb/Host/FileSystem.h"25#include "lldb/Symbol/CompilerDeclContext.h"26#include "lldb/Utility/LLDBAssert.h"27#include "lldb/lldb-types.h"2829#include "Plugins/ExpressionParser/Clang/CxxModuleHandler.h"3031#include "llvm/ADT/DenseMap.h"3233namespace lldb_private {3435class ClangASTMetadata;36class TypeSystemClang;3738/// Manages and observes all Clang AST node importing in LLDB.39///40/// The ClangASTImporter takes care of two things:41///42/// 1. Keeps track of all ASTImporter instances in LLDB.43///44/// Clang's ASTImporter takes care of importing types from one ASTContext to45/// another. This class expands this concept by allowing copying from several46/// ASTContext instances to several other ASTContext instances. Instead of47/// constructing a new ASTImporter manually to copy over a type/decl, this class48/// can be asked to do this. It will construct a ASTImporter for the caller (and49/// will cache the ASTImporter instance for later use) and then perform the50/// import.51///52/// This mainly prevents that a caller might construct several ASTImporter53/// instances for the same source/target ASTContext combination. As the54/// ASTImporter has an internal state that keeps track of already imported55/// declarations and so on, using only one ASTImporter instance is more56/// efficient and less error-prone than using multiple.57///58/// 2. Keeps track of from where declarations were imported (origin-tracking).59/// The ASTImporter instances in this class usually only performa a minimal60/// import, i.e., only a shallow copy is made that is filled out on demand61/// when more information is requested later on. This requires record-keeping62/// of where any shallow clone originally came from so that the right original63/// declaration can be found and used as the source of any missing information.64class ClangASTImporter {65public:66struct LayoutInfo {67LayoutInfo() = default;68typedef llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>69OffsetMap;7071uint64_t bit_size = 0;72uint64_t alignment = 0;73llvm::DenseMap<const clang::FieldDecl *, uint64_t> field_offsets;74OffsetMap base_offsets;75OffsetMap vbase_offsets;76};7778ClangASTImporter()79: m_file_manager(clang::FileSystemOptions(),80FileSystem::Instance().GetVirtualFileSystem()) {}8182/// Copies the given type and the respective declarations to the destination83/// type system.84///85/// This function does a shallow copy and requires that the target AST86/// has an ExternalASTSource which queries this ClangASTImporter instance87/// for any additional information that is maybe lacking in the shallow copy.88/// This also means that the type system of src_type can *not* be deleted89/// after this function has been called. If you need to delete the source90/// type system you either need to delete the destination type system first91/// or use \ref ClangASTImporter::DeportType.92///93/// \see ClangASTImporter::DeportType94CompilerType CopyType(TypeSystemClang &dst, const CompilerType &src_type);9596/// \see ClangASTImporter::CopyType97clang::Decl *CopyDecl(clang::ASTContext *dst_ctx, clang::Decl *decl);9899/// Copies the given type and the respective declarations to the destination100/// type system.101///102/// Unlike CopyType this function ensures that types/declarations which are103/// originally from the AST of src_type are fully copied over. The type104/// system of src_type can safely be deleted after calling this function.105/// \see ClangASTImporter::CopyType106CompilerType DeportType(TypeSystemClang &dst, const CompilerType &src_type);107108/// Copies the given decl to the destination type system.109/// \see ClangASTImporter::DeportType110clang::Decl *DeportDecl(clang::ASTContext *dst_ctx, clang::Decl *decl);111112/// Sets the layout for the given RecordDecl. The layout will later be113/// used by Clang's during code generation. Not calling this function for114/// a RecordDecl will cause that Clang's codegen tries to layout the115/// record by itself.116///117/// \param decl The RecordDecl to set the layout for.118/// \param layout The layout for the record.119void SetRecordLayout(clang::RecordDecl *decl, const LayoutInfo &layout);120121bool LayoutRecordType(122const clang::RecordDecl *record_decl, uint64_t &bit_size,123uint64_t &alignment,124llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,125llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>126&base_offsets,127llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>128&vbase_offsets);129130/// If \ref record has a valid origin, this function copies that131/// origin's layout into this ClangASTImporter instance.132///133/// \param[in] record The decl whose layout we're calculating.134/// \param[out] size Size of \ref record in bytes.135/// \param[out] alignment Alignment of \ref record in bytes.136/// \param[out] field_offsets Offsets of fields of \ref record.137/// \param[out] base_offsets Offsets of base classes of \ref record.138/// \param[out] vbase_offsets Offsets of virtual base classes of \ref record.139///140/// \returns Returns 'false' if no valid origin was found for \ref record or141/// this function failed to import the layout from the origin. Otherwise,142/// returns 'true' and the offsets/size/alignment are valid for use.143bool importRecordLayoutFromOrigin(144const clang::RecordDecl *record, uint64_t &size, uint64_t &alignment,145llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,146llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>147&base_offsets,148llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>149&vbase_offsets);150151/// Returns true iff the given type was copied from another TypeSystemClang152/// and the original type in this other TypeSystemClang might contain153/// additional information (e.g., the definition of a 'class' type) that could154/// be imported.155///156/// \see ClangASTImporter::Import157bool CanImport(const CompilerType &type);158159/// If the given type was copied from another TypeSystemClang then copy over160/// all missing information (e.g., the definition of a 'class' type).161///162/// \return True iff an original type in another TypeSystemClang was found.163/// Note: Does *not* return false if an original type was found but164/// no information was imported over.165///166/// \see ClangASTImporter::Import167bool Import(const CompilerType &type);168169bool CompleteType(const CompilerType &compiler_type);170171bool CompleteTagDecl(clang::TagDecl *decl);172173bool CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin);174175bool CompleteObjCInterfaceDecl(clang::ObjCInterfaceDecl *interface_decl);176177bool CompleteAndFetchChildren(clang::QualType type);178179bool RequireCompleteType(clang::QualType type);180181/// Updates the internal origin-tracking information so that the given182/// 'original' decl is from now on used to import additional information183/// into the given decl.184///185/// Usually the origin-tracking in the ClangASTImporter is automatically186/// updated when a declaration is imported, so the only valid reason to ever187/// call this is if there is a 'better' original decl and the target decl188/// is only a shallow clone that lacks any contents.189void SetDeclOrigin(const clang::Decl *decl, clang::Decl *original_decl);190191ClangASTMetadata *GetDeclMetadata(const clang::Decl *decl);192193//194// Namespace maps195//196197typedef std::pair<lldb::ModuleSP, CompilerDeclContext> NamespaceMapItem;198typedef std::vector<NamespaceMapItem> NamespaceMap;199typedef std::shared_ptr<NamespaceMap> NamespaceMapSP;200201void RegisterNamespaceMap(const clang::NamespaceDecl *decl,202NamespaceMapSP &namespace_map);203204NamespaceMapSP GetNamespaceMap(const clang::NamespaceDecl *decl);205206void BuildNamespaceMap(const clang::NamespaceDecl *decl);207208//209// Completers for maps210//211212class MapCompleter {213public:214virtual ~MapCompleter();215216virtual void CompleteNamespaceMap(NamespaceMapSP &namespace_map,217ConstString name,218NamespaceMapSP &parent_map) const = 0;219};220221void InstallMapCompleter(clang::ASTContext *dst_ctx,222MapCompleter &completer) {223ASTContextMetadataSP context_md;224ContextMetadataMap::iterator context_md_iter = m_metadata_map.find(dst_ctx);225226if (context_md_iter == m_metadata_map.end()) {227context_md = ASTContextMetadataSP(new ASTContextMetadata(dst_ctx));228m_metadata_map[dst_ctx] = context_md;229} else {230context_md = context_md_iter->second;231}232233context_md->m_map_completer = &completer;234}235236void ForgetDestination(clang::ASTContext *dst_ctx);237void ForgetSource(clang::ASTContext *dst_ctx, clang::ASTContext *src_ctx);238239struct DeclOrigin {240DeclOrigin() = default;241242DeclOrigin(clang::ASTContext *_ctx, clang::Decl *_decl)243: ctx(_ctx), decl(_decl) {244// The decl has to be in its associated ASTContext.245assert(_decl == nullptr || &_decl->getASTContext() == _ctx);246}247248DeclOrigin(const DeclOrigin &rhs) {249ctx = rhs.ctx;250decl = rhs.decl;251}252253void operator=(const DeclOrigin &rhs) {254ctx = rhs.ctx;255decl = rhs.decl;256}257258bool Valid() const { return (ctx != nullptr || decl != nullptr); }259260clang::ASTContext *ctx = nullptr;261clang::Decl *decl = nullptr;262};263264/// Listener interface used by the ASTImporterDelegate to inform other code265/// about decls that have been imported the first time.266struct NewDeclListener {267virtual ~NewDeclListener() = default;268/// A decl has been imported for the first time.269virtual void NewDeclImported(clang::Decl *from, clang::Decl *to) = 0;270};271272/// ASTImporter that intercepts and records the import process of the273/// underlying ASTImporter.274///275/// This class updates the map from declarations to their original276/// declarations and can record declarations that have been imported in a277/// certain interval.278///279/// When intercepting a declaration import, the ASTImporterDelegate uses the280/// CxxModuleHandler to replace any missing or malformed declarations with281/// their counterpart from a C++ module.282struct ASTImporterDelegate : public clang::ASTImporter {283ASTImporterDelegate(ClangASTImporter &main, clang::ASTContext *target_ctx,284clang::ASTContext *source_ctx)285: clang::ASTImporter(*target_ctx, main.m_file_manager, *source_ctx,286main.m_file_manager, true /*minimal*/),287m_main(main), m_source_ctx(source_ctx) {288// Target and source ASTContext shouldn't be identical. Importing AST289// nodes within the same AST doesn't make any sense as the whole idea290// is to import them to a different AST.291lldbassert(target_ctx != source_ctx && "Can't import into itself");292// This is always doing a minimal import of any declarations. This means293// that there has to be an ExternalASTSource in the target ASTContext294// (that should implement the callbacks that complete any declarations295// on demand). Without an ExternalASTSource, this ASTImporter will just296// do a minimal import and the imported declarations won't be completed.297assert(target_ctx->getExternalSource() && "Missing ExternalSource");298setODRHandling(clang::ASTImporter::ODRHandlingType::Liberal);299}300301/// Scope guard that attaches a CxxModuleHandler to an ASTImporterDelegate302/// and deattaches it at the end of the scope. Supports being used multiple303/// times on the same ASTImporterDelegate instance in nested scopes.304class CxxModuleScope {305/// The handler we attach to the ASTImporterDelegate.306CxxModuleHandler m_handler;307/// The ASTImporterDelegate we are supposed to attach the handler to.308ASTImporterDelegate &m_delegate;309/// True iff we attached the handler to the ASTImporterDelegate.310bool m_valid = false;311312public:313CxxModuleScope(ASTImporterDelegate &delegate, clang::ASTContext *dst_ctx)314: m_delegate(delegate) {315// If the delegate doesn't have a CxxModuleHandler yet, create one316// and attach it.317if (!delegate.m_std_handler) {318m_handler = CxxModuleHandler(delegate, dst_ctx);319m_valid = true;320delegate.m_std_handler = &m_handler;321}322}323~CxxModuleScope() {324if (m_valid) {325// Make sure no one messed with the handler we placed.326assert(m_delegate.m_std_handler == &m_handler);327m_delegate.m_std_handler = nullptr;328}329}330};331332void ImportDefinitionTo(clang::Decl *to, clang::Decl *from);333334void Imported(clang::Decl *from, clang::Decl *to) override;335336clang::Decl *GetOriginalDecl(clang::Decl *To) override;337338void SetImportListener(NewDeclListener *listener) {339assert(m_new_decl_listener == nullptr && "Already attached a listener?");340m_new_decl_listener = listener;341}342void RemoveImportListener() { m_new_decl_listener = nullptr; }343344protected:345llvm::Expected<clang::Decl *> ImportImpl(clang::Decl *From) override;346347private:348/// Decls we should ignore when mapping decls back to their original349/// ASTContext. Used by the CxxModuleHandler to mark declarations that350/// were created from the 'std' C++ module to prevent that the Importer351/// tries to sync them with the broken equivalent in the debug info AST.352llvm::SmallPtrSet<clang::Decl *, 16> m_decls_to_ignore;353ClangASTImporter &m_main;354clang::ASTContext *m_source_ctx;355CxxModuleHandler *m_std_handler = nullptr;356/// The currently attached listener.357NewDeclListener *m_new_decl_listener = nullptr;358};359360typedef std::shared_ptr<ASTImporterDelegate> ImporterDelegateSP;361typedef llvm::DenseMap<clang::ASTContext *, ImporterDelegateSP> DelegateMap;362typedef llvm::DenseMap<const clang::NamespaceDecl *, NamespaceMapSP>363NamespaceMetaMap;364365class ASTContextMetadata {366typedef llvm::DenseMap<const clang::Decl *, DeclOrigin> OriginMap;367368public:369ASTContextMetadata(clang::ASTContext *dst_ctx) : m_dst_ctx(dst_ctx) {}370371clang::ASTContext *m_dst_ctx;372DelegateMap m_delegates;373374NamespaceMetaMap m_namespace_maps;375MapCompleter *m_map_completer = nullptr;376377/// Sets the DeclOrigin for the given Decl and overwrites any existing378/// DeclOrigin.379void setOrigin(const clang::Decl *decl, DeclOrigin origin) {380// Setting the origin of any decl to itself (or to a different decl381// in the same ASTContext) doesn't make any sense. It will also cause382// ASTImporterDelegate::ImportImpl to infinite recurse when trying to find383// the 'original' Decl when importing code.384assert(&decl->getASTContext() != origin.ctx &&385"Trying to set decl origin to its own ASTContext?");386assert(decl != origin.decl && "Trying to set decl origin to itself?");387m_origins[decl] = origin;388}389390/// Removes any tracked DeclOrigin for the given decl.391void removeOrigin(const clang::Decl *decl) { m_origins.erase(decl); }392393/// Remove all DeclOrigin entries that point to the given ASTContext.394/// Useful when an ASTContext is about to be deleted and all the dangling395/// pointers to it need to be removed.396void removeOriginsWithContext(clang::ASTContext *ctx) {397for (OriginMap::iterator iter = m_origins.begin();398iter != m_origins.end();) {399if (iter->second.ctx == ctx)400m_origins.erase(iter++);401else402++iter;403}404}405406/// Returns the DeclOrigin for the given Decl or an invalid DeclOrigin407/// instance if there no known DeclOrigin for the given Decl.408DeclOrigin getOrigin(const clang::Decl *decl) const {409auto iter = m_origins.find(decl);410if (iter == m_origins.end())411return DeclOrigin();412return iter->second;413}414415/// Returns true there is a known DeclOrigin for the given Decl.416bool hasOrigin(const clang::Decl *decl) const {417return getOrigin(decl).Valid();418}419420private:421/// Maps declarations to the ASTContext/Decl from which they were imported422/// from. If a declaration is from an ASTContext which has been deleted423/// since the declaration was imported or the declaration wasn't created by424/// the ASTImporter, then it doesn't have a DeclOrigin and will not be425/// tracked here.426OriginMap m_origins;427};428429typedef std::shared_ptr<ASTContextMetadata> ASTContextMetadataSP;430typedef llvm::DenseMap<const clang::ASTContext *, ASTContextMetadataSP>431ContextMetadataMap;432433ContextMetadataMap m_metadata_map;434435ASTContextMetadataSP GetContextMetadata(clang::ASTContext *dst_ctx) {436ContextMetadataMap::iterator context_md_iter = m_metadata_map.find(dst_ctx);437438if (context_md_iter == m_metadata_map.end()) {439ASTContextMetadataSP context_md =440ASTContextMetadataSP(new ASTContextMetadata(dst_ctx));441m_metadata_map[dst_ctx] = context_md;442return context_md;443}444return context_md_iter->second;445}446447ASTContextMetadataSP MaybeGetContextMetadata(clang::ASTContext *dst_ctx) {448ContextMetadataMap::iterator context_md_iter = m_metadata_map.find(dst_ctx);449450if (context_md_iter != m_metadata_map.end())451return context_md_iter->second;452return ASTContextMetadataSP();453}454455ImporterDelegateSP GetDelegate(clang::ASTContext *dst_ctx,456clang::ASTContext *src_ctx) {457ASTContextMetadataSP context_md = GetContextMetadata(dst_ctx);458459DelegateMap &delegates = context_md->m_delegates;460DelegateMap::iterator delegate_iter = delegates.find(src_ctx);461462if (delegate_iter == delegates.end()) {463ImporterDelegateSP delegate =464ImporterDelegateSP(new ASTImporterDelegate(*this, dst_ctx, src_ctx));465delegates[src_ctx] = delegate;466return delegate;467}468return delegate_iter->second;469}470471DeclOrigin GetDeclOrigin(const clang::Decl *decl);472473clang::FileManager m_file_manager;474typedef llvm::DenseMap<const clang::RecordDecl *, LayoutInfo>475RecordDeclToLayoutMap;476477RecordDeclToLayoutMap m_record_decl_to_layout_map;478};479480template <class D> class TaggedASTDecl {481public:482TaggedASTDecl() : decl(nullptr) {}483TaggedASTDecl(D *_decl) : decl(_decl) {}484bool IsValid() const { return (decl != nullptr); }485bool IsInvalid() const { return !IsValid(); }486D *operator->() const { return decl; }487D *decl;488};489490template <class D2, template <class D> class TD, class D1>491TD<D2> DynCast(TD<D1> source) {492return TD<D2>(llvm::dyn_cast<D2>(source.decl));493}494495template <class D = clang::Decl> class DeclFromParser;496template <class D = clang::Decl> class DeclFromUser;497498template <class D> class DeclFromParser : public TaggedASTDecl<D> {499public:500DeclFromParser() : TaggedASTDecl<D>() {}501DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) {}502503DeclFromUser<D> GetOrigin(ClangASTImporter &importer);504};505506template <class D> class DeclFromUser : public TaggedASTDecl<D> {507public:508DeclFromUser() : TaggedASTDecl<D>() {}509DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) {}510511DeclFromParser<D> Import(clang::ASTContext *dest_ctx,512ClangASTImporter &importer);513};514515template <class D>516DeclFromUser<D> DeclFromParser<D>::GetOrigin(ClangASTImporter &importer) {517ClangASTImporter::DeclOrigin origin = importer.GetDeclOrigin(this->decl);518if (!origin.Valid())519return DeclFromUser<D>();520return DeclFromUser<D>(llvm::dyn_cast<D>(origin.decl));521}522523template <class D>524DeclFromParser<D> DeclFromUser<D>::Import(clang::ASTContext *dest_ctx,525ClangASTImporter &importer) {526DeclFromParser<> parser_generic_decl(importer.CopyDecl(dest_ctx, this->decl));527if (parser_generic_decl.IsInvalid())528return DeclFromParser<D>();529return DeclFromParser<D>(llvm::dyn_cast<D>(parser_generic_decl.decl));530}531532} // namespace lldb_private533534#endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGASTIMPORTER_H535536537