blob: da106f4f8f44a9e9f02dfff7f54870c7890c4876 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangExpressionDeclMap.cpp -----------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Sean Callanan4dbb2712015-09-25 20:35:58 +00009#include "ClangExpressionDeclMap.h"
10
11#include "ASTDumper.h"
12#include "ClangASTSource.h"
13#include "ClangModulesDeclVendor.h"
14#include "ClangPersistentVariables.h"
15
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Address.h"
17#include "lldb/Core/Module.h"
Greg Clayton9191db42013-10-21 18:40:51 +000018#include "lldb/Core/ModuleSpec.h"
Sean Callanane4ec90e2010-12-16 03:17:46 +000019#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan9b3569b2011-12-10 03:12:34 +000020#include "lldb/Core/ValueObjectVariable.h"
Sean Callanan96d27302013-04-11 00:09:05 +000021#include "lldb/Expression/Materializer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Symbol/ClangASTContext.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,
Krasimir Georgiev7ffc4682019-02-05 11:35:45 +000068 ExecutionContext &exe_ctx, ValueObject *ctx_obj)
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 : ClangASTSource(exe_ctx.GetTargetSP()), m_found_entities(),
70 m_struct_members(), m_keep_result_in_memory(keep_result_in_memory),
Krasimir Georgiev7ffc4682019-02-05 11:35:45 +000071 m_result_delegate(result_delegate), m_ctx_obj(ctx_obj), m_parser_vars(),
72 m_struct_vars() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 EnableStructVars();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074}
75
Kate Stoneb9c1b512016-09-06 20:57:50 +000076ClangExpressionDeclMap::~ClangExpressionDeclMap() {
77 // Note: The model is now that the parser's AST context and all associated
78 // data does not vanish until the expression has been executed. This means
79 // that valuable lookup data (like namespaces) doesn't vanish, but
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000080
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 DidParse();
82 DisableStructVars();
Sean Callanan979f74d2010-12-03 01:38:59 +000083}
Sean Callananbe3a1b12010-10-26 00:31:56 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085bool ClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx,
86 Materializer *materializer) {
87 ClangASTMetrics::ClearLocalCounters();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 EnableParserVars();
90 m_parser_vars->m_exe_ctx = exe_ctx;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000091
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 Target *target = exe_ctx.GetTargetPtr();
93 if (exe_ctx.GetFramePtr())
94 m_parser_vars->m_sym_ctx =
95 exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything);
96 else if (exe_ctx.GetThreadPtr() &&
97 exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0))
98 m_parser_vars->m_sym_ctx =
99 exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext(
100 lldb::eSymbolContextEverything);
101 else if (exe_ctx.GetProcessPtr()) {
102 m_parser_vars->m_sym_ctx.Clear(true);
103 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
104 } else if (target) {
105 m_parser_vars->m_sym_ctx.Clear(true);
106 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
107 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 if (target) {
110 m_parser_vars->m_persistent_vars = llvm::cast<ClangPersistentVariables>(
111 target->GetPersistentExpressionStateForLanguage(eLanguageTypeC));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 if (!target->GetScratchClangASTContext())
114 return false;
115 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 m_parser_vars->m_target_info = GetTargetInfo();
118 m_parser_vars->m_materializer = materializer;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 return true;
Sean Callanan979f74d2010-12-03 01:38:59 +0000121}
122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123void ClangExpressionDeclMap::InstallCodeGenerator(
124 clang::ASTConsumer *code_gen) {
125 assert(m_parser_vars);
126 m_parser_vars->m_code_gen = code_gen;
Sean Callanan80c97592015-05-01 00:47:29 +0000127}
128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129void ClangExpressionDeclMap::DidParse() {
130 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8106d802013-03-08 20:04:57 +0000131
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 if (log)
133 ClangASTMetrics::DumpCounters(log);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000134
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000135 if (m_parser_vars) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 for (size_t entity_index = 0, num_entities = m_found_entities.GetSize();
137 entity_index < num_entities; ++entity_index) {
138 ExpressionVariableSP var_sp(
139 m_found_entities.GetVariableAtIndex(entity_index));
140 if (var_sp)
141 llvm::cast<ClangExpressionVariable>(var_sp.get())
142 ->DisableParserVars(GetParserID());
Sean Callanan6b1b9532010-10-08 01:58:41 +0000143 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144
145 for (size_t pvar_index = 0,
146 num_pvars = m_parser_vars->m_persistent_vars->GetSize();
147 pvar_index < num_pvars; ++pvar_index) {
148 ExpressionVariableSP pvar_sp(
149 m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index));
150 if (ClangExpressionVariable *clang_var =
151 llvm::dyn_cast<ClangExpressionVariable>(pvar_sp.get()))
152 clang_var->DisableParserVars(GetParserID());
153 }
154
155 DisableParserVars();
156 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157}
158
Sean Callanan549c9f72010-07-13 21:41:46 +0000159// Interface for IRForTarget
160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161ClangExpressionDeclMap::TargetInfo ClangExpressionDeclMap::GetTargetInfo() {
162 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 TargetInfo ret;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
Sean Callanan933693b2012-02-10 01:22:05 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 Process *process = exe_ctx.GetProcessPtr();
169 if (process) {
170 ret.byte_order = process->GetByteOrder();
171 ret.address_byte_size = process->GetAddressByteSize();
172 } else {
173 Target *target = exe_ctx.GetTargetPtr();
174 if (target) {
175 ret.byte_order = target->GetArchitecture().GetByteOrder();
176 ret.address_byte_size = target->GetArchitecture().GetAddressByteSize();
Sean Callanan933693b2012-02-10 01:22:05 +0000177 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 }
Sean Callanan933693b2012-02-10 01:22:05 +0000179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 return ret;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000181}
182
Sean Callanan68e44232017-09-28 20:20:25 +0000183static clang::QualType ExportAllDeclaredTypes(
Raphael Isemann51e0bbb2019-10-04 08:26:17 +0000184 clang::ExternalASTMerger &parent_merger, clang::ExternalASTMerger &merger,
185 clang::ASTContext &source, clang::FileManager &source_file_manager,
Sean Callanan68e44232017-09-28 20:20:25 +0000186 const clang::ExternalASTMerger::OriginMap &source_origin_map,
187 clang::FileID file, clang::QualType root) {
Raphael Isemann51e0bbb2019-10-04 08:26:17 +0000188 // Mark the source as temporary to make sure all declarations from the
189 // AST are exported. Also add the parent_merger as the merger into the
190 // source AST so that the merger can track back any declarations from
191 // the persistent ASTs we used as sources.
Raphael Isemanncf628712019-10-01 09:02:05 +0000192 clang::ExternalASTMerger::ImporterSource importer_source(
Raphael Isemann51e0bbb2019-10-04 08:26:17 +0000193 source, source_file_manager, source_origin_map, /*Temporary*/ true,
194 &parent_merger);
Sean Callanan68e44232017-09-28 20:20:25 +0000195 merger.AddSources(importer_source);
196 clang::ASTImporter &exporter = merger.ImporterForOrigin(source);
Gabor Marton5ac6d492019-05-15 10:29:48 +0000197 llvm::Expected<clang::QualType> ret_or_error = exporter.Import(root);
Sean Callanan68e44232017-09-28 20:20:25 +0000198 merger.RemoveSources(importer_source);
Gabor Marton5ac6d492019-05-15 10:29:48 +0000199 if (ret_or_error) {
200 return *ret_or_error;
201 } else {
202 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
203 LLDB_LOG_ERROR(log, ret_or_error.takeError(), "Couldn't import type: {0}");
204 return clang::QualType();
205 }
Sean Callanan68e44232017-09-28 20:20:25 +0000206}
207
208TypeFromUser ClangExpressionDeclMap::DeportType(ClangASTContext &target,
209 ClangASTContext &source,
210 TypeFromParser parser_type) {
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +0000211 assert(&target == m_target->GetScratchClangASTContext());
212 assert((TypeSystem *)&source == parser_type.GetTypeSystem());
213 assert(source.getASTContext() == m_ast_context);
214
Sean Callanan68e44232017-09-28 20:20:25 +0000215 if (m_ast_importer_sp) {
216 return TypeFromUser(m_ast_importer_sp->DeportType(
217 target.getASTContext(), source.getASTContext(),
218 parser_type.GetOpaqueQualType()),
219 &target);
220 } else if (m_merger_up) {
221 clang::FileID source_file =
222 source.getASTContext()->getSourceManager().getFileID(
223 source.getASTContext()->getTranslationUnitDecl()->getLocation());
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +0000224 auto scratch_ast_context = static_cast<ClangASTContextForExpressions *>(
Sean Callanan68e44232017-09-28 20:20:25 +0000225 m_target->GetScratchClangASTContext());
226 clang::QualType exported_type = ExportAllDeclaredTypes(
Raphael Isemann51e0bbb2019-10-04 08:26:17 +0000227 *m_merger_up.get(), scratch_ast_context->GetMergerUnchecked(),
228 *source.getASTContext(), *source.getFileManager(),
229 m_merger_up->GetOrigins(), source_file,
Sean Callanan68e44232017-09-28 20:20:25 +0000230 clang::QualType::getFromOpaquePtr(parser_type.GetOpaqueQualType()));
231 return TypeFromUser(exported_type.getAsOpaquePtr(), &target);
232 } else {
Pavel Labath9344e452017-10-17 21:52:29 +0000233 lldbassert(0 && "No mechanism for deporting a type!");
Sean Callanan68e44232017-09-28 20:20:25 +0000234 return TypeFromUser();
235 }
236}
237
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238bool ClangExpressionDeclMap::AddPersistentVariable(const NamedDecl *decl,
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000239 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240 TypeFromParser parser_type,
241 bool is_result,
242 bool is_lvalue) {
243 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000244
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245 ClangASTContext *ast =
246 llvm::dyn_cast_or_null<ClangASTContext>(parser_type.GetTypeSystem());
247 if (ast == nullptr)
248 return false;
Greg Claytonf73034f2015-09-08 18:15:05 +0000249
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 if (m_parser_vars->m_materializer && is_result) {
Zachary Turner97206d52017-05-12 04:51:55 +0000251 Status err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000252
Sean Callanan933693b2012-02-10 01:22:05 +0000253 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
254 Target *target = exe_ctx.GetTargetPtr();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255 if (target == nullptr)
256 return false;
Greg Claytonc14ee322011-09-22 04:58:26 +0000257
Sean Callanan68e44232017-09-28 20:20:25 +0000258 TypeFromUser user_type =
259 DeportType(*target->GetScratchClangASTContext(), *ast, parser_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(
262 user_type, is_lvalue, m_keep_result_in_memory, m_result_delegate, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000263
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264 ClangExpressionVariable *var = new ClangExpressionVariable(
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);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 m_found_entities.AddNewlyConstructedVariable(var);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000270
Sean Callananbc8ac342015-09-04 20:49:51 +0000271 var->EnableParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 ClangExpressionVariable::ParserVars *parser_vars =
274 var->GetParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000275
Sean Callanan3c495c12013-01-15 23:29:36 +0000276 parser_vars->m_named_decl = decl;
277 parser_vars->m_parser_type = parser_type;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 var->EnableJITVars(GetParserID());
280
281 ClangExpressionVariable::JITVars *jit_vars = var->GetJITVars(GetParserID());
282
283 jit_vars->m_offset = offset;
284
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000285 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 }
287
288 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
289 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
290 Target *target = exe_ctx.GetTargetPtr();
Konrad Kleine248a1302019-05-23 11:14:47 +0000291 if (target == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 return false;
293
294 ClangASTContext *context(target->GetScratchClangASTContext());
295
Sean Callanan68e44232017-09-28 20:20:25 +0000296 TypeFromUser user_type = DeportType(*context, *ast, parser_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297
298 if (!user_type.GetOpaqueQualType()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000299 LLDB_LOGF(log, "Persistent variable's type wasn't copied successfully");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300 return false;
301 }
302
303 if (!m_parser_vars->m_target_info.IsValid())
304 return false;
305
306 ClangExpressionVariable *var = llvm::cast<ClangExpressionVariable>(
307 m_parser_vars->m_persistent_vars
308 ->CreatePersistentVariable(
309 exe_ctx.GetBestExecutionContextScope(), name, user_type,
310 m_parser_vars->m_target_info.byte_order,
311 m_parser_vars->m_target_info.address_byte_size)
312 .get());
313
314 if (!var)
315 return false;
316
317 var->m_frozen_sp->SetHasCompleteType();
318
319 if (is_result)
320 var->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry;
321 else
322 var->m_flags |=
323 ClangExpressionVariable::EVKeepInTarget; // explicitly-declared
324 // persistent variables should
325 // persist
326
327 if (is_lvalue) {
328 var->m_flags |= ClangExpressionVariable::EVIsProgramReference;
329 } else {
330 var->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
331 var->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
332 }
333
334 if (m_keep_result_in_memory) {
335 var->m_flags |= ClangExpressionVariable::EVKeepInTarget;
336 }
337
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000338 LLDB_LOGF(log, "Created persistent variable with flags 0x%hx", var->m_flags);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339
340 var->EnableParserVars(GetParserID());
341
342 ClangExpressionVariable::ParserVars *parser_vars =
343 var->GetParserVars(GetParserID());
344
345 parser_vars->m_named_decl = decl;
346 parser_vars->m_parser_type = parser_type;
347
348 return true;
Sean Callanan2235f322010-08-11 03:57:18 +0000349}
350
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351bool ClangExpressionDeclMap::AddValueToStruct(const NamedDecl *decl,
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000352 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 llvm::Value *value, size_t size,
354 lldb::offset_t alignment) {
355 assert(m_struct_vars.get());
356 assert(m_parser_vars.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000357
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358 bool is_persistent_variable = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000359
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000361
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 m_struct_vars->m_struct_laid_out = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000363
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364 if (ClangExpressionVariable::FindVariableInList(m_struct_members, decl,
365 GetParserID()))
366 return true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000367
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368 ClangExpressionVariable *var(ClangExpressionVariable::FindVariableInList(
369 m_found_entities, decl, GetParserID()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000370
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 if (!var) {
372 var = ClangExpressionVariable::FindVariableInList(
373 *m_parser_vars->m_persistent_vars, decl, GetParserID());
374 is_persistent_variable = true;
375 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000376
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 if (!var)
378 return false;
379
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000380 LLDB_LOGF(log, "Adding value for (NamedDecl*)%p [%s - %s] to the structure",
381 static_cast<const void *>(decl), name.GetCString(),
382 var->GetName().GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383
384 // We know entity->m_parser_vars is valid because we used a parser variable
385 // to find it
386
387 ClangExpressionVariable::ParserVars *parser_vars =
388 llvm::cast<ClangExpressionVariable>(var)->GetParserVars(GetParserID());
389
390 parser_vars->m_llvm_value = value;
391
392 if (ClangExpressionVariable::JITVars *jit_vars =
393 llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID())) {
394 // We already laid this out; do not touch
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000395
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000396 LLDB_LOGF(log, "Already placed at 0x%llx",
397 (unsigned long long)jit_vars->m_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000399
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 llvm::cast<ClangExpressionVariable>(var)->EnableJITVars(GetParserID());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000401
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402 ClangExpressionVariable::JITVars *jit_vars =
403 llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID());
Sean Callanan3c495c12013-01-15 23:29:36 +0000404
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 jit_vars->m_alignment = alignment;
406 jit_vars->m_size = size;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000407
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408 m_struct_members.AddVariable(var->shared_from_this());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000409
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410 if (m_parser_vars->m_materializer) {
411 uint32_t offset = 0;
412
Zachary Turner97206d52017-05-12 04:51:55 +0000413 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414
415 if (is_persistent_variable) {
416 ExpressionVariableSP var_sp(var->shared_from_this());
417 offset = m_parser_vars->m_materializer->AddPersistentVariable(
418 var_sp, nullptr, err);
419 } else {
420 if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym)
421 offset = m_parser_vars->m_materializer->AddSymbol(*sym, err);
422 else if (const RegisterInfo *reg_info = var->GetRegisterInfo())
423 offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err);
424 else if (parser_vars->m_lldb_var)
425 offset = m_parser_vars->m_materializer->AddVariable(
426 parser_vars->m_lldb_var, err);
Sean Callanan1582ee62013-04-18 22:06:33 +0000427 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000428
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429 if (!err.Success())
430 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000431
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000432 LLDB_LOGF(log, "Placed at 0x%llx", (unsigned long long)offset);
Sean Callanan3c495c12013-01-15 23:29:36 +0000433
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 jit_vars->m_offset =
435 offset; // TODO DoStructLayout() should not change this.
436 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000437
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 return true;
439}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000440
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441bool ClangExpressionDeclMap::DoStructLayout() {
442 assert(m_struct_vars.get());
Sean Callanan3dd6a422013-04-11 21:16:36 +0000443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 if (m_struct_vars->m_struct_laid_out)
445 return true;
Sean Callanandf667652013-04-11 02:05:11 +0000446
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 if (!m_parser_vars->m_materializer)
448 return false;
449
450 m_struct_vars->m_struct_alignment =
451 m_parser_vars->m_materializer->GetStructAlignment();
452 m_struct_vars->m_struct_size =
453 m_parser_vars->m_materializer->GetStructByteSize();
454 m_struct_vars->m_struct_laid_out = true;
455 return true;
456}
457
458bool ClangExpressionDeclMap::GetStructInfo(uint32_t &num_elements, size_t &size,
459 lldb::offset_t &alignment) {
460 assert(m_struct_vars.get());
461
462 if (!m_struct_vars->m_struct_laid_out)
463 return false;
464
465 num_elements = m_struct_members.GetSize();
466 size = m_struct_vars->m_struct_size;
467 alignment = m_struct_vars->m_struct_alignment;
468
469 return true;
470}
471
472bool ClangExpressionDeclMap::GetStructElement(const NamedDecl *&decl,
473 llvm::Value *&value,
474 lldb::offset_t &offset,
475 ConstString &name,
476 uint32_t index) {
477 assert(m_struct_vars.get());
478
479 if (!m_struct_vars->m_struct_laid_out)
480 return false;
481
482 if (index >= m_struct_members.GetSize())
483 return false;
484
485 ExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index));
486
487 if (!member_sp)
488 return false;
489
490 ClangExpressionVariable::ParserVars *parser_vars =
491 llvm::cast<ClangExpressionVariable>(member_sp.get())
492 ->GetParserVars(GetParserID());
493 ClangExpressionVariable::JITVars *jit_vars =
494 llvm::cast<ClangExpressionVariable>(member_sp.get())
495 ->GetJITVars(GetParserID());
496
497 if (!parser_vars || !jit_vars || !member_sp->GetValueObject())
498 return false;
499
500 decl = parser_vars->m_named_decl;
501 value = parser_vars->m_llvm_value;
502 offset = jit_vars->m_offset;
503 name = member_sp->GetName();
504
505 return true;
506}
507
508bool ClangExpressionDeclMap::GetFunctionInfo(const NamedDecl *decl,
509 uint64_t &ptr) {
510 ClangExpressionVariable *entity(ClangExpressionVariable::FindVariableInList(
511 m_found_entities, decl, GetParserID()));
512
513 if (!entity)
514 return false;
515
Adrian Prantl05097242018-04-30 16:49:04 +0000516 // We know m_parser_vars is valid since we searched for the variable by its
517 // NamedDecl
Kate Stoneb9c1b512016-09-06 20:57:50 +0000518
519 ClangExpressionVariable::ParserVars *parser_vars =
520 entity->GetParserVars(GetParserID());
521
522 ptr = parser_vars->m_lldb_value.GetScalar().ULongLong();
523
524 return true;
525}
526
527addr_t ClangExpressionDeclMap::GetSymbolAddress(Target &target,
528 Process *process,
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000529 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000530 lldb::SymbolType symbol_type,
531 lldb_private::Module *module) {
532 SymbolContextList sc_list;
533
534 if (module)
535 module->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
536 else
537 target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list);
538
539 const uint32_t num_matches = sc_list.GetSize();
540 addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
541
542 for (uint32_t i = 0;
543 i < num_matches &&
544 (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS);
545 i++) {
546 SymbolContext sym_ctx;
547 sc_list.GetContextAtIndex(i, sym_ctx);
548
549 const Address sym_address = sym_ctx.symbol->GetAddress();
550
551 if (!sym_address.IsValid())
552 continue;
553
554 switch (sym_ctx.symbol->GetType()) {
555 case eSymbolTypeCode:
556 case eSymbolTypeTrampoline:
557 symbol_load_addr = sym_address.GetCallableLoadAddress(&target);
558 break;
559
560 case eSymbolTypeResolver:
561 symbol_load_addr = sym_address.GetCallableLoadAddress(&target, true);
562 break;
563
564 case eSymbolTypeReExported: {
565 ConstString reexport_name = sym_ctx.symbol->GetReExportedSymbolName();
566 if (reexport_name) {
567 ModuleSP reexport_module_sp;
568 ModuleSpec reexport_module_spec;
569 reexport_module_spec.GetPlatformFileSpec() =
570 sym_ctx.symbol->GetReExportedSymbolSharedLibrary();
571 if (reexport_module_spec.GetPlatformFileSpec()) {
572 reexport_module_sp =
573 target.GetImages().FindFirstModule(reexport_module_spec);
574 if (!reexport_module_sp) {
575 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
576 reexport_module_sp =
577 target.GetImages().FindFirstModule(reexport_module_spec);
578 }
Sean Callanandf667652013-04-11 02:05:11 +0000579 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580 symbol_load_addr = GetSymbolAddress(
581 target, process, sym_ctx.symbol->GetReExportedSymbolName(),
582 symbol_type, reexport_module_sp.get());
583 }
584 } break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000585
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586 case eSymbolTypeData:
587 case eSymbolTypeRuntime:
588 case eSymbolTypeVariable:
589 case eSymbolTypeLocal:
590 case eSymbolTypeParam:
591 case eSymbolTypeInvalid:
592 case eSymbolTypeAbsolute:
593 case eSymbolTypeException:
594 case eSymbolTypeSourceFile:
595 case eSymbolTypeHeaderFile:
596 case eSymbolTypeObjectFile:
597 case eSymbolTypeCommonBlock:
598 case eSymbolTypeBlock:
599 case eSymbolTypeVariableType:
600 case eSymbolTypeLineEntry:
601 case eSymbolTypeLineHeader:
602 case eSymbolTypeScopeBegin:
603 case eSymbolTypeScopeEnd:
604 case eSymbolTypeAdditional:
605 case eSymbolTypeCompiler:
606 case eSymbolTypeInstrumentation:
607 case eSymbolTypeUndefined:
608 case eSymbolTypeObjCClass:
609 case eSymbolTypeObjCMetaClass:
610 case eSymbolTypeObjCIVar:
611 symbol_load_addr = sym_address.GetLoadAddress(&target);
612 break;
Sean Callanandf667652013-04-11 02:05:11 +0000613 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000614 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000615
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process) {
Alex Langforde823bbe2019-06-10 20:53:23 +0000617 ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process);
Sean Callanan549c9f72010-07-13 21:41:46 +0000618
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619 if (runtime) {
620 symbol_load_addr = runtime->LookupRuntimeSymbol(name);
Greg Clayton084db102011-06-23 04:25:29 +0000621 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000622 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000623
Kate Stoneb9c1b512016-09-06 20:57:50 +0000624 return symbol_load_addr;
Sean Callananc3a16002011-01-17 23:42:46 +0000625}
626
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000627addr_t ClangExpressionDeclMap::GetSymbolAddress(ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628 lldb::SymbolType symbol_type) {
629 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000630
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 if (!m_parser_vars->m_exe_ctx.GetTargetPtr())
632 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000633
Kate Stoneb9c1b512016-09-06 20:57:50 +0000634 return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(),
635 m_parser_vars->m_exe_ctx.GetProcessPtr(), name,
636 symbol_type);
Sean Callanand9ca42a2011-05-08 02:21:26 +0000637}
638
Kate Stoneb9c1b512016-09-06 20:57:50 +0000639lldb::VariableSP ClangExpressionDeclMap::FindGlobalVariable(
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000640 Target &target, ModuleSP &module, ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000641 CompilerDeclContext *namespace_decl, TypeFromUser *type) {
642 VariableList vars;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000643
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644 if (module && namespace_decl)
Pavel Labath34cda142018-05-31 09:46:26 +0000645 module->FindGlobalVariables(name, namespace_decl, -1, vars);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000646 else
Pavel Labath34cda142018-05-31 09:46:26 +0000647 target.GetImages().FindGlobalVariables(name, -1, vars);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000648
Kate Stoneb9c1b512016-09-06 20:57:50 +0000649 if (vars.GetSize()) {
650 if (type) {
651 for (size_t i = 0; i < vars.GetSize(); ++i) {
652 VariableSP var_sp = vars.GetVariableAtIndex(i);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000653
Kate Stoneb9c1b512016-09-06 20:57:50 +0000654 if (ClangASTContext::AreTypesSame(
655 *type, var_sp->GetType()->GetFullCompilerType()))
656 return var_sp;
657 }
658 } else {
659 return vars.GetVariableAtIndex(0);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000660 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000662
Kate Stoneb9c1b512016-09-06 20:57:50 +0000663 return VariableSP();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000664}
665
Kate Stoneb9c1b512016-09-06 20:57:50 +0000666ClangASTContext *ClangExpressionDeclMap::GetClangASTContext() {
667 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
668 if (frame == nullptr)
669 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +0000670
Kate Stoneb9c1b512016-09-06 20:57:50 +0000671 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
672 lldb::eSymbolContextBlock);
673 if (sym_ctx.block == nullptr)
674 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +0000675
Kate Stoneb9c1b512016-09-06 20:57:50 +0000676 CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext();
677 if (!frame_decl_context)
678 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +0000679
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680 return llvm::dyn_cast_or_null<ClangASTContext>(
681 frame_decl_context.GetTypeSystem());
Siva Chandra03ff5c82016-02-05 19:10:04 +0000682}
683
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684// Interface for ClangASTSource
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000685
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686void ClangExpressionDeclMap::FindExternalVisibleDecls(
687 NameSearchContext &context) {
688 assert(m_ast_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000689
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690 ClangASTMetrics::RegisterVisibleQuery();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000691
Kate Stoneb9c1b512016-09-06 20:57:50 +0000692 const ConstString name(context.m_decl_name.getAsString().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000693
Kate Stoneb9c1b512016-09-06 20:57:50 +0000694 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000695
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696 if (GetImportInProgress()) {
697 if (log && log->GetVerbose())
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000698 LLDB_LOGF(log, "Ignoring a query during an import");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000699 return;
700 }
701
702 static unsigned int invocation_id = 0;
703 unsigned int current_id = invocation_id++;
704
705 if (log) {
706 if (!context.m_decl_context)
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000707 LLDB_LOGF(log,
708 "ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for "
709 "'%s' in a NULL DeclContext",
710 current_id, name.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000711 else if (const NamedDecl *context_named_decl =
712 dyn_cast<NamedDecl>(context.m_decl_context))
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000713 LLDB_LOGF(log,
714 "ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for "
715 "'%s' in '%s'",
716 current_id, name.GetCString(),
717 context_named_decl->getNameAsString().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000718 else
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000719 LLDB_LOGF(log,
720 "ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for "
721 "'%s' in a '%s'",
722 current_id, name.GetCString(),
723 context.m_decl_context->getDeclKindName());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724 }
725
726 if (const NamespaceDecl *namespace_context =
727 dyn_cast<NamespaceDecl>(context.m_decl_context)) {
728 if (namespace_context->getName().str() ==
729 std::string(g_lldb_local_vars_namespace_cstr)) {
730 CompilerDeclContext compiler_decl_ctx(
731 GetClangASTContext(), const_cast<void *>(static_cast<const void *>(
732 context.m_decl_context)));
733 FindExternalVisibleDecls(context, lldb::ModuleSP(), compiler_decl_ctx,
734 current_id);
735 return;
Sean Callanan6abfabf2010-11-19 20:20:02 +0000736 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000737
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738 ClangASTImporter::NamespaceMapSP namespace_map =
Sean Callanan68e44232017-09-28 20:20:25 +0000739 m_ast_importer_sp
740 ? m_ast_importer_sp->GetNamespaceMap(namespace_context)
741 : ClangASTImporter::NamespaceMapSP();
742
743 if (!namespace_map)
744 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745
746 if (log && log->GetVerbose())
747 log->Printf(" CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)",
748 current_id, static_cast<void *>(namespace_map.get()),
749 (int)namespace_map->size());
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +0000750
Kate Stoneb9c1b512016-09-06 20:57:50 +0000751 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(),
752 e = namespace_map->end();
753 i != e; ++i) {
754 if (log)
755 log->Printf(" CEDM::FEVD[%u] Searching namespace %s in module %s",
756 current_id, i->second.GetName().AsCString(),
757 i->first->GetFileSpec().GetFilename().GetCString());
758
759 FindExternalVisibleDecls(context, i->first, i->second, current_id);
760 }
761 } else if (isa<TranslationUnitDecl>(context.m_decl_context)) {
762 CompilerDeclContext namespace_decl;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000763
Sean Callanan503aa522011-10-12 00:12:34 +0000764 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765 log->Printf(" CEDM::FEVD[%u] Searching the root namespace", current_id);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000766
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl,
768 current_id);
769 }
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +0000770
Sean Callananc02a1c02017-04-24 23:14:04 +0000771 ClangASTSource::FindExternalVisibleDecls(context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000772}
773
Raphael Isemannc34478f2019-11-20 13:41:44 +0100774void ClangExpressionDeclMap::MaybeRegisterFunctionBody(
775 FunctionDecl *copied_function_decl) {
776 if (copied_function_decl->getBody() && m_parser_vars->m_code_gen) {
777 clang::DeclGroupRef decl_group_ref(copied_function_decl);
778 m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref);
779 }
780}
781
782void ClangExpressionDeclMap::SearchPersistenDecls(NameSearchContext &context,
783 const ConstString name,
784 unsigned int current_id) {
785 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
786 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
787 if (!target)
788 return;
789
790 ClangASTContext *scratch_clang_ast_context =
791 target->GetScratchClangASTContext();
792
793 if (!scratch_clang_ast_context)
794 return;
795
796 ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
797
798 if (!scratch_ast_context)
799 return;
800
801 NamedDecl *persistent_decl =
802 m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
803
804 if (!persistent_decl)
805 return;
806
807 Decl *parser_persistent_decl = CopyDecl(persistent_decl);
808
809 if (!parser_persistent_decl)
810 return;
811
812 NamedDecl *parser_named_decl = dyn_cast<NamedDecl>(parser_persistent_decl);
813
814 if (!parser_named_decl)
815 return;
816
817 if (clang::FunctionDecl *parser_function_decl =
818 llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl)) {
819 MaybeRegisterFunctionBody(parser_function_decl);
820 }
821
822 LLDB_LOGF(log, " CEDM::FEVD[%u] Found persistent decl %s", current_id,
823 name.GetCString());
824
825 context.AddNamedDecl(parser_named_decl);
826}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827void ClangExpressionDeclMap::FindExternalVisibleDecls(
828 NameSearchContext &context, lldb::ModuleSP module_sp,
829 CompilerDeclContext &namespace_decl, unsigned int current_id) {
830 assert(m_ast_context);
831
Kate Stoneb9c1b512016-09-06 20:57:50 +0000832 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
833
834 SymbolContextList sc_list;
835
836 const ConstString name(context.m_decl_name.getAsString().c_str());
Sean Callanan1b3c43b2017-09-26 17:25:34 +0000837 if (IgnoreName(name, false))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838 return;
839
Adrian Prantl05097242018-04-30 16:49:04 +0000840 // Only look for functions by name out in our symbols if the function doesn't
841 // start with our phony prefix of '$'
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
843 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
844 SymbolContext sym_ctx;
845 if (frame != nullptr)
846 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
847 lldb::eSymbolContextBlock);
848
849 // Try the persistent decls, which take precedence over all else.
Raphael Isemannc34478f2019-11-20 13:41:44 +0100850 if (!namespace_decl)
851 SearchPersistenDecls(context, name, current_id);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852
Sean Callanan1b3c43b2017-09-26 17:25:34 +0000853 if (name.GetCString()[0] == '$' && !namespace_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000854 static ConstString g_lldb_class_name("$__lldb_class");
855
856 if (name == g_lldb_class_name) {
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000857 if (m_ctx_obj) {
858 Status status;
859 lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status);
860 if (!ctx_obj_ptr || status.Fail())
861 return;
862
863 AddThisType(context, TypeFromUser(m_ctx_obj->GetCompilerType()),
864 current_id);
865
866 m_struct_vars->m_object_pointer_type =
867 TypeFromUser(ctx_obj_ptr->GetCompilerType());
868
869 return;
870 }
871
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872 // Clang is looking for the type of "this"
873
Konrad Kleine248a1302019-05-23 11:14:47 +0000874 if (frame == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000875 return;
876
877 // Find the block that defines the function represented by "sym_ctx"
878 Block *function_block = sym_ctx.GetFunctionBlock();
879
880 if (!function_block)
881 return;
882
883 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
884
885 if (!function_decl_ctx)
886 return;
887
888 clang::CXXMethodDecl *method_decl =
889 ClangASTContext::DeclContextGetAsCXXMethodDecl(function_decl_ctx);
890
891 if (method_decl) {
892 clang::CXXRecordDecl *class_decl = method_decl->getParent();
893
894 QualType class_qual_type(class_decl->getTypeForDecl(), 0);
895
896 TypeFromUser class_user_type(
897 class_qual_type.getAsOpaquePtr(),
898 ClangASTContext::GetASTContext(&class_decl->getASTContext()));
899
900 if (log) {
901 ASTDumper ast_dumper(class_qual_type);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000902 LLDB_LOGF(log, " CEDM::FEVD[%u] Adding type for $__lldb_class: %s",
903 current_id, ast_dumper.GetCString());
Siva Chandra03ff5c82016-02-05 19:10:04 +0000904 }
905
Kate Stoneb9c1b512016-09-06 20:57:50 +0000906 AddThisType(context, class_user_type, current_id);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000907
Kate Stoneb9c1b512016-09-06 20:57:50 +0000908 if (method_decl->isInstance()) {
909 // self is a pointer to the object
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000910
Kate Stoneb9c1b512016-09-06 20:57:50 +0000911 QualType class_pointer_type =
912 method_decl->getASTContext().getPointerType(class_qual_type);
913
914 TypeFromUser self_user_type(
915 class_pointer_type.getAsOpaquePtr(),
916 ClangASTContext::GetASTContext(&method_decl->getASTContext()));
917
918 m_struct_vars->m_object_pointer_type = self_user_type;
919 }
920 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000921 // This branch will get hit if we are executing code in the context of
922 // a function that claims to have an object pointer (through
923 // DW_AT_object_pointer?) but is not formally a method of the class.
924 // In that case, just look up the "this" variable in the current scope
925 // and use its type.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000926 // FIXME: This code is formally correct, but clang doesn't currently
927 // emit DW_AT_object_pointer
928 // for C++ so it hasn't actually been tested.
929
930 VariableList *vars = frame->GetVariableList(false);
931
932 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
933
934 if (this_var && this_var->IsInScope(frame) &&
935 this_var->LocationIsValidForFrame(frame)) {
936 Type *this_type = this_var->GetType();
937
938 if (!this_type)
Sean Callananb2269162011-10-21 22:18:07 +0000939 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000940
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941 TypeFromUser pointee_type =
942 this_type->GetForwardCompilerType().GetPointeeType();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000943
Kate Stoneb9c1b512016-09-06 20:57:50 +0000944 if (pointee_type.IsValid()) {
945 if (log) {
946 ASTDumper ast_dumper(pointee_type);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000947 LLDB_LOGF(log, " FEVD[%u] Adding type for $__lldb_class: %s",
948 current_id, ast_dumper.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000949 }
950
951 AddThisType(context, pointee_type, current_id);
952 TypeFromUser this_user_type(this_type->GetFullCompilerType());
953 m_struct_vars->m_object_pointer_type = this_user_type;
954 return;
955 }
Sean Callanan503aa522011-10-12 00:12:34 +0000956 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000957 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000958
Kate Stoneb9c1b512016-09-06 20:57:50 +0000959 return;
960 }
961
962 static ConstString g_lldb_objc_class_name("$__lldb_objc_class");
963 if (name == g_lldb_objc_class_name) {
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000964 if (m_ctx_obj) {
965 Status status;
966 lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status);
967 if (!ctx_obj_ptr || status.Fail())
968 return;
969
970 AddOneType(context, TypeFromUser(m_ctx_obj->GetCompilerType()),
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +0000971 current_id);
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000972
973 m_struct_vars->m_object_pointer_type =
974 TypeFromUser(ctx_obj_ptr->GetCompilerType());
975
976 return;
977 }
978
Kate Stoneb9c1b512016-09-06 20:57:50 +0000979 // Clang is looking for the type of "*self"
980
981 if (!frame)
982 return;
983
984 SymbolContext sym_ctx = frame->GetSymbolContext(
985 lldb::eSymbolContextFunction | lldb::eSymbolContextBlock);
986
987 // Find the block that defines the function represented by "sym_ctx"
988 Block *function_block = sym_ctx.GetFunctionBlock();
989
990 if (!function_block)
991 return;
992
993 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
994
995 if (!function_decl_ctx)
996 return;
997
998 clang::ObjCMethodDecl *method_decl =
999 ClangASTContext::DeclContextGetAsObjCMethodDecl(function_decl_ctx);
1000
1001 if (method_decl) {
1002 ObjCInterfaceDecl *self_interface = method_decl->getClassInterface();
1003
1004 if (!self_interface)
1005 return;
1006
1007 const clang::Type *interface_type = self_interface->getTypeForDecl();
1008
1009 if (!interface_type)
1010 return; // This is unlikely, but we have seen crashes where this
1011 // occurred
1012
1013 TypeFromUser class_user_type(
1014 QualType(interface_type, 0).getAsOpaquePtr(),
1015 ClangASTContext::GetASTContext(&method_decl->getASTContext()));
1016
1017 if (log) {
1018 ASTDumper ast_dumper(interface_type);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001019 LLDB_LOGF(log, " FEVD[%u] Adding type for $__lldb_objc_class: %s",
1020 current_id, ast_dumper.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021 }
1022
1023 AddOneType(context, class_user_type, current_id);
1024
1025 if (method_decl->isInstanceMethod()) {
1026 // self is a pointer to the object
1027
1028 QualType class_pointer_type =
1029 method_decl->getASTContext().getObjCObjectPointerType(
1030 QualType(interface_type, 0));
1031
1032 TypeFromUser self_user_type(
1033 class_pointer_type.getAsOpaquePtr(),
1034 ClangASTContext::GetASTContext(&method_decl->getASTContext()));
1035
1036 m_struct_vars->m_object_pointer_type = self_user_type;
1037 } else {
1038 // self is a Class pointer
1039 QualType class_type = method_decl->getASTContext().getObjCClassType();
1040
1041 TypeFromUser self_user_type(
1042 class_type.getAsOpaquePtr(),
1043 ClangASTContext::GetASTContext(&method_decl->getASTContext()));
1044
1045 m_struct_vars->m_object_pointer_type = self_user_type;
1046 }
1047
1048 return;
1049 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00001050 // This branch will get hit if we are executing code in the context of
1051 // a function that claims to have an object pointer (through
1052 // DW_AT_object_pointer?) but is not formally a method of the class.
1053 // In that case, just look up the "self" variable in the current scope
1054 // and use its type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001055
1056 VariableList *vars = frame->GetVariableList(false);
1057
1058 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
1059
1060 if (self_var && self_var->IsInScope(frame) &&
1061 self_var->LocationIsValidForFrame(frame)) {
1062 Type *self_type = self_var->GetType();
1063
1064 if (!self_type)
1065 return;
1066
1067 CompilerType self_clang_type = self_type->GetFullCompilerType();
1068
1069 if (ClangASTContext::IsObjCClassType(self_clang_type)) {
1070 return;
1071 } else if (ClangASTContext::IsObjCObjectPointerType(
1072 self_clang_type)) {
1073 self_clang_type = self_clang_type.GetPointeeType();
1074
1075 if (!self_clang_type)
1076 return;
1077
1078 if (log) {
1079 ASTDumper ast_dumper(self_type->GetFullCompilerType());
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001080 LLDB_LOGF(log,
1081 " FEVD[%u] Adding type for $__lldb_objc_class: %s",
1082 current_id, ast_dumper.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001083 }
1084
1085 TypeFromUser class_user_type(self_clang_type);
1086
1087 AddOneType(context, class_user_type, current_id);
1088
1089 TypeFromUser self_user_type(self_type->GetFullCompilerType());
1090
1091 m_struct_vars->m_object_pointer_type = self_user_type;
1092 return;
1093 }
1094 }
1095 }
1096
1097 return;
1098 }
1099
1100 if (name == ConstString(g_lldb_local_vars_namespace_cstr)) {
1101 CompilerDeclContext frame_decl_context =
1102 sym_ctx.block != nullptr ? sym_ctx.block->GetDeclContext()
1103 : CompilerDeclContext();
1104
1105 if (frame_decl_context) {
Raphael Isemann6640f2e2019-11-20 11:36:15 +01001106 ClangASTContext *frame_ast = llvm::dyn_cast_or_null<ClangASTContext>(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001107 frame_decl_context.GetTypeSystem());
1108
Raphael Isemann6640f2e2019-11-20 11:36:15 +01001109 ClangASTContext *map_ast =
1110 ClangASTContext::GetASTContext(m_ast_context);
1111 if (frame_ast && map_ast) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112 clang::NamespaceDecl *namespace_decl =
Raphael Isemann6640f2e2019-11-20 11:36:15 +01001113 map_ast->GetUniqueNamespaceDeclaration(name.GetCString(),
1114 nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001115 if (namespace_decl) {
1116 context.AddNamedDecl(namespace_decl);
1117 clang::DeclContext *clang_decl_ctx =
1118 clang::Decl::castToDeclContext(namespace_decl);
1119 clang_decl_ctx->setHasExternalVisibleStorage(true);
1120 context.m_found.local_vars_nsp = true;
1121 }
1122 }
1123 }
1124
1125 return;
1126 }
1127
1128 // any other $__lldb names should be weeded out now
Sean Callanan1b3c43b2017-09-26 17:25:34 +00001129 if (name.GetStringRef().startswith("$__lldb"))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130 return;
1131
1132 ExpressionVariableSP pvar_sp(
1133 m_parser_vars->m_persistent_vars->GetVariable(name));
1134
1135 if (pvar_sp) {
1136 AddOneVariable(context, pvar_sp, current_id);
1137 return;
1138 }
1139
1140 const char *reg_name(&name.GetCString()[1]);
1141
1142 if (m_parser_vars->m_exe_ctx.GetRegisterContext()) {
1143 const RegisterInfo *reg_info(
1144 m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(
1145 reg_name));
1146
1147 if (reg_info) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001148 LLDB_LOGF(log, " CEDM::FEVD[%u] Found register %s", current_id,
1149 reg_info->name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001150
Kate Stoneb9c1b512016-09-06 20:57:50 +00001151 AddOneRegister(context, reg_info, current_id);
1152 }
1153 }
1154 } else {
1155 ValueObjectSP valobj;
1156 VariableSP var;
1157
1158 bool local_var_lookup =
1159 !namespace_decl || (namespace_decl.GetName() ==
1160 ConstString(g_lldb_local_vars_namespace_cstr));
1161 if (frame && local_var_lookup) {
1162 CompilerDeclContext compiler_decl_context =
1163 sym_ctx.block != nullptr ? sym_ctx.block->GetDeclContext()
1164 : CompilerDeclContext();
1165
1166 if (compiler_decl_context) {
1167 // Make sure that the variables are parsed so that we have the
1168 // declarations.
1169 VariableListSP vars = frame->GetInScopeVariableList(true);
1170 for (size_t i = 0; i < vars->GetSize(); i++)
1171 vars->GetVariableAtIndex(i)->GetDecl();
1172
1173 // Search for declarations matching the name. Do not include imported
Adrian Prantl05097242018-04-30 16:49:04 +00001174 // decls in the search if we are looking for decls in the artificial
1175 // namespace $__lldb_local_vars.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001176 std::vector<CompilerDecl> found_decls =
1177 compiler_decl_context.FindDeclByName(name,
1178 namespace_decl.IsValid());
1179
1180 bool variable_found = false;
1181 for (CompilerDecl decl : found_decls) {
1182 for (size_t vi = 0, ve = vars->GetSize(); vi != ve; ++vi) {
1183 VariableSP candidate_var = vars->GetVariableAtIndex(vi);
1184 if (candidate_var->GetDecl() == decl) {
1185 var = candidate_var;
1186 break;
1187 }
1188 }
1189
Sean Callananc02a1c02017-04-24 23:14:04 +00001190 if (var && !variable_found) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001191 variable_found = true;
1192 valobj = ValueObjectVariable::Create(frame, var);
1193 AddOneVariable(context, var, valobj, current_id);
1194 context.m_found.variable = true;
1195 }
1196 }
1197 if (variable_found)
1198 return;
1199 }
1200 }
1201 if (target) {
Konrad Kleine248a1302019-05-23 11:14:47 +00001202 var = FindGlobalVariable(*target, module_sp, name, &namespace_decl,
1203 nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001204
1205 if (var) {
1206 valobj = ValueObjectVariable::Create(target, var);
1207 AddOneVariable(context, var, valobj, current_id);
1208 context.m_found.variable = true;
1209 return;
1210 }
Sean Callanan503aa522011-10-12 00:12:34 +00001211 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001212
Kate Stoneb9c1b512016-09-06 20:57:50 +00001213 std::vector<clang::NamedDecl *> decls_from_modules;
Sean Callanan503aa522011-10-12 00:12:34 +00001214
Kate Stoneb9c1b512016-09-06 20:57:50 +00001215 if (target) {
1216 if (ClangModulesDeclVendor *decl_vendor =
1217 target->GetClangModulesDeclVendor()) {
1218 decl_vendor->FindDecls(name, false, UINT32_MAX, decls_from_modules);
1219 }
1220 }
Sean Callananb3a36df2016-03-19 00:51:43 +00001221
Sean Callananc02a1c02017-04-24 23:14:04 +00001222 const bool include_inlines = false;
Adrian Prantl1ad655e2019-10-17 19:56:40 +00001223 sc_list.Clear();
Sean Callananc02a1c02017-04-24 23:14:04 +00001224 if (namespace_decl && module_sp) {
1225 const bool include_symbols = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001226
Sean Callananc02a1c02017-04-24 23:14:04 +00001227 module_sp->FindFunctions(name, &namespace_decl, eFunctionNameTypeBase,
Adrian Prantl1ad655e2019-10-17 19:56:40 +00001228 include_symbols, include_inlines, sc_list);
Sean Callananc02a1c02017-04-24 23:14:04 +00001229 } else if (target && !namespace_decl) {
1230 const bool include_symbols = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001231
Sean Callananc02a1c02017-04-24 23:14:04 +00001232 // TODO Fix FindFunctions so that it doesn't return
1233 // instance methods for eFunctionNameTypeBase.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001234
Sean Callananc02a1c02017-04-24 23:14:04 +00001235 target->GetImages().FindFunctions(name, eFunctionNameTypeFull,
1236 include_symbols, include_inlines,
Adrian Prantl1ad655e2019-10-17 19:56:40 +00001237 sc_list);
Sean Callananc02a1c02017-04-24 23:14:04 +00001238 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001239
Adrian Prantl05097242018-04-30 16:49:04 +00001240 // If we found more than one function, see if we can use the frame's decl
1241 // context to remove functions that are shadowed by other functions which
1242 // match in type but are nearer in scope.
Sean Callananc02a1c02017-04-24 23:14:04 +00001243 //
1244 // AddOneFunction will not add a function whose type has already been
Adrian Prantl05097242018-04-30 16:49:04 +00001245 // added, so if there's another function in the list with a matching type,
1246 // check to see if their decl context is a parent of the current frame's or
1247 // was imported via a and using statement, and pick the best match
1248 // according to lookup rules.
Sean Callananc02a1c02017-04-24 23:14:04 +00001249 if (sc_list.GetSize() > 1) {
1250 // Collect some info about our frame's context.
1251 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1252 SymbolContext frame_sym_ctx;
1253 if (frame != nullptr)
1254 frame_sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
1255 lldb::eSymbolContextBlock);
1256 CompilerDeclContext frame_decl_context =
1257 frame_sym_ctx.block != nullptr ? frame_sym_ctx.block->GetDeclContext()
1258 : CompilerDeclContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001259
Sean Callananc02a1c02017-04-24 23:14:04 +00001260 // We can't do this without a compiler decl context for our frame.
1261 if (frame_decl_context) {
1262 clang::DeclContext *frame_decl_ctx =
1263 (clang::DeclContext *)frame_decl_context.GetOpaqueDeclContext();
1264 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(
1265 frame_decl_context.GetTypeSystem());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001266
Sean Callananc02a1c02017-04-24 23:14:04 +00001267 // Structure to hold the info needed when comparing function
1268 // declarations.
1269 struct FuncDeclInfo {
1270 ConstString m_name;
1271 CompilerType m_copied_type;
1272 uint32_t m_decl_lvl;
1273 SymbolContext m_sym_ctx;
1274 };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001275
Adrian Prantl05097242018-04-30 16:49:04 +00001276 // First, symplify things by looping through the symbol contexts to
1277 // remove unwanted functions and separate out the functions we want to
1278 // compare and prune into a separate list. Cache the info needed about
1279 // the function declarations in a vector for efficiency.
Sean Callananc02a1c02017-04-24 23:14:04 +00001280 SymbolContextList sc_sym_list;
1281 uint32_t num_indices = sc_list.GetSize();
1282 std::vector<FuncDeclInfo> fdi_cache;
1283 fdi_cache.reserve(num_indices);
1284 for (uint32_t index = 0; index < num_indices; ++index) {
1285 FuncDeclInfo fdi;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286 SymbolContext sym_ctx;
1287 sc_list.GetContextAtIndex(index, sym_ctx);
1288
Adrian Prantl05097242018-04-30 16:49:04 +00001289 // We don't know enough about symbols to compare them, but we should
1290 // keep them in the list.
Sean Callananc02a1c02017-04-24 23:14:04 +00001291 Function *function = sym_ctx.function;
1292 if (!function) {
1293 sc_sym_list.Append(sym_ctx);
1294 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001295 }
Sean Callananc02a1c02017-04-24 23:14:04 +00001296 // Filter out functions without declaration contexts, as well as
Adrian Prantl05097242018-04-30 16:49:04 +00001297 // class/instance methods, since they'll be skipped in the code that
1298 // follows anyway.
Sean Callananc02a1c02017-04-24 23:14:04 +00001299 CompilerDeclContext func_decl_context = function->GetDeclContext();
1300 if (!func_decl_context ||
1301 func_decl_context.IsClassMethod(nullptr, nullptr, nullptr))
1302 continue;
1303 // We can only prune functions for which we can copy the type.
1304 CompilerType func_clang_type =
1305 function->GetType()->GetFullCompilerType();
1306 CompilerType copied_func_type = GuardedCopyType(func_clang_type);
1307 if (!copied_func_type) {
1308 sc_sym_list.Append(sym_ctx);
1309 continue;
1310 }
1311
1312 fdi.m_sym_ctx = sym_ctx;
1313 fdi.m_name = function->GetName();
1314 fdi.m_copied_type = copied_func_type;
1315 fdi.m_decl_lvl = LLDB_INVALID_DECL_LEVEL;
1316 if (fdi.m_copied_type && func_decl_context) {
Adrian Prantl05097242018-04-30 16:49:04 +00001317 // Call CountDeclLevels to get the number of parent scopes we have
1318 // to look through before we find the function declaration. When
1319 // comparing functions of the same type, the one with a lower count
1320 // will be closer to us in the lookup scope and shadows the other.
Sean Callananc02a1c02017-04-24 23:14:04 +00001321 clang::DeclContext *func_decl_ctx =
1322 (clang::DeclContext *)func_decl_context.GetOpaqueDeclContext();
1323 fdi.m_decl_lvl = ast->CountDeclLevels(
1324 frame_decl_ctx, func_decl_ctx, &fdi.m_name, &fdi.m_copied_type);
1325 }
1326 fdi_cache.emplace_back(fdi);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001327 }
1328
Sean Callananc02a1c02017-04-24 23:14:04 +00001329 // Loop through the functions in our cache looking for matching types,
1330 // then compare their scope levels to see which is closer.
1331 std::multimap<CompilerType, const FuncDeclInfo *> matches;
1332 for (const FuncDeclInfo &fdi : fdi_cache) {
1333 const CompilerType t = fdi.m_copied_type;
1334 auto q = matches.find(t);
1335 if (q != matches.end()) {
1336 if (q->second->m_decl_lvl > fdi.m_decl_lvl)
1337 // This function is closer; remove the old set.
1338 matches.erase(t);
1339 else if (q->second->m_decl_lvl < fdi.m_decl_lvl)
1340 // The functions in our set are closer - skip this one.
1341 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001342 }
Sean Callananc02a1c02017-04-24 23:14:04 +00001343 matches.insert(std::make_pair(t, &fdi));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001344 }
1345
Sean Callananc02a1c02017-04-24 23:14:04 +00001346 // Loop through our matches and add their symbol contexts to our list.
1347 SymbolContextList sc_func_list;
1348 for (const auto &q : matches)
1349 sc_func_list.Append(q.second->m_sym_ctx);
1350
1351 // Rejoin the lists with the functions in front.
1352 sc_list = sc_func_list;
1353 sc_list.Append(sc_sym_list);
1354 }
1355 }
1356
1357 if (sc_list.GetSize()) {
Konrad Kleine248a1302019-05-23 11:14:47 +00001358 Symbol *extern_symbol = nullptr;
1359 Symbol *non_extern_symbol = nullptr;
Sean Callananc02a1c02017-04-24 23:14:04 +00001360
1361 for (uint32_t index = 0, num_indices = sc_list.GetSize();
1362 index < num_indices; ++index) {
1363 SymbolContext sym_ctx;
1364 sc_list.GetContextAtIndex(index, sym_ctx);
1365
1366 if (sym_ctx.function) {
1367 CompilerDeclContext decl_ctx = sym_ctx.function->GetDeclContext();
1368
1369 if (!decl_ctx)
1370 continue;
1371
1372 // Filter out class/instance methods.
1373 if (decl_ctx.IsClassMethod(nullptr, nullptr, nullptr))
1374 continue;
1375
Konrad Kleine248a1302019-05-23 11:14:47 +00001376 AddOneFunction(context, sym_ctx.function, nullptr, current_id);
Sean Callananc02a1c02017-04-24 23:14:04 +00001377 context.m_found.function_with_type_info = true;
1378 context.m_found.function = true;
1379 } else if (sym_ctx.symbol) {
1380 if (sym_ctx.symbol->GetType() == eSymbolTypeReExported && target) {
1381 sym_ctx.symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target);
Konrad Kleine248a1302019-05-23 11:14:47 +00001382 if (sym_ctx.symbol == nullptr)
Sean Callananc02a1c02017-04-24 23:14:04 +00001383 continue;
1384 }
1385
1386 if (sym_ctx.symbol->IsExternal())
1387 extern_symbol = sym_ctx.symbol;
1388 else
1389 non_extern_symbol = sym_ctx.symbol;
1390 }
1391 }
1392
1393 if (!context.m_found.function_with_type_info) {
1394 for (clang::NamedDecl *decl : decls_from_modules) {
1395 if (llvm::isa<clang::FunctionDecl>(decl)) {
1396 clang::NamedDecl *copied_decl =
Sean Callanan68e44232017-09-28 20:20:25 +00001397 llvm::cast_or_null<FunctionDecl>(CopyDecl(decl));
Sean Callananc02a1c02017-04-24 23:14:04 +00001398 if (copied_decl) {
1399 context.AddNamedDecl(copied_decl);
1400 context.m_found.function_with_type_info = true;
1401 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001402 }
1403 }
1404 }
1405
1406 if (!context.m_found.function_with_type_info) {
Sean Callananc02a1c02017-04-24 23:14:04 +00001407 if (extern_symbol) {
Konrad Kleine248a1302019-05-23 11:14:47 +00001408 AddOneFunction(context, nullptr, extern_symbol, current_id);
Sean Callananc02a1c02017-04-24 23:14:04 +00001409 context.m_found.function = true;
1410 } else if (non_extern_symbol) {
Konrad Kleine248a1302019-05-23 11:14:47 +00001411 AddOneFunction(context, nullptr, non_extern_symbol, current_id);
Sean Callananc02a1c02017-04-24 23:14:04 +00001412 context.m_found.function = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001413 }
1414 }
Sean Callananb3a36df2016-03-19 00:51:43 +00001415 }
Sean Callananc02a1c02017-04-24 23:14:04 +00001416
1417 if (!context.m_found.function_with_type_info) {
1418 // Try the modules next.
1419
1420 do {
1421 if (ClangModulesDeclVendor *modules_decl_vendor =
1422 m_target->GetClangModulesDeclVendor()) {
1423 bool append = false;
1424 uint32_t max_matches = 1;
1425 std::vector<clang::NamedDecl *> decls;
1426
1427 if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls))
1428 break;
1429
1430 clang::NamedDecl *const decl_from_modules = decls[0];
1431
1432 if (llvm::isa<clang::FunctionDecl>(decl_from_modules)) {
1433 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001434 LLDB_LOGF(log,
1435 " CAS::FEVD[%u] Matching function found for "
1436 "\"%s\" in the modules",
1437 current_id, name.GetCString());
Sean Callananc02a1c02017-04-24 23:14:04 +00001438 }
1439
Sean Callanan68e44232017-09-28 20:20:25 +00001440 clang::Decl *copied_decl = CopyDecl(decl_from_modules);
Sean Callananc02a1c02017-04-24 23:14:04 +00001441 clang::FunctionDecl *copied_function_decl =
1442 copied_decl ? dyn_cast<clang::FunctionDecl>(copied_decl)
1443 : nullptr;
1444
1445 if (!copied_function_decl) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001446 LLDB_LOGF(log,
1447 " CAS::FEVD[%u] - Couldn't export a function "
1448 "declaration from the modules",
1449 current_id);
Sean Callananc02a1c02017-04-24 23:14:04 +00001450
1451 break;
1452 }
1453
1454 MaybeRegisterFunctionBody(copied_function_decl);
1455
1456 context.AddNamedDecl(copied_function_decl);
1457
1458 context.m_found.function_with_type_info = true;
1459 context.m_found.function = true;
1460 } else if (llvm::isa<clang::VarDecl>(decl_from_modules)) {
1461 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001462 LLDB_LOGF(log,
1463 " CAS::FEVD[%u] Matching variable found for "
1464 "\"%s\" in the modules",
1465 current_id, name.GetCString());
Sean Callananc02a1c02017-04-24 23:14:04 +00001466 }
1467
Sean Callanan68e44232017-09-28 20:20:25 +00001468 clang::Decl *copied_decl = CopyDecl(decl_from_modules);
Sean Callananc02a1c02017-04-24 23:14:04 +00001469 clang::VarDecl *copied_var_decl =
1470 copied_decl ? dyn_cast_or_null<clang::VarDecl>(copied_decl)
1471 : nullptr;
1472
1473 if (!copied_var_decl) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001474 LLDB_LOGF(log,
1475 " CAS::FEVD[%u] - Couldn't export a variable "
1476 "declaration from the modules",
1477 current_id);
Sean Callananc02a1c02017-04-24 23:14:04 +00001478
1479 break;
1480 }
1481
1482 context.AddNamedDecl(copied_var_decl);
1483
1484 context.m_found.variable = true;
1485 }
1486 }
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00001487 } while (false);
Sean Callananc02a1c02017-04-24 23:14:04 +00001488 }
1489
1490 if (target && !context.m_found.variable && !namespace_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001491 // We couldn't find a non-symbol variable for this. Now we'll hunt for a
1492 // generic data symbol, and -- if it is found -- treat it as a variable.
Sean Callanan9c99faa2017-05-16 23:46:13 +00001493 Status error;
Stephane Sezer30fa7e22017-10-24 22:56:05 +00001494
Sean Callanan9c99faa2017-05-16 23:46:13 +00001495 const Symbol *data_symbol =
1496 m_parser_vars->m_sym_ctx.FindBestGlobalDataSymbol(name, error);
Stephane Sezer30fa7e22017-10-24 22:56:05 +00001497
Sean Callanan9c99faa2017-05-16 23:46:13 +00001498 if (!error.Success()) {
1499 const unsigned diag_id =
1500 m_ast_context->getDiagnostics().getCustomDiagID(
1501 clang::DiagnosticsEngine::Level::Error, "%0");
1502 m_ast_context->getDiagnostics().Report(diag_id) << error.AsCString();
1503 }
Stephane Sezer30fa7e22017-10-24 22:56:05 +00001504
Sean Callananc02a1c02017-04-24 23:14:04 +00001505 if (data_symbol) {
1506 std::string warning("got name from symbols: ");
1507 warning.append(name.AsCString());
1508 const unsigned diag_id =
1509 m_ast_context->getDiagnostics().getCustomDiagID(
1510 clang::DiagnosticsEngine::Level::Warning, "%0");
1511 m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str();
1512 AddOneGenericVariable(context, *data_symbol, current_id);
1513 context.m_found.variable = true;
1514 }
1515 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001516 }
Sean Callanan6b4067c2010-07-17 00:43:37 +00001517}
Greg Claytona2721472011-06-25 00:44:06 +00001518
Kate Stoneb9c1b512016-09-06 20:57:50 +00001519bool ClangExpressionDeclMap::GetVariableValue(VariableSP &var,
1520 lldb_private::Value &var_location,
1521 TypeFromUser *user_type,
1522 TypeFromParser *parser_type) {
1523 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001524
Kate Stoneb9c1b512016-09-06 20:57:50 +00001525 Type *var_type = var->GetType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001526
Kate Stoneb9c1b512016-09-06 20:57:50 +00001527 if (!var_type) {
Sean Callananea22d422010-07-16 00:09:46 +00001528 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001529 log->PutCString("Skipped a definition because it has no type");
1530 return false;
1531 }
1532
1533 CompilerType var_clang_type = var_type->GetFullCompilerType();
1534
1535 if (!var_clang_type) {
1536 if (log)
1537 log->PutCString("Skipped a definition because it has no Clang type");
1538 return false;
1539 }
1540
1541 ClangASTContext *clang_ast = llvm::dyn_cast_or_null<ClangASTContext>(
1542 var_type->GetForwardCompilerType().GetTypeSystem());
1543
1544 if (!clang_ast) {
1545 if (log)
1546 log->PutCString("Skipped a definition because it has no Clang AST");
1547 return false;
1548 }
1549
1550 ASTContext *ast = clang_ast->getASTContext();
1551
1552 if (!ast) {
1553 if (log)
1554 log->PutCString(
1555 "There is no AST context for the current execution context");
1556 return false;
1557 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001558
1559 DWARFExpression &var_location_expr = var->LocationExpression();
1560
1561 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Zachary Turner97206d52017-05-12 04:51:55 +00001562 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001563
1564 if (var->GetLocationIsConstantValueData()) {
1565 DataExtractor const_value_extractor;
1566
1567 if (var_location_expr.GetExpressionData(const_value_extractor)) {
1568 var_location = Value(const_value_extractor.GetDataStart(),
1569 const_value_extractor.GetByteSize());
1570 var_location.SetValueType(Value::eValueTypeHostAddress);
1571 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001572 LLDB_LOGF(log, "Error evaluating constant variable: %s", err.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001573 return false;
Greg Clayton7b462cc2010-10-15 22:48:33 +00001574 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001575 }
1576
1577 CompilerType type_to_use = GuardedCopyType(var_clang_type);
1578
1579 if (!type_to_use) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001580 LLDB_LOGF(log,
1581 "Couldn't copy a variable's type into the parser's AST context");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001582
1583 return false;
1584 }
1585
1586 if (parser_type)
1587 *parser_type = TypeFromParser(type_to_use);
1588
1589 if (var_location.GetContextType() == Value::eContextTypeInvalid)
1590 var_location.SetCompilerType(type_to_use);
1591
1592 if (var_location.GetValueType() == Value::eValueTypeFileAddress) {
1593 SymbolContext var_sc;
1594 var->CalculateSymbolContext(&var_sc);
1595
1596 if (!var_sc.module_sp)
1597 return false;
1598
1599 Address so_addr(var_location.GetScalar().ULongLong(),
1600 var_sc.module_sp->GetSectionList());
1601
1602 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
1603
1604 if (load_addr != LLDB_INVALID_ADDRESS) {
1605 var_location.GetScalar() = load_addr;
1606 var_location.SetValueType(Value::eValueTypeLoadAddress);
1607 }
1608 }
1609
1610 if (user_type)
1611 *user_type = TypeFromUser(var_clang_type);
1612
1613 return true;
Sean Callanan468574b2010-06-22 23:46:24 +00001614}
1615
Kate Stoneb9c1b512016-09-06 20:57:50 +00001616void ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
1617 VariableSP var,
1618 ValueObjectSP valobj,
1619 unsigned int current_id) {
1620 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001621
Kate Stoneb9c1b512016-09-06 20:57:50 +00001622 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001623
Kate Stoneb9c1b512016-09-06 20:57:50 +00001624 TypeFromUser ut;
1625 TypeFromParser pt;
1626 Value var_location;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001627
Kate Stoneb9c1b512016-09-06 20:57:50 +00001628 if (!GetVariableValue(var, var_location, &ut, &pt))
1629 return;
1630
1631 clang::QualType parser_opaque_type =
1632 QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
1633
1634 if (parser_opaque_type.isNull())
1635 return;
1636
1637 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr()) {
1638 if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1639 CompleteType(tag_type->getDecl());
1640 if (const ObjCObjectPointerType *objc_object_ptr_type =
1641 dyn_cast<ObjCObjectPointerType>(parser_type))
1642 CompleteType(objc_object_ptr_type->getInterfaceDecl());
1643 }
1644
1645 bool is_reference = pt.IsReferenceType();
1646
Konrad Kleine248a1302019-05-23 11:14:47 +00001647 NamedDecl *var_decl = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001648 if (is_reference)
1649 var_decl = context.AddVarDecl(pt);
1650 else
1651 var_decl = context.AddVarDecl(pt.GetLValueReferenceType());
1652
1653 std::string decl_name(context.m_decl_name.getAsString());
1654 ConstString entity_name(decl_name.c_str());
1655 ClangExpressionVariable *entity(new ClangExpressionVariable(valobj));
1656 m_found_entities.AddNewlyConstructedVariable(entity);
1657
1658 assert(entity);
1659 entity->EnableParserVars(GetParserID());
1660 ClangExpressionVariable::ParserVars *parser_vars =
1661 entity->GetParserVars(GetParserID());
1662 parser_vars->m_parser_type = pt;
1663 parser_vars->m_named_decl = var_decl;
Konrad Kleine248a1302019-05-23 11:14:47 +00001664 parser_vars->m_llvm_value = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001665 parser_vars->m_lldb_value = var_location;
1666 parser_vars->m_lldb_var = var;
1667
1668 if (is_reference)
1669 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference;
1670
1671 if (log) {
1672 ASTDumper orig_dumper(ut.GetOpaqueQualType());
1673 ASTDumper ast_dumper(var_decl);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001674 LLDB_LOGF(log,
1675 " CEDM::FEVD[%u] Found variable %s, returned %s (original %s)",
1676 current_id, decl_name.c_str(), ast_dumper.GetCString(),
1677 orig_dumper.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001678 }
1679}
1680
1681void ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
1682 ExpressionVariableSP &pvar_sp,
1683 unsigned int current_id) {
1684 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1685
1686 TypeFromUser user_type(
1687 llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetTypeFromUser());
1688
1689 TypeFromParser parser_type(GuardedCopyType(user_type));
1690
1691 if (!parser_type.GetOpaqueQualType()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001692 LLDB_LOGF(log, " CEDM::FEVD[%u] Couldn't import type for pvar %s",
1693 current_id, pvar_sp->GetName().GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001694 return;
1695 }
1696
1697 NamedDecl *var_decl =
1698 context.AddVarDecl(parser_type.GetLValueReferenceType());
1699
1700 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1701 ->EnableParserVars(GetParserID());
1702 ClangExpressionVariable::ParserVars *parser_vars =
1703 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1704 ->GetParserVars(GetParserID());
1705 parser_vars->m_parser_type = parser_type;
1706 parser_vars->m_named_decl = var_decl;
Konrad Kleine248a1302019-05-23 11:14:47 +00001707 parser_vars->m_llvm_value = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001708 parser_vars->m_lldb_value.Clear();
1709
1710 if (log) {
1711 ASTDumper ast_dumper(var_decl);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001712 LLDB_LOGF(log, " CEDM::FEVD[%u] Added pvar %s, returned %s", current_id,
1713 pvar_sp->GetName().GetCString(), ast_dumper.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001714 }
1715}
1716
1717void ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context,
1718 const Symbol &symbol,
1719 unsigned int current_id) {
1720 assert(m_parser_vars.get());
1721
1722 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1723
1724 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1725
Konrad Kleine248a1302019-05-23 11:14:47 +00001726 if (target == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001727 return;
1728
Raphael Isemannc502bae2019-11-20 12:40:08 +01001729 ClangASTContext *scratch_ast_context = target->GetScratchClangASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001730
Raphael Isemannc502bae2019-11-20 12:40:08 +01001731 TypeFromUser user_type(scratch_ast_context->GetBasicType(eBasicTypeVoid)
1732 .GetPointerType()
1733 .GetLValueReferenceType());
1734 ClangASTContext *own_context = ClangASTContext::GetASTContext(m_ast_context);
1735 TypeFromParser parser_type(own_context->GetBasicType(eBasicTypeVoid)
1736 .GetPointerType()
1737 .GetLValueReferenceType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001738 NamedDecl *var_decl = context.AddVarDecl(parser_type);
1739
1740 std::string decl_name(context.m_decl_name.getAsString());
1741 ConstString entity_name(decl_name.c_str());
1742 ClangExpressionVariable *entity(new ClangExpressionVariable(
1743 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), entity_name,
1744 user_type, m_parser_vars->m_target_info.byte_order,
1745 m_parser_vars->m_target_info.address_byte_size));
1746 m_found_entities.AddNewlyConstructedVariable(entity);
1747
1748 entity->EnableParserVars(GetParserID());
1749 ClangExpressionVariable::ParserVars *parser_vars =
1750 entity->GetParserVars(GetParserID());
1751
1752 const Address symbol_address = symbol.GetAddress();
1753 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target);
1754
1755 // parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType,
1756 // user_type.GetOpaqueQualType());
1757 parser_vars->m_lldb_value.SetCompilerType(user_type);
1758 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr;
1759 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1760
1761 parser_vars->m_parser_type = parser_type;
1762 parser_vars->m_named_decl = var_decl;
Konrad Kleine248a1302019-05-23 11:14:47 +00001763 parser_vars->m_llvm_value = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001764 parser_vars->m_lldb_sym = &symbol;
1765
1766 if (log) {
1767 ASTDumper ast_dumper(var_decl);
1768
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001769 LLDB_LOGF(log, " CEDM::FEVD[%u] Found variable %s, returned %s",
1770 current_id, decl_name.c_str(), ast_dumper.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001771 }
1772}
1773
Kate Stoneb9c1b512016-09-06 20:57:50 +00001774void ClangExpressionDeclMap::AddOneRegister(NameSearchContext &context,
1775 const RegisterInfo *reg_info,
1776 unsigned int current_id) {
1777 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001778
Kate Stoneb9c1b512016-09-06 20:57:50 +00001779 CompilerType clang_type =
1780 ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1781 m_ast_context, reg_info->encoding, reg_info->byte_size * 8);
Sean Callanan7736a202016-04-29 18:09:03 +00001782
Kate Stoneb9c1b512016-09-06 20:57:50 +00001783 if (!clang_type) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001784 LLDB_LOGF(log, " Tried to add a type for %s, but couldn't get one",
1785 context.m_decl_name.getAsString().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001786 return;
1787 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001788
Kate Stoneb9c1b512016-09-06 20:57:50 +00001789 TypeFromParser parser_clang_type(clang_type);
Sean Callananfa4fab72013-02-01 06:55:48 +00001790
Kate Stoneb9c1b512016-09-06 20:57:50 +00001791 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001792
Kate Stoneb9c1b512016-09-06 20:57:50 +00001793 ClangExpressionVariable *entity(new ClangExpressionVariable(
1794 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1795 m_parser_vars->m_target_info.byte_order,
1796 m_parser_vars->m_target_info.address_byte_size));
1797 m_found_entities.AddNewlyConstructedVariable(entity);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001798
Kate Stoneb9c1b512016-09-06 20:57:50 +00001799 std::string decl_name(context.m_decl_name.getAsString());
1800 entity->SetName(ConstString(decl_name.c_str()));
1801 entity->SetRegisterInfo(reg_info);
1802 entity->EnableParserVars(GetParserID());
1803 ClangExpressionVariable::ParserVars *parser_vars =
1804 entity->GetParserVars(GetParserID());
1805 parser_vars->m_parser_type = parser_clang_type;
1806 parser_vars->m_named_decl = var_decl;
Konrad Kleine248a1302019-05-23 11:14:47 +00001807 parser_vars->m_llvm_value = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001808 parser_vars->m_lldb_value.Clear();
1809 entity->m_flags |= ClangExpressionVariable::EVBareRegister;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001810
Kate Stoneb9c1b512016-09-06 20:57:50 +00001811 if (log) {
1812 ASTDumper ast_dumper(var_decl);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001813 LLDB_LOGF(log, " CEDM::FEVD[%d] Added register %s, returned %s",
1814 current_id, context.m_decl_name.getAsString().c_str(),
1815 ast_dumper.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001816 }
1817}
1818
1819void ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context,
1820 Function *function, Symbol *symbol,
1821 unsigned int current_id) {
1822 assert(m_parser_vars.get());
1823
1824 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1825
Konrad Kleine248a1302019-05-23 11:14:47 +00001826 NamedDecl *function_decl = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001827 Address fun_address;
1828 CompilerType function_clang_type;
1829
1830 bool is_indirect_function = false;
1831
1832 if (function) {
1833 Type *function_type = function->GetType();
1834
Luke Drummondf5bb1d62016-12-19 17:22:44 +00001835 const auto lang = function->GetCompileUnit()->GetLanguage();
1836 const auto name = function->GetMangled().GetMangledName().AsCString();
1837 const bool extern_c = (Language::LanguageIsC(lang) &&
1838 !CPlusPlusLanguage::IsCPPMangledName(name)) ||
1839 (Language::LanguageIsObjC(lang) &&
1840 !Language::LanguageIsCPlusPlus(lang));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001841
1842 if (!extern_c) {
1843 TypeSystem *type_system = function->GetDeclContext().GetTypeSystem();
Davide Italianofe34df52017-09-30 21:16:56 +00001844 if (llvm::isa<ClangASTContext>(type_system)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001845 clang::DeclContext *src_decl_context =
1846 (clang::DeclContext *)function->GetDeclContext()
1847 .GetOpaqueDeclContext();
1848 clang::FunctionDecl *src_function_decl =
1849 llvm::dyn_cast_or_null<clang::FunctionDecl>(src_decl_context);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001850 if (src_function_decl &&
1851 src_function_decl->getTemplateSpecializationInfo()) {
1852 clang::FunctionTemplateDecl *function_template =
1853 src_function_decl->getTemplateSpecializationInfo()->getTemplate();
1854 clang::FunctionTemplateDecl *copied_function_template =
1855 llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(
Sean Callanan68e44232017-09-28 20:20:25 +00001856 CopyDecl(function_template));
Sean Callanan09e91ac2017-05-11 22:08:05 +00001857 if (copied_function_template) {
1858 if (log) {
1859 ASTDumper ast_dumper((clang::Decl *)copied_function_template);
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +00001860
Sean Callanan09e91ac2017-05-11 22:08:05 +00001861 StreamString ss;
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +00001862
Sean Callanan09e91ac2017-05-11 22:08:05 +00001863 function->DumpSymbolContext(&ss);
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +00001864
Sean Callanan09e91ac2017-05-11 22:08:05 +00001865 log->Printf(" CEDM::FEVD[%u] Imported decl for function template"
1866 " %s (description %s), returned %s",
1867 current_id,
1868 copied_function_template->getNameAsString().c_str(),
1869 ss.GetData(), ast_dumper.GetCString());
1870 }
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +00001871
Sean Callanan09e91ac2017-05-11 22:08:05 +00001872 context.AddNamedDecl(copied_function_template);
1873 }
1874 } else if (src_function_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001875 if (clang::FunctionDecl *copied_function_decl =
1876 llvm::dyn_cast_or_null<clang::FunctionDecl>(
Jonas Devlieghere50a3f0e2019-07-23 21:14:23 +00001877 CopyDecl(src_function_decl))) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001878 if (log) {
1879 ASTDumper ast_dumper((clang::Decl *)copied_function_decl);
1880
1881 StreamString ss;
1882
1883 function->DumpSymbolContext(&ss);
1884
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001885 LLDB_LOGF(log,
1886 " CEDM::FEVD[%u] Imported decl for function %s "
1887 "(description %s), returned %s",
1888 current_id,
1889 copied_function_decl->getNameAsString().c_str(),
1890 ss.GetData(), ast_dumper.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001891 }
1892
1893 context.AddNamedDecl(copied_function_decl);
1894 return;
1895 } else {
1896 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001897 LLDB_LOGF(log, " Failed to import the function decl for '%s'",
1898 src_function_decl->getName().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001899 }
1900 }
Sean Callanan7736a202016-04-29 18:09:03 +00001901 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001902 }
Sean Callananfc55f5d2010-09-21 00:44:12 +00001903 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001904
Kate Stoneb9c1b512016-09-06 20:57:50 +00001905 if (!function_type) {
1906 if (log)
1907 log->PutCString(" Skipped a function because it has no type");
1908 return;
1909 }
1910
1911 function_clang_type = function_type->GetFullCompilerType();
1912
1913 if (!function_clang_type) {
1914 if (log)
1915 log->PutCString(" Skipped a function because it has no Clang type");
1916 return;
1917 }
1918
1919 fun_address = function->GetAddressRange().GetBaseAddress();
1920
1921 CompilerType copied_function_type = GuardedCopyType(function_clang_type);
1922 if (copied_function_type) {
1923 function_decl = context.AddFunDecl(copied_function_type, extern_c);
1924
1925 if (!function_decl) {
1926 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001927 LLDB_LOGF(
1928 log,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001929 " Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}",
1930 function_type->GetName().GetCString(), function_type->GetID());
1931 }
1932
Sean Callanan3b436d22015-10-23 21:45:02 +00001933 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001934 }
1935 } else {
1936 // We failed to copy the type we found
1937 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001938 LLDB_LOGF(log,
1939 " Failed to import the function type '%s' {0x%8.8" PRIx64
1940 "} into the expression parser AST contenxt",
1941 function_type->GetName().GetCString(),
1942 function_type->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001943 }
1944
1945 return;
1946 }
1947 } else if (symbol) {
1948 fun_address = symbol->GetAddress();
1949 function_decl = context.AddGenericFunDecl();
1950 is_indirect_function = symbol->IsIndirect();
1951 } else {
1952 if (log)
1953 log->PutCString(" AddOneFunction called with no function and no symbol");
1954 return;
1955 }
1956
1957 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1958
1959 lldb::addr_t load_addr =
1960 fun_address.GetCallableLoadAddress(target, is_indirect_function);
1961
1962 ClangExpressionVariable *entity(new ClangExpressionVariable(
1963 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1964 m_parser_vars->m_target_info.byte_order,
1965 m_parser_vars->m_target_info.address_byte_size));
1966 m_found_entities.AddNewlyConstructedVariable(entity);
1967
1968 std::string decl_name(context.m_decl_name.getAsString());
1969 entity->SetName(ConstString(decl_name.c_str()));
1970 entity->SetCompilerType(function_clang_type);
1971 entity->EnableParserVars(GetParserID());
1972
1973 ClangExpressionVariable::ParserVars *parser_vars =
1974 entity->GetParserVars(GetParserID());
1975
1976 if (load_addr != LLDB_INVALID_ADDRESS) {
1977 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1978 parser_vars->m_lldb_value.GetScalar() = load_addr;
1979 } else {
1980 // We have to try finding a file address.
1981
1982 lldb::addr_t file_addr = fun_address.GetFileAddress();
1983
1984 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress);
1985 parser_vars->m_lldb_value.GetScalar() = file_addr;
1986 }
1987
1988 parser_vars->m_named_decl = function_decl;
Konrad Kleine248a1302019-05-23 11:14:47 +00001989 parser_vars->m_llvm_value = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001990
1991 if (log) {
Pavel Labath03a77e92018-03-21 11:10:57 +00001992 std::string function_str =
1993 function_decl ? ASTDumper(function_decl).GetCString() : "nullptr";
Kate Stoneb9c1b512016-09-06 20:57:50 +00001994
1995 StreamString ss;
1996
1997 fun_address.Dump(&ss,
1998 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1999 Address::DumpStyleResolvedDescription);
2000
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00002001 LLDB_LOGF(
2002 log,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002003 " CEDM::FEVD[%u] Found %s function %s (description %s), returned %s",
2004 current_id, (function ? "specific" : "generic"), decl_name.c_str(),
Pavel Labath03a77e92018-03-21 11:10:57 +00002005 ss.GetData(), function_str.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002006 }
2007}
2008
2009void ClangExpressionDeclMap::AddThisType(NameSearchContext &context,
Aleksandr Urakov40624a02019-02-05 09:14:36 +00002010 const TypeFromUser &ut,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002011 unsigned int current_id) {
2012 CompilerType copied_clang_type = GuardedCopyType(ut);
2013
2014 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
2015
2016 if (!copied_clang_type) {
2017 if (log)
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00002018 LLDB_LOGF(
2019 log,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002020 "ClangExpressionDeclMap::AddThisType - Couldn't import the type");
Sean Callanan3b436d22015-10-23 21:45:02 +00002021
2022 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002023 }
2024
2025 if (copied_clang_type.IsAggregateType() &&
2026 copied_clang_type.GetCompleteType()) {
Raphael Isemannc502bae2019-11-20 12:40:08 +01002027 ClangASTContext *own_context =
2028 ClangASTContext::GetASTContext(m_ast_context);
2029 CompilerType void_clang_type = own_context->GetBasicType(eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002030 CompilerType void_ptr_clang_type = void_clang_type.GetPointerType();
2031
2032 CompilerType method_type = ClangASTContext::CreateFunctionType(
2033 m_ast_context, void_clang_type, &void_ptr_clang_type, 1, false, 0);
2034
2035 const bool is_virtual = false;
2036 const bool is_static = false;
2037 const bool is_inline = false;
2038 const bool is_explicit = false;
2039 const bool is_attr_used = true;
2040 const bool is_artificial = false;
2041
2042 CXXMethodDecl *method_decl =
2043 ClangASTContext::GetASTContext(m_ast_context)
2044 ->AddMethodToCXXRecordType(
Konrad Kleine248a1302019-05-23 11:14:47 +00002045 copied_clang_type.GetOpaqueQualType(), "$__lldb_expr", nullptr,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002046 method_type, lldb::eAccessPublic, is_virtual, is_static,
2047 is_inline, is_explicit, is_attr_used, is_artificial);
2048
2049 if (log) {
2050 ASTDumper method_ast_dumper((clang::Decl *)method_decl);
2051 ASTDumper type_ast_dumper(copied_clang_type);
2052
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00002053 LLDB_LOGF(log,
2054 " CEDM::AddThisType Added function $__lldb_expr "
2055 "(description %s) for this type %s",
2056 method_ast_dumper.GetCString(), type_ast_dumper.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002057 }
2058 }
2059
2060 if (!copied_clang_type.IsValid())
2061 return;
2062
2063 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(
2064 QualType::getFromOpaquePtr(copied_clang_type.GetOpaqueQualType()));
2065
2066 if (!type_source_info)
2067 return;
2068
2069 // Construct a typedef type because if "*this" is a templated type we can't
2070 // just return ClassTemplateSpecializationDecls in response to name queries.
2071 // Using a typedef makes this much more robust.
2072
2073 TypedefDecl *typedef_decl = TypedefDecl::Create(
2074 *m_ast_context, m_ast_context->getTranslationUnitDecl(), SourceLocation(),
2075 SourceLocation(), context.m_decl_name.getAsIdentifierInfo(),
2076 type_source_info);
2077
2078 if (!typedef_decl)
2079 return;
2080
2081 context.AddNamedDecl(typedef_decl);
2082
2083 return;
Sean Callananfa4fab72013-02-01 06:55:48 +00002084}
2085
Kate Stoneb9c1b512016-09-06 20:57:50 +00002086void ClangExpressionDeclMap::AddOneType(NameSearchContext &context,
Aleksandr Urakov40624a02019-02-05 09:14:36 +00002087 const TypeFromUser &ut,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002088 unsigned int current_id) {
2089 CompilerType copied_clang_type = GuardedCopyType(ut);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002090
Kate Stoneb9c1b512016-09-06 20:57:50 +00002091 if (!copied_clang_type) {
2092 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfa4fab72013-02-01 06:55:48 +00002093
Kate Stoneb9c1b512016-09-06 20:57:50 +00002094 if (log)
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00002095 LLDB_LOGF(
2096 log, "ClangExpressionDeclMap::AddOneType - Couldn't import the type");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002097
Kate Stoneb9c1b512016-09-06 20:57:50 +00002098 return;
2099 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002100
Kate Stoneb9c1b512016-09-06 20:57:50 +00002101 context.AddTypeDecl(copied_clang_type);
Sean Callanan5666b672010-08-04 01:02:13 +00002102}