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