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/RegisterValue.h"
25 #include "lldb/Core/ValueObjectConstResult.h"
26 #include "lldb/Core/ValueObjectVariable.h"
27 #include "lldb/Expression/ASTDumper.h"
28 #include "lldb/Expression/ClangASTSource.h"
29 #include "lldb/Expression/ClangPersistentVariables.h"
30 #include "lldb/Expression/Materializer.h"
31 #include "lldb/Host/Endian.h"
32 #include "lldb/Symbol/ClangASTContext.h"
33 #include "lldb/Symbol/ClangNamespaceDecl.h"
34 #include "lldb/Symbol/CompileUnit.h"
35 #include "lldb/Symbol/Function.h"
36 #include "lldb/Symbol/ObjectFile.h"
37 #include "lldb/Symbol/SymbolContext.h"
38 #include "lldb/Symbol/SymbolVendor.h"
39 #include "lldb/Symbol/Type.h"
40 #include "lldb/Symbol/TypeList.h"
41 #include "lldb/Symbol/Variable.h"
42 #include "lldb/Symbol/VariableList.h"
43 #include "lldb/Target/ExecutionContext.h"
44 #include "lldb/Target/ObjCLanguageRuntime.h"
45 #include "lldb/Target/Process.h"
46 #include "lldb/Target/RegisterContext.h"
47 #include "lldb/Target/StackFrame.h"
48 #include "lldb/Target/Target.h"
49 #include "lldb/Target/Thread.h"
52 using namespace lldb_private;
53 using namespace clang;
55 ClangExpressionDeclMap::ClangExpressionDeclMap (bool keep_result_in_memory, ExecutionContext &exe_ctx) :
56 ClangASTSource (exe_ctx.GetTargetSP()),
59 m_keep_result_in_memory (keep_result_in_memory),
66 ClangExpressionDeclMap::~ClangExpressionDeclMap()
68 // Note: The model is now that the parser's AST context and all associated
69 // data does not vanish until the expression has been executed. This means
70 // that valuable lookup data (like namespaces) doesn't vanish, but
77 ClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx,
78 Materializer *materializer)
80 ClangASTMetrics::ClearLocalCounters();
83 m_parser_vars->m_exe_ctx = exe_ctx;
85 Target *target = exe_ctx.GetTargetPtr();
86 if (exe_ctx.GetFramePtr())
87 m_parser_vars->m_sym_ctx = exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything);
88 else if (exe_ctx.GetThreadPtr() && exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0))
89 m_parser_vars->m_sym_ctx = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext(lldb::eSymbolContextEverything);
90 else if (exe_ctx.GetProcessPtr())
92 m_parser_vars->m_sym_ctx.Clear(true);
93 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
97 m_parser_vars->m_sym_ctx.Clear(true);
98 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
103 m_parser_vars->m_persistent_vars = &target->GetPersistentVariables();
105 if (!target->GetScratchClangASTContext())
109 m_parser_vars->m_target_info = GetTargetInfo();
110 m_parser_vars->m_materializer = materializer;
116 ClangExpressionDeclMap::DidParse()
118 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
121 ClangASTMetrics::DumpCounters(log);
123 if (m_parser_vars.get())
125 for (size_t entity_index = 0, num_entities = m_found_entities.GetSize();
126 entity_index < num_entities;
129 ClangExpressionVariableSP var_sp(m_found_entities.GetVariableAtIndex(entity_index));
131 var_sp->DisableParserVars(GetParserID());
134 for (size_t pvar_index = 0, num_pvars = m_parser_vars->m_persistent_vars->GetSize();
135 pvar_index < num_pvars;
138 ClangExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index));
140 pvar_sp->DisableParserVars(GetParserID());
147 // Interface for IRForTarget
149 ClangExpressionDeclMap::TargetInfo
150 ClangExpressionDeclMap::GetTargetInfo()
152 assert (m_parser_vars.get());
156 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
158 Process *process = exe_ctx.GetProcessPtr();
161 ret.byte_order = process->GetByteOrder();
162 ret.address_byte_size = process->GetAddressByteSize();
166 Target *target = exe_ctx.GetTargetPtr();
169 ret.byte_order = target->GetArchitecture().GetByteOrder();
170 ret.address_byte_size = target->GetArchitecture().GetAddressByteSize();
178 ClangExpressionDeclMap::AddPersistentVariable
180 const NamedDecl *decl,
181 const ConstString &name,
182 TypeFromParser parser_type,
187 assert (m_parser_vars.get());
189 if (m_parser_vars->m_materializer && is_result)
193 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
194 Target *target = exe_ctx.GetTargetPtr();
198 ASTContext *context(target->GetScratchClangASTContext()->getASTContext());
200 TypeFromUser user_type(m_ast_importer->DeportType(context,
201 parser_type.GetASTContext(),
202 parser_type.GetOpaqueQualType()),
205 uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(user_type, is_lvalue, m_keep_result_in_memory, err);
207 ClangExpressionVariableSP var_sp = m_found_entities.CreateVariable(exe_ctx.GetBestExecutionContextScope(),
210 m_parser_vars->m_target_info.byte_order,
211 m_parser_vars->m_target_info.address_byte_size);
216 var_sp->EnableParserVars(GetParserID());
218 ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
220 parser_vars->m_named_decl = decl;
221 parser_vars->m_parser_type = parser_type;
223 var_sp->EnableJITVars(GetParserID());
225 ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID());
227 jit_vars->m_offset = offset;
232 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
233 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
234 Target *target = exe_ctx.GetTargetPtr();
238 ASTContext *context(target->GetScratchClangASTContext()->getASTContext());
240 TypeFromUser user_type(m_ast_importer->DeportType(context,
241 parser_type.GetASTContext(),
242 parser_type.GetOpaqueQualType()),
245 if (!user_type.GetOpaqueQualType())
248 log->Printf("Persistent variable's type wasn't copied successfully");
252 if (!m_parser_vars->m_target_info.IsValid())
255 ClangExpressionVariableSP var_sp = m_parser_vars->m_persistent_vars->CreatePersistentVariable (exe_ctx.GetBestExecutionContextScope (),
258 m_parser_vars->m_target_info.byte_order,
259 m_parser_vars->m_target_info.address_byte_size);
264 var_sp->m_frozen_sp->SetHasCompleteType();
267 var_sp->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry;
269 var_sp->m_flags |= ClangExpressionVariable::EVKeepInTarget; // explicitly-declared persistent variables should persist
273 var_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
277 var_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
278 var_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
281 if (m_keep_result_in_memory)
283 var_sp->m_flags |= ClangExpressionVariable::EVKeepInTarget;
287 log->Printf("Created persistent variable with flags 0x%hx", var_sp->m_flags);
289 var_sp->EnableParserVars(GetParserID());
291 ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
293 parser_vars->m_named_decl = decl;
294 parser_vars->m_parser_type = parser_type;
300 ClangExpressionDeclMap::AddValueToStruct
302 const NamedDecl *decl,
303 const ConstString &name,
309 assert (m_struct_vars.get());
310 assert (m_parser_vars.get());
312 bool is_persistent_variable = false;
314 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
316 m_struct_vars->m_struct_laid_out = false;
318 if (m_struct_members.GetVariable(decl, GetParserID()))
321 ClangExpressionVariableSP var_sp (m_found_entities.GetVariable(decl, GetParserID()));
325 var_sp = m_parser_vars->m_persistent_vars->GetVariable(decl, GetParserID());
326 is_persistent_variable = true;
333 log->Printf("Adding value for (NamedDecl*)%p [%s - %s] to the structure",
336 var_sp->GetName().GetCString());
338 // We know entity->m_parser_vars is valid because we used a parser variable
341 ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
343 parser_vars->m_llvm_value = value;
345 if (ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID()))
347 // We already laid this out; do not touch
350 log->Printf("Already placed at 0x%llx", (unsigned long long)jit_vars->m_offset);
353 var_sp->EnableJITVars(GetParserID());
355 ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID());
357 jit_vars->m_alignment = alignment;
358 jit_vars->m_size = size;
360 m_struct_members.AddVariable(var_sp);
362 if (m_parser_vars->m_materializer)
368 if (is_persistent_variable)
370 offset = m_parser_vars->m_materializer->AddPersistentVariable(var_sp, err);
374 if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym)
375 offset = m_parser_vars->m_materializer->AddSymbol(*sym, err);
376 else if (const RegisterInfo *reg_info = var_sp->GetRegisterInfo())
377 offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err);
378 else if (parser_vars->m_lldb_var)
379 offset = m_parser_vars->m_materializer->AddVariable(parser_vars->m_lldb_var, err);
386 log->Printf("Placed at 0x%llx", (unsigned long long)offset);
388 jit_vars->m_offset = offset; // TODO DoStructLayout() should not change this.
395 ClangExpressionDeclMap::DoStructLayout ()
397 assert (m_struct_vars.get());
399 if (m_struct_vars->m_struct_laid_out)
402 if (!m_parser_vars->m_materializer)
405 m_struct_vars->m_struct_alignment = m_parser_vars->m_materializer->GetStructAlignment();
406 m_struct_vars->m_struct_size = m_parser_vars->m_materializer->GetStructByteSize();
407 m_struct_vars->m_struct_laid_out = true;
411 bool ClangExpressionDeclMap::GetStructInfo
413 uint32_t &num_elements,
418 assert (m_struct_vars.get());
420 if (!m_struct_vars->m_struct_laid_out)
423 num_elements = m_struct_members.GetSize();
424 size = m_struct_vars->m_struct_size;
425 alignment = m_struct_vars->m_struct_alignment;
431 ClangExpressionDeclMap::GetStructElement
433 const NamedDecl *&decl,
440 assert (m_struct_vars.get());
442 if (!m_struct_vars->m_struct_laid_out)
445 if (index >= m_struct_members.GetSize())
448 ClangExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index));
453 ClangExpressionVariable::ParserVars *parser_vars = member_sp->GetParserVars(GetParserID());
454 ClangExpressionVariable::JITVars *jit_vars = member_sp->GetJITVars(GetParserID());
458 !member_sp->GetValueObject())
461 decl = parser_vars->m_named_decl;
462 value = parser_vars->m_llvm_value;
463 offset = jit_vars->m_offset;
464 name = member_sp->GetName();
470 ClangExpressionDeclMap::GetFunctionInfo
472 const NamedDecl *decl,
476 ClangExpressionVariableSP entity_sp(m_found_entities.GetVariable(decl, GetParserID()));
481 // We know m_parser_vars is valid since we searched for the variable by
484 ClangExpressionVariable::ParserVars *parser_vars = entity_sp->GetParserVars(GetParserID());
486 ptr = parser_vars->m_lldb_value.GetScalar().ULongLong();
492 FindCodeSymbolInContext
494 const ConstString &name,
495 SymbolContext &sym_ctx,
496 SymbolContextList &sc_list
499 SymbolContextList temp_sc_list;
500 if (sym_ctx.module_sp)
501 sym_ctx.module_sp->FindSymbolsWithNameAndType(name, eSymbolTypeAny, temp_sc_list);
503 if (!sc_list.GetSize() && sym_ctx.target_sp)
504 sym_ctx.target_sp->GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, temp_sc_list);
506 unsigned temp_sc_list_size = temp_sc_list.GetSize();
507 for (unsigned i = 0; i < temp_sc_list_size; i++)
509 SymbolContext sym_ctx;
510 temp_sc_list.GetContextAtIndex(i, sym_ctx);
513 switch (sym_ctx.symbol->GetType())
515 case eSymbolTypeCode:
516 case eSymbolTypeResolver:
517 sc_list.Append(sym_ctx);
528 ClangExpressionDeclMap::GetFunctionAddress
530 const ConstString &name,
534 assert (m_parser_vars.get());
536 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
537 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
538 Target *target = exe_ctx.GetTargetPtr();
539 // Back out in all cases where we're not fully initialized
542 if (!m_parser_vars->m_sym_ctx.target_sp)
545 SymbolContextList sc_list;
547 FindCodeSymbolInContext(name, m_parser_vars->m_sym_ctx, sc_list);
549 if (!sc_list.GetSize())
551 // We occasionally get debug information in which a const function is reported
552 // as non-const, so the mangled name is wrong. This is a hack to compensate.
554 if (!strncmp(name.GetCString(), "_ZN", 3) &&
555 strncmp(name.GetCString(), "_ZNK", 4))
557 std::string fixed_scratch("_ZNK");
558 fixed_scratch.append(name.GetCString() + 3);
559 ConstString fixed_name(fixed_scratch.c_str());
562 log->Printf("Failed to find symbols given non-const name %s; trying %s", name.GetCString(), fixed_name.GetCString());
564 FindCodeSymbolInContext(fixed_name, m_parser_vars->m_sym_ctx, sc_list);
568 if (!sc_list.GetSize())
571 SymbolContext sym_ctx;
572 sc_list.GetContextAtIndex(0, sym_ctx);
574 const Address *func_so_addr = NULL;
575 bool is_indirect_function = false;
577 if (sym_ctx.function)
578 func_so_addr = &sym_ctx.function->GetAddressRange().GetBaseAddress();
579 else if (sym_ctx.symbol) {
580 func_so_addr = &sym_ctx.symbol->GetAddress();
581 is_indirect_function = sym_ctx.symbol->IsIndirect();
585 if (!func_so_addr || !func_so_addr->IsValid())
588 func_addr = func_so_addr->GetCallableLoadAddress (target, is_indirect_function);
594 ClangExpressionDeclMap::GetSymbolAddress (Target &target, Process *process, const ConstString &name, lldb::SymbolType symbol_type)
596 SymbolContextList sc_list;
598 target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list);
600 const uint32_t num_matches = sc_list.GetSize();
601 addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
603 for (uint32_t i=0; i<num_matches && (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS); i++)
605 SymbolContext sym_ctx;
606 sc_list.GetContextAtIndex(i, sym_ctx);
608 const Address *sym_address = &sym_ctx.symbol->GetAddress();
610 if (!sym_address || !sym_address->IsValid())
615 switch (sym_ctx.symbol->GetType())
617 case eSymbolTypeCode:
618 case eSymbolTypeTrampoline:
619 symbol_load_addr = sym_address->GetCallableLoadAddress (&target);
622 case eSymbolTypeResolver:
623 symbol_load_addr = sym_address->GetCallableLoadAddress (&target, true);
626 case eSymbolTypeData:
627 case eSymbolTypeRuntime:
628 case eSymbolTypeVariable:
629 case eSymbolTypeLocal:
630 case eSymbolTypeParam:
631 case eSymbolTypeInvalid:
632 case eSymbolTypeAbsolute:
633 case eSymbolTypeException:
634 case eSymbolTypeSourceFile:
635 case eSymbolTypeHeaderFile:
636 case eSymbolTypeObjectFile:
637 case eSymbolTypeCommonBlock:
638 case eSymbolTypeBlock:
639 case eSymbolTypeVariableType:
640 case eSymbolTypeLineEntry:
641 case eSymbolTypeLineHeader:
642 case eSymbolTypeScopeBegin:
643 case eSymbolTypeScopeEnd:
644 case eSymbolTypeAdditional:
645 case eSymbolTypeCompiler:
646 case eSymbolTypeInstrumentation:
647 case eSymbolTypeUndefined:
648 case eSymbolTypeObjCClass:
649 case eSymbolTypeObjCMetaClass:
650 case eSymbolTypeObjCIVar:
651 symbol_load_addr = sym_address->GetLoadAddress (&target);
657 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process)
659 ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime();
663 symbol_load_addr = runtime->LookupRuntimeSymbol(name);
667 return symbol_load_addr;
671 ClangExpressionDeclMap::GetSymbolAddress (const ConstString &name, lldb::SymbolType symbol_type)
673 assert (m_parser_vars.get());
675 if (!m_parser_vars->m_exe_ctx.GetTargetPtr())
678 return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(), m_parser_vars->m_exe_ctx.GetProcessPtr(), name, symbol_type);
682 ClangExpressionDeclMap::FindGlobalDataSymbol (Target &target,
683 const ConstString &name)
685 SymbolContextList sc_list;
687 target.GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
689 const uint32_t matches = sc_list.GetSize();
690 for (uint32_t i=0; i<matches; ++i)
692 SymbolContext sym_ctx;
693 sc_list.GetContextAtIndex(i, sym_ctx);
696 const Symbol *symbol = sym_ctx.symbol;
697 const Address *sym_address = &symbol->GetAddress();
699 if (sym_address && sym_address->IsValid())
701 switch (symbol->GetType())
703 case eSymbolTypeData:
704 case eSymbolTypeRuntime:
705 case eSymbolTypeAbsolute:
706 case eSymbolTypeObjCClass:
707 case eSymbolTypeObjCMetaClass:
708 case eSymbolTypeObjCIVar:
709 if (symbol->GetDemangledNameIsSynthesized())
711 // If the demangled name was synthesized, then don't use it
712 // for expressions. Only let the symbol match if the mangled
713 // named matches for these symbols.
714 if (symbol->GetMangled().GetMangledName() != name)
719 case eSymbolTypeCode: // We already lookup functions elsewhere
720 case eSymbolTypeVariable:
721 case eSymbolTypeLocal:
722 case eSymbolTypeParam:
723 case eSymbolTypeTrampoline:
724 case eSymbolTypeInvalid:
725 case eSymbolTypeException:
726 case eSymbolTypeSourceFile:
727 case eSymbolTypeHeaderFile:
728 case eSymbolTypeObjectFile:
729 case eSymbolTypeCommonBlock:
730 case eSymbolTypeBlock:
731 case eSymbolTypeVariableType:
732 case eSymbolTypeLineEntry:
733 case eSymbolTypeLineHeader:
734 case eSymbolTypeScopeBegin:
735 case eSymbolTypeScopeEnd:
736 case eSymbolTypeAdditional:
737 case eSymbolTypeCompiler:
738 case eSymbolTypeInstrumentation:
739 case eSymbolTypeUndefined:
740 case eSymbolTypeResolver:
751 ClangExpressionDeclMap::FindGlobalVariable
755 const ConstString &name,
756 ClangNamespaceDecl *namespace_decl,
762 if (module && namespace_decl)
763 module->FindGlobalVariables (name, namespace_decl, true, -1, vars);
765 target.GetImages().FindGlobalVariables(name, true, -1, vars);
771 for (size_t i = 0; i < vars.GetSize(); ++i)
773 VariableSP var_sp = vars.GetVariableAtIndex(i);
775 if (ClangASTContext::AreTypesSame(*type, var_sp->GetType()->GetClangFullType()))
781 return vars.GetVariableAtIndex(0);
788 // Interface for ClangASTSource
791 ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context)
793 assert (m_ast_context);
795 ClangASTMetrics::RegisterVisibleQuery();
797 const ConstString name(context.m_decl_name.getAsString().c_str());
799 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
801 if (GetImportInProgress())
803 if (log && log->GetVerbose())
804 log->Printf("Ignoring a query during an import");
808 static unsigned int invocation_id = 0;
809 unsigned int current_id = invocation_id++;
813 if (!context.m_decl_context)
814 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a NULL DeclContext", current_id, name.GetCString());
815 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
816 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in '%s'", current_id, name.GetCString(), context_named_decl->getNameAsString().c_str());
818 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a '%s'", current_id, name.GetCString(), context.m_decl_context->getDeclKindName());
821 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
823 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
825 if (log && log->GetVerbose())
826 log->Printf(" CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)",
829 (int)namespace_map->size());
834 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
839 log->Printf(" CEDM::FEVD[%u] Searching namespace %s in module %s",
841 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
842 i->first->GetFileSpec().GetFilename().GetCString());
844 FindExternalVisibleDecls(context,
850 else if (isa<TranslationUnitDecl>(context.m_decl_context))
852 ClangNamespaceDecl namespace_decl;
855 log->Printf(" CEDM::FEVD[%u] Searching the root namespace", current_id);
857 FindExternalVisibleDecls(context,
863 if (!context.m_found.variable)
864 ClangASTSource::FindExternalVisibleDecls(context);
868 ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context,
869 lldb::ModuleSP module_sp,
870 ClangNamespaceDecl &namespace_decl,
871 unsigned int current_id)
873 assert (m_ast_context);
875 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
877 SymbolContextList sc_list;
879 const ConstString name(context.m_decl_name.getAsString().c_str());
881 const char *name_unique_cstr = name.GetCString();
883 if (name_unique_cstr == NULL)
886 static ConstString id_name("id");
887 static ConstString Class_name("Class");
889 if (name == id_name || name == Class_name)
892 // Only look for functions by name out in our symbols if the function
893 // doesn't start with our phony prefix of '$'
894 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
895 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
896 if (name_unique_cstr[0] == '$' && !namespace_decl)
898 static ConstString g_lldb_class_name ("$__lldb_class");
900 if (name == g_lldb_class_name)
902 // Clang is looking for the type of "this"
907 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
909 if (!sym_ctx.function)
912 // Get the block that defines the function
913 Block *function_block = sym_ctx.GetFunctionBlock();
918 clang::DeclContext *decl_context = function_block->GetClangDeclContext();
923 clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context);
927 clang::CXXRecordDecl *class_decl = method_decl->getParent();
929 QualType class_qual_type(class_decl->getTypeForDecl(), 0);
931 TypeFromUser class_user_type (class_qual_type.getAsOpaquePtr(),
932 &class_decl->getASTContext());
936 ASTDumper ast_dumper(class_qual_type);
937 log->Printf(" CEDM::FEVD[%u] Adding type for $__lldb_class: %s", current_id, ast_dumper.GetCString());
940 TypeFromParser class_type = CopyClassType(class_user_type, current_id);
942 if (!class_type.IsValid())
945 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(QualType::getFromOpaquePtr(class_type.GetOpaqueQualType()));
947 if (!type_source_info)
950 TypedefDecl *typedef_decl = TypedefDecl::Create(*m_ast_context,
951 m_ast_context->getTranslationUnitDecl(),
954 context.m_decl_name.getAsIdentifierInfo(),
961 context.AddNamedDecl(typedef_decl);
963 if (method_decl->isInstance())
965 // self is a pointer to the object
967 QualType class_pointer_type = method_decl->getASTContext().getPointerType(class_qual_type);
969 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
970 &method_decl->getASTContext());
972 m_struct_vars->m_object_pointer_type = self_user_type;
977 // This branch will get hit if we are executing code in the context of a function that
978 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a
979 // method of the class. In that case, just look up the "this" variable in the the current
980 // scope and use its type.
981 // FIXME: This code is formally correct, but clang doesn't currently emit DW_AT_object_pointer
982 // for C++ so it hasn't actually been tested.
984 VariableList *vars = frame->GetVariableList(false);
986 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
989 this_var->IsInScope(frame) &&
990 this_var->LocationIsValidForFrame (frame))
992 Type *this_type = this_var->GetType();
997 ClangASTType pointee_type = this_type->GetClangForwardType().GetPointeeType();
999 if (pointee_type.IsValid())
1003 ASTDumper ast_dumper(this_type->GetClangFullType());
1004 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1007 TypeFromUser class_user_type(pointee_type);
1008 AddOneType(context, class_user_type, current_id);
1011 TypeFromUser this_user_type(this_type->GetClangFullType());
1012 m_struct_vars->m_object_pointer_type = this_user_type;
1021 static ConstString g_lldb_objc_class_name ("$__lldb_objc_class");
1022 if (name == g_lldb_objc_class_name)
1024 // Clang is looking for the type of "*self"
1029 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
1031 if (!sym_ctx.function)
1034 // Get the block that defines the function
1035 Block *function_block = sym_ctx.GetFunctionBlock();
1037 if (!function_block)
1040 clang::DeclContext *decl_context = function_block->GetClangDeclContext();
1045 clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context);
1049 ObjCInterfaceDecl* self_interface = method_decl->getClassInterface();
1051 if (!self_interface)
1054 const clang::Type *interface_type = self_interface->getTypeForDecl();
1056 if (!interface_type)
1057 return; // This is unlikely, but we have seen crashes where this occurred
1059 TypeFromUser class_user_type(QualType(interface_type, 0).getAsOpaquePtr(),
1060 &method_decl->getASTContext());
1064 ASTDumper ast_dumper(interface_type);
1065 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1068 AddOneType(context, class_user_type, current_id);
1070 if (method_decl->isInstanceMethod())
1072 // self is a pointer to the object
1074 QualType class_pointer_type = method_decl->getASTContext().getObjCObjectPointerType(QualType(interface_type, 0));
1076 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
1077 &method_decl->getASTContext());
1079 m_struct_vars->m_object_pointer_type = self_user_type;
1083 // self is a Class pointer
1084 QualType class_type = method_decl->getASTContext().getObjCClassType();
1086 TypeFromUser self_user_type(class_type.getAsOpaquePtr(),
1087 &method_decl->getASTContext());
1089 m_struct_vars->m_object_pointer_type = self_user_type;
1096 // This branch will get hit if we are executing code in the context of a function that
1097 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a
1098 // method of the class. In that case, just look up the "self" variable in the the current
1099 // scope and use its type.
1101 VariableList *vars = frame->GetVariableList(false);
1103 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
1106 self_var->IsInScope(frame) &&
1107 self_var->LocationIsValidForFrame (frame))
1109 Type *self_type = self_var->GetType();
1114 ClangASTType self_clang_type = self_type->GetClangFullType();
1116 if (self_clang_type.IsObjCClassType())
1120 else if (self_clang_type.IsObjCObjectPointerType())
1122 self_clang_type = self_clang_type.GetPointeeType();
1124 if (!self_clang_type)
1129 ASTDumper ast_dumper(self_type->GetClangFullType());
1130 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1133 TypeFromUser class_user_type (self_clang_type);
1135 AddOneType(context, class_user_type, current_id);
1137 TypeFromUser self_user_type(self_type->GetClangFullType());
1139 m_struct_vars->m_object_pointer_type = self_user_type;
1148 // any other $__lldb names should be weeded out now
1149 if (!::strncmp(name_unique_cstr, "$__lldb", sizeof("$__lldb") - 1))
1157 ClangASTContext *scratch_clang_ast_context = target->GetScratchClangASTContext();
1159 if (!scratch_clang_ast_context)
1162 ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
1164 if (!scratch_ast_context)
1167 TypeDecl *ptype_type_decl = m_parser_vars->m_persistent_vars->GetPersistentType(name);
1169 if (!ptype_type_decl)
1172 Decl *parser_ptype_decl = m_ast_importer->CopyDecl(m_ast_context, scratch_ast_context, ptype_type_decl);
1174 if (!parser_ptype_decl)
1177 TypeDecl *parser_ptype_type_decl = dyn_cast<TypeDecl>(parser_ptype_decl);
1179 if (!parser_ptype_type_decl)
1183 log->Printf(" CEDM::FEVD[%u] Found persistent type %s", current_id, name.GetCString());
1185 context.AddNamedDecl(parser_ptype_type_decl);
1188 ClangExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariable(name));
1192 AddOneVariable(context, pvar_sp, current_id);
1196 const char *reg_name(&name.GetCString()[1]);
1198 if (m_parser_vars->m_exe_ctx.GetRegisterContext())
1200 const RegisterInfo *reg_info(m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(reg_name));
1205 log->Printf(" CEDM::FEVD[%u] Found register %s", current_id, reg_info->name);
1207 AddOneRegister(context, reg_info, current_id);
1213 ValueObjectSP valobj;
1217 if (frame && !namespace_decl)
1219 valobj = frame->GetValueForVariableExpressionPath(name_unique_cstr,
1221 StackFrame::eExpressionPathOptionCheckPtrVsMember ||
1222 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess ||
1223 StackFrame::eExpressionPathOptionsNoFragileObjcIvar ||
1224 StackFrame::eExpressionPathOptionsNoSyntheticChildren ||
1225 StackFrame::eExpressionPathOptionsNoSyntheticArrayRange,
1229 // If we found a variable in scope, no need to pull up function names
1230 if (err.Success() && var)
1232 AddOneVariable(context, var, valobj, current_id);
1233 context.m_found.variable = true;
1240 var = FindGlobalVariable (*target,
1248 valobj = ValueObjectVariable::Create(target, var);
1249 AddOneVariable(context, var, valobj, current_id);
1250 context.m_found.variable = true;
1255 if (!context.m_found.variable)
1257 const bool include_inlines = false;
1258 const bool append = false;
1260 if (namespace_decl && module_sp)
1262 const bool include_symbols = false;
1264 module_sp->FindFunctions(name,
1266 eFunctionNameTypeBase,
1272 else if (target && !namespace_decl)
1274 const bool include_symbols = true;
1276 // TODO Fix FindFunctions so that it doesn't return
1277 // instance methods for eFunctionNameTypeBase.
1279 target->GetImages().FindFunctions(name,
1280 eFunctionNameTypeFull,
1287 if (sc_list.GetSize())
1289 Symbol *generic_symbol = NULL;
1290 Symbol *non_extern_symbol = NULL;
1292 for (uint32_t index = 0, num_indices = sc_list.GetSize();
1293 index < num_indices;
1296 SymbolContext sym_ctx;
1297 sc_list.GetContextAtIndex(index, sym_ctx);
1299 if (sym_ctx.function)
1301 clang::DeclContext *decl_ctx = sym_ctx.function->GetClangDeclContext();
1306 // Filter out class/instance methods.
1307 if (dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
1309 if (dyn_cast<clang::CXXMethodDecl>(decl_ctx))
1312 AddOneFunction(context, sym_ctx.function, NULL, current_id);
1313 context.m_found.function_with_type_info = true;
1314 context.m_found.function = true;
1316 else if (sym_ctx.symbol)
1318 if (sym_ctx.symbol->IsExternal())
1319 generic_symbol = sym_ctx.symbol;
1321 non_extern_symbol = sym_ctx.symbol;
1325 if (!context.m_found.function_with_type_info)
1329 AddOneFunction (context, NULL, generic_symbol, current_id);
1330 context.m_found.function = true;
1332 else if (non_extern_symbol)
1334 AddOneFunction (context, NULL, non_extern_symbol, current_id);
1335 context.m_found.function = true;
1340 if (target && !context.m_found.variable && !namespace_decl)
1342 // We couldn't find a non-symbol variable for this. Now we'll hunt for a generic
1343 // data symbol, and -- if it is found -- treat it as a variable.
1345 const Symbol *data_symbol = FindGlobalDataSymbol(*target, name);
1349 AddOneGenericVariable(context, *data_symbol, current_id);
1350 context.m_found.variable = true;
1358 MaybePromoteToBlockPointerType
1360 ASTContext *ast_context,
1361 clang_type_t candidate_type
1364 if (!candidate_type)
1365 return candidate_type;
1367 QualType candidate_qual_type = QualType::getFromOpaquePtr(candidate_type);
1369 const PointerType *candidate_pointer_type = dyn_cast<PointerType>(candidate_qual_type);
1371 if (!candidate_pointer_type)
1372 return candidate_type;
1374 QualType pointee_qual_type = candidate_pointer_type->getPointeeType();
1376 const RecordType *pointee_record_type = dyn_cast<RecordType>(pointee_qual_type);
1378 if (!pointee_record_type)
1379 return candidate_type;
1381 RecordDecl *pointee_record_decl = pointee_record_type->getDecl();
1383 if (!pointee_record_decl->isRecord())
1384 return candidate_type;
1386 if (!pointee_record_decl->getName().startswith(llvm::StringRef("__block_literal_")))
1387 return candidate_type;
1389 QualType generic_function_type = ast_context->getFunctionNoProtoType(ast_context->UnknownAnyTy);
1390 QualType block_pointer_type = ast_context->getBlockPointerType(generic_function_type);
1392 return block_pointer_type.getAsOpaquePtr();
1396 ClangExpressionDeclMap::GetVariableValue (VariableSP &var,
1397 lldb_private::Value &var_location,
1398 TypeFromUser *user_type,
1399 TypeFromParser *parser_type)
1401 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1403 Type *var_type = var->GetType();
1408 log->PutCString("Skipped a definition because it has no type");
1412 ClangASTType var_clang_type = var_type->GetClangFullType();
1414 if (!var_clang_type)
1417 log->PutCString("Skipped a definition because it has no Clang type");
1421 // commented out because of <rdar://problem/11024417>
1422 ASTContext *ast = var_type->GetClangASTContext().getASTContext();
1427 log->PutCString("There is no AST context for the current execution context");
1430 //var_clang_type = MaybePromoteToBlockPointerType (ast, var_clang_type);
1432 DWARFExpression &var_location_expr = var->LocationExpression();
1434 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
1436 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1438 if (var_location_expr.IsLocationList())
1440 SymbolContext var_sc;
1441 var->CalculateSymbolContext (&var_sc);
1442 loclist_base_load_addr = var_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
1446 if (var->GetLocationIsConstantValueData())
1448 DataExtractor const_value_extractor;
1450 if (var_location_expr.GetExpressionData(const_value_extractor))
1452 var_location = Value(const_value_extractor.GetDataStart(), const_value_extractor.GetByteSize());
1453 var_location.SetValueType(Value::eValueTypeHostAddress);
1458 log->Printf("Error evaluating constant variable: %s", err.AsCString());
1463 ClangASTType type_to_use = GuardedCopyType(var_clang_type);
1468 log->Printf("Couldn't copy a variable's type into the parser's AST context");
1474 *parser_type = TypeFromParser(type_to_use);
1476 if (var_location.GetContextType() == Value::eContextTypeInvalid)
1477 var_location.SetClangType(type_to_use);
1479 if (var_location.GetValueType() == Value::eValueTypeFileAddress)
1481 SymbolContext var_sc;
1482 var->CalculateSymbolContext(&var_sc);
1484 if (!var_sc.module_sp)
1487 Address so_addr(var_location.GetScalar().ULongLong(), var_sc.module_sp->GetSectionList());
1489 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
1491 if (load_addr != LLDB_INVALID_ADDRESS)
1493 var_location.GetScalar() = load_addr;
1494 var_location.SetValueType(Value::eValueTypeLoadAddress);
1499 *user_type = TypeFromUser(var_clang_type);
1505 ClangExpressionDeclMap::AddOneVariable (NameSearchContext &context, VariableSP var, ValueObjectSP valobj, unsigned int current_id)
1507 assert (m_parser_vars.get());
1509 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1515 if (!GetVariableValue (var, var_location, &ut, &pt))
1518 clang::QualType parser_opaque_type = QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
1520 if (parser_opaque_type.isNull())
1523 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr())
1525 if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1526 CompleteType(tag_type->getDecl());
1530 bool is_reference = pt.IsReferenceType();
1532 NamedDecl *var_decl = NULL;
1534 var_decl = context.AddVarDecl(pt);
1536 var_decl = context.AddVarDecl(pt.GetLValueReferenceType());
1538 std::string decl_name(context.m_decl_name.getAsString());
1539 ConstString entity_name(decl_name.c_str());
1540 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (valobj));
1542 assert (entity.get());
1543 entity->EnableParserVars(GetParserID());
1544 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1545 parser_vars->m_parser_type = pt;
1546 parser_vars->m_named_decl = var_decl;
1547 parser_vars->m_llvm_value = NULL;
1548 parser_vars->m_lldb_value = var_location;
1549 parser_vars->m_lldb_var = var;
1552 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference;
1556 ASTDumper orig_dumper(ut.GetOpaqueQualType());
1557 ASTDumper ast_dumper(var_decl);
1558 log->Printf(" CEDM::FEVD[%u] Found variable %s, returned %s (original %s)", current_id, decl_name.c_str(), ast_dumper.GetCString(), orig_dumper.GetCString());
1563 ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
1564 ClangExpressionVariableSP &pvar_sp,
1565 unsigned int current_id)
1567 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1569 TypeFromUser user_type (pvar_sp->GetTypeFromUser());
1571 TypeFromParser parser_type (GuardedCopyType(user_type));
1573 if (!parser_type.GetOpaqueQualType())
1576 log->Printf(" CEDM::FEVD[%u] Couldn't import type for pvar %s", current_id, pvar_sp->GetName().GetCString());
1580 NamedDecl *var_decl = context.AddVarDecl(parser_type.GetLValueReferenceType());
1582 pvar_sp->EnableParserVars(GetParserID());
1583 ClangExpressionVariable::ParserVars *parser_vars = pvar_sp->GetParserVars(GetParserID());
1584 parser_vars->m_parser_type = parser_type;
1585 parser_vars->m_named_decl = var_decl;
1586 parser_vars->m_llvm_value = NULL;
1587 parser_vars->m_lldb_value.Clear();
1591 ASTDumper ast_dumper(var_decl);
1592 log->Printf(" CEDM::FEVD[%u] Added pvar %s, returned %s", current_id, pvar_sp->GetName().GetCString(), ast_dumper.GetCString());
1597 ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context,
1598 const Symbol &symbol,
1599 unsigned int current_id)
1601 assert(m_parser_vars.get());
1603 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1605 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1610 ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext();
1612 TypeFromUser user_type (ClangASTContext::GetBasicType(scratch_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType());
1613 TypeFromParser parser_type (ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType());
1614 NamedDecl *var_decl = context.AddVarDecl(parser_type);
1616 std::string decl_name(context.m_decl_name.getAsString());
1617 ConstString entity_name(decl_name.c_str());
1618 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (),
1621 m_parser_vars->m_target_info.byte_order,
1622 m_parser_vars->m_target_info.address_byte_size));
1623 assert (entity.get());
1625 entity->EnableParserVars(GetParserID());
1626 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1628 const Address &symbol_address = symbol.GetAddress();
1629 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target);
1631 //parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType());
1632 parser_vars->m_lldb_value.SetClangType(user_type);
1633 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr;
1634 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1636 parser_vars->m_parser_type = parser_type;
1637 parser_vars->m_named_decl = var_decl;
1638 parser_vars->m_llvm_value = NULL;
1639 parser_vars->m_lldb_sym = &symbol;
1643 ASTDumper ast_dumper(var_decl);
1645 log->Printf(" CEDM::FEVD[%u] Found variable %s, returned %s", current_id, decl_name.c_str(), ast_dumper.GetCString());
1650 ClangExpressionDeclMap::ResolveUnknownTypes()
1652 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1653 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1655 ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext();
1657 for (size_t index = 0, num_entities = m_found_entities.GetSize();
1658 index < num_entities;
1661 ClangExpressionVariableSP entity = m_found_entities.GetVariableAtIndex(index);
1663 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1665 if (entity->m_flags & ClangExpressionVariable::EVUnknownType)
1667 const NamedDecl *named_decl = parser_vars->m_named_decl;
1668 const VarDecl *var_decl = dyn_cast<VarDecl>(named_decl);
1673 log->Printf("Entity of unknown type does not have a VarDecl");
1679 ASTDumper ast_dumper(const_cast<VarDecl*>(var_decl));
1680 log->Printf("Variable of unknown type now has Decl %s", ast_dumper.GetCString());
1683 QualType var_type = var_decl->getType();
1684 TypeFromParser parser_type(var_type.getAsOpaquePtr(), &var_decl->getASTContext());
1686 lldb::clang_type_t copied_type = m_ast_importer->CopyType(scratch_ast_context, &var_decl->getASTContext(), var_type.getAsOpaquePtr());
1691 log->Printf("ClangExpressionDeclMap::ResolveUnknownType - Couldn't import the type for a variable");
1693 return (bool) lldb::ClangExpressionVariableSP();
1696 TypeFromUser user_type(copied_type, scratch_ast_context);
1698 // parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType());
1699 parser_vars->m_lldb_value.SetClangType(user_type);
1700 parser_vars->m_parser_type = parser_type;
1702 entity->SetClangType(user_type);
1704 entity->m_flags &= ~(ClangExpressionVariable::EVUnknownType);
1712 ClangExpressionDeclMap::AddOneRegister (NameSearchContext &context,
1713 const RegisterInfo *reg_info,
1714 unsigned int current_id)
1716 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1718 ClangASTType clang_type = ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (m_ast_context,
1720 reg_info->byte_size * 8);
1725 log->Printf(" Tried to add a type for %s, but couldn't get one", context.m_decl_name.getAsString().c_str());
1729 TypeFromParser parser_clang_type (clang_type);
1731 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type);
1733 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1734 m_parser_vars->m_target_info.byte_order,
1735 m_parser_vars->m_target_info.address_byte_size));
1736 assert (entity.get());
1738 std::string decl_name(context.m_decl_name.getAsString());
1739 entity->SetName (ConstString (decl_name.c_str()));
1740 entity->SetRegisterInfo (reg_info);
1741 entity->EnableParserVars(GetParserID());
1742 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1743 parser_vars->m_parser_type = parser_clang_type;
1744 parser_vars->m_named_decl = var_decl;
1745 parser_vars->m_llvm_value = NULL;
1746 parser_vars->m_lldb_value.Clear();
1747 entity->m_flags |= ClangExpressionVariable::EVBareRegister;
1751 ASTDumper ast_dumper(var_decl);
1752 log->Printf(" CEDM::FEVD[%d] Added register %s, returned %s", current_id, context.m_decl_name.getAsString().c_str(), ast_dumper.GetCString());
1757 ClangExpressionDeclMap::AddOneFunction (NameSearchContext &context,
1760 unsigned int current_id)
1762 assert (m_parser_vars.get());
1764 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1766 NamedDecl *function_decl = NULL;
1767 const Address *fun_address = NULL;
1768 ClangASTType function_clang_type;
1770 bool is_indirect_function = false;
1774 Type *function_type = function->GetType();
1779 log->PutCString(" Skipped a function because it has no type");
1783 function_clang_type = function_type->GetClangFullType();
1785 if (!function_clang_type)
1788 log->PutCString(" Skipped a function because it has no Clang type");
1792 fun_address = &function->GetAddressRange().GetBaseAddress();
1794 ClangASTType copied_function_type = GuardedCopyType(function_clang_type);
1795 if (copied_function_type)
1797 function_decl = context.AddFunDecl(copied_function_type);
1803 log->Printf (" Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}",
1804 function_type->GetName().GetCString(),
1805 function_type->GetID());
1813 // We failed to copy the type we found
1816 log->Printf (" Failed to import the function type '%s' {0x%8.8" PRIx64 "} into the expression parser AST contenxt",
1817 function_type->GetName().GetCString(),
1818 function_type->GetID());
1826 fun_address = &symbol->GetAddress();
1827 function_decl = context.AddGenericFunDecl();
1828 is_indirect_function = symbol->IsIndirect();
1833 log->PutCString(" AddOneFunction called with no function and no symbol");
1837 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1839 lldb::addr_t load_addr = fun_address->GetCallableLoadAddress(target, is_indirect_function);
1841 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (),
1842 m_parser_vars->m_target_info.byte_order,
1843 m_parser_vars->m_target_info.address_byte_size));
1844 assert (entity.get());
1846 std::string decl_name(context.m_decl_name.getAsString());
1847 entity->SetName(ConstString(decl_name.c_str()));
1848 entity->SetClangType (function_clang_type);
1849 entity->EnableParserVars(GetParserID());
1851 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1853 if (load_addr != LLDB_INVALID_ADDRESS)
1855 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1856 parser_vars->m_lldb_value.GetScalar() = load_addr;
1860 // We have to try finding a file address.
1862 lldb::addr_t file_addr = fun_address->GetFileAddress();
1864 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress);
1865 parser_vars->m_lldb_value.GetScalar() = file_addr;
1869 parser_vars->m_named_decl = function_decl;
1870 parser_vars->m_llvm_value = NULL;
1874 ASTDumper ast_dumper(function_decl);
1878 fun_address->Dump(&ss, m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), Address::DumpStyleResolvedDescription);
1880 log->Printf(" CEDM::FEVD[%u] Found %s function %s (description %s), returned %s",
1882 (function ? "specific" : "generic"),
1885 ast_dumper.GetCString());
1890 ClangExpressionDeclMap::CopyClassType(TypeFromUser &ut,
1891 unsigned int current_id)
1893 ClangASTType copied_clang_type = GuardedCopyType(ut);
1895 if (!copied_clang_type)
1897 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1900 log->Printf("ClangExpressionDeclMap::CopyClassType - Couldn't import the type");
1902 return TypeFromParser();
1905 if (copied_clang_type.IsAggregateType() && copied_clang_type.GetCompleteType ())
1907 ClangASTType void_clang_type = ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid);
1908 ClangASTType void_ptr_clang_type = void_clang_type.GetPointerType();
1910 ClangASTType method_type = ClangASTContext::CreateFunctionType (m_ast_context,
1912 &void_ptr_clang_type,
1915 copied_clang_type.GetTypeQualifiers());
1917 const bool is_virtual = false;
1918 const bool is_static = false;
1919 const bool is_inline = false;
1920 const bool is_explicit = false;
1921 const bool is_attr_used = true;
1922 const bool is_artificial = false;
1924 copied_clang_type.AddMethodToCXXRecordType ("$__lldb_expr",
1926 lldb::eAccessPublic,
1935 return TypeFromParser(copied_clang_type);
1939 ClangExpressionDeclMap::AddOneType(NameSearchContext &context,
1941 unsigned int current_id)
1943 ClangASTType copied_clang_type = GuardedCopyType(ut);
1945 if (!copied_clang_type)
1947 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1950 log->Printf("ClangExpressionDeclMap::AddOneType - Couldn't import the type");
1955 context.AddTypeDecl(copied_clang_type);