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