Skip to content

Commit 3ca1292

Browse files
authored
Merge pull request #36521 from hborla/property-wrapper-request-refactoring
[Property Wrappers] Rework the dependencies between property wrapper requests.
2 parents e0b84da + c39d6fd commit 3ca1292

28 files changed

+334
-308
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: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,39 @@ 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+
/// The synthesized local wrapped value property, which shadows the original wrapped
185+
/// declaration if it is a parameter.
186+
VarDecl *localWrappedValueVar = nullptr;
187+
188+
PropertyWrapperAuxiliaryVariables() {}
189+
190+
PropertyWrapperAuxiliaryVariables(VarDecl *backingVar, VarDecl *projectionVar,
191+
VarDecl *localWrappedValueVar = nullptr)
192+
: backingVar(backingVar), projectionVar(projectionVar),
193+
localWrappedValueVar(localWrappedValueVar) {}
194+
195+
/// Whether this is a valid property wrapper.
196+
bool isValid() const {
197+
return backingVar != nullptr;
198+
}
199+
200+
explicit operator bool() const { return isValid(); }
201+
};
202+
203+
/// Describes how to initialize the backing storage of a property with
204+
/// an attached wrapper.
205+
class PropertyWrapperInitializerInfo {
183206
struct {
184207
/// An expression that initializes the backing property from a value of
185208
/// the original property's type via \c init(wrappedValue:) if supported
@@ -203,15 +226,10 @@ struct PropertyWrapperBackingPropertyInfo {
203226
} projectedValueInit;
204227

205228
public:
206-
PropertyWrapperBackingPropertyInfo() { }
207-
208-
PropertyWrapperBackingPropertyInfo(VarDecl *backingVar, VarDecl *projectionVar)
209-
: backingVar(backingVar), projectionVar(projectionVar) { }
229+
PropertyWrapperInitializerInfo() { }
210230

211-
PropertyWrapperBackingPropertyInfo(VarDecl *backingVar, VarDecl *projectionVar,
212-
Expr *wrappedValueInitExpr,
213-
Expr *projectedValueInitExpr)
214-
: backingVar(backingVar), projectionVar(projectionVar) {
231+
PropertyWrapperInitializerInfo(Expr *wrappedValueInitExpr,
232+
Expr *projectedValueInitExpr) {
215233
wrappedValueInit.expr = wrappedValueInitExpr;
216234
if (wrappedValueInitExpr) {
217235
wrappedValueInit.placeholder = findWrappedValuePlaceholder(wrappedValueInitExpr);
@@ -223,16 +241,11 @@ struct PropertyWrapperBackingPropertyInfo {
223241
}
224242
}
225243

226-
/// Whether this is a valid property wrapper.
227-
bool isValid() const {
228-
return backingVar != nullptr;
229-
}
230-
231244
bool hasInitFromWrappedValue() const {
232245
return wrappedValueInit.expr != nullptr;
233246
}
234247

235-
Expr *getInitFromWrappedValue() {
248+
Expr *getInitFromWrappedValue() const {
236249
return wrappedValueInit.expr;
237250
}
238251

@@ -244,7 +257,7 @@ struct PropertyWrapperBackingPropertyInfo {
244257
return projectedValueInit.expr != nullptr;
245258
}
246259

247-
Expr *getInitFromProjectedValue() {
260+
Expr *getInitFromProjectedValue() const {
248261
return projectedValueInit.expr;
249262
}
250263

@@ -255,14 +268,6 @@ struct PropertyWrapperBackingPropertyInfo {
255268
bool hasSynthesizedInitializers() const {
256269
return hasInitFromWrappedValue() || hasInitFromProjectedValue();
257270
}
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-
}
266271
};
267272

268273
void simple_display(
@@ -271,7 +276,11 @@ void simple_display(
271276

272277
void simple_display(
273278
llvm::raw_ostream &out,
274-
const PropertyWrapperBackingPropertyInfo &backingInfo);
279+
const PropertyWrapperInitializerInfo &initInfo);
280+
281+
void simple_display(
282+
llvm::raw_ostream &out,
283+
const PropertyWrapperAuxiliaryVariables &auxiliaryVars);
275284

276285
} // end namespace swift
277286

include/swift/AST/TypeCheckRequests.h

Lines changed: 13 additions & 13 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,19 +724,19 @@ class PropertyWrapperBackingPropertyInfoRequest :
725724
friend SimpleRequest;
726725

727726
// Evaluation.
728-
PropertyWrapperBackingPropertyInfo
727+
PropertyWrapperAuxiliaryVariables
729728
evaluate(Evaluator &evaluator, VarDecl *var) const;
730729

731730
public:
732731
// Caching
733732
bool isCached() const;
734733
};
735734

736-
/// Request the synthesized local wrapped value var for a parameter
737-
/// that has an attached property wrapper.
738-
class PropertyWrapperWrappedValueVarRequest :
739-
public SimpleRequest<PropertyWrapperWrappedValueVarRequest,
740-
VarDecl *(VarDecl *),
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 *),
741740
RequestFlags::Cached> {
742741
public:
743742
using SimpleRequest::SimpleRequest;
@@ -746,7 +745,8 @@ class PropertyWrapperWrappedValueVarRequest :
746745
friend SimpleRequest;
747746

748747
// Evaluation.
749-
VarDecl *evaluate(Evaluator &evaluator, VarDecl *var) const;
748+
PropertyWrapperInitializerInfo
749+
evaluate(Evaluator &evaluator, VarDecl *var) const;
750750

751751
public:
752752
// Caching

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ SWIFT_REQUEST(TypeChecker, PatternBindingEntryRequest,
189189
SeparatelyCached, NoLocationInfo)
190190
SWIFT_REQUEST(TypeChecker, PrimarySourceFilesRequest,
191191
ArrayRef<SourceFile *>(ModuleDecl *), Cached, NoLocationInfo)
192-
SWIFT_REQUEST(TypeChecker, PropertyWrapperBackingPropertyInfoRequest,
193-
PropertyWrapperBackingPropertyInfo(VarDecl *), Cached,
192+
SWIFT_REQUEST(TypeChecker, PropertyWrapperAuxiliaryVariablesRequest,
193+
PropertyWrapperAuxiliaryVariables(VarDecl *), Cached,
194194
NoLocationInfo)
195-
SWIFT_REQUEST(TypeChecker, PropertyWrapperWrappedValueVarRequest,
196-
VarDecl *(VarDecl *), Cached,
195+
SWIFT_REQUEST(TypeChecker, PropertyWrapperInitializerInfoRequest,
196+
PropertyWrapperInitializerInfo(VarDecl *), Cached,
197197
NoLocationInfo)
198198
SWIFT_REQUEST(TypeChecker, PropertyWrapperBackingPropertyTypeRequest,
199199
Type(VarDecl *), Cached, NoLocationInfo)

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: 20 additions & 15 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,20 +5973,15 @@ 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 {
5974-
auto &ctx = getASTContext();
5975-
auto mutableThis = const_cast<VarDecl *>(this);
5976-
return evaluateOrDefault(
5977-
ctx.evaluator,
5978-
PropertyWrapperWrappedValueVarRequest{mutableThis},
5979-
nullptr);
5984+
return getPropertyWrapperAuxiliaryVariables().localWrappedValueVar;
59805985
}
59815986

59825987
void VarDecl::visitAuxiliaryDecls(llvm::function_ref<void(VarDecl *)> visit) const {
@@ -6038,11 +6043,11 @@ bool VarDecl::isPropertyMemberwiseInitializedWithWrappedType() const {
60386043
}
60396044

60406045
Type VarDecl::getPropertyWrapperInitValueInterfaceType() const {
6041-
auto wrapperInfo = getPropertyWrapperBackingPropertyInfo();
6042-
if (!wrapperInfo || !wrapperInfo.getWrappedValuePlaceholder())
6046+
auto initInfo = getPropertyWrapperInitializerInfo();
6047+
if (!initInfo.getWrappedValuePlaceholder())
60436048
return Type();
60446049

6045-
Type valueInterfaceTy = wrapperInfo.getWrappedValuePlaceholder()->getType();
6050+
Type valueInterfaceTy = initInfo.getWrappedValuePlaceholder()->getType();
60466051
if (valueInterfaceTy->hasArchetype())
60476052
valueInterfaceTy = valueInterfaceTy->mapTypeOutOfContext();
60486053

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: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -485,15 +485,16 @@ bool AttachedPropertyWrapperTypeRequest::isCached() const {
485485

486486
bool PropertyWrapperBackingPropertyTypeRequest::isCached() const {
487487
auto var = std::get<0>(getStorage());
488-
return !var->getAttrs().isEmpty();
488+
return !var->getAttrs().isEmpty() &&
489+
!(isa<ParamDecl>(var) && isa<ClosureExpr>(var->getDeclContext()));
489490
}
490491

491-
bool PropertyWrapperBackingPropertyInfoRequest::isCached() const {
492+
bool PropertyWrapperAuxiliaryVariablesRequest::isCached() const {
492493
auto var = std::get<0>(getStorage());
493494
return !var->getAttrs().isEmpty() || var->hasImplicitPropertyWrapper();
494495
}
495496

496-
bool PropertyWrapperWrappedValueVarRequest::isCached() const {
497+
bool PropertyWrapperInitializerInfoRequest::isCached() const {
497498
auto var = std::get<0>(getStorage());
498499
return !var->getAttrs().isEmpty() || var->hasImplicitPropertyWrapper();
499500
}
@@ -520,10 +521,21 @@ void swift::simple_display(
520521

521522
void swift::simple_display(
522523
llvm::raw_ostream &out,
523-
const PropertyWrapperBackingPropertyInfo &backingInfo) {
524+
const PropertyWrapperInitializerInfo &initInfo) {
525+
out << "{";
526+
if (initInfo.hasInitFromWrappedValue())
527+
initInfo.getInitFromWrappedValue()->dump(out);
528+
if (initInfo.hasInitFromProjectedValue())
529+
initInfo.getInitFromProjectedValue()->dump(out);
530+
out << " }";
531+
}
532+
533+
void swift::simple_display(
534+
llvm::raw_ostream &out,
535+
const PropertyWrapperAuxiliaryVariables &auxiliaryVars) {
524536
out << "{ ";
525-
if (backingInfo.backingVar)
526-
backingInfo.backingVar->dumpRef(out);
537+
if (auxiliaryVars.backingVar)
538+
auxiliaryVars.backingVar->dumpRef(out);
527539
out << " }";
528540
}
529541

0 commit comments

Comments
 (0)