blob: 6e7f11fed5f9885335b10d9fd11cbb6e55008225 [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 Callanana0d56432014-11-11 02:49:44 +000035#include "lldb/Target/CPPLanguageRuntime.h"
Sean Callanan1d180662010-07-20 23:31:16 +000036#include "lldb/Target/ExecutionContext.h"
Sean Callanane0b23b52012-11-15 02:02:04 +000037#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callananea22d422010-07-16 00:09:46 +000038#include "lldb/Target/Process.h"
Sean Callananf4b9bd32010-10-05 20:18:48 +000039#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000040#include "lldb/Target/StackFrame.h"
Sean Callanan1d180662010-07-20 23:31:16 +000041#include "lldb/Target/Target.h"
Jim Ingham895c9822010-12-07 01:56:02 +000042#include "lldb/Target/Thread.h"
Zachary Turner01c32432017-02-14 19:06:07 +000043#include "lldb/Utility/Endian.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000044#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000045#include "lldb/Utility/RegisterValue.h"
Zachary Turner97206d52017-05-12 04:51:55 +000046#include "lldb/Utility/Status.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000047#include "lldb/lldb-private.h"
48#include "clang/AST/ASTConsumer.h"
49#include "clang/AST/ASTContext.h"
Sean Callanan68e44232017-09-28 20:20:25 +000050#include "clang/AST/ASTImporter.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000051#include "clang/AST/Decl.h"
52#include "clang/AST/DeclarationName.h"
Sean Callanan68e44232017-09-28 20:20:25 +000053#include "clang/AST/RecursiveASTVisitor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054
Siva Chandra0f4873d2015-09-03 23:27:10 +000055#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
56
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,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000068 ExecutionContext &exe_ctx,
69 ValueObject *ctx_obj)
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 : ClangASTSource(exe_ctx.GetTargetSP()), m_found_entities(),
71 m_struct_members(), m_keep_result_in_memory(keep_result_in_memory),
Aleksandr Urakov40624a02019-02-05 09:14:36 +000072 m_result_delegate(result_delegate), m_parser_vars(), m_struct_vars(),
73 m_ctx_obj(ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 EnableStructVars();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075}
76
Kate Stoneb9c1b512016-09-06 20:57:50 +000077ClangExpressionDeclMap::~ClangExpressionDeclMap() {
78 // Note: The model is now that the parser's AST context and all associated
79 // data does not vanish until the expression has been executed. This means
80 // that valuable lookup data (like namespaces) doesn't vanish, but
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 DidParse();
83 DisableStructVars();
Sean Callanan979f74d2010-12-03 01:38:59 +000084}
Sean Callananbe3a1b12010-10-26 00:31:56 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086bool ClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx,
87 Materializer *materializer) {
88 ClangASTMetrics::ClearLocalCounters();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 EnableParserVars();
91 m_parser_vars->m_exe_ctx = exe_ctx;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 Target *target = exe_ctx.GetTargetPtr();
94 if (exe_ctx.GetFramePtr())
95 m_parser_vars->m_sym_ctx =
96 exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything);
97 else if (exe_ctx.GetThreadPtr() &&
98 exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0))
99 m_parser_vars->m_sym_ctx =
100 exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext(
101 lldb::eSymbolContextEverything);
102 else if (exe_ctx.GetProcessPtr()) {
103 m_parser_vars->m_sym_ctx.Clear(true);
104 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
105 } else if (target) {
106 m_parser_vars->m_sym_ctx.Clear(true);
107 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
108 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 if (target) {
111 m_parser_vars->m_persistent_vars = llvm::cast<ClangPersistentVariables>(
112 target->GetPersistentExpressionStateForLanguage(eLanguageTypeC));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 if (!target->GetScratchClangASTContext())
115 return false;
116 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 m_parser_vars->m_target_info = GetTargetInfo();
119 m_parser_vars->m_materializer = materializer;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 return true;
Sean Callanan979f74d2010-12-03 01:38:59 +0000122}
123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124void ClangExpressionDeclMap::InstallCodeGenerator(
125 clang::ASTConsumer *code_gen) {
126 assert(m_parser_vars);
127 m_parser_vars->m_code_gen = code_gen;
Sean Callanan80c97592015-05-01 00:47:29 +0000128}
129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130void ClangExpressionDeclMap::DidParse() {
131 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8106d802013-03-08 20:04:57 +0000132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 if (log)
134 ClangASTMetrics::DumpCounters(log);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 if (m_parser_vars.get()) {
137 for (size_t entity_index = 0, num_entities = m_found_entities.GetSize();
138 entity_index < num_entities; ++entity_index) {
139 ExpressionVariableSP var_sp(
140 m_found_entities.GetVariableAtIndex(entity_index));
141 if (var_sp)
142 llvm::cast<ClangExpressionVariable>(var_sp.get())
143 ->DisableParserVars(GetParserID());
Sean Callanan6b1b9532010-10-08 01:58:41 +0000144 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145
146 for (size_t pvar_index = 0,
147 num_pvars = m_parser_vars->m_persistent_vars->GetSize();
148 pvar_index < num_pvars; ++pvar_index) {
149 ExpressionVariableSP pvar_sp(
150 m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index));
151 if (ClangExpressionVariable *clang_var =
152 llvm::dyn_cast<ClangExpressionVariable>(pvar_sp.get()))
153 clang_var->DisableParserVars(GetParserID());
154 }
155
156 DisableParserVars();
157 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158}
159
Sean Callanan549c9f72010-07-13 21:41:46 +0000160// Interface for IRForTarget
161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162ClangExpressionDeclMap::TargetInfo ClangExpressionDeclMap::GetTargetInfo() {
163 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 TargetInfo ret;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
Sean Callanan933693b2012-02-10 01:22:05 +0000168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 Process *process = exe_ctx.GetProcessPtr();
170 if (process) {
171 ret.byte_order = process->GetByteOrder();
172 ret.address_byte_size = process->GetAddressByteSize();
173 } else {
174 Target *target = exe_ctx.GetTargetPtr();
175 if (target) {
176 ret.byte_order = target->GetArchitecture().GetByteOrder();
177 ret.address_byte_size = target->GetArchitecture().GetAddressByteSize();
Sean Callanan933693b2012-02-10 01:22:05 +0000178 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 }
Sean Callanan933693b2012-02-10 01:22:05 +0000180
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 return ret;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000182}
183
Sean Callanan68e44232017-09-28 20:20:25 +0000184namespace {
185/// This class walks an AST and ensures that all DeclContexts defined inside the
186/// current source file are properly complete.
187///
188/// This is used to ensure that persistent types defined in the current source
189/// file migrate completely to the persistent AST context before they are
190/// reused. If that didn't happen, it would be impoossible to complete them
191/// because their origin would be gone.
192///
193/// The stragtegy used by this class is to check the SourceLocation (to be
194/// specific, the FileID) and see if it's the FileID for the current expression.
195/// Alternate strategies could include checking whether an ExternalASTMerger,
196/// set up to not have the current context as a source, can find an original for
197/// the type.
198class Completer : public clang::RecursiveASTVisitor<Completer> {
199private:
200 clang::ASTImporter &m_exporter; /// Used to import Decl contents
201 clang::FileID m_file; /// The file that's going away
202 llvm::DenseSet<clang::Decl *> m_completed; /// Visited Decls, to avoid cycles
203
204 bool ImportAndCheckCompletable(clang::Decl *decl) {
205 (void)m_exporter.Import(decl);
206 if (m_completed.count(decl))
207 return false;
208 if (!llvm::isa<DeclContext>(decl))
209 return false;
210 const clang::SourceLocation loc = decl->getLocation();
211 if (!loc.isValid())
212 return false;
213 const clang::FileID file =
214 m_exporter.getFromContext().getSourceManager().getFileID(loc);
215 if (file != m_file)
216 return false;
Adrian Prantl05097242018-04-30 16:49:04 +0000217 // We are assuming the Decl was parsed in this very expression, so it
218 // should not have external storage.
Sean Callanan68e44232017-09-28 20:20:25 +0000219 lldbassert(!llvm::cast<DeclContext>(decl)->hasExternalLexicalStorage());
220 return true;
221 }
222
223 void Complete(clang::Decl *decl) {
224 m_completed.insert(decl);
225 auto *decl_context = llvm::cast<DeclContext>(decl);
226 (void)m_exporter.Import(decl);
227 m_exporter.CompleteDecl(decl);
228 for (Decl *child : decl_context->decls())
229 if (ImportAndCheckCompletable(child))
230 Complete(child);
231 }
232
233 void MaybeComplete(clang::Decl *decl) {
234 if (ImportAndCheckCompletable(decl))
235 Complete(decl);
236 }
237
238public:
239 Completer(clang::ASTImporter &exporter, clang::FileID file)
240 : m_exporter(exporter), m_file(file) {}
241
242 // Implements the RecursiveASTVisitor's core API. It is called on each Decl
243 // that the RecursiveASTVisitor encounters, and returns true if the traversal
244 // should continue.
245 bool VisitDecl(clang::Decl *decl) {
246 MaybeComplete(decl);
247 return true;
248 }
249};
250}
251
252static void CompleteAllDeclContexts(clang::ASTImporter &exporter,
253 clang::FileID file,
254 clang::QualType root) {
255 clang::QualType canonical_type = root.getCanonicalType();
256 if (clang::TagDecl *tag_decl = canonical_type->getAsTagDecl()) {
257 Completer(exporter, file).TraverseDecl(tag_decl);
258 } else if (auto interface_type = llvm::dyn_cast<ObjCInterfaceType>(
259 canonical_type.getTypePtr())) {
260 Completer(exporter, file).TraverseDecl(interface_type->getDecl());
261 } else {
262 Completer(exporter, file).TraverseType(canonical_type);
263 }
264}
265
266static clang::QualType ExportAllDeclaredTypes(
267 clang::ExternalASTMerger &merger,
268 clang::ASTContext &source, clang::FileManager &source_file_manager,
269 const clang::ExternalASTMerger::OriginMap &source_origin_map,
270 clang::FileID file, clang::QualType root) {
271 clang::ExternalASTMerger::ImporterSource importer_source =
272 { source, source_file_manager, source_origin_map };
273 merger.AddSources(importer_source);
274 clang::ASTImporter &exporter = merger.ImporterForOrigin(source);
275 CompleteAllDeclContexts(exporter, file, root);
276 clang::QualType ret = exporter.Import(root);
277 merger.RemoveSources(importer_source);
278 return ret;
279}
280
281TypeFromUser ClangExpressionDeclMap::DeportType(ClangASTContext &target,
282 ClangASTContext &source,
283 TypeFromParser parser_type) {
284 assert (&target == m_target->GetScratchClangASTContext());
285 assert ((TypeSystem*)&source == parser_type.GetTypeSystem());
286 assert (source.getASTContext() == m_ast_context);
287
288 if (m_ast_importer_sp) {
289 return TypeFromUser(m_ast_importer_sp->DeportType(
290 target.getASTContext(), source.getASTContext(),
291 parser_type.GetOpaqueQualType()),
292 &target);
293 } else if (m_merger_up) {
294 clang::FileID source_file =
295 source.getASTContext()->getSourceManager().getFileID(
296 source.getASTContext()->getTranslationUnitDecl()->getLocation());
297 auto scratch_ast_context = static_cast<ClangASTContextForExpressions*>(
298 m_target->GetScratchClangASTContext());
299 clang::QualType exported_type = ExportAllDeclaredTypes(
300 scratch_ast_context->GetMergerUnchecked(),
301 *source.getASTContext(), *source.getFileManager(),
302 m_merger_up->GetOrigins(),
303 source_file,
304 clang::QualType::getFromOpaquePtr(parser_type.GetOpaqueQualType()));
305 return TypeFromUser(exported_type.getAsOpaquePtr(), &target);
306 } else {
Pavel Labath9344e452017-10-17 21:52:29 +0000307 lldbassert(0 && "No mechanism for deporting a type!");
Sean Callanan68e44232017-09-28 20:20:25 +0000308 return TypeFromUser();
309 }
310}
311
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312bool ClangExpressionDeclMap::AddPersistentVariable(const NamedDecl *decl,
313 const ConstString &name,
314 TypeFromParser parser_type,
315 bool is_result,
316 bool is_lvalue) {
317 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000318
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319 ClangASTContext *ast =
320 llvm::dyn_cast_or_null<ClangASTContext>(parser_type.GetTypeSystem());
321 if (ast == nullptr)
322 return false;
Greg Claytonf73034f2015-09-08 18:15:05 +0000323
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324 if (m_parser_vars->m_materializer && is_result) {
Zachary Turner97206d52017-05-12 04:51:55 +0000325 Status err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000326
Sean Callanan933693b2012-02-10 01:22:05 +0000327 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
328 Target *target = exe_ctx.GetTargetPtr();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329 if (target == nullptr)
330 return false;
Greg Claytonc14ee322011-09-22 04:58:26 +0000331
Sean Callanan68e44232017-09-28 20:20:25 +0000332 TypeFromUser user_type =
333 DeportType(*target->GetScratchClangASTContext(), *ast, parser_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000334
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(
336 user_type, is_lvalue, m_keep_result_in_memory, m_result_delegate, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000337
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338 ClangExpressionVariable *var = new ClangExpressionVariable(
339 exe_ctx.GetBestExecutionContextScope(), name, user_type,
340 m_parser_vars->m_target_info.byte_order,
341 m_parser_vars->m_target_info.address_byte_size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000342
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343 m_found_entities.AddNewlyConstructedVariable(var);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000344
Sean Callananbc8ac342015-09-04 20:49:51 +0000345 var->EnableParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000346
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 ClangExpressionVariable::ParserVars *parser_vars =
348 var->GetParserVars(GetParserID());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000349
Sean Callanan3c495c12013-01-15 23:29:36 +0000350 parser_vars->m_named_decl = decl;
351 parser_vars->m_parser_type = parser_type;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 var->EnableJITVars(GetParserID());
354
355 ClangExpressionVariable::JITVars *jit_vars = var->GetJITVars(GetParserID());
356
357 jit_vars->m_offset = offset;
358
Sean Callanan64dfc9a2010-08-23 23:09:38 +0000359 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 }
361
362 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
363 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
364 Target *target = exe_ctx.GetTargetPtr();
365 if (target == NULL)
366 return false;
367
368 ClangASTContext *context(target->GetScratchClangASTContext());
369
Sean Callanan68e44232017-09-28 20:20:25 +0000370 TypeFromUser user_type = DeportType(*context, *ast, parser_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371
372 if (!user_type.GetOpaqueQualType()) {
373 if (log)
374 log->Printf("Persistent variable's type wasn't copied successfully");
375 return false;
376 }
377
378 if (!m_parser_vars->m_target_info.IsValid())
379 return false;
380
381 ClangExpressionVariable *var = llvm::cast<ClangExpressionVariable>(
382 m_parser_vars->m_persistent_vars
383 ->CreatePersistentVariable(
384 exe_ctx.GetBestExecutionContextScope(), name, user_type,
385 m_parser_vars->m_target_info.byte_order,
386 m_parser_vars->m_target_info.address_byte_size)
387 .get());
388
389 if (!var)
390 return false;
391
392 var->m_frozen_sp->SetHasCompleteType();
393
394 if (is_result)
395 var->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry;
396 else
397 var->m_flags |=
398 ClangExpressionVariable::EVKeepInTarget; // explicitly-declared
399 // persistent variables should
400 // persist
401
402 if (is_lvalue) {
403 var->m_flags |= ClangExpressionVariable::EVIsProgramReference;
404 } else {
405 var->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
406 var->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
407 }
408
409 if (m_keep_result_in_memory) {
410 var->m_flags |= ClangExpressionVariable::EVKeepInTarget;
411 }
412
413 if (log)
414 log->Printf("Created persistent variable with flags 0x%hx", var->m_flags);
415
416 var->EnableParserVars(GetParserID());
417
418 ClangExpressionVariable::ParserVars *parser_vars =
419 var->GetParserVars(GetParserID());
420
421 parser_vars->m_named_decl = decl;
422 parser_vars->m_parser_type = parser_type;
423
424 return true;
Sean Callanan2235f322010-08-11 03:57:18 +0000425}
426
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427bool ClangExpressionDeclMap::AddValueToStruct(const NamedDecl *decl,
428 const ConstString &name,
429 llvm::Value *value, size_t size,
430 lldb::offset_t alignment) {
431 assert(m_struct_vars.get());
432 assert(m_parser_vars.get());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000433
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 bool is_persistent_variable = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000435
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000437
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 m_struct_vars->m_struct_laid_out = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000439
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440 if (ClangExpressionVariable::FindVariableInList(m_struct_members, decl,
441 GetParserID()))
442 return true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 ClangExpressionVariable *var(ClangExpressionVariable::FindVariableInList(
445 m_found_entities, decl, GetParserID()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000446
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 if (!var) {
448 var = ClangExpressionVariable::FindVariableInList(
449 *m_parser_vars->m_persistent_vars, decl, GetParserID());
450 is_persistent_variable = true;
451 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000452
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453 if (!var)
454 return false;
455
456 if (log)
457 log->Printf("Adding value for (NamedDecl*)%p [%s - %s] to the structure",
458 static_cast<const void *>(decl), name.GetCString(),
459 var->GetName().GetCString());
460
461 // We know entity->m_parser_vars is valid because we used a parser variable
462 // to find it
463
464 ClangExpressionVariable::ParserVars *parser_vars =
465 llvm::cast<ClangExpressionVariable>(var)->GetParserVars(GetParserID());
466
467 parser_vars->m_llvm_value = value;
468
469 if (ClangExpressionVariable::JITVars *jit_vars =
470 llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID())) {
471 // We already laid this out; do not touch
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000472
Sean Callanan823bb4c2010-08-30 22:17:16 +0000473 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 log->Printf("Already placed at 0x%llx",
475 (unsigned long long)jit_vars->m_offset);
476 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000477
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478 llvm::cast<ClangExpressionVariable>(var)->EnableJITVars(GetParserID());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000479
Kate Stoneb9c1b512016-09-06 20:57:50 +0000480 ClangExpressionVariable::JITVars *jit_vars =
481 llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID());
Sean Callanan3c495c12013-01-15 23:29:36 +0000482
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483 jit_vars->m_alignment = alignment;
484 jit_vars->m_size = size;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000485
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486 m_struct_members.AddVariable(var->shared_from_this());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000487
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488 if (m_parser_vars->m_materializer) {
489 uint32_t offset = 0;
490
Zachary Turner97206d52017-05-12 04:51:55 +0000491 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492
493 if (is_persistent_variable) {
494 ExpressionVariableSP var_sp(var->shared_from_this());
495 offset = m_parser_vars->m_materializer->AddPersistentVariable(
496 var_sp, nullptr, err);
497 } else {
498 if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym)
499 offset = m_parser_vars->m_materializer->AddSymbol(*sym, err);
500 else if (const RegisterInfo *reg_info = var->GetRegisterInfo())
501 offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err);
502 else if (parser_vars->m_lldb_var)
503 offset = m_parser_vars->m_materializer->AddVariable(
504 parser_vars->m_lldb_var, err);
Sean Callanan1582ee62013-04-18 22:06:33 +0000505 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000506
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507 if (!err.Success())
508 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000509
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 if (log)
511 log->Printf("Placed at 0x%llx", (unsigned long long)offset);
Sean Callanan3c495c12013-01-15 23:29:36 +0000512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 jit_vars->m_offset =
514 offset; // TODO DoStructLayout() should not change this.
515 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000516
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 return true;
518}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000519
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520bool ClangExpressionDeclMap::DoStructLayout() {
521 assert(m_struct_vars.get());
Sean Callanan3dd6a422013-04-11 21:16:36 +0000522
Kate Stoneb9c1b512016-09-06 20:57:50 +0000523 if (m_struct_vars->m_struct_laid_out)
524 return true;
Sean Callanandf667652013-04-11 02:05:11 +0000525
Kate Stoneb9c1b512016-09-06 20:57:50 +0000526 if (!m_parser_vars->m_materializer)
527 return false;
528
529 m_struct_vars->m_struct_alignment =
530 m_parser_vars->m_materializer->GetStructAlignment();
531 m_struct_vars->m_struct_size =
532 m_parser_vars->m_materializer->GetStructByteSize();
533 m_struct_vars->m_struct_laid_out = true;
534 return true;
535}
536
537bool ClangExpressionDeclMap::GetStructInfo(uint32_t &num_elements, size_t &size,
538 lldb::offset_t &alignment) {
539 assert(m_struct_vars.get());
540
541 if (!m_struct_vars->m_struct_laid_out)
542 return false;
543
544 num_elements = m_struct_members.GetSize();
545 size = m_struct_vars->m_struct_size;
546 alignment = m_struct_vars->m_struct_alignment;
547
548 return true;
549}
550
551bool ClangExpressionDeclMap::GetStructElement(const NamedDecl *&decl,
552 llvm::Value *&value,
553 lldb::offset_t &offset,
554 ConstString &name,
555 uint32_t index) {
556 assert(m_struct_vars.get());
557
558 if (!m_struct_vars->m_struct_laid_out)
559 return false;
560
561 if (index >= m_struct_members.GetSize())
562 return false;
563
564 ExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index));
565
566 if (!member_sp)
567 return false;
568
569 ClangExpressionVariable::ParserVars *parser_vars =
570 llvm::cast<ClangExpressionVariable>(member_sp.get())
571 ->GetParserVars(GetParserID());
572 ClangExpressionVariable::JITVars *jit_vars =
573 llvm::cast<ClangExpressionVariable>(member_sp.get())
574 ->GetJITVars(GetParserID());
575
576 if (!parser_vars || !jit_vars || !member_sp->GetValueObject())
577 return false;
578
579 decl = parser_vars->m_named_decl;
580 value = parser_vars->m_llvm_value;
581 offset = jit_vars->m_offset;
582 name = member_sp->GetName();
583
584 return true;
585}
586
587bool ClangExpressionDeclMap::GetFunctionInfo(const NamedDecl *decl,
588 uint64_t &ptr) {
589 ClangExpressionVariable *entity(ClangExpressionVariable::FindVariableInList(
590 m_found_entities, decl, GetParserID()));
591
592 if (!entity)
593 return false;
594
Adrian Prantl05097242018-04-30 16:49:04 +0000595 // We know m_parser_vars is valid since we searched for the variable by its
596 // NamedDecl
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597
598 ClangExpressionVariable::ParserVars *parser_vars =
599 entity->GetParserVars(GetParserID());
600
601 ptr = parser_vars->m_lldb_value.GetScalar().ULongLong();
602
603 return true;
604}
605
606addr_t ClangExpressionDeclMap::GetSymbolAddress(Target &target,
607 Process *process,
608 const ConstString &name,
609 lldb::SymbolType symbol_type,
610 lldb_private::Module *module) {
611 SymbolContextList sc_list;
612
613 if (module)
614 module->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
615 else
616 target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list);
617
618 const uint32_t num_matches = sc_list.GetSize();
619 addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
620
621 for (uint32_t i = 0;
622 i < num_matches &&
623 (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS);
624 i++) {
625 SymbolContext sym_ctx;
626 sc_list.GetContextAtIndex(i, sym_ctx);
627
628 const Address sym_address = sym_ctx.symbol->GetAddress();
629
630 if (!sym_address.IsValid())
631 continue;
632
633 switch (sym_ctx.symbol->GetType()) {
634 case eSymbolTypeCode:
635 case eSymbolTypeTrampoline:
636 symbol_load_addr = sym_address.GetCallableLoadAddress(&target);
637 break;
638
639 case eSymbolTypeResolver:
640 symbol_load_addr = sym_address.GetCallableLoadAddress(&target, true);
641 break;
642
643 case eSymbolTypeReExported: {
644 ConstString reexport_name = sym_ctx.symbol->GetReExportedSymbolName();
645 if (reexport_name) {
646 ModuleSP reexport_module_sp;
647 ModuleSpec reexport_module_spec;
648 reexport_module_spec.GetPlatformFileSpec() =
649 sym_ctx.symbol->GetReExportedSymbolSharedLibrary();
650 if (reexport_module_spec.GetPlatformFileSpec()) {
651 reexport_module_sp =
652 target.GetImages().FindFirstModule(reexport_module_spec);
653 if (!reexport_module_sp) {
654 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
655 reexport_module_sp =
656 target.GetImages().FindFirstModule(reexport_module_spec);
657 }
Sean Callanandf667652013-04-11 02:05:11 +0000658 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000659 symbol_load_addr = GetSymbolAddress(
660 target, process, sym_ctx.symbol->GetReExportedSymbolName(),
661 symbol_type, reexport_module_sp.get());
662 }
663 } break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000664
Kate Stoneb9c1b512016-09-06 20:57:50 +0000665 case eSymbolTypeData:
666 case eSymbolTypeRuntime:
667 case eSymbolTypeVariable:
668 case eSymbolTypeLocal:
669 case eSymbolTypeParam:
670 case eSymbolTypeInvalid:
671 case eSymbolTypeAbsolute:
672 case eSymbolTypeException:
673 case eSymbolTypeSourceFile:
674 case eSymbolTypeHeaderFile:
675 case eSymbolTypeObjectFile:
676 case eSymbolTypeCommonBlock:
677 case eSymbolTypeBlock:
678 case eSymbolTypeVariableType:
679 case eSymbolTypeLineEntry:
680 case eSymbolTypeLineHeader:
681 case eSymbolTypeScopeBegin:
682 case eSymbolTypeScopeEnd:
683 case eSymbolTypeAdditional:
684 case eSymbolTypeCompiler:
685 case eSymbolTypeInstrumentation:
686 case eSymbolTypeUndefined:
687 case eSymbolTypeObjCClass:
688 case eSymbolTypeObjCMetaClass:
689 case eSymbolTypeObjCIVar:
690 symbol_load_addr = sym_address.GetLoadAddress(&target);
691 break;
Sean Callanandf667652013-04-11 02:05:11 +0000692 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000694
Kate Stoneb9c1b512016-09-06 20:57:50 +0000695 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process) {
696 ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime();
Sean Callanan549c9f72010-07-13 21:41:46 +0000697
Kate Stoneb9c1b512016-09-06 20:57:50 +0000698 if (runtime) {
699 symbol_load_addr = runtime->LookupRuntimeSymbol(name);
Greg Clayton084db102011-06-23 04:25:29 +0000700 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000701 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000702
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703 return symbol_load_addr;
Sean Callananc3a16002011-01-17 23:42:46 +0000704}
705
Kate Stoneb9c1b512016-09-06 20:57:50 +0000706addr_t ClangExpressionDeclMap::GetSymbolAddress(const ConstString &name,
707 lldb::SymbolType symbol_type) {
708 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000709
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710 if (!m_parser_vars->m_exe_ctx.GetTargetPtr())
711 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000712
Kate Stoneb9c1b512016-09-06 20:57:50 +0000713 return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(),
714 m_parser_vars->m_exe_ctx.GetProcessPtr(), name,
715 symbol_type);
Sean Callanand9ca42a2011-05-08 02:21:26 +0000716}
717
Kate Stoneb9c1b512016-09-06 20:57:50 +0000718lldb::VariableSP ClangExpressionDeclMap::FindGlobalVariable(
719 Target &target, ModuleSP &module, const ConstString &name,
720 CompilerDeclContext *namespace_decl, TypeFromUser *type) {
721 VariableList vars;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000722
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723 if (module && namespace_decl)
Pavel Labath34cda142018-05-31 09:46:26 +0000724 module->FindGlobalVariables(name, namespace_decl, -1, vars);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000725 else
Pavel Labath34cda142018-05-31 09:46:26 +0000726 target.GetImages().FindGlobalVariables(name, -1, vars);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000727
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 if (vars.GetSize()) {
729 if (type) {
730 for (size_t i = 0; i < vars.GetSize(); ++i) {
731 VariableSP var_sp = vars.GetVariableAtIndex(i);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000732
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733 if (ClangASTContext::AreTypesSame(
734 *type, var_sp->GetType()->GetFullCompilerType()))
735 return var_sp;
736 }
737 } else {
738 return vars.GetVariableAtIndex(0);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000739 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000740 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000741
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742 return VariableSP();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000743}
744
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745ClangASTContext *ClangExpressionDeclMap::GetClangASTContext() {
746 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
747 if (frame == nullptr)
748 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +0000749
Kate Stoneb9c1b512016-09-06 20:57:50 +0000750 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
751 lldb::eSymbolContextBlock);
752 if (sym_ctx.block == nullptr)
753 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +0000754
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755 CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext();
756 if (!frame_decl_context)
757 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +0000758
Kate Stoneb9c1b512016-09-06 20:57:50 +0000759 return llvm::dyn_cast_or_null<ClangASTContext>(
760 frame_decl_context.GetTypeSystem());
Siva Chandra03ff5c82016-02-05 19:10:04 +0000761}
762
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763// Interface for ClangASTSource
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000764
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765void ClangExpressionDeclMap::FindExternalVisibleDecls(
766 NameSearchContext &context) {
767 assert(m_ast_context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000768
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 ClangASTMetrics::RegisterVisibleQuery();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000770
Kate Stoneb9c1b512016-09-06 20:57:50 +0000771 const ConstString name(context.m_decl_name.getAsString().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000772
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000774
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 if (GetImportInProgress()) {
776 if (log && log->GetVerbose())
777 log->Printf("Ignoring a query during an import");
778 return;
779 }
780
781 static unsigned int invocation_id = 0;
782 unsigned int current_id = invocation_id++;
783
784 if (log) {
785 if (!context.m_decl_context)
786 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for "
787 "'%s' in a NULL DeclContext",
788 current_id, name.GetCString());
789 else if (const NamedDecl *context_named_decl =
790 dyn_cast<NamedDecl>(context.m_decl_context))
791 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for "
792 "'%s' in '%s'",
793 current_id, name.GetCString(),
794 context_named_decl->getNameAsString().c_str());
795 else
796 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for "
797 "'%s' in a '%s'",
798 current_id, name.GetCString(),
799 context.m_decl_context->getDeclKindName());
800 }
801
802 if (const NamespaceDecl *namespace_context =
803 dyn_cast<NamespaceDecl>(context.m_decl_context)) {
804 if (namespace_context->getName().str() ==
805 std::string(g_lldb_local_vars_namespace_cstr)) {
806 CompilerDeclContext compiler_decl_ctx(
807 GetClangASTContext(), const_cast<void *>(static_cast<const void *>(
808 context.m_decl_context)));
809 FindExternalVisibleDecls(context, lldb::ModuleSP(), compiler_decl_ctx,
810 current_id);
811 return;
Sean Callanan6abfabf2010-11-19 20:20:02 +0000812 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000813
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814 ClangASTImporter::NamespaceMapSP namespace_map =
Sean Callanan68e44232017-09-28 20:20:25 +0000815 m_ast_importer_sp
816 ? m_ast_importer_sp->GetNamespaceMap(namespace_context)
817 : ClangASTImporter::NamespaceMapSP();
818
819 if (!namespace_map)
820 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000821
822 if (log && log->GetVerbose())
823 log->Printf(" CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)",
824 current_id, static_cast<void *>(namespace_map.get()),
825 (int)namespace_map->size());
Sean Callanan68e44232017-09-28 20:20:25 +0000826
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(),
828 e = namespace_map->end();
829 i != e; ++i) {
830 if (log)
831 log->Printf(" CEDM::FEVD[%u] Searching namespace %s in module %s",
832 current_id, i->second.GetName().AsCString(),
833 i->first->GetFileSpec().GetFilename().GetCString());
834
835 FindExternalVisibleDecls(context, i->first, i->second, current_id);
836 }
837 } else if (isa<TranslationUnitDecl>(context.m_decl_context)) {
838 CompilerDeclContext namespace_decl;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000839
Sean Callanan503aa522011-10-12 00:12:34 +0000840 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000841 log->Printf(" CEDM::FEVD[%u] Searching the root namespace", current_id);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000842
Kate Stoneb9c1b512016-09-06 20:57:50 +0000843 FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl,
844 current_id);
845 }
Sean Callananc02a1c02017-04-24 23:14:04 +0000846
847 ClangASTSource::FindExternalVisibleDecls(context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000848}
849
850void ClangExpressionDeclMap::FindExternalVisibleDecls(
851 NameSearchContext &context, lldb::ModuleSP module_sp,
852 CompilerDeclContext &namespace_decl, unsigned int current_id) {
853 assert(m_ast_context);
854
855 std::function<void(clang::FunctionDecl *)> MaybeRegisterFunctionBody =
856 [this](clang::FunctionDecl *copied_function_decl) {
857 if (copied_function_decl->getBody() && m_parser_vars->m_code_gen) {
858 DeclGroupRef decl_group_ref(copied_function_decl);
859 m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref);
860 }
861 };
862
863 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
864
865 SymbolContextList sc_list;
866
867 const ConstString name(context.m_decl_name.getAsString().c_str());
Sean Callanan1b3c43b2017-09-26 17:25:34 +0000868 if (IgnoreName(name, false))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000869 return;
870
Adrian Prantl05097242018-04-30 16:49:04 +0000871 // Only look for functions by name out in our symbols if the function doesn't
872 // start with our phony prefix of '$'
Kate Stoneb9c1b512016-09-06 20:57:50 +0000873 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
874 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
875 SymbolContext sym_ctx;
876 if (frame != nullptr)
877 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
878 lldb::eSymbolContextBlock);
879
880 // Try the persistent decls, which take precedence over all else.
881 if (!namespace_decl) {
882 do {
883 if (!target)
884 break;
885
886 ClangASTContext *scratch_clang_ast_context =
887 target->GetScratchClangASTContext();
888
889 if (!scratch_clang_ast_context)
890 break;
891
892 ASTContext *scratch_ast_context =
893 scratch_clang_ast_context->getASTContext();
894
895 if (!scratch_ast_context)
896 break;
897
898 NamedDecl *persistent_decl =
899 m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
900
901 if (!persistent_decl)
902 break;
903
Sean Callanan68e44232017-09-28 20:20:25 +0000904 Decl *parser_persistent_decl = CopyDecl(persistent_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000905
906 if (!parser_persistent_decl)
907 break;
908
909 NamedDecl *parser_named_decl =
910 dyn_cast<NamedDecl>(parser_persistent_decl);
911
912 if (!parser_named_decl)
913 break;
914
915 if (clang::FunctionDecl *parser_function_decl =
916 llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl)) {
917 MaybeRegisterFunctionBody(parser_function_decl);
918 }
919
920 if (log)
921 log->Printf(" CEDM::FEVD[%u] Found persistent decl %s", current_id,
922 name.GetCString());
923
924 context.AddNamedDecl(parser_named_decl);
925 } while (0);
926 }
927
Sean Callanan1b3c43b2017-09-26 17:25:34 +0000928 if (name.GetCString()[0] == '$' && !namespace_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000929 static ConstString g_lldb_class_name("$__lldb_class");
930
931 if (name == g_lldb_class_name) {
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000932 if (m_ctx_obj) {
933 Status status;
934 lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status);
935 if (!ctx_obj_ptr || status.Fail())
936 return;
937
938 AddThisType(context, TypeFromUser(m_ctx_obj->GetCompilerType()),
939 current_id);
940
941 m_struct_vars->m_object_pointer_type =
942 TypeFromUser(ctx_obj_ptr->GetCompilerType());
943
944 return;
945 }
946
Kate Stoneb9c1b512016-09-06 20:57:50 +0000947 // Clang is looking for the type of "this"
948
949 if (frame == NULL)
950 return;
951
952 // Find the block that defines the function represented by "sym_ctx"
953 Block *function_block = sym_ctx.GetFunctionBlock();
954
955 if (!function_block)
956 return;
957
958 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
959
960 if (!function_decl_ctx)
961 return;
962
963 clang::CXXMethodDecl *method_decl =
964 ClangASTContext::DeclContextGetAsCXXMethodDecl(function_decl_ctx);
965
966 if (method_decl) {
967 clang::CXXRecordDecl *class_decl = method_decl->getParent();
968
969 QualType class_qual_type(class_decl->getTypeForDecl(), 0);
970
971 TypeFromUser class_user_type(
972 class_qual_type.getAsOpaquePtr(),
973 ClangASTContext::GetASTContext(&class_decl->getASTContext()));
974
975 if (log) {
976 ASTDumper ast_dumper(class_qual_type);
977 log->Printf(" CEDM::FEVD[%u] Adding type for $__lldb_class: %s",
978 current_id, ast_dumper.GetCString());
Siva Chandra03ff5c82016-02-05 19:10:04 +0000979 }
980
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981 AddThisType(context, class_user_type, current_id);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000982
Kate Stoneb9c1b512016-09-06 20:57:50 +0000983 if (method_decl->isInstance()) {
984 // self is a pointer to the object
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000985
Kate Stoneb9c1b512016-09-06 20:57:50 +0000986 QualType class_pointer_type =
987 method_decl->getASTContext().getPointerType(class_qual_type);
988
989 TypeFromUser self_user_type(
990 class_pointer_type.getAsOpaquePtr(),
991 ClangASTContext::GetASTContext(&method_decl->getASTContext()));
992
993 m_struct_vars->m_object_pointer_type = self_user_type;
994 }
995 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000996 // This branch will get hit if we are executing code in the context of
997 // a function that claims to have an object pointer (through
998 // DW_AT_object_pointer?) but is not formally a method of the class.
999 // In that case, just look up the "this" variable in the current scope
1000 // and use its type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 // FIXME: This code is formally correct, but clang doesn't currently
1002 // emit DW_AT_object_pointer
1003 // for C++ so it hasn't actually been tested.
1004
1005 VariableList *vars = frame->GetVariableList(false);
1006
1007 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
1008
1009 if (this_var && this_var->IsInScope(frame) &&
1010 this_var->LocationIsValidForFrame(frame)) {
1011 Type *this_type = this_var->GetType();
1012
1013 if (!this_type)
Sean Callananb2269162011-10-21 22:18:07 +00001014 return;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001015
Kate Stoneb9c1b512016-09-06 20:57:50 +00001016 TypeFromUser pointee_type =
1017 this_type->GetForwardCompilerType().GetPointeeType();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001018
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019 if (pointee_type.IsValid()) {
1020 if (log) {
1021 ASTDumper ast_dumper(pointee_type);
1022 log->Printf(" FEVD[%u] Adding type for $__lldb_class: %s",
1023 current_id, ast_dumper.GetCString());
1024 }
1025
1026 AddThisType(context, pointee_type, current_id);
1027 TypeFromUser this_user_type(this_type->GetFullCompilerType());
1028 m_struct_vars->m_object_pointer_type = this_user_type;
1029 return;
1030 }
Sean Callanan503aa522011-10-12 00:12:34 +00001031 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001032 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001033
Kate Stoneb9c1b512016-09-06 20:57:50 +00001034 return;
1035 }
1036
1037 static ConstString g_lldb_objc_class_name("$__lldb_objc_class");
1038 if (name == g_lldb_objc_class_name) {
Aleksandr Urakov40624a02019-02-05 09:14:36 +00001039 if (m_ctx_obj) {
1040 Status status;
1041 lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status);
1042 if (!ctx_obj_ptr || status.Fail())
1043 return;
1044
1045 AddOneType(context, TypeFromUser(m_ctx_obj->GetCompilerType()),
1046 current_id);
1047
1048 m_struct_vars->m_object_pointer_type =
1049 TypeFromUser(ctx_obj_ptr->GetCompilerType());
1050
1051 return;
1052 }
1053
Kate Stoneb9c1b512016-09-06 20:57:50 +00001054 // Clang is looking for the type of "*self"
1055
1056 if (!frame)
1057 return;
1058
1059 SymbolContext sym_ctx = frame->GetSymbolContext(
1060 lldb::eSymbolContextFunction | lldb::eSymbolContextBlock);
1061
1062 // Find the block that defines the function represented by "sym_ctx"
1063 Block *function_block = sym_ctx.GetFunctionBlock();
1064
1065 if (!function_block)
1066 return;
1067
1068 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
1069
1070 if (!function_decl_ctx)
1071 return;
1072
1073 clang::ObjCMethodDecl *method_decl =
1074 ClangASTContext::DeclContextGetAsObjCMethodDecl(function_decl_ctx);
1075
1076 if (method_decl) {
1077 ObjCInterfaceDecl *self_interface = method_decl->getClassInterface();
1078
1079 if (!self_interface)
1080 return;
1081
1082 const clang::Type *interface_type = self_interface->getTypeForDecl();
1083
1084 if (!interface_type)
1085 return; // This is unlikely, but we have seen crashes where this
1086 // occurred
1087
1088 TypeFromUser class_user_type(
1089 QualType(interface_type, 0).getAsOpaquePtr(),
1090 ClangASTContext::GetASTContext(&method_decl->getASTContext()));
1091
1092 if (log) {
1093 ASTDumper ast_dumper(interface_type);
1094 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s",
1095 current_id, ast_dumper.GetCString());
1096 }
1097
1098 AddOneType(context, class_user_type, current_id);
1099
1100 if (method_decl->isInstanceMethod()) {
1101 // self is a pointer to the object
1102
1103 QualType class_pointer_type =
1104 method_decl->getASTContext().getObjCObjectPointerType(
1105 QualType(interface_type, 0));
1106
1107 TypeFromUser self_user_type(
1108 class_pointer_type.getAsOpaquePtr(),
1109 ClangASTContext::GetASTContext(&method_decl->getASTContext()));
1110
1111 m_struct_vars->m_object_pointer_type = self_user_type;
1112 } else {
1113 // self is a Class pointer
1114 QualType class_type = method_decl->getASTContext().getObjCClassType();
1115
1116 TypeFromUser self_user_type(
1117 class_type.getAsOpaquePtr(),
1118 ClangASTContext::GetASTContext(&method_decl->getASTContext()));
1119
1120 m_struct_vars->m_object_pointer_type = self_user_type;
1121 }
1122
1123 return;
1124 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00001125 // This branch will get hit if we are executing code in the context of
1126 // a function that claims to have an object pointer (through
1127 // DW_AT_object_pointer?) but is not formally a method of the class.
1128 // In that case, just look up the "self" variable in the current scope
1129 // and use its type.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130
1131 VariableList *vars = frame->GetVariableList(false);
1132
1133 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
1134
1135 if (self_var && self_var->IsInScope(frame) &&
1136 self_var->LocationIsValidForFrame(frame)) {
1137 Type *self_type = self_var->GetType();
1138
1139 if (!self_type)
1140 return;
1141
1142 CompilerType self_clang_type = self_type->GetFullCompilerType();
1143
1144 if (ClangASTContext::IsObjCClassType(self_clang_type)) {
1145 return;
1146 } else if (ClangASTContext::IsObjCObjectPointerType(
1147 self_clang_type)) {
1148 self_clang_type = self_clang_type.GetPointeeType();
1149
1150 if (!self_clang_type)
1151 return;
1152
1153 if (log) {
1154 ASTDumper ast_dumper(self_type->GetFullCompilerType());
1155 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s",
1156 current_id, ast_dumper.GetCString());
1157 }
1158
1159 TypeFromUser class_user_type(self_clang_type);
1160
1161 AddOneType(context, class_user_type, current_id);
1162
1163 TypeFromUser self_user_type(self_type->GetFullCompilerType());
1164
1165 m_struct_vars->m_object_pointer_type = self_user_type;
1166 return;
1167 }
1168 }
1169 }
1170
1171 return;
1172 }
1173
1174 if (name == ConstString(g_lldb_local_vars_namespace_cstr)) {
1175 CompilerDeclContext frame_decl_context =
1176 sym_ctx.block != nullptr ? sym_ctx.block->GetDeclContext()
1177 : CompilerDeclContext();
1178
1179 if (frame_decl_context) {
1180 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(
1181 frame_decl_context.GetTypeSystem());
1182
1183 if (ast) {
1184 clang::NamespaceDecl *namespace_decl =
1185 ClangASTContext::GetUniqueNamespaceDeclaration(
Sean Callanan1b3c43b2017-09-26 17:25:34 +00001186 m_ast_context, name.GetCString(), nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001187 if (namespace_decl) {
1188 context.AddNamedDecl(namespace_decl);
1189 clang::DeclContext *clang_decl_ctx =
1190 clang::Decl::castToDeclContext(namespace_decl);
1191 clang_decl_ctx->setHasExternalVisibleStorage(true);
1192 context.m_found.local_vars_nsp = true;
1193 }
1194 }
1195 }
1196
1197 return;
1198 }
1199
1200 // any other $__lldb names should be weeded out now
Sean Callanan1b3c43b2017-09-26 17:25:34 +00001201 if (name.GetStringRef().startswith("$__lldb"))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001202 return;
1203
1204 ExpressionVariableSP pvar_sp(
1205 m_parser_vars->m_persistent_vars->GetVariable(name));
1206
1207 if (pvar_sp) {
1208 AddOneVariable(context, pvar_sp, current_id);
1209 return;
1210 }
1211
1212 const char *reg_name(&name.GetCString()[1]);
1213
1214 if (m_parser_vars->m_exe_ctx.GetRegisterContext()) {
1215 const RegisterInfo *reg_info(
1216 m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(
1217 reg_name));
1218
1219 if (reg_info) {
Sean Callanan503aa522011-10-12 00:12:34 +00001220 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001221 log->Printf(" CEDM::FEVD[%u] Found register %s", current_id,
1222 reg_info->name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001223
Kate Stoneb9c1b512016-09-06 20:57:50 +00001224 AddOneRegister(context, reg_info, current_id);
1225 }
1226 }
1227 } else {
1228 ValueObjectSP valobj;
1229 VariableSP var;
1230
1231 bool local_var_lookup =
1232 !namespace_decl || (namespace_decl.GetName() ==
1233 ConstString(g_lldb_local_vars_namespace_cstr));
1234 if (frame && local_var_lookup) {
1235 CompilerDeclContext compiler_decl_context =
1236 sym_ctx.block != nullptr ? sym_ctx.block->GetDeclContext()
1237 : CompilerDeclContext();
1238
1239 if (compiler_decl_context) {
1240 // Make sure that the variables are parsed so that we have the
1241 // declarations.
1242 VariableListSP vars = frame->GetInScopeVariableList(true);
1243 for (size_t i = 0; i < vars->GetSize(); i++)
1244 vars->GetVariableAtIndex(i)->GetDecl();
1245
1246 // Search for declarations matching the name. Do not include imported
Adrian Prantl05097242018-04-30 16:49:04 +00001247 // decls in the search if we are looking for decls in the artificial
1248 // namespace $__lldb_local_vars.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001249 std::vector<CompilerDecl> found_decls =
1250 compiler_decl_context.FindDeclByName(name,
1251 namespace_decl.IsValid());
1252
1253 bool variable_found = false;
1254 for (CompilerDecl decl : found_decls) {
1255 for (size_t vi = 0, ve = vars->GetSize(); vi != ve; ++vi) {
1256 VariableSP candidate_var = vars->GetVariableAtIndex(vi);
1257 if (candidate_var->GetDecl() == decl) {
1258 var = candidate_var;
1259 break;
1260 }
1261 }
1262
Sean Callananc02a1c02017-04-24 23:14:04 +00001263 if (var && !variable_found) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001264 variable_found = true;
1265 valobj = ValueObjectVariable::Create(frame, var);
1266 AddOneVariable(context, var, valobj, current_id);
1267 context.m_found.variable = true;
1268 }
1269 }
1270 if (variable_found)
1271 return;
1272 }
1273 }
1274 if (target) {
1275 var = FindGlobalVariable(*target, module_sp, name, &namespace_decl, NULL);
1276
1277 if (var) {
1278 valobj = ValueObjectVariable::Create(target, var);
1279 AddOneVariable(context, var, valobj, current_id);
1280 context.m_found.variable = true;
1281 return;
1282 }
Sean Callanan503aa522011-10-12 00:12:34 +00001283 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001284
Kate Stoneb9c1b512016-09-06 20:57:50 +00001285 std::vector<clang::NamedDecl *> decls_from_modules;
Sean Callanan503aa522011-10-12 00:12:34 +00001286
Kate Stoneb9c1b512016-09-06 20:57:50 +00001287 if (target) {
1288 if (ClangModulesDeclVendor *decl_vendor =
1289 target->GetClangModulesDeclVendor()) {
1290 decl_vendor->FindDecls(name, false, UINT32_MAX, decls_from_modules);
1291 }
1292 }
Sean Callananb3a36df2016-03-19 00:51:43 +00001293
Sean Callananc02a1c02017-04-24 23:14:04 +00001294 const bool include_inlines = false;
1295 const bool append = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001296
Sean Callananc02a1c02017-04-24 23:14:04 +00001297 if (namespace_decl && module_sp) {
1298 const bool include_symbols = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001299
Sean Callananc02a1c02017-04-24 23:14:04 +00001300 module_sp->FindFunctions(name, &namespace_decl, eFunctionNameTypeBase,
1301 include_symbols, include_inlines, append,
1302 sc_list);
1303 } else if (target && !namespace_decl) {
1304 const bool include_symbols = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001305
Sean Callananc02a1c02017-04-24 23:14:04 +00001306 // TODO Fix FindFunctions so that it doesn't return
1307 // instance methods for eFunctionNameTypeBase.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001308
Sean Callananc02a1c02017-04-24 23:14:04 +00001309 target->GetImages().FindFunctions(name, eFunctionNameTypeFull,
1310 include_symbols, include_inlines,
1311 append, sc_list);
1312 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001313
Adrian Prantl05097242018-04-30 16:49:04 +00001314 // If we found more than one function, see if we can use the frame's decl
1315 // context to remove functions that are shadowed by other functions which
1316 // match in type but are nearer in scope.
Sean Callananc02a1c02017-04-24 23:14:04 +00001317 //
1318 // AddOneFunction will not add a function whose type has already been
Adrian Prantl05097242018-04-30 16:49:04 +00001319 // added, so if there's another function in the list with a matching type,
1320 // check to see if their decl context is a parent of the current frame's or
1321 // was imported via a and using statement, and pick the best match
1322 // according to lookup rules.
Sean Callananc02a1c02017-04-24 23:14:04 +00001323 if (sc_list.GetSize() > 1) {
1324 // Collect some info about our frame's context.
1325 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1326 SymbolContext frame_sym_ctx;
1327 if (frame != nullptr)
1328 frame_sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
1329 lldb::eSymbolContextBlock);
1330 CompilerDeclContext frame_decl_context =
1331 frame_sym_ctx.block != nullptr ? frame_sym_ctx.block->GetDeclContext()
1332 : CompilerDeclContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001333
Sean Callananc02a1c02017-04-24 23:14:04 +00001334 // We can't do this without a compiler decl context for our frame.
1335 if (frame_decl_context) {
1336 clang::DeclContext *frame_decl_ctx =
1337 (clang::DeclContext *)frame_decl_context.GetOpaqueDeclContext();
1338 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(
1339 frame_decl_context.GetTypeSystem());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001340
Sean Callananc02a1c02017-04-24 23:14:04 +00001341 // Structure to hold the info needed when comparing function
1342 // declarations.
1343 struct FuncDeclInfo {
1344 ConstString m_name;
1345 CompilerType m_copied_type;
1346 uint32_t m_decl_lvl;
1347 SymbolContext m_sym_ctx;
1348 };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001349
Adrian Prantl05097242018-04-30 16:49:04 +00001350 // First, symplify things by looping through the symbol contexts to
1351 // remove unwanted functions and separate out the functions we want to
1352 // compare and prune into a separate list. Cache the info needed about
1353 // the function declarations in a vector for efficiency.
Sean Callananc02a1c02017-04-24 23:14:04 +00001354 SymbolContextList sc_sym_list;
1355 uint32_t num_indices = sc_list.GetSize();
1356 std::vector<FuncDeclInfo> fdi_cache;
1357 fdi_cache.reserve(num_indices);
1358 for (uint32_t index = 0; index < num_indices; ++index) {
1359 FuncDeclInfo fdi;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001360 SymbolContext sym_ctx;
1361 sc_list.GetContextAtIndex(index, sym_ctx);
1362
Adrian Prantl05097242018-04-30 16:49:04 +00001363 // We don't know enough about symbols to compare them, but we should
1364 // keep them in the list.
Sean Callananc02a1c02017-04-24 23:14:04 +00001365 Function *function = sym_ctx.function;
1366 if (!function) {
1367 sc_sym_list.Append(sym_ctx);
1368 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001369 }
Sean Callananc02a1c02017-04-24 23:14:04 +00001370 // Filter out functions without declaration contexts, as well as
Adrian Prantl05097242018-04-30 16:49:04 +00001371 // class/instance methods, since they'll be skipped in the code that
1372 // follows anyway.
Sean Callananc02a1c02017-04-24 23:14:04 +00001373 CompilerDeclContext func_decl_context = function->GetDeclContext();
1374 if (!func_decl_context ||
1375 func_decl_context.IsClassMethod(nullptr, nullptr, nullptr))
1376 continue;
1377 // We can only prune functions for which we can copy the type.
1378 CompilerType func_clang_type =
1379 function->GetType()->GetFullCompilerType();
1380 CompilerType copied_func_type = GuardedCopyType(func_clang_type);
1381 if (!copied_func_type) {
1382 sc_sym_list.Append(sym_ctx);
1383 continue;
1384 }
1385
1386 fdi.m_sym_ctx = sym_ctx;
1387 fdi.m_name = function->GetName();
1388 fdi.m_copied_type = copied_func_type;
1389 fdi.m_decl_lvl = LLDB_INVALID_DECL_LEVEL;
1390 if (fdi.m_copied_type && func_decl_context) {
Adrian Prantl05097242018-04-30 16:49:04 +00001391 // Call CountDeclLevels to get the number of parent scopes we have
1392 // to look through before we find the function declaration. When
1393 // comparing functions of the same type, the one with a lower count
1394 // will be closer to us in the lookup scope and shadows the other.
Sean Callananc02a1c02017-04-24 23:14:04 +00001395 clang::DeclContext *func_decl_ctx =
1396 (clang::DeclContext *)func_decl_context.GetOpaqueDeclContext();
1397 fdi.m_decl_lvl = ast->CountDeclLevels(
1398 frame_decl_ctx, func_decl_ctx, &fdi.m_name, &fdi.m_copied_type);
1399 }
1400 fdi_cache.emplace_back(fdi);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001401 }
1402
Sean Callananc02a1c02017-04-24 23:14:04 +00001403 // Loop through the functions in our cache looking for matching types,
1404 // then compare their scope levels to see which is closer.
1405 std::multimap<CompilerType, const FuncDeclInfo *> matches;
1406 for (const FuncDeclInfo &fdi : fdi_cache) {
1407 const CompilerType t = fdi.m_copied_type;
1408 auto q = matches.find(t);
1409 if (q != matches.end()) {
1410 if (q->second->m_decl_lvl > fdi.m_decl_lvl)
1411 // This function is closer; remove the old set.
1412 matches.erase(t);
1413 else if (q->second->m_decl_lvl < fdi.m_decl_lvl)
1414 // The functions in our set are closer - skip this one.
1415 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001416 }
Sean Callananc02a1c02017-04-24 23:14:04 +00001417 matches.insert(std::make_pair(t, &fdi));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418 }
1419
Sean Callananc02a1c02017-04-24 23:14:04 +00001420 // Loop through our matches and add their symbol contexts to our list.
1421 SymbolContextList sc_func_list;
1422 for (const auto &q : matches)
1423 sc_func_list.Append(q.second->m_sym_ctx);
1424
1425 // Rejoin the lists with the functions in front.
1426 sc_list = sc_func_list;
1427 sc_list.Append(sc_sym_list);
1428 }
1429 }
1430
1431 if (sc_list.GetSize()) {
1432 Symbol *extern_symbol = NULL;
1433 Symbol *non_extern_symbol = NULL;
1434
1435 for (uint32_t index = 0, num_indices = sc_list.GetSize();
1436 index < num_indices; ++index) {
1437 SymbolContext sym_ctx;
1438 sc_list.GetContextAtIndex(index, sym_ctx);
1439
1440 if (sym_ctx.function) {
1441 CompilerDeclContext decl_ctx = sym_ctx.function->GetDeclContext();
1442
1443 if (!decl_ctx)
1444 continue;
1445
1446 // Filter out class/instance methods.
1447 if (decl_ctx.IsClassMethod(nullptr, nullptr, nullptr))
1448 continue;
1449
1450 AddOneFunction(context, sym_ctx.function, NULL, current_id);
1451 context.m_found.function_with_type_info = true;
1452 context.m_found.function = true;
1453 } else if (sym_ctx.symbol) {
1454 if (sym_ctx.symbol->GetType() == eSymbolTypeReExported && target) {
1455 sym_ctx.symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target);
1456 if (sym_ctx.symbol == NULL)
1457 continue;
1458 }
1459
1460 if (sym_ctx.symbol->IsExternal())
1461 extern_symbol = sym_ctx.symbol;
1462 else
1463 non_extern_symbol = sym_ctx.symbol;
1464 }
1465 }
1466
1467 if (!context.m_found.function_with_type_info) {
1468 for (clang::NamedDecl *decl : decls_from_modules) {
1469 if (llvm::isa<clang::FunctionDecl>(decl)) {
1470 clang::NamedDecl *copied_decl =
Sean Callanan68e44232017-09-28 20:20:25 +00001471 llvm::cast_or_null<FunctionDecl>(CopyDecl(decl));
Sean Callananc02a1c02017-04-24 23:14:04 +00001472 if (copied_decl) {
1473 context.AddNamedDecl(copied_decl);
1474 context.m_found.function_with_type_info = true;
1475 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001476 }
1477 }
1478 }
1479
1480 if (!context.m_found.function_with_type_info) {
Sean Callananc02a1c02017-04-24 23:14:04 +00001481 if (extern_symbol) {
1482 AddOneFunction(context, NULL, extern_symbol, current_id);
1483 context.m_found.function = true;
1484 } else if (non_extern_symbol) {
1485 AddOneFunction(context, NULL, non_extern_symbol, current_id);
1486 context.m_found.function = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001487 }
1488 }
Sean Callananb3a36df2016-03-19 00:51:43 +00001489 }
Sean Callananc02a1c02017-04-24 23:14:04 +00001490
1491 if (!context.m_found.function_with_type_info) {
1492 // Try the modules next.
1493
1494 do {
1495 if (ClangModulesDeclVendor *modules_decl_vendor =
1496 m_target->GetClangModulesDeclVendor()) {
1497 bool append = false;
1498 uint32_t max_matches = 1;
1499 std::vector<clang::NamedDecl *> decls;
1500
1501 if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls))
1502 break;
1503
1504 clang::NamedDecl *const decl_from_modules = decls[0];
1505
1506 if (llvm::isa<clang::FunctionDecl>(decl_from_modules)) {
1507 if (log) {
1508 log->Printf(" CAS::FEVD[%u] Matching function found for "
1509 "\"%s\" in the modules",
1510 current_id, name.GetCString());
1511 }
1512
Sean Callanan68e44232017-09-28 20:20:25 +00001513 clang::Decl *copied_decl = CopyDecl(decl_from_modules);
Sean Callananc02a1c02017-04-24 23:14:04 +00001514 clang::FunctionDecl *copied_function_decl =
1515 copied_decl ? dyn_cast<clang::FunctionDecl>(copied_decl)
1516 : nullptr;
1517
1518 if (!copied_function_decl) {
1519 if (log)
1520 log->Printf(" CAS::FEVD[%u] - Couldn't export a function "
1521 "declaration from the modules",
1522 current_id);
1523
1524 break;
1525 }
1526
1527 MaybeRegisterFunctionBody(copied_function_decl);
1528
1529 context.AddNamedDecl(copied_function_decl);
1530
1531 context.m_found.function_with_type_info = true;
1532 context.m_found.function = true;
1533 } else if (llvm::isa<clang::VarDecl>(decl_from_modules)) {
1534 if (log) {
1535 log->Printf(" CAS::FEVD[%u] Matching variable found for "
1536 "\"%s\" in the modules",
1537 current_id, name.GetCString());
1538 }
1539
Sean Callanan68e44232017-09-28 20:20:25 +00001540 clang::Decl *copied_decl = CopyDecl(decl_from_modules);
Sean Callananc02a1c02017-04-24 23:14:04 +00001541 clang::VarDecl *copied_var_decl =
1542 copied_decl ? dyn_cast_or_null<clang::VarDecl>(copied_decl)
1543 : nullptr;
1544
1545 if (!copied_var_decl) {
1546 if (log)
1547 log->Printf(" CAS::FEVD[%u] - Couldn't export a variable "
1548 "declaration from the modules",
1549 current_id);
1550
1551 break;
1552 }
1553
1554 context.AddNamedDecl(copied_var_decl);
1555
1556 context.m_found.variable = true;
1557 }
1558 }
1559 } while (0);
1560 }
1561
1562 if (target && !context.m_found.variable && !namespace_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001563 // We couldn't find a non-symbol variable for this. Now we'll hunt for a
1564 // generic data symbol, and -- if it is found -- treat it as a variable.
Sean Callanan9c99faa2017-05-16 23:46:13 +00001565 Status error;
Stephane Sezer30fa7e22017-10-24 22:56:05 +00001566
Sean Callanan9c99faa2017-05-16 23:46:13 +00001567 const Symbol *data_symbol =
1568 m_parser_vars->m_sym_ctx.FindBestGlobalDataSymbol(name, error);
Stephane Sezer30fa7e22017-10-24 22:56:05 +00001569
Sean Callanan9c99faa2017-05-16 23:46:13 +00001570 if (!error.Success()) {
1571 const unsigned diag_id =
1572 m_ast_context->getDiagnostics().getCustomDiagID(
1573 clang::DiagnosticsEngine::Level::Error, "%0");
1574 m_ast_context->getDiagnostics().Report(diag_id) << error.AsCString();
1575 }
Stephane Sezer30fa7e22017-10-24 22:56:05 +00001576
Sean Callananc02a1c02017-04-24 23:14:04 +00001577 if (data_symbol) {
1578 std::string warning("got name from symbols: ");
1579 warning.append(name.AsCString());
1580 const unsigned diag_id =
1581 m_ast_context->getDiagnostics().getCustomDiagID(
1582 clang::DiagnosticsEngine::Level::Warning, "%0");
1583 m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str();
1584 AddOneGenericVariable(context, *data_symbol, current_id);
1585 context.m_found.variable = true;
1586 }
1587 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001588 }
Sean Callanan6b4067c2010-07-17 00:43:37 +00001589}
Greg Claytona2721472011-06-25 00:44:06 +00001590
Kate Stoneb9c1b512016-09-06 20:57:50 +00001591bool ClangExpressionDeclMap::GetVariableValue(VariableSP &var,
1592 lldb_private::Value &var_location,
1593 TypeFromUser *user_type,
1594 TypeFromParser *parser_type) {
1595 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001596
Kate Stoneb9c1b512016-09-06 20:57:50 +00001597 Type *var_type = var->GetType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001598
Kate Stoneb9c1b512016-09-06 20:57:50 +00001599 if (!var_type) {
Sean Callananea22d422010-07-16 00:09:46 +00001600 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001601 log->PutCString("Skipped a definition because it has no type");
1602 return false;
1603 }
1604
1605 CompilerType var_clang_type = var_type->GetFullCompilerType();
1606
1607 if (!var_clang_type) {
1608 if (log)
1609 log->PutCString("Skipped a definition because it has no Clang type");
1610 return false;
1611 }
1612
1613 ClangASTContext *clang_ast = llvm::dyn_cast_or_null<ClangASTContext>(
1614 var_type->GetForwardCompilerType().GetTypeSystem());
1615
1616 if (!clang_ast) {
1617 if (log)
1618 log->PutCString("Skipped a definition because it has no Clang AST");
1619 return false;
1620 }
1621
1622 ASTContext *ast = clang_ast->getASTContext();
1623
1624 if (!ast) {
1625 if (log)
1626 log->PutCString(
1627 "There is no AST context for the current execution context");
1628 return false;
1629 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001630
1631 DWARFExpression &var_location_expr = var->LocationExpression();
1632
1633 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
Zachary Turner97206d52017-05-12 04:51:55 +00001634 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001635
1636 if (var->GetLocationIsConstantValueData()) {
1637 DataExtractor const_value_extractor;
1638
1639 if (var_location_expr.GetExpressionData(const_value_extractor)) {
1640 var_location = Value(const_value_extractor.GetDataStart(),
1641 const_value_extractor.GetByteSize());
1642 var_location.SetValueType(Value::eValueTypeHostAddress);
1643 } else {
1644 if (log)
1645 log->Printf("Error evaluating constant variable: %s", err.AsCString());
1646 return false;
Greg Clayton7b462cc2010-10-15 22:48:33 +00001647 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001648 }
1649
1650 CompilerType type_to_use = GuardedCopyType(var_clang_type);
1651
1652 if (!type_to_use) {
1653 if (log)
1654 log->Printf(
1655 "Couldn't copy a variable's type into the parser's AST context");
1656
1657 return false;
1658 }
1659
1660 if (parser_type)
1661 *parser_type = TypeFromParser(type_to_use);
1662
1663 if (var_location.GetContextType() == Value::eContextTypeInvalid)
1664 var_location.SetCompilerType(type_to_use);
1665
1666 if (var_location.GetValueType() == Value::eValueTypeFileAddress) {
1667 SymbolContext var_sc;
1668 var->CalculateSymbolContext(&var_sc);
1669
1670 if (!var_sc.module_sp)
1671 return false;
1672
1673 Address so_addr(var_location.GetScalar().ULongLong(),
1674 var_sc.module_sp->GetSectionList());
1675
1676 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
1677
1678 if (load_addr != LLDB_INVALID_ADDRESS) {
1679 var_location.GetScalar() = load_addr;
1680 var_location.SetValueType(Value::eValueTypeLoadAddress);
1681 }
1682 }
1683
1684 if (user_type)
1685 *user_type = TypeFromUser(var_clang_type);
1686
1687 return true;
Sean Callanan468574b2010-06-22 23:46:24 +00001688}
1689
Kate Stoneb9c1b512016-09-06 20:57:50 +00001690void ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
1691 VariableSP var,
1692 ValueObjectSP valobj,
1693 unsigned int current_id) {
1694 assert(m_parser_vars.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001695
Kate Stoneb9c1b512016-09-06 20:57:50 +00001696 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001697
Kate Stoneb9c1b512016-09-06 20:57:50 +00001698 TypeFromUser ut;
1699 TypeFromParser pt;
1700 Value var_location;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001701
Kate Stoneb9c1b512016-09-06 20:57:50 +00001702 if (!GetVariableValue(var, var_location, &ut, &pt))
1703 return;
1704
1705 clang::QualType parser_opaque_type =
1706 QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
1707
1708 if (parser_opaque_type.isNull())
1709 return;
1710
1711 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr()) {
1712 if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1713 CompleteType(tag_type->getDecl());
1714 if (const ObjCObjectPointerType *objc_object_ptr_type =
1715 dyn_cast<ObjCObjectPointerType>(parser_type))
1716 CompleteType(objc_object_ptr_type->getInterfaceDecl());
1717 }
1718
1719 bool is_reference = pt.IsReferenceType();
1720
1721 NamedDecl *var_decl = NULL;
1722 if (is_reference)
1723 var_decl = context.AddVarDecl(pt);
1724 else
1725 var_decl = context.AddVarDecl(pt.GetLValueReferenceType());
1726
1727 std::string decl_name(context.m_decl_name.getAsString());
1728 ConstString entity_name(decl_name.c_str());
1729 ClangExpressionVariable *entity(new ClangExpressionVariable(valobj));
1730 m_found_entities.AddNewlyConstructedVariable(entity);
1731
1732 assert(entity);
1733 entity->EnableParserVars(GetParserID());
1734 ClangExpressionVariable::ParserVars *parser_vars =
1735 entity->GetParserVars(GetParserID());
1736 parser_vars->m_parser_type = pt;
1737 parser_vars->m_named_decl = var_decl;
1738 parser_vars->m_llvm_value = NULL;
1739 parser_vars->m_lldb_value = var_location;
1740 parser_vars->m_lldb_var = var;
1741
1742 if (is_reference)
1743 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference;
1744
1745 if (log) {
1746 ASTDumper orig_dumper(ut.GetOpaqueQualType());
1747 ASTDumper ast_dumper(var_decl);
1748 log->Printf(" CEDM::FEVD[%u] Found variable %s, returned %s (original %s)",
1749 current_id, decl_name.c_str(), ast_dumper.GetCString(),
1750 orig_dumper.GetCString());
1751 }
1752}
1753
1754void ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
1755 ExpressionVariableSP &pvar_sp,
1756 unsigned int current_id) {
1757 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1758
1759 TypeFromUser user_type(
1760 llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetTypeFromUser());
1761
1762 TypeFromParser parser_type(GuardedCopyType(user_type));
1763
1764 if (!parser_type.GetOpaqueQualType()) {
1765 if (log)
1766 log->Printf(" CEDM::FEVD[%u] Couldn't import type for pvar %s",
1767 current_id, pvar_sp->GetName().GetCString());
1768 return;
1769 }
1770
1771 NamedDecl *var_decl =
1772 context.AddVarDecl(parser_type.GetLValueReferenceType());
1773
1774 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1775 ->EnableParserVars(GetParserID());
1776 ClangExpressionVariable::ParserVars *parser_vars =
1777 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1778 ->GetParserVars(GetParserID());
1779 parser_vars->m_parser_type = parser_type;
1780 parser_vars->m_named_decl = var_decl;
1781 parser_vars->m_llvm_value = NULL;
1782 parser_vars->m_lldb_value.Clear();
1783
1784 if (log) {
1785 ASTDumper ast_dumper(var_decl);
1786 log->Printf(" CEDM::FEVD[%u] Added pvar %s, returned %s", current_id,
1787 pvar_sp->GetName().GetCString(), ast_dumper.GetCString());
1788 }
1789}
1790
1791void ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context,
1792 const Symbol &symbol,
1793 unsigned int current_id) {
1794 assert(m_parser_vars.get());
1795
1796 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1797
1798 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1799
1800 if (target == NULL)
1801 return;
1802
1803 ASTContext *scratch_ast_context =
1804 target->GetScratchClangASTContext()->getASTContext();
1805
1806 TypeFromUser user_type(
1807 ClangASTContext::GetBasicType(scratch_ast_context, eBasicTypeVoid)
1808 .GetPointerType()
1809 .GetLValueReferenceType());
1810 TypeFromParser parser_type(
1811 ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid)
1812 .GetPointerType()
1813 .GetLValueReferenceType());
1814 NamedDecl *var_decl = context.AddVarDecl(parser_type);
1815
1816 std::string decl_name(context.m_decl_name.getAsString());
1817 ConstString entity_name(decl_name.c_str());
1818 ClangExpressionVariable *entity(new ClangExpressionVariable(
1819 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), entity_name,
1820 user_type, m_parser_vars->m_target_info.byte_order,
1821 m_parser_vars->m_target_info.address_byte_size));
1822 m_found_entities.AddNewlyConstructedVariable(entity);
1823
1824 entity->EnableParserVars(GetParserID());
1825 ClangExpressionVariable::ParserVars *parser_vars =
1826 entity->GetParserVars(GetParserID());
1827
1828 const Address symbol_address = symbol.GetAddress();
1829 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target);
1830
1831 // parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType,
1832 // user_type.GetOpaqueQualType());
1833 parser_vars->m_lldb_value.SetCompilerType(user_type);
1834 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr;
1835 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1836
1837 parser_vars->m_parser_type = parser_type;
1838 parser_vars->m_named_decl = var_decl;
1839 parser_vars->m_llvm_value = NULL;
1840 parser_vars->m_lldb_sym = &symbol;
1841
1842 if (log) {
1843 ASTDumper ast_dumper(var_decl);
1844
1845 log->Printf(" CEDM::FEVD[%u] Found variable %s, returned %s", current_id,
1846 decl_name.c_str(), ast_dumper.GetCString());
1847 }
1848}
1849
1850bool ClangExpressionDeclMap::ResolveUnknownTypes() {
1851 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1852 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1853
Sean Callanan68e44232017-09-28 20:20:25 +00001854 ClangASTContextForExpressions *scratch_ast_context =
1855 static_cast<ClangASTContextForExpressions*>(
1856 target->GetScratchClangASTContext());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001857
1858 for (size_t index = 0, num_entities = m_found_entities.GetSize();
1859 index < num_entities; ++index) {
1860 ExpressionVariableSP entity = m_found_entities.GetVariableAtIndex(index);
1861
1862 ClangExpressionVariable::ParserVars *parser_vars =
1863 llvm::cast<ClangExpressionVariable>(entity.get())
1864 ->GetParserVars(GetParserID());
1865
1866 if (entity->m_flags & ClangExpressionVariable::EVUnknownType) {
1867 const NamedDecl *named_decl = parser_vars->m_named_decl;
1868 const VarDecl *var_decl = dyn_cast<VarDecl>(named_decl);
1869
1870 if (!var_decl) {
Sean Callanane0a64f72011-12-01 21:04:37 +00001871 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001872 log->Printf("Entity of unknown type does not have a VarDecl");
1873 return false;
1874 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001875
Kate Stoneb9c1b512016-09-06 20:57:50 +00001876 if (log) {
1877 ASTDumper ast_dumper(const_cast<VarDecl *>(var_decl));
1878 log->Printf("Variable of unknown type now has Decl %s",
Greg Clayton64bc6ca2011-10-20 00:47:21 +00001879 ast_dumper.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001880 }
1881
1882 QualType var_type = var_decl->getType();
1883 TypeFromParser parser_type(
1884 var_type.getAsOpaquePtr(),
1885 ClangASTContext::GetASTContext(&var_decl->getASTContext()));
1886
Sean Callanan68e44232017-09-28 20:20:25 +00001887 lldb::opaque_compiler_type_t copied_type = 0;
1888 if (m_ast_importer_sp) {
1889 copied_type = m_ast_importer_sp->CopyType(
1890 scratch_ast_context->getASTContext(), &var_decl->getASTContext(),
1891 var_type.getAsOpaquePtr());
1892 } else if (HasMerger()) {
1893 copied_type = CopyTypeWithMerger(
1894 var_decl->getASTContext(),
1895 scratch_ast_context->GetMergerUnchecked(),
1896 var_type).getAsOpaquePtr();
1897 } else {
Pavel Labath9344e452017-10-17 21:52:29 +00001898 lldbassert(0 && "No mechanism to copy a resolved unknown type!");
Sean Callanan68e44232017-09-28 20:20:25 +00001899 return false;
1900 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001901
1902 if (!copied_type) {
1903 if (log)
1904 log->Printf("ClangExpressionDeclMap::ResolveUnknownType - Couldn't "
1905 "import the type for a variable");
1906
1907 return (bool)lldb::ExpressionVariableSP();
1908 }
1909
1910 TypeFromUser user_type(copied_type, scratch_ast_context);
1911
1912 // parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType,
1913 // user_type.GetOpaqueQualType());
1914 parser_vars->m_lldb_value.SetCompilerType(user_type);
1915 parser_vars->m_parser_type = parser_type;
1916
1917 entity->SetCompilerType(user_type);
1918
1919 entity->m_flags &= ~(ClangExpressionVariable::EVUnknownType);
Greg Clayton7b462cc2010-10-15 22:48:33 +00001920 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001921 }
1922
1923 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001924}
Sean Callanan5666b672010-08-04 01:02:13 +00001925
Kate Stoneb9c1b512016-09-06 20:57:50 +00001926void ClangExpressionDeclMap::AddOneRegister(NameSearchContext &context,
1927 const RegisterInfo *reg_info,
1928 unsigned int current_id) {
1929 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001930
Kate Stoneb9c1b512016-09-06 20:57:50 +00001931 CompilerType clang_type =
1932 ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1933 m_ast_context, reg_info->encoding, reg_info->byte_size * 8);
Sean Callanan7736a202016-04-29 18:09:03 +00001934
Kate Stoneb9c1b512016-09-06 20:57:50 +00001935 if (!clang_type) {
1936 if (log)
1937 log->Printf(" Tried to add a type for %s, but couldn't get one",
1938 context.m_decl_name.getAsString().c_str());
1939 return;
1940 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001941
Kate Stoneb9c1b512016-09-06 20:57:50 +00001942 TypeFromParser parser_clang_type(clang_type);
Sean Callananfa4fab72013-02-01 06:55:48 +00001943
Kate Stoneb9c1b512016-09-06 20:57:50 +00001944 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001945
Kate Stoneb9c1b512016-09-06 20:57:50 +00001946 ClangExpressionVariable *entity(new ClangExpressionVariable(
1947 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1948 m_parser_vars->m_target_info.byte_order,
1949 m_parser_vars->m_target_info.address_byte_size));
1950 m_found_entities.AddNewlyConstructedVariable(entity);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001951
Kate Stoneb9c1b512016-09-06 20:57:50 +00001952 std::string decl_name(context.m_decl_name.getAsString());
1953 entity->SetName(ConstString(decl_name.c_str()));
1954 entity->SetRegisterInfo(reg_info);
1955 entity->EnableParserVars(GetParserID());
1956 ClangExpressionVariable::ParserVars *parser_vars =
1957 entity->GetParserVars(GetParserID());
1958 parser_vars->m_parser_type = parser_clang_type;
1959 parser_vars->m_named_decl = var_decl;
1960 parser_vars->m_llvm_value = NULL;
1961 parser_vars->m_lldb_value.Clear();
1962 entity->m_flags |= ClangExpressionVariable::EVBareRegister;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001963
Kate Stoneb9c1b512016-09-06 20:57:50 +00001964 if (log) {
1965 ASTDumper ast_dumper(var_decl);
1966 log->Printf(" CEDM::FEVD[%d] Added register %s, returned %s", current_id,
1967 context.m_decl_name.getAsString().c_str(),
1968 ast_dumper.GetCString());
1969 }
1970}
1971
1972void ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context,
1973 Function *function, Symbol *symbol,
1974 unsigned int current_id) {
1975 assert(m_parser_vars.get());
1976
1977 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1978
1979 NamedDecl *function_decl = NULL;
1980 Address fun_address;
1981 CompilerType function_clang_type;
1982
1983 bool is_indirect_function = false;
1984
1985 if (function) {
1986 Type *function_type = function->GetType();
1987
Luke Drummondf5bb1d62016-12-19 17:22:44 +00001988 const auto lang = function->GetCompileUnit()->GetLanguage();
1989 const auto name = function->GetMangled().GetMangledName().AsCString();
1990 const bool extern_c = (Language::LanguageIsC(lang) &&
1991 !CPlusPlusLanguage::IsCPPMangledName(name)) ||
1992 (Language::LanguageIsObjC(lang) &&
1993 !Language::LanguageIsCPlusPlus(lang));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001994
1995 if (!extern_c) {
1996 TypeSystem *type_system = function->GetDeclContext().GetTypeSystem();
Davide Italianofe34df52017-09-30 21:16:56 +00001997 if (llvm::isa<ClangASTContext>(type_system)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001998 clang::DeclContext *src_decl_context =
1999 (clang::DeclContext *)function->GetDeclContext()
2000 .GetOpaqueDeclContext();
2001 clang::FunctionDecl *src_function_decl =
2002 llvm::dyn_cast_or_null<clang::FunctionDecl>(src_decl_context);
Sean Callanan09e91ac2017-05-11 22:08:05 +00002003 if (src_function_decl &&
2004 src_function_decl->getTemplateSpecializationInfo()) {
2005 clang::FunctionTemplateDecl *function_template =
2006 src_function_decl->getTemplateSpecializationInfo()->getTemplate();
2007 clang::FunctionTemplateDecl *copied_function_template =
2008 llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(
Sean Callanan68e44232017-09-28 20:20:25 +00002009 CopyDecl(function_template));
Sean Callanan09e91ac2017-05-11 22:08:05 +00002010 if (copied_function_template) {
2011 if (log) {
2012 ASTDumper ast_dumper((clang::Decl *)copied_function_template);
2013
2014 StreamString ss;
2015
2016 function->DumpSymbolContext(&ss);
2017
2018 log->Printf(" CEDM::FEVD[%u] Imported decl for function template"
2019 " %s (description %s), returned %s",
2020 current_id,
2021 copied_function_template->getNameAsString().c_str(),
2022 ss.GetData(), ast_dumper.GetCString());
2023 }
2024
2025 context.AddNamedDecl(copied_function_template);
2026 }
2027 } else if (src_function_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002028 if (clang::FunctionDecl *copied_function_decl =
2029 llvm::dyn_cast_or_null<clang::FunctionDecl>(
Sean Callanan68e44232017-09-28 20:20:25 +00002030 CopyDecl(src_function_decl))) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002031 if (log) {
2032 ASTDumper ast_dumper((clang::Decl *)copied_function_decl);
2033
2034 StreamString ss;
2035
2036 function->DumpSymbolContext(&ss);
2037
2038 log->Printf(" CEDM::FEVD[%u] Imported decl for function %s "
2039 "(description %s), returned %s",
2040 current_id,
2041 copied_function_decl->getNameAsString().c_str(),
2042 ss.GetData(), ast_dumper.GetCString());
2043 }
2044
2045 context.AddNamedDecl(copied_function_decl);
2046 return;
2047 } else {
2048 if (log) {
2049 log->Printf(" Failed to import the function decl for '%s'",
2050 src_function_decl->getName().str().c_str());
2051 }
2052 }
Sean Callanan7736a202016-04-29 18:09:03 +00002053 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002054 }
Sean Callananfc55f5d2010-09-21 00:44:12 +00002055 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002056
Kate Stoneb9c1b512016-09-06 20:57:50 +00002057 if (!function_type) {
2058 if (log)
2059 log->PutCString(" Skipped a function because it has no type");
2060 return;
2061 }
2062
2063 function_clang_type = function_type->GetFullCompilerType();
2064
2065 if (!function_clang_type) {
2066 if (log)
2067 log->PutCString(" Skipped a function because it has no Clang type");
2068 return;
2069 }
2070
2071 fun_address = function->GetAddressRange().GetBaseAddress();
2072
2073 CompilerType copied_function_type = GuardedCopyType(function_clang_type);
2074 if (copied_function_type) {
2075 function_decl = context.AddFunDecl(copied_function_type, extern_c);
2076
2077 if (!function_decl) {
2078 if (log) {
2079 log->Printf(
2080 " Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}",
2081 function_type->GetName().GetCString(), function_type->GetID());
2082 }
2083
Sean Callanan3b436d22015-10-23 21:45:02 +00002084 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002085 }
2086 } else {
2087 // We failed to copy the type we found
2088 if (log) {
2089 log->Printf(" Failed to import the function type '%s' {0x%8.8" PRIx64
2090 "} into the expression parser AST contenxt",
2091 function_type->GetName().GetCString(),
2092 function_type->GetID());
2093 }
2094
2095 return;
2096 }
2097 } else if (symbol) {
2098 fun_address = symbol->GetAddress();
2099 function_decl = context.AddGenericFunDecl();
2100 is_indirect_function = symbol->IsIndirect();
2101 } else {
2102 if (log)
2103 log->PutCString(" AddOneFunction called with no function and no symbol");
2104 return;
2105 }
2106
2107 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
2108
2109 lldb::addr_t load_addr =
2110 fun_address.GetCallableLoadAddress(target, is_indirect_function);
2111
2112 ClangExpressionVariable *entity(new ClangExpressionVariable(
2113 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
2114 m_parser_vars->m_target_info.byte_order,
2115 m_parser_vars->m_target_info.address_byte_size));
2116 m_found_entities.AddNewlyConstructedVariable(entity);
2117
2118 std::string decl_name(context.m_decl_name.getAsString());
2119 entity->SetName(ConstString(decl_name.c_str()));
2120 entity->SetCompilerType(function_clang_type);
2121 entity->EnableParserVars(GetParserID());
2122
2123 ClangExpressionVariable::ParserVars *parser_vars =
2124 entity->GetParserVars(GetParserID());
2125
2126 if (load_addr != LLDB_INVALID_ADDRESS) {
2127 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
2128 parser_vars->m_lldb_value.GetScalar() = load_addr;
2129 } else {
2130 // We have to try finding a file address.
2131
2132 lldb::addr_t file_addr = fun_address.GetFileAddress();
2133
2134 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress);
2135 parser_vars->m_lldb_value.GetScalar() = file_addr;
2136 }
2137
2138 parser_vars->m_named_decl = function_decl;
2139 parser_vars->m_llvm_value = NULL;
2140
2141 if (log) {
Pavel Labath03a77e92018-03-21 11:10:57 +00002142 std::string function_str =
2143 function_decl ? ASTDumper(function_decl).GetCString() : "nullptr";
Kate Stoneb9c1b512016-09-06 20:57:50 +00002144
2145 StreamString ss;
2146
2147 fun_address.Dump(&ss,
2148 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
2149 Address::DumpStyleResolvedDescription);
2150
2151 log->Printf(
2152 " CEDM::FEVD[%u] Found %s function %s (description %s), returned %s",
2153 current_id, (function ? "specific" : "generic"), decl_name.c_str(),
Pavel Labath03a77e92018-03-21 11:10:57 +00002154 ss.GetData(), function_str.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002155 }
2156}
2157
2158void ClangExpressionDeclMap::AddThisType(NameSearchContext &context,
Aleksandr Urakov40624a02019-02-05 09:14:36 +00002159 const TypeFromUser &ut,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002160 unsigned int current_id) {
2161 CompilerType copied_clang_type = GuardedCopyType(ut);
2162
2163 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
2164
2165 if (!copied_clang_type) {
2166 if (log)
2167 log->Printf(
2168 "ClangExpressionDeclMap::AddThisType - Couldn't import the type");
Sean Callanan3b436d22015-10-23 21:45:02 +00002169
2170 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002171 }
2172
2173 if (copied_clang_type.IsAggregateType() &&
2174 copied_clang_type.GetCompleteType()) {
2175 CompilerType void_clang_type =
2176 ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid);
2177 CompilerType void_ptr_clang_type = void_clang_type.GetPointerType();
2178
2179 CompilerType method_type = ClangASTContext::CreateFunctionType(
2180 m_ast_context, void_clang_type, &void_ptr_clang_type, 1, false, 0);
2181
2182 const bool is_virtual = false;
2183 const bool is_static = false;
2184 const bool is_inline = false;
2185 const bool is_explicit = false;
2186 const bool is_attr_used = true;
2187 const bool is_artificial = false;
2188
2189 CXXMethodDecl *method_decl =
2190 ClangASTContext::GetASTContext(m_ast_context)
2191 ->AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00002192 copied_clang_type.GetOpaqueQualType(), "$__lldb_expr", NULL,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002193 method_type, lldb::eAccessPublic, is_virtual, is_static,
2194 is_inline, is_explicit, is_attr_used, is_artificial);
2195
2196 if (log) {
2197 ASTDumper method_ast_dumper((clang::Decl *)method_decl);
2198 ASTDumper type_ast_dumper(copied_clang_type);
2199
2200 log->Printf(" CEDM::AddThisType Added function $__lldb_expr "
2201 "(description %s) for this type %s",
2202 method_ast_dumper.GetCString(), type_ast_dumper.GetCString());
2203 }
2204 }
2205
2206 if (!copied_clang_type.IsValid())
2207 return;
2208
2209 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(
2210 QualType::getFromOpaquePtr(copied_clang_type.GetOpaqueQualType()));
2211
2212 if (!type_source_info)
2213 return;
2214
2215 // Construct a typedef type because if "*this" is a templated type we can't
2216 // just return ClassTemplateSpecializationDecls in response to name queries.
2217 // Using a typedef makes this much more robust.
2218
2219 TypedefDecl *typedef_decl = TypedefDecl::Create(
2220 *m_ast_context, m_ast_context->getTranslationUnitDecl(), SourceLocation(),
2221 SourceLocation(), context.m_decl_name.getAsIdentifierInfo(),
2222 type_source_info);
2223
2224 if (!typedef_decl)
2225 return;
2226
2227 context.AddNamedDecl(typedef_decl);
2228
2229 return;
Sean Callananfa4fab72013-02-01 06:55:48 +00002230}
2231
Kate Stoneb9c1b512016-09-06 20:57:50 +00002232void ClangExpressionDeclMap::AddOneType(NameSearchContext &context,
Aleksandr Urakov40624a02019-02-05 09:14:36 +00002233 const TypeFromUser &ut,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002234 unsigned int current_id) {
2235 CompilerType copied_clang_type = GuardedCopyType(ut);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002236
Kate Stoneb9c1b512016-09-06 20:57:50 +00002237 if (!copied_clang_type) {
2238 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfa4fab72013-02-01 06:55:48 +00002239
Kate Stoneb9c1b512016-09-06 20:57:50 +00002240 if (log)
2241 log->Printf(
2242 "ClangExpressionDeclMap::AddOneType - Couldn't import the type");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002243
Kate Stoneb9c1b512016-09-06 20:57:50 +00002244 return;
2245 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002246
Kate Stoneb9c1b512016-09-06 20:57:50 +00002247 context.AddTypeDecl(copied_clang_type);
Sean Callanan5666b672010-08-04 01:02:13 +00002248}