summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Valentine-House <matt@eightbitraptor.com>2024-01-15 21:32:17 +0000
committerMatt Valentine-House <matt@eightbitraptor.com>2024-01-16 14:58:55 +0000
commitda383c0d7496b7e65fae8da4b26ad3265175bcc8 (patch)
tree4742d7cb3c67b324a6bd487b0154a42960a2681d
parentf4b299a1ed036234c0144797acba555c9feb3c6e (diff)
Return pm_local_index_t when looking up local indexes
instead of returning the index and updating found_depth in the parent scope
-rw-r--r--prism_compile.c28
1 files changed, 16 insertions, 12 deletions
diff --git a/prism_compile.c b/prism_compile.c
index 9d92017beb..91b21be4c0 100644
--- a/prism_compile.c
+++ b/prism_compile.c
@@ -791,9 +791,14 @@ pm_interpolated_node_compile(pm_node_list_t *parts, rb_iseq_t *iseq, NODE dummy_
// This recurses through scopes and finds the local index at any scope level
// It also takes a pointer to depth, and increments depth appropriately
// according to the depth of the local
-static int
+static pm_local_index_t
pm_lookup_local_index_any_scope(rb_iseq_t *iseq, pm_scope_node_t *scope_node, pm_constant_id_t constant_id, int *found_depth)
{
+ int level = 0;
+ pm_local_index_t lindex = {0};
+ if (found_depth) {
+ level = *found_depth;
+ }
if (!scope_node) {
// We have recursed up all scope nodes
// and have not found the local yet
@@ -805,28 +810,27 @@ pm_lookup_local_index_any_scope(rb_iseq_t *iseq, pm_scope_node_t *scope_node, pm
if (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
// Local does not exist at this level, continue recursing up
if (found_depth) {
+ level++;
(*found_depth)++;
+ RUBY_ASSERT(level == *found_depth);
}
return pm_lookup_local_index_any_scope(iseq, scope_node->previous, constant_id, found_depth);
}
- return scope_node->local_table_for_iseq_size - (int)local_index;
+ lindex.level = level;
+ lindex.index = scope_node->local_table_for_iseq_size - (int)local_index;
+ return lindex;
}
static pm_local_index_t
pm_lookup_local_index_with_depth(rb_iseq_t *iseq, pm_scope_node_t *scope_node, pm_constant_id_t constant_id, uint32_t depth)
{
- pm_local_index_t lindex = {0};
-
for(uint32_t i = 0; i < depth; i++) {
scope_node = scope_node->previous;
iseq = (rb_iseq_t *)ISEQ_BODY(iseq)->parent_iseq;
}
- lindex.level = (int)depth;
- lindex.index = pm_lookup_local_index_any_scope(iseq, scope_node, constant_id, NULL);
-
- return lindex;
+ return pm_lookup_local_index_any_scope(iseq, scope_node, constant_id, (int *)&depth);
}
// This returns the CRuby ID which maps to the pm_constant_id_t
@@ -4975,9 +4979,9 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
pm_constant_id_t constant_id = local_write_node->name;
int found_depth = 0;
- int index = pm_lookup_local_index_any_scope(iseq, scope_node, constant_id, &found_depth);
+ pm_local_index_t index = pm_lookup_local_index_any_scope(iseq, scope_node, constant_id, &found_depth);
- ADD_SETLOCAL(ret, &dummy_line_node, index, found_depth);
+ ADD_SETLOCAL(ret, &dummy_line_node, index.index, found_depth);
return;
}
case PM_LOCAL_VARIABLE_WRITE_NODE: {
@@ -4989,9 +4993,9 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
pm_constant_id_t constant_id = local_write_node->name;
int found_depth = 0;
- int index = pm_lookup_local_index_any_scope(iseq, scope_node, constant_id, &found_depth);
+ pm_local_index_t index = pm_lookup_local_index_any_scope(iseq, scope_node, constant_id, &found_depth);
- ADD_SETLOCAL(ret, &dummy_line_node, index, found_depth);
+ ADD_SETLOCAL(ret, &dummy_line_node, index.index, found_depth);
return;
}
case PM_MATCH_LAST_LINE_NODE: {