Ruby: ast.c Source File

Ruby 3.4.6p54 (2025-09-16 revision dbd83256b1cec76c69756ecb8758b9e1079833de)

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