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