Skip to content

Commit 21a86b5

Browse files
committed
[NFC][Property Wrappers] Split PropertyWrapperBackingPropertyInfoRequest into
two separate requests - one to synthesize the auxiliary declarations, and another to compute how the backing storage is initialized.
1 parent a79f3a6 commit 21a86b5

26 files changed

+256
-170
lines changed

include/swift/AST/ASTTypeIDZone.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ SWIFT_TYPEID(GenericSignature)
2626
SWIFT_TYPEID(ImplicitImportList)
2727
SWIFT_TYPEID(ImplicitMemberAction)
2828
SWIFT_TYPEID(ParamSpecifier)
29-
SWIFT_TYPEID(PropertyWrapperBackingPropertyInfo)
29+
SWIFT_TYPEID(PropertyWrapperAuxiliaryVariables)
30+
SWIFT_TYPEID(PropertyWrapperInitializerInfo)
3031
SWIFT_TYPEID(PropertyWrapperTypeInfo)
3132
SWIFT_TYPEID(Requirement)
3233
SWIFT_TYPEID(ResilienceExpansion)

include/swift/AST/ASTTypeIDs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ enum class ParamSpecifier : uint8_t;
5454
class PostfixOperatorDecl;
5555
class PrecedenceGroupDecl;
5656
class PrefixOperatorDecl;
57-
struct PropertyWrapperBackingPropertyInfo;
57+
struct PropertyWrapperAuxiliaryVariables;
58+
class PropertyWrapperInitializerInfo;
5859
struct PropertyWrapperTypeInfo;
5960
enum class CtorInitializerKind;
6061
struct PropertyWrapperLValueness;

include/swift/AST/Decl.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ namespace swift {
8282
class ParameterTypeFlags;
8383
class Pattern;
8484
struct PrintOptions;
85-
struct PropertyWrapperBackingPropertyInfo;
85+
struct PropertyWrapperAuxiliaryVariables;
86+
class PropertyWrapperInitializerInfo;
8687
struct PropertyWrapperTypeInfo;
8788
struct PropertyWrapperMutability;
8889
class ProtocolDecl;
@@ -4957,10 +4958,15 @@ class VarDecl : public AbstractStorageDecl {
49574958
/// unbound generic types. It will be the type of the backing property.
49584959
Type getPropertyWrapperBackingPropertyType() const;
49594960

4960-
/// Retrieve information about the backing properties of the attached
4961-
/// property wrapper.
4962-
PropertyWrapperBackingPropertyInfo
4963-
getPropertyWrapperBackingPropertyInfo() const;
4961+
/// If there is an attached property wrapper, retrieve the synthesized
4962+
/// auxiliary variables.
4963+
PropertyWrapperAuxiliaryVariables
4964+
getPropertyWrapperAuxiliaryVariables() const;
4965+
4966+
/// If there is an attached property wrapper, retrieve information about
4967+
/// how to initialize the backing property.
4968+
PropertyWrapperInitializerInfo
4969+
getPropertyWrapperInitializerInfo() const;
49644970

49654971
/// Retrieve information about the mutability of the composed
49664972
/// property wrappers.

include/swift/AST/PropertyWrappers.h

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,33 @@ void simple_display(llvm::raw_ostream &os, PropertyWrapperLValueness l);
170170
/// be initialized out-of-line using an expression of the wrapped property type.
171171
PropertyWrapperValuePlaceholderExpr *findWrappedValuePlaceholder(Expr *init);
172172

173-
/// Describes the backing property of a property that has an attached wrapper.
174-
struct PropertyWrapperBackingPropertyInfo {
173+
/// The synthesized auxiliary declarations for a wrapped property, including the
174+
/// backing property wrapper, the projected value variable, and if the wrapped
175+
/// declaration is a parameter, the local wrapped value variable.
176+
struct PropertyWrapperAuxiliaryVariables {
175177
/// The backing property.
176178
VarDecl *backingVar = nullptr;
177179

178180
/// The synthesized projection property, if any. When present, this takes the name
179181
/// of the original wrapped property prefixed with \c $
180182
VarDecl *projectionVar = nullptr;
181183

182-
private:
184+
PropertyWrapperAuxiliaryVariables() {}
185+
186+
PropertyWrapperAuxiliaryVariables(VarDecl *backingVar, VarDecl *projectionVar)
187+
: backingVar(backingVar), projectionVar(projectionVar) {}
188+
189+
/// Whether this is a valid property wrapper.
190+
bool isValid() const {
191+
return backingVar != nullptr;
192+
}
193+
194+
explicit operator bool() const { return isValid(); }
195+
};
196+
197+
/// Describes how to initialize the backing storage of a property with
198+
/// an attached wrapper.
199+
class PropertyWrapperInitializerInfo {
183200
struct {
184201
/// An expression that initializes the backing property from a value of
185202
/// the original property's type via \c init(wrappedValue:) if supported
@@ -203,15 +220,10 @@ struct PropertyWrapperBackingPropertyInfo {
203220
} projectedValueInit;
204221

205222
public:
206-
PropertyWrapperBackingPropertyInfo() { }
207-
208-
PropertyWrapperBackingPropertyInfo(VarDecl *backingVar, VarDecl *projectionVar)
209-
: backingVar(backingVar), projectionVar(projectionVar) { }
223+
PropertyWrapperInitializerInfo() { }
210224

211-
PropertyWrapperBackingPropertyInfo(VarDecl *backingVar, VarDecl *projectionVar,
212-
Expr *wrappedValueInitExpr,
213-
Expr *projectedValueInitExpr)
214-
: backingVar(backingVar), projectionVar(projectionVar) {
225+
PropertyWrapperInitializerInfo(Expr *wrappedValueInitExpr,
226+
Expr *projectedValueInitExpr) {
215227
wrappedValueInit.expr = wrappedValueInitExpr;
216228
if (wrappedValueInitExpr) {
217229
wrappedValueInit.placeholder = findWrappedValuePlaceholder(wrappedValueInitExpr);
@@ -223,16 +235,11 @@ struct PropertyWrapperBackingPropertyInfo {
223235
}
224236
}
225237

226-
/// Whether this is a valid property wrapper.
227-
bool isValid() const {
228-
return backingVar != nullptr;
229-
}
230-
231238
bool hasInitFromWrappedValue() const {
232239
return wrappedValueInit.expr != nullptr;
233240
}
234241

235-
Expr *getInitFromWrappedValue() {
242+
Expr *getInitFromWrappedValue() const {
236243
return wrappedValueInit.expr;
237244
}
238245

@@ -244,7 +251,7 @@ struct PropertyWrapperBackingPropertyInfo {
244251
return projectedValueInit.expr != nullptr;
245252
}
246253

247-
Expr *getInitFromProjectedValue() {
254+
Expr *getInitFromProjectedValue() const {
248255
return projectedValueInit.expr;
249256
}
250257

@@ -255,14 +262,6 @@ struct PropertyWrapperBackingPropertyInfo {
255262
bool hasSynthesizedInitializers() const {
256263
return hasInitFromWrappedValue() || hasInitFromProjectedValue();
257264
}
258-
259-
explicit operator bool() const { return isValid(); }
260-
261-
friend bool operator==(const PropertyWrapperBackingPropertyInfo &lhs,
262-
const PropertyWrapperBackingPropertyInfo &rhs) {
263-
// FIXME: Can't currently compare expressions.
264-
return lhs.backingVar == rhs.backingVar;
265-
}
266265
};
267266

268267
void simple_display(
@@ -271,7 +270,11 @@ void simple_display(
271270

272271
void simple_display(
273272
llvm::raw_ostream &out,
274-
const PropertyWrapperBackingPropertyInfo &backingInfo);
273+
const PropertyWrapperInitializerInfo &initInfo);
274+
275+
void simple_display(
276+
llvm::raw_ostream &out,
277+
const PropertyWrapperAuxiliaryVariables &auxiliaryVars);
275278

276279
} // end namespace swift
277280

include/swift/AST/TypeCheckRequests.h

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class DefaultArgumentExpr;
4444
class ClosureExpr;
4545
class GenericParamList;
4646
class PrecedenceGroupDecl;
47-
struct PropertyWrapperBackingPropertyInfo;
47+
class PropertyWrapperInitializerInfo;
4848
struct PropertyWrapperLValueness;
4949
struct PropertyWrapperMutability;
5050
class RequirementRepr;
@@ -712,11 +712,10 @@ class PropertyWrapperLValuenessRequest :
712712
bool isCached() const;
713713
};
714714

715-
/// Request information about the backing property for properties that have
716-
/// attached property wrappers.
717-
class PropertyWrapperBackingPropertyInfoRequest :
718-
public SimpleRequest<PropertyWrapperBackingPropertyInfoRequest,
719-
PropertyWrapperBackingPropertyInfo(VarDecl *),
715+
/// Request the synthesized auxiliary declarations for a wrapped property.
716+
class PropertyWrapperAuxiliaryVariablesRequest :
717+
public SimpleRequest<PropertyWrapperAuxiliaryVariablesRequest,
718+
PropertyWrapperAuxiliaryVariables(VarDecl *),
720719
RequestFlags::Cached> {
721720
public:
722721
using SimpleRequest::SimpleRequest;
@@ -725,7 +724,28 @@ class PropertyWrapperBackingPropertyInfoRequest :
725724
friend SimpleRequest;
726725

727726
// Evaluation.
728-
PropertyWrapperBackingPropertyInfo
727+
PropertyWrapperAuxiliaryVariables
728+
evaluate(Evaluator &evaluator, VarDecl *var) const;
729+
730+
public:
731+
// Caching
732+
bool isCached() const;
733+
};
734+
735+
/// Request information about initialization of the backing property
736+
/// for properties that have attached property wrappers.
737+
class PropertyWrapperInitializerInfoRequest :
738+
public SimpleRequest<PropertyWrapperInitializerInfoRequest,
739+
PropertyWrapperInitializerInfo(VarDecl *),
740+
RequestFlags::Cached> {
741+
public:
742+
using SimpleRequest::SimpleRequest;
743+
744+
private:
745+
friend SimpleRequest;
746+
747+
// Evaluation.
748+
PropertyWrapperInitializerInfo
729749
evaluate(Evaluator &evaluator, VarDecl *var) const;
730750

731751
public:

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,11 @@ SWIFT_REQUEST(TypeChecker, PatternBindingEntryRequest,
186186
SeparatelyCached, NoLocationInfo)
187187
SWIFT_REQUEST(TypeChecker, PrimarySourceFilesRequest,
188188
ArrayRef<SourceFile *>(ModuleDecl *), Cached, NoLocationInfo)
189-
SWIFT_REQUEST(TypeChecker, PropertyWrapperBackingPropertyInfoRequest,
190-
PropertyWrapperBackingPropertyInfo(VarDecl *), Cached,
189+
SWIFT_REQUEST(TypeChecker, PropertyWrapperAuxiliaryVariablesRequest,
190+
PropertyWrapperAuxiliaryVariables(VarDecl *), Cached,
191+
NoLocationInfo)
192+
SWIFT_REQUEST(TypeChecker, PropertyWrapperInitializerInfoRequest,
193+
PropertyWrapperInitializerInfo(VarDecl *), Cached,
191194
NoLocationInfo)
192195
SWIFT_REQUEST(TypeChecker, PropertyWrapperWrappedValueVarRequest,
193196
VarDecl *(VarDecl *), Cached,

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5020,7 +5020,7 @@ VarDecl *VarDecl::getOriginalWrappedProperty(
50205020
if (!kind)
50215021
return original;
50225022

5023-
auto wrapperInfo = original->getPropertyWrapperBackingPropertyInfo();
5023+
auto wrapperInfo = original->getPropertyWrapperAuxiliaryVariables();
50245024
switch (*kind) {
50255025
case PropertyWrapperSynthesizedPropertyKind::Backing:
50265026
return this == wrapperInfo.backingVar ? original : nullptr;

lib/AST/Decl.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5931,14 +5931,24 @@ Type VarDecl::getPropertyWrapperBackingPropertyType() const {
59315931
Type());
59325932
}
59335933

5934-
PropertyWrapperBackingPropertyInfo
5935-
VarDecl::getPropertyWrapperBackingPropertyInfo() const {
5934+
PropertyWrapperAuxiliaryVariables
5935+
VarDecl::getPropertyWrapperAuxiliaryVariables() const {
59365936
auto &ctx = getASTContext();
59375937
auto mutableThis = const_cast<VarDecl *>(this);
59385938
return evaluateOrDefault(
59395939
ctx.evaluator,
5940-
PropertyWrapperBackingPropertyInfoRequest{mutableThis},
5941-
PropertyWrapperBackingPropertyInfo());
5940+
PropertyWrapperAuxiliaryVariablesRequest{mutableThis},
5941+
PropertyWrapperAuxiliaryVariables());
5942+
}
5943+
5944+
PropertyWrapperInitializerInfo
5945+
VarDecl::getPropertyWrapperInitializerInfo() const {
5946+
auto &ctx = getASTContext();
5947+
auto mutableThis = const_cast<VarDecl *>(this);
5948+
return evaluateOrDefault(
5949+
ctx.evaluator,
5950+
PropertyWrapperInitializerInfoRequest{mutableThis},
5951+
PropertyWrapperInitializerInfo());
59425952
}
59435953

59445954
Optional<PropertyWrapperMutability>
@@ -5963,11 +5973,11 @@ VarDecl::getPropertyWrapperSynthesizedPropertyKind() const {
59635973
}
59645974

59655975
VarDecl *VarDecl::getPropertyWrapperBackingProperty() const {
5966-
return getPropertyWrapperBackingPropertyInfo().backingVar;
5976+
return getPropertyWrapperAuxiliaryVariables().backingVar;
59675977
}
59685978

59695979
VarDecl *VarDecl::getPropertyWrapperProjectionVar() const {
5970-
return getPropertyWrapperBackingPropertyInfo().projectionVar;
5980+
return getPropertyWrapperAuxiliaryVariables().projectionVar;
59715981
}
59725982

59735983
VarDecl *VarDecl::getPropertyWrapperWrappedValueVar() const {
@@ -6038,11 +6048,11 @@ bool VarDecl::isPropertyMemberwiseInitializedWithWrappedType() const {
60386048
}
60396049

60406050
Type VarDecl::getPropertyWrapperInitValueInterfaceType() const {
6041-
auto wrapperInfo = getPropertyWrapperBackingPropertyInfo();
6042-
if (!wrapperInfo || !wrapperInfo.getWrappedValuePlaceholder())
6051+
auto initInfo = getPropertyWrapperInitializerInfo();
6052+
if (!initInfo.getWrappedValuePlaceholder())
60436053
return Type();
60446054

6045-
Type valueInterfaceTy = wrapperInfo.getWrappedValuePlaceholder()->getType();
6055+
Type valueInterfaceTy = initInfo.getWrappedValuePlaceholder()->getType();
60466056
if (valueInterfaceTy->hasArchetype())
60476057
valueInterfaceTy = valueInterfaceTy->mapTypeOutOfContext();
60486058

lib/AST/NameLookup.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "swift/AST/ModuleNameLookup.h"
2929
#include "swift/AST/NameLookupRequests.h"
3030
#include "swift/AST/ParameterList.h"
31+
#include "swift/AST/PropertyWrappers.h"
3132
#include "swift/AST/SourceFile.h"
3233
#include "swift/Basic/Debug.h"
3334
#include "swift/Basic/STLExtras.h"
@@ -1673,8 +1674,10 @@ static void installPropertyWrapperMembersIfNeeded(NominalTypeDecl *target,
16731674
if (auto var = dyn_cast<VarDecl>(member)) {
16741675
if (var->hasAttachedPropertyWrapper()) {
16751676
auto sourceFile = var->getDeclContext()->getParentSourceFile();
1676-
if (sourceFile && sourceFile->Kind != SourceFileKind::Interface)
1677-
(void)var->getPropertyWrapperBackingProperty();
1677+
if (sourceFile && sourceFile->Kind != SourceFileKind::Interface) {
1678+
(void)var->getPropertyWrapperAuxiliaryVariables();
1679+
(void)var->getPropertyWrapperInitializerInfo();
1680+
}
16781681
}
16791682
}
16801683
}

lib/AST/TypeCheckRequests.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,12 @@ bool PropertyWrapperBackingPropertyTypeRequest::isCached() const {
488488
return !var->getAttrs().isEmpty();
489489
}
490490

491-
bool PropertyWrapperBackingPropertyInfoRequest::isCached() const {
491+
bool PropertyWrapperAuxiliaryVariablesRequest::isCached() const {
492+
auto var = std::get<0>(getStorage());
493+
return !var->getAttrs().isEmpty() || var->hasImplicitPropertyWrapper();
494+
}
495+
496+
bool PropertyWrapperInitializerInfoRequest::isCached() const {
492497
auto var = std::get<0>(getStorage());
493498
return !var->getAttrs().isEmpty() || var->hasImplicitPropertyWrapper();
494499
}
@@ -520,10 +525,21 @@ void swift::simple_display(
520525

521526
void swift::simple_display(
522527
llvm::raw_ostream &out,
523-
const PropertyWrapperBackingPropertyInfo &backingInfo) {
528+
const PropertyWrapperInitializerInfo &initInfo) {
529+
out << "{";
530+
if (initInfo.hasInitFromWrappedValue())
531+
initInfo.getInitFromWrappedValue()->dump(out);
532+
if (initInfo.hasInitFromProjectedValue())
533+
initInfo.getInitFromProjectedValue()->dump(out);
534+
out << " }";
535+
}
536+
537+
void swift::simple_display(
538+
llvm::raw_ostream &out,
539+
const PropertyWrapperAuxiliaryVariables &auxiliaryVars) {
524540
out << "{ ";
525-
if (backingInfo.backingVar)
526-
backingInfo.backingVar->dumpRef(out);
541+
if (auxiliaryVars.backingVar)
542+
auxiliaryVars.backingVar->dumpRef(out);
527543
out << " }";
528544
}
529545

0 commit comments

Comments
 (0)