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