Skip to content

Add tags to anonymous structs

Simon Ser requested to merge github/fork/Armael/anonymous_structs_tag_names into master

Created by: Armael

At present the wlroots interface includes several anonymous structs, which can make it difficult for some tools to create bindings to wlroots. I stumbled on this issue while writing OCaml FFI bindings for wlroots.

The problem

Currently, a number of struct types have neither a tag name nor a typedef alias. For example, struct wlr_backend contains a struct type without a name:

    struct {
        struct wl_signal destroy;
        struct wl_signal new_input;
        struct wl_signal new_output;
    } events;

Anonymous struct types like the type above make it difficult or impossible for certain tools to create foreign language bindings to a library. For example, one binding strategy (used, for instance, in the OCaml ctypes library) is to generate C code that determines the layout of C objects, then use the layout information to access those objects directly from the foreign language. The generated C code uses type names to determine layout, like this:

     sizeof(struct foo) 
     offsetof (struct foo, field_x) 
     alignmentof (struct foo) 

Clearly this approach won't work if the types involved don't have names.

The fix

The patch adds tags to all the anonymous types that form part of the public interface. The tag names are derived from the enclosing type and the name of the member — for example, the type of the events member in wlr_backend becomes struct wlr_backend_events.


Note that fully anonymous structs and unions with no type and no member name are fine. For example:

    union {
        struct {
            struct wlr_drm_plane *overlay;
            struct wlr_drm_plane *primary;
            struct wlr_drm_plane *cursor;
        };
        struct wlr_drm_plane *planes[3];
    };

Since the intermediate anonymous structs and unions cannot be named in C code, simply inferring the layout of their members is enough.

Merge request reports