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