Coverage Report

Created: 2026-06-03 06:22

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