@@ -82,8 +82,7 @@ class ScopeCreator final {
8282 if (auto *ip = child->insertionPointForDeferredExpansion ().getPtrOrNull ())
8383 return ip;
8484
85- ASTScopeImpl *insertionPoint =
86- child->expandAndBeCurrentDetectingRecursion (*this );
85+ ASTScopeImpl *insertionPoint = child->expandAndBeCurrent (*this );
8786 return insertionPoint;
8887 }
8988
@@ -224,15 +223,18 @@ ASTSourceFileScope *ASTScope::createScopeTree(SourceFile *SF) {
224223}
225224
226225void ASTSourceFileScope::buildFullyExpandedTree () {
227- expandAndBeCurrentDetectingRecursion (*scopeCreator);
226+ if (!getWasExpanded ())
227+ expandAndBeCurrent (*scopeCreator);
228228 preOrderChildrenDo ([&](ASTScopeImpl *s) {
229- s->expandAndBeCurrentDetectingRecursion (*scopeCreator);
229+ if (!s->getWasExpanded ())
230+ s->expandAndBeCurrent (*scopeCreator);
230231 });
231232}
232233
233234void ASTSourceFileScope::
234235 buildEnoughOfTreeForTopLevelExpressionsButDontRequestGenericsOrExtendedNominals () {
235- expandAndBeCurrentDetectingRecursion (*scopeCreator);
236+ if (!getWasExpanded ())
237+ expandAndBeCurrent (*scopeCreator);
236238}
237239
238240void ASTSourceFileScope::expandFunctionBody (AbstractFunctionDecl *AFD) {
@@ -242,7 +244,8 @@ void ASTSourceFileScope::expandFunctionBody(AbstractFunctionDecl *AFD) {
242244 if (sr.isInvalid ())
243245 return ;
244246 ASTScopeImpl *bodyScope = findInnermostEnclosingScope (sr.Start , nullptr );
245- bodyScope->expandAndBeCurrentDetectingRecursion (*scopeCreator);
247+ if (!bodyScope->getWasExpanded ())
248+ bodyScope->expandAndBeCurrent (*scopeCreator);
246249}
247250
248251ASTSourceFileScope::ASTSourceFileScope (SourceFile *SF,
@@ -411,8 +414,6 @@ class NodeAdder
411414 endLocForBraceStmt = *endLoc;
412415
413416 ASTContext &ctx = scopeCreator.getASTContext ();
414- if (auto *s = ctx.Stats )
415- ++s->getFrontendCounters ().NumBraceStmtASTScopes ;
416417
417418 return
418419 scopeCreator.constructExpandAndInsert <BraceStmtScope>(
@@ -578,44 +579,33 @@ ScopeCreator::addPatternBindingToScopeTree(PatternBindingDecl *patternBinding,
578579
579580void ASTScopeImpl::addChild (ASTScopeImpl *child, ASTContext &ctx) {
580581 ASTScopeAssert (!child->getParent (), " child should not already have parent" );
581- child->parent = this ;
582+ child->parentAndWasExpanded . setPointer ( this ) ;
582583
583584#ifndef NDEBUG
584585 checkSourceRangeBeforeAddingChild (child, ctx);
585586#endif
586587
587588 // If this is the first time we've added children, notify the ASTContext
588589 // 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 ())
591591 ctx.addDestructorCleanup (storedChildren);
592- haveAddedCleanup = true ;
593- }
592+
594593 storedChildren.push_back (child);
595594}
596595
597596#pragma mark implementations of expansion
598597
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-
615598ASTScopeImpl *ASTScopeImpl::expandAndBeCurrent (ScopeCreator &scopeCreator) {
616599 ASTScopeAssert (!getWasExpanded (),
617600 " Cannot expand the same scope twice" );
618601
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+
619609 auto *insertionPoint = expandSpecifically (scopeCreator);
620610 ASTScopeAssert (!insertionPointForDeferredExpansion () ||
621611 insertionPointForDeferredExpansion ().get () ==
@@ -624,8 +614,6 @@ ASTScopeImpl *ASTScopeImpl::expandAndBeCurrent(ScopeCreator &scopeCreator) {
624614 " accurate before expansion, the insertion point before "
625615 " expansion must be the same as after expansion." );
626616
627- setWasExpanded ();
628-
629617 return insertionPoint;
630618}
631619
@@ -826,9 +814,6 @@ BraceStmtScope::expandAScopeThatCreatesANewInsertionPoint(
826814 nd, insertionPoint, endLoc);
827815 }
828816
829- if (auto *s = scopeCreator.getASTContext ().Stats )
830- ++s->getFrontendCounters ().NumBraceStmtASTScopeExpansions ;
831-
832817 return {
833818 insertionPoint,
834819 " For top-level code decls, need the scope under, say a guard statment." };
@@ -1085,24 +1070,17 @@ ASTScopeImpl *GenericTypeOrExtensionWherePortion::expandScope(
10851070
10861071#pragma mark createBodyScope
10871072
1088- void IterableTypeScope::countBodies (ScopeCreator &scopeCreator) const {
1089- if (auto *s = scopeCreator.getASTContext ().Stats )
1090- ++s->getFrontendCounters ().NumIterableTypeBodyASTScopes ;
1091- }
1092-
10931073void ExtensionScope::createBodyScope (ASTScopeImpl *leaf,
10941074 ScopeCreator &scopeCreator) {
10951075 scopeCreator.constructWithPortionExpandAndInsert <ExtensionScope,
10961076 IterableTypeBodyPortion>(
10971077 leaf, decl);
1098- countBodies (scopeCreator);
10991078}
11001079void NominalTypeScope::createBodyScope (ASTScopeImpl *leaf,
11011080 ScopeCreator &scopeCreator) {
11021081 scopeCreator.constructWithPortionExpandAndInsert <NominalTypeScope,
11031082 IterableTypeBodyPortion>(
11041083 leaf, decl);
1105- countBodies (scopeCreator);
11061084}
11071085
11081086#pragma mark createTrailingWhereClauseScope
@@ -1192,9 +1170,6 @@ void GenericTypeOrExtensionScope::expandBody(ScopeCreator &) {}
11921170void IterableTypeScope::expandBody (ScopeCreator &scopeCreator) {
11931171 for (auto *d : getIterableDeclContext ().get ()->getMembers ())
11941172 scopeCreator.addToScopeTree (ASTNode (d), this );
1195-
1196- if (auto *s = scopeCreator.getASTContext ().Stats )
1197- ++s->getFrontendCounters ().NumIterableTypeBodyASTScopeExpansions ;
11981173}
11991174
12001175#pragma mark getScopeCreator
@@ -1241,16 +1216,3 @@ void ast_scope::simple_display(llvm::raw_ostream &out,
12411216 const ScopeCreator *scopeCreator) {
12421217 scopeCreator->print (out);
12431218}
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