1 //===-- ClangUserExpression.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_ClangUserExpression_h_ 11 #define liblldb_ClangUserExpression_h_ 12 13 // C Includes 14 // C++ Includes 15 #include <vector> 16 17 // Other libraries and framework includes 18 // Project includes 19 #include "ASTResultSynthesizer.h" 20 #include "ASTStructExtractor.h" 21 #include "ClangExpressionDeclMap.h" 22 #include "ClangExpressionHelper.h" 23 #include "ClangExpressionVariable.h" 24 #include "IRForTarget.h" 25 26 #include "lldb/Core/Address.h" 27 #include "lldb/Core/ClangForward.h" 28 #include "lldb/Expression/LLVMUserExpression.h" 29 #include "lldb/Expression/Materializer.h" 30 #include "lldb/Target/ExecutionContext.h" 31 #include "lldb/lldb-forward.h" 32 #include "lldb/lldb-private.h" 33 34 namespace lldb_private { 35 36 //---------------------------------------------------------------------- 37 /// @class ClangUserExpression ClangUserExpression.h 38 /// "lldb/Expression/ClangUserExpression.h" Encapsulates a single expression 39 /// for use with Clang 40 /// 41 /// LLDB uses expressions for various purposes, notably to call functions 42 /// and as a backend for the expr command. ClangUserExpression encapsulates 43 /// the objects needed to parse and interpret or JIT an expression. It uses 44 /// the Clang parser to produce LLVM IR from the expression. 45 //---------------------------------------------------------------------- 46 class ClangUserExpression : public LLVMUserExpression { 47 public: 48 enum { kDefaultTimeout = 500000u }; 49 50 class ClangUserExpressionHelper : public ClangExpressionHelper { 51 public: 52 ClangUserExpressionHelper(Target &target, bool top_level) 53 : m_target(target), m_top_level(top_level) {} 54 55 ~ClangUserExpressionHelper() override = default; 56 57 //------------------------------------------------------------------ 58 /// Return the object that the parser should use when resolving external 59 /// values. May be NULL if everything should be self-contained. 60 //------------------------------------------------------------------ 61 ClangExpressionDeclMap *DeclMap() override { 62 return m_expr_decl_map_up.get(); 63 } 64 65 void ResetDeclMap() { m_expr_decl_map_up.reset(); } 66 67 void ResetDeclMap(ExecutionContext &exe_ctx, 68 Materializer::PersistentVariableDelegate &result_delegate, 69 bool keep_result_in_memory); 70 71 //------------------------------------------------------------------ 72 /// Return the object that the parser should allow to access ASTs. May be 73 /// NULL if the ASTs do not need to be transformed. 74 /// 75 /// @param[in] passthrough 76 /// The ASTConsumer that the returned transformer should send 77 /// the ASTs to after transformation. 78 //------------------------------------------------------------------ 79 clang::ASTConsumer * 80 ASTTransformer(clang::ASTConsumer *passthrough) override; 81 82 void CommitPersistentDecls() override; 83 84 private: 85 Target &m_target; 86 std::unique_ptr<ClangExpressionDeclMap> m_expr_decl_map_up; 87 std::unique_ptr<ASTStructExtractor> m_struct_extractor_up; ///< The class 88 ///that generates 89 ///the argument 90 ///struct layout. 91 std::unique_ptr<ASTResultSynthesizer> m_result_synthesizer_up; 92 bool m_top_level; 93 }; 94 95 //------------------------------------------------------------------ 96 /// Constructor 97 /// 98 /// @param[in] expr 99 /// The expression to parse. 100 /// 101 /// @param[in] expr_prefix 102 /// If non-NULL, a C string containing translation-unit level 103 /// definitions to be included when the expression is parsed. 104 /// 105 /// @param[in] language 106 /// If not eLanguageTypeUnknown, a language to use when parsing 107 /// the expression. Currently restricted to those languages 108 /// supported by Clang. 109 /// 110 /// @param[in] desired_type 111 /// If not eResultTypeAny, the type to use for the expression 112 /// result. 113 //------------------------------------------------------------------ 114 ClangUserExpression(ExecutionContextScope &exe_scope, llvm::StringRef expr, 115 llvm::StringRef prefix, lldb::LanguageType language, 116 ResultType desired_type, 117 const EvaluateExpressionOptions &options); 118 119 ~ClangUserExpression() override; 120 121 //------------------------------------------------------------------ 122 /// Parse the expression 123 /// 124 /// @param[in] diagnostic_manager 125 /// A diagnostic manager to report parse errors and warnings to. 126 /// 127 /// @param[in] exe_ctx 128 /// The execution context to use when looking up entities that 129 /// are needed for parsing (locations of functions, types of 130 /// variables, persistent variables, etc.) 131 /// 132 /// @param[in] execution_policy 133 /// Determines whether interpretation is possible or mandatory. 134 /// 135 /// @param[in] keep_result_in_memory 136 /// True if the resulting persistent variable should reside in 137 /// target memory, if applicable. 138 /// 139 /// @return 140 /// True on success (no errors); false otherwise. 141 //------------------------------------------------------------------ 142 bool Parse(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx, 143 lldb_private::ExecutionPolicy execution_policy, 144 bool keep_result_in_memory, bool generate_debug_info) override; 145 146 bool Complete(ExecutionContext &exe_ctx, CompletionRequest &request, 147 unsigned complete_pos) override; 148 149 ExpressionTypeSystemHelper *GetTypeSystemHelper() override { 150 return &m_type_system_helper; 151 } 152 153 ClangExpressionDeclMap *DeclMap() { return m_type_system_helper.DeclMap(); } 154 155 void ResetDeclMap() { m_type_system_helper.ResetDeclMap(); } 156 157 void ResetDeclMap(ExecutionContext &exe_ctx, 158 Materializer::PersistentVariableDelegate &result_delegate, 159 bool keep_result_in_memory) { 160 m_type_system_helper.ResetDeclMap(exe_ctx, result_delegate, 161 keep_result_in_memory); 162 } 163 164 lldb::ExpressionVariableSP 165 GetResultAfterDematerialization(ExecutionContextScope *exe_scope) override; 166 167 private: 168 //------------------------------------------------------------------ 169 /// Populate m_in_cplusplus_method and m_in_objectivec_method based on the 170 /// environment. 171 //------------------------------------------------------------------ 172 173 void ScanContext(ExecutionContext &exe_ctx, 174 lldb_private::Status &err) override; 175 176 bool AddArguments(ExecutionContext &exe_ctx, std::vector<lldb::addr_t> &args, 177 lldb::addr_t struct_address, 178 DiagnosticManager &diagnostic_manager) override; 179 180 void UpdateLanguageForExpr(DiagnosticManager &diagnostic_manager, 181 ExecutionContext &exe_ctx); 182 bool SetupPersistentState(DiagnosticManager &diagnostic_manager, 183 ExecutionContext &exe_ctx); 184 bool PrepareForParsing(DiagnosticManager &diagnostic_manager, 185 ExecutionContext &exe_ctx); 186 187 ClangUserExpressionHelper m_type_system_helper; 188 189 class ResultDelegate : public Materializer::PersistentVariableDelegate { 190 public: 191 ResultDelegate(lldb::TargetSP target) : m_target_sp(target) {} 192 ConstString GetName() override; 193 void DidDematerialize(lldb::ExpressionVariableSP &variable) override; 194 195 void RegisterPersistentState(PersistentExpressionState *persistent_state); 196 lldb::ExpressionVariableSP &GetVariable(); 197 198 private: 199 PersistentExpressionState *m_persistent_state; 200 lldb::ExpressionVariableSP m_variable; 201 lldb::TargetSP m_target_sp; 202 }; 203 204 /// The language type of the current expression. 205 lldb::LanguageType m_expr_lang = lldb::eLanguageTypeUnknown; 206 207 /// The absolute character position in the transformed source code where the 208 /// user code (as typed by the user) starts. If the variable is empty, then we 209 /// were not able to calculate this position. 210 llvm::Optional<size_t> m_user_expression_start_pos; 211 ResultDelegate m_result_delegate; 212 }; 213 214 } // namespace lldb_private 215 216 #endif // liblldb_ClangUserExpression_h_ 217