blob: 0c9d90a2b57948f97d34fd43bbfaa2d5f2c4fe25 [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
Sean Callananaa301c42010-12-03 01:38:59 +0000102 m_expr_decl_map.reset(new ClangExpressionDeclMap());
103
104 m_expr_decl_map->WillParse(exe_ctx);
Sean Callanan830a9032010-08-27 23:31:21 +0000105
106 ClangExpressionParser parser(target_triple.GetCString(), *this);
107
108 unsigned num_errors = parser.Parse (error_stream);
109
110 if (num_errors)
111 {
112 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
Sean Callanane8a59a82010-09-13 21:34:21 +0000113
114 m_expr_decl_map.reset();
115
Sean Callanan830a9032010-08-27 23:31:21 +0000116 return false;
117 }
118
119 //////////////////////////////////
120 // JIT the output of the parser
121 //
122
123 Error jit_error = parser.MakeJIT (m_jit_begin, m_jit_end, exe_ctx);
124
Sean Callananaa301c42010-12-03 01:38:59 +0000125 m_expr_decl_map->DidParse();
126
Sean Callanane8a59a82010-09-13 21:34:21 +0000127 m_expr_decl_map.reset();
128
Sean Callanan830a9032010-08-27 23:31:21 +0000129 if (jit_error.Success())
130 {
131 return true;
132 }
133 else
134 {
135 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
136 return false;
137 }
138}
139
140