blob: 310cac9d5716678f7533d8fc2036cea436997bbd [file] [log] [blame]
Sean Callanan830a9032010-08-27 23:31:21 +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
18#include "lldb/Core/ConstString.h"
19#include "lldb/Core/Stream.h"
Sean Callanane8a59a82010-09-13 21:34:21 +000020#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callanan830a9032010-08-27 23:31:21 +000021#include "lldb/Expression/ClangExpressionParser.h"
22#include "lldb/Expression/ClangUtilityFunction.h"
23#include "lldb/Host/Host.h"
24#include "lldb/Target/ExecutionContext.h"
25#include "lldb/Target/Target.h"
26
27using namespace lldb_private;
28
29//------------------------------------------------------------------
30/// Constructor
31///
32/// @param[in] text
33/// The text of the function. Must be a full translation unit.
34///
35/// @param[in] name
36/// The name of the function, as used in the text.
37//------------------------------------------------------------------
38ClangUtilityFunction::ClangUtilityFunction (const char *text,
39 const char *name) :
40 m_function_text(text),
Sean Callananf18d91c2010-09-01 00:58:00 +000041 m_function_name(name),
42 m_jit_begin(LLDB_INVALID_ADDRESS),
43 m_jit_end(LLDB_INVALID_ADDRESS)
Sean Callanan830a9032010-08-27 23:31:21 +000044{
45}
46
47//------------------------------------------------------------------
48/// Install the utility function into a process
49///
50/// @param[in] error_stream
51/// A stream to print parse errors and warnings to.
52///
53/// @param[in] exe_ctx
54/// The execution context to install the utility function to.
55///
56/// @return
57/// True on success (no errors); false otherwise.
58//------------------------------------------------------------------
59bool
60ClangUtilityFunction::Install (Stream &error_stream,
61 ExecutionContext &exe_ctx)
Sean Callananf18d91c2010-09-01 00:58:00 +000062{
63 if (m_jit_begin != LLDB_INVALID_ADDRESS)
64 {
65 error_stream.PutCString("error: already installed\n");
66 return false;
67 }
68
Sean Callanan830a9032010-08-27 23:31:21 +000069 ////////////////////////////////////
70 // Set up the target and compiler
71 //
72
73 Target *target = exe_ctx.target;
74
75 if (!target)
76 {
77 error_stream.PutCString ("error: invalid target\n");
78 return false;
79 }
80
81 ConstString target_triple;
82
83 target->GetTargetTriple (target_triple);
84
85 if (!target_triple)
86 target_triple = Host::GetTargetTriple ();
87
88 if (!target_triple)
89 {
90 error_stream.PutCString ("error: invalid target triple\n");
91 return false;
92 }
93
94 //////////////////////////
95 // Parse the expression
96 //
Sean Callanane8a59a82010-09-13 21:34:21 +000097
98 m_expr_decl_map.reset(new ClangExpressionDeclMap(&exe_ctx));
Sean Callanan830a9032010-08-27 23:31:21 +000099
100 ClangExpressionParser parser(target_triple.GetCString(), *this);
101
102 unsigned num_errors = parser.Parse (error_stream);
103
104 if (num_errors)
105 {
106 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanane8a59a82010-09-13 21:34:21 +0000107
108 m_expr_decl_map.reset();
109
Sean Callanan830a9032010-08-27 23:31:21 +0000110 return false;
111 }
112
113 //////////////////////////////////
114 // JIT the output of the parser
115 //
116
117 Error jit_error = parser.MakeJIT (m_jit_begin, m_jit_end, exe_ctx);
118
Sean Callanane8a59a82010-09-13 21:34:21 +0000119 m_expr_decl_map.reset();
120
Sean Callanan830a9032010-08-27 23:31:21 +0000121 if (jit_error.Success())
122 {
123 return true;
124 }
125 else
126 {
127 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
128 return false;
129 }
130}
131
132