2020#include " swift/AST/Pattern.h"
2121#include " swift/AST/Stmt.h"
2222#include " swift/AST/TypeCheckRequests.h"
23+ #include " swift/AST/TypeWrappers.h"
2324#include " swift/Basic/LLVM.h"
2425#include " swift/Basic/SourceLoc.h"
2526
@@ -77,12 +78,6 @@ VarDecl *VarDecl::getUnderlyingTypeWrapperStorage() const {
7778 nullptr );
7879}
7980
80- struct TypeWrapperAttrInfo {
81- CustomAttr *Attr;
82- NominalTypeDecl *Wrapper;
83- NominalTypeDecl *AttachedTo;
84- };
85-
8681static void
8782getDeclaredProtocolConformances (NominalTypeDecl *decl,
8883 SmallVectorImpl<ProtocolDecl *> &protocols) {
@@ -110,9 +105,8 @@ getDeclaredProtocolConformances(NominalTypeDecl *decl,
110105 }
111106}
112107
113- static void
114- getTypeWrappers (NominalTypeDecl *decl,
115- SmallVectorImpl<TypeWrapperAttrInfo> &typeWrappers) {
108+ static void getTypeWrappers (NominalTypeDecl *decl,
109+ SmallVectorImpl<TypeWrapperInfo> &typeWrappers) {
116110 auto &ctx = decl->getASTContext ();
117111
118112 // Attributes applied directly to the type.
@@ -126,7 +120,8 @@ getTypeWrappers(NominalTypeDecl *decl,
126120
127121 auto *typeWrapper = nominal->getAttrs ().getAttribute <TypeWrapperAttr>();
128122 if (typeWrapper && typeWrapper->isValid ())
129- typeWrappers.push_back ({mutableAttr, nominal, decl});
123+ typeWrappers.push_back (
124+ {mutableAttr, nominal, decl, /* isInferred=*/ false });
130125 }
131126
132127 // Do not allow transitive protocol inference between protocols.
@@ -139,33 +134,33 @@ getTypeWrappers(NominalTypeDecl *decl,
139134 getDeclaredProtocolConformances (decl, protocols);
140135
141136 for (auto *protocol : protocols) {
142- SmallVector<TypeWrapperAttrInfo , 2 > inferredAttrs;
137+ SmallVector<TypeWrapperInfo , 2 > inferredAttrs;
143138 getTypeWrappers (protocol, inferredAttrs);
144139
145140 // De-duplicate inferred type wrappers. This also makes sure
146141 // that if both protocol and conforming type explicitly declare
147142 // the same type wrapper there is no clash between them.
148143 for (const auto &inferredAttr : inferredAttrs) {
149- if (llvm::find_if (typeWrappers, [&](const TypeWrapperAttrInfo &attr) {
144+ if (llvm::find_if (typeWrappers, [&](const TypeWrapperInfo &attr) {
150145 return attr.Wrapper == inferredAttr.Wrapper ;
151146 }) == typeWrappers.end ())
152- typeWrappers.push_back (inferredAttr);
147+ typeWrappers.push_back (inferredAttr. asInferred () );
153148 }
154149 }
155150}
156151
157- NominalTypeDecl * GetTypeWrapper::evaluate (Evaluator &evaluator,
158- NominalTypeDecl *decl) const {
152+ Optional<TypeWrapperInfo>
153+ GetTypeWrapper::evaluate (Evaluator &evaluator, NominalTypeDecl *decl) const {
159154 auto &ctx = decl->getASTContext ();
160155
161156 // Note that we don't actually care whether there are duplicates,
162157 // using the same type wrapper multiple times is still an error.
163- SmallVector<TypeWrapperAttrInfo , 2 > typeWrappers;
158+ SmallVector<TypeWrapperInfo , 2 > typeWrappers;
164159
165160 getTypeWrappers (decl, typeWrappers);
166161
167162 if (typeWrappers.empty ())
168- return nullptr ;
163+ return None ;
169164
170165 if (typeWrappers.size () != 1 ) {
171166 ctx.Diags .diagnose (decl, diag::cannot_use_multiple_type_wrappers,
@@ -183,25 +178,21 @@ NominalTypeDecl *GetTypeWrapper::evaluate(Evaluator &evaluator,
183178 }
184179 }
185180
186- return nullptr ;
181+ return None ;
187182 }
188183
189- return typeWrappers.front (). Wrapper ;
184+ return typeWrappers.front ();
190185}
191186
192187Type GetTypeWrapperType::evaluate (Evaluator &evaluator,
193188 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)
199191 return Type ();
200192
201- auto *typeWrapperAttr = typeWrappers.front ().Attr ;
202193 auto type = evaluateOrDefault (
203194 evaluator,
204- CustomAttrTypeRequest{typeWrapperAttr , decl->getDeclContext (),
195+ CustomAttrTypeRequest{typeWrapperInfo-> Attr , decl->getDeclContext (),
205196 CustomAttrTypeKind::TypeWrapper},
206197 Type ());
207198
@@ -325,7 +316,7 @@ GetTypeWrapperProperty::evaluate(Evaluator &evaluator,
325316 NominalTypeDecl *parent) const {
326317 auto &ctx = parent->getASTContext ();
327318
328- auto * typeWrapper = parent->getTypeWrapper ();
319+ auto typeWrapper = parent->getTypeWrapper ();
329320 if (!typeWrapper)
330321 return nullptr ;
331322
@@ -339,10 +330,9 @@ GetTypeWrapperProperty::evaluate(Evaluator &evaluator,
339330
340331 // $storage: Wrapper<<ParentType>, <ParentType>.$Storage>
341332 auto propertyTy = BoundGenericType::get (
342- typeWrapper, /* Parent=*/ typeWrapperType->getParent (),
333+ typeWrapper-> Wrapper , /* Parent=*/ typeWrapperType->getParent (),
343334 /* genericArgs=*/
344- {parent->getSelfInterfaceType (),
345- storage->getDeclaredInterfaceType ()});
335+ {parent->getSelfInterfaceType (), storage->getDeclaredInterfaceType ()});
346336
347337 return injectProperty (parent, ctx.Id_TypeWrapperProperty , propertyTy,
348338 VarDecl::Introducer::Var,
@@ -426,7 +416,7 @@ static SubscriptExpr *subscriptTypeWrappedProperty(VarDecl *var,
426416 ctx, DeclBaseName::createSubscript (),
427417 {ctx.Id_wrappedSelf , ctx.Id_propertyKeyPath , ctx.Id_storageKeyPath });
428418
429- auto *typeWrapper = parent->getTypeWrapper ();
419+ auto *typeWrapper = parent->getTypeWrapper ()-> Wrapper ;
430420 auto candidates = typeWrapper->lookupDirect (subscriptName);
431421
432422 if (!candidates.empty ()) {
0 commit comments