Project

General

Profile

Feature #5454 ยป 0002-use-struct-rb_args_info-instead-of-NODEs.patch

mame (Yusuke Endoh), 10/18/2011 01:21 AM

View differences:

compile.c
1067 1067
    debugs("iseq_set_arguments: %s\n", node_args ? "" : "0");
1068 1068

  
1069 1069
    if (node_args) {
1070
	NODE *node_aux = node_args->nd_next;
1071
	NODE *node_opt = node_args->nd_opt;
1070
	struct rb_args_info *args = node_args->nd_ainfo;
1072 1071
	ID rest_id = 0;
1073 1072
	int last_comma = 0;
1074 1073
	ID block_id = 0;
1075
	NODE *node_init = 0;
1076 1074

  
1077 1075
	if (nd_type(node_args) != NODE_ARGS) {
1078 1076
	    rb_bug("iseq_set_arguments: NODE_ARGS is expected, but %s",
1079 1077
		   ruby_node_name(nd_type(node_args)));
1080 1078
	}
1081 1079

  
1082
	/*
1083
         * new argument information:
1084
         *   NODE_ARGS     [m: int,  o: NODE_OPT_ARG, ->]
1085
         *   NODE_ARGS_AUX [r: ID,   b: ID,           ->]
1086
         *   NODE_ARGS_AUX [Pst: id, Plen: int,       init: NODE*]
1087
         *  optarg information:
1088
         *   NODE_OPT_ARGS [idx,     expr,            next ->]
1089
	 *  init arg:
1090
	 *   NODE_AND(m_init, p_init)
1091
	 *  if "r" is 1, it's means "{|x,|}" type block parameter.
1092
	 */
1093 1080

  
1094
	iseq->argc = (int)node_args->nd_frml;
1081
	iseq->argc = (int)args->pre_args_num;
1095 1082
	debugs("  - argc: %d\n", iseq->argc);
1096 1083

  
1097
	if (node_aux) {
1098
	    rest_id = node_aux->nd_rest;
1099
	    if (rest_id == 1) {
1100
		last_comma = 1;
1101
		rest_id = 0;
1102
	    }
1103
	    block_id = (ID)node_aux->nd_body;
1104
	    node_aux = node_aux->nd_next;
1105

  
1106
	    if (node_aux) {
1107
		ID post_start_id = node_aux->nd_pid;
1108
		iseq->arg_post_start = get_dyna_var_idx_at_raw(iseq, post_start_id);
1109
		iseq->arg_post_len = (int)node_aux->nd_plen;
1110
		node_init = node_aux->nd_next;
1111
	    }
1084
	rest_id = args->rest_arg;
1085
	if (rest_id == 1) {
1086
	    last_comma = 1;
1087
	    rest_id = 0;
1112 1088
	}
1089
	block_id = args->block_arg;
1113 1090

  
1114
	if (node_opt) {
1115
	    NODE *node = node_opt;
1091
	if (args->first_post_arg) {
1092
	    iseq->arg_post_start = get_dyna_var_idx_at_raw(iseq, args->first_post_arg);
1093
	    iseq->arg_post_len = args->post_args_num;
1094
	}
1095

  
1096
	if (args->opt_args) {
1097
	    NODE *node = args->opt_args;
1116 1098
	    LABEL *label;
1117 1099
	    VALUE labels = rb_ary_tmp_new(1);
1118 1100
	    int i = 0, j;
......
1144 1126
	    iseq->arg_opts = 0;
1145 1127
	}
1146 1128

  
1147
	if (node_init) {
1148
	    if (node_init->nd_1st) { /* m_init */
1149
		COMPILE_POPED(optargs, "init arguments (m)", node_init->nd_1st);
1150
	    }
1151
	    if (node_init->nd_2nd) { /* p_init */
1152
		COMPILE_POPED(optargs, "init arguments (p)", node_init->nd_2nd);
1153
	    }
1129
	if (args->pre_init) { /* m_init */
1130
	    COMPILE_POPED(optargs, "init arguments (m)", args->pre_init);
1131
	}
1132
	if (args->post_init) { /* p_init */
1133
	    COMPILE_POPED(optargs, "init arguments (p)", args->post_init);
1154 1134
	}
1155 1135

  
1156 1136
	if (rest_id) {
gc.c
1710 1710
	  case NODE_CALL:
1711 1711
	  case NODE_DEFS:
1712 1712
	  case NODE_OP_ASGN1:
1713
	  case NODE_ARGS:
1714 1713
	    gc_mark(objspace, (VALUE)obj->as.node.u1.node, lev);
1715 1714
	    /* fall through */
1716 1715
	  case NODE_SUPER:	/* 3 */
......
1778 1777
	    ptr = (VALUE)obj->as.node.u2.node;
1779 1778
	    goto again;
1780 1779

  
1780
	  case NODE_ARGS:	/* custom */
1781
	    {
1782
		struct rb_args_info *args = obj->as.node.u3.args;
1783
		if (args) {
1784
		    if (args->pre_init)  gc_mark(objspace, (VALUE)args->pre_init, lev);
1785
		    if (args->post_init) gc_mark(objspace, (VALUE)args->post_init, lev);
1786
		    if (args->opt_args)  gc_mark(objspace, (VALUE)args->opt_args, lev);
1787
		}
1788
	    }
1789
	    ptr = (VALUE)obj->as.node.u2.node;
1790
	    goto again;
1791

  
1781 1792
	  case NODE_ZARRAY:	/* - */
1782 1793
	  case NODE_ZSUPER:
1783 1794
	  case NODE_VCALL:
......
2371 2382
		xfree(RANY(obj)->as.node.u1.tbl);
2372 2383
	    }
2373 2384
	    break;
2385
	  case NODE_ARGS:
2386
	    if (RANY(obj)->as.node.u3.args) {
2387
		xfree(RANY(obj)->as.node.u3.args);
2388
	    }
2389
	    break;
2374 2390
	  case NODE_ALLOCA:
2375 2391
	    xfree(RANY(obj)->as.node.u1.node);
2376 2392
	    break;
node.c
837 837
	F_NODE(nd_2nd, "post arguments");
838 838
	break;
839 839

  
840
      case NODE_ARGS_AUX:
841
	ANN("method parameters (cont'd)");
842
	F_CUSTOM1(nd_rest, "rest argument", {
843
	    if (node->nd_rest == 1) A("nil (with last comma)");
844
	    else A_ID(node->nd_rest);
845
	});
846
	F_CUSTOM1(nd_body, "block argument", { A_ID((ID)node->nd_body); });
847
	LAST_NODE;
848
	F_CUSTOM2(nd_next, "aux info 2", {
849
	    node = node->nd_next;
850
	    next_indent = "|    ";
851
	    if (!node) {
852
		D_NULL_NODE;
853
	    }
854
	    else {
855
		D_NODE_HEADER(node);
856
		ANN("method parameters (cont'd)");
857
		F_ID(nd_pid, "first post argument");
858
		F_LONG(nd_plen, "post argument length");
859
		LAST_NODE;
860
		F_CUSTOM2(nd_next, "aux info 3", {
861
		    node = node->nd_next;
862
		    next_indent = "|   ";
863
		    if (!node) {
864
			D_NULL_NODE;
865
		    }
866
		    else {
867
			D_NODE_HEADER(node);
868
			ANN("method parameters (cont'd)");
869
			ANN("\"init arguments (m)\" evaluates multiple assignments before rest argument");
870
			ANN("\"init arguments (p)\" evaluates multiple assignments after rest argument");
871
			ANN("example: def foo((m1, m2), *r, (p1, p2))");
872
			F_NODE(nd_1st, "init arguments (m)");
873
			LAST_NODE;
874
			F_NODE(nd_2nd, "init arguments (p)");
875
		    }
876
		});
877
	    }
878
	});
879
	break;
880

  
881 840
      case NODE_ARGS:
882 841
	ANN("method parameters");
883 842
	ANN("format: def method_name(.., [nd_opt=some], *[nd_rest], [nd_pid], .., &[nd_body])");
884 843
	ANN("example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, &blk); end");
885
	F_LONG(nd_frml, "argc");
886
	F_NODE(nd_next, "aux info 1");
887
	LAST_NODE;
888
	F_NODE(nd_opt, "optional arguments");
844
	F_LONG(nd_ainfo->pre_args_num, "count of mandatory (pre-)arguments");
845
	F_NODE(nd_ainfo->pre_init, "initialization of (pre-)arguments");
846
	F_LONG(nd_ainfo->post_args_num, "count of mandatory post-arguments");
847
	F_NODE(nd_ainfo->post_init, "initialization of post-arguments");
848
	F_ID(nd_ainfo->first_post_arg, "first post argument");
849
	F_ID(nd_ainfo->rest_arg, "rest argument");
850
	F_ID(nd_ainfo->block_arg, "block argument");
851
	F_NODE(nd_ainfo->opt_args, "optional arguments");
889 852
	break;
890 853

  
891 854
      case NODE_SCOPE:
node.h
253 253
	ID id;
254 254
	long state;
255 255
	struct rb_global_entry *entry;
256
	struct rb_args_info *args;
256 257
	long cnt;
257 258
	VALUE value;
258 259
    } u3;
......
321 322
#define nd_recv  u1.node
322 323
#define nd_mid   u2.id
323 324
#define nd_args  u3.node
325
#define nd_ainfo u3.args
324 326

  
325 327
#define nd_noex  u3.id
326 328
#define nd_defn  u3.node
......
415 417
#define NEW_VCALL(m) NEW_NODE(NODE_VCALL,0,m,0)
416 418
#define NEW_SUPER(a) NEW_NODE(NODE_SUPER,0,0,a)
417 419
#define NEW_ZSUPER() NEW_NODE(NODE_ZSUPER,0,0,0)
418
#define NEW_ARGS(m,o) NEW_NODE(NODE_ARGS,o,m,0)
419 420
#define NEW_ARGS_AUX(r,b) NEW_NODE(NODE_ARGS_AUX,r,b,0)
420 421
#define NEW_OPT_ARG(i,v) NEW_NODE(NODE_OPT_ARG,i,v,0)
421 422
#define NEW_POSTARG(i,v) NEW_NODE(NODE_POSTARG,i,v,0)
......
483 484
VALUE rb_gvar_defined(struct rb_global_entry *);
484 485
const struct kwtable *rb_reserved_word(const char *, unsigned int);
485 486

  
487
struct rb_args_info {
488
    long pre_args_num;  /* count of mandatory pre-arguments */
489
    NODE *pre_init;
490

  
491
    long post_args_num; /* count of mandatory post-arguments */
492
    NODE *post_init;
493
    ID first_post_arg;
494

  
495
    ID rest_arg;
496
    ID block_arg;
497

  
498
    NODE *opt_args;
499
};
500

  
486 501
struct parser_params;
487 502
void *rb_parser_malloc(struct parser_params *, size_t);
488 503
void *rb_parser_realloc(struct parser_params *, void *, size_t);
parse.y
9069 9069
new_args_gen(struct parser_params *parser, NODE *m, NODE *o, ID r, NODE *p, ID b)
9070 9070
{
9071 9071
    int saved_line = ruby_sourceline;
9072
    NODE *node;
9073
    NODE *i1, *i2 = 0;
9072
    struct rb_args_info *args;
9074 9073

  
9075
    node = NEW_ARGS(m ? m->nd_plen : 0, o);
9076
    i1 = m ? m->nd_next : 0;
9077
    node->nd_next = NEW_ARGS_AUX(r, b);
9074
    args = ALLOC(struct rb_args_info);
9075

  
9076
    args->pre_args_num   = m ? m->nd_plen : 0;
9077
    args->pre_init       = m ? m->nd_next : 0;
9078

  
9079
    args->post_args_num  = p ? p->nd_plen : 0;
9080
    args->post_init      = p ? p->nd_next : 0;
9081
    args->first_post_arg = p ? p->nd_pid : 0;
9082

  
9083
    args->rest_arg       = r;
9084
    args->block_arg      = b;
9085

  
9086
    args->opt_args       = o;
9078 9087

  
9079
    if (p) {
9080
	i2 = p->nd_next;
9081
	node->nd_next->nd_next = NEW_ARGS_AUX(p->nd_pid, p->nd_plen);
9082
    }
9083
    else if (i1) {
9084
	node->nd_next->nd_next = NEW_ARGS_AUX(0, 0);
9085
    }
9086
    if (i1 || i2) {
9087
	node->nd_next->nd_next->nd_next = NEW_NODE(NODE_AND, i1, i2, 0);
9088
    }
9089 9088
    ruby_sourceline = saved_line;
9090
    return node;
9089
    return NEW_NODE(NODE_ARGS, 0, 0, args);
9091 9090
}
9092 9091
#endif /* !RIPPER */
9093 9092

  
9094
-