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