9#ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGASTIMPORTER_H
10#define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGASTIMPORTER_H
17#include "clang/AST/ASTContext.h"
18#include "clang/AST/ASTImporter.h"
19#include "clang/AST/CharUnits.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclCXX.h"
22#include "clang/Basic/FileManager.h"
23#include "clang/Basic/FileSystemOptions.h"
32#include "llvm/ADT/DenseMap.h"
36class ClangASTMetadata;
69 typedef llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
81 FileSystem::Instance().GetVirtualFileSystem()) {}
98 clang::Decl *
CopyDecl(clang::ASTContext *dst_ctx, clang::Decl *decl);
111 clang::Decl *
DeportDecl(clang::ASTContext *dst_ctx, clang::Decl *decl);
120 void SetRecordLayout(clang::RecordDecl *decl,
const LayoutInfo &layout);
123 const clang::RecordDecl *record_decl, uint64_t &bit_size,
125 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
126 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
128 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
145 const clang::RecordDecl *record, uint64_t &size, uint64_t &alignment,
146 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
147 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
149 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
190 void SetDeclOrigin(
const clang::Decl *decl, clang::Decl *original_decl);
192 std::optional<ClangASTMetadata>
GetDeclMetadata(
const clang::Decl *decl);
225 ContextMetadataMap::iterator context_md_iter =
m_metadata_map.find(dst_ctx);
231 context_md = context_md_iter->second;
234 context_md->m_map_completer = &completer;
238 void ForgetSource(clang::ASTContext *dst_ctx, clang::ASTContext *src_ctx);
246 assert(_decl ==
nullptr || &_decl->getASTContext() == _ctx);
261 clang::ASTContext *
ctx =
nullptr;
285 clang::ASTContext *source_ctx)
292 lldbassert(target_ctx != source_ctx &&
"Can't import into itself");
298 assert(target_ctx->getExternalSource() &&
"Missing ExternalSource");
299 setODRHandling(clang::ASTImporter::ODRHandlingType::Liberal);
335 void Imported(clang::Decl *from, clang::Decl *to)
override;
346 llvm::Expected<clang::Decl *>
ImportImpl(clang::Decl *From)
override;
362 typedef llvm::DenseMap<clang::ASTContext *, ImporterDelegateSP>
DelegateMap;
363 typedef llvm::DenseMap<const clang::NamespaceDecl *, NamespaceMapSP>
367 typedef llvm::DenseMap<const clang::Decl *, DeclOrigin>
OriginMap;
385 assert(&decl->getASTContext() != origin.
ctx &&
386 "Trying to set decl origin to its own ASTContext?");
387 assert(decl != origin.
decl &&
"Trying to set decl origin to itself?");
398 for (OriginMap::iterator iter =
m_origins.begin();
400 if (iter->second.ctx == ctx)
431 typedef llvm::DenseMap<const clang::ASTContext *, ASTContextMetadataSP>
437 ContextMetadataMap::iterator context_md_iter =
m_metadata_map.find(dst_ctx);
445 return context_md_iter->second;
449 ContextMetadataMap::iterator context_md_iter =
m_metadata_map.find(dst_ctx);
452 return context_md_iter->second;
457 clang::ASTContext *src_ctx) {
461 DelegateMap::iterator delegate_iter = delegates.find(src_ctx);
463 if (delegate_iter == delegates.end()) {
466 delegates[src_ctx] = delegate;
469 return delegate_iter->second;
475 typedef llvm::DenseMap<const clang::RecordDecl *, LayoutInfo>
491template <
class D2,
template <
class D>
class TD,
class D1>
493 return TD<D2>(llvm::dyn_cast<D2>(source.decl));
496template <
class D = clang::Decl>
class DeclFromParser;
497template <
class D = clang::Decl>
class DeclFromUser;
void removeOrigin(const clang::Decl *decl)
Removes any tracked DeclOrigin for the given decl.
void removeOriginsWithContext(clang::ASTContext *ctx)
Remove all DeclOrigin entries that point to the given ASTContext.
void setOrigin(const clang::Decl *decl, DeclOrigin origin)
Sets the DeclOrigin for the given Decl and overwrites any existing DeclOrigin.
MapCompleter * m_map_completer
OriginMap m_origins
Maps declarations to the ASTContext/Decl from which they were imported from.
clang::ASTContext * m_dst_ctx
bool hasOrigin(const clang::Decl *decl) const
Returns true there is a known DeclOrigin for the given Decl.
llvm::DenseMap< const clang::Decl *, DeclOrigin > OriginMap
ASTContextMetadata(clang::ASTContext *dst_ctx)
NamespaceMetaMap m_namespace_maps
DeclOrigin getOrigin(const clang::Decl *decl) const
Returns the DeclOrigin for the given Decl or an invalid DeclOrigin instance if there no known DeclOri...
Scope guard that attaches a CxxModuleHandler to an ASTImporterDelegate and deattaches it at the end o...
bool m_valid
True iff we attached the handler to the ASTImporterDelegate.
CxxModuleScope(ASTImporterDelegate &delegate, clang::ASTContext *dst_ctx)
CxxModuleHandler m_handler
The handler we attach to the ASTImporterDelegate.
ASTImporterDelegate & m_delegate
The ASTImporterDelegate we are supposed to attach the handler to.
virtual void CompleteNamespaceMap(NamespaceMapSP &namespace_map, ConstString name, NamespaceMapSP &parent_map) const =0
Manages and observes all Clang AST node importing in LLDB.
bool CompleteTagDecl(clang::TagDecl *decl)
std::optional< ClangASTMetadata > GetDeclMetadata(const clang::Decl *decl)
clang::Decl * DeportDecl(clang::ASTContext *dst_ctx, clang::Decl *decl)
Copies the given decl to the destination type system.
void BuildNamespaceMap(const clang::NamespaceDecl *decl)
void RegisterNamespaceMap(const clang::NamespaceDecl *decl, NamespaceMapSP &namespace_map)
void ForgetDestination(clang::ASTContext *dst_ctx)
CompilerType CopyType(TypeSystemClang &dst, const CompilerType &src_type)
Copies the given type and the respective declarations to the destination type system.
llvm::DenseMap< clang::ASTContext *, ImporterDelegateSP > DelegateMap
clang::Decl * CopyDecl(clang::ASTContext *dst_ctx, clang::Decl *decl)
DeclOrigin GetDeclOrigin(const clang::Decl *decl)
bool LayoutRecordType(const clang::RecordDecl *record_decl, uint64_t &bit_size, uint64_t &alignment, llvm::DenseMap< const clang::FieldDecl *, uint64_t > &field_offsets, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &base_offsets, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &vbase_offsets)
bool CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin)
bool CanImport(const CompilerType &type)
Returns true iff the given type was copied from another TypeSystemClang and the original type in this...
ASTContextMetadataSP GetContextMetadata(clang::ASTContext *dst_ctx)
bool importRecordLayoutFromOrigin(const clang::RecordDecl *record, uint64_t &size, uint64_t &alignment, llvm::DenseMap< const clang::FieldDecl *, uint64_t > &field_offsets, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &base_offsets, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &vbase_offsets)
If record has a valid origin, this function copies that origin's layout into this ClangASTImporter in...
void ForgetSource(clang::ASTContext *dst_ctx, clang::ASTContext *src_ctx)
bool CompleteObjCInterfaceDecl(clang::ObjCInterfaceDecl *interface_decl)
ASTContextMetadataSP MaybeGetContextMetadata(clang::ASTContext *dst_ctx)
void InstallMapCompleter(clang::ASTContext *dst_ctx, MapCompleter &completer)
std::shared_ptr< NamespaceMap > NamespaceMapSP
NamespaceMapSP GetNamespaceMap(const clang::NamespaceDecl *decl)
std::shared_ptr< ASTImporterDelegate > ImporterDelegateSP
llvm::DenseMap< const clang::NamespaceDecl *, NamespaceMapSP > NamespaceMetaMap
clang::FileManager m_file_manager
bool CompleteType(const CompilerType &compiler_type)
void SetRecordLayout(clang::RecordDecl *decl, const LayoutInfo &layout)
Sets the layout for the given RecordDecl.
ContextMetadataMap m_metadata_map
bool RequireCompleteType(clang::QualType type)
std::pair< lldb::ModuleSP, CompilerDeclContext > NamespaceMapItem
CompilerType DeportType(TypeSystemClang &dst, const CompilerType &src_type)
Copies the given type and the respective declarations to the destination type system.
llvm::DenseMap< const clang::RecordDecl *, LayoutInfo > RecordDeclToLayoutMap
RecordDeclToLayoutMap m_record_decl_to_layout_map
bool Import(const CompilerType &type)
If the given type was copied from another TypeSystemClang then copy over all missing information (e....
ImporterDelegateSP GetDelegate(clang::ASTContext *dst_ctx, clang::ASTContext *src_ctx)
void SetDeclOrigin(const clang::Decl *decl, clang::Decl *original_decl)
Updates the internal origin-tracking information so that the given 'original' decl is from now on use...
std::shared_ptr< ASTContextMetadata > ASTContextMetadataSP
bool CompleteAndFetchChildren(clang::QualType type)
llvm::DenseMap< const clang::ASTContext *, ASTContextMetadataSP > ContextMetadataMap
std::vector< NamespaceMapItem > NamespaceMap
Generic representation of a type in a programming language.
A uniqued constant string class.
Handles importing decls into an ASTContext with an attached C++ module.
DeclFromUser< D > GetOrigin(ClangASTImporter &importer)
DeclFromParser< D > Import(clang::ASTContext *dest_ctx, ClangASTImporter &importer)
A TypeSystem implementation based on Clang.
A class that represents a running process on the host machine.
TD< D2 > DynCast(TD< D1 > source)
ASTImporter that intercepts and records the import process of the underlying ASTImporter.
clang::Decl * GetOriginalDecl(clang::Decl *To) override
llvm::Expected< clang::Decl * > ImportImpl(clang::Decl *From) override
void RemoveImportListener()
llvm::SmallPtrSet< clang::Decl *, 16 > m_decls_to_ignore
Decls we should ignore when mapping decls back to their original ASTContext.
void SetImportListener(NewDeclListener *listener)
CxxModuleHandler * m_std_handler
void Imported(clang::Decl *from, clang::Decl *to) override
NewDeclListener * m_new_decl_listener
The currently attached listener.
ASTImporterDelegate(ClangASTImporter &main, clang::ASTContext *target_ctx, clang::ASTContext *source_ctx)
void ImportDefinitionTo(clang::Decl *to, clang::Decl *from)
clang::ASTContext * m_source_ctx
ClangASTImporter & m_main
DeclOrigin(clang::ASTContext *_ctx, clang::Decl *_decl)
DeclOrigin(const DeclOrigin &rhs)
void operator=(const DeclOrigin &rhs)
llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > OffsetMap
llvm::DenseMap< const clang::FieldDecl *, uint64_t > field_offsets
Listener interface used by the ASTImporterDelegate to inform other code about decls that have been im...
virtual ~NewDeclListener()=default
virtual void NewDeclImported(clang::Decl *from, clang::Decl *to)=0
A decl has been imported for the first time.