45#define INFIX_TYPE_INIT(id, T) \
46 {.category = INFIX_TYPE_PRIMITIVE, \
48 .alignment = _Alignof(T), \
49 .is_arena_allocated = false, \
50 .meta.primitive_id = id}
61 .size =
sizeof(
void *),
62 .alignment = _Alignof(
void *),
63 .is_arena_allocated =
false,
75#if !defined(INFIX_COMPILER_MSVC)
84#if defined(INFIX_COMPILER_MSVC) || (defined(INFIX_OS_WINDOWS) && defined(INFIX_COMPILER_CLANG)) || \
85 defined(INFIX_OS_MACOS)
117#if !defined(INFIX_COMPILER_MSVC)
130#if defined(INFIX_COMPILER_MSVC) || (defined(INFIX_OS_WINDOWS) && defined(INFIX_COMPILER_CLANG)) || \
131 defined(INFIX_OS_MACOS)
177 size_t num_members) {
178 if (out_type ==
nullptr)
182 for (
size_t i = 0; i < num_members; ++i) {
183 if (
members[i].type ==
nullptr) {
190 if (type ==
nullptr) {
197 if (num_members > 0) {
200 if (arena_members ==
nullptr) {
206 for (
size_t i = 0; i < num_members; ++i) {
209 size_t name_len = strlen(src_name) + 1;
216 arena_members[i].
name = dest_name;
222 *out_arena_members = arena_members;
233 if (!out_type || !pointee_type)
237 if (type ==
nullptr) {
257 size_t num_elements) {
258 if (out_type ==
nullptr || element_type ==
nullptr)
262 if (element_type->
size > 0 && num_elements > SIZE_MAX / element_type->
size) {
269 if (type ==
nullptr) {
279 type->
size = element_type->
size * num_elements;
292 if (out_type ==
nullptr || underlying_type ==
nullptr)
302 if (type ==
nullptr) {
308 (
unsigned long long)type->
size,
327 if (out_type ==
nullptr || base_type ==
nullptr)
335 if (type ==
nullptr) {
357 size_t num_elements) {
362 if (element_type->
size > 0 && num_elements > SIZE_MAX / element_type->
size) {
368 if (type ==
nullptr) {
378 type->
size = element_type->
size * num_elements;
395 size_t num_members) {
411 size_t max_alignment = 1;
415 for (
size_t i = 0; i < num_members; ++i) {
416 arena_members[i].
offset = 0;
417 if (arena_members[i].type->
size > max_size)
418 max_size = arena_members[i].
type->
size;
419 if (arena_members[i].type->
alignment > max_alignment)
427 if (type->
size < max_size) {
433 (
unsigned long long)type->
size,
448 size_t num_members) {
464 size_t current_offset = 0;
465 size_t max_alignment = 1;
468 for (
size_t i = 0; i < num_members; ++i) {
472 if (member_align == 0) {
481 if (aligned_offset < current_offset) {
485 current_offset = aligned_offset;
486 member->
offset = current_offset;
489 if (current_offset > SIZE_MAX - member->
type->
size) {
493 current_offset += member->
type->
size;
495 if (member_align > max_alignment)
496 max_alignment = member_align;
505 if (type->
size < current_offset) {
524 size_t num_members) {
525 if (out_type ==
nullptr || (num_members > 0 &&
members ==
nullptr) || alignment == 0)
529 if (type ==
nullptr) {
536 if (num_members > 0) {
539 if (arena_members ==
nullptr) {
547 type->
size = total_size;
567 if (out_type ==
nullptr || name ==
nullptr)
571 if (type ==
nullptr) {
577 size_t name_len = strlen(name) + 1;
579 if (arena_name ==
nullptr) {
611 if (src_type ==
nullptr)
620 if (dest_type ==
nullptr)
640 size_t name_len = strlen(src_agg_name) + 1;
664 size_t name_len = strlen(src_name) + 1;
691 size_t name_len = strlen(src_name) + 1;
729 return type ? type->
size : 0;
794 return trampoline ? trampoline->
num_args : 0;
810 return trampoline ? trampoline->
return_type :
nullptr;
817 if (!trampoline || index >= trampoline->
num_args)
826 return trampoline ? trampoline->
num_args : 0;
841 return trampoline ? trampoline->
return_type :
nullptr;
848 if (!trampoline || index >= trampoline->
num_args)
infix_arena_t * arena
Definition 005_layouts.c:57
infix_status status
Definition 103_unions.c:74
infix_struct_member * members
Definition 103_unions.c:68
#define c23_nodiscard
Definition compat_c23.h:93
@ INFIX_CODE_INTEGER_OVERFLOW
Definition infix.h:1070
@ INFIX_CATEGORY_PARSER
An error that occurred while parsing a signature string.
Definition infix.h:1043
c23_nodiscard size_t infix_type_get_size(const infix_type *type)
Retrieves the size of an infix_type in bytes.
Definition types.c:728
c23_nodiscard const infix_type * infix_type_get_arg_type(const infix_type *func_type, size_t index)
Retrieves the type of a function argument by its index.
Definition types.c:782
c23_nodiscard size_t infix_type_get_alignment(const infix_type *type)
Retrieves the alignment requirement of an infix_type in bytes.
Definition types.c:736
c23_nodiscard size_t infix_forward_get_num_fixed_args(const infix_forward_t *trampoline)
Retrieves the number of fixed (non-variadic) arguments for a forward trampoline.
Definition types.c:802
c23_nodiscard const infix_type * infix_forward_get_return_type(const infix_forward_t *trampoline)
Retrieves the return type for a forward trampoline.
Definition types.c:809
c23_nodiscard const infix_type * infix_forward_get_arg_type(const infix_forward_t *trampoline, size_t index)
Retrieves the type of a specific argument for a forward trampoline.
Definition types.c:816
c23_nodiscard const infix_type * infix_reverse_get_return_type(const infix_reverse_t *trampoline)
Retrieves the return type for a reverse trampoline.
Definition types.c:840
c23_nodiscard size_t infix_reverse_get_num_args(const infix_reverse_t *trampoline)
Retrieves the number of arguments for a reverse trampoline.
Definition types.c:825
c23_nodiscard size_t infix_type_get_member_count(const infix_type *type)
Retrieves the number of members in an aggregate type (struct or union).
Definition types.c:745
c23_nodiscard const char * infix_type_get_arg_name(const infix_type *func_type, size_t index)
Retrieves the name of a function argument by its index.
Definition types.c:769
c23_nodiscard size_t infix_forward_get_num_args(const infix_forward_t *trampoline)
Retrieves the number of arguments for a forward trampoline.
Definition types.c:793
c23_nodiscard infix_type_category infix_type_get_category(const infix_type *type)
Retrieves the fundamental category of an infix_type.
Definition types.c:719
c23_nodiscard const infix_type * infix_reverse_get_arg_type(const infix_reverse_t *trampoline, size_t index)
Retrieves the type of a specific argument for a reverse trampoline.
Definition types.c:847
c23_nodiscard size_t infix_reverse_get_num_fixed_args(const infix_reverse_t *trampoline)
Retrieves the number of fixed (non-variadic) arguments for a reverse trampoline.
Definition types.c:834
c23_nodiscard const infix_struct_member * infix_type_get_member(const infix_type *type, size_t index)
Retrieves a specific member from an aggregate type by its index.
Definition types.c:756
#define infix_memcpy
A macro for copying memory from a source to a destination pointer.
Definition infix.h:290
c23_nodiscard void * infix_arena_alloc(infix_arena_t *, size_t, size_t)
Allocates a block of memory from the arena with a specific alignment.
Definition arena.c:86
infix_status
An enumeration of all possible success or failure codes from the public API.
Definition infix.h:351
@ INFIX_ERROR_ALLOCATION_FAILED
A memory allocation request failed.
Definition infix.h:353
@ INFIX_SUCCESS
The operation completed successfully.
Definition infix.h:352
@ INFIX_ERROR_INVALID_ARGUMENT
An invalid argument was provided to a function.
Definition infix.h:354
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 infix_type for a packed struct from an arena.
Definition types.c:519
c23_nodiscard infix_type * infix_type_create_void(void)
Creates an infix_type descriptor for the void type.
Definition types.c:153
infix_primitive_type_id
Enumerates the specific primitive C types supported by the FFI system.
Definition infix.h:133
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 infix_type for a union from an arena.
Definition types.c:392
c23_nodiscard infix_status infix_type_create_enum(infix_arena_t *arena, infix_type **out_type, infix_type *underlying_type)
Creates a new infix_type for an enum from an arena.
Definition types.c:289
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 infix_type for a SIMD vector from an arena.
Definition types.c:354
struct infix_struct_member_t infix_struct_member
Describes a single member of an aggregate type (struct or union).
Definition infix.h:94
c23_nodiscard infix_type * infix_type_create_pointer(void)
Creates an infix_type descriptor for a generic void* pointer.
Definition types.c:145
c23_nodiscard infix_status infix_type_create_complex(infix_arena_t *arena, infix_type **out_type, infix_type *base_type)
Creates a new infix_type for a _Complex number from an arena.
Definition types.c:324
struct infix_function_argument_t infix_function_argument
Describes a single argument of a function type, including its optional name.
Definition infix.h:96
infix_type_category
Enumerates the fundamental categories of types supported by the FFI system.
Definition infix.h:114
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 infix_type for a fixed-size array from an arena.
Definition types.c:254
c23_nodiscard infix_status infix_type_create_pointer_to(infix_arena_t *arena, infix_type **out_type, infix_type *pointee_type)
Creates an infix_type for a pointer to a specific type from an arena.
Definition types.c:230
infix_aggregate_category_t
Distinguishes between struct and union for named references.
Definition infix.h:151
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 infix_type for a struct from an arena.
Definition types.c:445
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 new infix_type for a named reference from an arena.
Definition types.c:563
c23_nodiscard infix_type * infix_type_create_primitive(infix_primitive_type_id id)
Creates an infix_type descriptor for a primitive C type.
Definition types.c:97
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:161
@ INFIX_PRIMITIVE_UINT16
unsigned short, uint16_t
Definition infix.h:137
@ INFIX_PRIMITIVE_UINT32
unsigned int, uint32_t
Definition infix.h:139
@ INFIX_PRIMITIVE_LONG_DOUBLE
long double
Definition infix.h:147
@ INFIX_PRIMITIVE_FLOAT
float
Definition infix.h:145
@ INFIX_PRIMITIVE_DOUBLE
double
Definition infix.h:146
@ INFIX_PRIMITIVE_SINT16
signed short, int16_t
Definition infix.h:138
@ INFIX_PRIMITIVE_SINT64
signed long long, int64_t
Definition infix.h:142
@ INFIX_PRIMITIVE_SINT32
signed int, int32_t
Definition infix.h:140
@ INFIX_PRIMITIVE_UINT8
unsigned char, uint8_t
Definition infix.h:135
@ INFIX_PRIMITIVE_UINT128
__uint128_t (GCC/Clang specific)
Definition infix.h:143
@ INFIX_PRIMITIVE_BOOL
bool or _Bool
Definition infix.h:134
@ INFIX_PRIMITIVE_UINT64
unsigned long long, uint64_t
Definition infix.h:141
@ INFIX_PRIMITIVE_SINT128
__int128_t (GCC/Clang specific)
Definition infix.h:144
@ INFIX_PRIMITIVE_SINT8
signed char, int8_t
Definition infix.h:136
@ INFIX_TYPE_UNION
A user-defined union (union).
Definition infix.h:118
@ INFIX_TYPE_PRIMITIVE
A built-in type like int, float, double.
Definition infix.h:115
@ INFIX_TYPE_COMPLEX
A _Complex number type.
Definition infix.h:122
@ INFIX_TYPE_ARRAY
A fixed-size array.
Definition infix.h:119
@ INFIX_TYPE_VECTOR
A SIMD vector type.
Definition infix.h:123
@ INFIX_TYPE_VOID
The void type, used for function returns with no value.
Definition infix.h:125
@ INFIX_TYPE_POINTER
A generic void* pointer type.
Definition infix.h:116
@ INFIX_TYPE_NAMED_REFERENCE
A reference to a named type (e.g., struct<Node>).
Definition infix.h:124
@ INFIX_TYPE_REVERSE_TRAMPOLINE
A callback wrapper.
Definition infix.h:120
@ INFIX_TYPE_ENUM
A C-style enumeration, with an underlying integer type.
Definition infix.h:121
@ INFIX_TYPE_STRUCT
A user-defined structure (struct).
Definition infix.h:117
Declarations for internal-only functions, types, and constants.
void _infix_set_error(infix_error_category_t, infix_error_code_t, size_t)
Sets the thread-local error details for a library-internal error.
Definition error.c:44
static size_t _infix_align_up(size_t value, size_t alignment)
Definition infix_internals.h:383
static bool is_double(const infix_type *type)
Convenience helper to check if an infix_type is a double.
Definition infix_internals.h:394
void _infix_clear_error(void)
Resets the thread-local error state. Called at the start of every public API function.
Definition error.c:76
static bool is_float(const infix_type *type)
Convenience helper to check if an infix_type is a float.
Definition infix_internals.h:389
Definition infix_internals.h:130
Definition infix_internals.h:72
size_t num_args
The total number of arguments.
Definition infix_internals.h:77
size_t num_fixed_args
The number of non-variadic arguments.
Definition infix_internals.h:78
infix_type ** arg_types
An array of infix_type pointers for each argument.
Definition infix_internals.h:76
infix_type * return_type
The infix_type of the trampoline's return value.
Definition infix_internals.h:75
Describes a single argument to a function, pairing an optional name with its type.
Definition infix.h:229
infix_type * type
An infix_type describing the argument's type.
Definition infix.h:231
const char * name
The name of the argument (for reflection). Can be nullptr if anonymous.
Definition infix.h:230
Definition infix_internals.h:102
infix_type * return_type
The infix_type of the callback's return value.
Definition infix_internals.h:106
size_t num_args
The total number of arguments.
Definition infix_internals.h:108
size_t num_fixed_args
The number of non-variadic arguments.
Definition infix_internals.h:109
infix_type ** arg_types
An array of infix_type pointers for each argument.
Definition infix_internals.h:107
Describes a single member of an aggregate type (struct or union).
Definition infix.h:219
const char * name
The name of the member (for debugging/reflection).
Definition infix.h:220
infix_type * type
An infix_type describing the member's type.
Definition infix.h:221
size_t offset
The byte offset of the member from the start of the aggregate.
Definition infix.h:222
The central structure for describing any data type in the FFI system.
Definition infix.h:161
struct infix_type_t::@0::@1 pointer_info
For INFIX_TYPE_POINTER.
union infix_type_t::@0 meta
Type-specific metadata.
struct infix_type_t::@0::@7 vector_info
For INFIX_TYPE_VECTOR.
struct infix_type_t::@0::@4 func_ptr_info
For INFIX_TYPE_REVERSE_TRAMPOLINE.
size_t num_elements
The number of elements in the array.
Definition infix.h:183
size_t size
The total size of the type in bytes, per sizeof.
Definition infix.h:163
size_t alignment
The alignment requirement of the type in bytes, per _Alignof.
Definition infix.h:164
infix_struct_member * members
Array of members for the aggregate.
Definition infix.h:177
struct infix_type_t::@0::@6 complex_info
For INFIX_TYPE_COMPLEX.
const char * name
Optional name of the aggregate.
Definition infix.h:176
infix_function_argument * args
Array of function arguments (name and type).
Definition infix.h:188
infix_aggregate_category_t aggregate_category
Definition infix.h:208
infix_type_category category
The fundamental category of the type.
Definition infix.h:162
struct infix_type_t::@0::@2 aggregate_info
For INFIX_TYPE_STRUCT and INFIX_TYPE_UNION.
struct infix_type_t::@0::@3 array_info
For INFIX_TYPE_ARRAY.
struct infix_type_t * pointee_type
The type this pointer points to.
Definition infix.h:172
struct infix_type_t * element_type
The type of elements in the array.
Definition infix.h:182
struct infix_type_t * return_type
Reverse trampoline return value.
Definition infix.h:187
struct infix_type_t::@0::@5 enum_info
For INFIX_TYPE_ENUM.
struct infix_type_t * base_type
The floating point type of the real and imaginary parts.
Definition infix.h:198
size_t num_members
Number of members in the aggregate.
Definition infix.h:178
struct infix_type_t * underlying_type
The integer type this enum is based on.
Definition infix.h:194
struct infix_type_t::@0::@8 named_reference
For INFIX_TYPE_NAMED_REFERENCE. This is an internal placeholder for a named type like @Point.
infix_primitive_type_id primitive_id
For INFIX_TYPE_PRIMITIVE.
Definition infix.h:169
size_t num_args
The total number of fixed and variadic arguments.
Definition infix.h:189
bool is_arena_allocated
If true, this type was allocated from an arena and should not be individually freed.
Definition infix.h:165
static infix_type _infix_type_sint128
Definition types.c:78
static infix_type _infix_type_double
Definition types.c:81
static infix_type _infix_type_uint32
Definition types.c:71
static infix_type _infix_type_bool
Definition types.c:66
static infix_type _infix_type_float
Definition types.c:80
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 from one arena to another.
Definition types.c:610
static infix_type _infix_type_sint64
Definition types.c:74
#define INFIX_TYPE_INIT(id, T)
Definition types.c:45
static infix_type _infix_type_sint16
Definition types.c:70
static infix_type _infix_type_uint16
Definition types.c:69
static infix_type _infix_type_long_double
Definition types.c:88
static infix_type _infix_type_pointer
Definition types.c:60
static infix_type _infix_type_uint128
Definition types.c:77
static infix_type _infix_type_void
Definition types.c:57
static infix_type _infix_type_sint32
Definition types.c:72
static infix_type _infix_type_uint64
Definition types.c:73
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:173
static infix_type _infix_type_uint8
Definition types.c:67
static infix_type _infix_type_sint8
Definition types.c:68
A header for conditionally compiled debugging utilities.
#define INFIX_DEBUG_PRINTF(...)
Definition utility.h:100