blob: 01ab3d8f2d2a87a027a803a7137fd66cfe636f7d [file] [log] [blame]
Sean Callanan1a8d4092010-08-27 01:01:44 +00001//===-- ClangExpressionParser.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_ClangExpressionParser_h_
11#define liblldb_ClangExpressionParser_h_
12
Greg Claytone0d378b2011-03-24 21:19:54 +000013#include "lldb/lldb-public.h"
Greg Clayton514487e2011-02-15 21:59:32 +000014#include "lldb/Core/ArchSpec.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000015#include "lldb/Core/ClangForward.h"
16#include "lldb/Core/Error.h"
Jim Ingham151c0322015-09-15 21:13:50 +000017#include "lldb/Expression/ExpressionParser.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000018
19#include "IRForTarget.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000020
21#include <string>
22#include <vector>
23
Sean Callanan1a8d4092010-08-27 01:01:44 +000024namespace lldb_private
25{
26
Sean Callanan8dfb68e2013-03-19 00:10:07 +000027class IRExecutionUnit;
Sean Callanan1a8d4092010-08-27 01:01:44 +000028
29//----------------------------------------------------------------------
30/// @class ClangExpressionParser ClangExpressionParser.h "lldb/Expression/ClangExpressionParser.h"
31/// @brief Encapsulates an instance of Clang that can parse expressions.
32///
33/// ClangExpressionParser is responsible for preparing an instance of
34/// ClangExpression for execution. ClangExpressionParser uses ClangExpression
35/// as a glorified parameter list, performing the required parsing and
36/// conversion to formats (DWARF bytecode, or JIT compiled machine code)
37/// that can be executed.
38//----------------------------------------------------------------------
Jim Ingham151c0322015-09-15 21:13:50 +000039class ClangExpressionParser : public ExpressionParser
Sean Callanan1a8d4092010-08-27 01:01:44 +000040{
41public:
42 //------------------------------------------------------------------
43 /// Constructor
44 ///
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000045 /// Initializes class variables.
Sean Callanan1a8d4092010-08-27 01:01:44 +000046 ///
Greg Clayton514487e2011-02-15 21:59:32 +000047 /// @param[in] exe_scope,
48 /// If non-NULL, an execution context scope that can help to
49 /// correctly create an expression with a valid process for
50 /// optional tuning Objective-C runtime support. Can be NULL.
Sean Callananc3a16002011-01-17 23:42:46 +000051 ///
Sean Callanan1a8d4092010-08-27 01:01:44 +000052 /// @param[in] expr
53 /// The expression to be parsed.
54 //------------------------------------------------------------------
Greg Clayton514487e2011-02-15 21:59:32 +000055 ClangExpressionParser (ExecutionContextScope *exe_scope,
Jim Ingham151c0322015-09-15 21:13:50 +000056 Expression &expr,
Greg Clayton23f8c952014-03-24 23:10:19 +000057 bool generate_debug_info);
Sean Callanan1a8d4092010-08-27 01:01:44 +000058
59 //------------------------------------------------------------------
60 /// Destructor
61 //------------------------------------------------------------------
Jim Ingham151c0322015-09-15 21:13:50 +000062 ~ClangExpressionParser () override;
Sean Callanan1a8d4092010-08-27 01:01:44 +000063
64 //------------------------------------------------------------------
65 /// Parse a single expression and convert it to IR using Clang. Don't
66 /// wrap the expression in anything at all.
67 ///
68 /// @param[in] stream
69 /// The stream to print errors to.
70 ///
71 /// @return
72 /// The number of errors encountered during parsing. 0 means
73 /// success.
74 //------------------------------------------------------------------
75 unsigned
Jim Ingham151c0322015-09-15 21:13:50 +000076 Parse (Stream &stream) override;
Sean Callanan1a8d4092010-08-27 01:01:44 +000077
78 //------------------------------------------------------------------
Sean Callanan3bfdaa22011-09-15 02:13:07 +000079 /// Ready an already-parsed expression for execution, possibly
80 /// evaluating it statically.
Sean Callanan1a8d4092010-08-27 01:01:44 +000081 ///
82 /// @param[out] func_addr
83 /// The address to which the function has been written.
84 ///
Sean Callanane71d5532010-08-27 23:31:21 +000085 /// @param[out] func_end
86 /// The end of the function's allocated memory region. (func_addr
87 /// and func_end do not delimit an allocated region; the allocated
88 /// region may begin before func_addr.)
89 ///
Greg Clayton23f8c952014-03-24 23:10:19 +000090 /// @param[in] execution_unit_sp
Sean Callanan5a1af4e2013-04-05 02:22:57 +000091 /// After parsing, ownership of the execution unit for
Greg Clayton23f8c952014-03-24 23:10:19 +000092 /// for the expression is handed to this shared pointer.
Sean Callanan5a1af4e2013-04-05 02:22:57 +000093 ///
Sean Callanane71d5532010-08-27 23:31:21 +000094 /// @param[in] exe_ctx
Sean Callanan1a8d4092010-08-27 01:01:44 +000095 /// The execution context to write the function into.
96 ///
Sean Callanan3bfdaa22011-09-15 02:13:07 +000097 /// @param[out] evaluated_statically
98 /// Set to true if the expression could be interpreted statically;
99 /// untouched otherwise.
Sean Callanan79763a42011-05-23 21:40:23 +0000100 ///
Sean Callanane4ec90e2010-12-16 03:17:46 +0000101 /// @param[out] const_result
Sean Callanan63697e52011-05-07 01:06:41 +0000102 /// If the result of the expression is constant, and the
Sean Callanane4ec90e2010-12-16 03:17:46 +0000103 /// expression has no side effects, this is set to the result of the
Sean Callanan63697e52011-05-07 01:06:41 +0000104 /// expression.
105 ///
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000106 /// @param[in] execution_policy
107 /// Determines whether the expression must be JIT-compiled, must be
108 /// evaluated statically, or whether this decision may be made
109 /// opportunistically.
Sean Callanane4ec90e2010-12-16 03:17:46 +0000110 ///
Sean Callanan1a8d4092010-08-27 01:01:44 +0000111 /// @return
112 /// An error code indicating the success or failure of the operation.
113 /// Test with Success().
114 //------------------------------------------------------------------
115 Error
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000116 PrepareForExecution (lldb::addr_t &func_addr,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000117 lldb::addr_t &func_end,
Greg Clayton23f8c952014-03-24 23:10:19 +0000118 std::shared_ptr<IRExecutionUnit> &execution_unit_sp,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000119 ExecutionContext &exe_ctx,
Sean Callanan1582ee62013-04-18 22:06:33 +0000120 bool &can_interpret,
Jim Ingham151c0322015-09-15 21:13:50 +0000121 lldb_private::ExecutionPolicy execution_policy) override;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000122
Sean Callanan1a8d4092010-08-27 01:01:44 +0000123private:
Greg Clayton7b0992d2013-04-18 22:45:39 +0000124 std::unique_ptr<llvm::LLVMContext> m_llvm_context; ///< The LLVM context to generate IR into
125 std::unique_ptr<clang::FileManager> m_file_manager; ///< The Clang file manager object used by the compiler
126 std::unique_ptr<clang::CompilerInstance> m_compiler; ///< The Clang compiler used to parse expressions into IR
127 std::unique_ptr<clang::Builtin::Context> m_builtin_context; ///< Context for Clang built-ins
128 std::unique_ptr<clang::SelectorTable> m_selector_table; ///< Selector table for Objective-C methods
Greg Clayton7b0992d2013-04-18 22:45:39 +0000129 std::unique_ptr<clang::CodeGenerator> m_code_generator; ///< The Clang object that generates IR
Sean Callananc8278af2014-12-05 01:27:35 +0000130
131 class LLDBPreprocessorCallbacks;
132 LLDBPreprocessorCallbacks *m_pp_callbacks; ///< Called when the preprocessor encounters module imports
Greg Claytond8d4a572015-08-11 21:38:15 +0000133 std::unique_ptr<ClangASTContext> m_ast_context;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000134};
135
136}
137
138#endif // liblldb_ClangExpressionParser_h_