2.3.4 Structures and unions

C Type: gcc_jit_struct

A compound type analagous to a C struct.

C Type: gcc_jit_field

A field within a gcc_jit_struct.

You can model C struct types by creating gcc_jit_struct and gcc_jit_field instances, in either order:

C Function: gcc_jit_field * gcc_jit_context_new_field (gcc_jit_context *ctxt, gcc_jit_location *loc, gcc_jit_type *type, const char *name)

Construct a new field, with the given type and name.

The parameter type must be non-void.

The parameter name must be non-NULL. The call takes a copy of the underlying string, so it is valid to pass in a pointer to an on-stack buffer.

C Function: gcc_jit_field * gcc_jit_context_new_bitfield (gcc_jit_context *ctxt, gcc_jit_location *loc, gcc_jit_type *type, int width, const char *name)

Construct a new bit field, with the given type width and name.

The parameter name must be non-NULL. The call takes a copy of the underlying string, so it is valid to pass in a pointer to an on-stack buffer.

The parameter type must be an integer type.

The parameter width must be a positive integer that does not exceed the size of type.

This API entrypoint was added in LIBGCCJIT_ABI_12; you can test for its presence using

#ifdef LIBGCCJIT_HAVE_gcc_jit_context_new_bitfield
C Function: gcc_jit_object * gcc_jit_field_as_object (gcc_jit_field *field)

Upcast from field to object.

C Function: gcc_jit_struct *gcc_jit_context_new_struct_type (gcc_jit_context *ctxt, gcc_jit_location *loc, const char *name, int num_fields, gcc_jit_field **fields)

Construct a new struct type, with the given name and fields.

The parameter name must be non-NULL. The call takes a copy of the underlying string, so it is valid to pass in a pointer to an on-stack buffer.

C Function: gcc_jit_struct * gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt, gcc_jit_location *loc, const char *name)

Construct a new struct type, with the given name, but without specifying the fields. The fields can be omitted (in which case the size of the struct is not known), or later specified using gcc_jit_struct_set_fields().

The parameter name must be non-NULL. The call takes a copy of the underlying string, so it is valid to pass in a pointer to an on-stack buffer.

C Function: gcc_jit_type * gcc_jit_struct_as_type (gcc_jit_struct *struct_type)

Upcast from struct to type.

C Function: void gcc_jit_struct_set_fields (gcc_jit_struct *struct_type, gcc_jit_location *loc, int num_fields, gcc_jit_field **fields)

Populate the fields of a formerly-opaque struct type.

This can only be called once on a given struct type.

C Function: gcc_jit_type * gcc_jit_context_new_union_type (gcc_jit_context *ctxt, gcc_jit_location *loc, const char *name, int num_fields, gcc_jit_field **fields)

Construct a new union type, with the given name and fields.

The parameter name must be non-NULL. It is copied, so the input buffer does not need to outlive the call.

Example of use:


union int_or_float
{
  int as_int;
  float as_float;
};

void
create_code (gcc_jit_context *ctxt, void *user_data)
{
  /* Let's try to inject the equivalent of:
     float
     test_union (int i)
     {
        union int_or_float u;
	u.as_int = i;
	return u.as_float;
     }
  */
  gcc_jit_type *int_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  gcc_jit_type *float_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT);
  gcc_jit_field *as_int =
    gcc_jit_context_new_field (ctxt,
                               NULL,
                               int_type,
                               "as_int");
  gcc_jit_field *as_float =
    gcc_jit_context_new_field (ctxt,
                               NULL,
                               float_type,
                               "as_float");
  gcc_jit_field *fields[] = {as_int, as_float};
  gcc_jit_type *union_type =
    gcc_jit_context_new_union_type (ctxt, NULL,
				    "int_or_float", 2, fields);

  /* Build the test function.  */
  gcc_jit_param *param_i =
    gcc_jit_context_new_param (ctxt, NULL, int_type, "i");
  gcc_jit_function *test_fn =
    gcc_jit_context_new_function (ctxt, NULL,
                                  GCC_JIT_FUNCTION_EXPORTED,
                                  float_type,
                                  "test_union",
                                  1, &param_i,
                                  0);

  gcc_jit_lvalue *u =
    gcc_jit_function_new_local (test_fn, NULL,
				union_type, "u");

  gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);

  /* u.as_int = i; */
  gcc_jit_block_add_assignment (
    block,
    NULL,
    /* "u.as_int = ..." */
    gcc_jit_lvalue_access_field (u,
				 NULL,
				 as_int),
    gcc_jit_param_as_rvalue (param_i));

  /* return u.as_float; */
  gcc_jit_block_end_with_return (
    block, NULL,
    gcc_jit_rvalue_access_field (gcc_jit_lvalue_as_rvalue (u),
				 NULL,
				 as_float));
}