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