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