blob: 2dae5b7022f3077efd8a01a6734500d25ee36851 [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
Sean Callanan1a8d4092010-08-27 01:01:44 +00009#include <stdio.h>
10#if HAVE_SYS_TYPES_H
Kate Stoneb9c1b512016-09-06 20:57:50 +000011#include <sys/types.h>
Sean Callanan1a8d4092010-08-27 01:01:44 +000012#endif
13
Sean Callanan1a8d4092010-08-27 01:01:44 +000014#include <cstdlib>
Sean Callanan1a8d4092010-08-27 01:01:44 +000015#include <map>
Kate Stoneb9c1b512016-09-06 20:57:50 +000016#include <string>
Sean Callanan1a8d4092010-08-27 01:01:44 +000017
Sean Callanan4dbb2712015-09-25 20:35:58 +000018#include "ClangUserExpression.h"
19
20#include "ASTResultSynthesizer.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000021#include "ClangDiagnostic.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000022#include "ClangExpressionDeclMap.h"
23#include "ClangExpressionParser.h"
Jim Inghamea401ec2019-03-06 22:43:25 +000024#include "ClangExpressionSourceCode.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000025#include "ClangModulesDeclVendor.h"
26#include "ClangPersistentVariables.h"
27
Sean Callananbda7ef82016-06-22 17:32:17 +000028#include "lldb/Core/Debugger.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000029#include "lldb/Core/Module.h"
Greg Claytonc4e411f2011-01-18 19:36:39 +000030#include "lldb/Core/StreamFile.h"
Greg Claytonb71f3842010-10-05 03:13:51 +000031#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan9bc83842011-09-26 18:45:31 +000032#include "lldb/Expression/ExpressionSourceCode.h"
Sean Callanan14b1bae2013-04-16 23:25:35 +000033#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanan1582ee62013-04-18 22:06:33 +000034#include "lldb/Expression/IRInterpreter.h"
Sean Callanan96d27302013-04-11 00:09:05 +000035#include "lldb/Expression/Materializer.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000036#include "lldb/Host/HostInfo.h"
Greg Clayton1f746072012-08-29 21:13:06 +000037#include "lldb/Symbol/Block.h"
Jim Ingham5fdeed42012-10-30 23:35:54 +000038#include "lldb/Symbol/ClangASTContext.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000039#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +000040#include "lldb/Symbol/CompileUnit.h"
Jim Ingham5fdeed42012-10-30 23:35:54 +000041#include "lldb/Symbol/Function.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000042#include "lldb/Symbol/ObjectFile.h"
43#include "lldb/Symbol/SymbolVendor.h"
Jim Ingham5fdeed42012-10-30 23:35:54 +000044#include "lldb/Symbol/Type.h"
Sean Callananfc55f5d2010-09-21 00:44:12 +000045#include "lldb/Symbol/VariableList.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000046#include "lldb/Target/ExecutionContext.h"
Greg Clayton8f343b02010-11-04 01:54:29 +000047#include "lldb/Target/Process.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000048#include "lldb/Target/StackFrame.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000049#include "lldb/Target/Target.h"
Jim Inghamf48169b2010-11-30 02:22:11 +000050#include "lldb/Target/ThreadPlan.h"
51#include "lldb/Target/ThreadPlanCallUserExpression.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000052#include "lldb/Utility/ConstString.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000053#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000054#include "lldb/Utility/StreamString.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000055
Sean Callanan72e49402011-08-05 23:43:37 +000056#include "clang/AST/DeclCXX.h"
57#include "clang/AST/DeclObjC.h"
58
Tatyana Krasnukhaedebf122019-04-26 16:41:04 +000059#include "llvm/ADT/ScopeExit.h"
60
Sean Callanan1a8d4092010-08-27 01:01:44 +000061using namespace lldb_private;
62
Kate Stoneb9c1b512016-09-06 20:57:50 +000063ClangUserExpression::ClangUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000064 ExecutionContextScope &exe_scope, llvm::StringRef expr,
65 llvm::StringRef prefix, lldb::LanguageType language,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000066 ResultType desired_type, const EvaluateExpressionOptions &options,
67 ValueObject *ctx_obj)
Zachary Turnerc5d7df92016-11-08 04:52:16 +000068 : LLVMUserExpression(exe_scope, expr, prefix, language, desired_type,
Adrian Prantl7e34d782019-03-13 19:46:30 +000069 options, eKindClangUserExpression),
Jonas Devlieghere70355ac2019-02-12 03:47:39 +000070 m_type_system_helper(*m_target_wp.lock(), options.GetExecutionPolicy() ==
71 eExecutionPolicyTopLevel),
72 m_result_delegate(exe_scope.CalculateTarget()), m_ctx_obj(ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 switch (m_language) {
74 case lldb::eLanguageTypeC_plus_plus:
75 m_allow_cxx = true;
76 break;
77 case lldb::eLanguageTypeObjC:
78 m_allow_objc = true;
79 break;
80 case lldb::eLanguageTypeObjC_plus_plus:
81 default:
82 m_allow_cxx = true;
83 m_allow_objc = true;
84 break;
85 }
Sean Callanan1a8d4092010-08-27 01:01:44 +000086}
87
Kate Stoneb9c1b512016-09-06 20:57:50 +000088ClangUserExpression::~ClangUserExpression() {}
Sean Callanan1a8d4092010-08-27 01:01:44 +000089
Zachary Turner97206d52017-05-12 04:51:55 +000090void ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Status &err) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan70385082012-12-01 00:08:33 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 if (log)
94 log->Printf("ClangUserExpression::ScanContext()");
95
96 m_target = exe_ctx.GetTargetPtr();
97
98 if (!(m_allow_cxx || m_allow_objc)) {
Sean Callanan70385082012-12-01 00:08:33 +000099 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 log->Printf(" [CUE::SC] Settings inhibit C++ and Objective-C");
101 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) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 if (log)
107 log->Printf(" [CUE::SC] Null stack frame");
108 return;
109 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
112 lldb::eSymbolContextBlock);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 if (!sym_ctx.function) {
115 if (log)
116 log->Printf(" [CUE::SC] Null function");
117 return;
118 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 // Find the block that defines the function represented by "sym_ctx"
121 Block *function_block = sym_ctx.GetFunctionBlock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 if (!function_block) {
124 if (log)
125 log->Printf(" [CUE::SC] Null function block");
126 return;
127 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 CompilerDeclContext decl_context = function_block->GetDeclContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 if (!decl_context) {
132 if (log)
133 log->Printf(" [CUE::SC] Null decl context");
134 return;
135 }
Greg Clayton685c88c2012-07-14 00:53:55 +0000136
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000137 if (m_ctx_obj) {
138 switch (m_ctx_obj->GetObjectRuntimeLanguage()) {
139 case lldb::eLanguageTypeC:
140 case lldb::eLanguageTypeC89:
141 case lldb::eLanguageTypeC99:
142 case lldb::eLanguageTypeC11:
143 case lldb::eLanguageTypeC_plus_plus:
144 case lldb::eLanguageTypeC_plus_plus_03:
145 case lldb::eLanguageTypeC_plus_plus_11:
146 case lldb::eLanguageTypeC_plus_plus_14:
147 m_in_cplusplus_method = true;
148 break;
149 case lldb::eLanguageTypeObjC:
150 case lldb::eLanguageTypeObjC_plus_plus:
151 m_in_objectivec_method = true;
152 break;
153 default:
154 break;
155 }
156 m_needs_object_ptr = true;
157 } else if (clang::CXXMethodDecl *method_decl =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 ClangASTContext::DeclContextGetAsCXXMethodDecl(decl_context)) {
159 if (m_allow_cxx && method_decl->isInstance()) {
160 if (m_enforce_valid_object) {
161 lldb::VariableListSP variable_list_sp(
162 function_block->GetBlockVariableList(true));
Greg Clayton685c88c2012-07-14 00:53:55 +0000163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 const char *thisErrorString = "Stopped in a C++ method, but 'this' "
165 "isn't available; pretending we are in a "
166 "generic context";
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 if (!variable_list_sp) {
169 err.SetErrorString(thisErrorString);
170 return;
Sean Callanan3670ba52010-12-01 21:35:54 +0000171 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172
173 lldb::VariableSP this_var_sp(
174 variable_list_sp->FindVariable(ConstString("this")));
175
176 if (!this_var_sp || !this_var_sp->IsInScope(frame) ||
177 !this_var_sp->LocationIsValidForFrame(frame)) {
178 err.SetErrorString(thisErrorString);
179 return;
180 }
181 }
182
183 m_in_cplusplus_method = true;
184 m_needs_object_ptr = true;
Sean Callanan3670ba52010-12-01 21:35:54 +0000185 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 } else if (clang::ObjCMethodDecl *method_decl =
187 ClangASTContext::DeclContextGetAsObjCMethodDecl(
188 decl_context)) {
189 if (m_allow_objc) {
190 if (m_enforce_valid_object) {
191 lldb::VariableListSP variable_list_sp(
192 function_block->GetBlockVariableList(true));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000193
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 const char *selfErrorString = "Stopped in an Objective-C method, but "
195 "'self' isn't available; pretending we "
196 "are in a generic context";
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000197
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198 if (!variable_list_sp) {
199 err.SetErrorString(selfErrorString);
200 return;
201 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000202
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 lldb::VariableSP self_variable_sp =
204 variable_list_sp->FindVariable(ConstString("self"));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000205
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 if (!self_variable_sp || !self_variable_sp->IsInScope(frame) ||
207 !self_variable_sp->LocationIsValidForFrame(frame)) {
208 err.SetErrorString(selfErrorString);
209 return;
210 }
211 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000212
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 m_in_objectivec_method = true;
214 m_needs_object_ptr = true;
215
216 if (!method_decl->isInstanceMethod())
217 m_in_static_method = true;
218 }
219 } else if (clang::FunctionDecl *function_decl =
220 ClangASTContext::DeclContextGetAsFunctionDecl(decl_context)) {
221 // We might also have a function that said in the debug information that it
Adrian Prantl05097242018-04-30 16:49:04 +0000222 // captured an object pointer. The best way to deal with getting to the
223 // ivars at present is by pretending that this is a method of a class in
224 // whatever runtime the debug info says the object pointer belongs to. Do
225 // that here.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226
227 ClangASTMetadata *metadata =
228 ClangASTContext::DeclContextGetMetaData(decl_context, function_decl);
229 if (metadata && metadata->HasObjectPtr()) {
230 lldb::LanguageType language = metadata->GetObjectPtrLanguage();
231 if (language == lldb::eLanguageTypeC_plus_plus) {
232 if (m_enforce_valid_object) {
233 lldb::VariableListSP variable_list_sp(
234 function_block->GetBlockVariableList(true));
235
236 const char *thisErrorString = "Stopped in a context claiming to "
237 "capture a C++ object pointer, but "
238 "'this' isn't available; pretending we "
239 "are in a generic context";
240
241 if (!variable_list_sp) {
242 err.SetErrorString(thisErrorString);
243 return;
244 }
245
246 lldb::VariableSP this_var_sp(
247 variable_list_sp->FindVariable(ConstString("this")));
248
249 if (!this_var_sp || !this_var_sp->IsInScope(frame) ||
250 !this_var_sp->LocationIsValidForFrame(frame)) {
251 err.SetErrorString(thisErrorString);
252 return;
253 }
254 }
255
256 m_in_cplusplus_method = true;
257 m_needs_object_ptr = true;
258 } else if (language == lldb::eLanguageTypeObjC) {
259 if (m_enforce_valid_object) {
260 lldb::VariableListSP variable_list_sp(
261 function_block->GetBlockVariableList(true));
262
263 const char *selfErrorString =
264 "Stopped in a context claiming to capture an Objective-C object "
265 "pointer, but 'self' isn't available; pretending we are in a "
266 "generic context";
267
268 if (!variable_list_sp) {
269 err.SetErrorString(selfErrorString);
270 return;
271 }
272
273 lldb::VariableSP self_variable_sp =
274 variable_list_sp->FindVariable(ConstString("self"));
275
276 if (!self_variable_sp || !self_variable_sp->IsInScope(frame) ||
277 !self_variable_sp->LocationIsValidForFrame(frame)) {
278 err.SetErrorString(selfErrorString);
279 return;
280 }
281
282 Type *self_type = self_variable_sp->GetType();
283
284 if (!self_type) {
285 err.SetErrorString(selfErrorString);
286 return;
287 }
288
289 CompilerType self_clang_type = self_type->GetForwardCompilerType();
290
291 if (!self_clang_type) {
292 err.SetErrorString(selfErrorString);
293 return;
294 }
295
296 if (ClangASTContext::IsObjCClassType(self_clang_type)) {
297 return;
298 } else if (ClangASTContext::IsObjCObjectPointerType(
299 self_clang_type)) {
Dawn Perchik53f34c82015-07-01 00:54:02 +0000300 m_in_objectivec_method = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000301 m_needs_object_ptr = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 } else {
303 err.SetErrorString(selfErrorString);
304 return;
305 }
306 } else {
307 m_in_objectivec_method = true;
308 m_needs_object_ptr = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000309 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310 }
Sean Callanan3670ba52010-12-01 21:35:54 +0000311 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000313}
314
Adrian Prantl05097242018-04-30 16:49:04 +0000315// This is a really nasty hack, meant to fix Objective-C expressions of the
316// form (int)[myArray count]. Right now, because the type information for
317// count is not available, [myArray count] returns id, which can't be directly
318// cast to int without causing a clang error.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319static void ApplyObjcCastHack(std::string &expr) {
Sean Callanancf5498f2010-10-22 23:25:16 +0000320#define OBJC_CAST_HACK_FROM "(int)["
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321#define OBJC_CAST_HACK_TO "(int)(long long)["
Sean Callanancf5498f2010-10-22 23:25:16 +0000322
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 size_t from_offset;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000324
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
326 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1,
327 OBJC_CAST_HACK_TO);
Sean Callanancf5498f2010-10-22 23:25:16 +0000328
329#undef OBJC_CAST_HACK_TO
330#undef OBJC_CAST_HACK_FROM
331}
332
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000333bool ClangUserExpression::SetupPersistentState(DiagnosticManager &diagnostic_manager,
334 ExecutionContext &exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 if (Target *target = exe_ctx.GetTargetPtr()) {
336 if (PersistentExpressionState *persistent_state =
337 target->GetPersistentExpressionStateForLanguage(
338 lldb::eLanguageTypeC)) {
339 m_result_delegate.RegisterPersistentState(persistent_state);
340 } else {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000341 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 eDiagnosticSeverityError,
343 "couldn't start parsing (no persistent data)");
344 return false;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000345 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 } else {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000347 diagnostic_manager.PutString(eDiagnosticSeverityError,
348 "error: couldn't start parsing (no target)");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 return false;
350 }
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000351 return true;
352}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000354static void SetupDeclVendor(ExecutionContext &exe_ctx, Target *target) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 if (ClangModulesDeclVendor *decl_vendor =
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000356 target->GetClangModulesDeclVendor()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357 const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
358 llvm::cast<ClangPersistentVariables>(
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000359 target->GetPersistentExpressionStateForLanguage(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 lldb::eLanguageTypeC))
361 ->GetHandLoadedClangModules();
362 ClangModulesDeclVendor::ModuleVector modules_for_macros;
363
364 for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
365 modules_for_macros.push_back(module);
366 }
367
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000368 if (target->GetEnableAutoImportClangModules()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
370 if (Block *block = frame->GetFrameBlock()) {
371 SymbolContext sc;
372
373 block->CalculateSymbolContext(&sc);
374
375 if (sc.comp_unit) {
376 StreamString error_stream;
377
378 decl_vendor->AddModulesForCompileUnit(
379 *sc.comp_unit, modules_for_macros, error_stream);
380 }
381 }
382 }
383 }
384 }
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000385}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386
Raphael Isemanndababf72018-09-27 10:12:54 +0000387void ClangUserExpression::UpdateLanguageForExpr(
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000388 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
Raphael Isemann71569d02019-05-02 10:12:56 +0000389 std::vector<std::string> modules_to_import, bool for_completion) {
Raphael Isemanndababf72018-09-27 10:12:54 +0000390 m_expr_lang = lldb::LanguageType::eLanguageTypeUnknown;
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000391
392 std::string prefix = m_expr_prefix;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393
394 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
395 m_transformed_text = m_expr_text;
396 } else {
Jim Inghamea401ec2019-03-06 22:43:25 +0000397 std::unique_ptr<ClangExpressionSourceCode> source_code(
398 ClangExpressionSourceCode::CreateWrapped(prefix.c_str(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399 m_expr_text.c_str()));
400
401 if (m_in_cplusplus_method)
Raphael Isemanndababf72018-09-27 10:12:54 +0000402 m_expr_lang = lldb::eLanguageTypeC_plus_plus;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 else if (m_in_objectivec_method)
Raphael Isemanndababf72018-09-27 10:12:54 +0000404 m_expr_lang = lldb::eLanguageTypeObjC;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000405 else
Raphael Isemanndababf72018-09-27 10:12:54 +0000406 m_expr_lang = lldb::eLanguageTypeC;
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 Isemann71569d02019-05-02 10:12:56 +0000515 UpdateLanguageForExpr(diagnostic_manager, exe_ctx, used_modules,
516 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000530 if (log)
531 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
532
533 ////////////////////////////////////
534 // Set up the target and compiler
535 //
536
537 Target *target = exe_ctx.GetTargetPtr();
538
539 if (!target) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000540 diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000541 return false;
542 }
543
544 //////////////////////////
545 // Parse the expression
546 //
547
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000548 m_materializer_up.reset(new Materializer());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000549
550 ResetDeclMap(exe_ctx, m_result_delegate, keep_result_in_memory);
551
Tatyana Krasnukhaedebf122019-04-26 16:41:04 +0000552 auto on_exit = llvm::make_scope_exit([this]() { ResetDeclMap(); });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000554 if (!DeclMap()->WillParse(exe_ctx, m_materializer_up.get())) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000555 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556 eDiagnosticSeverityError,
557 "current process state is unsuitable for expression parsing");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558 return false;
559 }
560
561 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
562 DeclMap()->SetLookupsEnabled(true);
563 }
564
565 Process *process = exe_ctx.GetProcessPtr();
566 ExecutionContextScope *exe_scope = process;
567
568 if (!exe_scope)
569 exe_scope = exe_ctx.GetTargetPtr();
570
571 // We use a shared pointer here so we can use the original parser - if it
Adrian Prantl05097242018-04-30 16:49:04 +0000572 // succeeds or the rewrite parser we might make if it fails. But the
573 // parser_sp will never be empty.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000574
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000575 ClangExpressionParser parser(exe_scope, *this, generate_debug_info,
576 m_include_directories);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577
578 unsigned num_errors = parser.Parse(diagnostic_manager);
579
580 // Check here for FixItHints. If there are any try to apply the fixits and
Adrian Prantl05097242018-04-30 16:49:04 +0000581 // set the fixed text in m_fixed_text before returning an error.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000582 if (num_errors) {
583 if (diagnostic_manager.HasFixIts()) {
584 if (parser.RewriteExpression(diagnostic_manager)) {
585 size_t fixed_start;
586 size_t fixed_end;
587 const std::string &fixed_expression =
588 diagnostic_manager.GetFixedExpression();
Jim Inghamea401ec2019-03-06 22:43:25 +0000589 if (ClangExpressionSourceCode::GetOriginalBodyBounds(
Raphael Isemanndababf72018-09-27 10:12:54 +0000590 fixed_expression, m_expr_lang, fixed_start, fixed_end))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591 m_fixed_text =
592 fixed_expression.substr(fixed_start, fixed_end - fixed_start);
593 }
Sean Callanan9fda9d22015-10-03 09:09:01 +0000594 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000595 return false;
596 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000597
Raphael Isemann54d99bf2019-04-24 12:21:03 +0000598 //////////////////////////////////////////////////////////////////////////////
Adrian Prantl05097242018-04-30 16:49:04 +0000599 // Prepare the output of the parser for execution, evaluating it statically
600 // if possible
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000602
Kate Stoneb9c1b512016-09-06 20:57:50 +0000603 {
Zachary Turner97206d52017-05-12 04:51:55 +0000604 Status jit_error = parser.PrepareForExecution(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000605 m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,
606 m_can_interpret, execution_policy);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000607
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608 if (!jit_error.Success()) {
609 const char *error_cstr = jit_error.AsCString();
610 if (error_cstr && error_cstr[0])
Zachary Turnere2411fa2016-11-12 19:12:56 +0000611 diagnostic_manager.PutString(eDiagnosticSeverityError, error_cstr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612 else
Zachary Turnere2411fa2016-11-12 19:12:56 +0000613 diagnostic_manager.PutString(eDiagnosticSeverityError,
614 "expression can't be interpreted or run");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 return false;
616 }
617 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000618
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619 if (exe_ctx.GetProcessPtr() && execution_policy == eExecutionPolicyTopLevel) {
Zachary Turner97206d52017-05-12 04:51:55 +0000620 Status static_init_error =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621 parser.RunStaticInitializers(m_execution_unit_sp, exe_ctx);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000622
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 if (!static_init_error.Success()) {
624 const char *error_cstr = static_init_error.AsCString();
625 if (error_cstr && error_cstr[0])
626 diagnostic_manager.Printf(eDiagnosticSeverityError,
627 "couldn't run static initializers: %s\n",
628 error_cstr);
629 else
Zachary Turnere2411fa2016-11-12 19:12:56 +0000630 diagnostic_manager.PutString(eDiagnosticSeverityError,
631 "couldn't run static initializers\n");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000632 return false;
633 }
634 }
635
636 if (m_execution_unit_sp) {
637 bool register_execution_unit = false;
638
639 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
640 register_execution_unit = true;
Sean Callananb9951192011-08-01 18:18:33 +0000641 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000642
Adrian Prantl05097242018-04-30 16:49:04 +0000643 // If there is more than one external function in the execution unit, it
644 // needs to keep living even if it's not top level, because the result
645 // could refer to that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000646
647 if (m_execution_unit_sp->GetJittedFunctions().size() > 1) {
648 register_execution_unit = true;
Sean Callanan00294b32016-03-22 21:05:51 +0000649 }
650
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651 if (register_execution_unit) {
652 llvm::cast<PersistentExpressionState>(
653 exe_ctx.GetTargetPtr()->GetPersistentExpressionStateForLanguage(
654 m_language))
655 ->RegisterExecutionUnit(m_execution_unit_sp);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000656 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000657 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000658
Kate Stoneb9c1b512016-09-06 20:57:50 +0000659 if (generate_debug_info) {
660 lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000661
Kate Stoneb9c1b512016-09-06 20:57:50 +0000662 if (jit_module_sp) {
663 ConstString const_func_name(FunctionName());
664 FileSpec jit_file;
665 jit_file.GetFilename() = const_func_name;
666 jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());
667 m_jit_module_wp = jit_module_sp;
668 target->GetImages().Append(jit_module_sp);
Sean Callanan00294b32016-03-22 21:05:51 +0000669 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000670 }
Sean Callanan00294b32016-03-22 21:05:51 +0000671
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672 if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
673 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
674 return true;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000675}
676
Raphael Isemann74829732018-08-30 17:29:37 +0000677/// Converts an absolute position inside a given code string into
678/// a column/line pair.
679///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000680/// \param[in] abs_pos
Raphael Isemann74829732018-08-30 17:29:37 +0000681/// A absolute position in the code string that we want to convert
682/// to a column/line pair.
683///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000684/// \param[in] code
Raphael Isemann74829732018-08-30 17:29:37 +0000685/// A multi-line string usually representing source code.
686///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000687/// \param[out] line
Raphael Isemann74829732018-08-30 17:29:37 +0000688/// The line in the code that contains the given absolute position.
689/// The first line in the string is indexed as 1.
690///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000691/// \param[out] column
Raphael Isemann74829732018-08-30 17:29:37 +0000692/// The column in the line that contains the absolute position.
693/// The first character in a line is indexed as 0.
Raphael Isemannbfc5ef62018-09-22 13:33:08 +0000694static void AbsPosToLineColumnPos(size_t abs_pos, llvm::StringRef code,
Raphael Isemann74829732018-08-30 17:29:37 +0000695 unsigned &line, unsigned &column) {
696 // Reset to code position to beginning of the file.
697 line = 0;
698 column = 0;
699
700 assert(abs_pos <= code.size() && "Absolute position outside code string?");
701
702 // We have to walk up to the position and count lines/columns.
703 for (std::size_t i = 0; i < abs_pos; ++i) {
704 // If we hit a line break, we go back to column 0 and enter a new line.
705 // We only handle \n because that's what we internally use to make new
706 // lines for our temporary code strings.
707 if (code[i] == '\n') {
708 ++line;
709 column = 0;
710 continue;
711 }
712 ++column;
713 }
714}
715
716bool ClangUserExpression::Complete(ExecutionContext &exe_ctx,
Raphael Isemannc11a7802018-08-30 21:26:32 +0000717 CompletionRequest &request,
718 unsigned complete_pos) {
Raphael Isemann74829732018-08-30 17:29:37 +0000719 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
720
721 // We don't want any visible feedback when completing an expression. Mostly
722 // because the results we get from an incomplete invocation are probably not
723 // correct.
724 DiagnosticManager diagnostic_manager;
725
Raphael Isemann71569d02019-05-02 10:12:56 +0000726 if (!PrepareForParsing(diagnostic_manager, exe_ctx, /*for_completion*/ true))
Raphael Isemann74829732018-08-30 17:29:37 +0000727 return false;
728
Raphael Isemann74829732018-08-30 17:29:37 +0000729 if (log)
730 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
731
732 //////////////////////////
733 // Parse the expression
734 //
735
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000736 m_materializer_up.reset(new Materializer());
Raphael Isemann74829732018-08-30 17:29:37 +0000737
738 ResetDeclMap(exe_ctx, m_result_delegate, /*keep result in memory*/ true);
739
Tatyana Krasnukhaedebf122019-04-26 16:41:04 +0000740 auto on_exit = llvm::make_scope_exit([this]() { ResetDeclMap(); });
Raphael Isemann74829732018-08-30 17:29:37 +0000741
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000742 if (!DeclMap()->WillParse(exe_ctx, m_materializer_up.get())) {
Raphael Isemann74829732018-08-30 17:29:37 +0000743 diagnostic_manager.PutString(
744 eDiagnosticSeverityError,
745 "current process state is unsuitable for expression parsing");
746
747 return false;
748 }
749
750 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
751 DeclMap()->SetLookupsEnabled(true);
752 }
753
754 Process *process = exe_ctx.GetProcessPtr();
755 ExecutionContextScope *exe_scope = process;
756
757 if (!exe_scope)
758 exe_scope = exe_ctx.GetTargetPtr();
759
760 ClangExpressionParser parser(exe_scope, *this, false);
761
762 // We have to find the source code location where the user text is inside
763 // the transformed expression code. When creating the transformed text, we
764 // already stored the absolute position in the m_transformed_text string. The
765 // only thing left to do is to transform it into the line:column format that
766 // Clang expects.
767
768 // The line and column of the user expression inside the transformed source
769 // code.
770 unsigned user_expr_line, user_expr_column;
771 if (m_user_expression_start_pos.hasValue())
772 AbsPosToLineColumnPos(*m_user_expression_start_pos, m_transformed_text,
773 user_expr_line, user_expr_column);
774 else
775 return false;
776
777 // The actual column where we have to complete is the start column of the
778 // user expression + the offset inside the user code that we were given.
779 const unsigned completion_column = user_expr_column + complete_pos;
Raphael Isemannc11a7802018-08-30 21:26:32 +0000780 parser.Complete(request, user_expr_line, completion_column, complete_pos);
Raphael Isemann74829732018-08-30 17:29:37 +0000781
782 return true;
783}
784
Kate Stoneb9c1b512016-09-06 20:57:50 +0000785bool ClangUserExpression::AddArguments(ExecutionContext &exe_ctx,
786 std::vector<lldb::addr_t> &args,
787 lldb::addr_t struct_address,
788 DiagnosticManager &diagnostic_manager) {
789 lldb::addr_t object_ptr = LLDB_INVALID_ADDRESS;
790 lldb::addr_t cmd_ptr = LLDB_INVALID_ADDRESS;
Sean Callanan579e70c2016-03-19 00:03:59 +0000791
Kate Stoneb9c1b512016-09-06 20:57:50 +0000792 if (m_needs_object_ptr) {
793 lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
794 if (!frame_sp)
795 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000796
Kate Stoneb9c1b512016-09-06 20:57:50 +0000797 ConstString object_name;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000798
Kate Stoneb9c1b512016-09-06 20:57:50 +0000799 if (m_in_cplusplus_method) {
800 object_name.SetCString("this");
801 } else if (m_in_objectivec_method) {
802 object_name.SetCString("self");
803 } else {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000804 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000805 eDiagnosticSeverityError,
806 "need object pointer but don't know the language");
807 return false;
Jim Ingham51148e92015-11-05 00:24:18 +0000808 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000809
Zachary Turner97206d52017-05-12 04:51:55 +0000810 Status object_ptr_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000811
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000812 if (m_ctx_obj) {
813 AddressType address_type;
814 object_ptr = m_ctx_obj->GetAddressOf(false, &address_type);
815 if (object_ptr == LLDB_INVALID_ADDRESS ||
816 address_type != eAddressTypeLoad)
817 object_ptr_error.SetErrorString("Can't get context object's "
818 "debuggee address");
819 } else
820 object_ptr = GetObjectPointer(frame_sp, object_name, object_ptr_error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000821
822 if (!object_ptr_error.Success()) {
823 exe_ctx.GetTargetRef().GetDebugger().GetAsyncOutputStream()->Printf(
Jason Molenda599558e2017-02-21 05:09:26 +0000824 "warning: `%s' is not accessible (substituting 0)\n",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000825 object_name.AsCString());
826 object_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000827 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828
829 if (m_in_objectivec_method) {
830 ConstString cmd_name("_cmd");
831
832 cmd_ptr = GetObjectPointer(frame_sp, cmd_name, object_ptr_error);
833
834 if (!object_ptr_error.Success()) {
835 diagnostic_manager.Printf(
836 eDiagnosticSeverityWarning,
837 "couldn't get cmd pointer (substituting NULL): %s",
838 object_ptr_error.AsCString());
839 cmd_ptr = 0;
840 }
841 }
842
843 args.push_back(object_ptr);
844
845 if (m_in_objectivec_method)
846 args.push_back(cmd_ptr);
847
848 args.push_back(struct_address);
849 } else {
850 args.push_back(struct_address);
851 }
852 return true;
Jim Ingham36f3b362010-10-14 23:45:03 +0000853}
854
Kate Stoneb9c1b512016-09-06 20:57:50 +0000855lldb::ExpressionVariableSP ClangUserExpression::GetResultAfterDematerialization(
856 ExecutionContextScope *exe_scope) {
857 return m_result_delegate.GetVariable();
Sean Callanan9fda9d22015-10-03 09:09:01 +0000858}
859
Kate Stoneb9c1b512016-09-06 20:57:50 +0000860void ClangUserExpression::ClangUserExpressionHelper::ResetDeclMap(
861 ExecutionContext &exe_ctx,
862 Materializer::PersistentVariableDelegate &delegate,
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000863 bool keep_result_in_memory,
864 ValueObject *ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865 m_expr_decl_map_up.reset(
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000866 new ClangExpressionDeclMap(keep_result_in_memory, &delegate, exe_ctx,
867 ctx_obj));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000868}
Jim Ingham36f3b362010-10-14 23:45:03 +0000869
Jim Ingham151c0322015-09-15 21:13:50 +0000870clang::ASTConsumer *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000871ClangUserExpression::ClangUserExpressionHelper::ASTTransformer(
872 clang::ASTConsumer *passthrough) {
873 m_result_synthesizer_up.reset(
874 new ASTResultSynthesizer(passthrough, m_top_level, m_target));
Sean Callananc673a6e2010-12-07 10:00:20 +0000875
Kate Stoneb9c1b512016-09-06 20:57:50 +0000876 return m_result_synthesizer_up.get();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000877}
878
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879void ClangUserExpression::ClangUserExpressionHelper::CommitPersistentDecls() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000880 if (m_result_synthesizer_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000881 m_result_synthesizer_up->CommitPersistentDecls();
882 }
Sean Callanan00294b32016-03-22 21:05:51 +0000883}
884
Kate Stoneb9c1b512016-09-06 20:57:50 +0000885ConstString ClangUserExpression::ResultDelegate::GetName() {
Adrian Prantl03219f72018-04-30 23:59:17 +0000886 auto prefix = m_persistent_state->GetPersistentVariablePrefix();
887 return m_persistent_state->GetNextPersistentVariableName(*m_target_sp,
888 prefix);
Sean Callanan9fda9d22015-10-03 09:09:01 +0000889}
890
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891void ClangUserExpression::ResultDelegate::DidDematerialize(
892 lldb::ExpressionVariableSP &variable) {
893 m_variable = variable;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000894}
895
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896void ClangUserExpression::ResultDelegate::RegisterPersistentState(
897 PersistentExpressionState *persistent_state) {
898 m_persistent_state = persistent_state;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000899}
900
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901lldb::ExpressionVariableSP &ClangUserExpression::ResultDelegate::GetVariable() {
902 return m_variable;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000903}