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/Log.h" 22 #include "clang/AST/ASTContext.h" 23 #include "clang/AST/RecordLayout.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 // We are in the process of destruction, don't create clang ast context on 75 // demand by passing false to 76 // Target::GetScratchTypeSystemClang(create_on_demand). 77 TypeSystemClang *scratch_clang_ast_context = 78 TypeSystemClang::GetScratch(*m_target, false); 79 80 if (!scratch_clang_ast_context) 81 return; 82 83 clang::ASTContext &scratch_ast_context = 84 scratch_clang_ast_context->getASTContext(); 85 86 if (m_ast_context != &scratch_ast_context && m_ast_importer_sp) 87 m_ast_importer_sp->ForgetSource(&scratch_ast_context, m_ast_context); 88 } 89 90 void ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) { 91 if (!m_ast_context) 92 return; 93 94 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage(); 95 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage(); 96 } 97 98 // The core lookup interface. 99 bool ClangASTSource::FindExternalVisibleDeclsByName( 100 const DeclContext *decl_ctx, DeclarationName clang_decl_name) { 101 if (!m_ast_context) { 102 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); 103 return false; 104 } 105 106 std::string decl_name(clang_decl_name.getAsString()); 107 108 switch (clang_decl_name.getNameKind()) { 109 // Normal identifiers. 110 case DeclarationName::Identifier: { 111 clang::IdentifierInfo *identifier_info = 112 clang_decl_name.getAsIdentifierInfo(); 113 114 if (!identifier_info || identifier_info->getBuiltinID() != 0) { 115 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); 116 return false; 117 } 118 } break; 119 120 // Operator names. 121 case DeclarationName::CXXOperatorName: 122 case DeclarationName::CXXLiteralOperatorName: 123 break; 124 125 // Using directives found in this context. 126 // Tell Sema we didn't find any or we'll end up getting asked a *lot*. 127 case DeclarationName::CXXUsingDirective: 128 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); 129 return false; 130 131 case DeclarationName::ObjCZeroArgSelector: 132 case DeclarationName::ObjCOneArgSelector: 133 case DeclarationName::ObjCMultiArgSelector: { 134 llvm::SmallVector<NamedDecl *, 1> method_decls; 135 136 NameSearchContext method_search_context(*m_clang_ast_context, method_decls, 137 clang_decl_name, decl_ctx); 138 139 FindObjCMethodDecls(method_search_context); 140 141 SetExternalVisibleDeclsForName(decl_ctx, clang_decl_name, method_decls); 142 return (method_decls.size() > 0); 143 } 144 // These aren't possible in the global context. 145 case DeclarationName::CXXConstructorName: 146 case DeclarationName::CXXDestructorName: 147 case DeclarationName::CXXConversionFunctionName: 148 case DeclarationName::CXXDeductionGuideName: 149 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); 150 return false; 151 } 152 153 if (!GetLookupsEnabled()) { 154 // Wait until we see a '$' at the start of a name before we start doing any 155 // lookups so we can avoid lookup up all of the builtin types. 156 if (!decl_name.empty() && decl_name[0] == '$') { 157 SetLookupsEnabled(true); 158 } else { 159 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); 160 return false; 161 } 162 } 163 164 ConstString const_decl_name(decl_name.c_str()); 165 166 const char *uniqued_const_decl_name = const_decl_name.GetCString(); 167 if (m_active_lookups.find(uniqued_const_decl_name) != 168 m_active_lookups.end()) { 169 // We are currently looking up this name... 170 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); 171 return false; 172 } 173 m_active_lookups.insert(uniqued_const_decl_name); 174 llvm::SmallVector<NamedDecl *, 4> name_decls; 175 NameSearchContext name_search_context(*m_clang_ast_context, name_decls, 176 clang_decl_name, decl_ctx); 177 FindExternalVisibleDecls(name_search_context); 178 SetExternalVisibleDeclsForName(decl_ctx, clang_decl_name, name_decls); 179 m_active_lookups.erase(uniqued_const_decl_name); 180 return (name_decls.size() != 0); 181 } 182 183 TagDecl *ClangASTSource::FindCompleteType(const TagDecl *decl) { 184 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 185 186 if (const NamespaceDecl *namespace_context = 187 dyn_cast<NamespaceDecl>(decl->getDeclContext())) { 188 ClangASTImporter::NamespaceMapSP namespace_map = 189 m_ast_importer_sp->GetNamespaceMap(namespace_context); 190 191 LLDB_LOGV(log, " CTD Inspecting namespace map{0} ({1} entries)", 192 namespace_map.get(), namespace_map->size()); 193 194 if (!namespace_map) 195 return nullptr; 196 197 for (const ClangASTImporter::NamespaceMapItem &item : *namespace_map) { 198 LLDB_LOG(log, " CTD Searching namespace {0} in module {1}", 199 item.second.GetName(), item.first->GetFileSpec().GetFilename()); 200 201 TypeList types; 202 203 ConstString name(decl->getName()); 204 205 item.first->FindTypesInNamespace(name, item.second, UINT32_MAX, types); 206 207 for (uint32_t ti = 0, te = types.GetSize(); ti != te; ++ti) { 208 lldb::TypeSP type = types.GetTypeAtIndex(ti); 209 210 if (!type) 211 continue; 212 213 CompilerType clang_type(type->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 TypeList types; 234 235 ConstString name(decl->getName()); 236 237 const ModuleList &module_list = m_target->GetImages(); 238 239 bool exact_match = false; 240 llvm::DenseSet<SymbolFile *> searched_symbol_files; 241 module_list.FindTypes(nullptr, name, exact_match, UINT32_MAX, 242 searched_symbol_files, types); 243 244 for (uint32_t ti = 0, te = types.GetSize(); ti != te; ++ti) { 245 lldb::TypeSP type = types.GetTypeAtIndex(ti); 246 247 if (!type) 248 continue; 249 250 CompilerType clang_type(type->GetFullCompilerType()); 251 252 if (!ClangUtil::IsClangType(clang_type)) 253 continue; 254 255 const TagType *tag_type = 256 ClangUtil::GetQualType(clang_type)->getAs<TagType>(); 257 258 if (!tag_type) 259 continue; 260 261 TagDecl *candidate_tag_decl = const_cast<TagDecl *>(tag_type->getDecl()); 262 263 // We have found a type by basename and we need to make sure the decl 264 // contexts are the same before we can try to complete this type with 265 // another 266 if (!TypeSystemClang::DeclsAreEquivalent(const_cast<TagDecl *>(decl), 267 candidate_tag_decl)) 268 continue; 269 270 if (TypeSystemClang::GetCompleteDecl(&candidate_tag_decl->getASTContext(), 271 candidate_tag_decl)) 272 return candidate_tag_decl; 273 } 274 } 275 return nullptr; 276 } 277 278 void ClangASTSource::CompleteType(TagDecl *tag_decl) { 279 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 280 281 if (log) { 282 LLDB_LOG(log, 283 " CompleteTagDecl on (ASTContext*){0} Completing " 284 "(TagDecl*){1} named {2}", 285 m_clang_ast_context->getDisplayName(), tag_decl, 286 tag_decl->getName()); 287 288 LLDB_LOG(log, " CTD Before:\n{0}", ClangUtil::DumpDecl(tag_decl)); 289 } 290 291 auto iter = m_active_lexical_decls.find(tag_decl); 292 if (iter != m_active_lexical_decls.end()) 293 return; 294 m_active_lexical_decls.insert(tag_decl); 295 ScopedLexicalDeclEraser eraser(m_active_lexical_decls, tag_decl); 296 297 if (!m_ast_importer_sp->CompleteTagDecl(tag_decl)) { 298 // We couldn't complete the type. Maybe there's a definition somewhere 299 // else that can be completed. 300 if (TagDecl *alternate = FindCompleteType(tag_decl)) 301 m_ast_importer_sp->CompleteTagDeclWithOrigin(tag_decl, alternate); 302 } 303 304 LLDB_LOG(log, " [CTD] After:\n{0}", ClangUtil::DumpDecl(tag_decl)); 305 } 306 307 void ClangASTSource::CompleteType(clang::ObjCInterfaceDecl *interface_decl) { 308 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 309 310 LLDB_LOG(log, 311 " [CompleteObjCInterfaceDecl] on (ASTContext*){0} '{1}' " 312 "Completing an ObjCInterfaceDecl named {1}", 313 m_ast_context, m_clang_ast_context->getDisplayName(), 314 interface_decl->getName()); 315 LLDB_LOG(log, " [COID] Before:\n{0}", 316 ClangUtil::DumpDecl(interface_decl)); 317 318 ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(interface_decl); 319 320 if (original.Valid()) { 321 if (ObjCInterfaceDecl *original_iface_decl = 322 dyn_cast<ObjCInterfaceDecl>(original.decl)) { 323 ObjCInterfaceDecl *complete_iface_decl = 324 GetCompleteObjCInterface(original_iface_decl); 325 326 if (complete_iface_decl && (complete_iface_decl != original_iface_decl)) { 327 m_ast_importer_sp->SetDeclOrigin(interface_decl, complete_iface_decl); 328 } 329 } 330 } 331 332 m_ast_importer_sp->CompleteObjCInterfaceDecl(interface_decl); 333 334 if (interface_decl->getSuperClass() && 335 interface_decl->getSuperClass() != interface_decl) 336 CompleteType(interface_decl->getSuperClass()); 337 338 LLDB_LOG(log, " [COID] After:"); 339 LLDB_LOG(log, " [COID] {0}", ClangUtil::DumpDecl(interface_decl)); 340 } 341 342 clang::ObjCInterfaceDecl *ClangASTSource::GetCompleteObjCInterface( 343 const clang::ObjCInterfaceDecl *interface_decl) { 344 lldb::ProcessSP process(m_target->GetProcessSP()); 345 346 if (!process) 347 return nullptr; 348 349 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process)); 350 351 if (!language_runtime) 352 return nullptr; 353 354 ConstString class_name(interface_decl->getNameAsString().c_str()); 355 356 lldb::TypeSP complete_type_sp( 357 language_runtime->LookupInCompleteClassCache(class_name)); 358 359 if (!complete_type_sp) 360 return nullptr; 361 362 TypeFromUser complete_type = 363 TypeFromUser(complete_type_sp->GetFullCompilerType()); 364 lldb::opaque_compiler_type_t complete_opaque_type = 365 complete_type.GetOpaqueQualType(); 366 367 if (!complete_opaque_type) 368 return nullptr; 369 370 const clang::Type *complete_clang_type = 371 QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr(); 372 const ObjCInterfaceType *complete_interface_type = 373 dyn_cast<ObjCInterfaceType>(complete_clang_type); 374 375 if (!complete_interface_type) 376 return nullptr; 377 378 ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl()); 379 380 return complete_iface_decl; 381 } 382 383 void ClangASTSource::FindExternalLexicalDecls( 384 const DeclContext *decl_context, 385 llvm::function_ref<bool(Decl::Kind)> predicate, 386 llvm::SmallVectorImpl<Decl *> &decls) { 387 388 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 389 390 const Decl *context_decl = dyn_cast<Decl>(decl_context); 391 392 if (!context_decl) 393 return; 394 395 auto iter = m_active_lexical_decls.find(context_decl); 396 if (iter != m_active_lexical_decls.end()) 397 return; 398 m_active_lexical_decls.insert(context_decl); 399 ScopedLexicalDeclEraser eraser(m_active_lexical_decls, context_decl); 400 401 if (log) { 402 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl)) 403 LLDB_LOG(log, 404 "FindExternalLexicalDecls on (ASTContext*){0} '{1}' in " 405 "'{2}' (%sDecl*){3}", 406 m_ast_context, m_clang_ast_context->getDisplayName(), 407 context_named_decl->getNameAsString().c_str(), 408 context_decl->getDeclKindName(), 409 static_cast<const void *>(context_decl)); 410 else if (context_decl) 411 LLDB_LOG(log, 412 "FindExternalLexicalDecls on (ASTContext*){0} '{1}' in " 413 "({2}Decl*){3}", 414 m_ast_context, m_clang_ast_context->getDisplayName(), 415 context_decl->getDeclKindName(), 416 static_cast<const void *>(context_decl)); 417 else 418 LLDB_LOG(log, 419 "FindExternalLexicalDecls on (ASTContext*){0} '{1}' in a " 420 "NULL context", 421 m_ast_context, m_clang_ast_context->getDisplayName()); 422 } 423 424 ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(context_decl); 425 426 if (!original.Valid()) 427 return; 428 429 LLDB_LOG(log, " FELD Original decl {0} (Decl*){1:x}:\n{2}", 430 static_cast<void *>(original.ctx), 431 static_cast<void *>(original.decl), 432 ClangUtil::DumpDecl(original.decl)); 433 434 if (ObjCInterfaceDecl *original_iface_decl = 435 dyn_cast<ObjCInterfaceDecl>(original.decl)) { 436 ObjCInterfaceDecl *complete_iface_decl = 437 GetCompleteObjCInterface(original_iface_decl); 438 439 if (complete_iface_decl && (complete_iface_decl != original_iface_decl)) { 440 original.decl = complete_iface_decl; 441 original.ctx = &complete_iface_decl->getASTContext(); 442 443 m_ast_importer_sp->SetDeclOrigin(context_decl, complete_iface_decl); 444 } 445 } 446 447 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original.decl)) { 448 ExternalASTSource *external_source = original.ctx->getExternalSource(); 449 450 if (external_source) 451 external_source->CompleteType(original_tag_decl); 452 } 453 454 const DeclContext *original_decl_context = 455 dyn_cast<DeclContext>(original.decl); 456 457 if (!original_decl_context) 458 return; 459 460 // Indicates whether we skipped any Decls of the original DeclContext. 461 bool SkippedDecls = false; 462 for (Decl *decl : original_decl_context->decls()) { 463 // The predicate function returns true if the passed declaration kind is 464 // the one we are looking for. 465 // See clang::ExternalASTSource::FindExternalLexicalDecls() 466 if (predicate(decl->getKind())) { 467 if (log) { 468 std::string ast_dump = ClangUtil::DumpDecl(decl); 469 if (const NamedDecl *context_named_decl = 470 dyn_cast<NamedDecl>(context_decl)) 471 LLDB_LOG(log, " FELD Adding [to {0}Decl {1}] lexical {2}Decl {3}", 472 context_named_decl->getDeclKindName(), 473 context_named_decl->getName(), decl->getDeclKindName(), 474 ast_dump); 475 else 476 LLDB_LOG(log, " FELD Adding lexical {0}Decl {1}", 477 decl->getDeclKindName(), ast_dump); 478 } 479 480 Decl *copied_decl = CopyDecl(decl); 481 482 if (!copied_decl) 483 continue; 484 485 // FIXME: We should add the copied decl to the 'decls' list. This would 486 // add the copied Decl into the DeclContext and make sure that we 487 // correctly propagate that we added some Decls back to Clang. 488 // By leaving 'decls' empty we incorrectly return false from 489 // DeclContext::LoadLexicalDeclsFromExternalStorage which might cause 490 // lookup issues later on. 491 // We can't just add them for now as the ASTImporter already added the 492 // decl into the DeclContext and this would add it twice. 493 494 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl)) { 495 QualType copied_field_type = copied_field->getType(); 496 497 m_ast_importer_sp->RequireCompleteType(copied_field_type); 498 } 499 } else { 500 SkippedDecls = true; 501 } 502 } 503 504 // CopyDecl may build a lookup table which may set up ExternalLexicalStorage 505 // to false. However, since we skipped some of the external Decls we must 506 // set it back! 507 if (SkippedDecls) { 508 decl_context->setHasExternalLexicalStorage(true); 509 // This sets HasLazyExternalLexicalLookups to true. By setting this bit we 510 // ensure that the lookup table is rebuilt, which means the external source 511 // is consulted again when a clang::DeclContext::lookup is called. 512 const_cast<DeclContext *>(decl_context)->setMustBuildLookupTable(); 513 } 514 515 return; 516 } 517 518 void ClangASTSource::FindExternalVisibleDecls(NameSearchContext &context) { 519 assert(m_ast_context); 520 521 const ConstString name(context.m_decl_name.getAsString().c_str()); 522 523 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 524 525 if (log) { 526 if (!context.m_decl_context) 527 LLDB_LOG(log, 528 "ClangASTSource::FindExternalVisibleDecls on " 529 "(ASTContext*){0} '{1}' for '{2}' in a NULL DeclContext", 530 m_ast_context, m_clang_ast_context->getDisplayName(), name); 531 else if (const NamedDecl *context_named_decl = 532 dyn_cast<NamedDecl>(context.m_decl_context)) 533 LLDB_LOG(log, 534 "ClangASTSource::FindExternalVisibleDecls on " 535 "(ASTContext*){0} '{1}' for '{2}' in '{3}'", 536 m_ast_context, m_clang_ast_context->getDisplayName(), name, 537 context_named_decl->getName()); 538 else 539 LLDB_LOG(log, 540 "ClangASTSource::FindExternalVisibleDecls on " 541 "(ASTContext*){0} '{1}' for '{2}' in a '{3}'", 542 m_ast_context, m_clang_ast_context->getDisplayName(), name, 543 context.m_decl_context->getDeclKindName()); 544 } 545 546 if (isa<NamespaceDecl>(context.m_decl_context)) { 547 LookupInNamespace(context); 548 } else if (isa<ObjCInterfaceDecl>(context.m_decl_context)) { 549 FindObjCPropertyAndIvarDecls(context); 550 } else if (!isa<TranslationUnitDecl>(context.m_decl_context)) { 551 // we shouldn't be getting FindExternalVisibleDecls calls for these 552 return; 553 } else { 554 CompilerDeclContext namespace_decl; 555 556 LLDB_LOG(log, " CAS::FEVD Searching the root namespace"); 557 558 FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl); 559 } 560 561 if (!context.m_namespace_map->empty()) { 562 if (log && log->GetVerbose()) 563 LLDB_LOG(log, " CAS::FEVD Registering namespace map {0} ({1} entries)", 564 context.m_namespace_map.get(), context.m_namespace_map->size()); 565 566 NamespaceDecl *clang_namespace_decl = 567 AddNamespace(context, context.m_namespace_map); 568 569 if (clang_namespace_decl) 570 clang_namespace_decl->setHasExternalVisibleStorage(); 571 } 572 } 573 574 clang::Sema *ClangASTSource::getSema() { 575 return m_clang_ast_context->getSema(); 576 } 577 578 bool ClangASTSource::IgnoreName(const ConstString name, 579 bool ignore_all_dollar_names) { 580 static const ConstString id_name("id"); 581 static const ConstString Class_name("Class"); 582 583 if (m_ast_context->getLangOpts().ObjC) 584 if (name == id_name || name == Class_name) 585 return true; 586 587 StringRef name_string_ref = name.GetStringRef(); 588 589 // The ClangASTSource is not responsible for finding $-names. 590 return name_string_ref.empty() || 591 (ignore_all_dollar_names && name_string_ref.startswith("$")) || 592 name_string_ref.startswith("_$"); 593 } 594 595 void ClangASTSource::FindExternalVisibleDecls( 596 NameSearchContext &context, lldb::ModuleSP module_sp, 597 CompilerDeclContext &namespace_decl) { 598 assert(m_ast_context); 599 600 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 601 602 SymbolContextList sc_list; 603 604 const ConstString name(context.m_decl_name.getAsString().c_str()); 605 if (IgnoreName(name, true)) 606 return; 607 608 if (!m_target) 609 return; 610 611 FillNamespaceMap(context, module_sp, namespace_decl); 612 613 if (context.m_found_type) 614 return; 615 616 TypeList types; 617 const bool exact_match = true; 618 llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files; 619 if (module_sp && namespace_decl) 620 module_sp->FindTypesInNamespace(name, namespace_decl, 1, types); 621 else { 622 m_target->GetImages().FindTypes(module_sp.get(), name, exact_match, 1, 623 searched_symbol_files, types); 624 } 625 626 if (size_t num_types = types.GetSize()) { 627 for (size_t ti = 0; ti < num_types; ++ti) { 628 lldb::TypeSP type_sp = types.GetTypeAtIndex(ti); 629 630 if (log) { 631 const char *name_string = type_sp->GetName().GetCString(); 632 633 LLDB_LOG(log, " CAS::FEVD Matching type found for \"{0}\": {1}", name, 634 (name_string ? name_string : "<anonymous>")); 635 } 636 637 CompilerType full_type = type_sp->GetFullCompilerType(); 638 639 CompilerType copied_clang_type(GuardedCopyType(full_type)); 640 641 if (!copied_clang_type) { 642 LLDB_LOG(log, " CAS::FEVD - Couldn't export a type"); 643 644 continue; 645 } 646 647 context.AddTypeDecl(copied_clang_type); 648 649 context.m_found_type = true; 650 break; 651 } 652 } 653 654 if (!context.m_found_type) { 655 // Try the modules next. 656 FindDeclInModules(context, name); 657 } 658 659 if (!context.m_found_type) { 660 FindDeclInObjCRuntime(context, name); 661 } 662 } 663 664 void ClangASTSource::FillNamespaceMap( 665 NameSearchContext &context, lldb::ModuleSP module_sp, 666 const CompilerDeclContext &namespace_decl) { 667 const ConstString name(context.m_decl_name.getAsString().c_str()); 668 if (IgnoreName(name, true)) 669 return; 670 671 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 672 673 if (module_sp && namespace_decl) { 674 CompilerDeclContext found_namespace_decl; 675 676 if (SymbolFile *symbol_file = module_sp->GetSymbolFile()) { 677 found_namespace_decl = symbol_file->FindNamespace(name, namespace_decl); 678 679 if (found_namespace_decl) { 680 context.m_namespace_map->push_back( 681 std::pair<lldb::ModuleSP, CompilerDeclContext>( 682 module_sp, found_namespace_decl)); 683 684 LLDB_LOG(log, " CAS::FEVD Found namespace {0} in module {1}", name, 685 module_sp->GetFileSpec().GetFilename()); 686 } 687 } 688 return; 689 } 690 691 const ModuleList &target_images = m_target->GetImages(); 692 std::lock_guard<std::recursive_mutex> guard(target_images.GetMutex()); 693 694 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i) { 695 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i); 696 697 if (!image) 698 continue; 699 700 CompilerDeclContext found_namespace_decl; 701 702 SymbolFile *symbol_file = image->GetSymbolFile(); 703 704 if (!symbol_file) 705 continue; 706 707 found_namespace_decl = symbol_file->FindNamespace(name, namespace_decl); 708 709 if (found_namespace_decl) { 710 context.m_namespace_map->push_back( 711 std::pair<lldb::ModuleSP, CompilerDeclContext>(image, 712 found_namespace_decl)); 713 714 LLDB_LOG(log, " CAS::FEVD Found namespace {0} in module {1}", name, 715 image->GetFileSpec().GetFilename()); 716 } 717 } 718 } 719 720 template <class D> class TaggedASTDecl { 721 public: 722 TaggedASTDecl() : decl(nullptr) {} 723 TaggedASTDecl(D *_decl) : decl(_decl) {} 724 bool IsValid() const { return (decl != nullptr); } 725 bool IsInvalid() const { return !IsValid(); } 726 D *operator->() const { return decl; } 727 D *decl; 728 }; 729 730 template <class D2, template <class D> class TD, class D1> 731 TD<D2> DynCast(TD<D1> source) { 732 return TD<D2>(dyn_cast<D2>(source.decl)); 733 } 734 735 template <class D = Decl> class DeclFromParser; 736 template <class D = Decl> class DeclFromUser; 737 738 template <class D> class DeclFromParser : public TaggedASTDecl<D> { 739 public: 740 DeclFromParser() : TaggedASTDecl<D>() {} 741 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) {} 742 743 DeclFromUser<D> GetOrigin(ClangASTSource &source); 744 }; 745 746 template <class D> class DeclFromUser : public TaggedASTDecl<D> { 747 public: 748 DeclFromUser() : TaggedASTDecl<D>() {} 749 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) {} 750 751 DeclFromParser<D> Import(ClangASTSource &source); 752 }; 753 754 template <class D> 755 DeclFromUser<D> DeclFromParser<D>::GetOrigin(ClangASTSource &source) { 756 ClangASTImporter::DeclOrigin origin = source.GetDeclOrigin(this->decl); 757 if (!origin.Valid()) 758 return DeclFromUser<D>(); 759 return DeclFromUser<D>(dyn_cast<D>(origin.decl)); 760 } 761 762 template <class D> 763 DeclFromParser<D> DeclFromUser<D>::Import(ClangASTSource &source) { 764 DeclFromParser<> parser_generic_decl(source.CopyDecl(this->decl)); 765 if (parser_generic_decl.IsInvalid()) 766 return DeclFromParser<D>(); 767 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl)); 768 } 769 770 bool ClangASTSource::FindObjCMethodDeclsWithOrigin( 771 NameSearchContext &context, ObjCInterfaceDecl *original_interface_decl, 772 const char *log_info) { 773 const DeclarationName &decl_name(context.m_decl_name); 774 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext(); 775 776 Selector original_selector; 777 778 if (decl_name.isObjCZeroArgSelector()) { 779 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString()); 780 original_selector = original_ctx->Selectors.getSelector(0, &ident); 781 } else if (decl_name.isObjCOneArgSelector()) { 782 const std::string &decl_name_string = decl_name.getAsString(); 783 std::string decl_name_string_without_colon(decl_name_string.c_str(), 784 decl_name_string.length() - 1); 785 IdentifierInfo *ident = 786 &original_ctx->Idents.get(decl_name_string_without_colon); 787 original_selector = original_ctx->Selectors.getSelector(1, &ident); 788 } else { 789 SmallVector<IdentifierInfo *, 4> idents; 790 791 clang::Selector sel = decl_name.getObjCSelector(); 792 793 unsigned num_args = sel.getNumArgs(); 794 795 for (unsigned i = 0; i != num_args; ++i) { 796 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i))); 797 } 798 799 original_selector = 800 original_ctx->Selectors.getSelector(num_args, idents.data()); 801 } 802 803 DeclarationName original_decl_name(original_selector); 804 805 llvm::SmallVector<NamedDecl *, 1> methods; 806 807 TypeSystemClang::GetCompleteDecl(original_ctx, original_interface_decl); 808 809 if (ObjCMethodDecl *instance_method_decl = 810 original_interface_decl->lookupInstanceMethod(original_selector)) { 811 methods.push_back(instance_method_decl); 812 } else if (ObjCMethodDecl *class_method_decl = 813 original_interface_decl->lookupClassMethod( 814 original_selector)) { 815 methods.push_back(class_method_decl); 816 } 817 818 if (methods.empty()) { 819 return false; 820 } 821 822 for (NamedDecl *named_decl : methods) { 823 if (!named_decl) 824 continue; 825 826 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(named_decl); 827 828 if (!result_method) 829 continue; 830 831 Decl *copied_decl = CopyDecl(result_method); 832 833 if (!copied_decl) 834 continue; 835 836 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl); 837 838 if (!copied_method_decl) 839 continue; 840 841 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 842 843 LLDB_LOG(log, " CAS::FOMD found ({0}) {1}", log_info, 844 ClangUtil::DumpDecl(copied_method_decl)); 845 846 context.AddNamedDecl(copied_method_decl); 847 } 848 849 return true; 850 } 851 852 void ClangASTSource::FindDeclInModules(NameSearchContext &context, 853 ConstString name) { 854 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 855 856 ClangModulesDeclVendor *modules_decl_vendor = 857 m_target->GetClangModulesDeclVendor(); 858 if (!modules_decl_vendor) 859 return; 860 861 bool append = false; 862 uint32_t max_matches = 1; 863 std::vector<clang::NamedDecl *> decls; 864 865 if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls)) 866 return; 867 868 LLDB_LOG(log, " CAS::FEVD Matching entity found for \"{0}\" in the modules", 869 name); 870 871 clang::NamedDecl *const decl_from_modules = decls[0]; 872 873 if (llvm::isa<clang::TypeDecl>(decl_from_modules) || 874 llvm::isa<clang::ObjCContainerDecl>(decl_from_modules) || 875 llvm::isa<clang::EnumConstantDecl>(decl_from_modules)) { 876 clang::Decl *copied_decl = CopyDecl(decl_from_modules); 877 clang::NamedDecl *copied_named_decl = 878 copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr; 879 880 if (!copied_named_decl) { 881 LLDB_LOG(log, " CAS::FEVD - Couldn't export a type from the modules"); 882 883 return; 884 } 885 886 context.AddNamedDecl(copied_named_decl); 887 888 context.m_found_type = true; 889 } 890 } 891 892 void ClangASTSource::FindDeclInObjCRuntime(NameSearchContext &context, 893 ConstString name) { 894 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 895 896 lldb::ProcessSP process(m_target->GetProcessSP()); 897 898 if (!process) 899 return; 900 901 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process)); 902 903 if (!language_runtime) 904 return; 905 906 DeclVendor *decl_vendor = language_runtime->GetDeclVendor(); 907 908 if (!decl_vendor) 909 return; 910 911 bool append = false; 912 uint32_t max_matches = 1; 913 std::vector<clang::NamedDecl *> decls; 914 915 auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(decl_vendor); 916 if (!clang_decl_vendor->FindDecls(name, append, max_matches, decls)) 917 return; 918 919 LLDB_LOG(log, " CAS::FEVD Matching type found for \"{0}\" in the runtime", 920 name); 921 922 clang::Decl *copied_decl = CopyDecl(decls[0]); 923 clang::NamedDecl *copied_named_decl = 924 copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr; 925 926 if (!copied_named_decl) { 927 LLDB_LOG(log, " CAS::FEVD - Couldn't export a type from the runtime"); 928 929 return; 930 } 931 932 context.AddNamedDecl(copied_named_decl); 933 } 934 935 void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) { 936 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 937 938 const DeclarationName &decl_name(context.m_decl_name); 939 const DeclContext *decl_ctx(context.m_decl_context); 940 941 const ObjCInterfaceDecl *interface_decl = 942 dyn_cast<ObjCInterfaceDecl>(decl_ctx); 943 944 if (!interface_decl) 945 return; 946 947 do { 948 ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(interface_decl); 949 950 if (!original.Valid()) 951 break; 952 953 ObjCInterfaceDecl *original_interface_decl = 954 dyn_cast<ObjCInterfaceDecl>(original.decl); 955 956 if (FindObjCMethodDeclsWithOrigin(context, original_interface_decl, 957 "at origin")) 958 return; // found it, no need to look any further 959 } while (false); 960 961 StreamString ss; 962 963 if (decl_name.isObjCZeroArgSelector()) { 964 ss.Printf("%s", decl_name.getAsString().c_str()); 965 } else if (decl_name.isObjCOneArgSelector()) { 966 ss.Printf("%s", decl_name.getAsString().c_str()); 967 } else { 968 clang::Selector sel = decl_name.getObjCSelector(); 969 970 for (unsigned i = 0, e = sel.getNumArgs(); i != e; ++i) { 971 llvm::StringRef r = sel.getNameForSlot(i); 972 ss.Printf("%s:", r.str().c_str()); 973 } 974 } 975 ss.Flush(); 976 977 if (ss.GetString().contains("$__lldb")) 978 return; // we don't need any results 979 980 ConstString selector_name(ss.GetString()); 981 982 LLDB_LOG(log, 983 "ClangASTSource::FindObjCMethodDecls on (ASTContext*){0} '{1}' " 984 "for selector [{2} {3}]", 985 m_ast_context, m_clang_ast_context->getDisplayName(), 986 interface_decl->getName(), selector_name); 987 SymbolContextList sc_list; 988 989 const bool include_symbols = false; 990 const bool include_inlines = false; 991 992 std::string interface_name = interface_decl->getNameAsString(); 993 994 do { 995 StreamString ms; 996 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString()); 997 ms.Flush(); 998 ConstString instance_method_name(ms.GetString()); 999 1000 sc_list.Clear(); 1001 m_target->GetImages().FindFunctions( 1002 instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, 1003 include_inlines, sc_list); 1004 1005 if (sc_list.GetSize()) 1006 break; 1007 1008 ms.Clear(); 1009 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString()); 1010 ms.Flush(); 1011 ConstString class_method_name(ms.GetString()); 1012 1013 sc_list.Clear(); 1014 m_target->GetImages().FindFunctions( 1015 class_method_name, lldb::eFunctionNameTypeFull, include_symbols, 1016 include_inlines, sc_list); 1017 1018 if (sc_list.GetSize()) 1019 break; 1020 1021 // Fall back and check for methods in categories. If we find methods this 1022 // way, we need to check that they're actually in categories on the desired 1023 // class. 1024 1025 SymbolContextList candidate_sc_list; 1026 1027 m_target->GetImages().FindFunctions( 1028 selector_name, lldb::eFunctionNameTypeSelector, include_symbols, 1029 include_inlines, candidate_sc_list); 1030 1031 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize(); ci != ce; ++ci) { 1032 SymbolContext candidate_sc; 1033 1034 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc)) 1035 continue; 1036 1037 if (!candidate_sc.function) 1038 continue; 1039 1040 const char *candidate_name = candidate_sc.function->GetName().AsCString(); 1041 1042 const char *cursor = candidate_name; 1043 1044 if (*cursor != '+' && *cursor != '-') 1045 continue; 1046 1047 ++cursor; 1048 1049 if (*cursor != '[') 1050 continue; 1051 1052 ++cursor; 1053 1054 size_t interface_len = interface_name.length(); 1055 1056 if (strncmp(cursor, interface_name.c_str(), interface_len)) 1057 continue; 1058 1059 cursor += interface_len; 1060 1061 if (*cursor == ' ' || *cursor == '(') 1062 sc_list.Append(candidate_sc); 1063 } 1064 } while (false); 1065 1066 if (sc_list.GetSize()) { 1067 // We found a good function symbol. Use that. 1068 1069 for (uint32_t i = 0, e = sc_list.GetSize(); i != e; ++i) { 1070 SymbolContext sc; 1071 1072 if (!sc_list.GetContextAtIndex(i, sc)) 1073 continue; 1074 1075 if (!sc.function) 1076 continue; 1077 1078 CompilerDeclContext function_decl_ctx = sc.function->GetDeclContext(); 1079 if (!function_decl_ctx) 1080 continue; 1081 1082 ObjCMethodDecl *method_decl = 1083 TypeSystemClang::DeclContextGetAsObjCMethodDecl(function_decl_ctx); 1084 1085 if (!method_decl) 1086 continue; 1087 1088 ObjCInterfaceDecl *found_interface_decl = 1089 method_decl->getClassInterface(); 1090 1091 if (!found_interface_decl) 1092 continue; 1093 1094 if (found_interface_decl->getName() == interface_decl->getName()) { 1095 Decl *copied_decl = CopyDecl(method_decl); 1096 1097 if (!copied_decl) 1098 continue; 1099 1100 ObjCMethodDecl *copied_method_decl = 1101 dyn_cast<ObjCMethodDecl>(copied_decl); 1102 1103 if (!copied_method_decl) 1104 continue; 1105 1106 LLDB_LOG(log, " CAS::FOMD found (in symbols)\n{0}", 1107 ClangUtil::DumpDecl(copied_method_decl)); 1108 1109 context.AddNamedDecl(copied_method_decl); 1110 } 1111 } 1112 1113 return; 1114 } 1115 1116 // Try the debug information. 1117 1118 do { 1119 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface( 1120 const_cast<ObjCInterfaceDecl *>(interface_decl)); 1121 1122 if (!complete_interface_decl) 1123 break; 1124 1125 // We found the complete interface. The runtime never needs to be queried 1126 // in this scenario. 1127 1128 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl( 1129 complete_interface_decl); 1130 1131 if (complete_interface_decl == interface_decl) 1132 break; // already checked this one 1133 1134 LLDB_LOG(log, 1135 "CAS::FOPD trying origin " 1136 "(ObjCInterfaceDecl*){0}/(ASTContext*){1}...", 1137 complete_interface_decl, &complete_iface_decl->getASTContext()); 1138 1139 FindObjCMethodDeclsWithOrigin(context, complete_interface_decl, 1140 "in debug info"); 1141 1142 return; 1143 } while (false); 1144 1145 do { 1146 // Check the modules only if the debug information didn't have a complete 1147 // interface. 1148 1149 if (ClangModulesDeclVendor *modules_decl_vendor = 1150 m_target->GetClangModulesDeclVendor()) { 1151 ConstString interface_name(interface_decl->getNameAsString().c_str()); 1152 bool append = false; 1153 uint32_t max_matches = 1; 1154 std::vector<clang::NamedDecl *> decls; 1155 1156 if (!modules_decl_vendor->FindDecls(interface_name, append, max_matches, 1157 decls)) 1158 break; 1159 1160 ObjCInterfaceDecl *interface_decl_from_modules = 1161 dyn_cast<ObjCInterfaceDecl>(decls[0]); 1162 1163 if (!interface_decl_from_modules) 1164 break; 1165 1166 if (FindObjCMethodDeclsWithOrigin(context, interface_decl_from_modules, 1167 "in modules")) 1168 return; 1169 } 1170 } while (false); 1171 1172 do { 1173 // Check the runtime only if the debug information didn't have a complete 1174 // interface and the modules don't get us anywhere. 1175 1176 lldb::ProcessSP process(m_target->GetProcessSP()); 1177 1178 if (!process) 1179 break; 1180 1181 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process)); 1182 1183 if (!language_runtime) 1184 break; 1185 1186 DeclVendor *decl_vendor = language_runtime->GetDeclVendor(); 1187 1188 if (!decl_vendor) 1189 break; 1190 1191 ConstString interface_name(interface_decl->getNameAsString().c_str()); 1192 bool append = false; 1193 uint32_t max_matches = 1; 1194 std::vector<clang::NamedDecl *> decls; 1195 1196 auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(decl_vendor); 1197 if (!clang_decl_vendor->FindDecls(interface_name, append, max_matches, 1198 decls)) 1199 break; 1200 1201 ObjCInterfaceDecl *runtime_interface_decl = 1202 dyn_cast<ObjCInterfaceDecl>(decls[0]); 1203 1204 if (!runtime_interface_decl) 1205 break; 1206 1207 FindObjCMethodDeclsWithOrigin(context, runtime_interface_decl, 1208 "in runtime"); 1209 } while (false); 1210 } 1211 1212 static bool FindObjCPropertyAndIvarDeclsWithOrigin( 1213 NameSearchContext &context, ClangASTSource &source, 1214 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl) { 1215 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1216 1217 if (origin_iface_decl.IsInvalid()) 1218 return false; 1219 1220 std::string name_str = context.m_decl_name.getAsString(); 1221 StringRef name(name_str); 1222 IdentifierInfo &name_identifier( 1223 origin_iface_decl->getASTContext().Idents.get(name)); 1224 1225 DeclFromUser<ObjCPropertyDecl> origin_property_decl( 1226 origin_iface_decl->FindPropertyDeclaration( 1227 &name_identifier, ObjCPropertyQueryKind::OBJC_PR_query_instance)); 1228 1229 bool found = false; 1230 1231 if (origin_property_decl.IsValid()) { 1232 DeclFromParser<ObjCPropertyDecl> parser_property_decl( 1233 origin_property_decl.Import(source)); 1234 if (parser_property_decl.IsValid()) { 1235 LLDB_LOG(log, " CAS::FOPD found\n{0}", 1236 ClangUtil::DumpDecl(parser_property_decl.decl)); 1237 1238 context.AddNamedDecl(parser_property_decl.decl); 1239 found = true; 1240 } 1241 } 1242 1243 DeclFromUser<ObjCIvarDecl> origin_ivar_decl( 1244 origin_iface_decl->getIvarDecl(&name_identifier)); 1245 1246 if (origin_ivar_decl.IsValid()) { 1247 DeclFromParser<ObjCIvarDecl> parser_ivar_decl( 1248 origin_ivar_decl.Import(source)); 1249 if (parser_ivar_decl.IsValid()) { 1250 LLDB_LOG(log, " CAS::FOPD found\n{0}", 1251 ClangUtil::DumpDecl(parser_ivar_decl.decl)); 1252 1253 context.AddNamedDecl(parser_ivar_decl.decl); 1254 found = true; 1255 } 1256 } 1257 1258 return found; 1259 } 1260 1261 void ClangASTSource::FindObjCPropertyAndIvarDecls(NameSearchContext &context) { 1262 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1263 1264 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl( 1265 cast<ObjCInterfaceDecl>(context.m_decl_context)); 1266 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl( 1267 parser_iface_decl.GetOrigin(*this)); 1268 1269 ConstString class_name(parser_iface_decl->getNameAsString().c_str()); 1270 1271 LLDB_LOG(log, 1272 "ClangASTSource::FindObjCPropertyAndIvarDecls on " 1273 "(ASTContext*){0} '{1}' for '{2}.{3}'", 1274 m_ast_context, m_clang_ast_context->getDisplayName(), 1275 parser_iface_decl->getName(), context.m_decl_name.getAsString()); 1276 1277 if (FindObjCPropertyAndIvarDeclsWithOrigin(context, *this, origin_iface_decl)) 1278 return; 1279 1280 LLDB_LOG(log, 1281 "CAS::FOPD couldn't find the property on origin " 1282 "(ObjCInterfaceDecl*){0}/(ASTContext*){1}, searching " 1283 "elsewhere...", 1284 origin_iface_decl.decl, &origin_iface_decl->getASTContext()); 1285 1286 SymbolContext null_sc; 1287 TypeList type_list; 1288 1289 do { 1290 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface( 1291 const_cast<ObjCInterfaceDecl *>(parser_iface_decl.decl)); 1292 1293 if (!complete_interface_decl) 1294 break; 1295 1296 // We found the complete interface. The runtime never needs to be queried 1297 // in this scenario. 1298 1299 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl( 1300 complete_interface_decl); 1301 1302 if (complete_iface_decl.decl == origin_iface_decl.decl) 1303 break; // already checked this one 1304 1305 LLDB_LOG(log, 1306 "CAS::FOPD trying origin " 1307 "(ObjCInterfaceDecl*){0}/(ASTContext*){1}...", 1308 complete_iface_decl.decl, &complete_iface_decl->getASTContext()); 1309 1310 FindObjCPropertyAndIvarDeclsWithOrigin(context, *this, complete_iface_decl); 1311 1312 return; 1313 } while (false); 1314 1315 do { 1316 // Check the modules only if the debug information didn't have a complete 1317 // interface. 1318 1319 ClangModulesDeclVendor *modules_decl_vendor = 1320 m_target->GetClangModulesDeclVendor(); 1321 1322 if (!modules_decl_vendor) 1323 break; 1324 1325 bool append = false; 1326 uint32_t max_matches = 1; 1327 std::vector<clang::NamedDecl *> decls; 1328 1329 if (!modules_decl_vendor->FindDecls(class_name, append, max_matches, decls)) 1330 break; 1331 1332 DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_modules( 1333 dyn_cast<ObjCInterfaceDecl>(decls[0])); 1334 1335 if (!interface_decl_from_modules.IsValid()) 1336 break; 1337 1338 LLDB_LOG(log, 1339 "CAS::FOPD[{0}] trying module " 1340 "(ObjCInterfaceDecl*){0}/(ASTContext*){1}...", 1341 interface_decl_from_modules.decl, 1342 &interface_decl_from_modules->getASTContext()); 1343 1344 if (FindObjCPropertyAndIvarDeclsWithOrigin(context, *this, 1345 interface_decl_from_modules)) 1346 return; 1347 } while (false); 1348 1349 do { 1350 // Check the runtime only if the debug information didn't have a complete 1351 // interface and nothing was in the modules. 1352 1353 lldb::ProcessSP process(m_target->GetProcessSP()); 1354 1355 if (!process) 1356 return; 1357 1358 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process)); 1359 1360 if (!language_runtime) 1361 return; 1362 1363 DeclVendor *decl_vendor = language_runtime->GetDeclVendor(); 1364 1365 if (!decl_vendor) 1366 break; 1367 1368 bool append = false; 1369 uint32_t max_matches = 1; 1370 std::vector<clang::NamedDecl *> decls; 1371 1372 auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(decl_vendor); 1373 if (!clang_decl_vendor->FindDecls(class_name, append, max_matches, decls)) 1374 break; 1375 1376 DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_runtime( 1377 dyn_cast<ObjCInterfaceDecl>(decls[0])); 1378 1379 if (!interface_decl_from_runtime.IsValid()) 1380 break; 1381 1382 LLDB_LOG(log, 1383 "CAS::FOPD[{0}] trying runtime " 1384 "(ObjCInterfaceDecl*){0}/(ASTContext*){1}...", 1385 interface_decl_from_runtime.decl, 1386 &interface_decl_from_runtime->getASTContext()); 1387 1388 if (FindObjCPropertyAndIvarDeclsWithOrigin(context, *this, 1389 interface_decl_from_runtime)) 1390 return; 1391 } while (false); 1392 } 1393 1394 void ClangASTSource::LookupInNamespace(NameSearchContext &context) { 1395 const NamespaceDecl *namespace_context = 1396 dyn_cast<NamespaceDecl>(context.m_decl_context); 1397 1398 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1399 1400 ClangASTImporter::NamespaceMapSP namespace_map = 1401 m_ast_importer_sp->GetNamespaceMap(namespace_context); 1402 1403 LLDB_LOGV(log, " CAS::FEVD Inspecting namespace map {0} ({1} entries)", 1404 namespace_map.get(), namespace_map->size()); 1405 1406 if (!namespace_map) 1407 return; 1408 1409 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), 1410 e = namespace_map->end(); 1411 i != e; ++i) { 1412 LLDB_LOG(log, " CAS::FEVD Searching namespace {0} in module {1}", 1413 i->second.GetName(), i->first->GetFileSpec().GetFilename()); 1414 1415 FindExternalVisibleDecls(context, i->first, i->second); 1416 } 1417 } 1418 1419 typedef llvm::DenseMap<const FieldDecl *, uint64_t> FieldOffsetMap; 1420 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetMap; 1421 1422 template <class D, class O> 1423 static bool ImportOffsetMap(llvm::DenseMap<const D *, O> &destination_map, 1424 llvm::DenseMap<const D *, O> &source_map, 1425 ClangASTSource &source) { 1426 // When importing fields into a new record, clang has a hard requirement that 1427 // fields be imported in field offset order. Since they are stored in a 1428 // DenseMap with a pointer as the key type, this means we cannot simply 1429 // iterate over the map, as the order will be non-deterministic. Instead we 1430 // have to sort by the offset and then insert in sorted order. 1431 typedef llvm::DenseMap<const D *, O> MapType; 1432 typedef typename MapType::value_type PairType; 1433 std::vector<PairType> sorted_items; 1434 sorted_items.reserve(source_map.size()); 1435 sorted_items.assign(source_map.begin(), source_map.end()); 1436 llvm::sort(sorted_items.begin(), sorted_items.end(), 1437 [](const PairType &lhs, const PairType &rhs) { 1438 return lhs.second < rhs.second; 1439 }); 1440 1441 for (const auto &item : sorted_items) { 1442 DeclFromUser<D> user_decl(const_cast<D *>(item.first)); 1443 DeclFromParser<D> parser_decl(user_decl.Import(source)); 1444 if (parser_decl.IsInvalid()) 1445 return false; 1446 destination_map.insert( 1447 std::pair<const D *, O>(parser_decl.decl, item.second)); 1448 } 1449 1450 return true; 1451 } 1452 1453 template <bool IsVirtual> 1454 bool ExtractBaseOffsets(const ASTRecordLayout &record_layout, 1455 DeclFromUser<const CXXRecordDecl> &record, 1456 BaseOffsetMap &base_offsets) { 1457 for (CXXRecordDecl::base_class_const_iterator 1458 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()), 1459 be = (IsVirtual ? record->vbases_end() : record->bases_end()); 1460 bi != be; ++bi) { 1461 if (!IsVirtual && bi->isVirtual()) 1462 continue; 1463 1464 const clang::Type *origin_base_type = bi->getType().getTypePtr(); 1465 const clang::RecordType *origin_base_record_type = 1466 origin_base_type->getAs<RecordType>(); 1467 1468 if (!origin_base_record_type) 1469 return false; 1470 1471 DeclFromUser<RecordDecl> origin_base_record( 1472 origin_base_record_type->getDecl()); 1473 1474 if (origin_base_record.IsInvalid()) 1475 return false; 1476 1477 DeclFromUser<CXXRecordDecl> origin_base_cxx_record( 1478 DynCast<CXXRecordDecl>(origin_base_record)); 1479 1480 if (origin_base_cxx_record.IsInvalid()) 1481 return false; 1482 1483 CharUnits base_offset; 1484 1485 if (IsVirtual) 1486 base_offset = 1487 record_layout.getVBaseClassOffset(origin_base_cxx_record.decl); 1488 else 1489 base_offset = 1490 record_layout.getBaseClassOffset(origin_base_cxx_record.decl); 1491 1492 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>( 1493 origin_base_cxx_record.decl, base_offset)); 1494 } 1495 1496 return true; 1497 } 1498 1499 bool ClangASTSource::layoutRecordType(const RecordDecl *record, uint64_t &size, 1500 uint64_t &alignment, 1501 FieldOffsetMap &field_offsets, 1502 BaseOffsetMap &base_offsets, 1503 BaseOffsetMap &virtual_base_offsets) { 1504 1505 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1506 1507 LLDB_LOG(log, 1508 "LayoutRecordType on (ASTContext*){0} '{1}' for (RecordDecl*)" 1509 "{3} [name = '{4}']", 1510 m_ast_context, m_clang_ast_context->getDisplayName(), record, 1511 record->getName()); 1512 1513 DeclFromParser<const RecordDecl> parser_record(record); 1514 DeclFromUser<const RecordDecl> origin_record( 1515 parser_record.GetOrigin(*this)); 1516 1517 if (origin_record.IsInvalid()) 1518 return false; 1519 1520 FieldOffsetMap origin_field_offsets; 1521 BaseOffsetMap origin_base_offsets; 1522 BaseOffsetMap origin_virtual_base_offsets; 1523 1524 TypeSystemClang::GetCompleteDecl( 1525 &origin_record->getASTContext(), 1526 const_cast<RecordDecl *>(origin_record.decl)); 1527 1528 clang::RecordDecl *definition = origin_record.decl->getDefinition(); 1529 if (!definition || !definition->isCompleteDefinition()) 1530 return false; 1531 1532 const ASTRecordLayout &record_layout( 1533 origin_record->getASTContext().getASTRecordLayout(origin_record.decl)); 1534 1535 int field_idx = 0, field_count = record_layout.getFieldCount(); 1536 1537 for (RecordDecl::field_iterator fi = origin_record->field_begin(), 1538 fe = origin_record->field_end(); 1539 fi != fe; ++fi) { 1540 if (field_idx >= field_count) 1541 return false; // Layout didn't go well. Bail out. 1542 1543 uint64_t field_offset = record_layout.getFieldOffset(field_idx); 1544 1545 origin_field_offsets.insert( 1546 std::pair<const FieldDecl *, uint64_t>(*fi, field_offset)); 1547 1548 field_idx++; 1549 } 1550 1551 lldbassert(&record->getASTContext() == m_ast_context); 1552 1553 DeclFromUser<const CXXRecordDecl> origin_cxx_record( 1554 DynCast<const CXXRecordDecl>(origin_record)); 1555 1556 if (origin_cxx_record.IsValid()) { 1557 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, 1558 origin_base_offsets) || 1559 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, 1560 origin_virtual_base_offsets)) 1561 return false; 1562 } 1563 1564 if (!ImportOffsetMap(field_offsets, origin_field_offsets, *this) || 1565 !ImportOffsetMap(base_offsets, origin_base_offsets, *this) || 1566 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, 1567 *this)) 1568 return false; 1569 1570 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth(); 1571 alignment = record_layout.getAlignment().getQuantity() * 1572 m_ast_context->getCharWidth(); 1573 1574 if (log) { 1575 LLDB_LOG(log, "LRT returned:"); 1576 LLDB_LOG(log, "LRT Original = (RecordDecl*)%p", 1577 static_cast<const void *>(origin_record.decl)); 1578 LLDB_LOG(log, "LRT Size = %" PRId64, size); 1579 LLDB_LOG(log, "LRT Alignment = %" PRId64, alignment); 1580 LLDB_LOG(log, "LRT Fields:"); 1581 for (RecordDecl::field_iterator fi = record->field_begin(), 1582 fe = record->field_end(); 1583 fi != fe; ++fi) { 1584 LLDB_LOG(log, 1585 "LRT (FieldDecl*){0}, Name = '{1}', Offset = {2} bits", 1586 *fi, fi->getName(), field_offsets[*fi]); 1587 } 1588 DeclFromParser<const CXXRecordDecl> parser_cxx_record = 1589 DynCast<const CXXRecordDecl>(parser_record); 1590 if (parser_cxx_record.IsValid()) { 1591 LLDB_LOG(log, "LRT Bases:"); 1592 for (CXXRecordDecl::base_class_const_iterator 1593 bi = parser_cxx_record->bases_begin(), 1594 be = parser_cxx_record->bases_end(); 1595 bi != be; ++bi) { 1596 bool is_virtual = bi->isVirtual(); 1597 1598 QualType base_type = bi->getType(); 1599 const RecordType *base_record_type = base_type->getAs<RecordType>(); 1600 DeclFromParser<RecordDecl> base_record(base_record_type->getDecl()); 1601 DeclFromParser<CXXRecordDecl> base_cxx_record = 1602 DynCast<CXXRecordDecl>(base_record); 1603 1604 LLDB_LOG(log, 1605 "LRT {0}(CXXRecordDecl*){1}, Name = '{2}', Offset = " 1606 "{3} chars", 1607 (is_virtual ? "Virtual " : ""), base_cxx_record.decl, 1608 base_cxx_record.decl->getName(), 1609 (is_virtual 1610 ? virtual_base_offsets[base_cxx_record.decl].getQuantity() 1611 : base_offsets[base_cxx_record.decl].getQuantity())); 1612 } 1613 } else { 1614 LLDB_LOG(log, "LRD Not a CXXRecord, so no bases"); 1615 } 1616 } 1617 1618 return true; 1619 } 1620 1621 void ClangASTSource::CompleteNamespaceMap( 1622 ClangASTImporter::NamespaceMapSP &namespace_map, ConstString name, 1623 ClangASTImporter::NamespaceMapSP &parent_map) const { 1624 1625 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1626 1627 if (log) { 1628 if (parent_map && parent_map->size()) 1629 LLDB_LOG(log, 1630 "CompleteNamespaceMap on (ASTContext*){0} '{1}' Searching " 1631 "for namespace {2} in namespace {3}", 1632 m_ast_context, m_clang_ast_context->getDisplayName(), name, 1633 parent_map->begin()->second.GetName()); 1634 else 1635 LLDB_LOG(log, 1636 "CompleteNamespaceMap on (ASTContext*){0} '{1}' Searching " 1637 "for namespace {2}", 1638 m_ast_context, m_clang_ast_context->getDisplayName(), name); 1639 } 1640 1641 if (parent_map) { 1642 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), 1643 e = parent_map->end(); 1644 i != e; ++i) { 1645 CompilerDeclContext found_namespace_decl; 1646 1647 lldb::ModuleSP module_sp = i->first; 1648 CompilerDeclContext module_parent_namespace_decl = i->second; 1649 1650 SymbolFile *symbol_file = module_sp->GetSymbolFile(); 1651 1652 if (!symbol_file) 1653 continue; 1654 1655 found_namespace_decl = 1656 symbol_file->FindNamespace(name, module_parent_namespace_decl); 1657 1658 if (!found_namespace_decl) 1659 continue; 1660 1661 namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>( 1662 module_sp, found_namespace_decl)); 1663 1664 LLDB_LOG(log, " CMN Found namespace {0} in module {1}", name, 1665 module_sp->GetFileSpec().GetFilename()); 1666 } 1667 } else { 1668 const ModuleList &target_images = m_target->GetImages(); 1669 std::lock_guard<std::recursive_mutex> guard(target_images.GetMutex()); 1670 1671 CompilerDeclContext null_namespace_decl; 1672 1673 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i) { 1674 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i); 1675 1676 if (!image) 1677 continue; 1678 1679 CompilerDeclContext found_namespace_decl; 1680 1681 SymbolFile *symbol_file = image->GetSymbolFile(); 1682 1683 if (!symbol_file) 1684 continue; 1685 1686 found_namespace_decl = 1687 symbol_file->FindNamespace(name, null_namespace_decl); 1688 1689 if (!found_namespace_decl) 1690 continue; 1691 1692 namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>( 1693 image, found_namespace_decl)); 1694 1695 LLDB_LOG(log, " CMN[{0}] Found namespace {0} in module {1}", name, 1696 image->GetFileSpec().GetFilename()); 1697 } 1698 } 1699 } 1700 1701 NamespaceDecl *ClangASTSource::AddNamespace( 1702 NameSearchContext &context, 1703 ClangASTImporter::NamespaceMapSP &namespace_decls) { 1704 if (!namespace_decls) 1705 return nullptr; 1706 1707 const CompilerDeclContext &namespace_decl = namespace_decls->begin()->second; 1708 1709 clang::ASTContext *src_ast = 1710 TypeSystemClang::DeclContextGetTypeSystemClang(namespace_decl); 1711 if (!src_ast) 1712 return nullptr; 1713 clang::NamespaceDecl *src_namespace_decl = 1714 TypeSystemClang::DeclContextGetAsNamespaceDecl(namespace_decl); 1715 1716 if (!src_namespace_decl) 1717 return nullptr; 1718 1719 Decl *copied_decl = CopyDecl(src_namespace_decl); 1720 1721 if (!copied_decl) 1722 return nullptr; 1723 1724 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl); 1725 1726 if (!copied_namespace_decl) 1727 return nullptr; 1728 1729 context.m_decls.push_back(copied_namespace_decl); 1730 1731 m_ast_importer_sp->RegisterNamespaceMap(copied_namespace_decl, 1732 namespace_decls); 1733 1734 return dyn_cast<NamespaceDecl>(copied_decl); 1735 } 1736 1737 clang::Decl *ClangASTSource::CopyDecl(Decl *src_decl) { 1738 return m_ast_importer_sp->CopyDecl(m_ast_context, src_decl); 1739 } 1740 1741 ClangASTImporter::DeclOrigin ClangASTSource::GetDeclOrigin(const clang::Decl *decl) { 1742 return m_ast_importer_sp->GetDeclOrigin(decl); 1743 } 1744 1745 CompilerType ClangASTSource::GuardedCopyType(const CompilerType &src_type) { 1746 TypeSystemClang *src_ast = 1747 llvm::dyn_cast_or_null<TypeSystemClang>(src_type.GetTypeSystem()); 1748 if (src_ast == nullptr) 1749 return CompilerType(); 1750 1751 QualType copied_qual_type = ClangUtil::GetQualType( 1752 m_ast_importer_sp->CopyType(*m_clang_ast_context, src_type)); 1753 1754 if (copied_qual_type.getAsOpaquePtr() && 1755 copied_qual_type->getCanonicalTypeInternal().isNull()) 1756 // this shouldn't happen, but we're hardening because the AST importer 1757 // seems to be generating bad types on occasion. 1758 return CompilerType(); 1759 1760 return m_clang_ast_context->GetType(copied_qual_type); 1761 } 1762