Ruby  2.4.2p198(2017-09-14revision59899)
node.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  node.c - ruby node tree
4 
5  $Author: mame $
6  created at: 09/12/06 21:23:44 JST
7 
8  Copyright (C) 2009 Yusuke Endoh
9 
10 **********************************************************************/
11 
12 #include "ruby/ruby.h"
13 #include "vm_core.h"
14 
15 #define A(str) rb_str_cat2(buf, (str))
16 #define AR(str) rb_str_concat(buf, (str))
17 
18 #define A_INDENT add_indent(buf, indent)
19 #define D_INDENT rb_str_cat2(indent, next_indent)
20 #define D_DEDENT rb_str_resize(indent, RSTRING_LEN(indent) - 4)
21 #define A_ID(id) add_id(buf, (id))
22 #define A_INT(val) rb_str_catf(buf, "%d", (val))
23 #define A_LONG(val) rb_str_catf(buf, "%ld", (val))
24 #define A_LIT(lit) AR(rb_inspect(lit))
25 #define A_NODE_HEADER(node, term) \
26  rb_str_catf(buf, "@ %s (line: %d)"term, ruby_node_name(nd_type(node)), nd_line(node))
27 #define A_FIELD_HEADER(len, name, term) \
28  rb_str_catf(buf, "+- %.*s:"term, (len), (name))
29 #define D_FIELD_HEADER(len, name, term) (A_INDENT, A_FIELD_HEADER(len, name, term))
30 
31 #define D_NULL_NODE (A_INDENT, A("(null node)\n"))
32 #define D_NODE_HEADER(node) (A_INDENT, A_NODE_HEADER(node, "\n"))
33 
34 #define COMPOUND_FIELD(len, name, block) \
35  do { \
36  D_FIELD_HEADER((len), (name), "\n"); \
37  D_INDENT; \
38  block; \
39  D_DEDENT; \
40  } while (0)
41 
42 #define COMPOUND_FIELD1(name, ann, block) \
43  COMPOUND_FIELD(FIELD_NAME_LEN(name, ann), \
44  FIELD_NAME_DESC(name, ann), \
45  block)
46 
47 #define FIELD_NAME_DESC(name, ann) name " (" ann ")"
48 #define FIELD_NAME_LEN(name, ann) (int)( \
49  comment ? \
50  rb_strlen_lit(FIELD_NAME_DESC(name, ann)) : \
51  rb_strlen_lit(name))
52 #define SIMPLE_FIELD(len, name) \
53  for (D_FIELD_HEADER((len), (name), " "), field_flag = 1; \
54  field_flag; /* should be optimized away */ \
55  A("\n"), field_flag = 0)
56 
57 #define SIMPLE_FIELD1(name, ann) SIMPLE_FIELD(FIELD_NAME_LEN(name, ann), FIELD_NAME_DESC(name, ann))
58 #define F_CUSTOM1(name, ann) SIMPLE_FIELD1(#name, ann)
59 #define F_ID(name, ann) SIMPLE_FIELD1(#name, ann) A_ID(node->name)
60 #define F_GENTRY(name, ann) SIMPLE_FIELD1(#name, ann) A_ID((node->name)->id)
61 #define F_INT(name, ann) SIMPLE_FIELD1(#name, ann) A_INT(node->name)
62 #define F_LONG(name, ann) SIMPLE_FIELD1(#name, ann) A_LONG(node->name)
63 #define F_LIT(name, ann) SIMPLE_FIELD1(#name, ann) A_LIT(node->name)
64 #define F_MSG(name, ann, desc) SIMPLE_FIELD1(#name, ann) A(desc)
65 
66 #define F_NODE(name, ann) \
67  COMPOUND_FIELD1(#name, ann, dump_node(buf, indent, comment, node->name))
68 #define F_OPTION(name, ann) \
69  COMPOUND_FIELD1(#name, ann, dump_option(buf, indent, node->name))
70 
71 #define ANN(ann) \
72  if (comment) { \
73  A_INDENT; A("| # " ann "\n"); \
74  }
75 
76 #define LAST_NODE (next_indent = " ")
77 
78 static void
80 {
81  AR(indent);
82 }
83 
84 static void
86 {
87  if (id == 0) {
88  A("(null)");
89  }
90  else {
91  VALUE str = rb_id2str(id);
92  if (str) {
93  A(":"); AR(str);
94  }
95  else {
96  A("(internal variable)");
97  }
98  }
99 }
100 
104 };
105 
106 static int
108 {
109  struct add_option_arg *argp = (void *)args;
110  VALUE buf = argp->buf;
111  VALUE indent = argp->indent;
112 
113  A_INDENT;
114  A("+- ");
115  AR(rb_sym2str(key));
116  A(": ");
117  A_LIT(val);
118  A("\n");
119  return ST_CONTINUE;
120 }
121 
122 static void
124 {
125  struct add_option_arg arg;
126 
127  if (!RB_TYPE_P(opt, T_HASH)) {
128  A_LIT(opt);
129  return;
130  }
131  arg.buf = buf;
132  arg.indent = indent;
133  arg.count = 0;
134  rb_hash_foreach(opt, add_option_i, (VALUE)&arg);
135 }
136 
137 static void dump_node(VALUE, VALUE, int, NODE *);
138 static const char default_indent[] = "| ";
139 
140 static void
141 dump_array(VALUE buf, VALUE indent, int comment, NODE *node)
142 {
143  int field_flag;
144  const char *next_indent = default_indent;
145  F_LONG(nd_alen, "length");
146  F_NODE(nd_head, "element");
147  while (node->nd_next && nd_type(node->nd_next) == NODE_ARRAY) {
148  node = node->nd_next;
149  F_NODE(nd_head, "element");
150  }
151  LAST_NODE;
152  F_NODE(nd_next, "next element");
153 }
154 
155 static void
156 dump_node(VALUE buf, VALUE indent, int comment, NODE *node)
157 {
158  int field_flag;
159  int i;
160  const char *next_indent = default_indent;
161 
162  if (!node) {
163  D_NULL_NODE;
164  return;
165  }
166 
167  D_NODE_HEADER(node);
168 
169  switch (nd_type(node)) {
170  case NODE_BLOCK:
171  ANN("statement sequence");
172  ANN("format: [nd_head]; ...; [nd_next]");
173  ANN("example: foo; bar");
174  i = 0;
175  do {
176  A_INDENT;
177  rb_str_catf(buf, "+- nd_head (%s%d):\n",
178  comment ? "statement #" : "", ++i);
179  if (!node->nd_next) LAST_NODE;
180  D_INDENT;
181  dump_node(buf, indent, comment, node->nd_head);
182  D_DEDENT;
183  } while (node->nd_next &&
184  nd_type(node->nd_next) == NODE_BLOCK &&
185  (node = node->nd_next, 1));
186  if (!node->nd_next) break;
187  LAST_NODE;
188  F_NODE(nd_next, "next block");
189  break;
190 
191  case NODE_IF:
192  ANN("if statement");
193  ANN("format: if [nd_cond] then [nd_body] else [nd_else] end");
194  ANN("example: if x == 1 then foo else bar end");
195  F_NODE(nd_cond, "condition expr");
196  F_NODE(nd_body, "then clause");
197  LAST_NODE;
198  F_NODE(nd_else, "else clause");
199  break;
200 
201  case NODE_CASE:
202  ANN("case statement");
203  ANN("format: case [nd_head]; [nd_body]; end");
204  ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
205  F_NODE(nd_head, "case expr");
206  LAST_NODE;
207  F_NODE(nd_body, "when clauses");
208  break;
209 
210  case NODE_WHEN:
211  ANN("if statement");
212  ANN("format: when [nd_head]; [nd_body]; (when or else) [nd_next]");
213  ANN("example: case x; when 1; foo; when 2; bar; else baz; end");
214  F_NODE(nd_head, "when value");
215  F_NODE(nd_body, "when clause");
216  LAST_NODE;
217  F_NODE(nd_next, "next when clause");
218  break;
219 
220  case NODE_OPT_N:
221  ANN("wrapper for -n option");
222  ANN("format: ruby -ne '[nd_body]' (nd_cond is `gets')");
223  ANN("example: ruby -ne 'p $_'");
224  goto loop;
225  case NODE_WHILE:
226  ANN("while statement");
227  ANN("format: while [nd_cond]; [nd_body]; end");
228  ANN("example: while x == 1; foo; end");
229  goto loop;
230  case NODE_UNTIL:
231  ANN("until statement");
232  ANN("format: until [nd_cond]; [nd_body]; end");
233  ANN("example: until x == 1; foo; end");
234  loop:
235  F_CUSTOM1(nd_state, "begin-end-while?") {
236  A_INT((int)node->nd_state);
237  A((node->nd_state == 1) ? " (while-end)" : " (begin-end-while)");
238  }
239  F_NODE(nd_cond, "condition");
240  LAST_NODE;
241  F_NODE(nd_body, "body");
242  break;
243 
244  case NODE_ITER:
245  ANN("method call with block");
246  ANN("format: [nd_iter] { [nd_body] }");
247  ANN("example: 3.times { foo }");
248  goto iter;
249  case NODE_FOR:
250  ANN("for statement");
251  ANN("format: for * in [nd_iter] do [nd_body] end");
252  ANN("example: for i in 1..3 do foo end");
253  iter:
254  F_NODE(nd_iter, "iteration receiver");
255  LAST_NODE;
256  F_NODE(nd_body, "body");
257  break;
258 
259  case NODE_BREAK:
260  ANN("for statement");
261  ANN("format: break [nd_stts]");
262  ANN("example: break 1");
263  goto jump;
264  case NODE_NEXT:
265  ANN("next statement");
266  ANN("format: next [nd_stts]");
267  ANN("example: next 1");
268  goto jump;
269  case NODE_RETURN:
270  ANN("return statement");
271  ANN("format: return [nd_stts]");
272  ANN("example: return 1");
273  jump:
274  LAST_NODE;
275  F_NODE(nd_stts, "value");
276  break;
277 
278  case NODE_REDO:
279  ANN("redo statement");
280  ANN("format: redo");
281  ANN("example: redo");
282  break;
283 
284  case NODE_RETRY:
285  ANN("retry statement");
286  ANN("format: retry");
287  ANN("example: retry");
288  break;
289 
290  case NODE_BEGIN:
291  ANN("begin statement");
292  ANN("format: begin; [nd_body]; end");
293  ANN("example: begin; 1; end");
294  LAST_NODE;
295  F_NODE(nd_body, "body");
296  break;
297 
298  case NODE_RESCUE:
299  ANN("rescue clause");
300  ANN("format: begin; [nd_body]; (rescue) [nd_resq]; else [nd_else]; end");
301  ANN("example: begin; foo; rescue; bar; else; baz; end");
302  F_NODE(nd_head, "body");
303  F_NODE(nd_resq, "rescue clause list");
304  LAST_NODE;
305  F_NODE(nd_else, "rescue else clause");
306  break;
307 
308  case NODE_RESBODY:
309  ANN("rescue clause (cont'd)");
310  ANN("format: rescue [nd_args]; [nd_body]; (rescue) [nd_head]");
311  ANN("example: begin; foo; rescue; bar; else; baz; end");
312  F_NODE(nd_args, "rescue exceptions");
313  F_NODE(nd_body, "rescue clause");
314  LAST_NODE;
315  F_NODE(nd_head, "next rescue clause");
316  break;
317 
318  case NODE_ENSURE:
319  ANN("ensure clause");
320  ANN("format: begin; [nd_head]; ensure; [nd_ensr]; end");
321  ANN("example: begin; foo; ensure; bar; end");
322  F_NODE(nd_head, "body");
323  LAST_NODE;
324  F_NODE(nd_ensr, "ensure clause");
325  break;
326 
327  case NODE_AND:
328  ANN("&& operator");
329  ANN("format: [nd_1st] && [nd_2nd]");
330  ANN("example: foo && bar");
331  goto andor;
332  case NODE_OR:
333  ANN("|| operator");
334  ANN("format: [nd_1st] || [nd_2nd]");
335  ANN("example: foo && bar");
336  andor:
337  F_NODE(nd_1st, "left expr");
338  LAST_NODE;
339  F_NODE(nd_2nd, "right expr");
340  break;
341 
342  case NODE_MASGN:
343  ANN("multiple assignment");
344  ANN("format: [nd_head], [nd_args] = [nd_value]");
345  ANN("example: a, b = foo");
346  F_NODE(nd_value, "rhsn");
347  F_NODE(nd_head, "lhsn");
348  if ((VALUE)node->nd_args != (VALUE)-1) {
349  LAST_NODE;
350  F_NODE(nd_args, "splatn");
351  }
352  else {
353  F_MSG(nd_args, "splatn", "-1 (rest argument without name)");
354  }
355  break;
356 
357  case NODE_LASGN:
358  ANN("local variable assignment");
359  ANN("format: [nd_vid](lvar) = [nd_value]");
360  ANN("example: x = foo");
361  goto asgn;
362  case NODE_DASGN:
363  ANN("dynamic variable assignment (out of current scope)");
364  ANN("format: [nd_vid](dvar) = [nd_value]");
365  ANN("example: x = nil; 1.times { x = foo }");
366  goto asgn;
367  case NODE_DASGN_CURR:
368  ANN("dynamic variable assignment (in current scope)");
369  ANN("format: [nd_vid](current dvar) = [nd_value]");
370  ANN("example: 1.times { x = foo }");
371  goto asgn;
372  case NODE_IASGN:
373  ANN("instance variable assignment");
374  ANN("format: [nd_vid](ivar) = [nd_value]");
375  ANN("example: @x = foo");
376  goto asgn;
377  case NODE_CVASGN:
378  ANN("class variable assignment");
379  ANN("format: [nd_vid](cvar) = [nd_value]");
380  ANN("example: @@x = foo");
381  asgn:
382  F_ID(nd_vid, "variable");
383  LAST_NODE;
384  if (node->nd_value == (NODE *)-1) {
385  F_MSG(nd_value, "rvalue", "(required keyword argument)");
386  }
387  else {
388  F_NODE(nd_value, "rvalue");
389  }
390  break;
391 
392  case NODE_GASGN:
393  ANN("global variable assignment");
394  ANN("format: [nd_entry](gvar) = [nd_value]");
395  ANN("example: $x = foo");
396  F_GENTRY(nd_entry, "global variable");
397  LAST_NODE;
398  F_NODE(nd_value, "rvalue");
399  break;
400 
401  case NODE_CDECL:
402  ANN("constant declaration");
403  ANN("format: [nd_else]::[nd_vid](constant) = [nd_value]");
404  ANN("example: X = foo");
405  if (node->nd_vid) {
406  F_ID(nd_vid, "variable");
407  F_MSG(nd_else, "extension", "not used");
408  }
409  else {
410  F_MSG(nd_vid, "variable", "0 (see extension field)");
411  F_NODE(nd_else, "extension");
412  }
413  LAST_NODE;
414  F_NODE(nd_value, "rvalue");
415  break;
416 
417  case NODE_OP_ASGN1:
418  ANN("array assignment with operator");
419  ANN("format: [nd_value] [ [nd_args->nd_body] ] [nd_vid]= [nd_args->nd_head]");
420  ANN("example: ary[1] += foo");
421  F_NODE(nd_recv, "receiver");
422  F_ID(nd_vid, "operator");
423  F_NODE(nd_args->nd_body, "index");
424  LAST_NODE;
425  F_NODE(nd_args->nd_head, "rvalue");
426  break;
427 
428  case NODE_OP_ASGN2:
429  ANN("attr assignment with operator");
430  ANN("format: [nd_value].[attr] [nd_next->nd_mid]= [nd_value]");
431  ANN(" where [attr]: [nd_next->nd_vid]");
432  ANN("example: struct.field += foo");
433  F_NODE(nd_recv, "receiver");
434  F_CUSTOM1(nd_next->nd_vid, "attr") {
435  if (node->nd_next->nd_aid) A("? ");
436  A_ID(node->nd_next->nd_vid);
437  }
438  F_CUSTOM1(nd_next->nd_mid, "operator") {
439  switch (node->nd_next->nd_mid) {
440  case 0: A("0 (||)"); break;
441  case 1: A("1 (&&)"); break;
442  default: A_ID(node->nd_next->nd_mid);
443  }
444  }
445  LAST_NODE;
446  F_NODE(nd_value, "rvalue");
447  break;
448 
449  case NODE_OP_ASGN_AND:
450  ANN("assignment with && operator");
451  ANN("format: [nd_head] &&= [nd_value]");
452  ANN("example: foo &&= bar");
453  goto asgn_andor;
454  case NODE_OP_ASGN_OR:
455  ANN("assignment with || operator");
456  ANN("format: [nd_head] ||= [nd_value]");
457  ANN("example: foo ||= bar");
458  asgn_andor:
459  F_NODE(nd_head, "variable");
460  LAST_NODE;
461  F_NODE(nd_value, "rvalue");
462  break;
463 
464  case NODE_CALL:
465  ANN("method invocation");
466  ANN("format: [nd_recv].[nd_mid]([nd_args])");
467  ANN("example: obj.foo(1)");
468  F_ID(nd_mid, "method id");
469  F_NODE(nd_recv, "receiver");
470  LAST_NODE;
471  F_NODE(nd_args, "arguments");
472  break;
473 
474  case NODE_FCALL:
475  ANN("function call");
476  ANN("format: [nd_mid]([nd_args])");
477  ANN("example: foo(1)");
478  F_ID(nd_mid, "method id");
479  LAST_NODE;
480  F_NODE(nd_args, "arguments");
481  break;
482 
483  case NODE_VCALL:
484  ANN("function call with no argument");
485  ANN("format: [nd_mid]");
486  ANN("example: foo");
487  F_ID(nd_mid, "method id");
488  break;
489 
490  case NODE_QCALL:
491  ANN("safe method invocation");
492  ANN("format: [nd_recv]&.[nd_mid]([nd_args])");
493  ANN("example: obj&.foo(1)");
494  F_ID(nd_mid, "method id");
495  F_NODE(nd_recv, "receiver");
496  LAST_NODE;
497  F_NODE(nd_args, "arguments");
498  break;
499 
500  case NODE_SUPER:
501  ANN("super invocation");
502  ANN("format: super [nd_args]");
503  ANN("example: super 1");
504  LAST_NODE;
505  F_NODE(nd_args, "arguments");
506  break;
507 
508  case NODE_ZSUPER:
509  ANN("super invocation with no argument");
510  ANN("format: super");
511  ANN("example: super");
512  break;
513 
514  case NODE_ARRAY:
515  ANN("array constructor");
516  ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
517  ANN("example: [1, 2, 3]");
518  goto ary;
519  case NODE_VALUES:
520  ANN("return arguments");
521  ANN("format: [ [nd_head], [nd_next].. ] (length: [nd_alen])");
522  ANN("example: return 1, 2, 3");
523  ary:
524  dump_array(buf, indent, comment, node);
525  break;
526 
527  case NODE_ZARRAY:
528  ANN("empty array constructor");
529  ANN("format: []");
530  ANN("example: []");
531  break;
532 
533  case NODE_HASH:
534  ANN("hash constructor");
535  ANN("format: { [nd_head] }");
536  ANN("example: { 1 => 2, 3 => 4 }");
537  LAST_NODE;
538  F_NODE(nd_head, "contents");
539  break;
540 
541  case NODE_YIELD:
542  ANN("yield invocation");
543  ANN("format: yield [nd_head]");
544  ANN("example: yield 1");
545  LAST_NODE;
546  F_NODE(nd_head, "arguments");
547  break;
548 
549  case NODE_LVAR:
550  ANN("local variable reference");
551  ANN("format: [nd_vid](lvar)");
552  ANN("example: x");
553  goto var;
554  case NODE_DVAR:
555  ANN("dynamic variable reference");
556  ANN("format: [nd_vid](dvar)");
557  ANN("example: 1.times { x = 1; x }");
558  goto var;
559  case NODE_IVAR:
560  ANN("instance variable reference");
561  ANN("format: [nd_vid](ivar)");
562  ANN("example: @x");
563  goto var;
564  case NODE_CONST:
565  ANN("constant reference");
566  ANN("format: [nd_vid](constant)");
567  ANN("example: X");
568  goto var;
569  case NODE_CVAR:
570  ANN("class variable reference");
571  ANN("format: [nd_vid](cvar)");
572  ANN("example: @@x");
573  var:
574  F_ID(nd_vid, "local variable");
575  break;
576 
577  case NODE_GVAR:
578  ANN("global variable reference");
579  ANN("format: [nd_entry](gvar)");
580  ANN("example: $x");
581  F_GENTRY(nd_entry, "global variable");
582  break;
583 
584  case NODE_NTH_REF:
585  ANN("nth special variable reference");
586  ANN("format: $[nd_nth]");
587  ANN("example: $1, $2, ..");
588  F_CUSTOM1(nd_nth, "variable") { A("$"); A_LONG(node->nd_nth); }
589  break;
590 
591  case NODE_BACK_REF:
592  ANN("back special variable reference");
593  ANN("format: $[nd_nth]");
594  ANN("example: $&, $`, $', $+");
595  F_CUSTOM1(nd_nth, "variable") {
596  char name[3];
597  name[0] = '$';
598  name[1] = (char)node->nd_nth;
599  name[2] = '\0';
600  A(name);
601  }
602  break;
603 
604  case NODE_MATCH:
605  ANN("match expression (against $_ implicitly)");
606  ANN("format: [nd_lit] (in condition)");
607  ANN("example: if /foo/; foo; end");
608  F_LIT(nd_lit, "regexp");
609  break;
610 
611  case NODE_MATCH2:
612  ANN("match expression (regexp first)");
613  ANN("format: [nd_recv] =~ [nd_value]");
614  ANN("example: /foo/ =~ 'foo'");
615  F_NODE(nd_recv, "regexp (receiver)");
616  if (!node->nd_args) LAST_NODE;
617  F_NODE(nd_value, "string (argument)");
618  if (node->nd_args) {
619  LAST_NODE;
620  F_NODE(nd_args, "named captures");
621  }
622  break;
623 
624  case NODE_MATCH3:
625  ANN("match expression (regexp second)");
626  ANN("format: [nd_recv] =~ [nd_value]");
627  ANN("example: 'foo' =~ /foo/");
628  F_NODE(nd_recv, "string (receiver)");
629  LAST_NODE;
630  F_NODE(nd_value, "regexp (argument)");
631  break;
632 
633  case NODE_LIT:
634  ANN("literal");
635  ANN("format: [nd_lit]");
636  ANN("example: 1, /foo/");
637  goto lit;
638  case NODE_STR:
639  ANN("string literal");
640  ANN("format: [nd_lit]");
641  ANN("example: 'foo'");
642  goto lit;
643  case NODE_XSTR:
644  ANN("xstring literal");
645  ANN("format: [nd_lit]");
646  ANN("example: `foo`");
647  lit:
648  F_LIT(nd_lit, "literal");
649  break;
650 
651  case NODE_DSTR:
652  ANN("string literal with interpolation");
653  ANN("format: [nd_lit]");
654  ANN("example: \"foo#{ bar }baz\"");
655  goto dlit;
656  case NODE_DXSTR:
657  ANN("xstring literal with interpolation");
658  ANN("format: [nd_lit]");
659  ANN("example: `foo#{ bar }baz`");
660  goto dlit;
661  case NODE_DREGX:
662  ANN("regexp literal with interpolation");
663  ANN("format: [nd_lit]");
664  ANN("example: /foo#{ bar }baz/");
665  goto dlit;
666  case NODE_DREGX_ONCE:
667  ANN("regexp literal with interpolation and once flag");
668  ANN("format: [nd_lit]");
669  ANN("example: /foo#{ bar }baz/o");
670  goto dlit;
671  case NODE_DSYM:
672  ANN("symbol literal with interpolation");
673  ANN("format: [nd_lit]");
674  ANN("example: :\"foo#{ bar }baz\"");
675  dlit:
676  F_LIT(nd_lit, "preceding string");
677  F_NODE(nd_next->nd_head, "interpolation");
678  LAST_NODE;
679  F_NODE(nd_next->nd_next, "tailing strings");
680  break;
681 
682  case NODE_EVSTR:
683  ANN("interpolation expression");
684  ANN("format: \"..#{ [nd_lit] }..\"");
685  ANN("example: \"foo#{ bar }baz\"");
686  LAST_NODE;
687  F_NODE(nd_body, "body");
688  break;
689 
690  case NODE_ARGSCAT:
691  ANN("splat argument following arguments");
692  ANN("format: ..(*[nd_head], [nd_body..])");
693  ANN("example: foo(*ary, post_arg1, post_arg2)");
694  F_NODE(nd_head, "preceding array");
695  LAST_NODE;
696  F_NODE(nd_body, "following array");
697  break;
698 
699  case NODE_ARGSPUSH:
700  ANN("splat argument following one argument");
701  ANN("format: ..(*[nd_head], [nd_body])");
702  ANN("example: foo(*ary, post_arg)");
703  F_NODE(nd_head, "preceding array");
704  LAST_NODE;
705  F_NODE(nd_body, "following element");
706  break;
707 
708  case NODE_SPLAT:
709  ANN("splat argument");
710  ANN("format: *[nd_head]");
711  ANN("example: foo(*ary)");
712  LAST_NODE;
713  F_NODE(nd_head, "splat'ed array");
714  break;
715 
716  case NODE_BLOCK_PASS:
717  ANN("arguments with block argument");
718  ANN("format: ..([nd_head], &[nd_body])");
719  ANN("example: foo(x, &blk)");
720  F_NODE(nd_head, "other arguments");
721  LAST_NODE;
722  F_NODE(nd_body, "block argument");
723  break;
724 
725  case NODE_DEFN:
726  ANN("method definition");
727  ANN("format: def [nd_mid] [nd_defn]; end");
728  ANN("example; def foo; bar; end");
729  F_ID(nd_mid, "method name");
730  LAST_NODE;
731  F_NODE(nd_defn, "method definition");
732  break;
733 
734  case NODE_DEFS:
735  ANN("singleton method definition");
736  ANN("format: def [nd_recv].[nd_mid] [nd_defn]; end");
737  ANN("example; def obj.foo; bar; end");
738  F_NODE(nd_recv, "receiver");
739  F_ID(nd_mid, "method name");
740  LAST_NODE;
741  F_NODE(nd_defn, "method definition");
742  break;
743 
744  case NODE_ALIAS:
745  ANN("method alias statement");
746  ANN("format: alias [u1.node] [u2.node]");
747  ANN("example: alias bar foo");
748  F_NODE(u1.node, "new name");
749  LAST_NODE;
750  F_NODE(u2.node, "old name");
751  break;
752 
753  case NODE_VALIAS:
754  ANN("global variable alias statement");
755  ANN("format: alias [u1.id](gvar) [u2.id](gvar)");
756  ANN("example: alias $y $x");
757  F_ID(u1.id, "new name");
758  F_ID(u2.id, "old name");
759  break;
760 
761  case NODE_UNDEF:
762  ANN("method alias statement");
763  ANN("format: undef [u2.node]");
764  ANN("example: undef foo");
765  LAST_NODE;
766  F_NODE(u2.node, "old name");
767  break;
768 
769  case NODE_CLASS:
770  ANN("class definition");
771  ANN("format: class [nd_cpath] < [nd_super]; [nd_body]; end");
772  ANN("example: class C2 < C; ..; end");
773  F_NODE(nd_cpath, "class path");
774  F_NODE(nd_super, "superclass");
775  LAST_NODE;
776  F_NODE(nd_body, "class definition");
777  break;
778 
779  case NODE_MODULE:
780  ANN("module definition");
781  ANN("format: module [nd_cpath]; [nd_body]; end");
782  ANN("example: module M; ..; end");
783  F_NODE(nd_cpath, "module path");
784  LAST_NODE;
785  F_NODE(nd_body, "module definition");
786  break;
787 
788  case NODE_SCLASS:
789  ANN("singleton class definition");
790  ANN("format: class << [nd_recv]; [nd_body]; end");
791  ANN("example: class << obj; ..; end");
792  F_NODE(nd_recv, "receiver");
793  LAST_NODE;
794  F_NODE(nd_body, "singleton class definition");
795  break;
796 
797  case NODE_COLON2:
798  ANN("scoped constant reference");
799  ANN("format: [nd_head]::[nd_mid]");
800  ANN("example: M::C");
801  F_ID(nd_mid, "constant name");
802  LAST_NODE;
803  F_NODE(nd_head, "receiver");
804  break;
805 
806  case NODE_COLON3:
807  ANN("top-level constant reference");
808  ANN("format: ::[nd_mid]");
809  ANN("example: ::Object");
810  F_ID(nd_mid, "constant name");
811  break;
812 
813  case NODE_DOT2:
814  ANN("range constructor (incl.)");
815  ANN("format: [nd_beg]..[nd_end]");
816  ANN("example: 1..5");
817  goto dot;
818  case NODE_DOT3:
819  ANN("range constructor (excl.)");
820  ANN("format: [nd_beg]...[nd_end]");
821  ANN("example: 1...5");
822  goto dot;
823  case NODE_FLIP2:
824  ANN("flip-flop condition (incl.)");
825  ANN("format: [nd_beg]..[nd_end]");
826  ANN("example: if (x==1)..(x==5); foo; end");
827  goto dot;
828  case NODE_FLIP3:
829  ANN("flip-flop condition (excl.)");
830  ANN("format: [nd_beg]...[nd_end]");
831  ANN("example: if (x==1)...(x==5); foo; end");
832  dot:
833  F_NODE(nd_beg, "begin");
834  LAST_NODE;
835  F_NODE(nd_end, "end");
836  break;
837 
838  case NODE_SELF:
839  ANN("self");
840  ANN("format: self");
841  ANN("example: self");
842  break;
843 
844  case NODE_NIL:
845  ANN("nil");
846  ANN("format: nil");
847  ANN("example: nil");
848  break;
849 
850  case NODE_TRUE:
851  ANN("true");
852  ANN("format: true");
853  ANN("example: true");
854  break;
855 
856  case NODE_FALSE:
857  ANN("false");
858  ANN("format: false");
859  ANN("example: false");
860  break;
861 
862  case NODE_ERRINFO:
863  ANN("virtual reference to $!");
864  ANN("format: rescue => id");
865  ANN("example: rescue => id");
866  break;
867 
868  case NODE_DEFINED:
869  ANN("defined? expression");
870  ANN("format: defined?([nd_head])");
871  ANN("example: defined?(foo)");
872  F_NODE(nd_head, "expr");
873  break;
874 
875  case NODE_POSTEXE:
876  ANN("post-execution");
877  ANN("format: END { [nd_body] }");
878  ANN("example: END { foo }");
879  LAST_NODE;
880  F_NODE(nd_body, "END clause");
881  break;
882 
883  case NODE_ATTRASGN:
884  ANN("attr assignment");
885  ANN("format: [nd_recv].[nd_mid] = [nd_args]");
886  ANN("example: struct.field = foo");
887  if (node->nd_recv == (NODE *) 1) {
888  F_MSG(nd_recv, "receiver", "1 (self)");
889  }
890  else {
891  F_NODE(nd_recv, "receiver");
892  }
893  F_ID(nd_mid, "method name");
894  LAST_NODE;
895  F_NODE(nd_args, "arguments");
896  break;
897 
898  case NODE_PRELUDE:
899  ANN("pre-execution");
900  ANN("format: BEGIN { [nd_head] }; [nd_body]");
901  ANN("example: bar; BEGIN { foo }");
902 #define nd_compile_option u3.value
903  F_NODE(nd_head, "prelude");
904  if (!node->nd_compile_option) LAST_NODE;
905  F_NODE(nd_body, "body");
906  if (node->nd_compile_option) {
907  LAST_NODE;
908  F_OPTION(nd_compile_option, "compile_option");
909  }
910  break;
911 
912  case NODE_LAMBDA:
913  ANN("lambda expression");
914  ANN("format: -> [nd_body]");
915  ANN("example: -> { foo }");
916  LAST_NODE;
917  F_NODE(nd_body, "lambda clause");
918  break;
919 
920  case NODE_OPT_ARG:
921  ANN("optional arguments");
922  ANN("format: def method_name([nd_body=some], [nd_next..])");
923  ANN("example: def foo(a, b=1, c); end");
924  F_NODE(nd_body, "body");
925  LAST_NODE;
926  F_NODE(nd_next, "next");
927  break;
928 
929  case NODE_KW_ARG:
930  ANN("keyword arguments");
931  ANN("format: def method_name([nd_body=some], [nd_next..])");
932  ANN("example: def foo(a:1, b:2); end");
933  F_NODE(nd_body, "body");
934  LAST_NODE;
935  F_NODE(nd_next, "next");
936  break;
937 
938  case NODE_POSTARG:
939  ANN("post arguments");
940  ANN("format: *[nd_1st], [nd_2nd..] = ..");
941  ANN("example: a, *rest, z = foo");
942  if ((VALUE)node->nd_1st != (VALUE)-1) {
943  F_NODE(nd_1st, "rest argument");
944  }
945  else {
946  F_MSG(nd_1st, "rest argument", "-1 (rest argument without name)");
947  }
948  LAST_NODE;
949  F_NODE(nd_2nd, "post arguments");
950  break;
951 
952  case NODE_ARGS:
953  ANN("method parameters");
954  ANN("format: def method_name(.., [nd_opt=some], *[nd_rest], [nd_pid], .., &[nd_body])");
955  ANN("example: def foo(a, b, opt1=1, opt2=2, *rest, y, z, &blk); end");
956  F_INT(nd_ainfo->pre_args_num, "count of mandatory (pre-)arguments");
957  F_NODE(nd_ainfo->pre_init, "initialization of (pre-)arguments");
958  F_INT(nd_ainfo->post_args_num, "count of mandatory post-arguments");
959  F_NODE(nd_ainfo->post_init, "initialization of post-arguments");
960  F_ID(nd_ainfo->first_post_arg, "first post argument");
961  F_ID(nd_ainfo->rest_arg, "rest argument");
962  F_ID(nd_ainfo->block_arg, "block argument");
963  F_NODE(nd_ainfo->opt_args, "optional arguments");
964  LAST_NODE;
965  F_NODE(nd_ainfo->kw_args, "keyword arguments");
966  F_NODE(nd_ainfo->kw_rest_arg, "keyword rest argument");
967  break;
968 
969  case NODE_SCOPE:
970  ANN("new scope");
971  ANN("format: [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body");
972  F_CUSTOM1(nd_tbl, "local table") {
973  ID *tbl = node->nd_tbl;
974  int i;
975  int size = tbl ? (int)*tbl++ : 0;
976  if (size == 0) A("(empty)");
977  for (i = 0; i < size; i++) {
978  A_ID(tbl[i]); if (i < size - 1) A(",");
979  }
980  }
981  F_NODE(nd_args, "arguments");
982  LAST_NODE;
983  F_NODE(nd_body, "body");
984  break;
985 
986  default:
987  rb_bug("dump_node: unknown node: %s", ruby_node_name(nd_type(node)));
988  }
989 }
990 
991 VALUE
992 rb_parser_dump_tree(NODE *node, int comment)
993 {
995  "###########################################################\n"
996  "## Do NOT use this node dump for any purpose other than ##\n"
997  "## debug and research. Compatibility is not guaranteed. ##\n"
998  "###########################################################\n\n"
999  );
1000  dump_node(buf, rb_str_new_cstr("# "), comment, node);
1001  return buf;
1002 }
1003 
1004 void
1006 {
1007  switch (nd_type(obj)) {
1008  case NODE_SCOPE:
1009  if (RNODE(obj)->nd_tbl) {
1010  xfree(RNODE(obj)->nd_tbl);
1011  }
1012  break;
1013  case NODE_ARGS:
1014  if (RNODE(obj)->nd_ainfo) {
1015  xfree(RNODE(obj)->nd_ainfo);
1016  }
1017  break;
1018  case NODE_ALLOCA:
1019  xfree(RNODE(obj)->u1.node);
1020  break;
1021  }
1022 }
1023 
1024 size_t
1026 {
1027  size_t size = 0;
1028  switch (nd_type(obj)) {
1029  case NODE_SCOPE:
1030  if (RNODE(obj)->nd_tbl) {
1031  size += (RNODE(obj)->nd_tbl[0]+1) * sizeof(*RNODE(obj)->nd_tbl);
1032  }
1033  break;
1034  case NODE_ARGS:
1035  if (RNODE(obj)->nd_ainfo) {
1036  size += sizeof(*RNODE(obj)->nd_ainfo);
1037  }
1038  break;
1039  case NODE_ALLOCA:
1040  size += RNODE(obj)->nd_cnt * sizeof(VALUE);
1041  break;
1042  }
1043  return size;
1044 }
1045 
1046 VALUE
1048 {
1049  switch (nd_type(obj)) {
1050  case NODE_IF: /* 1,2,3 */
1051  case NODE_FOR:
1052  case NODE_ITER:
1053  case NODE_WHEN:
1054  case NODE_MASGN:
1055  case NODE_RESCUE:
1056  case NODE_RESBODY:
1057  case NODE_CLASS:
1058  case NODE_BLOCK_PASS:
1059  case NODE_MATCH2:
1060  rb_gc_mark(RNODE(obj)->u2.value);
1061  /* fall through */
1062  case NODE_BLOCK: /* 1,3 */
1063  case NODE_ARRAY:
1064  case NODE_DSTR:
1065  case NODE_DXSTR:
1066  case NODE_DREGX:
1067  case NODE_DREGX_ONCE:
1068  case NODE_ENSURE:
1069  case NODE_CALL:
1070  case NODE_DEFS:
1071  case NODE_OP_ASGN1:
1072  rb_gc_mark(RNODE(obj)->u1.value);
1073  /* fall through */
1074  case NODE_SUPER: /* 3 */
1075  case NODE_FCALL:
1076  case NODE_DEFN:
1077  case NODE_ARGS_AUX:
1078  return RNODE(obj)->u3.value;
1079 
1080  case NODE_WHILE: /* 1,2 */
1081  case NODE_UNTIL:
1082  case NODE_AND:
1083  case NODE_OR:
1084  case NODE_CASE:
1085  case NODE_SCLASS:
1086  case NODE_DOT2:
1087  case NODE_DOT3:
1088  case NODE_FLIP2:
1089  case NODE_FLIP3:
1090  case NODE_MATCH3:
1091  case NODE_OP_ASGN_OR:
1092  case NODE_OP_ASGN_AND:
1093  case NODE_MODULE:
1094  case NODE_ALIAS:
1095  case NODE_VALIAS:
1096  case NODE_ARGSCAT:
1097  rb_gc_mark(RNODE(obj)->u1.value);
1098  /* fall through */
1099  case NODE_GASGN: /* 2 */
1100  case NODE_LASGN:
1101  case NODE_DASGN:
1102  case NODE_DASGN_CURR:
1103  case NODE_IASGN:
1104  case NODE_IASGN2:
1105  case NODE_CVASGN:
1106  case NODE_COLON3:
1107  case NODE_OPT_N:
1108  case NODE_EVSTR:
1109  case NODE_UNDEF:
1110  case NODE_POSTEXE:
1111  return RNODE(obj)->u2.value;
1112 
1113  case NODE_HASH: /* 1 */
1114  case NODE_LIT:
1115  case NODE_STR:
1116  case NODE_XSTR:
1117  case NODE_DEFINED:
1118  case NODE_MATCH:
1119  case NODE_RETURN:
1120  case NODE_BREAK:
1121  case NODE_NEXT:
1122  case NODE_YIELD:
1123  case NODE_COLON2:
1124  case NODE_SPLAT:
1125  case NODE_TO_ARY:
1126  return RNODE(obj)->u1.value;
1127 
1128  case NODE_SCOPE: /* 2,3 */
1129  case NODE_CDECL:
1130  case NODE_OPT_ARG:
1131  rb_gc_mark(RNODE(obj)->u3.value);
1132  return RNODE(obj)->u2.value;
1133 
1134  case NODE_ARGS: /* custom */
1135  {
1136  struct rb_args_info *args = obj->u3.args;
1137  if (args) {
1138  if (args->pre_init) rb_gc_mark((VALUE)args->pre_init);
1139  if (args->post_init) rb_gc_mark((VALUE)args->post_init);
1140  if (args->opt_args) rb_gc_mark((VALUE)args->opt_args);
1141  if (args->kw_args) rb_gc_mark((VALUE)args->kw_args);
1142  if (args->kw_rest_arg) rb_gc_mark((VALUE)args->kw_rest_arg);
1143  }
1144  }
1145  return RNODE(obj)->u2.value;
1146 
1147  case NODE_ZARRAY: /* - */
1148  case NODE_ZSUPER:
1149  case NODE_VCALL:
1150  case NODE_GVAR:
1151  case NODE_LVAR:
1152  case NODE_DVAR:
1153  case NODE_IVAR:
1154  case NODE_CVAR:
1155  case NODE_NTH_REF:
1156  case NODE_BACK_REF:
1157  case NODE_REDO:
1158  case NODE_RETRY:
1159  case NODE_SELF:
1160  case NODE_NIL:
1161  case NODE_TRUE:
1162  case NODE_FALSE:
1163  case NODE_ERRINFO:
1164  case NODE_BLOCK_ARG:
1165  break;
1166  case NODE_ALLOCA:
1167  rb_gc_mark_locations((VALUE*)RNODE(obj)->u1.value,
1168  (VALUE*)RNODE(obj)->u1.value + RNODE(obj)->u3.cnt);
1169  rb_gc_mark(RNODE(obj)->u2.value);
1170  break;
1171 
1172  default: /* unlisted NODE */
1173  rb_gc_mark_maybe(RNODE(obj)->u1.value);
1174  rb_gc_mark_maybe(RNODE(obj)->u2.value);
1175  rb_gc_mark_maybe(RNODE(obj)->u3.value);
1176  }
1177  return 0;
1178 }
#define nd_recv
Definition: node.h:327
#define D_INDENT
Definition: node.c:19
Definition: node.h:93
Definition: node.h:29
#define D_NULL_NODE
Definition: node.c:31
#define nd_alen
Definition: node.h:287
VALUE rb_parser_dump_tree(NODE *node, int comment)
Definition: node.c:992
void rb_bug(const char *fmt,...)
Definition: error.c:482
#define nd_stts
Definition: node.h:302
Definition: node.h:47
#define nd_super
Definition: node.h:339
#define F_NODE(name, ann)
Definition: node.c:66
static void dump_option(VALUE buf, VALUE indent, VALUE opt)
Definition: node.c:123
#define D_NODE_HEADER(node)
Definition: node.c:32
static void add_id(VALUE buf, ID id)
Definition: node.c:85
void rb_gc_free_node(VALUE obj)
Definition: node.c:1005
#define nd_entry
Definition: node.h:304
#define F_CUSTOM1(name, ann)
Definition: node.c:58
#define rb_id2str(id)
Definition: vm_backtrace.c:29
Definition: st.h:99
static void dump_node(VALUE, VALUE, int, NODE *)
Definition: node.c:156
Definition: node.h:39
union RNode::@149 u3
#define D_DEDENT
Definition: node.c:20
#define nd_tbl
Definition: node.h:311
#define nd_end
Definition: node.h:345
#define nd_cond
Definition: node.h:290
#define T_HASH
Definition: ruby.h:499
#define nd_args
Definition: node.h:329
void rb_gc_mark(VALUE ptr)
Definition: gc.c:4394
st_data_t st_index_t
Definition: st.h:50
st_index_t count
Definition: node.c:103
NODE * pre_init
Definition: node.h:487
#define nd_type(n)
Definition: node.h:274
void rb_gc_mark_locations(const VALUE *start, const VALUE *end)
Definition: gc.c:4008
#define LAST_NODE
Definition: node.c:76
#define A_LONG(val)
Definition: node.c:23
Definition: node.h:27
Definition: node.h:235
void rb_hash_foreach(VALUE hash, int(*func)(ANYARGS), VALUE farg)
Definition: hash.c:402
#define RB_TYPE_P(obj, type)
Definition: ruby.h:527
#define F_GENTRY(name, ann)
Definition: node.c:60
#define A_INDENT
Definition: node.c:18
#define F_ID(name, ann)
Definition: node.c:59
VALUE buf
Definition: node.c:102
#define val
static int add_option_i(VALUE key, VALUE val, VALUE args)
Definition: node.c:107
static void add_indent(VALUE buf, VALUE indent)
Definition: node.c:79
NODE * opt_args
Definition: node.h:501
#define F_LONG(name, ann)
Definition: node.c:62
Definition: node.h:59
VALUE rb_gc_mark_node(NODE *obj)
Definition: node.c:1047
#define RNODE(obj)
Definition: node.h:262
#define nd_beg
Definition: node.h:344
#define nd_else
Definition: node.h:292
#define F_LIT(name, ann)
Definition: node.c:63
#define nd_2nd
Definition: node.h:300
#define A_ID(id)
Definition: node.c:21
#define nd_next
Definition: node.h:288
#define nd_defn
Definition: node.h:333
#define nd_cpath
Definition: node.h:338
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4309
unsigned long ID
Definition: ruby.h:86
#define nd_resq
Definition: node.h:296
VALUE indent
Definition: node.c:102
unsigned long VALUE
Definition: ruby.h:85
#define F_MSG(name, ann, desc)
Definition: node.c:64
NODE * post_init
Definition: node.h:488
VALUE rb_str_new_cstr(const char *)
Definition: string.c:770
#define A_INT(val)
Definition: node.c:22
#define nd_body
Definition: node.h:291
#define nd_state
Definition: node.h:346
static const char default_indent[]
Definition: node.c:138
#define A(str)
Definition: node.c:15
#define nd_ensr
Definition: node.h:297
size_t rb_node_memsize(VALUE obj)
Definition: node.c:1025
#define nd_compile_option
static void dump_array(VALUE buf, VALUE indent, int comment, NODE *node)
Definition: node.c:141
int size
Definition: encoding.c:57
Definition: node.h:45
Definition: node.h:207
#define F_OPTION(name, ann)
Definition: node.c:68
const char * ruby_node_name(int node)
Definition: iseq.c:1739
VALUE rb_str_catf(VALUE str, const char *format,...)
Definition: sprintf.c:1480
#define nd_head
Definition: node.h:286
Definition: node.h:141
#define A_LIT(lit)
Definition: node.c:24
#define F_INT(name, ann)
Definition: node.c:61
#define nd_ainfo
Definition: node.h:330
#define ANN(ann)
Definition: node.c:71
#define nd_nth
Definition: node.h:349
void rb_gc_mark_maybe(VALUE obj)
Definition: gc.c:4247
const char * name
Definition: nkf.c:208
Definition: node.h:61
#define nd_value
Definition: node.h:316
struct rb_args_info * args
Definition: node.h:256
Definition: node.h:31
#define nd_mid
Definition: node.h:328
void void xfree(void *)
Definition: node.h:41
#define AR(str)
Definition: node.c:16
NODE * kw_args
Definition: node.h:498
#define nd_iter
Definition: node.h:314
NODE * kw_rest_arg
Definition: node.h:499
#define nd_lit
Definition: node.h:319
Definition: node.h:139
#define nd_vid
Definition: node.h:305
#define rb_sym2str(sym)
Definition: console.c:107
#define nd_1st
Definition: node.h:299