1061da546Spatrick //===-- ClangExpressionParser.h ---------------------------------*- C++ -*-===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9dda28197Spatrick #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONPARSER_H
10dda28197Spatrick #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONPARSER_H
11061da546Spatrick 
12061da546Spatrick #include "lldb/Expression/DiagnosticManager.h"
13061da546Spatrick #include "lldb/Expression/ExpressionParser.h"
14061da546Spatrick #include "lldb/Utility/ArchSpec.h"
15061da546Spatrick #include "lldb/Utility/Status.h"
16061da546Spatrick #include "lldb/lldb-public.h"
17061da546Spatrick 
18061da546Spatrick #include <string>
19061da546Spatrick #include <vector>
20061da546Spatrick 
21dda28197Spatrick namespace llvm {
22dda28197Spatrick class LLVMContext;
23061da546Spatrick }
24061da546Spatrick 
25dda28197Spatrick namespace clang {
26dda28197Spatrick class CodeGenerator;
27dda28197Spatrick class CodeCompleteConsumer;
28dda28197Spatrick class CompilerInstance;
29dda28197Spatrick } // namespace clang
30dda28197Spatrick 
31061da546Spatrick namespace lldb_private {
32061da546Spatrick 
33061da546Spatrick class IRExecutionUnit;
34dda28197Spatrick class TypeSystemClang;
35061da546Spatrick 
36061da546Spatrick /// \class ClangExpressionParser ClangExpressionParser.h
37061da546Spatrick /// "lldb/Expression/ClangExpressionParser.h" Encapsulates an instance of
38061da546Spatrick /// Clang that can parse expressions.
39061da546Spatrick ///
40061da546Spatrick /// ClangExpressionParser is responsible for preparing an instance of
41061da546Spatrick /// ClangExpression for execution.  ClangExpressionParser uses ClangExpression
42061da546Spatrick /// as a glorified parameter list, performing the required parsing and
43061da546Spatrick /// conversion to formats (DWARF bytecode, or JIT compiled machine code) that
44061da546Spatrick /// can be executed.
45061da546Spatrick class ClangExpressionParser : public ExpressionParser {
46061da546Spatrick public:
47061da546Spatrick   /// Constructor
48061da546Spatrick   ///
49061da546Spatrick   /// Initializes class variables.
50061da546Spatrick   ///
51061da546Spatrick   /// \param[in] exe_scope
52061da546Spatrick   ///     If non-NULL, an execution context scope that can help to
53061da546Spatrick   ///     correctly create an expression with a valid process for
54061da546Spatrick   ///     optional tuning Objective-C runtime support. Can be NULL.
55061da546Spatrick   ///
56061da546Spatrick   /// \param[in] expr
57061da546Spatrick   ///     The expression to be parsed.
58061da546Spatrick   ///
59061da546Spatrick   /// @param[in] include_directories
60061da546Spatrick   ///     List of include directories that should be used when parsing the
61061da546Spatrick   ///     expression.
62061da546Spatrick   ///
63061da546Spatrick   /// @param[in] filename
64061da546Spatrick   ///     Name of the source file that should be used when rendering
65061da546Spatrick   ///     diagnostics (i.e. errors, warnings or notes from Clang).
66061da546Spatrick   ClangExpressionParser(ExecutionContextScope *exe_scope, Expression &expr,
67061da546Spatrick                         bool generate_debug_info,
68061da546Spatrick                         std::vector<std::string> include_directories = {},
69061da546Spatrick                         std::string filename = "<clang expression>");
70061da546Spatrick 
71061da546Spatrick   /// Destructor
72061da546Spatrick   ~ClangExpressionParser() override;
73061da546Spatrick 
74061da546Spatrick   bool Complete(CompletionRequest &request, unsigned line, unsigned pos,
75061da546Spatrick                 unsigned typed_pos) override;
76061da546Spatrick 
77061da546Spatrick   /// Parse a single expression and convert it to IR using Clang.  Don't wrap
78061da546Spatrick   /// the expression in anything at all.
79061da546Spatrick   ///
80061da546Spatrick   /// \param[in] diagnostic_manager
81061da546Spatrick   ///     The diagnostic manager to report errors to.
82061da546Spatrick   ///
83061da546Spatrick   /// \return
84061da546Spatrick   ///     The number of errors encountered during parsing.  0 means
85061da546Spatrick   ///     success.
86061da546Spatrick   unsigned Parse(DiagnosticManager &diagnostic_manager);
87061da546Spatrick 
88061da546Spatrick   bool RewriteExpression(DiagnosticManager &diagnostic_manager) override;
89061da546Spatrick 
90061da546Spatrick   /// Ready an already-parsed expression for execution, possibly evaluating it
91061da546Spatrick   /// statically.
92061da546Spatrick   ///
93061da546Spatrick   /// \param[out] func_addr
94061da546Spatrick   ///     The address to which the function has been written.
95061da546Spatrick   ///
96061da546Spatrick   /// \param[out] func_end
97061da546Spatrick   ///     The end of the function's allocated memory region.  (func_addr
98061da546Spatrick   ///     and func_end do not delimit an allocated region; the allocated
99061da546Spatrick   ///     region may begin before func_addr.)
100061da546Spatrick   ///
101061da546Spatrick   /// \param[in] execution_unit_sp
102061da546Spatrick   ///     After parsing, ownership of the execution unit for
103061da546Spatrick   ///     for the expression is handed to this shared pointer.
104061da546Spatrick   ///
105061da546Spatrick   /// \param[in] exe_ctx
106061da546Spatrick   ///     The execution context to write the function into.
107061da546Spatrick   ///
108061da546Spatrick   /// \param[in] execution_policy
109061da546Spatrick   ///     Determines whether the expression must be JIT-compiled, must be
110061da546Spatrick   ///     evaluated statically, or whether this decision may be made
111061da546Spatrick   ///     opportunistically.
112061da546Spatrick   ///
113061da546Spatrick   /// \return
114061da546Spatrick   ///     An error code indicating the success or failure of the operation.
115061da546Spatrick   ///     Test with Success().
116061da546Spatrick   Status
117061da546Spatrick   PrepareForExecution(lldb::addr_t &func_addr, lldb::addr_t &func_end,
118061da546Spatrick                       lldb::IRExecutionUnitSP &execution_unit_sp,
119061da546Spatrick                       ExecutionContext &exe_ctx, bool &can_interpret,
120061da546Spatrick                       lldb_private::ExecutionPolicy execution_policy) override;
121061da546Spatrick 
122061da546Spatrick   /// Run all static initializers for an execution unit.
123061da546Spatrick   ///
124061da546Spatrick   /// \param[in] execution_unit_sp
125061da546Spatrick   ///     The execution unit.
126061da546Spatrick   ///
127061da546Spatrick   /// \param[in] exe_ctx
128061da546Spatrick   ///     The execution context to use when running them.  Thread can't be null.
129061da546Spatrick   ///
130061da546Spatrick   /// \return
131061da546Spatrick   ///     The error code indicating the
132061da546Spatrick   Status RunStaticInitializers(lldb::IRExecutionUnitSP &execution_unit_sp,
133061da546Spatrick                                ExecutionContext &exe_ctx);
134061da546Spatrick 
135061da546Spatrick   /// Returns a string representing current ABI.
136061da546Spatrick   ///
137061da546Spatrick   /// \param[in] target_arch
138061da546Spatrick   ///     The target architecture.
139061da546Spatrick   ///
140061da546Spatrick   /// \return
141061da546Spatrick   ///     A string representing target ABI for the current architecture.
142061da546Spatrick   std::string GetClangTargetABI(const ArchSpec &target_arch);
143061da546Spatrick 
144061da546Spatrick private:
145061da546Spatrick   /// Parses the expression.
146061da546Spatrick   ///
147061da546Spatrick   /// \param[in] diagnostic_manager
148061da546Spatrick   ///     The diagnostic manager that should receive the diagnostics
149061da546Spatrick   ///     from the parsing process.
150061da546Spatrick   ///
151061da546Spatrick   /// \param[in] completion
152061da546Spatrick   ///     The completion consumer that should be used during parsing
153061da546Spatrick   ///     (or a nullptr if no consumer should be attached).
154061da546Spatrick   ///
155061da546Spatrick   /// \param[in] completion_line
156061da546Spatrick   ///     The line in which the completion marker should be placed.
157061da546Spatrick   ///     The first line is represented by the value 0.
158061da546Spatrick   ///
159061da546Spatrick   /// \param[in] completion_column
160061da546Spatrick   ///     The column in which the completion marker should be placed.
161061da546Spatrick   ///     The first column is represented by the value 0.
162061da546Spatrick   ///
163061da546Spatrick   /// \return
164061da546Spatrick   ///    The number of parsing errors.
165061da546Spatrick   unsigned ParseInternal(DiagnosticManager &diagnostic_manager,
166061da546Spatrick                          clang::CodeCompleteConsumer *completion = nullptr,
167061da546Spatrick                          unsigned completion_line = 0,
168061da546Spatrick                          unsigned completion_column = 0);
169061da546Spatrick 
170061da546Spatrick   std::unique_ptr<llvm::LLVMContext>
171061da546Spatrick       m_llvm_context; ///< The LLVM context to generate IR into
172061da546Spatrick   std::unique_ptr<clang::CompilerInstance>
173061da546Spatrick       m_compiler; ///< The Clang compiler used to parse expressions into IR
174061da546Spatrick   std::unique_ptr<clang::CodeGenerator>
175061da546Spatrick       m_code_generator; ///< The Clang object that generates IR
176061da546Spatrick 
177061da546Spatrick   class LLDBPreprocessorCallbacks;
178061da546Spatrick   LLDBPreprocessorCallbacks *m_pp_callbacks; ///< Called when the preprocessor
179061da546Spatrick                                              ///encounters module imports
180*f6aab3d8Srobert   std::shared_ptr<TypeSystemClang> m_ast_context;
181061da546Spatrick 
182061da546Spatrick   std::vector<std::string> m_include_directories;
183061da546Spatrick   /// File name used for the user expression.
184061da546Spatrick   std::string m_filename;
185061da546Spatrick };
186061da546Spatrick }
187061da546Spatrick 
188dda28197Spatrick #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONPARSER_H
189