Skip to content

Commit 91042d7

Browse files
committed
[SILGen] Make it possible to emit generator function given its body and result type
This would be used for runtime attribute generators that have to emit `if #available(...)` block in the body which is much easier to do during Sema.
1 parent bcaaa91 commit 91042d7

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

lib/SILGen/SILGenFunction.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,28 @@ void SILGenFunction::emitGeneratorFunction(SILDeclRef function, VarDecl *var) {
11531153
emitEpilog(loc);
11541154
}
11551155

1156+
void SILGenFunction::emitGeneratorFunction(SILDeclRef function,
1157+
Type resultInterfaceType,
1158+
BraceStmt *body) {
1159+
MagicFunctionName = SILGenModule::getMagicFunctionName(function);
1160+
1161+
RegularLocation loc(body);
1162+
loc.markAutoGenerated();
1163+
1164+
auto *dc = function.getDecl()->getInnermostDeclContext();
1165+
auto captureInfo = SGM.M.Types.getLoweredLocalCaptures(function);
1166+
emitProlog(captureInfo, ParameterList::createEmpty(getASTContext()),
1167+
/*selfParam=*/nullptr, dc, resultInterfaceType, /*throws=*/false,
1168+
SourceLoc());
1169+
1170+
prepareEpilog(resultInterfaceType, /*hasThrows=*/false, CleanupLocation(loc));
1171+
1172+
emitStmt(body);
1173+
1174+
emitEpilog(loc);
1175+
mergeCleanupBlocks();
1176+
}
1177+
11561178
void SILGenFunction::emitProfilerIncrement(ASTNode Node) {
11571179
emitProfilerIncrement(ProfileCounterRef::node(Node));
11581180
}

lib/SILGen/SILGenFunction.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,11 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
756756
/// expression initializer.
757757
void emitGeneratorFunction(SILDeclRef function, VarDecl *var);
758758

759+
/// Generate a nullary function that has the given result interface type and
760+
/// body.
761+
void emitGeneratorFunction(SILDeclRef function, Type resultInterfaceType,
762+
BraceStmt *body);
763+
759764
/// Generate an ObjC-compatible destructor (-dealloc).
760765
void emitObjCDestructor(SILDeclRef dtor);
761766

0 commit comments

Comments
 (0)