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"
14 // Other libraries and framework includes
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/DeclarationName.h"
18 #include "clang/AST/Decl.h"
19 #include "lldb/lldb-private.h"
20 #include "lldb/Core/Address.h"
21 #include "lldb/Core/Error.h"
22 #include "lldb/Core/Log.h"
23 #include "lldb/Core/Module.h"
24 #include "lldb/Core/ModuleSpec.h"
25 #include "lldb/Core/RegisterValue.h"
26 #include "lldb/Core/ValueObjectConstResult.h"
27 #include "lldb/Core/ValueObjectVariable.h"
28 #include "lldb/Expression/ASTDumper.h"
29 #include "lldb/Expression/ClangASTSource.h"
30 #include "lldb/Expression/ClangPersistentVariables.h"
31 #include "lldb/Expression/Materializer.h"
32 #include "lldb/Host/Endian.h"
33 #include "lldb/Symbol/ClangASTContext.h"
34 #include "lldb/Symbol/ClangNamespaceDecl.h"
35 #include "lldb/Symbol/CompileUnit.h"
36 #include "lldb/Symbol/Function.h"
37 #include "lldb/Symbol/ObjectFile.h"
38 #include "lldb/Symbol/SymbolContext.h"
39 #include "lldb/Symbol/SymbolVendor.h"
40 #include "lldb/Symbol/Type.h"
41 #include "lldb/Symbol/TypeList.h"
42 #include "lldb/Symbol/Variable.h"
43 #include "lldb/Symbol/VariableList.h"
44 #include "lldb/Target/ExecutionContext.h"
45 #include "lldb/Target/ObjCLanguageRuntime.h"
46 #include "lldb/Target/Process.h"
47 #include "lldb/Target/RegisterContext.h"
48 #include "lldb/Target/StackFrame.h"
49 #include "lldb/Target/Target.h"
50 #include "lldb/Target/Thread.h"
53 using namespace lldb_private;
54 using namespace clang;
56 ClangExpressionDeclMap::ClangExpressionDeclMap (bool keep_result_in_memory, ExecutionContext &exe_ctx) :
57 ClangASTSource (exe_ctx.GetTargetSP()),
60 m_keep_result_in_memory (keep_result_in_memory),
67 ClangExpressionDeclMap::~ClangExpressionDeclMap()
69 // Note: The model is now that the parser's AST context and all associated
70 // data does not vanish until the expression has been executed. This means
71 // that valuable lookup data (like namespaces) doesn't vanish, but
78 ClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx,
79 Materializer *materializer)
81 ClangASTMetrics::ClearLocalCounters();
84 m_parser_vars->m_exe_ctx = exe_ctx;
86 Target *target = exe_ctx.GetTargetPtr();
87 if (exe_ctx.GetFramePtr())
88 m_parser_vars->m_sym_ctx = exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything);
89 else if (exe_ctx.GetThreadPtr() && exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0))
90 m_parser_vars->m_sym_ctx = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext(lldb::eSymbolContextEverything);
91 else if (exe_ctx.GetProcessPtr())
93 m_parser_vars->m_sym_ctx.Clear(true);
94 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
98 m_parser_vars->m_sym_ctx.Clear(true);
99 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
104 m_parser_vars->m_persistent_vars = &target->GetPersistentVariables();
106 if (!target->GetScratchClangASTContext())
110 m_parser_vars->m_target_info = GetTargetInfo();
111 m_parser_vars->m_materializer = materializer;
117 ClangExpressionDeclMap::DidParse()
119 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
122 ClangASTMetrics::DumpCounters(log);
124 if (m_parser_vars.get())
126 for (size_t entity_index = 0, num_entities = m_found_entities.GetSize();
127 entity_index < num_entities;
130 ClangExpressionVariableSP var_sp(m_found_entities.GetVariableAtIndex(entity_index));
132 var_sp->DisableParserVars(GetParserID());
135 for (size_t pvar_index = 0, num_pvars = m_parser_vars->m_persistent_vars->GetSize();
136 pvar_index < num_pvars;
139 ClangExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index));
141 pvar_sp->DisableParserVars(GetParserID());
148 // Interface for IRForTarget
150 ClangExpressionDeclMap::TargetInfo
151 ClangExpressionDeclMap::GetTargetInfo()
153 assert (m_parser_vars.get());
157 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
159 Process *process = exe_ctx.GetProcessPtr();
162 ret.byte_order = process->GetByteOrder();
163 ret.address_byte_size = process->GetAddressByteSize();
167 Target *target = exe_ctx.GetTargetPtr();
170 ret.byte_order = target->GetArchitecture().GetByteOrder();
171 ret.address_byte_size = target->GetArchitecture().GetAddressByteSize();
179 ClangExpressionDeclMap::AddPersistentVariable
181 const NamedDecl *decl,
182 const ConstString &name,
183 TypeFromParser parser_type,
188 assert (m_parser_vars.get());
190 if (m_parser_vars->m_materializer && is_result)
194 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
195 Target *target = exe_ctx.GetTargetPtr();
199 ASTContext *context(target->GetScratchClangASTContext()->getASTContext());
201 TypeFromUser user_type(m_ast_importer->DeportType(context,
202 parser_type.GetASTContext(),
203 parser_type.GetOpaqueQualType()),
206 uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(user_type, is_lvalue, m_keep_result_in_memory, err);
208 ClangExpressionVariableSP var_sp = m_found_entities.CreateVariable(exe_ctx.GetBestExecutionContextScope(),
211 m_parser_vars->m_target_info.byte_order,
212 m_parser_vars->m_target_info.address_byte_size);
217 var_sp->EnableParserVars(GetParserID());
219 ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
221 parser_vars->m_named_decl = decl;
222 parser_vars->m_parser_type = parser_type;
224 var_sp->EnableJITVars(GetParserID());
226 ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID());
228 jit_vars->m_offset = offset;
233 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
234 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
235 Target *target = exe_ctx.GetTargetPtr();
239 ASTContext *context(target->GetScratchClangASTContext()->getASTContext());
241 TypeFromUser user_type(m_ast_importer->DeportType(context,
242 parser_type.GetASTContext(),
243 parser_type.GetOpaqueQualType()),
246 if (!user_type.GetOpaqueQualType())
249 log->Printf("Persistent variable's type wasn't copied successfully");
253 if (!m_parser_vars->m_target_info.IsValid())
256 ClangExpressionVariableSP var_sp = m_parser_vars->m_persistent_vars->CreatePersistentVariable (exe_ctx.GetBestExecutionContextScope (),
259 m_parser_vars->m_target_info.byte_order,
260 m_parser_vars->m_target_info.address_byte_size);
265 var_sp->m_frozen_sp->SetHasCompleteType();
268 var_sp->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry;
270 var_sp->m_flags |= ClangExpressionVariable::EVKeepInTarget; // explicitly-declared persistent variables should persist
274 var_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
278 var_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
279 var_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
282 if (m_keep_result_in_memory)
284 var_sp->m_flags |= ClangExpressionVariable::EVKeepInTarget;
288 log->Printf("Created persistent variable with flags 0x%hx", var_sp->m_flags);
290 var_sp->EnableParserVars(GetParserID());
292 ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
294 parser_vars->m_named_decl = decl;
295 parser_vars->m_parser_type = parser_type;
301 ClangExpressionDeclMap::AddValueToStruct
303 const NamedDecl *decl,
304 const ConstString &name,
310 assert (m_struct_vars.get());
311 assert (m_parser_vars.get());
313 bool is_persistent_variable = false;
315 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
317 m_struct_vars->m_struct_laid_out = false;
319 if (m_struct_members.GetVariable(decl, GetParserID()))
322 ClangExpressionVariableSP var_sp (m_found_entities.GetVariable(decl, GetParserID()));
326 var_sp = m_parser_vars->m_persistent_vars->GetVariable(decl, GetParserID());
327 is_persistent_variable = true;
334 log->Printf("Adding value for (NamedDecl*)%p [%s - %s] to the structure",
337 var_sp->GetName().GetCString());
339 // We know entity->m_parser_vars is valid because we used a parser variable
342 ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
344 parser_vars->m_llvm_value = value;
346 if (ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID()))
348 // We already laid this out; do not touch
351 log->Printf("Already placed at 0x%llx", (unsigned long long)jit_vars->m_offset);
354 var_sp->EnableJITVars(GetParserID());
356 ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID());
358 jit_vars->m_alignment = alignment;
359 jit_vars->m_size = size;
361 m_struct_members.AddVariable(var_sp);
363 if (m_parser_vars->m_materializer)
369 if (is_persistent_variable)
371 offset = m_parser_vars->m_materializer->AddPersistentVariable(var_sp, err);
375 if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym)
376 offset = m_parser_vars->m_materializer->AddSymbol(*sym, err);
377 else if (const RegisterInfo *reg_info = var_sp->GetRegisterInfo())
378 offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err);
379 else if (parser_vars->m_lldb_var)
380 offset = m_parser_vars->m_materializer->AddVariable(parser_vars->m_lldb_var, err);
387 log->Printf("Placed at 0x%llx", (unsigned long long)offset);
389 jit_vars->m_offset = offset; // TODO DoStructLayout() should not change this.
396 ClangExpressionDeclMap::DoStructLayout ()
398 assert (m_struct_vars.get());
400 if (m_struct_vars->m_struct_laid_out)
403 if (!m_parser_vars->m_materializer)
406 m_struct_vars->m_struct_alignment = m_parser_vars->m_materializer->GetStructAlignment();
407 m_struct_vars->m_struct_size = m_parser_vars->m_materializer->GetStructByteSize();
408 m_struct_vars->m_struct_laid_out = true;
412 bool ClangExpressionDeclMap::GetStructInfo
414 uint32_t &num_elements,
419 assert (m_struct_vars.get());
421 if (!m_struct_vars->m_struct_laid_out)
424 num_elements = m_struct_members.GetSize();
425 size = m_struct_vars->m_struct_size;
426 alignment = m_struct_vars->m_struct_alignment;
432 ClangExpressionDeclMap::GetStructElement
434 const NamedDecl *&decl,
441 assert (m_struct_vars.get());
443 if (!m_struct_vars->m_struct_laid_out)
446 if (index >= m_struct_members.GetSize())
449 ClangExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index));
454 ClangExpressionVariable::ParserVars *parser_vars = member_sp->GetParserVars(GetParserID());
455 ClangExpressionVariable::JITVars *jit_vars = member_sp->GetJITVars(GetParserID());
459 !member_sp->GetValueObject())
462 decl = parser_vars->m_named_decl;
463 value = parser_vars->m_llvm_value;
464 offset = jit_vars->m_offset;
465 name = member_sp->GetName();
471 ClangExpressionDeclMap::GetFunctionInfo
473 const NamedDecl *decl,
477 ClangExpressionVariableSP entity_sp(m_found_entities.GetVariable(decl, GetParserID()));
482 // We know m_parser_vars is valid since we searched for the variable by
485 ClangExpressionVariable::ParserVars *parser_vars = entity_sp->GetParserVars(GetParserID());
487 ptr = parser_vars->m_lldb_value.GetScalar().ULongLong();
493 FindCodeSymbolInContext
495 const ConstString &name,
496 SymbolContext &sym_ctx,
497 SymbolContextList &sc_list
500 SymbolContextList temp_sc_list;
501 if (sym_ctx.module_sp)
502 sym_ctx.module_sp->FindSymbolsWithNameAndType(name, eSymbolTypeAny, temp_sc_list);
504 if (!sc_list.GetSize() && sym_ctx.target_sp)
505 sym_ctx.target_sp->GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, temp_sc_list);
507 unsigned temp_sc_list_size = temp_sc_list.GetSize();
508 for (unsigned i = 0; i < temp_sc_list_size; i++)
510 SymbolContext sym_ctx;
511 temp_sc_list.GetContextAtIndex(i, sym_ctx);
514 switch (sym_ctx.symbol->GetType())
516 case eSymbolTypeCode:
517 case eSymbolTypeResolver:
518 case eSymbolTypeReExported:
519 sc_list.Append(sym_ctx);
530 ClangExpressionDeclMap::GetFunctionAddress
532 const ConstString &name,
536 assert (m_parser_vars.get());
538 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
539 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
540 Target *target = exe_ctx.GetTargetPtr();
541 // Back out in all cases where we're not fully initialized
544 if (!m_parser_vars->m_sym_ctx.target_sp)
547 SymbolContextList sc_list;
549 FindCodeSymbolInContext(name, m_parser_vars->m_sym_ctx, sc_list);
551 uint32_t sc_list_size = sc_list.GetSize();
552 if (sc_list_size == 0)
554 // We occasionally get debug information in which a const function is reported
555 // as non-const, so the mangled name is wrong. This is a hack to compensate.
557 if (!strncmp(name.GetCString(), "_ZN", 3) &&
558 strncmp(name.GetCString(), "_ZNK", 4))
560 std::string fixed_scratch("_ZNK");
561 fixed_scratch.append(name.GetCString() + 3);
562 ConstString fixed_name(fixed_scratch.c_str());
565 log->Printf("Failed to find symbols given non-const name %s; trying %s", name.GetCString(), fixed_name.GetCString());
567 FindCodeSymbolInContext(fixed_name, m_parser_vars->m_sym_ctx, sc_list);
568 sc_list_size = sc_list.GetSize();
572 for (uint32_t i=0; i<sc_list_size; ++i)
574 SymbolContext sym_ctx;
575 sc_list.GetContextAtIndex(i, sym_ctx);
577 const Address *func_so_addr = NULL;
578 bool is_indirect_function = false;
579 if (sym_ctx.function)
580 func_so_addr = &sym_ctx.function->GetAddressRange().GetBaseAddress();
581 else if (sym_ctx.symbol)
583 if (sym_ctx.symbol->GetType() == eSymbolTypeReExported)
585 Symbol *reexported_symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target);
586 if (reexported_symbol)
588 func_so_addr = &reexported_symbol->GetAddress();
589 is_indirect_function = reexported_symbol->IsIndirect();
594 func_so_addr = &sym_ctx.symbol->GetAddress();
595 is_indirect_function = sym_ctx.symbol->IsIndirect();
599 if (func_so_addr && func_so_addr->IsValid())
601 lldb::addr_t load_addr = func_so_addr->GetCallableLoadAddress (target, is_indirect_function);
603 if (load_addr != LLDB_INVALID_ADDRESS)
605 func_addr = load_addr;
614 ClangExpressionDeclMap::GetSymbolAddress (Target &target,
616 const ConstString &name,
617 lldb::SymbolType symbol_type,
618 lldb_private::Module *module)
620 SymbolContextList sc_list;
623 module->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
625 target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list);
627 const uint32_t num_matches = sc_list.GetSize();
628 addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
630 for (uint32_t i=0; i<num_matches && (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS); i++)
632 SymbolContext sym_ctx;
633 sc_list.GetContextAtIndex(i, sym_ctx);
635 const Address *sym_address = &sym_ctx.symbol->GetAddress();
637 if (!sym_address || !sym_address->IsValid())
642 switch (sym_ctx.symbol->GetType())
644 case eSymbolTypeCode:
645 case eSymbolTypeTrampoline:
646 symbol_load_addr = sym_address->GetCallableLoadAddress (&target);
649 case eSymbolTypeResolver:
650 symbol_load_addr = sym_address->GetCallableLoadAddress (&target, true);
653 case eSymbolTypeReExported:
655 ConstString reexport_name = sym_ctx.symbol->GetReExportedSymbolName();
658 ModuleSP reexport_module_sp;
659 ModuleSpec reexport_module_spec;
660 reexport_module_spec.GetPlatformFileSpec() = sym_ctx.symbol->GetReExportedSymbolSharedLibrary();
661 if (reexport_module_spec.GetPlatformFileSpec())
663 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
664 if (!reexport_module_sp)
666 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
667 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
670 symbol_load_addr = GetSymbolAddress(target, process, sym_ctx.symbol->GetReExportedSymbolName(), symbol_type, reexport_module_sp.get());
675 case eSymbolTypeData:
676 case eSymbolTypeRuntime:
677 case eSymbolTypeVariable:
678 case eSymbolTypeLocal:
679 case eSymbolTypeParam:
680 case eSymbolTypeInvalid:
681 case eSymbolTypeAbsolute:
682 case eSymbolTypeException:
683 case eSymbolTypeSourceFile:
684 case eSymbolTypeHeaderFile:
685 case eSymbolTypeObjectFile:
686 case eSymbolTypeCommonBlock:
687 case eSymbolTypeBlock:
688 case eSymbolTypeVariableType:
689 case eSymbolTypeLineEntry:
690 case eSymbolTypeLineHeader:
691 case eSymbolTypeScopeBegin:
692 case eSymbolTypeScopeEnd:
693 case eSymbolTypeAdditional:
694 case eSymbolTypeCompiler:
695 case eSymbolTypeInstrumentation:
696 case eSymbolTypeUndefined:
697 case eSymbolTypeObjCClass:
698 case eSymbolTypeObjCMetaClass:
699 case eSymbolTypeObjCIVar:
700 symbol_load_addr = sym_address->GetLoadAddress (&target);
706 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process)
708 ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime();
712 symbol_load_addr = runtime->LookupRuntimeSymbol(name);
716 return symbol_load_addr;
720 ClangExpressionDeclMap::GetSymbolAddress (const ConstString &name, lldb::SymbolType symbol_type)
722 assert (m_parser_vars.get());
724 if (!m_parser_vars->m_exe_ctx.GetTargetPtr())
727 return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(), m_parser_vars->m_exe_ctx.GetProcessPtr(), name, symbol_type);
731 ClangExpressionDeclMap::FindGlobalDataSymbol (Target &target,
732 const ConstString &name,
733 lldb_private::Module *module)
735 SymbolContextList sc_list;
738 module->FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
740 target.GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
742 const uint32_t matches = sc_list.GetSize();
743 for (uint32_t i=0; i<matches; ++i)
745 SymbolContext sym_ctx;
746 sc_list.GetContextAtIndex(i, sym_ctx);
749 const Symbol *symbol = sym_ctx.symbol;
750 const Address *sym_address = &symbol->GetAddress();
752 if (sym_address && sym_address->IsValid())
754 switch (symbol->GetType())
756 case eSymbolTypeData:
757 case eSymbolTypeRuntime:
758 case eSymbolTypeAbsolute:
759 case eSymbolTypeObjCClass:
760 case eSymbolTypeObjCMetaClass:
761 case eSymbolTypeObjCIVar:
762 if (symbol->GetDemangledNameIsSynthesized())
764 // If the demangled name was synthesized, then don't use it
765 // for expressions. Only let the symbol match if the mangled
766 // named matches for these symbols.
767 if (symbol->GetMangled().GetMangledName() != name)
772 case eSymbolTypeReExported:
774 ConstString reexport_name = symbol->GetReExportedSymbolName();
777 ModuleSP reexport_module_sp;
778 ModuleSpec reexport_module_spec;
779 reexport_module_spec.GetPlatformFileSpec() = symbol->GetReExportedSymbolSharedLibrary();
780 if (reexport_module_spec.GetPlatformFileSpec())
782 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
783 if (!reexport_module_sp)
785 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
786 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
789 return FindGlobalDataSymbol(target, symbol->GetReExportedSymbolName(), reexport_module_sp.get());
794 case eSymbolTypeCode: // We already lookup functions elsewhere
795 case eSymbolTypeVariable:
796 case eSymbolTypeLocal:
797 case eSymbolTypeParam:
798 case eSymbolTypeTrampoline:
799 case eSymbolTypeInvalid:
800 case eSymbolTypeException:
801 case eSymbolTypeSourceFile:
802 case eSymbolTypeHeaderFile:
803 case eSymbolTypeObjectFile:
804 case eSymbolTypeCommonBlock:
805 case eSymbolTypeBlock:
806 case eSymbolTypeVariableType:
807 case eSymbolTypeLineEntry:
808 case eSymbolTypeLineHeader:
809 case eSymbolTypeScopeBegin:
810 case eSymbolTypeScopeEnd:
811 case eSymbolTypeAdditional:
812 case eSymbolTypeCompiler:
813 case eSymbolTypeInstrumentation:
814 case eSymbolTypeUndefined:
815 case eSymbolTypeResolver:
826 ClangExpressionDeclMap::FindGlobalVariable
830 const ConstString &name,
831 ClangNamespaceDecl *namespace_decl,
837 if (module && namespace_decl)
838 module->FindGlobalVariables (name, namespace_decl, true, -1, vars);
840 target.GetImages().FindGlobalVariables(name, true, -1, vars);
846 for (size_t i = 0; i < vars.GetSize(); ++i)
848 VariableSP var_sp = vars.GetVariableAtIndex(i);
850 if (ClangASTContext::AreTypesSame(*type, var_sp->GetType()->GetClangFullType()))
856 return vars.GetVariableAtIndex(0);
863 // Interface for ClangASTSource
866 ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context)
868 assert (m_ast_context);
870 ClangASTMetrics::RegisterVisibleQuery();
872 const ConstString name(context.m_decl_name.getAsString().c_str());
874 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
876 if (GetImportInProgress())
878 if (log && log->GetVerbose())
879 log->Printf("Ignoring a query during an import");
883 static unsigned int invocation_id = 0;
884 unsigned int current_id = invocation_id++;
888 if (!context.m_decl_context)
889 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a NULL DeclContext", current_id, name.GetCString());
890 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
891 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in '%s'", current_id, name.GetCString(), context_named_decl->getNameAsString().c_str());
893 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a '%s'", current_id, name.GetCString(), context.m_decl_context->getDeclKindName());
896 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
898 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
900 if (log && log->GetVerbose())
901 log->Printf(" CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)",
904 (int)namespace_map->size());
909 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
914 log->Printf(" CEDM::FEVD[%u] Searching namespace %s in module %s",
916 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
917 i->first->GetFileSpec().GetFilename().GetCString());
919 FindExternalVisibleDecls(context,
925 else if (isa<TranslationUnitDecl>(context.m_decl_context))
927 ClangNamespaceDecl namespace_decl;
930 log->Printf(" CEDM::FEVD[%u] Searching the root namespace", current_id);
932 FindExternalVisibleDecls(context,
938 if (!context.m_found.variable)
939 ClangASTSource::FindExternalVisibleDecls(context);
943 ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context,
944 lldb::ModuleSP module_sp,
945 ClangNamespaceDecl &namespace_decl,
946 unsigned int current_id)
948 assert (m_ast_context);
950 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
952 SymbolContextList sc_list;
954 const ConstString name(context.m_decl_name.getAsString().c_str());
956 const char *name_unique_cstr = name.GetCString();
958 if (name_unique_cstr == NULL)
961 static ConstString id_name("id");
962 static ConstString Class_name("Class");
964 if (name == id_name || name == Class_name)
967 // Only look for functions by name out in our symbols if the function
968 // doesn't start with our phony prefix of '$'
969 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
970 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
971 if (name_unique_cstr[0] == '$' && !namespace_decl)
973 static ConstString g_lldb_class_name ("$__lldb_class");
975 if (name == g_lldb_class_name)
977 // Clang is looking for the type of "this"
982 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
984 if (!sym_ctx.function)
987 // Get the block that defines the function
988 Block *function_block = sym_ctx.GetFunctionBlock();
993 clang::DeclContext *decl_context = function_block->GetClangDeclContext();
998 clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context);
1002 clang::CXXRecordDecl *class_decl = method_decl->getParent();
1004 QualType class_qual_type(class_decl->getTypeForDecl(), 0);
1006 TypeFromUser class_user_type (class_qual_type.getAsOpaquePtr(),
1007 &class_decl->getASTContext());
1011 ASTDumper ast_dumper(class_qual_type);
1012 log->Printf(" CEDM::FEVD[%u] Adding type for $__lldb_class: %s", current_id, ast_dumper.GetCString());
1015 TypeFromParser class_type = CopyClassType(class_user_type, current_id);
1017 if (!class_type.IsValid())
1020 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(QualType::getFromOpaquePtr(class_type.GetOpaqueQualType()));
1022 if (!type_source_info)
1025 TypedefDecl *typedef_decl = TypedefDecl::Create(*m_ast_context,
1026 m_ast_context->getTranslationUnitDecl(),
1029 context.m_decl_name.getAsIdentifierInfo(),
1036 context.AddNamedDecl(typedef_decl);
1038 if (method_decl->isInstance())
1040 // self is a pointer to the object
1042 QualType class_pointer_type = method_decl->getASTContext().getPointerType(class_qual_type);
1044 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
1045 &method_decl->getASTContext());
1047 m_struct_vars->m_object_pointer_type = self_user_type;
1052 // This branch will get hit if we are executing code in the context of a function that
1053 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a
1054 // method of the class. In that case, just look up the "this" variable in the the current
1055 // scope and use its type.
1056 // FIXME: This code is formally correct, but clang doesn't currently emit DW_AT_object_pointer
1057 // for C++ so it hasn't actually been tested.
1059 VariableList *vars = frame->GetVariableList(false);
1061 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
1064 this_var->IsInScope(frame) &&
1065 this_var->LocationIsValidForFrame (frame))
1067 Type *this_type = this_var->GetType();
1072 ClangASTType pointee_type = this_type->GetClangForwardType().GetPointeeType();
1074 if (pointee_type.IsValid())
1078 ASTDumper ast_dumper(this_type->GetClangFullType());
1079 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1082 TypeFromUser class_user_type(pointee_type);
1083 AddOneType(context, class_user_type, current_id);
1086 TypeFromUser this_user_type(this_type->GetClangFullType());
1087 m_struct_vars->m_object_pointer_type = this_user_type;
1096 static ConstString g_lldb_objc_class_name ("$__lldb_objc_class");
1097 if (name == g_lldb_objc_class_name)
1099 // Clang is looking for the type of "*self"
1104 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
1106 if (!sym_ctx.function)
1109 // Get the block that defines the function
1110 Block *function_block = sym_ctx.GetFunctionBlock();
1112 if (!function_block)
1115 clang::DeclContext *decl_context = function_block->GetClangDeclContext();
1120 clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context);
1124 ObjCInterfaceDecl* self_interface = method_decl->getClassInterface();
1126 if (!self_interface)
1129 const clang::Type *interface_type = self_interface->getTypeForDecl();
1131 if (!interface_type)
1132 return; // This is unlikely, but we have seen crashes where this occurred
1134 TypeFromUser class_user_type(QualType(interface_type, 0).getAsOpaquePtr(),
1135 &method_decl->getASTContext());
1139 ASTDumper ast_dumper(interface_type);
1140 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1143 AddOneType(context, class_user_type, current_id);
1145 if (method_decl->isInstanceMethod())
1147 // self is a pointer to the object
1149 QualType class_pointer_type = method_decl->getASTContext().getObjCObjectPointerType(QualType(interface_type, 0));
1151 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
1152 &method_decl->getASTContext());
1154 m_struct_vars->m_object_pointer_type = self_user_type;
1158 // self is a Class pointer
1159 QualType class_type = method_decl->getASTContext().getObjCClassType();
1161 TypeFromUser self_user_type(class_type.getAsOpaquePtr(),
1162 &method_decl->getASTContext());
1164 m_struct_vars->m_object_pointer_type = self_user_type;
1171 // This branch will get hit if we are executing code in the context of a function that
1172 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a
1173 // method of the class. In that case, just look up the "self" variable in the the current
1174 // scope and use its type.
1176 VariableList *vars = frame->GetVariableList(false);
1178 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
1181 self_var->IsInScope(frame) &&
1182 self_var->LocationIsValidForFrame (frame))
1184 Type *self_type = self_var->GetType();
1189 ClangASTType self_clang_type = self_type->GetClangFullType();
1191 if (self_clang_type.IsObjCClassType())
1195 else if (self_clang_type.IsObjCObjectPointerType())
1197 self_clang_type = self_clang_type.GetPointeeType();
1199 if (!self_clang_type)
1204 ASTDumper ast_dumper(self_type->GetClangFullType());
1205 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1208 TypeFromUser class_user_type (self_clang_type);
1210 AddOneType(context, class_user_type, current_id);
1212 TypeFromUser self_user_type(self_type->GetClangFullType());
1214 m_struct_vars->m_object_pointer_type = self_user_type;
1223 // any other $__lldb names should be weeded out now
1224 if (!::strncmp(name_unique_cstr, "$__lldb", sizeof("$__lldb") - 1))
1232 ClangASTContext *scratch_clang_ast_context = target->GetScratchClangASTContext();
1234 if (!scratch_clang_ast_context)
1237 ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
1239 if (!scratch_ast_context)
1242 TypeDecl *ptype_type_decl = m_parser_vars->m_persistent_vars->GetPersistentType(name);
1244 if (!ptype_type_decl)
1247 Decl *parser_ptype_decl = m_ast_importer->CopyDecl(m_ast_context, scratch_ast_context, ptype_type_decl);
1249 if (!parser_ptype_decl)
1252 TypeDecl *parser_ptype_type_decl = dyn_cast<TypeDecl>(parser_ptype_decl);
1254 if (!parser_ptype_type_decl)
1258 log->Printf(" CEDM::FEVD[%u] Found persistent type %s", current_id, name.GetCString());
1260 context.AddNamedDecl(parser_ptype_type_decl);
1263 ClangExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariable(name));
1267 AddOneVariable(context, pvar_sp, current_id);
1271 const char *reg_name(&name.GetCString()[1]);
1273 if (m_parser_vars->m_exe_ctx.GetRegisterContext())
1275 const RegisterInfo *reg_info(m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(reg_name));
1280 log->Printf(" CEDM::FEVD[%u] Found register %s", current_id, reg_info->name);
1282 AddOneRegister(context, reg_info, current_id);
1288 ValueObjectSP valobj;
1292 if (frame && !namespace_decl)
1294 valobj = frame->GetValueForVariableExpressionPath(name_unique_cstr,
1296 StackFrame::eExpressionPathOptionCheckPtrVsMember ||
1297 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess ||
1298 StackFrame::eExpressionPathOptionsNoFragileObjcIvar ||
1299 StackFrame::eExpressionPathOptionsNoSyntheticChildren ||
1300 StackFrame::eExpressionPathOptionsNoSyntheticArrayRange,
1304 // If we found a variable in scope, no need to pull up function names
1305 if (err.Success() && var)
1307 AddOneVariable(context, var, valobj, current_id);
1308 context.m_found.variable = true;
1315 var = FindGlobalVariable (*target,
1323 valobj = ValueObjectVariable::Create(target, var);
1324 AddOneVariable(context, var, valobj, current_id);
1325 context.m_found.variable = true;
1330 if (!context.m_found.variable)
1332 const bool include_inlines = false;
1333 const bool append = false;
1335 if (namespace_decl && module_sp)
1337 const bool include_symbols = false;
1339 module_sp->FindFunctions(name,
1341 eFunctionNameTypeBase,
1347 else if (target && !namespace_decl)
1349 const bool include_symbols = true;
1351 // TODO Fix FindFunctions so that it doesn't return
1352 // instance methods for eFunctionNameTypeBase.
1354 target->GetImages().FindFunctions(name,
1355 eFunctionNameTypeFull,
1362 if (sc_list.GetSize())
1364 Symbol *extern_symbol = NULL;
1365 Symbol *non_extern_symbol = NULL;
1367 for (uint32_t index = 0, num_indices = sc_list.GetSize();
1368 index < num_indices;
1371 SymbolContext sym_ctx;
1372 sc_list.GetContextAtIndex(index, sym_ctx);
1374 if (sym_ctx.function)
1376 clang::DeclContext *decl_ctx = sym_ctx.function->GetClangDeclContext();
1381 // Filter out class/instance methods.
1382 if (dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
1384 if (dyn_cast<clang::CXXMethodDecl>(decl_ctx))
1387 AddOneFunction(context, sym_ctx.function, NULL, current_id);
1388 context.m_found.function_with_type_info = true;
1389 context.m_found.function = true;
1391 else if (sym_ctx.symbol)
1393 if (sym_ctx.symbol->GetType() == eSymbolTypeReExported)
1395 sym_ctx.symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target);
1396 if (sym_ctx.symbol == NULL)
1400 if (sym_ctx.symbol->IsExternal())
1401 extern_symbol = sym_ctx.symbol;
1403 non_extern_symbol = sym_ctx.symbol;
1407 if (!context.m_found.function_with_type_info)
1411 AddOneFunction (context, NULL, extern_symbol, current_id);
1412 context.m_found.function = true;
1414 else if (non_extern_symbol)
1416 AddOneFunction (context, NULL, non_extern_symbol, current_id);
1417 context.m_found.function = true;
1422 if (target && !context.m_found.variable && !namespace_decl)
1424 // We couldn't find a non-symbol variable for this. Now we'll hunt for a generic
1425 // data symbol, and -- if it is found -- treat it as a variable.
1427 const Symbol *data_symbol = FindGlobalDataSymbol(*target, name);
1431 std::string warning("got name from symbols: ");
1432 warning.append(name.AsCString());
1433 const unsigned diag_id = m_ast_context->getDiagnostics().getCustomDiagID(clang::DiagnosticsEngine::Level::Warning, "%0");
1434 m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str();
1435 AddOneGenericVariable(context, *data_symbol, current_id);
1436 context.m_found.variable = true;
1444 MaybePromoteToBlockPointerType
1446 ASTContext *ast_context,
1447 clang_type_t candidate_type
1450 if (!candidate_type)
1451 return candidate_type;
1453 QualType candidate_qual_type = QualType::getFromOpaquePtr(candidate_type);
1455 const PointerType *candidate_pointer_type = dyn_cast<PointerType>(candidate_qual_type);
1457 if (!candidate_pointer_type)
1458 return candidate_type;
1460 QualType pointee_qual_type = candidate_pointer_type->getPointeeType();
1462 const RecordType *pointee_record_type = dyn_cast<RecordType>(pointee_qual_type);
1464 if (!pointee_record_type)
1465 return candidate_type;
1467 RecordDecl *pointee_record_decl = pointee_record_type->getDecl();
1469 if (!pointee_record_decl->isRecord())
1470 return candidate_type;
1472 if (!pointee_record_decl->getName().startswith(llvm::StringRef("__block_literal_")))
1473 return candidate_type;
1475 QualType generic_function_type = ast_context->getFunctionNoProtoType(ast_context->UnknownAnyTy);
1476 QualType block_pointer_type = ast_context->getBlockPointerType(generic_function_type);
1478 return block_pointer_type.getAsOpaquePtr();
1482 ClangExpressionDeclMap::GetVariableValue (VariableSP &var,
1483 lldb_private::Value &var_location,
1484 TypeFromUser *user_type,
1485 TypeFromParser *parser_type)
1487 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1489 Type *var_type = var->GetType();
1494 log->PutCString("Skipped a definition because it has no type");
1498 ClangASTType var_clang_type = var_type->GetClangFullType();
1500 if (!var_clang_type)
1503 log->PutCString("Skipped a definition because it has no Clang type");
1507 ASTContext *ast = var_type->GetClangASTContext().getASTContext();
1512 log->PutCString("There is no AST context for the current execution context");
1515 //var_clang_type = MaybePromoteToBlockPointerType (ast, var_clang_type);
1517 DWARFExpression &var_location_expr = var->LocationExpression();
1519 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
1521 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1523 if (var_location_expr.IsLocationList())
1525 SymbolContext var_sc;
1526 var->CalculateSymbolContext (&var_sc);
1527 loclist_base_load_addr = var_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
1531 if (var->GetLocationIsConstantValueData())
1533 DataExtractor const_value_extractor;
1535 if (var_location_expr.GetExpressionData(const_value_extractor))
1537 var_location = Value(const_value_extractor.GetDataStart(), const_value_extractor.GetByteSize());
1538 var_location.SetValueType(Value::eValueTypeHostAddress);
1543 log->Printf("Error evaluating constant variable: %s", err.AsCString());
1548 ClangASTType type_to_use = GuardedCopyType(var_clang_type);
1553 log->Printf("Couldn't copy a variable's type into the parser's AST context");
1559 *parser_type = TypeFromParser(type_to_use);
1561 if (var_location.GetContextType() == Value::eContextTypeInvalid)
1562 var_location.SetClangType(type_to_use);
1564 if (var_location.GetValueType() == Value::eValueTypeFileAddress)
1566 SymbolContext var_sc;
1567 var->CalculateSymbolContext(&var_sc);
1569 if (!var_sc.module_sp)
1572 Address so_addr(var_location.GetScalar().ULongLong(), var_sc.module_sp->GetSectionList());
1574 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
1576 if (load_addr != LLDB_INVALID_ADDRESS)
1578 var_location.GetScalar() = load_addr;
1579 var_location.SetValueType(Value::eValueTypeLoadAddress);
1584 *user_type = TypeFromUser(var_clang_type);
1590 ClangExpressionDeclMap::AddOneVariable (NameSearchContext &context, VariableSP var, ValueObjectSP valobj, unsigned int current_id)
1592 assert (m_parser_vars.get());
1594 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1600 if (!GetVariableValue (var, var_location, &ut, &pt))
1603 clang::QualType parser_opaque_type = QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
1605 if (parser_opaque_type.isNull())
1608 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr())
1610 if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1611 CompleteType(tag_type->getDecl());
1612 if (const ObjCObjectPointerType *objc_object_ptr_type = dyn_cast<ObjCObjectPointerType>(parser_type))
1613 CompleteType(objc_object_ptr_type->getInterfaceDecl());
1617 bool is_reference = pt.IsReferenceType();
1619 NamedDecl *var_decl = NULL;
1621 var_decl = context.AddVarDecl(pt);
1623 var_decl = context.AddVarDecl(pt.GetLValueReferenceType());
1625 std::string decl_name(context.m_decl_name.getAsString());
1626 ConstString entity_name(decl_name.c_str());
1627 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (valobj));
1629 assert (entity.get());
1630 entity->EnableParserVars(GetParserID());
1631 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1632 parser_vars->m_parser_type = pt;
1633 parser_vars->m_named_decl = var_decl;
1634 parser_vars->m_llvm_value = NULL;
1635 parser_vars->m_lldb_value = var_location;
1636 parser_vars->m_lldb_var = var;
1639 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference;
1643 ASTDumper orig_dumper(ut.GetOpaqueQualType());
1644 ASTDumper ast_dumper(var_decl);
1645 log->Printf(" CEDM::FEVD[%u] Found variable %s, returned %s (original %s)", current_id, decl_name.c_str(), ast_dumper.GetCString(), orig_dumper.GetCString());
1650 ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
1651 ClangExpressionVariableSP &pvar_sp,
1652 unsigned int current_id)
1654 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1656 TypeFromUser user_type (pvar_sp->GetTypeFromUser());
1658 TypeFromParser parser_type (GuardedCopyType(user_type));
1660 if (!parser_type.GetOpaqueQualType())
1663 log->Printf(" CEDM::FEVD[%u] Couldn't import type for pvar %s", current_id, pvar_sp->GetName().GetCString());
1667 NamedDecl *var_decl = context.AddVarDecl(parser_type.GetLValueReferenceType());
1669 pvar_sp->EnableParserVars(GetParserID());
1670 ClangExpressionVariable::ParserVars *parser_vars = pvar_sp->GetParserVars(GetParserID());
1671 parser_vars->m_parser_type = parser_type;
1672 parser_vars->m_named_decl = var_decl;
1673 parser_vars->m_llvm_value = NULL;
1674 parser_vars->m_lldb_value.Clear();
1678 ASTDumper ast_dumper(var_decl);
1679 log->Printf(" CEDM::FEVD[%u] Added pvar %s, returned %s", current_id, pvar_sp->GetName().GetCString(), ast_dumper.GetCString());
1684 ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context,
1685 const Symbol &symbol,
1686 unsigned int current_id)
1688 assert(m_parser_vars.get());
1690 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1692 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1697 ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext();
1699 TypeFromUser user_type (ClangASTContext::GetBasicType(scratch_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType());
1700 TypeFromParser parser_type (ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType());
1701 NamedDecl *var_decl = context.AddVarDecl(parser_type);
1703 std::string decl_name(context.m_decl_name.getAsString());
1704 ConstString entity_name(decl_name.c_str());
1705 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (),
1708 m_parser_vars->m_target_info.byte_order,
1709 m_parser_vars->m_target_info.address_byte_size));
1710 assert (entity.get());
1712 entity->EnableParserVars(GetParserID());
1713 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1715 const Address &symbol_address = symbol.GetAddress();
1716 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target);
1718 //parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType());
1719 parser_vars->m_lldb_value.SetClangType(user_type);
1720 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr;
1721 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1723 parser_vars->m_parser_type = parser_type;
1724 parser_vars->m_named_decl = var_decl;
1725 parser_vars->m_llvm_value = NULL;
1726 parser_vars->m_lldb_sym = &symbol;
1730 ASTDumper ast_dumper(var_decl);
1732 log->Printf(" CEDM::FEVD[%u] Found variable %s, returned %s", current_id, decl_name.c_str(), ast_dumper.GetCString());
1737 ClangExpressionDeclMap::ResolveUnknownTypes()
1739 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1740 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1742 ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext();
1744 for (size_t index = 0, num_entities = m_found_entities.GetSize();
1745 index < num_entities;
1748 ClangExpressionVariableSP entity = m_found_entities.GetVariableAtIndex(index);
1750 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1752 if (entity->m_flags & ClangExpressionVariable::EVUnknownType)
1754 const NamedDecl *named_decl = parser_vars->m_named_decl;
1755 const VarDecl *var_decl = dyn_cast<VarDecl>(named_decl);
1760 log->Printf("Entity of unknown type does not have a VarDecl");
1766 ASTDumper ast_dumper(const_cast<VarDecl*>(var_decl));
1767 log->Printf("Variable of unknown type now has Decl %s", ast_dumper.GetCString());
1770 QualType var_type = var_decl->getType();
1771 TypeFromParser parser_type(var_type.getAsOpaquePtr(), &var_decl->getASTContext());
1773 lldb::clang_type_t copied_type = m_ast_importer->CopyType(scratch_ast_context, &var_decl->getASTContext(), var_type.getAsOpaquePtr());
1778 log->Printf("ClangExpressionDeclMap::ResolveUnknownType - Couldn't import the type for a variable");
1780 return (bool) lldb::ClangExpressionVariableSP();
1783 TypeFromUser user_type(copied_type, scratch_ast_context);
1785 // parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType());
1786 parser_vars->m_lldb_value.SetClangType(user_type);
1787 parser_vars->m_parser_type = parser_type;
1789 entity->SetClangType(user_type);
1791 entity->m_flags &= ~(ClangExpressionVariable::EVUnknownType);
1799 ClangExpressionDeclMap::AddOneRegister (NameSearchContext &context,
1800 const RegisterInfo *reg_info,
1801 unsigned int current_id)
1803 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1805 ClangASTType clang_type = ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (m_ast_context,
1807 reg_info->byte_size * 8);
1812 log->Printf(" Tried to add a type for %s, but couldn't get one", context.m_decl_name.getAsString().c_str());
1816 TypeFromParser parser_clang_type (clang_type);
1818 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type);
1820 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1821 m_parser_vars->m_target_info.byte_order,
1822 m_parser_vars->m_target_info.address_byte_size));
1823 assert (entity.get());
1825 std::string decl_name(context.m_decl_name.getAsString());
1826 entity->SetName (ConstString (decl_name.c_str()));
1827 entity->SetRegisterInfo (reg_info);
1828 entity->EnableParserVars(GetParserID());
1829 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1830 parser_vars->m_parser_type = parser_clang_type;
1831 parser_vars->m_named_decl = var_decl;
1832 parser_vars->m_llvm_value = NULL;
1833 parser_vars->m_lldb_value.Clear();
1834 entity->m_flags |= ClangExpressionVariable::EVBareRegister;
1838 ASTDumper ast_dumper(var_decl);
1839 log->Printf(" CEDM::FEVD[%d] Added register %s, returned %s", current_id, context.m_decl_name.getAsString().c_str(), ast_dumper.GetCString());
1844 ClangExpressionDeclMap::AddOneFunction (NameSearchContext &context,
1847 unsigned int current_id)
1849 assert (m_parser_vars.get());
1851 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1853 NamedDecl *function_decl = NULL;
1854 const Address *fun_address = NULL;
1855 ClangASTType function_clang_type;
1857 bool is_indirect_function = false;
1861 Type *function_type = function->GetType();
1866 log->PutCString(" Skipped a function because it has no type");
1870 function_clang_type = function_type->GetClangFullType();
1872 if (!function_clang_type)
1875 log->PutCString(" Skipped a function because it has no Clang type");
1879 fun_address = &function->GetAddressRange().GetBaseAddress();
1881 ClangASTType copied_function_type = GuardedCopyType(function_clang_type);
1882 if (copied_function_type)
1884 function_decl = context.AddFunDecl(copied_function_type);
1890 log->Printf (" Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}",
1891 function_type->GetName().GetCString(),
1892 function_type->GetID());
1900 // We failed to copy the type we found
1903 log->Printf (" Failed to import the function type '%s' {0x%8.8" PRIx64 "} into the expression parser AST contenxt",
1904 function_type->GetName().GetCString(),
1905 function_type->GetID());
1913 fun_address = &symbol->GetAddress();
1914 function_decl = context.AddGenericFunDecl();
1915 is_indirect_function = symbol->IsIndirect();
1920 log->PutCString(" AddOneFunction called with no function and no symbol");
1924 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1926 lldb::addr_t load_addr = fun_address->GetCallableLoadAddress(target, is_indirect_function);
1928 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (),
1929 m_parser_vars->m_target_info.byte_order,
1930 m_parser_vars->m_target_info.address_byte_size));
1931 assert (entity.get());
1933 std::string decl_name(context.m_decl_name.getAsString());
1934 entity->SetName(ConstString(decl_name.c_str()));
1935 entity->SetClangType (function_clang_type);
1936 entity->EnableParserVars(GetParserID());
1938 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1940 if (load_addr != LLDB_INVALID_ADDRESS)
1942 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1943 parser_vars->m_lldb_value.GetScalar() = load_addr;
1947 // We have to try finding a file address.
1949 lldb::addr_t file_addr = fun_address->GetFileAddress();
1951 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress);
1952 parser_vars->m_lldb_value.GetScalar() = file_addr;
1956 parser_vars->m_named_decl = function_decl;
1957 parser_vars->m_llvm_value = NULL;
1961 ASTDumper ast_dumper(function_decl);
1965 fun_address->Dump(&ss, m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), Address::DumpStyleResolvedDescription);
1967 log->Printf(" CEDM::FEVD[%u] Found %s function %s (description %s), returned %s",
1969 (function ? "specific" : "generic"),
1972 ast_dumper.GetCString());
1977 ClangExpressionDeclMap::CopyClassType(TypeFromUser &ut,
1978 unsigned int current_id)
1980 ClangASTType copied_clang_type = GuardedCopyType(ut);
1982 if (!copied_clang_type)
1984 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1987 log->Printf("ClangExpressionDeclMap::CopyClassType - Couldn't import the type");
1989 return TypeFromParser();
1992 if (copied_clang_type.IsAggregateType() && copied_clang_type.GetCompleteType ())
1994 ClangASTType void_clang_type = ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid);
1995 ClangASTType void_ptr_clang_type = void_clang_type.GetPointerType();
1997 ClangASTType method_type = ClangASTContext::CreateFunctionType (m_ast_context,
1999 &void_ptr_clang_type,
2002 copied_clang_type.GetTypeQualifiers());
2004 const bool is_virtual = false;
2005 const bool is_static = false;
2006 const bool is_inline = false;
2007 const bool is_explicit = false;
2008 const bool is_attr_used = true;
2009 const bool is_artificial = false;
2011 copied_clang_type.AddMethodToCXXRecordType ("$__lldb_expr",
2013 lldb::eAccessPublic,
2022 return TypeFromParser(copied_clang_type);
2026 ClangExpressionDeclMap::AddOneType(NameSearchContext &context,
2028 unsigned int current_id)
2030 ClangASTType copied_clang_type = GuardedCopyType(ut);
2032 if (!copied_clang_type)
2034 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2037 log->Printf("ClangExpressionDeclMap::AddOneType - Couldn't import the type");
2042 context.AddTypeDecl(copied_clang_type);