Skip to content

Commit d7a406d

Browse files
authored
Merge pull request #20950 from CodaFi/christmas-declarations
[NFC] Upgrade EnumElementDecl to a DeclContext
2 parents ae0f04a + c5b7230 commit d7a406d

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;

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void typeCheckContextImpl(DeclContext *DC, SourceLoc Loc) {
4949
case DeclContextKind::Module:
5050
case DeclContextKind::SerializedLocal:
5151
case DeclContextKind::TopLevelCodeDecl:
52+
case DeclContextKind::EnumElementDecl:
5253
// Nothing to do for these.
5354
break;
5455

lib/IDE/Refactoring.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ ExtractCheckResult checkExtractConditions(ResolvedRangeInfo &RangeInfo,
935935
switch (RangeInfo.RangeContext->getContextKind()) {
936936
case swift::DeclContextKind::Initializer:
937937
case swift::DeclContextKind::SubscriptDecl:
938+
case swift::DeclContextKind::EnumElementDecl:
938939
case swift::DeclContextKind::AbstractFunctionDecl:
939940
case swift::DeclContextKind::AbstractClosureExpr:
940941
case swift::DeclContextKind::TopLevelCodeDecl:

lib/IRGen/GenDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,7 @@ IRGenModule::getAddrOfParentContextDescriptor(DeclContext *from,
771771
case DeclContextKind::AbstractClosureExpr:
772772
case DeclContextKind::AbstractFunctionDecl:
773773
case DeclContextKind::SubscriptDecl:
774+
case DeclContextKind::EnumElementDecl:
774775
case DeclContextKind::TopLevelCodeDecl:
775776
case DeclContextKind::Initializer:
776777
case DeclContextKind::SerializedLocal:

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
465465
case DeclContextKind::Initializer:
466466
case DeclContextKind::ExtensionDecl:
467467
case DeclContextKind::SubscriptDecl:
468+
case DeclContextKind::EnumElementDecl:
468469
case DeclContextKind::TopLevelCodeDecl:
469470
return getOrCreateContext(DC->getParent());
470471

lib/SIL/SILPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ static void printFullContext(const DeclContext *Context, raw_ostream &Buffer) {
212212
// FIXME
213213
Buffer << "<subscript>";
214214
return;
215+
case DeclContextKind::EnumElementDecl:
216+
// FIXME
217+
Buffer << "<enum element>";
218+
return;
215219
}
216220
llvm_unreachable("bad decl context");
217221
}

lib/Sema/LookupVisibleDecls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,9 @@ static void doDynamicLookup(VisibleDeclConsumer &Consumer,
294294

295295
// These cases are probably impossible here but can also just
296296
// be safely ignored.
297-
case DeclKind::EnumElement:
298297
case DeclKind::Param:
299298
case DeclKind::Module:
299+
case DeclKind::EnumElement:
300300
return;
301301

302302
// For other kinds of values, check if we already reported a decl

lib/Serialization/Serialization.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ static const Decl *getDeclForContext(const DeclContext *DC) {
475475
return cast<AbstractFunctionDecl>(DC);
476476
case DeclContextKind::SubscriptDecl:
477477
return cast<SubscriptDecl>(DC);
478+
case DeclContextKind::EnumElementDecl:
479+
return cast<EnumElementDecl>(DC);
478480
}
479481

480482
llvm_unreachable("Unhandled DeclContextKind in switch.");
@@ -1870,6 +1872,7 @@ void Serializer::writeCrossReference(const DeclContext *DC, uint32_t pathLen) {
18701872
case DeclContextKind::Initializer:
18711873
case DeclContextKind::TopLevelCodeDecl:
18721874
case DeclContextKind::SerializedLocal:
1875+
case DeclContextKind::EnumElementDecl:
18731876
llvm_unreachable("cannot cross-reference this context");
18741877

18751878
case DeclContextKind::FileUnit:
@@ -2405,6 +2408,7 @@ void Serializer::writeDeclContext(const DeclContext *DC) {
24052408
case DeclContextKind::SubscriptDecl:
24062409
case DeclContextKind::GenericTypeDecl:
24072410
case DeclContextKind::ExtensionDecl:
2411+
case DeclContextKind::EnumElementDecl:
24082412
declOrDeclContextID = addDeclRef(getDeclForContext(DC));
24092413
isDecl = true;
24102414
break;

tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ static bool initDocEntityInfo(const Decl *D,
413413
case DeclContextKind::TopLevelCodeDecl:
414414
case DeclContextKind::AbstractFunctionDecl:
415415
case DeclContextKind::SubscriptDecl:
416+
case DeclContextKind::EnumElementDecl:
416417
case DeclContextKind::Initializer:
417418
case DeclContextKind::SerializedLocal:
418419
case DeclContextKind::ExtensionDecl:

0 commit comments

Comments
 (0)