[Feature #16254] Allow `__builtin.func` style

This commit is contained in:
Nobuyoshi Nakada 2020-05-18 10:09:28 +09:00
Родитель f3e081c6b6
Коммит c8703a17ce
2 изменённых файлов: 38 добавлений и 10 удалений

Просмотреть файл

@ -7039,18 +7039,30 @@ iseq_builtin_function_lookup(const rb_iseq_t *iseq, const char *name)
}
static const char *
iseq_builtin_function_name(ID mid)
iseq_builtin_function_name(const enum node_type type, const NODE *recv, ID mid)
{
const char *name = rb_id2name(mid);
static const char prefix[] = "__builtin_";
const size_t prefix_len = sizeof(prefix) - 1;
if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
return &name[prefix_len];
if (type == NODE_CALL) {
if (recv) {
switch (nd_type(recv)) {
case NODE_VCALL:
if (recv->nd_mid == rb_intern("__builtin")) {
return name;
}
break;
default: break;
}
}
}
else {
return NULL;
else if (type == NODE_VCALL || type == NODE_FCALL) {
if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
return &name[prefix_len];
}
}
return NULL;
}
static int
@ -7204,8 +7216,7 @@ compile_call(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, co
NODE *args_node = node->nd_args;
if (UNLIKELY(iseq_has_builtin_function_table(iseq)) &&
(type == NODE_VCALL || type == NODE_FCALL) &&
(builtin_func = iseq_builtin_function_name(mid)) != NULL) {
(builtin_func = iseq_builtin_function_name(type, node->nd_recv, mid)) != NULL) {
if (parent_block != NULL) {
COMPILE_ERROR(iseq, line, "should not call builtins here.");

Просмотреть файл

@ -72,7 +72,7 @@ end
def collect_builtin base, tree, name, bs, inlines, params = nil
while tree
call = sep = mid = args = nil
call = recv = sep = mid = args = nil
case tree.first
when :def
params = collect_params(tree[2])
@ -93,6 +93,8 @@ def collect_builtin base, tree, name, bs, inlines, params = nil
when :method_add_arg
_, mid, (_, (_, args)) = tree
case mid.first
when :call
_, recv, sep, mid = mid
when :fcall
_, mid = mid
else
@ -102,12 +104,27 @@ def collect_builtin base, tree, name, bs, inlines, params = nil
_, mid = tree
when :command # FCALL
_, mid, (_, args) = tree
when :call, :command_call # CALL
_, recv, sep, mid, (_, args) = tree
end
if mid
raise "unknown sexp: #{mid.inspect}" unless mid.first == :@ident
_, mid, (lineno,) = mid
if /\A__builtin_(.+)/ =~ mid
cfunc_name = func_name = $1
if recv
func_name = nil
case recv.first
when :vcall
_, recv = recv
if recv.first == :@ident and recv[1] == "__builtin"
func_name = mid.to_s
end
end
collect_builtin(base, recv, name, bs, inlines) unless func_name
else
func_name = mid[/\A__builtin_(.+)/, 1]
end
if func_name
cfunc_name = func_name
args.pop unless (args ||= []).last
argc = args.size