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