ClangExpressionParser.h revision 355940
1//===-- ClangExpressionParser.h ---------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef liblldb_ClangExpressionParser_h_
10#define liblldb_ClangExpressionParser_h_
11
12#include "lldb/Core/ClangForward.h"
13#include "lldb/Expression/DiagnosticManager.h"
14#include "lldb/Expression/ExpressionParser.h"
15#include "lldb/Utility/ArchSpec.h"
16#include "lldb/Utility/Status.h"
17#include "lldb/lldb-public.h"
18
19#include <string>
20#include <vector>
21
22namespace clang {
23class CodeCompleteConsumer;
24}
25
26namespace lldb_private {
27
28class IRExecutionUnit;
29
30/// \class ClangExpressionParser ClangExpressionParser.h
31/// "lldb/Expression/ClangExpressionParser.h" Encapsulates an instance of
32/// Clang that can parse expressions.
33///
34/// ClangExpressionParser is responsible for preparing an instance of
35/// ClangExpression for execution.  ClangExpressionParser uses ClangExpression
36/// as a glorified parameter list, performing the required parsing and
37/// conversion to formats (DWARF bytecode, or JIT compiled machine code) that
38/// can be executed.
39class ClangExpressionParser : public ExpressionParser {
40public:
41  /// Constructor
42  ///
43  /// Initializes class variables.
44  ///
45  /// \param[in] exe_scope,
46  ///     If non-NULL, an execution context scope that can help to
47  ///     correctly create an expression with a valid process for
48  ///     optional tuning Objective-C runtime support. Can be NULL.
49  ///
50  /// \param[in] expr
51  ///     The expression to be parsed.
52  ///
53  /// @param[in] include_directories
54  ///     List of include directories that should be used when parsing the
55  ///     expression.
56  ClangExpressionParser(ExecutionContextScope *exe_scope, Expression &expr,
57                        bool generate_debug_info,
58                        std::vector<ConstString> include_directories = {});
59
60  /// Destructor
61  ~ClangExpressionParser() override;
62
63  bool Complete(CompletionRequest &request, unsigned line, unsigned pos,
64                unsigned typed_pos) override;
65
66  /// Parse a single expression and convert it to IR using Clang.  Don't wrap
67  /// the expression in anything at all.
68  ///
69  /// \param[in] diagnostic_manager
70  ///     The diagnostic manager to report errors to.
71  ///
72  /// \return
73  ///     The number of errors encountered during parsing.  0 means
74  ///     success.
75  unsigned Parse(DiagnosticManager &diagnostic_manager) override;
76
77  bool RewriteExpression(DiagnosticManager &diagnostic_manager) override;
78
79  /// Ready an already-parsed expression for execution, possibly evaluating it
80  /// statically.
81  ///
82  /// \param[out] func_addr
83  ///     The address to which the function has been written.
84  ///
85  /// \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  ///
90  /// \param[in] execution_unit_sp
91  ///     After parsing, ownership of the execution unit for
92  ///     for the expression is handed to this shared pointer.
93  ///
94  /// \param[in] exe_ctx
95  ///     The execution context to write the function into.
96  ///
97  /// \param[out] evaluated_statically
98  ///     Set to true if the expression could be interpreted statically;
99  ///     untouched otherwise.
100  ///
101  /// \param[out] const_result
102  ///     If the result of the expression is constant, and the
103  ///     expression has no side effects, this is set to the result of the
104  ///     expression.
105  ///
106  /// \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.
110  ///
111  /// \return
112  ///     An error code indicating the success or failure of the operation.
113  ///     Test with Success().
114  Status
115  PrepareForExecution(lldb::addr_t &func_addr, lldb::addr_t &func_end,
116                      lldb::IRExecutionUnitSP &execution_unit_sp,
117                      ExecutionContext &exe_ctx, bool &can_interpret,
118                      lldb_private::ExecutionPolicy execution_policy) override;
119
120  /// Run all static initializers for an execution unit.
121  ///
122  /// \param[in] execution_unit_sp
123  ///     The execution unit.
124  ///
125  /// \param[in] exe_ctx
126  ///     The execution context to use when running them.  Thread can't be null.
127  ///
128  /// \return
129  ///     The error code indicating the
130  Status RunStaticInitializers(lldb::IRExecutionUnitSP &execution_unit_sp,
131                               ExecutionContext &exe_ctx);
132
133  /// Returns a string representing current ABI.
134  ///
135  /// \param[in] target_arch
136  ///     The target architecture.
137  ///
138  /// \return
139  ///     A string representing target ABI for the current architecture.
140  std::string GetClangTargetABI(const ArchSpec &target_arch);
141
142private:
143  /// Parses the expression.
144  ///
145  /// \param[in] diagnostic_manager
146  ///     The diagnostic manager that should receive the diagnostics
147  ///     from the parsing process.
148  ///
149  /// \param[in] completion
150  ///     The completion consumer that should be used during parsing
151  ///     (or a nullptr if no consumer should be attached).
152  ///
153  /// \param[in] completion_line
154  ///     The line in which the completion marker should be placed.
155  ///     The first line is represented by the value 0.
156  ///
157  /// \param[in] completion_column
158  ///     The column in which the completion marker should be placed.
159  ///     The first column is represented by the value 0.
160  ///
161  /// \return
162  ///    The number of parsing errors.
163  unsigned ParseInternal(DiagnosticManager &diagnostic_manager,
164                         clang::CodeCompleteConsumer *completion = nullptr,
165                         unsigned completion_line = 0,
166                         unsigned completion_column = 0);
167
168  std::unique_ptr<llvm::LLVMContext>
169      m_llvm_context; ///< The LLVM context to generate IR into
170  std::unique_ptr<clang::CompilerInstance>
171      m_compiler; ///< The Clang compiler used to parse expressions into IR
172  std::unique_ptr<clang::CodeGenerator>
173      m_code_generator; ///< The Clang object that generates IR
174
175  class LLDBPreprocessorCallbacks;
176  LLDBPreprocessorCallbacks *m_pp_callbacks; ///< Called when the preprocessor
177                                             ///encounters module imports
178  std::unique_ptr<ClangASTContext> m_ast_context;
179
180  std::vector<ConstString> m_include_directories;
181};
182}
183
184#endif // liblldb_ClangExpressionParser_h_
185