Skip to content

Commit

Permalink
[ruby/prism] Shareable constant nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored and matzbot committed Mar 15, 2024
1 parent 5978568 commit c45ad17
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 16 deletions.
5 changes: 5 additions & 0 deletions lib/prism/translation/parser/compiler.rb
Expand Up @@ -1423,6 +1423,11 @@ def visit_self_node(node)
builder.self(token(node.location))
end

# A shareable constant.
def visit_shareable_constant_node(node)
visit(node.write)
end

# class << self; end
# ^^^^^^^^^^^^^^^^^^
def visit_singleton_class_node(node)
Expand Down
5 changes: 5 additions & 0 deletions lib/prism/translation/ripper.rb
Expand Up @@ -2858,6 +2858,11 @@ def visit_self_node(node)
on_var_ref(on_kw("self"))
end

# A shareable constant.
def visit_shareable_constant_node(node)
visit(node.write)
end

# class << self; end
# ^^^^^^^^^^^^^^^^^^
def visit_singleton_class_node(node)
Expand Down
5 changes: 5 additions & 0 deletions lib/prism/translation/ruby_parser.rb
Expand Up @@ -1274,6 +1274,11 @@ def visit_self_node(node)
s(node, :self)
end

# A shareable constant.
def visit_shareable_constant_node(node)
visit(node.write)
end

# class << self; end
# ^^^^^^^^^^^^^^^^^^
def visit_singleton_class_node(node)
Expand Down
33 changes: 33 additions & 0 deletions prism/config.yml
Expand Up @@ -248,6 +248,7 @@ warnings:
- INTEGER_IN_FLIP_FLOP
- INVALID_CHARACTER
- INVALID_NUMBERED_REFERENCE
- INVALID_SHAREABLE_CONSTANT_VALUE
- KEYWORD_EOL
- LITERAL_IN_CONDITION_DEFAULT
- LITERAL_IN_CONDITION_VERBOSE
Expand Down Expand Up @@ -667,6 +668,15 @@ flags:
- name: FORCED_US_ASCII_ENCODING
comment: "internal bytes forced the encoding to US-ASCII"
comment: Flags for regular expression and match last line nodes.
- name: ShareableConstantNodeFlags
values:
- name: LITERAL
comment: "constant writes that should be modified with shareable constant value literal"
- name: EXPERIMENTAL_EVERYTHING
comment: "constant writes that should be modified with shareable constant value experimental everything"
- name: EXPERIMENTAL_COPY
comment: "constant writes that should be modified with shareable constant value experimental copy"
comment: Flags for shareable constant nodes.
- name: StringFlags
values:
- name: FORCED_UTF8_ENCODING
Expand Down Expand Up @@ -3063,6 +3073,29 @@ nodes:
self
^^^^
- name: ShareableConstantNode
fields:
- name: flags
type: flags
kind: ShareableConstantNodeFlags
- name: write
type: node
kind:
- ConstantWriteNode
- ConstantAndWriteNode
- ConstantOrWriteNode
- ConstantOperatorWriteNode
- ConstantPathWriteNode
- ConstantPathAndWriteNode
- ConstantPathOrWriteNode
- ConstantPathOperatorWriteNode
comment: The constant write that should be modified with the shareability state.
comment: |
This node wraps a constant write to indicate that when the value is written, it should have its shareability state modified.
# shareable_constant_value: literal
C = { a: 1 }
^^^^^^^^^^^^
- name: SingletonClassNode
fields:
- name: locals
Expand Down
13 changes: 13 additions & 0 deletions prism/parser.h
Expand Up @@ -448,6 +448,13 @@ typedef struct {
void (*callback)(void *data, pm_parser_t *parser, pm_token_t *token);
} pm_lex_callback_t;

/** The type of shareable constant value that can be set. */
typedef uint8_t pm_shareable_constant_value_t;
static const pm_shareable_constant_value_t PM_SCOPE_SHAREABLE_CONSTANT_NONE = 0x0;
static const pm_shareable_constant_value_t PM_SCOPE_SHAREABLE_CONSTANT_LITERAL = 0x1;
static const pm_shareable_constant_value_t PM_SCOPE_SHAREABLE_CONSTANT_EXPERIMENTAL_EVERYTHING = 0x2;
static const pm_shareable_constant_value_t PM_SCOPE_SHAREABLE_CONSTANT_EXPERIMENTAL_COPY = 0x4;

/**
* This struct represents a node in a linked list of scopes. Some scopes can see
* into their parent scopes, while others cannot.
Expand Down Expand Up @@ -487,6 +494,12 @@ typedef struct pm_scope {
*/
int8_t numbered_parameters;

/**
* The current state of constant shareability for this scope. This is
* changed by magic shareable_constant_value comments.
*/
pm_shareable_constant_value_t shareable_constant;

/**
* A boolean indicating whether or not this scope can see into its parent.
* If closed is true, then the scope cannot see into its parent.
Expand Down
114 changes: 100 additions & 14 deletions prism/prism.c
Expand Up @@ -5731,6 +5731,25 @@ pm_self_node_create(pm_parser_t *parser, const pm_token_t *token) {
return node;
}

/**
* Allocate and initialize a new ShareableConstantNode node.
*/
static pm_shareable_constant_node_t *
pm_shareable_constant_node_create(pm_parser_t *parser, pm_node_t *write, pm_shareable_constant_value_t value) {
pm_shareable_constant_node_t *node = PM_ALLOC_NODE(parser, pm_shareable_constant_node_t);

*node = (pm_shareable_constant_node_t) {
{
.type = PM_SHAREABLE_CONSTANT_NODE,
.flags = (pm_node_flags_t) value,
.location = PM_LOCATION_NODE_VALUE(write)
},
.write = write
};

return node;
}

/**
* Allocate a new SingletonClassNode node.
*/
Expand Down Expand Up @@ -6745,6 +6764,7 @@ pm_parser_scope_push(pm_parser_t *parser, bool closed) {
.locals = { 0 },
.parameters = PM_SCOPE_PARAMETERS_NONE,
.numbered_parameters = PM_SCOPE_NUMBERED_PARAMETERS_NONE,
.shareable_constant = (closed || parser->current_scope == NULL) ? PM_SCOPE_SHAREABLE_CONSTANT_NONE : parser->current_scope->shareable_constant,
.closed = closed
};

Expand Down Expand Up @@ -6790,6 +6810,27 @@ pm_parser_scope_forwarding_keywords_check(pm_parser_t *parser, const pm_token_t
pm_parser_scope_forwarding_param_check(parser, token, PM_SCOPE_PARAMETERS_FORWARDING_KEYWORDS, PM_ERR_EXPECT_EXPRESSION_AFTER_SPLAT_HASH);
}

/**
* Get the current state of constant shareability.
*/
static inline pm_shareable_constant_value_t
pm_parser_scope_shareable_constant_get(pm_parser_t *parser) {
return parser->current_scope->shareable_constant;
}

/**
* Set the current state of constant shareability. We'll set it on all of the
* open scopes so that reads are quick.
*/
static void
pm_parser_scope_shareable_constant_set(pm_parser_t *parser, pm_shareable_constant_value_t shareable_constant) {
pm_scope_t *scope = parser->current_scope;

do {
scope->shareable_constant = shareable_constant;
} while (!scope->closed && (scope = scope->previous) != NULL);
}

/**
* Save the current param name as the return value and set it to the given
* constant id.
Expand Down Expand Up @@ -7345,6 +7386,28 @@ parser_lex_magic_comment(pm_parser_t *parser, bool semantic_token_seen) {
}
}

// If we have hit a ractor pragma, attempt to lex that.
uint32_t value_length = (uint32_t) (value_end - value_start);
if (key_length == 24 && pm_strncasecmp(key_source, (const uint8_t *) "shareable_constant_value", 24) == 0) {
if (value_length == 4 && pm_strncasecmp(value_start, (const uint8_t *) "none", 4) == 0) {
pm_parser_scope_shareable_constant_set(parser, PM_SCOPE_SHAREABLE_CONSTANT_NONE);
} else if (value_length == 7 && pm_strncasecmp(value_start, (const uint8_t *) "literal", 7) == 0) {
pm_parser_scope_shareable_constant_set(parser, PM_SCOPE_SHAREABLE_CONSTANT_LITERAL);
} else if (value_length == 23 && pm_strncasecmp(value_start, (const uint8_t *) "experimental_everything", 23) == 0) {
pm_parser_scope_shareable_constant_set(parser, PM_SCOPE_SHAREABLE_CONSTANT_EXPERIMENTAL_EVERYTHING);
} else if (value_length == 17 && pm_strncasecmp(value_start, (const uint8_t *) "experimental_copy", 17) == 0) {
pm_parser_scope_shareable_constant_set(parser, PM_SCOPE_SHAREABLE_CONSTANT_EXPERIMENTAL_COPY);
} else {
PM_PARSER_WARN_TOKEN_FORMAT(
parser,
parser->current,
PM_WARN_INVALID_SHAREABLE_CONSTANT_VALUE,
(int) value_length,
(const char *) value_start
);
}
}

// When we're done, we want to free the string in case we had to
// allocate memory for it.
pm_string_free(&key);
Expand Down Expand Up @@ -11991,6 +12054,21 @@ parse_target_validate(pm_parser_t *parser, pm_node_t *target) {
return result;
}

/**
* Potentially wrap a constant write node in a shareable constant node depending
* on the current state.
*/
static pm_node_t *
parse_shareable_constant_write(pm_parser_t *parser, pm_node_t *write) {
pm_shareable_constant_value_t shareable_constant = pm_parser_scope_shareable_constant_get(parser);

if (shareable_constant != PM_SCOPE_SHAREABLE_CONSTANT_NONE) {
return (pm_node_t *) pm_shareable_constant_node_create(parser, write, shareable_constant);
}

return write;
}

/**
* Convert the given node into a valid write node.
*/
Expand All @@ -12005,15 +12083,17 @@ parse_write(pm_parser_t *parser, pm_node_t *target, pm_token_t *operator, pm_nod
pm_node_destroy(parser, target);
return (pm_node_t *) node;
}
case PM_CONSTANT_PATH_NODE:
return (pm_node_t *) pm_constant_path_write_node_create(parser, (pm_constant_path_node_t *) target, operator, value);
case PM_CONSTANT_PATH_NODE: {
pm_node_t *node = (pm_node_t *) pm_constant_path_write_node_create(parser, (pm_constant_path_node_t *) target, operator, value);
return parse_shareable_constant_write(parser, node);
}
case PM_CONSTANT_READ_NODE: {
pm_constant_write_node_t *node = pm_constant_write_node_create(parser, (pm_constant_read_node_t *) target, operator, value);
pm_node_t *node = (pm_node_t *) pm_constant_write_node_create(parser, (pm_constant_read_node_t *) target, operator, value);
if (context_def_p(parser)) {
pm_parser_err_node(parser, (pm_node_t *) node, PM_ERR_WRITE_TARGET_IN_METHOD);
pm_parser_err_node(parser, node, PM_ERR_WRITE_TARGET_IN_METHOD);
}
pm_node_destroy(parser, target);
return (pm_node_t *) node;
return parse_shareable_constant_write(parser, node);
}
case PM_BACK_REFERENCE_READ_NODE:
case PM_NUMBERED_REFERENCE_READ_NODE:
Expand Down Expand Up @@ -17954,16 +18034,18 @@ parse_expression_infix(pm_parser_t *parser, pm_node_t *node, pm_binding_power_t
parser_lex(parser);

pm_node_t *value = parse_assignment_value(parser, previous_binding_power, binding_power, accepts_command_call, PM_ERR_EXPECT_EXPRESSION_AFTER_AMPAMPEQ);
return (pm_node_t *) pm_constant_path_and_write_node_create(parser, (pm_constant_path_node_t *) node, &token, value);
pm_node_t *write = (pm_node_t *) pm_constant_path_and_write_node_create(parser, (pm_constant_path_node_t *) node, &token, value);

return parse_shareable_constant_write(parser, write);
}
case PM_CONSTANT_READ_NODE: {
parser_lex(parser);

pm_node_t *value = parse_assignment_value(parser, previous_binding_power, binding_power, accepts_command_call, PM_ERR_EXPECT_EXPRESSION_AFTER_AMPAMPEQ);
pm_node_t *result = (pm_node_t *) pm_constant_and_write_node_create(parser, (pm_constant_read_node_t *) node, &token, value);
pm_node_t *write = (pm_node_t *) pm_constant_and_write_node_create(parser, (pm_constant_read_node_t *) node, &token, value);

pm_node_destroy(parser, node);
return result;
return parse_shareable_constant_write(parser, write);
}
case PM_INSTANCE_VARIABLE_READ_NODE: {
parser_lex(parser);
Expand Down Expand Up @@ -18065,16 +18147,18 @@ parse_expression_infix(pm_parser_t *parser, pm_node_t *node, pm_binding_power_t
parser_lex(parser);

pm_node_t *value = parse_assignment_value(parser, previous_binding_power, binding_power, accepts_command_call, PM_ERR_EXPECT_EXPRESSION_AFTER_PIPEPIPEEQ);
return (pm_node_t *) pm_constant_path_or_write_node_create(parser, (pm_constant_path_node_t *) node, &token, value);
pm_node_t *write = (pm_node_t *) pm_constant_path_or_write_node_create(parser, (pm_constant_path_node_t *) node, &token, value);

return parse_shareable_constant_write(parser, write);
}
case PM_CONSTANT_READ_NODE: {
parser_lex(parser);

pm_node_t *value = parse_assignment_value(parser, previous_binding_power, binding_power, accepts_command_call, PM_ERR_EXPECT_EXPRESSION_AFTER_PIPEPIPEEQ);
pm_node_t *result = (pm_node_t *) pm_constant_or_write_node_create(parser, (pm_constant_read_node_t *) node, &token, value);
pm_node_t *write = (pm_node_t *) pm_constant_or_write_node_create(parser, (pm_constant_read_node_t *) node, &token, value);

pm_node_destroy(parser, node);
return result;
return parse_shareable_constant_write(parser, write);
}
case PM_INSTANCE_VARIABLE_READ_NODE: {
parser_lex(parser);
Expand Down Expand Up @@ -18186,16 +18270,18 @@ parse_expression_infix(pm_parser_t *parser, pm_node_t *node, pm_binding_power_t
parser_lex(parser);

pm_node_t *value = parse_assignment_value(parser, previous_binding_power, binding_power, accepts_command_call, PM_ERR_EXPECT_EXPRESSION_AFTER_OPERATOR);
return (pm_node_t *) pm_constant_path_operator_write_node_create(parser, (pm_constant_path_node_t *) node, &token, value);
pm_node_t *write = (pm_node_t *) pm_constant_path_operator_write_node_create(parser, (pm_constant_path_node_t *) node, &token, value);

return parse_shareable_constant_write(parser, write);
}
case PM_CONSTANT_READ_NODE: {
parser_lex(parser);

pm_node_t *value = parse_assignment_value(parser, previous_binding_power, binding_power, accepts_command_call, PM_ERR_EXPECT_EXPRESSION_AFTER_OPERATOR);
pm_node_t *result = (pm_node_t *) pm_constant_operator_write_node_create(parser, (pm_constant_read_node_t *) node, &token, value);
pm_node_t *write = (pm_node_t *) pm_constant_operator_write_node_create(parser, (pm_constant_read_node_t *) node, &token, value);

pm_node_destroy(parser, node);
return result;
return parse_shareable_constant_write(parser, write);
}
case PM_INSTANCE_VARIABLE_READ_NODE: {
parser_lex(parser);
Expand Down
1 change: 1 addition & 0 deletions prism/templates/src/diagnostic.c.erb
Expand Up @@ -327,6 +327,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
[PM_WARN_FLOAT_OUT_OF_RANGE] = { "Float %.*s%s out of range", PM_WARNING_LEVEL_VERBOSE },
[PM_WARN_INTEGER_IN_FLIP_FLOP] = { "integer literal in flip-flop", PM_WARNING_LEVEL_DEFAULT },
[PM_WARN_INVALID_CHARACTER] = { "invalid character syntax; use %s%s%s", PM_WARNING_LEVEL_DEFAULT },
[PM_WARN_INVALID_SHAREABLE_CONSTANT_VALUE] = { "invalid value for shareable_constant_value: %.*s", PM_WARNING_LEVEL_VERBOSE },
[PM_WARN_INVALID_NUMBERED_REFERENCE] = { "'%.*s' is too big for a number variable, always nil", PM_WARNING_LEVEL_DEFAULT },
[PM_WARN_KEYWORD_EOL] = { "`%.*s` at the end of line without an expression", PM_WARNING_LEVEL_VERBOSE },
[PM_WARN_LITERAL_IN_CONDITION_DEFAULT] = { "%sliteral in %s", PM_WARNING_LEVEL_DEFAULT },
Expand Down
12 changes: 10 additions & 2 deletions test/prism/location_test.rb
Expand Up @@ -781,6 +781,15 @@ def test_SelfNode
assert_location(SelfNode, "self")
end

def test_ShareableConstantNode
source = <<~RUBY
# shareable_constant_value: literal
C = { foo: 1 }
RUBY

assert_location(ShareableConstantNode, source, 36...50)
end

def test_SingletonClassNode
assert_location(SingletonClassNode, "class << self; end")
end
Expand Down Expand Up @@ -915,8 +924,7 @@ def test_all_tested

def assert_location(kind, source, expected = 0...source.length, **options)
result = Prism.parse(source, **options)
assert_equal [], result.comments
assert_equal [], result.errors
assert result.success?

node = result.value.statements.body.last
node = yield node if block_given?
Expand Down

0 comments on commit c45ad17

Please sign in to comment.