@@ -1568,7 +1568,7 @@ DeclContext *ASTScope::getDeclContext() const {
1568
1568
1569
1569
case ASTScopeKind::Accessors:
1570
1570
// FIXME: Somewhat odd modeling because Subscripts don't have their
1571
- // own nodes. Maybe that should.
1571
+ // own nodes. Maybe they should.
1572
1572
if (auto subscript = dyn_cast<SubscriptDecl>(abstractStorageDecl))
1573
1573
return subscript;
1574
1574
@@ -1607,6 +1607,89 @@ DeclContext *ASTScope::getInnermostEnclosingDeclContext() const {
1607
1607
llvm_unreachable (" Top-most scope is a declaration context" );
1608
1608
}
1609
1609
1610
+ SmallVector<ValueDecl *, 4 > ASTScope::getLocalBindings () const {
1611
+ SmallVector<ValueDecl *, 4 > result;
1612
+
1613
+ auto handlePattern = [&](const Pattern *pattern) {
1614
+ if (!pattern) return ;
1615
+ pattern->forEachVariable ([&](VarDecl *var) {
1616
+ result.push_back (var);
1617
+ });
1618
+ };
1619
+
1620
+ switch (getKind ()) {
1621
+ case ASTScopeKind::Preexpanded:
1622
+ case ASTScopeKind::SourceFile:
1623
+ case ASTScopeKind::TypeOrExtensionBody:
1624
+ case ASTScopeKind::AbstractFunctionDecl:
1625
+ case ASTScopeKind::DefaultArgument:
1626
+ case ASTScopeKind::PatternBinding:
1627
+ case ASTScopeKind::PatternInitializer:
1628
+ case ASTScopeKind::BraceStmt:
1629
+ case ASTScopeKind::IfStmt:
1630
+ case ASTScopeKind::GuardStmt:
1631
+ case ASTScopeKind::RepeatWhileStmt:
1632
+ case ASTScopeKind::ForEachStmt:
1633
+ case ASTScopeKind::DoCatchStmt:
1634
+ case ASTScopeKind::SwitchStmt:
1635
+ case ASTScopeKind::ForStmt:
1636
+ case ASTScopeKind::Accessors:
1637
+ case ASTScopeKind::TopLevelCode:
1638
+ // No local declarations.
1639
+ break ;
1640
+
1641
+ case ASTScopeKind::GenericParams:
1642
+ result.push_back (genericParams.params ->getParams ()[genericParams.index ]);
1643
+ break ;
1644
+
1645
+ case ASTScopeKind::AbstractFunctionParams:
1646
+ result.push_back (abstractFunctionParams.decl ->getParameterList (
1647
+ abstractFunctionParams.listIndex )
1648
+ ->get (abstractFunctionParams.paramIndex ));
1649
+ break ;
1650
+
1651
+ case ASTScopeKind::AfterPatternBinding:
1652
+ handlePattern (patternBinding.decl ->getPattern (patternBinding.entry ));
1653
+ break ;
1654
+
1655
+ case ASTScopeKind::LocalDeclaration:
1656
+ result.push_back (cast<ValueDecl>(localDeclaration));
1657
+ break ;
1658
+
1659
+ case ASTScopeKind::ConditionalClause:
1660
+ handlePattern (conditionalClause.stmt ->getCond ()[conditionalClause.index ]
1661
+ .getPatternOrNull ());
1662
+ break ;
1663
+
1664
+ case ASTScopeKind::ForEachPattern:
1665
+ handlePattern (forEach->getPattern ());
1666
+ break ;
1667
+
1668
+ case ASTScopeKind::CatchStmt:
1669
+ handlePattern (catchStmt->getErrorPattern ());
1670
+ break ;
1671
+
1672
+ case ASTScopeKind::CaseStmt:
1673
+ for (const auto &item : caseStmt->getCaseLabelItems ())
1674
+ handlePattern (item.getPattern ());
1675
+ break ;
1676
+
1677
+ case ASTScopeKind::ForStmtInitializer:
1678
+ for (auto decl : forStmt->getInitializerVarDecls ())
1679
+ result.push_back (cast<ValueDecl>(decl));
1680
+ break ;
1681
+
1682
+ case ASTScopeKind::Closure:
1683
+ // Note: Parameters all at once is different from functions, but it's not
1684
+ // relevant because there are no default arguments.
1685
+ for (auto param : *closure->getParameters ())
1686
+ result.push_back (param);
1687
+ break ;
1688
+ }
1689
+
1690
+ return result;
1691
+ }
1692
+
1610
1693
void ASTScope::expandAll () const {
1611
1694
if (!isExpanded ())
1612
1695
expand ();
0 commit comments