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 "clang/AST/Decl.h"
11 #include "clang/AST/DeclCXX.h"
12 #include "clang/AST/DeclObjC.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include "lldb/Core/Log.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Symbol/ClangASTContext.h"
17 #include "lldb/Symbol/ClangASTImporter.h"
18 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
19 #include "lldb/Symbol/ClangNamespaceDecl.h"
21 using namespace lldb_private;
22 using namespace clang;
24 ClangASTMetrics::Counters ClangASTMetrics::global_counters = { 0, 0, 0, 0, 0, 0 };
25 ClangASTMetrics::Counters ClangASTMetrics::local_counters = { 0, 0, 0, 0, 0, 0 };
27 void ClangASTMetrics::DumpCounters (Log *log, ClangASTMetrics::Counters &counters)
29 log->Printf(" Number of visible Decl queries by name : %" PRIu64, counters.m_visible_query_count);
30 log->Printf(" Number of lexical Decl queries : %" PRIu64, counters.m_lexical_query_count);
31 log->Printf(" Number of imports initiated by LLDB : %" PRIu64, counters.m_lldb_import_count);
32 log->Printf(" Number of imports conducted by Clang : %" PRIu64, counters.m_clang_import_count);
33 log->Printf(" Number of Decls completed : %" PRIu64, counters.m_decls_completed_count);
34 log->Printf(" Number of records laid out : %" PRIu64, counters.m_record_layout_count);
37 void ClangASTMetrics::DumpCounters (Log *log)
42 log->Printf("== ClangASTMetrics output ==");
43 log->Printf("-- Global metrics --");
44 DumpCounters (log, global_counters);
45 log->Printf("-- Local metrics --");
46 DumpCounters (log, local_counters);
50 ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
51 clang::ASTContext *src_ast,
54 MinionSP minion_sp (GetMinion(dst_ast, src_ast));
57 return minion_sp->Import(type);
63 ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
64 clang::ASTContext *src_ast,
65 lldb::clang_type_t type)
67 return CopyType (dst_ast, src_ast, QualType::getFromOpaquePtr(type)).getAsOpaquePtr();
71 ClangASTImporter::CopyDecl (clang::ASTContext *dst_ast,
72 clang::ASTContext *src_ast,
77 minion_sp = GetMinion(dst_ast, src_ast);
81 clang::Decl *result = minion_sp->Import(decl);
85 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
89 lldb::user_id_t user_id = LLDB_INVALID_UID;
90 ClangASTMetadata *metadata = GetDeclMetadata(decl);
92 user_id = metadata->GetUserID();
94 if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
95 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s '%s', metadata 0x%" PRIx64,
96 decl->getDeclKindName(),
97 named_decl->getNameAsString().c_str(),
100 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s, metadata 0x%" PRIx64,
101 decl->getDeclKindName(),
113 ClangASTImporter::DeportType (clang::ASTContext *dst_ctx,
114 clang::ASTContext *src_ctx,
115 lldb::clang_type_t type)
117 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
122 std::set<NamedDecl *> decls_to_deport;
123 std::set<NamedDecl *> decls_already_deported;
125 minion_sp->InitDeportWorkQueues(&decls_to_deport,
126 &decls_already_deported);
128 lldb::clang_type_t result = CopyType(dst_ctx, src_ctx, type);
130 minion_sp->ExecuteDeportWorkQueues();
140 ClangASTImporter::DeportDecl (clang::ASTContext *dst_ctx,
141 clang::ASTContext *src_ctx,
144 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
147 log->Printf(" [ClangASTImporter] DeportDecl called on (%sDecl*)%p from (ASTContext*)%p to (ASTContex*)%p",
148 decl->getDeclKindName(), static_cast<void*>(decl),
149 static_cast<void*>(src_ctx),
150 static_cast<void*>(dst_ctx));
152 MinionSP minion_sp (GetMinion (dst_ctx, src_ctx));
157 std::set<NamedDecl *> decls_to_deport;
158 std::set<NamedDecl *> decls_already_deported;
160 minion_sp->InitDeportWorkQueues(&decls_to_deport,
161 &decls_already_deported);
163 clang::Decl *result = CopyDecl(dst_ctx, src_ctx, decl);
165 minion_sp->ExecuteDeportWorkQueues();
171 log->Printf(" [ClangASTImporter] DeportDecl deported (%sDecl*)%p to (%sDecl*)%p",
172 decl->getDeclKindName(), static_cast<void*>(decl),
173 result->getDeclKindName(), static_cast<void*>(result));
179 ClangASTImporter::CompleteDecl (clang::Decl *decl)
181 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
184 log->Printf(" [ClangASTImporter] CompleteDecl called on (%sDecl*)%p",
185 decl->getDeclKindName(), static_cast<void*>(decl));
187 if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl))
189 if (!interface_decl->getDefinition())
191 interface_decl->startDefinition();
192 CompleteObjCInterfaceDecl(interface_decl);
195 else if (ObjCProtocolDecl *protocol_decl = dyn_cast<ObjCProtocolDecl>(decl))
197 if (!protocol_decl->getDefinition())
198 protocol_decl->startDefinition();
200 else if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl))
202 if (!tag_decl->getDefinition() && !tag_decl->isBeingDefined())
204 tag_decl->startDefinition();
205 CompleteTagDecl(tag_decl);
206 tag_decl->setCompleteDefinition(true);
211 assert (0 && "CompleteDecl called on a Decl that can't be completed");
216 ClangASTImporter::CompleteTagDecl (clang::TagDecl *decl)
218 ClangASTMetrics::RegisterDeclCompletion();
220 DeclOrigin decl_origin = GetDeclOrigin(decl);
222 if (!decl_origin.Valid())
225 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
228 MinionSP minion_sp (GetMinion(&decl->getASTContext(), decl_origin.ctx));
231 minion_sp->ImportDefinitionTo(decl, decl_origin.decl);
237 ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin_decl)
239 ClangASTMetrics::RegisterDeclCompletion();
241 clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
243 if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
246 MinionSP minion_sp (GetMinion(&decl->getASTContext(), origin_ast_ctx));
249 minion_sp->ImportDefinitionTo(decl, origin_decl);
251 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
253 OriginMap &origins = context_md->m_origins;
255 origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
261 ClangASTImporter::CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl)
263 ClangASTMetrics::RegisterDeclCompletion();
265 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
267 if (!decl_origin.Valid())
270 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
273 MinionSP minion_sp (GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
276 minion_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
282 ClangASTImporter::RequireCompleteType (clang::QualType type)
287 if (const TagType *tag_type = type->getAs<TagType>())
289 return CompleteTagDecl(tag_type->getDecl());
291 if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>())
293 if (ObjCInterfaceDecl *objc_interface_decl = objc_object_type->getInterface())
294 return CompleteObjCInterfaceDecl(objc_interface_decl);
298 if (const ArrayType *array_type = type->getAsArrayTypeUnsafe())
300 return RequireCompleteType(array_type->getElementType());
302 if (const AtomicType *atomic_type = type->getAs<AtomicType>())
304 return RequireCompleteType(atomic_type->getPointeeType());
311 ClangASTImporter::GetDeclMetadata (const clang::Decl *decl)
313 DeclOrigin decl_origin = GetDeclOrigin(decl);
315 if (decl_origin.Valid())
316 return ClangASTContext::GetMetadata(decl_origin.ctx, decl_origin.decl);
318 return ClangASTContext::GetMetadata(&decl->getASTContext(), decl);
321 ClangASTImporter::DeclOrigin
322 ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
324 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
326 OriginMap &origins = context_md->m_origins;
328 OriginMap::iterator iter = origins.find(decl);
330 if (iter != origins.end())
337 ClangASTImporter::SetDeclOrigin (const clang::Decl *decl, clang::Decl *original_decl)
339 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
341 OriginMap &origins = context_md->m_origins;
343 OriginMap::iterator iter = origins.find(decl);
345 if (iter != origins.end())
347 iter->second.decl = original_decl;
348 iter->second.ctx = &original_decl->getASTContext();
352 origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
357 ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
358 NamespaceMapSP &namespace_map)
360 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
362 context_md->m_namespace_maps[decl] = namespace_map;
365 ClangASTImporter::NamespaceMapSP
366 ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
368 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
370 NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
372 NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
374 if (iter != namespace_maps.end())
377 return NamespaceMapSP();
381 ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
384 ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
386 const DeclContext *parent_context = decl->getDeclContext();
387 const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
388 NamespaceMapSP parent_map;
390 if (parent_namespace)
391 parent_map = GetNamespaceMap(parent_namespace);
393 NamespaceMapSP new_map;
395 new_map.reset(new NamespaceMap);
397 if (context_md->m_map_completer)
399 std::string namespace_string = decl->getDeclName().getAsString();
401 context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
404 context_md->m_namespace_maps[decl] = new_map;
408 ClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast)
410 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
413 log->Printf(" [ClangASTImporter] Forgetting destination (ASTContext*)%p",
414 static_cast<void*>(dst_ast));
416 m_metadata_map.erase(dst_ast);
420 ClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast)
422 ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast);
424 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
427 log->Printf(" [ClangASTImporter] Forgetting source->dest (ASTContext*)%p->(ASTContext*)%p",
428 static_cast<void*>(src_ast), static_cast<void*>(dst_ast));
433 md->m_minions.erase(src_ast);
435 for (OriginMap::iterator iter = md->m_origins.begin();
436 iter != md->m_origins.end();
439 if (iter->second.ctx == src_ast)
440 md->m_origins.erase(iter++);
446 ClangASTImporter::MapCompleter::~MapCompleter ()
452 ClangASTImporter::Minion::InitDeportWorkQueues (std::set<clang::NamedDecl *> *decls_to_deport,
453 std::set<clang::NamedDecl *> *decls_already_deported)
455 assert(!m_decls_to_deport); // TODO make debug only
456 assert(!m_decls_already_deported);
458 m_decls_to_deport = decls_to_deport;
459 m_decls_already_deported = decls_already_deported;
463 ClangASTImporter::Minion::ExecuteDeportWorkQueues ()
465 assert(m_decls_to_deport); // TODO make debug only
466 assert(m_decls_already_deported);
468 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&getToContext());
470 while (!m_decls_to_deport->empty())
472 NamedDecl *decl = *m_decls_to_deport->begin();
474 m_decls_already_deported->insert(decl);
475 m_decls_to_deport->erase(decl);
477 DeclOrigin &origin = to_context_md->m_origins[decl];
479 assert (origin.ctx == m_source_ctx); // otherwise we should never have added this
480 // because it doesn't need to be deported
482 Decl *original_decl = to_context_md->m_origins[decl].decl;
484 ClangASTContext::GetCompleteDecl (m_source_ctx, original_decl);
486 if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl))
488 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
489 if (original_tag_decl->isCompleteDefinition())
490 ImportDefinitionTo(tag_decl, original_tag_decl);
492 tag_decl->setHasExternalLexicalStorage(false);
493 tag_decl->setHasExternalVisibleStorage(false);
495 else if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl))
497 interface_decl->setHasExternalLexicalStorage(false);
498 interface_decl->setHasExternalVisibleStorage(false);
501 to_context_md->m_origins.erase(decl);
504 m_decls_to_deport = nullptr;
505 m_decls_already_deported = nullptr;
509 ClangASTImporter::Minion::ImportDefinitionTo (clang::Decl *to, clang::Decl *from)
511 ASTImporter::Imported(from, to);
513 ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to);
516 if (to_objc_interface)
517 to_objc_interface->startDefinition();
519 CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
522 to_cxx_record->startDefinition();
525 ImportDefinition(from);
527 // If we're dealing with an Objective-C class, ensure that the inheritance has
528 // been set up correctly. The ASTImporter may not do this correctly if the
529 // class was originally sourced from symbols.
531 if (to_objc_interface)
535 ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
538 break; // we're not going to override it if it's set
540 ObjCInterfaceDecl *from_objc_interface = dyn_cast<ObjCInterfaceDecl>(from);
542 if (!from_objc_interface)
545 ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
547 if (!from_superclass)
550 Decl *imported_from_superclass_decl = Import(from_superclass);
552 if (!imported_from_superclass_decl)
555 ObjCInterfaceDecl *imported_from_superclass = dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl);
557 if (!imported_from_superclass)
560 if (!to_objc_interface->hasDefinition())
561 to_objc_interface->startDefinition();
563 to_objc_interface->setSuperClass(imported_from_superclass);
570 ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
572 ClangASTMetrics::RegisterClangImport();
574 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
576 lldb::user_id_t user_id = LLDB_INVALID_UID;
577 ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
579 user_id = metadata->GetUserID();
583 if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
585 std::string name_string;
586 llvm::raw_string_ostream name_stream(name_string);
587 from_named_decl->printName(name_stream);
590 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p), metadata 0x%" PRIx64,
591 from->getDeclKindName(), static_cast<void*>(to),
592 name_string.c_str(), static_cast<void*>(from),
597 log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p), metadata 0x%" PRIx64,
598 from->getDeclKindName(), static_cast<void*>(to),
599 static_cast<void*>(from), user_id);
603 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
604 ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
608 OriginMap &origins = from_context_md->m_origins;
610 OriginMap::iterator origin_iter = origins.find(from);
612 if (origin_iter != origins.end())
614 if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
615 user_id != LLDB_INVALID_UID)
617 to_context_md->m_origins[to] = origin_iter->second;
620 MinionSP direct_completer = m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx);
622 if (direct_completer.get() != this)
623 direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
626 log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
627 static_cast<void*>(origin_iter->second.decl),
628 static_cast<void*>(origin_iter->second.ctx),
629 static_cast<void*>(&from->getASTContext()),
630 static_cast<void*>(&to->getASTContext()));
634 if (m_decls_to_deport && m_decls_already_deported)
636 if (isa<TagDecl>(to) || isa<ObjCInterfaceDecl>(to))
638 NamedDecl *to_named_decl = dyn_cast<NamedDecl>(to);
640 if (!m_decls_already_deported->count(to_named_decl))
641 m_decls_to_deport->insert(to_named_decl);
645 if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
646 user_id != LLDB_INVALID_UID)
648 to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
652 log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
653 static_cast<void*>(&from->getASTContext()));
656 if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
658 clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
660 NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
662 NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
664 if (namespace_map_iter != namespace_maps.end())
665 to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
670 to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
673 log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
674 static_cast<void*>(from),
675 static_cast<void*>(m_source_ctx),
676 static_cast<void*>(&to->getASTContext()));
679 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
681 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
683 to_tag_decl->setHasExternalLexicalStorage();
684 to_tag_decl->setMustBuildLookupTable();
687 log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
688 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
689 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
690 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
691 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
694 if (isa<NamespaceDecl>(from))
696 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
698 m_master.BuildNamespaceMap(to_namespace_decl);
700 to_namespace_decl->setHasExternalVisibleStorage();
703 if (isa<ObjCInterfaceDecl>(from))
705 ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
707 to_interface_decl->setHasExternalLexicalStorage();
708 to_interface_decl->setHasExternalVisibleStorage();
710 /*to_interface_decl->setExternallyCompleted();*/
713 log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
714 (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
715 (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
716 (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
719 return clang::ASTImporter::Imported(from, to);
722 clang::Decl *ClangASTImporter::Minion::GetOriginalDecl (clang::Decl *To)
724 ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&To->getASTContext());
729 OriginMap::iterator iter = to_context_md->m_origins.find(To);
731 if (iter == to_context_md->m_origins.end())
734 return const_cast<clang::Decl*>(iter->second.decl);