3939using namespace swift ;
4040using namespace ast_scope ;
4141
42- static std::vector<ASTNode> asNodeVector (DeclRange dr) {
43- std::vector<ASTNode> nodes;
44- llvm::transform (dr, std::back_inserter (nodes),
45- [&](Decl *d) { return ASTNode (d); });
46- return nodes;
47- }
48-
4942namespace swift {
5043namespace ast_scope {
5144
@@ -67,22 +60,6 @@ class ScopeCreator final {
6760 ScopeCreator (const ScopeCreator &) = delete; // ensure no copies
6861 ScopeCreator (const ScopeCreator &&) = delete; // ensure no moves
6962
70- // / Given an array of ASTNodes or Decl pointers, add them
71- // / Return the resultant insertionPoint
72- // /
73- // / \param endLoc The end location for any "scopes until the end" that
74- // / we introduce here, such as PatternEntryDeclScope and GuardStmtScope
75- ASTScopeImpl *
76- addSiblingsToScopeTree (ASTScopeImpl *const insertionPoint,
77- ArrayRef<ASTNode> nodesOrDeclsToAdd,
78- Optional<SourceLoc> endLoc) {
79- auto *ip = insertionPoint;
80- for (auto nd : nodesOrDeclsToAdd) {
81- ip = addToScopeTreeAndReturnInsertionPoint (nd, ip, endLoc);
82- }
83- return ip;
84- }
85-
8663public:
8764 // / For each of searching, call this unless the insertion point is needed
8865 void addToScopeTree (ASTNode n, ASTScopeImpl *parent) {
@@ -270,7 +247,7 @@ void ASTSourceFileScope::expandFunctionBody(AbstractFunctionDecl *AFD) {
270247
271248ASTSourceFileScope::ASTSourceFileScope (SourceFile *SF,
272249 ScopeCreator *scopeCreator)
273- : SF(SF), scopeCreator(scopeCreator), insertionPoint( this ) {}
250+ : SF(SF), scopeCreator(scopeCreator) {}
274251
275252#pragma mark NodeAdder
276253
@@ -708,18 +685,15 @@ AnnotatedInsertionPoint
708685ASTSourceFileScope::expandAScopeThatCreatesANewInsertionPoint (
709686 ScopeCreator &scopeCreator) {
710687 ASTScopeAssert (SF, " Must already have a SourceFile." );
711- ArrayRef<Decl *> decls = SF->getTopLevelDecls ();
712688
713689 SourceLoc endLoc = getSourceRangeOfThisASTNode ().End ;
714690
715- std::vector<ASTNode> newNodes (decls.begin (), decls.end ());
716- insertionPoint =
717- scopeCreator.addSiblingsToScopeTree (insertionPoint,
718- newNodes, endLoc);
691+ ASTScopeImpl *insertionPoint = this ;
692+ for (auto *d : SF->getTopLevelDecls ()) {
693+ insertionPoint = scopeCreator.addToScopeTreeAndReturnInsertionPoint (
694+ ASTNode (d), insertionPoint, endLoc);
695+ }
719696
720- // Too slow to perform all the time:
721- // ASTScopeAssert(scopeCreator->containsAllDeclContextsFromAST(),
722- // "ASTScope tree missed some DeclContexts or made some up");
723697 return {insertionPoint, " Next time decls are added they go here." };
724698}
725699
@@ -840,14 +814,15 @@ GenericTypeOrExtensionScope::expandAScopeThatCreatesANewInsertionPoint(
840814AnnotatedInsertionPoint
841815BraceStmtScope::expandAScopeThatCreatesANewInsertionPoint (
842816 ScopeCreator &scopeCreator) {
843- // TODO: remove the sort after fixing parser to create brace statement
844- // elements in source order
845- auto * insertionPoint =
846- scopeCreator. addSiblingsToScopeTree ( this ,
847- stmt-> getElements (),
848- endLoc);
817+ ASTScopeImpl *insertionPoint = this ;
818+ for ( auto nd : stmt-> getElements ()) {
819+ insertionPoint = scopeCreator. addToScopeTreeAndReturnInsertionPoint (
820+ nd, insertionPoint, endLoc);
821+ }
822+
849823 if (auto *s = scopeCreator.getASTContext ().Stats )
850824 ++s->getFrontendCounters ().NumBraceStmtASTScopeExpansions ;
825+
851826 return {
852827 insertionPoint,
853828 " For top-level code decls, need the scope under, say a guard statment." };
@@ -1209,8 +1184,9 @@ void FunctionBodyScope::expandBody(ScopeCreator &scopeCreator) {
12091184void GenericTypeOrExtensionScope::expandBody (ScopeCreator &) {}
12101185
12111186void IterableTypeScope::expandBody (ScopeCreator &scopeCreator) {
1212- auto nodes = asNodeVector (getIterableDeclContext ().get ()->getMembers ());
1213- scopeCreator.addSiblingsToScopeTree (this , nodes, None);
1187+ for (auto *d : getIterableDeclContext ().get ()->getMembers ())
1188+ scopeCreator.addToScopeTree (ASTNode (d), this );
1189+
12141190 if (auto *s = scopeCreator.getASTContext ().Stats )
12151191 ++s->getFrontendCounters ().NumIterableTypeBodyASTScopeExpansions ;
12161192}
0 commit comments