39
39
using namespace swift ;
40
40
using namespace ast_scope ;
41
41
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
-
49
42
namespace swift {
50
43
namespace ast_scope {
51
44
@@ -67,22 +60,6 @@ class ScopeCreator final {
67
60
ScopeCreator (const ScopeCreator &) = delete; // ensure no copies
68
61
ScopeCreator (const ScopeCreator &&) = delete; // ensure no moves
69
62
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
-
86
63
public:
87
64
// / For each of searching, call this unless the insertion point is needed
88
65
void addToScopeTree (ASTNode n, ASTScopeImpl *parent) {
@@ -270,7 +247,7 @@ void ASTSourceFileScope::expandFunctionBody(AbstractFunctionDecl *AFD) {
270
247
271
248
ASTSourceFileScope::ASTSourceFileScope (SourceFile *SF,
272
249
ScopeCreator *scopeCreator)
273
- : SF(SF), scopeCreator(scopeCreator), insertionPoint( this ) {}
250
+ : SF(SF), scopeCreator(scopeCreator) {}
274
251
275
252
#pragma mark NodeAdder
276
253
@@ -708,18 +685,15 @@ AnnotatedInsertionPoint
708
685
ASTSourceFileScope::expandAScopeThatCreatesANewInsertionPoint (
709
686
ScopeCreator &scopeCreator) {
710
687
ASTScopeAssert (SF, " Must already have a SourceFile." );
711
- ArrayRef<Decl *> decls = SF->getTopLevelDecls ();
712
688
713
689
SourceLoc endLoc = getSourceRangeOfThisASTNode ().End ;
714
690
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
+ }
719
696
720
- // Too slow to perform all the time:
721
- // ASTScopeAssert(scopeCreator->containsAllDeclContextsFromAST(),
722
- // "ASTScope tree missed some DeclContexts or made some up");
723
697
return {insertionPoint, " Next time decls are added they go here." };
724
698
}
725
699
@@ -840,14 +814,15 @@ GenericTypeOrExtensionScope::expandAScopeThatCreatesANewInsertionPoint(
840
814
AnnotatedInsertionPoint
841
815
BraceStmtScope::expandAScopeThatCreatesANewInsertionPoint (
842
816
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
+
849
823
if (auto *s = scopeCreator.getASTContext ().Stats )
850
824
++s->getFrontendCounters ().NumBraceStmtASTScopeExpansions ;
825
+
851
826
return {
852
827
insertionPoint,
853
828
" For top-level code decls, need the scope under, say a guard statment." };
@@ -1209,8 +1184,9 @@ void FunctionBodyScope::expandBody(ScopeCreator &scopeCreator) {
1209
1184
void GenericTypeOrExtensionScope::expandBody (ScopeCreator &) {}
1210
1185
1211
1186
void 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
+
1214
1190
if (auto *s = scopeCreator.getASTContext ().Stats )
1215
1191
++s->getFrontendCounters ().NumIterableTypeBodyASTScopeExpansions ;
1216
1192
}
0 commit comments