Skip to content

Commit 4d2b5d4

Browse files
committed
emit apply
1 parent 588e82f commit 4d2b5d4

File tree

5 files changed

+100
-36
lines changed

5 files changed

+100
-36
lines changed

lib/SILGen/SILGen.cpp

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,16 +1060,39 @@ void SILGenModule::emitDestructor(ClassDecl *cd, DestructorDecl *dd) {
10601060
}
10611061
}
10621062

1063-
void SILGenModule::emitDefaultArgGenerator(SILDeclRef constant, Expr *arg,
1064-
DefaultArgumentKind kind,
1065-
DeclContext *initDC) {
1066-
switch (kind) {
1063+
void SILGenModule::emitDefaultArgGenerator(SILDeclRef constant,
1064+
ParamDecl *param) {
1065+
auto initDC = param->getDefaultArgumentInitContext();
1066+
1067+
switch (param->getDefaultArgumentKind()) {
10671068
case DefaultArgumentKind::None:
10681069
llvm_unreachable("No default argument here?");
10691070

1070-
case DefaultArgumentKind::Normal:
1071-
case DefaultArgumentKind::StoredProperty:
1072-
break;
1071+
case DefaultArgumentKind::Normal: {
1072+
auto arg = param->getDefaultValue();
1073+
emitOrDelayFunction(*this, constant,
1074+
[this,constant,arg,initDC](SILFunction *f) {
1075+
preEmitFunction(constant, arg, f, arg);
1076+
PrettyStackTraceSILFunction X("silgen emitDefaultArgGenerator ", f);
1077+
SILGenFunction SGF(*this, *f, initDC);
1078+
SGF.emitGeneratorFunction(constant, arg);
1079+
postEmitFunction(constant, f);
1080+
});
1081+
return;
1082+
}
1083+
1084+
case DefaultArgumentKind::StoredProperty: {
1085+
auto arg = param->getStoredProperty();
1086+
emitOrDelayFunction(*this, constant,
1087+
[this,constant,arg,initDC](SILFunction *f) {
1088+
preEmitFunction(constant, arg, f, arg);
1089+
PrettyStackTraceSILFunction X("silgen emitDefaultArgGenerator ", f);
1090+
SILGenFunction SGF(*this, *f, initDC);
1091+
SGF.emitGeneratorFunction(constant, arg);
1092+
postEmitFunction(constant, f);
1093+
});
1094+
return;
1095+
}
10731096

10741097
case DefaultArgumentKind::Inherited:
10751098
case DefaultArgumentKind::Column:
@@ -1082,15 +1105,6 @@ void SILGenModule::emitDefaultArgGenerator(SILDeclRef constant, Expr *arg,
10821105
case DefaultArgumentKind::EmptyDictionary:
10831106
return;
10841107
}
1085-
1086-
emitOrDelayFunction(*this, constant,
1087-
[this,constant,arg,initDC](SILFunction *f) {
1088-
preEmitFunction(constant, arg, f, arg);
1089-
PrettyStackTraceSILFunction X("silgen emitDefaultArgGenerator ", f);
1090-
SILGenFunction SGF(*this, *f, initDC);
1091-
SGF.emitGeneratorFunction(constant, arg);
1092-
postEmitFunction(constant, f);
1093-
});
10941108
}
10951109

10961110
void SILGenModule::
@@ -1159,15 +1173,9 @@ void SILGenModule::emitDefaultArgGenerators(SILDeclRef::Loc decl,
11591173
ParameterList *paramList) {
11601174
unsigned index = 0;
11611175
for (auto param : *paramList) {
1162-
auto defaultArg = param->getDefaultValue();
1163-
1164-
if (auto var = param->getStoredProperty())
1165-
defaultArg = var->getParentInitializer();
1166-
1167-
if (defaultArg)
1176+
if (param->isDefaultArgument())
11681177
emitDefaultArgGenerator(SILDeclRef::getDefaultArgGenerator(decl, index),
1169-
defaultArg, param->getDefaultArgumentKind(),
1170-
param->getDefaultArgumentInitContext());
1178+
param);
11711179
++index;
11721180
}
11731181
}

lib/SILGen/SILGen.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
240240
void emitEnumConstructor(EnumElementDecl *decl);
241241

242242
/// Emits the default argument generator with the given expression.
243-
void emitDefaultArgGenerator(SILDeclRef constant, Expr *arg,
244-
DefaultArgumentKind kind, DeclContext *DC);
243+
void emitDefaultArgGenerator(SILDeclRef constant, ParamDecl *param);
245244

246245
/// Emits the stored property initializer for the given pattern.
247246
void emitStoredPropertyInitialization(PatternBindingDecl *pd, unsigned i);

lib/SILGen/SILGenFunction.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,54 @@ void SILGenFunction::emitGeneratorFunction(SILDeclRef function, Expr *value) {
638638
emitEpilog(Loc);
639639
}
640640

641+
void SILGenFunction::emitGeneratorFunction(SILDeclRef function, VarDecl *var) {
642+
MagicFunctionName = SILGenModule::getMagicFunctionName(function);
643+
644+
RegularLocation loc(var);
645+
loc.markAutoGenerated();
646+
647+
auto decl = function.getAbstractFunctionDecl();
648+
auto *dc = decl->getInnermostDeclContext();
649+
auto interfaceType = var->getInterfaceType();
650+
emitProlog(/*paramList*/ nullptr, /*selfParam*/ nullptr, interfaceType, dc,
651+
false);
652+
prepareEpilog(var->getType(), false, CleanupLocation::get(loc));
653+
654+
auto pbd = var->getParentPatternBinding();
655+
auto entry = pbd->getPatternEntryForVarDecl(var);
656+
auto subs = getForwardingSubstitutionMap();
657+
auto resultType = decl->mapTypeIntoContext(interfaceType)->getCanonicalType();
658+
auto origResultType = AbstractionPattern(resultType);
659+
660+
if (F.getConventions().hasIndirectSILResults()) {
661+
Scope scope(Cleanups, CleanupLocation(var));
662+
663+
SmallVector<SILValue, 4> directResults;
664+
SmallVector<CleanupHandle, 4> cleanups;
665+
auto init = prepareIndirectResultInit(resultType, directResults, cleanups);
666+
667+
emitApplyOfStoredPropertyInitializer(loc, entry, subs, resultType,
668+
origResultType,
669+
SGFContext(init.get()));
670+
671+
for (auto cleanup : cleanups) {
672+
Cleanups.forwardCleanup(cleanup);
673+
}
674+
675+
Cleanups.emitBranchAndCleanups(ReturnDest, loc, directResults);
676+
} else {
677+
// If we have no indirect results, just return the result.
678+
auto result = emitApplyOfStoredPropertyInitializer(loc, entry, subs,
679+
resultType,
680+
origResultType,
681+
SGFContext())
682+
.getAsSingleValue(*this, loc);
683+
B.createReturn(loc, result);
684+
}
685+
686+
emitEpilog(loc);
687+
}
688+
641689
static SILLocation getLocation(ASTNode Node) {
642690
if (auto *E = Node.dyn_cast<Expr *>())
643691
return E;

lib/SILGen/SILGenFunction.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,11 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
554554
// be in a different scope.
555555
}
556556

557+
std::unique_ptr<Initialization>
558+
prepareIndirectResultInit(CanType formalResultType,
559+
SmallVectorImpl<SILValue> &directResultsBuffer,
560+
SmallVectorImpl<CleanupHandle> &cleanups);
561+
557562
//===--------------------------------------------------------------------===//
558563
// Entry points for codegen
559564
//===--------------------------------------------------------------------===//
@@ -624,6 +629,10 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
624629
/// Generate a nullary function that returns the given value.
625630
void emitGeneratorFunction(SILDeclRef function, Expr *value);
626631

632+
/// Generate a nullary function that returns the value of the given variable's
633+
/// expression initializer.
634+
void emitGeneratorFunction(SILDeclRef function, VarDecl *var);
635+
627636
/// Generate an ObjC-compatible destructor (-dealloc).
628637
void emitObjCDestructor(SILDeclRef dtor);
629638

lib/SILGen/SILGenStmt.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -425,22 +425,22 @@ prepareIndirectResultInit(SILGenFunction &SGF, CanType resultType,
425425
/// components of the result
426426
/// \param cleanups - will be filled (after initialization completes)
427427
/// with all the active cleanups managing the result values
428-
static std::unique_ptr<Initialization>
429-
prepareIndirectResultInit(SILGenFunction &SGF, CanType formalResultType,
430-
SmallVectorImpl<SILValue> &directResultsBuffer,
431-
SmallVectorImpl<CleanupHandle> &cleanups) {
432-
auto fnConv = SGF.F.getConventions();
428+
std::unique_ptr<Initialization>
429+
SILGenFunction::prepareIndirectResultInit(CanType formalResultType,
430+
SmallVectorImpl<SILValue> &directResultsBuffer,
431+
SmallVectorImpl<CleanupHandle> &cleanups) {
432+
auto fnConv = F.getConventions();
433433

434434
// Make space in the direct-results array for all the entries we need.
435435
directResultsBuffer.append(fnConv.getNumDirectSILResults(), SILValue());
436436

437437
ArrayRef<SILResultInfo> allResults = fnConv.funcTy->getResults();
438438
MutableArrayRef<SILValue> directResults = directResultsBuffer;
439-
ArrayRef<SILArgument*> indirectResultAddrs = SGF.F.getIndirectResults();
439+
ArrayRef<SILArgument*> indirectResultAddrs = F.getIndirectResults();
440440

441-
auto init = prepareIndirectResultInit(SGF, formalResultType, allResults,
442-
directResults, indirectResultAddrs,
443-
cleanups);
441+
auto init = ::prepareIndirectResultInit(*this, formalResultType, allResults,
442+
directResults, indirectResultAddrs,
443+
cleanups);
444444

445445
assert(allResults.empty());
446446
assert(directResults.empty());
@@ -460,7 +460,7 @@ void SILGenFunction::emitReturnExpr(SILLocation branchLoc,
460460
// Build an initialization which recursively destructures the tuple.
461461
SmallVector<CleanupHandle, 4> resultCleanups;
462462
InitializationPtr resultInit =
463-
prepareIndirectResultInit(*this, ret->getType()->getCanonicalType(),
463+
prepareIndirectResultInit(ret->getType()->getCanonicalType(),
464464
directResults, resultCleanups);
465465

466466
// Emit the result expression into the initialization.

0 commit comments

Comments
 (0)