@@ -1294,27 +1294,19 @@ VarDecl *PatternBindingInitializer::getInitializedLazyVar() const {
12941294 return nullptr ;
12951295}
12961296
1297- static bool patternContainsVarDeclBinding (const Pattern *P, const VarDecl *VD) {
1298- bool Result = false ;
1299- P->forEachVariable ([&](VarDecl *FoundVD) {
1300- Result |= FoundVD == VD;
1301- });
1302- return Result;
1303- }
1304-
13051297unsigned PatternBindingDecl::getPatternEntryIndexForVarDecl (const VarDecl *VD) const {
13061298 assert (VD && " Cannot find a null VarDecl" );
13071299
13081300 auto List = getPatternList ();
13091301 if (List.size () == 1 ) {
1310- assert (patternContainsVarDeclBinding ( List[0 ].getPattern (), VD) &&
1302+ assert (List[0 ].getPattern ()-> containsVarDecl ( VD) &&
13111303 " Single entry PatternBindingDecl is set up wrong" );
13121304 return 0 ;
13131305 }
13141306
13151307 unsigned Result = 0 ;
13161308 for (auto entry : List) {
1317- if (patternContainsVarDeclBinding ( entry.getPattern (), VD))
1309+ if (entry.getPattern ()-> containsVarDecl ( VD))
13181310 return Result;
13191311 ++Result;
13201312 }
@@ -4927,12 +4919,6 @@ SourceRange VarDecl::getTypeSourceRangeForDiagnostics() const {
49274919 return SourceRange ();
49284920}
49294921
4930- static bool isVarInPattern (const VarDecl *vd, Pattern *p) {
4931- bool foundIt = false ;
4932- p->forEachVariable ([&](VarDecl *foundFD) { foundIt |= foundFD == vd; });
4933- return foundIt;
4934- }
4935-
49364922static Optional<std::pair<CaseStmt *, Pattern *>>
49374923findParentPatternCaseStmtAndPattern (const VarDecl *inputVD) {
49384924 auto getMatchingPattern = [&](CaseStmt *cs) -> Pattern * {
@@ -4946,7 +4932,7 @@ findParentPatternCaseStmtAndPattern(const VarDecl *inputVD) {
49464932
49474933 // Then check the rest of our case label items.
49484934 for (auto &item : cs->getMutableCaseLabelItems ()) {
4949- if (isVarInPattern (inputVD, item.getPattern ())) {
4935+ if (item.getPattern ()-> containsVarDecl (inputVD )) {
49504936 return item.getPattern ();
49514937 }
49524938 }
@@ -5039,15 +5025,15 @@ Pattern *VarDecl::getParentPattern() const {
50395025 // In a case statement, search for the pattern that contains it. This is
50405026 // a bit silly, because you can't have something like "case x, y:" anyway.
50415027 for (auto items : cs->getCaseLabelItems ()) {
5042- if (isVarInPattern ( this , items.getPattern ()))
5028+ if (items.getPattern ()-> containsVarDecl ( this ))
50435029 return items.getPattern ();
50445030 }
50455031 }
50465032
50475033 if (auto *LCS = dyn_cast<LabeledConditionalStmt>(stmt)) {
50485034 for (auto &elt : LCS->getCond ())
50495035 if (auto pat = elt.getPatternOrNull ())
5050- if (isVarInPattern (this , pat ))
5036+ if (pat-> containsVarDecl (this ))
50515037 return pat;
50525038 }
50535039
@@ -5066,6 +5052,55 @@ Pattern *VarDecl::getParentPattern() const {
50665052 return nullptr ;
50675053}
50685054
5055+ NullablePtr<VarDecl>
5056+ VarDecl::getCorrespondingFirstCaseLabelItemVarDecl () const {
5057+ if (!hasName ())
5058+ return nullptr ;
5059+
5060+ auto *caseStmt = dyn_cast_or_null<CaseStmt>(getRecursiveParentPatternStmt ());
5061+ if (!caseStmt)
5062+ return nullptr ;
5063+
5064+ auto *pattern = caseStmt->getCaseLabelItems ().front ().getPattern ();
5065+ SmallVector<VarDecl *, 8 > vars;
5066+ pattern->collectVariables (vars);
5067+ for (auto *vd : vars) {
5068+ if (vd->hasName () && vd->getName () == getName ())
5069+ return vd;
5070+ }
5071+ return nullptr ;
5072+ }
5073+
5074+ bool VarDecl::isCaseBodyVariable () const {
5075+ auto *caseStmt = dyn_cast_or_null<CaseStmt>(getRecursiveParentPatternStmt ());
5076+ if (!caseStmt)
5077+ return false ;
5078+ return llvm::any_of (caseStmt->getCaseBodyVariablesOrEmptyArray (),
5079+ [&](VarDecl *vd) { return vd == this ; });
5080+ }
5081+
5082+ NullablePtr<VarDecl> VarDecl::getCorrespondingCaseBodyVariable () const {
5083+ // Only var decls associated with case statements can have child var decls.
5084+ auto *caseStmt = dyn_cast_or_null<CaseStmt>(getRecursiveParentPatternStmt ());
5085+ if (!caseStmt)
5086+ return nullptr ;
5087+
5088+ // If this var decl doesn't have a name, it can not have a corresponding case
5089+ // body variable.
5090+ if (!hasName ())
5091+ return nullptr ;
5092+
5093+ auto name = getName ();
5094+
5095+ // A var decl associated with a case stmt implies that the case stmt has body
5096+ // var decls. So we can access the optional value here without worry.
5097+ auto caseBodyVars = *caseStmt->getCaseBodyVariables ();
5098+ auto result = llvm::find_if (caseBodyVars, [&](VarDecl *caseBodyVar) {
5099+ return caseBodyVar->getName () == name;
5100+ });
5101+ return (result != caseBodyVars.end ()) ? *result : nullptr ;
5102+ }
5103+
50695104bool VarDecl::isSelfParameter () const {
50705105 if (isa<ParamDecl>(this )) {
50715106 if (auto *AFD = dyn_cast<AbstractFunctionDecl>(getDeclContext ()))
0 commit comments