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