Coverage Report

Created: 2026-06-02 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_ast.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright © Zend Technologies Ltd., a subsidiary company of          |
6
   |     Perforce Software, Inc., and Contributors.                       |
7
   +----------------------------------------------------------------------+
8
   | This source file is subject to the Modified BSD License that is      |
9
   | bundled with this package in the file LICENSE, and is available      |
10
   | through the World Wide Web at <https://www.php.net/license/>.        |
11
   |                                                                      |
12
   | SPDX-License-Identifier: BSD-3-Clause                                |
13
   +----------------------------------------------------------------------+
14
   | Authors: Bob Weinand <bwoebi@php.net>                                |
15
   |          Dmitry Stogov <dmitry@php.net>                              |
16
   +----------------------------------------------------------------------+
17
*/
18
19
#include "zend_ast.h"
20
#include "zend_API.h"
21
#include "zend_operators.h"
22
#include "zend_language_parser.h"
23
#include "zend_smart_str.h"
24
#include "zend_exceptions.h"
25
#include "zend_closures.h"
26
#include "zend_constants.h"
27
#include "zend_enum.h"
28
29
ZEND_API zend_ast_process_t zend_ast_process = NULL;
30
31
124k
static inline void *zend_ast_alloc(size_t size) {
32
124k
  return zend_arena_alloc(&CG(ast_arena), size);
33
124k
}
34
35
1.28k
static inline void *zend_ast_realloc(const void *old, size_t old_size, size_t new_size) {
36
1.28k
  void *new = zend_ast_alloc(new_size);
37
1.28k
  memcpy(new, old, old_size);
38
1.28k
  return new;
39
1.28k
}
40
41
30.9k
static inline size_t zend_ast_list_size(uint32_t children) {
42
30.9k
  return sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * children;
43
30.9k
}
44
45
224
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_znode(const znode *node) {
46
224
  zend_ast_znode *ast;
47
48
224
  ast = zend_ast_alloc(sizeof(zend_ast_znode));
49
224
  ast->kind = ZEND_AST_ZNODE;
50
224
  ast->attr = 0;
51
224
  ast->lineno = CG(zend_lineno);
52
224
  ast->node = *node;
53
224
  return (zend_ast *) ast;
54
224
}
55
56
76
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_fcc(zend_ast *args) {
57
76
  zend_ast_fcc *ast;
58
59
76
  ast = zend_ast_alloc(sizeof(zend_ast_fcc));
60
76
  ast->kind = ZEND_AST_CALLABLE_CONVERT;
61
76
  ast->attr = 0;
62
76
  ast->lineno = CG(zend_lineno);
63
76
  ast->args = args;
64
76
  ZEND_MAP_PTR_INIT(ast->fptr, NULL);
65
66
76
  return (zend_ast *) ast;
67
76
}
68
69
47.3k
static zend_always_inline zend_ast * zend_ast_create_zval_int(const zval *zv, uint32_t attr, uint32_t lineno) {
70
47.3k
  zend_ast_zval *ast;
71
72
47.3k
  ast = zend_ast_alloc(sizeof(zend_ast_zval));
73
47.3k
  ast->kind = ZEND_AST_ZVAL;
74
47.3k
  ast->attr = attr;
75
47.3k
  ZVAL_COPY_VALUE(&ast->val, zv);
76
47.3k
  Z_LINENO(ast->val) = lineno;
77
47.3k
  return (zend_ast *) ast;
78
47.3k
}
79
80
44.5k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_with_lineno(const zval *zv, uint32_t lineno) {
81
44.5k
  return zend_ast_create_zval_int(zv, 0, lineno);
82
44.5k
}
83
84
15
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_ex(const zval *zv, zend_ast_attr attr) {
85
15
  return zend_ast_create_zval_int(zv, attr, CG(zend_lineno));
86
15
}
87
88
1.32k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval(const zval *zv) {
89
1.32k
  return zend_ast_create_zval_int(zv, 0, CG(zend_lineno));
90
1.32k
}
91
92
238
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_str(zend_string *str) {
93
238
  zval zv;
94
238
  ZVAL_STR(&zv, str);
95
238
  return zend_ast_create_zval_int(&zv, 0, CG(zend_lineno));
96
238
}
97
98
1.25k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_long(zend_long lval) {
99
1.25k
  zval zv;
100
1.25k
  ZVAL_LONG(&zv, lval);
101
1.25k
  return zend_ast_create_zval_int(&zv, 0, CG(zend_lineno));
102
1.25k
}
103
104
97
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_constant(zend_string *name, zend_ast_attr attr) {
105
97
  zend_ast_zval *ast;
106
107
97
  ast = zend_ast_alloc(sizeof(zend_ast_zval));
108
97
  ast->kind = ZEND_AST_CONSTANT;
109
97
  ast->attr = attr;
110
97
  ZVAL_STR(&ast->val, name);
111
97
  Z_LINENO(ast->val) = CG(zend_lineno);
112
97
  return (zend_ast *) ast;
113
97
}
114
115
3
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_op_array(zend_op_array *op_array) {
116
3
  zend_ast_op_array *ast;
117
118
3
  ast = zend_ast_alloc(sizeof(zend_ast_op_array));
119
3
  ast->kind = ZEND_AST_OP_ARRAY;
120
3
  ast->attr = 0;
121
3
  ast->lineno = CG(zend_lineno);
122
3
  ast->op_array = op_array;
123
124
3
  return (zend_ast *) ast;
125
3
}
126
127
351
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_class_const_or_name(zend_ast *class_name, zend_ast *name) {
128
351
  zend_string *name_str = zend_ast_get_str(name);
129
351
  if (zend_string_equals_ci(name_str, ZSTR_KNOWN(ZEND_STR_CLASS))) {
130
114
    zend_string_release(name_str);
131
114
    return zend_ast_create(ZEND_AST_CLASS_NAME, class_name);
132
237
  } else {
133
237
    return zend_ast_create(ZEND_AST_CLASS_CONST, class_name, name);
134
237
  }
135
351
}
136
137
ZEND_API zend_ast *zend_ast_create_decl(
138
  zend_ast_kind kind, uint32_t flags, uint32_t start_lineno, zend_string *doc_comment,
139
  zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3, zend_ast *child4
140
3.53k
) {
141
3.53k
  zend_ast_decl *ast;
142
143
3.53k
  ast = zend_ast_alloc(sizeof(zend_ast_decl));
144
3.53k
  ast->kind = kind;
145
3.53k
  ast->attr = 0;
146
3.53k
  ast->start_lineno = start_lineno;
147
3.53k
  ast->end_lineno = CG(zend_lineno);
148
3.53k
  ast->flags = flags;
149
3.53k
  ast->doc_comment = doc_comment;
150
3.53k
  ast->name = name;
151
3.53k
  ast->child[0] = child0;
152
3.53k
  ast->child[1] = child1;
153
3.53k
  ast->child[2] = child2;
154
3.53k
  ast->child[3] = child3;
155
3.53k
  ast->child[4] = child4;
156
157
3.53k
  return (zend_ast *) ast;
158
3.53k
}
159
160
6.85k
static bool zend_ast_is_placeholder_arg(zend_ast *arg) {
161
6.85k
  return arg->kind == ZEND_AST_PLACEHOLDER_ARG
162
6.78k
    || (arg->kind == ZEND_AST_NAMED_ARG
163
52
        && arg->child[1]->kind == ZEND_AST_PLACEHOLDER_ARG);
164
6.85k
}
165
166
#if ZEND_AST_SPEC
167
408
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_0(zend_ast_kind kind) {
168
408
  zend_ast *ast;
169
170
408
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 0);
171
408
  ast = zend_ast_alloc(zend_ast_size(0));
172
408
  ast->kind = kind;
173
408
  ast->attr = 0;
174
408
  ast->lineno = CG(zend_lineno);
175
176
408
  return ast;
177
408
}
178
179
19.9k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_1(zend_ast_kind kind, zend_ast *child) {
180
19.9k
  zend_ast *ast;
181
19.9k
  uint32_t lineno;
182
183
19.9k
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 1);
184
19.9k
  ast = zend_ast_alloc(zend_ast_size(1));
185
19.9k
  ast->kind = kind;
186
19.9k
  ast->attr = 0;
187
19.9k
  ast->child[0] = child;
188
19.9k
  if (child) {
189
19.8k
    lineno = zend_ast_get_lineno(child);
190
19.8k
  } else {
191
44
    lineno = CG(zend_lineno);
192
44
  }
193
19.9k
  ast->lineno = lineno;
194
195
19.9k
  return ast;
196
19.9k
}
197
198
18.9k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_2(zend_ast_kind kind, zend_ast *child1, zend_ast *child2) {
199
18.9k
  zend_ast *ast;
200
18.9k
  uint32_t lineno;
201
202
18.9k
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 2);
203
18.9k
  ast = zend_ast_alloc(zend_ast_size(2));
204
18.9k
  ast->kind = kind;
205
18.9k
  ast->attr = 0;
206
18.9k
  ast->child[0] = child1;
207
18.9k
  ast->child[1] = child2;
208
18.9k
  if (child1) {
209
18.7k
    lineno = zend_ast_get_lineno(child1);
210
18.7k
  } else if (child2) {
211
50
    lineno = zend_ast_get_lineno(child2);
212
90
  } else {
213
90
    lineno = CG(zend_lineno);
214
90
  }
215
18.9k
  ast->lineno = lineno;
216
217
18.9k
  return ast;
218
18.9k
}
219
220
2.31k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_3(zend_ast_kind kind, zend_ast *child1, zend_ast *child2, zend_ast *child3) {
221
2.31k
  zend_ast *ast;
222
2.31k
  uint32_t lineno;
223
224
2.31k
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 3);
225
2.31k
  ast = zend_ast_alloc(zend_ast_size(3));
226
2.31k
  ast->kind = kind;
227
2.31k
  ast->attr = 0;
228
2.31k
  ast->child[0] = child1;
229
2.31k
  ast->child[1] = child2;
230
2.31k
  ast->child[2] = child3;
231
2.31k
  if (child1) {
232
2.00k
    lineno = zend_ast_get_lineno(child1);
233
2.00k
  } else if (child2) {
234
313
    lineno = zend_ast_get_lineno(child2);
235
313
  } else if (child3) {
236
0
    lineno = zend_ast_get_lineno(child3);
237
0
  } else {
238
0
    lineno = CG(zend_lineno);
239
0
  }
240
2.31k
  ast->lineno = lineno;
241
242
2.31k
  return ast;
243
2.31k
}
244
245
1.26k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_4(zend_ast_kind kind, zend_ast *child1, zend_ast *child2, zend_ast *child3, zend_ast *child4) {
246
1.26k
  zend_ast *ast;
247
1.26k
  uint32_t lineno;
248
249
1.26k
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 4);
250
1.26k
  ast = zend_ast_alloc(zend_ast_size(4));
251
1.26k
  ast->kind = kind;
252
1.26k
  ast->attr = 0;
253
1.26k
  ast->child[0] = child1;
254
1.26k
  ast->child[1] = child2;
255
1.26k
  ast->child[2] = child3;
256
1.26k
  ast->child[3] = child4;
257
1.26k
  if (child1) {
258
1.18k
    lineno = zend_ast_get_lineno(child1);
259
1.18k
  } else if (child2) {
260
68
    lineno = zend_ast_get_lineno(child2);
261
68
  } else if (child3) {
262
8
    lineno = zend_ast_get_lineno(child3);
263
8
  } else if (child4) {
264
5
    lineno = zend_ast_get_lineno(child4);
265
5
  } else {
266
1
    lineno = CG(zend_lineno);
267
1
  }
268
1.26k
  ast->lineno = lineno;
269
270
1.26k
  return ast;
271
1.26k
}
272
273
0
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_5(zend_ast_kind kind, zend_ast *child1, zend_ast *child2, zend_ast *child3, zend_ast *child4, zend_ast *child5) {
274
0
  zend_ast *ast;
275
0
  uint32_t lineno;
276
277
0
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 5);
278
0
  ast = zend_ast_alloc(zend_ast_size(5));
279
0
  ast->kind = kind;
280
0
  ast->attr = 0;
281
0
  ast->child[0] = child1;
282
0
  ast->child[1] = child2;
283
0
  ast->child[2] = child3;
284
0
  ast->child[3] = child4;
285
0
  ast->child[4] = child5;
286
0
  if (child1) {
287
0
    lineno = zend_ast_get_lineno(child1);
288
0
  } else if (child2) {
289
0
    lineno = zend_ast_get_lineno(child2);
290
0
  } else if (child3) {
291
0
    lineno = zend_ast_get_lineno(child3);
292
0
  } else if (child4) {
293
0
    lineno = zend_ast_get_lineno(child4);
294
0
  } else if (child5) {
295
0
    lineno = zend_ast_get_lineno(child5);
296
0
  } else {
297
0
    lineno = CG(zend_lineno);
298
0
  }
299
0
  ast->lineno = lineno;
300
301
0
  return ast;
302
0
}
303
304
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_va(
305
846
    zend_ast_kind kind, zend_ast_attr attr, va_list *va) {
306
846
  uint32_t lineno = (uint32_t)-1;
307
846
  uint32_t children = kind >> ZEND_AST_NUM_CHILDREN_SHIFT;
308
846
  zend_ast *ast = zend_ast_alloc(zend_ast_size(children));
309
846
  ast->kind = kind;
310
846
  ast->attr = attr;
311
5.92k
  for (uint32_t i = 0; i < children; i++) {
312
5.07k
    ast->child[i] = va_arg(*va, zend_ast *);
313
5.07k
    if (lineno == (uint32_t)-1 && ast->child[i]) {
314
846
      lineno = zend_ast_get_lineno(ast->child[i]);
315
846
    }
316
5.07k
  }
317
846
  if (lineno == (uint32_t)-1) {
318
0
    lineno = CG(zend_lineno);
319
0
  }
320
846
  ast->lineno = lineno;
321
846
  return ast;
322
846
}
323
324
50
ZEND_API zend_ast * zend_ast_create_n(unsigned kind, ...) {
325
50
  va_list va;
326
50
  va_start(va, kind);
327
50
  zend_ast *ast = zend_ast_create_va(kind, 0, &va);
328
50
  va_end(va);
329
50
  return ast;
330
50
}
331
332
ZEND_API zend_ast * zend_ast_create_ex_n(
333
796
    zend_ast_kind kind, unsigned attr, ...) {
334
796
  va_list va;
335
796
  va_start(va, attr);
336
796
  zend_ast *ast = zend_ast_create_va(kind, attr, &va);
337
796
  va_end(va);
338
796
  return ast;
339
796
}
340
341
15.1k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_0(zend_ast_kind kind) {
342
15.1k
  zend_ast *ast;
343
15.1k
  zend_ast_list *list;
344
345
15.1k
  ast = zend_ast_alloc(zend_ast_list_size(4));
346
15.1k
  list = (zend_ast_list *) ast;
347
15.1k
  list->kind = kind;
348
15.1k
  list->attr = 0;
349
15.1k
  list->lineno = CG(zend_lineno);
350
15.1k
  list->children = 0;
351
352
15.1k
  return ast;
353
15.1k
}
354
355
12.4k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_1(zend_ast_kind kind, zend_ast *child) {
356
12.4k
  zend_ast *ast;
357
12.4k
  zend_ast_list *list;
358
12.4k
  uint32_t lineno;
359
360
12.4k
  ast = zend_ast_alloc(zend_ast_list_size(4));
361
12.4k
  list = (zend_ast_list *) ast;
362
12.4k
  list->kind = kind;
363
12.4k
  list->attr = 0;
364
12.4k
  list->children = 1;
365
12.4k
  list->child[0] = child;
366
12.4k
  if (child) {
367
12.2k
    lineno = zend_ast_get_lineno(child);
368
12.2k
    if (lineno > CG(zend_lineno)) {
369
4
      lineno = CG(zend_lineno);
370
4
    }
371
12.2k
  } else {
372
229
    lineno = CG(zend_lineno);
373
229
  }
374
12.4k
  list->lineno = lineno;
375
376
12.4k
  return ast;
377
12.4k
}
378
379
705
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_2(zend_ast_kind kind, zend_ast *child1, zend_ast *child2) {
380
705
  zend_ast *ast;
381
705
  zend_ast_list *list;
382
705
  uint32_t lineno;
383
384
705
  ast = zend_ast_alloc(zend_ast_list_size(4));
385
705
  list = (zend_ast_list *) ast;
386
705
  list->kind = kind;
387
705
  list->attr = 0;
388
705
  list->children = 2;
389
705
  list->child[0] = child1;
390
705
  list->child[1] = child2;
391
705
  if (child1) {
392
705
    lineno = zend_ast_get_lineno(child1);
393
705
    if (lineno > CG(zend_lineno)) {
394
0
      lineno = CG(zend_lineno);
395
0
    }
396
705
  } else if (child2) {
397
0
    lineno = zend_ast_get_lineno(child2);
398
0
    if (lineno > CG(zend_lineno)) {
399
0
      lineno = CG(zend_lineno);
400
0
    }
401
0
  } else {
402
0
    list->children = 0;
403
0
    lineno = CG(zend_lineno);
404
0
  }
405
705
  list->lineno = lineno;
406
407
705
  return ast;
408
705
}
409
410
1.35k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_arg_list_0(zend_ast_kind kind) {
411
1.35k
  return zend_ast_create_list(0, kind);
412
1.35k
}
413
414
5.31k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_arg_list_1(zend_ast_kind kind, zend_ast *arg) {
415
5.31k
  zend_ast *list = zend_ast_create_list(1, kind, arg);
416
417
5.31k
  if (zend_ast_is_placeholder_arg(arg)) {
418
74
    return zend_ast_create_fcc(list);
419
74
  }
420
421
5.24k
  return list;
422
5.31k
}
423
424
4
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_arg_list_2(zend_ast_kind kind, zend_ast *arg1, zend_ast *arg2) {
425
4
  zend_ast *list = zend_ast_create_list(2, kind, arg1, arg2);
426
427
4
  if (zend_ast_is_placeholder_arg(arg1) || zend_ast_is_placeholder_arg(arg2)) {
428
0
    return zend_ast_create_fcc(list);
429
0
  }
430
431
4
  return list;
432
4
}
433
#else
434
static zend_ast *zend_ast_create_from_va_list(zend_ast_kind kind, zend_ast_attr attr, va_list va) {
435
  uint32_t i, children = kind >> ZEND_AST_NUM_CHILDREN_SHIFT;
436
  zend_ast *ast;
437
438
  ast = zend_ast_alloc(zend_ast_size(children));
439
  ast->kind = kind;
440
  ast->attr = attr;
441
  ast->lineno = (uint32_t) -1;
442
443
  for (i = 0; i < children; ++i) {
444
    ast->child[i] = va_arg(va, zend_ast *);
445
    if (ast->child[i] != NULL) {
446
      uint32_t lineno = zend_ast_get_lineno(ast->child[i]);
447
      if (lineno < ast->lineno) {
448
        ast->lineno = lineno;
449
      }
450
    }
451
  }
452
453
  if (ast->lineno == UINT_MAX) {
454
    ast->lineno = CG(zend_lineno);
455
  }
456
457
  return ast;
458
}
459
460
ZEND_API zend_ast *zend_ast_create_ex(zend_ast_kind kind, zend_ast_attr attr, ...) {
461
  va_list va;
462
  zend_ast *ast;
463
464
  va_start(va, attr);
465
  ast = zend_ast_create_from_va_list(kind, attr, va);
466
  va_end(va);
467
468
  return ast;
469
}
470
471
ZEND_API zend_ast *zend_ast_create(zend_ast_kind kind, ...) {
472
  va_list va;
473
  zend_ast *ast;
474
475
  va_start(va, kind);
476
  ast = zend_ast_create_from_va_list(kind, 0, va);
477
  va_end(va);
478
479
  return ast;
480
}
481
482
ZEND_API zend_ast *zend_ast_create_list(uint32_t init_children, zend_ast_kind kind, ...) {
483
  zend_ast *ast;
484
  zend_ast_list *list;
485
486
  ast = zend_ast_alloc(zend_ast_list_size(4));
487
  list = (zend_ast_list *) ast;
488
  list->kind = kind;
489
  list->attr = 0;
490
  list->lineno = CG(zend_lineno);
491
  list->children = 0;
492
493
  {
494
    va_list va;
495
    uint32_t i;
496
    va_start(va, kind);
497
    for (i = 0; i < init_children; ++i) {
498
      zend_ast *child = va_arg(va, zend_ast *);
499
      ast = zend_ast_list_add(ast, child);
500
      if (child != NULL) {
501
        uint32_t lineno = zend_ast_get_lineno(child);
502
        if (lineno < ast->lineno) {
503
          ast->lineno = lineno;
504
        }
505
      }
506
    }
507
    va_end(va);
508
  }
509
510
  return ast;
511
}
512
513
ZEND_API zend_ast *zend_ast_create_arg_list(uint32_t init_children, zend_ast_kind kind, ...) {
514
  zend_ast *ast;
515
  zend_ast_list *list;
516
  bool has_placeholders = false;
517
518
  ast = zend_ast_alloc(zend_ast_list_size(4));
519
  list = (zend_ast_list *) ast;
520
  list->kind = kind;
521
  list->attr = 0;
522
  list->lineno = CG(zend_lineno);
523
  list->children = 0;
524
525
  {
526
    va_list va;
527
    uint32_t i;
528
    va_start(va, kind);
529
    for (i = 0; i < init_children; ++i) {
530
      zend_ast *child = va_arg(va, zend_ast *);
531
      ast = zend_ast_list_add(ast, child);
532
      uint32_t lineno = zend_ast_get_lineno(child);
533
      if (lineno < ast->lineno) {
534
        ast->lineno = lineno;
535
      }
536
      has_placeholders = has_placeholders || zend_ast_is_placeholder_arg(child);
537
    }
538
    va_end(va);
539
  }
540
541
  if (has_placeholders) {
542
    return zend_ast_create_fcc(list);
543
  }
544
545
  return ast;
546
}
547
#endif
548
549
383
zend_ast *zend_ast_create_concat_op(zend_ast *op0, zend_ast *op1) {
550
383
  if (op0->kind == ZEND_AST_ZVAL && op1->kind == ZEND_AST_ZVAL) {
551
79
    zval *zv0 = zend_ast_get_zval(op0);
552
79
    zval *zv1 = zend_ast_get_zval(op1);
553
79
    if (!zend_binary_op_produces_error(ZEND_CONCAT, zv0, zv1) &&
554
79
        concat_function(zv0, zv0, zv1) == SUCCESS) {
555
79
      zval_ptr_dtor_nogc(zv1);
556
79
      return zend_ast_create_zval(zv0);
557
79
    }
558
79
  }
559
304
  return zend_ast_create_binary_op(ZEND_CONCAT, op0, op1);
560
383
}
561
562
2.65k
static inline bool is_power_of_two(uint32_t n) {
563
2.65k
  return ((n != 0) && (n == (n & (~n + 1))));
564
2.65k
}
565
566
25.9k
ZEND_ATTRIBUTE_NODISCARD ZEND_API zend_ast * ZEND_FASTCALL zend_ast_list_add(zend_ast *ast, zend_ast *op) {
567
25.9k
  zend_ast_list *list = zend_ast_get_list(ast);
568
25.9k
  if (list->children >= 4 && is_power_of_two(list->children)) {
569
1.28k
      list = zend_ast_realloc(list,
570
1.28k
      zend_ast_list_size(list->children), zend_ast_list_size(list->children * 2));
571
1.28k
  }
572
25.9k
  list->child[list->children++] = op;
573
25.9k
  return (zend_ast *) list;
574
25.9k
}
575
576
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_arg_list_add(zend_ast *list, zend_ast *arg)
577
1.53k
{
578
1.53k
  if (list->kind == ZEND_AST_CALLABLE_CONVERT) {
579
4
    zend_ast_fcc *fcc_ast = (zend_ast_fcc*)list;
580
4
    fcc_ast->args = zend_ast_list_add(fcc_ast->args, arg);
581
4
    return (zend_ast*)fcc_ast;
582
4
  }
583
584
1.53k
  ZEND_ASSERT(list->kind == ZEND_AST_ARG_LIST);
585
586
1.53k
  if (zend_ast_is_placeholder_arg(arg)) {
587
2
    return zend_ast_create_fcc(zend_ast_list_add(list, arg));
588
2
  }
589
590
1.53k
  return zend_ast_list_add(list, arg);
591
1.53k
}
592
593
static zend_result zend_ast_add_array_element(const zval *result, zval *offset, zval *expr)
594
11
{
595
11
  if (Z_TYPE_P(offset) == IS_UNDEF) {
596
7
    if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), expr)) {
597
0
      zend_throw_error(NULL,
598
0
        "Cannot add element to the array as the next element is already occupied");
599
0
      return FAILURE;
600
0
    }
601
7
    return SUCCESS;
602
7
  }
603
604
4
  if (array_set_zval_key(Z_ARRVAL_P(result), offset, expr) == FAILURE) {
605
2
    return FAILURE;
606
2
  }
607
608
2
  zval_ptr_dtor_nogc(offset);
609
2
  zval_ptr_dtor_nogc(expr);
610
2
  return SUCCESS;
611
4
}
612
613
2
static zend_result zend_ast_add_unpacked_element(const zval *result, const zval *expr) {
614
2
  if (EXPECTED(Z_TYPE_P(expr) == IS_ARRAY)) {
615
0
    const HashTable *ht = Z_ARRVAL_P(expr);
616
0
    zval *val;
617
0
    zend_string *key;
618
619
0
    ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
620
0
      if (key) {
621
0
        zend_hash_update(Z_ARRVAL_P(result), key, val);
622
0
      } else {
623
0
        if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
624
0
          zend_throw_error(NULL,
625
0
            "Cannot add element to the array as the next element is already occupied");
626
0
          return FAILURE;
627
0
        }
628
0
      }
629
0
      Z_TRY_ADDREF_P(val);
630
0
    } ZEND_HASH_FOREACH_END();
631
0
    return SUCCESS;
632
0
  }
633
634
2
  zend_throw_error(NULL, "Only arrays can be unpacked in constant expression");
635
2
  return FAILURE;
636
2
}
637
638
static zend_class_entry *zend_ast_fetch_class(zend_ast *ast, zend_class_entry *scope)
639
15
{
640
15
  return zend_fetch_class_with_scope(zend_ast_get_str(ast), (ast->attr >> ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT) | ZEND_FETCH_CLASS_EXCEPTION, scope);
641
15
}
642
643
static zend_result ZEND_FASTCALL zend_ast_evaluate_inner(
644
  zval *result,
645
  zend_ast *ast,
646
  zend_class_entry *scope,
647
  bool *short_circuited_ptr,
648
  zend_ast_evaluate_ctx *ctx
649
);
650
651
ZEND_API zend_result ZEND_FASTCALL zend_ast_evaluate_ex(
652
  zval *result,
653
  zend_ast *ast,
654
  zend_class_entry *scope,
655
  bool *short_circuited_ptr,
656
  zend_ast_evaluate_ctx *ctx
657
404
) {
658
404
  zend_string *previous_filename;
659
404
  zend_long previous_lineno;
660
404
  if (scope) {
661
209
    previous_filename = EG(filename_override);
662
209
    previous_lineno = EG(lineno_override);
663
209
    EG(filename_override) = scope->info.user.filename;
664
209
    EG(lineno_override) = zend_ast_get_lineno(ast);
665
209
  }
666
404
  zend_result r = zend_ast_evaluate_inner(result, ast, scope, short_circuited_ptr, ctx);
667
404
  if (scope) {
668
209
    EG(filename_override) = previous_filename;
669
209
    EG(lineno_override) = previous_lineno;
670
209
  }
671
404
  return r;
672
404
}
673
674
static zend_result ZEND_FASTCALL zend_ast_evaluate_inner(
675
  zval *result,
676
  zend_ast *ast,
677
  zend_class_entry *scope,
678
  bool *short_circuited_ptr,
679
  zend_ast_evaluate_ctx *ctx
680
404
) {
681
404
  zval op1, op2;
682
404
  zend_result ret = SUCCESS;
683
404
  bool short_circuited;
684
404
  *short_circuited_ptr = false;
685
686
404
  switch (ast->kind) {
687
19
    case ZEND_AST_BINARY_OP:
688
19
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
689
6
        ret = FAILURE;
690
13
      } else if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
691
0
        zval_ptr_dtor_nogc(&op1);
692
0
        ret = FAILURE;
693
13
      } else {
694
13
        binary_op_type op = get_binary_op(ast->attr);
695
13
        op(result, &op1, &op2);
696
13
        zval_ptr_dtor_nogc(&op1);
697
13
        zval_ptr_dtor_nogc(&op2);
698
13
        ret = EG(exception) ? FAILURE : SUCCESS;
699
13
      }
700
19
      break;
701
2
    case ZEND_AST_GREATER:
702
2
    case ZEND_AST_GREATER_EQUAL:
703
2
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
704
0
        ret = FAILURE;
705
2
      } else if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
706
0
        zval_ptr_dtor_nogc(&op1);
707
0
        ret = FAILURE;
708
2
      } else {
709
        /* op1 > op2 is the same as op2 < op1 */
710
2
        binary_op_type op = ast->kind == ZEND_AST_GREATER
711
2
          ? is_smaller_function : is_smaller_or_equal_function;
712
2
        op(result, &op2, &op1);
713
2
        zval_ptr_dtor_nogc(&op1);
714
2
        zval_ptr_dtor_nogc(&op2);
715
2
        ret = EG(exception) ? FAILURE : SUCCESS;
716
2
      }
717
2
      break;
718
2
    case ZEND_AST_UNARY_OP:
719
2
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
720
1
        ret = FAILURE;
721
1
      } else {
722
1
        unary_op_type op = get_unary_op(ast->attr);
723
1
        ret = op(result, &op1);
724
1
        zval_ptr_dtor_nogc(&op1);
725
1
      }
726
2
      break;
727
127
    case ZEND_AST_ZVAL:
728
127
    {
729
127
      zval *zv = zend_ast_get_zval(ast);
730
731
127
      ZVAL_COPY(result, zv);
732
127
      break;
733
2
    }
734
33
    case ZEND_AST_CONSTANT:
735
33
    {
736
33
      zend_string *name = zend_ast_get_constant_name(ast);
737
33
      zval *zv = zend_get_constant_ex(name, scope, ast->attr);
738
739
33
      if (UNEXPECTED(zv == NULL)) {
740
21
        ZVAL_UNDEF(result);
741
21
        return FAILURE;
742
21
      }
743
12
      ZVAL_COPY_OR_DUP(result, zv);
744
12
      break;
745
33
    }
746
0
    case ZEND_AST_CONSTANT_CLASS:
747
0
      if (scope) {
748
0
        ZVAL_STR_COPY(result, scope->name);
749
0
      } else {
750
0
        ZVAL_EMPTY_STRING(result);
751
0
      }
752
0
      break;
753
1
    case ZEND_AST_CLASS_NAME:
754
1
      if (!scope) {
755
0
        zend_throw_error(NULL, "Cannot use \"self\" when no class scope is active");
756
0
        return FAILURE;
757
0
      }
758
1
      if (ast->attr == ZEND_FETCH_CLASS_SELF) {
759
1
        ZVAL_STR_COPY(result, scope->name);
760
1
      } else if (ast->attr == ZEND_FETCH_CLASS_PARENT) {
761
0
        if (!scope->parent) {
762
0
          zend_throw_error(NULL,
763
0
            "Cannot use \"parent\" when current class scope has no parent");
764
0
          return FAILURE;
765
0
        }
766
0
        ZVAL_STR_COPY(result, scope->parent->name);
767
0
      } else {
768
0
        ZEND_ASSERT(0 && "Should have errored during compilation");
769
0
      }
770
1
      break;
771
3
    case ZEND_AST_AND:
772
3
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
773
0
        ret = FAILURE;
774
0
        break;
775
0
      }
776
3
      if (zend_is_true(&op1)) {
777
3
        if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
778
3
          zval_ptr_dtor_nogc(&op1);
779
3
          ret = FAILURE;
780
3
          break;
781
3
        }
782
0
        ZVAL_BOOL(result, zend_is_true(&op2));
783
0
        zval_ptr_dtor_nogc(&op2);
784
0
      } else {
785
0
        ZVAL_FALSE(result);
786
0
      }
787
0
      zval_ptr_dtor_nogc(&op1);
788
0
      break;
789
0
    case ZEND_AST_CAST:
790
0
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
791
0
        ret = FAILURE;
792
0
        break;
793
0
      }
794
0
      if (ast->attr == Z_TYPE(op1)) {
795
0
        ZVAL_COPY_VALUE(result, &op1);
796
0
      } else {
797
0
        switch (ast->attr) {
798
0
          case _IS_BOOL:
799
0
            ZVAL_BOOL(result, zend_is_true(&op1));
800
0
            break;
801
0
          case IS_LONG:
802
0
            ZVAL_LONG(result, zval_get_long_func(&op1, false));
803
0
            break;
804
0
          case IS_DOUBLE:
805
0
            ZVAL_DOUBLE(result, zval_get_double_func(&op1));
806
0
            break;
807
0
          case IS_STRING:
808
0
            ZVAL_STR(result, zval_get_string_func(&op1));
809
0
            break;
810
0
          case IS_ARRAY:
811
0
            zend_cast_zval_to_array(result, &op1, IS_VAR);
812
0
            break;
813
0
          case IS_OBJECT:
814
0
            zend_cast_zval_to_object(result, &op1, IS_VAR);
815
0
            break;
816
0
          default: ZEND_UNREACHABLE();
817
0
        }
818
0
        zval_ptr_dtor_nogc(&op1);
819
0
        if (UNEXPECTED(EG(exception))) {
820
0
          ret = FAILURE;
821
0
        }
822
0
      }
823
0
      break;
824
2
    case ZEND_AST_OR:
825
2
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
826
2
        ret = FAILURE;
827
2
        break;
828
2
      }
829
0
      if (zend_is_true(&op1)) {
830
0
        ZVAL_TRUE(result);
831
0
      } else {
832
0
        if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
833
0
          zval_ptr_dtor_nogc(&op1);
834
0
          ret = FAILURE;
835
0
          break;
836
0
        }
837
0
        ZVAL_BOOL(result, zend_is_true(&op2));
838
0
        zval_ptr_dtor_nogc(&op2);
839
0
      }
840
0
      zval_ptr_dtor_nogc(&op1);
841
0
      break;
842
0
    case ZEND_AST_CONDITIONAL:
843
0
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
844
0
        ret = FAILURE;
845
0
        break;
846
0
      }
847
0
      if (zend_is_true(&op1)) {
848
0
        if (!ast->child[1]) {
849
0
          *result = op1;
850
0
        } else {
851
0
          if (UNEXPECTED(zend_ast_evaluate_ex(result, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
852
0
            zval_ptr_dtor_nogc(&op1);
853
0
            ret = FAILURE;
854
0
            break;
855
0
          }
856
0
          zval_ptr_dtor_nogc(&op1);
857
0
        }
858
0
      } else {
859
0
        if (UNEXPECTED(zend_ast_evaluate_ex(result, ast->child[2], scope, &short_circuited, ctx) != SUCCESS)) {
860
0
          zval_ptr_dtor_nogc(&op1);
861
0
          ret = FAILURE;
862
0
          break;
863
0
        }
864
0
        zval_ptr_dtor_nogc(&op1);
865
0
      }
866
0
      break;
867
0
    case ZEND_AST_COALESCE:
868
0
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
869
0
        ret = FAILURE;
870
0
        break;
871
0
      }
872
0
      if (Z_TYPE(op1) > IS_NULL) {
873
0
        *result = op1;
874
0
      } else {
875
0
        if (UNEXPECTED(zend_ast_evaluate_ex(result, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
876
0
          zval_ptr_dtor_nogc(&op1);
877
0
          ret = FAILURE;
878
0
          break;
879
0
        }
880
0
        zval_ptr_dtor_nogc(&op1);
881
0
      }
882
0
      break;
883
2
    case ZEND_AST_UNARY_PLUS:
884
2
      if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
885
0
        ret = FAILURE;
886
2
      } else {
887
2
        ZVAL_LONG(&op1, 0);
888
2
        ret = add_function(result, &op1, &op2);
889
2
        zval_ptr_dtor_nogc(&op2);
890
2
      }
891
2
      break;
892
2
    case ZEND_AST_UNARY_MINUS:
893
2
      if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
894
1
        ret = FAILURE;
895
1
      } else {
896
1
        ZVAL_LONG(&op1, -1);
897
1
        ret = mul_function(result, &op1, &op2);
898
1
        zval_ptr_dtor_nogc(&op2);
899
1
      }
900
2
      break;
901
24
    case ZEND_AST_ARRAY:
902
24
      {
903
24
        uint32_t i;
904
24
        zend_ast_list *list = zend_ast_get_list(ast);
905
906
24
        if (!list->children) {
907
0
          ZVAL_EMPTY_ARRAY(result);
908
0
          break;
909
0
        }
910
24
        array_init(result);
911
33
        for (i = 0; i < list->children; i++) {
912
30
          zend_ast *elem = list->child[i];
913
30
          if (elem->kind == ZEND_AST_UNPACK) {
914
4
            if (UNEXPECTED(zend_ast_evaluate_ex(&op1, elem->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
915
2
              zval_ptr_dtor_nogc(result);
916
2
              return FAILURE;
917
2
            }
918
2
            if (UNEXPECTED(zend_ast_add_unpacked_element(result, &op1) != SUCCESS)) {
919
2
              zval_ptr_dtor_nogc(&op1);
920
2
              zval_ptr_dtor_nogc(result);
921
2
              return FAILURE;
922
2
            }
923
0
            zval_ptr_dtor_nogc(&op1);
924
0
            continue;
925
2
          }
926
26
          if (elem->child[1]) {
927
13
            if (UNEXPECTED(zend_ast_evaluate_ex(&op1, elem->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
928
5
              zval_ptr_dtor_nogc(result);
929
5
              return FAILURE;
930
5
            }
931
13
          } else {
932
13
            ZVAL_UNDEF(&op1);
933
13
          }
934
21
          if (UNEXPECTED(zend_ast_evaluate_ex(&op2, elem->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
935
10
            zval_ptr_dtor_nogc(&op1);
936
10
            zval_ptr_dtor_nogc(result);
937
10
            return FAILURE;
938
10
          }
939
11
          if (UNEXPECTED(zend_ast_add_array_element(result, &op1, &op2) != SUCCESS)) {
940
2
            zval_ptr_dtor_nogc(&op1);
941
2
            zval_ptr_dtor_nogc(&op2);
942
2
            zval_ptr_dtor_nogc(result);
943
2
            return FAILURE;
944
2
          }
945
11
        }
946
24
      }
947
3
      break;
948
8
    case ZEND_AST_DIM:
949
8
      if (ast->child[1] == NULL) {
950
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
951
0
      }
952
953
8
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
954
1
        ret = FAILURE;
955
1
        break;
956
1
      }
957
7
      if (short_circuited) {
958
0
        *short_circuited_ptr = true;
959
0
        ZVAL_NULL(result);
960
0
        return SUCCESS;
961
0
      }
962
963
      // DIM on objects is disallowed because it allows executing arbitrary expressions
964
7
      if (Z_TYPE(op1) == IS_OBJECT) {
965
0
        zval_ptr_dtor_nogc(&op1);
966
0
        zend_throw_error(NULL, "Cannot use [] on objects in constant expression");
967
0
        ret = FAILURE;
968
0
        break;
969
0
      }
970
971
7
      if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
972
1
        zval_ptr_dtor_nogc(&op1);
973
1
        ret = FAILURE;
974
1
        break;
975
1
      }
976
977
6
      zend_fetch_dimension_const(result, &op1, &op2, (ast->attr & ZEND_DIM_IS) ? BP_VAR_IS : BP_VAR_R);
978
979
6
      zval_ptr_dtor_nogc(&op1);
980
6
      zval_ptr_dtor_nogc(&op2);
981
6
      if (UNEXPECTED(EG(exception))) {
982
1
        return FAILURE;
983
1
      }
984
985
5
      break;
986
87
    case ZEND_AST_CONST_ENUM_INIT:
987
87
    {
988
      // Preloading will attempt to resolve constants but objects can't be stored in shm
989
      // Aborting here to store the const AST instead
990
87
      if (CG(in_compilation)) {
991
0
        return FAILURE;
992
0
      }
993
994
87
      zend_ast *class_name_ast = ast->child[0];
995
87
      zend_string *class_name = zend_ast_get_str(class_name_ast);
996
997
87
      zend_ast *case_id_ast = ast->child[1];
998
87
      int case_id = (int)Z_LVAL_P(zend_ast_get_zval(case_id_ast));
999
1000
87
      zend_ast *case_name_ast = ast->child[2];
1001
87
      zend_string *case_name = zend_ast_get_str(case_name_ast);
1002
1003
87
      zend_ast *case_value_ast = ast->child[3];
1004
1005
87
      zval case_value_zv;
1006
87
      ZVAL_UNDEF(&case_value_zv);
1007
87
      if (case_value_ast != NULL) {
1008
30
        if (UNEXPECTED(zend_ast_evaluate_ex(&case_value_zv, case_value_ast, scope, &short_circuited, ctx) != SUCCESS)) {
1009
2
          return FAILURE;
1010
2
        }
1011
30
      }
1012
1013
85
      zend_class_entry *ce = zend_lookup_class(class_name);
1014
85
      zend_enum_new(result, ce, case_id, case_name, case_value_ast != NULL ? &case_value_zv : NULL);
1015
85
      zval_ptr_dtor_nogc(&case_value_zv);
1016
85
      break;
1017
87
    }
1018
45
    case ZEND_AST_CLASS_CONST:
1019
45
    {
1020
45
      zend_string *class_name = zend_ast_get_str(ast->child[0]);
1021
45
      if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
1022
3
        return FAILURE;
1023
3
      }
1024
42
      if (UNEXPECTED(Z_TYPE(op2) != IS_STRING)) {
1025
0
        zend_invalid_class_constant_type_error(Z_TYPE(op2));
1026
0
        zval_ptr_dtor_nogc(&op2);
1027
0
        return FAILURE;
1028
0
      }
1029
42
      zend_string *const_name = Z_STR(op2);
1030
1031
42
      zend_string *previous_filename;
1032
42
      zend_long previous_lineno;
1033
42
      if (scope) {
1034
31
        previous_filename = EG(filename_override);
1035
31
        previous_lineno = EG(lineno_override);
1036
31
        EG(filename_override) = scope->info.user.filename;
1037
31
        EG(lineno_override) = zend_ast_get_lineno(ast);
1038
31
      }
1039
42
      zval *zv = zend_get_class_constant_ex(class_name, const_name, scope, ast->attr);
1040
42
      if (scope) {
1041
31
        EG(filename_override) = previous_filename;
1042
31
        EG(lineno_override) = previous_lineno;
1043
31
      }
1044
1045
42
      if (UNEXPECTED(zv == NULL)) {
1046
14
        ZVAL_UNDEF(result);
1047
14
        zval_ptr_dtor_nogc(&op2);
1048
14
        return FAILURE;
1049
14
      }
1050
28
      ZVAL_COPY_OR_DUP(result, zv);
1051
28
      zval_ptr_dtor_nogc(&op2);
1052
28
      break;
1053
42
    }
1054
12
    case ZEND_AST_NEW:
1055
12
    {
1056
12
      zend_class_entry *ce = zend_ast_fetch_class(ast->child[0], scope);
1057
12
      if (!ce) {
1058
2
        return FAILURE;
1059
2
      }
1060
1061
10
      if (object_init_ex(result, ce) != SUCCESS) {
1062
0
        return FAILURE;
1063
0
      }
1064
1065
      /* Even if there is no constructor, the object can have cause side-effects in various ways (__toString(), __get(), __isset(), etc). */
1066
10
      ctx->had_side_effects = true;
1067
1068
10
      zend_ast_list *args_ast = zend_ast_get_list(ast->child[1]);
1069
10
      if (args_ast->attr) {
1070
        /* Has named arguments. */
1071
0
        HashTable *args = zend_new_array(args_ast->children);
1072
0
        for (uint32_t i = 0; i < args_ast->children; i++) {
1073
0
          zend_ast *arg_ast = args_ast->child[i];
1074
0
          zend_string *name = NULL;
1075
0
          zval arg;
1076
0
          if (arg_ast->kind == ZEND_AST_NAMED_ARG) {
1077
0
            name = zend_ast_get_str(arg_ast->child[0]);
1078
0
            arg_ast = arg_ast->child[1];
1079
0
          }
1080
0
          if (zend_ast_evaluate_ex(&arg, arg_ast, scope, &short_circuited, ctx) == FAILURE) {
1081
0
            zend_array_destroy(args);
1082
0
            zval_ptr_dtor(result);
1083
0
            return FAILURE;
1084
0
          }
1085
0
          if (name) {
1086
0
            if (!zend_hash_add(args, name, &arg)) {
1087
0
              zend_throw_error(NULL,
1088
0
                "Named parameter $%s overwrites previous argument",
1089
0
                ZSTR_VAL(name));
1090
0
              zend_array_destroy(args);
1091
0
              zval_ptr_dtor(result);
1092
0
              return FAILURE;
1093
0
            }
1094
0
          } else {
1095
0
            zend_hash_next_index_insert(args, &arg);
1096
0
          }
1097
0
        }
1098
1099
0
        zend_function *ctor = Z_OBJ_HT_P(result)->get_constructor(Z_OBJ_P(result));
1100
0
        if (ctor) {
1101
0
          zend_call_known_function(
1102
0
            ctor, Z_OBJ_P(result), Z_OBJCE_P(result), NULL, 0, NULL, args);
1103
0
        }
1104
1105
0
        zend_array_destroy(args);
1106
10
      } else {
1107
10
        ALLOCA_FLAG(use_heap)
1108
10
        zval *args = do_alloca(sizeof(zval) * args_ast->children, use_heap);
1109
10
        for (uint32_t i = 0; i < args_ast->children; i++) {
1110
0
          if (zend_ast_evaluate_ex(&args[i], args_ast->child[i], scope, &short_circuited, ctx) == FAILURE) {
1111
0
            for (uint32_t j = 0; j < i; j++) {
1112
0
              zval_ptr_dtor(&args[j]);
1113
0
            }
1114
0
            free_alloca(args, use_heap);
1115
0
            zval_ptr_dtor(result);
1116
0
            return FAILURE;
1117
0
          }
1118
0
        }
1119
1120
10
        zend_function *ctor = Z_OBJ_HT_P(result)->get_constructor(Z_OBJ_P(result));
1121
10
        if (ctor) {
1122
2
          zend_call_known_instance_method(
1123
2
            ctor, Z_OBJ_P(result), NULL, args_ast->children, args);
1124
2
        }
1125
1126
10
        for (uint32_t i = 0; i < args_ast->children; i++) {
1127
0
          zval_ptr_dtor(&args[i]);
1128
0
        }
1129
10
        free_alloca(args, use_heap);
1130
10
      }
1131
1132
10
      if (EG(exception)) {
1133
0
        zend_object_store_ctor_failed(Z_OBJ_P(result));
1134
0
        zval_ptr_dtor(result);
1135
0
        return FAILURE;
1136
0
      }
1137
10
      return SUCCESS;
1138
10
    }
1139
15
    case ZEND_AST_CALL:
1140
18
    case ZEND_AST_STATIC_CALL:
1141
18
    {
1142
18
      zend_function *fptr;
1143
18
      zend_class_entry *called_scope = NULL;
1144
1145
18
      zend_ast *args_ast = zend_ast_call_get_args(ast);
1146
18
      ZEND_ASSERT(args_ast->kind == ZEND_AST_CALLABLE_CONVERT);
1147
1148
18
      zend_ast_fcc *fcc_ast = (zend_ast_fcc*)args_ast;
1149
1150
18
      zend_ast_list *args = zend_ast_get_list(fcc_ast->args);
1151
18
      ZEND_ASSERT(args->children > 0);
1152
18
      if (args->children != 1 || args->child[0]->attr != ZEND_PLACEHOLDER_VARIADIC) {
1153
        /* TODO: PFAs */
1154
0
        zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
1155
0
      }
1156
1157
18
      switch (ast->kind) {
1158
15
        case ZEND_AST_CALL: {
1159
15
          fptr = ZEND_MAP_PTR_GET(fcc_ast->fptr);
1160
1161
15
          if (!fptr) {
1162
15
            zend_string *function_name = zend_ast_get_str(ast->child[0]);
1163
15
            zend_string *function_name_lc = zend_string_tolower(function_name);
1164
15
            fptr = zend_fetch_function(function_name_lc);
1165
15
            if (!fptr && ast->child[0]->attr != ZEND_NAME_FQ) {
1166
2
              const char *backslash = zend_memrchr(ZSTR_VAL(function_name_lc), '\\', ZSTR_LEN(function_name_lc));
1167
2
              if (backslash) {
1168
1
                fptr = zend_fetch_function_str(backslash + 1, ZSTR_LEN(function_name_lc) - (backslash - ZSTR_VAL(function_name_lc) + 1));
1169
1
              }
1170
2
            }
1171
15
            zend_string_release(function_name_lc);
1172
15
            if (!fptr) {
1173
4
              zend_throw_error(NULL, "Call to undefined function %s()", ZSTR_VAL(function_name));
1174
4
              return FAILURE;
1175
4
            }
1176
1177
11
            ZEND_MAP_PTR_SET(fcc_ast->fptr, fptr);
1178
11
          }
1179
1180
11
          break;
1181
15
        }
1182
11
        case ZEND_AST_STATIC_CALL: {
1183
3
          zend_class_entry *ce = zend_ast_fetch_class(ast->child[0], scope);
1184
3
          if (!ce) {
1185
1
            return FAILURE;
1186
1
          }
1187
2
          called_scope = ce;
1188
1189
2
          fptr = ZEND_MAP_PTR_GET(fcc_ast->fptr);
1190
1191
2
          if (!fptr) {
1192
2
            zend_string *method_name = zend_ast_get_str(ast->child[1]);
1193
2
            if (ce->get_static_method) {
1194
0
              fptr = ce->get_static_method(ce, method_name);
1195
2
            } else {
1196
2
              fptr = zend_hash_find_ptr_lc(&ce->function_table, method_name);
1197
2
              if (fptr) {
1198
1
                if (!zend_check_method_accessible(fptr, scope)) {
1199
0
                  if (ce->__callstatic) {
1200
0
                    zend_throw_error(NULL, "Creating a callable for the magic __callStatic() method is not supported in constant expressions");
1201
0
                  } else {
1202
0
                    zend_bad_method_call(fptr, method_name, scope);
1203
0
                  }
1204
1205
0
                  return FAILURE;
1206
0
                }
1207
1
              } else {
1208
1
                if (ce->__callstatic) {
1209
0
                  zend_throw_error(NULL, "Creating a callable for the magic __callStatic() method is not supported in constant expressions");
1210
1
                } else {
1211
1
                  zend_undefined_method(ce, method_name);
1212
1
                }
1213
1214
1
                return FAILURE;
1215
1
              }
1216
2
            }
1217
1218
1
            if (!(fptr->common.fn_flags & ZEND_ACC_STATIC)) {
1219
0
              zend_non_static_method_call(fptr);
1220
1221
0
              return FAILURE;
1222
0
            }
1223
1
            if ((fptr->common.fn_flags & ZEND_ACC_ABSTRACT)) {
1224
1
              zend_abstract_method_call(fptr);
1225
1226
1
              return FAILURE;
1227
1
            } else if (fptr->common.scope->ce_flags & ZEND_ACC_TRAIT) {
1228
0
              zend_error(E_DEPRECATED,
1229
0
                "Calling static trait method %s::%s is deprecated, "
1230
0
                "it should only be called on a class using the trait",
1231
0
                ZSTR_VAL(fptr->common.scope->name), ZSTR_VAL(fptr->common.function_name));
1232
0
              if (EG(exception)) {
1233
0
                return FAILURE;
1234
0
              }
1235
0
            }
1236
1237
0
            ZEND_MAP_PTR_SET(fcc_ast->fptr, fptr);
1238
0
          }
1239
1240
0
          break;
1241
2
        }
1242
0
        default: ZEND_UNREACHABLE();
1243
18
      }
1244
1245
11
      zend_create_fake_closure(result, fptr, fptr->common.scope, called_scope, NULL);
1246
1247
11
      return SUCCESS;
1248
18
    }
1249
2
    case ZEND_AST_OP_ARRAY:
1250
2
    {
1251
      // Preloading will attempt to resolve constants but objects can't be stored in shm
1252
      // Aborting here to store the const AST instead
1253
2
      if (CG(in_compilation)) {
1254
0
        return FAILURE;
1255
0
      }
1256
1257
2
      zend_function *func = (zend_function *)zend_ast_get_op_array(ast)->op_array;
1258
1259
2
      zend_create_closure(result, func, scope, scope, NULL);
1260
2
      return SUCCESS;
1261
2
    }
1262
11
    case ZEND_AST_PROP:
1263
15
    case ZEND_AST_NULLSAFE_PROP:
1264
15
    {
1265
15
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
1266
1
        return FAILURE;
1267
1
      }
1268
14
      if (short_circuited) {
1269
0
        *short_circuited_ptr = true;
1270
0
        ZVAL_NULL(result);
1271
0
        return SUCCESS;
1272
0
      }
1273
14
      if (ast->kind == ZEND_AST_NULLSAFE_PROP && Z_TYPE(op1) == IS_NULL) {
1274
1
        *short_circuited_ptr = true;
1275
1
        ZVAL_NULL(result);
1276
1
        return SUCCESS;
1277
1
      }
1278
1279
13
      if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
1280
1
        zval_ptr_dtor_nogc(&op1);
1281
1
        return FAILURE;
1282
1
      }
1283
1284
12
      if (!try_convert_to_string(&op2)) {
1285
1
        zval_ptr_dtor_nogc(&op1);
1286
1
        zval_ptr_dtor_nogc(&op2);
1287
1
        return FAILURE;
1288
1
      }
1289
1290
11
      if (Z_TYPE(op1) != IS_OBJECT) {
1291
5
        zend_wrong_property_read(&op1, &op2);
1292
1293
5
        zval_ptr_dtor_nogc(&op1);
1294
5
        zval_ptr_dtor_nogc(&op2);
1295
1296
5
        ZVAL_NULL(result);
1297
5
        return SUCCESS;
1298
5
      }
1299
1300
6
      zend_object *zobj = Z_OBJ(op1);
1301
6
      if (!(zobj->ce->ce_flags & ZEND_ACC_ENUM)) {
1302
2
        zend_throw_error(NULL, "Fetching properties on non-enums in constant expressions is not allowed");
1303
2
        zval_ptr_dtor_nogc(&op1);
1304
2
        zval_ptr_dtor_nogc(&op2);
1305
2
        return FAILURE;
1306
2
      }
1307
1308
4
      zend_string *name = Z_STR(op2);
1309
4
      zval *property_result = zend_read_property_ex(scope, zobj, name, 0, result);
1310
4
      if (EG(exception)) {
1311
0
        zval_ptr_dtor_nogc(&op1);
1312
0
        zval_ptr_dtor_nogc(&op2);
1313
0
        return FAILURE;
1314
0
      }
1315
1316
4
      if (result != property_result) {
1317
4
        ZVAL_COPY(result, property_result);
1318
4
      }
1319
4
      zval_ptr_dtor_nogc(&op1);
1320
4
      zval_ptr_dtor_nogc(&op2);
1321
4
      return SUCCESS;
1322
4
    }
1323
0
    default:
1324
0
      zend_throw_error(NULL, "Unsupported constant expression");
1325
0
      ret = FAILURE;
1326
404
  }
1327
295
  return ret;
1328
404
}
1329
1330
ZEND_API zend_result ZEND_FASTCALL zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope)
1331
0
{
1332
0
  zend_ast_evaluate_ctx ctx = {0};
1333
0
  bool short_circuited;
1334
0
  return zend_ast_evaluate_ex(result, ast, scope, &short_circuited, &ctx);
1335
0
}
1336
1337
static size_t ZEND_FASTCALL zend_ast_tree_size(zend_ast *ast)
1338
1.17k
{
1339
1.17k
  size_t size;
1340
1341
1.17k
  if (ast->kind == ZEND_AST_ZVAL || ast->kind == ZEND_AST_CONSTANT) {
1342
721
    size = sizeof(zend_ast_zval);
1343
721
  } else if (ast->kind == ZEND_AST_OP_ARRAY) {
1344
3
    size = sizeof(zend_ast_op_array);
1345
447
  } else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) {
1346
20
    zend_ast *args_ast = ((zend_ast_fcc*)ast)->args;
1347
20
    size = sizeof(zend_ast_fcc) + zend_ast_tree_size(args_ast);
1348
427
  } else if (zend_ast_is_list(ast)) {
1349
69
    uint32_t i;
1350
69
    const zend_ast_list *list = zend_ast_get_list(ast);
1351
1352
69
    size = zend_ast_list_size(list->children);
1353
142
    for (i = 0; i < list->children; i++) {
1354
73
      if (list->child[i]) {
1355
73
        size += zend_ast_tree_size(list->child[i]);
1356
73
      }
1357
73
    }
1358
358
  } else if (zend_ast_is_decl(ast)) {
1359
    /* Not implemented. */
1360
0
    ZEND_UNREACHABLE();
1361
358
  } else {
1362
358
    uint32_t i, children = zend_ast_get_num_children(ast);
1363
1364
358
    size = zend_ast_size(children);
1365
1.26k
    for (i = 0; i < children; i++) {
1366
902
      if (ast->child[i]) {
1367
781
        size += zend_ast_tree_size(ast->child[i]);
1368
781
      }
1369
902
    }
1370
358
  }
1371
1.17k
  return size;
1372
1.17k
}
1373
1374
static void* ZEND_FASTCALL zend_ast_tree_copy(zend_ast *ast, void *buf)
1375
1.17k
{
1376
1.17k
  if (ast->kind == ZEND_AST_ZVAL) {
1377
624
    zend_ast_zval *new = (zend_ast_zval*)buf;
1378
624
    new->kind = ZEND_AST_ZVAL;
1379
624
    new->attr = ast->attr;
1380
624
    ZVAL_COPY(&new->val, zend_ast_get_zval(ast));
1381
624
    Z_LINENO(new->val) = zend_ast_get_lineno(ast);
1382
624
    buf = (void*)((char*)buf + sizeof(zend_ast_zval));
1383
624
  } else if (ast->kind == ZEND_AST_CONSTANT) {
1384
97
    zend_ast_zval *new = (zend_ast_zval*)buf;
1385
97
    new->kind = ZEND_AST_CONSTANT;
1386
97
    new->attr = ast->attr;
1387
97
    ZVAL_STR_COPY(&new->val, zend_ast_get_constant_name(ast));
1388
97
    Z_LINENO(new->val) = zend_ast_get_lineno(ast);
1389
97
    buf = (void*)((char*)buf + sizeof(zend_ast_zval));
1390
450
  } else if (zend_ast_is_list(ast)) {
1391
69
    const zend_ast_list *list = zend_ast_get_list(ast);
1392
69
    zend_ast_list *new = (zend_ast_list*)buf;
1393
69
    uint32_t i;
1394
69
    new->kind = list->kind;
1395
69
    new->attr = list->attr;
1396
69
    new->children = list->children;
1397
69
    new->lineno = list->lineno;
1398
69
    buf = (void*)((char*)buf + zend_ast_list_size(list->children));
1399
142
    for (i = 0; i < list->children; i++) {
1400
73
      if (list->child[i]) {
1401
73
        new->child[i] = (zend_ast*)buf;
1402
73
        buf = zend_ast_tree_copy(list->child[i], buf);
1403
73
      } else {
1404
0
        new->child[i] = NULL;
1405
0
      }
1406
73
    }
1407
381
  } else if (ast->kind == ZEND_AST_OP_ARRAY) {
1408
3
    const zend_ast_op_array *old = zend_ast_get_op_array(ast);
1409
3
    zend_ast_op_array *new = (zend_ast_op_array*)buf;
1410
3
    new->kind = old->kind;
1411
3
    new->attr = old->attr;
1412
3
    new->lineno = old->lineno;
1413
3
    new->op_array = old->op_array;
1414
3
    function_add_ref((zend_function *)new->op_array);
1415
3
    buf = (void*)((char*)buf + sizeof(zend_ast_op_array));
1416
378
  } else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) {
1417
20
    const zend_ast_fcc *old = (zend_ast_fcc*)ast;
1418
20
    zend_ast_fcc *new = (zend_ast_fcc*)buf;
1419
20
    new->kind = old->kind;
1420
20
    new->attr = old->attr;
1421
20
    new->lineno = old->lineno;
1422
20
    ZEND_MAP_PTR_INIT(new->fptr, ZEND_MAP_PTR(old->fptr));
1423
20
    buf = (void*)((char*)buf + sizeof(zend_ast_fcc));
1424
20
    new->args = buf;
1425
20
    buf = zend_ast_tree_copy(old->args, buf);
1426
358
  } else if (zend_ast_is_decl(ast)) {
1427
    /* Not implemented. */
1428
0
    ZEND_UNREACHABLE();
1429
358
  } else {
1430
358
    uint32_t i, children = zend_ast_get_num_children(ast);
1431
358
    zend_ast *new = (zend_ast*)buf;
1432
358
    new->kind = ast->kind;
1433
358
    new->attr = ast->attr;
1434
358
    new->lineno = ast->lineno;
1435
358
    buf = (void*)((char*)buf + zend_ast_size(children));
1436
1.26k
    for (i = 0; i < children; i++) {
1437
902
      if (ast->child[i]) {
1438
781
        new->child[i] = (zend_ast*)buf;
1439
781
        buf = zend_ast_tree_copy(ast->child[i], buf);
1440
781
      } else {
1441
121
        new->child[i] = NULL;
1442
121
      }
1443
902
    }
1444
358
  }
1445
1.17k
  return buf;
1446
1.17k
}
1447
1448
ZEND_API zend_ast_ref * ZEND_FASTCALL zend_ast_copy(zend_ast *ast)
1449
297
{
1450
297
  size_t tree_size;
1451
297
  zend_ast_ref *ref;
1452
1453
297
  ZEND_ASSERT(ast != NULL);
1454
297
  tree_size = zend_ast_tree_size(ast) + sizeof(zend_ast_ref);
1455
297
  ref = emalloc(tree_size);
1456
297
  zend_ast_tree_copy(ast, GC_AST(ref));
1457
297
  GC_SET_REFCOUNT(ref, 1);
1458
297
  GC_TYPE_INFO(ref) = GC_CONSTANT_AST;
1459
297
  return ref;
1460
297
}
1461
1462
ZEND_API void ZEND_FASTCALL zend_ast_destroy(zend_ast *ast)
1463
70.9k
{
1464
134k
tail_call:
1465
134k
  if (!ast) {
1466
23.4k
    return;
1467
23.4k
  }
1468
1469
111k
  if (EXPECTED(ast->kind >= ZEND_AST_VAR)) {
1470
41.5k
    uint32_t i, children = zend_ast_get_num_children(ast);
1471
1472
71.0k
    for (i = 1; i < children; i++) {
1473
29.4k
      zend_ast_destroy(ast->child[i]);
1474
29.4k
    }
1475
41.5k
    ast = ast->child[0];
1476
41.5k
    goto tail_call;
1477
69.6k
  } else if (EXPECTED(ast->kind == ZEND_AST_ZVAL)) {
1478
41.3k
    zval_ptr_dtor_nogc(zend_ast_get_zval(ast));
1479
41.3k
  } else if (EXPECTED(zend_ast_is_list(ast))) {
1480
24.8k
    const zend_ast_list *list = zend_ast_get_list(ast);
1481
24.8k
    if (list->children) {
1482
19.2k
      uint32_t i;
1483
1484
35.4k
      for (i = 1; i < list->children; i++) {
1485
16.2k
        zend_ast_destroy(list->child[i]);
1486
16.2k
      }
1487
19.2k
      ast = list->child[0];
1488
19.2k
      goto tail_call;
1489
19.2k
    }
1490
24.8k
  } else if (EXPECTED(ast->kind == ZEND_AST_CONSTANT)) {
1491
191
    zend_string_release_ex(zend_ast_get_constant_name(ast), 0);
1492
3.23k
  } else if (EXPECTED(ast->kind == ZEND_AST_OP_ARRAY)) {
1493
6
    destroy_op_array(zend_ast_get_op_array(ast)->op_array);
1494
3.22k
  } else if (EXPECTED(zend_ast_is_decl(ast))) {
1495
2.79k
    const zend_ast_decl *decl = (const zend_ast_decl *) ast;
1496
1497
2.79k
    if (decl->name) {
1498
2.47k
        zend_string_release_ex(decl->name, 0);
1499
2.47k
    }
1500
2.79k
    if (decl->doc_comment) {
1501
2
      zend_string_release_ex(decl->doc_comment, 0);
1502
2
    }
1503
2.79k
    zend_ast_destroy(decl->child[0]);
1504
2.79k
    zend_ast_destroy(decl->child[1]);
1505
2.79k
    zend_ast_destroy(decl->child[2]);
1506
2.79k
    zend_ast_destroy(decl->child[3]);
1507
2.79k
    ast = decl->child[4];
1508
2.79k
    goto tail_call;
1509
2.79k
  } else if (EXPECTED(ast->kind == ZEND_AST_CALLABLE_CONVERT)) {
1510
81
    zend_ast_fcc *fcc_ast = (zend_ast_fcc*) ast;
1511
1512
81
    ast = fcc_ast->args;
1513
81
    goto tail_call;
1514
81
  }
1515
111k
}
1516
1517
ZEND_API void ZEND_FASTCALL zend_ast_ref_destroy(zend_ast_ref *ast)
1518
293
{
1519
293
  zend_ast_destroy(GC_AST(ast));
1520
293
  efree(ast);
1521
293
}
1522
1523
736
ZEND_API void zend_ast_apply(zend_ast *ast, zend_ast_apply_func fn, void *context) {
1524
736
  if (zend_ast_is_list(ast)) {
1525
157
    zend_ast_list *list = zend_ast_get_list(ast);
1526
157
    uint32_t i;
1527
256
    for (i = 0; i < list->children; ++i) {
1528
99
      fn(&list->child[i], context);
1529
99
    }
1530
579
  } else if (zend_ast_is_decl(ast)) {
1531
    /* Not implemented. */
1532
0
    ZEND_UNREACHABLE();
1533
579
  } else {
1534
579
    uint32_t i, children = zend_ast_get_num_children(ast);
1535
1.75k
    for (i = 0; i < children; ++i) {
1536
1.17k
      fn(&ast->child[i], context);
1537
1.17k
    }
1538
579
  }
1539
736
}
1540
1541
/*
1542
 * Operator Precedence
1543
 * ====================
1544
 * priority  associativity  operators
1545
 * ----------------------------------
1546
 *   10     left            include, include_once, eval, require, require_once
1547
 *   20     left            ,
1548
 *   30     left            or
1549
 *   40     left            xor
1550
 *   50     left            and
1551
 *   60     right           print
1552
 *   70     right           yield
1553
 *   80     right           =>
1554
 *   85     right           yield from
1555
 *   90     right           = += -= *= /= .= %= &= |= ^= <<= >>= **=
1556
 *  100     left            ? :
1557
 *  110     right           ??
1558
 *  120     left            ||
1559
 *  130     left            &&
1560
 *  140     left            |
1561
 *  150     left            ^
1562
 *  160     left            &
1563
 *  170     non-associative == != === !==
1564
 *  180     non-associative < <= > >= <=>
1565
 *  185     left            .
1566
 *  190     left            << >>
1567
 *  200     left            + -
1568
 *  210     left            * / %
1569
 *  220     right           !
1570
 *  230     non-associative instanceof
1571
 *  240     right           + - ++ -- ~ (type) @
1572
 *  250     right           **
1573
 *  260     left            [
1574
 *  270     non-associative clone new
1575
 */
1576
1577
static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int priority, int indent);
1578
1579
static ZEND_COLD void zend_ast_export_str(smart_str *str, const zend_string *s)
1580
11
{
1581
11
  size_t i;
1582
1583
428
  for (i = 0; i < ZSTR_LEN(s); i++) {
1584
417
    unsigned char c = ZSTR_VAL(s)[i];
1585
417
    if (c == '\'' || c == '\\') {
1586
37
      smart_str_appendc(str, '\\');
1587
37
      smart_str_appendc(str, c);
1588
380
    } else {
1589
380
      smart_str_appendc(str, c);
1590
380
    }
1591
417
  }
1592
11
}
1593
1594
static ZEND_COLD void zend_ast_export_qstr(smart_str *str, char quote, const zend_string *s)
1595
82
{
1596
82
  size_t i;
1597
1598
3.36k
  for (i = 0; i < ZSTR_LEN(s); i++) {
1599
3.27k
    unsigned char c = ZSTR_VAL(s)[i];
1600
3.27k
    if (c < ' ') {
1601
1.16k
      switch (c) {
1602
65
        case '\n':
1603
65
          smart_str_appends(str, "\\n");
1604
65
          break;
1605
1
        case '\r':
1606
1
          smart_str_appends(str, "\\r");
1607
1
          break;
1608
21
        case '\t':
1609
21
          smart_str_appends(str, "\\t");
1610
21
          break;
1611
31
        case '\f':
1612
31
          smart_str_appends(str, "\\f");
1613
31
          break;
1614
204
        case '\v':
1615
204
          smart_str_appends(str, "\\v");
1616
204
          break;
1617
#ifdef ZEND_WIN32
1618
        case VK_ESCAPE:
1619
#else
1620
4
        case '\e':
1621
4
#endif
1622
4
          smart_str_appends(str, "\\e");
1623
4
          break;
1624
834
        default:
1625
834
          smart_str_appends(str, "\\0");
1626
834
          smart_str_appendc(str, '0' + (c / 8));
1627
834
          smart_str_appendc(str, '0' + (c % 8));
1628
834
          break;
1629
1.16k
      }
1630
2.11k
    } else {
1631
2.11k
      if (c == quote || c == '$' || c == '\\') {
1632
29
        smart_str_appendc(str, '\\');
1633
29
      }
1634
2.11k
      smart_str_appendc(str, c);
1635
2.11k
    }
1636
3.27k
  }
1637
82
}
1638
1639
static ZEND_COLD void zend_ast_export_indent(smart_str *str, int indent)
1640
24
{
1641
36
  while (indent > 0) {
1642
12
    smart_str_appends(str, "    ");
1643
12
    indent--;
1644
12
  }
1645
24
}
1646
1647
static ZEND_COLD void zend_ast_export_name(smart_str *str, zend_ast *ast, int priority, int indent)
1648
3
{
1649
3
  if (ast->kind == ZEND_AST_ZVAL) {
1650
3
    const zval *zv = zend_ast_get_zval(ast);
1651
1652
3
    if (Z_TYPE_P(zv) == IS_STRING) {
1653
3
      smart_str_append(str, Z_STR_P(zv));
1654
3
      return;
1655
3
    }
1656
3
  }
1657
0
  zend_ast_export_ex(str, ast, priority, indent);
1658
0
}
1659
1660
static ZEND_COLD void zend_ast_export_ns_name(smart_str *str, zend_ast *ast, int priority, int indent)
1661
120
{
1662
120
  if (ast->kind == ZEND_AST_ZVAL) {
1663
113
    const zval *zv = zend_ast_get_zval(ast);
1664
1665
113
    if (Z_TYPE_P(zv) == IS_STRING) {
1666
113
        if (ast->attr == ZEND_NAME_FQ) {
1667
1
        smart_str_appendc(str, '\\');
1668
112
        } else if (ast->attr == ZEND_NAME_RELATIVE) {
1669
0
        smart_str_appends(str, "namespace\\");
1670
0
        }
1671
113
      smart_str_append(str, Z_STR_P(zv));
1672
113
      return;
1673
113
    }
1674
113
  }
1675
7
  zend_ast_export_ex(str, ast, priority, indent);
1676
7
}
1677
1678
static ZEND_COLD bool zend_ast_valid_var_char(char ch)
1679
46
{
1680
46
  unsigned char c = (unsigned char)ch;
1681
1682
46
  if (c != '_' && c < 127 &&
1683
46
      (c < '0' || c > '9') &&
1684
46
      (c < 'A' || c > 'Z') &&
1685
46
      (c < 'a' || c > 'z')) {
1686
46
    return false;
1687
46
  }
1688
0
  return true;
1689
46
}
1690
1691
static ZEND_COLD bool zend_ast_valid_var_name(const char *s, size_t len)
1692
60
{
1693
60
  unsigned char c;
1694
60
  size_t i;
1695
1696
60
  if (len == 0) {
1697
0
    return false;
1698
0
  }
1699
60
  c = (unsigned char)s[0];
1700
60
  if (c != '_' && c < 127 &&
1701
27
      (c < 'A' || c > 'Z') &&
1702
24
      (c < 'a' || c > 'z')) {
1703
0
    return false;
1704
0
  }
1705
389
  for (i = 1; i < len; i++) {
1706
329
    c = (unsigned char)s[i];
1707
329
    if (c != '_' && c < 127 &&
1708
287
        (c < '0' || c > '9') &&
1709
283
        (c < 'A' || c > 'Z') &&
1710
47
        (c < 'a' || c > 'z')) {
1711
0
      return false;
1712
0
    }
1713
329
  }
1714
60
  return true;
1715
60
}
1716
1717
static ZEND_COLD bool zend_ast_var_needs_braces(char ch)
1718
46
{
1719
46
  return ch == '[' || zend_ast_valid_var_char(ch);
1720
46
}
1721
1722
static ZEND_COLD void zend_ast_export_var(smart_str *str, zend_ast *ast, int indent)
1723
60
{
1724
60
  if (ast->kind == ZEND_AST_ZVAL) {
1725
60
    zval *zv = zend_ast_get_zval(ast);
1726
60
    if (Z_TYPE_P(zv) == IS_STRING &&
1727
60
        zend_ast_valid_var_name(Z_STRVAL_P(zv), Z_STRLEN_P(zv))) {
1728
60
      smart_str_append(str, Z_STR_P(zv));
1729
60
      return;
1730
60
    }
1731
60
  } else if (ast->kind == ZEND_AST_VAR) {
1732
0
    zend_ast_export_ex(str, ast, 0, indent);
1733
0
    return;
1734
0
  }
1735
0
  smart_str_appendc(str, '{');
1736
0
  zend_ast_export_name(str, ast, 0, indent);
1737
0
  smart_str_appendc(str, '}');
1738
0
}
1739
1740
/* Use zend_ast_export_list() unless fewer than `list->children` children should
1741
 * be exported. */
1742
static ZEND_COLD void zend_ast_export_list_ex(smart_str *str, const zend_ast_list *list, bool separator, int priority, int indent, uint32_t children)
1743
89
{
1744
89
  ZEND_ASSERT(children <= list->children);
1745
89
  uint32_t i = 0;
1746
1747
179
  while (i < children) {
1748
90
    if (i != 0 && separator) {
1749
13
      smart_str_appends(str, ", ");
1750
13
    }
1751
90
    zend_ast_export_ex(str, list->child[i], priority, indent);
1752
90
    i++;
1753
90
  }
1754
89
}
1755
1756
static ZEND_COLD void zend_ast_export_list(smart_str *str, const zend_ast_list *list, bool separator, int priority, int indent)
1757
89
{
1758
89
  zend_ast_export_list_ex(str, list, separator, priority, indent, list->children);
1759
89
}
1760
1761
static ZEND_COLD void zend_ast_export_encaps_list(smart_str *str, char quote, const zend_ast_list *list, int indent)
1762
38
{
1763
38
  uint32_t i = 0;
1764
38
  zend_ast *ast;
1765
1766
173
  while (i < list->children) {
1767
135
    ast = list->child[i];
1768
135
    if (ast->kind == ZEND_AST_ZVAL) {
1769
81
      const zval *zv = zend_ast_get_zval(ast);
1770
1771
81
      ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
1772
81
      zend_ast_export_qstr(str, quote, Z_STR_P(zv));
1773
81
    } else if (ast->kind == ZEND_AST_VAR &&
1774
52
               ast->child[0]->kind == ZEND_AST_ZVAL &&
1775
52
               (i + 1 == list->children ||
1776
48
                list->child[i + 1]->kind != ZEND_AST_ZVAL ||
1777
46
                !zend_ast_var_needs_braces(
1778
46
                    *Z_STRVAL_P(
1779
52
                        zend_ast_get_zval(list->child[i + 1]))))) {
1780
52
      zend_ast_export_ex(str, ast, 0, indent);
1781
52
    } else {
1782
2
      smart_str_appendc(str, '{');
1783
2
      zend_ast_export_ex(str, ast, 0, indent);
1784
2
      smart_str_appendc(str, '}');
1785
2
    }
1786
135
    i++;
1787
135
  }
1788
38
}
1789
1790
static ZEND_COLD void zend_ast_export_name_list_ex(smart_str *str, const zend_ast_list *list, int indent, const char *separator)
1791
0
{
1792
0
  uint32_t i = 0;
1793
1794
0
  while (i < list->children) {
1795
0
    if (i != 0) {
1796
0
      smart_str_appends(str, separator);
1797
0
    }
1798
0
    zend_ast_export_name(str, list->child[i], 0, indent);
1799
0
    i++;
1800
0
  }
1801
0
}
1802
1803
0
#define zend_ast_export_name_list(s, l, i) zend_ast_export_name_list_ex(s, l, i, ", ")
1804
0
#define zend_ast_export_catch_name_list(s, l, i) zend_ast_export_name_list_ex(s, l, i, "|")
1805
1806
static ZEND_COLD void zend_ast_export_var_list(smart_str *str, const zend_ast_list *list, int indent)
1807
0
{
1808
0
  uint32_t i = 0;
1809
1810
0
  while (i < list->children) {
1811
0
    if (i != 0) {
1812
0
      smart_str_appends(str, ", ");
1813
0
    }
1814
0
    if (list->child[i]->attr & ZEND_BIND_REF) {
1815
0
      smart_str_appendc(str, '&');
1816
0
    }
1817
0
    smart_str_appendc(str, '$');
1818
0
    zend_ast_export_name(str, list->child[i], 20, indent);
1819
0
    i++;
1820
0
  }
1821
0
}
1822
1823
static ZEND_COLD void zend_ast_export_stmt(smart_str *str, zend_ast *ast, int indent)
1824
23
{
1825
23
  if (!ast) {
1826
0
    return;
1827
0
  }
1828
1829
23
  if (ast->kind == ZEND_AST_STMT_LIST ||
1830
15
      ast->kind == ZEND_AST_TRAIT_ADAPTATIONS) {
1831
15
    const zend_ast_list *list = (const zend_ast_list*)ast;
1832
15
    uint32_t i = 0;
1833
1834
23
    while (i < list->children) {
1835
8
      ast = list->child[i];
1836
8
      zend_ast_export_stmt(str, ast, indent);
1837
8
      i++;
1838
8
    }
1839
15
  } else {
1840
8
    zend_ast_export_indent(str, indent);
1841
8
    zend_ast_export_ex(str, ast, 0, indent);
1842
8
    switch (ast->kind) {
1843
0
      case ZEND_AST_LABEL:
1844
0
      case ZEND_AST_IF:
1845
0
      case ZEND_AST_SWITCH:
1846
0
      case ZEND_AST_WHILE:
1847
0
      case ZEND_AST_TRY:
1848
0
      case ZEND_AST_FOR:
1849
0
      case ZEND_AST_FOREACH:
1850
0
      case ZEND_AST_FUNC_DECL:
1851
2
      case ZEND_AST_METHOD:
1852
3
      case ZEND_AST_CLASS:
1853
3
      case ZEND_AST_USE_TRAIT:
1854
3
      case ZEND_AST_NAMESPACE:
1855
3
      case ZEND_AST_DECLARE:
1856
3
        break;
1857
0
      case ZEND_AST_PROP_GROUP: {
1858
0
        const zend_ast *first_prop = zend_ast_get_list(ast->child[1])->child[0];
1859
0
        const zend_ast *hook_list = first_prop->child[3];
1860
0
        if (hook_list == NULL) {
1861
0
          smart_str_appendc(str, ';');
1862
0
        }
1863
0
        break;
1864
3
      }
1865
5
      default:
1866
5
        smart_str_appendc(str, ';');
1867
5
        break;
1868
8
    }
1869
8
    smart_str_appendc(str, '\n');
1870
8
  }
1871
23
}
1872
1873
static ZEND_COLD void zend_ast_export_if_stmt(smart_str *str, const zend_ast_list *list, int indent)
1874
0
{
1875
0
  uint32_t i;
1876
0
  zend_ast *ast;
1877
1878
0
tail_call:
1879
0
  i = 0;
1880
0
  while (i < list->children) {
1881
0
    ast = list->child[i];
1882
0
    ZEND_ASSERT(ast->kind == ZEND_AST_IF_ELEM);
1883
0
    if (ast->child[0]) {
1884
0
      if (i == 0) {
1885
0
        smart_str_appends(str, "if (");
1886
0
      } else {
1887
0
        zend_ast_export_indent(str, indent);
1888
0
        smart_str_appends(str, "} elseif (");
1889
0
      }
1890
0
      zend_ast_export_ex(str, ast->child[0], 0, indent);
1891
0
      smart_str_appends(str, ") {\n");
1892
0
      zend_ast_export_stmt(str, ast->child[1], indent + 1);
1893
0
    } else {
1894
0
      zend_ast_export_indent(str, indent);
1895
0
      smart_str_appends(str, "} else ");
1896
0
      if (ast->child[1] && ast->child[1]->kind == ZEND_AST_IF) {
1897
0
        list = (const zend_ast_list*)ast->child[1];
1898
0
        goto tail_call;
1899
0
      } else {
1900
0
        smart_str_appends(str, "{\n");
1901
0
        zend_ast_export_stmt(str, ast->child[1], indent + 1);
1902
0
      }
1903
0
    }
1904
0
    i++;
1905
0
  }
1906
0
  zend_ast_export_indent(str, indent);
1907
0
  smart_str_appendc(str, '}');
1908
0
}
1909
1910
static ZEND_COLD void zend_ast_export_zval(smart_str *str, const zval *zv, int priority, int indent)
1911
46
{
1912
46
  ZVAL_DEREF(zv);
1913
46
  switch (Z_TYPE_P(zv)) {
1914
1
    case IS_NULL:
1915
1
      smart_str_appends(str, "null");
1916
1
      break;
1917
0
    case IS_FALSE:
1918
0
      smart_str_appends(str, "false");
1919
0
      break;
1920
0
    case IS_TRUE:
1921
0
      smart_str_appends(str, "true");
1922
0
      break;
1923
23
    case IS_LONG:
1924
23
      smart_str_append_long(str, Z_LVAL_P(zv));
1925
23
      break;
1926
3
    case IS_DOUBLE:
1927
3
      smart_str_append_double(
1928
3
        str, Z_DVAL_P(zv), (int) EG(precision), /* zero_fraction */ true);
1929
3
      break;
1930
11
    case IS_STRING:
1931
11
      smart_str_appendc(str, '\'');
1932
11
      zend_ast_export_str(str, Z_STR_P(zv));
1933
11
      smart_str_appendc(str, '\'');
1934
11
      break;
1935
8
    case IS_ARRAY: {
1936
8
      zend_long idx;
1937
8
      zend_string *key;
1938
8
      zval *val;
1939
8
      bool first = true;
1940
8
      smart_str_appendc(str, '[');
1941
16
      ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(zv), idx, key, val) {
1942
16
        if (first) {
1943
3
          first = false;
1944
3
        } else {
1945
1
          smart_str_appends(str, ", ");
1946
1
        }
1947
16
        if (key) {
1948
0
          smart_str_appendc(str, '\'');
1949
0
          zend_ast_export_str(str, key);
1950
0
          smart_str_appends(str, "' => ");
1951
4
        } else {
1952
4
          smart_str_append_long(str, idx);
1953
4
          smart_str_appends(str, " => ");
1954
4
        }
1955
16
        zend_ast_export_zval(str, val, 0, indent);
1956
16
      } ZEND_HASH_FOREACH_END();
1957
8
      smart_str_appendc(str, ']');
1958
8
      break;
1959
0
    }
1960
0
    case IS_CONSTANT_AST:
1961
0
      zend_ast_export_ex(str, Z_ASTVAL_P(zv), priority, indent);
1962
0
      break;
1963
0
    default: ZEND_UNREACHABLE();
1964
46
  }
1965
46
}
1966
1967
9
static ZEND_COLD void zend_ast_export_class_no_header(smart_str *str, const zend_ast_decl *decl, int indent) {
1968
9
  if (decl->child[0]) {
1969
1
    smart_str_appends(str, " extends ");
1970
1
    zend_ast_export_ns_name(str, decl->child[0], 0, indent);
1971
1
  }
1972
9
  if (decl->child[1]) {
1973
0
    smart_str_appends(str, " implements ");
1974
0
    zend_ast_export_ex(str, decl->child[1], 0, indent);
1975
0
  }
1976
9
  smart_str_appends(str, " {\n");
1977
9
  zend_ast_export_stmt(str, decl->child[2], indent + 1);
1978
9
  zend_ast_export_indent(str, indent);
1979
9
  smart_str_appendc(str, '}');
1980
9
}
1981
1982
1
static ZEND_COLD void zend_ast_export_attribute_group(smart_str *str, zend_ast *ast, int indent) {
1983
1
  const zend_ast_list *list = zend_ast_get_list(ast);
1984
2
  for (uint32_t i = 0; i < list->children; i++) {
1985
1
    const zend_ast *attr = list->child[i];
1986
1987
1
    if (i) {
1988
0
      smart_str_appends(str, ", ");
1989
0
    }
1990
1
    zend_ast_export_ns_name(str, attr->child[0], 0, indent);
1991
1992
1
    if (attr->child[1]) {
1993
1
      smart_str_appendc(str, '(');
1994
1
      zend_ast_export_ex(str, attr->child[1], 0, indent);
1995
1
      smart_str_appendc(str, ')');
1996
1
    }
1997
1
  }
1998
1
}
1999
2000
1
static ZEND_COLD void zend_ast_export_attributes(smart_str *str, zend_ast *ast, int indent, bool newlines) {
2001
1
  const zend_ast_list *list = zend_ast_get_list(ast);
2002
1
  uint32_t i;
2003
2004
2
  for (i = 0; i < list->children; i++) {
2005
1
    smart_str_appends(str, "#[");
2006
1
    zend_ast_export_attribute_group(str, list->child[i], indent);
2007
1
    smart_str_appendc(str, ']');
2008
2009
1
    if (newlines) {
2010
1
      smart_str_appendc(str, '\n');
2011
1
      zend_ast_export_indent(str, indent);
2012
1
    } else {
2013
0
      smart_str_appendc(str, ' ');
2014
0
    }
2015
1
  }
2016
1
}
2017
2018
14
static ZEND_COLD void zend_ast_export_visibility(smart_str *str, uint32_t flags, zend_modifier_target target) {
2019
14
  if (flags & ZEND_ACC_PUBLIC) {
2020
5
    smart_str_appends(str, "public ");
2021
9
  } else if (flags & ZEND_ACC_PROTECTED) {
2022
0
    smart_str_appends(str, "protected ");
2023
9
  } else if (flags & ZEND_ACC_PRIVATE) {
2024
0
    smart_str_appends(str, "private ");
2025
0
  }
2026
2027
14
  if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
2028
2
    if (flags & ZEND_ACC_PRIVATE_SET) {
2029
0
      smart_str_appends(str, "private(set) ");
2030
2
    } else if (flags & ZEND_ACC_PROTECTED_SET) {
2031
0
      smart_str_appends(str, "protected(set) ");
2032
2
    } else if (flags & ZEND_ACC_PUBLIC_SET) {
2033
0
      smart_str_appends(str, "public(set) ");
2034
0
    }
2035
2
  }
2036
14
}
2037
2038
2
static ZEND_COLD void zend_ast_export_type(smart_str *str, zend_ast *ast, int indent) {
2039
2
  if (ast->kind == ZEND_AST_TYPE_UNION) {
2040
0
    const zend_ast_list *list = zend_ast_get_list(ast);
2041
0
    for (uint32_t i = 0; i < list->children; i++) {
2042
0
      if (i != 0) {
2043
0
        smart_str_appendc(str, '|');
2044
0
      }
2045
0
      zend_ast_export_type(str, list->child[i], indent);
2046
0
    }
2047
0
    return;
2048
0
  }
2049
2
  if (ast->kind == ZEND_AST_TYPE_INTERSECTION) {
2050
0
    const zend_ast_list *list = zend_ast_get_list(ast);
2051
0
    for (uint32_t i = 0; i < list->children; i++) {
2052
0
      if (i != 0) {
2053
0
        smart_str_appendc(str, '&');
2054
0
      }
2055
0
      zend_ast_export_type(str, list->child[i], indent);
2056
0
    }
2057
0
    return;
2058
0
  }
2059
2
  if (ast->attr & ZEND_TYPE_NULLABLE) {
2060
0
    smart_str_appendc(str, '?');
2061
0
  }
2062
2
  zend_ast_export_ns_name(str, ast, 0, indent);
2063
2
}
2064
2065
static ZEND_COLD void zend_ast_export_hook_list(smart_str *str, const zend_ast_list *hook_list, int indent)
2066
0
{
2067
0
  smart_str_appends(str, " {");
2068
0
  smart_str_appendc(str, '\n');
2069
0
  indent++;
2070
0
  zend_ast_export_indent(str, indent);
2071
2072
0
  for (uint32_t i = 0; i < hook_list->children; i++) {
2073
0
    const zend_ast_decl *hook = (const zend_ast_decl *)hook_list->child[i];
2074
0
    zend_ast_export_visibility(str, hook->flags, ZEND_MODIFIER_TARGET_PROPERTY);
2075
0
    if (hook->flags & ZEND_ACC_FINAL) {
2076
0
      smart_str_appends(str, "final ");
2077
0
    }
2078
0
    smart_str_append(str, hook->name);
2079
0
    zend_ast *body = hook->child[2];
2080
0
    if (body == NULL) {
2081
0
      smart_str_appendc(str, ';');
2082
0
    } else if (body->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
2083
0
      smart_str_appends(str, " => ");
2084
0
      zend_ast_export_ex(str, body->child[0], 0, indent);
2085
0
      smart_str_appendc(str, ';');
2086
0
    } else {
2087
0
      smart_str_appends(str, " {\n");
2088
0
      zend_ast_export_stmt(str, body, indent + 1);
2089
0
      zend_ast_export_indent(str, indent);
2090
0
      smart_str_appendc(str, '}');
2091
0
    }
2092
0
    if (i < (hook_list->children - 1)) {
2093
0
      smart_str_appendc(str, '\n');
2094
0
      zend_ast_export_indent(str, indent);
2095
0
    }
2096
0
  }
2097
0
  smart_str_appendc(str, '\n');
2098
0
  indent--;
2099
0
  zend_ast_export_indent(str, indent);
2100
0
  smart_str_appendc(str, '}');
2101
0
}
2102
2103
31
#define BINARY_OP(_op, _p, _pl, _pr) do { \
2104
31
    op = _op; \
2105
31
    p = _p; \
2106
31
    pl = _pl; \
2107
31
    pr = _pr; \
2108
31
    goto binary_op; \
2109
31
  } while (0)
2110
2111
94
#define PREFIX_OP(_op, _p, _pl) do { \
2112
94
    op = _op; \
2113
94
    p = _p; \
2114
94
    pl = _pl; \
2115
94
    goto prefix_op; \
2116
94
  } while (0)
2117
2118
0
#define FUNC_OP(_op) do { \
2119
0
    op = _op; \
2120
0
    goto func_op; \
2121
0
  } while (0)
2122
2123
0
#define POSTFIX_OP(_op, _p, _pl) do { \
2124
0
    op = _op; \
2125
0
    p = _p; \
2126
0
    pl = _pl; \
2127
0
    goto postfix_op; \
2128
0
  } while (0)
2129
2130
2
#define APPEND_NODE_1(_op) do { \
2131
2
    op = _op; \
2132
2
    goto append_node_1; \
2133
2
  } while (0)
2134
2135
3
#define APPEND_STR(_op) do { \
2136
3
    op = _op; \
2137
3
    goto append_str; \
2138
3
  } while (0)
2139
2140
1
#define APPEND_DEFAULT_VALUE(n) do { \
2141
1
    p = n; \
2142
1
    goto append_default_value; \
2143
1
  } while (0)
2144
2145
static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int priority, int indent)
2146
543
{
2147
543
  const zend_ast_decl *decl;
2148
543
  int p, pl, pr;
2149
543
  const char *op;
2150
2151
548
tail_call:
2152
548
  if (!ast) {
2153
11
    return;
2154
11
  }
2155
537
  switch (ast->kind) {
2156
    /* special nodes */
2157
42
    case ZEND_AST_ZVAL:
2158
42
      zend_ast_export_zval(str, zend_ast_get_zval(ast), priority, indent);
2159
42
      break;
2160
7
    case ZEND_AST_CONSTANT: {
2161
7
      zend_string *name = zend_ast_get_constant_name(ast);
2162
7
      smart_str_append(str, name);
2163
7
      break;
2164
0
    }
2165
1
    case ZEND_AST_OP_ARRAY:
2166
1
      smart_str_appends(str, "Closure(");
2167
1
      smart_str_append(str, zend_ast_get_op_array(ast)->op_array->function_name);
2168
1
      smart_str_appendc(str, ')');
2169
1
      break;
2170
0
    case ZEND_AST_CONSTANT_CLASS:
2171
0
      smart_str_appendl(str, "__CLASS__", sizeof("__CLASS__")-1);
2172
0
      break;
2173
0
    case ZEND_AST_ZNODE:
2174
      /* This AST kind is only used for temporary nodes during compilation */
2175
0
      ZEND_UNREACHABLE();
2176
0
      break;
2177
2178
    /* declaration nodes */
2179
0
    case ZEND_AST_FUNC_DECL:
2180
4
    case ZEND_AST_CLOSURE:
2181
9
    case ZEND_AST_ARROW_FUNC:
2182
11
    case ZEND_AST_METHOD:
2183
11
      decl = (const zend_ast_decl *) ast;
2184
11
      if (decl->kind == ZEND_AST_ARROW_FUNC && (decl->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) {
2185
2
        smart_str_appendc(str, '(');
2186
2
      }
2187
11
      if (decl->child[4]) {
2188
0
        bool newlines = !(ast->kind == ZEND_AST_CLOSURE || ast->kind == ZEND_AST_ARROW_FUNC);
2189
0
        zend_ast_export_attributes(str, decl->child[4], indent, newlines);
2190
0
      }
2191
2192
11
      zend_ast_export_visibility(str, decl->flags, ZEND_MODIFIER_TARGET_METHOD);
2193
2194
11
      if (decl->flags & ZEND_ACC_STATIC) {
2195
0
        smart_str_appends(str, "static ");
2196
0
      }
2197
11
      if (decl->flags & ZEND_ACC_ABSTRACT) {
2198
0
        smart_str_appends(str, "abstract ");
2199
0
      }
2200
11
      if (decl->flags & ZEND_ACC_FINAL) {
2201
0
        smart_str_appends(str, "final ");
2202
0
      }
2203
11
      if (decl->kind == ZEND_AST_ARROW_FUNC) {
2204
5
        smart_str_appends(str, "fn");
2205
6
      } else {
2206
6
        smart_str_appends(str, "function ");
2207
6
      }
2208
11
      if (decl->flags & ZEND_ACC_RETURN_REFERENCE) {
2209
0
        smart_str_appendc(str, '&');
2210
0
      }
2211
11
      if (ast->kind != ZEND_AST_CLOSURE && ast->kind != ZEND_AST_ARROW_FUNC) {
2212
2
        smart_str_append(str, decl->name);
2213
2
      }
2214
11
      smart_str_appendc(str, '(');
2215
11
      zend_ast_export_ex(str, decl->child[0], 0, indent);
2216
11
      smart_str_appendc(str, ')');
2217
11
      zend_ast_export_ex(str, decl->child[1], 0, indent);
2218
11
      if (decl->child[3]) {
2219
0
        smart_str_appends(str, ": ");
2220
0
        zend_ast_export_type(str, decl->child[3], indent);
2221
0
      }
2222
11
      if (decl->child[2]) {
2223
11
        if (decl->kind == ZEND_AST_ARROW_FUNC) {
2224
5
          zend_ast *body = decl->child[2];
2225
5
          if (body->kind == ZEND_AST_RETURN) {
2226
0
            body = body->child[0];
2227
0
          }
2228
5
          smart_str_appends(str, " => ");
2229
5
          zend_ast_export_ex(str, body, 0, indent);
2230
5
          if (decl->attr & ZEND_PARENTHESIZED_ARROW_FUNC) {
2231
2
            smart_str_appendc(str, ')');
2232
2
          }
2233
5
          break;
2234
5
        }
2235
2236
6
        smart_str_appends(str, " {\n");
2237
6
        zend_ast_export_stmt(str, decl->child[2], indent + 1);
2238
6
        zend_ast_export_indent(str, indent);
2239
6
        smart_str_appendc(str, '}');
2240
6
        if (ast->kind != ZEND_AST_CLOSURE) {
2241
2
          smart_str_appendc(str, '\n');
2242
2
        }
2243
6
      } else {
2244
0
        smart_str_appends(str, ";\n");
2245
0
      }
2246
6
      break;
2247
6
    case ZEND_AST_CLASS:
2248
1
      decl = (const zend_ast_decl *) ast;
2249
1
      if (decl->child[3]) {
2250
1
        zend_ast_export_attributes(str, decl->child[3], indent, true);
2251
1
      }
2252
1
      if (decl->flags & ZEND_ACC_INTERFACE) {
2253
0
        smart_str_appends(str, "interface ");
2254
1
      } else if (decl->flags & ZEND_ACC_TRAIT) {
2255
0
        smart_str_appends(str, "trait ");
2256
1
      } else if (decl->flags & ZEND_ACC_ENUM) {
2257
0
        smart_str_appends(str, "enum ");
2258
1
      } else {
2259
1
        if (decl->flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
2260
0
          smart_str_appends(str, "abstract ");
2261
0
        }
2262
1
        if (decl->flags & ZEND_ACC_FINAL) {
2263
0
          smart_str_appends(str, "final ");
2264
0
        }
2265
1
        if (decl->flags & ZEND_ACC_READONLY_CLASS) {
2266
0
          smart_str_appends(str, "readonly ");
2267
0
        }
2268
1
        smart_str_appends(str, "class ");
2269
1
      }
2270
1
      smart_str_append(str, decl->name);
2271
1
      if (decl->flags & ZEND_ACC_ENUM && decl->child[4]) {
2272
0
        smart_str_appends(str, ": ");
2273
0
        zend_ast_export_type(str, decl->child[4], indent);
2274
0
      }
2275
1
      zend_ast_export_class_no_header(str, decl, indent);
2276
1
      smart_str_appendc(str, '\n');
2277
1
      break;
2278
2279
    /* list nodes */
2280
64
    case ZEND_AST_ARG_LIST:
2281
64
    case ZEND_AST_EXPR_LIST:
2282
75
    case ZEND_AST_PARAM_LIST:
2283
79
simple_list:
2284
79
      zend_ast_export_list(str, zend_ast_get_list(ast), true, 20, indent);
2285
79
      break;
2286
10
    case ZEND_AST_ARRAY:
2287
10
      smart_str_appendc(str, '[');
2288
10
      zend_ast_export_list(str, zend_ast_get_list(ast), true, 20, indent);
2289
10
      smart_str_appendc(str, ']');
2290
10
      break;
2291
38
    case ZEND_AST_ENCAPS_LIST:
2292
38
      smart_str_appendc(str, '"');
2293
38
      zend_ast_export_encaps_list(str, '"', zend_ast_get_list(ast), indent);
2294
38
      smart_str_appendc(str, '"');
2295
38
      break;
2296
0
    case ZEND_AST_STMT_LIST:
2297
0
    case ZEND_AST_TRAIT_ADAPTATIONS:
2298
0
      zend_ast_export_stmt(str, ast, indent);
2299
0
      break;
2300
0
    case ZEND_AST_IF:
2301
0
      zend_ast_export_if_stmt(str, zend_ast_get_list(ast), indent);
2302
0
      break;
2303
0
    case ZEND_AST_SWITCH_LIST:
2304
0
    case ZEND_AST_CATCH_LIST:
2305
0
    case ZEND_AST_MATCH_ARM_LIST:
2306
0
      zend_ast_export_list(str, zend_ast_get_list(ast), false, 0, indent);
2307
0
      break;
2308
0
    case ZEND_AST_CLOSURE_USES:
2309
0
      smart_str_appends(str, " use(");
2310
0
      zend_ast_export_var_list(str, zend_ast_get_list(ast), indent);
2311
0
      smart_str_appendc(str, ')');
2312
0
      break;
2313
0
    case ZEND_AST_PROP_GROUP: {
2314
0
      zend_ast *type_ast = ast->child[0];
2315
0
      zend_ast *prop_ast = ast->child[1];
2316
2317
0
      if (ast->child[2]) {
2318
0
        zend_ast_export_attributes(str, ast->child[2], indent, true);
2319
0
      }
2320
2321
0
      zend_ast_export_visibility(str, ast->attr, ZEND_MODIFIER_TARGET_PROPERTY);
2322
2323
0
      if (ast->attr & ZEND_ACC_STATIC) {
2324
0
        smart_str_appends(str, "static ");
2325
0
      }
2326
0
      if (ast->attr & ZEND_ACC_READONLY) {
2327
0
        smart_str_appends(str, "readonly ");
2328
0
      }
2329
2330
0
      if (type_ast) {
2331
0
        zend_ast_export_type(str, type_ast, indent);
2332
0
        smart_str_appendc(str, ' ');
2333
0
      }
2334
2335
0
      ast = prop_ast;
2336
0
      goto simple_list;
2337
0
    }
2338
2339
0
    case ZEND_AST_CONST_DECL: {
2340
0
      zend_ast_list *ast_list = zend_ast_get_list(ast);
2341
      /* Attributes are stored at the end of the list if present. */
2342
0
      if (ast_list->child[ast_list->children - 1]->kind == ZEND_AST_ATTRIBUTE_LIST) {
2343
0
        zend_ast_export_attributes(
2344
0
          str,
2345
0
          ast_list->child[ast_list->children - 1],
2346
0
          indent,
2347
0
          true
2348
0
        );
2349
        /* So that the list printing doesn't try to print the attributes,
2350
         * use zend_ast_export_list_ex() to override the number of children
2351
         * to print. */
2352
0
        smart_str_appends(str, "const ");
2353
0
        zend_ast_export_list_ex(str, ast_list, true, 20, indent, ast_list->children - 1);
2354
0
        break;
2355
0
      }
2356
0
      smart_str_appends(str, "const ");
2357
0
      goto simple_list;
2358
0
    }
2359
1
    case ZEND_AST_CLASS_CONST_GROUP:
2360
1
      if (ast->child[1]) {
2361
0
        zend_ast_export_attributes(str, ast->child[1], indent, true);
2362
0
      }
2363
2364
1
      zend_ast_export_visibility(str, ast->attr, ZEND_MODIFIER_TARGET_CONSTANT);
2365
1
      smart_str_appends(str, "const ");
2366
1
      if (ast->child[2]) {
2367
1
        zend_ast_export_type(str, ast->child[2], indent);
2368
1
        smart_str_appendc(str, ' ');
2369
1
      }
2370
2371
1
      ast = ast->child[0];
2372
2373
1
      goto simple_list;
2374
0
    case ZEND_AST_NAME_LIST:
2375
0
      zend_ast_export_name_list(str, zend_ast_get_list(ast), indent);
2376
0
      break;
2377
0
    case ZEND_AST_USE:
2378
0
      smart_str_appends(str, "use ");
2379
0
      if (ast->attr == T_FUNCTION) {
2380
0
        smart_str_appends(str, "function ");
2381
0
      } else if (ast->attr == T_CONST) {
2382
0
        smart_str_appends(str, "const ");
2383
0
      }
2384
0
      goto simple_list;
2385
2386
    /* 0 child nodes */
2387
0
    case ZEND_AST_MAGIC_CONST:
2388
0
      switch (ast->attr) {
2389
0
        case T_LINE:     APPEND_STR("__LINE__");
2390
0
        case T_FILE:     APPEND_STR("__FILE__");
2391
0
        case T_DIR:      APPEND_STR("__DIR__");
2392
0
        case T_TRAIT_C:  APPEND_STR("__TRAIT__");
2393
0
        case T_METHOD_C: APPEND_STR("__METHOD__");
2394
0
        case T_FUNC_C:   APPEND_STR("__FUNCTION__");
2395
0
        case T_PROPERTY_C: APPEND_STR("__PROPERTY__");
2396
0
        case T_NS_C:     APPEND_STR("__NAMESPACE__");
2397
0
        case T_CLASS_C:  APPEND_STR("__CLASS__");
2398
0
        default: ZEND_UNREACHABLE();
2399
0
      }
2400
0
      break;
2401
0
    case ZEND_AST_TYPE:
2402
0
      switch (ast->attr & ~ZEND_TYPE_NULLABLE) {
2403
0
        case IS_ARRAY:    APPEND_STR("array");
2404
0
        case IS_CALLABLE: APPEND_STR("callable");
2405
0
        case IS_STATIC:   APPEND_STR("static");
2406
0
        case IS_MIXED:    APPEND_STR("mixed");
2407
0
        default: ZEND_UNREACHABLE();
2408
0
      }
2409
0
      break;
2410
3
    case ZEND_AST_PLACEHOLDER_ARG:
2411
3
      if (ast->attr == ZEND_PLACEHOLDER_VARIADIC) {
2412
3
        APPEND_STR("...");
2413
3
      } else  {
2414
0
        APPEND_STR("?");
2415
0
      }
2416
0
      break;
2417
2418
    /* 1 child node */
2419
58
    case ZEND_AST_VAR:
2420
58
      smart_str_appendc(str, '$');
2421
58
      zend_ast_export_var(str, ast->child[0], indent);
2422
58
      break;
2423
54
    case ZEND_AST_CONST:
2424
54
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2425
54
      break;
2426
0
    case ZEND_AST_UNPACK:
2427
0
      smart_str_appends(str, "...");
2428
0
      ast = ast->child[0];
2429
0
      goto tail_call;
2430
0
    case ZEND_AST_UNARY_PLUS:  PREFIX_OP("+", 240, 241);
2431
1
    case ZEND_AST_UNARY_MINUS: PREFIX_OP("-", 240, 241);
2432
0
    case ZEND_AST_CAST:
2433
0
      switch (ast->attr) {
2434
0
        case IS_NULL:      PREFIX_OP("(unset)",  240, 241);
2435
0
        case _IS_BOOL:     PREFIX_OP("(bool)",   240, 241);
2436
0
        case IS_LONG:      PREFIX_OP("(int)",    240, 241);
2437
0
        case IS_DOUBLE:    PREFIX_OP("(float)", 240, 241);
2438
0
        case IS_STRING:    PREFIX_OP("(string)", 240, 241);
2439
0
        case IS_ARRAY:     PREFIX_OP("(array)",  240, 241);
2440
0
        case IS_OBJECT:    PREFIX_OP("(object)", 240, 241);
2441
0
        default: ZEND_UNREACHABLE();
2442
0
      }
2443
0
      break;
2444
1
    case ZEND_AST_CAST_VOID:
2445
1
      PREFIX_OP("(void)", 240, 241);
2446
0
      break;
2447
0
    case ZEND_AST_EMPTY:
2448
0
      FUNC_OP("empty");
2449
0
    case ZEND_AST_ISSET:
2450
0
      FUNC_OP("isset");
2451
36
    case ZEND_AST_SILENCE:
2452
36
      PREFIX_OP("@", 240, 241);
2453
1
    case ZEND_AST_SHELL_EXEC:
2454
1
      smart_str_appendc(str, '`');
2455
1
      if (ast->child[0]->kind == ZEND_AST_ENCAPS_LIST) {
2456
0
        zend_ast_export_encaps_list(str, '`', zend_ast_get_list(ast->child[0]), indent);
2457
1
      } else {
2458
1
        zval *zv;
2459
1
        ZEND_ASSERT(ast->child[0]->kind == ZEND_AST_ZVAL);
2460
1
        zv = zend_ast_get_zval(ast->child[0]);
2461
1
        ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
2462
1
        zend_ast_export_qstr(str, '`', Z_STR_P(zv));
2463
1
      }
2464
1
      smart_str_appendc(str, '`');
2465
1
      break;
2466
0
    case ZEND_AST_PRINT:
2467
0
      PREFIX_OP("print ", 60, 61);
2468
0
    case ZEND_AST_INCLUDE_OR_EVAL:
2469
0
      switch (ast->attr) {
2470
0
        case ZEND_INCLUDE_ONCE: FUNC_OP("include_once");
2471
0
        case ZEND_INCLUDE:      FUNC_OP("include");
2472
0
        case ZEND_REQUIRE_ONCE: FUNC_OP("require_once");
2473
0
        case ZEND_REQUIRE:      FUNC_OP("require");
2474
0
        case ZEND_EVAL:         FUNC_OP("eval");
2475
0
        default: ZEND_UNREACHABLE();
2476
0
      }
2477
0
      break;
2478
54
    case ZEND_AST_UNARY_OP:
2479
54
      switch (ast->attr) {
2480
48
        case ZEND_BW_NOT:   PREFIX_OP("~", 240, 241);
2481
6
        case ZEND_BOOL_NOT: PREFIX_OP("!", 240, 241);
2482
0
        default: ZEND_UNREACHABLE();
2483
54
      }
2484
0
      break;
2485
0
    case ZEND_AST_PRE_INC:
2486
0
      PREFIX_OP("++", 240, 241);
2487
2
    case ZEND_AST_PRE_DEC:
2488
2
      PREFIX_OP("--", 240, 241);
2489
0
    case ZEND_AST_POST_INC:
2490
0
      POSTFIX_OP("++", 240, 241);
2491
0
    case ZEND_AST_POST_DEC:
2492
0
      POSTFIX_OP("--", 240, 241);
2493
2494
0
    case ZEND_AST_GLOBAL:
2495
0
      APPEND_NODE_1("global");
2496
0
    case ZEND_AST_UNSET:
2497
0
      FUNC_OP("unset");
2498
1
    case ZEND_AST_RETURN:
2499
1
      APPEND_NODE_1("return");
2500
0
    case ZEND_AST_LABEL:
2501
0
      zend_ast_export_name(str, ast->child[0], 0, indent);
2502
0
      smart_str_appendc(str, ':');
2503
0
      break;
2504
0
    case ZEND_AST_REF:
2505
0
      smart_str_appendc(str, '&');
2506
0
      ast = ast->child[0];
2507
0
      goto tail_call;
2508
0
    case ZEND_AST_HALT_COMPILER:
2509
0
      APPEND_STR("__HALT_COMPILER()");
2510
1
    case ZEND_AST_ECHO:
2511
1
      APPEND_NODE_1("echo");
2512
0
    case ZEND_AST_THROW:
2513
0
      APPEND_NODE_1("throw");
2514
0
    case ZEND_AST_GOTO:
2515
0
      smart_str_appends(str, "goto ");
2516
0
      zend_ast_export_name(str, ast->child[0], 0, indent);
2517
0
      break;
2518
0
    case ZEND_AST_BREAK:
2519
0
      APPEND_NODE_1("break");
2520
0
    case ZEND_AST_CONTINUE:
2521
0
      APPEND_NODE_1("continue");
2522
2523
    /* 2 child nodes */
2524
2
    case ZEND_AST_DIM:
2525
2
      zend_ast_export_ex(str, ast->child[0], 260, indent);
2526
2
      smart_str_appendc(str, '[');
2527
2
      if (ast->child[1]) {
2528
2
        zend_ast_export_ex(str, ast->child[1], 0, indent);
2529
2
      }
2530
2
      smart_str_appendc(str, ']');
2531
2
      break;
2532
2
    case ZEND_AST_PROP:
2533
2
    case ZEND_AST_NULLSAFE_PROP:
2534
2
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2535
2
      smart_str_appends(str, ast->kind == ZEND_AST_NULLSAFE_PROP ? "?->" : "->");
2536
2
      zend_ast_export_var(str, ast->child[1], indent);
2537
2
      break;
2538
0
    case ZEND_AST_STATIC_PROP:
2539
0
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2540
0
      smart_str_appends(str, "::$");
2541
0
      zend_ast_export_var(str, ast->child[1], indent);
2542
0
      break;
2543
62
    case ZEND_AST_CALL: {
2544
62
      zend_ast *left = ast->child[0];
2545
62
      if (left->kind == ZEND_AST_ARROW_FUNC || left->kind == ZEND_AST_CLOSURE) {
2546
1
        smart_str_appendc(str, '(');
2547
1
        zend_ast_export_ns_name(str, left, 0, indent);
2548
1
        smart_str_appendc(str, ')');
2549
61
      } else {
2550
61
        zend_ast_export_ns_name(str, left, 0, indent);
2551
61
      }
2552
62
      smart_str_appendc(str, '(');
2553
62
      zend_ast_export_ex(str, ast->child[1], 0, indent);
2554
62
      smart_str_appendc(str, ')');
2555
62
      break;
2556
2
    }
2557
3
    case ZEND_AST_CALLABLE_CONVERT: {
2558
3
      zend_ast_fcc *fcc_ast = (zend_ast_fcc*)ast;
2559
3
      ast = fcc_ast->args;
2560
3
      goto simple_list;
2561
2
    }
2562
0
    case ZEND_AST_CLASS_CONST:
2563
0
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2564
0
      smart_str_appends(str, "::");
2565
0
      zend_ast_export_name(str, ast->child[1], 0, indent);
2566
0
      break;
2567
0
    case ZEND_AST_CLASS_NAME:
2568
0
      if (ast->child[0] == NULL) {
2569
        /* The const expr representation stores the fetch type instead. */
2570
0
        switch (ast->attr) {
2571
0
          case ZEND_FETCH_CLASS_SELF:
2572
0
            smart_str_append(str, ZSTR_KNOWN(ZEND_STR_SELF));
2573
0
            break;
2574
0
          case ZEND_FETCH_CLASS_PARENT:
2575
0
            smart_str_append(str, ZSTR_KNOWN(ZEND_STR_PARENT));
2576
0
            break;
2577
0
          default: ZEND_UNREACHABLE();
2578
0
        }
2579
0
      } else {
2580
0
        zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2581
0
      }
2582
0
      smart_str_appends(str, "::class");
2583
0
      break;
2584
0
    case ZEND_AST_ASSIGN:            BINARY_OP(" = ",   90, 91, 90);
2585
0
    case ZEND_AST_ASSIGN_REF:        BINARY_OP(" =& ",  90, 91, 90);
2586
1
    case ZEND_AST_ASSIGN_OP:
2587
1
      switch (ast->attr) {
2588
0
        case ZEND_ADD:    BINARY_OP(" += ",  90, 91, 90);
2589
0
        case ZEND_SUB:    BINARY_OP(" -= ",  90, 91, 90);
2590
0
        case ZEND_MUL:    BINARY_OP(" *= ",  90, 91, 90);
2591
0
        case ZEND_DIV:    BINARY_OP(" /= ",  90, 91, 90);
2592
0
        case ZEND_MOD:    BINARY_OP(" %= ",  90, 91, 90);
2593
0
        case ZEND_SL:     BINARY_OP(" <<= ", 90, 91, 90);
2594
0
        case ZEND_SR:     BINARY_OP(" >>= ", 90, 91, 90);
2595
0
        case ZEND_CONCAT: BINARY_OP(" .= ",  90, 91, 90);
2596
0
        case ZEND_BW_OR:  BINARY_OP(" |= ",  90, 91, 90);
2597
0
        case ZEND_BW_AND: BINARY_OP(" &= ",  90, 91, 90);
2598
0
        case ZEND_BW_XOR: BINARY_OP(" ^= ",  90, 91, 90);
2599
1
        case ZEND_POW:    BINARY_OP(" **= ", 90, 91, 90);
2600
0
        default: ZEND_UNREACHABLE();
2601
1
      }
2602
0
      break;
2603
0
    case ZEND_AST_ASSIGN_COALESCE: BINARY_OP(" \?\?= ", 90, 91, 90);
2604
14
    case ZEND_AST_BINARY_OP:
2605
14
      switch (ast->attr) {
2606
2
        case ZEND_ADD:                 BINARY_OP(" + ",   200, 200, 201);
2607
2
        case ZEND_SUB:                 BINARY_OP(" - ",   200, 200, 201);
2608
2
        case ZEND_MUL:                 BINARY_OP(" * ",   210, 210, 211);
2609
3
        case ZEND_DIV:                 BINARY_OP(" / ",   210, 210, 211);
2610
2
        case ZEND_MOD:                 BINARY_OP(" % ",   210, 210, 211);
2611
0
        case ZEND_SL:                  BINARY_OP(" << ",  190, 190, 191);
2612
0
        case ZEND_SR:                  BINARY_OP(" >> ",  190, 190, 191);
2613
0
        case ZEND_CONCAT:              BINARY_OP(" . ",   185, 185, 186);
2614
1
        case ZEND_BW_OR:               BINARY_OP(" | ",   140, 140, 141);
2615
0
        case ZEND_BW_AND:              BINARY_OP(" & ",   160, 160, 161);
2616
0
        case ZEND_BW_XOR:              BINARY_OP(" ^ ",   150, 150, 151);
2617
0
        case ZEND_IS_IDENTICAL:        BINARY_OP(" === ", 170, 171, 171);
2618
0
        case ZEND_IS_NOT_IDENTICAL:    BINARY_OP(" !== ", 170, 171, 171);
2619
0
        case ZEND_IS_EQUAL:            BINARY_OP(" == ",  170, 171, 171);
2620
0
        case ZEND_IS_NOT_EQUAL:        BINARY_OP(" != ",  170, 171, 171);
2621
2
        case ZEND_IS_SMALLER:          BINARY_OP(" < ",   180, 181, 181);
2622
0
        case ZEND_IS_SMALLER_OR_EQUAL: BINARY_OP(" <= ",  180, 181, 181);
2623
0
        case ZEND_POW:                 BINARY_OP(" ** ",  250, 251, 250);
2624
0
        case ZEND_BOOL_XOR:            BINARY_OP(" xor ",  40,  40,  41);
2625
0
        case ZEND_SPACESHIP:           BINARY_OP(" <=> ", 180, 181, 181);
2626
0
        default: ZEND_UNREACHABLE();
2627
14
      }
2628
0
      break;
2629
1
    case ZEND_AST_GREATER:                 BINARY_OP(" > ",   180, 181, 181);
2630
0
    case ZEND_AST_GREATER_EQUAL:           BINARY_OP(" >= ",  180, 181, 181);
2631
13
    case ZEND_AST_AND:                     BINARY_OP(" && ",  130, 130, 131);
2632
0
    case ZEND_AST_OR:                      BINARY_OP(" || ",  120, 120, 121);
2633
2
    case ZEND_AST_PIPE:                    BINARY_OP(" |> ",  183, 183, 184);
2634
22
    case ZEND_AST_ARRAY_ELEM:
2635
22
      if (ast->child[1]) {
2636
0
        zend_ast_export_ex(str, ast->child[1], 80, indent);
2637
0
        smart_str_appends(str, " => ");
2638
0
      }
2639
22
      if (ast->attr)
2640
0
        smart_str_appendc(str, '&');
2641
22
      zend_ast_export_ex(str, ast->child[0], 80, indent);
2642
22
      break;
2643
8
    case ZEND_AST_NEW:
2644
8
      smart_str_appends(str, "new ");
2645
8
      if (ast->child[0]->kind == ZEND_AST_CLASS) {
2646
8
        const zend_ast_decl *decl = (const zend_ast_decl *) ast->child[0];
2647
8
        if (decl->child[3]) {
2648
0
          zend_ast_export_attributes(str, decl->child[3], indent, false);
2649
0
        }
2650
8
        smart_str_appends(str, "class");
2651
8
        if (!zend_ast_is_list(ast->child[1])
2652
6
            || zend_ast_get_list(ast->child[1])->children) {
2653
4
          smart_str_appendc(str, '(');
2654
4
          zend_ast_export_ex(str, ast->child[1], 0, indent);
2655
4
          smart_str_appendc(str, ')');
2656
4
        }
2657
8
        zend_ast_export_class_no_header(str, decl, indent);
2658
8
      } else {
2659
0
        zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2660
0
        smart_str_appendc(str, '(');
2661
0
        zend_ast_export_ex(str, ast->child[1], 0, indent);
2662
0
        smart_str_appendc(str, ')');
2663
0
      }
2664
8
      break;
2665
0
    case ZEND_AST_INSTANCEOF:
2666
0
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2667
0
      smart_str_appends(str, " instanceof ");
2668
0
      zend_ast_export_ns_name(str, ast->child[1], 0, indent);
2669
0
      break;
2670
1
    case ZEND_AST_YIELD:
2671
1
      if (priority > 70) smart_str_appendc(str, '(');
2672
1
      smart_str_appends(str, "yield ");
2673
1
      if (ast->child[0]) {
2674
1
        if (ast->child[1]) {
2675
0
          zend_ast_export_ex(str, ast->child[1], 70, indent);
2676
0
          smart_str_appends(str, " => ");
2677
0
        }
2678
1
        zend_ast_export_ex(str, ast->child[0], 70, indent);
2679
1
      }
2680
1
      if (priority > 70) smart_str_appendc(str, ')');
2681
1
      break;
2682
0
    case ZEND_AST_YIELD_FROM:
2683
0
      PREFIX_OP("yield from ", 85, 86);
2684
0
    case ZEND_AST_COALESCE: BINARY_OP(" ?? ", 110, 111, 110);
2685
0
    case ZEND_AST_STATIC:
2686
0
      smart_str_appends(str, "static $");
2687
0
      zend_ast_export_name(str, ast->child[0], 0, indent);
2688
0
      APPEND_DEFAULT_VALUE(1);
2689
0
    case ZEND_AST_WHILE:
2690
0
      smart_str_appends(str, "while (");
2691
0
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2692
0
      smart_str_appends(str, ") {\n");
2693
0
      zend_ast_export_stmt(str, ast->child[1], indent + 1);
2694
0
      zend_ast_export_indent(str, indent);
2695
0
      smart_str_appendc(str, '}');
2696
0
      break;
2697
0
    case ZEND_AST_DO_WHILE:
2698
0
      smart_str_appends(str, "do {\n");
2699
0
      zend_ast_export_stmt(str, ast->child[0], indent + 1);
2700
0
      zend_ast_export_indent(str, indent);
2701
0
      smart_str_appends(str, "} while (");
2702
0
      zend_ast_export_ex(str, ast->child[1], 0, indent);
2703
0
      smart_str_appendc(str, ')');
2704
0
      break;
2705
2706
0
    case ZEND_AST_IF_ELEM:
2707
0
      if (ast->child[0]) {
2708
0
        smart_str_appends(str, "if (");
2709
0
        zend_ast_export_ex(str, ast->child[0], 0, indent);
2710
0
        smart_str_appends(str, ") {\n");
2711
0
        zend_ast_export_stmt(str, ast->child[1], indent + 1);
2712
0
      } else {
2713
0
        smart_str_appends(str, "else {\n");
2714
0
        zend_ast_export_stmt(str, ast->child[1], indent + 1);
2715
0
      }
2716
0
      zend_ast_export_indent(str, indent);
2717
0
      smart_str_appendc(str, '}');
2718
0
      break;
2719
0
    case ZEND_AST_SWITCH:
2720
0
      smart_str_appends(str, "switch (");
2721
0
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2722
0
      smart_str_appends(str, ") {\n");
2723
0
      zend_ast_export_ex(str, ast->child[1], 0, indent + 1);
2724
0
      zend_ast_export_indent(str, indent);
2725
0
      smart_str_appendc(str, '}');
2726
0
      break;
2727
0
    case ZEND_AST_SWITCH_CASE:
2728
0
      zend_ast_export_indent(str, indent);
2729
0
      if (ast->child[0]) {
2730
0
        smart_str_appends(str, "case ");
2731
0
        zend_ast_export_ex(str, ast->child[0], 0, indent);
2732
0
        smart_str_appends(str, ":\n");
2733
0
      } else {
2734
0
        smart_str_appends(str, "default:\n");
2735
0
      }
2736
0
      zend_ast_export_stmt(str, ast->child[1], indent + 1);
2737
0
      break;
2738
0
    case ZEND_AST_MATCH:
2739
0
      smart_str_appends(str, "match (");
2740
0
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2741
0
      smart_str_appends(str, ") {\n");
2742
0
      zend_ast_export_ex(str, ast->child[1], 0, indent + 1);
2743
0
      zend_ast_export_indent(str, indent);
2744
0
      smart_str_appendc(str, '}');
2745
0
      break;
2746
0
    case ZEND_AST_MATCH_ARM:
2747
0
      zend_ast_export_indent(str, indent);
2748
0
      if (ast->child[0]) {
2749
0
        zend_ast_export_list(str, zend_ast_get_list(ast->child[0]), true, 0, indent);
2750
0
        smart_str_appends(str, " => ");
2751
0
      } else {
2752
0
        smart_str_appends(str, "default => ");
2753
0
      }
2754
0
      zend_ast_export_ex(str, ast->child[1], 0, 0);
2755
0
      smart_str_appends(str, ",\n");
2756
0
      break;
2757
0
    case ZEND_AST_DECLARE:
2758
0
      smart_str_appends(str, "declare(");
2759
0
      ZEND_ASSERT(ast->child[0]->kind == ZEND_AST_CONST_DECL);
2760
0
      zend_ast_export_list(str, zend_ast_get_list(ast->child[0]), true, 0, indent);
2761
0
      smart_str_appendc(str, ')');
2762
0
      if (ast->child[1]) {
2763
0
        smart_str_appends(str, " {\n");
2764
0
        zend_ast_export_stmt(str, ast->child[1], indent + 1);
2765
0
        zend_ast_export_indent(str, indent);
2766
0
        smart_str_appendc(str, '}');
2767
0
      } else {
2768
0
        smart_str_appendc(str, ';');
2769
0
      }
2770
0
      break;
2771
0
    case ZEND_AST_PROP_ELEM:
2772
0
      smart_str_appendc(str, '$');
2773
0
      zend_ast_export_name(str, ast->child[0], 0, indent);
2774
2775
0
      zend_ast *default_value = ast->child[1];
2776
0
      if (default_value) {
2777
0
        smart_str_appends(str, " = ");
2778
0
        zend_ast_export_ex(str, default_value, 0, indent + 1);
2779
0
      }
2780
2781
0
      if (ast->child[3]) {
2782
0
        zend_ast_export_hook_list(str, zend_ast_get_list(ast->child[3]), indent);
2783
0
      }
2784
0
      break;
2785
1
    case ZEND_AST_CONST_ELEM:
2786
1
      zend_ast_export_name(str, ast->child[0], 0, indent);
2787
1
      APPEND_DEFAULT_VALUE(1);
2788
0
    case ZEND_AST_USE_TRAIT:
2789
0
      smart_str_appends(str, "use ");
2790
0
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2791
0
      if (ast->child[1]) {
2792
0
        smart_str_appends(str, " {\n");
2793
0
        zend_ast_export_ex(str, ast->child[1], 0, indent + 1);
2794
0
        zend_ast_export_indent(str, indent);
2795
0
        smart_str_appendc(str, '}');
2796
0
      } else {
2797
0
        smart_str_appendc(str, ';');
2798
0
      }
2799
0
      break;
2800
0
    case ZEND_AST_TRAIT_PRECEDENCE:
2801
0
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2802
0
      smart_str_appends(str, " insteadof ");
2803
0
      zend_ast_export_ex(str, ast->child[1], 0, indent);
2804
0
      break;
2805
0
    case ZEND_AST_TRAIT_METHOD_REFERENCE:
2806
0
      if (ast->child[0]) {
2807
0
        zend_ast_export_name(str, ast->child[0], 0, indent);
2808
0
        smart_str_appends(str, "::");
2809
0
      }
2810
0
      zend_ast_export_name(str, ast->child[1], 0, indent);
2811
0
      break;
2812
0
    case ZEND_AST_NAMESPACE:
2813
0
      smart_str_appends(str, "namespace");
2814
0
      if (ast->child[0]) {
2815
0
        smart_str_appendc(str, ' ');
2816
0
        zend_ast_export_name(str, ast->child[0], 0, indent);
2817
0
      }
2818
0
      if (ast->child[1]) {
2819
0
        smart_str_appends(str, " {\n");
2820
0
        zend_ast_export_stmt(str, ast->child[1], indent + 1);
2821
0
        zend_ast_export_indent(str, indent);
2822
0
        smart_str_appends(str, "}\n");
2823
0
      } else {
2824
0
        smart_str_appendc(str, ';');
2825
0
      }
2826
0
      break;
2827
0
    case ZEND_AST_USE_ELEM:
2828
0
    case ZEND_AST_TRAIT_ALIAS:
2829
0
      zend_ast_export_name(str, ast->child[0], 0, indent);
2830
0
      if (ast->attr & ZEND_ACC_PUBLIC) {
2831
0
        smart_str_appends(str, " as public");
2832
0
      } else if (ast->attr & ZEND_ACC_PROTECTED) {
2833
0
        smart_str_appends(str, " as protected");
2834
0
      } else if (ast->attr & ZEND_ACC_PRIVATE) {
2835
0
        smart_str_appends(str, " as private");
2836
0
      } else if (ast->child[1]) {
2837
0
        smart_str_appends(str, " as");
2838
0
      }
2839
0
      if (ast->child[1]) {
2840
0
        smart_str_appendc(str, ' ');
2841
0
        zend_ast_export_name(str, ast->child[1], 0, indent);
2842
0
      }
2843
0
      break;
2844
2
    case ZEND_AST_NAMED_ARG:
2845
2
      smart_str_append(str, zend_ast_get_str(ast->child[0]));
2846
2
      smart_str_appends(str, ": ");
2847
2
      ast = ast->child[1];
2848
2
      goto tail_call;
2849
2850
    /* 3 child nodes */
2851
0
    case ZEND_AST_METHOD_CALL:
2852
0
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2853
0
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2854
0
      smart_str_appends(str, ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL ? "?->" : "->");
2855
0
      zend_ast_export_var(str, ast->child[1], indent);
2856
0
      smart_str_appendc(str, '(');
2857
0
      zend_ast_export_ex(str, ast->child[2], 0, indent);
2858
0
      smart_str_appendc(str, ')');
2859
0
      break;
2860
0
    case ZEND_AST_STATIC_CALL:
2861
0
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2862
0
      smart_str_appends(str, "::");
2863
0
      zend_ast_export_var(str, ast->child[1], indent);
2864
0
      smart_str_appendc(str, '(');
2865
0
      zend_ast_export_ex(str, ast->child[2], 0, indent);
2866
0
      smart_str_appendc(str, ')');
2867
0
      break;
2868
3
    case ZEND_AST_CONDITIONAL:
2869
3
      if (priority > 100) smart_str_appendc(str, '(');
2870
3
      zend_ast_export_ex(str, ast->child[0], 100, indent);
2871
3
      if (ast->child[1]) {
2872
0
        smart_str_appends(str, " ? ");
2873
0
        zend_ast_export_ex(str, ast->child[1], 101, indent);
2874
0
        smart_str_appends(str, " : ");
2875
3
      } else {
2876
3
        smart_str_appends(str, " ?: ");
2877
3
      }
2878
3
      zend_ast_export_ex(str, ast->child[2], 101, indent);
2879
3
      if (priority > 100) smart_str_appendc(str, ')');
2880
3
      break;
2881
2882
0
    case ZEND_AST_TRY:
2883
0
      smart_str_appends(str, "try {\n");
2884
0
      zend_ast_export_stmt(str, ast->child[0], indent + 1);
2885
0
      zend_ast_export_indent(str, indent);
2886
0
      zend_ast_export_ex(str, ast->child[1], 0, indent);
2887
0
      if (ast->child[2]) {
2888
0
        smart_str_appends(str, "} finally {\n");
2889
0
        zend_ast_export_stmt(str, ast->child[2], indent + 1);
2890
0
        zend_ast_export_indent(str, indent);
2891
0
      }
2892
0
      smart_str_appendc(str, '}');
2893
0
      break;
2894
0
    case ZEND_AST_CATCH:
2895
0
      smart_str_appends(str, "} catch (");
2896
0
      zend_ast_export_catch_name_list(str, zend_ast_get_list(ast->child[0]), indent);
2897
0
      if (ast->child[1]) {
2898
0
        smart_str_appends(str, " $");
2899
0
        zend_ast_export_var(str, ast->child[1], indent);
2900
0
      }
2901
0
      smart_str_appends(str, ") {\n");
2902
0
      zend_ast_export_stmt(str, ast->child[2], indent + 1);
2903
0
      zend_ast_export_indent(str, indent);
2904
0
      break;
2905
2
    case ZEND_AST_PARAM:
2906
2
      if (ast->child[3]) {
2907
0
        zend_ast_export_attributes(str, ast->child[3], indent, false);
2908
0
      }
2909
2
      zend_ast_export_visibility(str, ast->attr, ZEND_MODIFIER_TARGET_CPP);
2910
2
      if (ast->attr & ZEND_ACC_FINAL) {
2911
1
        smart_str_appends(str, "final ");
2912
1
      }
2913
2
      if (ast->child[0]) {
2914
1
        zend_ast_export_type(str, ast->child[0], indent);
2915
1
        smart_str_appendc(str, ' ');
2916
1
      }
2917
2
      if (ast->attr & ZEND_PARAM_REF) {
2918
0
        smart_str_appendc(str, '&');
2919
0
      }
2920
2
      if (ast->attr & ZEND_PARAM_VARIADIC) {
2921
0
        smart_str_appends(str, "...");
2922
0
      }
2923
2
      smart_str_appendc(str, '$');
2924
2
      zend_ast_export_name(str, ast->child[1], 0, indent);
2925
2
      if (ast->child[2]) {
2926
0
        smart_str_appends(str, " = ");
2927
0
        zend_ast_export_ex(str, ast->child[2], 0, indent);
2928
0
      }
2929
2
      if (ast->child[5]) {
2930
0
        zend_ast_export_hook_list(str, zend_ast_get_list(ast->child[5]), indent);
2931
0
      }
2932
2
      break;
2933
0
    case ZEND_AST_ENUM_CASE:
2934
0
      if (ast->child[3]) {
2935
0
        zend_ast_export_attributes(str, ast->child[3], indent, true);
2936
0
      }
2937
0
      smart_str_appends(str, "case ");
2938
0
      zend_ast_export_name(str, ast->child[0], 0, indent);
2939
0
      if (ast->child[1]) {
2940
0
        smart_str_appends(str, " = ");
2941
0
        zend_ast_export_ex(str, ast->child[1], 0, indent);
2942
0
      }
2943
0
      break;
2944
2945
    /* 4 child nodes */
2946
0
    case ZEND_AST_FOR:
2947
0
      smart_str_appends(str, "for (");
2948
0
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2949
0
      smart_str_appendc(str, ';');
2950
0
      if (ast->child[1]) {
2951
0
        smart_str_appendc(str, ' ');
2952
0
        zend_ast_export_ex(str, ast->child[1], 0, indent);
2953
0
      }
2954
0
      smart_str_appendc(str, ';');
2955
0
      if (ast->child[2]) {
2956
0
        smart_str_appendc(str, ' ');
2957
0
        zend_ast_export_ex(str, ast->child[2], 0, indent);
2958
0
      }
2959
0
      smart_str_appends(str, ") {\n");
2960
0
      zend_ast_export_stmt(str, ast->child[3], indent + 1);
2961
0
      zend_ast_export_indent(str, indent);
2962
0
      smart_str_appendc(str, '}');
2963
0
      break;
2964
0
    case ZEND_AST_FOREACH:
2965
0
      smart_str_appends(str, "foreach (");
2966
0
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2967
0
      smart_str_appends(str, " as ");
2968
0
      if (ast->child[2]) {
2969
0
        zend_ast_export_ex(str, ast->child[2], 0, indent);
2970
0
        smart_str_appends(str, " => ");
2971
0
      }
2972
0
      zend_ast_export_ex(str, ast->child[1], 0, indent);
2973
0
      smart_str_appends(str, ") {\n");
2974
0
      zend_ast_export_stmt(str, ast->child[3], indent + 1);
2975
0
      zend_ast_export_indent(str, indent);
2976
0
      smart_str_appendc(str, '}');
2977
0
      break;
2978
0
    default: ZEND_UNREACHABLE();
2979
537
  }
2980
404
  return;
2981
2982
404
binary_op:
2983
31
  if (priority > p) smart_str_appendc(str, '(');
2984
31
  zend_ast_export_ex(str, ast->child[0], pl, indent);
2985
31
  smart_str_appends(str, op);
2986
31
  zend_ast_export_ex(str, ast->child[1], pr, indent);
2987
31
  if (priority > p) smart_str_appendc(str, ')');
2988
31
  return;
2989
2990
94
prefix_op:
2991
94
  if (priority > p) smart_str_appendc(str, '(');
2992
94
  smart_str_appends(str, op);
2993
94
  zend_ast_export_ex(str, ast->child[0], pl, indent);
2994
94
  if (priority > p) smart_str_appendc(str, ')');
2995
94
  return;
2996
2997
0
postfix_op:
2998
0
  if (priority > p) smart_str_appendc(str, '(');
2999
0
  zend_ast_export_ex(str, ast->child[0], pl, indent);
3000
0
  smart_str_appends(str, op);
3001
0
  if (priority > p) smart_str_appendc(str, ')');
3002
0
  return;
3003
3004
0
func_op:
3005
0
  smart_str_appends(str, op);
3006
0
  smart_str_appendc(str, '(');
3007
0
  zend_ast_export_ex(str, ast->child[0], 0, indent);
3008
0
  smart_str_appendc(str, ')');
3009
0
  return;
3010
3011
2
append_node_1:
3012
2
  smart_str_appends(str, op);
3013
2
  if (ast->child[0]) {
3014
2
    smart_str_appendc(str, ' ');
3015
2
    ast = ast->child[0];
3016
2
    goto tail_call;
3017
2
  }
3018
0
  return;
3019
3020
3
append_str:
3021
3
  smart_str_appends(str, op);
3022
3
  return;
3023
3024
1
append_default_value:
3025
1
  if (ast->child[p]) {
3026
1
    smart_str_appends(str, " = ");
3027
1
    ast = ast->child[p];
3028
1
    goto tail_call;
3029
1
  }
3030
0
  return;
3031
1
}
3032
3033
ZEND_API ZEND_COLD zend_string *zend_ast_export(const char *prefix, zend_ast *ast, const char *suffix)
3034
99
{
3035
99
  smart_str str = {0};
3036
3037
99
  smart_str_appends(&str, prefix);
3038
99
  zend_ast_export_ex(&str, ast, 0, 0);
3039
99
  smart_str_appends(&str, suffix);
3040
99
  smart_str_0(&str);
3041
99
  return str.s;
3042
99
}
3043
3044
zend_ast * ZEND_FASTCALL zend_ast_with_attributes(zend_ast *ast, zend_ast *attr)
3045
142
{
3046
142
  ZEND_ASSERT(attr->kind == ZEND_AST_ATTRIBUTE_LIST);
3047
3048
142
  switch (ast->kind) {
3049
21
  case ZEND_AST_FUNC_DECL:
3050
21
  case ZEND_AST_CLOSURE:
3051
36
  case ZEND_AST_METHOD:
3052
36
  case ZEND_AST_ARROW_FUNC:
3053
41
  case ZEND_AST_PROPERTY_HOOK:
3054
41
    ((zend_ast_decl *) ast)->child[4] = attr;
3055
41
    break;
3056
60
  case ZEND_AST_CLASS:
3057
60
    ((zend_ast_decl *) ast)->child[3] = attr;
3058
60
    break;
3059
10
  case ZEND_AST_PROP_GROUP:
3060
10
    ast->child[2] = attr;
3061
10
    break;
3062
9
  case ZEND_AST_PARAM:
3063
13
  case ZEND_AST_ENUM_CASE:
3064
13
    ast->child[3] = attr;
3065
13
    break;
3066
2
  case ZEND_AST_CLASS_CONST_GROUP:
3067
2
    ast->child[1] = attr;
3068
2
    break;
3069
16
  case ZEND_AST_CONST_DECL:
3070
    /* Since constants are already stored in a list, just add the attributes
3071
     * to that list instead of storing them elsewhere;
3072
     * zend_compile_const_decl() checks the kind of the list elements. */
3073
16
    ast = zend_ast_list_add(ast, attr);
3074
16
    break;
3075
0
  default: ZEND_UNREACHABLE();
3076
142
  }
3077
3078
142
  return ast;
3079
142
}
3080
3081
zend_ast * ZEND_FASTCALL zend_ast_call_get_args(zend_ast *ast)
3082
19
{
3083
19
  if (ast->kind == ZEND_AST_CALL) {
3084
16
    return ast->child[1];
3085
16
  } else if (ast->kind == ZEND_AST_STATIC_CALL || ast->kind == ZEND_AST_METHOD_CALL) {
3086
3
    return ast->child[2];
3087
3
  }
3088
3089
0
  ZEND_UNREACHABLE();
3090
0
  return NULL;
3091
0
}