1 //===-- ClangExpressionDeclMap.cpp -----------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "ClangExpressionDeclMap.h" 10 11 #include "ClangASTSource.h" 12 #include "ClangModulesDeclVendor.h" 13 #include "ClangPersistentVariables.h" 14 15 #include "lldb/Core/Address.h" 16 #include "lldb/Core/Module.h" 17 #include "lldb/Core/ModuleSpec.h" 18 #include "lldb/Core/ValueObjectConstResult.h" 19 #include "lldb/Core/ValueObjectVariable.h" 20 #include "lldb/Expression/Materializer.h" 21 #include "lldb/Symbol/ClangASTContext.h" 22 #include "lldb/Symbol/ClangUtil.h" 23 #include "lldb/Symbol/CompileUnit.h" 24 #include "lldb/Symbol/CompilerDecl.h" 25 #include "lldb/Symbol/CompilerDeclContext.h" 26 #include "lldb/Symbol/Function.h" 27 #include "lldb/Symbol/ObjectFile.h" 28 #include "lldb/Symbol/SymbolContext.h" 29 #include "lldb/Symbol/SymbolFile.h" 30 #include "lldb/Symbol/SymbolVendor.h" 31 #include "lldb/Symbol/Type.h" 32 #include "lldb/Symbol/TypeList.h" 33 #include "lldb/Symbol/Variable.h" 34 #include "lldb/Symbol/VariableList.h" 35 #include "lldb/Target/ExecutionContext.h" 36 #include "lldb/Target/Process.h" 37 #include "lldb/Target/RegisterContext.h" 38 #include "lldb/Target/StackFrame.h" 39 #include "lldb/Target/Target.h" 40 #include "lldb/Target/Thread.h" 41 #include "lldb/Utility/Endian.h" 42 #include "lldb/Utility/Log.h" 43 #include "lldb/Utility/RegisterValue.h" 44 #include "lldb/Utility/Status.h" 45 #include "lldb/lldb-private.h" 46 #include "clang/AST/ASTConsumer.h" 47 #include "clang/AST/ASTContext.h" 48 #include "clang/AST/ASTImporter.h" 49 #include "clang/AST/Decl.h" 50 #include "clang/AST/DeclarationName.h" 51 #include "clang/AST/RecursiveASTVisitor.h" 52 53 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" 54 #include "Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h" 55 #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" 56 57 using namespace lldb; 58 using namespace lldb_private; 59 using namespace clang; 60 61 namespace { 62 const char *g_lldb_local_vars_namespace_cstr = "$__lldb_local_vars"; 63 } // anonymous namespace 64 65 ClangExpressionDeclMap::ClangExpressionDeclMap( 66 bool keep_result_in_memory, 67 Materializer::PersistentVariableDelegate *result_delegate, 68 ExecutionContext &exe_ctx, ValueObject *ctx_obj) 69 : ClangASTSource(exe_ctx.GetTargetSP()), m_found_entities(), 70 m_struct_members(), m_keep_result_in_memory(keep_result_in_memory), 71 m_result_delegate(result_delegate), m_ctx_obj(ctx_obj), m_parser_vars(), 72 m_struct_vars() { 73 EnableStructVars(); 74 } 75 76 ClangExpressionDeclMap::~ClangExpressionDeclMap() { 77 // Note: The model is now that the parser's AST context and all associated 78 // data does not vanish until the expression has been executed. This means 79 // that valuable lookup data (like namespaces) doesn't vanish, but 80 81 DidParse(); 82 DisableStructVars(); 83 } 84 85 bool ClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx, 86 Materializer *materializer) { 87 ClangASTMetrics::ClearLocalCounters(); 88 89 EnableParserVars(); 90 m_parser_vars->m_exe_ctx = exe_ctx; 91 92 Target *target = exe_ctx.GetTargetPtr(); 93 if (exe_ctx.GetFramePtr()) 94 m_parser_vars->m_sym_ctx = 95 exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything); 96 else if (exe_ctx.GetThreadPtr() && 97 exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)) 98 m_parser_vars->m_sym_ctx = 99 exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext( 100 lldb::eSymbolContextEverything); 101 else if (exe_ctx.GetProcessPtr()) { 102 m_parser_vars->m_sym_ctx.Clear(true); 103 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP(); 104 } else if (target) { 105 m_parser_vars->m_sym_ctx.Clear(true); 106 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP(); 107 } 108 109 if (target) { 110 m_parser_vars->m_persistent_vars = llvm::cast<ClangPersistentVariables>( 111 target->GetPersistentExpressionStateForLanguage(eLanguageTypeC)); 112 113 if (!target->GetScratchClangASTContext()) 114 return false; 115 } 116 117 m_parser_vars->m_target_info = GetTargetInfo(); 118 m_parser_vars->m_materializer = materializer; 119 120 return true; 121 } 122 123 void ClangExpressionDeclMap::InstallCodeGenerator( 124 clang::ASTConsumer *code_gen) { 125 assert(m_parser_vars); 126 m_parser_vars->m_code_gen = code_gen; 127 } 128 129 void ClangExpressionDeclMap::DidParse() { 130 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 131 132 if (log) 133 ClangASTMetrics::DumpCounters(log); 134 135 if (m_parser_vars) { 136 for (size_t entity_index = 0, num_entities = m_found_entities.GetSize(); 137 entity_index < num_entities; ++entity_index) { 138 ExpressionVariableSP var_sp( 139 m_found_entities.GetVariableAtIndex(entity_index)); 140 if (var_sp) 141 llvm::cast<ClangExpressionVariable>(var_sp.get()) 142 ->DisableParserVars(GetParserID()); 143 } 144 145 for (size_t pvar_index = 0, 146 num_pvars = m_parser_vars->m_persistent_vars->GetSize(); 147 pvar_index < num_pvars; ++pvar_index) { 148 ExpressionVariableSP pvar_sp( 149 m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index)); 150 if (ClangExpressionVariable *clang_var = 151 llvm::dyn_cast<ClangExpressionVariable>(pvar_sp.get())) 152 clang_var->DisableParserVars(GetParserID()); 153 } 154 155 DisableParserVars(); 156 } 157 } 158 159 // Interface for IRForTarget 160 161 ClangExpressionDeclMap::TargetInfo ClangExpressionDeclMap::GetTargetInfo() { 162 assert(m_parser_vars.get()); 163 164 TargetInfo ret; 165 166 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx; 167 168 Process *process = exe_ctx.GetProcessPtr(); 169 if (process) { 170 ret.byte_order = process->GetByteOrder(); 171 ret.address_byte_size = process->GetAddressByteSize(); 172 } else { 173 Target *target = exe_ctx.GetTargetPtr(); 174 if (target) { 175 ret.byte_order = target->GetArchitecture().GetByteOrder(); 176 ret.address_byte_size = target->GetArchitecture().GetAddressByteSize(); 177 } 178 } 179 180 return ret; 181 } 182 183 static clang::QualType ExportAllDeclaredTypes( 184 clang::ExternalASTMerger &parent_merger, clang::ExternalASTMerger &merger, 185 clang::ASTContext &source, clang::FileManager &source_file_manager, 186 const clang::ExternalASTMerger::OriginMap &source_origin_map, 187 clang::FileID file, clang::QualType root) { 188 // Mark the source as temporary to make sure all declarations from the 189 // AST are exported. Also add the parent_merger as the merger into the 190 // source AST so that the merger can track back any declarations from 191 // the persistent ASTs we used as sources. 192 clang::ExternalASTMerger::ImporterSource importer_source( 193 source, source_file_manager, source_origin_map, /*Temporary*/ true, 194 &parent_merger); 195 merger.AddSources(importer_source); 196 clang::ASTImporter &exporter = merger.ImporterForOrigin(source); 197 llvm::Expected<clang::QualType> ret_or_error = exporter.Import(root); 198 merger.RemoveSources(importer_source); 199 if (ret_or_error) { 200 return *ret_or_error; 201 } else { 202 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS); 203 LLDB_LOG_ERROR(log, ret_or_error.takeError(), "Couldn't import type: {0}"); 204 return clang::QualType(); 205 } 206 } 207 208 TypeFromUser ClangExpressionDeclMap::DeportType(ClangASTContext &target, 209 ClangASTContext &source, 210 TypeFromParser parser_type) { 211 assert(&target == m_target->GetScratchClangASTContext()); 212 assert((TypeSystem *)&source == parser_type.GetTypeSystem()); 213 assert(source.getASTContext() == m_ast_context); 214 215 if (m_ast_importer_sp) { 216 return TypeFromUser(m_ast_importer_sp->DeportType( 217 target.getASTContext(), source.getASTContext(), 218 parser_type.GetOpaqueQualType()), 219 &target); 220 } else if (m_merger_up) { 221 clang::FileID source_file = 222 source.getASTContext()->getSourceManager().getFileID( 223 source.getASTContext()->getTranslationUnitDecl()->getLocation()); 224 auto scratch_ast_context = static_cast<ClangASTContextForExpressions *>( 225 m_target->GetScratchClangASTContext()); 226 clang::QualType exported_type = ExportAllDeclaredTypes( 227 *m_merger_up.get(), scratch_ast_context->GetMergerUnchecked(), 228 *source.getASTContext(), *source.getFileManager(), 229 m_merger_up->GetOrigins(), source_file, 230 clang::QualType::getFromOpaquePtr(parser_type.GetOpaqueQualType())); 231 return TypeFromUser(exported_type.getAsOpaquePtr(), &target); 232 } else { 233 lldbassert(0 && "No mechanism for deporting a type!"); 234 return TypeFromUser(); 235 } 236 } 237 238 bool ClangExpressionDeclMap::AddPersistentVariable(const NamedDecl *decl, 239 ConstString name, 240 TypeFromParser parser_type, 241 bool is_result, 242 bool is_lvalue) { 243 assert(m_parser_vars.get()); 244 245 ClangASTContext *ast = 246 llvm::dyn_cast_or_null<ClangASTContext>(parser_type.GetTypeSystem()); 247 if (ast == nullptr) 248 return false; 249 250 if (m_parser_vars->m_materializer && is_result) { 251 Status err; 252 253 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx; 254 Target *target = exe_ctx.GetTargetPtr(); 255 if (target == nullptr) 256 return false; 257 258 TypeFromUser user_type = 259 DeportType(*target->GetScratchClangASTContext(), *ast, parser_type); 260 261 uint32_t offset = m_parser_vars->m_materializer->AddResultVariable( 262 user_type, is_lvalue, m_keep_result_in_memory, m_result_delegate, err); 263 264 ClangExpressionVariable *var = new ClangExpressionVariable( 265 exe_ctx.GetBestExecutionContextScope(), name, user_type, 266 m_parser_vars->m_target_info.byte_order, 267 m_parser_vars->m_target_info.address_byte_size); 268 269 m_found_entities.AddNewlyConstructedVariable(var); 270 271 var->EnableParserVars(GetParserID()); 272 273 ClangExpressionVariable::ParserVars *parser_vars = 274 var->GetParserVars(GetParserID()); 275 276 parser_vars->m_named_decl = decl; 277 parser_vars->m_parser_type = parser_type; 278 279 var->EnableJITVars(GetParserID()); 280 281 ClangExpressionVariable::JITVars *jit_vars = var->GetJITVars(GetParserID()); 282 283 jit_vars->m_offset = offset; 284 285 return true; 286 } 287 288 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 289 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx; 290 Target *target = exe_ctx.GetTargetPtr(); 291 if (target == nullptr) 292 return false; 293 294 ClangASTContext *context(target->GetScratchClangASTContext()); 295 296 TypeFromUser user_type = DeportType(*context, *ast, parser_type); 297 298 if (!user_type.GetOpaqueQualType()) { 299 LLDB_LOGF(log, "Persistent variable's type wasn't copied successfully"); 300 return false; 301 } 302 303 if (!m_parser_vars->m_target_info.IsValid()) 304 return false; 305 306 ClangExpressionVariable *var = llvm::cast<ClangExpressionVariable>( 307 m_parser_vars->m_persistent_vars 308 ->CreatePersistentVariable( 309 exe_ctx.GetBestExecutionContextScope(), name, user_type, 310 m_parser_vars->m_target_info.byte_order, 311 m_parser_vars->m_target_info.address_byte_size) 312 .get()); 313 314 if (!var) 315 return false; 316 317 var->m_frozen_sp->SetHasCompleteType(); 318 319 if (is_result) 320 var->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry; 321 else 322 var->m_flags |= 323 ClangExpressionVariable::EVKeepInTarget; // explicitly-declared 324 // persistent variables should 325 // persist 326 327 if (is_lvalue) { 328 var->m_flags |= ClangExpressionVariable::EVIsProgramReference; 329 } else { 330 var->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated; 331 var->m_flags |= ClangExpressionVariable::EVNeedsAllocation; 332 } 333 334 if (m_keep_result_in_memory) { 335 var->m_flags |= ClangExpressionVariable::EVKeepInTarget; 336 } 337 338 LLDB_LOGF(log, "Created persistent variable with flags 0x%hx", var->m_flags); 339 340 var->EnableParserVars(GetParserID()); 341 342 ClangExpressionVariable::ParserVars *parser_vars = 343 var->GetParserVars(GetParserID()); 344 345 parser_vars->m_named_decl = decl; 346 parser_vars->m_parser_type = parser_type; 347 348 return true; 349 } 350 351 bool ClangExpressionDeclMap::AddValueToStruct(const NamedDecl *decl, 352 ConstString name, 353 llvm::Value *value, size_t size, 354 lldb::offset_t alignment) { 355 assert(m_struct_vars.get()); 356 assert(m_parser_vars.get()); 357 358 bool is_persistent_variable = false; 359 360 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 361 362 m_struct_vars->m_struct_laid_out = false; 363 364 if (ClangExpressionVariable::FindVariableInList(m_struct_members, decl, 365 GetParserID())) 366 return true; 367 368 ClangExpressionVariable *var(ClangExpressionVariable::FindVariableInList( 369 m_found_entities, decl, GetParserID())); 370 371 if (!var) { 372 var = ClangExpressionVariable::FindVariableInList( 373 *m_parser_vars->m_persistent_vars, decl, GetParserID()); 374 is_persistent_variable = true; 375 } 376 377 if (!var) 378 return false; 379 380 LLDB_LOGF(log, "Adding value for (NamedDecl*)%p [%s - %s] to the structure", 381 static_cast<const void *>(decl), name.GetCString(), 382 var->GetName().GetCString()); 383 384 // We know entity->m_parser_vars is valid because we used a parser variable 385 // to find it 386 387 ClangExpressionVariable::ParserVars *parser_vars = 388 llvm::cast<ClangExpressionVariable>(var)->GetParserVars(GetParserID()); 389 390 parser_vars->m_llvm_value = value; 391 392 if (ClangExpressionVariable::JITVars *jit_vars = 393 llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID())) { 394 // We already laid this out; do not touch 395 396 LLDB_LOGF(log, "Already placed at 0x%llx", 397 (unsigned long long)jit_vars->m_offset); 398 } 399 400 llvm::cast<ClangExpressionVariable>(var)->EnableJITVars(GetParserID()); 401 402 ClangExpressionVariable::JITVars *jit_vars = 403 llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID()); 404 405 jit_vars->m_alignment = alignment; 406 jit_vars->m_size = size; 407 408 m_struct_members.AddVariable(var->shared_from_this()); 409 410 if (m_parser_vars->m_materializer) { 411 uint32_t offset = 0; 412 413 Status err; 414 415 if (is_persistent_variable) { 416 ExpressionVariableSP var_sp(var->shared_from_this()); 417 offset = m_parser_vars->m_materializer->AddPersistentVariable( 418 var_sp, nullptr, err); 419 } else { 420 if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym) 421 offset = m_parser_vars->m_materializer->AddSymbol(*sym, err); 422 else if (const RegisterInfo *reg_info = var->GetRegisterInfo()) 423 offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err); 424 else if (parser_vars->m_lldb_var) 425 offset = m_parser_vars->m_materializer->AddVariable( 426 parser_vars->m_lldb_var, err); 427 } 428 429 if (!err.Success()) 430 return false; 431 432 LLDB_LOGF(log, "Placed at 0x%llx", (unsigned long long)offset); 433 434 jit_vars->m_offset = 435 offset; // TODO DoStructLayout() should not change this. 436 } 437 438 return true; 439 } 440 441 bool ClangExpressionDeclMap::DoStructLayout() { 442 assert(m_struct_vars.get()); 443 444 if (m_struct_vars->m_struct_laid_out) 445 return true; 446 447 if (!m_parser_vars->m_materializer) 448 return false; 449 450 m_struct_vars->m_struct_alignment = 451 m_parser_vars->m_materializer->GetStructAlignment(); 452 m_struct_vars->m_struct_size = 453 m_parser_vars->m_materializer->GetStructByteSize(); 454 m_struct_vars->m_struct_laid_out = true; 455 return true; 456 } 457 458 bool ClangExpressionDeclMap::GetStructInfo(uint32_t &num_elements, size_t &size, 459 lldb::offset_t &alignment) { 460 assert(m_struct_vars.get()); 461 462 if (!m_struct_vars->m_struct_laid_out) 463 return false; 464 465 num_elements = m_struct_members.GetSize(); 466 size = m_struct_vars->m_struct_size; 467 alignment = m_struct_vars->m_struct_alignment; 468 469 return true; 470 } 471 472 bool ClangExpressionDeclMap::GetStructElement(const NamedDecl *&decl, 473 llvm::Value *&value, 474 lldb::offset_t &offset, 475 ConstString &name, 476 uint32_t index) { 477 assert(m_struct_vars.get()); 478 479 if (!m_struct_vars->m_struct_laid_out) 480 return false; 481 482 if (index >= m_struct_members.GetSize()) 483 return false; 484 485 ExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index)); 486 487 if (!member_sp) 488 return false; 489 490 ClangExpressionVariable::ParserVars *parser_vars = 491 llvm::cast<ClangExpressionVariable>(member_sp.get()) 492 ->GetParserVars(GetParserID()); 493 ClangExpressionVariable::JITVars *jit_vars = 494 llvm::cast<ClangExpressionVariable>(member_sp.get()) 495 ->GetJITVars(GetParserID()); 496 497 if (!parser_vars || !jit_vars || !member_sp->GetValueObject()) 498 return false; 499 500 decl = parser_vars->m_named_decl; 501 value = parser_vars->m_llvm_value; 502 offset = jit_vars->m_offset; 503 name = member_sp->GetName(); 504 505 return true; 506 } 507 508 bool ClangExpressionDeclMap::GetFunctionInfo(const NamedDecl *decl, 509 uint64_t &ptr) { 510 ClangExpressionVariable *entity(ClangExpressionVariable::FindVariableInList( 511 m_found_entities, decl, GetParserID())); 512 513 if (!entity) 514 return false; 515 516 // We know m_parser_vars is valid since we searched for the variable by its 517 // NamedDecl 518 519 ClangExpressionVariable::ParserVars *parser_vars = 520 entity->GetParserVars(GetParserID()); 521 522 ptr = parser_vars->m_lldb_value.GetScalar().ULongLong(); 523 524 return true; 525 } 526 527 addr_t ClangExpressionDeclMap::GetSymbolAddress(Target &target, 528 Process *process, 529 ConstString name, 530 lldb::SymbolType symbol_type, 531 lldb_private::Module *module) { 532 SymbolContextList sc_list; 533 534 if (module) 535 module->FindSymbolsWithNameAndType(name, symbol_type, sc_list); 536 else 537 target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list); 538 539 const uint32_t num_matches = sc_list.GetSize(); 540 addr_t symbol_load_addr = LLDB_INVALID_ADDRESS; 541 542 for (uint32_t i = 0; 543 i < num_matches && 544 (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS); 545 i++) { 546 SymbolContext sym_ctx; 547 sc_list.GetContextAtIndex(i, sym_ctx); 548 549 const Address sym_address = sym_ctx.symbol->GetAddress(); 550 551 if (!sym_address.IsValid()) 552 continue; 553 554 switch (sym_ctx.symbol->GetType()) { 555 case eSymbolTypeCode: 556 case eSymbolTypeTrampoline: 557 symbol_load_addr = sym_address.GetCallableLoadAddress(&target); 558 break; 559 560 case eSymbolTypeResolver: 561 symbol_load_addr = sym_address.GetCallableLoadAddress(&target, true); 562 break; 563 564 case eSymbolTypeReExported: { 565 ConstString reexport_name = sym_ctx.symbol->GetReExportedSymbolName(); 566 if (reexport_name) { 567 ModuleSP reexport_module_sp; 568 ModuleSpec reexport_module_spec; 569 reexport_module_spec.GetPlatformFileSpec() = 570 sym_ctx.symbol->GetReExportedSymbolSharedLibrary(); 571 if (reexport_module_spec.GetPlatformFileSpec()) { 572 reexport_module_sp = 573 target.GetImages().FindFirstModule(reexport_module_spec); 574 if (!reexport_module_sp) { 575 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear(); 576 reexport_module_sp = 577 target.GetImages().FindFirstModule(reexport_module_spec); 578 } 579 } 580 symbol_load_addr = GetSymbolAddress( 581 target, process, sym_ctx.symbol->GetReExportedSymbolName(), 582 symbol_type, reexport_module_sp.get()); 583 } 584 } break; 585 586 case eSymbolTypeData: 587 case eSymbolTypeRuntime: 588 case eSymbolTypeVariable: 589 case eSymbolTypeLocal: 590 case eSymbolTypeParam: 591 case eSymbolTypeInvalid: 592 case eSymbolTypeAbsolute: 593 case eSymbolTypeException: 594 case eSymbolTypeSourceFile: 595 case eSymbolTypeHeaderFile: 596 case eSymbolTypeObjectFile: 597 case eSymbolTypeCommonBlock: 598 case eSymbolTypeBlock: 599 case eSymbolTypeVariableType: 600 case eSymbolTypeLineEntry: 601 case eSymbolTypeLineHeader: 602 case eSymbolTypeScopeBegin: 603 case eSymbolTypeScopeEnd: 604 case eSymbolTypeAdditional: 605 case eSymbolTypeCompiler: 606 case eSymbolTypeInstrumentation: 607 case eSymbolTypeUndefined: 608 case eSymbolTypeObjCClass: 609 case eSymbolTypeObjCMetaClass: 610 case eSymbolTypeObjCIVar: 611 symbol_load_addr = sym_address.GetLoadAddress(&target); 612 break; 613 } 614 } 615 616 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process) { 617 ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process); 618 619 if (runtime) { 620 symbol_load_addr = runtime->LookupRuntimeSymbol(name); 621 } 622 } 623 624 return symbol_load_addr; 625 } 626 627 addr_t ClangExpressionDeclMap::GetSymbolAddress(ConstString name, 628 lldb::SymbolType symbol_type) { 629 assert(m_parser_vars.get()); 630 631 if (!m_parser_vars->m_exe_ctx.GetTargetPtr()) 632 return false; 633 634 return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(), 635 m_parser_vars->m_exe_ctx.GetProcessPtr(), name, 636 symbol_type); 637 } 638 639 lldb::VariableSP ClangExpressionDeclMap::FindGlobalVariable( 640 Target &target, ModuleSP &module, ConstString name, 641 CompilerDeclContext *namespace_decl, TypeFromUser *type) { 642 VariableList vars; 643 644 if (module && namespace_decl) 645 module->FindGlobalVariables(name, namespace_decl, -1, vars); 646 else 647 target.GetImages().FindGlobalVariables(name, -1, vars); 648 649 if (vars.GetSize()) { 650 if (type) { 651 for (VariableSP var_sp : vars) { 652 if (ClangASTContext::AreTypesSame( 653 *type, var_sp->GetType()->GetFullCompilerType())) 654 return var_sp; 655 } 656 } else { 657 return vars.GetVariableAtIndex(0); 658 } 659 } 660 661 return VariableSP(); 662 } 663 664 ClangASTContext *ClangExpressionDeclMap::GetClangASTContext() { 665 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); 666 if (frame == nullptr) 667 return nullptr; 668 669 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | 670 lldb::eSymbolContextBlock); 671 if (sym_ctx.block == nullptr) 672 return nullptr; 673 674 CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext(); 675 if (!frame_decl_context) 676 return nullptr; 677 678 return llvm::dyn_cast_or_null<ClangASTContext>( 679 frame_decl_context.GetTypeSystem()); 680 } 681 682 // Interface for ClangASTSource 683 684 void ClangExpressionDeclMap::FindExternalVisibleDecls( 685 NameSearchContext &context) { 686 assert(m_ast_context); 687 688 ClangASTMetrics::RegisterVisibleQuery(); 689 690 const ConstString name(context.m_decl_name.getAsString().c_str()); 691 692 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 693 694 if (GetImportInProgress()) { 695 if (log && log->GetVerbose()) 696 LLDB_LOGF(log, "Ignoring a query during an import"); 697 return; 698 } 699 700 static unsigned int invocation_id = 0; 701 unsigned int current_id = invocation_id++; 702 703 if (log) { 704 if (!context.m_decl_context) 705 LLDB_LOGF(log, 706 "ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for " 707 "'%s' in a NULL DeclContext", 708 current_id, name.GetCString()); 709 else if (const NamedDecl *context_named_decl = 710 dyn_cast<NamedDecl>(context.m_decl_context)) 711 LLDB_LOGF(log, 712 "ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for " 713 "'%s' in '%s'", 714 current_id, name.GetCString(), 715 context_named_decl->getNameAsString().c_str()); 716 else 717 LLDB_LOGF(log, 718 "ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for " 719 "'%s' in a '%s'", 720 current_id, name.GetCString(), 721 context.m_decl_context->getDeclKindName()); 722 } 723 724 if (const NamespaceDecl *namespace_context = 725 dyn_cast<NamespaceDecl>(context.m_decl_context)) { 726 if (namespace_context->getName().str() == 727 std::string(g_lldb_local_vars_namespace_cstr)) { 728 CompilerDeclContext compiler_decl_ctx( 729 GetClangASTContext(), const_cast<void *>(static_cast<const void *>( 730 context.m_decl_context))); 731 FindExternalVisibleDecls(context, lldb::ModuleSP(), compiler_decl_ctx, 732 current_id); 733 return; 734 } 735 736 ClangASTImporter::NamespaceMapSP namespace_map = 737 m_ast_importer_sp 738 ? m_ast_importer_sp->GetNamespaceMap(namespace_context) 739 : ClangASTImporter::NamespaceMapSP(); 740 741 if (!namespace_map) 742 return; 743 744 if (log && log->GetVerbose()) 745 log->Printf(" CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)", 746 current_id, static_cast<void *>(namespace_map.get()), 747 (int)namespace_map->size()); 748 749 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), 750 e = namespace_map->end(); 751 i != e; ++i) { 752 if (log) 753 log->Printf(" CEDM::FEVD[%u] Searching namespace %s in module %s", 754 current_id, i->second.GetName().AsCString(), 755 i->first->GetFileSpec().GetFilename().GetCString()); 756 757 FindExternalVisibleDecls(context, i->first, i->second, current_id); 758 } 759 } else if (isa<TranslationUnitDecl>(context.m_decl_context)) { 760 CompilerDeclContext namespace_decl; 761 762 if (log) 763 log->Printf(" CEDM::FEVD[%u] Searching the root namespace", current_id); 764 765 FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl, 766 current_id); 767 } 768 769 ClangASTSource::FindExternalVisibleDecls(context); 770 } 771 772 void ClangExpressionDeclMap::MaybeRegisterFunctionBody( 773 FunctionDecl *copied_function_decl) { 774 if (copied_function_decl->getBody() && m_parser_vars->m_code_gen) { 775 clang::DeclGroupRef decl_group_ref(copied_function_decl); 776 m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref); 777 } 778 } 779 780 void ClangExpressionDeclMap::SearchPersistenDecls(NameSearchContext &context, 781 const ConstString name, 782 unsigned int current_id) { 783 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 784 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 785 if (!target) 786 return; 787 788 ClangASTContext *scratch_clang_ast_context = 789 target->GetScratchClangASTContext(); 790 791 if (!scratch_clang_ast_context) 792 return; 793 794 ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext(); 795 796 if (!scratch_ast_context) 797 return; 798 799 NamedDecl *persistent_decl = 800 m_parser_vars->m_persistent_vars->GetPersistentDecl(name); 801 802 if (!persistent_decl) 803 return; 804 805 Decl *parser_persistent_decl = CopyDecl(persistent_decl); 806 807 if (!parser_persistent_decl) 808 return; 809 810 NamedDecl *parser_named_decl = dyn_cast<NamedDecl>(parser_persistent_decl); 811 812 if (!parser_named_decl) 813 return; 814 815 if (clang::FunctionDecl *parser_function_decl = 816 llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl)) { 817 MaybeRegisterFunctionBody(parser_function_decl); 818 } 819 820 LLDB_LOGF(log, " CEDM::FEVD[%u] Found persistent decl %s", current_id, 821 name.GetCString()); 822 823 context.AddNamedDecl(parser_named_decl); 824 } 825 826 void ClangExpressionDeclMap::LookUpLldbClass(NameSearchContext &context, 827 unsigned int current_id) { 828 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 829 830 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); 831 SymbolContext sym_ctx; 832 if (frame != nullptr) 833 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | 834 lldb::eSymbolContextBlock); 835 836 if (m_ctx_obj) { 837 Status status; 838 lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status); 839 if (!ctx_obj_ptr || status.Fail()) 840 return; 841 842 AddThisType(context, TypeFromUser(m_ctx_obj->GetCompilerType()), 843 current_id); 844 845 m_struct_vars->m_object_pointer_type = 846 TypeFromUser(ctx_obj_ptr->GetCompilerType()); 847 848 return; 849 } 850 851 // Clang is looking for the type of "this" 852 853 if (frame == nullptr) 854 return; 855 856 // Find the block that defines the function represented by "sym_ctx" 857 Block *function_block = sym_ctx.GetFunctionBlock(); 858 859 if (!function_block) 860 return; 861 862 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext(); 863 864 if (!function_decl_ctx) 865 return; 866 867 clang::CXXMethodDecl *method_decl = 868 ClangASTContext::DeclContextGetAsCXXMethodDecl(function_decl_ctx); 869 870 if (method_decl) { 871 clang::CXXRecordDecl *class_decl = method_decl->getParent(); 872 873 QualType class_qual_type(class_decl->getTypeForDecl(), 0); 874 875 TypeFromUser class_user_type( 876 class_qual_type.getAsOpaquePtr(), 877 ClangASTContext::GetASTContext(&class_decl->getASTContext())); 878 879 LLDB_LOG(log, " CEDM::FEVD[{0}] Adding type for $__lldb_class: {1}", 880 current_id, class_qual_type.getAsString()); 881 882 AddThisType(context, class_user_type, current_id); 883 884 if (method_decl->isInstance()) { 885 // self is a pointer to the object 886 887 QualType class_pointer_type = 888 method_decl->getASTContext().getPointerType(class_qual_type); 889 890 TypeFromUser self_user_type( 891 class_pointer_type.getAsOpaquePtr(), 892 ClangASTContext::GetASTContext(&method_decl->getASTContext())); 893 894 m_struct_vars->m_object_pointer_type = self_user_type; 895 } 896 return; 897 } 898 899 // This branch will get hit if we are executing code in the context of 900 // a function that claims to have an object pointer (through 901 // DW_AT_object_pointer?) but is not formally a method of the class. 902 // In that case, just look up the "this" variable in the current scope 903 // and use its type. 904 // FIXME: This code is formally correct, but clang doesn't currently 905 // emit DW_AT_object_pointer 906 // for C++ so it hasn't actually been tested. 907 908 VariableList *vars = frame->GetVariableList(false); 909 910 lldb::VariableSP this_var = vars->FindVariable(ConstString("this")); 911 912 if (this_var && this_var->IsInScope(frame) && 913 this_var->LocationIsValidForFrame(frame)) { 914 Type *this_type = this_var->GetType(); 915 916 if (!this_type) 917 return; 918 919 TypeFromUser pointee_type = 920 this_type->GetForwardCompilerType().GetPointeeType(); 921 922 LLDB_LOG(log, " FEVD[{0}] Adding type for $__lldb_class: {1}", current_id, 923 ClangUtil::GetQualType(pointee_type).getAsString()); 924 925 AddThisType(context, pointee_type, current_id); 926 TypeFromUser this_user_type(this_type->GetFullCompilerType()); 927 m_struct_vars->m_object_pointer_type = this_user_type; 928 } 929 } 930 931 void ClangExpressionDeclMap::LookUpLldbObjCClass(NameSearchContext &context, 932 unsigned int current_id) { 933 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 934 935 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); 936 937 if (m_ctx_obj) { 938 Status status; 939 lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status); 940 if (!ctx_obj_ptr || status.Fail()) 941 return; 942 943 AddOneType(context, TypeFromUser(m_ctx_obj->GetCompilerType()), current_id); 944 945 m_struct_vars->m_object_pointer_type = 946 TypeFromUser(ctx_obj_ptr->GetCompilerType()); 947 948 return; 949 } 950 951 // Clang is looking for the type of "*self" 952 953 if (!frame) 954 return; 955 956 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | 957 lldb::eSymbolContextBlock); 958 959 // Find the block that defines the function represented by "sym_ctx" 960 Block *function_block = sym_ctx.GetFunctionBlock(); 961 962 if (!function_block) 963 return; 964 965 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext(); 966 967 if (!function_decl_ctx) 968 return; 969 970 clang::ObjCMethodDecl *method_decl = 971 ClangASTContext::DeclContextGetAsObjCMethodDecl(function_decl_ctx); 972 973 if (method_decl) { 974 ObjCInterfaceDecl *self_interface = method_decl->getClassInterface(); 975 976 if (!self_interface) 977 return; 978 979 const clang::Type *interface_type = self_interface->getTypeForDecl(); 980 981 if (!interface_type) 982 return; // This is unlikely, but we have seen crashes where this 983 // occurred 984 985 TypeFromUser class_user_type( 986 QualType(interface_type, 0).getAsOpaquePtr(), 987 ClangASTContext::GetASTContext(&method_decl->getASTContext())); 988 989 LLDB_LOG(log, " FEVD[{0}] Adding type for $__lldb_objc_class: {1}", 990 current_id, ClangUtil::ToString(interface_type)); 991 992 AddOneType(context, class_user_type, current_id); 993 994 if (method_decl->isInstanceMethod()) { 995 // self is a pointer to the object 996 997 QualType class_pointer_type = 998 method_decl->getASTContext().getObjCObjectPointerType( 999 QualType(interface_type, 0)); 1000 1001 TypeFromUser self_user_type( 1002 class_pointer_type.getAsOpaquePtr(), 1003 ClangASTContext::GetASTContext(&method_decl->getASTContext())); 1004 1005 m_struct_vars->m_object_pointer_type = self_user_type; 1006 } else { 1007 // self is a Class pointer 1008 QualType class_type = method_decl->getASTContext().getObjCClassType(); 1009 1010 TypeFromUser self_user_type( 1011 class_type.getAsOpaquePtr(), 1012 ClangASTContext::GetASTContext(&method_decl->getASTContext())); 1013 1014 m_struct_vars->m_object_pointer_type = self_user_type; 1015 } 1016 1017 return; 1018 } 1019 // This branch will get hit if we are executing code in the context of 1020 // a function that claims to have an object pointer (through 1021 // DW_AT_object_pointer?) but is not formally a method of the class. 1022 // In that case, just look up the "self" variable in the current scope 1023 // and use its type. 1024 1025 VariableList *vars = frame->GetVariableList(false); 1026 1027 lldb::VariableSP self_var = vars->FindVariable(ConstString("self")); 1028 1029 if (!self_var) 1030 return; 1031 if (!self_var->IsInScope(frame)) 1032 return; 1033 if (!self_var->LocationIsValidForFrame(frame)) 1034 return; 1035 1036 Type *self_type = self_var->GetType(); 1037 1038 if (!self_type) 1039 return; 1040 1041 CompilerType self_clang_type = self_type->GetFullCompilerType(); 1042 1043 if (ClangASTContext::IsObjCClassType(self_clang_type)) { 1044 return; 1045 } 1046 if (!ClangASTContext::IsObjCObjectPointerType(self_clang_type)) 1047 return; 1048 self_clang_type = self_clang_type.GetPointeeType(); 1049 1050 if (!self_clang_type) 1051 return; 1052 1053 LLDB_LOG(log, " FEVD[{0}] Adding type for $__lldb_objc_class: {1}", 1054 current_id, ClangUtil::ToString(self_type->GetFullCompilerType())); 1055 1056 TypeFromUser class_user_type(self_clang_type); 1057 1058 AddOneType(context, class_user_type, current_id); 1059 1060 TypeFromUser self_user_type(self_type->GetFullCompilerType()); 1061 1062 m_struct_vars->m_object_pointer_type = self_user_type; 1063 } 1064 1065 void ClangExpressionDeclMap::LookupLocalVarNamespace( 1066 SymbolContext &sym_ctx, NameSearchContext &name_context) { 1067 if (sym_ctx.block == nullptr) 1068 return; 1069 1070 CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext(); 1071 if (!frame_decl_context) 1072 return; 1073 1074 ClangASTContext *frame_ast = llvm::dyn_cast_or_null<ClangASTContext>( 1075 frame_decl_context.GetTypeSystem()); 1076 if (!frame_ast) 1077 return; 1078 1079 clang::NamespaceDecl *namespace_decl = 1080 m_clang_ast_context->GetUniqueNamespaceDeclaration( 1081 g_lldb_local_vars_namespace_cstr, nullptr); 1082 if (!namespace_decl) 1083 return; 1084 1085 name_context.AddNamedDecl(namespace_decl); 1086 clang::DeclContext *ctxt = clang::Decl::castToDeclContext(namespace_decl); 1087 ctxt->setHasExternalVisibleStorage(true); 1088 name_context.m_found.local_vars_nsp = true; 1089 } 1090 1091 void ClangExpressionDeclMap::LookupInModulesDeclVendor( 1092 NameSearchContext &context, ConstString name, unsigned current_id) { 1093 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1094 1095 auto *modules_decl_vendor = m_target->GetClangModulesDeclVendor(); 1096 if (!modules_decl_vendor) 1097 return; 1098 1099 bool append = false; 1100 uint32_t max_matches = 1; 1101 std::vector<clang::NamedDecl *> decls; 1102 1103 if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls)) 1104 return; 1105 1106 assert(!decls.empty() && "FindDecls returned true but no decls?"); 1107 clang::NamedDecl *const decl_from_modules = decls[0]; 1108 1109 LLDB_LOG(log, 1110 " CAS::FEVD[{0}] Matching decl found for " 1111 "\"{1}\" in the modules", 1112 current_id, name); 1113 1114 clang::Decl *copied_decl = CopyDecl(decl_from_modules); 1115 if (!copied_decl) { 1116 LLDB_LOG(log, 1117 " CAS::FEVD[{0}] - Couldn't export a " 1118 "declaration from the modules", 1119 current_id); 1120 return; 1121 } 1122 1123 if (auto copied_function = dyn_cast<clang::FunctionDecl>(copied_decl)) { 1124 MaybeRegisterFunctionBody(copied_function); 1125 1126 context.AddNamedDecl(copied_function); 1127 1128 context.m_found.function_with_type_info = true; 1129 context.m_found.function = true; 1130 } else if (auto copied_var = dyn_cast<clang::VarDecl>(copied_decl)) { 1131 context.AddNamedDecl(copied_var); 1132 context.m_found.variable = true; 1133 } 1134 } 1135 1136 bool ClangExpressionDeclMap::LookupLocalVariable( 1137 NameSearchContext &context, ConstString name, unsigned current_id, 1138 SymbolContext &sym_ctx, CompilerDeclContext &namespace_decl) { 1139 if (sym_ctx.block == nullptr) 1140 return false; 1141 1142 CompilerDeclContext decl_context = sym_ctx.block->GetDeclContext(); 1143 if (!decl_context) 1144 return false; 1145 1146 // Make sure that the variables are parsed so that we have the 1147 // declarations. 1148 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); 1149 VariableListSP vars = frame->GetInScopeVariableList(true); 1150 for (size_t i = 0; i < vars->GetSize(); i++) 1151 vars->GetVariableAtIndex(i)->GetDecl(); 1152 1153 // Search for declarations matching the name. Do not include imported 1154 // decls in the search if we are looking for decls in the artificial 1155 // namespace $__lldb_local_vars. 1156 std::vector<CompilerDecl> found_decls = 1157 decl_context.FindDeclByName(name, namespace_decl.IsValid()); 1158 1159 VariableSP var; 1160 bool variable_found = false; 1161 for (CompilerDecl decl : found_decls) { 1162 for (size_t vi = 0, ve = vars->GetSize(); vi != ve; ++vi) { 1163 VariableSP candidate_var = vars->GetVariableAtIndex(vi); 1164 if (candidate_var->GetDecl() == decl) { 1165 var = candidate_var; 1166 break; 1167 } 1168 } 1169 1170 if (var && !variable_found) { 1171 variable_found = true; 1172 ValueObjectSP valobj = ValueObjectVariable::Create(frame, var); 1173 AddOneVariable(context, var, valobj, current_id); 1174 context.m_found.variable = true; 1175 } 1176 } 1177 return variable_found; 1178 } 1179 1180 /// Structure to hold the info needed when comparing function 1181 /// declarations. 1182 namespace { 1183 struct FuncDeclInfo { 1184 ConstString m_name; 1185 CompilerType m_copied_type; 1186 uint32_t m_decl_lvl; 1187 SymbolContext m_sym_ctx; 1188 }; 1189 } // namespace 1190 1191 SymbolContextList ClangExpressionDeclMap::SearchFunctionsInSymbolContexts( 1192 const SymbolContextList &sc_list, 1193 const CompilerDeclContext &frame_decl_context) { 1194 // First, symplify things by looping through the symbol contexts to 1195 // remove unwanted functions and separate out the functions we want to 1196 // compare and prune into a separate list. Cache the info needed about 1197 // the function declarations in a vector for efficiency. 1198 uint32_t num_indices = sc_list.GetSize(); 1199 SymbolContextList sc_sym_list; 1200 std::vector<FuncDeclInfo> decl_infos; 1201 decl_infos.reserve(num_indices); 1202 clang::DeclContext *frame_decl_ctx = 1203 (clang::DeclContext *)frame_decl_context.GetOpaqueDeclContext(); 1204 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>( 1205 frame_decl_context.GetTypeSystem()); 1206 1207 for (uint32_t index = 0; index < num_indices; ++index) { 1208 FuncDeclInfo fdi; 1209 SymbolContext sym_ctx; 1210 sc_list.GetContextAtIndex(index, sym_ctx); 1211 1212 // We don't know enough about symbols to compare them, but we should 1213 // keep them in the list. 1214 Function *function = sym_ctx.function; 1215 if (!function) { 1216 sc_sym_list.Append(sym_ctx); 1217 continue; 1218 } 1219 // Filter out functions without declaration contexts, as well as 1220 // class/instance methods, since they'll be skipped in the code that 1221 // follows anyway. 1222 CompilerDeclContext func_decl_context = function->GetDeclContext(); 1223 if (!func_decl_context || 1224 func_decl_context.IsClassMethod(nullptr, nullptr, nullptr)) 1225 continue; 1226 // We can only prune functions for which we can copy the type. 1227 CompilerType func_clang_type = function->GetType()->GetFullCompilerType(); 1228 CompilerType copied_func_type = GuardedCopyType(func_clang_type); 1229 if (!copied_func_type) { 1230 sc_sym_list.Append(sym_ctx); 1231 continue; 1232 } 1233 1234 fdi.m_sym_ctx = sym_ctx; 1235 fdi.m_name = function->GetName(); 1236 fdi.m_copied_type = copied_func_type; 1237 fdi.m_decl_lvl = LLDB_INVALID_DECL_LEVEL; 1238 if (fdi.m_copied_type && func_decl_context) { 1239 // Call CountDeclLevels to get the number of parent scopes we have 1240 // to look through before we find the function declaration. When 1241 // comparing functions of the same type, the one with a lower count 1242 // will be closer to us in the lookup scope and shadows the other. 1243 clang::DeclContext *func_decl_ctx = 1244 (clang::DeclContext *)func_decl_context.GetOpaqueDeclContext(); 1245 fdi.m_decl_lvl = ast->CountDeclLevels(frame_decl_ctx, func_decl_ctx, 1246 &fdi.m_name, &fdi.m_copied_type); 1247 } 1248 decl_infos.emplace_back(fdi); 1249 } 1250 1251 // Loop through the functions in our cache looking for matching types, 1252 // then compare their scope levels to see which is closer. 1253 std::multimap<CompilerType, const FuncDeclInfo *> matches; 1254 for (const FuncDeclInfo &fdi : decl_infos) { 1255 const CompilerType t = fdi.m_copied_type; 1256 auto q = matches.find(t); 1257 if (q != matches.end()) { 1258 if (q->second->m_decl_lvl > fdi.m_decl_lvl) 1259 // This function is closer; remove the old set. 1260 matches.erase(t); 1261 else if (q->second->m_decl_lvl < fdi.m_decl_lvl) 1262 // The functions in our set are closer - skip this one. 1263 continue; 1264 } 1265 matches.insert(std::make_pair(t, &fdi)); 1266 } 1267 1268 // Loop through our matches and add their symbol contexts to our list. 1269 SymbolContextList sc_func_list; 1270 for (const auto &q : matches) 1271 sc_func_list.Append(q.second->m_sym_ctx); 1272 1273 // Rejoin the lists with the functions in front. 1274 sc_func_list.Append(sc_sym_list); 1275 return sc_func_list; 1276 } 1277 1278 void ClangExpressionDeclMap::LookupFunction(NameSearchContext &context, 1279 lldb::ModuleSP module_sp, 1280 ConstString name, 1281 CompilerDeclContext &namespace_decl, 1282 unsigned current_id) { 1283 1284 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 1285 1286 std::vector<clang::NamedDecl *> decls_from_modules; 1287 1288 if (target) { 1289 if (ClangModulesDeclVendor *decl_vendor = 1290 target->GetClangModulesDeclVendor()) { 1291 decl_vendor->FindDecls(name, false, UINT32_MAX, decls_from_modules); 1292 } 1293 } 1294 1295 const bool include_inlines = false; 1296 SymbolContextList sc_list; 1297 if (namespace_decl && module_sp) { 1298 const bool include_symbols = false; 1299 1300 module_sp->FindFunctions(name, &namespace_decl, eFunctionNameTypeBase, 1301 include_symbols, include_inlines, sc_list); 1302 } else if (target && !namespace_decl) { 1303 const bool include_symbols = true; 1304 1305 // TODO Fix FindFunctions so that it doesn't return 1306 // instance methods for eFunctionNameTypeBase. 1307 1308 target->GetImages().FindFunctions(name, eFunctionNameTypeFull, 1309 include_symbols, include_inlines, 1310 sc_list); 1311 } 1312 1313 // If we found more than one function, see if we can use the frame's decl 1314 // context to remove functions that are shadowed by other functions which 1315 // match in type but are nearer in scope. 1316 // 1317 // AddOneFunction will not add a function whose type has already been 1318 // added, so if there's another function in the list with a matching type, 1319 // check to see if their decl context is a parent of the current frame's or 1320 // was imported via a and using statement, and pick the best match 1321 // according to lookup rules. 1322 if (sc_list.GetSize() > 1) { 1323 // Collect some info about our frame's context. 1324 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); 1325 SymbolContext frame_sym_ctx; 1326 if (frame != nullptr) 1327 frame_sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | 1328 lldb::eSymbolContextBlock); 1329 CompilerDeclContext frame_decl_context = 1330 frame_sym_ctx.block != nullptr ? frame_sym_ctx.block->GetDeclContext() 1331 : CompilerDeclContext(); 1332 1333 // We can't do this without a compiler decl context for our frame. 1334 if (frame_decl_context) { 1335 sc_list = SearchFunctionsInSymbolContexts(sc_list, frame_decl_context); 1336 } 1337 } 1338 1339 if (sc_list.GetSize()) { 1340 Symbol *extern_symbol = nullptr; 1341 Symbol *non_extern_symbol = nullptr; 1342 1343 for (uint32_t index = 0, num_indices = sc_list.GetSize(); 1344 index < num_indices; ++index) { 1345 SymbolContext sym_ctx; 1346 sc_list.GetContextAtIndex(index, sym_ctx); 1347 1348 if (sym_ctx.function) { 1349 CompilerDeclContext decl_ctx = sym_ctx.function->GetDeclContext(); 1350 1351 if (!decl_ctx) 1352 continue; 1353 1354 // Filter out class/instance methods. 1355 if (decl_ctx.IsClassMethod(nullptr, nullptr, nullptr)) 1356 continue; 1357 1358 AddOneFunction(context, sym_ctx.function, nullptr, current_id); 1359 context.m_found.function_with_type_info = true; 1360 context.m_found.function = true; 1361 } else if (sym_ctx.symbol) { 1362 if (sym_ctx.symbol->GetType() == eSymbolTypeReExported && target) { 1363 sym_ctx.symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target); 1364 if (sym_ctx.symbol == nullptr) 1365 continue; 1366 } 1367 1368 if (sym_ctx.symbol->IsExternal()) 1369 extern_symbol = sym_ctx.symbol; 1370 else 1371 non_extern_symbol = sym_ctx.symbol; 1372 } 1373 } 1374 1375 if (!context.m_found.function_with_type_info) { 1376 for (clang::NamedDecl *decl : decls_from_modules) { 1377 if (llvm::isa<clang::FunctionDecl>(decl)) { 1378 clang::NamedDecl *copied_decl = 1379 llvm::cast_or_null<FunctionDecl>(CopyDecl(decl)); 1380 if (copied_decl) { 1381 context.AddNamedDecl(copied_decl); 1382 context.m_found.function_with_type_info = true; 1383 } 1384 } 1385 } 1386 } 1387 1388 if (!context.m_found.function_with_type_info) { 1389 if (extern_symbol) { 1390 AddOneFunction(context, nullptr, extern_symbol, current_id); 1391 context.m_found.function = true; 1392 } else if (non_extern_symbol) { 1393 AddOneFunction(context, nullptr, non_extern_symbol, current_id); 1394 context.m_found.function = true; 1395 } 1396 } 1397 } 1398 } 1399 1400 void ClangExpressionDeclMap::FindExternalVisibleDecls( 1401 NameSearchContext &context, lldb::ModuleSP module_sp, 1402 CompilerDeclContext &namespace_decl, unsigned int current_id) { 1403 assert(m_ast_context); 1404 1405 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1406 1407 const ConstString name(context.m_decl_name.getAsString().c_str()); 1408 if (IgnoreName(name, false)) 1409 return; 1410 1411 // Only look for functions by name out in our symbols if the function doesn't 1412 // start with our phony prefix of '$' 1413 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 1414 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); 1415 SymbolContext sym_ctx; 1416 if (frame != nullptr) 1417 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | 1418 lldb::eSymbolContextBlock); 1419 1420 // Try the persistent decls, which take precedence over all else. 1421 if (!namespace_decl) 1422 SearchPersistenDecls(context, name, current_id); 1423 1424 if (name.GetStringRef().startswith("$") && !namespace_decl) { 1425 if (name == "$__lldb_class") { 1426 LookUpLldbClass(context, current_id); 1427 return; 1428 } 1429 1430 if (name == "$__lldb_objc_class") { 1431 LookUpLldbObjCClass(context, current_id); 1432 return; 1433 } 1434 if (name == g_lldb_local_vars_namespace_cstr) { 1435 LookupLocalVarNamespace(sym_ctx, context); 1436 return; 1437 } 1438 1439 // any other $__lldb names should be weeded out now 1440 if (name.GetStringRef().startswith("$__lldb")) 1441 return; 1442 1443 ExpressionVariableSP pvar_sp( 1444 m_parser_vars->m_persistent_vars->GetVariable(name)); 1445 1446 if (pvar_sp) { 1447 AddOneVariable(context, pvar_sp, current_id); 1448 return; 1449 } 1450 1451 assert(name.GetStringRef().startswith("$")); 1452 llvm::StringRef reg_name = name.GetStringRef().substr(1); 1453 1454 if (m_parser_vars->m_exe_ctx.GetRegisterContext()) { 1455 const RegisterInfo *reg_info( 1456 m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName( 1457 reg_name)); 1458 1459 if (reg_info) { 1460 LLDB_LOGF(log, " CEDM::FEVD[%u] Found register %s", current_id, 1461 reg_info->name); 1462 1463 AddOneRegister(context, reg_info, current_id); 1464 } 1465 } 1466 return; 1467 } 1468 1469 bool local_var_lookup = !namespace_decl || (namespace_decl.GetName() == 1470 g_lldb_local_vars_namespace_cstr); 1471 if (frame && local_var_lookup) 1472 if (LookupLocalVariable(context, name, current_id, sym_ctx, namespace_decl)) 1473 return; 1474 1475 if (target) { 1476 ValueObjectSP valobj; 1477 VariableSP var; 1478 var = 1479 FindGlobalVariable(*target, module_sp, name, &namespace_decl, nullptr); 1480 1481 if (var) { 1482 valobj = ValueObjectVariable::Create(target, var); 1483 AddOneVariable(context, var, valobj, current_id); 1484 context.m_found.variable = true; 1485 return; 1486 } 1487 } 1488 1489 LookupFunction(context, module_sp, name, namespace_decl, current_id); 1490 1491 // Try the modules next. 1492 if (!context.m_found.function_with_type_info) 1493 LookupInModulesDeclVendor(context, name, current_id); 1494 1495 if (target && !context.m_found.variable && !namespace_decl) { 1496 // We couldn't find a non-symbol variable for this. Now we'll hunt for a 1497 // generic data symbol, and -- if it is found -- treat it as a variable. 1498 Status error; 1499 1500 const Symbol *data_symbol = 1501 m_parser_vars->m_sym_ctx.FindBestGlobalDataSymbol(name, error); 1502 1503 if (!error.Success()) { 1504 const unsigned diag_id = 1505 m_ast_context->getDiagnostics().getCustomDiagID( 1506 clang::DiagnosticsEngine::Level::Error, "%0"); 1507 m_ast_context->getDiagnostics().Report(diag_id) << error.AsCString(); 1508 } 1509 1510 if (data_symbol) { 1511 std::string warning("got name from symbols: "); 1512 warning.append(name.AsCString()); 1513 const unsigned diag_id = 1514 m_ast_context->getDiagnostics().getCustomDiagID( 1515 clang::DiagnosticsEngine::Level::Warning, "%0"); 1516 m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str(); 1517 AddOneGenericVariable(context, *data_symbol, current_id); 1518 context.m_found.variable = true; 1519 } 1520 } 1521 } 1522 1523 bool ClangExpressionDeclMap::GetVariableValue(VariableSP &var, 1524 lldb_private::Value &var_location, 1525 TypeFromUser *user_type, 1526 TypeFromParser *parser_type) { 1527 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1528 1529 Type *var_type = var->GetType(); 1530 1531 if (!var_type) { 1532 if (log) 1533 log->PutCString("Skipped a definition because it has no type"); 1534 return false; 1535 } 1536 1537 CompilerType var_clang_type = var_type->GetFullCompilerType(); 1538 1539 if (!var_clang_type) { 1540 if (log) 1541 log->PutCString("Skipped a definition because it has no Clang type"); 1542 return false; 1543 } 1544 1545 ClangASTContext *clang_ast = llvm::dyn_cast_or_null<ClangASTContext>( 1546 var_type->GetForwardCompilerType().GetTypeSystem()); 1547 1548 if (!clang_ast) { 1549 if (log) 1550 log->PutCString("Skipped a definition because it has no Clang AST"); 1551 return false; 1552 } 1553 1554 ASTContext *ast = clang_ast->getASTContext(); 1555 1556 if (!ast) { 1557 if (log) 1558 log->PutCString( 1559 "There is no AST context for the current execution context"); 1560 return false; 1561 } 1562 1563 DWARFExpression &var_location_expr = var->LocationExpression(); 1564 1565 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 1566 Status err; 1567 1568 if (var->GetLocationIsConstantValueData()) { 1569 DataExtractor const_value_extractor; 1570 1571 if (var_location_expr.GetExpressionData(const_value_extractor)) { 1572 var_location = Value(const_value_extractor.GetDataStart(), 1573 const_value_extractor.GetByteSize()); 1574 var_location.SetValueType(Value::eValueTypeHostAddress); 1575 } else { 1576 LLDB_LOGF(log, "Error evaluating constant variable: %s", err.AsCString()); 1577 return false; 1578 } 1579 } 1580 1581 CompilerType type_to_use = GuardedCopyType(var_clang_type); 1582 1583 if (!type_to_use) { 1584 LLDB_LOGF(log, 1585 "Couldn't copy a variable's type into the parser's AST context"); 1586 1587 return false; 1588 } 1589 1590 if (parser_type) 1591 *parser_type = TypeFromParser(type_to_use); 1592 1593 if (var_location.GetContextType() == Value::eContextTypeInvalid) 1594 var_location.SetCompilerType(type_to_use); 1595 1596 if (var_location.GetValueType() == Value::eValueTypeFileAddress) { 1597 SymbolContext var_sc; 1598 var->CalculateSymbolContext(&var_sc); 1599 1600 if (!var_sc.module_sp) 1601 return false; 1602 1603 Address so_addr(var_location.GetScalar().ULongLong(), 1604 var_sc.module_sp->GetSectionList()); 1605 1606 lldb::addr_t load_addr = so_addr.GetLoadAddress(target); 1607 1608 if (load_addr != LLDB_INVALID_ADDRESS) { 1609 var_location.GetScalar() = load_addr; 1610 var_location.SetValueType(Value::eValueTypeLoadAddress); 1611 } 1612 } 1613 1614 if (user_type) 1615 *user_type = TypeFromUser(var_clang_type); 1616 1617 return true; 1618 } 1619 1620 void ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context, 1621 VariableSP var, 1622 ValueObjectSP valobj, 1623 unsigned int current_id) { 1624 assert(m_parser_vars.get()); 1625 1626 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1627 1628 TypeFromUser ut; 1629 TypeFromParser pt; 1630 Value var_location; 1631 1632 if (!GetVariableValue(var, var_location, &ut, &pt)) 1633 return; 1634 1635 clang::QualType parser_opaque_type = 1636 QualType::getFromOpaquePtr(pt.GetOpaqueQualType()); 1637 1638 if (parser_opaque_type.isNull()) 1639 return; 1640 1641 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr()) { 1642 if (const TagType *tag_type = dyn_cast<TagType>(parser_type)) 1643 CompleteType(tag_type->getDecl()); 1644 if (const ObjCObjectPointerType *objc_object_ptr_type = 1645 dyn_cast<ObjCObjectPointerType>(parser_type)) 1646 CompleteType(objc_object_ptr_type->getInterfaceDecl()); 1647 } 1648 1649 bool is_reference = pt.IsReferenceType(); 1650 1651 NamedDecl *var_decl = nullptr; 1652 if (is_reference) 1653 var_decl = context.AddVarDecl(pt); 1654 else 1655 var_decl = context.AddVarDecl(pt.GetLValueReferenceType()); 1656 1657 std::string decl_name(context.m_decl_name.getAsString()); 1658 ConstString entity_name(decl_name.c_str()); 1659 ClangExpressionVariable *entity(new ClangExpressionVariable(valobj)); 1660 m_found_entities.AddNewlyConstructedVariable(entity); 1661 1662 assert(entity); 1663 entity->EnableParserVars(GetParserID()); 1664 ClangExpressionVariable::ParserVars *parser_vars = 1665 entity->GetParserVars(GetParserID()); 1666 parser_vars->m_parser_type = pt; 1667 parser_vars->m_named_decl = var_decl; 1668 parser_vars->m_llvm_value = nullptr; 1669 parser_vars->m_lldb_value = var_location; 1670 parser_vars->m_lldb_var = var; 1671 1672 if (is_reference) 1673 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference; 1674 1675 LLDB_LOG(log, 1676 " CEDM::FEVD[{0}] Found variable {1}, returned\n{2} (original {3})", 1677 current_id, decl_name, ClangUtil::DumpDecl(var_decl), 1678 ClangUtil::ToString(ut)); 1679 } 1680 1681 void ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context, 1682 ExpressionVariableSP &pvar_sp, 1683 unsigned int current_id) { 1684 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1685 1686 TypeFromUser user_type( 1687 llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetTypeFromUser()); 1688 1689 TypeFromParser parser_type(GuardedCopyType(user_type)); 1690 1691 if (!parser_type.GetOpaqueQualType()) { 1692 LLDB_LOGF(log, " CEDM::FEVD[%u] Couldn't import type for pvar %s", 1693 current_id, pvar_sp->GetName().GetCString()); 1694 return; 1695 } 1696 1697 NamedDecl *var_decl = 1698 context.AddVarDecl(parser_type.GetLValueReferenceType()); 1699 1700 llvm::cast<ClangExpressionVariable>(pvar_sp.get()) 1701 ->EnableParserVars(GetParserID()); 1702 ClangExpressionVariable::ParserVars *parser_vars = 1703 llvm::cast<ClangExpressionVariable>(pvar_sp.get()) 1704 ->GetParserVars(GetParserID()); 1705 parser_vars->m_parser_type = parser_type; 1706 parser_vars->m_named_decl = var_decl; 1707 parser_vars->m_llvm_value = nullptr; 1708 parser_vars->m_lldb_value.Clear(); 1709 1710 LLDB_LOG(log, " CEDM::FEVD[{0}] Added pvar {1}, returned\n{2}", current_id, 1711 pvar_sp->GetName(), ClangUtil::DumpDecl(var_decl)); 1712 } 1713 1714 void ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context, 1715 const Symbol &symbol, 1716 unsigned int current_id) { 1717 assert(m_parser_vars.get()); 1718 1719 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1720 1721 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 1722 1723 if (target == nullptr) 1724 return; 1725 1726 ClangASTContext *scratch_ast_context = target->GetScratchClangASTContext(); 1727 1728 TypeFromUser user_type(scratch_ast_context->GetBasicType(eBasicTypeVoid) 1729 .GetPointerType() 1730 .GetLValueReferenceType()); 1731 TypeFromParser parser_type(m_clang_ast_context->GetBasicType(eBasicTypeVoid) 1732 .GetPointerType() 1733 .GetLValueReferenceType()); 1734 NamedDecl *var_decl = context.AddVarDecl(parser_type); 1735 1736 std::string decl_name(context.m_decl_name.getAsString()); 1737 ConstString entity_name(decl_name.c_str()); 1738 ClangExpressionVariable *entity(new ClangExpressionVariable( 1739 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), entity_name, 1740 user_type, m_parser_vars->m_target_info.byte_order, 1741 m_parser_vars->m_target_info.address_byte_size)); 1742 m_found_entities.AddNewlyConstructedVariable(entity); 1743 1744 entity->EnableParserVars(GetParserID()); 1745 ClangExpressionVariable::ParserVars *parser_vars = 1746 entity->GetParserVars(GetParserID()); 1747 1748 const Address symbol_address = symbol.GetAddress(); 1749 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target); 1750 1751 // parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, 1752 // user_type.GetOpaqueQualType()); 1753 parser_vars->m_lldb_value.SetCompilerType(user_type); 1754 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr; 1755 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress); 1756 1757 parser_vars->m_parser_type = parser_type; 1758 parser_vars->m_named_decl = var_decl; 1759 parser_vars->m_llvm_value = nullptr; 1760 parser_vars->m_lldb_sym = &symbol; 1761 1762 LLDB_LOG(log, " CEDM::FEVD[{0}] Found variable {1}, returned\n{2}", 1763 current_id, decl_name, ClangUtil::DumpDecl(var_decl)); 1764 } 1765 1766 void ClangExpressionDeclMap::AddOneRegister(NameSearchContext &context, 1767 const RegisterInfo *reg_info, 1768 unsigned int current_id) { 1769 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1770 1771 CompilerType clang_type = 1772 m_clang_ast_context->GetBuiltinTypeForEncodingAndBitSize( 1773 reg_info->encoding, reg_info->byte_size * 8); 1774 1775 if (!clang_type) { 1776 LLDB_LOGF(log, " Tried to add a type for %s, but couldn't get one", 1777 context.m_decl_name.getAsString().c_str()); 1778 return; 1779 } 1780 1781 TypeFromParser parser_clang_type(clang_type); 1782 1783 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type); 1784 1785 ClangExpressionVariable *entity(new ClangExpressionVariable( 1786 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), 1787 m_parser_vars->m_target_info.byte_order, 1788 m_parser_vars->m_target_info.address_byte_size)); 1789 m_found_entities.AddNewlyConstructedVariable(entity); 1790 1791 std::string decl_name(context.m_decl_name.getAsString()); 1792 entity->SetName(ConstString(decl_name.c_str())); 1793 entity->SetRegisterInfo(reg_info); 1794 entity->EnableParserVars(GetParserID()); 1795 ClangExpressionVariable::ParserVars *parser_vars = 1796 entity->GetParserVars(GetParserID()); 1797 parser_vars->m_parser_type = parser_clang_type; 1798 parser_vars->m_named_decl = var_decl; 1799 parser_vars->m_llvm_value = nullptr; 1800 parser_vars->m_lldb_value.Clear(); 1801 entity->m_flags |= ClangExpressionVariable::EVBareRegister; 1802 1803 LLDB_LOG(log, " CEDM::FEVD[{0}] Added register {1}, returned\n{2}", 1804 current_id, context.m_decl_name.getAsString(), 1805 ClangUtil::DumpDecl(var_decl)); 1806 } 1807 1808 void ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context, 1809 Function *function, Symbol *symbol, 1810 unsigned int current_id) { 1811 assert(m_parser_vars.get()); 1812 1813 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1814 1815 NamedDecl *function_decl = nullptr; 1816 Address fun_address; 1817 CompilerType function_clang_type; 1818 1819 bool is_indirect_function = false; 1820 1821 if (function) { 1822 Type *function_type = function->GetType(); 1823 1824 const auto lang = function->GetCompileUnit()->GetLanguage(); 1825 const auto name = function->GetMangled().GetMangledName().AsCString(); 1826 const bool extern_c = (Language::LanguageIsC(lang) && 1827 !CPlusPlusLanguage::IsCPPMangledName(name)) || 1828 (Language::LanguageIsObjC(lang) && 1829 !Language::LanguageIsCPlusPlus(lang)); 1830 1831 if (!extern_c) { 1832 TypeSystem *type_system = function->GetDeclContext().GetTypeSystem(); 1833 if (llvm::isa<ClangASTContext>(type_system)) { 1834 clang::DeclContext *src_decl_context = 1835 (clang::DeclContext *)function->GetDeclContext() 1836 .GetOpaqueDeclContext(); 1837 clang::FunctionDecl *src_function_decl = 1838 llvm::dyn_cast_or_null<clang::FunctionDecl>(src_decl_context); 1839 if (src_function_decl && 1840 src_function_decl->getTemplateSpecializationInfo()) { 1841 clang::FunctionTemplateDecl *function_template = 1842 src_function_decl->getTemplateSpecializationInfo()->getTemplate(); 1843 clang::FunctionTemplateDecl *copied_function_template = 1844 llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>( 1845 CopyDecl(function_template)); 1846 if (copied_function_template) { 1847 if (log) { 1848 StreamString ss; 1849 1850 function->DumpSymbolContext(&ss); 1851 1852 LLDB_LOG(log, 1853 " CEDM::FEVD[{0}] Imported decl for function template" 1854 " {1} (description {2}), returned\n{3}", 1855 current_id, copied_function_template->getNameAsString(), 1856 ss.GetData(), 1857 ClangUtil::DumpDecl(copied_function_template)); 1858 } 1859 1860 context.AddNamedDecl(copied_function_template); 1861 } 1862 } else if (src_function_decl) { 1863 if (clang::FunctionDecl *copied_function_decl = 1864 llvm::dyn_cast_or_null<clang::FunctionDecl>( 1865 CopyDecl(src_function_decl))) { 1866 if (log) { 1867 StreamString ss; 1868 1869 function->DumpSymbolContext(&ss); 1870 1871 LLDB_LOG(log, 1872 " CEDM::FEVD[{0}]] Imported decl for function {1} " 1873 "(description {2}), returned\n{3}", 1874 current_id, copied_function_decl->getNameAsString(), 1875 ss.GetData(), ClangUtil::DumpDecl(copied_function_decl)); 1876 } 1877 1878 context.AddNamedDecl(copied_function_decl); 1879 return; 1880 } else { 1881 if (log) { 1882 LLDB_LOGF(log, " Failed to import the function decl for '%s'", 1883 src_function_decl->getName().str().c_str()); 1884 } 1885 } 1886 } 1887 } 1888 } 1889 1890 if (!function_type) { 1891 if (log) 1892 log->PutCString(" Skipped a function because it has no type"); 1893 return; 1894 } 1895 1896 function_clang_type = function_type->GetFullCompilerType(); 1897 1898 if (!function_clang_type) { 1899 if (log) 1900 log->PutCString(" Skipped a function because it has no Clang type"); 1901 return; 1902 } 1903 1904 fun_address = function->GetAddressRange().GetBaseAddress(); 1905 1906 CompilerType copied_function_type = GuardedCopyType(function_clang_type); 1907 if (copied_function_type) { 1908 function_decl = context.AddFunDecl(copied_function_type, extern_c); 1909 1910 if (!function_decl) { 1911 if (log) { 1912 LLDB_LOGF( 1913 log, 1914 " Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}", 1915 function_type->GetName().GetCString(), function_type->GetID()); 1916 } 1917 1918 return; 1919 } 1920 } else { 1921 // We failed to copy the type we found 1922 if (log) { 1923 LLDB_LOGF(log, 1924 " Failed to import the function type '%s' {0x%8.8" PRIx64 1925 "} into the expression parser AST contenxt", 1926 function_type->GetName().GetCString(), 1927 function_type->GetID()); 1928 } 1929 1930 return; 1931 } 1932 } else if (symbol) { 1933 fun_address = symbol->GetAddress(); 1934 function_decl = context.AddGenericFunDecl(); 1935 is_indirect_function = symbol->IsIndirect(); 1936 } else { 1937 if (log) 1938 log->PutCString(" AddOneFunction called with no function and no symbol"); 1939 return; 1940 } 1941 1942 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 1943 1944 lldb::addr_t load_addr = 1945 fun_address.GetCallableLoadAddress(target, is_indirect_function); 1946 1947 ClangExpressionVariable *entity(new ClangExpressionVariable( 1948 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), 1949 m_parser_vars->m_target_info.byte_order, 1950 m_parser_vars->m_target_info.address_byte_size)); 1951 m_found_entities.AddNewlyConstructedVariable(entity); 1952 1953 std::string decl_name(context.m_decl_name.getAsString()); 1954 entity->SetName(ConstString(decl_name.c_str())); 1955 entity->SetCompilerType(function_clang_type); 1956 entity->EnableParserVars(GetParserID()); 1957 1958 ClangExpressionVariable::ParserVars *parser_vars = 1959 entity->GetParserVars(GetParserID()); 1960 1961 if (load_addr != LLDB_INVALID_ADDRESS) { 1962 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress); 1963 parser_vars->m_lldb_value.GetScalar() = load_addr; 1964 } else { 1965 // We have to try finding a file address. 1966 1967 lldb::addr_t file_addr = fun_address.GetFileAddress(); 1968 1969 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress); 1970 parser_vars->m_lldb_value.GetScalar() = file_addr; 1971 } 1972 1973 parser_vars->m_named_decl = function_decl; 1974 parser_vars->m_llvm_value = nullptr; 1975 1976 if (log) { 1977 StreamString ss; 1978 1979 fun_address.Dump(&ss, 1980 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), 1981 Address::DumpStyleResolvedDescription); 1982 1983 LLDB_LOG(log, 1984 " CEDM::FEVD[{0}] Found {1} function {2} (description {3}), " 1985 "returned\n{4}", 1986 current_id, (function ? "specific" : "generic"), decl_name, 1987 ss.GetData(), ClangUtil::DumpDecl(function_decl)); 1988 } 1989 } 1990 1991 void ClangExpressionDeclMap::AddThisType(NameSearchContext &context, 1992 const TypeFromUser &ut, 1993 unsigned int current_id) { 1994 CompilerType copied_clang_type = GuardedCopyType(ut); 1995 1996 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1997 1998 if (!copied_clang_type) { 1999 if (log) 2000 LLDB_LOGF( 2001 log, 2002 "ClangExpressionDeclMap::AddThisType - Couldn't import the type"); 2003 2004 return; 2005 } 2006 2007 if (copied_clang_type.IsAggregateType() && 2008 copied_clang_type.GetCompleteType()) { 2009 CompilerType void_clang_type = 2010 m_clang_ast_context->GetBasicType(eBasicTypeVoid); 2011 CompilerType void_ptr_clang_type = void_clang_type.GetPointerType(); 2012 2013 CompilerType method_type = ClangASTContext::CreateFunctionType( 2014 m_ast_context, void_clang_type, &void_ptr_clang_type, 1, false, 0); 2015 2016 const bool is_virtual = false; 2017 const bool is_static = false; 2018 const bool is_inline = false; 2019 const bool is_explicit = false; 2020 const bool is_attr_used = true; 2021 const bool is_artificial = false; 2022 2023 CXXMethodDecl *method_decl = m_clang_ast_context->AddMethodToCXXRecordType( 2024 copied_clang_type.GetOpaqueQualType(), "$__lldb_expr", nullptr, 2025 method_type, lldb::eAccessPublic, is_virtual, is_static, is_inline, 2026 is_explicit, is_attr_used, is_artificial); 2027 2028 LLDB_LOG(log, 2029 " CEDM::AddThisType Added function $__lldb_expr " 2030 "(description {0}) for this type\n{1}", 2031 ClangUtil::ToString(copied_clang_type), 2032 ClangUtil::DumpDecl(method_decl)); 2033 } 2034 2035 if (!copied_clang_type.IsValid()) 2036 return; 2037 2038 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo( 2039 QualType::getFromOpaquePtr(copied_clang_type.GetOpaqueQualType())); 2040 2041 if (!type_source_info) 2042 return; 2043 2044 // Construct a typedef type because if "*this" is a templated type we can't 2045 // just return ClassTemplateSpecializationDecls in response to name queries. 2046 // Using a typedef makes this much more robust. 2047 2048 TypedefDecl *typedef_decl = TypedefDecl::Create( 2049 *m_ast_context, m_ast_context->getTranslationUnitDecl(), SourceLocation(), 2050 SourceLocation(), context.m_decl_name.getAsIdentifierInfo(), 2051 type_source_info); 2052 2053 if (!typedef_decl) 2054 return; 2055 2056 context.AddNamedDecl(typedef_decl); 2057 2058 return; 2059 } 2060 2061 void ClangExpressionDeclMap::AddOneType(NameSearchContext &context, 2062 const TypeFromUser &ut, 2063 unsigned int current_id) { 2064 CompilerType copied_clang_type = GuardedCopyType(ut); 2065 2066 if (!copied_clang_type) { 2067 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 2068 2069 if (log) 2070 LLDB_LOGF( 2071 log, "ClangExpressionDeclMap::AddOneType - Couldn't import the type"); 2072 2073 return; 2074 } 2075 2076 context.AddTypeDecl(copied_clang_type); 2077 } 2078