blob: 6a980db293b18925af651b541e31bb2021018e39 [file] [log] [blame]
Sean Callanan65dafa82010-08-27 01:01:44 +00001//===-- ClangUserExpression.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// C Includes
11#include <stdio.h>
12#if HAVE_SYS_TYPES_H
13# include <sys/types.h>
14#endif
15
16// C++ Includes
17#include <cstdlib>
18#include <string>
19#include <map>
20
21#include "lldb/Core/ConstString.h"
22#include "lldb/Core/Log.h"
23#include "lldb/Core/StreamString.h"
Greg Claytond1719722010-10-05 03:13:51 +000024#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000025#include "lldb/Expression/ClangExpressionDeclMap.h"
26#include "lldb/Expression/ClangExpressionParser.h"
27#include "lldb/Expression/ClangFunction.h"
28#include "lldb/Expression/ASTResultSynthesizer.h"
29#include "lldb/Expression/ClangUserExpression.h"
30#include "lldb/Host/Host.h"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000031#include "lldb/Symbol/VariableList.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000032#include "lldb/Target/ExecutionContext.h"
Greg Clayton0baa3942010-11-04 01:54:29 +000033#include "lldb/Target/Process.h"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000034#include "lldb/Target/StackFrame.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000035#include "lldb/Target/Target.h"
Jim Ingham360f53f2010-11-30 02:22:11 +000036#include "lldb/Target/ThreadPlan.h"
37#include "lldb/Target/ThreadPlanCallUserExpression.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000038
39using namespace lldb_private;
40
Sean Callanan77e93942010-10-29 00:29:03 +000041ClangUserExpression::ClangUserExpression (const char *expr,
42 const char *expr_prefix) :
Sean Callanan65dafa82010-08-27 01:01:44 +000043 m_expr_text(expr),
Johnny Chenb4c0f022010-10-29 20:19:44 +000044 m_expr_prefix(expr_prefix ? expr_prefix : ""),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000045 m_transformed_text(),
46 m_jit_addr(LLDB_INVALID_ADDRESS),
47 m_cplusplus(false),
48 m_objectivec(false),
Sean Callanana91dd992010-11-19 02:52:21 +000049 m_needs_object_ptr(false),
Sean Callanane8e55572010-12-01 21:35:54 +000050 m_const_object(false),
Sean Callanana91dd992010-11-19 02:52:21 +000051 m_desired_type(NULL, NULL)
Sean Callanan65dafa82010-08-27 01:01:44 +000052{
Sean Callanan65dafa82010-08-27 01:01:44 +000053}
54
Sean Callanan830a9032010-08-27 23:31:21 +000055ClangUserExpression::~ClangUserExpression ()
56{
57}
58
Sean Callanan65dafa82010-08-27 01:01:44 +000059clang::ASTConsumer *
60ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
61{
Sean Callanana91dd992010-11-19 02:52:21 +000062 return new ASTResultSynthesizer(passthrough,
63 m_desired_type);
Sean Callanan65dafa82010-08-27 01:01:44 +000064}
65
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000066void
67ClangUserExpression::ScanContext(ExecutionContext &exe_ctx)
68{
69 if (!exe_ctx.frame)
70 return;
71
72 VariableList *vars = exe_ctx.frame->GetVariableList(false);
73
74 if (!vars)
75 return;
76
Sean Callanane8e55572010-12-01 21:35:54 +000077 lldb::VariableSP this_var(vars->FindVariable(ConstString("this")));
78 lldb::VariableSP self_var(vars->FindVariable(ConstString("self")));
79
80 if (this_var.get())
81 {
82 Type *this_type = this_var->GetType();
83
84 lldb::clang_type_t pointer_target_type;
85
86 if (ClangASTContext::IsPointerType(this_type->GetClangType(),
87 &pointer_target_type))
88 {
89 TypeFromUser target_ast_type(pointer_target_type, this_type->GetClangAST());
90
91 if (target_ast_type.IsDefined())
92 m_cplusplus = true;
93
94 if (target_ast_type.IsConst())
95 m_const_object = true;
96 }
97 }
98 else if (self_var.get())
99 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000100 m_objectivec = true;
Sean Callanane8e55572010-12-01 21:35:54 +0000101 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000102}
103
Sean Callanan550f2762010-10-22 23:25:16 +0000104// This is a really nasty hack, meant to fix Objective-C expressions of the form
105// (int)[myArray count]. Right now, because the type information for count is
106// not available, [myArray count] returns id, which can't be directly cast to
107// int without causing a clang error.
108static void
109ApplyObjcCastHack(std::string &expr)
110{
111#define OBJC_CAST_HACK_FROM "(int)["
112#define OBJC_CAST_HACK_TO "(int)(long long)["
113
114 size_t from_offset;
115
116 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
117 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
118
119#undef OBJC_CAST_HACK_TO
120#undef OBJC_CAST_HACK_FROM
121}
122
Sean Callanan30892372010-10-24 20:45:49 +0000123// Another hack, meant to allow use of unichar despite it not being available in
124// the type information. Although we could special-case it in type lookup,
125// hopefully we'll figure out a way to #include the same environment as is
126// present in the original source file rather than try to hack specific type
127// definitions in as needed.
128static void
129ApplyUnicharHack(std::string &expr)
130{
131#define UNICHAR_HACK_FROM "unichar"
132#define UNICHAR_HACK_TO "unsigned short"
133
134 size_t from_offset;
135
136 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
137 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
138
139#undef UNICHAR_HACK_TO
140#undef UNICHAR_HACK_FROM
141}
142
Sean Callanan550f2762010-10-22 23:25:16 +0000143bool
Sean Callanana91dd992010-11-19 02:52:21 +0000144ClangUserExpression::Parse (Stream &error_stream,
145 ExecutionContext &exe_ctx,
146 TypeFromUser desired_type)
Sean Callanan65dafa82010-08-27 01:01:44 +0000147{
Greg Claytone005f2c2010-11-06 01:53:30 +0000148 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000149
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000150 ScanContext(exe_ctx);
151
152 StreamString m_transformed_stream;
153
154 ////////////////////////////////////
155 // Generate the expression
156 //
Sean Callanan550f2762010-10-22 23:25:16 +0000157
158 ApplyObjcCastHack(m_expr_text);
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000159 //ApplyUnicharHack(m_expr_text);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000160
161 if (m_cplusplus)
162 {
Sean Callanan77e93942010-10-29 00:29:03 +0000163 m_transformed_stream.Printf("%s \n"
164 "typedef unsigned short unichar; \n"
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000165 "void \n"
Sean Callanane8e55572010-12-01 21:35:54 +0000166 "$__lldb_class::%s(void *$__lldb_arg) %s\n"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000167 "{ \n"
168 " %s; \n"
169 "} \n",
Sean Callanan77e93942010-10-29 00:29:03 +0000170 m_expr_prefix.c_str(),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000171 FunctionName(),
Sean Callanane8e55572010-12-01 21:35:54 +0000172 (m_const_object ? "const" : ""),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000173 m_expr_text.c_str());
174
175 m_needs_object_ptr = true;
176 }
177 else
178 {
Sean Callanan77e93942010-10-29 00:29:03 +0000179 m_transformed_stream.Printf("%s \n"
180 "typedef unsigned short unichar;\n"
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000181 "void \n"
Sean Callanan550f2762010-10-22 23:25:16 +0000182 "%s(void *$__lldb_arg) \n"
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000183 "{ \n"
184 " %s; \n"
185 "} \n",
Sean Callanan77e93942010-10-29 00:29:03 +0000186 m_expr_prefix.c_str(),
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000187 FunctionName(),
188 m_expr_text.c_str());
189 }
190
191 m_transformed_text = m_transformed_stream.GetData();
192
193
194 if (log)
195 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
196
Sean Callanan65dafa82010-08-27 01:01:44 +0000197 ////////////////////////////////////
198 // Set up the target and compiler
199 //
200
201 Target *target = exe_ctx.target;
202
203 if (!target)
204 {
205 error_stream.PutCString ("error: invalid target\n");
206 return false;
207 }
208
209 ConstString target_triple;
210
211 target->GetTargetTriple (target_triple);
212
213 if (!target_triple)
214 target_triple = Host::GetTargetTriple ();
215
216 if (!target_triple)
217 {
218 error_stream.PutCString ("error: invalid target triple\n");
219 return false;
220 }
221
222 //////////////////////////
223 // Parse the expression
224 //
225
Sean Callanana91dd992010-11-19 02:52:21 +0000226 m_desired_type = desired_type;
227
Sean Callananaa301c42010-12-03 01:38:59 +0000228 m_expr_decl_map.reset(new ClangExpressionDeclMap());
229
230 m_expr_decl_map->WillParse(exe_ctx);
Sean Callanan65dafa82010-08-27 01:01:44 +0000231
232 ClangExpressionParser parser(target_triple.GetCString(), *this);
233
234 unsigned num_errors = parser.Parse (error_stream);
235
236 if (num_errors)
237 {
238 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callananaa301c42010-12-03 01:38:59 +0000239
240 m_expr_decl_map->DidParse();
241
Sean Callanan65dafa82010-08-27 01:01:44 +0000242 return false;
243 }
244
245 ///////////////////////////////////////////////
246 // Convert the output of the parser to DWARF
247 //
248
249 m_dwarf_opcodes.reset(new StreamString);
250 m_dwarf_opcodes->SetByteOrder (lldb::eByteOrderHost);
251 m_dwarf_opcodes->GetFlags ().Set (Stream::eBinary);
252
253 m_local_variables.reset(new ClangExpressionVariableStore());
254
255 Error dwarf_error = parser.MakeDWARF ();
256
257 if (dwarf_error.Success())
258 {
259 if (log)
260 log->Printf("Code can be interpreted.");
261
Sean Callananaa301c42010-12-03 01:38:59 +0000262 m_expr_decl_map->DidParse();
263
Sean Callanan65dafa82010-08-27 01:01:44 +0000264 return true;
265 }
266
267 //////////////////////////////////
268 // JIT the output of the parser
269 //
270
271 m_dwarf_opcodes.reset();
272
Sean Callanan830a9032010-08-27 23:31:21 +0000273 lldb::addr_t jit_end;
274
275 Error jit_error = parser.MakeJIT (m_jit_addr, jit_end, exe_ctx);
Sean Callanan65dafa82010-08-27 01:01:44 +0000276
Sean Callananaa301c42010-12-03 01:38:59 +0000277 m_expr_decl_map->DidParse();
278
Sean Callanan65dafa82010-08-27 01:01:44 +0000279 if (jit_error.Success())
280 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000281 return true;
282 }
283 else
284 {
285 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
286 return false;
287 }
288}
289
290bool
Jim Inghamd1686902010-10-14 23:45:03 +0000291ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
Sean Callananab06af92010-10-19 23:57:21 +0000292 ExecutionContext &exe_ctx,
293 lldb::addr_t &struct_address,
294 lldb::addr_t &object_ptr)
Sean Callanan65dafa82010-08-27 01:01:44 +0000295{
Greg Claytone005f2c2010-11-06 01:53:30 +0000296 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan65dafa82010-08-27 01:01:44 +0000297
Jim Inghamd1686902010-10-14 23:45:03 +0000298 if (m_jit_addr != LLDB_INVALID_ADDRESS)
Sean Callanan65dafa82010-08-27 01:01:44 +0000299 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000300
301 Error materialize_error;
302
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000303
Sean Callananaa301c42010-12-03 01:38:59 +0000304 if (m_needs_object_ptr && !(m_expr_decl_map->GetObjectPointer(object_ptr, exe_ctx, materialize_error)))
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000305 {
306 error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
307 return false;
308 }
309
Sean Callananaa301c42010-12-03 01:38:59 +0000310 if (!m_expr_decl_map->Materialize(exe_ctx, struct_address, materialize_error))
Sean Callanan65dafa82010-08-27 01:01:44 +0000311 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000312 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
Sean Callanan65dafa82010-08-27 01:01:44 +0000313 return false;
314 }
315
316 if (log)
317 {
318 log->Printf("Function address : 0x%llx", (uint64_t)m_jit_addr);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000319
320 if (m_needs_object_ptr)
321 log->Printf("Object pointer : 0x%llx", (uint64_t)object_ptr);
322
Sean Callanan65dafa82010-08-27 01:01:44 +0000323 log->Printf("Structure address : 0x%llx", (uint64_t)struct_address);
324
325 StreamString args;
326
327 Error dump_error;
328
Sean Callanane8a59a82010-09-13 21:34:21 +0000329 if (struct_address)
Sean Callanan65dafa82010-08-27 01:01:44 +0000330 {
Sean Callananaa301c42010-12-03 01:38:59 +0000331 if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
Sean Callanane8a59a82010-09-13 21:34:21 +0000332 {
333 log->Printf("Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
334 }
335 else
336 {
337 log->Printf("Structure contents:\n%s", args.GetData());
338 }
Sean Callanan65dafa82010-08-27 01:01:44 +0000339 }
340 }
Jim Inghamd1686902010-10-14 23:45:03 +0000341 }
342 return true;
343}
344
345ThreadPlan *
346ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
347 ExecutionContext &exe_ctx)
348{
349 lldb::addr_t struct_address;
350
351 lldb::addr_t object_ptr = NULL;
352
353 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
354
Jim Ingham360f53f2010-11-30 02:22:11 +0000355 // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
356 // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are
357 // forcing unwind_on_error to be true here, in practical terms that can't happen.
Jim Inghamd1686902010-10-14 23:45:03 +0000358 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
Sean Callanana65b5272010-12-01 01:28:23 +0000359 m_jit_addr,
360 struct_address,
361 error_stream,
362 true,
363 true,
364 (m_needs_object_ptr ? &object_ptr : NULL));
Jim Inghamd1686902010-10-14 23:45:03 +0000365}
366
367bool
368ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
369 ExecutionContext &exe_ctx,
370 ClangExpressionVariable *&result)
371{
372 Error expr_error;
373
Sean Callananaa301c42010-12-03 01:38:59 +0000374 if (!m_expr_decl_map->Dematerialize(exe_ctx, result, expr_error))
Jim Inghamd1686902010-10-14 23:45:03 +0000375 {
376 error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
377 return false;
378 }
379 return true;
380}
381
Jim Ingham360f53f2010-11-30 02:22:11 +0000382Process::ExecutionResults
Jim Inghamd1686902010-10-14 23:45:03 +0000383ClangUserExpression::Execute (Stream &error_stream,
384 ExecutionContext &exe_ctx,
Jim Inghamea9d4262010-11-05 19:25:48 +0000385 bool discard_on_error,
Jim Ingham360f53f2010-11-30 02:22:11 +0000386 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
Jim Inghamd1686902010-10-14 23:45:03 +0000387 ClangExpressionVariable *&result)
388{
389 if (m_dwarf_opcodes.get())
390 {
391 // TODO execute the JITted opcodes
392
393 error_stream.Printf("We don't currently support executing DWARF expressions");
394
Jim Ingham360f53f2010-11-30 02:22:11 +0000395 return Process::eExecutionSetupError;
Jim Inghamd1686902010-10-14 23:45:03 +0000396 }
397 else if (m_jit_addr != LLDB_INVALID_ADDRESS)
398 {
399 lldb::addr_t struct_address;
400
401 lldb::addr_t object_ptr = NULL;
402
403 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr);
Sean Callanan65dafa82010-08-27 01:01:44 +0000404
Jim Inghamea9d4262010-11-05 19:25:48 +0000405 const bool stop_others = true;
406 const bool try_all_threads = true;
Sean Callanan65dafa82010-08-27 01:01:44 +0000407
Jim Ingham360f53f2010-11-30 02:22:11 +0000408 Address wrapper_address (NULL, m_jit_addr);
409 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (*(exe_ctx.thread), wrapper_address, struct_address,
410 stop_others, discard_on_error,
411 (m_needs_object_ptr ? &object_ptr : NULL),
412 shared_ptr_to_me));
413 if (call_plan_sp == NULL || !call_plan_sp->ValidatePlan (NULL))
414 return Process::eExecutionSetupError;
415
416 call_plan_sp->SetPrivate(true);
417
418 uint32_t single_thread_timeout_usec = 10000000;
419 Process::ExecutionResults execution_result =
420 exe_ctx.process->RunThreadPlan (exe_ctx, call_plan_sp, stop_others, try_all_threads, discard_on_error,
421 single_thread_timeout_usec, error_stream);
422
423 if (execution_result == Process::eExecutionInterrupted)
Sean Callanan65dafa82010-08-27 01:01:44 +0000424 {
Jim Ingham360f53f2010-11-30 02:22:11 +0000425 if (discard_on_error)
426 error_stream.Printf ("Expression execution was interrupted. The process has been returned to the state before execution.");
427 else
428 error_stream.Printf ("Expression execution was interrupted. The process has been left at the point where it was interrupted.");
429
430 return execution_result;
431 }
432 else if (execution_result != Process::eExecutionCompleted)
433 {
434 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
435 return execution_result;
Sean Callanan65dafa82010-08-27 01:01:44 +0000436 }
437
Jim Ingham360f53f2010-11-30 02:22:11 +0000438 if (FinalizeJITExecution (error_stream, exe_ctx, result))
439 return Process::eExecutionCompleted;
440 else
441 return Process::eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000442 }
443 else
444 {
Johnny Chencb395442010-11-10 19:02:11 +0000445 error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function is present");
Jim Ingham360f53f2010-11-30 02:22:11 +0000446 return Process::eExecutionSetupError;
Sean Callanan65dafa82010-08-27 01:01:44 +0000447 }
448}
449
450StreamString &
451ClangUserExpression::DwarfOpcodeStream ()
452{
453 if (!m_dwarf_opcodes.get())
454 m_dwarf_opcodes.reset(new StreamString());
455
456 return *m_dwarf_opcodes.get();
457}
Greg Clayton377e0b42010-10-05 00:31:29 +0000458
Jim Ingham360f53f2010-11-30 02:22:11 +0000459Process::ExecutionResults
Sean Callanan77e93942010-10-29 00:29:03 +0000460ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
Jim Inghamea9d4262010-11-05 19:25:48 +0000461 bool discard_on_error,
Sean Callanan77e93942010-10-29 00:29:03 +0000462 const char *expr_cstr,
Jim Ingham360f53f2010-11-30 02:22:11 +0000463 const char *expr_prefix,
464 lldb::ValueObjectSP &result_valobj_sp)
Greg Clayton377e0b42010-10-05 00:31:29 +0000465{
466 Error error;
Jim Ingham360f53f2010-11-30 02:22:11 +0000467 Process::ExecutionResults execution_results = Process::eExecutionSetupError;
Greg Clayton0baa3942010-11-04 01:54:29 +0000468
469 if (exe_ctx.process == NULL)
Jim Ingham360f53f2010-11-30 02:22:11 +0000470 {
471 error.SetErrorString ("Must have a process to evaluate expressions.");
472
473 result_valobj_sp.reset (new ValueObjectConstResult (error));
474 return Process::eExecutionSetupError;
475 }
476
Greg Clayton0baa3942010-11-04 01:54:29 +0000477 if (!exe_ctx.process->GetDynamicCheckers())
478 {
479 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
480
481 StreamString install_errors;
482
483 if (!dynamic_checkers->Install(install_errors, exe_ctx))
Sean Callananf7731452010-11-05 00:57:06 +0000484 {
485 if (install_errors.GetString().empty())
486 error.SetErrorString ("couldn't install checkers, unknown error");
487 else
488 error.SetErrorString (install_errors.GetString().c_str());
489
490 result_valobj_sp.reset (new ValueObjectConstResult (error));
Jim Ingham360f53f2010-11-30 02:22:11 +0000491 return Process::eExecutionSetupError;
Sean Callananf7731452010-11-05 00:57:06 +0000492 }
493
Greg Clayton0baa3942010-11-04 01:54:29 +0000494 exe_ctx.process->SetDynamicCheckers(dynamic_checkers);
495 }
496
Jim Ingham360f53f2010-11-30 02:22:11 +0000497 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix));
498
Greg Clayton377e0b42010-10-05 00:31:29 +0000499 StreamString error_stream;
500
Jim Ingham360f53f2010-11-30 02:22:11 +0000501 if (!user_expression_sp->Parse (error_stream, exe_ctx, TypeFromUser(NULL, NULL)))
Greg Clayton377e0b42010-10-05 00:31:29 +0000502 {
503 if (error_stream.GetString().empty())
504 error.SetErrorString ("expression failed to parse, unknown error");
505 else
506 error.SetErrorString (error_stream.GetString().c_str());
507 }
508 else
509 {
510 ClangExpressionVariable *expr_result = NULL;
511
512 error_stream.GetString().clear();
513
Jim Ingham360f53f2010-11-30 02:22:11 +0000514 execution_results = user_expression_sp->Execute (error_stream,
515 exe_ctx,
516 discard_on_error,
517 user_expression_sp,
518 expr_result);
519 if (execution_results != Process::eExecutionCompleted)
Greg Clayton377e0b42010-10-05 00:31:29 +0000520 {
521 if (error_stream.GetString().empty())
522 error.SetErrorString ("expression failed to execute, unknown error");
523 else
524 error.SetErrorString (error_stream.GetString().c_str());
525 }
526 else
527 {
528 // TODO: seems weird to get a pointer to a result object back from
529 // a function. Do we own it? Feels like we do, but from looking at the
530 // code we don't. Might be best to make this a reference and state
531 // explicitly that we don't own it when we get a reference back from
532 // the execute?
533 if (expr_result)
534 {
535 result_valobj_sp = expr_result->GetExpressionResult (&exe_ctx);
536 }
537 else
538 {
Sean Callanan44820ec2010-10-19 20:15:00 +0000539 error.SetErrorString ("Expression did not return a result");
Greg Clayton377e0b42010-10-05 00:31:29 +0000540 }
541 }
542 }
Sean Callanan44820ec2010-10-19 20:15:00 +0000543
Greg Claytond1719722010-10-05 03:13:51 +0000544 if (result_valobj_sp.get() == NULL)
545 result_valobj_sp.reset (new ValueObjectConstResult (error));
546
Jim Ingham360f53f2010-11-30 02:22:11 +0000547 return execution_results;
Johnny Chenb4c0f022010-10-29 20:19:44 +0000548}