Skip to content

Commit e585ec4

Browse files
committed
[SILGen] Record local auxiliary decls when a parameter is emitted, and
emit those auxiliary decls inside the function body brace statement. This generalizes the old code to work for parameters to any kind of function (e.g. initializers).
1 parent d4b52c8 commit e585ec4

File tree

4 files changed

+21
-23
lines changed

4 files changed

+21
-23
lines changed

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ struct ArgumentInitHelper {
244244
}
245245

246246
void emitParam(ParamDecl *PD) {
247+
PD->visitAuxiliaryDecls([&](VarDecl *localVar) {
248+
SGF.LocalAuxiliaryDecls.push_back(localVar);
249+
});
250+
247251
if (PD->hasExternalPropertyWrapper()) {
248252
PD = cast<ParamDecl>(PD->getPropertyWrapperBackingProperty());
249253
}

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*>())

0 commit comments

Comments
 (0)