1717#ifndef SWIFT_AST_STMT_H
1818#define SWIFT_AST_STMT_H
1919
20+ #include " swift/AST/ASTNode.h"
2021#include " swift/AST/Availability.h"
2122#include " swift/AST/AvailabilitySpec.h"
22- #include " swift/AST/ASTNode.h"
2323#include " swift/AST/IfConfigClause.h"
2424#include " swift/AST/TypeAlignments.h"
2525#include " swift/Basic/NullablePtr.h"
26+ #include " llvm/ADT/TinyPtrVector.h"
2627#include " llvm/Support/TrailingObjects.h"
2728
2829namespace swift {
@@ -984,24 +985,22 @@ class CaseStmt final
984985 SourceLoc CaseLoc;
985986 SourceLoc ColonLoc;
986987
987- llvm::PointerIntPair<Stmt *, 1 , bool > BodyAndHasBoundDecls ;
988+ llvm::PointerIntPair<Stmt *, 1 , bool > BodyAndHasFallthrough ;
988989
989- // / Set to true if we have a fallthrough.
990- // /
991- // / TODO: Once we have CaseBodyVarDecls, use the bit in BodyAndHasBoundDecls
992- // / for this instead. This is separate now for staging reasons.
993- bool hasFallthrough;
990+ Optional<MutableArrayRef<VarDecl *>> CaseBodyVariables;
994991
995992 CaseStmt (SourceLoc CaseLoc, ArrayRef<CaseLabelItem> CaseLabelItems,
996- bool HasBoundDecls, SourceLoc UnknownAttrLoc, SourceLoc ColonLoc,
997- Stmt *Body, Optional<bool > Implicit,
993+ SourceLoc UnknownAttrLoc, SourceLoc ColonLoc, Stmt *Body,
994+ Optional<MutableArrayRef<VarDecl *>> CaseBodyVariables,
995+ Optional<bool > Implicit,
998996 NullablePtr<FallthroughStmt> fallthroughStmt);
999997
1000998public:
1001999 static CaseStmt *
10021000 create (ASTContext &C, SourceLoc CaseLoc,
1003- ArrayRef<CaseLabelItem> CaseLabelItems, bool HasBoundDecls,
1004- SourceLoc UnknownAttrLoc, SourceLoc ColonLoc, Stmt *Body,
1001+ ArrayRef<CaseLabelItem> CaseLabelItems, SourceLoc UnknownAttrLoc,
1002+ SourceLoc ColonLoc, Stmt *Body,
1003+ Optional<MutableArrayRef<VarDecl *>> CaseBodyVariables,
10051004 Optional<bool > Implicit = None,
10061005 NullablePtr<FallthroughStmt> fallthroughStmt = nullptr );
10071006
@@ -1020,18 +1019,18 @@ class CaseStmt final
10201019 }
10211020
10221021 NullablePtr<CaseStmt> getFallthroughDest () {
1023- if (!hasFallthrough )
1022+ if (!hasFallthroughDest () )
10241023 return nullptr ;
10251024 return (*getTrailingObjects<FallthroughStmt *>())->getFallthroughDest ();
10261025 }
10271026
1028- bool hasFallthroughDest () const { return hasFallthrough ; }
1027+ bool hasFallthroughDest () const { return BodyAndHasFallthrough. getInt () ; }
10291028
1030- Stmt *getBody () const { return BodyAndHasBoundDecls .getPointer (); }
1031- void setBody (Stmt *body) { BodyAndHasBoundDecls .setPointer (body); }
1029+ Stmt *getBody () const { return BodyAndHasFallthrough .getPointer (); }
1030+ void setBody (Stmt *body) { BodyAndHasFallthrough .setPointer (body); }
10321031
10331032 // / True if the case block declares any patterns with local variable bindings.
1034- bool hasBoundDecls () const { return BodyAndHasBoundDecls. getInt (); }
1033+ bool hasBoundDecls () const { return CaseBodyVariables. hasValue (); }
10351034
10361035 // / Get the source location of the 'case' or 'default' of the first label.
10371036 SourceLoc getLoc () const { return CaseLoc; }
@@ -1056,14 +1055,38 @@ class CaseStmt final
10561055 return UnknownAttrLoc.isValid ();
10571056 }
10581057
1058+ Optional<ArrayRef<VarDecl *>> getCaseBodyVariables () const {
1059+ if (!CaseBodyVariables)
1060+ return None;
1061+ ArrayRef<VarDecl *> a = *CaseBodyVariables;
1062+ return a;
1063+ }
1064+
1065+ Optional<MutableArrayRef<VarDecl *>> getCaseBodyVariables () {
1066+ return CaseBodyVariables;
1067+ }
1068+
1069+ ArrayRef<VarDecl *> getCaseBodyVariablesOrEmptyArray () const {
1070+ if (!CaseBodyVariables)
1071+ return ArrayRef<VarDecl *>();
1072+ ArrayRef<VarDecl *> a = *CaseBodyVariables;
1073+ return a;
1074+ }
1075+
1076+ MutableArrayRef<VarDecl *> getCaseBodyVariablesOrEmptyArray () {
1077+ if (!CaseBodyVariables)
1078+ return MutableArrayRef<VarDecl *>();
1079+ return *CaseBodyVariables;
1080+ }
1081+
10591082 static bool classof (const Stmt *S) { return S->getKind () == StmtKind::Case; }
10601083
10611084 size_t numTrailingObjects (OverloadToken<CaseLabelItem>) const {
10621085 return getNumCaseLabelItems ();
10631086 }
10641087
10651088 size_t numTrailingObjects (OverloadToken<FallthroughStmt *>) const {
1066- return hasFallthrough ? 1 : 0 ;
1089+ return hasFallthroughDest () ? 1 : 0 ;
10671090 }
10681091};
10691092
0 commit comments