1 //===-- ClangExpressionDeclMap.cpp -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Expression/ClangExpressionDeclMap.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/DeclarationName.h"
13 #include "clang/AST/Decl.h"
14 #include "lldb/lldb-private.h"
15 #include "lldb/Core/Address.h"
16 #include "lldb/Core/Error.h"
17 #include "lldb/Core/Log.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/ASTDumper.h"
24 #include "lldb/Expression/ClangASTSource.h"
25 #include "lldb/Expression/ClangPersistentVariables.h"
26 #include "lldb/Expression/Materializer.h"
27 #include "lldb/Host/Endian.h"
28 #include "lldb/Symbol/ClangASTContext.h"
29 #include "lldb/Symbol/ClangNamespaceDecl.h"
30 #include "lldb/Symbol/CompileUnit.h"
31 #include "lldb/Symbol/Function.h"
32 #include "lldb/Symbol/ObjectFile.h"
33 #include "lldb/Symbol/SymbolContext.h"
34 #include "lldb/Symbol/SymbolVendor.h"
35 #include "lldb/Symbol/Type.h"
36 #include "lldb/Symbol/TypeList.h"
37 #include "lldb/Symbol/Variable.h"
38 #include "lldb/Symbol/VariableList.h"
39 #include "lldb/Target/CPPLanguageRuntime.h"
40 #include "lldb/Target/ExecutionContext.h"
41 #include "lldb/Target/ObjCLanguageRuntime.h"
42 #include "lldb/Target/Process.h"
43 #include "lldb/Target/RegisterContext.h"
44 #include "lldb/Target/StackFrame.h"
45 #include "lldb/Target/Target.h"
46 #include "lldb/Target/Thread.h"
49 using namespace lldb_private;
50 using namespace clang;
52 ClangExpressionDeclMap::ClangExpressionDeclMap (bool keep_result_in_memory, ExecutionContext &exe_ctx) :
53 ClangASTSource (exe_ctx.GetTargetSP()),
56 m_keep_result_in_memory (keep_result_in_memory),
63 ClangExpressionDeclMap::~ClangExpressionDeclMap()
65 // Note: The model is now that the parser's AST context and all associated
66 // data does not vanish until the expression has been executed. This means
67 // that valuable lookup data (like namespaces) doesn't vanish, but
74 ClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx,
75 Materializer *materializer)
77 ClangASTMetrics::ClearLocalCounters();
80 m_parser_vars->m_exe_ctx = exe_ctx;
82 Target *target = exe_ctx.GetTargetPtr();
83 if (exe_ctx.GetFramePtr())
84 m_parser_vars->m_sym_ctx = exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything);
85 else if (exe_ctx.GetThreadPtr() && exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0))
86 m_parser_vars->m_sym_ctx = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext(lldb::eSymbolContextEverything);
87 else if (exe_ctx.GetProcessPtr())
89 m_parser_vars->m_sym_ctx.Clear(true);
90 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
94 m_parser_vars->m_sym_ctx.Clear(true);
95 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
100 m_parser_vars->m_persistent_vars = &target->GetPersistentVariables();
102 if (!target->GetScratchClangASTContext())
106 m_parser_vars->m_target_info = GetTargetInfo();
107 m_parser_vars->m_materializer = materializer;
113 ClangExpressionDeclMap::DidParse()
115 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
118 ClangASTMetrics::DumpCounters(log);
120 if (m_parser_vars.get())
122 for (size_t entity_index = 0, num_entities = m_found_entities.GetSize();
123 entity_index < num_entities;
126 ClangExpressionVariableSP var_sp(m_found_entities.GetVariableAtIndex(entity_index));
128 var_sp->DisableParserVars(GetParserID());
131 for (size_t pvar_index = 0, num_pvars = m_parser_vars->m_persistent_vars->GetSize();
132 pvar_index < num_pvars;
135 ClangExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index));
137 pvar_sp->DisableParserVars(GetParserID());
144 // Interface for IRForTarget
146 ClangExpressionDeclMap::TargetInfo
147 ClangExpressionDeclMap::GetTargetInfo()
149 assert (m_parser_vars.get());
153 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
155 Process *process = exe_ctx.GetProcessPtr();
158 ret.byte_order = process->GetByteOrder();
159 ret.address_byte_size = process->GetAddressByteSize();
163 Target *target = exe_ctx.GetTargetPtr();
166 ret.byte_order = target->GetArchitecture().GetByteOrder();
167 ret.address_byte_size = target->GetArchitecture().GetAddressByteSize();
175 ClangExpressionDeclMap::AddPersistentVariable
177 const NamedDecl *decl,
178 const ConstString &name,
179 TypeFromParser parser_type,
184 assert (m_parser_vars.get());
186 if (m_parser_vars->m_materializer && is_result)
190 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
191 Target *target = exe_ctx.GetTargetPtr();
195 ASTContext *context(target->GetScratchClangASTContext()->getASTContext());
197 TypeFromUser user_type(m_ast_importer->DeportType(context,
198 parser_type.GetASTContext(),
199 parser_type.GetOpaqueQualType()),
202 uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(user_type, is_lvalue, m_keep_result_in_memory, err);
204 ClangExpressionVariableSP var_sp = m_found_entities.CreateVariable(exe_ctx.GetBestExecutionContextScope(),
207 m_parser_vars->m_target_info.byte_order,
208 m_parser_vars->m_target_info.address_byte_size);
213 var_sp->EnableParserVars(GetParserID());
215 ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
217 parser_vars->m_named_decl = decl;
218 parser_vars->m_parser_type = parser_type;
220 var_sp->EnableJITVars(GetParserID());
222 ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID());
224 jit_vars->m_offset = offset;
229 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
230 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
231 Target *target = exe_ctx.GetTargetPtr();
235 ASTContext *context(target->GetScratchClangASTContext()->getASTContext());
237 TypeFromUser user_type(m_ast_importer->DeportType(context,
238 parser_type.GetASTContext(),
239 parser_type.GetOpaqueQualType()),
242 if (!user_type.GetOpaqueQualType())
245 log->Printf("Persistent variable's type wasn't copied successfully");
249 if (!m_parser_vars->m_target_info.IsValid())
252 ClangExpressionVariableSP var_sp = m_parser_vars->m_persistent_vars->CreatePersistentVariable (exe_ctx.GetBestExecutionContextScope (),
255 m_parser_vars->m_target_info.byte_order,
256 m_parser_vars->m_target_info.address_byte_size);
261 var_sp->m_frozen_sp->SetHasCompleteType();
264 var_sp->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry;
266 var_sp->m_flags |= ClangExpressionVariable::EVKeepInTarget; // explicitly-declared persistent variables should persist
270 var_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
274 var_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
275 var_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
278 if (m_keep_result_in_memory)
280 var_sp->m_flags |= ClangExpressionVariable::EVKeepInTarget;
284 log->Printf("Created persistent variable with flags 0x%hx", var_sp->m_flags);
286 var_sp->EnableParserVars(GetParserID());
288 ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
290 parser_vars->m_named_decl = decl;
291 parser_vars->m_parser_type = parser_type;
297 ClangExpressionDeclMap::AddValueToStruct
299 const NamedDecl *decl,
300 const ConstString &name,
303 lldb::offset_t alignment
306 assert (m_struct_vars.get());
307 assert (m_parser_vars.get());
309 bool is_persistent_variable = false;
311 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
313 m_struct_vars->m_struct_laid_out = false;
315 if (m_struct_members.GetVariable(decl, GetParserID()))
318 ClangExpressionVariableSP var_sp (m_found_entities.GetVariable(decl, GetParserID()));
322 var_sp = m_parser_vars->m_persistent_vars->GetVariable(decl, GetParserID());
323 is_persistent_variable = true;
330 log->Printf("Adding value for (NamedDecl*)%p [%s - %s] to the structure",
331 static_cast<const void*>(decl), name.GetCString(),
332 var_sp->GetName().GetCString());
334 // We know entity->m_parser_vars is valid because we used a parser variable
337 ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
339 parser_vars->m_llvm_value = value;
341 if (ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID()))
343 // We already laid this out; do not touch
346 log->Printf("Already placed at 0x%llx", (unsigned long long)jit_vars->m_offset);
349 var_sp->EnableJITVars(GetParserID());
351 ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID());
353 jit_vars->m_alignment = alignment;
354 jit_vars->m_size = size;
356 m_struct_members.AddVariable(var_sp);
358 if (m_parser_vars->m_materializer)
364 if (is_persistent_variable)
366 offset = m_parser_vars->m_materializer->AddPersistentVariable(var_sp, err);
370 if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym)
371 offset = m_parser_vars->m_materializer->AddSymbol(*sym, err);
372 else if (const RegisterInfo *reg_info = var_sp->GetRegisterInfo())
373 offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err);
374 else if (parser_vars->m_lldb_var)
375 offset = m_parser_vars->m_materializer->AddVariable(parser_vars->m_lldb_var, err);
382 log->Printf("Placed at 0x%llx", (unsigned long long)offset);
384 jit_vars->m_offset = offset; // TODO DoStructLayout() should not change this.
391 ClangExpressionDeclMap::DoStructLayout ()
393 assert (m_struct_vars.get());
395 if (m_struct_vars->m_struct_laid_out)
398 if (!m_parser_vars->m_materializer)
401 m_struct_vars->m_struct_alignment = m_parser_vars->m_materializer->GetStructAlignment();
402 m_struct_vars->m_struct_size = m_parser_vars->m_materializer->GetStructByteSize();
403 m_struct_vars->m_struct_laid_out = true;
407 bool ClangExpressionDeclMap::GetStructInfo
409 uint32_t &num_elements,
411 lldb::offset_t &alignment
414 assert (m_struct_vars.get());
416 if (!m_struct_vars->m_struct_laid_out)
419 num_elements = m_struct_members.GetSize();
420 size = m_struct_vars->m_struct_size;
421 alignment = m_struct_vars->m_struct_alignment;
427 ClangExpressionDeclMap::GetStructElement
429 const NamedDecl *&decl,
431 lldb::offset_t &offset,
436 assert (m_struct_vars.get());
438 if (!m_struct_vars->m_struct_laid_out)
441 if (index >= m_struct_members.GetSize())
444 ClangExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index));
449 ClangExpressionVariable::ParserVars *parser_vars = member_sp->GetParserVars(GetParserID());
450 ClangExpressionVariable::JITVars *jit_vars = member_sp->GetJITVars(GetParserID());
454 !member_sp->GetValueObject())
457 decl = parser_vars->m_named_decl;
458 value = parser_vars->m_llvm_value;
459 offset = jit_vars->m_offset;
460 name = member_sp->GetName();
466 ClangExpressionDeclMap::GetFunctionInfo
468 const NamedDecl *decl,
472 ClangExpressionVariableSP entity_sp(m_found_entities.GetVariable(decl, GetParserID()));
477 // We know m_parser_vars is valid since we searched for the variable by
480 ClangExpressionVariable::ParserVars *parser_vars = entity_sp->GetParserVars(GetParserID());
482 ptr = parser_vars->m_lldb_value.GetScalar().ULongLong();
488 FindCodeSymbolInContext
490 const ConstString &name,
491 SymbolContext &sym_ctx,
492 SymbolContextList &sc_list
495 SymbolContextList temp_sc_list;
496 if (sym_ctx.module_sp)
497 sym_ctx.module_sp->FindSymbolsWithNameAndType(name, eSymbolTypeAny, temp_sc_list);
499 if (!sc_list.GetSize() && sym_ctx.target_sp)
500 sym_ctx.target_sp->GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, temp_sc_list);
502 unsigned temp_sc_list_size = temp_sc_list.GetSize();
503 for (unsigned i = 0; i < temp_sc_list_size; i++)
505 SymbolContext sym_ctx;
506 temp_sc_list.GetContextAtIndex(i, sym_ctx);
509 switch (sym_ctx.symbol->GetType())
511 case eSymbolTypeCode:
512 case eSymbolTypeResolver:
513 case eSymbolTypeReExported:
514 sc_list.Append(sym_ctx);
525 ClangExpressionDeclMap::GetFunctionAddress
527 const ConstString &name,
531 assert (m_parser_vars.get());
533 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
534 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
535 Target *target = exe_ctx.GetTargetPtr();
536 // Back out in all cases where we're not fully initialized
539 if (!m_parser_vars->m_sym_ctx.target_sp)
542 SymbolContextList sc_list;
544 FindCodeSymbolInContext(name, m_parser_vars->m_sym_ctx, sc_list);
546 uint32_t sc_list_size = sc_list.GetSize();
548 if (sc_list_size == 0)
550 // We occasionally get debug information in which a const function is reported
551 // as non-const, so the mangled name is wrong. This is a hack to compensate.
553 if (!strncmp(name.GetCString(), "_ZN", 3) &&
554 strncmp(name.GetCString(), "_ZNK", 4))
556 std::string fixed_scratch("_ZNK");
557 fixed_scratch.append(name.GetCString() + 3);
558 ConstString fixed_name(fixed_scratch.c_str());
561 log->Printf("Failed to find symbols given non-const name %s; trying %s", name.GetCString(), fixed_name.GetCString());
563 FindCodeSymbolInContext(fixed_name, m_parser_vars->m_sym_ctx, sc_list);
564 sc_list_size = sc_list.GetSize();
568 if (sc_list_size == 0)
570 // Sometimes we get a mangled name for a global function that actually should be "extern C."
571 // This is a hack to compensate.
573 const bool is_mangled = true;
574 Mangled mangled(name, is_mangled);
576 CPPLanguageRuntime::MethodName method_name(mangled.GetDemangledName());
578 llvm::StringRef basename = method_name.GetBasename();
580 if (!basename.empty())
582 FindCodeSymbolInContext(ConstString(basename), m_parser_vars->m_sym_ctx, sc_list);
583 sc_list_size = sc_list.GetSize();
587 for (uint32_t i=0; i<sc_list_size; ++i)
589 SymbolContext sym_ctx;
590 sc_list.GetContextAtIndex(i, sym_ctx);
592 lldb::addr_t callable_load_addr = LLDB_INVALID_ADDRESS;
594 if (sym_ctx.function)
596 const Address func_so_addr = sym_ctx.function->GetAddressRange().GetBaseAddress();
597 if (func_so_addr.IsValid())
599 callable_load_addr = func_so_addr.GetCallableLoadAddress(target, false);
602 else if (sym_ctx.symbol)
604 callable_load_addr = sym_ctx.symbol->ResolveCallableAddress(*target);
607 if (callable_load_addr != LLDB_INVALID_ADDRESS)
609 func_addr = callable_load_addr;
617 ClangExpressionDeclMap::GetSymbolAddress (Target &target,
619 const ConstString &name,
620 lldb::SymbolType symbol_type,
621 lldb_private::Module *module)
623 SymbolContextList sc_list;
626 module->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
628 target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list);
630 const uint32_t num_matches = sc_list.GetSize();
631 addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
633 for (uint32_t i=0; i<num_matches && (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS); i++)
635 SymbolContext sym_ctx;
636 sc_list.GetContextAtIndex(i, sym_ctx);
638 const Address *sym_address = &sym_ctx.symbol->GetAddress();
640 if (!sym_address || !sym_address->IsValid())
645 switch (sym_ctx.symbol->GetType())
647 case eSymbolTypeCode:
648 case eSymbolTypeTrampoline:
649 symbol_load_addr = sym_address->GetCallableLoadAddress (&target);
652 case eSymbolTypeResolver:
653 symbol_load_addr = sym_address->GetCallableLoadAddress (&target, true);
656 case eSymbolTypeReExported:
658 ConstString reexport_name = sym_ctx.symbol->GetReExportedSymbolName();
661 ModuleSP reexport_module_sp;
662 ModuleSpec reexport_module_spec;
663 reexport_module_spec.GetPlatformFileSpec() = sym_ctx.symbol->GetReExportedSymbolSharedLibrary();
664 if (reexport_module_spec.GetPlatformFileSpec())
666 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
667 if (!reexport_module_sp)
669 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
670 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
673 symbol_load_addr = GetSymbolAddress(target, process, sym_ctx.symbol->GetReExportedSymbolName(), symbol_type, reexport_module_sp.get());
678 case eSymbolTypeData:
679 case eSymbolTypeRuntime:
680 case eSymbolTypeVariable:
681 case eSymbolTypeLocal:
682 case eSymbolTypeParam:
683 case eSymbolTypeInvalid:
684 case eSymbolTypeAbsolute:
685 case eSymbolTypeException:
686 case eSymbolTypeSourceFile:
687 case eSymbolTypeHeaderFile:
688 case eSymbolTypeObjectFile:
689 case eSymbolTypeCommonBlock:
690 case eSymbolTypeBlock:
691 case eSymbolTypeVariableType:
692 case eSymbolTypeLineEntry:
693 case eSymbolTypeLineHeader:
694 case eSymbolTypeScopeBegin:
695 case eSymbolTypeScopeEnd:
696 case eSymbolTypeAdditional:
697 case eSymbolTypeCompiler:
698 case eSymbolTypeInstrumentation:
699 case eSymbolTypeUndefined:
700 case eSymbolTypeObjCClass:
701 case eSymbolTypeObjCMetaClass:
702 case eSymbolTypeObjCIVar:
703 symbol_load_addr = sym_address->GetLoadAddress (&target);
709 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process)
711 ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime();
715 symbol_load_addr = runtime->LookupRuntimeSymbol(name);
719 return symbol_load_addr;
723 ClangExpressionDeclMap::GetSymbolAddress (const ConstString &name, lldb::SymbolType symbol_type)
725 assert (m_parser_vars.get());
727 if (!m_parser_vars->m_exe_ctx.GetTargetPtr())
730 return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(), m_parser_vars->m_exe_ctx.GetProcessPtr(), name, symbol_type);
734 ClangExpressionDeclMap::FindGlobalDataSymbol (Target &target,
735 const ConstString &name,
736 lldb_private::Module *module)
738 SymbolContextList sc_list;
741 module->FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
743 target.GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
745 const uint32_t matches = sc_list.GetSize();
746 for (uint32_t i=0; i<matches; ++i)
748 SymbolContext sym_ctx;
749 sc_list.GetContextAtIndex(i, sym_ctx);
752 const Symbol *symbol = sym_ctx.symbol;
753 const Address *sym_address = &symbol->GetAddress();
755 if (sym_address && sym_address->IsValid())
757 switch (symbol->GetType())
759 case eSymbolTypeData:
760 case eSymbolTypeRuntime:
761 case eSymbolTypeAbsolute:
762 case eSymbolTypeObjCClass:
763 case eSymbolTypeObjCMetaClass:
764 case eSymbolTypeObjCIVar:
765 if (symbol->GetDemangledNameIsSynthesized())
767 // If the demangled name was synthesized, then don't use it
768 // for expressions. Only let the symbol match if the mangled
769 // named matches for these symbols.
770 if (symbol->GetMangled().GetMangledName() != name)
775 case eSymbolTypeReExported:
777 ConstString reexport_name = symbol->GetReExportedSymbolName();
780 ModuleSP reexport_module_sp;
781 ModuleSpec reexport_module_spec;
782 reexport_module_spec.GetPlatformFileSpec() = symbol->GetReExportedSymbolSharedLibrary();
783 if (reexport_module_spec.GetPlatformFileSpec())
785 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
786 if (!reexport_module_sp)
788 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
789 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
792 return FindGlobalDataSymbol(target, symbol->GetReExportedSymbolName(), reexport_module_sp.get());
797 case eSymbolTypeCode: // We already lookup functions elsewhere
798 case eSymbolTypeVariable:
799 case eSymbolTypeLocal:
800 case eSymbolTypeParam:
801 case eSymbolTypeTrampoline:
802 case eSymbolTypeInvalid:
803 case eSymbolTypeException:
804 case eSymbolTypeSourceFile:
805 case eSymbolTypeHeaderFile:
806 case eSymbolTypeObjectFile:
807 case eSymbolTypeCommonBlock:
808 case eSymbolTypeBlock:
809 case eSymbolTypeVariableType:
810 case eSymbolTypeLineEntry:
811 case eSymbolTypeLineHeader:
812 case eSymbolTypeScopeBegin:
813 case eSymbolTypeScopeEnd:
814 case eSymbolTypeAdditional:
815 case eSymbolTypeCompiler:
816 case eSymbolTypeInstrumentation:
817 case eSymbolTypeUndefined:
818 case eSymbolTypeResolver:
829 ClangExpressionDeclMap::FindGlobalVariable
833 const ConstString &name,
834 ClangNamespaceDecl *namespace_decl,
840 if (module && namespace_decl)
841 module->FindGlobalVariables (name, namespace_decl, true, -1, vars);
843 target.GetImages().FindGlobalVariables(name, true, -1, vars);
849 for (size_t i = 0; i < vars.GetSize(); ++i)
851 VariableSP var_sp = vars.GetVariableAtIndex(i);
853 if (ClangASTContext::AreTypesSame(*type, var_sp->GetType()->GetClangFullType()))
859 return vars.GetVariableAtIndex(0);
866 // Interface for ClangASTSource
869 ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context)
871 assert (m_ast_context);
873 ClangASTMetrics::RegisterVisibleQuery();
875 const ConstString name(context.m_decl_name.getAsString().c_str());
877 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
879 if (GetImportInProgress())
881 if (log && log->GetVerbose())
882 log->Printf("Ignoring a query during an import");
886 static unsigned int invocation_id = 0;
887 unsigned int current_id = invocation_id++;
891 if (!context.m_decl_context)
892 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a NULL DeclContext", current_id, name.GetCString());
893 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
894 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in '%s'", current_id, name.GetCString(), context_named_decl->getNameAsString().c_str());
896 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a '%s'", current_id, name.GetCString(), context.m_decl_context->getDeclKindName());
899 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
901 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
903 if (log && log->GetVerbose())
904 log->Printf(" CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)",
905 current_id, static_cast<void*>(namespace_map.get()),
906 (int)namespace_map->size());
911 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
916 log->Printf(" CEDM::FEVD[%u] Searching namespace %s in module %s",
918 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
919 i->first->GetFileSpec().GetFilename().GetCString());
921 FindExternalVisibleDecls(context,
927 else if (isa<TranslationUnitDecl>(context.m_decl_context))
929 ClangNamespaceDecl namespace_decl;
932 log->Printf(" CEDM::FEVD[%u] Searching the root namespace", current_id);
934 FindExternalVisibleDecls(context,
940 if (!context.m_found.variable)
941 ClangASTSource::FindExternalVisibleDecls(context);
945 ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context,
946 lldb::ModuleSP module_sp,
947 ClangNamespaceDecl &namespace_decl,
948 unsigned int current_id)
950 assert (m_ast_context);
952 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
954 SymbolContextList sc_list;
956 const ConstString name(context.m_decl_name.getAsString().c_str());
958 const char *name_unique_cstr = name.GetCString();
960 if (name_unique_cstr == NULL)
963 static ConstString id_name("id");
964 static ConstString Class_name("Class");
966 if (name == id_name || name == Class_name)
969 // Only look for functions by name out in our symbols if the function
970 // doesn't start with our phony prefix of '$'
971 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
972 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
973 if (name_unique_cstr[0] == '$' && !namespace_decl)
975 static ConstString g_lldb_class_name ("$__lldb_class");
977 if (name == g_lldb_class_name)
979 // Clang is looking for the type of "this"
984 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
986 if (!sym_ctx.function)
989 // Get the block that defines the function
990 Block *function_block = sym_ctx.GetFunctionBlock();
995 clang::DeclContext *decl_context = function_block->GetClangDeclContext();
1000 clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context);
1004 clang::CXXRecordDecl *class_decl = method_decl->getParent();
1006 QualType class_qual_type(class_decl->getTypeForDecl(), 0);
1008 TypeFromUser class_user_type (class_qual_type.getAsOpaquePtr(),
1009 &class_decl->getASTContext());
1013 ASTDumper ast_dumper(class_qual_type);
1014 log->Printf(" CEDM::FEVD[%u] Adding type for $__lldb_class: %s", current_id, ast_dumper.GetCString());
1017 TypeFromParser class_type = CopyClassType(class_user_type, current_id);
1019 if (!class_type.IsValid())
1022 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(QualType::getFromOpaquePtr(class_type.GetOpaqueQualType()));
1024 if (!type_source_info)
1027 TypedefDecl *typedef_decl = TypedefDecl::Create(*m_ast_context,
1028 m_ast_context->getTranslationUnitDecl(),
1031 context.m_decl_name.getAsIdentifierInfo(),
1038 context.AddNamedDecl(typedef_decl);
1040 if (method_decl->isInstance())
1042 // self is a pointer to the object
1044 QualType class_pointer_type = method_decl->getASTContext().getPointerType(class_qual_type);
1046 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
1047 &method_decl->getASTContext());
1049 m_struct_vars->m_object_pointer_type = self_user_type;
1054 // This branch will get hit if we are executing code in the context of a function that
1055 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a
1056 // method of the class. In that case, just look up the "this" variable in the the current
1057 // scope and use its type.
1058 // FIXME: This code is formally correct, but clang doesn't currently emit DW_AT_object_pointer
1059 // for C++ so it hasn't actually been tested.
1061 VariableList *vars = frame->GetVariableList(false);
1063 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
1066 this_var->IsInScope(frame) &&
1067 this_var->LocationIsValidForFrame (frame))
1069 Type *this_type = this_var->GetType();
1074 ClangASTType pointee_type = this_type->GetClangForwardType().GetPointeeType();
1076 if (pointee_type.IsValid())
1080 ASTDumper ast_dumper(this_type->GetClangFullType());
1081 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1084 TypeFromUser class_user_type(pointee_type);
1085 AddOneType(context, class_user_type, current_id);
1088 TypeFromUser this_user_type(this_type->GetClangFullType());
1089 m_struct_vars->m_object_pointer_type = this_user_type;
1098 static ConstString g_lldb_objc_class_name ("$__lldb_objc_class");
1099 if (name == g_lldb_objc_class_name)
1101 // Clang is looking for the type of "*self"
1106 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
1108 if (!sym_ctx.function)
1111 // Get the block that defines the function
1112 Block *function_block = sym_ctx.GetFunctionBlock();
1114 if (!function_block)
1117 clang::DeclContext *decl_context = function_block->GetClangDeclContext();
1122 clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context);
1126 ObjCInterfaceDecl* self_interface = method_decl->getClassInterface();
1128 if (!self_interface)
1131 const clang::Type *interface_type = self_interface->getTypeForDecl();
1133 if (!interface_type)
1134 return; // This is unlikely, but we have seen crashes where this occurred
1136 TypeFromUser class_user_type(QualType(interface_type, 0).getAsOpaquePtr(),
1137 &method_decl->getASTContext());
1141 ASTDumper ast_dumper(interface_type);
1142 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1145 AddOneType(context, class_user_type, current_id);
1147 if (method_decl->isInstanceMethod())
1149 // self is a pointer to the object
1151 QualType class_pointer_type = method_decl->getASTContext().getObjCObjectPointerType(QualType(interface_type, 0));
1153 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
1154 &method_decl->getASTContext());
1156 m_struct_vars->m_object_pointer_type = self_user_type;
1160 // self is a Class pointer
1161 QualType class_type = method_decl->getASTContext().getObjCClassType();
1163 TypeFromUser self_user_type(class_type.getAsOpaquePtr(),
1164 &method_decl->getASTContext());
1166 m_struct_vars->m_object_pointer_type = self_user_type;
1173 // This branch will get hit if we are executing code in the context of a function that
1174 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a
1175 // method of the class. In that case, just look up the "self" variable in the the current
1176 // scope and use its type.
1178 VariableList *vars = frame->GetVariableList(false);
1180 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
1183 self_var->IsInScope(frame) &&
1184 self_var->LocationIsValidForFrame (frame))
1186 Type *self_type = self_var->GetType();
1191 ClangASTType self_clang_type = self_type->GetClangFullType();
1193 if (self_clang_type.IsObjCClassType())
1197 else if (self_clang_type.IsObjCObjectPointerType())
1199 self_clang_type = self_clang_type.GetPointeeType();
1201 if (!self_clang_type)
1206 ASTDumper ast_dumper(self_type->GetClangFullType());
1207 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1210 TypeFromUser class_user_type (self_clang_type);
1212 AddOneType(context, class_user_type, current_id);
1214 TypeFromUser self_user_type(self_type->GetClangFullType());
1216 m_struct_vars->m_object_pointer_type = self_user_type;
1225 // any other $__lldb names should be weeded out now
1226 if (!::strncmp(name_unique_cstr, "$__lldb", sizeof("$__lldb") - 1))
1234 ClangASTContext *scratch_clang_ast_context = target->GetScratchClangASTContext();
1236 if (!scratch_clang_ast_context)
1239 ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
1241 if (!scratch_ast_context)
1244 TypeDecl *ptype_type_decl = m_parser_vars->m_persistent_vars->GetPersistentType(name);
1246 if (!ptype_type_decl)
1249 Decl *parser_ptype_decl = m_ast_importer->CopyDecl(m_ast_context, scratch_ast_context, ptype_type_decl);
1251 if (!parser_ptype_decl)
1254 TypeDecl *parser_ptype_type_decl = dyn_cast<TypeDecl>(parser_ptype_decl);
1256 if (!parser_ptype_type_decl)
1260 log->Printf(" CEDM::FEVD[%u] Found persistent type %s", current_id, name.GetCString());
1262 context.AddNamedDecl(parser_ptype_type_decl);
1265 ClangExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariable(name));
1269 AddOneVariable(context, pvar_sp, current_id);
1273 const char *reg_name(&name.GetCString()[1]);
1275 if (m_parser_vars->m_exe_ctx.GetRegisterContext())
1277 const RegisterInfo *reg_info(m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(reg_name));
1282 log->Printf(" CEDM::FEVD[%u] Found register %s", current_id, reg_info->name);
1284 AddOneRegister(context, reg_info, current_id);
1290 ValueObjectSP valobj;
1294 if (frame && !namespace_decl)
1296 valobj = frame->GetValueForVariableExpressionPath(name_unique_cstr,
1298 StackFrame::eExpressionPathOptionCheckPtrVsMember |
1299 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1300 StackFrame::eExpressionPathOptionsNoSyntheticChildren |
1301 StackFrame::eExpressionPathOptionsNoSyntheticArrayRange,
1305 // If we found a variable in scope, no need to pull up function names
1306 if (err.Success() && var)
1308 AddOneVariable(context, var, valobj, current_id);
1309 context.m_found.variable = true;
1316 var = FindGlobalVariable (*target,
1324 valobj = ValueObjectVariable::Create(target, var);
1325 AddOneVariable(context, var, valobj, current_id);
1326 context.m_found.variable = true;
1331 std::vector<clang::NamedDecl *> decls_from_modules;
1335 if (ClangModulesDeclVendor *decl_vendor = target->GetClangModulesDeclVendor())
1337 decl_vendor->FindDecls(name, false, UINT32_MAX, decls_from_modules);
1341 if (!context.m_found.variable)
1343 const bool include_inlines = false;
1344 const bool append = false;
1346 if (namespace_decl && module_sp)
1348 const bool include_symbols = false;
1350 module_sp->FindFunctions(name,
1352 eFunctionNameTypeBase,
1358 else if (target && !namespace_decl)
1360 const bool include_symbols = true;
1362 // TODO Fix FindFunctions so that it doesn't return
1363 // instance methods for eFunctionNameTypeBase.
1365 target->GetImages().FindFunctions(name,
1366 eFunctionNameTypeFull,
1373 if (sc_list.GetSize())
1375 Symbol *extern_symbol = NULL;
1376 Symbol *non_extern_symbol = NULL;
1378 for (uint32_t index = 0, num_indices = sc_list.GetSize();
1379 index < num_indices;
1382 SymbolContext sym_ctx;
1383 sc_list.GetContextAtIndex(index, sym_ctx);
1385 if (sym_ctx.function)
1387 clang::DeclContext *decl_ctx = sym_ctx.function->GetClangDeclContext();
1392 // Filter out class/instance methods.
1393 if (dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
1395 if (dyn_cast<clang::CXXMethodDecl>(decl_ctx))
1398 AddOneFunction(context, sym_ctx.function, NULL, current_id);
1399 context.m_found.function_with_type_info = true;
1400 context.m_found.function = true;
1402 else if (sym_ctx.symbol)
1404 if (sym_ctx.symbol->GetType() == eSymbolTypeReExported && target)
1406 sym_ctx.symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target);
1407 if (sym_ctx.symbol == NULL)
1411 if (sym_ctx.symbol->IsExternal())
1412 extern_symbol = sym_ctx.symbol;
1414 non_extern_symbol = sym_ctx.symbol;
1418 if (!context.m_found.function_with_type_info)
1420 for (clang::NamedDecl *decl : decls_from_modules)
1422 if (llvm::isa<clang::FunctionDecl>(decl))
1424 clang::NamedDecl *copied_decl = llvm::cast<FunctionDecl>(m_ast_importer->CopyDecl(m_ast_context, &decl->getASTContext(), decl));
1425 context.AddNamedDecl(copied_decl);
1426 context.m_found.function_with_type_info = true;
1431 if (!context.m_found.function_with_type_info)
1435 AddOneFunction (context, NULL, extern_symbol, current_id);
1436 context.m_found.function = true;
1438 else if (non_extern_symbol)
1440 AddOneFunction (context, NULL, non_extern_symbol, current_id);
1441 context.m_found.function = true;
1446 if (target && !context.m_found.variable && !namespace_decl)
1448 // We couldn't find a non-symbol variable for this. Now we'll hunt for a generic
1449 // data symbol, and -- if it is found -- treat it as a variable.
1451 const Symbol *data_symbol = FindGlobalDataSymbol(*target, name);
1455 std::string warning("got name from symbols: ");
1456 warning.append(name.AsCString());
1457 const unsigned diag_id = m_ast_context->getDiagnostics().getCustomDiagID(clang::DiagnosticsEngine::Level::Warning, "%0");
1458 m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str();
1459 AddOneGenericVariable(context, *data_symbol, current_id);
1460 context.m_found.variable = true;
1467 //static clang_type_t
1468 //MaybePromoteToBlockPointerType
1470 // ASTContext *ast_context,
1471 // clang_type_t candidate_type
1474 // if (!candidate_type)
1475 // return candidate_type;
1477 // QualType candidate_qual_type = QualType::getFromOpaquePtr(candidate_type);
1479 // const PointerType *candidate_pointer_type = dyn_cast<PointerType>(candidate_qual_type);
1481 // if (!candidate_pointer_type)
1482 // return candidate_type;
1484 // QualType pointee_qual_type = candidate_pointer_type->getPointeeType();
1486 // const RecordType *pointee_record_type = dyn_cast<RecordType>(pointee_qual_type);
1488 // if (!pointee_record_type)
1489 // return candidate_type;
1491 // RecordDecl *pointee_record_decl = pointee_record_type->getDecl();
1493 // if (!pointee_record_decl->isRecord())
1494 // return candidate_type;
1496 // if (!pointee_record_decl->getName().startswith(llvm::StringRef("__block_literal_")))
1497 // return candidate_type;
1499 // QualType generic_function_type = ast_context->getFunctionNoProtoType(ast_context->UnknownAnyTy);
1500 // QualType block_pointer_type = ast_context->getBlockPointerType(generic_function_type);
1502 // return block_pointer_type.getAsOpaquePtr();
1506 ClangExpressionDeclMap::GetVariableValue (VariableSP &var,
1507 lldb_private::Value &var_location,
1508 TypeFromUser *user_type,
1509 TypeFromParser *parser_type)
1511 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1513 Type *var_type = var->GetType();
1518 log->PutCString("Skipped a definition because it has no type");
1522 ClangASTType var_clang_type = var_type->GetClangFullType();
1524 if (!var_clang_type)
1527 log->PutCString("Skipped a definition because it has no Clang type");
1531 ASTContext *ast = var_type->GetClangASTContext().getASTContext();
1536 log->PutCString("There is no AST context for the current execution context");
1539 //var_clang_type = MaybePromoteToBlockPointerType (ast, var_clang_type);
1541 DWARFExpression &var_location_expr = var->LocationExpression();
1543 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1546 if (var->GetLocationIsConstantValueData())
1548 DataExtractor const_value_extractor;
1550 if (var_location_expr.GetExpressionData(const_value_extractor))
1552 var_location = Value(const_value_extractor.GetDataStart(), const_value_extractor.GetByteSize());
1553 var_location.SetValueType(Value::eValueTypeHostAddress);
1558 log->Printf("Error evaluating constant variable: %s", err.AsCString());
1563 ClangASTType type_to_use = GuardedCopyType(var_clang_type);
1568 log->Printf("Couldn't copy a variable's type into the parser's AST context");
1574 *parser_type = TypeFromParser(type_to_use);
1576 if (var_location.GetContextType() == Value::eContextTypeInvalid)
1577 var_location.SetClangType(type_to_use);
1579 if (var_location.GetValueType() == Value::eValueTypeFileAddress)
1581 SymbolContext var_sc;
1582 var->CalculateSymbolContext(&var_sc);
1584 if (!var_sc.module_sp)
1587 Address so_addr(var_location.GetScalar().ULongLong(), var_sc.module_sp->GetSectionList());
1589 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
1591 if (load_addr != LLDB_INVALID_ADDRESS)
1593 var_location.GetScalar() = load_addr;
1594 var_location.SetValueType(Value::eValueTypeLoadAddress);
1599 *user_type = TypeFromUser(var_clang_type);
1605 ClangExpressionDeclMap::AddOneVariable (NameSearchContext &context, VariableSP var, ValueObjectSP valobj, unsigned int current_id)
1607 assert (m_parser_vars.get());
1609 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1615 if (!GetVariableValue (var, var_location, &ut, &pt))
1618 clang::QualType parser_opaque_type = QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
1620 if (parser_opaque_type.isNull())
1623 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr())
1625 if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1626 CompleteType(tag_type->getDecl());
1627 if (const ObjCObjectPointerType *objc_object_ptr_type = dyn_cast<ObjCObjectPointerType>(parser_type))
1628 CompleteType(objc_object_ptr_type->getInterfaceDecl());
1632 bool is_reference = pt.IsReferenceType();
1634 NamedDecl *var_decl = NULL;
1636 var_decl = context.AddVarDecl(pt);
1638 var_decl = context.AddVarDecl(pt.GetLValueReferenceType());
1640 std::string decl_name(context.m_decl_name.getAsString());
1641 ConstString entity_name(decl_name.c_str());
1642 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (valobj));
1644 assert (entity.get());
1645 entity->EnableParserVars(GetParserID());
1646 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1647 parser_vars->m_parser_type = pt;
1648 parser_vars->m_named_decl = var_decl;
1649 parser_vars->m_llvm_value = NULL;
1650 parser_vars->m_lldb_value = var_location;
1651 parser_vars->m_lldb_var = var;
1654 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference;
1658 ASTDumper orig_dumper(ut.GetOpaqueQualType());
1659 ASTDumper ast_dumper(var_decl);
1660 log->Printf(" CEDM::FEVD[%u] Found variable %s, returned %s (original %s)", current_id, decl_name.c_str(), ast_dumper.GetCString(), orig_dumper.GetCString());
1665 ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
1666 ClangExpressionVariableSP &pvar_sp,
1667 unsigned int current_id)
1669 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1671 TypeFromUser user_type (pvar_sp->GetTypeFromUser());
1673 TypeFromParser parser_type (GuardedCopyType(user_type));
1675 if (!parser_type.GetOpaqueQualType())
1678 log->Printf(" CEDM::FEVD[%u] Couldn't import type for pvar %s", current_id, pvar_sp->GetName().GetCString());
1682 NamedDecl *var_decl = context.AddVarDecl(parser_type.GetLValueReferenceType());
1684 pvar_sp->EnableParserVars(GetParserID());
1685 ClangExpressionVariable::ParserVars *parser_vars = pvar_sp->GetParserVars(GetParserID());
1686 parser_vars->m_parser_type = parser_type;
1687 parser_vars->m_named_decl = var_decl;
1688 parser_vars->m_llvm_value = NULL;
1689 parser_vars->m_lldb_value.Clear();
1693 ASTDumper ast_dumper(var_decl);
1694 log->Printf(" CEDM::FEVD[%u] Added pvar %s, returned %s", current_id, pvar_sp->GetName().GetCString(), ast_dumper.GetCString());
1699 ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context,
1700 const Symbol &symbol,
1701 unsigned int current_id)
1703 assert(m_parser_vars.get());
1705 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1707 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1712 ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext();
1714 TypeFromUser user_type (ClangASTContext::GetBasicType(scratch_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType());
1715 TypeFromParser parser_type (ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType());
1716 NamedDecl *var_decl = context.AddVarDecl(parser_type);
1718 std::string decl_name(context.m_decl_name.getAsString());
1719 ConstString entity_name(decl_name.c_str());
1720 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (),
1723 m_parser_vars->m_target_info.byte_order,
1724 m_parser_vars->m_target_info.address_byte_size));
1725 assert (entity.get());
1727 entity->EnableParserVars(GetParserID());
1728 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1730 const Address &symbol_address = symbol.GetAddress();
1731 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target);
1733 //parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType());
1734 parser_vars->m_lldb_value.SetClangType(user_type);
1735 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr;
1736 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1738 parser_vars->m_parser_type = parser_type;
1739 parser_vars->m_named_decl = var_decl;
1740 parser_vars->m_llvm_value = NULL;
1741 parser_vars->m_lldb_sym = &symbol;
1745 ASTDumper ast_dumper(var_decl);
1747 log->Printf(" CEDM::FEVD[%u] Found variable %s, returned %s", current_id, decl_name.c_str(), ast_dumper.GetCString());
1752 ClangExpressionDeclMap::ResolveUnknownTypes()
1754 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1755 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1757 ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext();
1759 for (size_t index = 0, num_entities = m_found_entities.GetSize();
1760 index < num_entities;
1763 ClangExpressionVariableSP entity = m_found_entities.GetVariableAtIndex(index);
1765 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1767 if (entity->m_flags & ClangExpressionVariable::EVUnknownType)
1769 const NamedDecl *named_decl = parser_vars->m_named_decl;
1770 const VarDecl *var_decl = dyn_cast<VarDecl>(named_decl);
1775 log->Printf("Entity of unknown type does not have a VarDecl");
1781 ASTDumper ast_dumper(const_cast<VarDecl*>(var_decl));
1782 log->Printf("Variable of unknown type now has Decl %s", ast_dumper.GetCString());
1785 QualType var_type = var_decl->getType();
1786 TypeFromParser parser_type(var_type.getAsOpaquePtr(), &var_decl->getASTContext());
1788 lldb::clang_type_t copied_type = m_ast_importer->CopyType(scratch_ast_context, &var_decl->getASTContext(), var_type.getAsOpaquePtr());
1793 log->Printf("ClangExpressionDeclMap::ResolveUnknownType - Couldn't import the type for a variable");
1795 return (bool) lldb::ClangExpressionVariableSP();
1798 TypeFromUser user_type(copied_type, scratch_ast_context);
1800 // parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType());
1801 parser_vars->m_lldb_value.SetClangType(user_type);
1802 parser_vars->m_parser_type = parser_type;
1804 entity->SetClangType(user_type);
1806 entity->m_flags &= ~(ClangExpressionVariable::EVUnknownType);
1814 ClangExpressionDeclMap::AddOneRegister (NameSearchContext &context,
1815 const RegisterInfo *reg_info,
1816 unsigned int current_id)
1818 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1820 ClangASTType clang_type = ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (m_ast_context,
1822 reg_info->byte_size * 8);
1827 log->Printf(" Tried to add a type for %s, but couldn't get one", context.m_decl_name.getAsString().c_str());
1831 TypeFromParser parser_clang_type (clang_type);
1833 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type);
1835 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1836 m_parser_vars->m_target_info.byte_order,
1837 m_parser_vars->m_target_info.address_byte_size));
1838 assert (entity.get());
1840 std::string decl_name(context.m_decl_name.getAsString());
1841 entity->SetName (ConstString (decl_name.c_str()));
1842 entity->SetRegisterInfo (reg_info);
1843 entity->EnableParserVars(GetParserID());
1844 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1845 parser_vars->m_parser_type = parser_clang_type;
1846 parser_vars->m_named_decl = var_decl;
1847 parser_vars->m_llvm_value = NULL;
1848 parser_vars->m_lldb_value.Clear();
1849 entity->m_flags |= ClangExpressionVariable::EVBareRegister;
1853 ASTDumper ast_dumper(var_decl);
1854 log->Printf(" CEDM::FEVD[%d] Added register %s, returned %s", current_id, context.m_decl_name.getAsString().c_str(), ast_dumper.GetCString());
1859 ClangExpressionDeclMap::AddOneFunction (NameSearchContext &context,
1862 unsigned int current_id)
1864 assert (m_parser_vars.get());
1866 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1868 NamedDecl *function_decl = NULL;
1869 const Address *fun_address = NULL;
1870 ClangASTType function_clang_type;
1872 bool is_indirect_function = false;
1876 Type *function_type = function->GetType();
1881 log->PutCString(" Skipped a function because it has no type");
1885 function_clang_type = function_type->GetClangFullType();
1887 if (!function_clang_type)
1890 log->PutCString(" Skipped a function because it has no Clang type");
1894 fun_address = &function->GetAddressRange().GetBaseAddress();
1896 ClangASTType copied_function_type = GuardedCopyType(function_clang_type);
1897 if (copied_function_type)
1899 function_decl = context.AddFunDecl(copied_function_type);
1905 log->Printf (" Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}",
1906 function_type->GetName().GetCString(),
1907 function_type->GetID());
1915 // We failed to copy the type we found
1918 log->Printf (" Failed to import the function type '%s' {0x%8.8" PRIx64 "} into the expression parser AST contenxt",
1919 function_type->GetName().GetCString(),
1920 function_type->GetID());
1928 fun_address = &symbol->GetAddress();
1929 function_decl = context.AddGenericFunDecl();
1930 is_indirect_function = symbol->IsIndirect();
1935 log->PutCString(" AddOneFunction called with no function and no symbol");
1939 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1941 lldb::addr_t load_addr = fun_address->GetCallableLoadAddress(target, is_indirect_function);
1943 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (),
1944 m_parser_vars->m_target_info.byte_order,
1945 m_parser_vars->m_target_info.address_byte_size));
1946 assert (entity.get());
1948 std::string decl_name(context.m_decl_name.getAsString());
1949 entity->SetName(ConstString(decl_name.c_str()));
1950 entity->SetClangType (function_clang_type);
1951 entity->EnableParserVars(GetParserID());
1953 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1955 if (load_addr != LLDB_INVALID_ADDRESS)
1957 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1958 parser_vars->m_lldb_value.GetScalar() = load_addr;
1962 // We have to try finding a file address.
1964 lldb::addr_t file_addr = fun_address->GetFileAddress();
1966 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress);
1967 parser_vars->m_lldb_value.GetScalar() = file_addr;
1971 parser_vars->m_named_decl = function_decl;
1972 parser_vars->m_llvm_value = NULL;
1976 ASTDumper ast_dumper(function_decl);
1980 fun_address->Dump(&ss, m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), Address::DumpStyleResolvedDescription);
1982 log->Printf(" CEDM::FEVD[%u] Found %s function %s (description %s), returned %s",
1984 (function ? "specific" : "generic"),
1987 ast_dumper.GetCString());
1992 ClangExpressionDeclMap::CopyClassType(TypeFromUser &ut,
1993 unsigned int current_id)
1995 ClangASTType copied_clang_type = GuardedCopyType(ut);
1997 if (!copied_clang_type)
1999 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2002 log->Printf("ClangExpressionDeclMap::CopyClassType - Couldn't import the type");
2004 return TypeFromParser();
2007 if (copied_clang_type.IsAggregateType() && copied_clang_type.GetCompleteType ())
2009 ClangASTType void_clang_type = ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid);
2010 ClangASTType void_ptr_clang_type = void_clang_type.GetPointerType();
2012 ClangASTType method_type = ClangASTContext::CreateFunctionType (m_ast_context,
2014 &void_ptr_clang_type,
2017 copied_clang_type.GetTypeQualifiers());
2019 const bool is_virtual = false;
2020 const bool is_static = false;
2021 const bool is_inline = false;
2022 const bool is_explicit = false;
2023 const bool is_attr_used = true;
2024 const bool is_artificial = false;
2026 copied_clang_type.AddMethodToCXXRecordType ("$__lldb_expr",
2028 lldb::eAccessPublic,
2037 return TypeFromParser(copied_clang_type);
2041 ClangExpressionDeclMap::AddOneType(NameSearchContext &context,
2043 unsigned int current_id)
2045 ClangASTType copied_clang_type = GuardedCopyType(ut);
2047 if (!copied_clang_type)
2049 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2052 log->Printf("ClangExpressionDeclMap::AddOneType - Couldn't import the type");
2057 context.AddTypeDecl(copied_clang_type);