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