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.55M
static inline void *zend_ast_alloc(size_t size) {
33
3.55M
  return zend_arena_alloc(&CG(ast_arena), size);
34
3.55M
}
35
36
59.4k
static inline void *zend_ast_realloc(const void *old, size_t old_size, size_t new_size) {
37
59.4k
  void *new = zend_ast_alloc(new_size);
38
59.4k
  memcpy(new, old, old_size);
39
59.4k
  return new;
40
59.4k
}
41
42
582k
static inline size_t zend_ast_list_size(uint32_t children) {
43
582k
  return sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * children;
44
582k
}
45
46
5.31k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_znode(const znode *node) {
47
5.31k
  zend_ast_znode *ast;
48
49
5.31k
  ast = zend_ast_alloc(sizeof(zend_ast_znode));
50
5.31k
  ast->kind = ZEND_AST_ZNODE;
51
5.31k
  ast->attr = 0;
52
5.31k
  ast->lineno = CG(zend_lineno);
53
5.31k
  ast->node = *node;
54
5.31k
  return (zend_ast *) ast;
55
5.31k
}
56
57
565
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_fcc(void) {
58
565
  zend_ast_fcc *ast;
59
60
565
  ast = zend_ast_alloc(sizeof(zend_ast_fcc));
61
565
  ast->kind = ZEND_AST_CALLABLE_CONVERT;
62
565
  ast->attr = 0;
63
565
  ast->lineno = CG(zend_lineno);
64
565
  ZEND_MAP_PTR_INIT(ast->fptr, NULL);
65
66
565
  return (zend_ast *) ast;
67
565
}
68
69
1.42M
static zend_always_inline zend_ast * zend_ast_create_zval_int(const zval *zv, uint32_t attr, uint32_t lineno) {
70
1.42M
  zend_ast_zval *ast;
71
72
1.42M
  ast = zend_ast_alloc(sizeof(zend_ast_zval));
73
1.42M
  ast->kind = ZEND_AST_ZVAL;
74
1.42M
  ast->attr = attr;
75
1.42M
  ZVAL_COPY_VALUE(&ast->val, zv);
76
1.42M
  Z_LINENO(ast->val) = lineno;
77
1.42M
  return (zend_ast *) ast;
78
1.42M
}
79
80
1.36M
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_with_lineno(const zval *zv, uint32_t lineno) {
81
1.36M
  return zend_ast_create_zval_int(zv, 0, lineno);
82
1.36M
}
83
84
397
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_ex(const zval *zv, zend_ast_attr attr) {
85
397
  return zend_ast_create_zval_int(zv, attr, CG(zend_lineno));
86
397
}
87
88
34.4k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval(const zval *zv) {
89
34.4k
  return zend_ast_create_zval_int(zv, 0, CG(zend_lineno));
90
34.4k
}
91
92
2.51k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_str(zend_string *str) {
93
2.51k
  zval zv;
94
2.51k
  ZVAL_STR(&zv, str);
95
2.51k
  return zend_ast_create_zval_int(&zv, 0, CG(zend_lineno));
96
2.51k
}
97
98
19.4k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_long(zend_long lval) {
99
19.4k
  zval zv;
100
19.4k
  ZVAL_LONG(&zv, lval);
101
19.4k
  return zend_ast_create_zval_int(&zv, 0, CG(zend_lineno));
102
19.4k
}
103
104
1.56k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_constant(zend_string *name, zend_ast_attr attr) {
105
1.56k
  zend_ast_zval *ast;
106
107
1.56k
  ast = zend_ast_alloc(sizeof(zend_ast_zval));
108
1.56k
  ast->kind = ZEND_AST_CONSTANT;
109
1.56k
  ast->attr = attr;
110
1.56k
  ZVAL_STR(&ast->val, name);
111
1.56k
  Z_LINENO(ast->val) = CG(zend_lineno);
112
1.56k
  return (zend_ast *) ast;
113
1.56k
}
114
115
28
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_op_array(zend_op_array *op_array) {
116
28
  zend_ast_op_array *ast;
117
118
28
  ast = zend_ast_alloc(sizeof(zend_ast_op_array));
119
28
  ast->kind = ZEND_AST_OP_ARRAY;
120
28
  ast->attr = 0;
121
28
  ast->lineno = CG(zend_lineno);
122
28
  ast->op_array = op_array;
123
124
28
  return (zend_ast *) ast;
125
28
}
126
127
5.93k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_class_const_or_name(zend_ast *class_name, zend_ast *name) {
128
5.93k
  zend_string *name_str = zend_ast_get_str(name);
129
5.93k
  if (zend_string_equals_ci(name_str, ZSTR_KNOWN(ZEND_STR_CLASS))) {
130
2.30k
    zend_string_release(name_str);
131
2.30k
    return zend_ast_create(ZEND_AST_CLASS_NAME, class_name);
132
3.63k
  } else {
133
3.63k
    return zend_ast_create(ZEND_AST_CLASS_CONST, class_name, name);
134
3.63k
  }
135
5.93k
}
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
39.1k
) {
141
39.1k
  zend_ast_decl *ast;
142
143
39.1k
  ast = zend_ast_alloc(sizeof(zend_ast_decl));
144
39.1k
  ast->kind = kind;
145
39.1k
  ast->attr = 0;
146
39.1k
  ast->start_lineno = start_lineno;
147
39.1k
  ast->end_lineno = CG(zend_lineno);
148
39.1k
  ast->flags = flags;
149
39.1k
  ast->doc_comment = doc_comment;
150
39.1k
  ast->name = name;
151
39.1k
  ast->child[0] = child0;
152
39.1k
  ast->child[1] = child1;
153
39.1k
  ast->child[2] = child2;
154
39.1k
  ast->child[3] = child3;
155
39.1k
  ast->child[4] = child4;
156
157
39.1k
  return (zend_ast *) ast;
158
39.1k
}
159
160
#if ZEND_AST_SPEC
161
6.40k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_0(zend_ast_kind kind) {
162
6.40k
  zend_ast *ast;
163
164
6.40k
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 0);
165
6.40k
  ast = zend_ast_alloc(zend_ast_size(0));
166
6.40k
  ast->kind = kind;
167
6.40k
  ast->attr = 0;
168
6.40k
  ast->lineno = CG(zend_lineno);
169
170
6.40k
  return ast;
171
6.40k
}
172
173
714k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_1(zend_ast_kind kind, zend_ast *child) {
174
714k
  zend_ast *ast;
175
714k
  uint32_t lineno;
176
177
714k
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 1);
178
714k
  ast = zend_ast_alloc(zend_ast_size(1));
179
714k
  ast->kind = kind;
180
714k
  ast->attr = 0;
181
714k
  ast->child[0] = child;
182
714k
  if (child) {
183
713k
    lineno = zend_ast_get_lineno(child);
184
713k
  } else {
185
571
    lineno = CG(zend_lineno);
186
571
  }
187
714k
  ast->lineno = lineno;
188
189
714k
  return ast;
190
714k
}
191
192
724k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_2(zend_ast_kind kind, zend_ast *child1, zend_ast *child2) {
193
724k
  zend_ast *ast;
194
724k
  uint32_t lineno;
195
196
724k
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 2);
197
724k
  ast = zend_ast_alloc(zend_ast_size(2));
198
724k
  ast->kind = kind;
199
724k
  ast->attr = 0;
200
724k
  ast->child[0] = child1;
201
724k
  ast->child[1] = child2;
202
724k
  if (child1) {
203
723k
    lineno = zend_ast_get_lineno(child1);
204
723k
  } else if (child2) {
205
1.22k
    lineno = zend_ast_get_lineno(child2);
206
1.22k
  } else {
207
354
    lineno = CG(zend_lineno);
208
354
  }
209
724k
  ast->lineno = lineno;
210
211
724k
  return ast;
212
724k
}
213
214
82.1k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_3(zend_ast_kind kind, zend_ast *child1, zend_ast *child2, zend_ast *child3) {
215
82.1k
  zend_ast *ast;
216
82.1k
  uint32_t lineno;
217
218
82.1k
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 3);
219
82.1k
  ast = zend_ast_alloc(zend_ast_size(3));
220
82.1k
  ast->kind = kind;
221
82.1k
  ast->attr = 0;
222
82.1k
  ast->child[0] = child1;
223
82.1k
  ast->child[1] = child2;
224
82.1k
  ast->child[2] = child3;
225
82.1k
  if (child1) {
226
77.3k
    lineno = zend_ast_get_lineno(child1);
227
77.3k
  } else if (child2) {
228
4.76k
    lineno = zend_ast_get_lineno(child2);
229
4.76k
  } else if (child3) {
230
0
    lineno = zend_ast_get_lineno(child3);
231
0
  } else {
232
0
    lineno = CG(zend_lineno);
233
0
  }
234
82.1k
  ast->lineno = lineno;
235
236
82.1k
  return ast;
237
82.1k
}
238
239
20.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
20.8k
  zend_ast *ast;
241
20.8k
  uint32_t lineno;
242
243
20.8k
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 4);
244
20.8k
  ast = zend_ast_alloc(zend_ast_size(4));
245
20.8k
  ast->kind = kind;
246
20.8k
  ast->attr = 0;
247
20.8k
  ast->child[0] = child1;
248
20.8k
  ast->child[1] = child2;
249
20.8k
  ast->child[2] = child3;
250
20.8k
  ast->child[3] = child4;
251
20.8k
  if (child1) {
252
20.6k
    lineno = zend_ast_get_lineno(child1);
253
20.6k
  } else if (child2) {
254
128
    lineno = zend_ast_get_lineno(child2);
255
128
  } else if (child3) {
256
10
    lineno = zend_ast_get_lineno(child3);
257
41
  } else if (child4) {
258
41
    lineno = zend_ast_get_lineno(child4);
259
41
  } else {
260
0
    lineno = CG(zend_lineno);
261
0
  }
262
20.8k
  ast->lineno = lineno;
263
264
20.8k
  return ast;
265
20.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
14.9k
    zend_ast_kind kind, zend_ast_attr attr, va_list *va) {
300
14.9k
  uint32_t lineno = (uint32_t)-1;
301
14.9k
  uint32_t children = kind >> ZEND_AST_NUM_CHILDREN_SHIFT;
302
14.9k
  zend_ast *ast = zend_ast_alloc(zend_ast_size(children));
303
14.9k
  ast->kind = kind;
304
14.9k
  ast->attr = attr;
305
104k
  for (uint32_t i = 0; i < children; i++) {
306
89.4k
    ast->child[i] = va_arg(*va, zend_ast *);
307
89.4k
    if (lineno == (uint32_t)-1 && ast->child[i]) {
308
14.9k
      lineno = zend_ast_get_lineno(ast->child[i]);
309
14.9k
    }
310
89.4k
  }
311
14.9k
  if (lineno == (uint32_t)-1) {
312
0
    lineno = CG(zend_lineno);
313
0
  }
314
14.9k
  ast->lineno = lineno;
315
14.9k
  return ast;
316
14.9k
}
317
318
344
ZEND_API zend_ast * zend_ast_create_n(unsigned kind, ...) {
319
344
  va_list va;
320
344
  va_start(va, kind);
321
344
  zend_ast *ast = zend_ast_create_va(kind, 0, &va);
322
344
  va_end(va);
323
344
  return ast;
324
344
}
325
326
ZEND_API zend_ast * zend_ast_create_ex_n(
327
14.5k
    zend_ast_kind kind, unsigned attr, ...) {
328
14.5k
  va_list va;
329
14.5k
  va_start(va, attr);
330
14.5k
  zend_ast *ast = zend_ast_create_va(kind, attr, &va);
331
14.5k
  va_end(va);
332
14.5k
  return ast;
333
14.5k
}
334
335
214k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_0(zend_ast_kind kind) {
336
214k
  zend_ast *ast;
337
214k
  zend_ast_list *list;
338
339
214k
  ast = zend_ast_alloc(zend_ast_list_size(4));
340
214k
  list = (zend_ast_list *) ast;
341
214k
  list->kind = kind;
342
214k
  list->attr = 0;
343
214k
  list->lineno = CG(zend_lineno);
344
214k
  list->children = 0;
345
346
214k
  return ast;
347
214k
}
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
230k
    lineno = zend_ast_get_lineno(child);
362
230k
    if (lineno > CG(zend_lineno)) {
363
58
      lineno = CG(zend_lineno);
364
58
    }
365
230k
  } else {
366
2.43k
    lineno = CG(zend_lineno);
367
2.43k
  }
368
232k
  list->lineno = lineno;
369
370
232k
  return ast;
371
232k
}
372
373
15.5k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_2(zend_ast_kind kind, zend_ast *child1, zend_ast *child2) {
374
15.5k
  zend_ast *ast;
375
15.5k
  zend_ast_list *list;
376
15.5k
  uint32_t lineno;
377
378
15.5k
  ast = zend_ast_alloc(zend_ast_list_size(4));
379
15.5k
  list = (zend_ast_list *) ast;
380
15.5k
  list->kind = kind;
381
15.5k
  list->attr = 0;
382
15.5k
  list->children = 2;
383
15.5k
  list->child[0] = child1;
384
15.5k
  list->child[1] = child2;
385
15.5k
  if (child1) {
386
15.5k
    lineno = zend_ast_get_lineno(child1);
387
15.5k
    if (lineno > CG(zend_lineno)) {
388
0
      lineno = CG(zend_lineno);
389
0
    }
390
15.5k
  } 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
15.5k
  list->lineno = lineno;
400
401
15.5k
  return ast;
402
15.5k
}
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.7k
zend_ast *zend_ast_create_concat_op(zend_ast *op0, zend_ast *op1) {
485
41.7k
  if (op0->kind == ZEND_AST_ZVAL && op1->kind == ZEND_AST_ZVAL) {
486
8.07k
    zval *zv0 = zend_ast_get_zval(op0);
487
8.07k
    zval *zv1 = zend_ast_get_zval(op1);
488
8.07k
    if (!zend_binary_op_produces_error(ZEND_CONCAT, zv0, zv1) &&
489
8.07k
        concat_function(zv0, zv0, zv1) == SUCCESS) {
490
8.07k
      zval_ptr_dtor_nogc(zv1);
491
8.07k
      return zend_ast_create_zval(zv0);
492
8.07k
    }
493
8.07k
  }
494
33.7k
  return zend_ast_create_binary_op(ZEND_CONCAT, op0, op1);
495
41.7k
}
496
497
427k
static inline bool is_power_of_two(uint32_t n) {
498
427k
  return ((n != 0) && (n == (n & (~n + 1))));
499
427k
}
500
501
798k
ZEND_ATTRIBUTE_NODISCARD ZEND_API zend_ast * ZEND_FASTCALL zend_ast_list_add(zend_ast *ast, zend_ast *op) {
502
798k
  zend_ast_list *list = zend_ast_get_list(ast);
503
798k
  if (list->children >= 4 && is_power_of_two(list->children)) {
504
59.4k
      list = zend_ast_realloc(list,
505
59.4k
      zend_ast_list_size(list->children), zend_ast_list_size(list->children * 2));
506
59.4k
  }
507
798k
  list->child[list->children++] = op;
508
798k
  return (zend_ast *) list;
509
798k
}
510
511
static zend_result zend_ast_add_array_element(const zval *result, zval *offset, zval *expr)
512
226
{
513
226
  if (Z_TYPE_P(offset) == IS_UNDEF) {
514
46
    if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), expr)) {
515
2
      zend_throw_error(NULL,
516
2
        "Cannot add element to the array as the next element is already occupied");
517
2
      return FAILURE;
518
2
    }
519
44
    return SUCCESS;
520
46
  }
521
522
180
  if (array_set_zval_key(Z_ARRVAL_P(result), offset, expr) == FAILURE) {
523
4
    return FAILURE;
524
4
  }
525
526
176
  zval_ptr_dtor_nogc(offset);
527
176
  zval_ptr_dtor_nogc(expr);
528
176
  return SUCCESS;
529
180
}
530
531
6
static zend_result zend_ast_add_unpacked_element(const zval *result, const zval *expr) {
532
6
  if (EXPECTED(Z_TYPE_P(expr) == IS_ARRAY)) {
533
4
    const HashTable *ht = Z_ARRVAL_P(expr);
534
4
    zval *val;
535
4
    zend_string *key;
536
537
24
    ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
538
24
      if (key) {
539
0
        zend_hash_update(Z_ARRVAL_P(result), key, val);
540
10
      } else {
541
10
        if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
542
2
          zend_throw_error(NULL,
543
2
            "Cannot add element to the array as the next element is already occupied");
544
2
          return FAILURE;
545
2
        }
546
10
      }
547
8
      Z_TRY_ADDREF_P(val);
548
8
    } ZEND_HASH_FOREACH_END();
549
2
    return SUCCESS;
550
4
  }
551
552
2
  zend_throw_error(NULL, "Only arrays can be unpacked in constant expression");
553
2
  return FAILURE;
554
6
}
555
556
static zend_class_entry *zend_ast_fetch_class(zend_ast *ast, zend_class_entry *scope)
557
152
{
558
152
  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
152
}
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
4.52k
) {
576
4.52k
  zend_string *previous_filename;
577
4.52k
  zend_long previous_lineno;
578
4.52k
  if (scope) {
579
2.88k
    previous_filename = EG(filename_override);
580
2.88k
    previous_lineno = EG(lineno_override);
581
2.88k
    EG(filename_override) = scope->info.user.filename;
582
2.88k
    EG(lineno_override) = zend_ast_get_lineno(ast);
583
2.88k
  }
584
4.52k
  zend_result r = zend_ast_evaluate_inner(result, ast, scope, short_circuited_ptr, ctx);
585
4.52k
  if (scope) {
586
2.88k
    EG(filename_override) = previous_filename;
587
2.88k
    EG(lineno_override) = previous_lineno;
588
2.88k
  }
589
4.52k
  return r;
590
4.52k
}
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
4.52k
) {
599
4.52k
  zval op1, op2;
600
4.52k
  zend_result ret = SUCCESS;
601
4.52k
  bool short_circuited;
602
4.52k
  *short_circuited_ptr = false;
603
604
4.52k
  switch (ast->kind) {
605
206
    case ZEND_AST_BINARY_OP:
606
206
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
607
50
        ret = FAILURE;
608
156
      } else if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
609
10
        zval_ptr_dtor_nogc(&op1);
610
10
        ret = FAILURE;
611
146
      } else {
612
146
        binary_op_type op = get_binary_op(ast->attr);
613
146
        op(result, &op1, &op2);
614
146
        zval_ptr_dtor_nogc(&op1);
615
146
        zval_ptr_dtor_nogc(&op2);
616
146
        ret = EG(exception) ? FAILURE : SUCCESS;
617
146
      }
618
206
      break;
619
18
    case ZEND_AST_GREATER:
620
32
    case ZEND_AST_GREATER_EQUAL:
621
32
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
622
2
        ret = FAILURE;
623
30
      } else if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
624
0
        zval_ptr_dtor_nogc(&op1);
625
0
        ret = FAILURE;
626
30
      } else {
627
        /* op1 > op2 is the same as op2 < op1 */
628
30
        binary_op_type op = ast->kind == ZEND_AST_GREATER
629
30
          ? is_smaller_function : is_smaller_or_equal_function;
630
30
        op(result, &op2, &op1);
631
30
        zval_ptr_dtor_nogc(&op1);
632
30
        zval_ptr_dtor_nogc(&op2);
633
30
        ret = EG(exception) ? FAILURE : SUCCESS;
634
30
      }
635
32
      break;
636
16
    case ZEND_AST_UNARY_OP:
637
16
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
638
0
        ret = FAILURE;
639
16
      } else {
640
16
        unary_op_type op = get_unary_op(ast->attr);
641
16
        ret = op(result, &op1);
642
16
        zval_ptr_dtor_nogc(&op1);
643
16
      }
644
16
      break;
645
1.49k
    case ZEND_AST_ZVAL:
646
1.49k
    {
647
1.49k
      zval *zv = zend_ast_get_zval(ast);
648
649
1.49k
      ZVAL_COPY(result, zv);
650
1.49k
      break;
651
18
    }
652
524
    case ZEND_AST_CONSTANT:
653
524
    {
654
524
      zend_string *name = zend_ast_get_constant_name(ast);
655
524
      zval *zv = zend_get_constant_ex(name, scope, ast->attr);
656
657
524
      if (UNEXPECTED(zv == NULL)) {
658
54
        ZVAL_UNDEF(result);
659
54
        return FAILURE;
660
54
      }
661
470
      ZVAL_COPY_OR_DUP(result, zv);
662
470
      break;
663
524
    }
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
32
    case ZEND_AST_CLASS_NAME:
672
32
      if (!scope) {
673
2
        zend_throw_error(NULL, "Cannot use \"self\" when no class scope is active");
674
2
        return FAILURE;
675
2
      }
676
30
      if (ast->attr == ZEND_FETCH_CLASS_SELF) {
677
30
        ZVAL_STR_COPY(result, scope->name);
678
30
      } 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
30
      break;
689
30
    case ZEND_AST_AND:
690
22
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
691
2
        ret = FAILURE;
692
2
        break;
693
2
      }
694
20
      if (zend_is_true(&op1)) {
695
10
        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
10
        ZVAL_BOOL(result, zend_is_true(&op2));
701
10
        zval_ptr_dtor_nogc(&op2);
702
10
      } else {
703
10
        ZVAL_FALSE(result);
704
10
      }
705
20
      zval_ptr_dtor_nogc(&op1);
706
20
      break;
707
80
    case ZEND_AST_CAST:
708
80
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
709
4
        ret = FAILURE;
710
4
        break;
711
4
      }
712
76
      if (ast->attr == Z_TYPE(op1)) {
713
0
        ZVAL_COPY_VALUE(result, &op1);
714
76
      } else {
715
76
        switch (ast->attr) {
716
4
          case _IS_BOOL:
717
4
            ZVAL_BOOL(result, zend_is_true(&op1));
718
4
            break;
719
12
          case IS_LONG:
720
12
            ZVAL_LONG(result, zval_get_long_func(&op1, false));
721
12
            break;
722
4
          case IS_DOUBLE:
723
4
            ZVAL_DOUBLE(result, zval_get_double_func(&op1));
724
4
            break;
725
14
          case IS_STRING:
726
14
            ZVAL_STR(result, zval_get_string_func(&op1));
727
14
            break;
728
30
          case IS_ARRAY:
729
30
            zend_cast_zval_to_array(result, &op1, IS_VAR);
730
30
            break;
731
12
          case IS_OBJECT:
732
12
            zend_cast_zval_to_object(result, &op1, IS_VAR);
733
12
            break;
734
0
          EMPTY_SWITCH_DEFAULT_CASE();
735
76
        }
736
76
        zval_ptr_dtor_nogc(&op1);
737
76
        if (UNEXPECTED(EG(exception))) {
738
0
          ret = FAILURE;
739
0
        }
740
76
      }
741
76
      break;
742
76
    case ZEND_AST_OR:
743
20
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
744
2
        ret = FAILURE;
745
2
        break;
746
2
      }
747
18
      if (zend_is_true(&op1)) {
748
10
        ZVAL_TRUE(result);
749
10
      } else {
750
8
        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
8
        ZVAL_BOOL(result, zend_is_true(&op2));
756
8
        zval_ptr_dtor_nogc(&op2);
757
8
      }
758
18
      zval_ptr_dtor_nogc(&op1);
759
18
      break;
760
40
    case ZEND_AST_CONDITIONAL:
761
40
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
762
4
        ret = FAILURE;
763
4
        break;
764
4
      }
765
36
      if (zend_is_true(&op1)) {
766
22
        if (!ast->child[1]) {
767
8
          *result = op1;
768
14
        } else {
769
14
          if (UNEXPECTED(zend_ast_evaluate_ex(result, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
770
0
            zval_ptr_dtor_nogc(&op1);
771
0
            ret = FAILURE;
772
0
            break;
773
0
          }
774
14
          zval_ptr_dtor_nogc(&op1);
775
14
        }
776
22
      } else {
777
14
        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
14
        zval_ptr_dtor_nogc(&op1);
783
14
      }
784
36
      break;
785
36
    case ZEND_AST_COALESCE:
786
18
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
787
2
        ret = FAILURE;
788
2
        break;
789
2
      }
790
16
      if (Z_TYPE(op1) > IS_NULL) {
791
0
        *result = op1;
792
16
      } else {
793
16
        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
16
        zval_ptr_dtor_nogc(&op1);
799
16
      }
800
16
      break;
801
32
    case ZEND_AST_UNARY_PLUS:
802
32
      if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
803
0
        ret = FAILURE;
804
32
      } else {
805
32
        ZVAL_LONG(&op1, 0);
806
32
        ret = add_function(result, &op1, &op2);
807
32
        zval_ptr_dtor_nogc(&op2);
808
32
      }
809
32
      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
0
        ret = FAILURE;
813
18
      } else {
814
18
        ZVAL_LONG(&op1, -1);
815
18
        ret = mul_function(result, &op1, &op2);
816
18
        zval_ptr_dtor_nogc(&op2);
817
18
      }
818
18
      break;
819
216
    case ZEND_AST_ARRAY:
820
216
      {
821
216
        uint32_t i;
822
216
        zend_ast_list *list = zend_ast_get_list(ast);
823
824
216
        if (!list->children) {
825
0
          ZVAL_EMPTY_ARRAY(result);
826
0
          break;
827
0
        }
828
216
        array_init(result);
829
438
        for (i = 0; i < list->children; i++) {
830
282
          zend_ast *elem = list->child[i];
831
282
          if (elem->kind == ZEND_AST_UNPACK) {
832
18
            if (UNEXPECTED(zend_ast_evaluate_ex(&op1, elem->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
833
12
              zval_ptr_dtor_nogc(result);
834
12
              return FAILURE;
835
12
            }
836
6
            if (UNEXPECTED(zend_ast_add_unpacked_element(result, &op1) != SUCCESS)) {
837
4
              zval_ptr_dtor_nogc(&op1);
838
4
              zval_ptr_dtor_nogc(result);
839
4
              return FAILURE;
840
4
            }
841
2
            zval_ptr_dtor_nogc(&op1);
842
2
            continue;
843
6
          }
844
264
          if (elem->child[1]) {
845
204
            if (UNEXPECTED(zend_ast_evaluate_ex(&op1, elem->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
846
12
              zval_ptr_dtor_nogc(result);
847
12
              return FAILURE;
848
12
            }
849
204
          } else {
850
60
            ZVAL_UNDEF(&op1);
851
60
          }
852
252
          if (UNEXPECTED(zend_ast_evaluate_ex(&op2, elem->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
853
26
            zval_ptr_dtor_nogc(&op1);
854
26
            zval_ptr_dtor_nogc(result);
855
26
            return FAILURE;
856
26
          }
857
226
          if (UNEXPECTED(zend_ast_add_array_element(result, &op1, &op2) != SUCCESS)) {
858
6
            zval_ptr_dtor_nogc(&op1);
859
6
            zval_ptr_dtor_nogc(&op2);
860
6
            zval_ptr_dtor_nogc(result);
861
6
            return FAILURE;
862
6
          }
863
226
        }
864
216
      }
865
156
      break;
866
156
    case ZEND_AST_DIM:
867
88
      if (ast->child[1] == NULL) {
868
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
869
0
      }
870
871
88
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
872
2
        ret = FAILURE;
873
2
        break;
874
2
      }
875
86
      if (short_circuited) {
876
4
        *short_circuited_ptr = true;
877
4
        ZVAL_NULL(result);
878
4
        return SUCCESS;
879
4
      }
880
881
      // DIM on objects is disallowed because it allows executing arbitrary expressions
882
82
      if (Z_TYPE(op1) == IS_OBJECT) {
883
2
        zval_ptr_dtor_nogc(&op1);
884
2
        zend_throw_error(NULL, "Cannot use [] on objects in constant expression");
885
2
        ret = FAILURE;
886
2
        break;
887
2
      }
888
889
80
      if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
890
2
        zval_ptr_dtor_nogc(&op1);
891
2
        ret = FAILURE;
892
2
        break;
893
2
      }
894
895
78
      zend_fetch_dimension_const(result, &op1, &op2, (ast->attr & ZEND_DIM_IS) ? BP_VAR_IS : BP_VAR_R);
896
897
78
      zval_ptr_dtor_nogc(&op1);
898
78
      zval_ptr_dtor_nogc(&op2);
899
78
      if (UNEXPECTED(EG(exception))) {
900
2
        return FAILURE;
901
2
      }
902
903
76
      break;
904
693
    case ZEND_AST_CONST_ENUM_INIT:
905
693
    {
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
693
      if (CG(in_compilation)) {
909
0
        return FAILURE;
910
0
      }
911
912
693
      zend_ast *class_name_ast = ast->child[0];
913
693
      zend_string *class_name = zend_ast_get_str(class_name_ast);
914
915
693
      zend_ast *case_name_ast = ast->child[1];
916
693
      zend_string *case_name = zend_ast_get_str(case_name_ast);
917
918
693
      zend_ast *case_value_ast = ast->child[2];
919
920
693
      zval case_value_zv;
921
693
      ZVAL_UNDEF(&case_value_zv);
922
693
      if (case_value_ast != NULL) {
923
324
        if (UNEXPECTED(zend_ast_evaluate_ex(&case_value_zv, case_value_ast, scope, &short_circuited, ctx) != SUCCESS)) {
924
2
          return FAILURE;
925
2
        }
926
324
      }
927
928
691
      zend_class_entry *ce = zend_lookup_class(class_name);
929
691
      zend_enum_new(result, ce, case_name, case_value_ast != NULL ? &case_value_zv : NULL);
930
691
      zval_ptr_dtor_nogc(&case_value_zv);
931
691
      break;
932
693
    }
933
606
    case ZEND_AST_CLASS_CONST:
934
606
    {
935
606
      zend_string *class_name = zend_ast_get_str(ast->child[0]);
936
606
      if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
937
2
        return FAILURE;
938
2
      }
939
604
      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
604
      zend_string *const_name = Z_STR(op2);
945
946
604
      zend_string *previous_filename;
947
604
      zend_long previous_lineno;
948
604
      if (scope) {
949
536
        previous_filename = EG(filename_override);
950
536
        previous_lineno = EG(lineno_override);
951
536
        EG(filename_override) = scope->info.user.filename;
952
536
        EG(lineno_override) = zend_ast_get_lineno(ast);
953
536
      }
954
604
      zval *zv = zend_get_class_constant_ex(class_name, const_name, scope, ast->attr);
955
604
      if (scope) {
956
534
        EG(filename_override) = previous_filename;
957
534
        EG(lineno_override) = previous_lineno;
958
534
      }
959
960
604
      if (UNEXPECTED(zv == NULL)) {
961
66
        ZVAL_UNDEF(result);
962
66
        zval_ptr_dtor_nogc(&op2);
963
66
        return FAILURE;
964
66
      }
965
538
      ZVAL_COPY_OR_DUP(result, zv);
966
538
      zval_ptr_dtor_nogc(&op2);
967
538
      break;
968
604
    }
969
112
    case ZEND_AST_NEW:
970
112
    {
971
112
      zend_class_entry *ce = zend_ast_fetch_class(ast->child[0], scope);
972
112
      if (!ce) {
973
14
        return FAILURE;
974
14
      }
975
976
98
      if (object_init_ex(result, ce) != SUCCESS) {
977
4
        return FAILURE;
978
4
      }
979
980
      /* Even if there is no constructor, the object can have cause side-effects in various ways (__toString(), __get(), __isset(), etc). */
981
94
      ctx->had_side_effects = true;
982
983
94
      zend_ast_list *args_ast = zend_ast_get_list(ast->child[1]);
984
94
      if (args_ast->attr) {
985
        /* Has named arguments. */
986
4
        HashTable *args = zend_new_array(args_ast->children);
987
8
        for (uint32_t i = 0; i < args_ast->children; i++) {
988
4
          zend_ast *arg_ast = args_ast->child[i];
989
4
          zend_string *name = NULL;
990
4
          zval arg;
991
4
          if (arg_ast->kind == ZEND_AST_NAMED_ARG) {
992
4
            name = zend_ast_get_str(arg_ast->child[0]);
993
4
            arg_ast = arg_ast->child[1];
994
4
          }
995
4
          if (zend_ast_evaluate_ex(&arg, arg_ast, scope, &short_circuited, ctx) == FAILURE) {
996
0
            zend_array_destroy(args);
997
0
            zval_ptr_dtor(result);
998
0
            return FAILURE;
999
0
          }
1000
4
          if (name) {
1001
4
            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
4
          } else {
1010
0
            zend_hash_next_index_insert(args, &arg);
1011
0
          }
1012
4
        }
1013
1014
4
        zend_function *ctor = Z_OBJ_HT_P(result)->get_constructor(Z_OBJ_P(result));
1015
4
        if (ctor) {
1016
4
          zend_call_known_function(
1017
4
            ctor, Z_OBJ_P(result), Z_OBJCE_P(result), NULL, 0, NULL, args);
1018
4
        }
1019
1020
4
        zend_array_destroy(args);
1021
90
      } else {
1022
90
        ALLOCA_FLAG(use_heap)
1023
90
        zval *args = do_alloca(sizeof(zval) * args_ast->children, use_heap);
1024
104
        for (uint32_t i = 0; i < args_ast->children; i++) {
1025
14
          if (zend_ast_evaluate_ex(&args[i], args_ast->child[i], scope, &short_circuited, ctx) == FAILURE) {
1026
0
            for (uint32_t j = 0; j < i; j++) {
1027
0
              zval_ptr_dtor(&args[j]);
1028
0
            }
1029
0
            free_alloca(args, use_heap);
1030
0
            zval_ptr_dtor(result);
1031
0
            return FAILURE;
1032
0
          }
1033
14
        }
1034
1035
90
        zend_function *ctor = Z_OBJ_HT_P(result)->get_constructor(Z_OBJ_P(result));
1036
90
        if (ctor) {
1037
32
          zend_call_known_instance_method(
1038
32
            ctor, Z_OBJ_P(result), NULL, args_ast->children, args);
1039
32
        }
1040
1041
104
        for (uint32_t i = 0; i < args_ast->children; i++) {
1042
14
          zval_ptr_dtor(&args[i]);
1043
14
        }
1044
90
        free_alloca(args, use_heap);
1045
90
      }
1046
1047
94
      if (EG(exception)) {
1048
0
        zend_object_store_ctor_failed(Z_OBJ_P(result));
1049
0
        zval_ptr_dtor(result);
1050
0
        return FAILURE;
1051
0
      }
1052
94
      return SUCCESS;
1053
94
    }
1054
54
    case ZEND_AST_CALL:
1055
94
    case ZEND_AST_STATIC_CALL:
1056
94
    {
1057
94
      zend_function *fptr;
1058
94
      zend_class_entry *called_scope = NULL;
1059
94
      switch (ast->kind) {
1060
54
        case ZEND_AST_CALL: {
1061
54
          ZEND_ASSERT(ast->child[1]->kind == ZEND_AST_CALLABLE_CONVERT);
1062
54
          zend_ast_fcc *fcc_ast = (zend_ast_fcc*)ast->child[1];
1063
54
          fptr = ZEND_MAP_PTR_GET(fcc_ast->fptr);
1064
1065
54
          if (!fptr) {
1066
50
            zend_string *function_name = zend_ast_get_str(ast->child[0]);
1067
50
            zend_string *function_name_lc = zend_string_tolower(function_name);
1068
50
            fptr = zend_fetch_function(function_name_lc);
1069
50
            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
10
                fptr = zend_fetch_function_str(backslash + 1, ZSTR_LEN(function_name_lc) - (backslash - ZSTR_VAL(function_name_lc) + 1));
1073
10
              }
1074
12
            }
1075
50
            zend_string_release(function_name_lc);
1076
50
            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
44
            ZEND_MAP_PTR_SET(fcc_ast->fptr, fptr);
1082
44
          }
1083
1084
48
          break;
1085
54
        }
1086
48
        case ZEND_AST_STATIC_CALL: {
1087
40
          ZEND_ASSERT(ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT);
1088
40
          zend_ast_fcc *fcc_ast = (zend_ast_fcc*)ast->child[2];
1089
1090
40
          zend_class_entry *ce = zend_ast_fetch_class(ast->child[0], scope);
1091
40
          if (!ce) {
1092
4
            return FAILURE;
1093
4
          }
1094
36
          called_scope = ce;
1095
1096
36
          fptr = ZEND_MAP_PTR_GET(fcc_ast->fptr);
1097
1098
36
          if (!fptr) {
1099
36
            zend_string *method_name = zend_ast_get_str(ast->child[1]);
1100
36
            if (ce->get_static_method) {
1101
0
              fptr = ce->get_static_method(ce, method_name);
1102
36
            } else {
1103
36
              fptr = zend_hash_find_ptr_lc(&ce->function_table, method_name);
1104
36
              if (fptr) {
1105
32
                if (!zend_check_method_accessible(fptr, scope)) {
1106
4
                  if (ce->__callstatic) {
1107
0
                    zend_throw_error(NULL, "Creating a callable for the magic __callStatic() method is not supported in constant expressions");
1108
4
                  } else {
1109
4
                    zend_bad_method_call(fptr, method_name, scope);
1110
4
                  }
1111
1112
4
                  return FAILURE;
1113
4
                }
1114
32
              } else {
1115
4
                if (ce->__callstatic) {
1116
2
                  zend_throw_error(NULL, "Creating a callable for the magic __callStatic() method is not supported in constant expressions");
1117
2
                } else {
1118
2
                  zend_undefined_method(ce, method_name);
1119
2
                }
1120
1121
4
                return FAILURE;
1122
4
              }
1123
36
            }
1124
1125
28
            if (!(fptr->common.fn_flags & ZEND_ACC_STATIC)) {
1126
2
              zend_non_static_method_call(fptr);
1127
              
1128
2
              return FAILURE;
1129
2
            }
1130
26
            if ((fptr->common.fn_flags & ZEND_ACC_ABSTRACT)) {
1131
2
              zend_abstract_method_call(fptr);
1132
              
1133
2
              return FAILURE;
1134
24
            } else if (fptr->common.scope->ce_flags & ZEND_ACC_TRAIT) {
1135
2
              zend_error(E_DEPRECATED,
1136
2
                "Calling static trait method %s::%s is deprecated, "
1137
2
                "it should only be called on a class using the trait",
1138
2
                ZSTR_VAL(fptr->common.scope->name), ZSTR_VAL(fptr->common.function_name));
1139
2
              if (EG(exception)) {
1140
0
                return FAILURE;
1141
0
              }
1142
2
            }
1143
1144
24
            ZEND_MAP_PTR_SET(fcc_ast->fptr, fptr);
1145
24
          }
1146
1147
24
          break;
1148
36
        }
1149
94
      }
1150
1151
72
      zend_create_fake_closure(result, fptr, fptr->common.scope, called_scope, NULL);
1152
1153
72
      return SUCCESS;
1154
94
    }
1155
22
    case ZEND_AST_OP_ARRAY:
1156
22
    {
1157
22
      zend_function *func = (zend_function *)zend_ast_get_op_array(ast)->op_array;
1158
1159
22
      zend_create_closure(result, func, scope, scope, NULL);
1160
22
      return SUCCESS;
1161
94
    }
1162
78
    case ZEND_AST_PROP:
1163
144
    case ZEND_AST_NULLSAFE_PROP:
1164
144
    {
1165
144
      if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) {
1166
6
        return FAILURE;
1167
6
      }
1168
138
      if (short_circuited) {
1169
6
        *short_circuited_ptr = true;
1170
6
        ZVAL_NULL(result);
1171
6
        return SUCCESS;
1172
6
      }
1173
132
      if (ast->kind == ZEND_AST_NULLSAFE_PROP && Z_TYPE(op1) == IS_NULL) {
1174
28
        *short_circuited_ptr = true;
1175
28
        ZVAL_NULL(result);
1176
28
        return SUCCESS;
1177
28
      }
1178
1179
104
      if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) {
1180
2
        zval_ptr_dtor_nogc(&op1);
1181
2
        return FAILURE;
1182
2
      }
1183
1184
102
      if (!try_convert_to_string(&op2)) {
1185
2
        zval_ptr_dtor_nogc(&op1);
1186
2
        zval_ptr_dtor_nogc(&op2);
1187
2
        return FAILURE;
1188
2
      }
1189
1190
100
      if (Z_TYPE(op1) != IS_OBJECT) {
1191
14
        zend_wrong_property_read(&op1, &op2);
1192
1193
14
        zval_ptr_dtor_nogc(&op1);
1194
14
        zval_ptr_dtor_nogc(&op2);
1195
1196
14
        ZVAL_NULL(result);
1197
14
        return SUCCESS;
1198
14
      }
1199
1200
86
      zend_object *zobj = Z_OBJ(op1);
1201
86
      if (!(zobj->ce->ce_flags & ZEND_ACC_ENUM)) {
1202
10
        zend_throw_error(NULL, "Fetching properties on non-enums in constant expressions is not allowed");
1203
10
        zval_ptr_dtor_nogc(&op1);
1204
10
        zval_ptr_dtor_nogc(&op2);
1205
10
        return FAILURE;
1206
10
      }
1207
1208
76
      zend_string *name = Z_STR(op2);
1209
76
      zval *property_result = zend_read_property_ex(scope, zobj, name, 0, result);
1210
76
      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
76
      if (result != property_result) {
1217
76
        ZVAL_COPY(result, property_result);
1218
76
      }
1219
76
      zval_ptr_dtor_nogc(&op1);
1220
76
      zval_ptr_dtor_nogc(&op2);
1221
76
      return SUCCESS;
1222
76
    }
1223
0
    default:
1224
0
      zend_throw_error(NULL, "Unsupported constant expression");
1225
0
      ret = FAILURE;
1226
4.52k
  }
1227
3.95k
  return ret;
1228
4.52k
}
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
10.8k
{
1239
10.8k
  size_t size;
1240
1241
10.8k
  if (ast->kind == ZEND_AST_ZVAL || ast->kind == ZEND_AST_CONSTANT) {
1242
6.48k
    size = sizeof(zend_ast_zval);
1243
6.48k
  } else if (ast->kind == ZEND_AST_OP_ARRAY) {
1244
28
    size = sizeof(zend_ast_op_array);
1245
4.36k
  } else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) {
1246
120
    size = sizeof(zend_ast_fcc);
1247
4.24k
  } else if (zend_ast_is_list(ast)) {
1248
537
    uint32_t i;
1249
537
    const zend_ast_list *list = zend_ast_get_list(ast);
1250
1251
537
    size = zend_ast_list_size(list->children);
1252
1.13k
    for (i = 0; i < list->children; i++) {
1253
602
      if (list->child[i]) {
1254
602
        size += zend_ast_tree_size(list->child[i]);
1255
602
      }
1256
602
    }
1257
3.70k
  } else if (zend_ast_is_decl(ast)) {
1258
    /* Not implemented. */
1259
0
    ZEND_UNREACHABLE();
1260
3.70k
  } else {
1261
3.70k
    uint32_t i, children = zend_ast_get_num_children(ast);
1262
1263
3.70k
    size = zend_ast_size(children);
1264
11.8k
    for (i = 0; i < children; i++) {
1265
8.09k
      if (ast->child[i]) {
1266
7.30k
        size += zend_ast_tree_size(ast->child[i]);
1267
7.30k
      }
1268
8.09k
    }
1269
3.70k
  }
1270
10.8k
  return size;
1271
10.8k
}
1272
1273
static void* ZEND_FASTCALL zend_ast_tree_copy(zend_ast *ast, void *buf)
1274
10.8k
{
1275
10.8k
  if (ast->kind == ZEND_AST_ZVAL) {
1276
4.91k
    zend_ast_zval *new = (zend_ast_zval*)buf;
1277
4.91k
    new->kind = ZEND_AST_ZVAL;
1278
4.91k
    new->attr = ast->attr;
1279
4.91k
    ZVAL_COPY(&new->val, zend_ast_get_zval(ast));
1280
4.91k
    Z_LINENO(new->val) = zend_ast_get_lineno(ast);
1281
4.91k
    buf = (void*)((char*)buf + sizeof(zend_ast_zval));
1282
5.95k
  } else if (ast->kind == ZEND_AST_CONSTANT) {
1283
1.56k
    zend_ast_zval *new = (zend_ast_zval*)buf;
1284
1.56k
    new->kind = ZEND_AST_CONSTANT;
1285
1.56k
    new->attr = ast->attr;
1286
1.56k
    ZVAL_STR_COPY(&new->val, zend_ast_get_constant_name(ast));
1287
1.56k
    Z_LINENO(new->val) = zend_ast_get_lineno(ast);
1288
1.56k
    buf = (void*)((char*)buf + sizeof(zend_ast_zval));
1289
4.39k
  } else if (zend_ast_is_list(ast)) {
1290
537
    const zend_ast_list *list = zend_ast_get_list(ast);
1291
537
    zend_ast_list *new = (zend_ast_list*)buf;
1292
537
    uint32_t i;
1293
537
    new->kind = list->kind;
1294
537
    new->attr = list->attr;
1295
537
    new->children = list->children;
1296
537
    new->lineno = list->lineno;
1297
537
    buf = (void*)((char*)buf + zend_ast_list_size(list->children));
1298
1.13k
    for (i = 0; i < list->children; i++) {
1299
602
      if (list->child[i]) {
1300
602
        new->child[i] = (zend_ast*)buf;
1301
602
        buf = zend_ast_tree_copy(list->child[i], buf);
1302
602
      } else {
1303
0
        new->child[i] = NULL;
1304
0
      }
1305
602
    }
1306
3.85k
  } else if (ast->kind == ZEND_AST_OP_ARRAY) {
1307
28
    const zend_ast_op_array *old = zend_ast_get_op_array(ast);
1308
28
    zend_ast_op_array *new = (zend_ast_op_array*)buf;
1309
28
    new->kind = old->kind;
1310
28
    new->attr = old->attr;
1311
28
    new->lineno = old->lineno;
1312
28
    new->op_array = old->op_array;
1313
28
    function_add_ref((zend_function *)new->op_array);
1314
28
    buf = (void*)((char*)buf + sizeof(zend_ast_op_array));
1315
3.82k
  } else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) {
1316
120
    const zend_ast_fcc *old = (zend_ast_fcc*)ast;
1317
120
    zend_ast_fcc *new = (zend_ast_fcc*)buf;
1318
120
    new->kind = old->kind;
1319
120
    new->attr = old->attr;
1320
120
    new->lineno = old->lineno;
1321
120
    ZEND_MAP_PTR_INIT(new->fptr, ZEND_MAP_PTR(old->fptr));
1322
120
    buf = (void*)((char*)buf + sizeof(zend_ast_fcc));
1323
3.70k
  } else if (zend_ast_is_decl(ast)) {
1324
    /* Not implemented. */
1325
0
    ZEND_UNREACHABLE();
1326
3.70k
  } else {
1327
3.70k
    uint32_t i, children = zend_ast_get_num_children(ast);
1328
3.70k
    zend_ast *new = (zend_ast*)buf;
1329
3.70k
    new->kind = ast->kind;
1330
3.70k
    new->attr = ast->attr;
1331
3.70k
    new->lineno = ast->lineno;
1332
3.70k
    buf = (void*)((char*)buf + zend_ast_size(children));
1333
11.8k
    for (i = 0; i < children; i++) {
1334
8.09k
      if (ast->child[i]) {
1335
7.30k
        new->child[i] = (zend_ast*)buf;
1336
7.30k
        buf = zend_ast_tree_copy(ast->child[i], buf);
1337
7.30k
      } else {
1338
795
        new->child[i] = NULL;
1339
795
      }
1340
8.09k
    }
1341
3.70k
  }
1342
10.8k
  return buf;
1343
10.8k
}
1344
1345
ZEND_API zend_ast_ref * ZEND_FASTCALL zend_ast_copy(zend_ast *ast)
1346
2.96k
{
1347
2.96k
  size_t tree_size;
1348
2.96k
  zend_ast_ref *ref;
1349
1350
2.96k
  ZEND_ASSERT(ast != NULL);
1351
2.96k
  tree_size = zend_ast_tree_size(ast) + sizeof(zend_ast_ref);
1352
2.96k
  ref = emalloc(tree_size);
1353
2.96k
  zend_ast_tree_copy(ast, GC_AST(ref));
1354
2.96k
  GC_SET_REFCOUNT(ref, 1);
1355
2.96k
  GC_TYPE_INFO(ref) = GC_CONSTANT_AST;
1356
2.96k
  return ref;
1357
2.96k
}
1358
1359
ZEND_API void ZEND_FASTCALL zend_ast_destroy(zend_ast *ast)
1360
1.91M
{
1361
3.84M
tail_call:
1362
3.84M
  if (!ast) {
1363
486k
    return;
1364
486k
  }
1365
1366
3.36M
  if (EXPECTED(ast->kind >= ZEND_AST_VAR)) {
1367
1.54M
    uint32_t i, children = zend_ast_get_num_children(ast);
1368
1369
2.55M
    for (i = 1; i < children; i++) {
1370
1.00M
      zend_ast_destroy(ast->child[i]);
1371
1.00M
    }
1372
1.54M
    ast = ast->child[0];
1373
1.54M
    goto tail_call;
1374
1.81M
  } else if (EXPECTED(ast->kind == ZEND_AST_ZVAL)) {
1375
1.33M
    zval_ptr_dtor_nogc(zend_ast_get_zval(ast));
1376
1.33M
  } else if (EXPECTED(zend_ast_is_list(ast))) {
1377
434k
    const zend_ast_list *list = zend_ast_get_list(ast);
1378
434k
    if (list->children) {
1379
356k
      uint32_t i;
1380
1381
1.02M
      for (i = 1; i < list->children; i++) {
1382
669k
        zend_ast_destroy(list->child[i]);
1383
669k
      }
1384
356k
      ast = list->child[0];
1385
356k
      goto tail_call;
1386
356k
    }
1387
434k
  } else if (EXPECTED(ast->kind == ZEND_AST_CONSTANT)) {
1388
1.59k
    zend_string_release_ex(zend_ast_get_constant_name(ast), 0);
1389
43.4k
  } else if (EXPECTED(ast->kind == ZEND_AST_OP_ARRAY)) {
1390
28
    destroy_op_array(zend_ast_get_op_array(ast)->op_array);
1391
43.4k
  } else if (EXPECTED(zend_ast_is_decl(ast))) {
1392
36.6k
    const zend_ast_decl *decl = (const zend_ast_decl *) ast;
1393
1394
36.6k
    if (decl->name) {
1395
31.8k
        zend_string_release_ex(decl->name, 0);
1396
31.8k
    }
1397
36.6k
    if (decl->doc_comment) {
1398
38
      zend_string_release_ex(decl->doc_comment, 0);
1399
38
    }
1400
36.6k
    zend_ast_destroy(decl->child[0]);
1401
36.6k
    zend_ast_destroy(decl->child[1]);
1402
36.6k
    zend_ast_destroy(decl->child[2]);
1403
36.6k
    zend_ast_destroy(decl->child[3]);
1404
36.6k
    ast = decl->child[4];
1405
36.6k
    goto tail_call;
1406
36.6k
  }
1407
3.36M
}
1408
1409
ZEND_API void ZEND_FASTCALL zend_ast_ref_destroy(zend_ast_ref *ast)
1410
34
{
1411
34
  zend_ast_destroy(GC_AST(ast));
1412
34
  efree(ast);
1413
34
}
1414
1415
9.53k
ZEND_API void zend_ast_apply(zend_ast *ast, zend_ast_apply_func fn, void *context) {
1416
9.53k
  if (zend_ast_is_list(ast)) {
1417
1.86k
    zend_ast_list *list = zend_ast_get_list(ast);
1418
1.86k
    uint32_t i;
1419
3.79k
    for (i = 0; i < list->children; ++i) {
1420
1.92k
      fn(&list->child[i], context);
1421
1.92k
    }
1422
7.66k
  } else if (zend_ast_is_decl(ast)) {
1423
    /* Not implemented. */
1424
0
    ZEND_UNREACHABLE();
1425
7.66k
  } else {
1426
7.66k
    uint32_t i, children = zend_ast_get_num_children(ast);
1427
20.4k
    for (i = 0; i < children; ++i) {
1428
12.8k
      fn(&ast->child[i], context);
1429
12.8k
    }
1430
7.66k
  }
1431
9.53k
}
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
392
{
1473
392
  size_t i;
1474
1475
1.35k
  for (i = 0; i < ZSTR_LEN(s); i++) {
1476
962
    unsigned char c = ZSTR_VAL(s)[i];
1477
962
    if (c == '\'' || c == '\\') {
1478
16
      smart_str_appendc(str, '\\');
1479
16
      smart_str_appendc(str, c);
1480
946
    } else {
1481
946
      smart_str_appendc(str, c);
1482
946
    }
1483
962
  }
1484
392
}
1485
1486
static ZEND_COLD void zend_ast_export_qstr(smart_str *str, char quote, const zend_string *s)
1487
25.8k
{
1488
25.8k
  size_t i;
1489
1490
348k
  for (i = 0; i < ZSTR_LEN(s); i++) {
1491
322k
    unsigned char c = ZSTR_VAL(s)[i];
1492
322k
    if (c < ' ') {
1493
100k
      switch (c) {
1494
19.8k
        case '\n':
1495
19.8k
          smart_str_appends(str, "\\n");
1496
19.8k
          break;
1497
395
        case '\r':
1498
395
          smart_str_appends(str, "\\r");
1499
395
          break;
1500
208
        case '\t':
1501
208
          smart_str_appends(str, "\\t");
1502
208
          break;
1503
100
        case '\f':
1504
100
          smart_str_appends(str, "\\f");
1505
100
          break;
1506
6.25k
        case '\v':
1507
6.25k
          smart_str_appends(str, "\\v");
1508
6.25k
          break;
1509
#ifdef ZEND_WIN32
1510
        case VK_ESCAPE:
1511
#else
1512
36
        case '\e':
1513
36
#endif
1514
36
          smart_str_appends(str, "\\e");
1515
36
          break;
1516
73.6k
        default:
1517
73.6k
          smart_str_appends(str, "\\0");
1518
73.6k
          smart_str_appendc(str, '0' + (c / 8));
1519
73.6k
          smart_str_appendc(str, '0' + (c % 8));
1520
73.6k
          break;
1521
100k
      }
1522
222k
    } else {
1523
222k
      if (c == quote || c == '$' || c == '\\') {
1524
2.51k
        smart_str_appendc(str, '\\');
1525
2.51k
      }
1526
222k
      smart_str_appendc(str, c);
1527
222k
    }
1528
322k
  }
1529
25.8k
}
1530
1531
static ZEND_COLD void zend_ast_export_indent(smart_str *str, int indent)
1532
2.73k
{
1533
9.14k
  while (indent > 0) {
1534
6.41k
    smart_str_appends(str, "    ");
1535
6.41k
    indent--;
1536
6.41k
  }
1537
2.73k
}
1538
1539
static ZEND_COLD void zend_ast_export_name(smart_str *str, zend_ast *ast, int priority, int indent)
1540
2.52k
{
1541
2.52k
  if (ast->kind == ZEND_AST_ZVAL) {
1542
2.27k
    const zval *zv = zend_ast_get_zval(ast);
1543
1544
2.27k
    if (Z_TYPE_P(zv) == IS_STRING) {
1545
2.21k
      smart_str_append(str, Z_STR_P(zv));
1546
2.21k
      return;
1547
2.21k
    }
1548
2.27k
  }
1549
314
  zend_ast_export_ex(str, ast, priority, indent);
1550
314
}
1551
1552
static ZEND_COLD void zend_ast_export_ns_name(smart_str *str, zend_ast *ast, int priority, int indent)
1553
3.64k
{
1554
3.64k
  if (ast->kind == ZEND_AST_ZVAL) {
1555
3.32k
    const zval *zv = zend_ast_get_zval(ast);
1556
1557
3.32k
    if (Z_TYPE_P(zv) == IS_STRING) {
1558
3.32k
        if (ast->attr == ZEND_NAME_FQ) {
1559
176
        smart_str_appendc(str, '\\');
1560
3.14k
        } else if (ast->attr == ZEND_NAME_RELATIVE) {
1561
54
        smart_str_appends(str, "namespace\\");
1562
54
        }
1563
3.32k
      smart_str_append(str, Z_STR_P(zv));
1564
3.32k
      return;
1565
3.32k
    }
1566
3.32k
  }
1567
320
  zend_ast_export_ex(str, ast, priority, indent);
1568
320
}
1569
1570
static ZEND_COLD bool zend_ast_valid_var_char(char ch)
1571
11.4k
{
1572
11.4k
  unsigned char c = (unsigned char)ch;
1573
1574
11.4k
  if (c != '_' && c < 127 &&
1575
11.4k
      (c < '0' || c > '9') &&
1576
11.4k
      (c < 'A' || c > 'Z') &&
1577
11.4k
      (c < 'a' || c > 'z')) {
1578
11.3k
    return false;
1579
11.3k
  }
1580
62
  return true;
1581
11.4k
}
1582
1583
static ZEND_COLD bool zend_ast_valid_var_name(const char *s, size_t len)
1584
41.6k
{
1585
41.6k
  unsigned char c;
1586
41.6k
  size_t i;
1587
1588
41.6k
  if (len == 0) {
1589
0
    return false;
1590
0
  }
1591
41.6k
  c = (unsigned char)s[0];
1592
41.6k
  if (c != '_' && c < 127 &&
1593
40.4k
      (c < 'A' || c > 'Z') &&
1594
28.3k
      (c < 'a' || c > 'z')) {
1595
18
    return false;
1596
18
  }
1597
77.6k
  for (i = 1; i < len; i++) {
1598
36.0k
    c = (unsigned char)s[i];
1599
36.0k
    if (c != '_' && c < 127 &&
1600
27.8k
        (c < '0' || c > '9') &&
1601
26.1k
        (c < 'A' || c > 'Z') &&
1602
23.3k
        (c < 'a' || c > 'z')) {
1603
0
      return false;
1604
0
    }
1605
36.0k
  }
1606
41.5k
  return true;
1607
41.5k
}
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
41.9k
{
1616
41.9k
  if (ast->kind == ZEND_AST_ZVAL) {
1617
41.6k
    zval *zv = zend_ast_get_zval(ast);
1618
41.6k
    if (Z_TYPE_P(zv) == IS_STRING &&
1619
41.6k
        zend_ast_valid_var_name(Z_STRVAL_P(zv), Z_STRLEN_P(zv))) {
1620
41.5k
      smart_str_append(str, Z_STR_P(zv));
1621
41.5k
      return;
1622
41.5k
    }
1623
41.6k
  } else if (ast->kind == ZEND_AST_VAR) {
1624
32
    zend_ast_export_ex(str, ast, 0, indent);
1625
32
    return;
1626
32
  }
1627
278
  smart_str_appendc(str, '{');
1628
278
  zend_ast_export_name(str, ast, 0, indent);
1629
278
  smart_str_appendc(str, '}');
1630
278
}
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
2.91k
{
1636
2.91k
  ZEND_ASSERT(children <= list->children);
1637
2.91k
  uint32_t i = 0;
1638
1639
5.53k
  while (i < children) {
1640
2.61k
    if (i != 0 && separator) {
1641
421
      smart_str_appends(str, ", ");
1642
421
    }
1643
2.61k
    zend_ast_export_ex(str, list->child[i], priority, indent);
1644
2.61k
    i++;
1645
2.61k
  }
1646
2.91k
}
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
2.91k
{
1650
2.91k
  zend_ast_export_list_ex(str, list, separator, priority, indent, list->children);
1651
2.91k
}
1652
1653
static ZEND_COLD void zend_ast_export_encaps_list(smart_str *str, char quote, const zend_ast_list *list, int indent)
1654
1.56k
{
1655
1.56k
  uint32_t i = 0;
1656
1.56k
  zend_ast *ast;
1657
1658
53.0k
  while (i < list->children) {
1659
51.4k
    ast = list->child[i];
1660
51.4k
    if (ast->kind == ZEND_AST_ZVAL) {
1661
25.8k
      const zval *zv = zend_ast_get_zval(ast);
1662
1663
25.8k
      ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
1664
25.8k
      zend_ast_export_qstr(str, quote, Z_STR_P(zv));
1665
25.8k
    } else if (ast->kind == ZEND_AST_VAR &&
1666
11.8k
               ast->child[0]->kind == ZEND_AST_ZVAL &&
1667
11.8k
               (i + 1 == list->children ||
1668
11.7k
                list->child[i + 1]->kind != ZEND_AST_ZVAL ||
1669
11.4k
                !zend_ast_var_needs_braces(
1670
11.4k
                    *Z_STRVAL_P(
1671
11.7k
                        zend_ast_get_zval(list->child[i + 1]))))) {
1672
11.7k
      zend_ast_export_ex(str, ast, 0, indent);
1673
13.9k
    } else {
1674
13.9k
      smart_str_appendc(str, '{');
1675
13.9k
      zend_ast_export_ex(str, ast, 0, indent);
1676
13.9k
      smart_str_appendc(str, '}');
1677
13.9k
    }
1678
51.4k
    i++;
1679
51.4k
  }
1680
1.56k
}
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
108
{
1684
108
  uint32_t i = 0;
1685
1686
252
  while (i < list->children) {
1687
144
    if (i != 0) {
1688
36
      smart_str_appends(str, separator);
1689
36
    }
1690
144
    zend_ast_export_name(str, list->child[i], 0, indent);
1691
144
    i++;
1692
144
  }
1693
108
}
1694
1695
72
#define zend_ast_export_name_list(s, l, i) zend_ast_export_name_list_ex(s, l, i, ", ")
1696
36
#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
54
{
1700
54
  uint32_t i = 0;
1701
1702
162
  while (i < list->children) {
1703
108
    if (i != 0) {
1704
54
      smart_str_appends(str, ", ");
1705
54
    }
1706
108
    if (list->child[i]->attr & ZEND_BIND_REF) {
1707
54
      smart_str_appendc(str, '&');
1708
54
    }
1709
108
    smart_str_appendc(str, '$');
1710
108
    zend_ast_export_name(str, list->child[i], 20, indent);
1711
108
    i++;
1712
108
  }
1713
54
}
1714
1715
static ZEND_COLD void zend_ast_export_stmt(smart_str *str, zend_ast *ast, int indent)
1716
2.80k
{
1717
2.80k
  if (!ast) {
1718
84
    return;
1719
84
  }
1720
1721
2.72k
  if (ast->kind == ZEND_AST_STMT_LIST ||
1722
1.87k
      ast->kind == ZEND_AST_TRAIT_ADAPTATIONS) {
1723
866
    const zend_ast_list *list = (const zend_ast_list*)ast;
1724
866
    uint32_t i = 0;
1725
1726
2.89k
    while (i < list->children) {
1727
2.02k
      ast = list->child[i];
1728
2.02k
      zend_ast_export_stmt(str, ast, indent);
1729
2.02k
      i++;
1730
2.02k
    }
1731
1.85k
  } else {
1732
1.85k
    zend_ast_export_indent(str, indent);
1733
1.85k
    zend_ast_export_ex(str, ast, 0, indent);
1734
1.85k
    switch (ast->kind) {
1735
20
      case ZEND_AST_LABEL:
1736
110
      case ZEND_AST_IF:
1737
128
      case ZEND_AST_SWITCH:
1738
146
      case ZEND_AST_WHILE:
1739
164
      case ZEND_AST_TRY:
1740
182
      case ZEND_AST_FOR:
1741
200
      case ZEND_AST_FOREACH:
1742
200
      case ZEND_AST_FUNC_DECL:
1743
262
      case ZEND_AST_METHOD:
1744
330
      case ZEND_AST_CLASS:
1745
366
      case ZEND_AST_USE_TRAIT:
1746
366
      case ZEND_AST_NAMESPACE:
1747
402
      case ZEND_AST_DECLARE:
1748
402
        break;
1749
84
      case ZEND_AST_PROP_GROUP: {
1750
84
        const zend_ast *first_prop = zend_ast_get_list(ast->child[1])->child[0];
1751
84
        const zend_ast *hook_list = first_prop->child[3];
1752
84
        if (hook_list == NULL) {
1753
72
          smart_str_appendc(str, ';');
1754
72
        }
1755
84
        break;
1756
366
      }
1757
1.37k
      default:
1758
1.37k
        smart_str_appendc(str, ';');
1759
1.37k
        break;
1760
1.85k
    }
1761
1.85k
    smart_str_appendc(str, '\n');
1762
1.85k
  }
1763
2.72k
}
1764
1765
static ZEND_COLD void zend_ast_export_if_stmt(smart_str *str, const zend_ast_list *list, int indent)
1766
90
{
1767
90
  uint32_t i;
1768
90
  zend_ast *ast;
1769
1770
126
tail_call:
1771
126
  i = 0;
1772
304
  while (i < list->children) {
1773
214
    ast = list->child[i];
1774
214
    ZEND_ASSERT(ast->kind == ZEND_AST_IF_ELEM);
1775
214
    if (ast->child[0]) {
1776
144
      if (i == 0) {
1777
126
        smart_str_appends(str, "if (");
1778
126
      } else {
1779
18
        zend_ast_export_indent(str, indent);
1780
18
        smart_str_appends(str, "} elseif (");
1781
18
      }
1782
144
      zend_ast_export_ex(str, ast->child[0], 0, indent);
1783
144
      smart_str_appends(str, ") {\n");
1784
144
      zend_ast_export_stmt(str, ast->child[1], indent + 1);
1785
144
    } else {
1786
70
      zend_ast_export_indent(str, indent);
1787
70
      smart_str_appends(str, "} else ");
1788
70
      if (ast->child[1] && ast->child[1]->kind == ZEND_AST_IF) {
1789
36
        list = (const zend_ast_list*)ast->child[1];
1790
36
        goto tail_call;
1791
36
      } else {
1792
34
        smart_str_appends(str, "{\n");
1793
34
        zend_ast_export_stmt(str, ast->child[1], indent + 1);
1794
34
      }
1795
70
    }
1796
178
    i++;
1797
178
  }
1798
90
  zend_ast_export_indent(str, indent);
1799
90
  smart_str_appendc(str, '}');
1800
90
}
1801
1802
static ZEND_COLD void zend_ast_export_zval(smart_str *str, const zval *zv, int priority, int indent)
1803
2.61k
{
1804
2.61k
  ZVAL_DEREF(zv);
1805
2.61k
  switch (Z_TYPE_P(zv)) {
1806
0
    case IS_NULL:
1807
0
      smart_str_appends(str, "null");
1808
0
      break;
1809
22
    case IS_FALSE:
1810
22
      smart_str_appends(str, "false");
1811
22
      break;
1812
0
    case IS_TRUE:
1813
0
      smart_str_appends(str, "true");
1814
0
      break;
1815
1.88k
    case IS_LONG:
1816
1.88k
      smart_str_append_long(str, Z_LVAL_P(zv));
1817
1.88k
      break;
1818
293
    case IS_DOUBLE:
1819
293
      smart_str_append_double(
1820
293
        str, Z_DVAL_P(zv), (int) EG(precision), /* zero_fraction */ true);
1821
293
      break;
1822
392
    case IS_STRING:
1823
392
      smart_str_appendc(str, '\'');
1824
392
      zend_ast_export_str(str, Z_STR_P(zv));
1825
392
      smart_str_appendc(str, '\'');
1826
392
      break;
1827
22
    case IS_ARRAY: {
1828
22
      zend_long idx;
1829
22
      zend_string *key;
1830
22
      zval *val;
1831
22
      bool first = true;
1832
22
      smart_str_appendc(str, '[');
1833
110
      ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(zv), idx, key, val) {
1834
110
        if (first) {
1835
22
          first = false;
1836
22
        } else {
1837
22
          smart_str_appends(str, ", ");
1838
22
        }
1839
110
        if (key) {
1840
0
          smart_str_appendc(str, '\'');
1841
0
          zend_ast_export_str(str, key);
1842
0
          smart_str_appends(str, "' => ");
1843
44
        } else {
1844
44
          smart_str_append_long(str, idx);
1845
44
          smart_str_appends(str, " => ");
1846
44
        }
1847
110
        zend_ast_export_zval(str, val, 0, indent);
1848
110
      } ZEND_HASH_FOREACH_END();
1849
22
      smart_str_appendc(str, ']');
1850
22
      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
2.61k
  }
1857
2.61k
}
1858
1859
94
static ZEND_COLD void zend_ast_export_class_no_header(smart_str *str, const zend_ast_decl *decl, int indent) {
1860
94
  if (decl->child[0]) {
1861
20
    smart_str_appends(str, " extends ");
1862
20
    zend_ast_export_ns_name(str, decl->child[0], 0, indent);
1863
20
  }
1864
94
  if (decl->child[1]) {
1865
18
    smart_str_appends(str, " implements ");
1866
18
    zend_ast_export_ex(str, decl->child[1], 0, indent);
1867
18
  }
1868
94
  smart_str_appends(str, " {\n");
1869
94
  zend_ast_export_stmt(str, decl->child[2], indent + 1);
1870
94
  zend_ast_export_indent(str, indent);
1871
94
  smart_str_appendc(str, '}');
1872
94
}
1873
1874
48
static ZEND_COLD void zend_ast_export_attribute_group(smart_str *str, zend_ast *ast, int indent) {
1875
48
  const zend_ast_list *list = zend_ast_get_list(ast);
1876
96
  for (uint32_t i = 0; i < list->children; i++) {
1877
48
    const zend_ast *attr = list->child[i];
1878
1879
48
    if (i) {
1880
0
      smart_str_appends(str, ", ");
1881
0
    }
1882
48
    zend_ast_export_ns_name(str, attr->child[0], 0, indent);
1883
1884
48
    if (attr->child[1]) {
1885
18
      smart_str_appendc(str, '(');
1886
18
      zend_ast_export_ex(str, attr->child[1], 0, indent);
1887
18
      smart_str_appendc(str, ')');
1888
18
    }
1889
48
  }
1890
48
}
1891
1892
42
static ZEND_COLD void zend_ast_export_attributes(smart_str *str, zend_ast *ast, int indent, bool newlines) {
1893
42
  const zend_ast_list *list = zend_ast_get_list(ast);
1894
42
  uint32_t i;
1895
1896
90
  for (i = 0; i < list->children; i++) {
1897
48
    smart_str_appends(str, "#[");
1898
48
    zend_ast_export_attribute_group(str, list->child[i], indent);
1899
48
    smart_str_appendc(str, ']');
1900
1901
48
    if (newlines) {
1902
20
      smart_str_appendc(str, '\n');
1903
20
      zend_ast_export_indent(str, indent);
1904
28
    } else {
1905
28
      smart_str_appendc(str, ' ');
1906
28
    }
1907
48
  }
1908
42
}
1909
1910
3.22k
static ZEND_COLD void zend_ast_export_visibility(smart_str *str, uint32_t flags, zend_modifier_target target) {
1911
3.22k
  if (flags & ZEND_ACC_PUBLIC) {
1912
118
    smart_str_appends(str, "public ");
1913
3.10k
  } else if (flags & ZEND_ACC_PROTECTED) {
1914
36
    smart_str_appends(str, "protected ");
1915
3.06k
  } else if (flags & ZEND_ACC_PRIVATE) {
1916
36
    smart_str_appends(str, "private ");
1917
36
  }
1918
1919
3.22k
  if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
1920
1.55k
    if (flags & ZEND_ACC_PRIVATE_SET) {
1921
4
      smart_str_appends(str, "private(set) ");
1922
1.54k
    } else if (flags & ZEND_ACC_PROTECTED_SET) {
1923
2
      smart_str_appends(str, "protected(set) ");
1924
1.54k
    } else if (flags & ZEND_ACC_PUBLIC_SET) {
1925
0
      smart_str_appends(str, "public(set) ");
1926
0
    }
1927
1.55k
  }
1928
3.22k
}
1929
1930
1.56k
static ZEND_COLD void zend_ast_export_type(smart_str *str, zend_ast *ast, int indent) {
1931
1.56k
  if (ast->kind == ZEND_AST_TYPE_UNION) {
1932
18
    const zend_ast_list *list = zend_ast_get_list(ast);
1933
54
    for (uint32_t i = 0; i < list->children; i++) {
1934
36
      if (i != 0) {
1935
18
        smart_str_appendc(str, '|');
1936
18
      }
1937
36
      zend_ast_export_type(str, list->child[i], indent);
1938
36
    }
1939
18
    return;
1940
18
  }
1941
1.54k
  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
1.54k
  if (ast->attr & ZEND_TYPE_NULLABLE) {
1952
86
    smart_str_appendc(str, '?');
1953
86
  }
1954
1.54k
  zend_ast_export_ns_name(str, ast, 0, indent);
1955
1.54k
}
1956
1957
static ZEND_COLD void zend_ast_export_hook_list(smart_str *str, const zend_ast_list *hook_list, int indent)
1958
14
{
1959
14
  smart_str_appends(str, " {");
1960
14
  smart_str_appendc(str, '\n');
1961
14
  indent++;
1962
14
  zend_ast_export_indent(str, indent);
1963
1964
36
  for (uint32_t i = 0; i < hook_list->children; i++) {
1965
22
    const zend_ast_decl *hook = (const zend_ast_decl *)hook_list->child[i];
1966
22
    zend_ast_export_visibility(str, hook->flags, ZEND_MODIFIER_TARGET_PROPERTY);
1967
22
    if (hook->flags & ZEND_ACC_FINAL) {
1968
6
      smart_str_appends(str, "final ");
1969
6
    }
1970
22
    smart_str_append(str, hook->name);
1971
22
    zend_ast *body = hook->child[2];
1972
22
    if (body == NULL) {
1973
8
      smart_str_appendc(str, ';');
1974
14
    } else if (body->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
1975
6
      smart_str_appends(str, " => ");
1976
6
      zend_ast_export_ex(str, body->child[0], 0, indent);
1977
6
      smart_str_appendc(str, ';');
1978
8
    } else {
1979
8
      smart_str_appends(str, " {\n");
1980
8
      zend_ast_export_stmt(str, body, indent + 1);
1981
8
      zend_ast_export_indent(str, indent);
1982
8
      smart_str_appendc(str, '}');
1983
8
    }
1984
22
    if (i < (hook_list->children - 1)) {
1985
8
      smart_str_appendc(str, '\n');
1986
8
      zend_ast_export_indent(str, indent);
1987
8
    }
1988
22
  }
1989
14
  smart_str_appendc(str, '\n');
1990
14
  indent--;
1991
14
  zend_ast_export_indent(str, indent);
1992
14
  smart_str_appendc(str, '}');
1993
14
}
1994
1995
3.95k
#define BINARY_OP(_op, _p, _pl, _pr) do { \
1996
3.95k
    op = _op; \
1997
3.95k
    p = _p; \
1998
3.95k
    pl = _pl; \
1999
3.95k
    pr = _pr; \
2000
3.95k
    goto binary_op; \
2001
3.95k
  } while (0)
2002
2003
234
#define PREFIX_OP(_op, _p, _pl) do { \
2004
234
    op = _op; \
2005
234
    p = _p; \
2006
234
    pl = _pl; \
2007
234
    goto prefix_op; \
2008
234
  } while (0)
2009
2010
214
#define FUNC_OP(_op) do { \
2011
214
    op = _op; \
2012
214
    goto func_op; \
2013
214
  } while (0)
2014
2015
74
#define POSTFIX_OP(_op, _p, _pl) do { \
2016
74
    op = _op; \
2017
74
    p = _p; \
2018
74
    pl = _pl; \
2019
74
    goto postfix_op; \
2020
74
  } while (0)
2021
2022
302
#define APPEND_NODE_1(_op) do { \
2023
302
    op = _op; \
2024
302
    goto append_node_1; \
2025
302
  } while (0)
2026
2027
90
#define APPEND_STR(_op) do { \
2028
90
    op = _op; \
2029
90
    goto append_str; \
2030
90
  } while (0)
2031
2032
140
#define APPEND_DEFAULT_VALUE(n) do { \
2033
140
    p = n; \
2034
140
    goto append_default_value; \
2035
140
  } while (0)
2036
2037
static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int priority, int indent)
2038
61.6k
{
2039
61.6k
  const zend_ast_decl *decl;
2040
61.6k
  int p, pl, pr;
2041
61.6k
  const char *op;
2042
2043
62.0k
tail_call:
2044
62.0k
  if (!ast) {
2045
1.57k
    return;
2046
1.57k
  }
2047
60.5k
  switch (ast->kind) {
2048
    /* special nodes */
2049
2.56k
    case ZEND_AST_ZVAL:
2050
2.56k
      zend_ast_export_zval(str, zend_ast_get_zval(ast), priority, indent);
2051
2.56k
      break;
2052
44
    case ZEND_AST_CONSTANT: {
2053
44
      zend_string *name = zend_ast_get_constant_name(ast);
2054
44
      smart_str_appendl(str, ZSTR_VAL(name), ZSTR_LEN(name));
2055
44
      break;
2056
0
    }
2057
2
    case ZEND_AST_OP_ARRAY:
2058
2
      smart_str_appends(str, "Closure(");
2059
2
      smart_str_append(str, zend_ast_get_op_array(ast)->op_array->function_name);
2060
2
      smart_str_appendc(str, ')');
2061
2
      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
150
    case ZEND_AST_CLOSURE:
2073
1.57k
    case ZEND_AST_ARROW_FUNC:
2074
1.63k
    case ZEND_AST_METHOD:
2075
1.63k
      decl = (const zend_ast_decl *) ast;
2076
1.63k
      if (decl->kind == ZEND_AST_ARROW_FUNC && (decl->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) {
2077
4
        smart_str_appendc(str, '(');
2078
4
      }
2079
1.63k
      if (decl->child[4]) {
2080
14
        bool newlines = !(ast->kind == ZEND_AST_CLOSURE || ast->kind == ZEND_AST_ARROW_FUNC);
2081
14
        zend_ast_export_attributes(str, decl->child[4], indent, newlines);
2082
14
      }
2083
2084
1.63k
      zend_ast_export_visibility(str, decl->flags, ZEND_MODIFIER_TARGET_METHOD);
2085
2086
1.63k
      if (decl->flags & ZEND_ACC_STATIC) {
2087
22
        smart_str_appends(str, "static ");
2088
22
      }
2089
1.63k
      if (decl->flags & ZEND_ACC_ABSTRACT) {
2090
18
        smart_str_appends(str, "abstract ");
2091
18
      }
2092
1.63k
      if (decl->flags & ZEND_ACC_FINAL) {
2093
18
        smart_str_appends(str, "final ");
2094
18
      }
2095
1.63k
      if (decl->kind == ZEND_AST_ARROW_FUNC) {
2096
1.42k
        smart_str_appends(str, "fn");
2097
1.42k
      } else {
2098
212
        smart_str_appends(str, "function ");
2099
212
      }
2100
1.63k
      if (decl->flags & ZEND_ACC_RETURN_REFERENCE) {
2101
74
        smart_str_appendc(str, '&');
2102
74
      }
2103
1.63k
      if (ast->kind != ZEND_AST_CLOSURE && ast->kind != ZEND_AST_ARROW_FUNC) {
2104
62
        smart_str_appendl(str, ZSTR_VAL(decl->name), ZSTR_LEN(decl->name));
2105
62
      }
2106
1.63k
      smart_str_appendc(str, '(');
2107
1.63k
      zend_ast_export_ex(str, decl->child[0], 0, indent);
2108
1.63k
      smart_str_appendc(str, ')');
2109
1.63k
      zend_ast_export_ex(str, decl->child[1], 0, indent);
2110
1.63k
      if (decl->child[3]) {
2111
80
        smart_str_appends(str, ": ");
2112
80
        zend_ast_export_type(str, decl->child[3], indent);
2113
80
      }
2114
1.63k
      if (decl->child[2]) {
2115
1.61k
        if (decl->kind == ZEND_AST_ARROW_FUNC) {
2116
1.42k
          zend_ast *body = decl->child[2];
2117
1.42k
          if (body->kind == ZEND_AST_RETURN) {
2118
0
            body = body->child[0];
2119
0
          }
2120
1.42k
          smart_str_appends(str, " => ");
2121
1.42k
          zend_ast_export_ex(str, body, 0, indent);
2122
1.42k
          if (decl->attr & ZEND_PARENTHESIZED_ARROW_FUNC) {
2123
4
            smart_str_appendc(str, ')');
2124
4
          }
2125
1.42k
          break;
2126
1.42k
        }
2127
2128
194
        smart_str_appends(str, " {\n");
2129
194
        zend_ast_export_stmt(str, decl->child[2], indent + 1);
2130
194
        zend_ast_export_indent(str, indent);
2131
194
        smart_str_appendc(str, '}');
2132
194
        if (ast->kind != ZEND_AST_CLOSURE) {
2133
44
          smart_str_appendc(str, '\n');
2134
44
        }
2135
194
      } else {
2136
18
        smart_str_appends(str, ";\n");
2137
18
      }
2138
212
      break;
2139
212
    case ZEND_AST_CLASS:
2140
68
      decl = (const zend_ast_decl *) ast;
2141
68
      if (decl->child[3]) {
2142
10
        zend_ast_export_attributes(str, decl->child[3], indent, true);
2143
10
      }
2144
68
      if (decl->flags & ZEND_ACC_INTERFACE) {
2145
2
        smart_str_appends(str, "interface ");
2146
66
      } else if (decl->flags & ZEND_ACC_TRAIT) {
2147
2
        smart_str_appends(str, "trait ");
2148
64
      } else if (decl->flags & ZEND_ACC_ENUM) {
2149
4
        smart_str_appends(str, "enum ");
2150
60
      } else {
2151
60
        if (decl->flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
2152
18
          smart_str_appends(str, "abstract ");
2153
18
        }
2154
60
        if (decl->flags & ZEND_ACC_FINAL) {
2155
18
          smart_str_appends(str, "final ");
2156
18
        }
2157
60
        if (decl->flags & ZEND_ACC_READONLY_CLASS) {
2158
0
          smart_str_appends(str, "readonly ");
2159
0
        }
2160
60
        smart_str_appends(str, "class ");
2161
60
      }
2162
68
      smart_str_appendl(str, ZSTR_VAL(decl->name), ZSTR_LEN(decl->name));
2163
68
      if (decl->flags & ZEND_ACC_ENUM && decl->child[4]) {
2164
2
        smart_str_appends(str, ": ");
2165
2
        zend_ast_export_type(str, decl->child[4], indent);
2166
2
      }
2167
68
      zend_ast_export_class_no_header(str, decl, indent);
2168
68
      smart_str_appendc(str, '\n');
2169
68
      break;
2170
2171
    /* list nodes */
2172
890
    case ZEND_AST_ARG_LIST:
2173
944
    case ZEND_AST_EXPR_LIST:
2174
2.57k
    case ZEND_AST_PARAM_LIST:
2175
2.70k
simple_list:
2176
2.70k
      zend_ast_export_list(str, zend_ast_get_list(ast), true, 20, indent);
2177
2.70k
      break;
2178
126
    case ZEND_AST_ARRAY:
2179
126
      smart_str_appendc(str, '[');
2180
126
      zend_ast_export_list(str, zend_ast_get_list(ast), true, 20, indent);
2181
126
      smart_str_appendc(str, ']');
2182
126
      break;
2183
1.54k
    case ZEND_AST_ENCAPS_LIST:
2184
1.54k
      smart_str_appendc(str, '"');
2185
1.54k
      zend_ast_export_encaps_list(str, '"', zend_ast_get_list(ast), indent);
2186
1.54k
      smart_str_appendc(str, '"');
2187
1.54k
      break;
2188
0
    case ZEND_AST_STMT_LIST:
2189
18
    case ZEND_AST_TRAIT_ADAPTATIONS:
2190
18
      zend_ast_export_stmt(str, ast, indent);
2191
18
      break;
2192
90
    case ZEND_AST_IF:
2193
90
      zend_ast_export_if_stmt(str, zend_ast_get_list(ast), indent);
2194
90
      break;
2195
18
    case ZEND_AST_SWITCH_LIST:
2196
36
    case ZEND_AST_CATCH_LIST:
2197
42
    case ZEND_AST_MATCH_ARM_LIST:
2198
42
      zend_ast_export_list(str, zend_ast_get_list(ast), false, 0, indent);
2199
42
      break;
2200
54
    case ZEND_AST_CLOSURE_USES:
2201
54
      smart_str_appends(str, " use(");
2202
54
      zend_ast_export_var_list(str, zend_ast_get_list(ast), indent);
2203
54
      smart_str_appendc(str, ')');
2204
54
      break;
2205
84
    case ZEND_AST_PROP_GROUP: {
2206
84
      zend_ast *type_ast = ast->child[0];
2207
84
      zend_ast *prop_ast = ast->child[1];
2208
2209
84
      if (ast->child[2]) {
2210
2
        zend_ast_export_attributes(str, ast->child[2], indent, true);
2211
2
      }
2212
2213
84
      zend_ast_export_visibility(str, ast->attr, ZEND_MODIFIER_TARGET_PROPERTY);
2214
2215
84
      if (ast->attr & ZEND_ACC_STATIC) {
2216
18
        smart_str_appends(str, "static ");
2217
18
      }
2218
84
      if (ast->attr & ZEND_ACC_READONLY) {
2219
0
        smart_str_appends(str, "readonly ");
2220
0
      }
2221
2222
84
      if (type_ast) {
2223
12
        zend_ast_export_type(str, type_ast, indent);
2224
12
        smart_str_appendc(str, ' ');
2225
12
      }
2226
2227
84
      ast = prop_ast;
2228
84
      goto simple_list;
2229
36
    }
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
40
    case ZEND_AST_CLASS_CONST_GROUP:
2252
40
      if (ast->child[1]) {
2253
2
        zend_ast_export_attributes(str, ast->child[1], indent, true);
2254
2
      }
2255
2256
40
      zend_ast_export_visibility(str, ast->attr, ZEND_MODIFIER_TARGET_CONSTANT);
2257
40
      smart_str_appends(str, "const ");
2258
40
      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
40
      ast = ast->child[0];
2264
2265
40
      goto simple_list;
2266
72
    case ZEND_AST_NAME_LIST:
2267
72
      zend_ast_export_name_list(str, zend_ast_get_list(ast), indent);
2268
72
      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
90
    case ZEND_AST_TYPE:
2294
90
      switch (ast->attr & ~ZEND_TYPE_NULLABLE) {
2295
72
        case IS_ARRAY:    APPEND_STR("array");
2296
0
        case IS_CALLABLE: APPEND_STR("callable");
2297
18
        case IS_STATIC:   APPEND_STR("static");
2298
0
        case IS_MIXED:    APPEND_STR("mixed");
2299
0
        EMPTY_SWITCH_DEFAULT_CASE();
2300
90
      }
2301
0
      break;
2302
2303
    /* 1 child node */
2304
27.8k
    case ZEND_AST_VAR:
2305
27.8k
      smart_str_appendc(str, '$');
2306
27.8k
      zend_ast_export_var(str, ast->child[0], 0, indent);
2307
27.8k
      break;
2308
1.06k
    case ZEND_AST_CONST:
2309
1.06k
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2310
1.06k
      break;
2311
20
    case ZEND_AST_UNPACK:
2312
20
      smart_str_appends(str, "...");
2313
20
      ast = ast->child[0];
2314
20
      goto tail_call;
2315
2
    case ZEND_AST_UNARY_PLUS:  PREFIX_OP("+", 240, 241);
2316
30
    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
14
    case ZEND_AST_EMPTY:
2333
14
      FUNC_OP("empty");
2334
12
    case ZEND_AST_ISSET:
2335
12
      FUNC_OP("isset");
2336
18
    case ZEND_AST_SILENCE:
2337
18
      PREFIX_OP("@", 240, 241);
2338
20
    case ZEND_AST_SHELL_EXEC:
2339
20
      smart_str_appendc(str, '`');
2340
20
      if (ast->child[0]->kind == ZEND_AST_ENCAPS_LIST) {
2341
18
        zend_ast_export_encaps_list(str, '`', zend_ast_get_list(ast->child[0]), indent);
2342
18
      } else {
2343
2
        zval *zv;
2344
2
        ZEND_ASSERT(ast->child[0]->kind == ZEND_AST_ZVAL);
2345
2
        zv = zend_ast_get_zval(ast->child[0]);
2346
2
        ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
2347
2
        zend_ast_export_qstr(str, '`', Z_STR_P(zv));
2348
2
      }
2349
20
      smart_str_appendc(str, '`');
2350
20
      break;
2351
18
    case ZEND_AST_PRINT:
2352
18
      PREFIX_OP("print ", 60, 61);
2353
174
    case ZEND_AST_INCLUDE_OR_EVAL:
2354
174
      switch (ast->attr) {
2355
160
        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
14
        case ZEND_EVAL:         FUNC_OP("eval");
2360
0
        EMPTY_SWITCH_DEFAULT_CASE();
2361
174
      }
2362
0
      break;
2363
132
    case ZEND_AST_UNARY_OP:
2364
132
      switch (ast->attr) {
2365
82
        case ZEND_BW_NOT:   PREFIX_OP("~", 240, 241);
2366
50
        case ZEND_BOOL_NOT: PREFIX_OP("!", 240, 241);
2367
0
        EMPTY_SWITCH_DEFAULT_CASE();
2368
132
      }
2369
0
      break;
2370
0
    case ZEND_AST_PRE_INC:
2371
0
      PREFIX_OP("++", 240, 241);
2372
18
    case ZEND_AST_PRE_DEC:
2373
18
      PREFIX_OP("--", 240, 241);
2374
54
    case ZEND_AST_POST_INC:
2375
54
      POSTFIX_OP("++", 240, 241);
2376
20
    case ZEND_AST_POST_DEC:
2377
20
      POSTFIX_OP("--", 240, 241);
2378
2379
28
    case ZEND_AST_GLOBAL:
2380
28
      APPEND_NODE_1("global");
2381
14
    case ZEND_AST_UNSET:
2382
14
      FUNC_OP("unset");
2383
122
    case ZEND_AST_RETURN:
2384
122
      APPEND_NODE_1("return");
2385
20
    case ZEND_AST_LABEL:
2386
20
      zend_ast_export_name(str, ast->child[0], 0, indent);
2387
20
      smart_str_appendc(str, ':');
2388
20
      break;
2389
18
    case ZEND_AST_REF:
2390
18
      smart_str_appendc(str, '&');
2391
18
      ast = ast->child[0];
2392
18
      goto tail_call;
2393
0
    case ZEND_AST_HALT_COMPILER:
2394
0
      APPEND_STR("__HALT_COMPILER()");
2395
80
    case ZEND_AST_ECHO:
2396
80
      APPEND_NODE_1("echo");
2397
0
    case ZEND_AST_THROW:
2398
0
      APPEND_NODE_1("throw");
2399
18
    case ZEND_AST_GOTO:
2400
18
      smart_str_appends(str, "goto ");
2401
18
      zend_ast_export_name(str, ast->child[0], 0, indent);
2402
18
      break;
2403
36
    case ZEND_AST_BREAK:
2404
36
      APPEND_NODE_1("break");
2405
36
    case ZEND_AST_CONTINUE:
2406
36
      APPEND_NODE_1("continue");
2407
2408
    /* 2 child nodes */
2409
182
    case ZEND_AST_DIM:
2410
182
      zend_ast_export_ex(str, ast->child[0], 260, indent);
2411
182
      smart_str_appendc(str, '[');
2412
182
      if (ast->child[1]) {
2413
182
        zend_ast_export_ex(str, ast->child[1], 0, indent);
2414
182
      }
2415
182
      smart_str_appendc(str, ']');
2416
182
      break;
2417
12.2k
    case ZEND_AST_PROP:
2418
13.8k
    case ZEND_AST_NULLSAFE_PROP:
2419
13.8k
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2420
13.8k
      smart_str_appends(str, ast->kind == ZEND_AST_NULLSAFE_PROP ? "?->" : "->");
2421
13.8k
      zend_ast_export_var(str, ast->child[1], 0, indent);
2422
13.8k
      break;
2423
62
    case ZEND_AST_STATIC_PROP:
2424
62
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2425
62
      smart_str_appends(str, "::$");
2426
62
      zend_ast_export_var(str, ast->child[1], 0, indent);
2427
62
      break;
2428
698
    case ZEND_AST_CALL: {
2429
698
      zend_ast *left = ast->child[0];
2430
698
      if (left->kind == ZEND_AST_ARROW_FUNC || left->kind == ZEND_AST_CLOSURE) {
2431
146
        smart_str_appendc(str, '(');
2432
146
        zend_ast_export_ns_name(str, left, 0, indent);
2433
146
        smart_str_appendc(str, ')');
2434
552
      } else {
2435
552
        zend_ast_export_ns_name(str, left, 0, indent);
2436
552
      }
2437
698
      smart_str_appendc(str, '(');
2438
698
      zend_ast_export_ex(str, ast->child[1], 0, indent);
2439
698
      smart_str_appendc(str, ')');
2440
698
      break;
2441
12.2k
    }
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
24
    case ZEND_AST_CALLABLE_CONVERT:
2449
24
      smart_str_appends(str, "...");
2450
24
      break;
2451
68
    case ZEND_AST_CLASS_CONST:
2452
68
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2453
68
      smart_str_appends(str, "::");
2454
68
      zend_ast_export_name(str, ast->child[1], 0, indent);
2455
68
      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
804
    case ZEND_AST_ASSIGN:            BINARY_OP(" = ",   90, 91, 90);
2474
0
    case ZEND_AST_ASSIGN_REF:        BINARY_OP(" =& ",  90, 91, 90);
2475
6
    case ZEND_AST_ASSIGN_OP:
2476
6
      switch (ast->attr) {
2477
0
        case ZEND_ADD:    BINARY_OP(" += ",  90, 91, 90);
2478
2
        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
2
        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
6
      }
2491
0
      break;
2492
0
    case ZEND_AST_ASSIGN_COALESCE: BINARY_OP(" \?\?= ", 90, 91, 90);
2493
2.77k
    case ZEND_AST_BINARY_OP:
2494
2.77k
      switch (ast->attr) {
2495
26
        case ZEND_ADD:                 BINARY_OP(" + ",   200, 200, 201);
2496
432
        case ZEND_SUB:                 BINARY_OP(" - ",   200, 200, 201);
2497
1.27k
        case ZEND_MUL:                 BINARY_OP(" * ",   210, 210, 211);
2498
18
        case ZEND_DIV:                 BINARY_OP(" / ",   210, 210, 211);
2499
140
        case ZEND_MOD:                 BINARY_OP(" % ",   210, 210, 211);
2500
4
        case ZEND_SL:                  BINARY_OP(" << ",  190, 190, 191);
2501
0
        case ZEND_SR:                  BINARY_OP(" >> ",  190, 190, 191);
2502
230
        case ZEND_CONCAT:              BINARY_OP(" . ",   185, 185, 186);
2503
0
        case ZEND_BW_OR:               BINARY_OP(" | ",   140, 140, 141);
2504
276
        case ZEND_BW_AND:              BINARY_OP(" & ",   160, 160, 161);
2505
12
        case ZEND_BW_XOR:              BINARY_OP(" ^ ",   150, 150, 151);
2506
75
        case ZEND_IS_IDENTICAL:        BINARY_OP(" === ", 170, 171, 171);
2507
0
        case ZEND_IS_NOT_IDENTICAL:    BINARY_OP(" !== ", 170, 171, 171);
2508
124
        case ZEND_IS_EQUAL:            BINARY_OP(" == ",  170, 171, 171);
2509
0
        case ZEND_IS_NOT_EQUAL:        BINARY_OP(" != ",  170, 171, 171);
2510
164
        case ZEND_IS_SMALLER:          BINARY_OP(" < ",   180, 181, 181);
2511
0
        case ZEND_IS_SMALLER_OR_EQUAL: BINARY_OP(" <= ",  180, 181, 181);
2512
2
        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
2.77k
      }
2517
0
      break;
2518
26
    case ZEND_AST_GREATER:                 BINARY_OP(" > ",   180, 181, 181);
2519
0
    case ZEND_AST_GREATER_EQUAL:           BINARY_OP(" >= ",  180, 181, 181);
2520
240
    case ZEND_AST_AND:                     BINARY_OP(" && ",  130, 130, 131);
2521
48
    case ZEND_AST_OR:                      BINARY_OP(" || ",  120, 120, 121);
2522
42
    case ZEND_AST_PIPE:                    BINARY_OP(" |> ",  183, 183, 184);
2523
296
    case ZEND_AST_ARRAY_ELEM:
2524
296
      if (ast->child[1]) {
2525
52
        zend_ast_export_ex(str, ast->child[1], 80, indent);
2526
52
        smart_str_appends(str, " => ");
2527
52
      }
2528
296
      if (ast->attr)
2529
0
        smart_str_appendc(str, '&');
2530
296
      zend_ast_export_ex(str, ast->child[0], 80, indent);
2531
296
      break;
2532
86
    case ZEND_AST_NEW:
2533
86
      smart_str_appends(str, "new ");
2534
86
      if (ast->child[0]->kind == ZEND_AST_CLASS) {
2535
26
        const zend_ast_decl *decl = (const zend_ast_decl *) ast->child[0];
2536
26
        if (decl->child[3]) {
2537
6
          zend_ast_export_attributes(str, decl->child[3], indent, false);
2538
6
        }
2539
26
        smart_str_appends(str, "class");
2540
26
        if (!zend_ast_is_list(ast->child[1])
2541
24
            || zend_ast_get_list(ast->child[1])->children) {
2542
4
          smart_str_appendc(str, '(');
2543
4
          zend_ast_export_ex(str, ast->child[1], 0, indent);
2544
4
          smart_str_appendc(str, ')');
2545
4
        }
2546
26
        zend_ast_export_class_no_header(str, decl, indent);
2547
60
      } else {
2548
60
        zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2549
60
        smart_str_appendc(str, '(');
2550
60
        zend_ast_export_ex(str, ast->child[1], 0, indent);
2551
60
        smart_str_appendc(str, ')');
2552
60
      }
2553
86
      break;
2554
22
    case ZEND_AST_INSTANCEOF:
2555
22
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2556
22
      smart_str_appends(str, " instanceof ");
2557
22
      zend_ast_export_ns_name(str, ast->child[1], 0, indent);
2558
22
      break;
2559
22
    case ZEND_AST_YIELD:
2560
22
      if (priority > 70) smart_str_appendc(str, '(');
2561
22
      smart_str_appends(str, "yield ");
2562
22
      if (ast->child[0]) {
2563
22
        if (ast->child[1]) {
2564
14
          zend_ast_export_ex(str, ast->child[1], 70, indent);
2565
14
          smart_str_appends(str, " => ");
2566
14
        }
2567
22
        zend_ast_export_ex(str, ast->child[0], 70, indent);
2568
22
      }
2569
22
      if (priority > 70) smart_str_appendc(str, ')');
2570
22
      break;
2571
14
    case ZEND_AST_YIELD_FROM:
2572
14
      PREFIX_OP("yield from ", 85, 86);
2573
14
    case ZEND_AST_COALESCE: BINARY_OP(" ?? ", 110, 111, 110);
2574
28
    case ZEND_AST_STATIC:
2575
28
      smart_str_appends(str, "static $");
2576
28
      zend_ast_export_name(str, ast->child[0], 0, indent);
2577
28
      APPEND_DEFAULT_VALUE(1);
2578
18
    case ZEND_AST_WHILE:
2579
18
      smart_str_appends(str, "while (");
2580
18
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2581
18
      smart_str_appends(str, ") {\n");
2582
18
      zend_ast_export_stmt(str, ast->child[1], indent + 1);
2583
18
      zend_ast_export_indent(str, indent);
2584
18
      smart_str_appendc(str, '}');
2585
18
      break;
2586
36
    case ZEND_AST_DO_WHILE:
2587
36
      smart_str_appends(str, "do {\n");
2588
36
      zend_ast_export_stmt(str, ast->child[0], indent + 1);
2589
36
      zend_ast_export_indent(str, indent);
2590
36
      smart_str_appends(str, "} while (");
2591
36
      zend_ast_export_ex(str, ast->child[1], 0, indent);
2592
36
      smart_str_appendc(str, ')');
2593
36
      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
18
    case ZEND_AST_SWITCH:
2609
18
      smart_str_appends(str, "switch (");
2610
18
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2611
18
      smart_str_appends(str, ") {\n");
2612
18
      zend_ast_export_ex(str, ast->child[1], 0, indent + 1);
2613
18
      zend_ast_export_indent(str, indent);
2614
18
      smart_str_appendc(str, '}');
2615
18
      break;
2616
106
    case ZEND_AST_SWITCH_CASE:
2617
106
      zend_ast_export_indent(str, indent);
2618
106
      if (ast->child[0]) {
2619
88
        smart_str_appends(str, "case ");
2620
88
        zend_ast_export_ex(str, ast->child[0], 0, indent);
2621
88
        smart_str_appends(str, ":\n");
2622
88
      } else {
2623
18
        smart_str_appends(str, "default:\n");
2624
18
      }
2625
106
      zend_ast_export_stmt(str, ast->child[1], indent + 1);
2626
106
      break;
2627
6
    case ZEND_AST_MATCH:
2628
6
      smart_str_appends(str, "match (");
2629
6
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2630
6
      smart_str_appends(str, ") {\n");
2631
6
      zend_ast_export_ex(str, ast->child[1], 0, indent + 1);
2632
6
      zend_ast_export_indent(str, indent);
2633
6
      smart_str_appendc(str, '}');
2634
6
      break;
2635
18
    case ZEND_AST_MATCH_ARM:
2636
18
      zend_ast_export_indent(str, indent);
2637
18
      if (ast->child[0]) {
2638
14
        zend_ast_export_list(str, zend_ast_get_list(ast->child[0]), true, 0, indent);
2639
14
        smart_str_appends(str, " => ");
2640
14
      } else {
2641
4
        smart_str_appends(str, "default => ");
2642
4
      }
2643
18
      zend_ast_export_ex(str, ast->child[1], 0, 0);
2644
18
      smart_str_appends(str, ",\n");
2645
18
      break;
2646
36
    case ZEND_AST_DECLARE:
2647
36
      smart_str_appends(str, "declare(");
2648
36
      ZEND_ASSERT(ast->child[0]->kind == ZEND_AST_CONST_DECL);
2649
36
      zend_ast_export_list(str, zend_ast_get_list(ast->child[0]), true, 0, indent);
2650
36
      smart_str_appendc(str, ')');
2651
36
      if (ast->child[1]) {
2652
18
        smart_str_appends(str, " {\n");
2653
18
        zend_ast_export_stmt(str, ast->child[1], indent + 1);
2654
18
        zend_ast_export_indent(str, indent);
2655
18
        smart_str_appendc(str, '}');
2656
18
      } else {
2657
18
        smart_str_appendc(str, ';');
2658
18
      }
2659
36
      break;
2660
102
    case ZEND_AST_PROP_ELEM:
2661
102
      smart_str_appendc(str, '$');
2662
102
      zend_ast_export_name(str, ast->child[0], 0, indent);
2663
2664
102
      zend_ast *default_value = ast->child[1];
2665
102
      if (default_value) {
2666
40
        smart_str_appends(str, " = ");
2667
40
        zend_ast_export_ex(str, default_value, 0, indent + 1);
2668
40
      }
2669
2670
102
      if (ast->child[3]) {
2671
12
        zend_ast_export_hook_list(str, zend_ast_get_list(ast->child[3]), indent);
2672
12
      }
2673
102
      break;
2674
112
    case ZEND_AST_CONST_ELEM:
2675
112
      zend_ast_export_name(str, ast->child[0], 0, indent);
2676
112
      APPEND_DEFAULT_VALUE(1);
2677
36
    case ZEND_AST_USE_TRAIT:
2678
36
      smart_str_appends(str, "use ");
2679
36
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2680
36
      if (ast->child[1]) {
2681
18
        smart_str_appends(str, " {\n");
2682
18
        zend_ast_export_ex(str, ast->child[1], 0, indent + 1);
2683
18
        zend_ast_export_indent(str, indent);
2684
18
        smart_str_appendc(str, '}');
2685
18
      } else {
2686
18
        smart_str_appendc(str, ';');
2687
18
      }
2688
36
      break;
2689
18
    case ZEND_AST_TRAIT_PRECEDENCE:
2690
18
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2691
18
      smart_str_appends(str, " insteadof ");
2692
18
      zend_ast_export_ex(str, ast->child[1], 0, indent);
2693
18
      break;
2694
72
    case ZEND_AST_METHOD_REFERENCE:
2695
72
      if (ast->child[0]) {
2696
36
        zend_ast_export_name(str, ast->child[0], 0, indent);
2697
36
        smart_str_appends(str, "::");
2698
36
      }
2699
72
      zend_ast_export_name(str, ast->child[1], 0, indent);
2700
72
      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
54
    case ZEND_AST_TRAIT_ALIAS:
2718
54
      zend_ast_export_name(str, ast->child[0], 0, indent);
2719
54
      if (ast->attr & ZEND_ACC_PUBLIC) {
2720
18
        smart_str_appends(str, " as public");
2721
36
      } else if (ast->attr & ZEND_ACC_PROTECTED) {
2722
18
        smart_str_appends(str, " as protected");
2723
18
      } else if (ast->attr & ZEND_ACC_PRIVATE) {
2724
0
        smart_str_appends(str, " as private");
2725
18
      } else if (ast->child[1]) {
2726
18
        smart_str_appends(str, " as");
2727
18
      }
2728
54
      if (ast->child[1]) {
2729
36
        smart_str_appendc(str, ' ');
2730
36
        zend_ast_export_name(str, ast->child[1], 0, indent);
2731
36
      }
2732
54
      break;
2733
38
    case ZEND_AST_NAMED_ARG:
2734
38
      smart_str_append(str, zend_ast_get_str(ast->child[0]));
2735
38
      smart_str_appends(str, ": ");
2736
38
      ast = ast->child[1];
2737
38
      goto tail_call;
2738
2739
    /* 3 child nodes */
2740
54
    case ZEND_AST_METHOD_CALL:
2741
72
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2742
72
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2743
72
      smart_str_appends(str, ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL ? "?->" : "->");
2744
72
      zend_ast_export_var(str, ast->child[1], 0, indent);
2745
72
      smart_str_appendc(str, '(');
2746
72
      zend_ast_export_ex(str, ast->child[2], 0, indent);
2747
72
      smart_str_appendc(str, ')');
2748
72
      break;
2749
62
    case ZEND_AST_STATIC_CALL:
2750
62
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2751
62
      smart_str_appends(str, "::");
2752
62
      zend_ast_export_var(str, ast->child[1], 0, indent);
2753
62
      smart_str_appendc(str, '(');
2754
62
      zend_ast_export_ex(str, ast->child[2], 0, indent);
2755
62
      smart_str_appendc(str, ')');
2756
62
      break;
2757
30
    case ZEND_AST_CONDITIONAL:
2758
30
      if (priority > 100) smart_str_appendc(str, '(');
2759
30
      zend_ast_export_ex(str, ast->child[0], 100, indent);
2760
30
      if (ast->child[1]) {
2761
14
        smart_str_appends(str, " ? ");
2762
14
        zend_ast_export_ex(str, ast->child[1], 101, indent);
2763
14
        smart_str_appends(str, " : ");
2764
16
      } else {
2765
16
        smart_str_appends(str, " ?: ");
2766
16
      }
2767
30
      zend_ast_export_ex(str, ast->child[2], 101, indent);
2768
30
      if (priority > 100) smart_str_appendc(str, ')');
2769
30
      break;
2770
2771
18
    case ZEND_AST_TRY:
2772
18
      smart_str_appends(str, "try {\n");
2773
18
      zend_ast_export_stmt(str, ast->child[0], indent + 1);
2774
18
      zend_ast_export_indent(str, indent);
2775
18
      zend_ast_export_ex(str, ast->child[1], 0, indent);
2776
18
      if (ast->child[2]) {
2777
18
        smart_str_appends(str, "} finally {\n");
2778
18
        zend_ast_export_stmt(str, ast->child[2], indent + 1);
2779
18
        zend_ast_export_indent(str, indent);
2780
18
      }
2781
18
      smart_str_appendc(str, '}');
2782
18
      break;
2783
36
    case ZEND_AST_CATCH:
2784
36
      smart_str_appends(str, "} catch (");
2785
36
      zend_ast_export_catch_name_list(str, zend_ast_get_list(ast->child[0]), indent);
2786
36
      if (ast->child[1]) {
2787
36
        smart_str_appends(str, " $");
2788
36
        zend_ast_export_var(str, ast->child[1], 0, indent);
2789
36
      }
2790
36
      smart_str_appends(str, ") {\n");
2791
36
      zend_ast_export_stmt(str, ast->child[2], indent + 1);
2792
36
      zend_ast_export_indent(str, indent);
2793
36
      break;
2794
1.44k
    case ZEND_AST_PARAM:
2795
1.44k
      if (ast->child[3]) {
2796
6
        zend_ast_export_attributes(str, ast->child[3], indent, false);
2797
6
      }
2798
1.44k
      zend_ast_export_visibility(str, ast->attr, ZEND_MODIFIER_TARGET_CPP);
2799
1.44k
      if (ast->attr & ZEND_ACC_FINAL) {
2800
2
        smart_str_appends(str, "final ");
2801
2
      }
2802
1.44k
      if (ast->child[0]) {
2803
1.43k
        zend_ast_export_type(str, ast->child[0], indent);
2804
1.43k
        smart_str_appendc(str, ' ');
2805
1.43k
      }
2806
1.44k
      if (ast->attr & ZEND_PARAM_REF) {
2807
662
        smart_str_appendc(str, '&');
2808
662
      }
2809
1.44k
      if (ast->attr & ZEND_PARAM_VARIADIC) {
2810
20
        smart_str_appends(str, "...");
2811
20
      }
2812
1.44k
      smart_str_appendc(str, '$');
2813
1.44k
      zend_ast_export_name(str, ast->child[1], 0, indent);
2814
1.44k
      if (ast->child[2]) {
2815
650
        smart_str_appends(str, " = ");
2816
650
        zend_ast_export_ex(str, ast->child[2], 0, indent);
2817
650
      }
2818
1.44k
      if (ast->child[5]) {
2819
2
        zend_ast_export_hook_list(str, zend_ast_get_list(ast->child[5]), indent);
2820
2
      }
2821
1.44k
      break;
2822
6
    case ZEND_AST_ENUM_CASE:
2823
6
      if (ast->child[3]) {
2824
2
        zend_ast_export_attributes(str, ast->child[3], indent, true);
2825
2
      }
2826
6
      smart_str_appends(str, "case ");
2827
6
      zend_ast_export_name(str, ast->child[0], 0, indent);
2828
6
      if (ast->child[1]) {
2829
4
        smart_str_appends(str, " = ");
2830
4
        zend_ast_export_ex(str, ast->child[1], 0, indent);
2831
4
      }
2832
6
      break;
2833
2834
    /* 4 child nodes */
2835
18
    case ZEND_AST_FOR:
2836
18
      smart_str_appends(str, "for (");
2837
18
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2838
18
      smart_str_appendc(str, ';');
2839
18
      if (ast->child[1]) {
2840
18
        smart_str_appendc(str, ' ');
2841
18
        zend_ast_export_ex(str, ast->child[1], 0, indent);
2842
18
      }
2843
18
      smart_str_appendc(str, ';');
2844
18
      if (ast->child[2]) {
2845
18
        smart_str_appendc(str, ' ');
2846
18
        zend_ast_export_ex(str, ast->child[2], 0, indent);
2847
18
      }
2848
18
      smart_str_appends(str, ") {\n");
2849
18
      zend_ast_export_stmt(str, ast->child[3], indent + 1);
2850
18
      zend_ast_export_indent(str, indent);
2851
18
      smart_str_appendc(str, '}');
2852
18
      break;
2853
18
    case ZEND_AST_FOREACH:
2854
18
      smart_str_appends(str, "foreach (");
2855
18
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2856
18
      smart_str_appends(str, " as ");
2857
18
      if (ast->child[2]) {
2858
18
        zend_ast_export_ex(str, ast->child[2], 0, indent);
2859
18
        smart_str_appends(str, " => ");
2860
18
      }
2861
18
      zend_ast_export_ex(str, ast->child[1], 0, indent);
2862
18
      smart_str_appends(str, ") {\n");
2863
18
      zend_ast_export_stmt(str, ast->child[3], indent + 1);
2864
18
      zend_ast_export_indent(str, indent);
2865
18
      smart_str_appendc(str, '}');
2866
18
      break;
2867
0
    EMPTY_SWITCH_DEFAULT_CASE();
2868
60.5k
  }
2869
55.4k
  return;
2870
2871
55.4k
binary_op:
2872
3.95k
  if (priority > p) smart_str_appendc(str, '(');
2873
3.95k
  zend_ast_export_ex(str, ast->child[0], pl, indent);
2874
3.95k
  smart_str_appends(str, op);
2875
3.95k
  zend_ast_export_ex(str, ast->child[1], pr, indent);
2876
3.95k
  if (priority > p) smart_str_appendc(str, ')');
2877
3.95k
  return;
2878
2879
234
prefix_op:
2880
234
  if (priority > p) smart_str_appendc(str, '(');
2881
234
  smart_str_appends(str, op);
2882
234
  zend_ast_export_ex(str, ast->child[0], pl, indent);
2883
234
  if (priority > p) smart_str_appendc(str, ')');
2884
234
  return;
2885
2886
74
postfix_op:
2887
74
  if (priority > p) smart_str_appendc(str, '(');
2888
74
  zend_ast_export_ex(str, ast->child[0], pl, indent);
2889
74
  smart_str_appends(str, op);
2890
74
  if (priority > p) smart_str_appendc(str, ')');
2891
74
  return;
2892
2893
214
func_op:
2894
214
  smart_str_appends(str, op);
2895
214
  smart_str_appendc(str, '(');
2896
214
  zend_ast_export_ex(str, ast->child[0], 0, indent);
2897
214
  smart_str_appendc(str, ')');
2898
214
  return;
2899
2900
302
append_node_1:
2901
302
  smart_str_appends(str, op);
2902
302
  if (ast->child[0]) {
2903
248
    smart_str_appendc(str, ' ');
2904
248
    ast = ast->child[0];
2905
248
    goto tail_call;
2906
248
  }
2907
54
  return;
2908
2909
90
append_str:
2910
90
  smart_str_appends(str, op);
2911
90
  return;
2912
2913
140
append_default_value:
2914
140
  if (ast->child[p]) {
2915
126
    smart_str_appends(str, " = ");
2916
126
    ast = ast->child[p];
2917
126
    goto tail_call;
2918
126
  }
2919
14
  return;
2920
140
}
2921
2922
ZEND_API ZEND_COLD zend_string *zend_ast_export(const char *prefix, zend_ast *ast, const char *suffix)
2923
810
{
2924
810
  smart_str str = {0};
2925
2926
810
  smart_str_appends(&str, prefix);
2927
810
  zend_ast_export_ex(&str, ast, 0, 0);
2928
810
  smart_str_appends(&str, suffix);
2929
810
  smart_str_0(&str);
2930
810
  return str.s;
2931
810
}
2932
2933
zend_ast * ZEND_FASTCALL zend_ast_with_attributes(zend_ast *ast, zend_ast *attr)
2934
1.63k
{
2935
1.63k
  ZEND_ASSERT(attr->kind == ZEND_AST_ATTRIBUTE_LIST);
2936
2937
1.63k
  switch (ast->kind) {
2938
192
  case ZEND_AST_FUNC_DECL:
2939
340
  case ZEND_AST_CLOSURE:
2940
550
  case ZEND_AST_METHOD:
2941
584
  case ZEND_AST_ARROW_FUNC:
2942
640
  case ZEND_AST_PROPERTY_HOOK:
2943
640
    ((zend_ast_decl *) ast)->child[4] = attr;
2944
640
    break;
2945
599
  case ZEND_AST_CLASS:
2946
599
    ((zend_ast_decl *) ast)->child[3] = attr;
2947
599
    break;
2948
120
  case ZEND_AST_PROP_GROUP:
2949
120
    ast->child[2] = attr;
2950
120
    break;
2951
108
  case ZEND_AST_PARAM:
2952
120
  case ZEND_AST_ENUM_CASE:
2953
120
    ast->child[3] = attr;
2954
120
    break;
2955
66
  case ZEND_AST_CLASS_CONST_GROUP:
2956
66
    ast->child[1] = attr;
2957
66
    break;
2958
92
  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
92
    ast = zend_ast_list_add(ast, attr);
2963
92
    break;
2964
1.63k
  EMPTY_SWITCH_DEFAULT_CASE()
2965
1.63k
  }
2966
2967
1.63k
  return ast;
2968
1.63k
}