Skip to content

Commit 588e82f

Browse files
committed
Emit default arg generator for stored property kind
1 parent 8975875 commit 588e82f

File tree

5 files changed

+32
-40
lines changed

5 files changed

+32
-40
lines changed

lib/SILGen/SILGen.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,11 +1068,10 @@ void SILGenModule::emitDefaultArgGenerator(SILDeclRef constant, Expr *arg,
10681068
llvm_unreachable("No default argument here?");
10691069

10701070
case DefaultArgumentKind::Normal:
1071+
case DefaultArgumentKind::StoredProperty:
10711072
break;
10721073

10731074
case DefaultArgumentKind::Inherited:
1074-
return;
1075-
10761075
case DefaultArgumentKind::Column:
10771076
case DefaultArgumentKind::File:
10781077
case DefaultArgumentKind::Line:
@@ -1081,7 +1080,6 @@ void SILGenModule::emitDefaultArgGenerator(SILDeclRef constant, Expr *arg,
10811080
case DefaultArgumentKind::NilLiteral:
10821081
case DefaultArgumentKind::EmptyArray:
10831082
case DefaultArgumentKind::EmptyDictionary:
1084-
case DefaultArgumentKind::StoredProperty:
10851083
return;
10861084
}
10871085

@@ -1161,7 +1159,12 @@ void SILGenModule::emitDefaultArgGenerators(SILDeclRef::Loc decl,
11611159
ParameterList *paramList) {
11621160
unsigned index = 0;
11631161
for (auto param : *paramList) {
1164-
if (auto defaultArg = param->getDefaultValue())
1162+
auto defaultArg = param->getDefaultValue();
1163+
1164+
if (auto var = param->getStoredProperty())
1165+
defaultArg = var->getParentInitializer();
1166+
1167+
if (defaultArg)
11651168
emitDefaultArgGenerator(SILDeclRef::getDefaultArgGenerator(decl, index),
11661169
defaultArg, param->getDefaultArgumentKind(),
11671170
param->getDefaultArgumentInitContext());

lib/SILGen/SILGenApply.cpp

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3186,40 +3186,11 @@ void DelayedArgument::emitDefaultArgument(SILGenFunction &SGF,
31863186
const DefaultArgumentStorage &info,
31873187
SmallVectorImpl<ManagedValue> &args,
31883188
size_t &argIndex) {
3189-
RValue value;
3190-
bool isStoredPropertyDefaultArg = false;
3191-
3192-
// Check if this is a synthesized memberwise constructor for stored property
3193-
// default values
3194-
if (auto ctor = dyn_cast<ConstructorDecl>(info.defaultArgsOwner.getDecl())) {
3195-
if (ctor->isMemberwiseInitializer() && ctor->isImplicit()) {
3196-
auto param = ctor->getParameters()->get(info.destIndex);
3197-
3198-
if (auto var = param->getStoredProperty()) {
3199-
// This is a stored property default arg. Do not emit a call to the
3200-
// default arg generator, but rather the variable initializer expression
3201-
isStoredPropertyDefaultArg = true;
3202-
3203-
auto pbd = var->getParentPatternBinding();
3204-
auto entry = pbd->getPatternEntryForVarDecl(var);
3205-
auto subs = info.defaultArgsOwner.getSubstitutions();
3206-
3207-
value = SGF.emitApplyOfStoredPropertyInitializer(info.loc,
3208-
entry, subs,
3209-
info.resultType,
3210-
info.origResultType,
3211-
SGFContext());
3212-
}
3213-
}
3214-
}
3215-
3216-
if (!isStoredPropertyDefaultArg) {
3217-
value = SGF.emitApplyOfDefaultArgGenerator(info.loc,
3218-
info.defaultArgsOwner,
3219-
info.destIndex,
3220-
info.resultType,
3221-
info.origResultType);
3222-
}
3189+
auto value = SGF.emitApplyOfDefaultArgGenerator(info.loc,
3190+
info.defaultArgsOwner,
3191+
info.destIndex,
3192+
info.resultType,
3193+
info.origResultType);
32233194

32243195
SmallVector<ManagedValue, 4> loweredArgs;
32253196
SmallVector<DelayedArgument, 4> delayedArgs;

test/SILGen/stored_property_default_arg.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ func checkConcreteType() {
1717
// CHECK-NEXT: {{.*}} = apply [[A1_REF]]({{.*}}, [[C1]], {{.*}}) : $@convention(method) (Int, Bool, @thin A.Type) -> A
1818
let d = A(b: 1)
1919

20-
// CHECK: function_ref variable initialization expression of A.b
21-
// CHECK-NEXT: [[B1_REF:%.*]] = function_ref @$s27stored_property_default_arg1AV1bSivpfi : $@convention(thin) () -> Int
20+
// CHECK: function_ref default argument 0 of A.init(b:c:)
21+
// CHECK-NEXT: [[B1_REF:%.*]] = function_ref @$s27stored_property_default_arg1AV1b1cACSi_SbtcfcfA_ : $@convention(thin) () -> Int
2222
// CHECK-NEXT: [[B1:%.*]] = apply [[B1_REF]]() : $@convention(thin) () -> Int
2323
// CHECK-NEXT: function_ref A.init(b:c:)
2424
// CHECK-NEXT: [[A2_REF:%.*]] = function_ref @$s27stored_property_default_arg1AV1b1cACSi_SbtcfC : $@convention(method) (Int, Bool, @thin A.Type) -> A
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
struct Foo {
2+
var bar = 0
3+
var baz = false
4+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-module -enable-testing -o %t %S/Inputs/stored_property_default_arg_testable.swift
3+
// RUN: %target-swift-frontend -emit-silgen -I %t %s | %FileCheck %s
4+
5+
@testable import stored_property_default_arg_testable
6+
7+
// CHECK: function_ref default argument 0 of Foo.init(bar:baz:)
8+
// CHECK-NEXT: [[BAR_REF:%.*]] = function_ref @$s36stored_property_default_arg_testable3FooV3bar3bazACSi_SbtcfcfA_ : $@convention(thin) () -> Int
9+
// CHECK-NEXT: [[BAR:%.*]] = apply [[BAR_REF]]() : $@convention(thin) () -> Int
10+
// CHECK-NEXT: function_ref Foo.init(bar:baz:)
11+
// CHECK-NEXT: [[FOO_REF:%.*]] = function_ref @$s36stored_property_default_arg_testable3FooV3bar3bazACSi_SbtcfC : $@convention(method) (Int, Bool, @thin Foo.Type) -> Foo
12+
// CHECK-NEXT: {{.*}} = apply [[FOO_REF]]([[BAR]], {{.*}}, {{.*}}) : $@convention(method) (Int, Bool, @thin Foo.Type) -> Foo
13+
14+
_ = Foo(baz: true)

0 commit comments

Comments
 (0)