Commit b758ea15

b758ea15a55fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified

Description

Merge pull request #1494 from ryanseys/rs-define-singleton-method

Support define_singleton_method with a literal name

Details

Commit
b758ea15a55f
Parent
1679802946bd
Merge parent
a74fc490802e

src/analyze_scope.cmodifiedView file

@@ -663,7 +663,24 @@
663663 function (class_id stays -1), matching `def`. */
664664 const char *dm_cn = nt_str(c->nt, id, "name");
665665 int dm_recv = nt_ref(c->nt, id, "receiver");
666- if (dm_cn && !strcmp(dm_cn, "define_method") && dm_recv < 0) {
666+ int dm_is_dm = dm_cn && !strcmp(dm_cn, "define_method") && dm_recv < 0;
667+ int dm_is_dsm = dm_cn && !strcmp(dm_cn, "define_singleton_method");
668+ /* define_singleton_method registers a class method on the resolved target:
669+ a class constant receiver, `self` in a class body, or no receiver (the
670+ enclosing class). An arbitrary-instance singleton has no compile-time
671+ class, so it is not registered (the later call rejects). */
672+ int dm_cmethod = 0, dm_cls = class_id, dm_ok = dm_is_dm;
673+ if (dm_is_dsm) {
674+ dm_cmethod = 1;
675+ const char *dsm_rty = dm_recv >= 0 ? nt_type(c->nt, dm_recv) : NULL;
676+ if (dm_recv < 0) dm_cls = class_id;
677+ else if (dsm_rty && (!strcmp(dsm_rty, "ConstantReadNode") || !strcmp(dsm_rty, "ConstantPathNode")))
678+ dm_cls = comp_class_index(c, nt_str(c->nt, dm_recv, "name"));
679+ else if (dsm_rty && !strcmp(dsm_rty, "SelfNode")) dm_cls = class_id;
680+ else dm_cls = -1;
681+ dm_ok = dm_cls >= 0;
682+ }
683+ if (dm_ok) {
667684 int dm_args = nt_ref(c->nt, id, "arguments");
668685 int dm_na = 0;
669686 const int *dm_argv = dm_args >= 0 ? nt_arr(c->nt, dm_args, "arguments", &dm_na) : NULL;
@@ -679,7 +696,8 @@
679696 Scope *dm_s = comp_scope_new(c, dm_mname, id);
680697 int dm_new_idx = c->nscopes - 1;
681698 dm_s->body = nt_ref(c->nt, dm_blk, "body");
682- dm_s->class_id = class_id;
699+ dm_s->class_id = dm_cls;
700+ dm_s->is_cmethod = dm_cmethod;
683701 /* the block's params are the defined method's params (e.g. the
684702 `&:to_s`-rewritten `{ |_spx| _spx.to_s }`'s _spx). */
685703 int dm_pn = nt_ref(c->nt, dm_blk, "parameters");