diff options
author | Kevin Newton <kddnewton@gmail.com> | 2023-12-08 09:57:31 -0500 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2023-12-11 15:32:31 +0000 |
commit | b673b5b4329d020b19907142f291c8ecd69e95e0 (patch) | |
tree | ab10e325b99c101f6538d14924057e57f0090b47 /prism | |
parent | c69d1367a7994c70f956fc6486568823e8fa5812 (diff) |
[ruby/prism] Split up CallNode in target position
In this commit we're splitting up the call nodes that were in target
positions (that is, for loop indices, rescue error captures, and
multi assign targets).
Previously, we would simply leave the call nodes in place. This had
the benefit of keeping the AST relatively simple, but had the
downside of not being very explicit. If a static analysis tool wanted
to only look at call nodes, it could easily be confused because the
method would have 1 fewer argument than it would actually be called
with.
This also brings some consistency to the AST. All of the nodes in
a target position are now *TargetNode nodes. These should all be
treated the same, and the call nodes can now be treated the same.
Finally, there is benefit to memory. Because being in a target
position ensures we don't have some fields, we can strip down the
number of fields on these nodes.
So this commit introduces two new nodes: CallTargetNode and
IndexTargetNode. For CallTargetNode we get to drop the opening_loc,
closing_loc, arguments, and block. Those can never be present. We
also get to mark their fields as non-null, so they will always be
seen as present.
The IndexTargetNode keeps around most of its fields but gets to
drop both the name (because it will always be []=) and the
message_loc (which was always super confusing because it included
the arguments by virtue of being inside the []).
Overall, this adds complexity to the AST at the expense of memory
savings and explicitness. I believe this tradeoff is worth it in
this case, especially because these are very much not common nodes
in the first place.
https://github.com/ruby/prism/commit/3ef71cdb45
Diffstat (limited to 'prism')
-rw-r--r-- | prism/config.yml | 55 | ||||
-rw-r--r-- | prism/prism.c | 71 |
2 files changed, 115 insertions, 11 deletions
diff --git a/prism/config.yml b/prism/config.yml index 50299141bd..5f20c80f16 100644 --- a/prism/config.yml +++ b/prism/config.yml @@ -790,6 +790,32 @@ nodes: foo.bar ||= value ^^^^^^^^^^^^^^^^^ + - name: CallTargetNode + fields: + - name: flags + type: flags + kind: CallNodeFlags + - name: receiver + type: node + - name: call_operator_loc + type: location + - name: name + type: constant + - name: message_loc + type: location + comment: | + Represents assigning to a method call. + + foo.bar, = 1 + ^^^^^^^ + + begin + rescue => foo.bar + ^^^^^^^ + end + + for foo.bar in baz do end + ^^^^^^^ - name: CapturePatternNode fields: - name: value @@ -1596,6 +1622,35 @@ nodes: foo.bar[baz] ||= value ^^^^^^^^^^^^^^^^^^^^^^ + - name: IndexTargetNode + fields: + - name: flags + type: flags + kind: CallNodeFlags + - name: receiver + type: node + - name: opening_loc + type: location + - name: arguments + type: node? + kind: ArgumentsNode + - name: closing_loc + type: location + - name: block + type: node? + comment: | + Represents assigning to an index. + + foo[bar], = 1 + ^^^^^^^^ + + begin + rescue => foo[bar] + ^^^^^^^^ + end + + for foo[bar] in baz do end + ^^^^^^^^ - name: InstanceVariableAndWriteNode fields: - name: name diff --git a/prism/prism.c b/prism/prism.c index 1b5d82b4bb..747e1bb978 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -2112,6 +2112,63 @@ pm_index_or_write_node_create(pm_parser_t *parser, pm_call_node_t *target, const } /** + * Allocate and initialize a new CallTargetNode node from an existing call + * node. + */ +static pm_call_target_node_t * +pm_call_target_node_create(pm_parser_t *parser, pm_call_node_t *target) { + pm_call_target_node_t *node = PM_ALLOC_NODE(parser, pm_call_target_node_t); + + *node = (pm_call_target_node_t) { + { + .type = PM_CALL_TARGET_NODE, + .flags = target->base.flags, + .location = target->base.location + }, + .receiver = target->receiver, + .call_operator_loc = target->call_operator_loc, + .name = target->name, + .message_loc = target->message_loc + }; + + // Here we're going to free the target, since it is no longer necessary. + // However, we don't want to call `pm_node_destroy` because we want to keep + // around all of its children since we just reused them. + free(target); + + return node; +} + +/** + * Allocate and initialize a new IndexTargetNode node from an existing call + * node. + */ +static pm_index_target_node_t * +pm_index_target_node_create(pm_parser_t *parser, pm_call_node_t *target) { + pm_index_target_node_t *node = PM_ALLOC_NODE(parser, pm_index_target_node_t); + + *node = (pm_index_target_node_t) { + { + .type = PM_INDEX_TARGET_NODE, + .flags = target->base.flags, + .location = target->base.location + }, + .receiver = target->receiver, + .opening_loc = target->opening_loc, + .arguments = target->arguments, + .closing_loc = target->closing_loc, + .block = target->block + }; + + // Here we're going to free the target, since it is no longer necessary. + // However, we don't want to call `pm_node_destroy` because we want to keep + // around all of its children since we just reused them. + free(target); + + return node; +} + +/** * Allocate and initialize a new CapturePatternNode node. */ static pm_capture_pattern_node_t * @@ -10618,23 +10675,15 @@ parse_target(pm_parser_t *parser, pm_node_t *target) { if (*call->message_loc.start == '_' || parser->encoding->alnum_char(call->message_loc.start, call->message_loc.end - call->message_loc.start)) { parse_write_name(parser, &call->name); - return (pm_node_t *) call; + return (pm_node_t *) pm_call_target_node_create(parser, call); } } // If there is no call operator and the message is "[]" then this is // an aref expression, and we can transform it into an aset // expression. - if ( - (call->call_operator_loc.start == NULL) && - (call->message_loc.start != NULL) && - (call->message_loc.start[0] == '[') && - (call->message_loc.end[-1] == ']') && - (call->block == NULL) - ) { - // Replace the name with "[]=". - call->name = pm_parser_constant_id_constant(parser, "[]=", 3); - return target; + if (pm_call_node_index_p(call)) { + return (pm_node_t *) pm_index_target_node_create(parser, call); } } /* fallthrough */ |