diff --git a/bindgen/ir/Struct.cpp b/bindgen/ir/Struct.cpp index 0c03556..e8cd1d5 100644 --- a/bindgen/ir/Struct.cpp +++ b/bindgen/ir/Struct.cpp @@ -7,37 +7,6 @@ Field::Field(std::string name, std::shared_ptr type) : TypeAndName(std::move(name), std::move(type)) {} -std::string Field::generateSetter(int fieldIndex) { - std::string setter = handleReservedWords(getName(), "_="); - std::string parameterType = type->str(); - std::string value = "value"; - if (isAliasForType(type.get()) || - isAliasForType(type.get())) { - parameterType = "native.Ptr[" + parameterType + "]"; - value = "!" + value; - } - std::stringstream s; - s << " def " << setter << "(value: " + parameterType + "): Unit = !p._" - << std::to_string(fieldIndex + 1) << " = " << value; - return s.str(); -} - -std::string Field::generateGetter(int fieldIndex) { - std::string getter = handleReservedWords(getName()); - std::string returnType = type->str(); - std::string methodBody; - if (isAliasForType(type.get()) || - isAliasForType(type.get())) { - returnType = "native.Ptr[" + returnType + "]"; - methodBody = "p._" + std::to_string(fieldIndex + 1); - } else { - methodBody = "!p._" + std::to_string(fieldIndex + 1); - } - std::stringstream s; - s << " def " << getter << ": " << returnType << " = " << methodBody; - return s.str(); -} - StructOrUnion::StructOrUnion(std::string name, std::vector> fields, std::shared_ptr location) @@ -101,11 +70,11 @@ std::string Struct::generateHelperClass() const { s << " implicit class " << type << "_ops(val p: native.Ptr[" << type << "])" << " extends AnyVal {\n"; - int fieldIndex = 0; + unsigned fieldIndex = 0; for (const auto &field : fields) { if (!field->getName().empty()) { - s << field->generateGetter(fieldIndex) << "\n"; - s << field->generateSetter(fieldIndex) << "\n"; + s << generateGetter(fieldIndex) << "\n"; + s << generateSetter(fieldIndex) << "\n"; } fieldIndex++; } @@ -158,6 +127,39 @@ bool Struct::operator==(const Type &other) const { return false; } +std::string Struct::generateSetter(unsigned fieldIndex) const { + std::shared_ptr field = fields[fieldIndex]; + std::string setter = handleReservedWords(field->getName(), "_="); + std::string parameterType = field->getType()->str(); + std::string value = "value"; + if (isAliasForType(field->getType().get()) || + isAliasForType(field->getType().get())) { + parameterType = "native.Ptr[" + parameterType + "]"; + value = "!" + value; + } + std::stringstream s; + s << " def " << setter << "(value: " + parameterType + "): Unit = !p._" + << std::to_string(fieldIndex + 1) << " = " << value; + return s.str(); +} + +std::string Struct::generateGetter(unsigned fieldIndex) const { + std::shared_ptr field = fields[fieldIndex]; + std::string getter = handleReservedWords(field->getName()); + std::string returnType = field->getType()->str(); + std::string methodBody; + if (isAliasForType(field->getType().get()) || + isAliasForType(field->getType().get())) { + returnType = "native.Ptr[" + returnType + "]"; + methodBody = "p._" + std::to_string(fieldIndex + 1); + } else { + methodBody = "!p._" + std::to_string(fieldIndex + 1); + } + std::stringstream s; + s << " def " << getter << ": " << returnType << " = " << methodBody; + return s.str(); +} + Union::Union(std::string name, std::vector> fields, uint64_t maxSize, std::shared_ptr location) : StructOrUnion(std::move(name), std::move(fields), std::move(location)), diff --git a/bindgen/ir/Struct.h b/bindgen/ir/Struct.h index 0bc704c..517abe3 100644 --- a/bindgen/ir/Struct.h +++ b/bindgen/ir/Struct.h @@ -12,10 +12,6 @@ class Field : public TypeAndName { public: Field(std::string name, std::shared_ptr type); - - std::string generateSetter(int fieldIndex); - - std::string generateGetter(int fieldIndex); }; class StructOrUnion { @@ -66,6 +62,10 @@ class Struct : public StructOrUnion, bool operator==(const Type &other) const override; + std::string generateSetter(unsigned fieldIndex) const; + + std::string generateGetter(unsigned fieldIndex) const; + private: /* type size is needed if number of fields is bigger than 22 */ uint64_t typeSize;