1 //===-- ClangASTSource.cpp ------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "ClangASTSource.h" 10 11 #include "ClangDeclVendor.h" 12 #include "ClangModulesDeclVendor.h" 13 14 #include "lldb/Core/Module.h" 15 #include "lldb/Core/ModuleList.h" 16 #include "lldb/Symbol/CompilerDeclContext.h" 17 #include "lldb/Symbol/Function.h" 18 #include "lldb/Symbol/SymbolFile.h" 19 #include "lldb/Symbol/TaggedASTType.h" 20 #include "lldb/Target/Target.h" 21 #include "lldb/Utility/LLDBLog.h" 22 #include "lldb/Utility/Log.h" 23 #include "clang/AST/ASTContext.h" 24 #include "clang/Basic/SourceManager.h" 25 26 #include "Plugins/ExpressionParser/Clang/ClangUtil.h" 27 #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" 28 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" 29 30 #include <memory> 31 #include <vector> 32 33 using namespace clang; 34 using namespace lldb_private; 35 36 // Scoped class that will remove an active lexical decl from the set when it 37 // goes out of scope. 38 namespace { 39 class ScopedLexicalDeclEraser { 40 public: 41 ScopedLexicalDeclEraser(std::set<const clang::Decl *> &decls, 42 const clang::Decl *decl) 43 : m_active_lexical_decls(decls), m_decl(decl) {} 44 45 ~ScopedLexicalDeclEraser() { m_active_lexical_decls.erase(m_decl); } 46 47 private: 48 std::set<const clang::Decl *> &m_active_lexical_decls; 49 const clang::Decl *m_decl; 50 }; 51 } 52 53 ClangASTSource::ClangASTSource( 54 const lldb::TargetSP &target, 55 const std::shared_ptr<ClangASTImporter> &importer) 56 : m_lookups_enabled(false), m_target(target), m_ast_context(nullptr), 57 m_ast_importer_sp(importer), m_active_lexical_decls(), 58 m_active_lookups() { 59 assert(m_ast_importer_sp && "No ClangASTImporter passed to ClangASTSource?"); 60 } 61 62 void ClangASTSource::InstallASTContext(TypeSystemClang &clang_ast_context) { 63 m_ast_context = &clang_ast_context.getASTContext(); 64 m_clang_ast_context = &clang_ast_context; 65 m_file_manager = &m_ast_context->getSourceManager().getFileManager(); 66 m_ast_importer_sp->InstallMapCompleter(m_ast_context, *this); 67 } 68 69 ClangASTSource::~ClangASTSource() { 70 m_ast_importer_sp->ForgetDestination(m_ast_context); 71 72 if (!m_target) 73 return; 74 75 // Unregister the current ASTContext as a source for all scratch 76 // ASTContexts in the ClangASTImporter. Without this the scratch AST might 77 // query the deleted ASTContext for additional type information. 78 // We unregister from *all* scratch ASTContexts in case a type got exported 79 // to a scratch AST that isn't the best fitting scratch ASTContext. 80 lldb::TypeSystemClangSP scratch_ts_sp = ScratchTypeSystemClang::GetForTarget( 81 *m_target, ScratchTypeSystemClang::DefaultAST, false); 82 83 if (!scratch_ts_sp) 84 return; 85 86 ScratchTypeSystemClang *default_scratch_ast = 87 llvm::cast<ScratchTypeSystemClang>(scratch_ts_sp.get()); 88 // Unregister from the default scratch AST (and all sub-ASTs). 89 default_scratch_ast->ForgetSource(m_ast_context, *m_ast_importer_sp); 90 } 91 92 void ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) { 93 if (!m_ast_context) 94 return; 95 96 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage(); 97 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage(); 98 } 99 100 // The core lookup interface. 101 bool ClangASTSource::FindExternalVisibleDeclsByName( 102 const DeclContext *decl_ctx, DeclarationName clang_decl_name) { 103 if (!m_ast_context) { 104 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); 105 return false; 106 } 107 108 std::string decl_name(clang_decl_name.getAsString()); 109 110 switch (clang_decl_name.getNameKind()) { 111 // Normal identifiers. 112 case DeclarationName::Identifier: { 113 clang::IdentifierInfo *identifier_info = 114 clang_decl_name.getAsIdentifierInfo(); 115 116 if (!identifier_info || identifier_info->getBuiltinID() != 0) { 117 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); 118 return false; 119 } 120 } break; 121 122 // Operator names. 123 case DeclarationName::CXXOperatorName: 124 case DeclarationName::CXXLiteralOperatorName: 125 break; 126 127 // Using directives found in this context. 128 // Tell Sema we didn't find any or we'll end up getting asked a *lot*. 129 case DeclarationName::CXXUsingDirective: 130 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); 131 return false; 132 133 case DeclarationName::ObjCZeroArgSelector: 134 case DeclarationName::ObjCOneArgSelector: 135 case DeclarationName::ObjCMultiArgSelector: { 136 llvm::SmallVector<NamedDecl *, 1> method_decls; 137 138 NameSearchContext method_search_context(*m_clang_ast_context, method_decls, 139 clang_decl_name, decl_ctx); 140 141 FindObjCMethodDecls(method_search_context); 142 143 SetExternalVisibleDeclsForName(decl_ctx, clang_decl_name, method_decls); 144 return (method_decls.size() > 0); 145 } 146 // These aren't possible in the global context. 147 case DeclarationName::CXXConstructorName: 148 case DeclarationName::CXXDestructorName: 149 case DeclarationName::CXXConversionFunctionName: 150 case DeclarationName::CXXDeductionGuideName: 151 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); 152 return false; 153 } 154 155 if (!GetLookupsEnabled()) { 156 // Wait until we see a '$' at the start of a name before we start doing any 157 // lookups so we can avoid lookup up all of the builtin types. 158 if (!decl_name.empty() && decl_name[0] == '$') { 159 SetLookupsEnabled(true); 160 } else { 161 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); 162 return false; 163 } 164 } 165 166 ConstString const_decl_name(decl_name.c_str()); 167 168 const char *uniqued_const_decl_name = const_decl_name.GetCString(); 169 if (m_active_lookups.find(uniqued_const_decl_name) != 170 m_active_lookups.end()) { 171 // We are currently looking up this name... 172 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); 173 return false; 174 } 175 m_active_lookups.insert(uniqued_const_decl_name); 176 llvm::SmallVector<NamedDecl *, 4> name_decls; 177 NameSearchContext name_search_context(*m_clang_ast_context, name_decls, 178 clang_decl_name, decl_ctx); 179 FindExternalVisibleDecls(name_search_context); 180 SetExternalVisibleDeclsForName(decl_ctx, clang_decl_name, name_decls); 181 m_active_lookups.erase(uniqued_const_decl_name); 182 return (name_decls.size() != 0); 183 } 184 185 TagDecl *ClangASTSource::FindCompleteType(const TagDecl *decl) { 186 Log *log = GetLog(LLDBLog::Expressions); 187 188 if (const NamespaceDecl *namespace_context = 189 dyn_cast<NamespaceDecl>(decl->getDeclContext())) { 190 ClangASTImporter::NamespaceMapSP namespace_map = 191 m_ast_importer_sp->GetNamespaceMap(namespace_context); 192 193 if (!namespace_map) 194 return nullptr; 195 196 LLDB_LOGV(log, " CTD Inspecting namespace map{0:x} ({1} entries)", 197 namespace_map.get(), namespace_map->size()); 198 199 for (const ClangASTImporter::NamespaceMapItem &item : *namespace_map) { 200 LLDB_LOG(log, " CTD Searching namespace {0} in module {1}", 201 item.second.GetName(), item.first->GetFileSpec().GetFilename()); 202 203 ConstString name(decl->getName()); 204 205 // Create a type matcher using the CompilerDeclContext for the namespace 206 // as the context (item.second) and search for the name inside of this 207 // context. 208 TypeQuery query(item.second, name); 209 TypeResults results; 210 item.first->FindTypes(query, results); 211 212 for (const lldb::TypeSP &type_sp : results.GetTypeMap().Types()) { 213 CompilerType clang_type(type_sp->GetFullCompilerType()); 214 215 if (!ClangUtil::IsClangType(clang_type)) 216 continue; 217 218 const TagType *tag_type = 219 ClangUtil::GetQualType(clang_type)->getAs<TagType>(); 220 221 if (!tag_type) 222 continue; 223 224 TagDecl *candidate_tag_decl = 225 const_cast<TagDecl *>(tag_type->getDecl()); 226 227 if (TypeSystemClang::GetCompleteDecl( 228 &candidate_tag_decl->getASTContext(), candidate_tag_decl)) 229 return candidate_tag_decl; 230 } 231 } 232 } else { 233 const ModuleList &module_list = m_target->GetImages(); 234 // Create a type matcher using a CompilerDecl. Each TypeSystem class knows 235 // how to fill out a CompilerContext array using a CompilerDecl. 236 TypeQuery query(CompilerDecl(m_clang_ast_context, (void *)decl)); 237 TypeResults results; 238 module_list.FindTypes(nullptr, query, results); 239 for (const lldb::TypeSP &type_sp : results.GetTypeMap().Types()) { 240 241 CompilerType clang_type(type_sp->GetFullCompilerType()); 242 243 if (!ClangUtil::IsClangType(clang_type)) 244 continue; 245 246 const TagType *tag_type = 247 ClangUtil::GetQualType(clang_type)->getAs<TagType>(); 248 249 if (!tag_type) 250 continue; 251 252 TagDecl *candidate_tag_decl = const_cast<TagDecl *>(tag_type->getDecl()); 253 254 if (TypeSystemClang::GetCompleteDecl(&candidate_tag_decl->getASTContext(), 255 candidate_tag_decl)) 256 return candidate_tag_decl; 257 } 258 } 259 return nullptr; 260 } 261 262 void ClangASTSource::CompleteType(TagDecl *tag_decl) { 263 Log *log = GetLog(LLDBLog::Expressions); 264 265 if (log) { 266 LLDB_LOG(log, 267 " CompleteTagDecl on (ASTContext*){0} Completing " 268 "(TagDecl*){1:x} named {2}", 269 m_clang_ast_context->getDisplayName(), tag_decl, 270 tag_decl->getName()); 271 272 LLDB_LOG(log, " CTD Before:\n{0}", ClangUtil::DumpDecl(tag_decl)); 273 } 274 275 auto iter = m_active_lexical_decls.find(tag_decl); 276 if (iter != m_active_lexical_decls.end()) 277 return; 278 m_active_lexical_decls.insert(tag_decl); 279 ScopedLexicalDeclEraser eraser(m_active_lexical_decls, tag_decl); 280 281 if (!m_ast_importer_sp->CompleteTagDecl(tag_decl)) { 282 // We couldn't complete the type. Maybe there's a definition somewhere 283 // else that can be completed. 284 if (TagDecl *alternate = FindCompleteType(tag_decl)) 285 m_ast_importer_sp->CompleteTagDeclWithOrigin(tag_decl, alternate); 286 } 287 288 LLDB_LOG(log, " [CTD] After:\n{0}", ClangUtil::DumpDecl(tag_decl)); 289 } 290 291 void ClangASTSource::CompleteType(clang::ObjCInterfaceDecl *interface_decl) { 292 Log *log = GetLog(LLDBLog::Expressions); 293 294 LLDB_LOG(log, 295 " [CompleteObjCInterfaceDecl] on (ASTContext*){0:x} '{1}' " 296 "Completing an ObjCInterfaceDecl named {1}", 297 m_ast_context, m_clang_ast_context->getDisplayName(), 298 interface_decl->getName()); 299 LLDB_LOG(log, " [COID] Before:\n{0}", 300 ClangUtil::DumpDecl(interface_decl)); 301 302 ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(interface_decl); 303 304 if (original.Valid()) { 305 if (ObjCInterfaceDecl *original_iface_decl = 306 dyn_cast<ObjCInterfaceDecl>(original.decl)) { 307 ObjCInterfaceDecl *complete_iface_decl = 308 GetCompleteObjCInterface(original_iface_decl); 309 310 if (complete_iface_decl && (complete_iface_decl != original_iface_decl)) { 311 m_ast_importer_sp->SetDeclOrigin(interface_decl, complete_iface_decl); 312 } 313 } 314 } 315 316 m_ast_importer_sp->CompleteObjCInterfaceDecl(interface_decl); 317 318 if (interface_decl->getSuperClass() && 319 interface_decl->getSuperClass() != interface_decl) 320 CompleteType(interface_decl->getSuperClass()); 321 322 LLDB_LOG(log, " [COID] After:"); 323 LLDB_LOG(log, " [COID] {0}", ClangUtil::DumpDecl(interface_decl)); 324 } 325 326 clang::ObjCInterfaceDecl *ClangASTSource::GetCompleteObjCInterface( 327 const clang::ObjCInterfaceDecl *interface_decl) { 328 lldb::ProcessSP process(m_target->GetProcessSP()); 329 330 if (!process) 331 return nullptr; 332 333 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process)); 334 335 if (!language_runtime) 336 return nullptr; 337 338 ConstString class_name(interface_decl->getNameAsString().c_str()); 339 340 lldb::TypeSP complete_type_sp( 341 language_runtime->LookupInCompleteClassCache(class_name)); 342 343 if (!complete_type_sp) 344 return nullptr; 345 346 TypeFromUser complete_type = 347 TypeFromUser(complete_type_sp->GetFullCompilerType()); 348 lldb::opaque_compiler_type_t complete_opaque_type = 349 complete_type.GetOpaqueQualType(); 350 351 if (!complete_opaque_type) 352 return nullptr; 353 354 const clang::Type *complete_clang_type = 355 QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr(); 356 const ObjCInterfaceType *complete_interface_type = 357 dyn_cast<ObjCInterfaceType>(complete_clang_type); 358 359 if (!complete_interface_type) 360 return nullptr; 361 362 ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl()); 363 364 return complete_iface_decl; 365 } 366 367 void ClangASTSource::FindExternalLexicalDecls( 368 const DeclContext *decl_context, 369 llvm::function_ref<bool(Decl::Kind)> predicate, 370 llvm::SmallVectorImpl<Decl *> &decls) { 371 372 Log *log = GetLog(LLDBLog::Expressions); 373 374 const Decl *context_decl = dyn_cast<Decl>(decl_context); 375 376 if (!context_decl) 377 return; 378 379 auto iter = m_active_lexical_decls.find(context_decl); 380 if (iter != m_active_lexical_decls.end()) 381 return; 382 m_active_lexical_decls.insert(context_decl); 383 ScopedLexicalDeclEraser eraser(m_active_lexical_decls, context_decl); 384 385 if (log) { 386 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl)) 387 LLDB_LOG(log, 388 "FindExternalLexicalDecls on (ASTContext*){0:x} '{1}' in " 389 "'{2}' ({3}Decl*){4}", 390 m_ast_context, m_clang_ast_context->getDisplayName(), 391 context_named_decl->getNameAsString().c_str(), 392 context_decl->getDeclKindName(), 393 static_cast<const void *>(context_decl)); 394 else if (context_decl) 395 LLDB_LOG(log, 396 "FindExternalLexicalDecls on (ASTContext*){0:x} '{1}' in " 397 "({2}Decl*){3}", 398 m_ast_context, m_clang_ast_context->getDisplayName(), 399 context_decl->getDeclKindName(), 400 static_cast<const void *>(context_decl)); 401 else 402 LLDB_LOG(log, 403 "FindExternalLexicalDecls on (ASTContext*){0:x} '{1}' in a " 404 "NULL context", 405 m_ast_context, m_clang_ast_context->getDisplayName()); 406 } 407 408 ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(context_decl); 409 410 if (!original.Valid()) 411 return; 412 413 LLDB_LOG(log, " FELD Original decl (ASTContext*){0:x} (Decl*){1:x}:\n{2}", 414 static_cast<void *>(original.ctx), 415 static_cast<void *>(original.decl), 416 ClangUtil::DumpDecl(original.decl)); 417 418 if (ObjCInterfaceDecl *original_iface_decl = 419 dyn_cast<ObjCInterfaceDecl>(original.decl)) { 420 ObjCInterfaceDecl *complete_iface_decl = 421 GetCompleteObjCInterface(original_iface_decl); 422 423 if (complete_iface_decl && (complete_iface_decl != original_iface_decl)) { 424 original.decl = complete_iface_decl; 425 original.ctx = &complete_iface_decl->getASTContext(); 426 427 m_ast_importer_sp->SetDeclOrigin(context_decl, complete_iface_decl); 428 } 429 } 430 431 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original.decl)) { 432 ExternalASTSource *external_source = original.ctx->getExternalSource(); 433 434 if (external_source) 435 external_source->CompleteType(original_tag_decl); 436 } 437 438 const DeclContext *original_decl_context = 439 dyn_cast<DeclContext>(original.decl); 440 441 if (!original_decl_context) 442 return; 443 444 // Indicates whether we skipped any Decls of the original DeclContext. 445 bool SkippedDecls = false; 446 for (Decl *decl : original_decl_context->decls()) { 447 // The predicate function returns true if the passed declaration kind is 448 // the one we are looking for. 449 // See clang::ExternalASTSource::FindExternalLexicalDecls() 450 if (predicate(decl->getKind())) { 451 if (log) { 452 std::string ast_dump = ClangUtil::DumpDecl(decl); 453 if (const NamedDecl *context_named_decl = 454 dyn_cast<NamedDecl>(context_decl)) 455 LLDB_LOG(log, " FELD Adding [to {0}Decl {1}] lexical {2}Decl {3}", 456 context_named_decl->getDeclKindName(), 457 context_named_decl->getName(), decl->getDeclKindName(), 458 ast_dump); 459 else 460 LLDB_LOG(log, " FELD Adding lexical {0}Decl {1}", 461 decl->getDeclKindName(), ast_dump); 462 } 463 464 Decl *copied_decl = CopyDecl(decl); 465 466 if (!copied_decl) 467 continue; 468 469 // FIXME: We should add the copied decl to the 'decls' list. This would 470 // add the copied Decl into the DeclContext and make sure that we 471 // correctly propagate that we added some Decls back to Clang. 472 // By leaving 'decls' empty we incorrectly return false from 473 // DeclContext::LoadLexicalDeclsFromExternalStorage which might cause 474 // lookup issues later on. 475 // We can't just add them for now as the ASTImporter already added the 476 // decl into the DeclContext and this would add it twice. 477 478 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl)) { 479 QualType copied_field_type = copied_field->getType(); 480 481 m_ast_importer_sp->RequireCompleteType(copied_field_type); 482 } 483 } else { 484 SkippedDecls = true; 485 } 486 } 487 488 // CopyDecl may build a lookup table which may set up ExternalLexicalStorage 489 // to false. However, since we skipped some of the external Decls we must 490 // set it back! 491 if (SkippedDecls) { 492 decl_context->setHasExternalLexicalStorage(true); 493 // This sets HasLazyExternalLexicalLookups to true. By setting this bit we 494 // ensure that the lookup table is rebuilt, which means the external source 495 // is consulted again when a clang::DeclContext::lookup is called. 496 const_cast<DeclContext *>(decl_context)->setMustBuildLookupTable(); 497 } 498 } 499 500 void ClangASTSource::FindExternalVisibleDecls(NameSearchContext &context) { 501 assert(m_ast_context); 502 503 const ConstString name(context.m_decl_name.getAsString().c_str()); 504 505 Log *log = GetLog(LLDBLog::Expressions); 506 507 if (log) { 508 if (!context.m_decl_context) 509 LLDB_LOG(log, 510 "ClangASTSource::FindExternalVisibleDecls on " 511 "(ASTContext*){0:x} '{1}' for '{2}' in a NULL DeclContext", 512 m_ast_context, m_clang_ast_context->getDisplayName(), name); 513 else if (const NamedDecl *context_named_decl = 514 dyn_cast<NamedDecl>(context.m_decl_context)) 515 LLDB_LOG(log, 516 "ClangASTSource::FindExternalVisibleDecls on " 517 "(ASTContext*){0:x} '{1}' for '{2}' in '{3}'", 518 m_ast_context, m_clang_ast_context->getDisplayName(), name, 519 context_named_decl->getName()); 520 else 521 LLDB_LOG(log, 522 "ClangASTSource::FindExternalVisibleDecls on " 523 "(ASTContext*){0:x} '{1}' for '{2}' in a '{3}'", 524 m_ast_context, m_clang_ast_context->getDisplayName(), name, 525 context.m_decl_context->getDeclKindName()); 526 } 527 528 if (isa<NamespaceDecl>(context.m_decl_context)) { 529 LookupInNamespace(context); 530 } else if (isa<ObjCInterfaceDecl>(context.m_decl_context)) { 531 FindObjCPropertyAndIvarDecls(context); 532 } else if (!isa<TranslationUnitDecl>(context.m_decl_context)) { 533 // we shouldn't be getting FindExternalVisibleDecls calls for these 534 return; 535 } else { 536 CompilerDeclContext namespace_decl; 537 538 LLDB_LOG(log, " CAS::FEVD Searching the root namespace"); 539 540 FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl); 541 } 542 543 if (!context.m_namespace_map->empty()) { 544 if (log && log->GetVerbose()) 545 LLDB_LOG(log, " CAS::FEVD Registering namespace map {0:x} ({1} entries)", 546 context.m_namespace_map.get(), context.m_namespace_map->size()); 547 548 NamespaceDecl *clang_namespace_decl = 549 AddNamespace(context, context.m_namespace_map); 550 551 if (clang_namespace_decl) 552 clang_namespace_decl->setHasExternalVisibleStorage(); 553 } 554 } 555 556 clang::Sema *ClangASTSource::getSema() { 557 return m_clang_ast_context->getSema(); 558 } 559 560 bool ClangASTSource::IgnoreName(const ConstString name, 561 bool ignore_all_dollar_names) { 562 static const ConstString id_name("id"); 563 static const ConstString Class_name("Class"); 564 565 if (m_ast_context->getLangOpts().ObjC) 566 if (name == id_name || name == Class_name) 567 return true; 568 569 StringRef name_string_ref = name.GetStringRef(); 570 571 // The ClangASTSource is not responsible for finding $-names. 572 return name_string_ref.empty() || 573 (ignore_all_dollar_names && name_string_ref.starts_with("$")) || 574 name_string_ref.starts_with("_$"); 575 } 576 577 void ClangASTSource::FindExternalVisibleDecls( 578 NameSearchContext &context, lldb::ModuleSP module_sp, 579 CompilerDeclContext &namespace_decl) { 580 assert(m_ast_context); 581 582 Log *log = GetLog(LLDBLog::Expressions); 583 584 SymbolContextList sc_list; 585 586 const ConstString name(context.m_decl_name.getAsString().c_str()); 587 if (IgnoreName(name, true)) 588 return; 589 590 if (!m_target) 591 return; 592 593 FillNamespaceMap(context, module_sp, namespace_decl); 594 595 if (context.m_found_type) 596 return; 597 598 lldb::TypeSP type_sp; 599 TypeResults results; 600 if (module_sp && namespace_decl) { 601 // Match the name in the specified decl context. 602 TypeQuery query(namespace_decl, name, TypeQueryOptions::e_find_one); 603 module_sp->FindTypes(query, results); 604 type_sp = results.GetFirstType(); 605 } else { 606 // Match the exact name of the type at the root level. 607 TypeQuery query(name.GetStringRef(), TypeQueryOptions::e_exact_match | 608 TypeQueryOptions::e_find_one); 609 m_target->GetImages().FindTypes(nullptr, query, results); 610 type_sp = results.GetFirstType(); 611 } 612 613 if (type_sp) { 614 if (log) { 615 const char *name_string = type_sp->GetName().GetCString(); 616 617 LLDB_LOG(log, " CAS::FEVD Matching type found for \"{0}\": {1}", name, 618 (name_string ? name_string : "<anonymous>")); 619 } 620 621 CompilerType full_type = type_sp->GetFullCompilerType(); 622 623 CompilerType copied_clang_type(GuardedCopyType(full_type)); 624 625 if (!copied_clang_type) { 626 LLDB_LOG(log, " CAS::FEVD - Couldn't export a type"); 627 } else { 628 629 context.AddTypeDecl(copied_clang_type); 630 631 context.m_found_type = true; 632 } 633 } 634 635 if (!context.m_found_type) { 636 // Try the modules next. 637 FindDeclInModules(context, name); 638 } 639 640 if (!context.m_found_type && m_ast_context->getLangOpts().ObjC) { 641 FindDeclInObjCRuntime(context, name); 642 } 643 } 644 645 void ClangASTSource::FillNamespaceMap( 646 NameSearchContext &context, lldb::ModuleSP module_sp, 647 const CompilerDeclContext &namespace_decl) { 648 const ConstString name(context.m_decl_name.getAsString().c_str()); 649 if (IgnoreName(name, true)) 650 return; 651 652 Log *log = GetLog(LLDBLog::Expressions); 653 654 if (module_sp && namespace_decl) { 655 CompilerDeclContext found_namespace_decl; 656 657 if (SymbolFile *symbol_file = module_sp->GetSymbolFile()) { 658 found_namespace_decl = symbol_file->FindNamespace(name, namespace_decl); 659 660 if (found_namespace_decl) { 661 context.m_namespace_map->push_back( 662 std::pair<lldb::ModuleSP, CompilerDeclContext>( 663 module_sp, found_namespace_decl)); 664 665 LLDB_LOG(log, " CAS::FEVD Found namespace {0} in module {1}", name, 666 module_sp->GetFileSpec().GetFilename()); 667 } 668 } 669 return; 670 } 671 672 for (lldb::ModuleSP image : m_target->GetImages().Modules()) { 673 if (!image) 674 continue; 675 676 CompilerDeclContext found_namespace_decl; 677 678 SymbolFile *symbol_file = image->GetSymbolFile(); 679 680 if (!symbol_file) 681 continue; 682 683 // If namespace_decl is not valid, 'FindNamespace' would look for 684 // any namespace called 'name' (ignoring parent contexts) and return 685 // the first one it finds. Thus if we're doing a qualified lookup only 686 // consider root namespaces. E.g., in an expression ::A::B::Foo, the 687 // lookup of ::A will result in a qualified lookup. Note, namespace 688 // disambiguation for function calls are handled separately in 689 // SearchFunctionsInSymbolContexts. 690 const bool find_root_namespaces = 691 context.m_decl_context && 692 context.m_decl_context->shouldUseQualifiedLookup(); 693 found_namespace_decl = symbol_file->FindNamespace( 694 name, namespace_decl, /* only root namespaces */ find_root_namespaces); 695 696 if (found_namespace_decl) { 697 context.m_namespace_map->push_back( 698 std::pair<lldb::ModuleSP, CompilerDeclContext>(image, 699 found_namespace_decl)); 700 701 LLDB_LOG(log, " CAS::FEVD Found namespace {0} in module {1}", name, 702 image->GetFileSpec().GetFilename()); 703 } 704 } 705 } 706 707 bool ClangASTSource::FindObjCMethodDeclsWithOrigin( 708 NameSearchContext &context, ObjCInterfaceDecl *original_interface_decl, 709 const char *log_info) { 710 const DeclarationName &decl_name(context.m_decl_name); 711 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext(); 712 713 Selector original_selector; 714 715 if (decl_name.isObjCZeroArgSelector()) { 716 const IdentifierInfo *ident = 717 &original_ctx->Idents.get(decl_name.getAsString()); 718 original_selector = original_ctx->Selectors.getSelector(0, &ident); 719 } else if (decl_name.isObjCOneArgSelector()) { 720 const std::string &decl_name_string = decl_name.getAsString(); 721 std::string decl_name_string_without_colon(decl_name_string.c_str(), 722 decl_name_string.length() - 1); 723 const IdentifierInfo *ident = 724 &original_ctx->Idents.get(decl_name_string_without_colon); 725 original_selector = original_ctx->Selectors.getSelector(1, &ident); 726 } else { 727 SmallVector<const IdentifierInfo *, 4> idents; 728 729 clang::Selector sel = decl_name.getObjCSelector(); 730 731 unsigned num_args = sel.getNumArgs(); 732 733 for (unsigned i = 0; i != num_args; ++i) { 734 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i))); 735 } 736 737 original_selector = 738 original_ctx->Selectors.getSelector(num_args, idents.data()); 739 } 740 741 DeclarationName original_decl_name(original_selector); 742 743 llvm::SmallVector<NamedDecl *, 1> methods; 744 745 TypeSystemClang::GetCompleteDecl(original_ctx, original_interface_decl); 746 747 if (ObjCMethodDecl *instance_method_decl = 748 original_interface_decl->lookupInstanceMethod(original_selector)) { 749 methods.push_back(instance_method_decl); 750 } else if (ObjCMethodDecl *class_method_decl = 751 original_interface_decl->lookupClassMethod( 752 original_selector)) { 753 methods.push_back(class_method_decl); 754 } 755 756 if (methods.empty()) { 757 return false; 758 } 759 760 for (NamedDecl *named_decl : methods) { 761 if (!named_decl) 762 continue; 763 764 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(named_decl); 765 766 if (!result_method) 767 continue; 768 769 Decl *copied_decl = CopyDecl(result_method); 770 771 if (!copied_decl) 772 continue; 773 774 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl); 775 776 if (!copied_method_decl) 777 continue; 778 779 Log *log = GetLog(LLDBLog::Expressions); 780 781 LLDB_LOG(log, " CAS::FOMD found ({0}) {1}", log_info, 782 ClangUtil::DumpDecl(copied_method_decl)); 783 784 context.AddNamedDecl(copied_method_decl); 785 } 786 787 return true; 788 } 789 790 void ClangASTSource::FindDeclInModules(NameSearchContext &context, 791 ConstString name) { 792 Log *log = GetLog(LLDBLog::Expressions); 793 794 std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor = 795 GetClangModulesDeclVendor(); 796 if (!modules_decl_vendor) 797 return; 798 799 bool append = false; 800 uint32_t max_matches = 1; 801 std::vector<clang::NamedDecl *> decls; 802 803 if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls)) 804 return; 805 806 LLDB_LOG(log, " CAS::FEVD Matching entity found for \"{0}\" in the modules", 807 name); 808 809 clang::NamedDecl *const decl_from_modules = decls[0]; 810 811 if (llvm::isa<clang::TypeDecl>(decl_from_modules) || 812 llvm::isa<clang::ObjCContainerDecl>(decl_from_modules) || 813 llvm::isa<clang::EnumConstantDecl>(decl_from_modules)) { 814 clang::Decl *copied_decl = CopyDecl(decl_from_modules); 815 clang::NamedDecl *copied_named_decl = 816 copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr; 817 818 if (!copied_named_decl) { 819 LLDB_LOG(log, " CAS::FEVD - Couldn't export a type from the modules"); 820 821 return; 822 } 823 824 context.AddNamedDecl(copied_named_decl); 825 826 context.m_found_type = true; 827 } 828 } 829 830 void ClangASTSource::FindDeclInObjCRuntime(NameSearchContext &context, 831 ConstString name) { 832 Log *log = GetLog(LLDBLog::Expressions); 833 834 lldb::ProcessSP process(m_target->GetProcessSP()); 835 836 if (!process) 837 return; 838 839 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process)); 840 841 if (!language_runtime) 842 return; 843 844 DeclVendor *decl_vendor = language_runtime->GetDeclVendor(); 845 846 if (!decl_vendor) 847 return; 848 849 bool append = false; 850 uint32_t max_matches = 1; 851 std::vector<clang::NamedDecl *> decls; 852 853 auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(decl_vendor); 854 if (!clang_decl_vendor->FindDecls(name, append, max_matches, decls)) 855 return; 856 857 LLDB_LOG(log, " CAS::FEVD Matching type found for \"{0}\" in the runtime", 858 name); 859 860 clang::Decl *copied_decl = CopyDecl(decls[0]); 861 clang::NamedDecl *copied_named_decl = 862 copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr; 863 864 if (!copied_named_decl) { 865 LLDB_LOG(log, " CAS::FEVD - Couldn't export a type from the runtime"); 866 867 return; 868 } 869 870 context.AddNamedDecl(copied_named_decl); 871 } 872 873 void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) { 874 Log *log = GetLog(LLDBLog::Expressions); 875 876 const DeclarationName &decl_name(context.m_decl_name); 877 const DeclContext *decl_ctx(context.m_decl_context); 878 879 const ObjCInterfaceDecl *interface_decl = 880 dyn_cast<ObjCInterfaceDecl>(decl_ctx); 881 882 if (!interface_decl) 883 return; 884 885 do { 886 ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(interface_decl); 887 888 if (!original.Valid()) 889 break; 890 891 ObjCInterfaceDecl *original_interface_decl = 892 dyn_cast<ObjCInterfaceDecl>(original.decl); 893 894 if (FindObjCMethodDeclsWithOrigin(context, original_interface_decl, 895 "at origin")) 896 return; // found it, no need to look any further 897 } while (false); 898 899 StreamString ss; 900 901 if (decl_name.isObjCZeroArgSelector()) { 902 ss.Printf("%s", decl_name.getAsString().c_str()); 903 } else if (decl_name.isObjCOneArgSelector()) { 904 ss.Printf("%s", decl_name.getAsString().c_str()); 905 } else { 906 clang::Selector sel = decl_name.getObjCSelector(); 907 908 for (unsigned i = 0, e = sel.getNumArgs(); i != e; ++i) { 909 llvm::StringRef r = sel.getNameForSlot(i); 910 ss.Printf("%s:", r.str().c_str()); 911 } 912 } 913 ss.Flush(); 914 915 if (ss.GetString().contains("$__lldb")) 916 return; // we don't need any results 917 918 ConstString selector_name(ss.GetString()); 919 920 LLDB_LOG(log, 921 "ClangASTSource::FindObjCMethodDecls on (ASTContext*){0:x} '{1}' " 922 "for selector [{2} {3}]", 923 m_ast_context, m_clang_ast_context->getDisplayName(), 924 interface_decl->getName(), selector_name); 925 SymbolContextList sc_list; 926 927 ModuleFunctionSearchOptions function_options; 928 function_options.include_symbols = false; 929 function_options.include_inlines = false; 930 931 std::string interface_name = interface_decl->getNameAsString(); 932 933 do { 934 StreamString ms; 935 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString()); 936 ms.Flush(); 937 ConstString instance_method_name(ms.GetString()); 938 939 sc_list.Clear(); 940 m_target->GetImages().FindFunctions(instance_method_name, 941 lldb::eFunctionNameTypeFull, 942 function_options, sc_list); 943 944 if (sc_list.GetSize()) 945 break; 946 947 ms.Clear(); 948 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString()); 949 ms.Flush(); 950 ConstString class_method_name(ms.GetString()); 951 952 sc_list.Clear(); 953 m_target->GetImages().FindFunctions(class_method_name, 954 lldb::eFunctionNameTypeFull, 955 function_options, sc_list); 956 957 if (sc_list.GetSize()) 958 break; 959 960 // Fall back and check for methods in categories. If we find methods this 961 // way, we need to check that they're actually in categories on the desired 962 // class. 963 964 SymbolContextList candidate_sc_list; 965 966 m_target->GetImages().FindFunctions(selector_name, 967 lldb::eFunctionNameTypeSelector, 968 function_options, candidate_sc_list); 969 970 for (const SymbolContext &candidate_sc : candidate_sc_list) { 971 if (!candidate_sc.function) 972 continue; 973 974 const char *candidate_name = candidate_sc.function->GetName().AsCString(); 975 976 const char *cursor = candidate_name; 977 978 if (*cursor != '+' && *cursor != '-') 979 continue; 980 981 ++cursor; 982 983 if (*cursor != '[') 984 continue; 985 986 ++cursor; 987 988 size_t interface_len = interface_name.length(); 989 990 if (strncmp(cursor, interface_name.c_str(), interface_len)) 991 continue; 992 993 cursor += interface_len; 994 995 if (*cursor == ' ' || *cursor == '(') 996 sc_list.Append(candidate_sc); 997 } 998 } while (false); 999 1000 if (sc_list.GetSize()) { 1001 // We found a good function symbol. Use that. 1002 1003 for (const SymbolContext &sc : sc_list) { 1004 if (!sc.function) 1005 continue; 1006 1007 CompilerDeclContext function_decl_ctx = sc.function->GetDeclContext(); 1008 if (!function_decl_ctx) 1009 continue; 1010 1011 ObjCMethodDecl *method_decl = 1012 TypeSystemClang::DeclContextGetAsObjCMethodDecl(function_decl_ctx); 1013 1014 if (!method_decl) 1015 continue; 1016 1017 ObjCInterfaceDecl *found_interface_decl = 1018 method_decl->getClassInterface(); 1019 1020 if (!found_interface_decl) 1021 continue; 1022 1023 if (found_interface_decl->getName() == interface_decl->getName()) { 1024 Decl *copied_decl = CopyDecl(method_decl); 1025 1026 if (!copied_decl) 1027 continue; 1028 1029 ObjCMethodDecl *copied_method_decl = 1030 dyn_cast<ObjCMethodDecl>(copied_decl); 1031 1032 if (!copied_method_decl) 1033 continue; 1034 1035 LLDB_LOG(log, " CAS::FOMD found (in symbols)\n{0}", 1036 ClangUtil::DumpDecl(copied_method_decl)); 1037 1038 context.AddNamedDecl(copied_method_decl); 1039 } 1040 } 1041 1042 return; 1043 } 1044 1045 // Try the debug information. 1046 1047 do { 1048 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface( 1049 const_cast<ObjCInterfaceDecl *>(interface_decl)); 1050 1051 if (!complete_interface_decl) 1052 break; 1053 1054 // We found the complete interface. The runtime never needs to be queried 1055 // in this scenario. 1056 1057 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl( 1058 complete_interface_decl); 1059 1060 if (complete_interface_decl == interface_decl) 1061 break; // already checked this one 1062 1063 LLDB_LOG(log, 1064 "CAS::FOPD trying origin " 1065 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...", 1066 complete_interface_decl, &complete_iface_decl->getASTContext()); 1067 1068 FindObjCMethodDeclsWithOrigin(context, complete_interface_decl, 1069 "in debug info"); 1070 1071 return; 1072 } while (false); 1073 1074 do { 1075 // Check the modules only if the debug information didn't have a complete 1076 // interface. 1077 1078 if (std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor = 1079 GetClangModulesDeclVendor()) { 1080 ConstString interface_name(interface_decl->getNameAsString().c_str()); 1081 bool append = false; 1082 uint32_t max_matches = 1; 1083 std::vector<clang::NamedDecl *> decls; 1084 1085 if (!modules_decl_vendor->FindDecls(interface_name, append, max_matches, 1086 decls)) 1087 break; 1088 1089 ObjCInterfaceDecl *interface_decl_from_modules = 1090 dyn_cast<ObjCInterfaceDecl>(decls[0]); 1091 1092 if (!interface_decl_from_modules) 1093 break; 1094 1095 if (FindObjCMethodDeclsWithOrigin(context, interface_decl_from_modules, 1096 "in modules")) 1097 return; 1098 } 1099 } while (false); 1100 1101 do { 1102 // Check the runtime only if the debug information didn't have a complete 1103 // interface and the modules don't get us anywhere. 1104 1105 lldb::ProcessSP process(m_target->GetProcessSP()); 1106 1107 if (!process) 1108 break; 1109 1110 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process)); 1111 1112 if (!language_runtime) 1113 break; 1114 1115 DeclVendor *decl_vendor = language_runtime->GetDeclVendor(); 1116 1117 if (!decl_vendor) 1118 break; 1119 1120 ConstString interface_name(interface_decl->getNameAsString().c_str()); 1121 bool append = false; 1122 uint32_t max_matches = 1; 1123 std::vector<clang::NamedDecl *> decls; 1124 1125 auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(decl_vendor); 1126 if (!clang_decl_vendor->FindDecls(interface_name, append, max_matches, 1127 decls)) 1128 break; 1129 1130 ObjCInterfaceDecl *runtime_interface_decl = 1131 dyn_cast<ObjCInterfaceDecl>(decls[0]); 1132 1133 if (!runtime_interface_decl) 1134 break; 1135 1136 FindObjCMethodDeclsWithOrigin(context, runtime_interface_decl, 1137 "in runtime"); 1138 } while (false); 1139 } 1140 1141 bool ClangASTSource::FindObjCPropertyAndIvarDeclsWithOrigin( 1142 NameSearchContext &context, 1143 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl) { 1144 Log *log = GetLog(LLDBLog::Expressions); 1145 1146 if (origin_iface_decl.IsInvalid()) 1147 return false; 1148 1149 std::string name_str = context.m_decl_name.getAsString(); 1150 StringRef name(name_str); 1151 IdentifierInfo &name_identifier( 1152 origin_iface_decl->getASTContext().Idents.get(name)); 1153 1154 DeclFromUser<ObjCPropertyDecl> origin_property_decl( 1155 origin_iface_decl->FindPropertyDeclaration( 1156 &name_identifier, ObjCPropertyQueryKind::OBJC_PR_query_instance)); 1157 1158 bool found = false; 1159 1160 if (origin_property_decl.IsValid()) { 1161 DeclFromParser<ObjCPropertyDecl> parser_property_decl( 1162 origin_property_decl.Import(m_ast_context, *m_ast_importer_sp)); 1163 if (parser_property_decl.IsValid()) { 1164 LLDB_LOG(log, " CAS::FOPD found\n{0}", 1165 ClangUtil::DumpDecl(parser_property_decl.decl)); 1166 1167 context.AddNamedDecl(parser_property_decl.decl); 1168 found = true; 1169 } 1170 } 1171 1172 DeclFromUser<ObjCIvarDecl> origin_ivar_decl( 1173 origin_iface_decl->getIvarDecl(&name_identifier)); 1174 1175 if (origin_ivar_decl.IsValid()) { 1176 DeclFromParser<ObjCIvarDecl> parser_ivar_decl( 1177 origin_ivar_decl.Import(m_ast_context, *m_ast_importer_sp)); 1178 if (parser_ivar_decl.IsValid()) { 1179 LLDB_LOG(log, " CAS::FOPD found\n{0}", 1180 ClangUtil::DumpDecl(parser_ivar_decl.decl)); 1181 1182 context.AddNamedDecl(parser_ivar_decl.decl); 1183 found = true; 1184 } 1185 } 1186 1187 return found; 1188 } 1189 1190 void ClangASTSource::FindObjCPropertyAndIvarDecls(NameSearchContext &context) { 1191 Log *log = GetLog(LLDBLog::Expressions); 1192 1193 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl( 1194 cast<ObjCInterfaceDecl>(context.m_decl_context)); 1195 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl( 1196 parser_iface_decl.GetOrigin(*m_ast_importer_sp)); 1197 1198 ConstString class_name(parser_iface_decl->getNameAsString().c_str()); 1199 1200 LLDB_LOG(log, 1201 "ClangASTSource::FindObjCPropertyAndIvarDecls on " 1202 "(ASTContext*){0:x} '{1}' for '{2}.{3}'", 1203 m_ast_context, m_clang_ast_context->getDisplayName(), 1204 parser_iface_decl->getName(), context.m_decl_name.getAsString()); 1205 1206 if (FindObjCPropertyAndIvarDeclsWithOrigin(context, origin_iface_decl)) 1207 return; 1208 1209 LLDB_LOG(log, 1210 "CAS::FOPD couldn't find the property on origin " 1211 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}, searching " 1212 "elsewhere...", 1213 origin_iface_decl.decl, &origin_iface_decl->getASTContext()); 1214 1215 SymbolContext null_sc; 1216 TypeList type_list; 1217 1218 do { 1219 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface( 1220 const_cast<ObjCInterfaceDecl *>(parser_iface_decl.decl)); 1221 1222 if (!complete_interface_decl) 1223 break; 1224 1225 // We found the complete interface. The runtime never needs to be queried 1226 // in this scenario. 1227 1228 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl( 1229 complete_interface_decl); 1230 1231 if (complete_iface_decl.decl == origin_iface_decl.decl) 1232 break; // already checked this one 1233 1234 LLDB_LOG(log, 1235 "CAS::FOPD trying origin " 1236 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...", 1237 complete_iface_decl.decl, &complete_iface_decl->getASTContext()); 1238 1239 FindObjCPropertyAndIvarDeclsWithOrigin(context, complete_iface_decl); 1240 1241 return; 1242 } while (false); 1243 1244 do { 1245 // Check the modules only if the debug information didn't have a complete 1246 // interface. 1247 1248 std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor = 1249 GetClangModulesDeclVendor(); 1250 1251 if (!modules_decl_vendor) 1252 break; 1253 1254 bool append = false; 1255 uint32_t max_matches = 1; 1256 std::vector<clang::NamedDecl *> decls; 1257 1258 if (!modules_decl_vendor->FindDecls(class_name, append, max_matches, decls)) 1259 break; 1260 1261 DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_modules( 1262 dyn_cast<ObjCInterfaceDecl>(decls[0])); 1263 1264 if (!interface_decl_from_modules.IsValid()) 1265 break; 1266 1267 LLDB_LOG(log, 1268 "CAS::FOPD[{0:x}] trying module " 1269 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...", 1270 interface_decl_from_modules.decl, 1271 &interface_decl_from_modules->getASTContext()); 1272 1273 if (FindObjCPropertyAndIvarDeclsWithOrigin(context, 1274 interface_decl_from_modules)) 1275 return; 1276 } while (false); 1277 1278 do { 1279 // Check the runtime only if the debug information didn't have a complete 1280 // interface and nothing was in the modules. 1281 1282 lldb::ProcessSP process(m_target->GetProcessSP()); 1283 1284 if (!process) 1285 return; 1286 1287 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process)); 1288 1289 if (!language_runtime) 1290 return; 1291 1292 DeclVendor *decl_vendor = language_runtime->GetDeclVendor(); 1293 1294 if (!decl_vendor) 1295 break; 1296 1297 bool append = false; 1298 uint32_t max_matches = 1; 1299 std::vector<clang::NamedDecl *> decls; 1300 1301 auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(decl_vendor); 1302 if (!clang_decl_vendor->FindDecls(class_name, append, max_matches, decls)) 1303 break; 1304 1305 DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_runtime( 1306 dyn_cast<ObjCInterfaceDecl>(decls[0])); 1307 1308 if (!interface_decl_from_runtime.IsValid()) 1309 break; 1310 1311 LLDB_LOG(log, 1312 "CAS::FOPD[{0:x}] trying runtime " 1313 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...", 1314 interface_decl_from_runtime.decl, 1315 &interface_decl_from_runtime->getASTContext()); 1316 1317 if (FindObjCPropertyAndIvarDeclsWithOrigin(context, 1318 interface_decl_from_runtime)) 1319 return; 1320 } while (false); 1321 } 1322 1323 void ClangASTSource::LookupInNamespace(NameSearchContext &context) { 1324 const NamespaceDecl *namespace_context = 1325 dyn_cast<NamespaceDecl>(context.m_decl_context); 1326 1327 Log *log = GetLog(LLDBLog::Expressions); 1328 1329 ClangASTImporter::NamespaceMapSP namespace_map = 1330 m_ast_importer_sp->GetNamespaceMap(namespace_context); 1331 1332 LLDB_LOGV(log, " CAS::FEVD Inspecting namespace map {0:x} ({1} entries)", 1333 namespace_map.get(), namespace_map->size()); 1334 1335 if (!namespace_map) 1336 return; 1337 1338 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), 1339 e = namespace_map->end(); 1340 i != e; ++i) { 1341 LLDB_LOG(log, " CAS::FEVD Searching namespace {0} in module {1}", 1342 i->second.GetName(), i->first->GetFileSpec().GetFilename()); 1343 1344 FindExternalVisibleDecls(context, i->first, i->second); 1345 } 1346 } 1347 1348 bool ClangASTSource::layoutRecordType( 1349 const RecordDecl *record, uint64_t &size, uint64_t &alignment, 1350 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets, 1351 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> 1352 &base_offsets, 1353 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> 1354 &virtual_base_offsets) { 1355 return m_ast_importer_sp->importRecordLayoutFromOrigin( 1356 record, size, alignment, field_offsets, base_offsets, 1357 virtual_base_offsets); 1358 } 1359 1360 void ClangASTSource::CompleteNamespaceMap( 1361 ClangASTImporter::NamespaceMapSP &namespace_map, ConstString name, 1362 ClangASTImporter::NamespaceMapSP &parent_map) const { 1363 1364 Log *log = GetLog(LLDBLog::Expressions); 1365 1366 if (log) { 1367 if (parent_map && parent_map->size()) 1368 LLDB_LOG(log, 1369 "CompleteNamespaceMap on (ASTContext*){0:x} '{1}' Searching " 1370 "for namespace {2} in namespace {3}", 1371 m_ast_context, m_clang_ast_context->getDisplayName(), name, 1372 parent_map->begin()->second.GetName()); 1373 else 1374 LLDB_LOG(log, 1375 "CompleteNamespaceMap on (ASTContext*){0} '{1}' Searching " 1376 "for namespace {2}", 1377 m_ast_context, m_clang_ast_context->getDisplayName(), name); 1378 } 1379 1380 if (parent_map) { 1381 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), 1382 e = parent_map->end(); 1383 i != e; ++i) { 1384 CompilerDeclContext found_namespace_decl; 1385 1386 lldb::ModuleSP module_sp = i->first; 1387 CompilerDeclContext module_parent_namespace_decl = i->second; 1388 1389 SymbolFile *symbol_file = module_sp->GetSymbolFile(); 1390 1391 if (!symbol_file) 1392 continue; 1393 1394 found_namespace_decl = 1395 symbol_file->FindNamespace(name, module_parent_namespace_decl); 1396 1397 if (!found_namespace_decl) 1398 continue; 1399 1400 namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>( 1401 module_sp, found_namespace_decl)); 1402 1403 LLDB_LOG(log, " CMN Found namespace {0} in module {1}", name, 1404 module_sp->GetFileSpec().GetFilename()); 1405 } 1406 } else { 1407 CompilerDeclContext null_namespace_decl; 1408 for (lldb::ModuleSP image : m_target->GetImages().Modules()) { 1409 if (!image) 1410 continue; 1411 1412 CompilerDeclContext found_namespace_decl; 1413 1414 SymbolFile *symbol_file = image->GetSymbolFile(); 1415 1416 if (!symbol_file) 1417 continue; 1418 1419 found_namespace_decl = 1420 symbol_file->FindNamespace(name, null_namespace_decl); 1421 1422 if (!found_namespace_decl) 1423 continue; 1424 1425 namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>( 1426 image, found_namespace_decl)); 1427 1428 LLDB_LOG(log, " CMN[{0}] Found namespace {0} in module {1}", name, 1429 image->GetFileSpec().GetFilename()); 1430 } 1431 } 1432 } 1433 1434 NamespaceDecl *ClangASTSource::AddNamespace( 1435 NameSearchContext &context, 1436 ClangASTImporter::NamespaceMapSP &namespace_decls) { 1437 if (!namespace_decls) 1438 return nullptr; 1439 1440 const CompilerDeclContext &namespace_decl = namespace_decls->begin()->second; 1441 1442 clang::ASTContext *src_ast = 1443 TypeSystemClang::DeclContextGetTypeSystemClang(namespace_decl); 1444 if (!src_ast) 1445 return nullptr; 1446 clang::NamespaceDecl *src_namespace_decl = 1447 TypeSystemClang::DeclContextGetAsNamespaceDecl(namespace_decl); 1448 1449 if (!src_namespace_decl) 1450 return nullptr; 1451 1452 Decl *copied_decl = CopyDecl(src_namespace_decl); 1453 1454 if (!copied_decl) 1455 return nullptr; 1456 1457 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl); 1458 1459 if (!copied_namespace_decl) 1460 return nullptr; 1461 1462 context.m_decls.push_back(copied_namespace_decl); 1463 1464 m_ast_importer_sp->RegisterNamespaceMap(copied_namespace_decl, 1465 namespace_decls); 1466 1467 return dyn_cast<NamespaceDecl>(copied_decl); 1468 } 1469 1470 clang::Decl *ClangASTSource::CopyDecl(Decl *src_decl) { 1471 return m_ast_importer_sp->CopyDecl(m_ast_context, src_decl); 1472 } 1473 1474 ClangASTImporter::DeclOrigin ClangASTSource::GetDeclOrigin(const clang::Decl *decl) { 1475 return m_ast_importer_sp->GetDeclOrigin(decl); 1476 } 1477 1478 CompilerType ClangASTSource::GuardedCopyType(const CompilerType &src_type) { 1479 auto ts = src_type.GetTypeSystem(); 1480 auto src_ast = ts.dyn_cast_or_null<TypeSystemClang>(); 1481 if (!src_ast) 1482 return {}; 1483 1484 QualType copied_qual_type = ClangUtil::GetQualType( 1485 m_ast_importer_sp->CopyType(*m_clang_ast_context, src_type)); 1486 1487 if (copied_qual_type.getAsOpaquePtr() && 1488 copied_qual_type->getCanonicalTypeInternal().isNull()) 1489 // this shouldn't happen, but we're hardening because the AST importer 1490 // seems to be generating bad types on occasion. 1491 return {}; 1492 1493 return m_clang_ast_context->GetType(copied_qual_type); 1494 } 1495 1496 std::shared_ptr<ClangModulesDeclVendor> 1497 ClangASTSource::GetClangModulesDeclVendor() { 1498 auto persistent_vars = llvm::cast<ClangPersistentVariables>( 1499 m_target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC)); 1500 return persistent_vars->GetClangModulesDeclVendor(); 1501 } 1502