blob: 6634824be01f9ea2dee186d45febb31103613da0 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangExpressionDeclMap.cpp -----------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Sean Callanan4dbb2712015-09-25 20:35:58 +00009#include "ClangExpressionDeclMap.h"
10
Sean Callanan4dbb2712015-09-25 20:35:58 +000011#include "ClangASTSource.h"
12#include "ClangModulesDeclVendor.h"
13#include "ClangPersistentVariables.h"
14
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/Address.h"
16#include "lldb/Core/Module.h"
Greg Clayton9191db42013-10-21 18:40:51 +000017#include "lldb/Core/ModuleSpec.h"
Sean Callanane4ec90e2010-12-16 03:17:46 +000018#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan9b3569b2011-12-10 03:12:34 +000019#include "lldb/Core/ValueObjectVariable.h"
Sean Callanan96d27302013-04-11 00:09:05 +000020#include "lldb/Expression/Materializer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Symbol/ClangASTContext.h"
Raphael Isemann7a6588a2019-11-25 13:27:51 +010022#include "lldb/Symbol/ClangUtil.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000023#include "lldb/Symbol/CompileUnit.h"
Paul Hermand628cbb2015-09-15 23:44:17 +000024#include "lldb/Symbol/CompilerDecl.h"
Greg Clayton99558cc42015-08-24 23:46:31 +000025#include "lldb/Symbol/CompilerDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Symbol/Function.h"
27#include "lldb/Symbol/ObjectFile.h"
28#include "lldb/Symbol/SymbolContext.h"
Siva Chandra9293fc42016-01-07 23:32:34 +000029#include "lldb/Symbol/SymbolFile.h"
Sean Callanan503aa522011-10-12 00:12:34 +000030#include "lldb/Symbol/SymbolVendor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Symbol/Type.h"
32#include "lldb/Symbol/TypeList.h"
33#include "lldb/Symbol/Variable.h"
34#include "lldb/Symbol/VariableList.h"
Sean Callanan1d180662010-07-20 23:31:16 +000035#include "lldb/Target/ExecutionContext.h"
Sean Callananea22d422010-07-16 00:09:46 +000036#include "lldb/Target/Process.h"
Sean Callananf4b9bd32010-10-05 20:18:48 +000037#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000038#include "lldb/Target/StackFrame.h"
Sean Callanan1d180662010-07-20 23:31:16 +000039#include "lldb/Target/Target.h"
Jim Ingham895c9822010-12-07 01:56:02 +000040#include "lldb/Target/Thread.h"
Zachary Turner01c32432017-02-14 19:06:07 +000041#include "lldb/Utility/Endian.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000042#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000043#include "lldb/Utility/RegisterValue.h"
Zachary Turner97206d52017-05-12 04:51:55 +000044#include "lldb/Utility/Status.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000045#include "lldb/lldb-private.h"
46#include "clang/AST/ASTConsumer.h"
47#include "clang/AST/ASTContext.h"
Sean Callanan68e44232017-09-28 20:20:25 +000048#include "clang/AST/ASTImporter.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000049#include "clang/AST/Decl.h"
50#include "clang/AST/DeclarationName.h"
Sean Callanan68e44232017-09-28 20:20:25 +000051#include "clang/AST/RecursiveASTVisitor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052
Siva Chandra0f4873d2015-09-03 23:27:10 +000053#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
Alex Langforde0678ca2019-07-12 20:09:32 +000054#include "Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h"
Alex Langfordb57017102019-07-15 22:56:12 +000055#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
Siva Chandra0f4873d2015-09-03 23:27:10 +000056
Greg Clayton83c5cd92010-11-14 22:13:40 +000057using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058using namespace lldb_private;
59using namespace clang;
60
Kate Stoneb9c1b512016-09-06 20:57:50 +000061namespace {
62const char *g_lldb_local_vars_namespace_cstr = "$__lldb_local_vars";
Siva Chandra03ff5c82016-02-05 19:10:04 +000063} // anonymous namespace
64
Kate Stoneb9c1b512016-09-06 20:57:50 +000065ClangExpressionDeclMap::ClangExpressionDeclMap(
66 bool keep_result_in_memory,
67 Materializer::PersistentVariableDelegate *result_delegate,
Raphael Isemann4aee81c2019-12-17 13:38:07 +010068 const lldb::TargetSP &target, const lldb::ClangASTImporterSP &importer,
69 ValueObject *ctx_obj)
70 : ClangASTSource(target, importer), m_found_entities(), m_struct_members(),
71 m_keep_result_in_memory(keep_result_in_memory),
Krasimir Georgiev7ffc4682019-02-05 11:35:45 +000072 m_result_delegate(result_delegate), m_ctx_obj(ctx_obj), m_parser_vars(),
73 m_struct_vars() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 EnableStructVars();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075}
76
Kate Stoneb9c1b512016-09-06 20:57:50 +000077ClangExpressionDeclMap::~ClangExpressionDeclMap() {
78 // Note: The model is now that the parser's AST context and all associated
79 // data does not vanish until the expression has been executed. This means
80 // that valuable lookup data (like namespaces) doesn't vanish, but
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 DidParse();
83 DisableStructVars();
Sean Callanan979f74d2010-12-03 01:38:59 +000084}
Sean Callananbe3a1b12010-10-26 00:31:56 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086bool ClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx,
87 Materializer *materializer) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 EnableParserVars();
89 m_parser_vars->m_exe_ctx = exe_ctx;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 Target *target = exe_ctx.GetTargetPtr();
92 if (exe_ctx.GetFramePtr())
93 m_parser_vars->m_sym_ctx =
94 exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything);
95 else if (exe_ctx.GetThreadPtr() &&
96 exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0))
97 m_parser_vars->m_sym_ctx =
98 exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext(
99 lldb::eSymbolContextEverything);
100 else if (exe_ctx.GetProcessPtr()) {
101 m_parser_vars->m_sym_ctx.Clear(true);
102 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
103 } else if (target) {
104 m_parser_vars->m_sym_ctx.Clear(true);
105 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
106 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 if (target) {
109 m_parser_vars->m_persistent_vars = llvm::cast<ClangPersistentVariables>(
110 target->GetPersistentExpressionStateForLanguage(eLanguageTypeC));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000111
Alex Langford30318182019-11-14 13:41:52 -0800112 if (!ClangASTContext::GetScratch(*target))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 return false;
114 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 m_parser_vars->m_target_info = GetTargetInfo();
117 m_parser_vars->m_materializer = materializer;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 return true;
Sean Callanan979f74d2010-12-03 01:38:59 +0000120}
121
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122void ClangExpressionDeclMap::InstallCodeGenerator(
123 clang::ASTConsumer *code_gen) {
124 assert(m_parser_vars);
125 m_parser_vars->m_code_gen = code_gen;
Sean Callanan80c97592015-05-01 00:47:29 +0000126}
127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128void ClangExpressionDeclMap::DidParse() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000129 if (m_parser_vars) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 for (size_t entity_index = 0, num_entities = m_found_entities.GetSize();
131 entity_index < num_entities; ++entity_index) {
132 ExpressionVariableSP var_sp(
133 m_found_entities.GetVariableAtIndex(entity_index));
134 if (var_sp)
135 llvm::cast<ClangExpressionVariable>(var_sp.get())
136 ->DisableParserVars(GetParserID());
Sean Callanan6b1b9532010-10-08 01:58:41 +0000137 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138
139 for (size_t pvar_index = 0,
140 num_pvars = m_parser_vars->m_persistent_vars->GetSize();
141 pvar_index < num_pvars; ++pvar_index) {
142 ExpressionVariableSP pvar_sp(
143 m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index));
144 if (ClangExpressionVariable *clang_var =
145 llvm::dyn_cast<ClangExpressionVariable>(pvar_sp.get()))
146 clang_var->DisableParserVars(GetParserID());
147 }
148
149 DisableParserVars();
150 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151}
152
Sean Callanan549c9f72010-07-13 21:41:46 +0000153// Interface for IRForTarget
154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155ClangExpressionDeclMap::TargetInfo ClangExpressionDeclMap::GetTargetInfo() {
156 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 TargetInfo ret;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
Sean Callanan933693b2012-02-10 01:22:05 +0000161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 Process *process = exe_ctx.GetProcessPtr();
163 if (process) {
164 ret.byte_order = process->GetByteOrder();
165 ret.address_byte_size = process->GetAddressByteSize();
166 } else {
167 Target *target = exe_ctx.GetTargetPtr();
168 if (target) {
169 ret.byte_order = target->GetArchitecture().GetByteOrder();
170 ret.address_byte_size = target->GetArchitecture().GetAddressByteSize();
Sean Callanan933693b2012-02-10 01:22:05 +0000171 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172 }
Sean Callanan933693b2012-02-10 01:22:05 +0000173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 return ret;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000175}
176
Sean Callanan68e44232017-09-28 20:20:25 +0000177TypeFromUser ClangExpressionDeclMap::DeportType(ClangASTContext &target,
178 ClangASTContext &source,
179 TypeFromParser parser_type) {
Alex Langford30318182019-11-14 13:41:52 -0800180 assert(&target == ClangASTContext::GetScratch(*m_target));
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +0000181 assert((TypeSystem *)&source == parser_type.GetTypeSystem());
Raphael Isemannf9f49d32019-12-21 22:40:52 +0100182 assert(&source.getASTContext() == m_ast_context);
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +0000183
Sean Callanan68e44232017-09-28 20:20:25 +0000184 if (m_ast_importer_sp) {
Raphael Isemann75e8a912019-12-16 11:52:36 +0100185 return TypeFromUser(m_ast_importer_sp->DeportType(target, parser_type));
Sean Callanan68e44232017-09-28 20:20:25 +0000186 } else {
Pavel Labath9344e452017-10-17 21:52:29 +0000187 lldbassert(0 && "No mechanism for deporting a type!");
Sean Callanan68e44232017-09-28 20:20:25 +0000188 return TypeFromUser();
189 }
190}
191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192bool ClangExpressionDeclMap::AddPersistentVariable(const NamedDecl *decl,
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000193 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 TypeFromParser parser_type,
195 bool is_result,
196 bool is_lvalue) {
197 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 ClangASTContext *ast =
200 llvm::dyn_cast_or_null<ClangASTContext>(parser_type.GetTypeSystem());
201 if (ast == nullptr)
202 return false;
Greg Claytonf73034f2015-09-08 18:15:05 +0000203
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 if (m_parser_vars->m_materializer && is_result) {
Zachary Turner97206d52017-05-12 04:51:55 +0000205 Status err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000206
Sean Callanan933693b2012-02-10 01:22:05 +0000207 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
208 Target *target = exe_ctx.GetTargetPtr();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 if (target == nullptr)
210 return false;
Greg Claytonc14ee322011-09-22 04:58:26 +0000211
Alex Langford30318182019-11-14 13:41:52 -0800212 auto *clang_ast_context = ClangASTContext::GetScratch(*target);
213 if (!clang_ast_context)
214 return false;
215
216 TypeFromUser user_type = DeportType(*clang_ast_context, *ast, parser_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(
219 user_type, is_lvalue, m_keep_result_in_memory, m_result_delegate, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 ClangExpressionVariable *var = new ClangExpressionVariable(
222 exe_ctx.GetBestExecutionContextScope(), name, user_type,
223 m_parser_vars->m_target_info.byte_order,
224 m_parser_vars->m_target_info.address_byte_size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 m_found_entities.AddNewlyConstructedVariable(var);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000227
Sean Callananbc8ac342015-09-04 20:49:51 +0000228 var->EnableParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 ClangExpressionVariable::ParserVars *parser_vars =
231 var->GetParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000232
Sean Callanan3c495c12013-01-15 23:29:36 +0000233 parser_vars->m_named_decl = decl;
234 parser_vars->m_parser_type = parser_type;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000235
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236 var->EnableJITVars(GetParserID());
237
238 ClangExpressionVariable::JITVars *jit_vars = var->GetJITVars(GetParserID());
239
240 jit_vars->m_offset = offset;
241
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000242 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 }
244
245 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
246 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
247 Target *target = exe_ctx.GetTargetPtr();
Konrad Kleine248a1302019-05-23 11:14:47 +0000248 if (target == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249 return false;
250
Alex Langford30318182019-11-14 13:41:52 -0800251 ClangASTContext *context = ClangASTContext::GetScratch(*target);
252 if (!context)
253 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254
Sean Callanan68e44232017-09-28 20:20:25 +0000255 TypeFromUser user_type = DeportType(*context, *ast, parser_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256
257 if (!user_type.GetOpaqueQualType()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000258 LLDB_LOGF(log, "Persistent variable's type wasn't copied successfully");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 return false;
260 }
261
262 if (!m_parser_vars->m_target_info.IsValid())
263 return false;
264
265 ClangExpressionVariable *var = llvm::cast<ClangExpressionVariable>(
266 m_parser_vars->m_persistent_vars
267 ->CreatePersistentVariable(
268 exe_ctx.GetBestExecutionContextScope(), name, user_type,
269 m_parser_vars->m_target_info.byte_order,
270 m_parser_vars->m_target_info.address_byte_size)
271 .get());
272
273 if (!var)
274 return false;
275
276 var->m_frozen_sp->SetHasCompleteType();
277
278 if (is_result)
279 var->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry;
280 else
281 var->m_flags |=
282 ClangExpressionVariable::EVKeepInTarget; // explicitly-declared
283 // persistent variables should
284 // persist
285
286 if (is_lvalue) {
287 var->m_flags |= ClangExpressionVariable::EVIsProgramReference;
288 } else {
289 var->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
290 var->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
291 }
292
293 if (m_keep_result_in_memory) {
294 var->m_flags |= ClangExpressionVariable::EVKeepInTarget;
295 }
296
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000297 LLDB_LOGF(log, "Created persistent variable with flags 0x%hx", var->m_flags);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298
299 var->EnableParserVars(GetParserID());
300
301 ClangExpressionVariable::ParserVars *parser_vars =
302 var->GetParserVars(GetParserID());
303
304 parser_vars->m_named_decl = decl;
305 parser_vars->m_parser_type = parser_type;
306
307 return true;
Sean Callanan2235f322010-08-11 03:57:18 +0000308}
309
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310bool ClangExpressionDeclMap::AddValueToStruct(const NamedDecl *decl,
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000311 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312 llvm::Value *value, size_t size,
313 lldb::offset_t alignment) {
314 assert(m_struct_vars.get());
315 assert(m_parser_vars.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000316
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317 bool is_persistent_variable = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000318
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000320
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321 m_struct_vars->m_struct_laid_out = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000322
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 if (ClangExpressionVariable::FindVariableInList(m_struct_members, decl,
324 GetParserID()))
325 return true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000326
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327 ClangExpressionVariable *var(ClangExpressionVariable::FindVariableInList(
328 m_found_entities, decl, GetParserID()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000329
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330 if (!var) {
331 var = ClangExpressionVariable::FindVariableInList(
332 *m_parser_vars->m_persistent_vars, decl, GetParserID());
333 is_persistent_variable = true;
334 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000335
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 if (!var)
337 return false;
338
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000339 LLDB_LOGF(log, "Adding value for (NamedDecl*)%p [%s - %s] to the structure",
340 static_cast<const void *>(decl), name.GetCString(),
341 var->GetName().GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342
343 // We know entity->m_parser_vars is valid because we used a parser variable
344 // to find it
345
346 ClangExpressionVariable::ParserVars *parser_vars =
347 llvm::cast<ClangExpressionVariable>(var)->GetParserVars(GetParserID());
348
349 parser_vars->m_llvm_value = value;
350
351 if (ClangExpressionVariable::JITVars *jit_vars =
352 llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID())) {
353 // We already laid this out; do not touch
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000354
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000355 LLDB_LOGF(log, "Already placed at 0x%llx",
356 (unsigned long long)jit_vars->m_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000358
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359 llvm::cast<ClangExpressionVariable>(var)->EnableJITVars(GetParserID());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000360
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 ClangExpressionVariable::JITVars *jit_vars =
362 llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID());
Sean Callanan3c495c12013-01-15 23:29:36 +0000363
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364 jit_vars->m_alignment = alignment;
365 jit_vars->m_size = size;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 m_struct_members.AddVariable(var->shared_from_this());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000368
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 if (m_parser_vars->m_materializer) {
370 uint32_t offset = 0;
371
Zachary Turner97206d52017-05-12 04:51:55 +0000372 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373
374 if (is_persistent_variable) {
375 ExpressionVariableSP var_sp(var->shared_from_this());
376 offset = m_parser_vars->m_materializer->AddPersistentVariable(
377 var_sp, nullptr, err);
378 } else {
379 if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym)
380 offset = m_parser_vars->m_materializer->AddSymbol(*sym, err);
381 else if (const RegisterInfo *reg_info = var->GetRegisterInfo())
382 offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err);
383 else if (parser_vars->m_lldb_var)
384 offset = m_parser_vars->m_materializer->AddVariable(
385 parser_vars->m_lldb_var, err);
Sean Callanan1582ee62013-04-18 22:06:33 +0000386 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000387
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388 if (!err.Success())
389 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000390
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000391 LLDB_LOGF(log, "Placed at 0x%llx", (unsigned long long)offset);
Sean Callanan3c495c12013-01-15 23:29:36 +0000392
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393 jit_vars->m_offset =
394 offset; // TODO DoStructLayout() should not change this.
395 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 return true;
398}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000399
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400bool ClangExpressionDeclMap::DoStructLayout() {
401 assert(m_struct_vars.get());
Sean Callanan3dd6a422013-04-11 21:16:36 +0000402
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 if (m_struct_vars->m_struct_laid_out)
404 return true;
Sean Callanandf667652013-04-11 02:05:11 +0000405
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406 if (!m_parser_vars->m_materializer)
407 return false;
408
409 m_struct_vars->m_struct_alignment =
410 m_parser_vars->m_materializer->GetStructAlignment();
411 m_struct_vars->m_struct_size =
412 m_parser_vars->m_materializer->GetStructByteSize();
413 m_struct_vars->m_struct_laid_out = true;
414 return true;
415}
416
417bool ClangExpressionDeclMap::GetStructInfo(uint32_t &num_elements, size_t &size,
418 lldb::offset_t &alignment) {
419 assert(m_struct_vars.get());
420
421 if (!m_struct_vars->m_struct_laid_out)
422 return false;
423
424 num_elements = m_struct_members.GetSize();
425 size = m_struct_vars->m_struct_size;
426 alignment = m_struct_vars->m_struct_alignment;
427
428 return true;
429}
430
431bool ClangExpressionDeclMap::GetStructElement(const NamedDecl *&decl,
432 llvm::Value *&value,
433 lldb::offset_t &offset,
434 ConstString &name,
435 uint32_t index) {
436 assert(m_struct_vars.get());
437
438 if (!m_struct_vars->m_struct_laid_out)
439 return false;
440
441 if (index >= m_struct_members.GetSize())
442 return false;
443
444 ExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index));
445
446 if (!member_sp)
447 return false;
448
449 ClangExpressionVariable::ParserVars *parser_vars =
450 llvm::cast<ClangExpressionVariable>(member_sp.get())
451 ->GetParserVars(GetParserID());
452 ClangExpressionVariable::JITVars *jit_vars =
453 llvm::cast<ClangExpressionVariable>(member_sp.get())
454 ->GetJITVars(GetParserID());
455
456 if (!parser_vars || !jit_vars || !member_sp->GetValueObject())
457 return false;
458
459 decl = parser_vars->m_named_decl;
460 value = parser_vars->m_llvm_value;
461 offset = jit_vars->m_offset;
462 name = member_sp->GetName();
463
464 return true;
465}
466
467bool ClangExpressionDeclMap::GetFunctionInfo(const NamedDecl *decl,
468 uint64_t &ptr) {
469 ClangExpressionVariable *entity(ClangExpressionVariable::FindVariableInList(
470 m_found_entities, decl, GetParserID()));
471
472 if (!entity)
473 return false;
474
Adrian Prantl05097242018-04-30 16:49:04 +0000475 // We know m_parser_vars is valid since we searched for the variable by its
476 // NamedDecl
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477
478 ClangExpressionVariable::ParserVars *parser_vars =
479 entity->GetParserVars(GetParserID());
480
481 ptr = parser_vars->m_lldb_value.GetScalar().ULongLong();
482
483 return true;
484}
485
486addr_t ClangExpressionDeclMap::GetSymbolAddress(Target &target,
487 Process *process,
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000488 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489 lldb::SymbolType symbol_type,
490 lldb_private::Module *module) {
491 SymbolContextList sc_list;
492
493 if (module)
494 module->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
495 else
496 target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list);
497
498 const uint32_t num_matches = sc_list.GetSize();
499 addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
500
501 for (uint32_t i = 0;
502 i < num_matches &&
503 (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS);
504 i++) {
505 SymbolContext sym_ctx;
506 sc_list.GetContextAtIndex(i, sym_ctx);
507
508 const Address sym_address = sym_ctx.symbol->GetAddress();
509
510 if (!sym_address.IsValid())
511 continue;
512
513 switch (sym_ctx.symbol->GetType()) {
514 case eSymbolTypeCode:
515 case eSymbolTypeTrampoline:
516 symbol_load_addr = sym_address.GetCallableLoadAddress(&target);
517 break;
518
519 case eSymbolTypeResolver:
520 symbol_load_addr = sym_address.GetCallableLoadAddress(&target, true);
521 break;
522
523 case eSymbolTypeReExported: {
524 ConstString reexport_name = sym_ctx.symbol->GetReExportedSymbolName();
525 if (reexport_name) {
526 ModuleSP reexport_module_sp;
527 ModuleSpec reexport_module_spec;
528 reexport_module_spec.GetPlatformFileSpec() =
529 sym_ctx.symbol->GetReExportedSymbolSharedLibrary();
530 if (reexport_module_spec.GetPlatformFileSpec()) {
531 reexport_module_sp =
532 target.GetImages().FindFirstModule(reexport_module_spec);
533 if (!reexport_module_sp) {
534 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
535 reexport_module_sp =
536 target.GetImages().FindFirstModule(reexport_module_spec);
537 }
Sean Callanandf667652013-04-11 02:05:11 +0000538 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539 symbol_load_addr = GetSymbolAddress(
540 target, process, sym_ctx.symbol->GetReExportedSymbolName(),
541 symbol_type, reexport_module_sp.get());
542 }
543 } break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000544
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545 case eSymbolTypeData:
546 case eSymbolTypeRuntime:
547 case eSymbolTypeVariable:
548 case eSymbolTypeLocal:
549 case eSymbolTypeParam:
550 case eSymbolTypeInvalid:
551 case eSymbolTypeAbsolute:
552 case eSymbolTypeException:
553 case eSymbolTypeSourceFile:
554 case eSymbolTypeHeaderFile:
555 case eSymbolTypeObjectFile:
556 case eSymbolTypeCommonBlock:
557 case eSymbolTypeBlock:
558 case eSymbolTypeVariableType:
559 case eSymbolTypeLineEntry:
560 case eSymbolTypeLineHeader:
561 case eSymbolTypeScopeBegin:
562 case eSymbolTypeScopeEnd:
563 case eSymbolTypeAdditional:
564 case eSymbolTypeCompiler:
565 case eSymbolTypeInstrumentation:
566 case eSymbolTypeUndefined:
567 case eSymbolTypeObjCClass:
568 case eSymbolTypeObjCMetaClass:
569 case eSymbolTypeObjCIVar:
570 symbol_load_addr = sym_address.GetLoadAddress(&target);
571 break;
Sean Callanandf667652013-04-11 02:05:11 +0000572 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000574
Kate Stoneb9c1b512016-09-06 20:57:50 +0000575 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process) {
Alex Langforde823bbe2019-06-10 20:53:23 +0000576 ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process);
Sean Callanan549c9f72010-07-13 21:41:46 +0000577
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578 if (runtime) {
579 symbol_load_addr = runtime->LookupRuntimeSymbol(name);
Greg Clayton084db102011-06-23 04:25:29 +0000580 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000582
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583 return symbol_load_addr;
Sean Callananc3a16002011-01-17 23:42:46 +0000584}
585
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000586addr_t ClangExpressionDeclMap::GetSymbolAddress(ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000587 lldb::SymbolType symbol_type) {
588 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000589
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590 if (!m_parser_vars->m_exe_ctx.GetTargetPtr())
591 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000592
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(),
594 m_parser_vars->m_exe_ctx.GetProcessPtr(), name,
595 symbol_type);
Sean Callanand9ca42a2011-05-08 02:21:26 +0000596}
597
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598lldb::VariableSP ClangExpressionDeclMap::FindGlobalVariable(
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000599 Target &target, ModuleSP &module, ConstString name,
Raphael Isemannb6c29d92019-12-23 12:18:49 +0100600 CompilerDeclContext *namespace_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601 VariableList vars;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000602
Kate Stoneb9c1b512016-09-06 20:57:50 +0000603 if (module && namespace_decl)
Pavel Labath34cda142018-05-31 09:46:26 +0000604 module->FindGlobalVariables(name, namespace_decl, -1, vars);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000605 else
Pavel Labath34cda142018-05-31 09:46:26 +0000606 target.GetImages().FindGlobalVariables(name, -1, vars);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000607
Raphael Isemannb6c29d92019-12-23 12:18:49 +0100608 if (vars.GetSize() == 0)
609 return VariableSP();
610 return vars.GetVariableAtIndex(0);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000611}
612
Kate Stoneb9c1b512016-09-06 20:57:50 +0000613ClangASTContext *ClangExpressionDeclMap::GetClangASTContext() {
614 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
615 if (frame == nullptr)
616 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +0000617
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
619 lldb::eSymbolContextBlock);
620 if (sym_ctx.block == nullptr)
621 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +0000622
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext();
624 if (!frame_decl_context)
625 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +0000626
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627 return llvm::dyn_cast_or_null<ClangASTContext>(
628 frame_decl_context.GetTypeSystem());
Siva Chandra03ff5c82016-02-05 19:10:04 +0000629}
630
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631// Interface for ClangASTSource
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000632
Kate Stoneb9c1b512016-09-06 20:57:50 +0000633void ClangExpressionDeclMap::FindExternalVisibleDecls(
634 NameSearchContext &context) {
635 assert(m_ast_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000636
Kate Stoneb9c1b512016-09-06 20:57:50 +0000637 const ConstString name(context.m_decl_name.getAsString().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000638
Kate Stoneb9c1b512016-09-06 20:57:50 +0000639 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000640
Kate Stoneb9c1b512016-09-06 20:57:50 +0000641 if (GetImportInProgress()) {
642 if (log && log->GetVerbose())
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000643 LLDB_LOGF(log, "Ignoring a query during an import");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644 return;
645 }
646
647 static unsigned int invocation_id = 0;
648 unsigned int current_id = invocation_id++;
649
650 if (log) {
651 if (!context.m_decl_context)
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000652 LLDB_LOGF(log,
653 "ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for "
654 "'%s' in a NULL DeclContext",
655 current_id, name.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656 else if (const NamedDecl *context_named_decl =
657 dyn_cast<NamedDecl>(context.m_decl_context))
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000658 LLDB_LOGF(log,
659 "ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for "
660 "'%s' in '%s'",
661 current_id, name.GetCString(),
662 context_named_decl->getNameAsString().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000663 else
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000664 LLDB_LOGF(log,
665 "ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for "
666 "'%s' in a '%s'",
667 current_id, name.GetCString(),
668 context.m_decl_context->getDeclKindName());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669 }
670
671 if (const NamespaceDecl *namespace_context =
672 dyn_cast<NamespaceDecl>(context.m_decl_context)) {
673 if (namespace_context->getName().str() ==
674 std::string(g_lldb_local_vars_namespace_cstr)) {
Raphael Isemann73951a12019-12-23 10:55:21 +0100675 CompilerDeclContext compiler_decl_ctx =
676 m_clang_ast_context->CreateDeclContext(
677 const_cast<clang::DeclContext *>(context.m_decl_context));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000678 FindExternalVisibleDecls(context, lldb::ModuleSP(), compiler_decl_ctx,
679 current_id);
680 return;
Sean Callanan6abfabf2010-11-19 20:20:02 +0000681 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000682
Kate Stoneb9c1b512016-09-06 20:57:50 +0000683 ClangASTImporter::NamespaceMapSP namespace_map =
Sean Callanan68e44232017-09-28 20:20:25 +0000684 m_ast_importer_sp
685 ? m_ast_importer_sp->GetNamespaceMap(namespace_context)
686 : ClangASTImporter::NamespaceMapSP();
687
688 if (!namespace_map)
689 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690
691 if (log && log->GetVerbose())
692 log->Printf(" CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)",
693 current_id, static_cast<void *>(namespace_map.get()),
694 (int)namespace_map->size());
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +0000695
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(),
697 e = namespace_map->end();
698 i != e; ++i) {
699 if (log)
700 log->Printf(" CEDM::FEVD[%u] Searching namespace %s in module %s",
701 current_id, i->second.GetName().AsCString(),
702 i->first->GetFileSpec().GetFilename().GetCString());
703
704 FindExternalVisibleDecls(context, i->first, i->second, current_id);
705 }
706 } else if (isa<TranslationUnitDecl>(context.m_decl_context)) {
707 CompilerDeclContext namespace_decl;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000708
Sean Callanan503aa522011-10-12 00:12:34 +0000709 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710 log->Printf(" CEDM::FEVD[%u] Searching the root namespace", current_id);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000711
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712 FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl,
713 current_id);
714 }
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +0000715
Sean Callananc02a1c02017-04-24 23:14:04 +0000716 ClangASTSource::FindExternalVisibleDecls(context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000717}
718
Raphael Isemannc34478f2019-11-20 13:41:44 +0100719void ClangExpressionDeclMap::MaybeRegisterFunctionBody(
720 FunctionDecl *copied_function_decl) {
721 if (copied_function_decl->getBody() && m_parser_vars->m_code_gen) {
722 clang::DeclGroupRef decl_group_ref(copied_function_decl);
723 m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref);
724 }
725}
726
Raphael Isemannd8a31942019-12-17 16:12:07 +0100727clang::NamedDecl *ClangExpressionDeclMap::GetPersistentDecl(ConstString name) {
Raphael Isemann4aee81c2019-12-17 13:38:07 +0100728 if (!m_parser_vars)
Raphael Isemannd8a31942019-12-17 16:12:07 +0100729 return nullptr;
Raphael Isemannc34478f2019-11-20 13:41:44 +0100730 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
731 if (!target)
Raphael Isemannd8a31942019-12-17 16:12:07 +0100732 return nullptr;
Raphael Isemannc34478f2019-11-20 13:41:44 +0100733
Raphael Isemannf9f49d32019-12-21 22:40:52 +0100734 ClangASTContext::GetScratch(*target);
Raphael Isemannc34478f2019-11-20 13:41:44 +0100735
Raphael Isemannd8a31942019-12-17 16:12:07 +0100736 return m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
737}
738
739void ClangExpressionDeclMap::SearchPersistenDecls(NameSearchContext &context,
740 const ConstString name,
741 unsigned int current_id) {
742 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
743
744 NamedDecl *persistent_decl = GetPersistentDecl(name);
Raphael Isemannc34478f2019-11-20 13:41:44 +0100745
746 if (!persistent_decl)
747 return;
748
749 Decl *parser_persistent_decl = CopyDecl(persistent_decl);
750
751 if (!parser_persistent_decl)
752 return;
753
754 NamedDecl *parser_named_decl = dyn_cast<NamedDecl>(parser_persistent_decl);
755
756 if (!parser_named_decl)
757 return;
758
759 if (clang::FunctionDecl *parser_function_decl =
760 llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl)) {
761 MaybeRegisterFunctionBody(parser_function_decl);
762 }
763
764 LLDB_LOGF(log, " CEDM::FEVD[%u] Found persistent decl %s", current_id,
765 name.GetCString());
766
767 context.AddNamedDecl(parser_named_decl);
768}
Raphael Isemanne7cc8332019-11-20 14:35:18 +0100769
770void ClangExpressionDeclMap::LookUpLldbClass(NameSearchContext &context,
771 unsigned int current_id) {
772 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
773
774 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
775 SymbolContext sym_ctx;
776 if (frame != nullptr)
777 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
778 lldb::eSymbolContextBlock);
779
780 if (m_ctx_obj) {
781 Status status;
782 lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status);
783 if (!ctx_obj_ptr || status.Fail())
784 return;
785
786 AddThisType(context, TypeFromUser(m_ctx_obj->GetCompilerType()),
787 current_id);
788
789 m_struct_vars->m_object_pointer_type =
790 TypeFromUser(ctx_obj_ptr->GetCompilerType());
791
792 return;
793 }
794
795 // Clang is looking for the type of "this"
796
797 if (frame == nullptr)
798 return;
799
800 // Find the block that defines the function represented by "sym_ctx"
801 Block *function_block = sym_ctx.GetFunctionBlock();
802
803 if (!function_block)
804 return;
805
806 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
807
808 if (!function_decl_ctx)
809 return;
810
811 clang::CXXMethodDecl *method_decl =
812 ClangASTContext::DeclContextGetAsCXXMethodDecl(function_decl_ctx);
813
814 if (method_decl) {
815 clang::CXXRecordDecl *class_decl = method_decl->getParent();
816
817 QualType class_qual_type(class_decl->getTypeForDecl(), 0);
818
Raphael Isemannb036f552019-12-25 23:43:52 +0100819 TypeFromUser class_user_type(class_qual_type.getAsOpaquePtr(),
820 function_decl_ctx.GetTypeSystem());
Raphael Isemanne7cc8332019-11-20 14:35:18 +0100821
Raphael Isemann7a6588a2019-11-25 13:27:51 +0100822 LLDB_LOG(log, " CEDM::FEVD[{0}] Adding type for $__lldb_class: {1}",
823 current_id, class_qual_type.getAsString());
Raphael Isemanne7cc8332019-11-20 14:35:18 +0100824
825 AddThisType(context, class_user_type, current_id);
826
827 if (method_decl->isInstance()) {
828 // self is a pointer to the object
829
830 QualType class_pointer_type =
831 method_decl->getASTContext().getPointerType(class_qual_type);
832
Raphael Isemannb036f552019-12-25 23:43:52 +0100833 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
834 function_decl_ctx.GetTypeSystem());
Raphael Isemanne7cc8332019-11-20 14:35:18 +0100835
836 m_struct_vars->m_object_pointer_type = self_user_type;
837 }
838 return;
839 }
840
841 // This branch will get hit if we are executing code in the context of
842 // a function that claims to have an object pointer (through
843 // DW_AT_object_pointer?) but is not formally a method of the class.
844 // In that case, just look up the "this" variable in the current scope
845 // and use its type.
846 // FIXME: This code is formally correct, but clang doesn't currently
847 // emit DW_AT_object_pointer
848 // for C++ so it hasn't actually been tested.
849
850 VariableList *vars = frame->GetVariableList(false);
851
852 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
853
854 if (this_var && this_var->IsInScope(frame) &&
855 this_var->LocationIsValidForFrame(frame)) {
856 Type *this_type = this_var->GetType();
857
858 if (!this_type)
859 return;
860
861 TypeFromUser pointee_type =
862 this_type->GetForwardCompilerType().GetPointeeType();
863
Raphael Isemann7a6588a2019-11-25 13:27:51 +0100864 LLDB_LOG(log, " FEVD[{0}] Adding type for $__lldb_class: {1}", current_id,
865 ClangUtil::GetQualType(pointee_type).getAsString());
Raphael Isemanne7cc8332019-11-20 14:35:18 +0100866
Raphael Isemann7a6588a2019-11-25 13:27:51 +0100867 AddThisType(context, pointee_type, current_id);
868 TypeFromUser this_user_type(this_type->GetFullCompilerType());
869 m_struct_vars->m_object_pointer_type = this_user_type;
Raphael Isemanne7cc8332019-11-20 14:35:18 +0100870 }
871}
872
Raphael Isemann51ad0252019-11-20 15:58:07 +0100873void ClangExpressionDeclMap::LookUpLldbObjCClass(NameSearchContext &context,
874 unsigned int current_id) {
875 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
876
877 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
878
879 if (m_ctx_obj) {
880 Status status;
881 lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status);
882 if (!ctx_obj_ptr || status.Fail())
883 return;
884
885 AddOneType(context, TypeFromUser(m_ctx_obj->GetCompilerType()), current_id);
886
887 m_struct_vars->m_object_pointer_type =
888 TypeFromUser(ctx_obj_ptr->GetCompilerType());
889
890 return;
891 }
892
893 // Clang is looking for the type of "*self"
894
895 if (!frame)
896 return;
897
898 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
899 lldb::eSymbolContextBlock);
900
901 // Find the block that defines the function represented by "sym_ctx"
902 Block *function_block = sym_ctx.GetFunctionBlock();
903
904 if (!function_block)
905 return;
906
907 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
908
909 if (!function_decl_ctx)
910 return;
911
912 clang::ObjCMethodDecl *method_decl =
913 ClangASTContext::DeclContextGetAsObjCMethodDecl(function_decl_ctx);
914
915 if (method_decl) {
916 ObjCInterfaceDecl *self_interface = method_decl->getClassInterface();
917
918 if (!self_interface)
919 return;
920
921 const clang::Type *interface_type = self_interface->getTypeForDecl();
922
923 if (!interface_type)
924 return; // This is unlikely, but we have seen crashes where this
925 // occurred
926
Raphael Isemannb036f552019-12-25 23:43:52 +0100927 TypeFromUser class_user_type(QualType(interface_type, 0).getAsOpaquePtr(),
928 function_decl_ctx.GetTypeSystem());
Raphael Isemann51ad0252019-11-20 15:58:07 +0100929
Raphael Isemann7a6588a2019-11-25 13:27:51 +0100930 LLDB_LOG(log, " FEVD[{0}] Adding type for $__lldb_objc_class: {1}",
931 current_id, ClangUtil::ToString(interface_type));
Raphael Isemann51ad0252019-11-20 15:58:07 +0100932
933 AddOneType(context, class_user_type, current_id);
934
935 if (method_decl->isInstanceMethod()) {
936 // self is a pointer to the object
937
938 QualType class_pointer_type =
939 method_decl->getASTContext().getObjCObjectPointerType(
940 QualType(interface_type, 0));
941
Raphael Isemannb036f552019-12-25 23:43:52 +0100942 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
943 function_decl_ctx.GetTypeSystem());
Raphael Isemann51ad0252019-11-20 15:58:07 +0100944
945 m_struct_vars->m_object_pointer_type = self_user_type;
946 } else {
947 // self is a Class pointer
948 QualType class_type = method_decl->getASTContext().getObjCClassType();
949
Raphael Isemannb036f552019-12-25 23:43:52 +0100950 TypeFromUser self_user_type(class_type.getAsOpaquePtr(),
951 function_decl_ctx.GetTypeSystem());
Raphael Isemann51ad0252019-11-20 15:58:07 +0100952
953 m_struct_vars->m_object_pointer_type = self_user_type;
954 }
955
956 return;
957 }
958 // This branch will get hit if we are executing code in the context of
959 // a function that claims to have an object pointer (through
960 // DW_AT_object_pointer?) but is not formally a method of the class.
961 // In that case, just look up the "self" variable in the current scope
962 // and use its type.
963
964 VariableList *vars = frame->GetVariableList(false);
965
966 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
967
Raphael Isemann1e0d3952019-11-23 21:09:41 +0100968 if (!self_var)
969 return;
970 if (!self_var->IsInScope(frame))
971 return;
972 if (!self_var->LocationIsValidForFrame(frame))
973 return;
Raphael Isemann51ad0252019-11-20 15:58:07 +0100974
Raphael Isemann1e0d3952019-11-23 21:09:41 +0100975 Type *self_type = self_var->GetType();
Raphael Isemann51ad0252019-11-20 15:58:07 +0100976
Raphael Isemann1e0d3952019-11-23 21:09:41 +0100977 if (!self_type)
978 return;
Raphael Isemann51ad0252019-11-20 15:58:07 +0100979
Raphael Isemann1e0d3952019-11-23 21:09:41 +0100980 CompilerType self_clang_type = self_type->GetFullCompilerType();
Raphael Isemann51ad0252019-11-20 15:58:07 +0100981
Raphael Isemann1e0d3952019-11-23 21:09:41 +0100982 if (ClangASTContext::IsObjCClassType(self_clang_type)) {
983 return;
Raphael Isemann51ad0252019-11-20 15:58:07 +0100984 }
Raphael Isemann1e0d3952019-11-23 21:09:41 +0100985 if (!ClangASTContext::IsObjCObjectPointerType(self_clang_type))
986 return;
987 self_clang_type = self_clang_type.GetPointeeType();
988
989 if (!self_clang_type)
990 return;
991
Raphael Isemann7a6588a2019-11-25 13:27:51 +0100992 LLDB_LOG(log, " FEVD[{0}] Adding type for $__lldb_objc_class: {1}",
993 current_id, ClangUtil::ToString(self_type->GetFullCompilerType()));
Raphael Isemann1e0d3952019-11-23 21:09:41 +0100994
995 TypeFromUser class_user_type(self_clang_type);
996
997 AddOneType(context, class_user_type, current_id);
998
999 TypeFromUser self_user_type(self_type->GetFullCompilerType());
1000
1001 m_struct_vars->m_object_pointer_type = self_user_type;
Raphael Isemann51ad0252019-11-20 15:58:07 +01001002}
1003
Raphael Isemann337151f2019-11-21 10:45:04 +01001004void ClangExpressionDeclMap::LookupLocalVarNamespace(
Raphael Isemann1e0d3952019-11-23 21:09:41 +01001005 SymbolContext &sym_ctx, NameSearchContext &name_context) {
1006 if (sym_ctx.block == nullptr)
1007 return;
Raphael Isemann337151f2019-11-21 10:45:04 +01001008
Raphael Isemann1e0d3952019-11-23 21:09:41 +01001009 CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext();
1010 if (!frame_decl_context)
1011 return;
Raphael Isemann337151f2019-11-21 10:45:04 +01001012
Raphael Isemann1e0d3952019-11-23 21:09:41 +01001013 ClangASTContext *frame_ast = llvm::dyn_cast_or_null<ClangASTContext>(
1014 frame_decl_context.GetTypeSystem());
1015 if (!frame_ast)
1016 return;
1017
Raphael Isemannbc7f1df2019-11-29 13:02:41 +01001018 clang::NamespaceDecl *namespace_decl =
1019 m_clang_ast_context->GetUniqueNamespaceDeclaration(
1020 g_lldb_local_vars_namespace_cstr, nullptr);
Raphael Isemann1e0d3952019-11-23 21:09:41 +01001021 if (!namespace_decl)
1022 return;
1023
1024 name_context.AddNamedDecl(namespace_decl);
1025 clang::DeclContext *ctxt = clang::Decl::castToDeclContext(namespace_decl);
1026 ctxt->setHasExternalVisibleStorage(true);
1027 name_context.m_found.local_vars_nsp = true;
Raphael Isemann337151f2019-11-21 10:45:04 +01001028}
1029
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001030void ClangExpressionDeclMap::LookupInModulesDeclVendor(
1031 NameSearchContext &context, ConstString name, unsigned current_id) {
1032 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1033
Raphael Isemann4aee81c2019-12-17 13:38:07 +01001034 if (!m_target)
1035 return;
1036
Raphael Isemann46883f42019-11-23 20:06:50 +01001037 auto *modules_decl_vendor = m_target->GetClangModulesDeclVendor();
1038 if (!modules_decl_vendor)
1039 return;
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001040
Raphael Isemann46883f42019-11-23 20:06:50 +01001041 bool append = false;
1042 uint32_t max_matches = 1;
1043 std::vector<clang::NamedDecl *> decls;
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001044
Raphael Isemann46883f42019-11-23 20:06:50 +01001045 if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls))
1046 return;
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001047
Raphael Isemann46883f42019-11-23 20:06:50 +01001048 assert(!decls.empty() && "FindDecls returned true but no decls?");
1049 clang::NamedDecl *const decl_from_modules = decls[0];
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001050
Raphael Isemann46883f42019-11-23 20:06:50 +01001051 LLDB_LOG(log,
1052 " CAS::FEVD[{0}] Matching decl found for "
1053 "\"{1}\" in the modules",
1054 current_id, name);
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001055
Raphael Isemann46883f42019-11-23 20:06:50 +01001056 clang::Decl *copied_decl = CopyDecl(decl_from_modules);
1057 if (!copied_decl) {
1058 LLDB_LOG(log,
1059 " CAS::FEVD[{0}] - Couldn't export a "
1060 "declaration from the modules",
1061 current_id);
1062 return;
1063 }
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001064
Raphael Isemann46883f42019-11-23 20:06:50 +01001065 if (auto copied_function = dyn_cast<clang::FunctionDecl>(copied_decl)) {
1066 MaybeRegisterFunctionBody(copied_function);
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001067
Raphael Isemann46883f42019-11-23 20:06:50 +01001068 context.AddNamedDecl(copied_function);
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001069
Raphael Isemann46883f42019-11-23 20:06:50 +01001070 context.m_found.function_with_type_info = true;
1071 context.m_found.function = true;
1072 } else if (auto copied_var = dyn_cast<clang::VarDecl>(copied_decl)) {
1073 context.AddNamedDecl(copied_var);
1074 context.m_found.variable = true;
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001075 }
1076}
1077
Raphael Isemann7fa976d2019-11-21 12:16:00 +01001078bool ClangExpressionDeclMap::LookupLocalVariable(
1079 NameSearchContext &context, ConstString name, unsigned current_id,
1080 SymbolContext &sym_ctx, CompilerDeclContext &namespace_decl) {
Raphael Isemann7a0c5482019-11-23 17:01:25 +01001081 if (sym_ctx.block == nullptr)
1082 return false;
Raphael Isemann7fa976d2019-11-21 12:16:00 +01001083
Raphael Isemann7a0c5482019-11-23 17:01:25 +01001084 CompilerDeclContext decl_context = sym_ctx.block->GetDeclContext();
1085 if (!decl_context)
1086 return false;
1087
1088 // Make sure that the variables are parsed so that we have the
1089 // declarations.
Raphael Isemann7fa976d2019-11-21 12:16:00 +01001090 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
Raphael Isemann7a0c5482019-11-23 17:01:25 +01001091 VariableListSP vars = frame->GetInScopeVariableList(true);
1092 for (size_t i = 0; i < vars->GetSize(); i++)
1093 vars->GetVariableAtIndex(i)->GetDecl();
Raphael Isemann7fa976d2019-11-21 12:16:00 +01001094
Raphael Isemann7a0c5482019-11-23 17:01:25 +01001095 // Search for declarations matching the name. Do not include imported
1096 // decls in the search if we are looking for decls in the artificial
1097 // namespace $__lldb_local_vars.
1098 std::vector<CompilerDecl> found_decls =
1099 decl_context.FindDeclByName(name, namespace_decl.IsValid());
Raphael Isemann7fa976d2019-11-21 12:16:00 +01001100
Raphael Isemann7a0c5482019-11-23 17:01:25 +01001101 VariableSP var;
1102 bool variable_found = false;
1103 for (CompilerDecl decl : found_decls) {
1104 for (size_t vi = 0, ve = vars->GetSize(); vi != ve; ++vi) {
1105 VariableSP candidate_var = vars->GetVariableAtIndex(vi);
1106 if (candidate_var->GetDecl() == decl) {
1107 var = candidate_var;
1108 break;
Raphael Isemann7fa976d2019-11-21 12:16:00 +01001109 }
1110 }
Raphael Isemann7a0c5482019-11-23 17:01:25 +01001111
1112 if (var && !variable_found) {
1113 variable_found = true;
1114 ValueObjectSP valobj = ValueObjectVariable::Create(frame, var);
1115 AddOneVariable(context, var, valobj, current_id);
1116 context.m_found.variable = true;
1117 }
Raphael Isemann7fa976d2019-11-21 12:16:00 +01001118 }
Raphael Isemann7a0c5482019-11-23 17:01:25 +01001119 return variable_found;
Raphael Isemann7fa976d2019-11-21 12:16:00 +01001120}
1121
Raphael Isemann16c06532019-11-29 15:05:42 +01001122/// Structure to hold the info needed when comparing function
1123/// declarations.
1124namespace {
1125struct FuncDeclInfo {
1126 ConstString m_name;
1127 CompilerType m_copied_type;
1128 uint32_t m_decl_lvl;
1129 SymbolContext m_sym_ctx;
1130};
1131} // namespace
1132
1133SymbolContextList ClangExpressionDeclMap::SearchFunctionsInSymbolContexts(
1134 const SymbolContextList &sc_list,
1135 const CompilerDeclContext &frame_decl_context) {
1136 // First, symplify things by looping through the symbol contexts to
1137 // remove unwanted functions and separate out the functions we want to
1138 // compare and prune into a separate list. Cache the info needed about
1139 // the function declarations in a vector for efficiency.
1140 uint32_t num_indices = sc_list.GetSize();
1141 SymbolContextList sc_sym_list;
1142 std::vector<FuncDeclInfo> decl_infos;
1143 decl_infos.reserve(num_indices);
1144 clang::DeclContext *frame_decl_ctx =
1145 (clang::DeclContext *)frame_decl_context.GetOpaqueDeclContext();
1146 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(
1147 frame_decl_context.GetTypeSystem());
1148
1149 for (uint32_t index = 0; index < num_indices; ++index) {
1150 FuncDeclInfo fdi;
1151 SymbolContext sym_ctx;
1152 sc_list.GetContextAtIndex(index, sym_ctx);
1153
1154 // We don't know enough about symbols to compare them, but we should
1155 // keep them in the list.
1156 Function *function = sym_ctx.function;
1157 if (!function) {
1158 sc_sym_list.Append(sym_ctx);
1159 continue;
1160 }
1161 // Filter out functions without declaration contexts, as well as
1162 // class/instance methods, since they'll be skipped in the code that
1163 // follows anyway.
1164 CompilerDeclContext func_decl_context = function->GetDeclContext();
1165 if (!func_decl_context ||
1166 func_decl_context.IsClassMethod(nullptr, nullptr, nullptr))
1167 continue;
1168 // We can only prune functions for which we can copy the type.
1169 CompilerType func_clang_type = function->GetType()->GetFullCompilerType();
1170 CompilerType copied_func_type = GuardedCopyType(func_clang_type);
1171 if (!copied_func_type) {
1172 sc_sym_list.Append(sym_ctx);
1173 continue;
1174 }
1175
1176 fdi.m_sym_ctx = sym_ctx;
1177 fdi.m_name = function->GetName();
1178 fdi.m_copied_type = copied_func_type;
1179 fdi.m_decl_lvl = LLDB_INVALID_DECL_LEVEL;
1180 if (fdi.m_copied_type && func_decl_context) {
1181 // Call CountDeclLevels to get the number of parent scopes we have
1182 // to look through before we find the function declaration. When
1183 // comparing functions of the same type, the one with a lower count
1184 // will be closer to us in the lookup scope and shadows the other.
1185 clang::DeclContext *func_decl_ctx =
1186 (clang::DeclContext *)func_decl_context.GetOpaqueDeclContext();
1187 fdi.m_decl_lvl = ast->CountDeclLevels(frame_decl_ctx, func_decl_ctx,
1188 &fdi.m_name, &fdi.m_copied_type);
1189 }
1190 decl_infos.emplace_back(fdi);
1191 }
1192
1193 // Loop through the functions in our cache looking for matching types,
1194 // then compare their scope levels to see which is closer.
1195 std::multimap<CompilerType, const FuncDeclInfo *> matches;
1196 for (const FuncDeclInfo &fdi : decl_infos) {
1197 const CompilerType t = fdi.m_copied_type;
1198 auto q = matches.find(t);
1199 if (q != matches.end()) {
1200 if (q->second->m_decl_lvl > fdi.m_decl_lvl)
1201 // This function is closer; remove the old set.
1202 matches.erase(t);
1203 else if (q->second->m_decl_lvl < fdi.m_decl_lvl)
1204 // The functions in our set are closer - skip this one.
1205 continue;
1206 }
1207 matches.insert(std::make_pair(t, &fdi));
1208 }
1209
1210 // Loop through our matches and add their symbol contexts to our list.
1211 SymbolContextList sc_func_list;
1212 for (const auto &q : matches)
1213 sc_func_list.Append(q.second->m_sym_ctx);
1214
1215 // Rejoin the lists with the functions in front.
1216 sc_func_list.Append(sc_sym_list);
1217 return sc_func_list;
1218}
1219
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001220void ClangExpressionDeclMap::LookupFunction(NameSearchContext &context,
1221 lldb::ModuleSP module_sp,
1222 ConstString name,
1223 CompilerDeclContext &namespace_decl,
1224 unsigned current_id) {
Raphael Isemann4aee81c2019-12-17 13:38:07 +01001225 if (!m_parser_vars)
1226 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001227
Kate Stoneb9c1b512016-09-06 20:57:50 +00001228 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Sean Callananb3a36df2016-03-19 00:51:43 +00001229
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001230 std::vector<clang::NamedDecl *> decls_from_modules;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001231
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001232 if (target) {
1233 if (ClangModulesDeclVendor *decl_vendor =
1234 target->GetClangModulesDeclVendor()) {
1235 decl_vendor->FindDecls(name, false, UINT32_MAX, decls_from_modules);
Sean Callananc02a1c02017-04-24 23:14:04 +00001236 }
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001237 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001238
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001239 const bool include_inlines = false;
Raphael Isemann24e98862019-11-21 13:43:48 +01001240 SymbolContextList sc_list;
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001241 if (namespace_decl && module_sp) {
1242 const bool include_symbols = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001243
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001244 module_sp->FindFunctions(name, &namespace_decl, eFunctionNameTypeBase,
1245 include_symbols, include_inlines, sc_list);
1246 } else if (target && !namespace_decl) {
1247 const bool include_symbols = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001248
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001249 // TODO Fix FindFunctions so that it doesn't return
1250 // instance methods for eFunctionNameTypeBase.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001251
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001252 target->GetImages().FindFunctions(name, eFunctionNameTypeFull,
1253 include_symbols, include_inlines,
1254 sc_list);
1255 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001256
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001257 // If we found more than one function, see if we can use the frame's decl
1258 // context to remove functions that are shadowed by other functions which
1259 // match in type but are nearer in scope.
1260 //
1261 // AddOneFunction will not add a function whose type has already been
1262 // added, so if there's another function in the list with a matching type,
1263 // check to see if their decl context is a parent of the current frame's or
1264 // was imported via a and using statement, and pick the best match
1265 // according to lookup rules.
1266 if (sc_list.GetSize() > 1) {
1267 // Collect some info about our frame's context.
1268 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1269 SymbolContext frame_sym_ctx;
1270 if (frame != nullptr)
1271 frame_sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
1272 lldb::eSymbolContextBlock);
1273 CompilerDeclContext frame_decl_context =
1274 frame_sym_ctx.block != nullptr ? frame_sym_ctx.block->GetDeclContext()
1275 : CompilerDeclContext();
Sean Callananc02a1c02017-04-24 23:14:04 +00001276
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001277 // We can't do this without a compiler decl context for our frame.
1278 if (frame_decl_context) {
Raphael Isemann16c06532019-11-29 15:05:42 +01001279 sc_list = SearchFunctionsInSymbolContexts(sc_list, frame_decl_context);
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001280 }
1281 }
1282
1283 if (sc_list.GetSize()) {
1284 Symbol *extern_symbol = nullptr;
1285 Symbol *non_extern_symbol = nullptr;
1286
1287 for (uint32_t index = 0, num_indices = sc_list.GetSize();
1288 index < num_indices; ++index) {
1289 SymbolContext sym_ctx;
1290 sc_list.GetContextAtIndex(index, sym_ctx);
1291
1292 if (sym_ctx.function) {
1293 CompilerDeclContext decl_ctx = sym_ctx.function->GetDeclContext();
1294
1295 if (!decl_ctx)
1296 continue;
1297
1298 // Filter out class/instance methods.
1299 if (decl_ctx.IsClassMethod(nullptr, nullptr, nullptr))
1300 continue;
1301
1302 AddOneFunction(context, sym_ctx.function, nullptr, current_id);
1303 context.m_found.function_with_type_info = true;
1304 context.m_found.function = true;
1305 } else if (sym_ctx.symbol) {
1306 if (sym_ctx.symbol->GetType() == eSymbolTypeReExported && target) {
1307 sym_ctx.symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target);
1308 if (sym_ctx.symbol == nullptr)
1309 continue;
1310 }
1311
1312 if (sym_ctx.symbol->IsExternal())
1313 extern_symbol = sym_ctx.symbol;
1314 else
1315 non_extern_symbol = sym_ctx.symbol;
1316 }
1317 }
1318
1319 if (!context.m_found.function_with_type_info) {
1320 for (clang::NamedDecl *decl : decls_from_modules) {
1321 if (llvm::isa<clang::FunctionDecl>(decl)) {
1322 clang::NamedDecl *copied_decl =
1323 llvm::cast_or_null<FunctionDecl>(CopyDecl(decl));
1324 if (copied_decl) {
1325 context.AddNamedDecl(copied_decl);
1326 context.m_found.function_with_type_info = true;
1327 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001328 }
1329 }
Sean Callananb3a36df2016-03-19 00:51:43 +00001330 }
Sean Callananc02a1c02017-04-24 23:14:04 +00001331
1332 if (!context.m_found.function_with_type_info) {
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001333 if (extern_symbol) {
1334 AddOneFunction(context, nullptr, extern_symbol, current_id);
1335 context.m_found.function = true;
1336 } else if (non_extern_symbol) {
1337 AddOneFunction(context, nullptr, non_extern_symbol, current_id);
1338 context.m_found.function = true;
1339 }
1340 }
1341 }
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001342}
1343
1344void ClangExpressionDeclMap::FindExternalVisibleDecls(
1345 NameSearchContext &context, lldb::ModuleSP module_sp,
1346 CompilerDeclContext &namespace_decl, unsigned int current_id) {
1347 assert(m_ast_context);
1348
1349 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1350
1351 const ConstString name(context.m_decl_name.getAsString().c_str());
1352 if (IgnoreName(name, false))
1353 return;
1354
1355 // Only look for functions by name out in our symbols if the function doesn't
1356 // start with our phony prefix of '$'
Raphael Isemann4aee81c2019-12-17 13:38:07 +01001357
1358 Target *target = nullptr;
1359 StackFrame *frame = nullptr;
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001360 SymbolContext sym_ctx;
Raphael Isemann4aee81c2019-12-17 13:38:07 +01001361 if (m_parser_vars) {
1362 target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1363 frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1364 }
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001365 if (frame != nullptr)
1366 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
1367 lldb::eSymbolContextBlock);
1368
1369 // Try the persistent decls, which take precedence over all else.
1370 if (!namespace_decl)
1371 SearchPersistenDecls(context, name, current_id);
1372
Raphael Isemann8cf8ec402019-11-21 14:40:45 +01001373 if (name.GetStringRef().startswith("$") && !namespace_decl) {
1374 if (name == "$__lldb_class") {
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001375 LookUpLldbClass(context, current_id);
1376 return;
1377 }
1378
Raphael Isemann8cf8ec402019-11-21 14:40:45 +01001379 if (name == "$__lldb_objc_class") {
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001380 LookUpLldbObjCClass(context, current_id);
1381 return;
1382 }
Raphael Isemann8cf8ec402019-11-21 14:40:45 +01001383 if (name == g_lldb_local_vars_namespace_cstr) {
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001384 LookupLocalVarNamespace(sym_ctx, context);
1385 return;
1386 }
1387
1388 // any other $__lldb names should be weeded out now
1389 if (name.GetStringRef().startswith("$__lldb"))
1390 return;
1391
Raphael Isemannd8a31942019-12-17 16:12:07 +01001392 // No ParserVars means we can't do register or variable lookup.
1393 if (!m_parser_vars)
1394 return;
1395
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001396 ExpressionVariableSP pvar_sp(
1397 m_parser_vars->m_persistent_vars->GetVariable(name));
1398
1399 if (pvar_sp) {
1400 AddOneVariable(context, pvar_sp, current_id);
1401 return;
1402 }
1403
Raphael Isemann8cf8ec402019-11-21 14:40:45 +01001404 assert(name.GetStringRef().startswith("$"));
1405 llvm::StringRef reg_name = name.GetStringRef().substr(1);
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001406
1407 if (m_parser_vars->m_exe_ctx.GetRegisterContext()) {
1408 const RegisterInfo *reg_info(
1409 m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(
1410 reg_name));
1411
1412 if (reg_info) {
1413 LLDB_LOGF(log, " CEDM::FEVD[%u] Found register %s", current_id,
1414 reg_info->name);
1415
1416 AddOneRegister(context, reg_info, current_id);
1417 }
1418 }
1419 return;
1420 }
1421
Raphael Isemann8cf8ec402019-11-21 14:40:45 +01001422 bool local_var_lookup = !namespace_decl || (namespace_decl.GetName() ==
1423 g_lldb_local_vars_namespace_cstr);
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001424 if (frame && local_var_lookup)
1425 if (LookupLocalVariable(context, name, current_id, sym_ctx, namespace_decl))
1426 return;
1427
1428 if (target) {
1429 ValueObjectSP valobj;
1430 VariableSP var;
Raphael Isemannb6c29d92019-12-23 12:18:49 +01001431 var = FindGlobalVariable(*target, module_sp, name, &namespace_decl);
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001432
1433 if (var) {
1434 valobj = ValueObjectVariable::Create(target, var);
1435 AddOneVariable(context, var, valobj, current_id);
1436 context.m_found.variable = true;
1437 return;
1438 }
1439 }
1440
1441 LookupFunction(context, module_sp, name, namespace_decl, current_id);
Sean Callananc02a1c02017-04-24 23:14:04 +00001442
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001443 // Try the modules next.
1444 if (!context.m_found.function_with_type_info)
1445 LookupInModulesDeclVendor(context, name, current_id);
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001446
1447 if (target && !context.m_found.variable && !namespace_decl) {
1448 // We couldn't find a non-symbol variable for this. Now we'll hunt for a
1449 // generic data symbol, and -- if it is found -- treat it as a variable.
1450 Status error;
1451
1452 const Symbol *data_symbol =
1453 m_parser_vars->m_sym_ctx.FindBestGlobalDataSymbol(name, error);
1454
1455 if (!error.Success()) {
1456 const unsigned diag_id =
1457 m_ast_context->getDiagnostics().getCustomDiagID(
1458 clang::DiagnosticsEngine::Level::Error, "%0");
1459 m_ast_context->getDiagnostics().Report(diag_id) << error.AsCString();
Sean Callananc02a1c02017-04-24 23:14:04 +00001460 }
1461
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001462 if (data_symbol) {
1463 std::string warning("got name from symbols: ");
1464 warning.append(name.AsCString());
1465 const unsigned diag_id =
1466 m_ast_context->getDiagnostics().getCustomDiagID(
1467 clang::DiagnosticsEngine::Level::Warning, "%0");
1468 m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str();
1469 AddOneGenericVariable(context, *data_symbol, current_id);
1470 context.m_found.variable = true;
Sean Callananc02a1c02017-04-24 23:14:04 +00001471 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001472 }
Sean Callanan6b4067c2010-07-17 00:43:37 +00001473}
Greg Claytona2721472011-06-25 00:44:06 +00001474
Kate Stoneb9c1b512016-09-06 20:57:50 +00001475bool ClangExpressionDeclMap::GetVariableValue(VariableSP &var,
1476 lldb_private::Value &var_location,
1477 TypeFromUser *user_type,
1478 TypeFromParser *parser_type) {
1479 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001480
Kate Stoneb9c1b512016-09-06 20:57:50 +00001481 Type *var_type = var->GetType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001482
Kate Stoneb9c1b512016-09-06 20:57:50 +00001483 if (!var_type) {
Sean Callananea22d422010-07-16 00:09:46 +00001484 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001485 log->PutCString("Skipped a definition because it has no type");
1486 return false;
1487 }
1488
1489 CompilerType var_clang_type = var_type->GetFullCompilerType();
1490
1491 if (!var_clang_type) {
1492 if (log)
1493 log->PutCString("Skipped a definition because it has no Clang type");
1494 return false;
1495 }
1496
1497 ClangASTContext *clang_ast = llvm::dyn_cast_or_null<ClangASTContext>(
1498 var_type->GetForwardCompilerType().GetTypeSystem());
1499
1500 if (!clang_ast) {
1501 if (log)
1502 log->PutCString("Skipped a definition because it has no Clang AST");
1503 return false;
1504 }
1505
Kate Stoneb9c1b512016-09-06 20:57:50 +00001506 DWARFExpression &var_location_expr = var->LocationExpression();
1507
1508 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Zachary Turner97206d52017-05-12 04:51:55 +00001509 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001510
1511 if (var->GetLocationIsConstantValueData()) {
1512 DataExtractor const_value_extractor;
1513
1514 if (var_location_expr.GetExpressionData(const_value_extractor)) {
1515 var_location = Value(const_value_extractor.GetDataStart(),
1516 const_value_extractor.GetByteSize());
1517 var_location.SetValueType(Value::eValueTypeHostAddress);
1518 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001519 LLDB_LOGF(log, "Error evaluating constant variable: %s", err.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001520 return false;
Greg Clayton7b462cc2010-10-15 22:48:33 +00001521 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001522 }
1523
1524 CompilerType type_to_use = GuardedCopyType(var_clang_type);
1525
1526 if (!type_to_use) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001527 LLDB_LOGF(log,
1528 "Couldn't copy a variable's type into the parser's AST context");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001529
1530 return false;
1531 }
1532
1533 if (parser_type)
1534 *parser_type = TypeFromParser(type_to_use);
1535
1536 if (var_location.GetContextType() == Value::eContextTypeInvalid)
1537 var_location.SetCompilerType(type_to_use);
1538
1539 if (var_location.GetValueType() == Value::eValueTypeFileAddress) {
1540 SymbolContext var_sc;
1541 var->CalculateSymbolContext(&var_sc);
1542
1543 if (!var_sc.module_sp)
1544 return false;
1545
1546 Address so_addr(var_location.GetScalar().ULongLong(),
1547 var_sc.module_sp->GetSectionList());
1548
1549 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
1550
1551 if (load_addr != LLDB_INVALID_ADDRESS) {
1552 var_location.GetScalar() = load_addr;
1553 var_location.SetValueType(Value::eValueTypeLoadAddress);
1554 }
1555 }
1556
1557 if (user_type)
1558 *user_type = TypeFromUser(var_clang_type);
1559
1560 return true;
Sean Callanan468574b2010-06-22 23:46:24 +00001561}
1562
Kate Stoneb9c1b512016-09-06 20:57:50 +00001563void ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
1564 VariableSP var,
1565 ValueObjectSP valobj,
1566 unsigned int current_id) {
1567 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001568
Kate Stoneb9c1b512016-09-06 20:57:50 +00001569 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001570
Kate Stoneb9c1b512016-09-06 20:57:50 +00001571 TypeFromUser ut;
1572 TypeFromParser pt;
1573 Value var_location;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001574
Kate Stoneb9c1b512016-09-06 20:57:50 +00001575 if (!GetVariableValue(var, var_location, &ut, &pt))
1576 return;
1577
1578 clang::QualType parser_opaque_type =
1579 QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
1580
1581 if (parser_opaque_type.isNull())
1582 return;
1583
1584 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr()) {
1585 if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1586 CompleteType(tag_type->getDecl());
1587 if (const ObjCObjectPointerType *objc_object_ptr_type =
1588 dyn_cast<ObjCObjectPointerType>(parser_type))
1589 CompleteType(objc_object_ptr_type->getInterfaceDecl());
1590 }
1591
1592 bool is_reference = pt.IsReferenceType();
1593
Konrad Kleine248a1302019-05-23 11:14:47 +00001594 NamedDecl *var_decl = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001595 if (is_reference)
1596 var_decl = context.AddVarDecl(pt);
1597 else
1598 var_decl = context.AddVarDecl(pt.GetLValueReferenceType());
1599
1600 std::string decl_name(context.m_decl_name.getAsString());
1601 ConstString entity_name(decl_name.c_str());
1602 ClangExpressionVariable *entity(new ClangExpressionVariable(valobj));
1603 m_found_entities.AddNewlyConstructedVariable(entity);
1604
1605 assert(entity);
1606 entity->EnableParserVars(GetParserID());
1607 ClangExpressionVariable::ParserVars *parser_vars =
1608 entity->GetParserVars(GetParserID());
1609 parser_vars->m_parser_type = pt;
1610 parser_vars->m_named_decl = var_decl;
Konrad Kleine248a1302019-05-23 11:14:47 +00001611 parser_vars->m_llvm_value = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001612 parser_vars->m_lldb_value = var_location;
1613 parser_vars->m_lldb_var = var;
1614
1615 if (is_reference)
1616 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference;
1617
Raphael Isemann7a6588a2019-11-25 13:27:51 +01001618 LLDB_LOG(log,
1619 " CEDM::FEVD[{0}] Found variable {1}, returned\n{2} (original {3})",
1620 current_id, decl_name, ClangUtil::DumpDecl(var_decl),
1621 ClangUtil::ToString(ut));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001622}
1623
1624void ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
1625 ExpressionVariableSP &pvar_sp,
1626 unsigned int current_id) {
1627 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1628
1629 TypeFromUser user_type(
1630 llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetTypeFromUser());
1631
1632 TypeFromParser parser_type(GuardedCopyType(user_type));
1633
1634 if (!parser_type.GetOpaqueQualType()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001635 LLDB_LOGF(log, " CEDM::FEVD[%u] Couldn't import type for pvar %s",
1636 current_id, pvar_sp->GetName().GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001637 return;
1638 }
1639
1640 NamedDecl *var_decl =
1641 context.AddVarDecl(parser_type.GetLValueReferenceType());
1642
1643 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1644 ->EnableParserVars(GetParserID());
1645 ClangExpressionVariable::ParserVars *parser_vars =
1646 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1647 ->GetParserVars(GetParserID());
1648 parser_vars->m_parser_type = parser_type;
1649 parser_vars->m_named_decl = var_decl;
Konrad Kleine248a1302019-05-23 11:14:47 +00001650 parser_vars->m_llvm_value = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001651 parser_vars->m_lldb_value.Clear();
1652
Raphael Isemann7a6588a2019-11-25 13:27:51 +01001653 LLDB_LOG(log, " CEDM::FEVD[{0}] Added pvar {1}, returned\n{2}", current_id,
1654 pvar_sp->GetName(), ClangUtil::DumpDecl(var_decl));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001655}
1656
1657void ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context,
1658 const Symbol &symbol,
1659 unsigned int current_id) {
1660 assert(m_parser_vars.get());
1661
1662 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1663
1664 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1665
Konrad Kleine248a1302019-05-23 11:14:47 +00001666 if (target == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001667 return;
1668
Alex Langford30318182019-11-14 13:41:52 -08001669 ClangASTContext *scratch_ast_context = ClangASTContext::GetScratch(*target);
1670 if (!scratch_ast_context)
1671 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001672
Raphael Isemannc502bae2019-11-20 12:40:08 +01001673 TypeFromUser user_type(scratch_ast_context->GetBasicType(eBasicTypeVoid)
1674 .GetPointerType()
1675 .GetLValueReferenceType());
Raphael Isemannbc7f1df2019-11-29 13:02:41 +01001676 TypeFromParser parser_type(m_clang_ast_context->GetBasicType(eBasicTypeVoid)
Raphael Isemannc502bae2019-11-20 12:40:08 +01001677 .GetPointerType()
1678 .GetLValueReferenceType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001679 NamedDecl *var_decl = context.AddVarDecl(parser_type);
1680
1681 std::string decl_name(context.m_decl_name.getAsString());
1682 ConstString entity_name(decl_name.c_str());
1683 ClangExpressionVariable *entity(new ClangExpressionVariable(
1684 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), entity_name,
1685 user_type, m_parser_vars->m_target_info.byte_order,
1686 m_parser_vars->m_target_info.address_byte_size));
1687 m_found_entities.AddNewlyConstructedVariable(entity);
1688
1689 entity->EnableParserVars(GetParserID());
1690 ClangExpressionVariable::ParserVars *parser_vars =
1691 entity->GetParserVars(GetParserID());
1692
1693 const Address symbol_address = symbol.GetAddress();
1694 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target);
1695
1696 // parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType,
1697 // user_type.GetOpaqueQualType());
1698 parser_vars->m_lldb_value.SetCompilerType(user_type);
1699 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr;
1700 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1701
1702 parser_vars->m_parser_type = parser_type;
1703 parser_vars->m_named_decl = var_decl;
Konrad Kleine248a1302019-05-23 11:14:47 +00001704 parser_vars->m_llvm_value = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001705 parser_vars->m_lldb_sym = &symbol;
1706
Raphael Isemann7a6588a2019-11-25 13:27:51 +01001707 LLDB_LOG(log, " CEDM::FEVD[{0}] Found variable {1}, returned\n{2}",
1708 current_id, decl_name, ClangUtil::DumpDecl(var_decl));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001709}
1710
Kate Stoneb9c1b512016-09-06 20:57:50 +00001711void ClangExpressionDeclMap::AddOneRegister(NameSearchContext &context,
1712 const RegisterInfo *reg_info,
1713 unsigned int current_id) {
1714 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001715
Kate Stoneb9c1b512016-09-06 20:57:50 +00001716 CompilerType clang_type =
Raphael Isemannc214c922019-11-29 13:43:23 +01001717 m_clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
1718 reg_info->encoding, reg_info->byte_size * 8);
Sean Callanan7736a202016-04-29 18:09:03 +00001719
Kate Stoneb9c1b512016-09-06 20:57:50 +00001720 if (!clang_type) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001721 LLDB_LOGF(log, " Tried to add a type for %s, but couldn't get one",
1722 context.m_decl_name.getAsString().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001723 return;
1724 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001725
Kate Stoneb9c1b512016-09-06 20:57:50 +00001726 TypeFromParser parser_clang_type(clang_type);
Sean Callananfa4fab72013-02-01 06:55:48 +00001727
Kate Stoneb9c1b512016-09-06 20:57:50 +00001728 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001729
Kate Stoneb9c1b512016-09-06 20:57:50 +00001730 ClangExpressionVariable *entity(new ClangExpressionVariable(
1731 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1732 m_parser_vars->m_target_info.byte_order,
1733 m_parser_vars->m_target_info.address_byte_size));
1734 m_found_entities.AddNewlyConstructedVariable(entity);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001735
Kate Stoneb9c1b512016-09-06 20:57:50 +00001736 std::string decl_name(context.m_decl_name.getAsString());
1737 entity->SetName(ConstString(decl_name.c_str()));
1738 entity->SetRegisterInfo(reg_info);
1739 entity->EnableParserVars(GetParserID());
1740 ClangExpressionVariable::ParserVars *parser_vars =
1741 entity->GetParserVars(GetParserID());
1742 parser_vars->m_parser_type = parser_clang_type;
1743 parser_vars->m_named_decl = var_decl;
Konrad Kleine248a1302019-05-23 11:14:47 +00001744 parser_vars->m_llvm_value = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001745 parser_vars->m_lldb_value.Clear();
1746 entity->m_flags |= ClangExpressionVariable::EVBareRegister;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001747
Raphael Isemann7a6588a2019-11-25 13:27:51 +01001748 LLDB_LOG(log, " CEDM::FEVD[{0}] Added register {1}, returned\n{2}",
1749 current_id, context.m_decl_name.getAsString(),
1750 ClangUtil::DumpDecl(var_decl));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001751}
1752
1753void ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context,
1754 Function *function, Symbol *symbol,
1755 unsigned int current_id) {
1756 assert(m_parser_vars.get());
1757
1758 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1759
Konrad Kleine248a1302019-05-23 11:14:47 +00001760 NamedDecl *function_decl = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001761 Address fun_address;
1762 CompilerType function_clang_type;
1763
1764 bool is_indirect_function = false;
1765
1766 if (function) {
1767 Type *function_type = function->GetType();
1768
Luke Drummondf5bb1d62016-12-19 17:22:44 +00001769 const auto lang = function->GetCompileUnit()->GetLanguage();
1770 const auto name = function->GetMangled().GetMangledName().AsCString();
1771 const bool extern_c = (Language::LanguageIsC(lang) &&
1772 !CPlusPlusLanguage::IsCPPMangledName(name)) ||
1773 (Language::LanguageIsObjC(lang) &&
1774 !Language::LanguageIsCPlusPlus(lang));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001775
1776 if (!extern_c) {
1777 TypeSystem *type_system = function->GetDeclContext().GetTypeSystem();
Davide Italianofe34df52017-09-30 21:16:56 +00001778 if (llvm::isa<ClangASTContext>(type_system)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001779 clang::DeclContext *src_decl_context =
1780 (clang::DeclContext *)function->GetDeclContext()
1781 .GetOpaqueDeclContext();
1782 clang::FunctionDecl *src_function_decl =
1783 llvm::dyn_cast_or_null<clang::FunctionDecl>(src_decl_context);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001784 if (src_function_decl &&
1785 src_function_decl->getTemplateSpecializationInfo()) {
1786 clang::FunctionTemplateDecl *function_template =
1787 src_function_decl->getTemplateSpecializationInfo()->getTemplate();
1788 clang::FunctionTemplateDecl *copied_function_template =
1789 llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(
Sean Callanan68e44232017-09-28 20:20:25 +00001790 CopyDecl(function_template));
Sean Callanan09e91ac2017-05-11 22:08:05 +00001791 if (copied_function_template) {
1792 if (log) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001793 StreamString ss;
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +00001794
Sean Callanan09e91ac2017-05-11 22:08:05 +00001795 function->DumpSymbolContext(&ss);
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +00001796
Raphael Isemann7a6588a2019-11-25 13:27:51 +01001797 LLDB_LOG(log,
1798 " CEDM::FEVD[{0}] Imported decl for function template"
1799 " {1} (description {2}), returned\n{3}",
1800 current_id, copied_function_template->getNameAsString(),
1801 ss.GetData(),
1802 ClangUtil::DumpDecl(copied_function_template));
Sean Callanan09e91ac2017-05-11 22:08:05 +00001803 }
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +00001804
Sean Callanan09e91ac2017-05-11 22:08:05 +00001805 context.AddNamedDecl(copied_function_template);
1806 }
1807 } else if (src_function_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001808 if (clang::FunctionDecl *copied_function_decl =
1809 llvm::dyn_cast_or_null<clang::FunctionDecl>(
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +00001810 CopyDecl(src_function_decl))) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001811 if (log) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001812 StreamString ss;
1813
1814 function->DumpSymbolContext(&ss);
1815
Raphael Isemann7a6588a2019-11-25 13:27:51 +01001816 LLDB_LOG(log,
1817 " CEDM::FEVD[{0}]] Imported decl for function {1} "
1818 "(description {2}), returned\n{3}",
1819 current_id, copied_function_decl->getNameAsString(),
1820 ss.GetData(), ClangUtil::DumpDecl(copied_function_decl));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001821 }
1822
1823 context.AddNamedDecl(copied_function_decl);
1824 return;
1825 } else {
1826 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001827 LLDB_LOGF(log, " Failed to import the function decl for '%s'",
1828 src_function_decl->getName().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001829 }
1830 }
Sean Callanan7736a202016-04-29 18:09:03 +00001831 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001832 }
Sean Callananfc55f5d2010-09-21 00:44:12 +00001833 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001834
Kate Stoneb9c1b512016-09-06 20:57:50 +00001835 if (!function_type) {
1836 if (log)
1837 log->PutCString(" Skipped a function because it has no type");
1838 return;
1839 }
1840
1841 function_clang_type = function_type->GetFullCompilerType();
1842
1843 if (!function_clang_type) {
1844 if (log)
1845 log->PutCString(" Skipped a function because it has no Clang type");
1846 return;
1847 }
1848
1849 fun_address = function->GetAddressRange().GetBaseAddress();
1850
1851 CompilerType copied_function_type = GuardedCopyType(function_clang_type);
1852 if (copied_function_type) {
1853 function_decl = context.AddFunDecl(copied_function_type, extern_c);
1854
1855 if (!function_decl) {
1856 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001857 LLDB_LOGF(
1858 log,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001859 " Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}",
1860 function_type->GetName().GetCString(), function_type->GetID());
1861 }
1862
Sean Callanan3b436d22015-10-23 21:45:02 +00001863 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001864 }
1865 } else {
1866 // We failed to copy the type we found
1867 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001868 LLDB_LOGF(log,
1869 " Failed to import the function type '%s' {0x%8.8" PRIx64
1870 "} into the expression parser AST contenxt",
1871 function_type->GetName().GetCString(),
1872 function_type->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001873 }
1874
1875 return;
1876 }
1877 } else if (symbol) {
1878 fun_address = symbol->GetAddress();
1879 function_decl = context.AddGenericFunDecl();
1880 is_indirect_function = symbol->IsIndirect();
1881 } else {
1882 if (log)
1883 log->PutCString(" AddOneFunction called with no function and no symbol");
1884 return;
1885 }
1886
1887 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1888
1889 lldb::addr_t load_addr =
1890 fun_address.GetCallableLoadAddress(target, is_indirect_function);
1891
1892 ClangExpressionVariable *entity(new ClangExpressionVariable(
1893 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1894 m_parser_vars->m_target_info.byte_order,
1895 m_parser_vars->m_target_info.address_byte_size));
1896 m_found_entities.AddNewlyConstructedVariable(entity);
1897
1898 std::string decl_name(context.m_decl_name.getAsString());
1899 entity->SetName(ConstString(decl_name.c_str()));
1900 entity->SetCompilerType(function_clang_type);
1901 entity->EnableParserVars(GetParserID());
1902
1903 ClangExpressionVariable::ParserVars *parser_vars =
1904 entity->GetParserVars(GetParserID());
1905
1906 if (load_addr != LLDB_INVALID_ADDRESS) {
1907 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1908 parser_vars->m_lldb_value.GetScalar() = load_addr;
1909 } else {
1910 // We have to try finding a file address.
1911
1912 lldb::addr_t file_addr = fun_address.GetFileAddress();
1913
1914 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress);
1915 parser_vars->m_lldb_value.GetScalar() = file_addr;
1916 }
1917
1918 parser_vars->m_named_decl = function_decl;
Konrad Kleine248a1302019-05-23 11:14:47 +00001919 parser_vars->m_llvm_value = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001920
1921 if (log) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001922 StreamString ss;
1923
1924 fun_address.Dump(&ss,
1925 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1926 Address::DumpStyleResolvedDescription);
1927
Raphael Isemann7a6588a2019-11-25 13:27:51 +01001928 LLDB_LOG(log,
1929 " CEDM::FEVD[{0}] Found {1} function {2} (description {3}), "
1930 "returned\n{4}",
1931 current_id, (function ? "specific" : "generic"), decl_name,
1932 ss.GetData(), ClangUtil::DumpDecl(function_decl));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001933 }
1934}
1935
1936void ClangExpressionDeclMap::AddThisType(NameSearchContext &context,
Aleksandr Urakov40624a02019-02-05 09:14:36 +00001937 const TypeFromUser &ut,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001938 unsigned int current_id) {
1939 CompilerType copied_clang_type = GuardedCopyType(ut);
1940
1941 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1942
1943 if (!copied_clang_type) {
1944 if (log)
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001945 LLDB_LOGF(
1946 log,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001947 "ClangExpressionDeclMap::AddThisType - Couldn't import the type");
Sean Callanan3b436d22015-10-23 21:45:02 +00001948
1949 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001950 }
1951
1952 if (copied_clang_type.IsAggregateType() &&
1953 copied_clang_type.GetCompleteType()) {
Raphael Isemannbc7f1df2019-11-29 13:02:41 +01001954 CompilerType void_clang_type =
1955 m_clang_ast_context->GetBasicType(eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001956 CompilerType void_ptr_clang_type = void_clang_type.GetPointerType();
1957
Raphael Isemann0007f9d2019-12-29 20:10:29 +01001958 CompilerType method_type = m_clang_ast_context->CreateFunctionType(
1959 void_clang_type, &void_ptr_clang_type, 1, false, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001960
1961 const bool is_virtual = false;
1962 const bool is_static = false;
1963 const bool is_inline = false;
1964 const bool is_explicit = false;
1965 const bool is_attr_used = true;
1966 const bool is_artificial = false;
1967
Raphael Isemannbc7f1df2019-11-29 13:02:41 +01001968 CXXMethodDecl *method_decl = m_clang_ast_context->AddMethodToCXXRecordType(
1969 copied_clang_type.GetOpaqueQualType(), "$__lldb_expr", nullptr,
1970 method_type, lldb::eAccessPublic, is_virtual, is_static, is_inline,
1971 is_explicit, is_attr_used, is_artificial);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001972
Raphael Isemann7a6588a2019-11-25 13:27:51 +01001973 LLDB_LOG(log,
1974 " CEDM::AddThisType Added function $__lldb_expr "
1975 "(description {0}) for this type\n{1}",
1976 ClangUtil::ToString(copied_clang_type),
1977 ClangUtil::DumpDecl(method_decl));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001978 }
1979
1980 if (!copied_clang_type.IsValid())
1981 return;
1982
1983 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(
1984 QualType::getFromOpaquePtr(copied_clang_type.GetOpaqueQualType()));
1985
1986 if (!type_source_info)
1987 return;
1988
1989 // Construct a typedef type because if "*this" is a templated type we can't
1990 // just return ClassTemplateSpecializationDecls in response to name queries.
1991 // Using a typedef makes this much more robust.
1992
1993 TypedefDecl *typedef_decl = TypedefDecl::Create(
1994 *m_ast_context, m_ast_context->getTranslationUnitDecl(), SourceLocation(),
1995 SourceLocation(), context.m_decl_name.getAsIdentifierInfo(),
1996 type_source_info);
1997
1998 if (!typedef_decl)
1999 return;
2000
2001 context.AddNamedDecl(typedef_decl);
2002
2003 return;
Sean Callananfa4fab72013-02-01 06:55:48 +00002004}
2005
Kate Stoneb9c1b512016-09-06 20:57:50 +00002006void ClangExpressionDeclMap::AddOneType(NameSearchContext &context,
Aleksandr Urakov40624a02019-02-05 09:14:36 +00002007 const TypeFromUser &ut,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002008 unsigned int current_id) {
2009 CompilerType copied_clang_type = GuardedCopyType(ut);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002010
Kate Stoneb9c1b512016-09-06 20:57:50 +00002011 if (!copied_clang_type) {
2012 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfa4fab72013-02-01 06:55:48 +00002013
Kate Stoneb9c1b512016-09-06 20:57:50 +00002014 if (log)
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00002015 LLDB_LOGF(
2016 log, "ClangExpressionDeclMap::AddOneType - Couldn't import the type");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002017
Kate Stoneb9c1b512016-09-06 20:57:50 +00002018 return;
2019 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002020
Kate Stoneb9c1b512016-09-06 20:57:50 +00002021 context.AddTypeDecl(copied_clang_type);
Sean Callanan5666b672010-08-04 01:02:13 +00002022}