Skip to content

Commit feace85

Browse files
committed
Enhance SubscriptDecl to be a DeclContext, so it can hold its indices.
This is necessary for some other work I'm doing, which really wants paramdecls to have reasonable declcontexts. It is also a small step towards generic subscripts.
1 parent 6dcb6ef commit feace85

21 files changed

+104
-26
lines changed

include/swift/AST/Decl.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4239,7 +4239,7 @@ enum class ObjCSubscriptKind {
42394239
/// A given type can have multiple subscript declarations, so long as the
42404240
/// signatures (indices and element type) are distinct.
42414241
///
4242-
class SubscriptDecl : public AbstractStorageDecl {
4242+
class SubscriptDecl : public AbstractStorageDecl, public DeclContext {
42434243
SourceLoc ArrowLoc;
42444244
Pattern *Indices;
42454245
TypeLoc ElementTy;
@@ -4248,6 +4248,7 @@ class SubscriptDecl : public AbstractStorageDecl {
42484248
SubscriptDecl(DeclName Name, SourceLoc SubscriptLoc, Pattern *Indices,
42494249
SourceLoc ArrowLoc, TypeLoc ElementTy, DeclContext *Parent)
42504250
: AbstractStorageDecl(DeclKind::Subscript, Parent, Name, SubscriptLoc),
4251+
DeclContext(DeclContextKind::SubscriptDecl, Parent),
42514252
ArrowLoc(ArrowLoc), Indices(nullptr), ElementTy(ElementTy) {
42524253
setIndices(Indices);
42534254
}
@@ -4288,6 +4289,13 @@ class SubscriptDecl : public AbstractStorageDecl {
42884289
static bool classof(const Decl *D) {
42894290
return D->getKind() == DeclKind::Subscript;
42904291
}
4292+
4293+
static bool classof(const DeclContext *DC) {
4294+
return DC->getContextKind() == DeclContextKind::SubscriptDecl;
4295+
}
4296+
4297+
using DeclContext::operator new;
4298+
using Decl::getASTContext;
42914299
};
42924300

42934301
/// \brief Base class for function-like declarations.

include/swift/AST/DeclContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ enum class DeclContextKind : uint8_t {
6666
AbstractClosureExpr,
6767
Initializer,
6868
TopLevelCodeDecl,
69+
SubscriptDecl,
6970
AbstractFunctionDecl,
7071
SerializedLocal,
7172
Last_LocalDeclContextKind = SerializedLocal,

lib/AST/ASTDumper.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,9 @@ static void printContext(raw_ostream &os, DeclContext *dc) {
986986
os << "deinit";
987987
break;
988988
}
989+
case DeclContextKind::SubscriptDecl:
990+
os << "subscript decl";
991+
break;
989992
}
990993
}
991994

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,6 +2388,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
23882388
case DeclContextKind::AbstractFunctionDecl:
23892389
visit(cast<AbstractFunctionDecl>(DC)->getType());
23902390
return;
2391+
2392+
case DeclContextKind::SubscriptDecl:
2393+
visit(cast<SubscriptDecl>(DC)->getType());
2394+
return;
23912395
}
23922396
}
23932397

lib/AST/Decl.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3432,10 +3432,18 @@ static bool isIntegralType(Type type) {
34323432
return false;
34333433
}
34343434

3435+
/// Set the DeclContext of any VarDecls in P to the specified DeclContext.
3436+
static void setDeclContextOfPatternVars(Pattern *P, DeclContext *DC) {
3437+
if (!P) return;
3438+
P->forEachVariable([&](VarDecl *VD) {
3439+
assert(isa<ParamDecl>(VD) && "Pattern variable is not a parameter?");
3440+
VD->setDeclContext(DC);
3441+
});
3442+
}
3443+
34353444
void SubscriptDecl::setIndices(Pattern *p) {
34363445
Indices = p;
3437-
3438-
// FIXME: What context should the indices patterns be in?
3446+
setDeclContextOfPatternVars(Indices, this);
34393447
}
34403448

34413449
Type SubscriptDecl::getIndicesType() const {
@@ -3881,15 +3889,6 @@ AbstractFunctionDecl *AbstractFunctionDecl::getOverriddenDecl() const {
38813889
return nullptr;
38823890
}
38833891

3884-
/// Set the DeclContext of any VarDecls in P to the specified DeclContext.
3885-
static void setDeclContextOfPatternVars(Pattern *P, DeclContext *DC) {
3886-
if (!P) return;
3887-
P->forEachVariable([&](VarDecl *VD) {
3888-
assert(isa<ParamDecl>(VD) && "Pattern variable is not a parameter?");
3889-
VD->setDeclContext(DC);
3890-
});
3891-
}
3892-
38933892
FuncDecl *FuncDecl::createImpl(ASTContext &Context,
38943893
SourceLoc StaticLoc,
38953894
StaticSpellingKind StaticSpelling,

lib/AST/DeclContext.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ DeclContext::isNominalTypeOrNominalTypeExtensionContext() const {
4646
case DeclContextKind::AbstractClosureExpr:
4747
case DeclContextKind::TopLevelCodeDecl:
4848
case DeclContextKind::AbstractFunctionDecl:
49+
case DeclContextKind::SubscriptDecl:
4950
case DeclContextKind::Initializer:
5051
case DeclContextKind::SerializedLocal:
5152
return nullptr;
@@ -107,6 +108,7 @@ Type DeclContext::getDeclaredTypeOfContext() const {
107108
case DeclContextKind::AbstractClosureExpr:
108109
case DeclContextKind::TopLevelCodeDecl:
109110
case DeclContextKind::AbstractFunctionDecl:
111+
case DeclContextKind::SubscriptDecl:
110112
case DeclContextKind::Initializer:
111113
case DeclContextKind::SerializedLocal:
112114
return Type();
@@ -144,6 +146,7 @@ Type DeclContext::getDeclaredTypeInContext() const {
144146
case DeclContextKind::AbstractClosureExpr:
145147
case DeclContextKind::TopLevelCodeDecl:
146148
case DeclContextKind::AbstractFunctionDecl:
149+
case DeclContextKind::SubscriptDecl:
147150
case DeclContextKind::Initializer:
148151
case DeclContextKind::SerializedLocal:
149152
return Type();
@@ -178,6 +181,7 @@ Type DeclContext::getDeclaredInterfaceType() const {
178181
case DeclContextKind::AbstractClosureExpr:
179182
case DeclContextKind::TopLevelCodeDecl:
180183
case DeclContextKind::AbstractFunctionDecl:
184+
case DeclContextKind::SubscriptDecl:
181185
case DeclContextKind::Initializer:
182186
case DeclContextKind::SerializedLocal:
183187
return Type();
@@ -205,6 +209,7 @@ GenericParamList *DeclContext::getGenericParamsOfContext() const {
205209
case DeclContextKind::SerializedLocal:
206210
case DeclContextKind::Initializer:
207211
case DeclContextKind::AbstractClosureExpr:
212+
case DeclContextKind::SubscriptDecl:
208213
// Closures and initializers can't themselves be generic, but they
209214
// can occur in generic contexts.
210215
continue;
@@ -245,6 +250,7 @@ GenericSignature *DeclContext::getGenericSignatureOfContext() const {
245250
case DeclContextKind::Initializer:
246251
case DeclContextKind::SerializedLocal:
247252
case DeclContextKind::AbstractClosureExpr:
253+
case DeclContextKind::SubscriptDecl:
248254
// Closures and initializers can't themselves be generic, but they
249255
// can occur in generic contexts.
250256
continue;
@@ -309,6 +315,7 @@ AbstractFunctionDecl *DeclContext::getInnermostMethodContext() {
309315
case DeclContextKind::Module:
310316
case DeclContextKind::NominalTypeDecl:
311317
case DeclContextKind::TopLevelCodeDecl:
318+
case DeclContextKind::SubscriptDecl:
312319
// Not in a method context.
313320
return nullptr;
314321
}
@@ -323,6 +330,7 @@ DeclContext *DeclContext::getInnermostTypeContext() {
323330
case DeclContextKind::Initializer:
324331
case DeclContextKind::TopLevelCodeDecl:
325332
case DeclContextKind::AbstractFunctionDecl:
333+
case DeclContextKind::SubscriptDecl:
326334
case DeclContextKind::SerializedLocal:
327335
Result = Result->getParent();
328336
continue;
@@ -355,6 +363,9 @@ Decl *DeclContext::getInnermostDeclarationDeclContext() {
355363
case DeclContextKind::AbstractFunctionDecl:
356364
return cast<AbstractFunctionDecl>(DC);
357365

366+
case DeclContextKind::SubscriptDecl:
367+
return cast<SubscriptDecl>(DC);
368+
358369
case DeclContextKind::NominalTypeDecl:
359370
return cast<NominalTypeDecl>(DC);
360371

@@ -408,6 +419,7 @@ bool DeclContext::isGenericContext() const {
408419
case DeclContextKind::Initializer:
409420
case DeclContextKind::AbstractClosureExpr:
410421
case DeclContextKind::SerializedLocal:
422+
case DeclContextKind::SubscriptDecl:
411423
// Check parent context.
412424
continue;
413425

@@ -506,6 +518,9 @@ DeclContext::isCascadingContextForLookup(bool functionsAreNonCascading) const {
506518
break;
507519
}
508520

521+
case DeclContextKind::SubscriptDecl:
522+
return false;
523+
509524
case DeclContextKind::Module:
510525
case DeclContextKind::FileUnit:
511526
return true;
@@ -553,6 +568,8 @@ bool DeclContext::walkContext(ASTWalker &Walker) {
553568
return cast<TopLevelCodeDecl>(this)->walk(Walker);
554569
case DeclContextKind::AbstractFunctionDecl:
555570
return cast<AbstractFunctionDecl>(this)->walk(Walker);
571+
case DeclContextKind::SubscriptDecl:
572+
return cast<SubscriptDecl>(this)->walk(Walker);
556573
case DeclContextKind::SerializedLocal:
557574
llvm_unreachable("walk is unimplemented for deserialized contexts");
558575
case DeclContextKind::Initializer:
@@ -626,6 +643,7 @@ unsigned DeclContext::printContext(raw_ostream &OS, unsigned indent) const {
626643
case DeclContextKind::AbstractFunctionDecl:
627644
Kind = "AbstractFunctionDecl";
628645
break;
646+
case DeclContextKind::SubscriptDecl: Kind = "SubscriptDecl"; break;
629647
}
630648
OS.indent(Depth*2 + indent) << "0x" << (void*)this << " " << Kind;
631649

@@ -673,6 +691,15 @@ unsigned DeclContext::printContext(raw_ostream &OS, unsigned indent) const {
673691
OS << " : (no type set)";
674692
break;
675693
}
694+
case DeclContextKind::SubscriptDecl: {
695+
auto *SD = cast<SubscriptDecl>(this);
696+
OS << " name=" << SD->getName();
697+
if (SD->hasType())
698+
OS << " : " << SD->getType();
699+
else
700+
OS << " : (no type set)";
701+
break;
702+
}
676703
case DeclContextKind::Initializer:
677704
switch (cast<Initializer>(this)->getInitializerKind()) {
678705
case InitializerKind::PatternBinding: {

lib/AST/DiagnosticEngine.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ void DiagnosticEngine::emitDiagnostic(const Diagnostic &diagnostic) {
558558
case DeclContextKind::Initializer:
559559
case DeclContextKind::AbstractClosureExpr:
560560
case DeclContextKind::AbstractFunctionDecl:
561+
case DeclContextKind::SubscriptDecl:
561562
break;
562563
}
563564

lib/AST/Mangle.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ void Mangler::mangleContext(const DeclContext *ctx, BindGenerics shouldBind) {
299299
return mangleEntity(fn, ResilienceExpansion::Minimal, /*uncurry*/ 0);
300300
}
301301

302+
case DeclContextKind::SubscriptDecl:
303+
return mangleContext(ctx->getParent(), shouldBind);
304+
302305
case DeclContextKind::Initializer:
303306
switch (cast<Initializer>(ctx)->getInitializerKind()) {
304307
case InitializerKind::DefaultArgument: {

lib/AST/Module.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ void Module::lookupMember(SmallVectorImpl<ValueDecl*> &results,
458458
case DeclContextKind::Initializer:
459459
case DeclContextKind::TopLevelCodeDecl:
460460
case DeclContextKind::AbstractFunctionDecl:
461+
case DeclContextKind::SubscriptDecl:
461462
llvm_unreachable("This context does not support lookup.");
462463

463464
case DeclContextKind::FileUnit:

lib/AST/Verifier.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ struct ASTNodeBase {};
516516
case DeclContextKind::Initializer:
517517
case DeclContextKind::AbstractClosureExpr:
518518
case DeclContextKind::SerializedLocal:
519+
case DeclContextKind::SubscriptDecl:
519520
return nullptr;
520521

521522
case DeclContextKind::AbstractFunctionDecl:
@@ -1601,6 +1602,7 @@ struct ASTNodeBase {};
16011602
case DeclContextKind::Initializer:
16021603
case DeclContextKind::NominalTypeDecl:
16031604
case DeclContextKind::ExtensionDecl:
1605+
case DeclContextKind::SubscriptDecl:
16041606
return hasEnclosingFunctionContext(dc->getParent());
16051607
}
16061608
}
@@ -1616,7 +1618,7 @@ struct ASTNodeBase {};
16161618
// Make sure that there are no archetypes in the interface type.
16171619
if (VD->getDeclContext()->isTypeContext() &&
16181620
!hasEnclosingFunctionContext(VD->getDeclContext()) &&
1619-
!isa<ParamDecl>(VD) && /* because of subscripts */
1621+
// !isa<ParamDecl>(VD) && /* because of subscripts */
16201622
VD->getInterfaceType().findIf([](Type type) {
16211623
return type->is<ArchetypeType>();
16221624
})) {

0 commit comments

Comments
 (0)