1 .. Copyright (C) 2014-2025 Free Software Foundation, Inc.
2 Originally contributed by David Malcolm <dmalcolm@redhat.com>
4 This is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see
16 <https://www.gnu.org/licenses/>.
23 .. c:type:: gcc_jit_type
25 gcc_jit_type represents a type within the library.
27 .. function:: gcc_jit_object *gcc_jit_type_as_object (gcc_jit_type *type)
29 Upcast a type to an object.
31 Types can be created in several ways:
33 * fundamental types can be accessed using
34 :func:`gcc_jit_context_get_type`:
38 gcc_jit_type *int_type = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
40 See :func:`gcc_jit_context_get_type` for the available types.
42 * derived types can be accessed by using functions such as
43 :func:`gcc_jit_type_get_pointer` and :func:`gcc_jit_type_get_const`:
47 gcc_jit_type *const_int_star = gcc_jit_type_get_pointer (gcc_jit_type_get_const (int_type));
48 gcc_jit_type *int_const_star = gcc_jit_type_get_const (gcc_jit_type_get_pointer (int_type));
50 * by creating structures (see below).
55 .. function:: gcc_jit_type *gcc_jit_context_get_type (gcc_jit_context *ctxt, \
56 enum gcc_jit_types type_)
58 Access a specific type. The available types are:
63 * - `enum gcc_jit_types` value
66 * - :c:data:`GCC_JIT_TYPE_VOID`
68 * - :c:data:`GCC_JIT_TYPE_VOID_PTR`
70 * - :c:data:`GCC_JIT_TYPE_BOOL`
71 - C++'s ``bool`` type; also C99's ``_Bool`` type, aka ``bool`` if using stdbool.h.
72 * - :c:data:`GCC_JIT_TYPE_CHAR`
73 - C's ``char`` (of some signedness)
74 * - :c:data:`GCC_JIT_TYPE_SIGNED_CHAR`
76 * - :c:data:`GCC_JIT_TYPE_UNSIGNED_CHAR`
77 - C's ``unsigned char``
78 * - :c:data:`GCC_JIT_TYPE_SHORT`
79 - C's ``short`` (signed)
80 * - :c:data:`GCC_JIT_TYPE_UNSIGNED_SHORT`
81 - C's ``unsigned short``
82 * - :c:data:`GCC_JIT_TYPE_INT`
83 - C's ``int`` (signed)
84 * - :c:data:`GCC_JIT_TYPE_UNSIGNED_INT`
85 - C's ``unsigned int``
86 * - :c:data:`GCC_JIT_TYPE_LONG`
87 - C's ``long`` (signed)
88 * - :c:data:`GCC_JIT_TYPE_UNSIGNED_LONG`
89 - C's ``unsigned long``
90 * - :c:data:`GCC_JIT_TYPE_LONG_LONG`
91 - C99's ``long long`` (signed)
92 * - :c:data:`GCC_JIT_TYPE_UNSIGNED_LONG_LONG`
93 - C99's ``unsigned long long``
94 * - :c:data:`GCC_JIT_TYPE_UINT8_T`
96 * - :c:data:`GCC_JIT_TYPE_UINT16_T`
98 * - :c:data:`GCC_JIT_TYPE_UINT32_T`
100 * - :c:data:`GCC_JIT_TYPE_UINT64_T`
102 * - :c:data:`GCC_JIT_TYPE_UINT128_T`
103 - C99's ``__uint128_t``
104 * - :c:data:`GCC_JIT_TYPE_INT8_T`
106 * - :c:data:`GCC_JIT_TYPE_INT16_T`
108 * - :c:data:`GCC_JIT_TYPE_INT32_T`
110 * - :c:data:`GCC_JIT_TYPE_INT64_T`
112 * - :c:data:`GCC_JIT_TYPE_INT128_T`
113 - C99's ``__int128_t``
114 * - :c:data:`GCC_JIT_TYPE_FLOAT`
116 * - :c:data:`GCC_JIT_TYPE_BFLOAT16`
118 * - :c:data:`GCC_JIT_TYPE_DOUBLE`
120 * - :c:data:`GCC_JIT_TYPE_LONG_DOUBLE`
122 * - :c:data:`GCC_JIT_TYPE_CONST_CHAR_PTR`
123 - C type: ``(const char *)``
124 * - :c:data:`GCC_JIT_TYPE_SIZE_T`
125 - C's ``size_t`` type
126 * - :c:data:`GCC_JIT_TYPE_FILE_PTR`
127 - C type: ``(FILE *)``
128 * - :c:data:`GCC_JIT_TYPE_COMPLEX_FLOAT`
129 - C99's ``_Complex float``
130 * - :c:data:`GCC_JIT_TYPE_COMPLEX_DOUBLE`
131 - C99's ``_Complex double``
132 * - :c:data:`GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE`
133 - C99's ``_Complex long double``
135 .. function:: gcc_jit_type *\
136 gcc_jit_context_get_int_type (gcc_jit_context *ctxt, \
137 int num_bytes, int is_signed)
139 Access the integer type of the given size.
142 Pointers, `const`, and `volatile`
143 ---------------------------------
145 .. function:: gcc_jit_type *gcc_jit_type_get_pointer (gcc_jit_type *type)
147 Given type "T", get type "T*".
149 .. function:: gcc_jit_type *gcc_jit_type_get_const (gcc_jit_type *type)
151 Given type "T", get type "const T".
153 .. function:: gcc_jit_type *gcc_jit_type_get_volatile (gcc_jit_type *type)
155 Given type "T", get type "volatile T".
157 .. function:: gcc_jit_type *\
158 gcc_jit_context_new_array_type (gcc_jit_context *ctxt, \
159 gcc_jit_location *loc, \
160 gcc_jit_type *element_type, \
163 Given non-`void` type "T", get type "T[N]" (for a constant N).
165 .. function:: gcc_jit_type *\
166 gcc_jit_type_get_aligned (gcc_jit_type *type, \
167 size_t alignment_in_bytes)
169 Given non-`void` type "T", get type:
173 T __attribute__ ((aligned (ALIGNMENT_IN_BYTES)))
175 The alignment must be a power of two.
177 This entrypoint was added in :ref:`LIBGCCJIT_ABI_7`; you can test for
182 #ifdef LIBGCCJIT_HAVE_gcc_jit_type_get_aligned
187 .. function:: gcc_jit_type *\
188 gcc_jit_type_get_vector (gcc_jit_type *type, \
191 Given type "T", get type:
195 T __attribute__ ((vector_size (sizeof(T) * num_units))
197 T must be integral or floating point; num_units must be a power of two.
199 This can be used to construct a vector type in which operations
200 are applied element-wise. The compiler will automatically
201 use SIMD instructions where possible. See:
202 https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
204 For example, assuming 4-byte ``ints``, then:
208 typedef int v4si __attribute__ ((vector_size (16)));
210 can be obtained using:
214 gcc_jit_type *int_type = gcc_jit_context_get_type (ctxt,
216 gcc_jit_type *v4si_type = gcc_jit_type_get_vector (int_type, 4);
218 This API entrypoint was added in :ref:`LIBGCCJIT_ABI_8`; you can test
219 for its presence using
223 #ifdef LIBGCCJIT_HAVE_gcc_jit_type_get_vector
225 Vector rvalues can be generated using
226 :func:`gcc_jit_context_new_rvalue_from_vector`.
229 Structures and unions
230 ---------------------
232 .. c:type:: gcc_jit_struct
234 A compound type analagous to a C `struct`.
236 .. c:type:: gcc_jit_field
238 A field within a :c:type:`gcc_jit_struct`.
240 You can model C `struct` types by creating :c:type:`gcc_jit_struct` and
241 :c:type:`gcc_jit_field` instances, in either order:
243 * by creating the fields, then the structure. For example, to model:
247 struct coord {double x; double y; };
253 gcc_jit_field *field_x =
254 gcc_jit_context_new_field (ctxt, NULL, double_type, "x");
255 gcc_jit_field *field_y =
256 gcc_jit_context_new_field (ctxt, NULL, double_type, "y");
257 gcc_jit_field *fields[2] = {field_x, field_y};
258 gcc_jit_struct *coord =
259 gcc_jit_context_new_struct_type (ctxt, NULL, "coord", 2, fields);
261 * by creating the structure, then populating it with fields, typically
262 to allow modelling self-referential structs such as:
266 struct node { int m_hash; struct node *m_next; };
273 gcc_jit_context_new_opaque_struct (ctxt, NULL, "node");
274 gcc_jit_type *node_ptr =
275 gcc_jit_type_get_pointer (node);
276 gcc_jit_field *field_hash =
277 gcc_jit_context_new_field (ctxt, NULL, int_type, "m_hash");
278 gcc_jit_field *field_next =
279 gcc_jit_context_new_field (ctxt, NULL, node_ptr, "m_next");
280 gcc_jit_field *fields[2] = {field_hash, field_next};
281 gcc_jit_struct_set_fields (node, NULL, 2, fields);
283 .. function:: gcc_jit_field *\
284 gcc_jit_context_new_field (gcc_jit_context *ctxt,\
285 gcc_jit_location *loc,\
289 Construct a new field, with the given type and name.
291 The parameter ``type`` must be non-`void`.
293 The parameter ``name`` must be non-NULL. The call takes a copy of the
294 underlying string, so it is valid to pass in a pointer to an on-stack
297 .. function:: gcc_jit_field *\
298 gcc_jit_context_new_bitfield (gcc_jit_context *ctxt,\
299 gcc_jit_location *loc,\
304 Construct a new bit field, with the given type width and name.
306 The parameter ``name`` must be non-NULL. The call takes a copy of the
307 underlying string, so it is valid to pass in a pointer to an on-stack
310 The parameter ``type`` must be an integer type.
312 The parameter ``width`` must be a positive integer that does not exceed the
315 This API entrypoint was added in :ref:`LIBGCCJIT_ABI_12`; you can test
316 for its presence using
320 #ifdef LIBGCCJIT_HAVE_gcc_jit_context_new_bitfield
322 .. function:: gcc_jit_object *\
323 gcc_jit_field_as_object (gcc_jit_field *field)
325 Upcast from field to object.
327 .. function:: gcc_jit_struct *\
328 gcc_jit_context_new_struct_type (gcc_jit_context *ctxt,\
329 gcc_jit_location *loc,\
332 gcc_jit_field **fields)
334 Construct a new struct type, with the given name and fields.
336 The parameter ``name`` must be non-NULL. The call takes a copy of
337 the underlying string, so it is valid to pass in a pointer to an
340 .. function:: gcc_jit_struct *\
341 gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt,\
342 gcc_jit_location *loc,\
345 Construct a new struct type, with the given name, but without
346 specifying the fields. The fields can be omitted (in which case the
347 size of the struct is not known), or later specified using
348 :c:func:`gcc_jit_struct_set_fields`.
350 The parameter ``name`` must be non-NULL. The call takes a copy of
351 the underlying string, so it is valid to pass in a pointer to an
354 .. function:: gcc_jit_type *\
355 gcc_jit_struct_as_type (gcc_jit_struct *struct_type)
357 Upcast from struct to type.
360 gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,\
361 gcc_jit_location *loc,\
363 gcc_jit_field **fields)
365 Populate the fields of a formerly-opaque struct type.
367 This can only be called once on a given struct type.
369 .. function:: gcc_jit_type *\
370 gcc_jit_context_new_union_type (gcc_jit_context *ctxt,\
371 gcc_jit_location *loc,\
374 gcc_jit_field **fields)
376 Construct a new union type, with the given name and fields.
378 The parameter ``name`` must be non-NULL. It is copied, so the input
379 buffer does not need to outlive the call.
383 .. literalinclude:: ../../../testsuite/jit.dg/test-accessing-union.c
384 :start-after: /* Quote from here in docs/topics/types.rst. */
385 :end-before: /* Quote up to here in docs/topics/types.rst. */
388 Function pointer types
389 ----------------------
391 Function pointer types can be created using
392 :c:func:`gcc_jit_context_new_function_ptr_type`.
397 .. function:: gcc_jit_type *\
398 gcc_jit_type_dyncast_array (gcc_jit_type *type)
400 Get the element type of an array type or NULL if it's not an array.
403 gcc_jit_type_is_bool (gcc_jit_type *type)
405 Return non-zero if the type is a bool.
407 .. function:: gcc_jit_function_type *\
408 gcc_jit_type_dyncast_function_ptr_type (gcc_jit_type *type)
410 Return the function type if it is one or NULL.
412 .. function:: gcc_jit_type *\
413 gcc_jit_function_type_get_return_type (gcc_jit_function_type *function_type)
415 Given a function type, return its return type.
417 .. function:: size_t\
418 gcc_jit_function_type_get_param_count (gcc_jit_function_type *function_type)
420 Given a function type, return its number of parameters.
422 .. function:: gcc_jit_type *\
423 gcc_jit_function_type_get_param_type (gcc_jit_function_type *function_type,\
426 Given a function type, return the type of the specified parameter.
429 gcc_jit_type_is_integral (gcc_jit_type *type)
431 Return non-zero if the type is an integral.
433 .. function:: gcc_jit_type *\
434 gcc_jit_type_is_pointer (gcc_jit_type *type)
436 Return the type pointed by the pointer type or NULL if it's not a pointer.
438 .. function:: gcc_jit_vector_type *\
439 gcc_jit_type_dyncast_vector (gcc_jit_type *type)
441 Given a type, return a dynamic cast to a vector type or NULL.
443 .. function:: gcc_jit_struct *\
444 gcc_jit_type_is_struct (gcc_jit_type *type)
446 Given a type, return a dynamic cast to a struct type or NULL.
448 .. function:: size_t\
449 gcc_jit_vector_type_get_num_units (gcc_jit_vector_type *vector_type)
451 Given a vector type, return the number of units it contains.
453 .. function:: gcc_jit_type *\
454 gcc_jit_vector_type_get_element_type (gcc_jit_vector_type * vector_type)
456 Given a vector type, return the type of its elements.
458 .. function:: gcc_jit_type *\
459 gcc_jit_type_unqualified (gcc_jit_type *type)
461 Given a type, return the unqualified type, removing "const", "volatile" and
462 alignment qualifiers.
464 .. function:: gcc_jit_field *\
465 gcc_jit_struct_get_field (gcc_jit_struct *struct_type,\
468 Get a struct field by index.
470 .. function:: size_t\
471 gcc_jit_struct_get_field_count (gcc_jit_struct *struct_type)
473 Get the number of fields in the struct.
475 The API entrypoints related to the reflection API:
477 * :c:func:`gcc_jit_function_type_get_return_type`
479 * :c:func:`gcc_jit_function_type_get_param_count`
481 * :c:func:`gcc_jit_function_type_get_param_type`
483 * :c:func:`gcc_jit_type_unqualified`
485 * :c:func:`gcc_jit_type_dyncast_array`
487 * :c:func:`gcc_jit_type_is_bool`
489 * :c:func:`gcc_jit_type_dyncast_function_ptr_type`
491 * :c:func:`gcc_jit_type_is_integral`
493 * :c:func:`gcc_jit_type_is_pointer`
495 * :c:func:`gcc_jit_type_dyncast_vector`
497 * :c:func:`gcc_jit_vector_type_get_element_type`
499 * :c:func:`gcc_jit_vector_type_get_num_units`
501 * :c:func:`gcc_jit_struct_get_field`
503 * :c:func:`gcc_jit_type_is_struct`
505 * :c:func:`gcc_jit_struct_get_field_count`
507 were added in :ref:`LIBGCCJIT_ABI_16`; you can test for their presence
512 #ifdef LIBGCCJIT_HAVE_REFLECTION
514 .. type:: gcc_jit_case
517 gcc_jit_compatible_types (gcc_jit_type *ltype,\
520 Return non-zero if the two types are compatible. For instance,
521 if :c:data:`GCC_JIT_TYPE_UINT64_T` and :c:data:`GCC_JIT_TYPE_UNSIGNED_LONG`
522 are the same size on the target, this will return non-zero.
523 The parameters ``ltype`` and ``rtype`` must be non-NULL.
526 This entrypoint was added in :ref:`LIBGCCJIT_ABI_20`; you can test for
531 #ifdef LIBGCCJIT_HAVE_SIZED_INTEGERS
533 .. function:: ssize_t\
534 gcc_jit_type_get_size (gcc_jit_type *type)
536 Return the size of a type, in bytes. It only works on integer types for now.
537 The parameter ``type`` must be non-NULL.
540 This entrypoint was added in :ref:`LIBGCCJIT_ABI_20`; you can test for
545 #ifdef LIBGCCJIT_HAVE_SIZED_INTEGERS
547 .. function:: gcc_jit_type *\
548 gcc_jit_type_get_restrict (gcc_jit_type *type)
550 Given type "T", get type "restrict T".
552 This entrypoint was added in :ref:`LIBGCCJIT_ABI_25`; you can test for
557 #ifdef LIBGCCJIT_HAVE_gcc_jit_type_get_restrict