Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
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
11static VALUE rb_mAST;
12static VALUE rb_cNode;
13
17};
18
19static void
20node_gc_mark(void *ptr)
21{
22 struct ASTNodeData *data = (struct ASTNodeData *)ptr;
23 rb_gc_mark((VALUE)data->ast);
24}
25
26static size_t
27node_memsize(const void *ptr)
28{
29 struct ASTNodeData *data = (struct ASTNodeData *)ptr;
30 return rb_ast_memsize(data->ast);
31}
32
33static const rb_data_type_t rb_node_type = {
34 "AST/node",
35 {node_gc_mark, RUBY_TYPED_DEFAULT_FREE, node_memsize,},
36 0, 0,
38};
39
40static VALUE rb_ast_node_alloc(VALUE klass);
41
42static void
43setup_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
52static VALUE
53ast_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
63static VALUE rb_ast_parse_str(VALUE str);
64static VALUE rb_ast_parse_file(VALUE path);
65static VALUE rb_ast_parse_array(VALUE array);
66
67static VALUE
68ast_parse_new(void)
69{
71}
72
73static VALUE
74ast_parse_done(rb_ast_t *ast)
75{
76 if (!ast->body.root) {
78 rb_exc_raise(GET_EC()->errinfo);
79 }
80
81 return ast_new_internal(ast, (NODE *)ast->body.root);
82}
83
84static VALUE
85ast_s_parse(rb_execution_context_t *ec, VALUE module, VALUE str)
86{
87 return rb_ast_parse_str(str);
88}
89
90static VALUE
91rb_ast_parse_str(VALUE str)
92{
93 rb_ast_t *ast = 0;
94
96 ast = rb_parser_compile_string_path(ast_parse_new(), Qnil, str, 1);
97 return ast_parse_done(ast);
98}
99
100static VALUE
101ast_s_parse_file(rb_execution_context_t *ec, VALUE module, VALUE path)
102{
103 return rb_ast_parse_file(path);
104}
105
106static VALUE
107rb_ast_parse_file(VALUE path)
108{
109 VALUE f;
110 rb_ast_t *ast = 0;
112
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
121static VALUE
122lex_array(VALUE array, int index)
123{
124 VALUE str = rb_ary_entry(array, index);
125 if (!NIL_P(str)) {
128 rb_raise(rb_eArgError, "invalid source encoding");
129 }
130 }
131 return str;
132}
133
134static VALUE
135rb_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
144static VALUE node_children(rb_ast_t*, NODE*);
145
146static VALUE
147node_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
170extern VALUE rb_e_script;
171
172static VALUE
173script_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
186static VALUE
187ast_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
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
221static VALUE
222rb_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
230static const char*
231node_type_to_str(const NODE *node)
232{
233 return (ruby_node_name(nd_type(node)) + rb_strlen_lit("NODE_"));
234}
235
236static VALUE
237ast_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
247static VALUE
248rb_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 *);
261 }
262 va_end(ar);
263 return ary;
264}
265
266static VALUE
267dump_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
282static VALUE
283dump_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
297static VALUE
298var_name(ID id)
299{
300 if (!id) return Qnil;
301 if (!rb_id2str(id)) return Qnil;
302 return ID2SYM(id);
303}
304
305static VALUE
306node_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:
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(5, NEW_CHILD(ast, node->nd_recv),
411 node->nd_next->nd_aid ? Qtrue : Qfalse,
412 ID2SYM(node->nd_next->nd_vid),
413 ID2SYM(node->nd_next->nd_mid),
414 NEW_CHILD(ast, node->nd_value));
415 case NODE_OP_ASGN_AND:
416 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_head), ID2SYM(idANDOP),
417 NEW_CHILD(ast, node->nd_value));
418 case NODE_OP_ASGN_OR:
419 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_head), ID2SYM(idOROP),
420 NEW_CHILD(ast, node->nd_value));
421 case NODE_OP_CDECL:
422 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_head),
423 ID2SYM(node->nd_aid),
424 NEW_CHILD(ast, node->nd_value));
425 case NODE_CALL:
426 case NODE_OPCALL:
427 case NODE_QCALL:
428 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_recv),
429 ID2SYM(node->nd_mid),
430 NEW_CHILD(ast, node->nd_args));
431 case NODE_FCALL:
432 return rb_ary_new_from_args(2, ID2SYM(node->nd_mid),
433 NEW_CHILD(ast, node->nd_args));
434 case NODE_VCALL:
435 return rb_ary_new_from_args(1, ID2SYM(node->nd_mid));
436 case NODE_SUPER:
437 return rb_ary_new_from_node_args(ast, 1, node->nd_args);
438 case NODE_ZSUPER:
439 return rb_ary_new_from_node_args(ast, 0);
440 case NODE_LIST:
441 goto ary;
442 case NODE_VALUES:
443 ary:
444 return dump_array(ast, node);
445 case NODE_ZLIST:
446 return rb_ary_new_from_node_args(ast, 0);
447 case NODE_HASH:
448 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
449 case NODE_YIELD:
450 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
451 case NODE_LVAR:
452 case NODE_DVAR:
453 return rb_ary_new_from_args(1, var_name(node->nd_vid));
454 case NODE_IVAR:
455 case NODE_CONST:
456 case NODE_CVAR:
457 case NODE_GVAR:
458 return rb_ary_new_from_args(1, ID2SYM(node->nd_vid));
459 case NODE_NTH_REF:
460 snprintf(name, sizeof(name), "$%ld", node->nd_nth);
462 case NODE_BACK_REF:
463 name[0] = '$';
464 name[1] = (char)node->nd_nth;
465 name[2] = '\0';
467 case NODE_MATCH:
468 goto lit;
469 case NODE_MATCH2:
470 if (node->nd_args) {
471 return rb_ary_new_from_node_args(ast, 3, node->nd_recv, node->nd_value, node->nd_args);
472 }
473 return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_value);
474 case NODE_MATCH3:
475 return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_value);
476 case NODE_LIT:
477 goto lit;
478 case NODE_STR:
479 goto lit;
480 case NODE_XSTR:
481 lit:
482 return rb_ary_new_from_args(1, node->nd_lit);
483 case NODE_ONCE:
484 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
485 case NODE_DSTR:
486 goto dlit;
487 case NODE_DXSTR:
488 goto dlit;
489 case NODE_DREGX:
490 goto dlit;
491 case NODE_DSYM:
492 dlit:
493 return rb_ary_new_from_args(3, node->nd_lit,
494 NEW_CHILD(ast, node->nd_next->nd_head),
495 NEW_CHILD(ast, node->nd_next->nd_next));
496 case NODE_EVSTR:
497 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
498 case NODE_ARGSCAT:
499 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
500 case NODE_ARGSPUSH:
501 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
502 case NODE_SPLAT:
503 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
504 case NODE_BLOCK_PASS:
505 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
506 case NODE_DEFN:
507 return rb_ary_new_from_args(2, ID2SYM(node->nd_mid), NEW_CHILD(ast, node->nd_defn));
508 case NODE_DEFS:
509 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_recv), ID2SYM(node->nd_mid), NEW_CHILD(ast, node->nd_defn));
510 case NODE_ALIAS:
511 return rb_ary_new_from_node_args(ast, 2, node->nd_1st, node->nd_2nd);
512 case NODE_VALIAS:
513 return rb_ary_new_from_args(2, ID2SYM(node->nd_alias), ID2SYM(node->nd_orig));
514 case NODE_UNDEF:
515 return rb_ary_new_from_node_args(ast, 1, node->nd_undef);
516 case NODE_CLASS:
517 return rb_ary_new_from_node_args(ast, 3, node->nd_cpath, node->nd_super, node->nd_body);
518 case NODE_MODULE:
519 return rb_ary_new_from_node_args(ast, 2, node->nd_cpath, node->nd_body);
520 case NODE_SCLASS:
521 return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_body);
522 case NODE_COLON2:
523 return rb_ary_new_from_args(2, NEW_CHILD(ast, node->nd_head), ID2SYM(node->nd_mid));
524 case NODE_COLON3:
525 return rb_ary_new_from_args(1, ID2SYM(node->nd_mid));
526 case NODE_DOT2:
527 goto dot;
528 case NODE_DOT3:
529 goto dot;
530 case NODE_FLIP2:
531 goto dot;
532 case NODE_FLIP3:
533 dot:
534 return rb_ary_new_from_node_args(ast, 2, node->nd_beg, node->nd_end);
535 case NODE_SELF:
536 return rb_ary_new_from_node_args(ast, 0);
537 case NODE_NIL:
538 return rb_ary_new_from_node_args(ast, 0);
539 case NODE_TRUE:
540 return rb_ary_new_from_node_args(ast, 0);
541 case NODE_FALSE:
542 return rb_ary_new_from_node_args(ast, 0);
543 case NODE_ERRINFO:
544 return rb_ary_new_from_node_args(ast, 0);
545 case NODE_DEFINED:
546 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
547 case NODE_POSTEXE:
548 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
549 case NODE_ATTRASGN:
550 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_recv), ID2SYM(node->nd_mid), NEW_CHILD(ast, node->nd_args));
551 case NODE_LAMBDA:
552 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
553 case NODE_OPT_ARG:
554 return rb_ary_new_from_node_args(ast, 2, node->nd_body, node->nd_next);
555 case NODE_KW_ARG:
556 return rb_ary_new_from_node_args(ast, 2, node->nd_body, node->nd_next);
557 case NODE_POSTARG:
558 if (NODE_NAMED_REST_P(node->nd_1st)) {
559 return rb_ary_new_from_node_args(ast, 2, node->nd_1st, node->nd_2nd);
560 }
561 return rb_ary_new_from_args(2, ID2SYM(rb_intern("NODE_SPECIAL_NO_NAME_REST")),
562 NEW_CHILD(ast, node->nd_2nd));
563 case NODE_ARGS:
564 {
565 struct rb_args_info *ainfo = node->nd_ainfo;
566 return rb_ary_new_from_args(10,
567 INT2NUM(ainfo->pre_args_num),
568 NEW_CHILD(ast, ainfo->pre_init),
569 NEW_CHILD(ast, ainfo->opt_args),
570 var_name(ainfo->first_post_arg),
571 INT2NUM(ainfo->post_args_num),
572 NEW_CHILD(ast, ainfo->post_init),
574 ? ID2SYM(rb_intern("NODE_SPECIAL_EXCESSIVE_COMMA"))
575 : var_name(ainfo->rest_arg)),
576 (ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast, ainfo->kw_args)),
577 (ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast, ainfo->kw_rest_arg)),
578 var_name(ainfo->block_arg));
579 }
580 case NODE_SCOPE:
581 {
582 ID *tbl = node->nd_tbl;
583 int i, size = tbl ? (int)*tbl++ : 0;
584 VALUE locals = rb_ary_new_capa(size);
585 for (i = 0; i < size; i++) {
586 rb_ary_push(locals, var_name(tbl[i]));
587 }
588 return rb_ary_new_from_args(3, locals, NEW_CHILD(ast, node->nd_args), NEW_CHILD(ast, node->nd_body));
589 }
590 case NODE_ARYPTN:
591 {
592 struct rb_ary_pattern_info *apinfo = node->nd_apinfo;
593 VALUE rest = NODE_NAMED_REST_P(apinfo->rest_arg) ? NEW_CHILD(ast, apinfo->rest_arg) :
594 ID2SYM(rb_intern("NODE_SPECIAL_NO_NAME_REST"));
595 return rb_ary_new_from_args(4,
596 NEW_CHILD(ast, node->nd_pconst),
597 NEW_CHILD(ast, apinfo->pre_args),
598 rest,
599 NEW_CHILD(ast, apinfo->post_args));
600 }
601 case NODE_HSHPTN:
602 {
603 VALUE kwrest = node->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD ? ID2SYM(rb_intern("NODE_SPECIAL_NO_REST_KEYWORD")) :
604 NEW_CHILD(ast, node->nd_pkwrestarg);
605
606 return rb_ary_new_from_args(3,
607 NEW_CHILD(ast, node->nd_pconst),
608 NEW_CHILD(ast, node->nd_pkwargs),
609 kwrest);
610 }
611 case NODE_ARGS_AUX:
612 case NODE_LAST:
613 break;
614 }
615
616 rb_bug("node_children: unknown node: %s", ruby_node_name(type));
617}
618
619static VALUE
620ast_node_children(rb_execution_context_t *ec, VALUE self)
621{
622 struct ASTNodeData *data;
623 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
624
625 return node_children(data->ast, data->node);
626}
627
628static VALUE
629ast_node_first_lineno(rb_execution_context_t *ec, VALUE self)
630{
631 struct ASTNodeData *data;
632 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
633
634 return INT2NUM(nd_first_lineno(data->node));
635}
636
637static VALUE
638ast_node_first_column(rb_execution_context_t *ec, VALUE self)
639{
640 struct ASTNodeData *data;
641 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
642
643 return INT2NUM(nd_first_column(data->node));
644}
645
646static VALUE
647ast_node_last_lineno(rb_execution_context_t *ec, VALUE self)
648{
649 struct ASTNodeData *data;
650 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
651
652 return INT2NUM(nd_last_lineno(data->node));
653}
654
655static VALUE
656ast_node_last_column(rb_execution_context_t *ec, VALUE self)
657{
658 struct ASTNodeData *data;
659 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
660
661 return INT2NUM(nd_last_column(data->node));
662}
663
664static VALUE
665ast_node_inspect(rb_execution_context_t *ec, VALUE self)
666{
667 VALUE str;
668 VALUE cname;
669 struct ASTNodeData *data;
670 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
671
672 cname = rb_class_path(rb_obj_class(self));
673 str = rb_str_new2("#<");
674
675 rb_str_append(str, cname);
676 rb_str_catf(str, ":%s@%d:%d-%d:%d>",
677 node_type_to_str(data->node),
679 nd_last_lineno(data->node), nd_last_column(data->node));
680
681 return str;
682}
683
684#include "ast.rbinc"
685
686void
688{
689 rb_mAST = rb_define_module_under(rb_cRubyVM, "AbstractSyntaxTree");
690 rb_cNode = rb_define_class_under(rb_mAST, "Node", rb_cObject);
691 rb_undef_alloc_func(rb_cNode);
692
693 load_ast();
694}
VALUE rb_e_script
Definition: ruby.c:1491
#define NEW_CHILD(ast, node)
Definition: ast.c:245
void Init_ast(void)
Definition: ast.c:687
struct RIMemo * ptr
Definition: debug.c:65
rb_encoding * rb_utf8_encoding(void)
Definition: encoding.c:1328
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:872
VALUE rb_enc_from_encoding(rb_encoding *encoding)
Definition: encoding.c:116
#define rb_enc_asciicompat(enc)
Definition: encoding.h:245
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
VALUE rb_define_class_under(VALUE, const char *, VALUE)
Defines a class under the namespace of outer.
Definition: class.c:711
VALUE rb_define_module_under(VALUE, const char *)
Definition: class.c:810
VALUE rb_cObject
Object class.
Definition: ruby.h:2012
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2671
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:668
void rb_bug(const char *fmt,...)
Definition: error.c:636
VALUE rb_eArgError
Definition: error.c:925
VALUE rb_obj_class(VALUE)
Equivalent to Object#class in Ruby.
Definition: object.c:217
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:39
const char * name
Definition: nkf.c:208
#define RARRAY_LEN(a)
#define rb_str_new2
VALUE rb_hash_lookup(VALUE, VALUE)
Definition: hash.c:2063
#define nd_pkwrestarg
void rb_ast_dispose(rb_ast_t *)
Definition: node.c:1387
#define NULL
VALUE rb_iseq_path(const rb_iseq_t *iseq)
Definition: iseq.c:1027
rb_ast_t * rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE input, int line)
Definition: ripper.c:13755
int memcmp(const void *, const void *, size_t)
Definition: memcmp.c:7
use StringValue() instead")))
#define RSTRING_LEN(str)
#define RTEST(v)
VALUE rb_parser_new(void)
Definition: ripper.c:20026
#define nd_last_lineno(n)
VALUE rb_parser_set_context(VALUE, const struct rb_iseq_struct *, int)
Definition: ripper.c:20036
#define nd_first_lineno(n)
#define CHAR_BIT
const VALUE VALUE obj
VALUE rb_io_close(VALUE)
Definition: io.c:4824
#define RSTRING_PTR(str)
int snprintf(char *__restrict__, size_t, const char *__restrict__,...) __attribute__((__format__(__printf__
#define GET_EC()
VALUE rb_obj_is_proc(VALUE)
Definition: proc.c:152
#define NIL_P(v)
#define ID2SYM(x)
#define RUBY_TYPED_DEFAULT_FREE
const char size_t n
VALUE rb_ary_push(VALUE, VALUE)
Definition: array.c:1195
VALUE rb_cRubyVM
Definition: vm.c:365
#define rb_sym_intern_ascii_cstr(ptr)
#define nd_first_column(n)
#define FilePathValue(v)
#define nd_node_id(n)
int rb_const_defined_at(VALUE, ID)
Definition: variable.c:2692
uint32_t i
#define char
VALUE rb_file_open_str(VALUE, const char *)
Definition: io.c:6256
VALUE rb_class_path(VALUE)
Definition: variable.c:153
#define INT2NUM(x)
#define NODE_NAMED_REST_P(node)
#define va_end(v)
VALUE rb_const_get_at(VALUE, ID)
Definition: variable.c:2397
#define T_HASH
__gnuc_va_list va_list
#define RUBY_TYPED_FREE_IMMEDIATELY
#define TypedData_Get_Struct(obj, type, data_type, sval)
#define rb_funcall(recv, mid, argc,...)
VALUE rb_ary_new(void)
Definition: array.c:723
void rb_gc_mark(VALUE)
Definition: gc.c:5228
#define rb_intern(str)
#define va_arg(v, l)
#define va_start(v, l)
VALUE rb_ary_new_capa(long capa)
Definition: array.c:717
const rb_iseq_t * iseq
VALUE rb_str_catf(VALUE, const char *,...) __attribute__((format(printf
#define CONST_ID(var, str)
const char * ruby_node_name(int node)
Definition: iseq.c:2534
unsigned int size
#define Qtrue
#define rb_strlen_lit(str)
#define NODE_SPECIAL_NO_REST_KEYWORD
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2965
size_t rb_ast_memsize(const rb_ast_t *)
Definition: node.c:1373
#define Qnil
#define Qfalse
#define T_ARRAY
#define nd_last_column(n)
#define RB_TYPE_P(obj, type)
VALUE rb_check_array_type(VALUE)
Definition: array.c:909
rb_ast_t * rb_parser_compile_generic(VALUE vparser, VALUE(*lex_gets)(VALUE, int), VALUE fname, VALUE input, int line)
Definition: ripper.c:13775
const rb_iseq_t * rb_method_iseq(VALUE body)
Definition: proc.c:2691
#define TypedData_Make_Struct(klass, type, data_type, sval)
#define NODE_REQUIRED_KEYWORD_P(node)
__inline__ int
#define CLASS_OF(v)
void rb_undef_alloc_func(VALUE)
Definition: vm_method.c:729
#define nd_type(n)
unsigned long ID
rb_ast_t * rb_parser_compile_string_path(VALUE vparser, VALUE fname, VALUE src, int line)
Definition: ripper.c:13740
#define rb_ary_new_from_args(n,...)
#define rb_ary_new2
#define NODE_SPECIAL_EXCESSIVE_COMMA
#define RARRAY_AREF(a, i)
#define rb_str_new_cstr(str)
VALUE rb_ary_entry(VALUE, long)
Definition: array.c:1512
unsigned long VALUE
Definition: ruby.h:102
#define f
NODE * node
Definition: ast.c:16
rb_ast_t * ast
Definition: ast.c:15
struct rb_iseq_constant_body * body
#define DECIMAL_SIZE_OF_BITS(n)
Definition: util.h:50
#define rb_id2str(id)
Definition: vm_backtrace.c:30