Skip to content

Commit d1e2ac1

Browse files
committed
Factor out SILDeclRef::getInitializationExpr
1 parent d0ffd29 commit d1e2ac1

File tree

3 files changed

+59
-23
lines changed

3 files changed

+59
-23
lines changed

include/swift/SIL/SILDeclRef.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,11 @@ struct SILDeclRef {
565565
/// subclassed.
566566
SubclassScope getSubclassScope() const;
567567

568+
/// For a SILDeclRef that describes a variable initializer or backing
569+
/// initializer, retrieves the expression that will be emitted for that
570+
/// initialization. Otherwise, returns \c nullptr.
571+
Expr *getInitializationExpr() const;
572+
568573
bool isDynamicallyReplaceable() const;
569574

570575
bool canBeDynamicReplacement() const;

lib/SIL/IR/SILDeclRef.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/AnyFunctionRef.h"
1717
#include "swift/AST/Initializer.h"
1818
#include "swift/AST/ParameterList.h"
19+
#include "swift/AST/PropertyWrappers.h"
1920
#include "swift/AST/SourceFile.h"
2021
#include "swift/ClangImporter/ClangImporter.h"
2122
#include "swift/ClangImporter/ClangModule.h"
@@ -1497,6 +1498,45 @@ SubclassScope SILDeclRef::getSubclassScope() const {
14971498
llvm_unreachable("Unhandled access level in switch.");
14981499
}
14991500

1501+
Expr *SILDeclRef::getInitializationExpr() const {
1502+
switch (kind) {
1503+
case Kind::StoredPropertyInitializer: {
1504+
auto *var = cast<VarDecl>(getDecl());
1505+
auto *pbd = var->getParentPatternBinding();
1506+
unsigned idx = pbd->getPatternEntryIndexForVarDecl(var);
1507+
auto *init = pbd->getInit(idx);
1508+
assert(!pbd->isInitializerSubsumed(idx));
1509+
1510+
// If this is the backing storage for a property with an attached wrapper
1511+
// that was initialized with `=`, use that expression as the initializer.
1512+
if (auto originalProperty = var->getOriginalWrappedProperty()) {
1513+
if (originalProperty->isPropertyMemberwiseInitializedWithWrappedType()) {
1514+
auto wrapperInfo =
1515+
originalProperty->getPropertyWrapperInitializerInfo();
1516+
auto *placeholder = wrapperInfo.getWrappedValuePlaceholder();
1517+
init = placeholder->getOriginalWrappedValue();
1518+
assert(init);
1519+
}
1520+
}
1521+
return init;
1522+
}
1523+
case Kind::PropertyWrapperBackingInitializer: {
1524+
auto *var = cast<VarDecl>(getDecl());
1525+
auto wrapperInfo = var->getPropertyWrapperInitializerInfo();
1526+
assert(wrapperInfo.hasInitFromWrappedValue());
1527+
return wrapperInfo.getInitFromWrappedValue();
1528+
}
1529+
case Kind::PropertyWrapperInitFromProjectedValue: {
1530+
auto *var = cast<VarDecl>(getDecl());
1531+
auto wrapperInfo = var->getPropertyWrapperInitializerInfo();
1532+
assert(wrapperInfo.hasInitFromProjectedValue());
1533+
return wrapperInfo.getInitFromProjectedValue();
1534+
}
1535+
default:
1536+
return nullptr;
1537+
}
1538+
}
1539+
15001540
unsigned SILDeclRef::getParameterListCount() const {
15011541
// Only decls can introduce currying.
15021542
if (!hasDecl())

lib/SILGen/SILGen.cpp

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -944,22 +944,10 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
944944

945945
auto *pbd = var->getParentPatternBinding();
946946
unsigned idx = pbd->getPatternEntryIndexForVarDecl(var);
947-
auto *init = pbd->getInit(idx);
948947
auto *initDC = pbd->getInitContext(idx);
949948
auto captureInfo = pbd->getCaptureInfo(idx);
950-
assert(!pbd->isInitializerSubsumed(idx));
951-
952-
// If this is the backing storage for a property with an attached wrapper
953-
// that was initialized with `=`, use that expression as the initializer.
954-
if (auto originalProperty = var->getOriginalWrappedProperty()) {
955-
if (originalProperty
956-
->isPropertyMemberwiseInitializedWithWrappedType()) {
957-
auto wrapperInfo =
958-
originalProperty->getPropertyWrapperInitializerInfo();
959-
assert(wrapperInfo.getWrappedValuePlaceholder()->getOriginalWrappedValue());
960-
init = wrapperInfo.getWrappedValuePlaceholder()->getOriginalWrappedValue();
961-
}
962-
}
949+
auto *init = constant.getInitializationExpr();
950+
assert(init);
963951

964952
auto loc = RegularLocation::getAutoGeneratedLocation(init);
965953
preEmitFunction(constant, f, loc);
@@ -989,13 +977,14 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
989977
preEmitFunction(constant, f, loc);
990978
PrettyStackTraceSILFunction X(
991979
"silgen emitPropertyWrapperBackingInitializer", f);
992-
auto wrapperInfo = var->getPropertyWrapperInitializerInfo();
993-
assert(wrapperInfo.hasInitFromWrappedValue());
994-
f->createProfiler(wrapperInfo.getInitFromWrappedValue(), constant);
980+
981+
auto *init = constant.getInitializationExpr();
982+
assert(init);
983+
984+
f->createProfiler(init, constant);
995985
auto varDC = var->getInnermostDeclContext();
996986
SILGenFunction SGF(*this, *f, varDC);
997-
SGF.emitGeneratorFunction(constant, wrapperInfo.getInitFromWrappedValue(),
998-
/*EmitProfilerIncrement*/ true);
987+
SGF.emitGeneratorFunction(constant, init, /*EmitProfilerIncrement*/ true);
999988
postEmitFunction(constant, f);
1000989
break;
1001990
}
@@ -1007,12 +996,14 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
1007996
preEmitFunction(constant, f, loc);
1008997
PrettyStackTraceSILFunction X(
1009998
"silgen emitPropertyWrapperInitFromProjectedValue", f);
1010-
auto initInfo = var->getPropertyWrapperInitializerInfo();
1011-
assert(initInfo.hasInitFromProjectedValue());
1012-
f->createProfiler(initInfo.getInitFromProjectedValue(), constant);
999+
1000+
auto *init = constant.getInitializationExpr();
1001+
assert(init);
1002+
1003+
f->createProfiler(init, constant);
10131004
auto varDC = var->getInnermostDeclContext();
10141005
SILGenFunction SGF(*this, *f, varDC);
1015-
SGF.emitGeneratorFunction(constant, initInfo.getInitFromProjectedValue());
1006+
SGF.emitGeneratorFunction(constant, init);
10161007
postEmitFunction(constant, f);
10171008
break;
10181009
}

0 commit comments

Comments
 (0)