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

Improve recursive struct detection #290

Open
keynmol opened this issue Jun 27, 2024 · 0 comments
Open

Improve recursive struct detection #290

keynmol opened this issue Jun 27, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@keynmol
Copy link
Contributor

keynmol commented Jun 27, 2024

This was surprising:

typedef struct FT_ListNodeRec_ *FT_ListNode;

typedef struct FT_ListNodeRec_ {
  FT_ListNode prev;
  FT_ListNode next;
  void *data;

} FT_ListNodeRec;

Generated code doesn't compile:

r] illegal cyclic type reference: alias scala.scalanative.unsafe.Ptr[ft.structs.FT_ListNodeRec_] of type FT_ListNode refers back to the type itself
[error]
[error]  Run with -explain-cyclic for more details.
[error]   opaque type FT_ListNode = Ptr[FT_ListNodeRec_]
[error]               ^
[error] /var/folders/jr/4vy422xj6zs44hsn5d1_b4g40000gn/T/15695818354923484461/.scala-build/15695818354923484461_12528d1359-a97710677f/src_generated/main/stdin.scala:16:36
[error] Found:    scala.scalanative.unsafe.Tag.Ptr[ft.structs.FT_ListNodeRec_]
[error] Required: scala.scalanative.unsafe.Tag[ft.aliases.FT_ListNode]
[error]     given _tag: Tag[FT_ListNode] = Tag.Ptr[FT_ListNodeRec_](FT_ListNodeRec_._tag)
[error]                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error] /var/folders/jr/4vy422xj6zs44hsn5d1_b4g40000gn/T/15695818354923484461/.scala-build/15695818354923484461_12528d1359-a97710677f/src_generated/main/stdin.scala:19:48
[error] Found:    (v : ft.aliases.FT_ListNode)
[error] Required: scala.scalanative.unsafe.Ptr[ft.structs.FT_ListNodeRec_]
[error]       inline def value: Ptr[FT_ListNodeRec_] = v
[error]                                                ^
[error] /var/folders/jr/4vy422xj6zs44hsn5d1_b4g40000gn/T/15695818354923484461/.scala-build/15695818354923484461_12528d1359-a97710677f/src_generated/main/stdin.scala:72:3
[error] exports are only allowed from objects and classes, they can not belong to local blocks
[error]   export _root_.ft.structs.FT_ListNodeRec
[error]   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error] /var/folders/jr/4vy422xj6zs44hsn5d1_b4g40000gn/T/15695818354923484461/.scala-build/15695818354923484461_12528d1359-a97710677f/src_generated/main/stdin.scala:73:3
[error] exports are only allowed from objects and classes, they can not belong to local blocks
[error]   export _root_.ft.structs.FT_ListNodeRec_
[error]   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error compiling project (Scala 3.4.1, Scala Native 0.5.1)

Here's the code generated by 0.1.4

package ft

import _root_.scala.scalanative.unsafe.*
import _root_.scala.scalanative.unsigned.*
import _root_.scala.scalanative.libc.*
import _root_.scala.scalanative.*

object aliases:
  import _root_.ft.aliases.*
  import _root_.ft.structs.*
  /**
   * [bindgen] header: repro.h
  */
  opaque type FT_ListNode = Ptr[FT_ListNodeRec_]
  object FT_ListNode: 
    given _tag: Tag[FT_ListNode] = Tag.Ptr[FT_ListNodeRec_](FT_ListNodeRec_._tag)
    inline def apply(inline o: Ptr[FT_ListNodeRec_]): FT_ListNode = o
    extension (v: FT_ListNode)
      inline def value: Ptr[FT_ListNodeRec_] = v

object structs:
  import _root_.ft.aliases.*
  import _root_.ft.structs.*
  /**
   * [bindgen] header: repro.h
  */
  opaque type FT_ListNodeRec = CStruct3[FT_ListNode, FT_ListNode, Ptr[Byte]]
  object FT_ListNodeRec:
    given _tag: Tag[FT_ListNodeRec] = Tag.materializeCStruct3Tag[FT_ListNode, FT_ListNode, Ptr[Byte]]
    def apply()(using Zone): Ptr[FT_ListNodeRec] = scala.scalanative.unsafe.alloc[FT_ListNodeRec](1)
    def apply(prev : FT_ListNode, next : FT_ListNode, data : Ptr[Byte])(using Zone): Ptr[FT_ListNodeRec] = 
      val ____ptr = apply()
      (!____ptr).prev = prev
      (!____ptr).next = next
      (!____ptr).data = data
      ____ptr
    extension (struct: FT_ListNodeRec)
      def prev : FT_ListNode = struct._1
      def prev_=(value: FT_ListNode): Unit = !struct.at1 = value
      def next : FT_ListNode = struct._2
      def next_=(value: FT_ListNode): Unit = !struct.at2 = value
      def data : Ptr[Byte] = struct._3
      def data_=(value: Ptr[Byte]): Unit = !struct.at3 = value

  /**
   * [bindgen] header: repro.h
  */
  opaque type FT_ListNodeRec_ = CStruct3[FT_ListNode, FT_ListNode, Ptr[Byte]]
  object FT_ListNodeRec_ :
    given _tag: Tag[FT_ListNodeRec_] = Tag.materializeCStruct3Tag[FT_ListNode, FT_ListNode, Ptr[Byte]]
    def apply()(using Zone): Ptr[FT_ListNodeRec_] = scala.scalanative.unsafe.alloc[FT_ListNodeRec_](1)
    def apply(prev : FT_ListNode, next : FT_ListNode, data : Ptr[Byte])(using Zone): Ptr[FT_ListNodeRec_] = 
      val ____ptr = apply()
      (!____ptr).prev = prev
      (!____ptr).next = next
      (!____ptr).data = data
      ____ptr
    extension (struct: FT_ListNodeRec_)
      def prev : FT_ListNode = struct._1
      def prev_=(value: FT_ListNode): Unit = !struct.at1 = value
      def next : FT_ListNode = struct._2
      def next_=(value: FT_ListNode): Unit = !struct.at2 = value
      def data : Ptr[Byte] = struct._3
      def data_=(value: Ptr[Byte]): Unit = !struct.at3 = value

object types:
  export _root_.ft.structs.*
  export _root_.ft.aliases.*

object all:
  export _root_.ft.aliases.FT_ListNode
  export _root_.ft.structs.FT_ListNodeRec
  export _root_.ft.structs.FT_ListNodeRec_
@keynmol keynmol added the bug Something isn't working label Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant