1 //===-- 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 
13 #include "lldb/Core/ArchSpec.h"
14 #include "lldb/Core/ClangForward.h"
15 #include "lldb/Core/Error.h"
16 #include "lldb/Expression/DiagnosticManager.h"
17 #include "lldb/Expression/ExpressionParser.h"
18 #include "lldb/lldb-public.h"
19 
20 #include <string>
21 #include <vector>
22 
23 namespace lldb_private
24 {
25 
26 class IRExecutionUnit;
27 
28 //----------------------------------------------------------------------
29 /// @class ClangExpressionParser ClangExpressionParser.h "lldb/Expression/ClangExpressionParser.h"
30 /// @brief Encapsulates an instance of Clang that can parse expressions.
31 ///
32 /// ClangExpressionParser is responsible for preparing an instance of
33 /// ClangExpression for execution.  ClangExpressionParser uses ClangExpression
34 /// as a glorified parameter list, performing the required parsing and
35 /// conversion to formats (DWARF bytecode, or JIT compiled machine code)
36 /// that can be executed.
37 //----------------------------------------------------------------------
38 class ClangExpressionParser : public ExpressionParser
39 {
40 public:
41     //------------------------------------------------------------------
42     /// Constructor
43     ///
44     /// Initializes class variables.
45     ///
46     /// @param[in] exe_scope,
47     ///     If non-NULL, an execution context scope that can help to
48     ///     correctly create an expression with a valid process for
49     ///     optional tuning Objective-C runtime support. Can be NULL.
50     ///
51     /// @param[in] expr
52     ///     The expression to be parsed.
53     //------------------------------------------------------------------
54     ClangExpressionParser (ExecutionContextScope *exe_scope,
55                            Expression &expr,
56                            bool generate_debug_info);
57 
58     //------------------------------------------------------------------
59     /// Destructor
60     //------------------------------------------------------------------
61     ~ClangExpressionParser () override;
62 
63     //------------------------------------------------------------------
64     /// Parse a single expression and convert it to IR using Clang.  Don't
65     /// wrap the expression in anything at all.
66     ///
67     /// @param[in] diagnostic_manager
68     ///     The diagnostic manager to report errors to.
69     ///
70     /// @return
71     ///     The number of errors encountered during parsing.  0 means
72     ///     success.
73     //------------------------------------------------------------------
74     unsigned
75     Parse(DiagnosticManager &diagnostic_manager) override;
76 
77     bool
78     RewriteExpression(DiagnosticManager &diagnostic_manager) override;
79 
80     //------------------------------------------------------------------
81     /// Ready an already-parsed expression for execution, possibly
82     /// evaluating it statically.
83     ///
84     /// @param[out] func_addr
85     ///     The address to which the function has been written.
86     ///
87     /// @param[out] func_end
88     ///     The end of the function's allocated memory region.  (func_addr
89     ///     and func_end do not delimit an allocated region; the allocated
90     ///     region may begin before func_addr.)
91     ///
92     /// @param[in] execution_unit_sp
93     ///     After parsing, ownership of the execution unit for
94     ///     for the expression is handed to this shared pointer.
95     ///
96     /// @param[in] exe_ctx
97     ///     The execution context to write the function into.
98     ///
99     /// @param[out] evaluated_statically
100     ///     Set to true if the expression could be interpreted statically;
101     ///     untouched otherwise.
102     ///
103     /// @param[out] const_result
104     ///     If the result of the expression is constant, and the
105     ///     expression has no side effects, this is set to the result of the
106     ///     expression.
107     ///
108     /// @param[in] execution_policy
109     ///     Determines whether the expression must be JIT-compiled, must be
110     ///     evaluated statically, or whether this decision may be made
111     ///     opportunistically.
112     ///
113     /// @return
114     ///     An error code indicating the success or failure of the operation.
115     ///     Test with Success().
116     //------------------------------------------------------------------
117     Error
118     PrepareForExecution (lldb::addr_t &func_addr,
119                          lldb::addr_t &func_end,
120                          lldb::IRExecutionUnitSP &execution_unit_sp,
121                          ExecutionContext &exe_ctx,
122                          bool &can_interpret,
123                          lldb_private::ExecutionPolicy execution_policy) override;
124 
125     //------------------------------------------------------------------
126     /// Run all static initializers for an execution unit.
127     ///
128     /// @param[in] execution_unit_sp
129     ///     The execution unit.
130     ///
131     /// @param[in] exe_ctx
132     ///     The execution context to use when running them.  Thread can't be null.
133     ///
134     /// @return
135     ///     The error code indicating the
136     //------------------------------------------------------------------
137     Error
138     RunStaticInitializers (lldb::IRExecutionUnitSP &execution_unit_sp,
139                            ExecutionContext &exe_ctx);
140 
141 private:
142     std::unique_ptr<llvm::LLVMContext>       m_llvm_context;         ///< The LLVM context to generate IR into
143     std::unique_ptr<clang::FileManager>      m_file_manager;         ///< The Clang file manager object used by the compiler
144     std::unique_ptr<clang::CompilerInstance> m_compiler;             ///< The Clang compiler used to parse expressions into IR
145     std::unique_ptr<clang::Builtin::Context> m_builtin_context;      ///< Context for Clang built-ins
146     std::unique_ptr<clang::SelectorTable>    m_selector_table;       ///< Selector table for Objective-C methods
147     std::unique_ptr<clang::CodeGenerator>    m_code_generator;       ///< The Clang object that generates IR
148 
149     class LLDBPreprocessorCallbacks;
150     LLDBPreprocessorCallbacks               *m_pp_callbacks;         ///< Called when the preprocessor encounters module imports
151     std::unique_ptr<ClangASTContext>         m_ast_context;
152 };
153 
154 }
155 
156 #endif  // liblldb_ClangExpressionParser_h_
157