Skip to content
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

Fix #210: recursive struct via array of pointers indirection #211

Merged
merged 2 commits into from
Jun 20, 2023
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
3 changes: 3 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ lazy val Versions = new {

inThisBuild(
Seq(
semanticdbEnabled := true,
semanticdbVersion := "4.7.8",
semanticdbIncludeInJar := false,
organization := "com.indoorvivants",
organizationName := "Anton Sviridov",
homepage := Some(url("https://github.com/indoorvivants/sn-bindgen")),
Expand Down
12 changes: 12 additions & 0 deletions modules/bindgen/src/main/scala/render/hack_recursive_structs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def isCyclical(typ: CType, structName: StructName)(using
case Pointer(to) =>
go(to, visited, level + 1)

case Arr(of, _) =>
go(of, visited, level + 1)

case _ => Option.empty
end match
// trace((" " * level) + s"result of $t is '$result', visited: $visited")
Expand Down Expand Up @@ -123,6 +126,15 @@ def hack_recursive_structs(
newRichType = Pointer(to)
)
)
case arr @ Arr(to, sz) =>
Some(
ParameterRewrite(
name = parameterName,
originalType = originalType,
newRawType = Arr(Pointer(Void), sz),
newRichType = arr
)
)

case ref @ Reference(Name.Model(name, _)) =>
aliasResolver(name) match
Expand Down
2 changes: 1 addition & 1 deletion modules/bindgen/src/main/scala/render/utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ val scalaKeyWords =
)

def sanitise(name: String) =
val reserved = Set("notify", "wait")
val reserved = Set("notify", "wait", "eq", "ne")
if name == "_" then Sanitation.Renamed("$underscore")
else if reserved(name) then Sanitation.Renamed(s"_$name")
else if scalaKeyWords.contains(name) || name.endsWith("_") then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,23 @@ typedef struct Recrusive_Simple {
double d;
} Recrusive_Simple;

struct Recursive_Func;

typedef struct {
typedef struct Recursive_Func {
double d;
void (*free)(struct Recursive_Func *entry);
int freed;
} Recursive_Func;

typedef struct {
typedef struct Ptr_Recursive {
double d;
struct Ptr_Recursive **elements;
} Ptr_Recursive;

typedef struct {
typedef struct Ptr_Recursive2 {
double d;
struct Ptr_Recursive2 ***elements;
} Ptr_Recursive2;

struct Ptr_Recursive_Array {
struct Ptr_Recursive_Array *opt[3];
int ne;
};
21 changes: 21 additions & 0 deletions modules/tests/src/test/scalanative/TestRecursiveStructs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.junit.Test

import scala.scalanative.unsafe.*
import scala.scalanative.unsigned.*
import lib_test_recursive_structs.structs.Ptr_Recursive_Array

class TestRecursiveStructs:
import lib_test_recursive_structs.types.*
Expand All @@ -28,6 +29,26 @@ class TestRecursiveStructs:
)
}

@Test def test_array_recursive(): Unit =
zone {
val arr = stackalloc[CArray[Ptr[Ptr_Recursive_Array], Nat._3]]()

val st1 = Ptr_Recursive_Array(null, _ne = 25)
val st2 = Ptr_Recursive_Array(null, _ne = 26)
val st3 = Ptr_Recursive_Array(null, _ne = 27)

(!arr)(0) = st1
(!arr)(1) = st2
(!arr)(2) = st3

val st_all = Ptr_Recursive_Array(!arr, 150)

assertEquals((!st_all)._ne, 150)
assertEquals(25, (!(!st_all).opt(0))._ne)
assertEquals(26, (!(!st_all).opt(1))._ne)
assertEquals(27, (!(!st_all).opt(2))._ne)
}

@Test def test_mutually_recursive(): Unit =
zone {
val struct1 = Recrusive_Struct1(null, 1.0)
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.14")

addSbtPlugin("com.indoorvivants" % "sbt-commandmatrix" % "0.0.5")

addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.11")
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12")

addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")

Expand Down