1 //===-- ClangUserExpression.h -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_ClangUserExpression_h_
11 #define liblldb_ClangUserExpression_h_
19 // Other libraries and framework includes
22 #include "lldb/lldb-forward.h"
23 #include "lldb/lldb-private.h"
24 #include "lldb/Core/Address.h"
25 #include "lldb/Core/ClangForward.h"
26 #include "lldb/Expression/ClangExpression.h"
27 #include "lldb/Expression/ClangExpressionVariable.h"
28 #include "lldb/Expression/IRForTarget.h"
29 #include "lldb/Expression/Materializer.h"
30 #include "lldb/Symbol/TaggedASTType.h"
31 #include "lldb/Target/ExecutionContext.h"
33 #include "llvm/ExecutionEngine/JITMemoryManager.h"
35 namespace lldb_private
38 //----------------------------------------------------------------------
39 /// @class ClangUserExpression ClangUserExpression.h "lldb/Expression/ClangUserExpression.h"
40 /// @brief Encapsulates a single expression for use with Clang
42 /// LLDB uses expressions for various purposes, notably to call functions
43 /// and as a backend for the expr command. ClangUserExpression encapsulates
44 /// the objects needed to parse and interpret or JIT an expression. It
45 /// uses the Clang parser to produce LLVM IR from the expression.
46 //----------------------------------------------------------------------
47 class ClangUserExpression : public ClangExpression
50 typedef std::shared_ptr<ClangUserExpression> ClangUserExpressionSP;
52 enum { kDefaultTimeout = 500000u };
53 //------------------------------------------------------------------
57 /// The expression to parse.
59 /// @param[in] expr_prefix
60 /// If non-NULL, a C string containing translation-unit level
61 /// definitions to be included when the expression is parsed.
63 /// @param[in] language
64 /// If not eLanguageTypeUnknown, a language to use when parsing
65 /// the expression. Currently restricted to those languages
66 /// supported by Clang.
68 /// @param[in] desired_type
69 /// If not eResultTypeAny, the type to use for the expression
71 //------------------------------------------------------------------
72 ClangUserExpression (const char *expr,
73 const char *expr_prefix,
74 lldb::LanguageType language,
75 ResultType desired_type);
77 //------------------------------------------------------------------
79 //------------------------------------------------------------------
81 ~ClangUserExpression ();
83 //------------------------------------------------------------------
84 /// Parse the expression
86 /// @param[in] error_stream
87 /// A stream to print parse errors and warnings to.
89 /// @param[in] exe_ctx
90 /// The execution context to use when looking up entities that
91 /// are needed for parsing (locations of functions, types of
92 /// variables, persistent variables, etc.)
94 /// @param[in] execution_policy
95 /// Determines whether interpretation is possible or mandatory.
97 /// @param[in] keep_result_in_memory
98 /// True if the resulting persistent variable should reside in
99 /// target memory, if applicable.
102 /// True on success (no errors); false otherwise.
103 //------------------------------------------------------------------
105 Parse (Stream &error_stream,
106 ExecutionContext &exe_ctx,
107 lldb_private::ExecutionPolicy execution_policy,
108 bool keep_result_in_memory);
113 return m_can_interpret;
117 MatchesContext (ExecutionContext &exe_ctx);
119 //------------------------------------------------------------------
120 /// Execute the parsed expression
122 /// @param[in] error_stream
123 /// A stream to print errors to.
125 /// @param[in] exe_ctx
126 /// The execution context to use when looking up entities that
127 /// are needed for parsing (locations of variables, etc.)
129 /// @param[in] options
130 /// Expression evaluation options.
132 /// @param[in] shared_ptr_to_me
133 /// This is a shared pointer to this ClangUserExpression. This is
134 /// needed because Execute can push a thread plan that will hold onto
135 /// the ClangUserExpression for an unbounded period of time. So you
136 /// need to give the thread plan a reference to this object that can
139 /// @param[in] result
140 /// A pointer to direct at the persistent variable in which the
141 /// expression's result is stored.
144 /// A Process::Execution results value.
145 //------------------------------------------------------------------
147 Execute (Stream &error_stream,
148 ExecutionContext &exe_ctx,
149 const EvaluateExpressionOptions& options,
150 ClangUserExpressionSP &shared_ptr_to_me,
151 lldb::ClangExpressionVariableSP &result);
153 //------------------------------------------------------------------
154 /// Apply the side effects of the function to program state.
156 /// @param[in] error_stream
157 /// A stream to print errors to.
159 /// @param[in] exe_ctx
160 /// The execution context to use when looking up entities that
161 /// are needed for parsing (locations of variables, etc.)
163 /// @param[in] result
164 /// A pointer to direct at the persistent variable in which the
165 /// expression's result is stored.
167 /// @param[in] function_stack_pointer
168 /// A pointer to the base of the function's stack frame. This
169 /// is used to determine whether the expession result resides in
170 /// memory that will still be valid, or whether it needs to be
171 /// treated as homeless for the purpose of future expressions.
174 /// A Process::Execution results value.
175 //------------------------------------------------------------------
177 FinalizeJITExecution (Stream &error_stream,
178 ExecutionContext &exe_ctx,
179 lldb::ClangExpressionVariableSP &result,
180 lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS,
181 lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS);
183 //------------------------------------------------------------------
184 /// Return the string that the parser should parse. Must be a full
185 /// translation unit.
186 //------------------------------------------------------------------
190 return m_transformed_text.c_str();
193 //------------------------------------------------------------------
194 /// Return the string that the user typed.
195 //------------------------------------------------------------------
199 return m_expr_text.c_str();
202 //------------------------------------------------------------------
203 /// Return the function name that should be used for executing the
204 /// expression. Text() should contain the definition of this
206 //------------------------------------------------------------------
210 return "$__lldb_expr";
213 //------------------------------------------------------------------
214 /// Return the language that should be used when parsing. To use
215 /// the default, return eLanguageTypeUnknown.
216 //------------------------------------------------------------------
217 virtual lldb::LanguageType
223 //------------------------------------------------------------------
224 /// Return the object that the parser should use when resolving external
225 /// values. May be NULL if everything should be self-contained.
226 //------------------------------------------------------------------
227 ClangExpressionDeclMap *
230 return m_expr_decl_map.get();
233 //------------------------------------------------------------------
234 /// Return the object that the parser should allow to access ASTs.
235 /// May be NULL if the ASTs do not need to be transformed.
237 /// @param[in] passthrough
238 /// The ASTConsumer that the returned transformer should send
239 /// the ASTs to after transformation.
240 //------------------------------------------------------------------
242 ASTTransformer (clang::ASTConsumer *passthrough);
244 //------------------------------------------------------------------
245 /// Return the desired result type of the function, or
246 /// eResultTypeAny if indifferent.
247 //------------------------------------------------------------------
251 return m_desired_type;
254 //------------------------------------------------------------------
255 /// Return true if validation code should be inserted into the
257 //------------------------------------------------------------------
264 //------------------------------------------------------------------
265 /// Return true if external variables in the expression should be
267 //------------------------------------------------------------------
269 NeedsVariableResolution ()
274 //------------------------------------------------------------------
275 /// Evaluate one expression and return its result.
277 /// @param[in] exe_ctx
278 /// The execution context to use when evaluating the expression.
280 /// @param[in] options
281 /// Expression evaluation options.
283 /// @param[in] expr_cstr
284 /// A C string containing the expression to be evaluated.
286 /// @param[in] expr_prefix
287 /// If non-NULL, a C string containing translation-unit level
288 /// definitions to be included when the expression is parsed.
290 /// @param[in/out] result_valobj_sp
291 /// If execution is successful, the result valobj is placed here.
294 /// Filled in with an error in case the expression evaluation
295 /// fails to parse, run, or evaluated.
298 /// A Process::ExecutionResults value. eExecutionCompleted for success.
299 //------------------------------------------------------------------
300 static ExecutionResults
301 Evaluate (ExecutionContext &exe_ctx,
302 const EvaluateExpressionOptions& options,
303 const char *expr_cstr,
304 const char *expr_prefix,
305 lldb::ValueObjectSP &result_valobj_sp,
308 static const Error::ValueType kNoResult = 0x1001; ///< ValueObject::GetError() returns this if there is no result from the expression.
310 //------------------------------------------------------------------
311 /// Populate m_cplusplus and m_objetivec based on the environment.
312 //------------------------------------------------------------------
315 ScanContext (ExecutionContext &exe_ctx,
316 lldb_private::Error &err);
319 PrepareToExecuteJITExpression (Stream &error_stream,
320 ExecutionContext &exe_ctx,
321 lldb::addr_t &struct_address,
322 lldb::addr_t &object_ptr,
323 lldb::addr_t &cmd_ptr);
326 InstallContext (ExecutionContext &exe_ctx);
329 LockAndCheckContext (ExecutionContext &exe_ctx,
330 lldb::TargetSP &target_sp,
331 lldb::ProcessSP &process_sp,
332 lldb::StackFrameSP &frame_sp);
334 lldb::ProcessWP m_process_wp; ///< The process used as the context for the expression.
335 Address m_address; ///< The address the process is stopped in.
336 lldb::addr_t m_stack_frame_bottom; ///< The bottom of the allocated stack frame.
337 lldb::addr_t m_stack_frame_top; ///< The top of the allocated stack frame.
339 std::string m_expr_text; ///< The text of the expression, as typed by the user
340 std::string m_expr_prefix; ///< The text of the translation-level definitions, as provided by the user
341 lldb::LanguageType m_language; ///< The language to use when parsing (eLanguageTypeUnknown means use defaults)
342 bool m_allow_cxx; ///< True if the language allows C++.
343 bool m_allow_objc; ///< True if the language allows Objective-C.
344 std::string m_transformed_text; ///< The text of the expression, as send to the parser
345 ResultType m_desired_type; ///< The type to coerce the expression's result to. If eResultTypeAny, inferred from the expression.
347 std::unique_ptr<ClangExpressionDeclMap> m_expr_decl_map; ///< The map to use when parsing the expression.
348 std::unique_ptr<IRExecutionUnit> m_execution_unit_ap; ///< The execution unit the expression is stored in.
349 std::unique_ptr<Materializer> m_materializer_ap; ///< The materializer to use when running the expression.
350 std::unique_ptr<ASTResultSynthesizer> m_result_synthesizer; ///< The result synthesizer, if one is needed.
352 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.
353 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).
354 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).
355 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).
356 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.
357 bool m_const_object; ///< True if "this" is const.
358 Target *m_target; ///< The target for storing persistent data like types and variables.
360 bool m_can_interpret; ///< True if the expression could be evaluated statically; false otherwise.
361 lldb::addr_t m_materialized_address; ///< The address at which the arguments to the expression have been materialized.
362 Materializer::DematerializerSP m_dematerializer_sp; ///< The dematerializer.
365 } // namespace lldb_private
367 #endif // liblldb_ClangUserExpression_h_