56#define INFIX_TYPE_INIT(id, T) \
58 .category = INFIX_TYPE_PRIMITIVE, \
60 .alignment = _Alignof(T), \
61 .is_arena_allocated = false, \
64 .meta.primitive_id = id}
74 .is_arena_allocated =
false,
85 .size =
sizeof(
void *),
86 .alignment = _Alignof(
void *),
87 .is_arena_allocated =
false,
109#if !defined(INFIX_COMPILER_MSVC)
119#if defined(INFIX_COMPILER_MSVC) || (defined(INFIX_OS_WINDOWS) && defined(INFIX_COMPILER_CLANG)) || \
120 defined(INFIX_OS_MACOS)
153#if !defined(INFIX_COMPILER_MSVC)
164#if defined(INFIX_COMPILER_MSVC) || (defined(INFIX_OS_WINDOWS) && defined(INFIX_COMPILER_CLANG)) || \
165 defined(INFIX_OS_MACOS)
218 size_t current_byte_offset = 0;
219 uint8_t current_bit_offset = 0;
220 size_t max_alignment = 1;
228 if (current_bit_offset > 0) {
229 if (current_byte_offset == SIZE_MAX) {
233 current_byte_offset++;
234 current_bit_offset = 0;
239 if (member_align == 0)
243 if (aligned < current_byte_offset) {
247 current_byte_offset = aligned;
248 member->
offset = current_byte_offset;
250 if (member_align > max_alignment)
251 max_alignment = member_align;
259 if (current_bit_offset > 0) {
260 if (current_byte_offset == SIZE_MAX) {
264 current_byte_offset++;
265 current_bit_offset = 0;
272 if (aligned < current_byte_offset) {
276 current_byte_offset = aligned;
277 member->
offset = current_byte_offset;
280 if (align > max_alignment)
281 max_alignment = align;
287 if (current_bit_offset + member->
bit_width > 8) {
289 if (current_byte_offset == SIZE_MAX) {
293 current_byte_offset++;
294 current_bit_offset = 0;
297 member->
offset = current_byte_offset;
302 if (current_bit_offset == 8) {
303 if (current_byte_offset == SIZE_MAX) {
307 current_byte_offset++;
308 current_bit_offset = 0;
315 if (align > max_alignment)
316 max_alignment = align;
322 if (current_bit_offset > 0) {
323 if (current_byte_offset == SIZE_MAX) {
327 current_byte_offset++;
328 current_bit_offset = 0;
332 if (member_align == 0)
335 if (member_align > max_alignment)
336 max_alignment = member_align;
339 if (aligned < current_byte_offset) {
343 current_byte_offset = aligned;
344 member->
offset = current_byte_offset;
346 if (current_byte_offset > SIZE_MAX - member->
type->
size) {
350 current_byte_offset += member->
type->
size;
355 if (current_bit_offset > 0)
356 current_byte_offset++;
389 size_t num_members) {
390 if (out_type ==
nullptr)
393 for (
size_t i = 0; i < num_members; ++i) {
394 if (
members[i].type ==
nullptr) {
401 if (type ==
nullptr) {
407 if (num_members > 0) {
410 if (arena_members ==
nullptr) {
418 *out_arena_members = arena_members;
431 if (!out_type || !pointee_type) {
436 if (type ==
nullptr) {
461 size_t num_elements) {
462 if (out_type ==
nullptr || element_type ==
nullptr) {
466 if (element_type->
size > 0 && num_elements > SIZE_MAX / element_type->
size) {
472 if (type ==
nullptr) {
484 type->
size = element_type->
size * num_elements;
499 if (out_type ==
nullptr || element_type ==
nullptr) {
511 if (type ==
nullptr) {
542 if (out_type ==
nullptr || underlying_type ==
nullptr) {
552 if (type ==
nullptr) {
576 if (out_type ==
nullptr || base_type ==
nullptr || (!
is_float(base_type) && !
is_double(base_type))) {
581 if (type ==
nullptr) {
606 size_t num_elements) {
611 if (element_type->
size > 0 && num_elements > SIZE_MAX / element_type->
size) {
617 if (type ==
nullptr) {
626 type->
size = element_type->
size * num_elements;
644 size_t num_members) {
660 size_t max_alignment = 1;
661 for (
size_t i = 0; i < num_members; ++i) {
662 arena_members[i].
offset = 0;
663 if (arena_members[i].type->
size > max_size)
664 max_size = arena_members[i].
type->
size;
665 if (arena_members[i].type->
alignment > max_alignment)
672 if (type->
size < max_size) {
691 size_t num_members) {
716 for (
size_t i = 0; i < num_members; ++i) {
752 size_t num_members) {
753 if (out_type ==
nullptr || (num_members > 0 &&
members ==
nullptr)) {
757 if (alignment == 0) {
762 if (type ==
nullptr) {
768 if (num_members > 0) {
771 if (arena_members ==
nullptr) {
779 type->
size = total_size;
803 if (out_type ==
nullptr || name ==
nullptr) {
808 if (type ==
nullptr) {
814 size_t name_len = strlen(name) + 1;
816 if (arena_name ==
nullptr) {
887 visited_node->
next = *visited_head;
888 *visited_head = visited_node;
912 size_t max_alignment = 1;
915 if (member_type->
size > max_size)
916 max_size = member_type->
size;
917 if (member_type->
alignment > max_alignment)
984 if (src_type ==
nullptr)
988 if (src_type->
arena == dest_arena)
996 if (node->src == src_type)
1000 if (dest_type ==
nullptr)
1007 new_memo_node->
src = src_type;
1008 new_memo_node->
dest = dest_type;
1009 new_memo_node->
next = *memo_head;
1010 *memo_head = new_memo_node;
1012 *dest_type = *src_type;
1015 dest_type->
arena = dest_arena;
1017 if (src_type->
name) {
1018 size_t name_len = strlen(src_type->
name) + 1;
1023 dest_type->
name = dest_name;
1053 size_t name_len = strlen(src_name) + 1;
1074 size_t name_len = strlen(src_name) + 1;
1098 size_t name_len = strlen(src_name) + 1;
1165 if (v->type ==
type)
1170 if (!visited_node) {
1176 visited_node->
next = *visited_head;
1177 *visited_head = visited_node;
1181 total_size += strlen(
type->
name) + 1;
1196 total_size += strlen(member->
name) + 1;
1212 total_size += strlen(arg->
name) + 1;
1239 if (!temp_arena || !
type)
1252 if (
type ==
nullptr)
1330 return trampoline ? trampoline->
num_args : 0;
1346 return trampoline ? trampoline->
return_type :
nullptr;
1355 if (!trampoline || index >= trampoline->
num_args)
1365 return trampoline ? trampoline->
num_args : 0;
1381 return trampoline ? trampoline->
return_type :
nullptr;
1390 if (!trampoline || index >= trampoline->
num_args)
infix_arena_t * arena
Definition 005_layouts.c:60
infix_status status
Definition 103_unions.c:59
infix_struct_member * members
Definition 103_unions.c:53
#define c23_nodiscard
A compatibility macro for the C23 [[nodiscard]] attribute.
Definition compat_c23.h:106
@ INFIX_CODE_INVALID_MEMBER_TYPE
Definition infix.h:1252
@ INFIX_CODE_INVALID_ALIGNMENT
Definition infix.h:1236
@ INFIX_CODE_INTEGER_OVERFLOW
Definition infix.h:1243
@ INFIX_CODE_NULL_POINTER
Definition infix.h:1229
@ INFIX_CODE_OUT_OF_MEMORY
Definition infix.h:1233
@ INFIX_CATEGORY_ALLOCATION
Definition infix.h:1218
@ INFIX_CATEGORY_GENERAL
Definition infix.h:1217
@ INFIX_CATEGORY_PARSER
Definition infix.h:1219
struct infix_type_t::@0::@1 pointer_info
Metadata for INFIX_TYPE_POINTER.
union infix_type_t::@0 meta
A union containing metadata specific to the type's category.
bool is_packed
Definition infix.h:215
struct infix_type_t::@0::@7 vector_info
Metadata for INFIX_TYPE_VECTOR.
bool is_incomplete
Definition infix.h:200
infix_type * type
Definition infix.h:268
struct infix_type_t::@0::@4 func_ptr_info
Metadata for INFIX_TYPE_REVERSE_TRAMPOLINE.
size_t num_elements
Definition infix.h:220
infix_arena_t * arena
Definition infix.h:201
size_t size
Definition infix.h:197
size_t alignment
Definition infix.h:198
uint8_t bit_offset
Definition infix.h:259
infix_struct_member * members
Definition infix.h:213
bool is_bitfield
Definition infix.h:260
struct infix_type_t::@0::@6 complex_info
Metadata for INFIX_TYPE_COMPLEX.
const char * name
Definition infix.h:195
infix_function_argument * args
Definition infix.h:226
infix_aggregate_category_t aggregate_category
Definition infix.h:246
infix_status
Enumerates the possible status codes returned by infix API functions.
Definition infix.h:354
const char * name
Definition infix.h:267
const char * name
Definition infix.h:255
infix_type_category category
Definition infix.h:196
struct infix_type_t::@0::@2 aggregate_info
Metadata for INFIX_TYPE_STRUCT and INFIX_TYPE_UNION.
struct infix_type_t::@0::@3 array_info
Metadata for INFIX_TYPE_ARRAY.
struct infix_type_t * pointee_type
Definition infix.h:209
infix_type * type
Definition infix.h:256
struct infix_type_t * element_type
Definition infix.h:219
bool is_flexible
Definition infix.h:221
struct infix_type_t * return_type
Definition infix.h:225
size_t offset
Definition infix.h:257
struct infix_type_t::@0::@5 enum_info
Metadata for INFIX_TYPE_ENUM.
struct infix_type_t * base_type
Definition infix.h:236
uint8_t bit_width
Definition infix.h:258
size_t num_members
Definition infix.h:214
struct infix_type_t * underlying_type
Definition infix.h:232
struct infix_type_t::@0::@8 named_reference
Metadata for INFIX_TYPE_NAMED_REFERENCE.
infix_primitive_type_id primitive_id
Metadata for INFIX_TYPE_PRIMITIVE.
Definition infix.h:206
size_t num_args
Definition infix.h:227
bool is_arena_allocated
Definition infix.h:199
@ INFIX_ERROR_ALLOCATION_FAILED
Definition infix.h:356
@ INFIX_SUCCESS
Definition infix.h:355
@ INFIX_ERROR_INVALID_ARGUMENT
Definition infix.h:357
c23_nodiscard size_t infix_forward_get_num_args(const infix_forward_t *trampoline)
Gets the total number of arguments for a forward trampoline.
Definition types.c:1329
void infix_arena_destroy(infix_arena_t *)
Destroys an arena and frees all memory allocated from it.
Definition arena.c:83
#define infix_memcpy
A macro that can be defined to override the default memcpy function.
Definition infix.h:307
c23_nodiscard void * infix_arena_calloc(infix_arena_t *, size_t, size_t, size_t)
Allocates and zero-initializes a block of memory from an arena.
Definition arena.c:179
c23_nodiscard void * infix_arena_alloc(infix_arena_t *, size_t, size_t)
Allocates a block of memory from an arena.
Definition arena.c:117
c23_nodiscard infix_arena_t * infix_arena_create(size_t)
Creates a new memory arena.
Definition arena.c:52
c23_nodiscard infix_status infix_type_create_packed_struct(infix_arena_t *arena, infix_type **out_type, size_t total_size, size_t alignment, infix_struct_member *members, size_t num_members)
Creates a new packed struct type with a user-specified layout.
Definition types.c:747
struct infix_type_t infix_type
A semi-opaque object describing a C type's memory layout and calling convention. See infix_type_t for...
Definition infix.h:128
c23_nodiscard size_t infix_type_get_size(const infix_type *type)
Gets the size of a type in bytes.
Definition types.c:1269
c23_nodiscard infix_status infix_type_create_flexible_array(infix_arena_t *arena, infix_type **out_type, infix_type *element_type)
Creates a new flexible array member type ([?:type]).
Definition types.c:496
c23_nodiscard const infix_type * infix_type_get_arg_type(const infix_type *func_type, size_t index)
Gets the type of a specific argument from a function type.
Definition types.c:1318
c23_nodiscard infix_type * infix_type_create_void(void)
Creates a static descriptor for the void type.
Definition types.c:186
c23_nodiscard size_t infix_type_get_alignment(const infix_type *type)
Gets the alignment requirement of a type in bytes.
Definition types.c:1275
c23_nodiscard size_t infix_forward_get_num_fixed_args(const infix_forward_t *trampoline)
Gets the number of fixed (non-variadic) arguments for a forward trampoline.
Definition types.c:1337
c23_nodiscard const char * infix_type_get_name(const infix_type *type)
Gets the semantic alias of a type, if one exists.
Definition types.c:1251
c23_nodiscard const infix_type * infix_forward_get_return_type(const infix_forward_t *trampoline)
Gets the return type for a forward trampoline.
Definition types.c:1345
infix_primitive_type_id
Enumerates the supported primitive C types.
Definition infix.h:164
c23_nodiscard const infix_type * infix_forward_get_arg_type(const infix_forward_t *trampoline, size_t index)
Gets the type of a specific argument for a forward trampoline.
Definition types.c:1354
c23_nodiscard infix_status infix_type_create_union(infix_arena_t *arena, infix_type **out_type, infix_struct_member *members, size_t num_members)
Creates a new union type from an array of members.
Definition types.c:641
c23_nodiscard infix_status infix_type_create_enum(infix_arena_t *arena, infix_type **out_type, infix_type *underlying_type)
Creates a new enum type with a specified underlying integer type.
Definition types.c:539
c23_nodiscard infix_status infix_type_create_vector(infix_arena_t *arena, infix_type **out_type, infix_type *element_type, size_t num_elements)
Creates a new SIMD vector type.
Definition types.c:603
c23_nodiscard const infix_type * infix_reverse_get_return_type(const infix_reverse_t *trampoline)
Gets the return type for a reverse trampoline.
Definition types.c:1380
struct infix_struct_member_t infix_struct_member
A semi-opaque object describing a member of a C struct or union. See infix_struct_member_t for detail...
Definition infix.h:130
c23_nodiscard infix_type * infix_type_create_pointer(void)
Creates a static descriptor for a generic pointer (void*).
Definition types.c:181
c23_nodiscard infix_status infix_type_create_complex(infix_arena_t *arena, infix_type **out_type, infix_type *base_type)
Creates a new _Complex number type.
Definition types.c:573
c23_nodiscard size_t infix_reverse_get_num_args(const infix_reverse_t *trampoline)
Gets the total number of arguments for a reverse trampoline.
Definition types.c:1364
c23_nodiscard size_t infix_type_get_member_count(const infix_type *type)
Gets the number of members in a struct or union type.
Definition types.c:1282
struct infix_function_argument_t infix_function_argument
A semi-opaque object describing an argument to a C function. See infix_function_argument_t for detail...
Definition infix.h:132
c23_nodiscard const char * infix_type_get_arg_name(const infix_type *func_type, size_t index)
Gets the name of a specific argument from a function type.
Definition types.c:1306
infix_type_category
Enumerates the fundamental categories of types that infix can represent.
Definition infix.h:148
c23_nodiscard infix_type_category infix_type_get_category(const infix_type *type)
Gets the fundamental category of a type.
Definition types.c:1261
c23_nodiscard infix_status infix_type_create_array(infix_arena_t *arena, infix_type **out_type, infix_type *element_type, size_t num_elements)
Creates a new fixed-size array type.
Definition types.c:458
c23_nodiscard infix_status infix_type_create_pointer_to(infix_arena_t *arena, infix_type **out_type, infix_type *pointee_type)
Creates a new pointer type that points to a specific type.
Definition types.c:428
infix_aggregate_category_t
Specifies whether a named type reference refers to a struct or a union.
Definition infix.h:184
infix_struct_member infix_type_create_bitfield_member(const char *name, infix_type *type, size_t offset, uint8_t bit_width)
A factory function to create a bitfield infix_struct_member.
Definition types.c:205
c23_nodiscard infix_status infix_type_create_struct(infix_arena_t *arena, infix_type **out_type, infix_struct_member *members, size_t num_members)
Creates a new struct type from an array of members, calculating layout automatically.
Definition types.c:688
c23_nodiscard infix_status infix_type_create_named_reference(infix_arena_t *arena, infix_type **out_type, const char *name, infix_aggregate_category_t agg_cat)
Creates a placeholder for a named type that will be resolved later by a type registry.
Definition types.c:799
c23_nodiscard const infix_type * infix_reverse_get_arg_type(const infix_reverse_t *trampoline, size_t index)
Gets the type of a specific argument for a reverse trampoline.
Definition types.c:1389
c23_nodiscard size_t infix_reverse_get_num_fixed_args(const infix_reverse_t *trampoline)
Gets the number of fixed (non-variadic) arguments for a reverse trampoline.
Definition types.c:1372
c23_nodiscard infix_type * infix_type_create_primitive(infix_primitive_type_id id)
Creates a static descriptor for a primitive C type.
Definition types.c:133
infix_struct_member infix_type_create_member(const char *name, infix_type *type, size_t offset)
A factory function to create an infix_struct_member.
Definition types.c:194
c23_nodiscard const infix_struct_member * infix_type_get_member(const infix_type *type, size_t index)
Gets a specific member from a struct or union type.
Definition types.c:1293
@ INFIX_PRIMITIVE_UINT16
Definition infix.h:168
@ INFIX_PRIMITIVE_UINT32
Definition infix.h:170
@ INFIX_PRIMITIVE_LONG_DOUBLE
Definition infix.h:178
@ INFIX_PRIMITIVE_FLOAT
Definition infix.h:176
@ INFIX_PRIMITIVE_DOUBLE
Definition infix.h:177
@ INFIX_PRIMITIVE_SINT16
Definition infix.h:169
@ INFIX_PRIMITIVE_SINT64
Definition infix.h:173
@ INFIX_PRIMITIVE_SINT32
Definition infix.h:171
@ INFIX_PRIMITIVE_UINT8
Definition infix.h:166
@ INFIX_PRIMITIVE_UINT128
Definition infix.h:174
@ INFIX_PRIMITIVE_BOOL
Definition infix.h:165
@ INFIX_PRIMITIVE_UINT64
Definition infix.h:172
@ INFIX_PRIMITIVE_SINT128
Definition infix.h:175
@ INFIX_PRIMITIVE_SINT8
Definition infix.h:167
@ INFIX_TYPE_UNION
Definition infix.h:152
@ INFIX_TYPE_PRIMITIVE
Definition infix.h:149
@ INFIX_TYPE_COMPLEX
Definition infix.h:156
@ INFIX_TYPE_ARRAY
Definition infix.h:153
@ INFIX_TYPE_VECTOR
Definition infix.h:157
@ INFIX_TYPE_VOID
Definition infix.h:159
@ INFIX_TYPE_POINTER
Definition infix.h:150
@ INFIX_TYPE_NAMED_REFERENCE
Definition infix.h:158
@ INFIX_TYPE_REVERSE_TRAMPOLINE
Definition infix.h:154
@ INFIX_TYPE_ENUM
Definition infix.h:155
@ INFIX_TYPE_STRUCT
Definition infix.h:151
Internal data structures, function prototypes, and constants.
void _infix_set_error(infix_error_category_t category, infix_error_code_t code, size_t position)
Sets the thread-local error state with detailed information.
Definition error.c:173
static size_t _infix_align_up(size_t value, size_t alignment)
Aligns a value up to the next multiple of a power-of-two alignment.
Definition infix_internals.h:727
static bool is_double(const infix_type *type)
A fast inline check to determine if an infix_type is a double.
Definition infix_internals.h:743
void _infix_clear_error(void)
Clears the thread-local error state.
Definition error.c:266
static bool is_float(const infix_type *type)
A fast inline check to determine if an infix_type is a float.
Definition infix_internals.h:735
struct estimate_visited_node_t * next
Definition types.c:1144
const infix_type * type
Definition types.c:1143
Internal definition of a memory arena.
Definition infix_internals.h:138
Internal definition of a forward trampoline handle.
Definition infix_internals.h:88
size_t num_args
Definition infix_internals.h:95
size_t num_fixed_args
Definition infix_internals.h:96
infix_type ** arg_types
Definition infix_internals.h:94
infix_type * return_type
Definition infix_internals.h:93
Describes a single argument to a C function.
Definition infix.h:266
Internal definition of a reverse trampoline (callback/closure) handle.
Definition infix_internals.h:114
infix_type * return_type
Definition infix_internals.h:118
size_t num_args
Definition infix_internals.h:120
size_t num_fixed_args
Definition infix_internals.h:121
infix_type ** arg_types
Definition infix_internals.h:119
Describes a single member of a C struct or union.
Definition infix.h:254
A semi-opaque structure that describes a C type.
Definition infix.h:194
infix_type * dest
Definition types.c:964
struct memo_node_t * next
Definition types.c:965
const infix_type * src
Definition types.c:963
struct recalc_visited_node_t * next
Definition types.c:846
infix_type * type
Definition types.c:845
static size_t _estimate_graph_size_recursive(infix_arena_t *temp_arena, const infix_type *type, estimate_visited_node_t **visited_head)
Definition types.c:1158
static infix_type _infix_type_sint128
Definition types.c:113
void _infix_type_recalculate_layout(infix_type *type)
Recalculates the layout of a fully resolved type graph.
Definition types.c:945
static infix_type _infix_type_double
Definition types.c:118
static infix_type _infix_type_uint32
Definition types.c:102
static infix_type _infix_type_bool
Definition types.c:92
static infix_type _infix_type_float
Definition types.c:116
infix_type * _copy_type_graph_to_arena(infix_arena_t *dest_arena, const infix_type *src_type)
Performs a deep copy of a type graph into a destination arena.
Definition types.c:1133
static infix_type _infix_type_sint64
Definition types.c:108
#define INFIX_TYPE_INIT(id, T)
Definition types.c:56
size_t _infix_estimate_graph_size(infix_arena_t *temp_arena, const infix_type *type)
Estimates the total memory required to deep-copy a complete type graph.
Definition types.c:1238
static infix_type _infix_type_sint16
Definition types.c:100
static infix_type _infix_type_uint16
Definition types.c:98
static infix_type _infix_type_long_double
Definition types.c:124
static infix_type _infix_type_pointer
Definition types.c:83
static infix_type _infix_type_uint128
Definition types.c:111
static infix_type _infix_type_void
Definition types.c:70
static infix_type _infix_type_sint32
Definition types.c:104
static infix_type * _copy_type_graph_to_arena_recursive(infix_arena_t *dest_arena, const infix_type *src_type, memo_node_t **memo_head)
Definition types.c:981
static infix_type _infix_type_uint64
Definition types.c:106
static infix_status _create_aggregate_setup(infix_arena_t *arena, infix_type **out_type, infix_struct_member **out_arena_members, infix_struct_member *members, size_t num_members)
Definition types.c:385
static void _infix_type_recalculate_layout_recursive(infix_arena_t *temp_arena, infix_type *type, recalc_visited_node_t **visited_head)
Definition types.c:870
static bool _layout_struct(infix_type *type)
Definition types.c:217
static infix_type _infix_type_uint8
Definition types.c:94
static infix_type _infix_type_sint8
Definition types.c:96
A header for conditionally compiled debugging utilities.