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 
175ffd83dbSDimitry Andric #include "clang/AST/ASTImporter.h"
185ffd83dbSDimitry Andric #include "clang/AST/CharUnits.h"
195ffd83dbSDimitry Andric #include "clang/AST/Decl.h"
205ffd83dbSDimitry Andric #include "clang/AST/DeclCXX.h"
215ffd83dbSDimitry Andric #include "clang/Basic/FileManager.h"
225ffd83dbSDimitry Andric #include "clang/Basic/FileSystemOptions.h"
235ffd83dbSDimitry Andric 
245ffd83dbSDimitry Andric #include "lldb/Host/FileSystem.h"
255ffd83dbSDimitry Andric #include "lldb/Symbol/CompilerDeclContext.h"
26fe6060f1SDimitry Andric #include "lldb/Utility/LLDBAssert.h"
275ffd83dbSDimitry Andric #include "lldb/lldb-types.h"
285ffd83dbSDimitry Andric 
295ffd83dbSDimitry Andric #include "Plugins/ExpressionParser/Clang/CxxModuleHandler.h"
305ffd83dbSDimitry Andric 
315ffd83dbSDimitry Andric #include "llvm/ADT/DenseMap.h"
325ffd83dbSDimitry Andric 
335ffd83dbSDimitry Andric namespace lldb_private {
345ffd83dbSDimitry Andric 
355ffd83dbSDimitry Andric class ClangASTMetadata;
365ffd83dbSDimitry Andric class TypeSystemClang;
375ffd83dbSDimitry Andric 
38fe6060f1SDimitry Andric /// Manages and observes all Clang AST node importing in LLDB.
39fe6060f1SDimitry Andric ///
40fe6060f1SDimitry Andric /// The ClangASTImporter takes care of two things:
41fe6060f1SDimitry Andric ///
42fe6060f1SDimitry Andric /// 1. Keeps track of all ASTImporter instances in LLDB.
43fe6060f1SDimitry Andric ///
44fe6060f1SDimitry Andric /// Clang's ASTImporter takes care of importing types from one ASTContext to
45fe6060f1SDimitry Andric /// another. This class expands this concept by allowing copying from several
46fe6060f1SDimitry Andric /// ASTContext instances to several other ASTContext instances. Instead of
47fe6060f1SDimitry Andric /// constructing a new ASTImporter manually to copy over a type/decl, this class
48fe6060f1SDimitry Andric /// can be asked to do this. It will construct a ASTImporter for the caller (and
49fe6060f1SDimitry Andric /// will cache the ASTImporter instance for later use) and then perform the
50fe6060f1SDimitry Andric /// import.
51fe6060f1SDimitry Andric ///
52fe6060f1SDimitry Andric /// This mainly prevents that a caller might construct several ASTImporter
53fe6060f1SDimitry Andric /// instances for the same source/target ASTContext combination. As the
54fe6060f1SDimitry Andric /// ASTImporter has an internal state that keeps track of already imported
55fe6060f1SDimitry Andric /// declarations and so on, using only one ASTImporter instance is more
56fe6060f1SDimitry Andric /// efficient and less error-prone than using multiple.
57fe6060f1SDimitry Andric ///
58fe6060f1SDimitry Andric /// 2. Keeps track of from where declarations were imported (origin-tracking).
59fe6060f1SDimitry Andric /// The ASTImporter instances in this class usually only performa a minimal
60fe6060f1SDimitry Andric /// import, i.e., only a shallow copy is made that is filled out on demand
61fe6060f1SDimitry Andric /// when more information is requested later on. This requires record-keeping
62fe6060f1SDimitry Andric /// of where any shallow clone originally came from so that the right original
63fe6060f1SDimitry Andric /// declaration can be found and used as the source of any missing information.
645ffd83dbSDimitry Andric class ClangASTImporter {
655ffd83dbSDimitry Andric public:
665ffd83dbSDimitry Andric   struct LayoutInfo {
675ffd83dbSDimitry Andric     LayoutInfo() = default;
685ffd83dbSDimitry Andric     typedef llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
695ffd83dbSDimitry Andric         OffsetMap;
705ffd83dbSDimitry Andric 
715ffd83dbSDimitry Andric     uint64_t bit_size = 0;
725ffd83dbSDimitry Andric     uint64_t alignment = 0;
735ffd83dbSDimitry Andric     llvm::DenseMap<const clang::FieldDecl *, uint64_t> field_offsets;
745ffd83dbSDimitry Andric     OffsetMap base_offsets;
755ffd83dbSDimitry Andric     OffsetMap vbase_offsets;
765ffd83dbSDimitry Andric   };
775ffd83dbSDimitry Andric 
ClangASTImporter()785ffd83dbSDimitry Andric   ClangASTImporter()
795ffd83dbSDimitry Andric       : m_file_manager(clang::FileSystemOptions(),
805ffd83dbSDimitry Andric                        FileSystem::Instance().GetVirtualFileSystem()) {}
815ffd83dbSDimitry Andric 
82fe6060f1SDimitry Andric   /// Copies the given type and the respective declarations to the destination
83fe6060f1SDimitry Andric   /// type system.
84fe6060f1SDimitry Andric   ///
85fe6060f1SDimitry Andric   /// This function does a shallow copy and requires that the target AST
86fe6060f1SDimitry Andric   /// has an ExternalASTSource which queries this ClangASTImporter instance
87fe6060f1SDimitry Andric   /// for any additional information that is maybe lacking in the shallow copy.
88fe6060f1SDimitry Andric   /// This also means that the type system of src_type can *not* be deleted
89fe6060f1SDimitry Andric   /// after this function has been called. If you need to delete the source
90fe6060f1SDimitry Andric   /// type system you either need to delete the destination type system first
91fe6060f1SDimitry Andric   /// or use \ref ClangASTImporter::DeportType.
92fe6060f1SDimitry Andric   ///
93fe6060f1SDimitry Andric   /// \see ClangASTImporter::DeportType
945ffd83dbSDimitry Andric   CompilerType CopyType(TypeSystemClang &dst, const CompilerType &src_type);
955ffd83dbSDimitry Andric 
96fe6060f1SDimitry Andric   /// \see ClangASTImporter::CopyType
975ffd83dbSDimitry Andric   clang::Decl *CopyDecl(clang::ASTContext *dst_ctx, clang::Decl *decl);
985ffd83dbSDimitry Andric 
99fe6060f1SDimitry Andric   /// Copies the given type and the respective declarations to the destination
100fe6060f1SDimitry Andric   /// type system.
101fe6060f1SDimitry Andric   ///
102fe6060f1SDimitry Andric   /// Unlike CopyType this function ensures that types/declarations which are
103fe6060f1SDimitry Andric   /// originally from the AST of src_type are fully copied over. The type
104fe6060f1SDimitry Andric   /// system of src_type can safely be deleted after calling this function.
105fe6060f1SDimitry Andric   /// \see ClangASTImporter::CopyType
1065ffd83dbSDimitry Andric   CompilerType DeportType(TypeSystemClang &dst, const CompilerType &src_type);
1075ffd83dbSDimitry Andric 
108fe6060f1SDimitry Andric   /// Copies the given decl to the destination type system.
109fe6060f1SDimitry Andric   /// \see ClangASTImporter::DeportType
1105ffd83dbSDimitry Andric   clang::Decl *DeportDecl(clang::ASTContext *dst_ctx, clang::Decl *decl);
1115ffd83dbSDimitry Andric 
1125ffd83dbSDimitry Andric   /// Sets the layout for the given RecordDecl. The layout will later be
1135ffd83dbSDimitry Andric   /// used by Clang's during code generation. Not calling this function for
1145ffd83dbSDimitry Andric   /// a RecordDecl will cause that Clang's codegen tries to layout the
1155ffd83dbSDimitry Andric   /// record by itself.
1165ffd83dbSDimitry Andric   ///
1175ffd83dbSDimitry Andric   /// \param decl The RecordDecl to set the layout for.
1185ffd83dbSDimitry Andric   /// \param layout The layout for the record.
1195ffd83dbSDimitry Andric   void SetRecordLayout(clang::RecordDecl *decl, const LayoutInfo &layout);
1205ffd83dbSDimitry Andric 
1215ffd83dbSDimitry Andric   bool LayoutRecordType(
1225ffd83dbSDimitry Andric       const clang::RecordDecl *record_decl, uint64_t &bit_size,
1235ffd83dbSDimitry Andric       uint64_t &alignment,
1245ffd83dbSDimitry Andric       llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
1255ffd83dbSDimitry Andric       llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
1265ffd83dbSDimitry Andric           &base_offsets,
1275ffd83dbSDimitry Andric       llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
1285ffd83dbSDimitry Andric           &vbase_offsets);
1295ffd83dbSDimitry Andric 
130fe6060f1SDimitry Andric   /// Returns true iff the given type was copied from another TypeSystemClang
131fe6060f1SDimitry Andric   /// and the original type in this other TypeSystemClang might contain
132fe6060f1SDimitry Andric   /// additional information (e.g., the definition of a 'class' type) that could
133fe6060f1SDimitry Andric   /// be imported.
134fe6060f1SDimitry Andric   ///
135fe6060f1SDimitry Andric   /// \see ClangASTImporter::Import
1365ffd83dbSDimitry Andric   bool CanImport(const CompilerType &type);
1375ffd83dbSDimitry Andric 
138fe6060f1SDimitry Andric   /// If the given type was copied from another TypeSystemClang then copy over
139fe6060f1SDimitry Andric   /// all missing information (e.g., the definition of a 'class' type).
140fe6060f1SDimitry Andric   ///
141fe6060f1SDimitry Andric   /// \return True iff an original type in another TypeSystemClang was found.
142fe6060f1SDimitry Andric   ///         Note: Does *not* return false if an original type was found but
143fe6060f1SDimitry Andric   ///               no information was imported over.
144fe6060f1SDimitry Andric   ///
145fe6060f1SDimitry Andric   /// \see ClangASTImporter::Import
1465ffd83dbSDimitry Andric   bool Import(const CompilerType &type);
1475ffd83dbSDimitry Andric 
1485ffd83dbSDimitry Andric   bool CompleteType(const CompilerType &compiler_type);
1495ffd83dbSDimitry Andric 
1505ffd83dbSDimitry Andric   bool CompleteTagDecl(clang::TagDecl *decl);
1515ffd83dbSDimitry Andric 
1525ffd83dbSDimitry Andric   bool CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin);
1535ffd83dbSDimitry Andric 
1545ffd83dbSDimitry Andric   bool CompleteObjCInterfaceDecl(clang::ObjCInterfaceDecl *interface_decl);
1555ffd83dbSDimitry Andric 
1565ffd83dbSDimitry Andric   bool CompleteAndFetchChildren(clang::QualType type);
1575ffd83dbSDimitry Andric 
1585ffd83dbSDimitry Andric   bool RequireCompleteType(clang::QualType type);
1595ffd83dbSDimitry Andric 
160fe6060f1SDimitry Andric   /// Updates the internal origin-tracking information so that the given
161fe6060f1SDimitry Andric   /// 'original' decl is from now on used to import additional information
162fe6060f1SDimitry Andric   /// into the given decl.
163fe6060f1SDimitry Andric   ///
164fe6060f1SDimitry Andric   /// Usually the origin-tracking in the ClangASTImporter is automatically
165fe6060f1SDimitry Andric   /// updated when a declaration is imported, so the only valid reason to ever
166fe6060f1SDimitry Andric   /// call this is if there is a 'better' original decl and the target decl
167fe6060f1SDimitry Andric   /// is only a shallow clone that lacks any contents.
1685ffd83dbSDimitry Andric   void SetDeclOrigin(const clang::Decl *decl, clang::Decl *original_decl);
1695ffd83dbSDimitry Andric 
1705ffd83dbSDimitry Andric   ClangASTMetadata *GetDeclMetadata(const clang::Decl *decl);
1715ffd83dbSDimitry Andric 
1725ffd83dbSDimitry Andric   //
1735ffd83dbSDimitry Andric   // Namespace maps
1745ffd83dbSDimitry Andric   //
1755ffd83dbSDimitry Andric 
1765ffd83dbSDimitry Andric   typedef std::pair<lldb::ModuleSP, CompilerDeclContext> NamespaceMapItem;
1775ffd83dbSDimitry Andric   typedef std::vector<NamespaceMapItem> NamespaceMap;
1785ffd83dbSDimitry Andric   typedef std::shared_ptr<NamespaceMap> NamespaceMapSP;
1795ffd83dbSDimitry Andric 
1805ffd83dbSDimitry Andric   void RegisterNamespaceMap(const clang::NamespaceDecl *decl,
1815ffd83dbSDimitry Andric                             NamespaceMapSP &namespace_map);
1825ffd83dbSDimitry Andric 
1835ffd83dbSDimitry Andric   NamespaceMapSP GetNamespaceMap(const clang::NamespaceDecl *decl);
1845ffd83dbSDimitry Andric 
1855ffd83dbSDimitry Andric   void BuildNamespaceMap(const clang::NamespaceDecl *decl);
1865ffd83dbSDimitry Andric 
1875ffd83dbSDimitry Andric   //
1885ffd83dbSDimitry Andric   // Completers for maps
1895ffd83dbSDimitry Andric   //
1905ffd83dbSDimitry Andric 
1915ffd83dbSDimitry Andric   class MapCompleter {
1925ffd83dbSDimitry Andric   public:
1935ffd83dbSDimitry Andric     virtual ~MapCompleter();
1945ffd83dbSDimitry Andric 
1955ffd83dbSDimitry Andric     virtual void CompleteNamespaceMap(NamespaceMapSP &namespace_map,
1965ffd83dbSDimitry Andric                                       ConstString name,
1975ffd83dbSDimitry Andric                                       NamespaceMapSP &parent_map) const = 0;
1985ffd83dbSDimitry Andric   };
1995ffd83dbSDimitry Andric 
InstallMapCompleter(clang::ASTContext * dst_ctx,MapCompleter & completer)2005ffd83dbSDimitry Andric   void InstallMapCompleter(clang::ASTContext *dst_ctx,
2015ffd83dbSDimitry Andric                            MapCompleter &completer) {
2025ffd83dbSDimitry Andric     ASTContextMetadataSP context_md;
2035ffd83dbSDimitry Andric     ContextMetadataMap::iterator context_md_iter = m_metadata_map.find(dst_ctx);
2045ffd83dbSDimitry Andric 
2055ffd83dbSDimitry Andric     if (context_md_iter == m_metadata_map.end()) {
2065ffd83dbSDimitry Andric       context_md = ASTContextMetadataSP(new ASTContextMetadata(dst_ctx));
2075ffd83dbSDimitry Andric       m_metadata_map[dst_ctx] = context_md;
2085ffd83dbSDimitry Andric     } else {
2095ffd83dbSDimitry Andric       context_md = context_md_iter->second;
2105ffd83dbSDimitry Andric     }
2115ffd83dbSDimitry Andric 
2125ffd83dbSDimitry Andric     context_md->m_map_completer = &completer;
2135ffd83dbSDimitry Andric   }
2145ffd83dbSDimitry Andric 
2155ffd83dbSDimitry Andric   void ForgetDestination(clang::ASTContext *dst_ctx);
2165ffd83dbSDimitry Andric   void ForgetSource(clang::ASTContext *dst_ctx, clang::ASTContext *src_ctx);
2175ffd83dbSDimitry Andric 
2185ffd83dbSDimitry Andric   struct DeclOrigin {
219fe6060f1SDimitry Andric     DeclOrigin() = default;
2205ffd83dbSDimitry Andric 
DeclOriginDeclOrigin2215ffd83dbSDimitry Andric     DeclOrigin(clang::ASTContext *_ctx, clang::Decl *_decl)
222fe6060f1SDimitry Andric         : ctx(_ctx), decl(_decl) {
223fe6060f1SDimitry Andric       // The decl has to be in its associated ASTContext.
224fe6060f1SDimitry Andric       assert(_decl == nullptr || &_decl->getASTContext() == _ctx);
225fe6060f1SDimitry Andric     }
2265ffd83dbSDimitry Andric 
DeclOriginDeclOrigin2275ffd83dbSDimitry Andric     DeclOrigin(const DeclOrigin &rhs) {
2285ffd83dbSDimitry Andric       ctx = rhs.ctx;
2295ffd83dbSDimitry Andric       decl = rhs.decl;
2305ffd83dbSDimitry Andric     }
2315ffd83dbSDimitry Andric 
2325ffd83dbSDimitry Andric     void operator=(const DeclOrigin &rhs) {
2335ffd83dbSDimitry Andric       ctx = rhs.ctx;
2345ffd83dbSDimitry Andric       decl = rhs.decl;
2355ffd83dbSDimitry Andric     }
2365ffd83dbSDimitry Andric 
ValidDeclOrigin237e8d8bef9SDimitry Andric     bool Valid() const { return (ctx != nullptr || decl != nullptr); }
2385ffd83dbSDimitry Andric 
239fe6060f1SDimitry Andric     clang::ASTContext *ctx = nullptr;
240fe6060f1SDimitry Andric     clang::Decl *decl = nullptr;
2415ffd83dbSDimitry Andric   };
2425ffd83dbSDimitry Andric 
2435ffd83dbSDimitry Andric   /// Listener interface used by the ASTImporterDelegate to inform other code
2445ffd83dbSDimitry Andric   /// about decls that have been imported the first time.
2455ffd83dbSDimitry Andric   struct NewDeclListener {
2465ffd83dbSDimitry Andric     virtual ~NewDeclListener() = default;
2475ffd83dbSDimitry Andric     /// A decl has been imported for the first time.
2485ffd83dbSDimitry Andric     virtual void NewDeclImported(clang::Decl *from, clang::Decl *to) = 0;
2495ffd83dbSDimitry Andric   };
2505ffd83dbSDimitry Andric 
2515ffd83dbSDimitry Andric   /// ASTImporter that intercepts and records the import process of the
2525ffd83dbSDimitry Andric   /// underlying ASTImporter.
2535ffd83dbSDimitry Andric   ///
2545ffd83dbSDimitry Andric   /// This class updates the map from declarations to their original
2555ffd83dbSDimitry Andric   /// declarations and can record declarations that have been imported in a
2565ffd83dbSDimitry Andric   /// certain interval.
2575ffd83dbSDimitry Andric   ///
2585ffd83dbSDimitry Andric   /// When intercepting a declaration import, the ASTImporterDelegate uses the
2595ffd83dbSDimitry Andric   /// CxxModuleHandler to replace any missing or malformed declarations with
2605ffd83dbSDimitry Andric   /// their counterpart from a C++ module.
2615ffd83dbSDimitry Andric   struct ASTImporterDelegate : public clang::ASTImporter {
ASTImporterDelegateASTImporterDelegate262*349cc55cSDimitry Andric     ASTImporterDelegate(ClangASTImporter &main, clang::ASTContext *target_ctx,
2635ffd83dbSDimitry Andric                         clang::ASTContext *source_ctx)
264*349cc55cSDimitry Andric         : clang::ASTImporter(*target_ctx, main.m_file_manager, *source_ctx,
265*349cc55cSDimitry Andric                              main.m_file_manager, true /*minimal*/),
266*349cc55cSDimitry Andric           m_main(main), m_source_ctx(source_ctx) {
267fe6060f1SDimitry Andric       // Target and source ASTContext shouldn't be identical. Importing AST
268fe6060f1SDimitry Andric       // nodes within the same AST doesn't make any sense as the whole idea
269fe6060f1SDimitry Andric       // is to import them to a different AST.
270fe6060f1SDimitry Andric       lldbassert(target_ctx != source_ctx && "Can't import into itself");
271fe6060f1SDimitry Andric       // This is always doing a minimal import of any declarations. This means
272fe6060f1SDimitry Andric       // that there has to be an ExternalASTSource in the target ASTContext
273fe6060f1SDimitry Andric       // (that should implement the callbacks that complete any declarations
274fe6060f1SDimitry Andric       // on demand). Without an ExternalASTSource, this ASTImporter will just
275fe6060f1SDimitry Andric       // do a minimal import and the imported declarations won't be completed.
276fe6060f1SDimitry Andric       assert(target_ctx->getExternalSource() && "Missing ExternalSource");
2775ffd83dbSDimitry Andric       setODRHandling(clang::ASTImporter::ODRHandlingType::Liberal);
2785ffd83dbSDimitry Andric     }
2795ffd83dbSDimitry Andric 
2805ffd83dbSDimitry Andric     /// Scope guard that attaches a CxxModuleHandler to an ASTImporterDelegate
2815ffd83dbSDimitry Andric     /// and deattaches it at the end of the scope. Supports being used multiple
2825ffd83dbSDimitry Andric     /// times on the same ASTImporterDelegate instance in nested scopes.
2835ffd83dbSDimitry Andric     class CxxModuleScope {
2845ffd83dbSDimitry Andric       /// The handler we attach to the ASTImporterDelegate.
2855ffd83dbSDimitry Andric       CxxModuleHandler m_handler;
2865ffd83dbSDimitry Andric       /// The ASTImporterDelegate we are supposed to attach the handler to.
2875ffd83dbSDimitry Andric       ASTImporterDelegate &m_delegate;
2885ffd83dbSDimitry Andric       /// True iff we attached the handler to the ASTImporterDelegate.
2895ffd83dbSDimitry Andric       bool m_valid = false;
2905ffd83dbSDimitry Andric 
2915ffd83dbSDimitry Andric     public:
CxxModuleScopeASTImporterDelegate2925ffd83dbSDimitry Andric       CxxModuleScope(ASTImporterDelegate &delegate, clang::ASTContext *dst_ctx)
2935ffd83dbSDimitry Andric           : m_delegate(delegate) {
2945ffd83dbSDimitry Andric         // If the delegate doesn't have a CxxModuleHandler yet, create one
2955ffd83dbSDimitry Andric         // and attach it.
2965ffd83dbSDimitry Andric         if (!delegate.m_std_handler) {
2975ffd83dbSDimitry Andric           m_handler = CxxModuleHandler(delegate, dst_ctx);
2985ffd83dbSDimitry Andric           m_valid = true;
2995ffd83dbSDimitry Andric           delegate.m_std_handler = &m_handler;
3005ffd83dbSDimitry Andric         }
3015ffd83dbSDimitry Andric       }
~CxxModuleScopeASTImporterDelegate3025ffd83dbSDimitry Andric       ~CxxModuleScope() {
3035ffd83dbSDimitry Andric         if (m_valid) {
3045ffd83dbSDimitry Andric           // Make sure no one messed with the handler we placed.
3055ffd83dbSDimitry Andric           assert(m_delegate.m_std_handler == &m_handler);
3065ffd83dbSDimitry Andric           m_delegate.m_std_handler = nullptr;
3075ffd83dbSDimitry Andric         }
3085ffd83dbSDimitry Andric       }
3095ffd83dbSDimitry Andric     };
3105ffd83dbSDimitry Andric 
3115ffd83dbSDimitry Andric     void ImportDefinitionTo(clang::Decl *to, clang::Decl *from);
3125ffd83dbSDimitry Andric 
3135ffd83dbSDimitry Andric     void Imported(clang::Decl *from, clang::Decl *to) override;
3145ffd83dbSDimitry Andric 
3155ffd83dbSDimitry Andric     clang::Decl *GetOriginalDecl(clang::Decl *To) override;
3165ffd83dbSDimitry Andric 
SetImportListenerASTImporterDelegate3175ffd83dbSDimitry Andric     void SetImportListener(NewDeclListener *listener) {
3185ffd83dbSDimitry Andric       assert(m_new_decl_listener == nullptr && "Already attached a listener?");
3195ffd83dbSDimitry Andric       m_new_decl_listener = listener;
3205ffd83dbSDimitry Andric     }
RemoveImportListenerASTImporterDelegate3215ffd83dbSDimitry Andric     void RemoveImportListener() { m_new_decl_listener = nullptr; }
3225ffd83dbSDimitry Andric 
3235ffd83dbSDimitry Andric   protected:
3245ffd83dbSDimitry Andric     llvm::Expected<clang::Decl *> ImportImpl(clang::Decl *From) override;
3255ffd83dbSDimitry Andric 
3265ffd83dbSDimitry Andric   private:
3275ffd83dbSDimitry Andric     /// Decls we should ignore when mapping decls back to their original
3285ffd83dbSDimitry Andric     /// ASTContext. Used by the CxxModuleHandler to mark declarations that
3295ffd83dbSDimitry Andric     /// were created from the 'std' C++ module to prevent that the Importer
3305ffd83dbSDimitry Andric     /// tries to sync them with the broken equivalent in the debug info AST.
3315ffd83dbSDimitry Andric     llvm::SmallPtrSet<clang::Decl *, 16> m_decls_to_ignore;
332*349cc55cSDimitry Andric     ClangASTImporter &m_main;
3335ffd83dbSDimitry Andric     clang::ASTContext *m_source_ctx;
3345ffd83dbSDimitry Andric     CxxModuleHandler *m_std_handler = nullptr;
3355ffd83dbSDimitry Andric     /// The currently attached listener.
3365ffd83dbSDimitry Andric     NewDeclListener *m_new_decl_listener = nullptr;
3375ffd83dbSDimitry Andric   };
3385ffd83dbSDimitry Andric 
3395ffd83dbSDimitry Andric   typedef std::shared_ptr<ASTImporterDelegate> ImporterDelegateSP;
3405ffd83dbSDimitry Andric   typedef llvm::DenseMap<clang::ASTContext *, ImporterDelegateSP> DelegateMap;
3415ffd83dbSDimitry Andric   typedef llvm::DenseMap<const clang::NamespaceDecl *, NamespaceMapSP>
3425ffd83dbSDimitry Andric       NamespaceMetaMap;
3435ffd83dbSDimitry Andric 
344e8d8bef9SDimitry Andric   class ASTContextMetadata {
345e8d8bef9SDimitry Andric     typedef llvm::DenseMap<const clang::Decl *, DeclOrigin> OriginMap;
346e8d8bef9SDimitry Andric 
347e8d8bef9SDimitry Andric   public:
ASTContextMetadata(clang::ASTContext * dst_ctx)348e8d8bef9SDimitry Andric     ASTContextMetadata(clang::ASTContext *dst_ctx) : m_dst_ctx(dst_ctx) {}
3495ffd83dbSDimitry Andric 
3505ffd83dbSDimitry Andric     clang::ASTContext *m_dst_ctx;
3515ffd83dbSDimitry Andric     DelegateMap m_delegates;
3525ffd83dbSDimitry Andric 
3535ffd83dbSDimitry Andric     NamespaceMetaMap m_namespace_maps;
354e8d8bef9SDimitry Andric     MapCompleter *m_map_completer = nullptr;
355e8d8bef9SDimitry Andric 
356e8d8bef9SDimitry Andric     /// Sets the DeclOrigin for the given Decl and overwrites any existing
357e8d8bef9SDimitry Andric     /// DeclOrigin.
setOrigin(const clang::Decl * decl,DeclOrigin origin)358e8d8bef9SDimitry Andric     void setOrigin(const clang::Decl *decl, DeclOrigin origin) {
359fe6060f1SDimitry Andric       // Setting the origin of any decl to itself (or to a different decl
360fe6060f1SDimitry Andric       // in the same ASTContext) doesn't make any sense. It will also cause
361fe6060f1SDimitry Andric       // ASTImporterDelegate::ImportImpl to infinite recurse when trying to find
362fe6060f1SDimitry Andric       // the 'original' Decl when importing code.
363fe6060f1SDimitry Andric       assert(&decl->getASTContext() != origin.ctx &&
364fe6060f1SDimitry Andric              "Trying to set decl origin to its own ASTContext?");
365fe6060f1SDimitry Andric       assert(decl != origin.decl && "Trying to set decl origin to itself?");
366e8d8bef9SDimitry Andric       m_origins[decl] = origin;
367e8d8bef9SDimitry Andric     }
368e8d8bef9SDimitry Andric 
369e8d8bef9SDimitry Andric     /// Removes any tracked DeclOrigin for the given decl.
removeOrigin(const clang::Decl * decl)370e8d8bef9SDimitry Andric     void removeOrigin(const clang::Decl *decl) { m_origins.erase(decl); }
371e8d8bef9SDimitry Andric 
372e8d8bef9SDimitry Andric     /// Remove all DeclOrigin entries that point to the given ASTContext.
373e8d8bef9SDimitry Andric     /// Useful when an ASTContext is about to be deleted and all the dangling
374e8d8bef9SDimitry Andric     /// pointers to it need to be removed.
removeOriginsWithContext(clang::ASTContext * ctx)375e8d8bef9SDimitry Andric     void removeOriginsWithContext(clang::ASTContext *ctx) {
376e8d8bef9SDimitry Andric       for (OriginMap::iterator iter = m_origins.begin();
377e8d8bef9SDimitry Andric            iter != m_origins.end();) {
378e8d8bef9SDimitry Andric         if (iter->second.ctx == ctx)
379e8d8bef9SDimitry Andric           m_origins.erase(iter++);
380e8d8bef9SDimitry Andric         else
381e8d8bef9SDimitry Andric           ++iter;
382e8d8bef9SDimitry Andric       }
383e8d8bef9SDimitry Andric     }
384e8d8bef9SDimitry Andric 
385e8d8bef9SDimitry Andric     /// Returns the DeclOrigin for the given Decl or an invalid DeclOrigin
386e8d8bef9SDimitry Andric     /// instance if there no known DeclOrigin for the given Decl.
getOrigin(const clang::Decl * decl)387e8d8bef9SDimitry Andric     DeclOrigin getOrigin(const clang::Decl *decl) const {
388e8d8bef9SDimitry Andric       auto iter = m_origins.find(decl);
389e8d8bef9SDimitry Andric       if (iter == m_origins.end())
390e8d8bef9SDimitry Andric         return DeclOrigin();
391e8d8bef9SDimitry Andric       return iter->second;
392e8d8bef9SDimitry Andric     }
393e8d8bef9SDimitry Andric 
394e8d8bef9SDimitry Andric     /// Returns true there is a known DeclOrigin for the given Decl.
hasOrigin(const clang::Decl * decl)395e8d8bef9SDimitry Andric     bool hasOrigin(const clang::Decl *decl) const {
396e8d8bef9SDimitry Andric       return getOrigin(decl).Valid();
397e8d8bef9SDimitry Andric     }
398e8d8bef9SDimitry Andric 
399e8d8bef9SDimitry Andric   private:
400e8d8bef9SDimitry Andric     /// Maps declarations to the ASTContext/Decl from which they were imported
401e8d8bef9SDimitry Andric     /// from. If a declaration is from an ASTContext which has been deleted
402e8d8bef9SDimitry Andric     /// since the declaration was imported or the declaration wasn't created by
403e8d8bef9SDimitry Andric     /// the ASTImporter, then it doesn't have a DeclOrigin and will not be
404e8d8bef9SDimitry Andric     /// tracked here.
405e8d8bef9SDimitry Andric     OriginMap m_origins;
4065ffd83dbSDimitry Andric   };
4075ffd83dbSDimitry Andric 
4085ffd83dbSDimitry Andric   typedef std::shared_ptr<ASTContextMetadata> ASTContextMetadataSP;
4095ffd83dbSDimitry Andric   typedef llvm::DenseMap<const clang::ASTContext *, ASTContextMetadataSP>
4105ffd83dbSDimitry Andric       ContextMetadataMap;
4115ffd83dbSDimitry Andric 
4125ffd83dbSDimitry Andric   ContextMetadataMap m_metadata_map;
4135ffd83dbSDimitry Andric 
GetContextMetadata(clang::ASTContext * dst_ctx)4145ffd83dbSDimitry Andric   ASTContextMetadataSP GetContextMetadata(clang::ASTContext *dst_ctx) {
4155ffd83dbSDimitry Andric     ContextMetadataMap::iterator context_md_iter = m_metadata_map.find(dst_ctx);
4165ffd83dbSDimitry Andric 
4175ffd83dbSDimitry Andric     if (context_md_iter == m_metadata_map.end()) {
4185ffd83dbSDimitry Andric       ASTContextMetadataSP context_md =
4195ffd83dbSDimitry Andric           ASTContextMetadataSP(new ASTContextMetadata(dst_ctx));
4205ffd83dbSDimitry Andric       m_metadata_map[dst_ctx] = context_md;
4215ffd83dbSDimitry Andric       return context_md;
4225ffd83dbSDimitry Andric     }
4235ffd83dbSDimitry Andric     return context_md_iter->second;
4245ffd83dbSDimitry Andric   }
4255ffd83dbSDimitry Andric 
MaybeGetContextMetadata(clang::ASTContext * dst_ctx)4265ffd83dbSDimitry Andric   ASTContextMetadataSP MaybeGetContextMetadata(clang::ASTContext *dst_ctx) {
4275ffd83dbSDimitry Andric     ContextMetadataMap::iterator context_md_iter = m_metadata_map.find(dst_ctx);
4285ffd83dbSDimitry Andric 
4295ffd83dbSDimitry Andric     if (context_md_iter != m_metadata_map.end())
4305ffd83dbSDimitry Andric       return context_md_iter->second;
4315ffd83dbSDimitry Andric     return ASTContextMetadataSP();
4325ffd83dbSDimitry Andric   }
4335ffd83dbSDimitry Andric 
GetDelegate(clang::ASTContext * dst_ctx,clang::ASTContext * src_ctx)4345ffd83dbSDimitry Andric   ImporterDelegateSP GetDelegate(clang::ASTContext *dst_ctx,
4355ffd83dbSDimitry Andric                                  clang::ASTContext *src_ctx) {
4365ffd83dbSDimitry Andric     ASTContextMetadataSP context_md = GetContextMetadata(dst_ctx);
4375ffd83dbSDimitry Andric 
4385ffd83dbSDimitry Andric     DelegateMap &delegates = context_md->m_delegates;
4395ffd83dbSDimitry Andric     DelegateMap::iterator delegate_iter = delegates.find(src_ctx);
4405ffd83dbSDimitry Andric 
4415ffd83dbSDimitry Andric     if (delegate_iter == delegates.end()) {
4425ffd83dbSDimitry Andric       ImporterDelegateSP delegate =
4435ffd83dbSDimitry Andric           ImporterDelegateSP(new ASTImporterDelegate(*this, dst_ctx, src_ctx));
4445ffd83dbSDimitry Andric       delegates[src_ctx] = delegate;
4455ffd83dbSDimitry Andric       return delegate;
4465ffd83dbSDimitry Andric     }
4475ffd83dbSDimitry Andric     return delegate_iter->second;
4485ffd83dbSDimitry Andric   }
4495ffd83dbSDimitry Andric 
4505ffd83dbSDimitry Andric   DeclOrigin GetDeclOrigin(const clang::Decl *decl);
4515ffd83dbSDimitry Andric 
4525ffd83dbSDimitry Andric   clang::FileManager m_file_manager;
4535ffd83dbSDimitry Andric   typedef llvm::DenseMap<const clang::RecordDecl *, LayoutInfo>
4545ffd83dbSDimitry Andric       RecordDeclToLayoutMap;
4555ffd83dbSDimitry Andric 
4565ffd83dbSDimitry Andric   RecordDeclToLayoutMap m_record_decl_to_layout_map;
4575ffd83dbSDimitry Andric };
4585ffd83dbSDimitry Andric 
4595ffd83dbSDimitry Andric } // namespace lldb_private
4605ffd83dbSDimitry Andric 
4615ffd83dbSDimitry Andric #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGASTIMPORTER_H
462