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",
183 tag_decl->getName().str().c_str());
185 log->Printf(" CTD[%u] Before:", current_id);
186 ASTDumper dumper((Decl*)tag_decl);
187 dumper.ToLog(log, " [CTD] ");
190 if (!m_ast_importer->CompleteTagDecl (tag_decl))
192 // We couldn't complete the type. Maybe there's a definition
193 // somewhere else that can be completed.
196 log->Printf(" CTD[%u] Type could not be completed in the module in which it was first found.", current_id);
200 DeclContext *decl_ctx = tag_decl->getDeclContext();
202 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(decl_ctx))
204 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
206 if (log && log->GetVerbose())
207 log->Printf(" CTD[%u] Inspecting namespace map %p (%d entries)",
210 (int)namespace_map->size());
215 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
220 log->Printf(" CTD[%u] Searching namespace %s in module %s",
222 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
223 i->first->GetFileSpec().GetFilename().GetCString());
227 SymbolContext null_sc;
228 ConstString name(tag_decl->getName().str().c_str());
230 i->first->FindTypesInNamespace(null_sc, name, &i->second, UINT32_MAX, types);
232 for (uint32_t ti = 0, te = types.GetSize();
236 lldb::TypeSP type = types.GetTypeAtIndex(ti);
241 ClangASTType clang_type (type->GetClangFullType());
246 const TagType *tag_type = clang_type.GetQualType()->getAs<TagType>();
251 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
253 if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
262 SymbolContext null_sc;
263 ConstString name(tag_decl->getName().str().c_str());
264 ClangNamespaceDecl namespace_decl;
266 const ModuleList &module_list = m_target->GetImages();
268 bool exact_match = false;
269 module_list.FindTypes (null_sc, name, exact_match, UINT32_MAX, types);
271 for (uint32_t ti = 0, te = types.GetSize();
275 lldb::TypeSP type = types.GetTypeAtIndex(ti);
280 ClangASTType clang_type (type->GetClangFullType());
285 const TagType *tag_type = clang_type.GetQualType()->getAs<TagType>();
290 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
292 if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
300 log->Printf(" [CTD] After:");
301 ASTDumper dumper((Decl*)tag_decl);
302 dumper.ToLog(log, " [CTD] ");
307 ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl)
309 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
313 log->Printf(" [CompleteObjCInterfaceDecl] on (ASTContext*)%p Completing an ObjCInterfaceDecl named %s", m_ast_context, interface_decl->getName().str().c_str());
314 log->Printf(" [COID] Before:");
315 ASTDumper dumper((Decl*)interface_decl);
316 dumper.ToLog(log, " [COID] ");
319 m_ast_importer->CompleteObjCInterfaceDecl (interface_decl);
323 log->Printf(" [COID] After:");
324 ASTDumper dumper((Decl*)interface_decl);
325 dumper.ToLog(log, " [COID] ");
329 clang::ObjCInterfaceDecl *
330 ClangASTSource::GetCompleteObjCInterface (clang::ObjCInterfaceDecl *interface_decl)
332 lldb::ProcessSP process(m_target->GetProcessSP());
337 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
339 if (!language_runtime)
342 ConstString class_name(interface_decl->getNameAsString().c_str());
344 lldb::TypeSP complete_type_sp(language_runtime->LookupInCompleteClassCache(class_name));
346 if (!complete_type_sp)
349 TypeFromUser complete_type = TypeFromUser(complete_type_sp->GetClangFullType());
350 lldb::clang_type_t complete_opaque_type = complete_type.GetOpaqueQualType();
352 if (!complete_opaque_type)
355 const clang::Type *complete_clang_type = QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
356 const ObjCInterfaceType *complete_interface_type = dyn_cast<ObjCInterfaceType>(complete_clang_type);
358 if (!complete_interface_type)
361 ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl());
363 return complete_iface_decl;
366 clang::ExternalLoadResult
367 ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
368 bool (*predicate)(Decl::Kind),
369 llvm::SmallVectorImpl<Decl*> &decls)
371 ClangASTMetrics::RegisterLexicalQuery();
373 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
375 const Decl *context_decl = dyn_cast<Decl>(decl_context);
380 static unsigned int invocation_id = 0;
381 unsigned int current_id = invocation_id++;
385 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
386 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate",
389 context_named_decl->getNameAsString().c_str(),
390 context_decl->getDeclKindName(),
392 (predicate ? "non-null" : "null"));
393 else if(context_decl)
394 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate",
397 context_decl->getDeclKindName(),
399 (predicate ? "non-null" : "null"));
401 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate",
404 (predicate ? "non-null" : "null"));
407 Decl *original_decl = NULL;
408 ASTContext *original_ctx = NULL;
410 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
415 log->Printf(" FELD[%u] Original decl (ASTContext*)%p (Decl*)%p:", current_id, original_ctx, original_decl);
416 ASTDumper(original_decl).ToLog(log, " ");
419 if (ObjCInterfaceDecl *original_iface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl))
421 ObjCInterfaceDecl *complete_iface_decl = GetCompleteObjCInterface(original_iface_decl);
423 if (complete_iface_decl && (complete_iface_decl != original_iface_decl))
425 original_decl = complete_iface_decl;
426 original_ctx = &complete_iface_decl->getASTContext();
428 m_ast_importer->SetDeclOrigin(context_decl, original_iface_decl);
432 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
434 ExternalASTSource *external_source = original_ctx->getExternalSource();
437 external_source->CompleteType (original_tag_decl);
440 const DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
442 if (!original_decl_context)
445 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
446 iter != original_decl_context->decls_end();
451 if (!predicate || predicate(decl->getKind()))
455 ASTDumper ast_dumper(decl);
456 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
457 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());
459 log->Printf(" FELD[%d] Adding lexical %sDecl %s", current_id, decl->getDeclKindName(), ast_dumper.GetCString());
462 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
467 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl))
469 QualType copied_field_type = copied_field->getType();
471 m_ast_importer->RequireCompleteType(copied_field_type);
474 decls.push_back(copied_decl);
476 DeclContext *decl_context_non_const = const_cast<DeclContext *>(decl_context);
478 if (copied_decl->getDeclContext() != decl_context)
480 if (copied_decl->getDeclContext()->containsDecl(copied_decl))
481 copied_decl->getDeclContext()->removeDecl(copied_decl);
482 copied_decl->setDeclContext(decl_context_non_const);
485 if (!decl_context_non_const->containsDecl(copied_decl))
486 decl_context_non_const->addDeclInternal(copied_decl);
490 return ELR_AlreadyLoaded;
494 ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
496 assert (m_ast_context);
498 ClangASTMetrics::RegisterVisibleQuery();
500 const ConstString name(context.m_decl_name.getAsString().c_str());
502 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
504 static unsigned int invocation_id = 0;
505 unsigned int current_id = invocation_id++;
509 if (!context.m_decl_context)
510 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a NULL DeclContext", current_id, m_ast_context, name.GetCString());
511 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
512 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in '%s'", current_id, m_ast_context, name.GetCString(), context_named_decl->getNameAsString().c_str());
514 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a '%s'", current_id, m_ast_context, name.GetCString(), context.m_decl_context->getDeclKindName());
517 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
519 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
521 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
523 if (log && log->GetVerbose())
524 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
527 (int)namespace_map->size());
532 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
537 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
539 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
540 i->first->GetFileSpec().GetFilename().GetCString());
542 FindExternalVisibleDecls(context,
548 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
550 FindObjCPropertyAndIvarDecls(context);
552 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
554 // we shouldn't be getting FindExternalVisibleDecls calls for these
559 ClangNamespaceDecl namespace_decl;
562 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
564 FindExternalVisibleDecls(context,
570 if (!context.m_namespace_map->empty())
572 if (log && log->GetVerbose())
573 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
575 context.m_namespace_map.get(),
576 (int)context.m_namespace_map->size());
578 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
580 if (clang_namespace_decl)
581 clang_namespace_decl->setHasExternalVisibleStorage();
586 ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
587 lldb::ModuleSP module_sp,
588 ClangNamespaceDecl &namespace_decl,
589 unsigned int current_id)
591 assert (m_ast_context);
593 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
595 SymbolContextList sc_list;
597 const ConstString name(context.m_decl_name.getAsString().c_str());
599 const char *name_unique_cstr = name.GetCString();
601 static ConstString id_name("id");
602 static ConstString Class_name("Class");
604 if (name == id_name || name == Class_name)
607 if (name_unique_cstr == NULL)
610 // The ClangASTSource is not responsible for finding $-names.
611 if (name_unique_cstr[0] == '$')
614 if (module_sp && namespace_decl)
616 ClangNamespaceDecl found_namespace_decl;
618 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
622 SymbolContext null_sc;
624 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
626 if (found_namespace_decl)
628 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
631 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
634 module_sp->GetFileSpec().GetFilename().GetCString());
640 const ModuleList &target_images = m_target->GetImages();
641 Mutex::Locker modules_locker (target_images.GetMutex());
643 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
645 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
650 ClangNamespaceDecl found_namespace_decl;
652 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
657 SymbolContext null_sc;
659 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
661 if (found_namespace_decl)
663 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
666 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
669 image->GetFileSpec().GetFilename().GetCString());
677 SymbolContext null_sc;
678 const bool exact_match = false;
680 if (module_sp && namespace_decl)
681 module_sp->FindTypesInNamespace(null_sc, name, &namespace_decl, 1, types);
683 m_target->GetImages().FindTypes(null_sc, name, exact_match, 1, types);
687 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
691 const char *name_string = type_sp->GetName().GetCString();
693 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
696 (name_string ? name_string : "<anonymous>"));
699 ClangASTType full_type = type_sp->GetClangFullType();
701 ClangASTType copied_clang_type (GuardedCopyType(full_type));
703 if (!copied_clang_type)
706 log->Printf(" CAS::FEVD[%u] - Couldn't export a type",
712 context.AddTypeDecl(copied_clang_type);
718 // Couldn't find any types elsewhere. Try the Objective-C runtime if one exists.
720 lldb::ProcessSP process(m_target->GetProcessSP());
725 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
727 if (!language_runtime)
730 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
736 uint32_t max_matches = 1;
737 std::vector <ClangASTType> types;
739 if (!type_vendor->FindTypes(name,
747 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\" in the runtime",
752 ClangASTType copied_clang_type (GuardedCopyType(types[0]));
754 if (!copied_clang_type)
757 log->Printf(" CAS::FEVD[%u] - Couldn't export a type from the runtime",
763 context.AddTypeDecl(copied_clang_type);
771 template <class D> class TaggedASTDecl {
773 TaggedASTDecl() : decl(NULL) { }
774 TaggedASTDecl(D *_decl) : decl(_decl) { }
775 bool IsValid() const { return (decl != NULL); }
776 bool IsInvalid() const { return !IsValid(); }
777 D *operator->() const { return decl; }
781 template <class D2, template <class D> class TD, class D1>
783 DynCast(TD<D1> source)
785 return TD<D2> (dyn_cast<D2>(source.decl));
788 template <class D = Decl> class DeclFromParser;
789 template <class D = Decl> class DeclFromUser;
791 template <class D> class DeclFromParser : public TaggedASTDecl<D> {
793 DeclFromParser() : TaggedASTDecl<D>() { }
794 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { }
796 DeclFromUser<D> GetOrigin(ClangASTImporter *importer);
799 template <class D> class DeclFromUser : public TaggedASTDecl<D> {
801 DeclFromUser() : TaggedASTDecl<D>() { }
802 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { }
804 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx);
809 DeclFromParser<D>::GetOrigin(ClangASTImporter *importer)
811 DeclFromUser <> origin_decl;
812 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
813 if (origin_decl.IsInvalid())
814 return DeclFromUser<D>();
815 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
820 DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx)
822 DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl));
823 if (parser_generic_decl.IsInvalid())
824 return DeclFromParser<D>();
825 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
829 FindObjCMethodDeclsWithOrigin (unsigned int current_id,
830 NameSearchContext &context,
831 ObjCInterfaceDecl *original_interface_decl,
832 clang::ASTContext *ast_context,
833 ClangASTImporter *ast_importer,
834 const char *log_info)
836 const DeclarationName &decl_name(context.m_decl_name);
837 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();
839 Selector original_selector;
841 if (decl_name.isObjCZeroArgSelector())
843 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString());
844 original_selector = original_ctx->Selectors.getSelector(0, &ident);
846 else if (decl_name.isObjCOneArgSelector())
848 const std::string &decl_name_string = decl_name.getAsString();
849 std::string decl_name_string_without_colon(decl_name_string.c_str(), decl_name_string.length() - 1);
850 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name_string_without_colon.c_str());
851 original_selector = original_ctx->Selectors.getSelector(1, &ident);
855 SmallVector<IdentifierInfo *, 4> idents;
857 clang::Selector sel = decl_name.getObjCSelector();
859 unsigned num_args = sel.getNumArgs();
865 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
868 original_selector = original_ctx->Selectors.getSelector(num_args, idents.data());
871 DeclarationName original_decl_name(original_selector);
873 ObjCInterfaceDecl::lookup_result result = original_interface_decl->lookup(original_decl_name);
881 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(result[0]);
886 Decl *copied_decl = ast_importer->CopyDecl(ast_context, &result_method->getASTContext(), result_method);
891 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
893 if (!copied_method_decl)
896 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
900 ASTDumper dumper((Decl*)copied_method_decl);
901 log->Printf(" CAS::FOMD[%d] found (%s) %s", current_id, log_info, dumper.GetCString());
904 context.AddNamedDecl(copied_method_decl);
910 ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
912 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
914 static unsigned int invocation_id = 0;
915 unsigned int current_id = invocation_id++;
917 const DeclarationName &decl_name(context.m_decl_name);
918 const DeclContext *decl_ctx(context.m_decl_context);
920 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
927 Decl *original_decl = NULL;
928 ASTContext *original_ctx = NULL;
930 m_ast_importer->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx);
935 ObjCInterfaceDecl *original_interface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl);
937 if (FindObjCMethodDeclsWithOrigin(current_id,
939 original_interface_decl,
943 return; // found it, no need to look any further
948 if (decl_name.isObjCZeroArgSelector())
950 ss.Printf("%s", decl_name.getAsString().c_str());
952 else if (decl_name.isObjCOneArgSelector())
954 ss.Printf("%s", decl_name.getAsString().c_str());
958 clang::Selector sel = decl_name.getObjCSelector();
960 for (unsigned i = 0, e = sel.getNumArgs();
964 llvm::StringRef r = sel.getNameForSlot(i);
965 ss.Printf("%s:", r.str().c_str());
970 ConstString selector_name(ss.GetData());
973 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
976 interface_decl->getNameAsString().c_str(),
977 selector_name.AsCString());
978 SymbolContextList sc_list;
980 const bool include_symbols = false;
981 const bool include_inlines = false;
982 const bool append = false;
984 std::string interface_name = interface_decl->getNameAsString();
989 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
991 ConstString instance_method_name(ms.GetData());
993 m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
995 if (sc_list.GetSize())
999 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
1001 ConstString class_method_name(ms.GetData());
1003 m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
1005 if (sc_list.GetSize())
1008 // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in
1009 // categories on the desired class.
1011 SymbolContextList candidate_sc_list;
1013 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list);
1015 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize();
1019 SymbolContext candidate_sc;
1021 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
1024 if (!candidate_sc.function)
1027 const char *candidate_name = candidate_sc.function->GetName().AsCString();
1029 const char *cursor = candidate_name;
1031 if (*cursor != '+' && *cursor != '-')
1041 size_t interface_len = interface_name.length();
1043 if (strncmp(cursor, interface_name.c_str(), interface_len))
1046 cursor += interface_len;
1048 if (*cursor == ' ' || *cursor == '(')
1049 sc_list.Append(candidate_sc);
1054 if (sc_list.GetSize())
1056 // We found a good function symbol. Use that.
1058 for (uint32_t i = 0, e = sc_list.GetSize();
1064 if (!sc_list.GetContextAtIndex(i, sc))
1070 DeclContext *function_ctx = sc.function->GetClangDeclContext();
1075 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
1080 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
1082 if (!found_interface_decl)
1085 if (found_interface_decl->getName() == interface_decl->getName())
1087 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
1092 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
1094 if (!copied_method_decl)
1099 ASTDumper dumper((Decl*)copied_method_decl);
1100 log->Printf(" CAS::FOMD[%d] found (in symbols) %s", current_id, dumper.GetCString());
1103 context.AddNamedDecl(copied_method_decl);
1110 // Try the debug information.
1114 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(interface_decl));
1116 if (!complete_interface_decl)
1119 // We found the complete interface. The runtime never needs to be queried in this scenario.
1121 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
1123 if (complete_interface_decl == interface_decl)
1124 break; // already checked this one
1127 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1129 complete_interface_decl,
1130 &complete_iface_decl->getASTContext());
1132 FindObjCMethodDeclsWithOrigin(current_id,
1134 complete_interface_decl,
1145 // Check the runtime only if the debug information didn't have a complete interface.
1147 lldb::ProcessSP process(m_target->GetProcessSP());
1152 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1154 if (!language_runtime)
1157 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1162 ConstString interface_name(interface_decl->getNameAsString().c_str());
1163 bool append = false;
1164 uint32_t max_matches = 1;
1165 std::vector <ClangASTType> types;
1167 if (!type_vendor->FindTypes(interface_name,
1173 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1175 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1177 if (!runtime_interface_type)
1180 ObjCInterfaceDecl *runtime_interface_decl = runtime_interface_type->getDecl();
1182 FindObjCMethodDeclsWithOrigin(current_id,
1184 runtime_interface_decl,
1193 FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id,
1194 NameSearchContext &context,
1195 clang::ASTContext &ast_context,
1196 ClangASTImporter *ast_importer,
1197 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl)
1199 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1201 if (origin_iface_decl.IsInvalid())
1204 std::string name_str = context.m_decl_name.getAsString();
1205 StringRef name(name_str.c_str());
1206 IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name));
1208 DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier));
1212 if (origin_property_decl.IsValid())
1214 DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context));
1215 if (parser_property_decl.IsValid())
1219 ASTDumper dumper((Decl*)parser_property_decl.decl);
1220 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1223 context.AddNamedDecl(parser_property_decl.decl);
1228 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier));
1230 if (origin_ivar_decl.IsValid())
1232 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context));
1233 if (parser_ivar_decl.IsValid())
1237 ASTDumper dumper((Decl*)parser_ivar_decl.decl);
1238 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1241 context.AddNamedDecl(parser_ivar_decl.decl);
1250 ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context)
1252 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1254 static unsigned int invocation_id = 0;
1255 unsigned int current_id = invocation_id++;
1257 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context));
1258 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer));
1260 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
1263 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'",
1266 parser_iface_decl->getNameAsString().c_str(),
1267 context.m_decl_name.getAsString().c_str());
1269 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1277 log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...",
1279 origin_iface_decl.decl,
1280 &origin_iface_decl->getASTContext());
1282 SymbolContext null_sc;
1287 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(parser_iface_decl.decl));
1289 if (!complete_interface_decl)
1292 // We found the complete interface. The runtime never needs to be queried in this scenario.
1294 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
1296 if (complete_iface_decl.decl == origin_iface_decl.decl)
1297 break; // already checked this one
1300 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1302 complete_iface_decl.decl,
1303 &complete_iface_decl->getASTContext());
1305 FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1309 complete_iface_decl);
1317 // Check the runtime only if the debug information didn't have a complete interface.
1319 lldb::ProcessSP process(m_target->GetProcessSP());
1324 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1326 if (!language_runtime)
1329 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1334 bool append = false;
1335 uint32_t max_matches = 1;
1336 std::vector <ClangASTType> types;
1338 if (!type_vendor->FindTypes(class_name,
1344 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1346 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1348 if (!runtime_interface_type)
1351 DeclFromUser<const ObjCInterfaceDecl> runtime_iface_decl(runtime_interface_type->getDecl());
1354 log->Printf("CAS::FOPD[%d] trying runtime (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1356 runtime_iface_decl.decl,
1357 &runtime_iface_decl->getASTContext());
1359 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1363 runtime_iface_decl))
1369 typedef llvm::DenseMap <const FieldDecl *, uint64_t> FieldOffsetMap;
1370 typedef llvm::DenseMap <const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1372 template <class D, class O>
1374 ImportOffsetMap (llvm::DenseMap <const D*, O> &destination_map,
1375 llvm::DenseMap <const D*, O> &source_map,
1376 ClangASTImporter *importer,
1377 ASTContext &dest_ctx)
1379 typedef llvm::DenseMap <const D*, O> MapType;
1381 for (typename MapType::iterator fi = source_map.begin(), fe = source_map.end();
1385 DeclFromUser <D> user_decl(const_cast<D*>(fi->first));
1386 DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx));
1387 if (parser_decl.IsInvalid())
1389 destination_map.insert(std::pair<const D *, O>(parser_decl.decl, fi->second));
1395 template <bool IsVirtual> bool ExtractBaseOffsets (const ASTRecordLayout &record_layout,
1396 DeclFromUser<const CXXRecordDecl> &record,
1397 BaseOffsetMap &base_offsets)
1399 for (CXXRecordDecl::base_class_const_iterator
1400 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
1401 be = (IsVirtual ? record->vbases_end() : record->bases_end());
1405 if (!IsVirtual && bi->isVirtual())
1408 const clang::Type *origin_base_type = bi->getType().getTypePtr();
1409 const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>();
1411 if (!origin_base_record_type)
1414 DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl());
1416 if (origin_base_record.IsInvalid())
1419 DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record));
1421 if (origin_base_cxx_record.IsInvalid())
1424 CharUnits base_offset;
1427 base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1429 base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1431 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset));
1438 ClangASTSource::layoutRecordType(const RecordDecl *record,
1440 uint64_t &alignment,
1441 FieldOffsetMap &field_offsets,
1442 BaseOffsetMap &base_offsets,
1443 BaseOffsetMap &virtual_base_offsets)
1445 ClangASTMetrics::RegisterRecordLayout();
1447 static unsigned int invocation_id = 0;
1448 unsigned int current_id = invocation_id++;
1450 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1454 log->Printf("LayoutRecordType[%u] on (ASTContext*)%p for (RecordDecl*)%p [name = '%s']",
1458 record->getNameAsString().c_str());
1462 DeclFromParser <const RecordDecl> parser_record(record);
1463 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer));
1465 if (origin_record.IsInvalid())
1468 FieldOffsetMap origin_field_offsets;
1469 BaseOffsetMap origin_base_offsets;
1470 BaseOffsetMap origin_virtual_base_offsets;
1472 ClangASTContext::GetCompleteDecl(&origin_record->getASTContext(), const_cast<RecordDecl*>(origin_record.decl));
1474 if (!origin_record.decl->getDefinition())
1477 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1479 int field_idx = 0, field_count = record_layout.getFieldCount();
1481 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end();
1485 if (field_idx >= field_count)
1486 return false; // Layout didn't go well. Bail out.
1488 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1490 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1495 ASTContext &parser_ast_context(record->getASTContext());
1497 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1499 if (origin_cxx_record.IsValid())
1501 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1502 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1506 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) ||
1507 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) ||
1508 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context))
1511 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1512 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
1516 log->Printf("LRT[%u] returned:", current_id);
1517 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id, origin_record.decl);
1518 log->Printf("LRT[%u] Size = %" PRId64, current_id, size);
1519 log->Printf("LRT[%u] Alignment = %" PRId64, current_id, alignment);
1520 log->Printf("LRT[%u] Fields:", current_id);
1521 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1525 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %" PRId64 " bits",
1528 fi->getNameAsString().c_str(),
1529 field_offsets[*fi]);
1531 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1532 if (parser_cxx_record.IsValid())
1534 log->Printf("LRT[%u] Bases:", current_id);
1535 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1539 bool is_virtual = bi->isVirtual();
1541 QualType base_type = bi->getType();
1542 const RecordType *base_record_type = base_type->getAs<RecordType>();
1543 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1544 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
1546 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %" PRId64 " chars",
1548 (is_virtual ? "Virtual " : ""),
1549 base_cxx_record.decl,
1550 base_cxx_record.decl->getNameAsString().c_str(),
1551 (is_virtual ? virtual_base_offsets[base_cxx_record.decl].getQuantity() :
1552 base_offsets[base_cxx_record.decl].getQuantity()));
1557 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1565 ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1566 const ConstString &name,
1567 ClangASTImporter::NamespaceMapSP &parent_map) const
1569 static unsigned int invocation_id = 0;
1570 unsigned int current_id = invocation_id++;
1572 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1576 if (parent_map && parent_map->size())
1577 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
1581 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
1583 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
1592 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1596 ClangNamespaceDecl found_namespace_decl;
1598 lldb::ModuleSP module_sp = i->first;
1599 ClangNamespaceDecl module_parent_namespace_decl = i->second;
1601 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
1606 SymbolContext null_sc;
1608 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
1610 if (!found_namespace_decl)
1613 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
1616 log->Printf(" CMN[%u] Found namespace %s in module %s",
1619 module_sp->GetFileSpec().GetFilename().GetCString());
1624 const ModuleList &target_images = m_target->GetImages();
1625 Mutex::Locker modules_locker(target_images.GetMutex());
1627 ClangNamespaceDecl null_namespace_decl;
1629 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
1631 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
1636 ClangNamespaceDecl found_namespace_decl;
1638 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
1643 SymbolContext null_sc;
1645 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
1647 if (!found_namespace_decl)
1650 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
1653 log->Printf(" CMN[%u] Found namespace %s in module %s",
1656 image->GetFileSpec().GetFilename().GetCString());
1662 ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1664 if (!namespace_decls)
1667 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
1669 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
1674 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1676 if (!copied_namespace_decl)
1679 context.m_decls.push_back(copied_namespace_decl);
1681 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1683 return dyn_cast<NamespaceDecl>(copied_decl);
1687 ClangASTSource::GuardedCopyType (const ClangASTType &src_type)
1689 ClangASTMetrics::RegisterLLDBImport();
1691 SetImportInProgress(true);
1693 QualType copied_qual_type = m_ast_importer->CopyType (m_ast_context, src_type.GetASTContext(), src_type.GetQualType());
1695 SetImportInProgress(false);
1697 if (copied_qual_type.getAsOpaquePtr() && copied_qual_type->getCanonicalTypeInternal().isNull())
1698 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1700 return ClangASTType();
1702 return ClangASTType(m_ast_context, copied_qual_type);
1706 NameSearchContext::AddVarDecl(const ClangASTType &type)
1708 assert (type && "Type for variable must be valid!");
1710 if (!type.IsValid())
1713 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
1715 clang::ASTContext *ast = type.GetASTContext();
1717 clang::NamedDecl *Decl = VarDecl::Create(*ast,
1718 const_cast<DeclContext*>(m_decl_context),
1725 m_decls.push_back(Decl);
1731 NameSearchContext::AddFunDecl (const ClangASTType &type)
1733 assert (type && "Type for variable must be valid!");
1735 if (!type.IsValid())
1738 if (m_function_types.count(type))
1741 m_function_types.insert(type);
1743 QualType qual_type (type.GetQualType());
1745 clang::ASTContext *ast = type.GetASTContext();
1747 const bool isInlineSpecified = false;
1748 const bool hasWrittenPrototype = true;
1749 const bool isConstexprSpecified = false;
1751 clang::FunctionDecl *func_decl = FunctionDecl::Create (*ast,
1752 const_cast<DeclContext*>(m_decl_context),
1755 m_decl_name.getAsIdentifierInfo(),
1760 hasWrittenPrototype,
1761 isConstexprSpecified);
1763 // We have to do more than just synthesize the FunctionDecl. We have to
1764 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1765 // this, we raid the function's FunctionProtoType for types.
1767 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
1769 if (func_proto_type)
1771 unsigned NumArgs = func_proto_type->getNumArgs();
1774 SmallVector<ParmVarDecl *, 5> parm_var_decls;
1776 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1778 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
1780 parm_var_decls.push_back(ParmVarDecl::Create (*ast,
1781 const_cast<DeclContext*>(m_decl_context),
1791 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
1795 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1798 log->Printf("Function type wasn't a FunctionProtoType");
1801 m_decls.push_back(func_decl);
1807 NameSearchContext::AddGenericFunDecl()
1809 FunctionProtoType::ExtProtoInfo proto_info;
1811 proto_info.Variadic = true;
1813 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
1814 ArrayRef<QualType>(), // argument types
1817 return AddFunDecl(ClangASTType (m_ast_source.m_ast_context, generic_function_type));
1821 NameSearchContext::AddTypeDecl(const ClangASTType &clang_type)
1825 QualType qual_type = clang_type.GetQualType();
1827 if (const TypedefType *typedef_type = llvm::dyn_cast<TypedefType>(qual_type))
1829 TypedefNameDecl *typedef_name_decl = typedef_type->getDecl();
1831 m_decls.push_back(typedef_name_decl);
1833 return (NamedDecl*)typedef_name_decl;
1835 else if (const TagType *tag_type = qual_type->getAs<TagType>())
1837 TagDecl *tag_decl = tag_type->getDecl();
1839 m_decls.push_back(tag_decl);
1843 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
1845 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
1847 m_decls.push_back((NamedDecl*)interface_decl);
1849 return (NamedDecl*)interface_decl;
1856 NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1858 for (clang::NamedDecl *decl : result)
1859 m_decls.push_back (decl);
1863 NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1865 m_decls.push_back (decl);