| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 1 | //===-- ClangExpressionParser.h ---------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef liblldb_ClangExpressionParser_h_ |
| 11 | #define liblldb_ClangExpressionParser_h_ |
| 12 | |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 13 | #include "lldb/Core/ClangForward.h" |
| Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 14 | #include "lldb/Expression/DiagnosticManager.h" |
| Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 15 | #include "lldb/Expression/ExpressionParser.h" |
| Pavel Labath | 5f19b90 | 2017-11-13 16:16:33 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/ArchSpec.h" |
| Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 17 | #include "lldb/Utility/Status.h" |
| Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 18 | #include "lldb/lldb-public.h" |
| Sean Callanan | 4dbb271 | 2015-09-25 20:35:58 +0000 | [diff] [blame] | 19 | |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | namespace lldb_private { |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 24 | |
| Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 25 | class IRExecutionUnit; |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 26 | |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 27 | //---------------------------------------------------------------------- |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 28 | /// @class ClangExpressionParser ClangExpressionParser.h |
| Adrian Prantl | d8f460e | 2018-05-02 16:55:16 +0000 | [diff] [blame^] | 29 | /// "lldb/Expression/ClangExpressionParser.h" Encapsulates an instance of |
| 30 | /// Clang that can parse expressions. |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 31 | /// |
| 32 | /// ClangExpressionParser is responsible for preparing an instance of |
| 33 | /// ClangExpression for execution. ClangExpressionParser uses ClangExpression |
| 34 | /// as a glorified parameter list, performing the required parsing and |
| Adrian Prantl | d8f460e | 2018-05-02 16:55:16 +0000 | [diff] [blame^] | 35 | /// conversion to formats (DWARF bytecode, or JIT compiled machine code) that |
| 36 | /// can be executed. |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 37 | //---------------------------------------------------------------------- |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | class ClangExpressionParser : public ExpressionParser { |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 39 | public: |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | //------------------------------------------------------------------ |
| 41 | /// Constructor |
| 42 | /// |
| 43 | /// Initializes class variables. |
| 44 | /// |
| 45 | /// @param[in] exe_scope, |
| 46 | /// 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 | /// |
| 50 | /// @param[in] expr |
| 51 | /// The expression to be parsed. |
| 52 | //------------------------------------------------------------------ |
| 53 | ClangExpressionParser(ExecutionContextScope *exe_scope, Expression &expr, |
| 54 | bool generate_debug_info); |
| Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 55 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | //------------------------------------------------------------------ |
| 57 | /// Destructor |
| 58 | //------------------------------------------------------------------ |
| 59 | ~ClangExpressionParser() override; |
| Sagar Thakur | adc1abe | 2016-05-13 11:04:47 +0000 | [diff] [blame] | 60 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | //------------------------------------------------------------------ |
| Adrian Prantl | d8f460e | 2018-05-02 16:55:16 +0000 | [diff] [blame^] | 62 | /// Parse a single expression and convert it to IR using Clang. Don't wrap |
| 63 | /// the expression in anything at all. |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | /// |
| 65 | /// @param[in] diagnostic_manager |
| 66 | /// The diagnostic manager to report errors to. |
| 67 | /// |
| 68 | /// @return |
| 69 | /// The number of errors encountered during parsing. 0 means |
| 70 | /// success. |
| 71 | //------------------------------------------------------------------ |
| 72 | unsigned Parse(DiagnosticManager &diagnostic_manager) override; |
| 73 | |
| 74 | bool RewriteExpression(DiagnosticManager &diagnostic_manager) override; |
| 75 | |
| 76 | //------------------------------------------------------------------ |
| Adrian Prantl | d8f460e | 2018-05-02 16:55:16 +0000 | [diff] [blame^] | 77 | /// Ready an already-parsed expression for execution, possibly evaluating it |
| 78 | /// statically. |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | /// |
| 80 | /// @param[out] func_addr |
| 81 | /// The address to which the function has been written. |
| 82 | /// |
| 83 | /// @param[out] func_end |
| 84 | /// The end of the function's allocated memory region. (func_addr |
| 85 | /// and func_end do not delimit an allocated region; the allocated |
| 86 | /// region may begin before func_addr.) |
| 87 | /// |
| 88 | /// @param[in] execution_unit_sp |
| 89 | /// After parsing, ownership of the execution unit for |
| 90 | /// for the expression is handed to this shared pointer. |
| 91 | /// |
| 92 | /// @param[in] exe_ctx |
| 93 | /// The execution context to write the function into. |
| 94 | /// |
| 95 | /// @param[out] evaluated_statically |
| 96 | /// Set to true if the expression could be interpreted statically; |
| 97 | /// untouched otherwise. |
| 98 | /// |
| 99 | /// @param[out] const_result |
| 100 | /// If the result of the expression is constant, and the |
| 101 | /// expression has no side effects, this is set to the result of the |
| 102 | /// expression. |
| 103 | /// |
| 104 | /// @param[in] execution_policy |
| 105 | /// Determines whether the expression must be JIT-compiled, must be |
| 106 | /// evaluated statically, or whether this decision may be made |
| 107 | /// opportunistically. |
| 108 | /// |
| 109 | /// @return |
| 110 | /// An error code indicating the success or failure of the operation. |
| 111 | /// Test with Success(). |
| 112 | //------------------------------------------------------------------ |
| Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 113 | Status |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | PrepareForExecution(lldb::addr_t &func_addr, lldb::addr_t &func_end, |
| 115 | lldb::IRExecutionUnitSP &execution_unit_sp, |
| 116 | ExecutionContext &exe_ctx, bool &can_interpret, |
| 117 | lldb_private::ExecutionPolicy execution_policy) override; |
| 118 | |
| 119 | //------------------------------------------------------------------ |
| 120 | /// Run all static initializers for an execution unit. |
| 121 | /// |
| 122 | /// @param[in] execution_unit_sp |
| 123 | /// The execution unit. |
| 124 | /// |
| 125 | /// @param[in] exe_ctx |
| 126 | /// The execution context to use when running them. Thread can't be null. |
| 127 | /// |
| 128 | /// @return |
| 129 | /// The error code indicating the |
| 130 | //------------------------------------------------------------------ |
| Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 131 | Status RunStaticInitializers(lldb::IRExecutionUnitSP &execution_unit_sp, |
| 132 | ExecutionContext &exe_ctx); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 133 | |
| 134 | //------------------------------------------------------------------ |
| 135 | /// Returns a string representing current ABI. |
| 136 | /// |
| 137 | /// @param[in] target_arch |
| 138 | /// The target architecture. |
| 139 | /// |
| 140 | /// @return |
| 141 | /// A string representing target ABI for the current architecture. |
| 142 | //------------------------------------------------------------------- |
| 143 | std::string GetClangTargetABI(const ArchSpec &target_arch); |
| 144 | |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 145 | private: |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 146 | std::unique_ptr<llvm::LLVMContext> |
| 147 | m_llvm_context; ///< The LLVM context to generate IR into |
| 148 | std::unique_ptr<clang::FileManager> |
| 149 | m_file_manager; ///< The Clang file manager object used by the compiler |
| 150 | std::unique_ptr<clang::CompilerInstance> |
| 151 | m_compiler; ///< The Clang compiler used to parse expressions into IR |
| 152 | std::unique_ptr<clang::Builtin::Context> |
| 153 | m_builtin_context; ///< Context for Clang built-ins |
| 154 | std::unique_ptr<clang::SelectorTable> |
| 155 | m_selector_table; ///< Selector table for Objective-C methods |
| 156 | std::unique_ptr<clang::CodeGenerator> |
| 157 | m_code_generator; ///< The Clang object that generates IR |
| 158 | |
| 159 | class LLDBPreprocessorCallbacks; |
| 160 | LLDBPreprocessorCallbacks *m_pp_callbacks; ///< Called when the preprocessor |
| 161 | ///encounters module imports |
| 162 | std::unique_ptr<ClangASTContext> m_ast_context; |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 163 | }; |
| Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 166 | #endif // liblldb_ClangExpressionParser_h_ |