Coverage Report

Created: 2025-12-14 06:05

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