blob: 5b92e74174f842edf2862b3172d7f1cd97b2a804 [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"
Sean Callanan3d654b32012-09-24 22:25:51 +000011#include "clang/AST/ASTContext.h"
Sean Callanane2ef6e32010-09-23 03:01:22 +000012#include "clang/AST/DeclarationName.h"
Sean Callanan530782942010-11-01 23:22:47 +000013#include "clang/AST/Decl.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/lldb-private.h"
15#include "lldb/Core/Address.h"
Sean Callananea22d422010-07-16 00:09:46 +000016#include "lldb/Core/Error.h"
Sean Callananf06ba8d2010-06-23 00:47:48 +000017#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/Module.h"
Greg Clayton9191db42013-10-21 18:40:51 +000019#include "lldb/Core/ModuleSpec.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000020#include "lldb/Core/RegisterValue.h"
Sean Callanane4ec90e2010-12-16 03:17:46 +000021#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan9b3569b2011-12-10 03:12:34 +000022#include "lldb/Core/ValueObjectVariable.h"
Sean Callanan0917d6e2011-02-01 23:43:26 +000023#include "lldb/Expression/ASTDumper.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Expression/ClangASTSource.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000025#include "lldb/Expression/ClangModulesDeclVendor.h"
Sean Callanan2235f322010-08-11 03:57:18 +000026#include "lldb/Expression/ClangPersistentVariables.h"
Sean Callanan96d27302013-04-11 00:09:05 +000027#include "lldb/Expression/Materializer.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000028#include "lldb/Host/Endian.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Symbol/ClangASTContext.h"
Greg Clayton526e5af2010-11-13 03:52:47 +000030#include "lldb/Symbol/ClangNamespaceDecl.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Symbol/CompileUnit.h"
32#include "lldb/Symbol/Function.h"
33#include "lldb/Symbol/ObjectFile.h"
34#include "lldb/Symbol/SymbolContext.h"
Sean Callanan503aa522011-10-12 00:12:34 +000035#include "lldb/Symbol/SymbolVendor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Symbol/Type.h"
37#include "lldb/Symbol/TypeList.h"
38#include "lldb/Symbol/Variable.h"
39#include "lldb/Symbol/VariableList.h"
Sean Callanana0d56432014-11-11 02:49:44 +000040#include "lldb/Target/CPPLanguageRuntime.h"
Sean Callanan1d180662010-07-20 23:31:16 +000041#include "lldb/Target/ExecutionContext.h"
Sean Callanane0b23b52012-11-15 02:02:04 +000042#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callananea22d422010-07-16 00:09:46 +000043#include "lldb/Target/Process.h"
Sean Callananf4b9bd32010-10-05 20:18:48 +000044#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000045#include "lldb/Target/StackFrame.h"
Sean Callanan1d180662010-07-20 23:31:16 +000046#include "lldb/Target/Target.h"
Jim Ingham895c9822010-12-07 01:56:02 +000047#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048
Greg Clayton83c5cd92010-11-14 22:13:40 +000049using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050using namespace lldb_private;
51using namespace clang;
52
Sean Callanan1ee44b72011-10-29 01:58:46 +000053ClangExpressionDeclMap::ClangExpressionDeclMap (bool keep_result_in_memory, ExecutionContext &exe_ctx) :
54 ClangASTSource (exe_ctx.GetTargetSP()),
Greg Clayton7b462cc2010-10-15 22:48:33 +000055 m_found_entities (),
56 m_struct_members (),
Stephen Wilson71c21d12011-04-11 19:41:40 +000057 m_keep_result_in_memory (keep_result_in_memory),
Sean Callanan979f74d2010-12-03 01:38:59 +000058 m_parser_vars (),
Stephen Wilson71c21d12011-04-11 19:41:40 +000059 m_struct_vars ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060{
Sean Callanan979f74d2010-12-03 01:38:59 +000061 EnableStructVars();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062}
63
64ClangExpressionDeclMap::~ClangExpressionDeclMap()
Sean Callanan979f74d2010-12-03 01:38:59 +000065{
Sean Callanane3aef1d2011-10-12 22:20:02 +000066 // Note: The model is now that the parser's AST context and all associated
67 // data does not vanish until the expression has been executed. This means
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000068 // that valuable lookup data (like namespaces) doesn't vanish, but
69
Sean Callanane3aef1d2011-10-12 22:20:02 +000070 DidParse();
Sean Callanan979f74d2010-12-03 01:38:59 +000071 DisableStructVars();
72}
Sean Callananbe3a1b12010-10-26 00:31:56 +000073
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000074bool
Sean Callanan96d27302013-04-11 00:09:05 +000075ClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx,
76 Materializer *materializer)
Sean Callanan8106d802013-03-08 20:04:57 +000077{
78 ClangASTMetrics::ClearLocalCounters();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000079
Sean Callanan979f74d2010-12-03 01:38:59 +000080 EnableParserVars();
Sean Callanan933693b2012-02-10 01:22:05 +000081 m_parser_vars->m_exe_ctx = exe_ctx;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000082
Greg Claytonc14ee322011-09-22 04:58:26 +000083 Target *target = exe_ctx.GetTargetPtr();
84 if (exe_ctx.GetFramePtr())
85 m_parser_vars->m_sym_ctx = exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything);
Sean Callanand4fac252013-02-21 22:01:43 +000086 else if (exe_ctx.GetThreadPtr() && exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0))
Greg Claytonc14ee322011-09-22 04:58:26 +000087 m_parser_vars->m_sym_ctx = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext(lldb::eSymbolContextEverything);
88 else if (exe_ctx.GetProcessPtr())
Greg Clayton4d122c42011-09-17 08:33:22 +000089 {
Greg Clayton72310352013-02-23 04:12:47 +000090 m_parser_vars->m_sym_ctx.Clear(true);
Greg Claytonc14ee322011-09-22 04:58:26 +000091 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
Greg Clayton4d122c42011-09-17 08:33:22 +000092 }
Greg Claytonc14ee322011-09-22 04:58:26 +000093 else if (target)
Greg Clayton4d122c42011-09-17 08:33:22 +000094 {
Greg Clayton72310352013-02-23 04:12:47 +000095 m_parser_vars->m_sym_ctx.Clear(true);
Greg Claytonc14ee322011-09-22 04:58:26 +000096 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
Greg Clayton4d122c42011-09-17 08:33:22 +000097 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000098
Greg Claytonc14ee322011-09-22 04:58:26 +000099 if (target)
100 {
101 m_parser_vars->m_persistent_vars = &target->GetPersistentVariables();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000102
Greg Claytonc14ee322011-09-22 04:58:26 +0000103 if (!target->GetScratchClangASTContext())
104 return false;
105 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000106
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000107 m_parser_vars->m_target_info = GetTargetInfo();
Sean Callanan96d27302013-04-11 00:09:05 +0000108 m_parser_vars->m_materializer = materializer;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000109
Sean Callananb9951192011-08-01 18:18:33 +0000110 return true;
Sean Callanan979f74d2010-12-03 01:38:59 +0000111}
112
Sean Callanan96d27302013-04-11 00:09:05 +0000113void
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000114ClangExpressionDeclMap::DidParse()
Sean Callanan979f74d2010-12-03 01:38:59 +0000115{
Greg Clayton5160ce52013-03-27 23:08:40 +0000116 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8106d802013-03-08 20:04:57 +0000117
118 if (log)
119 ClangASTMetrics::DumpCounters(log);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000120
Sean Callanan979f74d2010-12-03 01:38:59 +0000121 if (m_parser_vars.get())
122 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000123 for (size_t entity_index = 0, num_entities = m_found_entities.GetSize();
Sean Callanan979f74d2010-12-03 01:38:59 +0000124 entity_index < num_entities;
125 ++entity_index)
126 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000127 ClangExpressionVariableSP var_sp(m_found_entities.GetVariableAtIndex(entity_index));
Jim Ingham28eb5712012-10-12 17:34:26 +0000128 if (var_sp)
Sean Callanan3c495c12013-01-15 23:29:36 +0000129 var_sp->DisableParserVars(GetParserID());
Sean Callanan979f74d2010-12-03 01:38:59 +0000130 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000131
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000132 for (size_t pvar_index = 0, num_pvars = m_parser_vars->m_persistent_vars->GetSize();
Sean Callanan979f74d2010-12-03 01:38:59 +0000133 pvar_index < num_pvars;
134 ++pvar_index)
135 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000136 ClangExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index));
137 if (pvar_sp)
Sean Callanan3c495c12013-01-15 23:29:36 +0000138 pvar_sp->DisableParserVars(GetParserID());
Sean Callanan979f74d2010-12-03 01:38:59 +0000139 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000140
Sean Callanan979f74d2010-12-03 01:38:59 +0000141 DisableParserVars();
Sean Callanan6b1b9532010-10-08 01:58:41 +0000142 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143}
144
Sean Callanan549c9f72010-07-13 21:41:46 +0000145// Interface for IRForTarget
146
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000147ClangExpressionDeclMap::TargetInfo
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000148ClangExpressionDeclMap::GetTargetInfo()
149{
150 assert (m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000151
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000152 TargetInfo ret;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000153
Sean Callanan933693b2012-02-10 01:22:05 +0000154 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
155
156 Process *process = exe_ctx.GetProcessPtr();
157 if (process)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000158 {
Sean Callanan933693b2012-02-10 01:22:05 +0000159 ret.byte_order = process->GetByteOrder();
160 ret.address_byte_size = process->GetAddressByteSize();
161 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000162 else
Sean Callanan933693b2012-02-10 01:22:05 +0000163 {
164 Target *target = exe_ctx.GetTargetPtr();
165 if (target)
Greg Claytonc14ee322011-09-22 04:58:26 +0000166 {
Sean Callanan933693b2012-02-10 01:22:05 +0000167 ret.byte_order = target->GetArchitecture().GetByteOrder();
168 ret.address_byte_size = target->GetArchitecture().GetAddressByteSize();
Greg Claytonc14ee322011-09-22 04:58:26 +0000169 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000170 }
Sean Callanan933693b2012-02-10 01:22:05 +0000171
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000172 return ret;
173}
174
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000175bool
176ClangExpressionDeclMap::AddPersistentVariable
Greg Clayton7b462cc2010-10-15 22:48:33 +0000177(
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000178 const NamedDecl *decl,
179 const ConstString &name,
Sean Callanan92adcac2011-01-13 08:53:35 +0000180 TypeFromParser parser_type,
181 bool is_result,
182 bool is_lvalue
Greg Clayton7b462cc2010-10-15 22:48:33 +0000183)
Sean Callanan2235f322010-08-11 03:57:18 +0000184{
Sean Callanan979f74d2010-12-03 01:38:59 +0000185 assert (m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000186
Sean Callanan1582ee62013-04-18 22:06:33 +0000187 if (m_parser_vars->m_materializer && is_result)
188 {
189 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000190
Sean Callanan1582ee62013-04-18 22:06:33 +0000191 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
192 Target *target = exe_ctx.GetTargetPtr();
193 if (target == NULL)
194 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000195
Sean Callanan1582ee62013-04-18 22:06:33 +0000196 ASTContext *context(target->GetScratchClangASTContext()->getASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000197
Sean Callanan1582ee62013-04-18 22:06:33 +0000198 TypeFromUser user_type(m_ast_importer->DeportType(context,
199 parser_type.GetASTContext(),
200 parser_type.GetOpaqueQualType()),
201 context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000202
Sean Callanan1582ee62013-04-18 22:06:33 +0000203 uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(user_type, is_lvalue, m_keep_result_in_memory, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000204
Sean Callananf35bbbc2013-07-15 18:43:36 +0000205 ClangExpressionVariableSP var_sp = m_found_entities.CreateVariable(exe_ctx.GetBestExecutionContextScope(),
206 name,
207 user_type,
208 m_parser_vars->m_target_info.byte_order,
209 m_parser_vars->m_target_info.address_byte_size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000210
Sean Callanan1582ee62013-04-18 22:06:33 +0000211 if (!var_sp)
212 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000213
Sean Callanan1582ee62013-04-18 22:06:33 +0000214 var_sp->EnableParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000215
Sean Callanan1582ee62013-04-18 22:06:33 +0000216 ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
217
218 parser_vars->m_named_decl = decl;
219 parser_vars->m_parser_type = parser_type;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000220
Sean Callanan1582ee62013-04-18 22:06:33 +0000221 var_sp->EnableJITVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000222
Sean Callanan1582ee62013-04-18 22:06:33 +0000223 ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000224
Sean Callanan1582ee62013-04-18 22:06:33 +0000225 jit_vars->m_offset = offset;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000226
Sean Callanan1582ee62013-04-18 22:06:33 +0000227 return true;
228 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000229
Greg Clayton5160ce52013-03-27 23:08:40 +0000230 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan933693b2012-02-10 01:22:05 +0000231 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
232 Target *target = exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +0000233 if (target == NULL)
234 return false;
235
236 ASTContext *context(target->GetScratchClangASTContext()->getASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000237
238 TypeFromUser user_type(m_ast_importer->DeportType(context,
Sean Callananbb120042011-12-16 21:06:35 +0000239 parser_type.GetASTContext(),
240 parser_type.GetOpaqueQualType()),
Sean Callanane1175b72011-01-13 21:23:32 +0000241 context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000242
Sean Callanan00f43622011-11-18 03:28:09 +0000243 if (!user_type.GetOpaqueQualType())
244 {
245 if (log)
246 log->Printf("Persistent variable's type wasn't copied successfully");
247 return false;
248 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000249
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000250 if (!m_parser_vars->m_target_info.IsValid())
251 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000252
Sean Callananf35bbbc2013-07-15 18:43:36 +0000253 ClangExpressionVariableSP var_sp = m_parser_vars->m_persistent_vars->CreatePersistentVariable (exe_ctx.GetBestExecutionContextScope (),
254 name,
255 user_type,
256 m_parser_vars->m_target_info.byte_order,
257 m_parser_vars->m_target_info.address_byte_size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000258
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000259 if (!var_sp)
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000260 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000261
Sean Callanan2cb5e522012-09-20 23:21:16 +0000262 var_sp->m_frozen_sp->SetHasCompleteType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000263
Sean Callanan92adcac2011-01-13 08:53:35 +0000264 if (is_result)
265 var_sp->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry;
266 else
267 var_sp->m_flags |= ClangExpressionVariable::EVKeepInTarget; // explicitly-declared persistent variables should persist
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000268
Sean Callanan92adcac2011-01-13 08:53:35 +0000269 if (is_lvalue)
270 {
271 var_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
272 }
273 else
274 {
275 var_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
276 var_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
277 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000278
Sean Callanan35005f72013-04-12 18:10:34 +0000279 if (m_keep_result_in_memory)
280 {
281 var_sp->m_flags |= ClangExpressionVariable::EVKeepInTarget;
282 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000283
Sean Callanan92adcac2011-01-13 08:53:35 +0000284 if (log)
285 log->Printf("Created persistent variable with flags 0x%hx", var_sp->m_flags);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000286
Sean Callanan3c495c12013-01-15 23:29:36 +0000287 var_sp->EnableParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000288
Sean Callanan3c495c12013-01-15 23:29:36 +0000289 ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000290
Sean Callanan3c495c12013-01-15 23:29:36 +0000291 parser_vars->m_named_decl = decl;
292 parser_vars->m_parser_type = parser_type;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000293
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000294 return true;
Sean Callanan2235f322010-08-11 03:57:18 +0000295}
296
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000297bool
298ClangExpressionDeclMap::AddValueToStruct
Greg Clayton7b462cc2010-10-15 22:48:33 +0000299(
Sean Callanancc427fa2011-07-30 02:42:06 +0000300 const NamedDecl *decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +0000301 const ConstString &name,
302 llvm::Value *value,
303 size_t size,
Zachary Turnera746e8e2014-07-02 17:24:07 +0000304 lldb::offset_t alignment
Greg Clayton7b462cc2010-10-15 22:48:33 +0000305)
Sean Callanan549c9f72010-07-13 21:41:46 +0000306{
Sean Callanan979f74d2010-12-03 01:38:59 +0000307 assert (m_struct_vars.get());
308 assert (m_parser_vars.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000309
Sean Callanandf667652013-04-11 02:05:11 +0000310 bool is_persistent_variable = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000311
Greg Clayton5160ce52013-03-27 23:08:40 +0000312 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000313
Sean Callanan979f74d2010-12-03 01:38:59 +0000314 m_struct_vars->m_struct_laid_out = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000315
Sean Callanan3c495c12013-01-15 23:29:36 +0000316 if (m_struct_members.GetVariable(decl, GetParserID()))
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000317 return true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000318
Sean Callanan3c495c12013-01-15 23:29:36 +0000319 ClangExpressionVariableSP var_sp (m_found_entities.GetVariable(decl, GetParserID()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000320
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000321 if (!var_sp)
Sean Callanandf667652013-04-11 02:05:11 +0000322 {
Sean Callanan3c495c12013-01-15 23:29:36 +0000323 var_sp = m_parser_vars->m_persistent_vars->GetVariable(decl, GetParserID());
Sean Callanandf667652013-04-11 02:05:11 +0000324 is_persistent_variable = true;
325 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000326
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000327 if (!var_sp)
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000328 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000329
Sean Callanan823bb4c2010-08-30 22:17:16 +0000330 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000331 log->Printf("Adding value for (NamedDecl*)%p [%s - %s] to the structure",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000332 static_cast<const void*>(decl), name.GetCString(),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000333 var_sp->GetName().GetCString());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000334
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000335 // We know entity->m_parser_vars is valid because we used a parser variable
336 // to find it
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000337
Sean Callanan3c495c12013-01-15 23:29:36 +0000338 ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
339
340 parser_vars->m_llvm_value = value;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000341
Sean Callanan1582ee62013-04-18 22:06:33 +0000342 if (ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID()))
343 {
344 // We already laid this out; do not touch
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000345
Sean Callanan1582ee62013-04-18 22:06:33 +0000346 if (log)
347 log->Printf("Already placed at 0x%llx", (unsigned long long)jit_vars->m_offset);
348 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000349
Sean Callanan3c495c12013-01-15 23:29:36 +0000350 var_sp->EnableJITVars(GetParserID());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000351
Sean Callanan3c495c12013-01-15 23:29:36 +0000352 ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID());
353
354 jit_vars->m_alignment = alignment;
355 jit_vars->m_size = size;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000356
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000357 m_struct_members.AddVariable(var_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000358
Sean Callanandf667652013-04-11 02:05:11 +0000359 if (m_parser_vars->m_materializer)
360 {
Sean Callanan3dd6a422013-04-11 21:16:36 +0000361 uint32_t offset = 0;
362
Sean Callanandf667652013-04-11 02:05:11 +0000363 Error err;
364
365 if (is_persistent_variable)
366 {
Sean Callanan3dd6a422013-04-11 21:16:36 +0000367 offset = m_parser_vars->m_materializer->AddPersistentVariable(var_sp, err);
Sean Callanandf667652013-04-11 02:05:11 +0000368 }
369 else
370 {
371 if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym)
Sean Callanan3dd6a422013-04-11 21:16:36 +0000372 offset = m_parser_vars->m_materializer->AddSymbol(*sym, err);
Sean Callanandf667652013-04-11 02:05:11 +0000373 else if (const RegisterInfo *reg_info = var_sp->GetRegisterInfo())
Sean Callanan3dd6a422013-04-11 21:16:36 +0000374 offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err);
Sean Callanandf667652013-04-11 02:05:11 +0000375 else if (parser_vars->m_lldb_var)
Sean Callanan3dd6a422013-04-11 21:16:36 +0000376 offset = m_parser_vars->m_materializer->AddVariable(parser_vars->m_lldb_var, err);
Sean Callanandf667652013-04-11 02:05:11 +0000377 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000378
Sean Callanan3dd6a422013-04-11 21:16:36 +0000379 if (!err.Success())
380 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000381
Sean Callanan3dd6a422013-04-11 21:16:36 +0000382 if (log)
383 log->Printf("Placed at 0x%llx", (unsigned long long)offset);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000384
Sean Callanan3dd6a422013-04-11 21:16:36 +0000385 jit_vars->m_offset = offset; // TODO DoStructLayout() should not change this.
Sean Callanandf667652013-04-11 02:05:11 +0000386 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000387
Sean Callanan549c9f72010-07-13 21:41:46 +0000388 return true;
389}
390
391bool
392ClangExpressionDeclMap::DoStructLayout ()
393{
Sean Callanan979f74d2010-12-03 01:38:59 +0000394 assert (m_struct_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000395
Sean Callanan979f74d2010-12-03 01:38:59 +0000396 if (m_struct_vars->m_struct_laid_out)
Sean Callanan549c9f72010-07-13 21:41:46 +0000397 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000398
Sean Callanan14b1bae2013-04-16 23:25:35 +0000399 if (!m_parser_vars->m_materializer)
400 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000401
Sean Callanan14b1bae2013-04-16 23:25:35 +0000402 m_struct_vars->m_struct_alignment = m_parser_vars->m_materializer->GetStructAlignment();
403 m_struct_vars->m_struct_size = m_parser_vars->m_materializer->GetStructByteSize();
Sean Callanan979f74d2010-12-03 01:38:59 +0000404 m_struct_vars->m_struct_laid_out = true;
Sean Callanan549c9f72010-07-13 21:41:46 +0000405 return true;
406}
407
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000408bool ClangExpressionDeclMap::GetStructInfo
Greg Clayton7b462cc2010-10-15 22:48:33 +0000409(
410 uint32_t &num_elements,
411 size_t &size,
Zachary Turnera746e8e2014-07-02 17:24:07 +0000412 lldb::offset_t &alignment
Greg Clayton7b462cc2010-10-15 22:48:33 +0000413)
Sean Callanan549c9f72010-07-13 21:41:46 +0000414{
Sean Callanan979f74d2010-12-03 01:38:59 +0000415 assert (m_struct_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000416
Sean Callanan979f74d2010-12-03 01:38:59 +0000417 if (!m_struct_vars->m_struct_laid_out)
Sean Callanan549c9f72010-07-13 21:41:46 +0000418 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000419
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000420 num_elements = m_struct_members.GetSize();
Sean Callanan979f74d2010-12-03 01:38:59 +0000421 size = m_struct_vars->m_struct_size;
422 alignment = m_struct_vars->m_struct_alignment;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000423
Sean Callanan549c9f72010-07-13 21:41:46 +0000424 return true;
425}
426
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000427bool
428ClangExpressionDeclMap::GetStructElement
Greg Clayton7b462cc2010-10-15 22:48:33 +0000429(
Sean Callanancc427fa2011-07-30 02:42:06 +0000430 const NamedDecl *&decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +0000431 llvm::Value *&value,
Zachary Turnera746e8e2014-07-02 17:24:07 +0000432 lldb::offset_t &offset,
Greg Clayton7b462cc2010-10-15 22:48:33 +0000433 ConstString &name,
434 uint32_t index
435)
Sean Callanan549c9f72010-07-13 21:41:46 +0000436{
Sean Callanan979f74d2010-12-03 01:38:59 +0000437 assert (m_struct_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000438
Sean Callanan979f74d2010-12-03 01:38:59 +0000439 if (!m_struct_vars->m_struct_laid_out)
Sean Callanan549c9f72010-07-13 21:41:46 +0000440 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000441
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000442 if (index >= m_struct_members.GetSize())
Sean Callanan549c9f72010-07-13 21:41:46 +0000443 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000444
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000445 ClangExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000446
Sean Callanan3c495c12013-01-15 23:29:36 +0000447 if (!member_sp)
448 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000449
Sean Callanan3c495c12013-01-15 23:29:36 +0000450 ClangExpressionVariable::ParserVars *parser_vars = member_sp->GetParserVars(GetParserID());
451 ClangExpressionVariable::JITVars *jit_vars = member_sp->GetJITVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000452
Sean Callanan3c495c12013-01-15 23:29:36 +0000453 if (!parser_vars ||
454 !jit_vars ||
Sean Callanan6f3e5ad2012-04-12 16:58:26 +0000455 !member_sp->GetValueObject())
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000456 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000457
Sean Callanan3c495c12013-01-15 23:29:36 +0000458 decl = parser_vars->m_named_decl;
459 value = parser_vars->m_llvm_value;
460 offset = jit_vars->m_offset;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000461 name = member_sp->GetName();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000462
Sean Callanan549c9f72010-07-13 21:41:46 +0000463 return true;
464}
465
Sean Callanan7ea35012010-07-27 21:39:39 +0000466bool
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000467ClangExpressionDeclMap::GetFunctionInfo
Greg Clayton7b462cc2010-10-15 22:48:33 +0000468(
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000469 const NamedDecl *decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +0000470 uint64_t &ptr
471)
Sean Callanan4edba2d2010-07-27 02:07:53 +0000472{
Sean Callanan3c495c12013-01-15 23:29:36 +0000473 ClangExpressionVariableSP entity_sp(m_found_entities.GetVariable(decl, GetParserID()));
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000474
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000475 if (!entity_sp)
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000476 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000477
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000478 // We know m_parser_vars is valid since we searched for the variable by
479 // its NamedDecl
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000480
Sean Callanan3c495c12013-01-15 23:29:36 +0000481 ClangExpressionVariable::ParserVars *parser_vars = entity_sp->GetParserVars(GetParserID());
482
Greg Clayton57ee3062013-07-11 22:46:58 +0000483 ptr = parser_vars->m_lldb_value.GetScalar().ULongLong();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000484
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000485 return true;
Sean Callanan4edba2d2010-07-27 02:07:53 +0000486}
487
Sean Callananefe9a422011-08-16 18:09:29 +0000488static void
489FindCodeSymbolInContext
490(
491 const ConstString &name,
492 SymbolContext &sym_ctx,
493 SymbolContextList &sc_list
494)
495{
Greg Clayton58ea3e32015-03-09 16:46:57 +0000496 sc_list.Clear();
Matt Kopec00049b82013-02-27 20:13:38 +0000497 SymbolContextList temp_sc_list;
Sean Callananefe9a422011-08-16 18:09:29 +0000498 if (sym_ctx.module_sp)
Greg Clayton58ea3e32015-03-09 16:46:57 +0000499 sym_ctx.module_sp->FindFunctions(name,
500 NULL,
501 eFunctionNameTypeAuto,
502 true, // include_symbols
503 false, // include_inlines
504 true, // append
505 temp_sc_list);
506 if (temp_sc_list.GetSize() == 0)
507 {
508 if (sym_ctx.target_sp)
509 sym_ctx.target_sp->GetImages().FindFunctions(name,
510 eFunctionNameTypeAuto,
511 true, // include_symbols
512 false, // include_inlines
513 true, // append
514 temp_sc_list);
515 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000516
Greg Clayton58ea3e32015-03-09 16:46:57 +0000517 SymbolContextList internal_symbol_sc_list;
Matt Kopec00049b82013-02-27 20:13:38 +0000518 unsigned temp_sc_list_size = temp_sc_list.GetSize();
519 for (unsigned i = 0; i < temp_sc_list_size; i++)
520 {
Greg Clayton58ea3e32015-03-09 16:46:57 +0000521 SymbolContext sc;
522 temp_sc_list.GetContextAtIndex(i, sc);
523 if (sc.function)
Matt Kopec00049b82013-02-27 20:13:38 +0000524 {
Greg Clayton58ea3e32015-03-09 16:46:57 +0000525 sc_list.Append(sc);
526 }
527 else if (sc.symbol)
528 {
529 if (sc.symbol->IsExternal())
Matt Kopec00049b82013-02-27 20:13:38 +0000530 {
Greg Clayton58ea3e32015-03-09 16:46:57 +0000531 sc_list.Append(sc);
532 }
533 else
534 {
535 internal_symbol_sc_list.Append(sc);
Matt Kopec00049b82013-02-27 20:13:38 +0000536 }
537 }
538 }
Greg Clayton58ea3e32015-03-09 16:46:57 +0000539
540 // If we had internal symbols and we didn't find any external symbols or
541 // functions in debug info, then fallback to the internal symbols
542 if (sc_list.GetSize() == 0 && internal_symbol_sc_list.GetSize())
543 {
544 sc_list = internal_symbol_sc_list;
545 }
Sean Callananefe9a422011-08-16 18:09:29 +0000546}
547
Sean Callanan5300d372010-07-31 01:32:05 +0000548bool
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000549ClangExpressionDeclMap::GetFunctionAddress
Greg Clayton7b462cc2010-10-15 22:48:33 +0000550(
551 const ConstString &name,
Greg Clayton3f5c08f2011-05-18 22:01:49 +0000552 uint64_t &func_addr
Greg Clayton7b462cc2010-10-15 22:48:33 +0000553)
Sean Callanan5300d372010-07-31 01:32:05 +0000554{
Sean Callanan979f74d2010-12-03 01:38:59 +0000555 assert (m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000556
Greg Clayton5160ce52013-03-27 23:08:40 +0000557 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan933693b2012-02-10 01:22:05 +0000558 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
559 Target *target = exe_ctx.GetTargetPtr();
Sean Callanan5300d372010-07-31 01:32:05 +0000560 // Back out in all cases where we're not fully initialized
Greg Claytonc14ee322011-09-22 04:58:26 +0000561 if (target == NULL)
Jim Ingham895c9822010-12-07 01:56:02 +0000562 return false;
563 if (!m_parser_vars->m_sym_ctx.target_sp)
Sean Callanan5300d372010-07-31 01:32:05 +0000564 return false;
565
Greg Clayton7a345282010-11-09 23:46:37 +0000566 SymbolContextList sc_list;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000567
Sean Callananefe9a422011-08-16 18:09:29 +0000568 FindCodeSymbolInContext(name, m_parser_vars->m_sym_ctx, sc_list);
Matt Kopec00049b82013-02-27 20:13:38 +0000569
Greg Claytonf32db512013-10-30 21:37:46 +0000570 uint32_t sc_list_size = sc_list.GetSize();
Sean Callanana0d56432014-11-11 02:49:44 +0000571
Greg Claytonf32db512013-10-30 21:37:46 +0000572 if (sc_list_size == 0)
Sean Callanan19b6afe2011-05-13 18:27:02 +0000573 {
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000574 // We occasionally get debug information in which a const function is reported
Sean Callanan19b6afe2011-05-13 18:27:02 +0000575 // as non-const, so the mangled name is wrong. This is a hack to compensate.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000576
Sean Callananf4638562011-10-25 18:02:05 +0000577 if (!strncmp(name.GetCString(), "_ZN", 3) &&
578 strncmp(name.GetCString(), "_ZNK", 4))
Sean Callanan19b6afe2011-05-13 18:27:02 +0000579 {
Sean Callananf4638562011-10-25 18:02:05 +0000580 std::string fixed_scratch("_ZNK");
581 fixed_scratch.append(name.GetCString() + 3);
582 ConstString fixed_name(fixed_scratch.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000583
Sean Callanan19b6afe2011-05-13 18:27:02 +0000584 if (log)
Sean Callananf4638562011-10-25 18:02:05 +0000585 log->Printf("Failed to find symbols given non-const name %s; trying %s", name.GetCString(), fixed_name.GetCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000586
Sean Callananf4638562011-10-25 18:02:05 +0000587 FindCodeSymbolInContext(fixed_name, m_parser_vars->m_sym_ctx, sc_list);
Greg Claytonf32db512013-10-30 21:37:46 +0000588 sc_list_size = sc_list.GetSize();
Sean Callanan19b6afe2011-05-13 18:27:02 +0000589 }
590 }
Sean Callanana0d56432014-11-11 02:49:44 +0000591
592 if (sc_list_size == 0)
593 {
594 // Sometimes we get a mangled name for a global function that actually should be "extern C."
595 // This is a hack to compensate.
596
597 const bool is_mangled = true;
598 Mangled mangled(name, is_mangled);
599
600 CPPLanguageRuntime::MethodName method_name(mangled.GetDemangledName());
601
602 llvm::StringRef basename = method_name.GetBasename();
603
604 if (!basename.empty())
605 {
606 FindCodeSymbolInContext(ConstString(basename), m_parser_vars->m_sym_ctx, sc_list);
607 sc_list_size = sc_list.GetSize();
608 }
609 }
Matt Kopec00049b82013-02-27 20:13:38 +0000610
Greg Clayton12ba7332015-03-05 23:12:02 +0000611 lldb::addr_t intern_callable_load_addr = LLDB_INVALID_ADDRESS;
612
Greg Claytonf32db512013-10-30 21:37:46 +0000613 for (uint32_t i=0; i<sc_list_size; ++i)
614 {
615 SymbolContext sym_ctx;
616 sc_list.GetContextAtIndex(i, sym_ctx);
Matt Kopec00049b82013-02-27 20:13:38 +0000617
Greg Clayton12ba7332015-03-05 23:12:02 +0000618
Sean Callanan25ea6a12014-05-23 02:30:48 +0000619 lldb::addr_t callable_load_addr = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000620
Greg Claytonf32db512013-10-30 21:37:46 +0000621 if (sym_ctx.function)
Greg Claytonf32db512013-10-30 21:37:46 +0000622 {
Sean Callanan25ea6a12014-05-23 02:30:48 +0000623 const Address func_so_addr = sym_ctx.function->GetAddressRange().GetBaseAddress();
624 if (func_so_addr.IsValid())
Greg Claytonf32db512013-10-30 21:37:46 +0000625 {
Sean Callanan25ea6a12014-05-23 02:30:48 +0000626 callable_load_addr = func_so_addr.GetCallableLoadAddress(target, false);
Greg Claytonf32db512013-10-30 21:37:46 +0000627 }
628 }
Sean Callanan25ea6a12014-05-23 02:30:48 +0000629 else if (sym_ctx.symbol)
Greg Claytonf32db512013-10-30 21:37:46 +0000630 {
Greg Clayton12ba7332015-03-05 23:12:02 +0000631 if (sym_ctx.symbol->IsExternal())
632 callable_load_addr = sym_ctx.symbol->ResolveCallableAddress(*target);
633 else
634 {
635 if (intern_callable_load_addr == LLDB_INVALID_ADDRESS)
636 intern_callable_load_addr = sym_ctx.symbol->ResolveCallableAddress(*target);
637 }
Sean Callanan25ea6a12014-05-23 02:30:48 +0000638 }
639
640 if (callable_load_addr != LLDB_INVALID_ADDRESS)
641 {
642 func_addr = callable_load_addr;
643 return true;
Greg Claytonf32db512013-10-30 21:37:46 +0000644 }
645 }
Greg Clayton12ba7332015-03-05 23:12:02 +0000646
647 // See if we found an internal symbol
648 if (intern_callable_load_addr != LLDB_INVALID_ADDRESS)
649 {
650 func_addr = intern_callable_load_addr;
651 return true;
652 }
653
Greg Claytonf32db512013-10-30 21:37:46 +0000654 return false;
Sean Callanan5300d372010-07-31 01:32:05 +0000655}
656
Greg Clayton084db102011-06-23 04:25:29 +0000657addr_t
Greg Clayton9191db42013-10-21 18:40:51 +0000658ClangExpressionDeclMap::GetSymbolAddress (Target &target,
659 Process *process,
660 const ConstString &name,
661 lldb::SymbolType symbol_type,
Deepak Panickald66b50c2013-10-22 12:27:43 +0000662 lldb_private::Module *module)
Sean Callananc3a16002011-01-17 23:42:46 +0000663{
Sean Callananc3a16002011-01-17 23:42:46 +0000664 SymbolContextList sc_list;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000665
Greg Clayton9191db42013-10-21 18:40:51 +0000666 if (module)
667 module->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
668 else
669 target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000670
Greg Clayton084db102011-06-23 04:25:29 +0000671 const uint32_t num_matches = sc_list.GetSize();
672 addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
673
Sean Callanan947ccc72011-12-01 02:04:16 +0000674 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 +0000675 {
676 SymbolContext sym_ctx;
677 sc_list.GetContextAtIndex(i, sym_ctx);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000678
Greg Claytone7612132012-03-07 21:03:09 +0000679 const Address *sym_address = &sym_ctx.symbol->GetAddress();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000680
Sean Callananc6466fc2011-07-07 23:05:43 +0000681 if (!sym_address || !sym_address->IsValid())
Sean Callanan116ace22013-04-30 00:21:42 +0000682 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000683
Greg Clayton084db102011-06-23 04:25:29 +0000684 if (sym_address)
685 {
686 switch (sym_ctx.symbol->GetType())
687 {
688 case eSymbolTypeCode:
689 case eSymbolTypeTrampoline:
690 symbol_load_addr = sym_address->GetCallableLoadAddress (&target);
691 break;
Matt Kopec00049b82013-02-27 20:13:38 +0000692
693 case eSymbolTypeResolver:
694 symbol_load_addr = sym_address->GetCallableLoadAddress (&target, true);
695 break;
696
Greg Clayton9191db42013-10-21 18:40:51 +0000697 case eSymbolTypeReExported:
698 {
699 ConstString reexport_name = sym_ctx.symbol->GetReExportedSymbolName();
700 if (reexport_name)
701 {
702 ModuleSP reexport_module_sp;
703 ModuleSpec reexport_module_spec;
704 reexport_module_spec.GetPlatformFileSpec() = sym_ctx.symbol->GetReExportedSymbolSharedLibrary();
705 if (reexport_module_spec.GetPlatformFileSpec())
706 {
707 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
708 if (!reexport_module_sp)
709 {
710 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
711 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
712 }
713 }
714 symbol_load_addr = GetSymbolAddress(target, process, sym_ctx.symbol->GetReExportedSymbolName(), symbol_type, reexport_module_sp.get());
715 }
716 }
717 break;
718
Greg Clayton084db102011-06-23 04:25:29 +0000719 case eSymbolTypeData:
720 case eSymbolTypeRuntime:
721 case eSymbolTypeVariable:
722 case eSymbolTypeLocal:
723 case eSymbolTypeParam:
724 case eSymbolTypeInvalid:
725 case eSymbolTypeAbsolute:
Greg Clayton084db102011-06-23 04:25:29 +0000726 case eSymbolTypeException:
727 case eSymbolTypeSourceFile:
728 case eSymbolTypeHeaderFile:
729 case eSymbolTypeObjectFile:
730 case eSymbolTypeCommonBlock:
731 case eSymbolTypeBlock:
732 case eSymbolTypeVariableType:
733 case eSymbolTypeLineEntry:
734 case eSymbolTypeLineHeader:
735 case eSymbolTypeScopeBegin:
736 case eSymbolTypeScopeEnd:
737 case eSymbolTypeAdditional:
738 case eSymbolTypeCompiler:
739 case eSymbolTypeInstrumentation:
740 case eSymbolTypeUndefined:
Greg Clayton456809c2011-12-03 02:30:59 +0000741 case eSymbolTypeObjCClass:
742 case eSymbolTypeObjCMetaClass:
743 case eSymbolTypeObjCIVar:
Greg Clayton084db102011-06-23 04:25:29 +0000744 symbol_load_addr = sym_address->GetLoadAddress (&target);
745 break;
746 }
747 }
748 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000749
Sean Callanane0b23b52012-11-15 02:02:04 +0000750 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process)
751 {
752 ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000753
Sean Callanane0b23b52012-11-15 02:02:04 +0000754 if (runtime)
755 {
756 symbol_load_addr = runtime->LookupRuntimeSymbol(name);
757 }
758 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000759
Greg Clayton084db102011-06-23 04:25:29 +0000760 return symbol_load_addr;
Sean Callananc3a16002011-01-17 23:42:46 +0000761}
762
Greg Clayton084db102011-06-23 04:25:29 +0000763addr_t
Sean Callanan947ccc72011-12-01 02:04:16 +0000764ClangExpressionDeclMap::GetSymbolAddress (const ConstString &name, lldb::SymbolType symbol_type)
Sean Callanand9ca42a2011-05-08 02:21:26 +0000765{
766 assert (m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000767
Sean Callanan933693b2012-02-10 01:22:05 +0000768 if (!m_parser_vars->m_exe_ctx.GetTargetPtr())
Sean Callanand9ca42a2011-05-08 02:21:26 +0000769 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000770
Sean Callanane0b23b52012-11-15 02:02:04 +0000771 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 +0000772}
773
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000774const Symbol *
775ClangExpressionDeclMap::FindGlobalDataSymbol (Target &target,
Greg Clayton9191db42013-10-21 18:40:51 +0000776 const ConstString &name,
Deepak Panickald66b50c2013-10-22 12:27:43 +0000777 lldb_private::Module *module)
Sean Callanand9ca42a2011-05-08 02:21:26 +0000778{
779 SymbolContextList sc_list;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000780
Greg Clayton9191db42013-10-21 18:40:51 +0000781 if (module)
782 module->FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
783 else
784 target.GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000785
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000786 const uint32_t matches = sc_list.GetSize();
787 for (uint32_t i=0; i<matches; ++i)
Sean Callanand9ca42a2011-05-08 02:21:26 +0000788 {
789 SymbolContext sym_ctx;
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000790 sc_list.GetContextAtIndex(i, sym_ctx);
791 if (sym_ctx.symbol)
792 {
793 const Symbol *symbol = sym_ctx.symbol;
794 const Address *sym_address = &symbol->GetAddress();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000795
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000796 if (sym_address && sym_address->IsValid())
797 {
798 switch (symbol->GetType())
799 {
800 case eSymbolTypeData:
801 case eSymbolTypeRuntime:
802 case eSymbolTypeAbsolute:
803 case eSymbolTypeObjCClass:
804 case eSymbolTypeObjCMetaClass:
805 case eSymbolTypeObjCIVar:
806 if (symbol->GetDemangledNameIsSynthesized())
807 {
808 // If the demangled name was synthesized, then don't use it
809 // for expressions. Only let the symbol match if the mangled
810 // named matches for these symbols.
811 if (symbol->GetMangled().GetMangledName() != name)
812 break;
813 }
814 return symbol;
815
Greg Clayton9191db42013-10-21 18:40:51 +0000816 case eSymbolTypeReExported:
817 {
818 ConstString reexport_name = symbol->GetReExportedSymbolName();
819 if (reexport_name)
820 {
821 ModuleSP reexport_module_sp;
822 ModuleSpec reexport_module_spec;
823 reexport_module_spec.GetPlatformFileSpec() = symbol->GetReExportedSymbolSharedLibrary();
824 if (reexport_module_spec.GetPlatformFileSpec())
825 {
826 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
827 if (!reexport_module_sp)
828 {
829 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
830 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
831 }
832 }
833 return FindGlobalDataSymbol(target, symbol->GetReExportedSymbolName(), reexport_module_sp.get());
834 }
835 }
836 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000837
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000838 case eSymbolTypeCode: // We already lookup functions elsewhere
839 case eSymbolTypeVariable:
840 case eSymbolTypeLocal:
841 case eSymbolTypeParam:
842 case eSymbolTypeTrampoline:
843 case eSymbolTypeInvalid:
844 case eSymbolTypeException:
845 case eSymbolTypeSourceFile:
846 case eSymbolTypeHeaderFile:
847 case eSymbolTypeObjectFile:
848 case eSymbolTypeCommonBlock:
849 case eSymbolTypeBlock:
850 case eSymbolTypeVariableType:
851 case eSymbolTypeLineEntry:
852 case eSymbolTypeLineHeader:
853 case eSymbolTypeScopeBegin:
854 case eSymbolTypeScopeEnd:
855 case eSymbolTypeAdditional:
856 case eSymbolTypeCompiler:
857 case eSymbolTypeInstrumentation:
858 case eSymbolTypeUndefined:
Matt Kopec00049b82013-02-27 20:13:38 +0000859 case eSymbolTypeResolver:
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000860 break;
861 }
862 }
863 }
Sean Callanand9ca42a2011-05-08 02:21:26 +0000864 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000865
Sean Callanand9ca42a2011-05-08 02:21:26 +0000866 return NULL;
867}
868
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000869lldb::VariableSP
870ClangExpressionDeclMap::FindGlobalVariable
871(
872 Target &target,
Sean Callanan1fd3f4f2011-10-12 16:59:31 +0000873 ModuleSP &module,
874 const ConstString &name,
875 ClangNamespaceDecl *namespace_decl,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000876 TypeFromUser *type
877)
878{
879 VariableList vars;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000880
Sean Callanan1fd3f4f2011-10-12 16:59:31 +0000881 if (module && namespace_decl)
882 module->FindGlobalVariables (name, namespace_decl, true, -1, vars);
883 else
884 target.GetImages().FindGlobalVariables(name, true, -1, vars);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000885
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000886 if (vars.GetSize())
887 {
888 if (type)
889 {
890 for (size_t i = 0; i < vars.GetSize(); ++i)
891 {
892 VariableSP var_sp = vars.GetVariableAtIndex(i);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000893
Greg Clayton57ee3062013-07-11 22:46:58 +0000894 if (ClangASTContext::AreTypesSame(*type, var_sp->GetType()->GetClangFullType()))
895 return var_sp;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000896 }
897 }
898 else
899 {
900 return vars.GetVariableAtIndex(0);
901 }
902 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000903
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000904 return VariableSP();
905}
906
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000907// Interface for ClangASTSource
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000908
Sean Callanan503aa522011-10-12 00:12:34 +0000909void
Sean Callananeddeb3b2011-10-28 23:38:38 +0000910ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000911{
Sean Callananeddeb3b2011-10-28 23:38:38 +0000912 assert (m_ast_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000913
Sean Callanan8106d802013-03-08 20:04:57 +0000914 ClangASTMetrics::RegisterVisibleQuery();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000915
Sean Callananeddeb3b2011-10-28 23:38:38 +0000916 const ConstString name(context.m_decl_name.getAsString().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000917
Greg Clayton5160ce52013-03-27 23:08:40 +0000918 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000919
Sean Callananfb3e4302011-10-29 19:50:43 +0000920 if (GetImportInProgress())
Sean Callanan6abfabf2010-11-19 20:20:02 +0000921 {
Sean Callanan7dd98122011-10-14 20:34:21 +0000922 if (log && log->GetVerbose())
Sean Callanan6abfabf2010-11-19 20:20:02 +0000923 log->Printf("Ignoring a query during an import");
924 return;
925 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000926
Sean Callanan7dd98122011-10-14 20:34:21 +0000927 static unsigned int invocation_id = 0;
928 unsigned int current_id = invocation_id++;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000929
Sean Callanan503aa522011-10-12 00:12:34 +0000930 if (log)
931 {
932 if (!context.m_decl_context)
Sean Callananfb3e4302011-10-29 19:50:43 +0000933 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a NULL DeclContext", current_id, name.GetCString());
Sean Callanan503aa522011-10-12 00:12:34 +0000934 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callananfb3e4302011-10-29 19:50:43 +0000935 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 +0000936 else
Sean Callananfb3e4302011-10-29 19:50:43 +0000937 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 +0000938 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000939
Sean Callanan503aa522011-10-12 00:12:34 +0000940 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
941 {
Sean Callanan1ee44b72011-10-29 01:58:46 +0000942 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000943
Sean Callanan7dd98122011-10-14 20:34:21 +0000944 if (log && log->GetVerbose())
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000945 log->Printf(" CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000946 current_id, static_cast<void*>(namespace_map.get()),
Sean Callanan7dd98122011-10-14 20:34:21 +0000947 (int)namespace_map->size());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000948
Sean Callananb2269162011-10-21 22:18:07 +0000949 if (!namespace_map)
950 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000951
Sean Callanan503aa522011-10-12 00:12:34 +0000952 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
953 i != e;
954 ++i)
955 {
956 if (log)
Sean Callananfb3e4302011-10-29 19:50:43 +0000957 log->Printf(" CEDM::FEVD[%u] Searching namespace %s in module %s",
Sean Callanan7dd98122011-10-14 20:34:21 +0000958 current_id,
Sean Callanan503aa522011-10-12 00:12:34 +0000959 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
960 i->first->GetFileSpec().GetFilename().GetCString());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000961
Sean Callananebe60672011-10-13 21:50:33 +0000962 FindExternalVisibleDecls(context,
963 i->first,
964 i->second,
Sean Callanan7dd98122011-10-14 20:34:21 +0000965 current_id);
Sean Callanan503aa522011-10-12 00:12:34 +0000966 }
967 }
Sean Callanand5c17ed2011-11-15 02:11:17 +0000968 else if (isa<TranslationUnitDecl>(context.m_decl_context))
Sean Callanan503aa522011-10-12 00:12:34 +0000969 {
970 ClangNamespaceDecl namespace_decl;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000971
Sean Callanan503aa522011-10-12 00:12:34 +0000972 if (log)
Sean Callananfb3e4302011-10-29 19:50:43 +0000973 log->Printf(" CEDM::FEVD[%u] Searching the root namespace", current_id);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000974
Sean Callanan503aa522011-10-12 00:12:34 +0000975 FindExternalVisibleDecls(context,
976 lldb::ModuleSP(),
977 namespace_decl,
Sean Callanan7dd98122011-10-14 20:34:21 +0000978 current_id);
Sean Callanan503aa522011-10-12 00:12:34 +0000979 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000980
Sean Callananfb3e4302011-10-29 19:50:43 +0000981 if (!context.m_found.variable)
982 ClangASTSource::FindExternalVisibleDecls(context);
Sean Callanan503aa522011-10-12 00:12:34 +0000983}
984
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000985void
986ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context,
Sean Callanan1a2c5382011-10-12 18:44:30 +0000987 lldb::ModuleSP module_sp,
Sean Callanan88972242011-10-12 01:39:28 +0000988 ClangNamespaceDecl &namespace_decl,
Sean Callanan7dd98122011-10-14 20:34:21 +0000989 unsigned int current_id)
Sean Callanan503aa522011-10-12 00:12:34 +0000990{
Sean Callananeddeb3b2011-10-28 23:38:38 +0000991 assert (m_ast_context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000992
Greg Clayton5160ce52013-03-27 23:08:40 +0000993 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000994
Greg Clayton7a345282010-11-09 23:46:37 +0000995 SymbolContextList sc_list;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000996
Sean Callananfb3e4302011-10-29 19:50:43 +0000997 const ConstString name(context.m_decl_name.getAsString().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000998
Greg Clayton83c5cd92010-11-14 22:13:40 +0000999 const char *name_unique_cstr = name.GetCString();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001000
Greg Clayton83c5cd92010-11-14 22:13:40 +00001001 if (name_unique_cstr == NULL)
1002 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001003
Sean Callanan5b26f272012-02-04 08:49:35 +00001004 static ConstString id_name("id");
1005 static ConstString Class_name("Class");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001006
Sean Callanan5b26f272012-02-04 08:49:35 +00001007 if (name == id_name || name == Class_name)
1008 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001009
1010 // Only look for functions by name out in our symbols if the function
Greg Clayton7b462cc2010-10-15 22:48:33 +00001011 // doesn't start with our phony prefix of '$'
Sean Callanan933693b2012-02-10 01:22:05 +00001012 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Jason Molendab57e4a12013-11-04 09:33:30 +00001013 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
Sean Callanan88972242011-10-12 01:39:28 +00001014 if (name_unique_cstr[0] == '$' && !namespace_decl)
1015 {
1016 static ConstString g_lldb_class_name ("$__lldb_class");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001017
Sean Callanan88972242011-10-12 01:39:28 +00001018 if (name == g_lldb_class_name)
1019 {
1020 // Clang is looking for the type of "this"
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001021
Sean Callananc03bdd92012-02-08 03:45:08 +00001022 if (frame == NULL)
Sean Callanan88972242011-10-12 01:39:28 +00001023 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001024
Sean Callananc03bdd92012-02-08 03:45:08 +00001025 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001026
Sean Callananc03bdd92012-02-08 03:45:08 +00001027 if (!sym_ctx.function)
Sean Callanan88972242011-10-12 01:39:28 +00001028 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001029
Greg Clayton685c88c2012-07-14 00:53:55 +00001030 // Get the block that defines the function
1031 Block *function_block = sym_ctx.GetFunctionBlock();
1032
1033 if (!function_block)
1034 return;
1035
1036 clang::DeclContext *decl_context = function_block->GetClangDeclContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001037
Sean Callananc03bdd92012-02-08 03:45:08 +00001038 if (!decl_context)
Sean Callanan88972242011-10-12 01:39:28 +00001039 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001040
Sean Callananc03bdd92012-02-08 03:45:08 +00001041 clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001042
Jim Ingham5fdeed42012-10-30 23:35:54 +00001043 if (method_decl)
Sean Callanan88972242011-10-12 01:39:28 +00001044 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001045 clang::CXXRecordDecl *class_decl = method_decl->getParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001046
Jim Ingham5fdeed42012-10-30 23:35:54 +00001047 QualType class_qual_type(class_decl->getTypeForDecl(), 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001048
Jim Ingham5fdeed42012-10-30 23:35:54 +00001049 TypeFromUser class_user_type (class_qual_type.getAsOpaquePtr(),
1050 &class_decl->getASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001051
Jim Ingham5fdeed42012-10-30 23:35:54 +00001052 if (log)
1053 {
1054 ASTDumper ast_dumper(class_qual_type);
1055 log->Printf(" CEDM::FEVD[%u] Adding type for $__lldb_class: %s", current_id, ast_dumper.GetCString());
1056 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001057
Sean Callananfa4fab72013-02-01 06:55:48 +00001058 TypeFromParser class_type = CopyClassType(class_user_type, current_id);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001059
Sean Callananfa4fab72013-02-01 06:55:48 +00001060 if (!class_type.IsValid())
1061 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001062
Sean Callanan34cf8202013-03-12 21:22:00 +00001063 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(QualType::getFromOpaquePtr(class_type.GetOpaqueQualType()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001064
Sean Callananfa4fab72013-02-01 06:55:48 +00001065 if (!type_source_info)
1066 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001067
Sean Callananfa4fab72013-02-01 06:55:48 +00001068 TypedefDecl *typedef_decl = TypedefDecl::Create(*m_ast_context,
1069 m_ast_context->getTranslationUnitDecl(),
1070 SourceLocation(),
1071 SourceLocation(),
1072 context.m_decl_name.getAsIdentifierInfo(),
1073 type_source_info);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001074
1075
Sean Callananfa4fab72013-02-01 06:55:48 +00001076 if (!typedef_decl)
1077 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001078
Sean Callananfa4fab72013-02-01 06:55:48 +00001079 context.AddNamedDecl(typedef_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001080
Jim Ingham5fdeed42012-10-30 23:35:54 +00001081 if (method_decl->isInstance())
1082 {
1083 // self is a pointer to the object
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001084
Jim Ingham5fdeed42012-10-30 23:35:54 +00001085 QualType class_pointer_type = method_decl->getASTContext().getPointerType(class_qual_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001086
Jim Ingham5fdeed42012-10-30 23:35:54 +00001087 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
1088 &method_decl->getASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001089
Jim Ingham5fdeed42012-10-30 23:35:54 +00001090 m_struct_vars->m_object_pointer_type = self_user_type;
1091 }
Sean Callanan88972242011-10-12 01:39:28 +00001092 }
Jim Ingham5fdeed42012-10-30 23:35:54 +00001093 else
Sean Callananbb6e7ed2012-03-05 22:08:20 +00001094 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001095 // This branch will get hit if we are executing code in the context of a function that
1096 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a
1097 // method of the class. In that case, just look up the "this" variable in the the current
1098 // scope and use its type.
1099 // FIXME: This code is formally correct, but clang doesn't currently emit DW_AT_object_pointer
1100 // for C++ so it hasn't actually been tested.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001101
Jim Ingham5fdeed42012-10-30 23:35:54 +00001102 VariableList *vars = frame->GetVariableList(false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001103
Jim Ingham5fdeed42012-10-30 23:35:54 +00001104 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001105
Jim Ingham5fdeed42012-10-30 23:35:54 +00001106 if (this_var &&
1107 this_var->IsInScope(frame) &&
1108 this_var->LocationIsValidForFrame (frame))
1109 {
1110 Type *this_type = this_var->GetType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001111
Jim Ingham5fdeed42012-10-30 23:35:54 +00001112 if (!this_type)
1113 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001114
Greg Clayton57ee3062013-07-11 22:46:58 +00001115 ClangASTType pointee_type = this_type->GetClangForwardType().GetPointeeType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001116
Greg Clayton57ee3062013-07-11 22:46:58 +00001117 if (pointee_type.IsValid())
Jim Ingham5fdeed42012-10-30 23:35:54 +00001118 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001119 if (log)
1120 {
1121 ASTDumper ast_dumper(this_type->GetClangFullType());
1122 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1123 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001124
Greg Clayton57ee3062013-07-11 22:46:58 +00001125 TypeFromUser class_user_type(pointee_type);
Sean Callananfa4fab72013-02-01 06:55:48 +00001126 AddOneType(context, class_user_type, current_id);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001127
1128
Greg Clayton57ee3062013-07-11 22:46:58 +00001129 TypeFromUser this_user_type(this_type->GetClangFullType());
Jim Ingham5fdeed42012-10-30 23:35:54 +00001130 m_struct_vars->m_object_pointer_type = this_user_type;
1131 return;
1132 }
1133 }
Sean Callananbb6e7ed2012-03-05 22:08:20 +00001134 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001135
Sean Callanan88972242011-10-12 01:39:28 +00001136 return;
1137 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001138
Sean Callanan88972242011-10-12 01:39:28 +00001139 static ConstString g_lldb_objc_class_name ("$__lldb_objc_class");
1140 if (name == g_lldb_objc_class_name)
1141 {
1142 // Clang is looking for the type of "*self"
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001143
Sean Callanan88972242011-10-12 01:39:28 +00001144 if (!frame)
1145 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001146
Sean Callanand5c17ed2011-11-15 02:11:17 +00001147 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001148
Sean Callanand5c17ed2011-11-15 02:11:17 +00001149 if (!sym_ctx.function)
1150 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001151
Greg Clayton685c88c2012-07-14 00:53:55 +00001152 // Get the block that defines the function
1153 Block *function_block = sym_ctx.GetFunctionBlock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001154
Greg Clayton685c88c2012-07-14 00:53:55 +00001155 if (!function_block)
1156 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001157
Greg Clayton685c88c2012-07-14 00:53:55 +00001158 clang::DeclContext *decl_context = function_block->GetClangDeclContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001159
Sean Callanand5c17ed2011-11-15 02:11:17 +00001160 if (!decl_context)
1161 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001162
Sean Callanand5c17ed2011-11-15 02:11:17 +00001163 clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001164
Jim Ingham5fdeed42012-10-30 23:35:54 +00001165 if (method_decl)
Sean Callanand5c17ed2011-11-15 02:11:17 +00001166 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001167 ObjCInterfaceDecl* self_interface = method_decl->getClassInterface();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001168
Jim Ingham5fdeed42012-10-30 23:35:54 +00001169 if (!self_interface)
Sean Callananc03bdd92012-02-08 03:45:08 +00001170 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001171
Jim Ingham5fdeed42012-10-30 23:35:54 +00001172 const clang::Type *interface_type = self_interface->getTypeForDecl();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001173
Sean Callanandfb87d62013-04-09 21:30:48 +00001174 if (!interface_type)
1175 return; // This is unlikely, but we have seen crashes where this occurred
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001176
Jim Ingham5fdeed42012-10-30 23:35:54 +00001177 TypeFromUser class_user_type(QualType(interface_type, 0).getAsOpaquePtr(),
1178 &method_decl->getASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001179
Jim Ingham5fdeed42012-10-30 23:35:54 +00001180 if (log)
1181 {
1182 ASTDumper ast_dumper(interface_type);
1183 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1184 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001185
Sean Callananfa4fab72013-02-01 06:55:48 +00001186 AddOneType(context, class_user_type, current_id);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001187
Jim Ingham5fdeed42012-10-30 23:35:54 +00001188 if (method_decl->isInstanceMethod())
1189 {
1190 // self is a pointer to the object
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001191
Jim Ingham5fdeed42012-10-30 23:35:54 +00001192 QualType class_pointer_type = method_decl->getASTContext().getObjCObjectPointerType(QualType(interface_type, 0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001193
Jim Ingham5fdeed42012-10-30 23:35:54 +00001194 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
1195 &method_decl->getASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001196
Jim Ingham5fdeed42012-10-30 23:35:54 +00001197 m_struct_vars->m_object_pointer_type = self_user_type;
1198 }
1199 else
1200 {
1201 // self is a Class pointer
1202 QualType class_type = method_decl->getASTContext().getObjCClassType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001203
Jim Ingham5fdeed42012-10-30 23:35:54 +00001204 TypeFromUser self_user_type(class_type.getAsOpaquePtr(),
1205 &method_decl->getASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001206
Jim Ingham5fdeed42012-10-30 23:35:54 +00001207 m_struct_vars->m_object_pointer_type = self_user_type;
1208 }
1209
1210 return;
Sean Callananc03bdd92012-02-08 03:45:08 +00001211 }
1212 else
1213 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001214 // This branch will get hit if we are executing code in the context of a function that
1215 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a
1216 // method of the class. In that case, just look up the "self" variable in the the current
1217 // scope and use its type.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001218
Jim Ingham5fdeed42012-10-30 23:35:54 +00001219 VariableList *vars = frame->GetVariableList(false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001220
Jim Ingham5fdeed42012-10-30 23:35:54 +00001221 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001222
Jim Ingham5fdeed42012-10-30 23:35:54 +00001223 if (self_var &&
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001224 self_var->IsInScope(frame) &&
Jim Ingham5fdeed42012-10-30 23:35:54 +00001225 self_var->LocationIsValidForFrame (frame))
1226 {
1227 Type *self_type = self_var->GetType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001228
Jim Ingham5fdeed42012-10-30 23:35:54 +00001229 if (!self_type)
1230 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001231
Greg Clayton57ee3062013-07-11 22:46:58 +00001232 ClangASTType self_clang_type = self_type->GetClangFullType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001233
Greg Clayton57ee3062013-07-11 22:46:58 +00001234 if (self_clang_type.IsObjCClassType())
Jim Ingham5fdeed42012-10-30 23:35:54 +00001235 {
Sean Callanana2868d42013-01-19 01:49:02 +00001236 return;
1237 }
Greg Clayton57ee3062013-07-11 22:46:58 +00001238 else if (self_clang_type.IsObjCObjectPointerType())
Sean Callanana2868d42013-01-19 01:49:02 +00001239 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001240 self_clang_type = self_clang_type.GetPointeeType();
1241
1242 if (!self_clang_type)
Sean Callanane5270592013-04-26 22:54:19 +00001243 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001244
Jim Ingham5fdeed42012-10-30 23:35:54 +00001245 if (log)
1246 {
1247 ASTDumper ast_dumper(self_type->GetClangFullType());
1248 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1249 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001250
Greg Clayton57ee3062013-07-11 22:46:58 +00001251 TypeFromUser class_user_type (self_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001252
Sean Callananfa4fab72013-02-01 06:55:48 +00001253 AddOneType(context, class_user_type, current_id);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001254
Greg Clayton57ee3062013-07-11 22:46:58 +00001255 TypeFromUser self_user_type(self_type->GetClangFullType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001256
Jim Ingham5fdeed42012-10-30 23:35:54 +00001257 m_struct_vars->m_object_pointer_type = self_user_type;
1258 return;
1259 }
1260 }
Sean Callananc03bdd92012-02-08 03:45:08 +00001261 }
1262
Sean Callanan88972242011-10-12 01:39:28 +00001263 return;
1264 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001265
Sean Callanan88972242011-10-12 01:39:28 +00001266 // any other $__lldb names should be weeded out now
1267 if (!::strncmp(name_unique_cstr, "$__lldb", sizeof("$__lldb") - 1))
1268 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001269
Sean Callanan88972242011-10-12 01:39:28 +00001270 do
1271 {
1272 if (!target)
1273 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001274
Sean Callanan88972242011-10-12 01:39:28 +00001275 ClangASTContext *scratch_clang_ast_context = target->GetScratchClangASTContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001276
Sean Callanan88972242011-10-12 01:39:28 +00001277 if (!scratch_clang_ast_context)
1278 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001279
Sean Callanan88972242011-10-12 01:39:28 +00001280 ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001281
Sean Callanan88972242011-10-12 01:39:28 +00001282 if (!scratch_ast_context)
1283 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001284
Sean Callanan88972242011-10-12 01:39:28 +00001285 TypeDecl *ptype_type_decl = m_parser_vars->m_persistent_vars->GetPersistentType(name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001286
Sean Callanan88972242011-10-12 01:39:28 +00001287 if (!ptype_type_decl)
1288 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001289
Sean Callanan0eed0d42011-12-06 03:41:14 +00001290 Decl *parser_ptype_decl = m_ast_importer->CopyDecl(m_ast_context, scratch_ast_context, ptype_type_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001291
Sean Callanan88972242011-10-12 01:39:28 +00001292 if (!parser_ptype_decl)
1293 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001294
Sean Callanan88972242011-10-12 01:39:28 +00001295 TypeDecl *parser_ptype_type_decl = dyn_cast<TypeDecl>(parser_ptype_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001296
Sean Callanan88972242011-10-12 01:39:28 +00001297 if (!parser_ptype_type_decl)
1298 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001299
Sean Callanan88972242011-10-12 01:39:28 +00001300 if (log)
Sean Callananfb3e4302011-10-29 19:50:43 +00001301 log->Printf(" CEDM::FEVD[%u] Found persistent type %s", current_id, name.GetCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001302
Sean Callanan88972242011-10-12 01:39:28 +00001303 context.AddNamedDecl(parser_ptype_type_decl);
1304 } while (0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001305
Sean Callanan88972242011-10-12 01:39:28 +00001306 ClangExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariable(name));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001307
Sean Callanan88972242011-10-12 01:39:28 +00001308 if (pvar_sp)
1309 {
Sean Callanan7dd98122011-10-14 20:34:21 +00001310 AddOneVariable(context, pvar_sp, current_id);
Sean Callanan88972242011-10-12 01:39:28 +00001311 return;
1312 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001313
Sean Callanan88972242011-10-12 01:39:28 +00001314 const char *reg_name(&name.GetCString()[1]);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001315
Sean Callanan933693b2012-02-10 01:22:05 +00001316 if (m_parser_vars->m_exe_ctx.GetRegisterContext())
Sean Callanan88972242011-10-12 01:39:28 +00001317 {
Sean Callanan933693b2012-02-10 01:22:05 +00001318 const RegisterInfo *reg_info(m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(reg_name));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001319
Sean Callanan88972242011-10-12 01:39:28 +00001320 if (reg_info)
Sean Callanan35c7f982011-10-18 16:46:55 +00001321 {
1322 if (log)
Sean Callananfb3e4302011-10-29 19:50:43 +00001323 log->Printf(" CEDM::FEVD[%u] Found register %s", current_id, reg_info->name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001324
Sean Callanan7dd98122011-10-14 20:34:21 +00001325 AddOneRegister(context, reg_info, current_id);
Sean Callanan35c7f982011-10-18 16:46:55 +00001326 }
Sean Callanan88972242011-10-12 01:39:28 +00001327 }
1328 }
1329 else
Sean Callanan8ade1042010-07-27 00:55:47 +00001330 {
Sean Callanan69b53412011-08-06 00:28:14 +00001331 ValueObjectSP valobj;
1332 VariableSP var;
1333 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001334
Sean Callanan88972242011-10-12 01:39:28 +00001335 if (frame && !namespace_decl)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001336 {
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001337 valobj = frame->GetValueForVariableExpressionPath(name_unique_cstr,
1338 eNoDynamicValues,
Jason Molenda0060a382014-10-16 08:15:11 +00001339 StackFrame::eExpressionPathOptionCheckPtrVsMember |
Jason Molenda0060a382014-10-16 08:15:11 +00001340 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1341 StackFrame::eExpressionPathOptionsNoSyntheticChildren |
Jason Molendab57e4a12013-11-04 09:33:30 +00001342 StackFrame::eExpressionPathOptionsNoSyntheticArrayRange,
Sean Callanan503aa522011-10-12 00:12:34 +00001343 var,
1344 err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001345
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001346 // If we found a variable in scope, no need to pull up function names
Sean Callanan9a028512012-08-09 00:50:26 +00001347 if (err.Success() && var)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001348 {
Sean Callanan0712f462011-11-29 22:03:21 +00001349 AddOneVariable(context, var, valobj, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001350 context.m_found.variable = true;
Sean Callananefa7d1f2011-10-25 20:36:57 +00001351 return;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001352 }
Greg Clayton7b462cc2010-10-15 22:48:33 +00001353 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001354
Sean Callananfd1ba912011-12-10 04:03:38 +00001355 if (target)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001356 {
Sean Callanan1fd3f4f2011-10-12 16:59:31 +00001357 var = FindGlobalVariable (*target,
Sean Callanan1a2c5382011-10-12 18:44:30 +00001358 module_sp,
Sean Callanan1fd3f4f2011-10-12 16:59:31 +00001359 name,
1360 &namespace_decl,
Greg Claytonc14ee322011-09-22 04:58:26 +00001361 NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001362
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001363 if (var)
1364 {
Sean Callanan9b3569b2011-12-10 03:12:34 +00001365 valobj = ValueObjectVariable::Create(target, var);
Sean Callanan0712f462011-11-29 22:03:21 +00001366 AddOneVariable(context, var, valobj, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001367 context.m_found.variable = true;
Sean Callananfd1ba912011-12-10 04:03:38 +00001368 return;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001369 }
1370 }
Sean Callananc8278af2014-12-05 01:27:35 +00001371
1372 std::vector<clang::NamedDecl *> decls_from_modules;
1373
1374 if (target)
1375 {
1376 if (ClangModulesDeclVendor *decl_vendor = target->GetClangModulesDeclVendor())
1377 {
1378 decl_vendor->FindDecls(name, false, UINT32_MAX, decls_from_modules);
1379 }
1380 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001381
Sean Callananca4e0fd2011-10-12 20:29:25 +00001382 if (!context.m_found.variable)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001383 {
Sean Callanan9df05fb2012-02-10 22:52:19 +00001384 const bool include_inlines = false;
Greg Clayton931180e2011-01-27 06:44:37 +00001385 const bool append = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001386
Sean Callanan1a2c5382011-10-12 18:44:30 +00001387 if (namespace_decl && module_sp)
Sean Callanan25ea08e2011-10-12 17:38:09 +00001388 {
Sean Callanan3ae61792012-02-15 17:14:49 +00001389 const bool include_symbols = false;
1390
Sean Callanan1a2c5382011-10-12 18:44:30 +00001391 module_sp->FindFunctions(name,
1392 &namespace_decl,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001393 eFunctionNameTypeBase,
Sean Callanan1a2c5382011-10-12 18:44:30 +00001394 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001395 include_inlines,
Sean Callanan1a2c5382011-10-12 18:44:30 +00001396 append,
1397 sc_list);
Sean Callanan25ea08e2011-10-12 17:38:09 +00001398 }
Jim Ingham28eb5712012-10-12 17:34:26 +00001399 else if (target && !namespace_decl)
Sean Callanan25ea08e2011-10-12 17:38:09 +00001400 {
Sean Callanan3ae61792012-02-15 17:14:49 +00001401 const bool include_symbols = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001402
Sean Callanan308a3c52012-07-28 00:21:01 +00001403 // TODO Fix FindFunctions so that it doesn't return
1404 // instance methods for eFunctionNameTypeBase.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001405
Sean Callanan25ea08e2011-10-12 17:38:09 +00001406 target->GetImages().FindFunctions(name,
Greg Clayton43fe2172013-04-03 02:00:15 +00001407 eFunctionNameTypeFull,
Sean Callanan25ea08e2011-10-12 17:38:09 +00001408 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001409 include_inlines,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001410 append,
Sean Callanan25ea08e2011-10-12 17:38:09 +00001411 sc_list);
1412 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001413
Sean Callanand9ca42a2011-05-08 02:21:26 +00001414 if (sc_list.GetSize())
Greg Clayton7b462cc2010-10-15 22:48:33 +00001415 {
Greg Claytondc25a0b2013-10-31 16:59:47 +00001416 Symbol *extern_symbol = NULL;
1417 Symbol *non_extern_symbol = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001418
Sean Callanand9ca42a2011-05-08 02:21:26 +00001419 for (uint32_t index = 0, num_indices = sc_list.GetSize();
1420 index < num_indices;
1421 ++index)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001422 {
Sean Callanand9ca42a2011-05-08 02:21:26 +00001423 SymbolContext sym_ctx;
1424 sc_list.GetContextAtIndex(index, sym_ctx);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001425
Sean Callanand9ca42a2011-05-08 02:21:26 +00001426 if (sym_ctx.function)
1427 {
Sean Callanan308a3c52012-07-28 00:21:01 +00001428 clang::DeclContext *decl_ctx = sym_ctx.function->GetClangDeclContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001429
Sean Callanan956dca92013-04-27 01:57:44 +00001430 if (!decl_ctx)
1431 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001432
Sean Callanan308a3c52012-07-28 00:21:01 +00001433 // Filter out class/instance methods.
1434 if (dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
1435 continue;
1436 if (dyn_cast<clang::CXXMethodDecl>(decl_ctx))
1437 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001438
Sean Callanan485f7322013-04-24 00:34:41 +00001439 AddOneFunction(context, sym_ctx.function, NULL, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001440 context.m_found.function_with_type_info = true;
1441 context.m_found.function = true;
Sean Callanand9ca42a2011-05-08 02:21:26 +00001442 }
1443 else if (sym_ctx.symbol)
1444 {
Greg Claytonfc56a012014-06-13 21:57:58 +00001445 if (sym_ctx.symbol->GetType() == eSymbolTypeReExported && target)
Greg Claytondc25a0b2013-10-31 16:59:47 +00001446 {
1447 sym_ctx.symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target);
1448 if (sym_ctx.symbol == NULL)
1449 continue;
1450 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001451
Greg Claytondc25a0b2013-10-31 16:59:47 +00001452 if (sym_ctx.symbol->IsExternal())
1453 extern_symbol = sym_ctx.symbol;
Sean Callanand9ca42a2011-05-08 02:21:26 +00001454 else
Greg Claytondc25a0b2013-10-31 16:59:47 +00001455 non_extern_symbol = sym_ctx.symbol;
Sean Callanand9ca42a2011-05-08 02:21:26 +00001456 }
Greg Clayton7b462cc2010-10-15 22:48:33 +00001457 }
Sean Callananc8278af2014-12-05 01:27:35 +00001458
1459 if (!context.m_found.function_with_type_info)
1460 {
1461 for (clang::NamedDecl *decl : decls_from_modules)
1462 {
1463 if (llvm::isa<clang::FunctionDecl>(decl))
1464 {
1465 clang::NamedDecl *copied_decl = llvm::cast<FunctionDecl>(m_ast_importer->CopyDecl(m_ast_context, &decl->getASTContext(), decl));
1466 context.AddNamedDecl(copied_decl);
1467 context.m_found.function_with_type_info = true;
1468 }
1469 }
1470 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001471
Sean Callananca4e0fd2011-10-12 20:29:25 +00001472 if (!context.m_found.function_with_type_info)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001473 {
Greg Claytondc25a0b2013-10-31 16:59:47 +00001474 if (extern_symbol)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001475 {
Greg Claytondc25a0b2013-10-31 16:59:47 +00001476 AddOneFunction (context, NULL, extern_symbol, current_id);
1477 context.m_found.function = true;
1478 }
1479 else if (non_extern_symbol)
1480 {
1481 AddOneFunction (context, NULL, non_extern_symbol, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001482 context.m_found.function = true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001483 }
Sean Callanand9ca42a2011-05-08 02:21:26 +00001484 }
Greg Clayton7b462cc2010-10-15 22:48:33 +00001485 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001486
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001487 if (target && !context.m_found.variable && !namespace_decl)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001488 {
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001489 // We couldn't find a non-symbol variable for this. Now we'll hunt for a generic
Sean Callanand9ca42a2011-05-08 02:21:26 +00001490 // data symbol, and -- if it is found -- treat it as a variable.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001491
Greg Clayton3d51b9f2012-11-27 01:52:16 +00001492 const Symbol *data_symbol = FindGlobalDataSymbol(*target, name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001493
Sean Callanand9ca42a2011-05-08 02:21:26 +00001494 if (data_symbol)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001495 {
Sean Callanan92cdbc82014-02-19 23:37:25 +00001496 std::string warning("got name from symbols: ");
1497 warning.append(name.AsCString());
1498 const unsigned diag_id = m_ast_context->getDiagnostics().getCustomDiagID(clang::DiagnosticsEngine::Level::Warning, "%0");
1499 m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str();
Sean Callanan7dd98122011-10-14 20:34:21 +00001500 AddOneGenericVariable(context, *data_symbol, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001501 context.m_found.variable = true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001502 }
Greg Clayton526e5af2010-11-13 03:52:47 +00001503 }
Sean Callanan3883b5a2010-09-07 21:49:41 +00001504 }
Sean Callananfb40b0d2011-10-27 02:06:03 +00001505 }
Sean Callanan6b4067c2010-07-17 00:43:37 +00001506}
Greg Claytona2721472011-06-25 00:44:06 +00001507
Saleem Abdulrasoola68f7b62014-03-20 06:08:36 +00001508//static clang_type_t
1509//MaybePromoteToBlockPointerType
1510//(
1511// ASTContext *ast_context,
1512// clang_type_t candidate_type
1513//)
1514//{
1515// if (!candidate_type)
1516// return candidate_type;
1517//
1518// QualType candidate_qual_type = QualType::getFromOpaquePtr(candidate_type);
1519//
1520// const PointerType *candidate_pointer_type = dyn_cast<PointerType>(candidate_qual_type);
1521//
1522// if (!candidate_pointer_type)
1523// return candidate_type;
1524//
1525// QualType pointee_qual_type = candidate_pointer_type->getPointeeType();
1526//
1527// const RecordType *pointee_record_type = dyn_cast<RecordType>(pointee_qual_type);
1528//
1529// if (!pointee_record_type)
1530// return candidate_type;
1531//
1532// RecordDecl *pointee_record_decl = pointee_record_type->getDecl();
1533//
1534// if (!pointee_record_decl->isRecord())
1535// return candidate_type;
1536//
1537// if (!pointee_record_decl->getName().startswith(llvm::StringRef("__block_literal_")))
1538// return candidate_type;
1539//
1540// QualType generic_function_type = ast_context->getFunctionNoProtoType(ast_context->UnknownAnyTy);
1541// QualType block_pointer_type = ast_context->getBlockPointerType(generic_function_type);
1542//
1543// return block_pointer_type.getAsOpaquePtr();
1544//}
Sean Callanan75383bf2012-03-06 21:56:33 +00001545
Greg Clayton57ee3062013-07-11 22:46:58 +00001546bool
1547ClangExpressionDeclMap::GetVariableValue (VariableSP &var,
1548 lldb_private::Value &var_location,
1549 TypeFromUser *user_type,
1550 TypeFromParser *parser_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001551{
Greg Clayton5160ce52013-03-27 23:08:40 +00001552 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001553
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001554 Type *var_type = var->GetType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001555
1556 if (!var_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001557 {
Sean Callananea22d422010-07-16 00:09:46 +00001558 if (log)
1559 log->PutCString("Skipped a definition because it has no type");
Greg Clayton57ee3062013-07-11 22:46:58 +00001560 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001561 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001562
Greg Clayton57ee3062013-07-11 22:46:58 +00001563 ClangASTType var_clang_type = var_type->GetClangFullType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001564
Greg Clayton57ee3062013-07-11 22:46:58 +00001565 if (!var_clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001566 {
Sean Callananea22d422010-07-16 00:09:46 +00001567 if (log)
1568 log->PutCString("Skipped a definition because it has no Clang type");
Greg Clayton57ee3062013-07-11 22:46:58 +00001569 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001570 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001571
Sean Callanancc427fa2011-07-30 02:42:06 +00001572 ASTContext *ast = var_type->GetClangASTContext().getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001573
Greg Clayton6beaaa62011-01-17 03:46:26 +00001574 if (!ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001575 {
Sean Callananea22d422010-07-16 00:09:46 +00001576 if (log)
1577 log->PutCString("There is no AST context for the current execution context");
Greg Clayton57ee3062013-07-11 22:46:58 +00001578 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001579 }
Greg Clayton57ee3062013-07-11 22:46:58 +00001580 //var_clang_type = MaybePromoteToBlockPointerType (ast, var_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001581
Sean Callanan6b4067c2010-07-17 00:43:37 +00001582 DWARFExpression &var_location_expr = var->LocationExpression();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001583
Sean Callanan933693b2012-02-10 01:22:05 +00001584 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001585 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001586
Sean Callanan7b5805d2013-01-18 21:20:51 +00001587 if (var->GetLocationIsConstantValueData())
1588 {
1589 DataExtractor const_value_extractor;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001590
Sean Callanan7b5805d2013-01-18 21:20:51 +00001591 if (var_location_expr.GetExpressionData(const_value_extractor))
1592 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001593 var_location = Value(const_value_extractor.GetDataStart(), const_value_extractor.GetByteSize());
1594 var_location.SetValueType(Value::eValueTypeHostAddress);
Sean Callanan7b5805d2013-01-18 21:20:51 +00001595 }
1596 else
1597 {
1598 if (log)
1599 log->Printf("Error evaluating constant variable: %s", err.AsCString());
Greg Clayton57ee3062013-07-11 22:46:58 +00001600 return false;
Sean Callanan7b5805d2013-01-18 21:20:51 +00001601 }
1602 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001603
Greg Clayton57ee3062013-07-11 22:46:58 +00001604 ClangASTType type_to_use = GuardedCopyType(var_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001605
Greg Clayton57ee3062013-07-11 22:46:58 +00001606 if (!type_to_use)
Sean Callanan1d180662010-07-20 23:31:16 +00001607 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001608 if (log)
1609 log->Printf("Couldn't copy a variable's type into the parser's AST context");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001610
Greg Clayton57ee3062013-07-11 22:46:58 +00001611 return false;
Sean Callanan1d180662010-07-20 23:31:16 +00001612 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001613
Greg Clayton57ee3062013-07-11 22:46:58 +00001614 if (parser_type)
1615 *parser_type = TypeFromParser(type_to_use);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001616
Greg Clayton57ee3062013-07-11 22:46:58 +00001617 if (var_location.GetContextType() == Value::eContextTypeInvalid)
1618 var_location.SetClangType(type_to_use);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001619
Greg Clayton57ee3062013-07-11 22:46:58 +00001620 if (var_location.GetValueType() == Value::eValueTypeFileAddress)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001621 {
1622 SymbolContext var_sc;
1623 var->CalculateSymbolContext(&var_sc);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001624
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001625 if (!var_sc.module_sp)
Michael Sartain89c862f2013-08-07 19:05:15 +00001626 return false;
Greg Clayton3046e662013-07-10 01:23:25 +00001627
Greg Clayton57ee3062013-07-11 22:46:58 +00001628 Address so_addr(var_location.GetScalar().ULongLong(), var_sc.module_sp->GetSectionList());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001629
Greg Claytonc14ee322011-09-22 04:58:26 +00001630 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001631
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001632 if (load_addr != LLDB_INVALID_ADDRESS)
1633 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001634 var_location.GetScalar() = load_addr;
1635 var_location.SetValueType(Value::eValueTypeLoadAddress);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001636 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001637 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001638
Sean Callanan1d180662010-07-20 23:31:16 +00001639 if (user_type)
Greg Clayton57ee3062013-07-11 22:46:58 +00001640 *user_type = TypeFromUser(var_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001641
Greg Clayton57ee3062013-07-11 22:46:58 +00001642 return true;
Sean Callanan6b4067c2010-07-17 00:43:37 +00001643}
1644
1645void
Sean Callanan0712f462011-11-29 22:03:21 +00001646ClangExpressionDeclMap::AddOneVariable (NameSearchContext &context, VariableSP var, ValueObjectSP valobj, unsigned int current_id)
Sean Callanan6b4067c2010-07-17 00:43:37 +00001647{
Sean Callanan979f74d2010-12-03 01:38:59 +00001648 assert (m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001649
Greg Clayton5160ce52013-03-27 23:08:40 +00001650 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001651
Sean Callanan1d180662010-07-20 23:31:16 +00001652 TypeFromUser ut;
1653 TypeFromParser pt;
Greg Clayton57ee3062013-07-11 22:46:58 +00001654 Value var_location;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001655
Greg Clayton57ee3062013-07-11 22:46:58 +00001656 if (!GetVariableValue (var, var_location, &ut, &pt))
1657 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001658
Sean Callanancf128622012-03-15 01:53:17 +00001659 clang::QualType parser_opaque_type = QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001660
Sean Callanane2d47482012-03-21 17:13:20 +00001661 if (parser_opaque_type.isNull())
1662 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001663
Sean Callanancf128622012-03-15 01:53:17 +00001664 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr())
1665 {
1666 if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1667 CompleteType(tag_type->getDecl());
Sean Callananf4c0a222013-12-20 04:09:05 +00001668 if (const ObjCObjectPointerType *objc_object_ptr_type = dyn_cast<ObjCObjectPointerType>(parser_type))
1669 CompleteType(objc_object_ptr_type->getInterfaceDecl());
Sean Callanancf128622012-03-15 01:53:17 +00001670 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001671
1672
Greg Clayton57ee3062013-07-11 22:46:58 +00001673 bool is_reference = pt.IsReferenceType();
Sean Callanan98298012011-10-27 19:41:13 +00001674
Greg Clayton57ee3062013-07-11 22:46:58 +00001675 NamedDecl *var_decl = NULL;
Sean Callanan98298012011-10-27 19:41:13 +00001676 if (is_reference)
Greg Clayton57ee3062013-07-11 22:46:58 +00001677 var_decl = context.AddVarDecl(pt);
Sean Callanan98298012011-10-27 19:41:13 +00001678 else
Greg Clayton57ee3062013-07-11 22:46:58 +00001679 var_decl = context.AddVarDecl(pt.GetLValueReferenceType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001680
Greg Clayton7b462cc2010-10-15 22:48:33 +00001681 std::string decl_name(context.m_decl_name.getAsString());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001682 ConstString entity_name(decl_name.c_str());
Sean Callanan0712f462011-11-29 22:03:21 +00001683 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (valobj));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001684
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001685 assert (entity.get());
Sean Callanan3c495c12013-01-15 23:29:36 +00001686 entity->EnableParserVars(GetParserID());
1687 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1688 parser_vars->m_parser_type = pt;
1689 parser_vars->m_named_decl = var_decl;
1690 parser_vars->m_llvm_value = NULL;
1691 parser_vars->m_lldb_value = var_location;
1692 parser_vars->m_lldb_var = var;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001693
Sean Callanan98298012011-10-27 19:41:13 +00001694 if (is_reference)
1695 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001696
Sean Callananea22d422010-07-16 00:09:46 +00001697 if (log)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001698 {
Sean Callanan82695d62011-11-07 23:32:52 +00001699 ASTDumper orig_dumper(ut.GetOpaqueQualType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001700 ASTDumper ast_dumper(var_decl);
Sean Callanan82695d62011-11-07 23:32:52 +00001701 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 +00001702 }
Sean Callanan468574b2010-06-22 23:46:24 +00001703}
1704
1705void
Sean Callanan2235f322010-08-11 03:57:18 +00001706ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001707 ClangExpressionVariableSP &pvar_sp,
Sean Callanan7dd98122011-10-14 20:34:21 +00001708 unsigned int current_id)
Sean Callanan2235f322010-08-11 03:57:18 +00001709{
Greg Clayton5160ce52013-03-27 23:08:40 +00001710 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001711
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001712 TypeFromUser user_type (pvar_sp->GetTypeFromUser());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001713
Greg Clayton57ee3062013-07-11 22:46:58 +00001714 TypeFromParser parser_type (GuardedCopyType(user_type));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001715
Sean Callanane0a64f72011-12-01 21:04:37 +00001716 if (!parser_type.GetOpaqueQualType())
1717 {
1718 if (log)
1719 log->Printf(" CEDM::FEVD[%u] Couldn't import type for pvar %s", current_id, pvar_sp->GetName().GetCString());
1720 return;
1721 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001722
Greg Clayton57ee3062013-07-11 22:46:58 +00001723 NamedDecl *var_decl = context.AddVarDecl(parser_type.GetLValueReferenceType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001724
Sean Callanan3c495c12013-01-15 23:29:36 +00001725 pvar_sp->EnableParserVars(GetParserID());
1726 ClangExpressionVariable::ParserVars *parser_vars = pvar_sp->GetParserVars(GetParserID());
1727 parser_vars->m_parser_type = parser_type;
Greg Clayton57ee3062013-07-11 22:46:58 +00001728 parser_vars->m_named_decl = var_decl;
1729 parser_vars->m_llvm_value = NULL;
1730 parser_vars->m_lldb_value.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001731
Sean Callanan823bb4c2010-08-30 22:17:16 +00001732 if (log)
Sean Callanan530782942010-11-01 23:22:47 +00001733 {
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001734 ASTDumper ast_dumper(var_decl);
Sean Callananfb3e4302011-10-29 19:50:43 +00001735 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 +00001736 }
Sean Callanan2235f322010-08-11 03:57:18 +00001737}
1738
Sean Callanan348b5892010-11-30 00:27:43 +00001739void
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001740ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context,
Greg Clayton3d51b9f2012-11-27 01:52:16 +00001741 const Symbol &symbol,
Sean Callanan7dd98122011-10-14 20:34:21 +00001742 unsigned int current_id)
Sean Callanand9ca42a2011-05-08 02:21:26 +00001743{
1744 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001745
Greg Clayton5160ce52013-03-27 23:08:40 +00001746 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001747
Sean Callanan933693b2012-02-10 01:22:05 +00001748 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00001749
1750 if (target == NULL)
1751 return;
1752
1753 ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001754
Greg Clayton57ee3062013-07-11 22:46:58 +00001755 TypeFromUser user_type (ClangASTContext::GetBasicType(scratch_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType());
1756 TypeFromParser parser_type (ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType());
1757 NamedDecl *var_decl = context.AddVarDecl(parser_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001758
Sean Callanand9ca42a2011-05-08 02:21:26 +00001759 std::string decl_name(context.m_decl_name.getAsString());
1760 ConstString entity_name(decl_name.c_str());
Sean Callanan933693b2012-02-10 01:22:05 +00001761 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (),
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001762 entity_name,
Sean Callanand9ca42a2011-05-08 02:21:26 +00001763 user_type,
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001764 m_parser_vars->m_target_info.byte_order,
1765 m_parser_vars->m_target_info.address_byte_size));
Sean Callanand9ca42a2011-05-08 02:21:26 +00001766 assert (entity.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001767
Greg Clayton57ee3062013-07-11 22:46:58 +00001768 entity->EnableParserVars(GetParserID());
1769 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1770
Greg Clayton3d51b9f2012-11-27 01:52:16 +00001771 const Address &symbol_address = symbol.GetAddress();
Greg Claytonc14ee322011-09-22 04:58:26 +00001772 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001773
Greg Clayton57ee3062013-07-11 22:46:58 +00001774 //parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType());
1775 parser_vars->m_lldb_value.SetClangType(user_type);
1776 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr;
1777 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001778
Sean Callanan3c495c12013-01-15 23:29:36 +00001779 parser_vars->m_parser_type = parser_type;
1780 parser_vars->m_named_decl = var_decl;
1781 parser_vars->m_llvm_value = NULL;
Sean Callanan3c495c12013-01-15 23:29:36 +00001782 parser_vars->m_lldb_sym = &symbol;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001783
Sean Callanand9ca42a2011-05-08 02:21:26 +00001784 if (log)
1785 {
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001786 ASTDumper ast_dumper(var_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001787
Sean Callananfb3e4302011-10-29 19:50:43 +00001788 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 +00001789 }
1790}
1791
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001792bool
Sean Callanan77502262011-05-12 23:54:16 +00001793ClangExpressionDeclMap::ResolveUnknownTypes()
1794{
Greg Clayton5160ce52013-03-27 23:08:40 +00001795 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan933693b2012-02-10 01:22:05 +00001796 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00001797
1798 ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext();
Sean Callanan77502262011-05-12 23:54:16 +00001799
1800 for (size_t index = 0, num_entities = m_found_entities.GetSize();
1801 index < num_entities;
1802 ++index)
1803 {
1804 ClangExpressionVariableSP entity = m_found_entities.GetVariableAtIndex(index);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001805
Sean Callanan3c495c12013-01-15 23:29:36 +00001806 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001807
Sean Callanan77502262011-05-12 23:54:16 +00001808 if (entity->m_flags & ClangExpressionVariable::EVUnknownType)
1809 {
Sean Callanan3c495c12013-01-15 23:29:36 +00001810 const NamedDecl *named_decl = parser_vars->m_named_decl;
Sean Callanan77502262011-05-12 23:54:16 +00001811 const VarDecl *var_decl = dyn_cast<VarDecl>(named_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001812
Sean Callanan77502262011-05-12 23:54:16 +00001813 if (!var_decl)
1814 {
1815 if (log)
1816 log->Printf("Entity of unknown type does not have a VarDecl");
1817 return false;
1818 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001819
Sean Callanan77502262011-05-12 23:54:16 +00001820 if (log)
1821 {
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001822 ASTDumper ast_dumper(const_cast<VarDecl*>(var_decl));
1823 log->Printf("Variable of unknown type now has Decl %s", ast_dumper.GetCString());
Sean Callanan77502262011-05-12 23:54:16 +00001824 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001825
Sean Callanan77502262011-05-12 23:54:16 +00001826 QualType var_type = var_decl->getType();
1827 TypeFromParser parser_type(var_type.getAsOpaquePtr(), &var_decl->getASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001828
Sean Callanan00f43622011-11-18 03:28:09 +00001829 lldb::clang_type_t copied_type = m_ast_importer->CopyType(scratch_ast_context, &var_decl->getASTContext(), var_type.getAsOpaquePtr());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001830
Sean Callanane0a64f72011-12-01 21:04:37 +00001831 if (!copied_type)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001832 {
Sean Callanane0a64f72011-12-01 21:04:37 +00001833 if (log)
1834 log->Printf("ClangExpressionDeclMap::ResolveUnknownType - Couldn't import the type for a variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001835
Jim Inghamf94e1792012-08-11 00:35:26 +00001836 return (bool) lldb::ClangExpressionVariableSP();
Sean Callanane0a64f72011-12-01 21:04:37 +00001837 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001838
Sean Callanan77502262011-05-12 23:54:16 +00001839 TypeFromUser user_type(copied_type, scratch_ast_context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001840
Greg Clayton57ee3062013-07-11 22:46:58 +00001841// parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType());
1842 parser_vars->m_lldb_value.SetClangType(user_type);
Sean Callanan3c495c12013-01-15 23:29:36 +00001843 parser_vars->m_parser_type = parser_type;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001844
Greg Clayton57ee3062013-07-11 22:46:58 +00001845 entity->SetClangType(user_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001846
Sean Callanan77502262011-05-12 23:54:16 +00001847 entity->m_flags &= ~(ClangExpressionVariable::EVUnknownType);
1848 }
1849 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001850
Sean Callanan77502262011-05-12 23:54:16 +00001851 return true;
1852}
1853
Sean Callanand9ca42a2011-05-08 02:21:26 +00001854void
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001855ClangExpressionDeclMap::AddOneRegister (NameSearchContext &context,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001856 const RegisterInfo *reg_info,
Sean Callanan7dd98122011-10-14 20:34:21 +00001857 unsigned int current_id)
Sean Callanan348b5892010-11-30 00:27:43 +00001858{
Greg Clayton5160ce52013-03-27 23:08:40 +00001859 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001860
Greg Clayton57ee3062013-07-11 22:46:58 +00001861 ClangASTType clang_type = ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (m_ast_context,
1862 reg_info->encoding,
1863 reg_info->byte_size * 8);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001864
Greg Clayton57ee3062013-07-11 22:46:58 +00001865 if (!clang_type)
Sean Callanan348b5892010-11-30 00:27:43 +00001866 {
Johnny Chene95fcf72011-08-09 22:52:27 +00001867 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +00001868 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 +00001869 return;
1870 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001871
Greg Clayton57ee3062013-07-11 22:46:58 +00001872 TypeFromParser parser_clang_type (clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001873
Greg Clayton57ee3062013-07-11 22:46:58 +00001874 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001875
Sean Callanan933693b2012-02-10 01:22:05 +00001876 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001877 m_parser_vars->m_target_info.byte_order,
1878 m_parser_vars->m_target_info.address_byte_size));
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001879 assert (entity.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001880
Sean Callanan348b5892010-11-30 00:27:43 +00001881 std::string decl_name(context.m_decl_name.getAsString());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001882 entity->SetName (ConstString (decl_name.c_str()));
1883 entity->SetRegisterInfo (reg_info);
Sean Callanan3c495c12013-01-15 23:29:36 +00001884 entity->EnableParserVars(GetParserID());
1885 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
Greg Clayton57ee3062013-07-11 22:46:58 +00001886 parser_vars->m_parser_type = parser_clang_type;
1887 parser_vars->m_named_decl = var_decl;
1888 parser_vars->m_llvm_value = NULL;
1889 parser_vars->m_lldb_value.Clear();
Sean Callananf673e762012-02-15 01:40:39 +00001890 entity->m_flags |= ClangExpressionVariable::EVBareRegister;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001891
Sean Callananf673e762012-02-15 01:40:39 +00001892 if (log)
Sean Callanan348b5892010-11-30 00:27:43 +00001893 {
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001894 ASTDumper ast_dumper(var_decl);
Sean Callananfb3e4302011-10-29 19:50:43 +00001895 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 +00001896 }
1897}
1898
Sean Callanan2235f322010-08-11 03:57:18 +00001899void
Greg Clayton3f5c08f2011-05-18 22:01:49 +00001900ClangExpressionDeclMap::AddOneFunction (NameSearchContext &context,
Greg Clayton57ee3062013-07-11 22:46:58 +00001901 Function* function,
Sean Callanan7dd98122011-10-14 20:34:21 +00001902 Symbol* symbol,
1903 unsigned int current_id)
Sean Callanan468574b2010-06-22 23:46:24 +00001904{
Sean Callanan979f74d2010-12-03 01:38:59 +00001905 assert (m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001906
Greg Clayton5160ce52013-03-27 23:08:40 +00001907 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001908
Greg Clayton57ee3062013-07-11 22:46:58 +00001909 NamedDecl *function_decl = NULL;
Sean Callanan77eaf442011-07-08 00:39:14 +00001910 const Address *fun_address = NULL;
Greg Clayton57ee3062013-07-11 22:46:58 +00001911 ClangASTType function_clang_type;
Matt Kopec00049b82013-02-27 20:13:38 +00001912
1913 bool is_indirect_function = false;
1914
Greg Clayton57ee3062013-07-11 22:46:58 +00001915 if (function)
Sean Callanan8ade1042010-07-27 00:55:47 +00001916 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001917 Type *function_type = function->GetType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001918
Greg Clayton57ee3062013-07-11 22:46:58 +00001919 if (!function_type)
Sean Callanan8ade1042010-07-27 00:55:47 +00001920 {
1921 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +00001922 log->PutCString(" Skipped a function because it has no type");
Sean Callanan8ade1042010-07-27 00:55:47 +00001923 return;
1924 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001925
Greg Clayton57ee3062013-07-11 22:46:58 +00001926 function_clang_type = function_type->GetClangFullType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001927
Greg Clayton57ee3062013-07-11 22:46:58 +00001928 if (!function_clang_type)
Sean Callanan8ade1042010-07-27 00:55:47 +00001929 {
1930 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +00001931 log->PutCString(" Skipped a function because it has no Clang type");
Sean Callanan8ade1042010-07-27 00:55:47 +00001932 return;
1933 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001934
Greg Clayton57ee3062013-07-11 22:46:58 +00001935 fun_address = &function->GetAddressRange().GetBaseAddress();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001936
Greg Clayton57ee3062013-07-11 22:46:58 +00001937 ClangASTType copied_function_type = GuardedCopyType(function_clang_type);
1938 if (copied_function_type)
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001939 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001940 function_decl = context.AddFunDecl(copied_function_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001941
Greg Clayton57ee3062013-07-11 22:46:58 +00001942 if (!function_decl)
Sean Callanan485f7322013-04-24 00:34:41 +00001943 {
1944 if (log)
1945 {
1946 log->Printf (" Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}",
Greg Clayton57ee3062013-07-11 22:46:58 +00001947 function_type->GetName().GetCString(),
1948 function_type->GetID());
Sean Callanan485f7322013-04-24 00:34:41 +00001949 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001950
Sean Callanan485f7322013-04-24 00:34:41 +00001951 return;
1952 }
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001953 }
1954 else
1955 {
1956 // We failed to copy the type we found
1957 if (log)
1958 {
Daniel Malead01b2952012-11-29 21:49:15 +00001959 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 +00001960 function_type->GetName().GetCString(),
1961 function_type->GetID());
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001962 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001963
Sean Callananed8d58f2011-11-16 00:40:13 +00001964 return;
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001965 }
Sean Callanan8ade1042010-07-27 00:55:47 +00001966 }
1967 else if (symbol)
1968 {
Greg Claytone7612132012-03-07 21:03:09 +00001969 fun_address = &symbol->GetAddress();
Greg Clayton57ee3062013-07-11 22:46:58 +00001970 function_decl = context.AddGenericFunDecl();
Matt Kopec00049b82013-02-27 20:13:38 +00001971 is_indirect_function = symbol->IsIndirect();
Sean Callanan8ade1042010-07-27 00:55:47 +00001972 }
1973 else
1974 {
1975 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +00001976 log->PutCString(" AddOneFunction called with no function and no symbol");
Sean Callanan8ade1042010-07-27 00:55:47 +00001977 return;
1978 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001979
Sean Callanan933693b2012-02-10 01:22:05 +00001980 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00001981
Matt Kopec00049b82013-02-27 20:13:38 +00001982 lldb::addr_t load_addr = fun_address->GetCallableLoadAddress(target, is_indirect_function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001983
Greg Clayton57ee3062013-07-11 22:46:58 +00001984 ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (),
1985 m_parser_vars->m_target_info.byte_order,
1986 m_parser_vars->m_target_info.address_byte_size));
1987 assert (entity.get());
1988
1989 std::string decl_name(context.m_decl_name.getAsString());
1990 entity->SetName(ConstString(decl_name.c_str()));
1991 entity->SetClangType (function_clang_type);
1992 entity->EnableParserVars(GetParserID());
1993
1994 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001995
Sean Callanan9be9d172013-03-19 01:45:02 +00001996 if (load_addr != LLDB_INVALID_ADDRESS)
1997 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001998 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1999 parser_vars->m_lldb_value.GetScalar() = load_addr;
Sean Callanan9be9d172013-03-19 01:45:02 +00002000 }
2001 else
2002 {
2003 // We have to try finding a file address.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002004
Sean Callanan9be9d172013-03-19 01:45:02 +00002005 lldb::addr_t file_addr = fun_address->GetFileAddress();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002006
Greg Clayton57ee3062013-07-11 22:46:58 +00002007 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress);
2008 parser_vars->m_lldb_value.GetScalar() = file_addr;
Sean Callanan9be9d172013-03-19 01:45:02 +00002009 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002010
Greg Clayton57ee3062013-07-11 22:46:58 +00002011
2012 parser_vars->m_named_decl = function_decl;
Sean Callanan3c495c12013-01-15 23:29:36 +00002013 parser_vars->m_llvm_value = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002014
Sean Callananea22d422010-07-16 00:09:46 +00002015 if (log)
Greg Clayton7b462cc2010-10-15 22:48:33 +00002016 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002017 ASTDumper ast_dumper(function_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002018
Sean Callanan308a3c52012-07-28 00:21:01 +00002019 StreamString ss;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002020
Sean Callanan308a3c52012-07-28 00:21:01 +00002021 fun_address->Dump(&ss, m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), Address::DumpStyleResolvedDescription);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002022
Sean Callanan308a3c52012-07-28 00:21:01 +00002023 log->Printf(" CEDM::FEVD[%u] Found %s function %s (description %s), returned %s",
Sean Callanan7dd98122011-10-14 20:34:21 +00002024 current_id,
Greg Clayton57ee3062013-07-11 22:46:58 +00002025 (function ? "specific" : "generic"),
Sean Callanan308a3c52012-07-28 00:21:01 +00002026 decl_name.c_str(),
2027 ss.GetData(),
Greg Clayton64bc6ca2011-10-20 00:47:21 +00002028 ast_dumper.GetCString());
Greg Clayton7b462cc2010-10-15 22:48:33 +00002029 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002030}
Sean Callanan5666b672010-08-04 01:02:13 +00002031
Sean Callananfa4fab72013-02-01 06:55:48 +00002032TypeFromParser
2033ClangExpressionDeclMap::CopyClassType(TypeFromUser &ut,
2034 unsigned int current_id)
Sean Callanan5666b672010-08-04 01:02:13 +00002035{
Greg Clayton57ee3062013-07-11 22:46:58 +00002036 ClangASTType copied_clang_type = GuardedCopyType(ut);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002037
Greg Clayton57ee3062013-07-11 22:46:58 +00002038 if (!copied_clang_type)
Sean Callanane0a64f72011-12-01 21:04:37 +00002039 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002040 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002041
Sean Callananfa4fab72013-02-01 06:55:48 +00002042 if (log)
2043 log->Printf("ClangExpressionDeclMap::CopyClassType - Couldn't import the type");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002044
Sean Callananfa4fab72013-02-01 06:55:48 +00002045 return TypeFromParser();
Sean Callanane0a64f72011-12-01 21:04:37 +00002046 }
Sean Callananfa4fab72013-02-01 06:55:48 +00002047
Greg Clayton57ee3062013-07-11 22:46:58 +00002048 if (copied_clang_type.IsAggregateType() && copied_clang_type.GetCompleteType ())
Sean Callananfc55f5d2010-09-21 00:44:12 +00002049 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002050 ClangASTType void_clang_type = ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid);
2051 ClangASTType void_ptr_clang_type = void_clang_type.GetPointerType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002052
Greg Clayton57ee3062013-07-11 22:46:58 +00002053 ClangASTType method_type = ClangASTContext::CreateFunctionType (m_ast_context,
2054 void_clang_type,
2055 &void_ptr_clang_type,
Sean Callananc1b732d2011-11-01 18:07:13 +00002056 1,
2057 false,
Greg Clayton57ee3062013-07-11 22:46:58 +00002058 copied_clang_type.GetTypeQualifiers());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002059
Greg Clayton0fffff52010-09-24 05:15:53 +00002060 const bool is_virtual = false;
2061 const bool is_static = false;
2062 const bool is_inline = false;
Greg Claytonf51de672010-10-01 02:31:07 +00002063 const bool is_explicit = false;
Sean Callananc8324752011-11-02 18:09:01 +00002064 const bool is_attr_used = true;
Sean Callanandbb58392011-11-02 01:38:59 +00002065 const bool is_artificial = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002066
Greg Clayton57ee3062013-07-11 22:46:58 +00002067 copied_clang_type.AddMethodToCXXRecordType ("$__lldb_expr",
2068 method_type,
2069 lldb::eAccessPublic,
2070 is_virtual,
2071 is_static,
2072 is_inline,
2073 is_explicit,
2074 is_attr_used,
2075 is_artificial);
Sean Callananfc55f5d2010-09-21 00:44:12 +00002076 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002077
Greg Clayton57ee3062013-07-11 22:46:58 +00002078 return TypeFromParser(copied_clang_type);
Sean Callananfa4fab72013-02-01 06:55:48 +00002079}
2080
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002081void
2082ClangExpressionDeclMap::AddOneType(NameSearchContext &context,
Sean Callananfa4fab72013-02-01 06:55:48 +00002083 TypeFromUser &ut,
2084 unsigned int current_id)
2085{
Greg Clayton57ee3062013-07-11 22:46:58 +00002086 ClangASTType copied_clang_type = GuardedCopyType(ut);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002087
Greg Clayton57ee3062013-07-11 22:46:58 +00002088 if (!copied_clang_type)
Sean Callananfa4fab72013-02-01 06:55:48 +00002089 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002090 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfa4fab72013-02-01 06:55:48 +00002091
2092 if (log)
2093 log->Printf("ClangExpressionDeclMap::AddOneType - Couldn't import the type");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002094
Sean Callananfa4fab72013-02-01 06:55:48 +00002095 return;
2096 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002097
Greg Clayton57ee3062013-07-11 22:46:58 +00002098 context.AddTypeDecl(copied_clang_type);
Sean Callanan5666b672010-08-04 01:02:13 +00002099}