infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
signature.c File Reference

Implements the infix signature string parser and type printer. More...

#include "common/infix_internals.h"
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for signature.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  parser_state
 
struct  printer_state
 

Macros

#define MAX_RECURSION_DEPTH   32
 

Functions

static infix_typeparse_type (parser_state *state)
 
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)
 
static void set_parser_error (parser_state *state, infix_error_code_t code)
 
static void skip_whitespace (parser_state *state)
 
static bool parse_size_t (parser_state *state, size_t *out_val)
 
static const char * parse_identifier (parser_state *state)
 
static bool consume_keyword (parser_state *state, const char *keyword)
 
static const char * parse_optional_name_prefix (parser_state *state)
 
static bool is_function_signature_ahead (const parser_state *state)
 
static infix_struct_memberparse_aggregate_members (parser_state *state, char end_char, size_t *out_num_members)
 
static infix_typeparse_aggregate (parser_state *state, char start_char, char end_char)
 
static infix_typeparse_packed_struct (parser_state *state)
 
static infix_typeparse_primitive (parser_state *state)
 
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.
 
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.
 
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.
 
static void _print (printer_state *state, const char *fmt,...)
 
static void _infix_type_print_signature_recursive (printer_state *state, const infix_type *type)
 
static void _infix_type_print_body_only_recursive (printer_state *state, const infix_type *type)
 
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.
 
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.
 
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.
 
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.
 

Variables

INFIX_TLS const char * g_infix_last_signature_context
 A thread-local pointer to the full signature string being parsed.
 

Detailed Description

Implements the infix signature string parser and type printer.

Copyright (c) 2025 Sanko Robinson

This source code is dual-licensed under the Artistic License 2.0 or the MIT License. You may choose to use this code under the terms of either license.

SPDX-License-Identifier: (Artistic-2.0 OR MIT)

The documentation blocks within this file are licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0).

SPDX-License-Identifier: CC-BY-4.0

This module is responsible for two key functionalities that form the user-facing API of the library:

  1. Parsing: It contains a hand-written recursive descent parser that transforms a human-readable signature string (e.g., "({int, *char}) -> void") into an unresolved infix_type object graph. This is the **"Parse"** stage of the core data pipeline. The internal entry point for this stage is _infix_parse_type_internal.
  2. Printing: It provides functions to serialize a fully resolved infix_type graph back into a canonical signature string. This is crucial for introspection, debugging, and verifying the library's understanding of a type.

The public functions infix_type_from_signature and infix_signature_parse are high-level orchestrators. They manage the entire **"Parse -> Copy -> Resolve -> Layout"** pipeline, providing the user with a fully validated, self-contained, and ready-to-use type object that is safe to use for the lifetime of its returned arena.

Macro Definition Documentation

◆ MAX_RECURSION_DEPTH

#define MAX_RECURSION_DEPTH   32

Function Documentation

◆ _infix_parse_type_internal()

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.

Located in src/core/signature.c. This is the "Parse" stage of the data pipeline. It takes a signature string and produces a raw, unresolved infix_type graph in a new, temporary arena. It does not perform any copying, resolution, or layout calculation.

Parameters
[out]out_typeOn success, receives the parsed type graph.
[out]out_arenaOn success, receives the temporary arena holding the graph.
[in]signatureThe signature string to parse.
Returns
INFIX_SUCCESS on success.

◆ _infix_type_print_body_only()

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.

Located in src/core/signature.c. Unlike infix_type_print, which will print @Name for a named struct, this function will always print the full {...} body. This is essential for infix_registry_print to function correctly.

Parameters
[out]bufferThe output buffer.
[in]buffer_sizeThe size of the buffer.
[in]typeThe type to print.
[in]dialectThe output format dialect.
Returns
INFIX_SUCCESS on success.

◆ _infix_type_print_body_only_recursive()

static void _infix_type_print_body_only_recursive ( printer_state state,
const infix_type type 
)
static

◆ _infix_type_print_signature_recursive()

static void _infix_type_print_signature_recursive ( printer_state state,
const infix_type type 
)
static

◆ _print()

static void _print ( printer_state state,
const char *  fmt,
  ... 
)
static

◆ consume_keyword()

static bool consume_keyword ( parser_state state,
const char *  keyword 
)
static

◆ is_function_signature_ahead()

static bool is_function_signature_ahead ( const parser_state state)
static

◆ parse_aggregate()

static infix_type * parse_aggregate ( parser_state state,
char  start_char,
char  end_char 
)
static

◆ parse_aggregate_members()

static infix_struct_member * parse_aggregate_members ( parser_state state,
char  end_char,
size_t *  out_num_members 
)
static

◆ parse_function_signature_details()

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 
)
static

◆ parse_identifier()

static const char * parse_identifier ( parser_state state)
static

◆ parse_optional_name_prefix()

static const char * parse_optional_name_prefix ( parser_state state)
static

◆ parse_packed_struct()

static infix_type * parse_packed_struct ( parser_state state)
static

◆ parse_primitive()

static infix_type * parse_primitive ( parser_state state)
static

◆ parse_size_t()

static bool parse_size_t ( parser_state state,
size_t *  out_val 
)
static

◆ parse_type()

static infix_type * parse_type ( parser_state state)
static

◆ set_parser_error()

static void set_parser_error ( parser_state state,
infix_error_code_t  code 
)
static

◆ skip_whitespace()

static void skip_whitespace ( parser_state state)
static

Variable Documentation

◆ g_infix_last_signature_context

INFIX_TLS const char* g_infix_last_signature_context
extern

A thread-local pointer to the full signature string being parsed.

This is set by the high-level API functions (infix_type_from_signature, etc.) before parsing begins. If a parser error occurs, _infix_set_error uses this context to generate a rich, contextual error message.