Skip to content

Fix enums values #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions ir/Enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ std::string Enumerator::getName() {
return name;
}

uint64_t Enumerator::getValue() {
return value;
}

Enum::Enum(std::string name, std::vector<Enumerator> enumerators)
: name(std::move(name)), enumerators(std::move(enumerators)) {}

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

llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e) {
int i = 0;
for (auto enumerator : e.enumerators) {
std::string enumeratorName;
if (!e.name.empty()) {
enumeratorName = "enum_" + e.name + "_" + enumerator.getName();
} else {
enumeratorName = "enum_" + enumerator.getName();
}
s << " final val " << enumeratorName << " = " << std::to_string(i++) << "\n";
s << " final val " << enumeratorName << " = " << std::to_string(enumerator.getValue()) << "\n";
}
return s;
}
2 changes: 2 additions & 0 deletions ir/Enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Enumerator {

std::string getName();

uint64_t getValue();

private:
std::string name;
uint64_t value;
Expand Down
10 changes: 7 additions & 3 deletions ir/IR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
if (!ir.enums.empty()) {
s << "object " << ir.libName << "Enums {\n";

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

s << "}\n\n";
Expand Down
9 changes: 9 additions & 0 deletions tests/samples/Enum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
enum days {
MONDAY, // = 0
TUESDAY = 200,
WEDNESDAY, // = 201
THURSDAY = 4,
FRIDAY = 5,
SATURDAY = 3,
SUNDAY // = 4
};
21 changes: 21 additions & 0 deletions tests/samples/Enum.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import scala.scalanative._
import scala.scalanative.native._
import scala.scalanative.native.Nat._

@native.link("Enum")
@native.extern
object Enum {
type enum_days = native.CInt
}

import Enum._

object EnumEnums {
final val enum_days_MONDAY = 0
final val enum_days_TUESDAY = 200
final val enum_days_WEDNESDAY = 201
final val enum_days_THURSDAY = 4
final val enum_days_FRIDAY = 5
final val enum_days_SATURDAY = 3
final val enum_days_SUNDAY = 4
}
1 change: 0 additions & 1 deletion tests/samples/Typedef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ object TypedefEnums {

final val enum_toggle_e_OFF = 0
final val enum_toggle_e_ON = 1

}
4 changes: 2 additions & 2 deletions visitor/TreeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ bool TreeVisitor::VisitEnumDecl(clang::EnumDecl *enumdecl) {

std::vector<Enumerator> enumerators;

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

ir->addEnum(name, enumerators);
Expand Down