blob: 63068dc30dcc07ec6144f93fac4cafcf06f61ba5 [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
Sean Callanan1a8d4092010-08-27 01:01:44 +000059using namespace lldb_private;
60
Kate Stoneb9c1b512016-09-06 20:57:50 +000061ClangUserExpression::ClangUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000062 ExecutionContextScope &exe_scope, llvm::StringRef expr,
63 llvm::StringRef prefix, lldb::LanguageType language,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000064 ResultType desired_type, const EvaluateExpressionOptions &options,
65 ValueObject *ctx_obj)
Zachary Turnerc5d7df92016-11-08 04:52:16 +000066 : LLVMUserExpression(exe_scope, expr, prefix, language, desired_type,
Adrian Prantl7e34d782019-03-13 19:46:30 +000067 options, eKindClangUserExpression),
Jonas Devlieghere70355ac2019-02-12 03:47:39 +000068 m_type_system_helper(*m_target_wp.lock(), options.GetExecutionPolicy() ==
69 eExecutionPolicyTopLevel),
70 m_result_delegate(exe_scope.CalculateTarget()), m_ctx_obj(ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 switch (m_language) {
72 case lldb::eLanguageTypeC_plus_plus:
73 m_allow_cxx = true;
74 break;
75 case lldb::eLanguageTypeObjC:
76 m_allow_objc = true;
77 break;
78 case lldb::eLanguageTypeObjC_plus_plus:
79 default:
80 m_allow_cxx = true;
81 m_allow_objc = true;
82 break;
83 }
Sean Callanan1a8d4092010-08-27 01:01:44 +000084}
85
Kate Stoneb9c1b512016-09-06 20:57:50 +000086ClangUserExpression::~ClangUserExpression() {}
Sean Callanan1a8d4092010-08-27 01:01:44 +000087
Zachary Turner97206d52017-05-12 04:51:55 +000088void ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Status &err) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan70385082012-12-01 00:08:33 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 if (log)
92 log->Printf("ClangUserExpression::ScanContext()");
93
94 m_target = exe_ctx.GetTargetPtr();
95
96 if (!(m_allow_cxx || m_allow_objc)) {
Sean Callanan70385082012-12-01 00:08:33 +000097 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 log->Printf(" [CUE::SC] Settings inhibit C++ and Objective-C");
99 return;
100 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 StackFrame *frame = exe_ctx.GetFramePtr();
103 if (frame == NULL) {
104 if (log)
105 log->Printf(" [CUE::SC] Null stack frame");
106 return;
107 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
110 lldb::eSymbolContextBlock);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 if (!sym_ctx.function) {
113 if (log)
114 log->Printf(" [CUE::SC] Null function");
115 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) {
122 if (log)
123 log->Printf(" [CUE::SC] Null function block");
124 return;
125 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 CompilerDeclContext decl_context = function_block->GetDeclContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 if (!decl_context) {
130 if (log)
131 log->Printf(" [CUE::SC] Null decl context");
132 return;
133 }
Greg Clayton685c88c2012-07-14 00:53:55 +0000134
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000135 if (m_ctx_obj) {
136 switch (m_ctx_obj->GetObjectRuntimeLanguage()) {
137 case lldb::eLanguageTypeC:
138 case lldb::eLanguageTypeC89:
139 case lldb::eLanguageTypeC99:
140 case lldb::eLanguageTypeC11:
141 case lldb::eLanguageTypeC_plus_plus:
142 case lldb::eLanguageTypeC_plus_plus_03:
143 case lldb::eLanguageTypeC_plus_plus_11:
144 case lldb::eLanguageTypeC_plus_plus_14:
145 m_in_cplusplus_method = true;
146 break;
147 case lldb::eLanguageTypeObjC:
148 case lldb::eLanguageTypeObjC_plus_plus:
149 m_in_objectivec_method = true;
150 break;
151 default:
152 break;
153 }
154 m_needs_object_ptr = true;
155 } else if (clang::CXXMethodDecl *method_decl =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 ClangASTContext::DeclContextGetAsCXXMethodDecl(decl_context)) {
157 if (m_allow_cxx && method_decl->isInstance()) {
158 if (m_enforce_valid_object) {
159 lldb::VariableListSP variable_list_sp(
160 function_block->GetBlockVariableList(true));
Greg Clayton685c88c2012-07-14 00:53:55 +0000161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 const char *thisErrorString = "Stopped in a C++ method, but 'this' "
163 "isn't available; pretending we are in a "
164 "generic context";
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 if (!variable_list_sp) {
167 err.SetErrorString(thisErrorString);
168 return;
Sean Callanan3670ba52010-12-01 21:35:54 +0000169 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170
171 lldb::VariableSP this_var_sp(
172 variable_list_sp->FindVariable(ConstString("this")));
173
174 if (!this_var_sp || !this_var_sp->IsInScope(frame) ||
175 !this_var_sp->LocationIsValidForFrame(frame)) {
176 err.SetErrorString(thisErrorString);
177 return;
178 }
179 }
180
181 m_in_cplusplus_method = true;
182 m_needs_object_ptr = true;
Sean Callanan3670ba52010-12-01 21:35:54 +0000183 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 } else if (clang::ObjCMethodDecl *method_decl =
185 ClangASTContext::DeclContextGetAsObjCMethodDecl(
186 decl_context)) {
187 if (m_allow_objc) {
188 if (m_enforce_valid_object) {
189 lldb::VariableListSP variable_list_sp(
190 function_block->GetBlockVariableList(true));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192 const char *selfErrorString = "Stopped in an Objective-C method, but "
193 "'self' isn't available; pretending we "
194 "are in a generic context";
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000195
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 if (!variable_list_sp) {
197 err.SetErrorString(selfErrorString);
198 return;
199 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 lldb::VariableSP self_variable_sp =
202 variable_list_sp->FindVariable(ConstString("self"));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000203
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 if (!self_variable_sp || !self_variable_sp->IsInScope(frame) ||
205 !self_variable_sp->LocationIsValidForFrame(frame)) {
206 err.SetErrorString(selfErrorString);
207 return;
208 }
209 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 m_in_objectivec_method = true;
212 m_needs_object_ptr = true;
213
214 if (!method_decl->isInstanceMethod())
215 m_in_static_method = true;
216 }
217 } else if (clang::FunctionDecl *function_decl =
218 ClangASTContext::DeclContextGetAsFunctionDecl(decl_context)) {
219 // We might also have a function that said in the debug information that it
Adrian Prantl05097242018-04-30 16:49:04 +0000220 // captured an object pointer. The best way to deal with getting to the
221 // ivars at present is by pretending that this is a method of a class in
222 // whatever runtime the debug info says the object pointer belongs to. Do
223 // that here.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224
225 ClangASTMetadata *metadata =
226 ClangASTContext::DeclContextGetMetaData(decl_context, function_decl);
227 if (metadata && metadata->HasObjectPtr()) {
228 lldb::LanguageType language = metadata->GetObjectPtrLanguage();
229 if (language == lldb::eLanguageTypeC_plus_plus) {
230 if (m_enforce_valid_object) {
231 lldb::VariableListSP variable_list_sp(
232 function_block->GetBlockVariableList(true));
233
234 const char *thisErrorString = "Stopped in a context claiming to "
235 "capture a C++ object pointer, but "
236 "'this' isn't available; pretending we "
237 "are in a generic context";
238
239 if (!variable_list_sp) {
240 err.SetErrorString(thisErrorString);
241 return;
242 }
243
244 lldb::VariableSP this_var_sp(
245 variable_list_sp->FindVariable(ConstString("this")));
246
247 if (!this_var_sp || !this_var_sp->IsInScope(frame) ||
248 !this_var_sp->LocationIsValidForFrame(frame)) {
249 err.SetErrorString(thisErrorString);
250 return;
251 }
252 }
253
254 m_in_cplusplus_method = true;
255 m_needs_object_ptr = true;
256 } else if (language == lldb::eLanguageTypeObjC) {
257 if (m_enforce_valid_object) {
258 lldb::VariableListSP variable_list_sp(
259 function_block->GetBlockVariableList(true));
260
261 const char *selfErrorString =
262 "Stopped in a context claiming to capture an Objective-C object "
263 "pointer, but 'self' isn't available; pretending we are in a "
264 "generic context";
265
266 if (!variable_list_sp) {
267 err.SetErrorString(selfErrorString);
268 return;
269 }
270
271 lldb::VariableSP self_variable_sp =
272 variable_list_sp->FindVariable(ConstString("self"));
273
274 if (!self_variable_sp || !self_variable_sp->IsInScope(frame) ||
275 !self_variable_sp->LocationIsValidForFrame(frame)) {
276 err.SetErrorString(selfErrorString);
277 return;
278 }
279
280 Type *self_type = self_variable_sp->GetType();
281
282 if (!self_type) {
283 err.SetErrorString(selfErrorString);
284 return;
285 }
286
287 CompilerType self_clang_type = self_type->GetForwardCompilerType();
288
289 if (!self_clang_type) {
290 err.SetErrorString(selfErrorString);
291 return;
292 }
293
294 if (ClangASTContext::IsObjCClassType(self_clang_type)) {
295 return;
296 } else if (ClangASTContext::IsObjCObjectPointerType(
297 self_clang_type)) {
Dawn Perchik53f34c82015-07-01 00:54:02 +0000298 m_in_objectivec_method = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000299 m_needs_object_ptr = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300 } else {
301 err.SetErrorString(selfErrorString);
302 return;
303 }
304 } else {
305 m_in_objectivec_method = true;
306 m_needs_object_ptr = true;
Sean Callanan9bc83842011-09-26 18:45:31 +0000307 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308 }
Sean Callanan3670ba52010-12-01 21:35:54 +0000309 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000311}
312
Adrian Prantl05097242018-04-30 16:49:04 +0000313// This is a really nasty hack, meant to fix Objective-C expressions of the
314// form (int)[myArray count]. Right now, because the type information for
315// count is not available, [myArray count] returns id, which can't be directly
316// cast to int without causing a clang error.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317static void ApplyObjcCastHack(std::string &expr) {
Sean Callanancf5498f2010-10-22 23:25:16 +0000318#define OBJC_CAST_HACK_FROM "(int)["
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319#define OBJC_CAST_HACK_TO "(int)(long long)["
Sean Callanancf5498f2010-10-22 23:25:16 +0000320
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321 size_t from_offset;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000322
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
324 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1,
325 OBJC_CAST_HACK_TO);
Sean Callanancf5498f2010-10-22 23:25:16 +0000326
327#undef OBJC_CAST_HACK_TO
328#undef OBJC_CAST_HACK_FROM
329}
330
Raphael Isemann12468902018-06-19 21:25:59 +0000331namespace {
332// Utility guard that calls a callback when going out of scope.
333class OnExit {
334public:
335 typedef std::function<void(void)> Callback;
336
337 OnExit(Callback const &callback) : m_callback(callback) {}
338
339 ~OnExit() { m_callback(); }
340
341private:
342 Callback m_callback;
343};
344} // namespace
345
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000346bool ClangUserExpression::SetupPersistentState(DiagnosticManager &diagnostic_manager,
347 ExecutionContext &exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 if (Target *target = exe_ctx.GetTargetPtr()) {
349 if (PersistentExpressionState *persistent_state =
350 target->GetPersistentExpressionStateForLanguage(
351 lldb::eLanguageTypeC)) {
352 m_result_delegate.RegisterPersistentState(persistent_state);
353 } else {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000354 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 eDiagnosticSeverityError,
356 "couldn't start parsing (no persistent data)");
357 return false;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000358 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359 } else {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000360 diagnostic_manager.PutString(eDiagnosticSeverityError,
361 "error: couldn't start parsing (no target)");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 return false;
363 }
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000364 return true;
365}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000367static void SetupDeclVendor(ExecutionContext &exe_ctx, Target *target) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368 if (ClangModulesDeclVendor *decl_vendor =
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000369 target->GetClangModulesDeclVendor()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
371 llvm::cast<ClangPersistentVariables>(
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000372 target->GetPersistentExpressionStateForLanguage(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373 lldb::eLanguageTypeC))
374 ->GetHandLoadedClangModules();
375 ClangModulesDeclVendor::ModuleVector modules_for_macros;
376
377 for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
378 modules_for_macros.push_back(module);
379 }
380
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000381 if (target->GetEnableAutoImportClangModules()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
383 if (Block *block = frame->GetFrameBlock()) {
384 SymbolContext sc;
385
386 block->CalculateSymbolContext(&sc);
387
388 if (sc.comp_unit) {
389 StreamString error_stream;
390
391 decl_vendor->AddModulesForCompileUnit(
392 *sc.comp_unit, modules_for_macros, error_stream);
393 }
394 }
395 }
396 }
397 }
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000398}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399
Raphael Isemanndababf72018-09-27 10:12:54 +0000400void ClangUserExpression::UpdateLanguageForExpr(
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000401 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
402 std::vector<std::string> modules_to_import) {
Raphael Isemanndababf72018-09-27 10:12:54 +0000403 m_expr_lang = lldb::LanguageType::eLanguageTypeUnknown;
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000404
405 std::string prefix = m_expr_prefix;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406
407 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
408 m_transformed_text = m_expr_text;
409 } else {
Jim Inghamea401ec2019-03-06 22:43:25 +0000410 std::unique_ptr<ClangExpressionSourceCode> source_code(
411 ClangExpressionSourceCode::CreateWrapped(prefix.c_str(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 m_expr_text.c_str()));
413
414 if (m_in_cplusplus_method)
Raphael Isemanndababf72018-09-27 10:12:54 +0000415 m_expr_lang = lldb::eLanguageTypeC_plus_plus;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416 else if (m_in_objectivec_method)
Raphael Isemanndababf72018-09-27 10:12:54 +0000417 m_expr_lang = lldb::eLanguageTypeObjC;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000418 else
Raphael Isemanndababf72018-09-27 10:12:54 +0000419 m_expr_lang = lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420
Raphael Isemanndababf72018-09-27 10:12:54 +0000421 if (!source_code->GetText(m_transformed_text, m_expr_lang,
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000422 m_in_static_method, exe_ctx, !m_ctx_obj,
423 modules_to_import)) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000424 diagnostic_manager.PutString(eDiagnosticSeverityError,
425 "couldn't construct expression body");
Raphael Isemanndababf72018-09-27 10:12:54 +0000426 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427 }
Raphael Isemann74829732018-08-30 17:29:37 +0000428
429 // Find and store the start position of the original code inside the
430 // transformed code. We need this later for the code completion.
431 std::size_t original_start;
432 std::size_t original_end;
433 bool found_bounds = source_code->GetOriginalBodyBounds(
Raphael Isemanndababf72018-09-27 10:12:54 +0000434 m_transformed_text, m_expr_lang, original_start, original_end);
Raphael Isemann74829732018-08-30 17:29:37 +0000435 if (found_bounds) {
436 m_user_expression_start_pos = original_start;
437 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 }
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000439}
440
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000441static bool SupportsCxxModuleImport(lldb::LanguageType language) {
442 switch (language) {
443 case lldb::eLanguageTypeC_plus_plus:
444 case lldb::eLanguageTypeC_plus_plus_03:
445 case lldb::eLanguageTypeC_plus_plus_11:
446 case lldb::eLanguageTypeC_plus_plus_14:
447 case lldb::eLanguageTypeObjC_plus_plus:
448 return true;
449 default:
450 return false;
451 }
452}
453
454std::vector<std::string>
455ClangUserExpression::GetModulesToImport(ExecutionContext &exe_ctx) {
456 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
457
458 if (!SupportsCxxModuleImport(Language()))
459 return {};
460
461 Target *target = exe_ctx.GetTargetPtr();
462 if (!target || !target->GetEnableImportStdModule())
463 return {};
464
465 StackFrame *frame = exe_ctx.GetFramePtr();
466 if (!frame)
467 return {};
468
469 Block *block = frame->GetFrameBlock();
470 if (!block)
471 return {};
472
473 SymbolContext sc;
474 block->CalculateSymbolContext(&sc);
475 if (!sc.comp_unit)
476 return {};
477
478 if (log) {
479 for (const SourceModule &m : sc.comp_unit->GetImportedModules()) {
480 LLDB_LOG(log, "Found module in compile unit: {0:$[.]} - include dir: {1}",
481 llvm::make_range(m.path.begin(), m.path.end()), m.search_path);
482 }
483 }
484
485 for (const SourceModule &m : sc.comp_unit->GetImportedModules())
486 m_include_directories.push_back(m.search_path);
487
488 // Check if we imported 'std' or any of its submodules.
489 // We currently don't support importing any other modules in the expression
490 // parser.
491 for (const SourceModule &m : sc.comp_unit->GetImportedModules())
492 if (!m.path.empty() && m.path.front() == ConstString("std"))
493 return {"std"};
494
495 return {};
496}
497
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000498bool ClangUserExpression::PrepareForParsing(
499 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx) {
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000500 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
501
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000502 InstallContext(exe_ctx);
503
504 if (!SetupPersistentState(diagnostic_manager, exe_ctx))
505 return false;
506
507 Status err;
508 ScanContext(exe_ctx, err);
509
510 if (!err.Success()) {
511 diagnostic_manager.PutString(eDiagnosticSeverityWarning, err.AsCString());
512 }
513
514 ////////////////////////////////////
515 // Generate the expression
516 //
517
518 ApplyObjcCastHack(m_expr_text);
519
520 SetupDeclVendor(exe_ctx, m_target);
Raphael Isemanndababf72018-09-27 10:12:54 +0000521
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000522 std::vector<std::string> used_modules = GetModulesToImport(exe_ctx);
523 m_imported_cpp_modules = !used_modules.empty();
524
525 LLDB_LOG(log, "List of imported modules in expression: {0}",
526 llvm::make_range(used_modules.begin(), used_modules.end()));
527
528 UpdateLanguageForExpr(diagnostic_manager, exe_ctx, used_modules);
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000529 return true;
530}
531
532bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
533 ExecutionContext &exe_ctx,
534 lldb_private::ExecutionPolicy execution_policy,
535 bool keep_result_in_memory,
536 bool generate_debug_info) {
537 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
538
539 if (!PrepareForParsing(diagnostic_manager, exe_ctx))
540 return false;
541
Kate Stoneb9c1b512016-09-06 20:57:50 +0000542 if (log)
543 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
544
545 ////////////////////////////////////
546 // Set up the target and compiler
547 //
548
549 Target *target = exe_ctx.GetTargetPtr();
550
551 if (!target) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000552 diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553 return false;
554 }
555
556 //////////////////////////
557 // Parse the expression
558 //
559
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000560 m_materializer_up.reset(new Materializer());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561
562 ResetDeclMap(exe_ctx, m_result_delegate, keep_result_in_memory);
563
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 OnExit on_exit([this]() { ResetDeclMap(); });
565
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000566 if (!DeclMap()->WillParse(exe_ctx, m_materializer_up.get())) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000567 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568 eDiagnosticSeverityError,
569 "current process state is unsuitable for expression parsing");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570 return false;
571 }
572
573 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
574 DeclMap()->SetLookupsEnabled(true);
575 }
576
577 Process *process = exe_ctx.GetProcessPtr();
578 ExecutionContextScope *exe_scope = process;
579
580 if (!exe_scope)
581 exe_scope = exe_ctx.GetTargetPtr();
582
583 // We use a shared pointer here so we can use the original parser - if it
Adrian Prantl05097242018-04-30 16:49:04 +0000584 // succeeds or the rewrite parser we might make if it fails. But the
585 // parser_sp will never be empty.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000587 ClangExpressionParser parser(exe_scope, *this, generate_debug_info,
588 m_include_directories);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589
590 unsigned num_errors = parser.Parse(diagnostic_manager);
591
592 // Check here for FixItHints. If there are any try to apply the fixits and
Adrian Prantl05097242018-04-30 16:49:04 +0000593 // set the fixed text in m_fixed_text before returning an error.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 if (num_errors) {
595 if (diagnostic_manager.HasFixIts()) {
596 if (parser.RewriteExpression(diagnostic_manager)) {
597 size_t fixed_start;
598 size_t fixed_end;
599 const std::string &fixed_expression =
600 diagnostic_manager.GetFixedExpression();
Jim Inghamea401ec2019-03-06 22:43:25 +0000601 if (ClangExpressionSourceCode::GetOriginalBodyBounds(
Raphael Isemanndababf72018-09-27 10:12:54 +0000602 fixed_expression, m_expr_lang, fixed_start, fixed_end))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000603 m_fixed_text =
604 fixed_expression.substr(fixed_start, fixed_end - fixed_start);
605 }
Sean Callanan9fda9d22015-10-03 09:09:01 +0000606 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607 return false;
608 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000609
Raphael Isemann54d99bf2019-04-24 12:21:03 +0000610 //////////////////////////////////////////////////////////////////////////////
Adrian Prantl05097242018-04-30 16:49:04 +0000611 // Prepare the output of the parser for execution, evaluating it statically
612 // if possible
Kate Stoneb9c1b512016-09-06 20:57:50 +0000613 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000614
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 {
Zachary Turner97206d52017-05-12 04:51:55 +0000616 Status jit_error = parser.PrepareForExecution(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000617 m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,
618 m_can_interpret, execution_policy);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000619
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620 if (!jit_error.Success()) {
621 const char *error_cstr = jit_error.AsCString();
622 if (error_cstr && error_cstr[0])
Zachary Turnere2411fa2016-11-12 19:12:56 +0000623 diagnostic_manager.PutString(eDiagnosticSeverityError, error_cstr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000624 else
Zachary Turnere2411fa2016-11-12 19:12:56 +0000625 diagnostic_manager.PutString(eDiagnosticSeverityError,
626 "expression can't be interpreted or run");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627 return false;
628 }
629 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000630
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 if (exe_ctx.GetProcessPtr() && execution_policy == eExecutionPolicyTopLevel) {
Zachary Turner97206d52017-05-12 04:51:55 +0000632 Status static_init_error =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000633 parser.RunStaticInitializers(m_execution_unit_sp, exe_ctx);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000634
Kate Stoneb9c1b512016-09-06 20:57:50 +0000635 if (!static_init_error.Success()) {
636 const char *error_cstr = static_init_error.AsCString();
637 if (error_cstr && error_cstr[0])
638 diagnostic_manager.Printf(eDiagnosticSeverityError,
639 "couldn't run static initializers: %s\n",
640 error_cstr);
641 else
Zachary Turnere2411fa2016-11-12 19:12:56 +0000642 diagnostic_manager.PutString(eDiagnosticSeverityError,
643 "couldn't run static initializers\n");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644 return false;
645 }
646 }
647
648 if (m_execution_unit_sp) {
649 bool register_execution_unit = false;
650
651 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
652 register_execution_unit = true;
Sean Callananb9951192011-08-01 18:18:33 +0000653 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000654
Adrian Prantl05097242018-04-30 16:49:04 +0000655 // If there is more than one external function in the execution unit, it
656 // needs to keep living even if it's not top level, because the result
657 // could refer to that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000658
659 if (m_execution_unit_sp->GetJittedFunctions().size() > 1) {
660 register_execution_unit = true;
Sean Callanan00294b32016-03-22 21:05:51 +0000661 }
662
Kate Stoneb9c1b512016-09-06 20:57:50 +0000663 if (register_execution_unit) {
664 llvm::cast<PersistentExpressionState>(
665 exe_ctx.GetTargetPtr()->GetPersistentExpressionStateForLanguage(
666 m_language))
667 ->RegisterExecutionUnit(m_execution_unit_sp);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000668 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000670
Kate Stoneb9c1b512016-09-06 20:57:50 +0000671 if (generate_debug_info) {
672 lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000673
Kate Stoneb9c1b512016-09-06 20:57:50 +0000674 if (jit_module_sp) {
675 ConstString const_func_name(FunctionName());
676 FileSpec jit_file;
677 jit_file.GetFilename() = const_func_name;
678 jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());
679 m_jit_module_wp = jit_module_sp;
680 target->GetImages().Append(jit_module_sp);
Sean Callanan00294b32016-03-22 21:05:51 +0000681 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000682 }
Sean Callanan00294b32016-03-22 21:05:51 +0000683
Kate Stoneb9c1b512016-09-06 20:57:50 +0000684 if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
685 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
686 return true;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000687}
688
Raphael Isemann74829732018-08-30 17:29:37 +0000689/// Converts an absolute position inside a given code string into
690/// a column/line pair.
691///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000692/// \param[in] abs_pos
Raphael Isemann74829732018-08-30 17:29:37 +0000693/// A absolute position in the code string that we want to convert
694/// to a column/line pair.
695///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000696/// \param[in] code
Raphael Isemann74829732018-08-30 17:29:37 +0000697/// A multi-line string usually representing source code.
698///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000699/// \param[out] line
Raphael Isemann74829732018-08-30 17:29:37 +0000700/// The line in the code that contains the given absolute position.
701/// The first line in the string is indexed as 1.
702///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000703/// \param[out] column
Raphael Isemann74829732018-08-30 17:29:37 +0000704/// The column in the line that contains the absolute position.
705/// The first character in a line is indexed as 0.
Raphael Isemannbfc5ef62018-09-22 13:33:08 +0000706static void AbsPosToLineColumnPos(size_t abs_pos, llvm::StringRef code,
Raphael Isemann74829732018-08-30 17:29:37 +0000707 unsigned &line, unsigned &column) {
708 // Reset to code position to beginning of the file.
709 line = 0;
710 column = 0;
711
712 assert(abs_pos <= code.size() && "Absolute position outside code string?");
713
714 // We have to walk up to the position and count lines/columns.
715 for (std::size_t i = 0; i < abs_pos; ++i) {
716 // If we hit a line break, we go back to column 0 and enter a new line.
717 // We only handle \n because that's what we internally use to make new
718 // lines for our temporary code strings.
719 if (code[i] == '\n') {
720 ++line;
721 column = 0;
722 continue;
723 }
724 ++column;
725 }
726}
727
728bool ClangUserExpression::Complete(ExecutionContext &exe_ctx,
Raphael Isemannc11a7802018-08-30 21:26:32 +0000729 CompletionRequest &request,
730 unsigned complete_pos) {
Raphael Isemann74829732018-08-30 17:29:37 +0000731 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
732
733 // We don't want any visible feedback when completing an expression. Mostly
734 // because the results we get from an incomplete invocation are probably not
735 // correct.
736 DiagnosticManager diagnostic_manager;
737
738 if (!PrepareForParsing(diagnostic_manager, exe_ctx))
739 return false;
740
Raphael Isemann74829732018-08-30 17:29:37 +0000741 if (log)
742 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
743
744 //////////////////////////
745 // Parse the expression
746 //
747
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000748 m_materializer_up.reset(new Materializer());
Raphael Isemann74829732018-08-30 17:29:37 +0000749
750 ResetDeclMap(exe_ctx, m_result_delegate, /*keep result in memory*/ true);
751
752 OnExit on_exit([this]() { ResetDeclMap(); });
753
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000754 if (!DeclMap()->WillParse(exe_ctx, m_materializer_up.get())) {
Raphael Isemann74829732018-08-30 17:29:37 +0000755 diagnostic_manager.PutString(
756 eDiagnosticSeverityError,
757 "current process state is unsuitable for expression parsing");
758
759 return false;
760 }
761
762 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
763 DeclMap()->SetLookupsEnabled(true);
764 }
765
766 Process *process = exe_ctx.GetProcessPtr();
767 ExecutionContextScope *exe_scope = process;
768
769 if (!exe_scope)
770 exe_scope = exe_ctx.GetTargetPtr();
771
772 ClangExpressionParser parser(exe_scope, *this, false);
773
774 // We have to find the source code location where the user text is inside
775 // the transformed expression code. When creating the transformed text, we
776 // already stored the absolute position in the m_transformed_text string. The
777 // only thing left to do is to transform it into the line:column format that
778 // Clang expects.
779
780 // The line and column of the user expression inside the transformed source
781 // code.
782 unsigned user_expr_line, user_expr_column;
783 if (m_user_expression_start_pos.hasValue())
784 AbsPosToLineColumnPos(*m_user_expression_start_pos, m_transformed_text,
785 user_expr_line, user_expr_column);
786 else
787 return false;
788
789 // The actual column where we have to complete is the start column of the
790 // user expression + the offset inside the user code that we were given.
791 const unsigned completion_column = user_expr_column + complete_pos;
Raphael Isemannc11a7802018-08-30 21:26:32 +0000792 parser.Complete(request, user_expr_line, completion_column, complete_pos);
Raphael Isemann74829732018-08-30 17:29:37 +0000793
794 return true;
795}
796
Kate Stoneb9c1b512016-09-06 20:57:50 +0000797bool ClangUserExpression::AddArguments(ExecutionContext &exe_ctx,
798 std::vector<lldb::addr_t> &args,
799 lldb::addr_t struct_address,
800 DiagnosticManager &diagnostic_manager) {
801 lldb::addr_t object_ptr = LLDB_INVALID_ADDRESS;
802 lldb::addr_t cmd_ptr = LLDB_INVALID_ADDRESS;
Sean Callanan579e70c2016-03-19 00:03:59 +0000803
Kate Stoneb9c1b512016-09-06 20:57:50 +0000804 if (m_needs_object_ptr) {
805 lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
806 if (!frame_sp)
807 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000808
Kate Stoneb9c1b512016-09-06 20:57:50 +0000809 ConstString object_name;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000810
Kate Stoneb9c1b512016-09-06 20:57:50 +0000811 if (m_in_cplusplus_method) {
812 object_name.SetCString("this");
813 } else if (m_in_objectivec_method) {
814 object_name.SetCString("self");
815 } else {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000816 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817 eDiagnosticSeverityError,
818 "need object pointer but don't know the language");
819 return false;
Jim Ingham51148e92015-11-05 00:24:18 +0000820 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000821
Zachary Turner97206d52017-05-12 04:51:55 +0000822 Status object_ptr_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000823
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000824 if (m_ctx_obj) {
825 AddressType address_type;
826 object_ptr = m_ctx_obj->GetAddressOf(false, &address_type);
827 if (object_ptr == LLDB_INVALID_ADDRESS ||
828 address_type != eAddressTypeLoad)
829 object_ptr_error.SetErrorString("Can't get context object's "
830 "debuggee address");
831 } else
832 object_ptr = GetObjectPointer(frame_sp, object_name, object_ptr_error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000833
834 if (!object_ptr_error.Success()) {
835 exe_ctx.GetTargetRef().GetDebugger().GetAsyncOutputStream()->Printf(
Jason Molenda599558e2017-02-21 05:09:26 +0000836 "warning: `%s' is not accessible (substituting 0)\n",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 object_name.AsCString());
838 object_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000839 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000840
841 if (m_in_objectivec_method) {
842 ConstString cmd_name("_cmd");
843
844 cmd_ptr = GetObjectPointer(frame_sp, cmd_name, object_ptr_error);
845
846 if (!object_ptr_error.Success()) {
847 diagnostic_manager.Printf(
848 eDiagnosticSeverityWarning,
849 "couldn't get cmd pointer (substituting NULL): %s",
850 object_ptr_error.AsCString());
851 cmd_ptr = 0;
852 }
853 }
854
855 args.push_back(object_ptr);
856
857 if (m_in_objectivec_method)
858 args.push_back(cmd_ptr);
859
860 args.push_back(struct_address);
861 } else {
862 args.push_back(struct_address);
863 }
864 return true;
Jim Ingham36f3b362010-10-14 23:45:03 +0000865}
866
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867lldb::ExpressionVariableSP ClangUserExpression::GetResultAfterDematerialization(
868 ExecutionContextScope *exe_scope) {
869 return m_result_delegate.GetVariable();
Sean Callanan9fda9d22015-10-03 09:09:01 +0000870}
871
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872void ClangUserExpression::ClangUserExpressionHelper::ResetDeclMap(
873 ExecutionContext &exe_ctx,
874 Materializer::PersistentVariableDelegate &delegate,
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000875 bool keep_result_in_memory,
876 ValueObject *ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000877 m_expr_decl_map_up.reset(
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000878 new ClangExpressionDeclMap(keep_result_in_memory, &delegate, exe_ctx,
879 ctx_obj));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000880}
Jim Ingham36f3b362010-10-14 23:45:03 +0000881
Jim Ingham151c0322015-09-15 21:13:50 +0000882clang::ASTConsumer *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000883ClangUserExpression::ClangUserExpressionHelper::ASTTransformer(
884 clang::ASTConsumer *passthrough) {
885 m_result_synthesizer_up.reset(
886 new ASTResultSynthesizer(passthrough, m_top_level, m_target));
Sean Callananc673a6e2010-12-07 10:00:20 +0000887
Kate Stoneb9c1b512016-09-06 20:57:50 +0000888 return m_result_synthesizer_up.get();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000889}
890
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891void ClangUserExpression::ClangUserExpressionHelper::CommitPersistentDecls() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000892 if (m_result_synthesizer_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893 m_result_synthesizer_up->CommitPersistentDecls();
894 }
Sean Callanan00294b32016-03-22 21:05:51 +0000895}
896
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897ConstString ClangUserExpression::ResultDelegate::GetName() {
Adrian Prantl03219f72018-04-30 23:59:17 +0000898 auto prefix = m_persistent_state->GetPersistentVariablePrefix();
899 return m_persistent_state->GetNextPersistentVariableName(*m_target_sp,
900 prefix);
Sean Callanan9fda9d22015-10-03 09:09:01 +0000901}
902
Kate Stoneb9c1b512016-09-06 20:57:50 +0000903void ClangUserExpression::ResultDelegate::DidDematerialize(
904 lldb::ExpressionVariableSP &variable) {
905 m_variable = variable;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000906}
907
Kate Stoneb9c1b512016-09-06 20:57:50 +0000908void ClangUserExpression::ResultDelegate::RegisterPersistentState(
909 PersistentExpressionState *persistent_state) {
910 m_persistent_state = persistent_state;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000911}
912
Kate Stoneb9c1b512016-09-06 20:57:50 +0000913lldb::ExpressionVariableSP &ClangUserExpression::ResultDelegate::GetVariable() {
914 return m_variable;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000915}