1 //===-- ClangASTImporter.cpp ------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Symbol/ClangASTImporter.h"
10 #include "lldb/Core/Module.h"
11 #include "lldb/Symbol/ClangASTContext.h"
12 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
13 #include "lldb/Symbol/ClangUtil.h"
14 #include "lldb/Utility/LLDBAssert.h"
15 #include "lldb/Utility/Log.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/Sema/Lookup.h"
20 #include "clang/Sema/Sema.h"
21 #include "llvm/Support/raw_ostream.h"
25 using namespace lldb_private;
26 using namespace clang;
28 ClangASTMetrics::Counters ClangASTMetrics::global_counters = {0, 0, 0, 0, 0, 0};
29 ClangASTMetrics::Counters ClangASTMetrics::local_counters = {0, 0, 0, 0, 0, 0};
31 void ClangASTMetrics::DumpCounters(Log *log,
32 ClangASTMetrics::Counters &counters) {
33 log->Printf(" Number of visible Decl queries by name : %" PRIu64,
34 counters.m_visible_query_count);
35 log->Printf(" Number of lexical Decl queries : %" PRIu64,
36 counters.m_lexical_query_count);
37 log->Printf(" Number of imports initiated by LLDB : %" PRIu64,
38 counters.m_lldb_import_count);
39 log->Printf(" Number of imports conducted by Clang : %" PRIu64,
40 counters.m_clang_import_count);
41 log->Printf(" Number of Decls completed : %" PRIu64,
42 counters.m_decls_completed_count);
43 log->Printf(" Number of records laid out : %" PRIu64,
44 counters.m_record_layout_count);
47 void ClangASTMetrics::DumpCounters(Log *log) {
51 log->Printf("== ClangASTMetrics output ==");
52 log->Printf("-- Global metrics --");
53 DumpCounters(log, global_counters);
54 log->Printf("-- Local metrics --");
55 DumpCounters(log, local_counters);
58 clang::QualType ClangASTImporter::CopyType(clang::ASTContext *dst_ast,
59 clang::ASTContext *src_ast,
60 clang::QualType type) {
61 ImporterDelegateSP delegate_sp(GetDelegate(dst_ast, src_ast));
63 ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp, dst_ast);
68 llvm::Expected<QualType> ret_or_error = delegate_sp->Import(type);
71 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
72 LLDB_LOG_ERROR(log, ret_or_error.takeError(),
73 "Couldn't import type: {0}");
79 lldb::opaque_compiler_type_t
80 ClangASTImporter::CopyType(clang::ASTContext *dst_ast,
81 clang::ASTContext *src_ast,
82 lldb::opaque_compiler_type_t type) {
83 return CopyType(dst_ast, src_ast, QualType::getFromOpaquePtr(type))
87 CompilerType ClangASTImporter::CopyType(ClangASTContext &dst_ast,
88 const CompilerType &src_type) {
89 clang::ASTContext *dst_clang_ast = dst_ast.getASTContext();
91 ClangASTContext *src_ast =
92 llvm::dyn_cast_or_null<ClangASTContext>(src_type.GetTypeSystem());
94 clang::ASTContext *src_clang_ast = src_ast->getASTContext();
96 lldb::opaque_compiler_type_t dst_clang_type = CopyType(
97 dst_clang_ast, src_clang_ast, src_type.GetOpaqueQualType());
100 return CompilerType(&dst_ast, dst_clang_type);
104 return CompilerType();
107 clang::Decl *ClangASTImporter::CopyDecl(clang::ASTContext *dst_ast,
108 clang::ASTContext *src_ast,
110 ImporterDelegateSP delegate_sp;
112 delegate_sp = GetDelegate(dst_ast, src_ast);
114 ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp, dst_ast);
119 llvm::Expected<clang::Decl *> result = delegate_sp->Import(decl);
121 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
122 LLDB_LOG_ERROR(log, result.takeError(), "Couldn't import decl: {0}");
124 lldb::user_id_t user_id = LLDB_INVALID_UID;
125 ClangASTMetadata *metadata = GetDeclMetadata(decl);
127 user_id = metadata->GetUserID();
129 if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
130 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s "
131 "'%s', metadata 0x%" PRIx64,
132 decl->getDeclKindName(),
133 named_decl->getNameAsString().c_str(), user_id);
135 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s, "
136 "metadata 0x%" PRIx64,
137 decl->getDeclKindName(), user_id);
145 class DeclContextOverride {
148 clang::DeclContext *decl_context;
149 clang::DeclContext *lexical_decl_context;
152 std::map<clang::Decl *, Backup> m_backups;
154 void OverrideOne(clang::Decl *decl) {
155 if (m_backups.find(decl) != m_backups.end()) {
159 m_backups[decl] = {decl->getDeclContext(), decl->getLexicalDeclContext()};
161 decl->setDeclContext(decl->getASTContext().getTranslationUnitDecl());
162 decl->setLexicalDeclContext(decl->getASTContext().getTranslationUnitDecl());
165 bool ChainPassesThrough(
166 clang::Decl *decl, clang::DeclContext *base,
167 clang::DeclContext *(clang::Decl::*contextFromDecl)(),
168 clang::DeclContext *(clang::DeclContext::*contextFromContext)()) {
169 for (DeclContext *decl_ctx = (decl->*contextFromDecl)(); decl_ctx;
170 decl_ctx = (decl_ctx->*contextFromContext)()) {
171 if (decl_ctx == base) {
179 clang::Decl *GetEscapedChild(clang::Decl *decl,
180 clang::DeclContext *base = nullptr) {
182 // decl's DeclContext chains must pass through base.
184 if (!ChainPassesThrough(decl, base, &clang::Decl::getDeclContext,
185 &clang::DeclContext::getParent) ||
186 !ChainPassesThrough(decl, base, &clang::Decl::getLexicalDeclContext,
187 &clang::DeclContext::getLexicalParent)) {
191 base = clang::dyn_cast<clang::DeclContext>(decl);
198 if (clang::DeclContext *context =
199 clang::dyn_cast<clang::DeclContext>(decl)) {
200 for (clang::Decl *decl : context->decls()) {
201 if (clang::Decl *escaped_child = GetEscapedChild(decl)) {
202 return escaped_child;
210 void Override(clang::Decl *decl) {
211 if (clang::Decl *escaped_child = GetEscapedChild(decl)) {
212 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
215 log->Printf(" [ClangASTImporter] DeclContextOverride couldn't "
216 "override (%sDecl*)%p - its child (%sDecl*)%p escapes",
217 decl->getDeclKindName(), static_cast<void *>(decl),
218 escaped_child->getDeclKindName(),
219 static_cast<void *>(escaped_child));
220 lldbassert(0 && "Couldn't override!");
227 DeclContextOverride() {}
229 void OverrideAllDeclsFromContainingFunction(clang::Decl *decl) {
230 for (DeclContext *decl_context = decl->getLexicalDeclContext();
231 decl_context; decl_context = decl_context->getLexicalParent()) {
232 DeclContext *redecl_context = decl_context->getRedeclContext();
234 if (llvm::isa<FunctionDecl>(redecl_context) &&
235 llvm::isa<TranslationUnitDecl>(redecl_context->getLexicalParent())) {
236 for (clang::Decl *child_decl : decl_context->decls()) {
237 Override(child_decl);
243 ~DeclContextOverride() {
244 for (const std::pair<clang::Decl *, Backup> &backup : m_backups) {
245 backup.first->setDeclContext(backup.second.decl_context);
246 backup.first->setLexicalDeclContext(backup.second.lexical_decl_context);
251 lldb::opaque_compiler_type_t
252 ClangASTImporter::DeportType(clang::ASTContext *dst_ctx,
253 clang::ASTContext *src_ctx,
254 lldb::opaque_compiler_type_t type) {
255 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
258 log->Printf(" [ClangASTImporter] DeportType called on (%sType*)0x%llx "
259 "from (ASTContext*)%p to (ASTContext*)%p",
260 QualType::getFromOpaquePtr(type)->getTypeClassName(),
261 (unsigned long long)type, static_cast<void *>(src_ctx),
262 static_cast<void *>(dst_ctx));
264 ImporterDelegateSP delegate_sp(GetDelegate(dst_ctx, src_ctx));
269 std::set<NamedDecl *> decls_to_deport;
270 std::set<NamedDecl *> decls_already_deported;
272 DeclContextOverride decl_context_override;
274 if (const clang::TagType *tag_type =
275 clang::QualType::getFromOpaquePtr(type)->getAs<TagType>()) {
276 decl_context_override.OverrideAllDeclsFromContainingFunction(
277 tag_type->getDecl());
280 delegate_sp->InitDeportWorkQueues(&decls_to_deport, &decls_already_deported);
282 lldb::opaque_compiler_type_t result = CopyType(dst_ctx, src_ctx, type);
284 delegate_sp->ExecuteDeportWorkQueues();
292 clang::Decl *ClangASTImporter::DeportDecl(clang::ASTContext *dst_ctx,
293 clang::ASTContext *src_ctx,
295 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
298 log->Printf(" [ClangASTImporter] DeportDecl called on (%sDecl*)%p from "
299 "(ASTContext*)%p to (ASTContext*)%p",
300 decl->getDeclKindName(), static_cast<void *>(decl),
301 static_cast<void *>(src_ctx), static_cast<void *>(dst_ctx));
303 ImporterDelegateSP delegate_sp(GetDelegate(dst_ctx, src_ctx));
308 std::set<NamedDecl *> decls_to_deport;
309 std::set<NamedDecl *> decls_already_deported;
311 DeclContextOverride decl_context_override;
313 decl_context_override.OverrideAllDeclsFromContainingFunction(decl);
315 delegate_sp->InitDeportWorkQueues(&decls_to_deport, &decls_already_deported);
317 clang::Decl *result = CopyDecl(dst_ctx, src_ctx, decl);
319 delegate_sp->ExecuteDeportWorkQueues();
326 " [ClangASTImporter] DeportDecl deported (%sDecl*)%p to (%sDecl*)%p",
327 decl->getDeclKindName(), static_cast<void *>(decl),
328 result->getDeclKindName(), static_cast<void *>(result));
333 bool ClangASTImporter::CanImport(const CompilerType &type) {
334 if (!ClangUtil::IsClangType(type))
337 // TODO: remove external completion BOOL
338 // CompleteAndFetchChildren should get the Decl out and check for the
340 clang::QualType qual_type(
341 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
343 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
344 switch (type_class) {
345 case clang::Type::Record: {
346 const clang::CXXRecordDecl *cxx_record_decl =
347 qual_type->getAsCXXRecordDecl();
348 if (cxx_record_decl) {
349 if (ResolveDeclOrigin(cxx_record_decl, nullptr, nullptr))
354 case clang::Type::Enum: {
355 clang::EnumDecl *enum_decl =
356 llvm::cast<clang::EnumType>(qual_type)->getDecl();
358 if (ResolveDeclOrigin(enum_decl, nullptr, nullptr))
363 case clang::Type::ObjCObject:
364 case clang::Type::ObjCInterface: {
365 const clang::ObjCObjectType *objc_class_type =
366 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
367 if (objc_class_type) {
368 clang::ObjCInterfaceDecl *class_interface_decl =
369 objc_class_type->getInterface();
370 // We currently can't complete objective C types through the newly added
371 // ASTContext because it only supports TagDecl objects right now...
372 if (class_interface_decl) {
373 if (ResolveDeclOrigin(class_interface_decl, nullptr, nullptr))
379 case clang::Type::Typedef:
380 return CanImport(CompilerType(type.GetTypeSystem(),
381 llvm::cast<clang::TypedefType>(qual_type)
383 ->getUnderlyingType()
386 case clang::Type::Auto:
387 return CanImport(CompilerType(type.GetTypeSystem(),
388 llvm::cast<clang::AutoType>(qual_type)
392 case clang::Type::Elaborated:
393 return CanImport(CompilerType(type.GetTypeSystem(),
394 llvm::cast<clang::ElaboratedType>(qual_type)
398 case clang::Type::Paren:
399 return CanImport(CompilerType(
400 type.GetTypeSystem(),
401 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
410 bool ClangASTImporter::Import(const CompilerType &type) {
411 if (!ClangUtil::IsClangType(type))
413 // TODO: remove external completion BOOL
414 // CompleteAndFetchChildren should get the Decl out and check for the
416 clang::QualType qual_type(
417 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
419 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
420 switch (type_class) {
421 case clang::Type::Record: {
422 const clang::CXXRecordDecl *cxx_record_decl =
423 qual_type->getAsCXXRecordDecl();
424 if (cxx_record_decl) {
425 if (ResolveDeclOrigin(cxx_record_decl, nullptr, nullptr))
426 return CompleteAndFetchChildren(qual_type);
430 case clang::Type::Enum: {
431 clang::EnumDecl *enum_decl =
432 llvm::cast<clang::EnumType>(qual_type)->getDecl();
434 if (ResolveDeclOrigin(enum_decl, nullptr, nullptr))
435 return CompleteAndFetchChildren(qual_type);
439 case clang::Type::ObjCObject:
440 case clang::Type::ObjCInterface: {
441 const clang::ObjCObjectType *objc_class_type =
442 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
443 if (objc_class_type) {
444 clang::ObjCInterfaceDecl *class_interface_decl =
445 objc_class_type->getInterface();
446 // We currently can't complete objective C types through the newly added
447 // ASTContext because it only supports TagDecl objects right now...
448 if (class_interface_decl) {
449 if (ResolveDeclOrigin(class_interface_decl, nullptr, nullptr))
450 return CompleteAndFetchChildren(qual_type);
455 case clang::Type::Typedef:
456 return Import(CompilerType(type.GetTypeSystem(),
457 llvm::cast<clang::TypedefType>(qual_type)
459 ->getUnderlyingType()
462 case clang::Type::Auto:
463 return Import(CompilerType(type.GetTypeSystem(),
464 llvm::cast<clang::AutoType>(qual_type)
468 case clang::Type::Elaborated:
469 return Import(CompilerType(type.GetTypeSystem(),
470 llvm::cast<clang::ElaboratedType>(qual_type)
474 case clang::Type::Paren:
475 return Import(CompilerType(
476 type.GetTypeSystem(),
477 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
485 bool ClangASTImporter::CompleteType(const CompilerType &compiler_type) {
486 if (!CanImport(compiler_type))
489 if (Import(compiler_type)) {
490 ClangASTContext::CompleteTagDeclarationDefinition(compiler_type);
494 ClangASTContext::SetHasExternalStorage(compiler_type.GetOpaqueQualType(),
499 bool ClangASTImporter::LayoutRecordType(
500 const clang::RecordDecl *record_decl, uint64_t &bit_size,
502 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
503 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
505 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
507 RecordDeclToLayoutMap::iterator pos =
508 m_record_decl_to_layout_map.find(record_decl);
509 bool success = false;
510 base_offsets.clear();
511 vbase_offsets.clear();
512 if (pos != m_record_decl_to_layout_map.end()) {
513 bit_size = pos->second.bit_size;
514 alignment = pos->second.alignment;
515 field_offsets.swap(pos->second.field_offsets);
516 base_offsets.swap(pos->second.base_offsets);
517 vbase_offsets.swap(pos->second.vbase_offsets);
518 m_record_decl_to_layout_map.erase(pos);
523 field_offsets.clear();
528 void ClangASTImporter::InsertRecordDecl(clang::RecordDecl *decl,
529 const LayoutInfo &layout) {
530 m_record_decl_to_layout_map.insert(std::make_pair(decl, layout));
533 void ClangASTImporter::CompleteDecl(clang::Decl *decl) {
534 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
537 log->Printf(" [ClangASTImporter] CompleteDecl called on (%sDecl*)%p",
538 decl->getDeclKindName(), static_cast<void *>(decl));
540 if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl)) {
541 if (!interface_decl->getDefinition()) {
542 interface_decl->startDefinition();
543 CompleteObjCInterfaceDecl(interface_decl);
545 } else if (ObjCProtocolDecl *protocol_decl =
546 dyn_cast<ObjCProtocolDecl>(decl)) {
547 if (!protocol_decl->getDefinition())
548 protocol_decl->startDefinition();
549 } else if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl)) {
550 if (!tag_decl->getDefinition() && !tag_decl->isBeingDefined()) {
551 tag_decl->startDefinition();
552 CompleteTagDecl(tag_decl);
553 tag_decl->setCompleteDefinition(true);
556 assert(0 && "CompleteDecl called on a Decl that can't be completed");
560 bool ClangASTImporter::CompleteTagDecl(clang::TagDecl *decl) {
561 ClangASTMetrics::RegisterDeclCompletion();
563 DeclOrigin decl_origin = GetDeclOrigin(decl);
565 if (!decl_origin.Valid())
568 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
571 ImporterDelegateSP delegate_sp(
572 GetDelegate(&decl->getASTContext(), decl_origin.ctx));
574 ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp,
575 &decl->getASTContext());
577 delegate_sp->ImportDefinitionTo(decl, decl_origin.decl);
582 bool ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl,
583 clang::TagDecl *origin_decl) {
584 ClangASTMetrics::RegisterDeclCompletion();
586 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
588 if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
591 ImporterDelegateSP delegate_sp(
592 GetDelegate(&decl->getASTContext(), origin_ast_ctx));
595 delegate_sp->ImportDefinitionTo(decl, origin_decl);
597 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
599 OriginMap &origins = context_md->m_origins;
601 origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
606 bool ClangASTImporter::CompleteObjCInterfaceDecl(
607 clang::ObjCInterfaceDecl *interface_decl) {
608 ClangASTMetrics::RegisterDeclCompletion();
610 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
612 if (!decl_origin.Valid())
615 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
618 ImporterDelegateSP delegate_sp(
619 GetDelegate(&interface_decl->getASTContext(), decl_origin.ctx));
622 delegate_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
624 if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass())
625 RequireCompleteType(clang::QualType(super_class->getTypeForDecl(), 0));
630 bool ClangASTImporter::CompleteAndFetchChildren(clang::QualType type) {
631 if (!RequireCompleteType(type))
634 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
636 if (const TagType *tag_type = type->getAs<TagType>()) {
637 TagDecl *tag_decl = tag_type->getDecl();
639 DeclOrigin decl_origin = GetDeclOrigin(tag_decl);
641 if (!decl_origin.Valid())
644 ImporterDelegateSP delegate_sp(
645 GetDelegate(&tag_decl->getASTContext(), decl_origin.ctx));
647 ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp,
648 &tag_decl->getASTContext());
650 TagDecl *origin_tag_decl = llvm::dyn_cast<TagDecl>(decl_origin.decl);
652 for (Decl *origin_child_decl : origin_tag_decl->decls()) {
653 llvm::Expected<Decl *> imported_or_err =
654 delegate_sp->Import(origin_child_decl);
655 if (!imported_or_err) {
656 LLDB_LOG_ERROR(log, imported_or_err.takeError(),
657 "Couldn't import decl: {0}");
662 if (RecordDecl *record_decl = dyn_cast<RecordDecl>(origin_tag_decl)) {
663 record_decl->setHasLoadedFieldsFromExternalStorage(true);
669 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
670 if (ObjCInterfaceDecl *objc_interface_decl =
671 objc_object_type->getInterface()) {
672 DeclOrigin decl_origin = GetDeclOrigin(objc_interface_decl);
674 if (!decl_origin.Valid())
677 ImporterDelegateSP delegate_sp(
678 GetDelegate(&objc_interface_decl->getASTContext(), decl_origin.ctx));
680 ObjCInterfaceDecl *origin_interface_decl =
681 llvm::dyn_cast<ObjCInterfaceDecl>(decl_origin.decl);
683 for (Decl *origin_child_decl : origin_interface_decl->decls()) {
684 llvm::Expected<Decl *> imported_or_err =
685 delegate_sp->Import(origin_child_decl);
686 if (!imported_or_err) {
687 LLDB_LOG_ERROR(log, imported_or_err.takeError(),
688 "Couldn't import decl: {0}");
702 bool ClangASTImporter::RequireCompleteType(clang::QualType type) {
706 if (const TagType *tag_type = type->getAs<TagType>()) {
707 TagDecl *tag_decl = tag_type->getDecl();
709 if (tag_decl->getDefinition() || tag_decl->isBeingDefined())
712 return CompleteTagDecl(tag_decl);
714 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
715 if (ObjCInterfaceDecl *objc_interface_decl =
716 objc_object_type->getInterface())
717 return CompleteObjCInterfaceDecl(objc_interface_decl);
721 if (const ArrayType *array_type = type->getAsArrayTypeUnsafe()) {
722 return RequireCompleteType(array_type->getElementType());
724 if (const AtomicType *atomic_type = type->getAs<AtomicType>()) {
725 return RequireCompleteType(atomic_type->getPointeeType());
731 ClangASTMetadata *ClangASTImporter::GetDeclMetadata(const clang::Decl *decl) {
732 DeclOrigin decl_origin = GetDeclOrigin(decl);
734 if (decl_origin.Valid())
735 return ClangASTContext::GetMetadata(decl_origin.ctx, decl_origin.decl);
737 return ClangASTContext::GetMetadata(&decl->getASTContext(), decl);
740 ClangASTImporter::DeclOrigin
741 ClangASTImporter::GetDeclOrigin(const clang::Decl *decl) {
742 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
744 OriginMap &origins = context_md->m_origins;
746 OriginMap::iterator iter = origins.find(decl);
748 if (iter != origins.end())
754 void ClangASTImporter::SetDeclOrigin(const clang::Decl *decl,
755 clang::Decl *original_decl) {
756 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
758 OriginMap &origins = context_md->m_origins;
760 OriginMap::iterator iter = origins.find(decl);
762 if (iter != origins.end()) {
763 iter->second.decl = original_decl;
764 iter->second.ctx = &original_decl->getASTContext();
766 origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
770 void ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
771 NamespaceMapSP &namespace_map) {
772 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
774 context_md->m_namespace_maps[decl] = namespace_map;
777 ClangASTImporter::NamespaceMapSP
778 ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl) {
779 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
781 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
783 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
785 if (iter != namespace_maps.end())
788 return NamespaceMapSP();
791 void ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl) {
793 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
795 const DeclContext *parent_context = decl->getDeclContext();
796 const NamespaceDecl *parent_namespace =
797 dyn_cast<NamespaceDecl>(parent_context);
798 NamespaceMapSP parent_map;
800 if (parent_namespace)
801 parent_map = GetNamespaceMap(parent_namespace);
803 NamespaceMapSP new_map;
805 new_map = std::make_shared<NamespaceMap>();
807 if (context_md->m_map_completer) {
808 std::string namespace_string = decl->getDeclName().getAsString();
810 context_md->m_map_completer->CompleteNamespaceMap(
811 new_map, ConstString(namespace_string.c_str()), parent_map);
814 context_md->m_namespace_maps[decl] = new_map;
817 void ClangASTImporter::ForgetDestination(clang::ASTContext *dst_ast) {
818 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
821 log->Printf(" [ClangASTImporter] Forgetting destination (ASTContext*)%p",
822 static_cast<void *>(dst_ast));
824 m_metadata_map.erase(dst_ast);
827 void ClangASTImporter::ForgetSource(clang::ASTContext *dst_ast,
828 clang::ASTContext *src_ast) {
829 ASTContextMetadataSP md = MaybeGetContextMetadata(dst_ast);
831 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
834 log->Printf(" [ClangASTImporter] Forgetting source->dest "
835 "(ASTContext*)%p->(ASTContext*)%p",
836 static_cast<void *>(src_ast), static_cast<void *>(dst_ast));
841 md->m_delegates.erase(src_ast);
843 for (OriginMap::iterator iter = md->m_origins.begin();
844 iter != md->m_origins.end();) {
845 if (iter->second.ctx == src_ast)
846 md->m_origins.erase(iter++);
852 ClangASTImporter::MapCompleter::~MapCompleter() { return; }
854 llvm::Expected<Decl *>
855 ClangASTImporter::ASTImporterDelegate::ImportImpl(Decl *From) {
857 llvm::Optional<Decl *> D = m_std_handler->Import(From);
859 // Make sure we don't use this decl later to map it back to it's original
860 // decl. The decl the CxxModuleHandler created has nothing to do with
861 // the one from debug info, and linking those two would just cause the
862 // ASTImporter to try 'updating' the module decl with the minimal one from
864 m_decls_to_ignore.insert(*D);
869 return ASTImporter::ImportImpl(From);
872 void ClangASTImporter::ASTImporterDelegate::InitDeportWorkQueues(
873 std::set<clang::NamedDecl *> *decls_to_deport,
874 std::set<clang::NamedDecl *> *decls_already_deported) {
875 assert(!m_decls_to_deport);
876 assert(!m_decls_already_deported);
878 m_decls_to_deport = decls_to_deport;
879 m_decls_already_deported = decls_already_deported;
882 void ClangASTImporter::ASTImporterDelegate::ExecuteDeportWorkQueues() {
883 assert(m_decls_to_deport);
884 assert(m_decls_already_deported);
886 ASTContextMetadataSP to_context_md =
887 m_master.GetContextMetadata(&getToContext());
889 while (!m_decls_to_deport->empty()) {
890 NamedDecl *decl = *m_decls_to_deport->begin();
892 m_decls_already_deported->insert(decl);
893 m_decls_to_deport->erase(decl);
895 DeclOrigin &origin = to_context_md->m_origins[decl];
896 UNUSED_IF_ASSERT_DISABLED(origin);
899 m_source_ctx); // otherwise we should never have added this
900 // because it doesn't need to be deported
902 Decl *original_decl = to_context_md->m_origins[decl].decl;
904 ClangASTContext::GetCompleteDecl(m_source_ctx, original_decl);
906 if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl)) {
907 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl)) {
908 if (original_tag_decl->isCompleteDefinition()) {
909 ImportDefinitionTo(tag_decl, original_tag_decl);
910 tag_decl->setCompleteDefinition(true);
914 tag_decl->setHasExternalLexicalStorage(false);
915 tag_decl->setHasExternalVisibleStorage(false);
916 } else if (ObjCContainerDecl *container_decl =
917 dyn_cast<ObjCContainerDecl>(decl)) {
918 container_decl->setHasExternalLexicalStorage(false);
919 container_decl->setHasExternalVisibleStorage(false);
922 to_context_md->m_origins.erase(decl);
925 m_decls_to_deport = nullptr;
926 m_decls_already_deported = nullptr;
929 void ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo(
930 clang::Decl *to, clang::Decl *from) {
931 ASTImporter::Imported(from, to);
934 if (to_objc_interface)
935 to_objc_interface->startDefinition();
937 CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
940 to_cxx_record->startDefinition();
943 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
945 if (llvm::Error err = ImportDefinition(from)) {
946 LLDB_LOG_ERROR(log, std::move(err),
947 "[ClangASTImporter] Error during importing definition: {0}");
951 if (clang::TagDecl *to_tag = dyn_cast<clang::TagDecl>(to)) {
952 if (clang::TagDecl *from_tag = dyn_cast<clang::TagDecl>(from)) {
953 to_tag->setCompleteDefinition(from_tag->isCompleteDefinition());
956 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_AST)) {
957 std::string name_string;
958 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
959 llvm::raw_string_ostream name_stream(name_string);
960 from_named_decl->printName(name_stream);
963 LLDB_LOG(log_ast, "==== [ClangASTImporter][TUDecl: {0}] Imported "
964 "({1}Decl*){2}, named {3} (from "
966 static_cast<void *>(to->getTranslationUnitDecl()),
967 from->getDeclKindName(), static_cast<void *>(to), name_string,
968 static_cast<void *>(from));
970 // Log the AST of the TU.
971 std::string ast_string;
972 llvm::raw_string_ostream ast_stream(ast_string);
973 to->getTranslationUnitDecl()->dump(ast_stream);
974 LLDB_LOG(log_ast, "{0}", ast_string);
979 // If we're dealing with an Objective-C class, ensure that the inheritance
980 // has been set up correctly. The ASTImporter may not do this correctly if
981 // the class was originally sourced from symbols.
983 if (ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to)) {
985 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
988 break; // we're not going to override it if it's set
990 ObjCInterfaceDecl *from_objc_interface =
991 dyn_cast<ObjCInterfaceDecl>(from);
993 if (!from_objc_interface)
996 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
998 if (!from_superclass)
1001 llvm::Expected<Decl *> imported_from_superclass_decl =
1002 Import(from_superclass);
1004 if (!imported_from_superclass_decl) {
1005 LLDB_LOG_ERROR(log, imported_from_superclass_decl.takeError(),
1006 "Couldn't import decl: {0}");
1010 ObjCInterfaceDecl *imported_from_superclass =
1011 dyn_cast<ObjCInterfaceDecl>(*imported_from_superclass_decl);
1013 if (!imported_from_superclass)
1016 if (!to_objc_interface->hasDefinition())
1017 to_objc_interface->startDefinition();
1019 to_objc_interface->setSuperClass(m_source_ctx->getTrivialTypeSourceInfo(
1020 m_source_ctx->getObjCInterfaceType(imported_from_superclass)));
1025 void ClangASTImporter::ASTImporterDelegate::Imported(clang::Decl *from,
1027 ClangASTMetrics::RegisterClangImport();
1029 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1031 // Some decls shouldn't be tracked here because they were not created by
1032 // copying 'from' to 'to'. Just exit early for those.
1033 if (m_decls_to_ignore.find(to) != m_decls_to_ignore.end())
1034 return clang::ASTImporter::Imported(from, to);
1036 lldb::user_id_t user_id = LLDB_INVALID_UID;
1037 ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
1039 user_id = metadata->GetUserID();
1042 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
1043 std::string name_string;
1044 llvm::raw_string_ostream name_stream(name_string);
1045 from_named_decl->printName(name_stream);
1046 name_stream.flush();
1048 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from "
1049 "(Decl*)%p), metadata 0x%" PRIx64,
1050 from->getDeclKindName(), static_cast<void *>(to),
1051 name_string.c_str(), static_cast<void *>(from), user_id);
1053 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from "
1054 "(Decl*)%p), metadata 0x%" PRIx64,
1055 from->getDeclKindName(), static_cast<void *>(to),
1056 static_cast<void *>(from), user_id);
1060 ASTContextMetadataSP to_context_md =
1061 m_master.GetContextMetadata(&to->getASTContext());
1062 ASTContextMetadataSP from_context_md =
1063 m_master.MaybeGetContextMetadata(m_source_ctx);
1065 if (from_context_md) {
1066 OriginMap &origins = from_context_md->m_origins;
1068 OriginMap::iterator origin_iter = origins.find(from);
1070 if (origin_iter != origins.end()) {
1071 if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
1072 user_id != LLDB_INVALID_UID) {
1073 if (origin_iter->second.ctx != &to->getASTContext())
1074 to_context_md->m_origins[to] = origin_iter->second;
1077 ImporterDelegateSP direct_completer =
1078 m_master.GetDelegate(&to->getASTContext(), origin_iter->second.ctx);
1080 if (direct_completer.get() != this)
1081 direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
1084 log->Printf(" [ClangASTImporter] Propagated origin "
1085 "(Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to "
1087 static_cast<void *>(origin_iter->second.decl),
1088 static_cast<void *>(origin_iter->second.ctx),
1089 static_cast<void *>(&from->getASTContext()),
1090 static_cast<void *>(&to->getASTContext()));
1092 if (m_decls_to_deport && m_decls_already_deported) {
1093 if (isa<TagDecl>(to) || isa<ObjCInterfaceDecl>(to)) {
1094 RecordDecl *from_record_decl = dyn_cast<RecordDecl>(from);
1095 if (from_record_decl == nullptr ||
1096 !from_record_decl->isInjectedClassName()) {
1097 NamedDecl *to_named_decl = dyn_cast<NamedDecl>(to);
1099 if (!m_decls_already_deported->count(to_named_decl))
1100 m_decls_to_deport->insert(to_named_decl);
1105 if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
1106 user_id != LLDB_INVALID_UID) {
1107 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
1111 log->Printf(" [ClangASTImporter] Decl has no origin information in "
1113 static_cast<void *>(&from->getASTContext()));
1116 if (clang::NamespaceDecl *to_namespace =
1117 dyn_cast<clang::NamespaceDecl>(to)) {
1118 clang::NamespaceDecl *from_namespace =
1119 dyn_cast<clang::NamespaceDecl>(from);
1121 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
1123 NamespaceMetaMap::iterator namespace_map_iter =
1124 namespace_maps.find(from_namespace);
1126 if (namespace_map_iter != namespace_maps.end())
1127 to_context_md->m_namespace_maps[to_namespace] =
1128 namespace_map_iter->second;
1131 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
1134 log->Printf(" [ClangASTImporter] Sourced origin "
1135 "(Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
1136 static_cast<void *>(from), static_cast<void *>(m_source_ctx),
1137 static_cast<void *>(&to->getASTContext()));
1140 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from)) {
1141 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
1143 to_tag_decl->setHasExternalLexicalStorage();
1144 to_tag_decl->getPrimaryContext()->setMustBuildLookupTable();
1148 " [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
1149 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1150 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
1151 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
1152 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
1155 if (isa<NamespaceDecl>(from)) {
1156 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
1158 m_master.BuildNamespaceMap(to_namespace_decl);
1160 to_namespace_decl->setHasExternalVisibleStorage();
1163 if (isa<ObjCContainerDecl>(from)) {
1164 ObjCContainerDecl *to_container_decl = dyn_cast<ObjCContainerDecl>(to);
1166 to_container_decl->setHasExternalLexicalStorage();
1167 to_container_decl->setHasExternalVisibleStorage();
1169 /*to_interface_decl->setExternallyCompleted();*/
1172 if (ObjCInterfaceDecl *to_interface_decl =
1173 llvm::dyn_cast<ObjCInterfaceDecl>(to_container_decl)) {
1175 " [ClangASTImporter] To is an ObjCInterfaceDecl - attributes "
1177 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1178 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
1179 (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
1182 " [ClangASTImporter] To is an %sDecl - attributes %s%s",
1183 ((Decl *)to_container_decl)->getDeclKindName(),
1184 (to_container_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1185 (to_container_decl->hasExternalVisibleStorage() ? " Visible" : ""));
1192 ClangASTImporter::ASTImporterDelegate::GetOriginalDecl(clang::Decl *To) {
1193 ASTContextMetadataSP to_context_md =
1194 m_master.GetContextMetadata(&To->getASTContext());
1199 OriginMap::iterator iter = to_context_md->m_origins.find(To);
1201 if (iter == to_context_md->m_origins.end())
1204 return const_cast<clang::Decl *>(iter->second.decl);