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