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