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 //===----------------------------------------------------------------------===//
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/RecordLayout.h"
13 #include "lldb/Core/Log.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/ModuleList.h"
16 #include "lldb/Expression/ASTDumper.h"
17 #include "lldb/Expression/ClangASTSource.h"
18 #include "lldb/Expression/ClangExpression.h"
19 #include "lldb/Symbol/ClangNamespaceDecl.h"
20 #include "lldb/Symbol/Function.h"
21 #include "lldb/Symbol/SymbolVendor.h"
22 #include "lldb/Target/ObjCLanguageRuntime.h"
23 #include "lldb/Target/Target.h"
25 using namespace clang;
26 using namespace lldb_private;
28 ClangASTSource::~ClangASTSource()
30 m_ast_importer->ForgetDestination(m_ast_context);
32 // We are in the process of destruction, don't create clang ast context on demand
33 // by passing false to Target::GetScratchClangASTContext(create_on_demand).
34 ClangASTContext *scratch_clang_ast_context = m_target->GetScratchClangASTContext(false);
36 if (!scratch_clang_ast_context)
39 clang::ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
41 if (!scratch_ast_context)
44 if (m_ast_context != scratch_ast_context)
45 m_ast_importer->ForgetSource(scratch_ast_context, m_ast_context);
49 ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer)
54 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
55 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
58 // The core lookup interface.
60 ClangASTSource::FindExternalVisibleDeclsByName
62 const DeclContext *decl_ctx,
63 DeclarationName clang_decl_name
68 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
72 if (GetImportInProgress())
74 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
78 std::string decl_name (clang_decl_name.getAsString());
80 // if (m_decl_map.DoingASTImport ())
81 // return DeclContext::lookup_result();
83 switch (clang_decl_name.getNameKind()) {
84 // Normal identifiers.
85 case DeclarationName::Identifier:
87 clang::IdentifierInfo *identifier_info = clang_decl_name.getAsIdentifierInfo();
89 if (!identifier_info ||
90 identifier_info->getBuiltinID() != 0)
92 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
98 // Operator names. Not important for now.
99 case DeclarationName::CXXOperatorName:
100 case DeclarationName::CXXLiteralOperatorName:
101 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
104 // Using directives found in this context.
105 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
106 case DeclarationName::CXXUsingDirective:
107 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
110 case DeclarationName::ObjCZeroArgSelector:
111 case DeclarationName::ObjCOneArgSelector:
112 case DeclarationName::ObjCMultiArgSelector:
114 llvm::SmallVector<NamedDecl*, 1> method_decls;
116 NameSearchContext method_search_context (*this, method_decls, clang_decl_name, decl_ctx);
118 FindObjCMethodDecls(method_search_context);
120 SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, method_decls);
121 return (method_decls.size() > 0);
123 // These aren't possible in the global context.
124 case DeclarationName::CXXConstructorName:
125 case DeclarationName::CXXDestructorName:
126 case DeclarationName::CXXConversionFunctionName:
127 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
132 if (!GetLookupsEnabled())
134 // Wait until we see a '$' at the start of a name before we start doing
135 // any lookups so we can avoid lookup up all of the builtin types.
136 if (!decl_name.empty() && decl_name[0] == '$')
138 SetLookupsEnabled (true);
142 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
147 ConstString const_decl_name(decl_name.c_str());
149 const char *uniqued_const_decl_name = const_decl_name.GetCString();
150 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
152 // We are currently looking up this name...
153 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
156 m_active_lookups.insert(uniqued_const_decl_name);
157 // static uint32_t g_depth = 0;
159 // printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
160 llvm::SmallVector<NamedDecl*, 4> name_decls;
161 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
162 FindExternalVisibleDecls(name_search_context);
163 SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls);
165 m_active_lookups.erase (uniqued_const_decl_name);
166 return (name_decls.size() != 0);
170 ClangASTSource::CompleteType (TagDecl *tag_decl)
172 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
174 static unsigned int invocation_id = 0;
175 unsigned int current_id = invocation_id++;
179 log->Printf(" CompleteTagDecl[%u] on (ASTContext*)%p Completing (TagDecl*)%p named %s",
180 current_id, static_cast<void*>(m_ast_context),
181 static_cast<void*>(tag_decl),
182 tag_decl->getName().str().c_str());
184 log->Printf(" CTD[%u] Before:", current_id);
185 ASTDumper dumper((Decl*)tag_decl);
186 dumper.ToLog(log, " [CTD] ");
189 if (!m_ast_importer->CompleteTagDecl (tag_decl))
191 // We couldn't complete the type. Maybe there's a definition
192 // somewhere else that can be completed.
195 log->Printf(" CTD[%u] Type could not be completed in the module in which it was first found.", current_id);
199 DeclContext *decl_ctx = tag_decl->getDeclContext();
201 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(decl_ctx))
203 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
205 if (log && log->GetVerbose())
206 log->Printf(" CTD[%u] Inspecting namespace map %p (%d entries)",
207 current_id, static_cast<void*>(namespace_map.get()),
208 static_cast<int>(namespace_map->size()));
213 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
218 log->Printf(" CTD[%u] Searching namespace %s in module %s",
220 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
221 i->first->GetFileSpec().GetFilename().GetCString());
225 SymbolContext null_sc;
226 ConstString name(tag_decl->getName().str().c_str());
228 i->first->FindTypesInNamespace(null_sc, name, &i->second, UINT32_MAX, types);
230 for (uint32_t ti = 0, te = types.GetSize();
234 lldb::TypeSP type = types.GetTypeAtIndex(ti);
239 ClangASTType clang_type (type->GetClangFullType());
244 const TagType *tag_type = clang_type.GetQualType()->getAs<TagType>();
249 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
251 if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
260 SymbolContext null_sc;
261 ConstString name(tag_decl->getName().str().c_str());
262 ClangNamespaceDecl namespace_decl;
264 const ModuleList &module_list = m_target->GetImages();
266 bool exact_match = false;
267 module_list.FindTypes (null_sc, name, exact_match, UINT32_MAX, types);
269 for (uint32_t ti = 0, te = types.GetSize();
273 lldb::TypeSP type = types.GetTypeAtIndex(ti);
278 ClangASTType clang_type (type->GetClangFullType());
283 const TagType *tag_type = clang_type.GetQualType()->getAs<TagType>();
288 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
290 if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
298 log->Printf(" [CTD] After:");
299 ASTDumper dumper((Decl*)tag_decl);
300 dumper.ToLog(log, " [CTD] ");
305 ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl)
307 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
311 log->Printf(" [CompleteObjCInterfaceDecl] on (ASTContext*)%p Completing an ObjCInterfaceDecl named %s",
312 static_cast<void*>(m_ast_context),
313 interface_decl->getName().str().c_str());
314 log->Printf(" [COID] Before:");
315 ASTDumper dumper((Decl*)interface_decl);
316 dumper.ToLog(log, " [COID] ");
319 Decl *original_decl = NULL;
320 ASTContext *original_ctx = NULL;
322 if (m_ast_importer->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx))
324 if (ObjCInterfaceDecl *original_iface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl))
326 ObjCInterfaceDecl *complete_iface_decl = GetCompleteObjCInterface(original_iface_decl);
328 if (complete_iface_decl && (complete_iface_decl != original_iface_decl))
330 m_ast_importer->SetDeclOrigin(interface_decl, original_iface_decl);
335 m_ast_importer->CompleteObjCInterfaceDecl (interface_decl);
337 if (interface_decl->getSuperClass() &&
338 interface_decl->getSuperClass() != interface_decl)
339 CompleteType(interface_decl->getSuperClass());
343 log->Printf(" [COID] After:");
344 ASTDumper dumper((Decl*)interface_decl);
345 dumper.ToLog(log, " [COID] ");
349 clang::ObjCInterfaceDecl *
350 ClangASTSource::GetCompleteObjCInterface (clang::ObjCInterfaceDecl *interface_decl)
352 lldb::ProcessSP process(m_target->GetProcessSP());
357 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
359 if (!language_runtime)
362 ConstString class_name(interface_decl->getNameAsString().c_str());
364 lldb::TypeSP complete_type_sp(language_runtime->LookupInCompleteClassCache(class_name));
366 if (!complete_type_sp)
369 TypeFromUser complete_type = TypeFromUser(complete_type_sp->GetClangFullType());
370 lldb::clang_type_t complete_opaque_type = complete_type.GetOpaqueQualType();
372 if (!complete_opaque_type)
375 const clang::Type *complete_clang_type = QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
376 const ObjCInterfaceType *complete_interface_type = dyn_cast<ObjCInterfaceType>(complete_clang_type);
378 if (!complete_interface_type)
381 ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl());
383 return complete_iface_decl;
386 clang::ExternalLoadResult
387 ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
388 bool (*predicate)(Decl::Kind),
389 llvm::SmallVectorImpl<Decl*> &decls)
391 ClangASTMetrics::RegisterLexicalQuery();
393 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
395 const Decl *context_decl = dyn_cast<Decl>(decl_context);
400 static unsigned int invocation_id = 0;
401 unsigned int current_id = invocation_id++;
405 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
406 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate",
407 current_id, static_cast<void*>(m_ast_context),
408 context_named_decl->getNameAsString().c_str(),
409 context_decl->getDeclKindName(),
410 static_cast<const void*>(context_decl),
411 (predicate ? "non-null" : "null"));
412 else if(context_decl)
413 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate",
414 current_id, static_cast<void*>(m_ast_context),
415 context_decl->getDeclKindName(),
416 static_cast<const void*>(context_decl),
417 (predicate ? "non-null" : "null"));
419 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate",
420 current_id, static_cast<const void*>(m_ast_context),
421 (predicate ? "non-null" : "null"));
424 Decl *original_decl = NULL;
425 ASTContext *original_ctx = NULL;
427 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
432 log->Printf(" FELD[%u] Original decl (ASTContext*)%p (Decl*)%p:",
433 current_id, static_cast<void*>(original_ctx),
434 static_cast<void*>(original_decl));
435 ASTDumper(original_decl).ToLog(log, " ");
438 if (ObjCInterfaceDecl *original_iface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl))
440 ObjCInterfaceDecl *complete_iface_decl = GetCompleteObjCInterface(original_iface_decl);
442 if (complete_iface_decl && (complete_iface_decl != original_iface_decl))
444 original_decl = complete_iface_decl;
445 original_ctx = &complete_iface_decl->getASTContext();
447 m_ast_importer->SetDeclOrigin(context_decl, original_iface_decl);
451 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
453 ExternalASTSource *external_source = original_ctx->getExternalSource();
456 external_source->CompleteType (original_tag_decl);
459 const DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
461 if (!original_decl_context)
464 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
465 iter != original_decl_context->decls_end();
470 if (!predicate || predicate(decl->getKind()))
474 ASTDumper ast_dumper(decl);
475 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
476 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());
478 log->Printf(" FELD[%d] Adding lexical %sDecl %s", current_id, decl->getDeclKindName(), ast_dumper.GetCString());
481 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
486 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl))
488 QualType copied_field_type = copied_field->getType();
490 m_ast_importer->RequireCompleteType(copied_field_type);
493 decls.push_back(copied_decl);
495 DeclContext *decl_context_non_const = const_cast<DeclContext *>(decl_context);
497 if (copied_decl->getDeclContext() != decl_context)
499 if (copied_decl->getDeclContext()->containsDecl(copied_decl))
500 copied_decl->getDeclContext()->removeDecl(copied_decl);
501 copied_decl->setDeclContext(decl_context_non_const);
504 if (!decl_context_non_const->containsDecl(copied_decl))
505 decl_context_non_const->addDeclInternal(copied_decl);
509 return ELR_AlreadyLoaded;
513 ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
515 assert (m_ast_context);
517 ClangASTMetrics::RegisterVisibleQuery();
519 const ConstString name(context.m_decl_name.getAsString().c_str());
521 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
523 static unsigned int invocation_id = 0;
524 unsigned int current_id = invocation_id++;
528 if (!context.m_decl_context)
529 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a NULL DeclContext",
530 current_id, static_cast<void*>(m_ast_context),
532 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
533 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in '%s'",
534 current_id, static_cast<void*>(m_ast_context),
536 context_named_decl->getNameAsString().c_str());
538 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a '%s'",
539 current_id, static_cast<void*>(m_ast_context),
541 context.m_decl_context->getDeclKindName());
544 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
546 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
548 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
550 if (log && log->GetVerbose())
551 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
552 current_id, static_cast<void*>(namespace_map.get()),
553 static_cast<int>(namespace_map->size()));
558 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
563 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
565 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
566 i->first->GetFileSpec().GetFilename().GetCString());
568 FindExternalVisibleDecls(context,
574 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
576 FindObjCPropertyAndIvarDecls(context);
578 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
580 // we shouldn't be getting FindExternalVisibleDecls calls for these
585 ClangNamespaceDecl namespace_decl;
588 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
590 FindExternalVisibleDecls(context,
596 if (!context.m_namespace_map->empty())
598 if (log && log->GetVerbose())
599 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
601 static_cast<void*>(context.m_namespace_map.get()),
602 static_cast<int>(context.m_namespace_map->size()));
604 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
606 if (clang_namespace_decl)
607 clang_namespace_decl->setHasExternalVisibleStorage();
612 ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
613 lldb::ModuleSP module_sp,
614 ClangNamespaceDecl &namespace_decl,
615 unsigned int current_id)
617 assert (m_ast_context);
619 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
621 SymbolContextList sc_list;
623 const ConstString name(context.m_decl_name.getAsString().c_str());
625 const char *name_unique_cstr = name.GetCString();
627 static ConstString id_name("id");
628 static ConstString Class_name("Class");
630 if (name == id_name || name == Class_name)
633 if (name_unique_cstr == NULL)
636 // The ClangASTSource is not responsible for finding $-names.
637 if (name_unique_cstr[0] == '$')
640 if (module_sp && namespace_decl)
642 ClangNamespaceDecl found_namespace_decl;
644 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
648 SymbolContext null_sc;
650 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
652 if (found_namespace_decl)
654 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
657 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
660 module_sp->GetFileSpec().GetFilename().GetCString());
666 const ModuleList &target_images = m_target->GetImages();
667 Mutex::Locker modules_locker (target_images.GetMutex());
669 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
671 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
676 ClangNamespaceDecl found_namespace_decl;
678 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
683 SymbolContext null_sc;
685 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
687 if (found_namespace_decl)
689 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
692 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
695 image->GetFileSpec().GetFilename().GetCString());
703 SymbolContext null_sc;
704 const bool exact_match = false;
706 if (module_sp && namespace_decl)
707 module_sp->FindTypesInNamespace(null_sc, name, &namespace_decl, 1, types);
709 m_target->GetImages().FindTypes(null_sc, name, exact_match, 1, types);
713 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
717 const char *name_string = type_sp->GetName().GetCString();
719 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
722 (name_string ? name_string : "<anonymous>"));
725 ClangASTType full_type = type_sp->GetClangFullType();
727 ClangASTType copied_clang_type (GuardedCopyType(full_type));
729 if (!copied_clang_type)
732 log->Printf(" CAS::FEVD[%u] - Couldn't export a type",
738 context.AddTypeDecl(copied_clang_type);
744 // Couldn't find any types elsewhere. Try the Objective-C runtime if one exists.
746 lldb::ProcessSP process(m_target->GetProcessSP());
751 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
753 if (!language_runtime)
756 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
762 uint32_t max_matches = 1;
763 std::vector <ClangASTType> types;
765 if (!type_vendor->FindTypes(name,
773 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\" in the runtime",
778 ClangASTType copied_clang_type (GuardedCopyType(types[0]));
780 if (!copied_clang_type)
783 log->Printf(" CAS::FEVD[%u] - Couldn't export a type from the runtime",
789 context.AddTypeDecl(copied_clang_type);
797 template <class D> class TaggedASTDecl {
799 TaggedASTDecl() : decl(NULL) { }
800 TaggedASTDecl(D *_decl) : decl(_decl) { }
801 bool IsValid() const { return (decl != NULL); }
802 bool IsInvalid() const { return !IsValid(); }
803 D *operator->() const { return decl; }
807 template <class D2, template <class D> class TD, class D1>
809 DynCast(TD<D1> source)
811 return TD<D2> (dyn_cast<D2>(source.decl));
814 template <class D = Decl> class DeclFromParser;
815 template <class D = Decl> class DeclFromUser;
817 template <class D> class DeclFromParser : public TaggedASTDecl<D> {
819 DeclFromParser() : TaggedASTDecl<D>() { }
820 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { }
822 DeclFromUser<D> GetOrigin(ClangASTImporter *importer);
825 template <class D> class DeclFromUser : public TaggedASTDecl<D> {
827 DeclFromUser() : TaggedASTDecl<D>() { }
828 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { }
830 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx);
835 DeclFromParser<D>::GetOrigin(ClangASTImporter *importer)
837 DeclFromUser <> origin_decl;
838 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
839 if (origin_decl.IsInvalid())
840 return DeclFromUser<D>();
841 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
846 DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx)
848 DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl));
849 if (parser_generic_decl.IsInvalid())
850 return DeclFromParser<D>();
851 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
855 FindObjCMethodDeclsWithOrigin (unsigned int current_id,
856 NameSearchContext &context,
857 ObjCInterfaceDecl *original_interface_decl,
858 clang::ASTContext *ast_context,
859 ClangASTImporter *ast_importer,
860 const char *log_info)
862 const DeclarationName &decl_name(context.m_decl_name);
863 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();
865 Selector original_selector;
867 if (decl_name.isObjCZeroArgSelector())
869 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString());
870 original_selector = original_ctx->Selectors.getSelector(0, &ident);
872 else if (decl_name.isObjCOneArgSelector())
874 const std::string &decl_name_string = decl_name.getAsString();
875 std::string decl_name_string_without_colon(decl_name_string.c_str(), decl_name_string.length() - 1);
876 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name_string_without_colon.c_str());
877 original_selector = original_ctx->Selectors.getSelector(1, &ident);
881 SmallVector<IdentifierInfo *, 4> idents;
883 clang::Selector sel = decl_name.getObjCSelector();
885 unsigned num_args = sel.getNumArgs();
891 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
894 original_selector = original_ctx->Selectors.getSelector(num_args, idents.data());
897 DeclarationName original_decl_name(original_selector);
899 ObjCInterfaceDecl::lookup_result result = original_interface_decl->lookup(original_decl_name);
907 for (NamedDecl *named_decl : result)
909 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(named_decl);
914 Decl *copied_decl = ast_importer->CopyDecl(ast_context, &result_method->getASTContext(), result_method);
919 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
921 if (!copied_method_decl)
924 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
928 ASTDumper dumper((Decl*)copied_method_decl);
929 log->Printf(" CAS::FOMD[%d] found (%s) %s", current_id, log_info, dumper.GetCString());
932 context.AddNamedDecl(copied_method_decl);
939 ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
941 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
943 static unsigned int invocation_id = 0;
944 unsigned int current_id = invocation_id++;
946 const DeclarationName &decl_name(context.m_decl_name);
947 const DeclContext *decl_ctx(context.m_decl_context);
949 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
956 Decl *original_decl = NULL;
957 ASTContext *original_ctx = NULL;
959 m_ast_importer->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx);
964 ObjCInterfaceDecl *original_interface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl);
966 if (FindObjCMethodDeclsWithOrigin(current_id,
968 original_interface_decl,
972 return; // found it, no need to look any further
977 if (decl_name.isObjCZeroArgSelector())
979 ss.Printf("%s", decl_name.getAsString().c_str());
981 else if (decl_name.isObjCOneArgSelector())
983 ss.Printf("%s", decl_name.getAsString().c_str());
987 clang::Selector sel = decl_name.getObjCSelector();
989 for (unsigned i = 0, e = sel.getNumArgs();
993 llvm::StringRef r = sel.getNameForSlot(i);
994 ss.Printf("%s:", r.str().c_str());
999 if (strstr(ss.GetData(), "$__lldb"))
1000 return; // we don't need any results
1002 ConstString selector_name(ss.GetData());
1005 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
1006 current_id, static_cast<void*>(m_ast_context),
1007 interface_decl->getNameAsString().c_str(),
1008 selector_name.AsCString());
1009 SymbolContextList sc_list;
1011 const bool include_symbols = false;
1012 const bool include_inlines = false;
1013 const bool append = false;
1015 std::string interface_name = interface_decl->getNameAsString();
1020 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
1022 ConstString instance_method_name(ms.GetData());
1024 m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
1026 if (sc_list.GetSize())
1030 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
1032 ConstString class_method_name(ms.GetData());
1034 m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
1036 if (sc_list.GetSize())
1039 // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in
1040 // categories on the desired class.
1042 SymbolContextList candidate_sc_list;
1044 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list);
1046 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize();
1050 SymbolContext candidate_sc;
1052 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
1055 if (!candidate_sc.function)
1058 const char *candidate_name = candidate_sc.function->GetName().AsCString();
1060 const char *cursor = candidate_name;
1062 if (*cursor != '+' && *cursor != '-')
1072 size_t interface_len = interface_name.length();
1074 if (strncmp(cursor, interface_name.c_str(), interface_len))
1077 cursor += interface_len;
1079 if (*cursor == ' ' || *cursor == '(')
1080 sc_list.Append(candidate_sc);
1085 if (sc_list.GetSize())
1087 // We found a good function symbol. Use that.
1089 for (uint32_t i = 0, e = sc_list.GetSize();
1095 if (!sc_list.GetContextAtIndex(i, sc))
1101 DeclContext *function_ctx = sc.function->GetClangDeclContext();
1106 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
1111 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
1113 if (!found_interface_decl)
1116 if (found_interface_decl->getName() == interface_decl->getName())
1118 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
1123 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
1125 if (!copied_method_decl)
1130 ASTDumper dumper((Decl*)copied_method_decl);
1131 log->Printf(" CAS::FOMD[%d] found (in symbols) %s", current_id, dumper.GetCString());
1134 context.AddNamedDecl(copied_method_decl);
1141 // Try the debug information.
1145 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(interface_decl));
1147 if (!complete_interface_decl)
1150 // We found the complete interface. The runtime never needs to be queried in this scenario.
1152 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
1154 if (complete_interface_decl == interface_decl)
1155 break; // already checked this one
1158 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1159 current_id, static_cast<void*>(complete_interface_decl),
1160 static_cast<void*>(&complete_iface_decl->getASTContext()));
1162 FindObjCMethodDeclsWithOrigin(current_id,
1164 complete_interface_decl,
1175 // Check the runtime only if the debug information didn't have a complete interface.
1177 lldb::ProcessSP process(m_target->GetProcessSP());
1182 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1184 if (!language_runtime)
1187 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1192 ConstString interface_name(interface_decl->getNameAsString().c_str());
1193 bool append = false;
1194 uint32_t max_matches = 1;
1195 std::vector <ClangASTType> types;
1197 if (!type_vendor->FindTypes(interface_name,
1203 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1205 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1207 if (!runtime_interface_type)
1210 ObjCInterfaceDecl *runtime_interface_decl = runtime_interface_type->getDecl();
1212 FindObjCMethodDeclsWithOrigin(current_id,
1214 runtime_interface_decl,
1223 FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id,
1224 NameSearchContext &context,
1225 clang::ASTContext &ast_context,
1226 ClangASTImporter *ast_importer,
1227 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl)
1229 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1231 if (origin_iface_decl.IsInvalid())
1234 std::string name_str = context.m_decl_name.getAsString();
1235 StringRef name(name_str.c_str());
1236 IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name));
1238 DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier));
1242 if (origin_property_decl.IsValid())
1244 DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context));
1245 if (parser_property_decl.IsValid())
1249 ASTDumper dumper((Decl*)parser_property_decl.decl);
1250 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1253 context.AddNamedDecl(parser_property_decl.decl);
1258 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier));
1260 if (origin_ivar_decl.IsValid())
1262 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context));
1263 if (parser_ivar_decl.IsValid())
1267 ASTDumper dumper((Decl*)parser_ivar_decl.decl);
1268 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1271 context.AddNamedDecl(parser_ivar_decl.decl);
1280 ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context)
1282 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1284 static unsigned int invocation_id = 0;
1285 unsigned int current_id = invocation_id++;
1287 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context));
1288 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer));
1290 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
1293 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'",
1294 current_id, static_cast<void*>(m_ast_context),
1295 parser_iface_decl->getNameAsString().c_str(),
1296 context.m_decl_name.getAsString().c_str());
1298 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1306 log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...",
1307 current_id, static_cast<const void*>(origin_iface_decl.decl),
1308 static_cast<void*>(&origin_iface_decl->getASTContext()));
1310 SymbolContext null_sc;
1315 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(parser_iface_decl.decl));
1317 if (!complete_interface_decl)
1320 // We found the complete interface. The runtime never needs to be queried in this scenario.
1322 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
1324 if (complete_iface_decl.decl == origin_iface_decl.decl)
1325 break; // already checked this one
1328 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1330 static_cast<const void*>(complete_iface_decl.decl),
1331 static_cast<void*>(&complete_iface_decl->getASTContext()));
1333 FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1337 complete_iface_decl);
1345 // Check the runtime only if the debug information didn't have a complete interface.
1347 lldb::ProcessSP process(m_target->GetProcessSP());
1352 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1354 if (!language_runtime)
1357 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1362 bool append = false;
1363 uint32_t max_matches = 1;
1364 std::vector <ClangASTType> types;
1366 if (!type_vendor->FindTypes(class_name,
1372 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1374 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1376 if (!runtime_interface_type)
1379 DeclFromUser<const ObjCInterfaceDecl> runtime_iface_decl(runtime_interface_type->getDecl());
1382 log->Printf("CAS::FOPD[%d] trying runtime (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1384 static_cast<const void*>(runtime_iface_decl.decl),
1385 static_cast<void*>(&runtime_iface_decl->getASTContext()));
1387 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1391 runtime_iface_decl))
1397 typedef llvm::DenseMap <const FieldDecl *, uint64_t> FieldOffsetMap;
1398 typedef llvm::DenseMap <const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1400 template <class D, class O>
1402 ImportOffsetMap (llvm::DenseMap <const D*, O> &destination_map,
1403 llvm::DenseMap <const D*, O> &source_map,
1404 ClangASTImporter *importer,
1405 ASTContext &dest_ctx)
1407 typedef llvm::DenseMap <const D*, O> MapType;
1409 for (typename MapType::iterator fi = source_map.begin(), fe = source_map.end();
1413 DeclFromUser <D> user_decl(const_cast<D*>(fi->first));
1414 DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx));
1415 if (parser_decl.IsInvalid())
1417 destination_map.insert(std::pair<const D *, O>(parser_decl.decl, fi->second));
1423 template <bool IsVirtual> bool ExtractBaseOffsets (const ASTRecordLayout &record_layout,
1424 DeclFromUser<const CXXRecordDecl> &record,
1425 BaseOffsetMap &base_offsets)
1427 for (CXXRecordDecl::base_class_const_iterator
1428 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
1429 be = (IsVirtual ? record->vbases_end() : record->bases_end());
1433 if (!IsVirtual && bi->isVirtual())
1436 const clang::Type *origin_base_type = bi->getType().getTypePtr();
1437 const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>();
1439 if (!origin_base_record_type)
1442 DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl());
1444 if (origin_base_record.IsInvalid())
1447 DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record));
1449 if (origin_base_cxx_record.IsInvalid())
1452 CharUnits base_offset;
1455 base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1457 base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1459 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset));
1466 ClangASTSource::layoutRecordType(const RecordDecl *record,
1468 uint64_t &alignment,
1469 FieldOffsetMap &field_offsets,
1470 BaseOffsetMap &base_offsets,
1471 BaseOffsetMap &virtual_base_offsets)
1473 ClangASTMetrics::RegisterRecordLayout();
1475 static unsigned int invocation_id = 0;
1476 unsigned int current_id = invocation_id++;
1478 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1481 log->Printf("LayoutRecordType[%u] on (ASTContext*)%p for (RecordDecl*)%p [name = '%s']",
1482 current_id, static_cast<void*>(m_ast_context),
1483 static_cast<const void*>(record),
1484 record->getNameAsString().c_str());
1486 DeclFromParser <const RecordDecl> parser_record(record);
1487 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer));
1489 if (origin_record.IsInvalid())
1492 FieldOffsetMap origin_field_offsets;
1493 BaseOffsetMap origin_base_offsets;
1494 BaseOffsetMap origin_virtual_base_offsets;
1496 ClangASTContext::GetCompleteDecl(&origin_record->getASTContext(), const_cast<RecordDecl*>(origin_record.decl));
1498 if (!origin_record.decl->getDefinition())
1501 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1503 int field_idx = 0, field_count = record_layout.getFieldCount();
1505 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end();
1509 if (field_idx >= field_count)
1510 return false; // Layout didn't go well. Bail out.
1512 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1514 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1519 ASTContext &parser_ast_context(record->getASTContext());
1521 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1523 if (origin_cxx_record.IsValid())
1525 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1526 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1530 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) ||
1531 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) ||
1532 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context))
1535 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1536 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
1540 log->Printf("LRT[%u] returned:", current_id);
1541 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id,
1542 static_cast<const void*>(origin_record.decl));
1543 log->Printf("LRT[%u] Size = %" PRId64, current_id, size);
1544 log->Printf("LRT[%u] Alignment = %" PRId64, current_id, alignment);
1545 log->Printf("LRT[%u] Fields:", current_id);
1546 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1550 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %" PRId64 " bits",
1551 current_id, static_cast<void*>(*fi),
1552 fi->getNameAsString().c_str(), field_offsets[*fi]);
1554 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1555 if (parser_cxx_record.IsValid())
1557 log->Printf("LRT[%u] Bases:", current_id);
1558 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1562 bool is_virtual = bi->isVirtual();
1564 QualType base_type = bi->getType();
1565 const RecordType *base_record_type = base_type->getAs<RecordType>();
1566 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1567 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
1569 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %" PRId64 " chars",
1570 current_id, (is_virtual ? "Virtual " : ""),
1571 static_cast<void*>(base_cxx_record.decl),
1572 base_cxx_record.decl->getNameAsString().c_str(),
1574 ? virtual_base_offsets[base_cxx_record.decl].getQuantity()
1575 : base_offsets[base_cxx_record.decl].getQuantity()));
1580 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1588 ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1589 const ConstString &name,
1590 ClangASTImporter::NamespaceMapSP &parent_map) const
1592 static unsigned int invocation_id = 0;
1593 unsigned int current_id = invocation_id++;
1595 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1599 if (parent_map && parent_map->size())
1600 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
1601 current_id, static_cast<void*>(m_ast_context),
1603 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
1605 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
1606 current_id, static_cast<void*>(m_ast_context),
1612 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1616 ClangNamespaceDecl found_namespace_decl;
1618 lldb::ModuleSP module_sp = i->first;
1619 ClangNamespaceDecl module_parent_namespace_decl = i->second;
1621 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
1626 SymbolContext null_sc;
1628 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
1630 if (!found_namespace_decl)
1633 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
1636 log->Printf(" CMN[%u] Found namespace %s in module %s",
1639 module_sp->GetFileSpec().GetFilename().GetCString());
1644 const ModuleList &target_images = m_target->GetImages();
1645 Mutex::Locker modules_locker(target_images.GetMutex());
1647 ClangNamespaceDecl null_namespace_decl;
1649 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
1651 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
1656 ClangNamespaceDecl found_namespace_decl;
1658 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
1663 SymbolContext null_sc;
1665 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
1667 if (!found_namespace_decl)
1670 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
1673 log->Printf(" CMN[%u] Found namespace %s in module %s",
1676 image->GetFileSpec().GetFilename().GetCString());
1682 ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1684 if (!namespace_decls)
1687 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
1689 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
1694 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1696 if (!copied_namespace_decl)
1699 context.m_decls.push_back(copied_namespace_decl);
1701 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1703 return dyn_cast<NamespaceDecl>(copied_decl);
1707 ClangASTSource::GuardedCopyType (const ClangASTType &src_type)
1709 ClangASTMetrics::RegisterLLDBImport();
1711 SetImportInProgress(true);
1713 QualType copied_qual_type = m_ast_importer->CopyType (m_ast_context, src_type.GetASTContext(), src_type.GetQualType());
1715 SetImportInProgress(false);
1717 if (copied_qual_type.getAsOpaquePtr() && copied_qual_type->getCanonicalTypeInternal().isNull())
1718 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1720 return ClangASTType();
1722 return ClangASTType(m_ast_context, copied_qual_type);
1726 NameSearchContext::AddVarDecl(const ClangASTType &type)
1728 assert (type && "Type for variable must be valid!");
1730 if (!type.IsValid())
1733 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
1735 clang::ASTContext *ast = type.GetASTContext();
1737 clang::NamedDecl *Decl = VarDecl::Create(*ast,
1738 const_cast<DeclContext*>(m_decl_context),
1745 m_decls.push_back(Decl);
1751 NameSearchContext::AddFunDecl (const ClangASTType &type)
1753 assert (type && "Type for variable must be valid!");
1755 if (!type.IsValid())
1758 if (m_function_types.count(type))
1761 m_function_types.insert(type);
1763 QualType qual_type (type.GetQualType());
1765 clang::ASTContext *ast = type.GetASTContext();
1767 const bool isInlineSpecified = false;
1768 const bool hasWrittenPrototype = true;
1769 const bool isConstexprSpecified = false;
1771 clang::FunctionDecl *func_decl = FunctionDecl::Create (*ast,
1772 const_cast<DeclContext*>(m_decl_context),
1775 m_decl_name.getAsIdentifierInfo(),
1780 hasWrittenPrototype,
1781 isConstexprSpecified);
1783 // We have to do more than just synthesize the FunctionDecl. We have to
1784 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1785 // this, we raid the function's FunctionProtoType for types.
1787 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
1789 if (func_proto_type)
1791 unsigned NumArgs = func_proto_type->getNumParams();
1794 SmallVector<ParmVarDecl *, 5> parm_var_decls;
1796 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1798 QualType arg_qual_type (func_proto_type->getParamType(ArgIndex));
1800 parm_var_decls.push_back(ParmVarDecl::Create (*ast,
1801 const_cast<DeclContext*>(m_decl_context),
1811 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
1815 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1818 log->Printf("Function type wasn't a FunctionProtoType");
1821 m_decls.push_back(func_decl);
1827 NameSearchContext::AddGenericFunDecl()
1829 FunctionProtoType::ExtProtoInfo proto_info;
1831 proto_info.Variadic = true;
1833 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
1834 ArrayRef<QualType>(), // argument types
1837 return AddFunDecl(ClangASTType (m_ast_source.m_ast_context, generic_function_type));
1841 NameSearchContext::AddTypeDecl(const ClangASTType &clang_type)
1845 QualType qual_type = clang_type.GetQualType();
1847 if (const TypedefType *typedef_type = llvm::dyn_cast<TypedefType>(qual_type))
1849 TypedefNameDecl *typedef_name_decl = typedef_type->getDecl();
1851 m_decls.push_back(typedef_name_decl);
1853 return (NamedDecl*)typedef_name_decl;
1855 else if (const TagType *tag_type = qual_type->getAs<TagType>())
1857 TagDecl *tag_decl = tag_type->getDecl();
1859 m_decls.push_back(tag_decl);
1863 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
1865 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
1867 m_decls.push_back((NamedDecl*)interface_decl);
1869 return (NamedDecl*)interface_decl;
1876 NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1878 for (clang::NamedDecl *decl : result)
1879 m_decls.push_back (decl);
1883 NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1885 m_decls.push_back (decl);