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 13 #include "lldb/Core/ClangForward.h" 14 #include "lldb/Expression/DiagnosticManager.h" 15 #include "lldb/Expression/ExpressionParser.h" 16 #include "lldb/Utility/ArchSpec.h" 17 #include "lldb/Utility/Status.h" 18 #include "lldb/lldb-public.h" 19 20 #include <string> 21 #include <vector> 22 23 namespace clang { 24 class CodeCompleteConsumer; 25 } 26 27 namespace lldb_private { 28 29 class IRExecutionUnit; 30 31 //---------------------------------------------------------------------- 32 /// @class ClangExpressionParser ClangExpressionParser.h 33 /// "lldb/Expression/ClangExpressionParser.h" Encapsulates an instance of 34 /// Clang that can parse expressions. 35 /// 36 /// ClangExpressionParser is responsible for preparing an instance of 37 /// ClangExpression for execution. ClangExpressionParser uses ClangExpression 38 /// as a glorified parameter list, performing the required parsing and 39 /// conversion to formats (DWARF bytecode, or JIT compiled machine code) that 40 /// can be executed. 41 //---------------------------------------------------------------------- 42 class ClangExpressionParser : public ExpressionParser { 43 public: 44 //------------------------------------------------------------------ 45 /// Constructor 46 /// 47 /// Initializes class variables. 48 /// 49 /// @param[in] exe_scope, 50 /// If non-NULL, an execution context scope that can help to 51 /// correctly create an expression with a valid process for 52 /// optional tuning Objective-C runtime support. Can be NULL. 53 /// 54 /// @param[in] expr 55 /// The expression to be parsed. 56 //------------------------------------------------------------------ 57 ClangExpressionParser(ExecutionContextScope *exe_scope, Expression &expr, 58 bool generate_debug_info); 59 60 //------------------------------------------------------------------ 61 /// Destructor 62 //------------------------------------------------------------------ 63 ~ClangExpressionParser() override; 64 65 bool Complete(CompletionRequest &request, unsigned line, unsigned pos, 66 unsigned typed_pos) override; 67 68 //------------------------------------------------------------------ 69 /// Parse a single expression and convert it to IR using Clang. Don't wrap 70 /// the expression in anything at all. 71 /// 72 /// @param[in] diagnostic_manager 73 /// The diagnostic manager to report errors to. 74 /// 75 /// @return 76 /// The number of errors encountered during parsing. 0 means 77 /// success. 78 //------------------------------------------------------------------ 79 unsigned Parse(DiagnosticManager &diagnostic_manager) override; 80 81 bool RewriteExpression(DiagnosticManager &diagnostic_manager) override; 82 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 //------------------------------------------------------------------ 120 Status 121 PrepareForExecution(lldb::addr_t &func_addr, lldb::addr_t &func_end, 122 lldb::IRExecutionUnitSP &execution_unit_sp, 123 ExecutionContext &exe_ctx, bool &can_interpret, 124 lldb_private::ExecutionPolicy execution_policy) override; 125 126 //------------------------------------------------------------------ 127 /// Run all static initializers for an execution unit. 128 /// 129 /// @param[in] execution_unit_sp 130 /// The execution unit. 131 /// 132 /// @param[in] exe_ctx 133 /// The execution context to use when running them. Thread can't be null. 134 /// 135 /// @return 136 /// The error code indicating the 137 //------------------------------------------------------------------ 138 Status RunStaticInitializers(lldb::IRExecutionUnitSP &execution_unit_sp, 139 ExecutionContext &exe_ctx); 140 141 //------------------------------------------------------------------ 142 /// Returns a string representing current ABI. 143 /// 144 /// @param[in] target_arch 145 /// The target architecture. 146 /// 147 /// @return 148 /// A string representing target ABI for the current architecture. 149 //------------------------------------------------------------------- 150 std::string GetClangTargetABI(const ArchSpec &target_arch); 151 152 private: 153 //------------------------------------------------------------------ 154 /// Parses the expression. 155 /// 156 /// @param[in] diagnostic_manager 157 /// The diagnostic manager that should receive the diagnostics 158 /// from the parsing process. 159 /// 160 /// @param[in] completion 161 /// The completion consumer that should be used during parsing 162 /// (or a nullptr if no consumer should be attached). 163 /// 164 /// @param[in] completion_line 165 /// The line in which the completion marker should be placed. 166 /// The first line is represented by the value 0. 167 /// 168 /// @param[in] completion_column 169 /// The column in which the completion marker should be placed. 170 /// The first column is represented by the value 0. 171 /// 172 /// @return 173 /// The number of parsing errors. 174 //------------------------------------------------------------------- 175 unsigned ParseInternal(DiagnosticManager &diagnostic_manager, 176 clang::CodeCompleteConsumer *completion = nullptr, 177 unsigned completion_line = 0, 178 unsigned completion_column = 0); 179 180 std::unique_ptr<llvm::LLVMContext> 181 m_llvm_context; ///< The LLVM context to generate IR into 182 std::unique_ptr<clang::FileManager> 183 m_file_manager; ///< The Clang file manager object used by the compiler 184 std::unique_ptr<clang::CompilerInstance> 185 m_compiler; ///< The Clang compiler used to parse expressions into IR 186 std::unique_ptr<clang::CodeGenerator> 187 m_code_generator; ///< The Clang object that generates IR 188 189 class LLDBPreprocessorCallbacks; 190 LLDBPreprocessorCallbacks *m_pp_callbacks; ///< Called when the preprocessor 191 ///encounters module imports 192 std::unique_ptr<ClangASTContext> m_ast_context; 193 }; 194 } 195 196 #endif // liblldb_ClangExpressionParser_h_ 197