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 = GetClangASTContext()->CreateDeclContext(const_cast<clang::DeclContext*>(context.m_decl_context)); 686 FindExternalVisibleDecls(context, lldb::ModuleSP(), compiler_decl_ctx, 687 current_id); 688 return; 689 } 690 691 ClangASTImporter::NamespaceMapSP namespace_map = 692 m_ast_importer_sp 693 ? m_ast_importer_sp->GetNamespaceMap(namespace_context) 694 : ClangASTImporter::NamespaceMapSP(); 695 696 if (!namespace_map) 697 return; 698 699 if (log && log->GetVerbose()) 700 log->Printf(" CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)", 701 current_id, static_cast<void *>(namespace_map.get()), 702 (int)namespace_map->size()); 703 704 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), 705 e = namespace_map->end(); 706 i != e; ++i) { 707 if (log) 708 log->Printf(" CEDM::FEVD[%u] Searching namespace %s in module %s", 709 current_id, i->second.GetName().AsCString(), 710 i->first->GetFileSpec().GetFilename().GetCString()); 711 712 FindExternalVisibleDecls(context, i->first, i->second, current_id); 713 } 714 } else if (isa<TranslationUnitDecl>(context.m_decl_context)) { 715 CompilerDeclContext namespace_decl; 716 717 if (log) 718 log->Printf(" CEDM::FEVD[%u] Searching the root namespace", current_id); 719 720 FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl, 721 current_id); 722 } 723 724 ClangASTSource::FindExternalVisibleDecls(context); 725 } 726 727 void ClangExpressionDeclMap::MaybeRegisterFunctionBody( 728 FunctionDecl *copied_function_decl) { 729 if (copied_function_decl->getBody() && m_parser_vars->m_code_gen) { 730 clang::DeclGroupRef decl_group_ref(copied_function_decl); 731 m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref); 732 } 733 } 734 735 clang::NamedDecl *ClangExpressionDeclMap::GetPersistentDecl(ConstString name) { 736 if (!m_parser_vars) 737 return nullptr; 738 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 739 if (!target) 740 return nullptr; 741 742 ClangASTContext::GetScratch(*target); 743 744 return m_parser_vars->m_persistent_vars->GetPersistentDecl(name); 745 } 746 747 void ClangExpressionDeclMap::SearchPersistenDecls(NameSearchContext &context, 748 const ConstString name, 749 unsigned int current_id) { 750 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 751 752 NamedDecl *persistent_decl = GetPersistentDecl(name); 753 754 if (!persistent_decl) 755 return; 756 757 Decl *parser_persistent_decl = CopyDecl(persistent_decl); 758 759 if (!parser_persistent_decl) 760 return; 761 762 NamedDecl *parser_named_decl = dyn_cast<NamedDecl>(parser_persistent_decl); 763 764 if (!parser_named_decl) 765 return; 766 767 if (clang::FunctionDecl *parser_function_decl = 768 llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl)) { 769 MaybeRegisterFunctionBody(parser_function_decl); 770 } 771 772 LLDB_LOGF(log, " CEDM::FEVD[%u] Found persistent decl %s", current_id, 773 name.GetCString()); 774 775 context.AddNamedDecl(parser_named_decl); 776 } 777 778 void ClangExpressionDeclMap::LookUpLldbClass(NameSearchContext &context, 779 unsigned int current_id) { 780 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 781 782 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); 783 SymbolContext sym_ctx; 784 if (frame != nullptr) 785 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | 786 lldb::eSymbolContextBlock); 787 788 if (m_ctx_obj) { 789 Status status; 790 lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status); 791 if (!ctx_obj_ptr || status.Fail()) 792 return; 793 794 AddThisType(context, TypeFromUser(m_ctx_obj->GetCompilerType()), 795 current_id); 796 797 m_struct_vars->m_object_pointer_type = 798 TypeFromUser(ctx_obj_ptr->GetCompilerType()); 799 800 return; 801 } 802 803 // Clang is looking for the type of "this" 804 805 if (frame == nullptr) 806 return; 807 808 // Find the block that defines the function represented by "sym_ctx" 809 Block *function_block = sym_ctx.GetFunctionBlock(); 810 811 if (!function_block) 812 return; 813 814 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext(); 815 816 if (!function_decl_ctx) 817 return; 818 819 clang::CXXMethodDecl *method_decl = 820 ClangASTContext::DeclContextGetAsCXXMethodDecl(function_decl_ctx); 821 822 if (method_decl) { 823 clang::CXXRecordDecl *class_decl = method_decl->getParent(); 824 825 QualType class_qual_type(class_decl->getTypeForDecl(), 0); 826 827 TypeFromUser class_user_type( 828 class_qual_type.getAsOpaquePtr(), 829 ClangASTContext::GetASTContext(&class_decl->getASTContext())); 830 831 LLDB_LOG(log, " CEDM::FEVD[{0}] Adding type for $__lldb_class: {1}", 832 current_id, class_qual_type.getAsString()); 833 834 AddThisType(context, class_user_type, current_id); 835 836 if (method_decl->isInstance()) { 837 // self is a pointer to the object 838 839 QualType class_pointer_type = 840 method_decl->getASTContext().getPointerType(class_qual_type); 841 842 TypeFromUser self_user_type( 843 class_pointer_type.getAsOpaquePtr(), 844 ClangASTContext::GetASTContext(&method_decl->getASTContext())); 845 846 m_struct_vars->m_object_pointer_type = self_user_type; 847 } 848 return; 849 } 850 851 // This branch will get hit if we are executing code in the context of 852 // a function that claims to have an object pointer (through 853 // DW_AT_object_pointer?) but is not formally a method of the class. 854 // In that case, just look up the "this" variable in the current scope 855 // and use its type. 856 // FIXME: This code is formally correct, but clang doesn't currently 857 // emit DW_AT_object_pointer 858 // for C++ so it hasn't actually been tested. 859 860 VariableList *vars = frame->GetVariableList(false); 861 862 lldb::VariableSP this_var = vars->FindVariable(ConstString("this")); 863 864 if (this_var && this_var->IsInScope(frame) && 865 this_var->LocationIsValidForFrame(frame)) { 866 Type *this_type = this_var->GetType(); 867 868 if (!this_type) 869 return; 870 871 TypeFromUser pointee_type = 872 this_type->GetForwardCompilerType().GetPointeeType(); 873 874 LLDB_LOG(log, " FEVD[{0}] Adding type for $__lldb_class: {1}", current_id, 875 ClangUtil::GetQualType(pointee_type).getAsString()); 876 877 AddThisType(context, pointee_type, current_id); 878 TypeFromUser this_user_type(this_type->GetFullCompilerType()); 879 m_struct_vars->m_object_pointer_type = this_user_type; 880 } 881 } 882 883 void ClangExpressionDeclMap::LookUpLldbObjCClass(NameSearchContext &context, 884 unsigned int current_id) { 885 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 886 887 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); 888 889 if (m_ctx_obj) { 890 Status status; 891 lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status); 892 if (!ctx_obj_ptr || status.Fail()) 893 return; 894 895 AddOneType(context, TypeFromUser(m_ctx_obj->GetCompilerType()), current_id); 896 897 m_struct_vars->m_object_pointer_type = 898 TypeFromUser(ctx_obj_ptr->GetCompilerType()); 899 900 return; 901 } 902 903 // Clang is looking for the type of "*self" 904 905 if (!frame) 906 return; 907 908 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | 909 lldb::eSymbolContextBlock); 910 911 // Find the block that defines the function represented by "sym_ctx" 912 Block *function_block = sym_ctx.GetFunctionBlock(); 913 914 if (!function_block) 915 return; 916 917 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext(); 918 919 if (!function_decl_ctx) 920 return; 921 922 clang::ObjCMethodDecl *method_decl = 923 ClangASTContext::DeclContextGetAsObjCMethodDecl(function_decl_ctx); 924 925 if (method_decl) { 926 ObjCInterfaceDecl *self_interface = method_decl->getClassInterface(); 927 928 if (!self_interface) 929 return; 930 931 const clang::Type *interface_type = self_interface->getTypeForDecl(); 932 933 if (!interface_type) 934 return; // This is unlikely, but we have seen crashes where this 935 // occurred 936 937 TypeFromUser class_user_type( 938 QualType(interface_type, 0).getAsOpaquePtr(), 939 ClangASTContext::GetASTContext(&method_decl->getASTContext())); 940 941 LLDB_LOG(log, " FEVD[{0}] Adding type for $__lldb_objc_class: {1}", 942 current_id, ClangUtil::ToString(interface_type)); 943 944 AddOneType(context, class_user_type, current_id); 945 946 if (method_decl->isInstanceMethod()) { 947 // self is a pointer to the object 948 949 QualType class_pointer_type = 950 method_decl->getASTContext().getObjCObjectPointerType( 951 QualType(interface_type, 0)); 952 953 TypeFromUser self_user_type( 954 class_pointer_type.getAsOpaquePtr(), 955 ClangASTContext::GetASTContext(&method_decl->getASTContext())); 956 957 m_struct_vars->m_object_pointer_type = self_user_type; 958 } else { 959 // self is a Class pointer 960 QualType class_type = method_decl->getASTContext().getObjCClassType(); 961 962 TypeFromUser self_user_type( 963 class_type.getAsOpaquePtr(), 964 ClangASTContext::GetASTContext(&method_decl->getASTContext())); 965 966 m_struct_vars->m_object_pointer_type = self_user_type; 967 } 968 969 return; 970 } 971 // This branch will get hit if we are executing code in the context of 972 // a function that claims to have an object pointer (through 973 // DW_AT_object_pointer?) but is not formally a method of the class. 974 // In that case, just look up the "self" variable in the current scope 975 // and use its type. 976 977 VariableList *vars = frame->GetVariableList(false); 978 979 lldb::VariableSP self_var = vars->FindVariable(ConstString("self")); 980 981 if (!self_var) 982 return; 983 if (!self_var->IsInScope(frame)) 984 return; 985 if (!self_var->LocationIsValidForFrame(frame)) 986 return; 987 988 Type *self_type = self_var->GetType(); 989 990 if (!self_type) 991 return; 992 993 CompilerType self_clang_type = self_type->GetFullCompilerType(); 994 995 if (ClangASTContext::IsObjCClassType(self_clang_type)) { 996 return; 997 } 998 if (!ClangASTContext::IsObjCObjectPointerType(self_clang_type)) 999 return; 1000 self_clang_type = self_clang_type.GetPointeeType(); 1001 1002 if (!self_clang_type) 1003 return; 1004 1005 LLDB_LOG(log, " FEVD[{0}] Adding type for $__lldb_objc_class: {1}", 1006 current_id, ClangUtil::ToString(self_type->GetFullCompilerType())); 1007 1008 TypeFromUser class_user_type(self_clang_type); 1009 1010 AddOneType(context, class_user_type, current_id); 1011 1012 TypeFromUser self_user_type(self_type->GetFullCompilerType()); 1013 1014 m_struct_vars->m_object_pointer_type = self_user_type; 1015 } 1016 1017 void ClangExpressionDeclMap::LookupLocalVarNamespace( 1018 SymbolContext &sym_ctx, NameSearchContext &name_context) { 1019 if (sym_ctx.block == nullptr) 1020 return; 1021 1022 CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext(); 1023 if (!frame_decl_context) 1024 return; 1025 1026 ClangASTContext *frame_ast = llvm::dyn_cast_or_null<ClangASTContext>( 1027 frame_decl_context.GetTypeSystem()); 1028 if (!frame_ast) 1029 return; 1030 1031 clang::NamespaceDecl *namespace_decl = 1032 m_clang_ast_context->GetUniqueNamespaceDeclaration( 1033 g_lldb_local_vars_namespace_cstr, nullptr); 1034 if (!namespace_decl) 1035 return; 1036 1037 name_context.AddNamedDecl(namespace_decl); 1038 clang::DeclContext *ctxt = clang::Decl::castToDeclContext(namespace_decl); 1039 ctxt->setHasExternalVisibleStorage(true); 1040 name_context.m_found.local_vars_nsp = true; 1041 } 1042 1043 void ClangExpressionDeclMap::LookupInModulesDeclVendor( 1044 NameSearchContext &context, ConstString name, unsigned current_id) { 1045 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1046 1047 if (!m_target) 1048 return; 1049 1050 auto *modules_decl_vendor = m_target->GetClangModulesDeclVendor(); 1051 if (!modules_decl_vendor) 1052 return; 1053 1054 bool append = false; 1055 uint32_t max_matches = 1; 1056 std::vector<clang::NamedDecl *> decls; 1057 1058 if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls)) 1059 return; 1060 1061 assert(!decls.empty() && "FindDecls returned true but no decls?"); 1062 clang::NamedDecl *const decl_from_modules = decls[0]; 1063 1064 LLDB_LOG(log, 1065 " CAS::FEVD[{0}] Matching decl found for " 1066 "\"{1}\" in the modules", 1067 current_id, name); 1068 1069 clang::Decl *copied_decl = CopyDecl(decl_from_modules); 1070 if (!copied_decl) { 1071 LLDB_LOG(log, 1072 " CAS::FEVD[{0}] - Couldn't export a " 1073 "declaration from the modules", 1074 current_id); 1075 return; 1076 } 1077 1078 if (auto copied_function = dyn_cast<clang::FunctionDecl>(copied_decl)) { 1079 MaybeRegisterFunctionBody(copied_function); 1080 1081 context.AddNamedDecl(copied_function); 1082 1083 context.m_found.function_with_type_info = true; 1084 context.m_found.function = true; 1085 } else if (auto copied_var = dyn_cast<clang::VarDecl>(copied_decl)) { 1086 context.AddNamedDecl(copied_var); 1087 context.m_found.variable = true; 1088 } 1089 } 1090 1091 bool ClangExpressionDeclMap::LookupLocalVariable( 1092 NameSearchContext &context, ConstString name, unsigned current_id, 1093 SymbolContext &sym_ctx, CompilerDeclContext &namespace_decl) { 1094 if (sym_ctx.block == nullptr) 1095 return false; 1096 1097 CompilerDeclContext decl_context = sym_ctx.block->GetDeclContext(); 1098 if (!decl_context) 1099 return false; 1100 1101 // Make sure that the variables are parsed so that we have the 1102 // declarations. 1103 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); 1104 VariableListSP vars = frame->GetInScopeVariableList(true); 1105 for (size_t i = 0; i < vars->GetSize(); i++) 1106 vars->GetVariableAtIndex(i)->GetDecl(); 1107 1108 // Search for declarations matching the name. Do not include imported 1109 // decls in the search if we are looking for decls in the artificial 1110 // namespace $__lldb_local_vars. 1111 std::vector<CompilerDecl> found_decls = 1112 decl_context.FindDeclByName(name, namespace_decl.IsValid()); 1113 1114 VariableSP var; 1115 bool variable_found = false; 1116 for (CompilerDecl decl : found_decls) { 1117 for (size_t vi = 0, ve = vars->GetSize(); vi != ve; ++vi) { 1118 VariableSP candidate_var = vars->GetVariableAtIndex(vi); 1119 if (candidate_var->GetDecl() == decl) { 1120 var = candidate_var; 1121 break; 1122 } 1123 } 1124 1125 if (var && !variable_found) { 1126 variable_found = true; 1127 ValueObjectSP valobj = ValueObjectVariable::Create(frame, var); 1128 AddOneVariable(context, var, valobj, current_id); 1129 context.m_found.variable = true; 1130 } 1131 } 1132 return variable_found; 1133 } 1134 1135 /// Structure to hold the info needed when comparing function 1136 /// declarations. 1137 namespace { 1138 struct FuncDeclInfo { 1139 ConstString m_name; 1140 CompilerType m_copied_type; 1141 uint32_t m_decl_lvl; 1142 SymbolContext m_sym_ctx; 1143 }; 1144 } // namespace 1145 1146 SymbolContextList ClangExpressionDeclMap::SearchFunctionsInSymbolContexts( 1147 const SymbolContextList &sc_list, 1148 const CompilerDeclContext &frame_decl_context) { 1149 // First, symplify things by looping through the symbol contexts to 1150 // remove unwanted functions and separate out the functions we want to 1151 // compare and prune into a separate list. Cache the info needed about 1152 // the function declarations in a vector for efficiency. 1153 uint32_t num_indices = sc_list.GetSize(); 1154 SymbolContextList sc_sym_list; 1155 std::vector<FuncDeclInfo> decl_infos; 1156 decl_infos.reserve(num_indices); 1157 clang::DeclContext *frame_decl_ctx = 1158 (clang::DeclContext *)frame_decl_context.GetOpaqueDeclContext(); 1159 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>( 1160 frame_decl_context.GetTypeSystem()); 1161 1162 for (uint32_t index = 0; index < num_indices; ++index) { 1163 FuncDeclInfo fdi; 1164 SymbolContext sym_ctx; 1165 sc_list.GetContextAtIndex(index, sym_ctx); 1166 1167 // We don't know enough about symbols to compare them, but we should 1168 // keep them in the list. 1169 Function *function = sym_ctx.function; 1170 if (!function) { 1171 sc_sym_list.Append(sym_ctx); 1172 continue; 1173 } 1174 // Filter out functions without declaration contexts, as well as 1175 // class/instance methods, since they'll be skipped in the code that 1176 // follows anyway. 1177 CompilerDeclContext func_decl_context = function->GetDeclContext(); 1178 if (!func_decl_context || 1179 func_decl_context.IsClassMethod(nullptr, nullptr, nullptr)) 1180 continue; 1181 // We can only prune functions for which we can copy the type. 1182 CompilerType func_clang_type = function->GetType()->GetFullCompilerType(); 1183 CompilerType copied_func_type = GuardedCopyType(func_clang_type); 1184 if (!copied_func_type) { 1185 sc_sym_list.Append(sym_ctx); 1186 continue; 1187 } 1188 1189 fdi.m_sym_ctx = sym_ctx; 1190 fdi.m_name = function->GetName(); 1191 fdi.m_copied_type = copied_func_type; 1192 fdi.m_decl_lvl = LLDB_INVALID_DECL_LEVEL; 1193 if (fdi.m_copied_type && func_decl_context) { 1194 // Call CountDeclLevels to get the number of parent scopes we have 1195 // to look through before we find the function declaration. When 1196 // comparing functions of the same type, the one with a lower count 1197 // will be closer to us in the lookup scope and shadows the other. 1198 clang::DeclContext *func_decl_ctx = 1199 (clang::DeclContext *)func_decl_context.GetOpaqueDeclContext(); 1200 fdi.m_decl_lvl = ast->CountDeclLevels(frame_decl_ctx, func_decl_ctx, 1201 &fdi.m_name, &fdi.m_copied_type); 1202 } 1203 decl_infos.emplace_back(fdi); 1204 } 1205 1206 // Loop through the functions in our cache looking for matching types, 1207 // then compare their scope levels to see which is closer. 1208 std::multimap<CompilerType, const FuncDeclInfo *> matches; 1209 for (const FuncDeclInfo &fdi : decl_infos) { 1210 const CompilerType t = fdi.m_copied_type; 1211 auto q = matches.find(t); 1212 if (q != matches.end()) { 1213 if (q->second->m_decl_lvl > fdi.m_decl_lvl) 1214 // This function is closer; remove the old set. 1215 matches.erase(t); 1216 else if (q->second->m_decl_lvl < fdi.m_decl_lvl) 1217 // The functions in our set are closer - skip this one. 1218 continue; 1219 } 1220 matches.insert(std::make_pair(t, &fdi)); 1221 } 1222 1223 // Loop through our matches and add their symbol contexts to our list. 1224 SymbolContextList sc_func_list; 1225 for (const auto &q : matches) 1226 sc_func_list.Append(q.second->m_sym_ctx); 1227 1228 // Rejoin the lists with the functions in front. 1229 sc_func_list.Append(sc_sym_list); 1230 return sc_func_list; 1231 } 1232 1233 void ClangExpressionDeclMap::LookupFunction(NameSearchContext &context, 1234 lldb::ModuleSP module_sp, 1235 ConstString name, 1236 CompilerDeclContext &namespace_decl, 1237 unsigned current_id) { 1238 if (!m_parser_vars) 1239 return; 1240 1241 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 1242 1243 std::vector<clang::NamedDecl *> decls_from_modules; 1244 1245 if (target) { 1246 if (ClangModulesDeclVendor *decl_vendor = 1247 target->GetClangModulesDeclVendor()) { 1248 decl_vendor->FindDecls(name, false, UINT32_MAX, decls_from_modules); 1249 } 1250 } 1251 1252 const bool include_inlines = false; 1253 SymbolContextList sc_list; 1254 if (namespace_decl && module_sp) { 1255 const bool include_symbols = false; 1256 1257 module_sp->FindFunctions(name, &namespace_decl, eFunctionNameTypeBase, 1258 include_symbols, include_inlines, sc_list); 1259 } else if (target && !namespace_decl) { 1260 const bool include_symbols = true; 1261 1262 // TODO Fix FindFunctions so that it doesn't return 1263 // instance methods for eFunctionNameTypeBase. 1264 1265 target->GetImages().FindFunctions(name, eFunctionNameTypeFull, 1266 include_symbols, include_inlines, 1267 sc_list); 1268 } 1269 1270 // If we found more than one function, see if we can use the frame's decl 1271 // context to remove functions that are shadowed by other functions which 1272 // match in type but are nearer in scope. 1273 // 1274 // AddOneFunction will not add a function whose type has already been 1275 // added, so if there's another function in the list with a matching type, 1276 // check to see if their decl context is a parent of the current frame's or 1277 // was imported via a and using statement, and pick the best match 1278 // according to lookup rules. 1279 if (sc_list.GetSize() > 1) { 1280 // Collect some info about our frame's context. 1281 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); 1282 SymbolContext frame_sym_ctx; 1283 if (frame != nullptr) 1284 frame_sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | 1285 lldb::eSymbolContextBlock); 1286 CompilerDeclContext frame_decl_context = 1287 frame_sym_ctx.block != nullptr ? frame_sym_ctx.block->GetDeclContext() 1288 : CompilerDeclContext(); 1289 1290 // We can't do this without a compiler decl context for our frame. 1291 if (frame_decl_context) { 1292 sc_list = SearchFunctionsInSymbolContexts(sc_list, frame_decl_context); 1293 } 1294 } 1295 1296 if (sc_list.GetSize()) { 1297 Symbol *extern_symbol = nullptr; 1298 Symbol *non_extern_symbol = nullptr; 1299 1300 for (uint32_t index = 0, num_indices = sc_list.GetSize(); 1301 index < num_indices; ++index) { 1302 SymbolContext sym_ctx; 1303 sc_list.GetContextAtIndex(index, sym_ctx); 1304 1305 if (sym_ctx.function) { 1306 CompilerDeclContext decl_ctx = sym_ctx.function->GetDeclContext(); 1307 1308 if (!decl_ctx) 1309 continue; 1310 1311 // Filter out class/instance methods. 1312 if (decl_ctx.IsClassMethod(nullptr, nullptr, nullptr)) 1313 continue; 1314 1315 AddOneFunction(context, sym_ctx.function, nullptr, current_id); 1316 context.m_found.function_with_type_info = true; 1317 context.m_found.function = true; 1318 } else if (sym_ctx.symbol) { 1319 if (sym_ctx.symbol->GetType() == eSymbolTypeReExported && target) { 1320 sym_ctx.symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target); 1321 if (sym_ctx.symbol == nullptr) 1322 continue; 1323 } 1324 1325 if (sym_ctx.symbol->IsExternal()) 1326 extern_symbol = sym_ctx.symbol; 1327 else 1328 non_extern_symbol = sym_ctx.symbol; 1329 } 1330 } 1331 1332 if (!context.m_found.function_with_type_info) { 1333 for (clang::NamedDecl *decl : decls_from_modules) { 1334 if (llvm::isa<clang::FunctionDecl>(decl)) { 1335 clang::NamedDecl *copied_decl = 1336 llvm::cast_or_null<FunctionDecl>(CopyDecl(decl)); 1337 if (copied_decl) { 1338 context.AddNamedDecl(copied_decl); 1339 context.m_found.function_with_type_info = true; 1340 } 1341 } 1342 } 1343 } 1344 1345 if (!context.m_found.function_with_type_info) { 1346 if (extern_symbol) { 1347 AddOneFunction(context, nullptr, extern_symbol, current_id); 1348 context.m_found.function = true; 1349 } else if (non_extern_symbol) { 1350 AddOneFunction(context, nullptr, non_extern_symbol, current_id); 1351 context.m_found.function = true; 1352 } 1353 } 1354 } 1355 } 1356 1357 void ClangExpressionDeclMap::FindExternalVisibleDecls( 1358 NameSearchContext &context, lldb::ModuleSP module_sp, 1359 CompilerDeclContext &namespace_decl, unsigned int current_id) { 1360 assert(m_ast_context); 1361 1362 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1363 1364 const ConstString name(context.m_decl_name.getAsString().c_str()); 1365 if (IgnoreName(name, false)) 1366 return; 1367 1368 // Only look for functions by name out in our symbols if the function doesn't 1369 // start with our phony prefix of '$' 1370 1371 Target *target = nullptr; 1372 StackFrame *frame = nullptr; 1373 SymbolContext sym_ctx; 1374 if (m_parser_vars) { 1375 target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 1376 frame = m_parser_vars->m_exe_ctx.GetFramePtr(); 1377 } 1378 if (frame != nullptr) 1379 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | 1380 lldb::eSymbolContextBlock); 1381 1382 // Try the persistent decls, which take precedence over all else. 1383 if (!namespace_decl) 1384 SearchPersistenDecls(context, name, current_id); 1385 1386 if (name.GetStringRef().startswith("$") && !namespace_decl) { 1387 if (name == "$__lldb_class") { 1388 LookUpLldbClass(context, current_id); 1389 return; 1390 } 1391 1392 if (name == "$__lldb_objc_class") { 1393 LookUpLldbObjCClass(context, current_id); 1394 return; 1395 } 1396 if (name == g_lldb_local_vars_namespace_cstr) { 1397 LookupLocalVarNamespace(sym_ctx, context); 1398 return; 1399 } 1400 1401 // any other $__lldb names should be weeded out now 1402 if (name.GetStringRef().startswith("$__lldb")) 1403 return; 1404 1405 // No ParserVars means we can't do register or variable lookup. 1406 if (!m_parser_vars) 1407 return; 1408 1409 ExpressionVariableSP pvar_sp( 1410 m_parser_vars->m_persistent_vars->GetVariable(name)); 1411 1412 if (pvar_sp) { 1413 AddOneVariable(context, pvar_sp, current_id); 1414 return; 1415 } 1416 1417 assert(name.GetStringRef().startswith("$")); 1418 llvm::StringRef reg_name = name.GetStringRef().substr(1); 1419 1420 if (m_parser_vars->m_exe_ctx.GetRegisterContext()) { 1421 const RegisterInfo *reg_info( 1422 m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName( 1423 reg_name)); 1424 1425 if (reg_info) { 1426 LLDB_LOGF(log, " CEDM::FEVD[%u] Found register %s", current_id, 1427 reg_info->name); 1428 1429 AddOneRegister(context, reg_info, current_id); 1430 } 1431 } 1432 return; 1433 } 1434 1435 bool local_var_lookup = !namespace_decl || (namespace_decl.GetName() == 1436 g_lldb_local_vars_namespace_cstr); 1437 if (frame && local_var_lookup) 1438 if (LookupLocalVariable(context, name, current_id, sym_ctx, namespace_decl)) 1439 return; 1440 1441 if (target) { 1442 ValueObjectSP valobj; 1443 VariableSP var; 1444 var = 1445 FindGlobalVariable(*target, module_sp, name, &namespace_decl, nullptr); 1446 1447 if (var) { 1448 valobj = ValueObjectVariable::Create(target, var); 1449 AddOneVariable(context, var, valobj, current_id); 1450 context.m_found.variable = true; 1451 return; 1452 } 1453 } 1454 1455 LookupFunction(context, module_sp, name, namespace_decl, current_id); 1456 1457 // Try the modules next. 1458 if (!context.m_found.function_with_type_info) 1459 LookupInModulesDeclVendor(context, name, current_id); 1460 1461 if (target && !context.m_found.variable && !namespace_decl) { 1462 // We couldn't find a non-symbol variable for this. Now we'll hunt for a 1463 // generic data symbol, and -- if it is found -- treat it as a variable. 1464 Status error; 1465 1466 const Symbol *data_symbol = 1467 m_parser_vars->m_sym_ctx.FindBestGlobalDataSymbol(name, error); 1468 1469 if (!error.Success()) { 1470 const unsigned diag_id = 1471 m_ast_context->getDiagnostics().getCustomDiagID( 1472 clang::DiagnosticsEngine::Level::Error, "%0"); 1473 m_ast_context->getDiagnostics().Report(diag_id) << error.AsCString(); 1474 } 1475 1476 if (data_symbol) { 1477 std::string warning("got name from symbols: "); 1478 warning.append(name.AsCString()); 1479 const unsigned diag_id = 1480 m_ast_context->getDiagnostics().getCustomDiagID( 1481 clang::DiagnosticsEngine::Level::Warning, "%0"); 1482 m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str(); 1483 AddOneGenericVariable(context, *data_symbol, current_id); 1484 context.m_found.variable = true; 1485 } 1486 } 1487 } 1488 1489 bool ClangExpressionDeclMap::GetVariableValue(VariableSP &var, 1490 lldb_private::Value &var_location, 1491 TypeFromUser *user_type, 1492 TypeFromParser *parser_type) { 1493 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1494 1495 Type *var_type = var->GetType(); 1496 1497 if (!var_type) { 1498 if (log) 1499 log->PutCString("Skipped a definition because it has no type"); 1500 return false; 1501 } 1502 1503 CompilerType var_clang_type = var_type->GetFullCompilerType(); 1504 1505 if (!var_clang_type) { 1506 if (log) 1507 log->PutCString("Skipped a definition because it has no Clang type"); 1508 return false; 1509 } 1510 1511 ClangASTContext *clang_ast = llvm::dyn_cast_or_null<ClangASTContext>( 1512 var_type->GetForwardCompilerType().GetTypeSystem()); 1513 1514 if (!clang_ast) { 1515 if (log) 1516 log->PutCString("Skipped a definition because it has no Clang AST"); 1517 return false; 1518 } 1519 1520 DWARFExpression &var_location_expr = var->LocationExpression(); 1521 1522 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 1523 Status err; 1524 1525 if (var->GetLocationIsConstantValueData()) { 1526 DataExtractor const_value_extractor; 1527 1528 if (var_location_expr.GetExpressionData(const_value_extractor)) { 1529 var_location = Value(const_value_extractor.GetDataStart(), 1530 const_value_extractor.GetByteSize()); 1531 var_location.SetValueType(Value::eValueTypeHostAddress); 1532 } else { 1533 LLDB_LOGF(log, "Error evaluating constant variable: %s", err.AsCString()); 1534 return false; 1535 } 1536 } 1537 1538 CompilerType type_to_use = GuardedCopyType(var_clang_type); 1539 1540 if (!type_to_use) { 1541 LLDB_LOGF(log, 1542 "Couldn't copy a variable's type into the parser's AST context"); 1543 1544 return false; 1545 } 1546 1547 if (parser_type) 1548 *parser_type = TypeFromParser(type_to_use); 1549 1550 if (var_location.GetContextType() == Value::eContextTypeInvalid) 1551 var_location.SetCompilerType(type_to_use); 1552 1553 if (var_location.GetValueType() == Value::eValueTypeFileAddress) { 1554 SymbolContext var_sc; 1555 var->CalculateSymbolContext(&var_sc); 1556 1557 if (!var_sc.module_sp) 1558 return false; 1559 1560 Address so_addr(var_location.GetScalar().ULongLong(), 1561 var_sc.module_sp->GetSectionList()); 1562 1563 lldb::addr_t load_addr = so_addr.GetLoadAddress(target); 1564 1565 if (load_addr != LLDB_INVALID_ADDRESS) { 1566 var_location.GetScalar() = load_addr; 1567 var_location.SetValueType(Value::eValueTypeLoadAddress); 1568 } 1569 } 1570 1571 if (user_type) 1572 *user_type = TypeFromUser(var_clang_type); 1573 1574 return true; 1575 } 1576 1577 void ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context, 1578 VariableSP var, 1579 ValueObjectSP valobj, 1580 unsigned int current_id) { 1581 assert(m_parser_vars.get()); 1582 1583 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1584 1585 TypeFromUser ut; 1586 TypeFromParser pt; 1587 Value var_location; 1588 1589 if (!GetVariableValue(var, var_location, &ut, &pt)) 1590 return; 1591 1592 clang::QualType parser_opaque_type = 1593 QualType::getFromOpaquePtr(pt.GetOpaqueQualType()); 1594 1595 if (parser_opaque_type.isNull()) 1596 return; 1597 1598 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr()) { 1599 if (const TagType *tag_type = dyn_cast<TagType>(parser_type)) 1600 CompleteType(tag_type->getDecl()); 1601 if (const ObjCObjectPointerType *objc_object_ptr_type = 1602 dyn_cast<ObjCObjectPointerType>(parser_type)) 1603 CompleteType(objc_object_ptr_type->getInterfaceDecl()); 1604 } 1605 1606 bool is_reference = pt.IsReferenceType(); 1607 1608 NamedDecl *var_decl = nullptr; 1609 if (is_reference) 1610 var_decl = context.AddVarDecl(pt); 1611 else 1612 var_decl = context.AddVarDecl(pt.GetLValueReferenceType()); 1613 1614 std::string decl_name(context.m_decl_name.getAsString()); 1615 ConstString entity_name(decl_name.c_str()); 1616 ClangExpressionVariable *entity(new ClangExpressionVariable(valobj)); 1617 m_found_entities.AddNewlyConstructedVariable(entity); 1618 1619 assert(entity); 1620 entity->EnableParserVars(GetParserID()); 1621 ClangExpressionVariable::ParserVars *parser_vars = 1622 entity->GetParserVars(GetParserID()); 1623 parser_vars->m_parser_type = pt; 1624 parser_vars->m_named_decl = var_decl; 1625 parser_vars->m_llvm_value = nullptr; 1626 parser_vars->m_lldb_value = var_location; 1627 parser_vars->m_lldb_var = var; 1628 1629 if (is_reference) 1630 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference; 1631 1632 LLDB_LOG(log, 1633 " CEDM::FEVD[{0}] Found variable {1}, returned\n{2} (original {3})", 1634 current_id, decl_name, ClangUtil::DumpDecl(var_decl), 1635 ClangUtil::ToString(ut)); 1636 } 1637 1638 void ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context, 1639 ExpressionVariableSP &pvar_sp, 1640 unsigned int current_id) { 1641 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1642 1643 TypeFromUser user_type( 1644 llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetTypeFromUser()); 1645 1646 TypeFromParser parser_type(GuardedCopyType(user_type)); 1647 1648 if (!parser_type.GetOpaqueQualType()) { 1649 LLDB_LOGF(log, " CEDM::FEVD[%u] Couldn't import type for pvar %s", 1650 current_id, pvar_sp->GetName().GetCString()); 1651 return; 1652 } 1653 1654 NamedDecl *var_decl = 1655 context.AddVarDecl(parser_type.GetLValueReferenceType()); 1656 1657 llvm::cast<ClangExpressionVariable>(pvar_sp.get()) 1658 ->EnableParserVars(GetParserID()); 1659 ClangExpressionVariable::ParserVars *parser_vars = 1660 llvm::cast<ClangExpressionVariable>(pvar_sp.get()) 1661 ->GetParserVars(GetParserID()); 1662 parser_vars->m_parser_type = parser_type; 1663 parser_vars->m_named_decl = var_decl; 1664 parser_vars->m_llvm_value = nullptr; 1665 parser_vars->m_lldb_value.Clear(); 1666 1667 LLDB_LOG(log, " CEDM::FEVD[{0}] Added pvar {1}, returned\n{2}", current_id, 1668 pvar_sp->GetName(), ClangUtil::DumpDecl(var_decl)); 1669 } 1670 1671 void ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context, 1672 const Symbol &symbol, 1673 unsigned int current_id) { 1674 assert(m_parser_vars.get()); 1675 1676 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1677 1678 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 1679 1680 if (target == nullptr) 1681 return; 1682 1683 ClangASTContext *scratch_ast_context = ClangASTContext::GetScratch(*target); 1684 if (!scratch_ast_context) 1685 return; 1686 1687 TypeFromUser user_type(scratch_ast_context->GetBasicType(eBasicTypeVoid) 1688 .GetPointerType() 1689 .GetLValueReferenceType()); 1690 TypeFromParser parser_type(m_clang_ast_context->GetBasicType(eBasicTypeVoid) 1691 .GetPointerType() 1692 .GetLValueReferenceType()); 1693 NamedDecl *var_decl = context.AddVarDecl(parser_type); 1694 1695 std::string decl_name(context.m_decl_name.getAsString()); 1696 ConstString entity_name(decl_name.c_str()); 1697 ClangExpressionVariable *entity(new ClangExpressionVariable( 1698 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), entity_name, 1699 user_type, m_parser_vars->m_target_info.byte_order, 1700 m_parser_vars->m_target_info.address_byte_size)); 1701 m_found_entities.AddNewlyConstructedVariable(entity); 1702 1703 entity->EnableParserVars(GetParserID()); 1704 ClangExpressionVariable::ParserVars *parser_vars = 1705 entity->GetParserVars(GetParserID()); 1706 1707 const Address symbol_address = symbol.GetAddress(); 1708 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target); 1709 1710 // parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, 1711 // user_type.GetOpaqueQualType()); 1712 parser_vars->m_lldb_value.SetCompilerType(user_type); 1713 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr; 1714 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress); 1715 1716 parser_vars->m_parser_type = parser_type; 1717 parser_vars->m_named_decl = var_decl; 1718 parser_vars->m_llvm_value = nullptr; 1719 parser_vars->m_lldb_sym = &symbol; 1720 1721 LLDB_LOG(log, " CEDM::FEVD[{0}] Found variable {1}, returned\n{2}", 1722 current_id, decl_name, ClangUtil::DumpDecl(var_decl)); 1723 } 1724 1725 void ClangExpressionDeclMap::AddOneRegister(NameSearchContext &context, 1726 const RegisterInfo *reg_info, 1727 unsigned int current_id) { 1728 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1729 1730 CompilerType clang_type = 1731 m_clang_ast_context->GetBuiltinTypeForEncodingAndBitSize( 1732 reg_info->encoding, reg_info->byte_size * 8); 1733 1734 if (!clang_type) { 1735 LLDB_LOGF(log, " Tried to add a type for %s, but couldn't get one", 1736 context.m_decl_name.getAsString().c_str()); 1737 return; 1738 } 1739 1740 TypeFromParser parser_clang_type(clang_type); 1741 1742 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type); 1743 1744 ClangExpressionVariable *entity(new ClangExpressionVariable( 1745 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), 1746 m_parser_vars->m_target_info.byte_order, 1747 m_parser_vars->m_target_info.address_byte_size)); 1748 m_found_entities.AddNewlyConstructedVariable(entity); 1749 1750 std::string decl_name(context.m_decl_name.getAsString()); 1751 entity->SetName(ConstString(decl_name.c_str())); 1752 entity->SetRegisterInfo(reg_info); 1753 entity->EnableParserVars(GetParserID()); 1754 ClangExpressionVariable::ParserVars *parser_vars = 1755 entity->GetParserVars(GetParserID()); 1756 parser_vars->m_parser_type = parser_clang_type; 1757 parser_vars->m_named_decl = var_decl; 1758 parser_vars->m_llvm_value = nullptr; 1759 parser_vars->m_lldb_value.Clear(); 1760 entity->m_flags |= ClangExpressionVariable::EVBareRegister; 1761 1762 LLDB_LOG(log, " CEDM::FEVD[{0}] Added register {1}, returned\n{2}", 1763 current_id, context.m_decl_name.getAsString(), 1764 ClangUtil::DumpDecl(var_decl)); 1765 } 1766 1767 void ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context, 1768 Function *function, Symbol *symbol, 1769 unsigned int current_id) { 1770 assert(m_parser_vars.get()); 1771 1772 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1773 1774 NamedDecl *function_decl = nullptr; 1775 Address fun_address; 1776 CompilerType function_clang_type; 1777 1778 bool is_indirect_function = false; 1779 1780 if (function) { 1781 Type *function_type = function->GetType(); 1782 1783 const auto lang = function->GetCompileUnit()->GetLanguage(); 1784 const auto name = function->GetMangled().GetMangledName().AsCString(); 1785 const bool extern_c = (Language::LanguageIsC(lang) && 1786 !CPlusPlusLanguage::IsCPPMangledName(name)) || 1787 (Language::LanguageIsObjC(lang) && 1788 !Language::LanguageIsCPlusPlus(lang)); 1789 1790 if (!extern_c) { 1791 TypeSystem *type_system = function->GetDeclContext().GetTypeSystem(); 1792 if (llvm::isa<ClangASTContext>(type_system)) { 1793 clang::DeclContext *src_decl_context = 1794 (clang::DeclContext *)function->GetDeclContext() 1795 .GetOpaqueDeclContext(); 1796 clang::FunctionDecl *src_function_decl = 1797 llvm::dyn_cast_or_null<clang::FunctionDecl>(src_decl_context); 1798 if (src_function_decl && 1799 src_function_decl->getTemplateSpecializationInfo()) { 1800 clang::FunctionTemplateDecl *function_template = 1801 src_function_decl->getTemplateSpecializationInfo()->getTemplate(); 1802 clang::FunctionTemplateDecl *copied_function_template = 1803 llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>( 1804 CopyDecl(function_template)); 1805 if (copied_function_template) { 1806 if (log) { 1807 StreamString ss; 1808 1809 function->DumpSymbolContext(&ss); 1810 1811 LLDB_LOG(log, 1812 " CEDM::FEVD[{0}] Imported decl for function template" 1813 " {1} (description {2}), returned\n{3}", 1814 current_id, copied_function_template->getNameAsString(), 1815 ss.GetData(), 1816 ClangUtil::DumpDecl(copied_function_template)); 1817 } 1818 1819 context.AddNamedDecl(copied_function_template); 1820 } 1821 } else if (src_function_decl) { 1822 if (clang::FunctionDecl *copied_function_decl = 1823 llvm::dyn_cast_or_null<clang::FunctionDecl>( 1824 CopyDecl(src_function_decl))) { 1825 if (log) { 1826 StreamString ss; 1827 1828 function->DumpSymbolContext(&ss); 1829 1830 LLDB_LOG(log, 1831 " CEDM::FEVD[{0}]] Imported decl for function {1} " 1832 "(description {2}), returned\n{3}", 1833 current_id, copied_function_decl->getNameAsString(), 1834 ss.GetData(), ClangUtil::DumpDecl(copied_function_decl)); 1835 } 1836 1837 context.AddNamedDecl(copied_function_decl); 1838 return; 1839 } else { 1840 if (log) { 1841 LLDB_LOGF(log, " Failed to import the function decl for '%s'", 1842 src_function_decl->getName().str().c_str()); 1843 } 1844 } 1845 } 1846 } 1847 } 1848 1849 if (!function_type) { 1850 if (log) 1851 log->PutCString(" Skipped a function because it has no type"); 1852 return; 1853 } 1854 1855 function_clang_type = function_type->GetFullCompilerType(); 1856 1857 if (!function_clang_type) { 1858 if (log) 1859 log->PutCString(" Skipped a function because it has no Clang type"); 1860 return; 1861 } 1862 1863 fun_address = function->GetAddressRange().GetBaseAddress(); 1864 1865 CompilerType copied_function_type = GuardedCopyType(function_clang_type); 1866 if (copied_function_type) { 1867 function_decl = context.AddFunDecl(copied_function_type, extern_c); 1868 1869 if (!function_decl) { 1870 if (log) { 1871 LLDB_LOGF( 1872 log, 1873 " Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}", 1874 function_type->GetName().GetCString(), function_type->GetID()); 1875 } 1876 1877 return; 1878 } 1879 } else { 1880 // We failed to copy the type we found 1881 if (log) { 1882 LLDB_LOGF(log, 1883 " Failed to import the function type '%s' {0x%8.8" PRIx64 1884 "} into the expression parser AST contenxt", 1885 function_type->GetName().GetCString(), 1886 function_type->GetID()); 1887 } 1888 1889 return; 1890 } 1891 } else if (symbol) { 1892 fun_address = symbol->GetAddress(); 1893 function_decl = context.AddGenericFunDecl(); 1894 is_indirect_function = symbol->IsIndirect(); 1895 } else { 1896 if (log) 1897 log->PutCString(" AddOneFunction called with no function and no symbol"); 1898 return; 1899 } 1900 1901 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 1902 1903 lldb::addr_t load_addr = 1904 fun_address.GetCallableLoadAddress(target, is_indirect_function); 1905 1906 ClangExpressionVariable *entity(new ClangExpressionVariable( 1907 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), 1908 m_parser_vars->m_target_info.byte_order, 1909 m_parser_vars->m_target_info.address_byte_size)); 1910 m_found_entities.AddNewlyConstructedVariable(entity); 1911 1912 std::string decl_name(context.m_decl_name.getAsString()); 1913 entity->SetName(ConstString(decl_name.c_str())); 1914 entity->SetCompilerType(function_clang_type); 1915 entity->EnableParserVars(GetParserID()); 1916 1917 ClangExpressionVariable::ParserVars *parser_vars = 1918 entity->GetParserVars(GetParserID()); 1919 1920 if (load_addr != LLDB_INVALID_ADDRESS) { 1921 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress); 1922 parser_vars->m_lldb_value.GetScalar() = load_addr; 1923 } else { 1924 // We have to try finding a file address. 1925 1926 lldb::addr_t file_addr = fun_address.GetFileAddress(); 1927 1928 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress); 1929 parser_vars->m_lldb_value.GetScalar() = file_addr; 1930 } 1931 1932 parser_vars->m_named_decl = function_decl; 1933 parser_vars->m_llvm_value = nullptr; 1934 1935 if (log) { 1936 StreamString ss; 1937 1938 fun_address.Dump(&ss, 1939 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), 1940 Address::DumpStyleResolvedDescription); 1941 1942 LLDB_LOG(log, 1943 " CEDM::FEVD[{0}] Found {1} function {2} (description {3}), " 1944 "returned\n{4}", 1945 current_id, (function ? "specific" : "generic"), decl_name, 1946 ss.GetData(), ClangUtil::DumpDecl(function_decl)); 1947 } 1948 } 1949 1950 void ClangExpressionDeclMap::AddThisType(NameSearchContext &context, 1951 const TypeFromUser &ut, 1952 unsigned int current_id) { 1953 CompilerType copied_clang_type = GuardedCopyType(ut); 1954 1955 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1956 1957 if (!copied_clang_type) { 1958 if (log) 1959 LLDB_LOGF( 1960 log, 1961 "ClangExpressionDeclMap::AddThisType - Couldn't import the type"); 1962 1963 return; 1964 } 1965 1966 if (copied_clang_type.IsAggregateType() && 1967 copied_clang_type.GetCompleteType()) { 1968 CompilerType void_clang_type = 1969 m_clang_ast_context->GetBasicType(eBasicTypeVoid); 1970 CompilerType void_ptr_clang_type = void_clang_type.GetPointerType(); 1971 1972 CompilerType method_type = ClangASTContext::CreateFunctionType( 1973 m_ast_context, void_clang_type, &void_ptr_clang_type, 1, false, 0); 1974 1975 const bool is_virtual = false; 1976 const bool is_static = false; 1977 const bool is_inline = false; 1978 const bool is_explicit = false; 1979 const bool is_attr_used = true; 1980 const bool is_artificial = false; 1981 1982 CXXMethodDecl *method_decl = m_clang_ast_context->AddMethodToCXXRecordType( 1983 copied_clang_type.GetOpaqueQualType(), "$__lldb_expr", nullptr, 1984 method_type, lldb::eAccessPublic, is_virtual, is_static, is_inline, 1985 is_explicit, is_attr_used, is_artificial); 1986 1987 LLDB_LOG(log, 1988 " CEDM::AddThisType Added function $__lldb_expr " 1989 "(description {0}) for this type\n{1}", 1990 ClangUtil::ToString(copied_clang_type), 1991 ClangUtil::DumpDecl(method_decl)); 1992 } 1993 1994 if (!copied_clang_type.IsValid()) 1995 return; 1996 1997 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo( 1998 QualType::getFromOpaquePtr(copied_clang_type.GetOpaqueQualType())); 1999 2000 if (!type_source_info) 2001 return; 2002 2003 // Construct a typedef type because if "*this" is a templated type we can't 2004 // just return ClassTemplateSpecializationDecls in response to name queries. 2005 // Using a typedef makes this much more robust. 2006 2007 TypedefDecl *typedef_decl = TypedefDecl::Create( 2008 *m_ast_context, m_ast_context->getTranslationUnitDecl(), SourceLocation(), 2009 SourceLocation(), context.m_decl_name.getAsIdentifierInfo(), 2010 type_source_info); 2011 2012 if (!typedef_decl) 2013 return; 2014 2015 context.AddNamedDecl(typedef_decl); 2016 2017 return; 2018 } 2019 2020 void ClangExpressionDeclMap::AddOneType(NameSearchContext &context, 2021 const TypeFromUser &ut, 2022 unsigned int current_id) { 2023 CompilerType copied_clang_type = GuardedCopyType(ut); 2024 2025 if (!copied_clang_type) { 2026 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 2027 2028 if (log) 2029 LLDB_LOGF( 2030 log, "ClangExpressionDeclMap::AddOneType - Couldn't import the type"); 2031 2032 return; 2033 } 2034 2035 context.AddTypeDecl(copied_clang_type); 2036 } 2037