Ruby 4.1.0dev (2026-04-04 revision 6ab9b22553ac802819aa1643a9ac9575e75d1286)
prism_compile.h (6ab9b22553ac802819aa1643a9ac9575e75d1286)
1#include "prism/prism.h"
2#include "ruby/encoding.h"
3
12typedef struct pm_local_index_struct {
13 int index, level;
15
16// A declaration for the struct that lives in compile.c.
17struct iseq_link_anchor;
18
27typedef struct {
29 int *values;
30
33
35 bool owned;
37
39#define PM_INDEX_LOOKUP_SPECIALS 4
40
42#define PM_SPECIAL_CONSTANT_FLAG ((pm_constant_id_t) (1 << 31))
43#define PM_INDEX_LOOKUP_SPECIAL_MULT 0
44#define PM_INDEX_LOOKUP_SPECIAL_POW 1
45#define PM_INDEX_LOOKUP_SPECIAL_AND 2
46#define PM_INDEX_LOOKUP_SPECIAL_DOT3 3
47
53#define PM_CONSTANT_MULT ((pm_constant_id_t) (PM_SPECIAL_CONSTANT_FLAG | PM_INDEX_LOOKUP_SPECIAL_MULT))
54#define PM_CONSTANT_POW ((pm_constant_id_t) (PM_SPECIAL_CONSTANT_FLAG | PM_INDEX_LOOKUP_SPECIAL_POW))
55#define PM_CONSTANT_AND ((pm_constant_id_t) (PM_SPECIAL_CONSTANT_FLAG | PM_INDEX_LOOKUP_SPECIAL_AND))
56#define PM_CONSTANT_DOT3 ((pm_constant_id_t) (PM_SPECIAL_CONSTANT_FLAG | PM_INDEX_LOOKUP_SPECIAL_DOT3))
57
58static inline int
59pm_index_lookup_table_index(const pm_index_lookup_table_t *table, pm_constant_id_t key)
60{
61 if (LIKELY(!(key & PM_SPECIAL_CONSTANT_FLAG))) {
62 return (int) key - 1;
63 }
64 return table->capacity - PM_INDEX_LOOKUP_SPECIALS + (int)(key & ~PM_SPECIAL_CONSTANT_FLAG);
65}
66
67static inline void
68pm_index_lookup_table_insert(pm_index_lookup_table_t *table, pm_constant_id_t key, int value)
69{
70 int idx = pm_index_lookup_table_index(table, key);
71 RUBY_ASSERT(idx >= 0 && idx < table->capacity);
72 table->values[idx] = value;
73}
74
75static inline int
76pm_index_lookup_table_lookup(const pm_index_lookup_table_t *table, pm_constant_id_t key, int *value)
77{
78 int idx = pm_index_lookup_table_index(table, key);
79 RUBY_ASSERT(idx >= 0 && idx < table->capacity);
80 if (table->values[idx] == -1) return 0;
81 *value = table->values[idx];
82 return 1;
83}
84
85static inline void
86pm_index_lookup_table_init_heap(pm_index_lookup_table_t *table, int constants_size)
87{
88 int cap = constants_size + PM_INDEX_LOOKUP_SPECIALS;
89 table->values = (int *) ruby_xmalloc(cap * sizeof(int));
90 memset(table->values, -1, cap * sizeof(int));
91 table->capacity = cap;
92 table->owned = true;
93}
94
95// ScopeNodes are helper nodes, and will never be part of the AST. We manually
96// declare them here to avoid generating them.
97typedef struct pm_scope_node {
98 pm_node_t base;
99 struct pm_scope_node *previous;
100 pm_node_t *ast_node;
101 pm_node_t *parameters;
102 pm_node_t *body;
104
105 const pm_parser_t *parser;
106 const pm_options_t *options;
107 const pm_line_offset_list_t *line_offsets;
108 int32_t start_line;
109 rb_encoding *encoding;
110
120
127
128 // The size of the local table on the iseq which includes locals and hidden
129 // variables.
130 int local_table_for_iseq_size;
131
132 ID *constants;
133
141
142 // The current coverage setting, passed down through the various scopes.
143 int coverage_enabled;
144
150
156 size_t last_line;
158
159void pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous);
160void pm_scope_node_destroy(pm_scope_node_t *scope_node);
161
181
182void pm_parse_result_init(pm_parse_result_t *result);
183VALUE pm_load_file(pm_parse_result_t *result, VALUE filepath, bool load_error);
184VALUE pm_parse_file(pm_parse_result_t *result, VALUE filepath, VALUE *script_lines);
185VALUE pm_load_parse_file(pm_parse_result_t *result, VALUE filepath, VALUE *script_lines);
186VALUE pm_parse_string(pm_parse_result_t *result, VALUE source, VALUE filepath, VALUE *script_lines);
187VALUE pm_parse_stdin(pm_parse_result_t *result);
188void pm_options_version_for_current_ruby_set(pm_options_t *options);
189void pm_parse_result_free(pm_parse_result_t *result);
190
191rb_iseq_t *pm_iseq_new(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent, enum rb_iseq_type, int *error_state);
192rb_iseq_t *pm_iseq_new_top(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent, int *error_state);
193rb_iseq_t *pm_iseq_new_main(pm_scope_node_t *node, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt, int *error_state);
194rb_iseq_t *pm_iseq_new_eval(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth, int *error_state);
195rb_iseq_t *pm_iseq_new_with_opt(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth, enum rb_iseq_type, const rb_compile_option_t *option, int *error_state);
196rb_iseq_t *pm_iseq_build(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth, enum rb_iseq_type, const rb_compile_option_t *option);
197
198VALUE pm_iseq_compile_node(rb_iseq_t *iseq, pm_scope_node_t *node);
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
Encoding relates APIs.
The main header file for the prism parser.
A list of constant IDs.
A direct-indexed lookup table mapping constant IDs to local variable indices.
int capacity
Total number of slots (constants_size + PM_INDEX_LOOKUP_SPECIALS).
bool owned
Whether the values array is heap-allocated and needs explicit free.
int * values
Array of local indices, indexed by constant_id.
A list of offsets of the start of lines in a string.
the getlocal and setlocal instructions require two parameters.
This is the base structure that represents a node in the syntax tree.
Definition ast.h:1065
bool parsed
Whether or not this parse result has performed its parsing yet.
pm_parser_t * parser
The parser that will do the actual parsing.
pm_source_t * source
The source backing the parse (file, string, or stream).
pm_scope_node_t node
The resulting scope node that will hold the generated AST.
pm_options_t * options
The options that will be passed to the parser.
pm_arena_t * arena
The arena allocator for AST-lifetime memory.
rb_encoding * filepath_encoding
This is the encoding of the actual filepath object that will be used when a FILE node is compiled or ...
pm_index_lookup_table_t index_lookup_table
A flat lookup table mapping constant IDs (or special IDs) to local variable indices.
size_t last_line
Cached line hint for line offset list lookups.
struct iseq_link_anchor * pre_execution_anchor
This will only be set on the top-level scope node.
VALUE * script_lines
This is a pointer to the list of script lines for the ISEQs that will be associated with this scope n...
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40