• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
DidImportCxxModules()171   bool DidImportCxxModules() const { return m_imported_cpp_modules; }
172 
173 private:
174   /// Populate m_in_cplusplus_method and m_in_objectivec_method based on the
175   /// environment.
176 
177   void ScanContext(ExecutionContext &exe_ctx,
178                    lldb_private::Status &err) override;
179 
180   bool AddArguments(ExecutionContext &exe_ctx, std::vector<lldb::addr_t> &args,
181                     lldb::addr_t struct_address,
182                     DiagnosticManager &diagnostic_manager) override;
183 
184   void CreateSourceCode(DiagnosticManager &diagnostic_manager,
185                         ExecutionContext &exe_ctx,
186                         std::vector<std::string> modules_to_import,
187                         bool for_completion);
188   /// Defines how the current expression should be wrapped.
189   ClangExpressionSourceCode::WrapKind GetWrapKind() const;
190   bool SetupPersistentState(DiagnosticManager &diagnostic_manager,
191                                    ExecutionContext &exe_ctx);
192   bool PrepareForParsing(DiagnosticManager &diagnostic_manager,
193                          ExecutionContext &exe_ctx, bool for_completion);
194 
195   ClangUserExpressionHelper m_type_system_helper;
196 
197   class ResultDelegate : public Materializer::PersistentVariableDelegate {
198   public:
ResultDelegate(lldb::TargetSP target)199     ResultDelegate(lldb::TargetSP target) : m_target_sp(target) {}
200     ConstString GetName() override;
201     void DidDematerialize(lldb::ExpressionVariableSP &variable) override;
202 
203     void RegisterPersistentState(PersistentExpressionState *persistent_state);
204     lldb::ExpressionVariableSP &GetVariable();
205 
206   private:
207     PersistentExpressionState *m_persistent_state;
208     lldb::ExpressionVariableSP m_variable;
209     lldb::TargetSP m_target_sp;
210   };
211 
212   /// The include directories that should be used when parsing the expression.
213   std::vector<std::string> m_include_directories;
214 
215   /// The absolute character position in the transformed source code where the
216   /// user code (as typed by the user) starts. If the variable is empty, then we
217   /// were not able to calculate this position.
218   llvm::Optional<size_t> m_user_expression_start_pos;
219   ResultDelegate m_result_delegate;
220   ClangPersistentVariables *m_clang_state;
221   std::unique_ptr<ClangExpressionSourceCode> m_source_code;
222   /// File name used for the expression.
223   std::string m_filename;
224 
225   /// The object (if any) in which context the expression is evaluated.
226   /// See the comment to `UserExpression::Evaluate` for details.
227   ValueObject *m_ctx_obj;
228 
229   /// True iff this expression explicitly imported C++ modules.
230   bool m_imported_cpp_modules = false;
231 
232   /// True if the expression parser should enforce the presence of a valid class
233   /// pointer in order to generate the expression as a method.
234   bool m_enforce_valid_object = true;
235   /// True if the expression is compiled as a C++ member function (true if it
236   /// was parsed when exe_ctx was in a C++ method).
237   bool m_in_cplusplus_method = false;
238   /// True if the expression is compiled as an Objective-C method (true if it
239   /// was parsed when exe_ctx was in an Objective-C method).
240   bool m_in_objectivec_method = false;
241   /// True if the expression is compiled as a static (or class) method
242   /// (currently true if it was parsed when exe_ctx was in an Objective-C class
243   /// method).
244   bool m_in_static_method = false;
245   /// True if "this" or "self" must be looked up and passed in.  False if the
246   /// expression doesn't really use them and they can be NULL.
247   bool m_needs_object_ptr = false;
248 };
249 
250 } // namespace lldb_private
251 
252 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGUSEREXPRESSION_H
253