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