Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

ast.c
Go to the documentation of this file.
1 /* indent-tabs-mode: nil */
2 #include "ruby.h"
3 #include "ruby/encoding.h"
4 #include "ruby/util.h"
5 #include "internal.h"
6 #include "node.h"
7 #include "vm_core.h"
8 #include "iseq.h"
9 #include "builtin.h"
10
11 static VALUE rb_mAST;
12 static VALUE rb_cNode;
13
14 struct ASTNodeData {
15 rb_ast_t *ast;
16 NODE *node;
17 };
18
19 static void
20 node_gc_mark(void *ptr)
21 {
22 struct ASTNodeData *data = (struct ASTNodeData *)ptr;
23 rb_gc_mark((VALUE)data->ast);
24 }
25
26 static size_t
27 node_memsize(const void *ptr)
28 {
29 struct ASTNodeData *data = (struct ASTNodeData *)ptr;
30 return rb_ast_memsize(data->ast);
31 }
32
33 static const rb_data_type_t rb_node_type = {
34 "AST/node",
35 {node_gc_mark, RUBY_TYPED_DEFAULT_FREE, node_memsize,},
36 0, 0,
37 RUBY_TYPED_FREE_IMMEDIATELY,
38 };
39
40 static VALUE rb_ast_node_alloc(VALUE klass);
41
42 static void
43 setup_node(VALUE obj, rb_ast_t *ast, NODE *node)
44 {
45 struct ASTNodeData *data;
46
47 TypedData_Get_Struct(obj, struct ASTNodeData, &rb_node_type, data);
48 data->ast = ast;
49 data->node = node;
50 }
51
52 static VALUE
53 ast_new_internal(rb_ast_t *ast, NODE *node)
54 {
55 VALUE obj;
56
57 obj = rb_ast_node_alloc(rb_cNode);
58 setup_node(obj, ast, node);
59
60 return obj;
61 }
62
63 static VALUE rb_ast_parse_str(VALUE str);
64 static VALUE rb_ast_parse_file(VALUE path);
65 static VALUE rb_ast_parse_array(VALUE array);
66
67 static VALUE
68 ast_parse_new(void)
69 {
70 return rb_parser_set_context(rb_parser_new(), NULL, 0);
71 }
72
73 static VALUE
74 ast_parse_done(rb_ast_t *ast)
75 {
76 if (!ast->body.root) {
77 rb_ast_dispose(ast);
78 rb_exc_raise(GET_EC()->errinfo);
79 }
80
81 return ast_new_internal(ast, (NODE *)ast->body.root);
82 }
83
84 static VALUE
85 ast_s_parse(rb_execution_context_t *ec, VALUE module, VALUE str)
86 {
87 return rb_ast_parse_str(str);
88 }
89
90 static VALUE
91 rb_ast_parse_str(VALUE str)
92 {
93 rb_ast_t *ast = 0;
94
95 StringValue(str);
96 ast = rb_parser_compile_string_path(ast_parse_new(), Qnil, str, 1);
97 return ast_parse_done(ast);
98 }
99
100 static VALUE
101 ast_s_parse_file(rb_execution_context_t *ec, VALUE module, VALUE path)
102 {
103 return rb_ast_parse_file(path);
104 }
105
106 static VALUE
107 rb_ast_parse_file(VALUE path)
108 {
109 VALUE f;
110 rb_ast_t *ast = 0;
111 rb_encoding *enc = rb_utf8_encoding();
112
113 FilePathValue(path);
114 f = rb_file_open_str(path, "r");
115 rb_funcall(f, rb_intern("set_encoding"), 2, rb_enc_from_encoding(enc), rb_str_new_cstr("-"));
116 ast = rb_parser_compile_file_path(ast_parse_new(), Qnil, f, 1);
117 rb_io_close(f);
118 return ast_parse_done(ast);
119 }
120
121 static VALUE
122 lex_array(VALUE array, int index)
123 {
124 VALUE str = rb_ary_entry(array, index);
125 if (!NIL_P(str)) {
126 StringValue(str);
127 if (!rb_enc_asciicompat(rb_enc_get(str))) {
128 rb_raise(rb_eArgError, "invalid source encoding");
129 }
130 }
131 return str;
132 }
133
134 static VALUE
135 rb_ast_parse_array(VALUE array)
136 {
137 rb_ast_t *ast = 0;
138
139 array = rb_check_array_type(array);
140 ast = rb_parser_compile_generic(ast_parse_new(), lex_array, Qnil, array, 1);
141 return ast_parse_done(ast);
142 }
143
144 static VALUE node_children(rb_ast_t*, NODE*);
145
146 static VALUE
147 node_find(VALUE self, const int node_id)
148 {
149 VALUE ary;
150 long i;
151 struct ASTNodeData *data;
152 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
153
154 if (nd_node_id(data->node) == node_id) return self;
155
156 ary = node_children(data->ast, data->node);
157
158 for (i = 0; i < RARRAY_LEN(ary); i++) {
159 VALUE child = RARRAY_AREF(ary, i);
160
161 if (CLASS_OF(child) == rb_cNode) {
162 VALUE result = node_find(child, node_id);
163 if (RTEST(result)) return result;
164 }
165 }
166
167 return Qnil;
168 }
169
170 extern VALUE rb_e_script;
171
172 static VALUE
173 script_lines(VALUE path)
174 {
175 VALUE hash, lines;
176 ID script_lines;
177 CONST_ID(script_lines, "SCRIPT_LINES__");
178 if (!rb_const_defined_at(rb_cObject, script_lines)) return Qnil;
179 hash = rb_const_get_at(rb_cObject, script_lines);
180 if (!RB_TYPE_P(hash, T_HASH)) return Qnil;
181 lines = rb_hash_lookup(hash, path);
182 if (!RB_TYPE_P(lines, T_ARRAY)) return Qnil;
183 return lines;
184 }
185
186 static VALUE
187 ast_s_of(rb_execution_context_t *ec, VALUE module, VALUE body)
188 {
189 VALUE path, node, lines;
190 int node_id;
191 const rb_iseq_t *iseq = NULL;
192
193 if (rb_obj_is_proc(body)) {
194 iseq = vm_proc_iseq(body);
195
196 if (!rb_obj_is_iseq((VALUE)iseq)) {
197 iseq = NULL;
198 }
199 }
200 else {
201 iseq = rb_method_iseq(body);
202 }
203
204 if (!iseq) return Qnil;
205
206 path = rb_iseq_path(iseq);
207 node_id = iseq->body->location.node_id;
208 if (!NIL_P(lines = script_lines(path))) {
209 node = rb_ast_parse_array(lines);
210 }
211 else if (RSTRING_LEN(path) == 2 && memcmp(RSTRING_PTR(path), "-e", 2) == 0) {
212 node = rb_ast_parse_str(rb_e_script);
213 }
214 else {
215 node = rb_ast_parse_file(path);
216 }
217
218 return node_find(node, node_id);
219 }
220
221 static VALUE
222 rb_ast_node_alloc(VALUE klass)
223 {
224 struct ASTNodeData *data;
225 VALUE obj = TypedData_Make_Struct(klass, struct ASTNodeData, &rb_node_type, data);
226
227 return obj;
228 }
229
230 static const char*
231 node_type_to_str(const NODE *node)
232 {
233 return (ruby_node_name(nd_type(node)) + rb_strlen_lit("NODE_"));
234 }
235
236 static VALUE
237 ast_node_type(rb_execution_context_t *ec, VALUE self)
238 {
239 struct ASTNodeData *data;
240 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
241
242 return rb_sym_intern_ascii_cstr(node_type_to_str(data->node));
243 }
244
245 #define NEW_CHILD(ast, node) node ? ast_new_internal(ast, node) : Qnil
246
247 static VALUE
248 rb_ary_new_from_node_args(rb_ast_t *ast, long n, ...)
249 {
250 va_list ar;
251 VALUE ary;
252 long i;
253
254 ary = rb_ary_new2(n);
255
256 va_start(ar, n);
257 for (i=0; i<n; i++) {
258 NODE *node;
259 node = va_arg(ar, NODE *);
260 rb_ary_push(ary, NEW_CHILD(ast, node));
261 }
262 va_end(ar);
263 return ary;
264 }
265
266 static VALUE
267 dump_block(rb_ast_t *ast, NODE *node)
268 {
269 VALUE ary = rb_ary_new();
270 do {
271 rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
272 } while (node->nd_next &&
273 nd_type(node->nd_next) == NODE_BLOCK &&
274 (node = node->nd_next, 1));
275 if (node->nd_next) {
276 rb_ary_push(ary, NEW_CHILD(ast, node->nd_next));
277 }
278
279 return ary;
280 }
281
282 static VALUE
283 dump_array(rb_ast_t *ast, NODE *node)
284 {
285 VALUE ary = rb_ary_new();
286 rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
287
288 while (node->nd_next && nd_type(node->nd_next) == NODE_LIST) {
289 node = node->nd_next;
290 rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
291 }
292 rb_ary_push(ary, NEW_CHILD(ast, node->nd_next));
293
294 return ary;
295 }
296
297 static VALUE
298 var_name(ID id)
299 {
300 if (!id) return Qnil;
301 if (!rb_id2str(id)) return Qnil;
302 return ID2SYM(id);
303 }
304
305 static VALUE
306 node_children(rb_ast_t *ast, NODE *node)
307 {
308 char name[DECIMAL_SIZE_OF_BITS(sizeof(long) * CHAR_BIT) + 2]; /* including '$' */
309
310 enum node_type type = nd_type(node);
311 switch (type) {
312 case NODE_BLOCK:
313 return dump_block(ast, node);
314 case NODE_IF:
315 return rb_ary_new_from_node_args(ast, 3, node->nd_cond, node->nd_body, node->nd_else);
316 case NODE_UNLESS:
317 return rb_ary_new_from_node_args(ast, 3, node->nd_cond, node->nd_body, node->nd_else);
318 case NODE_CASE:
319 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
320 case NODE_CASE2:
321 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
322 case NODE_CASE3:
323 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
324 case NODE_WHEN:
325 return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_body, node->nd_next);
326 case NODE_IN:
327 return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_body, node->nd_next);
328 case NODE_WHILE:
329 goto loop;
330 case NODE_UNTIL:
331 loop:
332 return rb_ary_push(rb_ary_new_from_node_args(ast, 2, node->nd_cond, node->nd_body),
333 (node->nd_state ? Qtrue : Qfalse));
334 case NODE_ITER:
335 case NODE_FOR:
336 return rb_ary_new_from_node_args(ast, 2, node->nd_iter, node->nd_body);
337 case NODE_FOR_MASGN:
338 return rb_ary_new_from_node_args(ast, 1, node->nd_var);
339 case NODE_BREAK:
340 goto jump;
341 case NODE_NEXT:
342 goto jump;
343 case NODE_RETURN:
344 jump:
345 return rb_ary_new_from_node_args(ast, 1, node->nd_stts);
346 case NODE_REDO:
347 return rb_ary_new_from_node_args(ast, 0);
348 case NODE_RETRY:
349 return rb_ary_new_from_node_args(ast, 0);
350 case NODE_BEGIN:
351 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
352 case NODE_RESCUE:
353 return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_resq, node->nd_else);
354 case NODE_RESBODY:
355 return rb_ary_new_from_node_args(ast, 3, node->nd_args, node->nd_body, node->nd_head);
356 case NODE_ENSURE:
357 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_ensr);
358 case NODE_AND:
359 goto andor;
360 case NODE_OR:
361 andor:
362 {
363 VALUE ary = rb_ary_new();
364
365 while (1) {
366 rb_ary_push(ary, NEW_CHILD(ast, node->nd_1st));
367 if (!node->nd_2nd || nd_type(node->nd_2nd) != (int)type)
368 break;
369 node = node->nd_2nd;
370 }
371 rb_ary_push(ary, NEW_CHILD(ast, node->nd_2nd));
372 return ary;
373 }
374 case NODE_MASGN:
375 if (NODE_NAMED_REST_P(node->nd_args)) {
376 return rb_ary_new_from_node_args(ast, 3, node->nd_value, node->nd_head, node->nd_args);
377 }
378 else {
379 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_value),
380 NEW_CHILD(ast, node->nd_head),
381 ID2SYM(rb_intern("NODE_SPECIAL_NO_NAME_REST")));
382 }
383 case NODE_LASGN:
384 goto asgn;
385 case NODE_DASGN:
386 goto asgn;
387 case NODE_DASGN_CURR:
388 goto asgn;
389 case NODE_IASGN:
390 goto asgn;
391 case NODE_CVASGN:
392 asgn:
393 if (NODE_REQUIRED_KEYWORD_P(node)) {
394 return rb_ary_new_from_args(2, var_name(node->nd_vid), ID2SYM(rb_intern("NODE_SPECIAL_REQUIRED_KEYWORD")));
395 }
396 return rb_ary_new_from_args(2, var_name(node->nd_vid), NEW_CHILD(ast, node->nd_value));
397 case NODE_GASGN:
398 goto asgn;
399 case NODE_CDECL:
400 if (node->nd_vid) {
401 return rb_ary_new_from_args(2, ID2SYM(node->nd_vid), NEW_CHILD(ast, node->nd_value));
402 }
403 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_else), ID2SYM(node->nd_else->nd_mid), NEW_CHILD(ast, node->nd_value));
404 case NODE_OP_ASGN1:
405 return rb_ary_new_from_args(4, NEW_CHILD(ast, node->nd_recv),
406 ID2SYM(node->nd_mid),
407 NEW_CHILD(ast, node->nd_args->nd_head),
408 NEW_CHILD(ast, node->nd_args->nd_body));
409 case NODE_OP_ASGN2:
410 return rb_ary_new_from_args(4, NEW_CHILD(ast, node->nd_recv),
411 node->nd_next->nd_aid ? Qtrue : Qfalse,
412 ID2SYM(node->nd_next->nd_vid),
413 NEW_CHILD(ast, node->nd_value));
414 case NODE_OP_ASGN_AND:
415 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_head), ID2SYM(idANDOP),
416 NEW_CHILD(ast, node->nd_value));
417 case NODE_OP_ASGN_OR:
418 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_head), ID2SYM(idOROP),
419 NEW_CHILD(ast, node->nd_value));
420 case NODE_OP_CDECL:
421 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_head),
422 ID2SYM(node->nd_aid),
423 NEW_CHILD(ast, node->nd_value));
424 case NODE_CALL:
425 case NODE_OPCALL:
426 case NODE_QCALL:
427 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_recv),
428 ID2SYM(node->nd_mid),
429 NEW_CHILD(ast, node->nd_args));
430 case NODE_FCALL:
431 return rb_ary_new_from_args(2, ID2SYM(node->nd_mid),
432 NEW_CHILD(ast, node->nd_args));
433 case NODE_VCALL:
434 return rb_ary_new_from_args(1, ID2SYM(node->nd_mid));
435 case NODE_SUPER:
436 return rb_ary_new_from_node_args(ast, 1, node->nd_args);
437 case NODE_ZSUPER:
438 return rb_ary_new_from_node_args(ast, 0);
439 case NODE_LIST:
440 goto ary;
441 case NODE_VALUES:
442 ary:
443 return dump_array(ast, node);
444 case NODE_ZLIST:
445 return rb_ary_new_from_node_args(ast, 0);
446 case NODE_HASH:
447 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
448 case NODE_YIELD:
449 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
450 case NODE_LVAR:
451 case NODE_DVAR:
452 return rb_ary_new_from_args(1, var_name(node->nd_vid));
453 case NODE_IVAR:
454 case NODE_CONST:
455 case NODE_CVAR:
456 case NODE_GVAR:
457 return rb_ary_new_from_args(1, ID2SYM(node->nd_vid));
458 case NODE_NTH_REF:
459 snprintf(name, sizeof(name), "$%ld", node->nd_nth);
460 return rb_ary_new_from_args(1, ID2SYM(rb_intern(name)));
461 case NODE_BACK_REF:
462 name[0] = '$';
463 name[1] = (char)node->nd_nth;
464 name[2] = '\0';
465 return rb_ary_new_from_args(1, ID2SYM(rb_intern(name)));
466 case NODE_MATCH:
467 goto lit;
468 case NODE_MATCH2:
469 if (node->nd_args) {
470 return rb_ary_new_from_node_args(ast, 3, node->nd_recv, node->nd_value, node->nd_args);
471 }
472 return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_value);
473 case NODE_MATCH3:
474 return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_value);
475 case NODE_LIT:
476 goto lit;
477 case NODE_STR:
478 goto lit;
479 case NODE_XSTR:
480 lit:
481 return rb_ary_new_from_args(1, node->nd_lit);
482 case NODE_ONCE:
483 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
484 case NODE_DSTR:
485 goto dlit;
486 case NODE_DXSTR:
487 goto dlit;
488 case NODE_DREGX:
489 goto dlit;
490 case NODE_DSYM:
491 dlit:
492 return rb_ary_new_from_args(3, node->nd_lit,
493 NEW_CHILD(ast, node->nd_next->nd_head),
494 NEW_CHILD(ast, node->nd_next->nd_next));
495 case NODE_EVSTR:
496 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
497 case NODE_ARGSCAT:
498 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
499 case NODE_ARGSPUSH:
500 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
501 case NODE_SPLAT:
502 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
503 case NODE_BLOCK_PASS:
504 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
505 case NODE_DEFN:
506 return rb_ary_new_from_args(2, ID2SYM(node->nd_mid), NEW_CHILD(ast, node->nd_defn));
507 case NODE_DEFS:
508 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_recv), ID2SYM(node->nd_mid), NEW_CHILD(ast, node->nd_defn));
509 case NODE_ALIAS:
510 return rb_ary_new_from_node_args(ast, 2, node->nd_1st, node->nd_2nd);
511 case NODE_VALIAS:
512 return rb_ary_new_from_args(2, ID2SYM(node->nd_alias), ID2SYM(node->nd_orig));
513 case NODE_UNDEF:
514 return rb_ary_new_from_node_args(ast, 1, node->nd_undef);
515 case NODE_CLASS:
516 return rb_ary_new_from_node_args(ast, 3, node->nd_cpath, node->nd_super, node->nd_body);
517 case NODE_MODULE:
518 return rb_ary_new_from_node_args(ast, 2, node->nd_cpath, node->nd_body);
519 case NODE_SCLASS:
520 return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_body);
521 case NODE_COLON2:
522 return rb_ary_new_from_args(2, NEW_CHILD(ast, node->nd_head), ID2SYM(node->nd_mid));
523 case NODE_COLON3:
524 return rb_ary_new_from_args(1, ID2SYM(node->nd_mid));
525 case NODE_DOT2:
526 goto dot;
527 case NODE_DOT3:
528 goto dot;
529 case NODE_FLIP2:
530 goto dot;
531 case NODE_FLIP3:
532 dot:
533 return rb_ary_new_from_node_args(ast, 2, node->nd_beg, node->nd_end);
534 case NODE_SELF:
535 return rb_ary_new_from_node_args(ast, 0);
536 case NODE_NIL:
537 return rb_ary_new_from_node_args(ast, 0);
538 case NODE_TRUE:
539 return rb_ary_new_from_node_args(ast, 0);
540 case NODE_FALSE:
541 return rb_ary_new_from_node_args(ast, 0);
542 case NODE_ERRINFO:
543 return rb_ary_new_from_node_args(ast, 0);
544 case NODE_DEFINED:
545 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
546 case NODE_POSTEXE:
547 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
548 case NODE_ATTRASGN:
549 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_recv), ID2SYM(node->nd_mid), NEW_CHILD(ast, node->nd_args));
550 case NODE_LAMBDA:
551 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
552 case NODE_OPT_ARG:
553 return rb_ary_new_from_node_args(ast, 2, node->nd_body, node->nd_next);
554 case NODE_KW_ARG:
555 return rb_ary_new_from_node_args(ast, 2, node->nd_body, node->nd_next);
556 case NODE_POSTARG:
557 if (NODE_NAMED_REST_P(node->nd_1st)) {
558 return rb_ary_new_from_node_args(ast, 2, node->nd_1st, node->nd_2nd);
559 }
560 return rb_ary_new_from_args(2, ID2SYM(rb_intern("NODE_SPECIAL_NO_NAME_REST")),
561 NEW_CHILD(ast, node->nd_2nd));
562 case NODE_ARGS:
563 {
564 struct rb_args_info *ainfo = node->nd_ainfo;
565 return rb_ary_new_from_args(10,
566 INT2NUM(ainfo->pre_args_num),
567 NEW_CHILD(ast, ainfo->pre_init),
568 NEW_CHILD(ast, ainfo->opt_args),
569 var_name(ainfo->first_post_arg),
570 INT2NUM(ainfo->post_args_num),
571 NEW_CHILD(ast, ainfo->post_init),
572 var_name(ainfo->rest_arg),
573 (ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast, ainfo->kw_args)),
574 (ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast, ainfo->kw_rest_arg)),
575 var_name(ainfo->block_arg));
576 }
577 case NODE_SCOPE:
578 {
579 ID *tbl = node->nd_tbl;
580 int i, size = tbl ? (int)*tbl++ : 0;
581 VALUE locals = rb_ary_new_capa(size);
582 for (i = 0; i < size; i++) {
583 rb_ary_push(locals, var_name(tbl[i]));
584 }
585 return rb_ary_new_from_args(3, locals, NEW_CHILD(ast, node->nd_args), NEW_CHILD(ast, node->nd_body));
586 }
587 case NODE_ARYPTN:
588 {
589 struct rb_ary_pattern_info *apinfo = node->nd_apinfo;
590 VALUE rest = NODE_NAMED_REST_P(apinfo->rest_arg) ? NEW_CHILD(ast, apinfo->rest_arg) :
591 ID2SYM(rb_intern("NODE_SPECIAL_NO_NAME_REST"));
592 return rb_ary_new_from_args(4,
593 NEW_CHILD(ast, node->nd_pconst),
594 NEW_CHILD(ast, apinfo->pre_args),
595 rest,
596 NEW_CHILD(ast, apinfo->post_args));
597 }
598 case NODE_HSHPTN:
599 {
600 VALUE kwrest = node->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD ? ID2SYM(rb_intern("NODE_SPECIAL_NO_REST_KEYWORD")) :
601 NEW_CHILD(ast, node->nd_pkwrestarg);
602
603 return rb_ary_new_from_args(3,
604 NEW_CHILD(ast, node->nd_pconst),
605 NEW_CHILD(ast, node->nd_pkwargs),
606 kwrest);
607 }
608 case NODE_ARGS_AUX:
609 case NODE_LAST:
610 break;
611 }
612
613 rb_bug("node_children: unknown node: %s", ruby_node_name(type));
614 }
615
616 static VALUE
617 ast_node_children(rb_execution_context_t *ec, VALUE self)
618 {
619 struct ASTNodeData *data;
620 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
621
622 return node_children(data->ast, data->node);
623 }
624
625 static VALUE
626 ast_node_first_lineno(rb_execution_context_t *ec, VALUE self)
627 {
628 struct ASTNodeData *data;
629 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
630
631 return INT2NUM(nd_first_lineno(data->node));
632 }
633
634 static VALUE
635 ast_node_first_column(rb_execution_context_t *ec, VALUE self)
636 {
637 struct ASTNodeData *data;
638 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
639
640 return INT2NUM(nd_first_column(data->node));
641 }
642
643 static VALUE
644 ast_node_last_lineno(rb_execution_context_t *ec, VALUE self)
645 {
646 struct ASTNodeData *data;
647 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
648
649 return INT2NUM(nd_last_lineno(data->node));
650 }
651
652 static VALUE
653 ast_node_last_column(rb_execution_context_t *ec, VALUE self)
654 {
655 struct ASTNodeData *data;
656 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
657
658 return INT2NUM(nd_last_column(data->node));
659 }
660
661 static VALUE
662 ast_node_inspect(rb_execution_context_t *ec, VALUE self)
663 {
664 VALUE str;
665 VALUE cname;
666 struct ASTNodeData *data;
667 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
668
669 cname = rb_class_path(rb_obj_class(self));
670 str = rb_str_new2("#<");
671
672 rb_str_append(str, cname);
673 rb_str_catf(str, ":%s@%d:%d-%d:%d>",
674 node_type_to_str(data->node),
675 nd_first_lineno(data->node), nd_first_column(data->node),
676 nd_last_lineno(data->node), nd_last_column(data->node));
677
678 return str;
679 }
680
681 #include "ast.rbinc"
682
683 void
684 Init_ast(void)
685 {
686 rb_mAST = rb_define_module_under(rb_cRubyVM, "AbstractSyntaxTree");
687 rb_cNode = rb_define_class_under(rb_mAST, "Node", rb_cObject);
688 rb_undef_alloc_func(rb_cNode);
689
690 load_ast();
691 }
NODE_NEXT
@ NODE_NEXT
Definition: node.h:47
rb_ary_new_capa
VALUE rb_ary_new_capa(long capa)
Definition: array.c:717
rb_parser_new
VALUE rb_parser_new(void)
Definition: ripper.c:19224
NODE_COLON3
@ NODE_COLON3
Definition: node.h:119
NODE_NIL
@ NODE_NIL
Definition: node.h:125
ASTNodeData
Definition: ast.c:14
ID
unsigned long ID
Definition: ruby.h:103
ruby_node_name
const char * ruby_node_name(int node)
Definition: iseq.c:2534
TypedData_Make_Struct
#define TypedData_Make_Struct(klass, type, data_type, sval)
Definition: ruby.h:1244
NODE_ATTRASGN
@ NODE_ATTRASGN
Definition: node.h:132
idANDOP
@ idANDOP
Definition: id.h:108
NODE_DSYM
@ NODE_DSYM
Definition: node.h:131
rb_str_new2
#define rb_str_new2
Definition: intern.h:903
NODE_HSHPTN
@ NODE_HSHPTN
Definition: node.h:135
NODE_RESCUE
@ NODE_RESCUE
Definition: node.h:51
rb_ast_struct::body
rb_ast_body_t body
Definition: node.h:402
rb_io_close
VALUE rb_io_close(VALUE)
Definition: io.c:4820
NODE_OP_ASGN1
@ NODE_OP_ASGN1
Definition: node.h:64
rb_ary_pattern_info::pre_args
NODE * pre_args
Definition: node.h:454
rb_iseq_struct
Definition: vm_core.h:456
va_arg
#define va_arg(v, l)
Definition: rb_mjit_min_header-2.7.0.h:3980
path
VALUE path
Definition: rb_mjit_min_header-2.7.0.h:7351
NODE_TRUE
@ NODE_TRUE
Definition: node.h:126
rb_define_module_under
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:797
NODE_MATCH3
@ NODE_MATCH3
Definition: node.h:92
NODE_NTH_REF
@ NODE_NTH_REF
Definition: node.h:88
NODE_ZLIST
@ NODE_ZLIST
Definition: node.h:77
NODE_DREGX
@ NODE_DREGX
Definition: node.h:99
NODE_OR
@ NODE_OR
Definition: node.h:55
NODE_ARYPTN
@ NODE_ARYPTN
Definition: node.h:134
rb_ary_new_from_args
#define rb_ary_new_from_args(n,...)
Definition: rb_mjit_min_header-2.7.0.h:7214
rb_ast_dispose
void rb_ast_dispose(rb_ast_t *ast)
Definition: node.c:1389
NODE_OP_ASGN_OR
@ NODE_OP_ASGN_OR
Definition: node.h:67
snprintf
int snprintf(char *__restrict, size_t, const char *__restrict,...) __attribute__((__format__(__printf__
NODE_HASH
@ NODE_HASH
Definition: node.h:79
NODE_CONST
@ NODE_CONST
Definition: node.h:86
RSTRING_PTR
#define RSTRING_PTR(str)
Definition: ruby.h:1009
NODE_LASGN
@ NODE_LASGN
Definition: node.h:57
NODE_FLIP2
@ NODE_FLIP2
Definition: node.h:122
NODE_ARGSPUSH
@ NODE_ARGSPUSH
Definition: node.h:107
NODE_FOR
@ NODE_FOR
Definition: node.h:44
VALUE
unsigned long VALUE
Definition: ruby.h:102
NODE_CASE
@ NODE_CASE
Definition: node.h:36
index
int index
Definition: rb_mjit_min_header-2.7.0.h:11246
rb_eArgError
VALUE rb_eArgError
Definition: error.c:923
encoding.h
rb_intern
#define rb_intern(str)
NODE_OP_ASGN2
@ NODE_OP_ASGN2
Definition: node.h:65
NODE_ARGSCAT
@ NODE_ARGSCAT
Definition: node.h:106
RB_TYPE_P
#define RB_TYPE_P(obj, type)
Definition: ruby.h:560
rb_enc_get
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:872
rb_enc_asciicompat
#define rb_enc_asciicompat(enc)
Definition: encoding.h:245
rb_args_info::pre_args_num
int pre_args_num
Definition: node.h:435
nd_last_lineno
#define nd_last_lineno(n)
Definition: node.h:207
NODE_DOT2
@ NODE_DOT2
Definition: node.h:120
NODE_EVSTR
@ NODE_EVSTR
Definition: node.h:98
Init_ast
void Init_ast(void)
Definition: ast.c:684
int
__inline__ int
Definition: rb_mjit_min_header-2.7.0.h:2839
rb_e_script
VALUE rb_e_script
Definition: ruby.c:1467
rb_args_info::kw_rest_arg
NODE * kw_rest_arg
Definition: node.h:444
rb_const_get_at
VALUE rb_const_get_at(VALUE, ID)
Definition: variable.c:2393
rb_iseq_path
VALUE rb_iseq_path(const rb_iseq_t *iseq)
Definition: iseq.c:1027
NODE_UNTIL
@ NODE_UNTIL
Definition: node.h:42
rb_args_info::post_init
NODE * post_init
Definition: node.h:433
rb_iseq_constant_body::location
rb_iseq_location_t location
Definition: vm_core.h:399
NODE_CVASGN
@ NODE_CVASGN
Definition: node.h:63
NODE_AND
@ NODE_AND
Definition: node.h:54
NODE_LIT
@ NODE_LIT
Definition: node.h:93
StringValue
use StringValue() instead")))
rb_parser_compile_file_path
rb_ast_t * rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
Definition: ripper.c:12912
NODE_MASGN
@ NODE_MASGN
Definition: node.h:56
rb_parser_compile_generic
rb_ast_t * rb_parser_compile_generic(VALUE vparser, VALUE(*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
Definition: ripper.c:12932
NODE_DXSTR
@ NODE_DXSTR
Definition: node.h:97
CHAR_BIT
#define CHAR_BIT
Definition: ruby.h:227
NODE_OPCALL
@ NODE_OPCALL
Definition: node.h:70
GET_EC
#define GET_EC()
Definition: vm_core.h:1766
INT2NUM
#define INT2NUM(x)
Definition: ruby.h:1609
ptr
struct RIMemo * ptr
Definition: debug.c:74
NODE_SPECIAL_NO_REST_KEYWORD
#define NODE_SPECIAL_NO_REST_KEYWORD
Definition: node.h:386
Qfalse
#define Qfalse
Definition: ruby.h:467
NODE_OP_ASGN_AND
@ NODE_OP_ASGN_AND
Definition: node.h:66
FilePathValue
#define FilePathValue(v)
Definition: ruby.h:624
rb_id2str
#define rb_id2str(id)
Definition: vm_backtrace.c:30
NODE_DASGN
@ NODE_DASGN
Definition: node.h:58
NODE_BLOCK
@ NODE_BLOCK
Definition: node.h:33
NULL
#define NULL
Definition: _sdbm.c:101
NODE_ERRINFO
@ NODE_ERRINFO
Definition: node.h:128
rb_args_info::first_post_arg
ID first_post_arg
Definition: node.h:438
rb_enc_from_encoding
VALUE rb_enc_from_encoding(rb_encoding *encoding)
Definition: encoding.c:116
NODE_KW_ARG
@ NODE_KW_ARG
Definition: node.h:104
NODE_DSTR
@ NODE_DSTR
Definition: node.h:95
ID2SYM
#define ID2SYM(x)
Definition: ruby.h:414
NODE_SPLAT
@ NODE_SPLAT
Definition: node.h:108
NODE_ITER
@ NODE_ITER
Definition: node.h:43
NODE_CDECL
@ NODE_CDECL
Definition: node.h:62
rb_args_info::kw_args
NODE * kw_args
Definition: node.h:443
NODE_CVAR
@ NODE_CVAR
Definition: node.h:87
rb_args_info::pre_init
NODE * pre_init
Definition: node.h:432
NODE_IN
@ NODE_IN
Definition: node.h:40
NODE_ZSUPER
@ NODE_ZSUPER
Definition: node.h:75
RUBY_TYPED_DEFAULT_FREE
#define RUBY_TYPED_DEFAULT_FREE
Definition: ruby.h:1203
NODE_LAMBDA
@ NODE_LAMBDA
Definition: node.h:133
NODE_SUPER
@ NODE_SUPER
Definition: node.h:74
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2669
NODE_VCALL
@ NODE_VCALL
Definition: node.h:72
NODE_VALIAS
@ NODE_VALIAS
Definition: node.h:113
rb_ary_entry
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1512
NODE_DEFS
@ NODE_DEFS
Definition: node.h:111
obj
const VALUE VALUE obj
Definition: rb_mjit_min_header-2.7.0.h:5742
NODE_UNLESS
@ NODE_UNLESS
Definition: node.h:35
rb_obj_class
VALUE rb_obj_class(VALUE)
Equivalent to Object#class in Ruby.
Definition: object.c:217
rb_obj_is_proc
VALUE rb_obj_is_proc(VALUE)
Definition: proc.c:152
rb_ast_struct
Definition: node.h:399
NODE_DEFN
@ NODE_DEFN
Definition: node.h:110
rb_args_info::opt_args
NODE * opt_args
Definition: node.h:446
rb_file_open_str
VALUE rb_file_open_str(VALUE, const char *)
Definition: io.c:6252
rb_args_info
Definition: node.h:431
rb_cRubyVM
VALUE rb_cRubyVM
Definition: vm.c:365
rb_ary_pattern_info::post_args
NODE * post_args
Definition: node.h:456
rb_encoding
const typedef OnigEncodingType rb_encoding
Definition: encoding.h:115
rb_ary_pattern_info::rest_arg
NODE * rest_arg
Definition: node.h:455
NEW_CHILD
#define NEW_CHILD(ast, node)
Definition: ast.c:245
iseq.h
node.h
i
uint32_t i
Definition: rb_mjit_min_header-2.7.0.h:5464
NODE_CLASS
@ NODE_CLASS
Definition: node.h:115
nd_node_id
#define nd_node_id(n)
Definition: node.h:211
rb_ast_memsize
size_t rb_ast_memsize(const rb_ast_t *ast)
Definition: node.c:1375
NODE_IF
@ NODE_IF
Definition: node.h:34
NODE_ENSURE
@ NODE_ENSURE
Definition: node.h:53
NODE_MATCH2
@ NODE_MATCH2
Definition: node.h:91
rb_hash_lookup
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Definition: hash.c:1990
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:1195
NODE_CASE3
@ NODE_CASE3
Definition: node.h:38
ruby.h
va_end
#define va_end(v)
Definition: rb_mjit_min_header-2.7.0.h:3979
vm_core.h
NODE_DASGN_CURR
@ NODE_DASGN_CURR
Definition: node.h:59
NODE_COLON2
@ NODE_COLON2
Definition: node.h:118
NODE_DOT3
@ NODE_DOT3
Definition: node.h:121
node_type
node_type
Definition: node.h:22
NODE_BREAK
@ NODE_BREAK
Definition: node.h:46
NODE_GVAR
@ NODE_GVAR
Definition: node.h:84
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition: ruby.h:1101
NODE_XSTR
@ NODE_XSTR
Definition: node.h:96
ASTNodeData::ast
rb_ast_t * ast
Definition: ast.c:15
size
int size
Definition: encoding.c:58
NODE_ARGS
@ NODE_ARGS
Definition: node.h:101
nd_first_column
#define nd_first_column(n)
Definition: node.h:198
rb_parser_compile_string_path
rb_ast_t * rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
Definition: ripper.c:12897
NODE_DVAR
@ NODE_DVAR
Definition: node.h:83
memcmp
int memcmp(const void *s1, const void *s2, size_t len)
Definition: memcmp.c:7
rb_str_new_cstr
#define rb_str_new_cstr(str)
Definition: rb_mjit_min_header-2.7.0.h:6117
NODE_ALIAS
@ NODE_ALIAS
Definition: node.h:112
rb_args_info::post_args_num
int post_args_num
Definition: node.h:436
CONST_ID
#define CONST_ID(var, str)
Definition: ruby.h:1841
nd_pkwrestarg
#define nd_pkwrestarg
Definition: node.h:278
NODE_OP_CDECL
@ NODE_OP_CDECL
Definition: node.h:68
rb_check_array_type
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:909
T_HASH
#define T_HASH
Definition: ruby.h:531
NODE_POSTARG
@ NODE_POSTARG
Definition: node.h:105
NODE_SELF
@ NODE_SELF
Definition: node.h:124
rb_args_info::no_kwarg
unsigned int no_kwarg
Definition: node.h:447
NODE_LVAR
@ NODE_LVAR
Definition: node.h:82
NODE_STR
@ NODE_STR
Definition: node.h:94
CLASS_OF
#define CLASS_OF(v)
Definition: ruby.h:484
RARRAY_LEN
#define RARRAY_LEN(a)
Definition: ruby.h:1070
rb_sym_intern_ascii_cstr
#define rb_sym_intern_ascii_cstr(ptr)
Definition: rb_mjit_min_header-2.7.0.h:7758
NODE_IVAR
@ NODE_IVAR
Definition: node.h:85
char
#define char
Definition: rb_mjit_min_header-2.7.0.h:2876
rb_cObject
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:2010
rb_ary_new2
#define rb_ary_new2
Definition: intern.h:103
nd_last_column
#define nd_last_column(n)
Definition: node.h:205
n
const char size_t n
Definition: rb_mjit_min_header-2.7.0.h:5456
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:667
NODE_BEGIN
@ NODE_BEGIN
Definition: node.h:50
TypedData_Get_Struct
#define TypedData_Get_Struct(obj, type, data_type, sval)
Definition: ruby.h:1252
rb_funcall
#define rb_funcall(recv, mid, argc,...)
Definition: rb_mjit_min_header-2.7.0.h:6585
rb_str_append
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2965
NODE_POSTEXE
@ NODE_POSTEXE
Definition: node.h:130
NODE_RESBODY
@ NODE_RESBODY
Definition: node.h:52
rb_bug
void rb_bug(const char *fmt,...)
Definition: error.c:634
NODE_RETURN
@ NODE_RETURN
Definition: node.h:80
internal.h
NODE_FALSE
@ NODE_FALSE
Definition: node.h:127
T_ARRAY
#define T_ARRAY
Definition: ruby.h:530
NODE_ONCE
@ NODE_ONCE
Definition: node.h:100
f
#define f
idOROP
@ idOROP
Definition: id.h:109
NODE_IASGN
@ NODE_IASGN
Definition: node.h:61
klass
VALUE klass
Definition: rb_mjit_min_header-2.7.0.h:13254
NODE_ARGS_AUX
@ NODE_ARGS_AUX
Definition: node.h:102
rb_utf8_encoding
rb_encoding * rb_utf8_encoding(void)
Definition: encoding.c:1328
NODE_NAMED_REST_P
#define NODE_NAMED_REST_P(node)
Definition: node.h:384
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
nd_first_lineno
#define nd_first_lineno(n)
Definition: node.h:200
NODE_WHEN
@ NODE_WHEN
Definition: node.h:39
NODE_BLOCK_PASS
@ NODE_BLOCK_PASS
Definition: node.h:109
NODE_MATCH
@ NODE_MATCH
Definition: node.h:90
RUBY_TYPED_FREE_IMMEDIATELY
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1207
ASTNodeData::node
NODE * node
Definition: ast.c:16
rb_parser_set_context
VALUE rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
Definition: ripper.c:19234
NODE_OPT_ARG
@ NODE_OPT_ARG
Definition: node.h:103
NODE_CALL
@ NODE_CALL
Definition: node.h:69
NIL_P
#define NIL_P(v)
Definition: ruby.h:482
NODE_CASE2
@ NODE_CASE2
Definition: node.h:37
rb_data_type_struct
Definition: ruby.h:1148
NODE_FCALL
@ NODE_FCALL
Definition: node.h:71
rb_gc_mark
void rb_gc_mark(VALUE ptr)
Definition: gc.c:5212
rb_iseq_location_struct::node_id
int node_id
Definition: vm_core.h:277
NODE_YIELD
@ NODE_YIELD
Definition: node.h:81
Qtrue
#define Qtrue
Definition: ruby.h:468
rb_str_catf
VALUE rb_str_catf(VALUE str, const char *format,...)
Definition: sprintf.c:1237
rb_class_path
VALUE rb_class_path(VALUE)
Definition: variable.c:153
rb_iseq_struct::body
struct rb_iseq_constant_body * body
Definition: vm_core.h:460
NODE_UNDEF
@ NODE_UNDEF
Definition: node.h:114
va_start
#define va_start(v, l)
Definition: rb_mjit_min_header-2.7.0.h:3978
NODE_BACK_REF
@ NODE_BACK_REF
Definition: node.h:89
NODE_DEFINED
@ NODE_DEFINED
Definition: node.h:129
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:698
va_list
__gnuc_va_list va_list
Definition: rb_mjit_min_header-2.7.0.h:836
rb_args_info::block_arg
ID block_arg
Definition: node.h:441
NODE_REQUIRED_KEYWORD_P
#define NODE_REQUIRED_KEYWORD_P(node)
Definition: node.h:382
rb_const_defined_at
int rb_const_defined_at(VALUE, ID)
Definition: variable.c:2688
iseq
const rb_iseq_t * iseq
Definition: rb_mjit_min_header-2.7.0.h:13504
NODE_LAST
@ NODE_LAST
Definition: node.h:136
DECIMAL_SIZE_OF_BITS
#define DECIMAL_SIZE_OF_BITS(n)
Definition: util.h:50
NODE_VALUES
@ NODE_VALUES
Definition: node.h:78
NODE_FLIP3
@ NODE_FLIP3
Definition: node.h:123
nd_type
#define nd_type(n)
Definition: node.h:188
rb_method_iseq
const rb_iseq_t * rb_method_iseq(VALUE body)
Definition: proc.c:2679
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:723
builtin.h
rb_ast_body_struct::root
const NODE * root
Definition: node.h:395
Qnil
#define Qnil
Definition: ruby.h:469
NODE_REDO
@ NODE_REDO
Definition: node.h:48
util.h
rb_ary_pattern_info
Definition: node.h:453
rb_undef_alloc_func
void rb_undef_alloc_func(VALUE)
Definition: vm_method.c:722
NODE_MODULE
@ NODE_MODULE
Definition: node.h:116
rb_strlen_lit
#define rb_strlen_lit(str)
Definition: intern.h:913
RSTRING_LEN
#define RSTRING_LEN(str)
Definition: ruby.h:1005
rb_args_info::rest_arg
ID rest_arg
Definition: node.h:440
NODE_QCALL
@ NODE_QCALL
Definition: node.h:73
NODE_SCOPE
@ NODE_SCOPE
Definition: node.h:32
NODE_RETRY
@ NODE_RETRY
Definition: node.h:49
NODE_LIST
@ NODE_LIST
Definition: node.h:76
RTEST
#define RTEST(v)
Definition: ruby.h:481
NODE_FOR_MASGN
@ NODE_FOR_MASGN
Definition: node.h:45
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:39
RNode
Definition: node.h:149
NODE_WHILE
@ NODE_WHILE
Definition: node.h:41
NODE_SCLASS
@ NODE_SCLASS
Definition: node.h:117
name
const char * name
Definition: nkf.c:208
NODE_GASGN
@ NODE_GASGN
Definition: node.h:60
rb_execution_context_struct
Definition: vm_core.h:843