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);
321 if (interface_decl->getSuperClass() &&
322 interface_decl->getSuperClass() != interface_decl)
323 CompleteType(interface_decl->getSuperClass());
327 log->Printf(" [COID] After:");
328 ASTDumper dumper((Decl*)interface_decl);
329 dumper.ToLog(log, " [COID] ");
333 clang::ObjCInterfaceDecl *
334 ClangASTSource::GetCompleteObjCInterface (clang::ObjCInterfaceDecl *interface_decl)
336 lldb::ProcessSP process(m_target->GetProcessSP());
341 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
343 if (!language_runtime)
346 ConstString class_name(interface_decl->getNameAsString().c_str());
348 lldb::TypeSP complete_type_sp(language_runtime->LookupInCompleteClassCache(class_name));
350 if (!complete_type_sp)
353 TypeFromUser complete_type = TypeFromUser(complete_type_sp->GetClangFullType());
354 lldb::clang_type_t complete_opaque_type = complete_type.GetOpaqueQualType();
356 if (!complete_opaque_type)
359 const clang::Type *complete_clang_type = QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
360 const ObjCInterfaceType *complete_interface_type = dyn_cast<ObjCInterfaceType>(complete_clang_type);
362 if (!complete_interface_type)
365 ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl());
367 return complete_iface_decl;
370 clang::ExternalLoadResult
371 ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
372 bool (*predicate)(Decl::Kind),
373 llvm::SmallVectorImpl<Decl*> &decls)
375 ClangASTMetrics::RegisterLexicalQuery();
377 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
379 const Decl *context_decl = dyn_cast<Decl>(decl_context);
384 static unsigned int invocation_id = 0;
385 unsigned int current_id = invocation_id++;
389 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
390 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate",
393 context_named_decl->getNameAsString().c_str(),
394 context_decl->getDeclKindName(),
396 (predicate ? "non-null" : "null"));
397 else if(context_decl)
398 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate",
401 context_decl->getDeclKindName(),
403 (predicate ? "non-null" : "null"));
405 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate",
408 (predicate ? "non-null" : "null"));
411 Decl *original_decl = NULL;
412 ASTContext *original_ctx = NULL;
414 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
419 log->Printf(" FELD[%u] Original decl (ASTContext*)%p (Decl*)%p:", current_id, original_ctx, original_decl);
420 ASTDumper(original_decl).ToLog(log, " ");
423 if (ObjCInterfaceDecl *original_iface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl))
425 ObjCInterfaceDecl *complete_iface_decl = GetCompleteObjCInterface(original_iface_decl);
427 if (complete_iface_decl && (complete_iface_decl != original_iface_decl))
429 original_decl = complete_iface_decl;
430 original_ctx = &complete_iface_decl->getASTContext();
432 m_ast_importer->SetDeclOrigin(context_decl, original_iface_decl);
436 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
438 ExternalASTSource *external_source = original_ctx->getExternalSource();
441 external_source->CompleteType (original_tag_decl);
444 const DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
446 if (!original_decl_context)
449 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
450 iter != original_decl_context->decls_end();
455 if (!predicate || predicate(decl->getKind()))
459 ASTDumper ast_dumper(decl);
460 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
461 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());
463 log->Printf(" FELD[%d] Adding lexical %sDecl %s", current_id, decl->getDeclKindName(), ast_dumper.GetCString());
466 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
471 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl))
473 QualType copied_field_type = copied_field->getType();
475 m_ast_importer->RequireCompleteType(copied_field_type);
478 decls.push_back(copied_decl);
480 DeclContext *decl_context_non_const = const_cast<DeclContext *>(decl_context);
482 if (copied_decl->getDeclContext() != decl_context)
484 if (copied_decl->getDeclContext()->containsDecl(copied_decl))
485 copied_decl->getDeclContext()->removeDecl(copied_decl);
486 copied_decl->setDeclContext(decl_context_non_const);
489 if (!decl_context_non_const->containsDecl(copied_decl))
490 decl_context_non_const->addDeclInternal(copied_decl);
494 return ELR_AlreadyLoaded;
498 ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
500 assert (m_ast_context);
502 ClangASTMetrics::RegisterVisibleQuery();
504 const ConstString name(context.m_decl_name.getAsString().c_str());
506 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
508 static unsigned int invocation_id = 0;
509 unsigned int current_id = invocation_id++;
513 if (!context.m_decl_context)
514 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a NULL DeclContext", current_id, m_ast_context, name.GetCString());
515 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
516 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());
518 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());
521 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
523 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
525 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
527 if (log && log->GetVerbose())
528 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
531 (int)namespace_map->size());
536 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
541 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
543 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
544 i->first->GetFileSpec().GetFilename().GetCString());
546 FindExternalVisibleDecls(context,
552 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
554 FindObjCPropertyAndIvarDecls(context);
556 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
558 // we shouldn't be getting FindExternalVisibleDecls calls for these
563 ClangNamespaceDecl namespace_decl;
566 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
568 FindExternalVisibleDecls(context,
574 if (!context.m_namespace_map->empty())
576 if (log && log->GetVerbose())
577 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
579 context.m_namespace_map.get(),
580 (int)context.m_namespace_map->size());
582 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
584 if (clang_namespace_decl)
585 clang_namespace_decl->setHasExternalVisibleStorage();
590 ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
591 lldb::ModuleSP module_sp,
592 ClangNamespaceDecl &namespace_decl,
593 unsigned int current_id)
595 assert (m_ast_context);
597 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
599 SymbolContextList sc_list;
601 const ConstString name(context.m_decl_name.getAsString().c_str());
603 const char *name_unique_cstr = name.GetCString();
605 static ConstString id_name("id");
606 static ConstString Class_name("Class");
608 if (name == id_name || name == Class_name)
611 if (name_unique_cstr == NULL)
614 // The ClangASTSource is not responsible for finding $-names.
615 if (name_unique_cstr[0] == '$')
618 if (module_sp && namespace_decl)
620 ClangNamespaceDecl found_namespace_decl;
622 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
626 SymbolContext null_sc;
628 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
630 if (found_namespace_decl)
632 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
635 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
638 module_sp->GetFileSpec().GetFilename().GetCString());
644 const ModuleList &target_images = m_target->GetImages();
645 Mutex::Locker modules_locker (target_images.GetMutex());
647 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
649 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
654 ClangNamespaceDecl found_namespace_decl;
656 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
661 SymbolContext null_sc;
663 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
665 if (found_namespace_decl)
667 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
670 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
673 image->GetFileSpec().GetFilename().GetCString());
681 SymbolContext null_sc;
682 const bool exact_match = false;
684 if (module_sp && namespace_decl)
685 module_sp->FindTypesInNamespace(null_sc, name, &namespace_decl, 1, types);
687 m_target->GetImages().FindTypes(null_sc, name, exact_match, 1, types);
691 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
695 const char *name_string = type_sp->GetName().GetCString();
697 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
700 (name_string ? name_string : "<anonymous>"));
703 ClangASTType full_type = type_sp->GetClangFullType();
705 ClangASTType copied_clang_type (GuardedCopyType(full_type));
707 if (!copied_clang_type)
710 log->Printf(" CAS::FEVD[%u] - Couldn't export a type",
716 context.AddTypeDecl(copied_clang_type);
722 // Couldn't find any types elsewhere. Try the Objective-C runtime if one exists.
724 lldb::ProcessSP process(m_target->GetProcessSP());
729 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
731 if (!language_runtime)
734 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
740 uint32_t max_matches = 1;
741 std::vector <ClangASTType> types;
743 if (!type_vendor->FindTypes(name,
751 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\" in the runtime",
756 ClangASTType copied_clang_type (GuardedCopyType(types[0]));
758 if (!copied_clang_type)
761 log->Printf(" CAS::FEVD[%u] - Couldn't export a type from the runtime",
767 context.AddTypeDecl(copied_clang_type);
775 template <class D> class TaggedASTDecl {
777 TaggedASTDecl() : decl(NULL) { }
778 TaggedASTDecl(D *_decl) : decl(_decl) { }
779 bool IsValid() const { return (decl != NULL); }
780 bool IsInvalid() const { return !IsValid(); }
781 D *operator->() const { return decl; }
785 template <class D2, template <class D> class TD, class D1>
787 DynCast(TD<D1> source)
789 return TD<D2> (dyn_cast<D2>(source.decl));
792 template <class D = Decl> class DeclFromParser;
793 template <class D = Decl> class DeclFromUser;
795 template <class D> class DeclFromParser : public TaggedASTDecl<D> {
797 DeclFromParser() : TaggedASTDecl<D>() { }
798 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { }
800 DeclFromUser<D> GetOrigin(ClangASTImporter *importer);
803 template <class D> class DeclFromUser : public TaggedASTDecl<D> {
805 DeclFromUser() : TaggedASTDecl<D>() { }
806 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { }
808 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx);
813 DeclFromParser<D>::GetOrigin(ClangASTImporter *importer)
815 DeclFromUser <> origin_decl;
816 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
817 if (origin_decl.IsInvalid())
818 return DeclFromUser<D>();
819 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
824 DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx)
826 DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl));
827 if (parser_generic_decl.IsInvalid())
828 return DeclFromParser<D>();
829 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
833 FindObjCMethodDeclsWithOrigin (unsigned int current_id,
834 NameSearchContext &context,
835 ObjCInterfaceDecl *original_interface_decl,
836 clang::ASTContext *ast_context,
837 ClangASTImporter *ast_importer,
838 const char *log_info)
840 const DeclarationName &decl_name(context.m_decl_name);
841 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();
843 Selector original_selector;
845 if (decl_name.isObjCZeroArgSelector())
847 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString());
848 original_selector = original_ctx->Selectors.getSelector(0, &ident);
850 else if (decl_name.isObjCOneArgSelector())
852 const std::string &decl_name_string = decl_name.getAsString();
853 std::string decl_name_string_without_colon(decl_name_string.c_str(), decl_name_string.length() - 1);
854 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name_string_without_colon.c_str());
855 original_selector = original_ctx->Selectors.getSelector(1, &ident);
859 SmallVector<IdentifierInfo *, 4> idents;
861 clang::Selector sel = decl_name.getObjCSelector();
863 unsigned num_args = sel.getNumArgs();
869 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
872 original_selector = original_ctx->Selectors.getSelector(num_args, idents.data());
875 DeclarationName original_decl_name(original_selector);
877 ObjCInterfaceDecl::lookup_result result = original_interface_decl->lookup(original_decl_name);
885 for (NamedDecl *named_decl : result)
887 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(named_decl);
892 Decl *copied_decl = ast_importer->CopyDecl(ast_context, &result_method->getASTContext(), result_method);
897 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
899 if (!copied_method_decl)
902 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
906 ASTDumper dumper((Decl*)copied_method_decl);
907 log->Printf(" CAS::FOMD[%d] found (%s) %s", current_id, log_info, dumper.GetCString());
910 context.AddNamedDecl(copied_method_decl);
917 ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
919 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
921 static unsigned int invocation_id = 0;
922 unsigned int current_id = invocation_id++;
924 const DeclarationName &decl_name(context.m_decl_name);
925 const DeclContext *decl_ctx(context.m_decl_context);
927 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
934 Decl *original_decl = NULL;
935 ASTContext *original_ctx = NULL;
937 m_ast_importer->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx);
942 ObjCInterfaceDecl *original_interface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl);
944 if (FindObjCMethodDeclsWithOrigin(current_id,
946 original_interface_decl,
950 return; // found it, no need to look any further
955 if (decl_name.isObjCZeroArgSelector())
957 ss.Printf("%s", decl_name.getAsString().c_str());
959 else if (decl_name.isObjCOneArgSelector())
961 ss.Printf("%s", decl_name.getAsString().c_str());
965 clang::Selector sel = decl_name.getObjCSelector();
967 for (unsigned i = 0, e = sel.getNumArgs();
971 llvm::StringRef r = sel.getNameForSlot(i);
972 ss.Printf("%s:", r.str().c_str());
977 if (strstr(ss.GetData(), "$__lldb"))
978 return; // we don't need any results
980 ConstString selector_name(ss.GetData());
983 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
986 interface_decl->getNameAsString().c_str(),
987 selector_name.AsCString());
988 SymbolContextList sc_list;
990 const bool include_symbols = false;
991 const bool include_inlines = false;
992 const bool append = false;
994 std::string interface_name = interface_decl->getNameAsString();
999 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
1001 ConstString instance_method_name(ms.GetData());
1003 m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
1005 if (sc_list.GetSize())
1009 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
1011 ConstString class_method_name(ms.GetData());
1013 m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
1015 if (sc_list.GetSize())
1018 // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in
1019 // categories on the desired class.
1021 SymbolContextList candidate_sc_list;
1023 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list);
1025 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize();
1029 SymbolContext candidate_sc;
1031 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
1034 if (!candidate_sc.function)
1037 const char *candidate_name = candidate_sc.function->GetName().AsCString();
1039 const char *cursor = candidate_name;
1041 if (*cursor != '+' && *cursor != '-')
1051 size_t interface_len = interface_name.length();
1053 if (strncmp(cursor, interface_name.c_str(), interface_len))
1056 cursor += interface_len;
1058 if (*cursor == ' ' || *cursor == '(')
1059 sc_list.Append(candidate_sc);
1064 if (sc_list.GetSize())
1066 // We found a good function symbol. Use that.
1068 for (uint32_t i = 0, e = sc_list.GetSize();
1074 if (!sc_list.GetContextAtIndex(i, sc))
1080 DeclContext *function_ctx = sc.function->GetClangDeclContext();
1085 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
1090 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
1092 if (!found_interface_decl)
1095 if (found_interface_decl->getName() == interface_decl->getName())
1097 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
1102 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
1104 if (!copied_method_decl)
1109 ASTDumper dumper((Decl*)copied_method_decl);
1110 log->Printf(" CAS::FOMD[%d] found (in symbols) %s", current_id, dumper.GetCString());
1113 context.AddNamedDecl(copied_method_decl);
1120 // Try the debug information.
1124 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(interface_decl));
1126 if (!complete_interface_decl)
1129 // We found the complete interface. The runtime never needs to be queried in this scenario.
1131 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
1133 if (complete_interface_decl == interface_decl)
1134 break; // already checked this one
1137 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1139 complete_interface_decl,
1140 &complete_iface_decl->getASTContext());
1142 FindObjCMethodDeclsWithOrigin(current_id,
1144 complete_interface_decl,
1155 // Check the runtime only if the debug information didn't have a complete interface.
1157 lldb::ProcessSP process(m_target->GetProcessSP());
1162 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1164 if (!language_runtime)
1167 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1172 ConstString interface_name(interface_decl->getNameAsString().c_str());
1173 bool append = false;
1174 uint32_t max_matches = 1;
1175 std::vector <ClangASTType> types;
1177 if (!type_vendor->FindTypes(interface_name,
1183 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1185 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1187 if (!runtime_interface_type)
1190 ObjCInterfaceDecl *runtime_interface_decl = runtime_interface_type->getDecl();
1192 FindObjCMethodDeclsWithOrigin(current_id,
1194 runtime_interface_decl,
1203 FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id,
1204 NameSearchContext &context,
1205 clang::ASTContext &ast_context,
1206 ClangASTImporter *ast_importer,
1207 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl)
1209 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1211 if (origin_iface_decl.IsInvalid())
1214 std::string name_str = context.m_decl_name.getAsString();
1215 StringRef name(name_str.c_str());
1216 IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name));
1218 DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier));
1222 if (origin_property_decl.IsValid())
1224 DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context));
1225 if (parser_property_decl.IsValid())
1229 ASTDumper dumper((Decl*)parser_property_decl.decl);
1230 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1233 context.AddNamedDecl(parser_property_decl.decl);
1238 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier));
1240 if (origin_ivar_decl.IsValid())
1242 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context));
1243 if (parser_ivar_decl.IsValid())
1247 ASTDumper dumper((Decl*)parser_ivar_decl.decl);
1248 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1251 context.AddNamedDecl(parser_ivar_decl.decl);
1260 ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context)
1262 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1264 static unsigned int invocation_id = 0;
1265 unsigned int current_id = invocation_id++;
1267 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context));
1268 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer));
1270 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
1273 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'",
1276 parser_iface_decl->getNameAsString().c_str(),
1277 context.m_decl_name.getAsString().c_str());
1279 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1287 log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...",
1289 origin_iface_decl.decl,
1290 &origin_iface_decl->getASTContext());
1292 SymbolContext null_sc;
1297 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(parser_iface_decl.decl));
1299 if (!complete_interface_decl)
1302 // We found the complete interface. The runtime never needs to be queried in this scenario.
1304 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
1306 if (complete_iface_decl.decl == origin_iface_decl.decl)
1307 break; // already checked this one
1310 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1312 complete_iface_decl.decl,
1313 &complete_iface_decl->getASTContext());
1315 FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1319 complete_iface_decl);
1327 // Check the runtime only if the debug information didn't have a complete interface.
1329 lldb::ProcessSP process(m_target->GetProcessSP());
1334 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1336 if (!language_runtime)
1339 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1344 bool append = false;
1345 uint32_t max_matches = 1;
1346 std::vector <ClangASTType> types;
1348 if (!type_vendor->FindTypes(class_name,
1354 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1356 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1358 if (!runtime_interface_type)
1361 DeclFromUser<const ObjCInterfaceDecl> runtime_iface_decl(runtime_interface_type->getDecl());
1364 log->Printf("CAS::FOPD[%d] trying runtime (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1366 runtime_iface_decl.decl,
1367 &runtime_iface_decl->getASTContext());
1369 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1373 runtime_iface_decl))
1379 typedef llvm::DenseMap <const FieldDecl *, uint64_t> FieldOffsetMap;
1380 typedef llvm::DenseMap <const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1382 template <class D, class O>
1384 ImportOffsetMap (llvm::DenseMap <const D*, O> &destination_map,
1385 llvm::DenseMap <const D*, O> &source_map,
1386 ClangASTImporter *importer,
1387 ASTContext &dest_ctx)
1389 typedef llvm::DenseMap <const D*, O> MapType;
1391 for (typename MapType::iterator fi = source_map.begin(), fe = source_map.end();
1395 DeclFromUser <D> user_decl(const_cast<D*>(fi->first));
1396 DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx));
1397 if (parser_decl.IsInvalid())
1399 destination_map.insert(std::pair<const D *, O>(parser_decl.decl, fi->second));
1405 template <bool IsVirtual> bool ExtractBaseOffsets (const ASTRecordLayout &record_layout,
1406 DeclFromUser<const CXXRecordDecl> &record,
1407 BaseOffsetMap &base_offsets)
1409 for (CXXRecordDecl::base_class_const_iterator
1410 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
1411 be = (IsVirtual ? record->vbases_end() : record->bases_end());
1415 if (!IsVirtual && bi->isVirtual())
1418 const clang::Type *origin_base_type = bi->getType().getTypePtr();
1419 const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>();
1421 if (!origin_base_record_type)
1424 DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl());
1426 if (origin_base_record.IsInvalid())
1429 DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record));
1431 if (origin_base_cxx_record.IsInvalid())
1434 CharUnits base_offset;
1437 base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1439 base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1441 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset));
1448 ClangASTSource::layoutRecordType(const RecordDecl *record,
1450 uint64_t &alignment,
1451 FieldOffsetMap &field_offsets,
1452 BaseOffsetMap &base_offsets,
1453 BaseOffsetMap &virtual_base_offsets)
1455 ClangASTMetrics::RegisterRecordLayout();
1457 static unsigned int invocation_id = 0;
1458 unsigned int current_id = invocation_id++;
1460 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1464 log->Printf("LayoutRecordType[%u] on (ASTContext*)%p for (RecordDecl*)%p [name = '%s']",
1468 record->getNameAsString().c_str());
1472 DeclFromParser <const RecordDecl> parser_record(record);
1473 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer));
1475 if (origin_record.IsInvalid())
1478 FieldOffsetMap origin_field_offsets;
1479 BaseOffsetMap origin_base_offsets;
1480 BaseOffsetMap origin_virtual_base_offsets;
1482 ClangASTContext::GetCompleteDecl(&origin_record->getASTContext(), const_cast<RecordDecl*>(origin_record.decl));
1484 if (!origin_record.decl->getDefinition())
1487 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1489 int field_idx = 0, field_count = record_layout.getFieldCount();
1491 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end();
1495 if (field_idx >= field_count)
1496 return false; // Layout didn't go well. Bail out.
1498 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1500 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1505 ASTContext &parser_ast_context(record->getASTContext());
1507 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1509 if (origin_cxx_record.IsValid())
1511 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1512 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1516 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) ||
1517 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) ||
1518 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context))
1521 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1522 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
1526 log->Printf("LRT[%u] returned:", current_id);
1527 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id, origin_record.decl);
1528 log->Printf("LRT[%u] Size = %" PRId64, current_id, size);
1529 log->Printf("LRT[%u] Alignment = %" PRId64, current_id, alignment);
1530 log->Printf("LRT[%u] Fields:", current_id);
1531 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1535 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %" PRId64 " bits",
1538 fi->getNameAsString().c_str(),
1539 field_offsets[*fi]);
1541 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1542 if (parser_cxx_record.IsValid())
1544 log->Printf("LRT[%u] Bases:", current_id);
1545 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1549 bool is_virtual = bi->isVirtual();
1551 QualType base_type = bi->getType();
1552 const RecordType *base_record_type = base_type->getAs<RecordType>();
1553 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1554 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
1556 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %" PRId64 " chars",
1558 (is_virtual ? "Virtual " : ""),
1559 base_cxx_record.decl,
1560 base_cxx_record.decl->getNameAsString().c_str(),
1561 (is_virtual ? virtual_base_offsets[base_cxx_record.decl].getQuantity() :
1562 base_offsets[base_cxx_record.decl].getQuantity()));
1567 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1575 ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1576 const ConstString &name,
1577 ClangASTImporter::NamespaceMapSP &parent_map) const
1579 static unsigned int invocation_id = 0;
1580 unsigned int current_id = invocation_id++;
1582 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1586 if (parent_map && parent_map->size())
1587 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
1591 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
1593 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
1602 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1606 ClangNamespaceDecl found_namespace_decl;
1608 lldb::ModuleSP module_sp = i->first;
1609 ClangNamespaceDecl module_parent_namespace_decl = i->second;
1611 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
1616 SymbolContext null_sc;
1618 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
1620 if (!found_namespace_decl)
1623 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
1626 log->Printf(" CMN[%u] Found namespace %s in module %s",
1629 module_sp->GetFileSpec().GetFilename().GetCString());
1634 const ModuleList &target_images = m_target->GetImages();
1635 Mutex::Locker modules_locker(target_images.GetMutex());
1637 ClangNamespaceDecl null_namespace_decl;
1639 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
1641 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
1646 ClangNamespaceDecl found_namespace_decl;
1648 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
1653 SymbolContext null_sc;
1655 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
1657 if (!found_namespace_decl)
1660 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
1663 log->Printf(" CMN[%u] Found namespace %s in module %s",
1666 image->GetFileSpec().GetFilename().GetCString());
1672 ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1674 if (!namespace_decls)
1677 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
1679 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
1684 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1686 if (!copied_namespace_decl)
1689 context.m_decls.push_back(copied_namespace_decl);
1691 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1693 return dyn_cast<NamespaceDecl>(copied_decl);
1697 ClangASTSource::GuardedCopyType (const ClangASTType &src_type)
1699 ClangASTMetrics::RegisterLLDBImport();
1701 SetImportInProgress(true);
1703 QualType copied_qual_type = m_ast_importer->CopyType (m_ast_context, src_type.GetASTContext(), src_type.GetQualType());
1705 SetImportInProgress(false);
1707 if (copied_qual_type.getAsOpaquePtr() && copied_qual_type->getCanonicalTypeInternal().isNull())
1708 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1710 return ClangASTType();
1712 return ClangASTType(m_ast_context, copied_qual_type);
1716 NameSearchContext::AddVarDecl(const ClangASTType &type)
1718 assert (type && "Type for variable must be valid!");
1720 if (!type.IsValid())
1723 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
1725 clang::ASTContext *ast = type.GetASTContext();
1727 clang::NamedDecl *Decl = VarDecl::Create(*ast,
1728 const_cast<DeclContext*>(m_decl_context),
1735 m_decls.push_back(Decl);
1741 NameSearchContext::AddFunDecl (const ClangASTType &type)
1743 assert (type && "Type for variable must be valid!");
1745 if (!type.IsValid())
1748 if (m_function_types.count(type))
1751 m_function_types.insert(type);
1753 QualType qual_type (type.GetQualType());
1755 clang::ASTContext *ast = type.GetASTContext();
1757 const bool isInlineSpecified = false;
1758 const bool hasWrittenPrototype = true;
1759 const bool isConstexprSpecified = false;
1761 clang::FunctionDecl *func_decl = FunctionDecl::Create (*ast,
1762 const_cast<DeclContext*>(m_decl_context),
1765 m_decl_name.getAsIdentifierInfo(),
1770 hasWrittenPrototype,
1771 isConstexprSpecified);
1773 // We have to do more than just synthesize the FunctionDecl. We have to
1774 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1775 // this, we raid the function's FunctionProtoType for types.
1777 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
1779 if (func_proto_type)
1781 unsigned NumArgs = func_proto_type->getNumArgs();
1784 SmallVector<ParmVarDecl *, 5> parm_var_decls;
1786 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1788 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
1790 parm_var_decls.push_back(ParmVarDecl::Create (*ast,
1791 const_cast<DeclContext*>(m_decl_context),
1801 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
1805 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1808 log->Printf("Function type wasn't a FunctionProtoType");
1811 m_decls.push_back(func_decl);
1817 NameSearchContext::AddGenericFunDecl()
1819 FunctionProtoType::ExtProtoInfo proto_info;
1821 proto_info.Variadic = true;
1823 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
1824 ArrayRef<QualType>(), // argument types
1827 return AddFunDecl(ClangASTType (m_ast_source.m_ast_context, generic_function_type));
1831 NameSearchContext::AddTypeDecl(const ClangASTType &clang_type)
1835 QualType qual_type = clang_type.GetQualType();
1837 if (const TypedefType *typedef_type = llvm::dyn_cast<TypedefType>(qual_type))
1839 TypedefNameDecl *typedef_name_decl = typedef_type->getDecl();
1841 m_decls.push_back(typedef_name_decl);
1843 return (NamedDecl*)typedef_name_decl;
1845 else if (const TagType *tag_type = qual_type->getAs<TagType>())
1847 TagDecl *tag_decl = tag_type->getDecl();
1849 m_decls.push_back(tag_decl);
1853 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
1855 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
1857 m_decls.push_back((NamedDecl*)interface_decl);
1859 return (NamedDecl*)interface_decl;
1866 NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1868 for (clang::NamedDecl *decl : result)
1869 m_decls.push_back (decl);
1873 NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1875 m_decls.push_back (decl);