20
20
#include " swift/AST/Pattern.h"
21
21
#include " swift/AST/Stmt.h"
22
22
#include " swift/AST/TypeCheckRequests.h"
23
+ #include " swift/AST/TypeWrappers.h"
23
24
#include " swift/Basic/LLVM.h"
24
25
#include " swift/Basic/SourceLoc.h"
25
26
@@ -77,12 +78,6 @@ VarDecl *VarDecl::getUnderlyingTypeWrapperStorage() const {
77
78
nullptr );
78
79
}
79
80
80
- struct TypeWrapperAttrInfo {
81
- CustomAttr *Attr;
82
- NominalTypeDecl *Wrapper;
83
- NominalTypeDecl *AttachedTo;
84
- };
85
-
86
81
static void
87
82
getDeclaredProtocolConformances (NominalTypeDecl *decl,
88
83
SmallVectorImpl<ProtocolDecl *> &protocols) {
@@ -110,9 +105,8 @@ getDeclaredProtocolConformances(NominalTypeDecl *decl,
110
105
}
111
106
}
112
107
113
- static void
114
- getTypeWrappers (NominalTypeDecl *decl,
115
- SmallVectorImpl<TypeWrapperAttrInfo> &typeWrappers) {
108
+ static void getTypeWrappers (NominalTypeDecl *decl,
109
+ SmallVectorImpl<TypeWrapperInfo> &typeWrappers) {
116
110
auto &ctx = decl->getASTContext ();
117
111
118
112
// Attributes applied directly to the type.
@@ -126,7 +120,8 @@ getTypeWrappers(NominalTypeDecl *decl,
126
120
127
121
auto *typeWrapper = nominal->getAttrs ().getAttribute <TypeWrapperAttr>();
128
122
if (typeWrapper && typeWrapper->isValid ())
129
- typeWrappers.push_back ({mutableAttr, nominal, decl});
123
+ typeWrappers.push_back (
124
+ {mutableAttr, nominal, decl, /* isInferred=*/ false });
130
125
}
131
126
132
127
// Do not allow transitive protocol inference between protocols.
@@ -139,33 +134,33 @@ getTypeWrappers(NominalTypeDecl *decl,
139
134
getDeclaredProtocolConformances (decl, protocols);
140
135
141
136
for (auto *protocol : protocols) {
142
- SmallVector<TypeWrapperAttrInfo , 2 > inferredAttrs;
137
+ SmallVector<TypeWrapperInfo , 2 > inferredAttrs;
143
138
getTypeWrappers (protocol, inferredAttrs);
144
139
145
140
// De-duplicate inferred type wrappers. This also makes sure
146
141
// that if both protocol and conforming type explicitly declare
147
142
// the same type wrapper there is no clash between them.
148
143
for (const auto &inferredAttr : inferredAttrs) {
149
- if (llvm::find_if (typeWrappers, [&](const TypeWrapperAttrInfo &attr) {
144
+ if (llvm::find_if (typeWrappers, [&](const TypeWrapperInfo &attr) {
150
145
return attr.Wrapper == inferredAttr.Wrapper ;
151
146
}) == typeWrappers.end ())
152
- typeWrappers.push_back (inferredAttr);
147
+ typeWrappers.push_back (inferredAttr. asInferred () );
153
148
}
154
149
}
155
150
}
156
151
157
- NominalTypeDecl * GetTypeWrapper::evaluate (Evaluator &evaluator,
158
- NominalTypeDecl *decl) const {
152
+ Optional<TypeWrapperInfo>
153
+ GetTypeWrapper::evaluate (Evaluator &evaluator, NominalTypeDecl *decl) const {
159
154
auto &ctx = decl->getASTContext ();
160
155
161
156
// Note that we don't actually care whether there are duplicates,
162
157
// using the same type wrapper multiple times is still an error.
163
- SmallVector<TypeWrapperAttrInfo , 2 > typeWrappers;
158
+ SmallVector<TypeWrapperInfo , 2 > typeWrappers;
164
159
165
160
getTypeWrappers (decl, typeWrappers);
166
161
167
162
if (typeWrappers.empty ())
168
- return nullptr ;
163
+ return None ;
169
164
170
165
if (typeWrappers.size () != 1 ) {
171
166
ctx.Diags .diagnose (decl, diag::cannot_use_multiple_type_wrappers,
@@ -183,25 +178,21 @@ NominalTypeDecl *GetTypeWrapper::evaluate(Evaluator &evaluator,
183
178
}
184
179
}
185
180
186
- return nullptr ;
181
+ return None ;
187
182
}
188
183
189
- return typeWrappers.front (). Wrapper ;
184
+ return typeWrappers.front ();
190
185
}
191
186
192
187
Type GetTypeWrapperType::evaluate (Evaluator &evaluator,
193
188
NominalTypeDecl *decl) const {
194
- SmallVector<TypeWrapperAttrInfo, 2 > typeWrappers;
195
-
196
- getTypeWrappers (decl, typeWrappers);
197
-
198
- if (typeWrappers.size () != 1 )
189
+ auto typeWrapperInfo = decl->getTypeWrapper ();
190
+ if (!typeWrapperInfo)
199
191
return Type ();
200
192
201
- auto *typeWrapperAttr = typeWrappers.front ().Attr ;
202
193
auto type = evaluateOrDefault (
203
194
evaluator,
204
- CustomAttrTypeRequest{typeWrapperAttr , decl->getDeclContext (),
195
+ CustomAttrTypeRequest{typeWrapperInfo-> Attr , decl->getDeclContext (),
205
196
CustomAttrTypeKind::TypeWrapper},
206
197
Type ());
207
198
@@ -325,7 +316,7 @@ GetTypeWrapperProperty::evaluate(Evaluator &evaluator,
325
316
NominalTypeDecl *parent) const {
326
317
auto &ctx = parent->getASTContext ();
327
318
328
- auto * typeWrapper = parent->getTypeWrapper ();
319
+ auto typeWrapper = parent->getTypeWrapper ();
329
320
if (!typeWrapper)
330
321
return nullptr ;
331
322
@@ -339,10 +330,9 @@ GetTypeWrapperProperty::evaluate(Evaluator &evaluator,
339
330
340
331
// $storage: Wrapper<<ParentType>, <ParentType>.$Storage>
341
332
auto propertyTy = BoundGenericType::get (
342
- typeWrapper, /* Parent=*/ typeWrapperType->getParent (),
333
+ typeWrapper-> Wrapper , /* Parent=*/ typeWrapperType->getParent (),
343
334
/* genericArgs=*/
344
- {parent->getSelfInterfaceType (),
345
- storage->getDeclaredInterfaceType ()});
335
+ {parent->getSelfInterfaceType (), storage->getDeclaredInterfaceType ()});
346
336
347
337
return injectProperty (parent, ctx.Id_TypeWrapperProperty , propertyTy,
348
338
VarDecl::Introducer::Var,
@@ -426,7 +416,7 @@ static SubscriptExpr *subscriptTypeWrappedProperty(VarDecl *var,
426
416
ctx, DeclBaseName::createSubscript (),
427
417
{ctx.Id_wrappedSelf , ctx.Id_propertyKeyPath , ctx.Id_storageKeyPath });
428
418
429
- auto *typeWrapper = parent->getTypeWrapper ();
419
+ auto *typeWrapper = parent->getTypeWrapper ()-> Wrapper ;
430
420
auto candidates = typeWrapper->lookupDirect (subscriptName);
431
421
432
422
if (!candidates.empty ()) {
0 commit comments