xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
15ffd83dbSDimitry Andric //===-- ClangASTImporter.h --------------------------------------*- C++ -*-===//
25ffd83dbSDimitry Andric //
35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ffd83dbSDimitry Andric //
75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
85ffd83dbSDimitry Andric 
95ffd83dbSDimitry Andric #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGASTIMPORTER_H
105ffd83dbSDimitry Andric #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGASTIMPORTER_H
115ffd83dbSDimitry Andric 
125ffd83dbSDimitry Andric #include <map>
135ffd83dbSDimitry Andric #include <memory>
145ffd83dbSDimitry Andric #include <set>
155ffd83dbSDimitry Andric #include <vector>
165ffd83dbSDimitry Andric 
17*0fca6ea1SDimitry Andric #include "clang/AST/ASTContext.h"
185ffd83dbSDimitry Andric #include "clang/AST/ASTImporter.h"
195ffd83dbSDimitry Andric #include "clang/AST/CharUnits.h"
205ffd83dbSDimitry Andric #include "clang/AST/Decl.h"
215ffd83dbSDimitry Andric #include "clang/AST/DeclCXX.h"
225ffd83dbSDimitry Andric #include "clang/Basic/FileManager.h"
235ffd83dbSDimitry Andric #include "clang/Basic/FileSystemOptions.h"
245ffd83dbSDimitry Andric 
255ffd83dbSDimitry Andric #include "lldb/Host/FileSystem.h"
265ffd83dbSDimitry Andric #include "lldb/Symbol/CompilerDeclContext.h"
27fe6060f1SDimitry Andric #include "lldb/Utility/LLDBAssert.h"
285ffd83dbSDimitry Andric #include "lldb/lldb-types.h"
295ffd83dbSDimitry Andric 
305ffd83dbSDimitry Andric #include "Plugins/ExpressionParser/Clang/CxxModuleHandler.h"
315ffd83dbSDimitry Andric 
325ffd83dbSDimitry Andric #include "llvm/ADT/DenseMap.h"
335ffd83dbSDimitry Andric 
345ffd83dbSDimitry Andric namespace lldb_private {
355ffd83dbSDimitry Andric 
365ffd83dbSDimitry Andric class ClangASTMetadata;
375ffd83dbSDimitry Andric class TypeSystemClang;
385ffd83dbSDimitry Andric 
39fe6060f1SDimitry Andric /// Manages and observes all Clang AST node importing in LLDB.
40fe6060f1SDimitry Andric ///
41fe6060f1SDimitry Andric /// The ClangASTImporter takes care of two things:
42fe6060f1SDimitry Andric ///
43fe6060f1SDimitry Andric /// 1. Keeps track of all ASTImporter instances in LLDB.
44fe6060f1SDimitry Andric ///
45fe6060f1SDimitry Andric /// Clang's ASTImporter takes care of importing types from one ASTContext to
46fe6060f1SDimitry Andric /// another. This class expands this concept by allowing copying from several
47fe6060f1SDimitry Andric /// ASTContext instances to several other ASTContext instances. Instead of
48fe6060f1SDimitry Andric /// constructing a new ASTImporter manually to copy over a type/decl, this class
49fe6060f1SDimitry Andric /// can be asked to do this. It will construct a ASTImporter for the caller (and
50fe6060f1SDimitry Andric /// will cache the ASTImporter instance for later use) and then perform the
51fe6060f1SDimitry Andric /// import.
52fe6060f1SDimitry Andric ///
53fe6060f1SDimitry Andric /// This mainly prevents that a caller might construct several ASTImporter
54fe6060f1SDimitry Andric /// instances for the same source/target ASTContext combination. As the
55fe6060f1SDimitry Andric /// ASTImporter has an internal state that keeps track of already imported
56fe6060f1SDimitry Andric /// declarations and so on, using only one ASTImporter instance is more
57fe6060f1SDimitry Andric /// efficient and less error-prone than using multiple.
58fe6060f1SDimitry Andric ///
59fe6060f1SDimitry Andric /// 2. Keeps track of from where declarations were imported (origin-tracking).
60fe6060f1SDimitry Andric /// The ASTImporter instances in this class usually only performa a minimal
61fe6060f1SDimitry Andric /// import, i.e., only a shallow copy is made that is filled out on demand
62fe6060f1SDimitry Andric /// when more information is requested later on. This requires record-keeping
63fe6060f1SDimitry Andric /// of where any shallow clone originally came from so that the right original
64fe6060f1SDimitry Andric /// declaration can be found and used as the source of any missing information.
655ffd83dbSDimitry Andric class ClangASTImporter {
665ffd83dbSDimitry Andric public:
675ffd83dbSDimitry Andric   struct LayoutInfo {
685ffd83dbSDimitry Andric     LayoutInfo() = default;
695ffd83dbSDimitry Andric     typedef llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
705ffd83dbSDimitry Andric         OffsetMap;
715ffd83dbSDimitry Andric 
725ffd83dbSDimitry Andric     uint64_t bit_size = 0;
735ffd83dbSDimitry Andric     uint64_t alignment = 0;
745ffd83dbSDimitry Andric     llvm::DenseMap<const clang::FieldDecl *, uint64_t> field_offsets;
755ffd83dbSDimitry Andric     OffsetMap base_offsets;
765ffd83dbSDimitry Andric     OffsetMap vbase_offsets;
775ffd83dbSDimitry Andric   };
785ffd83dbSDimitry Andric 
795ffd83dbSDimitry Andric   ClangASTImporter()
805ffd83dbSDimitry Andric       : m_file_manager(clang::FileSystemOptions(),
815ffd83dbSDimitry Andric                        FileSystem::Instance().GetVirtualFileSystem()) {}
825ffd83dbSDimitry Andric 
83fe6060f1SDimitry Andric   /// Copies the given type and the respective declarations to the destination
84fe6060f1SDimitry Andric   /// type system.
85fe6060f1SDimitry Andric   ///
86fe6060f1SDimitry Andric   /// This function does a shallow copy and requires that the target AST
87fe6060f1SDimitry Andric   /// has an ExternalASTSource which queries this ClangASTImporter instance
88fe6060f1SDimitry Andric   /// for any additional information that is maybe lacking in the shallow copy.
89fe6060f1SDimitry Andric   /// This also means that the type system of src_type can *not* be deleted
90fe6060f1SDimitry Andric   /// after this function has been called. If you need to delete the source
91fe6060f1SDimitry Andric   /// type system you either need to delete the destination type system first
92fe6060f1SDimitry Andric   /// or use \ref ClangASTImporter::DeportType.
93fe6060f1SDimitry Andric   ///
94fe6060f1SDimitry Andric   /// \see ClangASTImporter::DeportType
955ffd83dbSDimitry Andric   CompilerType CopyType(TypeSystemClang &dst, const CompilerType &src_type);
965ffd83dbSDimitry Andric 
97fe6060f1SDimitry Andric   /// \see ClangASTImporter::CopyType
985ffd83dbSDimitry Andric   clang::Decl *CopyDecl(clang::ASTContext *dst_ctx, clang::Decl *decl);
995ffd83dbSDimitry Andric 
100fe6060f1SDimitry Andric   /// Copies the given type and the respective declarations to the destination
101fe6060f1SDimitry Andric   /// type system.
102fe6060f1SDimitry Andric   ///
103fe6060f1SDimitry Andric   /// Unlike CopyType this function ensures that types/declarations which are
104fe6060f1SDimitry Andric   /// originally from the AST of src_type are fully copied over. The type
105fe6060f1SDimitry Andric   /// system of src_type can safely be deleted after calling this function.
106fe6060f1SDimitry Andric   /// \see ClangASTImporter::CopyType
1075ffd83dbSDimitry Andric   CompilerType DeportType(TypeSystemClang &dst, const CompilerType &src_type);
1085ffd83dbSDimitry Andric 
109fe6060f1SDimitry Andric   /// Copies the given decl to the destination type system.
110fe6060f1SDimitry Andric   /// \see ClangASTImporter::DeportType
1115ffd83dbSDimitry Andric   clang::Decl *DeportDecl(clang::ASTContext *dst_ctx, clang::Decl *decl);
1125ffd83dbSDimitry Andric 
1135ffd83dbSDimitry Andric   /// Sets the layout for the given RecordDecl. The layout will later be
1145ffd83dbSDimitry Andric   /// used by Clang's during code generation. Not calling this function for
1155ffd83dbSDimitry Andric   /// a RecordDecl will cause that Clang's codegen tries to layout the
1165ffd83dbSDimitry Andric   /// record by itself.
1175ffd83dbSDimitry Andric   ///
1185ffd83dbSDimitry Andric   /// \param decl The RecordDecl to set the layout for.
1195ffd83dbSDimitry Andric   /// \param layout The layout for the record.
1205ffd83dbSDimitry Andric   void SetRecordLayout(clang::RecordDecl *decl, const LayoutInfo &layout);
1215ffd83dbSDimitry Andric 
1225ffd83dbSDimitry Andric   bool LayoutRecordType(
1235ffd83dbSDimitry Andric       const clang::RecordDecl *record_decl, uint64_t &bit_size,
1245ffd83dbSDimitry Andric       uint64_t &alignment,
1255ffd83dbSDimitry Andric       llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
1265ffd83dbSDimitry Andric       llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
1275ffd83dbSDimitry Andric           &base_offsets,
1285ffd83dbSDimitry Andric       llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
1295ffd83dbSDimitry Andric           &vbase_offsets);
1305ffd83dbSDimitry Andric 
131*0fca6ea1SDimitry Andric   /// If \ref record has a valid origin, this function copies that
132*0fca6ea1SDimitry Andric   /// origin's layout into this ClangASTImporter instance.
133*0fca6ea1SDimitry Andric   ///
134*0fca6ea1SDimitry Andric   /// \param[in] record The decl whose layout we're calculating.
135*0fca6ea1SDimitry Andric   /// \param[out] size Size of \ref record in bytes.
136*0fca6ea1SDimitry Andric   /// \param[out] alignment Alignment of \ref record in bytes.
137*0fca6ea1SDimitry Andric   /// \param[out] field_offsets Offsets of fields of \ref record.
138*0fca6ea1SDimitry Andric   /// \param[out] base_offsets Offsets of base classes of \ref record.
139*0fca6ea1SDimitry Andric   /// \param[out] vbase_offsets Offsets of virtual base classes of \ref record.
140*0fca6ea1SDimitry Andric   ///
141*0fca6ea1SDimitry Andric   /// \returns Returns 'false' if no valid origin was found for \ref record or
142*0fca6ea1SDimitry Andric   /// this function failed to import the layout from the origin. Otherwise,
143*0fca6ea1SDimitry Andric   /// returns 'true' and the offsets/size/alignment are valid for use.
144*0fca6ea1SDimitry Andric   bool importRecordLayoutFromOrigin(
145*0fca6ea1SDimitry Andric       const clang::RecordDecl *record, uint64_t &size, uint64_t &alignment,
146*0fca6ea1SDimitry Andric       llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
147*0fca6ea1SDimitry Andric       llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
148*0fca6ea1SDimitry Andric           &base_offsets,
149*0fca6ea1SDimitry Andric       llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
150*0fca6ea1SDimitry Andric           &vbase_offsets);
151*0fca6ea1SDimitry Andric 
152fe6060f1SDimitry Andric   /// Returns true iff the given type was copied from another TypeSystemClang
153fe6060f1SDimitry Andric   /// and the original type in this other TypeSystemClang might contain
154fe6060f1SDimitry Andric   /// additional information (e.g., the definition of a 'class' type) that could
155fe6060f1SDimitry Andric   /// be imported.
156fe6060f1SDimitry Andric   ///
157fe6060f1SDimitry Andric   /// \see ClangASTImporter::Import
1585ffd83dbSDimitry Andric   bool CanImport(const CompilerType &type);
1595ffd83dbSDimitry Andric 
160fe6060f1SDimitry Andric   /// If the given type was copied from another TypeSystemClang then copy over
161fe6060f1SDimitry Andric   /// all missing information (e.g., the definition of a 'class' type).
162fe6060f1SDimitry Andric   ///
163fe6060f1SDimitry Andric   /// \return True iff an original type in another TypeSystemClang was found.
164fe6060f1SDimitry Andric   ///         Note: Does *not* return false if an original type was found but
165fe6060f1SDimitry Andric   ///               no information was imported over.
166fe6060f1SDimitry Andric   ///
167fe6060f1SDimitry Andric   /// \see ClangASTImporter::Import
1685ffd83dbSDimitry Andric   bool Import(const CompilerType &type);
1695ffd83dbSDimitry Andric 
1705ffd83dbSDimitry Andric   bool CompleteType(const CompilerType &compiler_type);
1715ffd83dbSDimitry Andric 
1725ffd83dbSDimitry Andric   bool CompleteTagDecl(clang::TagDecl *decl);
1735ffd83dbSDimitry Andric 
1745ffd83dbSDimitry Andric   bool CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin);
1755ffd83dbSDimitry Andric 
1765ffd83dbSDimitry Andric   bool CompleteObjCInterfaceDecl(clang::ObjCInterfaceDecl *interface_decl);
1775ffd83dbSDimitry Andric 
1785ffd83dbSDimitry Andric   bool CompleteAndFetchChildren(clang::QualType type);
1795ffd83dbSDimitry Andric 
1805ffd83dbSDimitry Andric   bool RequireCompleteType(clang::QualType type);
1815ffd83dbSDimitry Andric 
182fe6060f1SDimitry Andric   /// Updates the internal origin-tracking information so that the given
183fe6060f1SDimitry Andric   /// 'original' decl is from now on used to import additional information
184fe6060f1SDimitry Andric   /// into the given decl.
185fe6060f1SDimitry Andric   ///
186fe6060f1SDimitry Andric   /// Usually the origin-tracking in the ClangASTImporter is automatically
187fe6060f1SDimitry Andric   /// updated when a declaration is imported, so the only valid reason to ever
188fe6060f1SDimitry Andric   /// call this is if there is a 'better' original decl and the target decl
189fe6060f1SDimitry Andric   /// is only a shallow clone that lacks any contents.
1905ffd83dbSDimitry Andric   void SetDeclOrigin(const clang::Decl *decl, clang::Decl *original_decl);
1915ffd83dbSDimitry Andric 
1925ffd83dbSDimitry Andric   ClangASTMetadata *GetDeclMetadata(const clang::Decl *decl);
1935ffd83dbSDimitry Andric 
1945ffd83dbSDimitry Andric   //
1955ffd83dbSDimitry Andric   // Namespace maps
1965ffd83dbSDimitry Andric   //
1975ffd83dbSDimitry Andric 
1985ffd83dbSDimitry Andric   typedef std::pair<lldb::ModuleSP, CompilerDeclContext> NamespaceMapItem;
1995ffd83dbSDimitry Andric   typedef std::vector<NamespaceMapItem> NamespaceMap;
2005ffd83dbSDimitry Andric   typedef std::shared_ptr<NamespaceMap> NamespaceMapSP;
2015ffd83dbSDimitry Andric 
2025ffd83dbSDimitry Andric   void RegisterNamespaceMap(const clang::NamespaceDecl *decl,
2035ffd83dbSDimitry Andric                             NamespaceMapSP &namespace_map);
2045ffd83dbSDimitry Andric 
2055ffd83dbSDimitry Andric   NamespaceMapSP GetNamespaceMap(const clang::NamespaceDecl *decl);
2065ffd83dbSDimitry Andric 
2075ffd83dbSDimitry Andric   void BuildNamespaceMap(const clang::NamespaceDecl *decl);
2085ffd83dbSDimitry Andric 
2095ffd83dbSDimitry Andric   //
2105ffd83dbSDimitry Andric   // Completers for maps
2115ffd83dbSDimitry Andric   //
2125ffd83dbSDimitry Andric 
2135ffd83dbSDimitry Andric   class MapCompleter {
2145ffd83dbSDimitry Andric   public:
2155ffd83dbSDimitry Andric     virtual ~MapCompleter();
2165ffd83dbSDimitry Andric 
2175ffd83dbSDimitry Andric     virtual void CompleteNamespaceMap(NamespaceMapSP &namespace_map,
2185ffd83dbSDimitry Andric                                       ConstString name,
2195ffd83dbSDimitry Andric                                       NamespaceMapSP &parent_map) const = 0;
2205ffd83dbSDimitry Andric   };
2215ffd83dbSDimitry Andric 
2225ffd83dbSDimitry Andric   void InstallMapCompleter(clang::ASTContext *dst_ctx,
2235ffd83dbSDimitry Andric                            MapCompleter &completer) {
2245ffd83dbSDimitry Andric     ASTContextMetadataSP context_md;
2255ffd83dbSDimitry Andric     ContextMetadataMap::iterator context_md_iter = m_metadata_map.find(dst_ctx);
2265ffd83dbSDimitry Andric 
2275ffd83dbSDimitry Andric     if (context_md_iter == m_metadata_map.end()) {
2285ffd83dbSDimitry Andric       context_md = ASTContextMetadataSP(new ASTContextMetadata(dst_ctx));
2295ffd83dbSDimitry Andric       m_metadata_map[dst_ctx] = context_md;
2305ffd83dbSDimitry Andric     } else {
2315ffd83dbSDimitry Andric       context_md = context_md_iter->second;
2325ffd83dbSDimitry Andric     }
2335ffd83dbSDimitry Andric 
2345ffd83dbSDimitry Andric     context_md->m_map_completer = &completer;
2355ffd83dbSDimitry Andric   }
2365ffd83dbSDimitry Andric 
2375ffd83dbSDimitry Andric   void ForgetDestination(clang::ASTContext *dst_ctx);
2385ffd83dbSDimitry Andric   void ForgetSource(clang::ASTContext *dst_ctx, clang::ASTContext *src_ctx);
2395ffd83dbSDimitry Andric 
2405ffd83dbSDimitry Andric   struct DeclOrigin {
241fe6060f1SDimitry Andric     DeclOrigin() = default;
2425ffd83dbSDimitry Andric 
2435ffd83dbSDimitry Andric     DeclOrigin(clang::ASTContext *_ctx, clang::Decl *_decl)
244fe6060f1SDimitry Andric         : ctx(_ctx), decl(_decl) {
245fe6060f1SDimitry Andric       // The decl has to be in its associated ASTContext.
246fe6060f1SDimitry Andric       assert(_decl == nullptr || &_decl->getASTContext() == _ctx);
247fe6060f1SDimitry Andric     }
2485ffd83dbSDimitry Andric 
2495ffd83dbSDimitry Andric     DeclOrigin(const DeclOrigin &rhs) {
2505ffd83dbSDimitry Andric       ctx = rhs.ctx;
2515ffd83dbSDimitry Andric       decl = rhs.decl;
2525ffd83dbSDimitry Andric     }
2535ffd83dbSDimitry Andric 
2545ffd83dbSDimitry Andric     void operator=(const DeclOrigin &rhs) {
2555ffd83dbSDimitry Andric       ctx = rhs.ctx;
2565ffd83dbSDimitry Andric       decl = rhs.decl;
2575ffd83dbSDimitry Andric     }
2585ffd83dbSDimitry Andric 
259e8d8bef9SDimitry Andric     bool Valid() const { return (ctx != nullptr || decl != nullptr); }
2605ffd83dbSDimitry Andric 
261fe6060f1SDimitry Andric     clang::ASTContext *ctx = nullptr;
262fe6060f1SDimitry Andric     clang::Decl *decl = nullptr;
2635ffd83dbSDimitry Andric   };
2645ffd83dbSDimitry Andric 
2655ffd83dbSDimitry Andric   /// Listener interface used by the ASTImporterDelegate to inform other code
2665ffd83dbSDimitry Andric   /// about decls that have been imported the first time.
2675ffd83dbSDimitry Andric   struct NewDeclListener {
2685ffd83dbSDimitry Andric     virtual ~NewDeclListener() = default;
2695ffd83dbSDimitry Andric     /// A decl has been imported for the first time.
2705ffd83dbSDimitry Andric     virtual void NewDeclImported(clang::Decl *from, clang::Decl *to) = 0;
2715ffd83dbSDimitry Andric   };
2725ffd83dbSDimitry Andric 
2735ffd83dbSDimitry Andric   /// ASTImporter that intercepts and records the import process of the
2745ffd83dbSDimitry Andric   /// underlying ASTImporter.
2755ffd83dbSDimitry Andric   ///
2765ffd83dbSDimitry Andric   /// This class updates the map from declarations to their original
2775ffd83dbSDimitry Andric   /// declarations and can record declarations that have been imported in a
2785ffd83dbSDimitry Andric   /// certain interval.
2795ffd83dbSDimitry Andric   ///
2805ffd83dbSDimitry Andric   /// When intercepting a declaration import, the ASTImporterDelegate uses the
2815ffd83dbSDimitry Andric   /// CxxModuleHandler to replace any missing or malformed declarations with
2825ffd83dbSDimitry Andric   /// their counterpart from a C++ module.
2835ffd83dbSDimitry Andric   struct ASTImporterDelegate : public clang::ASTImporter {
284349cc55cSDimitry Andric     ASTImporterDelegate(ClangASTImporter &main, clang::ASTContext *target_ctx,
2855ffd83dbSDimitry Andric                         clang::ASTContext *source_ctx)
286349cc55cSDimitry Andric         : clang::ASTImporter(*target_ctx, main.m_file_manager, *source_ctx,
287349cc55cSDimitry Andric                              main.m_file_manager, true /*minimal*/),
288349cc55cSDimitry Andric           m_main(main), m_source_ctx(source_ctx) {
289fe6060f1SDimitry Andric       // Target and source ASTContext shouldn't be identical. Importing AST
290fe6060f1SDimitry Andric       // nodes within the same AST doesn't make any sense as the whole idea
291fe6060f1SDimitry Andric       // is to import them to a different AST.
292fe6060f1SDimitry Andric       lldbassert(target_ctx != source_ctx && "Can't import into itself");
293fe6060f1SDimitry Andric       // This is always doing a minimal import of any declarations. This means
294fe6060f1SDimitry Andric       // that there has to be an ExternalASTSource in the target ASTContext
295fe6060f1SDimitry Andric       // (that should implement the callbacks that complete any declarations
296fe6060f1SDimitry Andric       // on demand). Without an ExternalASTSource, this ASTImporter will just
297fe6060f1SDimitry Andric       // do a minimal import and the imported declarations won't be completed.
298fe6060f1SDimitry Andric       assert(target_ctx->getExternalSource() && "Missing ExternalSource");
2995ffd83dbSDimitry Andric       setODRHandling(clang::ASTImporter::ODRHandlingType::Liberal);
3005ffd83dbSDimitry Andric     }
3015ffd83dbSDimitry Andric 
3025ffd83dbSDimitry Andric     /// Scope guard that attaches a CxxModuleHandler to an ASTImporterDelegate
3035ffd83dbSDimitry Andric     /// and deattaches it at the end of the scope. Supports being used multiple
3045ffd83dbSDimitry Andric     /// times on the same ASTImporterDelegate instance in nested scopes.
3055ffd83dbSDimitry Andric     class CxxModuleScope {
3065ffd83dbSDimitry Andric       /// The handler we attach to the ASTImporterDelegate.
3075ffd83dbSDimitry Andric       CxxModuleHandler m_handler;
3085ffd83dbSDimitry Andric       /// The ASTImporterDelegate we are supposed to attach the handler to.
3095ffd83dbSDimitry Andric       ASTImporterDelegate &m_delegate;
3105ffd83dbSDimitry Andric       /// True iff we attached the handler to the ASTImporterDelegate.
3115ffd83dbSDimitry Andric       bool m_valid = false;
3125ffd83dbSDimitry Andric 
3135ffd83dbSDimitry Andric     public:
3145ffd83dbSDimitry Andric       CxxModuleScope(ASTImporterDelegate &delegate, clang::ASTContext *dst_ctx)
3155ffd83dbSDimitry Andric           : m_delegate(delegate) {
3165ffd83dbSDimitry Andric         // If the delegate doesn't have a CxxModuleHandler yet, create one
3175ffd83dbSDimitry Andric         // and attach it.
3185ffd83dbSDimitry Andric         if (!delegate.m_std_handler) {
3195ffd83dbSDimitry Andric           m_handler = CxxModuleHandler(delegate, dst_ctx);
3205ffd83dbSDimitry Andric           m_valid = true;
3215ffd83dbSDimitry Andric           delegate.m_std_handler = &m_handler;
3225ffd83dbSDimitry Andric         }
3235ffd83dbSDimitry Andric       }
3245ffd83dbSDimitry Andric       ~CxxModuleScope() {
3255ffd83dbSDimitry Andric         if (m_valid) {
3265ffd83dbSDimitry Andric           // Make sure no one messed with the handler we placed.
3275ffd83dbSDimitry Andric           assert(m_delegate.m_std_handler == &m_handler);
3285ffd83dbSDimitry Andric           m_delegate.m_std_handler = nullptr;
3295ffd83dbSDimitry Andric         }
3305ffd83dbSDimitry Andric       }
3315ffd83dbSDimitry Andric     };
3325ffd83dbSDimitry Andric 
3335ffd83dbSDimitry Andric     void ImportDefinitionTo(clang::Decl *to, clang::Decl *from);
3345ffd83dbSDimitry Andric 
3355ffd83dbSDimitry Andric     void Imported(clang::Decl *from, clang::Decl *to) override;
3365ffd83dbSDimitry Andric 
3375ffd83dbSDimitry Andric     clang::Decl *GetOriginalDecl(clang::Decl *To) override;
3385ffd83dbSDimitry Andric 
3395ffd83dbSDimitry Andric     void SetImportListener(NewDeclListener *listener) {
3405ffd83dbSDimitry Andric       assert(m_new_decl_listener == nullptr && "Already attached a listener?");
3415ffd83dbSDimitry Andric       m_new_decl_listener = listener;
3425ffd83dbSDimitry Andric     }
3435ffd83dbSDimitry Andric     void RemoveImportListener() { m_new_decl_listener = nullptr; }
3445ffd83dbSDimitry Andric 
3455ffd83dbSDimitry Andric   protected:
3465ffd83dbSDimitry Andric     llvm::Expected<clang::Decl *> ImportImpl(clang::Decl *From) override;
3475ffd83dbSDimitry Andric 
3485ffd83dbSDimitry Andric   private:
3495ffd83dbSDimitry Andric     /// Decls we should ignore when mapping decls back to their original
3505ffd83dbSDimitry Andric     /// ASTContext. Used by the CxxModuleHandler to mark declarations that
3515ffd83dbSDimitry Andric     /// were created from the 'std' C++ module to prevent that the Importer
3525ffd83dbSDimitry Andric     /// tries to sync them with the broken equivalent in the debug info AST.
3535ffd83dbSDimitry Andric     llvm::SmallPtrSet<clang::Decl *, 16> m_decls_to_ignore;
354349cc55cSDimitry Andric     ClangASTImporter &m_main;
3555ffd83dbSDimitry Andric     clang::ASTContext *m_source_ctx;
3565ffd83dbSDimitry Andric     CxxModuleHandler *m_std_handler = nullptr;
3575ffd83dbSDimitry Andric     /// The currently attached listener.
3585ffd83dbSDimitry Andric     NewDeclListener *m_new_decl_listener = nullptr;
3595ffd83dbSDimitry Andric   };
3605ffd83dbSDimitry Andric 
3615ffd83dbSDimitry Andric   typedef std::shared_ptr<ASTImporterDelegate> ImporterDelegateSP;
3625ffd83dbSDimitry Andric   typedef llvm::DenseMap<clang::ASTContext *, ImporterDelegateSP> DelegateMap;
3635ffd83dbSDimitry Andric   typedef llvm::DenseMap<const clang::NamespaceDecl *, NamespaceMapSP>
3645ffd83dbSDimitry Andric       NamespaceMetaMap;
3655ffd83dbSDimitry Andric 
366e8d8bef9SDimitry Andric   class ASTContextMetadata {
367e8d8bef9SDimitry Andric     typedef llvm::DenseMap<const clang::Decl *, DeclOrigin> OriginMap;
368e8d8bef9SDimitry Andric 
369e8d8bef9SDimitry Andric   public:
370e8d8bef9SDimitry Andric     ASTContextMetadata(clang::ASTContext *dst_ctx) : m_dst_ctx(dst_ctx) {}
3715ffd83dbSDimitry Andric 
3725ffd83dbSDimitry Andric     clang::ASTContext *m_dst_ctx;
3735ffd83dbSDimitry Andric     DelegateMap m_delegates;
3745ffd83dbSDimitry Andric 
3755ffd83dbSDimitry Andric     NamespaceMetaMap m_namespace_maps;
376e8d8bef9SDimitry Andric     MapCompleter *m_map_completer = nullptr;
377e8d8bef9SDimitry Andric 
378e8d8bef9SDimitry Andric     /// Sets the DeclOrigin for the given Decl and overwrites any existing
379e8d8bef9SDimitry Andric     /// DeclOrigin.
380e8d8bef9SDimitry Andric     void setOrigin(const clang::Decl *decl, DeclOrigin origin) {
381fe6060f1SDimitry Andric       // Setting the origin of any decl to itself (or to a different decl
382fe6060f1SDimitry Andric       // in the same ASTContext) doesn't make any sense. It will also cause
383fe6060f1SDimitry Andric       // ASTImporterDelegate::ImportImpl to infinite recurse when trying to find
384fe6060f1SDimitry Andric       // the 'original' Decl when importing code.
385fe6060f1SDimitry Andric       assert(&decl->getASTContext() != origin.ctx &&
386fe6060f1SDimitry Andric              "Trying to set decl origin to its own ASTContext?");
387fe6060f1SDimitry Andric       assert(decl != origin.decl && "Trying to set decl origin to itself?");
388e8d8bef9SDimitry Andric       m_origins[decl] = origin;
389e8d8bef9SDimitry Andric     }
390e8d8bef9SDimitry Andric 
391e8d8bef9SDimitry Andric     /// Removes any tracked DeclOrigin for the given decl.
392e8d8bef9SDimitry Andric     void removeOrigin(const clang::Decl *decl) { m_origins.erase(decl); }
393e8d8bef9SDimitry Andric 
394e8d8bef9SDimitry Andric     /// Remove all DeclOrigin entries that point to the given ASTContext.
395e8d8bef9SDimitry Andric     /// Useful when an ASTContext is about to be deleted and all the dangling
396e8d8bef9SDimitry Andric     /// pointers to it need to be removed.
397e8d8bef9SDimitry Andric     void removeOriginsWithContext(clang::ASTContext *ctx) {
398e8d8bef9SDimitry Andric       for (OriginMap::iterator iter = m_origins.begin();
399e8d8bef9SDimitry Andric            iter != m_origins.end();) {
400e8d8bef9SDimitry Andric         if (iter->second.ctx == ctx)
401e8d8bef9SDimitry Andric           m_origins.erase(iter++);
402e8d8bef9SDimitry Andric         else
403e8d8bef9SDimitry Andric           ++iter;
404e8d8bef9SDimitry Andric       }
405e8d8bef9SDimitry Andric     }
406e8d8bef9SDimitry Andric 
407e8d8bef9SDimitry Andric     /// Returns the DeclOrigin for the given Decl or an invalid DeclOrigin
408e8d8bef9SDimitry Andric     /// instance if there no known DeclOrigin for the given Decl.
409e8d8bef9SDimitry Andric     DeclOrigin getOrigin(const clang::Decl *decl) const {
410e8d8bef9SDimitry Andric       auto iter = m_origins.find(decl);
411e8d8bef9SDimitry Andric       if (iter == m_origins.end())
412e8d8bef9SDimitry Andric         return DeclOrigin();
413e8d8bef9SDimitry Andric       return iter->second;
414e8d8bef9SDimitry Andric     }
415e8d8bef9SDimitry Andric 
416e8d8bef9SDimitry Andric     /// Returns true there is a known DeclOrigin for the given Decl.
417e8d8bef9SDimitry Andric     bool hasOrigin(const clang::Decl *decl) const {
418e8d8bef9SDimitry Andric       return getOrigin(decl).Valid();
419e8d8bef9SDimitry Andric     }
420e8d8bef9SDimitry Andric 
421e8d8bef9SDimitry Andric   private:
422e8d8bef9SDimitry Andric     /// Maps declarations to the ASTContext/Decl from which they were imported
423e8d8bef9SDimitry Andric     /// from. If a declaration is from an ASTContext which has been deleted
424e8d8bef9SDimitry Andric     /// since the declaration was imported or the declaration wasn't created by
425e8d8bef9SDimitry Andric     /// the ASTImporter, then it doesn't have a DeclOrigin and will not be
426e8d8bef9SDimitry Andric     /// tracked here.
427e8d8bef9SDimitry Andric     OriginMap m_origins;
4285ffd83dbSDimitry Andric   };
4295ffd83dbSDimitry Andric 
4305ffd83dbSDimitry Andric   typedef std::shared_ptr<ASTContextMetadata> ASTContextMetadataSP;
4315ffd83dbSDimitry Andric   typedef llvm::DenseMap<const clang::ASTContext *, ASTContextMetadataSP>
4325ffd83dbSDimitry Andric       ContextMetadataMap;
4335ffd83dbSDimitry Andric 
4345ffd83dbSDimitry Andric   ContextMetadataMap m_metadata_map;
4355ffd83dbSDimitry Andric 
4365ffd83dbSDimitry Andric   ASTContextMetadataSP GetContextMetadata(clang::ASTContext *dst_ctx) {
4375ffd83dbSDimitry Andric     ContextMetadataMap::iterator context_md_iter = m_metadata_map.find(dst_ctx);
4385ffd83dbSDimitry Andric 
4395ffd83dbSDimitry Andric     if (context_md_iter == m_metadata_map.end()) {
4405ffd83dbSDimitry Andric       ASTContextMetadataSP context_md =
4415ffd83dbSDimitry Andric           ASTContextMetadataSP(new ASTContextMetadata(dst_ctx));
4425ffd83dbSDimitry Andric       m_metadata_map[dst_ctx] = context_md;
4435ffd83dbSDimitry Andric       return context_md;
4445ffd83dbSDimitry Andric     }
4455ffd83dbSDimitry Andric     return context_md_iter->second;
4465ffd83dbSDimitry Andric   }
4475ffd83dbSDimitry Andric 
4485ffd83dbSDimitry Andric   ASTContextMetadataSP MaybeGetContextMetadata(clang::ASTContext *dst_ctx) {
4495ffd83dbSDimitry Andric     ContextMetadataMap::iterator context_md_iter = m_metadata_map.find(dst_ctx);
4505ffd83dbSDimitry Andric 
4515ffd83dbSDimitry Andric     if (context_md_iter != m_metadata_map.end())
4525ffd83dbSDimitry Andric       return context_md_iter->second;
4535ffd83dbSDimitry Andric     return ASTContextMetadataSP();
4545ffd83dbSDimitry Andric   }
4555ffd83dbSDimitry Andric 
4565ffd83dbSDimitry Andric   ImporterDelegateSP GetDelegate(clang::ASTContext *dst_ctx,
4575ffd83dbSDimitry Andric                                  clang::ASTContext *src_ctx) {
4585ffd83dbSDimitry Andric     ASTContextMetadataSP context_md = GetContextMetadata(dst_ctx);
4595ffd83dbSDimitry Andric 
4605ffd83dbSDimitry Andric     DelegateMap &delegates = context_md->m_delegates;
4615ffd83dbSDimitry Andric     DelegateMap::iterator delegate_iter = delegates.find(src_ctx);
4625ffd83dbSDimitry Andric 
4635ffd83dbSDimitry Andric     if (delegate_iter == delegates.end()) {
4645ffd83dbSDimitry Andric       ImporterDelegateSP delegate =
4655ffd83dbSDimitry Andric           ImporterDelegateSP(new ASTImporterDelegate(*this, dst_ctx, src_ctx));
4665ffd83dbSDimitry Andric       delegates[src_ctx] = delegate;
4675ffd83dbSDimitry Andric       return delegate;
4685ffd83dbSDimitry Andric     }
4695ffd83dbSDimitry Andric     return delegate_iter->second;
4705ffd83dbSDimitry Andric   }
4715ffd83dbSDimitry Andric 
4725ffd83dbSDimitry Andric   DeclOrigin GetDeclOrigin(const clang::Decl *decl);
4735ffd83dbSDimitry Andric 
4745ffd83dbSDimitry Andric   clang::FileManager m_file_manager;
4755ffd83dbSDimitry Andric   typedef llvm::DenseMap<const clang::RecordDecl *, LayoutInfo>
4765ffd83dbSDimitry Andric       RecordDeclToLayoutMap;
4775ffd83dbSDimitry Andric 
4785ffd83dbSDimitry Andric   RecordDeclToLayoutMap m_record_decl_to_layout_map;
4795ffd83dbSDimitry Andric };
4805ffd83dbSDimitry Andric 
481*0fca6ea1SDimitry Andric template <class D> class TaggedASTDecl {
482*0fca6ea1SDimitry Andric public:
483*0fca6ea1SDimitry Andric   TaggedASTDecl() : decl(nullptr) {}
484*0fca6ea1SDimitry Andric   TaggedASTDecl(D *_decl) : decl(_decl) {}
485*0fca6ea1SDimitry Andric   bool IsValid() const { return (decl != nullptr); }
486*0fca6ea1SDimitry Andric   bool IsInvalid() const { return !IsValid(); }
487*0fca6ea1SDimitry Andric   D *operator->() const { return decl; }
488*0fca6ea1SDimitry Andric   D *decl;
489*0fca6ea1SDimitry Andric };
490*0fca6ea1SDimitry Andric 
491*0fca6ea1SDimitry Andric template <class D2, template <class D> class TD, class D1>
492*0fca6ea1SDimitry Andric TD<D2> DynCast(TD<D1> source) {
493*0fca6ea1SDimitry Andric   return TD<D2>(llvm::dyn_cast<D2>(source.decl));
494*0fca6ea1SDimitry Andric }
495*0fca6ea1SDimitry Andric 
496*0fca6ea1SDimitry Andric template <class D = clang::Decl> class DeclFromParser;
497*0fca6ea1SDimitry Andric template <class D = clang::Decl> class DeclFromUser;
498*0fca6ea1SDimitry Andric 
499*0fca6ea1SDimitry Andric template <class D> class DeclFromParser : public TaggedASTDecl<D> {
500*0fca6ea1SDimitry Andric public:
501*0fca6ea1SDimitry Andric   DeclFromParser() : TaggedASTDecl<D>() {}
502*0fca6ea1SDimitry Andric   DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) {}
503*0fca6ea1SDimitry Andric 
504*0fca6ea1SDimitry Andric   DeclFromUser<D> GetOrigin(ClangASTImporter &importer);
505*0fca6ea1SDimitry Andric };
506*0fca6ea1SDimitry Andric 
507*0fca6ea1SDimitry Andric template <class D> class DeclFromUser : public TaggedASTDecl<D> {
508*0fca6ea1SDimitry Andric public:
509*0fca6ea1SDimitry Andric   DeclFromUser() : TaggedASTDecl<D>() {}
510*0fca6ea1SDimitry Andric   DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) {}
511*0fca6ea1SDimitry Andric 
512*0fca6ea1SDimitry Andric   DeclFromParser<D> Import(clang::ASTContext *dest_ctx,
513*0fca6ea1SDimitry Andric                            ClangASTImporter &importer);
514*0fca6ea1SDimitry Andric };
515*0fca6ea1SDimitry Andric 
516*0fca6ea1SDimitry Andric template <class D>
517*0fca6ea1SDimitry Andric DeclFromUser<D> DeclFromParser<D>::GetOrigin(ClangASTImporter &importer) {
518*0fca6ea1SDimitry Andric   ClangASTImporter::DeclOrigin origin = importer.GetDeclOrigin(this->decl);
519*0fca6ea1SDimitry Andric   if (!origin.Valid())
520*0fca6ea1SDimitry Andric     return DeclFromUser<D>();
521*0fca6ea1SDimitry Andric   return DeclFromUser<D>(llvm::dyn_cast<D>(origin.decl));
522*0fca6ea1SDimitry Andric }
523*0fca6ea1SDimitry Andric 
524*0fca6ea1SDimitry Andric template <class D>
525*0fca6ea1SDimitry Andric DeclFromParser<D> DeclFromUser<D>::Import(clang::ASTContext *dest_ctx,
526*0fca6ea1SDimitry Andric                                           ClangASTImporter &importer) {
527*0fca6ea1SDimitry Andric   DeclFromParser<> parser_generic_decl(importer.CopyDecl(dest_ctx, this->decl));
528*0fca6ea1SDimitry Andric   if (parser_generic_decl.IsInvalid())
529*0fca6ea1SDimitry Andric     return DeclFromParser<D>();
530*0fca6ea1SDimitry Andric   return DeclFromParser<D>(llvm::dyn_cast<D>(parser_generic_decl.decl));
531*0fca6ea1SDimitry Andric }
532*0fca6ea1SDimitry Andric 
5335ffd83dbSDimitry Andric } // namespace lldb_private
5345ffd83dbSDimitry Andric 
5355ffd83dbSDimitry Andric #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGASTIMPORTER_H
536