From 22e1dd031f23ea94606041040ca819131e8a08de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20M=C3=BCller?= Date: Thu, 14 Dec 2023 02:35:17 +0100 Subject: [PATCH 1/6] fix compiler error and segfault --- bindgen/ir/Enum.cpp | 1 + bindgen/ir/IR.cpp | 1 + bindgen/ir/LocationManager.cpp | 1 + bindgen/ir/Record.cpp | 5 +++-- 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/bindgen/ir/Enum.cpp b/bindgen/ir/Enum.cpp index 80116f2..2832ad6 100644 --- a/bindgen/ir/Enum.cpp +++ b/bindgen/ir/Enum.cpp @@ -1,4 +1,5 @@ #include "Enum.h" +#include Enumerator::Enumerator(std::string name, int64_t value) : name(std::move(name)), value(value) {} diff --git a/bindgen/ir/IR.cpp b/bindgen/ir/IR.cpp index fd5dd2b..80b3fe2 100644 --- a/bindgen/ir/IR.cpp +++ b/bindgen/ir/IR.cpp @@ -1,5 +1,6 @@ #include "IR.h" #include "../Utils.h" +#include IR::IR(std::string libName, std::string linkName, std::string objectName, std::string packageName, const LocationManager &locationManager) diff --git a/bindgen/ir/LocationManager.cpp b/bindgen/ir/LocationManager.cpp index dcd5af3..2c6fc92 100644 --- a/bindgen/ir/LocationManager.cpp +++ b/bindgen/ir/LocationManager.cpp @@ -4,6 +4,7 @@ #include "Struct.h" #include #include +#include LocationManager::LocationManager(std::string mainHeaderPath) : mainHeaderPath(std::move(mainHeaderPath)) {} diff --git a/bindgen/ir/Record.cpp b/bindgen/ir/Record.cpp index 3f5dd8e..00378b8 100644 --- a/bindgen/ir/Record.cpp +++ b/bindgen/ir/Record.cpp @@ -23,11 +23,12 @@ bool Record::usesType( if (contains(this, visitedTypes)) { return false; } + visitedTypes.push_back(shared_from_this()); for (const auto &field : fields) { - if (*field->getType() == *type || - field->getType()->usesType(type, stopOnTypeDefs, visitedTypes)) { + if (field->getType() && (*field->getType() == *type || + field->getType()->usesType(type, stopOnTypeDefs, visitedTypes))) { visitedTypes.pop_back(); return true; } From de0470c968e10b282827cede64b811b6bb2d1560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20M=C3=BCller?= Date: Thu, 14 Dec 2023 03:14:28 +0100 Subject: [PATCH 2/6] rename package native to unsafe --- bindgen/TypeTranslator.cpp | 50 ++++++++++++------------ bindgen/defines/DefineFinder.cpp | 14 +++---- bindgen/ir/Enum.cpp | 6 +-- bindgen/ir/Function.cpp | 6 +-- bindgen/ir/IR.cpp | 6 +-- bindgen/ir/IR.h | 4 +- bindgen/ir/Struct.cpp | 14 +++---- bindgen/ir/TypeDef.cpp | 2 +- bindgen/ir/Union.cpp | 12 +++--- bindgen/ir/VarDefine.cpp | 2 +- bindgen/ir/Variable.cpp | 2 +- bindgen/ir/types/ArrayType.cpp | 2 +- bindgen/ir/types/FunctionPointerType.cpp | 4 +- bindgen/ir/types/PointerType.cpp | 2 +- bindgen/ir/types/PrimitiveType.h | 2 +- 15 files changed, 64 insertions(+), 64 deletions(-) diff --git a/bindgen/TypeTranslator.cpp b/bindgen/TypeTranslator.cpp index 553e39a..526b213 100644 --- a/bindgen/TypeTranslator.cpp +++ b/bindgen/TypeTranslator.cpp @@ -8,29 +8,29 @@ TypeTranslator::TypeTranslator(clang::ASTContext *ctx_, IR &ir) // Native Types typeMap["void"] = "Unit"; - typeMap["bool"] = "native.CBool"; - typeMap["_Bool"] = "native.CBool"; - typeMap["char"] = "native.CChar"; - typeMap["signed char"] = "native.CSignedChar"; - typeMap["unsigned char"] = "native.CUnsignedChar"; - typeMap["short"] = "native.CShort"; - typeMap["unsigned short"] = "native.CUnsignedShort"; - typeMap["int"] = "native.CInt"; - typeMap["long int"] = "native.CLongInt"; - typeMap["unsigned int"] = "native.CUnsignedInt"; - typeMap["unsigned long int"] = "native.CUnsignedLongInt"; - typeMap["long"] = "native.CLong"; - typeMap["unsigned long"] = "native.CUnsignedLong"; - typeMap["long long"] = "native.CLongLong"; - typeMap["unsigned long long"] = "native.CUnsignedLongLong"; - typeMap["size_t"] = "native.CSize"; - typeMap["ptrdiff_t"] = "native.CPtrDiff"; - typeMap["wchar_t"] = "native.CWideChar"; - typeMap["char16_t"] = "native.CChar16"; - typeMap["char32_t"] = "native.CChar32"; - typeMap["float"] = "native.CFloat"; - typeMap["double"] = "native.CDouble"; - typeMap["long double"] = "native.CDouble"; + typeMap["bool"] = "unsafe.CBool"; + typeMap["_Bool"] = "unsafe.CBool"; + typeMap["char"] = "unsafe.CChar"; + typeMap["signed char"] = "unsafe.CSignedChar"; + typeMap["unsigned char"] = "unsafe.CUnsignedChar"; + typeMap["short"] = "unsafe.CShort"; + typeMap["unsigned short"] = "unsafe.CUnsignedShort"; + typeMap["int"] = "unsafe.CInt"; + typeMap["long int"] = "unsafe.CLongInt"; + typeMap["unsigned int"] = "unsafe.CUnsignedInt"; + typeMap["unsigned long int"] = "unsafe.CUnsignedLongInt"; + typeMap["long"] = "unsafe.CLong"; + typeMap["unsigned long"] = "unsafe.CUnsignedLong"; + typeMap["long long"] = "unsafe.CLongLong"; + typeMap["unsigned long long"] = "unsafe.CUnsignedLongLong"; + typeMap["size_t"] = "unsafe.CSize"; + typeMap["ptrdiff_t"] = "unsafe.CPtrDiff"; + typeMap["wchar_t"] = "unsafe.CWideChar"; + typeMap["char16_t"] = "unsafe.CChar16"; + typeMap["char32_t"] = "unsafe.CChar32"; + typeMap["float"] = "unsafe.CFloat"; + typeMap["double"] = "unsafe.CDouble"; + typeMap["long double"] = "unsafe.CDouble"; } std::shared_ptr @@ -73,8 +73,8 @@ TypeTranslator::translatePointer(const clang::QualType &pte) { // Take care of char* if (as->getKind() == clang::BuiltinType::Char_S || as->getKind() == clang::BuiltinType::SChar) { - // TODO: new PointerType(new PrimitiveType("native.CChar")) - return std::make_shared("native.CString"); + // TODO: new PointerType(new PrimitiveType("unsafe.CChar")) + return std::make_shared("unsafe.CString"); } } diff --git a/bindgen/defines/DefineFinder.cpp b/bindgen/defines/DefineFinder.cpp index 51db8cb..7bc286e 100644 --- a/bindgen/defines/DefineFinder.cpp +++ b/bindgen/defines/DefineFinder.cpp @@ -48,7 +48,7 @@ void DefineFinder::MacroDefined(const clang::Token ¯oNameTok, stringToken.getLength()); ir.addLiteralDefine( macroName, "c" + literal, - std::make_shared("native.CString")); + std::make_shared("unsafe.CString")); } else if (tokens->size() == 1 && (*tokens)[0].getKind() == clang::tok::identifier) { // token might be a variable @@ -126,7 +126,7 @@ void DefineFinder::addNumericConstantDefine(const std::string ¯oName, if (parser.isLongLong) { /* literal has `LL` ending. `long long` is represented as `Long` * in Scala Native */ - type = "native.CLongLong"; + type = "unsafe.CLongLong"; /* must fit into Scala integer type */ if (!integerFitsIntoType(parser, positive)) { @@ -134,7 +134,7 @@ void DefineFinder::addNumericConstantDefine(const std::string ¯oName, } } else if (parser.isLong) { /* literal has `L` ending */ - type = "native.CLong"; + type = "unsafe.CLong"; /* must fit into Scala integer type */ if (!integerFitsIntoType(parser, positive)) { @@ -146,13 +146,13 @@ void DefineFinder::addNumericConstantDefine(const std::string ¯oName, if (!type.empty()) { scalaLiteral = getDecimalLiteral(parser); - if (type == "native.CLong" || type == "native.CLongLong") { + if (type == "unsafe.CLong" || type == "unsafe.CLongLong") { scalaLiteral = scalaLiteral + "L"; } } } else if (parser.isFloatingLiteral()) { if (fitsIntoDouble(parser)) { - type = "native.CDouble"; + type = "unsafe.CDouble"; scalaLiteral = getDoubleLiteral(parser); } } @@ -172,9 +172,9 @@ DefineFinder::getTypeOfIntegerLiteral(const clang::NumericLiteralParser &parser, bool positive) { if (integerFitsIntoType(parser, positive)) { - return "native.CInt"; + return "unsafe.CInt"; } else if (integerFitsIntoType(parser, positive)) { - return "native.CLong"; + return "unsafe.CLong"; } else { llvm::errs() << "Warning: integer value does not fit into 8 bytes: " << literal << "\n"; diff --git a/bindgen/ir/Enum.cpp b/bindgen/ir/Enum.cpp index 2832ad6..6d49585 100644 --- a/bindgen/ir/Enum.cpp +++ b/bindgen/ir/Enum.cpp @@ -30,11 +30,11 @@ std::string Enum::getEnumerators() const { std::string Enum::getTypeCastSuffix() const { std::string primitiveType = PrimitiveType::getType(); - if (primitiveType == "native.CLong") { + if (primitiveType == "unsafe.CLong") { return "L"; - } else if (primitiveType == "native.CUnsignedInt") { + } else if (primitiveType == "unsafe.CUnsignedInt") { return ".toUInt"; - } else if (primitiveType == "native.CUnsignedLong") { + } else if (primitiveType == "unsafe.CUnsignedLong") { return "L.toULong"; } return ""; diff --git a/bindgen/ir/Function.cpp b/bindgen/ir/Function.cpp index ace1235..376384d 100644 --- a/bindgen/ir/Function.cpp +++ b/bindgen/ir/Function.cpp @@ -17,7 +17,7 @@ std::string Function::getDefinition(const LocationManager &locationManager) const { std::stringstream s; if (scalaName != name) { - s << " @native.link(\"" << name << "\")\n"; + s << " @unsafe.link(\"" << name << "\")\n"; } s << " def " << handleReservedWords(scalaName) << "("; std::string sep = ""; @@ -29,9 +29,9 @@ Function::getDefinition(const LocationManager &locationManager) const { if (isVariadic) { /* the C Iso require at least one argument in a variadic function, so * the comma is fine */ - s << ", " << getVarargsParameterName() << ": native.CVararg*"; + s << ", " << getVarargsParameterName() << ": unsafe.CVararg*"; } - s << "): " << retType->str(locationManager) << " = native.extern\n"; + s << "): " << retType->str(locationManager) << " = unsafe.extern\n"; return s.str(); } diff --git a/bindgen/ir/IR.cpp b/bindgen/ir/IR.cpp index 80b3fe2..1cf72ff 100644 --- a/bindgen/ir/IR.cpp +++ b/bindgen/ir/IR.cpp @@ -104,14 +104,14 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) { } s << "import scala.scalanative._\n" - << "import scala.scalanative.native._\n\n"; + << "import scala.scalanative.unsafe._\n\n"; if (!ir.functions.empty() || !ir.varDefines.empty() || !ir.variables.empty()) { if (!ir.linkName.empty()) { - s << "@native.link(\"" << ir.linkName << "\")\n"; + s << "@unsafe.link(\"" << ir.linkName << "\")\n"; } - s << "@native.extern\n"; + s << "@unsafe.extern\n"; } s << "object " << handleReservedWords(ir.objectName) << " {\n"; diff --git a/bindgen/ir/IR.h b/bindgen/ir/IR.h index 29b8b4d..1f29752 100644 --- a/bindgen/ir/IR.h +++ b/bindgen/ir/IR.h @@ -89,14 +89,14 @@ class IR { * * Example: * @code - * type __int32_t = native.CInt + * type __int32_t = unsafe.CInt * type __darwin_pid_t = __int32_t * type pid_t = __darwin_pid_t * @endcode * * Becomes: * @code - * type pid_t = native.CInt + * type pid_t = unsafe.CInt * @endcode * */ diff --git a/bindgen/ir/Struct.cpp b/bindgen/ir/Struct.cpp index 5d1bd46..13cbd7b 100644 --- a/bindgen/ir/Struct.cpp +++ b/bindgen/ir/Struct.cpp @@ -34,7 +34,7 @@ Struct::generateHelperClass(const LocationManager &locationManager) const { assert(hasHelperMethods()); std::stringstream s; std::string type = replaceChar(getTypeName(), " ", "_"); - s << " implicit class " << type << "_ops(val p: native.Ptr[" << type + s << " implicit class " << type << "_ops(val p: unsafe.Ptr[" << type << "])" << " extends AnyVal {\n"; if (isRepresentedAsStruct()) { @@ -89,7 +89,7 @@ std::string Struct::getTypeName() const { return "struct " + name; } std::string Struct::str(const LocationManager &locationManager) const { std::stringstream ss; - ss << "native.CStruct" << std::to_string(fields.size()) << "["; + ss << "unsafe.CStruct" << std::to_string(fields.size()) << "["; std::string sep = ""; for (const auto &field : fields) { @@ -262,7 +262,7 @@ Struct::getTypeReplacement(std::shared_ptr type, * value type */ replacementType = replacementType->replaceType( recordTypeDef, - std::make_shared("native.CStruct0")); + std::make_shared("unsafe.CStruct0")); } } return replacementType; @@ -350,8 +350,8 @@ Struct::getConstructorHelper(const LocationManager &locationManager) const { << " import implicits._\n"; /* constructor with no parameters */ - s << " def apply()(implicit z: native.Zone): native.Ptr[" + type + "]" - << " = native.alloc[" + type + "]\n"; + s << " def apply()(implicit z: unsafe.Zone): unsafe.Ptr[" + type + "]" + << " = unsafe.alloc[" + type + "]\n"; /* constructor that initializes all fields */ s << " def apply("; @@ -361,8 +361,8 @@ Struct::getConstructorHelper(const LocationManager &locationManager) const { << wrapArrayOrRecordInPointer(field->getType())->str(locationManager); sep = ", "; } - s << ")(implicit z: native.Zone): native.Ptr[" << type << "] = {\n" - << " val ptr = native.alloc[" << type << "]\n"; + s << ")(implicit z: unsafe.Zone): unsafe.Ptr[" << type << "] = {\n" + << " val ptr = unsafe.alloc[" << type << "]\n"; for (const auto &field : fields) { std::string name = handleReservedWords(field->getName()); s << " ptr." << name << " = " << name << "\n"; diff --git a/bindgen/ir/TypeDef.cpp b/bindgen/ir/TypeDef.cpp index 5a40266..fda9287 100644 --- a/bindgen/ir/TypeDef.cpp +++ b/bindgen/ir/TypeDef.cpp @@ -17,7 +17,7 @@ TypeDef::getDefinition(const LocationManager &locationManager) const { if (type) { s << type->str(locationManager); } else { - s << "native.CStruct0 // incomplete type"; + s << "unsafe.CStruct0 // incomplete type"; } s << "\n"; return s.str(); diff --git a/bindgen/ir/Union.cpp b/bindgen/ir/Union.cpp index 855dee9..5f474a9 100644 --- a/bindgen/ir/Union.cpp +++ b/bindgen/ir/Union.cpp @@ -23,7 +23,7 @@ Union::generateHelperClass(const LocationManager &locationManager) const { std::stringstream s; std::string type = replaceChar(getTypeName(), " ", "_"); s << " implicit class " << type << "_pos" - << "(val p: native.Ptr[" << type << "]) extends AnyVal {\n"; + << "(val p: unsafe.Ptr[" << type << "]) extends AnyVal {\n"; for (const auto &field : fields) { if (!field->getName().empty()) { s << generateGetter(field, locationManager); @@ -71,8 +71,8 @@ Union::generateGetter(const std::shared_ptr &field, const LocationManager &locationManager) const { std::string getter = handleReservedWords(field->getName()); std::string ftype = field->getType()->str(locationManager); - return " def " + getter + ": native.Ptr[" + ftype + - "] = p.cast[native.Ptr[" + ftype + "]]\n"; + return " def " + getter + ": unsafe.Ptr[" + ftype + + "] = p.cast[unsafe.Ptr[" + ftype + "]]\n"; } std::string @@ -82,9 +82,9 @@ Union::generateSetter(const std::shared_ptr &field, std::string ftype = field->getType()->str(locationManager); if (isAliasForType(field->getType().get()) || isAliasForType(field->getType().get())) { - return " def " + setter + "(value: native.Ptr[" + ftype + - "]): Unit = !p.cast[native.Ptr[" + ftype + "]] = !value\n"; + return " def " + setter + "(value: unsafe.Ptr[" + ftype + + "]): Unit = !p.cast[unsafe.Ptr[" + ftype + "]] = !value\n"; } return " def " + setter + "(value: " + ftype + - "): Unit = !p.cast[native.Ptr[" + ftype + "]] = value\n"; + "): Unit = !p.cast[unsafe.Ptr[" + ftype + "]] = value\n"; } diff --git a/bindgen/ir/VarDefine.cpp b/bindgen/ir/VarDefine.cpp index 3c6a5a0..d955956 100644 --- a/bindgen/ir/VarDefine.cpp +++ b/bindgen/ir/VarDefine.cpp @@ -7,7 +7,7 @@ std::string VarDefine::getDefinition(const LocationManager &locationManager) const { return " @name(\"" + variable->getName() + "\")\n" + " val " + name + ": " + variable->getType()->str(locationManager) + - " = native.extern\n"; + " = unsafe.extern\n"; } bool VarDefine::hasIllegalUsageOfOpaqueType() const { diff --git a/bindgen/ir/Variable.cpp b/bindgen/ir/Variable.cpp index 1dc2c95..f360e36 100644 --- a/bindgen/ir/Variable.cpp +++ b/bindgen/ir/Variable.cpp @@ -7,7 +7,7 @@ Variable::Variable(const std::string &name, std::shared_ptr type) std::string Variable::getDefinition(const LocationManager &locationManager) const { return " val " + name + ": " + type->str(locationManager) + - " = native.extern\n"; + " = unsafe.extern\n"; } bool Variable::hasIllegalUsageOfOpaqueType() const { diff --git a/bindgen/ir/types/ArrayType.cpp b/bindgen/ir/types/ArrayType.cpp index 0fb27dd..040809b 100644 --- a/bindgen/ir/types/ArrayType.cpp +++ b/bindgen/ir/types/ArrayType.cpp @@ -6,7 +6,7 @@ ArrayType::ArrayType(std::shared_ptr elementsType, uint64_t size) : size(size), elementsType(std::move(elementsType)) {} std::string ArrayType::str(const LocationManager &locationManager) const { - return "native.CArray[" + elementsType->str(locationManager) + ", " + + return "unsafe.CArray[" + elementsType->str(locationManager) + ", " + uint64ToScalaNat(size) + "]"; } diff --git a/bindgen/ir/types/FunctionPointerType.cpp b/bindgen/ir/types/FunctionPointerType.cpp index c5c7d5e..a76549a 100644 --- a/bindgen/ir/types/FunctionPointerType.cpp +++ b/bindgen/ir/types/FunctionPointerType.cpp @@ -11,14 +11,14 @@ FunctionPointerType::FunctionPointerType( std::string FunctionPointerType::str(const LocationManager &locationManager) const { std::stringstream ss; - ss << "native.CFunctionPtr" << parametersTypes.size() << "["; + ss << "unsafe.CFunctionPtr" << parametersTypes.size() << "["; for (const auto ¶meterType : parametersTypes) { ss << parameterType->str(locationManager) << ", "; } if (isVariadic) { - ss << "native.CVararg, "; + ss << "unsafe.CVararg, "; } ss << returnType->str(locationManager) << "]"; return ss.str(); diff --git a/bindgen/ir/types/PointerType.cpp b/bindgen/ir/types/PointerType.cpp index e93d7ca..7c0ae23 100644 --- a/bindgen/ir/types/PointerType.cpp +++ b/bindgen/ir/types/PointerType.cpp @@ -5,7 +5,7 @@ PointerType::PointerType(std::shared_ptr type) : type(std::move(type)) {} std::string PointerType::str(const LocationManager &locationManager) const { - return "native.Ptr[" + type->str(locationManager) + "]"; + return "unsafe.Ptr[" + type->str(locationManager) + "]"; } bool PointerType::usesType( diff --git a/bindgen/ir/types/PrimitiveType.h b/bindgen/ir/types/PrimitiveType.h index 83c91b3..8a07509 100644 --- a/bindgen/ir/types/PrimitiveType.h +++ b/bindgen/ir/types/PrimitiveType.h @@ -5,7 +5,7 @@ #include /** - * For example native.CInt + * For example unsafe.CInt */ class PrimitiveType : virtual public Type { public: From 6ae7b57d1cab68d2b37888ca33bbe525accbf380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20M=C3=BCller?= Date: Thu, 14 Dec 2023 03:55:13 +0100 Subject: [PATCH 3/6] getter, setter, arrays, casts --- bindgen/Utils.h | 17 ----------------- bindgen/ir/Struct.cpp | 19 +++++++++---------- bindgen/ir/Union.cpp | 6 +++--- bindgen/ir/types/ArrayType.cpp | 2 +- 4 files changed, 13 insertions(+), 31 deletions(-) diff --git a/bindgen/Utils.h b/bindgen/Utils.h index 7ba919c..abb691a 100644 --- a/bindgen/Utils.h +++ b/bindgen/Utils.h @@ -5,23 +5,6 @@ #include "ir/types/Type.h" #include -inline std::string uint64ToScalaNat(uint64_t v, std::string accumulator = "") { - if (v == 0) - return accumulator; - - auto last_digit = v % 10; - auto rest = v / 10; - - if (accumulator.empty()) { - return uint64ToScalaNat(rest, - "native.Nat._" + std::to_string(last_digit)); - } else { - return uint64ToScalaNat(rest, "native.Nat.Digit[native.Nat._" + - std::to_string(last_digit) + ", " + - accumulator + "]"); - } -} - static std::array reserved_words = { {"abstract", "case", "catch", "class", "def", "do", "else", "extends", "false", "final", "finally", "for", diff --git a/bindgen/ir/Struct.cpp b/bindgen/ir/Struct.cpp index 13cbd7b..56a7bf2 100644 --- a/bindgen/ir/Struct.cpp +++ b/bindgen/ir/Struct.cpp @@ -136,12 +136,10 @@ std::string Struct::generateSetterForStructRepresentation( /* field type is changed to avoid cyclic types in generated code */ std::shared_ptr typeReplacement = getTypeReplacement( field->getType(), structTypesThatShouldBeReplaced); - value = value + ".cast[" + typeReplacement->str(locationManager) + "]"; - } else if (isArrayOrRecord(field->getType())) { - value = "!" + value; + value = value + ".asInstanceOf[" + typeReplacement->str(locationManager) + "]"; } std::stringstream s; - s << " def " << setter << "(value: " + parameterType + "): Unit = !p._" + s << " def " << setter << "(value: " + parameterType + "): Unit = p._" << std::to_string(fieldIndex + 1) << " = " << value << "\n"; return s.str(); } @@ -154,12 +152,13 @@ std::string Struct::generateGetterForStructRepresentation( wrapArrayOrRecordInPointer(field->getType())->str(locationManager); std::string methodBody = "p._" + std::to_string(fieldIndex + 1); if (!isArrayOrRecord(field->getType())) { - methodBody = "!" + methodBody; if (!shouldFieldBreakCycle(field).empty()) { /* field type is changed to avoid cyclic types in generated code */ - methodBody = "(" + methodBody + ").cast[" + + methodBody = "(" + methodBody + ").asInstanceOf[" + field->getType()->str(locationManager) + "]"; } + } else { + methodBody = "p.at" + std::to_string(fieldIndex + 1); } std::stringstream s; s << " def " << getter << ": " << returnType << " = " << methodBody @@ -186,7 +185,7 @@ std::string Struct::generateSetterForArrayRepresentation( castedField = "(" + castedField + " + " + std::to_string(offsetInBytes) + ")"; } - castedField = "!" + castedField + ".cast[" + + castedField = "!" + castedField + ".asInstanceOf[" + pointerToFieldType.str(locationManager) + "]"; std::vector> structTypesThatShouldBeReplaced = shouldFieldBreakCycle(field); @@ -194,7 +193,7 @@ std::string Struct::generateSetterForArrayRepresentation( /* field type is changed to avoid cyclic types in generated code */ std::shared_ptr typeReplacement = getTypeReplacement( field->getType(), structTypesThatShouldBeReplaced); - value = value + ".cast[" + typeReplacement->str(locationManager) + "]"; + value = value + ".asInstanceOf[" + typeReplacement->str(locationManager) + "]"; } else if (isArrayOrRecord(field->getType())) { value = "!" + value; } @@ -221,13 +220,13 @@ std::string Struct::generateGetterForArrayRepresentation( methodBody = "p._1"; } methodBody = - methodBody + ".cast[" + pointerToFieldType.str(locationManager) + "]"; + methodBody + ".asInstanceOf[" + pointerToFieldType.str(locationManager) + "]"; if (!isArrayOrRecord(field->getType())) { methodBody = "!" + methodBody; if (!shouldFieldBreakCycle(field).empty()) { /* field type is changed to avoid cyclic types in generated code */ - methodBody = "(" + methodBody + ").cast[" + + methodBody = "(" + methodBody + ").asInstanceOf[" + field->getType()->str(locationManager) + "]"; } } diff --git a/bindgen/ir/Union.cpp b/bindgen/ir/Union.cpp index 5f474a9..d6c822e 100644 --- a/bindgen/ir/Union.cpp +++ b/bindgen/ir/Union.cpp @@ -72,7 +72,7 @@ Union::generateGetter(const std::shared_ptr &field, std::string getter = handleReservedWords(field->getName()); std::string ftype = field->getType()->str(locationManager); return " def " + getter + ": unsafe.Ptr[" + ftype + - "] = p.cast[unsafe.Ptr[" + ftype + "]]\n"; + "] = p.asInstanceOf[unsafe.Ptr[" + ftype + "]]\n"; } std::string @@ -83,8 +83,8 @@ Union::generateSetter(const std::shared_ptr &field, if (isAliasForType(field->getType().get()) || isAliasForType(field->getType().get())) { return " def " + setter + "(value: unsafe.Ptr[" + ftype + - "]): Unit = !p.cast[unsafe.Ptr[" + ftype + "]] = !value\n"; + "]): Unit = !p.asInstanceOf[unsafe.Ptr[" + ftype + "]] = !value\n"; } return " def " + setter + "(value: " + ftype + - "): Unit = !p.cast[unsafe.Ptr[" + ftype + "]] = value\n"; + "): Unit = !p.asInstanceOf[unsafe.Ptr[" + ftype + "]] = value\n"; } diff --git a/bindgen/ir/types/ArrayType.cpp b/bindgen/ir/types/ArrayType.cpp index 040809b..d9c5003 100644 --- a/bindgen/ir/types/ArrayType.cpp +++ b/bindgen/ir/types/ArrayType.cpp @@ -7,7 +7,7 @@ ArrayType::ArrayType(std::shared_ptr elementsType, uint64_t size) std::string ArrayType::str(const LocationManager &locationManager) const { return "unsafe.CArray[" + elementsType->str(locationManager) + ", " + - uint64ToScalaNat(size) + "]"; + std::to_string(size) + "]"; } bool ArrayType::usesType( From 2345c51fa720f90d55917dcf823298e88d214901 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20M=C3=BCller?= Date: Thu, 14 Dec 2023 04:19:13 +0100 Subject: [PATCH 4/6] call unsafe.alloc with parentheses --- bindgen/ir/Struct.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bindgen/ir/Struct.cpp b/bindgen/ir/Struct.cpp index 56a7bf2..845c445 100644 --- a/bindgen/ir/Struct.cpp +++ b/bindgen/ir/Struct.cpp @@ -350,7 +350,7 @@ Struct::getConstructorHelper(const LocationManager &locationManager) const { /* constructor with no parameters */ s << " def apply()(implicit z: unsafe.Zone): unsafe.Ptr[" + type + "]" - << " = unsafe.alloc[" + type + "]\n"; + << " = unsafe.alloc[" + type + "]()\n"; /* constructor that initializes all fields */ s << " def apply("; @@ -361,7 +361,7 @@ Struct::getConstructorHelper(const LocationManager &locationManager) const { sep = ", "; } s << ")(implicit z: unsafe.Zone): unsafe.Ptr[" << type << "] = {\n" - << " val ptr = unsafe.alloc[" << type << "]\n"; + << " val ptr = unsafe.alloc[" << type << "]()\n"; for (const auto &field : fields) { std::string name = handleReservedWords(field->getName()); s << " ptr." << name << " = " << name << "\n"; From 0b1485f02e29bbb7cd053e240dc6028c8f5a6de4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20M=C3=BCller?= Date: Thu, 14 Dec 2023 04:20:37 +0100 Subject: [PATCH 5/6] give unnamed struct members a name --- bindgen/TypeTranslator.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/bindgen/TypeTranslator.cpp b/bindgen/TypeTranslator.cpp index 526b213..21868d5 100644 --- a/bindgen/TypeTranslator.cpp +++ b/bindgen/TypeTranslator.cpp @@ -188,13 +188,21 @@ std::shared_ptr TypeTranslator::getLocation(clang::Decl *decl) { return std::make_shared(path, lineNumber); } +std::string getFieldName(const clang::FieldDecl *field) { + std::string name = field->getNameAsString(); + if (name.empty()) { + name = "field" + std::to_string(field->getFieldIndex()); + } + return name; +} + std::shared_ptr TypeTranslator::addUnionDefinition(clang::RecordDecl *record, std::string name) { std::vector> fields; for (const clang::FieldDecl *field : record->fields()) { - std::string fname = field->getNameAsString(); + std::string fname = getFieldName(field); std::shared_ptr ftype = translate(field->getType()); fields.push_back(std::make_shared(fname, ftype)); @@ -229,7 +237,7 @@ TypeTranslator::addStructDefinition(clang::RecordDecl *record, std::shared_ptr ftype = translate(field->getType()); uint64_t recordOffsetInBits = recordLayout.getFieldOffset(field->getFieldIndex()); - fields.push_back(std::make_shared(field->getNameAsString(), + fields.push_back(std::make_shared(getFieldName(field), ftype, recordOffsetInBits)); } From ce457d6b5c5c2e203492a7d5c13e7e3622adb9e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20M=C3=BCller?= Date: Thu, 14 Dec 2023 04:36:39 +0100 Subject: [PATCH 6/6] formatting --- bindgen/TypeTranslator.cpp | 4 ++-- bindgen/ir/LocationManager.cpp | 2 +- bindgen/ir/Record.cpp | 5 +++-- bindgen/ir/Struct.cpp | 10 ++++++---- bindgen/ir/Union.cpp | 3 ++- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/bindgen/TypeTranslator.cpp b/bindgen/TypeTranslator.cpp index 21868d5..5fd47f7 100644 --- a/bindgen/TypeTranslator.cpp +++ b/bindgen/TypeTranslator.cpp @@ -237,8 +237,8 @@ TypeTranslator::addStructDefinition(clang::RecordDecl *record, std::shared_ptr ftype = translate(field->getType()); uint64_t recordOffsetInBits = recordLayout.getFieldOffset(field->getFieldIndex()); - fields.push_back(std::make_shared(getFieldName(field), - ftype, recordOffsetInBits)); + fields.push_back(std::make_shared(getFieldName(field), ftype, + recordOffsetInBits)); } uint64_t sizeInBits = ctx->getTypeSize(record->getTypeForDecl()); diff --git a/bindgen/ir/LocationManager.cpp b/bindgen/ir/LocationManager.cpp index 2c6fc92..0356ceb 100644 --- a/bindgen/ir/LocationManager.cpp +++ b/bindgen/ir/LocationManager.cpp @@ -3,8 +3,8 @@ #include "Enum.h" #include "Struct.h" #include -#include #include +#include LocationManager::LocationManager(std::string mainHeaderPath) : mainHeaderPath(std::move(mainHeaderPath)) {} diff --git a/bindgen/ir/Record.cpp b/bindgen/ir/Record.cpp index 00378b8..e5a7cfd 100644 --- a/bindgen/ir/Record.cpp +++ b/bindgen/ir/Record.cpp @@ -27,8 +27,9 @@ bool Record::usesType( visitedTypes.push_back(shared_from_this()); for (const auto &field : fields) { - if (field->getType() && (*field->getType() == *type || - field->getType()->usesType(type, stopOnTypeDefs, visitedTypes))) { + if (field->getType() && + (*field->getType() == *type || + field->getType()->usesType(type, stopOnTypeDefs, visitedTypes))) { visitedTypes.pop_back(); return true; } diff --git a/bindgen/ir/Struct.cpp b/bindgen/ir/Struct.cpp index 845c445..3a5e73b 100644 --- a/bindgen/ir/Struct.cpp +++ b/bindgen/ir/Struct.cpp @@ -136,7 +136,8 @@ std::string Struct::generateSetterForStructRepresentation( /* field type is changed to avoid cyclic types in generated code */ std::shared_ptr typeReplacement = getTypeReplacement( field->getType(), structTypesThatShouldBeReplaced); - value = value + ".asInstanceOf[" + typeReplacement->str(locationManager) + "]"; + value = value + ".asInstanceOf[" + + typeReplacement->str(locationManager) + "]"; } std::stringstream s; s << " def " << setter << "(value: " + parameterType + "): Unit = p._" @@ -193,7 +194,8 @@ std::string Struct::generateSetterForArrayRepresentation( /* field type is changed to avoid cyclic types in generated code */ std::shared_ptr typeReplacement = getTypeReplacement( field->getType(), structTypesThatShouldBeReplaced); - value = value + ".asInstanceOf[" + typeReplacement->str(locationManager) + "]"; + value = value + ".asInstanceOf[" + + typeReplacement->str(locationManager) + "]"; } else if (isArrayOrRecord(field->getType())) { value = "!" + value; } @@ -219,8 +221,8 @@ std::string Struct::generateGetterForArrayRepresentation( } else { methodBody = "p._1"; } - methodBody = - methodBody + ".asInstanceOf[" + pointerToFieldType.str(locationManager) + "]"; + methodBody = methodBody + ".asInstanceOf[" + + pointerToFieldType.str(locationManager) + "]"; if (!isArrayOrRecord(field->getType())) { methodBody = "!" + methodBody; diff --git a/bindgen/ir/Union.cpp b/bindgen/ir/Union.cpp index d6c822e..59380dd 100644 --- a/bindgen/ir/Union.cpp +++ b/bindgen/ir/Union.cpp @@ -83,7 +83,8 @@ Union::generateSetter(const std::shared_ptr &field, if (isAliasForType(field->getType().get()) || isAliasForType(field->getType().get())) { return " def " + setter + "(value: unsafe.Ptr[" + ftype + - "]): Unit = !p.asInstanceOf[unsafe.Ptr[" + ftype + "]] = !value\n"; + "]): Unit = !p.asInstanceOf[unsafe.Ptr[" + ftype + + "]] = !value\n"; } return " def " + setter + "(value: " + ftype + "): Unit = !p.asInstanceOf[unsafe.Ptr[" + ftype + "]] = value\n";