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