scala-native / scala-native-bindgen

Scala Native Binding Generator

Home Page:https://scala-native.github.io/scala-native-bindgen/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sort types

kornilova203 opened this issue · comments

Bindgen generates types in the order they appear in a header file.

Consider following example:

typedef struct points points;
typedef struct point point;

struct points {
    point *point1;
    point *point2;
};

struct point {
    int x;
    int y;
};

It will generate types in the following order:

type struct_points = native.CStruct2[native.Ptr[point], native.Ptr[point]]
type points = struct_points
type struct_point = native.CStruct2[native.CInt, native.CInt]
type point = struct_point

In generated code points appear before point although points uses point.

To sort types we need to know what types are used by other types and then do topological sort.

There is also one problem that should be considered, types may have cyclic dependency:

struct b;
struct c;

struct a {
   struct b *b;
};

struct b {
    struct c *c;
};

struct c {
    struct a *a;
};

I think this should be moved to 0.4