Skip to content

Commit 82a4e4f

Browse files
authored
Merge pull request #29331 from DougGregor/generalize-functions-builders-again
[Take 2] Reimplement function builders as statement transformations
2 parents f6e0db5 + d098129 commit 82a4e4f

17 files changed

+919
-294
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
@@ -1755,7 +1755,7 @@ class ValueWitnessRequest
17551755
void cacheResult(Witness value) const;
17561756
};
17571757

1758-
enum class FunctionBuilderClosurePreCheck : uint8_t {
1758+
enum class FunctionBuilderBodyPreCheck : uint8_t {
17591759
/// There were no problems pre-checking the closure.
17601760
Okay,
17611761

@@ -1768,7 +1768,7 @@ enum class FunctionBuilderClosurePreCheck : uint8_t {
17681768

17691769
class PreCheckFunctionBuilderRequest
17701770
: public SimpleRequest<PreCheckFunctionBuilderRequest,
1771-
FunctionBuilderClosurePreCheck(AnyFunctionRef),
1771+
FunctionBuilderBodyPreCheck(AnyFunctionRef),
17721772
CacheKind::Cached> {
17731773
public:
17741774
using SimpleRequest::SimpleRequest;
@@ -1777,7 +1777,7 @@ class PreCheckFunctionBuilderRequest
17771777
friend SimpleRequest;
17781778

17791779
// Evaluation.
1780-
llvm::Expected<FunctionBuilderClosurePreCheck>
1780+
llvm::Expected<FunctionBuilderBodyPreCheck>
17811781
evaluate(Evaluator &evaluator, AnyFunctionRef fn) const;
17821782

17831783
public:
@@ -2078,7 +2078,7 @@ AnyValue::Holder<GenericSignature>::equals(const HolderBase &other) const {
20782078
void simple_display(llvm::raw_ostream &out, Type value);
20792079
void simple_display(llvm::raw_ostream &out, const TypeRepr *TyR);
20802080
void simple_display(llvm::raw_ostream &out, ImplicitMemberAction action);
2081-
void simple_display(llvm::raw_ostream &out, FunctionBuilderClosurePreCheck pck);
2081+
void simple_display(llvm::raw_ostream &out, FunctionBuilderBodyPreCheck pck);
20822082

20832083
#define SWIFT_TYPEID_ZONE TypeChecker
20842084
#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
@@ -1125,15 +1125,15 @@ void ValueWitnessRequest::cacheResult(Witness type) const {
11251125
//----------------------------------------------------------------------------//
11261126

11271127
void swift::simple_display(llvm::raw_ostream &out,
1128-
FunctionBuilderClosurePreCheck value) {
1128+
FunctionBuilderBodyPreCheck value) {
11291129
switch (value) {
1130-
case FunctionBuilderClosurePreCheck::Okay:
1130+
case FunctionBuilderBodyPreCheck::Okay:
11311131
out << "okay";
11321132
break;
1133-
case FunctionBuilderClosurePreCheck::HasReturnStmt:
1133+
case FunctionBuilderBodyPreCheck::HasReturnStmt:
11341134
out << "has return statement";
11351135
break;
1136-
case FunctionBuilderClosurePreCheck::Error:
1136+
case FunctionBuilderBodyPreCheck::Error:
11371137
out << "error";
11381138
break;
11391139
}

0 commit comments

Comments
 (0)