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

Implements the named type registry system. More...

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

Go to the source code of this file.

Classes

struct  resolve_memo_node_t
 
struct  _registry_parser_state_t
 

Macros

#define INITIAL_REGISTRY_BUCKETS   61
 

Typedefs

typedef struct resolve_memo_node_t resolve_memo_node_t
 

Functions

static uint64_t _registry_hash_string (const char *str)
 
static _infix_registry_entry_t_registry_lookup (infix_registry_t *registry, const char *name)
 
static _infix_registry_entry_t_registry_insert (infix_registry_t *registry, const char *name)
 
c23_nodiscard infix_registry_tinfix_registry_create (void)
 Creates a new, empty named type registry.
 
c23_nodiscard infix_registry_tinfix_registry_create_in_arena (infix_arena_t *arena)
 Creates a new, empty named type registry that allocates from a user-provided arena.
 
void infix_registry_destroy (infix_registry_t *registry)
 Destroys a type registry and frees all associated memory.
 
static infix_status _resolve_type_graph_inplace_recursive (infix_arena_t *temp_arena, infix_type **type_ptr, infix_registry_t *registry, resolve_memo_node_t **memo_head)
 
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.
 
static void _registry_parser_skip_whitespace (_registry_parser_state_t *state)
 
static char * _registry_parser_parse_name (_registry_parser_state_t *state, char *buffer, size_t buf_size)
 
c23_nodiscard infix_status infix_register_types (infix_registry_t *registry, const char *definitions)
 Parses a string of type definitions and adds them to a registry.
 
c23_nodiscard infix_registry_iterator_t infix_registry_iterator_begin (const infix_registry_t *registry)
 Initializes an iterator for traversing the types in a registry.
 
c23_nodiscard bool infix_registry_iterator_next (infix_registry_iterator_t *iter)
 Advances the iterator to the next defined type in the registry.
 
c23_nodiscard const char * infix_registry_iterator_get_name (const infix_registry_iterator_t *iter)
 Gets the name of the type at the iterator's current position.
 
c23_nodiscard const infix_typeinfix_registry_iterator_get_type (const infix_registry_iterator_t *iter)
 Gets the infix_type object of the type at the iterator's current position.
 
c23_nodiscard bool infix_registry_is_defined (const infix_registry_t *registry, const char *name)
 Checks if a type with the given name is fully defined in the registry.
 
c23_nodiscard const infix_typeinfix_registry_lookup_type (const infix_registry_t *registry, const char *name)
 Retrieves the canonical infix_type object for a given name from the registry.
 

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 named type registry system.

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 provides the functionality for defining, storing, and resolving complex C types by name. It is a critical component for handling recursive types (e.g., linked lists) and mutually-dependent types, making the signature string syntax much more powerful and manageable.

The registry is built on a hash table for efficient lookups. Its most important functions are:

  1. infix_register_types(): This function uses a robust three-pass algorithm to parse a string of definitions. This approach allows for out-of-order definitions and forward declarations, which is essential for complex type graphs.
  2. _infix_resolve_type_graph_inplace(): This internal function implements the **"Resolve"** stage of the library's core "Parse -> Copy -> Resolve -> Layout" data pipeline. It traverses a type graph and replaces all named placeholders (@MyType) with direct pointers to the canonical type objects stored in the registry.

Macro Definition Documentation

◆ INITIAL_REGISTRY_BUCKETS

#define INITIAL_REGISTRY_BUCKETS   61

Typedef Documentation

◆ resolve_memo_node_t

Function Documentation

◆ _infix_resolve_type_graph_inplace()

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.

Located in src/core/type_registry.c. This is the "Resolve" stage of the data pipeline. It traverses a type graph and replaces all INFIX_TYPE_NAMED_REFERENCE nodes (@Name) with direct pointers to the canonical infix_type objects from the registry.

Parameters
[in,out]type_ptrA pointer to the root of the type graph to resolve. The pointer may be changed.
[in]registryThe registry to use for lookups.
Returns
INFIX_SUCCESS on success, or an error if a name cannot be resolved.

◆ _registry_hash_string()

static uint64_t _registry_hash_string ( const char *  str)
static

◆ _registry_insert()

static _infix_registry_entry_t * _registry_insert ( infix_registry_t registry,
const char *  name 
)
static

◆ _registry_lookup()

static _infix_registry_entry_t * _registry_lookup ( infix_registry_t registry,
const char *  name 
)
static

◆ _registry_parser_parse_name()

static char * _registry_parser_parse_name ( _registry_parser_state_t state,
char *  buffer,
size_t  buf_size 
)
static

◆ _registry_parser_skip_whitespace()

static void _registry_parser_skip_whitespace ( _registry_parser_state_t state)
static

◆ _resolve_type_graph_inplace_recursive()

static infix_status _resolve_type_graph_inplace_recursive ( infix_arena_t temp_arena,
infix_type **  type_ptr,
infix_registry_t registry,
resolve_memo_node_t **  memo_head 
)
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.