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