Skip to content

Commit 32fc28b

Browse files
committed
Support negative enumerators
1 parent 728c49b commit 32fc28b

File tree

5 files changed

+38
-20
lines changed

5 files changed

+38
-20
lines changed

ir/Enum.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#include "Enum.h"
22
#include <sstream>
33

4-
Enumerator::Enumerator(std::string name, uint64_t value)
4+
Enumerator::Enumerator(std::string name, int64_t value)
55
: name(std::move(name)), value(value) {}
66

77
std::string Enumerator::getName() {
88
return name;
99
}
1010

11-
uint64_t Enumerator::getValue() {
11+
int64_t Enumerator::getValue() {
1212
return value;
1313
}
1414

@@ -42,14 +42,14 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e) {
4242
s << ": " << type
4343
<< " = " << std::to_string(enumerator.getValue());
4444

45-
if (e.type == "native.CUnsignedInt") {
46-
s << ".toUInt" << "\n";
45+
if (e.type == "native.CLong") {
46+
s << "L";
47+
} else if (e.type == "native.CUnsignedInt") {
48+
s << ".toUInt";
4749
} else if (e.type == "native.CUnsignedLong") {
48-
s << "L.toULong" << "\n";
49-
} else {
50-
llvm::errs() << "Enum type is unsupported: " << e.type << "\n";
51-
llvm::errs().flush();
50+
s << "L.toULong";
5251
}
52+
s << "\n";
5353
}
5454
return s;
5555
}

ir/Enum.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
class Enumerator {
1111
public:
12-
Enumerator(std::string name, uint64_t value);
12+
Enumerator(std::string name, int64_t value);
1313

1414
std::string getName();
1515

16-
uint64_t getValue();
16+
int64_t getValue();
1717

1818
private:
1919
std::string name;
20-
uint64_t value;
20+
int64_t value;
2121
};
2222

2323

tests/samples/Enum.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,20 @@ enum days {
99
};
1010

1111
enum bigValues {
12-
A = 10000000000, // does not fit into int
13-
B = 1
12+
BIG_A = 10000000000, // does not fit into int
13+
BIG_B = 1
1414
};
1515

1616
enum { // anonymous enum
17-
C, D
17+
ANON_A, ANON_B
18+
};
19+
20+
enum negativeValues {
21+
NEG_A = -1,
22+
NEG_B = -2
23+
};
24+
25+
enum bigNegativeValues {
26+
BIG_NEG_A = -10000000000,
27+
BIG_NEG_B = -1
1828
};

tests/samples/Enum.scala

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import scala.scalanative.native.Nat._
77
object Enum {
88
type enum_days = native.CUnsignedInt
99
type enum_bigValues = native.CUnsignedLong
10+
type enum_negativeValues = native.CInt
11+
type enum_bigNegativeValues = native.CLong
1012
}
1113

1214
import Enum._
@@ -20,9 +22,15 @@ object EnumEnums {
2022
final val enum_days_SATURDAY: enum_days = 3.toUInt
2123
final val enum_days_SUNDAY: enum_days = 4.toUInt
2224

23-
final val enum_bigValues_A: enum_bigValues = 10000000000L.toULong
24-
final val enum_bigValues_B: enum_bigValues = 1L.toULong
25+
final val enum_bigValues_BIG_A: enum_bigValues = 10000000000L.toULong
26+
final val enum_bigValues_BIG_B: enum_bigValues = 1L.toULong
2527

26-
final val enum_C: native.CUnsignedInt = 0.toUInt
27-
final val enum_D: native.CUnsignedInt = 1.toUInt
28+
final val enum_ANON_A: native.CUnsignedInt = 0.toUInt
29+
final val enum_ANON_B: native.CUnsignedInt = 1.toUInt
30+
31+
final val enum_negativeValues_NEG_A: enum_negativeValues = -1
32+
final val enum_negativeValues_NEG_B: enum_negativeValues = -2
33+
34+
final val enum_bigNegativeValues_BIG_NEG_A: enum_bigNegativeValues = -10000000000L
35+
final val enum_bigNegativeValues_BIG_NEG_B: enum_bigNegativeValues = -1L
2836
}

visitor/TreeVisitor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ bool TreeVisitor::VisitEnumDecl(clang::EnumDecl *enumdecl) {
6363
std::vector<Enumerator> enumerators;
6464

6565
for (const clang::EnumConstantDecl *en : enumdecl->enumerators()) {
66-
uint64_t value = en->getInitVal().getLimitedValue();
67-
enumerators.push_back(Enumerator(en->getNameAsString(), value));
66+
int64_t value = en->getInitVal().getSExtValue();
67+
enumerators.emplace_back(en->getNameAsString(), value);
6868
}
6969

7070
ir->addEnum(name, typeTranslator.Translate(enumdecl->getIntegerType()), enumerators);

0 commit comments

Comments
 (0)