ClangUserExpression.h revision 377e0b4b4677128244b151396f77b3421bf2b9fa
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
54    ~ClangUserExpression ();
55
56    //------------------------------------------------------------------
57    /// Parse the expression
58    ///
59    /// @param[in] error_stream
60    ///     A stream to print parse errors and warnings to.
61    ///
62    /// @param[in] exe_ctx
63    ///     The execution context to use when looking up entities that
64    ///     are needed for parsing (locations of functions, types of
65    ///     variables, persistent variables, etc.)
66    ///
67    /// @return
68    ///     True on success (no errors); false otherwise.
69    //------------------------------------------------------------------
70    bool
71    Parse (Stream &error_stream, ExecutionContext &exe_ctx);
72
73    //------------------------------------------------------------------
74    /// Execute the parsed expression
75    ///
76    /// @param[in] error_stream
77    ///     A stream to print errors to.
78    ///
79    /// @param[in] exe_ctx
80    ///     The execution context to use when looking up entities that
81    ///     are needed for parsing (locations of variables, etc.)
82    ///
83    /// @param[in] result
84    ///     A pointer to direct at the persistent variable in which the
85    ///     expression's result is stored.
86    ///
87    /// @return
88    ///     True on success; false otherwise.
89    //------------------------------------------------------------------
90    bool
91    Execute (Stream &error_stream,
92             ExecutionContext &exe_ctx,
93             ClangExpressionVariable *& result);
94
95    //------------------------------------------------------------------
96    /// Return the string that the parser should parse.  Must be a full
97    /// translation unit.
98    //------------------------------------------------------------------
99    const char *
100    Text ()
101    {
102        return m_transformed_text.c_str();
103    }
104
105    //------------------------------------------------------------------
106    /// Return the function name that should be used for executing the
107    /// expression.  Text() should contain the definition of this
108    /// function.
109    //------------------------------------------------------------------
110    const char *
111    FunctionName ()
112    {
113        return "___clang_expr";
114    }
115
116    //------------------------------------------------------------------
117    /// Return the object that the parser should use when resolving external
118    /// values.  May be NULL if everything should be self-contained.
119    //------------------------------------------------------------------
120    ClangExpressionDeclMap *
121    DeclMap ()
122    {
123        return m_expr_decl_map.get();
124    }
125
126    //------------------------------------------------------------------
127    /// Return the object that the parser should use when registering
128    /// local variables.  May be NULL if the Expression doesn't care.
129    //------------------------------------------------------------------
130    ClangExpressionVariableStore *
131    LocalVariables ()
132    {
133        return m_local_variables.get();
134    }
135
136    //------------------------------------------------------------------
137    /// Return the object that the parser should allow to access ASTs.
138    /// May be NULL if the ASTs do not need to be transformed.
139    ///
140    /// @param[in] passthrough
141    ///     The ASTConsumer that the returned transformer should send
142    ///     the ASTs to after transformation.
143    //------------------------------------------------------------------
144    clang::ASTConsumer *
145    ASTTransformer (clang::ASTConsumer *passthrough);
146
147    //------------------------------------------------------------------
148    /// Return the stream that the parser should use to write DWARF
149    /// opcodes.
150    //------------------------------------------------------------------
151    StreamString &
152    DwarfOpcodeStream ();
153
154    //------------------------------------------------------------------
155    /// Return true if validation code should be inserted into the
156    /// expression.
157    //------------------------------------------------------------------
158    bool
159    NeedsValidation ()
160    {
161        return true;
162    }
163
164    //------------------------------------------------------------------
165    /// Return true if external variables in the expression should be
166    /// resolved.
167    //------------------------------------------------------------------
168    bool
169    NeedsVariableResolution ()
170    {
171        return true;
172    }
173
174
175    static Error
176    Evaluate (ExecutionContext &exe_ctx,
177              const char *expr_cstr,
178              lldb::ValueObjectSP &result_valobj_sp);
179
180private:
181    //------------------------------------------------------------------
182    /// Populate m_cplusplus and m_objetivec based on the environment.
183    //------------------------------------------------------------------
184    void
185    ScanContext(ExecutionContext &exe_ctx);
186
187    std::string                                 m_expr_text;            ///< The text of the expression, as typed by the user
188    std::string                                 m_transformed_text;     ///< The text of the expression, as send to the parser
189
190    std::auto_ptr<ClangExpressionDeclMap>       m_expr_decl_map;        ///< The map to use when parsing and materializing the expression.
191    std::auto_ptr<ClangExpressionVariableStore> m_local_variables;      ///< The local expression variables, if the expression is DWARF.
192    std::auto_ptr<StreamString>                 m_dwarf_opcodes;        ///< The DWARF opcodes for the expression.  May be NULL.
193    lldb::addr_t                                m_jit_addr;             ///< The address of the JITted code.  LLDB_INVALID_ADDRESS if invalid.
194
195    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).
196    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).
197    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.
198};
199
200} // namespace lldb_private
201
202#endif  // liblldb_ClangUserExpression_h_
203