@@ -654,9 +654,8 @@ class ScopeCreator final {
654
654
bool containsAllDeclContextsFromAST () {
655
655
auto allDeclContexts = findLocalizableDeclContextsInAST ();
656
656
llvm::DenseMap<const DeclContext *, const ASTScopeImpl *> bogusDCs;
657
- bool rebuilt = false ;
658
657
sourceFileScope->preOrderDo ([&](ASTScopeImpl *scope) {
659
- rebuilt |= scope->reexpandIfObsolete (*this );
658
+ scope->expandAndBeCurrentDetectingRecursion (*this );
660
659
});
661
660
sourceFileScope->postOrderDo ([&](ASTScopeImpl *scope) {
662
661
if (auto *dc = scope->getDeclContext ().getPtrOrNull ()) {
@@ -673,8 +672,6 @@ class ScopeCreator final {
673
672
d->getSourceRange ().dump (ctx.SourceMgr );
674
673
llvm::errs () << " : " ;
675
674
d->dump (llvm::errs ());
676
- if (rebuilt)
677
- llvm::errs () << " (rebuilt)" ;
678
675
llvm::errs () << " \n " ;
679
676
};
680
677
bool foundOmission = false ;
@@ -750,35 +747,17 @@ ASTSourceFileScope *ASTScope::createScopeTree(SourceFile *SF) {
750
747
}
751
748
752
749
void ASTSourceFileScope::buildFullyExpandedTree () {
753
- addNewDeclsToScopeTree ();
754
- preOrderChildrenDo (
755
- [&](ASTScopeImpl *s) { s->reexpandIfObsolete (*scopeCreator); });
750
+ expandAndBeCurrentDetectingRecursion (*scopeCreator);
751
+ preOrderChildrenDo ([&](ASTScopeImpl *s) {
752
+ s->expandAndBeCurrentDetectingRecursion (*scopeCreator);
753
+ });
756
754
}
757
755
758
756
void ASTSourceFileScope::
759
757
buildEnoughOfTreeForTopLevelExpressionsButDontRequestGenericsOrExtendedNominals () {
760
- addNewDeclsToScopeTree ( );
758
+ expandAndBeCurrentDetectingRecursion (*scopeCreator );
761
759
}
762
760
763
- void ASTSourceFileScope::addNewDeclsToScopeTree () {
764
- ASTScopeAssert (SF && scopeCreator,
765
- " Must already have a SourceFile and a ScopeCreator." );
766
- ArrayRef<Decl *> decls = SF->Decls ;
767
- // Assume that decls are only added at the end, in source order
768
- ArrayRef<Decl *> newDecls = decls.slice (numberOfDeclsAlreadySeen);
769
- std::vector<ASTNode> newNodes (newDecls.begin (), newDecls.end ());
770
- insertionPoint =
771
- scopeCreator->addSiblingsToScopeTree (insertionPoint, this , newNodes);
772
-
773
- // TODO: use regular expansion machinery for ASTSourceFileScope
774
- // rdar://55562483
775
- numberOfDeclsAlreadySeen = SF->Decls .size ();
776
- setWasExpanded ();
777
-
778
- // Too slow to perform all the time:
779
- // ASTScopeAssert(scopeCreator->containsAllDeclContextsFromAST(),
780
- // "ASTScope tree missed some DeclContexts or made some up");
781
- }
782
761
783
762
ASTSourceFileScope::ASTSourceFileScope (SourceFile *SF,
784
763
ScopeCreator *scopeCreator)
@@ -1094,10 +1073,33 @@ ASTScopeImpl::expandAndBeCurrentDetectingRecursion(ScopeCreator &scopeCreator) {
1094
1073
llvm::Expected<ASTScopeImpl *>
1095
1074
ExpandASTScopeRequest::evaluate (Evaluator &evaluator, ASTScopeImpl *parent,
1096
1075
ScopeCreator *scopeCreator) const {
1097
- return parent->expandAndBeCurrent (*scopeCreator);
1076
+ auto *insertionPoint = parent->expandAndBeCurrent (*scopeCreator);
1077
+ ASTScopeAssert (insertionPoint,
1078
+ " Used to return a null pointer if the insertion point would "
1079
+ " not be used, but it breaks the request dependency hashing" );
1080
+ return insertionPoint;
1081
+ }
1082
+
1083
+ bool ASTScopeImpl::doesExpansionOnlyAddNewDeclsAtEnd () const { return false ; }
1084
+ bool ASTSourceFileScope::doesExpansionOnlyAddNewDeclsAtEnd () const {
1085
+ return true ;
1098
1086
}
1099
1087
1100
1088
ASTScopeImpl *ASTScopeImpl::expandAndBeCurrent (ScopeCreator &scopeCreator) {
1089
+
1090
+ // We might be reexpanding, so save any scopes that were inserted here from
1091
+ // above it in the AST
1092
+ auto astAncestorScopes = rescueASTAncestorScopesForReuseFromMeOrDescendants ();
1093
+ ASTScopeAssert (astAncestorScopes.empty () ||
1094
+ !doesExpansionOnlyAddNewDeclsAtEnd (),
1095
+ " ASTSourceFileScope has no ancestors to be rescued." );
1096
+
1097
+ // If reexpanding, we need to remove descendant decls from the duplication set
1098
+ // in order to re-add them as sub-scopes. Since expansion only adds new Decls
1099
+ // at end, don't bother with descendants
1100
+ if (!doesExpansionOnlyAddNewDeclsAtEnd ())
1101
+ disownDescendants (scopeCreator);
1102
+
1101
1103
auto *insertionPoint = expandSpecifically (scopeCreator);
1102
1104
if (scopeCreator.shouldBeLazy ()) {
1103
1105
ASTScopeAssert (!insertionPointForDeferredExpansion () ||
@@ -1107,6 +1109,7 @@ ASTScopeImpl *ASTScopeImpl::expandAndBeCurrent(ScopeCreator &scopeCreator) {
1107
1109
" accurate before expansion, the insertion point before "
1108
1110
" expansion must be the same as after expansion." );
1109
1111
}
1112
+ replaceASTAncestorScopes (astAncestorScopes);
1110
1113
setWasExpanded ();
1111
1114
beCurrent ();
1112
1115
ASTScopeAssert (checkSourceRangeAfterExpansion (scopeCreator.getASTContext ()),
@@ -1132,6 +1135,7 @@ ASTScopeImpl *ASTScopeImpl::expandAndBeCurrent(ScopeCreator &scopeCreator) {
1132
1135
#define NO_EXPANSION (Scope ) \
1133
1136
ASTScopeImpl *Scope::expandSpecifically (ScopeCreator &) { return this ; }
1134
1137
1138
+ CREATES_NEW_INSERTION_POINT (ASTSourceFileScope)
1135
1139
CREATES_NEW_INSERTION_POINT(ParameterListScope)
1136
1140
CREATES_NEW_INSERTION_POINT(ConditionalClauseScope)
1137
1141
CREATES_NEW_INSERTION_POINT(GuardStmtScope)
@@ -1163,7 +1167,6 @@ NO_NEW_INSERTION_POINT(WhileStmtScope)
1163
1167
NO_NEW_INSERTION_POINT(WholeClosureScope)
1164
1168
1165
1169
NO_EXPANSION(GenericParamScope)
1166
- NO_EXPANSION(ASTSourceFileScope)
1167
1170
NO_EXPANSION(ClosureParametersScope)
1168
1171
NO_EXPANSION(SpecializeAttributeScope)
1169
1172
NO_EXPANSION(ConditionalClausePatternUseScope)
@@ -1172,6 +1175,22 @@ NO_EXPANSION(LookupParentDiversionScope)
1172
1175
#undef CREATES_NEW_INSERTION_POINT
1173
1176
#undef NO_NEW_INSERTION_POINT
1174
1177
1178
+ AnnotatedInsertionPoint
1179
+ ASTSourceFileScope::expandAScopeThatCreatesANewInsertionPoint (
1180
+ ScopeCreator &scopeCreator) {
1181
+ ASTScopeAssert (SF, " Must already have a SourceFile." );
1182
+ ArrayRef<Decl *> decls = SF->Decls ;
1183
+ // Assume that decls are only added at the end, in source order
1184
+ ArrayRef<Decl *> newDecls = decls.slice (numberOfDeclsAlreadySeen);
1185
+ std::vector<ASTNode> newNodes (newDecls.begin (), newDecls.end ());
1186
+ insertionPoint =
1187
+ scopeCreator.addSiblingsToScopeTree (insertionPoint, this , newNodes);
1188
+ // Too slow to perform all the time:
1189
+ // ASTScopeAssert(scopeCreator->containsAllDeclContextsFromAST(),
1190
+ // "ASTScope tree missed some DeclContexts or made some up");
1191
+ return {insertionPoint, " Next time decls are added they go here." };
1192
+ }
1193
+
1175
1194
AnnotatedInsertionPoint
1176
1195
ParameterListScope::expandAScopeThatCreatesANewInsertionPoint (
1177
1196
ScopeCreator &scopeCreator) {
@@ -1230,7 +1249,8 @@ PatternEntryInitializerScope::expandAScopeThatCreatesANewInsertionPoint(
1230
1249
" get its endpoint in order to push back start of "
1231
1250
" PatternEntryUseScope" };
1232
1251
1233
- return {nullptr , " Unused" };
1252
+ // null pointer here blows up request printing
1253
+ return {getParent ().get (), " Unused" };
1234
1254
}
1235
1255
1236
1256
AnnotatedInsertionPoint
@@ -1308,11 +1328,6 @@ TopLevelCodeScope::expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &
1308
1328
1309
1329
#pragma mark expandAScopeThatDoesNotCreateANewInsertionPoint
1310
1330
1311
- void ASTSourceFileScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
1312
- ScopeCreator &scopeCreator) {
1313
- ASTScope_unreachable (" expanded by addNewDeclsToScopeTree()" );
1314
- }
1315
-
1316
1331
// Create child scopes for every declaration in a body.
1317
1332
1318
1333
void AbstractFunctionDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
@@ -1752,27 +1767,6 @@ void IterableTypeScope::expandBody(ScopeCreator &scopeCreator) {
1752
1767
++s->getFrontendCounters ().NumIterableTypeBodyASTScopeExpansions ;
1753
1768
}
1754
1769
1755
- #pragma mark - reexpandIfObsolete
1756
-
1757
- bool ASTScopeImpl::reexpandIfObsolete (ScopeCreator &scopeCreator) {
1758
- if (isCurrent () &&
1759
- !scopeCreator.getASTContext ().LangOpts .StressASTScopeLookup ) {
1760
- ASTScopeAssert (getWasExpanded (), " Cannot be current if unexpanded." );
1761
- return false ;
1762
- }
1763
- reexpand (scopeCreator);
1764
- return true ;
1765
- }
1766
-
1767
- void ASTScopeImpl::reexpand (ScopeCreator &scopeCreator) {
1768
- auto astAncestorScopes = rescueASTAncestorScopesForReuseFromMeOrDescendants ();
1769
- disownDescendants (scopeCreator);
1770
- // If the expansion recurses back into the tree for lookup, the ASTAncestor
1771
- // scopes will have already been rescued and won't be found!
1772
- expandAndBeCurrentDetectingRecursion (scopeCreator);
1773
- replaceASTAncestorScopes (astAncestorScopes);
1774
- }
1775
-
1776
1770
#pragma mark getScopeCreator
1777
1771
ScopeCreator &ASTScopeImpl::getScopeCreator () {
1778
1772
return getParent ().get ()->getScopeCreator ();
@@ -1847,13 +1841,25 @@ IterableTypeBodyPortion::insertionPointForDeferredExpansion(
1847
1841
return s->getParent ().get ();
1848
1842
}
1849
1843
1844
+ bool ASTScopeImpl::isExpansionNeeded (const ScopeCreator &scopeCreator) const {
1845
+ return !isCurrent () ||
1846
+ scopeCreator.getASTContext ().LangOpts .StressASTScopeLookup ;
1847
+ }
1848
+
1850
1849
bool ASTScopeImpl::isCurrent () const {
1851
1850
return getWasExpanded () && isCurrentIfWasExpanded ();
1852
1851
}
1853
1852
1854
1853
void ASTScopeImpl::beCurrent () {}
1855
1854
bool ASTScopeImpl::isCurrentIfWasExpanded () const { return true ; }
1856
1855
1856
+ void ASTSourceFileScope::beCurrent () {
1857
+ numberOfDeclsAlreadySeen = SF->Decls .size ();
1858
+ }
1859
+ bool ASTSourceFileScope::isCurrentIfWasExpanded () const {
1860
+ return SF->Decls .size () == numberOfDeclsAlreadySeen;
1861
+ }
1862
+
1857
1863
void IterableTypeScope::beCurrent () { portion->beCurrent (this ); }
1858
1864
bool IterableTypeScope::isCurrentIfWasExpanded () const {
1859
1865
return portion->isCurrentIfWasExpanded (this );
@@ -2092,3 +2098,17 @@ void ast_scope::simple_display(llvm::raw_ostream &out,
2092
2098
const ScopeCreator *scopeCreator) {
2093
2099
scopeCreator->print (out);
2094
2100
}
2101
+
2102
+ // ----------------------------------------------------------------------------//
2103
+ // ExpandASTScopeRequest computation.
2104
+ // ----------------------------------------------------------------------------//
2105
+
2106
+ bool ExpandASTScopeRequest::isCached () const {
2107
+ ASTScopeImpl *scope = std::get<0 >(getStorage ());
2108
+ ScopeCreator *scopeCreator = std::get<1 >(getStorage ());
2109
+ return !scope->isExpansionNeeded (*scopeCreator);
2110
+ }
2111
+
2112
+ Optional<ASTScopeImpl *> ExpandASTScopeRequest::getCachedResult () const {
2113
+ return std::get<0 >(getStorage ());
2114
+ }
0 commit comments