blob: 3acd3f283fa61a8d6a9b41bf8adfb56da3cc5b9c [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"
20#include "lldb/Expression/ClangExpressionParser.h"
21#include "lldb/Expression/ClangUtilityFunction.h"
22#include "lldb/Host/Host.h"
23#include "lldb/Target/ExecutionContext.h"
24#include "lldb/Target/Target.h"
25
26using namespace lldb_private;
27
28//------------------------------------------------------------------
29/// Constructor
30///
31/// @param[in] text
32/// The text of the function. Must be a full translation unit.
33///
34/// @param[in] name
35/// The name of the function, as used in the text.
36//------------------------------------------------------------------
37ClangUtilityFunction::ClangUtilityFunction (const char *text,
38 const char *name) :
39 m_function_text(text),
40 m_function_name(name)
41{
42}
43
44//------------------------------------------------------------------
45/// Install the utility function into a process
46///
47/// @param[in] error_stream
48/// A stream to print parse errors and warnings to.
49///
50/// @param[in] exe_ctx
51/// The execution context to install the utility function to.
52///
53/// @return
54/// True on success (no errors); false otherwise.
55//------------------------------------------------------------------
56bool
57ClangUtilityFunction::Install (Stream &error_stream,
58 ExecutionContext &exe_ctx)
59{
60 ////////////////////////////////////
61 // Set up the target and compiler
62 //
63
64 Target *target = exe_ctx.target;
65
66 if (!target)
67 {
68 error_stream.PutCString ("error: invalid target\n");
69 return false;
70 }
71
72 ConstString target_triple;
73
74 target->GetTargetTriple (target_triple);
75
76 if (!target_triple)
77 target_triple = Host::GetTargetTriple ();
78
79 if (!target_triple)
80 {
81 error_stream.PutCString ("error: invalid target triple\n");
82 return false;
83 }
84
85 //////////////////////////
86 // Parse the expression
87 //
88
89 ClangExpressionParser parser(target_triple.GetCString(), *this);
90
91 unsigned num_errors = parser.Parse (error_stream);
92
93 if (num_errors)
94 {
95 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
96 return false;
97 }
98
99 //////////////////////////////////
100 // JIT the output of the parser
101 //
102
103 Error jit_error = parser.MakeJIT (m_jit_begin, m_jit_end, exe_ctx);
104
105 if (jit_error.Success())
106 {
107 return true;
108 }
109 else
110 {
111 error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors);
112 return false;
113 }
114}
115
116