Skip to content

Commit 3a95e9c

Browse files
committed
Fix underlying type of enums
1 parent 3e5508d commit 3a95e9c

File tree

7 files changed

+19
-9
lines changed

7 files changed

+19
-9
lines changed

ir/Enum.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ uint64_t Enumerator::getValue() {
1313
return value;
1414
}
1515

16-
Enum::Enum(std::string name, std::vector<Enumerator> enumerators)
17-
: name(std::move(name)), enumerators(std::move(enumerators)) {}
16+
Enum::Enum(std::string name, std::string type, std::vector<Enumerator> enumerators)
17+
: name(std::move(name)), type(std::move(type)), enumerators(std::move(enumerators)) {}
1818

1919
bool Enum::isAnonymous() const {
2020
return name.empty();
2121
}
2222

2323
TypeDef Enum::generateTypeDef() const {
2424
assert (!isAnonymous());
25-
return TypeDef("enum_" + name, "native.CInt");
25+
return TypeDef("enum_" + name, type);
2626
}
2727

2828
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e) {

ir/Enum.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Enumerator {
2323

2424
class Enum {
2525
public:
26-
Enum(std::string name, std::vector<Enumerator> enumerators);
26+
Enum(std::string name, std::string type, std::vector<Enumerator> enumerators);
2727

2828
bool isAnonymous() const;
2929

@@ -33,6 +33,7 @@ class Enum {
3333

3434
private:
3535
std::string name; // might be empty
36+
std::string type;
3637
std::vector<Enumerator> enumerators;
3738
};
3839

ir/IR.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ void IR::addTypeDef(std::string name, std::string type) {
1717
typeDefs.push_back(TypeDef(std::move(name), std::move(type)));
1818
}
1919

20-
void IR::addEnum(std::string name, std::vector<Enumerator> enumerators) {
21-
enums.push_back(Enum(std::move(name), std::move(enumerators)));
20+
void IR::addEnum(std::string name, std::string type, std::vector<Enumerator> enumerators) {
21+
enums.push_back(Enum(std::move(name), std::move(type), std::move(enumerators)));
2222
}
2323

2424
void IR::addStruct(std::string name, std::vector<Field> fields, uint64_t typeSize) {

ir/IR.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class IR {
1919

2020
void addTypeDef(std::string name, std::string type);
2121

22-
void addEnum(std::string name, std::vector<Enumerator> enumerators);
22+
void addEnum(std::string name, std::string type, std::vector<Enumerator> enumerators);
2323

2424
void addStruct(std::string name, std::vector<Field> fields, uint64_t typeSize);
2525

tests/samples/Enum.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ enum days {
77
SATURDAY = 3,
88
SUNDAY // = 4
99
};
10+
11+
enum bigValues {
12+
A = 10000000000, // does not fit into int
13+
B // 10000000001
14+
};

tests/samples/Enum.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import scala.scalanative.native.Nat._
55
@native.link("Enum")
66
@native.extern
77
object Enum {
8-
type enum_days = native.CInt
8+
type enum_days = native.CUnsignedInt
9+
type enum_bigValues = native.CUnsignedLong
910
}
1011

1112
import Enum._
@@ -18,4 +19,7 @@ object EnumEnums {
1819
final val enum_days_FRIDAY = 5
1920
final val enum_days_SATURDAY = 3
2021
final val enum_days_SUNDAY = 4
22+
23+
final val enum_bigValues_A = 10000000000
24+
final val enum_bigValues_B = 10000000001
2125
}

visitor/TreeVisitor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ bool TreeVisitor::VisitEnumDecl(clang::EnumDecl *enumdecl) {
6767
enumerators.push_back(Enumerator(en->getNameAsString(), value));
6868
}
6969

70-
ir->addEnum(name, enumerators);
70+
ir->addEnum(name, typeTranslator.Translate(enumdecl->getIntegerType()), enumerators);
7171

7272
return true;
7373
}

0 commit comments

Comments
 (0)