[ruby/yarp] Add class variables to the constant pool

https://github.com/ruby/yarp/commit/be5cb60c83
This commit is contained in:
Kevin Newton 2023-08-30 15:28:10 -04:00 committed by git
parent 151e94fee5
commit 00dbee94ac
18 changed files with 81 additions and 41 deletions

View File

@ -8,7 +8,7 @@ module YARP
# #
# @@foo && @@foo = bar # @@foo && @@foo = bar
def visit_class_variable_and_write_node(node) def visit_class_variable_and_write_node(node)
desugar_and_write_node(node, ClassVariableReadNode, ClassVariableWriteNode) desugar_and_write_node(node, ClassVariableReadNode, ClassVariableWriteNode, arguments: [node.name])
end end
# @@foo ||= bar # @@foo ||= bar
@ -17,7 +17,7 @@ module YARP
# #
# defined?(@@foo) ? @@foo : @@foo = bar # defined?(@@foo) ? @@foo : @@foo = bar
def visit_class_variable_or_write_node(node) def visit_class_variable_or_write_node(node)
desugar_or_write_defined_node(node, ClassVariableReadNode, ClassVariableWriteNode) desugar_or_write_defined_node(node, ClassVariableReadNode, ClassVariableWriteNode, arguments: [node.name])
end end
# @@foo += bar # @@foo += bar
@ -26,7 +26,7 @@ module YARP
# #
# @@foo = @@foo + bar # @@foo = @@foo + bar
def visit_class_variable_operator_write_node(node) def visit_class_variable_operator_write_node(node)
desugar_operator_write_node(node, ClassVariableReadNode, ClassVariableWriteNode) desugar_operator_write_node(node, ClassVariableReadNode, ClassVariableWriteNode, arguments: [node.name])
end end
# Foo &&= bar # Foo &&= bar
@ -245,15 +245,15 @@ module YARP
end end
# Don't desugar `x ||= y` to `defined?(x) ? x : x = y` # Don't desugar `x ||= y` to `defined?(x) ? x : x = y`
def desugar_or_write_defined_node(node, read_class, write_class) def desugar_or_write_defined_node(node, read_class, write_class, arguments: [])
IfNode.new( IfNode.new(
node.operator_loc, node.operator_loc,
DefinedNode.new(nil, read_class.new(node.name_loc), nil, node.operator_loc, node.name_loc), DefinedNode.new(nil, read_class.new(*arguments, node.name_loc), nil, node.operator_loc, node.name_loc),
StatementsNode.new([read_class.new(node.name_loc)], node.location), StatementsNode.new([read_class.new(*arguments, node.name_loc)], node.location),
ElseNode.new( ElseNode.new(
node.operator_loc, node.operator_loc,
StatementsNode.new( StatementsNode.new(
[write_class.new(node.name_loc, node.value, node.operator_loc, node.location)], [write_class.new(*arguments, node.name_loc, node.value, node.operator_loc, node.location)],
node.location node.location
), ),
node.operator_loc, node.operator_loc,

View File

@ -910,7 +910,7 @@ ProgramNode(0...1194)(
), ),
DefNode(811...826)( DefNode(811...826)(
(821...822), (821...822),
ClassVariableReadNode(815...820)(), ClassVariableReadNode(815...820)(:@@var),
nil, nil,
nil, nil,
[], [],

View File

@ -941,7 +941,7 @@ ProgramNode(0...3743)(
"foo" "foo"
), ),
PinnedVariableNode(974...980)( PinnedVariableNode(974...980)(
ClassVariableReadNode(975...980)(), ClassVariableReadNode(975...980)(:@@bar),
(974...975) (974...975)
), ),
(971...973) (971...973)

View File

@ -502,7 +502,7 @@ ProgramNode(0...747)(
(633...634) (633...634)
), ),
PinnedVariableNode(638...642)( PinnedVariableNode(638...642)(
ClassVariableReadNode(639...642)(), ClassVariableReadNode(639...642)(:@@c),
(638...639) (638...639)
)], )],
nil, nil,

View File

@ -20,7 +20,7 @@ ProgramNode(0...498)(
(122...123), (122...123),
[EmbeddedVariableNode(123...129)( [EmbeddedVariableNode(123...129)(
(123...124), (123...124),
ClassVariableReadNode(124...129)() ClassVariableReadNode(124...129)(:@@foo)
)], )],
(129...130) (129...130)
), ),

View File

@ -54,8 +54,8 @@ ProgramNode(0...704)(
(54...55) (54...55)
), ),
MultiWriteNode(65...84)( MultiWriteNode(65...84)(
[ClassVariableTargetNode(66...69)(), [ClassVariableTargetNode(66...69)(:@@a),
ClassVariableTargetNode(71...74)()], ClassVariableTargetNode(71...74)(:@@b)],
(76...77), (76...77),
ArrayNode(78...84)( ArrayNode(78...84)(
[IntegerNode(79...80)(), IntegerNode(82...83)()], [IntegerNode(79...80)(), IntegerNode(82...83)()],
@ -291,6 +291,7 @@ ProgramNode(0...704)(
) )
), ),
ClassVariableWriteNode(302...309)( ClassVariableWriteNode(302...309)(
:@@a,
(302...305), (302...305),
IntegerNode(308...309)(), IntegerNode(308...309)(),
(306...307) (306...307)

View File

@ -84,7 +84,7 @@ ProgramNode(0...299)(
[StringNode(167...168)(nil, (167...168), nil, "a"), [StringNode(167...168)(nil, (167...168), nil, "a"),
EmbeddedVariableNode(168...172)( EmbeddedVariableNode(168...172)(
(168...169), (168...169),
ClassVariableReadNode(169...172)() ClassVariableReadNode(169...172)(:@@a)
)], )],
(172...173) (172...173)
), ),

View File

@ -158,7 +158,7 @@ ProgramNode(0...916)(
StringNode(210...211)(nil, (210...211), nil, " "), StringNode(210...211)(nil, (210...211), nil, " "),
EmbeddedVariableNode(211...215)( EmbeddedVariableNode(211...215)(
(211...212), (211...212),
ClassVariableReadNode(212...215)() ClassVariableReadNode(212...215)(:@@a)
), ),
StringNode(215...216)(nil, (215...216), nil, " "), StringNode(215...216)(nil, (215...216), nil, " "),
EmbeddedVariableNode(216...219)( EmbeddedVariableNode(216...219)(

View File

@ -3,7 +3,7 @@ ProgramNode(0...66)(
StatementsNode(0...66)( StatementsNode(0...66)(
[CallNode(0...1)(nil, nil, (0...1), nil, nil, nil, nil, 2, "a"), [CallNode(0...1)(nil, nil, (0...1), nil, nil, nil, nil, 2, "a"),
InstanceVariableReadNode(2...4)(:@a), InstanceVariableReadNode(2...4)(:@a),
ClassVariableReadNode(5...8)(), ClassVariableReadNode(5...8)(:@@a),
GlobalVariableReadNode(9...11)(), GlobalVariableReadNode(9...11)(),
NumberedReferenceReadNode(12...14)(), NumberedReferenceReadNode(12...14)(),
BackReferenceReadNode(15...17)(), BackReferenceReadNode(15...17)(),

View File

@ -246,7 +246,7 @@ ProgramNode(0...608)(
[StringNode(598...599)(nil, (598...599), nil, "a"), [StringNode(598...599)(nil, (598...599), nil, "a"),
EmbeddedVariableNode(599...603)( EmbeddedVariableNode(599...603)(
(599...600), (599...600),
ClassVariableReadNode(600...603)() ClassVariableReadNode(600...603)(:@@a)
)], )],
(603...604) (603...604)
), ),

View File

@ -1,21 +1,23 @@
ProgramNode(0...293)( ProgramNode(0...293)(
[:abc, :foo, :bar, :baz], [:abc, :foo, :bar, :baz],
StatementsNode(0...293)( StatementsNode(0...293)(
[ClassVariableReadNode(0...5)(), [ClassVariableReadNode(0...5)(:@@abc),
ClassVariableWriteNode(7...16)( ClassVariableWriteNode(7...16)(
:@@abc,
(7...12), (7...12),
IntegerNode(15...16)(), IntegerNode(15...16)(),
(13...14) (13...14)
), ),
MultiWriteNode(18...34)( MultiWriteNode(18...34)(
[ClassVariableTargetNode(18...23)(), [ClassVariableTargetNode(18...23)(:@@foo),
ClassVariableTargetNode(25...30)()], ClassVariableTargetNode(25...30)(:@@bar)],
(31...32), (31...32),
IntegerNode(33...34)(), IntegerNode(33...34)(),
nil, nil,
nil nil
), ),
ClassVariableWriteNode(36...48)( ClassVariableWriteNode(36...48)(
:@@foo,
(36...41), (36...41),
ArrayNode(44...48)( ArrayNode(44...48)(
[IntegerNode(44...45)(), IntegerNode(47...48)()], [IntegerNode(44...45)(), IntegerNode(47...48)()],

View File

@ -1 +1,4 @@
ProgramNode(0...5)([], StatementsNode(0...5)([ClassVariableReadNode(0...5)()])) ProgramNode(0...5)(
[],
StatementsNode(0...5)([ClassVariableReadNode(0...5)(:@@foo)])
)

View File

@ -1,6 +1,11 @@
ProgramNode(0...10)( ProgramNode(0...10)(
[], [],
StatementsNode(0...10)( StatementsNode(0...10)(
[ClassVariableWriteNode(0...10)((0...5), IntegerNode(8...10)(), (6...7))] [ClassVariableWriteNode(0...10)(
:@@var,
(0...5),
IntegerNode(8...10)(),
(6...7)
)]
) )
) )

View File

@ -52,7 +52,7 @@ ProgramNode(0...139)(
), ),
MultiWriteNode(47...65)( MultiWriteNode(47...65)(
[InstanceVariableTargetNode(47...51)(:@foo), [InstanceVariableTargetNode(47...51)(:@foo),
ClassVariableTargetNode(53...58)()], ClassVariableTargetNode(53...58)(:@@bar)],
(59...60), (59...60),
ArrayNode(61...65)( ArrayNode(61...65)(
[SplatNode(61...65)( [SplatNode(61...65)(

View File

@ -8,7 +8,10 @@ ProgramNode(0...14)(
InstanceVariableReadNode(2...4)(:@a) InstanceVariableReadNode(2...4)(:@a)
), ),
StringNode(4...5)(nil, (4...5), nil, " "), StringNode(4...5)(nil, (4...5), nil, " "),
EmbeddedVariableNode(5...9)((5...6), ClassVariableReadNode(6...9)()), EmbeddedVariableNode(5...9)(
(5...6),
ClassVariableReadNode(6...9)(:@@a)
),
StringNode(9...10)(nil, (9...10), nil, " "), StringNode(9...10)(nil, (9...10), nil, " "),
EmbeddedVariableNode(10...13)( EmbeddedVariableNode(10...13)(
(10...11), (10...11),

View File

@ -2,6 +2,7 @@ ProgramNode(0...53)(
[:a], [:a],
StatementsNode(0...53)( StatementsNode(0...53)(
[ClassVariableOperatorWriteNode(0...11)( [ClassVariableOperatorWriteNode(0...11)(
:@@var,
(0...5), (0...5),
(6...8), (6...8),
IntegerNode(9...11)(), IntegerNode(9...11)(),
@ -28,6 +29,7 @@ ProgramNode(0...53)(
nil, nil,
StatementsNode(37...48)( StatementsNode(37...48)(
[ClassVariableOperatorWriteNode(37...48)( [ClassVariableOperatorWriteNode(37...48)(
:@@var,
(37...42), (37...42),
(43...45), (43...45),
IntegerNode(46...48)(), IntegerNode(46...48)(),

View File

@ -727,6 +727,8 @@ nodes:
^^^^^^^^^^^^^ ^^^^^^^^^^^^^
- name: ClassVariableAndWriteNode - name: ClassVariableAndWriteNode
child_nodes: child_nodes:
- name: name
type: constant
- name: name_loc - name: name_loc
type: location type: location
- name: operator_loc - name: operator_loc
@ -740,6 +742,8 @@ nodes:
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
- name: ClassVariableOperatorWriteNode - name: ClassVariableOperatorWriteNode
child_nodes: child_nodes:
- name: name
type: constant
- name: name_loc - name: name_loc
type: location type: location
- name: operator_loc - name: operator_loc
@ -755,6 +759,8 @@ nodes:
^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^
- name: ClassVariableOrWriteNode - name: ClassVariableOrWriteNode
child_nodes: child_nodes:
- name: name
type: constant
- name: name_loc - name: name_loc
type: location type: location
- name: operator_loc - name: operator_loc
@ -767,12 +773,18 @@ nodes:
@@target ||= value @@target ||= value
^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
- name: ClassVariableReadNode - name: ClassVariableReadNode
child_nodes:
- name: name
type: constant
comment: | comment: |
Represents referencing a class variable. Represents referencing a class variable.
@@foo @@foo
^^^^^ ^^^^^
- name: ClassVariableTargetNode - name: ClassVariableTargetNode
child_nodes:
- name: name
type: constant
comment: | comment: |
Represents writing to a class variable in a context that doesn't have an explicit value. Represents writing to a class variable in a context that doesn't have an explicit value.
@ -780,6 +792,8 @@ nodes:
^^^^^ ^^^^^ ^^^^^ ^^^^^
- name: ClassVariableWriteNode - name: ClassVariableWriteNode
child_nodes: child_nodes:
- name: name
type: constant
- name: name_loc - name: name_loc
type: location type: location
- name: value - name: value

View File

@ -1558,8 +1558,7 @@ yp_class_node_create(yp_parser_t *parser, yp_constant_id_list_t *locals, const y
// Allocate and initialize a new ClassVariableAndWriteNode node. // Allocate and initialize a new ClassVariableAndWriteNode node.
static yp_class_variable_and_write_node_t * static yp_class_variable_and_write_node_t *
yp_class_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) { yp_class_variable_and_write_node_create(yp_parser_t *parser, yp_class_variable_read_node_t *target, const yp_token_t *operator, yp_node_t *value) {
assert(YP_NODE_TYPE_P(target, YP_NODE_CLASS_VARIABLE_READ_NODE));
assert(operator->type == YP_TOKEN_AMPERSAND_AMPERSAND_EQUAL); assert(operator->type == YP_TOKEN_AMPERSAND_AMPERSAND_EQUAL);
yp_class_variable_and_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_and_write_node_t); yp_class_variable_and_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_and_write_node_t);
@ -1567,11 +1566,12 @@ yp_class_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *target,
{ {
.type = YP_NODE_CLASS_VARIABLE_AND_WRITE_NODE, .type = YP_NODE_CLASS_VARIABLE_AND_WRITE_NODE,
.location = { .location = {
.start = target->location.start, .start = target->base.location.start,
.end = value->location.end .end = value->location.end
} }
}, },
.name_loc = target->location, .name = target->name,
.name_loc = target->base.location,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator), .operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
.value = value .value = value
}; };
@ -1581,18 +1581,19 @@ yp_class_variable_and_write_node_create(yp_parser_t *parser, yp_node_t *target,
// Allocate and initialize a new ClassVariableOperatorWriteNode node. // Allocate and initialize a new ClassVariableOperatorWriteNode node.
static yp_class_variable_operator_write_node_t * static yp_class_variable_operator_write_node_t *
yp_class_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) { yp_class_variable_operator_write_node_create(yp_parser_t *parser, yp_class_variable_read_node_t *target, const yp_token_t *operator, yp_node_t *value) {
yp_class_variable_operator_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_operator_write_node_t); yp_class_variable_operator_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_operator_write_node_t);
*node = (yp_class_variable_operator_write_node_t) { *node = (yp_class_variable_operator_write_node_t) {
{ {
.type = YP_NODE_CLASS_VARIABLE_OPERATOR_WRITE_NODE, .type = YP_NODE_CLASS_VARIABLE_OPERATOR_WRITE_NODE,
.location = { .location = {
.start = target->location.start, .start = target->base.location.start,
.end = value->location.end .end = value->location.end
} }
}, },
.name_loc = target->location, .name = target->name,
.name_loc = target->base.location,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator), .operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
.value = value, .value = value,
.operator = yp_parser_constant_id_location(parser, operator->start, operator->end - 1) .operator = yp_parser_constant_id_location(parser, operator->start, operator->end - 1)
@ -1603,8 +1604,7 @@ yp_class_variable_operator_write_node_create(yp_parser_t *parser, yp_node_t *tar
// Allocate and initialize a new ClassVariableOrWriteNode node. // Allocate and initialize a new ClassVariableOrWriteNode node.
static yp_class_variable_or_write_node_t * static yp_class_variable_or_write_node_t *
yp_class_variable_or_write_node_create(yp_parser_t *parser, yp_node_t *target, const yp_token_t *operator, yp_node_t *value) { yp_class_variable_or_write_node_create(yp_parser_t *parser, yp_class_variable_read_node_t *target, const yp_token_t *operator, yp_node_t *value) {
assert(YP_NODE_TYPE_P(target, YP_NODE_CLASS_VARIABLE_READ_NODE));
assert(operator->type == YP_TOKEN_PIPE_PIPE_EQUAL); assert(operator->type == YP_TOKEN_PIPE_PIPE_EQUAL);
yp_class_variable_or_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_or_write_node_t); yp_class_variable_or_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_or_write_node_t);
@ -1612,11 +1612,12 @@ yp_class_variable_or_write_node_create(yp_parser_t *parser, yp_node_t *target, c
{ {
.type = YP_NODE_CLASS_VARIABLE_OR_WRITE_NODE, .type = YP_NODE_CLASS_VARIABLE_OR_WRITE_NODE,
.location = { .location = {
.start = target->location.start, .start = target->base.location.start,
.end = value->location.end .end = value->location.end
} }
}, },
.name_loc = target->location, .name = target->name,
.name_loc = target->base.location,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator), .operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
.value = value .value = value
}; };
@ -1629,13 +1630,21 @@ static yp_class_variable_read_node_t *
yp_class_variable_read_node_create(yp_parser_t *parser, const yp_token_t *token) { yp_class_variable_read_node_create(yp_parser_t *parser, const yp_token_t *token) {
assert(token->type == YP_TOKEN_CLASS_VARIABLE); assert(token->type == YP_TOKEN_CLASS_VARIABLE);
yp_class_variable_read_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_read_node_t); yp_class_variable_read_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_read_node_t);
*node = (yp_class_variable_read_node_t) {{ .type = YP_NODE_CLASS_VARIABLE_READ_NODE, .location = YP_LOCATION_TOKEN_VALUE(token) }};
*node = (yp_class_variable_read_node_t) {
{
.type = YP_NODE_CLASS_VARIABLE_READ_NODE,
.location = YP_LOCATION_TOKEN_VALUE(token)
},
.name = yp_parser_constant_id_location(parser, token->start, token->end)
};
return node; return node;
} }
// Initialize a new ClassVariableWriteNode node from a ClassVariableRead node. // Initialize a new ClassVariableWriteNode node from a ClassVariableRead node.
static yp_class_variable_write_node_t * static yp_class_variable_write_node_t *
yp_class_variable_read_node_to_class_variable_write_node(yp_parser_t *parser, yp_class_variable_read_node_t *read_node, yp_token_t *operator, yp_node_t *value) { yp_class_variable_write_node_create(yp_parser_t *parser, yp_class_variable_read_node_t *read_node, yp_token_t *operator, yp_node_t *value) {
yp_class_variable_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_write_node_t); yp_class_variable_write_node_t *node = YP_ALLOC_NODE(parser, yp_class_variable_write_node_t);
*node = (yp_class_variable_write_node_t) { *node = (yp_class_variable_write_node_t) {
@ -1646,6 +1655,7 @@ yp_class_variable_read_node_to_class_variable_write_node(yp_parser_t *parser, yp
.end = value->location.end .end = value->location.end
}, },
}, },
.name = read_node->name,
.name_loc = YP_LOCATION_NODE_VALUE((yp_node_t *) read_node), .name_loc = YP_LOCATION_NODE_VALUE((yp_node_t *) read_node),
.operator_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(operator), .operator_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(operator),
.value = value .value = value
@ -8007,7 +8017,7 @@ parse_write(yp_parser_t *parser, yp_node_t *target, yp_token_t *operator, yp_nod
case YP_NODE_MISSING_NODE: case YP_NODE_MISSING_NODE:
return target; return target;
case YP_NODE_CLASS_VARIABLE_READ_NODE: { case YP_NODE_CLASS_VARIABLE_READ_NODE: {
yp_class_variable_write_node_t *write_node = yp_class_variable_read_node_to_class_variable_write_node(parser, (yp_class_variable_read_node_t *) target, operator, value); yp_class_variable_write_node_t *write_node = yp_class_variable_write_node_create(parser, (yp_class_variable_read_node_t *) target, operator, value);
yp_node_destroy(parser, target); yp_node_destroy(parser, target);
return (yp_node_t *) write_node; return (yp_node_t *) write_node;
} }
@ -12837,7 +12847,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t
parser_lex(parser); parser_lex(parser);
yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after &&="); yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after &&=");
yp_node_t *result = (yp_node_t *) yp_class_variable_and_write_node_create(parser, node, &token, value); yp_node_t *result = (yp_node_t *) yp_class_variable_and_write_node_create(parser, (yp_class_variable_read_node_t *) node, &token, value);
yp_node_destroy(parser, node); yp_node_destroy(parser, node);
return result; return result;
@ -12938,7 +12948,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t
parser_lex(parser); parser_lex(parser);
yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after ||="); yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after ||=");
yp_node_t *result = (yp_node_t *) yp_class_variable_or_write_node_create(parser, node, &token, value); yp_node_t *result = (yp_node_t *) yp_class_variable_or_write_node_create(parser, (yp_class_variable_read_node_t *) node, &token, value);
yp_node_destroy(parser, node); yp_node_destroy(parser, node);
return result; return result;
@ -13049,7 +13059,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t
parser_lex(parser); parser_lex(parser);
yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after the operator."); yp_node_t *value = parse_expression(parser, binding_power, "Expected a value after the operator.");
yp_node_t *result = (yp_node_t *) yp_class_variable_operator_write_node_create(parser, node, &token, value); yp_node_t *result = (yp_node_t *) yp_class_variable_operator_write_node_create(parser, (yp_class_variable_read_node_t *) node, &token, value);
yp_node_destroy(parser, node); yp_node_destroy(parser, node);
return result; return result;