From bc54c072ea8983b0eefe6ed5c76967cf3ba4ded6 Mon Sep 17 00:00:00 2001 From: kornilova-l Date: Wed, 6 Jun 2018 14:55:10 +0300 Subject: [PATCH] Fix enums values --- ir/Enum.cpp | 7 +++++-- ir/Enum.h | 2 ++ ir/IR.cpp | 10 +++++++--- tests/samples/Enum.h | 9 +++++++++ tests/samples/Enum.scala | 21 +++++++++++++++++++++ tests/samples/Typedef.scala | 1 - visitor/TreeVisitor.cpp | 4 ++-- 7 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 tests/samples/Enum.h create mode 100644 tests/samples/Enum.scala diff --git a/ir/Enum.cpp b/ir/Enum.cpp index c257eca..096b230 100644 --- a/ir/Enum.cpp +++ b/ir/Enum.cpp @@ -9,6 +9,10 @@ std::string Enumerator::getName() { return name; } +uint64_t Enumerator::getValue() { + return value; +} + Enum::Enum(std::string name, std::vector enumerators) : name(std::move(name)), enumerators(std::move(enumerators)) {} @@ -22,7 +26,6 @@ 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()) { @@ -30,7 +33,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e) { } 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; } diff --git a/ir/Enum.h b/ir/Enum.h index 5e240c0..2fd4b91 100644 --- a/ir/Enum.h +++ b/ir/Enum.h @@ -13,6 +13,8 @@ class Enumerator { std::string getName(); + uint64_t getValue(); + private: std::string name; uint64_t value; diff --git a/ir/IR.cpp b/ir/IR.cpp index 29ce66f..0256731 100644 --- a/ir/IR.cpp +++ b/ir/IR.cpp @@ -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"; diff --git a/tests/samples/Enum.h b/tests/samples/Enum.h new file mode 100644 index 0000000..a162e8f --- /dev/null +++ b/tests/samples/Enum.h @@ -0,0 +1,9 @@ +enum days { + MONDAY, // = 0 + TUESDAY = 200, + WEDNESDAY, // = 201 + THURSDAY = 4, + FRIDAY = 5, + SATURDAY = 3, + SUNDAY // = 4 +}; diff --git a/tests/samples/Enum.scala b/tests/samples/Enum.scala new file mode 100644 index 0000000..a0fdc4e --- /dev/null +++ b/tests/samples/Enum.scala @@ -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 +} diff --git a/tests/samples/Typedef.scala b/tests/samples/Typedef.scala index 5ee66db..a7b24b7 100644 --- a/tests/samples/Typedef.scala +++ b/tests/samples/Typedef.scala @@ -26,5 +26,4 @@ object TypedefEnums { final val enum_toggle_e_OFF = 0 final val enum_toggle_e_ON = 1 - } diff --git a/visitor/TreeVisitor.cpp b/visitor/TreeVisitor.cpp index 7d1d507..a053fd3 100644 --- a/visitor/TreeVisitor.cpp +++ b/visitor/TreeVisitor.cpp @@ -62,9 +62,9 @@ bool TreeVisitor::VisitEnumDecl(clang::EnumDecl *enumdecl) { std::vector enumerators; - int i = 0; for (const clang::EnumConstantDecl *en : enumdecl->enumerators()) { - enumerators.push_back(Enumerator(en->getNameAsString(), static_cast(i++))); + uint64_t value = en->getInitVal().getLimitedValue(); + enumerators.push_back(Enumerator(en->getNameAsString(), value)); } ir->addEnum(name, enumerators);