Coverage Report

Created: 2026-06-02 06:40

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