1 //===-- ClangExpressionParser.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_ClangExpressionParser_h_
11 #define liblldb_ClangExpressionParser_h_
13 #include "lldb/Core/ClangForward.h"
14 #include "lldb/Expression/DiagnosticManager.h"
15 #include "lldb/Expression/ExpressionParser.h"
16 #include "lldb/Utility/ArchSpec.h"
17 #include "lldb/Utility/Status.h"
18 #include "lldb/lldb-public.h"
24 class CodeCompleteConsumer;
27 namespace lldb_private {
29 class IRExecutionUnit;
31 //----------------------------------------------------------------------
32 /// @class ClangExpressionParser ClangExpressionParser.h
33 /// "lldb/Expression/ClangExpressionParser.h" Encapsulates an instance of
34 /// Clang that can parse expressions.
36 /// ClangExpressionParser is responsible for preparing an instance of
37 /// ClangExpression for execution. ClangExpressionParser uses ClangExpression
38 /// as a glorified parameter list, performing the required parsing and
39 /// conversion to formats (DWARF bytecode, or JIT compiled machine code) that
41 //----------------------------------------------------------------------
42 class ClangExpressionParser : public ExpressionParser {
44 //------------------------------------------------------------------
47 /// Initializes class variables.
49 /// @param[in] exe_scope,
50 /// If non-NULL, an execution context scope that can help to
51 /// correctly create an expression with a valid process for
52 /// optional tuning Objective-C runtime support. Can be NULL.
55 /// The expression to be parsed.
56 //------------------------------------------------------------------
57 ClangExpressionParser(ExecutionContextScope *exe_scope, Expression &expr,
58 bool generate_debug_info);
60 //------------------------------------------------------------------
62 //------------------------------------------------------------------
63 ~ClangExpressionParser() override;
65 bool Complete(CompletionRequest &request, unsigned line, unsigned pos,
66 unsigned typed_pos) override;
68 //------------------------------------------------------------------
69 /// Parse a single expression and convert it to IR using Clang. Don't wrap
70 /// the expression in anything at all.
72 /// @param[in] diagnostic_manager
73 /// The diagnostic manager to report errors to.
76 /// The number of errors encountered during parsing. 0 means
78 //------------------------------------------------------------------
79 unsigned Parse(DiagnosticManager &diagnostic_manager) override;
81 bool RewriteExpression(DiagnosticManager &diagnostic_manager) override;
83 //------------------------------------------------------------------
84 /// Ready an already-parsed expression for execution, possibly evaluating it
87 /// @param[out] func_addr
88 /// The address to which the function has been written.
90 /// @param[out] func_end
91 /// The end of the function's allocated memory region. (func_addr
92 /// and func_end do not delimit an allocated region; the allocated
93 /// region may begin before func_addr.)
95 /// @param[in] execution_unit_sp
96 /// After parsing, ownership of the execution unit for
97 /// for the expression is handed to this shared pointer.
99 /// @param[in] exe_ctx
100 /// The execution context to write the function into.
102 /// @param[out] evaluated_statically
103 /// Set to true if the expression could be interpreted statically;
104 /// untouched otherwise.
106 /// @param[out] const_result
107 /// If the result of the expression is constant, and the
108 /// expression has no side effects, this is set to the result of the
111 /// @param[in] execution_policy
112 /// Determines whether the expression must be JIT-compiled, must be
113 /// evaluated statically, or whether this decision may be made
114 /// opportunistically.
117 /// An error code indicating the success or failure of the operation.
118 /// Test with Success().
119 //------------------------------------------------------------------
121 PrepareForExecution(lldb::addr_t &func_addr, lldb::addr_t &func_end,
122 lldb::IRExecutionUnitSP &execution_unit_sp,
123 ExecutionContext &exe_ctx, bool &can_interpret,
124 lldb_private::ExecutionPolicy execution_policy) override;
126 //------------------------------------------------------------------
127 /// Run all static initializers for an execution unit.
129 /// @param[in] execution_unit_sp
130 /// The execution unit.
132 /// @param[in] exe_ctx
133 /// The execution context to use when running them. Thread can't be null.
136 /// The error code indicating the
137 //------------------------------------------------------------------
138 Status RunStaticInitializers(lldb::IRExecutionUnitSP &execution_unit_sp,
139 ExecutionContext &exe_ctx);
141 //------------------------------------------------------------------
142 /// Returns a string representing current ABI.
144 /// @param[in] target_arch
145 /// The target architecture.
148 /// A string representing target ABI for the current architecture.
149 //-------------------------------------------------------------------
150 std::string GetClangTargetABI(const ArchSpec &target_arch);
153 //------------------------------------------------------------------
154 /// Parses the expression.
156 /// @param[in] diagnostic_manager
157 /// The diagnostic manager that should receive the diagnostics
158 /// from the parsing process.
160 /// @param[in] completion
161 /// The completion consumer that should be used during parsing
162 /// (or a nullptr if no consumer should be attached).
164 /// @param[in] completion_line
165 /// The line in which the completion marker should be placed.
166 /// The first line is represented by the value 0.
168 /// @param[in] completion_column
169 /// The column in which the completion marker should be placed.
170 /// The first column is represented by the value 0.
173 /// The number of parsing errors.
174 //-------------------------------------------------------------------
175 unsigned ParseInternal(DiagnosticManager &diagnostic_manager,
176 clang::CodeCompleteConsumer *completion = nullptr,
177 unsigned completion_line = 0,
178 unsigned completion_column = 0);
180 std::unique_ptr<llvm::LLVMContext>
181 m_llvm_context; ///< The LLVM context to generate IR into
182 std::unique_ptr<clang::FileManager>
183 m_file_manager; ///< The Clang file manager object used by the compiler
184 std::unique_ptr<clang::CompilerInstance>
185 m_compiler; ///< The Clang compiler used to parse expressions into IR
186 std::unique_ptr<clang::CodeGenerator>
187 m_code_generator; ///< The Clang object that generates IR
189 class LLDBPreprocessorCallbacks;
190 LLDBPreprocessorCallbacks *m_pp_callbacks; ///< Called when the preprocessor
191 ///encounters module imports
192 std::unique_ptr<ClangASTContext> m_ast_context;
196 #endif // liblldb_ClangExpressionParser_h_