blob: da1ca785635c9d00abe3c06b41ca2ff69aef76b1 [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"
26#include "ClangModulesDeclVendor.h"
27#include "ClangPersistentVariables.h"
Raphael Isemann9379d192019-09-24 10:08:18 +000028#include "CppModuleConfiguration.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000029
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) {
Raphael Isemann3ee98a12019-09-17 07:58:01 +0000316 const std::string from = "(int)[";
317 const std::string to = "(int)(long long)[";
Sean Callanancf5498f2010-10-22 23:25:16 +0000318
Raphael Isemann3ee98a12019-09-17 07:58:01 +0000319 size_t offset;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000320
Raphael Isemann3ee98a12019-09-17 07:58:01 +0000321 while ((offset = expr.find(from)) != expr.npos)
322 expr.replace(offset, from.size(), to);
Sean Callanancf5498f2010-10-22 23:25:16 +0000323}
324
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000325bool ClangUserExpression::SetupPersistentState(DiagnosticManager &diagnostic_manager,
326 ExecutionContext &exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327 if (Target *target = exe_ctx.GetTargetPtr()) {
328 if (PersistentExpressionState *persistent_state =
329 target->GetPersistentExpressionStateForLanguage(
330 lldb::eLanguageTypeC)) {
Raphael Isemann1442efe2019-09-18 08:53:35 +0000331 m_clang_state = llvm::cast<ClangPersistentVariables>(persistent_state);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 m_result_delegate.RegisterPersistentState(persistent_state);
333 } else {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000334 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 eDiagnosticSeverityError,
336 "couldn't start parsing (no persistent data)");
337 return false;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000338 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 } else {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000340 diagnostic_manager.PutString(eDiagnosticSeverityError,
341 "error: couldn't start parsing (no target)");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 return false;
343 }
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000344 return true;
345}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000347static void SetupDeclVendor(ExecutionContext &exe_ctx, Target *target) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 if (ClangModulesDeclVendor *decl_vendor =
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000349 target->GetClangModulesDeclVendor()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
351 llvm::cast<ClangPersistentVariables>(
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000352 target->GetPersistentExpressionStateForLanguage(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 lldb::eLanguageTypeC))
354 ->GetHandLoadedClangModules();
355 ClangModulesDeclVendor::ModuleVector modules_for_macros;
356
357 for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
358 modules_for_macros.push_back(module);
359 }
360
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000361 if (target->GetEnableAutoImportClangModules()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
363 if (Block *block = frame->GetFrameBlock()) {
364 SymbolContext sc;
365
366 block->CalculateSymbolContext(&sc);
367
368 if (sc.comp_unit) {
369 StreamString error_stream;
370
371 decl_vendor->AddModulesForCompileUnit(
372 *sc.comp_unit, modules_for_macros, error_stream);
373 }
374 }
375 }
376 }
377 }
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000378}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379
Raphael Isemann5eb87cf2019-08-05 20:31:47 +0000380void ClangUserExpression::UpdateLanguageForExpr() {
381 m_expr_lang = lldb::LanguageType::eLanguageTypeUnknown;
382 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel)
383 return;
384 if (m_in_cplusplus_method)
385 m_expr_lang = lldb::eLanguageTypeC_plus_plus;
386 else if (m_in_objectivec_method)
387 m_expr_lang = lldb::eLanguageTypeObjC;
388 else
389 m_expr_lang = lldb::eLanguageTypeC;
390}
391
392void ClangUserExpression::CreateSourceCode(
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000393 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
Raphael Isemann71569d02019-05-02 10:12:56 +0000394 std::vector<std::string> modules_to_import, bool for_completion) {
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000395
Raphael Isemann1442efe2019-09-18 08:53:35 +0000396 m_filename = m_clang_state->GetNextExprFileName();
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000397 std::string prefix = m_expr_prefix;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398
399 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
400 m_transformed_text = m_expr_text;
401 } else {
Raphael Isemann1442efe2019-09-18 08:53:35 +0000402 m_source_code.reset(ClangExpressionSourceCode::CreateWrapped(
403 m_filename, prefix.c_str(), m_expr_text.c_str()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404
Raphael Isemann1442efe2019-09-18 08:53:35 +0000405 if (!m_source_code->GetText(m_transformed_text, m_expr_lang,
406 m_in_static_method, exe_ctx, !m_ctx_obj,
407 for_completion, modules_to_import)) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000408 diagnostic_manager.PutString(eDiagnosticSeverityError,
409 "couldn't construct expression body");
Raphael Isemanndababf72018-09-27 10:12:54 +0000410 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411 }
Raphael Isemann74829732018-08-30 17:29:37 +0000412
413 // Find and store the start position of the original code inside the
414 // transformed code. We need this later for the code completion.
415 std::size_t original_start;
416 std::size_t original_end;
Raphael Isemann1442efe2019-09-18 08:53:35 +0000417 bool found_bounds = m_source_code->GetOriginalBodyBounds(
Raphael Isemanndababf72018-09-27 10:12:54 +0000418 m_transformed_text, m_expr_lang, original_start, original_end);
Raphael Isemannd59c8d32019-04-24 12:55:00 +0000419 if (found_bounds)
Raphael Isemann74829732018-08-30 17:29:37 +0000420 m_user_expression_start_pos = original_start;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 }
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000422}
423
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000424static bool SupportsCxxModuleImport(lldb::LanguageType language) {
425 switch (language) {
426 case lldb::eLanguageTypeC_plus_plus:
427 case lldb::eLanguageTypeC_plus_plus_03:
428 case lldb::eLanguageTypeC_plus_plus_11:
429 case lldb::eLanguageTypeC_plus_plus_14:
430 case lldb::eLanguageTypeObjC_plus_plus:
431 return true;
432 default:
433 return false;
434 }
435}
436
Raphael Isemann9379d192019-09-24 10:08:18 +0000437/// Utility method that puts a message into the expression log and
438/// returns an invalid module configuration.
439static CppModuleConfiguration LogConfigError(const std::string &msg) {
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000440 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Raphael Isemann9379d192019-09-24 10:08:18 +0000441 LLDB_LOG(log, "[C++ module config] {0}", msg);
442 return CppModuleConfiguration();
443}
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000444
Raphael Isemann9379d192019-09-24 10:08:18 +0000445CppModuleConfiguration GetModuleConfig(lldb::LanguageType language,
446 ExecutionContext &exe_ctx) {
Raphael Isemannccd54a12019-10-10 13:43:37 +0000447 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
448
Raphael Isemann9379d192019-09-24 10:08:18 +0000449 // Don't do anything if this is not a C++ module configuration.
450 if (!SupportsCxxModuleImport(language))
451 return LogConfigError("Language doesn't support C++ modules");
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000452
453 Target *target = exe_ctx.GetTargetPtr();
Raphael Isemann9379d192019-09-24 10:08:18 +0000454 if (!target)
455 return LogConfigError("No target");
456
457 if (!target->GetEnableImportStdModule())
458 return LogConfigError("Importing std module not enabled in settings");
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000459
460 StackFrame *frame = exe_ctx.GetFramePtr();
461 if (!frame)
Raphael Isemann9379d192019-09-24 10:08:18 +0000462 return LogConfigError("No frame");
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000463
464 Block *block = frame->GetFrameBlock();
465 if (!block)
Raphael Isemann9379d192019-09-24 10:08:18 +0000466 return LogConfigError("No block");
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000467
468 SymbolContext sc;
469 block->CalculateSymbolContext(&sc);
470 if (!sc.comp_unit)
Raphael Isemann9379d192019-09-24 10:08:18 +0000471 return LogConfigError("Couldn't calculate symbol context");
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000472
Raphael Isemann9379d192019-09-24 10:08:18 +0000473 // Build a list of files we need to analyze to build the configuration.
474 FileSpecList files;
475 for (const FileSpec &f : sc.comp_unit->GetSupportFiles())
476 files.AppendIfUnique(f);
477 // We also need to look at external modules in the case of -gmodules as they
478 // contain the support files for libc++ and the C library.
479 sc.comp_unit->ForEachExternalModule([&files](lldb::ModuleSP module) {
480 for (std::size_t i = 0; i < module->GetNumCompileUnits(); ++i) {
481 const FileSpecList &support_files =
482 module->GetCompileUnitAtIndex(i)->GetSupportFiles();
483 for (const FileSpec &f : support_files) {
484 files.AppendIfUnique(f);
485 }
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000486 }
Raphael Isemann9379d192019-09-24 10:08:18 +0000487 });
Raphael Isemannccd54a12019-10-10 13:43:37 +0000488
489 LLDB_LOG(log, "[C++ module config] Found {0} support files to analyze",
490 files.GetSize());
491 if (log && log->GetVerbose()) {
492 for (const FileSpec &f : files)
493 LLDB_LOGV(log, "[C++ module config] Analyzing support file: {0}",
494 f.GetPath());
495 }
496
Raphael Isemann9379d192019-09-24 10:08:18 +0000497 // Try to create a configuration from the files. If there is no valid
498 // configuration possible with the files, this just returns an invalid
499 // configuration.
500 return CppModuleConfiguration(files);
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000501}
502
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000503bool ClangUserExpression::PrepareForParsing(
Raphael Isemann71569d02019-05-02 10:12:56 +0000504 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
505 bool for_completion) {
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000506 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
507
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000508 InstallContext(exe_ctx);
509
510 if (!SetupPersistentState(diagnostic_manager, exe_ctx))
511 return false;
512
513 Status err;
514 ScanContext(exe_ctx, err);
515
516 if (!err.Success()) {
517 diagnostic_manager.PutString(eDiagnosticSeverityWarning, err.AsCString());
518 }
519
520 ////////////////////////////////////
521 // Generate the expression
522 //
523
524 ApplyObjcCastHack(m_expr_text);
525
526 SetupDeclVendor(exe_ctx, m_target);
Raphael Isemanndababf72018-09-27 10:12:54 +0000527
Raphael Isemann9379d192019-09-24 10:08:18 +0000528 CppModuleConfiguration module_config = GetModuleConfig(m_language, exe_ctx);
529 llvm::ArrayRef<std::string> imported_modules =
530 module_config.GetImportedModules();
531 m_imported_cpp_modules = !imported_modules.empty();
532 m_include_directories = module_config.GetIncludeDirs();
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000533
534 LLDB_LOG(log, "List of imported modules in expression: {0}",
Raphael Isemann9379d192019-09-24 10:08:18 +0000535 llvm::make_range(imported_modules.begin(), imported_modules.end()));
536 LLDB_LOG(log, "List of include directories gathered for modules: {0}",
537 llvm::make_range(m_include_directories.begin(),
538 m_include_directories.end()));
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000539
Raphael Isemann5eb87cf2019-08-05 20:31:47 +0000540 UpdateLanguageForExpr();
Raphael Isemann9379d192019-09-24 10:08:18 +0000541 CreateSourceCode(diagnostic_manager, exe_ctx, imported_modules,
542 for_completion);
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000543 return true;
544}
545
546bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
547 ExecutionContext &exe_ctx,
548 lldb_private::ExecutionPolicy execution_policy,
549 bool keep_result_in_memory,
550 bool generate_debug_info) {
551 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
552
Raphael Isemann71569d02019-05-02 10:12:56 +0000553 if (!PrepareForParsing(diagnostic_manager, exe_ctx, /*for_completion*/ false))
Raphael Isemannba800e7d2018-07-10 22:12:39 +0000554 return false;
555
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000556 LLDB_LOGF(log, "Parsing the following code:\n%s", m_transformed_text.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557
558 ////////////////////////////////////
559 // Set up the target and compiler
560 //
561
562 Target *target = exe_ctx.GetTargetPtr();
563
564 if (!target) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000565 diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566 return false;
567 }
568
569 //////////////////////////
570 // Parse the expression
571 //
572
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000573 m_materializer_up.reset(new Materializer());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000574
575 ResetDeclMap(exe_ctx, m_result_delegate, keep_result_in_memory);
576
Tatyana Krasnukhaedebf122019-04-26 16:41:04 +0000577 auto on_exit = llvm::make_scope_exit([this]() { ResetDeclMap(); });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000579 if (!DeclMap()->WillParse(exe_ctx, m_materializer_up.get())) {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000580 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581 eDiagnosticSeverityError,
582 "current process state is unsuitable for expression parsing");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583 return false;
584 }
585
586 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
587 DeclMap()->SetLookupsEnabled(true);
588 }
589
590 Process *process = exe_ctx.GetProcessPtr();
591 ExecutionContextScope *exe_scope = process;
592
593 if (!exe_scope)
594 exe_scope = exe_ctx.GetTargetPtr();
595
596 // We use a shared pointer here so we can use the original parser - if it
Adrian Prantl05097242018-04-30 16:49:04 +0000597 // succeeds or the rewrite parser we might make if it fails. But the
598 // parser_sp will never be empty.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000599
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000600 ClangExpressionParser parser(exe_scope, *this, generate_debug_info,
Raphael Isemann1442efe2019-09-18 08:53:35 +0000601 m_include_directories, m_filename);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602
603 unsigned num_errors = parser.Parse(diagnostic_manager);
604
605 // Check here for FixItHints. If there are any try to apply the fixits and
Adrian Prantl05097242018-04-30 16:49:04 +0000606 // set the fixed text in m_fixed_text before returning an error.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607 if (num_errors) {
608 if (diagnostic_manager.HasFixIts()) {
609 if (parser.RewriteExpression(diagnostic_manager)) {
610 size_t fixed_start;
611 size_t fixed_end;
612 const std::string &fixed_expression =
613 diagnostic_manager.GetFixedExpression();
Raphael Isemann06e98a82019-09-25 07:34:56 +0000614 // Retrieve the original expression in case we don't have a top level
615 // expression (which has no surrounding source code).
616 if (m_source_code &&
617 m_source_code->GetOriginalBodyBounds(fixed_expression, m_expr_lang,
Raphael Isemann1442efe2019-09-18 08:53:35 +0000618 fixed_start, fixed_end))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619 m_fixed_text =
620 fixed_expression.substr(fixed_start, fixed_end - fixed_start);
621 }
Sean Callanan9fda9d22015-10-03 09:09:01 +0000622 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 return false;
624 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000625
Raphael Isemann54d99bf2019-04-24 12:21:03 +0000626 //////////////////////////////////////////////////////////////////////////////
Adrian Prantl05097242018-04-30 16:49:04 +0000627 // Prepare the output of the parser for execution, evaluating it statically
628 // if possible
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000630
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 {
Zachary Turner97206d52017-05-12 04:51:55 +0000632 Status jit_error = parser.PrepareForExecution(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000633 m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,
634 m_can_interpret, execution_policy);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000635
Kate Stoneb9c1b512016-09-06 20:57:50 +0000636 if (!jit_error.Success()) {
637 const char *error_cstr = jit_error.AsCString();
638 if (error_cstr && error_cstr[0])
Zachary Turnere2411fa2016-11-12 19:12:56 +0000639 diagnostic_manager.PutString(eDiagnosticSeverityError, error_cstr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 else
Zachary Turnere2411fa2016-11-12 19:12:56 +0000641 diagnostic_manager.PutString(eDiagnosticSeverityError,
642 "expression can't be interpreted or run");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643 return false;
644 }
645 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000646
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647 if (exe_ctx.GetProcessPtr() && execution_policy == eExecutionPolicyTopLevel) {
Zachary Turner97206d52017-05-12 04:51:55 +0000648 Status static_init_error =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000649 parser.RunStaticInitializers(m_execution_unit_sp, exe_ctx);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000650
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651 if (!static_init_error.Success()) {
652 const char *error_cstr = static_init_error.AsCString();
653 if (error_cstr && error_cstr[0])
654 diagnostic_manager.Printf(eDiagnosticSeverityError,
655 "couldn't run static initializers: %s\n",
656 error_cstr);
657 else
Zachary Turnere2411fa2016-11-12 19:12:56 +0000658 diagnostic_manager.PutString(eDiagnosticSeverityError,
659 "couldn't run static initializers\n");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000660 return false;
661 }
662 }
663
664 if (m_execution_unit_sp) {
665 bool register_execution_unit = false;
666
667 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
668 register_execution_unit = true;
Sean Callananb9951192011-08-01 18:18:33 +0000669 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000670
Adrian Prantl05097242018-04-30 16:49:04 +0000671 // If there is more than one external function in the execution unit, it
672 // needs to keep living even if it's not top level, because the result
673 // could refer to that function.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000674
675 if (m_execution_unit_sp->GetJittedFunctions().size() > 1) {
676 register_execution_unit = true;
Sean Callanan00294b32016-03-22 21:05:51 +0000677 }
678
Raphael Isemannefe5fd12019-08-06 08:07:45 +0000679 if (register_execution_unit)
680 exe_ctx.GetTargetPtr()
681 ->GetPersistentExpressionStateForLanguage(m_language)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000682 ->RegisterExecutionUnit(m_execution_unit_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000683 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000684
Kate Stoneb9c1b512016-09-06 20:57:50 +0000685 if (generate_debug_info) {
686 lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000687
Kate Stoneb9c1b512016-09-06 20:57:50 +0000688 if (jit_module_sp) {
689 ConstString const_func_name(FunctionName());
690 FileSpec jit_file;
691 jit_file.GetFilename() = const_func_name;
692 jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());
693 m_jit_module_wp = jit_module_sp;
694 target->GetImages().Append(jit_module_sp);
Sean Callanan00294b32016-03-22 21:05:51 +0000695 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696 }
Sean Callanan00294b32016-03-22 21:05:51 +0000697
Kate Stoneb9c1b512016-09-06 20:57:50 +0000698 if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
699 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
700 return true;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000701}
702
Raphael Isemann74829732018-08-30 17:29:37 +0000703/// Converts an absolute position inside a given code string into
704/// a column/line pair.
705///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000706/// \param[in] abs_pos
Raphael Isemann74829732018-08-30 17:29:37 +0000707/// A absolute position in the code string that we want to convert
708/// to a column/line pair.
709///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000710/// \param[in] code
Raphael Isemann74829732018-08-30 17:29:37 +0000711/// A multi-line string usually representing source code.
712///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000713/// \param[out] line
Raphael Isemann74829732018-08-30 17:29:37 +0000714/// The line in the code that contains the given absolute position.
715/// The first line in the string is indexed as 1.
716///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000717/// \param[out] column
Raphael Isemann74829732018-08-30 17:29:37 +0000718/// The column in the line that contains the absolute position.
719/// The first character in a line is indexed as 0.
Raphael Isemannbfc5ef62018-09-22 13:33:08 +0000720static void AbsPosToLineColumnPos(size_t abs_pos, llvm::StringRef code,
Raphael Isemann74829732018-08-30 17:29:37 +0000721 unsigned &line, unsigned &column) {
722 // Reset to code position to beginning of the file.
723 line = 0;
724 column = 0;
725
726 assert(abs_pos <= code.size() && "Absolute position outside code string?");
727
728 // We have to walk up to the position and count lines/columns.
729 for (std::size_t i = 0; i < abs_pos; ++i) {
730 // If we hit a line break, we go back to column 0 and enter a new line.
731 // We only handle \n because that's what we internally use to make new
732 // lines for our temporary code strings.
733 if (code[i] == '\n') {
734 ++line;
735 column = 0;
736 continue;
737 }
738 ++column;
739 }
740}
741
742bool ClangUserExpression::Complete(ExecutionContext &exe_ctx,
Raphael Isemannc11a7802018-08-30 21:26:32 +0000743 CompletionRequest &request,
744 unsigned complete_pos) {
Raphael Isemann74829732018-08-30 17:29:37 +0000745 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
746
747 // We don't want any visible feedback when completing an expression. Mostly
748 // because the results we get from an incomplete invocation are probably not
749 // correct.
750 DiagnosticManager diagnostic_manager;
751
Raphael Isemann71569d02019-05-02 10:12:56 +0000752 if (!PrepareForParsing(diagnostic_manager, exe_ctx, /*for_completion*/ true))
Raphael Isemann74829732018-08-30 17:29:37 +0000753 return false;
754
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000755 LLDB_LOGF(log, "Parsing the following code:\n%s", m_transformed_text.c_str());
Raphael Isemann74829732018-08-30 17:29:37 +0000756
757 //////////////////////////
758 // Parse the expression
759 //
760
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000761 m_materializer_up.reset(new Materializer());
Raphael Isemann74829732018-08-30 17:29:37 +0000762
763 ResetDeclMap(exe_ctx, m_result_delegate, /*keep result in memory*/ true);
764
Tatyana Krasnukhaedebf122019-04-26 16:41:04 +0000765 auto on_exit = llvm::make_scope_exit([this]() { ResetDeclMap(); });
Raphael Isemann74829732018-08-30 17:29:37 +0000766
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000767 if (!DeclMap()->WillParse(exe_ctx, m_materializer_up.get())) {
Raphael Isemann74829732018-08-30 17:29:37 +0000768 diagnostic_manager.PutString(
769 eDiagnosticSeverityError,
770 "current process state is unsuitable for expression parsing");
771
772 return false;
773 }
774
775 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
776 DeclMap()->SetLookupsEnabled(true);
777 }
778
779 Process *process = exe_ctx.GetProcessPtr();
780 ExecutionContextScope *exe_scope = process;
781
782 if (!exe_scope)
783 exe_scope = exe_ctx.GetTargetPtr();
784
785 ClangExpressionParser parser(exe_scope, *this, false);
786
787 // We have to find the source code location where the user text is inside
788 // the transformed expression code. When creating the transformed text, we
789 // already stored the absolute position in the m_transformed_text string. The
790 // only thing left to do is to transform it into the line:column format that
791 // Clang expects.
792
793 // The line and column of the user expression inside the transformed source
794 // code.
795 unsigned user_expr_line, user_expr_column;
796 if (m_user_expression_start_pos.hasValue())
797 AbsPosToLineColumnPos(*m_user_expression_start_pos, m_transformed_text,
798 user_expr_line, user_expr_column);
799 else
800 return false;
801
802 // The actual column where we have to complete is the start column of the
803 // user expression + the offset inside the user code that we were given.
804 const unsigned completion_column = user_expr_column + complete_pos;
Raphael Isemannc11a7802018-08-30 21:26:32 +0000805 parser.Complete(request, user_expr_line, completion_column, complete_pos);
Raphael Isemann74829732018-08-30 17:29:37 +0000806
807 return true;
808}
809
Kate Stoneb9c1b512016-09-06 20:57:50 +0000810bool ClangUserExpression::AddArguments(ExecutionContext &exe_ctx,
811 std::vector<lldb::addr_t> &args,
812 lldb::addr_t struct_address,
813 DiagnosticManager &diagnostic_manager) {
814 lldb::addr_t object_ptr = LLDB_INVALID_ADDRESS;
815 lldb::addr_t cmd_ptr = LLDB_INVALID_ADDRESS;
Sean Callanan579e70c2016-03-19 00:03:59 +0000816
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817 if (m_needs_object_ptr) {
818 lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
819 if (!frame_sp)
820 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000821
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822 ConstString object_name;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000823
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824 if (m_in_cplusplus_method) {
825 object_name.SetCString("this");
826 } else if (m_in_objectivec_method) {
827 object_name.SetCString("self");
828 } else {
Zachary Turnere2411fa2016-11-12 19:12:56 +0000829 diagnostic_manager.PutString(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000830 eDiagnosticSeverityError,
831 "need object pointer but don't know the language");
832 return false;
Jim Ingham51148e92015-11-05 00:24:18 +0000833 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000834
Zachary Turner97206d52017-05-12 04:51:55 +0000835 Status object_ptr_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000836
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000837 if (m_ctx_obj) {
838 AddressType address_type;
839 object_ptr = m_ctx_obj->GetAddressOf(false, &address_type);
840 if (object_ptr == LLDB_INVALID_ADDRESS ||
841 address_type != eAddressTypeLoad)
842 object_ptr_error.SetErrorString("Can't get context object's "
843 "debuggee address");
844 } else
845 object_ptr = GetObjectPointer(frame_sp, object_name, object_ptr_error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000846
847 if (!object_ptr_error.Success()) {
848 exe_ctx.GetTargetRef().GetDebugger().GetAsyncOutputStream()->Printf(
Jason Molenda599558e2017-02-21 05:09:26 +0000849 "warning: `%s' is not accessible (substituting 0)\n",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000850 object_name.AsCString());
851 object_ptr = 0;
Jim Ingham36f3b362010-10-14 23:45:03 +0000852 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853
854 if (m_in_objectivec_method) {
855 ConstString cmd_name("_cmd");
856
857 cmd_ptr = GetObjectPointer(frame_sp, cmd_name, object_ptr_error);
858
859 if (!object_ptr_error.Success()) {
860 diagnostic_manager.Printf(
861 eDiagnosticSeverityWarning,
862 "couldn't get cmd pointer (substituting NULL): %s",
863 object_ptr_error.AsCString());
864 cmd_ptr = 0;
865 }
866 }
867
868 args.push_back(object_ptr);
869
870 if (m_in_objectivec_method)
871 args.push_back(cmd_ptr);
872
873 args.push_back(struct_address);
874 } else {
875 args.push_back(struct_address);
876 }
877 return true;
Jim Ingham36f3b362010-10-14 23:45:03 +0000878}
879
Kate Stoneb9c1b512016-09-06 20:57:50 +0000880lldb::ExpressionVariableSP ClangUserExpression::GetResultAfterDematerialization(
881 ExecutionContextScope *exe_scope) {
882 return m_result_delegate.GetVariable();
Sean Callanan9fda9d22015-10-03 09:09:01 +0000883}
884
Kate Stoneb9c1b512016-09-06 20:57:50 +0000885void ClangUserExpression::ClangUserExpressionHelper::ResetDeclMap(
886 ExecutionContext &exe_ctx,
887 Materializer::PersistentVariableDelegate &delegate,
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000888 bool keep_result_in_memory,
889 ValueObject *ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000890 m_expr_decl_map_up.reset(
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000891 new ClangExpressionDeclMap(keep_result_in_memory, &delegate, exe_ctx,
892 ctx_obj));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000893}
Jim Ingham36f3b362010-10-14 23:45:03 +0000894
Jim Ingham151c0322015-09-15 21:13:50 +0000895clang::ASTConsumer *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896ClangUserExpression::ClangUserExpressionHelper::ASTTransformer(
897 clang::ASTConsumer *passthrough) {
898 m_result_synthesizer_up.reset(
899 new ASTResultSynthesizer(passthrough, m_top_level, m_target));
Sean Callananc673a6e2010-12-07 10:00:20 +0000900
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901 return m_result_synthesizer_up.get();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000902}
903
Kate Stoneb9c1b512016-09-06 20:57:50 +0000904void ClangUserExpression::ClangUserExpressionHelper::CommitPersistentDecls() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000905 if (m_result_synthesizer_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000906 m_result_synthesizer_up->CommitPersistentDecls();
907 }
Sean Callanan00294b32016-03-22 21:05:51 +0000908}
909
Kate Stoneb9c1b512016-09-06 20:57:50 +0000910ConstString ClangUserExpression::ResultDelegate::GetName() {
Adrian Prantl03219f72018-04-30 23:59:17 +0000911 auto prefix = m_persistent_state->GetPersistentVariablePrefix();
912 return m_persistent_state->GetNextPersistentVariableName(*m_target_sp,
913 prefix);
Sean Callanan9fda9d22015-10-03 09:09:01 +0000914}
915
Kate Stoneb9c1b512016-09-06 20:57:50 +0000916void ClangUserExpression::ResultDelegate::DidDematerialize(
917 lldb::ExpressionVariableSP &variable) {
918 m_variable = variable;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000919}
920
Kate Stoneb9c1b512016-09-06 20:57:50 +0000921void ClangUserExpression::ResultDelegate::RegisterPersistentState(
922 PersistentExpressionState *persistent_state) {
923 m_persistent_state = persistent_state;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000924}
925
Kate Stoneb9c1b512016-09-06 20:57:50 +0000926lldb::ExpressionVariableSP &ClangUserExpression::ResultDelegate::GetVariable() {
927 return m_variable;
Sean Callanan9fda9d22015-10-03 09:09:01 +0000928}