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