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);
118 if (m_backups.contains(decl)) {
122 m_backups[decl] = {decl->getDeclContext(), decl->getLexicalDeclContext()};
124 decl->setDeclContext(decl->getASTContext().getTranslationUnitDecl());
125 decl->setLexicalDeclContext(decl->getASTContext().getTranslationUnitDecl());
129 clang::Decl *decl, clang::DeclContext *base,
130 clang::DeclContext *(clang::Decl::*contextFromDecl)(),
131 clang::DeclContext *(clang::DeclContext::*contextFromContext)()) {
132 for (DeclContext *decl_ctx = (decl->*contextFromDecl)(); decl_ctx;
133 decl_ctx = (decl_ctx->*contextFromContext)()) {
134 if (decl_ctx == base) {
143 clang::DeclContext *base =
nullptr) {
147 if (!ChainPassesThrough(decl, base, &clang::Decl::getDeclContext,
148 &clang::DeclContext::getParent) ||
149 !ChainPassesThrough(decl, base, &clang::Decl::getLexicalDeclContext,
150 &clang::DeclContext::getLexicalParent)) {
154 base = clang::dyn_cast<clang::DeclContext>(decl);
161 if (clang::DeclContext *context =
162 clang::dyn_cast<clang::DeclContext>(decl)) {
163 for (clang::Decl *decl : context->decls()) {
164 if (clang::Decl *escaped_child = GetEscapedChild(decl)) {
165 return escaped_child;
174 if (clang::Decl *escaped_child = GetEscapedChild(decl)) {
178 " [ClangASTImporter] DeclContextOverride couldn't "
179 "override ({0}Decl*){1} - its child ({2}Decl*){3} escapes",
180 decl->getDeclKindName(), decl, escaped_child->getDeclKindName(),
192 for (DeclContext *decl_context = decl->getLexicalDeclContext();
193 decl_context; decl_context = decl_context->getLexicalParent()) {
194 DeclContext *redecl_context = decl_context->getRedeclContext();
196 if (llvm::isa<FunctionDecl>(redecl_context) &&
197 llvm::isa<TranslationUnitDecl>(redecl_context->getLexicalParent())) {
198 for (clang::Decl *child_decl : decl_context->decls()) {
199 Override(child_decl);
206 for (
const std::pair<clang::Decl *, Backup> &backup : m_backups) {
207 backup.first->setDeclContext(backup.second.decl_context);
208 backup.first->setLexicalDeclContext(backup.second.lexical_decl_context);
224 llvm::SetVector<NamedDecl *> m_decls_to_complete;
227 llvm::SmallPtrSet<NamedDecl *, 32> m_decls_already_completed;
228 clang::ASTContext *m_dst_ctx;
229 clang::ASTContext *m_src_ctx;
238 clang::ASTContext *dst_ctx,
239 clang::ASTContext *src_ctx)
240 : m_delegate(importer.GetDelegate(dst_ctx, src_ctx)), m_dst_ctx(dst_ctx),
241 m_src_ctx(src_ctx), importer(importer) {
242 m_delegate->SetImportListener(
this);
245 ~CompleteTagDeclsScope()
override {
250 while (!m_decls_to_complete.empty()) {
251 NamedDecl *decl = m_decls_to_complete.pop_back_val();
252 m_decls_already_completed.insert(decl);
256 assert(to_context_md->hasOrigin(decl));
258 assert(to_context_md->getOrigin(decl).ctx == m_src_ctx);
260 Decl *original_decl = to_context_md->getOrigin(decl).decl;
264 if (
auto *tag_decl = dyn_cast<TagDecl>(decl)) {
265 if (
auto *original_tag_decl = dyn_cast<TagDecl>(original_decl)) {
266 if (original_tag_decl->isCompleteDefinition()) {
267 m_delegate->ImportDefinitionTo(tag_decl, original_tag_decl);
268 tag_decl->setCompleteDefinition(
true);
272 tag_decl->setHasExternalLexicalStorage(
false);
273 tag_decl->setHasExternalVisibleStorage(
false);
274 }
else if (
auto *container_decl = dyn_cast<ObjCContainerDecl>(decl)) {
275 container_decl->setHasExternalLexicalStorage(
false);
276 container_decl->setHasExternalVisibleStorage(
false);
279 to_context_md->removeOrigin(decl);
284 m_delegate->RemoveImportListener();
287 void NewDeclImported(clang::Decl *from, clang::Decl *to)
override {
289 if (!isa<TagDecl>(to) && !isa<ObjCInterfaceDecl>(to))
291 RecordDecl *from_record_decl = dyn_cast<RecordDecl>(from);
293 if (from_record_decl && from_record_decl->isInjectedClassName())
296 NamedDecl *to_named_decl = dyn_cast<NamedDecl>(to);
298 if (m_decls_already_completed.contains(to_named_decl))
301 m_decls_to_complete.insert(to_named_decl);
315 " [ClangASTImporter] DeportType called on ({0}Type*){1:x} "
316 "from (ASTContext*){2:x} to (ASTContext*){3:x}",
325 CompleteTagDeclsScope complete_scope(*
this, &dst.
getASTContext(),
326 &src_ctxt->getASTContext());
334 clang::ASTContext *src_ctx = &decl->getASTContext();
336 " [ClangASTImporter] DeportDecl called on ({0}Decl*){1:x} from "
337 "(ASTContext*){2:x} to (ASTContext*){3:x}",
338 decl->getDeclKindName(), decl, src_ctx, dst_ctx);
346 CompleteTagDeclsScope complete_scope(*
this, dst_ctx, src_ctx);
354 " [ClangASTImporter] DeportDecl deported ({0}Decl*){1:x} to "
356 decl->getDeclKindName(), decl, result->getDeclKindName(), result);
365 clang::QualType qual_type(
368 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
369 switch (type_class) {
370 case clang::Type::Record: {
371 const clang::CXXRecordDecl *cxx_record_decl =
372 qual_type->getAsCXXRecordDecl();
373 if (cxx_record_decl) {
379 case clang::Type::Enum: {
380 clang::EnumDecl *enum_decl =
381 llvm::cast<clang::EnumType>(qual_type)->getDecl();
388 case clang::Type::ObjCObject:
389 case clang::Type::ObjCInterface: {
390 const clang::ObjCObjectType *objc_class_type =
391 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
392 if (objc_class_type) {
393 clang::ObjCInterfaceDecl *class_interface_decl =
394 objc_class_type->getInterface();
397 if (class_interface_decl) {
404 case clang::Type::Typedef:
406 llvm::cast<clang::TypedefType>(qual_type)
408 ->getUnderlyingType()
411 case clang::Type::Auto:
413 llvm::cast<clang::AutoType>(qual_type)
417 case clang::Type::Elaborated:
419 llvm::cast<clang::ElaboratedType>(qual_type)
423 case clang::Type::Paren:
426 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
439 clang::QualType qual_type(
442 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
443 switch (type_class) {
444 case clang::Type::Record: {
445 const clang::CXXRecordDecl *cxx_record_decl =
446 qual_type->getAsCXXRecordDecl();
447 if (cxx_record_decl) {
453 case clang::Type::Enum: {
454 clang::EnumDecl *enum_decl =
455 llvm::cast<clang::EnumType>(qual_type)->getDecl();
462 case clang::Type::ObjCObject:
463 case clang::Type::ObjCInterface: {
464 const clang::ObjCObjectType *objc_class_type =
465 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
466 if (objc_class_type) {
467 clang::ObjCInterfaceDecl *class_interface_decl =
468 objc_class_type->getInterface();
471 if (class_interface_decl) {
478 case clang::Type::Typedef:
480 llvm::cast<clang::TypedefType>(qual_type)
482 ->getUnderlyingType()
485 case clang::Type::Auto:
487 llvm::cast<clang::AutoType>(qual_type)
491 case clang::Type::Elaborated:
493 llvm::cast<clang::ElaboratedType>(qual_type)
497 case clang::Type::Paren:
500 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
512 if (
Import(compiler_type)) {
541template <
class D,
class O>
543 llvm::DenseMap<const D *, O> &destination_map,
544 llvm::DenseMap<const D *, O> &source_map,
551 typedef llvm::DenseMap<const D *, O> MapType;
552 typedef typename MapType::value_type PairType;
553 std::vector<PairType> sorted_items;
554 sorted_items.reserve(source_map.size());
555 sorted_items.assign(source_map.begin(), source_map.end());
556 llvm::sort(sorted_items, llvm::less_second());
558 for (
const auto &item : sorted_items) {
563 destination_map.insert(
564 std::pair<const D *, O>(parser_decl.
decl, item.second));
580template <
bool IsVirtual>
583 llvm::DenseMap<
const clang::CXXRecordDecl *,
584 clang::CharUnits> &base_offsets) {
585 for (CXXRecordDecl::base_class_const_iterator
586 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
587 be = (IsVirtual ? record->vbases_end() : record->bases_end());
589 if (!IsVirtual && bi->isVirtual())
592 const clang::Type *origin_base_type = bi->getType().getTypePtr();
593 const clang::RecordType *origin_base_record_type =
594 origin_base_type->getAs<RecordType>();
596 if (!origin_base_record_type)
600 origin_base_record_type->getDecl());
606 DynCast<CXXRecordDecl>(origin_base_record));
611 CharUnits base_offset;
615 record_layout.getVBaseClassOffset(origin_base_cxx_record.
decl);
618 record_layout.getBaseClassOffset(origin_base_cxx_record.
decl);
620 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(
621 origin_base_cxx_record.
decl, base_offset));
628 const RecordDecl *record, uint64_t &size, uint64_t &alignment,
629 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
630 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
632 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
637 clang::ASTContext &dest_ctx = record->getASTContext();
639 "LayoutRecordType on (ASTContext*){0:x} '{1}' for (RecordDecl*)"
640 "{2:x} [name = '{3}']",
651 std::remove_reference_t<
decltype(field_offsets)> origin_field_offsets;
652 std::remove_reference_t<
decltype(base_offsets)> origin_base_offsets;
653 std::remove_reference_t<
decltype(vbase_offsets)> origin_virtual_base_offsets;
656 &origin_record->getASTContext(),
657 const_cast<RecordDecl *
>(origin_record.
decl));
659 clang::RecordDecl *definition = origin_record.
decl->getDefinition();
660 if (!definition || !definition->isCompleteDefinition())
663 const ASTRecordLayout &record_layout(
664 origin_record->getASTContext().getASTRecordLayout(origin_record.
decl));
666 int field_idx = 0, field_count = record_layout.getFieldCount();
668 for (RecordDecl::field_iterator fi = origin_record->field_begin(),
669 fe = origin_record->field_end();
671 if (field_idx >= field_count)
674 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
676 origin_field_offsets.insert(
677 std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
683 DynCast<const CXXRecordDecl>(origin_record));
685 if (origin_cxx_record.
IsValid()) {
686 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record,
687 origin_base_offsets) ||
688 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record,
689 origin_virtual_base_offsets))
693 if (!
ImportOffsetMap(&dest_ctx, field_offsets, origin_field_offsets, *
this) ||
694 !
ImportOffsetMap(&dest_ctx, base_offsets, origin_base_offsets, *
this) ||
695 !
ImportOffsetMap(&dest_ctx, vbase_offsets, origin_virtual_base_offsets,
699 size = record_layout.getSize().getQuantity() * dest_ctx.getCharWidth();
701 record_layout.getAlignment().getQuantity() * dest_ctx.getCharWidth();
705 LLDB_LOG(log,
"LRT Original = (RecordDecl*){0:x}",
706 static_cast<const void *
>(origin_record.
decl));
707 LLDB_LOG(log,
"LRT Size = {0}", size);
708 LLDB_LOG(log,
"LRT Alignment = {0}", alignment);
710 for (RecordDecl::field_iterator fi = record->field_begin(),
711 fe = record->field_end();
715 "LRT (FieldDecl*){0:x}, Name = '{1}', Type = '{2}', Offset = "
717 *fi, fi->getName(), fi->getType().getAsString(), field_offsets[*fi]);
720 DynCast<const CXXRecordDecl>(parser_record);
721 if (parser_cxx_record.
IsValid()) {
723 for (CXXRecordDecl::base_class_const_iterator
724 bi = parser_cxx_record->bases_begin(),
725 be = parser_cxx_record->bases_end();
727 bool is_virtual = bi->isVirtual();
729 QualType base_type = bi->getType();
730 const RecordType *base_record_type = base_type->getAs<RecordType>();
733 DynCast<CXXRecordDecl>(base_record);
736 "LRT {0}(CXXRecordDecl*){1:x}, Name = '{2}', Offset = "
738 (is_virtual ?
"Virtual " :
""), base_cxx_record.
decl,
739 base_cxx_record.
decl->getName(),
741 ? vbase_offsets[base_cxx_record.
decl].getQuantity()
742 : base_offsets[base_cxx_record.
decl].getQuantity()));
745 LLDB_LOG(log,
"LRD Not a CXXRecord, so no bases");
753 const clang::RecordDecl *record_decl, uint64_t &bit_size,
755 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
756 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
758 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
760 RecordDeclToLayoutMap::iterator pos =
762 base_offsets.clear();
763 vbase_offsets.clear();
765 bit_size = pos->second.bit_size;
766 alignment = pos->second.alignment;
767 field_offsets.swap(pos->second.field_offsets);
768 base_offsets.swap(pos->second.base_offsets);
769 vbase_offsets.swap(pos->second.vbase_offsets);
779 field_offsets, base_offsets,
785 field_offsets.clear();
798 if (!decl_origin.
Valid())
808 &decl->getASTContext());
810 delegate_sp->ImportDefinitionTo(decl, decl_origin.
decl);
816 clang::TagDecl *origin_decl) {
817 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
823 GetDelegate(&decl->getASTContext(), origin_ast_ctx));
826 delegate_sp->ImportDefinitionTo(decl, origin_decl);
830 context_md->setOrigin(decl,
DeclOrigin(origin_ast_ctx, origin_decl));
835 clang::ObjCInterfaceDecl *interface_decl) {
838 if (!decl_origin.
Valid())
848 delegate_sp->ImportDefinitionTo(interface_decl, decl_origin.
decl);
850 if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass())
862 if (
const TagType *tag_type = type->getAs<TagType>()) {
863 TagDecl *tag_decl = tag_type->getDecl();
867 if (!decl_origin.
Valid())
874 &tag_decl->getASTContext());
876 TagDecl *origin_tag_decl = llvm::dyn_cast<TagDecl>(decl_origin.
decl);
878 for (Decl *origin_child_decl : origin_tag_decl->decls()) {
879 llvm::Expected<Decl *> imported_or_err =
880 delegate_sp->Import(origin_child_decl);
881 if (!imported_or_err) {
883 "Couldn't import decl: {0}");
888 if (RecordDecl *record_decl = dyn_cast<RecordDecl>(origin_tag_decl))
889 record_decl->setHasLoadedFieldsFromExternalStorage(
true);
894 if (
const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
895 if (ObjCInterfaceDecl *objc_interface_decl =
896 objc_object_type->getInterface()) {
899 if (!decl_origin.
Valid())
903 GetDelegate(&objc_interface_decl->getASTContext(), decl_origin.
ctx));
905 ObjCInterfaceDecl *origin_interface_decl =
906 llvm::dyn_cast<ObjCInterfaceDecl>(decl_origin.
decl);
908 for (Decl *origin_child_decl : origin_interface_decl->decls()) {
909 llvm::Expected<Decl *> imported_or_err =
910 delegate_sp->Import(origin_child_decl);
911 if (!imported_or_err) {
913 "Couldn't import decl: {0}");
930 if (
const TagType *tag_type = type->getAs<TagType>()) {
931 TagDecl *tag_decl = tag_type->getDecl();
933 if (tag_decl->getDefinition() || tag_decl->isBeingDefined())
938 if (
const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
939 if (ObjCInterfaceDecl *objc_interface_decl =
940 objc_object_type->getInterface())
944 if (
const ArrayType *array_type = type->getAsArrayTypeUnsafe())
946 if (
const AtomicType *atomic_type = type->getAs<AtomicType>())
952std::optional<ClangASTMetadata>
956 if (decl_origin.
Valid()) {
968 return context_md->getOrigin(decl);
972 clang::Decl *original_decl) {
974 context_md->setOrigin(
975 decl,
DeclOrigin(&original_decl->getASTContext(), original_decl));
982 context_md->m_namespace_maps[decl] = namespace_map;
991 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
993 if (iter != namespace_maps.end())
1002 const DeclContext *parent_context = decl->getDeclContext();
1003 const NamespaceDecl *parent_namespace =
1004 dyn_cast<NamespaceDecl>(parent_context);
1007 if (parent_namespace)
1012 new_map = std::make_shared<NamespaceMap>();
1014 if (context_md->m_map_completer) {
1015 std::string namespace_string = decl->getDeclName().getAsString();
1017 context_md->m_map_completer->CompleteNamespaceMap(
1018 new_map,
ConstString(namespace_string.c_str()), parent_map);
1021 context_md->m_namespace_maps[decl] = new_map;
1028 " [ClangASTImporter] Forgetting destination (ASTContext*){0:x}",
1035 clang::ASTContext *src_ast) {
1041 " [ClangASTImporter] Forgetting source->dest "
1042 "(ASTContext*){0:x}->(ASTContext*){1:x}",
1048 md->m_delegates.erase(src_ast);
1049 md->removeOriginsWithContext(src_ast);
1054llvm::Expected<Decl *>
1073 assert(origin.
decl != From &&
"Origin points to itself?");
1084 if (origin.
Valid() && origin.
ctx == &getToContext()) {
1085 RegisterImportedDecl(From, origin.
decl);
1098 if (origin.
Valid()) {
1101 RegisterImportedDecl(From, R);
1109 auto *td = dyn_cast<TagDecl>(From);
1110 if (td && md && md->IsForcefullyCompleted()) {
1113 "[ClangASTImporter] Searching for a complete definition of {0} in "
1116 Expected<DeclContext *> dc_or_err = ImportContext(td->getDeclContext());
1118 return dc_or_err.takeError();
1119 Expected<DeclarationName> dn_or_err =
Import(td->getDeclName());
1121 return dn_or_err.takeError();
1122 DeclContext *dc = *dc_or_err;
1123 DeclContext::lookup_result lr = dc->lookup(*dn_or_err);
1124 for (clang::Decl *candidate : lr) {
1125 if (candidate->getKind() == From->getKind()) {
1126 RegisterImportedDecl(From, candidate);
1131 LLDB_LOG(log,
"[ClangASTImporter] Complete definition not found");
1134 return ASTImporter::ImportImpl(From);
1138 clang::Decl *to, clang::Decl *from) {
1141 auto getDeclName = [](Decl
const *decl) {
1142 std::string name_string;
1143 if (
auto const *from_named_decl = dyn_cast<clang::NamedDecl>(decl)) {
1144 llvm::raw_string_ostream name_stream(name_string);
1145 from_named_decl->printName(name_stream);
1152 if (
auto *D = GetAlreadyImportedOrNull(from); D && D != to) {
1155 "[ClangASTImporter] ERROR: overwriting an already imported decl "
1156 "'{0:x}' ('{1}') from '{2:x}' with '{3:x}'. Likely due to a name "
1157 "conflict when importing '{1}'.",
1158 D, getDeclName(from), from, to);
1169 MapImported(from, to);
1171 if (llvm::Error err = ImportDefinition(from)) {
1173 "[ClangASTImporter] Error during importing definition: {0}");
1177 if (clang::TagDecl *to_tag = dyn_cast<clang::TagDecl>(to)) {
1178 if (clang::TagDecl *from_tag = dyn_cast<clang::TagDecl>(from)) {
1179 to_tag->setCompleteDefinition(from_tag->isCompleteDefinition());
1183 "==== [ClangASTImporter][TUDecl: {0:x}] Imported "
1184 "({1}Decl*){2:x}, named {3} (from "
1186 static_cast<void *
>(to->getTranslationUnitDecl()),
1187 from->getDeclKindName(),
static_cast<void *
>(to),
1188 getDeclName(from),
static_cast<void *
>(from));
1191 std::string ast_string;
1192 llvm::raw_string_ostream ast_stream(ast_string);
1193 to->getTranslationUnitDecl()->dump(ast_stream);
1194 LLDB_LOG(log_ast,
"{0}", ast_string);
1203 if (ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to)) {
1204 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
1209 ObjCInterfaceDecl *from_objc_interface = dyn_cast<ObjCInterfaceDecl>(from);
1211 if (!from_objc_interface)
1214 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
1216 if (!from_superclass)
1219 llvm::Expected<Decl *> imported_from_superclass_decl =
1222 if (!imported_from_superclass_decl) {
1224 "Couldn't import decl: {0}");
1228 ObjCInterfaceDecl *imported_from_superclass =
1229 dyn_cast<ObjCInterfaceDecl>(*imported_from_superclass_decl);
1231 if (!imported_from_superclass)
1234 if (!to_objc_interface->hasDefinition())
1235 to_objc_interface->startDefinition();
1237 to_objc_interface->setSuperClass(m_source_ctx->getTrivialTypeSourceInfo(
1238 m_source_ctx->getObjCInterfaceType(imported_from_superclass)));
1247 CXXMethodDecl *to_method) {
1248 if (!to_method->isVirtual())
1250 QualType return_type = to_method->getReturnType();
1251 if (!return_type->isPointerType() && !return_type->isReferenceType())
1254 clang::RecordDecl *rd = return_type->getPointeeType()->getAsRecordDecl();
1257 if (rd->getDefinition())
1272 from_source.
GetIDForModule(module->Parent), from_source, to_source);
1275 module->IsExplicit);
1284 if (m_decls_to_ignore.count(to))
1288 auto *from_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>(
1289 getFromContext().getExternalSource());
1291 auto *to_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>(
1292 getToContext().getExternalSource());
1293 if (from_source && to_source) {
1302 if (std::optional<ClangASTMetadata> metadata = m_main.GetDeclMetadata(from))
1303 user_id = metadata->GetUserID();
1306 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
1307 std::string name_string;
1308 llvm::raw_string_ostream name_stream(name_string);
1309 from_named_decl->printName(name_stream);
1313 " [ClangASTImporter] Imported ({0}Decl*){1:x}, named {2} (from "
1314 "(Decl*){3:x}), metadata {4}",
1315 from->getDeclKindName(), to, name_string, from, user_id);
1318 " [ClangASTImporter] Imported ({0}Decl*){1:x} (from "
1319 "(Decl*){2:x}), metadata {3}",
1320 from->getDeclKindName(), to, from, user_id);
1325 m_main.GetContextMetadata(&to->getASTContext());
1327 m_main.MaybeGetContextMetadata(m_source_ctx);
1329 if (from_context_md) {
1330 DeclOrigin origin = from_context_md->getOrigin(from);
1332 if (origin.
Valid()) {
1333 if (origin.
ctx != &to->getASTContext()) {
1335 to_context_md->setOrigin(to, origin);
1338 " [ClangASTImporter] Propagated origin "
1339 "(Decl*){0:x}/(ASTContext*){1:x} from (ASTContext*){2:x} to "
1340 "(ASTContext*){3:x}",
1341 origin.
decl, origin.
ctx, &from->getASTContext(),
1342 &to->getASTContext());
1345 if (m_new_decl_listener)
1346 m_new_decl_listener->NewDeclImported(from, to);
1349 to_context_md->setOrigin(to,
DeclOrigin(m_source_ctx, from));
1352 " [ClangASTImporter] Decl has no origin information in "
1353 "(ASTContext*){0:x}",
1354 &from->getASTContext());
1357 if (
auto *to_namespace = dyn_cast<clang::NamespaceDecl>(to)) {
1358 auto *from_namespace = cast<clang::NamespaceDecl>(from);
1362 NamespaceMetaMap::iterator namespace_map_iter =
1363 namespace_maps.find(from_namespace);
1365 if (namespace_map_iter != namespace_maps.end())
1366 to_context_md->m_namespace_maps[to_namespace] =
1367 namespace_map_iter->second;
1370 to_context_md->setOrigin(to,
DeclOrigin(m_source_ctx, from));
1373 " [ClangASTImporter] Sourced origin "
1374 "(Decl*){0:x}/(ASTContext*){1:x} into (ASTContext*){2:x}",
1375 from, m_source_ctx, &to->getASTContext());
1378 if (
auto *to_tag_decl = dyn_cast<TagDecl>(to)) {
1379 to_tag_decl->setHasExternalLexicalStorage();
1380 to_tag_decl->getPrimaryContext()->setMustBuildLookupTable();
1381 auto from_tag_decl = cast<TagDecl>(from);
1385 " [ClangASTImporter] To is a TagDecl - attributes {0}{1} [{2}->{3}]",
1386 (to_tag_decl->hasExternalLexicalStorage() ?
" Lexical" :
""),
1387 (to_tag_decl->hasExternalVisibleStorage() ?
" Visible" :
""),
1388 (from_tag_decl->isCompleteDefinition() ?
"complete" :
"incomplete"),
1389 (to_tag_decl->isCompleteDefinition() ?
"complete" :
"incomplete"));
1392 if (
auto *to_namespace_decl = dyn_cast<NamespaceDecl>(to)) {
1393 m_main.BuildNamespaceMap(to_namespace_decl);
1394 to_namespace_decl->setHasExternalVisibleStorage();
1397 if (
auto *to_container_decl = dyn_cast<ObjCContainerDecl>(to)) {
1398 to_container_decl->setHasExternalLexicalStorage();
1399 to_container_decl->setHasExternalVisibleStorage();
1402 if (ObjCInterfaceDecl *to_interface_decl =
1403 llvm::dyn_cast<ObjCInterfaceDecl>(to_container_decl)) {
1406 " [ClangASTImporter] To is an ObjCInterfaceDecl - attributes "
1408 (to_interface_decl->hasExternalLexicalStorage() ?
" Lexical" :
""),
1409 (to_interface_decl->hasExternalVisibleStorage() ?
" Visible" :
""),
1410 (to_interface_decl->hasDefinition() ?
" HasDefinition" :
""));
1413 log,
" [ClangASTImporter] To is an {0}Decl - attributes {1}{2}",
1414 ((Decl *)to_container_decl)->getDeclKindName(),
1415 (to_container_decl->hasExternalLexicalStorage() ?
" Lexical" :
""),
1416 (to_container_decl->hasExternalVisibleStorage() ?
" Visible" :
""));
1421 if (clang::CXXMethodDecl *to_method = dyn_cast<CXXMethodDecl>(to))
1427 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)())
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
std::shared_ptr< TypeSystemType > dyn_cast_or_null()
Return a shared_ptr<TypeSystemType> if dyn_cast succeeds.
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.
std::optional< clang::Decl * > Import(clang::Decl *d)
Attempts to import the given decl into the target ASTContext by deserializing it from the 'std' modul...
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.
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
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
void ImportDefinitionTo(clang::Decl *to, clang::Decl *from)
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)