Skip to content

Commit 85ddda1

Browse files
committed
AST: Introduce a getFirstElement() utility on EnumCaseDecl.
This utility helps codify the practice of using the attributes of the first element of a case decl as the attributes for the entire case.
1 parent 8e5988e commit 85ddda1

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

include/swift/AST/Decl.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -7479,7 +7479,16 @@ class EnumCaseDecl final : public Decl,
74797479
Bits.EnumCaseDecl.NumElements};
74807480
}
74817481
SourceRange getSourceRange() const;
7482-
7482+
7483+
/// Returns the first of the member elements or null if there are no elements.
7484+
/// The attributes written with an EnumCaseDecl will be attached to each of
7485+
/// the elements instead so inspecting the attributes of the first element is
7486+
/// often useful.
7487+
EnumElementDecl *getFirstElement() const {
7488+
auto elements = getElements();
7489+
return elements.empty() ? nullptr : elements.front();
7490+
}
7491+
74837492
static bool classof(const Decl *D) {
74847493
return D->getKind() == DeclKind::EnumCase;
74857494
}

lib/AST/ASTPrinter.cpp

+7-10
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,12 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
278278

279279
// Skip enum cases containing enum elements we wouldn't print.
280280
if (auto *ECD = dyn_cast<EnumCaseDecl>(D)) {
281-
auto elements = ECD->getElements();
282-
if (!elements.empty()) {
281+
if (auto *element = ECD->getFirstElement()) {
283282
// Enum elements are usually not printed, so we have to override the
284-
// print option controlling that. We only check the first element
285-
// because all the elements in a single case decl should have the same
286-
// characteristics.
283+
// print option controlling that.
287284
PrintOptions optionsCopy = options;
288285
optionsCopy.ExplodeEnumCaseDecls = true;
289-
if (!shouldPrint(elements[0], optionsCopy))
286+
if (!shouldPrint(element, optionsCopy))
290287
return false;
291288
}
292289
}
@@ -4219,14 +4216,14 @@ void PrintAST::printEnumElement(EnumElementDecl *elt) {
42194216
}
42204217

42214218
void PrintAST::visitEnumCaseDecl(EnumCaseDecl *decl) {
4222-
auto elems = decl->getElements();
4223-
if (!elems.empty()) {
4219+
if (auto *element = decl->getFirstElement()) {
42244220
// Documentation comments over the case are attached to the enum elements.
4225-
printDocumentationComment(elems[0]);
4226-
printAttributes(elems[0]);
4221+
printDocumentationComment(element);
4222+
printAttributes(element);
42274223
}
42284224
Printer.printIntroducerKeyword("case", Options, " ");
42294225

4226+
auto elems = decl->getElements();
42304227
llvm::interleave(elems.begin(), elems.end(),
42314228
[&](EnumElementDecl *elt) {
42324229
printEnumElement(elt);

lib/IDE/SyntaxModel.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1077,8 +1077,8 @@ ASTWalker::PreWalkAction ModelASTWalker::walkToDeclPre(Decl *D) {
10771077

10781078
// We need to handle the special case where attributes semantically
10791079
// attach to enum element decls while syntactically locate before enum case decl.
1080-
if (!EnumCaseD->getElements().empty()) {
1081-
if (!handleAttrs(EnumCaseD->getElements().front()->getAttrs()))
1080+
if (auto *element = EnumCaseD->getFirstElement()) {
1081+
if (!handleAttrs(element->getAttrs()))
10821082
return Action::SkipChildren();
10831083
}
10841084
if (pushStructureNode(SN, D)) {

lib/Sema/TypeCheckAvailability.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1609,9 +1609,8 @@ abstractSyntaxDeclForAvailableAttribute(const Decl *ConcreteSyntaxDecl) {
16091609
} else if (auto *ECD = dyn_cast<EnumCaseDecl>(ConcreteSyntaxDecl)) {
16101610
// Similar to the PatternBindingDecl case above, we return the
16111611
// first EnumElementDecl.
1612-
ArrayRef<EnumElementDecl *> Elems = ECD->getElements();
1613-
if (!Elems.empty()) {
1614-
return Elems.front();
1612+
if (auto *Elem = ECD->getFirstElement()) {
1613+
return Elem;
16151614
}
16161615
}
16171616

0 commit comments

Comments
 (0)