1 //===-- ClangASTSource.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 "ClangASTSource.h"
12 #include "ASTDumper.h"
13 #include "ClangModulesDeclVendor.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/RecordLayout.h"
17 #include "lldb/Core/Log.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/ModuleList.h"
20 #include "lldb/Symbol/ClangASTContext.h"
21 #include "lldb/Symbol/CompilerDeclContext.h"
22 #include "lldb/Symbol/Function.h"
23 #include "lldb/Symbol/SymbolVendor.h"
24 #include "lldb/Symbol/TaggedASTType.h"
25 #include "lldb/Target/ObjCLanguageRuntime.h"
26 #include "lldb/Target/Target.h"
30 using namespace clang;
31 using namespace lldb_private;
33 //------------------------------------------------------------------
34 // Scoped class that will remove an active lexical decl from the set
35 // when it goes out of scope.
36 //------------------------------------------------------------------
38 class ScopedLexicalDeclEraser
41 ScopedLexicalDeclEraser(std::set<const clang::Decl *> &decls,
42 const clang::Decl *decl)
43 : m_active_lexical_decls(decls), m_decl(decl)
47 ~ScopedLexicalDeclEraser()
49 m_active_lexical_decls.erase(m_decl);
53 std::set<const clang::Decl *> &m_active_lexical_decls;
54 const clang::Decl *m_decl;
58 ClangASTSource::~ClangASTSource()
60 m_ast_importer_sp->ForgetDestination(m_ast_context);
62 // We are in the process of destruction, don't create clang ast context on demand
63 // by passing false to Target::GetScratchClangASTContext(create_on_demand).
64 ClangASTContext *scratch_clang_ast_context = m_target->GetScratchClangASTContext(false);
66 if (!scratch_clang_ast_context)
69 clang::ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
71 if (!scratch_ast_context)
74 if (m_ast_context != scratch_ast_context)
75 m_ast_importer_sp->ForgetSource(scratch_ast_context, m_ast_context);
79 ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer)
84 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
85 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
88 // The core lookup interface.
90 ClangASTSource::FindExternalVisibleDeclsByName
92 const DeclContext *decl_ctx,
93 DeclarationName clang_decl_name
98 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
102 if (GetImportInProgress())
104 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
108 std::string decl_name (clang_decl_name.getAsString());
110 // if (m_decl_map.DoingASTImport ())
111 // return DeclContext::lookup_result();
113 switch (clang_decl_name.getNameKind()) {
114 // Normal identifiers.
115 case DeclarationName::Identifier:
117 clang::IdentifierInfo *identifier_info = clang_decl_name.getAsIdentifierInfo();
119 if (!identifier_info ||
120 identifier_info->getBuiltinID() != 0)
122 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
129 case DeclarationName::CXXOperatorName:
130 case DeclarationName::CXXLiteralOperatorName:
133 // Using directives found in this context.
134 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
135 case DeclarationName::CXXUsingDirective:
136 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
139 case DeclarationName::ObjCZeroArgSelector:
140 case DeclarationName::ObjCOneArgSelector:
141 case DeclarationName::ObjCMultiArgSelector:
143 llvm::SmallVector<NamedDecl*, 1> method_decls;
145 NameSearchContext method_search_context (*this, method_decls, clang_decl_name, decl_ctx);
147 FindObjCMethodDecls(method_search_context);
149 SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, method_decls);
150 return (method_decls.size() > 0);
152 // These aren't possible in the global context.
153 case DeclarationName::CXXConstructorName:
154 case DeclarationName::CXXDestructorName:
155 case DeclarationName::CXXConversionFunctionName:
156 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
161 if (!GetLookupsEnabled())
163 // Wait until we see a '$' at the start of a name before we start doing
164 // any lookups so we can avoid lookup up all of the builtin types.
165 if (!decl_name.empty() && decl_name[0] == '$')
167 SetLookupsEnabled (true);
171 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
176 ConstString const_decl_name(decl_name.c_str());
178 const char *uniqued_const_decl_name = const_decl_name.GetCString();
179 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
181 // We are currently looking up this name...
182 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
185 m_active_lookups.insert(uniqued_const_decl_name);
186 // static uint32_t g_depth = 0;
188 // printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
189 llvm::SmallVector<NamedDecl*, 4> name_decls;
190 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
191 FindExternalVisibleDecls(name_search_context);
192 SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls);
194 m_active_lookups.erase (uniqued_const_decl_name);
195 return (name_decls.size() != 0);
199 ClangASTSource::CompleteType (TagDecl *tag_decl)
201 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
203 static unsigned int invocation_id = 0;
204 unsigned int current_id = invocation_id++;
208 log->Printf(" CompleteTagDecl[%u] on (ASTContext*)%p Completing (TagDecl*)%p named %s",
209 current_id, static_cast<void*>(m_ast_context),
210 static_cast<void*>(tag_decl),
211 tag_decl->getName().str().c_str());
213 log->Printf(" CTD[%u] Before:", current_id);
214 ASTDumper dumper((Decl*)tag_decl);
215 dumper.ToLog(log, " [CTD] ");
218 auto iter = m_active_lexical_decls.find(tag_decl);
219 if (iter != m_active_lexical_decls.end())
221 m_active_lexical_decls.insert(tag_decl);
222 ScopedLexicalDeclEraser eraser(m_active_lexical_decls, tag_decl);
224 if (!m_ast_importer_sp->CompleteTagDecl (tag_decl))
226 // We couldn't complete the type. Maybe there's a definition
227 // somewhere else that can be completed.
230 log->Printf(" CTD[%u] Type could not be completed in the module in which it was first found.", current_id);
234 DeclContext *decl_ctx = tag_decl->getDeclContext();
236 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(decl_ctx))
238 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer_sp->GetNamespaceMap(namespace_context);
240 if (log && log->GetVerbose())
241 log->Printf(" CTD[%u] Inspecting namespace map %p (%d entries)",
242 current_id, static_cast<void*>(namespace_map.get()),
243 static_cast<int>(namespace_map->size()));
248 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
253 log->Printf(" CTD[%u] Searching namespace %s in module %s",
255 i->second.GetName().AsCString(),
256 i->first->GetFileSpec().GetFilename().GetCString());
260 SymbolContext null_sc;
261 ConstString name(tag_decl->getName().str().c_str());
263 i->first->FindTypesInNamespace(null_sc, name, &i->second, UINT32_MAX, types);
265 for (uint32_t ti = 0, te = types.GetSize();
269 lldb::TypeSP type = types.GetTypeAtIndex(ti);
274 CompilerType clang_type (type->GetFullCompilerType ());
279 const TagType *tag_type = ClangASTContext::GetQualType(clang_type)->getAs<TagType>();
284 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
286 if (m_ast_importer_sp->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
295 SymbolContext null_sc;
296 ConstString name(tag_decl->getName().str().c_str());
297 CompilerDeclContext namespace_decl;
299 const ModuleList &module_list = m_target->GetImages();
301 bool exact_match = false;
302 module_list.FindTypes (null_sc, name, exact_match, UINT32_MAX, types);
304 for (uint32_t ti = 0, te = types.GetSize();
308 lldb::TypeSP type = types.GetTypeAtIndex(ti);
313 CompilerType clang_type (type->GetFullCompilerType ());
318 const TagType *tag_type = ClangASTContext::GetQualType(clang_type)->getAs<TagType>();
323 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
325 // We have found a type by basename and we need to make sure the decl contexts
326 // are the same before we can try to complete this type with another
327 if (!ClangASTContext::DeclsAreEquivalent (tag_decl, candidate_tag_decl))
330 if (m_ast_importer_sp->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
338 log->Printf(" [CTD] After:");
339 ASTDumper dumper((Decl*)tag_decl);
340 dumper.ToLog(log, " [CTD] ");
345 ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl)
347 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
351 log->Printf(" [CompleteObjCInterfaceDecl] on (ASTContext*)%p Completing an ObjCInterfaceDecl named %s",
352 static_cast<void*>(m_ast_context),
353 interface_decl->getName().str().c_str());
354 log->Printf(" [COID] Before:");
355 ASTDumper dumper((Decl*)interface_decl);
356 dumper.ToLog(log, " [COID] ");
359 Decl *original_decl = NULL;
360 ASTContext *original_ctx = NULL;
362 if (m_ast_importer_sp->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx))
364 if (ObjCInterfaceDecl *original_iface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl))
366 ObjCInterfaceDecl *complete_iface_decl = GetCompleteObjCInterface(original_iface_decl);
368 if (complete_iface_decl && (complete_iface_decl != original_iface_decl))
370 m_ast_importer_sp->SetDeclOrigin(interface_decl, original_iface_decl);
375 m_ast_importer_sp->CompleteObjCInterfaceDecl (interface_decl);
377 if (interface_decl->getSuperClass() &&
378 interface_decl->getSuperClass() != interface_decl)
379 CompleteType(interface_decl->getSuperClass());
383 log->Printf(" [COID] After:");
384 ASTDumper dumper((Decl*)interface_decl);
385 dumper.ToLog(log, " [COID] ");
389 clang::ObjCInterfaceDecl *
390 ClangASTSource::GetCompleteObjCInterface (clang::ObjCInterfaceDecl *interface_decl)
392 lldb::ProcessSP process(m_target->GetProcessSP());
397 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
399 if (!language_runtime)
402 ConstString class_name(interface_decl->getNameAsString().c_str());
404 lldb::TypeSP complete_type_sp(language_runtime->LookupInCompleteClassCache(class_name));
406 if (!complete_type_sp)
409 TypeFromUser complete_type = TypeFromUser(complete_type_sp->GetFullCompilerType ());
410 lldb::opaque_compiler_type_t complete_opaque_type = complete_type.GetOpaqueQualType();
412 if (!complete_opaque_type)
415 const clang::Type *complete_clang_type = QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
416 const ObjCInterfaceType *complete_interface_type = dyn_cast<ObjCInterfaceType>(complete_clang_type);
418 if (!complete_interface_type)
421 ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl());
423 return complete_iface_decl;
427 ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
428 llvm::function_ref<bool(Decl::Kind)> predicate,
429 llvm::SmallVectorImpl<Decl*> &decls)
431 ClangASTMetrics::RegisterLexicalQuery();
433 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
435 const Decl *context_decl = dyn_cast<Decl>(decl_context);
440 auto iter = m_active_lexical_decls.find(context_decl);
441 if (iter != m_active_lexical_decls.end())
443 m_active_lexical_decls.insert(context_decl);
444 ScopedLexicalDeclEraser eraser(m_active_lexical_decls, context_decl);
446 static unsigned int invocation_id = 0;
447 unsigned int current_id = invocation_id++;
451 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
452 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p",
453 current_id, static_cast<void*>(m_ast_context),
454 context_named_decl->getNameAsString().c_str(),
455 context_decl->getDeclKindName(),
456 static_cast<const void*>(context_decl));
457 else if(context_decl)
458 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p",
459 current_id, static_cast<void*>(m_ast_context),
460 context_decl->getDeclKindName(),
461 static_cast<const void*>(context_decl));
463 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context",
464 current_id, static_cast<const void*>(m_ast_context));
467 Decl *original_decl = NULL;
468 ASTContext *original_ctx = NULL;
470 if (!m_ast_importer_sp->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
475 log->Printf(" FELD[%u] Original decl (ASTContext*)%p (Decl*)%p:",
476 current_id, static_cast<void*>(original_ctx),
477 static_cast<void*>(original_decl));
478 ASTDumper(original_decl).ToLog(log, " ");
481 if (ObjCInterfaceDecl *original_iface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl))
483 ObjCInterfaceDecl *complete_iface_decl = GetCompleteObjCInterface(original_iface_decl);
485 if (complete_iface_decl && (complete_iface_decl != original_iface_decl))
487 original_decl = complete_iface_decl;
488 original_ctx = &complete_iface_decl->getASTContext();
490 m_ast_importer_sp->SetDeclOrigin(context_decl, original_iface_decl);
494 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
496 ExternalASTSource *external_source = original_ctx->getExternalSource();
499 external_source->CompleteType (original_tag_decl);
502 const DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
504 if (!original_decl_context)
507 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
508 iter != original_decl_context->decls_end();
513 if (predicate(decl->getKind()))
517 ASTDumper ast_dumper(decl);
518 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
519 log->Printf(" FELD[%d] Adding [to %sDecl %s] lexical %sDecl %s", current_id, context_named_decl->getDeclKindName(), context_named_decl->getNameAsString().c_str(), decl->getDeclKindName(), ast_dumper.GetCString());
521 log->Printf(" FELD[%d] Adding lexical %sDecl %s", current_id, decl->getDeclKindName(), ast_dumper.GetCString());
524 Decl *copied_decl = m_ast_importer_sp->CopyDecl(m_ast_context, original_ctx, decl);
529 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl))
531 QualType copied_field_type = copied_field->getType();
533 m_ast_importer_sp->RequireCompleteType(copied_field_type);
536 DeclContext *decl_context_non_const = const_cast<DeclContext *>(decl_context);
538 if (copied_decl->getDeclContext() != decl_context)
540 if (copied_decl->getDeclContext()->containsDecl(copied_decl))
541 copied_decl->getDeclContext()->removeDecl(copied_decl);
542 copied_decl->setDeclContext(decl_context_non_const);
545 if (!decl_context_non_const->containsDecl(copied_decl))
546 decl_context_non_const->addDeclInternal(copied_decl);
554 ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
556 assert (m_ast_context);
558 ClangASTMetrics::RegisterVisibleQuery();
560 const ConstString name(context.m_decl_name.getAsString().c_str());
562 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
564 static unsigned int invocation_id = 0;
565 unsigned int current_id = invocation_id++;
569 if (!context.m_decl_context)
570 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a NULL DeclContext",
571 current_id, static_cast<void*>(m_ast_context),
573 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
574 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in '%s'",
575 current_id, static_cast<void*>(m_ast_context),
577 context_named_decl->getNameAsString().c_str());
579 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a '%s'",
580 current_id, static_cast<void*>(m_ast_context),
582 context.m_decl_context->getDeclKindName());
585 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
587 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
589 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer_sp->GetNamespaceMap(namespace_context);
591 if (log && log->GetVerbose())
592 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
593 current_id, static_cast<void*>(namespace_map.get()),
594 static_cast<int>(namespace_map->size()));
599 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
604 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
606 i->second.GetName().AsCString(),
607 i->first->GetFileSpec().GetFilename().GetCString());
609 FindExternalVisibleDecls(context,
615 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
617 FindObjCPropertyAndIvarDecls(context);
619 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
621 // we shouldn't be getting FindExternalVisibleDecls calls for these
626 CompilerDeclContext namespace_decl;
629 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
631 FindExternalVisibleDecls(context,
637 if (!context.m_namespace_map->empty())
639 if (log && log->GetVerbose())
640 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
642 static_cast<void*>(context.m_namespace_map.get()),
643 static_cast<int>(context.m_namespace_map->size()));
645 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
647 if (clang_namespace_decl)
648 clang_namespace_decl->setHasExternalVisibleStorage();
653 ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
654 lldb::ModuleSP module_sp,
655 CompilerDeclContext &namespace_decl,
656 unsigned int current_id)
658 assert (m_ast_context);
660 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
662 SymbolContextList sc_list;
664 const ConstString name(context.m_decl_name.getAsString().c_str());
666 const char *name_unique_cstr = name.GetCString();
668 static ConstString id_name("id");
669 static ConstString Class_name("Class");
671 if (name == id_name || name == Class_name)
674 if (name_unique_cstr == NULL)
677 // The ClangASTSource is not responsible for finding $-names.
678 if (name_unique_cstr[0] == '$')
681 if (module_sp && namespace_decl)
683 CompilerDeclContext found_namespace_decl;
685 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
689 SymbolContext null_sc;
691 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
693 if (found_namespace_decl)
695 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>(module_sp, found_namespace_decl));
698 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
701 module_sp->GetFileSpec().GetFilename().GetCString());
707 const ModuleList &target_images = m_target->GetImages();
708 Mutex::Locker modules_locker (target_images.GetMutex());
710 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
712 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
717 CompilerDeclContext found_namespace_decl;
719 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
724 SymbolContext null_sc;
726 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
728 if (found_namespace_decl)
730 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>(image, found_namespace_decl));
733 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
736 image->GetFileSpec().GetFilename().GetCString());
744 SymbolContext null_sc;
745 const bool exact_match = false;
747 if (module_sp && namespace_decl)
748 module_sp->FindTypesInNamespace(null_sc, name, &namespace_decl, 1, types);
750 m_target->GetImages().FindTypes(null_sc, name, exact_match, 1, types);
752 bool found_a_type = false;
754 if (size_t num_types = types.GetSize())
756 for (size_t ti = 0; ti < num_types; ++ti)
758 lldb::TypeSP type_sp = types.GetTypeAtIndex(ti);
762 const char *name_string = type_sp->GetName().GetCString();
764 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
767 (name_string ? name_string : "<anonymous>"));
770 CompilerType full_type = type_sp->GetFullCompilerType();
772 CompilerType copied_clang_type (GuardedCopyType(full_type));
774 if (!copied_clang_type)
777 log->Printf(" CAS::FEVD[%u] - Couldn't export a type",
783 context.AddTypeDecl(copied_clang_type);
792 // Try the modules next.
796 if (ClangModulesDeclVendor *modules_decl_vendor = m_target->GetClangModulesDeclVendor())
799 uint32_t max_matches = 1;
800 std::vector <clang::NamedDecl *> decls;
802 if (!modules_decl_vendor->FindDecls(name,
810 log->Printf(" CAS::FEVD[%u] Matching entity found for \"%s\" in the modules",
815 clang::NamedDecl *const decl_from_modules = decls[0];
817 if (llvm::isa<clang::TypeDecl>(decl_from_modules) ||
818 llvm::isa<clang::ObjCContainerDecl>(decl_from_modules) ||
819 llvm::isa<clang::EnumConstantDecl>(decl_from_modules))
821 clang::Decl *copied_decl = m_ast_importer_sp->CopyDecl(m_ast_context, &decl_from_modules->getASTContext(), decl_from_modules);
822 clang::NamedDecl *copied_named_decl = copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr;
824 if (!copied_named_decl)
827 log->Printf(" CAS::FEVD[%u] - Couldn't export a type from the modules",
833 context.AddNamedDecl(copied_named_decl);
845 // Couldn't find any types elsewhere. Try the Objective-C runtime if one exists.
847 lldb::ProcessSP process(m_target->GetProcessSP());
852 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
854 if (!language_runtime)
857 DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
863 uint32_t max_matches = 1;
864 std::vector <clang::NamedDecl *> decls;
866 if (!decl_vendor->FindDecls(name,
874 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\" in the runtime",
879 clang::Decl *copied_decl = m_ast_importer_sp->CopyDecl(m_ast_context, &decls[0]->getASTContext(), decls[0]);
880 clang::NamedDecl *copied_named_decl = copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr;
882 if (!copied_named_decl)
885 log->Printf(" CAS::FEVD[%u] - Couldn't export a type from the runtime",
891 context.AddNamedDecl(copied_named_decl);
899 template <class D> class TaggedASTDecl {
901 TaggedASTDecl() : decl(NULL) { }
902 TaggedASTDecl(D *_decl) : decl(_decl) { }
903 bool IsValid() const { return (decl != NULL); }
904 bool IsInvalid() const { return !IsValid(); }
905 D *operator->() const { return decl; }
909 template <class D2, template <class D> class TD, class D1>
911 DynCast(TD<D1> source)
913 return TD<D2> (dyn_cast<D2>(source.decl));
916 template <class D = Decl> class DeclFromParser;
917 template <class D = Decl> class DeclFromUser;
919 template <class D> class DeclFromParser : public TaggedASTDecl<D> {
921 DeclFromParser() : TaggedASTDecl<D>() { }
922 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { }
924 DeclFromUser<D> GetOrigin(ClangASTImporter *importer);
927 template <class D> class DeclFromUser : public TaggedASTDecl<D> {
929 DeclFromUser() : TaggedASTDecl<D>() { }
930 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { }
932 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx);
937 DeclFromParser<D>::GetOrigin(ClangASTImporter *importer)
939 DeclFromUser <> origin_decl;
940 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
941 if (origin_decl.IsInvalid())
942 return DeclFromUser<D>();
943 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
948 DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx)
950 DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl));
951 if (parser_generic_decl.IsInvalid())
952 return DeclFromParser<D>();
953 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
957 FindObjCMethodDeclsWithOrigin (unsigned int current_id,
958 NameSearchContext &context,
959 ObjCInterfaceDecl *original_interface_decl,
960 clang::ASTContext *ast_context,
961 ClangASTImporter *ast_importer,
962 const char *log_info)
964 const DeclarationName &decl_name(context.m_decl_name);
965 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();
967 Selector original_selector;
969 if (decl_name.isObjCZeroArgSelector())
971 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString());
972 original_selector = original_ctx->Selectors.getSelector(0, &ident);
974 else if (decl_name.isObjCOneArgSelector())
976 const std::string &decl_name_string = decl_name.getAsString();
977 std::string decl_name_string_without_colon(decl_name_string.c_str(), decl_name_string.length() - 1);
978 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name_string_without_colon.c_str());
979 original_selector = original_ctx->Selectors.getSelector(1, &ident);
983 SmallVector<IdentifierInfo *, 4> idents;
985 clang::Selector sel = decl_name.getObjCSelector();
987 unsigned num_args = sel.getNumArgs();
993 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
996 original_selector = original_ctx->Selectors.getSelector(num_args, idents.data());
999 DeclarationName original_decl_name(original_selector);
1001 llvm::SmallVector<NamedDecl *, 1> methods;
1003 ClangASTContext::GetCompleteDecl(original_ctx, original_interface_decl);
1005 if (ObjCMethodDecl *instance_method_decl = original_interface_decl->lookupInstanceMethod(original_selector))
1007 methods.push_back(instance_method_decl);
1009 else if (ObjCMethodDecl *class_method_decl = original_interface_decl->lookupClassMethod(original_selector))
1011 methods.push_back(class_method_decl);
1014 if (methods.empty())
1019 for (NamedDecl *named_decl : methods)
1024 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(named_decl);
1029 Decl *copied_decl = ast_importer->CopyDecl(ast_context, &result_method->getASTContext(), result_method);
1034 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
1036 if (!copied_method_decl)
1039 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1043 ASTDumper dumper((Decl*)copied_method_decl);
1044 log->Printf(" CAS::FOMD[%d] found (%s) %s", current_id, log_info, dumper.GetCString());
1047 context.AddNamedDecl(copied_method_decl);
1054 ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
1056 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1058 static unsigned int invocation_id = 0;
1059 unsigned int current_id = invocation_id++;
1061 const DeclarationName &decl_name(context.m_decl_name);
1062 const DeclContext *decl_ctx(context.m_decl_context);
1064 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
1066 if (!interface_decl)
1071 Decl *original_decl = NULL;
1072 ASTContext *original_ctx = NULL;
1074 m_ast_importer_sp->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx);
1079 ObjCInterfaceDecl *original_interface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl);
1081 if (FindObjCMethodDeclsWithOrigin(current_id,
1083 original_interface_decl,
1085 m_ast_importer_sp.get(),
1087 return; // found it, no need to look any further
1092 if (decl_name.isObjCZeroArgSelector())
1094 ss.Printf("%s", decl_name.getAsString().c_str());
1096 else if (decl_name.isObjCOneArgSelector())
1098 ss.Printf("%s", decl_name.getAsString().c_str());
1102 clang::Selector sel = decl_name.getObjCSelector();
1104 for (unsigned i = 0, e = sel.getNumArgs();
1108 llvm::StringRef r = sel.getNameForSlot(i);
1109 ss.Printf("%s:", r.str().c_str());
1114 if (strstr(ss.GetData(), "$__lldb"))
1115 return; // we don't need any results
1117 ConstString selector_name(ss.GetData());
1120 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
1121 current_id, static_cast<void*>(m_ast_context),
1122 interface_decl->getNameAsString().c_str(),
1123 selector_name.AsCString());
1124 SymbolContextList sc_list;
1126 const bool include_symbols = false;
1127 const bool include_inlines = false;
1128 const bool append = false;
1130 std::string interface_name = interface_decl->getNameAsString();
1135 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
1137 ConstString instance_method_name(ms.GetData());
1139 m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
1141 if (sc_list.GetSize())
1145 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
1147 ConstString class_method_name(ms.GetData());
1149 m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
1151 if (sc_list.GetSize())
1154 // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in
1155 // categories on the desired class.
1157 SymbolContextList candidate_sc_list;
1159 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list);
1161 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize();
1165 SymbolContext candidate_sc;
1167 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
1170 if (!candidate_sc.function)
1173 const char *candidate_name = candidate_sc.function->GetName().AsCString();
1175 const char *cursor = candidate_name;
1177 if (*cursor != '+' && *cursor != '-')
1187 size_t interface_len = interface_name.length();
1189 if (strncmp(cursor, interface_name.c_str(), interface_len))
1192 cursor += interface_len;
1194 if (*cursor == ' ' || *cursor == '(')
1195 sc_list.Append(candidate_sc);
1200 if (sc_list.GetSize())
1202 // We found a good function symbol. Use that.
1204 for (uint32_t i = 0, e = sc_list.GetSize();
1210 if (!sc_list.GetContextAtIndex(i, sc))
1216 CompilerDeclContext function_decl_ctx = sc.function->GetDeclContext();
1217 if (!function_decl_ctx)
1220 ObjCMethodDecl *method_decl = ClangASTContext::DeclContextGetAsObjCMethodDecl(function_decl_ctx);
1225 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
1227 if (!found_interface_decl)
1230 if (found_interface_decl->getName() == interface_decl->getName())
1232 Decl *copied_decl = m_ast_importer_sp->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
1237 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
1239 if (!copied_method_decl)
1244 ASTDumper dumper((Decl*)copied_method_decl);
1245 log->Printf(" CAS::FOMD[%d] found (in symbols) %s", current_id, dumper.GetCString());
1248 context.AddNamedDecl(copied_method_decl);
1255 // Try the debug information.
1259 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(interface_decl));
1261 if (!complete_interface_decl)
1264 // We found the complete interface. The runtime never needs to be queried in this scenario.
1266 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
1268 if (complete_interface_decl == interface_decl)
1269 break; // already checked this one
1272 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1273 current_id, static_cast<void*>(complete_interface_decl),
1274 static_cast<void*>(&complete_iface_decl->getASTContext()));
1276 FindObjCMethodDeclsWithOrigin(current_id,
1278 complete_interface_decl,
1280 m_ast_importer_sp.get(),
1289 // Check the modules only if the debug information didn't have a complete interface.
1291 if (ClangModulesDeclVendor *modules_decl_vendor = m_target->GetClangModulesDeclVendor())
1293 ConstString interface_name(interface_decl->getNameAsString().c_str());
1294 bool append = false;
1295 uint32_t max_matches = 1;
1296 std::vector <clang::NamedDecl *> decls;
1298 if (!modules_decl_vendor->FindDecls(interface_name,
1304 ObjCInterfaceDecl *interface_decl_from_modules = dyn_cast<ObjCInterfaceDecl>(decls[0]);
1306 if (!interface_decl_from_modules)
1309 if (FindObjCMethodDeclsWithOrigin(current_id,
1311 interface_decl_from_modules,
1313 m_ast_importer_sp.get(),
1322 // Check the runtime only if the debug information didn't have a complete interface and the modules don't get us anywhere.
1324 lldb::ProcessSP process(m_target->GetProcessSP());
1329 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1331 if (!language_runtime)
1334 DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
1339 ConstString interface_name(interface_decl->getNameAsString().c_str());
1340 bool append = false;
1341 uint32_t max_matches = 1;
1342 std::vector <clang::NamedDecl *> decls;
1344 if (!decl_vendor->FindDecls(interface_name,
1350 ObjCInterfaceDecl *runtime_interface_decl = dyn_cast<ObjCInterfaceDecl>(decls[0]);
1352 if (!runtime_interface_decl)
1355 FindObjCMethodDeclsWithOrigin(current_id,
1357 runtime_interface_decl,
1359 m_ast_importer_sp.get(),
1366 FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id,
1367 NameSearchContext &context,
1368 clang::ASTContext &ast_context,
1369 ClangASTImporter *ast_importer,
1370 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl)
1372 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1374 if (origin_iface_decl.IsInvalid())
1377 std::string name_str = context.m_decl_name.getAsString();
1378 StringRef name(name_str.c_str());
1379 IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name));
1381 DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier));
1385 if (origin_property_decl.IsValid())
1387 DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context));
1388 if (parser_property_decl.IsValid())
1392 ASTDumper dumper((Decl*)parser_property_decl.decl);
1393 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1396 context.AddNamedDecl(parser_property_decl.decl);
1401 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier));
1403 if (origin_ivar_decl.IsValid())
1405 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context));
1406 if (parser_ivar_decl.IsValid())
1410 ASTDumper dumper((Decl*)parser_ivar_decl.decl);
1411 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1414 context.AddNamedDecl(parser_ivar_decl.decl);
1423 ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context)
1425 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1427 static unsigned int invocation_id = 0;
1428 unsigned int current_id = invocation_id++;
1430 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context));
1431 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer_sp.get()));
1433 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
1436 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'",
1437 current_id, static_cast<void*>(m_ast_context),
1438 parser_iface_decl->getNameAsString().c_str(),
1439 context.m_decl_name.getAsString().c_str());
1441 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1444 m_ast_importer_sp.get(),
1449 log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...",
1450 current_id, static_cast<const void*>(origin_iface_decl.decl),
1451 static_cast<void*>(&origin_iface_decl->getASTContext()));
1453 SymbolContext null_sc;
1458 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(parser_iface_decl.decl));
1460 if (!complete_interface_decl)
1463 // We found the complete interface. The runtime never needs to be queried in this scenario.
1465 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
1467 if (complete_iface_decl.decl == origin_iface_decl.decl)
1468 break; // already checked this one
1471 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1473 static_cast<const void*>(complete_iface_decl.decl),
1474 static_cast<void*>(&complete_iface_decl->getASTContext()));
1476 FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1479 m_ast_importer_sp.get(),
1480 complete_iface_decl);
1488 // Check the modules only if the debug information didn't have a complete interface.
1490 ClangModulesDeclVendor *modules_decl_vendor = m_target->GetClangModulesDeclVendor();
1492 if (!modules_decl_vendor)
1495 bool append = false;
1496 uint32_t max_matches = 1;
1497 std::vector <clang::NamedDecl *> decls;
1499 if (!modules_decl_vendor->FindDecls(class_name,
1505 DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_modules(dyn_cast<ObjCInterfaceDecl>(decls[0]));
1507 if (!interface_decl_from_modules.IsValid())
1511 log->Printf("CAS::FOPD[%d] trying module (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1513 static_cast<const void*>(interface_decl_from_modules.decl),
1514 static_cast<void*>(&interface_decl_from_modules->getASTContext()));
1516 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1519 m_ast_importer_sp.get(),
1520 interface_decl_from_modules))
1527 // Check the runtime only if the debug information didn't have a complete interface
1528 // and nothing was in the modules.
1530 lldb::ProcessSP process(m_target->GetProcessSP());
1535 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1537 if (!language_runtime)
1540 DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
1545 bool append = false;
1546 uint32_t max_matches = 1;
1547 std::vector <clang::NamedDecl *> decls;
1549 if (!decl_vendor->FindDecls(class_name,
1555 DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_runtime(dyn_cast<ObjCInterfaceDecl>(decls[0]));
1557 if (!interface_decl_from_runtime.IsValid())
1561 log->Printf("CAS::FOPD[%d] trying runtime (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1563 static_cast<const void*>(interface_decl_from_runtime.decl),
1564 static_cast<void*>(&interface_decl_from_runtime->getASTContext()));
1566 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1569 m_ast_importer_sp.get(),
1570 interface_decl_from_runtime))
1576 typedef llvm::DenseMap<const FieldDecl *, uint64_t> FieldOffsetMap;
1577 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1579 template <class D, class O>
1581 ImportOffsetMap(llvm::DenseMap<const D *, O> &destination_map, llvm::DenseMap<const D *, O> &source_map,
1582 ClangASTImporter *importer, ASTContext &dest_ctx)
1584 // When importing fields into a new record, clang has a hard requirement that
1585 // fields be imported in field offset order. Since they are stored in a DenseMap
1586 // with a pointer as the key type, this means we cannot simply iterate over the
1587 // map, as the order will be non-deterministic. Instead we have to sort by the offset
1588 // and then insert in sorted order.
1589 typedef llvm::DenseMap<const D *, O> MapType;
1590 typedef typename MapType::value_type PairType;
1591 std::vector<PairType> sorted_items;
1592 sorted_items.reserve(source_map.size());
1593 sorted_items.assign(source_map.begin(), source_map.end());
1594 std::sort(sorted_items.begin(), sorted_items.end(),
1595 [](const PairType &lhs, const PairType &rhs)
1597 return lhs.second < rhs.second;
1600 for (const auto &item : sorted_items)
1602 DeclFromUser<D> user_decl(const_cast<D *>(item.first));
1603 DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx));
1604 if (parser_decl.IsInvalid())
1606 destination_map.insert(std::pair<const D *, O>(parser_decl.decl, item.second));
1612 template <bool IsVirtual>
1614 ExtractBaseOffsets(const ASTRecordLayout &record_layout, DeclFromUser<const CXXRecordDecl> &record,
1615 BaseOffsetMap &base_offsets)
1617 for (CXXRecordDecl::base_class_const_iterator bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
1618 be = (IsVirtual ? record->vbases_end() : record->bases_end());
1621 if (!IsVirtual && bi->isVirtual())
1624 const clang::Type *origin_base_type = bi->getType().getTypePtr();
1625 const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>();
1627 if (!origin_base_record_type)
1630 DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl());
1632 if (origin_base_record.IsInvalid())
1635 DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record));
1637 if (origin_base_cxx_record.IsInvalid())
1640 CharUnits base_offset;
1643 base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1645 base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1647 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset));
1654 ClangASTSource::layoutRecordType(const RecordDecl *record, uint64_t &size, uint64_t &alignment,
1655 FieldOffsetMap &field_offsets, BaseOffsetMap &base_offsets,
1656 BaseOffsetMap &virtual_base_offsets)
1658 ClangASTMetrics::RegisterRecordLayout();
1660 static unsigned int invocation_id = 0;
1661 unsigned int current_id = invocation_id++;
1663 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1666 log->Printf("LayoutRecordType[%u] on (ASTContext*)%p for (RecordDecl*)%p [name = '%s']",
1667 current_id, static_cast<void*>(m_ast_context),
1668 static_cast<const void*>(record),
1669 record->getNameAsString().c_str());
1671 DeclFromParser <const RecordDecl> parser_record(record);
1672 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer_sp.get()));
1674 if (origin_record.IsInvalid())
1677 FieldOffsetMap origin_field_offsets;
1678 BaseOffsetMap origin_base_offsets;
1679 BaseOffsetMap origin_virtual_base_offsets;
1681 ClangASTContext::GetCompleteDecl(&origin_record->getASTContext(), const_cast<RecordDecl*>(origin_record.decl));
1683 clang::RecordDecl* definition = origin_record.decl->getDefinition();
1684 if (!definition || !definition->isCompleteDefinition())
1687 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1689 int field_idx = 0, field_count = record_layout.getFieldCount();
1691 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end(); fi != fe; ++fi)
1693 if (field_idx >= field_count)
1694 return false; // Layout didn't go well. Bail out.
1696 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1698 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1703 ASTContext &parser_ast_context(record->getASTContext());
1705 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1707 if (origin_cxx_record.IsValid())
1709 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1710 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1714 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer_sp.get(), parser_ast_context) ||
1715 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer_sp.get(), parser_ast_context) ||
1716 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer_sp.get(), parser_ast_context))
1719 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1720 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
1724 log->Printf("LRT[%u] returned:", current_id);
1725 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id,
1726 static_cast<const void*>(origin_record.decl));
1727 log->Printf("LRT[%u] Size = %" PRId64, current_id, size);
1728 log->Printf("LRT[%u] Alignment = %" PRId64, current_id, alignment);
1729 log->Printf("LRT[%u] Fields:", current_id);
1730 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1734 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %" PRId64 " bits", current_id,
1735 static_cast<void *>(*fi), fi->getNameAsString().c_str(), field_offsets[*fi]);
1737 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1738 if (parser_cxx_record.IsValid())
1740 log->Printf("LRT[%u] Bases:", current_id);
1741 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1745 bool is_virtual = bi->isVirtual();
1747 QualType base_type = bi->getType();
1748 const RecordType *base_record_type = base_type->getAs<RecordType>();
1749 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1750 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
1752 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %" PRId64 " chars", current_id,
1753 (is_virtual ? "Virtual " : ""), static_cast<void *>(base_cxx_record.decl),
1754 base_cxx_record.decl->getNameAsString().c_str(),
1755 (is_virtual ? virtual_base_offsets[base_cxx_record.decl].getQuantity()
1756 : base_offsets[base_cxx_record.decl].getQuantity()));
1761 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1769 ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1770 const ConstString &name,
1771 ClangASTImporter::NamespaceMapSP &parent_map) const
1773 static unsigned int invocation_id = 0;
1774 unsigned int current_id = invocation_id++;
1776 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1780 if (parent_map && parent_map->size())
1781 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
1782 current_id, static_cast<void*>(m_ast_context),
1784 parent_map->begin()->second.GetName().AsCString());
1786 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
1787 current_id, static_cast<void*>(m_ast_context),
1793 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1797 CompilerDeclContext found_namespace_decl;
1799 lldb::ModuleSP module_sp = i->first;
1800 CompilerDeclContext module_parent_namespace_decl = i->second;
1802 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
1807 SymbolContext null_sc;
1809 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
1811 if (!found_namespace_decl)
1814 namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>(module_sp, found_namespace_decl));
1817 log->Printf(" CMN[%u] Found namespace %s in module %s",
1820 module_sp->GetFileSpec().GetFilename().GetCString());
1825 const ModuleList &target_images = m_target->GetImages();
1826 Mutex::Locker modules_locker(target_images.GetMutex());
1828 CompilerDeclContext null_namespace_decl;
1830 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
1832 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
1837 CompilerDeclContext found_namespace_decl;
1839 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
1844 SymbolContext null_sc;
1846 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
1848 if (!found_namespace_decl)
1851 namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>(image, found_namespace_decl));
1854 log->Printf(" CMN[%u] Found namespace %s in module %s",
1857 image->GetFileSpec().GetFilename().GetCString());
1863 ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1865 if (!namespace_decls)
1868 const CompilerDeclContext &namespace_decl = namespace_decls->begin()->second;
1870 clang::ASTContext *src_ast = ClangASTContext::DeclContextGetClangASTContext(namespace_decl);
1873 clang::NamespaceDecl *src_namespace_decl = ClangASTContext::DeclContextGetAsNamespaceDecl(namespace_decl);
1875 if (!src_namespace_decl)
1878 Decl *copied_decl = m_ast_importer_sp->CopyDecl(m_ast_context, src_ast, src_namespace_decl);
1883 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1885 if (!copied_namespace_decl)
1888 context.m_decls.push_back(copied_namespace_decl);
1890 m_ast_importer_sp->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1892 return dyn_cast<NamespaceDecl>(copied_decl);
1896 ClangASTSource::GuardedCopyType (const CompilerType &src_type)
1898 ClangASTContext *src_ast = llvm::dyn_cast_or_null<ClangASTContext>(src_type.GetTypeSystem());
1899 if (src_ast == nullptr)
1900 return CompilerType();
1902 ClangASTMetrics::RegisterLLDBImport();
1904 SetImportInProgress(true);
1906 QualType copied_qual_type = m_ast_importer_sp->CopyType (m_ast_context, src_ast->getASTContext(), ClangASTContext::GetQualType(src_type));
1908 SetImportInProgress(false);
1910 if (copied_qual_type.getAsOpaquePtr() && copied_qual_type->getCanonicalTypeInternal().isNull())
1911 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1913 return CompilerType();
1915 return CompilerType(m_ast_context, copied_qual_type);
1919 NameSearchContext::AddVarDecl(const CompilerType &type)
1921 assert (type && "Type for variable must be valid!");
1923 if (!type.IsValid())
1926 ClangASTContext* lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
1930 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
1932 clang::ASTContext *ast = lldb_ast->getASTContext();
1934 clang::NamedDecl *Decl = VarDecl::Create(*ast,
1935 const_cast<DeclContext*>(m_decl_context),
1939 ClangASTContext::GetQualType(type),
1942 m_decls.push_back(Decl);
1948 NameSearchContext::AddFunDecl (const CompilerType &type, bool extern_c)
1950 assert (type && "Type for variable must be valid!");
1952 if (!type.IsValid())
1955 if (m_function_types.count(type))
1958 ClangASTContext* lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
1962 m_function_types.insert(type);
1964 QualType qual_type (ClangASTContext::GetQualType(type));
1966 clang::ASTContext *ast = lldb_ast->getASTContext();
1968 const bool isInlineSpecified = false;
1969 const bool hasWrittenPrototype = true;
1970 const bool isConstexprSpecified = false;
1972 clang::DeclContext *context = const_cast<DeclContext*>(m_decl_context);
1975 context = LinkageSpecDecl::Create(*ast,
1979 clang::LinkageSpecDecl::LanguageIDs::lang_c,
1983 // Pass the identifier info for functions the decl_name is needed for operators
1984 clang::DeclarationName decl_name = m_decl_name.getNameKind() == DeclarationName::Identifier ? m_decl_name.getAsIdentifierInfo() : m_decl_name;
1986 clang::FunctionDecl *func_decl = FunctionDecl::Create (*ast,
1995 hasWrittenPrototype,
1996 isConstexprSpecified);
1998 // We have to do more than just synthesize the FunctionDecl. We have to
1999 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
2000 // this, we raid the function's FunctionProtoType for types.
2002 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
2004 if (func_proto_type)
2006 unsigned NumArgs = func_proto_type->getNumParams();
2009 SmallVector<ParmVarDecl *, 5> parm_var_decls;
2011 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
2013 QualType arg_qual_type (func_proto_type->getParamType(ArgIndex));
2015 parm_var_decls.push_back(ParmVarDecl::Create (*ast,
2016 const_cast<DeclContext*>(context),
2026 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
2030 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2033 log->Printf("Function type wasn't a FunctionProtoType");
2036 m_decls.push_back(func_decl);
2042 NameSearchContext::AddGenericFunDecl()
2044 FunctionProtoType::ExtProtoInfo proto_info;
2046 proto_info.Variadic = true;
2048 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
2049 ArrayRef<QualType>(), // argument types
2052 return AddFunDecl(CompilerType (m_ast_source.m_ast_context, generic_function_type), true);
2056 NameSearchContext::AddTypeDecl(const CompilerType &clang_type)
2060 QualType qual_type = ClangASTContext::GetQualType(clang_type);
2062 if (const TypedefType *typedef_type = llvm::dyn_cast<TypedefType>(qual_type))
2064 TypedefNameDecl *typedef_name_decl = typedef_type->getDecl();
2066 m_decls.push_back(typedef_name_decl);
2068 return (NamedDecl*)typedef_name_decl;
2070 else if (const TagType *tag_type = qual_type->getAs<TagType>())
2072 TagDecl *tag_decl = tag_type->getDecl();
2074 m_decls.push_back(tag_decl);
2078 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
2080 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
2082 m_decls.push_back((NamedDecl*)interface_decl);
2084 return (NamedDecl*)interface_decl;
2091 NameSearchContext::AddLookupResult (clang::DeclContextLookupResult result)
2093 for (clang::NamedDecl *decl : result)
2094 m_decls.push_back (decl);
2098 NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
2100 m_decls.push_back (decl);