blob: 54d852b2d8393827a17431fbf6bfa2628d58bd69 [file] [log] [blame]
Sean Callanan79763a42011-05-23 21:40:23 +00001//===-- ClangUserExpression.cpp ---------------------------------*- C++ -*-===//
Sean Callanan1a8d4092010-08-27 01:01:44 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Sean Callanan1a8d4092010-08-27 01:01:44 +00006//
7//===----------------------------------------------------------------------===//
8
Haibo Huangc6551bf2019-08-07 06:15:01 +00009#include "lldb/Host/Config.h"
10
Sean Callanan1a8d4092010-08-27 01:01:44 +000011#include <stdio.h>
12#if HAVE_SYS_TYPES_H
Kate Stoneb9c1b512016-09-06 20:57:50 +000013#include <sys/types.h>
Sean Callanan1a8d4092010-08-27 01:01:44 +000014#endif
15
Sean Callanan1a8d4092010-08-27 01:01:44 +000016#include <cstdlib>
Sean Callanan1a8d4092010-08-27 01:01:44 +000017#include <map>
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include <string>
Sean Callanan1a8d4092010-08-27 01:01:44 +000019
Sean Callanan4dbb2712015-09-25 20:35:58 +000020#include "ClangUserExpression.h"
21
22#include "ASTResultSynthesizer.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000023#include "ClangDiagnostic.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000024#include "ClangExpressionDeclMap.h"
25#include "ClangExpressionParser.h"
Jim Inghamea401ec2019-03-06 22:43:25 +000026#include "ClangExpressionSourceCode.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000027#include "ClangModulesDeclVendor.h"
28#include "ClangPersistentVariables.h"
29
Sean Callananbda7ef82016-06-22 17:32:17 +000030#include "lldb/Core/Debugger.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000031#include "lldb/Core/Module.h"
Greg Claytonc4e411f2011-01-18 19:36:39 +000032#include "lldb/Core/StreamFile.h"
Greg Claytonb71f3842010-10-05 03:13:51 +000033#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan9bc83842011-09-26 18:45:31 +000034#include "lldb/Expression/ExpressionSourceCode.h"
Sean Callanan14b1bae2013-04-16 23:25:35 +000035#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanan1582ee62013-04-18 22:06:33 +000036#include "lldb/Expression/IRInterpreter.h"
Sean Callanan96d27302013-04-11 00:09:05 +000037#include "lldb/Expression/Materializer.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000038#include "lldb/Host/HostInfo.h"
Greg Clayton1f746072012-08-29 21:13:06 +000039#include "lldb/Symbol/Block.h"
Jim Ingham5fdeed42012-10-30 23:35:54 +000040#include "lldb/Symbol/ClangASTContext.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000041#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +000042#include "lldb/Symbol/CompileUnit.h"
Jim Ingham5fdeed42012-10-30 23:35:54 +000043#include "lldb/Symbol/Function.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000044#include "lldb/Symbol/ObjectFile.h"
45#include "lldb/Symbol/SymbolVendor.h"
Jim Ingham5fdeed42012-10-30 23:35:54 +000046#include "lldb/Symbol/Type.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000047#include "lldb/Symbol/VariableList.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000048#include "lldb/Target/ExecutionContext.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000049#include "lldb/Target/Process.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000050#include "lldb/Target/StackFrame.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000051#include "lldb/Target/Target.h"
Jim Inghamf48169b2010-11-30 02:22:11 +000052#include "lldb/Target/ThreadPlan.h"
53#include "lldb/Target/ThreadPlanCallUserExpression.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000054#include "lldb/Utility/ConstString.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000055#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000056#include "lldb/Utility/StreamString.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000057
Sean Callanan72e49402011-08-05 23:43:37 +000058#include "clang/AST/DeclCXX.h"
59#include "clang/AST/DeclObjC.h"
60
Tatyana Krasnukhaedebf122019-04-26 16:41:04 +000061#include "llvm/ADT/ScopeExit.h"
62
Sean Callanan1a8d4092010-08-27 01:01:44 +000063using namespace lldb_private;
64
Kate Stoneb9c1b512016-09-06 20:57:50 +000065ClangUserExpression::ClangUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000066 ExecutionContextScope &exe_scope, llvm::StringRef expr,
67 llvm::StringRef prefix, lldb::LanguageType language,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000068 ResultType desired_type, const EvaluateExpressionOptions &options,
69 ValueObject *ctx_obj)
Zachary Turnerc5d7df92016-11-08 04:52:16 +000070 : LLVMUserExpression(exe_scope, expr, prefix, language, desired_type,
Adrian Prantl7e34d782019-03-13 19:46:30 +000071 options, eKindClangUserExpression),
Jonas Devlieghere70355ac2019-02-12 03:47:39 +000072 m_type_system_helper(*m_target_wp.lock(), options.GetExecutionPolicy() ==
73 eExecutionPolicyTopLevel),
74 m_result_delegate(exe_scope.CalculateTarget()), m_ctx_obj(ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 switch (m_language) {
76 case lldb::eLanguageTypeC_plus_plus:
77 m_allow_cxx = true;
78 break;
79 case lldb::eLanguageTypeObjC:
80 m_allow_objc = true;
81 break;
82 case lldb::eLanguageTypeObjC_plus_plus:
83 default:
84 m_allow_cxx = true;
85 m_allow_objc = true;
86 break;
87 }
Sean Callanan1a8d4092010-08-27 01:01:44 +000088}
89
Kate Stoneb9c1b512016-09-06 20:57:50 +000090ClangUserExpression::~ClangUserExpression() {}
Sean Callanan1a8d4092010-08-27 01:01:44 +000091
Zachary Turner97206d52017-05-12 04:51:55 +000092void ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Status &err) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan70385082012-12-01 00:08:33 +000094
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +000095 LLDB_LOGF(log, "ClangUserExpression::ScanContext()");
Kate Stoneb9c1b512016-09-06 20:57:50 +000096
97 m_target = exe_ctx.GetTargetPtr();
98
99 if (!(m_allow_cxx || m_allow_objc)) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000100 LLDB_LOGF(log, " [CUE::SC] Settings inhibit C++ and Objective-C");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 return;
102 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 StackFrame *frame = exe_ctx.GetFramePtr();
Konrad Kleine248a1302019-05-23 11:14:47 +0000105 if (frame == nullptr) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000106 LLDB_LOGF(log, " [CUE::SC] Null stack frame");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 return;
108 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
111 lldb::eSymbolContextBlock);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 if (!sym_ctx.function) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000114 LLDB_LOGF(log, " [CUE::SC] Null function");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 return;
116 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 // Find the block that defines the function represented by "sym_ctx"
119 Block *function_block = sym_ctx.GetFunctionBlock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 if (!function_block) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000122 LLDB_LOGF(log, " [CUE::SC] Null function block");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 return;
124 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 CompilerDeclContext decl_context = function_block->GetDeclContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 if (!decl_context) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000129 LLDB_LOGF(log, " [CUE::SC] Null decl context");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 return;
131 }
Greg Clayton685c88c2012-07-14 00:53:55 +0000132
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000133 if (m_ctx_obj) {
134 switch (m_ctx_obj->GetObjectRuntimeLanguage()) {
135 case lldb::eLanguageTypeC:
136 case lldb::eLanguageTypeC89:
137 case lldb::eLanguageTypeC99:
138 case lldb::eLanguageTypeC11:
139 case lldb::eLanguageTypeC_plus_plus:
140 case lldb::eLanguageTypeC_plus_plus_03:
141 case lldb::eLanguageTypeC_plus_plus_11:
142 case lldb::eLanguageTypeC_plus_plus_14:
143 m_in_cplusplus_method = true;
144 break;
145 case lldb::eLanguageTypeObjC:
146 case lldb::eLanguageTypeObjC_plus_plus:
147 m_in_objectivec_method = true;
148 break;
149 default:
150 break;
151 }
152 m_needs_object_ptr = true;
153 } else if (clang::CXXMethodDecl *method_decl =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 ClangASTContext::DeclContextGetAsCXXMethodDecl(decl_context)) {
155 if (m_allow_cxx && method_decl->isInstance()) {
156 if (m_enforce_valid_object) {
157 lldb::VariableListSP variable_list_sp(
158 function_block->GetBlockVariableList(true));
Greg Clayton685c88c2012-07-14 00:53:55 +0000159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 const char *thisErrorString = "Stopped in a C++ method, but 'this' "
161 "isn't available; pretending we are in a "
162 "generic context";
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 if (!variable_list_sp) {
165 err.SetErrorString(thisErrorString);
166 return;
Sean Callanan3670ba52010-12-01 21:35:54 +0000167 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168
169 lldb::VariableSP this_var_sp(
170 variable_list_sp->FindVariable(ConstString("this")));
171
172 if (!this_var_sp || !this_var_sp->IsInScope(frame) ||
173 !this_var_sp->LocationIsValidForFrame(frame)) {
174 err.SetErrorString(thisErrorString);
175 return;
176 }
177 }
178
179 m_in_cplusplus_method = true;
180 m_needs_object_ptr = true;
Sean Callanan3670ba52010-12-01 21:35:54 +0000181 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 } else if (clang::ObjCMethodDecl *method_decl =
183 ClangASTContext::DeclContextGetAsObjCMethodDecl(
184 decl_context)) {
185 if (m_allow_objc) {
186 if (m_enforce_valid_object) {
187 lldb::VariableListSP variable_list_sp(
188 function_block->GetBlockVariableList(true));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000189
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 const char *selfErrorString = "Stopped in an Objective-C method, but "
191 "'self' isn't available; pretending we "
192 "are in a generic context";
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000193
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 if (!variable_list_sp) {
195 err.SetErrorString(selfErrorString);
196 return;
197 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 lldb::VariableSP self_variable_sp =
200 variable_list_sp->FindVariable(ConstString("self"));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 if (!self_variable_sp || !self_variable_sp->IsInScope(frame) ||
203 !self_variable_sp->LocationIsValidForFrame(frame)) {
204 err.SetErrorString(selfErrorString);
205 return;
206 }
207 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 m_in_objectivec_method = true;
210 m_needs_object_ptr = true;
211
212 if (!method_decl->isInstanceMethod())
213 m_in_static_method = true;
214 }
215 } else if (clang::FunctionDecl *function_decl =
216 ClangASTContext::DeclContextGetAsFunctionDecl(decl_context)) {
217 // We might also have a function that said in the debug information that it
Adrian Prantl05097242018-04-30 16:49:04 +0000218 // captured an object pointer. The best way to deal with getting to the
219 // ivars at present is by pretending that this is a method of a class in
220 // whatever runtime the debug info says the object pointer belongs to. Do
221 // that here.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222
223 ClangASTMetadata *metadata =
224 ClangASTContext::DeclContextGetMetaData(decl_context, function_decl);
225 if (metadata && metadata->HasObjectPtr()) {
226 lldb::LanguageType language = metadata->GetObjectPtrLanguage();
227 if (language == lldb::eLanguageTypeC_plus_plus) {
228 if (m_enforce_valid_object) {
229 lldb::VariableListSP variable_list_sp(
230 function_block->GetBlockVariableList(true));
231
232 const char *thisErrorString = "Stopped in a context claiming to "
233 "capture a C++ object pointer, but "
234 "'this' isn't available; pretending we "
235 "are in a generic context";
236
237 if (!variable_list_sp) {
238 err.SetErrorString(thisErrorString);
239 return;
240 }
241
242 lldb::VariableSP this_var_sp(
243 variable_list_sp->FindVariable(ConstString("this")));
244
245 if (!this_var_sp || !this_var_sp->IsInScope(frame) ||
246 !this_var_sp->LocationIsValidForFrame(frame)) {
247 err.SetErrorString(thisErrorString);
248 return;
249 }
250 }
251
252 m_in_cplusplus_method = true;
253 m_needs_object_ptr = true;
254 } else if (language == lldb::eLanguageTypeObjC) {
255 if (m_enforce_valid_object) {
256 lldb::VariableListSP variable_list_sp(
257 function_block->GetBlockVariableList(true));
258
259 const char *selfErrorString =
260 "Stopped in a context claiming to capture an Objective-C object "
261 "pointer, but 'self' isn't available; pretending we are in a "
262 "generic context";
263
264 if (!variable_list_sp) {
265 err.SetErrorString(selfErrorString);
266 return;
267 }
268
269 lldb::VariableSP self_variable_sp =
270 variable_list_sp->FindVariable(ConstString("self"));
271
272 if (!self_variable_sp || !self_variable_sp->IsInScope(frame) ||
273 !self_variable_sp->LocationIsValidForFrame(frame)) {
274 err.SetErrorString(selfErrorString);
275 return;
276 }
277
278 Type *self_type = self_variable_sp->GetType();
279
280 if (!self_type) {
281 err.SetErrorString(selfErrorString);
282 return;
283 }
284
285 CompilerType self_clang_type = self_type->GetForwardCompilerType();
286
287 if (!self_clang_type) {
288 err.SetErrorString(selfErrorString);
289 return;
290 }
291
292 if (ClangASTContext::IsObjCClassType(self_clang_type)) {
293 return;
294 } else if (ClangASTContext::IsObjCObjectPointerType(
295 self_clang_type)) {
Dawn Perchik53f34c82015-07-01 00:54:02 +0000296 m_in_objectivec_method = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000297 m_needs_object_ptr = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 } else {
299 err.SetErrorString(selfErrorString);
300 return;
301 }
302 } else {
303 m_in_objectivec_method = true;
304 m_needs_object_ptr = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000305 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306 }
Sean Callanan3670ba52010-12-01 21:35:54 +0000307 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000309}
310
Adrian Prantl05097242018-04-30 16:49:04 +0000311// This is a really nasty hack, meant to fix Objective-C expressions of the
312// form (int)[myArray count]. Right now, because the type information for
313// count is not available, [myArray count] returns id, which can't be directly
314// cast to int without causing a clang error.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315static void ApplyObjcCastHack(std::string &expr) {
Sean Callanancf5498f2010-10-22 23:25:16 +0000316#define OBJC_CAST_HACK_FROM "(int)["
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317#define OBJC_CAST_HACK_TO "(int)(long long)["
Sean Callanancf5498f2010-10-22 23:25:16 +0000318
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319 size_t from_offset;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000320
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
322 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1,
323 OBJC_CAST_HACK_TO);
Sean Callanancf5498f2010-10-22 23:25:16 +0000324
325#undef OBJC_CAST_HACK_TO
326#undef OBJC_CAST_HACK_FROM
327}
328
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000329bool ClangUserExpression::SetupPersistentState(DiagnosticManager &diagnostic_manager,
330 ExecutionContext &exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331 if (Target *target = exe_ctx.GetTargetPtr()) {
332 if (PersistentExpressionState *persistent_state =
333 target->GetPersistentExpressionStateForLanguage(
334 lldb::eLanguageTypeC)) {
335 m_result_delegate.RegisterPersistentState(persistent_state);
336 } else {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000337 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338 eDiagnosticSeverityError,
339 "couldn't start parsing (no persistent data)");
340 return false;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000341 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 } else {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000343 diagnostic_manager.PutString(eDiagnosticSeverityError,
344 "error: couldn't start parsing (no target)");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345 return false;
346 }
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000347 return true;
348}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000350static void SetupDeclVendor(ExecutionContext &exe_ctx, Target *target) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 if (ClangModulesDeclVendor *decl_vendor =
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000352 target->GetClangModulesDeclVendor()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
354 llvm::cast<ClangPersistentVariables>(
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000355 target->GetPersistentExpressionStateForLanguage(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 lldb::eLanguageTypeC))
357 ->GetHandLoadedClangModules();
358 ClangModulesDeclVendor::ModuleVector modules_for_macros;
359
360 for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
361 modules_for_macros.push_back(module);
362 }
363
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000364 if (target->GetEnableAutoImportClangModules()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
366 if (Block *block = frame->GetFrameBlock()) {
367 SymbolContext sc;
368
369 block->CalculateSymbolContext(&sc);
370
371 if (sc.comp_unit) {
372 StreamString error_stream;
373
374 decl_vendor->AddModulesForCompileUnit(
375 *sc.comp_unit, modules_for_macros, error_stream);
376 }
377 }
378 }
379 }
380 }
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000381}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382
Raphael Isemann5eb87cf2019-08-05 20:31:47 +0000383void ClangUserExpression::UpdateLanguageForExpr() {
384 m_expr_lang = lldb::LanguageType::eLanguageTypeUnknown;
385 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel)
386 return;
387 if (m_in_cplusplus_method)
388 m_expr_lang = lldb::eLanguageTypeC_plus_plus;
389 else if (m_in_objectivec_method)
390 m_expr_lang = lldb::eLanguageTypeObjC;
391 else
392 m_expr_lang = lldb::eLanguageTypeC;
393}
394
395void ClangUserExpression::CreateSourceCode(
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000396 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
Raphael Isemann71569d02019-05-02 10:12:56 +0000397 std::vector<std::string> modules_to_import, bool for_completion) {
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000398
399 std::string prefix = m_expr_prefix;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400
401 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
402 m_transformed_text = m_expr_text;
403 } else {
Jim Inghamea401ec2019-03-06 22:43:25 +0000404 std::unique_ptr<ClangExpressionSourceCode> source_code(
405 ClangExpressionSourceCode::CreateWrapped(prefix.c_str(),
Raphael Isemann5eb87cf2019-08-05 20:31:47 +0000406 m_expr_text.c_str()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407
Raphael Isemanndababf72018-09-27 10:12:54 +0000408 if (!source_code->GetText(m_transformed_text, m_expr_lang,
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000409 m_in_static_method, exe_ctx, !m_ctx_obj,
Raphael Isemann71569d02019-05-02 10:12:56 +0000410 for_completion, modules_to_import)) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000411 diagnostic_manager.PutString(eDiagnosticSeverityError,
412 "couldn't construct expression body");
Raphael Isemanndababf72018-09-27 10:12:54 +0000413 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414 }
Raphael Isemann74829732018-08-30 17:29:37 +0000415
416 // Find and store the start position of the original code inside the
417 // transformed code. We need this later for the code completion.
418 std::size_t original_start;
419 std::size_t original_end;
420 bool found_bounds = source_code->GetOriginalBodyBounds(
Raphael Isemanndababf72018-09-27 10:12:54 +0000421 m_transformed_text, m_expr_lang, original_start, original_end);
Raphael Isemannd59c8d32019-04-24 12:55:00 +0000422 if (found_bounds)
Raphael Isemann74829732018-08-30 17:29:37 +0000423 m_user_expression_start_pos = original_start;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424 }
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000425}
426
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000427static bool SupportsCxxModuleImport(lldb::LanguageType language) {
428 switch (language) {
429 case lldb::eLanguageTypeC_plus_plus:
430 case lldb::eLanguageTypeC_plus_plus_03:
431 case lldb::eLanguageTypeC_plus_plus_11:
432 case lldb::eLanguageTypeC_plus_plus_14:
433 case lldb::eLanguageTypeObjC_plus_plus:
434 return true;
435 default:
436 return false;
437 }
438}
439
440std::vector<std::string>
441ClangUserExpression::GetModulesToImport(ExecutionContext &exe_ctx) {
442 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
443
444 if (!SupportsCxxModuleImport(Language()))
445 return {};
446
447 Target *target = exe_ctx.GetTargetPtr();
448 if (!target || !target->GetEnableImportStdModule())
449 return {};
450
451 StackFrame *frame = exe_ctx.GetFramePtr();
452 if (!frame)
453 return {};
454
455 Block *block = frame->GetFrameBlock();
456 if (!block)
457 return {};
458
459 SymbolContext sc;
460 block->CalculateSymbolContext(&sc);
461 if (!sc.comp_unit)
462 return {};
463
464 if (log) {
465 for (const SourceModule &m : sc.comp_unit->GetImportedModules()) {
466 LLDB_LOG(log, "Found module in compile unit: {0:$[.]} - include dir: {1}",
467 llvm::make_range(m.path.begin(), m.path.end()), m.search_path);
468 }
469 }
470
471 for (const SourceModule &m : sc.comp_unit->GetImportedModules())
472 m_include_directories.push_back(m.search_path);
473
474 // Check if we imported 'std' or any of its submodules.
475 // We currently don't support importing any other modules in the expression
476 // parser.
477 for (const SourceModule &m : sc.comp_unit->GetImportedModules())
Raphael Isemann05cfdb02019-04-26 07:21:36 +0000478 if (!m.path.empty() && m.path.front() == "std")
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000479 return {"std"};
480
481 return {};
482}
483
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000484bool ClangUserExpression::PrepareForParsing(
Raphael Isemann71569d02019-05-02 10:12:56 +0000485 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
486 bool for_completion) {
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000487 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
488
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000489 InstallContext(exe_ctx);
490
491 if (!SetupPersistentState(diagnostic_manager, exe_ctx))
492 return false;
493
494 Status err;
495 ScanContext(exe_ctx, err);
496
497 if (!err.Success()) {
498 diagnostic_manager.PutString(eDiagnosticSeverityWarning, err.AsCString());
499 }
500
501 ////////////////////////////////////
502 // Generate the expression
503 //
504
505 ApplyObjcCastHack(m_expr_text);
506
507 SetupDeclVendor(exe_ctx, m_target);
Raphael Isemanndababf72018-09-27 10:12:54 +0000508
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000509 std::vector<std::string> used_modules = GetModulesToImport(exe_ctx);
510 m_imported_cpp_modules = !used_modules.empty();
511
512 LLDB_LOG(log, "List of imported modules in expression: {0}",
513 llvm::make_range(used_modules.begin(), used_modules.end()));
514
Raphael Isemann5eb87cf2019-08-05 20:31:47 +0000515 UpdateLanguageForExpr();
516 CreateSourceCode(diagnostic_manager, exe_ctx, used_modules, for_completion);
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000517 return true;
518}
519
520bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
521 ExecutionContext &exe_ctx,
522 lldb_private::ExecutionPolicy execution_policy,
523 bool keep_result_in_memory,
524 bool generate_debug_info) {
525 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
526
Raphael Isemann71569d02019-05-02 10:12:56 +0000527 if (!PrepareForParsing(diagnostic_manager, exe_ctx, /*for_completion*/ false))
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000528 return false;
529
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000530 LLDB_LOGF(log, "Parsing the following code:\n%s", m_transformed_text.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000531
532 ////////////////////////////////////
533 // Set up the target and compiler
534 //
535
536 Target *target = exe_ctx.GetTargetPtr();
537
538 if (!target) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000539 diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540 return false;
541 }
542
543 //////////////////////////
544 // Parse the expression
545 //
546
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000547 m_materializer_up.reset(new Materializer());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000548
549 ResetDeclMap(exe_ctx, m_result_delegate, keep_result_in_memory);
550
Tatyana Krasnukhaedebf122019-04-26 16:41:04 +0000551 auto on_exit = llvm::make_scope_exit([this]() { ResetDeclMap(); });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000553 if (!DeclMap()->WillParse(exe_ctx, m_materializer_up.get())) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000554 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555 eDiagnosticSeverityError,
556 "current process state is unsuitable for expression parsing");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557 return false;
558 }
559
560 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
561 DeclMap()->SetLookupsEnabled(true);
562 }
563
564 Process *process = exe_ctx.GetProcessPtr();
565 ExecutionContextScope *exe_scope = process;
566
567 if (!exe_scope)
568 exe_scope = exe_ctx.GetTargetPtr();
569
570 // We use a shared pointer here so we can use the original parser - if it
Adrian Prantl05097242018-04-30 16:49:04 +0000571 // succeeds or the rewrite parser we might make if it fails. But the
572 // parser_sp will never be empty.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000574 ClangExpressionParser parser(exe_scope, *this, generate_debug_info,
575 m_include_directories);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576
577 unsigned num_errors = parser.Parse(diagnostic_manager);
578
579 // Check here for FixItHints. If there are any try to apply the fixits and
Adrian Prantl05097242018-04-30 16:49:04 +0000580 // set the fixed text in m_fixed_text before returning an error.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581 if (num_errors) {
582 if (diagnostic_manager.HasFixIts()) {
583 if (parser.RewriteExpression(diagnostic_manager)) {
584 size_t fixed_start;
585 size_t fixed_end;
586 const std::string &fixed_expression =
587 diagnostic_manager.GetFixedExpression();
Jim Inghamea401ec2019-03-06 22:43:25 +0000588 if (ClangExpressionSourceCode::GetOriginalBodyBounds(
Raphael Isemanndababf72018-09-27 10:12:54 +0000589 fixed_expression, m_expr_lang, fixed_start, fixed_end))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590 m_fixed_text =
591 fixed_expression.substr(fixed_start, fixed_end - fixed_start);
592 }
Sean Callanan9fda9d22015-10-03 09:09:01 +0000593 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 return false;
595 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000596
Raphael Isemann54d99bf2019-04-24 12:21:03 +0000597 //////////////////////////////////////////////////////////////////////////////
Adrian Prantl05097242018-04-30 16:49:04 +0000598 // Prepare the output of the parser for execution, evaluating it statically
599 // if possible
Kate Stoneb9c1b512016-09-06 20:57:50 +0000600 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000601
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602 {
Zachary Turner97206d52017-05-12 04:51:55 +0000603 Status jit_error = parser.PrepareForExecution(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000604 m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,
605 m_can_interpret, execution_policy);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000606
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607 if (!jit_error.Success()) {
608 const char *error_cstr = jit_error.AsCString();
609 if (error_cstr && error_cstr[0])
Zachary Turnere2411fa2016-11-12 19:12:56 +0000610 diagnostic_manager.PutString(eDiagnosticSeverityError, error_cstr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000611 else
Zachary Turnere2411fa2016-11-12 19:12:56 +0000612 diagnostic_manager.PutString(eDiagnosticSeverityError,
613 "expression can't be interpreted or run");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000614 return false;
615 }
616 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000617
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618 if (exe_ctx.GetProcessPtr() && execution_policy == eExecutionPolicyTopLevel) {
Zachary Turner97206d52017-05-12 04:51:55 +0000619 Status static_init_error =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620 parser.RunStaticInitializers(m_execution_unit_sp, exe_ctx);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000621
Kate Stoneb9c1b512016-09-06 20:57:50 +0000622 if (!static_init_error.Success()) {
623 const char *error_cstr = static_init_error.AsCString();
624 if (error_cstr && error_cstr[0])
625 diagnostic_manager.Printf(eDiagnosticSeverityError,
626 "couldn't run static initializers: %s\n",
627 error_cstr);
628 else
Zachary Turnere2411fa2016-11-12 19:12:56 +0000629 diagnostic_manager.PutString(eDiagnosticSeverityError,
630 "couldn't run static initializers\n");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 return false;
632 }
633 }
634
635 if (m_execution_unit_sp) {
636 bool register_execution_unit = false;
637
638 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
639 register_execution_unit = true;
Sean Callananb9951192011-08-01 18:18:33 +0000640 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000641
Adrian Prantl05097242018-04-30 16:49:04 +0000642 // If there is more than one external function in the execution unit, it
643 // needs to keep living even if it's not top level, because the result
644 // could refer to that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000645
646 if (m_execution_unit_sp->GetJittedFunctions().size() > 1) {
647 register_execution_unit = true;
Sean Callanan00294b32016-03-22 21:05:51 +0000648 }
649
Raphael Isemannefe5fd12019-08-06 08:07:45 +0000650 if (register_execution_unit)
651 exe_ctx.GetTargetPtr()
652 ->GetPersistentExpressionStateForLanguage(m_language)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000653 ->RegisterExecutionUnit(m_execution_unit_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000654 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000655
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656 if (generate_debug_info) {
657 lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000658
Kate Stoneb9c1b512016-09-06 20:57:50 +0000659 if (jit_module_sp) {
660 ConstString const_func_name(FunctionName());
661 FileSpec jit_file;
662 jit_file.GetFilename() = const_func_name;
663 jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());
664 m_jit_module_wp = jit_module_sp;
665 target->GetImages().Append(jit_module_sp);
Sean Callanan00294b32016-03-22 21:05:51 +0000666 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000667 }
Sean Callanan00294b32016-03-22 21:05:51 +0000668
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669 if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
670 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
671 return true;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000672}
673
Raphael Isemann74829732018-08-30 17:29:37 +0000674/// Converts an absolute position inside a given code string into
675/// a column/line pair.
676///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000677/// \param[in] abs_pos
Raphael Isemann74829732018-08-30 17:29:37 +0000678/// A absolute position in the code string that we want to convert
679/// to a column/line pair.
680///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000681/// \param[in] code
Raphael Isemann74829732018-08-30 17:29:37 +0000682/// A multi-line string usually representing source code.
683///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000684/// \param[out] line
Raphael Isemann74829732018-08-30 17:29:37 +0000685/// The line in the code that contains the given absolute position.
686/// The first line in the string is indexed as 1.
687///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000688/// \param[out] column
Raphael Isemann74829732018-08-30 17:29:37 +0000689/// The column in the line that contains the absolute position.
690/// The first character in a line is indexed as 0.
Raphael Isemannbfc5ef62018-09-22 13:33:08 +0000691static void AbsPosToLineColumnPos(size_t abs_pos, llvm::StringRef code,
Raphael Isemann74829732018-08-30 17:29:37 +0000692 unsigned &line, unsigned &column) {
693 // Reset to code position to beginning of the file.
694 line = 0;
695 column = 0;
696
697 assert(abs_pos <= code.size() && "Absolute position outside code string?");
698
699 // We have to walk up to the position and count lines/columns.
700 for (std::size_t i = 0; i < abs_pos; ++i) {
701 // If we hit a line break, we go back to column 0 and enter a new line.
702 // We only handle \n because that's what we internally use to make new
703 // lines for our temporary code strings.
704 if (code[i] == '\n') {
705 ++line;
706 column = 0;
707 continue;
708 }
709 ++column;
710 }
711}
712
713bool ClangUserExpression::Complete(ExecutionContext &exe_ctx,
Raphael Isemannc11a7802018-08-30 21:26:32 +0000714 CompletionRequest &request,
715 unsigned complete_pos) {
Raphael Isemann74829732018-08-30 17:29:37 +0000716 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
717
718 // We don't want any visible feedback when completing an expression. Mostly
719 // because the results we get from an incomplete invocation are probably not
720 // correct.
721 DiagnosticManager diagnostic_manager;
722
Raphael Isemann71569d02019-05-02 10:12:56 +0000723 if (!PrepareForParsing(diagnostic_manager, exe_ctx, /*for_completion*/ true))
Raphael Isemann74829732018-08-30 17:29:37 +0000724 return false;
725
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000726 LLDB_LOGF(log, "Parsing the following code:\n%s", m_transformed_text.c_str());
Raphael Isemann74829732018-08-30 17:29:37 +0000727
728 //////////////////////////
729 // Parse the expression
730 //
731
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000732 m_materializer_up.reset(new Materializer());
Raphael Isemann74829732018-08-30 17:29:37 +0000733
734 ResetDeclMap(exe_ctx, m_result_delegate, /*keep result in memory*/ true);
735
Tatyana Krasnukhaedebf122019-04-26 16:41:04 +0000736 auto on_exit = llvm::make_scope_exit([this]() { ResetDeclMap(); });
Raphael Isemann74829732018-08-30 17:29:37 +0000737
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000738 if (!DeclMap()->WillParse(exe_ctx, m_materializer_up.get())) {
Raphael Isemann74829732018-08-30 17:29:37 +0000739 diagnostic_manager.PutString(
740 eDiagnosticSeverityError,
741 "current process state is unsuitable for expression parsing");
742
743 return false;
744 }
745
746 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
747 DeclMap()->SetLookupsEnabled(true);
748 }
749
750 Process *process = exe_ctx.GetProcessPtr();
751 ExecutionContextScope *exe_scope = process;
752
753 if (!exe_scope)
754 exe_scope = exe_ctx.GetTargetPtr();
755
756 ClangExpressionParser parser(exe_scope, *this, false);
757
758 // We have to find the source code location where the user text is inside
759 // the transformed expression code. When creating the transformed text, we
760 // already stored the absolute position in the m_transformed_text string. The
761 // only thing left to do is to transform it into the line:column format that
762 // Clang expects.
763
764 // The line and column of the user expression inside the transformed source
765 // code.
766 unsigned user_expr_line, user_expr_column;
767 if (m_user_expression_start_pos.hasValue())
768 AbsPosToLineColumnPos(*m_user_expression_start_pos, m_transformed_text,
769 user_expr_line, user_expr_column);
770 else
771 return false;
772
773 // The actual column where we have to complete is the start column of the
774 // user expression + the offset inside the user code that we were given.
775 const unsigned completion_column = user_expr_column + complete_pos;
Raphael Isemannc11a7802018-08-30 21:26:32 +0000776 parser.Complete(request, user_expr_line, completion_column, complete_pos);
Raphael Isemann74829732018-08-30 17:29:37 +0000777
778 return true;
779}
780
Kate Stoneb9c1b512016-09-06 20:57:50 +0000781bool ClangUserExpression::AddArguments(ExecutionContext &exe_ctx,
782 std::vector<lldb::addr_t> &args,
783 lldb::addr_t struct_address,
784 DiagnosticManager &diagnostic_manager) {
785 lldb::addr_t object_ptr = LLDB_INVALID_ADDRESS;
786 lldb::addr_t cmd_ptr = LLDB_INVALID_ADDRESS;
Sean Callanan579e70c2016-03-19 00:03:59 +0000787
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788 if (m_needs_object_ptr) {
789 lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
790 if (!frame_sp)
791 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000792
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793 ConstString object_name;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000794
Kate Stoneb9c1b512016-09-06 20:57:50 +0000795 if (m_in_cplusplus_method) {
796 object_name.SetCString("this");
797 } else if (m_in_objectivec_method) {
798 object_name.SetCString("self");
799 } else {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000800 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000801 eDiagnosticSeverityError,
802 "need object pointer but don't know the language");
803 return false;
Jim Ingham51148e92015-11-05 00:24:18 +0000804 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000805
Zachary Turner97206d52017-05-12 04:51:55 +0000806 Status object_ptr_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000807
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000808 if (m_ctx_obj) {
809 AddressType address_type;
810 object_ptr = m_ctx_obj->GetAddressOf(false, &address_type);
811 if (object_ptr == LLDB_INVALID_ADDRESS ||
812 address_type != eAddressTypeLoad)
813 object_ptr_error.SetErrorString("Can't get context object's "
814 "debuggee address");
815 } else
816 object_ptr = GetObjectPointer(frame_sp, object_name, object_ptr_error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817
818 if (!object_ptr_error.Success()) {
819 exe_ctx.GetTargetRef().GetDebugger().GetAsyncOutputStream()->Printf(
Jason Molenda599558e2017-02-21 05:09:26 +0000820 "warning: `%s' is not accessible (substituting 0)\n",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000821 object_name.AsCString());
822 object_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000823 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824
825 if (m_in_objectivec_method) {
826 ConstString cmd_name("_cmd");
827
828 cmd_ptr = GetObjectPointer(frame_sp, cmd_name, object_ptr_error);
829
830 if (!object_ptr_error.Success()) {
831 diagnostic_manager.Printf(
832 eDiagnosticSeverityWarning,
833 "couldn't get cmd pointer (substituting NULL): %s",
834 object_ptr_error.AsCString());
835 cmd_ptr = 0;
836 }
837 }
838
839 args.push_back(object_ptr);
840
841 if (m_in_objectivec_method)
842 args.push_back(cmd_ptr);
843
844 args.push_back(struct_address);
845 } else {
846 args.push_back(struct_address);
847 }
848 return true;
Jim Ingham36f3b362010-10-14 23:45:03 +0000849}
850
Kate Stoneb9c1b512016-09-06 20:57:50 +0000851lldb::ExpressionVariableSP ClangUserExpression::GetResultAfterDematerialization(
852 ExecutionContextScope *exe_scope) {
853 return m_result_delegate.GetVariable();
Sean Callanan9fda9d22015-10-03 09:09:01 +0000854}
855
Kate Stoneb9c1b512016-09-06 20:57:50 +0000856void ClangUserExpression::ClangUserExpressionHelper::ResetDeclMap(
857 ExecutionContext &exe_ctx,
858 Materializer::PersistentVariableDelegate &delegate,
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000859 bool keep_result_in_memory,
860 ValueObject *ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000861 m_expr_decl_map_up.reset(
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000862 new ClangExpressionDeclMap(keep_result_in_memory, &delegate, exe_ctx,
863 ctx_obj));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000864}
Jim Ingham36f3b362010-10-14 23:45:03 +0000865
Jim Ingham151c0322015-09-15 21:13:50 +0000866clang::ASTConsumer *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867ClangUserExpression::ClangUserExpressionHelper::ASTTransformer(
868 clang::ASTConsumer *passthrough) {
869 m_result_synthesizer_up.reset(
870 new ASTResultSynthesizer(passthrough, m_top_level, m_target));
Sean Callananc673a6e2010-12-07 10:00:20 +0000871
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872 return m_result_synthesizer_up.get();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000873}
874
Kate Stoneb9c1b512016-09-06 20:57:50 +0000875void ClangUserExpression::ClangUserExpressionHelper::CommitPersistentDecls() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000876 if (m_result_synthesizer_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000877 m_result_synthesizer_up->CommitPersistentDecls();
878 }
Sean Callanan00294b32016-03-22 21:05:51 +0000879}
880
Kate Stoneb9c1b512016-09-06 20:57:50 +0000881ConstString ClangUserExpression::ResultDelegate::GetName() {
Adrian Prantl03219f72018-04-30 23:59:17 +0000882 auto prefix = m_persistent_state->GetPersistentVariablePrefix();
883 return m_persistent_state->GetNextPersistentVariableName(*m_target_sp,
884 prefix);
Sean Callanan9fda9d22015-10-03 09:09:01 +0000885}
886
Kate Stoneb9c1b512016-09-06 20:57:50 +0000887void ClangUserExpression::ResultDelegate::DidDematerialize(
888 lldb::ExpressionVariableSP &variable) {
889 m_variable = variable;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000890}
891
Kate Stoneb9c1b512016-09-06 20:57:50 +0000892void ClangUserExpression::ResultDelegate::RegisterPersistentState(
893 PersistentExpressionState *persistent_state) {
894 m_persistent_state = persistent_state;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000895}
896
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897lldb::ExpressionVariableSP &ClangUserExpression::ResultDelegate::GetVariable() {
898 return m_variable;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000899}