Ruby 4.1.0dev (2026-10-28 revision 1b06b950bb460d20a9dcecb8b97d363adbf4a358)
ast.c (1b06b950bb460d20a9dcecb8b97d363adbf4a358)
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 rb_len_t 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
288node_id_for_backtrace_location(rb_execution_context_t *ec, VALUE module, VALUE location)
289{
290 int node_id;
291
292 if (!rb_frame_info_p(location)) {
293 rb_raise(rb_eTypeError, "Thread::Backtrace::Location object expected");
294 }
295
296 node_id = rb_get_node_id_from_frame_info(location);
297 if (node_id == -1) {
298 return Qnil;
299 }
300
301 return INT2NUM(node_id);
302}
303
304static VALUE
305iseq_of_backtrace_location(rb_execution_context_t *ec, VALUE module, VALUE location)
306{
307 if (!rb_frame_info_p(location)) {
308 rb_raise(rb_eTypeError, "Thread::Backtrace::Location object expected");
309 }
310
311 const rb_iseq_t *iseq = rb_get_iseq_from_frame_info(location);
312 if (!iseq) return Qnil;
313 return rb_iseqw_new(iseq);
314}
315
316static VALUE
317ast_s_of(rb_execution_context_t *ec, VALUE module, VALUE body, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
318{
319 VALUE node, lines = Qnil;
320 const rb_iseq_t *iseq;
321 int node_id;
322
323 if (rb_frame_info_p(body)) {
324 iseq = rb_get_iseq_from_frame_info(body);
325 node_id = rb_get_node_id_from_frame_info(body);
326 }
327 else {
328 iseq = NULL;
329
330 if (rb_obj_is_proc(body)) {
331 iseq = vm_proc_iseq(body);
332
333 if (!rb_obj_is_iseq((VALUE)iseq)) return Qnil;
334 }
335 else {
336 iseq = rb_method_iseq(body);
337 }
338 if (iseq) {
339 node_id = ISEQ_BODY(iseq)->location.node_id;
340 }
341 }
342
343 if (!iseq) {
344 return Qnil;
345 }
346
347 if (ISEQ_BODY(iseq)->prism) {
348 rb_raise(rb_eRuntimeError, "cannot get AST for ISEQ compiled by prism");
349 }
350
351 lines = ISEQ_SCRIPT_LINES(iseq);
352
353 VALUE path = rb_iseq_path(iseq);
354 int e_option = RSTRING_LEN(path) == 2 && memcmp(RSTRING_PTR(path), "-e", 2) == 0;
355
356 if (NIL_P(lines) && rb_iseq_from_eval_p(iseq) && !e_option) {
357 rb_raise(rb_eArgError, "cannot get AST for method defined in eval");
358 }
359
360 if (!NIL_P(lines)) {
361 node = rb_ast_parse_array(lines, keep_script_lines, error_tolerant, keep_tokens);
362 }
363 else if (e_option) {
364 node = rb_ast_parse_str(rb_e_script, keep_script_lines, error_tolerant, keep_tokens);
365 }
366 else {
367 node = rb_ast_parse_file(path, keep_script_lines, error_tolerant, keep_tokens);
368 }
369
370 return node_find(node, node_id);
371}
372
373static VALUE
374rb_ast_node_alloc(VALUE klass)
375{
376 struct ASTNodeData *data;
377 VALUE obj = TypedData_Make_Struct(klass, struct ASTNodeData, &rb_node_type, data);
378
379 return obj;
380}
381
382static const char*
383node_type_to_str(const NODE *node)
384{
385 return (ruby_node_name(nd_type(node)) + rb_strlen_lit("NODE_"));
386}
387
388static VALUE
389ast_node_type(rb_execution_context_t *ec, VALUE self)
390{
391 struct ASTNodeData *data;
392 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
393
394 return rb_sym_intern_ascii_cstr(node_type_to_str(data->node));
395}
396
397static VALUE
398ast_node_node_id(rb_execution_context_t *ec, VALUE self)
399{
400 struct ASTNodeData *data;
401 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
402
403 return INT2FIX(nd_node_id(data->node));
404}
405
406#define NEW_CHILD(ast_value, node) (node ? ast_new_internal(ast_value, node) : Qnil)
407
408static VALUE
409rb_ary_new_from_node_args(VALUE ast_value, long n, ...)
410{
411 va_list ar;
412 VALUE ary;
413 long i;
414
415 ary = rb_ary_new2(n);
416
417 va_start(ar, n);
418 for (i=0; i<n; i++) {
419 NODE *node;
420 node = va_arg(ar, NODE *);
421 rb_ary_push(ary, NEW_CHILD(ast_value, node));
422 }
423 va_end(ar);
424 return ary;
425}
426
427static VALUE
428dump_block(VALUE ast_value, const struct RNode_BLOCK *node)
429{
430 VALUE ary = rb_ary_new();
431 do {
432 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_head));
433 } while (node->nd_next &&
434 nd_type_p(node->nd_next, NODE_BLOCK) &&
435 (node = RNODE_BLOCK(node->nd_next), 1));
436 if (node->nd_next) {
437 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_next));
438 }
439
440 return ary;
441}
442
443static VALUE
444dump_array(VALUE ast_value, const struct RNode_LIST *node)
445{
446 VALUE ary = rb_ary_new();
447 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_head));
448
449 while (node->nd_next && nd_type_p(node->nd_next, NODE_LIST)) {
450 node = RNODE_LIST(node->nd_next);
451 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_head));
452 }
453 rb_ary_push(ary, NEW_CHILD(ast_value, node->nd_next));
454
455 return ary;
456}
457
458static VALUE
459dump_parser_array(VALUE ast_value, rb_parser_ary_t *p_ary)
460{
461 VALUE ary;
462
463 if (p_ary->data_type != PARSER_ARY_DATA_NODE) {
464 rb_bug("unexpected rb_parser_ary_data_type: %d", p_ary->data_type);
465 }
466
467 ary = rb_ary_new();
468
469 for (long i = 0; i < p_ary->len; i++) {
470 rb_ary_push(ary, NEW_CHILD(ast_value, p_ary->data[i]));
471 }
472
473 return ary;
474}
475
476static VALUE
477var_name(ID id)
478{
479 if (!id) return Qnil;
480 if (!rb_id2str(id)) return Qnil;
481 return ID2SYM(id);
482}
483
484static VALUE
485no_name_rest(void)
486{
487 ID rest;
488 CONST_ID(rest, "NODE_SPECIAL_NO_NAME_REST");
489 return ID2SYM(rest);
490}
491
492static VALUE
493rest_arg(VALUE ast_value, const NODE *rest_arg)
494{
495 return NODE_NAMED_REST_P(rest_arg) ? NEW_CHILD(ast_value, rest_arg) : no_name_rest();
496}
497
498static ID
499node_colon_name(const NODE *node)
500{
501 switch (nd_type(node)) {
502 case NODE_COLON2:
503 return RNODE_COLON2(node)->nd_mid;
504 case NODE_COLON3:
505 return RNODE_COLON3(node)->nd_mid;
506 default:
507 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
508 }
509}
510
511static VALUE
512node_children(VALUE ast_value, const NODE *node)
513{
514 char name[sizeof("$") + DECIMAL_SIZE_OF(long)];
515
516 enum node_type type = nd_type(node);
517 switch (type) {
518 case NODE_BLOCK:
519 return dump_block(ast_value, RNODE_BLOCK(node));
520 case NODE_IF:
521 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);
522 case NODE_UNLESS:
523 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);
524 case NODE_CASE:
525 return rb_ary_new_from_node_args(ast_value, 2, RNODE_CASE(node)->nd_head, RNODE_CASE(node)->nd_body);
526 case NODE_CASE2:
527 return rb_ary_new_from_node_args(ast_value, 2, RNODE_CASE2(node)->nd_head, RNODE_CASE2(node)->nd_body);
528 case NODE_CASE3:
529 return rb_ary_new_from_node_args(ast_value, 2, RNODE_CASE3(node)->nd_head, RNODE_CASE3(node)->nd_body);
530 case NODE_WHEN:
531 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);
532 case NODE_IN:
533 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);
534 case NODE_WHILE:
535 case NODE_UNTIL:
536 return rb_ary_push(rb_ary_new_from_node_args(ast_value, 2, RNODE_WHILE(node)->nd_cond, RNODE_WHILE(node)->nd_body),
537 RBOOL(RNODE_WHILE(node)->nd_state));
538 case NODE_ITER:
539 case NODE_FOR:
540 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ITER(node)->nd_iter, RNODE_ITER(node)->nd_body);
541 case NODE_FOR_MASGN:
542 return rb_ary_new_from_node_args(ast_value, 1, RNODE_FOR_MASGN(node)->nd_var);
543 case NODE_BREAK:
544 return rb_ary_new_from_node_args(ast_value, 1, RNODE_BREAK(node)->nd_stts);
545 case NODE_NEXT:
546 return rb_ary_new_from_node_args(ast_value, 1, RNODE_NEXT(node)->nd_stts);
547 case NODE_RETURN:
548 return rb_ary_new_from_node_args(ast_value, 1, RNODE_RETURN(node)->nd_stts);
549 case NODE_REDO:
550 return rb_ary_new_from_node_args(ast_value, 0);
551 case NODE_RETRY:
552 return rb_ary_new_from_node_args(ast_value, 0);
553 case NODE_BEGIN:
554 return rb_ary_new_from_node_args(ast_value, 1, RNODE_BEGIN(node)->nd_body);
555 case NODE_RESCUE:
556 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);
557 case NODE_RESBODY:
558 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);
559 case NODE_ENSURE:
560 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ENSURE(node)->nd_head, RNODE_ENSURE(node)->nd_ensr);
561 case NODE_AND:
562 case NODE_OR:
563 {
564 VALUE ary = rb_ary_new();
565
566 while (1) {
567 rb_ary_push(ary, NEW_CHILD(ast_value, RNODE_AND(node)->nd_1st));
568 if (!RNODE_AND(node)->nd_2nd || !nd_type_p(RNODE_AND(node)->nd_2nd, type))
569 break;
570 node = RNODE_AND(node)->nd_2nd;
571 }
572 rb_ary_push(ary, NEW_CHILD(ast_value, RNODE_AND(node)->nd_2nd));
573 return ary;
574 }
575 case NODE_MASGN:
576 if (NODE_NAMED_REST_P(RNODE_MASGN(node)->nd_args)) {
577 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);
578 }
579 else {
580 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_MASGN(node)->nd_value),
581 NEW_CHILD(ast_value, RNODE_MASGN(node)->nd_head),
582 no_name_rest());
583 }
584 case NODE_LASGN:
585 if (NODE_REQUIRED_KEYWORD_P(RNODE_LASGN(node)->nd_value)) {
586 return rb_ary_new_from_args(2, var_name(RNODE_LASGN(node)->nd_vid), ID2SYM(rb_intern("NODE_SPECIAL_REQUIRED_KEYWORD")));
587 }
588 return rb_ary_new_from_args(2, var_name(RNODE_LASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_LASGN(node)->nd_value));
589 case NODE_DASGN:
590 if (NODE_REQUIRED_KEYWORD_P(RNODE_DASGN(node)->nd_value)) {
591 return rb_ary_new_from_args(2, var_name(RNODE_DASGN(node)->nd_vid), ID2SYM(rb_intern("NODE_SPECIAL_REQUIRED_KEYWORD")));
592 }
593 return rb_ary_new_from_args(2, var_name(RNODE_DASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_DASGN(node)->nd_value));
594 case NODE_IASGN:
595 return rb_ary_new_from_args(2, var_name(RNODE_IASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_IASGN(node)->nd_value));
596 case NODE_CVASGN:
597 return rb_ary_new_from_args(2, var_name(RNODE_CVASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_CVASGN(node)->nd_value));
598 case NODE_GASGN:
599 return rb_ary_new_from_args(2, var_name(RNODE_GASGN(node)->nd_vid), NEW_CHILD(ast_value, RNODE_GASGN(node)->nd_value));
600 case NODE_CDECL:
601 if (RNODE_CDECL(node)->nd_vid) {
602 return rb_ary_new_from_args(2, ID2SYM(RNODE_CDECL(node)->nd_vid), NEW_CHILD(ast_value, RNODE_CDECL(node)->nd_value));
603 }
604 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));
605 case NODE_OP_ASGN1:
606 return rb_ary_new_from_args(4, NEW_CHILD(ast_value, RNODE_OP_ASGN1(node)->nd_recv),
607 ID2SYM(RNODE_OP_ASGN1(node)->nd_mid),
608 NEW_CHILD(ast_value, RNODE_OP_ASGN1(node)->nd_index),
609 NEW_CHILD(ast_value, RNODE_OP_ASGN1(node)->nd_rvalue));
610 case NODE_OP_ASGN2:
611 return rb_ary_new_from_args(5, NEW_CHILD(ast_value, RNODE_OP_ASGN2(node)->nd_recv),
612 RBOOL(RNODE_OP_ASGN2(node)->nd_aid),
613 ID2SYM(RNODE_OP_ASGN2(node)->nd_vid),
614 ID2SYM(RNODE_OP_ASGN2(node)->nd_mid),
615 NEW_CHILD(ast_value, RNODE_OP_ASGN2(node)->nd_value));
616 case NODE_OP_ASGN_AND:
617 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_OP_ASGN_AND(node)->nd_head), ID2SYM(idANDOP),
618 NEW_CHILD(ast_value, RNODE_OP_ASGN_AND(node)->nd_value));
619 case NODE_OP_ASGN_OR:
620 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_OP_ASGN_OR(node)->nd_head), ID2SYM(idOROP),
621 NEW_CHILD(ast_value, RNODE_OP_ASGN_OR(node)->nd_value));
622 case NODE_OP_CDECL:
623 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_OP_CDECL(node)->nd_head),
624 ID2SYM(RNODE_OP_CDECL(node)->nd_aid),
625 NEW_CHILD(ast_value, RNODE_OP_CDECL(node)->nd_value));
626 case NODE_CALL:
627 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_CALL(node)->nd_recv),
628 ID2SYM(RNODE_CALL(node)->nd_mid),
629 NEW_CHILD(ast_value, RNODE_CALL(node)->nd_args));
630 case NODE_OPCALL:
631 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_OPCALL(node)->nd_recv),
632 ID2SYM(RNODE_OPCALL(node)->nd_mid),
633 NEW_CHILD(ast_value, RNODE_OPCALL(node)->nd_args));
634 case NODE_QCALL:
635 return rb_ary_new_from_args(3, NEW_CHILD(ast_value, RNODE_QCALL(node)->nd_recv),
636 ID2SYM(RNODE_QCALL(node)->nd_mid),
637 NEW_CHILD(ast_value, RNODE_QCALL(node)->nd_args));
638 case NODE_FCALL:
639 return rb_ary_new_from_args(2, ID2SYM(RNODE_FCALL(node)->nd_mid),
640 NEW_CHILD(ast_value, RNODE_FCALL(node)->nd_args));
641 case NODE_VCALL:
642 return rb_ary_new_from_args(1, ID2SYM(RNODE_VCALL(node)->nd_mid));
643 case NODE_SUPER:
644 return rb_ary_new_from_node_args(ast_value, 1, RNODE_SUPER(node)->nd_args);
645 case NODE_ZSUPER:
646 return rb_ary_new_from_node_args(ast_value, 0);
647 case NODE_LIST:
648 return dump_array(ast_value, RNODE_LIST(node));
649 case NODE_ZLIST:
650 return rb_ary_new_from_node_args(ast_value, 0);
651 case NODE_HASH:
652 return rb_ary_new_from_node_args(ast_value, 1, RNODE_HASH(node)->nd_head);
653 case NODE_YIELD:
654 return rb_ary_new_from_node_args(ast_value, 1, RNODE_YIELD(node)->nd_head);
655 case NODE_LVAR:
656 return rb_ary_new_from_args(1, var_name(RNODE_LVAR(node)->nd_vid));
657 case NODE_DVAR:
658 return rb_ary_new_from_args(1, var_name(RNODE_DVAR(node)->nd_vid));
659 case NODE_IVAR:
660 return rb_ary_new_from_args(1, ID2SYM(RNODE_IVAR(node)->nd_vid));
661 case NODE_CONST:
662 return rb_ary_new_from_args(1, ID2SYM(RNODE_CONST(node)->nd_vid));
663 case NODE_CVAR:
664 return rb_ary_new_from_args(1, ID2SYM(RNODE_CVAR(node)->nd_vid));
665 case NODE_GVAR:
666 return rb_ary_new_from_args(1, ID2SYM(RNODE_GVAR(node)->nd_vid));
667 case NODE_NTH_REF:
668 snprintf(name, sizeof(name), "$%ld", RNODE_NTH_REF(node)->nd_nth);
669 return rb_ary_new_from_args(1, ID2SYM(rb_intern(name)));
670 case NODE_BACK_REF:
671 name[0] = '$';
672 name[1] = (char)RNODE_BACK_REF(node)->nd_nth;
673 name[2] = '\0';
674 return rb_ary_new_from_args(1, ID2SYM(rb_intern(name)));
675 case NODE_MATCH:
676 return rb_ary_new_from_args(1, rb_node_regx_string_val(node));
677 case NODE_MATCH2:
678 if (RNODE_MATCH2(node)->nd_args) {
679 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);
680 }
681 return rb_ary_new_from_node_args(ast_value, 2, RNODE_MATCH2(node)->nd_recv, RNODE_MATCH2(node)->nd_value);
682 case NODE_MATCH3:
683 return rb_ary_new_from_node_args(ast_value, 2, RNODE_MATCH3(node)->nd_recv, RNODE_MATCH3(node)->nd_value);
684 case NODE_STR:
685 case NODE_XSTR:
686 return rb_ary_new_from_args(1, rb_node_str_string_val(node));
687 case NODE_INTEGER:
688 return rb_ary_new_from_args(1, rb_node_integer_literal_val(node));
689 case NODE_FLOAT:
690 return rb_ary_new_from_args(1, rb_node_float_literal_val(node));
691 case NODE_RATIONAL:
692 return rb_ary_new_from_args(1, rb_node_rational_literal_val(node));
693 case NODE_IMAGINARY:
694 return rb_ary_new_from_args(1, rb_node_imaginary_literal_val(node));
695 case NODE_REGX:
696 return rb_ary_new_from_args(1, rb_node_regx_string_val(node));
697 case NODE_ONCE:
698 return rb_ary_new_from_node_args(ast_value, 1, RNODE_ONCE(node)->nd_body);
699 case NODE_DSTR:
700 case NODE_DXSTR:
701 case NODE_DREGX:
702 case NODE_DSYM:
703 {
704 struct RNode_LIST *n = RNODE_DSTR(node)->nd_next;
705 VALUE head = Qnil, next = Qnil;
706 if (n) {
707 head = NEW_CHILD(ast_value, n->nd_head);
708 next = NEW_CHILD(ast_value, n->nd_next);
709 }
710 return rb_ary_new_from_args(3, rb_node_dstr_string_val(node), head, next);
711 }
712 case NODE_SYM:
713 return rb_ary_new_from_args(1, rb_node_sym_string_val(node));
714 case NODE_EVSTR:
715 return rb_ary_new_from_node_args(ast_value, 1, RNODE_EVSTR(node)->nd_body);
716 case NODE_ARGSCAT:
717 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ARGSCAT(node)->nd_head, RNODE_ARGSCAT(node)->nd_body);
718 case NODE_ARGSPUSH:
719 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ARGSPUSH(node)->nd_head, RNODE_ARGSPUSH(node)->nd_body);
720 case NODE_SPLAT:
721 return rb_ary_new_from_node_args(ast_value, 1, RNODE_SPLAT(node)->nd_head);
722 case NODE_BLOCK_PASS:
723 return rb_ary_new_from_node_args(ast_value, 2, RNODE_BLOCK_PASS(node)->nd_head, RNODE_BLOCK_PASS(node)->nd_body);
724 case NODE_DEFN:
725 return rb_ary_new_from_args(2, ID2SYM(RNODE_DEFN(node)->nd_mid), NEW_CHILD(ast_value, RNODE_DEFN(node)->nd_defn));
726 case NODE_DEFS:
727 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));
728 case NODE_ALIAS:
729 return rb_ary_new_from_node_args(ast_value, 2, RNODE_ALIAS(node)->nd_1st, RNODE_ALIAS(node)->nd_2nd);
730 case NODE_VALIAS:
731 return rb_ary_new_from_args(2, ID2SYM(RNODE_VALIAS(node)->nd_alias), ID2SYM(RNODE_VALIAS(node)->nd_orig));
732 case NODE_UNDEF:
733 return rb_ary_new_from_args(1, dump_parser_array(ast_value, RNODE_UNDEF(node)->nd_undefs));
734 case NODE_CLASS:
735 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);
736 case NODE_MODULE:
737 return rb_ary_new_from_node_args(ast_value, 2, RNODE_MODULE(node)->nd_cpath, RNODE_MODULE(node)->nd_body);
738 case NODE_SCLASS:
739 return rb_ary_new_from_node_args(ast_value, 2, RNODE_SCLASS(node)->nd_recv, RNODE_SCLASS(node)->nd_body);
740 case NODE_COLON2:
741 return rb_ary_new_from_args(2, NEW_CHILD(ast_value, RNODE_COLON2(node)->nd_head), ID2SYM(RNODE_COLON2(node)->nd_mid));
742 case NODE_COLON3:
743 return rb_ary_new_from_args(1, ID2SYM(RNODE_COLON3(node)->nd_mid));
744 case NODE_DOT2:
745 case NODE_DOT3:
746 case NODE_FLIP2:
747 case NODE_FLIP3:
748 return rb_ary_new_from_node_args(ast_value, 2, RNODE_DOT2(node)->nd_beg, RNODE_DOT2(node)->nd_end);
749 case NODE_SELF:
750 return rb_ary_new_from_node_args(ast_value, 0);
751 case NODE_NIL:
752 return rb_ary_new_from_node_args(ast_value, 0);
753 case NODE_TRUE:
754 return rb_ary_new_from_node_args(ast_value, 0);
755 case NODE_FALSE:
756 return rb_ary_new_from_node_args(ast_value, 0);
757 case NODE_ERRINFO:
758 return rb_ary_new_from_node_args(ast_value, 0);
759 case NODE_DEFINED:
760 return rb_ary_new_from_node_args(ast_value, 1, RNODE_DEFINED(node)->nd_head);
761 case NODE_POSTEXE:
762 return rb_ary_new_from_node_args(ast_value, 1, RNODE_POSTEXE(node)->nd_body);
763 case NODE_ATTRASGN:
764 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));
765 case NODE_LAMBDA:
766 return rb_ary_new_from_node_args(ast_value, 1, RNODE_LAMBDA(node)->nd_body);
767 case NODE_OPT_ARG:
768 return rb_ary_new_from_node_args(ast_value, 2, RNODE_OPT_ARG(node)->nd_body, RNODE_OPT_ARG(node)->nd_next);
769 case NODE_KW_ARG:
770 return rb_ary_new_from_node_args(ast_value, 2, RNODE_KW_ARG(node)->nd_body, RNODE_KW_ARG(node)->nd_next);
771 case NODE_POSTARG:
772 if (NODE_NAMED_REST_P(RNODE_POSTARG(node)->nd_1st)) {
773 return rb_ary_new_from_node_args(ast_value, 2, RNODE_POSTARG(node)->nd_1st, RNODE_POSTARG(node)->nd_2nd);
774 }
775 return rb_ary_new_from_args(2, no_name_rest(),
776 NEW_CHILD(ast_value, RNODE_POSTARG(node)->nd_2nd));
777 case NODE_ARGS:
778 {
779 struct rb_args_info *ainfo = &RNODE_ARGS(node)->nd_ainfo;
780 return rb_ary_new_from_args(10,
781 INT2NUM(ainfo->pre_args_num),
782 NEW_CHILD(ast_value, ainfo->pre_init),
783 NEW_CHILD(ast_value, (NODE *)ainfo->opt_args),
784 var_name(ainfo->first_post_arg),
785 INT2NUM(ainfo->post_args_num),
786 NEW_CHILD(ast_value, ainfo->post_init),
787 (ainfo->rest_arg == NODE_SPECIAL_EXCESSIVE_COMMA
788 ? ID2SYM(rb_intern("NODE_SPECIAL_EXCESSIVE_COMMA"))
789 : var_name(ainfo->rest_arg)),
790 (ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast_value, (NODE *)ainfo->kw_args)),
791 (ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast_value, ainfo->kw_rest_arg)),
792 (ainfo->no_blockarg ? Qfalse : var_name(ainfo->block_arg)));
793 }
794 case NODE_SCOPE:
795 {
796 rb_ast_id_table_t *tbl = RNODE_SCOPE(node)->nd_tbl;
797 int i, size = tbl ? tbl->size : 0;
798 VALUE locals = rb_ary_new_capa(size);
799 for (i = 0; i < size; i++) {
800 rb_ary_push(locals, var_name(tbl->ids[i]));
801 }
802 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));
803 }
804 case NODE_ARYPTN:
805 {
806 VALUE rest = rest_arg(ast_value, RNODE_ARYPTN(node)->rest_arg);
807 return rb_ary_new_from_args(4,
808 NEW_CHILD(ast_value, RNODE_ARYPTN(node)->nd_pconst),
809 NEW_CHILD(ast_value, RNODE_ARYPTN(node)->pre_args),
810 rest,
811 NEW_CHILD(ast_value, RNODE_ARYPTN(node)->post_args));
812 }
813 case NODE_FNDPTN:
814 {
815 VALUE pre_rest = rest_arg(ast_value, RNODE_FNDPTN(node)->pre_rest_arg);
816 VALUE post_rest = rest_arg(ast_value, RNODE_FNDPTN(node)->post_rest_arg);
817 return rb_ary_new_from_args(4,
818 NEW_CHILD(ast_value, RNODE_FNDPTN(node)->nd_pconst),
819 pre_rest,
820 NEW_CHILD(ast_value, RNODE_FNDPTN(node)->args),
821 post_rest);
822 }
823 case NODE_HSHPTN:
824 {
825 VALUE kwrest = RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD ? ID2SYM(rb_intern("NODE_SPECIAL_NO_REST_KEYWORD")) :
826 NEW_CHILD(ast_value, RNODE_HSHPTN(node)->nd_pkwrestarg);
827
828 return rb_ary_new_from_args(3,
829 NEW_CHILD(ast_value, RNODE_HSHPTN(node)->nd_pconst),
830 NEW_CHILD(ast_value, RNODE_HSHPTN(node)->nd_pkwargs),
831 kwrest);
832 }
833 case NODE_LINE:
834 return rb_ary_new_from_args(1, rb_node_line_lineno_val(node));
835 case NODE_FILE:
836 return rb_ary_new_from_args(1, rb_node_file_path_val(node));
837 case NODE_ENCODING:
838 return rb_ary_new_from_args(1, rb_node_encoding_val(node));
839 case NODE_ERROR:
840 return rb_ary_new_from_node_args(ast_value, 0);
841 case NODE_ARGS_AUX:
842 case NODE_LAST:
843 break;
844 }
845
846 rb_bug("node_children: unknown node: %s", ruby_node_name(type));
847}
848
849static VALUE
850ast_node_children(rb_execution_context_t *ec, VALUE self)
851{
852 struct ASTNodeData *data;
853 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
854
855 return node_children(data->ast_value, data->node);
856}
857
858static int
859null_loc_p(rb_code_location_t *loc)
860{
861 return (loc->beg_pos.lineno == 0 && loc->beg_pos.column == -1 && loc->end_pos.lineno == 0 && loc->end_pos.column == -1);
862}
863
864static VALUE
865location_new(rb_code_location_t *loc)
866{
867 VALUE obj;
868 struct ASTLocationData *data;
869
870 if (null_loc_p(loc)) return Qnil;
871
872 obj = TypedData_Make_Struct(rb_cLocation, struct ASTLocationData, &rb_location_type, data);
873 data->first_lineno = loc->beg_pos.lineno;
874 data->first_column = loc->beg_pos.column;
875 data->last_lineno = loc->end_pos.lineno;
876 data->last_column = loc->end_pos.column;
877
878 return obj;
879}
880
881static VALUE
882node_locations(VALUE ast_value, const NODE *node)
883{
884 enum node_type type = nd_type(node);
885 switch (type) {
886 case NODE_ALIAS:
887 return rb_ary_new_from_args(2,
888 location_new(nd_code_loc(node)),
889 location_new(&RNODE_ALIAS(node)->keyword_loc));
890 case NODE_AND:
891 return rb_ary_new_from_args(2,
892 location_new(nd_code_loc(node)),
893 location_new(&RNODE_AND(node)->operator_loc));
894 case NODE_BLOCK_PASS:
895 return rb_ary_new_from_args(2,
896 location_new(nd_code_loc(node)),
897 location_new(&RNODE_BLOCK_PASS(node)->operator_loc));
898 case NODE_BREAK:
899 return rb_ary_new_from_args(2,
900 location_new(nd_code_loc(node)),
901 location_new(&RNODE_BREAK(node)->keyword_loc));
902 case NODE_CASE:
903 return rb_ary_new_from_args(3,
904 location_new(nd_code_loc(node)),
905 location_new(&RNODE_CASE(node)->case_keyword_loc),
906 location_new(&RNODE_CASE(node)->end_keyword_loc));
907 case NODE_CASE2:
908 return rb_ary_new_from_args(3,
909 location_new(nd_code_loc(node)),
910 location_new(&RNODE_CASE2(node)->case_keyword_loc),
911 location_new(&RNODE_CASE2(node)->end_keyword_loc));
912 case NODE_CASE3:
913 return rb_ary_new_from_args(3,
914 location_new(nd_code_loc(node)),
915 location_new(&RNODE_CASE3(node)->case_keyword_loc),
916 location_new(&RNODE_CASE3(node)->end_keyword_loc));
917 case NODE_CLASS:
918 return rb_ary_new_from_args(4,
919 location_new(nd_code_loc(node)),
920 location_new(&RNODE_CLASS(node)->class_keyword_loc),
921 location_new(&RNODE_CLASS(node)->inheritance_operator_loc),
922 location_new(&RNODE_CLASS(node)->end_keyword_loc));
923 case NODE_COLON2:
924 return rb_ary_new_from_args(3,
925 location_new(nd_code_loc(node)),
926 location_new(&RNODE_COLON2(node)->delimiter_loc),
927 location_new(&RNODE_COLON2(node)->name_loc));
928 case NODE_COLON3:
929 return rb_ary_new_from_args(3,
930 location_new(nd_code_loc(node)),
931 location_new(&RNODE_COLON3(node)->delimiter_loc),
932 location_new(&RNODE_COLON3(node)->name_loc));
933 case NODE_DEFINED:
934 return rb_ary_new_from_args(2,
935 location_new(nd_code_loc(node)),
936 location_new(&RNODE_DEFINED(node)->keyword_loc));
937 case NODE_DOT2:
938 return rb_ary_new_from_args(2,
939 location_new(nd_code_loc(node)),
940 location_new(&RNODE_DOT2(node)->operator_loc));
941 case NODE_DOT3:
942 return rb_ary_new_from_args(2,
943 location_new(nd_code_loc(node)),
944 location_new(&RNODE_DOT3(node)->operator_loc));
945 case NODE_EVSTR:
946 return rb_ary_new_from_args(3,
947 location_new(nd_code_loc(node)),
948 location_new(&RNODE_EVSTR(node)->opening_loc),
949 location_new(&RNODE_EVSTR(node)->closing_loc));
950 case NODE_FLIP2:
951 return rb_ary_new_from_args(2,
952 location_new(nd_code_loc(node)),
953 location_new(&RNODE_FLIP2(node)->operator_loc));
954 case NODE_FLIP3:
955 return rb_ary_new_from_args(2,
956 location_new(nd_code_loc(node)),
957 location_new(&RNODE_FLIP3(node)->operator_loc));
958 case NODE_FOR:
959 return rb_ary_new_from_args(5,
960 location_new(nd_code_loc(node)),
961 location_new(&RNODE_FOR(node)->for_keyword_loc),
962 location_new(&RNODE_FOR(node)->in_keyword_loc),
963 location_new(&RNODE_FOR(node)->do_keyword_loc),
964 location_new(&RNODE_FOR(node)->end_keyword_loc));
965 case NODE_LAMBDA:
966 return rb_ary_new_from_args(4,
967 location_new(nd_code_loc(node)),
968 location_new(&RNODE_LAMBDA(node)->operator_loc),
969 location_new(&RNODE_LAMBDA(node)->opening_loc),
970 location_new(&RNODE_LAMBDA(node)->closing_loc));
971 case NODE_IF:
972 return rb_ary_new_from_args(4,
973 location_new(nd_code_loc(node)),
974 location_new(&RNODE_IF(node)->if_keyword_loc),
975 location_new(&RNODE_IF(node)->then_keyword_loc),
976 location_new(&RNODE_IF(node)->end_keyword_loc));
977 case NODE_IN:
978 return rb_ary_new_from_args(4,
979 location_new(nd_code_loc(node)),
980 location_new(&RNODE_IN(node)->in_keyword_loc),
981 location_new(&RNODE_IN(node)->then_keyword_loc),
982 location_new(&RNODE_IN(node)->operator_loc));
983 case NODE_MODULE:
984 return rb_ary_new_from_args(3,
985 location_new(nd_code_loc(node)),
986 location_new(&RNODE_MODULE(node)->module_keyword_loc),
987 location_new(&RNODE_MODULE(node)->end_keyword_loc));
988 case NODE_NEXT:
989 return rb_ary_new_from_args(2,
990 location_new(nd_code_loc(node)),
991 location_new(&RNODE_NEXT(node)->keyword_loc));
992 case NODE_OR:
993 return rb_ary_new_from_args(2,
994 location_new(nd_code_loc(node)),
995 location_new(&RNODE_OR(node)->operator_loc));
996 case NODE_OP_ASGN1:
997 return rb_ary_new_from_args(5,
998 location_new(nd_code_loc(node)),
999 location_new(&RNODE_OP_ASGN1(node)->call_operator_loc),
1000 location_new(&RNODE_OP_ASGN1(node)->opening_loc),
1001 location_new(&RNODE_OP_ASGN1(node)->closing_loc),
1002 location_new(&RNODE_OP_ASGN1(node)->binary_operator_loc));
1003 case NODE_OP_ASGN2:
1004 return rb_ary_new_from_args(4,
1005 location_new(nd_code_loc(node)),
1006 location_new(&RNODE_OP_ASGN2(node)->call_operator_loc),
1007 location_new(&RNODE_OP_ASGN2(node)->message_loc),
1008 location_new(&RNODE_OP_ASGN2(node)->binary_operator_loc));
1009 case NODE_POSTEXE:
1010 return rb_ary_new_from_args(4,
1011 location_new(nd_code_loc(node)),
1012 location_new(&RNODE_POSTEXE(node)->keyword_loc),
1013 location_new(&RNODE_POSTEXE(node)->opening_loc),
1014 location_new(&RNODE_POSTEXE(node)->closing_loc));
1015 case NODE_REDO:
1016 return rb_ary_new_from_args(2,
1017 location_new(nd_code_loc(node)),
1018 location_new(&RNODE_REDO(node)->keyword_loc));
1019 case NODE_REGX:
1020 return rb_ary_new_from_args(4,
1021 location_new(nd_code_loc(node)),
1022 location_new(&RNODE_REGX(node)->opening_loc),
1023 location_new(&RNODE_REGX(node)->content_loc),
1024 location_new(&RNODE_REGX(node)->closing_loc));
1025 case NODE_RETURN:
1026 return rb_ary_new_from_args(2,
1027 location_new(nd_code_loc(node)),
1028 location_new(&RNODE_RETURN(node)->keyword_loc));
1029
1030 case NODE_SCLASS:
1031 return rb_ary_new_from_args(4,
1032 location_new(nd_code_loc(node)),
1033 location_new(&RNODE_SCLASS(node)->class_keyword_loc),
1034 location_new(&RNODE_SCLASS(node)->operator_loc),
1035 location_new(&RNODE_SCLASS(node)->end_keyword_loc));
1036
1037 case NODE_SPLAT:
1038 return rb_ary_new_from_args(2,
1039 location_new(nd_code_loc(node)),
1040 location_new(&RNODE_SPLAT(node)->operator_loc));
1041 case NODE_SUPER:
1042 return rb_ary_new_from_args(4,
1043 location_new(nd_code_loc(node)),
1044 location_new(&RNODE_SUPER(node)->keyword_loc),
1045 location_new(&RNODE_SUPER(node)->lparen_loc),
1046 location_new(&RNODE_SUPER(node)->rparen_loc));
1047 case NODE_UNDEF:
1048 return rb_ary_new_from_args(2,
1049 location_new(nd_code_loc(node)),
1050 location_new(&RNODE_UNDEF(node)->keyword_loc));
1051 case NODE_UNLESS:
1052 return rb_ary_new_from_args(4,
1053 location_new(nd_code_loc(node)),
1054 location_new(&RNODE_UNLESS(node)->keyword_loc),
1055 location_new(&RNODE_UNLESS(node)->then_keyword_loc),
1056 location_new(&RNODE_UNLESS(node)->end_keyword_loc));
1057 case NODE_VALIAS:
1058 return rb_ary_new_from_args(2,
1059 location_new(nd_code_loc(node)),
1060 location_new(&RNODE_VALIAS(node)->keyword_loc));
1061 case NODE_WHEN:
1062 return rb_ary_new_from_args(3,
1063 location_new(nd_code_loc(node)),
1064 location_new(&RNODE_WHEN(node)->keyword_loc),
1065 location_new(&RNODE_WHEN(node)->then_keyword_loc));
1066 case NODE_WHILE:
1067 return rb_ary_new_from_args(3,
1068 location_new(nd_code_loc(node)),
1069 location_new(&RNODE_WHILE(node)->keyword_loc),
1070 location_new(&RNODE_WHILE(node)->closing_loc));
1071 case NODE_UNTIL:
1072 return rb_ary_new_from_args(3,
1073 location_new(nd_code_loc(node)),
1074 location_new(&RNODE_UNTIL(node)->keyword_loc),
1075 location_new(&RNODE_UNTIL(node)->closing_loc));
1076 case NODE_YIELD:
1077 return rb_ary_new_from_args(4,
1078 location_new(nd_code_loc(node)),
1079 location_new(&RNODE_YIELD(node)->keyword_loc),
1080 location_new(&RNODE_YIELD(node)->lparen_loc),
1081 location_new(&RNODE_YIELD(node)->rparen_loc));
1082 case NODE_ARGS_AUX:
1083 case NODE_LAST:
1084 break;
1085 default:
1086 return rb_ary_new_from_args(1, location_new(nd_code_loc(node)));
1087 }
1088
1089 rb_bug("node_locations: unknown node: %s", ruby_node_name(type));
1090}
1091
1092static VALUE
1093ast_node_locations(rb_execution_context_t *ec, VALUE self)
1094{
1095 struct ASTNodeData *data;
1096 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1097
1098 return node_locations(data->ast_value, data->node);
1099}
1100
1101static VALUE
1102ast_node_first_lineno(rb_execution_context_t *ec, VALUE self)
1103{
1104 struct ASTNodeData *data;
1105 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1106
1107 return INT2NUM(nd_first_lineno(data->node));
1108}
1109
1110static VALUE
1111ast_node_first_column(rb_execution_context_t *ec, VALUE self)
1112{
1113 struct ASTNodeData *data;
1114 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1115
1116 return INT2NUM(nd_first_column(data->node));
1117}
1118
1119static VALUE
1120ast_node_last_lineno(rb_execution_context_t *ec, VALUE self)
1121{
1122 struct ASTNodeData *data;
1123 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1124
1125 return INT2NUM(nd_last_lineno(data->node));
1126}
1127
1128static VALUE
1129ast_node_last_column(rb_execution_context_t *ec, VALUE self)
1130{
1131 struct ASTNodeData *data;
1132 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1133
1134 return INT2NUM(nd_last_column(data->node));
1135}
1136
1137static VALUE
1138ast_node_all_tokens(rb_execution_context_t *ec, VALUE self)
1139{
1140 long i;
1141 struct ASTNodeData *data;
1142 rb_ast_t *ast;
1143 rb_parser_ary_t *parser_tokens;
1144 rb_parser_ast_token_t *parser_token;
1145 VALUE str, loc, token, all_tokens;
1146
1147 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1148 ast = rb_ruby_ast_data_get(data->ast_value);
1149
1150 parser_tokens = ast->node_buffer->tokens;
1151 if (parser_tokens == NULL) {
1152 return Qnil;
1153 }
1154
1155 all_tokens = rb_ary_new2(parser_tokens->len);
1156 for (i = 0; i < parser_tokens->len; i++) {
1157 parser_token = parser_tokens->data[i];
1158 str = rb_str_new(parser_token->str->ptr, parser_token->str->len);
1159 loc = rb_ary_new_from_args(4,
1160 INT2FIX(parser_token->loc.beg_pos.lineno),
1161 INT2FIX(parser_token->loc.beg_pos.column),
1162 INT2FIX(parser_token->loc.end_pos.lineno),
1163 INT2FIX(parser_token->loc.end_pos.column)
1164 );
1165 token = rb_ary_new_from_args(4, INT2FIX(parser_token->id), ID2SYM(rb_intern(parser_token->type_name)), str, loc);
1166 rb_ary_push(all_tokens, token);
1167 }
1168 rb_ary_freeze(all_tokens);
1169
1170 return all_tokens;
1171}
1172
1173static VALUE
1174ast_node_inspect(rb_execution_context_t *ec, VALUE self)
1175{
1176 VALUE str;
1177 VALUE cname;
1178 struct ASTNodeData *data;
1179 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1180
1181 cname = rb_class_path(rb_obj_class(self));
1182 str = rb_str_new2("#<");
1183
1184 rb_str_append(str, cname);
1185 rb_str_catf(str, ":%s@%d:%d-%d:%d>",
1186 node_type_to_str(data->node),
1187 nd_first_lineno(data->node), nd_first_column(data->node),
1188 nd_last_lineno(data->node), nd_last_column(data->node));
1189
1190 return str;
1191}
1192
1193static VALUE
1194ast_node_script_lines(rb_execution_context_t *ec, VALUE self)
1195{
1196 struct ASTNodeData *data;
1197 rb_ast_t *ast;
1198 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
1199 ast = rb_ruby_ast_data_get(data->ast_value);
1200 rb_parser_ary_t *ret = ast->body.script_lines;
1201 return rb_parser_build_script_lines_from(ret);
1202}
1203
1204static VALUE
1205ast_location_first_lineno(rb_execution_context_t *ec, VALUE self)
1206{
1207 struct ASTLocationData *data;
1208 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1209
1210 return INT2NUM(data->first_lineno);
1211}
1212
1213static VALUE
1214ast_location_first_column(rb_execution_context_t *ec, VALUE self)
1215{
1216 struct ASTLocationData *data;
1217 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1218
1219 return INT2NUM(data->first_column);
1220}
1221
1222static VALUE
1223ast_location_last_lineno(rb_execution_context_t *ec, VALUE self)
1224{
1225 struct ASTLocationData *data;
1226 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1227
1228 return INT2NUM(data->last_lineno);
1229}
1230
1231static VALUE
1232ast_location_last_column(rb_execution_context_t *ec, VALUE self)
1233{
1234 struct ASTLocationData *data;
1235 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1236
1237 return INT2NUM(data->last_column);
1238}
1239
1240static VALUE
1241ast_location_inspect(rb_execution_context_t *ec, VALUE self)
1242{
1243 VALUE str;
1244 VALUE cname;
1245 struct ASTLocationData *data;
1246 TypedData_Get_Struct(self, struct ASTLocationData, &rb_location_type, data);
1247
1248 cname = rb_class_path(rb_obj_class(self));
1249 str = rb_str_new2("#<");
1250
1251 rb_str_append(str, cname);
1252 rb_str_catf(str, ":@%d:%d-%d:%d>",
1253 data->first_lineno, data->first_column,
1254 data->last_lineno, data->last_column);
1255
1256 return str;
1257}
1258
1259#include "ast.rbinc"
1260
1261void
1262Init_ast(void)
1263{
1264 rb_mAST = rb_define_module_under(rb_cRubyVM, "AbstractSyntaxTree");
1265 rb_cNode = rb_define_class_under(rb_mAST, "Node", rb_cObject);
1266 rb_cLocation = rb_define_class_under(rb_mAST, "Location", rb_cObject);
1267 rb_undef_alloc_func(rb_cNode);
1268 rb_undef_alloc_func(rb_cLocation);
1269}
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1700
#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:1457
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1455
VALUE rb_cObject
Object class.
Definition object.c:60
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:240
Encoding relates APIs.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1148
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_ary_new_capa(rb_len_t capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
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_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:7515
VALUE rb_io_close(VALUE io)
Closes the IO.
Definition io.c:5985
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:3938
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1523
#define rb_strlen_lit(str)
Length of a string literal.
Definition string.h:1717
#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:1539
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:396
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1865
#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)
References an object in an array.
Definition rarray.h:421
#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
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
Definition value.h:142
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