Skip to content

Commit be95bf6

Browse files
authored
Merge pull request #3453 from swiftwasm/release/5.5
[pull] swiftwasm-release/5.5 from release/5.5
2 parents ede19bc + 43aaf29 commit be95bf6

File tree

10 files changed

+175
-77
lines changed

10 files changed

+175
-77
lines changed

lib/AST/Parameter.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ ParameterList *ParameterList::clone(const ASTContext &C,
6060

6161
// Remap the ParamDecls inside of the ParameterList.
6262
for (auto &decl : params) {
63-
bool hadDefaultArgument =
64-
decl->getDefaultArgumentKind() == DefaultArgumentKind::Normal;
63+
auto defaultArgKind = decl->getDefaultArgumentKind();
6564

6665
decl = ParamDecl::cloneWithoutType(C, decl);
6766
if (options & Implicit)
@@ -74,11 +73,18 @@ ParameterList *ParameterList::clone(const ASTContext &C,
7473

7574
// If we're inheriting a default argument, mark it as such.
7675
// FIXME: Figure out how to clone default arguments as well.
77-
if (hadDefaultArgument) {
78-
if (options & Inherited)
76+
if (options & Inherited) {
77+
switch (defaultArgKind) {
78+
case DefaultArgumentKind::Normal:
79+
case DefaultArgumentKind::StoredProperty:
7980
decl->setDefaultArgumentKind(DefaultArgumentKind::Inherited);
80-
else
81-
decl->setDefaultArgumentKind(DefaultArgumentKind::None);
81+
break;
82+
83+
default:
84+
break;
85+
}
86+
} else {
87+
decl->setDefaultArgumentKind(DefaultArgumentKind::None);
8288
}
8389
}
8490

lib/SILGen/SILGen.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,8 +1299,8 @@ emitMarkFunctionEscapeForTopLevelCodeGlobals(SILLocation loc,
12991299
}
13001300

13011301
void SILGenModule::emitAbstractFuncDecl(AbstractFunctionDecl *AFD) {
1302-
// Emit any default argument generators.
1303-
emitDefaultArgGenerators(AFD, AFD->getParameters());
1302+
// Emit default arguments and property wrapper initializers.
1303+
emitArgumentGenerators(AFD, AFD->getParameters());
13041304

13051305
// If this is a function at global scope, it may close over a global variable.
13061306
// If we're emitting top-level code, then emit a "mark_function_escape" that
@@ -1389,6 +1389,9 @@ SILFunction *SILGenModule::emitClosure(AbstractClosureExpr *ce) {
13891389
if (!f->isExternalDeclaration())
13901390
return f;
13911391

1392+
// Emit property wrapper argument generators.
1393+
emitArgumentGenerators(ce, ce->getParameters());
1394+
13921395
emitFunctionDefinition(constant, f);
13931396
return f;
13941397
}
@@ -1587,13 +1590,17 @@ void SILGenModule::emitGlobalAccessor(VarDecl *global,
15871590
emitOrDelayFunction(*this, accessor);
15881591
}
15891592

1590-
void SILGenModule::emitDefaultArgGenerators(SILDeclRef::Loc decl,
1591-
ParameterList *paramList) {
1593+
void SILGenModule::emitArgumentGenerators(SILDeclRef::Loc decl,
1594+
ParameterList *paramList) {
15921595
unsigned index = 0;
15931596
for (auto param : *paramList) {
15941597
if (param->isDefaultArgument())
15951598
emitDefaultArgGenerator(SILDeclRef::getDefaultArgGenerator(decl, index),
15961599
param);
1600+
1601+
if (param->hasExternalPropertyWrapper())
1602+
emitPropertyWrapperBackingInitializer(param);
1603+
15971604
++index;
15981605
}
15991606
}

lib/SILGen/SILGen.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,9 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
321321
/// Emits the backing initializer for a property with an attached wrapper.
322322
void emitPropertyWrapperBackingInitializer(VarDecl *var);
323323

324-
/// Emits default argument generators for the given parameter list.
325-
void emitDefaultArgGenerators(SILDeclRef::Loc decl,
326-
ParameterList *paramList);
324+
/// Emits argument generators, including default argument generators and
325+
/// property wrapper argument generators, for the given parameter list.
326+
void emitArgumentGenerators(SILDeclRef::Loc decl, ParameterList *paramList);
327327

328328
/// Emits a thunk from a foreign function to the native Swift convention.
329329
void emitForeignToNativeThunk(SILDeclRef thunk);

lib/SILGen/SILGenFunction.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -517,23 +517,6 @@ void SILGenFunction::emitFunction(FuncDecl *fd) {
517517
// original asyncHandler.
518518
!F.isAsync()) {
519519
emitAsyncHandler(fd);
520-
} else if (llvm::any_of(*fd->getParameters(),
521-
[](ParamDecl *p){ return p->hasAttachedPropertyWrapper(); })) {
522-
// If any parameters have property wrappers, emit the local auxiliary
523-
// variables before emitting the function body.
524-
LexicalScope BraceScope(*this, CleanupLocation(fd));
525-
for (auto *param : *fd->getParameters()) {
526-
param->visitAuxiliaryDecls([&](VarDecl *auxiliaryVar) {
527-
SILLocation WrapperLoc(auxiliaryVar);
528-
WrapperLoc.markAsPrologue();
529-
if (auto *patternBinding = auxiliaryVar->getParentPatternBinding())
530-
visitPatternBindingDecl(patternBinding);
531-
532-
visit(auxiliaryVar);
533-
});
534-
}
535-
536-
emitStmt(fd->getTypecheckedBody());
537520
} else {
538521
emitStmt(fd->getTypecheckedBody());
539522
}
@@ -606,11 +589,6 @@ void SILGenFunction::emitClosure(AbstractClosureExpr *ace) {
606589
emitProlog(captureInfo, ace->getParameters(), /*selfParam=*/nullptr,
607590
ace, resultIfaceTy, ace->isBodyThrowing(), ace->getLoc());
608591
prepareEpilog(true, ace->isBodyThrowing(), CleanupLocation(ace));
609-
for (auto *param : *ace->getParameters()) {
610-
param->visitAuxiliaryDecls([&](VarDecl *auxiliaryVar) {
611-
visit(auxiliaryVar);
612-
});
613-
}
614592

615593
if (auto *ce = dyn_cast<ClosureExpr>(ace)) {
616594
emitStmt(ce->getBody());

lib/SILGen/SILGenFunction.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
376376
/// a local variable.
377377
llvm::DenseMap<ValueDecl*, VarLoc> VarLocs;
378378

379+
/// The local auxiliary declarations for the parameters of this function that
380+
/// need to be emitted inside the next brace statement.
381+
llvm::SmallVector<VarDecl *, 2> LocalAuxiliaryDecls;
382+
379383
// Context information for tracking an `async let` child task.
380384
struct AsyncLetChildTask {
381385
SILValue asyncLet; // RawPointer to the async let state

lib/SILGen/SILGenProlog.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,11 @@ struct ArgumentInitHelper {
244244
}
245245

246246
void emitParam(ParamDecl *PD) {
247-
if (PD->hasExternalPropertyWrapper()) {
248-
auto initInfo = PD->getPropertyWrapperInitializerInfo();
249-
if (initInfo.hasSynthesizedInitializers()) {
250-
SGF.SGM.emitPropertyWrapperBackingInitializer(PD);
251-
}
247+
PD->visitAuxiliaryDecls([&](VarDecl *localVar) {
248+
SGF.LocalAuxiliaryDecls.push_back(localVar);
249+
});
252250

251+
if (PD->hasExternalPropertyWrapper()) {
253252
PD = cast<ParamDecl>(PD->getPropertyWrapperBackingProperty());
254253
}
255254

@@ -307,6 +306,10 @@ static void makeArgument(Type ty, ParamDecl *decl,
307306

308307
void SILGenFunction::bindParameterForForwarding(ParamDecl *param,
309308
SmallVectorImpl<SILValue> &parameters) {
309+
if (param->hasExternalPropertyWrapper()) {
310+
param = cast<ParamDecl>(param->getPropertyWrapperBackingProperty());
311+
}
312+
310313
makeArgument(param->getType(), param, parameters, *this);
311314
}
312315

lib/SILGen/SILGenStmt.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,19 @@ void StmtEmitter::visitBraceStmt(BraceStmt *S) {
282282
const unsigned ThrowStmtType = 3;
283283
const unsigned UnknownStmtType = 4;
284284
unsigned StmtType = UnknownStmtType;
285-
285+
286+
// Emit local auxiliary declarations.
287+
if (!SGF.LocalAuxiliaryDecls.empty()) {
288+
for (auto *var : SGF.LocalAuxiliaryDecls) {
289+
if (auto *patternBinding = var->getParentPatternBinding())
290+
SGF.visit(patternBinding);
291+
292+
SGF.visit(var);
293+
}
294+
295+
SGF.LocalAuxiliaryDecls.clear();
296+
}
297+
286298
for (auto &ESD : S->getElements()) {
287299

288300
if (auto D = ESD.dyn_cast<Decl*>())

lib/SILGen/SILGenType.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ class SILGenType : public TypeMemberVisitor<SILGenType> {
11061106
return;
11071107

11081108
// Emit any default argument generators.
1109-
SGM.emitDefaultArgGenerators(EED, EED->getParameterList());
1109+
SGM.emitArgumentGenerators(EED, EED->getParameterList());
11101110
}
11111111

11121112
void visitPatternBindingDecl(PatternBindingDecl *pd) {
@@ -1141,7 +1141,7 @@ class SILGenType : public TypeMemberVisitor<SILGenType> {
11411141
}
11421142

11431143
void visitSubscriptDecl(SubscriptDecl *sd) {
1144-
SGM.emitDefaultArgGenerators(sd, sd->getIndices());
1144+
SGM.emitArgumentGenerators(sd, sd->getIndices());
11451145
visitAbstractStorageDecl(sd);
11461146
}
11471147

@@ -1268,7 +1268,7 @@ class SILGenExtension : public TypeMemberVisitor<SILGenExtension> {
12681268
}
12691269

12701270
void visitSubscriptDecl(SubscriptDecl *sd) {
1271-
SGM.emitDefaultArgGenerators(sd, sd->getIndices());
1271+
SGM.emitArgumentGenerators(sd, sd->getIndices());
12721272
visitAbstractStorageDecl(sd);
12731273
}
12741274

test/SILGen/default_arguments.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,23 @@ func testCallableWithDefault(_ x: CallableWithDefault) {
434434
x(y: 5)
435435
}
436436

437+
enum E {
438+
// CHECK-LABEL: sil hidden [ossa] @$s17default_arguments1EO6ResultV4name9platformsAESS_SaySiGtcfcfA0_ : $@convention(thin) () -> @owned Array<Int>
439+
struct Result {
440+
var name: String
441+
var platforms: [Int] = []
442+
}
443+
444+
// CHECK-LABEL: sil hidden [ossa] @$s17default_arguments1EO4testyyFZ : $@convention(method) (@thin E.Type) -> ()
445+
static func test() {
446+
// CHECK: function_ref @$s17default_arguments1EO6ResultV4name9platformsAESS_SaySiGtcfcfA0_ : $@convention(thin) () -> @owned Array<Int>
447+
// CHECK: function_ref @$s17default_arguments1EO4testyyFZAC6ResultVSS_SaySiGtcfu_ : $@convention(thin) (@guaranteed String, @guaranteed Array<Int>) -> @owned E.Result
448+
449+
// CHECK-LABEL: sil private [ossa] @$s17default_arguments1EO4testyyFZAC6ResultVSS_SaySiGtcfu_ : $@convention(thin) (@guaranteed String, @guaranteed Array<Int>) -> @owned E.Result
450+
var result = Self.Result(name: "")
451+
}
452+
}
453+
437454
// FIXME: Arguably we shouldn't allow calling a constructor like this, as
438455
// we usually require the user write an explicit '.init'.
439456
struct WeirdUMEInitCase {

0 commit comments

Comments
 (0)