File: SwiftUserExpression.h

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (208 lines) | stat: -rw-r--r-- 7,515 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//===-- SwiftUserExpression.h -----------------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_SwiftUserExpression_h_
#define liblldb_SwiftUserExpression_h_

// C Includes
// C++ Includes
#include <map>
#include <string>
#include <vector>

#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
#include "SwiftExpressionParser.h"
#include "lldb/Expression/LLVMUserExpression.h"
#include "lldb/Expression/Materializer.h"

// Other libraries and framework includes
// Project includes

namespace lldb_private {
class SwiftExpressionSourceCode;
  
//----------------------------------------------------------------------
/// @class SwiftUserExpression SwiftUserExpression.h
/// "lldb/Expression/SwiftUserExpression.h"
/// @brief Encapsulates a single expression for use with Clang
///
/// LLDB uses expressions for various purposes, notably to call functions
/// and as a backend for the expr command.  SwiftUserExpression encapsulates
/// the objects needed to parse and interpret or JIT an expression.  It
/// uses the Swift parser to produce LLVM IR from the expression.
//----------------------------------------------------------------------
class SwiftUserExpression : public LLVMUserExpression {
  // LLVM RTTI support
  static char ID;

public:
  bool isA(const void *ClassID) const override {
    return ClassID == &ID || LLVMUserExpression::isA(ClassID);
  }
  static bool classof(const Expression *obj) { return obj->isA(&ID); }

  enum { kDefaultTimeout = 500000u };

  class SwiftUserExpressionHelper : public ExpressionTypeSystemHelper {
  public:
    SwiftUserExpressionHelper(Target &) : ExpressionTypeSystemHelper() {}

    ~SwiftUserExpressionHelper() {}
  };

  //------------------------------------------------------------------
  /// Constructor
  ///
  /// @param[in] expr
  ///     The expression to parse.
  ///
  /// @param[in] expr_prefix
  ///     If non-NULL, a C string containing translation-unit level
  ///     definitions to be included when the expression is parsed.
  ///
  /// @param[in] language
  ///     If not eLanguageTypeUnknown, a language to use when parsing
  ///     the expression.  Currently restricted to those languages
  ///     supported by Clang.
  ///
  /// @param[in] desired_type
  ///     If not eResultTypeAny, the type to use for the expression
  ///     result.
  ///
  /// @param[in] options
  ///     Additional options for the expression.
  //------------------------------------------------------------------
  SwiftUserExpression(ExecutionContextScope &exe_scope, llvm::StringRef expr,
                      llvm::StringRef prefix, SourceLanguage language,
                      ResultType desired_type,
                      const EvaluateExpressionOptions &options);

  //------------------------------------------------------------------
  /// Destructor
  //------------------------------------------------------------------
  ~SwiftUserExpression() override;

  //------------------------------------------------------------------
  /// Parse the expression
  ///
  /// @param[in] diagnostic_manager
  ///     A diagnostic manager to report parse errors and warnings to.
  ///
  /// @param[in] exe_ctx
  ///     The execution context to use when looking up entities that
  ///     are needed for parsing (locations of functions, types of
  ///     variables, persistent variables, etc.)
  ///
  /// @param[in] execution_policy
  ///     Determines whether interpretation is possible or mandatory.
  ///
  /// @param[in] keep_result_in_memory
  ///     True if the resulting persistent variable should reside in
  ///     target memory, if applicable.
  ///
  /// @return
  ///     True on success (no errors); false otherwise.
  //------------------------------------------------------------------
  bool Parse(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
             lldb_private::ExecutionPolicy execution_policy,
             bool keep_result_in_memory, bool generate_debug_info) override;

  ExpressionTypeSystemHelper *GetTypeSystemHelper() override {
    return &m_type_system_helper;
  }

  Materializer::PersistentVariableDelegate &GetResultDelegate() {
    return m_result_delegate;
  }

  Materializer::PersistentVariableDelegate &GetErrorDelegate() {
    return m_error_delegate;
  }

  Materializer::PersistentVariableDelegate &GetPersistentVariableDelegate() {
    return m_persistent_variable_delegate;
  }

  lldb::ExpressionVariableSP
  GetResultAfterDematerialization(ExecutionContextScope *exe_scope) override;

  void WillStartExecuting() override;
  void DidFinishExecuting() override;

  bool IsParseCacheable() override {
    return m_parser->IsParseCacheable();
  }

private:
  //------------------------------------------------------------------
  /// Populate m_in_cplusplus_method and m_in_objectivec_method based on the
  /// environment.
  //------------------------------------------------------------------

  void ScanContext(ExecutionContext &exe_ctx,
                   lldb_private::Status &err) override;

  bool AddArguments(ExecutionContext &exe_ctx, std::vector<lldb::addr_t> &args,
                    lldb::addr_t struct_address,
                    DiagnosticManager &diagnostic_manager) override;

  SwiftExpressionParser::ParseResult
  GetTextAndSetExpressionParser(DiagnosticManager &diagnostic_manager,
                  std::unique_ptr<SwiftExpressionSourceCode> &source_code,
                  ExecutionContext &exe_ctx, ExecutionContextScope *exe_scope);

  SwiftUserExpressionHelper m_type_system_helper;

  class ResultDelegate : public Materializer::PersistentVariableDelegate {
  public:
    ResultDelegate(lldb::TargetSP target, SwiftUserExpression &, bool is_error);
    ConstString GetName() override;
    void DidDematerialize(lldb::ExpressionVariableSP &variable) override;

    void RegisterPersistentState(PersistentExpressionState *persistent_state);
    lldb::ExpressionVariableSP &GetVariable();

  private:
    lldb::TargetSP m_target_sp;
    PersistentExpressionState *m_persistent_state;
    lldb::ExpressionVariableSP m_variable;
    bool m_is_error;
  };

  ResultDelegate m_result_delegate;
  ResultDelegate m_error_delegate;

  class PersistentVariableDelegate
      : public Materializer::PersistentVariableDelegate {
  public:
    PersistentVariableDelegate(SwiftUserExpression &);
    ConstString GetName() override;
    void DidDematerialize(lldb::ExpressionVariableSP &variable) override;
  };

  std::optional<SwiftScratchContextReader> m_swift_scratch_ctx;
  SwiftASTContextForExpressions *m_swift_ast_ctx;
  PersistentVariableDelegate m_persistent_variable_delegate;
  std::unique_ptr<SwiftExpressionParser> m_parser;
  std::optional<SwiftLanguageRuntime::GenericSignature> m_generic_signature;
  std::optional<lldb::user_id_t> m_debugger_id;
  Status m_err;
  bool m_runs_in_playground_or_repl;
  bool m_needs_object_ptr = false;
  bool m_in_static_method = false;
  bool m_is_class = false;
  bool m_is_weak_self = false;
};

} // namespace lldb_private

#endif // liblldb_SwiftUserExpression_h_