Coverage Report

Created: 2025-12-14 06:09

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