Skip to content

Commit 09bfd94

Browse files
authored
Merge pull request swiftlang#29133 from DougGregor/generlize-function-builders
Reimplement function builders as statement transformations.
2 parents 616fcb3 + 0a7f040 commit 09bfd94

17 files changed

+865
-268
lines changed

include/swift/AST/ASTTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
SWIFT_TYPEID(AncestryFlags)
1919
SWIFT_TYPEID(CtorInitializerKind)
20-
SWIFT_TYPEID(FunctionBuilderClosurePreCheck)
20+
SWIFT_TYPEID(FunctionBuilderBodyPreCheck)
2121
SWIFT_TYPEID(GenericSignature)
2222
SWIFT_TYPEID(ImplicitMemberAction)
2323
SWIFT_TYPEID(ParamSpecifier)

include/swift/AST/ASTTypeIDs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ConstructorDecl;
2828
class CustomAttr;
2929
class Decl;
3030
class EnumDecl;
31-
enum class FunctionBuilderClosurePreCheck : uint8_t;
31+
enum class FunctionBuilderBodyPreCheck : uint8_t;
3232
class GenericParamList;
3333
class GenericSignature;
3434
class GenericTypeParamType;

include/swift/AST/Expr.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3599,7 +3599,10 @@ class ClosureExpr : public AbstractClosureExpr {
35993599
/// the CaptureListExpr which would normally maintain this sort of
36003600
/// information about captured variables), we need to have some way to access
36013601
/// this information directly on the ClosureExpr.
3602-
VarDecl *CapturedSelfDecl;
3602+
///
3603+
/// The bit indicates whether this closure has had a function builder
3604+
/// applied to it.
3605+
llvm::PointerIntPair<VarDecl *, 1, bool> CapturedSelfDeclAndAppliedBuilder;
36033606

36043607
/// The location of the "throws", if present.
36053608
SourceLoc ThrowsLoc;
@@ -3624,7 +3627,8 @@ class ClosureExpr : public AbstractClosureExpr {
36243627
unsigned discriminator, DeclContext *parent)
36253628
: AbstractClosureExpr(ExprKind::Closure, Type(), /*Implicit=*/false,
36263629
discriminator, parent),
3627-
BracketRange(bracketRange), CapturedSelfDecl(capturedSelfDecl),
3630+
BracketRange(bracketRange),
3631+
CapturedSelfDeclAndAppliedBuilder(capturedSelfDecl, false),
36283632
ThrowsLoc(throwsLoc), ArrowLoc(arrowLoc), InLoc(inLoc),
36293633
ExplicitResultType(explicitResultType), Body(nullptr) {
36303634
setParameterList(params);
@@ -3726,13 +3730,23 @@ class ClosureExpr : public AbstractClosureExpr {
37263730
bool hasEmptyBody() const;
37273731

37283732
/// VarDecl captured by this closure under the literal name \c self , if any.
3729-
VarDecl *getCapturedSelfDecl() const { return CapturedSelfDecl; }
3733+
VarDecl *getCapturedSelfDecl() const {
3734+
return CapturedSelfDeclAndAppliedBuilder.getPointer();
3735+
}
37303736

37313737
/// Whether this closure captures the \c self param in its body in such a
37323738
/// way that implicit \c self is enabled within its body (i.e. \c self is
37333739
/// captured non-weakly).
37343740
bool capturesSelfEnablingImplictSelf() const;
37353741

3742+
bool hasAppliedFunctionBuilder() const {
3743+
return CapturedSelfDeclAndAppliedBuilder.getInt();
3744+
}
3745+
3746+
void setAppliedFunctionBuilder(bool flag = true) {
3747+
CapturedSelfDeclAndAppliedBuilder.setInt(flag);
3748+
}
3749+
37363750
static bool classof(const Expr *E) {
37373751
return E->getKind() == ExprKind::Closure;
37383752
}

include/swift/AST/TypeCheckRequests.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,7 +1750,7 @@ class ValueWitnessRequest
17501750
void cacheResult(Witness value) const;
17511751
};
17521752

1753-
enum class FunctionBuilderClosurePreCheck : uint8_t {
1753+
enum class FunctionBuilderBodyPreCheck : uint8_t {
17541754
/// There were no problems pre-checking the closure.
17551755
Okay,
17561756

@@ -1763,7 +1763,7 @@ enum class FunctionBuilderClosurePreCheck : uint8_t {
17631763

17641764
class PreCheckFunctionBuilderRequest
17651765
: public SimpleRequest<PreCheckFunctionBuilderRequest,
1766-
FunctionBuilderClosurePreCheck(AnyFunctionRef),
1766+
FunctionBuilderBodyPreCheck(AnyFunctionRef),
17671767
CacheKind::Cached> {
17681768
public:
17691769
using SimpleRequest::SimpleRequest;
@@ -1772,7 +1772,7 @@ class PreCheckFunctionBuilderRequest
17721772
friend SimpleRequest;
17731773

17741774
// Evaluation.
1775-
llvm::Expected<FunctionBuilderClosurePreCheck>
1775+
llvm::Expected<FunctionBuilderBodyPreCheck>
17761776
evaluate(Evaluator &evaluator, AnyFunctionRef fn) const;
17771777

17781778
public:
@@ -2044,7 +2044,7 @@ AnyValue::Holder<GenericSignature>::equals(const HolderBase &other) const {
20442044
void simple_display(llvm::raw_ostream &out, Type value);
20452045
void simple_display(llvm::raw_ostream &out, const TypeRepr *TyR);
20462046
void simple_display(llvm::raw_ostream &out, ImplicitMemberAction action);
2047-
void simple_display(llvm::raw_ostream &out, FunctionBuilderClosurePreCheck pck);
2047+
void simple_display(llvm::raw_ostream &out, FunctionBuilderBodyPreCheck pck);
20482048

20492049
#define SWIFT_TYPEID_ZONE TypeChecker
20502050
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"

lib/AST/TypeCheckRequests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,15 +1122,15 @@ void ValueWitnessRequest::cacheResult(Witness type) const {
11221122
//----------------------------------------------------------------------------//
11231123

11241124
void swift::simple_display(llvm::raw_ostream &out,
1125-
FunctionBuilderClosurePreCheck value) {
1125+
FunctionBuilderBodyPreCheck value) {
11261126
switch (value) {
1127-
case FunctionBuilderClosurePreCheck::Okay:
1127+
case FunctionBuilderBodyPreCheck::Okay:
11281128
out << "okay";
11291129
break;
1130-
case FunctionBuilderClosurePreCheck::HasReturnStmt:
1130+
case FunctionBuilderBodyPreCheck::HasReturnStmt:
11311131
out << "has return statement";
11321132
break;
1133-
case FunctionBuilderClosurePreCheck::Error:
1133+
case FunctionBuilderBodyPreCheck::Error:
11341134
out << "error";
11351135
break;
11361136
}

0 commit comments

Comments
 (0)