51#define MAX_RECURSION_DEPTH 32
70 size_t * out_num_args,
71 size_t * out_num_fixed_args);
92 while (isspace((
unsigned char)*state->
p))
95 while (*state->
p !=
'\n' && *state->
p !=
'\0')
110 const char *
start = state->
p;
112 unsigned long long val = strtoull(
start, &
end, 10);
117 *out_val = (size_t)val;
132 const char *
start = state->
p;
133 if (!isalpha((
unsigned char)*
start) && *
start !=
'_')
135 while (isalnum((
unsigned char)*state->
p) || *state->
p ==
'_' || *state->
p ==
':') {
136 if (*state->
p ==
':' && state->
p[1] !=
':')
138 if (*state->
p ==
':')
142 size_t len = state->
p -
start;
167 size_t len = strlen(keyword);
168 if (strncmp(state->
p, keyword, len) == 0) {
170 if (isalnum((
unsigned char)state->
p[len]) || state->
p[len] ==
'_')
191 const char * p_before = state->
p;
195 if (*state->
p ==
':') {
220 const char * p = state->
p;
226 while (*p !=
'\0' && depth > 0) {
236 while (isspace((
unsigned char)*p) || *p ==
'#') {
238 while (*p !=
'\n' && *p !=
'\0')
244 return (p[0] ==
'-' && p[1] ==
'>');
262 typedef struct member_node {
264 struct member_node * next;
266 member_node *head =
nullptr, *tail =
nullptr;
267 size_t num_members = 0;
270 if (*state->
p != end_char) {
272 const char * p_before_member = state->
p;
275 if (name && (*state->
p ==
',' || *state->
p == end_char)) {
276 state->
p = p_before_member + strlen(name);
299 node->next =
nullptr;
310 if (*state->
p ==
',') {
314 if (*state->
p == end_char) {
319 else if (*state->
p == end_char)
322 if (*state->
p ==
'\0') {
332 *out_num_members = num_members;
333 if (num_members == 0)
343 member_node * current = head;
344 for (
size_t i = 0; i < num_members; i++) {
346 current = current->next;
366 if (*state->
p != start_char) {
373 size_t num_members = 0;
381 if (*state->
p != end_char) {
408 size_t alignment = 1;
409 if (*state->
p ==
'!') {
411 if (isdigit((
unsigned char)*state->
p)) {
415 if (*state->
p !=
':') {
424 if (*state->
p !=
'{') {
429 size_t num_members = 0;
434 if (*state->
p !=
'}') {
446 size_t total_size = 0;
447 for (
size_t i = 0; i < num_members; ++i)
448 total_size +=
members[i].type->size;
611 const char * p_before_type = state->
p;
613 if (*state->
p ==
'@') {
623 result_type =
nullptr;
625 else if (*state->
p ==
'*') {
634 result_type =
nullptr;
636 else if (*state->
p ==
'(') {
640 size_t num_args = 0, num_fixed = 0;
654 func_type->
size =
sizeof(
void *);
662 result_type = func_type;
673 if (*state->
p !=
')') {
675 result_type =
nullptr;
681 else if (*state->
p ==
'[') {
690 if (*state->
p !=
':') {
708 if (*state->
p !=
']') {
715 result_type =
nullptr;
717 else if (*state->
p ==
'!')
719 else if (*state->
p ==
'{')
721 else if (*state->
p ==
'<')
723 else if (*state->
p ==
'e' && state->
p[1] ==
':') {
733 result_type =
nullptr;
735 else if (*state->
p ==
'c' && state->
p[1] ==
'[') {
744 if (*state->
p !=
']') {
751 result_type =
nullptr;
753 else if (*state->
p ==
'v' && state->
p[1] ==
'[') {
761 if (*state->
p !=
':') {
772 if (*state->
p !=
']') {
779 result_type =
nullptr;
786 state->
p = p_before_type;
787 if (isalpha((
unsigned char)*state->
p) || *state->
p ==
'_')
815 size_t * out_num_args,
816 size_t * out_num_fixed_args) {
817 if (*state->
p !=
'(') {
825 typedef struct arg_node {
827 struct arg_node * next;
829 arg_node *head =
nullptr, *tail =
nullptr;
833 if (*state->
p !=
')' && *state->
p !=
';') {
836 if (*state->
p ==
')' || *state->
p ==
';')
849 node->arg.type = arg_type;
850 node->arg.name = name;
851 node->next =
nullptr;
860 if (*state->
p ==
',') {
863 if (*state->
p ==
')' || *state->
p ==
';') {
868 else if (*state->
p !=
')' && *state->
p !=
';') {
876 *out_num_fixed_args = num_args;
879 if (*state->
p ==
';') {
881 if (*state->
p !=
')') {
884 if (*state->
p ==
')')
897 node->arg.type = arg_type;
898 node->arg.name = name;
899 node->next =
nullptr;
908 if (*state->
p ==
',') {
911 if (*state->
p ==
')') {
916 else if (*state->
p !=
')') {
926 if (*state->
p !=
')') {
934 if (state->
p[0] !=
'-' || state->
p[1] !=
'>') {
947 if (num_args > 0 && !
args) {
951 arg_node * current = head;
952 for (
size_t i = 0; i < num_args; i++) {
953 args[i] = current->arg;
954 current = current->next;
957 *out_num_args = num_args;
981 const char * signature) {
982 if (!out_type || !out_arena || !signature || *signature ==
'\0') {
992 parser_state state = {.
p = signature, .start = signature, .arena = *out_arena, .depth = 0};
997 if (state.
p[0] !=
'\0') {
1005 *out_arena =
nullptr;
1006 *out_type =
nullptr;
1028 const char * signature,
1053 *out_arena =
nullptr;
1062 *out_arena =
nullptr;
1063 *out_type =
nullptr;
1068 *out_type = final_type;
1095 size_t * out_num_args,
1096 size_t * out_num_fixed_args,
1099 if (!signature || !out_arena || !out_ret_type || !out_args || !out_num_args || !out_num_fixed_args) {
1130 if (!final_func_type) {
1132 *out_arena =
nullptr;
1141 *out_arena =
nullptr;
1179 va_start(
args, fmt);
1182 if (written < 0 || (
size_t)written >= state->
remaining)
1186 state->
p += written;
1302 bool printed_alias =
false;
1305 if (num_elements == 8 &&
is_double(element_type)) {
1307 printed_alias =
true;
1309 else if (num_elements == 16 &&
is_float(element_type)) {
1311 printed_alias =
true;
1315 printed_alias =
true;
1317 else if (num_elements == 4 &&
is_double(element_type)) {
1319 printed_alias =
true;
1321 else if (num_elements == 8 &&
is_float(element_type)) {
1323 printed_alias =
true;
1327 if (!printed_alias) {
1328 _print(state,
"v[%zu:", num_elements);
1364 _print(state,
"sint128");
1367 _print(state,
"uint128");
1376 _print(state,
"longdouble");
1493 _print(state,
"sint128");
1496 _print(state,
"uint128");
1505 _print(state,
"longdouble");
1541 buffer[buffer_size - 1] =
'\0';
1559 if (!buffer || buffer_size == 0 || !type) {
1571 _print(&state,
"unsupported_dialect");
1580 buffer[buffer_size - 1] =
'\0';
1584 else if (buffer_size > 0)
1586 buffer[buffer_size - 1] =
'\0';
1605 const char * function_name,
1609 size_t num_fixed_args,
1612 if (!buffer || buffer_size == 0 || !
ret_type || (num_args > 0 && !
args)) {
1619 (void)function_name;
1623 for (
size_t i = 0; i < num_fixed_args; ++i) {
1628 if (num_args > num_fixed_args) {
1630 for (
size_t i = num_fixed_args; i < num_args; ++i) {
1631 if (i > num_fixed_args)
1640 _print(&state,
"unsupported_dialect");
1648 if (buffer_size > 0)
1649 buffer[buffer_size - 1] =
'\0';
1653 else if (buffer_size > 0)
1654 buffer[buffer_size - 1] =
'\0';
1674 if (!buffer || buffer_size == 0 || !
registry)
1684 if (entry->type && !entry->is_forward_declaration) {
1685 char type_body_buffer[1024];
1691 goto end_print_loop;
1694 _print(&state,
"@%s = %s;\n", entry->name, type_body_buffer);
1696 goto end_print_loop;
infix_registry_t * registry
Definition 008_registry_introspection.c:35
infix_status status
Definition 103_unions.c:66
infix_struct_member * members
Definition 103_unions.c:60
void * args[]
Definition 202_in_structs.c:64
clock_t start
Definition 901_call_overhead.c:50
infix_type * ret_type
Definition 901_call_overhead.c:66
clock_t end
Definition 901_call_overhead.c:50
#define c23_nodiscard
A compatibility macro for the C23 [[nodiscard]] attribute.
Definition compat_c23.h:113
#define INFIX_TLS
Definition error.c:58
infix_error_code_t
Enumerates specific error codes.
Definition infix.h:1324
infix_error_details_t infix_get_last_error(void)
Retrieves detailed information about the last error that occurred on the current thread.
Definition error.c:270
@ INFIX_CODE_SUCCESS
Definition infix.h:1326
@ INFIX_CODE_INVALID_MEMBER_TYPE
Definition infix.h:1347
@ INFIX_CODE_UNEXPECTED_TOKEN
Definition infix.h:1335
@ INFIX_CODE_MISSING_RETURN_TYPE
Definition infix.h:1338
@ INFIX_CODE_UNKNOWN
Definition infix.h:1327
@ INFIX_CODE_RECURSION_DEPTH_EXCEEDED
Definition infix.h:1340
@ INFIX_CODE_UNTERMINATED_AGGREGATE
Definition infix.h:1336
@ INFIX_CODE_INVALID_KEYWORD
Definition infix.h:1337
@ INFIX_CODE_OUT_OF_MEMORY
Definition infix.h:1330
@ INFIX_CATEGORY_ALLOCATION
Definition infix.h:1316
@ INFIX_CATEGORY_GENERAL
Definition infix.h:1315
@ INFIX_CATEGORY_PARSER
Definition infix.h:1317
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.
struct infix_type_t::@0::@7 vector_info
Metadata for INFIX_TYPE_VECTOR.
infix_type * type
Definition infix.h:289
struct infix_type_t::@0::@4 func_ptr_info
Metadata for INFIX_TYPE_REVERSE_TRAMPOLINE.
c23_nodiscard infix_status infix_type_from_signature(infix_type **out_type, infix_arena_t **out_arena, const char *signature, infix_registry_t *registry)
Parses a signature string representing a single data type.
Definition signature.c:1026
size_t num_elements
Definition infix.h:238
size_t size
Definition infix.h:214
size_t alignment
Definition infix.h:215
infix_struct_member * members
Definition infix.h:231
struct infix_type_t::@0::@6 complex_info
Metadata for INFIX_TYPE_COMPLEX.
const char * name
Definition infix.h:212
infix_function_argument * args
Definition infix.h:244
infix_status
Enumerates the possible status codes returned by infix API functions.
Definition infix.h:389
const char * name
Definition infix.h:288
const char * name
Definition infix.h:278
infix_type_category category
Definition infix.h:213
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:226
infix_type * type
Definition infix.h:279
struct infix_type_t * element_type
Definition infix.h:237
struct infix_type_t * return_type
Definition infix.h:243
c23_nodiscard infix_status infix_signature_parse(const char *signature, infix_arena_t **out_arena, infix_type **out_ret_type, infix_function_argument **out_args, size_t *out_num_args, size_t *out_num_fixed_args, infix_registry_t *registry)
Parses a full function signature string into its constituent parts.
Definition signature.c:1091
struct infix_type_t::@0::@5 enum_info
Metadata for INFIX_TYPE_ENUM.
struct infix_type_t * base_type
Definition infix.h:256
size_t num_members
Definition infix.h:232
struct infix_type_t * underlying_type
Definition infix.h:251
struct infix_type_t::@0::@8 named_reference
Metadata for INFIX_TYPE_NAMED_REFERENCE.
size_t num_fixed_args
Definition infix.h:246
infix_primitive_type_id primitive_id
Metadata for INFIX_TYPE_PRIMITIVE.
Definition infix.h:222
size_t num_args
Definition infix.h:245
bool is_arena_allocated
Definition infix.h:216
@ INFIX_ERROR_ALLOCATION_FAILED
Definition infix.h:391
@ INFIX_SUCCESS
Definition infix.h:390
@ INFIX_ERROR_INVALID_ARGUMENT
Definition infix.h:392
c23_nodiscard infix_status infix_type_print(char *buffer, size_t buffer_size, const infix_type *type, infix_print_dialect_t dialect)
Serializes an infix_type object graph back into a signature string.
Definition signature.c:1554
c23_nodiscard infix_status infix_function_print(char *buffer, size_t buffer_size, const char *function_name, const infix_type *ret_type, const infix_function_argument *args, size_t num_args, size_t num_fixed_args, infix_print_dialect_t dialect)
Serializes a function signature's components into a string.
Definition signature.c:1603
infix_print_dialect_t
Specifies the output format for printing types and function signatures.
Definition infix.h:1112
@ INFIX_DIALECT_SIGNATURE
Definition infix.h:1113
void infix_arena_destroy(infix_arena_t *)
Destroys an arena and frees all memory allocated from it.
Definition arena.c:90
#define infix_memcpy
A macro that can be defined to override the default memcpy function.
Definition infix.h:335
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:198
c23_nodiscard infix_arena_t * infix_arena_create(size_t)
Creates a new memory arena.
Definition arena.c:55
c23_nodiscard infix_status infix_registry_print(char *buffer, size_t buffer_size, const infix_registry_t *registry)
Serializes all defined types within a registry into a single, human-readable string.
Definition signature.c:1673
c23_nodiscard infix_status infix_type_create_packed_struct(infix_arena_t *, infix_type **, size_t, size_t, infix_struct_member *, size_t)
Creates a new packed struct type with a user-specified layout.
Definition types.c:572
c23_nodiscard infix_type * infix_type_create_void(void)
Creates a static descriptor for the void type.
Definition types.c:193
c23_nodiscard infix_status infix_type_create_union(infix_arena_t *, infix_type **, infix_struct_member *, size_t)
Creates a new union type from an array of members.
Definition types.c:442
c23_nodiscard infix_status infix_type_create_enum(infix_arena_t *, infix_type **, infix_type *)
Creates a new enum type with a specified underlying integer type.
Definition types.c:337
c23_nodiscard infix_status infix_type_create_vector(infix_arena_t *, infix_type **, infix_type *, size_t)
Creates a new SIMD vector type.
Definition types.c:403
c23_nodiscard infix_status infix_type_create_complex(infix_arena_t *, infix_type **, infix_type *)
Creates a new _Complex number type.
Definition types.c:372
c23_nodiscard infix_status infix_type_create_array(infix_arena_t *, infix_type **, infix_type *, size_t)
Creates a new fixed-size array type.
Definition types.c:299
c23_nodiscard infix_status infix_type_create_pointer_to(infix_arena_t *, infix_type **, infix_type *)
Creates a new pointer type that points to a specific type.
Definition types.c:268
c23_nodiscard infix_status infix_type_create_struct(infix_arena_t *, infix_type **, infix_struct_member *, size_t)
Creates a new struct type from an array of members, calculating layout automatically.
Definition types.c:490
c23_nodiscard infix_status infix_type_create_named_reference(infix_arena_t *, infix_type **, const char *, infix_aggregate_category_t)
Creates a placeholder for a named type to be resolved by a registry.
Definition types.c:620
c23_nodiscard infix_type * infix_type_create_primitive(infix_primitive_type_id)
Creates a static descriptor for a primitive C type.
Definition types.c:138
infix_struct_member infix_type_create_member(const char *, infix_type *, size_t)
A factory function to create an infix_struct_member.
Definition types.c:202
@ INFIX_PRIMITIVE_UINT16
Definition infix.h:183
@ INFIX_PRIMITIVE_UINT32
Definition infix.h:185
@ INFIX_PRIMITIVE_LONG_DOUBLE
Definition infix.h:193
@ INFIX_PRIMITIVE_FLOAT
Definition infix.h:191
@ INFIX_PRIMITIVE_DOUBLE
Definition infix.h:192
@ INFIX_PRIMITIVE_SINT16
Definition infix.h:184
@ INFIX_PRIMITIVE_SINT64
Definition infix.h:188
@ INFIX_PRIMITIVE_SINT32
Definition infix.h:186
@ INFIX_PRIMITIVE_UINT8
Definition infix.h:181
@ INFIX_PRIMITIVE_UINT128
Definition infix.h:189
@ INFIX_PRIMITIVE_BOOL
Definition infix.h:180
@ INFIX_PRIMITIVE_UINT64
Definition infix.h:187
@ INFIX_PRIMITIVE_SINT128
Definition infix.h:190
@ INFIX_PRIMITIVE_SINT8
Definition infix.h:182
@ INFIX_TYPE_UNION
Definition infix.h:166
@ INFIX_TYPE_PRIMITIVE
Definition infix.h:163
@ INFIX_TYPE_COMPLEX
Definition infix.h:170
@ INFIX_TYPE_ARRAY
Definition infix.h:167
@ INFIX_TYPE_VECTOR
Definition infix.h:171
@ INFIX_TYPE_VOID
Definition infix.h:173
@ INFIX_TYPE_POINTER
Definition infix.h:164
@ INFIX_TYPE_NAMED_REFERENCE
Definition infix.h:172
@ INFIX_TYPE_REVERSE_TRAMPOLINE
Definition infix.h:168
@ INFIX_TYPE_ENUM
Definition infix.h:169
@ INFIX_TYPE_STRUCT
Definition infix.h:165
@ INFIX_AGGREGATE_STRUCT
Definition infix.h:200
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:155
void _infix_type_recalculate_layout(infix_type *type)
Recalculates the layout of a fully resolved type graph.
Definition types.c:780
infix_type * _copy_type_graph_to_arena(infix_arena_t *, const infix_type *)
Performs a deep copy of a type graph into a destination arena.
Definition types.c:971
c23_nodiscard infix_status _infix_resolve_type_graph_inplace(infix_type **type_ptr, infix_registry_t *registry)
Resolves all named type references in a type graph in-place.
Definition type_registry.c:358
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:719
void _infix_clear_error(void)
Clears the thread-local error state.
Definition error.c:257
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:711
static void _print(printer_state *state, const char *fmt,...)
Definition signature.c:1175
static infix_struct_member * parse_aggregate_members(parser_state *state, char end_char, size_t *out_num_members)
Definition signature.c:260
static bool is_function_signature_ahead(const parser_state *state)
Definition signature.c:219
static void _infix_type_print_body_only_recursive(printer_state *state, const infix_type *type)
Definition signature.c:1398
c23_nodiscard infix_status _infix_type_print_body_only(char *buffer, size_t buffer_size, const infix_type *type, infix_print_dialect_t dialect)
An internal-only function to serialize a type's body without its registered name.
Definition signature.c:1526
#define MAX_RECURSION_DEPTH
Definition signature.c:51
static bool parse_size_t(parser_state *state, size_t *out_val)
Definition signature.c:109
c23_nodiscard infix_status _infix_parse_type_internal(infix_type **out_type, infix_arena_t **out_arena, const char *signature)
The internal core of the signature parser.
Definition signature.c:979
static void set_parser_error(parser_state *state, infix_error_code_t code)
Definition signature.c:81
static infix_type * parse_primitive(parser_state *state)
Definition signature.c:470
static infix_status parse_function_signature_details(parser_state *state, infix_type **out_ret_type, infix_function_argument **out_args, size_t *out_num_args, size_t *out_num_fixed_args)
Definition signature.c:812
static bool consume_keyword(parser_state *state, const char *keyword)
Definition signature.c:165
static infix_type * parse_type(parser_state *state)
Definition signature.c:603
static infix_type * parse_packed_struct(parser_state *state)
Definition signature.c:407
static infix_type * parse_aggregate(parser_state *state, char start_char, char end_char)
Definition signature.c:359
static const char * parse_identifier(parser_state *state)
Definition signature.c:130
static void _infix_type_print_signature_recursive(printer_state *state, const infix_type *type)
Definition signature.c:1206
static const char * parse_optional_name_prefix(parser_state *state)
Definition signature.c:188
INFIX_TLS const char * g_infix_last_signature_context
A thread-local pointer to the full signature string being parsed.
Definition error.c:89
static void skip_whitespace(parser_state *state)
Definition signature.c:90
A single entry in the registry's hash table.
Definition infix_internals.h:161
Internal definition of a memory arena.
Definition infix_internals.h:146
Describes a single argument to a C function.
Definition infix.h:287
Internal definition of a named type registry.
Definition infix_internals.h:175
_infix_registry_entry_t ** buckets
Definition infix_internals.h:180
size_t num_buckets
Definition infix_internals.h:178
Describes a single member of a C struct or union.
Definition infix.h:277
A semi-opaque structure that describes a C type.
Definition infix.h:211
Definition signature.c:58
const char * start
Definition signature.c:60
infix_arena_t * arena
Definition signature.c:61
int depth
Definition signature.c:62
const char * p
Definition signature.c:59
Definition signature.c:1161
char * p
Definition signature.c:1162
size_t remaining
Definition signature.c:1163
infix_status status
Definition signature.c:1164