1254721Semaste//===-- ClangExpressionDeclMap.cpp -----------------------------*- C++ -*-===// 2254721Semaste// 3254721Semaste// The LLVM Compiler Infrastructure 4254721Semaste// 5254721Semaste// This file is distributed under the University of Illinois Open Source 6254721Semaste// License. See LICENSE.TXT for details. 7254721Semaste// 8254721Semaste//===----------------------------------------------------------------------===// 9254721Semaste 10254721Semaste#include "lldb/Expression/ClangExpressionDeclMap.h" 11254721Semaste 12254721Semaste// C Includes 13254721Semaste// C++ Includes 14254721Semaste// Other libraries and framework includes 15254721Semaste// Project includes 16254721Semaste#include "clang/AST/ASTContext.h" 17254721Semaste#include "clang/AST/DeclarationName.h" 18254721Semaste#include "clang/AST/Decl.h" 19254721Semaste#include "lldb/lldb-private.h" 20254721Semaste#include "lldb/Core/Address.h" 21254721Semaste#include "lldb/Core/Error.h" 22254721Semaste#include "lldb/Core/Log.h" 23254721Semaste#include "lldb/Core/Module.h" 24254721Semaste#include "lldb/Core/RegisterValue.h" 25254721Semaste#include "lldb/Core/ValueObjectConstResult.h" 26254721Semaste#include "lldb/Core/ValueObjectVariable.h" 27254721Semaste#include "lldb/Expression/ASTDumper.h" 28254721Semaste#include "lldb/Expression/ClangASTSource.h" 29254721Semaste#include "lldb/Expression/ClangPersistentVariables.h" 30254721Semaste#include "lldb/Expression/Materializer.h" 31254721Semaste#include "lldb/Host/Endian.h" 32254721Semaste#include "lldb/Symbol/ClangASTContext.h" 33254721Semaste#include "lldb/Symbol/ClangNamespaceDecl.h" 34254721Semaste#include "lldb/Symbol/CompileUnit.h" 35254721Semaste#include "lldb/Symbol/Function.h" 36254721Semaste#include "lldb/Symbol/ObjectFile.h" 37254721Semaste#include "lldb/Symbol/SymbolContext.h" 38254721Semaste#include "lldb/Symbol/SymbolVendor.h" 39254721Semaste#include "lldb/Symbol/Type.h" 40254721Semaste#include "lldb/Symbol/TypeList.h" 41254721Semaste#include "lldb/Symbol/Variable.h" 42254721Semaste#include "lldb/Symbol/VariableList.h" 43254721Semaste#include "lldb/Target/ExecutionContext.h" 44254721Semaste#include "lldb/Target/ObjCLanguageRuntime.h" 45254721Semaste#include "lldb/Target/Process.h" 46254721Semaste#include "lldb/Target/RegisterContext.h" 47254721Semaste#include "lldb/Target/StackFrame.h" 48254721Semaste#include "lldb/Target/Target.h" 49254721Semaste#include "lldb/Target/Thread.h" 50254721Semaste 51254721Semasteusing namespace lldb; 52254721Semasteusing namespace lldb_private; 53254721Semasteusing namespace clang; 54254721Semaste 55254721SemasteClangExpressionDeclMap::ClangExpressionDeclMap (bool keep_result_in_memory, ExecutionContext &exe_ctx) : 56254721Semaste ClangASTSource (exe_ctx.GetTargetSP()), 57254721Semaste m_found_entities (), 58254721Semaste m_struct_members (), 59254721Semaste m_keep_result_in_memory (keep_result_in_memory), 60254721Semaste m_parser_vars (), 61254721Semaste m_struct_vars () 62254721Semaste{ 63254721Semaste EnableStructVars(); 64254721Semaste} 65254721Semaste 66254721SemasteClangExpressionDeclMap::~ClangExpressionDeclMap() 67254721Semaste{ 68254721Semaste // Note: The model is now that the parser's AST context and all associated 69254721Semaste // data does not vanish until the expression has been executed. This means 70254721Semaste // that valuable lookup data (like namespaces) doesn't vanish, but 71254721Semaste 72254721Semaste DidParse(); 73254721Semaste DisableStructVars(); 74254721Semaste} 75254721Semaste 76254721Semastebool 77254721SemasteClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx, 78254721Semaste Materializer *materializer) 79254721Semaste{ 80254721Semaste ClangASTMetrics::ClearLocalCounters(); 81254721Semaste 82254721Semaste EnableParserVars(); 83254721Semaste m_parser_vars->m_exe_ctx = exe_ctx; 84254721Semaste 85254721Semaste Target *target = exe_ctx.GetTargetPtr(); 86254721Semaste if (exe_ctx.GetFramePtr()) 87254721Semaste m_parser_vars->m_sym_ctx = exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything); 88254721Semaste else if (exe_ctx.GetThreadPtr() && exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)) 89254721Semaste m_parser_vars->m_sym_ctx = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext(lldb::eSymbolContextEverything); 90254721Semaste else if (exe_ctx.GetProcessPtr()) 91254721Semaste { 92254721Semaste m_parser_vars->m_sym_ctx.Clear(true); 93254721Semaste m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP(); 94254721Semaste } 95254721Semaste else if (target) 96254721Semaste { 97254721Semaste m_parser_vars->m_sym_ctx.Clear(true); 98254721Semaste m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP(); 99254721Semaste } 100254721Semaste 101254721Semaste if (target) 102254721Semaste { 103254721Semaste m_parser_vars->m_persistent_vars = &target->GetPersistentVariables(); 104254721Semaste 105254721Semaste if (!target->GetScratchClangASTContext()) 106254721Semaste return false; 107254721Semaste } 108254721Semaste 109254721Semaste m_parser_vars->m_target_info = GetTargetInfo(); 110254721Semaste m_parser_vars->m_materializer = materializer; 111254721Semaste 112254721Semaste return true; 113254721Semaste} 114254721Semaste 115254721Semastevoid 116254721SemasteClangExpressionDeclMap::DidParse() 117254721Semaste{ 118254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 119254721Semaste 120254721Semaste if (log) 121254721Semaste ClangASTMetrics::DumpCounters(log); 122254721Semaste 123254721Semaste if (m_parser_vars.get()) 124254721Semaste { 125254721Semaste for (size_t entity_index = 0, num_entities = m_found_entities.GetSize(); 126254721Semaste entity_index < num_entities; 127254721Semaste ++entity_index) 128254721Semaste { 129254721Semaste ClangExpressionVariableSP var_sp(m_found_entities.GetVariableAtIndex(entity_index)); 130254721Semaste if (var_sp) 131254721Semaste var_sp->DisableParserVars(GetParserID()); 132254721Semaste } 133254721Semaste 134254721Semaste for (size_t pvar_index = 0, num_pvars = m_parser_vars->m_persistent_vars->GetSize(); 135254721Semaste pvar_index < num_pvars; 136254721Semaste ++pvar_index) 137254721Semaste { 138254721Semaste ClangExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index)); 139254721Semaste if (pvar_sp) 140254721Semaste pvar_sp->DisableParserVars(GetParserID()); 141254721Semaste } 142254721Semaste 143254721Semaste DisableParserVars(); 144254721Semaste } 145254721Semaste} 146254721Semaste 147254721Semaste// Interface for IRForTarget 148254721Semaste 149254721SemasteClangExpressionDeclMap::TargetInfo 150254721SemasteClangExpressionDeclMap::GetTargetInfo() 151254721Semaste{ 152254721Semaste assert (m_parser_vars.get()); 153254721Semaste 154254721Semaste TargetInfo ret; 155254721Semaste 156254721Semaste ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx; 157254721Semaste 158254721Semaste Process *process = exe_ctx.GetProcessPtr(); 159254721Semaste if (process) 160254721Semaste { 161254721Semaste ret.byte_order = process->GetByteOrder(); 162254721Semaste ret.address_byte_size = process->GetAddressByteSize(); 163254721Semaste } 164254721Semaste else 165254721Semaste { 166254721Semaste Target *target = exe_ctx.GetTargetPtr(); 167254721Semaste if (target) 168254721Semaste { 169254721Semaste ret.byte_order = target->GetArchitecture().GetByteOrder(); 170254721Semaste ret.address_byte_size = target->GetArchitecture().GetAddressByteSize(); 171254721Semaste } 172254721Semaste } 173254721Semaste 174254721Semaste return ret; 175254721Semaste} 176254721Semaste 177254721Semastebool 178254721SemasteClangExpressionDeclMap::AddPersistentVariable 179254721Semaste( 180254721Semaste const NamedDecl *decl, 181254721Semaste const ConstString &name, 182254721Semaste TypeFromParser parser_type, 183254721Semaste bool is_result, 184254721Semaste bool is_lvalue 185254721Semaste) 186254721Semaste{ 187254721Semaste assert (m_parser_vars.get()); 188254721Semaste 189254721Semaste if (m_parser_vars->m_materializer && is_result) 190254721Semaste { 191254721Semaste Error err; 192254721Semaste 193254721Semaste ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx; 194254721Semaste Target *target = exe_ctx.GetTargetPtr(); 195254721Semaste if (target == NULL) 196254721Semaste return false; 197254721Semaste 198254721Semaste ASTContext *context(target->GetScratchClangASTContext()->getASTContext()); 199254721Semaste 200254721Semaste TypeFromUser user_type(m_ast_importer->DeportType(context, 201254721Semaste parser_type.GetASTContext(), 202254721Semaste parser_type.GetOpaqueQualType()), 203254721Semaste context); 204254721Semaste 205254721Semaste uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(user_type, is_lvalue, m_keep_result_in_memory, err); 206254721Semaste 207254721Semaste ClangExpressionVariableSP var_sp = m_found_entities.CreateVariable(exe_ctx.GetBestExecutionContextScope(), 208254721Semaste name, 209254721Semaste user_type, 210254721Semaste m_parser_vars->m_target_info.byte_order, 211254721Semaste m_parser_vars->m_target_info.address_byte_size); 212254721Semaste 213254721Semaste if (!var_sp) 214254721Semaste return false; 215254721Semaste 216254721Semaste var_sp->EnableParserVars(GetParserID()); 217254721Semaste 218254721Semaste ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID()); 219254721Semaste 220254721Semaste parser_vars->m_named_decl = decl; 221254721Semaste parser_vars->m_parser_type = parser_type; 222254721Semaste 223254721Semaste var_sp->EnableJITVars(GetParserID()); 224254721Semaste 225254721Semaste ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID()); 226254721Semaste 227254721Semaste jit_vars->m_offset = offset; 228254721Semaste 229254721Semaste return true; 230254721Semaste } 231254721Semaste 232254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 233254721Semaste ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx; 234254721Semaste Target *target = exe_ctx.GetTargetPtr(); 235254721Semaste if (target == NULL) 236254721Semaste return false; 237254721Semaste 238254721Semaste ASTContext *context(target->GetScratchClangASTContext()->getASTContext()); 239254721Semaste 240254721Semaste TypeFromUser user_type(m_ast_importer->DeportType(context, 241254721Semaste parser_type.GetASTContext(), 242254721Semaste parser_type.GetOpaqueQualType()), 243254721Semaste context); 244254721Semaste 245254721Semaste if (!user_type.GetOpaqueQualType()) 246254721Semaste { 247254721Semaste if (log) 248254721Semaste log->Printf("Persistent variable's type wasn't copied successfully"); 249254721Semaste return false; 250254721Semaste } 251254721Semaste 252254721Semaste if (!m_parser_vars->m_target_info.IsValid()) 253254721Semaste return false; 254254721Semaste 255254721Semaste ClangExpressionVariableSP var_sp = m_parser_vars->m_persistent_vars->CreatePersistentVariable (exe_ctx.GetBestExecutionContextScope (), 256254721Semaste name, 257254721Semaste user_type, 258254721Semaste m_parser_vars->m_target_info.byte_order, 259254721Semaste m_parser_vars->m_target_info.address_byte_size); 260254721Semaste 261254721Semaste if (!var_sp) 262254721Semaste return false; 263254721Semaste 264254721Semaste var_sp->m_frozen_sp->SetHasCompleteType(); 265254721Semaste 266254721Semaste if (is_result) 267254721Semaste var_sp->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry; 268254721Semaste else 269254721Semaste var_sp->m_flags |= ClangExpressionVariable::EVKeepInTarget; // explicitly-declared persistent variables should persist 270254721Semaste 271254721Semaste if (is_lvalue) 272254721Semaste { 273254721Semaste var_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference; 274254721Semaste } 275254721Semaste else 276254721Semaste { 277254721Semaste var_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated; 278254721Semaste var_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation; 279254721Semaste } 280254721Semaste 281254721Semaste if (m_keep_result_in_memory) 282254721Semaste { 283254721Semaste var_sp->m_flags |= ClangExpressionVariable::EVKeepInTarget; 284254721Semaste } 285254721Semaste 286254721Semaste if (log) 287254721Semaste log->Printf("Created persistent variable with flags 0x%hx", var_sp->m_flags); 288254721Semaste 289254721Semaste var_sp->EnableParserVars(GetParserID()); 290254721Semaste 291254721Semaste ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID()); 292254721Semaste 293254721Semaste parser_vars->m_named_decl = decl; 294254721Semaste parser_vars->m_parser_type = parser_type; 295254721Semaste 296254721Semaste return true; 297254721Semaste} 298254721Semaste 299254721Semastebool 300254721SemasteClangExpressionDeclMap::AddValueToStruct 301254721Semaste( 302254721Semaste const NamedDecl *decl, 303254721Semaste const ConstString &name, 304254721Semaste llvm::Value *value, 305254721Semaste size_t size, 306254721Semaste off_t alignment 307254721Semaste) 308254721Semaste{ 309254721Semaste assert (m_struct_vars.get()); 310254721Semaste assert (m_parser_vars.get()); 311254721Semaste 312254721Semaste bool is_persistent_variable = false; 313254721Semaste 314254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 315254721Semaste 316254721Semaste m_struct_vars->m_struct_laid_out = false; 317254721Semaste 318254721Semaste if (m_struct_members.GetVariable(decl, GetParserID())) 319254721Semaste return true; 320254721Semaste 321254721Semaste ClangExpressionVariableSP var_sp (m_found_entities.GetVariable(decl, GetParserID())); 322254721Semaste 323254721Semaste if (!var_sp) 324254721Semaste { 325254721Semaste var_sp = m_parser_vars->m_persistent_vars->GetVariable(decl, GetParserID()); 326254721Semaste is_persistent_variable = true; 327254721Semaste } 328254721Semaste 329254721Semaste if (!var_sp) 330254721Semaste return false; 331254721Semaste 332254721Semaste if (log) 333254721Semaste log->Printf("Adding value for (NamedDecl*)%p [%s - %s] to the structure", 334254721Semaste decl, 335254721Semaste name.GetCString(), 336254721Semaste var_sp->GetName().GetCString()); 337254721Semaste 338254721Semaste // We know entity->m_parser_vars is valid because we used a parser variable 339254721Semaste // to find it 340254721Semaste 341254721Semaste ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID()); 342254721Semaste 343254721Semaste parser_vars->m_llvm_value = value; 344254721Semaste 345254721Semaste if (ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID())) 346254721Semaste { 347254721Semaste // We already laid this out; do not touch 348254721Semaste 349254721Semaste if (log) 350254721Semaste log->Printf("Already placed at 0x%llx", (unsigned long long)jit_vars->m_offset); 351254721Semaste } 352254721Semaste 353254721Semaste var_sp->EnableJITVars(GetParserID()); 354254721Semaste 355254721Semaste ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID()); 356254721Semaste 357254721Semaste jit_vars->m_alignment = alignment; 358254721Semaste jit_vars->m_size = size; 359254721Semaste 360254721Semaste m_struct_members.AddVariable(var_sp); 361254721Semaste 362254721Semaste if (m_parser_vars->m_materializer) 363254721Semaste { 364254721Semaste uint32_t offset = 0; 365254721Semaste 366254721Semaste Error err; 367254721Semaste 368254721Semaste if (is_persistent_variable) 369254721Semaste { 370254721Semaste offset = m_parser_vars->m_materializer->AddPersistentVariable(var_sp, err); 371254721Semaste } 372254721Semaste else 373254721Semaste { 374254721Semaste if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym) 375254721Semaste offset = m_parser_vars->m_materializer->AddSymbol(*sym, err); 376254721Semaste else if (const RegisterInfo *reg_info = var_sp->GetRegisterInfo()) 377254721Semaste offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err); 378254721Semaste else if (parser_vars->m_lldb_var) 379254721Semaste offset = m_parser_vars->m_materializer->AddVariable(parser_vars->m_lldb_var, err); 380254721Semaste } 381254721Semaste 382254721Semaste if (!err.Success()) 383254721Semaste return false; 384254721Semaste 385254721Semaste if (log) 386254721Semaste log->Printf("Placed at 0x%llx", (unsigned long long)offset); 387254721Semaste 388254721Semaste jit_vars->m_offset = offset; // TODO DoStructLayout() should not change this. 389254721Semaste } 390254721Semaste 391254721Semaste return true; 392254721Semaste} 393254721Semaste 394254721Semastebool 395254721SemasteClangExpressionDeclMap::DoStructLayout () 396254721Semaste{ 397254721Semaste assert (m_struct_vars.get()); 398254721Semaste 399254721Semaste if (m_struct_vars->m_struct_laid_out) 400254721Semaste return true; 401254721Semaste 402254721Semaste if (!m_parser_vars->m_materializer) 403254721Semaste return false; 404254721Semaste 405254721Semaste m_struct_vars->m_struct_alignment = m_parser_vars->m_materializer->GetStructAlignment(); 406254721Semaste m_struct_vars->m_struct_size = m_parser_vars->m_materializer->GetStructByteSize(); 407254721Semaste m_struct_vars->m_struct_laid_out = true; 408254721Semaste return true; 409254721Semaste} 410254721Semaste 411254721Semastebool ClangExpressionDeclMap::GetStructInfo 412254721Semaste( 413254721Semaste uint32_t &num_elements, 414254721Semaste size_t &size, 415254721Semaste off_t &alignment 416254721Semaste) 417254721Semaste{ 418254721Semaste assert (m_struct_vars.get()); 419254721Semaste 420254721Semaste if (!m_struct_vars->m_struct_laid_out) 421254721Semaste return false; 422254721Semaste 423254721Semaste num_elements = m_struct_members.GetSize(); 424254721Semaste size = m_struct_vars->m_struct_size; 425254721Semaste alignment = m_struct_vars->m_struct_alignment; 426254721Semaste 427254721Semaste return true; 428254721Semaste} 429254721Semaste 430254721Semastebool 431254721SemasteClangExpressionDeclMap::GetStructElement 432254721Semaste( 433254721Semaste const NamedDecl *&decl, 434254721Semaste llvm::Value *&value, 435254721Semaste off_t &offset, 436254721Semaste ConstString &name, 437254721Semaste uint32_t index 438254721Semaste) 439254721Semaste{ 440254721Semaste assert (m_struct_vars.get()); 441254721Semaste 442254721Semaste if (!m_struct_vars->m_struct_laid_out) 443254721Semaste return false; 444254721Semaste 445254721Semaste if (index >= m_struct_members.GetSize()) 446254721Semaste return false; 447254721Semaste 448254721Semaste ClangExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index)); 449254721Semaste 450254721Semaste if (!member_sp) 451254721Semaste return false; 452254721Semaste 453254721Semaste ClangExpressionVariable::ParserVars *parser_vars = member_sp->GetParserVars(GetParserID()); 454254721Semaste ClangExpressionVariable::JITVars *jit_vars = member_sp->GetJITVars(GetParserID()); 455254721Semaste 456254721Semaste if (!parser_vars || 457254721Semaste !jit_vars || 458254721Semaste !member_sp->GetValueObject()) 459254721Semaste return false; 460254721Semaste 461254721Semaste decl = parser_vars->m_named_decl; 462254721Semaste value = parser_vars->m_llvm_value; 463254721Semaste offset = jit_vars->m_offset; 464254721Semaste name = member_sp->GetName(); 465254721Semaste 466254721Semaste return true; 467254721Semaste} 468254721Semaste 469254721Semastebool 470254721SemasteClangExpressionDeclMap::GetFunctionInfo 471254721Semaste( 472254721Semaste const NamedDecl *decl, 473254721Semaste uint64_t &ptr 474254721Semaste) 475254721Semaste{ 476254721Semaste ClangExpressionVariableSP entity_sp(m_found_entities.GetVariable(decl, GetParserID())); 477254721Semaste 478254721Semaste if (!entity_sp) 479254721Semaste return false; 480254721Semaste 481254721Semaste // We know m_parser_vars is valid since we searched for the variable by 482254721Semaste // its NamedDecl 483254721Semaste 484254721Semaste ClangExpressionVariable::ParserVars *parser_vars = entity_sp->GetParserVars(GetParserID()); 485254721Semaste 486254721Semaste ptr = parser_vars->m_lldb_value.GetScalar().ULongLong(); 487254721Semaste 488254721Semaste return true; 489254721Semaste} 490254721Semaste 491254721Semastestatic void 492254721SemasteFindCodeSymbolInContext 493254721Semaste( 494254721Semaste const ConstString &name, 495254721Semaste SymbolContext &sym_ctx, 496254721Semaste SymbolContextList &sc_list 497254721Semaste) 498254721Semaste{ 499254721Semaste SymbolContextList temp_sc_list; 500254721Semaste if (sym_ctx.module_sp) 501254721Semaste sym_ctx.module_sp->FindSymbolsWithNameAndType(name, eSymbolTypeAny, temp_sc_list); 502254721Semaste 503254721Semaste if (!sc_list.GetSize() && sym_ctx.target_sp) 504254721Semaste sym_ctx.target_sp->GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, temp_sc_list); 505254721Semaste 506254721Semaste unsigned temp_sc_list_size = temp_sc_list.GetSize(); 507254721Semaste for (unsigned i = 0; i < temp_sc_list_size; i++) 508254721Semaste { 509254721Semaste SymbolContext sym_ctx; 510254721Semaste temp_sc_list.GetContextAtIndex(i, sym_ctx); 511254721Semaste if (sym_ctx.symbol) 512254721Semaste { 513254721Semaste switch (sym_ctx.symbol->GetType()) 514254721Semaste { 515254721Semaste case eSymbolTypeCode: 516254721Semaste case eSymbolTypeResolver: 517254721Semaste sc_list.Append(sym_ctx); 518254721Semaste break; 519254721Semaste 520254721Semaste default: 521254721Semaste break; 522254721Semaste } 523254721Semaste } 524254721Semaste } 525254721Semaste} 526254721Semaste 527254721Semastebool 528254721SemasteClangExpressionDeclMap::GetFunctionAddress 529254721Semaste( 530254721Semaste const ConstString &name, 531254721Semaste uint64_t &func_addr 532254721Semaste) 533254721Semaste{ 534254721Semaste assert (m_parser_vars.get()); 535254721Semaste 536254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 537254721Semaste ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx; 538254721Semaste Target *target = exe_ctx.GetTargetPtr(); 539254721Semaste // Back out in all cases where we're not fully initialized 540254721Semaste if (target == NULL) 541254721Semaste return false; 542254721Semaste if (!m_parser_vars->m_sym_ctx.target_sp) 543254721Semaste return false; 544254721Semaste 545254721Semaste SymbolContextList sc_list; 546254721Semaste 547254721Semaste FindCodeSymbolInContext(name, m_parser_vars->m_sym_ctx, sc_list); 548254721Semaste 549254721Semaste if (!sc_list.GetSize()) 550254721Semaste { 551254721Semaste // We occasionally get debug information in which a const function is reported 552254721Semaste // as non-const, so the mangled name is wrong. This is a hack to compensate. 553254721Semaste 554254721Semaste if (!strncmp(name.GetCString(), "_ZN", 3) && 555254721Semaste strncmp(name.GetCString(), "_ZNK", 4)) 556254721Semaste { 557254721Semaste std::string fixed_scratch("_ZNK"); 558254721Semaste fixed_scratch.append(name.GetCString() + 3); 559254721Semaste ConstString fixed_name(fixed_scratch.c_str()); 560254721Semaste 561254721Semaste if (log) 562254721Semaste log->Printf("Failed to find symbols given non-const name %s; trying %s", name.GetCString(), fixed_name.GetCString()); 563254721Semaste 564254721Semaste FindCodeSymbolInContext(fixed_name, m_parser_vars->m_sym_ctx, sc_list); 565254721Semaste } 566254721Semaste } 567254721Semaste 568254721Semaste if (!sc_list.GetSize()) 569254721Semaste return false; 570254721Semaste 571254721Semaste SymbolContext sym_ctx; 572254721Semaste sc_list.GetContextAtIndex(0, sym_ctx); 573254721Semaste 574254721Semaste const Address *func_so_addr = NULL; 575254721Semaste bool is_indirect_function = false; 576254721Semaste 577254721Semaste if (sym_ctx.function) 578254721Semaste func_so_addr = &sym_ctx.function->GetAddressRange().GetBaseAddress(); 579254721Semaste else if (sym_ctx.symbol) { 580254721Semaste func_so_addr = &sym_ctx.symbol->GetAddress(); 581254721Semaste is_indirect_function = sym_ctx.symbol->IsIndirect(); 582254721Semaste } else 583254721Semaste return false; 584254721Semaste 585254721Semaste if (!func_so_addr || !func_so_addr->IsValid()) 586254721Semaste return false; 587254721Semaste 588254721Semaste func_addr = func_so_addr->GetCallableLoadAddress (target, is_indirect_function); 589254721Semaste 590254721Semaste return true; 591254721Semaste} 592254721Semaste 593254721Semasteaddr_t 594254721SemasteClangExpressionDeclMap::GetSymbolAddress (Target &target, Process *process, const ConstString &name, lldb::SymbolType symbol_type) 595254721Semaste{ 596254721Semaste SymbolContextList sc_list; 597254721Semaste 598254721Semaste target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list); 599254721Semaste 600254721Semaste const uint32_t num_matches = sc_list.GetSize(); 601254721Semaste addr_t symbol_load_addr = LLDB_INVALID_ADDRESS; 602254721Semaste 603254721Semaste for (uint32_t i=0; i<num_matches && (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS); i++) 604254721Semaste { 605254721Semaste SymbolContext sym_ctx; 606254721Semaste sc_list.GetContextAtIndex(i, sym_ctx); 607254721Semaste 608254721Semaste const Address *sym_address = &sym_ctx.symbol->GetAddress(); 609254721Semaste 610254721Semaste if (!sym_address || !sym_address->IsValid()) 611254721Semaste continue; 612254721Semaste 613254721Semaste if (sym_address) 614254721Semaste { 615254721Semaste switch (sym_ctx.symbol->GetType()) 616254721Semaste { 617254721Semaste case eSymbolTypeCode: 618254721Semaste case eSymbolTypeTrampoline: 619254721Semaste symbol_load_addr = sym_address->GetCallableLoadAddress (&target); 620254721Semaste break; 621254721Semaste 622254721Semaste case eSymbolTypeResolver: 623254721Semaste symbol_load_addr = sym_address->GetCallableLoadAddress (&target, true); 624254721Semaste break; 625254721Semaste 626254721Semaste case eSymbolTypeData: 627254721Semaste case eSymbolTypeRuntime: 628254721Semaste case eSymbolTypeVariable: 629254721Semaste case eSymbolTypeLocal: 630254721Semaste case eSymbolTypeParam: 631254721Semaste case eSymbolTypeInvalid: 632254721Semaste case eSymbolTypeAbsolute: 633254721Semaste case eSymbolTypeException: 634254721Semaste case eSymbolTypeSourceFile: 635254721Semaste case eSymbolTypeHeaderFile: 636254721Semaste case eSymbolTypeObjectFile: 637254721Semaste case eSymbolTypeCommonBlock: 638254721Semaste case eSymbolTypeBlock: 639254721Semaste case eSymbolTypeVariableType: 640254721Semaste case eSymbolTypeLineEntry: 641254721Semaste case eSymbolTypeLineHeader: 642254721Semaste case eSymbolTypeScopeBegin: 643254721Semaste case eSymbolTypeScopeEnd: 644254721Semaste case eSymbolTypeAdditional: 645254721Semaste case eSymbolTypeCompiler: 646254721Semaste case eSymbolTypeInstrumentation: 647254721Semaste case eSymbolTypeUndefined: 648254721Semaste case eSymbolTypeObjCClass: 649254721Semaste case eSymbolTypeObjCMetaClass: 650254721Semaste case eSymbolTypeObjCIVar: 651254721Semaste symbol_load_addr = sym_address->GetLoadAddress (&target); 652254721Semaste break; 653254721Semaste } 654254721Semaste } 655254721Semaste } 656254721Semaste 657254721Semaste if (symbol_load_addr == LLDB_INVALID_ADDRESS && process) 658254721Semaste { 659254721Semaste ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime(); 660254721Semaste 661254721Semaste if (runtime) 662254721Semaste { 663254721Semaste symbol_load_addr = runtime->LookupRuntimeSymbol(name); 664254721Semaste } 665254721Semaste } 666254721Semaste 667254721Semaste return symbol_load_addr; 668254721Semaste} 669254721Semaste 670254721Semasteaddr_t 671254721SemasteClangExpressionDeclMap::GetSymbolAddress (const ConstString &name, lldb::SymbolType symbol_type) 672254721Semaste{ 673254721Semaste assert (m_parser_vars.get()); 674254721Semaste 675254721Semaste if (!m_parser_vars->m_exe_ctx.GetTargetPtr()) 676254721Semaste return false; 677254721Semaste 678254721Semaste return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(), m_parser_vars->m_exe_ctx.GetProcessPtr(), name, symbol_type); 679254721Semaste} 680254721Semaste 681254721Semasteconst Symbol * 682254721SemasteClangExpressionDeclMap::FindGlobalDataSymbol (Target &target, 683254721Semaste const ConstString &name) 684254721Semaste{ 685254721Semaste SymbolContextList sc_list; 686254721Semaste 687254721Semaste target.GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list); 688254721Semaste 689254721Semaste const uint32_t matches = sc_list.GetSize(); 690254721Semaste for (uint32_t i=0; i<matches; ++i) 691254721Semaste { 692254721Semaste SymbolContext sym_ctx; 693254721Semaste sc_list.GetContextAtIndex(i, sym_ctx); 694254721Semaste if (sym_ctx.symbol) 695254721Semaste { 696254721Semaste const Symbol *symbol = sym_ctx.symbol; 697254721Semaste const Address *sym_address = &symbol->GetAddress(); 698254721Semaste 699254721Semaste if (sym_address && sym_address->IsValid()) 700254721Semaste { 701254721Semaste switch (symbol->GetType()) 702254721Semaste { 703254721Semaste case eSymbolTypeData: 704254721Semaste case eSymbolTypeRuntime: 705254721Semaste case eSymbolTypeAbsolute: 706254721Semaste case eSymbolTypeObjCClass: 707254721Semaste case eSymbolTypeObjCMetaClass: 708254721Semaste case eSymbolTypeObjCIVar: 709254721Semaste if (symbol->GetDemangledNameIsSynthesized()) 710254721Semaste { 711254721Semaste // If the demangled name was synthesized, then don't use it 712254721Semaste // for expressions. Only let the symbol match if the mangled 713254721Semaste // named matches for these symbols. 714254721Semaste if (symbol->GetMangled().GetMangledName() != name) 715254721Semaste break; 716254721Semaste } 717254721Semaste return symbol; 718254721Semaste 719254721Semaste case eSymbolTypeCode: // We already lookup functions elsewhere 720254721Semaste case eSymbolTypeVariable: 721254721Semaste case eSymbolTypeLocal: 722254721Semaste case eSymbolTypeParam: 723254721Semaste case eSymbolTypeTrampoline: 724254721Semaste case eSymbolTypeInvalid: 725254721Semaste case eSymbolTypeException: 726254721Semaste case eSymbolTypeSourceFile: 727254721Semaste case eSymbolTypeHeaderFile: 728254721Semaste case eSymbolTypeObjectFile: 729254721Semaste case eSymbolTypeCommonBlock: 730254721Semaste case eSymbolTypeBlock: 731254721Semaste case eSymbolTypeVariableType: 732254721Semaste case eSymbolTypeLineEntry: 733254721Semaste case eSymbolTypeLineHeader: 734254721Semaste case eSymbolTypeScopeBegin: 735254721Semaste case eSymbolTypeScopeEnd: 736254721Semaste case eSymbolTypeAdditional: 737254721Semaste case eSymbolTypeCompiler: 738254721Semaste case eSymbolTypeInstrumentation: 739254721Semaste case eSymbolTypeUndefined: 740254721Semaste case eSymbolTypeResolver: 741254721Semaste break; 742254721Semaste } 743254721Semaste } 744254721Semaste } 745254721Semaste } 746254721Semaste 747254721Semaste return NULL; 748254721Semaste} 749254721Semaste 750254721Semastelldb::VariableSP 751254721SemasteClangExpressionDeclMap::FindGlobalVariable 752254721Semaste( 753254721Semaste Target &target, 754254721Semaste ModuleSP &module, 755254721Semaste const ConstString &name, 756254721Semaste ClangNamespaceDecl *namespace_decl, 757254721Semaste TypeFromUser *type 758254721Semaste) 759254721Semaste{ 760254721Semaste VariableList vars; 761254721Semaste 762254721Semaste if (module && namespace_decl) 763254721Semaste module->FindGlobalVariables (name, namespace_decl, true, -1, vars); 764254721Semaste else 765254721Semaste target.GetImages().FindGlobalVariables(name, true, -1, vars); 766254721Semaste 767254721Semaste if (vars.GetSize()) 768254721Semaste { 769254721Semaste if (type) 770254721Semaste { 771254721Semaste for (size_t i = 0; i < vars.GetSize(); ++i) 772254721Semaste { 773254721Semaste VariableSP var_sp = vars.GetVariableAtIndex(i); 774254721Semaste 775254721Semaste if (ClangASTContext::AreTypesSame(*type, var_sp->GetType()->GetClangFullType())) 776254721Semaste return var_sp; 777254721Semaste } 778254721Semaste } 779254721Semaste else 780254721Semaste { 781254721Semaste return vars.GetVariableAtIndex(0); 782254721Semaste } 783254721Semaste } 784254721Semaste 785254721Semaste return VariableSP(); 786254721Semaste} 787254721Semaste 788254721Semaste// Interface for ClangASTSource 789254721Semaste 790254721Semastevoid 791254721SemasteClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context) 792254721Semaste{ 793254721Semaste assert (m_ast_context); 794254721Semaste 795254721Semaste ClangASTMetrics::RegisterVisibleQuery(); 796254721Semaste 797254721Semaste const ConstString name(context.m_decl_name.getAsString().c_str()); 798254721Semaste 799254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 800254721Semaste 801254721Semaste if (GetImportInProgress()) 802254721Semaste { 803254721Semaste if (log && log->GetVerbose()) 804254721Semaste log->Printf("Ignoring a query during an import"); 805254721Semaste return; 806254721Semaste } 807254721Semaste 808254721Semaste static unsigned int invocation_id = 0; 809254721Semaste unsigned int current_id = invocation_id++; 810254721Semaste 811254721Semaste if (log) 812254721Semaste { 813254721Semaste if (!context.m_decl_context) 814254721Semaste log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a NULL DeclContext", current_id, name.GetCString()); 815254721Semaste else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context)) 816254721Semaste log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in '%s'", current_id, name.GetCString(), context_named_decl->getNameAsString().c_str()); 817254721Semaste else 818254721Semaste log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a '%s'", current_id, name.GetCString(), context.m_decl_context->getDeclKindName()); 819254721Semaste } 820254721Semaste 821254721Semaste if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context)) 822254721Semaste { 823254721Semaste ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context); 824254721Semaste 825254721Semaste if (log && log->GetVerbose()) 826254721Semaste log->Printf(" CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)", 827254721Semaste current_id, 828254721Semaste namespace_map.get(), 829254721Semaste (int)namespace_map->size()); 830254721Semaste 831254721Semaste if (!namespace_map) 832254721Semaste return; 833254721Semaste 834254721Semaste for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end(); 835254721Semaste i != e; 836254721Semaste ++i) 837254721Semaste { 838254721Semaste if (log) 839254721Semaste log->Printf(" CEDM::FEVD[%u] Searching namespace %s in module %s", 840254721Semaste current_id, 841254721Semaste i->second.GetNamespaceDecl()->getNameAsString().c_str(), 842254721Semaste i->first->GetFileSpec().GetFilename().GetCString()); 843254721Semaste 844254721Semaste FindExternalVisibleDecls(context, 845254721Semaste i->first, 846254721Semaste i->second, 847254721Semaste current_id); 848254721Semaste } 849254721Semaste } 850254721Semaste else if (isa<TranslationUnitDecl>(context.m_decl_context)) 851254721Semaste { 852254721Semaste ClangNamespaceDecl namespace_decl; 853254721Semaste 854254721Semaste if (log) 855254721Semaste log->Printf(" CEDM::FEVD[%u] Searching the root namespace", current_id); 856254721Semaste 857254721Semaste FindExternalVisibleDecls(context, 858254721Semaste lldb::ModuleSP(), 859254721Semaste namespace_decl, 860254721Semaste current_id); 861254721Semaste } 862254721Semaste 863254721Semaste if (!context.m_found.variable) 864254721Semaste ClangASTSource::FindExternalVisibleDecls(context); 865254721Semaste} 866254721Semaste 867254721Semastevoid 868254721SemasteClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context, 869254721Semaste lldb::ModuleSP module_sp, 870254721Semaste ClangNamespaceDecl &namespace_decl, 871254721Semaste unsigned int current_id) 872254721Semaste{ 873254721Semaste assert (m_ast_context); 874254721Semaste 875254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 876254721Semaste 877254721Semaste SymbolContextList sc_list; 878254721Semaste 879254721Semaste const ConstString name(context.m_decl_name.getAsString().c_str()); 880254721Semaste 881254721Semaste const char *name_unique_cstr = name.GetCString(); 882254721Semaste 883254721Semaste if (name_unique_cstr == NULL) 884254721Semaste return; 885254721Semaste 886254721Semaste static ConstString id_name("id"); 887254721Semaste static ConstString Class_name("Class"); 888254721Semaste 889254721Semaste if (name == id_name || name == Class_name) 890254721Semaste return; 891254721Semaste 892254721Semaste // Only look for functions by name out in our symbols if the function 893254721Semaste // doesn't start with our phony prefix of '$' 894254721Semaste Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 895254721Semaste StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); 896254721Semaste if (name_unique_cstr[0] == '$' && !namespace_decl) 897254721Semaste { 898254721Semaste static ConstString g_lldb_class_name ("$__lldb_class"); 899254721Semaste 900254721Semaste if (name == g_lldb_class_name) 901254721Semaste { 902254721Semaste // Clang is looking for the type of "this" 903254721Semaste 904254721Semaste if (frame == NULL) 905254721Semaste return; 906254721Semaste 907254721Semaste SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction); 908254721Semaste 909254721Semaste if (!sym_ctx.function) 910254721Semaste return; 911254721Semaste 912254721Semaste // Get the block that defines the function 913254721Semaste Block *function_block = sym_ctx.GetFunctionBlock(); 914254721Semaste 915254721Semaste if (!function_block) 916254721Semaste return; 917254721Semaste 918254721Semaste clang::DeclContext *decl_context = function_block->GetClangDeclContext(); 919254721Semaste 920254721Semaste if (!decl_context) 921254721Semaste return; 922254721Semaste 923254721Semaste clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context); 924254721Semaste 925254721Semaste if (method_decl) 926254721Semaste { 927254721Semaste clang::CXXRecordDecl *class_decl = method_decl->getParent(); 928254721Semaste 929254721Semaste QualType class_qual_type(class_decl->getTypeForDecl(), 0); 930254721Semaste 931254721Semaste TypeFromUser class_user_type (class_qual_type.getAsOpaquePtr(), 932254721Semaste &class_decl->getASTContext()); 933254721Semaste 934254721Semaste if (log) 935254721Semaste { 936254721Semaste ASTDumper ast_dumper(class_qual_type); 937254721Semaste log->Printf(" CEDM::FEVD[%u] Adding type for $__lldb_class: %s", current_id, ast_dumper.GetCString()); 938254721Semaste } 939254721Semaste 940254721Semaste TypeFromParser class_type = CopyClassType(class_user_type, current_id); 941254721Semaste 942254721Semaste if (!class_type.IsValid()) 943254721Semaste return; 944254721Semaste 945254721Semaste TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(QualType::getFromOpaquePtr(class_type.GetOpaqueQualType())); 946254721Semaste 947254721Semaste if (!type_source_info) 948254721Semaste return; 949254721Semaste 950254721Semaste TypedefDecl *typedef_decl = TypedefDecl::Create(*m_ast_context, 951254721Semaste m_ast_context->getTranslationUnitDecl(), 952254721Semaste SourceLocation(), 953254721Semaste SourceLocation(), 954254721Semaste context.m_decl_name.getAsIdentifierInfo(), 955254721Semaste type_source_info); 956254721Semaste 957254721Semaste 958254721Semaste if (!typedef_decl) 959254721Semaste return; 960254721Semaste 961254721Semaste context.AddNamedDecl(typedef_decl); 962254721Semaste 963254721Semaste if (method_decl->isInstance()) 964254721Semaste { 965254721Semaste // self is a pointer to the object 966254721Semaste 967254721Semaste QualType class_pointer_type = method_decl->getASTContext().getPointerType(class_qual_type); 968254721Semaste 969254721Semaste TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(), 970254721Semaste &method_decl->getASTContext()); 971254721Semaste 972254721Semaste m_struct_vars->m_object_pointer_type = self_user_type; 973254721Semaste } 974254721Semaste } 975254721Semaste else 976254721Semaste { 977254721Semaste // This branch will get hit if we are executing code in the context of a function that 978254721Semaste // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a 979254721Semaste // method of the class. In that case, just look up the "this" variable in the the current 980254721Semaste // scope and use its type. 981254721Semaste // FIXME: This code is formally correct, but clang doesn't currently emit DW_AT_object_pointer 982254721Semaste // for C++ so it hasn't actually been tested. 983254721Semaste 984254721Semaste VariableList *vars = frame->GetVariableList(false); 985254721Semaste 986254721Semaste lldb::VariableSP this_var = vars->FindVariable(ConstString("this")); 987254721Semaste 988254721Semaste if (this_var && 989254721Semaste this_var->IsInScope(frame) && 990254721Semaste this_var->LocationIsValidForFrame (frame)) 991254721Semaste { 992254721Semaste Type *this_type = this_var->GetType(); 993254721Semaste 994254721Semaste if (!this_type) 995254721Semaste return; 996254721Semaste 997254721Semaste ClangASTType pointee_type = this_type->GetClangForwardType().GetPointeeType(); 998254721Semaste 999254721Semaste if (pointee_type.IsValid()) 1000254721Semaste { 1001254721Semaste if (log) 1002254721Semaste { 1003254721Semaste ASTDumper ast_dumper(this_type->GetClangFullType()); 1004254721Semaste log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString()); 1005254721Semaste } 1006254721Semaste 1007254721Semaste TypeFromUser class_user_type(pointee_type); 1008254721Semaste AddOneType(context, class_user_type, current_id); 1009254721Semaste 1010254721Semaste 1011254721Semaste TypeFromUser this_user_type(this_type->GetClangFullType()); 1012254721Semaste m_struct_vars->m_object_pointer_type = this_user_type; 1013254721Semaste return; 1014254721Semaste } 1015254721Semaste } 1016254721Semaste } 1017254721Semaste 1018254721Semaste return; 1019254721Semaste } 1020254721Semaste 1021254721Semaste static ConstString g_lldb_objc_class_name ("$__lldb_objc_class"); 1022254721Semaste if (name == g_lldb_objc_class_name) 1023254721Semaste { 1024254721Semaste // Clang is looking for the type of "*self" 1025254721Semaste 1026254721Semaste if (!frame) 1027254721Semaste return; 1028254721Semaste 1029254721Semaste SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction); 1030254721Semaste 1031254721Semaste if (!sym_ctx.function) 1032254721Semaste return; 1033254721Semaste 1034254721Semaste // Get the block that defines the function 1035254721Semaste Block *function_block = sym_ctx.GetFunctionBlock(); 1036254721Semaste 1037254721Semaste if (!function_block) 1038254721Semaste return; 1039254721Semaste 1040254721Semaste clang::DeclContext *decl_context = function_block->GetClangDeclContext(); 1041254721Semaste 1042254721Semaste if (!decl_context) 1043254721Semaste return; 1044254721Semaste 1045254721Semaste clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context); 1046254721Semaste 1047254721Semaste if (method_decl) 1048254721Semaste { 1049254721Semaste ObjCInterfaceDecl* self_interface = method_decl->getClassInterface(); 1050254721Semaste 1051254721Semaste if (!self_interface) 1052254721Semaste return; 1053254721Semaste 1054254721Semaste const clang::Type *interface_type = self_interface->getTypeForDecl(); 1055254721Semaste 1056254721Semaste if (!interface_type) 1057254721Semaste return; // This is unlikely, but we have seen crashes where this occurred 1058254721Semaste 1059254721Semaste TypeFromUser class_user_type(QualType(interface_type, 0).getAsOpaquePtr(), 1060254721Semaste &method_decl->getASTContext()); 1061254721Semaste 1062254721Semaste if (log) 1063254721Semaste { 1064254721Semaste ASTDumper ast_dumper(interface_type); 1065254721Semaste log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString()); 1066254721Semaste } 1067254721Semaste 1068254721Semaste AddOneType(context, class_user_type, current_id); 1069254721Semaste 1070254721Semaste if (method_decl->isInstanceMethod()) 1071254721Semaste { 1072254721Semaste // self is a pointer to the object 1073254721Semaste 1074254721Semaste QualType class_pointer_type = method_decl->getASTContext().getObjCObjectPointerType(QualType(interface_type, 0)); 1075254721Semaste 1076254721Semaste TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(), 1077254721Semaste &method_decl->getASTContext()); 1078254721Semaste 1079254721Semaste m_struct_vars->m_object_pointer_type = self_user_type; 1080254721Semaste } 1081254721Semaste else 1082254721Semaste { 1083254721Semaste // self is a Class pointer 1084254721Semaste QualType class_type = method_decl->getASTContext().getObjCClassType(); 1085254721Semaste 1086254721Semaste TypeFromUser self_user_type(class_type.getAsOpaquePtr(), 1087254721Semaste &method_decl->getASTContext()); 1088254721Semaste 1089254721Semaste m_struct_vars->m_object_pointer_type = self_user_type; 1090254721Semaste } 1091254721Semaste 1092254721Semaste return; 1093254721Semaste } 1094254721Semaste else 1095254721Semaste { 1096254721Semaste // This branch will get hit if we are executing code in the context of a function that 1097254721Semaste // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a 1098254721Semaste // method of the class. In that case, just look up the "self" variable in the the current 1099254721Semaste // scope and use its type. 1100254721Semaste 1101254721Semaste VariableList *vars = frame->GetVariableList(false); 1102254721Semaste 1103254721Semaste lldb::VariableSP self_var = vars->FindVariable(ConstString("self")); 1104254721Semaste 1105254721Semaste if (self_var && 1106254721Semaste self_var->IsInScope(frame) && 1107254721Semaste self_var->LocationIsValidForFrame (frame)) 1108254721Semaste { 1109254721Semaste Type *self_type = self_var->GetType(); 1110254721Semaste 1111254721Semaste if (!self_type) 1112254721Semaste return; 1113254721Semaste 1114254721Semaste ClangASTType self_clang_type = self_type->GetClangFullType(); 1115254721Semaste 1116254721Semaste if (self_clang_type.IsObjCClassType()) 1117254721Semaste { 1118254721Semaste return; 1119254721Semaste } 1120254721Semaste else if (self_clang_type.IsObjCObjectPointerType()) 1121254721Semaste { 1122254721Semaste self_clang_type = self_clang_type.GetPointeeType(); 1123254721Semaste 1124254721Semaste if (!self_clang_type) 1125254721Semaste return; 1126254721Semaste 1127254721Semaste if (log) 1128254721Semaste { 1129254721Semaste ASTDumper ast_dumper(self_type->GetClangFullType()); 1130254721Semaste log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString()); 1131254721Semaste } 1132254721Semaste 1133254721Semaste TypeFromUser class_user_type (self_clang_type); 1134254721Semaste 1135254721Semaste AddOneType(context, class_user_type, current_id); 1136254721Semaste 1137254721Semaste TypeFromUser self_user_type(self_type->GetClangFullType()); 1138254721Semaste 1139254721Semaste m_struct_vars->m_object_pointer_type = self_user_type; 1140254721Semaste return; 1141254721Semaste } 1142254721Semaste } 1143254721Semaste } 1144254721Semaste 1145254721Semaste return; 1146254721Semaste } 1147254721Semaste 1148254721Semaste // any other $__lldb names should be weeded out now 1149254721Semaste if (!::strncmp(name_unique_cstr, "$__lldb", sizeof("$__lldb") - 1)) 1150254721Semaste return; 1151254721Semaste 1152254721Semaste do 1153254721Semaste { 1154254721Semaste if (!target) 1155254721Semaste break; 1156254721Semaste 1157254721Semaste ClangASTContext *scratch_clang_ast_context = target->GetScratchClangASTContext(); 1158254721Semaste 1159254721Semaste if (!scratch_clang_ast_context) 1160254721Semaste break; 1161254721Semaste 1162254721Semaste ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext(); 1163254721Semaste 1164254721Semaste if (!scratch_ast_context) 1165254721Semaste break; 1166254721Semaste 1167254721Semaste TypeDecl *ptype_type_decl = m_parser_vars->m_persistent_vars->GetPersistentType(name); 1168254721Semaste 1169254721Semaste if (!ptype_type_decl) 1170254721Semaste break; 1171254721Semaste 1172254721Semaste Decl *parser_ptype_decl = m_ast_importer->CopyDecl(m_ast_context, scratch_ast_context, ptype_type_decl); 1173254721Semaste 1174254721Semaste if (!parser_ptype_decl) 1175254721Semaste break; 1176254721Semaste 1177254721Semaste TypeDecl *parser_ptype_type_decl = dyn_cast<TypeDecl>(parser_ptype_decl); 1178254721Semaste 1179254721Semaste if (!parser_ptype_type_decl) 1180254721Semaste break; 1181254721Semaste 1182254721Semaste if (log) 1183254721Semaste log->Printf(" CEDM::FEVD[%u] Found persistent type %s", current_id, name.GetCString()); 1184254721Semaste 1185254721Semaste context.AddNamedDecl(parser_ptype_type_decl); 1186254721Semaste } while (0); 1187254721Semaste 1188254721Semaste ClangExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariable(name)); 1189254721Semaste 1190254721Semaste if (pvar_sp) 1191254721Semaste { 1192254721Semaste AddOneVariable(context, pvar_sp, current_id); 1193254721Semaste return; 1194254721Semaste } 1195254721Semaste 1196254721Semaste const char *reg_name(&name.GetCString()[1]); 1197254721Semaste 1198254721Semaste if (m_parser_vars->m_exe_ctx.GetRegisterContext()) 1199254721Semaste { 1200254721Semaste const RegisterInfo *reg_info(m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(reg_name)); 1201254721Semaste 1202254721Semaste if (reg_info) 1203254721Semaste { 1204254721Semaste if (log) 1205254721Semaste log->Printf(" CEDM::FEVD[%u] Found register %s", current_id, reg_info->name); 1206254721Semaste 1207254721Semaste AddOneRegister(context, reg_info, current_id); 1208254721Semaste } 1209254721Semaste } 1210254721Semaste } 1211254721Semaste else 1212254721Semaste { 1213254721Semaste ValueObjectSP valobj; 1214254721Semaste VariableSP var; 1215254721Semaste Error err; 1216254721Semaste 1217254721Semaste if (frame && !namespace_decl) 1218254721Semaste { 1219254721Semaste valobj = frame->GetValueForVariableExpressionPath(name_unique_cstr, 1220254721Semaste eNoDynamicValues, 1221254721Semaste StackFrame::eExpressionPathOptionCheckPtrVsMember || 1222254721Semaste StackFrame::eExpressionPathOptionsAllowDirectIVarAccess || 1223254721Semaste StackFrame::eExpressionPathOptionsNoFragileObjcIvar || 1224254721Semaste StackFrame::eExpressionPathOptionsNoSyntheticChildren || 1225254721Semaste StackFrame::eExpressionPathOptionsNoSyntheticArrayRange, 1226254721Semaste var, 1227254721Semaste err); 1228254721Semaste 1229254721Semaste // If we found a variable in scope, no need to pull up function names 1230254721Semaste if (err.Success() && var) 1231254721Semaste { 1232254721Semaste AddOneVariable(context, var, valobj, current_id); 1233254721Semaste context.m_found.variable = true; 1234254721Semaste return; 1235254721Semaste } 1236254721Semaste } 1237254721Semaste 1238254721Semaste if (target) 1239254721Semaste { 1240254721Semaste var = FindGlobalVariable (*target, 1241254721Semaste module_sp, 1242254721Semaste name, 1243254721Semaste &namespace_decl, 1244254721Semaste NULL); 1245254721Semaste 1246254721Semaste if (var) 1247254721Semaste { 1248254721Semaste valobj = ValueObjectVariable::Create(target, var); 1249254721Semaste AddOneVariable(context, var, valobj, current_id); 1250254721Semaste context.m_found.variable = true; 1251254721Semaste return; 1252254721Semaste } 1253254721Semaste } 1254254721Semaste 1255254721Semaste if (!context.m_found.variable) 1256254721Semaste { 1257254721Semaste const bool include_inlines = false; 1258254721Semaste const bool append = false; 1259254721Semaste 1260254721Semaste if (namespace_decl && module_sp) 1261254721Semaste { 1262254721Semaste const bool include_symbols = false; 1263254721Semaste 1264254721Semaste module_sp->FindFunctions(name, 1265254721Semaste &namespace_decl, 1266254721Semaste eFunctionNameTypeBase, 1267254721Semaste include_symbols, 1268254721Semaste include_inlines, 1269254721Semaste append, 1270254721Semaste sc_list); 1271254721Semaste } 1272254721Semaste else if (target && !namespace_decl) 1273254721Semaste { 1274254721Semaste const bool include_symbols = true; 1275254721Semaste 1276254721Semaste // TODO Fix FindFunctions so that it doesn't return 1277254721Semaste // instance methods for eFunctionNameTypeBase. 1278254721Semaste 1279254721Semaste target->GetImages().FindFunctions(name, 1280254721Semaste eFunctionNameTypeFull, 1281254721Semaste include_symbols, 1282254721Semaste include_inlines, 1283254721Semaste append, 1284254721Semaste sc_list); 1285254721Semaste } 1286254721Semaste 1287254721Semaste if (sc_list.GetSize()) 1288254721Semaste { 1289254721Semaste Symbol *generic_symbol = NULL; 1290254721Semaste Symbol *non_extern_symbol = NULL; 1291254721Semaste 1292254721Semaste for (uint32_t index = 0, num_indices = sc_list.GetSize(); 1293254721Semaste index < num_indices; 1294254721Semaste ++index) 1295254721Semaste { 1296254721Semaste SymbolContext sym_ctx; 1297254721Semaste sc_list.GetContextAtIndex(index, sym_ctx); 1298254721Semaste 1299254721Semaste if (sym_ctx.function) 1300254721Semaste { 1301254721Semaste clang::DeclContext *decl_ctx = sym_ctx.function->GetClangDeclContext(); 1302254721Semaste 1303254721Semaste if (!decl_ctx) 1304254721Semaste continue; 1305254721Semaste 1306254721Semaste // Filter out class/instance methods. 1307254721Semaste if (dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) 1308254721Semaste continue; 1309254721Semaste if (dyn_cast<clang::CXXMethodDecl>(decl_ctx)) 1310254721Semaste continue; 1311254721Semaste 1312254721Semaste AddOneFunction(context, sym_ctx.function, NULL, current_id); 1313254721Semaste context.m_found.function_with_type_info = true; 1314254721Semaste context.m_found.function = true; 1315254721Semaste } 1316254721Semaste else if (sym_ctx.symbol) 1317254721Semaste { 1318254721Semaste if (sym_ctx.symbol->IsExternal()) 1319254721Semaste generic_symbol = sym_ctx.symbol; 1320254721Semaste else 1321254721Semaste non_extern_symbol = sym_ctx.symbol; 1322254721Semaste } 1323254721Semaste } 1324254721Semaste 1325254721Semaste if (!context.m_found.function_with_type_info) 1326254721Semaste { 1327254721Semaste if (generic_symbol) 1328254721Semaste { 1329254721Semaste AddOneFunction (context, NULL, generic_symbol, current_id); 1330254721Semaste context.m_found.function = true; 1331254721Semaste } 1332254721Semaste else if (non_extern_symbol) 1333254721Semaste { 1334254721Semaste AddOneFunction (context, NULL, non_extern_symbol, current_id); 1335254721Semaste context.m_found.function = true; 1336254721Semaste } 1337254721Semaste } 1338254721Semaste } 1339254721Semaste 1340254721Semaste if (target && !context.m_found.variable && !namespace_decl) 1341254721Semaste { 1342254721Semaste // We couldn't find a non-symbol variable for this. Now we'll hunt for a generic 1343254721Semaste // data symbol, and -- if it is found -- treat it as a variable. 1344254721Semaste 1345254721Semaste const Symbol *data_symbol = FindGlobalDataSymbol(*target, name); 1346254721Semaste 1347254721Semaste if (data_symbol) 1348254721Semaste { 1349254721Semaste AddOneGenericVariable(context, *data_symbol, current_id); 1350254721Semaste context.m_found.variable = true; 1351254721Semaste } 1352254721Semaste } 1353254721Semaste } 1354254721Semaste } 1355254721Semaste} 1356254721Semaste 1357254721Semastestatic clang_type_t 1358254721SemasteMaybePromoteToBlockPointerType 1359254721Semaste( 1360254721Semaste ASTContext *ast_context, 1361254721Semaste clang_type_t candidate_type 1362254721Semaste) 1363254721Semaste{ 1364254721Semaste if (!candidate_type) 1365254721Semaste return candidate_type; 1366254721Semaste 1367254721Semaste QualType candidate_qual_type = QualType::getFromOpaquePtr(candidate_type); 1368254721Semaste 1369254721Semaste const PointerType *candidate_pointer_type = dyn_cast<PointerType>(candidate_qual_type); 1370254721Semaste 1371254721Semaste if (!candidate_pointer_type) 1372254721Semaste return candidate_type; 1373254721Semaste 1374254721Semaste QualType pointee_qual_type = candidate_pointer_type->getPointeeType(); 1375254721Semaste 1376254721Semaste const RecordType *pointee_record_type = dyn_cast<RecordType>(pointee_qual_type); 1377254721Semaste 1378254721Semaste if (!pointee_record_type) 1379254721Semaste return candidate_type; 1380254721Semaste 1381254721Semaste RecordDecl *pointee_record_decl = pointee_record_type->getDecl(); 1382254721Semaste 1383254721Semaste if (!pointee_record_decl->isRecord()) 1384254721Semaste return candidate_type; 1385254721Semaste 1386254721Semaste if (!pointee_record_decl->getName().startswith(llvm::StringRef("__block_literal_"))) 1387254721Semaste return candidate_type; 1388254721Semaste 1389254721Semaste QualType generic_function_type = ast_context->getFunctionNoProtoType(ast_context->UnknownAnyTy); 1390254721Semaste QualType block_pointer_type = ast_context->getBlockPointerType(generic_function_type); 1391254721Semaste 1392254721Semaste return block_pointer_type.getAsOpaquePtr(); 1393254721Semaste} 1394254721Semaste 1395254721Semastebool 1396254721SemasteClangExpressionDeclMap::GetVariableValue (VariableSP &var, 1397254721Semaste lldb_private::Value &var_location, 1398254721Semaste TypeFromUser *user_type, 1399254721Semaste TypeFromParser *parser_type) 1400254721Semaste{ 1401254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1402254721Semaste 1403254721Semaste Type *var_type = var->GetType(); 1404254721Semaste 1405254721Semaste if (!var_type) 1406254721Semaste { 1407254721Semaste if (log) 1408254721Semaste log->PutCString("Skipped a definition because it has no type"); 1409254721Semaste return false; 1410254721Semaste } 1411254721Semaste 1412254721Semaste ClangASTType var_clang_type = var_type->GetClangFullType(); 1413254721Semaste 1414254721Semaste if (!var_clang_type) 1415254721Semaste { 1416254721Semaste if (log) 1417254721Semaste log->PutCString("Skipped a definition because it has no Clang type"); 1418254721Semaste return false; 1419254721Semaste } 1420254721Semaste 1421254721Semaste // commented out because of <rdar://problem/11024417> 1422254721Semaste ASTContext *ast = var_type->GetClangASTContext().getASTContext(); 1423254721Semaste 1424254721Semaste if (!ast) 1425254721Semaste { 1426254721Semaste if (log) 1427254721Semaste log->PutCString("There is no AST context for the current execution context"); 1428254721Semaste return false; 1429254721Semaste } 1430254721Semaste //var_clang_type = MaybePromoteToBlockPointerType (ast, var_clang_type); 1431254721Semaste 1432254721Semaste DWARFExpression &var_location_expr = var->LocationExpression(); 1433254721Semaste 1434254721Semaste lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS; 1435254721Semaste 1436254721Semaste Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 1437254721Semaste 1438254721Semaste if (var_location_expr.IsLocationList()) 1439254721Semaste { 1440254721Semaste SymbolContext var_sc; 1441254721Semaste var->CalculateSymbolContext (&var_sc); 1442254721Semaste loclist_base_load_addr = var_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target); 1443254721Semaste } 1444254721Semaste Error err; 1445254721Semaste 1446254721Semaste if (var->GetLocationIsConstantValueData()) 1447254721Semaste { 1448254721Semaste DataExtractor const_value_extractor; 1449254721Semaste 1450254721Semaste if (var_location_expr.GetExpressionData(const_value_extractor)) 1451254721Semaste { 1452254721Semaste var_location = Value(const_value_extractor.GetDataStart(), const_value_extractor.GetByteSize()); 1453254721Semaste var_location.SetValueType(Value::eValueTypeHostAddress); 1454254721Semaste } 1455254721Semaste else 1456254721Semaste { 1457254721Semaste if (log) 1458254721Semaste log->Printf("Error evaluating constant variable: %s", err.AsCString()); 1459254721Semaste return false; 1460254721Semaste } 1461254721Semaste } 1462254721Semaste 1463254721Semaste ClangASTType type_to_use = GuardedCopyType(var_clang_type); 1464254721Semaste 1465254721Semaste if (!type_to_use) 1466254721Semaste { 1467254721Semaste if (log) 1468254721Semaste log->Printf("Couldn't copy a variable's type into the parser's AST context"); 1469254721Semaste 1470254721Semaste return false; 1471254721Semaste } 1472254721Semaste 1473254721Semaste if (parser_type) 1474254721Semaste *parser_type = TypeFromParser(type_to_use); 1475254721Semaste 1476254721Semaste if (var_location.GetContextType() == Value::eContextTypeInvalid) 1477254721Semaste var_location.SetClangType(type_to_use); 1478254721Semaste 1479254721Semaste if (var_location.GetValueType() == Value::eValueTypeFileAddress) 1480254721Semaste { 1481254721Semaste SymbolContext var_sc; 1482254721Semaste var->CalculateSymbolContext(&var_sc); 1483254721Semaste 1484254721Semaste if (!var_sc.module_sp) 1485254721Semaste return false; 1486254721Semaste 1487254721Semaste Address so_addr(var_location.GetScalar().ULongLong(), var_sc.module_sp->GetSectionList()); 1488254721Semaste 1489254721Semaste lldb::addr_t load_addr = so_addr.GetLoadAddress(target); 1490254721Semaste 1491254721Semaste if (load_addr != LLDB_INVALID_ADDRESS) 1492254721Semaste { 1493254721Semaste var_location.GetScalar() = load_addr; 1494254721Semaste var_location.SetValueType(Value::eValueTypeLoadAddress); 1495254721Semaste } 1496254721Semaste } 1497254721Semaste 1498254721Semaste if (user_type) 1499254721Semaste *user_type = TypeFromUser(var_clang_type); 1500254721Semaste 1501254721Semaste return true; 1502254721Semaste} 1503254721Semaste 1504254721Semastevoid 1505254721SemasteClangExpressionDeclMap::AddOneVariable (NameSearchContext &context, VariableSP var, ValueObjectSP valobj, unsigned int current_id) 1506254721Semaste{ 1507254721Semaste assert (m_parser_vars.get()); 1508254721Semaste 1509254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1510254721Semaste 1511254721Semaste TypeFromUser ut; 1512254721Semaste TypeFromParser pt; 1513254721Semaste Value var_location; 1514254721Semaste 1515254721Semaste if (!GetVariableValue (var, var_location, &ut, &pt)) 1516254721Semaste return; 1517254721Semaste 1518254721Semaste clang::QualType parser_opaque_type = QualType::getFromOpaquePtr(pt.GetOpaqueQualType()); 1519254721Semaste 1520254721Semaste if (parser_opaque_type.isNull()) 1521254721Semaste return; 1522254721Semaste 1523254721Semaste if (const clang::Type *parser_type = parser_opaque_type.getTypePtr()) 1524254721Semaste { 1525254721Semaste if (const TagType *tag_type = dyn_cast<TagType>(parser_type)) 1526254721Semaste CompleteType(tag_type->getDecl()); 1527254721Semaste } 1528254721Semaste 1529254721Semaste 1530254721Semaste bool is_reference = pt.IsReferenceType(); 1531254721Semaste 1532254721Semaste NamedDecl *var_decl = NULL; 1533254721Semaste if (is_reference) 1534254721Semaste var_decl = context.AddVarDecl(pt); 1535254721Semaste else 1536254721Semaste var_decl = context.AddVarDecl(pt.GetLValueReferenceType()); 1537254721Semaste 1538254721Semaste std::string decl_name(context.m_decl_name.getAsString()); 1539254721Semaste ConstString entity_name(decl_name.c_str()); 1540254721Semaste ClangExpressionVariableSP entity(m_found_entities.CreateVariable (valobj)); 1541254721Semaste 1542254721Semaste assert (entity.get()); 1543254721Semaste entity->EnableParserVars(GetParserID()); 1544254721Semaste ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID()); 1545254721Semaste parser_vars->m_parser_type = pt; 1546254721Semaste parser_vars->m_named_decl = var_decl; 1547254721Semaste parser_vars->m_llvm_value = NULL; 1548254721Semaste parser_vars->m_lldb_value = var_location; 1549254721Semaste parser_vars->m_lldb_var = var; 1550254721Semaste 1551254721Semaste if (is_reference) 1552254721Semaste entity->m_flags |= ClangExpressionVariable::EVTypeIsReference; 1553254721Semaste 1554254721Semaste if (log) 1555254721Semaste { 1556254721Semaste ASTDumper orig_dumper(ut.GetOpaqueQualType()); 1557254721Semaste ASTDumper ast_dumper(var_decl); 1558254721Semaste log->Printf(" CEDM::FEVD[%u] Found variable %s, returned %s (original %s)", current_id, decl_name.c_str(), ast_dumper.GetCString(), orig_dumper.GetCString()); 1559254721Semaste } 1560254721Semaste} 1561254721Semaste 1562254721Semastevoid 1563254721SemasteClangExpressionDeclMap::AddOneVariable(NameSearchContext &context, 1564254721Semaste ClangExpressionVariableSP &pvar_sp, 1565254721Semaste unsigned int current_id) 1566254721Semaste{ 1567254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1568254721Semaste 1569254721Semaste TypeFromUser user_type (pvar_sp->GetTypeFromUser()); 1570254721Semaste 1571254721Semaste TypeFromParser parser_type (GuardedCopyType(user_type)); 1572254721Semaste 1573254721Semaste if (!parser_type.GetOpaqueQualType()) 1574254721Semaste { 1575254721Semaste if (log) 1576254721Semaste log->Printf(" CEDM::FEVD[%u] Couldn't import type for pvar %s", current_id, pvar_sp->GetName().GetCString()); 1577254721Semaste return; 1578254721Semaste } 1579254721Semaste 1580254721Semaste NamedDecl *var_decl = context.AddVarDecl(parser_type.GetLValueReferenceType()); 1581254721Semaste 1582254721Semaste pvar_sp->EnableParserVars(GetParserID()); 1583254721Semaste ClangExpressionVariable::ParserVars *parser_vars = pvar_sp->GetParserVars(GetParserID()); 1584254721Semaste parser_vars->m_parser_type = parser_type; 1585254721Semaste parser_vars->m_named_decl = var_decl; 1586254721Semaste parser_vars->m_llvm_value = NULL; 1587254721Semaste parser_vars->m_lldb_value.Clear(); 1588254721Semaste 1589254721Semaste if (log) 1590254721Semaste { 1591254721Semaste ASTDumper ast_dumper(var_decl); 1592254721Semaste log->Printf(" CEDM::FEVD[%u] Added pvar %s, returned %s", current_id, pvar_sp->GetName().GetCString(), ast_dumper.GetCString()); 1593254721Semaste } 1594254721Semaste} 1595254721Semaste 1596254721Semastevoid 1597254721SemasteClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context, 1598254721Semaste const Symbol &symbol, 1599254721Semaste unsigned int current_id) 1600254721Semaste{ 1601254721Semaste assert(m_parser_vars.get()); 1602254721Semaste 1603254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1604254721Semaste 1605254721Semaste Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 1606254721Semaste 1607254721Semaste if (target == NULL) 1608254721Semaste return; 1609254721Semaste 1610254721Semaste ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext(); 1611254721Semaste 1612254721Semaste TypeFromUser user_type (ClangASTContext::GetBasicType(scratch_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType()); 1613254721Semaste TypeFromParser parser_type (ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType()); 1614254721Semaste NamedDecl *var_decl = context.AddVarDecl(parser_type); 1615254721Semaste 1616254721Semaste std::string decl_name(context.m_decl_name.getAsString()); 1617254721Semaste ConstString entity_name(decl_name.c_str()); 1618254721Semaste ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (), 1619254721Semaste entity_name, 1620254721Semaste user_type, 1621254721Semaste m_parser_vars->m_target_info.byte_order, 1622254721Semaste m_parser_vars->m_target_info.address_byte_size)); 1623254721Semaste assert (entity.get()); 1624254721Semaste 1625254721Semaste entity->EnableParserVars(GetParserID()); 1626254721Semaste ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID()); 1627254721Semaste 1628254721Semaste const Address &symbol_address = symbol.GetAddress(); 1629254721Semaste lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target); 1630254721Semaste 1631254721Semaste //parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType()); 1632254721Semaste parser_vars->m_lldb_value.SetClangType(user_type); 1633254721Semaste parser_vars->m_lldb_value.GetScalar() = symbol_load_addr; 1634254721Semaste parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress); 1635254721Semaste 1636254721Semaste parser_vars->m_parser_type = parser_type; 1637254721Semaste parser_vars->m_named_decl = var_decl; 1638254721Semaste parser_vars->m_llvm_value = NULL; 1639254721Semaste parser_vars->m_lldb_sym = &symbol; 1640254721Semaste 1641254721Semaste if (log) 1642254721Semaste { 1643254721Semaste ASTDumper ast_dumper(var_decl); 1644254721Semaste 1645254721Semaste log->Printf(" CEDM::FEVD[%u] Found variable %s, returned %s", current_id, decl_name.c_str(), ast_dumper.GetCString()); 1646254721Semaste } 1647254721Semaste} 1648254721Semaste 1649254721Semastebool 1650254721SemasteClangExpressionDeclMap::ResolveUnknownTypes() 1651254721Semaste{ 1652254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1653254721Semaste Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 1654254721Semaste 1655254721Semaste ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext(); 1656254721Semaste 1657254721Semaste for (size_t index = 0, num_entities = m_found_entities.GetSize(); 1658254721Semaste index < num_entities; 1659254721Semaste ++index) 1660254721Semaste { 1661254721Semaste ClangExpressionVariableSP entity = m_found_entities.GetVariableAtIndex(index); 1662254721Semaste 1663254721Semaste ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID()); 1664254721Semaste 1665254721Semaste if (entity->m_flags & ClangExpressionVariable::EVUnknownType) 1666254721Semaste { 1667254721Semaste const NamedDecl *named_decl = parser_vars->m_named_decl; 1668254721Semaste const VarDecl *var_decl = dyn_cast<VarDecl>(named_decl); 1669254721Semaste 1670254721Semaste if (!var_decl) 1671254721Semaste { 1672254721Semaste if (log) 1673254721Semaste log->Printf("Entity of unknown type does not have a VarDecl"); 1674254721Semaste return false; 1675254721Semaste } 1676254721Semaste 1677254721Semaste if (log) 1678254721Semaste { 1679254721Semaste ASTDumper ast_dumper(const_cast<VarDecl*>(var_decl)); 1680254721Semaste log->Printf("Variable of unknown type now has Decl %s", ast_dumper.GetCString()); 1681254721Semaste } 1682254721Semaste 1683254721Semaste QualType var_type = var_decl->getType(); 1684254721Semaste TypeFromParser parser_type(var_type.getAsOpaquePtr(), &var_decl->getASTContext()); 1685254721Semaste 1686254721Semaste lldb::clang_type_t copied_type = m_ast_importer->CopyType(scratch_ast_context, &var_decl->getASTContext(), var_type.getAsOpaquePtr()); 1687254721Semaste 1688254721Semaste if (!copied_type) 1689254721Semaste { 1690254721Semaste if (log) 1691254721Semaste log->Printf("ClangExpressionDeclMap::ResolveUnknownType - Couldn't import the type for a variable"); 1692254721Semaste 1693254721Semaste return (bool) lldb::ClangExpressionVariableSP(); 1694254721Semaste } 1695254721Semaste 1696254721Semaste TypeFromUser user_type(copied_type, scratch_ast_context); 1697254721Semaste 1698254721Semaste// parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType()); 1699254721Semaste parser_vars->m_lldb_value.SetClangType(user_type); 1700254721Semaste parser_vars->m_parser_type = parser_type; 1701254721Semaste 1702254721Semaste entity->SetClangType(user_type); 1703254721Semaste 1704254721Semaste entity->m_flags &= ~(ClangExpressionVariable::EVUnknownType); 1705254721Semaste } 1706254721Semaste } 1707254721Semaste 1708254721Semaste return true; 1709254721Semaste} 1710254721Semaste 1711254721Semastevoid 1712254721SemasteClangExpressionDeclMap::AddOneRegister (NameSearchContext &context, 1713254721Semaste const RegisterInfo *reg_info, 1714254721Semaste unsigned int current_id) 1715254721Semaste{ 1716254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1717254721Semaste 1718254721Semaste ClangASTType clang_type = ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (m_ast_context, 1719254721Semaste reg_info->encoding, 1720254721Semaste reg_info->byte_size * 8); 1721254721Semaste 1722254721Semaste if (!clang_type) 1723254721Semaste { 1724254721Semaste if (log) 1725254721Semaste log->Printf(" Tried to add a type for %s, but couldn't get one", context.m_decl_name.getAsString().c_str()); 1726254721Semaste return; 1727254721Semaste } 1728254721Semaste 1729254721Semaste TypeFromParser parser_clang_type (clang_type); 1730254721Semaste 1731254721Semaste NamedDecl *var_decl = context.AddVarDecl(parser_clang_type); 1732254721Semaste 1733254721Semaste ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), 1734254721Semaste m_parser_vars->m_target_info.byte_order, 1735254721Semaste m_parser_vars->m_target_info.address_byte_size)); 1736254721Semaste assert (entity.get()); 1737254721Semaste 1738254721Semaste std::string decl_name(context.m_decl_name.getAsString()); 1739254721Semaste entity->SetName (ConstString (decl_name.c_str())); 1740254721Semaste entity->SetRegisterInfo (reg_info); 1741254721Semaste entity->EnableParserVars(GetParserID()); 1742254721Semaste ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID()); 1743254721Semaste parser_vars->m_parser_type = parser_clang_type; 1744254721Semaste parser_vars->m_named_decl = var_decl; 1745254721Semaste parser_vars->m_llvm_value = NULL; 1746254721Semaste parser_vars->m_lldb_value.Clear(); 1747254721Semaste entity->m_flags |= ClangExpressionVariable::EVBareRegister; 1748254721Semaste 1749254721Semaste if (log) 1750254721Semaste { 1751254721Semaste ASTDumper ast_dumper(var_decl); 1752254721Semaste log->Printf(" CEDM::FEVD[%d] Added register %s, returned %s", current_id, context.m_decl_name.getAsString().c_str(), ast_dumper.GetCString()); 1753254721Semaste } 1754254721Semaste} 1755254721Semaste 1756254721Semastevoid 1757254721SemasteClangExpressionDeclMap::AddOneFunction (NameSearchContext &context, 1758254721Semaste Function* function, 1759254721Semaste Symbol* symbol, 1760254721Semaste unsigned int current_id) 1761254721Semaste{ 1762254721Semaste assert (m_parser_vars.get()); 1763254721Semaste 1764254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1765254721Semaste 1766254721Semaste NamedDecl *function_decl = NULL; 1767254721Semaste const Address *fun_address = NULL; 1768254721Semaste ClangASTType function_clang_type; 1769254721Semaste 1770254721Semaste bool is_indirect_function = false; 1771254721Semaste 1772254721Semaste if (function) 1773254721Semaste { 1774254721Semaste Type *function_type = function->GetType(); 1775254721Semaste 1776254721Semaste if (!function_type) 1777254721Semaste { 1778254721Semaste if (log) 1779254721Semaste log->PutCString(" Skipped a function because it has no type"); 1780254721Semaste return; 1781254721Semaste } 1782254721Semaste 1783254721Semaste function_clang_type = function_type->GetClangFullType(); 1784254721Semaste 1785254721Semaste if (!function_clang_type) 1786254721Semaste { 1787254721Semaste if (log) 1788254721Semaste log->PutCString(" Skipped a function because it has no Clang type"); 1789254721Semaste return; 1790254721Semaste } 1791254721Semaste 1792254721Semaste fun_address = &function->GetAddressRange().GetBaseAddress(); 1793254721Semaste 1794254721Semaste ClangASTType copied_function_type = GuardedCopyType(function_clang_type); 1795254721Semaste if (copied_function_type) 1796254721Semaste { 1797254721Semaste function_decl = context.AddFunDecl(copied_function_type); 1798254721Semaste 1799254721Semaste if (!function_decl) 1800254721Semaste { 1801254721Semaste if (log) 1802254721Semaste { 1803254721Semaste log->Printf (" Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}", 1804254721Semaste function_type->GetName().GetCString(), 1805254721Semaste function_type->GetID()); 1806254721Semaste } 1807254721Semaste 1808254721Semaste return; 1809254721Semaste } 1810254721Semaste } 1811254721Semaste else 1812254721Semaste { 1813254721Semaste // We failed to copy the type we found 1814254721Semaste if (log) 1815254721Semaste { 1816254721Semaste log->Printf (" Failed to import the function type '%s' {0x%8.8" PRIx64 "} into the expression parser AST contenxt", 1817254721Semaste function_type->GetName().GetCString(), 1818254721Semaste function_type->GetID()); 1819254721Semaste } 1820254721Semaste 1821254721Semaste return; 1822254721Semaste } 1823254721Semaste } 1824254721Semaste else if (symbol) 1825254721Semaste { 1826254721Semaste fun_address = &symbol->GetAddress(); 1827254721Semaste function_decl = context.AddGenericFunDecl(); 1828254721Semaste is_indirect_function = symbol->IsIndirect(); 1829254721Semaste } 1830254721Semaste else 1831254721Semaste { 1832254721Semaste if (log) 1833254721Semaste log->PutCString(" AddOneFunction called with no function and no symbol"); 1834254721Semaste return; 1835254721Semaste } 1836254721Semaste 1837254721Semaste Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 1838254721Semaste 1839254721Semaste lldb::addr_t load_addr = fun_address->GetCallableLoadAddress(target, is_indirect_function); 1840254721Semaste 1841254721Semaste ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (), 1842254721Semaste m_parser_vars->m_target_info.byte_order, 1843254721Semaste m_parser_vars->m_target_info.address_byte_size)); 1844254721Semaste assert (entity.get()); 1845254721Semaste 1846254721Semaste std::string decl_name(context.m_decl_name.getAsString()); 1847254721Semaste entity->SetName(ConstString(decl_name.c_str())); 1848254721Semaste entity->SetClangType (function_clang_type); 1849254721Semaste entity->EnableParserVars(GetParserID()); 1850254721Semaste 1851254721Semaste ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID()); 1852254721Semaste 1853254721Semaste if (load_addr != LLDB_INVALID_ADDRESS) 1854254721Semaste { 1855254721Semaste parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress); 1856254721Semaste parser_vars->m_lldb_value.GetScalar() = load_addr; 1857254721Semaste } 1858254721Semaste else 1859254721Semaste { 1860254721Semaste // We have to try finding a file address. 1861254721Semaste 1862254721Semaste lldb::addr_t file_addr = fun_address->GetFileAddress(); 1863254721Semaste 1864254721Semaste parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress); 1865254721Semaste parser_vars->m_lldb_value.GetScalar() = file_addr; 1866254721Semaste } 1867254721Semaste 1868254721Semaste 1869254721Semaste parser_vars->m_named_decl = function_decl; 1870254721Semaste parser_vars->m_llvm_value = NULL; 1871254721Semaste 1872254721Semaste if (log) 1873254721Semaste { 1874254721Semaste ASTDumper ast_dumper(function_decl); 1875254721Semaste 1876254721Semaste StreamString ss; 1877254721Semaste 1878254721Semaste fun_address->Dump(&ss, m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), Address::DumpStyleResolvedDescription); 1879254721Semaste 1880254721Semaste log->Printf(" CEDM::FEVD[%u] Found %s function %s (description %s), returned %s", 1881254721Semaste current_id, 1882254721Semaste (function ? "specific" : "generic"), 1883254721Semaste decl_name.c_str(), 1884254721Semaste ss.GetData(), 1885254721Semaste ast_dumper.GetCString()); 1886254721Semaste } 1887254721Semaste} 1888254721Semaste 1889254721SemasteTypeFromParser 1890254721SemasteClangExpressionDeclMap::CopyClassType(TypeFromUser &ut, 1891254721Semaste unsigned int current_id) 1892254721Semaste{ 1893254721Semaste ClangASTType copied_clang_type = GuardedCopyType(ut); 1894254721Semaste 1895254721Semaste if (!copied_clang_type) 1896254721Semaste { 1897254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1898254721Semaste 1899254721Semaste if (log) 1900254721Semaste log->Printf("ClangExpressionDeclMap::CopyClassType - Couldn't import the type"); 1901254721Semaste 1902254721Semaste return TypeFromParser(); 1903254721Semaste } 1904254721Semaste 1905254721Semaste if (copied_clang_type.IsAggregateType() && copied_clang_type.GetCompleteType ()) 1906254721Semaste { 1907254721Semaste ClangASTType void_clang_type = ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid); 1908254721Semaste ClangASTType void_ptr_clang_type = void_clang_type.GetPointerType(); 1909254721Semaste 1910254721Semaste ClangASTType method_type = ClangASTContext::CreateFunctionType (m_ast_context, 1911254721Semaste void_clang_type, 1912254721Semaste &void_ptr_clang_type, 1913254721Semaste 1, 1914254721Semaste false, 1915254721Semaste copied_clang_type.GetTypeQualifiers()); 1916254721Semaste 1917254721Semaste const bool is_virtual = false; 1918254721Semaste const bool is_static = false; 1919254721Semaste const bool is_inline = false; 1920254721Semaste const bool is_explicit = false; 1921254721Semaste const bool is_attr_used = true; 1922254721Semaste const bool is_artificial = false; 1923254721Semaste 1924254721Semaste copied_clang_type.AddMethodToCXXRecordType ("$__lldb_expr", 1925254721Semaste method_type, 1926254721Semaste lldb::eAccessPublic, 1927254721Semaste is_virtual, 1928254721Semaste is_static, 1929254721Semaste is_inline, 1930254721Semaste is_explicit, 1931254721Semaste is_attr_used, 1932254721Semaste is_artificial); 1933254721Semaste } 1934254721Semaste 1935254721Semaste return TypeFromParser(copied_clang_type); 1936254721Semaste} 1937254721Semaste 1938254721Semastevoid 1939254721SemasteClangExpressionDeclMap::AddOneType(NameSearchContext &context, 1940254721Semaste TypeFromUser &ut, 1941254721Semaste unsigned int current_id) 1942254721Semaste{ 1943254721Semaste ClangASTType copied_clang_type = GuardedCopyType(ut); 1944254721Semaste 1945254721Semaste if (!copied_clang_type) 1946254721Semaste { 1947254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1948254721Semaste 1949254721Semaste if (log) 1950254721Semaste log->Printf("ClangExpressionDeclMap::AddOneType - Couldn't import the type"); 1951254721Semaste 1952254721Semaste return; 1953254721Semaste } 1954254721Semaste 1955254721Semaste context.AddTypeDecl(copied_clang_type); 1956254721Semaste} 1957