13#include "clang/AST/ASTContext.h"
14#include "clang/AST/Decl.h"
15#include "clang/AST/DeclCXX.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/AST/RecordLayout.h"
18#include "clang/Sema/Lookup.h"
19#include "clang/Sema/Sema.h"
20#include "llvm/Support/raw_ostream.h"
44 clang::ASTContext &src_clang_ast = src_ast->getASTContext();
54 llvm::Expected<QualType> ret_or_error = delegate_sp->Import(src_qual_type);
58 "Couldn't import type: {0}");
65 return CompilerType(dst_ast.weak_from_this(), dst_clang_type);
73 clang::ASTContext *src_ast = &decl->getASTContext();
81 llvm::Expected<clang::Decl *> result = delegate_sp->Import(decl);
84 LLDB_LOG_ERROR(log, result.takeError(),
"Couldn't import decl: {0}");
88 user_id = metadata->GetUserID();
90 if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
92 " [ClangASTImporter] WARNING: Failed to import a {0} "
93 "'{1}', metadata {2}",
94 decl->getDeclKindName(), named_decl->getNameAsString(),
98 " [ClangASTImporter] WARNING: Failed to import a {0}, "
100 decl->getDeclKindName(), user_id);
122 m_backups[decl] = {decl->getDeclContext(), decl->getLexicalDeclContext()};
124 decl->setDeclContext(decl->getASTContext().getTranslationUnitDecl());
125 decl->setLexicalDeclContext(decl->getASTContext().getTranslationUnitDecl());
131 decl->invalidateCachedLinkage();
135 clang::Decl *decl, clang::DeclContext *base,
136 clang::DeclContext *(clang::Decl::*contextFromDecl)(),
137 clang::DeclContext *(clang::DeclContext::*contextFromContext)()) {
138 for (DeclContext *decl_ctx = (decl->*contextFromDecl)(); decl_ctx;
139 decl_ctx = (decl_ctx->*contextFromContext)()) {
140 if (decl_ctx == base) {
149 clang::DeclContext *base =
nullptr) {
154 &clang::DeclContext::getParent) ||
156 &clang::DeclContext::getLexicalParent)) {
160 base = clang::dyn_cast<clang::DeclContext>(decl);
167 if (clang::DeclContext *context =
168 clang::dyn_cast<clang::DeclContext>(decl)) {
169 for (clang::Decl *decl : context->decls()) {
171 return escaped_child;
184 " [ClangASTImporter] DeclContextOverride couldn't "
185 "override ({0}Decl*){1} - its child ({2}Decl*){3} escapes",
186 decl->getDeclKindName(), decl, escaped_child->getDeclKindName(),
198 for (DeclContext *decl_context = decl->getLexicalDeclContext();
199 decl_context; decl_context = decl_context->getLexicalParent()) {
200 DeclContext *redecl_context = decl_context->getRedeclContext();
202 if (llvm::isa<FunctionDecl>(redecl_context) &&
203 llvm::isa<TranslationUnitDecl>(redecl_context->getLexicalParent())) {
204 for (clang::Decl *child_decl : decl_context->decls()) {
212 for (
const std::pair<clang::Decl *, Backup> &backup :
m_backups) {
213 backup.first->setDeclContext(backup.second.decl_context);
214 backup.first->setLexicalDeclContext(backup.second.lexical_decl_context);
230 llvm::SetVector<NamedDecl *> m_decls_to_complete;
233 llvm::SmallPtrSet<NamedDecl *, 32> m_decls_already_completed;
234 clang::ASTContext *m_dst_ctx;
235 clang::ASTContext *m_src_ctx;
236 ClangASTImporter &importer;
240 lldb_private::ClangASTImporter::ASTContextMetadata
const &to_context_md) {
251 if (
auto *tag_decl = dyn_cast<TagDecl>(decl)) {
252 if (
auto *original_tag_decl = dyn_cast<TagDecl>(original_decl)) {
253 if (original_tag_decl->isCompleteDefinition()) {
254 m_delegate->ImportDefinitionTo(tag_decl, original_tag_decl);
255 tag_decl->setCompleteDefinition(
true);
259 tag_decl->setHasExternalLexicalStorage(
false);
260 tag_decl->setHasExternalVisibleStorage(
false);
261 }
else if (
auto *container_decl = dyn_cast<ObjCContainerDecl>(decl)) {
262 container_decl->setHasExternalLexicalStorage(
false);
263 container_decl->setHasExternalVisibleStorage(
false);
272 explicit CompleteTagDeclsScope(ClangASTImporter &importer,
273 clang::ASTContext *dst_ctx,
274 clang::ASTContext *src_ctx)
275 : m_delegate(importer.GetDelegate(dst_ctx, src_ctx)), m_dst_ctx(dst_ctx),
276 m_src_ctx(src_ctx), importer(importer) {
277 m_delegate->SetImportListener(
this);
280 ~CompleteTagDeclsScope()
override {
285 while (!m_decls_to_complete.empty()) {
286 NamedDecl *decl = m_decls_to_complete.pop_back_val();
287 m_decls_already_completed.insert(decl);
289 CompleteDecl(decl, *to_context_md);
291 to_context_md->removeOrigin(decl);
296 m_delegate->RemoveImportListener();
299 void NewDeclImported(clang::Decl *from, clang::Decl *to)
override {
301 if (!isa<TagDecl>(to) && !isa<ObjCInterfaceDecl>(to))
303 auto *from_record_decl = dyn_cast<CXXRecordDecl>(from);
305 if (from_record_decl && from_record_decl->isInjectedClassName())
308 NamedDecl *to_named_decl = dyn_cast<NamedDecl>(to);
310 if (m_decls_already_completed.contains(to_named_decl))
313 m_decls_to_complete.insert(to_named_decl);
327 " [ClangASTImporter] DeportType called on ({0}Type*){1:x} "
328 "from (ASTContext*){2:x} to (ASTContext*){3:x}",
337 CompleteTagDeclsScope complete_scope(*
this, &dst.
getASTContext(),
338 &src_ctxt->getASTContext());
346 clang::ASTContext *src_ctx = &decl->getASTContext();
348 " [ClangASTImporter] DeportDecl called on ({0}Decl*){1:x} from "
349 "(ASTContext*){2:x} to (ASTContext*){3:x}",
350 decl->getDeclKindName(), decl, src_ctx, dst_ctx);
358 CompleteTagDeclsScope complete_scope(*
this, dst_ctx, src_ctx);
366 " [ClangASTImporter] DeportDecl deported ({0}Decl*){1:x} to "
368 decl->getDeclKindName(), decl, result->getDeclKindName(), result);
378 if (isa<ObjCInterfaceDecl>(d))
387 clang::QualType qual_type(
390 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
391 switch (type_class) {
392 case clang::Type::Record:
393 return CanImport(qual_type->getAsCXXRecordDecl());
394 case clang::Type::Enum:
395 return CanImport(llvm::cast<clang::EnumType>(qual_type)->getDecl());
396 case clang::Type::ObjCObject:
397 case clang::Type::ObjCInterface: {
398 const clang::ObjCObjectType *objc_class_type =
399 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
400 if (objc_class_type) {
401 clang::ObjCInterfaceDecl *class_interface_decl =
402 objc_class_type->getInterface();
409 case clang::Type::Typedef:
411 llvm::cast<clang::TypedefType>(qual_type)
413 ->getUnderlyingType()
416 case clang::Type::Auto:
418 llvm::cast<clang::AutoType>(qual_type)
422 case clang::Type::Paren:
425 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
438 clang::QualType qual_type(
441 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
442 switch (type_class) {
443 case clang::Type::Record: {
444 const clang::CXXRecordDecl *cxx_record_decl =
445 qual_type->getAsCXXRecordDecl();
446 if (cxx_record_decl) {
452 case clang::Type::Enum: {
453 clang::EnumDecl *enum_decl =
454 llvm::cast<clang::EnumType>(qual_type)->getDecl();
461 case clang::Type::ObjCObject:
462 case clang::Type::ObjCInterface: {
463 const clang::ObjCObjectType *objc_class_type =
464 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
465 if (objc_class_type) {
466 clang::ObjCInterfaceDecl *class_interface_decl =
467 objc_class_type->getInterface();
470 if (class_interface_decl) {
477 case clang::Type::Typedef:
479 llvm::cast<clang::TypedefType>(qual_type)
481 ->getUnderlyingType()
484 case clang::Type::Auto:
486 llvm::cast<clang::AutoType>(qual_type)
490 case clang::Type::Paren:
493 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
505 if (
Import(compiler_type)) {
534template <
class D,
class O>
536 llvm::DenseMap<const D *, O> &destination_map,
537 llvm::DenseMap<const D *, O> &source_map,
544 typedef llvm::DenseMap<const D *, O> MapType;
545 typedef typename MapType::value_type PairType;
546 std::vector<PairType> sorted_items;
547 sorted_items.reserve(source_map.size());
548 sorted_items.assign(source_map.begin(), source_map.end());
549 llvm::sort(sorted_items, llvm::less_second());
551 for (
const auto &item : sorted_items) {
556 destination_map.insert(
557 std::pair<const D *, O>(parser_decl.
decl, item.second));
573template <
bool IsVirtual>
576 llvm::DenseMap<
const clang::CXXRecordDecl *,
577 clang::CharUnits> &base_offsets) {
578 for (CXXRecordDecl::base_class_const_iterator
579 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
580 be = (IsVirtual ? record->vbases_end() : record->bases_end());
582 if (!IsVirtual && bi->isVirtual())
585 const clang::Type *origin_base_type = bi->getType().getTypePtr();
586 const clang::RecordType *origin_base_record_type =
587 origin_base_type->getAs<RecordType>();
589 if (!origin_base_record_type)
593 origin_base_record_type->getDecl());
604 CharUnits base_offset;
608 record_layout.getVBaseClassOffset(origin_base_cxx_record.
decl);
611 record_layout.getBaseClassOffset(origin_base_cxx_record.
decl);
613 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(
614 origin_base_cxx_record.
decl, base_offset));
621 const RecordDecl *record, uint64_t &size, uint64_t &alignment,
622 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
623 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
625 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
630 clang::ASTContext &dest_ctx = record->getASTContext();
632 "LayoutRecordType on (ASTContext*){0:x} '{1}' for (RecordDecl*)"
633 "{2:x} [name = '{3}']",
644 std::remove_reference_t<
decltype(field_offsets)> origin_field_offsets;
645 std::remove_reference_t<
decltype(base_offsets)> origin_base_offsets;
646 std::remove_reference_t<
decltype(vbase_offsets)> origin_virtual_base_offsets;
649 &origin_record->getASTContext(),
650 const_cast<RecordDecl *
>(origin_record.
decl));
652 clang::RecordDecl *definition = origin_record.
decl->getDefinition();
653 if (!definition || !definition->isCompleteDefinition())
656 const ASTRecordLayout &record_layout(
657 origin_record->getASTContext().getASTRecordLayout(origin_record.
decl));
659 int field_idx = 0, field_count = record_layout.getFieldCount();
661 for (RecordDecl::field_iterator fi = origin_record->field_begin(),
662 fe = origin_record->field_end();
664 if (field_idx >= field_count)
667 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
669 origin_field_offsets.insert(
670 std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
678 if (origin_cxx_record.
IsValid()) {
680 origin_base_offsets) ||
682 origin_virtual_base_offsets))
686 if (!
ImportOffsetMap(&dest_ctx, field_offsets, origin_field_offsets, *
this) ||
687 !
ImportOffsetMap(&dest_ctx, base_offsets, origin_base_offsets, *
this) ||
688 !
ImportOffsetMap(&dest_ctx, vbase_offsets, origin_virtual_base_offsets,
692 size = record_layout.getSize().getQuantity() * dest_ctx.getCharWidth();
694 record_layout.getAlignment().getQuantity() * dest_ctx.getCharWidth();
698 LLDB_LOG(log,
"LRT Original = (RecordDecl*){0:x}",
699 static_cast<const void *
>(origin_record.
decl));
700 LLDB_LOG(log,
"LRT Size = {0}", size);
701 LLDB_LOG(log,
"LRT Alignment = {0}", alignment);
703 for (RecordDecl::field_iterator fi = record->field_begin(),
704 fe = record->field_end();
708 "LRT (FieldDecl*){0:x}, Name = '{1}', Type = '{2}', Offset = "
710 *fi, fi->getName(), fi->getType().getAsString(), field_offsets[*fi]);
714 if (parser_cxx_record.
IsValid()) {
716 for (CXXRecordDecl::base_class_const_iterator
717 bi = parser_cxx_record->bases_begin(),
718 be = parser_cxx_record->bases_end();
720 bool is_virtual = bi->isVirtual();
722 QualType base_type = bi->getType();
723 const RecordType *base_record_type = base_type->getAs<RecordType>();
729 "LRT {0}(CXXRecordDecl*){1:x}, Name = '{2}', Offset = "
731 (is_virtual ?
"Virtual " :
""), base_cxx_record.
decl,
732 base_cxx_record.
decl->getName(),
734 ? vbase_offsets[base_cxx_record.
decl].getQuantity()
735 : base_offsets[base_cxx_record.
decl].getQuantity()));
738 LLDB_LOG(log,
"LRD Not a CXXRecord, so no bases");
746 const clang::RecordDecl *record_decl, uint64_t &bit_size,
748 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
749 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
751 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
753 RecordDeclToLayoutMap::iterator pos =
755 base_offsets.clear();
756 vbase_offsets.clear();
758 bit_size = pos->second.bit_size;
759 alignment = pos->second.alignment;
760 field_offsets.swap(pos->second.field_offsets);
761 base_offsets.swap(pos->second.base_offsets);
762 vbase_offsets.swap(pos->second.vbase_offsets);
772 field_offsets, base_offsets,
778 field_offsets.clear();
791 if (!decl_origin.
Valid())
801 &decl->getASTContext());
803 delegate_sp->ImportDefinitionTo(decl, decl_origin.
decl);
809 clang::TagDecl *origin_decl) {
810 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
816 GetDelegate(&decl->getASTContext(), origin_ast_ctx));
819 delegate_sp->ImportDefinitionTo(decl, origin_decl);
823 context_md->setOrigin(decl,
DeclOrigin(origin_ast_ctx, origin_decl));
828 clang::ObjCInterfaceDecl *interface_decl) {
831 if (!decl_origin.
Valid())
841 delegate_sp->ImportDefinitionTo(interface_decl, decl_origin.
decl);
843 if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass())
855 if (
const TagType *tag_type = type->getAs<TagType>()) {
856 TagDecl *tag_decl = tag_type->getDecl();
860 if (!decl_origin.
Valid())
867 &tag_decl->getASTContext());
869 TagDecl *origin_tag_decl = llvm::dyn_cast<TagDecl>(decl_origin.
decl);
871 for (Decl *origin_child_decl : origin_tag_decl->decls()) {
872 llvm::Expected<Decl *> imported_or_err =
873 delegate_sp->Import(origin_child_decl);
874 if (!imported_or_err) {
876 "Couldn't import decl: {0}");
881 if (RecordDecl *record_decl = dyn_cast<RecordDecl>(origin_tag_decl))
882 record_decl->setHasLoadedFieldsFromExternalStorage(
true);
887 if (
const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
888 if (ObjCInterfaceDecl *objc_interface_decl =
889 objc_object_type->getInterface()) {
892 if (!decl_origin.
Valid())
896 GetDelegate(&objc_interface_decl->getASTContext(), decl_origin.
ctx));
898 ObjCInterfaceDecl *origin_interface_decl =
899 llvm::dyn_cast<ObjCInterfaceDecl>(decl_origin.
decl);
901 for (Decl *origin_child_decl : origin_interface_decl->decls()) {
902 llvm::Expected<Decl *> imported_or_err =
903 delegate_sp->Import(origin_child_decl);
904 if (!imported_or_err) {
906 "Couldn't import decl: {0}");
923 if (
const TagType *tag_type = type->getAs<TagType>()) {
924 TagDecl *tag_decl = tag_type->getDecl();
926 if (tag_decl->getDefinition())
931 if (
const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
932 if (ObjCInterfaceDecl *objc_interface_decl =
933 objc_object_type->getInterface())
937 if (
const ArrayType *array_type = type->getAsArrayTypeUnsafe())
939 if (
const AtomicType *atomic_type = type->getAs<AtomicType>())
945std::optional<ClangASTMetadata>
949 if (decl_origin.
Valid()) {
961 return context_md->getOrigin(decl);
965 clang::Decl *original_decl) {
967 context_md->setOrigin(
968 decl,
DeclOrigin(&original_decl->getASTContext(), original_decl));
975 context_md->m_namespace_maps[decl] = namespace_map;
984 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
986 if (iter != namespace_maps.end())
995 const DeclContext *parent_context = decl->getDeclContext();
996 const NamespaceDecl *parent_namespace =
997 dyn_cast<NamespaceDecl>(parent_context);
1000 if (parent_namespace)
1005 new_map = std::make_shared<NamespaceMap>();
1007 if (context_md->m_map_completer) {
1008 std::string namespace_string = decl->getDeclName().getAsString();
1010 context_md->m_map_completer->CompleteNamespaceMap(
1011 new_map,
ConstString(namespace_string.c_str()), parent_map);
1014 context_md->m_namespace_maps[decl] = new_map;
1021 " [ClangASTImporter] Forgetting destination (ASTContext*){0:x}",
1028 clang::ASTContext *src_ast) {
1034 " [ClangASTImporter] Forgetting source->dest "
1035 "(ASTContext*){0:x}->(ASTContext*){1:x}",
1041 md->m_delegates.erase(src_ast);
1042 md->removeOriginsWithContext(src_ast);
1047llvm::Expected<Decl *>
1054 if (
const auto *CXX = llvm::dyn_cast<clang::CXXRecordDecl>(From))
1055 if (CXX->isLambda())
1056 return llvm::make_error<ASTImportError>(
1057 ASTImportError::UnsupportedConstruct);
1076 assert(origin.
decl != From &&
"Origin points to itself?");
1087 if (origin.
Valid() && origin.
ctx == &getToContext()) {
1088 RegisterImportedDecl(From, origin.
decl);
1101 if (origin.
Valid()) {
1102 auto R =
m_main.CopyDecl(&getToContext(), origin.
decl);
1104 RegisterImportedDecl(From, R);
1111 std::optional<ClangASTMetadata> md =
m_main.GetDeclMetadata(From);
1112 auto *td = dyn_cast<TagDecl>(From);
1113 if (td && md && md->IsForcefullyCompleted()) {
1116 "[ClangASTImporter] Searching for a complete definition of {0} in "
1119 Expected<DeclContext *> dc_or_err = ImportContext(td->getDeclContext());
1121 return dc_or_err.takeError();
1122 Expected<DeclarationName> dn_or_err =
Import(td->getDeclName());
1124 return dn_or_err.takeError();
1125 DeclContext *dc = *dc_or_err;
1126 DeclContext::lookup_result lr = dc->lookup(*dn_or_err);
1127 for (clang::Decl *candidate : lr) {
1128 if (candidate->getKind() == From->getKind()) {
1129 RegisterImportedDecl(From, candidate);
1134 LLDB_LOG(log,
"[ClangASTImporter] Complete definition not found");
1137 return ASTImporter::ImportImpl(From);
1141 clang::Decl *to, clang::Decl *from) {
1144 auto getDeclName = [](Decl
const *decl) {
1145 std::string name_string;
1146 if (
auto const *from_named_decl = dyn_cast<clang::NamedDecl>(decl)) {
1147 llvm::raw_string_ostream name_stream(name_string);
1148 from_named_decl->printName(name_stream);
1155 if (
auto *D = GetAlreadyImportedOrNull(from); D && D != to) {
1158 "[ClangASTImporter] ERROR: overwriting an already imported decl "
1159 "'{0:x}' ('{1}') from '{2:x}' with '{3:x}'. Likely due to a name "
1160 "conflict when importing '{1}'.",
1161 D, getDeclName(from), from, to);
1172 MapImported(from, to);
1174 if (llvm::Error err = ImportDefinition(from)) {
1176 "[ClangASTImporter] Error during importing definition: {0}");
1180 if (clang::TagDecl *to_tag = dyn_cast<clang::TagDecl>(to)) {
1181 if (clang::TagDecl *from_tag = dyn_cast<clang::TagDecl>(from)) {
1182 to_tag->setCompleteDefinition(from_tag->isCompleteDefinition());
1186 "==== [ClangASTImporter][TUDecl: {0:x}] Imported "
1187 "({1}Decl*){2:x}, named {3} (from "
1189 static_cast<void *
>(to->getTranslationUnitDecl()),
1190 from->getDeclKindName(),
static_cast<void *
>(to),
1191 getDeclName(from),
static_cast<void *
>(from));
1194 std::string ast_string;
1195 llvm::raw_string_ostream ast_stream(ast_string);
1196 to->getTranslationUnitDecl()->dump(ast_stream);
1197 LLDB_LOG(log_ast,
"{0}", ast_string);
1206 if (ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to)) {
1207 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
1212 ObjCInterfaceDecl *from_objc_interface = dyn_cast<ObjCInterfaceDecl>(from);
1214 if (!from_objc_interface)
1217 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
1219 if (!from_superclass)
1222 llvm::Expected<Decl *> imported_from_superclass_decl =
1225 if (!imported_from_superclass_decl) {
1227 "Couldn't import decl: {0}");
1231 ObjCInterfaceDecl *imported_from_superclass =
1232 dyn_cast<ObjCInterfaceDecl>(*imported_from_superclass_decl);
1234 if (!imported_from_superclass)
1237 if (!to_objc_interface->hasDefinition())
1238 to_objc_interface->startDefinition();
1240 to_objc_interface->setSuperClass(
m_source_ctx->getTrivialTypeSourceInfo(
1241 m_source_ctx->getObjCInterfaceType(imported_from_superclass)));
1250 CXXMethodDecl *to_method) {
1251 if (!to_method->isVirtual())
1253 QualType return_type = to_method->getReturnType();
1254 if (!return_type->isPointerType() && !return_type->isReferenceType())
1257 clang::RecordDecl *rd = return_type->getPointeeType()->getAsRecordDecl();
1260 if (rd->getDefinition())
1267static OptionalClangModuleID
1275 from_source.
GetIDForModule(module->Parent), from_source, to_source);
1278 module->IsExplicit);
1291 auto *from_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>(
1292 getFromContext().getExternalSource());
1294 auto *to_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>(
1295 getToContext().getExternalSource());
1296 if (from_source && to_source) {
1305 if (std::optional<ClangASTMetadata> metadata =
m_main.GetDeclMetadata(from))
1306 user_id = metadata->GetUserID();
1309 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
1310 std::string name_string;
1311 llvm::raw_string_ostream name_stream(name_string);
1312 from_named_decl->printName(name_stream);
1316 " [ClangASTImporter] Imported ({0}Decl*){1:x}, named {2} (from "
1317 "(Decl*){3:x}), metadata {4}",
1318 from->getDeclKindName(), to, name_string, from, user_id);
1321 " [ClangASTImporter] Imported ({0}Decl*){1:x} (from "
1322 "(Decl*){2:x}), metadata {3}",
1323 from->getDeclKindName(), to, from, user_id);
1328 m_main.GetContextMetadata(&to->getASTContext());
1332 if (from_context_md) {
1333 DeclOrigin origin = from_context_md->getOrigin(from);
1335 if (origin.
Valid()) {
1336 if (origin.
ctx != &to->getASTContext()) {
1338 to_context_md->setOrigin(to, origin);
1341 " [ClangASTImporter] Propagated origin "
1342 "(Decl*){0:x}/(ASTContext*){1:x} from (ASTContext*){2:x} to "
1343 "(ASTContext*){3:x}",
1344 origin.
decl, origin.
ctx, &from->getASTContext(),
1345 &to->getASTContext());
1355 " [ClangASTImporter] Decl has no origin information in "
1356 "(ASTContext*){0:x}",
1357 &from->getASTContext());
1360 if (
auto *to_namespace = dyn_cast<clang::NamespaceDecl>(to)) {
1361 auto *from_namespace = cast<clang::NamespaceDecl>(from);
1365 NamespaceMetaMap::iterator namespace_map_iter =
1366 namespace_maps.find(from_namespace);
1368 if (namespace_map_iter != namespace_maps.end())
1369 to_context_md->m_namespace_maps[to_namespace] =
1370 namespace_map_iter->second;
1376 " [ClangASTImporter] Sourced origin "
1377 "(Decl*){0:x}/(ASTContext*){1:x} into (ASTContext*){2:x}",
1381 if (
auto *to_namespace_decl = dyn_cast<NamespaceDecl>(to)) {
1382 m_main.BuildNamespaceMap(to_namespace_decl);
1383 to_namespace_decl->setHasExternalVisibleStorage();
1393 if (
auto *to_tag_decl = dyn_cast<TagDecl>(to)) {
1394 to_tag_decl->setHasExternalLexicalStorage();
1395 to_tag_decl->getPrimaryContext()->setMustBuildLookupTable();
1396 auto from_tag_decl = cast<TagDecl>(from);
1400 " [ClangASTImporter] To is a TagDecl - attributes {0}{1} [{2}->{3}]",
1401 (to_tag_decl->hasExternalLexicalStorage() ?
" Lexical" :
""),
1402 (to_tag_decl->hasExternalVisibleStorage() ?
" Visible" :
""),
1403 (from_tag_decl->isCompleteDefinition() ?
"complete" :
"incomplete"),
1404 (to_tag_decl->isCompleteDefinition() ?
"complete" :
"incomplete"));
1407 if (
auto *to_container_decl = dyn_cast<ObjCContainerDecl>(to)) {
1408 to_container_decl->setHasExternalLexicalStorage();
1409 to_container_decl->setHasExternalVisibleStorage();
1412 if (ObjCInterfaceDecl *to_interface_decl =
1413 llvm::dyn_cast<ObjCInterfaceDecl>(to_container_decl)) {
1416 " [ClangASTImporter] To is an ObjCInterfaceDecl - attributes "
1418 (to_interface_decl->hasExternalLexicalStorage() ?
" Lexical" :
""),
1419 (to_interface_decl->hasExternalVisibleStorage() ?
" Visible" :
""),
1420 (to_interface_decl->hasDefinition() ?
" HasDefinition" :
""));
1423 log,
" [ClangASTImporter] To is an {0}Decl - attributes {1}{2}",
1424 ((Decl *)to_container_decl)->getDeclKindName(),
1425 (to_container_decl->hasExternalLexicalStorage() ?
" Lexical" :
""),
1426 (to_container_decl->hasExternalVisibleStorage() ?
" Visible" :
""));
1431 if (clang::CXXMethodDecl *to_method = dyn_cast<CXXMethodDecl>(to))
1437 return m_main.GetDeclOrigin(To).decl;
static OptionalClangModuleID RemapModule(OptionalClangModuleID from_id, ClangExternalASTSourceCallbacks &from_source, ClangExternalASTSourceCallbacks &to_source)
Recreate a module with its parents in to_source and return its id.
bool ExtractBaseOffsets(const ASTRecordLayout &record_layout, DeclFromUser< const CXXRecordDecl > &record, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &base_offsets)
Given a CXXRecordDecl, will calculate and populate base_offsets with the integral offsets of any of i...
static void MaybeCompleteReturnType(ClangASTImporter &importer, CXXMethodDecl *to_method)
Takes a CXXMethodDecl and completes the return type if necessary.
static bool ImportOffsetMap(clang::ASTContext *dest_ctx, llvm::DenseMap< const D *, O > &destination_map, llvm::DenseMap< const D *, O > &source_map, ClangASTImporter &importer)
Copy layout information from source_map to the destination_map.
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
#define LLDB_LOG_ERROR(log, error,...)
DeclContextOverride()=default
void Override(clang::Decl *decl)
llvm::DenseMap< clang::Decl *, Backup > m_backups
void OverrideAllDeclsFromContainingFunction(clang::Decl *decl)
void OverrideOne(clang::Decl *decl)
clang::Decl * GetEscapedChild(clang::Decl *decl, clang::DeclContext *base=nullptr)
bool ChainPassesThrough(clang::Decl *decl, clang::DeclContext *base, clang::DeclContext *(clang::Decl::*contextFromDecl)(), clang::DeclContext *(clang::DeclContext::*contextFromContext)())
bool hasOrigin(const clang::Decl *decl) const
Returns true there is a known DeclOrigin for the given Decl.
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...
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.
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)
std::shared_ptr< NamespaceMap > NamespaceMapSP
NamespaceMapSP GetNamespaceMap(const clang::NamespaceDecl *decl)
std::shared_ptr< ASTImporterDelegate > ImporterDelegateSP
llvm::DenseMap< const clang::NamespaceDecl *, NamespaceMapSP > NamespaceMetaMap
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)
CompilerType DeportType(TypeSystemClang &dst, const CompilerType &src_type)
Copies the given type and the respective declarations to the destination type system.
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)
TypeSystemClang & GetTypeSystem() const
OptionalClangModuleID GetIDForModule(clang::Module *module)
clang::Module * getModule(unsigned ID) override
Generic representation of a type in a programming language.
TypeSystemSPWrapper GetTypeSystem() const
Accessors.
lldb::opaque_compiler_type_t GetOpaqueQualType() const
ConstString GetTypeName(bool BaseOnly=false) const
A uniqued constant string class.
DeclFromUser< D > GetOrigin(ClangASTImporter &importer)
DeclFromParser< D > Import(clang::ASTContext *dest_ctx, ClangASTImporter &importer)
unsigned GetValue() const
A TypeSystem implementation based on Clang.
static void SetOwningModule(clang::Decl *decl, OptionalClangModuleID owning_module)
Set the owning module for decl.
OptionalClangModuleID GetOrCreateClangModule(llvm::StringRef name, OptionalClangModuleID parent, bool is_framework=false, bool is_explicit=false)
Synthesize a clang::Module and return its ID or a default-constructed ID.
static TypeSystemClang * GetASTContext(clang::ASTContext *ast_ctx)
std::optional< ClangASTMetadata > GetMetadata(const clang::Decl *object)
static bool CompleteTagDeclarationDefinition(const CompilerType &type)
static bool SetHasExternalStorage(lldb::opaque_compiler_type_t type, bool has_extern)
bool GetCompleteDecl(clang::Decl *decl)
clang::ASTContext & getASTContext() const
Returns the clang::ASTContext instance managed by this TypeSystemClang.
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
TD< D2 > DynCast(TD< D1 > source)
void * opaque_compiler_type_t
clang::DeclContext * decl_context
clang::DeclContext * lexical_decl_context
clang::Decl * GetOriginalDecl(clang::Decl *To) override
llvm::Expected< clang::Decl * > ImportImpl(clang::Decl *From) override
void MarkDeclImported(clang::Decl *from, clang::Decl *to)
llvm::SmallPtrSet< clang::Decl *, 16 > m_decls_to_ignore
Decls we should ignore when mapping decls back to their original ASTContext.
CxxModuleHandler * m_std_handler
void Imported(clang::Decl *from, clang::Decl *to) override
NewDeclListener * m_new_decl_listener
The currently attached listener.
void ImportDefinitionTo(clang::Decl *to, clang::Decl *from)
clang::ASTContext * m_source_ctx
ClangASTImporter & m_main
Listener interface used by the ASTImporterDelegate to inform other code about decls that have been im...
static clang::QualType GetQualType(const CompilerType &ct)
static clang::QualType GetCanonicalQualType(const CompilerType &ct)
static bool IsClangType(const CompilerType &ct)
static CompilerType RemoveFastQualifiers(const CompilerType &ct)