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