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