C unions are distinct from Crystal Unions for a few reasons:
C unions are a layout constraint that associates names to each variant member; Crystal Unions are a type constraint and cannot do the same.
C unions may use repeated types among variant members, like button and axis below.
It is well-defined in C to write to a variant member of a union and then read from a different variant member. (This is undefined behaviour in C++ but most compilers use the C behaviour anyway.)
This patch allows C unions to have their structures copied or their instance property methods generated. Nested structures are also handled properly. After this PR, a compound structure like
typedef struct SDL_GameControllerButtonBind
{
SDL_GameControllerBindType bindType;
union
{
int button;
int axis;
struct {
int hat;
int hat_mask;
} hat;
} value;
} SDL_GameControllerButtonBind;
will generate
lib Binding
struct SDLGameControllerButtonBind
bind_type : SDLGameControllerBindType
value : SDLGameControllerButtonBind_Unnamed0
end
union SDLGameControllerButtonBind_Unnamed0
button : Int32
axis : Int32
hat : SDLGameControllerButtonBind_Unnamed0_Unnamed0
end
struct SDLGameControllerButtonBind_Unnamed0_Unnamed0
hat : Int32
hat_mask : Int32
end
end
enum SDLGameControllerBindType
# ...
end
This PR doesn't change how Crystal wrappers are generated for anonymous types, so if SDL_GameControllerButtonBind is wrapped, the only property methods available would be #bind_type and #bind_type=, as value will be ignored and its members cannot be inlined.
C
union
s are distinct from CrystalUnion
s for a few reasons:union
s are a layout constraint that associates names to each variant member; CrystalUnion
s are a type constraint and cannot do the same.union
s may use repeated types among variant members, likebutton
andaxis
below.union
and then read from a different variant member. (This is undefined behaviour in C++ but most compilers use the C behaviour anyway.)This patch allows C
union
s to have their structures copied or their instance property methods generated. Nested structures are also handled properly. After this PR, a compound structure likewill generate
This PR doesn't change how Crystal wrappers are generated for anonymous types, so if
SDL_GameControllerButtonBind
is wrapped, the only property methods available would be#bind_type
and#bind_type=
, asvalue
will be ignored and its members cannot be inlined.