blob: 556b27348e54dcad3fd279acd420b2e978e4b109 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangExpressionDeclMap.cpp -----------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Sean Callanan4dbb2712015-09-25 20:35:58 +000010#include "ClangExpressionDeclMap.h"
11
12#include "ASTDumper.h"
13#include "ClangASTSource.h"
14#include "ClangModulesDeclVendor.h"
15#include "ClangPersistentVariables.h"
16
Sean Callanan80c97592015-05-01 00:47:29 +000017#include "clang/AST/ASTConsumer.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000018#include "clang/AST/ASTContext.h"
Sean Callanane2ef6e32010-09-23 03:01:22 +000019#include "clang/AST/DeclarationName.h"
Sean Callanan530782942010-11-01 23:22:47 +000020#include "clang/AST/Decl.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/lldb-private.h"
22#include "lldb/Core/Address.h"
Sean Callananea22d422010-07-16 00:09:46 +000023#include "lldb/Core/Error.h"
Sean Callananf06ba8d2010-06-23 00:47:48 +000024#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Module.h"
Greg Clayton9191db42013-10-21 18:40:51 +000026#include "lldb/Core/ModuleSpec.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000027#include "lldb/Core/RegisterValue.h"
Sean Callanane4ec90e2010-12-16 03:17:46 +000028#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan9b3569b2011-12-10 03:12:34 +000029#include "lldb/Core/ValueObjectVariable.h"
Sean Callanan96d27302013-04-11 00:09:05 +000030#include "lldb/Expression/Materializer.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000031#include "lldb/Host/Endian.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Symbol/ClangASTContext.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000033#include "lldb/Symbol/CompilerDecl.h"
Greg Clayton99558cc42015-08-24 23:46:31 +000034#include "lldb/Symbol/CompilerDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Symbol/CompileUnit.h"
36#include "lldb/Symbol/Function.h"
37#include "lldb/Symbol/ObjectFile.h"
38#include "lldb/Symbol/SymbolContext.h"
Siva Chandra9293fc42016-01-07 23:32:34 +000039#include "lldb/Symbol/SymbolFile.h"
Sean Callanan503aa522011-10-12 00:12:34 +000040#include "lldb/Symbol/SymbolVendor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "lldb/Symbol/Type.h"
42#include "lldb/Symbol/TypeList.h"
43#include "lldb/Symbol/Variable.h"
44#include "lldb/Symbol/VariableList.h"
Sean Callanana0d56432014-11-11 02:49:44 +000045#include "lldb/Target/CPPLanguageRuntime.h"
Sean Callanan1d180662010-07-20 23:31:16 +000046#include "lldb/Target/ExecutionContext.h"
Sean Callanane0b23b52012-11-15 02:02:04 +000047#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callananea22d422010-07-16 00:09:46 +000048#include "lldb/Target/Process.h"
Sean Callananf4b9bd32010-10-05 20:18:48 +000049#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000050#include "lldb/Target/StackFrame.h"
Sean Callanan1d180662010-07-20 23:31:16 +000051#include "lldb/Target/Target.h"
Jim Ingham895c9822010-12-07 01:56:02 +000052#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053
Siva Chandra0f4873d2015-09-03 23:27:10 +000054#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
55
Greg Clayton83c5cd92010-11-14 22:13:40 +000056using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057using namespace lldb_private;
58using namespace clang;
59
Siva Chandra03ff5c82016-02-05 19:10:04 +000060namespace
61{
62 const char *g_lldb_local_vars_namespace_cstr = "$__lldb_local_vars";
63} // anonymous namespace
64
Sean Callanan9fda9d22015-10-03 09:09:01 +000065ClangExpressionDeclMap::ClangExpressionDeclMap (bool keep_result_in_memory,
66 Materializer::PersistentVariableDelegate *result_delegate,
67 ExecutionContext &exe_ctx) :
Sean Callanan1ee44b72011-10-29 01:58:46 +000068 ClangASTSource (exe_ctx.GetTargetSP()),
Greg Clayton7b462cc2010-10-15 22:48:33 +000069 m_found_entities (),
70 m_struct_members (),
Stephen Wilson71c21d12011-04-11 19:41:40 +000071 m_keep_result_in_memory (keep_result_in_memory),
Sean Callanan9fda9d22015-10-03 09:09:01 +000072 m_result_delegate (result_delegate),
Sean Callanan979f74d2010-12-03 01:38:59 +000073 m_parser_vars (),
Stephen Wilson71c21d12011-04-11 19:41:40 +000074 m_struct_vars ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075{
Sean Callanan979f74d2010-12-03 01:38:59 +000076 EnableStructVars();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077}
78
79ClangExpressionDeclMap::~ClangExpressionDeclMap()
Sean Callanan979f74d2010-12-03 01:38:59 +000080{
Sean Callanane3aef1d2011-10-12 22:20:02 +000081 // Note: The model is now that the parser's AST context and all associated
82 // data does not vanish until the expression has been executed. This means
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000083 // that valuable lookup data (like namespaces) doesn't vanish, but
84
Sean Callanane3aef1d2011-10-12 22:20:02 +000085 DidParse();
Sean Callanan979f74d2010-12-03 01:38:59 +000086 DisableStructVars();
87}
Sean Callananbe3a1b12010-10-26 00:31:56 +000088
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000089bool
Sean Callanan96d27302013-04-11 00:09:05 +000090ClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx,
91 Materializer *materializer)
Sean Callanan8106d802013-03-08 20:04:57 +000092{
93 ClangASTMetrics::ClearLocalCounters();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000094
Sean Callanan979f74d2010-12-03 01:38:59 +000095 EnableParserVars();
Sean Callanan933693b2012-02-10 01:22:05 +000096 m_parser_vars->m_exe_ctx = exe_ctx;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000097
Greg Claytonc14ee322011-09-22 04:58:26 +000098 Target *target = exe_ctx.GetTargetPtr();
99 if (exe_ctx.GetFramePtr())
100 m_parser_vars->m_sym_ctx = exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything);
Sean Callanand4fac252013-02-21 22:01:43 +0000101 else if (exe_ctx.GetThreadPtr() && exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0))
Greg Claytonc14ee322011-09-22 04:58:26 +0000102 m_parser_vars->m_sym_ctx = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext(lldb::eSymbolContextEverything);
103 else if (exe_ctx.GetProcessPtr())
Greg Clayton4d122c42011-09-17 08:33:22 +0000104 {
Greg Clayton72310352013-02-23 04:12:47 +0000105 m_parser_vars->m_sym_ctx.Clear(true);
Greg Claytonc14ee322011-09-22 04:58:26 +0000106 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
Greg Clayton4d122c42011-09-17 08:33:22 +0000107 }
Greg Claytonc14ee322011-09-22 04:58:26 +0000108 else if (target)
Greg Clayton4d122c42011-09-17 08:33:22 +0000109 {
Greg Clayton72310352013-02-23 04:12:47 +0000110 m_parser_vars->m_sym_ctx.Clear(true);
Greg Claytonc14ee322011-09-22 04:58:26 +0000111 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
Greg Clayton4d122c42011-09-17 08:33:22 +0000112 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000113
Greg Claytonc14ee322011-09-22 04:58:26 +0000114 if (target)
115 {
Sean Callananb92bd752015-10-01 16:28:02 +0000116 m_parser_vars->m_persistent_vars = llvm::cast<ClangPersistentVariables>(target->GetPersistentExpressionStateForLanguage(eLanguageTypeC));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000117
Greg Claytonc14ee322011-09-22 04:58:26 +0000118 if (!target->GetScratchClangASTContext())
119 return false;
120 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000121
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000122 m_parser_vars->m_target_info = GetTargetInfo();
Sean Callanan96d27302013-04-11 00:09:05 +0000123 m_parser_vars->m_materializer = materializer;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000124
Sean Callananb9951192011-08-01 18:18:33 +0000125 return true;
Sean Callanan979f74d2010-12-03 01:38:59 +0000126}
127
Sean Callanan96d27302013-04-11 00:09:05 +0000128void
Sean Callanan80c97592015-05-01 00:47:29 +0000129ClangExpressionDeclMap::InstallCodeGenerator (clang::ASTConsumer *code_gen)
130{
131 assert(m_parser_vars);
132 m_parser_vars->m_code_gen = code_gen;
133}
134
135void
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000136ClangExpressionDeclMap::DidParse()
Sean Callanan979f74d2010-12-03 01:38:59 +0000137{
Greg Clayton5160ce52013-03-27 23:08:40 +0000138 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8106d802013-03-08 20:04:57 +0000139
140 if (log)
141 ClangASTMetrics::DumpCounters(log);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000142
Sean Callanan979f74d2010-12-03 01:38:59 +0000143 if (m_parser_vars.get())
144 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000145 for (size_t entity_index = 0, num_entities = m_found_entities.GetSize();
Sean Callanan979f74d2010-12-03 01:38:59 +0000146 entity_index < num_entities;
147 ++entity_index)
148 {
Sean Callananbc8ac342015-09-04 20:49:51 +0000149 ExpressionVariableSP var_sp(m_found_entities.GetVariableAtIndex(entity_index));
Jim Ingham28eb5712012-10-12 17:34:26 +0000150 if (var_sp)
Sean Callanan3820e9c2015-09-08 22:23:39 +0000151 llvm::cast<ClangExpressionVariable>(var_sp.get())->DisableParserVars(GetParserID());
Sean Callanan979f74d2010-12-03 01:38:59 +0000152 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000153
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000154 for (size_t pvar_index = 0, num_pvars = m_parser_vars->m_persistent_vars->GetSize();
Sean Callanan979f74d2010-12-03 01:38:59 +0000155 pvar_index < num_pvars;
156 ++pvar_index)
157 {
Sean Callananbc8ac342015-09-04 20:49:51 +0000158 ExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index));
Sean Callanan85aabaf2015-09-14 21:03:44 +0000159 if (ClangExpressionVariable *clang_var = llvm::dyn_cast<ClangExpressionVariable>(pvar_sp.get()))
160 clang_var->DisableParserVars(GetParserID());
Sean Callanan979f74d2010-12-03 01:38:59 +0000161 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000162
Sean Callanan979f74d2010-12-03 01:38:59 +0000163 DisableParserVars();
Sean Callanan6b1b9532010-10-08 01:58:41 +0000164 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165}
166
Sean Callanan549c9f72010-07-13 21:41:46 +0000167// Interface for IRForTarget
168
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000169ClangExpressionDeclMap::TargetInfo
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000170ClangExpressionDeclMap::GetTargetInfo()
171{
172 assert (m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000173
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000174 TargetInfo ret;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000175
Sean Callanan933693b2012-02-10 01:22:05 +0000176 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
177
178 Process *process = exe_ctx.GetProcessPtr();
179 if (process)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000180 {
Sean Callanan933693b2012-02-10 01:22:05 +0000181 ret.byte_order = process->GetByteOrder();
182 ret.address_byte_size = process->GetAddressByteSize();
183 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000184 else
Sean Callanan933693b2012-02-10 01:22:05 +0000185 {
186 Target *target = exe_ctx.GetTargetPtr();
187 if (target)
Greg Claytonc14ee322011-09-22 04:58:26 +0000188 {
Sean Callanan933693b2012-02-10 01:22:05 +0000189 ret.byte_order = target->GetArchitecture().GetByteOrder();
190 ret.address_byte_size = target->GetArchitecture().GetAddressByteSize();
Greg Claytonc14ee322011-09-22 04:58:26 +0000191 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000192 }
Sean Callanan933693b2012-02-10 01:22:05 +0000193
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000194 return ret;
195}
196
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000197bool
198ClangExpressionDeclMap::AddPersistentVariable
Greg Clayton7b462cc2010-10-15 22:48:33 +0000199(
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000200 const NamedDecl *decl,
201 const ConstString &name,
Sean Callanan92adcac2011-01-13 08:53:35 +0000202 TypeFromParser parser_type,
203 bool is_result,
204 bool is_lvalue
Greg Clayton7b462cc2010-10-15 22:48:33 +0000205)
Sean Callanan2235f322010-08-11 03:57:18 +0000206{
Sean Callanan979f74d2010-12-03 01:38:59 +0000207 assert (m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000208
Greg Claytonf73034f2015-09-08 18:15:05 +0000209 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(parser_type.GetTypeSystem());
210 if (ast == nullptr)
211 return false;
212
Sean Callanan1582ee62013-04-18 22:06:33 +0000213 if (m_parser_vars->m_materializer && is_result)
214 {
215 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000216
Sean Callanan1582ee62013-04-18 22:06:33 +0000217 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
218 Target *target = exe_ctx.GetTargetPtr();
Greg Claytonf73034f2015-09-08 18:15:05 +0000219 if (target == nullptr)
Sean Callanan1582ee62013-04-18 22:06:33 +0000220 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000221
Greg Claytond8d4a572015-08-11 21:38:15 +0000222 ClangASTContext *context(target->GetScratchClangASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000223
Sean Callanana3444ff2015-11-10 22:54:42 +0000224 TypeFromUser user_type(m_ast_importer_sp->DeportType(context->getASTContext(),
225 ast->getASTContext(),
226 parser_type.GetOpaqueQualType()),
Sean Callanan1582ee62013-04-18 22:06:33 +0000227 context);
Sean Callanan9fda9d22015-10-03 09:09:01 +0000228
229 uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(user_type,
230 is_lvalue,
231 m_keep_result_in_memory,
232 m_result_delegate,
233 err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000234
Sean Callanan9301ec12015-10-01 23:07:06 +0000235 ClangExpressionVariable *var = new ClangExpressionVariable(exe_ctx.GetBestExecutionContextScope(),
236 name,
237 user_type,
238 m_parser_vars->m_target_info.byte_order,
239 m_parser_vars->m_target_info.address_byte_size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000240
Sean Callanan9301ec12015-10-01 23:07:06 +0000241 m_found_entities.AddNewlyConstructedVariable(var);
242
Sean Callananbc8ac342015-09-04 20:49:51 +0000243 var->EnableParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000244
Sean Callananbc8ac342015-09-04 20:49:51 +0000245 ClangExpressionVariable::ParserVars *parser_vars = var->GetParserVars(GetParserID());
Sean Callanan1582ee62013-04-18 22:06:33 +0000246
247 parser_vars->m_named_decl = decl;
248 parser_vars->m_parser_type = parser_type;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000249
Sean Callananbc8ac342015-09-04 20:49:51 +0000250 var->EnableJITVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000251
Sean Callananbc8ac342015-09-04 20:49:51 +0000252 ClangExpressionVariable::JITVars *jit_vars = var->GetJITVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000253
Sean Callanan1582ee62013-04-18 22:06:33 +0000254 jit_vars->m_offset = offset;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000255
Sean Callanan1582ee62013-04-18 22:06:33 +0000256 return true;
257 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000258
Greg Clayton5160ce52013-03-27 23:08:40 +0000259 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan933693b2012-02-10 01:22:05 +0000260 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
261 Target *target = exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +0000262 if (target == NULL)
263 return false;
264
Greg Claytond8d4a572015-08-11 21:38:15 +0000265 ClangASTContext *context(target->GetScratchClangASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000266
Sean Callanana3444ff2015-11-10 22:54:42 +0000267 TypeFromUser user_type(m_ast_importer_sp->DeportType(context->getASTContext(),
268 ast->getASTContext(),
269 parser_type.GetOpaqueQualType()),
Sean Callanane1175b72011-01-13 21:23:32 +0000270 context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000271
Sean Callanan00f43622011-11-18 03:28:09 +0000272 if (!user_type.GetOpaqueQualType())
273 {
274 if (log)
275 log->Printf("Persistent variable's type wasn't copied successfully");
276 return false;
277 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000278
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000279 if (!m_parser_vars->m_target_info.IsValid())
280 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000281
Sean Callanan9301ec12015-10-01 23:07:06 +0000282 ClangExpressionVariable *var = llvm::cast<ClangExpressionVariable>(m_parser_vars->m_persistent_vars->CreatePersistentVariable (exe_ctx.GetBestExecutionContextScope (),
283 name,
284 user_type,
285 m_parser_vars->m_target_info.byte_order,
286 m_parser_vars->m_target_info.address_byte_size).get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000287
Sean Callananbc8ac342015-09-04 20:49:51 +0000288 if (!var)
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000289 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000290
Sean Callananbc8ac342015-09-04 20:49:51 +0000291 var->m_frozen_sp->SetHasCompleteType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000292
Sean Callanan92adcac2011-01-13 08:53:35 +0000293 if (is_result)
Sean Callananbc8ac342015-09-04 20:49:51 +0000294 var->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry;
Sean Callanan92adcac2011-01-13 08:53:35 +0000295 else
Sean Callananbc8ac342015-09-04 20:49:51 +0000296 var->m_flags |= ClangExpressionVariable::EVKeepInTarget; // explicitly-declared persistent variables should persist
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000297
Sean Callanan92adcac2011-01-13 08:53:35 +0000298 if (is_lvalue)
299 {
Sean Callananbc8ac342015-09-04 20:49:51 +0000300 var->m_flags |= ClangExpressionVariable::EVIsProgramReference;
Sean Callanan92adcac2011-01-13 08:53:35 +0000301 }
302 else
303 {
Sean Callananbc8ac342015-09-04 20:49:51 +0000304 var->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
305 var->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
Sean Callanan92adcac2011-01-13 08:53:35 +0000306 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000307
Sean Callanan35005f72013-04-12 18:10:34 +0000308 if (m_keep_result_in_memory)
309 {
Sean Callananbc8ac342015-09-04 20:49:51 +0000310 var->m_flags |= ClangExpressionVariable::EVKeepInTarget;
Sean Callanan35005f72013-04-12 18:10:34 +0000311 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000312
Sean Callanan92adcac2011-01-13 08:53:35 +0000313 if (log)
Sean Callananbc8ac342015-09-04 20:49:51 +0000314 log->Printf("Created persistent variable with flags 0x%hx", var->m_flags);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000315
Sean Callananbc8ac342015-09-04 20:49:51 +0000316 var->EnableParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000317
Sean Callananbc8ac342015-09-04 20:49:51 +0000318 ClangExpressionVariable::ParserVars *parser_vars = var->GetParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000319
Sean Callanan3c495c12013-01-15 23:29:36 +0000320 parser_vars->m_named_decl = decl;
321 parser_vars->m_parser_type = parser_type;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000322
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000323 return true;
Sean Callanan2235f322010-08-11 03:57:18 +0000324}
325
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000326bool
327ClangExpressionDeclMap::AddValueToStruct
Greg Clayton7b462cc2010-10-15 22:48:33 +0000328(
Sean Callanancc427fa2011-07-30 02:42:06 +0000329 const NamedDecl *decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +0000330 const ConstString &name,
331 llvm::Value *value,
332 size_t size,
Zachary Turnera746e8e2014-07-02 17:24:07 +0000333 lldb::offset_t alignment
Greg Clayton7b462cc2010-10-15 22:48:33 +0000334)
Sean Callanan549c9f72010-07-13 21:41:46 +0000335{
Sean Callanan979f74d2010-12-03 01:38:59 +0000336 assert (m_struct_vars.get());
337 assert (m_parser_vars.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000338
Sean Callanandf667652013-04-11 02:05:11 +0000339 bool is_persistent_variable = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000340
Greg Clayton5160ce52013-03-27 23:08:40 +0000341 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000342
Sean Callanan979f74d2010-12-03 01:38:59 +0000343 m_struct_vars->m_struct_laid_out = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000344
Sean Callananbc8ac342015-09-04 20:49:51 +0000345 if (ClangExpressionVariable::FindVariableInList(m_struct_members, decl, GetParserID()))
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000346 return true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000347
Sean Callananbc8ac342015-09-04 20:49:51 +0000348 ClangExpressionVariable *var(ClangExpressionVariable::FindVariableInList(m_found_entities, decl, GetParserID()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000349
Sean Callananbc8ac342015-09-04 20:49:51 +0000350 if (!var)
Sean Callanandf667652013-04-11 02:05:11 +0000351 {
Sean Callananbc8ac342015-09-04 20:49:51 +0000352 var = ClangExpressionVariable::FindVariableInList(*m_parser_vars->m_persistent_vars, decl, GetParserID());
Sean Callanandf667652013-04-11 02:05:11 +0000353 is_persistent_variable = true;
354 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000355
Sean Callananbc8ac342015-09-04 20:49:51 +0000356 if (!var)
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000357 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000358
Sean Callanan823bb4c2010-08-30 22:17:16 +0000359 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000360 log->Printf("Adding value for (NamedDecl*)%p [%s - %s] to the structure",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000361 static_cast<const void*>(decl), name.GetCString(),
Sean Callananbc8ac342015-09-04 20:49:51 +0000362 var->GetName().GetCString());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000363
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000364 // We know entity->m_parser_vars is valid because we used a parser variable
365 // to find it
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000366
Sean Callanan3820e9c2015-09-08 22:23:39 +0000367 ClangExpressionVariable::ParserVars *parser_vars = llvm::cast<ClangExpressionVariable>(var)->GetParserVars(GetParserID());
Sean Callanan3c495c12013-01-15 23:29:36 +0000368
369 parser_vars->m_llvm_value = value;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000370
Sean Callanan3820e9c2015-09-08 22:23:39 +0000371 if (ClangExpressionVariable::JITVars *jit_vars = llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID()))
Sean Callanan1582ee62013-04-18 22:06:33 +0000372 {
373 // We already laid this out; do not touch
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000374
Sean Callanan1582ee62013-04-18 22:06:33 +0000375 if (log)
376 log->Printf("Already placed at 0x%llx", (unsigned long long)jit_vars->m_offset);
377 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000378
Sean Callanan3820e9c2015-09-08 22:23:39 +0000379 llvm::cast<ClangExpressionVariable>(var)->EnableJITVars(GetParserID());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000380
Sean Callanan3820e9c2015-09-08 22:23:39 +0000381 ClangExpressionVariable::JITVars *jit_vars = llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID());
Sean Callanan3c495c12013-01-15 23:29:36 +0000382
383 jit_vars->m_alignment = alignment;
384 jit_vars->m_size = size;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000385
Sean Callananbc8ac342015-09-04 20:49:51 +0000386 m_struct_members.AddVariable(var->shared_from_this());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000387
Sean Callanandf667652013-04-11 02:05:11 +0000388 if (m_parser_vars->m_materializer)
389 {
Sean Callanan3dd6a422013-04-11 21:16:36 +0000390 uint32_t offset = 0;
391
Sean Callanandf667652013-04-11 02:05:11 +0000392 Error err;
393
394 if (is_persistent_variable)
395 {
Sean Callananbc8ac342015-09-04 20:49:51 +0000396 ExpressionVariableSP var_sp(var->shared_from_this());
Sean Callanan9fda9d22015-10-03 09:09:01 +0000397 offset = m_parser_vars->m_materializer->AddPersistentVariable(var_sp, nullptr, err);
Sean Callanandf667652013-04-11 02:05:11 +0000398 }
399 else
400 {
401 if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym)
Sean Callanan3dd6a422013-04-11 21:16:36 +0000402 offset = m_parser_vars->m_materializer->AddSymbol(*sym, err);
Sean Callananbc8ac342015-09-04 20:49:51 +0000403 else if (const RegisterInfo *reg_info = var->GetRegisterInfo())
Sean Callanan3dd6a422013-04-11 21:16:36 +0000404 offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err);
Sean Callanandf667652013-04-11 02:05:11 +0000405 else if (parser_vars->m_lldb_var)
Sean Callanan3dd6a422013-04-11 21:16:36 +0000406 offset = m_parser_vars->m_materializer->AddVariable(parser_vars->m_lldb_var, err);
Sean Callanandf667652013-04-11 02:05:11 +0000407 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000408
Sean Callanan3dd6a422013-04-11 21:16:36 +0000409 if (!err.Success())
410 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000411
Sean Callanan3dd6a422013-04-11 21:16:36 +0000412 if (log)
413 log->Printf("Placed at 0x%llx", (unsigned long long)offset);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000414
Sean Callanan3dd6a422013-04-11 21:16:36 +0000415 jit_vars->m_offset = offset; // TODO DoStructLayout() should not change this.
Sean Callanandf667652013-04-11 02:05:11 +0000416 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000417
Sean Callanan549c9f72010-07-13 21:41:46 +0000418 return true;
419}
420
421bool
422ClangExpressionDeclMap::DoStructLayout ()
423{
Sean Callanan979f74d2010-12-03 01:38:59 +0000424 assert (m_struct_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000425
Sean Callanan979f74d2010-12-03 01:38:59 +0000426 if (m_struct_vars->m_struct_laid_out)
Sean Callanan549c9f72010-07-13 21:41:46 +0000427 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000428
Sean Callanan14b1bae2013-04-16 23:25:35 +0000429 if (!m_parser_vars->m_materializer)
430 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000431
Sean Callanan14b1bae2013-04-16 23:25:35 +0000432 m_struct_vars->m_struct_alignment = m_parser_vars->m_materializer->GetStructAlignment();
433 m_struct_vars->m_struct_size = m_parser_vars->m_materializer->GetStructByteSize();
Sean Callanan979f74d2010-12-03 01:38:59 +0000434 m_struct_vars->m_struct_laid_out = true;
Sean Callanan549c9f72010-07-13 21:41:46 +0000435 return true;
436}
437
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000438bool ClangExpressionDeclMap::GetStructInfo
Greg Clayton7b462cc2010-10-15 22:48:33 +0000439(
440 uint32_t &num_elements,
441 size_t &size,
Zachary Turnera746e8e2014-07-02 17:24:07 +0000442 lldb::offset_t &alignment
Greg Clayton7b462cc2010-10-15 22:48:33 +0000443)
Sean Callanan549c9f72010-07-13 21:41:46 +0000444{
Sean Callanan979f74d2010-12-03 01:38:59 +0000445 assert (m_struct_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000446
Sean Callanan979f74d2010-12-03 01:38:59 +0000447 if (!m_struct_vars->m_struct_laid_out)
Sean Callanan549c9f72010-07-13 21:41:46 +0000448 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000449
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000450 num_elements = m_struct_members.GetSize();
Sean Callanan979f74d2010-12-03 01:38:59 +0000451 size = m_struct_vars->m_struct_size;
452 alignment = m_struct_vars->m_struct_alignment;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000453
Sean Callanan549c9f72010-07-13 21:41:46 +0000454 return true;
455}
456
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000457bool
458ClangExpressionDeclMap::GetStructElement
Greg Clayton7b462cc2010-10-15 22:48:33 +0000459(
Sean Callanancc427fa2011-07-30 02:42:06 +0000460 const NamedDecl *&decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +0000461 llvm::Value *&value,
Zachary Turnera746e8e2014-07-02 17:24:07 +0000462 lldb::offset_t &offset,
Greg Clayton7b462cc2010-10-15 22:48:33 +0000463 ConstString &name,
464 uint32_t index
465)
Sean Callanan549c9f72010-07-13 21:41:46 +0000466{
Sean Callanan979f74d2010-12-03 01:38:59 +0000467 assert (m_struct_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000468
Sean Callanan979f74d2010-12-03 01:38:59 +0000469 if (!m_struct_vars->m_struct_laid_out)
Sean Callanan549c9f72010-07-13 21:41:46 +0000470 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000471
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000472 if (index >= m_struct_members.GetSize())
Sean Callanan549c9f72010-07-13 21:41:46 +0000473 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000474
Sean Callananbc8ac342015-09-04 20:49:51 +0000475 ExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000476
Sean Callanan3c495c12013-01-15 23:29:36 +0000477 if (!member_sp)
478 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000479
Sean Callanan3820e9c2015-09-08 22:23:39 +0000480 ClangExpressionVariable::ParserVars *parser_vars = llvm::cast<ClangExpressionVariable>(member_sp.get())->GetParserVars(GetParserID());
481 ClangExpressionVariable::JITVars *jit_vars = llvm::cast<ClangExpressionVariable>(member_sp.get())->GetJITVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000482
Sean Callanan3c495c12013-01-15 23:29:36 +0000483 if (!parser_vars ||
484 !jit_vars ||
Sean Callanan6f3e5ad2012-04-12 16:58:26 +0000485 !member_sp->GetValueObject())
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000486 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000487
Sean Callanan3c495c12013-01-15 23:29:36 +0000488 decl = parser_vars->m_named_decl;
489 value = parser_vars->m_llvm_value;
490 offset = jit_vars->m_offset;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000491 name = member_sp->GetName();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000492
Sean Callanan549c9f72010-07-13 21:41:46 +0000493 return true;
494}
495
Sean Callanan7ea35012010-07-27 21:39:39 +0000496bool
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000497ClangExpressionDeclMap::GetFunctionInfo
Greg Clayton7b462cc2010-10-15 22:48:33 +0000498(
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000499 const NamedDecl *decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +0000500 uint64_t &ptr
501)
Sean Callanan4edba2d2010-07-27 02:07:53 +0000502{
Sean Callananbc8ac342015-09-04 20:49:51 +0000503 ClangExpressionVariable *entity(ClangExpressionVariable::FindVariableInList(m_found_entities, decl, GetParserID()));
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000504
Sean Callananbc8ac342015-09-04 20:49:51 +0000505 if (!entity)
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000506 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000507
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000508 // We know m_parser_vars is valid since we searched for the variable by
509 // its NamedDecl
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000510
Sean Callananbc8ac342015-09-04 20:49:51 +0000511 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
Sean Callanan3c495c12013-01-15 23:29:36 +0000512
Greg Clayton57ee3062013-07-11 22:46:58 +0000513 ptr = parser_vars->m_lldb_value.GetScalar().ULongLong();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000514
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000515 return true;
Sean Callanan4edba2d2010-07-27 02:07:53 +0000516}
517
Greg Clayton084db102011-06-23 04:25:29 +0000518addr_t
Greg Clayton9191db42013-10-21 18:40:51 +0000519ClangExpressionDeclMap::GetSymbolAddress (Target &target,
520 Process *process,
521 const ConstString &name,
522 lldb::SymbolType symbol_type,
Deepak Panickald66b50c2013-10-22 12:27:43 +0000523 lldb_private::Module *module)
Sean Callananc3a16002011-01-17 23:42:46 +0000524{
Sean Callananc3a16002011-01-17 23:42:46 +0000525 SymbolContextList sc_list;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000526
Greg Clayton9191db42013-10-21 18:40:51 +0000527 if (module)
528 module->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
529 else
530 target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000531
Greg Clayton084db102011-06-23 04:25:29 +0000532 const uint32_t num_matches = sc_list.GetSize();
533 addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
534
Sean Callanan947ccc72011-12-01 02:04:16 +0000535 for (uint32_t i=0; i<num_matches && (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS); i++)
Greg Clayton084db102011-06-23 04:25:29 +0000536 {
537 SymbolContext sym_ctx;
538 sc_list.GetContextAtIndex(i, sym_ctx);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000539
Greg Clayton358cf1e2015-06-25 21:46:34 +0000540 const Address sym_address = sym_ctx.symbol->GetAddress();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000541
Greg Clayton358cf1e2015-06-25 21:46:34 +0000542 if (!sym_address.IsValid())
Sean Callanan116ace22013-04-30 00:21:42 +0000543 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000544
Greg Clayton358cf1e2015-06-25 21:46:34 +0000545 switch (sym_ctx.symbol->GetType())
Greg Clayton084db102011-06-23 04:25:29 +0000546 {
Greg Clayton358cf1e2015-06-25 21:46:34 +0000547 case eSymbolTypeCode:
548 case eSymbolTypeTrampoline:
549 symbol_load_addr = sym_address.GetCallableLoadAddress (&target);
550 break;
Matt Kopec00049b82013-02-27 20:13:38 +0000551
Greg Clayton358cf1e2015-06-25 21:46:34 +0000552 case eSymbolTypeResolver:
553 symbol_load_addr = sym_address.GetCallableLoadAddress (&target, true);
554 break;
Matt Kopec00049b82013-02-27 20:13:38 +0000555
Greg Clayton358cf1e2015-06-25 21:46:34 +0000556 case eSymbolTypeReExported:
557 {
558 ConstString reexport_name = sym_ctx.symbol->GetReExportedSymbolName();
559 if (reexport_name)
Greg Clayton9191db42013-10-21 18:40:51 +0000560 {
Greg Clayton358cf1e2015-06-25 21:46:34 +0000561 ModuleSP reexport_module_sp;
562 ModuleSpec reexport_module_spec;
563 reexport_module_spec.GetPlatformFileSpec() = sym_ctx.symbol->GetReExportedSymbolSharedLibrary();
564 if (reexport_module_spec.GetPlatformFileSpec())
Greg Clayton9191db42013-10-21 18:40:51 +0000565 {
Greg Clayton358cf1e2015-06-25 21:46:34 +0000566 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
567 if (!reexport_module_sp)
Greg Clayton9191db42013-10-21 18:40:51 +0000568 {
Greg Clayton358cf1e2015-06-25 21:46:34 +0000569 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
Greg Clayton9191db42013-10-21 18:40:51 +0000570 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
Greg Clayton9191db42013-10-21 18:40:51 +0000571 }
Greg Clayton9191db42013-10-21 18:40:51 +0000572 }
Greg Clayton358cf1e2015-06-25 21:46:34 +0000573 symbol_load_addr = GetSymbolAddress(target, process, sym_ctx.symbol->GetReExportedSymbolName(), symbol_type, reexport_module_sp.get());
Greg Clayton9191db42013-10-21 18:40:51 +0000574 }
Greg Clayton358cf1e2015-06-25 21:46:34 +0000575 }
576 break;
Greg Clayton9191db42013-10-21 18:40:51 +0000577
Greg Clayton358cf1e2015-06-25 21:46:34 +0000578 case eSymbolTypeData:
579 case eSymbolTypeRuntime:
580 case eSymbolTypeVariable:
581 case eSymbolTypeLocal:
582 case eSymbolTypeParam:
583 case eSymbolTypeInvalid:
584 case eSymbolTypeAbsolute:
585 case eSymbolTypeException:
586 case eSymbolTypeSourceFile:
587 case eSymbolTypeHeaderFile:
588 case eSymbolTypeObjectFile:
589 case eSymbolTypeCommonBlock:
590 case eSymbolTypeBlock:
591 case eSymbolTypeVariableType:
592 case eSymbolTypeLineEntry:
593 case eSymbolTypeLineHeader:
594 case eSymbolTypeScopeBegin:
595 case eSymbolTypeScopeEnd:
596 case eSymbolTypeAdditional:
597 case eSymbolTypeCompiler:
598 case eSymbolTypeInstrumentation:
599 case eSymbolTypeUndefined:
600 case eSymbolTypeObjCClass:
601 case eSymbolTypeObjCMetaClass:
602 case eSymbolTypeObjCIVar:
603 symbol_load_addr = sym_address.GetLoadAddress (&target);
604 break;
Greg Clayton084db102011-06-23 04:25:29 +0000605 }
606 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000607
Sean Callanane0b23b52012-11-15 02:02:04 +0000608 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process)
609 {
610 ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000611
Sean Callanane0b23b52012-11-15 02:02:04 +0000612 if (runtime)
613 {
614 symbol_load_addr = runtime->LookupRuntimeSymbol(name);
615 }
616 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000617
Greg Clayton084db102011-06-23 04:25:29 +0000618 return symbol_load_addr;
Sean Callananc3a16002011-01-17 23:42:46 +0000619}
620
Greg Clayton084db102011-06-23 04:25:29 +0000621addr_t
Sean Callanan947ccc72011-12-01 02:04:16 +0000622ClangExpressionDeclMap::GetSymbolAddress (const ConstString &name, lldb::SymbolType symbol_type)
Sean Callanand9ca42a2011-05-08 02:21:26 +0000623{
624 assert (m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000625
Sean Callanan933693b2012-02-10 01:22:05 +0000626 if (!m_parser_vars->m_exe_ctx.GetTargetPtr())
Sean Callanand9ca42a2011-05-08 02:21:26 +0000627 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000628
Sean Callanane0b23b52012-11-15 02:02:04 +0000629 return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(), m_parser_vars->m_exe_ctx.GetProcessPtr(), name, symbol_type);
Sean Callanand9ca42a2011-05-08 02:21:26 +0000630}
631
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000632const Symbol *
633ClangExpressionDeclMap::FindGlobalDataSymbol (Target &target,
Greg Clayton9191db42013-10-21 18:40:51 +0000634 const ConstString &name,
Deepak Panickald66b50c2013-10-22 12:27:43 +0000635 lldb_private::Module *module)
Sean Callanand9ca42a2011-05-08 02:21:26 +0000636{
637 SymbolContextList sc_list;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000638
Greg Clayton9191db42013-10-21 18:40:51 +0000639 if (module)
640 module->FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
641 else
642 target.GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000643
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000644 const uint32_t matches = sc_list.GetSize();
645 for (uint32_t i=0; i<matches; ++i)
Sean Callanand9ca42a2011-05-08 02:21:26 +0000646 {
647 SymbolContext sym_ctx;
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000648 sc_list.GetContextAtIndex(i, sym_ctx);
649 if (sym_ctx.symbol)
650 {
651 const Symbol *symbol = sym_ctx.symbol;
Greg Clayton358cf1e2015-06-25 21:46:34 +0000652 const Address sym_address = symbol->GetAddress();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000653
Greg Clayton358cf1e2015-06-25 21:46:34 +0000654 if (sym_address.IsValid())
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000655 {
656 switch (symbol->GetType())
657 {
658 case eSymbolTypeData:
659 case eSymbolTypeRuntime:
660 case eSymbolTypeAbsolute:
661 case eSymbolTypeObjCClass:
662 case eSymbolTypeObjCMetaClass:
663 case eSymbolTypeObjCIVar:
664 if (symbol->GetDemangledNameIsSynthesized())
665 {
666 // If the demangled name was synthesized, then don't use it
667 // for expressions. Only let the symbol match if the mangled
668 // named matches for these symbols.
669 if (symbol->GetMangled().GetMangledName() != name)
670 break;
671 }
672 return symbol;
673
Greg Clayton9191db42013-10-21 18:40:51 +0000674 case eSymbolTypeReExported:
675 {
676 ConstString reexport_name = symbol->GetReExportedSymbolName();
677 if (reexport_name)
678 {
679 ModuleSP reexport_module_sp;
680 ModuleSpec reexport_module_spec;
681 reexport_module_spec.GetPlatformFileSpec() = symbol->GetReExportedSymbolSharedLibrary();
682 if (reexport_module_spec.GetPlatformFileSpec())
683 {
684 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
685 if (!reexport_module_sp)
686 {
687 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
688 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
689 }
690 }
Greg Clayton67d49482015-05-15 21:27:16 +0000691 // Don't allow us to try and resolve a re-exported symbol if it is the same
692 // as the current symbol
693 if (name == symbol->GetReExportedSymbolName() && module == reexport_module_sp.get())
694 return NULL;
695
Greg Clayton9191db42013-10-21 18:40:51 +0000696 return FindGlobalDataSymbol(target, symbol->GetReExportedSymbolName(), reexport_module_sp.get());
697 }
698 }
699 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000700
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000701 case eSymbolTypeCode: // We already lookup functions elsewhere
702 case eSymbolTypeVariable:
703 case eSymbolTypeLocal:
704 case eSymbolTypeParam:
705 case eSymbolTypeTrampoline:
706 case eSymbolTypeInvalid:
707 case eSymbolTypeException:
708 case eSymbolTypeSourceFile:
709 case eSymbolTypeHeaderFile:
710 case eSymbolTypeObjectFile:
711 case eSymbolTypeCommonBlock:
712 case eSymbolTypeBlock:
713 case eSymbolTypeVariableType:
714 case eSymbolTypeLineEntry:
715 case eSymbolTypeLineHeader:
716 case eSymbolTypeScopeBegin:
717 case eSymbolTypeScopeEnd:
718 case eSymbolTypeAdditional:
719 case eSymbolTypeCompiler:
720 case eSymbolTypeInstrumentation:
721 case eSymbolTypeUndefined:
Matt Kopec00049b82013-02-27 20:13:38 +0000722 case eSymbolTypeResolver:
Greg Clayton3d51b9f2012-11-27 01:52:16 +0000723 break;
724 }
725 }
726 }
Sean Callanand9ca42a2011-05-08 02:21:26 +0000727 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000728
Sean Callanand9ca42a2011-05-08 02:21:26 +0000729 return NULL;
730}
731
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000732lldb::VariableSP
733ClangExpressionDeclMap::FindGlobalVariable
734(
735 Target &target,
Sean Callanan1fd3f4f2011-10-12 16:59:31 +0000736 ModuleSP &module,
737 const ConstString &name,
Greg Clayton99558cc42015-08-24 23:46:31 +0000738 CompilerDeclContext *namespace_decl,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000739 TypeFromUser *type
740)
741{
742 VariableList vars;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000743
Sean Callanan1fd3f4f2011-10-12 16:59:31 +0000744 if (module && namespace_decl)
745 module->FindGlobalVariables (name, namespace_decl, true, -1, vars);
746 else
747 target.GetImages().FindGlobalVariables(name, true, -1, vars);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000748
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000749 if (vars.GetSize())
750 {
751 if (type)
752 {
753 for (size_t i = 0; i < vars.GetSize(); ++i)
754 {
755 VariableSP var_sp = vars.GetVariableAtIndex(i);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000756
Greg Clayton99558cc42015-08-24 23:46:31 +0000757 if (ClangASTContext::AreTypesSame(*type, var_sp->GetType()->GetFullCompilerType ()))
Greg Clayton57ee3062013-07-11 22:46:58 +0000758 return var_sp;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000759 }
760 }
761 else
762 {
763 return vars.GetVariableAtIndex(0);
764 }
765 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000766
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000767 return VariableSP();
768}
769
Siva Chandra03ff5c82016-02-05 19:10:04 +0000770ClangASTContext *
771ClangExpressionDeclMap::GetClangASTContext ()
772{
773 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
774 if (frame == nullptr)
775 return nullptr;
776
777 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction|lldb::eSymbolContextBlock);
778 if (sym_ctx.block == nullptr)
779 return nullptr;
780
781 CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext();
782 if (!frame_decl_context)
783 return nullptr;
784
785 return llvm::dyn_cast_or_null<ClangASTContext>(frame_decl_context.GetTypeSystem());
786}
787
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000788// Interface for ClangASTSource
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000789
Sean Callanan503aa522011-10-12 00:12:34 +0000790void
Sean Callananeddeb3b2011-10-28 23:38:38 +0000791ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000792{
Sean Callananeddeb3b2011-10-28 23:38:38 +0000793 assert (m_ast_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000794
Sean Callanan8106d802013-03-08 20:04:57 +0000795 ClangASTMetrics::RegisterVisibleQuery();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000796
Sean Callananeddeb3b2011-10-28 23:38:38 +0000797 const ConstString name(context.m_decl_name.getAsString().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000798
Greg Clayton5160ce52013-03-27 23:08:40 +0000799 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000800
Sean Callananfb3e4302011-10-29 19:50:43 +0000801 if (GetImportInProgress())
Sean Callanan6abfabf2010-11-19 20:20:02 +0000802 {
Sean Callanan7dd98122011-10-14 20:34:21 +0000803 if (log && log->GetVerbose())
Sean Callanan6abfabf2010-11-19 20:20:02 +0000804 log->Printf("Ignoring a query during an import");
805 return;
806 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000807
Sean Callanan7dd98122011-10-14 20:34:21 +0000808 static unsigned int invocation_id = 0;
809 unsigned int current_id = invocation_id++;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000810
Sean Callanan503aa522011-10-12 00:12:34 +0000811 if (log)
812 {
813 if (!context.m_decl_context)
Sean Callananfb3e4302011-10-29 19:50:43 +0000814 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a NULL DeclContext", current_id, name.GetCString());
Sean Callanan503aa522011-10-12 00:12:34 +0000815 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callananfb3e4302011-10-29 19:50:43 +0000816 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in '%s'", current_id, name.GetCString(), context_named_decl->getNameAsString().c_str());
Sean Callanan503aa522011-10-12 00:12:34 +0000817 else
Sean Callananfb3e4302011-10-29 19:50:43 +0000818 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a '%s'", current_id, name.GetCString(), context.m_decl_context->getDeclKindName());
Sean Callanan503aa522011-10-12 00:12:34 +0000819 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000820
Sean Callanan503aa522011-10-12 00:12:34 +0000821 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
822 {
Siva Chandra03ff5c82016-02-05 19:10:04 +0000823 if (namespace_context->getName().str() == std::string(g_lldb_local_vars_namespace_cstr))
824 {
Saleem Abdulrasool2f1d3ac2016-02-15 03:23:14 +0000825 CompilerDeclContext compiler_decl_ctx(GetClangASTContext(), const_cast<void *>(static_cast<const void *>(context.m_decl_context)));
Siva Chandra03ff5c82016-02-05 19:10:04 +0000826 FindExternalVisibleDecls(context, lldb::ModuleSP(), compiler_decl_ctx, current_id);
827 return;
828 }
829
Sean Callanana3444ff2015-11-10 22:54:42 +0000830 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer_sp->GetNamespaceMap(namespace_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000831
Sean Callanan7dd98122011-10-14 20:34:21 +0000832 if (log && log->GetVerbose())
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000833 log->Printf(" CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000834 current_id, static_cast<void*>(namespace_map.get()),
Sean Callanan7dd98122011-10-14 20:34:21 +0000835 (int)namespace_map->size());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000836
Sean Callananb2269162011-10-21 22:18:07 +0000837 if (!namespace_map)
838 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000839
Sean Callanan503aa522011-10-12 00:12:34 +0000840 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
841 i != e;
842 ++i)
843 {
844 if (log)
Sean Callananfb3e4302011-10-29 19:50:43 +0000845 log->Printf(" CEDM::FEVD[%u] Searching namespace %s in module %s",
Sean Callanan7dd98122011-10-14 20:34:21 +0000846 current_id,
Greg Clayton99558cc42015-08-24 23:46:31 +0000847 i->second.GetName().AsCString(),
Sean Callanan503aa522011-10-12 00:12:34 +0000848 i->first->GetFileSpec().GetFilename().GetCString());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000849
Sean Callananebe60672011-10-13 21:50:33 +0000850 FindExternalVisibleDecls(context,
851 i->first,
852 i->second,
Sean Callanan7dd98122011-10-14 20:34:21 +0000853 current_id);
Sean Callanan503aa522011-10-12 00:12:34 +0000854 }
855 }
Sean Callanand5c17ed2011-11-15 02:11:17 +0000856 else if (isa<TranslationUnitDecl>(context.m_decl_context))
Sean Callanan503aa522011-10-12 00:12:34 +0000857 {
Greg Clayton99558cc42015-08-24 23:46:31 +0000858 CompilerDeclContext namespace_decl;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000859
Sean Callanan503aa522011-10-12 00:12:34 +0000860 if (log)
Sean Callananfb3e4302011-10-29 19:50:43 +0000861 log->Printf(" CEDM::FEVD[%u] Searching the root namespace", current_id);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000862
Sean Callanan503aa522011-10-12 00:12:34 +0000863 FindExternalVisibleDecls(context,
864 lldb::ModuleSP(),
865 namespace_decl,
Sean Callanan7dd98122011-10-14 20:34:21 +0000866 current_id);
Sean Callanan503aa522011-10-12 00:12:34 +0000867 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000868
Sean Callananfb3e4302011-10-29 19:50:43 +0000869 if (!context.m_found.variable)
870 ClangASTSource::FindExternalVisibleDecls(context);
Sean Callanan503aa522011-10-12 00:12:34 +0000871}
872
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000873void
874ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context,
Sean Callanan1a2c5382011-10-12 18:44:30 +0000875 lldb::ModuleSP module_sp,
Greg Clayton99558cc42015-08-24 23:46:31 +0000876 CompilerDeclContext &namespace_decl,
Sean Callanan7dd98122011-10-14 20:34:21 +0000877 unsigned int current_id)
Sean Callanan503aa522011-10-12 00:12:34 +0000878{
Sean Callananeddeb3b2011-10-28 23:38:38 +0000879 assert (m_ast_context);
Sean Callananb3a36df2016-03-19 00:51:43 +0000880
881 std::function<void (clang::FunctionDecl *)> MaybeRegisterFunctionBody =
882 [this](clang::FunctionDecl *copied_function_decl)
883 {
884 if (copied_function_decl->getBody() && m_parser_vars->m_code_gen)
885 {
886 DeclGroupRef decl_group_ref(copied_function_decl);
887 m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref);
888 }
889 };
890
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000891
Greg Clayton5160ce52013-03-27 23:08:40 +0000892 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000893
Greg Clayton7a345282010-11-09 23:46:37 +0000894 SymbolContextList sc_list;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000895
Sean Callananfb3e4302011-10-29 19:50:43 +0000896 const ConstString name(context.m_decl_name.getAsString().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000897
Greg Clayton83c5cd92010-11-14 22:13:40 +0000898 const char *name_unique_cstr = name.GetCString();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000899
Greg Clayton83c5cd92010-11-14 22:13:40 +0000900 if (name_unique_cstr == NULL)
901 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000902
Sean Callanan5b26f272012-02-04 08:49:35 +0000903 static ConstString id_name("id");
904 static ConstString Class_name("Class");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000905
Sean Callanan5b26f272012-02-04 08:49:35 +0000906 if (name == id_name || name == Class_name)
907 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000908
909 // Only look for functions by name out in our symbols if the function
Greg Clayton7b462cc2010-10-15 22:48:33 +0000910 // doesn't start with our phony prefix of '$'
Sean Callanan933693b2012-02-10 01:22:05 +0000911 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Jason Molendab57e4a12013-11-04 09:33:30 +0000912 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
Paul Hermand628cbb2015-09-15 23:44:17 +0000913 SymbolContext sym_ctx;
914 if (frame != nullptr)
915 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction|lldb::eSymbolContextBlock);
Sean Callananb3a36df2016-03-19 00:51:43 +0000916
917 // Try the persistent decls, which take precedence over all else.
918 if (!namespace_decl)
919 {
920 do
921 {
922 if (!target)
923 break;
924
925 ClangASTContext *scratch_clang_ast_context = target->GetScratchClangASTContext();
926
927 if (!scratch_clang_ast_context)
928 break;
929
930 ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
931
932 if (!scratch_ast_context)
933 break;
934
935 NamedDecl *persistent_decl = m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
936
937 if (!persistent_decl)
938 break;
939
940 Decl *parser_persistent_decl = m_ast_importer_sp->CopyDecl(m_ast_context, scratch_ast_context, persistent_decl);
941
942 if (!parser_persistent_decl)
943 break;
944
945 NamedDecl *parser_named_decl = dyn_cast<NamedDecl>(parser_persistent_decl);
946
947 if (!parser_named_decl)
948 break;
949
950 if (clang::FunctionDecl *parser_function_decl = llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl))
951 {
952 MaybeRegisterFunctionBody(parser_function_decl);
953 }
954
955 if (log)
956 log->Printf(" CEDM::FEVD[%u] Found persistent decl %s", current_id, name.GetCString());
957
958 context.AddNamedDecl(parser_named_decl);
959 } while (0);
960 }
961
Sean Callanan88972242011-10-12 01:39:28 +0000962 if (name_unique_cstr[0] == '$' && !namespace_decl)
963 {
964 static ConstString g_lldb_class_name ("$__lldb_class");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000965
Sean Callanan88972242011-10-12 01:39:28 +0000966 if (name == g_lldb_class_name)
967 {
968 // Clang is looking for the type of "this"
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000969
Sean Callananc03bdd92012-02-08 03:45:08 +0000970 if (frame == NULL)
Sean Callanan88972242011-10-12 01:39:28 +0000971 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000972
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000973
Greg Clayton99558cc42015-08-24 23:46:31 +0000974 // Find the block that defines the function represented by "sym_ctx"
Greg Clayton685c88c2012-07-14 00:53:55 +0000975 Block *function_block = sym_ctx.GetFunctionBlock();
976
977 if (!function_block)
978 return;
979
Greg Clayton99558cc42015-08-24 23:46:31 +0000980 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000981
Greg Clayton99558cc42015-08-24 23:46:31 +0000982 if (!function_decl_ctx)
Sean Callanan88972242011-10-12 01:39:28 +0000983 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000984
Greg Clayton99558cc42015-08-24 23:46:31 +0000985 clang::CXXMethodDecl *method_decl = ClangASTContext::DeclContextGetAsCXXMethodDecl(function_decl_ctx);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000986
Jim Ingham5fdeed42012-10-30 23:35:54 +0000987 if (method_decl)
Sean Callanan88972242011-10-12 01:39:28 +0000988 {
Jim Ingham5fdeed42012-10-30 23:35:54 +0000989 clang::CXXRecordDecl *class_decl = method_decl->getParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000990
Jim Ingham5fdeed42012-10-30 23:35:54 +0000991 QualType class_qual_type(class_decl->getTypeForDecl(), 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000992
Jim Ingham5fdeed42012-10-30 23:35:54 +0000993 TypeFromUser class_user_type (class_qual_type.getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +0000994 ClangASTContext::GetASTContext(&class_decl->getASTContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000995
Jim Ingham5fdeed42012-10-30 23:35:54 +0000996 if (log)
997 {
998 ASTDumper ast_dumper(class_qual_type);
999 log->Printf(" CEDM::FEVD[%u] Adding type for $__lldb_class: %s", current_id, ast_dumper.GetCString());
1000 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001001
Sean Callanan3b436d22015-10-23 21:45:02 +00001002 AddThisType(context, class_user_type, current_id);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001003
Jim Ingham5fdeed42012-10-30 23:35:54 +00001004 if (method_decl->isInstance())
1005 {
1006 // self is a pointer to the object
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001007
Jim Ingham5fdeed42012-10-30 23:35:54 +00001008 QualType class_pointer_type = method_decl->getASTContext().getPointerType(class_qual_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001009
Jim Ingham5fdeed42012-10-30 23:35:54 +00001010 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +00001011 ClangASTContext::GetASTContext(&method_decl->getASTContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001012
Jim Ingham5fdeed42012-10-30 23:35:54 +00001013 m_struct_vars->m_object_pointer_type = self_user_type;
1014 }
Sean Callanan88972242011-10-12 01:39:28 +00001015 }
Jim Ingham5fdeed42012-10-30 23:35:54 +00001016 else
Sean Callananbb6e7ed2012-03-05 22:08:20 +00001017 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001018 // This branch will get hit if we are executing code in the context of a function that
1019 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a
Bruce Mitchener58ef3912015-06-18 05:27:05 +00001020 // method of the class. In that case, just look up the "this" variable in the current
Jim Ingham5fdeed42012-10-30 23:35:54 +00001021 // scope and use its type.
1022 // FIXME: This code is formally correct, but clang doesn't currently emit DW_AT_object_pointer
1023 // for C++ so it hasn't actually been tested.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001024
Jim Ingham5fdeed42012-10-30 23:35:54 +00001025 VariableList *vars = frame->GetVariableList(false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001026
Jim Ingham5fdeed42012-10-30 23:35:54 +00001027 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001028
Jim Ingham5fdeed42012-10-30 23:35:54 +00001029 if (this_var &&
1030 this_var->IsInScope(frame) &&
1031 this_var->LocationIsValidForFrame (frame))
1032 {
1033 Type *this_type = this_var->GetType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001034
Jim Ingham5fdeed42012-10-30 23:35:54 +00001035 if (!this_type)
1036 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001037
Sean Callanan3b436d22015-10-23 21:45:02 +00001038 TypeFromUser pointee_type = this_type->GetForwardCompilerType ().GetPointeeType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001039
Greg Clayton57ee3062013-07-11 22:46:58 +00001040 if (pointee_type.IsValid())
Jim Ingham5fdeed42012-10-30 23:35:54 +00001041 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001042 if (log)
1043 {
Sean Callanan3b436d22015-10-23 21:45:02 +00001044 ASTDumper ast_dumper(pointee_type);
1045 log->Printf(" FEVD[%u] Adding type for $__lldb_class: %s", current_id, ast_dumper.GetCString());
Jim Ingham5fdeed42012-10-30 23:35:54 +00001046 }
Sean Callanan3b436d22015-10-23 21:45:02 +00001047
1048 AddThisType(context, pointee_type, current_id);
Greg Clayton99558cc42015-08-24 23:46:31 +00001049 TypeFromUser this_user_type(this_type->GetFullCompilerType ());
Jim Ingham5fdeed42012-10-30 23:35:54 +00001050 m_struct_vars->m_object_pointer_type = this_user_type;
1051 return;
1052 }
1053 }
Sean Callananbb6e7ed2012-03-05 22:08:20 +00001054 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001055
Sean Callanan88972242011-10-12 01:39:28 +00001056 return;
1057 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001058
Sean Callanan88972242011-10-12 01:39:28 +00001059 static ConstString g_lldb_objc_class_name ("$__lldb_objc_class");
1060 if (name == g_lldb_objc_class_name)
1061 {
1062 // Clang is looking for the type of "*self"
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001063
Sean Callanan88972242011-10-12 01:39:28 +00001064 if (!frame)
1065 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001066
Greg Clayton99558cc42015-08-24 23:46:31 +00001067 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction|lldb::eSymbolContextBlock);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001068
Greg Clayton99558cc42015-08-24 23:46:31 +00001069 // Find the block that defines the function represented by "sym_ctx"
Greg Clayton685c88c2012-07-14 00:53:55 +00001070 Block *function_block = sym_ctx.GetFunctionBlock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001071
Greg Clayton685c88c2012-07-14 00:53:55 +00001072 if (!function_block)
1073 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001074
Greg Clayton99558cc42015-08-24 23:46:31 +00001075 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001076
Greg Clayton99558cc42015-08-24 23:46:31 +00001077 if (!function_decl_ctx)
Sean Callanand5c17ed2011-11-15 02:11:17 +00001078 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001079
Greg Clayton99558cc42015-08-24 23:46:31 +00001080 clang::ObjCMethodDecl *method_decl = ClangASTContext::DeclContextGetAsObjCMethodDecl(function_decl_ctx);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001081
Jim Ingham5fdeed42012-10-30 23:35:54 +00001082 if (method_decl)
Sean Callanand5c17ed2011-11-15 02:11:17 +00001083 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001084 ObjCInterfaceDecl* self_interface = method_decl->getClassInterface();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001085
Jim Ingham5fdeed42012-10-30 23:35:54 +00001086 if (!self_interface)
Sean Callananc03bdd92012-02-08 03:45:08 +00001087 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001088
Jim Ingham5fdeed42012-10-30 23:35:54 +00001089 const clang::Type *interface_type = self_interface->getTypeForDecl();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001090
Sean Callanandfb87d62013-04-09 21:30:48 +00001091 if (!interface_type)
1092 return; // This is unlikely, but we have seen crashes where this occurred
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001093
Jim Ingham5fdeed42012-10-30 23:35:54 +00001094 TypeFromUser class_user_type(QualType(interface_type, 0).getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +00001095 ClangASTContext::GetASTContext(&method_decl->getASTContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001096
Jim Ingham5fdeed42012-10-30 23:35:54 +00001097 if (log)
1098 {
1099 ASTDumper ast_dumper(interface_type);
1100 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1101 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001102
Sean Callananfa4fab72013-02-01 06:55:48 +00001103 AddOneType(context, class_user_type, current_id);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001104
Jim Ingham5fdeed42012-10-30 23:35:54 +00001105 if (method_decl->isInstanceMethod())
1106 {
1107 // self is a pointer to the object
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001108
Jim Ingham5fdeed42012-10-30 23:35:54 +00001109 QualType class_pointer_type = method_decl->getASTContext().getObjCObjectPointerType(QualType(interface_type, 0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001110
Jim Ingham5fdeed42012-10-30 23:35:54 +00001111 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +00001112 ClangASTContext::GetASTContext(&method_decl->getASTContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001113
Jim Ingham5fdeed42012-10-30 23:35:54 +00001114 m_struct_vars->m_object_pointer_type = self_user_type;
1115 }
1116 else
1117 {
1118 // self is a Class pointer
1119 QualType class_type = method_decl->getASTContext().getObjCClassType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001120
Jim Ingham5fdeed42012-10-30 23:35:54 +00001121 TypeFromUser self_user_type(class_type.getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +00001122 ClangASTContext::GetASTContext(&method_decl->getASTContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001123
Jim Ingham5fdeed42012-10-30 23:35:54 +00001124 m_struct_vars->m_object_pointer_type = self_user_type;
1125 }
1126
1127 return;
Sean Callananc03bdd92012-02-08 03:45:08 +00001128 }
1129 else
1130 {
Jim Ingham5fdeed42012-10-30 23:35:54 +00001131 // This branch will get hit if we are executing code in the context of a function that
1132 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a
Bruce Mitchener58ef3912015-06-18 05:27:05 +00001133 // method of the class. In that case, just look up the "self" variable in the current
Jim Ingham5fdeed42012-10-30 23:35:54 +00001134 // scope and use its type.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001135
Jim Ingham5fdeed42012-10-30 23:35:54 +00001136 VariableList *vars = frame->GetVariableList(false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001137
Jim Ingham5fdeed42012-10-30 23:35:54 +00001138 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001139
Jim Ingham5fdeed42012-10-30 23:35:54 +00001140 if (self_var &&
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001141 self_var->IsInScope(frame) &&
Jim Ingham5fdeed42012-10-30 23:35:54 +00001142 self_var->LocationIsValidForFrame (frame))
1143 {
1144 Type *self_type = self_var->GetType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001145
Jim Ingham5fdeed42012-10-30 23:35:54 +00001146 if (!self_type)
1147 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001148
Greg Clayton99558cc42015-08-24 23:46:31 +00001149 CompilerType self_clang_type = self_type->GetFullCompilerType ();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001150
Greg Claytond8d4a572015-08-11 21:38:15 +00001151 if (ClangASTContext::IsObjCClassType(self_clang_type))
Jim Ingham5fdeed42012-10-30 23:35:54 +00001152 {
Sean Callanana2868d42013-01-19 01:49:02 +00001153 return;
1154 }
Greg Claytond8d4a572015-08-11 21:38:15 +00001155 else if (ClangASTContext::IsObjCObjectPointerType(self_clang_type))
Sean Callanana2868d42013-01-19 01:49:02 +00001156 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001157 self_clang_type = self_clang_type.GetPointeeType();
1158
1159 if (!self_clang_type)
Sean Callanane5270592013-04-26 22:54:19 +00001160 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001161
Jim Ingham5fdeed42012-10-30 23:35:54 +00001162 if (log)
1163 {
Greg Clayton99558cc42015-08-24 23:46:31 +00001164 ASTDumper ast_dumper(self_type->GetFullCompilerType ());
Jim Ingham5fdeed42012-10-30 23:35:54 +00001165 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1166 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001167
Greg Clayton57ee3062013-07-11 22:46:58 +00001168 TypeFromUser class_user_type (self_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001169
Sean Callananfa4fab72013-02-01 06:55:48 +00001170 AddOneType(context, class_user_type, current_id);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001171
Greg Clayton99558cc42015-08-24 23:46:31 +00001172 TypeFromUser self_user_type(self_type->GetFullCompilerType ());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001173
Jim Ingham5fdeed42012-10-30 23:35:54 +00001174 m_struct_vars->m_object_pointer_type = self_user_type;
1175 return;
1176 }
1177 }
Sean Callananc03bdd92012-02-08 03:45:08 +00001178 }
1179
Sean Callanan88972242011-10-12 01:39:28 +00001180 return;
1181 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001182
Siva Chandra03ff5c82016-02-05 19:10:04 +00001183 if (name == ConstString(g_lldb_local_vars_namespace_cstr))
1184 {
1185 CompilerDeclContext frame_decl_context = sym_ctx.block != nullptr ?
1186 sym_ctx.block->GetDeclContext() :
1187 CompilerDeclContext();
1188
1189 if (frame_decl_context)
1190 {
1191 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(frame_decl_context.GetTypeSystem());
1192
1193 if (ast)
1194 {
1195 clang::NamespaceDecl *namespace_decl = ClangASTContext::GetUniqueNamespaceDeclaration(
1196 m_ast_context, name_unique_cstr, nullptr);
1197 if (namespace_decl)
1198 {
1199 context.AddNamedDecl(namespace_decl);
1200 clang::DeclContext *clang_decl_ctx = clang::Decl::castToDeclContext(namespace_decl);
1201 clang_decl_ctx->setHasExternalVisibleStorage(true);
1202 }
1203 }
1204 }
1205
1206 return;
1207 }
1208
Sean Callanan88972242011-10-12 01:39:28 +00001209 // any other $__lldb names should be weeded out now
1210 if (!::strncmp(name_unique_cstr, "$__lldb", sizeof("$__lldb") - 1))
1211 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001212
Sean Callananbc8ac342015-09-04 20:49:51 +00001213 ExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariable(name));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001214
Sean Callanan88972242011-10-12 01:39:28 +00001215 if (pvar_sp)
1216 {
Sean Callanan7dd98122011-10-14 20:34:21 +00001217 AddOneVariable(context, pvar_sp, current_id);
Sean Callanan88972242011-10-12 01:39:28 +00001218 return;
1219 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001220
Sean Callanan88972242011-10-12 01:39:28 +00001221 const char *reg_name(&name.GetCString()[1]);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001222
Sean Callanan933693b2012-02-10 01:22:05 +00001223 if (m_parser_vars->m_exe_ctx.GetRegisterContext())
Sean Callanan88972242011-10-12 01:39:28 +00001224 {
Sean Callanan933693b2012-02-10 01:22:05 +00001225 const RegisterInfo *reg_info(m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(reg_name));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001226
Sean Callanan88972242011-10-12 01:39:28 +00001227 if (reg_info)
Sean Callanan35c7f982011-10-18 16:46:55 +00001228 {
1229 if (log)
Sean Callananfb3e4302011-10-29 19:50:43 +00001230 log->Printf(" CEDM::FEVD[%u] Found register %s", current_id, reg_info->name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001231
Sean Callanan7dd98122011-10-14 20:34:21 +00001232 AddOneRegister(context, reg_info, current_id);
Sean Callanan35c7f982011-10-18 16:46:55 +00001233 }
Sean Callanan88972242011-10-12 01:39:28 +00001234 }
1235 }
1236 else
Sean Callanan8ade1042010-07-27 00:55:47 +00001237 {
Sean Callanan69b53412011-08-06 00:28:14 +00001238 ValueObjectSP valobj;
1239 VariableSP var;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001240
Siva Chandra03ff5c82016-02-05 19:10:04 +00001241 bool local_var_lookup = !namespace_decl ||
1242 (namespace_decl.GetName() == ConstString(g_lldb_local_vars_namespace_cstr));
1243 if (frame && local_var_lookup)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001244 {
Paul Hermand628cbb2015-09-15 23:44:17 +00001245 CompilerDeclContext compiler_decl_context = sym_ctx.block != nullptr ? sym_ctx.block->GetDeclContext() : CompilerDeclContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001246
Paul Hermand628cbb2015-09-15 23:44:17 +00001247 if (compiler_decl_context)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001248 {
Siva Chandra03ff5c82016-02-05 19:10:04 +00001249 // Make sure that the variables are parsed so that we have the declarations.
Paul Hermand628cbb2015-09-15 23:44:17 +00001250 VariableListSP vars = frame->GetInScopeVariableList(true);
1251 for (size_t i = 0; i < vars->GetSize(); i++)
1252 vars->GetVariableAtIndex(i)->GetDecl();
1253
Siva Chandra03ff5c82016-02-05 19:10:04 +00001254 // Search for declarations matching the name. Do not include imported decls
1255 // in the search if we are looking for decls in the artificial namespace
1256 // $__lldb_local_vars.
1257 std::vector<CompilerDecl> found_decls = compiler_decl_context.FindDeclByName(name, namespace_decl.IsValid());
Paul Hermanea188fc2015-09-16 18:48:30 +00001258
1259 bool variable_found = false;
Paul Hermand628cbb2015-09-15 23:44:17 +00001260 for (CompilerDecl decl : found_decls)
1261 {
1262 var = decl.GetAsVariable();
1263 if (var)
1264 {
Paul Hermanea188fc2015-09-16 18:48:30 +00001265 variable_found = true;
Paul Hermand628cbb2015-09-15 23:44:17 +00001266 valobj = ValueObjectVariable::Create(frame, var);
1267 AddOneVariable(context, var, valobj, current_id);
1268 context.m_found.variable = true;
Paul Hermand628cbb2015-09-15 23:44:17 +00001269 }
1270 }
Paul Hermanea188fc2015-09-16 18:48:30 +00001271 if (variable_found)
1272 return;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001273 }
Greg Clayton7b462cc2010-10-15 22:48:33 +00001274 }
Sean Callananfd1ba912011-12-10 04:03:38 +00001275 if (target)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001276 {
Sean Callanan1fd3f4f2011-10-12 16:59:31 +00001277 var = FindGlobalVariable (*target,
Sean Callanan1a2c5382011-10-12 18:44:30 +00001278 module_sp,
Sean Callanan1fd3f4f2011-10-12 16:59:31 +00001279 name,
1280 &namespace_decl,
Greg Claytonc14ee322011-09-22 04:58:26 +00001281 NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001282
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001283 if (var)
1284 {
Sean Callanan9b3569b2011-12-10 03:12:34 +00001285 valobj = ValueObjectVariable::Create(target, var);
Sean Callanan0712f462011-11-29 22:03:21 +00001286 AddOneVariable(context, var, valobj, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001287 context.m_found.variable = true;
Sean Callananfd1ba912011-12-10 04:03:38 +00001288 return;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001289 }
1290 }
Sean Callananc8278af2014-12-05 01:27:35 +00001291
1292 std::vector<clang::NamedDecl *> decls_from_modules;
1293
1294 if (target)
1295 {
1296 if (ClangModulesDeclVendor *decl_vendor = target->GetClangModulesDeclVendor())
1297 {
1298 decl_vendor->FindDecls(name, false, UINT32_MAX, decls_from_modules);
1299 }
1300 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001301
Sean Callananca4e0fd2011-10-12 20:29:25 +00001302 if (!context.m_found.variable)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001303 {
Sean Callanan9df05fb2012-02-10 22:52:19 +00001304 const bool include_inlines = false;
Greg Clayton931180e2011-01-27 06:44:37 +00001305 const bool append = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001306
Sean Callanan1a2c5382011-10-12 18:44:30 +00001307 if (namespace_decl && module_sp)
Sean Callanan25ea08e2011-10-12 17:38:09 +00001308 {
Sean Callanan3ae61792012-02-15 17:14:49 +00001309 const bool include_symbols = false;
1310
Sean Callanan1a2c5382011-10-12 18:44:30 +00001311 module_sp->FindFunctions(name,
1312 &namespace_decl,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001313 eFunctionNameTypeBase,
Sean Callanan1a2c5382011-10-12 18:44:30 +00001314 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001315 include_inlines,
Sean Callanan1a2c5382011-10-12 18:44:30 +00001316 append,
1317 sc_list);
Sean Callanan25ea08e2011-10-12 17:38:09 +00001318 }
Jim Ingham28eb5712012-10-12 17:34:26 +00001319 else if (target && !namespace_decl)
Sean Callanan25ea08e2011-10-12 17:38:09 +00001320 {
Sean Callanan3ae61792012-02-15 17:14:49 +00001321 const bool include_symbols = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001322
Sean Callanan308a3c52012-07-28 00:21:01 +00001323 // TODO Fix FindFunctions so that it doesn't return
1324 // instance methods for eFunctionNameTypeBase.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001325
Sean Callanan25ea08e2011-10-12 17:38:09 +00001326 target->GetImages().FindFunctions(name,
Greg Clayton43fe2172013-04-03 02:00:15 +00001327 eFunctionNameTypeFull,
Sean Callanan25ea08e2011-10-12 17:38:09 +00001328 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001329 include_inlines,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001330 append,
Sean Callanan25ea08e2011-10-12 17:38:09 +00001331 sc_list);
1332 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001333
Dawn Perchikb5925782015-12-12 19:31:41 +00001334 // If we found more than one function, see if we can use the
1335 // frame's decl context to remove functions that are shadowed
1336 // by other functions which match in type but are nearer in scope.
1337 //
1338 // AddOneFunction will not add a function whose type has already been
1339 // added, so if there's another function in the list with a matching
1340 // type, check to see if their decl context is a parent of the current
1341 // frame's or was imported via a and using statement, and pick the
1342 // best match according to lookup rules.
1343 if (sc_list.GetSize() > 1)
1344 {
1345 // Collect some info about our frame's context.
1346 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1347 SymbolContext frame_sym_ctx;
1348 if (frame != nullptr)
1349 frame_sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction|lldb::eSymbolContextBlock);
1350 CompilerDeclContext frame_decl_context = frame_sym_ctx.block != nullptr ? frame_sym_ctx.block->GetDeclContext() : CompilerDeclContext();
1351
1352 // We can't do this without a compiler decl context for our frame.
1353 if (frame_decl_context)
1354 {
1355 clang::DeclContext *frame_decl_ctx = (clang::DeclContext *)frame_decl_context.GetOpaqueDeclContext();
1356 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(frame_decl_context.GetTypeSystem());
1357
1358 // Structure to hold the info needed when comparing function
1359 // declarations.
1360 struct FuncDeclInfo
1361 {
1362 ConstString m_name;
1363 CompilerType m_copied_type;
1364 uint32_t m_decl_lvl;
1365 SymbolContext m_sym_ctx;
1366 };
1367
1368 // First, symplify things by looping through the symbol contexts
1369 // to remove unwanted functions and separate out the functions we
1370 // want to compare and prune into a separate list.
1371 // Cache the info needed about the function declarations in a
1372 // vector for efficiency.
1373 SymbolContextList sc_sym_list;
1374 uint32_t num_indices = sc_list.GetSize();
1375 std::vector<FuncDeclInfo> fdi_cache;
1376 fdi_cache.reserve(num_indices);
1377 for (uint32_t index = 0; index < num_indices; ++index)
1378 {
1379 FuncDeclInfo fdi;
1380 SymbolContext sym_ctx;
1381 sc_list.GetContextAtIndex(index, sym_ctx);
1382
1383 // We don't know enough about symbols to compare them,
1384 // but we should keep them in the list.
1385 Function *function = sym_ctx.function;
1386 if (!function)
1387 {
1388 sc_sym_list.Append(sym_ctx);
1389 continue;
1390 }
1391 // Filter out functions without declaration contexts, as well as
1392 // class/instance methods, since they'll be skipped in the
1393 // code that follows anyway.
1394 CompilerDeclContext func_decl_context = function->GetDeclContext();
1395 if (!func_decl_context || func_decl_context.IsClassMethod(nullptr, nullptr, nullptr))
1396 continue;
1397 // We can only prune functions for which we can copy the type.
1398 CompilerType func_clang_type = function->GetType()->GetFullCompilerType();
1399 CompilerType copied_func_type = GuardedCopyType(func_clang_type);
1400 if (!copied_func_type)
1401 {
1402 sc_sym_list.Append(sym_ctx);
1403 continue;
1404 }
1405
1406 fdi.m_sym_ctx = sym_ctx;
1407 fdi.m_name = function->GetName();
1408 fdi.m_copied_type = copied_func_type;
1409 fdi.m_decl_lvl = LLDB_INVALID_DECL_LEVEL;
1410 if (fdi.m_copied_type && func_decl_context)
1411 {
1412 // Call CountDeclLevels to get the number of parent scopes we
1413 // have to look through before we find the function declaration.
1414 // When comparing functions of the same type, the one with a
1415 // lower count will be closer to us in the lookup scope and
1416 // shadows the other.
1417 clang::DeclContext *func_decl_ctx = (clang::DeclContext *)func_decl_context.GetOpaqueDeclContext();
1418 fdi.m_decl_lvl = ast->CountDeclLevels(frame_decl_ctx,
1419 func_decl_ctx,
1420 &fdi.m_name,
1421 &fdi.m_copied_type);
1422 }
1423 fdi_cache.emplace_back(fdi);
1424 }
1425
1426 // Loop through the functions in our cache looking for matching types,
1427 // then compare their scope levels to see which is closer.
1428 std::multimap<CompilerType, const FuncDeclInfo*> matches;
1429 for (const FuncDeclInfo &fdi : fdi_cache)
1430 {
1431 const CompilerType t = fdi.m_copied_type;
1432 auto q = matches.find(t);
1433 if (q != matches.end())
1434 {
1435 if (q->second->m_decl_lvl > fdi.m_decl_lvl)
1436 // This function is closer; remove the old set.
1437 matches.erase(t);
1438 else if (q->second->m_decl_lvl < fdi.m_decl_lvl)
1439 // The functions in our set are closer - skip this one.
1440 continue;
1441 }
1442 matches.insert(std::make_pair(t, &fdi));
1443 }
1444
1445 // Loop through our matches and add their symbol contexts to our list.
1446 SymbolContextList sc_func_list;
1447 for (const auto &q : matches)
1448 sc_func_list.Append(q.second->m_sym_ctx);
1449
1450 // Rejoin the lists with the functions in front.
1451 sc_list = sc_func_list;
1452 sc_list.Append(sc_sym_list);
1453 }
1454 }
1455
Sean Callanand9ca42a2011-05-08 02:21:26 +00001456 if (sc_list.GetSize())
Greg Clayton7b462cc2010-10-15 22:48:33 +00001457 {
Greg Claytondc25a0b2013-10-31 16:59:47 +00001458 Symbol *extern_symbol = NULL;
1459 Symbol *non_extern_symbol = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001460
Sean Callanand9ca42a2011-05-08 02:21:26 +00001461 for (uint32_t index = 0, num_indices = sc_list.GetSize();
1462 index < num_indices;
1463 ++index)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001464 {
Sean Callanand9ca42a2011-05-08 02:21:26 +00001465 SymbolContext sym_ctx;
1466 sc_list.GetContextAtIndex(index, sym_ctx);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001467
Sean Callanand9ca42a2011-05-08 02:21:26 +00001468 if (sym_ctx.function)
1469 {
Greg Clayton99558cc42015-08-24 23:46:31 +00001470 CompilerDeclContext decl_ctx = sym_ctx.function->GetDeclContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001471
Sean Callanan956dca92013-04-27 01:57:44 +00001472 if (!decl_ctx)
1473 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001474
Sean Callanan308a3c52012-07-28 00:21:01 +00001475 // Filter out class/instance methods.
Greg Clayton99558cc42015-08-24 23:46:31 +00001476 if (decl_ctx.IsClassMethod(nullptr, nullptr, nullptr))
Sean Callanan308a3c52012-07-28 00:21:01 +00001477 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001478
Sean Callanan485f7322013-04-24 00:34:41 +00001479 AddOneFunction(context, sym_ctx.function, NULL, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001480 context.m_found.function_with_type_info = true;
1481 context.m_found.function = true;
Sean Callanand9ca42a2011-05-08 02:21:26 +00001482 }
1483 else if (sym_ctx.symbol)
1484 {
Greg Claytonfc56a012014-06-13 21:57:58 +00001485 if (sym_ctx.symbol->GetType() == eSymbolTypeReExported && target)
Greg Claytondc25a0b2013-10-31 16:59:47 +00001486 {
1487 sym_ctx.symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target);
1488 if (sym_ctx.symbol == NULL)
1489 continue;
1490 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001491
Greg Claytondc25a0b2013-10-31 16:59:47 +00001492 if (sym_ctx.symbol->IsExternal())
1493 extern_symbol = sym_ctx.symbol;
Sean Callanand9ca42a2011-05-08 02:21:26 +00001494 else
Greg Claytondc25a0b2013-10-31 16:59:47 +00001495 non_extern_symbol = sym_ctx.symbol;
Sean Callanand9ca42a2011-05-08 02:21:26 +00001496 }
Greg Clayton7b462cc2010-10-15 22:48:33 +00001497 }
Sean Callananc8278af2014-12-05 01:27:35 +00001498
1499 if (!context.m_found.function_with_type_info)
1500 {
1501 for (clang::NamedDecl *decl : decls_from_modules)
1502 {
1503 if (llvm::isa<clang::FunctionDecl>(decl))
1504 {
Sean Callanana3444ff2015-11-10 22:54:42 +00001505 clang::NamedDecl *copied_decl = llvm::cast<FunctionDecl>(m_ast_importer_sp->CopyDecl(m_ast_context, &decl->getASTContext(), decl));
Sean Callananc8278af2014-12-05 01:27:35 +00001506 context.AddNamedDecl(copied_decl);
1507 context.m_found.function_with_type_info = true;
1508 }
1509 }
1510 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001511
Sean Callananca4e0fd2011-10-12 20:29:25 +00001512 if (!context.m_found.function_with_type_info)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001513 {
Greg Claytondc25a0b2013-10-31 16:59:47 +00001514 if (extern_symbol)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001515 {
Greg Claytondc25a0b2013-10-31 16:59:47 +00001516 AddOneFunction (context, NULL, extern_symbol, current_id);
1517 context.m_found.function = true;
1518 }
1519 else if (non_extern_symbol)
1520 {
1521 AddOneFunction (context, NULL, non_extern_symbol, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001522 context.m_found.function = true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001523 }
Sean Callanand9ca42a2011-05-08 02:21:26 +00001524 }
Greg Clayton7b462cc2010-10-15 22:48:33 +00001525 }
Sean Callanan80c97592015-05-01 00:47:29 +00001526
1527 if (!context.m_found.function_with_type_info)
1528 {
1529 // Try the modules next.
1530
1531 do
1532 {
1533 if (ClangModulesDeclVendor *modules_decl_vendor = m_target->GetClangModulesDeclVendor())
1534 {
1535 bool append = false;
1536 uint32_t max_matches = 1;
1537 std::vector <clang::NamedDecl *> decls;
1538
1539 if (!modules_decl_vendor->FindDecls(name,
1540 append,
1541 max_matches,
1542 decls))
1543 break;
1544
1545 clang::NamedDecl *const decl_from_modules = decls[0];
1546
1547 if (llvm::isa<clang::FunctionDecl>(decl_from_modules))
1548 {
1549 if (log)
1550 {
1551 log->Printf(" CAS::FEVD[%u] Matching function found for \"%s\" in the modules",
1552 current_id,
1553 name.GetCString());
1554 }
1555
Sean Callanana3444ff2015-11-10 22:54:42 +00001556 clang::Decl *copied_decl = m_ast_importer_sp->CopyDecl(m_ast_context, &decl_from_modules->getASTContext(), decl_from_modules);
Sean Callanan80c97592015-05-01 00:47:29 +00001557 clang::FunctionDecl *copied_function_decl = copied_decl ? dyn_cast<clang::FunctionDecl>(copied_decl) : nullptr;
1558
1559 if (!copied_function_decl)
1560 {
1561 if (log)
1562 log->Printf(" CAS::FEVD[%u] - Couldn't export a function declaration from the modules",
1563 current_id);
1564
1565 break;
1566 }
1567
Sean Callananb3a36df2016-03-19 00:51:43 +00001568 MaybeRegisterFunctionBody(copied_function_decl);
Sean Callanan80c97592015-05-01 00:47:29 +00001569
1570 context.AddNamedDecl(copied_function_decl);
1571
1572 context.m_found.function_with_type_info = true;
1573 context.m_found.function = true;
1574 }
Sean Callanan9ff456c2015-07-10 17:34:23 +00001575 else if (llvm::isa<clang::VarDecl>(decl_from_modules))
1576 {
1577 if (log)
1578 {
1579 log->Printf(" CAS::FEVD[%u] Matching variable found for \"%s\" in the modules",
1580 current_id,
1581 name.GetCString());
1582 }
1583
Sean Callanana3444ff2015-11-10 22:54:42 +00001584 clang::Decl *copied_decl = m_ast_importer_sp->CopyDecl(m_ast_context, &decl_from_modules->getASTContext(), decl_from_modules);
Sean Callanan9ff456c2015-07-10 17:34:23 +00001585 clang::VarDecl *copied_var_decl = copied_decl ? dyn_cast_or_null<clang::VarDecl>(copied_decl) : nullptr;
1586
1587 if (!copied_var_decl)
1588 {
1589 if (log)
1590 log->Printf(" CAS::FEVD[%u] - Couldn't export a variable declaration from the modules",
1591 current_id);
1592
1593 break;
1594 }
1595
1596 context.AddNamedDecl(copied_var_decl);
1597
1598 context.m_found.variable = true;
1599 }
Sean Callanan80c97592015-05-01 00:47:29 +00001600 }
1601 } while (0);
1602 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001603
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001604 if (target && !context.m_found.variable && !namespace_decl)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001605 {
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001606 // We couldn't find a non-symbol variable for this. Now we'll hunt for a generic
Sean Callanand9ca42a2011-05-08 02:21:26 +00001607 // data symbol, and -- if it is found -- treat it as a variable.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001608
Greg Clayton3d51b9f2012-11-27 01:52:16 +00001609 const Symbol *data_symbol = FindGlobalDataSymbol(*target, name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001610
Sean Callanand9ca42a2011-05-08 02:21:26 +00001611 if (data_symbol)
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001612 {
Sean Callanan92cdbc82014-02-19 23:37:25 +00001613 std::string warning("got name from symbols: ");
1614 warning.append(name.AsCString());
1615 const unsigned diag_id = m_ast_context->getDiagnostics().getCustomDiagID(clang::DiagnosticsEngine::Level::Warning, "%0");
1616 m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str();
Sean Callanan7dd98122011-10-14 20:34:21 +00001617 AddOneGenericVariable(context, *data_symbol, current_id);
Sean Callananca4e0fd2011-10-12 20:29:25 +00001618 context.m_found.variable = true;
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001619 }
Greg Clayton526e5af2010-11-13 03:52:47 +00001620 }
Sean Callanan3883b5a2010-09-07 21:49:41 +00001621 }
Sean Callananfb40b0d2011-10-27 02:06:03 +00001622 }
Sean Callanan6b4067c2010-07-17 00:43:37 +00001623}
Greg Claytona2721472011-06-25 00:44:06 +00001624
Bruce Mitchener23a3b0e2015-09-22 17:04:24 +00001625//static opaque_compiler_type_t
Saleem Abdulrasoola68f7b62014-03-20 06:08:36 +00001626//MaybePromoteToBlockPointerType
1627//(
1628// ASTContext *ast_context,
Bruce Mitchener23a3b0e2015-09-22 17:04:24 +00001629// opaque_compiler_type_t candidate_type
Saleem Abdulrasoola68f7b62014-03-20 06:08:36 +00001630//)
1631//{
1632// if (!candidate_type)
1633// return candidate_type;
1634//
1635// QualType candidate_qual_type = QualType::getFromOpaquePtr(candidate_type);
1636//
1637// const PointerType *candidate_pointer_type = dyn_cast<PointerType>(candidate_qual_type);
1638//
1639// if (!candidate_pointer_type)
1640// return candidate_type;
1641//
1642// QualType pointee_qual_type = candidate_pointer_type->getPointeeType();
1643//
1644// const RecordType *pointee_record_type = dyn_cast<RecordType>(pointee_qual_type);
1645//
1646// if (!pointee_record_type)
1647// return candidate_type;
1648//
1649// RecordDecl *pointee_record_decl = pointee_record_type->getDecl();
1650//
1651// if (!pointee_record_decl->isRecord())
1652// return candidate_type;
1653//
1654// if (!pointee_record_decl->getName().startswith(llvm::StringRef("__block_literal_")))
1655// return candidate_type;
1656//
1657// QualType generic_function_type = ast_context->getFunctionNoProtoType(ast_context->UnknownAnyTy);
1658// QualType block_pointer_type = ast_context->getBlockPointerType(generic_function_type);
1659//
1660// return block_pointer_type.getAsOpaquePtr();
1661//}
Sean Callanan75383bf2012-03-06 21:56:33 +00001662
Greg Clayton57ee3062013-07-11 22:46:58 +00001663bool
1664ClangExpressionDeclMap::GetVariableValue (VariableSP &var,
1665 lldb_private::Value &var_location,
1666 TypeFromUser *user_type,
1667 TypeFromParser *parser_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001668{
Greg Clayton5160ce52013-03-27 23:08:40 +00001669 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001670
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001671 Type *var_type = var->GetType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001672
1673 if (!var_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001674 {
Sean Callananea22d422010-07-16 00:09:46 +00001675 if (log)
1676 log->PutCString("Skipped a definition because it has no type");
Greg Clayton57ee3062013-07-11 22:46:58 +00001677 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001678 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001679
Greg Clayton99558cc42015-08-24 23:46:31 +00001680 CompilerType var_clang_type = var_type->GetFullCompilerType ();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001681
Greg Clayton57ee3062013-07-11 22:46:58 +00001682 if (!var_clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001683 {
Sean Callananea22d422010-07-16 00:09:46 +00001684 if (log)
1685 log->PutCString("Skipped a definition because it has no Clang type");
Greg Clayton57ee3062013-07-11 22:46:58 +00001686 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001687 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001688
Greg Clayton56939cb2015-09-17 22:23:34 +00001689 ClangASTContext *clang_ast = llvm::dyn_cast_or_null<ClangASTContext>(var_type->GetForwardCompilerType().GetTypeSystem());
1690
1691 if (!clang_ast)
1692 {
1693 if (log)
1694 log->PutCString("Skipped a definition because it has no Clang AST");
1695 return false;
1696 }
1697
1698
1699 ASTContext *ast = clang_ast->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001700
Greg Clayton6beaaa62011-01-17 03:46:26 +00001701 if (!ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001702 {
Sean Callananea22d422010-07-16 00:09:46 +00001703 if (log)
1704 log->PutCString("There is no AST context for the current execution context");
Greg Clayton57ee3062013-07-11 22:46:58 +00001705 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001706 }
Greg Clayton57ee3062013-07-11 22:46:58 +00001707 //var_clang_type = MaybePromoteToBlockPointerType (ast, var_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001708
Sean Callanan6b4067c2010-07-17 00:43:37 +00001709 DWARFExpression &var_location_expr = var->LocationExpression();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001710
Sean Callanan933693b2012-02-10 01:22:05 +00001711 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001712 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001713
Sean Callanan7b5805d2013-01-18 21:20:51 +00001714 if (var->GetLocationIsConstantValueData())
1715 {
1716 DataExtractor const_value_extractor;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001717
Sean Callanan7b5805d2013-01-18 21:20:51 +00001718 if (var_location_expr.GetExpressionData(const_value_extractor))
1719 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001720 var_location = Value(const_value_extractor.GetDataStart(), const_value_extractor.GetByteSize());
1721 var_location.SetValueType(Value::eValueTypeHostAddress);
Sean Callanan7b5805d2013-01-18 21:20:51 +00001722 }
1723 else
1724 {
1725 if (log)
1726 log->Printf("Error evaluating constant variable: %s", err.AsCString());
Greg Clayton57ee3062013-07-11 22:46:58 +00001727 return false;
Sean Callanan7b5805d2013-01-18 21:20:51 +00001728 }
1729 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001730
Greg Claytona1e5dc82015-08-11 22:53:00 +00001731 CompilerType type_to_use = GuardedCopyType(var_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001732
Greg Clayton57ee3062013-07-11 22:46:58 +00001733 if (!type_to_use)
Sean Callanan1d180662010-07-20 23:31:16 +00001734 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001735 if (log)
1736 log->Printf("Couldn't copy a variable's type into the parser's AST context");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001737
Greg Clayton57ee3062013-07-11 22:46:58 +00001738 return false;
Sean Callanan1d180662010-07-20 23:31:16 +00001739 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001740
Greg Clayton57ee3062013-07-11 22:46:58 +00001741 if (parser_type)
1742 *parser_type = TypeFromParser(type_to_use);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001743
Greg Clayton57ee3062013-07-11 22:46:58 +00001744 if (var_location.GetContextType() == Value::eContextTypeInvalid)
Greg Clayton99558cc42015-08-24 23:46:31 +00001745 var_location.SetCompilerType(type_to_use);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001746
Greg Clayton57ee3062013-07-11 22:46:58 +00001747 if (var_location.GetValueType() == Value::eValueTypeFileAddress)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001748 {
1749 SymbolContext var_sc;
1750 var->CalculateSymbolContext(&var_sc);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001751
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001752 if (!var_sc.module_sp)
Michael Sartain89c862f2013-08-07 19:05:15 +00001753 return false;
Greg Clayton3046e662013-07-10 01:23:25 +00001754
Greg Clayton57ee3062013-07-11 22:46:58 +00001755 Address so_addr(var_location.GetScalar().ULongLong(), var_sc.module_sp->GetSectionList());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001756
Greg Claytonc14ee322011-09-22 04:58:26 +00001757 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001758
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001759 if (load_addr != LLDB_INVALID_ADDRESS)
1760 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001761 var_location.GetScalar() = load_addr;
1762 var_location.SetValueType(Value::eValueTypeLoadAddress);
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001763 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001764 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001765
Sean Callanan1d180662010-07-20 23:31:16 +00001766 if (user_type)
Greg Clayton57ee3062013-07-11 22:46:58 +00001767 *user_type = TypeFromUser(var_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001768
Greg Clayton57ee3062013-07-11 22:46:58 +00001769 return true;
Sean Callanan6b4067c2010-07-17 00:43:37 +00001770}
1771
1772void
Sean Callanan0712f462011-11-29 22:03:21 +00001773ClangExpressionDeclMap::AddOneVariable (NameSearchContext &context, VariableSP var, ValueObjectSP valobj, unsigned int current_id)
Sean Callanan6b4067c2010-07-17 00:43:37 +00001774{
Sean Callanan979f74d2010-12-03 01:38:59 +00001775 assert (m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001776
Greg Clayton5160ce52013-03-27 23:08:40 +00001777 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001778
Sean Callanan1d180662010-07-20 23:31:16 +00001779 TypeFromUser ut;
1780 TypeFromParser pt;
Greg Clayton57ee3062013-07-11 22:46:58 +00001781 Value var_location;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001782
Greg Clayton57ee3062013-07-11 22:46:58 +00001783 if (!GetVariableValue (var, var_location, &ut, &pt))
1784 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001785
Sean Callanancf128622012-03-15 01:53:17 +00001786 clang::QualType parser_opaque_type = QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001787
Sean Callanane2d47482012-03-21 17:13:20 +00001788 if (parser_opaque_type.isNull())
1789 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001790
Sean Callanancf128622012-03-15 01:53:17 +00001791 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr())
1792 {
1793 if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1794 CompleteType(tag_type->getDecl());
Sean Callananf4c0a222013-12-20 04:09:05 +00001795 if (const ObjCObjectPointerType *objc_object_ptr_type = dyn_cast<ObjCObjectPointerType>(parser_type))
1796 CompleteType(objc_object_ptr_type->getInterfaceDecl());
Sean Callanancf128622012-03-15 01:53:17 +00001797 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001798
1799
Greg Clayton57ee3062013-07-11 22:46:58 +00001800 bool is_reference = pt.IsReferenceType();
Sean Callanan98298012011-10-27 19:41:13 +00001801
Greg Clayton57ee3062013-07-11 22:46:58 +00001802 NamedDecl *var_decl = NULL;
Sean Callanan98298012011-10-27 19:41:13 +00001803 if (is_reference)
Greg Clayton57ee3062013-07-11 22:46:58 +00001804 var_decl = context.AddVarDecl(pt);
Sean Callanan98298012011-10-27 19:41:13 +00001805 else
Greg Clayton56939cb2015-09-17 22:23:34 +00001806 var_decl = context.AddVarDecl(pt.GetLValueReferenceType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001807
Greg Clayton7b462cc2010-10-15 22:48:33 +00001808 std::string decl_name(context.m_decl_name.getAsString());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001809 ConstString entity_name(decl_name.c_str());
Sean Callanan9301ec12015-10-01 23:07:06 +00001810 ClangExpressionVariable *entity(new ClangExpressionVariable(valobj));
1811 m_found_entities.AddNewlyConstructedVariable(entity);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001812
Sean Callananbc8ac342015-09-04 20:49:51 +00001813 assert (entity);
Sean Callanan3c495c12013-01-15 23:29:36 +00001814 entity->EnableParserVars(GetParserID());
1815 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1816 parser_vars->m_parser_type = pt;
1817 parser_vars->m_named_decl = var_decl;
1818 parser_vars->m_llvm_value = NULL;
1819 parser_vars->m_lldb_value = var_location;
1820 parser_vars->m_lldb_var = var;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001821
Sean Callanan98298012011-10-27 19:41:13 +00001822 if (is_reference)
1823 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001824
Sean Callananea22d422010-07-16 00:09:46 +00001825 if (log)
Greg Clayton7b462cc2010-10-15 22:48:33 +00001826 {
Sean Callanan82695d62011-11-07 23:32:52 +00001827 ASTDumper orig_dumper(ut.GetOpaqueQualType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001828 ASTDumper ast_dumper(var_decl);
Sean Callanan82695d62011-11-07 23:32:52 +00001829 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 +00001830 }
Sean Callanan468574b2010-06-22 23:46:24 +00001831}
1832
1833void
Sean Callanan2235f322010-08-11 03:57:18 +00001834ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
Sean Callananbc8ac342015-09-04 20:49:51 +00001835 ExpressionVariableSP &pvar_sp,
Sean Callanan7dd98122011-10-14 20:34:21 +00001836 unsigned int current_id)
Sean Callanan2235f322010-08-11 03:57:18 +00001837{
Greg Clayton5160ce52013-03-27 23:08:40 +00001838 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001839
Sean Callanan3820e9c2015-09-08 22:23:39 +00001840 TypeFromUser user_type (llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetTypeFromUser());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001841
Greg Clayton57ee3062013-07-11 22:46:58 +00001842 TypeFromParser parser_type (GuardedCopyType(user_type));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001843
Sean Callanane0a64f72011-12-01 21:04:37 +00001844 if (!parser_type.GetOpaqueQualType())
1845 {
1846 if (log)
1847 log->Printf(" CEDM::FEVD[%u] Couldn't import type for pvar %s", current_id, pvar_sp->GetName().GetCString());
1848 return;
1849 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001850
Greg Clayton56939cb2015-09-17 22:23:34 +00001851 NamedDecl *var_decl = context.AddVarDecl(parser_type.GetLValueReferenceType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001852
Sean Callanan3820e9c2015-09-08 22:23:39 +00001853 llvm::cast<ClangExpressionVariable>(pvar_sp.get())->EnableParserVars(GetParserID());
1854 ClangExpressionVariable::ParserVars *parser_vars = llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetParserVars(GetParserID());
Sean Callanan3c495c12013-01-15 23:29:36 +00001855 parser_vars->m_parser_type = parser_type;
Greg Clayton57ee3062013-07-11 22:46:58 +00001856 parser_vars->m_named_decl = var_decl;
1857 parser_vars->m_llvm_value = NULL;
1858 parser_vars->m_lldb_value.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001859
Sean Callanan823bb4c2010-08-30 22:17:16 +00001860 if (log)
Sean Callanan530782942010-11-01 23:22:47 +00001861 {
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001862 ASTDumper ast_dumper(var_decl);
Sean Callananfb3e4302011-10-29 19:50:43 +00001863 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 +00001864 }
Sean Callanan2235f322010-08-11 03:57:18 +00001865}
1866
Sean Callanan348b5892010-11-30 00:27:43 +00001867void
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001868ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context,
Greg Clayton3d51b9f2012-11-27 01:52:16 +00001869 const Symbol &symbol,
Sean Callanan7dd98122011-10-14 20:34:21 +00001870 unsigned int current_id)
Sean Callanand9ca42a2011-05-08 02:21:26 +00001871{
1872 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001873
Greg Clayton5160ce52013-03-27 23:08:40 +00001874 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001875
Sean Callanan933693b2012-02-10 01:22:05 +00001876 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00001877
1878 if (target == NULL)
1879 return;
1880
1881 ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001882
Greg Clayton56939cb2015-09-17 22:23:34 +00001883 TypeFromUser user_type (ClangASTContext::GetBasicType(scratch_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType());
1884 TypeFromParser parser_type (ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType());
Greg Clayton57ee3062013-07-11 22:46:58 +00001885 NamedDecl *var_decl = context.AddVarDecl(parser_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001886
Sean Callanand9ca42a2011-05-08 02:21:26 +00001887 std::string decl_name(context.m_decl_name.getAsString());
1888 ConstString entity_name(decl_name.c_str());
Sean Callanan9301ec12015-10-01 23:07:06 +00001889 ClangExpressionVariable *entity(new ClangExpressionVariable(m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (),
1890 entity_name,
1891 user_type,
1892 m_parser_vars->m_target_info.byte_order,
1893 m_parser_vars->m_target_info.address_byte_size));
1894 m_found_entities.AddNewlyConstructedVariable(entity);
1895
Greg Clayton57ee3062013-07-11 22:46:58 +00001896 entity->EnableParserVars(GetParserID());
1897 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1898
Greg Clayton358cf1e2015-06-25 21:46:34 +00001899 const Address symbol_address = symbol.GetAddress();
Greg Claytonc14ee322011-09-22 04:58:26 +00001900 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001901
Greg Clayton57ee3062013-07-11 22:46:58 +00001902 //parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType());
Greg Clayton99558cc42015-08-24 23:46:31 +00001903 parser_vars->m_lldb_value.SetCompilerType(user_type);
Greg Clayton57ee3062013-07-11 22:46:58 +00001904 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr;
1905 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001906
Sean Callanan3c495c12013-01-15 23:29:36 +00001907 parser_vars->m_parser_type = parser_type;
1908 parser_vars->m_named_decl = var_decl;
1909 parser_vars->m_llvm_value = NULL;
Sean Callanan3c495c12013-01-15 23:29:36 +00001910 parser_vars->m_lldb_sym = &symbol;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001911
Sean Callanand9ca42a2011-05-08 02:21:26 +00001912 if (log)
1913 {
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001914 ASTDumper ast_dumper(var_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001915
Sean Callananfb3e4302011-10-29 19:50:43 +00001916 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 +00001917 }
1918}
1919
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001920bool
Sean Callanan77502262011-05-12 23:54:16 +00001921ClangExpressionDeclMap::ResolveUnknownTypes()
1922{
Greg Clayton5160ce52013-03-27 23:08:40 +00001923 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan933693b2012-02-10 01:22:05 +00001924 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00001925
Greg Claytond8d4a572015-08-11 21:38:15 +00001926 ClangASTContext *scratch_ast_context = target->GetScratchClangASTContext();
Sean Callanan77502262011-05-12 23:54:16 +00001927
1928 for (size_t index = 0, num_entities = m_found_entities.GetSize();
1929 index < num_entities;
1930 ++index)
1931 {
Sean Callananbc8ac342015-09-04 20:49:51 +00001932 ExpressionVariableSP entity = m_found_entities.GetVariableAtIndex(index);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001933
Sean Callanan3820e9c2015-09-08 22:23:39 +00001934 ClangExpressionVariable::ParserVars *parser_vars = llvm::cast<ClangExpressionVariable>(entity.get())->GetParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001935
Sean Callanan77502262011-05-12 23:54:16 +00001936 if (entity->m_flags & ClangExpressionVariable::EVUnknownType)
1937 {
Sean Callanan3c495c12013-01-15 23:29:36 +00001938 const NamedDecl *named_decl = parser_vars->m_named_decl;
Sean Callanan77502262011-05-12 23:54:16 +00001939 const VarDecl *var_decl = dyn_cast<VarDecl>(named_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001940
Sean Callanan77502262011-05-12 23:54:16 +00001941 if (!var_decl)
1942 {
1943 if (log)
1944 log->Printf("Entity of unknown type does not have a VarDecl");
1945 return false;
1946 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001947
Sean Callanan77502262011-05-12 23:54:16 +00001948 if (log)
1949 {
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001950 ASTDumper ast_dumper(const_cast<VarDecl*>(var_decl));
1951 log->Printf("Variable of unknown type now has Decl %s", ast_dumper.GetCString());
Sean Callanan77502262011-05-12 23:54:16 +00001952 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001953
Sean Callanan77502262011-05-12 23:54:16 +00001954 QualType var_type = var_decl->getType();
Greg Claytond8d4a572015-08-11 21:38:15 +00001955 TypeFromParser parser_type(var_type.getAsOpaquePtr(), ClangASTContext::GetASTContext(&var_decl->getASTContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001956
Sean Callanana3444ff2015-11-10 22:54:42 +00001957 lldb::opaque_compiler_type_t copied_type = m_ast_importer_sp->CopyType(scratch_ast_context->getASTContext(), &var_decl->getASTContext(), var_type.getAsOpaquePtr());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001958
Sean Callanane0a64f72011-12-01 21:04:37 +00001959 if (!copied_type)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001960 {
Sean Callanane0a64f72011-12-01 21:04:37 +00001961 if (log)
1962 log->Printf("ClangExpressionDeclMap::ResolveUnknownType - Couldn't import the type for a variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001963
Sean Callananbc8ac342015-09-04 20:49:51 +00001964 return (bool) lldb::ExpressionVariableSP();
Sean Callanane0a64f72011-12-01 21:04:37 +00001965 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001966
Sean Callanan77502262011-05-12 23:54:16 +00001967 TypeFromUser user_type(copied_type, scratch_ast_context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001968
Greg Clayton57ee3062013-07-11 22:46:58 +00001969// parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType());
Greg Clayton99558cc42015-08-24 23:46:31 +00001970 parser_vars->m_lldb_value.SetCompilerType(user_type);
Sean Callanan3c495c12013-01-15 23:29:36 +00001971 parser_vars->m_parser_type = parser_type;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001972
Greg Clayton99558cc42015-08-24 23:46:31 +00001973 entity->SetCompilerType(user_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001974
Sean Callanan77502262011-05-12 23:54:16 +00001975 entity->m_flags &= ~(ClangExpressionVariable::EVUnknownType);
1976 }
1977 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001978
Sean Callanan77502262011-05-12 23:54:16 +00001979 return true;
1980}
1981
Sean Callanand9ca42a2011-05-08 02:21:26 +00001982void
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001983ClangExpressionDeclMap::AddOneRegister (NameSearchContext &context,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001984 const RegisterInfo *reg_info,
Sean Callanan7dd98122011-10-14 20:34:21 +00001985 unsigned int current_id)
Sean Callanan348b5892010-11-30 00:27:43 +00001986{
Greg Clayton5160ce52013-03-27 23:08:40 +00001987 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001988
Greg Claytona1e5dc82015-08-11 22:53:00 +00001989 CompilerType clang_type = ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (m_ast_context,
Greg Clayton57ee3062013-07-11 22:46:58 +00001990 reg_info->encoding,
1991 reg_info->byte_size * 8);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001992
Greg Clayton57ee3062013-07-11 22:46:58 +00001993 if (!clang_type)
Sean Callanan348b5892010-11-30 00:27:43 +00001994 {
Johnny Chene95fcf72011-08-09 22:52:27 +00001995 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +00001996 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 +00001997 return;
1998 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001999
Greg Clayton57ee3062013-07-11 22:46:58 +00002000 TypeFromParser parser_clang_type (clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002001
Greg Clayton57ee3062013-07-11 22:46:58 +00002002 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002003
Sean Callanan9301ec12015-10-01 23:07:06 +00002004 ClangExpressionVariable *entity(new ClangExpressionVariable(m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
2005 m_parser_vars->m_target_info.byte_order,
2006 m_parser_vars->m_target_info.address_byte_size));
2007 m_found_entities.AddNewlyConstructedVariable(entity);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002008
Sean Callanan348b5892010-11-30 00:27:43 +00002009 std::string decl_name(context.m_decl_name.getAsString());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002010 entity->SetName (ConstString (decl_name.c_str()));
2011 entity->SetRegisterInfo (reg_info);
Sean Callanan3c495c12013-01-15 23:29:36 +00002012 entity->EnableParserVars(GetParserID());
2013 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
Greg Clayton57ee3062013-07-11 22:46:58 +00002014 parser_vars->m_parser_type = parser_clang_type;
2015 parser_vars->m_named_decl = var_decl;
2016 parser_vars->m_llvm_value = NULL;
2017 parser_vars->m_lldb_value.Clear();
Sean Callananf673e762012-02-15 01:40:39 +00002018 entity->m_flags |= ClangExpressionVariable::EVBareRegister;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002019
Sean Callananf673e762012-02-15 01:40:39 +00002020 if (log)
Sean Callanan348b5892010-11-30 00:27:43 +00002021 {
Greg Clayton64bc6ca2011-10-20 00:47:21 +00002022 ASTDumper ast_dumper(var_decl);
Sean Callananfb3e4302011-10-29 19:50:43 +00002023 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 +00002024 }
2025}
2026
Sean Callanan2235f322010-08-11 03:57:18 +00002027void
Greg Clayton3f5c08f2011-05-18 22:01:49 +00002028ClangExpressionDeclMap::AddOneFunction (NameSearchContext &context,
Greg Clayton57ee3062013-07-11 22:46:58 +00002029 Function* function,
Sean Callanan7dd98122011-10-14 20:34:21 +00002030 Symbol* symbol,
2031 unsigned int current_id)
Sean Callanan468574b2010-06-22 23:46:24 +00002032{
Sean Callanan979f74d2010-12-03 01:38:59 +00002033 assert (m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002034
Greg Clayton5160ce52013-03-27 23:08:40 +00002035 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002036
Greg Clayton57ee3062013-07-11 22:46:58 +00002037 NamedDecl *function_decl = NULL;
Greg Clayton358cf1e2015-06-25 21:46:34 +00002038 Address fun_address;
Greg Claytona1e5dc82015-08-11 22:53:00 +00002039 CompilerType function_clang_type;
Matt Kopec00049b82013-02-27 20:13:38 +00002040
2041 bool is_indirect_function = false;
2042
Greg Clayton57ee3062013-07-11 22:46:58 +00002043 if (function)
Sean Callanan8ade1042010-07-27 00:55:47 +00002044 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002045 Type *function_type = function->GetType();
Sean Callanan2a8fa2a2016-02-13 00:01:46 +00002046
2047 const lldb::LanguageType comp_unit_language = function->GetCompileUnit()->GetLanguage();
2048 const bool extern_c = Language::LanguageIsC(comp_unit_language) ||
2049 (Language::LanguageIsObjC(comp_unit_language) &&
2050 !Language::LanguageIsCPlusPlus(comp_unit_language));
2051
2052 if (!extern_c)
2053 {
2054 TypeSystem *type_system = function->GetDeclContext().GetTypeSystem();
2055 if (ClangASTContext *src_ast = llvm::dyn_cast<ClangASTContext>(type_system))
2056 {
2057 clang::DeclContext *src_decl_context = (clang::DeclContext*)function->GetDeclContext().GetOpaqueDeclContext();
2058 clang::FunctionDecl *src_function_decl = llvm::dyn_cast_or_null<clang::FunctionDecl>(src_decl_context);
2059
2060 if (src_function_decl)
2061 {
2062 if (clang::FunctionDecl *copied_function_decl = llvm::dyn_cast_or_null<clang::FunctionDecl>(m_ast_importer_sp->CopyDecl(m_ast_context, src_ast->getASTContext(), src_function_decl)))
2063 {
2064 if (log)
2065 {
2066 ASTDumper ast_dumper((clang::Decl*)copied_function_decl);
2067
2068 StreamString ss;
2069
2070 function->DumpSymbolContext(&ss);
2071
2072 log->Printf(" CEDM::FEVD[%u] Imported decl for function %s (description %s), returned %s",
2073 current_id,
2074 copied_function_decl->getName().str().c_str(),
2075 ss.GetData(),
2076 ast_dumper.GetCString());
2077
2078 }
2079
2080 context.AddNamedDecl(copied_function_decl);
2081 return;
2082 }
2083 else
2084 {
2085 if (log)
2086 {
2087 log->Printf (" Failed to import the function decl for '%s'",
2088 src_function_decl->getName().str().c_str());
2089 }
2090 }
2091 }
2092 }
2093 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002094
Greg Clayton57ee3062013-07-11 22:46:58 +00002095 if (!function_type)
Sean Callanan8ade1042010-07-27 00:55:47 +00002096 {
2097 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +00002098 log->PutCString(" Skipped a function because it has no type");
Sean Callanan8ade1042010-07-27 00:55:47 +00002099 return;
2100 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002101
Greg Clayton99558cc42015-08-24 23:46:31 +00002102 function_clang_type = function_type->GetFullCompilerType ();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002103
Greg Clayton57ee3062013-07-11 22:46:58 +00002104 if (!function_clang_type)
Sean Callanan8ade1042010-07-27 00:55:47 +00002105 {
2106 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +00002107 log->PutCString(" Skipped a function because it has no Clang type");
Sean Callanan8ade1042010-07-27 00:55:47 +00002108 return;
2109 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002110
Greg Clayton358cf1e2015-06-25 21:46:34 +00002111 fun_address = function->GetAddressRange().GetBaseAddress();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002112
Greg Claytona1e5dc82015-08-11 22:53:00 +00002113 CompilerType copied_function_type = GuardedCopyType(function_clang_type);
Greg Clayton57ee3062013-07-11 22:46:58 +00002114 if (copied_function_type)
Greg Clayton64bc6ca2011-10-20 00:47:21 +00002115 {
Sean Callanan2a8fa2a2016-02-13 00:01:46 +00002116 function_decl = context.AddFunDecl(copied_function_type, extern_c);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002117
Greg Clayton57ee3062013-07-11 22:46:58 +00002118 if (!function_decl)
Sean Callanan485f7322013-04-24 00:34:41 +00002119 {
2120 if (log)
2121 {
2122 log->Printf (" Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}",
Greg Clayton57ee3062013-07-11 22:46:58 +00002123 function_type->GetName().GetCString(),
2124 function_type->GetID());
Sean Callanan485f7322013-04-24 00:34:41 +00002125 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002126
Sean Callanan485f7322013-04-24 00:34:41 +00002127 return;
2128 }
Greg Clayton64bc6ca2011-10-20 00:47:21 +00002129 }
2130 else
2131 {
2132 // We failed to copy the type we found
2133 if (log)
2134 {
Daniel Malead01b2952012-11-29 21:49:15 +00002135 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 +00002136 function_type->GetName().GetCString(),
2137 function_type->GetID());
Greg Clayton64bc6ca2011-10-20 00:47:21 +00002138 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002139
Sean Callananed8d58f2011-11-16 00:40:13 +00002140 return;
Greg Clayton64bc6ca2011-10-20 00:47:21 +00002141 }
Sean Callanan8ade1042010-07-27 00:55:47 +00002142 }
2143 else if (symbol)
2144 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00002145 fun_address = symbol->GetAddress();
Greg Clayton57ee3062013-07-11 22:46:58 +00002146 function_decl = context.AddGenericFunDecl();
Matt Kopec00049b82013-02-27 20:13:38 +00002147 is_indirect_function = symbol->IsIndirect();
Sean Callanan8ade1042010-07-27 00:55:47 +00002148 }
2149 else
2150 {
2151 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +00002152 log->PutCString(" AddOneFunction called with no function and no symbol");
Sean Callanan8ade1042010-07-27 00:55:47 +00002153 return;
2154 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002155
Sean Callanan933693b2012-02-10 01:22:05 +00002156 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002157
Greg Clayton358cf1e2015-06-25 21:46:34 +00002158 lldb::addr_t load_addr = fun_address.GetCallableLoadAddress(target, is_indirect_function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002159
Sean Callanan9301ec12015-10-01 23:07:06 +00002160 ClangExpressionVariable *entity(new ClangExpressionVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (),
2161 m_parser_vars->m_target_info.byte_order,
2162 m_parser_vars->m_target_info.address_byte_size));
2163 m_found_entities.AddNewlyConstructedVariable(entity);
Greg Clayton57ee3062013-07-11 22:46:58 +00002164
2165 std::string decl_name(context.m_decl_name.getAsString());
2166 entity->SetName(ConstString(decl_name.c_str()));
Greg Clayton99558cc42015-08-24 23:46:31 +00002167 entity->SetCompilerType (function_clang_type);
Greg Clayton57ee3062013-07-11 22:46:58 +00002168 entity->EnableParserVars(GetParserID());
2169
2170 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002171
Sean Callanan9be9d172013-03-19 01:45:02 +00002172 if (load_addr != LLDB_INVALID_ADDRESS)
2173 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002174 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
2175 parser_vars->m_lldb_value.GetScalar() = load_addr;
Sean Callanan9be9d172013-03-19 01:45:02 +00002176 }
2177 else
2178 {
2179 // We have to try finding a file address.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002180
Greg Clayton358cf1e2015-06-25 21:46:34 +00002181 lldb::addr_t file_addr = fun_address.GetFileAddress();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002182
Greg Clayton57ee3062013-07-11 22:46:58 +00002183 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress);
2184 parser_vars->m_lldb_value.GetScalar() = file_addr;
Sean Callanan9be9d172013-03-19 01:45:02 +00002185 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002186
Greg Clayton57ee3062013-07-11 22:46:58 +00002187
2188 parser_vars->m_named_decl = function_decl;
Sean Callanan3c495c12013-01-15 23:29:36 +00002189 parser_vars->m_llvm_value = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002190
Sean Callananea22d422010-07-16 00:09:46 +00002191 if (log)
Greg Clayton7b462cc2010-10-15 22:48:33 +00002192 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002193 ASTDumper ast_dumper(function_decl);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002194
Sean Callanan308a3c52012-07-28 00:21:01 +00002195 StreamString ss;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002196
Greg Clayton358cf1e2015-06-25 21:46:34 +00002197 fun_address.Dump(&ss, m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), Address::DumpStyleResolvedDescription);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002198
Sean Callanan308a3c52012-07-28 00:21:01 +00002199 log->Printf(" CEDM::FEVD[%u] Found %s function %s (description %s), returned %s",
Sean Callanan7dd98122011-10-14 20:34:21 +00002200 current_id,
Greg Clayton57ee3062013-07-11 22:46:58 +00002201 (function ? "specific" : "generic"),
Sean Callanan308a3c52012-07-28 00:21:01 +00002202 decl_name.c_str(),
2203 ss.GetData(),
Greg Clayton64bc6ca2011-10-20 00:47:21 +00002204 ast_dumper.GetCString());
Greg Clayton7b462cc2010-10-15 22:48:33 +00002205 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002206}
Sean Callanan5666b672010-08-04 01:02:13 +00002207
Sean Callanan3b436d22015-10-23 21:45:02 +00002208void
2209ClangExpressionDeclMap::AddThisType(NameSearchContext &context,
2210 TypeFromUser &ut,
2211 unsigned int current_id)
Sean Callanan5666b672010-08-04 01:02:13 +00002212{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002213 CompilerType copied_clang_type = GuardedCopyType(ut);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002214
Sean Callanan8bdcd522016-04-28 01:36:21 +00002215 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2216
Greg Clayton57ee3062013-07-11 22:46:58 +00002217 if (!copied_clang_type)
Sean Callanane0a64f72011-12-01 21:04:37 +00002218 {
Sean Callananfa4fab72013-02-01 06:55:48 +00002219 if (log)
Sean Callanan3b436d22015-10-23 21:45:02 +00002220 log->Printf("ClangExpressionDeclMap::AddThisType - Couldn't import the type");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002221
Sean Callanan3b436d22015-10-23 21:45:02 +00002222 return;
Sean Callanane0a64f72011-12-01 21:04:37 +00002223 }
Sean Callananfa4fab72013-02-01 06:55:48 +00002224
Greg Clayton57ee3062013-07-11 22:46:58 +00002225 if (copied_clang_type.IsAggregateType() && copied_clang_type.GetCompleteType ())
Sean Callananfc55f5d2010-09-21 00:44:12 +00002226 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00002227 CompilerType void_clang_type = ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid);
2228 CompilerType void_ptr_clang_type = void_clang_type.GetPointerType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002229
Greg Claytona1e5dc82015-08-11 22:53:00 +00002230 CompilerType method_type = ClangASTContext::CreateFunctionType (m_ast_context,
Greg Clayton57ee3062013-07-11 22:46:58 +00002231 void_clang_type,
2232 &void_ptr_clang_type,
Sean Callananc1b732d2011-11-01 18:07:13 +00002233 1,
2234 false,
Sean Callanan8bdcd522016-04-28 01:36:21 +00002235 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002236
Greg Clayton0fffff52010-09-24 05:15:53 +00002237 const bool is_virtual = false;
2238 const bool is_static = false;
2239 const bool is_inline = false;
Greg Claytonf51de672010-10-01 02:31:07 +00002240 const bool is_explicit = false;
Sean Callananc8324752011-11-02 18:09:01 +00002241 const bool is_attr_used = true;
Sean Callanandbb58392011-11-02 01:38:59 +00002242 const bool is_artificial = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002243
Sean Callanan8bdcd522016-04-28 01:36:21 +00002244 CXXMethodDecl *method_decl = ClangASTContext::GetASTContext(m_ast_context)->
Greg Claytond8d4a572015-08-11 21:38:15 +00002245 AddMethodToCXXRecordType (copied_clang_type.GetOpaqueQualType(),
2246 "$__lldb_expr",
2247 method_type,
2248 lldb::eAccessPublic,
2249 is_virtual,
2250 is_static,
2251 is_inline,
2252 is_explicit,
2253 is_attr_used,
2254 is_artificial);
Sean Callanan8bdcd522016-04-28 01:36:21 +00002255
2256 if (log)
2257 {
2258 ASTDumper method_ast_dumper((clang::Decl*)method_decl);
2259 ASTDumper type_ast_dumper(copied_clang_type);
2260
2261 log->Printf(" CEDM::AddThisType Added function $__lldb_expr (description %s) for this type %s",
2262 method_ast_dumper.GetCString(),
2263 type_ast_dumper.GetCString());
2264 }
Sean Callananfc55f5d2010-09-21 00:44:12 +00002265 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002266
Sean Callanan3b436d22015-10-23 21:45:02 +00002267 if (!copied_clang_type.IsValid())
2268 return;
2269
2270 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(QualType::getFromOpaquePtr(copied_clang_type.GetOpaqueQualType()));
2271
2272 if (!type_source_info)
2273 return;
2274
2275 // Construct a typedef type because if "*this" is a templated type we can't just return ClassTemplateSpecializationDecls in response to name queries.
2276 // Using a typedef makes this much more robust.
2277
2278 TypedefDecl *typedef_decl = TypedefDecl::Create(*m_ast_context,
2279 m_ast_context->getTranslationUnitDecl(),
2280 SourceLocation(),
2281 SourceLocation(),
2282 context.m_decl_name.getAsIdentifierInfo(),
2283 type_source_info);
2284
2285
2286 if (!typedef_decl)
2287 return;
2288
2289 context.AddNamedDecl(typedef_decl);
2290
2291 return;
Sean Callananfa4fab72013-02-01 06:55:48 +00002292}
2293
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002294void
2295ClangExpressionDeclMap::AddOneType(NameSearchContext &context,
Sean Callananfa4fab72013-02-01 06:55:48 +00002296 TypeFromUser &ut,
2297 unsigned int current_id)
2298{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002299 CompilerType copied_clang_type = GuardedCopyType(ut);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002300
Greg Clayton57ee3062013-07-11 22:46:58 +00002301 if (!copied_clang_type)
Sean Callananfa4fab72013-02-01 06:55:48 +00002302 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002303 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfa4fab72013-02-01 06:55:48 +00002304
2305 if (log)
2306 log->Printf("ClangExpressionDeclMap::AddOneType - Couldn't import the type");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002307
Sean Callananfa4fab72013-02-01 06:55:48 +00002308 return;
2309 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002310
Greg Clayton57ee3062013-07-11 22:46:58 +00002311 context.AddTypeDecl(copied_clang_type);
Sean Callanan5666b672010-08-04 01:02:13 +00002312}