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/Log.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Symbol/ClangASTContext.h"
14 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
15 #include "lldb/Symbol/ClangUtil.h"
16 #include "lldb/Utility/LLDBAssert.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
358 // because it only supports TagDecl objects right now...
359 if (class_interface_decl) {
360 if (ResolveDeclOrigin(class_interface_decl, NULL, NULL))
366 case clang::Type::Typedef:
367 return CanImport(CompilerType(type.GetTypeSystem(),
368 llvm::cast<clang::TypedefType>(qual_type)
370 ->getUnderlyingType()
373 case clang::Type::Auto:
374 return CanImport(CompilerType(type.GetTypeSystem(),
375 llvm::cast<clang::AutoType>(qual_type)
379 case clang::Type::Elaborated:
380 return CanImport(CompilerType(type.GetTypeSystem(),
381 llvm::cast<clang::ElaboratedType>(qual_type)
385 case clang::Type::Paren:
386 return CanImport(CompilerType(
387 type.GetTypeSystem(),
388 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
397 bool ClangASTImporter::Import(const CompilerType &type) {
398 if (!ClangUtil::IsClangType(type))
400 // TODO: remove external completion BOOL
401 // CompleteAndFetchChildren should get the Decl out and check for the
403 clang::QualType qual_type(
404 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
406 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
407 switch (type_class) {
408 case clang::Type::Record: {
409 const clang::CXXRecordDecl *cxx_record_decl =
410 qual_type->getAsCXXRecordDecl();
411 if (cxx_record_decl) {
412 if (ResolveDeclOrigin(cxx_record_decl, NULL, NULL))
413 return CompleteAndFetchChildren(qual_type);
417 case clang::Type::Enum: {
418 clang::EnumDecl *enum_decl =
419 llvm::cast<clang::EnumType>(qual_type)->getDecl();
421 if (ResolveDeclOrigin(enum_decl, NULL, NULL))
422 return CompleteAndFetchChildren(qual_type);
426 case clang::Type::ObjCObject:
427 case clang::Type::ObjCInterface: {
428 const clang::ObjCObjectType *objc_class_type =
429 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
430 if (objc_class_type) {
431 clang::ObjCInterfaceDecl *class_interface_decl =
432 objc_class_type->getInterface();
433 // We currently can't complete objective C types through the newly added
435 // because it only supports TagDecl objects right now...
436 if (class_interface_decl) {
437 if (ResolveDeclOrigin(class_interface_decl, NULL, NULL))
438 return CompleteAndFetchChildren(qual_type);
443 case clang::Type::Typedef:
444 return Import(CompilerType(type.GetTypeSystem(),
445 llvm::cast<clang::TypedefType>(qual_type)
447 ->getUnderlyingType()
450 case clang::Type::Auto:
451 return Import(CompilerType(type.GetTypeSystem(),
452 llvm::cast<clang::AutoType>(qual_type)
456 case clang::Type::Elaborated:
457 return Import(CompilerType(type.GetTypeSystem(),
458 llvm::cast<clang::ElaboratedType>(qual_type)
462 case clang::Type::Paren:
463 return Import(CompilerType(
464 type.GetTypeSystem(),
465 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
473 bool ClangASTImporter::CompleteType(const CompilerType &compiler_type) {
474 if (!CanImport(compiler_type))
477 if (Import(compiler_type)) {
478 ClangASTContext::CompleteTagDeclarationDefinition(compiler_type);
482 ClangASTContext::SetHasExternalStorage(compiler_type.GetOpaqueQualType(),
487 bool ClangASTImporter::LayoutRecordType(
488 const clang::RecordDecl *record_decl, uint64_t &bit_size,
490 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
491 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
493 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
495 RecordDeclToLayoutMap::iterator pos =
496 m_record_decl_to_layout_map.find(record_decl);
497 bool success = false;
498 base_offsets.clear();
499 vbase_offsets.clear();
500 if (pos != m_record_decl_to_layout_map.end()) {
501 bit_size = pos->second.bit_size;
502 alignment = pos->second.alignment;
503 field_offsets.swap(pos->second.field_offsets);
504 base_offsets.swap(pos->second.base_offsets);
505 vbase_offsets.swap(pos->second.vbase_offsets);
506 m_record_decl_to_layout_map.erase(pos);
511 field_offsets.clear();
516 void ClangASTImporter::InsertRecordDecl(clang::RecordDecl *decl,
517 const LayoutInfo &layout) {
518 m_record_decl_to_layout_map.insert(std::make_pair(decl, layout));
521 void ClangASTImporter::CompleteDecl(clang::Decl *decl) {
522 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
525 log->Printf(" [ClangASTImporter] CompleteDecl called on (%sDecl*)%p",
526 decl->getDeclKindName(), static_cast<void *>(decl));
528 if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl)) {
529 if (!interface_decl->getDefinition()) {
530 interface_decl->startDefinition();
531 CompleteObjCInterfaceDecl(interface_decl);
533 } else if (ObjCProtocolDecl *protocol_decl =
534 dyn_cast<ObjCProtocolDecl>(decl)) {
535 if (!protocol_decl->getDefinition())
536 protocol_decl->startDefinition();
537 } else if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl)) {
538 if (!tag_decl->getDefinition() && !tag_decl->isBeingDefined()) {
539 tag_decl->startDefinition();
540 CompleteTagDecl(tag_decl);
541 tag_decl->setCompleteDefinition(true);
544 assert(0 && "CompleteDecl called on a Decl that can't be completed");
548 bool ClangASTImporter::CompleteTagDecl(clang::TagDecl *decl) {
549 ClangASTMetrics::RegisterDeclCompletion();
551 DeclOrigin decl_origin = GetDeclOrigin(decl);
553 if (!decl_origin.Valid())
556 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
559 MinionSP minion_sp(GetMinion(&decl->getASTContext(), decl_origin.ctx));
562 minion_sp->ImportDefinitionTo(decl, decl_origin.decl);
567 bool ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl,
568 clang::TagDecl *origin_decl) {
569 ClangASTMetrics::RegisterDeclCompletion();
571 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
573 if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
576 MinionSP minion_sp(GetMinion(&decl->getASTContext(), origin_ast_ctx));
579 minion_sp->ImportDefinitionTo(decl, origin_decl);
581 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
583 OriginMap &origins = context_md->m_origins;
585 origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
590 bool ClangASTImporter::CompleteObjCInterfaceDecl(
591 clang::ObjCInterfaceDecl *interface_decl) {
592 ClangASTMetrics::RegisterDeclCompletion();
594 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
596 if (!decl_origin.Valid())
599 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
603 GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
606 minion_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
608 if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass())
609 RequireCompleteType(clang::QualType(super_class->getTypeForDecl(), 0));
614 bool ClangASTImporter::CompleteAndFetchChildren(clang::QualType type) {
615 if (!RequireCompleteType(type))
618 if (const TagType *tag_type = type->getAs<TagType>()) {
619 TagDecl *tag_decl = tag_type->getDecl();
621 DeclOrigin decl_origin = GetDeclOrigin(tag_decl);
623 if (!decl_origin.Valid())
626 MinionSP minion_sp(GetMinion(&tag_decl->getASTContext(), decl_origin.ctx));
628 TagDecl *origin_tag_decl = llvm::dyn_cast<TagDecl>(decl_origin.decl);
630 for (Decl *origin_child_decl : origin_tag_decl->decls()) {
631 minion_sp->Import(origin_child_decl);
634 if (RecordDecl *record_decl = dyn_cast<RecordDecl>(origin_tag_decl)) {
635 record_decl->setHasLoadedFieldsFromExternalStorage(true);
641 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
642 if (ObjCInterfaceDecl *objc_interface_decl =
643 objc_object_type->getInterface()) {
644 DeclOrigin decl_origin = GetDeclOrigin(objc_interface_decl);
646 if (!decl_origin.Valid())
650 GetMinion(&objc_interface_decl->getASTContext(), decl_origin.ctx));
652 ObjCInterfaceDecl *origin_interface_decl =
653 llvm::dyn_cast<ObjCInterfaceDecl>(decl_origin.decl);
655 for (Decl *origin_child_decl : origin_interface_decl->decls()) {
656 minion_sp->Import(origin_child_decl);
668 bool ClangASTImporter::RequireCompleteType(clang::QualType type) {
672 if (const TagType *tag_type = type->getAs<TagType>()) {
673 TagDecl *tag_decl = tag_type->getDecl();
675 if (tag_decl->getDefinition() || tag_decl->isBeingDefined())
678 return CompleteTagDecl(tag_decl);
680 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
681 if (ObjCInterfaceDecl *objc_interface_decl =
682 objc_object_type->getInterface())
683 return CompleteObjCInterfaceDecl(objc_interface_decl);
687 if (const ArrayType *array_type = type->getAsArrayTypeUnsafe()) {
688 return RequireCompleteType(array_type->getElementType());
690 if (const AtomicType *atomic_type = type->getAs<AtomicType>()) {
691 return RequireCompleteType(atomic_type->getPointeeType());
697 ClangASTMetadata *ClangASTImporter::GetDeclMetadata(const clang::Decl *decl) {
698 DeclOrigin decl_origin = GetDeclOrigin(decl);
700 if (decl_origin.Valid())
701 return ClangASTContext::GetMetadata(decl_origin.ctx, decl_origin.decl);
703 return ClangASTContext::GetMetadata(&decl->getASTContext(), decl);
706 ClangASTImporter::DeclOrigin
707 ClangASTImporter::GetDeclOrigin(const clang::Decl *decl) {
708 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
710 OriginMap &origins = context_md->m_origins;
712 OriginMap::iterator iter = origins.find(decl);
714 if (iter != origins.end())
720 void ClangASTImporter::SetDeclOrigin(const clang::Decl *decl,
721 clang::Decl *original_decl) {
722 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
724 OriginMap &origins = context_md->m_origins;
726 OriginMap::iterator iter = origins.find(decl);
728 if (iter != origins.end()) {
729 iter->second.decl = original_decl;
730 iter->second.ctx = &original_decl->getASTContext();
732 origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
736 void ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
737 NamespaceMapSP &namespace_map) {
738 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
740 context_md->m_namespace_maps[decl] = namespace_map;
743 ClangASTImporter::NamespaceMapSP
744 ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl) {
745 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
747 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
749 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
751 if (iter != namespace_maps.end())
754 return NamespaceMapSP();
757 void ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl) {
759 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
761 const DeclContext *parent_context = decl->getDeclContext();
762 const NamespaceDecl *parent_namespace =
763 dyn_cast<NamespaceDecl>(parent_context);
764 NamespaceMapSP parent_map;
766 if (parent_namespace)
767 parent_map = GetNamespaceMap(parent_namespace);
769 NamespaceMapSP new_map;
771 new_map.reset(new NamespaceMap);
773 if (context_md->m_map_completer) {
774 std::string namespace_string = decl->getDeclName().getAsString();
776 context_md->m_map_completer->CompleteNamespaceMap(
777 new_map, ConstString(namespace_string.c_str()), parent_map);
780 context_md->m_namespace_maps[decl] = new_map;
783 void ClangASTImporter::ForgetDestination(clang::ASTContext *dst_ast) {
784 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
787 log->Printf(" [ClangASTImporter] Forgetting destination (ASTContext*)%p",
788 static_cast<void *>(dst_ast));
790 m_metadata_map.erase(dst_ast);
793 void ClangASTImporter::ForgetSource(clang::ASTContext *dst_ast,
794 clang::ASTContext *src_ast) {
795 ASTContextMetadataSP md = MaybeGetContextMetadata(dst_ast);
797 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
800 log->Printf(" [ClangASTImporter] Forgetting source->dest "
801 "(ASTContext*)%p->(ASTContext*)%p",
802 static_cast<void *>(src_ast), static_cast<void *>(dst_ast));
807 md->m_minions.erase(src_ast);
809 for (OriginMap::iterator iter = md->m_origins.begin();
810 iter != md->m_origins.end();) {
811 if (iter->second.ctx == src_ast)
812 md->m_origins.erase(iter++);
818 ClangASTImporter::MapCompleter::~MapCompleter() { return; }
820 void ClangASTImporter::Minion::InitDeportWorkQueues(
821 std::set<clang::NamedDecl *> *decls_to_deport,
822 std::set<clang::NamedDecl *> *decls_already_deported) {
823 assert(!m_decls_to_deport);
824 assert(!m_decls_already_deported);
826 m_decls_to_deport = decls_to_deport;
827 m_decls_already_deported = decls_already_deported;
830 void ClangASTImporter::Minion::ExecuteDeportWorkQueues() {
831 assert(m_decls_to_deport);
832 assert(m_decls_already_deported);
834 ASTContextMetadataSP to_context_md =
835 m_master.GetContextMetadata(&getToContext());
837 while (!m_decls_to_deport->empty()) {
838 NamedDecl *decl = *m_decls_to_deport->begin();
840 m_decls_already_deported->insert(decl);
841 m_decls_to_deport->erase(decl);
843 DeclOrigin &origin = to_context_md->m_origins[decl];
844 UNUSED_IF_ASSERT_DISABLED(origin);
847 m_source_ctx); // otherwise we should never have added this
848 // because it doesn't need to be deported
850 Decl *original_decl = to_context_md->m_origins[decl].decl;
852 ClangASTContext::GetCompleteDecl(m_source_ctx, original_decl);
854 if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl)) {
855 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl)) {
856 if (original_tag_decl->isCompleteDefinition()) {
857 ImportDefinitionTo(tag_decl, original_tag_decl);
858 tag_decl->setCompleteDefinition(true);
862 tag_decl->setHasExternalLexicalStorage(false);
863 tag_decl->setHasExternalVisibleStorage(false);
864 } else if (ObjCContainerDecl *container_decl =
865 dyn_cast<ObjCContainerDecl>(decl)) {
866 container_decl->setHasExternalLexicalStorage(false);
867 container_decl->setHasExternalVisibleStorage(false);
870 to_context_md->m_origins.erase(decl);
873 m_decls_to_deport = nullptr;
874 m_decls_already_deported = nullptr;
877 void ClangASTImporter::Minion::ImportDefinitionTo(clang::Decl *to,
879 ASTImporter::Imported(from, to);
882 if (to_objc_interface)
883 to_objc_interface->startDefinition();
885 CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
888 to_cxx_record->startDefinition();
891 ImportDefinition(from);
893 if (clang::TagDecl *to_tag = dyn_cast<clang::TagDecl>(to)) {
894 if (clang::TagDecl *from_tag = dyn_cast<clang::TagDecl>(from)) {
895 to_tag->setCompleteDefinition(from_tag->isCompleteDefinition());
899 // If we're dealing with an Objective-C class, ensure that the inheritance has
900 // been set up correctly. The ASTImporter may not do this correctly if the
901 // class was originally sourced from symbols.
903 if (ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to)) {
905 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
908 break; // we're not going to override it if it's set
910 ObjCInterfaceDecl *from_objc_interface =
911 dyn_cast<ObjCInterfaceDecl>(from);
913 if (!from_objc_interface)
916 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
918 if (!from_superclass)
921 Decl *imported_from_superclass_decl = Import(from_superclass);
923 if (!imported_from_superclass_decl)
926 ObjCInterfaceDecl *imported_from_superclass =
927 dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl);
929 if (!imported_from_superclass)
932 if (!to_objc_interface->hasDefinition())
933 to_objc_interface->startDefinition();
935 to_objc_interface->setSuperClass(m_source_ctx->getTrivialTypeSourceInfo(
936 m_source_ctx->getObjCInterfaceType(imported_from_superclass)));
941 clang::Decl *ClangASTImporter::Minion::Imported(clang::Decl *from,
943 ClangASTMetrics::RegisterClangImport();
945 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
947 lldb::user_id_t user_id = LLDB_INVALID_UID;
948 ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
950 user_id = metadata->GetUserID();
953 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
954 std::string name_string;
955 llvm::raw_string_ostream name_stream(name_string);
956 from_named_decl->printName(name_stream);
959 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from "
960 "(Decl*)%p), metadata 0x%" PRIx64,
961 from->getDeclKindName(), static_cast<void *>(to),
962 name_string.c_str(), static_cast<void *>(from), user_id);
964 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from "
965 "(Decl*)%p), metadata 0x%" PRIx64,
966 from->getDeclKindName(), static_cast<void *>(to),
967 static_cast<void *>(from), user_id);
971 ASTContextMetadataSP to_context_md =
972 m_master.GetContextMetadata(&to->getASTContext());
973 ASTContextMetadataSP from_context_md =
974 m_master.MaybeGetContextMetadata(m_source_ctx);
976 if (from_context_md) {
977 OriginMap &origins = from_context_md->m_origins;
979 OriginMap::iterator origin_iter = origins.find(from);
981 if (origin_iter != origins.end()) {
982 if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
983 user_id != LLDB_INVALID_UID) {
984 if (origin_iter->second.ctx != &to->getASTContext())
985 to_context_md->m_origins[to] = origin_iter->second;
988 MinionSP direct_completer =
989 m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx);
991 if (direct_completer.get() != this)
992 direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
995 log->Printf(" [ClangASTImporter] Propagated origin "
996 "(Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to "
998 static_cast<void *>(origin_iter->second.decl),
999 static_cast<void *>(origin_iter->second.ctx),
1000 static_cast<void *>(&from->getASTContext()),
1001 static_cast<void *>(&to->getASTContext()));
1003 if (m_decls_to_deport && m_decls_already_deported) {
1004 if (isa<TagDecl>(to) || isa<ObjCInterfaceDecl>(to)) {
1005 RecordDecl *from_record_decl = dyn_cast<RecordDecl>(from);
1006 if (from_record_decl == nullptr ||
1007 from_record_decl->isInjectedClassName() == false) {
1008 NamedDecl *to_named_decl = dyn_cast<NamedDecl>(to);
1010 if (!m_decls_already_deported->count(to_named_decl))
1011 m_decls_to_deport->insert(to_named_decl);
1016 if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
1017 user_id != LLDB_INVALID_UID) {
1018 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
1022 log->Printf(" [ClangASTImporter] Decl has no origin information in "
1024 static_cast<void *>(&from->getASTContext()));
1027 if (clang::NamespaceDecl *to_namespace =
1028 dyn_cast<clang::NamespaceDecl>(to)) {
1029 clang::NamespaceDecl *from_namespace =
1030 dyn_cast<clang::NamespaceDecl>(from);
1032 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
1034 NamespaceMetaMap::iterator namespace_map_iter =
1035 namespace_maps.find(from_namespace);
1037 if (namespace_map_iter != namespace_maps.end())
1038 to_context_md->m_namespace_maps[to_namespace] =
1039 namespace_map_iter->second;
1042 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
1045 log->Printf(" [ClangASTImporter] Sourced origin "
1046 "(Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
1047 static_cast<void *>(from), static_cast<void *>(m_source_ctx),
1048 static_cast<void *>(&to->getASTContext()));
1051 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from)) {
1052 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
1054 to_tag_decl->setHasExternalLexicalStorage();
1055 to_tag_decl->setMustBuildLookupTable();
1059 " [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
1060 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1061 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
1062 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
1063 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
1066 if (isa<NamespaceDecl>(from)) {
1067 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
1069 m_master.BuildNamespaceMap(to_namespace_decl);
1071 to_namespace_decl->setHasExternalVisibleStorage();
1074 if (isa<ObjCContainerDecl>(from)) {
1075 ObjCContainerDecl *to_container_decl = dyn_cast<ObjCContainerDecl>(to);
1077 to_container_decl->setHasExternalLexicalStorage();
1078 to_container_decl->setHasExternalVisibleStorage();
1080 /*to_interface_decl->setExternallyCompleted();*/
1083 if (ObjCInterfaceDecl *to_interface_decl =
1084 llvm::dyn_cast<ObjCInterfaceDecl>(to_container_decl)) {
1086 " [ClangASTImporter] To is an ObjCInterfaceDecl - attributes "
1088 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1089 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
1090 (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
1093 " [ClangASTImporter] To is an %sDecl - attributes %s%s",
1094 ((Decl *)to_container_decl)->getDeclKindName(),
1095 (to_container_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1096 (to_container_decl->hasExternalVisibleStorage() ? " Visible" : ""));
1101 return clang::ASTImporter::Imported(from, to);
1104 clang::Decl *ClangASTImporter::Minion::GetOriginalDecl(clang::Decl *To) {
1105 ASTContextMetadataSP to_context_md =
1106 m_master.GetContextMetadata(&To->getASTContext());
1111 OriginMap::iterator iter = to_context_md->m_origins.find(To);
1113 if (iter == to_context_md->m_origins.end())
1116 return const_cast<clang::Decl *>(iter->second.decl);