Skip to content

Commit 26247b4

Browse files
committed
Recursify ASTScope-based lookup
1 parent 3a16af0 commit 26247b4

File tree

1 file changed

+63
-63
lines changed

1 file changed

+63
-63
lines changed

lib/AST/UnqualifiedLookup.cpp

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,9 @@ class UnqualifiedLookupFactory {
266266
}
267267
};
268268

269-
/// Return status and new selfDC and new DC
270-
ASTScopeLookupState
271-
lookInScopeForASTScopeLookup(const ASTScopeLookupState);
269+
void lookInScopeForASTScopeLookup(const ASTScopeLookupState);
272270

273-
/// Returns status and selfDC and DC
274-
ASTScopeLookupState
275-
lookIntoDeclarationContextForASTScopeLookup(ASTScopeLookupState);
271+
void lookIntoDeclarationContextForASTScopeLookup(ASTScopeLookupState);
276272
/// Can lookup stop searching for results, assuming hasn't looked for outer
277273
/// results yet?
278274
bool isFirstResultEnough() const;
@@ -467,15 +463,7 @@ UnqualifiedLookupFactory::experimentallyLookInASTScopes(DeclContext *const start
467463
// Walk scopes outward from the innermost scope until we find something.
468464

469465
ASTScopeLookupState state{lookupScopeAndIsCascadingUse.first, nullptr, startDC, lookupScopeAndIsCascadingUse.second};
470-
while ( state.scope) {
471-
state = lookInScopeForASTScopeLookup(state);
472-
if (!state.scope)
473-
break;
474-
state = state.withParentScope();
475-
}
476-
if (isFirstResultEnough())
477-
return;
478-
lookInModuleScopeContext(DCAndResolvedIsCascadingUse{state.dc, state.isCascadingUse.getValue()});
466+
lookInScopeForASTScopeLookup(state);
479467
}
480468

481469
std::pair<const ASTScope *, bool>
@@ -509,8 +497,7 @@ UnqualifiedLookupFactory::nonoperatorScopeForASTScopeLookup(
509497
return std::make_pair(lookupScope, isCascadingUse);
510498
}
511499

512-
UnqualifiedLookupFactory::ASTScopeLookupState
513-
UnqualifiedLookupFactory::lookInScopeForASTScopeLookup(
500+
void UnqualifiedLookupFactory::lookInScopeForASTScopeLookup(
514501
const ASTScopeLookupState state) {
515502

516503
// Perform local lookup within this scope.
@@ -522,102 +509,116 @@ UnqualifiedLookupFactory::lookInScopeForASTScopeLookup(
522509
recordCompletionOfAScope();
523510
// If we found anything, we're done.
524511
if (isFirstResultEnough())
525-
return state.withNoScope();
512+
return;
526513

527514
// When we are in the body of a method, get the 'self' declaration.
528515
if (state.scope->getKind() == ASTScopeKind::AbstractFunctionBody &&
529516
state.scope->getAbstractFunctionDecl()
530517
->getDeclContext()
531518
->isTypeContext()) {
532-
return state.withSelfDC(state.scope->getAbstractFunctionDecl());
519+
lookInScopeForASTScopeLookup(
520+
state.withSelfDC(state.scope->getAbstractFunctionDecl())
521+
.withParentScope());
522+
return;
533523
}
534524

535525
// If there is a declaration context associated with this scope, we might
536526
// want to look in it.
537-
return lookIntoDeclarationContextForASTScopeLookup(state);
527+
lookIntoDeclarationContextForASTScopeLookup(state);
538528
}
539529

540-
UnqualifiedLookupFactory::ASTScopeLookupState
541-
UnqualifiedLookupFactory::lookIntoDeclarationContextForASTScopeLookup(
530+
void UnqualifiedLookupFactory::lookIntoDeclarationContextForASTScopeLookup(
542531
ASTScopeLookupState stateArg) {
543-
532+
544533
DeclContext *scopeDC = stateArg.scope->getDeclContext();
545-
if (!scopeDC)
546-
return stateArg;
534+
if (!scopeDC) {
535+
lookInScopeForASTScopeLookup(stateArg.withParentScope());
536+
return;
537+
}
547538

548539
// If we haven't determined whether we have a cascading use, do so now.
549540
const bool isCascadingUseResult = resolveIsCascadingUse(
550541
scopeDC, stateArg.isCascadingUse, /*onlyCareAboutFunctionBody=*/false);
551-
552-
const ASTScopeLookupState defaultReturnState = stateArg.withResolvedIsCascadingUse(isCascadingUseResult);
542+
543+
const ASTScopeLookupState defaultNextState =
544+
stateArg.withResolvedIsCascadingUse(isCascadingUseResult)
545+
.withParentScope();
553546

554547
// Pattern binding initializers are only interesting insofar as they
555548
// affect lookup in an enclosing nominal type or extension thereof.
556549
if (auto *bindingInit = dyn_cast<PatternBindingInitializer>(scopeDC)) {
557550
// Lazy variable initializer contexts have a 'self' parameter for
558551
// instance member lookup.
559-
if (bindingInit->getImplicitSelfDecl())
560-
return defaultReturnState.withSelfDC(bindingInit);
561-
562-
return defaultReturnState;
552+
lookInScopeForASTScopeLookup(bindingInit->getImplicitSelfDecl()
553+
? defaultNextState.withSelfDC(bindingInit)
554+
: defaultNextState);
555+
return;
563556
}
564557

565558
// Default arguments only have 'static' access to the members of the
566559
// enclosing type, if there is one.
567-
if (isa<DefaultArgumentInitializer>(scopeDC))
568-
return defaultReturnState;
569-
560+
if (isa<DefaultArgumentInitializer>(scopeDC)) {
561+
lookInScopeForASTScopeLookup(defaultNextState);
562+
return;
563+
}
570564
// Functions/initializers/deinitializers are only interesting insofar as
571565
// they affect lookup in an enclosing nominal type or extension thereof.
572-
if (isa<AbstractFunctionDecl>(scopeDC))
573-
return defaultReturnState;
574-
566+
if (isa<AbstractFunctionDecl>(scopeDC)) {
567+
lookInScopeForASTScopeLookup(defaultNextState);
568+
return;
569+
}
575570
// Subscripts have no lookup of their own.
576-
if (isa<SubscriptDecl>(scopeDC))
577-
return defaultReturnState;
578-
571+
if (isa<SubscriptDecl>(scopeDC)) {
572+
lookInScopeForASTScopeLookup(defaultNextState);
573+
return;
574+
}
579575
// Closures have no lookup of their own.
580-
if (isa<AbstractClosureExpr>(scopeDC))
581-
return defaultReturnState;
582-
576+
if (isa<AbstractClosureExpr>(scopeDC)) {
577+
lookInScopeForASTScopeLookup(defaultNextState);
578+
return;
579+
}
583580
// Top-level declarations have no lookup of their own.
584-
if (isa<TopLevelCodeDecl>(scopeDC))
585-
return defaultReturnState;
586-
581+
if (isa<TopLevelCodeDecl>(scopeDC)) {
582+
lookInScopeForASTScopeLookup(defaultNextState);
583+
return;
584+
}
587585
// Typealiases have no lookup of their own.
588-
if (isa<TypeAliasDecl>(scopeDC))
589-
return defaultReturnState;
590-
586+
if (isa<TypeAliasDecl>(scopeDC)) {
587+
lookInScopeForASTScopeLookup(defaultNextState);
588+
return;
589+
}
591590
// Lookup in the source file's scope marks the end.
592591
if (isa<SourceFile>(scopeDC)) {
593-
// FIXME: A bit of a hack.
594-
return defaultReturnState.withDC(scopeDC).withNoScope();
592+
lookInModuleScopeContext(
593+
DCAndResolvedIsCascadingUse{scopeDC, isCascadingUseResult});
594+
return;
595595
}
596596

597597
// We have a nominal type or an extension thereof. Perform lookup into
598598
// the nominal type.
599599
auto nominal = scopeDC->getSelfNominalTypeDecl();
600-
if (!nominal)
601-
return defaultReturnState;
602-
600+
if (!nominal) {
601+
lookInScopeForASTScopeLookup(defaultNextState);
602+
return;
603+
}
603604
// Dig out the type we're looking into.
604605
using LookupDecls = SmallVector<NominalTypeDecl *, 2>;
605606
LookupDecls lookupDecls;
606607
populateLookupDeclsFromContext(scopeDC, lookupDecls);
607608

608-
609609
// Perform lookup into the type.
610-
NLOptions options = baseNLOptions | (
611-
isCascadingUseResult ? NL_KnownCascadingDependency : NL_KnownNonCascadingDependency);
610+
NLOptions options =
611+
baseNLOptions | (isCascadingUseResult ? NL_KnownCascadingDependency
612+
: NL_KnownNonCascadingDependency);
612613

613614
SmallVector<ValueDecl *, 4> lookup;
614615
scopeDC->lookupQualified(lookupDecls, Name, options, lookup);
615616

616617
auto startIndex = Results.size();
617618
for (auto result : lookup) {
618619
auto *baseDC = scopeDC;
619-
if (!isa<TypeDecl>(result) && defaultReturnState.selfDC)
620-
baseDC = defaultReturnState.selfDC;
620+
if (!isa<TypeDecl>(result) && defaultNextState.selfDC)
621+
baseDC = defaultNextState.selfDC;
621622
Results.push_back(LookupResultEntry(baseDC, result));
622623
}
623624

@@ -640,15 +641,14 @@ UnqualifiedLookupFactory::lookIntoDeclarationContextForASTScopeLookup(
640641

641642
recordCompletionOfAScope();
642643
if (isFirstResultEnough())
643-
return defaultReturnState.withNoScope();
644+
return;
644645
}
645646
}
646-
647647
// Forget the 'self' declaration.
648-
return defaultReturnState.withSelfDC(nullptr);
648+
lookInScopeForASTScopeLookup(defaultNextState.withSelfDC(nullptr));
649649
}
650650

651-
#pragma mark context-based lookup declarations
651+
#pragma mark context-based lookup definitions
652652

653653
void
654654
UnqualifiedLookupFactory::lookInDeclContexts(DeclContext *dc, const Optional <bool> isCascadingUseArg) {

0 commit comments

Comments
 (0)