blob: 610008efdd4c7056066d74c56084168fb9dfe069 [file] [log] [blame]
Sean Callanan1a8d4092010-08-27 01:01:44 +00001//===-- ClangExpressionParser.h ---------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sean Callanan1a8d4092010-08-27 01:01:44 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef liblldb_ClangExpressionParser_h_
10#define liblldb_ClangExpressionParser_h_
11
Sean Callanan1a8d4092010-08-27 01:01:44 +000012#include "lldb/Core/ClangForward.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000013#include "lldb/Expression/DiagnosticManager.h"
Jim Ingham151c0322015-09-15 21:13:50 +000014#include "lldb/Expression/ExpressionParser.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000015#include "lldb/Utility/ArchSpec.h"
Zachary Turner97206d52017-05-12 04:51:55 +000016#include "lldb/Utility/Status.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000017#include "lldb/lldb-public.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000018
Sean Callanan1a8d4092010-08-27 01:01:44 +000019#include <string>
20#include <vector>
21
Raphael Isemann74829732018-08-30 17:29:37 +000022namespace clang {
23class CodeCompleteConsumer;
24}
25
Kate Stoneb9c1b512016-09-06 20:57:50 +000026namespace lldb_private {
Sean Callanan1a8d4092010-08-27 01:01:44 +000027
Sean Callanan8dfb68e2013-03-19 00:10:07 +000028class IRExecutionUnit;
Kate Stoneb9c1b512016-09-06 20:57:50 +000029
Sean Callanan1a8d4092010-08-27 01:01:44 +000030//----------------------------------------------------------------------
Adrian Prantlf05b42e2019-03-11 17:09:29 +000031/// \class ClangExpressionParser ClangExpressionParser.h
Adrian Prantld8f460e2018-05-02 16:55:16 +000032/// "lldb/Expression/ClangExpressionParser.h" Encapsulates an instance of
33/// Clang that can parse expressions.
Sean Callanan1a8d4092010-08-27 01:01:44 +000034///
35/// ClangExpressionParser is responsible for preparing an instance of
36/// ClangExpression for execution. ClangExpressionParser uses ClangExpression
37/// as a glorified parameter list, performing the required parsing and
Adrian Prantld8f460e2018-05-02 16:55:16 +000038/// conversion to formats (DWARF bytecode, or JIT compiled machine code) that
39/// can be executed.
Sean Callanan1a8d4092010-08-27 01:01:44 +000040//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000041class ClangExpressionParser : public ExpressionParser {
Sean Callanan1a8d4092010-08-27 01:01:44 +000042public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000043 //------------------------------------------------------------------
44 /// Constructor
45 ///
46 /// Initializes class variables.
47 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000048 /// \param[in] exe_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +000049 /// If non-NULL, an execution context scope that can help to
50 /// correctly create an expression with a valid process for
51 /// optional tuning Objective-C runtime support. Can be NULL.
52 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000053 /// \param[in] expr
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 /// The expression to be parsed.
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +000055 ///
56 /// @param[in] include_directories
57 /// List of include directories that should be used when parsing the
58 /// expression.
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 //------------------------------------------------------------------
60 ClangExpressionParser(ExecutionContextScope *exe_scope, Expression &expr,
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +000061 bool generate_debug_info,
62 std::vector<ConstString> include_directories = {});
Sean Callanan579e70c2016-03-19 00:03:59 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 //------------------------------------------------------------------
65 /// Destructor
66 //------------------------------------------------------------------
67 ~ClangExpressionParser() override;
Sagar Thakuradc1abe2016-05-13 11:04:47 +000068
Raphael Isemannc11a7802018-08-30 21:26:32 +000069 bool Complete(CompletionRequest &request, unsigned line, unsigned pos,
Raphael Isemann74829732018-08-30 17:29:37 +000070 unsigned typed_pos) override;
71
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 //------------------------------------------------------------------
Adrian Prantld8f460e2018-05-02 16:55:16 +000073 /// Parse a single expression and convert it to IR using Clang. Don't wrap
74 /// the expression in anything at all.
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000076 /// \param[in] diagnostic_manager
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 /// The diagnostic manager to report errors to.
78 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000079 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 /// The number of errors encountered during parsing. 0 means
81 /// success.
82 //------------------------------------------------------------------
83 unsigned Parse(DiagnosticManager &diagnostic_manager) override;
84
85 bool RewriteExpression(DiagnosticManager &diagnostic_manager) override;
86
87 //------------------------------------------------------------------
Adrian Prantld8f460e2018-05-02 16:55:16 +000088 /// Ready an already-parsed expression for execution, possibly evaluating it
89 /// statically.
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000091 /// \param[out] func_addr
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 /// The address to which the function has been written.
93 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000094 /// \param[out] func_end
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 /// The end of the function's allocated memory region. (func_addr
96 /// and func_end do not delimit an allocated region; the allocated
97 /// region may begin before func_addr.)
98 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000099 /// \param[in] execution_unit_sp
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 /// After parsing, ownership of the execution unit for
101 /// for the expression is handed to this shared pointer.
102 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000103 /// \param[in] exe_ctx
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 /// The execution context to write the function into.
105 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000106 /// \param[out] evaluated_statically
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 /// Set to true if the expression could be interpreted statically;
108 /// untouched otherwise.
109 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000110 /// \param[out] const_result
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 /// If the result of the expression is constant, and the
112 /// expression has no side effects, this is set to the result of the
113 /// expression.
114 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000115 /// \param[in] execution_policy
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 /// Determines whether the expression must be JIT-compiled, must be
117 /// evaluated statically, or whether this decision may be made
118 /// opportunistically.
119 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000120 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 /// An error code indicating the success or failure of the operation.
122 /// Test with Success().
123 //------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000124 Status
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 PrepareForExecution(lldb::addr_t &func_addr, lldb::addr_t &func_end,
126 lldb::IRExecutionUnitSP &execution_unit_sp,
127 ExecutionContext &exe_ctx, bool &can_interpret,
128 lldb_private::ExecutionPolicy execution_policy) override;
129
130 //------------------------------------------------------------------
131 /// Run all static initializers for an execution unit.
132 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000133 /// \param[in] execution_unit_sp
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 /// The execution unit.
135 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000136 /// \param[in] exe_ctx
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 /// The execution context to use when running them. Thread can't be null.
138 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000139 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 /// The error code indicating the
141 //------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000142 Status RunStaticInitializers(lldb::IRExecutionUnitSP &execution_unit_sp,
143 ExecutionContext &exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144
145 //------------------------------------------------------------------
146 /// Returns a string representing current ABI.
147 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000148 /// \param[in] target_arch
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 /// The target architecture.
150 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000151 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152 /// A string representing target ABI for the current architecture.
153 //-------------------------------------------------------------------
154 std::string GetClangTargetABI(const ArchSpec &target_arch);
155
Sean Callanan1a8d4092010-08-27 01:01:44 +0000156private:
Raphael Isemann74829732018-08-30 17:29:37 +0000157 //------------------------------------------------------------------
158 /// Parses the expression.
159 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000160 /// \param[in] diagnostic_manager
Raphael Isemann74829732018-08-30 17:29:37 +0000161 /// The diagnostic manager that should receive the diagnostics
162 /// from the parsing process.
163 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000164 /// \param[in] completion
Raphael Isemann74829732018-08-30 17:29:37 +0000165 /// The completion consumer that should be used during parsing
166 /// (or a nullptr if no consumer should be attached).
167 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000168 /// \param[in] completion_line
Raphael Isemann74829732018-08-30 17:29:37 +0000169 /// The line in which the completion marker should be placed.
170 /// The first line is represented by the value 0.
171 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000172 /// \param[in] completion_column
Raphael Isemann74829732018-08-30 17:29:37 +0000173 /// The column in which the completion marker should be placed.
174 /// The first column is represented by the value 0.
175 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000176 /// \return
Raphael Isemann74829732018-08-30 17:29:37 +0000177 /// The number of parsing errors.
178 //-------------------------------------------------------------------
179 unsigned ParseInternal(DiagnosticManager &diagnostic_manager,
180 clang::CodeCompleteConsumer *completion = nullptr,
181 unsigned completion_line = 0,
182 unsigned completion_column = 0);
183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 std::unique_ptr<llvm::LLVMContext>
185 m_llvm_context; ///< The LLVM context to generate IR into
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 std::unique_ptr<clang::CompilerInstance>
187 m_compiler; ///< The Clang compiler used to parse expressions into IR
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 std::unique_ptr<clang::CodeGenerator>
189 m_code_generator; ///< The Clang object that generates IR
190
191 class LLDBPreprocessorCallbacks;
192 LLDBPreprocessorCallbacks *m_pp_callbacks; ///< Called when the preprocessor
193 ///encounters module imports
194 std::unique_ptr<ClangASTContext> m_ast_context;
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000195
196 std::vector<ConstString> m_include_directories;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000197};
Sean Callanan1a8d4092010-08-27 01:01:44 +0000198}
199
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200#endif // liblldb_ClangExpressionParser_h_