1 //===-- ClangUserExpression.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 LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGUSEREXPRESSION_H 10 #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGUSEREXPRESSION_H 11 12 #include <vector> 13 14 #include "ASTResultSynthesizer.h" 15 #include "ASTStructExtractor.h" 16 #include "ClangExpressionDeclMap.h" 17 #include "ClangExpressionHelper.h" 18 #include "ClangExpressionSourceCode.h" 19 #include "ClangExpressionVariable.h" 20 #include "IRForTarget.h" 21 22 #include "lldb/Core/Address.h" 23 #include "lldb/Expression/LLVMUserExpression.h" 24 #include "lldb/Expression/Materializer.h" 25 #include "lldb/Target/ExecutionContext.h" 26 #include "lldb/lldb-forward.h" 27 #include "lldb/lldb-private.h" 28 29 namespace lldb_private { 30 31 class ClangExpressionParser; 32 33 /// \class ClangUserExpression ClangUserExpression.h 34 /// "lldb/Expression/ClangUserExpression.h" Encapsulates a single expression 35 /// for use with Clang 36 /// 37 /// LLDB uses expressions for various purposes, notably to call functions 38 /// and as a backend for the expr command. ClangUserExpression encapsulates 39 /// the objects needed to parse and interpret or JIT an expression. It uses 40 /// the Clang parser to produce LLVM IR from the expression. 41 class ClangUserExpression : public LLVMUserExpression { 42 // LLVM RTTI support 43 static char ID; 44 45 public: 46 bool isA(const void *ClassID) const override { 47 return ClassID == &ID || LLVMUserExpression::isA(ClassID); 48 } 49 static bool classof(const Expression *obj) { return obj->isA(&ID); } 50 51 enum { kDefaultTimeout = 500000u }; 52 53 class ClangUserExpressionHelper : public ClangExpressionHelper { 54 public: 55 ClangUserExpressionHelper(Target &target, bool top_level) 56 : m_target(target), m_top_level(top_level) {} 57 58 ~ClangUserExpressionHelper() override = default; 59 60 /// Return the object that the parser should use when resolving external 61 /// values. May be NULL if everything should be self-contained. 62 ClangExpressionDeclMap *DeclMap() override { 63 return m_expr_decl_map_up.get(); 64 } 65 66 void ResetDeclMap() { m_expr_decl_map_up.reset(); } 67 68 void ResetDeclMap(ExecutionContext &exe_ctx, 69 Materializer::PersistentVariableDelegate &result_delegate, 70 bool keep_result_in_memory, 71 ValueObject *ctx_obj); 72 73 /// Return the object that the parser should allow to access ASTs. May be 74 /// NULL if the ASTs do not need to be transformed. 75 /// 76 /// \param[in] passthrough 77 /// The ASTConsumer that the returned transformer should send 78 /// the ASTs to after transformation. 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 /// Constructor 96 /// 97 /// \param[in] expr 98 /// The expression to parse. 99 /// 100 /// \param[in] prefix 101 /// If non-NULL, a C string containing translation-unit level 102 /// definitions to be included when the expression is parsed. 103 /// 104 /// \param[in] language 105 /// If not eLanguageTypeUnknown, a language to use when parsing 106 /// the expression. Currently restricted to those languages 107 /// supported by Clang. 108 /// 109 /// \param[in] desired_type 110 /// If not eResultTypeAny, the type to use for the expression 111 /// result. 112 /// 113 /// \param[in] options 114 /// Additional options for the expression. 115 /// 116 /// \param[in] ctx_obj 117 /// The object (if any) in which context the expression 118 /// must be evaluated. For details see the comment to 119 /// `UserExpression::Evaluate`. 120 ClangUserExpression(ExecutionContextScope &exe_scope, llvm::StringRef expr, 121 llvm::StringRef prefix, lldb::LanguageType language, 122 ResultType desired_type, 123 const EvaluateExpressionOptions &options, 124 ValueObject *ctx_obj); 125 126 ~ClangUserExpression() override; 127 128 /// Parse the expression 129 /// 130 /// \param[in] diagnostic_manager 131 /// A diagnostic manager to report parse errors and warnings to. 132 /// 133 /// \param[in] exe_ctx 134 /// The execution context to use when looking up entities that 135 /// are needed for parsing (locations of functions, types of 136 /// variables, persistent variables, etc.) 137 /// 138 /// \param[in] execution_policy 139 /// Determines whether interpretation is possible or mandatory. 140 /// 141 /// \param[in] keep_result_in_memory 142 /// True if the resulting persistent variable should reside in 143 /// target memory, if applicable. 144 /// 145 /// \return 146 /// True on success (no errors); false otherwise. 147 bool Parse(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx, 148 lldb_private::ExecutionPolicy execution_policy, 149 bool keep_result_in_memory, bool generate_debug_info) override; 150 151 bool Complete(ExecutionContext &exe_ctx, CompletionRequest &request, 152 unsigned complete_pos) override; 153 154 ExpressionTypeSystemHelper *GetTypeSystemHelper() override { 155 return &m_type_system_helper; 156 } 157 158 ClangExpressionDeclMap *DeclMap() { return m_type_system_helper.DeclMap(); } 159 160 void ResetDeclMap() { m_type_system_helper.ResetDeclMap(); } 161 162 void ResetDeclMap(ExecutionContext &exe_ctx, 163 Materializer::PersistentVariableDelegate &result_delegate, 164 bool keep_result_in_memory) { 165 m_type_system_helper.ResetDeclMap(exe_ctx, result_delegate, 166 keep_result_in_memory, 167 m_ctx_obj); 168 } 169 170 lldb::ExpressionVariableSP 171 GetResultAfterDematerialization(ExecutionContextScope *exe_scope) override; 172 173 /// Returns true iff this expression is using any imported C++ modules. 174 bool DidImportCxxModules() const { return !m_imported_cpp_modules.empty(); } 175 176 private: 177 /// Populate m_in_cplusplus_method and m_in_objectivec_method based on the 178 /// environment. 179 180 /// Contains the actual parsing implementation. 181 /// The parameter have the same meaning as in ClangUserExpression::Parse. 182 /// \see ClangUserExpression::Parse 183 bool TryParse(DiagnosticManager &diagnostic_manager, 184 ExecutionContextScope *exe_scope, ExecutionContext &exe_ctx, 185 lldb_private::ExecutionPolicy execution_policy, bool keep_result_in_memory, 186 bool generate_debug_info); 187 188 void SetupCppModuleImports(ExecutionContext &exe_ctx); 189 190 void ScanContext(ExecutionContext &exe_ctx, 191 lldb_private::Status &err) override; 192 193 bool AddArguments(ExecutionContext &exe_ctx, std::vector<lldb::addr_t> &args, 194 lldb::addr_t struct_address, 195 DiagnosticManager &diagnostic_manager) override; 196 197 void CreateSourceCode(DiagnosticManager &diagnostic_manager, 198 ExecutionContext &exe_ctx, 199 std::vector<std::string> modules_to_import, 200 bool for_completion); 201 202 lldb::addr_t GetCppObjectPointer(lldb::StackFrameSP frame, 203 ConstString &object_name, Status &err); 204 205 /// Defines how the current expression should be wrapped. 206 ClangExpressionSourceCode::WrapKind GetWrapKind() const; 207 bool SetupPersistentState(DiagnosticManager &diagnostic_manager, 208 ExecutionContext &exe_ctx); 209 bool PrepareForParsing(DiagnosticManager &diagnostic_manager, 210 ExecutionContext &exe_ctx, bool for_completion); 211 212 ClangUserExpressionHelper m_type_system_helper; 213 214 class ResultDelegate : public Materializer::PersistentVariableDelegate { 215 public: 216 ResultDelegate(lldb::TargetSP target) : m_target_sp(target) {} 217 ConstString GetName() override; 218 void DidDematerialize(lldb::ExpressionVariableSP &variable) override; 219 220 void RegisterPersistentState(PersistentExpressionState *persistent_state); 221 lldb::ExpressionVariableSP &GetVariable(); 222 223 private: 224 PersistentExpressionState *m_persistent_state; 225 lldb::ExpressionVariableSP m_variable; 226 lldb::TargetSP m_target_sp; 227 }; 228 229 /// The include directories that should be used when parsing the expression. 230 std::vector<std::string> m_include_directories; 231 232 /// The absolute character position in the transformed source code where the 233 /// user code (as typed by the user) starts. If the variable is empty, then we 234 /// were not able to calculate this position. 235 llvm::Optional<size_t> m_user_expression_start_pos; 236 ResultDelegate m_result_delegate; 237 ClangPersistentVariables *m_clang_state; 238 std::unique_ptr<ClangExpressionSourceCode> m_source_code; 239 /// The parser instance we used to parse the expression. 240 std::unique_ptr<ClangExpressionParser> m_parser; 241 /// File name used for the expression. 242 std::string m_filename; 243 244 /// The object (if any) in which context the expression is evaluated. 245 /// See the comment to `UserExpression::Evaluate` for details. 246 ValueObject *m_ctx_obj; 247 248 /// A list of module names that should be imported when parsing. 249 /// \see CppModuleConfiguration::GetImportedModules 250 std::vector<std::string> m_imported_cpp_modules; 251 252 /// True if the expression parser should enforce the presence of a valid class 253 /// pointer in order to generate the expression as a method. 254 bool m_enforce_valid_object = true; 255 /// True if the expression is compiled as a C++ member function (true if it 256 /// was parsed when exe_ctx was in a C++ method). 257 bool m_in_cplusplus_method = false; 258 /// True if the expression is compiled as an Objective-C method (true if it 259 /// was parsed when exe_ctx was in an Objective-C method). 260 bool m_in_objectivec_method = false; 261 /// True if the expression is compiled as a static (or class) method 262 /// (currently true if it was parsed when exe_ctx was in an Objective-C class 263 /// method). 264 bool m_in_static_method = false; 265 /// True if "this" or "self" must be looked up and passed in. False if the 266 /// expression doesn't really use them and they can be NULL. 267 bool m_needs_object_ptr = false; 268 }; 269 270 } // namespace lldb_private 271 272 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGUSEREXPRESSION_H 273