blob: 0080347543b2875b4140e08c1c35c589132e69eb [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
Greg Clayton6a5aa8a2010-09-24 23:07:41 +000047ClangUtilityFunction::~ClangUtilityFunction ()
48{
49}
50
Sean Callanan830a9032010-08-27 23:31:21 +000051//------------------------------------------------------------------
52/// Install the utility function into a process
53///
54/// @param[in] error_stream
55/// A stream to print parse errors and warnings to.
56///
57/// @param[in] exe_ctx
58/// The execution context to install the utility function to.
59///
60/// @return
61/// True on success (no errors); false otherwise.
62//------------------------------------------------------------------
63bool
64ClangUtilityFunction::Install (Stream &error_stream,
65 ExecutionContext &exe_ctx)
Sean Callananf18d91c2010-09-01 00:58:00 +000066{
67 if (m_jit_begin != LLDB_INVALID_ADDRESS)
68 {
69 error_stream.PutCString("error: already installed\n");
70 return false;
71 }
72
Sean Callanan830a9032010-08-27 23:31:21 +000073 ////////////////////////////////////
74 // Set up the target and compiler
75 //
76
77 Target *target = exe_ctx.target;
78
79 if (!target)
80 {
81 error_stream.PutCString ("error: invalid target\n");
82 return false;
83 }
84
85 ConstString target_triple;
86
87 target->GetTargetTriple (target_triple);
88
89 if (!target_triple)
90 target_triple = Host::GetTargetTriple ();
91
92 if (!target_triple)
93 {
94 error_stream.PutCString ("error: invalid target triple\n");
95 return false;
96 }
97
98 //////////////////////////
99 // Parse the expression
100 //
Sean Callanane8a59a82010-09-13 21:34:21 +0000101
102 m_expr_decl_map.reset(new ClangExpressionDeclMap(&exe_ctx));
Sean Callanan830a9032010-08-27 23:31:21 +0000103
104 ClangExpressionParser parser(target_triple.GetCString(), *this);
105
106 unsigned num_errors = parser.Parse (error_stream);
107
108 if (num_errors)
109 {
110 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanane8a59a82010-09-13 21:34:21 +0000111
112 m_expr_decl_map.reset();
113
Sean Callanan830a9032010-08-27 23:31:21 +0000114 return false;
115 }
116
117 //////////////////////////////////
118 // JIT the output of the parser
119 //
120
121 Error jit_error = parser.MakeJIT (m_jit_begin, m_jit_end, exe_ctx);
122
Sean Callanane8a59a82010-09-13 21:34:21 +0000123 m_expr_decl_map.reset();
124
Sean Callanan830a9032010-08-27 23:31:21 +0000125 if (jit_error.Success())
126 {
127 return true;
128 }
129 else
130 {
131 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
132 return false;
133 }
134}
135
136