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