-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathSwiftDeclSynthesizer.h
335 lines (292 loc) · 13.7 KB
/
SwiftDeclSynthesizer.h
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//===--- DeclSynthesizer.h - Synthesize helper Swift decls ------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 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 SWIFT_SWIFT_DECL_SYNTHESIZER_H
#define SWIFT_SWIFT_DECL_SYNTHESIZER_H
#include "ImporterImpl.h"
#include "swift/ClangImporter/ClangImporter.h"
namespace swift {
class CallExpr;
enum class MakeStructRawValuedFlags {
/// whether to also create an unlabeled init
MakeUnlabeledValueInit = 0x01,
/// whether the raw value should be a let
IsLet = 0x02,
/// whether to mark the rawValue as implicit
IsImplicit = 0x04,
};
using MakeStructRawValuedOptions = OptionSet<MakeStructRawValuedFlags>;
inline MakeStructRawValuedOptions getDefaultMakeStructRawValuedOptions() {
MakeStructRawValuedOptions opts;
opts -= MakeStructRawValuedFlags::MakeUnlabeledValueInit; // default off
opts |= MakeStructRawValuedFlags::IsLet; // default on
opts |= MakeStructRawValuedFlags::IsImplicit; // default on
return opts;
}
inline AccessLevel getOverridableAccessLevel(const DeclContext *dc) {
return (dc->getSelfClassDecl() ? AccessLevel::Open : AccessLevel::Public);
}
enum class ReferenceReturnTypeBehaviorForBaseMethodSynthesis {
KeepReference,
RemoveReference,
RemoveReferenceIfPointer,
};
enum class ForwardingMethodKind { Base, Virtual };
class SwiftDeclSynthesizer {
private:
ClangImporter::Implementation &ImporterImpl;
public:
explicit SwiftDeclSynthesizer(ClangImporter::Implementation &Impl)
: ImporterImpl(Impl) {}
explicit SwiftDeclSynthesizer(ClangImporter *importer)
: ImporterImpl(importer->Impl) {}
/// Create a typedpattern(namedpattern(decl))
static Pattern *createTypedNamedPattern(VarDecl *decl);
/// Create a var member for this struct, along with its pattern binding, and
/// add it as a member.
static std::pair<VarDecl *, PatternBindingDecl *>
createVarWithPattern(DeclContext *dc, Identifier name, Type ty,
VarDecl::Introducer introducer, bool isImplicit,
AccessLevel access, AccessLevel setterAccess);
/// Create a reinterpretCast from the `exprType`, to the `givenType`.
static Expr *synthesizeReturnReinterpretCast(ASTContext &ctx, Type givenType,
Type exprType, Expr *baseExpr);
/// Create a new named constant with the given value.
///
/// \param name The name of the constant.
/// \param dc The declaration context into which the name will be introduced.
/// \param type The type of the named constant.
/// \param value The value of the named constant.
/// \param convertKind How to convert the constant to the given type.
/// \param isStatic Whether the constant should be a static member of \p dc.
ValueDecl *createConstant(Identifier name, DeclContext *dc, Type type,
const clang::APValue &value,
ConstantConvertKind convertKind, bool isStatic,
ClangNode ClangN);
/// Create a new named constant with the given value.
///
/// \param name The name of the constant.
/// \param dc The declaration context into which the name will be introduced.
/// \param type The type of the named constant.
/// \param value The value of the named constant.
/// \param convertKind How to convert the constant to the given type.
/// \param isStatic Whether the constant should be a static member of \p dc.
ValueDecl *createConstant(Identifier name, DeclContext *dc, Type type,
StringRef value, ConstantConvertKind convertKind,
bool isStatic, ClangNode ClangN);
/// Create a new named constant using the given expression.
///
/// \param name The name of the constant.
/// \param dc The declaration context into which the name will be introduced.
/// \param type The type of the named constant.
/// \param valueExpr An expression to use as the value of the constant.
/// \param convertKind How to convert the constant to the given type.
/// \param isStatic Whether the constant should be a static member of \p dc.
ValueDecl *createConstant(Identifier name, DeclContext *dc, Type type,
Expr *valueExpr, ConstantConvertKind convertKind,
bool isStatic, ClangNode ClangN);
/// Create a default constructor that initializes a struct to zero.
ConstructorDecl *createDefaultConstructor(NominalTypeDecl *structDecl);
/// Create a constructor that initializes a struct from its members.
ConstructorDecl *createValueConstructor(NominalTypeDecl *structDecl,
ArrayRef<VarDecl *> members,
bool wantCtorParamNames,
bool wantBody);
/// Create a rawValue-ed constructor that bridges to its underlying storage.
ConstructorDecl *createRawValueBridgingConstructor(StructDecl *structDecl,
VarDecl *computedRawValue,
VarDecl *storedRawValue,
bool wantLabel,
bool wantBody);
/// Make a struct declaration into a raw-value-backed struct, with
/// bridged computed rawValue property which differs from stored backing
///
/// \param structDecl the struct to make a raw value for
/// \param storedUnderlyingType the type of the stored raw value
/// \param bridgedType the type of the 'rawValue' computed property bridge
/// \param synthesizedProtocolAttrs synthesized protocol attributes to add
///
/// This will perform most of the work involved in making a new Swift struct
/// be backed by a stored raw value and computed raw value of bridged type.
/// This will populated derived protocols and synthesized protocols, add the
/// new variable and pattern bindings, and create the inits parameterized
/// over a bridged type that will cast to the stored type, as appropriate.
void makeStructRawValuedWithBridge(
StructDecl *structDecl, Type storedUnderlyingType, Type bridgedType,
ArrayRef<KnownProtocolKind> synthesizedProtocolAttrs,
bool makeUnlabeledValueInit = false);
/// Make a struct declaration into a raw-value-backed struct
///
/// \param structDecl the struct to make a raw value for
/// \param underlyingType the type of the raw value
/// \param synthesizedProtocolAttrs synthesized protocol attributes to add
/// \param setterAccess the access level of the raw value's setter
///
/// This will perform most of the work involved in making a new Swift struct
/// be backed by a raw value. This will populated derived protocols and
/// synthesized protocols, add the new variable and pattern bindings, and
/// create the inits parameterized over a raw value
///
void makeStructRawValued(StructDecl *structDecl, Type underlyingType,
ArrayRef<KnownProtocolKind> synthesizedProtocolAttrs,
MakeStructRawValuedOptions options =
getDefaultMakeStructRawValuedOptions(),
AccessLevel setterAccess = AccessLevel::Private);
/// Build the union field getter and setter.
///
/// \code
/// struct SomeImportedUnion {
/// var myField: Int {
/// get {
/// return Builtin.reinterpretCast(self)
/// }
/// set(newValue) {
/// Builtin.initialize(Builtin.addressof(self), newValue))
/// }
/// }
/// }
/// \endcode
///
/// \returns a pair of the getter and setter function decls.
std::pair<AccessorDecl *, AccessorDecl *>
makeUnionFieldAccessors(NominalTypeDecl *importedUnionDecl,
VarDecl *importedFieldDecl);
/// Build the bitfield getter and setter using Clang.
///
/// \code
/// static inline int get(RecordType self) {
/// return self.field;
/// }
/// static inline void set(int newValue, RecordType *self) {
/// self->field = newValue;
/// }
/// \endcode
///
/// \returns a pair of the getter and setter function decls.
std::pair<FuncDecl *, FuncDecl *> makeBitFieldAccessors(
clang::RecordDecl *structDecl, NominalTypeDecl *importedStructDecl,
clang::FieldDecl *fieldDecl, VarDecl *importedFieldDecl);
/// Build the indirect field getter and setter.
///
/// \code
/// struct SomeImportedIndirectField {
/// struct __Unnamed_struct___Anonymous_field_1 {
/// var myField : Int
/// }
/// var __Anonymous_field_1 : __Unnamed_struct___Anonymous_field_1
/// var myField : Int {
/// get {
/// __Anonymous_field_1.myField
/// }
/// set(newValue) {
/// __Anonymous_field_1.myField = newValue
/// }
/// }
/// }
/// \endcode
///
/// \returns a pair of getter and setter function decls.
std::pair<AccessorDecl *, AccessorDecl *>
makeIndirectFieldAccessors(const clang::IndirectFieldDecl *indirectField,
ArrayRef<VarDecl *> members,
NominalTypeDecl *importedStructDecl,
VarDecl *importedFieldDecl);
/// Build the init(rawValue:) initializer for an imported NS_ENUM.
///
/// \code
/// enum NSSomeEnum: RawType {
/// init?(rawValue: RawType) {
/// self = Builtin.reinterpretCast(rawValue)
/// }
/// }
/// \endcode
///
/// Unlike a standard init(rawValue:) enum initializer, this does a
/// reinterpret cast in order to preserve unknown or future cases from C.
ConstructorDecl *makeEnumRawValueConstructor(EnumDecl *enumDecl);
/// Build the rawValue getter for an imported NS_ENUM.
///
/// \code
/// enum NSSomeEnum: RawType {
/// var rawValue: RawType {
/// return Builtin.reinterpretCast(self)
/// }
/// }
/// \endcode
///
/// Unlike a standard init(rawValue:) enum initializer, this does a
/// reinterpret cast in order to preserve unknown or future cases from C.
void makeEnumRawValueGetter(EnumDecl *enumDecl, VarDecl *rawValueDecl);
/// Build the rawValue getter for a struct type.
///
/// \code
/// struct SomeType: RawRepresentable {
/// private var _rawValue: ObjCType
/// var rawValue: SwiftType {
/// return _rawValue as SwiftType
/// }
/// }
/// \endcode
AccessorDecl *makeStructRawValueGetter(StructDecl *structDecl,
VarDecl *computedVar,
VarDecl *storedVar);
/// Build a declaration for an Objective-C subscript getter.
AccessorDecl *buildSubscriptGetterDecl(SubscriptDecl *subscript,
const FuncDecl *getter, Type elementTy,
DeclContext *dc, ParamDecl *index);
/// Build a declaration for an Objective-C subscript setter.
AccessorDecl *buildSubscriptSetterDecl(SubscriptDecl *subscript,
const FuncDecl *setter,
Type elementInterfaceTy,
DeclContext *dc, ParamDecl *index);
/// Given either the getter, the setter, or both getter & setter
/// for a subscript operation, create the Swift subscript declaration.
///
/// \param getter function returning `UnsafePointer<T>`
/// \param setter function returning `UnsafeMutablePointer<T>`
/// \return subscript declaration
SubscriptDecl *makeSubscript(FuncDecl *getter, FuncDecl *setter);
/// Given an imported C++ dereference operator (`operator*()`), create a
/// `pointee` computed property.
///
/// \param getter function returning `UnsafePointer<T>`
/// \param setter function returning `UnsafeMutablePointer<T>`
/// \return computed property declaration
VarDecl *makeDereferencedPointeeProperty(FuncDecl *getter, FuncDecl *setter);
/// Given a C++ pre-increment operator (`operator++()`). create a non-mutating
/// function `successor() -> Self`.
FuncDecl *makeSuccessorFunc(FuncDecl *incrementFunc);
FuncDecl *makeOperator(FuncDecl *operatorMethod,
clang::OverloadedOperatorKind opKind);
// Synthesize a C++ method that invokes the method from the base
// class. This lets Clang take care of the cast from the derived class
// to the base class during the invocation of the method.
clang::CXXMethodDecl *synthesizeCXXForwardingMethod(
const clang::CXXRecordDecl *derivedClass,
const clang::CXXRecordDecl *baseClass, const clang::CXXMethodDecl *method,
ForwardingMethodKind forwardingMethodKind,
ReferenceReturnTypeBehaviorForBaseMethodSynthesis
referenceReturnTypeBehavior =
ReferenceReturnTypeBehaviorForBaseMethodSynthesis::KeepReference,
bool forceConstQualifier = false);
/// Given an overload of a C++ virtual method on a reference type, create a
/// method that dispatches the call dynamically.
FuncDecl *makeVirtualMethod(const clang::CXXMethodDecl *clangMethodDecl);
VarDecl *makeComputedPropertyFromCXXMethods(FuncDecl *getter,
FuncDecl *setter);
CallExpr *makeDefaultArgument(const clang::ParmVarDecl *param,
const swift::Type &swiftParamTy,
SourceLoc paramLoc);
private:
Type getConstantLiteralType(Type type, ConstantConvertKind convertKind);
};
} // namespace swift
#endif // SWIFT_SWIFT_DECL_SYNTHESIZER_H