1254721Semaste//===-- ClangUserExpression.h -----------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#ifndef liblldb_ClangUserExpression_h_
11254721Semaste#define liblldb_ClangUserExpression_h_
12254721Semaste
13254721Semaste// C Includes
14254721Semaste// C++ Includes
15254721Semaste#include <string>
16254721Semaste#include <map>
17254721Semaste#include <vector>
18254721Semaste
19254721Semaste// Other libraries and framework includes
20254721Semaste// Project includes
21254721Semaste
22254721Semaste#include "lldb/lldb-forward.h"
23254721Semaste#include "lldb/lldb-private.h"
24254721Semaste#include "lldb/Core/Address.h"
25254721Semaste#include "lldb/Core/ClangForward.h"
26254721Semaste#include "lldb/Expression/ClangExpression.h"
27254721Semaste#include "lldb/Expression/ClangExpressionVariable.h"
28254721Semaste#include "lldb/Expression/IRForTarget.h"
29254721Semaste#include "lldb/Expression/Materializer.h"
30254721Semaste#include "lldb/Symbol/TaggedASTType.h"
31254721Semaste#include "lldb/Target/ExecutionContext.h"
32254721Semaste
33254721Semaste#include "llvm/ExecutionEngine/JITMemoryManager.h"
34254721Semaste
35254721Semastenamespace lldb_private
36254721Semaste{
37254721Semaste
38254721Semaste//----------------------------------------------------------------------
39254721Semaste/// @class ClangUserExpression ClangUserExpression.h "lldb/Expression/ClangUserExpression.h"
40254721Semaste/// @brief Encapsulates a single expression for use with Clang
41254721Semaste///
42254721Semaste/// LLDB uses expressions for various purposes, notably to call functions
43254721Semaste/// and as a backend for the expr command.  ClangUserExpression encapsulates
44254721Semaste/// the objects needed to parse and interpret or JIT an expression.  It
45254721Semaste/// uses the Clang parser to produce LLVM IR from the expression.
46254721Semaste//----------------------------------------------------------------------
47254721Semasteclass ClangUserExpression : public ClangExpression
48254721Semaste{
49254721Semastepublic:
50254721Semaste    typedef std::shared_ptr<ClangUserExpression> ClangUserExpressionSP;
51254721Semaste
52254721Semaste    enum { kDefaultTimeout = 500000u };
53254721Semaste    //------------------------------------------------------------------
54254721Semaste    /// Constructor
55254721Semaste    ///
56254721Semaste    /// @param[in] expr
57254721Semaste    ///     The expression to parse.
58254721Semaste    ///
59254721Semaste    /// @param[in] expr_prefix
60254721Semaste    ///     If non-NULL, a C string containing translation-unit level
61254721Semaste    ///     definitions to be included when the expression is parsed.
62254721Semaste    ///
63254721Semaste    /// @param[in] language
64254721Semaste    ///     If not eLanguageTypeUnknown, a language to use when parsing
65254721Semaste    ///     the expression.  Currently restricted to those languages
66254721Semaste    ///     supported by Clang.
67254721Semaste    ///
68254721Semaste    /// @param[in] desired_type
69254721Semaste    ///     If not eResultTypeAny, the type to use for the expression
70254721Semaste    ///     result.
71254721Semaste    //------------------------------------------------------------------
72254721Semaste    ClangUserExpression (const char *expr,
73254721Semaste                         const char *expr_prefix,
74254721Semaste                         lldb::LanguageType language,
75254721Semaste                         ResultType desired_type);
76254721Semaste
77254721Semaste    //------------------------------------------------------------------
78254721Semaste    /// Destructor
79254721Semaste    //------------------------------------------------------------------
80254721Semaste    virtual
81254721Semaste    ~ClangUserExpression ();
82254721Semaste
83254721Semaste    //------------------------------------------------------------------
84254721Semaste    /// Parse the expression
85254721Semaste    ///
86254721Semaste    /// @param[in] error_stream
87254721Semaste    ///     A stream to print parse errors and warnings to.
88254721Semaste    ///
89254721Semaste    /// @param[in] exe_ctx
90254721Semaste    ///     The execution context to use when looking up entities that
91254721Semaste    ///     are needed for parsing (locations of functions, types of
92254721Semaste    ///     variables, persistent variables, etc.)
93254721Semaste    ///
94254721Semaste    /// @param[in] execution_policy
95254721Semaste    ///     Determines whether interpretation is possible or mandatory.
96254721Semaste    ///
97254721Semaste    /// @param[in] keep_result_in_memory
98254721Semaste    ///     True if the resulting persistent variable should reside in
99254721Semaste    ///     target memory, if applicable.
100254721Semaste    ///
101254721Semaste    /// @return
102254721Semaste    ///     True on success (no errors); false otherwise.
103254721Semaste    //------------------------------------------------------------------
104254721Semaste    bool
105254721Semaste    Parse (Stream &error_stream,
106254721Semaste           ExecutionContext &exe_ctx,
107254721Semaste           lldb_private::ExecutionPolicy execution_policy,
108254721Semaste           bool keep_result_in_memory);
109254721Semaste
110254721Semaste    bool
111254721Semaste    CanInterpret ()
112254721Semaste    {
113254721Semaste        return m_can_interpret;
114254721Semaste    }
115254721Semaste
116254721Semaste    bool
117254721Semaste    MatchesContext (ExecutionContext &exe_ctx);
118254721Semaste
119254721Semaste    //------------------------------------------------------------------
120254721Semaste    /// Execute the parsed expression
121254721Semaste    ///
122254721Semaste    /// @param[in] error_stream
123254721Semaste    ///     A stream to print errors to.
124254721Semaste    ///
125254721Semaste    /// @param[in] exe_ctx
126254721Semaste    ///     The execution context to use when looking up entities that
127254721Semaste    ///     are needed for parsing (locations of variables, etc.)
128254721Semaste    ///
129263363Semaste    /// @param[in] options
130263363Semaste    ///     Expression evaluation options.
131254721Semaste    ///
132254721Semaste    /// @param[in] shared_ptr_to_me
133254721Semaste    ///     This is a shared pointer to this ClangUserExpression.  This is
134254721Semaste    ///     needed because Execute can push a thread plan that will hold onto
135254721Semaste    ///     the ClangUserExpression for an unbounded period of time.  So you
136254721Semaste    ///     need to give the thread plan a reference to this object that can
137254721Semaste    ///     keep it alive.
138254721Semaste    ///
139254721Semaste    /// @param[in] result
140254721Semaste    ///     A pointer to direct at the persistent variable in which the
141254721Semaste    ///     expression's result is stored.
142254721Semaste    ///
143254721Semaste    /// @return
144254721Semaste    ///     A Process::Execution results value.
145254721Semaste    //------------------------------------------------------------------
146254721Semaste    ExecutionResults
147254721Semaste    Execute (Stream &error_stream,
148254721Semaste             ExecutionContext &exe_ctx,
149263363Semaste             const EvaluateExpressionOptions& options,
150254721Semaste             ClangUserExpressionSP &shared_ptr_to_me,
151263363Semaste             lldb::ClangExpressionVariableSP &result);
152254721Semaste
153254721Semaste    //------------------------------------------------------------------
154254721Semaste    /// Apply the side effects of the function to program state.
155254721Semaste    ///
156254721Semaste    /// @param[in] error_stream
157254721Semaste    ///     A stream to print errors to.
158254721Semaste    ///
159254721Semaste    /// @param[in] exe_ctx
160254721Semaste    ///     The execution context to use when looking up entities that
161254721Semaste    ///     are needed for parsing (locations of variables, etc.)
162254721Semaste    ///
163254721Semaste    /// @param[in] result
164254721Semaste    ///     A pointer to direct at the persistent variable in which the
165254721Semaste    ///     expression's result is stored.
166254721Semaste    ///
167254721Semaste    /// @param[in] function_stack_pointer
168254721Semaste    ///     A pointer to the base of the function's stack frame.  This
169254721Semaste    ///     is used to determine whether the expession result resides in
170254721Semaste    ///     memory that will still be valid, or whether it needs to be
171254721Semaste    ///     treated as homeless for the purpose of future expressions.
172254721Semaste    ///
173254721Semaste    /// @return
174254721Semaste    ///     A Process::Execution results value.
175254721Semaste    //------------------------------------------------------------------
176254721Semaste    bool
177254721Semaste    FinalizeJITExecution (Stream &error_stream,
178254721Semaste                          ExecutionContext &exe_ctx,
179254721Semaste                          lldb::ClangExpressionVariableSP &result,
180254721Semaste                          lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS,
181254721Semaste                          lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS);
182254721Semaste
183254721Semaste    //------------------------------------------------------------------
184254721Semaste    /// Return the string that the parser should parse.  Must be a full
185254721Semaste    /// translation unit.
186254721Semaste    //------------------------------------------------------------------
187254721Semaste    const char *
188254721Semaste    Text ()
189254721Semaste    {
190254721Semaste        return m_transformed_text.c_str();
191254721Semaste    }
192254721Semaste
193254721Semaste    //------------------------------------------------------------------
194254721Semaste    /// Return the string that the user typed.
195254721Semaste    //------------------------------------------------------------------
196254721Semaste    const char *
197254721Semaste    GetUserText ()
198254721Semaste    {
199254721Semaste        return m_expr_text.c_str();
200254721Semaste    }
201254721Semaste
202254721Semaste    //------------------------------------------------------------------
203254721Semaste    /// Return the function name that should be used for executing the
204254721Semaste    /// expression.  Text() should contain the definition of this
205254721Semaste    /// function.
206254721Semaste    //------------------------------------------------------------------
207254721Semaste    const char *
208254721Semaste    FunctionName ()
209254721Semaste    {
210254721Semaste        return "$__lldb_expr";
211254721Semaste    }
212254721Semaste
213254721Semaste    //------------------------------------------------------------------
214254721Semaste    /// Return the language that should be used when parsing.  To use
215254721Semaste    /// the default, return eLanguageTypeUnknown.
216254721Semaste    //------------------------------------------------------------------
217254721Semaste    virtual lldb::LanguageType
218254721Semaste    Language ()
219254721Semaste    {
220254721Semaste        return m_language;
221254721Semaste    }
222254721Semaste
223254721Semaste    //------------------------------------------------------------------
224254721Semaste    /// Return the object that the parser should use when resolving external
225254721Semaste    /// values.  May be NULL if everything should be self-contained.
226254721Semaste    //------------------------------------------------------------------
227254721Semaste    ClangExpressionDeclMap *
228254721Semaste    DeclMap ()
229254721Semaste    {
230254721Semaste        return m_expr_decl_map.get();
231254721Semaste    }
232254721Semaste
233254721Semaste    //------------------------------------------------------------------
234254721Semaste    /// Return the object that the parser should allow to access ASTs.
235254721Semaste    /// May be NULL if the ASTs do not need to be transformed.
236254721Semaste    ///
237254721Semaste    /// @param[in] passthrough
238254721Semaste    ///     The ASTConsumer that the returned transformer should send
239254721Semaste    ///     the ASTs to after transformation.
240254721Semaste    //------------------------------------------------------------------
241254721Semaste    clang::ASTConsumer *
242254721Semaste    ASTTransformer (clang::ASTConsumer *passthrough);
243254721Semaste
244254721Semaste    //------------------------------------------------------------------
245254721Semaste    /// Return the desired result type of the function, or
246254721Semaste    /// eResultTypeAny if indifferent.
247254721Semaste    //------------------------------------------------------------------
248254721Semaste    virtual ResultType
249254721Semaste    DesiredResultType ()
250254721Semaste    {
251254721Semaste        return m_desired_type;
252254721Semaste    }
253254721Semaste
254254721Semaste    //------------------------------------------------------------------
255254721Semaste    /// Return true if validation code should be inserted into the
256254721Semaste    /// expression.
257254721Semaste    //------------------------------------------------------------------
258254721Semaste    bool
259254721Semaste    NeedsValidation ()
260254721Semaste    {
261254721Semaste        return true;
262254721Semaste    }
263254721Semaste
264254721Semaste    //------------------------------------------------------------------
265254721Semaste    /// Return true if external variables in the expression should be
266254721Semaste    /// resolved.
267254721Semaste    //------------------------------------------------------------------
268254721Semaste    bool
269254721Semaste    NeedsVariableResolution ()
270254721Semaste    {
271254721Semaste        return true;
272254721Semaste    }
273254721Semaste
274254721Semaste    //------------------------------------------------------------------
275254721Semaste    /// Evaluate one expression and return its result.
276254721Semaste    ///
277254721Semaste    /// @param[in] exe_ctx
278254721Semaste    ///     The execution context to use when evaluating the expression.
279254721Semaste    ///
280263363Semaste    /// @param[in] options
281263363Semaste    ///     Expression evaluation options.
282254721Semaste    ///
283254721Semaste    /// @param[in] expr_cstr
284254721Semaste    ///     A C string containing the expression to be evaluated.
285254721Semaste    ///
286254721Semaste    /// @param[in] expr_prefix
287254721Semaste    ///     If non-NULL, a C string containing translation-unit level
288254721Semaste    ///     definitions to be included when the expression is parsed.
289254721Semaste    ///
290254721Semaste    /// @param[in/out] result_valobj_sp
291254721Semaste    ///      If execution is successful, the result valobj is placed here.
292254721Semaste    ///
293263363Semaste    /// @param[out]
294263363Semaste    ///     Filled in with an error in case the expression evaluation
295263363Semaste    ///     fails to parse, run, or evaluated.
296254721Semaste    ///
297254721Semaste    /// @result
298254721Semaste    ///      A Process::ExecutionResults value.  eExecutionCompleted for success.
299254721Semaste    //------------------------------------------------------------------
300254721Semaste    static ExecutionResults
301254721Semaste    Evaluate (ExecutionContext &exe_ctx,
302263363Semaste              const EvaluateExpressionOptions& options,
303254721Semaste              const char *expr_cstr,
304254721Semaste              const char *expr_prefix,
305254721Semaste              lldb::ValueObjectSP &result_valobj_sp,
306263363Semaste              Error &error);
307254721Semaste
308254721Semaste    static const Error::ValueType kNoResult = 0x1001; ///< ValueObject::GetError() returns this if there is no result from the expression.
309254721Semasteprivate:
310254721Semaste    //------------------------------------------------------------------
311254721Semaste    /// Populate m_cplusplus and m_objetivec based on the environment.
312254721Semaste    //------------------------------------------------------------------
313254721Semaste
314254721Semaste    void
315254721Semaste    ScanContext (ExecutionContext &exe_ctx,
316254721Semaste                 lldb_private::Error &err);
317254721Semaste
318254721Semaste    bool
319254721Semaste    PrepareToExecuteJITExpression (Stream &error_stream,
320254721Semaste                                   ExecutionContext &exe_ctx,
321254721Semaste                                   lldb::addr_t &struct_address,
322254721Semaste                                   lldb::addr_t &object_ptr,
323254721Semaste                                   lldb::addr_t &cmd_ptr);
324254721Semaste
325254721Semaste    void
326254721Semaste    InstallContext (ExecutionContext &exe_ctx);
327254721Semaste
328254721Semaste    bool
329254721Semaste    LockAndCheckContext (ExecutionContext &exe_ctx,
330254721Semaste                         lldb::TargetSP &target_sp,
331254721Semaste                         lldb::ProcessSP &process_sp,
332254721Semaste                         lldb::StackFrameSP &frame_sp);
333254721Semaste
334254721Semaste    lldb::ProcessWP                             m_process_wp;           ///< The process used as the context for the expression.
335254721Semaste    Address                                     m_address;              ///< The address the process is stopped in.
336254721Semaste    lldb::addr_t                                m_stack_frame_bottom;   ///< The bottom of the allocated stack frame.
337254721Semaste    lldb::addr_t                                m_stack_frame_top;      ///< The top of the allocated stack frame.
338254721Semaste
339254721Semaste    std::string                                 m_expr_text;            ///< The text of the expression, as typed by the user
340254721Semaste    std::string                                 m_expr_prefix;          ///< The text of the translation-level definitions, as provided by the user
341254721Semaste    lldb::LanguageType                          m_language;             ///< The language to use when parsing (eLanguageTypeUnknown means use defaults)
342254721Semaste    bool                                        m_allow_cxx;            ///< True if the language allows C++.
343254721Semaste    bool                                        m_allow_objc;           ///< True if the language allows Objective-C.
344254721Semaste    std::string                                 m_transformed_text;     ///< The text of the expression, as send to the parser
345254721Semaste    ResultType                                  m_desired_type;         ///< The type to coerce the expression's result to.  If eResultTypeAny, inferred from the expression.
346254721Semaste
347254721Semaste    std::unique_ptr<ClangExpressionDeclMap>      m_expr_decl_map;        ///< The map to use when parsing the expression.
348254721Semaste    std::unique_ptr<IRExecutionUnit>             m_execution_unit_ap;    ///< The execution unit the expression is stored in.
349254721Semaste    std::unique_ptr<Materializer>                m_materializer_ap;      ///< The materializer to use when running the expression.
350254721Semaste    std::unique_ptr<ASTResultSynthesizer>        m_result_synthesizer;   ///< The result synthesizer, if one is needed.
351254721Semaste
352254721Semaste    bool                                        m_enforce_valid_object; ///< True if the expression parser should enforce the presence of a valid class pointer in order to generate the expression as a method.
353254721Semaste    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).
354254721Semaste    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).
355254721Semaste    bool                                        m_static_method;        ///< True if the expression is compiled as a static (or class) method (currently true if it was parsed when exe_ctx was in an Objective-C class method).
356254721Semaste    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.
357254721Semaste    bool                                        m_const_object;         ///< True if "this" is const.
358254721Semaste    Target                                     *m_target;               ///< The target for storing persistent data like types and variables.
359254721Semaste
360254721Semaste    bool                                        m_can_interpret;        ///< True if the expression could be evaluated statically; false otherwise.
361254721Semaste    lldb::addr_t                                m_materialized_address; ///< The address at which the arguments to the expression have been materialized.
362254721Semaste    Materializer::DematerializerSP              m_dematerializer_sp;    ///< The dematerializer.
363254721Semaste};
364254721Semaste
365254721Semaste} // namespace lldb_private
366254721Semaste
367254721Semaste#endif  // liblldb_ClangUserExpression_h_
368