1 //===-- ClangASTImporter.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Symbol/ClangASTImporter.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Symbol/ClangASTContext.h"
13 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
14 #include "lldb/Symbol/ClangUtil.h"
15 #include "lldb/Utility/LLDBAssert.h"
16 #include "lldb/Utility/Log.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "llvm/Support/raw_ostream.h"
22 using namespace lldb_private;
23 using namespace clang;
25 ClangASTMetrics::Counters ClangASTMetrics::global_counters = {0, 0, 0, 0, 0, 0};
26 ClangASTMetrics::Counters ClangASTMetrics::local_counters = {0, 0, 0, 0, 0, 0};
28 void ClangASTMetrics::DumpCounters(Log *log,
29 ClangASTMetrics::Counters &counters) {
30 log->Printf(" Number of visible Decl queries by name : %" PRIu64,
31 counters.m_visible_query_count);
32 log->Printf(" Number of lexical Decl queries : %" PRIu64,
33 counters.m_lexical_query_count);
34 log->Printf(" Number of imports initiated by LLDB : %" PRIu64,
35 counters.m_lldb_import_count);
36 log->Printf(" Number of imports conducted by Clang : %" PRIu64,
37 counters.m_clang_import_count);
38 log->Printf(" Number of Decls completed : %" PRIu64,
39 counters.m_decls_completed_count);
40 log->Printf(" Number of records laid out : %" PRIu64,
41 counters.m_record_layout_count);
44 void ClangASTMetrics::DumpCounters(Log *log) {
48 log->Printf("== ClangASTMetrics output ==");
49 log->Printf("-- Global metrics --");
50 DumpCounters(log, global_counters);
51 log->Printf("-- Local metrics --");
52 DumpCounters(log, local_counters);
55 clang::QualType ClangASTImporter::CopyType(clang::ASTContext *dst_ast,
56 clang::ASTContext *src_ast,
57 clang::QualType type) {
58 MinionSP minion_sp(GetMinion(dst_ast, src_ast));
61 return minion_sp->Import(type);
66 lldb::opaque_compiler_type_t
67 ClangASTImporter::CopyType(clang::ASTContext *dst_ast,
68 clang::ASTContext *src_ast,
69 lldb::opaque_compiler_type_t type) {
70 return CopyType(dst_ast, src_ast, QualType::getFromOpaquePtr(type))
74 CompilerType ClangASTImporter::CopyType(ClangASTContext &dst_ast,
75 const CompilerType &src_type) {
76 clang::ASTContext *dst_clang_ast = dst_ast.getASTContext();
78 ClangASTContext *src_ast =
79 llvm::dyn_cast_or_null<ClangASTContext>(src_type.GetTypeSystem());
81 clang::ASTContext *src_clang_ast = src_ast->getASTContext();
83 lldb::opaque_compiler_type_t dst_clang_type = CopyType(
84 dst_clang_ast, src_clang_ast, src_type.GetOpaqueQualType());
87 return CompilerType(&dst_ast, dst_clang_type);
91 return CompilerType();
94 clang::Decl *ClangASTImporter::CopyDecl(clang::ASTContext *dst_ast,
95 clang::ASTContext *src_ast,
99 minion_sp = GetMinion(dst_ast, src_ast);
102 clang::Decl *result = minion_sp->Import(decl);
105 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
108 lldb::user_id_t user_id = LLDB_INVALID_UID;
109 ClangASTMetadata *metadata = GetDeclMetadata(decl);
111 user_id = metadata->GetUserID();
113 if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
114 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s "
115 "'%s', metadata 0x%" PRIx64,
116 decl->getDeclKindName(),
117 named_decl->getNameAsString().c_str(), user_id);
119 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s, "
120 "metadata 0x%" PRIx64,
121 decl->getDeclKindName(), user_id);
131 class DeclContextOverride {
134 clang::DeclContext *decl_context;
135 clang::DeclContext *lexical_decl_context;
138 std::map<clang::Decl *, Backup> m_backups;
140 void OverrideOne(clang::Decl *decl) {
141 if (m_backups.find(decl) != m_backups.end()) {
145 m_backups[decl] = {decl->getDeclContext(), decl->getLexicalDeclContext()};
147 decl->setDeclContext(decl->getASTContext().getTranslationUnitDecl());
148 decl->setLexicalDeclContext(decl->getASTContext().getTranslationUnitDecl());
151 bool ChainPassesThrough(
152 clang::Decl *decl, clang::DeclContext *base,
153 clang::DeclContext *(clang::Decl::*contextFromDecl)(),
154 clang::DeclContext *(clang::DeclContext::*contextFromContext)()) {
155 for (DeclContext *decl_ctx = (decl->*contextFromDecl)(); decl_ctx;
156 decl_ctx = (decl_ctx->*contextFromContext)()) {
157 if (decl_ctx == base) {
165 clang::Decl *GetEscapedChild(clang::Decl *decl,
166 clang::DeclContext *base = nullptr) {
168 // decl's DeclContext chains must pass through base.
170 if (!ChainPassesThrough(decl, base, &clang::Decl::getDeclContext,
171 &clang::DeclContext::getParent) ||
172 !ChainPassesThrough(decl, base, &clang::Decl::getLexicalDeclContext,
173 &clang::DeclContext::getLexicalParent)) {
177 base = clang::dyn_cast<clang::DeclContext>(decl);
184 if (clang::DeclContext *context =
185 clang::dyn_cast<clang::DeclContext>(decl)) {
186 for (clang::Decl *decl : context->decls()) {
187 if (clang::Decl *escaped_child = GetEscapedChild(decl)) {
188 return escaped_child;
196 void Override(clang::Decl *decl) {
197 if (clang::Decl *escaped_child = GetEscapedChild(decl)) {
198 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
201 log->Printf(" [ClangASTImporter] DeclContextOverride couldn't "
202 "override (%sDecl*)%p - its child (%sDecl*)%p escapes",
203 decl->getDeclKindName(), static_cast<void *>(decl),
204 escaped_child->getDeclKindName(),
205 static_cast<void *>(escaped_child));
206 lldbassert(0 && "Couldn't override!");
213 DeclContextOverride() {}
215 void OverrideAllDeclsFromContainingFunction(clang::Decl *decl) {
216 for (DeclContext *decl_context = decl->getLexicalDeclContext();
217 decl_context; decl_context = decl_context->getLexicalParent()) {
218 DeclContext *redecl_context = decl_context->getRedeclContext();
220 if (llvm::isa<FunctionDecl>(redecl_context) &&
221 llvm::isa<TranslationUnitDecl>(redecl_context->getLexicalParent())) {
222 for (clang::Decl *child_decl : decl_context->decls()) {
223 Override(child_decl);
229 ~DeclContextOverride() {
230 for (const std::pair<clang::Decl *, Backup> &backup : m_backups) {
231 backup.first->setDeclContext(backup.second.decl_context);
232 backup.first->setLexicalDeclContext(backup.second.lexical_decl_context);
237 lldb::opaque_compiler_type_t
238 ClangASTImporter::DeportType(clang::ASTContext *dst_ctx,
239 clang::ASTContext *src_ctx,
240 lldb::opaque_compiler_type_t type) {
241 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
244 log->Printf(" [ClangASTImporter] DeportType called on (%sType*)0x%llx "
245 "from (ASTContext*)%p to (ASTContext*)%p",
246 QualType::getFromOpaquePtr(type)->getTypeClassName(),
247 (unsigned long long)type, static_cast<void *>(src_ctx),
248 static_cast<void *>(dst_ctx));
250 MinionSP minion_sp(GetMinion(dst_ctx, src_ctx));
255 std::set<NamedDecl *> decls_to_deport;
256 std::set<NamedDecl *> decls_already_deported;
258 DeclContextOverride decl_context_override;
260 if (const clang::TagType *tag_type =
261 clang::QualType::getFromOpaquePtr(type)->getAs<TagType>()) {
262 decl_context_override.OverrideAllDeclsFromContainingFunction(
263 tag_type->getDecl());
266 minion_sp->InitDeportWorkQueues(&decls_to_deport, &decls_already_deported);
268 lldb::opaque_compiler_type_t result = CopyType(dst_ctx, src_ctx, type);
270 minion_sp->ExecuteDeportWorkQueues();
278 clang::Decl *ClangASTImporter::DeportDecl(clang::ASTContext *dst_ctx,
279 clang::ASTContext *src_ctx,
281 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
284 log->Printf(" [ClangASTImporter] DeportDecl called on (%sDecl*)%p from "
285 "(ASTContext*)%p to (ASTContext*)%p",
286 decl->getDeclKindName(), static_cast<void *>(decl),
287 static_cast<void *>(src_ctx), static_cast<void *>(dst_ctx));
289 MinionSP minion_sp(GetMinion(dst_ctx, src_ctx));
294 std::set<NamedDecl *> decls_to_deport;
295 std::set<NamedDecl *> decls_already_deported;
297 DeclContextOverride decl_context_override;
299 decl_context_override.OverrideAllDeclsFromContainingFunction(decl);
301 minion_sp->InitDeportWorkQueues(&decls_to_deport, &decls_already_deported);
303 clang::Decl *result = CopyDecl(dst_ctx, src_ctx, decl);
305 minion_sp->ExecuteDeportWorkQueues();
312 " [ClangASTImporter] DeportDecl deported (%sDecl*)%p to (%sDecl*)%p",
313 decl->getDeclKindName(), static_cast<void *>(decl),
314 result->getDeclKindName(), static_cast<void *>(result));
319 bool ClangASTImporter::CanImport(const CompilerType &type) {
320 if (!ClangUtil::IsClangType(type))
323 // TODO: remove external completion BOOL
324 // CompleteAndFetchChildren should get the Decl out and check for the
326 clang::QualType qual_type(
327 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
329 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
330 switch (type_class) {
331 case clang::Type::Record: {
332 const clang::CXXRecordDecl *cxx_record_decl =
333 qual_type->getAsCXXRecordDecl();
334 if (cxx_record_decl) {
335 if (ResolveDeclOrigin(cxx_record_decl, NULL, NULL))
340 case clang::Type::Enum: {
341 clang::EnumDecl *enum_decl =
342 llvm::cast<clang::EnumType>(qual_type)->getDecl();
344 if (ResolveDeclOrigin(enum_decl, NULL, NULL))
349 case clang::Type::ObjCObject:
350 case clang::Type::ObjCInterface: {
351 const clang::ObjCObjectType *objc_class_type =
352 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
353 if (objc_class_type) {
354 clang::ObjCInterfaceDecl *class_interface_decl =
355 objc_class_type->getInterface();
356 // We currently can't complete objective C types through the newly added
357 // ASTContext because it only supports TagDecl objects right now...
358 if (class_interface_decl) {
359 if (ResolveDeclOrigin(class_interface_decl, NULL, NULL))
365 case clang::Type::Typedef:
366 return CanImport(CompilerType(type.GetTypeSystem(),
367 llvm::cast<clang::TypedefType>(qual_type)
369 ->getUnderlyingType()
372 case clang::Type::Auto:
373 return CanImport(CompilerType(type.GetTypeSystem(),
374 llvm::cast<clang::AutoType>(qual_type)
378 case clang::Type::Elaborated:
379 return CanImport(CompilerType(type.GetTypeSystem(),
380 llvm::cast<clang::ElaboratedType>(qual_type)
384 case clang::Type::Paren:
385 return CanImport(CompilerType(
386 type.GetTypeSystem(),
387 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
396 bool ClangASTImporter::Import(const CompilerType &type) {
397 if (!ClangUtil::IsClangType(type))
399 // TODO: remove external completion BOOL
400 // CompleteAndFetchChildren should get the Decl out and check for the
402 clang::QualType qual_type(
403 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
405 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
406 switch (type_class) {
407 case clang::Type::Record: {
408 const clang::CXXRecordDecl *cxx_record_decl =
409 qual_type->getAsCXXRecordDecl();
410 if (cxx_record_decl) {
411 if (ResolveDeclOrigin(cxx_record_decl, NULL, NULL))
412 return CompleteAndFetchChildren(qual_type);
416 case clang::Type::Enum: {
417 clang::EnumDecl *enum_decl =
418 llvm::cast<clang::EnumType>(qual_type)->getDecl();
420 if (ResolveDeclOrigin(enum_decl, NULL, NULL))
421 return CompleteAndFetchChildren(qual_type);
425 case clang::Type::ObjCObject:
426 case clang::Type::ObjCInterface: {
427 const clang::ObjCObjectType *objc_class_type =
428 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
429 if (objc_class_type) {
430 clang::ObjCInterfaceDecl *class_interface_decl =
431 objc_class_type->getInterface();
432 // We currently can't complete objective C types through the newly added
433 // ASTContext because it only supports TagDecl objects right now...
434 if (class_interface_decl) {
435 if (ResolveDeclOrigin(class_interface_decl, NULL, NULL))
436 return CompleteAndFetchChildren(qual_type);
441 case clang::Type::Typedef:
442 return Import(CompilerType(type.GetTypeSystem(),
443 llvm::cast<clang::TypedefType>(qual_type)
445 ->getUnderlyingType()
448 case clang::Type::Auto:
449 return Import(CompilerType(type.GetTypeSystem(),
450 llvm::cast<clang::AutoType>(qual_type)
454 case clang::Type::Elaborated:
455 return Import(CompilerType(type.GetTypeSystem(),
456 llvm::cast<clang::ElaboratedType>(qual_type)
460 case clang::Type::Paren:
461 return Import(CompilerType(
462 type.GetTypeSystem(),
463 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
471 bool ClangASTImporter::CompleteType(const CompilerType &compiler_type) {
472 if (!CanImport(compiler_type))
475 if (Import(compiler_type)) {
476 ClangASTContext::CompleteTagDeclarationDefinition(compiler_type);
480 ClangASTContext::SetHasExternalStorage(compiler_type.GetOpaqueQualType(),
485 bool ClangASTImporter::LayoutRecordType(
486 const clang::RecordDecl *record_decl, uint64_t &bit_size,
488 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
489 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
491 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
493 RecordDeclToLayoutMap::iterator pos =
494 m_record_decl_to_layout_map.find(record_decl);
495 bool success = false;
496 base_offsets.clear();
497 vbase_offsets.clear();
498 if (pos != m_record_decl_to_layout_map.end()) {
499 bit_size = pos->second.bit_size;
500 alignment = pos->second.alignment;
501 field_offsets.swap(pos->second.field_offsets);
502 base_offsets.swap(pos->second.base_offsets);
503 vbase_offsets.swap(pos->second.vbase_offsets);
504 m_record_decl_to_layout_map.erase(pos);
509 field_offsets.clear();
514 void ClangASTImporter::InsertRecordDecl(clang::RecordDecl *decl,
515 const LayoutInfo &layout) {
516 m_record_decl_to_layout_map.insert(std::make_pair(decl, layout));
519 void ClangASTImporter::CompleteDecl(clang::Decl *decl) {
520 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
523 log->Printf(" [ClangASTImporter] CompleteDecl called on (%sDecl*)%p",
524 decl->getDeclKindName(), static_cast<void *>(decl));
526 if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl)) {
527 if (!interface_decl->getDefinition()) {
528 interface_decl->startDefinition();
529 CompleteObjCInterfaceDecl(interface_decl);
531 } else if (ObjCProtocolDecl *protocol_decl =
532 dyn_cast<ObjCProtocolDecl>(decl)) {
533 if (!protocol_decl->getDefinition())
534 protocol_decl->startDefinition();
535 } else if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl)) {
536 if (!tag_decl->getDefinition() && !tag_decl->isBeingDefined()) {
537 tag_decl->startDefinition();
538 CompleteTagDecl(tag_decl);
539 tag_decl->setCompleteDefinition(true);
542 assert(0 && "CompleteDecl called on a Decl that can't be completed");
546 bool ClangASTImporter::CompleteTagDecl(clang::TagDecl *decl) {
547 ClangASTMetrics::RegisterDeclCompletion();
549 DeclOrigin decl_origin = GetDeclOrigin(decl);
551 if (!decl_origin.Valid())
554 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
557 MinionSP minion_sp(GetMinion(&decl->getASTContext(), decl_origin.ctx));
560 minion_sp->ImportDefinitionTo(decl, decl_origin.decl);
565 bool ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl,
566 clang::TagDecl *origin_decl) {
567 ClangASTMetrics::RegisterDeclCompletion();
569 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
571 if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
574 MinionSP minion_sp(GetMinion(&decl->getASTContext(), origin_ast_ctx));
577 minion_sp->ImportDefinitionTo(decl, origin_decl);
579 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
581 OriginMap &origins = context_md->m_origins;
583 origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
588 bool ClangASTImporter::CompleteObjCInterfaceDecl(
589 clang::ObjCInterfaceDecl *interface_decl) {
590 ClangASTMetrics::RegisterDeclCompletion();
592 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
594 if (!decl_origin.Valid())
597 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
601 GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
604 minion_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
606 if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass())
607 RequireCompleteType(clang::QualType(super_class->getTypeForDecl(), 0));
612 bool ClangASTImporter::CompleteAndFetchChildren(clang::QualType type) {
613 if (!RequireCompleteType(type))
616 if (const TagType *tag_type = type->getAs<TagType>()) {
617 TagDecl *tag_decl = tag_type->getDecl();
619 DeclOrigin decl_origin = GetDeclOrigin(tag_decl);
621 if (!decl_origin.Valid())
624 MinionSP minion_sp(GetMinion(&tag_decl->getASTContext(), decl_origin.ctx));
626 TagDecl *origin_tag_decl = llvm::dyn_cast<TagDecl>(decl_origin.decl);
628 for (Decl *origin_child_decl : origin_tag_decl->decls()) {
629 minion_sp->Import(origin_child_decl);
632 if (RecordDecl *record_decl = dyn_cast<RecordDecl>(origin_tag_decl)) {
633 record_decl->setHasLoadedFieldsFromExternalStorage(true);
639 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
640 if (ObjCInterfaceDecl *objc_interface_decl =
641 objc_object_type->getInterface()) {
642 DeclOrigin decl_origin = GetDeclOrigin(objc_interface_decl);
644 if (!decl_origin.Valid())
648 GetMinion(&objc_interface_decl->getASTContext(), decl_origin.ctx));
650 ObjCInterfaceDecl *origin_interface_decl =
651 llvm::dyn_cast<ObjCInterfaceDecl>(decl_origin.decl);
653 for (Decl *origin_child_decl : origin_interface_decl->decls()) {
654 minion_sp->Import(origin_child_decl);
666 bool ClangASTImporter::RequireCompleteType(clang::QualType type) {
670 if (const TagType *tag_type = type->getAs<TagType>()) {
671 TagDecl *tag_decl = tag_type->getDecl();
673 if (tag_decl->getDefinition() || tag_decl->isBeingDefined())
676 return CompleteTagDecl(tag_decl);
678 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
679 if (ObjCInterfaceDecl *objc_interface_decl =
680 objc_object_type->getInterface())
681 return CompleteObjCInterfaceDecl(objc_interface_decl);
685 if (const ArrayType *array_type = type->getAsArrayTypeUnsafe()) {
686 return RequireCompleteType(array_type->getElementType());
688 if (const AtomicType *atomic_type = type->getAs<AtomicType>()) {
689 return RequireCompleteType(atomic_type->getPointeeType());
695 ClangASTMetadata *ClangASTImporter::GetDeclMetadata(const clang::Decl *decl) {
696 DeclOrigin decl_origin = GetDeclOrigin(decl);
698 if (decl_origin.Valid())
699 return ClangASTContext::GetMetadata(decl_origin.ctx, decl_origin.decl);
701 return ClangASTContext::GetMetadata(&decl->getASTContext(), decl);
704 ClangASTImporter::DeclOrigin
705 ClangASTImporter::GetDeclOrigin(const clang::Decl *decl) {
706 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
708 OriginMap &origins = context_md->m_origins;
710 OriginMap::iterator iter = origins.find(decl);
712 if (iter != origins.end())
718 void ClangASTImporter::SetDeclOrigin(const clang::Decl *decl,
719 clang::Decl *original_decl) {
720 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
722 OriginMap &origins = context_md->m_origins;
724 OriginMap::iterator iter = origins.find(decl);
726 if (iter != origins.end()) {
727 iter->second.decl = original_decl;
728 iter->second.ctx = &original_decl->getASTContext();
730 origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
734 void ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
735 NamespaceMapSP &namespace_map) {
736 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
738 context_md->m_namespace_maps[decl] = namespace_map;
741 ClangASTImporter::NamespaceMapSP
742 ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl) {
743 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
745 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
747 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
749 if (iter != namespace_maps.end())
752 return NamespaceMapSP();
755 void ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl) {
757 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
759 const DeclContext *parent_context = decl->getDeclContext();
760 const NamespaceDecl *parent_namespace =
761 dyn_cast<NamespaceDecl>(parent_context);
762 NamespaceMapSP parent_map;
764 if (parent_namespace)
765 parent_map = GetNamespaceMap(parent_namespace);
767 NamespaceMapSP new_map;
769 new_map.reset(new NamespaceMap);
771 if (context_md->m_map_completer) {
772 std::string namespace_string = decl->getDeclName().getAsString();
774 context_md->m_map_completer->CompleteNamespaceMap(
775 new_map, ConstString(namespace_string.c_str()), parent_map);
778 context_md->m_namespace_maps[decl] = new_map;
781 void ClangASTImporter::ForgetDestination(clang::ASTContext *dst_ast) {
782 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
785 log->Printf(" [ClangASTImporter] Forgetting destination (ASTContext*)%p",
786 static_cast<void *>(dst_ast));
788 m_metadata_map.erase(dst_ast);
791 void ClangASTImporter::ForgetSource(clang::ASTContext *dst_ast,
792 clang::ASTContext *src_ast) {
793 ASTContextMetadataSP md = MaybeGetContextMetadata(dst_ast);
795 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
798 log->Printf(" [ClangASTImporter] Forgetting source->dest "
799 "(ASTContext*)%p->(ASTContext*)%p",
800 static_cast<void *>(src_ast), static_cast<void *>(dst_ast));
805 md->m_minions.erase(src_ast);
807 for (OriginMap::iterator iter = md->m_origins.begin();
808 iter != md->m_origins.end();) {
809 if (iter->second.ctx == src_ast)
810 md->m_origins.erase(iter++);
816 ClangASTImporter::MapCompleter::~MapCompleter() { return; }
818 void ClangASTImporter::Minion::InitDeportWorkQueues(
819 std::set<clang::NamedDecl *> *decls_to_deport,
820 std::set<clang::NamedDecl *> *decls_already_deported) {
821 assert(!m_decls_to_deport);
822 assert(!m_decls_already_deported);
824 m_decls_to_deport = decls_to_deport;
825 m_decls_already_deported = decls_already_deported;
828 void ClangASTImporter::Minion::ExecuteDeportWorkQueues() {
829 assert(m_decls_to_deport);
830 assert(m_decls_already_deported);
832 ASTContextMetadataSP to_context_md =
833 m_master.GetContextMetadata(&getToContext());
835 while (!m_decls_to_deport->empty()) {
836 NamedDecl *decl = *m_decls_to_deport->begin();
838 m_decls_already_deported->insert(decl);
839 m_decls_to_deport->erase(decl);
841 DeclOrigin &origin = to_context_md->m_origins[decl];
842 UNUSED_IF_ASSERT_DISABLED(origin);
845 m_source_ctx); // otherwise we should never have added this
846 // because it doesn't need to be deported
848 Decl *original_decl = to_context_md->m_origins[decl].decl;
850 ClangASTContext::GetCompleteDecl(m_source_ctx, original_decl);
852 if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl)) {
853 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl)) {
854 if (original_tag_decl->isCompleteDefinition()) {
855 ImportDefinitionTo(tag_decl, original_tag_decl);
856 tag_decl->setCompleteDefinition(true);
860 tag_decl->setHasExternalLexicalStorage(false);
861 tag_decl->setHasExternalVisibleStorage(false);
862 } else if (ObjCContainerDecl *container_decl =
863 dyn_cast<ObjCContainerDecl>(decl)) {
864 container_decl->setHasExternalLexicalStorage(false);
865 container_decl->setHasExternalVisibleStorage(false);
868 to_context_md->m_origins.erase(decl);
871 m_decls_to_deport = nullptr;
872 m_decls_already_deported = nullptr;
875 void ClangASTImporter::Minion::ImportDefinitionTo(clang::Decl *to,
877 ASTImporter::Imported(from, to);
880 if (to_objc_interface)
881 to_objc_interface->startDefinition();
883 CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
886 to_cxx_record->startDefinition();
889 ImportDefinition(from);
891 if (clang::TagDecl *to_tag = dyn_cast<clang::TagDecl>(to)) {
892 if (clang::TagDecl *from_tag = dyn_cast<clang::TagDecl>(from)) {
893 to_tag->setCompleteDefinition(from_tag->isCompleteDefinition());
897 // If we're dealing with an Objective-C class, ensure that the inheritance
898 // has been set up correctly. The ASTImporter may not do this correctly if
899 // the class was originally sourced from symbols.
901 if (ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to)) {
903 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
906 break; // we're not going to override it if it's set
908 ObjCInterfaceDecl *from_objc_interface =
909 dyn_cast<ObjCInterfaceDecl>(from);
911 if (!from_objc_interface)
914 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
916 if (!from_superclass)
919 Decl *imported_from_superclass_decl = Import(from_superclass);
921 if (!imported_from_superclass_decl)
924 ObjCInterfaceDecl *imported_from_superclass =
925 dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl);
927 if (!imported_from_superclass)
930 if (!to_objc_interface->hasDefinition())
931 to_objc_interface->startDefinition();
933 to_objc_interface->setSuperClass(m_source_ctx->getTrivialTypeSourceInfo(
934 m_source_ctx->getObjCInterfaceType(imported_from_superclass)));
939 clang::Decl *ClangASTImporter::Minion::Imported(clang::Decl *from,
941 ClangASTMetrics::RegisterClangImport();
943 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
945 lldb::user_id_t user_id = LLDB_INVALID_UID;
946 ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
948 user_id = metadata->GetUserID();
951 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
952 std::string name_string;
953 llvm::raw_string_ostream name_stream(name_string);
954 from_named_decl->printName(name_stream);
957 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from "
958 "(Decl*)%p), metadata 0x%" PRIx64,
959 from->getDeclKindName(), static_cast<void *>(to),
960 name_string.c_str(), static_cast<void *>(from), user_id);
962 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from "
963 "(Decl*)%p), metadata 0x%" PRIx64,
964 from->getDeclKindName(), static_cast<void *>(to),
965 static_cast<void *>(from), user_id);
969 ASTContextMetadataSP to_context_md =
970 m_master.GetContextMetadata(&to->getASTContext());
971 ASTContextMetadataSP from_context_md =
972 m_master.MaybeGetContextMetadata(m_source_ctx);
974 if (from_context_md) {
975 OriginMap &origins = from_context_md->m_origins;
977 OriginMap::iterator origin_iter = origins.find(from);
979 if (origin_iter != origins.end()) {
980 if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
981 user_id != LLDB_INVALID_UID) {
982 if (origin_iter->second.ctx != &to->getASTContext())
983 to_context_md->m_origins[to] = origin_iter->second;
986 MinionSP direct_completer =
987 m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx);
989 if (direct_completer.get() != this)
990 direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
993 log->Printf(" [ClangASTImporter] Propagated origin "
994 "(Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to "
996 static_cast<void *>(origin_iter->second.decl),
997 static_cast<void *>(origin_iter->second.ctx),
998 static_cast<void *>(&from->getASTContext()),
999 static_cast<void *>(&to->getASTContext()));
1001 if (m_decls_to_deport && m_decls_already_deported) {
1002 if (isa<TagDecl>(to) || isa<ObjCInterfaceDecl>(to)) {
1003 RecordDecl *from_record_decl = dyn_cast<RecordDecl>(from);
1004 if (from_record_decl == nullptr ||
1005 from_record_decl->isInjectedClassName() == false) {
1006 NamedDecl *to_named_decl = dyn_cast<NamedDecl>(to);
1008 if (!m_decls_already_deported->count(to_named_decl))
1009 m_decls_to_deport->insert(to_named_decl);
1014 if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
1015 user_id != LLDB_INVALID_UID) {
1016 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
1020 log->Printf(" [ClangASTImporter] Decl has no origin information in "
1022 static_cast<void *>(&from->getASTContext()));
1025 if (clang::NamespaceDecl *to_namespace =
1026 dyn_cast<clang::NamespaceDecl>(to)) {
1027 clang::NamespaceDecl *from_namespace =
1028 dyn_cast<clang::NamespaceDecl>(from);
1030 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
1032 NamespaceMetaMap::iterator namespace_map_iter =
1033 namespace_maps.find(from_namespace);
1035 if (namespace_map_iter != namespace_maps.end())
1036 to_context_md->m_namespace_maps[to_namespace] =
1037 namespace_map_iter->second;
1040 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
1043 log->Printf(" [ClangASTImporter] Sourced origin "
1044 "(Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
1045 static_cast<void *>(from), static_cast<void *>(m_source_ctx),
1046 static_cast<void *>(&to->getASTContext()));
1049 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from)) {
1050 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
1052 to_tag_decl->setHasExternalLexicalStorage();
1053 to_tag_decl->setMustBuildLookupTable();
1057 " [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
1058 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1059 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
1060 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
1061 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
1064 if (isa<NamespaceDecl>(from)) {
1065 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
1067 m_master.BuildNamespaceMap(to_namespace_decl);
1069 to_namespace_decl->setHasExternalVisibleStorage();
1072 if (isa<ObjCContainerDecl>(from)) {
1073 ObjCContainerDecl *to_container_decl = dyn_cast<ObjCContainerDecl>(to);
1075 to_container_decl->setHasExternalLexicalStorage();
1076 to_container_decl->setHasExternalVisibleStorage();
1078 /*to_interface_decl->setExternallyCompleted();*/
1081 if (ObjCInterfaceDecl *to_interface_decl =
1082 llvm::dyn_cast<ObjCInterfaceDecl>(to_container_decl)) {
1084 " [ClangASTImporter] To is an ObjCInterfaceDecl - attributes "
1086 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1087 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
1088 (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
1091 " [ClangASTImporter] To is an %sDecl - attributes %s%s",
1092 ((Decl *)to_container_decl)->getDeclKindName(),
1093 (to_container_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1094 (to_container_decl->hasExternalVisibleStorage() ? " Visible" : ""));
1099 return clang::ASTImporter::Imported(from, to);
1102 clang::Decl *ClangASTImporter::Minion::GetOriginalDecl(clang::Decl *To) {
1103 ASTContextMetadataSP to_context_md =
1104 m_master.GetContextMetadata(&To->getASTContext());
1109 OriginMap::iterator iter = to_context_md->m_origins.find(To);
1111 if (iter == to_context_md->m_origins.end())
1114 return const_cast<clang::Decl *>(iter->second.decl);