Skip to content

Commit 3e5508d

Browse files
authored
Merge pull request #37 from kornilova-l/fix-enums-values
Fix enums values
2 parents 85f055e + bc54c07 commit 3e5508d

File tree

7 files changed

+46
-8
lines changed

7 files changed

+46
-8
lines changed

ir/Enum.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ std::string Enumerator::getName() {
99
return name;
1010
}
1111

12+
uint64_t Enumerator::getValue() {
13+
return value;
14+
}
15+
1216
Enum::Enum(std::string name, std::vector<Enumerator> enumerators)
1317
: name(std::move(name)), enumerators(std::move(enumerators)) {}
1418

@@ -22,15 +26,14 @@ TypeDef Enum::generateTypeDef() const {
2226
}
2327

2428
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e) {
25-
int i = 0;
2629
for (auto enumerator : e.enumerators) {
2730
std::string enumeratorName;
2831
if (!e.name.empty()) {
2932
enumeratorName = "enum_" + e.name + "_" + enumerator.getName();
3033
} else {
3134
enumeratorName = "enum_" + enumerator.getName();
3235
}
33-
s << " final val " << enumeratorName << " = " << std::to_string(i++) << "\n";
36+
s << " final val " << enumeratorName << " = " << std::to_string(enumerator.getValue()) << "\n";
3437
}
3538
return s;
3639
}

ir/Enum.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class Enumerator {
1313

1414
std::string getName();
1515

16+
uint64_t getValue();
17+
1618
private:
1719
std::string name;
1820
uint64_t value;

ir/IR.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,13 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
6868
if (!ir.enums.empty()) {
6969
s << "object " << ir.libName << "Enums {\n";
7070

71-
for (const auto &e : ir.enums) {
72-
s << e
73-
<< "\n"; // space between groups of enums
71+
unsigned long enumeratorsCount = ir.enums.size();
72+
for (unsigned long i = 0; i < enumeratorsCount; i++) {
73+
auto &e = ir.enums[i];
74+
s << e;
75+
if (i < enumeratorsCount - 1) {
76+
s << "\n"; // space between groups of enums
77+
}
7478
}
7579

7680
s << "}\n\n";

tests/samples/Enum.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
enum days {
2+
MONDAY, // = 0
3+
TUESDAY = 200,
4+
WEDNESDAY, // = 201
5+
THURSDAY = 4,
6+
FRIDAY = 5,
7+
SATURDAY = 3,
8+
SUNDAY // = 4
9+
};

tests/samples/Enum.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import scala.scalanative._
2+
import scala.scalanative.native._
3+
import scala.scalanative.native.Nat._
4+
5+
@native.link("Enum")
6+
@native.extern
7+
object Enum {
8+
type enum_days = native.CInt
9+
}
10+
11+
import Enum._
12+
13+
object EnumEnums {
14+
final val enum_days_MONDAY = 0
15+
final val enum_days_TUESDAY = 200
16+
final val enum_days_WEDNESDAY = 201
17+
final val enum_days_THURSDAY = 4
18+
final val enum_days_FRIDAY = 5
19+
final val enum_days_SATURDAY = 3
20+
final val enum_days_SUNDAY = 4
21+
}

tests/samples/Typedef.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,4 @@ object TypedefEnums {
2626

2727
final val enum_toggle_e_OFF = 0
2828
final val enum_toggle_e_ON = 1
29-
3029
}

visitor/TreeVisitor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ bool TreeVisitor::VisitEnumDecl(clang::EnumDecl *enumdecl) {
6262

6363
std::vector<Enumerator> enumerators;
6464

65-
int i = 0;
6665
for (const clang::EnumConstantDecl *en : enumdecl->enumerators()) {
67-
enumerators.push_back(Enumerator(en->getNameAsString(), static_cast<uint64_t>(i++)));
66+
uint64_t value = en->getInitVal().getLimitedValue();
67+
enumerators.push_back(Enumerator(en->getNameAsString(), value));
6868
}
6969

7070
ir->addEnum(name, enumerators);

0 commit comments

Comments
 (0)