1 //===-- ClangExpressionParser.h ---------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONPARSER_H
10 #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONPARSER_H
12 #include "lldb/Expression/DiagnosticManager.h"
13 #include "lldb/Expression/ExpressionParser.h"
14 #include "lldb/Utility/ArchSpec.h"
15 #include "lldb/Utility/Status.h"
16 #include "lldb/lldb-public.h"
27 class CodeCompleteConsumer
;
28 class CompilerInstance
;
31 namespace lldb_private
{
33 class IRExecutionUnit
;
34 class TypeSystemClang
;
36 /// \class ClangExpressionParser ClangExpressionParser.h
37 /// "lldb/Expression/ClangExpressionParser.h" Encapsulates an instance of
38 /// Clang that can parse expressions.
40 /// ClangExpressionParser is responsible for preparing an instance of
41 /// ClangExpression for execution. ClangExpressionParser uses ClangExpression
42 /// as a glorified parameter list, performing the required parsing and
43 /// conversion to formats (DWARF bytecode, or JIT compiled machine code) that
45 class ClangExpressionParser
: public ExpressionParser
{
49 /// Initializes class variables.
51 /// \param[in] exe_scope
52 /// If non-NULL, an execution context scope that can help to
53 /// correctly create an expression with a valid process for
54 /// optional tuning Objective-C runtime support. Can be NULL.
57 /// The expression to be parsed.
59 /// @param[in] include_directories
60 /// List of include directories that should be used when parsing the
63 /// @param[in] filename
64 /// Name of the source file that should be used when rendering
65 /// diagnostics (i.e. errors, warnings or notes from Clang).
66 ClangExpressionParser(ExecutionContextScope
*exe_scope
, Expression
&expr
,
67 bool generate_debug_info
,
68 std::vector
<std::string
> include_directories
= {},
69 std::string filename
= "<clang expression>");
72 ~ClangExpressionParser() override
;
74 bool Complete(CompletionRequest
&request
, unsigned line
, unsigned pos
,
75 unsigned typed_pos
) override
;
77 /// Parse a single expression and convert it to IR using Clang. Don't wrap
78 /// the expression in anything at all.
80 /// \param[in] diagnostic_manager
81 /// The diagnostic manager to report errors to.
84 /// The number of errors encountered during parsing. 0 means
86 unsigned Parse(DiagnosticManager
&diagnostic_manager
);
88 bool RewriteExpression(DiagnosticManager
&diagnostic_manager
) override
;
90 /// Ready an already-parsed expression for execution, possibly evaluating it
93 /// \param[out] func_addr
94 /// The address to which the function has been written.
96 /// \param[out] func_end
97 /// The end of the function's allocated memory region. (func_addr
98 /// and func_end do not delimit an allocated region; the allocated
99 /// region may begin before func_addr.)
101 /// \param[in] execution_unit_sp
102 /// After parsing, ownership of the execution unit for
103 /// for the expression is handed to this shared pointer.
105 /// \param[in] exe_ctx
106 /// The execution context to write the function into.
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.
114 /// An error code indicating the success or failure of the operation.
115 /// Test with Success().
116 Status
DoPrepareForExecution(
117 lldb::addr_t
&func_addr
, lldb::addr_t
&func_end
,
118 lldb::IRExecutionUnitSP
&execution_unit_sp
, ExecutionContext
&exe_ctx
,
120 lldb_private::ExecutionPolicy execution_policy
) override
;
123 /// Parses the expression.
125 /// \param[in] diagnostic_manager
126 /// The diagnostic manager that should receive the diagnostics
127 /// from the parsing process.
129 /// \param[in] completion
130 /// The completion consumer that should be used during parsing
131 /// (or a nullptr if no consumer should be attached).
133 /// \param[in] completion_line
134 /// The line in which the completion marker should be placed.
135 /// The first line is represented by the value 0.
137 /// \param[in] completion_column
138 /// The column in which the completion marker should be placed.
139 /// The first column is represented by the value 0.
142 /// The number of parsing errors.
143 unsigned ParseInternal(DiagnosticManager
&diagnostic_manager
,
144 clang::CodeCompleteConsumer
*completion
= nullptr,
145 unsigned completion_line
= 0,
146 unsigned completion_column
= 0);
148 std::unique_ptr
<llvm::LLVMContext
>
149 m_llvm_context
; ///< The LLVM context to generate IR into
150 std::unique_ptr
<clang::CompilerInstance
>
151 m_compiler
; ///< The Clang compiler used to parse expressions into IR
152 std::unique_ptr
<clang::CodeGenerator
>
153 m_code_generator
; ///< The Clang object that generates IR
155 class LLDBPreprocessorCallbacks
;
156 LLDBPreprocessorCallbacks
*m_pp_callbacks
; ///< Called when the preprocessor
157 ///encounters module imports
158 std::shared_ptr
<TypeSystemClang
> m_ast_context
;
160 std::vector
<std::string
> m_include_directories
;
161 /// File name used for the user expression.
162 std::string m_filename
;
166 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONPARSER_H