ClangExpressionParser.h revision 344779
1232366Sdavide//===-- ClangExpressionParser.h ---------------------------------*- C++ -*-===// 2232366Sdavide// 3232366Sdavide// The LLVM Compiler Infrastructure 4232366Sdavide// 5232366Sdavide// This file is distributed under the University of Illinois Open Source 6232366Sdavide// License. See LICENSE.TXT for details. 7232366Sdavide// 8232366Sdavide//===----------------------------------------------------------------------===// 9232366Sdavide 10232366Sdavide#ifndef liblldb_ClangExpressionParser_h_ 11232366Sdavide#define liblldb_ClangExpressionParser_h_ 12232366Sdavide 13232366Sdavide#include "lldb/Core/ClangForward.h" 14232366Sdavide#include "lldb/Expression/DiagnosticManager.h" 15232366Sdavide#include "lldb/Expression/ExpressionParser.h" 16232366Sdavide#include "lldb/Utility/ArchSpec.h" 17232366Sdavide#include "lldb/Utility/Status.h" 18232366Sdavide#include "lldb/lldb-public.h" 19232366Sdavide 20232366Sdavide#include <string> 21232366Sdavide#include <vector> 22232366Sdavide 23232366Sdavidenamespace clang { 24232366Sdavideclass CodeCompleteConsumer; 25232366Sdavide} 26232366Sdavide 27241741Ssbrunonamespace lldb_private { 28232377Spluknet 29232377Spluknetclass IRExecutionUnit; 30232366Sdavide 31232366Sdavide//---------------------------------------------------------------------- 32232377Spluknet/// @class ClangExpressionParser ClangExpressionParser.h 33232366Sdavide/// "lldb/Expression/ClangExpressionParser.h" Encapsulates an instance of 34232366Sdavide/// Clang that can parse expressions. 35232366Sdavide/// 36232366Sdavide/// ClangExpressionParser is responsible for preparing an instance of 37232366Sdavide/// ClangExpression for execution. ClangExpressionParser uses ClangExpression 38232366Sdavide/// as a glorified parameter list, performing the required parsing and 39232366Sdavide/// conversion to formats (DWARF bytecode, or JIT compiled machine code) that 40232366Sdavide/// can be executed. 41232366Sdavide//---------------------------------------------------------------------- 42232366Sdavideclass ClangExpressionParser : public ExpressionParser { 43232366Sdavidepublic: 44232366Sdavide //------------------------------------------------------------------ 45232366Sdavide /// Constructor 46232366Sdavide /// 47232366Sdavide /// Initializes class variables. 48232366Sdavide /// 49232366Sdavide /// @param[in] exe_scope, 50232366Sdavide /// If non-NULL, an execution context scope that can help to 51232366Sdavide /// correctly create an expression with a valid process for 52232366Sdavide /// optional tuning Objective-C runtime support. Can be NULL. 53232366Sdavide /// 54232366Sdavide /// @param[in] expr 55232366Sdavide /// The expression to be parsed. 56232366Sdavide //------------------------------------------------------------------ 57232366Sdavide ClangExpressionParser(ExecutionContextScope *exe_scope, Expression &expr, 58232366Sdavide bool generate_debug_info); 59232377Spluknet 60232366Sdavide //------------------------------------------------------------------ 61232366Sdavide /// Destructor 62232377Spluknet //------------------------------------------------------------------ 63232366Sdavide ~ClangExpressionParser() override; 64232366Sdavide 65232366Sdavide bool Complete(CompletionRequest &request, unsigned line, unsigned pos, 66232366Sdavide unsigned typed_pos) override; 67232366Sdavide 68232366Sdavide //------------------------------------------------------------------ 69232366Sdavide /// Parse a single expression and convert it to IR using Clang. Don't wrap 70232366Sdavide /// the expression in anything at all. 71232366Sdavide /// 72232366Sdavide /// @param[in] diagnostic_manager 73232366Sdavide /// The diagnostic manager to report errors to. 74232366Sdavide /// 75232366Sdavide /// @return 76232366Sdavide /// The number of errors encountered during parsing. 0 means 77232366Sdavide /// success. 78232377Spluknet //------------------------------------------------------------------ 79232366Sdavide unsigned Parse(DiagnosticManager &diagnostic_manager) override; 80232366Sdavide 81232366Sdavide bool RewriteExpression(DiagnosticManager &diagnostic_manager) override; 82232366Sdavide 83232366Sdavide //------------------------------------------------------------------ 84232366Sdavide /// Ready an already-parsed expression for execution, possibly evaluating it 85232366Sdavide /// statically. 86232366Sdavide /// 87232366Sdavide /// @param[out] func_addr 88232366Sdavide /// The address to which the function has been written. 89232366Sdavide /// 90232366Sdavide /// @param[out] func_end 91232366Sdavide /// The end of the function's allocated memory region. (func_addr 92232366Sdavide /// and func_end do not delimit an allocated region; the allocated 93232366Sdavide /// region may begin before func_addr.) 94232366Sdavide /// 95232366Sdavide /// @param[in] execution_unit_sp 96240164Sfabient /// After parsing, ownership of the execution unit for 97240164Sfabient /// for the expression is handed to this shared pointer. 98240164Sfabient /// 99240164Sfabient /// @param[in] exe_ctx 100240164Sfabient /// The execution context to write the function into. 101240164Sfabient /// 102240164Sfabient /// @param[out] evaluated_statically 103240164Sfabient /// Set to true if the expression could be interpreted statically; 104240164Sfabient /// untouched otherwise. 105240164Sfabient /// 106232366Sdavide /// @param[out] const_result 107240164Sfabient /// If the result of the expression is constant, and the 108232366Sdavide /// expression has no side effects, this is set to the result of the 109240164Sfabient /// expression. 110232366Sdavide /// 111240164Sfabient /// @param[in] execution_policy 112232366Sdavide /// Determines whether the expression must be JIT-compiled, must be 113240164Sfabient /// evaluated statically, or whether this decision may be made 114232366Sdavide /// opportunistically. 115240164Sfabient /// 116240164Sfabient /// @return 117240164Sfabient /// An error code indicating the success or failure of the operation. 118240164Sfabient /// Test with Success(). 119240164Sfabient //------------------------------------------------------------------ 120240164Sfabient Status 121240164Sfabient PrepareForExecution(lldb::addr_t &func_addr, lldb::addr_t &func_end, 122240164Sfabient lldb::IRExecutionUnitSP &execution_unit_sp, 123240164Sfabient ExecutionContext &exe_ctx, bool &can_interpret, 124240164Sfabient lldb_private::ExecutionPolicy execution_policy) override; 125240164Sfabient 126240164Sfabient //------------------------------------------------------------------ 127240164Sfabient /// Run all static initializers for an execution unit. 128240164Sfabient /// 129240164Sfabient /// @param[in] execution_unit_sp 130240164Sfabient /// The execution unit. 131240164Sfabient /// 132240164Sfabient /// @param[in] exe_ctx 133240164Sfabient /// The execution context to use when running them. Thread can't be null. 134240164Sfabient /// 135240164Sfabient /// @return 136240164Sfabient /// The error code indicating the 137240164Sfabient //------------------------------------------------------------------ 138240164Sfabient Status RunStaticInitializers(lldb::IRExecutionUnitSP &execution_unit_sp, 139240164Sfabient ExecutionContext &exe_ctx); 140240164Sfabient 141241974Ssbruno //------------------------------------------------------------------ 142240164Sfabient /// Returns a string representing current ABI. 143240164Sfabient /// 144240164Sfabient /// @param[in] target_arch 145240164Sfabient /// The target architecture. 146240164Sfabient /// 147240164Sfabient /// @return 148240164Sfabient /// A string representing target ABI for the current architecture. 149240164Sfabient //------------------------------------------------------------------- 150240164Sfabient std::string GetClangTargetABI(const ArchSpec &target_arch); 151240164Sfabient 152240164Sfabientprivate: 153240164Sfabient //------------------------------------------------------------------ 154240164Sfabient /// Parses the expression. 155240164Sfabient /// 156240164Sfabient /// @param[in] diagnostic_manager 157240164Sfabient /// The diagnostic manager that should receive the diagnostics 158240164Sfabient /// from the parsing process. 159240164Sfabient /// 160240164Sfabient /// @param[in] completion 161240164Sfabient /// The completion consumer that should be used during parsing 162240164Sfabient /// (or a nullptr if no consumer should be attached). 163240164Sfabient /// 164240164Sfabient /// @param[in] completion_line 165240164Sfabient /// The line in which the completion marker should be placed. 166240164Sfabient /// The first line is represented by the value 0. 167240164Sfabient /// 168240164Sfabient /// @param[in] completion_column 169240164Sfabient /// The column in which the completion marker should be placed. 170232366Sdavide /// The first column is represented by the value 0. 171232366Sdavide /// 172232366Sdavide /// @return 173232366Sdavide /// The number of parsing errors. 174232366Sdavide //------------------------------------------------------------------- 175232366Sdavide unsigned ParseInternal(DiagnosticManager &diagnostic_manager, 176232366Sdavide clang::CodeCompleteConsumer *completion = nullptr, 177232366Sdavide unsigned completion_line = 0, 178232366Sdavide unsigned completion_column = 0); 179232366Sdavide 180232366Sdavide std::unique_ptr<llvm::LLVMContext> 181232366Sdavide m_llvm_context; ///< The LLVM context to generate IR into 182232366Sdavide std::unique_ptr<clang::FileManager> 183232366Sdavide m_file_manager; ///< The Clang file manager object used by the compiler 184232366Sdavide std::unique_ptr<clang::CompilerInstance> 185232366Sdavide m_compiler; ///< The Clang compiler used to parse expressions into IR 186232366Sdavide std::unique_ptr<clang::CodeGenerator> 187232366Sdavide m_code_generator; ///< The Clang object that generates IR 188232366Sdavide 189232366Sdavide class LLDBPreprocessorCallbacks; 190232366Sdavide LLDBPreprocessorCallbacks *m_pp_callbacks; ///< Called when the preprocessor 191232366Sdavide ///encounters module imports 192232366Sdavide std::unique_ptr<ClangASTContext> m_ast_context; 193232366Sdavide}; 194232366Sdavide} 195232366Sdavide 196232366Sdavide#endif // liblldb_ClangExpressionParser_h_ 197232366Sdavide