1 //===-- ClangExpressionParser.h ---------------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef liblldb_ClangExpressionParser_h_ 10 #define liblldb_ClangExpressionParser_h_ 11 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" 17 18 #include <string> 19 #include <vector> 20 21 namespace llvm { 22 class LLVMContext; 23 } 24 25 namespace clang { 26 class CodeGenerator; 27 class CodeCompleteConsumer; 28 class CompilerInstance; 29 } // namespace clang 30 31 namespace lldb_private { 32 33 class IRExecutionUnit; 34 35 /// \class ClangExpressionParser ClangExpressionParser.h 36 /// "lldb/Expression/ClangExpressionParser.h" Encapsulates an instance of 37 /// Clang that can parse expressions. 38 /// 39 /// ClangExpressionParser is responsible for preparing an instance of 40 /// ClangExpression for execution. ClangExpressionParser uses ClangExpression 41 /// as a glorified parameter list, performing the required parsing and 42 /// conversion to formats (DWARF bytecode, or JIT compiled machine code) that 43 /// can be executed. 44 class ClangExpressionParser : public ExpressionParser { 45 public: 46 /// Constructor 47 /// 48 /// Initializes class variables. 49 /// 50 /// \param[in] exe_scope 51 /// If non-NULL, an execution context scope that can help to 52 /// correctly create an expression with a valid process for 53 /// optional tuning Objective-C runtime support. Can be NULL. 54 /// 55 /// \param[in] expr 56 /// The expression to be parsed. 57 /// 58 /// @param[in] include_directories 59 /// List of include directories that should be used when parsing the 60 /// expression. 61 /// 62 /// @param[in] filename 63 /// Name of the source file that should be used when rendering 64 /// diagnostics (i.e. errors, warnings or notes from Clang). 65 ClangExpressionParser(ExecutionContextScope *exe_scope, Expression &expr, 66 bool generate_debug_info, 67 std::vector<std::string> include_directories = {}, 68 std::string filename = "<clang expression>"); 69 70 /// Destructor 71 ~ClangExpressionParser() override; 72 73 bool Complete(CompletionRequest &request, unsigned line, unsigned pos, 74 unsigned typed_pos) override; 75 76 /// Parse a single expression and convert it to IR using Clang. Don't wrap 77 /// the expression in anything at all. 78 /// 79 /// \param[in] diagnostic_manager 80 /// The diagnostic manager to report errors to. 81 /// 82 /// \return 83 /// The number of errors encountered during parsing. 0 means 84 /// success. 85 unsigned Parse(DiagnosticManager &diagnostic_manager); 86 87 bool RewriteExpression(DiagnosticManager &diagnostic_manager) override; 88 89 /// Ready an already-parsed expression for execution, possibly evaluating it 90 /// statically. 91 /// 92 /// \param[out] func_addr 93 /// The address to which the function has been written. 94 /// 95 /// \param[out] func_end 96 /// The end of the function's allocated memory region. (func_addr 97 /// and func_end do not delimit an allocated region; the allocated 98 /// region may begin before func_addr.) 99 /// 100 /// \param[in] execution_unit_sp 101 /// After parsing, ownership of the execution unit for 102 /// for the expression is handed to this shared pointer. 103 /// 104 /// \param[in] exe_ctx 105 /// The execution context to write the function into. 106 /// 107 /// \param[in] execution_policy 108 /// Determines whether the expression must be JIT-compiled, must be 109 /// evaluated statically, or whether this decision may be made 110 /// opportunistically. 111 /// 112 /// \return 113 /// An error code indicating the success or failure of the operation. 114 /// Test with Success(). 115 Status 116 PrepareForExecution(lldb::addr_t &func_addr, lldb::addr_t &func_end, 117 lldb::IRExecutionUnitSP &execution_unit_sp, 118 ExecutionContext &exe_ctx, bool &can_interpret, 119 lldb_private::ExecutionPolicy execution_policy) override; 120 121 /// Run all static initializers for an execution unit. 122 /// 123 /// \param[in] execution_unit_sp 124 /// The execution unit. 125 /// 126 /// \param[in] exe_ctx 127 /// The execution context to use when running them. Thread can't be null. 128 /// 129 /// \return 130 /// The error code indicating the 131 Status RunStaticInitializers(lldb::IRExecutionUnitSP &execution_unit_sp, 132 ExecutionContext &exe_ctx); 133 134 /// Returns a string representing current ABI. 135 /// 136 /// \param[in] target_arch 137 /// The target architecture. 138 /// 139 /// \return 140 /// A string representing target ABI for the current architecture. 141 std::string GetClangTargetABI(const ArchSpec &target_arch); 142 143 private: 144 /// Parses the expression. 145 /// 146 /// \param[in] diagnostic_manager 147 /// The diagnostic manager that should receive the diagnostics 148 /// from the parsing process. 149 /// 150 /// \param[in] completion 151 /// The completion consumer that should be used during parsing 152 /// (or a nullptr if no consumer should be attached). 153 /// 154 /// \param[in] completion_line 155 /// The line in which the completion marker should be placed. 156 /// The first line is represented by the value 0. 157 /// 158 /// \param[in] completion_column 159 /// The column in which the completion marker should be placed. 160 /// The first column is represented by the value 0. 161 /// 162 /// \return 163 /// The number of parsing errors. 164 unsigned ParseInternal(DiagnosticManager &diagnostic_manager, 165 clang::CodeCompleteConsumer *completion = nullptr, 166 unsigned completion_line = 0, 167 unsigned completion_column = 0); 168 169 std::unique_ptr<llvm::LLVMContext> 170 m_llvm_context; ///< The LLVM context to generate IR into 171 std::unique_ptr<clang::CompilerInstance> 172 m_compiler; ///< The Clang compiler used to parse expressions into IR 173 std::unique_ptr<clang::CodeGenerator> 174 m_code_generator; ///< The Clang object that generates IR 175 176 class LLDBPreprocessorCallbacks; 177 LLDBPreprocessorCallbacks *m_pp_callbacks; ///< Called when the preprocessor 178 ///encounters module imports 179 std::unique_ptr<TypeSystemClang> m_ast_context; 180 181 std::vector<std::string> m_include_directories; 182 /// File name used for the user expression. 183 std::string m_filename; 184 }; 185 } 186 187 #endif // liblldb_ClangExpressionParser_h_ 188