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