Ruby 4.1.0dev (2026-03-06 revision d5d144c149d3beabbfb262e3994f60552469181b)
pm_constant_pool.h
Go to the documentation of this file.
1
10#ifndef PRISM_CONSTANT_POOL_H
11#define PRISM_CONSTANT_POOL_H
12
13#include "prism/defines.h"
14#include "prism/util/pm_arena.h"
15
16#include <assert.h>
17#include <stdbool.h>
18#include <stdint.h>
19#include <stdlib.h>
20#include <string.h>
21
27#define PM_CONSTANT_ID_UNSET 0
28
32typedef uint32_t pm_constant_id_t;
33
37typedef struct {
39 size_t size;
40
42 size_t capacity;
43
47
54
62void pm_constant_id_list_init_capacity(pm_arena_t *arena, pm_constant_id_list_t *list, size_t capacity);
63
72
81
90
95typedef unsigned int pm_constant_pool_bucket_type_t;
96
99
102
105
107typedef struct {
109 unsigned int id: 30;
110
113
115 uint32_t hash;
117
119typedef struct {
121 const uint8_t *start;
122
124 size_t length;
126
141
149bool pm_constant_pool_init(pm_constant_pool_t *pool, uint32_t capacity);
150
159
169pm_constant_id_t pm_constant_pool_find(const pm_constant_pool_t *pool, const uint8_t *start, size_t length);
170
180pm_constant_id_t pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const uint8_t *start, size_t length);
181
192pm_constant_id_t pm_constant_pool_insert_owned(pm_constant_pool_t *pool, uint8_t *start, size_t length);
193
203pm_constant_id_t pm_constant_pool_insert_constant(pm_constant_pool_t *pool, const uint8_t *start, size_t length);
204
211
212#endif
A bump allocator for the prism parser.
pm_constant_id_t pm_constant_pool_find(const pm_constant_pool_t *pool, const uint8_t *start, size_t length)
Find a constant in a constant pool.
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_DEFAULT
By default, each constant is a slice of the source.
void pm_constant_id_list_init_capacity(pm_arena_t *arena, pm_constant_id_list_t *list, size_t capacity)
Initialize a list of constant ids with a given capacity.
bool pm_constant_pool_init(pm_constant_pool_t *pool, uint32_t capacity)
Initialize a new constant pool with a given capacity.
pm_constant_id_t pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const uint8_t *start, size_t length)
Insert a constant into a constant pool that is a slice of a source string.
void pm_constant_id_list_init(pm_constant_id_list_t *list)
Initialize a list of constant ids.
unsigned int pm_constant_pool_bucket_type_t
The type of bucket in the constant pool hash map.
pm_constant_id_t pm_constant_pool_insert_constant(pm_constant_pool_t *pool, const uint8_t *start, size_t length)
Insert a constant into a constant pool from memory that is constant.
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
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.
void pm_constant_id_list_insert(pm_constant_id_list_t *list, size_t index, pm_constant_id_t id)
Insert a constant id into a list of constant ids at the specified index.
pm_constant_t * pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id)
Return a pointer to the constant indicated by the given constant id.
pm_constant_id_t pm_constant_pool_insert_owned(pm_constant_pool_t *pool, uint8_t *start, size_t length)
Insert a constant into a constant pool from memory that is now owned by the constant pool.
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_OWNED
An owned constant is one for which memory has been allocated.
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_CONSTANT
A constant constant is known at compile time.
bool pm_constant_id_list_includes(pm_constant_id_list_t *list, pm_constant_id_t id)
Checks if the current constant id list includes the given constant id.
void pm_constant_pool_free(pm_constant_pool_t *pool)
Free the memory associated with a constant pool.
Macro definitions used throughout the prism library.
C99 shim for <stdbool.h>
A bump allocator.
Definition pm_arena.h:39
A list of constant IDs.
size_t size
The number of constant ids in the list.
size_t capacity
The number of constant ids that have been allocated in the list.
pm_constant_id_t * ids
The constant ids in the list.
A bucket in the hash map.
uint32_t hash
The hash of the bucket.
unsigned int id
The incremental ID used for indexing back into the pool.
pm_constant_pool_bucket_type_t type
The type of the bucket, which determines how to free it.
The overall constant pool, which stores constants found while parsing.
uint32_t capacity
The number of buckets that have been allocated in the hash map.
pm_constant_pool_bucket_t * buckets
The buckets in the hash map.
uint32_t size
The number of buckets in the hash map.
pm_constant_t * constants
The constants that are stored in the buckets.
A constant in the pool which effectively stores a string.
size_t length
The length of the string.
const uint8_t * start
A pointer to the start of the string.