Ruby 4.1.0dev (2026-09-22 revision 1f717ae3d6ca76015582dc610e892be1aacb905b)
ast.c (1f717ae3d6ca76015582dc610e892be1aacb905b)
1/* indent-tabs-mode: nil */
2#include "internal.h"
3#include "internal/ruby_parser.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;
17static VALUE rb_cLocation;
18
20 VALUE ast_value;
21 const NODE *node;
22};
23
24static void
25node_gc_mark(void *ptr)
26{
27 struct ASTNodeData *data = (struct ASTNodeData *)ptr;
28 rb_gc_mark(data->ast_value);
29}
30
31static size_t
32node_memsize(const void *ptr)
33{
34 struct ASTNodeData *data = (struct ASTNodeData *)ptr;
35 size_t size = sizeof(struct ASTNodeData);
36 if (data->ast_value) {
37 rb_ast_t *ast = rb_ruby_ast_data_get(data->ast_value);
38 size += rb_ast_memsize(ast);
39 }
40
41 return size;
42}
43
44static const rb_data_type_t rb_node_type = {
45 "AST/node",
46 {node_gc_mark, RUBY_TYPED_DEFAULT_FREE, node_memsize,},
47 0, 0,
48 RUBY_TYPED_THREAD_SAFE_FREE,
49};
50
52 int first_lineno;
53 int first_column;
54 int last_lineno;
55 int last_column;
56};
57
58static void
59location_gc_mark(void *ptr)
60{
61}
62
63static size_t
64location_memsize(const void *ptr)
65{
66 return sizeof(struct ASTLocationData);
67}
68
69static const rb_data_type_t rb_location_type = {
70 "AST/location",
71 {location_gc_mark, RUBY_TYPED_DEFAULT_FREE, location_memsize,},
72 0, 0,
73 RUBY_TYPED_THREAD_SAFE_FREE,
74};
75
76
77static VALUE rb_ast_node_alloc(VALUE klass);
78
79static void
80setup_node(VALUE obj, VALUE ast_value, const NODE *node)
81{
82 struct ASTNodeData *data;
83
84 TypedData_Get_Struct(obj, struct ASTNodeData, &rb_node_type, data);
85 data->ast_value = ast_value;
86 data->node = node;
87}
88
89static VALUE
90ast_new_internal(VALUE ast_value, const NODE *node)
91{
92 VALUE obj;
93
94 obj = rb_ast_node_alloc(rb_cNode);
95 setup_node(obj, ast_value, node);
96
97 return obj;
98}
99
100static VALUE rb_ast_parse_str(VALUE str, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens);
101static VALUE rb_ast_parse_file(VALUE path, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens);
102
103static VALUE
104ast_parse_new(void)
105{
106 return rb_parser_set_context(rb_parser_new(), NULL, 0);
107}
108
109static VALUE
110ast_parse_done(VALUE ast_value)
111{
112 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
113
114 if (!ast->body.root) {
115 rb_ast_dispose(ast);
116 rb_exc_raise(GET_EC()->errinfo);
117 }
118
119 return ast_new_internal(ast_value, (NODE *)ast->body.root);
120}
121
122static VALUE
123setup_vparser(VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
124{
125 VALUE vparser = ast_parse_new();
126 if (RTEST(keep_script_lines)) rb_parser_set_script_lines(vparser);
127 if (RTEST(error_tolerant)) rb_parser_error_tolerant(vparser);
128 if (RTEST(keep_tokens)) rb_parser_keep_tokens(vparser);
129 return vparser;
130}
131
132static VALUE
133ast_s_parse(rb_execution_context_t *ec, VALUE module, VALUE str, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
134{
135 return rb_ast_parse_str(str, keep_script_lines, error_tolerant, keep_tokens);
136}
137
138static VALUE
139rb_ast_parse_str(VALUE str, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
140{
141 VALUE ast_value = Qnil;
142 StringValue(str);
143 VALUE vparser = setup_vparser(keep_script_lines, error_tolerant, keep_tokens);
144 ast_value = rb_parser_compile_string_path(vparser, Qnil, str, 1);
145 return ast_parse_done(ast_value);
146}
147
148static VALUE
149ast_s_parse_file(rb_execution_context_t *ec, VALUE module, VALUE path, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
150{
151 return rb_ast_parse_file(path, keep_script_lines, error_tolerant, keep_tokens);
152}
153
154static VALUE
155rb_ast_parse_file(VALUE path, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
156{
157 VALUE f;
158 VALUE ast_value = Qnil;
159 rb_encoding *enc = rb_utf8_encoding();
160
161 f = rb_file_open_str(path, "r");
162 rb_funcall(f, rb_intern("set_encoding"), 2, rb_enc_from_encoding(enc), rb_str_new_cstr("-"));
163 VALUE vparser = setup_vparser(keep_script_lines, error_tolerant, keep_tokens);
164 ast_value = rb_parser_compile_file_path(vparser, Qnil, f, 1);
165 rb_io_close(f);
166 return ast_parse_done(ast_value);
167}
168
169static VALUE
170rb_ast_parse_array(VALUE array, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
171{
172 VALUE ast_value = Qnil;
173
174 array = rb_check_array_type(array);
175 VALUE vparser = setup_vparser(keep_script_lines, error_tolerant, keep_tokens);
176 ast_value = rb_parser_compile_array(vparser, Qnil, array, 1);
177 return ast_parse_done(ast_value);
178}
179
180static VALUE node_children(VALUE, const NODE*);
181
183 VALUE node;
184 VALUE parent;
185};
186
187static bool
188node_find_with_parent(VALUE self, VALUE parent, const int node_id, struct node_find_result *result)
189{
190 VALUE ary;
191 long i;
192 struct ASTNodeData *data;
193 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
194
195 if (nd_node_id(data->node) == node_id) {
196 result->node = self;
197 result->parent = parent;
198 return true;
199 }
200
201 ary = node_children(data->ast_value, data->node);
202
203 for (i = 0; i < RARRAY_LEN(ary); i++) {
204 VALUE child = RARRAY_AREF(ary, i);
205
206 if (CLASS_OF(child) == rb_cNode) {
207 if (node_find_with_parent(child, self, node_id, result)) return true;
208 }
209 }
210
211 return false;
212}
213
214static VALUE
215node_find(VALUE self, const int node_id)
216{
217 struct node_find_result result = { Qnil, Qnil };
218 node_find_with_parent(self, Qnil, node_id, &result);
219 return result.node;
220}
221
222bool
223rb_ast_node_source_location(VALUE source, VALUE path, int first_lineno,
224 int node_id, bool block_iseq, int iseq_node_id,
225 rb_code_location_t *location)
226{
227 StringValue(source);
228 VALUE vparser = setup_vparser(Qfalse, Qfalse, Qfalse);
229 VALUE ast_value = rb_parser_compile_string_path(vparser, path, source, first_lineno);
230 VALUE ast = ast_parse_done(ast_value);
231
232 struct node_find_result result = { Qnil, Qnil };
233 if (!node_find_with_parent(ast, Qnil, node_id, &result)) return false;
234
235 struct ASTNodeData *data;
236 TypedData_Get_Struct(result.node, struct ASTNodeData, &rb_node_type, data);
237 const NODE *node = data->node;
238
239 if (!NIL_P(result.parent)) {
240 struct ASTNodeData *parent_data;
241 TypedData_Get_Struct(result.parent, struct ASTNodeData, &rb_node_type, parent_data);
242 const NODE *parent = parent_data->node;
243
244 /* Prism's call node includes its literal block. */
245 if (nd_type(parent) == NODE_ITER && RNODE_ITER(parent)->nd_iter == node) {
246 node = parent;
247 }
248 }
249
250 /* Prism's block node excludes the call that produced the block. */
251 if (block_iseq && node_id == iseq_node_id && nd_type(node) == NODE_ITER) {
252 const NODE *scope = RNODE_ITER(node)->nd_body;
253 if (scope && nd_type(scope) == NODE_SCOPE) {
254 node = scope;
255 }
256 }
257
258 *location = *nd_code_loc(node);
259 return true;
260}
261
262static VALUE
263ast_node_find(rb_execution_context_t *ec, VALUE self, VALUE root, VALUE node_id)
264{
265 return node_find(root, NUM2INT(node_id));
266}
267
268static VALUE
269ast_node_source_hash(rb_execution_context_t *ec, VALUE self, VALUE node)
270{
271 struct ASTNodeData *data;
272 TypedData_Get_Struct(node, struct ASTNodeData, &rb_node_type, data);
273
274 rb_ast_t *ast = rb_ruby_ast_data_get(data->ast_value);
275 if (!ast->body.has_source_hash) return Qnil;
276 return ULL2NUM(ast->body.source_hash);
277}
278
279extern VALUE rb_e_script;
280
281static VALUE
282iseq_compiled_by_prism_p(rb_execution_context_t *ec, VALUE self)
283{
284 return RBOOL(ISEQ_BODY(rb_iseqw_to_iseq(self))->prism);
285}
286
287static VALUE
288source_hash_of(rb_execution_context_t *ec, VALUE self, VALUE str)
289{
290 StringValue(str);
291
293 rb_source_hash_init(&state);
294 rb_source_hash_update(&state, (const uint8_t *)RSTRING_PTR(str), (size_t)RSTRING_LEN(str));
295 return ULL2NUM(rb_source_hash_finalize(&state));
296}
297
298static VALUE
299node_id_for_backtrace_location(rb_execution_context_t *ec, VALUE module, VALUE location)
300{
301 int node_id;
302
303 if (!rb_frame_info_p(location)) {
304 rb_raise(rb_eTypeError, "Thread::Backtrace::Location object expected");
305 }
306
307 node_id = rb_get_node_id_from_frame_info(location);
308 if (node_id == -1) {
309 return Qnil;
310 }
311
312 return INT2NUM(node_id);
313}
314
315static VALUE
316iseq_of_backtrace_location(rb_execution_context_t *ec, VALUE module, VALUE location)
317{
318 if (!rb_frame_info_p(location)) {
319 rb_raise(rb_eTypeError, "Thread::Backtrace::Location object expected");
320 }
321
322 const rb_iseq_t *iseq = rb_get_iseq_from_frame_info(location);
323 if (!iseq) return Qnil;
324 return rb_iseqw_new(iseq);
325}
326
327static VALUE
328ast_s_of(rb_execution_context_t *ec, VALUE module, VALUE body, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
329{
330 VALUE node, lines = Qnil;
331 const rb_iseq_t *iseq;
332 int node_id;
333
334 if (rb_frame_info_p(body)) {
335 iseq = rb_get_iseq_from_frame_info(body);
336 node_id = rb_get_node_id_from_frame_info(body);
337 }
338 else {
339 iseq = NULL;
340
341 if (rb_obj_is_proc(body)) {
342 iseq = vm_proc_iseq(body);
343
344 if (!rb_obj_is_iseq((VALUE)iseq)) return Qnil;
345 }
346 else {
347 iseq = rb_method_iseq(body);
348 }
349 if (iseq) {
350 node_id = ISEQ_BODY(iseq)->location.node_id;
351 }
352 }
353
354 if (!iseq) {
355 return Qnil;
356 }
357
358 if (ISEQ_BODY(iseq)->prism) {
359 rb_raise(rb_eRuntimeError, "cannot get AST for ISEQ compiled by prism");
360 }
361
362 lines = ISEQ_SCRIPT_LINES(iseq);
363
364 VALUE path = rb_iseq_path(iseq);
365 int e_option = RSTRING_LEN(path) == 2 && memcmp(RSTRING_PTR(path), "-e", 2) == 0;
366
367 if (NIL_P(lines) && rb_iseq_from_eval_p(iseq) && !e_option) {
368 rb_raise(rb_eArgError, "cannot get AST for method defined in eval");
369 }
370
371 if (!NIL_P(lines)) {
372 node = rb_ast_parse_array(lines, keep_script_lines, error_tolerant, keep_tokens);
373 }
374 else if (e_option) {
375 node = rb_ast_parse_str(rb_e_script, keep_script_lines, error_tolerant, keep_tokens);
376 }
377 else {
378 node = rb_ast_parse_file(path, keep_script_lines, error_tolerant, keep_tokens);
379 }
380
381 return node_find(node, node_id);
382}
383
384static VALUE
385rb_ast_node_alloc(VALUE klass)
386{
387 struct ASTNodeData *data;
388 VALUE obj = TypedData_Make_Struct(klass, struct ASTNodeData, &rb_node_type, data);
389
390 return obj;
391}
392
393static const char*
394node_type_to_str(const NODE *node)
395{
396 return (ruby_node_name(nd_type(node)) + rb_strlen_lit("NODE_"));
397}
398
399static VALUE
400ast_node_type(rb_execution_context_t *ec, VALUE self)
401{
402 struct ASTNodeData *data;
403 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
404
405 return rb_sym_intern_ascii_cstr(node_type_to_str(data->node));
406}
407
408static VALUE
409ast_node_node_id(rb_execution_context_t *ec, VALUE self)
410{
411 struct ASTNodeData *data;
412 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
413
414 return INT2FIX(nd_node_id(data->node));
415}
416
417#define NEW_CHILD(ast_value, node) (node ? ast_new_internal(ast_value, node) : Qnil)
418
419static VALUE
420rb_ary_new_from_node_args(VALUE ast_value, long n, ...)
421{
422 va_list ar;
423 VALUE ary;
424 long i;
425
426 ary = rb_ary_new2(n);
427
428 va_start(ar, n);
429 for (i=0; i<n; i++) {
430 NODE *node;
431 node = va_arg(ar, NODE *);
432 rb_ary_push(ary, NEW_CHILD(ast_value, node));
433 }
434 va_end(ar);
435 return ary;
436}
437
438static VALUE
439dump_block(VALUE ast_value, const struct RNode_BLOCK *node)
440{
441 VALUE ary = rb_ary_new();
442 do {
443 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_head));
444 } while (node->nd_next &&
445 nd_type_p(node->nd_next, NODE_BLOCK) &&
446 (node = RNODE_BLOCK(node->nd_next), 1));
447 if (node->nd_next) {
448 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_next));
449 }
450
451 return ary;
452}
453
454static VALUE
455dump_array(VALUE ast_value, const struct RNode_LIST *node)
456{
457 VALUE ary = rb_ary_new();
458 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_head));
459
460 while (node->nd_next && nd_type_p(node->nd_next, NODE_LIST)) {
461 node = RNODE_LIST(node->nd_next);
462 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_head));
463 }
464 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_next));
465
466 return ary;
467}
468
469static VALUE
470dump_parser_array(VALUE ast_value, rb_parser_ary_t *p_ary)
471{
472 VALUE ary;
473
474 if (p_ary->data_type != PARSER_ARY_DATA_NODE) {
475 rb_bug("unexpected rb_parser_ary_data_type: %d", p_ary->data_type);
476 }
477
478 ary = rb_ary_new();
479
480 for (long i = 0; i < p_ary->len; i++) {
481 rb_ary_push(ary, NEW_CHILD(ast_value, p_ary->data[i]));
482 }
483
484 return ary;
485}
486
487static VALUE
488var_name(ID id)
489{
490 if (!id) return Qnil;
491 if (!rb_id2str(id)) return Qnil;
492 return ID2SYM(id);
493}
494
495static VALUE
496no_name_rest(void)
497{
498 ID rest;
499 CONST_ID(rest, "NODE_SPECIAL_NO_NAME_REST");
500 return ID2SYM(rest);
501}
502
503static VALUE
504rest_arg(VALUE ast_value, const NODE *rest_arg)
505{
506 return NODE_NAMED_REST_P(rest_arg) ? NEW_CHILD(ast_value, rest_arg) : no_name_rest();
507}
508
509static ID
510node_colon_name(const NODE *node)
511{
512 switch (nd_type(node)) {
513 case NODE_COLON2:
514 return RNODE_COLON2(node)->nd_mid;
515 case NODE_COLON3:
516 return RNODE_COLON3(node)->nd_mid;
517 default:
518 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
519 }
520}
521
522static VALUE
523node_children(VALUE ast_value, const NODE *node)
524{
525 char name[sizeof("$") + DECIMAL_SIZE_OF(long)];
526
527 enum node_type type = nd_type(node);
528 switch (type) {
529 case NODE_BLOCK:
530 return dump_block(ast_value, RNODE_BLOCK(node));
531 case NODE_IF:
532 return rb_ary_new_from_node_args(ast_value, 3, RNODE_IF(node)->nd_cond, RNODE_IF(node)->nd_body, RNODE_IF(node)->nd_else);
533 case NODE_UNLESS:
534 return rb_ary_new_from_node_args(ast_value, 3, RNODE_UNLESS(node)->nd_cond, RNODE_UNLESS(node)->nd_body, RNODE_UNLESS(node)->nd_else);
535 case NODE_CASE:
536 return rb_ary_new_from_node_args(ast_value, 2, RNODE_CASE(node)->nd_head, RNODE_CASE(node)->nd_body);
537 case NODE_CASE2:
538 return rb_ary_new_from_node_args(ast_value, 2, RNODE_CASE2(node)->nd_head, RNODE_CASE2(node)->nd_body);
539 case NODE_CASE3:
540 return rb_ary_new_from_node_args(ast_value, 2, RNODE_CASE3(node)->nd_head, RNODE_CASE3(node)->nd_body);
541 case NODE_WHEN:
542 return rb_ary_new_from_node_args(ast_value, 3, RNODE_WHEN(node)->nd_head, RNODE_WHEN(node)->nd_body, RNODE_WHEN(node)->nd_next);
543 case NODE_IN:
544 return rb_ary_new_from_node_args(ast_value, 3, RNODE_IN(node)->nd_head, RNODE_IN(node)->nd_body, RNODE_IN(node)->nd_next);
545 case NODE_WHILE:
546 case NODE_UNTIL:
547 return rb_ary_push(rb_ary_new_from_node_args(ast_value, 2, RNODE_WHILE(node)->nd_cond, RNODE_WHILE(node)->nd_body),
548 RBOOL(RNODE_WHILE(node)->nd_state));
549 case NODE_ITER:
550 case NODE_FOR:
551 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ITER(node)->nd_iter, RNODE_ITER(node)->nd_body);
552 case NODE_FOR_MASGN:
553 return rb_ary_new_from_node_args(ast_value, 1, RNODE_FOR_MASGN(node)->nd_var);
554 case NODE_BREAK:
555 return rb_ary_new_from_node_args(ast_value, 1, RNODE_BREAK(node)->nd_stts);
556 case NODE_NEXT:
557 return rb_ary_new_from_node_args(ast_value, 1, RNODE_NEXT(node)->nd_stts);
558 case NODE_RETURN:
559 return rb_ary_new_from_node_args(ast_value, 1, RNODE_RETURN(node)->nd_stts);
560 case NODE_REDO:
561 return rb_ary_new_from_node_args(ast_value, 0);
562 case NODE_RETRY:
563 return rb_ary_new_from_node_args(ast_value, 0);
564 case NODE_BEGIN:
565 return rb_ary_new_from_node_args(ast_value, 1, RNODE_BEGIN(node)->nd_body);
566 case NODE_RESCUE:
567 return rb_ary_new_from_node_args(ast_value, 3, RNODE_RESCUE(node)->nd_head, RNODE_RESCUE(node)->nd_resq, RNODE_RESCUE(node)->nd_else);
568 case NODE_RESBODY:
569 return rb_ary_new_from_node_args(ast_value, 4, RNODE_RESBODY(node)->nd_args, RNODE_RESBODY(node)->nd_exc_var, RNODE_RESBODY(node)->nd_body, RNODE_RESBODY(node)->nd_next);
570 case NODE_ENSURE:
571 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ENSURE(node)->nd_head, RNODE_ENSURE(node)->nd_ensr);
572 case NODE_AND:
573 case NODE_OR:
574 {
575 VALUE ary = rb_ary_new();
576
577 while (1) {
578 rb_ary_push(ary, NEW_CHILD(ast_value, RNODE_AND(node)->nd_1st));
579 if (!RNODE_AND(node)->nd_2nd || !nd_type_p(RNODE_AND(node)->nd_2nd, type))
580 break;
581 node = RNODE_AND(node)->nd_2nd;
582 }
583 rb_ary_push(ary, NEW_CHILD(ast_value, RNODE_AND(node)->nd_2nd));
584 return ary;
585 }
586 case NODE_MASGN:
587 if (NODE_NAMED_REST_P(RNODE_MASGN(node)->nd_args)) {
588 return rb_ary_new_from_node_args(ast_value, 3, RNODE_MASGN(node)->nd_value, RNODE_MASGN(node)->nd_head, RNODE_MASGN(node)->nd_args);
589 }
590 else {
591 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_MASGN(node)->nd_value),
592 NEW_CHILD(ast_value, RNODE_MASGN(node)->nd_head),
593 no_name_rest());
594 }
595 case NODE_LASGN:
596 if (NODE_REQUIRED_KEYWORD_P(RNODE_LASGN(node)->nd_value)) {
597 return rb_ary_new_from_args(2, var_name(RNODE_LASGN(node)->nd_vid), ID2SYM(rb_intern("NODE_SPECIAL_REQUIRED_KEYWORD")));
598 }
599 return rb_ary_new_from_args(2, var_name(RNODE_LASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_LASGN(node)->nd_value));
600 case NODE_DASGN:
601 if (NODE_REQUIRED_KEYWORD_P(RNODE_DASGN(node)->nd_value)) {
602 return rb_ary_new_from_args(2, var_name(RNODE_DASGN(node)->nd_vid), ID2SYM(rb_intern("NODE_SPECIAL_REQUIRED_KEYWORD")));
603 }
604 return rb_ary_new_from_args(2, var_name(RNODE_DASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_DASGN(node)->nd_value));
605 case NODE_IASGN:
606 return rb_ary_new_from_args(2, var_name(RNODE_IASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_IASGN(node)->nd_value));
607 case NODE_CVASGN:
608 return rb_ary_new_from_args(2, var_name(RNODE_CVASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_CVASGN(node)->nd_value));
609 case NODE_GASGN:
610 return rb_ary_new_from_args(2, var_name(RNODE_GASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_GASGN(node)->nd_value));
611 case NODE_CDECL:
612 if (RNODE_CDECL(node)->nd_vid) {
613 return rb_ary_new_from_args(2, ID2SYM(RNODE_CDECL(node)->nd_vid), NEW_CHILD(ast_value, RNODE_CDECL(node)->nd_value));
614 }
615 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_CDECL(node)->nd_else), ID2SYM(node_colon_name(RNODE_CDECL(node)->nd_else)), NEW_CHILD(ast_value, RNODE_CDECL(node)->nd_value));
616 case NODE_OP_ASGN1:
617 return rb_ary_new_from_args(4, NEW_CHILD(ast_value, RNODE_OP_ASGN1(node)->nd_recv),
618 ID2SYM(RNODE_OP_ASGN1(node)->nd_mid),
619 NEW_CHILD(ast_value, RNODE_OP_ASGN1(node)->nd_index),
620 NEW_CHILD(ast_value, RNODE_OP_ASGN1(node)->nd_rvalue));
621 case NODE_OP_ASGN2:
622 return rb_ary_new_from_args(5, NEW_CHILD(ast_value, RNODE_OP_ASGN2(node)->nd_recv),
623 RBOOL(RNODE_OP_ASGN2(node)->nd_aid),
624 ID2SYM(RNODE_OP_ASGN2(node)->nd_vid),
625 ID2SYM(RNODE_OP_ASGN2(node)->nd_mid),
626 NEW_CHILD(ast_value, RNODE_OP_ASGN2(node)->nd_value));
627 case NODE_OP_ASGN_AND:
628 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_OP_ASGN_AND(node)->nd_head), ID2SYM(idANDOP),
629 NEW_CHILD(ast_value, RNODE_OP_ASGN_AND(node)->nd_value));
630 case NODE_OP_ASGN_OR:
631 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_OP_ASGN_OR(node)->nd_head), ID2SYM(idOROP),
632 NEW_CHILD(ast_value, RNODE_OP_ASGN_OR(node)->nd_value));
633 case NODE_OP_CDECL:
634 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_OP_CDECL(node)->nd_head),
635 ID2SYM(RNODE_OP_CDECL(node)->nd_aid),
636 NEW_CHILD(ast_value, RNODE_OP_CDECL(node)->nd_value));
637 case NODE_CALL:
638 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_CALL(node)->nd_recv),
639 ID2SYM(RNODE_CALL(node)->nd_mid),
640 NEW_CHILD(ast_value, RNODE_CALL(node)->nd_args));
641 case NODE_OPCALL:
642 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_OPCALL(node)->nd_recv),
643 ID2SYM(RNODE_OPCALL(node)->nd_mid),
644 NEW_CHILD(ast_value, RNODE_OPCALL(node)->nd_args));
645 case NODE_QCALL:
646 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_QCALL(node)->nd_recv),
647 ID2SYM(RNODE_QCALL(node)->nd_mid),
648 NEW_CHILD(ast_value, RNODE_QCALL(node)->nd_args));
649 case NODE_FCALL:
650 return rb_ary_new_from_args(2, ID2SYM(RNODE_FCALL(node)->nd_mid),
651 NEW_CHILD(ast_value, RNODE_FCALL(node)->nd_args));
652 case NODE_VCALL:
653 return rb_ary_new_from_args(1, ID2SYM(RNODE_VCALL(node)->nd_mid));
654 case NODE_SUPER:
655 return rb_ary_new_from_node_args(ast_value, 1, RNODE_SUPER(node)->nd_args);
656 case NODE_ZSUPER:
657 return rb_ary_new_from_node_args(ast_value, 0);
658 case NODE_LIST:
659 return dump_array(ast_value, RNODE_LIST(node));
660 case NODE_ZLIST:
661 return rb_ary_new_from_node_args(ast_value, 0);
662 case NODE_HASH:
663 return rb_ary_new_from_node_args(ast_value, 1, RNODE_HASH(node)->nd_head);
664 case NODE_YIELD:
665 return rb_ary_new_from_node_args(ast_value, 1, RNODE_YIELD(node)->nd_head);
666 case NODE_LVAR:
667 return rb_ary_new_from_args(1, var_name(RNODE_LVAR(node)->nd_vid));
668 case NODE_DVAR:
669 return rb_ary_new_from_args(1, var_name(RNODE_DVAR(node)->nd_vid));
670 case NODE_IVAR:
671 return rb_ary_new_from_args(1, ID2SYM(RNODE_IVAR(node)->nd_vid));
672 case NODE_CONST:
673 return rb_ary_new_from_args(1, ID2SYM(RNODE_CONST(node)->nd_vid));
674 case NODE_CVAR:
675 return rb_ary_new_from_args(1, ID2SYM(RNODE_CVAR(node)->nd_vid));
676 case NODE_GVAR:
677 return rb_ary_new_from_args(1, ID2SYM(RNODE_GVAR(node)->nd_vid));
678 case NODE_NTH_REF:
679 snprintf(name, sizeof(name), "$%ld", RNODE_NTH_REF(node)->nd_nth);
680 return rb_ary_new_from_args(1, ID2SYM(rb_intern(name)));
681 case NODE_BACK_REF:
682 name[0] = '$';
683 name[1] = (char)RNODE_BACK_REF(node)->nd_nth;
684 name[2] = '\0';
685 return rb_ary_new_from_args(1, ID2SYM(rb_intern(name)));
686 case NODE_MATCH:
687 return rb_ary_new_from_args(1, rb_node_regx_string_val(node));
688 case NODE_MATCH2:
689 if (RNODE_MATCH2(node)->nd_args) {
690 return rb_ary_new_from_node_args(ast_value, 3, RNODE_MATCH2(node)->nd_recv, RNODE_MATCH2(node)->nd_value, RNODE_MATCH2(node)->nd_args);
691 }
692 return rb_ary_new_from_node_args(ast_value, 2, RNODE_MATCH2(node)->nd_recv, RNODE_MATCH2(node)->nd_value);
693 case NODE_MATCH3:
694 return rb_ary_new_from_node_args(ast_value, 2, RNODE_MATCH3(node)->nd_recv, RNODE_MATCH3(node)->nd_value);
695 case NODE_STR:
696 case NODE_XSTR:
697 return rb_ary_new_from_args(1, rb_node_str_string_val(node));
698 case NODE_INTEGER:
699 return rb_ary_new_from_args(1, rb_node_integer_literal_val(node));
700 case NODE_FLOAT:
701 return rb_ary_new_from_args(1, rb_node_float_literal_val(node));
702 case NODE_RATIONAL:
703 return rb_ary_new_from_args(1, rb_node_rational_literal_val(node));
704 case NODE_IMAGINARY:
705 return rb_ary_new_from_args(1, rb_node_imaginary_literal_val(node));
706 case NODE_REGX:
707 return rb_ary_new_from_args(1, rb_node_regx_string_val(node));
708 case NODE_ONCE:
709 return rb_ary_new_from_node_args(ast_value, 1, RNODE_ONCE(node)->nd_body);
710 case NODE_DSTR:
711 case NODE_DXSTR:
712 case NODE_DREGX:
713 case NODE_DSYM:
714 {
715 struct RNode_LIST *n = RNODE_DSTR(node)->nd_next;
716 VALUE head = Qnil, next = Qnil;
717 if (n) {
718 head = NEW_CHILD(ast_value, n->nd_head);
719 next = NEW_CHILD(ast_value, n->nd_next);
720 }
721 return rb_ary_new_from_args(3, rb_node_dstr_string_val(node), head, next);
722 }
723 case NODE_SYM:
724 return rb_ary_new_from_args(1, rb_node_sym_string_val(node));
725 case NODE_EVSTR:
726 return rb_ary_new_from_node_args(ast_value, 1, RNODE_EVSTR(node)->nd_body);
727 case NODE_ARGSCAT:
728 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ARGSCAT(node)->nd_head, RNODE_ARGSCAT(node)->nd_body);
729 case NODE_ARGSPUSH:
730 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ARGSPUSH(node)->nd_head, RNODE_ARGSPUSH(node)->nd_body);
731 case NODE_SPLAT:
732 return rb_ary_new_from_node_args(ast_value, 1, RNODE_SPLAT(node)->nd_head);
733 case NODE_BLOCK_PASS:
734 return rb_ary_new_from_node_args(ast_value, 2, RNODE_BLOCK_PASS(node)->nd_head, RNODE_BLOCK_PASS(node)->nd_body);
735 case NODE_DEFN:
736 return rb_ary_new_from_args(2, ID2SYM(RNODE_DEFN(node)->nd_mid), NEW_CHILD(ast_value, RNODE_DEFN(node)->nd_defn));
737 case NODE_DEFS:
738 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_DEFS(node)->nd_recv), ID2SYM(RNODE_DEFS(node)->nd_mid), NEW_CHILD(ast_value, RNODE_DEFS(node)->nd_defn));
739 case NODE_ALIAS:
740 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ALIAS(node)->nd_1st, RNODE_ALIAS(node)->nd_2nd);
741 case NODE_VALIAS:
742 return rb_ary_new_from_args(2, ID2SYM(RNODE_VALIAS(node)->nd_alias), ID2SYM(RNODE_VALIAS(node)->nd_orig));
743 case NODE_UNDEF:
744 return rb_ary_new_from_args(1, dump_parser_array(ast_value, RNODE_UNDEF(node)->nd_undefs));
745 case NODE_CLASS:
746 return rb_ary_new_from_node_args(ast_value, 3, RNODE_CLASS(node)->nd_cpath, RNODE_CLASS(node)->nd_super, RNODE_CLASS(node)->nd_body);
747 case NODE_MODULE:
748 return rb_ary_new_from_node_args(ast_value, 2, RNODE_MODULE(node)->nd_cpath, RNODE_MODULE(node)->nd_body);
749 case NODE_SCLASS:
750 return rb_ary_new_from_node_args(ast_value, 2, RNODE_SCLASS(node)->nd_recv, RNODE_SCLASS(node)->nd_body);
751 case NODE_COLON2:
752 return rb_ary_new_from_args(2, NEW_CHILD(ast_value, RNODE_COLON2(node)->nd_head), ID2SYM(RNODE_COLON2(node)->nd_mid));
753 case NODE_COLON3:
754 return rb_ary_new_from_args(1, ID2SYM(RNODE_COLON3(node)->nd_mid));
755 case NODE_DOT2:
756 case NODE_DOT3:
757 case NODE_FLIP2:
758 case NODE_FLIP3:
759 return rb_ary_new_from_node_args(ast_value, 2, RNODE_DOT2(node)->nd_beg, RNODE_DOT2(node)->nd_end);
760 case NODE_SELF:
761 return rb_ary_new_from_node_args(ast_value, 0);
762 case NODE_NIL:
763 return rb_ary_new_from_node_args(ast_value, 0);
764 case NODE_TRUE:
765 return rb_ary_new_from_node_args(ast_value, 0);
766 case NODE_FALSE:
767 return rb_ary_new_from_node_args(ast_value, 0);
768 case NODE_ERRINFO:
769 return rb_ary_new_from_node_args(ast_value, 0);
770 case NODE_DEFINED:
771 return rb_ary_new_from_node_args(ast_value, 1, RNODE_DEFINED(node)->nd_head);
772 case NODE_POSTEXE:
773 return rb_ary_new_from_node_args(ast_value, 1, RNODE_POSTEXE(node)->nd_body);
774 case NODE_ATTRASGN:
775 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_ATTRASGN(node)->nd_recv), ID2SYM(RNODE_ATTRASGN(node)->nd_mid), NEW_CHILD(ast_value, RNODE_ATTRASGN(node)->nd_args));
776 case NODE_LAMBDA:
777 return rb_ary_new_from_node_args(ast_value, 1, RNODE_LAMBDA(node)->nd_body);
778 case NODE_OPT_ARG:
779 return rb_ary_new_from_node_args(ast_value, 2, RNODE_OPT_ARG(node)->nd_body, RNODE_OPT_ARG(node)->nd_next);
780 case NODE_KW_ARG:
781 return rb_ary_new_from_node_args(ast_value, 2, RNODE_KW_ARG(node)->nd_body, RNODE_KW_ARG(node)->nd_next);
782 case NODE_POSTARG:
783 if (NODE_NAMED_REST_P(RNODE_POSTARG(node)->nd_1st)) {
784 return rb_ary_new_from_node_args(ast_value, 2, RNODE_POSTARG(node)->nd_1st, RNODE_POSTARG(node)->nd_2nd);
785 }
786 return rb_ary_new_from_args(2, no_name_rest(),
787 NEW_CHILD(ast_value, RNODE_POSTARG(node)->nd_2nd));
788 case NODE_ARGS:
789 {
790 struct rb_args_info *ainfo = &RNODE_ARGS(node)->nd_ainfo;
791 return rb_ary_new_from_args(10,
792 INT2NUM(ainfo->pre_args_num),
793 NEW_CHILD(ast_value, ainfo->pre_init),
794 NEW_CHILD(ast_value, (NODE *)ainfo->opt_args),
795 var_name(ainfo->first_post_arg),
796 INT2NUM(ainfo->post_args_num),
797 NEW_CHILD(ast_value, ainfo->post_init),
798 (ainfo->rest_arg == NODE_SPECIAL_EXCESSIVE_COMMA
799 ? ID2SYM(rb_intern("NODE_SPECIAL_EXCESSIVE_COMMA"))
800 : var_name(ainfo->rest_arg)),
801 (ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast_value, (NODE *)ainfo->kw_args)),
802 (ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast_value, ainfo->kw_rest_arg)),
803 (ainfo->no_blockarg ? Qfalse : var_name(ainfo->block_arg)));
804 }
805 case NODE_SCOPE:
806 {
807 rb_ast_id_table_t *tbl = RNODE_SCOPE(node)->nd_tbl;
808 int i, size = tbl ? tbl->size : 0;
809 VALUE locals = rb_ary_new_capa(size);
810 for (i = 0; i < size; i++) {
811 rb_ary_push(locals, var_name(tbl->ids[i]));
812 }
813 return rb_ary_new_from_args(3, locals, NEW_CHILD(ast_value, (NODE *)RNODE_SCOPE(node)->nd_args), NEW_CHILD(ast_value, RNODE_SCOPE(node)->nd_body));
814 }
815 case NODE_ARYPTN:
816 {
817 VALUE rest = rest_arg(ast_value, RNODE_ARYPTN(node)->rest_arg);
818 return rb_ary_new_from_args(4,
819 NEW_CHILD(ast_value, RNODE_ARYPTN(node)->nd_pconst),
820 NEW_CHILD(ast_value, RNODE_ARYPTN(node)->pre_args),
821 rest,
822 NEW_CHILD(ast_value, RNODE_ARYPTN(node)->post_args));
823 }
824 case NODE_FNDPTN:
825 {
826 VALUE pre_rest = rest_arg(ast_value, RNODE_FNDPTN(node)->pre_rest_arg);
827 VALUE post_rest = rest_arg(ast_value, RNODE_FNDPTN(node)->post_rest_arg);
828 return rb_ary_new_from_args(4,
829 NEW_CHILD(ast_value, RNODE_FNDPTN(node)->nd_pconst),
830 pre_rest,
831 NEW_CHILD(ast_value, RNODE_FNDPTN(node)->args),
832 post_rest);
833 }
834 case NODE_HSHPTN:
835 {
836 VALUE kwrest = RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD ? ID2SYM(rb_intern("NODE_SPECIAL_NO_REST_KEYWORD")) :
837 NEW_CHILD(ast_value, RNODE_HSHPTN(node)->nd_pkwrestarg);
838
839 return rb_ary_new_from_args(3,
840 NEW_CHILD(ast_value, RNODE_HSHPTN(node)->nd_pconst),
841 NEW_CHILD(ast_value, RNODE_HSHPTN(node)->nd_pkwargs),
842 kwrest);
843 }
844 case NODE_LINE:
845 return rb_ary_new_from_args(1, rb_node_line_lineno_val(node));
846 case NODE_FILE:
847 return rb_ary_new_from_args(1, rb_node_file_path_val(node));
848 case NODE_ENCODING:
849 return rb_ary_new_from_args(1, rb_node_encoding_val(node));
850 case NODE_ERROR:
851 return rb_ary_new_from_node_args(ast_value, 0);
852 case NODE_ARGS_AUX:
853 case NODE_LAST:
854 break;
855 }
856
857 rb_bug("node_children: unknown node: %s", ruby_node_name(type));
858}
859
860static VALUE
861ast_node_children(rb_execution_context_t *ec, VALUE self)
862{
863 struct ASTNodeData *data;
864 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
865
866 return node_children(data->ast_value, data->node);
867}
868
869static int
870null_loc_p(rb_code_location_t *loc)
871{
872 return (loc->beg_pos.lineno == 0 && loc->beg_pos.column == -1 && loc->end_pos.lineno == 0 && loc->end_pos.column == -1);
873}
874
875static VALUE
876location_new(rb_code_location_t *loc)
877{
878 VALUE obj;
879 struct ASTLocationData *data;
880
881 if (null_loc_p(loc)) return Qnil;
882
883 obj = TypedData_Make_Struct(rb_cLocation, struct ASTLocationData, &rb_location_type, data);
884 data->first_lineno = loc->beg_pos.lineno;
885 data->first_column = loc->beg_pos.column;
886 data->last_lineno = loc->end_pos.lineno;
887 data->last_column = loc->end_pos.column;
888
889 return obj;
890}
891
892static VALUE
893node_locations(VALUE ast_value, const NODE *node)
894{
895 enum node_type type = nd_type(node);
896 switch (type) {
897 case NODE_ALIAS:
898 return rb_ary_new_from_args(2,
899 location_new(nd_code_loc(node)),
900 location_new(&RNODE_ALIAS(node)->keyword_loc));
901 case NODE_AND:
902 return rb_ary_new_from_args(2,
903 location_new(nd_code_loc(node)),
904 location_new(&RNODE_AND(node)->operator_loc));
905 case NODE_BLOCK_PASS:
906 return rb_ary_new_from_args(2,
907 location_new(nd_code_loc(node)),
908 location_new(&RNODE_BLOCK_PASS(node)->operator_loc));
909 case NODE_BREAK:
910 return rb_ary_new_from_args(2,
911 location_new(nd_code_loc(node)),
912 location_new(&RNODE_BREAK(node)->keyword_loc));
913 case NODE_CASE:
914 return rb_ary_new_from_args(3,
915 location_new(nd_code_loc(node)),
916 location_new(&RNODE_CASE(node)->case_keyword_loc),
917 location_new(&RNODE_CASE(node)->end_keyword_loc));
918 case NODE_CASE2:
919 return rb_ary_new_from_args(3,
920 location_new(nd_code_loc(node)),
921 location_new(&RNODE_CASE2(node)->case_keyword_loc),
922 location_new(&RNODE_CASE2(node)->end_keyword_loc));
923 case NODE_CASE3:
924 return rb_ary_new_from_args(3,
925 location_new(nd_code_loc(node)),
926 location_new(&RNODE_CASE3(node)->case_keyword_loc),
927 location_new(&RNODE_CASE3(node)->end_keyword_loc));
928 case NODE_CLASS:
929 return rb_ary_new_from_args(4,
930 location_new(nd_code_loc(node)),
931 location_new(&RNODE_CLASS(node)->class_keyword_loc),
932 location_new(&RNODE_CLASS(node)->inheritance_operator_loc),
933 location_new(&RNODE_CLASS(node)->end_keyword_loc));
934 case NODE_COLON2:
935 return rb_ary_new_from_args(3,
936 location_new(nd_code_loc(node)),
937 location_new(&RNODE_COLON2(node)->delimiter_loc),
938 location_new(&RNODE_COLON2(node)->name_loc));
939 case NODE_COLON3:
940 return rb_ary_new_from_args(3,
941 location_new(nd_code_loc(node)),
942 location_new(&RNODE_COLON3(node)->delimiter_loc),
943 location_new(&RNODE_COLON3(node)->name_loc));
944 case NODE_DEFINED:
945 return rb_ary_new_from_args(2,
946 location_new(nd_code_loc(node)),
947 location_new(&RNODE_DEFINED(node)->keyword_loc));
948 case NODE_DOT2:
949 return rb_ary_new_from_args(2,
950 location_new(nd_code_loc(node)),
951 location_new(&RNODE_DOT2(node)->operator_loc));
952 case NODE_DOT3:
953 return rb_ary_new_from_args(2,
954 location_new(nd_code_loc(node)),
955 location_new(&RNODE_DOT3(node)->operator_loc));
956 case NODE_EVSTR:
957 return rb_ary_new_from_args(3,
958 location_new(nd_code_loc(node)),
959 location_new(&RNODE_EVSTR(node)->opening_loc),
960 location_new(&RNODE_EVSTR(node)->closing_loc));
961 case NODE_FLIP2:
962 return rb_ary_new_from_args(2,
963 location_new(nd_code_loc(node)),
964 location_new(&RNODE_FLIP2(node)->operator_loc));
965 case NODE_FLIP3:
966 return rb_ary_new_from_args(2,
967 location_new(nd_code_loc(node)),
968 location_new(&RNODE_FLIP3(node)->operator_loc));
969 case NODE_FOR:
970 return rb_ary_new_from_args(5,
971 location_new(nd_code_loc(node)),
972 location_new(&RNODE_FOR(node)->for_keyword_loc),
973 location_new(&RNODE_FOR(node)->in_keyword_loc),
974 location_new(&RNODE_FOR(node)->do_keyword_loc),
975 location_new(&RNODE_FOR(node)->end_keyword_loc));
976 case NODE_LAMBDA:
977 return rb_ary_new_from_args(4,
978 location_new(nd_code_loc(node)),
979 location_new(&RNODE_LAMBDA(node)->operator_loc),
980 location_new(&RNODE_LAMBDA(node)->opening_loc),
981 location_new(&RNODE_LAMBDA(node)->closing_loc));
982 case NODE_IF:
983 return rb_ary_new_from_args(4,
984 location_new(nd_code_loc(node)),
985 location_new(&RNODE_IF(node)->if_keyword_loc),
986 location_new(&RNODE_IF(node)->then_keyword_loc),
987 location_new(&RNODE_IF(node)->end_keyword_loc));
988 case NODE_IN:
989 return rb_ary_new_from_args(4,
990 location_new(nd_code_loc(node)),
991 location_new(&RNODE_IN(node)->in_keyword_loc),
992 location_new(&RNODE_IN(node)->then_keyword_loc),
993 location_new(&RNODE_IN(node)->operator_loc));
994 case NODE_MODULE:
995 return rb_ary_new_from_args(3,
996 location_new(nd_code_loc(node)),
997 location_new(&RNODE_MODULE(node)->module_keyword_loc),
998 location_new(&RNODE_MODULE(node)->end_keyword_loc));
999 case NODE_NEXT:
1000 return rb_ary_new_from_args(2,
1001 location_new(nd_code_loc(node)),
1002 location_new(&RNODE_NEXT(node)->keyword_loc));
1003 case NODE_OR:
1004 return rb_ary_new_from_args(2,
1005 location_new(nd_code_loc(node)),
1006 location_new(&RNODE_OR(node)->operator_loc));
1007 case NODE_OP_ASGN1:
1008 return rb_ary_new_from_args(5,
1009 location_new(nd_code_loc(node)),
1010 location_new(&RNODE_OP_ASGN1(node)->call_operator_loc),
1011 location_new(&RNODE_OP_ASGN1(node)->opening_loc),
1012 location_new(&RNODE_OP_ASGN1(node)->closing_loc),
1013 location_new(&RNODE_OP_ASGN1(node)->binary_operator_loc));
1014 case NODE_OP_ASGN2:
1015 return rb_ary_new_from_args(4,
1016 location_new(nd_code_loc(node)),
1017 location_new(&RNODE_OP_ASGN2(node)->call_operator_loc),
1018 location_new(&RNODE_OP_ASGN2(node)->message_loc),
1019 location_new(&RNODE_OP_ASGN2(node)->binary_operator_loc));
1020 case NODE_POSTEXE:
1021 return rb_ary_new_from_args(4,
1022 location_new(nd_code_loc(node)),
1023 location_new(&RNODE_POSTEXE(node)->keyword_loc),
1024 location_new(&RNODE_POSTEXE(node)->opening_loc),
1025 location_new(&RNODE_POSTEXE(node)->closing_loc));
1026 case NODE_REDO:
1027 return rb_ary_new_from_args(2,
1028 location_new(nd_code_loc(node)),
1029 location_new(&RNODE_REDO(node)->keyword_loc));
1030 case NODE_REGX:
1031 return rb_ary_new_from_args(4,
1032 location_new(nd_code_loc(node)),
1033 location_new(&RNODE_REGX(node)->opening_loc),
1034 location_new(&RNODE_REGX(node)->content_loc),
1035 location_new(&RNODE_REGX(node)->closing_loc));
1036 case NODE_RETURN:
1037 return rb_ary_new_from_args(2,
1038 location_new(nd_code_loc(node)),
1039 location_new(&RNODE_RETURN(node)->keyword_loc));
1040
1041 case NODE_SCLASS:
1042 return rb_ary_new_from_args(4,
1043 location_new(nd_code_loc(node)),
1044 location_new(&RNODE_SCLASS(node)->class_keyword_loc),
1045 location_new(&RNODE_SCLASS(node)->operator_loc),
1046 location_new(&RNODE_SCLASS(node)->end_keyword_loc));
1047
1048 case NODE_SPLAT:
1049 return rb_ary_new_from_args(2,
1050 location_new(nd_code_loc(node)),
1051 location_new(&RNODE_SPLAT(node)->operator_loc));
1052 case NODE_SUPER:
1053 return rb_ary_new_from_args(4,
1054 location_new(nd_code_loc(node)),
1055 location_new(&RNODE_SUPER(node)->keyword_loc),
1056 location_new(&RNODE_SUPER(node)->lparen_loc),
1057 location_new(&RNODE_SUPER(node)->rparen_loc));
1058 case NODE_UNDEF:
1059 return rb_ary_new_from_args(2,
1060 location_new(nd_code_loc(node)),
1061 location_new(&RNODE_UNDEF(node)->keyword_loc));
1062 case NODE_UNLESS:
1063 return rb_ary_new_from_args(4,
1064 location_new(nd_code_loc(node)),
1065 location_new(&RNODE_UNLESS(node)->keyword_loc),
1066 location_new(&RNODE_UNLESS(node)->then_keyword_loc),
1067 location_new(&RNODE_UNLESS(node)->end_keyword_loc));
1068 case NODE_VALIAS:
1069 return rb_ary_new_from_args(2,
1070 location_new(nd_code_loc(node)),
1071 location_new(&RNODE_VALIAS(node)->keyword_loc));
1072 case NODE_WHEN:
1073 return rb_ary_new_from_args(3,
1074 location_new(nd_code_loc(node)),
1075 location_new(&RNODE_WHEN(node)->keyword_loc),
1076 location_new(&RNODE_WHEN(node)->then_keyword_loc));
1077 case NODE_WHILE:
1078 return rb_ary_new_from_args(3,
1079 location_new(nd_code_loc(node)),
1080 location_new(&RNODE_WHILE(node)->keyword_loc),
1081 location_new(&RNODE_WHILE(node)->closing_loc));
1082 case NODE_UNTIL:
1083 return rb_ary_new_from_args(3,
1084 location_new(nd_code_loc(node)),
1085 location_new(&RNODE_UNTIL(node)->keyword_loc),
1086 location_new(&RNODE_UNTIL(node)->closing_loc));
1087 case NODE_YIELD:
1088 return rb_ary_new_from_args(4,
1089 location_new(nd_code_loc(node)),
1090 location_new(&RNODE_YIELD(node)->keyword_loc),
1091 location_new(&RNODE_YIELD(node)->lparen_loc),
1092 location_new(&RNODE_YIELD(node)->rparen_loc));
1093 case NODE_ARGS_AUX:
1094 case NODE_LAST:
1095 break;
1096 default:
1097 return rb_ary_new_from_args(1, location_new(nd_code_loc(node)));
1098 }
1099
1100 rb_bug("node_locations: unknown node: %s", ruby_node_name(type));
1101}
1102
1103static VALUE
1104ast_node_locations(rb_execution_context_t *ec, VALUE self)
1105{
1106 struct ASTNodeData *data;
1107 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1108
1109 return node_locations(data->ast_value, data->node);
1110}
1111
1112static VALUE
1113ast_node_first_lineno(rb_execution_context_t *ec, VALUE self)
1114{
1115 struct ASTNodeData *data;
1116 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1117
1118 return INT2NUM(nd_first_lineno(data->node));
1119}
1120
1121static VALUE
1122ast_node_first_column(rb_execution_context_t *ec, VALUE self)
1123{
1124 struct ASTNodeData *data;
1125 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1126
1127 return INT2NUM(nd_first_column(data->node));
1128}
1129
1130static VALUE
1131ast_node_last_lineno(rb_execution_context_t *ec, VALUE self)
1132{
1133 struct ASTNodeData *data;
1134 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1135
1136 return INT2NUM(nd_last_lineno(data->node));
1137}
1138
1139static VALUE
1140ast_node_last_column(rb_execution_context_t *ec, VALUE self)
1141{
1142 struct ASTNodeData *data;
1143 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1144
1145 return INT2NUM(nd_last_column(data->node));
1146}
1147
1148static VALUE
1149ast_node_all_tokens(rb_execution_context_t *ec, VALUE self)
1150{
1151 long i;
1152 struct ASTNodeData *data;
1153 rb_ast_t *ast;
1154 rb_parser_ary_t *parser_tokens;
1155 rb_parser_ast_token_t *parser_token;
1156 VALUE str, loc, token, all_tokens;
1157
1158 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1159 ast = rb_ruby_ast_data_get(data->ast_value);
1160
1161 parser_tokens = ast->node_buffer->tokens;
1162 if (parser_tokens == NULL) {
1163 return Qnil;
1164 }
1165
1166 all_tokens = rb_ary_new2(parser_tokens->len);
1167 for (i = 0; i < parser_tokens->len; i++) {
1168 parser_token = parser_tokens->data[i];
1169 str = rb_str_new(parser_token->str->ptr, parser_token->str->len);
1170 loc = rb_ary_new_from_args(4,
1171 INT2FIX(parser_token->loc.beg_pos.lineno),
1172 INT2FIX(parser_token->loc.beg_pos.column),
1173 INT2FIX(parser_token->loc.end_pos.lineno),
1174 INT2FIX(parser_token->loc.end_pos.column)
1175 );
1176 token = rb_ary_new_from_args(4, INT2FIX(parser_token->id), ID2SYM(rb_intern(parser_token->type_name)), str, loc);
1177 rb_ary_push(all_tokens, token);
1178 }
1179 rb_ary_freeze(all_tokens);
1180
1181 return all_tokens;
1182}
1183
1184static VALUE
1185ast_node_inspect(rb_execution_context_t *ec, VALUE self)
1186{
1187 VALUE str;
1188 VALUE cname;
1189 struct ASTNodeData *data;
1190 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1191
1192 cname = rb_class_path(rb_obj_class(self));
1193 str = rb_str_new2("#<");
1194
1195 rb_str_append(str, cname);
1196 rb_str_catf(str, ":%s@%d:%d-%d:%d>",
1197 node_type_to_str(data->node),
1198 nd_first_lineno(data->node), nd_first_column(data->node),
1199 nd_last_lineno(data->node), nd_last_column(data->node));
1200
1201 return str;
1202}
1203
1204static VALUE
1205ast_node_script_lines(rb_execution_context_t *ec, VALUE self)
1206{
1207 struct ASTNodeData *data;
1208 rb_ast_t *ast;
1209 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1210 ast = rb_ruby_ast_data_get(data->ast_value);
1211 rb_parser_ary_t *ret = ast->body.script_lines;
1212 return rb_parser_build_script_lines_from(ret);
1213}
1214
1215static VALUE
1216ast_location_first_lineno(rb_execution_context_t *ec, VALUE self)
1217{
1218 struct ASTLocationData *data;
1219 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1220
1221 return INT2NUM(data->first_lineno);
1222}
1223
1224static VALUE
1225ast_location_first_column(rb_execution_context_t *ec, VALUE self)
1226{
1227 struct ASTLocationData *data;
1228 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1229
1230 return INT2NUM(data->first_column);
1231}
1232
1233static VALUE
1234ast_location_last_lineno(rb_execution_context_t *ec, VALUE self)
1235{
1236 struct ASTLocationData *data;
1237 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1238
1239 return INT2NUM(data->last_lineno);
1240}
1241
1242static VALUE
1243ast_location_last_column(rb_execution_context_t *ec, VALUE self)
1244{
1245 struct ASTLocationData *data;
1246 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1247
1248 return INT2NUM(data->last_column);
1249}
1250
1251static VALUE
1252ast_location_inspect(rb_execution_context_t *ec, VALUE self)
1253{
1254 VALUE str;
1255 VALUE cname;
1256 struct ASTLocationData *data;
1257 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1258
1259 cname = rb_class_path(rb_obj_class(self));
1260 str = rb_str_new2("#<");
1261
1262 rb_str_append(str, cname);
1263 rb_str_catf(str, ":@%d:%d-%d:%d>",
1264 data->first_lineno, data->first_column,
1265 data->last_lineno, data->last_column);
1266
1267 return str;
1268}
1269
1270#include "ast.rbinc"
1271
1272void
1273Init_ast(void)
1274{
1275 rb_mAST = rb_define_module_under(rb_cRubyVM, "AbstractSyntaxTree");
1276 rb_cNode = rb_define_class_under(rb_mAST, "Node", rb_cObject);
1277 rb_cLocation = rb_define_class_under(rb_mAST, "Location", rb_cObject);
1278 rb_undef_alloc_func(rb_cNode);
1279 rb_undef_alloc_func(rb_cLocation);
1280}
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1676
#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:205
#define ULL2NUM
Old name of RB_ULL2NUM.
Definition long_long.h:31
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
#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 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:657
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:678
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1463
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1461
VALUE rb_cObject
Object class.
Definition object.c:60
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:234
Encoding relates APIs.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1123
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
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:7405
VALUE rb_io_close(VALUE io)
Closes the IO.
Definition io.c:5877
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:386
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:3898
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1499
#define rb_strlen_lit(str)
Length of a string literal.
Definition string.h:1693
#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:1515
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:398
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1843
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
Definition util.h:48
VALUE type(ANYARGS)
ANYARGS-ed function type.
Functions related to nodes in the AST.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:50
#define RARRAY_AREF(a, i)
Definition rarray.h:402
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
Definition rtypeddata.h:81
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:773
#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:604
#define RTEST
This is an old name of RB_TEST.
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:242
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40