Skip to content

Commit 23d60ac

Browse files
committed
sort cases based on their assigned tag indices before printing
1 parent e7ea37f commit 23d60ac

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

include/swift/IRGen/IRABIDetailsProvider.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/Decl.h"
1717
#include "swift/AST/Type.h"
1818
#include "clang/AST/CharUnits.h"
19+
#include "llvm/ADT/MapVector.h"
1920
#include "llvm/ADT/Optional.h"
2021
#include "llvm/ADT/STLExtras.h"
2122
#include "llvm/ADT/SmallVector.h"
@@ -96,7 +97,7 @@ class IRABIDetailsProvider {
9697

9798
/// Returns EnumElementDecls (enum cases) in their declaration order with
9899
/// their tag indices from the given EnumDecl
99-
std::map<EnumElementDecl *, unsigned> getEnumTagMapping(EnumDecl *ED);
100+
llvm::MapVector<EnumElementDecl *, unsigned> getEnumTagMapping(EnumDecl *ED);
100101

101102
private:
102103
std::unique_ptr<IRABIDetailsProviderImpl> impl;

lib/IRGen/IRABIDetailsProvider.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ class IRABIDetailsProviderImpl {
123123
return {returnTy, {paramTy}};
124124
}
125125

126-
std::map<EnumElementDecl *, unsigned> getEnumTagMapping(EnumDecl *ED) {
127-
std::map<EnumElementDecl *, unsigned> elements;
126+
llvm::MapVector<EnumElementDecl *, unsigned> getEnumTagMapping(EnumDecl *ED) {
127+
llvm::MapVector<EnumElementDecl *, unsigned> elements;
128128
auto &enumImplStrat = getEnumImplStrategy(
129129
IGM, ED->getDeclaredType()->getCanonicalType());
130130

@@ -177,7 +177,7 @@ IRABIDetailsProvider::getTypeMetadataAccessFunctionSignature() {
177177
return impl->getTypeMetadataAccessFunctionSignature();
178178
}
179179

180-
std::map<EnumElementDecl *, unsigned>
180+
llvm::MapVector<EnumElementDecl *, unsigned>
181181
IRABIDetailsProvider::getEnumTagMapping(EnumDecl *ED) {
182182
return impl->getEnumTagMapping(ED);
183183
}

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,10 @@ class DeclAndTypePrinter::Implementation
395395
ClangSyntaxPrinter syntaxPrinter(os);
396396
auto elementTagMapping =
397397
owningPrinter.interopContext.getIrABIDetails().getEnumTagMapping(ED);
398+
// Sort cases based on their assigned tag indices
399+
llvm::stable_sort(elementTagMapping, [](const auto &p1, const auto &p2) {
400+
return p1.second < p2.second;
401+
});
398402

399403
if (elementTagMapping.empty()) {
400404
os << "\n";
@@ -417,6 +421,7 @@ class DeclAndTypePrinter::Implementation
417421
syntaxPrinter.printIdentifier(pair.first->getNameStr());
418422
os << ";\n";
419423
}
424+
// TODO: change to Swift's fatalError when it's available in C++
420425
os << " default: abort();\n";
421426
os << " }\n"; // switch's closing bracket
422427
os << " }\n"; // operator cases()'s closing bracket

test/Interop/SwiftToCxx/enums/swift-enum-case-functions.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,18 +193,18 @@ public func checkIntDoubleOrBignum(_ x: IntDoubleOrBignum, tag: Int) -> Bool {
193193

194194
// CHECK: inline operator cases() const {
195195
// CHECK-NEXT: switch (_getEnumTag()) {
196-
// CHECK-NEXT: case 1: return cases::Paragraph;
197196
// CHECK-NEXT: case 0: return cases::Char;
197+
// CHECK-NEXT: case 1: return cases::Paragraph;
198198
// CHECK-NEXT: case 2: return cases::Chapter;
199199
// CHECK-NEXT: default: abort();
200200
// CHECK-NEXT: }
201201
// CHECK-NEXT: }
202-
// CHECK-NEXT: inline bool isParagraph() const {
203-
// CHECK-NEXT: return *this == cases::Paragraph;
204-
// CHECK-NEXT: }
205202
// CHECK-NEXT: inline bool isChar() const {
206203
// CHECK-NEXT: return *this == cases::Char;
207204
// CHECK-NEXT: }
205+
// CHECK-NEXT: inline bool isParagraph() const {
206+
// CHECK-NEXT: return *this == cases::Paragraph;
207+
// CHECK-NEXT: }
208208
// CHECK-NEXT: inline bool isChapter() const {
209209
// CHECK-NEXT: return *this == cases::Chapter;
210210
// CHECK-NEXT: }
@@ -263,18 +263,18 @@ public func checkIntDoubleOrBignum(_ x: IntDoubleOrBignum, tag: Int) -> Bool {
263263

264264
// CHECK: inline operator cases() const {
265265
// CHECK-NEXT: switch (_getEnumTag()) {
266-
// CHECK-NEXT: case 1: return cases::NegInfinity;
267266
// CHECK-NEXT: case 0: return cases::Int;
267+
// CHECK-NEXT: case 1: return cases::NegInfinity;
268268
// CHECK-NEXT: case 2: return cases::PosInfinity;
269269
// CHECK-NEXT: default: abort();
270270
// CHECK-NEXT: }
271271
// CHECK-NEXT: }
272-
// CHECK-NEXT: inline bool isNegInfinity() const {
273-
// CHECK-NEXT: return *this == cases::NegInfinity;
274-
// CHECK-NEXT: }
275272
// CHECK-NEXT: inline bool isInt() const {
276273
// CHECK-NEXT: return *this == cases::Int;
277274
// CHECK-NEXT: }
275+
// CHECK-NEXT: inline bool isNegInfinity() const {
276+
// CHECK-NEXT: return *this == cases::NegInfinity;
277+
// CHECK-NEXT: }
278278
// CHECK-NEXT: inline bool isPosInfinity() const {
279279
// CHECK-NEXT: return *this == cases::PosInfinity;
280280
// CHECK-NEXT: }
@@ -289,18 +289,15 @@ public func checkIntDoubleOrBignum(_ x: IntDoubleOrBignum, tag: Int) -> Bool {
289289

290290
// CHECK: inline operator cases() const {
291291
// CHECK-NEXT: switch (_getEnumTag()) {
292-
// CHECK-NEXT: case 4: return cases::Cursor;
293292
// CHECK-NEXT: case 0: return cases::Plain;
294293
// CHECK-NEXT: case 1: return cases::Bold;
295294
// CHECK-NEXT: case 2: return cases::Underline;
296295
// CHECK-NEXT: case 3: return cases::Blink;
296+
// CHECK-NEXT: case 4: return cases::Cursor;
297297
// CHECK-NEXT: case 5: return cases::Empty;
298298
// CHECK-NEXT: default: abort();
299299
// CHECK-NEXT: }
300300
// CHECK-NEXT: }
301-
// CHECK-NEXT: inline bool isCursor() const {
302-
// CHECK-NEXT: return *this == cases::Cursor;
303-
// CHECK-NEXT: }
304301
// CHECK-NEXT: inline bool isPlain() const {
305302
// CHECK-NEXT: return *this == cases::Plain;
306303
// CHECK-NEXT: }
@@ -313,6 +310,9 @@ public func checkIntDoubleOrBignum(_ x: IntDoubleOrBignum, tag: Int) -> Bool {
313310
// CHECK-NEXT: inline bool isBlink() const {
314311
// CHECK-NEXT: return *this == cases::Blink;
315312
// CHECK-NEXT: }
313+
// CHECK-NEXT: inline bool isCursor() const {
314+
// CHECK-NEXT: return *this == cases::Cursor;
315+
// CHECK-NEXT: }
316316
// CHECK-NEXT: inline bool isEmpty() const {
317317
// CHECK-NEXT: return *this == cases::Empty;
318318
// CHECK-NEXT: }

test/Interop/SwiftToCxx/enums/swift-enum-cases-in-cxx.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public enum EnumCaseIsCxxKeyword {
2323

2424
// CHECK: class EnumCaseIsCxxKeyword final {
2525
// CHECK: enum class cases {
26-
// CHECK-NEXT: first,
2726
// CHECK-NEXT: second,
27+
// CHECK-NEXT: first,
2828
// CHECK-NEXT: const_
2929
// CHECK-NEXT: };
3030

0 commit comments

Comments
 (0)