Ruby 4.1.0dev (2026-11-18 revision 9de06831e37534f8a163e598cc0c6903c7687430)
constant_pool.h File Reference

(9de06831e37534f8a163e598cc0c6903c7687430)

A data structure that stores a set of strings. More...

#include "prism/compiler/exported.h"
#include "prism/compiler/inline.h"
#include "prism/compiler/nodiscard.h"
#include "prism/compiler/nonnull.h"
#include "prism/arena.h"
#include <stddef.h>
#include <stdint.h>
Include dependency graph for constant_pool.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pm_constant_id_list_t
 A list of constant IDs. More...

Typedefs

typedef uint32_t pm_constant_id_t
 A constant id is a unique identifier for a constant in the constant pool.
typedef struct pm_constant_t pm_constant_t
 A constant in the pool which effectively stores a string.
typedef struct pm_constant_pool_t pm_constant_pool_t
 The overall constant pool, which stores constants found while parsing.

Functions

static PRISM_INLINE size_t pm_constant_id_index (pm_constant_id_t constant_id)
 Returns the index of the constant with the given id among the constants of the parser that produced it.
PRISM_EXPORTED_FUNCTION const uint8_t * pm_constant_start (const pm_constant_t *constant) PRISM_NONNULL(1)
 Return a raw pointer to the start of a constant.
PRISM_EXPORTED_FUNCTION size_t pm_constant_length (const pm_constant_t *constant) PRISM_NONNULL(1)
 Return the length of a constant.
PRISM_EXPORTED_FUNCTION void pm_constant_id_list_init (pm_arena_t *arena, pm_constant_id_list_t *list, uint32_t size) PRISM_NONNULL(1
 Initialize a list of constant ids.
PRISM_EXPORTED_FUNCTION void PRISM_EXPORTED_FUNCTION void pm_constant_id_list_append (pm_arena_t *arena, pm_constant_id_list_t *list, pm_constant_id_t id) PRISM_NONNULL(1
 Append a constant id to a list of constant ids.

Detailed Description

A data structure that stores a set of strings.

Each string is assigned a unique id, which can be used to compare strings for equality. This comparison ends up being much faster than strcmp, since it only requires a single integer comparison.

Definition in file constant_pool.h.

Typedef Documentation

◆ pm_constant_id_t

typedef uint32_t pm_constant_id_t

A constant id is a unique identifier for a constant in the constant pool.

Definition at line 30 of file constant_pool.h.

◆ pm_constant_pool_t

typedef struct pm_constant_pool_t pm_constant_pool_t

The overall constant pool, which stores constants found while parsing.

Definition at line 69 of file constant_pool.h.

◆ pm_constant_t

typedef struct pm_constant_t pm_constant_t

A constant in the pool which effectively stores a string.

Definition at line 64 of file constant_pool.h.

Function Documentation

◆ pm_constant_id_index()

PRISM_INLINE size_t pm_constant_id_index ( pm_constant_id_t constant_id)
static

Returns the index of the constant with the given id among the constants of the parser that produced it.

The indices run from 0 to one less than pm_parser_constants_size, so they can index a cache with one entry per constant that was filled using pm_parser_constant_at.

Parameters
constant_idthe id of the constant. Optional constant fields on nodes hold 0 when they are absent, which is not a valid id, so check for it before calling this function.
Returns
the index of the constant

Definition at line 44 of file constant_pool.h.

◆ pm_constant_id_list_append()

PRISM_EXPORTED_FUNCTION void PRISM_EXPORTED_FUNCTION void pm_constant_id_list_append ( pm_arena_t * arena,
pm_constant_id_list_t * list,
pm_constant_id_t id )

Append a constant id to a list of constant ids.

A list's storage is always exactly its size, so it is regrown to exactly one more id by allocating from the given arena, which must live as long as the list does. For a list in a parsed tree, that is the arena that was passed to pm_parser_new.

Parameters
arenaThe arena to use for allocations.
listThe list to append to.
idThe constant id to append.

◆ pm_constant_id_list_init()

PRISM_EXPORTED_FUNCTION void pm_constant_id_list_init ( pm_arena_t * arena,
pm_constant_id_list_t * list,
uint32_t size )

Initialize a list of constant ids.

Parameters
arenaThe arena to use for allocations.
listThe list to initialize.
sizeThe size of the list.

◆ pm_constant_length()

PRISM_EXPORTED_FUNCTION size_t pm_constant_length ( const pm_constant_t * constant)

Return the length of a constant.

Parameters
constantThe constant to get the length of.
Returns
The length of the constant.

Definition at line 408 of file constant_pool.c.

◆ pm_constant_start()

PRISM_EXPORTED_FUNCTION const uint8_t * pm_constant_start ( const pm_constant_t * constant)

Return a raw pointer to the start of a constant.

Parameters
constantThe constant to get the start of.
Returns
A raw pointer to the start of the constant.

Definition at line 401 of file constant_pool.c.