blob: 80a92691eaacfb3f2a1f8cd7c87106d8c2da879a [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
Adrian Prantlf05b42e2019-03-11 17:09:29 +000030/// \class ClangExpressionParser ClangExpressionParser.h
Adrian Prantld8f460e2018-05-02 16:55:16 +000031/// "lldb/Expression/ClangExpressionParser.h" Encapsulates an instance of
32/// Clang that can parse expressions.
Sean Callanan1a8d4092010-08-27 01:01:44 +000033///
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
Adrian Prantld8f460e2018-05-02 16:55:16 +000037/// conversion to formats (DWARF bytecode, or JIT compiled machine code) that
38/// can be executed.
Kate Stoneb9c1b512016-09-06 20:57:50 +000039class ClangExpressionParser : public ExpressionParser {
Sean Callanan1a8d4092010-08-27 01:01:44 +000040public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000041 /// Constructor
42 ///
43 /// Initializes class variables.
44 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000045 /// \param[in] exe_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +000046 /// 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 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000050 /// \param[in] expr
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 /// The expression to be parsed.
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +000052 ///
53 /// @param[in] include_directories
54 /// List of include directories that should be used when parsing the
55 /// expression.
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 ClangExpressionParser(ExecutionContextScope *exe_scope, Expression &expr,
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +000057 bool generate_debug_info,
Raphael Isemann3ad82782019-09-11 14:33:11 +000058 std::vector<std::string> include_directories = {});
Sean Callanan579e70c2016-03-19 00:03:59 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 /// Destructor
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 ~ClangExpressionParser() override;
Sagar Thakuradc1abe2016-05-13 11:04:47 +000062
Raphael Isemannc11a7802018-08-30 21:26:32 +000063 bool Complete(CompletionRequest &request, unsigned line, unsigned pos,
Raphael Isemann74829732018-08-30 17:29:37 +000064 unsigned typed_pos) override;
65
Adrian Prantld8f460e2018-05-02 16:55:16 +000066 /// Parse a single expression and convert it to IR using Clang. Don't wrap
67 /// the expression in anything at all.
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000069 /// \param[in] diagnostic_manager
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 /// The diagnostic manager to report errors to.
71 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000072 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 /// The number of errors encountered during parsing. 0 means
74 /// success.
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 unsigned Parse(DiagnosticManager &diagnostic_manager) override;
76
77 bool RewriteExpression(DiagnosticManager &diagnostic_manager) override;
78
Adrian Prantld8f460e2018-05-02 16:55:16 +000079 /// Ready an already-parsed expression for execution, possibly evaluating it
80 /// statically.
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000082 /// \param[out] func_addr
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 /// The address to which the function has been written.
84 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000085 /// \param[out] func_end
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 /// 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 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000090 /// \param[in] execution_unit_sp
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 /// After parsing, ownership of the execution unit for
92 /// for the expression is handed to this shared pointer.
93 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000094 /// \param[in] exe_ctx
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 /// The execution context to write the function into.
96 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +000097 /// \param[out] evaluated_statically
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 /// Set to true if the expression could be interpreted statically;
99 /// untouched otherwise.
100 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000101 /// \param[out] const_result
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 /// 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 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000106 /// \param[in] execution_policy
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 /// Determines whether the expression must be JIT-compiled, must be
108 /// evaluated statically, or whether this decision may be made
109 /// opportunistically.
110 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000111 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 /// An error code indicating the success or failure of the operation.
113 /// Test with Success().
Zachary Turner97206d52017-05-12 04:51:55 +0000114 Status
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 /// Run all static initializers for an execution unit.
121 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000122 /// \param[in] execution_unit_sp
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 /// The execution unit.
124 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000125 /// \param[in] exe_ctx
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 /// The execution context to use when running them. Thread can't be null.
127 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000128 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 /// The error code indicating the
Zachary Turner97206d52017-05-12 04:51:55 +0000130 Status RunStaticInitializers(lldb::IRExecutionUnitSP &execution_unit_sp,
131 ExecutionContext &exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 /// Returns a string representing current ABI.
134 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000135 /// \param[in] target_arch
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 /// The target architecture.
137 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000138 /// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 /// A string representing target ABI for the current architecture.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 std::string GetClangTargetABI(const ArchSpec &target_arch);
141
Sean Callanan1a8d4092010-08-27 01:01:44 +0000142private:
Raphael Isemann74829732018-08-30 17:29:37 +0000143 /// Parses the expression.
144 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000145 /// \param[in] diagnostic_manager
Raphael Isemann74829732018-08-30 17:29:37 +0000146 /// The diagnostic manager that should receive the diagnostics
147 /// from the parsing process.
148 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000149 /// \param[in] completion
Raphael Isemann74829732018-08-30 17:29:37 +0000150 /// The completion consumer that should be used during parsing
151 /// (or a nullptr if no consumer should be attached).
152 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000153 /// \param[in] completion_line
Raphael Isemann74829732018-08-30 17:29:37 +0000154 /// The line in which the completion marker should be placed.
155 /// The first line is represented by the value 0.
156 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000157 /// \param[in] completion_column
Raphael Isemann74829732018-08-30 17:29:37 +0000158 /// The column in which the completion marker should be placed.
159 /// The first column is represented by the value 0.
160 ///
Adrian Prantlf05b42e2019-03-11 17:09:29 +0000161 /// \return
Raphael Isemann74829732018-08-30 17:29:37 +0000162 /// The number of parsing errors.
Raphael Isemann74829732018-08-30 17:29:37 +0000163 unsigned ParseInternal(DiagnosticManager &diagnostic_manager,
164 clang::CodeCompleteConsumer *completion = nullptr,
165 unsigned completion_line = 0,
166 unsigned completion_column = 0);
167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 std::unique_ptr<llvm::LLVMContext>
169 m_llvm_context; ///< The LLVM context to generate IR into
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 std::unique_ptr<clang::CompilerInstance>
171 m_compiler; ///< The Clang compiler used to parse expressions into IR
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172 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;
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000179
Raphael Isemann3ad82782019-09-11 14:33:11 +0000180 std::vector<std::string> m_include_directories;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000181};
Sean Callanan1a8d4092010-08-27 01:01:44 +0000182}
183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184#endif // liblldb_ClangExpressionParser_h_