Skip to content

Commit ec69f11

Browse files
authored
Merge pull request #4636 from DougGregor/astscope-lookup
2 parents f5d0ccb + 2552f41 commit ec69f11

File tree

10 files changed

+510
-173
lines changed

10 files changed

+510
-173
lines changed

include/swift/AST/ASTScope.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ enum class ASTScopeKind : uint8_t {
7575
PatternBinding,
7676
/// The scope introduced for an initializer of a pattern binding.
7777
PatternInitializer,
78-
/// The scope introduced by a particular clause in a pattern binding
79-
/// declaration.
78+
/// The scope following a particular clause in a pattern binding declaration,
79+
/// which is the outermost scope in which the variables introduced by that
80+
/// clause will be visible.
8081
AfterPatternBinding,
8182
/// The scope introduced by a brace statement.
8283
BraceStmt,
@@ -543,6 +544,18 @@ class ASTScope {
543544
/// \seealso getDeclContext().
544545
DeclContext *getInnermostEnclosingDeclContext() const;
545546

547+
/// Retrueve the declarations whose names are directly bound by this scope.
548+
///
549+
/// The declarations bound in this scope aren't available in the immediate
550+
/// parent of this scope, but will still be visible in child scopes (unless
551+
/// shadowed there).
552+
///
553+
/// Note that this routine does not produce bindings for anything that can
554+
/// be found via qualified name lookup in a \c DeclContext, such as nominal
555+
/// type declarations or extensions thereof, or the source file itself. The
556+
/// client can perform such lookups using the result of \c getDeclContext().
557+
SmallVector<ValueDecl *, 4> getLocalBindings() const;
558+
546559
/// Expand the entire scope map.
547560
///
548561
/// Normally, the scope map will be expanded only as needed by its queries,

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ namespace swift {
141141
/// new enough?
142142
bool EnableTargetOSChecking = true;
143143

144+
/// Should we use \c ASTScope-based resolution for unqualified name lookup?
145+
bool EnableASTScopeLookup = false;
146+
144147
/// Whether to use the import as member inference system
145148
///
146149
/// When importing a global, try to infer whether we can import it as a

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ def disable_target_os_checking :
102102
Flag<["-"], "disable-target-os-checking">,
103103
HelpText<"Disable checking the target OS of serialized modules">;
104104

105+
def enable_astscope_lookup : Flag<["-"], "enable-astscope-lookup">,
106+
HelpText<"Enable ASTScope-based unqualified name lookup">;
107+
105108
def print_clang_stats : Flag<["-"], "print-clang-stats">,
106109
HelpText<"Print Clang importer statistics">;
107110

include/swift/Parse/Parser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,9 @@ class Parser {
541541
}
542542

543543
ValueDecl *lookupInScope(DeclName Name) {
544+
if (Context.LangOpts.EnableASTScopeLookup)
545+
return nullptr;
546+
544547
return getScopeInfo().lookupValueName(Name);
545548
}
546549

lib/AST/ASTScope.cpp

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,7 @@ DeclContext *ASTScope::getDeclContext() const {
15681568

15691569
case ASTScopeKind::Accessors:
15701570
// FIXME: Somewhat odd modeling because Subscripts don't have their
1571-
// own nodes. Maybe that should.
1571+
// own nodes. Maybe they should.
15721572
if (auto subscript = dyn_cast<SubscriptDecl>(abstractStorageDecl))
15731573
return subscript;
15741574

@@ -1607,6 +1607,89 @@ DeclContext *ASTScope::getInnermostEnclosingDeclContext() const {
16071607
llvm_unreachable("Top-most scope is a declaration context");
16081608
}
16091609

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+
16101693
void ASTScope::expandAll() const {
16111694
if (!isExpanded())
16121695
expand();

0 commit comments

Comments
 (0)