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