Ruby 3.2.1p31 (2023-02-08 revision 31819e82c88c6f8ecfaeb162519bfa26a14b21fd)
ast.c
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
19 rb_ast_t *ast;
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,
41 RUBY_TYPED_FREE_IMMEDIATELY,
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, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens);
68static VALUE rb_ast_parse_file(VALUE path, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens);
69
70static VALUE
71ast_parse_new(void)
72{
73 return rb_parser_set_context(rb_parser_new(), NULL, 0);
74}
75
76static VALUE
77ast_parse_done(rb_ast_t *ast)
78{
79 if (!ast->body.root) {
80 rb_ast_dispose(ast);
81 rb_exc_raise(GET_EC()->errinfo);
82 }
83
84 return ast_new_internal(ast, (NODE *)ast->body.root);
85}
86
87static VALUE
88ast_s_parse(rb_execution_context_t *ec, VALUE module, VALUE str, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
89{
90 return rb_ast_parse_str(str, keep_script_lines, error_tolerant, keep_tokens);
91}
92
93static VALUE
94rb_ast_parse_str(VALUE str, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
95{
96 rb_ast_t *ast = 0;
97
98 StringValue(str);
99 VALUE vparser = ast_parse_new();
100 if (RTEST(keep_script_lines)) rb_parser_keep_script_lines(vparser);
101 if (RTEST(error_tolerant)) rb_parser_error_tolerant(vparser);
102 if (RTEST(keep_tokens)) rb_parser_keep_tokens(vparser);
103 ast = rb_parser_compile_string_path(vparser, Qnil, str, 1);
104 return ast_parse_done(ast);
105}
106
107static VALUE
108ast_s_parse_file(rb_execution_context_t *ec, VALUE module, VALUE path, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
109{
110 return rb_ast_parse_file(path, keep_script_lines, error_tolerant, keep_tokens);
111}
112
113static VALUE
114rb_ast_parse_file(VALUE path, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
115{
116 VALUE f;
117 rb_ast_t *ast = 0;
118 rb_encoding *enc = rb_utf8_encoding();
119
120 FilePathValue(path);
121 f = rb_file_open_str(path, "r");
122 rb_funcall(f, rb_intern("set_encoding"), 2, rb_enc_from_encoding(enc), rb_str_new_cstr("-"));
123 VALUE vparser = ast_parse_new();
124 if (RTEST(keep_script_lines)) rb_parser_keep_script_lines(vparser);
125 if (RTEST(error_tolerant)) rb_parser_error_tolerant(vparser);
126 if (RTEST(keep_tokens)) rb_parser_keep_tokens(vparser);
127 ast = rb_parser_compile_file_path(vparser, Qnil, f, 1);
128 rb_io_close(f);
129 return ast_parse_done(ast);
130}
131
132static VALUE
133lex_array(VALUE array, int index)
134{
135 VALUE str = rb_ary_entry(array, index);
136 if (!NIL_P(str)) {
137 StringValue(str);
138 if (!rb_enc_asciicompat(rb_enc_get(str))) {
139 rb_raise(rb_eArgError, "invalid source encoding");
140 }
141 }
142 return str;
143}
144
145static VALUE
146rb_ast_parse_array(VALUE array, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
147{
148 rb_ast_t *ast = 0;
149
150 array = rb_check_array_type(array);
151 VALUE vparser = ast_parse_new();
152 if (RTEST(keep_script_lines)) rb_parser_keep_script_lines(vparser);
153 if (RTEST(error_tolerant)) rb_parser_error_tolerant(vparser);
154 if (RTEST(keep_tokens)) rb_parser_keep_tokens(vparser);
155 ast = rb_parser_compile_generic(vparser, lex_array, Qnil, array, 1);
156 return ast_parse_done(ast);
157}
158
159static VALUE node_children(rb_ast_t*, const NODE*);
160
161static VALUE
162node_find(VALUE self, const int node_id)
163{
164 VALUE ary;
165 long i;
166 struct ASTNodeData *data;
167 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
168
169 if (nd_node_id(data->node) == node_id) return self;
170
171 ary = node_children(data->ast, data->node);
172
173 for (i = 0; i < RARRAY_LEN(ary); i++) {
174 VALUE child = RARRAY_AREF(ary, i);
175
176 if (CLASS_OF(child) == rb_cNode) {
177 VALUE result = node_find(child, node_id);
178 if (RTEST(result)) return result;
179 }
180 }
181
182 return Qnil;
183}
184
185extern VALUE rb_e_script;
186
187static VALUE
188script_lines(VALUE path)
189{
190 VALUE hash, lines;
191 ID script_lines;
192 CONST_ID(script_lines, "SCRIPT_LINES__");
193 if (!rb_const_defined_at(rb_cObject, script_lines)) return Qnil;
194 hash = rb_const_get_at(rb_cObject, script_lines);
195 if (!RB_TYPE_P(hash, T_HASH)) return Qnil;
196 lines = rb_hash_lookup(hash, path);
197 if (!RB_TYPE_P(lines, T_ARRAY)) return Qnil;
198 return lines;
199}
200
201static VALUE
202node_id_for_backtrace_location(rb_execution_context_t *ec, VALUE module, VALUE location)
203{
204 int node_id;
205
206 if (!rb_frame_info_p(location)) {
207 rb_raise(rb_eTypeError, "Thread::Backtrace::Location object expected");
208 }
209
210 node_id = rb_get_node_id_from_frame_info(location);
211 if (node_id == -1) {
212 return Qnil;
213 }
214
215 return INT2NUM(node_id);
216}
217
218static VALUE
219ast_s_of(rb_execution_context_t *ec, VALUE module, VALUE body, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
220{
221 VALUE node, lines = Qnil;
222 const rb_iseq_t *iseq;
223 int node_id;
224
225 if (rb_frame_info_p(body)) {
226 iseq = rb_get_iseq_from_frame_info(body);
227 node_id = rb_get_node_id_from_frame_info(body);
228 }
229 else {
230 iseq = NULL;
231
232 if (rb_obj_is_proc(body)) {
233 iseq = vm_proc_iseq(body);
234
235 if (!rb_obj_is_iseq((VALUE)iseq)) return Qnil;
236 }
237 else {
238 iseq = rb_method_iseq(body);
239 }
240 if (iseq) {
241 node_id = ISEQ_BODY(iseq)->location.node_id;
242 }
243 }
244
245 if (!iseq) {
246 return Qnil;
247 }
248 lines = ISEQ_BODY(iseq)->variable.script_lines;
249
250 VALUE path = rb_iseq_path(iseq);
251 int e_option = RSTRING_LEN(path) == 2 && memcmp(RSTRING_PTR(path), "-e", 2) == 0;
252
253 if (NIL_P(lines) && rb_iseq_from_eval_p(iseq) && !e_option) {
254 rb_raise(rb_eArgError, "cannot get AST for method defined in eval");
255 }
256
257 if (!NIL_P(lines) || !NIL_P(lines = script_lines(path))) {
258 node = rb_ast_parse_array(lines, keep_script_lines, error_tolerant, keep_tokens);
259 }
260 else if (e_option) {
261 node = rb_ast_parse_str(rb_e_script, keep_script_lines, error_tolerant, keep_tokens);
262 }
263 else {
264 node = rb_ast_parse_file(path, keep_script_lines, error_tolerant, keep_tokens);
265 }
266
267 return node_find(node, node_id);
268}
269
270static VALUE
271rb_ast_node_alloc(VALUE klass)
272{
273 struct ASTNodeData *data;
274 VALUE obj = TypedData_Make_Struct(klass, struct ASTNodeData, &rb_node_type, data);
275
276 return obj;
277}
278
279static const char*
280node_type_to_str(const NODE *node)
281{
282 return (ruby_node_name(nd_type(node)) + rb_strlen_lit("NODE_"));
283}
284
285static VALUE
286ast_node_type(rb_execution_context_t *ec, VALUE self)
287{
288 struct ASTNodeData *data;
289 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
290
291 return rb_sym_intern_ascii_cstr(node_type_to_str(data->node));
292}
293
294static VALUE
295ast_node_node_id(rb_execution_context_t *ec, VALUE self)
296{
297 struct ASTNodeData *data;
298 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
299
300 return INT2FIX(nd_node_id(data->node));
301}
302
303#define NEW_CHILD(ast, node) node ? ast_new_internal(ast, node) : Qnil
304
305static VALUE
306rb_ary_new_from_node_args(rb_ast_t *ast, long n, ...)
307{
308 va_list ar;
309 VALUE ary;
310 long i;
311
312 ary = rb_ary_new2(n);
313
314 va_start(ar, n);
315 for (i=0; i<n; i++) {
316 NODE *node;
317 node = va_arg(ar, NODE *);
318 rb_ary_push(ary, NEW_CHILD(ast, node));
319 }
320 va_end(ar);
321 return ary;
322}
323
324static VALUE
325dump_block(rb_ast_t *ast, const NODE *node)
326{
327 VALUE ary = rb_ary_new();
328 do {
329 rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
330 } while (node->nd_next &&
331 nd_type_p(node->nd_next, NODE_BLOCK) &&
332 (node = node->nd_next, 1));
333 if (node->nd_next) {
334 rb_ary_push(ary, NEW_CHILD(ast, node->nd_next));
335 }
336
337 return ary;
338}
339
340static VALUE
341dump_array(rb_ast_t *ast, const NODE *node)
342{
343 VALUE ary = rb_ary_new();
344 rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
345
346 while (node->nd_next && nd_type_p(node->nd_next, NODE_LIST)) {
347 node = node->nd_next;
348 rb_ary_push(ary, NEW_CHILD(ast, node->nd_head));
349 }
350 rb_ary_push(ary, NEW_CHILD(ast, node->nd_next));
351
352 return ary;
353}
354
355static VALUE
356var_name(ID id)
357{
358 if (!id) return Qnil;
359 if (!rb_id2str(id)) return Qnil;
360 return ID2SYM(id);
361}
362
363static VALUE
364no_name_rest(void)
365{
366 ID rest;
367 CONST_ID(rest, "NODE_SPECIAL_NO_NAME_REST");
368 return ID2SYM(rest);
369}
370
371static VALUE
372rest_arg(rb_ast_t *ast, const NODE *rest_arg)
373{
374 return NODE_NAMED_REST_P(rest_arg) ? NEW_CHILD(ast, rest_arg) : no_name_rest();
375}
376
377static VALUE
378node_children(rb_ast_t *ast, const NODE *node)
379{
380 char name[DECIMAL_SIZE_OF_BITS(sizeof(long) * CHAR_BIT) + 2]; /* including '$' */
381
382 enum node_type type = nd_type(node);
383 switch (type) {
384 case NODE_BLOCK:
385 return dump_block(ast, node);
386 case NODE_IF:
387 return rb_ary_new_from_node_args(ast, 3, node->nd_cond, node->nd_body, node->nd_else);
388 case NODE_UNLESS:
389 return rb_ary_new_from_node_args(ast, 3, node->nd_cond, node->nd_body, node->nd_else);
390 case NODE_CASE:
391 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
392 case NODE_CASE2:
393 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
394 case NODE_CASE3:
395 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
396 case NODE_WHEN:
397 return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_body, node->nd_next);
398 case NODE_IN:
399 return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_body, node->nd_next);
400 case NODE_WHILE:
401 case NODE_UNTIL:
402 return rb_ary_push(rb_ary_new_from_node_args(ast, 2, node->nd_cond, node->nd_body),
403 RBOOL(node->nd_state));
404 case NODE_ITER:
405 case NODE_FOR:
406 return rb_ary_new_from_node_args(ast, 2, node->nd_iter, node->nd_body);
407 case NODE_FOR_MASGN:
408 return rb_ary_new_from_node_args(ast, 1, node->nd_var);
409 case NODE_BREAK:
410 case NODE_NEXT:
411 case NODE_RETURN:
412 return rb_ary_new_from_node_args(ast, 1, node->nd_stts);
413 case NODE_REDO:
414 return rb_ary_new_from_node_args(ast, 0);
415 case NODE_RETRY:
416 return rb_ary_new_from_node_args(ast, 0);
417 case NODE_BEGIN:
418 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
419 case NODE_RESCUE:
420 return rb_ary_new_from_node_args(ast, 3, node->nd_head, node->nd_resq, node->nd_else);
421 case NODE_RESBODY:
422 return rb_ary_new_from_node_args(ast, 3, node->nd_args, node->nd_body, node->nd_head);
423 case NODE_ENSURE:
424 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_ensr);
425 case NODE_AND:
426 case NODE_OR:
427 {
428 VALUE ary = rb_ary_new();
429
430 while (1) {
431 rb_ary_push(ary, NEW_CHILD(ast, node->nd_1st));
432 if (!node->nd_2nd || !nd_type_p(node->nd_2nd, type))
433 break;
434 node = node->nd_2nd;
435 }
436 rb_ary_push(ary, NEW_CHILD(ast, node->nd_2nd));
437 return ary;
438 }
439 case NODE_MASGN:
440 if (NODE_NAMED_REST_P(node->nd_args)) {
441 return rb_ary_new_from_node_args(ast, 3, node->nd_value, node->nd_head, node->nd_args);
442 }
443 else {
444 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_value),
445 NEW_CHILD(ast, node->nd_head),
446 no_name_rest());
447 }
448 case NODE_LASGN:
449 case NODE_DASGN:
450 case NODE_IASGN:
451 case NODE_CVASGN:
452 case NODE_GASGN:
453 if (NODE_REQUIRED_KEYWORD_P(node)) {
454 return rb_ary_new_from_args(2, var_name(node->nd_vid), ID2SYM(rb_intern("NODE_SPECIAL_REQUIRED_KEYWORD")));
455 }
456 return rb_ary_new_from_args(2, var_name(node->nd_vid), NEW_CHILD(ast, node->nd_value));
457 case NODE_CDECL:
458 if (node->nd_vid) {
459 return rb_ary_new_from_args(2, ID2SYM(node->nd_vid), NEW_CHILD(ast, node->nd_value));
460 }
461 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));
462 case NODE_OP_ASGN1:
463 return rb_ary_new_from_args(4, NEW_CHILD(ast, node->nd_recv),
464 ID2SYM(node->nd_mid),
465 NEW_CHILD(ast, node->nd_args->nd_head),
466 NEW_CHILD(ast, node->nd_args->nd_body));
467 case NODE_OP_ASGN2:
468 return rb_ary_new_from_args(5, NEW_CHILD(ast, node->nd_recv),
469 RBOOL(node->nd_next->nd_aid),
470 ID2SYM(node->nd_next->nd_vid),
471 ID2SYM(node->nd_next->nd_mid),
472 NEW_CHILD(ast, node->nd_value));
473 case NODE_OP_ASGN_AND:
474 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_head), ID2SYM(idANDOP),
475 NEW_CHILD(ast, node->nd_value));
476 case NODE_OP_ASGN_OR:
477 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_head), ID2SYM(idOROP),
478 NEW_CHILD(ast, node->nd_value));
479 case NODE_OP_CDECL:
480 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_head),
481 ID2SYM(node->nd_aid),
482 NEW_CHILD(ast, node->nd_value));
483 case NODE_CALL:
484 case NODE_OPCALL:
485 case NODE_QCALL:
486 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_recv),
487 ID2SYM(node->nd_mid),
488 NEW_CHILD(ast, node->nd_args));
489 case NODE_FCALL:
490 return rb_ary_new_from_args(2, ID2SYM(node->nd_mid),
491 NEW_CHILD(ast, node->nd_args));
492 case NODE_VCALL:
493 return rb_ary_new_from_args(1, ID2SYM(node->nd_mid));
494 case NODE_SUPER:
495 return rb_ary_new_from_node_args(ast, 1, node->nd_args);
496 case NODE_ZSUPER:
497 return rb_ary_new_from_node_args(ast, 0);
498 case NODE_LIST:
499 case NODE_VALUES:
500 return dump_array(ast, node);
501 case NODE_ZLIST:
502 return rb_ary_new_from_node_args(ast, 0);
503 case NODE_HASH:
504 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
505 case NODE_YIELD:
506 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
507 case NODE_LVAR:
508 case NODE_DVAR:
509 return rb_ary_new_from_args(1, var_name(node->nd_vid));
510 case NODE_IVAR:
511 case NODE_CONST:
512 case NODE_CVAR:
513 case NODE_GVAR:
514 return rb_ary_new_from_args(1, ID2SYM(node->nd_vid));
515 case NODE_NTH_REF:
516 snprintf(name, sizeof(name), "$%ld", node->nd_nth);
517 return rb_ary_new_from_args(1, ID2SYM(rb_intern(name)));
518 case NODE_BACK_REF:
519 name[0] = '$';
520 name[1] = (char)node->nd_nth;
521 name[2] = '\0';
522 return rb_ary_new_from_args(1, ID2SYM(rb_intern(name)));
523 case NODE_MATCH2:
524 if (node->nd_args) {
525 return rb_ary_new_from_node_args(ast, 3, node->nd_recv, node->nd_value, node->nd_args);
526 }
527 return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_value);
528 case NODE_MATCH3:
529 return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_value);
530 case NODE_MATCH:
531 case NODE_LIT:
532 case NODE_STR:
533 case NODE_XSTR:
534 return rb_ary_new_from_args(1, node->nd_lit);
535 case NODE_ONCE:
536 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
537 case NODE_DSTR:
538 case NODE_DXSTR:
539 case NODE_DREGX:
540 case NODE_DSYM:
541 {
542 NODE *n = node->nd_next;
543 VALUE head = Qnil, next = Qnil;
544 if (n) {
545 head = NEW_CHILD(ast, n->nd_head);
546 next = NEW_CHILD(ast, n->nd_next);
547 }
548 return rb_ary_new_from_args(3, node->nd_lit, head, next);
549 }
550 case NODE_EVSTR:
551 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
552 case NODE_ARGSCAT:
553 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
554 case NODE_ARGSPUSH:
555 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
556 case NODE_SPLAT:
557 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
558 case NODE_BLOCK_PASS:
559 return rb_ary_new_from_node_args(ast, 2, node->nd_head, node->nd_body);
560 case NODE_DEFN:
561 return rb_ary_new_from_args(2, ID2SYM(node->nd_mid), NEW_CHILD(ast, node->nd_defn));
562 case NODE_DEFS:
563 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_recv), ID2SYM(node->nd_mid), NEW_CHILD(ast, node->nd_defn));
564 case NODE_ALIAS:
565 return rb_ary_new_from_node_args(ast, 2, node->nd_1st, node->nd_2nd);
566 case NODE_VALIAS:
567 return rb_ary_new_from_args(2, ID2SYM(node->nd_alias), ID2SYM(node->nd_orig));
568 case NODE_UNDEF:
569 return rb_ary_new_from_node_args(ast, 1, node->nd_undef);
570 case NODE_CLASS:
571 return rb_ary_new_from_node_args(ast, 3, node->nd_cpath, node->nd_super, node->nd_body);
572 case NODE_MODULE:
573 return rb_ary_new_from_node_args(ast, 2, node->nd_cpath, node->nd_body);
574 case NODE_SCLASS:
575 return rb_ary_new_from_node_args(ast, 2, node->nd_recv, node->nd_body);
576 case NODE_COLON2:
577 return rb_ary_new_from_args(2, NEW_CHILD(ast, node->nd_head), ID2SYM(node->nd_mid));
578 case NODE_COLON3:
579 return rb_ary_new_from_args(1, ID2SYM(node->nd_mid));
580 case NODE_DOT2:
581 case NODE_DOT3:
582 case NODE_FLIP2:
583 case NODE_FLIP3:
584 return rb_ary_new_from_node_args(ast, 2, node->nd_beg, node->nd_end);
585 case NODE_SELF:
586 return rb_ary_new_from_node_args(ast, 0);
587 case NODE_NIL:
588 return rb_ary_new_from_node_args(ast, 0);
589 case NODE_TRUE:
590 return rb_ary_new_from_node_args(ast, 0);
591 case NODE_FALSE:
592 return rb_ary_new_from_node_args(ast, 0);
593 case NODE_ERRINFO:
594 return rb_ary_new_from_node_args(ast, 0);
595 case NODE_DEFINED:
596 return rb_ary_new_from_node_args(ast, 1, node->nd_head);
597 case NODE_POSTEXE:
598 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
599 case NODE_ATTRASGN:
600 return rb_ary_new_from_args(3, NEW_CHILD(ast, node->nd_recv), ID2SYM(node->nd_mid), NEW_CHILD(ast, node->nd_args));
601 case NODE_LAMBDA:
602 return rb_ary_new_from_node_args(ast, 1, node->nd_body);
603 case NODE_OPT_ARG:
604 return rb_ary_new_from_node_args(ast, 2, node->nd_body, node->nd_next);
605 case NODE_KW_ARG:
606 return rb_ary_new_from_node_args(ast, 2, node->nd_body, node->nd_next);
607 case NODE_POSTARG:
608 if (NODE_NAMED_REST_P(node->nd_1st)) {
609 return rb_ary_new_from_node_args(ast, 2, node->nd_1st, node->nd_2nd);
610 }
611 return rb_ary_new_from_args(2, no_name_rest(),
612 NEW_CHILD(ast, node->nd_2nd));
613 case NODE_ARGS:
614 {
615 struct rb_args_info *ainfo = node->nd_ainfo;
616 return rb_ary_new_from_args(10,
617 INT2NUM(ainfo->pre_args_num),
618 NEW_CHILD(ast, ainfo->pre_init),
619 NEW_CHILD(ast, ainfo->opt_args),
620 var_name(ainfo->first_post_arg),
621 INT2NUM(ainfo->post_args_num),
622 NEW_CHILD(ast, ainfo->post_init),
623 (ainfo->rest_arg == NODE_SPECIAL_EXCESSIVE_COMMA
624 ? ID2SYM(rb_intern("NODE_SPECIAL_EXCESSIVE_COMMA"))
625 : var_name(ainfo->rest_arg)),
626 (ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast, ainfo->kw_args)),
627 (ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast, ainfo->kw_rest_arg)),
628 var_name(ainfo->block_arg));
629 }
630 case NODE_SCOPE:
631 {
632 rb_ast_id_table_t *tbl = node->nd_tbl;
633 int i, size = tbl ? tbl->size : 0;
634 VALUE locals = rb_ary_new_capa(size);
635 for (i = 0; i < size; i++) {
636 rb_ary_push(locals, var_name(tbl->ids[i]));
637 }
638 return rb_ary_new_from_args(3, locals, NEW_CHILD(ast, node->nd_args), NEW_CHILD(ast, node->nd_body));
639 }
640 case NODE_ARYPTN:
641 {
642 struct rb_ary_pattern_info *apinfo = node->nd_apinfo;
643 VALUE rest = rest_arg(ast, apinfo->rest_arg);
644 return rb_ary_new_from_args(4,
645 NEW_CHILD(ast, node->nd_pconst),
646 NEW_CHILD(ast, apinfo->pre_args),
647 rest,
648 NEW_CHILD(ast, apinfo->post_args));
649 }
650 case NODE_FNDPTN:
651 {
652 struct rb_fnd_pattern_info *fpinfo = node->nd_fpinfo;
653 VALUE pre_rest = rest_arg(ast, fpinfo->pre_rest_arg);
654 VALUE post_rest = rest_arg(ast, fpinfo->post_rest_arg);
655 return rb_ary_new_from_args(4,
656 NEW_CHILD(ast, node->nd_pconst),
657 pre_rest,
658 NEW_CHILD(ast, fpinfo->args),
659 post_rest);
660 }
661 case NODE_HSHPTN:
662 {
663 VALUE kwrest = node->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD ? ID2SYM(rb_intern("NODE_SPECIAL_NO_REST_KEYWORD")) :
664 NEW_CHILD(ast, node->nd_pkwrestarg);
665
666 return rb_ary_new_from_args(3,
667 NEW_CHILD(ast, node->nd_pconst),
668 NEW_CHILD(ast, node->nd_pkwargs),
669 kwrest);
670 }
671 case NODE_ERROR:
672 return rb_ary_new_from_node_args(ast, 0);
673 case NODE_ARGS_AUX:
674 case NODE_LAST:
675 break;
676 }
677
678 rb_bug("node_children: unknown node: %s", ruby_node_name(type));
679}
680
681static VALUE
682ast_node_children(rb_execution_context_t *ec, VALUE self)
683{
684 struct ASTNodeData *data;
685 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
686
687 return node_children(data->ast, data->node);
688}
689
690static VALUE
691ast_node_first_lineno(rb_execution_context_t *ec, VALUE self)
692{
693 struct ASTNodeData *data;
694 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
695
696 return INT2NUM(nd_first_lineno(data->node));
697}
698
699static VALUE
700ast_node_first_column(rb_execution_context_t *ec, VALUE self)
701{
702 struct ASTNodeData *data;
703 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
704
705 return INT2NUM(nd_first_column(data->node));
706}
707
708static VALUE
709ast_node_last_lineno(rb_execution_context_t *ec, VALUE self)
710{
711 struct ASTNodeData *data;
712 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
713
714 return INT2NUM(nd_last_lineno(data->node));
715}
716
717static VALUE
718ast_node_last_column(rb_execution_context_t *ec, VALUE self)
719{
720 struct ASTNodeData *data;
721 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
722
723 return INT2NUM(nd_last_column(data->node));
724}
725
726static VALUE
727ast_node_all_tokens(rb_execution_context_t *ec, VALUE self)
728{
729 struct ASTNodeData *data;
730 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
731
732 return rb_ast_tokens(data->ast);
733}
734
735static VALUE
736ast_node_inspect(rb_execution_context_t *ec, VALUE self)
737{
738 VALUE str;
739 VALUE cname;
740 struct ASTNodeData *data;
741 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
742
743 cname = rb_class_path(rb_obj_class(self));
744 str = rb_str_new2("#<");
745
746 rb_str_append(str, cname);
747 rb_str_catf(str, ":%s@%d:%d-%d:%d>",
748 node_type_to_str(data->node),
749 nd_first_lineno(data->node), nd_first_column(data->node),
750 nd_last_lineno(data->node), nd_last_column(data->node));
751
752 return str;
753}
754
755static VALUE
756ast_node_script_lines(rb_execution_context_t *ec, VALUE self)
757{
758 struct ASTNodeData *data;
759 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
760 VALUE ret = data->ast->body.script_lines;
761 if (!RB_TYPE_P(ret, T_ARRAY)) return Qnil;
762 return ret;
763}
764
765#include "ast.rbinc"
766
767void
768Init_ast(void)
769{
770 rb_mAST = rb_define_module_under(rb_cRubyVM, "AbstractSyntaxTree");
771 rb_cNode = rb_define_class_under(rb_mAST, "Node", rb_cObject);
772 rb_undef_alloc_func(rb_cNode);
773}
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:920
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
Definition: class.c:1022
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition: string.h:1675
#define INT2FIX
Old name of RB_INT2FIX.
Definition: long.h:48
#define ID2SYM
Old name of RB_ID2SYM.
Definition: symbol.h:44
#define CLASS_OF
Old name of rb_class_of.
Definition: globals.h:203
#define T_HASH
Old name of RUBY_T_HASH.
Definition: value_type.h:65
#define INT2NUM
Old name of RB_INT2NUM.
Definition: int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition: value_type.h:56
#define NIL_P
Old name of RB_NIL_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition: symbol.h:47
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition: array.h:651
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition: error.c:3148
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:684
void rb_bug(const char *fmt,...)
Interpreter panic switch.
Definition: error.c:794
VALUE rb_eTypeError
TypeError exception.
Definition: error.c:1091
VALUE rb_eArgError
ArgumentError exception.
Definition: error.c:1092
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition: object.c:190
Encoding relates APIs.
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_file_open_str(VALUE fname, const char *fmode)
Identical to rb_file_open(), except it takes the pathname as a Ruby's string instead of C's.
Definition: io.c:7166
VALUE rb_io_close(VALUE io)
Closes the IO.
Definition: io.c:5668
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition: proc.c:175
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition: string.c:3323
#define rb_strlen_lit(str)
Length of a string literal.
Definition: string.h:1692
#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_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
Definition: variable.c:2889
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
Definition: variable.c:3197
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition: variable.c:185
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition: vm_method.c:1159
#define DECIMAL_SIZE_OF_BITS(n)
an approximation of ceil(n * log10(2)), up to 65536 at least
Definition: util.h:37
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
Definition: sprintf.c:1242
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
#define RARRAY_LEN
Just another name of rb_array_len.
Definition: rarray.h:68
#define RARRAY_AREF(a, i)
Definition: rarray.h:583
#define StringValue(v)
Ensures that the parameter object is a String.
Definition: rstring.h:72
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
Definition: rtypeddata.h:79
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition: rtypeddata.h:507
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
Definition: rtypeddata.h:489
#define FilePathValue(v)
Ensures that the parameter object is a path.
Definition: ruby.h:91
#define RTEST
This is an old name of RB_TEST.
Definition: node.h:156
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition: value.h:52