Skip to content

Commit 21cbdfa

Browse files
committed
[Property Wrappers] Add a VarDecl helper method for visiting synthesized
property wrapper vars.
1 parent d8df621 commit 21cbdfa

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

include/swift/AST/Decl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5202,6 +5202,14 @@ class VarDecl : public AbstractStorageDecl {
52025202
/// property wrapper with a \c projectedValue .
52035203
VarDecl *getPropertyWrapperProjectionVar() const;
52045204

5205+
/// Visit all auxiliary declarations to this VarDecl.
5206+
///
5207+
/// An auxiliary declaration is a declaration synthesized by the compiler to support
5208+
/// this VarDecl, such as synthesized property wrapper variables.
5209+
///
5210+
/// \note this function only visits auxiliary decls that are not part of the AST.
5211+
void visitAuxiliaryDecls(llvm::function_ref<void(VarDecl *)>) const;
5212+
52055213
/// Retrieve the backing storage property for a lazy property.
52065214
VarDecl *getLazyStorageProperty() const;
52075215

lib/AST/Decl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5956,6 +5956,17 @@ VarDecl *VarDecl::getPropertyWrapperProjectionVar() const {
59565956
return getPropertyWrapperBackingPropertyInfo().projectionVar;
59575957
}
59585958

5959+
void VarDecl::visitAuxiliaryDecls(llvm::function_ref<void(VarDecl *)> visit) const {
5960+
if (!getDeclContext()->isLocalContext())
5961+
return;
5962+
5963+
if (auto *backingVar = getPropertyWrapperBackingProperty())
5964+
visit(backingVar);
5965+
5966+
if (auto *projectionVar = getPropertyWrapperProjectionVar())
5967+
visit(projectionVar);
5968+
}
5969+
59595970
VarDecl *VarDecl::getLazyStorageProperty() const {
59605971
auto &ctx = getASTContext();
59615972
auto mutableThis = const_cast<VarDecl *>(this);

lib/SILGen/SILGenDecl.cpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,13 +1195,6 @@ void SILGenFunction::emitPatternBinding(PatternBindingDecl *PBD,
11951195
}
11961196

11971197
void SILGenFunction::visitPatternBindingDecl(PatternBindingDecl *PBD) {
1198-
// Visit (local) property wrapper backing var first
1199-
auto *singleVar = PBD->getSingleVar();
1200-
if (singleVar && singleVar->hasAttachedPropertyWrapper() &&
1201-
singleVar->getDeclContext()->isLocalContext()) {
1202-
auto *backingVar = singleVar->getPropertyWrapperBackingProperty();
1203-
visitPatternBindingDecl(backingVar->getParentPatternBinding());
1204-
}
12051198

12061199
// Allocate the variables and build up an Initialization over their
12071200
// allocated storage.
@@ -1213,17 +1206,17 @@ void SILGenFunction::visitPatternBindingDecl(PatternBindingDecl *PBD) {
12131206
void SILGenFunction::visitVarDecl(VarDecl *D) {
12141207
// We handle emitting the variable storage when we see the pattern binding.
12151208

1216-
// Visit property wrapper synthesized accessors first.
1217-
if (D->hasAttachedPropertyWrapper() && D->getDeclContext()->isLocalContext()) {
1218-
auto wrapperInfo = D->getPropertyWrapperBackingPropertyInfo();
1219-
if (!wrapperInfo)
1220-
return;
1221-
1209+
// Emit the property wrapper backing initializer if necessary.
1210+
auto wrapperInfo = D->getPropertyWrapperBackingPropertyInfo();
1211+
if (wrapperInfo && wrapperInfo.initializeFromOriginal)
12221212
SGM.emitPropertyWrapperBackingInitializer(D);
1223-
visit(wrapperInfo.backingVar);
1224-
if (wrapperInfo.projectionVar)
1225-
visit(wrapperInfo.projectionVar);
1226-
}
1213+
1214+
D->visitAuxiliaryDecls([&](VarDecl *var) {
1215+
if (auto *patternBinding = var->getParentPatternBinding())
1216+
visitPatternBindingDecl(patternBinding);
1217+
1218+
visit(var);
1219+
});
12271220

12281221
// Emit the variable's accessors.
12291222
D->visitEmittedAccessors([&](AccessorDecl *accessor) {

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,16 +1418,10 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
14181418
(void) VD->getPropertyWrapperBackingProperty();
14191419
(void) VD->getImplInfo();
14201420

1421-
if (VD->hasAttachedPropertyWrapper() && VD->getDeclContext()->isLocalContext()) {
1422-
// If this is a local wrapped variable, visit the synthesized
1423-
// storage and projection first
1424-
auto wrapperInfo = VD->getPropertyWrapperBackingPropertyInfo();
1425-
assert(wrapperInfo);
1426-
1427-
visitBoundVariable(wrapperInfo.backingVar);
1428-
if (wrapperInfo.projectionVar)
1429-
visitBoundVariable(wrapperInfo.projectionVar);
1430-
}
1421+
// Visit auxiliary decls first
1422+
VD->visitAuxiliaryDecls([&](VarDecl *var) {
1423+
this->visitBoundVariable(var);
1424+
});
14311425

14321426
// Add the '@_hasStorage' attribute if this property is stored.
14331427
if (VD->hasStorage() && !VD->getAttrs().hasAttribute<HasStorageAttr>())

0 commit comments

Comments
 (0)