Skip to content

Commit c5e1388

Browse files
committed
ASTScope: Remove cull()
1 parent cb70220 commit c5e1388

File tree

1 file changed

+11
-90
lines changed

1 file changed

+11
-90
lines changed

lib/AST/ASTScopeCreation.cpp

Lines changed: 11 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -180,62 +180,6 @@ class ScopeCreator final {
180180
addToScopeTreeAndReturnInsertionPoint(ASTNode, ASTScopeImpl *parent,
181181
Optional<SourceLoc> endLoc);
182182

183-
bool isWorthTryingToCreateScopeFor(ASTNode n) const {
184-
if (!n)
185-
return false;
186-
if (n.is<Expr *>())
187-
return true;
188-
// Cannot ignore implicit statements because implict return can contain
189-
// scopes in the expression, such as closures.
190-
// But must ignore other implicit statements, e.g. brace statments
191-
// if they can have no children and no stmt source range.
192-
// Deal with it in visitBraceStmt
193-
if (n.is<Stmt *>())
194-
return true;
195-
196-
auto *const d = n.get<Decl *>();
197-
// Implicit nodes may not have source information for name lookup.
198-
if (!isLocalizable(d))
199-
return false;
200-
201-
// Commented out for
202-
// validation-test/compiler_crashers_fixed/27962-swift-rebindselfinconstructorexpr-getcalledconstructor.swift
203-
// In that test the invalid PBD -> var decl which contains the desired
204-
// closure scope
205-
// if (const auto *PBD = dyn_cast<PatternBindingDecl>(d))
206-
// if (!isLocalizable(PBD))
207-
// return false;
208-
/// In
209-
/// \code
210-
/// @propertyWrapper
211-
/// public struct Wrapper<T> {
212-
/// public var value: T
213-
///
214-
/// public init(body: () -> T) {
215-
/// self.value = body()
216-
/// }
217-
/// }
218-
///
219-
/// let globalInt = 17
220-
///
221-
/// @Wrapper(body: { globalInt })
222-
/// public var y: Int
223-
/// \endcode
224-
/// I'm seeing a dumped AST include:
225-
/// (pattern_binding_decl range=[test.swift:13:8 - line:12:29]
226-
const auto &SM = d->getASTContext().SourceMgr;
227-
228-
// Once we allow invalid PatternBindingDecls (see
229-
// isWorthTryingToCreateScopeFor), then
230-
// IDE/complete_property_delegate_attribute.swift fails because we try to
231-
// expand a member whose source range is backwards.
232-
(void)SM;
233-
ASTScopeAssert(d->getStartLoc().isInvalid() ||
234-
!SM.isBeforeInBuffer(d->getEndLoc(), d->getStartLoc()),
235-
"end-before-start will break tree search via location");
236-
return true;
237-
}
238-
239183
template <typename Scope, typename... Args>
240184
ASTScopeImpl *constructExpandAndInsert(ASTScopeImpl *parent, Args... args) {
241185
auto *child = new (ctx) Scope(args...);
@@ -339,26 +283,6 @@ class ScopeCreator final {
339283
ASTScopeImpl *parent,
340284
Optional<SourceLoc> endLoc);
341285

342-
/// Remove VarDecls because we'll find them when we expand the
343-
/// PatternBindingDecls. Remove EnunCases
344-
/// because they overlap EnumElements and AST includes the elements in the
345-
/// members.
346-
std::vector<ASTNode> cull(ArrayRef<ASTNode> input) const {
347-
// TODO: Investigate whether to move the real EndLoc tracking of
348-
// SubscriptDecl up into AbstractStorageDecl. May have to cull more.
349-
std::vector<ASTNode> culled;
350-
llvm::copy_if(input, std::back_inserter(culled), [&](ASTNode n) {
351-
ASTScopeAssert(
352-
!n.isDecl(DeclKind::Accessor),
353-
"Should not find accessors in iterable types or brace statements");
354-
return isLocalizable(n) &&
355-
!n.isDecl(DeclKind::Var) &&
356-
!n.isDecl(DeclKind::EnumCase) &&
357-
!n.isDecl(DeclKind::IfConfig);
358-
});
359-
return culled;
360-
}
361-
362286
SWIFT_DEBUG_DUMP { print(llvm::errs()); }
363287

364288
void print(raw_ostream &out) const {
@@ -466,6 +390,10 @@ class NodeAdder
466390
VISIT_AND_IGNORE(PoundDiagnosticDecl)
467391
VISIT_AND_IGNORE(MissingMemberDecl)
468392

393+
// Only members of the active clause are in scope, and those
394+
// are visited separately.
395+
VISIT_AND_IGNORE(IfConfigDecl)
396+
469397
// This declaration is handled from the PatternBindingDecl
470398
VISIT_AND_IGNORE(VarDecl)
471399

@@ -612,14 +540,6 @@ class NodeAdder
612540
return p;
613541
}
614542

615-
ASTScopeImpl *visitIfConfigDecl(IfConfigDecl *icd,
616-
ASTScopeImpl *p,
617-
ScopeCreator &scopeCreator) {
618-
ASTScope_unreachable(
619-
"Should be handled inside of "
620-
"expandIfConfigClausesThenCullAndSortElementsOrMembers");
621-
}
622-
623543
ASTScopeImpl *visitReturnStmt(ReturnStmt *rs, ASTScopeImpl *p,
624544
ScopeCreator &scopeCreator) {
625545
if (rs->hasResult())
@@ -657,9 +577,13 @@ ASTScopeImpl *
657577
ScopeCreator::addToScopeTreeAndReturnInsertionPoint(ASTNode n,
658578
ASTScopeImpl *parent,
659579
Optional<SourceLoc> endLoc) {
660-
if (!isWorthTryingToCreateScopeFor(n))
580+
if (!n)
661581
return parent;
662582

583+
if (auto *d = n.dyn_cast<Decl *>())
584+
if (d->isImplicit())
585+
return parent;
586+
663587
NodeAdder adder(endLoc);
664588
if (auto *p = n.dyn_cast<Decl *>())
665589
return adder.visit(p, parent, *this);
@@ -874,8 +798,7 @@ ASTSourceFileScope::expandAScopeThatCreatesANewInsertionPoint(
874798
std::vector<ASTNode> newNodes(decls.begin(), decls.end());
875799
insertionPoint =
876800
scopeCreator.addSiblingsToScopeTree(insertionPoint,
877-
scopeCreator.cull(newNodes),
878-
endLoc);
801+
newNodes, endLoc);
879802

880803
// Too slow to perform all the time:
881804
// ASTScopeAssert(scopeCreator->containsAllDeclContextsFromAST(),
@@ -1002,8 +925,7 @@ BraceStmtScope::expandAScopeThatCreatesANewInsertionPoint(
1002925
// elements in source order
1003926
auto *insertionPoint =
1004927
scopeCreator.addSiblingsToScopeTree(this,
1005-
scopeCreator.cull(
1006-
stmt->getElements()),
928+
stmt->getElements(),
1007929
endLoc);
1008930
if (auto *s = scopeCreator.getASTContext().Stats)
1009931
++s->getFrontendCounters().NumBraceStmtASTScopeExpansions;
@@ -1365,7 +1287,6 @@ void GenericTypeOrExtensionScope::expandBody(ScopeCreator &) {}
13651287

13661288
void IterableTypeScope::expandBody(ScopeCreator &scopeCreator) {
13671289
auto nodes = asNodeVector(getIterableDeclContext().get()->getMembers());
1368-
nodes = scopeCreator.cull(nodes);
13691290
scopeCreator.addSiblingsToScopeTree(this, nodes, None);
13701291
if (auto *s = scopeCreator.getASTContext().Stats)
13711292
++s->getFrontendCounters().NumIterableTypeBodyASTScopeExpansions;

0 commit comments

Comments
 (0)