@@ -82,8 +82,7 @@ class ScopeCreator final {
82
82
if (auto *ip = child->insertionPointForDeferredExpansion ().getPtrOrNull ())
83
83
return ip;
84
84
85
- ASTScopeImpl *insertionPoint =
86
- child->expandAndBeCurrentDetectingRecursion (*this );
85
+ ASTScopeImpl *insertionPoint = child->expandAndBeCurrent (*this );
87
86
return insertionPoint;
88
87
}
89
88
@@ -224,15 +223,18 @@ ASTSourceFileScope *ASTScope::createScopeTree(SourceFile *SF) {
224
223
}
225
224
226
225
void ASTSourceFileScope::buildFullyExpandedTree () {
227
- expandAndBeCurrentDetectingRecursion (*scopeCreator);
226
+ if (!getWasExpanded ())
227
+ expandAndBeCurrent (*scopeCreator);
228
228
preOrderChildrenDo ([&](ASTScopeImpl *s) {
229
- s->expandAndBeCurrentDetectingRecursion (*scopeCreator);
229
+ if (!s->getWasExpanded ())
230
+ s->expandAndBeCurrent (*scopeCreator);
230
231
});
231
232
}
232
233
233
234
void ASTSourceFileScope::
234
235
buildEnoughOfTreeForTopLevelExpressionsButDontRequestGenericsOrExtendedNominals () {
235
- expandAndBeCurrentDetectingRecursion (*scopeCreator);
236
+ if (!getWasExpanded ())
237
+ expandAndBeCurrent (*scopeCreator);
236
238
}
237
239
238
240
void ASTSourceFileScope::expandFunctionBody (AbstractFunctionDecl *AFD) {
@@ -242,7 +244,8 @@ void ASTSourceFileScope::expandFunctionBody(AbstractFunctionDecl *AFD) {
242
244
if (sr.isInvalid ())
243
245
return ;
244
246
ASTScopeImpl *bodyScope = findInnermostEnclosingScope (sr.Start , nullptr );
245
- bodyScope->expandAndBeCurrentDetectingRecursion (*scopeCreator);
247
+ if (!bodyScope->getWasExpanded ())
248
+ bodyScope->expandAndBeCurrent (*scopeCreator);
246
249
}
247
250
248
251
ASTSourceFileScope::ASTSourceFileScope (SourceFile *SF,
@@ -411,8 +414,6 @@ class NodeAdder
411
414
endLocForBraceStmt = *endLoc;
412
415
413
416
ASTContext &ctx = scopeCreator.getASTContext ();
414
- if (auto *s = ctx.Stats )
415
- ++s->getFrontendCounters ().NumBraceStmtASTScopes ;
416
417
417
418
return
418
419
scopeCreator.constructExpandAndInsert <BraceStmtScope>(
@@ -578,44 +579,33 @@ ScopeCreator::addPatternBindingToScopeTree(PatternBindingDecl *patternBinding,
578
579
579
580
void ASTScopeImpl::addChild (ASTScopeImpl *child, ASTContext &ctx) {
580
581
ASTScopeAssert (!child->getParent (), " child should not already have parent" );
581
- child->parent = this ;
582
+ child->parentAndWasExpanded . setPointer ( this ) ;
582
583
583
584
#ifndef NDEBUG
584
585
checkSourceRangeBeforeAddingChild (child, ctx);
585
586
#endif
586
587
587
588
// If this is the first time we've added children, notify the ASTContext
588
589
// that there's a SmallVector that needs to be cleaned up.
589
- // FIXME: If we had access to SmallVector::isSmall(), we could do better.
590
- if (storedChildren.empty () && !haveAddedCleanup) {
590
+ if (storedChildren.empty ())
591
591
ctx.addDestructorCleanup (storedChildren);
592
- haveAddedCleanup = true ;
593
- }
592
+
594
593
storedChildren.push_back (child);
595
594
}
596
595
597
596
#pragma mark implementations of expansion
598
597
599
- ASTScopeImpl *
600
- ASTScopeImpl::expandAndBeCurrentDetectingRecursion (ScopeCreator &scopeCreator) {
601
- return evaluateOrDefault (scopeCreator.getASTContext ().evaluator ,
602
- ExpandASTScopeRequest{this , &scopeCreator}, nullptr );
603
- }
604
-
605
- ASTScopeImpl *
606
- ExpandASTScopeRequest::evaluate (Evaluator &evaluator, ASTScopeImpl *parent,
607
- ScopeCreator *scopeCreator) const {
608
- auto *insertionPoint = parent->expandAndBeCurrent (*scopeCreator);
609
- ASTScopeAssert (insertionPoint,
610
- " Used to return a null pointer if the insertion point would "
611
- " not be used, but it breaks the request dependency hashing" );
612
- return insertionPoint;
613
- }
614
-
615
598
ASTScopeImpl *ASTScopeImpl::expandAndBeCurrent (ScopeCreator &scopeCreator) {
616
599
ASTScopeAssert (!getWasExpanded (),
617
600
" Cannot expand the same scope twice" );
618
601
602
+ // Set the flag before we actually expand, to detect re-entrant calls
603
+ // via the above assertion.
604
+ setWasExpanded ();
605
+
606
+ if (auto *s = scopeCreator.getASTContext ().Stats )
607
+ ++s->getFrontendCounters ().NumASTScopeExpansions ;
608
+
619
609
auto *insertionPoint = expandSpecifically (scopeCreator);
620
610
ASTScopeAssert (!insertionPointForDeferredExpansion () ||
621
611
insertionPointForDeferredExpansion ().get () ==
@@ -624,8 +614,6 @@ ASTScopeImpl *ASTScopeImpl::expandAndBeCurrent(ScopeCreator &scopeCreator) {
624
614
" accurate before expansion, the insertion point before "
625
615
" expansion must be the same as after expansion." );
626
616
627
- setWasExpanded ();
628
-
629
617
return insertionPoint;
630
618
}
631
619
@@ -826,9 +814,6 @@ BraceStmtScope::expandAScopeThatCreatesANewInsertionPoint(
826
814
nd, insertionPoint, endLoc);
827
815
}
828
816
829
- if (auto *s = scopeCreator.getASTContext ().Stats )
830
- ++s->getFrontendCounters ().NumBraceStmtASTScopeExpansions ;
831
-
832
817
return {
833
818
insertionPoint,
834
819
" For top-level code decls, need the scope under, say a guard statment." };
@@ -1085,24 +1070,17 @@ ASTScopeImpl *GenericTypeOrExtensionWherePortion::expandScope(
1085
1070
1086
1071
#pragma mark createBodyScope
1087
1072
1088
- void IterableTypeScope::countBodies (ScopeCreator &scopeCreator) const {
1089
- if (auto *s = scopeCreator.getASTContext ().Stats )
1090
- ++s->getFrontendCounters ().NumIterableTypeBodyASTScopes ;
1091
- }
1092
-
1093
1073
void ExtensionScope::createBodyScope (ASTScopeImpl *leaf,
1094
1074
ScopeCreator &scopeCreator) {
1095
1075
scopeCreator.constructWithPortionExpandAndInsert <ExtensionScope,
1096
1076
IterableTypeBodyPortion>(
1097
1077
leaf, decl);
1098
- countBodies (scopeCreator);
1099
1078
}
1100
1079
void NominalTypeScope::createBodyScope (ASTScopeImpl *leaf,
1101
1080
ScopeCreator &scopeCreator) {
1102
1081
scopeCreator.constructWithPortionExpandAndInsert <NominalTypeScope,
1103
1082
IterableTypeBodyPortion>(
1104
1083
leaf, decl);
1105
- countBodies (scopeCreator);
1106
1084
}
1107
1085
1108
1086
#pragma mark createTrailingWhereClauseScope
@@ -1192,9 +1170,6 @@ void GenericTypeOrExtensionScope::expandBody(ScopeCreator &) {}
1192
1170
void IterableTypeScope::expandBody (ScopeCreator &scopeCreator) {
1193
1171
for (auto *d : getIterableDeclContext ().get ()->getMembers ())
1194
1172
scopeCreator.addToScopeTree (ASTNode (d), this );
1195
-
1196
- if (auto *s = scopeCreator.getASTContext ().Stats )
1197
- ++s->getFrontendCounters ().NumIterableTypeBodyASTScopeExpansions ;
1198
1173
}
1199
1174
1200
1175
#pragma mark getScopeCreator
@@ -1241,16 +1216,3 @@ void ast_scope::simple_display(llvm::raw_ostream &out,
1241
1216
const ScopeCreator *scopeCreator) {
1242
1217
scopeCreator->print (out);
1243
1218
}
1244
-
1245
- // ----------------------------------------------------------------------------//
1246
- // ExpandASTScopeRequest computation.
1247
- // ----------------------------------------------------------------------------//
1248
-
1249
- bool ExpandASTScopeRequest::isCached () const {
1250
- ASTScopeImpl *scope = std::get<0 >(getStorage ());
1251
- return scope->getWasExpanded ();
1252
- }
1253
-
1254
- Optional<ASTScopeImpl *> ExpandASTScopeRequest::getCachedResult () const {
1255
- return std::get<0 >(getStorage ());
1256
- }
0 commit comments