@@ -266,13 +266,9 @@ class UnqualifiedLookupFactory {
266
266
}
267
267
};
268
268
269
- // / Return status and new selfDC and new DC
270
- ASTScopeLookupState
271
- lookInScopeForASTScopeLookup (const ASTScopeLookupState);
269
+ void lookInScopeForASTScopeLookup (const ASTScopeLookupState);
272
270
273
- // / Returns status and selfDC and DC
274
- ASTScopeLookupState
275
- lookIntoDeclarationContextForASTScopeLookup (ASTScopeLookupState);
271
+ void lookIntoDeclarationContextForASTScopeLookup (ASTScopeLookupState);
276
272
// / Can lookup stop searching for results, assuming hasn't looked for outer
277
273
// / results yet?
278
274
bool isFirstResultEnough () const ;
@@ -467,15 +463,7 @@ UnqualifiedLookupFactory::experimentallyLookInASTScopes(DeclContext *const start
467
463
// Walk scopes outward from the innermost scope until we find something.
468
464
469
465
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);
479
467
}
480
468
481
469
std::pair<const ASTScope *, bool >
@@ -509,8 +497,7 @@ UnqualifiedLookupFactory::nonoperatorScopeForASTScopeLookup(
509
497
return std::make_pair (lookupScope, isCascadingUse);
510
498
}
511
499
512
- UnqualifiedLookupFactory::ASTScopeLookupState
513
- UnqualifiedLookupFactory::lookInScopeForASTScopeLookup (
500
+ void UnqualifiedLookupFactory::lookInScopeForASTScopeLookup (
514
501
const ASTScopeLookupState state) {
515
502
516
503
// Perform local lookup within this scope.
@@ -522,102 +509,116 @@ UnqualifiedLookupFactory::lookInScopeForASTScopeLookup(
522
509
recordCompletionOfAScope ();
523
510
// If we found anything, we're done.
524
511
if (isFirstResultEnough ())
525
- return state. withNoScope () ;
512
+ return ;
526
513
527
514
// When we are in the body of a method, get the 'self' declaration.
528
515
if (state.scope ->getKind () == ASTScopeKind::AbstractFunctionBody &&
529
516
state.scope ->getAbstractFunctionDecl ()
530
517
->getDeclContext ()
531
518
->isTypeContext ()) {
532
- return state.withSelfDC (state.scope ->getAbstractFunctionDecl ());
519
+ lookInScopeForASTScopeLookup (
520
+ state.withSelfDC (state.scope ->getAbstractFunctionDecl ())
521
+ .withParentScope ());
522
+ return ;
533
523
}
534
524
535
525
// If there is a declaration context associated with this scope, we might
536
526
// want to look in it.
537
- return lookIntoDeclarationContextForASTScopeLookup (state);
527
+ lookIntoDeclarationContextForASTScopeLookup (state);
538
528
}
539
529
540
- UnqualifiedLookupFactory::ASTScopeLookupState
541
- UnqualifiedLookupFactory::lookIntoDeclarationContextForASTScopeLookup (
530
+ void UnqualifiedLookupFactory::lookIntoDeclarationContextForASTScopeLookup (
542
531
ASTScopeLookupState stateArg) {
543
-
532
+
544
533
DeclContext *scopeDC = stateArg.scope ->getDeclContext ();
545
- if (!scopeDC)
546
- return stateArg;
534
+ if (!scopeDC) {
535
+ lookInScopeForASTScopeLookup (stateArg.withParentScope ());
536
+ return ;
537
+ }
547
538
548
539
// If we haven't determined whether we have a cascading use, do so now.
549
540
const bool isCascadingUseResult = resolveIsCascadingUse (
550
541
scopeDC, stateArg.isCascadingUse , /* onlyCareAboutFunctionBody=*/ false );
551
-
552
- const ASTScopeLookupState defaultReturnState = stateArg.withResolvedIsCascadingUse (isCascadingUseResult);
542
+
543
+ const ASTScopeLookupState defaultNextState =
544
+ stateArg.withResolvedIsCascadingUse (isCascadingUseResult)
545
+ .withParentScope ();
553
546
554
547
// Pattern binding initializers are only interesting insofar as they
555
548
// affect lookup in an enclosing nominal type or extension thereof.
556
549
if (auto *bindingInit = dyn_cast<PatternBindingInitializer>(scopeDC)) {
557
550
// Lazy variable initializer contexts have a 'self' parameter for
558
551
// 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 ;
563
556
}
564
557
565
558
// Default arguments only have 'static' access to the members of the
566
559
// 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
+ }
570
564
// Functions/initializers/deinitializers are only interesting insofar as
571
565
// 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
+ }
575
570
// 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
+ }
579
575
// 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
+ }
583
580
// 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
+ }
587
585
// 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
+ }
591
590
// Lookup in the source file's scope marks the end.
592
591
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 ;
595
595
}
596
596
597
597
// We have a nominal type or an extension thereof. Perform lookup into
598
598
// the nominal type.
599
599
auto nominal = scopeDC->getSelfNominalTypeDecl ();
600
- if (!nominal)
601
- return defaultReturnState;
602
-
600
+ if (!nominal) {
601
+ lookInScopeForASTScopeLookup (defaultNextState);
602
+ return ;
603
+ }
603
604
// Dig out the type we're looking into.
604
605
using LookupDecls = SmallVector<NominalTypeDecl *, 2 >;
605
606
LookupDecls lookupDecls;
606
607
populateLookupDeclsFromContext (scopeDC, lookupDecls);
607
608
608
-
609
609
// 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);
612
613
613
614
SmallVector<ValueDecl *, 4 > lookup;
614
615
scopeDC->lookupQualified (lookupDecls, Name, options, lookup);
615
616
616
617
auto startIndex = Results.size ();
617
618
for (auto result : lookup) {
618
619
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 ;
621
622
Results.push_back (LookupResultEntry (baseDC, result));
622
623
}
623
624
@@ -640,15 +641,14 @@ UnqualifiedLookupFactory::lookIntoDeclarationContextForASTScopeLookup(
640
641
641
642
recordCompletionOfAScope ();
642
643
if (isFirstResultEnough ())
643
- return defaultReturnState. withNoScope () ;
644
+ return ;
644
645
}
645
646
}
646
-
647
647
// Forget the 'self' declaration.
648
- return defaultReturnState .withSelfDC (nullptr );
648
+ lookInScopeForASTScopeLookup (defaultNextState .withSelfDC (nullptr ) );
649
649
}
650
650
651
- #pragma mark context-based lookup declarations
651
+ #pragma mark context-based lookup definitions
652
652
653
653
void
654
654
UnqualifiedLookupFactory::lookInDeclContexts (DeclContext *dc, const Optional <bool > isCascadingUseArg) {
0 commit comments