Skip to content

Commit 72b2096

Browse files
author
David Ungar
committed
Outer generics
1 parent 2f2bbfb commit 72b2096

File tree

2 files changed

+59
-38
lines changed

2 files changed

+59
-38
lines changed

include/swift/AST/ASTScope.h

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,13 @@ class ASTScopeImpl {
360360
return {false, isCascadingUse};
361361
}
362362

363-
364-
virtual bool lookInGenericParameters(Optional<bool> isCascadingUse,
365-
DeclConsumer) const;
363+
virtual bool lookInMyGenericParameters(Optional<bool> isCascadingUse,
364+
DeclConsumer) const;
366365

367366
// Consume the generic parameters in the context and its outer contexts
368-
static bool lookInMyAndOuterGenericParameters(const GenericContext *,
369-
Optional<bool> isCascadingUse,
370-
DeclConsumer);
367+
static bool lookInGenericParametersOf(NullablePtr<const GenericParamList>,
368+
Optional<bool> isCascadingUse,
369+
DeclConsumer);
371370

372371
NullablePtr<const ASTScopeImpl> parentIfNotChildOfTopScope() const {
373372
const auto *p = getParent().get();
@@ -579,8 +578,6 @@ class GenericTypeOrExtensionScope : public ASTScopeImpl {
579578
virtual std::string declKindName() const = 0;
580579
virtual bool doesDeclHaveABody() const;
581580
const char *portionName() const { return portion->portionName; }
582-
bool lookInGenericParameters(Optional<bool> isCascadingUse,
583-
DeclConsumer) const override;
584581
NullablePtr<DeclContext>
585582
computeSelfDCForParent(NullablePtr<DeclContext>) const override;
586583
Optional<bool> resolveIsCascadingUseForThisScope(
@@ -605,9 +602,18 @@ class GenericTypeOrExtensionScope : public ASTScopeImpl {
605602
virtual NullablePtr<const ASTScopeImpl> getLookupLimitForDecl() const;
606603
};
607604

608-
class IterableTypeScope : public GenericTypeOrExtensionScope {
605+
class GenericTypeScope : public GenericTypeOrExtensionScope {
606+
public:
607+
GenericTypeScope(const Portion *p) : GenericTypeOrExtensionScope(p) {}
608+
virtual ~GenericTypeScope() {}
609+
610+
bool lookInMyGenericParameters(Optional<bool> isCascadingUse,
611+
DeclConsumer) const override;
612+
};
613+
614+
class IterableTypeScope : public GenericTypeScope {
609615
public:
610-
IterableTypeScope(const Portion *p) : GenericTypeOrExtensionScope(p) {}
616+
IterableTypeScope(const Portion *p) : GenericTypeScope(p) {}
611617
virtual ~IterableTypeScope() {}
612618

613619
virtual SourceRange getBraces() const = 0;
@@ -659,13 +665,15 @@ class ExtensionScope final : public IterableTypeScope {
659665
ScopeCreator &) override;
660666
void createBodyScope(ASTScopeImpl *leaf, ScopeCreator &) override;
661667
NullablePtr<Decl> getDecl() const override { return decl; }
668+
bool lookInMyGenericParameters(Optional<bool> isCascadingUse,
669+
DeclConsumer) const override;
662670
};
663671

664-
class TypeAliasScope final : public GenericTypeOrExtensionScope {
672+
class TypeAliasScope final : public GenericTypeScope {
665673
public:
666674
TypeAliasDecl *const decl;
667675
TypeAliasScope(const Portion *p, TypeAliasDecl *e)
668-
: GenericTypeOrExtensionScope(p), decl(e) {}
676+
: GenericTypeScope(p), decl(e) {}
669677
virtual ~TypeAliasScope() {}
670678

671679
std::string declKindName() const override { return "TypeAlias"; }
@@ -675,11 +683,11 @@ class TypeAliasScope final : public GenericTypeOrExtensionScope {
675683
NullablePtr<Decl> getDecl() const override { return decl; }
676684
};
677685

678-
class OpaqueTypeScope final : public GenericTypeOrExtensionScope {
686+
class OpaqueTypeScope final : public GenericTypeScope {
679687
public:
680688
OpaqueTypeDecl *const decl;
681689
OpaqueTypeScope(const Portion *p, OpaqueTypeDecl *e)
682-
: GenericTypeOrExtensionScope(p), decl(e) {}
690+
: GenericTypeScope(p), decl(e) {}
683691
virtual ~OpaqueTypeScope() {}
684692

685693
std::string declKindName() const override { return "OpaqueType"; }
@@ -764,8 +772,8 @@ class AbstractFunctionDeclScope final : public ASTScopeImpl {
764772

765773
protected:
766774
Decl *getEnclosingAbstractFunctionOrSubscriptDecl() const override;
767-
bool lookInGenericParameters(Optional<bool> isCascadingUse,
768-
DeclConsumer) const override;
775+
bool lookInMyGenericParameters(Optional<bool> isCascadingUse,
776+
DeclConsumer) const override;
769777
Optional<bool>
770778
resolveIsCascadingUseForThisScope(Optional<bool>) const override;
771779
};
@@ -1246,8 +1254,8 @@ class SubscriptDeclScope final : public ASTScopeImpl {
12461254

12471255
protected:
12481256
Decl *getEnclosingAbstractFunctionOrSubscriptDecl() const override;
1249-
bool lookInGenericParameters(Optional<bool> isCascadingUse,
1250-
DeclConsumer) const override;
1257+
bool lookInMyGenericParameters(Optional<bool> isCascadingUse,
1258+
DeclConsumer) const override;
12511259
NullablePtr<AbstractStorageDecl>
12521260
getEnclosingAbstractStorageDecl() const override {
12531261
return decl;

lib/AST/ASTScopeLookup.cpp

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ Optional<bool> ASTScopeImpl::lookup(
202202
// you can say "self.name" to get a name shadowed by a generic but you
203203
// can't do the opposite to get a generic shadowed by a name.
204204
if (!skipSearchForGenericsAndMembers) {
205-
if (lookInGenericParameters(isCascadingUseForThisScope, consumer))
205+
if (lookInMyGenericParameters(isCascadingUseForThisScope, consumer))
206206
return isCascadingUseForThisScope;
207207
}
208208
// Dig out the type we're looking into.
@@ -244,23 +244,26 @@ Optional<bool> ASTScopeImpl::lookupInParent(
244244
consumer);
245245
}
246246

247-
#pragma mark lookInGenericParameters
247+
#pragma mark lookInMyGenericParameters
248248

249-
bool ASTScopeImpl::lookInGenericParameters(Optional<bool> isCascadingUse,
250-
ASTScopeImpl::DeclConsumer) const {
249+
bool ASTScopeImpl::lookInMyGenericParameters(Optional<bool> isCascadingUse,
250+
ASTScopeImpl::DeclConsumer) const {
251251
return false;
252252
}
253253

254-
bool AbstractFunctionDeclScope::lookInGenericParameters(
254+
bool AbstractFunctionDeclScope::lookInMyGenericParameters(
255255
Optional<bool> isCascadingUse, ASTScopeImpl::DeclConsumer consumer) const {
256-
return lookInMyAndOuterGenericParameters(decl, isCascadingUse, consumer);
256+
return lookInGenericParametersOf(decl->getGenericParams(), isCascadingUse,
257+
consumer);
257258
}
258-
bool SubscriptDeclScope::lookInGenericParameters(
259+
260+
bool SubscriptDeclScope::lookInMyGenericParameters(
259261
Optional<bool> isCascadingUse, ASTScopeImpl::DeclConsumer consumer) const {
260-
return lookInMyAndOuterGenericParameters(decl, isCascadingUse, consumer);
262+
return lookInGenericParametersOf(decl->getGenericParams(), isCascadingUse,
263+
consumer);
261264
}
262265

263-
bool GenericTypeOrExtensionScope::lookInGenericParameters(
266+
bool GenericTypeScope::lookInMyGenericParameters(
264267
Optional<bool> isCascadingUse, ASTScopeImpl::DeclConsumer consumer) const {
265268
// For Decls:
266269
// WAIT, WHAT?! Isn't this covered by the GenericParamScope
@@ -271,25 +274,35 @@ bool GenericTypeOrExtensionScope::lookInGenericParameters(
271274
// Sigh... These must be here so that from body, we search generics before
272275
// members. But they also must be on the Decl scope for lookups starting from
273276
// generic parameters, where clauses, etc.
274-
return lookInMyAndOuterGenericParameters(getGenericContext(), isCascadingUse,
275-
consumer);
277+
return lookInGenericParametersOf(getGenericContext()->getGenericParams(),
278+
isCascadingUse, consumer);
276279
}
277280

278-
bool ASTScopeImpl::lookInMyAndOuterGenericParameters(
279-
const GenericContext *const gc, Optional<bool> isCascadingUse,
280-
ASTScopeImpl::DeclConsumer consumer) {
281-
for (auto *params = gc->getGenericParams(); params;
281+
bool ExtensionScope::lookInMyGenericParameters(
282+
Optional<bool> isCascadingUse, ASTScopeImpl::DeclConsumer consumer) const {
283+
// For extensions of nested types, must check outer parameters
284+
for (auto *params = decl->getGenericParams(); params;
282285
params = params->getOuterParameters()) {
283-
SmallVector<ValueDecl *, 32> bindings;
284-
for (auto *param : params->getParams())
285-
bindings.push_back(param);
286-
if (consumer.consume(bindings, DeclVisibilityKind::GenericParameter,
287-
isCascadingUse))
286+
if (lookInGenericParametersOf(params, isCascadingUse, consumer))
288287
return true;
289288
}
290289
return false;
291290
}
292291

292+
bool ASTScopeImpl::lookInGenericParametersOf(
293+
const NullablePtr<const GenericParamList> paramList,
294+
Optional<bool> isCascadingUse, ASTScopeImpl::DeclConsumer consumer) {
295+
if (!paramList)
296+
return false;
297+
SmallVector<ValueDecl *, 32> bindings;
298+
for (auto *param : paramList.get()->getParams())
299+
bindings.push_back(param);
300+
if (consumer.consume(bindings, DeclVisibilityKind::GenericParameter,
301+
isCascadingUse))
302+
return true;
303+
return false;
304+
}
305+
293306
#pragma mark lookupInSelfType
294307

295308
std::pair<bool, Optional<bool>>

0 commit comments

Comments
 (0)