Skip to content

Commit c5b7230

Browse files
committed
[NFC] Upgrade EnumElementDecl to a DeclContext
Pure plumbing for the sake of default arguments.
1 parent 4d128b1 commit c5b7230

18 files changed

+62
-7
lines changed

include/swift/AST/Decl.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5885,7 +5885,7 @@ class EnumCaseDecl final : public Decl,
58855885
/// enum. EnumElementDecls are represented in the AST as members of their
58865886
/// parent EnumDecl, although syntactically they are subordinate to the
58875887
/// EnumCaseDecl.
5888-
class EnumElementDecl : public ValueDecl {
5888+
class EnumElementDecl : public DeclContext, public ValueDecl {
58895889
/// This is the type specified with the enum element, for
58905890
/// example 'Int' in 'case Y(Int)'. This is null if there is no type
58915891
/// associated with this element, as in 'case Z' or in all elements of enum
@@ -5905,7 +5905,8 @@ class EnumElementDecl : public ValueDecl {
59055905
SourceLoc EqualsLoc,
59065906
LiteralExpr *RawValueExpr,
59075907
DeclContext *DC)
5908-
: ValueDecl(DeclKind::EnumElement, DC, Name, IdentifierLoc),
5908+
: DeclContext(DeclContextKind::EnumElementDecl, DC),
5909+
ValueDecl(DeclKind::EnumElement, DC, Name, IdentifierLoc),
59095910
Params(Params),
59105911
EqualsLoc(EqualsLoc),
59115912
RawValueExpr(RawValueExpr)
@@ -5973,14 +5974,23 @@ class EnumElementDecl : public ValueDecl {
59735974
return getParameterList() != nullptr;
59745975
}
59755976

5977+
/// True if the case is marked 'indirect'.
5978+
bool isIndirect() const {
5979+
return getAttrs().hasAttribute<IndirectAttr>();
5980+
}
5981+
59765982
static bool classof(const Decl *D) {
59775983
return D->getKind() == DeclKind::EnumElement;
59785984
}
59795985

5980-
/// True if the case is marked 'indirect'.
5981-
bool isIndirect() const {
5982-
return getAttrs().hasAttribute<IndirectAttr>();
5986+
static bool classof(const DeclContext *DC) {
5987+
if (auto D = DC->getAsDecl())
5988+
return classof(D);
5989+
return false;
59835990
}
5991+
5992+
using DeclContext::operator new;
5993+
using Decl::getASTContext;
59845994
};
59855995

59865996
inline SourceRange EnumCaseDecl::getSourceRange() const {

include/swift/AST/DeclContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ enum class DeclContextKind : unsigned {
7878
Initializer,
7979
TopLevelCodeDecl,
8080
SubscriptDecl,
81+
EnumElementDecl,
8182
AbstractFunctionDecl,
8283
SerializedLocal,
8384
Last_LocalDeclContextKind = SerializedLocal,
@@ -230,6 +231,7 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
230231
case DeclContextKind::TopLevelCodeDecl:
231232
case DeclContextKind::AbstractFunctionDecl:
232233
case DeclContextKind::SubscriptDecl:
234+
case DeclContextKind::EnumElementDecl:
233235
case DeclContextKind::GenericTypeDecl:
234236
case DeclContextKind::ExtensionDecl:
235237
return ASTHierarchy::Decl;

include/swift/AST/DeclNodes.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ ABSTRACT_DECL(Value, Decl)
171171
ABSTRACT_FUNCTION_DECL(Func, AbstractFunctionDecl)
172172
ABSTRACT_FUNCTION_DECL(Accessor, FuncDecl)
173173
DECL_RANGE(AbstractFunction, Constructor, Accessor)
174-
VALUE_DECL(EnumElement, ValueDecl)
174+
CONTEXT_VALUE_DECL(EnumElement, ValueDecl)
175175
DECL_RANGE(Value, Enum, EnumElement)
176176

177177
ITERABLE_GENERIC_DECL(Extension, Decl)

lib/AST/ASTDumper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,10 @@ void swift::printContext(raw_ostream &os, DeclContext *dc) {
13311331
case DeclContextKind::SubscriptDecl:
13321332
printName(os, cast<SubscriptDecl>(dc)->getFullName());
13331333
break;
1334+
1335+
case DeclContextKind::EnumElementDecl:
1336+
printName(os, cast<EnumElementDecl>(dc)->getFullName());
1337+
break;
13341338
}
13351339
}
13361340

lib/AST/ASTMangler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,11 @@ void ASTMangler::appendContext(const DeclContext *ctx) {
15081508
return appendEntity(fn);
15091509
}
15101510

1511+
case DeclContextKind::EnumElementDecl: {
1512+
auto eed = cast<EnumElementDecl>(ctx);
1513+
return appendEntity(eed);
1514+
}
1515+
15111516
case DeclContextKind::SubscriptDecl:
15121517
// FIXME: We may need to do something here if subscripts contain any symbols
15131518
// exposed with linkage names, or if/when they get generic parameters.

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ class Verifier : public ASTWalker {
750750
FUNCTION_LIKE(ConstructorDecl)
751751
FUNCTION_LIKE(DestructorDecl)
752752
FUNCTION_LIKE(FuncDecl)
753+
FUNCTION_LIKE(EnumElementDecl)
753754
SCOPE_LIKE(NominalTypeDecl)
754755
SCOPE_LIKE(ExtensionDecl)
755756

lib/AST/AccessRequests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ AccessLevelRequest::evaluate(Evaluator &evaluator, ValueDecl *D) const {
105105
case DeclContextKind::Initializer:
106106
case DeclContextKind::AbstractFunctionDecl:
107107
case DeclContextKind::SubscriptDecl:
108+
case DeclContextKind::EnumElementDecl:
108109
return AccessLevel::Private;
109110
case DeclContextKind::Module:
110111
case DeclContextKind::FileUnit:

lib/AST/DeclContext.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,9 @@ DeclContext::isCascadingContextForLookup(bool functionsAreNonCascading) const {
405405
case DeclContextKind::SubscriptDecl:
406406
break;
407407

408+
case DeclContextKind::EnumElementDecl:
409+
break;
410+
408411
case DeclContextKind::Module:
409412
case DeclContextKind::FileUnit:
410413
return true;
@@ -461,6 +464,8 @@ bool DeclContext::walkContext(ASTWalker &Walker) {
461464
return cast<AbstractFunctionDecl>(this)->walk(Walker);
462465
case DeclContextKind::SubscriptDecl:
463466
return cast<SubscriptDecl>(this)->walk(Walker);
467+
case DeclContextKind::EnumElementDecl:
468+
return cast<EnumElementDecl>(this)->walk(Walker);
464469
case DeclContextKind::SerializedLocal:
465470
llvm_unreachable("walk is unimplemented for deserialized contexts");
466471
case DeclContextKind::Initializer:
@@ -547,6 +552,7 @@ unsigned DeclContext::printContext(raw_ostream &OS, unsigned indent) const {
547552
Kind = "AbstractFunctionDecl";
548553
break;
549554
case DeclContextKind::SubscriptDecl: Kind = "SubscriptDecl"; break;
555+
case DeclContextKind::EnumElementDecl: Kind = "EnumElementDecl"; break;
550556
}
551557
OS.indent(Depth*2 + indent) << (void*)this << " " << Kind;
552558

@@ -601,6 +607,15 @@ unsigned DeclContext::printContext(raw_ostream &OS, unsigned indent) const {
601607
OS << " : (no type set)";
602608
break;
603609
}
610+
case DeclContextKind::EnumElementDecl: {
611+
auto *EED = cast<EnumElementDecl>(this);
612+
OS << " name=" << EED->getBaseName();
613+
if (EED->hasInterfaceType())
614+
OS << " : " << EED->getInterfaceType();
615+
else
616+
OS << " : (no type set)";
617+
break;
618+
}
604619
case DeclContextKind::Initializer:
605620
switch (cast<Initializer>(this)->getInitializerKind()) {
606621
case InitializerKind::PatternBinding: {
@@ -920,6 +935,8 @@ DeclContextKind DeclContext::getContextKind() const {
920935
return DeclContextKind::TopLevelCodeDecl;
921936
case DeclKind::Subscript:
922937
return DeclContextKind::SubscriptDecl;
938+
case DeclKind::EnumElement:
939+
return DeclContextKind::EnumElementDecl;
923940
case DeclKind::Extension:
924941
return DeclContextKind::ExtensionDecl;
925942
default:

lib/AST/DiagnosticEngine.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@ void DiagnosticEngine::emitDiagnostic(const Diagnostic &diagnostic) {
767767
case DeclContextKind::AbstractClosureExpr:
768768
case DeclContextKind::AbstractFunctionDecl:
769769
case DeclContextKind::SubscriptDecl:
770+
case DeclContextKind::EnumElementDecl:
770771
break;
771772
}
772773

lib/IDE/CodeCompletion.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,7 @@ static bool isTopLevelContext(const DeclContext *DC) {
14371437
return true;
14381438
case DeclContextKind::AbstractFunctionDecl:
14391439
case DeclContextKind::SubscriptDecl:
1440+
case DeclContextKind::EnumElementDecl:
14401441
return false;
14411442
default:
14421443
continue;

0 commit comments

Comments
 (0)