Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp
39648 views
//===-- ClangASTSource.cpp ------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "ClangASTSource.h"910#include "ClangDeclVendor.h"11#include "ClangModulesDeclVendor.h"1213#include "lldb/Core/Module.h"14#include "lldb/Core/ModuleList.h"15#include "lldb/Symbol/CompilerDeclContext.h"16#include "lldb/Symbol/Function.h"17#include "lldb/Symbol/SymbolFile.h"18#include "lldb/Symbol/TaggedASTType.h"19#include "lldb/Target/Target.h"20#include "lldb/Utility/LLDBLog.h"21#include "lldb/Utility/Log.h"22#include "clang/AST/ASTContext.h"23#include "clang/Basic/SourceManager.h"2425#include "Plugins/ExpressionParser/Clang/ClangUtil.h"26#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"27#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"2829#include <memory>30#include <vector>3132using namespace clang;33using namespace lldb_private;3435// Scoped class that will remove an active lexical decl from the set when it36// goes out of scope.37namespace {38class ScopedLexicalDeclEraser {39public:40ScopedLexicalDeclEraser(std::set<const clang::Decl *> &decls,41const clang::Decl *decl)42: m_active_lexical_decls(decls), m_decl(decl) {}4344~ScopedLexicalDeclEraser() { m_active_lexical_decls.erase(m_decl); }4546private:47std::set<const clang::Decl *> &m_active_lexical_decls;48const clang::Decl *m_decl;49};50}5152ClangASTSource::ClangASTSource(53const lldb::TargetSP &target,54const std::shared_ptr<ClangASTImporter> &importer)55: m_lookups_enabled(false), m_target(target), m_ast_context(nullptr),56m_ast_importer_sp(importer), m_active_lexical_decls(),57m_active_lookups() {58assert(m_ast_importer_sp && "No ClangASTImporter passed to ClangASTSource?");59}6061void ClangASTSource::InstallASTContext(TypeSystemClang &clang_ast_context) {62m_ast_context = &clang_ast_context.getASTContext();63m_clang_ast_context = &clang_ast_context;64m_file_manager = &m_ast_context->getSourceManager().getFileManager();65m_ast_importer_sp->InstallMapCompleter(m_ast_context, *this);66}6768ClangASTSource::~ClangASTSource() {69m_ast_importer_sp->ForgetDestination(m_ast_context);7071if (!m_target)72return;7374// Unregister the current ASTContext as a source for all scratch75// ASTContexts in the ClangASTImporter. Without this the scratch AST might76// query the deleted ASTContext for additional type information.77// We unregister from *all* scratch ASTContexts in case a type got exported78// to a scratch AST that isn't the best fitting scratch ASTContext.79lldb::TypeSystemClangSP scratch_ts_sp = ScratchTypeSystemClang::GetForTarget(80*m_target, ScratchTypeSystemClang::DefaultAST, false);8182if (!scratch_ts_sp)83return;8485ScratchTypeSystemClang *default_scratch_ast =86llvm::cast<ScratchTypeSystemClang>(scratch_ts_sp.get());87// Unregister from the default scratch AST (and all sub-ASTs).88default_scratch_ast->ForgetSource(m_ast_context, *m_ast_importer_sp);89}9091void ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) {92if (!m_ast_context)93return;9495m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();96m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();97}9899// The core lookup interface.100bool ClangASTSource::FindExternalVisibleDeclsByName(101const DeclContext *decl_ctx, DeclarationName clang_decl_name) {102if (!m_ast_context) {103SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);104return false;105}106107std::string decl_name(clang_decl_name.getAsString());108109switch (clang_decl_name.getNameKind()) {110// Normal identifiers.111case DeclarationName::Identifier: {112clang::IdentifierInfo *identifier_info =113clang_decl_name.getAsIdentifierInfo();114115if (!identifier_info || identifier_info->getBuiltinID() != 0) {116SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);117return false;118}119} break;120121// Operator names.122case DeclarationName::CXXOperatorName:123case DeclarationName::CXXLiteralOperatorName:124break;125126// Using directives found in this context.127// Tell Sema we didn't find any or we'll end up getting asked a *lot*.128case DeclarationName::CXXUsingDirective:129SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);130return false;131132case DeclarationName::ObjCZeroArgSelector:133case DeclarationName::ObjCOneArgSelector:134case DeclarationName::ObjCMultiArgSelector: {135llvm::SmallVector<NamedDecl *, 1> method_decls;136137NameSearchContext method_search_context(*m_clang_ast_context, method_decls,138clang_decl_name, decl_ctx);139140FindObjCMethodDecls(method_search_context);141142SetExternalVisibleDeclsForName(decl_ctx, clang_decl_name, method_decls);143return (method_decls.size() > 0);144}145// These aren't possible in the global context.146case DeclarationName::CXXConstructorName:147case DeclarationName::CXXDestructorName:148case DeclarationName::CXXConversionFunctionName:149case DeclarationName::CXXDeductionGuideName:150SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);151return false;152}153154if (!GetLookupsEnabled()) {155// Wait until we see a '$' at the start of a name before we start doing any156// lookups so we can avoid lookup up all of the builtin types.157if (!decl_name.empty() && decl_name[0] == '$') {158SetLookupsEnabled(true);159} else {160SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);161return false;162}163}164165ConstString const_decl_name(decl_name.c_str());166167const char *uniqued_const_decl_name = const_decl_name.GetCString();168if (m_active_lookups.find(uniqued_const_decl_name) !=169m_active_lookups.end()) {170// We are currently looking up this name...171SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);172return false;173}174m_active_lookups.insert(uniqued_const_decl_name);175llvm::SmallVector<NamedDecl *, 4> name_decls;176NameSearchContext name_search_context(*m_clang_ast_context, name_decls,177clang_decl_name, decl_ctx);178FindExternalVisibleDecls(name_search_context);179SetExternalVisibleDeclsForName(decl_ctx, clang_decl_name, name_decls);180m_active_lookups.erase(uniqued_const_decl_name);181return (name_decls.size() != 0);182}183184TagDecl *ClangASTSource::FindCompleteType(const TagDecl *decl) {185Log *log = GetLog(LLDBLog::Expressions);186187if (const NamespaceDecl *namespace_context =188dyn_cast<NamespaceDecl>(decl->getDeclContext())) {189ClangASTImporter::NamespaceMapSP namespace_map =190m_ast_importer_sp->GetNamespaceMap(namespace_context);191192if (!namespace_map)193return nullptr;194195LLDB_LOGV(log, " CTD Inspecting namespace map{0:x} ({1} entries)",196namespace_map.get(), namespace_map->size());197198for (const ClangASTImporter::NamespaceMapItem &item : *namespace_map) {199LLDB_LOG(log, " CTD Searching namespace {0} in module {1}",200item.second.GetName(), item.first->GetFileSpec().GetFilename());201202ConstString name(decl->getName());203204// Create a type matcher using the CompilerDeclContext for the namespace205// as the context (item.second) and search for the name inside of this206// context.207TypeQuery query(item.second, name);208TypeResults results;209item.first->FindTypes(query, results);210211for (const lldb::TypeSP &type_sp : results.GetTypeMap().Types()) {212CompilerType clang_type(type_sp->GetFullCompilerType());213214if (!ClangUtil::IsClangType(clang_type))215continue;216217const TagType *tag_type =218ClangUtil::GetQualType(clang_type)->getAs<TagType>();219220if (!tag_type)221continue;222223TagDecl *candidate_tag_decl =224const_cast<TagDecl *>(tag_type->getDecl());225226if (TypeSystemClang::GetCompleteDecl(227&candidate_tag_decl->getASTContext(), candidate_tag_decl))228return candidate_tag_decl;229}230}231} else {232const ModuleList &module_list = m_target->GetImages();233// Create a type matcher using a CompilerDecl. Each TypeSystem class knows234// how to fill out a CompilerContext array using a CompilerDecl.235TypeQuery query(CompilerDecl(m_clang_ast_context, (void *)decl));236TypeResults results;237module_list.FindTypes(nullptr, query, results);238for (const lldb::TypeSP &type_sp : results.GetTypeMap().Types()) {239240CompilerType clang_type(type_sp->GetFullCompilerType());241242if (!ClangUtil::IsClangType(clang_type))243continue;244245const TagType *tag_type =246ClangUtil::GetQualType(clang_type)->getAs<TagType>();247248if (!tag_type)249continue;250251TagDecl *candidate_tag_decl = const_cast<TagDecl *>(tag_type->getDecl());252253if (TypeSystemClang::GetCompleteDecl(&candidate_tag_decl->getASTContext(),254candidate_tag_decl))255return candidate_tag_decl;256}257}258return nullptr;259}260261void ClangASTSource::CompleteType(TagDecl *tag_decl) {262Log *log = GetLog(LLDBLog::Expressions);263264if (log) {265LLDB_LOG(log,266" CompleteTagDecl on (ASTContext*){0} Completing "267"(TagDecl*){1:x} named {2}",268m_clang_ast_context->getDisplayName(), tag_decl,269tag_decl->getName());270271LLDB_LOG(log, " CTD Before:\n{0}", ClangUtil::DumpDecl(tag_decl));272}273274auto iter = m_active_lexical_decls.find(tag_decl);275if (iter != m_active_lexical_decls.end())276return;277m_active_lexical_decls.insert(tag_decl);278ScopedLexicalDeclEraser eraser(m_active_lexical_decls, tag_decl);279280if (!m_ast_importer_sp->CompleteTagDecl(tag_decl)) {281// We couldn't complete the type. Maybe there's a definition somewhere282// else that can be completed.283if (TagDecl *alternate = FindCompleteType(tag_decl))284m_ast_importer_sp->CompleteTagDeclWithOrigin(tag_decl, alternate);285}286287LLDB_LOG(log, " [CTD] After:\n{0}", ClangUtil::DumpDecl(tag_decl));288}289290void ClangASTSource::CompleteType(clang::ObjCInterfaceDecl *interface_decl) {291Log *log = GetLog(LLDBLog::Expressions);292293LLDB_LOG(log,294" [CompleteObjCInterfaceDecl] on (ASTContext*){0:x} '{1}' "295"Completing an ObjCInterfaceDecl named {1}",296m_ast_context, m_clang_ast_context->getDisplayName(),297interface_decl->getName());298LLDB_LOG(log, " [COID] Before:\n{0}",299ClangUtil::DumpDecl(interface_decl));300301ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(interface_decl);302303if (original.Valid()) {304if (ObjCInterfaceDecl *original_iface_decl =305dyn_cast<ObjCInterfaceDecl>(original.decl)) {306ObjCInterfaceDecl *complete_iface_decl =307GetCompleteObjCInterface(original_iface_decl);308309if (complete_iface_decl && (complete_iface_decl != original_iface_decl)) {310m_ast_importer_sp->SetDeclOrigin(interface_decl, complete_iface_decl);311}312}313}314315m_ast_importer_sp->CompleteObjCInterfaceDecl(interface_decl);316317if (interface_decl->getSuperClass() &&318interface_decl->getSuperClass() != interface_decl)319CompleteType(interface_decl->getSuperClass());320321LLDB_LOG(log, " [COID] After:");322LLDB_LOG(log, " [COID] {0}", ClangUtil::DumpDecl(interface_decl));323}324325clang::ObjCInterfaceDecl *ClangASTSource::GetCompleteObjCInterface(326const clang::ObjCInterfaceDecl *interface_decl) {327lldb::ProcessSP process(m_target->GetProcessSP());328329if (!process)330return nullptr;331332ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));333334if (!language_runtime)335return nullptr;336337ConstString class_name(interface_decl->getNameAsString().c_str());338339lldb::TypeSP complete_type_sp(340language_runtime->LookupInCompleteClassCache(class_name));341342if (!complete_type_sp)343return nullptr;344345TypeFromUser complete_type =346TypeFromUser(complete_type_sp->GetFullCompilerType());347lldb::opaque_compiler_type_t complete_opaque_type =348complete_type.GetOpaqueQualType();349350if (!complete_opaque_type)351return nullptr;352353const clang::Type *complete_clang_type =354QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();355const ObjCInterfaceType *complete_interface_type =356dyn_cast<ObjCInterfaceType>(complete_clang_type);357358if (!complete_interface_type)359return nullptr;360361ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl());362363return complete_iface_decl;364}365366void ClangASTSource::FindExternalLexicalDecls(367const DeclContext *decl_context,368llvm::function_ref<bool(Decl::Kind)> predicate,369llvm::SmallVectorImpl<Decl *> &decls) {370371Log *log = GetLog(LLDBLog::Expressions);372373const Decl *context_decl = dyn_cast<Decl>(decl_context);374375if (!context_decl)376return;377378auto iter = m_active_lexical_decls.find(context_decl);379if (iter != m_active_lexical_decls.end())380return;381m_active_lexical_decls.insert(context_decl);382ScopedLexicalDeclEraser eraser(m_active_lexical_decls, context_decl);383384if (log) {385if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))386LLDB_LOG(log,387"FindExternalLexicalDecls on (ASTContext*){0:x} '{1}' in "388"'{2}' ({3}Decl*){4}",389m_ast_context, m_clang_ast_context->getDisplayName(),390context_named_decl->getNameAsString().c_str(),391context_decl->getDeclKindName(),392static_cast<const void *>(context_decl));393else if (context_decl)394LLDB_LOG(log,395"FindExternalLexicalDecls on (ASTContext*){0:x} '{1}' in "396"({2}Decl*){3}",397m_ast_context, m_clang_ast_context->getDisplayName(),398context_decl->getDeclKindName(),399static_cast<const void *>(context_decl));400else401LLDB_LOG(log,402"FindExternalLexicalDecls on (ASTContext*){0:x} '{1}' in a "403"NULL context",404m_ast_context, m_clang_ast_context->getDisplayName());405}406407ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(context_decl);408409if (!original.Valid())410return;411412LLDB_LOG(log, " FELD Original decl (ASTContext*){0:x} (Decl*){1:x}:\n{2}",413static_cast<void *>(original.ctx),414static_cast<void *>(original.decl),415ClangUtil::DumpDecl(original.decl));416417if (ObjCInterfaceDecl *original_iface_decl =418dyn_cast<ObjCInterfaceDecl>(original.decl)) {419ObjCInterfaceDecl *complete_iface_decl =420GetCompleteObjCInterface(original_iface_decl);421422if (complete_iface_decl && (complete_iface_decl != original_iface_decl)) {423original.decl = complete_iface_decl;424original.ctx = &complete_iface_decl->getASTContext();425426m_ast_importer_sp->SetDeclOrigin(context_decl, complete_iface_decl);427}428}429430if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original.decl)) {431ExternalASTSource *external_source = original.ctx->getExternalSource();432433if (external_source)434external_source->CompleteType(original_tag_decl);435}436437const DeclContext *original_decl_context =438dyn_cast<DeclContext>(original.decl);439440if (!original_decl_context)441return;442443// Indicates whether we skipped any Decls of the original DeclContext.444bool SkippedDecls = false;445for (Decl *decl : original_decl_context->decls()) {446// The predicate function returns true if the passed declaration kind is447// the one we are looking for.448// See clang::ExternalASTSource::FindExternalLexicalDecls()449if (predicate(decl->getKind())) {450if (log) {451std::string ast_dump = ClangUtil::DumpDecl(decl);452if (const NamedDecl *context_named_decl =453dyn_cast<NamedDecl>(context_decl))454LLDB_LOG(log, " FELD Adding [to {0}Decl {1}] lexical {2}Decl {3}",455context_named_decl->getDeclKindName(),456context_named_decl->getName(), decl->getDeclKindName(),457ast_dump);458else459LLDB_LOG(log, " FELD Adding lexical {0}Decl {1}",460decl->getDeclKindName(), ast_dump);461}462463Decl *copied_decl = CopyDecl(decl);464465if (!copied_decl)466continue;467468// FIXME: We should add the copied decl to the 'decls' list. This would469// add the copied Decl into the DeclContext and make sure that we470// correctly propagate that we added some Decls back to Clang.471// By leaving 'decls' empty we incorrectly return false from472// DeclContext::LoadLexicalDeclsFromExternalStorage which might cause473// lookup issues later on.474// We can't just add them for now as the ASTImporter already added the475// decl into the DeclContext and this would add it twice.476477if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl)) {478QualType copied_field_type = copied_field->getType();479480m_ast_importer_sp->RequireCompleteType(copied_field_type);481}482} else {483SkippedDecls = true;484}485}486487// CopyDecl may build a lookup table which may set up ExternalLexicalStorage488// to false. However, since we skipped some of the external Decls we must489// set it back!490if (SkippedDecls) {491decl_context->setHasExternalLexicalStorage(true);492// This sets HasLazyExternalLexicalLookups to true. By setting this bit we493// ensure that the lookup table is rebuilt, which means the external source494// is consulted again when a clang::DeclContext::lookup is called.495const_cast<DeclContext *>(decl_context)->setMustBuildLookupTable();496}497}498499void ClangASTSource::FindExternalVisibleDecls(NameSearchContext &context) {500assert(m_ast_context);501502const ConstString name(context.m_decl_name.getAsString().c_str());503504Log *log = GetLog(LLDBLog::Expressions);505506if (log) {507if (!context.m_decl_context)508LLDB_LOG(log,509"ClangASTSource::FindExternalVisibleDecls on "510"(ASTContext*){0:x} '{1}' for '{2}' in a NULL DeclContext",511m_ast_context, m_clang_ast_context->getDisplayName(), name);512else if (const NamedDecl *context_named_decl =513dyn_cast<NamedDecl>(context.m_decl_context))514LLDB_LOG(log,515"ClangASTSource::FindExternalVisibleDecls on "516"(ASTContext*){0:x} '{1}' for '{2}' in '{3}'",517m_ast_context, m_clang_ast_context->getDisplayName(), name,518context_named_decl->getName());519else520LLDB_LOG(log,521"ClangASTSource::FindExternalVisibleDecls on "522"(ASTContext*){0:x} '{1}' for '{2}' in a '{3}'",523m_ast_context, m_clang_ast_context->getDisplayName(), name,524context.m_decl_context->getDeclKindName());525}526527if (isa<NamespaceDecl>(context.m_decl_context)) {528LookupInNamespace(context);529} else if (isa<ObjCInterfaceDecl>(context.m_decl_context)) {530FindObjCPropertyAndIvarDecls(context);531} else if (!isa<TranslationUnitDecl>(context.m_decl_context)) {532// we shouldn't be getting FindExternalVisibleDecls calls for these533return;534} else {535CompilerDeclContext namespace_decl;536537LLDB_LOG(log, " CAS::FEVD Searching the root namespace");538539FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl);540}541542if (!context.m_namespace_map->empty()) {543if (log && log->GetVerbose())544LLDB_LOG(log, " CAS::FEVD Registering namespace map {0:x} ({1} entries)",545context.m_namespace_map.get(), context.m_namespace_map->size());546547NamespaceDecl *clang_namespace_decl =548AddNamespace(context, context.m_namespace_map);549550if (clang_namespace_decl)551clang_namespace_decl->setHasExternalVisibleStorage();552}553}554555clang::Sema *ClangASTSource::getSema() {556return m_clang_ast_context->getSema();557}558559bool ClangASTSource::IgnoreName(const ConstString name,560bool ignore_all_dollar_names) {561static const ConstString id_name("id");562static const ConstString Class_name("Class");563564if (m_ast_context->getLangOpts().ObjC)565if (name == id_name || name == Class_name)566return true;567568StringRef name_string_ref = name.GetStringRef();569570// The ClangASTSource is not responsible for finding $-names.571return name_string_ref.empty() ||572(ignore_all_dollar_names && name_string_ref.starts_with("$")) ||573name_string_ref.starts_with("_$");574}575576void ClangASTSource::FindExternalVisibleDecls(577NameSearchContext &context, lldb::ModuleSP module_sp,578CompilerDeclContext &namespace_decl) {579assert(m_ast_context);580581Log *log = GetLog(LLDBLog::Expressions);582583SymbolContextList sc_list;584585const ConstString name(context.m_decl_name.getAsString().c_str());586if (IgnoreName(name, true))587return;588589if (!m_target)590return;591592FillNamespaceMap(context, module_sp, namespace_decl);593594if (context.m_found_type)595return;596597lldb::TypeSP type_sp;598TypeResults results;599if (module_sp && namespace_decl) {600// Match the name in the specified decl context.601TypeQuery query(namespace_decl, name, TypeQueryOptions::e_find_one);602module_sp->FindTypes(query, results);603type_sp = results.GetFirstType();604} else {605// Match the exact name of the type at the root level.606TypeQuery query(name.GetStringRef(), TypeQueryOptions::e_exact_match |607TypeQueryOptions::e_find_one);608m_target->GetImages().FindTypes(nullptr, query, results);609type_sp = results.GetFirstType();610}611612if (type_sp) {613if (log) {614const char *name_string = type_sp->GetName().GetCString();615616LLDB_LOG(log, " CAS::FEVD Matching type found for \"{0}\": {1}", name,617(name_string ? name_string : "<anonymous>"));618}619620CompilerType full_type = type_sp->GetFullCompilerType();621622CompilerType copied_clang_type(GuardedCopyType(full_type));623624if (!copied_clang_type) {625LLDB_LOG(log, " CAS::FEVD - Couldn't export a type");626} else {627628context.AddTypeDecl(copied_clang_type);629630context.m_found_type = true;631}632}633634if (!context.m_found_type) {635// Try the modules next.636FindDeclInModules(context, name);637}638639if (!context.m_found_type && m_ast_context->getLangOpts().ObjC) {640FindDeclInObjCRuntime(context, name);641}642}643644void ClangASTSource::FillNamespaceMap(645NameSearchContext &context, lldb::ModuleSP module_sp,646const CompilerDeclContext &namespace_decl) {647const ConstString name(context.m_decl_name.getAsString().c_str());648if (IgnoreName(name, true))649return;650651Log *log = GetLog(LLDBLog::Expressions);652653if (module_sp && namespace_decl) {654CompilerDeclContext found_namespace_decl;655656if (SymbolFile *symbol_file = module_sp->GetSymbolFile()) {657found_namespace_decl = symbol_file->FindNamespace(name, namespace_decl);658659if (found_namespace_decl) {660context.m_namespace_map->push_back(661std::pair<lldb::ModuleSP, CompilerDeclContext>(662module_sp, found_namespace_decl));663664LLDB_LOG(log, " CAS::FEVD Found namespace {0} in module {1}", name,665module_sp->GetFileSpec().GetFilename());666}667}668return;669}670671for (lldb::ModuleSP image : m_target->GetImages().Modules()) {672if (!image)673continue;674675CompilerDeclContext found_namespace_decl;676677SymbolFile *symbol_file = image->GetSymbolFile();678679if (!symbol_file)680continue;681682// If namespace_decl is not valid, 'FindNamespace' would look for683// any namespace called 'name' (ignoring parent contexts) and return684// the first one it finds. Thus if we're doing a qualified lookup only685// consider root namespaces. E.g., in an expression ::A::B::Foo, the686// lookup of ::A will result in a qualified lookup. Note, namespace687// disambiguation for function calls are handled separately in688// SearchFunctionsInSymbolContexts.689const bool find_root_namespaces =690context.m_decl_context &&691context.m_decl_context->shouldUseQualifiedLookup();692found_namespace_decl = symbol_file->FindNamespace(693name, namespace_decl, /* only root namespaces */ find_root_namespaces);694695if (found_namespace_decl) {696context.m_namespace_map->push_back(697std::pair<lldb::ModuleSP, CompilerDeclContext>(image,698found_namespace_decl));699700LLDB_LOG(log, " CAS::FEVD Found namespace {0} in module {1}", name,701image->GetFileSpec().GetFilename());702}703}704}705706bool ClangASTSource::FindObjCMethodDeclsWithOrigin(707NameSearchContext &context, ObjCInterfaceDecl *original_interface_decl,708const char *log_info) {709const DeclarationName &decl_name(context.m_decl_name);710clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();711712Selector original_selector;713714if (decl_name.isObjCZeroArgSelector()) {715const IdentifierInfo *ident =716&original_ctx->Idents.get(decl_name.getAsString());717original_selector = original_ctx->Selectors.getSelector(0, &ident);718} else if (decl_name.isObjCOneArgSelector()) {719const std::string &decl_name_string = decl_name.getAsString();720std::string decl_name_string_without_colon(decl_name_string.c_str(),721decl_name_string.length() - 1);722const IdentifierInfo *ident =723&original_ctx->Idents.get(decl_name_string_without_colon);724original_selector = original_ctx->Selectors.getSelector(1, &ident);725} else {726SmallVector<const IdentifierInfo *, 4> idents;727728clang::Selector sel = decl_name.getObjCSelector();729730unsigned num_args = sel.getNumArgs();731732for (unsigned i = 0; i != num_args; ++i) {733idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));734}735736original_selector =737original_ctx->Selectors.getSelector(num_args, idents.data());738}739740DeclarationName original_decl_name(original_selector);741742llvm::SmallVector<NamedDecl *, 1> methods;743744TypeSystemClang::GetCompleteDecl(original_ctx, original_interface_decl);745746if (ObjCMethodDecl *instance_method_decl =747original_interface_decl->lookupInstanceMethod(original_selector)) {748methods.push_back(instance_method_decl);749} else if (ObjCMethodDecl *class_method_decl =750original_interface_decl->lookupClassMethod(751original_selector)) {752methods.push_back(class_method_decl);753}754755if (methods.empty()) {756return false;757}758759for (NamedDecl *named_decl : methods) {760if (!named_decl)761continue;762763ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(named_decl);764765if (!result_method)766continue;767768Decl *copied_decl = CopyDecl(result_method);769770if (!copied_decl)771continue;772773ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);774775if (!copied_method_decl)776continue;777778Log *log = GetLog(LLDBLog::Expressions);779780LLDB_LOG(log, " CAS::FOMD found ({0}) {1}", log_info,781ClangUtil::DumpDecl(copied_method_decl));782783context.AddNamedDecl(copied_method_decl);784}785786return true;787}788789void ClangASTSource::FindDeclInModules(NameSearchContext &context,790ConstString name) {791Log *log = GetLog(LLDBLog::Expressions);792793std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =794GetClangModulesDeclVendor();795if (!modules_decl_vendor)796return;797798bool append = false;799uint32_t max_matches = 1;800std::vector<clang::NamedDecl *> decls;801802if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls))803return;804805LLDB_LOG(log, " CAS::FEVD Matching entity found for \"{0}\" in the modules",806name);807808clang::NamedDecl *const decl_from_modules = decls[0];809810if (llvm::isa<clang::TypeDecl>(decl_from_modules) ||811llvm::isa<clang::ObjCContainerDecl>(decl_from_modules) ||812llvm::isa<clang::EnumConstantDecl>(decl_from_modules)) {813clang::Decl *copied_decl = CopyDecl(decl_from_modules);814clang::NamedDecl *copied_named_decl =815copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr;816817if (!copied_named_decl) {818LLDB_LOG(log, " CAS::FEVD - Couldn't export a type from the modules");819820return;821}822823context.AddNamedDecl(copied_named_decl);824825context.m_found_type = true;826}827}828829void ClangASTSource::FindDeclInObjCRuntime(NameSearchContext &context,830ConstString name) {831Log *log = GetLog(LLDBLog::Expressions);832833lldb::ProcessSP process(m_target->GetProcessSP());834835if (!process)836return;837838ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));839840if (!language_runtime)841return;842843DeclVendor *decl_vendor = language_runtime->GetDeclVendor();844845if (!decl_vendor)846return;847848bool append = false;849uint32_t max_matches = 1;850std::vector<clang::NamedDecl *> decls;851852auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(decl_vendor);853if (!clang_decl_vendor->FindDecls(name, append, max_matches, decls))854return;855856LLDB_LOG(log, " CAS::FEVD Matching type found for \"{0}\" in the runtime",857name);858859clang::Decl *copied_decl = CopyDecl(decls[0]);860clang::NamedDecl *copied_named_decl =861copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr;862863if (!copied_named_decl) {864LLDB_LOG(log, " CAS::FEVD - Couldn't export a type from the runtime");865866return;867}868869context.AddNamedDecl(copied_named_decl);870}871872void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) {873Log *log = GetLog(LLDBLog::Expressions);874875const DeclarationName &decl_name(context.m_decl_name);876const DeclContext *decl_ctx(context.m_decl_context);877878const ObjCInterfaceDecl *interface_decl =879dyn_cast<ObjCInterfaceDecl>(decl_ctx);880881if (!interface_decl)882return;883884do {885ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(interface_decl);886887if (!original.Valid())888break;889890ObjCInterfaceDecl *original_interface_decl =891dyn_cast<ObjCInterfaceDecl>(original.decl);892893if (FindObjCMethodDeclsWithOrigin(context, original_interface_decl,894"at origin"))895return; // found it, no need to look any further896} while (false);897898StreamString ss;899900if (decl_name.isObjCZeroArgSelector()) {901ss.Printf("%s", decl_name.getAsString().c_str());902} else if (decl_name.isObjCOneArgSelector()) {903ss.Printf("%s", decl_name.getAsString().c_str());904} else {905clang::Selector sel = decl_name.getObjCSelector();906907for (unsigned i = 0, e = sel.getNumArgs(); i != e; ++i) {908llvm::StringRef r = sel.getNameForSlot(i);909ss.Printf("%s:", r.str().c_str());910}911}912ss.Flush();913914if (ss.GetString().contains("$__lldb"))915return; // we don't need any results916917ConstString selector_name(ss.GetString());918919LLDB_LOG(log,920"ClangASTSource::FindObjCMethodDecls on (ASTContext*){0:x} '{1}' "921"for selector [{2} {3}]",922m_ast_context, m_clang_ast_context->getDisplayName(),923interface_decl->getName(), selector_name);924SymbolContextList sc_list;925926ModuleFunctionSearchOptions function_options;927function_options.include_symbols = false;928function_options.include_inlines = false;929930std::string interface_name = interface_decl->getNameAsString();931932do {933StreamString ms;934ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());935ms.Flush();936ConstString instance_method_name(ms.GetString());937938sc_list.Clear();939m_target->GetImages().FindFunctions(instance_method_name,940lldb::eFunctionNameTypeFull,941function_options, sc_list);942943if (sc_list.GetSize())944break;945946ms.Clear();947ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());948ms.Flush();949ConstString class_method_name(ms.GetString());950951sc_list.Clear();952m_target->GetImages().FindFunctions(class_method_name,953lldb::eFunctionNameTypeFull,954function_options, sc_list);955956if (sc_list.GetSize())957break;958959// Fall back and check for methods in categories. If we find methods this960// way, we need to check that they're actually in categories on the desired961// class.962963SymbolContextList candidate_sc_list;964965m_target->GetImages().FindFunctions(selector_name,966lldb::eFunctionNameTypeSelector,967function_options, candidate_sc_list);968969for (const SymbolContext &candidate_sc : candidate_sc_list) {970if (!candidate_sc.function)971continue;972973const char *candidate_name = candidate_sc.function->GetName().AsCString();974975const char *cursor = candidate_name;976977if (*cursor != '+' && *cursor != '-')978continue;979980++cursor;981982if (*cursor != '[')983continue;984985++cursor;986987size_t interface_len = interface_name.length();988989if (strncmp(cursor, interface_name.c_str(), interface_len))990continue;991992cursor += interface_len;993994if (*cursor == ' ' || *cursor == '(')995sc_list.Append(candidate_sc);996}997} while (false);998999if (sc_list.GetSize()) {1000// We found a good function symbol. Use that.10011002for (const SymbolContext &sc : sc_list) {1003if (!sc.function)1004continue;10051006CompilerDeclContext function_decl_ctx = sc.function->GetDeclContext();1007if (!function_decl_ctx)1008continue;10091010ObjCMethodDecl *method_decl =1011TypeSystemClang::DeclContextGetAsObjCMethodDecl(function_decl_ctx);10121013if (!method_decl)1014continue;10151016ObjCInterfaceDecl *found_interface_decl =1017method_decl->getClassInterface();10181019if (!found_interface_decl)1020continue;10211022if (found_interface_decl->getName() == interface_decl->getName()) {1023Decl *copied_decl = CopyDecl(method_decl);10241025if (!copied_decl)1026continue;10271028ObjCMethodDecl *copied_method_decl =1029dyn_cast<ObjCMethodDecl>(copied_decl);10301031if (!copied_method_decl)1032continue;10331034LLDB_LOG(log, " CAS::FOMD found (in symbols)\n{0}",1035ClangUtil::DumpDecl(copied_method_decl));10361037context.AddNamedDecl(copied_method_decl);1038}1039}10401041return;1042}10431044// Try the debug information.10451046do {1047ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(1048const_cast<ObjCInterfaceDecl *>(interface_decl));10491050if (!complete_interface_decl)1051break;10521053// We found the complete interface. The runtime never needs to be queried1054// in this scenario.10551056DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(1057complete_interface_decl);10581059if (complete_interface_decl == interface_decl)1060break; // already checked this one10611062LLDB_LOG(log,1063"CAS::FOPD trying origin "1064"(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...",1065complete_interface_decl, &complete_iface_decl->getASTContext());10661067FindObjCMethodDeclsWithOrigin(context, complete_interface_decl,1068"in debug info");10691070return;1071} while (false);10721073do {1074// Check the modules only if the debug information didn't have a complete1075// interface.10761077if (std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =1078GetClangModulesDeclVendor()) {1079ConstString interface_name(interface_decl->getNameAsString().c_str());1080bool append = false;1081uint32_t max_matches = 1;1082std::vector<clang::NamedDecl *> decls;10831084if (!modules_decl_vendor->FindDecls(interface_name, append, max_matches,1085decls))1086break;10871088ObjCInterfaceDecl *interface_decl_from_modules =1089dyn_cast<ObjCInterfaceDecl>(decls[0]);10901091if (!interface_decl_from_modules)1092break;10931094if (FindObjCMethodDeclsWithOrigin(context, interface_decl_from_modules,1095"in modules"))1096return;1097}1098} while (false);10991100do {1101// Check the runtime only if the debug information didn't have a complete1102// interface and the modules don't get us anywhere.11031104lldb::ProcessSP process(m_target->GetProcessSP());11051106if (!process)1107break;11081109ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));11101111if (!language_runtime)1112break;11131114DeclVendor *decl_vendor = language_runtime->GetDeclVendor();11151116if (!decl_vendor)1117break;11181119ConstString interface_name(interface_decl->getNameAsString().c_str());1120bool append = false;1121uint32_t max_matches = 1;1122std::vector<clang::NamedDecl *> decls;11231124auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(decl_vendor);1125if (!clang_decl_vendor->FindDecls(interface_name, append, max_matches,1126decls))1127break;11281129ObjCInterfaceDecl *runtime_interface_decl =1130dyn_cast<ObjCInterfaceDecl>(decls[0]);11311132if (!runtime_interface_decl)1133break;11341135FindObjCMethodDeclsWithOrigin(context, runtime_interface_decl,1136"in runtime");1137} while (false);1138}11391140bool ClangASTSource::FindObjCPropertyAndIvarDeclsWithOrigin(1141NameSearchContext &context,1142DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl) {1143Log *log = GetLog(LLDBLog::Expressions);11441145if (origin_iface_decl.IsInvalid())1146return false;11471148std::string name_str = context.m_decl_name.getAsString();1149StringRef name(name_str);1150IdentifierInfo &name_identifier(1151origin_iface_decl->getASTContext().Idents.get(name));11521153DeclFromUser<ObjCPropertyDecl> origin_property_decl(1154origin_iface_decl->FindPropertyDeclaration(1155&name_identifier, ObjCPropertyQueryKind::OBJC_PR_query_instance));11561157bool found = false;11581159if (origin_property_decl.IsValid()) {1160DeclFromParser<ObjCPropertyDecl> parser_property_decl(1161origin_property_decl.Import(m_ast_context, *m_ast_importer_sp));1162if (parser_property_decl.IsValid()) {1163LLDB_LOG(log, " CAS::FOPD found\n{0}",1164ClangUtil::DumpDecl(parser_property_decl.decl));11651166context.AddNamedDecl(parser_property_decl.decl);1167found = true;1168}1169}11701171DeclFromUser<ObjCIvarDecl> origin_ivar_decl(1172origin_iface_decl->getIvarDecl(&name_identifier));11731174if (origin_ivar_decl.IsValid()) {1175DeclFromParser<ObjCIvarDecl> parser_ivar_decl(1176origin_ivar_decl.Import(m_ast_context, *m_ast_importer_sp));1177if (parser_ivar_decl.IsValid()) {1178LLDB_LOG(log, " CAS::FOPD found\n{0}",1179ClangUtil::DumpDecl(parser_ivar_decl.decl));11801181context.AddNamedDecl(parser_ivar_decl.decl);1182found = true;1183}1184}11851186return found;1187}11881189void ClangASTSource::FindObjCPropertyAndIvarDecls(NameSearchContext &context) {1190Log *log = GetLog(LLDBLog::Expressions);11911192DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(1193cast<ObjCInterfaceDecl>(context.m_decl_context));1194DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(1195parser_iface_decl.GetOrigin(*m_ast_importer_sp));11961197ConstString class_name(parser_iface_decl->getNameAsString().c_str());11981199LLDB_LOG(log,1200"ClangASTSource::FindObjCPropertyAndIvarDecls on "1201"(ASTContext*){0:x} '{1}' for '{2}.{3}'",1202m_ast_context, m_clang_ast_context->getDisplayName(),1203parser_iface_decl->getName(), context.m_decl_name.getAsString());12041205if (FindObjCPropertyAndIvarDeclsWithOrigin(context, origin_iface_decl))1206return;12071208LLDB_LOG(log,1209"CAS::FOPD couldn't find the property on origin "1210"(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}, searching "1211"elsewhere...",1212origin_iface_decl.decl, &origin_iface_decl->getASTContext());12131214SymbolContext null_sc;1215TypeList type_list;12161217do {1218ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(1219const_cast<ObjCInterfaceDecl *>(parser_iface_decl.decl));12201221if (!complete_interface_decl)1222break;12231224// We found the complete interface. The runtime never needs to be queried1225// in this scenario.12261227DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(1228complete_interface_decl);12291230if (complete_iface_decl.decl == origin_iface_decl.decl)1231break; // already checked this one12321233LLDB_LOG(log,1234"CAS::FOPD trying origin "1235"(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...",1236complete_iface_decl.decl, &complete_iface_decl->getASTContext());12371238FindObjCPropertyAndIvarDeclsWithOrigin(context, complete_iface_decl);12391240return;1241} while (false);12421243do {1244// Check the modules only if the debug information didn't have a complete1245// interface.12461247std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =1248GetClangModulesDeclVendor();12491250if (!modules_decl_vendor)1251break;12521253bool append = false;1254uint32_t max_matches = 1;1255std::vector<clang::NamedDecl *> decls;12561257if (!modules_decl_vendor->FindDecls(class_name, append, max_matches, decls))1258break;12591260DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_modules(1261dyn_cast<ObjCInterfaceDecl>(decls[0]));12621263if (!interface_decl_from_modules.IsValid())1264break;12651266LLDB_LOG(log,1267"CAS::FOPD[{0:x}] trying module "1268"(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...",1269interface_decl_from_modules.decl,1270&interface_decl_from_modules->getASTContext());12711272if (FindObjCPropertyAndIvarDeclsWithOrigin(context,1273interface_decl_from_modules))1274return;1275} while (false);12761277do {1278// Check the runtime only if the debug information didn't have a complete1279// interface and nothing was in the modules.12801281lldb::ProcessSP process(m_target->GetProcessSP());12821283if (!process)1284return;12851286ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));12871288if (!language_runtime)1289return;12901291DeclVendor *decl_vendor = language_runtime->GetDeclVendor();12921293if (!decl_vendor)1294break;12951296bool append = false;1297uint32_t max_matches = 1;1298std::vector<clang::NamedDecl *> decls;12991300auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(decl_vendor);1301if (!clang_decl_vendor->FindDecls(class_name, append, max_matches, decls))1302break;13031304DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_runtime(1305dyn_cast<ObjCInterfaceDecl>(decls[0]));13061307if (!interface_decl_from_runtime.IsValid())1308break;13091310LLDB_LOG(log,1311"CAS::FOPD[{0:x}] trying runtime "1312"(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...",1313interface_decl_from_runtime.decl,1314&interface_decl_from_runtime->getASTContext());13151316if (FindObjCPropertyAndIvarDeclsWithOrigin(context,1317interface_decl_from_runtime))1318return;1319} while (false);1320}13211322void ClangASTSource::LookupInNamespace(NameSearchContext &context) {1323const NamespaceDecl *namespace_context =1324dyn_cast<NamespaceDecl>(context.m_decl_context);13251326Log *log = GetLog(LLDBLog::Expressions);13271328ClangASTImporter::NamespaceMapSP namespace_map =1329m_ast_importer_sp->GetNamespaceMap(namespace_context);13301331LLDB_LOGV(log, " CAS::FEVD Inspecting namespace map {0:x} ({1} entries)",1332namespace_map.get(), namespace_map->size());13331334if (!namespace_map)1335return;13361337for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(),1338e = namespace_map->end();1339i != e; ++i) {1340LLDB_LOG(log, " CAS::FEVD Searching namespace {0} in module {1}",1341i->second.GetName(), i->first->GetFileSpec().GetFilename());13421343FindExternalVisibleDecls(context, i->first, i->second);1344}1345}13461347bool ClangASTSource::layoutRecordType(1348const RecordDecl *record, uint64_t &size, uint64_t &alignment,1349llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,1350llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>1351&base_offsets,1352llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>1353&virtual_base_offsets) {1354return m_ast_importer_sp->importRecordLayoutFromOrigin(1355record, size, alignment, field_offsets, base_offsets,1356virtual_base_offsets);1357}13581359void ClangASTSource::CompleteNamespaceMap(1360ClangASTImporter::NamespaceMapSP &namespace_map, ConstString name,1361ClangASTImporter::NamespaceMapSP &parent_map) const {13621363Log *log = GetLog(LLDBLog::Expressions);13641365if (log) {1366if (parent_map && parent_map->size())1367LLDB_LOG(log,1368"CompleteNamespaceMap on (ASTContext*){0:x} '{1}' Searching "1369"for namespace {2} in namespace {3}",1370m_ast_context, m_clang_ast_context->getDisplayName(), name,1371parent_map->begin()->second.GetName());1372else1373LLDB_LOG(log,1374"CompleteNamespaceMap on (ASTContext*){0} '{1}' Searching "1375"for namespace {2}",1376m_ast_context, m_clang_ast_context->getDisplayName(), name);1377}13781379if (parent_map) {1380for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(),1381e = parent_map->end();1382i != e; ++i) {1383CompilerDeclContext found_namespace_decl;13841385lldb::ModuleSP module_sp = i->first;1386CompilerDeclContext module_parent_namespace_decl = i->second;13871388SymbolFile *symbol_file = module_sp->GetSymbolFile();13891390if (!symbol_file)1391continue;13921393found_namespace_decl =1394symbol_file->FindNamespace(name, module_parent_namespace_decl);13951396if (!found_namespace_decl)1397continue;13981399namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>(1400module_sp, found_namespace_decl));14011402LLDB_LOG(log, " CMN Found namespace {0} in module {1}", name,1403module_sp->GetFileSpec().GetFilename());1404}1405} else {1406CompilerDeclContext null_namespace_decl;1407for (lldb::ModuleSP image : m_target->GetImages().Modules()) {1408if (!image)1409continue;14101411CompilerDeclContext found_namespace_decl;14121413SymbolFile *symbol_file = image->GetSymbolFile();14141415if (!symbol_file)1416continue;14171418found_namespace_decl =1419symbol_file->FindNamespace(name, null_namespace_decl);14201421if (!found_namespace_decl)1422continue;14231424namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>(1425image, found_namespace_decl));14261427LLDB_LOG(log, " CMN[{0}] Found namespace {0} in module {1}", name,1428image->GetFileSpec().GetFilename());1429}1430}1431}14321433NamespaceDecl *ClangASTSource::AddNamespace(1434NameSearchContext &context,1435ClangASTImporter::NamespaceMapSP &namespace_decls) {1436if (!namespace_decls)1437return nullptr;14381439const CompilerDeclContext &namespace_decl = namespace_decls->begin()->second;14401441clang::ASTContext *src_ast =1442TypeSystemClang::DeclContextGetTypeSystemClang(namespace_decl);1443if (!src_ast)1444return nullptr;1445clang::NamespaceDecl *src_namespace_decl =1446TypeSystemClang::DeclContextGetAsNamespaceDecl(namespace_decl);14471448if (!src_namespace_decl)1449return nullptr;14501451Decl *copied_decl = CopyDecl(src_namespace_decl);14521453if (!copied_decl)1454return nullptr;14551456NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);14571458if (!copied_namespace_decl)1459return nullptr;14601461context.m_decls.push_back(copied_namespace_decl);14621463m_ast_importer_sp->RegisterNamespaceMap(copied_namespace_decl,1464namespace_decls);14651466return dyn_cast<NamespaceDecl>(copied_decl);1467}14681469clang::Decl *ClangASTSource::CopyDecl(Decl *src_decl) {1470return m_ast_importer_sp->CopyDecl(m_ast_context, src_decl);1471}14721473ClangASTImporter::DeclOrigin ClangASTSource::GetDeclOrigin(const clang::Decl *decl) {1474return m_ast_importer_sp->GetDeclOrigin(decl);1475}14761477CompilerType ClangASTSource::GuardedCopyType(const CompilerType &src_type) {1478auto ts = src_type.GetTypeSystem();1479auto src_ast = ts.dyn_cast_or_null<TypeSystemClang>();1480if (!src_ast)1481return {};14821483QualType copied_qual_type = ClangUtil::GetQualType(1484m_ast_importer_sp->CopyType(*m_clang_ast_context, src_type));14851486if (copied_qual_type.getAsOpaquePtr() &&1487copied_qual_type->getCanonicalTypeInternal().isNull())1488// this shouldn't happen, but we're hardening because the AST importer1489// seems to be generating bad types on occasion.1490return {};14911492return m_clang_ast_context->GetType(copied_qual_type);1493}14941495std::shared_ptr<ClangModulesDeclVendor>1496ClangASTSource::GetClangModulesDeclVendor() {1497auto persistent_vars = llvm::cast<ClangPersistentVariables>(1498m_target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC));1499return persistent_vars->GetClangModulesDeclVendor();1500}150115021503