| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 1 | //===-- ClangExpressionParser.h ---------------------------------*- C++ -*-===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef liblldb_ClangExpressionParser_h_ |
| 10 | #define liblldb_ClangExpressionParser_h_ |
| 11 | |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 12 | #include "lldb/Core/ClangForward.h" |
| Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 13 | #include "lldb/Expression/DiagnosticManager.h" |
| Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 14 | #include "lldb/Expression/ExpressionParser.h" |
| Pavel Labath | 5f19b90 | 2017-11-13 16:16:33 +0000 | [diff] [blame] | 15 | #include "lldb/Utility/ArchSpec.h" |
| Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/Status.h" |
| Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 17 | #include "lldb/lldb-public.h" |
| Sean Callanan | 4dbb271 | 2015-09-25 20:35:58 +0000 | [diff] [blame] | 18 | |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 22 | namespace clang { |
| 23 | class CodeCompleteConsumer; |
| 24 | } |
| 25 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 26 | namespace lldb_private { |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 27 | |
| Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 28 | class IRExecutionUnit; |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 30 | //---------------------------------------------------------------------- |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 31 | /// \class ClangExpressionParser ClangExpressionParser.h |
| Adrian Prantl | d8f460e | 2018-05-02 16:55:16 +0000 | [diff] [blame] | 32 | /// "lldb/Expression/ClangExpressionParser.h" Encapsulates an instance of |
| 33 | /// Clang that can parse expressions. |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 34 | /// |
| 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 Prantl | d8f460e | 2018-05-02 16:55:16 +0000 | [diff] [blame] | 38 | /// conversion to formats (DWARF bytecode, or JIT compiled machine code) that |
| 39 | /// can be executed. |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 40 | //---------------------------------------------------------------------- |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | class ClangExpressionParser : public ExpressionParser { |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 42 | public: |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | //------------------------------------------------------------------ |
| 44 | /// Constructor |
| 45 | /// |
| 46 | /// Initializes class variables. |
| 47 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 48 | /// \param[in] exe_scope, |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | /// 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 Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 53 | /// \param[in] expr |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | /// The expression to be parsed. |
| Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame^] | 55 | /// |
| 56 | /// @param[in] include_directories |
| 57 | /// List of include directories that should be used when parsing the |
| 58 | /// expression. |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | //------------------------------------------------------------------ |
| 60 | ClangExpressionParser(ExecutionContextScope *exe_scope, Expression &expr, |
| Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame^] | 61 | bool generate_debug_info, |
| 62 | std::vector<ConstString> include_directories = {}); |
| Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 63 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | //------------------------------------------------------------------ |
| 65 | /// Destructor |
| 66 | //------------------------------------------------------------------ |
| 67 | ~ClangExpressionParser() override; |
| Sagar Thakur | adc1abe | 2016-05-13 11:04:47 +0000 | [diff] [blame] | 68 | |
| Raphael Isemann | c11a780 | 2018-08-30 21:26:32 +0000 | [diff] [blame] | 69 | bool Complete(CompletionRequest &request, unsigned line, unsigned pos, |
| Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 70 | unsigned typed_pos) override; |
| 71 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | //------------------------------------------------------------------ |
| Adrian Prantl | d8f460e | 2018-05-02 16:55:16 +0000 | [diff] [blame] | 73 | /// Parse a single expression and convert it to IR using Clang. Don't wrap |
| 74 | /// the expression in anything at all. |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 76 | /// \param[in] diagnostic_manager |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 77 | /// The diagnostic manager to report errors to. |
| 78 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 79 | /// \return |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | /// 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 Prantl | d8f460e | 2018-05-02 16:55:16 +0000 | [diff] [blame] | 88 | /// Ready an already-parsed expression for execution, possibly evaluating it |
| 89 | /// statically. |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 91 | /// \param[out] func_addr |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 92 | /// The address to which the function has been written. |
| 93 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 94 | /// \param[out] func_end |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 95 | /// 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 Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 99 | /// \param[in] execution_unit_sp |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 100 | /// After parsing, ownership of the execution unit for |
| 101 | /// for the expression is handed to this shared pointer. |
| 102 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 103 | /// \param[in] exe_ctx |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | /// The execution context to write the function into. |
| 105 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 106 | /// \param[out] evaluated_statically |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | /// Set to true if the expression could be interpreted statically; |
| 108 | /// untouched otherwise. |
| 109 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 110 | /// \param[out] const_result |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | /// 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 Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 115 | /// \param[in] execution_policy |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | /// Determines whether the expression must be JIT-compiled, must be |
| 117 | /// evaluated statically, or whether this decision may be made |
| 118 | /// opportunistically. |
| 119 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 120 | /// \return |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | /// An error code indicating the success or failure of the operation. |
| 122 | /// Test with Success(). |
| 123 | //------------------------------------------------------------------ |
| Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 124 | Status |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | 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 Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 133 | /// \param[in] execution_unit_sp |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 134 | /// The execution unit. |
| 135 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 136 | /// \param[in] exe_ctx |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | /// The execution context to use when running them. Thread can't be null. |
| 138 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 139 | /// \return |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 140 | /// The error code indicating the |
| 141 | //------------------------------------------------------------------ |
| Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 142 | Status RunStaticInitializers(lldb::IRExecutionUnitSP &execution_unit_sp, |
| 143 | ExecutionContext &exe_ctx); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 144 | |
| 145 | //------------------------------------------------------------------ |
| 146 | /// Returns a string representing current ABI. |
| 147 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 148 | /// \param[in] target_arch |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 149 | /// The target architecture. |
| 150 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 151 | /// \return |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 152 | /// A string representing target ABI for the current architecture. |
| 153 | //------------------------------------------------------------------- |
| 154 | std::string GetClangTargetABI(const ArchSpec &target_arch); |
| 155 | |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 156 | private: |
| Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 157 | //------------------------------------------------------------------ |
| 158 | /// Parses the expression. |
| 159 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 160 | /// \param[in] diagnostic_manager |
| Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 161 | /// The diagnostic manager that should receive the diagnostics |
| 162 | /// from the parsing process. |
| 163 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 164 | /// \param[in] completion |
| Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 165 | /// The completion consumer that should be used during parsing |
| 166 | /// (or a nullptr if no consumer should be attached). |
| 167 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 168 | /// \param[in] completion_line |
| Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 169 | /// The line in which the completion marker should be placed. |
| 170 | /// The first line is represented by the value 0. |
| 171 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 172 | /// \param[in] completion_column |
| Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 173 | /// The column in which the completion marker should be placed. |
| 174 | /// The first column is represented by the value 0. |
| 175 | /// |
| Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 176 | /// \return |
| Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 177 | /// 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 Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 184 | std::unique_ptr<llvm::LLVMContext> |
| 185 | m_llvm_context; ///< The LLVM context to generate IR into |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 186 | std::unique_ptr<clang::CompilerInstance> |
| 187 | m_compiler; ///< The Clang compiler used to parse expressions into IR |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 188 | 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 Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame^] | 195 | |
| 196 | std::vector<ConstString> m_include_directories; |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 197 | }; |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 200 | #endif // liblldb_ClangExpressionParser_h_ |