blob: 58094e0252685922bde6c562feae3b23a369dcc6 [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- ClangExpressionDeclMap.cpp ----------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
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"
Alex Langford8be30212020-01-29 11:59:28 -080014#include "ClangUtil.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000015
Alex Langford8be30212020-01-29 11:59:28 -080016#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/Address.h"
18#include "lldb/Core/Module.h"
Greg Clayton9191db42013-10-21 18:40:51 +000019#include "lldb/Core/ModuleSpec.h"
Sean Callanane4ec90e2010-12-16 03:17:46 +000020#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan9b3569b2011-12-10 03:12:34 +000021#include "lldb/Core/ValueObjectVariable.h"
Sean Callanan96d27302013-04-11 00:09:05 +000022#include "lldb/Expression/Materializer.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
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +0100112 if (!TypeSystemClang::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() {
Adrian Prantle9331a52020-01-08 14:18:47 -0800129 if (m_parser_vars && m_parser_vars->m_persistent_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
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +0100177TypeFromUser ClangExpressionDeclMap::DeportType(TypeSystemClang &target,
178 TypeSystemClang &source,
Sean Callanan68e44232017-09-28 20:20:25 +0000179 TypeFromParser parser_type) {
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +0100180 assert(&target == TypeSystemClang::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
Raphael Isemann1ccc7022020-01-31 13:00:32 +0100184 return TypeFromUser(m_ast_importer_sp->DeportType(target, parser_type));
Sean Callanan68e44232017-09-28 20:20:25 +0000185}
186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187bool ClangExpressionDeclMap::AddPersistentVariable(const NamedDecl *decl,
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000188 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 TypeFromParser parser_type,
190 bool is_result,
191 bool is_lvalue) {
192 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000193
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +0100194 TypeSystemClang *ast =
195 llvm::dyn_cast_or_null<TypeSystemClang>(parser_type.GetTypeSystem());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 if (ast == nullptr)
197 return false;
Greg Claytonf73034f2015-09-08 18:15:05 +0000198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 if (m_parser_vars->m_materializer && is_result) {
Zachary Turner97206d52017-05-12 04:51:55 +0000200 Status err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000201
Sean Callanan933693b2012-02-10 01:22:05 +0000202 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
203 Target *target = exe_ctx.GetTargetPtr();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 if (target == nullptr)
205 return false;
Greg Claytonc14ee322011-09-22 04:58:26 +0000206
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +0100207 auto *clang_ast_context = TypeSystemClang::GetScratch(*target);
Alex Langford30318182019-11-14 13:41:52 -0800208 if (!clang_ast_context)
209 return false;
210
211 TypeFromUser user_type = DeportType(*clang_ast_context, *ast, parser_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000212
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(
214 user_type, is_lvalue, m_keep_result_in_memory, m_result_delegate, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 ClangExpressionVariable *var = new ClangExpressionVariable(
217 exe_ctx.GetBestExecutionContextScope(), name, user_type,
218 m_parser_vars->m_target_info.byte_order,
219 m_parser_vars->m_target_info.address_byte_size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 m_found_entities.AddNewlyConstructedVariable(var);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000222
Sean Callananbc8ac342015-09-04 20:49:51 +0000223 var->EnableParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 ClangExpressionVariable::ParserVars *parser_vars =
226 var->GetParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000227
Sean Callanan3c495c12013-01-15 23:29:36 +0000228 parser_vars->m_named_decl = decl;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 var->EnableJITVars(GetParserID());
231
232 ClangExpressionVariable::JITVars *jit_vars = var->GetJITVars(GetParserID());
233
234 jit_vars->m_offset = offset;
235
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000236 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 }
238
239 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
240 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
241 Target *target = exe_ctx.GetTargetPtr();
Konrad Kleine248a1302019-05-23 11:14:47 +0000242 if (target == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 return false;
244
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +0100245 TypeSystemClang *context = TypeSystemClang::GetScratch(*target);
Alex Langford30318182019-11-14 13:41:52 -0800246 if (!context)
247 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248
Sean Callanan68e44232017-09-28 20:20:25 +0000249 TypeFromUser user_type = DeportType(*context, *ast, parser_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250
251 if (!user_type.GetOpaqueQualType()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000252 LLDB_LOGF(log, "Persistent variable's type wasn't copied successfully");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 return false;
254 }
255
256 if (!m_parser_vars->m_target_info.IsValid())
257 return false;
258
Adrian Prantle9331a52020-01-08 14:18:47 -0800259 if (!m_parser_vars->m_persistent_vars)
260 return false;
261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 ClangExpressionVariable *var = llvm::cast<ClangExpressionVariable>(
263 m_parser_vars->m_persistent_vars
264 ->CreatePersistentVariable(
265 exe_ctx.GetBestExecutionContextScope(), name, user_type,
266 m_parser_vars->m_target_info.byte_order,
267 m_parser_vars->m_target_info.address_byte_size)
268 .get());
269
270 if (!var)
271 return false;
272
273 var->m_frozen_sp->SetHasCompleteType();
274
275 if (is_result)
276 var->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry;
277 else
278 var->m_flags |=
279 ClangExpressionVariable::EVKeepInTarget; // explicitly-declared
280 // persistent variables should
281 // persist
282
283 if (is_lvalue) {
284 var->m_flags |= ClangExpressionVariable::EVIsProgramReference;
285 } else {
286 var->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
287 var->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
288 }
289
290 if (m_keep_result_in_memory) {
291 var->m_flags |= ClangExpressionVariable::EVKeepInTarget;
292 }
293
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000294 LLDB_LOGF(log, "Created persistent variable with flags 0x%hx", var->m_flags);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295
296 var->EnableParserVars(GetParserID());
297
298 ClangExpressionVariable::ParserVars *parser_vars =
299 var->GetParserVars(GetParserID());
300
301 parser_vars->m_named_decl = decl;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302
303 return true;
Sean Callanan2235f322010-08-11 03:57:18 +0000304}
305
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306bool ClangExpressionDeclMap::AddValueToStruct(const NamedDecl *decl,
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000307 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308 llvm::Value *value, size_t size,
309 lldb::offset_t alignment) {
310 assert(m_struct_vars.get());
311 assert(m_parser_vars.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000312
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313 bool is_persistent_variable = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000314
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000316
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317 m_struct_vars->m_struct_laid_out = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000318
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319 if (ClangExpressionVariable::FindVariableInList(m_struct_members, decl,
320 GetParserID()))
321 return true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000322
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 ClangExpressionVariable *var(ClangExpressionVariable::FindVariableInList(
324 m_found_entities, decl, GetParserID()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000325
Adrian Prantle9331a52020-01-08 14:18:47 -0800326 if (!var && m_parser_vars->m_persistent_vars) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327 var = ClangExpressionVariable::FindVariableInList(
328 *m_parser_vars->m_persistent_vars, decl, GetParserID());
329 is_persistent_variable = true;
330 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000331
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 if (!var)
333 return false;
334
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000335 LLDB_LOGF(log, "Adding value for (NamedDecl*)%p [%s - %s] to the structure",
336 static_cast<const void *>(decl), name.GetCString(),
337 var->GetName().GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338
339 // We know entity->m_parser_vars is valid because we used a parser variable
340 // to find it
341
342 ClangExpressionVariable::ParserVars *parser_vars =
343 llvm::cast<ClangExpressionVariable>(var)->GetParserVars(GetParserID());
344
345 parser_vars->m_llvm_value = value;
346
347 if (ClangExpressionVariable::JITVars *jit_vars =
348 llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID())) {
349 // We already laid this out; do not touch
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000350
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000351 LLDB_LOGF(log, "Already placed at 0x%llx",
352 (unsigned long long)jit_vars->m_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000354
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 llvm::cast<ClangExpressionVariable>(var)->EnableJITVars(GetParserID());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000356
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357 ClangExpressionVariable::JITVars *jit_vars =
358 llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID());
Sean Callanan3c495c12013-01-15 23:29:36 +0000359
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 jit_vars->m_alignment = alignment;
361 jit_vars->m_size = size;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 m_struct_members.AddVariable(var->shared_from_this());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000364
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 if (m_parser_vars->m_materializer) {
366 uint32_t offset = 0;
367
Zachary Turner97206d52017-05-12 04:51:55 +0000368 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369
370 if (is_persistent_variable) {
371 ExpressionVariableSP var_sp(var->shared_from_this());
372 offset = m_parser_vars->m_materializer->AddPersistentVariable(
373 var_sp, nullptr, err);
374 } else {
375 if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym)
376 offset = m_parser_vars->m_materializer->AddSymbol(*sym, err);
377 else if (const RegisterInfo *reg_info = var->GetRegisterInfo())
378 offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err);
379 else if (parser_vars->m_lldb_var)
380 offset = m_parser_vars->m_materializer->AddVariable(
381 parser_vars->m_lldb_var, err);
Sean Callanan1582ee62013-04-18 22:06:33 +0000382 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000383
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 if (!err.Success())
385 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000386
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000387 LLDB_LOGF(log, "Placed at 0x%llx", (unsigned long long)offset);
Sean Callanan3c495c12013-01-15 23:29:36 +0000388
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 jit_vars->m_offset =
390 offset; // TODO DoStructLayout() should not change this.
391 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000392
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393 return true;
394}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000395
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396bool ClangExpressionDeclMap::DoStructLayout() {
397 assert(m_struct_vars.get());
Sean Callanan3dd6a422013-04-11 21:16:36 +0000398
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399 if (m_struct_vars->m_struct_laid_out)
400 return true;
Sean Callanandf667652013-04-11 02:05:11 +0000401
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402 if (!m_parser_vars->m_materializer)
403 return false;
404
405 m_struct_vars->m_struct_alignment =
406 m_parser_vars->m_materializer->GetStructAlignment();
407 m_struct_vars->m_struct_size =
408 m_parser_vars->m_materializer->GetStructByteSize();
409 m_struct_vars->m_struct_laid_out = true;
410 return true;
411}
412
413bool ClangExpressionDeclMap::GetStructInfo(uint32_t &num_elements, size_t &size,
414 lldb::offset_t &alignment) {
415 assert(m_struct_vars.get());
416
417 if (!m_struct_vars->m_struct_laid_out)
418 return false;
419
420 num_elements = m_struct_members.GetSize();
421 size = m_struct_vars->m_struct_size;
422 alignment = m_struct_vars->m_struct_alignment;
423
424 return true;
425}
426
427bool ClangExpressionDeclMap::GetStructElement(const NamedDecl *&decl,
428 llvm::Value *&value,
429 lldb::offset_t &offset,
430 ConstString &name,
431 uint32_t index) {
432 assert(m_struct_vars.get());
433
434 if (!m_struct_vars->m_struct_laid_out)
435 return false;
436
437 if (index >= m_struct_members.GetSize())
438 return false;
439
440 ExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index));
441
442 if (!member_sp)
443 return false;
444
445 ClangExpressionVariable::ParserVars *parser_vars =
446 llvm::cast<ClangExpressionVariable>(member_sp.get())
447 ->GetParserVars(GetParserID());
448 ClangExpressionVariable::JITVars *jit_vars =
449 llvm::cast<ClangExpressionVariable>(member_sp.get())
450 ->GetJITVars(GetParserID());
451
452 if (!parser_vars || !jit_vars || !member_sp->GetValueObject())
453 return false;
454
455 decl = parser_vars->m_named_decl;
456 value = parser_vars->m_llvm_value;
457 offset = jit_vars->m_offset;
458 name = member_sp->GetName();
459
460 return true;
461}
462
463bool ClangExpressionDeclMap::GetFunctionInfo(const NamedDecl *decl,
464 uint64_t &ptr) {
465 ClangExpressionVariable *entity(ClangExpressionVariable::FindVariableInList(
466 m_found_entities, decl, GetParserID()));
467
468 if (!entity)
469 return false;
470
Adrian Prantl05097242018-04-30 16:49:04 +0000471 // We know m_parser_vars is valid since we searched for the variable by its
472 // NamedDecl
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473
474 ClangExpressionVariable::ParserVars *parser_vars =
475 entity->GetParserVars(GetParserID());
476
477 ptr = parser_vars->m_lldb_value.GetScalar().ULongLong();
478
479 return true;
480}
481
482addr_t ClangExpressionDeclMap::GetSymbolAddress(Target &target,
483 Process *process,
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000484 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485 lldb::SymbolType symbol_type,
486 lldb_private::Module *module) {
487 SymbolContextList sc_list;
488
489 if (module)
490 module->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
491 else
492 target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list);
493
494 const uint32_t num_matches = sc_list.GetSize();
495 addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
496
497 for (uint32_t i = 0;
498 i < num_matches &&
499 (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS);
500 i++) {
501 SymbolContext sym_ctx;
502 sc_list.GetContextAtIndex(i, sym_ctx);
503
504 const Address sym_address = sym_ctx.symbol->GetAddress();
505
506 if (!sym_address.IsValid())
507 continue;
508
509 switch (sym_ctx.symbol->GetType()) {
510 case eSymbolTypeCode:
511 case eSymbolTypeTrampoline:
512 symbol_load_addr = sym_address.GetCallableLoadAddress(&target);
513 break;
514
515 case eSymbolTypeResolver:
516 symbol_load_addr = sym_address.GetCallableLoadAddress(&target, true);
517 break;
518
519 case eSymbolTypeReExported: {
520 ConstString reexport_name = sym_ctx.symbol->GetReExportedSymbolName();
521 if (reexport_name) {
522 ModuleSP reexport_module_sp;
523 ModuleSpec reexport_module_spec;
524 reexport_module_spec.GetPlatformFileSpec() =
525 sym_ctx.symbol->GetReExportedSymbolSharedLibrary();
526 if (reexport_module_spec.GetPlatformFileSpec()) {
527 reexport_module_sp =
528 target.GetImages().FindFirstModule(reexport_module_spec);
529 if (!reexport_module_sp) {
530 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
531 reexport_module_sp =
532 target.GetImages().FindFirstModule(reexport_module_spec);
533 }
Sean Callanandf667652013-04-11 02:05:11 +0000534 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535 symbol_load_addr = GetSymbolAddress(
536 target, process, sym_ctx.symbol->GetReExportedSymbolName(),
537 symbol_type, reexport_module_sp.get());
538 }
539 } break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000540
Kate Stoneb9c1b512016-09-06 20:57:50 +0000541 case eSymbolTypeData:
542 case eSymbolTypeRuntime:
543 case eSymbolTypeVariable:
544 case eSymbolTypeLocal:
545 case eSymbolTypeParam:
546 case eSymbolTypeInvalid:
547 case eSymbolTypeAbsolute:
548 case eSymbolTypeException:
549 case eSymbolTypeSourceFile:
550 case eSymbolTypeHeaderFile:
551 case eSymbolTypeObjectFile:
552 case eSymbolTypeCommonBlock:
553 case eSymbolTypeBlock:
554 case eSymbolTypeVariableType:
555 case eSymbolTypeLineEntry:
556 case eSymbolTypeLineHeader:
557 case eSymbolTypeScopeBegin:
558 case eSymbolTypeScopeEnd:
559 case eSymbolTypeAdditional:
560 case eSymbolTypeCompiler:
561 case eSymbolTypeInstrumentation:
562 case eSymbolTypeUndefined:
563 case eSymbolTypeObjCClass:
564 case eSymbolTypeObjCMetaClass:
565 case eSymbolTypeObjCIVar:
566 symbol_load_addr = sym_address.GetLoadAddress(&target);
567 break;
Sean Callanandf667652013-04-11 02:05:11 +0000568 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000570
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process) {
Alex Langforde823bbe2019-06-10 20:53:23 +0000572 ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process);
Sean Callanan549c9f72010-07-13 21:41:46 +0000573
Kate Stoneb9c1b512016-09-06 20:57:50 +0000574 if (runtime) {
575 symbol_load_addr = runtime->LookupRuntimeSymbol(name);
Greg Clayton084db102011-06-23 04:25:29 +0000576 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000578
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 return symbol_load_addr;
Sean Callananc3a16002011-01-17 23:42:46 +0000580}
581
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000582addr_t ClangExpressionDeclMap::GetSymbolAddress(ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583 lldb::SymbolType symbol_type) {
584 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000585
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586 if (!m_parser_vars->m_exe_ctx.GetTargetPtr())
587 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000588
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589 return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(),
590 m_parser_vars->m_exe_ctx.GetProcessPtr(), name,
591 symbol_type);
Sean Callanand9ca42a2011-05-08 02:21:26 +0000592}
593
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594lldb::VariableSP ClangExpressionDeclMap::FindGlobalVariable(
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000595 Target &target, ModuleSP &module, ConstString name,
Raphael Isemannb6c29d92019-12-23 12:18:49 +0100596 CompilerDeclContext *namespace_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597 VariableList vars;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000598
Kate Stoneb9c1b512016-09-06 20:57:50 +0000599 if (module && namespace_decl)
Pavel Labath34cda142018-05-31 09:46:26 +0000600 module->FindGlobalVariables(name, namespace_decl, -1, vars);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601 else
Pavel Labath34cda142018-05-31 09:46:26 +0000602 target.GetImages().FindGlobalVariables(name, -1, vars);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000603
Raphael Isemannb6c29d92019-12-23 12:18:49 +0100604 if (vars.GetSize() == 0)
605 return VariableSP();
606 return vars.GetVariableAtIndex(0);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000607}
608
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +0100609TypeSystemClang *ClangExpressionDeclMap::GetTypeSystemClang() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
611 if (frame == nullptr)
612 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +0000613
Kate Stoneb9c1b512016-09-06 20:57:50 +0000614 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
615 lldb::eSymbolContextBlock);
616 if (sym_ctx.block == nullptr)
617 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +0000618
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619 CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext();
620 if (!frame_decl_context)
621 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +0000622
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +0100623 return llvm::dyn_cast_or_null<TypeSystemClang>(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000624 frame_decl_context.GetTypeSystem());
Siva Chandra03ff5c82016-02-05 19:10:04 +0000625}
626
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627// Interface for ClangASTSource
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000628
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629void ClangExpressionDeclMap::FindExternalVisibleDecls(
630 NameSearchContext &context) {
631 assert(m_ast_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000632
Kate Stoneb9c1b512016-09-06 20:57:50 +0000633 const ConstString name(context.m_decl_name.getAsString().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000634
Kate Stoneb9c1b512016-09-06 20:57:50 +0000635 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000636
Kate Stoneb9c1b512016-09-06 20:57:50 +0000637 if (GetImportInProgress()) {
638 if (log && log->GetVerbose())
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000639 LLDB_LOGF(log, "Ignoring a query during an import");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 return;
641 }
642
643 static unsigned int invocation_id = 0;
644 unsigned int current_id = invocation_id++;
645
646 if (log) {
647 if (!context.m_decl_context)
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000648 LLDB_LOGF(log,
649 "ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for "
650 "'%s' in a NULL DeclContext",
651 current_id, name.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000652 else if (const NamedDecl *context_named_decl =
653 dyn_cast<NamedDecl>(context.m_decl_context))
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000654 LLDB_LOGF(log,
655 "ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for "
656 "'%s' in '%s'",
657 current_id, name.GetCString(),
658 context_named_decl->getNameAsString().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000659 else
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000660 LLDB_LOGF(log,
661 "ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for "
662 "'%s' in a '%s'",
663 current_id, name.GetCString(),
664 context.m_decl_context->getDeclKindName());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000665 }
666
667 if (const NamespaceDecl *namespace_context =
668 dyn_cast<NamespaceDecl>(context.m_decl_context)) {
669 if (namespace_context->getName().str() ==
670 std::string(g_lldb_local_vars_namespace_cstr)) {
Raphael Isemann73951a12019-12-23 10:55:21 +0100671 CompilerDeclContext compiler_decl_ctx =
672 m_clang_ast_context->CreateDeclContext(
673 const_cast<clang::DeclContext *>(context.m_decl_context));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000674 FindExternalVisibleDecls(context, lldb::ModuleSP(), compiler_decl_ctx,
675 current_id);
676 return;
Sean Callanan6abfabf2010-11-19 20:20:02 +0000677 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000678
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679 ClangASTImporter::NamespaceMapSP namespace_map =
Raphael Isemann1ccc7022020-01-31 13:00:32 +0100680 m_ast_importer_sp->GetNamespaceMap(namespace_context);
Sean Callanan68e44232017-09-28 20:20:25 +0000681
682 if (!namespace_map)
683 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000684
685 if (log && log->GetVerbose())
686 log->Printf(" CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)",
687 current_id, static_cast<void *>(namespace_map.get()),
688 (int)namespace_map->size());
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +0000689
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(),
691 e = namespace_map->end();
692 i != e; ++i) {
693 if (log)
694 log->Printf(" CEDM::FEVD[%u] Searching namespace %s in module %s",
695 current_id, i->second.GetName().AsCString(),
696 i->first->GetFileSpec().GetFilename().GetCString());
697
698 FindExternalVisibleDecls(context, i->first, i->second, current_id);
699 }
700 } else if (isa<TranslationUnitDecl>(context.m_decl_context)) {
701 CompilerDeclContext namespace_decl;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000702
Sean Callanan503aa522011-10-12 00:12:34 +0000703 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000704 log->Printf(" CEDM::FEVD[%u] Searching the root namespace", current_id);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000705
Kate Stoneb9c1b512016-09-06 20:57:50 +0000706 FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl,
707 current_id);
708 }
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +0000709
Sean Callananc02a1c02017-04-24 23:14:04 +0000710 ClangASTSource::FindExternalVisibleDecls(context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000711}
712
Raphael Isemannc34478f2019-11-20 13:41:44 +0100713void ClangExpressionDeclMap::MaybeRegisterFunctionBody(
714 FunctionDecl *copied_function_decl) {
715 if (copied_function_decl->getBody() && m_parser_vars->m_code_gen) {
716 clang::DeclGroupRef decl_group_ref(copied_function_decl);
717 m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref);
718 }
719}
720
Raphael Isemannd8a31942019-12-17 16:12:07 +0100721clang::NamedDecl *ClangExpressionDeclMap::GetPersistentDecl(ConstString name) {
Raphael Isemann4aee81c2019-12-17 13:38:07 +0100722 if (!m_parser_vars)
Raphael Isemannd8a31942019-12-17 16:12:07 +0100723 return nullptr;
Raphael Isemannc34478f2019-11-20 13:41:44 +0100724 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
725 if (!target)
Raphael Isemannd8a31942019-12-17 16:12:07 +0100726 return nullptr;
Raphael Isemannc34478f2019-11-20 13:41:44 +0100727
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +0100728 TypeSystemClang::GetScratch(*target);
Raphael Isemannc34478f2019-11-20 13:41:44 +0100729
Adrian Prantle9331a52020-01-08 14:18:47 -0800730 if (!m_parser_vars->m_persistent_vars)
731 return nullptr;
Raphael Isemannd8a31942019-12-17 16:12:07 +0100732 return m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
733}
734
735void ClangExpressionDeclMap::SearchPersistenDecls(NameSearchContext &context,
736 const ConstString name,
737 unsigned int current_id) {
738 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
739
740 NamedDecl *persistent_decl = GetPersistentDecl(name);
Raphael Isemannc34478f2019-11-20 13:41:44 +0100741
742 if (!persistent_decl)
743 return;
744
745 Decl *parser_persistent_decl = CopyDecl(persistent_decl);
746
747 if (!parser_persistent_decl)
748 return;
749
750 NamedDecl *parser_named_decl = dyn_cast<NamedDecl>(parser_persistent_decl);
751
752 if (!parser_named_decl)
753 return;
754
755 if (clang::FunctionDecl *parser_function_decl =
756 llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl)) {
757 MaybeRegisterFunctionBody(parser_function_decl);
758 }
759
760 LLDB_LOGF(log, " CEDM::FEVD[%u] Found persistent decl %s", current_id,
761 name.GetCString());
762
763 context.AddNamedDecl(parser_named_decl);
764}
Raphael Isemanne7cc8332019-11-20 14:35:18 +0100765
766void ClangExpressionDeclMap::LookUpLldbClass(NameSearchContext &context,
767 unsigned int current_id) {
768 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
769
770 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
771 SymbolContext sym_ctx;
772 if (frame != nullptr)
773 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
774 lldb::eSymbolContextBlock);
775
776 if (m_ctx_obj) {
777 Status status;
778 lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status);
779 if (!ctx_obj_ptr || status.Fail())
780 return;
781
782 AddThisType(context, TypeFromUser(m_ctx_obj->GetCompilerType()),
783 current_id);
784
785 m_struct_vars->m_object_pointer_type =
786 TypeFromUser(ctx_obj_ptr->GetCompilerType());
787
788 return;
789 }
790
791 // Clang is looking for the type of "this"
792
793 if (frame == nullptr)
794 return;
795
796 // Find the block that defines the function represented by "sym_ctx"
797 Block *function_block = sym_ctx.GetFunctionBlock();
798
799 if (!function_block)
800 return;
801
802 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
803
804 if (!function_decl_ctx)
805 return;
806
807 clang::CXXMethodDecl *method_decl =
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +0100808 TypeSystemClang::DeclContextGetAsCXXMethodDecl(function_decl_ctx);
Raphael Isemanne7cc8332019-11-20 14:35:18 +0100809
810 if (method_decl) {
811 clang::CXXRecordDecl *class_decl = method_decl->getParent();
812
813 QualType class_qual_type(class_decl->getTypeForDecl(), 0);
814
Raphael Isemannb036f552019-12-25 23:43:52 +0100815 TypeFromUser class_user_type(class_qual_type.getAsOpaquePtr(),
816 function_decl_ctx.GetTypeSystem());
Raphael Isemanne7cc8332019-11-20 14:35:18 +0100817
Raphael Isemann7a6588a2019-11-25 13:27:51 +0100818 LLDB_LOG(log, " CEDM::FEVD[{0}] Adding type for $__lldb_class: {1}",
819 current_id, class_qual_type.getAsString());
Raphael Isemanne7cc8332019-11-20 14:35:18 +0100820
821 AddThisType(context, class_user_type, current_id);
822
823 if (method_decl->isInstance()) {
824 // self is a pointer to the object
825
826 QualType class_pointer_type =
827 method_decl->getASTContext().getPointerType(class_qual_type);
828
Raphael Isemannb036f552019-12-25 23:43:52 +0100829 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
830 function_decl_ctx.GetTypeSystem());
Raphael Isemanne7cc8332019-11-20 14:35:18 +0100831
832 m_struct_vars->m_object_pointer_type = self_user_type;
833 }
834 return;
835 }
836
837 // This branch will get hit if we are executing code in the context of
838 // a function that claims to have an object pointer (through
839 // DW_AT_object_pointer?) but is not formally a method of the class.
840 // In that case, just look up the "this" variable in the current scope
841 // and use its type.
842 // FIXME: This code is formally correct, but clang doesn't currently
843 // emit DW_AT_object_pointer
844 // for C++ so it hasn't actually been tested.
845
846 VariableList *vars = frame->GetVariableList(false);
847
848 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
849
850 if (this_var && this_var->IsInScope(frame) &&
851 this_var->LocationIsValidForFrame(frame)) {
852 Type *this_type = this_var->GetType();
853
854 if (!this_type)
855 return;
856
857 TypeFromUser pointee_type =
858 this_type->GetForwardCompilerType().GetPointeeType();
859
Raphael Isemann7a6588a2019-11-25 13:27:51 +0100860 LLDB_LOG(log, " FEVD[{0}] Adding type for $__lldb_class: {1}", current_id,
861 ClangUtil::GetQualType(pointee_type).getAsString());
Raphael Isemanne7cc8332019-11-20 14:35:18 +0100862
Raphael Isemann7a6588a2019-11-25 13:27:51 +0100863 AddThisType(context, pointee_type, current_id);
864 TypeFromUser this_user_type(this_type->GetFullCompilerType());
865 m_struct_vars->m_object_pointer_type = this_user_type;
Raphael Isemanne7cc8332019-11-20 14:35:18 +0100866 }
867}
868
Raphael Isemann51ad0252019-11-20 15:58:07 +0100869void ClangExpressionDeclMap::LookUpLldbObjCClass(NameSearchContext &context,
870 unsigned int current_id) {
871 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
872
873 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
874
875 if (m_ctx_obj) {
876 Status status;
877 lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status);
878 if (!ctx_obj_ptr || status.Fail())
879 return;
880
881 AddOneType(context, TypeFromUser(m_ctx_obj->GetCompilerType()), current_id);
882
883 m_struct_vars->m_object_pointer_type =
884 TypeFromUser(ctx_obj_ptr->GetCompilerType());
885
886 return;
887 }
888
889 // Clang is looking for the type of "*self"
890
891 if (!frame)
892 return;
893
894 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
895 lldb::eSymbolContextBlock);
896
897 // Find the block that defines the function represented by "sym_ctx"
898 Block *function_block = sym_ctx.GetFunctionBlock();
899
900 if (!function_block)
901 return;
902
903 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
904
905 if (!function_decl_ctx)
906 return;
907
908 clang::ObjCMethodDecl *method_decl =
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +0100909 TypeSystemClang::DeclContextGetAsObjCMethodDecl(function_decl_ctx);
Raphael Isemann51ad0252019-11-20 15:58:07 +0100910
911 if (method_decl) {
912 ObjCInterfaceDecl *self_interface = method_decl->getClassInterface();
913
914 if (!self_interface)
915 return;
916
917 const clang::Type *interface_type = self_interface->getTypeForDecl();
918
919 if (!interface_type)
920 return; // This is unlikely, but we have seen crashes where this
921 // occurred
922
Raphael Isemannb036f552019-12-25 23:43:52 +0100923 TypeFromUser class_user_type(QualType(interface_type, 0).getAsOpaquePtr(),
924 function_decl_ctx.GetTypeSystem());
Raphael Isemann51ad0252019-11-20 15:58:07 +0100925
Raphael Isemann7a6588a2019-11-25 13:27:51 +0100926 LLDB_LOG(log, " FEVD[{0}] Adding type for $__lldb_objc_class: {1}",
927 current_id, ClangUtil::ToString(interface_type));
Raphael Isemann51ad0252019-11-20 15:58:07 +0100928
929 AddOneType(context, class_user_type, current_id);
930
931 if (method_decl->isInstanceMethod()) {
932 // self is a pointer to the object
933
934 QualType class_pointer_type =
935 method_decl->getASTContext().getObjCObjectPointerType(
936 QualType(interface_type, 0));
937
Raphael Isemannb036f552019-12-25 23:43:52 +0100938 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
939 function_decl_ctx.GetTypeSystem());
Raphael Isemann51ad0252019-11-20 15:58:07 +0100940
941 m_struct_vars->m_object_pointer_type = self_user_type;
942 } else {
943 // self is a Class pointer
944 QualType class_type = method_decl->getASTContext().getObjCClassType();
945
Raphael Isemannb036f552019-12-25 23:43:52 +0100946 TypeFromUser self_user_type(class_type.getAsOpaquePtr(),
947 function_decl_ctx.GetTypeSystem());
Raphael Isemann51ad0252019-11-20 15:58:07 +0100948
949 m_struct_vars->m_object_pointer_type = self_user_type;
950 }
951
952 return;
953 }
954 // This branch will get hit if we are executing code in the context of
955 // a function that claims to have an object pointer (through
956 // DW_AT_object_pointer?) but is not formally a method of the class.
957 // In that case, just look up the "self" variable in the current scope
958 // and use its type.
959
960 VariableList *vars = frame->GetVariableList(false);
961
962 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
963
Raphael Isemann1e0d3952019-11-23 21:09:41 +0100964 if (!self_var)
965 return;
966 if (!self_var->IsInScope(frame))
967 return;
968 if (!self_var->LocationIsValidForFrame(frame))
969 return;
Raphael Isemann51ad0252019-11-20 15:58:07 +0100970
Raphael Isemann1e0d3952019-11-23 21:09:41 +0100971 Type *self_type = self_var->GetType();
Raphael Isemann51ad0252019-11-20 15:58:07 +0100972
Raphael Isemann1e0d3952019-11-23 21:09:41 +0100973 if (!self_type)
974 return;
Raphael Isemann51ad0252019-11-20 15:58:07 +0100975
Raphael Isemann1e0d3952019-11-23 21:09:41 +0100976 CompilerType self_clang_type = self_type->GetFullCompilerType();
Raphael Isemann51ad0252019-11-20 15:58:07 +0100977
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +0100978 if (TypeSystemClang::IsObjCClassType(self_clang_type)) {
Raphael Isemann1e0d3952019-11-23 21:09:41 +0100979 return;
Raphael Isemann51ad0252019-11-20 15:58:07 +0100980 }
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +0100981 if (!TypeSystemClang::IsObjCObjectPointerType(self_clang_type))
Raphael Isemann1e0d3952019-11-23 21:09:41 +0100982 return;
983 self_clang_type = self_clang_type.GetPointeeType();
984
985 if (!self_clang_type)
986 return;
987
Raphael Isemann7a6588a2019-11-25 13:27:51 +0100988 LLDB_LOG(log, " FEVD[{0}] Adding type for $__lldb_objc_class: {1}",
989 current_id, ClangUtil::ToString(self_type->GetFullCompilerType()));
Raphael Isemann1e0d3952019-11-23 21:09:41 +0100990
991 TypeFromUser class_user_type(self_clang_type);
992
993 AddOneType(context, class_user_type, current_id);
994
995 TypeFromUser self_user_type(self_type->GetFullCompilerType());
996
997 m_struct_vars->m_object_pointer_type = self_user_type;
Raphael Isemann51ad0252019-11-20 15:58:07 +0100998}
999
Raphael Isemann337151f2019-11-21 10:45:04 +01001000void ClangExpressionDeclMap::LookupLocalVarNamespace(
Raphael Isemann1e0d3952019-11-23 21:09:41 +01001001 SymbolContext &sym_ctx, NameSearchContext &name_context) {
1002 if (sym_ctx.block == nullptr)
1003 return;
Raphael Isemann337151f2019-11-21 10:45:04 +01001004
Raphael Isemann1e0d3952019-11-23 21:09:41 +01001005 CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext();
1006 if (!frame_decl_context)
1007 return;
Raphael Isemann337151f2019-11-21 10:45:04 +01001008
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +01001009 TypeSystemClang *frame_ast = llvm::dyn_cast_or_null<TypeSystemClang>(
Raphael Isemann1e0d3952019-11-23 21:09:41 +01001010 frame_decl_context.GetTypeSystem());
1011 if (!frame_ast)
1012 return;
1013
Raphael Isemannbc7f1df2019-11-29 13:02:41 +01001014 clang::NamespaceDecl *namespace_decl =
1015 m_clang_ast_context->GetUniqueNamespaceDeclaration(
1016 g_lldb_local_vars_namespace_cstr, nullptr);
Raphael Isemann1e0d3952019-11-23 21:09:41 +01001017 if (!namespace_decl)
1018 return;
1019
1020 name_context.AddNamedDecl(namespace_decl);
1021 clang::DeclContext *ctxt = clang::Decl::castToDeclContext(namespace_decl);
1022 ctxt->setHasExternalVisibleStorage(true);
1023 name_context.m_found.local_vars_nsp = true;
Raphael Isemann337151f2019-11-21 10:45:04 +01001024}
1025
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001026void ClangExpressionDeclMap::LookupInModulesDeclVendor(
1027 NameSearchContext &context, ConstString name, unsigned current_id) {
1028 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1029
Raphael Isemann4aee81c2019-12-17 13:38:07 +01001030 if (!m_target)
1031 return;
1032
Raphael Isemann46883f42019-11-23 20:06:50 +01001033 auto *modules_decl_vendor = m_target->GetClangModulesDeclVendor();
1034 if (!modules_decl_vendor)
1035 return;
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001036
Raphael Isemann46883f42019-11-23 20:06:50 +01001037 bool append = false;
1038 uint32_t max_matches = 1;
1039 std::vector<clang::NamedDecl *> decls;
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001040
Raphael Isemann46883f42019-11-23 20:06:50 +01001041 if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls))
1042 return;
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001043
Raphael Isemann46883f42019-11-23 20:06:50 +01001044 assert(!decls.empty() && "FindDecls returned true but no decls?");
1045 clang::NamedDecl *const decl_from_modules = decls[0];
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001046
Raphael Isemann46883f42019-11-23 20:06:50 +01001047 LLDB_LOG(log,
1048 " CAS::FEVD[{0}] Matching decl found for "
1049 "\"{1}\" in the modules",
1050 current_id, name);
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001051
Raphael Isemann46883f42019-11-23 20:06:50 +01001052 clang::Decl *copied_decl = CopyDecl(decl_from_modules);
1053 if (!copied_decl) {
1054 LLDB_LOG(log,
1055 " CAS::FEVD[{0}] - Couldn't export a "
1056 "declaration from the modules",
1057 current_id);
1058 return;
1059 }
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001060
Raphael Isemann46883f42019-11-23 20:06:50 +01001061 if (auto copied_function = dyn_cast<clang::FunctionDecl>(copied_decl)) {
1062 MaybeRegisterFunctionBody(copied_function);
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001063
Raphael Isemann46883f42019-11-23 20:06:50 +01001064 context.AddNamedDecl(copied_function);
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001065
Raphael Isemann46883f42019-11-23 20:06:50 +01001066 context.m_found.function_with_type_info = true;
1067 context.m_found.function = true;
1068 } else if (auto copied_var = dyn_cast<clang::VarDecl>(copied_decl)) {
1069 context.AddNamedDecl(copied_var);
1070 context.m_found.variable = true;
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001071 }
1072}
1073
Raphael Isemann7fa976d2019-11-21 12:16:00 +01001074bool ClangExpressionDeclMap::LookupLocalVariable(
1075 NameSearchContext &context, ConstString name, unsigned current_id,
1076 SymbolContext &sym_ctx, CompilerDeclContext &namespace_decl) {
Raphael Isemann7a0c5482019-11-23 17:01:25 +01001077 if (sym_ctx.block == nullptr)
1078 return false;
Raphael Isemann7fa976d2019-11-21 12:16:00 +01001079
Raphael Isemann7a0c5482019-11-23 17:01:25 +01001080 CompilerDeclContext decl_context = sym_ctx.block->GetDeclContext();
1081 if (!decl_context)
1082 return false;
1083
1084 // Make sure that the variables are parsed so that we have the
1085 // declarations.
Raphael Isemann7fa976d2019-11-21 12:16:00 +01001086 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
Raphael Isemann7a0c5482019-11-23 17:01:25 +01001087 VariableListSP vars = frame->GetInScopeVariableList(true);
1088 for (size_t i = 0; i < vars->GetSize(); i++)
1089 vars->GetVariableAtIndex(i)->GetDecl();
Raphael Isemann7fa976d2019-11-21 12:16:00 +01001090
Raphael Isemann7a0c5482019-11-23 17:01:25 +01001091 // Search for declarations matching the name. Do not include imported
1092 // decls in the search if we are looking for decls in the artificial
1093 // namespace $__lldb_local_vars.
1094 std::vector<CompilerDecl> found_decls =
1095 decl_context.FindDeclByName(name, namespace_decl.IsValid());
Raphael Isemann7fa976d2019-11-21 12:16:00 +01001096
Raphael Isemann7a0c5482019-11-23 17:01:25 +01001097 VariableSP var;
1098 bool variable_found = false;
1099 for (CompilerDecl decl : found_decls) {
1100 for (size_t vi = 0, ve = vars->GetSize(); vi != ve; ++vi) {
1101 VariableSP candidate_var = vars->GetVariableAtIndex(vi);
1102 if (candidate_var->GetDecl() == decl) {
1103 var = candidate_var;
1104 break;
Raphael Isemann7fa976d2019-11-21 12:16:00 +01001105 }
1106 }
Raphael Isemann7a0c5482019-11-23 17:01:25 +01001107
1108 if (var && !variable_found) {
1109 variable_found = true;
1110 ValueObjectSP valobj = ValueObjectVariable::Create(frame, var);
1111 AddOneVariable(context, var, valobj, current_id);
1112 context.m_found.variable = true;
1113 }
Raphael Isemann7fa976d2019-11-21 12:16:00 +01001114 }
Raphael Isemann7a0c5482019-11-23 17:01:25 +01001115 return variable_found;
Raphael Isemann7fa976d2019-11-21 12:16:00 +01001116}
1117
Raphael Isemann16c06532019-11-29 15:05:42 +01001118/// Structure to hold the info needed when comparing function
1119/// declarations.
1120namespace {
1121struct FuncDeclInfo {
1122 ConstString m_name;
1123 CompilerType m_copied_type;
1124 uint32_t m_decl_lvl;
1125 SymbolContext m_sym_ctx;
1126};
1127} // namespace
1128
1129SymbolContextList ClangExpressionDeclMap::SearchFunctionsInSymbolContexts(
1130 const SymbolContextList &sc_list,
1131 const CompilerDeclContext &frame_decl_context) {
1132 // First, symplify things by looping through the symbol contexts to
1133 // remove unwanted functions and separate out the functions we want to
1134 // compare and prune into a separate list. Cache the info needed about
1135 // the function declarations in a vector for efficiency.
1136 uint32_t num_indices = sc_list.GetSize();
1137 SymbolContextList sc_sym_list;
1138 std::vector<FuncDeclInfo> decl_infos;
1139 decl_infos.reserve(num_indices);
1140 clang::DeclContext *frame_decl_ctx =
1141 (clang::DeclContext *)frame_decl_context.GetOpaqueDeclContext();
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +01001142 TypeSystemClang *ast = llvm::dyn_cast_or_null<TypeSystemClang>(
Raphael Isemann16c06532019-11-29 15:05:42 +01001143 frame_decl_context.GetTypeSystem());
1144
1145 for (uint32_t index = 0; index < num_indices; ++index) {
1146 FuncDeclInfo fdi;
1147 SymbolContext sym_ctx;
1148 sc_list.GetContextAtIndex(index, sym_ctx);
1149
1150 // We don't know enough about symbols to compare them, but we should
1151 // keep them in the list.
1152 Function *function = sym_ctx.function;
1153 if (!function) {
1154 sc_sym_list.Append(sym_ctx);
1155 continue;
1156 }
1157 // Filter out functions without declaration contexts, as well as
1158 // class/instance methods, since they'll be skipped in the code that
1159 // follows anyway.
1160 CompilerDeclContext func_decl_context = function->GetDeclContext();
1161 if (!func_decl_context ||
1162 func_decl_context.IsClassMethod(nullptr, nullptr, nullptr))
1163 continue;
1164 // We can only prune functions for which we can copy the type.
1165 CompilerType func_clang_type = function->GetType()->GetFullCompilerType();
1166 CompilerType copied_func_type = GuardedCopyType(func_clang_type);
1167 if (!copied_func_type) {
1168 sc_sym_list.Append(sym_ctx);
1169 continue;
1170 }
1171
1172 fdi.m_sym_ctx = sym_ctx;
1173 fdi.m_name = function->GetName();
1174 fdi.m_copied_type = copied_func_type;
1175 fdi.m_decl_lvl = LLDB_INVALID_DECL_LEVEL;
1176 if (fdi.m_copied_type && func_decl_context) {
1177 // Call CountDeclLevels to get the number of parent scopes we have
1178 // to look through before we find the function declaration. When
1179 // comparing functions of the same type, the one with a lower count
1180 // will be closer to us in the lookup scope and shadows the other.
1181 clang::DeclContext *func_decl_ctx =
1182 (clang::DeclContext *)func_decl_context.GetOpaqueDeclContext();
1183 fdi.m_decl_lvl = ast->CountDeclLevels(frame_decl_ctx, func_decl_ctx,
1184 &fdi.m_name, &fdi.m_copied_type);
1185 }
1186 decl_infos.emplace_back(fdi);
1187 }
1188
1189 // Loop through the functions in our cache looking for matching types,
1190 // then compare their scope levels to see which is closer.
1191 std::multimap<CompilerType, const FuncDeclInfo *> matches;
1192 for (const FuncDeclInfo &fdi : decl_infos) {
1193 const CompilerType t = fdi.m_copied_type;
1194 auto q = matches.find(t);
1195 if (q != matches.end()) {
1196 if (q->second->m_decl_lvl > fdi.m_decl_lvl)
1197 // This function is closer; remove the old set.
1198 matches.erase(t);
1199 else if (q->second->m_decl_lvl < fdi.m_decl_lvl)
1200 // The functions in our set are closer - skip this one.
1201 continue;
1202 }
1203 matches.insert(std::make_pair(t, &fdi));
1204 }
1205
1206 // Loop through our matches and add their symbol contexts to our list.
1207 SymbolContextList sc_func_list;
1208 for (const auto &q : matches)
1209 sc_func_list.Append(q.second->m_sym_ctx);
1210
1211 // Rejoin the lists with the functions in front.
1212 sc_func_list.Append(sc_sym_list);
1213 return sc_func_list;
1214}
1215
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001216void ClangExpressionDeclMap::LookupFunction(NameSearchContext &context,
1217 lldb::ModuleSP module_sp,
1218 ConstString name,
1219 CompilerDeclContext &namespace_decl,
1220 unsigned current_id) {
Raphael Isemann4aee81c2019-12-17 13:38:07 +01001221 if (!m_parser_vars)
1222 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001223
Kate Stoneb9c1b512016-09-06 20:57:50 +00001224 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Sean Callananb3a36df2016-03-19 00:51:43 +00001225
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001226 std::vector<clang::NamedDecl *> decls_from_modules;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001227
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001228 if (target) {
1229 if (ClangModulesDeclVendor *decl_vendor =
1230 target->GetClangModulesDeclVendor()) {
1231 decl_vendor->FindDecls(name, false, UINT32_MAX, decls_from_modules);
Sean Callananc02a1c02017-04-24 23:14:04 +00001232 }
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001233 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001234
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001235 const bool include_inlines = false;
Raphael Isemann24e98862019-11-21 13:43:48 +01001236 SymbolContextList sc_list;
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001237 if (namespace_decl && module_sp) {
1238 const bool include_symbols = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001239
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001240 module_sp->FindFunctions(name, &namespace_decl, eFunctionNameTypeBase,
1241 include_symbols, include_inlines, sc_list);
1242 } else if (target && !namespace_decl) {
1243 const bool include_symbols = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001244
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001245 // TODO Fix FindFunctions so that it doesn't return
1246 // instance methods for eFunctionNameTypeBase.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001247
Levon Ter-Grigoryana705cf12020-01-14 14:33:43 +01001248 target->GetImages().FindFunctions(
1249 name, eFunctionNameTypeFull | eFunctionNameTypeBase, include_symbols,
1250 include_inlines, sc_list);
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001251 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001252
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001253 // If we found more than one function, see if we can use the frame's decl
1254 // context to remove functions that are shadowed by other functions which
1255 // match in type but are nearer in scope.
1256 //
1257 // AddOneFunction will not add a function whose type has already been
1258 // added, so if there's another function in the list with a matching type,
1259 // check to see if their decl context is a parent of the current frame's or
1260 // was imported via a and using statement, and pick the best match
1261 // according to lookup rules.
1262 if (sc_list.GetSize() > 1) {
1263 // Collect some info about our frame's context.
1264 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1265 SymbolContext frame_sym_ctx;
1266 if (frame != nullptr)
1267 frame_sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
1268 lldb::eSymbolContextBlock);
1269 CompilerDeclContext frame_decl_context =
1270 frame_sym_ctx.block != nullptr ? frame_sym_ctx.block->GetDeclContext()
1271 : CompilerDeclContext();
Sean Callananc02a1c02017-04-24 23:14:04 +00001272
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001273 // We can't do this without a compiler decl context for our frame.
1274 if (frame_decl_context) {
Raphael Isemann16c06532019-11-29 15:05:42 +01001275 sc_list = SearchFunctionsInSymbolContexts(sc_list, frame_decl_context);
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001276 }
1277 }
1278
1279 if (sc_list.GetSize()) {
1280 Symbol *extern_symbol = nullptr;
1281 Symbol *non_extern_symbol = nullptr;
1282
1283 for (uint32_t index = 0, num_indices = sc_list.GetSize();
1284 index < num_indices; ++index) {
1285 SymbolContext sym_ctx;
1286 sc_list.GetContextAtIndex(index, sym_ctx);
1287
1288 if (sym_ctx.function) {
1289 CompilerDeclContext decl_ctx = sym_ctx.function->GetDeclContext();
1290
1291 if (!decl_ctx)
1292 continue;
1293
1294 // Filter out class/instance methods.
1295 if (decl_ctx.IsClassMethod(nullptr, nullptr, nullptr))
1296 continue;
1297
1298 AddOneFunction(context, sym_ctx.function, nullptr, current_id);
1299 context.m_found.function_with_type_info = true;
1300 context.m_found.function = true;
1301 } else if (sym_ctx.symbol) {
1302 if (sym_ctx.symbol->GetType() == eSymbolTypeReExported && target) {
1303 sym_ctx.symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target);
1304 if (sym_ctx.symbol == nullptr)
1305 continue;
1306 }
1307
1308 if (sym_ctx.symbol->IsExternal())
1309 extern_symbol = sym_ctx.symbol;
1310 else
1311 non_extern_symbol = sym_ctx.symbol;
1312 }
1313 }
1314
1315 if (!context.m_found.function_with_type_info) {
1316 for (clang::NamedDecl *decl : decls_from_modules) {
1317 if (llvm::isa<clang::FunctionDecl>(decl)) {
1318 clang::NamedDecl *copied_decl =
1319 llvm::cast_or_null<FunctionDecl>(CopyDecl(decl));
1320 if (copied_decl) {
1321 context.AddNamedDecl(copied_decl);
1322 context.m_found.function_with_type_info = true;
1323 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001324 }
1325 }
Sean Callananb3a36df2016-03-19 00:51:43 +00001326 }
Sean Callananc02a1c02017-04-24 23:14:04 +00001327
1328 if (!context.m_found.function_with_type_info) {
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001329 if (extern_symbol) {
1330 AddOneFunction(context, nullptr, extern_symbol, current_id);
1331 context.m_found.function = true;
1332 } else if (non_extern_symbol) {
1333 AddOneFunction(context, nullptr, non_extern_symbol, current_id);
1334 context.m_found.function = true;
1335 }
1336 }
1337 }
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001338}
1339
1340void ClangExpressionDeclMap::FindExternalVisibleDecls(
1341 NameSearchContext &context, lldb::ModuleSP module_sp,
1342 CompilerDeclContext &namespace_decl, unsigned int current_id) {
1343 assert(m_ast_context);
1344
1345 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1346
1347 const ConstString name(context.m_decl_name.getAsString().c_str());
1348 if (IgnoreName(name, false))
1349 return;
1350
1351 // Only look for functions by name out in our symbols if the function doesn't
1352 // start with our phony prefix of '$'
Raphael Isemann4aee81c2019-12-17 13:38:07 +01001353
1354 Target *target = nullptr;
1355 StackFrame *frame = nullptr;
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001356 SymbolContext sym_ctx;
Raphael Isemann4aee81c2019-12-17 13:38:07 +01001357 if (m_parser_vars) {
1358 target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1359 frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1360 }
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001361 if (frame != nullptr)
1362 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
1363 lldb::eSymbolContextBlock);
1364
1365 // Try the persistent decls, which take precedence over all else.
1366 if (!namespace_decl)
1367 SearchPersistenDecls(context, name, current_id);
1368
Raphael Isemann8cf8ec402019-11-21 14:40:45 +01001369 if (name.GetStringRef().startswith("$") && !namespace_decl) {
1370 if (name == "$__lldb_class") {
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001371 LookUpLldbClass(context, current_id);
1372 return;
1373 }
1374
Raphael Isemann8cf8ec402019-11-21 14:40:45 +01001375 if (name == "$__lldb_objc_class") {
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001376 LookUpLldbObjCClass(context, current_id);
1377 return;
1378 }
Raphael Isemann8cf8ec402019-11-21 14:40:45 +01001379 if (name == g_lldb_local_vars_namespace_cstr) {
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001380 LookupLocalVarNamespace(sym_ctx, context);
1381 return;
1382 }
1383
1384 // any other $__lldb names should be weeded out now
1385 if (name.GetStringRef().startswith("$__lldb"))
1386 return;
1387
Raphael Isemannd8a31942019-12-17 16:12:07 +01001388 // No ParserVars means we can't do register or variable lookup.
Adrian Prantle9331a52020-01-08 14:18:47 -08001389 if (!m_parser_vars || !m_parser_vars->m_persistent_vars)
Raphael Isemannd8a31942019-12-17 16:12:07 +01001390 return;
1391
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001392 ExpressionVariableSP pvar_sp(
1393 m_parser_vars->m_persistent_vars->GetVariable(name));
1394
1395 if (pvar_sp) {
1396 AddOneVariable(context, pvar_sp, current_id);
1397 return;
1398 }
1399
Raphael Isemann8cf8ec402019-11-21 14:40:45 +01001400 assert(name.GetStringRef().startswith("$"));
1401 llvm::StringRef reg_name = name.GetStringRef().substr(1);
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001402
1403 if (m_parser_vars->m_exe_ctx.GetRegisterContext()) {
1404 const RegisterInfo *reg_info(
1405 m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(
1406 reg_name));
1407
1408 if (reg_info) {
1409 LLDB_LOGF(log, " CEDM::FEVD[%u] Found register %s", current_id,
1410 reg_info->name);
1411
1412 AddOneRegister(context, reg_info, current_id);
1413 }
1414 }
1415 return;
1416 }
1417
Raphael Isemann8cf8ec402019-11-21 14:40:45 +01001418 bool local_var_lookup = !namespace_decl || (namespace_decl.GetName() ==
1419 g_lldb_local_vars_namespace_cstr);
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001420 if (frame && local_var_lookup)
1421 if (LookupLocalVariable(context, name, current_id, sym_ctx, namespace_decl))
1422 return;
1423
1424 if (target) {
1425 ValueObjectSP valobj;
1426 VariableSP var;
Raphael Isemannb6c29d92019-12-23 12:18:49 +01001427 var = FindGlobalVariable(*target, module_sp, name, &namespace_decl);
Raphael Isemann5fb7dd82019-11-21 13:58:48 +01001428
1429 if (var) {
1430 valobj = ValueObjectVariable::Create(target, var);
1431 AddOneVariable(context, var, valobj, current_id);
1432 context.m_found.variable = true;
1433 return;
1434 }
1435 }
1436
1437 LookupFunction(context, module_sp, name, namespace_decl, current_id);
Sean Callananc02a1c02017-04-24 23:14:04 +00001438
Raphael Isemanna0408ab2019-11-21 11:29:59 +01001439 // Try the modules next.
1440 if (!context.m_found.function_with_type_info)
1441 LookupInModulesDeclVendor(context, name, current_id);
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001442
1443 if (target && !context.m_found.variable && !namespace_decl) {
1444 // We couldn't find a non-symbol variable for this. Now we'll hunt for a
1445 // generic data symbol, and -- if it is found -- treat it as a variable.
1446 Status error;
1447
1448 const Symbol *data_symbol =
1449 m_parser_vars->m_sym_ctx.FindBestGlobalDataSymbol(name, error);
1450
1451 if (!error.Success()) {
1452 const unsigned diag_id =
1453 m_ast_context->getDiagnostics().getCustomDiagID(
1454 clang::DiagnosticsEngine::Level::Error, "%0");
1455 m_ast_context->getDiagnostics().Report(diag_id) << error.AsCString();
Sean Callananc02a1c02017-04-24 23:14:04 +00001456 }
1457
Raphael Isemann2cada1e2019-11-20 16:12:50 +01001458 if (data_symbol) {
1459 std::string warning("got name from symbols: ");
1460 warning.append(name.AsCString());
1461 const unsigned diag_id =
1462 m_ast_context->getDiagnostics().getCustomDiagID(
1463 clang::DiagnosticsEngine::Level::Warning, "%0");
1464 m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str();
1465 AddOneGenericVariable(context, *data_symbol, current_id);
1466 context.m_found.variable = true;
Sean Callananc02a1c02017-04-24 23:14:04 +00001467 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468 }
Sean Callanan6b4067c2010-07-17 00:43:37 +00001469}
Greg Claytona2721472011-06-25 00:44:06 +00001470
Kate Stoneb9c1b512016-09-06 20:57:50 +00001471bool ClangExpressionDeclMap::GetVariableValue(VariableSP &var,
1472 lldb_private::Value &var_location,
1473 TypeFromUser *user_type,
1474 TypeFromParser *parser_type) {
1475 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001476
Kate Stoneb9c1b512016-09-06 20:57:50 +00001477 Type *var_type = var->GetType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001478
Kate Stoneb9c1b512016-09-06 20:57:50 +00001479 if (!var_type) {
Sean Callananea22d422010-07-16 00:09:46 +00001480 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001481 log->PutCString("Skipped a definition because it has no type");
1482 return false;
1483 }
1484
1485 CompilerType var_clang_type = var_type->GetFullCompilerType();
1486
1487 if (!var_clang_type) {
1488 if (log)
1489 log->PutCString("Skipped a definition because it has no Clang type");
1490 return false;
1491 }
1492
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +01001493 TypeSystemClang *clang_ast = llvm::dyn_cast_or_null<TypeSystemClang>(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001494 var_type->GetForwardCompilerType().GetTypeSystem());
1495
1496 if (!clang_ast) {
1497 if (log)
1498 log->PutCString("Skipped a definition because it has no Clang AST");
1499 return false;
1500 }
1501
Kate Stoneb9c1b512016-09-06 20:57:50 +00001502 DWARFExpression &var_location_expr = var->LocationExpression();
1503
1504 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Zachary Turner97206d52017-05-12 04:51:55 +00001505 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001506
1507 if (var->GetLocationIsConstantValueData()) {
1508 DataExtractor const_value_extractor;
1509
1510 if (var_location_expr.GetExpressionData(const_value_extractor)) {
1511 var_location = Value(const_value_extractor.GetDataStart(),
1512 const_value_extractor.GetByteSize());
1513 var_location.SetValueType(Value::eValueTypeHostAddress);
1514 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001515 LLDB_LOGF(log, "Error evaluating constant variable: %s", err.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001516 return false;
Greg Clayton7b462cc2010-10-15 22:48:33 +00001517 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001518 }
1519
1520 CompilerType type_to_use = GuardedCopyType(var_clang_type);
1521
1522 if (!type_to_use) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001523 LLDB_LOGF(log,
1524 "Couldn't copy a variable's type into the parser's AST context");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001525
1526 return false;
1527 }
1528
1529 if (parser_type)
1530 *parser_type = TypeFromParser(type_to_use);
1531
1532 if (var_location.GetContextType() == Value::eContextTypeInvalid)
1533 var_location.SetCompilerType(type_to_use);
1534
1535 if (var_location.GetValueType() == Value::eValueTypeFileAddress) {
1536 SymbolContext var_sc;
1537 var->CalculateSymbolContext(&var_sc);
1538
1539 if (!var_sc.module_sp)
1540 return false;
1541
1542 Address so_addr(var_location.GetScalar().ULongLong(),
1543 var_sc.module_sp->GetSectionList());
1544
1545 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
1546
1547 if (load_addr != LLDB_INVALID_ADDRESS) {
1548 var_location.GetScalar() = load_addr;
1549 var_location.SetValueType(Value::eValueTypeLoadAddress);
1550 }
1551 }
1552
1553 if (user_type)
1554 *user_type = TypeFromUser(var_clang_type);
1555
1556 return true;
Sean Callanan468574b2010-06-22 23:46:24 +00001557}
1558
Kate Stoneb9c1b512016-09-06 20:57:50 +00001559void ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
1560 VariableSP var,
1561 ValueObjectSP valobj,
1562 unsigned int current_id) {
1563 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001564
Kate Stoneb9c1b512016-09-06 20:57:50 +00001565 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001566
Kate Stoneb9c1b512016-09-06 20:57:50 +00001567 TypeFromUser ut;
1568 TypeFromParser pt;
1569 Value var_location;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001570
Kate Stoneb9c1b512016-09-06 20:57:50 +00001571 if (!GetVariableValue(var, var_location, &ut, &pt))
1572 return;
1573
1574 clang::QualType parser_opaque_type =
1575 QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
1576
1577 if (parser_opaque_type.isNull())
1578 return;
1579
1580 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr()) {
1581 if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1582 CompleteType(tag_type->getDecl());
1583 if (const ObjCObjectPointerType *objc_object_ptr_type =
1584 dyn_cast<ObjCObjectPointerType>(parser_type))
1585 CompleteType(objc_object_ptr_type->getInterfaceDecl());
1586 }
1587
1588 bool is_reference = pt.IsReferenceType();
1589
Konrad Kleine248a1302019-05-23 11:14:47 +00001590 NamedDecl *var_decl = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001591 if (is_reference)
1592 var_decl = context.AddVarDecl(pt);
1593 else
1594 var_decl = context.AddVarDecl(pt.GetLValueReferenceType());
1595
1596 std::string decl_name(context.m_decl_name.getAsString());
1597 ConstString entity_name(decl_name.c_str());
1598 ClangExpressionVariable *entity(new ClangExpressionVariable(valobj));
1599 m_found_entities.AddNewlyConstructedVariable(entity);
1600
1601 assert(entity);
1602 entity->EnableParserVars(GetParserID());
1603 ClangExpressionVariable::ParserVars *parser_vars =
1604 entity->GetParserVars(GetParserID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001605 parser_vars->m_named_decl = var_decl;
Konrad Kleine248a1302019-05-23 11:14:47 +00001606 parser_vars->m_llvm_value = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001607 parser_vars->m_lldb_value = var_location;
1608 parser_vars->m_lldb_var = var;
1609
1610 if (is_reference)
1611 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference;
1612
Raphael Isemann7a6588a2019-11-25 13:27:51 +01001613 LLDB_LOG(log,
1614 " CEDM::FEVD[{0}] Found variable {1}, returned\n{2} (original {3})",
1615 current_id, decl_name, ClangUtil::DumpDecl(var_decl),
1616 ClangUtil::ToString(ut));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001617}
1618
1619void ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
1620 ExpressionVariableSP &pvar_sp,
1621 unsigned int current_id) {
1622 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1623
1624 TypeFromUser user_type(
1625 llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetTypeFromUser());
1626
1627 TypeFromParser parser_type(GuardedCopyType(user_type));
1628
1629 if (!parser_type.GetOpaqueQualType()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001630 LLDB_LOGF(log, " CEDM::FEVD[%u] Couldn't import type for pvar %s",
1631 current_id, pvar_sp->GetName().GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001632 return;
1633 }
1634
1635 NamedDecl *var_decl =
1636 context.AddVarDecl(parser_type.GetLValueReferenceType());
1637
1638 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1639 ->EnableParserVars(GetParserID());
1640 ClangExpressionVariable::ParserVars *parser_vars =
1641 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1642 ->GetParserVars(GetParserID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001643 parser_vars->m_named_decl = var_decl;
Konrad Kleine248a1302019-05-23 11:14:47 +00001644 parser_vars->m_llvm_value = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001645 parser_vars->m_lldb_value.Clear();
1646
Raphael Isemann7a6588a2019-11-25 13:27:51 +01001647 LLDB_LOG(log, " CEDM::FEVD[{0}] Added pvar {1}, returned\n{2}", current_id,
1648 pvar_sp->GetName(), ClangUtil::DumpDecl(var_decl));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001649}
1650
1651void ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context,
1652 const Symbol &symbol,
1653 unsigned int current_id) {
1654 assert(m_parser_vars.get());
1655
1656 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1657
1658 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1659
Konrad Kleine248a1302019-05-23 11:14:47 +00001660 if (target == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001661 return;
1662
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +01001663 TypeSystemClang *scratch_ast_context = TypeSystemClang::GetScratch(*target);
Alex Langford30318182019-11-14 13:41:52 -08001664 if (!scratch_ast_context)
1665 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001666
Raphael Isemannc502bae2019-11-20 12:40:08 +01001667 TypeFromUser user_type(scratch_ast_context->GetBasicType(eBasicTypeVoid)
1668 .GetPointerType()
1669 .GetLValueReferenceType());
Raphael Isemannbc7f1df2019-11-29 13:02:41 +01001670 TypeFromParser parser_type(m_clang_ast_context->GetBasicType(eBasicTypeVoid)
Raphael Isemannc502bae2019-11-20 12:40:08 +01001671 .GetPointerType()
1672 .GetLValueReferenceType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001673 NamedDecl *var_decl = context.AddVarDecl(parser_type);
1674
1675 std::string decl_name(context.m_decl_name.getAsString());
1676 ConstString entity_name(decl_name.c_str());
1677 ClangExpressionVariable *entity(new ClangExpressionVariable(
1678 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), entity_name,
1679 user_type, m_parser_vars->m_target_info.byte_order,
1680 m_parser_vars->m_target_info.address_byte_size));
1681 m_found_entities.AddNewlyConstructedVariable(entity);
1682
1683 entity->EnableParserVars(GetParserID());
1684 ClangExpressionVariable::ParserVars *parser_vars =
1685 entity->GetParserVars(GetParserID());
1686
1687 const Address symbol_address = symbol.GetAddress();
1688 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target);
1689
1690 // parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType,
1691 // user_type.GetOpaqueQualType());
1692 parser_vars->m_lldb_value.SetCompilerType(user_type);
1693 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr;
1694 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1695
Kate Stoneb9c1b512016-09-06 20:57:50 +00001696 parser_vars->m_named_decl = var_decl;
Konrad Kleine248a1302019-05-23 11:14:47 +00001697 parser_vars->m_llvm_value = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001698 parser_vars->m_lldb_sym = &symbol;
1699
Raphael Isemann7a6588a2019-11-25 13:27:51 +01001700 LLDB_LOG(log, " CEDM::FEVD[{0}] Found variable {1}, returned\n{2}",
1701 current_id, decl_name, ClangUtil::DumpDecl(var_decl));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001702}
1703
Kate Stoneb9c1b512016-09-06 20:57:50 +00001704void ClangExpressionDeclMap::AddOneRegister(NameSearchContext &context,
1705 const RegisterInfo *reg_info,
1706 unsigned int current_id) {
1707 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001708
Kate Stoneb9c1b512016-09-06 20:57:50 +00001709 CompilerType clang_type =
Raphael Isemannc214c922019-11-29 13:43:23 +01001710 m_clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
1711 reg_info->encoding, reg_info->byte_size * 8);
Sean Callanan7736a202016-04-29 18:09:03 +00001712
Kate Stoneb9c1b512016-09-06 20:57:50 +00001713 if (!clang_type) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001714 LLDB_LOGF(log, " Tried to add a type for %s, but couldn't get one",
1715 context.m_decl_name.getAsString().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001716 return;
1717 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001718
Kate Stoneb9c1b512016-09-06 20:57:50 +00001719 TypeFromParser parser_clang_type(clang_type);
Sean Callananfa4fab72013-02-01 06:55:48 +00001720
Kate Stoneb9c1b512016-09-06 20:57:50 +00001721 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001722
Kate Stoneb9c1b512016-09-06 20:57:50 +00001723 ClangExpressionVariable *entity(new ClangExpressionVariable(
1724 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1725 m_parser_vars->m_target_info.byte_order,
1726 m_parser_vars->m_target_info.address_byte_size));
1727 m_found_entities.AddNewlyConstructedVariable(entity);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001728
Kate Stoneb9c1b512016-09-06 20:57:50 +00001729 std::string decl_name(context.m_decl_name.getAsString());
1730 entity->SetName(ConstString(decl_name.c_str()));
1731 entity->SetRegisterInfo(reg_info);
1732 entity->EnableParserVars(GetParserID());
1733 ClangExpressionVariable::ParserVars *parser_vars =
1734 entity->GetParserVars(GetParserID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001735 parser_vars->m_named_decl = var_decl;
Konrad Kleine248a1302019-05-23 11:14:47 +00001736 parser_vars->m_llvm_value = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001737 parser_vars->m_lldb_value.Clear();
1738 entity->m_flags |= ClangExpressionVariable::EVBareRegister;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001739
Raphael Isemann7a6588a2019-11-25 13:27:51 +01001740 LLDB_LOG(log, " CEDM::FEVD[{0}] Added register {1}, returned\n{2}",
1741 current_id, context.m_decl_name.getAsString(),
1742 ClangUtil::DumpDecl(var_decl));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001743}
1744
1745void ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context,
1746 Function *function, Symbol *symbol,
1747 unsigned int current_id) {
1748 assert(m_parser_vars.get());
1749
1750 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1751
Konrad Kleine248a1302019-05-23 11:14:47 +00001752 NamedDecl *function_decl = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001753 Address fun_address;
1754 CompilerType function_clang_type;
1755
1756 bool is_indirect_function = false;
1757
1758 if (function) {
1759 Type *function_type = function->GetType();
1760
Luke Drummondf5bb1d62016-12-19 17:22:44 +00001761 const auto lang = function->GetCompileUnit()->GetLanguage();
1762 const auto name = function->GetMangled().GetMangledName().AsCString();
1763 const bool extern_c = (Language::LanguageIsC(lang) &&
1764 !CPlusPlusLanguage::IsCPPMangledName(name)) ||
1765 (Language::LanguageIsObjC(lang) &&
1766 !Language::LanguageIsCPlusPlus(lang));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001767
1768 if (!extern_c) {
1769 TypeSystem *type_system = function->GetDeclContext().GetTypeSystem();
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +01001770 if (llvm::isa<TypeSystemClang>(type_system)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001771 clang::DeclContext *src_decl_context =
1772 (clang::DeclContext *)function->GetDeclContext()
1773 .GetOpaqueDeclContext();
1774 clang::FunctionDecl *src_function_decl =
1775 llvm::dyn_cast_or_null<clang::FunctionDecl>(src_decl_context);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001776 if (src_function_decl &&
1777 src_function_decl->getTemplateSpecializationInfo()) {
1778 clang::FunctionTemplateDecl *function_template =
1779 src_function_decl->getTemplateSpecializationInfo()->getTemplate();
1780 clang::FunctionTemplateDecl *copied_function_template =
1781 llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(
Sean Callanan68e44232017-09-28 20:20:25 +00001782 CopyDecl(function_template));
Sean Callanan09e91ac2017-05-11 22:08:05 +00001783 if (copied_function_template) {
1784 if (log) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001785 StreamString ss;
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +00001786
Sean Callanan09e91ac2017-05-11 22:08:05 +00001787 function->DumpSymbolContext(&ss);
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +00001788
Raphael Isemann7a6588a2019-11-25 13:27:51 +01001789 LLDB_LOG(log,
1790 " CEDM::FEVD[{0}] Imported decl for function template"
1791 " {1} (description {2}), returned\n{3}",
1792 current_id, copied_function_template->getNameAsString(),
1793 ss.GetData(),
1794 ClangUtil::DumpDecl(copied_function_template));
Sean Callanan09e91ac2017-05-11 22:08:05 +00001795 }
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +00001796
Sean Callanan09e91ac2017-05-11 22:08:05 +00001797 context.AddNamedDecl(copied_function_template);
1798 }
1799 } else if (src_function_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001800 if (clang::FunctionDecl *copied_function_decl =
1801 llvm::dyn_cast_or_null<clang::FunctionDecl>(
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +00001802 CopyDecl(src_function_decl))) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001803 if (log) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001804 StreamString ss;
1805
1806 function->DumpSymbolContext(&ss);
1807
Raphael Isemann7a6588a2019-11-25 13:27:51 +01001808 LLDB_LOG(log,
1809 " CEDM::FEVD[{0}]] Imported decl for function {1} "
1810 "(description {2}), returned\n{3}",
1811 current_id, copied_function_decl->getNameAsString(),
1812 ss.GetData(), ClangUtil::DumpDecl(copied_function_decl));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001813 }
1814
1815 context.AddNamedDecl(copied_function_decl);
1816 return;
1817 } else {
1818 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001819 LLDB_LOGF(log, " Failed to import the function decl for '%s'",
1820 src_function_decl->getName().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001821 }
1822 }
Sean Callanan7736a202016-04-29 18:09:03 +00001823 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001824 }
Sean Callananfc55f5d2010-09-21 00:44:12 +00001825 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001826
Kate Stoneb9c1b512016-09-06 20:57:50 +00001827 if (!function_type) {
1828 if (log)
1829 log->PutCString(" Skipped a function because it has no type");
1830 return;
1831 }
1832
1833 function_clang_type = function_type->GetFullCompilerType();
1834
1835 if (!function_clang_type) {
1836 if (log)
1837 log->PutCString(" Skipped a function because it has no Clang type");
1838 return;
1839 }
1840
1841 fun_address = function->GetAddressRange().GetBaseAddress();
1842
1843 CompilerType copied_function_type = GuardedCopyType(function_clang_type);
1844 if (copied_function_type) {
1845 function_decl = context.AddFunDecl(copied_function_type, extern_c);
1846
1847 if (!function_decl) {
1848 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001849 LLDB_LOGF(
1850 log,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001851 " Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}",
1852 function_type->GetName().GetCString(), function_type->GetID());
1853 }
1854
Sean Callanan3b436d22015-10-23 21:45:02 +00001855 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001856 }
1857 } else {
1858 // We failed to copy the type we found
1859 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001860 LLDB_LOGF(log,
1861 " Failed to import the function type '%s' {0x%8.8" PRIx64
1862 "} into the expression parser AST contenxt",
1863 function_type->GetName().GetCString(),
1864 function_type->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001865 }
1866
1867 return;
1868 }
1869 } else if (symbol) {
1870 fun_address = symbol->GetAddress();
1871 function_decl = context.AddGenericFunDecl();
1872 is_indirect_function = symbol->IsIndirect();
1873 } else {
1874 if (log)
1875 log->PutCString(" AddOneFunction called with no function and no symbol");
1876 return;
1877 }
1878
1879 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1880
1881 lldb::addr_t load_addr =
1882 fun_address.GetCallableLoadAddress(target, is_indirect_function);
1883
1884 ClangExpressionVariable *entity(new ClangExpressionVariable(
1885 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1886 m_parser_vars->m_target_info.byte_order,
1887 m_parser_vars->m_target_info.address_byte_size));
1888 m_found_entities.AddNewlyConstructedVariable(entity);
1889
1890 std::string decl_name(context.m_decl_name.getAsString());
1891 entity->SetName(ConstString(decl_name.c_str()));
1892 entity->SetCompilerType(function_clang_type);
1893 entity->EnableParserVars(GetParserID());
1894
1895 ClangExpressionVariable::ParserVars *parser_vars =
1896 entity->GetParserVars(GetParserID());
1897
1898 if (load_addr != LLDB_INVALID_ADDRESS) {
1899 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1900 parser_vars->m_lldb_value.GetScalar() = load_addr;
1901 } else {
1902 // We have to try finding a file address.
1903
1904 lldb::addr_t file_addr = fun_address.GetFileAddress();
1905
1906 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress);
1907 parser_vars->m_lldb_value.GetScalar() = file_addr;
1908 }
1909
1910 parser_vars->m_named_decl = function_decl;
Konrad Kleine248a1302019-05-23 11:14:47 +00001911 parser_vars->m_llvm_value = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001912
1913 if (log) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001914 StreamString ss;
1915
1916 fun_address.Dump(&ss,
1917 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1918 Address::DumpStyleResolvedDescription);
1919
Raphael Isemann7a6588a2019-11-25 13:27:51 +01001920 LLDB_LOG(log,
1921 " CEDM::FEVD[{0}] Found {1} function {2} (description {3}), "
1922 "returned\n{4}",
1923 current_id, (function ? "specific" : "generic"), decl_name,
1924 ss.GetData(), ClangUtil::DumpDecl(function_decl));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001925 }
1926}
1927
1928void ClangExpressionDeclMap::AddThisType(NameSearchContext &context,
Aleksandr Urakov40624a02019-02-05 09:14:36 +00001929 const TypeFromUser &ut,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001930 unsigned int current_id) {
1931 CompilerType copied_clang_type = GuardedCopyType(ut);
1932
1933 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1934
1935 if (!copied_clang_type) {
1936 if (log)
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001937 LLDB_LOGF(
1938 log,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001939 "ClangExpressionDeclMap::AddThisType - Couldn't import the type");
Sean Callanan3b436d22015-10-23 21:45:02 +00001940
1941 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001942 }
1943
1944 if (copied_clang_type.IsAggregateType() &&
1945 copied_clang_type.GetCompleteType()) {
Raphael Isemannbc7f1df2019-11-29 13:02:41 +01001946 CompilerType void_clang_type =
1947 m_clang_ast_context->GetBasicType(eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001948 CompilerType void_ptr_clang_type = void_clang_type.GetPointerType();
1949
Raphael Isemann0007f9d2019-12-29 20:10:29 +01001950 CompilerType method_type = m_clang_ast_context->CreateFunctionType(
1951 void_clang_type, &void_ptr_clang_type, 1, false, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001952
1953 const bool is_virtual = false;
1954 const bool is_static = false;
1955 const bool is_inline = false;
1956 const bool is_explicit = false;
1957 const bool is_attr_used = true;
1958 const bool is_artificial = false;
1959
Raphael Isemannbc7f1df2019-11-29 13:02:41 +01001960 CXXMethodDecl *method_decl = m_clang_ast_context->AddMethodToCXXRecordType(
1961 copied_clang_type.GetOpaqueQualType(), "$__lldb_expr", nullptr,
1962 method_type, lldb::eAccessPublic, is_virtual, is_static, is_inline,
1963 is_explicit, is_attr_used, is_artificial);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001964
Raphael Isemann7a6588a2019-11-25 13:27:51 +01001965 LLDB_LOG(log,
1966 " CEDM::AddThisType Added function $__lldb_expr "
1967 "(description {0}) for this type\n{1}",
1968 ClangUtil::ToString(copied_clang_type),
1969 ClangUtil::DumpDecl(method_decl));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001970 }
1971
1972 if (!copied_clang_type.IsValid())
1973 return;
1974
1975 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(
1976 QualType::getFromOpaquePtr(copied_clang_type.GetOpaqueQualType()));
1977
1978 if (!type_source_info)
1979 return;
1980
1981 // Construct a typedef type because if "*this" is a templated type we can't
1982 // just return ClassTemplateSpecializationDecls in response to name queries.
1983 // Using a typedef makes this much more robust.
1984
1985 TypedefDecl *typedef_decl = TypedefDecl::Create(
1986 *m_ast_context, m_ast_context->getTranslationUnitDecl(), SourceLocation(),
1987 SourceLocation(), context.m_decl_name.getAsIdentifierInfo(),
1988 type_source_info);
1989
1990 if (!typedef_decl)
1991 return;
1992
1993 context.AddNamedDecl(typedef_decl);
1994
1995 return;
Sean Callananfa4fab72013-02-01 06:55:48 +00001996}
1997
Kate Stoneb9c1b512016-09-06 20:57:50 +00001998void ClangExpressionDeclMap::AddOneType(NameSearchContext &context,
Aleksandr Urakov40624a02019-02-05 09:14:36 +00001999 const TypeFromUser &ut,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002000 unsigned int current_id) {
2001 CompilerType copied_clang_type = GuardedCopyType(ut);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002002
Kate Stoneb9c1b512016-09-06 20:57:50 +00002003 if (!copied_clang_type) {
2004 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfa4fab72013-02-01 06:55:48 +00002005
Kate Stoneb9c1b512016-09-06 20:57:50 +00002006 if (log)
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00002007 LLDB_LOGF(
2008 log, "ClangExpressionDeclMap::AddOneType - Couldn't import the type");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002009
Kate Stoneb9c1b512016-09-06 20:57:50 +00002010 return;
2011 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002012
Kate Stoneb9c1b512016-09-06 20:57:50 +00002013 context.AddTypeDecl(copied_clang_type);
Sean Callanan5666b672010-08-04 01:02:13 +00002014}