1 //===-- ClangASTSource.cpp ---------------------------------------*- C++ 2 //-*-===// 3 // 4 // The LLVM Compiler Infrastructure 5 // 6 // This file is distributed under the University of Illinois Open Source 7 // License. See LICENSE.TXT for details. 8 // 9 //===----------------------------------------------------------------------===// 10 11 #include "ClangASTSource.h" 12 13 #include "ASTDumper.h" 14 #include "ClangModulesDeclVendor.h" 15 16 #include "lldb/Core/Log.h" 17 #include "lldb/Core/Module.h" 18 #include "lldb/Core/ModuleList.h" 19 #include "lldb/Symbol/ClangASTContext.h" 20 #include "lldb/Symbol/ClangUtil.h" 21 #include "lldb/Symbol/CompilerDeclContext.h" 22 #include "lldb/Symbol/Function.h" 23 #include "lldb/Symbol/SymbolFile.h" 24 #include "lldb/Symbol/SymbolVendor.h" 25 #include "lldb/Symbol/TaggedASTType.h" 26 #include "lldb/Target/ObjCLanguageRuntime.h" 27 #include "lldb/Target/Target.h" 28 #include "clang/AST/ASTContext.h" 29 #include "clang/AST/RecordLayout.h" 30 31 #include <vector> 32 33 using namespace clang; 34 using namespace lldb_private; 35 36 //------------------------------------------------------------------ 37 // Scoped class that will remove an active lexical decl from the set 38 // when it goes out of scope. 39 //------------------------------------------------------------------ 40 namespace { 41 class ScopedLexicalDeclEraser { 42 public: 43 ScopedLexicalDeclEraser(std::set<const clang::Decl *> &decls, 44 const clang::Decl *decl) 45 : m_active_lexical_decls(decls), m_decl(decl) {} 46 47 ~ScopedLexicalDeclEraser() { m_active_lexical_decls.erase(m_decl); } 48 49 private: 50 std::set<const clang::Decl *> &m_active_lexical_decls; 51 const clang::Decl *m_decl; 52 }; 53 } 54 55 ClangASTSource::~ClangASTSource() { 56 m_ast_importer_sp->ForgetDestination(m_ast_context); 57 58 // We are in the process of destruction, don't create clang ast context on 59 // demand 60 // by passing false to Target::GetScratchClangASTContext(create_on_demand). 61 ClangASTContext *scratch_clang_ast_context = 62 m_target->GetScratchClangASTContext(false); 63 64 if (!scratch_clang_ast_context) 65 return; 66 67 clang::ASTContext *scratch_ast_context = 68 scratch_clang_ast_context->getASTContext(); 69 70 if (!scratch_ast_context) 71 return; 72 73 if (m_ast_context != scratch_ast_context) 74 m_ast_importer_sp->ForgetSource(scratch_ast_context, m_ast_context); 75 } 76 77 void ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) { 78 if (!m_ast_context) 79 return; 80 81 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage(); 82 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage(); 83 } 84 85 // The core lookup interface. 86 bool ClangASTSource::FindExternalVisibleDeclsByName( 87 const DeclContext *decl_ctx, DeclarationName clang_decl_name) { 88 if (!m_ast_context) { 89 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); 90 return false; 91 } 92 93 if (GetImportInProgress()) { 94 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); 95 return false; 96 } 97 98 std::string decl_name(clang_decl_name.getAsString()); 99 100 // if (m_decl_map.DoingASTImport ()) 101 // return DeclContext::lookup_result(); 102 // 103 switch (clang_decl_name.getNameKind()) { 104 // Normal identifiers. 105 case DeclarationName::Identifier: { 106 clang::IdentifierInfo *identifier_info = 107 clang_decl_name.getAsIdentifierInfo(); 108 109 if (!identifier_info || identifier_info->getBuiltinID() != 0) { 110 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); 111 return false; 112 } 113 } break; 114 115 // Operator names. 116 case DeclarationName::CXXOperatorName: 117 case DeclarationName::CXXLiteralOperatorName: 118 break; 119 120 // Using directives found in this context. 121 // Tell Sema we didn't find any or we'll end up getting asked a *lot*. 122 case DeclarationName::CXXUsingDirective: 123 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); 124 return false; 125 126 case DeclarationName::ObjCZeroArgSelector: 127 case DeclarationName::ObjCOneArgSelector: 128 case DeclarationName::ObjCMultiArgSelector: { 129 llvm::SmallVector<NamedDecl *, 1> method_decls; 130 131 NameSearchContext method_search_context(*this, method_decls, 132 clang_decl_name, decl_ctx); 133 134 FindObjCMethodDecls(method_search_context); 135 136 SetExternalVisibleDeclsForName(decl_ctx, clang_decl_name, method_decls); 137 return (method_decls.size() > 0); 138 } 139 // These aren't possible in the global context. 140 case DeclarationName::CXXConstructorName: 141 case DeclarationName::CXXDestructorName: 142 case DeclarationName::CXXConversionFunctionName: 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, original_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, original_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 void ClangASTSource::FindExternalVisibleDecls( 627 NameSearchContext &context, lldb::ModuleSP module_sp, 628 CompilerDeclContext &namespace_decl, unsigned int current_id) { 629 assert(m_ast_context); 630 631 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 632 633 SymbolContextList sc_list; 634 635 const ConstString name(context.m_decl_name.getAsString().c_str()); 636 637 const char *name_unique_cstr = name.GetCString(); 638 639 static ConstString id_name("id"); 640 static ConstString Class_name("Class"); 641 642 if (name == id_name || name == Class_name) 643 return; 644 645 if (name_unique_cstr == NULL) 646 return; 647 648 // The ClangASTSource is not responsible for finding $-names. 649 if (name_unique_cstr[0] == '$') 650 return; 651 652 if (module_sp && namespace_decl) { 653 CompilerDeclContext found_namespace_decl; 654 655 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(); 656 657 if (symbol_vendor) { 658 SymbolContext null_sc; 659 660 found_namespace_decl = 661 symbol_vendor->FindNamespace(null_sc, name, &namespace_decl); 662 663 if (found_namespace_decl) { 664 context.m_namespace_map->push_back( 665 std::pair<lldb::ModuleSP, CompilerDeclContext>( 666 module_sp, found_namespace_decl)); 667 668 if (log) 669 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s", 670 current_id, name.GetCString(), 671 module_sp->GetFileSpec().GetFilename().GetCString()); 672 } 673 } 674 } else { 675 const ModuleList &target_images = m_target->GetImages(); 676 std::lock_guard<std::recursive_mutex> guard(target_images.GetMutex()); 677 678 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i) { 679 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i); 680 681 if (!image) 682 continue; 683 684 CompilerDeclContext found_namespace_decl; 685 686 SymbolVendor *symbol_vendor = image->GetSymbolVendor(); 687 688 if (!symbol_vendor) 689 continue; 690 691 SymbolContext null_sc; 692 693 found_namespace_decl = 694 symbol_vendor->FindNamespace(null_sc, name, &namespace_decl); 695 696 if (found_namespace_decl) { 697 context.m_namespace_map->push_back( 698 std::pair<lldb::ModuleSP, CompilerDeclContext>( 699 image, found_namespace_decl)); 700 701 if (log) 702 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s", 703 current_id, name.GetCString(), 704 image->GetFileSpec().GetFilename().GetCString()); 705 } 706 } 707 } 708 709 do { 710 if (context.m_found.type) 711 break; 712 713 TypeList types; 714 SymbolContext null_sc; 715 const bool exact_match = false; 716 llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files; 717 if (module_sp && namespace_decl) 718 module_sp->FindTypesInNamespace(null_sc, name, &namespace_decl, 1, types); 719 else 720 m_target->GetImages().FindTypes(null_sc, name, exact_match, 1, 721 searched_symbol_files, types); 722 723 if (size_t num_types = types.GetSize()) { 724 for (size_t ti = 0; ti < num_types; ++ti) { 725 lldb::TypeSP type_sp = types.GetTypeAtIndex(ti); 726 727 if (log) { 728 const char *name_string = type_sp->GetName().GetCString(); 729 730 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s", 731 current_id, name.GetCString(), 732 (name_string ? name_string : "<anonymous>")); 733 } 734 735 CompilerType full_type = type_sp->GetFullCompilerType(); 736 737 CompilerType copied_clang_type(GuardedCopyType(full_type)); 738 739 if (!copied_clang_type) { 740 if (log) 741 log->Printf(" CAS::FEVD[%u] - Couldn't export a type", current_id); 742 743 continue; 744 } 745 746 context.AddTypeDecl(copied_clang_type); 747 748 context.m_found.type = true; 749 break; 750 } 751 } 752 753 if (!context.m_found.type) { 754 // Try the modules next. 755 756 do { 757 if (ClangModulesDeclVendor *modules_decl_vendor = 758 m_target->GetClangModulesDeclVendor()) { 759 bool append = false; 760 uint32_t max_matches = 1; 761 std::vector<clang::NamedDecl *> decls; 762 763 if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls)) 764 break; 765 766 if (log) { 767 log->Printf(" CAS::FEVD[%u] Matching entity found for \"%s\" in " 768 "the modules", 769 current_id, name.GetCString()); 770 } 771 772 clang::NamedDecl *const decl_from_modules = decls[0]; 773 774 if (llvm::isa<clang::TypeDecl>(decl_from_modules) || 775 llvm::isa<clang::ObjCContainerDecl>(decl_from_modules) || 776 llvm::isa<clang::EnumConstantDecl>(decl_from_modules)) { 777 clang::Decl *copied_decl = m_ast_importer_sp->CopyDecl( 778 m_ast_context, &decl_from_modules->getASTContext(), 779 decl_from_modules); 780 clang::NamedDecl *copied_named_decl = 781 copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr; 782 783 if (!copied_named_decl) { 784 if (log) 785 log->Printf( 786 " CAS::FEVD[%u] - Couldn't export a type from the modules", 787 current_id); 788 789 break; 790 } 791 792 context.AddNamedDecl(copied_named_decl); 793 794 context.m_found.type = true; 795 } 796 } 797 } while (0); 798 } 799 800 if (!context.m_found.type) { 801 do { 802 // Couldn't find any types elsewhere. Try the Objective-C runtime if 803 // one exists. 804 805 lldb::ProcessSP process(m_target->GetProcessSP()); 806 807 if (!process) 808 break; 809 810 ObjCLanguageRuntime *language_runtime( 811 process->GetObjCLanguageRuntime()); 812 813 if (!language_runtime) 814 break; 815 816 DeclVendor *decl_vendor = language_runtime->GetDeclVendor(); 817 818 if (!decl_vendor) 819 break; 820 821 bool append = false; 822 uint32_t max_matches = 1; 823 std::vector<clang::NamedDecl *> decls; 824 825 if (!decl_vendor->FindDecls(name, append, max_matches, decls)) 826 break; 827 828 if (log) { 829 log->Printf( 830 " CAS::FEVD[%u] Matching type found for \"%s\" in the runtime", 831 current_id, name.GetCString()); 832 } 833 834 clang::Decl *copied_decl = m_ast_importer_sp->CopyDecl( 835 m_ast_context, &decls[0]->getASTContext(), decls[0]); 836 clang::NamedDecl *copied_named_decl = 837 copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr; 838 839 if (!copied_named_decl) { 840 if (log) 841 log->Printf( 842 " CAS::FEVD[%u] - Couldn't export a type from the runtime", 843 current_id); 844 845 break; 846 } 847 848 context.AddNamedDecl(copied_named_decl); 849 } while (0); 850 } 851 852 } while (0); 853 } 854 855 template <class D> class TaggedASTDecl { 856 public: 857 TaggedASTDecl() : decl(NULL) {} 858 TaggedASTDecl(D *_decl) : decl(_decl) {} 859 bool IsValid() const { return (decl != NULL); } 860 bool IsInvalid() const { return !IsValid(); } 861 D *operator->() const { return decl; } 862 D *decl; 863 }; 864 865 template <class D2, template <class D> class TD, class D1> 866 TD<D2> DynCast(TD<D1> source) { 867 return TD<D2>(dyn_cast<D2>(source.decl)); 868 } 869 870 template <class D = Decl> class DeclFromParser; 871 template <class D = Decl> class DeclFromUser; 872 873 template <class D> class DeclFromParser : public TaggedASTDecl<D> { 874 public: 875 DeclFromParser() : TaggedASTDecl<D>() {} 876 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) {} 877 878 DeclFromUser<D> GetOrigin(ClangASTImporter *importer); 879 }; 880 881 template <class D> class DeclFromUser : public TaggedASTDecl<D> { 882 public: 883 DeclFromUser() : TaggedASTDecl<D>() {} 884 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) {} 885 886 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx); 887 }; 888 889 template <class D> 890 DeclFromUser<D> DeclFromParser<D>::GetOrigin(ClangASTImporter *importer) { 891 DeclFromUser<> origin_decl; 892 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL); 893 if (origin_decl.IsInvalid()) 894 return DeclFromUser<D>(); 895 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl)); 896 } 897 898 template <class D> 899 DeclFromParser<D> DeclFromUser<D>::Import(ClangASTImporter *importer, 900 ASTContext &dest_ctx) { 901 DeclFromParser<> parser_generic_decl( 902 importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl)); 903 if (parser_generic_decl.IsInvalid()) 904 return DeclFromParser<D>(); 905 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl)); 906 } 907 908 static bool FindObjCMethodDeclsWithOrigin( 909 unsigned int current_id, NameSearchContext &context, 910 ObjCInterfaceDecl *original_interface_decl, clang::ASTContext *ast_context, 911 ClangASTImporter *ast_importer, const char *log_info) { 912 const DeclarationName &decl_name(context.m_decl_name); 913 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext(); 914 915 Selector original_selector; 916 917 if (decl_name.isObjCZeroArgSelector()) { 918 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString()); 919 original_selector = original_ctx->Selectors.getSelector(0, &ident); 920 } else if (decl_name.isObjCOneArgSelector()) { 921 const std::string &decl_name_string = decl_name.getAsString(); 922 std::string decl_name_string_without_colon(decl_name_string.c_str(), 923 decl_name_string.length() - 1); 924 IdentifierInfo *ident = 925 &original_ctx->Idents.get(decl_name_string_without_colon.c_str()); 926 original_selector = original_ctx->Selectors.getSelector(1, &ident); 927 } else { 928 SmallVector<IdentifierInfo *, 4> idents; 929 930 clang::Selector sel = decl_name.getObjCSelector(); 931 932 unsigned num_args = sel.getNumArgs(); 933 934 for (unsigned i = 0; i != num_args; ++i) { 935 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i))); 936 } 937 938 original_selector = 939 original_ctx->Selectors.getSelector(num_args, idents.data()); 940 } 941 942 DeclarationName original_decl_name(original_selector); 943 944 llvm::SmallVector<NamedDecl *, 1> methods; 945 946 ClangASTContext::GetCompleteDecl(original_ctx, original_interface_decl); 947 948 if (ObjCMethodDecl *instance_method_decl = 949 original_interface_decl->lookupInstanceMethod(original_selector)) { 950 methods.push_back(instance_method_decl); 951 } else if (ObjCMethodDecl *class_method_decl = 952 original_interface_decl->lookupClassMethod( 953 original_selector)) { 954 methods.push_back(class_method_decl); 955 } 956 957 if (methods.empty()) { 958 return false; 959 } 960 961 for (NamedDecl *named_decl : methods) { 962 if (!named_decl) 963 continue; 964 965 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(named_decl); 966 967 if (!result_method) 968 continue; 969 970 Decl *copied_decl = ast_importer->CopyDecl( 971 ast_context, &result_method->getASTContext(), result_method); 972 973 if (!copied_decl) 974 continue; 975 976 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl); 977 978 if (!copied_method_decl) 979 continue; 980 981 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 982 983 if (log) { 984 ASTDumper dumper((Decl *)copied_method_decl); 985 log->Printf(" CAS::FOMD[%d] found (%s) %s", current_id, log_info, 986 dumper.GetCString()); 987 } 988 989 context.AddNamedDecl(copied_method_decl); 990 } 991 992 return true; 993 } 994 995 void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) { 996 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 997 998 static unsigned int invocation_id = 0; 999 unsigned int current_id = invocation_id++; 1000 1001 const DeclarationName &decl_name(context.m_decl_name); 1002 const DeclContext *decl_ctx(context.m_decl_context); 1003 1004 const ObjCInterfaceDecl *interface_decl = 1005 dyn_cast<ObjCInterfaceDecl>(decl_ctx); 1006 1007 if (!interface_decl) 1008 return; 1009 1010 do { 1011 Decl *original_decl = NULL; 1012 ASTContext *original_ctx = NULL; 1013 1014 m_ast_importer_sp->ResolveDeclOrigin(interface_decl, &original_decl, 1015 &original_ctx); 1016 1017 if (!original_decl) 1018 break; 1019 1020 ObjCInterfaceDecl *original_interface_decl = 1021 dyn_cast<ObjCInterfaceDecl>(original_decl); 1022 1023 if (FindObjCMethodDeclsWithOrigin(current_id, context, 1024 original_interface_decl, m_ast_context, 1025 m_ast_importer_sp.get(), "at origin")) 1026 return; // found it, no need to look any further 1027 } while (0); 1028 1029 StreamString ss; 1030 1031 if (decl_name.isObjCZeroArgSelector()) { 1032 ss.Printf("%s", decl_name.getAsString().c_str()); 1033 } else if (decl_name.isObjCOneArgSelector()) { 1034 ss.Printf("%s", decl_name.getAsString().c_str()); 1035 } else { 1036 clang::Selector sel = decl_name.getObjCSelector(); 1037 1038 for (unsigned i = 0, e = sel.getNumArgs(); i != e; ++i) { 1039 llvm::StringRef r = sel.getNameForSlot(i); 1040 ss.Printf("%s:", r.str().c_str()); 1041 } 1042 } 1043 ss.Flush(); 1044 1045 if (strstr(ss.GetData(), "$__lldb")) 1046 return; // we don't need any results 1047 1048 ConstString selector_name(ss.GetData()); 1049 1050 if (log) 1051 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p " 1052 "for selector [%s %s]", 1053 current_id, static_cast<void *>(m_ast_context), 1054 interface_decl->getNameAsString().c_str(), 1055 selector_name.AsCString()); 1056 SymbolContextList sc_list; 1057 1058 const bool include_symbols = false; 1059 const bool include_inlines = false; 1060 const bool append = false; 1061 1062 std::string interface_name = interface_decl->getNameAsString(); 1063 1064 do { 1065 StreamString ms; 1066 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString()); 1067 ms.Flush(); 1068 ConstString instance_method_name(ms.GetData()); 1069 1070 m_target->GetImages().FindFunctions( 1071 instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, 1072 include_inlines, append, sc_list); 1073 1074 if (sc_list.GetSize()) 1075 break; 1076 1077 ms.Clear(); 1078 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString()); 1079 ms.Flush(); 1080 ConstString class_method_name(ms.GetData()); 1081 1082 m_target->GetImages().FindFunctions( 1083 class_method_name, lldb::eFunctionNameTypeFull, include_symbols, 1084 include_inlines, append, sc_list); 1085 1086 if (sc_list.GetSize()) 1087 break; 1088 1089 // Fall back and check for methods in categories. If we find methods this 1090 // way, we need to check that they're actually in 1091 // categories on the desired class. 1092 1093 SymbolContextList candidate_sc_list; 1094 1095 m_target->GetImages().FindFunctions( 1096 selector_name, lldb::eFunctionNameTypeSelector, include_symbols, 1097 include_inlines, append, candidate_sc_list); 1098 1099 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize(); ci != ce; ++ci) { 1100 SymbolContext candidate_sc; 1101 1102 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc)) 1103 continue; 1104 1105 if (!candidate_sc.function) 1106 continue; 1107 1108 const char *candidate_name = candidate_sc.function->GetName().AsCString(); 1109 1110 const char *cursor = candidate_name; 1111 1112 if (*cursor != '+' && *cursor != '-') 1113 continue; 1114 1115 ++cursor; 1116 1117 if (*cursor != '[') 1118 continue; 1119 1120 ++cursor; 1121 1122 size_t interface_len = interface_name.length(); 1123 1124 if (strncmp(cursor, interface_name.c_str(), interface_len)) 1125 continue; 1126 1127 cursor += interface_len; 1128 1129 if (*cursor == ' ' || *cursor == '(') 1130 sc_list.Append(candidate_sc); 1131 } 1132 } while (0); 1133 1134 if (sc_list.GetSize()) { 1135 // We found a good function symbol. Use that. 1136 1137 for (uint32_t i = 0, e = sc_list.GetSize(); i != e; ++i) { 1138 SymbolContext sc; 1139 1140 if (!sc_list.GetContextAtIndex(i, sc)) 1141 continue; 1142 1143 if (!sc.function) 1144 continue; 1145 1146 CompilerDeclContext function_decl_ctx = sc.function->GetDeclContext(); 1147 if (!function_decl_ctx) 1148 continue; 1149 1150 ObjCMethodDecl *method_decl = 1151 ClangASTContext::DeclContextGetAsObjCMethodDecl(function_decl_ctx); 1152 1153 if (!method_decl) 1154 continue; 1155 1156 ObjCInterfaceDecl *found_interface_decl = 1157 method_decl->getClassInterface(); 1158 1159 if (!found_interface_decl) 1160 continue; 1161 1162 if (found_interface_decl->getName() == interface_decl->getName()) { 1163 Decl *copied_decl = m_ast_importer_sp->CopyDecl( 1164 m_ast_context, &method_decl->getASTContext(), method_decl); 1165 1166 if (!copied_decl) 1167 continue; 1168 1169 ObjCMethodDecl *copied_method_decl = 1170 dyn_cast<ObjCMethodDecl>(copied_decl); 1171 1172 if (!copied_method_decl) 1173 continue; 1174 1175 if (log) { 1176 ASTDumper dumper((Decl *)copied_method_decl); 1177 log->Printf(" CAS::FOMD[%d] found (in symbols) %s", current_id, 1178 dumper.GetCString()); 1179 } 1180 1181 context.AddNamedDecl(copied_method_decl); 1182 } 1183 } 1184 1185 return; 1186 } 1187 1188 // Try the debug information. 1189 1190 do { 1191 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface( 1192 const_cast<ObjCInterfaceDecl *>(interface_decl)); 1193 1194 if (!complete_interface_decl) 1195 break; 1196 1197 // We found the complete interface. The runtime never needs to be queried 1198 // in this scenario. 1199 1200 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl( 1201 complete_interface_decl); 1202 1203 if (complete_interface_decl == interface_decl) 1204 break; // already checked this one 1205 1206 if (log) 1207 log->Printf("CAS::FOPD[%d] trying origin " 1208 "(ObjCInterfaceDecl*)%p/(ASTContext*)%p...", 1209 current_id, static_cast<void *>(complete_interface_decl), 1210 static_cast<void *>(&complete_iface_decl->getASTContext())); 1211 1212 FindObjCMethodDeclsWithOrigin(current_id, context, complete_interface_decl, 1213 m_ast_context, m_ast_importer_sp.get(), 1214 "in debug info"); 1215 1216 return; 1217 } while (0); 1218 1219 do { 1220 // Check the modules only if the debug information didn't have a complete 1221 // interface. 1222 1223 if (ClangModulesDeclVendor *modules_decl_vendor = 1224 m_target->GetClangModulesDeclVendor()) { 1225 ConstString interface_name(interface_decl->getNameAsString().c_str()); 1226 bool append = false; 1227 uint32_t max_matches = 1; 1228 std::vector<clang::NamedDecl *> decls; 1229 1230 if (!modules_decl_vendor->FindDecls(interface_name, append, max_matches, 1231 decls)) 1232 break; 1233 1234 ObjCInterfaceDecl *interface_decl_from_modules = 1235 dyn_cast<ObjCInterfaceDecl>(decls[0]); 1236 1237 if (!interface_decl_from_modules) 1238 break; 1239 1240 if (FindObjCMethodDeclsWithOrigin( 1241 current_id, context, interface_decl_from_modules, m_ast_context, 1242 m_ast_importer_sp.get(), "in modules")) 1243 return; 1244 } 1245 } while (0); 1246 1247 do { 1248 // Check the runtime only if the debug information didn't have a complete 1249 // interface and the modules don't get us anywhere. 1250 1251 lldb::ProcessSP process(m_target->GetProcessSP()); 1252 1253 if (!process) 1254 break; 1255 1256 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime()); 1257 1258 if (!language_runtime) 1259 break; 1260 1261 DeclVendor *decl_vendor = language_runtime->GetDeclVendor(); 1262 1263 if (!decl_vendor) 1264 break; 1265 1266 ConstString interface_name(interface_decl->getNameAsString().c_str()); 1267 bool append = false; 1268 uint32_t max_matches = 1; 1269 std::vector<clang::NamedDecl *> decls; 1270 1271 if (!decl_vendor->FindDecls(interface_name, append, max_matches, decls)) 1272 break; 1273 1274 ObjCInterfaceDecl *runtime_interface_decl = 1275 dyn_cast<ObjCInterfaceDecl>(decls[0]); 1276 1277 if (!runtime_interface_decl) 1278 break; 1279 1280 FindObjCMethodDeclsWithOrigin(current_id, context, runtime_interface_decl, 1281 m_ast_context, m_ast_importer_sp.get(), 1282 "in runtime"); 1283 } while (0); 1284 } 1285 1286 static bool FindObjCPropertyAndIvarDeclsWithOrigin( 1287 unsigned int current_id, NameSearchContext &context, 1288 clang::ASTContext &ast_context, ClangASTImporter *ast_importer, 1289 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl) { 1290 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1291 1292 if (origin_iface_decl.IsInvalid()) 1293 return false; 1294 1295 std::string name_str = context.m_decl_name.getAsString(); 1296 StringRef name(name_str.c_str()); 1297 IdentifierInfo &name_identifier( 1298 origin_iface_decl->getASTContext().Idents.get(name)); 1299 1300 DeclFromUser<ObjCPropertyDecl> origin_property_decl( 1301 origin_iface_decl->FindPropertyDeclaration( 1302 &name_identifier, ObjCPropertyQueryKind::OBJC_PR_query_instance)); 1303 1304 bool found = false; 1305 1306 if (origin_property_decl.IsValid()) { 1307 DeclFromParser<ObjCPropertyDecl> parser_property_decl( 1308 origin_property_decl.Import(ast_importer, ast_context)); 1309 if (parser_property_decl.IsValid()) { 1310 if (log) { 1311 ASTDumper dumper((Decl *)parser_property_decl.decl); 1312 log->Printf(" CAS::FOPD[%d] found %s", current_id, 1313 dumper.GetCString()); 1314 } 1315 1316 context.AddNamedDecl(parser_property_decl.decl); 1317 found = true; 1318 } 1319 } 1320 1321 DeclFromUser<ObjCIvarDecl> origin_ivar_decl( 1322 origin_iface_decl->getIvarDecl(&name_identifier)); 1323 1324 if (origin_ivar_decl.IsValid()) { 1325 DeclFromParser<ObjCIvarDecl> parser_ivar_decl( 1326 origin_ivar_decl.Import(ast_importer, ast_context)); 1327 if (parser_ivar_decl.IsValid()) { 1328 if (log) { 1329 ASTDumper dumper((Decl *)parser_ivar_decl.decl); 1330 log->Printf(" CAS::FOPD[%d] found %s", current_id, 1331 dumper.GetCString()); 1332 } 1333 1334 context.AddNamedDecl(parser_ivar_decl.decl); 1335 found = true; 1336 } 1337 } 1338 1339 return found; 1340 } 1341 1342 void ClangASTSource::FindObjCPropertyAndIvarDecls(NameSearchContext &context) { 1343 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1344 1345 static unsigned int invocation_id = 0; 1346 unsigned int current_id = invocation_id++; 1347 1348 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl( 1349 cast<ObjCInterfaceDecl>(context.m_decl_context)); 1350 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl( 1351 parser_iface_decl.GetOrigin(m_ast_importer_sp.get())); 1352 1353 ConstString class_name(parser_iface_decl->getNameAsString().c_str()); 1354 1355 if (log) 1356 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on " 1357 "(ASTContext*)%p for '%s.%s'", 1358 current_id, static_cast<void *>(m_ast_context), 1359 parser_iface_decl->getNameAsString().c_str(), 1360 context.m_decl_name.getAsString().c_str()); 1361 1362 if (FindObjCPropertyAndIvarDeclsWithOrigin( 1363 current_id, context, *m_ast_context, m_ast_importer_sp.get(), 1364 origin_iface_decl)) 1365 return; 1366 1367 if (log) 1368 log->Printf("CAS::FOPD[%d] couldn't find the property on origin " 1369 "(ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching " 1370 "elsewhere...", 1371 current_id, static_cast<const void *>(origin_iface_decl.decl), 1372 static_cast<void *>(&origin_iface_decl->getASTContext())); 1373 1374 SymbolContext null_sc; 1375 TypeList type_list; 1376 1377 do { 1378 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface( 1379 const_cast<ObjCInterfaceDecl *>(parser_iface_decl.decl)); 1380 1381 if (!complete_interface_decl) 1382 break; 1383 1384 // We found the complete interface. The runtime never needs to be queried 1385 // in this scenario. 1386 1387 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl( 1388 complete_interface_decl); 1389 1390 if (complete_iface_decl.decl == origin_iface_decl.decl) 1391 break; // already checked this one 1392 1393 if (log) 1394 log->Printf("CAS::FOPD[%d] trying origin " 1395 "(ObjCInterfaceDecl*)%p/(ASTContext*)%p...", 1396 current_id, 1397 static_cast<const void *>(complete_iface_decl.decl), 1398 static_cast<void *>(&complete_iface_decl->getASTContext())); 1399 1400 FindObjCPropertyAndIvarDeclsWithOrigin(current_id, context, *m_ast_context, 1401 m_ast_importer_sp.get(), 1402 complete_iface_decl); 1403 1404 return; 1405 } while (0); 1406 1407 do { 1408 // Check the modules only if the debug information didn't have a complete 1409 // interface. 1410 1411 ClangModulesDeclVendor *modules_decl_vendor = 1412 m_target->GetClangModulesDeclVendor(); 1413 1414 if (!modules_decl_vendor) 1415 break; 1416 1417 bool append = false; 1418 uint32_t max_matches = 1; 1419 std::vector<clang::NamedDecl *> decls; 1420 1421 if (!modules_decl_vendor->FindDecls(class_name, append, max_matches, decls)) 1422 break; 1423 1424 DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_modules( 1425 dyn_cast<ObjCInterfaceDecl>(decls[0])); 1426 1427 if (!interface_decl_from_modules.IsValid()) 1428 break; 1429 1430 if (log) 1431 log->Printf( 1432 "CAS::FOPD[%d] trying module " 1433 "(ObjCInterfaceDecl*)%p/(ASTContext*)%p...", 1434 current_id, 1435 static_cast<const void *>(interface_decl_from_modules.decl), 1436 static_cast<void *>(&interface_decl_from_modules->getASTContext())); 1437 1438 if (FindObjCPropertyAndIvarDeclsWithOrigin( 1439 current_id, context, *m_ast_context, m_ast_importer_sp.get(), 1440 interface_decl_from_modules)) 1441 return; 1442 } while (0); 1443 1444 do { 1445 // Check the runtime only if the debug information didn't have a complete 1446 // interface 1447 // and nothing was in the modules. 1448 1449 lldb::ProcessSP process(m_target->GetProcessSP()); 1450 1451 if (!process) 1452 return; 1453 1454 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime()); 1455 1456 if (!language_runtime) 1457 return; 1458 1459 DeclVendor *decl_vendor = language_runtime->GetDeclVendor(); 1460 1461 if (!decl_vendor) 1462 break; 1463 1464 bool append = false; 1465 uint32_t max_matches = 1; 1466 std::vector<clang::NamedDecl *> decls; 1467 1468 if (!decl_vendor->FindDecls(class_name, append, max_matches, decls)) 1469 break; 1470 1471 DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_runtime( 1472 dyn_cast<ObjCInterfaceDecl>(decls[0])); 1473 1474 if (!interface_decl_from_runtime.IsValid()) 1475 break; 1476 1477 if (log) 1478 log->Printf( 1479 "CAS::FOPD[%d] trying runtime " 1480 "(ObjCInterfaceDecl*)%p/(ASTContext*)%p...", 1481 current_id, 1482 static_cast<const void *>(interface_decl_from_runtime.decl), 1483 static_cast<void *>(&interface_decl_from_runtime->getASTContext())); 1484 1485 if (FindObjCPropertyAndIvarDeclsWithOrigin( 1486 current_id, context, *m_ast_context, m_ast_importer_sp.get(), 1487 interface_decl_from_runtime)) 1488 return; 1489 } while (0); 1490 } 1491 1492 typedef llvm::DenseMap<const FieldDecl *, uint64_t> FieldOffsetMap; 1493 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetMap; 1494 1495 template <class D, class O> 1496 static bool ImportOffsetMap(llvm::DenseMap<const D *, O> &destination_map, 1497 llvm::DenseMap<const D *, O> &source_map, 1498 ClangASTImporter *importer, ASTContext &dest_ctx) { 1499 // When importing fields into a new record, clang has a hard requirement that 1500 // fields be imported in field offset order. Since they are stored in a 1501 // DenseMap 1502 // with a pointer as the key type, this means we cannot simply iterate over 1503 // the 1504 // map, as the order will be non-deterministic. Instead we have to sort by 1505 // the offset 1506 // and then insert in sorted order. 1507 typedef llvm::DenseMap<const D *, O> MapType; 1508 typedef typename MapType::value_type PairType; 1509 std::vector<PairType> sorted_items; 1510 sorted_items.reserve(source_map.size()); 1511 sorted_items.assign(source_map.begin(), source_map.end()); 1512 std::sort(sorted_items.begin(), sorted_items.end(), 1513 [](const PairType &lhs, const PairType &rhs) { 1514 return lhs.second < rhs.second; 1515 }); 1516 1517 for (const auto &item : sorted_items) { 1518 DeclFromUser<D> user_decl(const_cast<D *>(item.first)); 1519 DeclFromParser<D> parser_decl(user_decl.Import(importer, dest_ctx)); 1520 if (parser_decl.IsInvalid()) 1521 return false; 1522 destination_map.insert( 1523 std::pair<const D *, O>(parser_decl.decl, item.second)); 1524 } 1525 1526 return true; 1527 } 1528 1529 template <bool IsVirtual> 1530 bool ExtractBaseOffsets(const ASTRecordLayout &record_layout, 1531 DeclFromUser<const CXXRecordDecl> &record, 1532 BaseOffsetMap &base_offsets) { 1533 for (CXXRecordDecl::base_class_const_iterator 1534 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()), 1535 be = (IsVirtual ? record->vbases_end() : record->bases_end()); 1536 bi != be; ++bi) { 1537 if (!IsVirtual && bi->isVirtual()) 1538 continue; 1539 1540 const clang::Type *origin_base_type = bi->getType().getTypePtr(); 1541 const clang::RecordType *origin_base_record_type = 1542 origin_base_type->getAs<RecordType>(); 1543 1544 if (!origin_base_record_type) 1545 return false; 1546 1547 DeclFromUser<RecordDecl> origin_base_record( 1548 origin_base_record_type->getDecl()); 1549 1550 if (origin_base_record.IsInvalid()) 1551 return false; 1552 1553 DeclFromUser<CXXRecordDecl> origin_base_cxx_record( 1554 DynCast<CXXRecordDecl>(origin_base_record)); 1555 1556 if (origin_base_cxx_record.IsInvalid()) 1557 return false; 1558 1559 CharUnits base_offset; 1560 1561 if (IsVirtual) 1562 base_offset = 1563 record_layout.getVBaseClassOffset(origin_base_cxx_record.decl); 1564 else 1565 base_offset = 1566 record_layout.getBaseClassOffset(origin_base_cxx_record.decl); 1567 1568 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>( 1569 origin_base_cxx_record.decl, base_offset)); 1570 } 1571 1572 return true; 1573 } 1574 1575 bool ClangASTSource::layoutRecordType(const RecordDecl *record, uint64_t &size, 1576 uint64_t &alignment, 1577 FieldOffsetMap &field_offsets, 1578 BaseOffsetMap &base_offsets, 1579 BaseOffsetMap &virtual_base_offsets) { 1580 ClangASTMetrics::RegisterRecordLayout(); 1581 1582 static unsigned int invocation_id = 0; 1583 unsigned int current_id = invocation_id++; 1584 1585 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1586 1587 if (log) 1588 log->Printf("LayoutRecordType[%u] on (ASTContext*)%p for (RecordDecl*)%p " 1589 "[name = '%s']", 1590 current_id, static_cast<void *>(m_ast_context), 1591 static_cast<const void *>(record), 1592 record->getNameAsString().c_str()); 1593 1594 DeclFromParser<const RecordDecl> parser_record(record); 1595 DeclFromUser<const RecordDecl> origin_record( 1596 parser_record.GetOrigin(m_ast_importer_sp.get())); 1597 1598 if (origin_record.IsInvalid()) 1599 return false; 1600 1601 FieldOffsetMap origin_field_offsets; 1602 BaseOffsetMap origin_base_offsets; 1603 BaseOffsetMap origin_virtual_base_offsets; 1604 1605 ClangASTContext::GetCompleteDecl( 1606 &origin_record->getASTContext(), 1607 const_cast<RecordDecl *>(origin_record.decl)); 1608 1609 clang::RecordDecl *definition = origin_record.decl->getDefinition(); 1610 if (!definition || !definition->isCompleteDefinition()) 1611 return false; 1612 1613 const ASTRecordLayout &record_layout( 1614 origin_record->getASTContext().getASTRecordLayout(origin_record.decl)); 1615 1616 int field_idx = 0, field_count = record_layout.getFieldCount(); 1617 1618 for (RecordDecl::field_iterator fi = origin_record->field_begin(), 1619 fe = origin_record->field_end(); 1620 fi != fe; ++fi) { 1621 if (field_idx >= field_count) 1622 return false; // Layout didn't go well. Bail out. 1623 1624 uint64_t field_offset = record_layout.getFieldOffset(field_idx); 1625 1626 origin_field_offsets.insert( 1627 std::pair<const FieldDecl *, uint64_t>(*fi, field_offset)); 1628 1629 field_idx++; 1630 } 1631 1632 ASTContext &parser_ast_context(record->getASTContext()); 1633 1634 DeclFromUser<const CXXRecordDecl> origin_cxx_record( 1635 DynCast<const CXXRecordDecl>(origin_record)); 1636 1637 if (origin_cxx_record.IsValid()) { 1638 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, 1639 origin_base_offsets) || 1640 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, 1641 origin_virtual_base_offsets)) 1642 return false; 1643 } 1644 1645 if (!ImportOffsetMap(field_offsets, origin_field_offsets, 1646 m_ast_importer_sp.get(), parser_ast_context) || 1647 !ImportOffsetMap(base_offsets, origin_base_offsets, 1648 m_ast_importer_sp.get(), parser_ast_context) || 1649 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, 1650 m_ast_importer_sp.get(), parser_ast_context)) 1651 return false; 1652 1653 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth(); 1654 alignment = record_layout.getAlignment().getQuantity() * 1655 m_ast_context->getCharWidth(); 1656 1657 if (log) { 1658 log->Printf("LRT[%u] returned:", current_id); 1659 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id, 1660 static_cast<const void *>(origin_record.decl)); 1661 log->Printf("LRT[%u] Size = %" PRId64, current_id, size); 1662 log->Printf("LRT[%u] Alignment = %" PRId64, current_id, alignment); 1663 log->Printf("LRT[%u] Fields:", current_id); 1664 for (RecordDecl::field_iterator fi = record->field_begin(), 1665 fe = record->field_end(); 1666 fi != fe; ++fi) { 1667 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %" PRId64 1668 " bits", 1669 current_id, static_cast<void *>(*fi), 1670 fi->getNameAsString().c_str(), field_offsets[*fi]); 1671 } 1672 DeclFromParser<const CXXRecordDecl> parser_cxx_record = 1673 DynCast<const CXXRecordDecl>(parser_record); 1674 if (parser_cxx_record.IsValid()) { 1675 log->Printf("LRT[%u] Bases:", current_id); 1676 for (CXXRecordDecl::base_class_const_iterator 1677 bi = parser_cxx_record->bases_begin(), 1678 be = parser_cxx_record->bases_end(); 1679 bi != be; ++bi) { 1680 bool is_virtual = bi->isVirtual(); 1681 1682 QualType base_type = bi->getType(); 1683 const RecordType *base_record_type = base_type->getAs<RecordType>(); 1684 DeclFromParser<RecordDecl> base_record(base_record_type->getDecl()); 1685 DeclFromParser<CXXRecordDecl> base_cxx_record = 1686 DynCast<CXXRecordDecl>(base_record); 1687 1688 log->Printf( 1689 "LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %" PRId64 1690 " chars", 1691 current_id, (is_virtual ? "Virtual " : ""), 1692 static_cast<void *>(base_cxx_record.decl), 1693 base_cxx_record.decl->getNameAsString().c_str(), 1694 (is_virtual 1695 ? virtual_base_offsets[base_cxx_record.decl].getQuantity() 1696 : base_offsets[base_cxx_record.decl].getQuantity())); 1697 } 1698 } else { 1699 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id); 1700 } 1701 } 1702 1703 return true; 1704 } 1705 1706 void ClangASTSource::CompleteNamespaceMap( 1707 ClangASTImporter::NamespaceMapSP &namespace_map, const ConstString &name, 1708 ClangASTImporter::NamespaceMapSP &parent_map) const { 1709 static unsigned int invocation_id = 0; 1710 unsigned int current_id = invocation_id++; 1711 1712 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1713 1714 if (log) { 1715 if (parent_map && parent_map->size()) 1716 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for " 1717 "namespace %s in namespace %s", 1718 current_id, static_cast<void *>(m_ast_context), 1719 name.GetCString(), 1720 parent_map->begin()->second.GetName().AsCString()); 1721 else 1722 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for " 1723 "namespace %s", 1724 current_id, static_cast<void *>(m_ast_context), 1725 name.GetCString()); 1726 } 1727 1728 if (parent_map) { 1729 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), 1730 e = parent_map->end(); 1731 i != e; ++i) { 1732 CompilerDeclContext found_namespace_decl; 1733 1734 lldb::ModuleSP module_sp = i->first; 1735 CompilerDeclContext module_parent_namespace_decl = i->second; 1736 1737 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(); 1738 1739 if (!symbol_vendor) 1740 continue; 1741 1742 SymbolContext null_sc; 1743 1744 found_namespace_decl = symbol_vendor->FindNamespace( 1745 null_sc, name, &module_parent_namespace_decl); 1746 1747 if (!found_namespace_decl) 1748 continue; 1749 1750 namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>( 1751 module_sp, found_namespace_decl)); 1752 1753 if (log) 1754 log->Printf(" CMN[%u] Found namespace %s in module %s", current_id, 1755 name.GetCString(), 1756 module_sp->GetFileSpec().GetFilename().GetCString()); 1757 } 1758 } else { 1759 const ModuleList &target_images = m_target->GetImages(); 1760 std::lock_guard<std::recursive_mutex> guard(target_images.GetMutex()); 1761 1762 CompilerDeclContext null_namespace_decl; 1763 1764 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i) { 1765 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i); 1766 1767 if (!image) 1768 continue; 1769 1770 CompilerDeclContext found_namespace_decl; 1771 1772 SymbolVendor *symbol_vendor = image->GetSymbolVendor(); 1773 1774 if (!symbol_vendor) 1775 continue; 1776 1777 SymbolContext null_sc; 1778 1779 found_namespace_decl = 1780 symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl); 1781 1782 if (!found_namespace_decl) 1783 continue; 1784 1785 namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>( 1786 image, found_namespace_decl)); 1787 1788 if (log) 1789 log->Printf(" CMN[%u] Found namespace %s in module %s", current_id, 1790 name.GetCString(), 1791 image->GetFileSpec().GetFilename().GetCString()); 1792 } 1793 } 1794 } 1795 1796 NamespaceDecl *ClangASTSource::AddNamespace( 1797 NameSearchContext &context, 1798 ClangASTImporter::NamespaceMapSP &namespace_decls) { 1799 if (!namespace_decls) 1800 return nullptr; 1801 1802 const CompilerDeclContext &namespace_decl = namespace_decls->begin()->second; 1803 1804 clang::ASTContext *src_ast = 1805 ClangASTContext::DeclContextGetClangASTContext(namespace_decl); 1806 if (!src_ast) 1807 return nullptr; 1808 clang::NamespaceDecl *src_namespace_decl = 1809 ClangASTContext::DeclContextGetAsNamespaceDecl(namespace_decl); 1810 1811 if (!src_namespace_decl) 1812 return nullptr; 1813 1814 Decl *copied_decl = 1815 m_ast_importer_sp->CopyDecl(m_ast_context, src_ast, src_namespace_decl); 1816 1817 if (!copied_decl) 1818 return nullptr; 1819 1820 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl); 1821 1822 if (!copied_namespace_decl) 1823 return nullptr; 1824 1825 context.m_decls.push_back(copied_namespace_decl); 1826 1827 m_ast_importer_sp->RegisterNamespaceMap(copied_namespace_decl, 1828 namespace_decls); 1829 1830 return dyn_cast<NamespaceDecl>(copied_decl); 1831 } 1832 1833 CompilerType ClangASTSource::GuardedCopyType(const CompilerType &src_type) { 1834 ClangASTContext *src_ast = 1835 llvm::dyn_cast_or_null<ClangASTContext>(src_type.GetTypeSystem()); 1836 if (src_ast == nullptr) 1837 return CompilerType(); 1838 1839 ClangASTMetrics::RegisterLLDBImport(); 1840 1841 SetImportInProgress(true); 1842 1843 QualType copied_qual_type = 1844 m_ast_importer_sp->CopyType(m_ast_context, src_ast->getASTContext(), 1845 ClangUtil::GetQualType(src_type)); 1846 1847 SetImportInProgress(false); 1848 1849 if (copied_qual_type.getAsOpaquePtr() && 1850 copied_qual_type->getCanonicalTypeInternal().isNull()) 1851 // this shouldn't happen, but we're hardening because the AST importer seems 1852 // to be generating bad types 1853 // on occasion. 1854 return CompilerType(); 1855 1856 return CompilerType(m_ast_context, copied_qual_type); 1857 } 1858 1859 clang::NamedDecl *NameSearchContext::AddVarDecl(const CompilerType &type) { 1860 assert(type && "Type for variable must be valid!"); 1861 1862 if (!type.IsValid()) 1863 return NULL; 1864 1865 ClangASTContext *lldb_ast = 1866 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); 1867 if (!lldb_ast) 1868 return NULL; 1869 1870 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo(); 1871 1872 clang::ASTContext *ast = lldb_ast->getASTContext(); 1873 1874 clang::NamedDecl *Decl = VarDecl::Create( 1875 *ast, const_cast<DeclContext *>(m_decl_context), SourceLocation(), 1876 SourceLocation(), ii, ClangUtil::GetQualType(type), 0, SC_Static); 1877 m_decls.push_back(Decl); 1878 1879 return Decl; 1880 } 1881 1882 clang::NamedDecl *NameSearchContext::AddFunDecl(const CompilerType &type, 1883 bool extern_c) { 1884 assert(type && "Type for variable must be valid!"); 1885 1886 if (!type.IsValid()) 1887 return NULL; 1888 1889 if (m_function_types.count(type)) 1890 return NULL; 1891 1892 ClangASTContext *lldb_ast = 1893 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); 1894 if (!lldb_ast) 1895 return NULL; 1896 1897 m_function_types.insert(type); 1898 1899 QualType qual_type(ClangUtil::GetQualType(type)); 1900 1901 clang::ASTContext *ast = lldb_ast->getASTContext(); 1902 1903 const bool isInlineSpecified = false; 1904 const bool hasWrittenPrototype = true; 1905 const bool isConstexprSpecified = false; 1906 1907 clang::DeclContext *context = const_cast<DeclContext *>(m_decl_context); 1908 1909 if (extern_c) { 1910 context = LinkageSpecDecl::Create( 1911 *ast, context, SourceLocation(), SourceLocation(), 1912 clang::LinkageSpecDecl::LanguageIDs::lang_c, false); 1913 } 1914 1915 // Pass the identifier info for functions the decl_name is needed for 1916 // operators 1917 clang::DeclarationName decl_name = 1918 m_decl_name.getNameKind() == DeclarationName::Identifier 1919 ? m_decl_name.getAsIdentifierInfo() 1920 : m_decl_name; 1921 1922 clang::FunctionDecl *func_decl = FunctionDecl::Create( 1923 *ast, context, SourceLocation(), SourceLocation(), decl_name, qual_type, 1924 NULL, SC_Extern, isInlineSpecified, hasWrittenPrototype, 1925 isConstexprSpecified); 1926 1927 // We have to do more than just synthesize the FunctionDecl. We have to 1928 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do 1929 // this, we raid the function's FunctionProtoType for types. 1930 1931 const FunctionProtoType *func_proto_type = 1932 qual_type.getTypePtr()->getAs<FunctionProtoType>(); 1933 1934 if (func_proto_type) { 1935 unsigned NumArgs = func_proto_type->getNumParams(); 1936 unsigned ArgIndex; 1937 1938 SmallVector<ParmVarDecl *, 5> parm_var_decls; 1939 1940 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex) { 1941 QualType arg_qual_type(func_proto_type->getParamType(ArgIndex)); 1942 1943 parm_var_decls.push_back(ParmVarDecl::Create( 1944 *ast, const_cast<DeclContext *>(context), SourceLocation(), 1945 SourceLocation(), NULL, arg_qual_type, NULL, SC_Static, NULL)); 1946 } 1947 1948 func_decl->setParams(ArrayRef<ParmVarDecl *>(parm_var_decls)); 1949 } else { 1950 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1951 1952 if (log) 1953 log->Printf("Function type wasn't a FunctionProtoType"); 1954 } 1955 1956 m_decls.push_back(func_decl); 1957 1958 return func_decl; 1959 } 1960 1961 clang::NamedDecl *NameSearchContext::AddGenericFunDecl() { 1962 FunctionProtoType::ExtProtoInfo proto_info; 1963 1964 proto_info.Variadic = true; 1965 1966 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType( 1967 m_ast_source.m_ast_context->UnknownAnyTy, // result 1968 ArrayRef<QualType>(), // argument types 1969 proto_info)); 1970 1971 return AddFunDecl( 1972 CompilerType(m_ast_source.m_ast_context, generic_function_type), true); 1973 } 1974 1975 clang::NamedDecl * 1976 NameSearchContext::AddTypeDecl(const CompilerType &clang_type) { 1977 if (ClangUtil::IsClangType(clang_type)) { 1978 QualType qual_type = ClangUtil::GetQualType(clang_type); 1979 1980 if (const TypedefType *typedef_type = 1981 llvm::dyn_cast<TypedefType>(qual_type)) { 1982 TypedefNameDecl *typedef_name_decl = typedef_type->getDecl(); 1983 1984 m_decls.push_back(typedef_name_decl); 1985 1986 return (NamedDecl *)typedef_name_decl; 1987 } else if (const TagType *tag_type = qual_type->getAs<TagType>()) { 1988 TagDecl *tag_decl = tag_type->getDecl(); 1989 1990 m_decls.push_back(tag_decl); 1991 1992 return tag_decl; 1993 } else if (const ObjCObjectType *objc_object_type = 1994 qual_type->getAs<ObjCObjectType>()) { 1995 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface(); 1996 1997 m_decls.push_back((NamedDecl *)interface_decl); 1998 1999 return (NamedDecl *)interface_decl; 2000 } 2001 } 2002 return NULL; 2003 } 2004 2005 void NameSearchContext::AddLookupResult(clang::DeclContextLookupResult result) { 2006 for (clang::NamedDecl *decl : result) 2007 m_decls.push_back(decl); 2008 } 2009 2010 void NameSearchContext::AddNamedDecl(clang::NamedDecl *decl) { 2011 m_decls.push_back(decl); 2012 } 2013