Ruby: ast.c Source File

Ruby 3.3.6p108 (2024-11-05 revision 75015d4c1f6965b5e85e96fb309f1f2129f933c0)

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;
17
18 struct ASTNodeData {
19 rb_ast_t *ast;
20 const NODE *node;
21};
22
23static void
24node_gc_mark(void *ptr)
25{
26 struct ASTNodeData *data = (struct ASTNodeData *)ptr;
27 rb_gc_mark((VALUE)data->ast);
28}
29
30static size_t
31node_memsize(const void *ptr)
32{
33 struct ASTNodeData *data = (struct ASTNodeData *)ptr;
34 return rb_ast_memsize(data->ast);
35}
36
37static const rb_data_type_t rb_node_type = {
38 "AST/node",
39 {node_gc_mark, RUBY_TYPED_DEFAULT_FREE, node_memsize,},
40 0, 0,
41 RUBY_TYPED_FREE_IMMEDIATELY,
42};
43
44static VALUE rb_ast_node_alloc(VALUE klass);
45
46static void
47setup_node(VALUE obj, rb_ast_t *ast, const NODE *node)
48{
49 struct ASTNodeData *data;
50
51 TypedData_Get_Struct(obj, struct ASTNodeData, &rb_node_type, data);
52 data->ast = ast;
53 data->node = node;
54}
55
56static VALUE
57ast_new_internal(rb_ast_t *ast, const NODE *node)
58{
59 VALUE obj;
60
61 obj = rb_ast_node_alloc(rb_cNode);
62 setup_node(obj, ast, node);
63
64 return obj;
65}
66
67static VALUE rb_ast_parse_str(VALUE str, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens);
68static VALUE rb_ast_parse_file(VALUE path, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens);
69
70static VALUE
71ast_parse_new(void)
72{
73 return rb_parser_set_context(rb_parser_new(), NULL, 0);
74}
75
76static VALUE
77ast_parse_done(rb_ast_t *ast)
78{
79 if (!ast->body.root) {
80 rb_ast_dispose(ast);
81 rb_exc_raise(GET_EC()->errinfo);
82 }
83
84 return ast_new_internal(ast, (NODE *)ast->body.root);
85}
86
87static VALUE
88ast_s_parse(rb_execution_context_t *ec, VALUE module, VALUE str, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
89{
90 return rb_ast_parse_str(str, keep_script_lines, error_tolerant, keep_tokens);
91}
92
93static VALUE
94rb_ast_parse_str(VALUE str, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
95{
96 rb_ast_t *ast = 0;
97
98 StringValue(str);
99 VALUE vparser = ast_parse_new();
100 if (RTEST(keep_script_lines)) rb_parser_set_script_lines(vparser, Qtrue);
101 if (RTEST(error_tolerant)) rb_parser_error_tolerant(vparser);
102 if (RTEST(keep_tokens)) rb_parser_keep_tokens(vparser);
103 ast = rb_parser_compile_string_path(vparser, Qnil, str, 1);
104 return ast_parse_done(ast);
105}
106
107static VALUE
108ast_s_parse_file(rb_execution_context_t *ec, VALUE module, VALUE path, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
109{
110 return rb_ast_parse_file(path, keep_script_lines, error_tolerant, keep_tokens);
111}
112
113static VALUE
114rb_ast_parse_file(VALUE path, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
115{
116 VALUE f;
117 rb_ast_t *ast = 0;
118 rb_encoding *enc = rb_utf8_encoding();
119
120 f = rb_file_open_str(path, "r");
121 rb_funcall(f, rb_intern("set_encoding"), 2, rb_enc_from_encoding(enc), rb_str_new_cstr("-"));
122 VALUE vparser = ast_parse_new();
123 if (RTEST(keep_script_lines)) rb_parser_set_script_lines(vparser, Qtrue);
124 if (RTEST(error_tolerant)) rb_parser_error_tolerant(vparser);
125 if (RTEST(keep_tokens)) rb_parser_keep_tokens(vparser);
126 ast = rb_parser_compile_file_path(vparser, Qnil, f, 1);
127 rb_io_close(f);
128 return ast_parse_done(ast);
129}
130
131static VALUE
132lex_array(VALUE array, int index)
133{
134 VALUE str = rb_ary_entry(array, index);
135 if (!NIL_P(str)) {
136 StringValue(str);
137 if (!rb_enc_asciicompat(rb_enc_get(str))) {
138 rb_raise(rb_eArgError, "invalid source encoding");
139 }
140 }
141 return str;
142}
143
144static VALUE
145rb_ast_parse_array(VALUE array, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
146{
147 rb_ast_t *ast = 0;
148
149 array = rb_check_array_type(array);
150 VALUE vparser = ast_parse_new();
151 if (RTEST(keep_script_lines)) rb_parser_set_script_lines(vparser, Qtrue);
152 if (RTEST(error_tolerant)) rb_parser_error_tolerant(vparser);
153 if (RTEST(keep_tokens)) rb_parser_keep_tokens(vparser);
154 ast = rb_parser_compile_generic(vparser, lex_array, Qnil, array, 1);
155 return ast_parse_done(ast);
156}
157
158static VALUE node_children(rb_ast_t*, const NODE*);
159
160static VALUE
161node_find(VALUE self, const int node_id)
162{
163 VALUE ary;
164 long i;
165 struct ASTNodeData *data;
166 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
167
168 if (nd_node_id(data->node) == node_id) return self;
169
170 ary = node_children(data->ast, data->node);
171
172 for (i = 0; i < RARRAY_LEN(ary); i++) {
173 VALUE child = RARRAY_AREF(ary, i);
174
175 if (CLASS_OF(child) == rb_cNode) {
176 VALUE result = node_find(child, node_id);
177 if (RTEST(result)) return result;
178 }
179 }
180
181 return Qnil;
182}
183
184extern VALUE rb_e_script;
185
186 VALUE
187rb_script_lines_for(VALUE path, bool add)
188{
189 VALUE hash, lines;
190 ID script_lines;
191 CONST_ID(script_lines, "SCRIPT_LINES__");
192 if (!rb_const_defined_at(rb_cObject, script_lines)) return Qnil;
193 hash = rb_const_get_at(rb_cObject, script_lines);
194 if (!RB_TYPE_P(hash, T_HASH)) return Qnil;
195 if (add) {
196 rb_hash_aset(hash, path, lines = rb_ary_new());
197 }
198 else if (!RB_TYPE_P((lines = rb_hash_lookup(hash, path)), T_ARRAY)) {
199 return Qnil;
200 }
201 return lines;
202}
203static VALUE
204script_lines(VALUE path)
205{
206 return rb_script_lines_for(path, false);
207}
208
209static VALUE
210node_id_for_backtrace_location(rb_execution_context_t *ec, VALUE module, VALUE location)
211{
212 int node_id;
213
214 if (!rb_frame_info_p(location)) {
215 rb_raise(rb_eTypeError, "Thread::Backtrace::Location object expected");
216 }
217
218 node_id = rb_get_node_id_from_frame_info(location);
219 if (node_id == -1) {
220 return Qnil;
221 }
222
223 return INT2NUM(node_id);
224}
225
226static VALUE
227ast_s_of(rb_execution_context_t *ec, VALUE module, VALUE body, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
228{
229 VALUE node, lines = Qnil;
230 const rb_iseq_t *iseq;
231 int node_id;
232
233 if (rb_frame_info_p(body)) {
234 iseq = rb_get_iseq_from_frame_info(body);
235 node_id = rb_get_node_id_from_frame_info(body);
236 }
237 else {
238 iseq = NULL;
239
240 if (rb_obj_is_proc(body)) {
241 iseq = vm_proc_iseq(body);
242
243 if (!rb_obj_is_iseq((VALUE)iseq)) return Qnil;
244 }
245 else {
246 iseq = rb_method_iseq(body);
247 }
248 if (iseq) {
249 node_id = ISEQ_BODY(iseq)->location.node_id;
250 }
251 }
252
253 if (!iseq) {
254 return Qnil;
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) || !NIL_P(lines = script_lines(path))) {
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, node) node ? ast_new_internal(ast, node) : Qnil
312
313static VALUE
314rb_ary_new_from_node_args(rb_ast_t *ast, 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, node));
327 }
328 va_end(ar);
329 return ary;
330}
331
332static VALUE
333dump_block(rb_ast_t *ast, const struct RNode_BLOCK *node)
334{
335 VALUE ary = rb_ary_new();
336 do {
337 rb_ary_push(ary, NEW_CHILD(ast, 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, node->nd_next));
343 }
344
345 return ary;
346}
347
348static VALUE
349dump_array(rb_ast_t *ast, const struct RNode_LIST *node)
350{
351 VALUE ary = rb_ary_new();
352 rb_ary_push(ary, NEW_CHILD(ast, 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, node->nd_head));
357 }
358 rb_ary_push(ary, NEW_CHILD(ast, node->nd_next));
359
360 return ary;
361}
362
363static VALUE
364var_name(ID id)
365{
366 if (!id) return Qnil;
367 if (!rb_id2str(id)) return Qnil;
368 return ID2SYM(id);
369}
370
371static VALUE
372no_name_rest(void)
373{
374 ID rest;
375 CONST_ID(rest, "NODE_SPECIAL_NO_NAME_REST");
376 return ID2SYM(rest);
377}
378
379static VALUE
380rest_arg(rb_ast_t *ast, const NODE *rest_arg)
381{
382 return NODE_NAMED_REST_P(rest_arg) ? NEW_CHILD(ast, rest_arg) : no_name_rest();
383}
384
385static VALUE
386node_children(rb_ast_t *ast, const NODE *node)
387{
388 char name[sizeof("$") + DECIMAL_SIZE_OF(long)];
389
390 enum node_type type = nd_type(node);
391 switch (type) {
392 case NODE_BLOCK:
393 return dump_block(ast, RNODE_BLOCK(node));
394 case NODE_IF:
395 return rb_ary_new_from_node_args(ast, 3, RNODE_IF(node)->nd_cond, RNODE_IF(node)->nd_body, RNODE_IF(node)->nd_else);
396 case NODE_UNLESS:
397 return rb_ary_new_from_node_args(ast, 3, RNODE_UNLESS(node)->nd_cond, RNODE_UNLESS(node)->nd_body, RNODE_UNLESS(node)->nd_else);
398 case NODE_CASE:
399 return rb_ary_new_from_node_args(ast, 2, RNODE_CASE(node)->nd_head, RNODE_CASE(node)->nd_body);
400 case NODE_CASE2:
401 return rb_ary_new_from_node_args(ast, 2, RNODE_CASE2(node)->nd_head, RNODE_CASE2(node)->nd_body);
402 case NODE_CASE3:
403 return rb_ary_new_from_node_args(ast, 2, RNODE_CASE3(node)->nd_head, RNODE_CASE3(node)->nd_body);
404 case NODE_WHEN:
405 return rb_ary_new_from_node_args(ast, 3, RNODE_WHEN(node)->nd_head, RNODE_WHEN(node)->nd_body, RNODE_WHEN(node)->nd_next);
406 case NODE_IN:
407 return rb_ary_new_from_node_args(ast, 3, RNODE_IN(node)->nd_head, RNODE_IN(node)->nd_body, RNODE_IN(node)->nd_next);
408 case NODE_WHILE:
409 case NODE_UNTIL:
410 return rb_ary_push(rb_ary_new_from_node_args(ast, 2, RNODE_WHILE(node)->nd_cond, RNODE_WHILE(node)->nd_body),
411 RBOOL(RNODE_WHILE(node)->nd_state));
412 case NODE_ITER:
413 case NODE_FOR:
414 return rb_ary_new_from_node_args(ast, 2, RNODE_ITER(node)->nd_iter, RNODE_ITER(node)->nd_body);
415 case NODE_FOR_MASGN:
416 return rb_ary_new_from_node_args(ast, 1, RNODE_FOR_MASGN(node)->nd_var);
417 case NODE_BREAK:
418 return rb_ary_new_from_node_args(ast, 1, RNODE_BREAK(node)->nd_stts);
419 case NODE_NEXT:
420 return rb_ary_new_from_node_args(ast, 1, RNODE_NEXT(node)->nd_stts);
421 case NODE_RETURN:
422 return rb_ary_new_from_node_args(ast, 1, RNODE_RETURN(node)->nd_stts);
423 case NODE_REDO:
424 return rb_ary_new_from_node_args(ast, 0);
425 case NODE_RETRY:
426 return rb_ary_new_from_node_args(ast, 0);
427 case NODE_BEGIN:
428 return rb_ary_new_from_node_args(ast, 1, RNODE_BEGIN(node)->nd_body);
429 case NODE_RESCUE:
430 return rb_ary_new_from_node_args(ast, 3, RNODE_RESCUE(node)->nd_head, RNODE_RESCUE(node)->nd_resq, RNODE_RESCUE(node)->nd_else);
431 case NODE_RESBODY:
432 return rb_ary_new_from_node_args(ast, 3, RNODE_RESBODY(node)->nd_args, RNODE_RESBODY(node)->nd_body, RNODE_RESBODY(node)->nd_head);
433 case NODE_ENSURE:
434 return rb_ary_new_from_node_args(ast, 2, RNODE_ENSURE(node)->nd_head, RNODE_ENSURE(node)->nd_ensr);
435 case NODE_AND:
436 case NODE_OR:
437 {
438 VALUE ary = rb_ary_new();
439
440 while (1) {
441 rb_ary_push(ary, NEW_CHILD(ast, RNODE_AND(node)->nd_1st));
442 if (!RNODE_AND(node)->nd_2nd || !nd_type_p(RNODE_AND(node)->nd_2nd, type))
443 break;
444 node = RNODE_AND(node)->nd_2nd;
445 }
446 rb_ary_push(ary, NEW_CHILD(ast, RNODE_AND(node)->nd_2nd));
447 return ary;
448 }
449 case NODE_MASGN:
450 if (NODE_NAMED_REST_P(RNODE_MASGN(node)->nd_args)) {
451 return rb_ary_new_from_node_args(ast, 3, RNODE_MASGN(node)->nd_value, RNODE_MASGN(node)->nd_head, RNODE_MASGN(node)->nd_args);
452 }
453 else {
454 return rb_ary_new_from_args(3, NEW_CHILD(ast, RNODE_MASGN(node)->nd_value),
455 NEW_CHILD(ast, RNODE_MASGN(node)->nd_head),
456 no_name_rest());
457 }
458 case NODE_LASGN:
459 if (NODE_REQUIRED_KEYWORD_P(RNODE_LASGN(node)->nd_value)) {
460 return rb_ary_new_from_args(2, var_name(RNODE_LASGN(node)->nd_vid), ID2SYM(rb_intern("NODE_SPECIAL_REQUIRED_KEYWORD")));
461 }
462 return rb_ary_new_from_args(2, var_name(RNODE_LASGN(node)->nd_vid), NEW_CHILD(ast, RNODE_LASGN(node)->nd_value));
463 case NODE_DASGN:
464 if (NODE_REQUIRED_KEYWORD_P(RNODE_DASGN(node)->nd_value)) {
465 return rb_ary_new_from_args(2, var_name(RNODE_DASGN(node)->nd_vid), ID2SYM(rb_intern("NODE_SPECIAL_REQUIRED_KEYWORD")));
466 }
467 return rb_ary_new_from_args(2, var_name(RNODE_DASGN(node)->nd_vid), NEW_CHILD(ast, RNODE_DASGN(node)->nd_value));
468 case NODE_IASGN:
469 return rb_ary_new_from_args(2, var_name(RNODE_IASGN(node)->nd_vid), NEW_CHILD(ast, RNODE_IASGN(node)->nd_value));
470 case NODE_CVASGN:
471 return rb_ary_new_from_args(2, var_name(RNODE_CVASGN(node)->nd_vid), NEW_CHILD(ast, RNODE_CVASGN(node)->nd_value));
472 case NODE_GASGN:
473 return rb_ary_new_from_args(2, var_name(RNODE_GASGN(node)->nd_vid), NEW_CHILD(ast, RNODE_GASGN(node)->nd_value));
474 case NODE_CDECL:
475 if (RNODE_CDECL(node)->nd_vid) {
476 return rb_ary_new_from_args(2, ID2SYM(RNODE_CDECL(node)->nd_vid), NEW_CHILD(ast, RNODE_CDECL(node)->nd_value));
477 }
478 return rb_ary_new_from_args(3, NEW_CHILD(ast, RNODE_CDECL(node)->nd_else), ID2SYM(RNODE_COLON2(RNODE_CDECL(node)->nd_else)->nd_mid), NEW_CHILD(ast, RNODE_CDECL(node)->nd_value));
479 case NODE_OP_ASGN1:
480 return rb_ary_new_from_args(4, NEW_CHILD(ast, RNODE_OP_ASGN1(node)->nd_recv),
481 ID2SYM(RNODE_OP_ASGN1(node)->nd_mid),
482 NEW_CHILD(ast, RNODE_OP_ASGN1(node)->nd_index),
483 NEW_CHILD(ast, RNODE_OP_ASGN1(node)->nd_rvalue));
484 case NODE_OP_ASGN2:
485 return rb_ary_new_from_args(5, NEW_CHILD(ast, RNODE_OP_ASGN2(node)->nd_recv),
486 RBOOL(RNODE_OP_ASGN2(node)->nd_aid),
487 ID2SYM(RNODE_OP_ASGN2(node)->nd_vid),
488 ID2SYM(RNODE_OP_ASGN2(node)->nd_mid),
489 NEW_CHILD(ast, RNODE_OP_ASGN2(node)->nd_value));
490 case NODE_OP_ASGN_AND:
491 return rb_ary_new_from_args(3, NEW_CHILD(ast, RNODE_OP_ASGN_AND(node)->nd_head), ID2SYM(idANDOP),
492 NEW_CHILD(ast, RNODE_OP_ASGN_AND(node)->nd_value));
493 case NODE_OP_ASGN_OR:
494 return rb_ary_new_from_args(3, NEW_CHILD(ast, RNODE_OP_ASGN_OR(node)->nd_head), ID2SYM(idOROP),
495 NEW_CHILD(ast, RNODE_OP_ASGN_OR(node)->nd_value));
496 case NODE_OP_CDECL:
497 return rb_ary_new_from_args(3, NEW_CHILD(ast, RNODE_OP_CDECL(node)->nd_head),
498 ID2SYM(RNODE_OP_CDECL(node)->nd_aid),
499 NEW_CHILD(ast, RNODE_OP_CDECL(node)->nd_value));
500 case NODE_CALL:
501 return rb_ary_new_from_args(3, NEW_CHILD(ast, RNODE_CALL(node)->nd_recv),
502 ID2SYM(RNODE_CALL(node)->nd_mid),
503 NEW_CHILD(ast, RNODE_CALL(node)->nd_args));
504 case NODE_OPCALL:
505 return rb_ary_new_from_args(3, NEW_CHILD(ast, RNODE_OPCALL(node)->nd_recv),
506 ID2SYM(RNODE_OPCALL(node)->nd_mid),
507 NEW_CHILD(ast, RNODE_OPCALL(node)->nd_args));
508 case NODE_QCALL:
509 return rb_ary_new_from_args(3, NEW_CHILD(ast, RNODE_QCALL(node)->nd_recv),
510 ID2SYM(RNODE_QCALL(node)->nd_mid),
511 NEW_CHILD(ast, RNODE_QCALL(node)->nd_args));
512 case NODE_FCALL:
513 return rb_ary_new_from_args(2, ID2SYM(RNODE_FCALL(node)->nd_mid),
514 NEW_CHILD(ast, RNODE_FCALL(node)->nd_args));
515 case NODE_VCALL:
516 return rb_ary_new_from_args(1, ID2SYM(RNODE_VCALL(node)->nd_mid));
517 case NODE_SUPER:
518 return rb_ary_new_from_node_args(ast, 1, RNODE_SUPER(node)->nd_args);
519 case NODE_ZSUPER:
520 return rb_ary_new_from_node_args(ast, 0);
521 case NODE_LIST:
522 return dump_array(ast, RNODE_LIST(node));
523 case NODE_ZLIST:
524 return rb_ary_new_from_node_args(ast, 0);
525 case NODE_HASH:
526 return rb_ary_new_from_node_args(ast, 1, RNODE_HASH(node)->nd_head);
527 case NODE_YIELD:
528 return rb_ary_new_from_node_args(ast, 1, RNODE_YIELD(node)->nd_head);
529 case NODE_LVAR:
530 return rb_ary_new_from_args(1, var_name(RNODE_LVAR(node)->nd_vid));
531 case NODE_DVAR:
532 return rb_ary_new_from_args(1, var_name(RNODE_DVAR(node)->nd_vid));
533 case NODE_IVAR:
534 return rb_ary_new_from_args(1, ID2SYM(RNODE_IVAR(node)->nd_vid));
535 case NODE_CONST:
536 return rb_ary_new_from_args(1, ID2SYM(RNODE_CONST(node)->nd_vid));
537 case NODE_CVAR:
538 return rb_ary_new_from_args(1, ID2SYM(RNODE_CVAR(node)->nd_vid));
539 case NODE_GVAR:
540 return rb_ary_new_from_args(1, ID2SYM(RNODE_GVAR(node)->nd_vid));
541 case NODE_NTH_REF:
542 snprintf(name, sizeof(name), "$%ld", RNODE_NTH_REF(node)->nd_nth);
543 return rb_ary_new_from_args(1, ID2SYM(rb_intern(name)));
544 case NODE_BACK_REF:
545 name[0] = '$';
546 name[1] = (char)RNODE_BACK_REF(node)->nd_nth;
547 name[2] = '\0';
548 return rb_ary_new_from_args(1, ID2SYM(rb_intern(name)));
549 case NODE_MATCH2:
550 if (RNODE_MATCH2(node)->nd_args) {
551 return rb_ary_new_from_node_args(ast, 3, RNODE_MATCH2(node)->nd_recv, RNODE_MATCH2(node)->nd_value, RNODE_MATCH2(node)->nd_args);
552 }
553 return rb_ary_new_from_node_args(ast, 2, RNODE_MATCH2(node)->nd_recv, RNODE_MATCH2(node)->nd_value);
554 case NODE_MATCH3:
555 return rb_ary_new_from_node_args(ast, 2, RNODE_MATCH3(node)->nd_recv, RNODE_MATCH3(node)->nd_value);
556 case NODE_MATCH:
557 case NODE_LIT:
558 case NODE_STR:
559 case NODE_XSTR:
560 return rb_ary_new_from_args(1, RNODE_LIT(node)->nd_lit);
561 case NODE_ONCE:
562 return rb_ary_new_from_node_args(ast, 1, RNODE_ONCE(node)->nd_body);
563 case NODE_DSTR:
564 case NODE_DXSTR:
565 case NODE_DREGX:
566 case NODE_DSYM:
567 {
568 struct RNode_LIST *n = RNODE_DSTR(node)->nd_next;
569 VALUE head = Qnil, next = Qnil;
570 if (n) {
571 head = NEW_CHILD(ast, n->nd_head);
572 next = NEW_CHILD(ast, n->nd_next);
573 }
574 return rb_ary_new_from_args(3, RNODE_DSTR(node)->nd_lit, head, next);
575 }
576 case NODE_EVSTR:
577 return rb_ary_new_from_node_args(ast, 1, RNODE_EVSTR(node)->nd_body);
578 case NODE_ARGSCAT:
579 return rb_ary_new_from_node_args(ast, 2, RNODE_ARGSCAT(node)->nd_head, RNODE_ARGSCAT(node)->nd_body);
580 case NODE_ARGSPUSH:
581 return rb_ary_new_from_node_args(ast, 2, RNODE_ARGSPUSH(node)->nd_head, RNODE_ARGSPUSH(node)->nd_body);
582 case NODE_SPLAT:
583 return rb_ary_new_from_node_args(ast, 1, RNODE_SPLAT(node)->nd_head);
584 case NODE_BLOCK_PASS:
585 return rb_ary_new_from_node_args(ast, 2, RNODE_BLOCK_PASS(node)->nd_head, RNODE_BLOCK_PASS(node)->nd_body);
586 case NODE_DEFN:
587 return rb_ary_new_from_args(2, ID2SYM(RNODE_DEFN(node)->nd_mid), NEW_CHILD(ast, RNODE_DEFN(node)->nd_defn));
588 case NODE_DEFS:
589 return rb_ary_new_from_args(3, NEW_CHILD(ast, RNODE_DEFS(node)->nd_recv), ID2SYM(RNODE_DEFS(node)->nd_mid), NEW_CHILD(ast, RNODE_DEFS(node)->nd_defn));
590 case NODE_ALIAS:
591 return rb_ary_new_from_node_args(ast, 2, RNODE_ALIAS(node)->nd_1st, RNODE_ALIAS(node)->nd_2nd);
592 case NODE_VALIAS:
593 return rb_ary_new_from_args(2, ID2SYM(RNODE_VALIAS(node)->nd_alias), ID2SYM(RNODE_VALIAS(node)->nd_orig));
594 case NODE_UNDEF:
595 return rb_ary_new_from_node_args(ast, 1, RNODE_UNDEF(node)->nd_undef);
596 case NODE_CLASS:
597 return rb_ary_new_from_node_args(ast, 3, RNODE_CLASS(node)->nd_cpath, RNODE_CLASS(node)->nd_super, RNODE_CLASS(node)->nd_body);
598 case NODE_MODULE:
599 return rb_ary_new_from_node_args(ast, 2, RNODE_MODULE(node)->nd_cpath, RNODE_MODULE(node)->nd_body);
600 case NODE_SCLASS:
601 return rb_ary_new_from_node_args(ast, 2, RNODE_SCLASS(node)->nd_recv, RNODE_SCLASS(node)->nd_body);
602 case NODE_COLON2:
603 return rb_ary_new_from_args(2, NEW_CHILD(ast, RNODE_COLON2(node)->nd_head), ID2SYM(RNODE_COLON2(node)->nd_mid));
604 case NODE_COLON3:
605 return rb_ary_new_from_args(1, ID2SYM(RNODE_COLON3(node)->nd_mid));
606 case NODE_DOT2:
607 case NODE_DOT3:
608 case NODE_FLIP2:
609 case NODE_FLIP3:
610 return rb_ary_new_from_node_args(ast, 2, RNODE_DOT2(node)->nd_beg, RNODE_DOT2(node)->nd_end);
611 case NODE_SELF:
612 return rb_ary_new_from_node_args(ast, 0);
613 case NODE_NIL:
614 return rb_ary_new_from_node_args(ast, 0);
615 case NODE_TRUE:
616 return rb_ary_new_from_node_args(ast, 0);
617 case NODE_FALSE:
618 return rb_ary_new_from_node_args(ast, 0);
619 case NODE_ERRINFO:
620 return rb_ary_new_from_node_args(ast, 0);
621 case NODE_DEFINED:
622 return rb_ary_new_from_node_args(ast, 1, RNODE_DEFINED(node)->nd_head);
623 case NODE_POSTEXE:
624 return rb_ary_new_from_node_args(ast, 1, RNODE_POSTEXE(node)->nd_body);
625 case NODE_ATTRASGN:
626 return rb_ary_new_from_args(3, NEW_CHILD(ast, RNODE_ATTRASGN(node)->nd_recv), ID2SYM(RNODE_ATTRASGN(node)->nd_mid), NEW_CHILD(ast, RNODE_ATTRASGN(node)->nd_args));
627 case NODE_LAMBDA:
628 return rb_ary_new_from_node_args(ast, 1, RNODE_LAMBDA(node)->nd_body);
629 case NODE_OPT_ARG:
630 return rb_ary_new_from_node_args(ast, 2, RNODE_OPT_ARG(node)->nd_body, RNODE_OPT_ARG(node)->nd_next);
631 case NODE_KW_ARG:
632 return rb_ary_new_from_node_args(ast, 2, RNODE_KW_ARG(node)->nd_body, RNODE_KW_ARG(node)->nd_next);
633 case NODE_POSTARG:
634 if (NODE_NAMED_REST_P(RNODE_POSTARG(node)->nd_1st)) {
635 return rb_ary_new_from_node_args(ast, 2, RNODE_POSTARG(node)->nd_1st, RNODE_POSTARG(node)->nd_2nd);
636 }
637 return rb_ary_new_from_args(2, no_name_rest(),
638 NEW_CHILD(ast, RNODE_POSTARG(node)->nd_2nd));
639 case NODE_ARGS:
640 {
641 struct rb_args_info *ainfo = &RNODE_ARGS(node)->nd_ainfo;
642 return rb_ary_new_from_args(10,
643 INT2NUM(ainfo->pre_args_num),
644 NEW_CHILD(ast, ainfo->pre_init),
645 NEW_CHILD(ast, (NODE *)ainfo->opt_args),
646 var_name(ainfo->first_post_arg),
647 INT2NUM(ainfo->post_args_num),
648 NEW_CHILD(ast, ainfo->post_init),
649 (ainfo->rest_arg == NODE_SPECIAL_EXCESSIVE_COMMA
650 ? ID2SYM(rb_intern("NODE_SPECIAL_EXCESSIVE_COMMA"))
651 : var_name(ainfo->rest_arg)),
652 (ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast, (NODE *)ainfo->kw_args)),
653 (ainfo->no_kwarg ? Qfalse : NEW_CHILD(ast, ainfo->kw_rest_arg)),
654 var_name(ainfo->block_arg));
655 }
656 case NODE_SCOPE:
657 {
658 rb_ast_id_table_t *tbl = RNODE_SCOPE(node)->nd_tbl;
659 int i, size = tbl ? tbl->size : 0;
660 VALUE locals = rb_ary_new_capa(size);
661 for (i = 0; i < size; i++) {
662 rb_ary_push(locals, var_name(tbl->ids[i]));
663 }
664 return rb_ary_new_from_args(3, locals, NEW_CHILD(ast, (NODE *)RNODE_SCOPE(node)->nd_args), NEW_CHILD(ast, RNODE_SCOPE(node)->nd_body));
665 }
666 case NODE_ARYPTN:
667 {
668 VALUE rest = rest_arg(ast, RNODE_ARYPTN(node)->rest_arg);
669 return rb_ary_new_from_args(4,
670 NEW_CHILD(ast, RNODE_ARYPTN(node)->nd_pconst),
671 NEW_CHILD(ast, RNODE_ARYPTN(node)->pre_args),
672 rest,
673 NEW_CHILD(ast, RNODE_ARYPTN(node)->post_args));
674 }
675 case NODE_FNDPTN:
676 {
677 VALUE pre_rest = rest_arg(ast, RNODE_FNDPTN(node)->pre_rest_arg);
678 VALUE post_rest = rest_arg(ast, RNODE_FNDPTN(node)->post_rest_arg);
679 return rb_ary_new_from_args(4,
680 NEW_CHILD(ast, RNODE_FNDPTN(node)->nd_pconst),
681 pre_rest,
682 NEW_CHILD(ast, RNODE_FNDPTN(node)->args),
683 post_rest);
684 }
685 case NODE_HSHPTN:
686 {
687 VALUE kwrest = RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD ? ID2SYM(rb_intern("NODE_SPECIAL_NO_REST_KEYWORD")) :
688 NEW_CHILD(ast, RNODE_HSHPTN(node)->nd_pkwrestarg);
689
690 return rb_ary_new_from_args(3,
691 NEW_CHILD(ast, RNODE_HSHPTN(node)->nd_pconst),
692 NEW_CHILD(ast, RNODE_HSHPTN(node)->nd_pkwargs),
693 kwrest);
694 }
695 case NODE_ERROR:
696 return rb_ary_new_from_node_args(ast, 0);
697 case NODE_ARGS_AUX:
698 case NODE_RIPPER:
699 case NODE_RIPPER_VALUES:
700 case NODE_LAST:
701 break;
702 }
703
704 rb_bug("node_children: unknown node: %s", ruby_node_name(type));
705}
706
707static VALUE
708ast_node_children(rb_execution_context_t *ec, VALUE self)
709{
710 struct ASTNodeData *data;
711 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
712
713 return node_children(data->ast, data->node);
714}
715
716static VALUE
717ast_node_first_lineno(rb_execution_context_t *ec, VALUE self)
718{
719 struct ASTNodeData *data;
720 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
721
722 return INT2NUM(nd_first_lineno(data->node));
723}
724
725static VALUE
726ast_node_first_column(rb_execution_context_t *ec, VALUE self)
727{
728 struct ASTNodeData *data;
729 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
730
731 return INT2NUM(nd_first_column(data->node));
732}
733
734static VALUE
735ast_node_last_lineno(rb_execution_context_t *ec, VALUE self)
736{
737 struct ASTNodeData *data;
738 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
739
740 return INT2NUM(nd_last_lineno(data->node));
741}
742
743static VALUE
744ast_node_last_column(rb_execution_context_t *ec, VALUE self)
745{
746 struct ASTNodeData *data;
747 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
748
749 return INT2NUM(nd_last_column(data->node));
750}
751
752static VALUE
753ast_node_all_tokens(rb_execution_context_t *ec, VALUE self)
754{
755 struct ASTNodeData *data;
756 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
757
758 return rb_ast_tokens(data->ast);
759}
760
761static VALUE
762ast_node_inspect(rb_execution_context_t *ec, VALUE self)
763{
764 VALUE str;
765 VALUE cname;
766 struct ASTNodeData *data;
767 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
768
769 cname = rb_class_path(rb_obj_class(self));
770 str = rb_str_new2("#<");
771
772 rb_str_append(str, cname);
773 rb_str_catf(str, ":%s@%d:%d-%d:%d>",
774 node_type_to_str(data->node),
775 nd_first_lineno(data->node), nd_first_column(data->node),
776 nd_last_lineno(data->node), nd_last_column(data->node));
777
778 return str;
779}
780
781static VALUE
782ast_node_script_lines(rb_execution_context_t *ec, VALUE self)
783{
784 struct ASTNodeData *data;
785 TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
786 VALUE ret = data->ast->body.script_lines;
787 if (!RB_TYPE_P(ret, T_ARRAY)) return Qnil;
788 return ret;
789}
790
791#include "ast.rbinc"
792
793void
794Init_ast(void)
795{
796 rb_mAST = rb_define_module_under(rb_cRubyVM, "AbstractSyntaxTree");
797 rb_cNode = rb_define_class_under(rb_mAST, "Node", rb_cObject);
798 rb_undef_alloc_func(rb_cNode);
799}
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:1002
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:1109
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
T_HASH
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
Qtrue
#define Qtrue
Old name of RUBY_Qtrue.
Definition special_consts.h:61
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
T_ARRAY
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
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:651
rb_eTypeError
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1344
rb_obj_class
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:215
encoding.h
Encoding relates APIs.
rb_funcall
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1121
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:7183
rb_io_close
VALUE rb_io_close(VALUE io)
Closes the IO.
Definition io.c:5688
rb_obj_is_proc
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:119
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:3409
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_const_get_at
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
Definition variable.c:3147
rb_const_defined_at
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
Definition variable.c:3455
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:283
rb_undef_alloc_func
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1274
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
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
ASTNodeData
Definition ast.c:18
OnigEncodingTypeST
Definition onigmo.h:160
RNode_BLOCK
Definition rubyparser.h:180
RNode_LIST
Definition rubyparser.h:500
RNode
Definition rubyparser.h:166
rb_args_info
Definition rubyparser.h:670
rb_ast_id_table
Definition rubyparser.h:150
rb_ast_struct
Definition rubyparser.h:1096
rb_data_type_struct
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:200
rb_execution_context_struct
Definition vm_core.h:986
rb_iseq_struct
Definition vm_core.h:534
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