blob: 821dae2e515bc9084fc7f48fa717c41e024c3f50 [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
10#include "lldb/Expression/ClangExpressionDeclMap.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Sean Callanan3d654b32012-09-24 22:25:51 +000016#include "clang/AST/ASTContext.h"
Sean Callanane2ef6e32010-09-23 03:01:22 +000017#include "clang/AST/DeclarationName.h"
Sean Callanan530782942010-11-01 23:22:47 +000018#include "clang/AST/Decl.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/lldb-private.h"
20#include "lldb/Core/Address.h"
Sean Callananea22d422010-07-16 00:09:46 +000021#include "lldb/Core/Error.h"
Sean Callananf06ba8d2010-06-23 00:47:48 +000022#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Module.h"
Greg Clayton9191db42013-10-21 18:40:51 +000024#include "lldb/Core/ModuleSpec.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000025#include "lldb/Core/RegisterValue.h"
Sean Callanane4ec90e2010-12-16 03:17:46 +000026#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan9b3569b2011-12-10 03:12:34 +000027#include "lldb/Core/ValueObjectVariable.h"
Sean Callanan0917d6e2011-02-01 23:43:26 +000028#include "lldb/Expression/ASTDumper.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Expression/ClangASTSource.h"
Sean Callanan2235f322010-08-11 03:57:18 +000030#include "lldb/Expression/ClangPersistentVariables.h"
Sean Callanan96d27302013-04-11 00:09:05 +000031#include "lldb/Expression/Materializer.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000032#include "lldb/Host/Endian.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Symbol/ClangASTContext.h"
Greg Clayton526e5af2010-11-13 03:52:47 +000034#include "lldb/Symbol/ClangNamespaceDecl.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"
Sean Callanan503aa522011-10-12 00:12:34 +000039#include "lldb/Symbol/SymbolVendor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Symbol/Type.h"
41#include "lldb/Symbol/TypeList.h"
42#include "lldb/Symbol/Variable.h"
43#include "lldb/Symbol/VariableList.h"
Sean Callanan1d180662010-07-20 23:31:16 +000044#include "lldb/Target/ExecutionContext.h"
Sean Callanane0b23b52012-11-15 02:02:04 +000045#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callananea22d422010-07-16 00:09:46 +000046#include "lldb/Target/Process.h"
Sean Callananf4b9bd32010-10-05 20:18:48 +000047#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000048#include "lldb/Target/StackFrame.h"
Sean Callanan1d180662010-07-20 23:31:16 +000049#include "lldb/Target/Target.h"
Jim Ingham895c9822010-12-07 01:56:02 +000050#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051
Greg Clayton83c5cd92010-11-14 22:13:40 +000052using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053using namespace lldb_private;
54using namespace clang;
55
Sean Callanan1ee44b72011-10-29 01:58:46 +000056ClangExpressionDeclMap::ClangExpressionDeclMap (bool keep_result_in_memory, ExecutionContext &exe_ctx) :
57 ClangASTSource (exe_ctx.GetTargetSP()),
Greg Clayton7b462cc2010-10-15 22:48:33 +000058 m_found_entities (),
59 m_struct_members (),
Stephen Wilson71c21d12011-04-11 19:41:40 +000060 m_keep_result_in_memory (keep_result_in_memory),
Sean Callanan979f74d2010-12-03 01:38:59 +000061 m_parser_vars (),
Stephen Wilson71c21d12011-04-11 19:41:40 +000062 m_struct_vars ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063{
Sean Callanan979f74d2010-12-03 01:38:59 +000064 EnableStructVars();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065}
66
67ClangExpressionDeclMap::~ClangExpressionDeclMap()
Sean Callanan979f74d2010-12-03 01:38:59 +000068{
Sean Callanane3aef1d2011-10-12 22:20:02 +000069 // Note: The model is now that the parser's AST context and all associated
70 // data does not vanish until the expression has been executed. This means
71 // that valuable lookup data (like namespaces) doesn't vanish, but
72
73 DidParse();
Sean Callanan979f74d2010-12-03 01:38:59 +000074 DisableStructVars();
75}
Sean Callananbe3a1b12010-10-26 00:31:56 +000076
Sean Callananb9951192011-08-01 18:18:33 +000077bool
Sean Callanan96d27302013-04-11 00:09:05 +000078ClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx,
79 Materializer *materializer)
Sean Callanan8106d802013-03-08 20:04:57 +000080{
81 ClangASTMetrics::ClearLocalCounters();
82
Sean Callanan979f74d2010-12-03 01:38:59 +000083 EnableParserVars();
Sean Callanan933693b2012-02-10 01:22:05 +000084 m_parser_vars->m_exe_ctx = exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +000085
86 Target *target = exe_ctx.GetTargetPtr();
87 if (exe_ctx.GetFramePtr())
88 m_parser_vars->m_sym_ctx = exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything);
Sean Callanand4fac252013-02-21 22:01:43 +000089 else if (exe_ctx.GetThreadPtr() && exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0))
Greg Claytonc14ee322011-09-22 04:58:26 +000090 m_parser_vars->m_sym_ctx = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext(lldb::eSymbolContextEverything);
91 else if (exe_ctx.GetProcessPtr())
Greg Clayton4d122c42011-09-17 08:33:22 +000092 {
Greg Clayton72310352013-02-23 04:12:47 +000093 m_parser_vars->m_sym_ctx.Clear(true);
Greg Claytonc14ee322011-09-22 04:58:26 +000094 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
Greg Clayton4d122c42011-09-17 08:33:22 +000095 }
Greg Claytonc14ee322011-09-22 04:58:26 +000096 else if (target)
Greg Clayton4d122c42011-09-17 08:33:22 +000097 {
Greg Clayton72310352013-02-23 04:12:47 +000098 m_parser_vars->m_sym_ctx.Clear(true);
Greg Claytonc14ee322011-09-22 04:58:26 +000099 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
Greg Clayton4d122c42011-09-17 08:33:22 +0000100 }
Sean Callananc2ff2742011-09-15 18:41:04 +0000101
Greg Claytonc14ee322011-09-22 04:58:26 +0000102 if (target)
103 {
104 m_parser_vars->m_persistent_vars = &target->GetPersistentVariables();
Sean Callananb9951192011-08-01 18:18:33 +0000105
Greg Claytonc14ee322011-09-22 04:58:26 +0000106 if (!target->GetScratchClangASTContext())
107 return false;
108 }
Sean Callananb9951192011-08-01 18:18:33 +0000109
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000110 m_parser_vars->m_target_info = GetTargetInfo();
Sean Callanan96d27302013-04-11 00:09:05 +0000111 m_parser_vars->m_materializer = materializer;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000112
Sean Callananb9951192011-08-01 18:18:33 +0000113 return true;
Sean Callanan979f74d2010-12-03 01:38:59 +0000114}
115
Sean Callanan96d27302013-04-11 00:09:05 +0000116void
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000117ClangExpressionDeclMap::DidParse()
Sean Callanan979f74d2010-12-03 01:38:59 +0000118{
Greg Clayton5160ce52013-03-27 23:08:40 +0000119 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8106d802013-03-08 20:04:57 +0000120
121 if (log)
122 ClangASTMetrics::DumpCounters(log);
123
Sean Callanan979f74d2010-12-03 01:38:59 +0000124 if (m_parser_vars.get())
125 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000126 for (size_t entity_index = 0, num_entities = m_found_entities.GetSize();
Sean Callanan979f74d2010-12-03 01:38:59 +0000127 entity_index < num_entities;
128 ++entity_index)
129 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000130 ClangExpressionVariableSP var_sp(m_found_entities.GetVariableAtIndex(entity_index));
Jim Ingham28eb5712012-10-12 17:34:26 +0000131 if (var_sp)
Sean Callanan3c495c12013-01-15 23:29:36 +0000132 var_sp->DisableParserVars(GetParserID());
Sean Callanan979f74d2010-12-03 01:38:59 +0000133 }
134
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000135 for (size_t pvar_index = 0, num_pvars = m_parser_vars->m_persistent_vars->GetSize();
Sean Callanan979f74d2010-12-03 01:38:59 +0000136 pvar_index < num_pvars;
137 ++pvar_index)
138 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000139 ClangExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index));
140 if (pvar_sp)
Sean Callanan3c495c12013-01-15 23:29:36 +0000141 pvar_sp->DisableParserVars(GetParserID());
Sean Callanan979f74d2010-12-03 01:38:59 +0000142 }
143
144 DisableParserVars();
Sean Callanan6b1b9532010-10-08 01:58:41 +0000145 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146}
147
Sean Callanan549c9f72010-07-13 21:41:46 +0000148// Interface for IRForTarget
149
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000150ClangExpressionDeclMap::TargetInfo
151ClangExpressionDeclMap::GetTargetInfo()
152{
153 assert (m_parser_vars.get());
154
155 TargetInfo ret;
156
Sean Callanan933693b2012-02-10 01:22:05 +0000157 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
158
159 Process *process = exe_ctx.GetProcessPtr();
160 if (process)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000161 {
Sean Callanan933693b2012-02-10 01:22:05 +0000162 ret.byte_order = process->GetByteOrder();
163 ret.address_byte_size = process->GetAddressByteSize();
164 }
165 else
166 {
167 Target *target = exe_ctx.GetTargetPtr();
168 if (target)
Greg Claytonc14ee322011-09-22 04:58:26 +0000169 {
Sean Callanan933693b2012-02-10 01:22:05 +0000170 ret.byte_order = target->GetArchitecture().GetByteOrder();
171 ret.address_byte_size = target->GetArchitecture().GetAddressByteSize();
Greg Claytonc14ee322011-09-22 04:58:26 +0000172 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000173 }
Sean Callanan933693b2012-02-10 01:22:05 +0000174
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000175 return ret;
176}
177
Sean Callanan549c9f72010-07-13 21:41:46 +0000178bool
Greg Clayton7b462cc2010-10-15 22:48:33 +0000179ClangExpressionDeclMap::AddPersistentVariable
180(
Sean Callanancc427fa2011-07-30 02:42:06 +0000181 const NamedDecl *decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +0000182 const ConstString &name,
Sean Callanan92adcac2011-01-13 08:53:35 +0000183 TypeFromParser parser_type,
184 bool is_result,
185 bool is_lvalue
Greg Clayton7b462cc2010-10-15 22:48:33 +0000186)
Sean Callanan2235f322010-08-11 03:57:18 +0000187{
Sean Callanan979f74d2010-12-03 01:38:59 +0000188 assert (m_parser_vars.get());
189
Sean Callanan1582ee62013-04-18 22:06:33 +0000190 if (m_parser_vars->m_materializer && is_result)
191 {
192 Error err;
193
194 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
195 Target *target = exe_ctx.GetTargetPtr();
196 if (target == NULL)
197 return false;
198
199 ASTContext *context(target->GetScratchClangASTContext()->getASTContext());
200
201 TypeFromUser user_type(m_ast_importer->DeportType(context,
202 parser_type.GetASTContext(),
203 parser_type.GetOpaqueQualType()),
204 context);
205
206 uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(user_type, is_lvalue, m_keep_result_in_memory, err);
207
Sean Callananf35bbbc2013-07-15 18:43:36 +0000208 ClangExpressionVariableSP var_sp = m_found_entities.CreateVariable(exe_ctx.GetBestExecutionContextScope(),
209 name,
210 user_type,
211 m_parser_vars->m_target_info.byte_order,
212 m_parser_vars->m_target_info.address_byte_size);
Sean Callanan1582ee62013-04-18 22:06:33 +0000213
214 if (!var_sp)
215 return false;
216
217 var_sp->EnableParserVars(GetParserID());
218
219 ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
220
221 parser_vars->m_named_decl = decl;
222 parser_vars->m_parser_type = parser_type;
223
224 var_sp->EnableJITVars(GetParserID());
225
226 ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID());
227
228 jit_vars->m_offset = offset;
229
230 return true;
231 }
232
Greg Clayton5160ce52013-03-27 23:08:40 +0000233 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan933693b2012-02-10 01:22:05 +0000234 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
235 Target *target = exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +0000236 if (target == NULL)
237 return false;
238
239 ASTContext *context(target->GetScratchClangASTContext()->getASTContext());
Sean Callanan2235f322010-08-11 03:57:18 +0000240
Sean Callananbb120042011-12-16 21:06:35 +0000241 TypeFromUser user_type(m_ast_importer->DeportType(context,
242 parser_type.GetASTContext(),
243 parser_type.GetOpaqueQualType()),
Sean Callanane1175b72011-01-13 21:23:32 +0000244 context);
Sean Callanan00f43622011-11-18 03:28:09 +0000245
246 if (!user_type.GetOpaqueQualType())
247 {
248 if (log)
249 log->Printf("Persistent variable's type wasn't copied successfully");
250 return false;
251 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000252
253 if (!m_parser_vars->m_target_info.IsValid())
254 return false;
Sean Callanan2235f322010-08-11 03:57:18 +0000255
Sean Callananf35bbbc2013-07-15 18:43:36 +0000256 ClangExpressionVariableSP var_sp = m_parser_vars->m_persistent_vars->CreatePersistentVariable (exe_ctx.GetBestExecutionContextScope (),
257 name,
258 user_type,
259 m_parser_vars->m_target_info.byte_order,
260 m_parser_vars->m_target_info.address_byte_size);
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000261
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000262 if (!var_sp)
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000263 return false;
264
Sean Callanan2cb5e522012-09-20 23:21:16 +0000265 var_sp->m_frozen_sp->SetHasCompleteType();
266
Sean Callanan92adcac2011-01-13 08:53:35 +0000267 if (is_result)
268 var_sp->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry;
269 else
270 var_sp->m_flags |= ClangExpressionVariable::EVKeepInTarget; // explicitly-declared persistent variables should persist
271
272 if (is_lvalue)
273 {
274 var_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
275 }
276 else
277 {
278 var_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
279 var_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
280 }
281
Sean Callanan35005f72013-04-12 18:10:34 +0000282 if (m_keep_result_in_memory)
283 {
284 var_sp->m_flags |= ClangExpressionVariable::EVKeepInTarget;
285 }
286
Sean Callanan92adcac2011-01-13 08:53:35 +0000287 if (log)
288 log->Printf("Created persistent variable with flags 0x%hx", var_sp->m_flags);
289
Sean Callanan3c495c12013-01-15 23:29:36 +0000290 var_sp->EnableParserVars(GetParserID());
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000291
Sean Callanan3c495c12013-01-15 23:29:36 +0000292 ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
293
294 parser_vars->m_named_decl = decl;
295 parser_vars->m_parser_type = parser_type;
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000296
297 return true;
Sean Callanan2235f322010-08-11 03:57:18 +0000298}
299
300bool
Greg Clayton7b462cc2010-10-15 22:48:33 +0000301ClangExpressionDeclMap::AddValueToStruct
302(
Sean Callanancc427fa2011-07-30 02:42:06 +0000303 const NamedDecl *decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +0000304 const ConstString &name,
305 llvm::Value *value,
306 size_t size,
307 off_t alignment
308)
Sean Callanan549c9f72010-07-13 21:41:46 +0000309{
Sean Callanan979f74d2010-12-03 01:38:59 +0000310 assert (m_struct_vars.get());
311 assert (m_parser_vars.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000312
Sean Callanandf667652013-04-11 02:05:11 +0000313 bool is_persistent_variable = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000314
Greg Clayton5160ce52013-03-27 23:08:40 +0000315 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000316
Sean Callanan979f74d2010-12-03 01:38:59 +0000317 m_struct_vars->m_struct_laid_out = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000318
Sean Callanan3c495c12013-01-15 23:29:36 +0000319 if (m_struct_members.GetVariable(decl, GetParserID()))
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000320 return true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000321
Sean Callanan3c495c12013-01-15 23:29:36 +0000322 ClangExpressionVariableSP var_sp (m_found_entities.GetVariable(decl, GetParserID()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000323
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000324 if (!var_sp)
Sean Callanandf667652013-04-11 02:05:11 +0000325 {
Sean Callanan3c495c12013-01-15 23:29:36 +0000326 var_sp = m_parser_vars->m_persistent_vars->GetVariable(decl, GetParserID());
Sean Callanandf667652013-04-11 02:05:11 +0000327 is_persistent_variable = true;
328 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000329
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000330 if (!var_sp)
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000331 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000332
Sean Callanan823bb4c2010-08-30 22:17:16 +0000333 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000334 log->Printf("Adding value for (NamedDecl*)%p [%s - %s] to the structure",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000335 static_cast<const void*>(decl), name.GetCString(),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000336 var_sp->GetName().GetCString());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000337
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000338 // We know entity->m_parser_vars is valid because we used a parser variable
339 // to find it
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000340
Sean Callanan3c495c12013-01-15 23:29:36 +0000341 ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
342
343 parser_vars->m_llvm_value = value;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000344
Sean Callanan1582ee62013-04-18 22:06:33 +0000345 if (ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID()))
346 {
347 // We already laid this out; do not touch
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000348
Sean Callanan1582ee62013-04-18 22:06:33 +0000349 if (log)
350 log->Printf("Already placed at 0x%llx", (unsigned long long)jit_vars->m_offset);
351 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000352
Sean Callanan3c495c12013-01-15 23:29:36 +0000353 var_sp->EnableJITVars(GetParserID());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000354
Sean Callanan3c495c12013-01-15 23:29:36 +0000355 ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID());
356
357 jit_vars->m_alignment = alignment;
358 jit_vars->m_size = size;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000359
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000360 m_struct_members.AddVariable(var_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000361
Sean Callanandf667652013-04-11 02:05:11 +0000362 if (m_parser_vars->m_materializer)
363 {
Sean Callanan3dd6a422013-04-11 21:16:36 +0000364 uint32_t offset = 0;
365
Sean Callanandf667652013-04-11 02:05:11 +0000366 Error err;
367
368 if (is_persistent_variable)
369 {
Sean Callanan3dd6a422013-04-11 21:16:36 +0000370 offset = m_parser_vars->m_materializer->AddPersistentVariable(var_sp, err);
Sean Callanandf667652013-04-11 02:05:11 +0000371 }
372 else
373 {
374 if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym)
Sean Callanan3dd6a422013-04-11 21:16:36 +0000375 offset = m_parser_vars->m_materializer->AddSymbol(*sym, err);
Sean Callanandf667652013-04-11 02:05:11 +0000376 else if (const RegisterInfo *reg_info = var_sp->GetRegisterInfo())
Sean Callanan3dd6a422013-04-11 21:16:36 +0000377 offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err);
Sean Callanandf667652013-04-11 02:05:11 +0000378 else if (parser_vars->m_lldb_var)
Sean Callanan3dd6a422013-04-11 21:16:36 +0000379 offset = m_parser_vars->m_materializer->AddVariable(parser_vars->m_lldb_var, err);
Sean Callanandf667652013-04-11 02:05:11 +0000380 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000381
Sean Callanan3dd6a422013-04-11 21:16:36 +0000382 if (!err.Success())
383 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000384
Sean Callanan3dd6a422013-04-11 21:16:36 +0000385 if (log)
386 log->Printf("Placed at 0x%llx", (unsigned long long)offset);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000387
Sean Callanan3dd6a422013-04-11 21:16:36 +0000388 jit_vars->m_offset = offset; // TODO DoStructLayout() should not change this.
Sean Callanandf667652013-04-11 02:05:11 +0000389 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000390
Sean Callanan549c9f72010-07-13 21:41:46 +0000391 return true;
392}
393
394bool
395ClangExpressionDeclMap::DoStructLayout ()
396{
Sean Callanan979f74d2010-12-03 01:38:59 +0000397 assert (m_struct_vars.get());
398
399 if (m_struct_vars->m_struct_laid_out)
Sean Callanan549c9f72010-07-13 21:41:46 +0000400 return true;
401
Sean Callanan14b1bae2013-04-16 23:25:35 +0000402 if (!m_parser_vars->m_materializer)
403 return false;
Sean Callanan3dd6a422013-04-11 21:16:36 +0000404
Sean Callanan14b1bae2013-04-16 23:25:35 +0000405 m_struct_vars->m_struct_alignment = m_parser_vars->m_materializer->GetStructAlignment();
406 m_struct_vars->m_struct_size = m_parser_vars->m_materializer->GetStructByteSize();
Sean Callanan979f74d2010-12-03 01:38:59 +0000407 m_struct_vars->m_struct_laid_out = true;
Sean Callanan549c9f72010-07-13 21:41:46 +0000408 return true;
409}
410
Greg Clayton7b462cc2010-10-15 22:48:33 +0000411bool ClangExpressionDeclMap::GetStructInfo
412(
413 uint32_t &num_elements,
414 size_t &size,
415 off_t &alignment
416)
Sean Callanan549c9f72010-07-13 21:41:46 +0000417{
Sean Callanan979f74d2010-12-03 01:38:59 +0000418 assert (m_struct_vars.get());
419
420 if (!m_struct_vars->m_struct_laid_out)
Sean Callanan549c9f72010-07-13 21:41:46 +0000421 return false;
422
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000423 num_elements = m_struct_members.GetSize();
Sean Callanan979f74d2010-12-03 01:38:59 +0000424 size = m_struct_vars->m_struct_size;
425 alignment = m_struct_vars->m_struct_alignment;
Sean Callanan549c9f72010-07-13 21:41:46 +0000426
427 return true;
428}
429
430bool
Greg Clayton7b462cc2010-10-15 22:48:33 +0000431ClangExpressionDeclMap::GetStructElement
432(
Sean Callanancc427fa2011-07-30 02:42:06 +0000433 const NamedDecl *&decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +0000434 llvm::Value *&value,
435 off_t &offset,
436 ConstString &name,
437 uint32_t index
438)
Sean Callanan549c9f72010-07-13 21:41:46 +0000439{
Sean Callanan979f74d2010-12-03 01:38:59 +0000440 assert (m_struct_vars.get());
441
442 if (!m_struct_vars->m_struct_laid_out)
Sean Callanan549c9f72010-07-13 21:41:46 +0000443 return false;
444
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000445 if (index >= m_struct_members.GetSize())
Sean Callanan549c9f72010-07-13 21:41:46 +0000446 return false;
447
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000448 ClangExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index));
Sean Callanan549c9f72010-07-13 21:41:46 +0000449
Sean Callanan3c495c12013-01-15 23:29:36 +0000450 if (!member_sp)
451 return false;
452
453 ClangExpressionVariable::ParserVars *parser_vars = member_sp->GetParserVars(GetParserID());
454 ClangExpressionVariable::JITVars *jit_vars = member_sp->GetJITVars(GetParserID());
455
456 if (!parser_vars ||
457 !jit_vars ||
Sean Callanan6f3e5ad2012-04-12 16:58:26 +0000458 !member_sp->GetValueObject())
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000459 return false;
460
Sean Callanan3c495c12013-01-15 23:29:36 +0000461 decl = parser_vars->m_named_decl;
462 value = parser_vars->m_llvm_value;
463 offset = jit_vars->m_offset;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000464 name = member_sp->GetName();
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000465
Sean Callanan549c9f72010-07-13 21:41:46 +0000466 return true;
467}
468
Sean Callanan7ea35012010-07-27 21:39:39 +0000469bool
Greg Clayton7b462cc2010-10-15 22:48:33 +0000470ClangExpressionDeclMap::GetFunctionInfo
471(
Sean Callanancc427fa2011-07-30 02:42:06 +0000472 const NamedDecl *decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +0000473 uint64_t &ptr
474)
Sean Callanan4edba2d2010-07-27 02:07:53 +0000475{
Sean Callanan3c495c12013-01-15 23:29:36 +0000476 ClangExpressionVariableSP entity_sp(m_found_entities.GetVariable(decl, GetParserID()));
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000477
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000478 if (!entity_sp)
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000479 return false;
Sean Callanan4edba2d2010-07-27 02:07:53 +0000480
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000481 // We know m_parser_vars is valid since we searched for the variable by
482 // its NamedDecl
Sean Callanan4edba2d2010-07-27 02:07:53 +0000483
Sean Callanan3c495c12013-01-15 23:29:36 +0000484 ClangExpressionVariable::ParserVars *parser_vars = entity_sp->GetParserVars(GetParserID());
485
Greg Clayton57ee3062013-07-11 22:46:58 +0000486 ptr = parser_vars->m_lldb_value.GetScalar().ULongLong();
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000487
488 return true;
Sean Callanan4edba2d2010-07-27 02:07:53 +0000489}
490
Sean Callananefe9a422011-08-16 18:09:29 +0000491static void
492FindCodeSymbolInContext
493(
494 const ConstString &name,
495 SymbolContext &sym_ctx,
496 SymbolContextList &sc_list
497)
498{
Matt Kopec00049b82013-02-27 20:13:38 +0000499 SymbolContextList temp_sc_list;
Sean Callananefe9a422011-08-16 18:09:29 +0000500 if (sym_ctx.module_sp)
Matt Kopec00049b82013-02-27 20:13:38 +0000501 sym_ctx.module_sp->FindSymbolsWithNameAndType(name, eSymbolTypeAny, temp_sc_list);
Sean Callananefe9a422011-08-16 18:09:29 +0000502
Matt Kopec00049b82013-02-27 20:13:38 +0000503 if (!sc_list.GetSize() && sym_ctx.target_sp)
504 sym_ctx.target_sp->GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, temp_sc_list);
505
506 unsigned temp_sc_list_size = temp_sc_list.GetSize();
507 for (unsigned i = 0; i < temp_sc_list_size; i++)
508 {
509 SymbolContext sym_ctx;
510 temp_sc_list.GetContextAtIndex(i, sym_ctx);
511 if (sym_ctx.symbol)
512 {
513 switch (sym_ctx.symbol->GetType())
514 {
515 case eSymbolTypeCode:
516 case eSymbolTypeResolver:
Greg Claytonf32db512013-10-30 21:37:46 +0000517 case eSymbolTypeReExported:
Matt Kopec00049b82013-02-27 20:13:38 +0000518 sc_list.Append(sym_ctx);
519 break;
520
521 default:
522 break;
523 }
524 }
525 }
Sean Callananefe9a422011-08-16 18:09:29 +0000526}
527
Sean Callanan5300d372010-07-31 01:32:05 +0000528bool
Greg Clayton7b462cc2010-10-15 22:48:33 +0000529ClangExpressionDeclMap::GetFunctionAddress
530(
531 const ConstString &name,
Greg Clayton3f5c08f2011-05-18 22:01:49 +0000532 uint64_t &func_addr
Greg Clayton7b462cc2010-10-15 22:48:33 +0000533)
Sean Callanan5300d372010-07-31 01:32:05 +0000534{
Sean Callanan979f74d2010-12-03 01:38:59 +0000535 assert (m_parser_vars.get());
536
Greg Clayton5160ce52013-03-27 23:08:40 +0000537 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan933693b2012-02-10 01:22:05 +0000538 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
539 Target *target = exe_ctx.GetTargetPtr();
Sean Callanan5300d372010-07-31 01:32:05 +0000540 // Back out in all cases where we're not fully initialized
Greg Claytonc14ee322011-09-22 04:58:26 +0000541 if (target == NULL)
Jim Ingham895c9822010-12-07 01:56:02 +0000542 return false;
543 if (!m_parser_vars->m_sym_ctx.target_sp)
Sean Callanan5300d372010-07-31 01:32:05 +0000544 return false;
545
Greg Clayton7a345282010-11-09 23:46:37 +0000546 SymbolContextList sc_list;
Sean Callanan5300d372010-07-31 01:32:05 +0000547
Sean Callananefe9a422011-08-16 18:09:29 +0000548 FindCodeSymbolInContext(name, m_parser_vars->m_sym_ctx, sc_list);
Matt Kopec00049b82013-02-27 20:13:38 +0000549
Greg Claytonf32db512013-10-30 21:37:46 +0000550 uint32_t sc_list_size = sc_list.GetSize();
551 if (sc_list_size == 0)
Sean Callanan19b6afe2011-05-13 18:27:02 +0000552 {
553 // We occasionally get debug information in which a const function is reported
554 // as non-const, so the mangled name is wrong. This is a hack to compensate.
555
Sean Callananf4638562011-10-25 18:02:05 +0000556 if (!strncmp(name.GetCString(), "_ZN", 3) &&
557 strncmp(name.GetCString(), "_ZNK", 4))
Sean Callanan19b6afe2011-05-13 18:27:02 +0000558 {
Sean Callananf4638562011-10-25 18:02:05 +0000559 std::string fixed_scratch("_ZNK");
560 fixed_scratch.append(name.GetCString() + 3);
561 ConstString fixed_name(fixed_scratch.c_str());
Sean Callanan19b6afe2011-05-13 18:27:02 +0000562
563 if (log)
Sean Callananf4638562011-10-25 18:02:05 +0000564 log->Printf("Failed to find symbols given non-const name %s; trying %s", name.GetCString(), fixed_name.GetCString());
565
566 FindCodeSymbolInContext(fixed_name, m_parser_vars->m_sym_ctx, sc_list);
Greg Claytonf32db512013-10-30 21:37:46 +0000567 sc_list_size = sc_list.GetSize();
Sean Callanan19b6afe2011-05-13 18:27:02 +0000568 }
569 }
Matt Kopec00049b82013-02-27 20:13:38 +0000570
Greg Claytonf32db512013-10-30 21:37:46 +0000571 for (uint32_t i=0; i<sc_list_size; ++i)
572 {
573 SymbolContext sym_ctx;
574 sc_list.GetContextAtIndex(i, sym_ctx);
Matt Kopec00049b82013-02-27 20:13:38 +0000575
Greg Claytonf32db512013-10-30 21:37:46 +0000576 const Address *func_so_addr = NULL;
577 bool is_indirect_function = false;
578 if (sym_ctx.function)
579 func_so_addr = &sym_ctx.function->GetAddressRange().GetBaseAddress();
580 else if (sym_ctx.symbol)
581 {
582 if (sym_ctx.symbol->GetType() == eSymbolTypeReExported)
583 {
584 Symbol *reexported_symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target);
585 if (reexported_symbol)
586 {
587 func_so_addr = &reexported_symbol->GetAddress();
588 is_indirect_function = reexported_symbol->IsIndirect();
589 }
590 }
591 else
592 {
593 func_so_addr = &sym_ctx.symbol->GetAddress();
594 is_indirect_function = sym_ctx.symbol->IsIndirect();
595 }
596 }
Matt Kopec00049b82013-02-27 20:13:38 +0000597
Greg Claytonf32db512013-10-30 21:37:46 +0000598 if (func_so_addr && func_so_addr->IsValid())
599 {
600 lldb::addr_t load_addr = func_so_addr->GetCallableLoadAddress (target, is_indirect_function);
601
602 if (load_addr != LLDB_INVALID_ADDRESS)
603 {
604 func_addr = load_addr;
605 return true;
606 }
607 }
608 }
609 return false;
Sean Callanan5300d372010-07-31 01:32:05 +0000610}
611
Greg Clayton084db102011-06-23 04:25:29 +0000612addr_t
Greg Clayton9191db42013-10-21 18:40:51 +0000613ClangExpressionDeclMap::GetSymbolAddress (Target &target,
614 Process *process,
615 const ConstString &name,
616 lldb::SymbolType symbol_type,
Deepak Panickald66b50c2013-10-22 12:27:43 +0000617 lldb_private::Module *module)
Sean Callananc3a16002011-01-17 23:42:46 +0000618{
Sean Callananc3a16002011-01-17 23:42:46 +0000619 SymbolContextList sc_list;
620
Greg Clayton9191db42013-10-21 18:40:51 +0000621 if (module)
622 module->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
623 else
624 target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list);
Sean Callananc3a16002011-01-17 23:42:46 +0000625
Greg Clayton084db102011-06-23 04:25:29 +0000626 const uint32_t num_matches = sc_list.GetSize();
627 addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
628
Sean Callanan947ccc72011-12-01 02:04:16 +0000629 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 +0000630 {
631 SymbolContext sym_ctx;
632 sc_list.GetContextAtIndex(i, sym_ctx);
Sean Callananc3a16002011-01-17 23:42:46 +0000633
Greg Claytone7612132012-03-07 21:03:09 +0000634 const Address *sym_address = &sym_ctx.symbol->GetAddress();
Sean Callananc6466fc2011-07-07 23:05:43 +0000635
636 if (!sym_address || !sym_address->IsValid())
Sean Callanan116ace22013-04-30 00:21:42 +0000637 continue;
Sean Callananc6466fc2011-07-07 23:05:43 +0000638
Greg Clayton084db102011-06-23 04:25:29 +0000639 if (sym_address)
640 {
641 switch (sym_ctx.symbol->GetType())
642 {
643 case eSymbolTypeCode:
644 case eSymbolTypeTrampoline:
645 symbol_load_addr = sym_address->GetCallableLoadAddress (&target);
646 break;
Matt Kopec00049b82013-02-27 20:13:38 +0000647
648 case eSymbolTypeResolver:
649 symbol_load_addr = sym_address->GetCallableLoadAddress (&target, true);
650 break;
651
Greg Clayton9191db42013-10-21 18:40:51 +0000652 case eSymbolTypeReExported:
653 {
654 ConstString reexport_name = sym_ctx.symbol->GetReExportedSymbolName();
655 if (reexport_name)
656 {
657 ModuleSP reexport_module_sp;
658 ModuleSpec reexport_module_spec;
659 reexport_module_spec.GetPlatformFileSpec() = sym_ctx.symbol->GetReExportedSymbolSharedLibrary();
660 if (reexport_module_spec.GetPlatformFileSpec())
661 {
662 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
663 if (!reexport_module_sp)
664 {
665 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
666 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
667 }
668 }
669 symbol_load_addr = GetSymbolAddress(target, process, sym_ctx.symbol->GetReExportedSymbolName(), symbol_type, reexport_module_sp.get());
670 }
671 }
672 break;
673
Greg Clayton084db102011-06-23 04:25:29 +0000674 case eSymbolTypeData:
675 case eSymbolTypeRuntime:
676 case eSymbolTypeVariable:
677 case eSymbolTypeLocal:
678 case eSymbolTypeParam:
679 case eSymbolTypeInvalid:
680 case eSymbolTypeAbsolute:
Greg Clayton084db102011-06-23 04:25:29 +0000681 case eSymbolTypeException:
682 case eSymbolTypeSourceFile:
683 case eSymbolTypeHeaderFile:
684 case eSymbolTypeObjectFile:
685 case eSymbolTypeCommonBlock:
686 case eSymbolTypeBlock:
687 case eSymbolTypeVariableType:
688 case eSymbolTypeLineEntry:
689 case eSymbolTypeLineHeader:
690 case eSymbolTypeScopeBegin:
691 case eSymbolTypeScopeEnd:
692 case eSymbolTypeAdditional:
693 case eSymbolTypeCompiler:
694 case eSymbolTypeInstrumentation:
695 case eSymbolTypeUndefined:
Greg Clayton456809c2011-12-03 02:30:59 +0000696 case eSymbolTypeObjCClass:
697 case eSymbolTypeObjCMetaClass:
698 case eSymbolTypeObjCIVar:
Greg Clayton084db102011-06-23 04:25:29 +0000699 symbol_load_addr = sym_address->GetLoadAddress (&target);
700 break;
701 }
702 }
703 }
Sean Callananc3a16002011-01-17 23:42:46 +0000704
Sean Callanane0b23b52012-11-15 02:02:04 +0000705 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process)
706 {
707 ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime();
708
709 if (runtime)
710 {
711 symbol_load_addr = runtime->LookupRuntimeSymbol(name);
712 }
713 }
714
Greg Clayton084db102011-06-23 04:25:29 +0000715 return symbol_load_addr;
Sean Callananc3a16002011-01-17 23:42:46 +0000716}
717
Greg Clayton084db102011-06-23 04:25:29 +0000718addr_t
Sean Callanan947ccc72011-12-01 02:04:16 +0000719ClangExpressionDeclMap::GetSymbolAddress (const ConstString &name, lldb::SymbolType symbol_type)
Sean Callanand9ca42a2011-05-08 02:21:26 +0000720{
721 assert (m_parser_vars.get());
722
Sean Callanan933693b2012-02-10 01:22:05 +0000723 if (!m_parser_vars->m_exe_ctx.GetTargetPtr())
Sean Callanand9ca42a2011-05-08 02:21:26 +0000724 return false;
725
Sean Callanane0b23b52012-11-15 02:02:04 +0000726 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 +0000727}
728
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000729const Symbol *
730ClangExpressionDeclMap::FindGlobalDataSymbol (Target &target,
Greg Clayton9191db42013-10-21 18:40:51 +0000731 const ConstString &name,
Deepak Panickald66b50c2013-10-22 12:27:43 +0000732 lldb_private::Module *module)
Sean Callanand9ca42a2011-05-08 02:21:26 +0000733{
734 SymbolContextList sc_list;
735
Greg Clayton9191db42013-10-21 18:40:51 +0000736 if (module)
737 module->FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
738 else
739 target.GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
Sean Callanand9ca42a2011-05-08 02:21:26 +0000740
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000741 const uint32_t matches = sc_list.GetSize();
742 for (uint32_t i=0; i<matches; ++i)
Sean Callanand9ca42a2011-05-08 02:21:26 +0000743 {
744 SymbolContext sym_ctx;
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000745 sc_list.GetContextAtIndex(i, sym_ctx);
746 if (sym_ctx.symbol)
747 {
748 const Symbol *symbol = sym_ctx.symbol;
749 const Address *sym_address = &symbol->GetAddress();
750
751 if (sym_address && sym_address->IsValid())
752 {
753 switch (symbol->GetType())
754 {
755 case eSymbolTypeData:
756 case eSymbolTypeRuntime:
757 case eSymbolTypeAbsolute:
758 case eSymbolTypeObjCClass:
759 case eSymbolTypeObjCMetaClass:
760 case eSymbolTypeObjCIVar:
761 if (symbol->GetDemangledNameIsSynthesized())
762 {
763 // If the demangled name was synthesized, then don't use it
764 // for expressions. Only let the symbol match if the mangled
765 // named matches for these symbols.
766 if (symbol->GetMangled().GetMangledName() != name)
767 break;
768 }
769 return symbol;
770
Greg Clayton9191db42013-10-21 18:40:51 +0000771 case eSymbolTypeReExported:
772 {
773 ConstString reexport_name = symbol->GetReExportedSymbolName();
774 if (reexport_name)
775 {
776 ModuleSP reexport_module_sp;
777 ModuleSpec reexport_module_spec;
778 reexport_module_spec.GetPlatformFileSpec() = symbol->GetReExportedSymbolSharedLibrary();
779 if (reexport_module_spec.GetPlatformFileSpec())
780 {
781 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
782 if (!reexport_module_sp)
783 {
784 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
785 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
786 }
787 }
788 return FindGlobalDataSymbol(target, symbol->GetReExportedSymbolName(), reexport_module_sp.get());
789 }
790 }
791 break;
792
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000793 case eSymbolTypeCode: // We already lookup functions elsewhere
794 case eSymbolTypeVariable:
795 case eSymbolTypeLocal:
796 case eSymbolTypeParam:
797 case eSymbolTypeTrampoline:
798 case eSymbolTypeInvalid:
799 case eSymbolTypeException:
800 case eSymbolTypeSourceFile:
801 case eSymbolTypeHeaderFile:
802 case eSymbolTypeObjectFile:
803 case eSymbolTypeCommonBlock:
804 case eSymbolTypeBlock:
805 case eSymbolTypeVariableType:
806 case eSymbolTypeLineEntry:
807 case eSymbolTypeLineHeader:
808 case eSymbolTypeScopeBegin:
809 case eSymbolTypeScopeEnd:
810 case eSymbolTypeAdditional:
811 case eSymbolTypeCompiler:
812 case eSymbolTypeInstrumentation:
813 case eSymbolTypeUndefined:
Matt Kopec00049b82013-02-27 20:13:38 +0000814 case eSymbolTypeResolver:
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000815 break;
816 }
817 }
818 }
Sean Callanand9ca42a2011-05-08 02:21:26 +0000819 }
820
821 return NULL;
822}
823
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000824lldb::VariableSP
825ClangExpressionDeclMap::FindGlobalVariable
826(
827 Target &target,
Sean Callanan1fd3f4f2011-10-12 16:59:31 +0000828 ModuleSP &module,
829 const ConstString &name,
830 ClangNamespaceDecl *namespace_decl,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000831 TypeFromUser *type
832)
833{
834 VariableList vars;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000835
Sean Callanan1fd3f4f2011-10-12 16:59:31 +0000836 if (module && namespace_decl)
837 module->FindGlobalVariables (name, namespace_decl, true, -1, vars);
838 else
839 target.GetImages().FindGlobalVariables(name, true, -1, vars);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000840
841 if (vars.GetSize())
842 {
843 if (type)
844 {
845 for (size_t i = 0; i < vars.GetSize(); ++i)
846 {
847 VariableSP var_sp = vars.GetVariableAtIndex(i);
848
Greg Clayton57ee3062013-07-11 22:46:58 +0000849 if (ClangASTContext::AreTypesSame(*type, var_sp->GetType()->GetClangFullType()))
850 return var_sp;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000851 }
852 }
853 else
854 {
855 return vars.GetVariableAtIndex(0);
856 }
857 }
858
859 return VariableSP();
860}
861
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000862// Interface for ClangASTSource
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000863
Sean Callanan503aa522011-10-12 00:12:34 +0000864void
Sean Callananeddeb3b2011-10-28 23:38:38 +0000865ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000866{
Sean Callananeddeb3b2011-10-28 23:38:38 +0000867 assert (m_ast_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000868
Sean Callanan8106d802013-03-08 20:04:57 +0000869 ClangASTMetrics::RegisterVisibleQuery();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000870
Sean Callananeddeb3b2011-10-28 23:38:38 +0000871 const ConstString name(context.m_decl_name.getAsString().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000872
Greg Clayton5160ce52013-03-27 23:08:40 +0000873 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000874
Sean Callananfb3e4302011-10-29 19:50:43 +0000875 if (GetImportInProgress())
Sean Callanan6abfabf2010-11-19 20:20:02 +0000876 {
Sean Callanan7dd98122011-10-14 20:34:21 +0000877 if (log && log->GetVerbose())
Sean Callanan6abfabf2010-11-19 20:20:02 +0000878 log->Printf("Ignoring a query during an import");
879 return;
880 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000881
Sean Callanan7dd98122011-10-14 20:34:21 +0000882 static unsigned int invocation_id = 0;
883 unsigned int current_id = invocation_id++;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000884
Sean Callanan503aa522011-10-12 00:12:34 +0000885 if (log)
886 {
887 if (!context.m_decl_context)
Sean Callananfb3e4302011-10-29 19:50:43 +0000888 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a NULL DeclContext", current_id, name.GetCString());
Sean Callanan503aa522011-10-12 00:12:34 +0000889 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callananfb3e4302011-10-29 19:50:43 +0000890 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 +0000891 else
Sean Callananfb3e4302011-10-29 19:50:43 +0000892 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 +0000893 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000894
Sean Callanan503aa522011-10-12 00:12:34 +0000895 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
896 {
Sean Callanan1ee44b72011-10-29 01:58:46 +0000897 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000898
Sean Callanan7dd98122011-10-14 20:34:21 +0000899 if (log && log->GetVerbose())
Sean Callanan00f43622011-11-18 03:28:09 +0000900 log->Printf(" CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000901 current_id, static_cast<void*>(namespace_map.get()),
Sean Callanan7dd98122011-10-14 20:34:21 +0000902 (int)namespace_map->size());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000903
Sean Callananb2269162011-10-21 22:18:07 +0000904 if (!namespace_map)
905 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000906
Sean Callanan503aa522011-10-12 00:12:34 +0000907 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
908 i != e;
909 ++i)
910 {
911 if (log)
Sean Callananfb3e4302011-10-29 19:50:43 +0000912 log->Printf(" CEDM::FEVD[%u] Searching namespace %s in module %s",
Sean Callanan7dd98122011-10-14 20:34:21 +0000913 current_id,
Sean Callanan503aa522011-10-12 00:12:34 +0000914 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
915 i->first->GetFileSpec().GetFilename().GetCString());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000916
Sean Callananebe60672011-10-13 21:50:33 +0000917 FindExternalVisibleDecls(context,
918 i->first,
919 i->second,
Sean Callanan7dd98122011-10-14 20:34:21 +0000920 current_id);
Sean Callanan503aa522011-10-12 00:12:34 +0000921 }
922 }
Sean Callanand5c17ed2011-11-15 02:11:17 +0000923 else if (isa<TranslationUnitDecl>(context.m_decl_context))
Sean Callanan503aa522011-10-12 00:12:34 +0000924 {
925 ClangNamespaceDecl namespace_decl;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000926
Sean Callanan503aa522011-10-12 00:12:34 +0000927 if (log)
Sean Callananfb3e4302011-10-29 19:50:43 +0000928 log->Printf(" CEDM::FEVD[%u] Searching the root namespace", current_id);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000929
Sean Callanan503aa522011-10-12 00:12:34 +0000930 FindExternalVisibleDecls(context,
931 lldb::ModuleSP(),
932 namespace_decl,
Sean Callanan7dd98122011-10-14 20:34:21 +0000933 current_id);
Sean Callanan503aa522011-10-12 00:12:34 +0000934 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000935
Sean Callananfb3e4302011-10-29 19:50:43 +0000936 if (!context.m_found.variable)
937 ClangASTSource::FindExternalVisibleDecls(context);
Sean Callanan503aa522011-10-12 00:12:34 +0000938}
939
940void
941ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context,
Sean Callanan1a2c5382011-10-12 18:44:30 +0000942 lldb::ModuleSP module_sp,
Sean Callanan88972242011-10-12 01:39:28 +0000943 ClangNamespaceDecl &namespace_decl,
Sean Callanan7dd98122011-10-14 20:34:21 +0000944 unsigned int current_id)
Sean Callanan503aa522011-10-12 00:12:34 +0000945{
Sean Callananeddeb3b2011-10-28 23:38:38 +0000946 assert (m_ast_context);
Sean Callanan503aa522011-10-12 00:12:34 +0000947
Greg Clayton5160ce52013-03-27 23:08:40 +0000948 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfb3e4302011-10-29 19:50:43 +0000949
Greg Clayton7a345282010-11-09 23:46:37 +0000950 SymbolContextList sc_list;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000951
Sean Callananfb3e4302011-10-29 19:50:43 +0000952 const ConstString name(context.m_decl_name.getAsString().c_str());
953
Greg Clayton83c5cd92010-11-14 22:13:40 +0000954 const char *name_unique_cstr = name.GetCString();
955
956 if (name_unique_cstr == NULL)
957 return;
Sean Callananfb3e4302011-10-29 19:50:43 +0000958
Sean Callanan5b26f272012-02-04 08:49:35 +0000959 static ConstString id_name("id");
960 static ConstString Class_name("Class");
961
962 if (name == id_name || name == Class_name)
963 return;
964
Greg Clayton7b462cc2010-10-15 22:48:33 +0000965 // Only look for functions by name out in our symbols if the function
966 // doesn't start with our phony prefix of '$'
Sean Callanan933693b2012-02-10 01:22:05 +0000967 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Jason Molendab57e4a12013-11-04 09:33:30 +0000968 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
Sean Callanan88972242011-10-12 01:39:28 +0000969 if (name_unique_cstr[0] == '$' && !namespace_decl)
970 {
971 static ConstString g_lldb_class_name ("$__lldb_class");
972
973 if (name == g_lldb_class_name)
974 {
975 // Clang is looking for the type of "this"
Sean Callananc03bdd92012-02-08 03:45:08 +0000976
977 if (frame == NULL)
Sean Callanan88972242011-10-12 01:39:28 +0000978 return;
979
Sean Callananc03bdd92012-02-08 03:45:08 +0000980 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
Sean Callanan88972242011-10-12 01:39:28 +0000981
Sean Callananc03bdd92012-02-08 03:45:08 +0000982 if (!sym_ctx.function)
Sean Callanan88972242011-10-12 01:39:28 +0000983 return;
984
Greg Clayton685c88c2012-07-14 00:53:55 +0000985 // Get the block that defines the function
986 Block *function_block = sym_ctx.GetFunctionBlock();
987
988 if (!function_block)
989 return;
990
991 clang::DeclContext *decl_context = function_block->GetClangDeclContext();
Sean Callananc03bdd92012-02-08 03:45:08 +0000992
993 if (!decl_context)
Sean Callanan88972242011-10-12 01:39:28 +0000994 return;
995
Sean Callananc03bdd92012-02-08 03:45:08 +0000996 clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context);
Sean Callanan88972242011-10-12 01:39:28 +0000997
Jim Ingham5fdeed42012-10-30 23:35:54 +0000998 if (method_decl)
Sean Callanan88972242011-10-12 01:39:28 +0000999 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001000 clang::CXXRecordDecl *class_decl = method_decl->getParent();
1001
1002 QualType class_qual_type(class_decl->getTypeForDecl(), 0);
1003
1004 TypeFromUser class_user_type (class_qual_type.getAsOpaquePtr(),
1005 &class_decl->getASTContext());
1006
1007 if (log)
1008 {
1009 ASTDumper ast_dumper(class_qual_type);
1010 log->Printf(" CEDM::FEVD[%u] Adding type for $__lldb_class: %s", current_id, ast_dumper.GetCString());
1011 }
1012
Sean Callananfa4fab72013-02-01 06:55:48 +00001013 TypeFromParser class_type = CopyClassType(class_user_type, current_id);
1014
1015 if (!class_type.IsValid())
1016 return;
1017
Sean Callanan34cf8202013-03-12 21:22:00 +00001018 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(QualType::getFromOpaquePtr(class_type.GetOpaqueQualType()));
Sean Callananfa4fab72013-02-01 06:55:48 +00001019
1020 if (!type_source_info)
1021 return;
1022
1023 TypedefDecl *typedef_decl = TypedefDecl::Create(*m_ast_context,
1024 m_ast_context->getTranslationUnitDecl(),
1025 SourceLocation(),
1026 SourceLocation(),
1027 context.m_decl_name.getAsIdentifierInfo(),
1028 type_source_info);
1029
1030
1031 if (!typedef_decl)
1032 return;
1033
1034 context.AddNamedDecl(typedef_decl);
Jim Ingham5fdeed42012-10-30 23:35:54 +00001035
1036 if (method_decl->isInstance())
1037 {
1038 // self is a pointer to the object
1039
1040 QualType class_pointer_type = method_decl->getASTContext().getPointerType(class_qual_type);
1041
1042 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
1043 &method_decl->getASTContext());
1044
1045 m_struct_vars->m_object_pointer_type = self_user_type;
1046 }
Sean Callanan88972242011-10-12 01:39:28 +00001047 }
Jim Ingham5fdeed42012-10-30 23:35:54 +00001048 else
Sean Callananbb6e7ed2012-03-05 22:08:20 +00001049 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001050 // This branch will get hit if we are executing code in the context of a function that
1051 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a
1052 // method of the class. In that case, just look up the "this" variable in the the current
1053 // scope and use its type.
1054 // FIXME: This code is formally correct, but clang doesn't currently emit DW_AT_object_pointer
1055 // for C++ so it hasn't actually been tested.
Sean Callananbb6e7ed2012-03-05 22:08:20 +00001056
Jim Ingham5fdeed42012-10-30 23:35:54 +00001057 VariableList *vars = frame->GetVariableList(false);
Sean Callananbb6e7ed2012-03-05 22:08:20 +00001058
Jim Ingham5fdeed42012-10-30 23:35:54 +00001059 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
Sean Callananbb6e7ed2012-03-05 22:08:20 +00001060
Jim Ingham5fdeed42012-10-30 23:35:54 +00001061 if (this_var &&
1062 this_var->IsInScope(frame) &&
1063 this_var->LocationIsValidForFrame (frame))
1064 {
1065 Type *this_type = this_var->GetType();
1066
1067 if (!this_type)
1068 return;
1069
Greg Clayton57ee3062013-07-11 22:46:58 +00001070 ClangASTType pointee_type = this_type->GetClangForwardType().GetPointeeType();
Jim Ingham5fdeed42012-10-30 23:35:54 +00001071
Greg Clayton57ee3062013-07-11 22:46:58 +00001072 if (pointee_type.IsValid())
Jim Ingham5fdeed42012-10-30 23:35:54 +00001073 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001074 if (log)
1075 {
1076 ASTDumper ast_dumper(this_type->GetClangFullType());
1077 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1078 }
1079
Greg Clayton57ee3062013-07-11 22:46:58 +00001080 TypeFromUser class_user_type(pointee_type);
Sean Callananfa4fab72013-02-01 06:55:48 +00001081 AddOneType(context, class_user_type, current_id);
Jim Ingham5fdeed42012-10-30 23:35:54 +00001082
1083
Greg Clayton57ee3062013-07-11 22:46:58 +00001084 TypeFromUser this_user_type(this_type->GetClangFullType());
Jim Ingham5fdeed42012-10-30 23:35:54 +00001085 m_struct_vars->m_object_pointer_type = this_user_type;
1086 return;
1087 }
1088 }
Sean Callananbb6e7ed2012-03-05 22:08:20 +00001089 }
1090
Sean Callanan88972242011-10-12 01:39:28 +00001091 return;
1092 }
1093
1094 static ConstString g_lldb_objc_class_name ("$__lldb_objc_class");
1095 if (name == g_lldb_objc_class_name)
1096 {
1097 // Clang is looking for the type of "*self"
1098
1099 if (!frame)
1100 return;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001101
1102 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
1103
1104 if (!sym_ctx.function)
1105 return;
1106
Greg Clayton685c88c2012-07-14 00:53:55 +00001107 // Get the block that defines the function
1108 Block *function_block = sym_ctx.GetFunctionBlock();
Sean Callanand5c17ed2011-11-15 02:11:17 +00001109
Greg Clayton685c88c2012-07-14 00:53:55 +00001110 if (!function_block)
1111 return;
1112
1113 clang::DeclContext *decl_context = function_block->GetClangDeclContext();
Sean Callanand5c17ed2011-11-15 02:11:17 +00001114
1115 if (!decl_context)
1116 return;
1117
1118 clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context);
1119
Jim Ingham5fdeed42012-10-30 23:35:54 +00001120 if (method_decl)
Sean Callanand5c17ed2011-11-15 02:11:17 +00001121 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001122 ObjCInterfaceDecl* self_interface = method_decl->getClassInterface();
Sean Callanand5c17ed2011-11-15 02:11:17 +00001123
Jim Ingham5fdeed42012-10-30 23:35:54 +00001124 if (!self_interface)
Sean Callananc03bdd92012-02-08 03:45:08 +00001125 return;
1126
Jim Ingham5fdeed42012-10-30 23:35:54 +00001127 const clang::Type *interface_type = self_interface->getTypeForDecl();
Sean Callanandfb87d62013-04-09 21:30:48 +00001128
1129 if (!interface_type)
1130 return; // This is unlikely, but we have seen crashes where this occurred
Jim Ingham5fdeed42012-10-30 23:35:54 +00001131
1132 TypeFromUser class_user_type(QualType(interface_type, 0).getAsOpaquePtr(),
1133 &method_decl->getASTContext());
Sean Callananc03bdd92012-02-08 03:45:08 +00001134
Jim Ingham5fdeed42012-10-30 23:35:54 +00001135 if (log)
1136 {
1137 ASTDumper ast_dumper(interface_type);
1138 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1139 }
1140
Sean Callananfa4fab72013-02-01 06:55:48 +00001141 AddOneType(context, class_user_type, current_id);
Jim Ingham5fdeed42012-10-30 23:35:54 +00001142
1143 if (method_decl->isInstanceMethod())
1144 {
1145 // self is a pointer to the object
1146
1147 QualType class_pointer_type = method_decl->getASTContext().getObjCObjectPointerType(QualType(interface_type, 0));
1148
1149 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
1150 &method_decl->getASTContext());
1151
1152 m_struct_vars->m_object_pointer_type = self_user_type;
1153 }
1154 else
1155 {
1156 // self is a Class pointer
1157 QualType class_type = method_decl->getASTContext().getObjCClassType();
1158
1159 TypeFromUser self_user_type(class_type.getAsOpaquePtr(),
1160 &method_decl->getASTContext());
1161
1162 m_struct_vars->m_object_pointer_type = self_user_type;
1163 }
1164
1165 return;
Sean Callananc03bdd92012-02-08 03:45:08 +00001166 }
1167 else
1168 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001169 // This branch will get hit if we are executing code in the context of a function that
1170 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a
1171 // method of the class. In that case, just look up the "self" variable in the the current
1172 // scope and use its type.
Sean Callananc03bdd92012-02-08 03:45:08 +00001173
Jim Ingham5fdeed42012-10-30 23:35:54 +00001174 VariableList *vars = frame->GetVariableList(false);
Sean Callananc03bdd92012-02-08 03:45:08 +00001175
Jim Ingham5fdeed42012-10-30 23:35:54 +00001176 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
1177
1178 if (self_var &&
1179 self_var->IsInScope(frame) &&
1180 self_var->LocationIsValidForFrame (frame))
1181 {
1182 Type *self_type = self_var->GetType();
1183
1184 if (!self_type)
1185 return;
1186
Greg Clayton57ee3062013-07-11 22:46:58 +00001187 ClangASTType self_clang_type = self_type->GetClangFullType();
Jim Ingham5fdeed42012-10-30 23:35:54 +00001188
Greg Clayton57ee3062013-07-11 22:46:58 +00001189 if (self_clang_type.IsObjCClassType())
Jim Ingham5fdeed42012-10-30 23:35:54 +00001190 {
Sean Callanana2868d42013-01-19 01:49:02 +00001191 return;
1192 }
Greg Clayton57ee3062013-07-11 22:46:58 +00001193 else if (self_clang_type.IsObjCObjectPointerType())
Sean Callanana2868d42013-01-19 01:49:02 +00001194 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001195 self_clang_type = self_clang_type.GetPointeeType();
1196
1197 if (!self_clang_type)
Sean Callanane5270592013-04-26 22:54:19 +00001198 return;
1199
Jim Ingham5fdeed42012-10-30 23:35:54 +00001200 if (log)
1201 {
1202 ASTDumper ast_dumper(self_type->GetClangFullType());
1203 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1204 }
1205
Greg Clayton57ee3062013-07-11 22:46:58 +00001206 TypeFromUser class_user_type (self_clang_type);
Sean Callanana2868d42013-01-19 01:49:02 +00001207
Sean Callananfa4fab72013-02-01 06:55:48 +00001208 AddOneType(context, class_user_type, current_id);
Jim Ingham5fdeed42012-10-30 23:35:54 +00001209
Greg Clayton57ee3062013-07-11 22:46:58 +00001210 TypeFromUser self_user_type(self_type->GetClangFullType());
Jim Ingham5fdeed42012-10-30 23:35:54 +00001211
1212 m_struct_vars->m_object_pointer_type = self_user_type;
1213 return;
1214 }
1215 }
Sean Callananc03bdd92012-02-08 03:45:08 +00001216 }
1217
Sean Callanan88972242011-10-12 01:39:28 +00001218 return;
1219 }
1220
1221 // any other $__lldb names should be weeded out now
1222 if (!::strncmp(name_unique_cstr, "$__lldb", sizeof("$__lldb") - 1))
1223 return;
1224
1225 do
1226 {
1227 if (!target)
1228 break;
1229
1230 ClangASTContext *scratch_clang_ast_context = target->GetScratchClangASTContext();
1231
1232 if (!scratch_clang_ast_context)
1233 break;
1234
1235 ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
1236
1237 if (!scratch_ast_context)
1238 break;
1239
1240 TypeDecl *ptype_type_decl = m_parser_vars->m_persistent_vars->GetPersistentType(name);
1241
1242 if (!ptype_type_decl)
1243 break;
1244
Sean Callanan0eed0d42011-12-06 03:41:14 +00001245 Decl *parser_ptype_decl = m_ast_importer->CopyDecl(m_ast_context, scratch_ast_context, ptype_type_decl);
Sean Callanan88972242011-10-12 01:39:28 +00001246
1247 if (!parser_ptype_decl)
1248 break;
1249
1250 TypeDecl *parser_ptype_type_decl = dyn_cast<TypeDecl>(parser_ptype_decl);
1251
1252 if (!parser_ptype_type_decl)
1253 break;
1254
1255 if (log)
Sean Callananfb3e4302011-10-29 19:50:43 +00001256 log->Printf(" CEDM::FEVD[%u] Found persistent type %s", current_id, name.GetCString());
Sean Callanan88972242011-10-12 01:39:28 +00001257
1258 context.AddNamedDecl(parser_ptype_type_decl);
1259 } while (0);
1260
1261 ClangExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariable(name));
1262
1263 if (pvar_sp)
1264 {
Sean Callanan7dd98122011-10-14 20:34:21 +00001265 AddOneVariable(context, pvar_sp, current_id);
Sean Callanan88972242011-10-12 01:39:28 +00001266 return;
1267 }
1268
1269 const char *reg_name(&name.GetCString()[1]);
1270
Sean Callanan933693b2012-02-10 01:22:05 +00001271 if (m_parser_vars->m_exe_ctx.GetRegisterContext())
Sean Callanan88972242011-10-12 01:39:28 +00001272 {
Sean Callanan933693b2012-02-10 01:22:05 +00001273 const RegisterInfo *reg_info(m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(reg_name));
Sean Callananfb3e4302011-10-29 19:50:43 +00001274
Sean Callanan88972242011-10-12 01:39:28 +00001275 if (reg_info)
Sean Callanan35c7f982011-10-18 16:46:55 +00001276 {
1277 if (log)
Sean Callananfb3e4302011-10-29 19:50:43 +00001278 log->Printf(" CEDM::FEVD[%u] Found register %s", current_id, reg_info->name);
Sean Callanan35c7f982011-10-18 16:46:55 +00001279
Sean Callanan7dd98122011-10-14 20:34:21 +00001280 AddOneRegister(context, reg_info, current_id);
Sean Callanan35c7f982011-10-18 16:46:55 +00001281 }
Sean Callanan88972242011-10-12 01:39:28 +00001282 }
1283 }
1284 else
Sean Callanan8ade1042010-07-27 00:55:47 +00001285 {
Sean Callanan69b53412011-08-06 00:28:14 +00001286 ValueObjectSP valobj;
1287 VariableSP var;
1288 Error err;
1289
Sean Callanan88972242011-10-12 01:39:28 +00001290 if (frame && !namespace_decl)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001291 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001292 valobj = frame->GetValueForVariableExpressionPath(name_unique_cstr,
Sean Callanan503aa522011-10-12 00:12:34 +00001293 eNoDynamicValues,
Jason Molendab57e4a12013-11-04 09:33:30 +00001294 StackFrame::eExpressionPathOptionCheckPtrVsMember ||
1295 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess ||
1296 StackFrame::eExpressionPathOptionsNoFragileObjcIvar ||
1297 StackFrame::eExpressionPathOptionsNoSyntheticChildren ||
1298 StackFrame::eExpressionPathOptionsNoSyntheticArrayRange,
Sean Callanan503aa522011-10-12 00:12:34 +00001299 var,
1300 err);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001301
1302 // If we found a variable in scope, no need to pull up function names
Sean Callanan9a028512012-08-09 00:50:26 +00001303 if (err.Success() && var)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001304 {
Sean Callanan0712f462011-11-29 22:03:21 +00001305 AddOneVariable(context, var, valobj, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001306 context.m_found.variable = true;
Sean Callananefa7d1f2011-10-25 20:36:57 +00001307 return;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001308 }
Greg Clayton7b462cc2010-10-15 22:48:33 +00001309 }
Sean Callananfd1ba912011-12-10 04:03:38 +00001310
1311 if (target)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001312 {
Sean Callanan1fd3f4f2011-10-12 16:59:31 +00001313 var = FindGlobalVariable (*target,
Sean Callanan1a2c5382011-10-12 18:44:30 +00001314 module_sp,
Sean Callanan1fd3f4f2011-10-12 16:59:31 +00001315 name,
1316 &namespace_decl,
Greg Claytonc14ee322011-09-22 04:58:26 +00001317 NULL);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001318
1319 if (var)
1320 {
Sean Callanan9b3569b2011-12-10 03:12:34 +00001321 valobj = ValueObjectVariable::Create(target, var);
Sean Callanan0712f462011-11-29 22:03:21 +00001322 AddOneVariable(context, var, valobj, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001323 context.m_found.variable = true;
Sean Callananfd1ba912011-12-10 04:03:38 +00001324 return;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001325 }
1326 }
1327
Sean Callananca4e0fd2011-10-12 20:29:25 +00001328 if (!context.m_found.variable)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001329 {
Sean Callanan9df05fb2012-02-10 22:52:19 +00001330 const bool include_inlines = false;
Greg Clayton931180e2011-01-27 06:44:37 +00001331 const bool append = false;
Sean Callanan25ea08e2011-10-12 17:38:09 +00001332
Sean Callanan1a2c5382011-10-12 18:44:30 +00001333 if (namespace_decl && module_sp)
Sean Callanan25ea08e2011-10-12 17:38:09 +00001334 {
Sean Callanan3ae61792012-02-15 17:14:49 +00001335 const bool include_symbols = false;
1336
Sean Callanan1a2c5382011-10-12 18:44:30 +00001337 module_sp->FindFunctions(name,
1338 &namespace_decl,
1339 eFunctionNameTypeBase,
1340 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001341 include_inlines,
Sean Callanan1a2c5382011-10-12 18:44:30 +00001342 append,
1343 sc_list);
Sean Callanan25ea08e2011-10-12 17:38:09 +00001344 }
Jim Ingham28eb5712012-10-12 17:34:26 +00001345 else if (target && !namespace_decl)
Sean Callanan25ea08e2011-10-12 17:38:09 +00001346 {
Sean Callanan3ae61792012-02-15 17:14:49 +00001347 const bool include_symbols = true;
1348
Sean Callanan308a3c52012-07-28 00:21:01 +00001349 // TODO Fix FindFunctions so that it doesn't return
1350 // instance methods for eFunctionNameTypeBase.
1351
Sean Callanan25ea08e2011-10-12 17:38:09 +00001352 target->GetImages().FindFunctions(name,
Greg Clayton43fe2172013-04-03 02:00:15 +00001353 eFunctionNameTypeFull,
Sean Callanan25ea08e2011-10-12 17:38:09 +00001354 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001355 include_inlines,
Sean Callanan25ea08e2011-10-12 17:38:09 +00001356 append,
1357 sc_list);
1358 }
1359
Sean Callanand9ca42a2011-05-08 02:21:26 +00001360 if (sc_list.GetSize())
Greg Clayton7b462cc2010-10-15 22:48:33 +00001361 {
Greg Claytondc25a0b2013-10-31 16:59:47 +00001362 Symbol *extern_symbol = NULL;
1363 Symbol *non_extern_symbol = NULL;
Sean Callanand9ca42a2011-05-08 02:21:26 +00001364
1365 for (uint32_t index = 0, num_indices = sc_list.GetSize();
1366 index < num_indices;
1367 ++index)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001368 {
Sean Callanand9ca42a2011-05-08 02:21:26 +00001369 SymbolContext sym_ctx;
1370 sc_list.GetContextAtIndex(index, sym_ctx);
1371
1372 if (sym_ctx.function)
1373 {
Sean Callanan308a3c52012-07-28 00:21:01 +00001374 clang::DeclContext *decl_ctx = sym_ctx.function->GetClangDeclContext();
1375
Sean Callanan956dca92013-04-27 01:57:44 +00001376 if (!decl_ctx)
1377 continue;
1378
Sean Callanan308a3c52012-07-28 00:21:01 +00001379 // Filter out class/instance methods.
1380 if (dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
1381 continue;
1382 if (dyn_cast<clang::CXXMethodDecl>(decl_ctx))
1383 continue;
1384
Sean Callanan485f7322013-04-24 00:34:41 +00001385 AddOneFunction(context, sym_ctx.function, NULL, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001386 context.m_found.function_with_type_info = true;
1387 context.m_found.function = true;
Sean Callanand9ca42a2011-05-08 02:21:26 +00001388 }
1389 else if (sym_ctx.symbol)
1390 {
Greg Clayton9191db42013-10-21 18:40:51 +00001391 if (sym_ctx.symbol->GetType() == eSymbolTypeReExported)
Greg Claytondc25a0b2013-10-31 16:59:47 +00001392 {
1393 sym_ctx.symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target);
1394 if (sym_ctx.symbol == NULL)
1395 continue;
1396 }
1397
1398 if (sym_ctx.symbol->IsExternal())
1399 extern_symbol = sym_ctx.symbol;
Sean Callanand9ca42a2011-05-08 02:21:26 +00001400 else
Greg Claytondc25a0b2013-10-31 16:59:47 +00001401 non_extern_symbol = sym_ctx.symbol;
Sean Callanand9ca42a2011-05-08 02:21:26 +00001402 }
Greg Clayton7b462cc2010-10-15 22:48:33 +00001403 }
Sean Callanand9ca42a2011-05-08 02:21:26 +00001404
Sean Callananca4e0fd2011-10-12 20:29:25 +00001405 if (!context.m_found.function_with_type_info)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001406 {
Greg Claytondc25a0b2013-10-31 16:59:47 +00001407 if (extern_symbol)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001408 {
Greg Claytondc25a0b2013-10-31 16:59:47 +00001409 AddOneFunction (context, NULL, extern_symbol, current_id);
1410 context.m_found.function = true;
1411 }
1412 else if (non_extern_symbol)
1413 {
1414 AddOneFunction (context, NULL, non_extern_symbol, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001415 context.m_found.function = true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001416 }
Sean Callanand9ca42a2011-05-08 02:21:26 +00001417 }
Greg Clayton7b462cc2010-10-15 22:48:33 +00001418 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001419
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001420 if (target && !context.m_found.variable && !namespace_decl)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001421 {
Sean Callananca4e0fd2011-10-12 20:29:25 +00001422 // We couldn't find a non-symbol variable for this. Now we'll hunt for a generic
Sean Callanand9ca42a2011-05-08 02:21:26 +00001423 // data symbol, and -- if it is found -- treat it as a variable.
1424
Greg Clayton3d51b9f2012-11-27 01:52:16 +00001425 const Symbol *data_symbol = FindGlobalDataSymbol(*target, name);
Sean Callanand9ca42a2011-05-08 02:21:26 +00001426
1427 if (data_symbol)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001428 {
Sean Callanan92cdbc82014-02-19 23:37:25 +00001429 std::string warning("got name from symbols: ");
1430 warning.append(name.AsCString());
1431 const unsigned diag_id = m_ast_context->getDiagnostics().getCustomDiagID(clang::DiagnosticsEngine::Level::Warning, "%0");
1432 m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str();
Sean Callanan7dd98122011-10-14 20:34:21 +00001433 AddOneGenericVariable(context, *data_symbol, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001434 context.m_found.variable = true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001435 }
Greg Clayton526e5af2010-11-13 03:52:47 +00001436 }
Sean Callanan3883b5a2010-09-07 21:49:41 +00001437 }
Sean Callananfb40b0d2011-10-27 02:06:03 +00001438 }
Sean Callanan6b4067c2010-07-17 00:43:37 +00001439}
Greg Claytona2721472011-06-25 00:44:06 +00001440
Saleem Abdulrasoola68f7b62014-03-20 06:08:36 +00001441//static clang_type_t
1442//MaybePromoteToBlockPointerType
1443//(
1444// ASTContext *ast_context,
1445// clang_type_t candidate_type
1446//)
1447//{
1448// if (!candidate_type)
1449// return candidate_type;
1450//
1451// QualType candidate_qual_type = QualType::getFromOpaquePtr(candidate_type);
1452//
1453// const PointerType *candidate_pointer_type = dyn_cast<PointerType>(candidate_qual_type);
1454//
1455// if (!candidate_pointer_type)
1456// return candidate_type;
1457//
1458// QualType pointee_qual_type = candidate_pointer_type->getPointeeType();
1459//
1460// const RecordType *pointee_record_type = dyn_cast<RecordType>(pointee_qual_type);
1461//
1462// if (!pointee_record_type)
1463// return candidate_type;
1464//
1465// RecordDecl *pointee_record_decl = pointee_record_type->getDecl();
1466//
1467// if (!pointee_record_decl->isRecord())
1468// return candidate_type;
1469//
1470// if (!pointee_record_decl->getName().startswith(llvm::StringRef("__block_literal_")))
1471// return candidate_type;
1472//
1473// QualType generic_function_type = ast_context->getFunctionNoProtoType(ast_context->UnknownAnyTy);
1474// QualType block_pointer_type = ast_context->getBlockPointerType(generic_function_type);
1475//
1476// return block_pointer_type.getAsOpaquePtr();
1477//}
Sean Callanan75383bf2012-03-06 21:56:33 +00001478
Greg Clayton57ee3062013-07-11 22:46:58 +00001479bool
1480ClangExpressionDeclMap::GetVariableValue (VariableSP &var,
1481 lldb_private::Value &var_location,
1482 TypeFromUser *user_type,
1483 TypeFromParser *parser_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001484{
Greg Clayton5160ce52013-03-27 23:08:40 +00001485 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananf06ba8d2010-06-23 00:47:48 +00001486
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001487 Type *var_type = var->GetType();
1488
1489 if (!var_type)
1490 {
Sean Callananea22d422010-07-16 00:09:46 +00001491 if (log)
1492 log->PutCString("Skipped a definition because it has no type");
Greg Clayton57ee3062013-07-11 22:46:58 +00001493 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001494 }
1495
Greg Clayton57ee3062013-07-11 22:46:58 +00001496 ClangASTType var_clang_type = var_type->GetClangFullType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001497
Greg Clayton57ee3062013-07-11 22:46:58 +00001498 if (!var_clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001499 {
Sean Callananea22d422010-07-16 00:09:46 +00001500 if (log)
1501 log->PutCString("Skipped a definition because it has no Clang type");
Greg Clayton57ee3062013-07-11 22:46:58 +00001502 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001503 }
1504
Sean Callanancc427fa2011-07-30 02:42:06 +00001505 ASTContext *ast = var_type->GetClangASTContext().getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001506
Greg Clayton6beaaa62011-01-17 03:46:26 +00001507 if (!ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001508 {
Sean Callananea22d422010-07-16 00:09:46 +00001509 if (log)
1510 log->PutCString("There is no AST context for the current execution context");
Greg Clayton57ee3062013-07-11 22:46:58 +00001511 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001512 }
Greg Clayton57ee3062013-07-11 22:46:58 +00001513 //var_clang_type = MaybePromoteToBlockPointerType (ast, var_clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001514
Sean Callanan6b4067c2010-07-17 00:43:37 +00001515 DWARFExpression &var_location_expr = var->LocationExpression();
1516
Greg Clayton016a95e2010-09-14 02:20:48 +00001517 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
1518
Sean Callanan933693b2012-02-10 01:22:05 +00001519 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00001520
Greg Clayton016a95e2010-09-14 02:20:48 +00001521 if (var_location_expr.IsLocationList())
1522 {
1523 SymbolContext var_sc;
1524 var->CalculateSymbolContext (&var_sc);
Greg Claytonc14ee322011-09-22 04:58:26 +00001525 loclist_base_load_addr = var_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
Greg Clayton016a95e2010-09-14 02:20:48 +00001526 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001527 Error err;
1528
Sean Callanan7b5805d2013-01-18 21:20:51 +00001529 if (var->GetLocationIsConstantValueData())
1530 {
1531 DataExtractor const_value_extractor;
1532
1533 if (var_location_expr.GetExpressionData(const_value_extractor))
1534 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001535 var_location = Value(const_value_extractor.GetDataStart(), const_value_extractor.GetByteSize());
1536 var_location.SetValueType(Value::eValueTypeHostAddress);
Sean Callanan7b5805d2013-01-18 21:20:51 +00001537 }
1538 else
1539 {
1540 if (log)
1541 log->Printf("Error evaluating constant variable: %s", err.AsCString());
Greg Clayton57ee3062013-07-11 22:46:58 +00001542 return false;
Sean Callanan7b5805d2013-01-18 21:20:51 +00001543 }
1544 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00001545
Greg Clayton57ee3062013-07-11 22:46:58 +00001546 ClangASTType type_to_use = GuardedCopyType(var_clang_type);
Sean Callanan6b4067c2010-07-17 00:43:37 +00001547
Greg Clayton57ee3062013-07-11 22:46:58 +00001548 if (!type_to_use)
Sean Callanan1d180662010-07-20 23:31:16 +00001549 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001550 if (log)
1551 log->Printf("Couldn't copy a variable's type into the parser's AST context");
Sean Callanan1d180662010-07-20 23:31:16 +00001552
Greg Clayton57ee3062013-07-11 22:46:58 +00001553 return false;
Sean Callanan1d180662010-07-20 23:31:16 +00001554 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001555
Greg Clayton57ee3062013-07-11 22:46:58 +00001556 if (parser_type)
1557 *parser_type = TypeFromParser(type_to_use);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001558
Greg Clayton57ee3062013-07-11 22:46:58 +00001559 if (var_location.GetContextType() == Value::eContextTypeInvalid)
1560 var_location.SetClangType(type_to_use);
1561
1562 if (var_location.GetValueType() == Value::eValueTypeFileAddress)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001563 {
1564 SymbolContext var_sc;
1565 var->CalculateSymbolContext(&var_sc);
Sean Callanan6b4067c2010-07-17 00:43:37 +00001566
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001567 if (!var_sc.module_sp)
Michael Sartain89c862f2013-08-07 19:05:15 +00001568 return false;
Greg Clayton3046e662013-07-10 01:23:25 +00001569
Greg Clayton57ee3062013-07-11 22:46:58 +00001570 Address so_addr(var_location.GetScalar().ULongLong(), var_sc.module_sp->GetSectionList());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001571
Greg Claytonc14ee322011-09-22 04:58:26 +00001572 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001573
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001574 if (load_addr != LLDB_INVALID_ADDRESS)
1575 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001576 var_location.GetScalar() = load_addr;
1577 var_location.SetValueType(Value::eValueTypeLoadAddress);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001578 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001579 }
1580
Sean Callanan1d180662010-07-20 23:31:16 +00001581 if (user_type)
Greg Clayton57ee3062013-07-11 22:46:58 +00001582 *user_type = TypeFromUser(var_clang_type);
Sean Callanan6b4067c2010-07-17 00:43:37 +00001583
Greg Clayton57ee3062013-07-11 22:46:58 +00001584 return true;
Sean Callanan6b4067c2010-07-17 00:43:37 +00001585}
1586
1587void
Sean Callanan0712f462011-11-29 22:03:21 +00001588ClangExpressionDeclMap::AddOneVariable (NameSearchContext &context, VariableSP var, ValueObjectSP valobj, unsigned int current_id)
Sean Callanan6b4067c2010-07-17 00:43:37 +00001589{
Sean Callanan979f74d2010-12-03 01:38:59 +00001590 assert (m_parser_vars.get());
1591
Greg Clayton5160ce52013-03-27 23:08:40 +00001592 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001593
Sean Callanan1d180662010-07-20 23:31:16 +00001594 TypeFromUser ut;
1595 TypeFromParser pt;
Greg Clayton57ee3062013-07-11 22:46:58 +00001596 Value var_location;
Sean Callanan6b4067c2010-07-17 00:43:37 +00001597
Greg Clayton57ee3062013-07-11 22:46:58 +00001598 if (!GetVariableValue (var, var_location, &ut, &pt))
1599 return;
Sean Callanan6b4067c2010-07-17 00:43:37 +00001600
Sean Callanancf128622012-03-15 01:53:17 +00001601 clang::QualType parser_opaque_type = QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
1602
Sean Callanane2d47482012-03-21 17:13:20 +00001603 if (parser_opaque_type.isNull())
1604 return;
1605
Sean Callanancf128622012-03-15 01:53:17 +00001606 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr())
1607 {
1608 if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1609 CompleteType(tag_type->getDecl());
Sean Callananf4c0a222013-12-20 04:09:05 +00001610 if (const ObjCObjectPointerType *objc_object_ptr_type = dyn_cast<ObjCObjectPointerType>(parser_type))
1611 CompleteType(objc_object_ptr_type->getInterfaceDecl());
Sean Callanancf128622012-03-15 01:53:17 +00001612 }
1613
Sean Callanana3aa0cf2010-11-20 02:19:29 +00001614
Greg Clayton57ee3062013-07-11 22:46:58 +00001615 bool is_reference = pt.IsReferenceType();
Sean Callanan98298012011-10-27 19:41:13 +00001616
Greg Clayton57ee3062013-07-11 22:46:58 +00001617 NamedDecl *var_decl = NULL;
Sean Callanan98298012011-10-27 19:41:13 +00001618 if (is_reference)
Greg Clayton57ee3062013-07-11 22:46:58 +00001619 var_decl = context.AddVarDecl(pt);
Sean Callanan98298012011-10-27 19:41:13 +00001620 else
Greg Clayton57ee3062013-07-11 22:46:58 +00001621 var_decl = context.AddVarDecl(pt.GetLValueReferenceType());
Sean Callanan98298012011-10-27 19:41:13 +00001622
Greg Clayton7b462cc2010-10-15 22:48:33 +00001623 std::string decl_name(context.m_decl_name.getAsString());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001624 ConstString entity_name(decl_name.c_str());
Sean Callanan0712f462011-11-29 22:03:21 +00001625 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (valobj));
1626
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001627 assert (entity.get());
Sean Callanan3c495c12013-01-15 23:29:36 +00001628 entity->EnableParserVars(GetParserID());
1629 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1630 parser_vars->m_parser_type = pt;
1631 parser_vars->m_named_decl = var_decl;
1632 parser_vars->m_llvm_value = NULL;
1633 parser_vars->m_lldb_value = var_location;
1634 parser_vars->m_lldb_var = var;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001635
Sean Callanan98298012011-10-27 19:41:13 +00001636 if (is_reference)
1637 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference;
1638
Sean Callananea22d422010-07-16 00:09:46 +00001639 if (log)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001640 {
Sean Callanan82695d62011-11-07 23:32:52 +00001641 ASTDumper orig_dumper(ut.GetOpaqueQualType());
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001642 ASTDumper ast_dumper(var_decl);
Sean Callanan82695d62011-11-07 23:32:52 +00001643 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 +00001644 }
Sean Callanan468574b2010-06-22 23:46:24 +00001645}
1646
1647void
Sean Callanan2235f322010-08-11 03:57:18 +00001648ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
Sean Callanan7dd98122011-10-14 20:34:21 +00001649 ClangExpressionVariableSP &pvar_sp,
1650 unsigned int current_id)
Sean Callanan2235f322010-08-11 03:57:18 +00001651{
Greg Clayton5160ce52013-03-27 23:08:40 +00001652 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan823bb4c2010-08-30 22:17:16 +00001653
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001654 TypeFromUser user_type (pvar_sp->GetTypeFromUser());
Sean Callanan2235f322010-08-11 03:57:18 +00001655
Greg Clayton57ee3062013-07-11 22:46:58 +00001656 TypeFromParser parser_type (GuardedCopyType(user_type));
Sean Callanan2235f322010-08-11 03:57:18 +00001657
Sean Callanane0a64f72011-12-01 21:04:37 +00001658 if (!parser_type.GetOpaqueQualType())
1659 {
1660 if (log)
1661 log->Printf(" CEDM::FEVD[%u] Couldn't import type for pvar %s", current_id, pvar_sp->GetName().GetCString());
1662 return;
1663 }
1664
Greg Clayton57ee3062013-07-11 22:46:58 +00001665 NamedDecl *var_decl = context.AddVarDecl(parser_type.GetLValueReferenceType());
Sean Callanan64dfc9a2010-08-23 23:09:38 +00001666
Sean Callanan3c495c12013-01-15 23:29:36 +00001667 pvar_sp->EnableParserVars(GetParserID());
1668 ClangExpressionVariable::ParserVars *parser_vars = pvar_sp->GetParserVars(GetParserID());
1669 parser_vars->m_parser_type = parser_type;
Greg Clayton57ee3062013-07-11 22:46:58 +00001670 parser_vars->m_named_decl = var_decl;
1671 parser_vars->m_llvm_value = NULL;
1672 parser_vars->m_lldb_value.Clear();
Sean Callanan823bb4c2010-08-30 22:17:16 +00001673
1674 if (log)
Sean Callanan530782942010-11-01 23:22:47 +00001675 {
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001676 ASTDumper ast_dumper(var_decl);
Sean Callananfb3e4302011-10-29 19:50:43 +00001677 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 +00001678 }
Sean Callanan2235f322010-08-11 03:57:18 +00001679}
1680
Sean Callanan348b5892010-11-30 00:27:43 +00001681void
Sean Callanand9ca42a2011-05-08 02:21:26 +00001682ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context,
Greg Clayton3d51b9f2012-11-27 01:52:16 +00001683 const Symbol &symbol,
Sean Callanan7dd98122011-10-14 20:34:21 +00001684 unsigned int current_id)
Sean Callanand9ca42a2011-05-08 02:21:26 +00001685{
1686 assert(m_parser_vars.get());
1687
Greg Clayton5160ce52013-03-27 23:08:40 +00001688 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanand9ca42a2011-05-08 02:21:26 +00001689
Sean Callanan933693b2012-02-10 01:22:05 +00001690 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00001691
1692 if (target == NULL)
1693 return;
1694
1695 ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext();
Sean Callanand9ca42a2011-05-08 02:21:26 +00001696
Greg Clayton57ee3062013-07-11 22:46:58 +00001697 TypeFromUser user_type (ClangASTContext::GetBasicType(scratch_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType());
1698 TypeFromParser parser_type (ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType());
1699 NamedDecl *var_decl = context.AddVarDecl(parser_type);
Sean Callanand9ca42a2011-05-08 02:21:26 +00001700
1701 std::string decl_name(context.m_decl_name.getAsString());
1702 ConstString entity_name(decl_name.c_str());
Sean Callanan933693b2012-02-10 01:22:05 +00001703 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (),
Sean Callanand9ca42a2011-05-08 02:21:26 +00001704 entity_name,
1705 user_type,
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001706 m_parser_vars->m_target_info.byte_order,
1707 m_parser_vars->m_target_info.address_byte_size));
Sean Callanand9ca42a2011-05-08 02:21:26 +00001708 assert (entity.get());
Sean Callanand9ca42a2011-05-08 02:21:26 +00001709
Greg Clayton57ee3062013-07-11 22:46:58 +00001710 entity->EnableParserVars(GetParserID());
1711 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1712
Greg Clayton3d51b9f2012-11-27 01:52:16 +00001713 const Address &symbol_address = symbol.GetAddress();
Greg Claytonc14ee322011-09-22 04:58:26 +00001714 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target);
Sean Callanand9ca42a2011-05-08 02:21:26 +00001715
Greg Clayton57ee3062013-07-11 22:46:58 +00001716 //parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType());
1717 parser_vars->m_lldb_value.SetClangType(user_type);
1718 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr;
1719 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
Sean Callanand9ca42a2011-05-08 02:21:26 +00001720
Sean Callanan3c495c12013-01-15 23:29:36 +00001721 parser_vars->m_parser_type = parser_type;
1722 parser_vars->m_named_decl = var_decl;
1723 parser_vars->m_llvm_value = NULL;
Sean Callanan3c495c12013-01-15 23:29:36 +00001724 parser_vars->m_lldb_sym = &symbol;
Sean Callanand9ca42a2011-05-08 02:21:26 +00001725
1726 if (log)
1727 {
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001728 ASTDumper ast_dumper(var_decl);
Sean Callanand9ca42a2011-05-08 02:21:26 +00001729
Sean Callananfb3e4302011-10-29 19:50:43 +00001730 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 +00001731 }
1732}
1733
Sean Callanan77502262011-05-12 23:54:16 +00001734bool
1735ClangExpressionDeclMap::ResolveUnknownTypes()
1736{
Greg Clayton5160ce52013-03-27 23:08:40 +00001737 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan933693b2012-02-10 01:22:05 +00001738 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00001739
1740 ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext();
Sean Callanan77502262011-05-12 23:54:16 +00001741
1742 for (size_t index = 0, num_entities = m_found_entities.GetSize();
1743 index < num_entities;
1744 ++index)
1745 {
1746 ClangExpressionVariableSP entity = m_found_entities.GetVariableAtIndex(index);
1747
Sean Callanan3c495c12013-01-15 23:29:36 +00001748 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1749
Sean Callanan77502262011-05-12 23:54:16 +00001750 if (entity->m_flags & ClangExpressionVariable::EVUnknownType)
1751 {
Sean Callanan3c495c12013-01-15 23:29:36 +00001752 const NamedDecl *named_decl = parser_vars->m_named_decl;
Sean Callanan77502262011-05-12 23:54:16 +00001753 const VarDecl *var_decl = dyn_cast<VarDecl>(named_decl);
1754
1755 if (!var_decl)
1756 {
1757 if (log)
1758 log->Printf("Entity of unknown type does not have a VarDecl");
1759 return false;
1760 }
1761
1762 if (log)
1763 {
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001764 ASTDumper ast_dumper(const_cast<VarDecl*>(var_decl));
1765 log->Printf("Variable of unknown type now has Decl %s", ast_dumper.GetCString());
Sean Callanan77502262011-05-12 23:54:16 +00001766 }
1767
1768 QualType var_type = var_decl->getType();
1769 TypeFromParser parser_type(var_type.getAsOpaquePtr(), &var_decl->getASTContext());
1770
Sean Callanan00f43622011-11-18 03:28:09 +00001771 lldb::clang_type_t copied_type = m_ast_importer->CopyType(scratch_ast_context, &var_decl->getASTContext(), var_type.getAsOpaquePtr());
Sean Callanan77502262011-05-12 23:54:16 +00001772
Sean Callanane0a64f72011-12-01 21:04:37 +00001773 if (!copied_type)
1774 {
1775 if (log)
1776 log->Printf("ClangExpressionDeclMap::ResolveUnknownType - Couldn't import the type for a variable");
1777
Jim Inghamf94e1792012-08-11 00:35:26 +00001778 return (bool) lldb::ClangExpressionVariableSP();
Sean Callanane0a64f72011-12-01 21:04:37 +00001779 }
1780
Sean Callanan77502262011-05-12 23:54:16 +00001781 TypeFromUser user_type(copied_type, scratch_ast_context);
1782
Greg Clayton57ee3062013-07-11 22:46:58 +00001783// parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType());
1784 parser_vars->m_lldb_value.SetClangType(user_type);
Sean Callanan3c495c12013-01-15 23:29:36 +00001785 parser_vars->m_parser_type = parser_type;
Sean Callanan77502262011-05-12 23:54:16 +00001786
Greg Clayton57ee3062013-07-11 22:46:58 +00001787 entity->SetClangType(user_type);
Sean Callanan77502262011-05-12 23:54:16 +00001788
1789 entity->m_flags &= ~(ClangExpressionVariable::EVUnknownType);
1790 }
1791 }
1792
1793 return true;
1794}
1795
Sean Callanand9ca42a2011-05-08 02:21:26 +00001796void
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001797ClangExpressionDeclMap::AddOneRegister (NameSearchContext &context,
Sean Callanan7dd98122011-10-14 20:34:21 +00001798 const RegisterInfo *reg_info,
1799 unsigned int current_id)
Sean Callanan348b5892010-11-30 00:27:43 +00001800{
Greg Clayton5160ce52013-03-27 23:08:40 +00001801 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan348b5892010-11-30 00:27:43 +00001802
Greg Clayton57ee3062013-07-11 22:46:58 +00001803 ClangASTType clang_type = ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (m_ast_context,
1804 reg_info->encoding,
1805 reg_info->byte_size * 8);
Sean Callanan348b5892010-11-30 00:27:43 +00001806
Greg Clayton57ee3062013-07-11 22:46:58 +00001807 if (!clang_type)
Sean Callanan348b5892010-11-30 00:27:43 +00001808 {
Johnny Chene95fcf72011-08-09 22:52:27 +00001809 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +00001810 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 +00001811 return;
1812 }
1813
Greg Clayton57ee3062013-07-11 22:46:58 +00001814 TypeFromParser parser_clang_type (clang_type);
Sean Callanan348b5892010-11-30 00:27:43 +00001815
Greg Clayton57ee3062013-07-11 22:46:58 +00001816 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type);
Sean Callanan348b5892010-11-30 00:27:43 +00001817
Sean Callanan933693b2012-02-10 01:22:05 +00001818 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001819 m_parser_vars->m_target_info.byte_order,
1820 m_parser_vars->m_target_info.address_byte_size));
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001821 assert (entity.get());
Sean Callanan96d27302013-04-11 00:09:05 +00001822
Sean Callanan348b5892010-11-30 00:27:43 +00001823 std::string decl_name(context.m_decl_name.getAsString());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001824 entity->SetName (ConstString (decl_name.c_str()));
1825 entity->SetRegisterInfo (reg_info);
Sean Callanan3c495c12013-01-15 23:29:36 +00001826 entity->EnableParserVars(GetParserID());
1827 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
Greg Clayton57ee3062013-07-11 22:46:58 +00001828 parser_vars->m_parser_type = parser_clang_type;
1829 parser_vars->m_named_decl = var_decl;
1830 parser_vars->m_llvm_value = NULL;
1831 parser_vars->m_lldb_value.Clear();
Sean Callananf673e762012-02-15 01:40:39 +00001832 entity->m_flags |= ClangExpressionVariable::EVBareRegister;
Sean Callanan348b5892010-11-30 00:27:43 +00001833
Sean Callananf673e762012-02-15 01:40:39 +00001834 if (log)
Sean Callanan348b5892010-11-30 00:27:43 +00001835 {
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001836 ASTDumper ast_dumper(var_decl);
Sean Callananfb3e4302011-10-29 19:50:43 +00001837 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 +00001838 }
1839}
1840
Sean Callanan2235f322010-08-11 03:57:18 +00001841void
Greg Clayton3f5c08f2011-05-18 22:01:49 +00001842ClangExpressionDeclMap::AddOneFunction (NameSearchContext &context,
Greg Clayton57ee3062013-07-11 22:46:58 +00001843 Function* function,
Sean Callanan7dd98122011-10-14 20:34:21 +00001844 Symbol* symbol,
1845 unsigned int current_id)
Sean Callanan468574b2010-06-22 23:46:24 +00001846{
Sean Callanan979f74d2010-12-03 01:38:59 +00001847 assert (m_parser_vars.get());
1848
Greg Clayton5160ce52013-03-27 23:08:40 +00001849 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan468574b2010-06-22 23:46:24 +00001850
Greg Clayton57ee3062013-07-11 22:46:58 +00001851 NamedDecl *function_decl = NULL;
Sean Callanan77eaf442011-07-08 00:39:14 +00001852 const Address *fun_address = NULL;
Greg Clayton57ee3062013-07-11 22:46:58 +00001853 ClangASTType function_clang_type;
Matt Kopec00049b82013-02-27 20:13:38 +00001854
1855 bool is_indirect_function = false;
1856
Greg Clayton57ee3062013-07-11 22:46:58 +00001857 if (function)
Sean Callanan8ade1042010-07-27 00:55:47 +00001858 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001859 Type *function_type = function->GetType();
Sean Callanan8ade1042010-07-27 00:55:47 +00001860
Greg Clayton57ee3062013-07-11 22:46:58 +00001861 if (!function_type)
Sean Callanan8ade1042010-07-27 00:55:47 +00001862 {
1863 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +00001864 log->PutCString(" Skipped a function because it has no type");
Sean Callanan8ade1042010-07-27 00:55:47 +00001865 return;
1866 }
1867
Greg Clayton57ee3062013-07-11 22:46:58 +00001868 function_clang_type = function_type->GetClangFullType();
Sean Callanan8ade1042010-07-27 00:55:47 +00001869
Greg Clayton57ee3062013-07-11 22:46:58 +00001870 if (!function_clang_type)
Sean Callanan8ade1042010-07-27 00:55:47 +00001871 {
1872 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +00001873 log->PutCString(" Skipped a function because it has no Clang type");
Sean Callanan8ade1042010-07-27 00:55:47 +00001874 return;
1875 }
1876
Greg Clayton57ee3062013-07-11 22:46:58 +00001877 fun_address = &function->GetAddressRange().GetBaseAddress();
Sean Callanan8ade1042010-07-27 00:55:47 +00001878
Greg Clayton57ee3062013-07-11 22:46:58 +00001879 ClangASTType copied_function_type = GuardedCopyType(function_clang_type);
1880 if (copied_function_type)
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001881 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001882 function_decl = context.AddFunDecl(copied_function_type);
Sean Callanan485f7322013-04-24 00:34:41 +00001883
Greg Clayton57ee3062013-07-11 22:46:58 +00001884 if (!function_decl)
Sean Callanan485f7322013-04-24 00:34:41 +00001885 {
1886 if (log)
1887 {
1888 log->Printf (" Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}",
Greg Clayton57ee3062013-07-11 22:46:58 +00001889 function_type->GetName().GetCString(),
1890 function_type->GetID());
Sean Callanan485f7322013-04-24 00:34:41 +00001891 }
1892
1893 return;
1894 }
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001895 }
1896 else
1897 {
1898 // We failed to copy the type we found
1899 if (log)
1900 {
Daniel Malead01b2952012-11-29 21:49:15 +00001901 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 +00001902 function_type->GetName().GetCString(),
1903 function_type->GetID());
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001904 }
Sean Callananed8d58f2011-11-16 00:40:13 +00001905
1906 return;
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001907 }
Sean Callanan8ade1042010-07-27 00:55:47 +00001908 }
1909 else if (symbol)
1910 {
Greg Claytone7612132012-03-07 21:03:09 +00001911 fun_address = &symbol->GetAddress();
Greg Clayton57ee3062013-07-11 22:46:58 +00001912 function_decl = context.AddGenericFunDecl();
Matt Kopec00049b82013-02-27 20:13:38 +00001913 is_indirect_function = symbol->IsIndirect();
Sean Callanan8ade1042010-07-27 00:55:47 +00001914 }
1915 else
1916 {
1917 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +00001918 log->PutCString(" AddOneFunction called with no function and no symbol");
Sean Callanan8ade1042010-07-27 00:55:47 +00001919 return;
1920 }
1921
Sean Callanan933693b2012-02-10 01:22:05 +00001922 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00001923
Matt Kopec00049b82013-02-27 20:13:38 +00001924 lldb::addr_t load_addr = fun_address->GetCallableLoadAddress(target, is_indirect_function);
Sean Callanan9be9d172013-03-19 01:45:02 +00001925
Greg Clayton57ee3062013-07-11 22:46:58 +00001926 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (),
1927 m_parser_vars->m_target_info.byte_order,
1928 m_parser_vars->m_target_info.address_byte_size));
1929 assert (entity.get());
1930
1931 std::string decl_name(context.m_decl_name.getAsString());
1932 entity->SetName(ConstString(decl_name.c_str()));
1933 entity->SetClangType (function_clang_type);
1934 entity->EnableParserVars(GetParserID());
1935
1936 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1937
Sean Callanan9be9d172013-03-19 01:45:02 +00001938 if (load_addr != LLDB_INVALID_ADDRESS)
1939 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001940 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1941 parser_vars->m_lldb_value.GetScalar() = load_addr;
Sean Callanan9be9d172013-03-19 01:45:02 +00001942 }
1943 else
1944 {
1945 // We have to try finding a file address.
1946
1947 lldb::addr_t file_addr = fun_address->GetFileAddress();
1948
Greg Clayton57ee3062013-07-11 22:46:58 +00001949 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress);
1950 parser_vars->m_lldb_value.GetScalar() = file_addr;
Sean Callanan9be9d172013-03-19 01:45:02 +00001951 }
Sean Callanan468574b2010-06-22 23:46:24 +00001952
Greg Clayton57ee3062013-07-11 22:46:58 +00001953
1954 parser_vars->m_named_decl = function_decl;
Sean Callanan3c495c12013-01-15 23:29:36 +00001955 parser_vars->m_llvm_value = NULL;
Greg Clayton57ee3062013-07-11 22:46:58 +00001956
Sean Callananea22d422010-07-16 00:09:46 +00001957 if (log)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001958 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001959 ASTDumper ast_dumper(function_decl);
Sean Callanan530782942010-11-01 23:22:47 +00001960
Sean Callanan308a3c52012-07-28 00:21:01 +00001961 StreamString ss;
1962
1963 fun_address->Dump(&ss, m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), Address::DumpStyleResolvedDescription);
1964
1965 log->Printf(" CEDM::FEVD[%u] Found %s function %s (description %s), returned %s",
Sean Callanan7dd98122011-10-14 20:34:21 +00001966 current_id,
Greg Clayton57ee3062013-07-11 22:46:58 +00001967 (function ? "specific" : "generic"),
Sean Callanan308a3c52012-07-28 00:21:01 +00001968 decl_name.c_str(),
1969 ss.GetData(),
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001970 ast_dumper.GetCString());
Greg Clayton7b462cc2010-10-15 22:48:33 +00001971 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001972}
Sean Callanan5666b672010-08-04 01:02:13 +00001973
Sean Callananfa4fab72013-02-01 06:55:48 +00001974TypeFromParser
1975ClangExpressionDeclMap::CopyClassType(TypeFromUser &ut,
1976 unsigned int current_id)
Sean Callanan5666b672010-08-04 01:02:13 +00001977{
Greg Clayton57ee3062013-07-11 22:46:58 +00001978 ClangASTType copied_clang_type = GuardedCopyType(ut);
Sean Callanane0a64f72011-12-01 21:04:37 +00001979
Greg Clayton57ee3062013-07-11 22:46:58 +00001980 if (!copied_clang_type)
Sean Callanane0a64f72011-12-01 21:04:37 +00001981 {
Greg Clayton5160ce52013-03-27 23:08:40 +00001982 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanane0a64f72011-12-01 21:04:37 +00001983
Sean Callananfa4fab72013-02-01 06:55:48 +00001984 if (log)
1985 log->Printf("ClangExpressionDeclMap::CopyClassType - Couldn't import the type");
1986
1987 return TypeFromParser();
Sean Callanane0a64f72011-12-01 21:04:37 +00001988 }
Sean Callananfa4fab72013-02-01 06:55:48 +00001989
Greg Clayton57ee3062013-07-11 22:46:58 +00001990 if (copied_clang_type.IsAggregateType() && copied_clang_type.GetCompleteType ())
Sean Callananfc55f5d2010-09-21 00:44:12 +00001991 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001992 ClangASTType void_clang_type = ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid);
1993 ClangASTType void_ptr_clang_type = void_clang_type.GetPointerType();
Sean Callananfc55f5d2010-09-21 00:44:12 +00001994
Greg Clayton57ee3062013-07-11 22:46:58 +00001995 ClangASTType method_type = ClangASTContext::CreateFunctionType (m_ast_context,
1996 void_clang_type,
1997 &void_ptr_clang_type,
Sean Callananc1b732d2011-11-01 18:07:13 +00001998 1,
1999 false,
Greg Clayton57ee3062013-07-11 22:46:58 +00002000 copied_clang_type.GetTypeQualifiers());
Sean Callananc1b732d2011-11-01 18:07:13 +00002001
Greg Clayton0fffff52010-09-24 05:15:53 +00002002 const bool is_virtual = false;
2003 const bool is_static = false;
2004 const bool is_inline = false;
Greg Claytonf51de672010-10-01 02:31:07 +00002005 const bool is_explicit = false;
Sean Callananc8324752011-11-02 18:09:01 +00002006 const bool is_attr_used = true;
Sean Callanandbb58392011-11-02 01:38:59 +00002007 const bool is_artificial = false;
Greg Claytonf51de672010-10-01 02:31:07 +00002008
Greg Clayton57ee3062013-07-11 22:46:58 +00002009 copied_clang_type.AddMethodToCXXRecordType ("$__lldb_expr",
2010 method_type,
2011 lldb::eAccessPublic,
2012 is_virtual,
2013 is_static,
2014 is_inline,
2015 is_explicit,
2016 is_attr_used,
2017 is_artificial);
Sean Callananfc55f5d2010-09-21 00:44:12 +00002018 }
Sean Callanan5666b672010-08-04 01:02:13 +00002019
Greg Clayton57ee3062013-07-11 22:46:58 +00002020 return TypeFromParser(copied_clang_type);
Sean Callananfa4fab72013-02-01 06:55:48 +00002021}
2022
2023void
2024ClangExpressionDeclMap::AddOneType(NameSearchContext &context,
2025 TypeFromUser &ut,
2026 unsigned int current_id)
2027{
Greg Clayton57ee3062013-07-11 22:46:58 +00002028 ClangASTType copied_clang_type = GuardedCopyType(ut);
Sean Callananfa4fab72013-02-01 06:55:48 +00002029
Greg Clayton57ee3062013-07-11 22:46:58 +00002030 if (!copied_clang_type)
Sean Callananfa4fab72013-02-01 06:55:48 +00002031 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002032 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfa4fab72013-02-01 06:55:48 +00002033
2034 if (log)
2035 log->Printf("ClangExpressionDeclMap::AddOneType - Couldn't import the type");
2036
2037 return;
2038 }
2039
Greg Clayton57ee3062013-07-11 22:46:58 +00002040 context.AddTypeDecl(copied_clang_type);
Sean Callanan5666b672010-08-04 01:02:13 +00002041}