ClangUserExpression.h revision 3c9c5eb466869ede185e879d14a47335fb43194d
1//===-- ClangUserExpression.h -----------------------------------*- 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#ifndef liblldb_ClangUserExpression_h_
11#define liblldb_ClangUserExpression_h_
12
13// C Includes
14// C++ Includes
15#include <string>
16#include <map>
17#include <memory>
18#include <vector>
19
20// Other libraries and framework includes
21// Project includes
22
23#include "lldb/lldb-forward.h"
24#include "lldb/lldb-private.h"
25#include "lldb/Core/ClangForward.h"
26#include "lldb/Expression/ClangExpression.h"
27
28#include "llvm/ExecutionEngine/JITMemoryManager.h"
29
30namespace lldb_private
31{
32
33//----------------------------------------------------------------------
34/// @class ClangUserExpression ClangUserExpression.h "lldb/Expression/ClangUserExpression.h"
35/// @brief Encapsulates a single expression for use with Clang
36///
37/// LLDB uses expressions for various purposes, notably to call functions
38/// and as a backend for the expr command.  ClangUserExpression encapsulates
39/// the objects needed to parse and interpret or JIT an expression.  It
40/// uses the Clang parser to produce LLVM IR from the expression.
41//----------------------------------------------------------------------
42class ClangUserExpression : public ClangExpression
43{
44public:
45    //------------------------------------------------------------------
46    /// Constructor
47    //------------------------------------------------------------------
48    ClangUserExpression (const char *expr);
49
50    //------------------------------------------------------------------
51    /// Destructor
52    //------------------------------------------------------------------
53    virtual ~ClangUserExpression ();
54
55    //------------------------------------------------------------------
56    /// Parse the expression
57    ///
58    /// @param[in] error_stream
59    ///     A stream to print parse errors and warnings to.
60    ///
61    /// @param[in] exe_ctx
62    ///     The execution context to use when looking up entities that
63    ///     are needed for parsing (locations of functions, types of
64    ///     variables, persistent variables, etc.)
65    ///
66    /// @return
67    ///     True on success (no errors); false otherwise.
68    //------------------------------------------------------------------
69    bool
70    Parse (Stream &error_stream, ExecutionContext &exe_ctx);
71
72    //------------------------------------------------------------------
73    /// Execute the parsed expression
74    ///
75    /// @param[in] error_stream
76    ///     A stream to print errors to.
77    ///
78    /// @param[in] exe_ctx
79    ///     The execution context to use when looking up entities that
80    ///     are needed for parsing (locations of variables, etc.)
81    ///
82    /// @param[in] result
83    ///     A pointer to direct at the persistent variable in which the
84    ///     expression's result is stored.
85    ///
86    /// @return
87    ///     True on success; false otherwise.
88    //------------------------------------------------------------------
89    bool
90    Execute (Stream &error_stream,
91             ExecutionContext &exe_ctx,
92             ClangExpressionVariable *& result);
93
94    //------------------------------------------------------------------
95    /// Return the string that the parser should parse.  Must be a full
96    /// translation unit.
97    //------------------------------------------------------------------
98    const char *
99    Text ()
100    {
101        return m_transformed_text.c_str();
102    }
103
104    //------------------------------------------------------------------
105    /// Return the function name that should be used for executing the
106    /// expression.  Text() should contain the definition of this
107    /// function.
108    //------------------------------------------------------------------
109    const char *
110    FunctionName ()
111    {
112        return "___clang_expr";
113    }
114
115    //------------------------------------------------------------------
116    /// Return the object that the parser should use when resolving external
117    /// values.  May be NULL if everything should be self-contained.
118    //------------------------------------------------------------------
119    ClangExpressionDeclMap *
120    DeclMap ()
121    {
122        return m_expr_decl_map.get();
123    }
124
125    //------------------------------------------------------------------
126    /// Return the object that the parser should use when registering
127    /// local variables.  May be NULL if the Expression doesn't care.
128    //------------------------------------------------------------------
129    ClangExpressionVariableStore *
130    LocalVariables ()
131    {
132        return m_local_variables.get();
133    }
134
135    //------------------------------------------------------------------
136    /// Return the object that the parser should allow to access ASTs.
137    /// May be NULL if the ASTs do not need to be transformed.
138    ///
139    /// @param[in] passthrough
140    ///     The ASTConsumer that the returned transformer should send
141    ///     the ASTs to after transformation.
142    //------------------------------------------------------------------
143    clang::ASTConsumer *
144    ASTTransformer (clang::ASTConsumer *passthrough);
145
146    //------------------------------------------------------------------
147    /// Return the stream that the parser should use to write DWARF
148    /// opcodes.
149    //------------------------------------------------------------------
150    StreamString &
151    DwarfOpcodeStream ();
152
153    //------------------------------------------------------------------
154    /// Return true if validation code should be inserted into the
155    /// expression.
156    //------------------------------------------------------------------
157    bool
158    NeedsValidation ()
159    {
160        return true;
161    }
162
163    //------------------------------------------------------------------
164    /// Return true if external variables in the expression should be
165    /// resolved.
166    //------------------------------------------------------------------
167    bool
168    NeedsVariableResolution ()
169    {
170        return true;
171    }
172
173private:
174    //------------------------------------------------------------------
175    /// Populate m_cplusplus and m_objetivec based on the environment.
176    //------------------------------------------------------------------
177    void
178    ScanContext(ExecutionContext &exe_ctx);
179
180    std::string                                 m_expr_text;            ///< The text of the expression, as typed by the user
181    std::string                                 m_transformed_text;     ///< The text of the expression, as send to the parser
182
183    std::auto_ptr<ClangExpressionDeclMap>       m_expr_decl_map;        ///< The map to use when parsing and materializing the expression.
184    std::auto_ptr<ClangExpressionVariableStore> m_local_variables;      ///< The local expression variables, if the expression is DWARF.
185    std::auto_ptr<StreamString>                 m_dwarf_opcodes;        ///< The DWARF opcodes for the expression.  May be NULL.
186    lldb::addr_t                                m_jit_addr;             ///< The address of the JITted code.  LLDB_INVALID_ADDRESS if invalid.
187
188    bool                                        m_cplusplus;            ///< True if the expression is compiled as a C++ member function (true if it was parsed when exe_ctx was in a C++ method).
189    bool                                        m_objectivec;           ///< True if the expression is compiled as an Objective-C method (true if it was parsed when exe_ctx was in an Objective-C method).
190    bool                                        m_needs_object_ptr;     ///< True if "this" or "self" must be looked up and passed in.  False if the expression doesn't really use them and they can be NULL.
191};
192
193} // namespace lldb_private
194
195#endif  // liblldb_ClangUserExpression_h_
196