Skip to content

Commit

Permalink
Move enums to main object
Browse files Browse the repository at this point in the history
  • Loading branch information
kornilova203 committed Aug 10, 2018
1 parent d97e6b9 commit 326f804
Show file tree
Hide file tree
Showing 21 changed files with 185 additions and 185 deletions.
66 changes: 29 additions & 37 deletions bindgen/ir/Enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,30 @@ Enum::Enum(std::string name, std::string type,
: PrimitiveType(std::move(type)), LocatableType(std::move(location)),
name(std::move(name)), enumerators(std::move(enumerators)) {}

bool Enum::isAnonymous() const { return name.empty(); }

std::string Enum::getDefinition() {
return " type " + getTypeAlias() + " = " + PrimitiveType::str() + "\n";
}

llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e) {
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::string type;
if (e.isAnonymous()) {
type = e.getType();
} else {
type = "enum_" + e.name;
}
s << ": " << type << " = " << std::to_string(enumerator.getValue());

if (e.getType() == "native.CLong") {
s << "L";
} else if (e.getType() == "native.CUnsignedInt") {
s << ".toUInt";
} else if (e.getType() == "native.CUnsignedLong") {
s << "L.toULong";
}
s << "\n";
std::string Enum::getEnumerators() const {
std::stringstream s;
std::string typeCastSuffix = getTypeCastSuffix();
std::string type = getTypeAlias();
s << " object " << type << " {\n";
for (auto enumerator : enumerators) {
s << " final val " << enumerator.getName();
s << ": " << type << " = " << std::to_string(enumerator.getValue())
<< typeCastSuffix << "\n";
}
return s;
s << " }\n";
return s.str();
}

std::string Enum::getName() const { return name; }

std::string Enum::getTypeAlias() const {
assert(!isAnonymous());
return "enum_" + name;
std::string Enum::getTypeCastSuffix() const {
std::string primitiveType = PrimitiveType::getType();
if (primitiveType == "native.CLong") {
return "L";
} else if (primitiveType == "native.CUnsignedInt") {
return ".toUInt";
} else if (primitiveType == "native.CUnsignedLong") {
return "L.toULong";
}
return "";
}

std::string Enum::str(const LocationManager &locationManager) const {
Expand All @@ -61,3 +45,11 @@ std::string Enum::str(const LocationManager &locationManager) const {
}
return getTypeAlias();
}

std::string Enum::getTypeAlias() const { return "enum_" + name; }

std::string Enum::getDefinition() const {
return " type " + getTypeAlias() + " = " + PrimitiveType::str() + "\n";
}

std::string Enum::getName() const { return name; }
21 changes: 13 additions & 8 deletions bindgen/ir/Enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,26 @@ class Enum : public PrimitiveType, public LocatableType {
std::vector<Enumerator> enumerators,
std::shared_ptr<Location> location);

bool isAnonymous() const;
/**
* @return a string that contains all enumerators.
* If enum is not anonymous then enumerators are inside an object
* with the same name as enum type.
*/
std::string getEnumerators() const;

std::string getDefinition();
std::string str(const LocationManager &locationManager) const override;

std::string getName() const;

friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e);

std::string getTypeAlias() const;

std::string str(const LocationManager &locationManager) const override;
std::string getDefinition() const;

private:
std::string name; // might be empty
std::string name; // non-empty
std::vector<Enumerator> enumerators;

std::string getTypeCastSuffix() const;

std::string getTypeAlias() const;
};

#endif // SCALA_NATIVE_BINDGEN_ENUM_H
22 changes: 3 additions & 19 deletions bindgen/ir/IR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ std::shared_ptr<Enum> IR::addEnum(std::string name, const std::string &type,
std::shared_ptr<Location> location) {
std::shared_ptr<Enum> e = std::make_shared<Enum>(
std::move(name), type, std::move(enumerators), std::move(location));

enums.push_back(e);

return e;
}

Expand Down Expand Up @@ -122,8 +120,9 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {

for (const auto &e : ir.enums) {
visitedTypes.clear();
if (!e->isAnonymous() && ir.shouldOutput(e, visitedTypes)) {
if (ir.shouldOutput(e, visitedTypes)) {
s << e->getDefinition();
s << e->getEnumerators() << "\n";
}
}

Expand Down Expand Up @@ -184,25 +183,10 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
s << "}\n\n";
}

if (ir.shouldOutputType(ir.enums) || ir.hasHelperMethods()) {
if (ir.hasHelperMethods()) {
s << "import " << objectName << "._\n\n";
}

if (ir.shouldOutputType(ir.enums)) {
s << "object " << ir.libName << "Enums {\n";

std::string sep = "";
for (const auto &e : ir.enums) {
visitedTypes.clear();
if (ir.shouldOutput(e, visitedTypes)) {
s << sep << *e;
sep = "\n";
}
}

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

if (ir.hasHelperMethods()) {
s << "object " << ir.libName << "Helpers {\n";

Expand Down
9 changes: 7 additions & 2 deletions bindgen/visitor/TreeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ bool TreeVisitor::VisitEnumDecl(clang::EnumDecl *enumDecl) {
std::string scalaType = typeTranslator.getTypeFromTypeMap(
enumDecl->getIntegerType().getUnqualifiedType().getAsString());

std::shared_ptr<Location> location = typeTranslator.getLocation(enumDecl);
if (name.empty()) {
name = "anonymous_" + std::to_string(anonymousEnumId++);
}
std::shared_ptr<Enum> e =
ir.addEnum(name, scalaType, std::move(enumerators),
typeTranslator.getLocation(enumDecl));
ir.addEnum(name, scalaType, std::move(enumerators), location);

if (typedefName) {
/* add alias here because in VisitTypedefDecl it will be difficult to
* match typedef with enum */
ir.addTypeDef(name, e, typeTranslator.getLocation(typedefName));
}

Expand Down
1 change: 1 addition & 0 deletions bindgen/visitor/TreeVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TreeVisitor : public clang::RecursiveASTVisitor<TreeVisitor> {
clang::ASTContext *astContext;
TypeTranslator typeTranslator;
IR &ir;
uint anonymousEnumId = 0;

bool isAliasForAnonymousEnum(clang::TypedefDecl *tpdef) const;

Expand Down
6 changes: 6 additions & 0 deletions tests/samples/CustomNames.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import scala.scalanative.native._
@native.link("bindgentests")
@native.extern
object CustomNames {
type enum_enumWithTypedef = native.CUnsignedInt
object enum_enumWithTypedef {
final val CONST: enum_enumWithTypedef = 0.toUInt
}

type EnumWithTypedef = enum_enumWithTypedef
type page = native.CStruct2[native.CString, native.Ptr[Byte]]
type book = native.CStruct1[native.Ptr[page]]
type MY_INT = native.CInt
Expand Down
55 changes: 31 additions & 24 deletions tests/samples/Enum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,40 @@ import scala.scalanative.native._
@native.extern
object Enum {
type enum_days = native.CUnsignedInt
type enum_bigValues = native.CUnsignedLong
type enum_negativeValues = native.CInt
type enum_bigNegativeValues = native.CLong
def get_WEDNESDAY(): enum_days = native.extern
def check_BIG_NEG_A(big_neg_a: enum_bigNegativeValues): native.CString = native.extern
}
object enum_days {
final val MONDAY: enum_days = 0.toUInt
final val TUESDAY: enum_days = 200.toUInt
final val WEDNESDAY: enum_days = 201.toUInt
final val THURSDAY: enum_days = 4.toUInt
final val FRIDAY: enum_days = 5.toUInt
final val SATURDAY: enum_days = 3.toUInt
final val SUNDAY: enum_days = 4.toUInt
}

import Enum._

object EnumEnums {
final val enum_days_MONDAY: enum_days = 0.toUInt
final val enum_days_TUESDAY: enum_days = 200.toUInt
final val enum_days_WEDNESDAY: enum_days = 201.toUInt
final val enum_days_THURSDAY: enum_days = 4.toUInt
final val enum_days_FRIDAY: enum_days = 5.toUInt
final val enum_days_SATURDAY: enum_days = 3.toUInt
final val enum_days_SUNDAY: enum_days = 4.toUInt
type enum_bigValues = native.CUnsignedLong
object enum_bigValues {
final val BIG_A: enum_bigValues = 10000000000L.toULong
final val BIG_B: enum_bigValues = 1L.toULong
}

final val enum_bigValues_BIG_A: enum_bigValues = 10000000000L.toULong
final val enum_bigValues_BIG_B: enum_bigValues = 1L.toULong
type enum_anonymous_0 = native.CUnsignedInt
object enum_anonymous_0 {
final val ANON_A: enum_anonymous_0 = 0.toUInt
final val ANON_B: enum_anonymous_0 = 1.toUInt
}

final val enum_ANON_A: native.CUnsignedInt = 0.toUInt
final val enum_ANON_B: native.CUnsignedInt = 1.toUInt
type enum_negativeValues = native.CInt
object enum_negativeValues {
final val NEG_A: enum_negativeValues = -1
final val NEG_B: enum_negativeValues = -2
}

final val enum_negativeValues_NEG_A: enum_negativeValues = -1
final val enum_negativeValues_NEG_B: enum_negativeValues = -2
type enum_bigNegativeValues = native.CLong
object enum_bigNegativeValues {
final val BIG_NEG_A: enum_bigNegativeValues = -10000000000L
final val BIG_NEG_B: enum_bigNegativeValues = -1L
}

final val enum_bigNegativeValues_BIG_NEG_A: enum_bigNegativeValues = -10000000000L
final val enum_bigNegativeValues_BIG_NEG_B: enum_bigNegativeValues = -1L
def get_WEDNESDAY(): enum_days = native.extern
def check_BIG_NEG_A(big_neg_a: enum_bigNegativeValues): native.CString = native.extern
}
10 changes: 5 additions & 5 deletions tests/samples/Extern.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import scala.scalanative.native._
@native.extern
object Extern {
type enum_mode = native.CUnsignedInt
object enum_mode {
final val SYSTEM: enum_mode = 0.toUInt
final val USER: enum_mode = 1.toUInt
}

type struct_version = native.CStruct3[native.CInt, native.CInt, native.CInt]
val forty_two: native.CInt = native.extern
val version: native.CString = native.extern
Expand All @@ -16,11 +21,6 @@ object Extern {

import Extern._

object ExternEnums {
final val enum_mode_SYSTEM: enum_mode = 0.toUInt
final val enum_mode_USER: enum_mode = 1.toUInt
}

object ExternHelpers {

implicit class struct_version_ops(val p: native.Ptr[struct_version]) extends AnyVal {
Expand Down
10 changes: 5 additions & 5 deletions tests/samples/IncludesHeader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import scala.scalanative.native._
@native.extern
object IncludesHeader {
type enum_semester = native.CUnsignedInt
object enum_semester {
final val AUTUMN: enum_semester = 0.toUInt
final val SPRING: enum_semester = 1.toUInt
}

type size = native.CInt
type struct_metadata = native.CStruct2[native.CUnsignedInt, native.CString]
type metadata = struct_metadata
Expand All @@ -17,11 +22,6 @@ object IncludesHeader {

import IncludesHeader._

object IncludesHeaderEnums {
final val enum_semester_AUTUMN: enum_semester = 0.toUInt
final val enum_semester_SPRING: enum_semester = 1.toUInt
}

object IncludesHeaderHelpers {

implicit class struct_metadata_ops(val p: native.Ptr[struct_metadata]) extends AnyVal {
Expand Down
27 changes: 16 additions & 11 deletions tests/samples/PrivateMembers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,23 @@ import scala.scalanative.native._
@native.extern
object PrivateMembers {
type enum___privateEnum = native.CUnsignedInt
object enum___privateEnum {
final val A: enum___privateEnum = 0.toUInt
final val B: enum___privateEnum = 1.toUInt
}

type enum_enumWithPrivateMembers = native.CUnsignedInt
object enum_enumWithPrivateMembers {
final val __C: enum_enumWithPrivateMembers = 0.toUInt
final val D: enum_enumWithPrivateMembers = 1.toUInt
}

type enum_anonymous_0 = native.CUnsignedInt
object enum_anonymous_0 {
final val __E: enum_anonymous_0 = 0.toUInt
final val F: enum_anonymous_0 = 1.toUInt
}

type pid_t = native.CInt
type __private_type = native.CInt
type struct_structWithPrivateType = native.CStruct2[native.CInt, __private_type]
Expand All @@ -26,17 +42,6 @@ object PrivateMembers {

import PrivateMembers._

object PrivateMembersEnums {
final val enum___privateEnum_A: enum___privateEnum = 0.toUInt
final val enum___privateEnum_B: enum___privateEnum = 1.toUInt

final val enum_enumWithPrivateMembers___C: enum_enumWithPrivateMembers = 0.toUInt
final val enum_enumWithPrivateMembers_D: enum_enumWithPrivateMembers = 1.toUInt

final val enum___E: native.CUnsignedInt = 0.toUInt
final val enum_F: native.CUnsignedInt = 1.toUInt
}

object PrivateMembersHelpers {

implicit class struct_structWithPrivateType_ops(val p: native.Ptr[struct_structWithPrivateType]) extends AnyVal {
Expand Down
1 change: 1 addition & 0 deletions tests/samples/ReuseBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ struct usesImportedEnum {
void readBook(struct book *book);

myInt getMyInt();
enumWithTypedef getEnum();
3 changes: 2 additions & 1 deletion tests/samples/ReuseBindings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"names": {
"struct book": "book",
"struct page": "page",
"myInt": "MY_INT"
"myInt": "MY_INT",
"enumWithTypedef": "EnumWithTypedef"
}
}
}
1 change: 1 addition & 0 deletions tests/samples/ReuseBindings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object ReuseBindings {
def returnTypedef_point(): native.Ptr[org.scalanative.bindgen.samples.Struct.point] = native.extern
def readBook(book: native.Ptr[org.scalanative.bindgen.samples.CustomNames.book]): Unit = native.extern
def getMyInt(): org.scalanative.bindgen.samples.CustomNames.MY_INT = native.extern
def getEnum(): org.scalanative.bindgen.samples.CustomNames.EnumWithTypedef = native.extern
}

import ReuseBindings._
Expand Down
Loading

0 comments on commit 326f804

Please sign in to comment.