blob: a0ea8ae730fcd3703b78dda083fa8d7da6717abd [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangExpressionDeclMap.cpp -----------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Sean Callanan4dbb2712015-09-25 20:35:58 +000010#include "ClangExpressionDeclMap.h"
11
12#include "ASTDumper.h"
13#include "ClangASTSource.h"
14#include "ClangModulesDeclVendor.h"
15#include "ClangPersistentVariables.h"
16
Sean Callanan80c97592015-05-01 00:47:29 +000017#include "clang/AST/ASTConsumer.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000018#include "clang/AST/ASTContext.h"
Sean Callanane2ef6e32010-09-23 03:01:22 +000019#include "clang/AST/DeclarationName.h"
Sean Callanan530782942010-11-01 23:22:47 +000020#include "clang/AST/Decl.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/lldb-private.h"
22#include "lldb/Core/Address.h"
Sean Callananea22d422010-07-16 00:09:46 +000023#include "lldb/Core/Error.h"
Sean Callananf06ba8d2010-06-23 00:47:48 +000024#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Module.h"
Greg Clayton9191db42013-10-21 18:40:51 +000026#include "lldb/Core/ModuleSpec.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000027#include "lldb/Core/RegisterValue.h"
Sean Callanane4ec90e2010-12-16 03:17:46 +000028#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan9b3569b2011-12-10 03:12:34 +000029#include "lldb/Core/ValueObjectVariable.h"
Sean Callanan96d27302013-04-11 00:09:05 +000030#include "lldb/Expression/Materializer.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000031#include "lldb/Host/Endian.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Symbol/ClangASTContext.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000033#include "lldb/Symbol/CompilerDecl.h"
Greg Clayton99558cc42015-08-24 23:46:31 +000034#include "lldb/Symbol/CompilerDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Symbol/CompileUnit.h"
36#include "lldb/Symbol/Function.h"
37#include "lldb/Symbol/ObjectFile.h"
38#include "lldb/Symbol/SymbolContext.h"
Siva Chandra9293fc42016-01-07 23:32:34 +000039#include "lldb/Symbol/SymbolFile.h"
Sean Callanan503aa522011-10-12 00:12:34 +000040#include "lldb/Symbol/SymbolVendor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "lldb/Symbol/Type.h"
42#include "lldb/Symbol/TypeList.h"
43#include "lldb/Symbol/Variable.h"
44#include "lldb/Symbol/VariableList.h"
Sean Callanana0d56432014-11-11 02:49:44 +000045#include "lldb/Target/CPPLanguageRuntime.h"
Sean Callanan1d180662010-07-20 23:31:16 +000046#include "lldb/Target/ExecutionContext.h"
Sean Callanane0b23b52012-11-15 02:02:04 +000047#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callananea22d422010-07-16 00:09:46 +000048#include "lldb/Target/Process.h"
Sean Callananf4b9bd32010-10-05 20:18:48 +000049#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000050#include "lldb/Target/StackFrame.h"
Sean Callanan1d180662010-07-20 23:31:16 +000051#include "lldb/Target/Target.h"
Jim Ingham895c9822010-12-07 01:56:02 +000052#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053
Siva Chandra0f4873d2015-09-03 23:27:10 +000054#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
55
Greg Clayton83c5cd92010-11-14 22:13:40 +000056using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057using namespace lldb_private;
58using namespace clang;
59
Siva Chandra03ff5c82016-02-05 19:10:04 +000060namespace
61{
62 const char *g_lldb_local_vars_namespace_cstr = "$__lldb_local_vars";
63} // anonymous namespace
64
Sean Callanan9fda9d22015-10-03 09:09:01 +000065ClangExpressionDeclMap::ClangExpressionDeclMap (bool keep_result_in_memory,
66 Materializer::PersistentVariableDelegate *result_delegate,
67 ExecutionContext &exe_ctx) :
Sean Callanan1ee44b72011-10-29 01:58:46 +000068 ClangASTSource (exe_ctx.GetTargetSP()),
Greg Clayton7b462cc2010-10-15 22:48:33 +000069 m_found_entities (),
70 m_struct_members (),
Stephen Wilson71c21d12011-04-11 19:41:40 +000071 m_keep_result_in_memory (keep_result_in_memory),
Sean Callanan9fda9d22015-10-03 09:09:01 +000072 m_result_delegate (result_delegate),
Sean Callanan979f74d2010-12-03 01:38:59 +000073 m_parser_vars (),
Stephen Wilson71c21d12011-04-11 19:41:40 +000074 m_struct_vars ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075{
Sean Callanan979f74d2010-12-03 01:38:59 +000076 EnableStructVars();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077}
78
79ClangExpressionDeclMap::~ClangExpressionDeclMap()
Sean Callanan979f74d2010-12-03 01:38:59 +000080{
Sean Callanane3aef1d2011-10-12 22:20:02 +000081 // Note: The model is now that the parser's AST context and all associated
82 // data does not vanish until the expression has been executed. This means
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000083 // that valuable lookup data (like namespaces) doesn't vanish, but
84
Sean Callanane3aef1d2011-10-12 22:20:02 +000085 DidParse();
Sean Callanan979f74d2010-12-03 01:38:59 +000086 DisableStructVars();
87}
Sean Callananbe3a1b12010-10-26 00:31:56 +000088
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000089bool
Sean Callanan96d27302013-04-11 00:09:05 +000090ClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx,
91 Materializer *materializer)
Sean Callanan8106d802013-03-08 20:04:57 +000092{
93 ClangASTMetrics::ClearLocalCounters();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000094
Sean Callanan979f74d2010-12-03 01:38:59 +000095 EnableParserVars();
Sean Callanan933693b2012-02-10 01:22:05 +000096 m_parser_vars->m_exe_ctx = exe_ctx;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000097
Greg Claytonc14ee322011-09-22 04:58:26 +000098 Target *target = exe_ctx.GetTargetPtr();
99 if (exe_ctx.GetFramePtr())
100 m_parser_vars->m_sym_ctx = exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything);
Sean Callanand4fac252013-02-21 22:01:43 +0000101 else if (exe_ctx.GetThreadPtr() && exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0))
Greg Claytonc14ee322011-09-22 04:58:26 +0000102 m_parser_vars->m_sym_ctx = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext(lldb::eSymbolContextEverything);
103 else if (exe_ctx.GetProcessPtr())
Greg Clayton4d122c42011-09-17 08:33:22 +0000104 {
Greg Clayton72310352013-02-23 04:12:47 +0000105 m_parser_vars->m_sym_ctx.Clear(true);
Greg Claytonc14ee322011-09-22 04:58:26 +0000106 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
Greg Clayton4d122c42011-09-17 08:33:22 +0000107 }
Greg Claytonc14ee322011-09-22 04:58:26 +0000108 else if (target)
Greg Clayton4d122c42011-09-17 08:33:22 +0000109 {
Greg Clayton72310352013-02-23 04:12:47 +0000110 m_parser_vars->m_sym_ctx.Clear(true);
Greg Claytonc14ee322011-09-22 04:58:26 +0000111 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
Greg Clayton4d122c42011-09-17 08:33:22 +0000112 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000113
Greg Claytonc14ee322011-09-22 04:58:26 +0000114 if (target)
115 {
Sean Callananb92bd752015-10-01 16:28:02 +0000116 m_parser_vars->m_persistent_vars = llvm::cast<ClangPersistentVariables>(target->GetPersistentExpressionStateForLanguage(eLanguageTypeC));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000117
Greg Claytonc14ee322011-09-22 04:58:26 +0000118 if (!target->GetScratchClangASTContext())
119 return false;
120 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000121
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000122 m_parser_vars->m_target_info = GetTargetInfo();
Sean Callanan96d27302013-04-11 00:09:05 +0000123 m_parser_vars->m_materializer = materializer;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000124
Sean Callananb9951192011-08-01 18:18:33 +0000125 return true;
Sean Callanan979f74d2010-12-03 01:38:59 +0000126}
127
Sean Callanan96d27302013-04-11 00:09:05 +0000128void
Sean Callanan80c97592015-05-01 00:47:29 +0000129ClangExpressionDeclMap::InstallCodeGenerator (clang::ASTConsumer *code_gen)
130{
131 assert(m_parser_vars);
132 m_parser_vars->m_code_gen = code_gen;
133}
134
135void
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000136ClangExpressionDeclMap::DidParse()
Sean Callanan979f74d2010-12-03 01:38:59 +0000137{
Greg Clayton5160ce52013-03-27 23:08:40 +0000138 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8106d802013-03-08 20:04:57 +0000139
140 if (log)
141 ClangASTMetrics::DumpCounters(log);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000142
Sean Callanan979f74d2010-12-03 01:38:59 +0000143 if (m_parser_vars.get())
144 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000145 for (size_t entity_index = 0, num_entities = m_found_entities.GetSize();
Sean Callanan979f74d2010-12-03 01:38:59 +0000146 entity_index < num_entities;
147 ++entity_index)
148 {
Sean Callananbc8ac342015-09-04 20:49:51 +0000149 ExpressionVariableSP var_sp(m_found_entities.GetVariableAtIndex(entity_index));
Jim Ingham28eb5712012-10-12 17:34:26 +0000150 if (var_sp)
Sean Callanan3820e9c2015-09-08 22:23:39 +0000151 llvm::cast<ClangExpressionVariable>(var_sp.get())->DisableParserVars(GetParserID());
Sean Callanan979f74d2010-12-03 01:38:59 +0000152 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000153
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000154 for (size_t pvar_index = 0, num_pvars = m_parser_vars->m_persistent_vars->GetSize();
Sean Callanan979f74d2010-12-03 01:38:59 +0000155 pvar_index < num_pvars;
156 ++pvar_index)
157 {
Sean Callananbc8ac342015-09-04 20:49:51 +0000158 ExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index));
Sean Callanan85aabaf2015-09-14 21:03:44 +0000159 if (ClangExpressionVariable *clang_var = llvm::dyn_cast<ClangExpressionVariable>(pvar_sp.get()))
160 clang_var->DisableParserVars(GetParserID());
Sean Callanan979f74d2010-12-03 01:38:59 +0000161 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000162
Sean Callanan979f74d2010-12-03 01:38:59 +0000163 DisableParserVars();
Sean Callanan6b1b9532010-10-08 01:58:41 +0000164 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165}
166
Sean Callanan549c9f72010-07-13 21:41:46 +0000167// Interface for IRForTarget
168
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000169ClangExpressionDeclMap::TargetInfo
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000170ClangExpressionDeclMap::GetTargetInfo()
171{
172 assert (m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000173
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000174 TargetInfo ret;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000175
Sean Callanan933693b2012-02-10 01:22:05 +0000176 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
177
178 Process *process = exe_ctx.GetProcessPtr();
179 if (process)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000180 {
Sean Callanan933693b2012-02-10 01:22:05 +0000181 ret.byte_order = process->GetByteOrder();
182 ret.address_byte_size = process->GetAddressByteSize();
183 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000184 else
Sean Callanan933693b2012-02-10 01:22:05 +0000185 {
186 Target *target = exe_ctx.GetTargetPtr();
187 if (target)
Greg Claytonc14ee322011-09-22 04:58:26 +0000188 {
Sean Callanan933693b2012-02-10 01:22:05 +0000189 ret.byte_order = target->GetArchitecture().GetByteOrder();
190 ret.address_byte_size = target->GetArchitecture().GetAddressByteSize();
Greg Claytonc14ee322011-09-22 04:58:26 +0000191 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000192 }
Sean Callanan933693b2012-02-10 01:22:05 +0000193
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000194 return ret;
195}
196
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000197bool
198ClangExpressionDeclMap::AddPersistentVariable
Greg Clayton7b462cc2010-10-15 22:48:33 +0000199(
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000200 const NamedDecl *decl,
201 const ConstString &name,
Sean Callanan92adcac2011-01-13 08:53:35 +0000202 TypeFromParser parser_type,
203 bool is_result,
204 bool is_lvalue
Greg Clayton7b462cc2010-10-15 22:48:33 +0000205)
Sean Callanan2235f322010-08-11 03:57:18 +0000206{
Sean Callanan979f74d2010-12-03 01:38:59 +0000207 assert (m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000208
Greg Claytonf73034f2015-09-08 18:15:05 +0000209 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(parser_type.GetTypeSystem());
210 if (ast == nullptr)
211 return false;
212
Sean Callanan1582ee62013-04-18 22:06:33 +0000213 if (m_parser_vars->m_materializer && is_result)
214 {
215 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000216
Sean Callanan1582ee62013-04-18 22:06:33 +0000217 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
218 Target *target = exe_ctx.GetTargetPtr();
Greg Claytonf73034f2015-09-08 18:15:05 +0000219 if (target == nullptr)
Sean Callanan1582ee62013-04-18 22:06:33 +0000220 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000221
Greg Claytond8d4a572015-08-11 21:38:15 +0000222 ClangASTContext *context(target->GetScratchClangASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000223
Sean Callanana3444ff2015-11-10 22:54:42 +0000224 TypeFromUser user_type(m_ast_importer_sp->DeportType(context->getASTContext(),
225 ast->getASTContext(),
226 parser_type.GetOpaqueQualType()),
Sean Callanan1582ee62013-04-18 22:06:33 +0000227 context);
Sean Callanan9fda9d22015-10-03 09:09:01 +0000228
229 uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(user_type,
230 is_lvalue,
231 m_keep_result_in_memory,
232 m_result_delegate,
233 err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000234
Sean Callanan9301ec12015-10-01 23:07:06 +0000235 ClangExpressionVariable *var = new ClangExpressionVariable(exe_ctx.GetBestExecutionContextScope(),
236 name,
237 user_type,
238 m_parser_vars->m_target_info.byte_order,
239 m_parser_vars->m_target_info.address_byte_size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000240
Sean Callanan9301ec12015-10-01 23:07:06 +0000241 m_found_entities.AddNewlyConstructedVariable(var);
242
Sean Callananbc8ac342015-09-04 20:49:51 +0000243 var->EnableParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000244
Sean Callananbc8ac342015-09-04 20:49:51 +0000245 ClangExpressionVariable::ParserVars *parser_vars = var->GetParserVars(GetParserID());
Sean Callanan1582ee62013-04-18 22:06:33 +0000246
247 parser_vars->m_named_decl = decl;
248 parser_vars->m_parser_type = parser_type;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000249
Sean Callananbc8ac342015-09-04 20:49:51 +0000250 var->EnableJITVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000251
Sean Callananbc8ac342015-09-04 20:49:51 +0000252 ClangExpressionVariable::JITVars *jit_vars = var->GetJITVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000253
Sean Callanan1582ee62013-04-18 22:06:33 +0000254 jit_vars->m_offset = offset;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000255
Sean Callanan1582ee62013-04-18 22:06:33 +0000256 return true;
257 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000258
Greg Clayton5160ce52013-03-27 23:08:40 +0000259 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan933693b2012-02-10 01:22:05 +0000260 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
261 Target *target = exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +0000262 if (target == NULL)
263 return false;
264
Greg Claytond8d4a572015-08-11 21:38:15 +0000265 ClangASTContext *context(target->GetScratchClangASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000266
Sean Callanana3444ff2015-11-10 22:54:42 +0000267 TypeFromUser user_type(m_ast_importer_sp->DeportType(context->getASTContext(),
268 ast->getASTContext(),
269 parser_type.GetOpaqueQualType()),
Sean Callanane1175b72011-01-13 21:23:32 +0000270 context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000271
Sean Callanan00f43622011-11-18 03:28:09 +0000272 if (!user_type.GetOpaqueQualType())
273 {
274 if (log)
275 log->Printf("Persistent variable's type wasn't copied successfully");
276 return false;
277 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000278
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000279 if (!m_parser_vars->m_target_info.IsValid())
280 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000281
Sean Callanan9301ec12015-10-01 23:07:06 +0000282 ClangExpressionVariable *var = llvm::cast<ClangExpressionVariable>(m_parser_vars->m_persistent_vars->CreatePersistentVariable (exe_ctx.GetBestExecutionContextScope (),
283 name,
284 user_type,
285 m_parser_vars->m_target_info.byte_order,
286 m_parser_vars->m_target_info.address_byte_size).get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000287
Sean Callananbc8ac342015-09-04 20:49:51 +0000288 if (!var)
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000289 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000290
Sean Callananbc8ac342015-09-04 20:49:51 +0000291 var->m_frozen_sp->SetHasCompleteType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000292
Sean Callanan92adcac2011-01-13 08:53:35 +0000293 if (is_result)
Sean Callananbc8ac342015-09-04 20:49:51 +0000294 var->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry;
Sean Callanan92adcac2011-01-13 08:53:35 +0000295 else
Sean Callananbc8ac342015-09-04 20:49:51 +0000296 var->m_flags |= ClangExpressionVariable::EVKeepInTarget; // explicitly-declared persistent variables should persist
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000297
Sean Callanan92adcac2011-01-13 08:53:35 +0000298 if (is_lvalue)
299 {
Sean Callananbc8ac342015-09-04 20:49:51 +0000300 var->m_flags |= ClangExpressionVariable::EVIsProgramReference;
Sean Callanan92adcac2011-01-13 08:53:35 +0000301 }
302 else
303 {
Sean Callananbc8ac342015-09-04 20:49:51 +0000304 var->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
305 var->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
Sean Callanan92adcac2011-01-13 08:53:35 +0000306 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000307
Sean Callanan35005f72013-04-12 18:10:34 +0000308 if (m_keep_result_in_memory)
309 {
Sean Callananbc8ac342015-09-04 20:49:51 +0000310 var->m_flags |= ClangExpressionVariable::EVKeepInTarget;
Sean Callanan35005f72013-04-12 18:10:34 +0000311 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000312
Sean Callanan92adcac2011-01-13 08:53:35 +0000313 if (log)
Sean Callananbc8ac342015-09-04 20:49:51 +0000314 log->Printf("Created persistent variable with flags 0x%hx", var->m_flags);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000315
Sean Callananbc8ac342015-09-04 20:49:51 +0000316 var->EnableParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000317
Sean Callananbc8ac342015-09-04 20:49:51 +0000318 ClangExpressionVariable::ParserVars *parser_vars = var->GetParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000319
Sean Callanan3c495c12013-01-15 23:29:36 +0000320 parser_vars->m_named_decl = decl;
321 parser_vars->m_parser_type = parser_type;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000322
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000323 return true;
Sean Callanan2235f322010-08-11 03:57:18 +0000324}
325
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000326bool
327ClangExpressionDeclMap::AddValueToStruct
Greg Clayton7b462cc2010-10-15 22:48:33 +0000328(
Sean Callanancc427fa2011-07-30 02:42:06 +0000329 const NamedDecl *decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +0000330 const ConstString &name,
331 llvm::Value *value,
332 size_t size,
Zachary Turnera746e8e2014-07-02 17:24:07 +0000333 lldb::offset_t alignment
Greg Clayton7b462cc2010-10-15 22:48:33 +0000334)
Sean Callanan549c9f72010-07-13 21:41:46 +0000335{
Sean Callanan979f74d2010-12-03 01:38:59 +0000336 assert (m_struct_vars.get());
337 assert (m_parser_vars.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000338
Sean Callanandf667652013-04-11 02:05:11 +0000339 bool is_persistent_variable = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000340
Greg Clayton5160ce52013-03-27 23:08:40 +0000341 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000342
Sean Callanan979f74d2010-12-03 01:38:59 +0000343 m_struct_vars->m_struct_laid_out = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000344
Sean Callananbc8ac342015-09-04 20:49:51 +0000345 if (ClangExpressionVariable::FindVariableInList(m_struct_members, decl, GetParserID()))
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000346 return true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000347
Sean Callananbc8ac342015-09-04 20:49:51 +0000348 ClangExpressionVariable *var(ClangExpressionVariable::FindVariableInList(m_found_entities, decl, GetParserID()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000349
Sean Callananbc8ac342015-09-04 20:49:51 +0000350 if (!var)
Sean Callanandf667652013-04-11 02:05:11 +0000351 {
Sean Callananbc8ac342015-09-04 20:49:51 +0000352 var = ClangExpressionVariable::FindVariableInList(*m_parser_vars->m_persistent_vars, decl, GetParserID());
Sean Callanandf667652013-04-11 02:05:11 +0000353 is_persistent_variable = true;
354 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000355
Sean Callananbc8ac342015-09-04 20:49:51 +0000356 if (!var)
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000357 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000358
Sean Callanan823bb4c2010-08-30 22:17:16 +0000359 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000360 log->Printf("Adding value for (NamedDecl*)%p [%s - %s] to the structure",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000361 static_cast<const void*>(decl), name.GetCString(),
Sean Callananbc8ac342015-09-04 20:49:51 +0000362 var->GetName().GetCString());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000363
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000364 // We know entity->m_parser_vars is valid because we used a parser variable
365 // to find it
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000366
Sean Callanan3820e9c2015-09-08 22:23:39 +0000367 ClangExpressionVariable::ParserVars *parser_vars = llvm::cast<ClangExpressionVariable>(var)->GetParserVars(GetParserID());
Sean Callanan3c495c12013-01-15 23:29:36 +0000368
369 parser_vars->m_llvm_value = value;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000370
Sean Callanan3820e9c2015-09-08 22:23:39 +0000371 if (ClangExpressionVariable::JITVars *jit_vars = llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID()))
Sean Callanan1582ee62013-04-18 22:06:33 +0000372 {
373 // We already laid this out; do not touch
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000374
Sean Callanan1582ee62013-04-18 22:06:33 +0000375 if (log)
376 log->Printf("Already placed at 0x%llx", (unsigned long long)jit_vars->m_offset);
377 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000378
Sean Callanan3820e9c2015-09-08 22:23:39 +0000379 llvm::cast<ClangExpressionVariable>(var)->EnableJITVars(GetParserID());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000380
Sean Callanan3820e9c2015-09-08 22:23:39 +0000381 ClangExpressionVariable::JITVars *jit_vars = llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID());
Sean Callanan3c495c12013-01-15 23:29:36 +0000382
383 jit_vars->m_alignment = alignment;
384 jit_vars->m_size = size;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000385
Sean Callananbc8ac342015-09-04 20:49:51 +0000386 m_struct_members.AddVariable(var->shared_from_this());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000387
Sean Callanandf667652013-04-11 02:05:11 +0000388 if (m_parser_vars->m_materializer)
389 {
Sean Callanan3dd6a422013-04-11 21:16:36 +0000390 uint32_t offset = 0;
391
Sean Callanandf667652013-04-11 02:05:11 +0000392 Error err;
393
394 if (is_persistent_variable)
395 {
Sean Callananbc8ac342015-09-04 20:49:51 +0000396 ExpressionVariableSP var_sp(var->shared_from_this());
Sean Callanan9fda9d22015-10-03 09:09:01 +0000397 offset = m_parser_vars->m_materializer->AddPersistentVariable(var_sp, nullptr, err);
Sean Callanandf667652013-04-11 02:05:11 +0000398 }
399 else
400 {
401 if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym)
Sean Callanan3dd6a422013-04-11 21:16:36 +0000402 offset = m_parser_vars->m_materializer->AddSymbol(*sym, err);
Sean Callananbc8ac342015-09-04 20:49:51 +0000403 else if (const RegisterInfo *reg_info = var->GetRegisterInfo())
Sean Callanan3dd6a422013-04-11 21:16:36 +0000404 offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err);
Sean Callanandf667652013-04-11 02:05:11 +0000405 else if (parser_vars->m_lldb_var)
Sean Callanan3dd6a422013-04-11 21:16:36 +0000406 offset = m_parser_vars->m_materializer->AddVariable(parser_vars->m_lldb_var, err);
Sean Callanandf667652013-04-11 02:05:11 +0000407 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000408
Sean Callanan3dd6a422013-04-11 21:16:36 +0000409 if (!err.Success())
410 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000411
Sean Callanan3dd6a422013-04-11 21:16:36 +0000412 if (log)
413 log->Printf("Placed at 0x%llx", (unsigned long long)offset);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000414
Sean Callanan3dd6a422013-04-11 21:16:36 +0000415 jit_vars->m_offset = offset; // TODO DoStructLayout() should not change this.
Sean Callanandf667652013-04-11 02:05:11 +0000416 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000417
Sean Callanan549c9f72010-07-13 21:41:46 +0000418 return true;
419}
420
421bool
422ClangExpressionDeclMap::DoStructLayout ()
423{
Sean Callanan979f74d2010-12-03 01:38:59 +0000424 assert (m_struct_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000425
Sean Callanan979f74d2010-12-03 01:38:59 +0000426 if (m_struct_vars->m_struct_laid_out)
Sean Callanan549c9f72010-07-13 21:41:46 +0000427 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000428
Sean Callanan14b1bae2013-04-16 23:25:35 +0000429 if (!m_parser_vars->m_materializer)
430 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000431
Sean Callanan14b1bae2013-04-16 23:25:35 +0000432 m_struct_vars->m_struct_alignment = m_parser_vars->m_materializer->GetStructAlignment();
433 m_struct_vars->m_struct_size = m_parser_vars->m_materializer->GetStructByteSize();
Sean Callanan979f74d2010-12-03 01:38:59 +0000434 m_struct_vars->m_struct_laid_out = true;
Sean Callanan549c9f72010-07-13 21:41:46 +0000435 return true;
436}
437
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000438bool ClangExpressionDeclMap::GetStructInfo
Greg Clayton7b462cc2010-10-15 22:48:33 +0000439(
440 uint32_t &num_elements,
441 size_t &size,
Zachary Turnera746e8e2014-07-02 17:24:07 +0000442 lldb::offset_t &alignment
Greg Clayton7b462cc2010-10-15 22:48:33 +0000443)
Sean Callanan549c9f72010-07-13 21:41:46 +0000444{
Sean Callanan979f74d2010-12-03 01:38:59 +0000445 assert (m_struct_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000446
Sean Callanan979f74d2010-12-03 01:38:59 +0000447 if (!m_struct_vars->m_struct_laid_out)
Sean Callanan549c9f72010-07-13 21:41:46 +0000448 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000449
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000450 num_elements = m_struct_members.GetSize();
Sean Callanan979f74d2010-12-03 01:38:59 +0000451 size = m_struct_vars->m_struct_size;
452 alignment = m_struct_vars->m_struct_alignment;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000453
Sean Callanan549c9f72010-07-13 21:41:46 +0000454 return true;
455}
456
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000457bool
458ClangExpressionDeclMap::GetStructElement
Greg Clayton7b462cc2010-10-15 22:48:33 +0000459(
Sean Callanancc427fa2011-07-30 02:42:06 +0000460 const NamedDecl *&decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +0000461 llvm::Value *&value,
Zachary Turnera746e8e2014-07-02 17:24:07 +0000462 lldb::offset_t &offset,
Greg Clayton7b462cc2010-10-15 22:48:33 +0000463 ConstString &name,
464 uint32_t index
465)
Sean Callanan549c9f72010-07-13 21:41:46 +0000466{
Sean Callanan979f74d2010-12-03 01:38:59 +0000467 assert (m_struct_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000468
Sean Callanan979f74d2010-12-03 01:38:59 +0000469 if (!m_struct_vars->m_struct_laid_out)
Sean Callanan549c9f72010-07-13 21:41:46 +0000470 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000471
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000472 if (index >= m_struct_members.GetSize())
Sean Callanan549c9f72010-07-13 21:41:46 +0000473 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000474
Sean Callananbc8ac342015-09-04 20:49:51 +0000475 ExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000476
Sean Callanan3c495c12013-01-15 23:29:36 +0000477 if (!member_sp)
478 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000479
Sean Callanan3820e9c2015-09-08 22:23:39 +0000480 ClangExpressionVariable::ParserVars *parser_vars = llvm::cast<ClangExpressionVariable>(member_sp.get())->GetParserVars(GetParserID());
481 ClangExpressionVariable::JITVars *jit_vars = llvm::cast<ClangExpressionVariable>(member_sp.get())->GetJITVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000482
Sean Callanan3c495c12013-01-15 23:29:36 +0000483 if (!parser_vars ||
484 !jit_vars ||
Sean Callanan6f3e5ad2012-04-12 16:58:26 +0000485 !member_sp->GetValueObject())
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000486 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000487
Sean Callanan3c495c12013-01-15 23:29:36 +0000488 decl = parser_vars->m_named_decl;
489 value = parser_vars->m_llvm_value;
490 offset = jit_vars->m_offset;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000491 name = member_sp->GetName();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000492
Sean Callanan549c9f72010-07-13 21:41:46 +0000493 return true;
494}
495
Sean Callanan7ea35012010-07-27 21:39:39 +0000496bool
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000497ClangExpressionDeclMap::GetFunctionInfo
Greg Clayton7b462cc2010-10-15 22:48:33 +0000498(
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000499 const NamedDecl *decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +0000500 uint64_t &ptr
501)
Sean Callanan4edba2d2010-07-27 02:07:53 +0000502{
Sean Callananbc8ac342015-09-04 20:49:51 +0000503 ClangExpressionVariable *entity(ClangExpressionVariable::FindVariableInList(m_found_entities, decl, GetParserID()));
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000504
Sean Callananbc8ac342015-09-04 20:49:51 +0000505 if (!entity)
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000506 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000507
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000508 // We know m_parser_vars is valid since we searched for the variable by
509 // its NamedDecl
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000510
Sean Callananbc8ac342015-09-04 20:49:51 +0000511 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
Sean Callanan3c495c12013-01-15 23:29:36 +0000512
Greg Clayton57ee3062013-07-11 22:46:58 +0000513 ptr = parser_vars->m_lldb_value.GetScalar().ULongLong();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000514
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000515 return true;
Sean Callanan4edba2d2010-07-27 02:07:53 +0000516}
517
Greg Clayton084db102011-06-23 04:25:29 +0000518addr_t
Greg Clayton9191db42013-10-21 18:40:51 +0000519ClangExpressionDeclMap::GetSymbolAddress (Target &target,
520 Process *process,
521 const ConstString &name,
522 lldb::SymbolType symbol_type,
Deepak Panickald66b50c2013-10-22 12:27:43 +0000523 lldb_private::Module *module)
Sean Callananc3a16002011-01-17 23:42:46 +0000524{
Sean Callananc3a16002011-01-17 23:42:46 +0000525 SymbolContextList sc_list;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000526
Greg Clayton9191db42013-10-21 18:40:51 +0000527 if (module)
528 module->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
529 else
530 target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000531
Greg Clayton084db102011-06-23 04:25:29 +0000532 const uint32_t num_matches = sc_list.GetSize();
533 addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
534
Sean Callanan947ccc72011-12-01 02:04:16 +0000535 for (uint32_t i=0; i<num_matches && (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS); i++)
Greg Clayton084db102011-06-23 04:25:29 +0000536 {
537 SymbolContext sym_ctx;
538 sc_list.GetContextAtIndex(i, sym_ctx);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000539
Greg Clayton358cf1e2015-06-25 21:46:34 +0000540 const Address sym_address = sym_ctx.symbol->GetAddress();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000541
Greg Clayton358cf1e2015-06-25 21:46:34 +0000542 if (!sym_address.IsValid())
Sean Callanan116ace22013-04-30 00:21:42 +0000543 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000544
Greg Clayton358cf1e2015-06-25 21:46:34 +0000545 switch (sym_ctx.symbol->GetType())
Greg Clayton084db102011-06-23 04:25:29 +0000546 {
Greg Clayton358cf1e2015-06-25 21:46:34 +0000547 case eSymbolTypeCode:
548 case eSymbolTypeTrampoline:
549 symbol_load_addr = sym_address.GetCallableLoadAddress (&target);
550 break;
Matt Kopec00049b82013-02-27 20:13:38 +0000551
Greg Clayton358cf1e2015-06-25 21:46:34 +0000552 case eSymbolTypeResolver:
553 symbol_load_addr = sym_address.GetCallableLoadAddress (&target, true);
554 break;
Matt Kopec00049b82013-02-27 20:13:38 +0000555
Greg Clayton358cf1e2015-06-25 21:46:34 +0000556 case eSymbolTypeReExported:
557 {
558 ConstString reexport_name = sym_ctx.symbol->GetReExportedSymbolName();
559 if (reexport_name)
Greg Clayton9191db42013-10-21 18:40:51 +0000560 {
Greg Clayton358cf1e2015-06-25 21:46:34 +0000561 ModuleSP reexport_module_sp;
562 ModuleSpec reexport_module_spec;
563 reexport_module_spec.GetPlatformFileSpec() = sym_ctx.symbol->GetReExportedSymbolSharedLibrary();
564 if (reexport_module_spec.GetPlatformFileSpec())
Greg Clayton9191db42013-10-21 18:40:51 +0000565 {
Greg Clayton358cf1e2015-06-25 21:46:34 +0000566 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
567 if (!reexport_module_sp)
Greg Clayton9191db42013-10-21 18:40:51 +0000568 {
Greg Clayton358cf1e2015-06-25 21:46:34 +0000569 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
Greg Clayton9191db42013-10-21 18:40:51 +0000570 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
Greg Clayton9191db42013-10-21 18:40:51 +0000571 }
Greg Clayton9191db42013-10-21 18:40:51 +0000572 }
Greg Clayton358cf1e2015-06-25 21:46:34 +0000573 symbol_load_addr = GetSymbolAddress(target, process, sym_ctx.symbol->GetReExportedSymbolName(), symbol_type, reexport_module_sp.get());
Greg Clayton9191db42013-10-21 18:40:51 +0000574 }
Greg Clayton358cf1e2015-06-25 21:46:34 +0000575 }
576 break;
Greg Clayton9191db42013-10-21 18:40:51 +0000577
Greg Clayton358cf1e2015-06-25 21:46:34 +0000578 case eSymbolTypeData:
579 case eSymbolTypeRuntime:
580 case eSymbolTypeVariable:
581 case eSymbolTypeLocal:
582 case eSymbolTypeParam:
583 case eSymbolTypeInvalid:
584 case eSymbolTypeAbsolute:
585 case eSymbolTypeException:
586 case eSymbolTypeSourceFile:
587 case eSymbolTypeHeaderFile:
588 case eSymbolTypeObjectFile:
589 case eSymbolTypeCommonBlock:
590 case eSymbolTypeBlock:
591 case eSymbolTypeVariableType:
592 case eSymbolTypeLineEntry:
593 case eSymbolTypeLineHeader:
594 case eSymbolTypeScopeBegin:
595 case eSymbolTypeScopeEnd:
596 case eSymbolTypeAdditional:
597 case eSymbolTypeCompiler:
598 case eSymbolTypeInstrumentation:
599 case eSymbolTypeUndefined:
600 case eSymbolTypeObjCClass:
601 case eSymbolTypeObjCMetaClass:
602 case eSymbolTypeObjCIVar:
603 symbol_load_addr = sym_address.GetLoadAddress (&target);
604 break;
Greg Clayton084db102011-06-23 04:25:29 +0000605 }
606 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000607
Sean Callanane0b23b52012-11-15 02:02:04 +0000608 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process)
609 {
610 ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000611
Sean Callanane0b23b52012-11-15 02:02:04 +0000612 if (runtime)
613 {
614 symbol_load_addr = runtime->LookupRuntimeSymbol(name);
615 }
616 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000617
Greg Clayton084db102011-06-23 04:25:29 +0000618 return symbol_load_addr;
Sean Callananc3a16002011-01-17 23:42:46 +0000619}
620
Greg Clayton084db102011-06-23 04:25:29 +0000621addr_t
Sean Callanan947ccc72011-12-01 02:04:16 +0000622ClangExpressionDeclMap::GetSymbolAddress (const ConstString &name, lldb::SymbolType symbol_type)
Sean Callanand9ca42a2011-05-08 02:21:26 +0000623{
624 assert (m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000625
Sean Callanan933693b2012-02-10 01:22:05 +0000626 if (!m_parser_vars->m_exe_ctx.GetTargetPtr())
Sean Callanand9ca42a2011-05-08 02:21:26 +0000627 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000628
Sean Callanane0b23b52012-11-15 02:02:04 +0000629 return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(), m_parser_vars->m_exe_ctx.GetProcessPtr(), name, symbol_type);
Sean Callanand9ca42a2011-05-08 02:21:26 +0000630}
631
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000632const Symbol *
633ClangExpressionDeclMap::FindGlobalDataSymbol (Target &target,
Greg Clayton9191db42013-10-21 18:40:51 +0000634 const ConstString &name,
Deepak Panickald66b50c2013-10-22 12:27:43 +0000635 lldb_private::Module *module)
Sean Callanand9ca42a2011-05-08 02:21:26 +0000636{
637 SymbolContextList sc_list;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000638
Greg Clayton9191db42013-10-21 18:40:51 +0000639 if (module)
640 module->FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
641 else
642 target.GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000643
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000644 const uint32_t matches = sc_list.GetSize();
645 for (uint32_t i=0; i<matches; ++i)
Sean Callanand9ca42a2011-05-08 02:21:26 +0000646 {
647 SymbolContext sym_ctx;
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000648 sc_list.GetContextAtIndex(i, sym_ctx);
649 if (sym_ctx.symbol)
650 {
651 const Symbol *symbol = sym_ctx.symbol;
Greg Clayton358cf1e2015-06-25 21:46:34 +0000652 const Address sym_address = symbol->GetAddress();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000653
Greg Clayton358cf1e2015-06-25 21:46:34 +0000654 if (sym_address.IsValid())
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000655 {
656 switch (symbol->GetType())
657 {
658 case eSymbolTypeData:
659 case eSymbolTypeRuntime:
660 case eSymbolTypeAbsolute:
661 case eSymbolTypeObjCClass:
662 case eSymbolTypeObjCMetaClass:
663 case eSymbolTypeObjCIVar:
664 if (symbol->GetDemangledNameIsSynthesized())
665 {
666 // If the demangled name was synthesized, then don't use it
667 // for expressions. Only let the symbol match if the mangled
668 // named matches for these symbols.
669 if (symbol->GetMangled().GetMangledName() != name)
670 break;
671 }
672 return symbol;
673
Greg Clayton9191db42013-10-21 18:40:51 +0000674 case eSymbolTypeReExported:
675 {
676 ConstString reexport_name = symbol->GetReExportedSymbolName();
677 if (reexport_name)
678 {
679 ModuleSP reexport_module_sp;
680 ModuleSpec reexport_module_spec;
681 reexport_module_spec.GetPlatformFileSpec() = symbol->GetReExportedSymbolSharedLibrary();
682 if (reexport_module_spec.GetPlatformFileSpec())
683 {
684 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
685 if (!reexport_module_sp)
686 {
687 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
688 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
689 }
690 }
Greg Clayton67d49482015-05-15 21:27:16 +0000691 // Don't allow us to try and resolve a re-exported symbol if it is the same
692 // as the current symbol
693 if (name == symbol->GetReExportedSymbolName() && module == reexport_module_sp.get())
694 return NULL;
695
Greg Clayton9191db42013-10-21 18:40:51 +0000696 return FindGlobalDataSymbol(target, symbol->GetReExportedSymbolName(), reexport_module_sp.get());
697 }
698 }
699 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000700
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000701 case eSymbolTypeCode: // We already lookup functions elsewhere
702 case eSymbolTypeVariable:
703 case eSymbolTypeLocal:
704 case eSymbolTypeParam:
705 case eSymbolTypeTrampoline:
706 case eSymbolTypeInvalid:
707 case eSymbolTypeException:
708 case eSymbolTypeSourceFile:
709 case eSymbolTypeHeaderFile:
710 case eSymbolTypeObjectFile:
711 case eSymbolTypeCommonBlock:
712 case eSymbolTypeBlock:
713 case eSymbolTypeVariableType:
714 case eSymbolTypeLineEntry:
715 case eSymbolTypeLineHeader:
716 case eSymbolTypeScopeBegin:
717 case eSymbolTypeScopeEnd:
718 case eSymbolTypeAdditional:
719 case eSymbolTypeCompiler:
720 case eSymbolTypeInstrumentation:
721 case eSymbolTypeUndefined:
Matt Kopec00049b82013-02-27 20:13:38 +0000722 case eSymbolTypeResolver:
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000723 break;
724 }
725 }
726 }
Sean Callanand9ca42a2011-05-08 02:21:26 +0000727 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000728
Sean Callanand9ca42a2011-05-08 02:21:26 +0000729 return NULL;
730}
731
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000732lldb::VariableSP
733ClangExpressionDeclMap::FindGlobalVariable
734(
735 Target &target,
Sean Callanan1fd3f4f2011-10-12 16:59:31 +0000736 ModuleSP &module,
737 const ConstString &name,
Greg Clayton99558cc42015-08-24 23:46:31 +0000738 CompilerDeclContext *namespace_decl,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000739 TypeFromUser *type
740)
741{
742 VariableList vars;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000743
Sean Callanan1fd3f4f2011-10-12 16:59:31 +0000744 if (module && namespace_decl)
745 module->FindGlobalVariables (name, namespace_decl, true, -1, vars);
746 else
747 target.GetImages().FindGlobalVariables(name, true, -1, vars);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000748
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000749 if (vars.GetSize())
750 {
751 if (type)
752 {
753 for (size_t i = 0; i < vars.GetSize(); ++i)
754 {
755 VariableSP var_sp = vars.GetVariableAtIndex(i);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000756
Greg Clayton99558cc42015-08-24 23:46:31 +0000757 if (ClangASTContext::AreTypesSame(*type, var_sp->GetType()->GetFullCompilerType ()))
Greg Clayton57ee3062013-07-11 22:46:58 +0000758 return var_sp;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000759 }
760 }
761 else
762 {
763 return vars.GetVariableAtIndex(0);
764 }
765 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000766
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000767 return VariableSP();
768}
769
Siva Chandra03ff5c82016-02-05 19:10:04 +0000770ClangASTContext *
771ClangExpressionDeclMap::GetClangASTContext ()
772{
773 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
774 if (frame == nullptr)
775 return nullptr;
776
777 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction|lldb::eSymbolContextBlock);
778 if (sym_ctx.block == nullptr)
779 return nullptr;
780
781 CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext();
782 if (!frame_decl_context)
783 return nullptr;
784
785 return llvm::dyn_cast_or_null<ClangASTContext>(frame_decl_context.GetTypeSystem());
786}
787
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000788// Interface for ClangASTSource
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000789
Sean Callanan503aa522011-10-12 00:12:34 +0000790void
Sean Callananeddeb3b2011-10-28 23:38:38 +0000791ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000792{
Sean Callananeddeb3b2011-10-28 23:38:38 +0000793 assert (m_ast_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000794
Sean Callanan8106d802013-03-08 20:04:57 +0000795 ClangASTMetrics::RegisterVisibleQuery();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000796
Sean Callananeddeb3b2011-10-28 23:38:38 +0000797 const ConstString name(context.m_decl_name.getAsString().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000798
Greg Clayton5160ce52013-03-27 23:08:40 +0000799 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000800
Sean Callananfb3e4302011-10-29 19:50:43 +0000801 if (GetImportInProgress())
Sean Callanan6abfabf2010-11-19 20:20:02 +0000802 {
Sean Callanan7dd98122011-10-14 20:34:21 +0000803 if (log && log->GetVerbose())
Sean Callanan6abfabf2010-11-19 20:20:02 +0000804 log->Printf("Ignoring a query during an import");
805 return;
806 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000807
Sean Callanan7dd98122011-10-14 20:34:21 +0000808 static unsigned int invocation_id = 0;
809 unsigned int current_id = invocation_id++;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000810
Sean Callanan503aa522011-10-12 00:12:34 +0000811 if (log)
812 {
813 if (!context.m_decl_context)
Sean Callananfb3e4302011-10-29 19:50:43 +0000814 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a NULL DeclContext", current_id, name.GetCString());
Sean Callanan503aa522011-10-12 00:12:34 +0000815 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callananfb3e4302011-10-29 19:50:43 +0000816 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in '%s'", current_id, name.GetCString(), context_named_decl->getNameAsString().c_str());
Sean Callanan503aa522011-10-12 00:12:34 +0000817 else
Sean Callananfb3e4302011-10-29 19:50:43 +0000818 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a '%s'", current_id, name.GetCString(), context.m_decl_context->getDeclKindName());
Sean Callanan503aa522011-10-12 00:12:34 +0000819 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000820
Sean Callanan503aa522011-10-12 00:12:34 +0000821 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
822 {
Siva Chandra03ff5c82016-02-05 19:10:04 +0000823 if (namespace_context->getName().str() == std::string(g_lldb_local_vars_namespace_cstr))
824 {
Saleem Abdulrasool2f1d3ac2016-02-15 03:23:14 +0000825 CompilerDeclContext compiler_decl_ctx(GetClangASTContext(), const_cast<void *>(static_cast<const void *>(context.m_decl_context)));
Siva Chandra03ff5c82016-02-05 19:10:04 +0000826 FindExternalVisibleDecls(context, lldb::ModuleSP(), compiler_decl_ctx, current_id);
827 return;
828 }
829
Sean Callanana3444ff2015-11-10 22:54:42 +0000830 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer_sp->GetNamespaceMap(namespace_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000831
Sean Callanan7dd98122011-10-14 20:34:21 +0000832 if (log && log->GetVerbose())
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000833 log->Printf(" CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000834 current_id, static_cast<void*>(namespace_map.get()),
Sean Callanan7dd98122011-10-14 20:34:21 +0000835 (int)namespace_map->size());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000836
Sean Callananb2269162011-10-21 22:18:07 +0000837 if (!namespace_map)
838 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000839
Sean Callanan503aa522011-10-12 00:12:34 +0000840 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
841 i != e;
842 ++i)
843 {
844 if (log)
Sean Callananfb3e4302011-10-29 19:50:43 +0000845 log->Printf(" CEDM::FEVD[%u] Searching namespace %s in module %s",
Sean Callanan7dd98122011-10-14 20:34:21 +0000846 current_id,
Greg Clayton99558cc42015-08-24 23:46:31 +0000847 i->second.GetName().AsCString(),
Sean Callanan503aa522011-10-12 00:12:34 +0000848 i->first->GetFileSpec().GetFilename().GetCString());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000849
Sean Callananebe60672011-10-13 21:50:33 +0000850 FindExternalVisibleDecls(context,
851 i->first,
852 i->second,
Sean Callanan7dd98122011-10-14 20:34:21 +0000853 current_id);
Sean Callanan503aa522011-10-12 00:12:34 +0000854 }
855 }
Sean Callanand5c17ed2011-11-15 02:11:17 +0000856 else if (isa<TranslationUnitDecl>(context.m_decl_context))
Sean Callanan503aa522011-10-12 00:12:34 +0000857 {
Greg Clayton99558cc42015-08-24 23:46:31 +0000858 CompilerDeclContext namespace_decl;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000859
Sean Callanan503aa522011-10-12 00:12:34 +0000860 if (log)
Sean Callananfb3e4302011-10-29 19:50:43 +0000861 log->Printf(" CEDM::FEVD[%u] Searching the root namespace", current_id);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000862
Sean Callanan503aa522011-10-12 00:12:34 +0000863 FindExternalVisibleDecls(context,
864 lldb::ModuleSP(),
865 namespace_decl,
Sean Callanan7dd98122011-10-14 20:34:21 +0000866 current_id);
Sean Callanan503aa522011-10-12 00:12:34 +0000867 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000868
Jim Inghamdeb384d2016-04-28 02:17:02 +0000869 if (!context.m_found.variable && !context.m_found.local_vars_nsp)
Sean Callananfb3e4302011-10-29 19:50:43 +0000870 ClangASTSource::FindExternalVisibleDecls(context);
Sean Callanan503aa522011-10-12 00:12:34 +0000871}
872
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000873void
874ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context,
Sean Callanan1a2c5382011-10-12 18:44:30 +0000875 lldb::ModuleSP module_sp,
Greg Clayton99558cc42015-08-24 23:46:31 +0000876 CompilerDeclContext &namespace_decl,
Sean Callanan7dd98122011-10-14 20:34:21 +0000877 unsigned int current_id)
Sean Callanan503aa522011-10-12 00:12:34 +0000878{
Sean Callananeddeb3b2011-10-28 23:38:38 +0000879 assert (m_ast_context);
Sean Callananb3a36df2016-03-19 00:51:43 +0000880
881 std::function<void (clang::FunctionDecl *)> MaybeRegisterFunctionBody =
882 [this](clang::FunctionDecl *copied_function_decl)
883 {
884 if (copied_function_decl->getBody() && m_parser_vars->m_code_gen)
885 {
886 DeclGroupRef decl_group_ref(copied_function_decl);
887 m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref);
888 }
889 };
890
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000891
Greg Clayton5160ce52013-03-27 23:08:40 +0000892 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000893
Greg Clayton7a345282010-11-09 23:46:37 +0000894 SymbolContextList sc_list;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000895
Sean Callananfb3e4302011-10-29 19:50:43 +0000896 const ConstString name(context.m_decl_name.getAsString().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000897
Greg Clayton83c5cd92010-11-14 22:13:40 +0000898 const char *name_unique_cstr = name.GetCString();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000899
Greg Clayton83c5cd92010-11-14 22:13:40 +0000900 if (name_unique_cstr == NULL)
901 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000902
Sean Callanan5b26f272012-02-04 08:49:35 +0000903 static ConstString id_name("id");
904 static ConstString Class_name("Class");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000905
Sean Callanan5b26f272012-02-04 08:49:35 +0000906 if (name == id_name || name == Class_name)
907 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000908
909 // Only look for functions by name out in our symbols if the function
Greg Clayton7b462cc2010-10-15 22:48:33 +0000910 // doesn't start with our phony prefix of '$'
Sean Callanan933693b2012-02-10 01:22:05 +0000911 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Jason Molendab57e4a12013-11-04 09:33:30 +0000912 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
Paul Hermand628cbb2015-09-15 23:44:17 +0000913 SymbolContext sym_ctx;
914 if (frame != nullptr)
915 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction|lldb::eSymbolContextBlock);
Sean Callananb3a36df2016-03-19 00:51:43 +0000916
917 // Try the persistent decls, which take precedence over all else.
918 if (!namespace_decl)
919 {
920 do
921 {
922 if (!target)
923 break;
924
925 ClangASTContext *scratch_clang_ast_context = target->GetScratchClangASTContext();
926
927 if (!scratch_clang_ast_context)
928 break;
929
930 ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
931
932 if (!scratch_ast_context)
933 break;
934
935 NamedDecl *persistent_decl = m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
936
937 if (!persistent_decl)
938 break;
939
940 Decl *parser_persistent_decl = m_ast_importer_sp->CopyDecl(m_ast_context, scratch_ast_context, persistent_decl);
941
942 if (!parser_persistent_decl)
943 break;
944
945 NamedDecl *parser_named_decl = dyn_cast<NamedDecl>(parser_persistent_decl);
946
947 if (!parser_named_decl)
948 break;
949
950 if (clang::FunctionDecl *parser_function_decl = llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl))
951 {
952 MaybeRegisterFunctionBody(parser_function_decl);
953 }
954
955 if (log)
956 log->Printf(" CEDM::FEVD[%u] Found persistent decl %s", current_id, name.GetCString());
957
958 context.AddNamedDecl(parser_named_decl);
959 } while (0);
960 }
961
Sean Callanan88972242011-10-12 01:39:28 +0000962 if (name_unique_cstr[0] == '$' && !namespace_decl)
963 {
964 static ConstString g_lldb_class_name ("$__lldb_class");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000965
Sean Callanan88972242011-10-12 01:39:28 +0000966 if (name == g_lldb_class_name)
967 {
968 // Clang is looking for the type of "this"
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000969
Sean Callananc03bdd92012-02-08 03:45:08 +0000970 if (frame == NULL)
Sean Callanan88972242011-10-12 01:39:28 +0000971 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000972
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000973
Greg Clayton99558cc42015-08-24 23:46:31 +0000974 // Find the block that defines the function represented by "sym_ctx"
Greg Clayton685c88c2012-07-14 00:53:55 +0000975 Block *function_block = sym_ctx.GetFunctionBlock();
976
977 if (!function_block)
978 return;
979
Greg Clayton99558cc42015-08-24 23:46:31 +0000980 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000981
Greg Clayton99558cc42015-08-24 23:46:31 +0000982 if (!function_decl_ctx)
Sean Callanan88972242011-10-12 01:39:28 +0000983 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000984
Greg Clayton99558cc42015-08-24 23:46:31 +0000985 clang::CXXMethodDecl *method_decl = ClangASTContext::DeclContextGetAsCXXMethodDecl(function_decl_ctx);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000986
Jim Ingham5fdeed42012-10-30 23:35:54 +0000987 if (method_decl)
Sean Callanan88972242011-10-12 01:39:28 +0000988 {
Jim Ingham5fdeed42012-10-30 23:35:54 +0000989 clang::CXXRecordDecl *class_decl = method_decl->getParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000990
Jim Ingham5fdeed42012-10-30 23:35:54 +0000991 QualType class_qual_type(class_decl->getTypeForDecl(), 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000992
Jim Ingham5fdeed42012-10-30 23:35:54 +0000993 TypeFromUser class_user_type (class_qual_type.getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +0000994 ClangASTContext::GetASTContext(&class_decl->getASTContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000995
Jim Ingham5fdeed42012-10-30 23:35:54 +0000996 if (log)
997 {
998 ASTDumper ast_dumper(class_qual_type);
999 log->Printf(" CEDM::FEVD[%u] Adding type for $__lldb_class: %s", current_id, ast_dumper.GetCString());
1000 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001001
Sean Callanan3b436d22015-10-23 21:45:02 +00001002 AddThisType(context, class_user_type, current_id);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001003
Jim Ingham5fdeed42012-10-30 23:35:54 +00001004 if (method_decl->isInstance())
1005 {
1006 // self is a pointer to the object
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001007
Jim Ingham5fdeed42012-10-30 23:35:54 +00001008 QualType class_pointer_type = method_decl->getASTContext().getPointerType(class_qual_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001009
Jim Ingham5fdeed42012-10-30 23:35:54 +00001010 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +00001011 ClangASTContext::GetASTContext(&method_decl->getASTContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001012
Jim Ingham5fdeed42012-10-30 23:35:54 +00001013 m_struct_vars->m_object_pointer_type = self_user_type;
1014 }
Sean Callanan88972242011-10-12 01:39:28 +00001015 }
Jim Ingham5fdeed42012-10-30 23:35:54 +00001016 else
Sean Callananbb6e7ed2012-03-05 22:08:20 +00001017 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001018 // This branch will get hit if we are executing code in the context of a function that
1019 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a
Bruce Mitchener58ef3912015-06-18 05:27:05 +00001020 // method of the class. In that case, just look up the "this" variable in the current
Jim Ingham5fdeed42012-10-30 23:35:54 +00001021 // scope and use its type.
1022 // FIXME: This code is formally correct, but clang doesn't currently emit DW_AT_object_pointer
1023 // for C++ so it hasn't actually been tested.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001024
Jim Ingham5fdeed42012-10-30 23:35:54 +00001025 VariableList *vars = frame->GetVariableList(false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001026
Jim Ingham5fdeed42012-10-30 23:35:54 +00001027 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001028
Jim Ingham5fdeed42012-10-30 23:35:54 +00001029 if (this_var &&
1030 this_var->IsInScope(frame) &&
1031 this_var->LocationIsValidForFrame (frame))
1032 {
1033 Type *this_type = this_var->GetType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001034
Jim Ingham5fdeed42012-10-30 23:35:54 +00001035 if (!this_type)
1036 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001037
Sean Callanan3b436d22015-10-23 21:45:02 +00001038 TypeFromUser pointee_type = this_type->GetForwardCompilerType ().GetPointeeType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001039
Greg Clayton57ee3062013-07-11 22:46:58 +00001040 if (pointee_type.IsValid())
Jim Ingham5fdeed42012-10-30 23:35:54 +00001041 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001042 if (log)
1043 {
Sean Callanan3b436d22015-10-23 21:45:02 +00001044 ASTDumper ast_dumper(pointee_type);
1045 log->Printf(" FEVD[%u] Adding type for $__lldb_class: %s", current_id, ast_dumper.GetCString());
Jim Ingham5fdeed42012-10-30 23:35:54 +00001046 }
Sean Callanan3b436d22015-10-23 21:45:02 +00001047
1048 AddThisType(context, pointee_type, current_id);
Greg Clayton99558cc42015-08-24 23:46:31 +00001049 TypeFromUser this_user_type(this_type->GetFullCompilerType ());
Jim Ingham5fdeed42012-10-30 23:35:54 +00001050 m_struct_vars->m_object_pointer_type = this_user_type;
1051 return;
1052 }
1053 }
Sean Callananbb6e7ed2012-03-05 22:08:20 +00001054 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001055
Sean Callanan88972242011-10-12 01:39:28 +00001056 return;
1057 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001058
Sean Callanan88972242011-10-12 01:39:28 +00001059 static ConstString g_lldb_objc_class_name ("$__lldb_objc_class");
1060 if (name == g_lldb_objc_class_name)
1061 {
1062 // Clang is looking for the type of "*self"
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001063
Sean Callanan88972242011-10-12 01:39:28 +00001064 if (!frame)
1065 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001066
Greg Clayton99558cc42015-08-24 23:46:31 +00001067 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction|lldb::eSymbolContextBlock);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001068
Greg Clayton99558cc42015-08-24 23:46:31 +00001069 // Find the block that defines the function represented by "sym_ctx"
Greg Clayton685c88c2012-07-14 00:53:55 +00001070 Block *function_block = sym_ctx.GetFunctionBlock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001071
Greg Clayton685c88c2012-07-14 00:53:55 +00001072 if (!function_block)
1073 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001074
Greg Clayton99558cc42015-08-24 23:46:31 +00001075 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001076
Greg Clayton99558cc42015-08-24 23:46:31 +00001077 if (!function_decl_ctx)
Sean Callanand5c17ed2011-11-15 02:11:17 +00001078 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001079
Greg Clayton99558cc42015-08-24 23:46:31 +00001080 clang::ObjCMethodDecl *method_decl = ClangASTContext::DeclContextGetAsObjCMethodDecl(function_decl_ctx);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001081
Jim Ingham5fdeed42012-10-30 23:35:54 +00001082 if (method_decl)
Sean Callanand5c17ed2011-11-15 02:11:17 +00001083 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001084 ObjCInterfaceDecl* self_interface = method_decl->getClassInterface();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001085
Jim Ingham5fdeed42012-10-30 23:35:54 +00001086 if (!self_interface)
Sean Callananc03bdd92012-02-08 03:45:08 +00001087 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001088
Jim Ingham5fdeed42012-10-30 23:35:54 +00001089 const clang::Type *interface_type = self_interface->getTypeForDecl();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001090
Sean Callanandfb87d62013-04-09 21:30:48 +00001091 if (!interface_type)
1092 return; // This is unlikely, but we have seen crashes where this occurred
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001093
Jim Ingham5fdeed42012-10-30 23:35:54 +00001094 TypeFromUser class_user_type(QualType(interface_type, 0).getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +00001095 ClangASTContext::GetASTContext(&method_decl->getASTContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001096
Jim Ingham5fdeed42012-10-30 23:35:54 +00001097 if (log)
1098 {
1099 ASTDumper ast_dumper(interface_type);
1100 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1101 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001102
Sean Callananfa4fab72013-02-01 06:55:48 +00001103 AddOneType(context, class_user_type, current_id);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001104
Jim Ingham5fdeed42012-10-30 23:35:54 +00001105 if (method_decl->isInstanceMethod())
1106 {
1107 // self is a pointer to the object
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001108
Jim Ingham5fdeed42012-10-30 23:35:54 +00001109 QualType class_pointer_type = method_decl->getASTContext().getObjCObjectPointerType(QualType(interface_type, 0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001110
Jim Ingham5fdeed42012-10-30 23:35:54 +00001111 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +00001112 ClangASTContext::GetASTContext(&method_decl->getASTContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001113
Jim Ingham5fdeed42012-10-30 23:35:54 +00001114 m_struct_vars->m_object_pointer_type = self_user_type;
1115 }
1116 else
1117 {
1118 // self is a Class pointer
1119 QualType class_type = method_decl->getASTContext().getObjCClassType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001120
Jim Ingham5fdeed42012-10-30 23:35:54 +00001121 TypeFromUser self_user_type(class_type.getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +00001122 ClangASTContext::GetASTContext(&method_decl->getASTContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001123
Jim Ingham5fdeed42012-10-30 23:35:54 +00001124 m_struct_vars->m_object_pointer_type = self_user_type;
1125 }
1126
1127 return;
Sean Callananc03bdd92012-02-08 03:45:08 +00001128 }
1129 else
1130 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001131 // This branch will get hit if we are executing code in the context of a function that
1132 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a
Bruce Mitchener58ef3912015-06-18 05:27:05 +00001133 // method of the class. In that case, just look up the "self" variable in the current
Jim Ingham5fdeed42012-10-30 23:35:54 +00001134 // scope and use its type.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001135
Jim Ingham5fdeed42012-10-30 23:35:54 +00001136 VariableList *vars = frame->GetVariableList(false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001137
Jim Ingham5fdeed42012-10-30 23:35:54 +00001138 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001139
Jim Ingham5fdeed42012-10-30 23:35:54 +00001140 if (self_var &&
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001141 self_var->IsInScope(frame) &&
Jim Ingham5fdeed42012-10-30 23:35:54 +00001142 self_var->LocationIsValidForFrame (frame))
1143 {
1144 Type *self_type = self_var->GetType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001145
Jim Ingham5fdeed42012-10-30 23:35:54 +00001146 if (!self_type)
1147 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001148
Greg Clayton99558cc42015-08-24 23:46:31 +00001149 CompilerType self_clang_type = self_type->GetFullCompilerType ();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001150
Greg Claytond8d4a572015-08-11 21:38:15 +00001151 if (ClangASTContext::IsObjCClassType(self_clang_type))
Jim Ingham5fdeed42012-10-30 23:35:54 +00001152 {
Sean Callanana2868d42013-01-19 01:49:02 +00001153 return;
1154 }
Greg Claytond8d4a572015-08-11 21:38:15 +00001155 else if (ClangASTContext::IsObjCObjectPointerType(self_clang_type))
Sean Callanana2868d42013-01-19 01:49:02 +00001156 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001157 self_clang_type = self_clang_type.GetPointeeType();
1158
1159 if (!self_clang_type)
Sean Callanane5270592013-04-26 22:54:19 +00001160 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001161
Jim Ingham5fdeed42012-10-30 23:35:54 +00001162 if (log)
1163 {
Greg Clayton99558cc42015-08-24 23:46:31 +00001164 ASTDumper ast_dumper(self_type->GetFullCompilerType ());
Jim Ingham5fdeed42012-10-30 23:35:54 +00001165 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1166 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001167
Greg Clayton57ee3062013-07-11 22:46:58 +00001168 TypeFromUser class_user_type (self_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001169
Sean Callananfa4fab72013-02-01 06:55:48 +00001170 AddOneType(context, class_user_type, current_id);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001171
Greg Clayton99558cc42015-08-24 23:46:31 +00001172 TypeFromUser self_user_type(self_type->GetFullCompilerType ());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001173
Jim Ingham5fdeed42012-10-30 23:35:54 +00001174 m_struct_vars->m_object_pointer_type = self_user_type;
1175 return;
1176 }
1177 }
Sean Callananc03bdd92012-02-08 03:45:08 +00001178 }
1179
Sean Callanan88972242011-10-12 01:39:28 +00001180 return;
1181 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001182
Siva Chandra03ff5c82016-02-05 19:10:04 +00001183 if (name == ConstString(g_lldb_local_vars_namespace_cstr))
1184 {
1185 CompilerDeclContext frame_decl_context = sym_ctx.block != nullptr ?
1186 sym_ctx.block->GetDeclContext() :
1187 CompilerDeclContext();
1188
1189 if (frame_decl_context)
1190 {
1191 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(frame_decl_context.GetTypeSystem());
1192
1193 if (ast)
1194 {
1195 clang::NamespaceDecl *namespace_decl = ClangASTContext::GetUniqueNamespaceDeclaration(
1196 m_ast_context, name_unique_cstr, nullptr);
1197 if (namespace_decl)
1198 {
1199 context.AddNamedDecl(namespace_decl);
1200 clang::DeclContext *clang_decl_ctx = clang::Decl::castToDeclContext(namespace_decl);
1201 clang_decl_ctx->setHasExternalVisibleStorage(true);
Jim Inghamdeb384d2016-04-28 02:17:02 +00001202 context.m_found.local_vars_nsp = true;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001203 }
1204 }
1205 }
1206
1207 return;
1208 }
1209
Sean Callanan88972242011-10-12 01:39:28 +00001210 // any other $__lldb names should be weeded out now
1211 if (!::strncmp(name_unique_cstr, "$__lldb", sizeof("$__lldb") - 1))
1212 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001213
Sean Callananbc8ac342015-09-04 20:49:51 +00001214 ExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariable(name));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001215
Sean Callanan88972242011-10-12 01:39:28 +00001216 if (pvar_sp)
1217 {
Sean Callanan7dd98122011-10-14 20:34:21 +00001218 AddOneVariable(context, pvar_sp, current_id);
Sean Callanan88972242011-10-12 01:39:28 +00001219 return;
1220 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001221
Sean Callanan88972242011-10-12 01:39:28 +00001222 const char *reg_name(&name.GetCString()[1]);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001223
Sean Callanan933693b2012-02-10 01:22:05 +00001224 if (m_parser_vars->m_exe_ctx.GetRegisterContext())
Sean Callanan88972242011-10-12 01:39:28 +00001225 {
Sean Callanan933693b2012-02-10 01:22:05 +00001226 const RegisterInfo *reg_info(m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(reg_name));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001227
Sean Callanan88972242011-10-12 01:39:28 +00001228 if (reg_info)
Sean Callanan35c7f982011-10-18 16:46:55 +00001229 {
1230 if (log)
Sean Callananfb3e4302011-10-29 19:50:43 +00001231 log->Printf(" CEDM::FEVD[%u] Found register %s", current_id, reg_info->name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001232
Sean Callanan7dd98122011-10-14 20:34:21 +00001233 AddOneRegister(context, reg_info, current_id);
Sean Callanan35c7f982011-10-18 16:46:55 +00001234 }
Sean Callanan88972242011-10-12 01:39:28 +00001235 }
1236 }
1237 else
Sean Callanan8ade1042010-07-27 00:55:47 +00001238 {
Sean Callanan69b53412011-08-06 00:28:14 +00001239 ValueObjectSP valobj;
1240 VariableSP var;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001241
Siva Chandra03ff5c82016-02-05 19:10:04 +00001242 bool local_var_lookup = !namespace_decl ||
1243 (namespace_decl.GetName() == ConstString(g_lldb_local_vars_namespace_cstr));
1244 if (frame && local_var_lookup)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001245 {
Paul Hermand628cbb2015-09-15 23:44:17 +00001246 CompilerDeclContext compiler_decl_context = sym_ctx.block != nullptr ? sym_ctx.block->GetDeclContext() : CompilerDeclContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001247
Paul Hermand628cbb2015-09-15 23:44:17 +00001248 if (compiler_decl_context)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001249 {
Siva Chandra03ff5c82016-02-05 19:10:04 +00001250 // Make sure that the variables are parsed so that we have the declarations.
Paul Hermand628cbb2015-09-15 23:44:17 +00001251 VariableListSP vars = frame->GetInScopeVariableList(true);
1252 for (size_t i = 0; i < vars->GetSize(); i++)
1253 vars->GetVariableAtIndex(i)->GetDecl();
1254
Siva Chandra03ff5c82016-02-05 19:10:04 +00001255 // Search for declarations matching the name. Do not include imported decls
1256 // in the search if we are looking for decls in the artificial namespace
1257 // $__lldb_local_vars.
1258 std::vector<CompilerDecl> found_decls = compiler_decl_context.FindDeclByName(name, namespace_decl.IsValid());
Paul Hermanea188fc2015-09-16 18:48:30 +00001259
1260 bool variable_found = false;
Paul Hermand628cbb2015-09-15 23:44:17 +00001261 for (CompilerDecl decl : found_decls)
1262 {
Sean Callanan5ba32152016-05-23 18:30:59 +00001263 for (size_t vi = 0, ve = vars->GetSize(); vi != ve; ++vi)
1264 {
1265 VariableSP candidate_var = vars->GetVariableAtIndex(vi);
1266 if (candidate_var->GetDecl() == decl)
1267 {
1268 var = candidate_var;
1269 break;
1270 }
1271 }
1272
Paul Hermand628cbb2015-09-15 23:44:17 +00001273 if (var)
1274 {
Paul Hermanea188fc2015-09-16 18:48:30 +00001275 variable_found = true;
Paul Hermand628cbb2015-09-15 23:44:17 +00001276 valobj = ValueObjectVariable::Create(frame, var);
1277 AddOneVariable(context, var, valobj, current_id);
1278 context.m_found.variable = true;
Paul Hermand628cbb2015-09-15 23:44:17 +00001279 }
1280 }
Paul Hermanea188fc2015-09-16 18:48:30 +00001281 if (variable_found)
1282 return;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001283 }
Greg Clayton7b462cc2010-10-15 22:48:33 +00001284 }
Sean Callananfd1ba912011-12-10 04:03:38 +00001285 if (target)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001286 {
Sean Callanan1fd3f4f2011-10-12 16:59:31 +00001287 var = FindGlobalVariable (*target,
Sean Callanan1a2c5382011-10-12 18:44:30 +00001288 module_sp,
Sean Callanan1fd3f4f2011-10-12 16:59:31 +00001289 name,
1290 &namespace_decl,
Greg Claytonc14ee322011-09-22 04:58:26 +00001291 NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001292
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001293 if (var)
1294 {
Sean Callanan9b3569b2011-12-10 03:12:34 +00001295 valobj = ValueObjectVariable::Create(target, var);
Sean Callanan0712f462011-11-29 22:03:21 +00001296 AddOneVariable(context, var, valobj, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001297 context.m_found.variable = true;
Sean Callananfd1ba912011-12-10 04:03:38 +00001298 return;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001299 }
1300 }
Sean Callananc8278af2014-12-05 01:27:35 +00001301
1302 std::vector<clang::NamedDecl *> decls_from_modules;
1303
1304 if (target)
1305 {
1306 if (ClangModulesDeclVendor *decl_vendor = target->GetClangModulesDeclVendor())
1307 {
1308 decl_vendor->FindDecls(name, false, UINT32_MAX, decls_from_modules);
1309 }
1310 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001311
Sean Callananca4e0fd2011-10-12 20:29:25 +00001312 if (!context.m_found.variable)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001313 {
Sean Callanan9df05fb2012-02-10 22:52:19 +00001314 const bool include_inlines = false;
Greg Clayton931180e2011-01-27 06:44:37 +00001315 const bool append = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001316
Sean Callanan1a2c5382011-10-12 18:44:30 +00001317 if (namespace_decl && module_sp)
Sean Callanan25ea08e2011-10-12 17:38:09 +00001318 {
Sean Callanan3ae61792012-02-15 17:14:49 +00001319 const bool include_symbols = false;
1320
Sean Callanan1a2c5382011-10-12 18:44:30 +00001321 module_sp->FindFunctions(name,
1322 &namespace_decl,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001323 eFunctionNameTypeBase,
Sean Callanan1a2c5382011-10-12 18:44:30 +00001324 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001325 include_inlines,
Sean Callanan1a2c5382011-10-12 18:44:30 +00001326 append,
1327 sc_list);
Sean Callanan25ea08e2011-10-12 17:38:09 +00001328 }
Jim Ingham28eb5712012-10-12 17:34:26 +00001329 else if (target && !namespace_decl)
Sean Callanan25ea08e2011-10-12 17:38:09 +00001330 {
Sean Callanan3ae61792012-02-15 17:14:49 +00001331 const bool include_symbols = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001332
Sean Callanan308a3c52012-07-28 00:21:01 +00001333 // TODO Fix FindFunctions so that it doesn't return
1334 // instance methods for eFunctionNameTypeBase.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001335
Sean Callanan25ea08e2011-10-12 17:38:09 +00001336 target->GetImages().FindFunctions(name,
Greg Clayton43fe2172013-04-03 02:00:15 +00001337 eFunctionNameTypeFull,
Sean Callanan25ea08e2011-10-12 17:38:09 +00001338 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001339 include_inlines,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001340 append,
Sean Callanan25ea08e2011-10-12 17:38:09 +00001341 sc_list);
1342 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001343
Dawn Perchikb5925782015-12-12 19:31:41 +00001344 // If we found more than one function, see if we can use the
1345 // frame's decl context to remove functions that are shadowed
1346 // by other functions which match in type but are nearer in scope.
1347 //
1348 // AddOneFunction will not add a function whose type has already been
1349 // added, so if there's another function in the list with a matching
1350 // type, check to see if their decl context is a parent of the current
1351 // frame's or was imported via a and using statement, and pick the
1352 // best match according to lookup rules.
1353 if (sc_list.GetSize() > 1)
1354 {
1355 // Collect some info about our frame's context.
1356 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1357 SymbolContext frame_sym_ctx;
1358 if (frame != nullptr)
1359 frame_sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction|lldb::eSymbolContextBlock);
1360 CompilerDeclContext frame_decl_context = frame_sym_ctx.block != nullptr ? frame_sym_ctx.block->GetDeclContext() : CompilerDeclContext();
1361
1362 // We can't do this without a compiler decl context for our frame.
1363 if (frame_decl_context)
1364 {
1365 clang::DeclContext *frame_decl_ctx = (clang::DeclContext *)frame_decl_context.GetOpaqueDeclContext();
1366 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(frame_decl_context.GetTypeSystem());
1367
1368 // Structure to hold the info needed when comparing function
1369 // declarations.
1370 struct FuncDeclInfo
1371 {
1372 ConstString m_name;
1373 CompilerType m_copied_type;
1374 uint32_t m_decl_lvl;
1375 SymbolContext m_sym_ctx;
1376 };
1377
1378 // First, symplify things by looping through the symbol contexts
1379 // to remove unwanted functions and separate out the functions we
1380 // want to compare and prune into a separate list.
1381 // Cache the info needed about the function declarations in a
1382 // vector for efficiency.
1383 SymbolContextList sc_sym_list;
1384 uint32_t num_indices = sc_list.GetSize();
1385 std::vector<FuncDeclInfo> fdi_cache;
1386 fdi_cache.reserve(num_indices);
1387 for (uint32_t index = 0; index < num_indices; ++index)
1388 {
1389 FuncDeclInfo fdi;
1390 SymbolContext sym_ctx;
1391 sc_list.GetContextAtIndex(index, sym_ctx);
1392
1393 // We don't know enough about symbols to compare them,
1394 // but we should keep them in the list.
1395 Function *function = sym_ctx.function;
1396 if (!function)
1397 {
1398 sc_sym_list.Append(sym_ctx);
1399 continue;
1400 }
1401 // Filter out functions without declaration contexts, as well as
1402 // class/instance methods, since they'll be skipped in the
1403 // code that follows anyway.
1404 CompilerDeclContext func_decl_context = function->GetDeclContext();
1405 if (!func_decl_context || func_decl_context.IsClassMethod(nullptr, nullptr, nullptr))
1406 continue;
1407 // We can only prune functions for which we can copy the type.
1408 CompilerType func_clang_type = function->GetType()->GetFullCompilerType();
1409 CompilerType copied_func_type = GuardedCopyType(func_clang_type);
1410 if (!copied_func_type)
1411 {
1412 sc_sym_list.Append(sym_ctx);
1413 continue;
1414 }
1415
1416 fdi.m_sym_ctx = sym_ctx;
1417 fdi.m_name = function->GetName();
1418 fdi.m_copied_type = copied_func_type;
1419 fdi.m_decl_lvl = LLDB_INVALID_DECL_LEVEL;
1420 if (fdi.m_copied_type && func_decl_context)
1421 {
1422 // Call CountDeclLevels to get the number of parent scopes we
1423 // have to look through before we find the function declaration.
1424 // When comparing functions of the same type, the one with a
1425 // lower count will be closer to us in the lookup scope and
1426 // shadows the other.
1427 clang::DeclContext *func_decl_ctx = (clang::DeclContext *)func_decl_context.GetOpaqueDeclContext();
1428 fdi.m_decl_lvl = ast->CountDeclLevels(frame_decl_ctx,
1429 func_decl_ctx,
1430 &fdi.m_name,
1431 &fdi.m_copied_type);
1432 }
1433 fdi_cache.emplace_back(fdi);
1434 }
1435
1436 // Loop through the functions in our cache looking for matching types,
1437 // then compare their scope levels to see which is closer.
1438 std::multimap<CompilerType, const FuncDeclInfo*> matches;
1439 for (const FuncDeclInfo &fdi : fdi_cache)
1440 {
1441 const CompilerType t = fdi.m_copied_type;
1442 auto q = matches.find(t);
1443 if (q != matches.end())
1444 {
1445 if (q->second->m_decl_lvl > fdi.m_decl_lvl)
1446 // This function is closer; remove the old set.
1447 matches.erase(t);
1448 else if (q->second->m_decl_lvl < fdi.m_decl_lvl)
1449 // The functions in our set are closer - skip this one.
1450 continue;
1451 }
1452 matches.insert(std::make_pair(t, &fdi));
1453 }
1454
1455 // Loop through our matches and add their symbol contexts to our list.
1456 SymbolContextList sc_func_list;
1457 for (const auto &q : matches)
1458 sc_func_list.Append(q.second->m_sym_ctx);
1459
1460 // Rejoin the lists with the functions in front.
1461 sc_list = sc_func_list;
1462 sc_list.Append(sc_sym_list);
1463 }
1464 }
1465
Sean Callanand9ca42a2011-05-08 02:21:26 +00001466 if (sc_list.GetSize())
Greg Clayton7b462cc2010-10-15 22:48:33 +00001467 {
Greg Claytondc25a0b2013-10-31 16:59:47 +00001468 Symbol *extern_symbol = NULL;
1469 Symbol *non_extern_symbol = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001470
Sean Callanand9ca42a2011-05-08 02:21:26 +00001471 for (uint32_t index = 0, num_indices = sc_list.GetSize();
1472 index < num_indices;
1473 ++index)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001474 {
Sean Callanand9ca42a2011-05-08 02:21:26 +00001475 SymbolContext sym_ctx;
1476 sc_list.GetContextAtIndex(index, sym_ctx);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001477
Sean Callanand9ca42a2011-05-08 02:21:26 +00001478 if (sym_ctx.function)
1479 {
Greg Clayton99558cc42015-08-24 23:46:31 +00001480 CompilerDeclContext decl_ctx = sym_ctx.function->GetDeclContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001481
Sean Callanan956dca92013-04-27 01:57:44 +00001482 if (!decl_ctx)
1483 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001484
Sean Callanan308a3c52012-07-28 00:21:01 +00001485 // Filter out class/instance methods.
Greg Clayton99558cc42015-08-24 23:46:31 +00001486 if (decl_ctx.IsClassMethod(nullptr, nullptr, nullptr))
Sean Callanan308a3c52012-07-28 00:21:01 +00001487 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001488
Sean Callanan485f7322013-04-24 00:34:41 +00001489 AddOneFunction(context, sym_ctx.function, NULL, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001490 context.m_found.function_with_type_info = true;
1491 context.m_found.function = true;
Sean Callanand9ca42a2011-05-08 02:21:26 +00001492 }
1493 else if (sym_ctx.symbol)
1494 {
Greg Claytonfc56a012014-06-13 21:57:58 +00001495 if (sym_ctx.symbol->GetType() == eSymbolTypeReExported && target)
Greg Claytondc25a0b2013-10-31 16:59:47 +00001496 {
1497 sym_ctx.symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target);
1498 if (sym_ctx.symbol == NULL)
1499 continue;
1500 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001501
Greg Claytondc25a0b2013-10-31 16:59:47 +00001502 if (sym_ctx.symbol->IsExternal())
1503 extern_symbol = sym_ctx.symbol;
Sean Callanand9ca42a2011-05-08 02:21:26 +00001504 else
Greg Claytondc25a0b2013-10-31 16:59:47 +00001505 non_extern_symbol = sym_ctx.symbol;
Sean Callanand9ca42a2011-05-08 02:21:26 +00001506 }
Greg Clayton7b462cc2010-10-15 22:48:33 +00001507 }
Sean Callananc8278af2014-12-05 01:27:35 +00001508
1509 if (!context.m_found.function_with_type_info)
1510 {
1511 for (clang::NamedDecl *decl : decls_from_modules)
1512 {
1513 if (llvm::isa<clang::FunctionDecl>(decl))
1514 {
Sean Callanan37e26642016-05-19 19:23:37 +00001515 clang::NamedDecl *copied_decl = llvm::cast_or_null<FunctionDecl>(m_ast_importer_sp->CopyDecl(m_ast_context, &decl->getASTContext(), decl));
1516 if (copied_decl)
1517 {
1518 context.AddNamedDecl(copied_decl);
1519 context.m_found.function_with_type_info = true;
1520 }
Sean Callananc8278af2014-12-05 01:27:35 +00001521 }
1522 }
1523 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001524
Sean Callananca4e0fd2011-10-12 20:29:25 +00001525 if (!context.m_found.function_with_type_info)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001526 {
Greg Claytondc25a0b2013-10-31 16:59:47 +00001527 if (extern_symbol)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001528 {
Greg Claytondc25a0b2013-10-31 16:59:47 +00001529 AddOneFunction (context, NULL, extern_symbol, current_id);
1530 context.m_found.function = true;
1531 }
1532 else if (non_extern_symbol)
1533 {
1534 AddOneFunction (context, NULL, non_extern_symbol, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001535 context.m_found.function = true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001536 }
Sean Callanand9ca42a2011-05-08 02:21:26 +00001537 }
Greg Clayton7b462cc2010-10-15 22:48:33 +00001538 }
Sean Callanan80c97592015-05-01 00:47:29 +00001539
1540 if (!context.m_found.function_with_type_info)
1541 {
1542 // Try the modules next.
1543
1544 do
1545 {
1546 if (ClangModulesDeclVendor *modules_decl_vendor = m_target->GetClangModulesDeclVendor())
1547 {
1548 bool append = false;
1549 uint32_t max_matches = 1;
1550 std::vector <clang::NamedDecl *> decls;
1551
1552 if (!modules_decl_vendor->FindDecls(name,
1553 append,
1554 max_matches,
1555 decls))
1556 break;
1557
1558 clang::NamedDecl *const decl_from_modules = decls[0];
1559
1560 if (llvm::isa<clang::FunctionDecl>(decl_from_modules))
1561 {
1562 if (log)
1563 {
1564 log->Printf(" CAS::FEVD[%u] Matching function found for \"%s\" in the modules",
1565 current_id,
1566 name.GetCString());
1567 }
1568
Sean Callanana3444ff2015-11-10 22:54:42 +00001569 clang::Decl *copied_decl = m_ast_importer_sp->CopyDecl(m_ast_context, &decl_from_modules->getASTContext(), decl_from_modules);
Sean Callanan80c97592015-05-01 00:47:29 +00001570 clang::FunctionDecl *copied_function_decl = copied_decl ? dyn_cast<clang::FunctionDecl>(copied_decl) : nullptr;
1571
1572 if (!copied_function_decl)
1573 {
1574 if (log)
1575 log->Printf(" CAS::FEVD[%u] - Couldn't export a function declaration from the modules",
1576 current_id);
1577
1578 break;
1579 }
1580
Sean Callananb3a36df2016-03-19 00:51:43 +00001581 MaybeRegisterFunctionBody(copied_function_decl);
Sean Callanan80c97592015-05-01 00:47:29 +00001582
1583 context.AddNamedDecl(copied_function_decl);
1584
1585 context.m_found.function_with_type_info = true;
1586 context.m_found.function = true;
1587 }
Sean Callanan9ff456c2015-07-10 17:34:23 +00001588 else if (llvm::isa<clang::VarDecl>(decl_from_modules))
1589 {
1590 if (log)
1591 {
1592 log->Printf(" CAS::FEVD[%u] Matching variable found for \"%s\" in the modules",
1593 current_id,
1594 name.GetCString());
1595 }
1596
Sean Callanana3444ff2015-11-10 22:54:42 +00001597 clang::Decl *copied_decl = m_ast_importer_sp->CopyDecl(m_ast_context, &decl_from_modules->getASTContext(), decl_from_modules);
Sean Callanan9ff456c2015-07-10 17:34:23 +00001598 clang::VarDecl *copied_var_decl = copied_decl ? dyn_cast_or_null<clang::VarDecl>(copied_decl) : nullptr;
1599
1600 if (!copied_var_decl)
1601 {
1602 if (log)
1603 log->Printf(" CAS::FEVD[%u] - Couldn't export a variable declaration from the modules",
1604 current_id);
1605
1606 break;
1607 }
1608
1609 context.AddNamedDecl(copied_var_decl);
1610
1611 context.m_found.variable = true;
1612 }
Sean Callanan80c97592015-05-01 00:47:29 +00001613 }
1614 } while (0);
1615 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001616
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001617 if (target && !context.m_found.variable && !namespace_decl)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001618 {
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001619 // We couldn't find a non-symbol variable for this. Now we'll hunt for a generic
Sean Callanand9ca42a2011-05-08 02:21:26 +00001620 // data symbol, and -- if it is found -- treat it as a variable.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001621
Greg Clayton3d51b9f2012-11-27 01:52:16 +00001622 const Symbol *data_symbol = FindGlobalDataSymbol(*target, name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001623
Sean Callanand9ca42a2011-05-08 02:21:26 +00001624 if (data_symbol)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001625 {
Sean Callanan92cdbc82014-02-19 23:37:25 +00001626 std::string warning("got name from symbols: ");
1627 warning.append(name.AsCString());
1628 const unsigned diag_id = m_ast_context->getDiagnostics().getCustomDiagID(clang::DiagnosticsEngine::Level::Warning, "%0");
1629 m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str();
Sean Callanan7dd98122011-10-14 20:34:21 +00001630 AddOneGenericVariable(context, *data_symbol, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001631 context.m_found.variable = true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001632 }
Greg Clayton526e5af2010-11-13 03:52:47 +00001633 }
Sean Callanan3883b5a2010-09-07 21:49:41 +00001634 }
Sean Callananfb40b0d2011-10-27 02:06:03 +00001635 }
Sean Callanan6b4067c2010-07-17 00:43:37 +00001636}
Greg Claytona2721472011-06-25 00:44:06 +00001637
Bruce Mitchener23a3b0e2015-09-22 17:04:24 +00001638//static opaque_compiler_type_t
Saleem Abdulrasoola68f7b62014-03-20 06:08:36 +00001639//MaybePromoteToBlockPointerType
1640//(
1641// ASTContext *ast_context,
Bruce Mitchener23a3b0e2015-09-22 17:04:24 +00001642// opaque_compiler_type_t candidate_type
Saleem Abdulrasoola68f7b62014-03-20 06:08:36 +00001643//)
1644//{
1645// if (!candidate_type)
1646// return candidate_type;
1647//
1648// QualType candidate_qual_type = QualType::getFromOpaquePtr(candidate_type);
1649//
1650// const PointerType *candidate_pointer_type = dyn_cast<PointerType>(candidate_qual_type);
1651//
1652// if (!candidate_pointer_type)
1653// return candidate_type;
1654//
1655// QualType pointee_qual_type = candidate_pointer_type->getPointeeType();
1656//
1657// const RecordType *pointee_record_type = dyn_cast<RecordType>(pointee_qual_type);
1658//
1659// if (!pointee_record_type)
1660// return candidate_type;
1661//
1662// RecordDecl *pointee_record_decl = pointee_record_type->getDecl();
1663//
1664// if (!pointee_record_decl->isRecord())
1665// return candidate_type;
1666//
1667// if (!pointee_record_decl->getName().startswith(llvm::StringRef("__block_literal_")))
1668// return candidate_type;
1669//
1670// QualType generic_function_type = ast_context->getFunctionNoProtoType(ast_context->UnknownAnyTy);
1671// QualType block_pointer_type = ast_context->getBlockPointerType(generic_function_type);
1672//
1673// return block_pointer_type.getAsOpaquePtr();
1674//}
Sean Callanan75383bf2012-03-06 21:56:33 +00001675
Greg Clayton57ee3062013-07-11 22:46:58 +00001676bool
1677ClangExpressionDeclMap::GetVariableValue (VariableSP &var,
1678 lldb_private::Value &var_location,
1679 TypeFromUser *user_type,
1680 TypeFromParser *parser_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001681{
Greg Clayton5160ce52013-03-27 23:08:40 +00001682 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001683
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001684 Type *var_type = var->GetType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001685
1686 if (!var_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001687 {
Sean Callananea22d422010-07-16 00:09:46 +00001688 if (log)
1689 log->PutCString("Skipped a definition because it has no type");
Greg Clayton57ee3062013-07-11 22:46:58 +00001690 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001691 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001692
Greg Clayton99558cc42015-08-24 23:46:31 +00001693 CompilerType var_clang_type = var_type->GetFullCompilerType ();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001694
Greg Clayton57ee3062013-07-11 22:46:58 +00001695 if (!var_clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001696 {
Sean Callananea22d422010-07-16 00:09:46 +00001697 if (log)
1698 log->PutCString("Skipped a definition because it has no Clang type");
Greg Clayton57ee3062013-07-11 22:46:58 +00001699 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001700 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001701
Greg Clayton56939cb2015-09-17 22:23:34 +00001702 ClangASTContext *clang_ast = llvm::dyn_cast_or_null<ClangASTContext>(var_type->GetForwardCompilerType().GetTypeSystem());
1703
1704 if (!clang_ast)
1705 {
1706 if (log)
1707 log->PutCString("Skipped a definition because it has no Clang AST");
1708 return false;
1709 }
1710
1711
1712 ASTContext *ast = clang_ast->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001713
Greg Clayton6beaaa62011-01-17 03:46:26 +00001714 if (!ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001715 {
Sean Callananea22d422010-07-16 00:09:46 +00001716 if (log)
1717 log->PutCString("There is no AST context for the current execution context");
Greg Clayton57ee3062013-07-11 22:46:58 +00001718 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001719 }
Greg Clayton57ee3062013-07-11 22:46:58 +00001720 //var_clang_type = MaybePromoteToBlockPointerType (ast, var_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001721
Sean Callanan6b4067c2010-07-17 00:43:37 +00001722 DWARFExpression &var_location_expr = var->LocationExpression();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001723
Sean Callanan933693b2012-02-10 01:22:05 +00001724 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001725 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001726
Sean Callanan7b5805d2013-01-18 21:20:51 +00001727 if (var->GetLocationIsConstantValueData())
1728 {
1729 DataExtractor const_value_extractor;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001730
Sean Callanan7b5805d2013-01-18 21:20:51 +00001731 if (var_location_expr.GetExpressionData(const_value_extractor))
1732 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001733 var_location = Value(const_value_extractor.GetDataStart(), const_value_extractor.GetByteSize());
1734 var_location.SetValueType(Value::eValueTypeHostAddress);
Sean Callanan7b5805d2013-01-18 21:20:51 +00001735 }
1736 else
1737 {
1738 if (log)
1739 log->Printf("Error evaluating constant variable: %s", err.AsCString());
Greg Clayton57ee3062013-07-11 22:46:58 +00001740 return false;
Sean Callanan7b5805d2013-01-18 21:20:51 +00001741 }
1742 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001743
Greg Claytona1e5dc82015-08-11 22:53:00 +00001744 CompilerType type_to_use = GuardedCopyType(var_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001745
Greg Clayton57ee3062013-07-11 22:46:58 +00001746 if (!type_to_use)
Sean Callanan1d180662010-07-20 23:31:16 +00001747 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001748 if (log)
1749 log->Printf("Couldn't copy a variable's type into the parser's AST context");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001750
Greg Clayton57ee3062013-07-11 22:46:58 +00001751 return false;
Sean Callanan1d180662010-07-20 23:31:16 +00001752 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001753
Greg Clayton57ee3062013-07-11 22:46:58 +00001754 if (parser_type)
1755 *parser_type = TypeFromParser(type_to_use);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001756
Greg Clayton57ee3062013-07-11 22:46:58 +00001757 if (var_location.GetContextType() == Value::eContextTypeInvalid)
Greg Clayton99558cc42015-08-24 23:46:31 +00001758 var_location.SetCompilerType(type_to_use);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001759
Greg Clayton57ee3062013-07-11 22:46:58 +00001760 if (var_location.GetValueType() == Value::eValueTypeFileAddress)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001761 {
1762 SymbolContext var_sc;
1763 var->CalculateSymbolContext(&var_sc);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001764
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001765 if (!var_sc.module_sp)
Michael Sartain89c862f2013-08-07 19:05:15 +00001766 return false;
Greg Clayton3046e662013-07-10 01:23:25 +00001767
Greg Clayton57ee3062013-07-11 22:46:58 +00001768 Address so_addr(var_location.GetScalar().ULongLong(), var_sc.module_sp->GetSectionList());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001769
Greg Claytonc14ee322011-09-22 04:58:26 +00001770 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001771
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001772 if (load_addr != LLDB_INVALID_ADDRESS)
1773 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001774 var_location.GetScalar() = load_addr;
1775 var_location.SetValueType(Value::eValueTypeLoadAddress);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001776 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001777 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001778
Sean Callanan1d180662010-07-20 23:31:16 +00001779 if (user_type)
Greg Clayton57ee3062013-07-11 22:46:58 +00001780 *user_type = TypeFromUser(var_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001781
Greg Clayton57ee3062013-07-11 22:46:58 +00001782 return true;
Sean Callanan6b4067c2010-07-17 00:43:37 +00001783}
1784
1785void
Sean Callanan0712f462011-11-29 22:03:21 +00001786ClangExpressionDeclMap::AddOneVariable (NameSearchContext &context, VariableSP var, ValueObjectSP valobj, unsigned int current_id)
Sean Callanan6b4067c2010-07-17 00:43:37 +00001787{
Sean Callanan979f74d2010-12-03 01:38:59 +00001788 assert (m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001789
Greg Clayton5160ce52013-03-27 23:08:40 +00001790 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001791
Sean Callanan1d180662010-07-20 23:31:16 +00001792 TypeFromUser ut;
1793 TypeFromParser pt;
Greg Clayton57ee3062013-07-11 22:46:58 +00001794 Value var_location;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001795
Greg Clayton57ee3062013-07-11 22:46:58 +00001796 if (!GetVariableValue (var, var_location, &ut, &pt))
1797 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001798
Sean Callanancf128622012-03-15 01:53:17 +00001799 clang::QualType parser_opaque_type = QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001800
Sean Callanane2d47482012-03-21 17:13:20 +00001801 if (parser_opaque_type.isNull())
1802 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001803
Sean Callanancf128622012-03-15 01:53:17 +00001804 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr())
1805 {
1806 if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1807 CompleteType(tag_type->getDecl());
Sean Callananf4c0a222013-12-20 04:09:05 +00001808 if (const ObjCObjectPointerType *objc_object_ptr_type = dyn_cast<ObjCObjectPointerType>(parser_type))
1809 CompleteType(objc_object_ptr_type->getInterfaceDecl());
Sean Callanancf128622012-03-15 01:53:17 +00001810 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001811
1812
Greg Clayton57ee3062013-07-11 22:46:58 +00001813 bool is_reference = pt.IsReferenceType();
Sean Callanan98298012011-10-27 19:41:13 +00001814
Greg Clayton57ee3062013-07-11 22:46:58 +00001815 NamedDecl *var_decl = NULL;
Sean Callanan98298012011-10-27 19:41:13 +00001816 if (is_reference)
Greg Clayton57ee3062013-07-11 22:46:58 +00001817 var_decl = context.AddVarDecl(pt);
Sean Callanan98298012011-10-27 19:41:13 +00001818 else
Greg Clayton56939cb2015-09-17 22:23:34 +00001819 var_decl = context.AddVarDecl(pt.GetLValueReferenceType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001820
Greg Clayton7b462cc2010-10-15 22:48:33 +00001821 std::string decl_name(context.m_decl_name.getAsString());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001822 ConstString entity_name(decl_name.c_str());
Sean Callanan9301ec12015-10-01 23:07:06 +00001823 ClangExpressionVariable *entity(new ClangExpressionVariable(valobj));
1824 m_found_entities.AddNewlyConstructedVariable(entity);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001825
Sean Callananbc8ac342015-09-04 20:49:51 +00001826 assert (entity);
Sean Callanan3c495c12013-01-15 23:29:36 +00001827 entity->EnableParserVars(GetParserID());
1828 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1829 parser_vars->m_parser_type = pt;
1830 parser_vars->m_named_decl = var_decl;
1831 parser_vars->m_llvm_value = NULL;
1832 parser_vars->m_lldb_value = var_location;
1833 parser_vars->m_lldb_var = var;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001834
Sean Callanan98298012011-10-27 19:41:13 +00001835 if (is_reference)
1836 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001837
Sean Callananea22d422010-07-16 00:09:46 +00001838 if (log)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001839 {
Sean Callanan82695d62011-11-07 23:32:52 +00001840 ASTDumper orig_dumper(ut.GetOpaqueQualType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001841 ASTDumper ast_dumper(var_decl);
Sean Callanan82695d62011-11-07 23:32:52 +00001842 log->Printf(" CEDM::FEVD[%u] Found variable %s, returned %s (original %s)", current_id, decl_name.c_str(), ast_dumper.GetCString(), orig_dumper.GetCString());
Greg Clayton7b462cc2010-10-15 22:48:33 +00001843 }
Sean Callanan468574b2010-06-22 23:46:24 +00001844}
1845
1846void
Sean Callanan2235f322010-08-11 03:57:18 +00001847ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
Sean Callananbc8ac342015-09-04 20:49:51 +00001848 ExpressionVariableSP &pvar_sp,
Sean Callanan7dd98122011-10-14 20:34:21 +00001849 unsigned int current_id)
Sean Callanan2235f322010-08-11 03:57:18 +00001850{
Greg Clayton5160ce52013-03-27 23:08:40 +00001851 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001852
Sean Callanan3820e9c2015-09-08 22:23:39 +00001853 TypeFromUser user_type (llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetTypeFromUser());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001854
Greg Clayton57ee3062013-07-11 22:46:58 +00001855 TypeFromParser parser_type (GuardedCopyType(user_type));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001856
Sean Callanane0a64f72011-12-01 21:04:37 +00001857 if (!parser_type.GetOpaqueQualType())
1858 {
1859 if (log)
1860 log->Printf(" CEDM::FEVD[%u] Couldn't import type for pvar %s", current_id, pvar_sp->GetName().GetCString());
1861 return;
1862 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001863
Greg Clayton56939cb2015-09-17 22:23:34 +00001864 NamedDecl *var_decl = context.AddVarDecl(parser_type.GetLValueReferenceType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001865
Sean Callanan3820e9c2015-09-08 22:23:39 +00001866 llvm::cast<ClangExpressionVariable>(pvar_sp.get())->EnableParserVars(GetParserID());
1867 ClangExpressionVariable::ParserVars *parser_vars = llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetParserVars(GetParserID());
Sean Callanan3c495c12013-01-15 23:29:36 +00001868 parser_vars->m_parser_type = parser_type;
Greg Clayton57ee3062013-07-11 22:46:58 +00001869 parser_vars->m_named_decl = var_decl;
1870 parser_vars->m_llvm_value = NULL;
1871 parser_vars->m_lldb_value.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001872
Sean Callanan823bb4c2010-08-30 22:17:16 +00001873 if (log)
Sean Callanan530782942010-11-01 23:22:47 +00001874 {
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001875 ASTDumper ast_dumper(var_decl);
Sean Callananfb3e4302011-10-29 19:50:43 +00001876 log->Printf(" CEDM::FEVD[%u] Added pvar %s, returned %s", current_id, pvar_sp->GetName().GetCString(), ast_dumper.GetCString());
Sean Callanan530782942010-11-01 23:22:47 +00001877 }
Sean Callanan2235f322010-08-11 03:57:18 +00001878}
1879
Sean Callanan348b5892010-11-30 00:27:43 +00001880void
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001881ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context,
Greg Clayton3d51b9f2012-11-27 01:52:16 +00001882 const Symbol &symbol,
Sean Callanan7dd98122011-10-14 20:34:21 +00001883 unsigned int current_id)
Sean Callanand9ca42a2011-05-08 02:21:26 +00001884{
1885 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001886
Greg Clayton5160ce52013-03-27 23:08:40 +00001887 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001888
Sean Callanan933693b2012-02-10 01:22:05 +00001889 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00001890
1891 if (target == NULL)
1892 return;
1893
1894 ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001895
Greg Clayton56939cb2015-09-17 22:23:34 +00001896 TypeFromUser user_type (ClangASTContext::GetBasicType(scratch_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType());
1897 TypeFromParser parser_type (ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType());
Greg Clayton57ee3062013-07-11 22:46:58 +00001898 NamedDecl *var_decl = context.AddVarDecl(parser_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001899
Sean Callanand9ca42a2011-05-08 02:21:26 +00001900 std::string decl_name(context.m_decl_name.getAsString());
1901 ConstString entity_name(decl_name.c_str());
Sean Callanan9301ec12015-10-01 23:07:06 +00001902 ClangExpressionVariable *entity(new ClangExpressionVariable(m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (),
1903 entity_name,
1904 user_type,
1905 m_parser_vars->m_target_info.byte_order,
1906 m_parser_vars->m_target_info.address_byte_size));
1907 m_found_entities.AddNewlyConstructedVariable(entity);
1908
Greg Clayton57ee3062013-07-11 22:46:58 +00001909 entity->EnableParserVars(GetParserID());
1910 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1911
Greg Clayton358cf1e2015-06-25 21:46:34 +00001912 const Address symbol_address = symbol.GetAddress();
Greg Claytonc14ee322011-09-22 04:58:26 +00001913 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001914
Greg Clayton57ee3062013-07-11 22:46:58 +00001915 //parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType());
Greg Clayton99558cc42015-08-24 23:46:31 +00001916 parser_vars->m_lldb_value.SetCompilerType(user_type);
Greg Clayton57ee3062013-07-11 22:46:58 +00001917 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr;
1918 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001919
Sean Callanan3c495c12013-01-15 23:29:36 +00001920 parser_vars->m_parser_type = parser_type;
1921 parser_vars->m_named_decl = var_decl;
1922 parser_vars->m_llvm_value = NULL;
Sean Callanan3c495c12013-01-15 23:29:36 +00001923 parser_vars->m_lldb_sym = &symbol;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001924
Sean Callanand9ca42a2011-05-08 02:21:26 +00001925 if (log)
1926 {
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001927 ASTDumper ast_dumper(var_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001928
Sean Callananfb3e4302011-10-29 19:50:43 +00001929 log->Printf(" CEDM::FEVD[%u] Found variable %s, returned %s", current_id, decl_name.c_str(), ast_dumper.GetCString());
Sean Callanand9ca42a2011-05-08 02:21:26 +00001930 }
1931}
1932
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001933bool
Sean Callanan77502262011-05-12 23:54:16 +00001934ClangExpressionDeclMap::ResolveUnknownTypes()
1935{
Greg Clayton5160ce52013-03-27 23:08:40 +00001936 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan933693b2012-02-10 01:22:05 +00001937 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00001938
Greg Claytond8d4a572015-08-11 21:38:15 +00001939 ClangASTContext *scratch_ast_context = target->GetScratchClangASTContext();
Sean Callanan77502262011-05-12 23:54:16 +00001940
1941 for (size_t index = 0, num_entities = m_found_entities.GetSize();
1942 index < num_entities;
1943 ++index)
1944 {
Sean Callananbc8ac342015-09-04 20:49:51 +00001945 ExpressionVariableSP entity = m_found_entities.GetVariableAtIndex(index);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001946
Sean Callanan3820e9c2015-09-08 22:23:39 +00001947 ClangExpressionVariable::ParserVars *parser_vars = llvm::cast<ClangExpressionVariable>(entity.get())->GetParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001948
Sean Callanan77502262011-05-12 23:54:16 +00001949 if (entity->m_flags & ClangExpressionVariable::EVUnknownType)
1950 {
Sean Callanan3c495c12013-01-15 23:29:36 +00001951 const NamedDecl *named_decl = parser_vars->m_named_decl;
Sean Callanan77502262011-05-12 23:54:16 +00001952 const VarDecl *var_decl = dyn_cast<VarDecl>(named_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001953
Sean Callanan77502262011-05-12 23:54:16 +00001954 if (!var_decl)
1955 {
1956 if (log)
1957 log->Printf("Entity of unknown type does not have a VarDecl");
1958 return false;
1959 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001960
Sean Callanan77502262011-05-12 23:54:16 +00001961 if (log)
1962 {
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001963 ASTDumper ast_dumper(const_cast<VarDecl*>(var_decl));
1964 log->Printf("Variable of unknown type now has Decl %s", ast_dumper.GetCString());
Sean Callanan77502262011-05-12 23:54:16 +00001965 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001966
Sean Callanan77502262011-05-12 23:54:16 +00001967 QualType var_type = var_decl->getType();
Greg Claytond8d4a572015-08-11 21:38:15 +00001968 TypeFromParser parser_type(var_type.getAsOpaquePtr(), ClangASTContext::GetASTContext(&var_decl->getASTContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001969
Sean Callanana3444ff2015-11-10 22:54:42 +00001970 lldb::opaque_compiler_type_t copied_type = m_ast_importer_sp->CopyType(scratch_ast_context->getASTContext(), &var_decl->getASTContext(), var_type.getAsOpaquePtr());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001971
Sean Callanane0a64f72011-12-01 21:04:37 +00001972 if (!copied_type)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001973 {
Sean Callanane0a64f72011-12-01 21:04:37 +00001974 if (log)
1975 log->Printf("ClangExpressionDeclMap::ResolveUnknownType - Couldn't import the type for a variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001976
Sean Callananbc8ac342015-09-04 20:49:51 +00001977 return (bool) lldb::ExpressionVariableSP();
Sean Callanane0a64f72011-12-01 21:04:37 +00001978 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001979
Sean Callanan77502262011-05-12 23:54:16 +00001980 TypeFromUser user_type(copied_type, scratch_ast_context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001981
Greg Clayton57ee3062013-07-11 22:46:58 +00001982// parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType());
Greg Clayton99558cc42015-08-24 23:46:31 +00001983 parser_vars->m_lldb_value.SetCompilerType(user_type);
Sean Callanan3c495c12013-01-15 23:29:36 +00001984 parser_vars->m_parser_type = parser_type;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001985
Greg Clayton99558cc42015-08-24 23:46:31 +00001986 entity->SetCompilerType(user_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001987
Sean Callanan77502262011-05-12 23:54:16 +00001988 entity->m_flags &= ~(ClangExpressionVariable::EVUnknownType);
1989 }
1990 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001991
Sean Callanan77502262011-05-12 23:54:16 +00001992 return true;
1993}
1994
Sean Callanand9ca42a2011-05-08 02:21:26 +00001995void
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001996ClangExpressionDeclMap::AddOneRegister (NameSearchContext &context,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001997 const RegisterInfo *reg_info,
Sean Callanan7dd98122011-10-14 20:34:21 +00001998 unsigned int current_id)
Sean Callanan348b5892010-11-30 00:27:43 +00001999{
Greg Clayton5160ce52013-03-27 23:08:40 +00002000 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002001
Greg Claytona1e5dc82015-08-11 22:53:00 +00002002 CompilerType clang_type = ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (m_ast_context,
Greg Clayton57ee3062013-07-11 22:46:58 +00002003 reg_info->encoding,
2004 reg_info->byte_size * 8);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002005
Greg Clayton57ee3062013-07-11 22:46:58 +00002006 if (!clang_type)
Sean Callanan348b5892010-11-30 00:27:43 +00002007 {
Johnny Chene95fcf72011-08-09 22:52:27 +00002008 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +00002009 log->Printf(" Tried to add a type for %s, but couldn't get one", context.m_decl_name.getAsString().c_str());
Sean Callanan348b5892010-11-30 00:27:43 +00002010 return;
2011 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002012
Greg Clayton57ee3062013-07-11 22:46:58 +00002013 TypeFromParser parser_clang_type (clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002014
Greg Clayton57ee3062013-07-11 22:46:58 +00002015 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002016
Sean Callanan9301ec12015-10-01 23:07:06 +00002017 ClangExpressionVariable *entity(new ClangExpressionVariable(m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
2018 m_parser_vars->m_target_info.byte_order,
2019 m_parser_vars->m_target_info.address_byte_size));
2020 m_found_entities.AddNewlyConstructedVariable(entity);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002021
Sean Callanan348b5892010-11-30 00:27:43 +00002022 std::string decl_name(context.m_decl_name.getAsString());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002023 entity->SetName (ConstString (decl_name.c_str()));
2024 entity->SetRegisterInfo (reg_info);
Sean Callanan3c495c12013-01-15 23:29:36 +00002025 entity->EnableParserVars(GetParserID());
2026 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
Greg Clayton57ee3062013-07-11 22:46:58 +00002027 parser_vars->m_parser_type = parser_clang_type;
2028 parser_vars->m_named_decl = var_decl;
2029 parser_vars->m_llvm_value = NULL;
2030 parser_vars->m_lldb_value.Clear();
Sean Callananf673e762012-02-15 01:40:39 +00002031 entity->m_flags |= ClangExpressionVariable::EVBareRegister;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002032
Sean Callananf673e762012-02-15 01:40:39 +00002033 if (log)
Sean Callanan348b5892010-11-30 00:27:43 +00002034 {
Greg Clayton64bc6ca2011-10-20 00:47:21 +00002035 ASTDumper ast_dumper(var_decl);
Sean Callananfb3e4302011-10-29 19:50:43 +00002036 log->Printf(" CEDM::FEVD[%d] Added register %s, returned %s", current_id, context.m_decl_name.getAsString().c_str(), ast_dumper.GetCString());
Sean Callanan348b5892010-11-30 00:27:43 +00002037 }
2038}
2039
Sean Callanan2235f322010-08-11 03:57:18 +00002040void
Greg Clayton3f5c08f2011-05-18 22:01:49 +00002041ClangExpressionDeclMap::AddOneFunction (NameSearchContext &context,
Greg Clayton57ee3062013-07-11 22:46:58 +00002042 Function* function,
Sean Callanan7dd98122011-10-14 20:34:21 +00002043 Symbol* symbol,
2044 unsigned int current_id)
Sean Callanan468574b2010-06-22 23:46:24 +00002045{
Sean Callanan979f74d2010-12-03 01:38:59 +00002046 assert (m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002047
Greg Clayton5160ce52013-03-27 23:08:40 +00002048 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002049
Greg Clayton57ee3062013-07-11 22:46:58 +00002050 NamedDecl *function_decl = NULL;
Greg Clayton358cf1e2015-06-25 21:46:34 +00002051 Address fun_address;
Greg Claytona1e5dc82015-08-11 22:53:00 +00002052 CompilerType function_clang_type;
Matt Kopec00049b82013-02-27 20:13:38 +00002053
2054 bool is_indirect_function = false;
2055
Greg Clayton57ee3062013-07-11 22:46:58 +00002056 if (function)
Sean Callanan8ade1042010-07-27 00:55:47 +00002057 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002058 Type *function_type = function->GetType();
Sean Callanan2a8fa2a2016-02-13 00:01:46 +00002059
2060 const lldb::LanguageType comp_unit_language = function->GetCompileUnit()->GetLanguage();
2061 const bool extern_c = Language::LanguageIsC(comp_unit_language) ||
2062 (Language::LanguageIsObjC(comp_unit_language) &&
2063 !Language::LanguageIsCPlusPlus(comp_unit_language));
2064
2065 if (!extern_c)
2066 {
2067 TypeSystem *type_system = function->GetDeclContext().GetTypeSystem();
2068 if (ClangASTContext *src_ast = llvm::dyn_cast<ClangASTContext>(type_system))
2069 {
2070 clang::DeclContext *src_decl_context = (clang::DeclContext*)function->GetDeclContext().GetOpaqueDeclContext();
2071 clang::FunctionDecl *src_function_decl = llvm::dyn_cast_or_null<clang::FunctionDecl>(src_decl_context);
2072
2073 if (src_function_decl)
2074 {
2075 if (clang::FunctionDecl *copied_function_decl = llvm::dyn_cast_or_null<clang::FunctionDecl>(m_ast_importer_sp->CopyDecl(m_ast_context, src_ast->getASTContext(), src_function_decl)))
2076 {
2077 if (log)
2078 {
2079 ASTDumper ast_dumper((clang::Decl*)copied_function_decl);
2080
2081 StreamString ss;
2082
2083 function->DumpSymbolContext(&ss);
Pavel Labath1ac2b202016-08-15 14:32:32 +00002084
Sean Callanan2a8fa2a2016-02-13 00:01:46 +00002085 log->Printf(" CEDM::FEVD[%u] Imported decl for function %s (description %s), returned %s",
Pavel Labath1ac2b202016-08-15 14:32:32 +00002086 current_id, copied_function_decl->getNameAsString().c_str(), ss.GetData(),
Sean Callanan2a8fa2a2016-02-13 00:01:46 +00002087 ast_dumper.GetCString());
Sean Callanan2a8fa2a2016-02-13 00:01:46 +00002088 }
2089
2090 context.AddNamedDecl(copied_function_decl);
2091 return;
2092 }
2093 else
2094 {
2095 if (log)
2096 {
2097 log->Printf (" Failed to import the function decl for '%s'",
2098 src_function_decl->getName().str().c_str());
2099 }
2100 }
2101 }
2102 }
2103 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002104
Greg Clayton57ee3062013-07-11 22:46:58 +00002105 if (!function_type)
Sean Callanan8ade1042010-07-27 00:55:47 +00002106 {
2107 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +00002108 log->PutCString(" Skipped a function because it has no type");
Sean Callanan8ade1042010-07-27 00:55:47 +00002109 return;
2110 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002111
Greg Clayton99558cc42015-08-24 23:46:31 +00002112 function_clang_type = function_type->GetFullCompilerType ();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002113
Greg Clayton57ee3062013-07-11 22:46:58 +00002114 if (!function_clang_type)
Sean Callanan8ade1042010-07-27 00:55:47 +00002115 {
2116 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +00002117 log->PutCString(" Skipped a function because it has no Clang type");
Sean Callanan8ade1042010-07-27 00:55:47 +00002118 return;
2119 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002120
Greg Clayton358cf1e2015-06-25 21:46:34 +00002121 fun_address = function->GetAddressRange().GetBaseAddress();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002122
Greg Claytona1e5dc82015-08-11 22:53:00 +00002123 CompilerType copied_function_type = GuardedCopyType(function_clang_type);
Greg Clayton57ee3062013-07-11 22:46:58 +00002124 if (copied_function_type)
Greg Clayton64bc6ca2011-10-20 00:47:21 +00002125 {
Sean Callanan2a8fa2a2016-02-13 00:01:46 +00002126 function_decl = context.AddFunDecl(copied_function_type, extern_c);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002127
Greg Clayton57ee3062013-07-11 22:46:58 +00002128 if (!function_decl)
Sean Callanan485f7322013-04-24 00:34:41 +00002129 {
2130 if (log)
2131 {
2132 log->Printf (" Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}",
Greg Clayton57ee3062013-07-11 22:46:58 +00002133 function_type->GetName().GetCString(),
2134 function_type->GetID());
Sean Callanan485f7322013-04-24 00:34:41 +00002135 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002136
Sean Callanan485f7322013-04-24 00:34:41 +00002137 return;
2138 }
Greg Clayton64bc6ca2011-10-20 00:47:21 +00002139 }
2140 else
2141 {
2142 // We failed to copy the type we found
2143 if (log)
2144 {
Daniel Malead01b2952012-11-29 21:49:15 +00002145 log->Printf (" Failed to import the function type '%s' {0x%8.8" PRIx64 "} into the expression parser AST contenxt",
Greg Clayton57ee3062013-07-11 22:46:58 +00002146 function_type->GetName().GetCString(),
2147 function_type->GetID());
Greg Clayton64bc6ca2011-10-20 00:47:21 +00002148 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002149
Sean Callananed8d58f2011-11-16 00:40:13 +00002150 return;
Greg Clayton64bc6ca2011-10-20 00:47:21 +00002151 }
Sean Callanan8ade1042010-07-27 00:55:47 +00002152 }
2153 else if (symbol)
2154 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00002155 fun_address = symbol->GetAddress();
Greg Clayton57ee3062013-07-11 22:46:58 +00002156 function_decl = context.AddGenericFunDecl();
Matt Kopec00049b82013-02-27 20:13:38 +00002157 is_indirect_function = symbol->IsIndirect();
Sean Callanan8ade1042010-07-27 00:55:47 +00002158 }
2159 else
2160 {
2161 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +00002162 log->PutCString(" AddOneFunction called with no function and no symbol");
Sean Callanan8ade1042010-07-27 00:55:47 +00002163 return;
2164 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002165
Sean Callanan933693b2012-02-10 01:22:05 +00002166 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002167
Greg Clayton358cf1e2015-06-25 21:46:34 +00002168 lldb::addr_t load_addr = fun_address.GetCallableLoadAddress(target, is_indirect_function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002169
Sean Callanan9301ec12015-10-01 23:07:06 +00002170 ClangExpressionVariable *entity(new ClangExpressionVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (),
2171 m_parser_vars->m_target_info.byte_order,
2172 m_parser_vars->m_target_info.address_byte_size));
2173 m_found_entities.AddNewlyConstructedVariable(entity);
Greg Clayton57ee3062013-07-11 22:46:58 +00002174
2175 std::string decl_name(context.m_decl_name.getAsString());
2176 entity->SetName(ConstString(decl_name.c_str()));
Greg Clayton99558cc42015-08-24 23:46:31 +00002177 entity->SetCompilerType (function_clang_type);
Greg Clayton57ee3062013-07-11 22:46:58 +00002178 entity->EnableParserVars(GetParserID());
2179
2180 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002181
Sean Callanan9be9d172013-03-19 01:45:02 +00002182 if (load_addr != LLDB_INVALID_ADDRESS)
2183 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002184 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
2185 parser_vars->m_lldb_value.GetScalar() = load_addr;
Sean Callanan9be9d172013-03-19 01:45:02 +00002186 }
2187 else
2188 {
2189 // We have to try finding a file address.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002190
Greg Clayton358cf1e2015-06-25 21:46:34 +00002191 lldb::addr_t file_addr = fun_address.GetFileAddress();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002192
Greg Clayton57ee3062013-07-11 22:46:58 +00002193 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress);
2194 parser_vars->m_lldb_value.GetScalar() = file_addr;
Sean Callanan9be9d172013-03-19 01:45:02 +00002195 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002196
Greg Clayton57ee3062013-07-11 22:46:58 +00002197
2198 parser_vars->m_named_decl = function_decl;
Sean Callanan3c495c12013-01-15 23:29:36 +00002199 parser_vars->m_llvm_value = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002200
Sean Callananea22d422010-07-16 00:09:46 +00002201 if (log)
Greg Clayton7b462cc2010-10-15 22:48:33 +00002202 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002203 ASTDumper ast_dumper(function_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002204
Sean Callanan308a3c52012-07-28 00:21:01 +00002205 StreamString ss;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002206
Greg Clayton358cf1e2015-06-25 21:46:34 +00002207 fun_address.Dump(&ss, m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), Address::DumpStyleResolvedDescription);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002208
Sean Callanan308a3c52012-07-28 00:21:01 +00002209 log->Printf(" CEDM::FEVD[%u] Found %s function %s (description %s), returned %s",
Sean Callanan7dd98122011-10-14 20:34:21 +00002210 current_id,
Greg Clayton57ee3062013-07-11 22:46:58 +00002211 (function ? "specific" : "generic"),
Sean Callanan308a3c52012-07-28 00:21:01 +00002212 decl_name.c_str(),
2213 ss.GetData(),
Greg Clayton64bc6ca2011-10-20 00:47:21 +00002214 ast_dumper.GetCString());
Greg Clayton7b462cc2010-10-15 22:48:33 +00002215 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002216}
Sean Callanan5666b672010-08-04 01:02:13 +00002217
Sean Callanan3b436d22015-10-23 21:45:02 +00002218void
2219ClangExpressionDeclMap::AddThisType(NameSearchContext &context,
2220 TypeFromUser &ut,
2221 unsigned int current_id)
Sean Callanan5666b672010-08-04 01:02:13 +00002222{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002223 CompilerType copied_clang_type = GuardedCopyType(ut);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002224
Sean Callanan7736a202016-04-29 18:09:03 +00002225 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2226
Greg Clayton57ee3062013-07-11 22:46:58 +00002227 if (!copied_clang_type)
Sean Callanane0a64f72011-12-01 21:04:37 +00002228 {
Sean Callananfa4fab72013-02-01 06:55:48 +00002229 if (log)
Sean Callanan3b436d22015-10-23 21:45:02 +00002230 log->Printf("ClangExpressionDeclMap::AddThisType - Couldn't import the type");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002231
Sean Callanan3b436d22015-10-23 21:45:02 +00002232 return;
Sean Callanane0a64f72011-12-01 21:04:37 +00002233 }
Sean Callananfa4fab72013-02-01 06:55:48 +00002234
Greg Clayton57ee3062013-07-11 22:46:58 +00002235 if (copied_clang_type.IsAggregateType() && copied_clang_type.GetCompleteType ())
Sean Callananfc55f5d2010-09-21 00:44:12 +00002236 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00002237 CompilerType void_clang_type = ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid);
2238 CompilerType void_ptr_clang_type = void_clang_type.GetPointerType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002239
Greg Claytona1e5dc82015-08-11 22:53:00 +00002240 CompilerType method_type = ClangASTContext::CreateFunctionType (m_ast_context,
Greg Clayton57ee3062013-07-11 22:46:58 +00002241 void_clang_type,
2242 &void_ptr_clang_type,
Sean Callananc1b732d2011-11-01 18:07:13 +00002243 1,
2244 false,
Sean Callanan7736a202016-04-29 18:09:03 +00002245 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002246
Greg Clayton0fffff52010-09-24 05:15:53 +00002247 const bool is_virtual = false;
2248 const bool is_static = false;
2249 const bool is_inline = false;
Greg Claytonf51de672010-10-01 02:31:07 +00002250 const bool is_explicit = false;
Sean Callananc8324752011-11-02 18:09:01 +00002251 const bool is_attr_used = true;
Sean Callanandbb58392011-11-02 01:38:59 +00002252 const bool is_artificial = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002253
Sean Callanan7736a202016-04-29 18:09:03 +00002254 CXXMethodDecl *method_decl = ClangASTContext::GetASTContext(m_ast_context)->
Greg Claytond8d4a572015-08-11 21:38:15 +00002255 AddMethodToCXXRecordType (copied_clang_type.GetOpaqueQualType(),
2256 "$__lldb_expr",
2257 method_type,
2258 lldb::eAccessPublic,
2259 is_virtual,
2260 is_static,
2261 is_inline,
2262 is_explicit,
2263 is_attr_used,
2264 is_artificial);
Sean Callanan7736a202016-04-29 18:09:03 +00002265
2266 if (log)
2267 {
2268 ASTDumper method_ast_dumper((clang::Decl*)method_decl);
2269 ASTDumper type_ast_dumper(copied_clang_type);
2270
2271 log->Printf(" CEDM::AddThisType Added function $__lldb_expr (description %s) for this type %s",
2272 method_ast_dumper.GetCString(),
2273 type_ast_dumper.GetCString());
2274 }
Sean Callananfc55f5d2010-09-21 00:44:12 +00002275 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002276
Sean Callanan3b436d22015-10-23 21:45:02 +00002277 if (!copied_clang_type.IsValid())
2278 return;
2279
2280 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(QualType::getFromOpaquePtr(copied_clang_type.GetOpaqueQualType()));
2281
2282 if (!type_source_info)
2283 return;
2284
2285 // Construct a typedef type because if "*this" is a templated type we can't just return ClassTemplateSpecializationDecls in response to name queries.
2286 // Using a typedef makes this much more robust.
2287
2288 TypedefDecl *typedef_decl = TypedefDecl::Create(*m_ast_context,
2289 m_ast_context->getTranslationUnitDecl(),
2290 SourceLocation(),
2291 SourceLocation(),
2292 context.m_decl_name.getAsIdentifierInfo(),
2293 type_source_info);
2294
2295
2296 if (!typedef_decl)
2297 return;
2298
2299 context.AddNamedDecl(typedef_decl);
2300
2301 return;
Sean Callananfa4fab72013-02-01 06:55:48 +00002302}
2303
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002304void
2305ClangExpressionDeclMap::AddOneType(NameSearchContext &context,
Sean Callananfa4fab72013-02-01 06:55:48 +00002306 TypeFromUser &ut,
2307 unsigned int current_id)
2308{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002309 CompilerType copied_clang_type = GuardedCopyType(ut);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002310
Greg Clayton57ee3062013-07-11 22:46:58 +00002311 if (!copied_clang_type)
Sean Callananfa4fab72013-02-01 06:55:48 +00002312 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002313 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfa4fab72013-02-01 06:55:48 +00002314
2315 if (log)
2316 log->Printf("ClangExpressionDeclMap::AddOneType - Couldn't import the type");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002317
Sean Callananfa4fab72013-02-01 06:55:48 +00002318 return;
2319 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002320
Greg Clayton57ee3062013-07-11 22:46:58 +00002321 context.AddTypeDecl(copied_clang_type);
Sean Callanan5666b672010-08-04 01:02:13 +00002322}