Coverage Report

Created: 2022-10-06 21:30

/src/php-src/Zend/zend_ast.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to version 2.00 of the Zend license,     |
8
   | that is bundled with this package in the file LICENSE, and is        |
9
   | available through the world-wide-web at the following url:           |
10
   | http://www.zend.com/license/2_00.txt.                                |
11
   | If you did not receive a copy of the Zend license and are unable to  |
12
   | obtain it through the world-wide-web, please send a note to          |
13
   | license@zend.com so we can mail you a copy immediately.              |
14
   +----------------------------------------------------------------------+
15
   | Authors: Bob Weinand <bwoebi@php.net>                                |
16
   |          Dmitry Stogov <dmitry@php.net>                              |
17
   +----------------------------------------------------------------------+
18
*/
19
20
#include "zend_ast.h"
21
#include "zend_API.h"
22
#include "zend_operators.h"
23
#include "zend_language_parser.h"
24
#include "zend_smart_str.h"
25
#include "zend_exceptions.h"
26
#include "zend_constants.h"
27
28
ZEND_API zend_ast_process_t zend_ast_process = NULL;
29
30
40.3M
static inline void *zend_ast_alloc(size_t size) {
31
40.3M
  return zend_arena_alloc(&CG(ast_arena), size);
32
40.3M
}
33
34
531k
static inline void *zend_ast_realloc(void *old, size_t old_size, size_t new_size) {
35
531k
  void *new = zend_ast_alloc(new_size);
36
531k
  memcpy(new, old, old_size);
37
531k
  return new;
38
531k
}
39
40
15.8M
static inline size_t zend_ast_size(uint32_t children) {
41
15.8M
  return sizeof(zend_ast) - sizeof(zend_ast *) + sizeof(zend_ast *) * children;
42
15.8M
}
43
44
9.08M
static inline size_t zend_ast_list_size(uint32_t children) {
45
9.08M
  return sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * children;
46
9.08M
}
47
48
19.8k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_znode(znode *node) {
49
19.8k
  zend_ast_znode *ast;
50
51
19.8k
  ast = zend_ast_alloc(sizeof(zend_ast_znode));
52
19.8k
  ast->kind = ZEND_AST_ZNODE;
53
19.8k
  ast->attr = 0;
54
19.8k
  ast->lineno = CG(zend_lineno);
55
19.8k
  ast->node = *node;
56
19.8k
  return (zend_ast *) ast;
57
19.8k
}
58
59
15.4M
static zend_always_inline zend_ast * zend_ast_create_zval_int(zval *zv, uint32_t attr, uint32_t lineno) {
60
15.4M
  zend_ast_zval *ast;
61
62
15.4M
  ast = zend_ast_alloc(sizeof(zend_ast_zval));
63
15.4M
  ast->kind = ZEND_AST_ZVAL;
64
15.4M
  ast->attr = attr;
65
15.4M
  ZVAL_COPY_VALUE(&ast->val, zv);
66
15.4M
  Z_LINENO(ast->val) = lineno;
67
15.4M
  return (zend_ast *) ast;
68
15.4M
}
69
70
15.1M
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_with_lineno(zval *zv, uint32_t lineno) {
71
15.1M
  return zend_ast_create_zval_int(zv, 0, lineno);
72
15.1M
}
73
74
3.84k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_ex(zval *zv, zend_ast_attr attr) {
75
3.84k
  return zend_ast_create_zval_int(zv, attr, CG(zend_lineno));
76
3.84k
}
77
78
299k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval(zval *zv) {
79
299k
  return zend_ast_create_zval_int(zv, 0, CG(zend_lineno));
80
299k
}
81
82
21.5k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_str(zend_string *str) {
83
21.5k
  zval zv;
84
21.5k
  ZVAL_STR(&zv, str);
85
21.5k
  return zend_ast_create_zval_int(&zv, 0, CG(zend_lineno));
86
21.5k
}
87
88
463
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_long(zend_long lval) {
89
463
  zval zv;
90
463
  ZVAL_LONG(&zv, lval);
91
463
  return zend_ast_create_zval_int(&zv, 0, CG(zend_lineno));
92
463
}
93
94
33.5k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_constant(zend_string *name, zend_ast_attr attr) {
95
33.5k
  zend_ast_zval *ast;
96
97
33.5k
  ast = zend_ast_alloc(sizeof(zend_ast_zval));
98
33.5k
  ast->kind = ZEND_AST_CONSTANT;
99
33.5k
  ast->attr = attr;
100
33.5k
  ZVAL_STR(&ast->val, name);
101
33.5k
  Z_LINENO(ast->val) = CG(zend_lineno);
102
33.5k
  return (zend_ast *) ast;
103
33.5k
}
104
105
88.8k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_class_const_or_name(zend_ast *class_name, zend_ast *name) {
106
88.8k
  zend_string *name_str = zend_ast_get_str(name);
107
88.8k
  if (zend_string_equals_literal_ci(name_str, "class")) {
108
20.9k
    zend_string_release(name_str);
109
20.9k
    return zend_ast_create(ZEND_AST_CLASS_NAME, class_name);
110
67.9k
  } else {
111
67.9k
    return zend_ast_create(ZEND_AST_CLASS_CONST, class_name, name);
112
67.9k
  }
113
88.8k
}
114
115
ZEND_API zend_ast *zend_ast_create_decl(
116
  zend_ast_kind kind, uint32_t flags, uint32_t start_lineno, zend_string *doc_comment,
117
  zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3, zend_ast *child4
118
522k
) {
119
522k
  zend_ast_decl *ast;
120
121
522k
  ast = zend_ast_alloc(sizeof(zend_ast_decl));
122
522k
  ast->kind = kind;
123
522k
  ast->attr = 0;
124
522k
  ast->start_lineno = start_lineno;
125
522k
  ast->end_lineno = CG(zend_lineno);
126
522k
  ast->flags = flags;
127
522k
  ast->lex_pos = LANG_SCNG(yy_text);
128
522k
  ast->doc_comment = doc_comment;
129
522k
  ast->name = name;
130
522k
  ast->child[0] = child0;
131
522k
  ast->child[1] = child1;
132
522k
  ast->child[2] = child2;
133
522k
  ast->child[3] = child3;
134
522k
  ast->child[4] = child4;
135
136
522k
  return (zend_ast *) ast;
137
522k
}
138
139
#if ZEND_AST_SPEC
140
73.8k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_0(zend_ast_kind kind) {
141
73.8k
  zend_ast *ast;
142
143
73.8k
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 0);
144
73.8k
  ast = zend_ast_alloc(zend_ast_size(0));
145
73.8k
  ast->kind = kind;
146
73.8k
  ast->attr = 0;
147
73.8k
  ast->lineno = CG(zend_lineno);
148
149
73.8k
  return ast;
150
73.8k
}
151
152
7.16M
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_1(zend_ast_kind kind, zend_ast *child) {
153
7.16M
  zend_ast *ast;
154
7.16M
  uint32_t lineno;
155
156
7.16M
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 1);
157
7.16M
  ast = zend_ast_alloc(zend_ast_size(1));
158
7.16M
  ast->kind = kind;
159
7.16M
  ast->attr = 0;
160
7.16M
  ast->child[0] = child;
161
7.16M
  if (child) {
162
7.15M
    lineno = zend_ast_get_lineno(child);
163
11.6k
  } else {
164
11.6k
    lineno = CG(zend_lineno);
165
11.6k
  }
166
7.16M
  ast->lineno = lineno;
167
7.16M
  ast->lineno = lineno;
168
169
7.16M
  return ast;
170
7.16M
}
171
172
6.51M
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_2(zend_ast_kind kind, zend_ast *child1, zend_ast *child2) {
173
6.51M
  zend_ast *ast;
174
6.51M
  uint32_t lineno;
175
176
6.51M
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 2);
177
6.51M
  ast = zend_ast_alloc(zend_ast_size(2));
178
6.51M
  ast->kind = kind;
179
6.51M
  ast->attr = 0;
180
6.51M
  ast->child[0] = child1;
181
6.51M
  ast->child[1] = child2;
182
6.51M
  if (child1) {
183
6.48M
    lineno = zend_ast_get_lineno(child1);
184
25.9k
  } else if (child2) {
185
21.0k
    lineno = zend_ast_get_lineno(child2);
186
4.83k
  } else {
187
4.83k
    lineno = CG(zend_lineno);
188
4.83k
  }
189
6.51M
  ast->lineno = lineno;
190
191
6.51M
  return ast;
192
6.51M
}
193
194
1.68M
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_3(zend_ast_kind kind, zend_ast *child1, zend_ast *child2, zend_ast *child3) {
195
1.68M
  zend_ast *ast;
196
1.68M
  uint32_t lineno;
197
198
1.68M
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 3);
199
1.68M
  ast = zend_ast_alloc(zend_ast_size(3));
200
1.68M
  ast->kind = kind;
201
1.68M
  ast->attr = 0;
202
1.68M
  ast->child[0] = child1;
203
1.68M
  ast->child[1] = child2;
204
1.68M
  ast->child[2] = child3;
205
1.68M
  if (child1) {
206
1.62M
    lineno = zend_ast_get_lineno(child1);
207
62.8k
  } else if (child2) {
208
62.8k
    lineno = zend_ast_get_lineno(child2);
209
0
  } else if (child3) {
210
0
    lineno = zend_ast_get_lineno(child3);
211
0
  } else {
212
0
    lineno = CG(zend_lineno);
213
0
  }
214
1.68M
  ast->lineno = lineno;
215
216
1.68M
  return ast;
217
1.68M
}
218
219
64.9k
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) {
220
64.9k
  zend_ast *ast;
221
64.9k
  uint32_t lineno;
222
223
64.9k
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 4);
224
64.9k
  ast = zend_ast_alloc(zend_ast_size(4));
225
64.9k
  ast->kind = kind;
226
64.9k
  ast->attr = 0;
227
64.9k
  ast->child[0] = child1;
228
64.9k
  ast->child[1] = child2;
229
64.9k
  ast->child[2] = child3;
230
64.9k
  ast->child[3] = child4;
231
64.9k
  if (child1) {
232
64.8k
    lineno = zend_ast_get_lineno(child1);
233
106
  } else if (child2) {
234
33
    lineno = zend_ast_get_lineno(child2);
235
73
  } else if (child3) {
236
18
    lineno = zend_ast_get_lineno(child3);
237
55
  } else if (child4) {
238
54
    lineno = zend_ast_get_lineno(child4);
239
1
  } else {
240
1
    lineno = CG(zend_lineno);
241
1
  }
242
64.9k
  ast->lineno = lineno;
243
244
64.9k
  return ast;
245
64.9k
}
246
247
287k
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) {
248
287k
  zend_ast *ast;
249
287k
  uint32_t lineno;
250
251
287k
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 5);
252
287k
  ast = zend_ast_alloc(zend_ast_size(5));
253
287k
  ast->kind = kind;
254
287k
  ast->attr = 0;
255
287k
  ast->child[0] = child1;
256
287k
  ast->child[1] = child2;
257
287k
  ast->child[2] = child3;
258
287k
  ast->child[3] = child4;
259
287k
  ast->child[4] = child5;
260
287k
  if (child1) {
261
60.8k
    lineno = zend_ast_get_lineno(child1);
262
226k
  } else if (child2) {
263
226k
    lineno = zend_ast_get_lineno(child2);
264
0
  } else if (child3) {
265
0
    lineno = zend_ast_get_lineno(child3);
266
0
  } else if (child4) {
267
0
    lineno = zend_ast_get_lineno(child4);
268
0
  } else if (child5) {
269
0
    lineno = zend_ast_get_lineno(child5);
270
0
  } else {
271
0
    lineno = CG(zend_lineno);
272
0
  }
273
287k
  ast->lineno = lineno;
274
275
287k
  return ast;
276
287k
}
277
278
3.93M
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_0(zend_ast_kind kind) {
279
3.93M
  zend_ast *ast;
280
3.93M
  zend_ast_list *list;
281
282
3.93M
  ast = zend_ast_alloc(zend_ast_list_size(4));
283
3.93M
  list = (zend_ast_list *) ast;
284
3.93M
  list->kind = kind;
285
3.93M
  list->attr = 0;
286
3.93M
  list->lineno = CG(zend_lineno);
287
3.93M
  list->children = 0;
288
289
3.93M
  return ast;
290
3.93M
}
291
292
3.91M
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_1(zend_ast_kind kind, zend_ast *child) {
293
3.91M
  zend_ast *ast;
294
3.91M
  zend_ast_list *list;
295
3.91M
  uint32_t lineno;
296
297
3.91M
  ast = zend_ast_alloc(zend_ast_list_size(4));
298
3.91M
  list = (zend_ast_list *) ast;
299
3.91M
  list->kind = kind;
300
3.91M
  list->attr = 0;
301
3.91M
  list->children = 1;
302
3.91M
  list->child[0] = child;
303
3.91M
  if (child) {
304
3.86M
    lineno = zend_ast_get_lineno(child);
305
3.86M
    if (lineno > CG(zend_lineno)) {
306
965
      lineno = CG(zend_lineno);
307
965
    }
308
50.4k
  } else {
309
50.4k
    lineno = CG(zend_lineno);
310
50.4k
  }
311
3.91M
  list->lineno = lineno;
312
313
3.91M
  return ast;
314
3.91M
}
315
316
153k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_2(zend_ast_kind kind, zend_ast *child1, zend_ast *child2) {
317
153k
  zend_ast *ast;
318
153k
  zend_ast_list *list;
319
153k
  uint32_t lineno;
320
321
153k
  ast = zend_ast_alloc(zend_ast_list_size(4));
322
153k
  list = (zend_ast_list *) ast;
323
153k
  list->kind = kind;
324
153k
  list->attr = 0;
325
153k
  list->children = 2;
326
153k
  list->child[0] = child1;
327
153k
  list->child[1] = child2;
328
153k
  if (child1) {
329
153k
    lineno = zend_ast_get_lineno(child1);
330
153k
    if (lineno > CG(zend_lineno)) {
331
0
      lineno = CG(zend_lineno);
332
0
    }
333
0
  } else if (child2) {
334
0
    lineno = zend_ast_get_lineno(child2);
335
0
    if (lineno > CG(zend_lineno)) {
336
0
      lineno = CG(zend_lineno);
337
0
    }
338
0
  } else {
339
0
    list->children = 0;
340
0
    lineno = CG(zend_lineno);
341
0
  }
342
153k
  list->lineno = lineno;
343
344
153k
  return ast;
345
153k
}
346
#else
347
static zend_ast *zend_ast_create_from_va_list(zend_ast_kind kind, zend_ast_attr attr, va_list va) {
348
  uint32_t i, children = kind >> ZEND_AST_NUM_CHILDREN_SHIFT;
349
  zend_ast *ast;
350
351
  ast = zend_ast_alloc(zend_ast_size(children));
352
  ast->kind = kind;
353
  ast->attr = attr;
354
  ast->lineno = (uint32_t) -1;
355
356
  for (i = 0; i < children; ++i) {
357
    ast->child[i] = va_arg(va, zend_ast *);
358
    if (ast->child[i] != NULL) {
359
      uint32_t lineno = zend_ast_get_lineno(ast->child[i]);
360
      if (lineno < ast->lineno) {
361
        ast->lineno = lineno;
362
      }
363
    }
364
  }
365
366
  if (ast->lineno == UINT_MAX) {
367
    ast->lineno = CG(zend_lineno);
368
  }
369
370
  return ast;
371
}
372
373
ZEND_API zend_ast *zend_ast_create_ex(zend_ast_kind kind, zend_ast_attr attr, ...) {
374
  va_list va;
375
  zend_ast *ast;
376
377
  va_start(va, attr);
378
  ast = zend_ast_create_from_va_list(kind, attr, va);
379
  va_end(va);
380
381
  return ast;
382
}
383
384
ZEND_API zend_ast *zend_ast_create(zend_ast_kind kind, ...) {
385
  va_list va;
386
  zend_ast *ast;
387
388
  va_start(va, kind);
389
  ast = zend_ast_create_from_va_list(kind, 0, va);
390
  va_end(va);
391
392
  return ast;
393
}
394
395
ZEND_API zend_ast *zend_ast_create_list(uint32_t init_children, zend_ast_kind kind, ...) {
396
  zend_ast *ast;
397
  zend_ast_list *list;
398
399
  ast = zend_ast_alloc(zend_ast_list_size(4));
400
  list = (zend_ast_list *) ast;
401
  list->kind = kind;
402
  list->attr = 0;
403
  list->lineno = CG(zend_lineno);
404
  list->children = 0;
405
406
  {
407
    va_list va;
408
    uint32_t i;
409
    va_start(va, kind);
410
    for (i = 0; i < init_children; ++i) {
411
      zend_ast *child = va_arg(va, zend_ast *);
412
      ast = zend_ast_list_add(ast, child);
413
      if (child != NULL) {
414
        uint32_t lineno = zend_ast_get_lineno(child);
415
        if (lineno < ast->lineno) {
416
          ast->lineno = lineno;
417
        }
418
      }
419
    }
420
    va_end(va);
421
  }
422
423
  return ast;
424
}
425
#endif
426
427
3.08M
static inline zend_bool is_power_of_two(uint32_t n) {
428
3.08M
  return ((n != 0) && (n == (n & (~n + 1))));
429
3.08M
}
430
431
9.22M
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_list_add(zend_ast *ast, zend_ast *op) {
432
9.22M
  zend_ast_list *list = zend_ast_get_list(ast);
433
9.22M
  if (list->children >= 4 && is_power_of_two(list->children)) {
434
531k
      list = zend_ast_realloc(list,
435
531k
      zend_ast_list_size(list->children), zend_ast_list_size(list->children * 2));
436
531k
  }
437
9.22M
  list->child[list->children++] = op;
438
9.22M
  return (zend_ast *) list;
439
9.22M
}
440
441
static zend_result zend_ast_add_array_element(zval *result, zval *offset, zval *expr)
442
4.45k
{
443
4.45k
  switch (Z_TYPE_P(offset)) {
444
772
    case IS_UNDEF:
445
772
      if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), expr)) {
446
21
        zend_throw_error(NULL,
447
21
          "Cannot add element to the array as the next element is already occupied");
448
21
        return FAILURE;
449
21
      }
450
751
      break;
451
689
    case IS_STRING:
452
689
      zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(offset), expr);
453
689
      zval_ptr_dtor_str(offset);
454
689
      break;
455
162
    case IS_NULL:
456
162
      zend_symtable_update(Z_ARRVAL_P(result), ZSTR_EMPTY_ALLOC(), expr);
457
162
      break;
458
2.38k
    case IS_LONG:
459
2.38k
      zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(offset), expr);
460
2.38k
      break;
461
133
    case IS_FALSE:
462
133
      zend_hash_index_update(Z_ARRVAL_P(result), 0, expr);
463
133
      break;
464
115
    case IS_TRUE:
465
115
      zend_hash_index_update(Z_ARRVAL_P(result), 1, expr);
466
115
      break;
467
174
    case IS_DOUBLE:
468
174
      zend_hash_index_update(Z_ARRVAL_P(result), zend_dval_to_lval(Z_DVAL_P(offset)), expr);
469
174
      break;
470
0
    case IS_RESOURCE:
471
0
      zend_error(E_WARNING, "Resource ID#%d used as offset, casting to integer (%d)", Z_RES_HANDLE_P(offset), Z_RES_HANDLE_P(offset));
472
0
      zend_hash_index_update(Z_ARRVAL_P(result), Z_RES_HANDLE_P(offset), expr);
473
0
      break;
474
34
    default:
475
34
      zend_type_error("Illegal offset type");
476
34
      return FAILURE;
477
4.40k
  }
478
4.40k
  return SUCCESS;
479
4.40k
}
480
481
144
static zend_result zend_ast_add_unpacked_element(zval *result, zval *expr) {
482
144
  if (EXPECTED(Z_TYPE_P(expr) == IS_ARRAY)) {
483
144
    HashTable *ht = Z_ARRVAL_P(expr);
484
144
    zval *val;
485
144
    zend_string *key;
486
487
916
    ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
488
386
      if (key) {
489
0
        zend_throw_error(NULL, "Cannot unpack array with string keys");
490
0
        return FAILURE;
491
386
      } else {
492
386
        if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
493
46
          zend_throw_error(NULL,
494
46
            "Cannot add element to the array as the next element is already occupied");
495
46
          return FAILURE;
496
46
        }
497
340
        Z_TRY_ADDREF_P(val);
498
340
      }
499
386
    } ZEND_HASH_FOREACH_END();
500
98
    return SUCCESS;
501
0
  }
502
503
  /* Objects or references cannot occur in a constant expression. */
504
0
  zend_throw_error(NULL, "Only arrays and Traversables can be unpacked");
505
0
  return FAILURE;
506
0
}
507
508
ZEND_API zend_result ZEND_FASTCALL zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope)
509
46.8k
{
510
46.8k
  zval op1, op2;
511
46.8k
  zend_result ret = SUCCESS;
512
513
46.8k
  switch (ast->kind) {
514
2.80k
    case ZEND_AST_BINARY_OP:
515
2.80k
      if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
516
165
        ret = FAILURE;
517
2.63k
      } else if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) {
518
53
        zval_ptr_dtor_nogc(&op1);
519
53
        ret = FAILURE;
520
2.58k
      } else {
521
2.58k
        binary_op_type op = get_binary_op(ast->attr);
522
2.58k
        ret = op(result, &op1, &op2);
523
2.58k
        zval_ptr_dtor_nogc(&op1);
524
2.58k
        zval_ptr_dtor_nogc(&op2);
525
2.58k
      }
526
2.80k
      break;
527
412
    case ZEND_AST_GREATER:
528
784
    case ZEND_AST_GREATER_EQUAL:
529
784
      if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
530
46
        ret = FAILURE;
531
738
      } else if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) {
532
30
        zval_ptr_dtor_nogc(&op1);
533
30
        ret = FAILURE;
534
708
      } else {
535
        /* op1 > op2 is the same as op2 < op1 */
536
708
        binary_op_type op = ast->kind == ZEND_AST_GREATER
537
368
          ? is_smaller_function : is_smaller_or_equal_function;
538
708
        ret = op(result, &op2, &op1);
539
708
        zval_ptr_dtor_nogc(&op1);
540
708
        zval_ptr_dtor_nogc(&op2);
541
708
      }
542
784
      break;
543
341
    case ZEND_AST_UNARY_OP:
544
341
      if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
545
41
        ret = FAILURE;
546
300
      } else {
547
300
        unary_op_type op = get_unary_op(ast->attr);
548
300
        ret = op(result, &op1);
549
300
        zval_ptr_dtor_nogc(&op1);
550
300
      }
551
341
      break;
552
14.1k
    case ZEND_AST_ZVAL:
553
14.1k
    {
554
14.1k
      zval *zv = zend_ast_get_zval(ast);
555
556
14.1k
      ZVAL_COPY(result, zv);
557
14.1k
      break;
558
412
    }
559
14.9k
    case ZEND_AST_CONSTANT:
560
14.9k
    {
561
14.9k
      zend_string *name = zend_ast_get_constant_name(ast);
562
14.9k
      zval *zv = zend_get_constant_ex(name, scope, ast->attr);
563
564
14.9k
      if (UNEXPECTED(zv == NULL)) {
565
1.38k
        ZVAL_UNDEF(result);
566
1.38k
        return FAILURE;
567
1.38k
      }
568
13.5k
      ZVAL_COPY_OR_DUP(result, zv);
569
13.5k
      break;
570
13.5k
    }
571
195
    case ZEND_AST_CONSTANT_CLASS:
572
195
      if (scope) {
573
195
        ZVAL_STR_COPY(result, scope->name);
574
0
      } else {
575
0
        ZVAL_EMPTY_STRING(result);
576
0
      }
577
195
      break;
578
372
    case ZEND_AST_CLASS_NAME:
579
372
      if (!scope) {
580
25
        zend_throw_error(NULL, "Cannot use \"self\" when no class scope is active");
581
25
        return FAILURE;
582
25
      }
583
347
      if (ast->attr == ZEND_FETCH_CLASS_SELF) {
584
347
        ZVAL_STR_COPY(result, scope->name);
585
0
      } else if (ast->attr == ZEND_FETCH_CLASS_PARENT) {
586
0
        if (!scope->parent) {
587
0
          zend_throw_error(NULL,
588
0
            "Cannot use \"parent\" when current class scope has no parent");
589
0
          return FAILURE;
590
0
        }
591
0
        ZVAL_STR_COPY(result, scope->parent->name);
592
0
      } else {
593
0
        ZEND_ASSERT(0 && "Should have errored during compilation");
594
0
      }
595
347
      break;
596
571
    case ZEND_AST_AND:
597
571
      if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
598
48
        ret = FAILURE;
599
48
        break;
600
48
      }
601
523
      if (zend_is_true(&op1)) {
602
287
        if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) {
603
9
          zval_ptr_dtor_nogc(&op1);
604
9
          ret = FAILURE;
605
9
          break;
606
9
        }
607
278
        ZVAL_BOOL(result, zend_is_true(&op2));
608
278
        zval_ptr_dtor_nogc(&op2);
609
236
      } else {
610
236
        ZVAL_FALSE(result);
611
236
      }
612
514
      zval_ptr_dtor_nogc(&op1);
613
514
      break;
614
548
    case ZEND_AST_OR:
615
548
      if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
616
30
        ret = FAILURE;
617
30
        break;
618
30
      }
619
518
      if (zend_is_true(&op1)) {
620
248
        ZVAL_TRUE(result);
621
270
      } else {
622
270
        if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) {
623
6
          zval_ptr_dtor_nogc(&op1);
624
6
          ret = FAILURE;
625
6
          break;
626
6
        }
627
264
        ZVAL_BOOL(result, zend_is_true(&op2));
628
264
        zval_ptr_dtor_nogc(&op2);
629
264
      }
630
512
      zval_ptr_dtor_nogc(&op1);
631
512
      break;
632
964
    case ZEND_AST_CONDITIONAL:
633
964
      if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
634
54
        ret = FAILURE;
635
54
        break;
636
54
      }
637
910
      if (zend_is_true(&op1)) {
638
487
        if (!ast->child[1]) {
639
238
          *result = op1;
640
249
        } else {
641
249
          if (UNEXPECTED(zend_ast_evaluate(result, ast->child[1], scope) != SUCCESS)) {
642
6
            zval_ptr_dtor_nogc(&op1);
643
6
            ret = FAILURE;
644
6
            break;
645
6
          }
646
243
          zval_ptr_dtor_nogc(&op1);
647
243
        }
648
423
      } else {
649
423
        if (UNEXPECTED(zend_ast_evaluate(result, ast->child[2], scope) != SUCCESS)) {
650
3
          zval_ptr_dtor_nogc(&op1);
651
3
          ret = FAILURE;
652
3
          break;
653
3
        }
654
420
        zval_ptr_dtor_nogc(&op1);
655
420
      }
656
901
      break;
657
1.57k
    case ZEND_AST_COALESCE:
658
1.57k
      if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
659
116
        ret = FAILURE;
660
116
        break;
661
116
      }
662
1.45k
      if (Z_TYPE(op1) > IS_NULL) {
663
21
        *result = op1;
664
1.43k
      } else {
665
1.43k
        if (UNEXPECTED(zend_ast_evaluate(result, ast->child[1], scope) != SUCCESS)) {
666
5
          zval_ptr_dtor_nogc(&op1);
667
5
          ret = FAILURE;
668
5
          break;
669
5
        }
670
1.43k
        zval_ptr_dtor_nogc(&op1);
671
1.43k
      }
672
1.45k
      break;
673
650
    case ZEND_AST_UNARY_PLUS:
674
650
      if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[0], scope) != SUCCESS)) {
675
26
        ret = FAILURE;
676
624
      } else {
677
624
        ZVAL_LONG(&op1, 0);
678
624
        ret = add_function(result, &op1, &op2);
679
624
        zval_ptr_dtor_nogc(&op2);
680
624
      }
681
650
      break;
682
374
    case ZEND_AST_UNARY_MINUS:
683
374
      if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[0], scope) != SUCCESS)) {
684
41
        ret = FAILURE;
685
333
      } else {
686
333
        ZVAL_LONG(&op1, 0);
687
333
        ret = sub_function(result, &op1, &op2);
688
333
        zval_ptr_dtor_nogc(&op2);
689
333
      }
690
374
      break;
691
3.49k
    case ZEND_AST_ARRAY:
692
3.49k
      {
693
3.49k
        uint32_t i;
694
3.49k
        zend_ast_list *list = zend_ast_get_list(ast);
695
696
3.49k
        if (!list->children) {
697
0
          ZVAL_EMPTY_ARRAY(result);
698
0
          break;
699
0
        }
700
3.49k
        array_init(result);
701
8.00k
        for (i = 0; i < list->children; i++) {
702
5.39k
          zend_ast *elem = list->child[i];
703
5.39k
          if (elem->kind == ZEND_AST_UNPACK) {
704
281
            if (UNEXPECTED(zend_ast_evaluate(&op1, elem->child[0], scope) != SUCCESS)) {
705
137
              zval_ptr_dtor_nogc(result);
706
137
              return FAILURE;
707
137
            }
708
144
            if (UNEXPECTED(zend_ast_add_unpacked_element(result, &op1) != SUCCESS)) {
709
46
              zval_ptr_dtor_nogc(&op1);
710
46
              zval_ptr_dtor_nogc(result);
711
46
              return FAILURE;
712
46
            }
713
98
            zval_ptr_dtor_nogc(&op1);
714
98
            continue;
715
98
          }
716
5.11k
          if (elem->child[1]) {
717
4.09k
            if (UNEXPECTED(zend_ast_evaluate(&op1, elem->child[1], scope) != SUCCESS)) {
718
274
              zval_ptr_dtor_nogc(result);
719
274
              return FAILURE;
720
274
            }
721
1.01k
          } else {
722
1.01k
            ZVAL_UNDEF(&op1);
723
1.01k
          }
724
4.83k
          if (UNEXPECTED(zend_ast_evaluate(&op2, elem->child[0], scope) != SUCCESS)) {
725
377
            zval_ptr_dtor_nogc(&op1);
726
377
            zval_ptr_dtor_nogc(result);
727
377
            return FAILURE;
728
377
          }
729
4.45k
          if (UNEXPECTED(zend_ast_add_array_element(result, &op1, &op2) != SUCCESS)) {
730
55
            zval_ptr_dtor_nogc(&op1);
731
55
            zval_ptr_dtor_nogc(&op2);
732
55
            zval_ptr_dtor_nogc(result);
733
55
            return FAILURE;
734
55
          }
735
4.45k
        }
736
3.49k
      }
737
2.61k
      break;
738
5.07k
    case ZEND_AST_DIM:
739
5.07k
      if (ast->child[1] == NULL) {
740
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
741
0
      }
742
743
5.07k
      if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
744
195
        ret = FAILURE;
745
4.87k
      } else if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) {
746
42
        zval_ptr_dtor_nogc(&op1);
747
42
        ret = FAILURE;
748
4.83k
      } else {
749
4.83k
        zend_fetch_dimension_const(result, &op1, &op2, (ast->attr & ZEND_DIM_IS) ? BP_VAR_IS : BP_VAR_R);
750
751
4.83k
        zval_ptr_dtor_nogc(&op1);
752
4.83k
        zval_ptr_dtor_nogc(&op2);
753
4.83k
        if (UNEXPECTED(EG(exception))) {
754
27
          return FAILURE;
755
27
        }
756
5.04k
      }
757
5.04k
      break;
758
0
    default:
759
0
      zend_throw_error(NULL, "Unsupported constant expression");
760
0
      ret = FAILURE;
761
46.8k
  }
762
44.4k
  return ret;
763
46.8k
}
764
765
static size_t ZEND_FASTCALL zend_ast_tree_size(zend_ast *ast)
766
104k
{
767
104k
  size_t size;
768
769
104k
  if (ast->kind == ZEND_AST_ZVAL || ast->kind == ZEND_AST_CONSTANT) {
770
63.4k
    size = sizeof(zend_ast_zval);
771
41.1k
  } else if (zend_ast_is_list(ast)) {
772
6.88k
    uint32_t i;
773
6.88k
    zend_ast_list *list = zend_ast_get_list(ast);
774
775
6.88k
    size = zend_ast_list_size(list->children);
776
17.5k
    for (i = 0; i < list->children; i++) {
777
10.6k
      if (list->child[i]) {
778
10.6k
        size += zend_ast_tree_size(list->child[i]);
779
10.6k
      }
780
10.6k
    }
781
34.2k
  } else {
782
34.2k
    uint32_t i, children = zend_ast_get_num_children(ast);
783
784
34.2k
    size = zend_ast_size(children);
785
101k
    for (i = 0; i < children; i++) {
786
67.3k
      if (ast->child[i]) {
787
64.2k
        size += zend_ast_tree_size(ast->child[i]);
788
64.2k
      }
789
67.3k
    }
790
34.2k
  }
791
104k
  return size;
792
104k
}
793
794
static void* ZEND_FASTCALL zend_ast_tree_copy(zend_ast *ast, void *buf)
795
104k
{
796
104k
  if (ast->kind == ZEND_AST_ZVAL) {
797
29.9k
    zend_ast_zval *new = (zend_ast_zval*)buf;
798
29.9k
    new->kind = ZEND_AST_ZVAL;
799
29.9k
    new->attr = ast->attr;
800
29.9k
    ZVAL_COPY(&new->val, zend_ast_get_zval(ast));
801
29.9k
    buf = (void*)((char*)buf + sizeof(zend_ast_zval));
802
74.6k
  } else if (ast->kind == ZEND_AST_CONSTANT) {
803
33.5k
    zend_ast_zval *new = (zend_ast_zval*)buf;
804
33.5k
    new->kind = ZEND_AST_CONSTANT;
805
33.5k
    new->attr = ast->attr;
806
33.5k
    ZVAL_STR_COPY(&new->val, zend_ast_get_constant_name(ast));
807
33.5k
    buf = (void*)((char*)buf + sizeof(zend_ast_zval));
808
41.1k
  } else if (zend_ast_is_list(ast)) {
809
6.88k
    zend_ast_list *list = zend_ast_get_list(ast);
810
6.88k
    zend_ast_list *new = (zend_ast_list*)buf;
811
6.88k
    uint32_t i;
812
6.88k
    new->kind = list->kind;
813
6.88k
    new->attr = list->attr;
814
6.88k
    new->children = list->children;
815
6.88k
    buf = (void*)((char*)buf + zend_ast_list_size(list->children));
816
17.5k
    for (i = 0; i < list->children; i++) {
817
10.6k
      if (list->child[i]) {
818
10.6k
        new->child[i] = (zend_ast*)buf;
819
10.6k
        buf = zend_ast_tree_copy(list->child[i], buf);
820
0
      } else {
821
0
        new->child[i] = NULL;
822
0
      }
823
10.6k
    }
824
34.2k
  } else {
825
34.2k
    uint32_t i, children = zend_ast_get_num_children(ast);
826
34.2k
    zend_ast *new = (zend_ast*)buf;
827
34.2k
    new->kind = ast->kind;
828
34.2k
    new->attr = ast->attr;
829
34.2k
    buf = (void*)((char*)buf + zend_ast_size(children));
830
101k
    for (i = 0; i < children; i++) {
831
67.3k
      if (ast->child[i]) {
832
64.2k
        new->child[i] = (zend_ast*)buf;
833
64.2k
        buf = zend_ast_tree_copy(ast->child[i], buf);
834
3.06k
      } else {
835
3.06k
        new->child[i] = NULL;
836
3.06k
      }
837
67.3k
    }
838
34.2k
  }
839
104k
  return buf;
840
104k
}
841
842
ZEND_API zend_ast_ref * ZEND_FASTCALL zend_ast_copy(zend_ast *ast)
843
29.7k
{
844
29.7k
  size_t tree_size;
845
29.7k
  zend_ast_ref *ref;
846
847
29.7k
  ZEND_ASSERT(ast != NULL);
848
29.7k
  tree_size = zend_ast_tree_size(ast) + sizeof(zend_ast_ref);
849
29.7k
  ref = emalloc(tree_size);
850
29.7k
  zend_ast_tree_copy(ast, GC_AST(ref));
851
29.7k
  GC_SET_REFCOUNT(ref, 1);
852
29.7k
  GC_TYPE_INFO(ref) = GC_CONSTANT_AST;
853
29.7k
  return ref;
854
29.7k
}
855
856
ZEND_API void ZEND_FASTCALL zend_ast_destroy(zend_ast *ast)
857
21.3M
{
858
43.7M
tail_call:
859
43.7M
  if (!ast) {
860
4.93M
    return;
861
4.93M
  }
862
863
38.8M
  if (EXPECTED(ast->kind >= ZEND_AST_VAR)) {
864
15.4M
    uint32_t i, children = zend_ast_get_num_children(ast);
865
866
26.5M
    for (i = 1; i < children; i++) {
867
11.0M
      zend_ast_destroy(ast->child[i]);
868
11.0M
    }
869
15.4M
    ast = ast->child[0];
870
15.4M
    goto tail_call;
871
23.3M
  } else if (EXPECTED(ast->kind == ZEND_AST_ZVAL)) {
872
14.6M
    zval_ptr_dtor_nogc(zend_ast_get_zval(ast));
873
8.63M
  } else if (EXPECTED(zend_ast_is_list(ast))) {
874
7.87M
    zend_ast_list *list = zend_ast_get_list(ast);
875
7.87M
    if (list->children) {
876
6.43M
      uint32_t i;
877
878
13.1M
      for (i = 1; i < list->children; i++) {
879
6.73M
        zend_ast_destroy(list->child[i]);
880
6.73M
      }
881
6.43M
      ast = list->child[0];
882
6.43M
      goto tail_call;
883
6.43M
    }
884
768k
  } else if (EXPECTED(ast->kind == ZEND_AST_CONSTANT)) {
885
66.1k
    zend_string_release_ex(zend_ast_get_constant_name(ast), 0);
886
702k
  } else if (EXPECTED(ast->kind >= ZEND_AST_FUNC_DECL)) {
887
494k
    zend_ast_decl *decl = (zend_ast_decl *) ast;
888
889
494k
    if (decl->name) {
890
469k
        zend_string_release_ex(decl->name, 0);
891
469k
    }
892
494k
    if (decl->doc_comment) {
893
879
      zend_string_release_ex(decl->doc_comment, 0);
894
879
    }
895
494k
    zend_ast_destroy(decl->child[0]);
896
494k
    zend_ast_destroy(decl->child[1]);
897
494k
    zend_ast_destroy(decl->child[2]);
898
494k
    zend_ast_destroy(decl->child[3]);
899
494k
    ast = decl->child[4];
900
494k
    goto tail_call;
901
494k
  }
902
38.8M
}
903
904
ZEND_API void ZEND_FASTCALL zend_ast_ref_destroy(zend_ast_ref *ast)
905
28.8k
{
906
28.8k
  zend_ast_destroy(GC_AST(ast));
907
28.8k
  efree(ast);
908
28.8k
}
909
910
40.4k
ZEND_API void zend_ast_apply(zend_ast *ast, zend_ast_apply_func fn) {
911
40.4k
  if (zend_ast_is_list(ast)) {
912
6.74k
    zend_ast_list *list = zend_ast_get_list(ast);
913
6.74k
    uint32_t i;
914
17.2k
    for (i = 0; i < list->children; ++i) {
915
10.5k
      fn(&list->child[i]);
916
10.5k
    }
917
33.6k
  } else {
918
33.6k
    uint32_t i, children = zend_ast_get_num_children(ast);
919
100k
    for (i = 0; i < children; ++i) {
920
66.9k
      fn(&ast->child[i]);
921
66.9k
    }
922
33.6k
  }
923
40.4k
}
924
925
/*
926
 * Operator Precedence
927
 * ====================
928
 * priority  associativity  operators
929
 * ----------------------------------
930
 *   10     left            include, include_once, eval, require, require_once
931
 *   20     left            ,
932
 *   30     left            or
933
 *   40     left            xor
934
 *   50     left            and
935
 *   60     right           print
936
 *   70     right           yield
937
 *   80     right           =>
938
 *   85     right           yield from
939
 *   90     right           = += -= *= /= .= %= &= |= ^= <<= >>= **=
940
 *  100     left            ? :
941
 *  110     right           ??
942
 *  120     left            ||
943
 *  130     left            &&
944
 *  140     left            |
945
 *  150     left            ^
946
 *  160     left            &
947
 *  170     non-associative == != === !==
948
 *  180     non-associative < <= > >= <=>
949
 *  185     left            .
950
 *  190     left            << >>
951
 *  200     left            + -
952
 *  210     left            * / %
953
 *  220     right           !
954
 *  230     non-associative instanceof
955
 *  240     right           + - ++ -- ~ (type) @
956
 *  250     right           **
957
 *  260     left            [
958
 *  270     non-associative clone new
959
 */
960
961
static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int priority, int indent);
962
963
static ZEND_COLD void zend_ast_export_str(smart_str *str, zend_string *s)
964
14.2k
{
965
14.2k
  size_t i;
966
967
679k
  for (i = 0; i < ZSTR_LEN(s); i++) {
968
665k
    unsigned char c = ZSTR_VAL(s)[i];
969
665k
    if (c == '\'' || c == '\\') {
970
48.6k
      smart_str_appendc(str, '\\');
971
48.6k
      smart_str_appendc(str, c);
972
616k
    } else {
973
616k
      smart_str_appendc(str, c);
974
616k
    }
975
665k
  }
976
14.2k
}
977
978
static ZEND_COLD void zend_ast_export_qstr(smart_str *str, char quote, zend_string *s)
979
14.8k
{
980
14.8k
  size_t i;
981
982
672k
  for (i = 0; i < ZSTR_LEN(s); i++) {
983
657k
    unsigned char c = ZSTR_VAL(s)[i];
984
657k
    if (c < ' ') {
985
164k
      switch (c) {
986
11.7k
        case '\n':
987
11.7k
          smart_str_appends(str, "\\n");
988
11.7k
          break;
989
11.0k
        case '\r':
990
11.0k
          smart_str_appends(str, "\\r");
991
11.0k
          break;
992
30.3k
        case '\t':
993
30.3k
          smart_str_appends(str, "\\t");
994
30.3k
          break;
995
12.4k
        case '\f':
996
12.4k
          smart_str_appends(str, "\\f");
997
12.4k
          break;
998
21.8k
        case '\v':
999
21.8k
          smart_str_appends(str, "\\v");
1000
21.8k
          break;
1001
#ifdef ZEND_WIN32
1002
        case VK_ESCAPE:
1003
#else
1004
709
        case '\e':
1005
709
#endif
1006
709
          smart_str_appends(str, "\\e");
1007
709
          break;
1008
76.7k
        default:
1009
76.7k
          smart_str_appends(str, "\\0");
1010
76.7k
          smart_str_appendc(str, '0' + (c / 8));
1011
76.7k
          smart_str_appendc(str, '0' + (c % 8));
1012
76.7k
          break;
1013
492k
      }
1014
492k
    } else {
1015
492k
      if (c == quote || c == '$' || c == '\\') {
1016
90.2k
        smart_str_appendc(str, '\\');
1017
90.2k
      }
1018
492k
      smart_str_appendc(str, c);
1019
492k
    }
1020
657k
  }
1021
14.8k
}
1022
1023
static ZEND_COLD void zend_ast_export_indent(smart_str *str, int indent)
1024
63.8k
{
1025
211k
  while (indent > 0) {
1026
147k
    smart_str_appends(str, "    ");
1027
147k
    indent--;
1028
147k
  }
1029
63.8k
}
1030
1031
static ZEND_COLD void zend_ast_export_name(smart_str *str, zend_ast *ast, int priority, int indent)
1032
28.5k
{
1033
28.5k
  if (ast->kind == ZEND_AST_ZVAL) {
1034
22.4k
    zval *zv = zend_ast_get_zval(ast);
1035
1036
22.4k
    if (Z_TYPE_P(zv) == IS_STRING) {
1037
22.4k
      smart_str_append(str, Z_STR_P(zv));
1038
22.4k
      return;
1039
22.4k
    }
1040
6.08k
  }
1041
6.08k
  zend_ast_export_ex(str, ast, priority, indent);
1042
6.08k
}
1043
1044
static ZEND_COLD void zend_ast_export_ns_name(smart_str *str, zend_ast *ast, int priority, int indent)
1045
32.8k
{
1046
32.8k
  if (ast->kind == ZEND_AST_ZVAL) {
1047
28.6k
    zval *zv = zend_ast_get_zval(ast);
1048
1049
28.6k
    if (Z_TYPE_P(zv) == IS_STRING) {
1050
28.6k
        if (ast->attr == ZEND_NAME_FQ) {
1051
5.70k
        smart_str_appendc(str, '\\');
1052
22.9k
        } else if (ast->attr == ZEND_NAME_RELATIVE) {
1053
1.28k
        smart_str_appends(str, "namespace\\");
1054
1.28k
        }
1055
28.6k
      smart_str_append(str, Z_STR_P(zv));
1056
28.6k
      return;
1057
28.6k
    }
1058
4.19k
  }
1059
4.19k
  zend_ast_export_ex(str, ast, priority, indent);
1060
4.19k
}
1061
1062
static ZEND_COLD bool zend_ast_valid_var_char(char ch)
1063
5.63k
{
1064
5.63k
  unsigned char c = (unsigned char)ch;
1065
1066
5.63k
  if (c != '_' && c < 127 &&
1067
5.40k
      (c < '0' || c > '9') &&
1068
5.40k
      (c < 'A' || c > 'Z') &&
1069
5.39k
      (c < 'a' || c > 'z')) {
1070
4.48k
    return 0;
1071
4.48k
  }
1072
1.14k
  return 1;
1073
1.14k
}
1074
1075
static ZEND_COLD bool zend_ast_valid_var_name(const char *s, size_t len)
1076
70.6k
{
1077
70.6k
  unsigned char c;
1078
70.6k
  size_t i;
1079
1080
70.6k
  if (len == 0) {
1081
0
    return 0;
1082
0
  }
1083
70.6k
  c = (unsigned char)s[0];
1084
70.6k
  if (c != '_' && c < 127 &&
1085
69.2k
      (c < 'A' || c > 'Z') &&
1086
69.0k
      (c < 'a' || c > 'z')) {
1087
457
    return 0;
1088
457
  }
1089
174k
  for (i = 1; i < len; i++) {
1090
104k
    c = (unsigned char)s[i];
1091
104k
    if (c != '_' && c < 127 &&
1092
97.8k
        (c < '0' || c > '9') &&
1093
97.5k
        (c < 'A' || c > 'Z') &&
1094
92.1k
        (c < 'a' || c > 'z')) {
1095
1
      return 0;
1096
1
    }
1097
104k
  }
1098
70.2k
  return 1;
1099
70.2k
}
1100
1101
static ZEND_COLD bool zend_ast_var_needs_braces(char ch)
1102
6.56k
{
1103
6.56k
  return ch == '[' || zend_ast_valid_var_char(ch);
1104
6.56k
}
1105
1106
static ZEND_COLD void zend_ast_export_var(smart_str *str, zend_ast *ast, int priority, int indent)
1107
76.7k
{
1108
76.7k
  if (ast->kind == ZEND_AST_ZVAL) {
1109
70.6k
    zval *zv = zend_ast_get_zval(ast);
1110
70.6k
    if (Z_TYPE_P(zv) == IS_STRING &&
1111
70.6k
        zend_ast_valid_var_name(Z_STRVAL_P(zv), Z_STRLEN_P(zv))) {
1112
70.2k
      smart_str_append(str, Z_STR_P(zv));
1113
70.2k
      return;
1114
70.2k
    }
1115
6.11k
  } else if (ast->kind == ZEND_AST_VAR) {
1116
1.36k
    zend_ast_export_ex(str, ast, 0, indent);
1117
1.36k
    return;
1118
1.36k
  }
1119
5.20k
  smart_str_appendc(str, '{');
1120
5.20k
  zend_ast_export_name(str, ast, 0, indent);
1121
5.20k
  smart_str_appendc(str, '}');
1122
5.20k
}
1123
1124
static ZEND_COLD void zend_ast_export_list(smart_str *str, zend_ast_list *list, bool separator, int priority, int indent)
1125
29.3k
{
1126
29.3k
  uint32_t i = 0;
1127
1128
62.2k
  while (i < list->children) {
1129
32.9k
    if (i != 0 && separator) {
1130
11.5k
      smart_str_appends(str, ", ");
1131
11.5k
    }
1132
32.9k
    zend_ast_export_ex(str, list->child[i], priority, indent);
1133
32.9k
    i++;
1134
32.9k
  }
1135
29.3k
}
1136
1137
static ZEND_COLD void zend_ast_export_encaps_list(smart_str *str, char quote, zend_ast_list *list, int indent)
1138
6.40k
{
1139
6.40k
  uint32_t i = 0;
1140
6.40k
  zend_ast *ast;
1141
1142
32.3k
  while (i < list->children) {
1143
25.9k
    ast = list->child[i];
1144
25.9k
    if (ast->kind == ZEND_AST_ZVAL) {
1145
14.6k
      zval *zv = zend_ast_get_zval(ast);
1146
1147
14.6k
      ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
1148
14.6k
      zend_ast_export_qstr(str, quote, Z_STR_P(zv));
1149
11.2k
    } else if (ast->kind == ZEND_AST_VAR &&
1150
8.08k
               ast->child[0]->kind == ZEND_AST_ZVAL &&
1151
7.58k
               (i + 1 == list->children ||
1152
6.60k
                list->child[i + 1]->kind != ZEND_AST_ZVAL ||
1153
6.56k
                !zend_ast_var_needs_braces(
1154
6.56k
                    *Z_STRVAL_P(
1155
5.51k
                        zend_ast_get_zval(list->child[i + 1]))))) {
1156
5.51k
      zend_ast_export_ex(str, ast, 0, indent);
1157
5.78k
    } else {
1158
5.78k
      smart_str_appendc(str, '{');
1159
5.78k
      zend_ast_export_ex(str, ast, 0, indent);
1160
5.78k
      smart_str_appendc(str, '}');
1161
5.78k
    }
1162
25.9k
    i++;
1163
25.9k
  }
1164
6.40k
}
1165
1166
static ZEND_COLD void zend_ast_export_name_list_ex(smart_str *str, zend_ast_list *list, int indent, const char *separator)
1167
2.73k
{
1168
2.73k
  uint32_t i = 0;
1169
1170
6.38k
  while (i < list->children) {
1171
3.65k
    if (i != 0) {
1172
916
      smart_str_appends(str, separator);
1173
916
    }
1174
3.65k
    zend_ast_export_name(str, list->child[i], 0, indent);
1175
3.65k
    i++;
1176
3.65k
  }
1177
2.73k
}
1178
1179
1.78k
#define zend_ast_export_name_list(s, l, i) zend_ast_export_name_list_ex(s, l, i, ", ")
1180
954
#define zend_ast_export_catch_name_list(s, l, i) zend_ast_export_name_list_ex(s, l, i, "|")
1181
1182
static ZEND_COLD void zend_ast_export_var_list(smart_str *str, zend_ast_list *list, int indent)
1183
1.33k
{
1184
1.33k
  uint32_t i = 0;
1185
1186
3.99k
  while (i < list->children) {
1187
2.66k
    if (i != 0) {
1188
1.33k
      smart_str_appends(str, ", ");
1189
1.33k
    }
1190
2.66k
    if (list->child[i]->attr & ZEND_BIND_REF) {
1191
1.33k
      smart_str_appendc(str, '&');
1192
1.33k
    }
1193
2.66k
    smart_str_appendc(str, '$');
1194
2.66k
    zend_ast_export_name(str, list->child[i], 20, indent);
1195
2.66k
    i++;
1196
2.66k
  }
1197
1.33k
}
1198
1199
static ZEND_COLD void zend_ast_export_stmt(smart_str *str, zend_ast *ast, int indent)
1200
65.4k
{
1201
65.4k
  if (!ast) {
1202
1.04k
    return;
1203
1.04k
  }
1204
1205
64.3k
  if (ast->kind == ZEND_AST_STMT_LIST ||
1206
44.7k
      ast->kind == ZEND_AST_TRAIT_ADAPTATIONS) {
1207
20.1k
    zend_ast_list *list = (zend_ast_list*)ast;
1208
20.1k
    uint32_t i = 0;
1209
1210
67.3k
    while (i < list->children) {
1211
47.2k
      ast = list->child[i];
1212
47.2k
      zend_ast_export_stmt(str, ast, indent);
1213
47.2k
      i++;
1214
47.2k
    }
1215
44.2k
  } else {
1216
44.2k
    zend_ast_export_indent(str, indent);
1217
44.2k
    zend_ast_export_ex(str, ast, 0, indent);
1218
44.2k
    switch (ast->kind) {
1219
489
      case ZEND_AST_LABEL:
1220
2.61k
      case ZEND_AST_IF:
1221
3.04k
      case ZEND_AST_SWITCH:
1222
3.50k
      case ZEND_AST_WHILE:
1223
3.98k
      case ZEND_AST_TRY:
1224
4.43k
      case ZEND_AST_FOR:
1225
4.89k
      case ZEND_AST_FOREACH:
1226
4.89k
      case ZEND_AST_FUNC_DECL:
1227
6.34k
      case ZEND_AST_METHOD:
1228
8.05k
      case ZEND_AST_CLASS:
1229
8.95k
      case ZEND_AST_USE_TRAIT:
1230
8.95k
      case ZEND_AST_NAMESPACE:
1231
9.90k
      case ZEND_AST_DECLARE:
1232
9.90k
        break;
1233
34.3k
      default:
1234
34.3k
        smart_str_appendc(str, ';');
1235
34.3k
        break;
1236
44.2k
    }
1237
44.2k
    smart_str_appendc(str, '\n');
1238
44.2k
  }
1239
64.3k
}
1240
1241
static ZEND_COLD void zend_ast_export_if_stmt(smart_str *str, zend_ast_list *list, int indent)
1242
2.12k
{
1243
2.12k
  uint32_t i;
1244
2.12k
  zend_ast *ast;
1245
1246
2.92k
tail_call:
1247
2.92k
  i = 0;
1248
7.17k
  while (i < list->children) {
1249
5.04k
    ast = list->child[i];
1250
5.04k
    ZEND_ASSERT(ast->kind == ZEND_AST_IF_ELEM);
1251
5.04k
    if (ast->child[0]) {
1252
3.38k
      if (i == 0) {
1253
2.92k
        smart_str_appends(str, "if (");
1254
453
      } else {
1255
453
        zend_ast_export_indent(str, indent);
1256
453
        smart_str_appends(str, "} elseif (");
1257
453
      }
1258
3.38k
      zend_ast_export_ex(str, ast->child[0], 0, indent);
1259
3.38k
      smart_str_appends(str, ") {\n");
1260
3.38k
      zend_ast_export_stmt(str, ast->child[1], indent + 1);
1261
1.66k
    } else {
1262
1.66k
      zend_ast_export_indent(str, indent);
1263
1.66k
      smart_str_appends(str, "} else ");
1264
1.66k
      if (ast->child[1] && ast->child[1]->kind == ZEND_AST_IF) {
1265
798
        list = (zend_ast_list*)ast->child[1];
1266
798
        goto tail_call;
1267
864
      } else {
1268
864
        smart_str_appends(str, "{\n");
1269
864
        zend_ast_export_stmt(str, ast->child[1], indent + 1);
1270
864
      }
1271
1.66k
    }
1272
4.24k
    i++;
1273
4.24k
  }
1274
2.12k
  zend_ast_export_indent(str, indent);
1275
2.12k
  smart_str_appendc(str, '}');
1276
2.12k
}
1277
1278
static ZEND_COLD void zend_ast_export_zval(smart_str *str, zval *zv, int priority, int indent)
1279
41.6k
{
1280
41.6k
  zend_long idx;
1281
41.6k
  zend_string *key;
1282
41.6k
  zval *val;
1283
41.6k
  int first;
1284
1285
41.6k
  ZVAL_DEREF(zv);
1286
41.6k
  switch (Z_TYPE_P(zv)) {
1287
0
    case IS_NULL:
1288
0
      smart_str_appends(str, "null");
1289
0
      break;
1290
0
    case IS_FALSE:
1291
0
      smart_str_appends(str, "false");
1292
0
      break;
1293
0
    case IS_TRUE:
1294
0
      smart_str_appends(str, "true");
1295
0
      break;
1296
26.6k
    case IS_LONG:
1297
26.6k
      smart_str_append_long(str, Z_LVAL_P(zv));
1298
26.6k
      break;
1299
697
    case IS_DOUBLE:
1300
697
      key = zend_strpprintf(0, "%.*G", (int) EG(precision), Z_DVAL_P(zv));
1301
697
      smart_str_appendl(str, ZSTR_VAL(key), ZSTR_LEN(key));
1302
697
      zend_string_release_ex(key, 0);
1303
697
      break;
1304
14.2k
    case IS_STRING:
1305
14.2k
      smart_str_appendc(str, '\'');
1306
14.2k
      zend_ast_export_str(str, Z_STR_P(zv));
1307
14.2k
      smart_str_appendc(str, '\'');
1308
14.2k
      break;
1309
0
    case IS_ARRAY:
1310
0
      smart_str_appendc(str, '[');
1311
0
      first = 1;
1312
0
      ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(zv), idx, key, val) {
1313
0
        if (first) {
1314
0
          first = 0;
1315
0
        } else {
1316
0
          smart_str_appends(str, ", ");
1317
0
        }
1318
0
        if (key) {
1319
0
          smart_str_appendc(str, '\'');
1320
0
          zend_ast_export_str(str, key);
1321
0
          smart_str_appends(str, "' => ");
1322
0
        } else {
1323
0
          smart_str_append_long(str, idx);
1324
0
          smart_str_appends(str, " => ");
1325
0
        }
1326
0
        zend_ast_export_zval(str, val, 0, indent);
1327
0
      } ZEND_HASH_FOREACH_END();
1328
0
      smart_str_appendc(str, ']');
1329
0
      break;
1330
0
    case IS_CONSTANT_AST:
1331
0
      zend_ast_export_ex(str, Z_ASTVAL_P(zv), priority, indent);
1332
0
      break;
1333
0
    EMPTY_SWITCH_DEFAULT_CASE();
1334
41.6k
  }
1335
41.6k
}
1336
1337
1.88k
static ZEND_COLD void zend_ast_export_class_no_header(smart_str *str, zend_ast_decl *decl, int indent) {
1338
1.88k
  if (decl->child[0]) {
1339
485
    smart_str_appends(str, " extends ");
1340
485
    zend_ast_export_ns_name(str, decl->child[0], 0, indent);
1341
485
  }
1342
1.88k
  if (decl->child[1]) {
1343
465
    smart_str_appends(str, " implements ");
1344
465
    zend_ast_export_ex(str, decl->child[1], 0, indent);
1345
465
  }
1346
1.88k
  smart_str_appends(str, " {\n");
1347
1.88k
  zend_ast_export_stmt(str, decl->child[2], indent + 1);
1348
1.88k
  zend_ast_export_indent(str, indent);
1349
1.88k
  smart_str_appends(str, "}");
1350
1.88k
}
1351
1352
1.07k
static ZEND_COLD void zend_ast_export_attributes(smart_str *str, zend_ast *ast, int indent, zend_bool newlines) {
1353
1.07k
  zend_ast_list *list = zend_ast_get_list(ast);
1354
1.07k
  uint32_t i;
1355
1356
2.35k
  for (i = 0; i < list->children; i++) {
1357
1.28k
    zend_ast *attr = list->child[i];
1358
1359
1.28k
    smart_str_appends(str, "@@");
1360
1.28k
    zend_ast_export_ns_name(str, attr->child[0], 0, indent);
1361
1362
1.28k
    if (attr->child[1]) {
1363
199
      zend_ast_list *args = zend_ast_get_list(attr->child[1]);
1364
199
      uint32_t j;
1365
1366
199
      smart_str_appendc(str, '(');
1367
595
      for (j = 0; j < args->children; j++) {
1368
396
        if (j) {
1369
197
          smart_str_appends(str, ", ");
1370
197
        }
1371
396
        zend_ast_export_ex(str, args->child[j], 0, indent);
1372
396
      }
1373
199
      smart_str_appendc(str, ')');
1374
199
    }
1375
1376
1.28k
    if (newlines) {
1377
787
      smart_str_appendc(str, '\n');
1378
787
      zend_ast_export_indent(str, indent);
1379
494
    } else {
1380
494
      smart_str_appendc(str, ' ');
1381
494
    }
1382
1.28k
  }
1383
1.07k
}
1384
1385
7.91k
static ZEND_COLD void zend_ast_export_visibility(smart_str *str, uint32_t flags) {
1386
7.91k
  if (flags & ZEND_ACC_PUBLIC) {
1387
2.28k
    smart_str_appends(str, "public ");
1388
5.63k
  } else if (flags & ZEND_ACC_PROTECTED) {
1389
877
    smart_str_appends(str, "protected ");
1390
4.75k
  } else if (flags & ZEND_ACC_PRIVATE) {
1391
879
    smart_str_appends(str, "private ");
1392
879
  }
1393
7.91k
}
1394
1395
6.51k
static ZEND_COLD void zend_ast_export_type(smart_str *str, zend_ast *ast, int indent) {
1396
6.51k
  if (ast->kind == ZEND_AST_TYPE_UNION) {
1397
416
    zend_ast_list *list = zend_ast_get_list(ast);
1398
1.24k
    for (uint32_t i = 0; i < list->children; i++) {
1399
832
      if (i != 0) {
1400
416
        smart_str_appendc(str, '|');
1401
416
      }
1402
832
      zend_ast_export_type(str, list->child[i], indent);
1403
832
    }
1404
416
    return;
1405
416
  }
1406
6.10k
  if (ast->attr & ZEND_TYPE_NULLABLE) {
1407
1.96k
    smart_str_appendc(str, '?');
1408
1.96k
  }
1409
6.10k
  zend_ast_export_ns_name(str, ast, 0, indent);
1410
6.10k
}
1411
1412
40.8k
#define BINARY_OP(_op, _p, _pl, _pr) do { \
1413
40.8k
    op = _op; \
1414
40.8k
    p = _p; \
1415
40.8k
    pl = _pl; \
1416
40.8k
    pr = _pr; \
1417
40.8k
    goto binary_op; \
1418
0
  } while (0)
1419
1420
7.09k
#define PREFIX_OP(_op, _p, _pl) do { \
1421
7.09k
    op = _op; \
1422
7.09k
    p = _p; \
1423
7.09k
    pl = _pl; \
1424
7.09k
    goto prefix_op; \
1425
0
  } while (0)
1426
1427
1.24k
#define FUNC_OP(_op) do { \
1428
1.24k
    op = _op; \
1429
1.24k
    goto func_op; \
1430
0
  } while (0)
1431
1432
1.78k
#define POSTFIX_OP(_op, _p, _pl) do { \
1433
1.78k
    op = _op; \
1434
1.78k
    p = _p; \
1435
1.78k
    pl = _pl; \
1436
1.78k
    goto postfix_op; \
1437
0
  } while (0)
1438
1439
6.56k
#define APPEND_NODE_1(_op) do { \
1440
6.56k
    op = _op; \
1441
6.56k
    goto append_node_1; \
1442
0
  } while (0)
1443
1444
2.24k
#define APPEND_STR(_op) do { \
1445
2.24k
    op = _op; \
1446
2.24k
    goto append_str; \
1447
0
  } while (0)
1448
1449
9.50k
#define APPEND_DEFAULT_VALUE(n) do { \
1450
9.50k
    p = n; \
1451
9.50k
    goto append_default_value; \
1452
0
  } while (0)
1453
1454
static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int priority, int indent)
1455
272k
{
1456
272k
  zend_ast_decl *decl;
1457
272k
  int p, pl, pr;
1458
272k
  const char *op;
1459
1460
284k
tail_call:
1461
284k
  if (!ast) {
1462
3.95k
    return;
1463
3.95k
  }
1464
280k
  switch (ast->kind) {
1465
    /* special nodes */
1466
41.6k
    case ZEND_AST_ZVAL:
1467
41.6k
      zend_ast_export_zval(str, zend_ast_get_zval(ast), priority, indent);
1468
41.6k
      break;
1469
0
    case ZEND_AST_CONSTANT: {
1470
0
      zend_string *name = zend_ast_get_constant_name(ast);
1471
0
      smart_str_appendl(str, ZSTR_VAL(name), ZSTR_LEN(name));
1472
0
      break;
1473
0
    }
1474
0
    case ZEND_AST_CONSTANT_CLASS:
1475
0
      smart_str_appendl(str, "__CLASS__", sizeof("__CLASS__")-1);
1476
0
      break;
1477
0
    case ZEND_AST_ZNODE:
1478
      /* This AST kind is only used for temporary nodes during compilation */
1479
0
      ZEND_UNREACHABLE();
1480
0
      break;
1481
1482
    /* declaration nodes */
1483
0
    case ZEND_AST_FUNC_DECL:
1484
3.66k
    case ZEND_AST_CLOSURE:
1485
3.83k
    case ZEND_AST_ARROW_FUNC:
1486
5.28k
    case ZEND_AST_METHOD:
1487
5.28k
      decl = (zend_ast_decl *) ast;
1488
5.28k
      if (decl->child[4]) {
1489
306
        zend_bool newlines = !(ast->kind == ZEND_AST_CLOSURE || ast->kind == ZEND_AST_ARROW_FUNC);
1490
306
        zend_ast_export_attributes(str, decl->child[4], indent, newlines);
1491
306
      }
1492
1493
5.28k
      zend_ast_export_visibility(str, decl->flags);
1494
1495
5.28k
      if (decl->flags & ZEND_ACC_STATIC) {
1496
440
        smart_str_appends(str, "static ");
1497
440
      }
1498
5.28k
      if (decl->flags & ZEND_ACC_ABSTRACT) {
1499
462
        smart_str_appends(str, "abstract ");
1500
462
      }
1501
5.28k
      if (decl->flags & ZEND_ACC_FINAL) {
1502
418
        smart_str_appends(str, "final ");
1503
418
      }
1504
5.28k
      if (decl->kind == ZEND_AST_ARROW_FUNC) {
1505
170
        smart_str_appends(str, "fn");
1506
5.11k
      } else {
1507
5.11k
        smart_str_appends(str, "function ");
1508
5.11k
      }
1509
5.28k
      if (decl->flags & ZEND_ACC_RETURN_REFERENCE) {
1510
1.85k
        smart_str_appendc(str, '&');
1511
1.85k
      }
1512
5.28k
      if (ast->kind != ZEND_AST_CLOSURE && ast->kind != ZEND_AST_ARROW_FUNC) {
1513
1.45k
        smart_str_appendl(str, ZSTR_VAL(decl->name), ZSTR_LEN(decl->name));
1514
1.45k
      }
1515
5.28k
      smart_str_appendc(str, '(');
1516
5.28k
      zend_ast_export_ex(str, decl->child[0], 0, indent);
1517
5.28k
      smart_str_appendc(str, ')');
1518
5.28k
      zend_ast_export_ex(str, decl->child[1], 0, indent);
1519
5.28k
      if (decl->child[3]) {
1520
1.88k
        smart_str_appends(str, ": ");
1521
1.88k
        zend_ast_export_type(str, decl->child[3], indent);
1522
1.88k
      }
1523
5.28k
      if (decl->child[2]) {
1524
4.81k
        if (decl->kind == ZEND_AST_ARROW_FUNC) {
1525
170
          ZEND_ASSERT(decl->child[2]->kind == ZEND_AST_RETURN);
1526
170
          smart_str_appends(str, " => ");
1527
170
          zend_ast_export_ex(str, decl->child[2]->child[0], 0, indent);
1528
170
          break;
1529
170
        }
1530
1531
4.64k
        smart_str_appends(str, " {\n");
1532
4.64k
        zend_ast_export_stmt(str, decl->child[2], indent + 1);
1533
4.64k
        zend_ast_export_indent(str, indent);
1534
4.64k
        smart_str_appendc(str, '}');
1535
4.64k
        if (ast->kind != ZEND_AST_CLOSURE) {
1536
989
          smart_str_appendc(str, '\n');
1537
989
        }
1538
463
      } else {
1539
463
        smart_str_appends(str, ";\n");
1540
463
      }
1541
5.11k
      break;
1542
1.70k
    case ZEND_AST_CLASS:
1543
1.70k
      decl = (zend_ast_decl *) ast;
1544
1.70k
      if (decl->child[4]) {
1545
360
        zend_ast_export_attributes(str, decl->child[4], indent, 1);
1546
360
      }
1547
1.70k
      if (decl->flags & ZEND_ACC_INTERFACE) {
1548
121
        smart_str_appends(str, "interface ");
1549
1.58k
      } else if (decl->flags & ZEND_ACC_TRAIT) {
1550
127
        smart_str_appends(str, "trait ");
1551
1.46k
      } else {
1552
1.46k
        if (decl->flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
1553
465
          smart_str_appends(str, "abstract ");
1554
465
        }
1555
1.46k
        if (decl->flags & ZEND_ACC_FINAL) {
1556
416
          smart_str_appends(str, "final ");
1557
416
        }
1558
1.46k
        smart_str_appends(str, "class ");
1559
1.46k
      }
1560
1.70k
      smart_str_appendl(str, ZSTR_VAL(decl->name), ZSTR_LEN(decl->name));
1561
1.70k
      zend_ast_export_class_no_header(str, decl, indent);
1562
1.70k
      smart_str_appendc(str, '\n');
1563
1.70k
      break;
1564
1565
    /* list nodes */
1566
17.0k
    case ZEND_AST_ARG_LIST:
1567
18.3k
    case ZEND_AST_EXPR_LIST:
1568
23.6k
    case ZEND_AST_PARAM_LIST:
1569
26.2k
simple_list:
1570
26.2k
      zend_ast_export_list(str, (zend_ast_list*)ast, 1, 20, indent);
1571
26.2k
      break;
1572
1.08k
    case ZEND_AST_ARRAY:
1573
1.08k
      smart_str_appendc(str, '[');
1574
1.08k
      zend_ast_export_list(str, (zend_ast_list*)ast, 1, 20, indent);
1575
1.08k
      smart_str_appendc(str, ']');
1576
1.08k
      break;
1577
5.93k
    case ZEND_AST_ENCAPS_LIST:
1578
5.93k
      smart_str_appendc(str, '"');
1579
5.93k
      zend_ast_export_encaps_list(str, '"', (zend_ast_list*)ast, indent);
1580
5.93k
      smart_str_appendc(str, '"');
1581
5.93k
      break;
1582
0
    case ZEND_AST_STMT_LIST:
1583
451
    case ZEND_AST_TRAIT_ADAPTATIONS:
1584
451
      zend_ast_export_stmt(str, ast, indent);
1585
451
      break;
1586
2.12k
    case ZEND_AST_IF:
1587
2.12k
      zend_ast_export_if_stmt(str, (zend_ast_list*)ast, indent);
1588
2.12k
      break;
1589
428
    case ZEND_AST_SWITCH_LIST:
1590
907
    case ZEND_AST_CATCH_LIST:
1591
963
    case ZEND_AST_MATCH_ARM_LIST:
1592
963
      zend_ast_export_list(str, (zend_ast_list*)ast, 0, 0, indent);
1593
963
      break;
1594
1.33k
    case ZEND_AST_CLOSURE_USES:
1595
1.33k
      smart_str_appends(str, " use(");
1596
1.33k
      zend_ast_export_var_list(str, (zend_ast_list*)ast, indent);
1597
1.33k
      smart_str_appendc(str, ')');
1598
1.33k
      break;
1599
1.59k
    case ZEND_AST_PROP_GROUP: {
1600
1.59k
      zend_ast *type_ast = ast->child[0];
1601
1.59k
      zend_ast *prop_ast = ast->child[1];
1602
1603
1.59k
      if (ast->child[2]) {
1604
103
        zend_ast_export_attributes(str, ast->child[2], indent, 1);
1605
103
      }
1606
1607
1.59k
      zend_ast_export_visibility(str, ast->attr);
1608
1609
1.59k
      if (ast->attr & ZEND_ACC_STATIC) {
1610
486
        smart_str_appends(str, "static ");
1611
486
      }
1612
1613
1.59k
      if (type_ast) {
1614
102
        zend_ast_export_type(str, type_ast, indent);
1615
102
        smart_str_appendc(str, ' ');
1616
102
      }
1617
1618
1.59k
      ast = prop_ast;
1619
1.59k
      goto simple_list;
1620
907
    }
1621
1622
0
    case ZEND_AST_CONST_DECL:
1623
0
      smart_str_appends(str, "const ");
1624
0
      goto simple_list;
1625
1.03k
    case ZEND_AST_CLASS_CONST_GROUP:
1626
1.03k
      if (ast->child[1]) {
1627
106
        zend_ast_export_attributes(str, ast->child[1], indent, 1);
1628
106
      }
1629
1630
1.03k
      zend_ast_export_visibility(str, ast->attr);
1631
1.03k
      smart_str_appends(str, "const ");
1632
1633
1.03k
      ast = ast->child[0];
1634
1635
1.03k
      goto simple_list;
1636
1.78k
    case ZEND_AST_NAME_LIST:
1637
1.78k
      zend_ast_export_name_list(str, (zend_ast_list*)ast, indent);
1638
1.78k
      break;
1639
0
    case ZEND_AST_USE:
1640
0
      smart_str_appends(str, "use ");
1641
0
      if (ast->attr == T_FUNCTION) {
1642
0
        smart_str_appends(str, "function ");
1643
0
      } else if (ast->attr == T_CONST) {
1644
0
        smart_str_appends(str, "const ");
1645
0
      }
1646
0
      goto simple_list;
1647
1648
    /* 0 child nodes */
1649
0
    case ZEND_AST_MAGIC_CONST:
1650
0
      switch (ast->attr) {
1651
0
        case T_LINE:     APPEND_STR("__LINE__");
1652
0
        case T_FILE:     APPEND_STR("__FILE__");
1653
0
        case T_DIR:      APPEND_STR("__DIR__");
1654
0
        case T_TRAIT_C:  APPEND_STR("__TRAIT__");
1655
0
        case T_METHOD_C: APPEND_STR("__METHOD__");
1656
0
        case T_FUNC_C:   APPEND_STR("__FUNCTION__");
1657
0
        case T_NS_C:     APPEND_STR("__NAMESPACE__");
1658
0
        case T_CLASS_C:  APPEND_STR("__CLASS__");
1659
0
        EMPTY_SWITCH_DEFAULT_CASE();
1660
0
      }
1661
0
      break;
1662
2.24k
    case ZEND_AST_TYPE:
1663
2.24k
      switch (ast->attr & ~ZEND_TYPE_NULLABLE) {
1664
1.78k
        case IS_ARRAY:    APPEND_STR("array");
1665
0
        case IS_CALLABLE: APPEND_STR("callable");
1666
462
        case IS_STATIC:   APPEND_STR("static");
1667
0
        case IS_MIXED:    APPEND_STR("mixed");
1668
0
        EMPTY_SWITCH_DEFAULT_CASE();
1669
2.24k
      }
1670
0
      break;
1671
1672
    /* 1 child node */
1673
68.8k
    case ZEND_AST_VAR:
1674
68.8k
      smart_str_appendc(str, '$');
1675
68.8k
      zend_ast_export_var(str, ast->child[0], 0, indent);
1676
68.8k
      break;
1677
6.47k
    case ZEND_AST_CONST:
1678
6.47k
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
1679
6.47k
      break;
1680
444
    case ZEND_AST_UNPACK:
1681
444
      smart_str_appends(str, "...");
1682
444
      ast = ast->child[0];
1683
444
      goto tail_call;
1684
29
    case ZEND_AST_UNARY_PLUS:  PREFIX_OP("+", 240, 241);
1685
71
    case ZEND_AST_UNARY_MINUS: PREFIX_OP("-", 240, 241);
1686
0
    case ZEND_AST_CAST:
1687
0
      switch (ast->attr) {
1688
0
        case IS_NULL:      PREFIX_OP("(unset)",  240, 241);
1689
0
        case _IS_BOOL:     PREFIX_OP("(bool)",   240, 241);
1690
0
        case IS_LONG:      PREFIX_OP("(int)",    240, 241);
1691
0
        case IS_DOUBLE:    PREFIX_OP("(double)", 240, 241);
1692
0
        case IS_STRING:    PREFIX_OP("(string)", 240, 241);
1693
0
        case IS_ARRAY:     PREFIX_OP("(array)",  240, 241);
1694
0
        case IS_OBJECT:    PREFIX_OP("(object)", 240, 241);
1695
0
        EMPTY_SWITCH_DEFAULT_CASE();
1696
0
      }
1697
0
      break;
1698
309
    case ZEND_AST_EMPTY:
1699
309
      FUNC_OP("empty");
1700
306
    case ZEND_AST_ISSET:
1701
306
      FUNC_OP("isset");
1702
369
    case ZEND_AST_SILENCE:
1703
369
      PREFIX_OP("@", 240, 241);
1704
648
    case ZEND_AST_SHELL_EXEC:
1705
648
      smart_str_appendc(str, '`');
1706
648
      if (ast->child[0]->kind == ZEND_AST_ENCAPS_LIST) {
1707
472
        zend_ast_export_encaps_list(str, '`', (zend_ast_list*)ast->child[0], indent);
1708
176
      } else {
1709
176
        zval *zv;
1710
176
        ZEND_ASSERT(ast->child[0]->kind == ZEND_AST_ZVAL);
1711
176
        zv = zend_ast_get_zval(ast->child[0]);
1712
176
        ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
1713
176
        zend_ast_export_qstr(str, '`', Z_STR_P(zv));
1714
176
      }
1715
648
      smart_str_appendc(str, '`');
1716
648
      break;
1717
313
    case ZEND_AST_CLONE:
1718
313
      PREFIX_OP("clone ", 270, 271);
1719
0
    case ZEND_AST_EXIT:
1720
0
      if (ast->child[0]) {
1721
0
        FUNC_OP("exit");
1722
0
      } else {
1723
0
        APPEND_STR("exit");
1724
0
      }
1725
0
      break;
1726
460
    case ZEND_AST_PRINT:
1727
460
      PREFIX_OP("print ", 60, 61);
1728
311
    case ZEND_AST_INCLUDE_OR_EVAL:
1729
311
      switch (ast->attr) {
1730
0
        case ZEND_INCLUDE_ONCE: FUNC_OP("include_once");
1731
0
        case ZEND_INCLUDE:      FUNC_OP("include");
1732
0
        case ZEND_REQUIRE_ONCE: FUNC_OP("require_once");
1733
0
        case ZEND_REQUIRE:      FUNC_OP("require");
1734
311
        case ZEND_EVAL:         FUNC_OP("eval");
1735
0
        EMPTY_SWITCH_DEFAULT_CASE();
1736
311
      }
1737
0
      break;
1738
5.09k
    case ZEND_AST_UNARY_OP:
1739
5.09k
      switch (ast->attr) {
1740
1.68k
        case ZEND_BW_NOT:   PREFIX_OP("~", 240, 241);
1741
3.41k
        case ZEND_BOOL_NOT: PREFIX_OP("!", 240, 241);
1742
0
        EMPTY_SWITCH_DEFAULT_CASE();
1743
5.09k
      }
1744
0
      break;
1745
0
    case ZEND_AST_PRE_INC:
1746
0
      PREFIX_OP("++", 240, 241);
1747
444
    case ZEND_AST_PRE_DEC:
1748
444
      PREFIX_OP("--", 240, 241);
1749
1.36k
    case ZEND_AST_POST_INC:
1750
1.36k
      POSTFIX_OP("++", 240, 241);
1751
421
    case ZEND_AST_POST_DEC:
1752
421
      POSTFIX_OP("--", 240, 241);
1753
1754
635
    case ZEND_AST_GLOBAL:
1755
635
      APPEND_NODE_1("global");
1756
314
    case ZEND_AST_UNSET:
1757
314
      FUNC_OP("unset");
1758
2.44k
    case ZEND_AST_RETURN:
1759
2.44k
      APPEND_NODE_1("return");
1760
489
    case ZEND_AST_LABEL:
1761
489
      zend_ast_export_name(str, ast->child[0], 0, indent);
1762
489
      smart_str_appendc(str, ':');
1763
489
      break;
1764
458
    case ZEND_AST_REF:
1765
458
      smart_str_appendc(str, '&');
1766
458
      ast = ast->child[0];
1767
458
      goto tail_call;
1768
0
    case ZEND_AST_HALT_COMPILER:
1769
0
      APPEND_STR("__HALT_COMPILER()");
1770
1.89k
    case ZEND_AST_ECHO:
1771
1.89k
      APPEND_NODE_1("echo");
1772
0
    case ZEND_AST_THROW:
1773
0
      APPEND_NODE_1("throw");
1774
382
    case ZEND_AST_GOTO:
1775
382
      smart_str_appends(str, "goto ");
1776
382
      zend_ast_export_name(str, ast->child[0], 0, indent);
1777
382
      break;
1778
799
    case ZEND_AST_BREAK:
1779
799
      APPEND_NODE_1("break");
1780
786
    case ZEND_AST_CONTINUE:
1781
786
      APPEND_NODE_1("continue");
1782
1783
    /* 2 child nodes */
1784
5.25k
    case ZEND_AST_DIM:
1785
5.25k
      zend_ast_export_ex(str, ast->child[0], 260, indent);
1786
5.25k
      smart_str_appendc(str, '[');
1787
5.25k
      if (ast->child[1]) {
1788
5.24k
        zend_ast_export_ex(str, ast->child[1], 0, indent);
1789
5.24k
      }
1790
5.25k
      smart_str_appendc(str, ']');
1791
5.25k
      break;
1792
2.26k
    case ZEND_AST_PROP:
1793
2.69k
    case ZEND_AST_NULLSAFE_PROP:
1794
2.69k
      zend_ast_export_ex(str, ast->child[0], 0, indent);
1795
2.69k
      smart_str_appends(str, ast->kind == ZEND_AST_NULLSAFE_PROP ? "?->" : "->");
1796
2.69k
      zend_ast_export_var(str, ast->child[1], 0, indent);
1797
2.69k
      break;
1798
1.27k
    case ZEND_AST_STATIC_PROP:
1799
1.27k
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
1800
1.27k
      smart_str_appends(str, "::$");
1801
1.27k
      zend_ast_export_var(str, ast->child[1], 0, indent);
1802
1.27k
      break;
1803
11.7k
    case ZEND_AST_CALL:
1804
11.7k
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
1805
11.7k
      smart_str_appendc(str, '(');
1806
11.7k
      zend_ast_export_ex(str, ast->child[1], 0, indent);
1807
11.7k
      smart_str_appendc(str, ')');
1808
11.7k
      break;
1809
1.73k
    case ZEND_AST_CLASS_CONST:
1810
1.73k
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
1811
1.73k
      smart_str_appends(str, "::");
1812
1.73k
      zend_ast_export_name(str, ast->child[1], 0, indent);
1813
1.73k
      break;
1814
2
    case ZEND_AST_CLASS_NAME:
1815
2
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
1816
2
      smart_str_appends(str, "::class");
1817
2
      break;
1818
20.2k
    case ZEND_AST_ASSIGN:            BINARY_OP(" = ",   90, 91, 90);
1819
0
    case ZEND_AST_ASSIGN_REF:        BINARY_OP(" =& ",  90, 91, 90);
1820
34
    case ZEND_AST_ASSIGN_OP:
1821
34
      switch (ast->attr) {
1822
0
        case ZEND_ADD:    BINARY_OP(" += ",  90, 91, 90);
1823
1
        case ZEND_SUB:    BINARY_OP(" -= ",  90, 91, 90);
1824
1
        case ZEND_MUL:    BINARY_OP(" *= ",  90, 91, 90);
1825
1
        case ZEND_DIV:    BINARY_OP(" /= ",  90, 91, 90);
1826
0
        case ZEND_MOD:    BINARY_OP(" %= ",  90, 91, 90);
1827
0
        case ZEND_SL:     BINARY_OP(" <<= ", 90, 91, 90);
1828
0
        case ZEND_SR:     BINARY_OP(" >>= ", 90, 91, 90);
1829
1
        case ZEND_CONCAT: BINARY_OP(" .= ",  90, 91, 90);
1830
0
        case ZEND_BW_OR:  BINARY_OP(" |= ",  90, 91, 90);
1831
1
        case ZEND_BW_AND: BINARY_OP(" &= ",  90, 91, 90);
1832
0
        case ZEND_BW_XOR: BINARY_OP(" ^= ",  90, 91, 90);
1833
29
        case ZEND_POW:    BINARY_OP(" **= ", 90, 91, 90);
1834
0
        EMPTY_SWITCH_DEFAULT_CASE();
1835
34
      }
1836
0
      break;
1837
1
    case ZEND_AST_ASSIGN_COALESCE: BINARY_OP(" \?\?= ", 90, 91, 90);
1838
13.9k
    case ZEND_AST_BINARY_OP:
1839
13.9k
      switch (ast->attr) {
1840
669
        case ZEND_ADD:                 BINARY_OP(" + ",   200, 200, 201);
1841
127
        case ZEND_SUB:                 BINARY_OP(" - ",   200, 200, 201);
1842
484
        case ZEND_MUL:                 BINARY_OP(" * ",   210, 210, 211);
1843
458
        case ZEND_DIV:                 BINARY_OP(" / ",   210, 210, 211);
1844
25
        case ZEND_MOD:                 BINARY_OP(" % ",   210, 210, 211);
1845
0
        case ZEND_SL:                  BINARY_OP(" << ",  190, 190, 191);
1846
0
        case ZEND_SR:                  BINARY_OP(" >> ",  190, 190, 191);
1847
4.35k
        case ZEND_CONCAT:              BINARY_OP(" . ",   185, 185, 186);
1848
32
        case ZEND_BW_OR:               BINARY_OP(" | ",   140, 140, 141);
1849
137
        case ZEND_BW_AND:              BINARY_OP(" & ",   160, 160, 161);
1850
70
        case ZEND_BW_XOR:              BINARY_OP(" ^ ",   150, 150, 151);
1851
2.02k
        case ZEND_IS_IDENTICAL:        BINARY_OP(" === ", 170, 171, 171);
1852
22
        case ZEND_IS_NOT_IDENTICAL:    BINARY_OP(" !== ", 170, 171, 171);
1853
5.03k
        case ZEND_IS_EQUAL:            BINARY_OP(" == ",  170, 171, 171);
1854
11
        case ZEND_IS_NOT_EQUAL:        BINARY_OP(" != ",  170, 171, 171);
1855
488
        case ZEND_IS_SMALLER:          BINARY_OP(" < ",   180, 181, 181);
1856
30
        case ZEND_IS_SMALLER_OR_EQUAL: BINARY_OP(" <= ",  180, 181, 181);
1857
1
        case ZEND_POW:                 BINARY_OP(" ** ",  250, 251, 250);
1858
0
        case ZEND_BOOL_XOR:            BINARY_OP(" xor ",  40,  40,  41);
1859
0
        case ZEND_SPACESHIP:           BINARY_OP(" <=> ", 180, 181, 181);
1860
0
        EMPTY_SWITCH_DEFAULT_CASE();
1861
13.9k
      }
1862
0
      break;
1863
49
    case ZEND_AST_GREATER:                 BINARY_OP(" > ",   180, 181, 181);
1864
22
    case ZEND_AST_GREATER_EQUAL:           BINARY_OP(" >= ",  180, 181, 181);
1865
4.26k
    case ZEND_AST_AND:                     BINARY_OP(" && ",  130, 130, 131);
1866
1.86k
    case ZEND_AST_OR:                      BINARY_OP(" || ",  120, 120, 121);
1867
3.20k
    case ZEND_AST_ARRAY_ELEM:
1868
3.20k
      if (ast->child[1]) {
1869
630
        zend_ast_export_ex(str, ast->child[1], 80, indent);
1870
630
        smart_str_appends(str, " => ");
1871
630
      }
1872
3.20k
      if (ast->attr)
1873
0
        smart_str_appendc(str, '&');
1874
3.20k
      zend_ast_export_ex(str, ast->child[0], 80, indent);
1875
3.20k
      break;
1876
2.41k
    case ZEND_AST_NEW:
1877
2.41k
      smart_str_appends(str, "new ");
1878
2.41k
      if (ast->child[0]->kind == ZEND_AST_CLASS) {
1879
180
        zend_ast_decl *decl = (zend_ast_decl *) ast->child[0];
1880
180
        if (decl->child[4]) {
1881
107
          zend_ast_export_attributes(str, decl->child[4], indent, 0);
1882
107
        }
1883
180
        smart_str_appends(str, "class");
1884
180
        if (zend_ast_get_list(ast->child[1])->children) {
1885
20
          smart_str_appendc(str, '(');
1886
20
          zend_ast_export_ex(str, ast->child[1], 0, indent);
1887
20
          smart_str_appendc(str, ')');
1888
20
        }
1889
180
        zend_ast_export_class_no_header(str, decl, indent);
1890
2.23k
      } else {
1891
2.23k
        zend_ast_export_ns_name(str, ast->child[0], 0, indent);
1892
2.23k
        smart_str_appendc(str, '(');
1893
2.23k
        zend_ast_export_ex(str, ast->child[1], 0, indent);
1894
2.23k
        smart_str_appendc(str, ')');
1895
2.23k
      }
1896
2.41k
      break;
1897
237
    case ZEND_AST_INSTANCEOF:
1898
237
      zend_ast_export_ex(str, ast->child[0], 0, indent);
1899
237
      smart_str_appends(str, " instanceof ");
1900
237
      zend_ast_export_ns_name(str, ast->child[1], 0, indent);
1901
237
      break;
1902
342
    case ZEND_AST_YIELD:
1903
342
      if (priority > 70) smart_str_appendc(str, '(');
1904
342
      smart_str_appends(str, "yield ");
1905
342
      if (ast->child[0]) {
1906
340
        if (ast->child[1]) {
1907
314
          zend_ast_export_ex(str, ast->child[1], 70, indent);
1908
314
          smart_str_appends(str, " => ");
1909
314
        }
1910
340
        zend_ast_export_ex(str, ast->child[0], 70, indent);
1911
340
      }
1912
342
      if (priority > 70) smart_str_appendc(str, ')');
1913
342
      break;
1914
311
    case ZEND_AST_YIELD_FROM:
1915
311
      PREFIX_OP("yield from ", 85, 86);
1916
314
    case ZEND_AST_COALESCE: BINARY_OP(" ?? ", 110, 111, 110);
1917
630
    case ZEND_AST_STATIC:
1918
630
      smart_str_appends(str, "static $");
1919
630
      zend_ast_export_name(str, ast->child[0], 0, indent);
1920
630
      APPEND_DEFAULT_VALUE(1);
1921
463
    case ZEND_AST_WHILE:
1922
463
      smart_str_appends(str, "while (");
1923
463
      zend_ast_export_ex(str, ast->child[0], 0, indent);
1924
463
      smart_str_appends(str, ") {\n");
1925
463
      zend_ast_export_stmt(str, ast->child[1], indent + 1);
1926
463
      zend_ast_export_indent(str, indent);
1927
463
      smart_str_appendc(str, '}');
1928
463
      break;
1929
869
    case ZEND_AST_DO_WHILE:
1930
869
      smart_str_appends(str, "do {\n");
1931
869
      zend_ast_export_stmt(str, ast->child[0], indent + 1);
1932
869
      zend_ast_export_indent(str, indent);
1933
869
      smart_str_appends(str, "} while (");
1934
869
      zend_ast_export_ex(str, ast->child[1], 0, indent);
1935
869
      smart_str_appendc(str, ')');
1936
869
      break;
1937
1938
0
    case ZEND_AST_IF_ELEM:
1939
0
      if (ast->child[0]) {
1940
0
        smart_str_appends(str, "if (");
1941
0
        zend_ast_export_ex(str, ast->child[0], 0, indent);
1942
0
        smart_str_appends(str, ") {\n");
1943
0
        zend_ast_export_stmt(str, ast->child[1], indent + 1);
1944
0
      } else {
1945
0
        smart_str_appends(str, "else {\n");
1946
0
        zend_ast_export_stmt(str, ast->child[1], indent + 1);
1947
0
      }
1948
0
      zend_ast_export_indent(str, indent);
1949
0
      smart_str_appendc(str, '}');
1950
0
      break;
1951
428
    case ZEND_AST_SWITCH:
1952
428
      smart_str_appends(str, "switch (");
1953
428
      zend_ast_export_ex(str, ast->child[0], 0, indent);
1954
428
      smart_str_appends(str, ") {\n");
1955
428
      zend_ast_export_ex(str, ast->child[1], 0, indent + 1);
1956
428
      zend_ast_export_indent(str, indent);
1957
428
      smart_str_appendc(str, '}');
1958
428
      break;
1959
2.33k
    case ZEND_AST_SWITCH_CASE:
1960
2.33k
      zend_ast_export_indent(str, indent);
1961
2.33k
      if (ast->child[0]) {
1962
1.98k
        smart_str_appends(str, "case ");
1963
1.98k
        zend_ast_export_ex(str, ast->child[0], 0, indent);
1964
1.98k
        smart_str_appends(str, ":\n");
1965
358
      } else {
1966
358
        smart_str_appends(str, "default:\n");
1967
358
      }
1968
2.33k
      zend_ast_export_stmt(str, ast->child[1], indent + 1);
1969
2.33k
      break;
1970
56
    case ZEND_AST_MATCH:
1971
56
      smart_str_appends(str, "match (");
1972
56
      zend_ast_export_ex(str, ast->child[0], 0, indent);
1973
56
      smart_str_appends(str, ") {\n");
1974
56
      zend_ast_export_ex(str, ast->child[1], 0, indent + 1);
1975
56
      zend_ast_export_indent(str, indent);
1976
56
      smart_str_appendc(str, '}');
1977
56
      break;
1978
155
    case ZEND_AST_MATCH_ARM:
1979
155
      zend_ast_export_indent(str, indent);
1980
155
      if (ast->child[0]) {
1981
115
        zend_ast_export_list(str, (zend_ast_list*)ast->child[0], 1, 0, indent);
1982
115
        smart_str_appends(str, " => ");
1983
40
      } else {
1984
40
        smart_str_appends(str, "default => ");
1985
40
      }
1986
155
      zend_ast_export_ex(str, ast->child[1], 0, 0);
1987
155
      smart_str_appends(str, ",\n");
1988
155
      break;
1989
947
    case ZEND_AST_DECLARE:
1990
947
      smart_str_appends(str, "declare(");
1991
947
      ZEND_ASSERT(ast->child[0]->kind == ZEND_AST_CONST_DECL);
1992
947
      zend_ast_export_list(str, (zend_ast_list*)ast->child[0], 1, 0, indent);
1993
947
      smart_str_appendc(str, ')');
1994
947
      if (ast->child[1]) {
1995
469
        smart_str_appends(str, " {\n");
1996
469
        zend_ast_export_stmt(str, ast->child[1], indent + 1);
1997
469
        zend_ast_export_indent(str, indent);
1998
469
        smart_str_appendc(str, '}');
1999
478
      } else {
2000
478
        smart_str_appendc(str, ';');
2001
478
      }
2002
947
      break;
2003
2.05k
    case ZEND_AST_PROP_ELEM:
2004
2.05k
      smart_str_appendc(str, '$');
2005
      /* break missing intentionally */
2006
4.98k
    case ZEND_AST_CONST_ELEM:
2007
4.98k
      zend_ast_export_name(str, ast->child[0], 0, indent);
2008
4.98k
      APPEND_DEFAULT_VALUE(1);
2009
903
    case ZEND_AST_USE_TRAIT:
2010
903
      smart_str_appends(str, "use ");
2011
903
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2012
903
      if (ast->child[1]) {
2013
451
        smart_str_appends(str, " {\n");
2014
451
        zend_ast_export_ex(str, ast->child[1], 0, indent + 1);
2015
451
        zend_ast_export_indent(str, indent);
2016
451
        smart_str_appends(str, "}");
2017
452
      } else {
2018
452
        smart_str_appends(str, ";");
2019
452
      }
2020
903
      break;
2021
412
    case ZEND_AST_TRAIT_PRECEDENCE:
2022
412
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2023
412
      smart_str_appends(str, " insteadof ");
2024
412
      zend_ast_export_ex(str, ast->child[1], 0, indent);
2025
412
      break;
2026
1.75k
    case ZEND_AST_METHOD_REFERENCE:
2027
1.75k
      if (ast->child[0]) {
2028
859
        zend_ast_export_name(str, ast->child[0], 0, indent);
2029
859
        smart_str_appends(str, "::");
2030
859
      }
2031
1.75k
      zend_ast_export_name(str, ast->child[1], 0, indent);
2032
1.75k
      break;
2033
0
    case ZEND_AST_NAMESPACE:
2034
0
      smart_str_appends(str, "namespace");
2035
0
      if (ast->child[0]) {
2036
0
        smart_str_appendc(str, ' ');
2037
0
        zend_ast_export_name(str, ast->child[0], 0, indent);
2038
0
      }
2039
0
      if (ast->child[1]) {
2040
0
        smart_str_appends(str, " {\n");
2041
0
        zend_ast_export_stmt(str, ast->child[1], indent + 1);
2042
0
        zend_ast_export_indent(str, indent);
2043
0
        smart_str_appends(str, "}\n");
2044
0
      } else {
2045
0
        smart_str_appendc(str, ';');
2046
0
      }
2047
0
      break;
2048
0
    case ZEND_AST_USE_ELEM:
2049
1.33k
    case ZEND_AST_TRAIT_ALIAS:
2050
1.33k
      zend_ast_export_name(str, ast->child[0], 0, indent);
2051
1.33k
      if (ast->attr & ZEND_ACC_PUBLIC) {
2052
413
        smart_str_appends(str, " as public");
2053
925
      } else if (ast->attr & ZEND_ACC_PROTECTED) {
2054
449
        smart_str_appends(str, " as protected");
2055
476
      } else if (ast->attr & ZEND_ACC_PRIVATE) {
2056
0
        smart_str_appends(str, " as private");
2057
476
      } else if (ast->child[1]) {
2058
476
        smart_str_appends(str, " as");
2059
476
      }
2060
1.33k
      if (ast->child[1]) {
2061
925
        smart_str_appendc(str, ' ');
2062
925
        zend_ast_export_name(str, ast->child[1], 0, indent);
2063
925
      }
2064
1.33k
      break;
2065
453
    case ZEND_AST_NAMED_ARG:
2066
453
      smart_str_append(str, zend_ast_get_str(ast->child[0]));
2067
453
      smart_str_appends(str, ": ");
2068
453
      ast = ast->child[1];
2069
453
      goto tail_call;
2070
2071
    /* 3 child nodes */
2072
1.24k
    case ZEND_AST_METHOD_CALL:
2073
1.68k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2074
1.68k
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2075
1.68k
      smart_str_appends(str, ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL ? "?->" : "->");
2076
1.68k
      zend_ast_export_var(str, ast->child[1], 0, indent);
2077
1.68k
      smart_str_appendc(str, '(');
2078
1.68k
      zend_ast_export_ex(str, ast->child[2], 0, indent);
2079
1.68k
      smart_str_appendc(str, ')');
2080
1.68k
      break;
2081
1.29k
    case ZEND_AST_STATIC_CALL:
2082
1.29k
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2083
1.29k
      smart_str_appends(str, "::");
2084
1.29k
      zend_ast_export_var(str, ast->child[1], 0, indent);
2085
1.29k
      smart_str_appendc(str, '(');
2086
1.29k
      zend_ast_export_ex(str, ast->child[2], 0, indent);
2087
1.29k
      smart_str_appendc(str, ')');
2088
1.29k
      break;
2089
652
    case ZEND_AST_CONDITIONAL:
2090
652
      if (priority > 100) smart_str_appendc(str, '(');
2091
652
      zend_ast_export_ex(str, ast->child[0], 100, indent);
2092
652
      if (ast->child[1]) {
2093
316
        smart_str_appends(str, " ? ");
2094
316
        zend_ast_export_ex(str, ast->child[1], 101, indent);
2095
316
        smart_str_appends(str, " : ");
2096
336
      } else {
2097
336
        smart_str_appends(str, " ?: ");
2098
336
      }
2099
652
      zend_ast_export_ex(str, ast->child[2], 101, indent);
2100
652
      if (priority > 100) smart_str_appendc(str, ')');
2101
652
      break;
2102
2103
479
    case ZEND_AST_TRY:
2104
479
      smart_str_appends(str, "try {\n");
2105
479
      zend_ast_export_stmt(str, ast->child[0], indent + 1);
2106
479
      zend_ast_export_indent(str, indent);
2107
479
      zend_ast_export_ex(str, ast->child[1], 0, indent);
2108
479
      if (ast->child[2]) {
2109
477
        smart_str_appends(str, "} finally {\n");
2110
477
        zend_ast_export_stmt(str, ast->child[2], indent + 1);
2111
477
        zend_ast_export_indent(str, indent);
2112
477
      }
2113
479
      smart_str_appendc(str, '}');
2114
479
      break;
2115
954
    case ZEND_AST_CATCH:
2116
954
      smart_str_appends(str, "} catch (");
2117
954
      zend_ast_export_catch_name_list(str, zend_ast_get_list(ast->child[0]), indent);
2118
954
      if (ast->child[1]) {
2119
952
        smart_str_appends(str, " $");
2120
952
        zend_ast_export_var(str, ast->child[1], 0, indent);
2121
952
      }
2122
954
      smart_str_appends(str, ") {\n");
2123
954
      zend_ast_export_stmt(str, ast->child[2], indent + 1);
2124
954
      zend_ast_export_indent(str, indent);
2125
954
      break;
2126
3.89k
    case ZEND_AST_PARAM:
2127
3.89k
      if (ast->child[3]) {
2128
94
        zend_ast_export_attributes(str, ast->child[3], indent, 0);
2129
94
      }
2130
3.89k
      if (ast->child[0]) {
2131
3.70k
        zend_ast_export_type(str, ast->child[0], indent);
2132
3.70k
        smart_str_appendc(str, ' ');
2133
3.70k
      }
2134
3.89k
      if (ast->attr & ZEND_PARAM_REF) {
2135
1.81k
        smart_str_appendc(str, '&');
2136
1.81k
      }
2137
3.89k
      if (ast->attr & ZEND_PARAM_VARIADIC) {
2138
519
        smart_str_appends(str, "...");
2139
519
      }
2140
3.89k
      smart_str_appendc(str, '$');
2141
3.89k
      zend_ast_export_name(str, ast->child[1], 0, indent);
2142
3.89k
      APPEND_DEFAULT_VALUE(2);
2143
2144
    /* 4 child nodes */
2145
445
    case ZEND_AST_FOR:
2146
445
      smart_str_appends(str, "for (");
2147
445
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2148
445
      smart_str_appendc(str, ';');
2149
445
      if (ast->child[1]) {
2150
445
        smart_str_appendc(str, ' ');
2151
445
        zend_ast_export_ex(str, ast->child[1], 0, indent);
2152
445
      }
2153
445
      smart_str_appendc(str, ';');
2154
445
      if (ast->child[2]) {
2155
445
        smart_str_appendc(str, ' ');
2156
445
        zend_ast_export_ex(str, ast->child[2], 0, indent);
2157
445
      }
2158
445
      smart_str_appends(str, ") {\n");
2159
445
      zend_ast_export_stmt(str, ast->child[3], indent + 1);
2160
445
      zend_ast_export_indent(str, indent);
2161
445
      smart_str_appendc(str, '}');
2162
445
      break;
2163
458
    case ZEND_AST_FOREACH:
2164
458
      smart_str_appends(str, "foreach (");
2165
458
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2166
458
      smart_str_appends(str, " as ");
2167
458
      if (ast->child[2]) {
2168
458
        zend_ast_export_ex(str, ast->child[2], 0, indent);
2169
458
        smart_str_appends(str, " => ");
2170
458
      }
2171
458
      zend_ast_export_ex(str, ast->child[1], 0, indent);
2172
458
      smart_str_appends(str, ") {\n");
2173
458
      zend_ast_export_stmt(str, ast->child[3], indent + 1);
2174
458
      zend_ast_export_indent(str, indent);
2175
458
      smart_str_appendc(str, '}');
2176
458
      break;
2177
0
    EMPTY_SWITCH_DEFAULT_CASE();
2178
280k
  }
2179
210k
  return;
2180
2181
40.8k
binary_op:
2182
40.8k
  if (priority > p) smart_str_appendc(str, '(');
2183
40.8k
  zend_ast_export_ex(str, ast->child[0], pl, indent);
2184
40.8k
  smart_str_appends(str, op);
2185
40.8k
  zend_ast_export_ex(str, ast->child[1], pr, indent);
2186
40.8k
  if (priority > p) smart_str_appendc(str, ')');
2187
40.8k
  return;
2188
2189
7.09k
prefix_op:
2190
7.09k
  if (priority > p) smart_str_appendc(str, '(');
2191
7.09k
  smart_str_appends(str, op);
2192
7.09k
  zend_ast_export_ex(str, ast->child[0], pl, indent);
2193
7.09k
  if (priority > p) smart_str_appendc(str, ')');
2194
7.09k
  return;
2195
2196
1.78k
postfix_op:
2197
1.78k
  if (priority > p) smart_str_appendc(str, '(');
2198
1.78k
  zend_ast_export_ex(str, ast->child[0], pl, indent);
2199
1.78k
  smart_str_appends(str, op);
2200
1.78k
  if (priority > p) smart_str_appendc(str, ')');
2201
1.78k
  return;
2202
2203
1.24k
func_op:
2204
1.24k
  smart_str_appends(str, op);
2205
1.24k
  smart_str_appendc(str, '(');
2206
1.24k
  zend_ast_export_ex(str, ast->child[0], 0, indent);
2207
1.24k
  smart_str_appendc(str, ')');
2208
1.24k
  return;
2209
2210
6.56k
append_node_1:
2211
6.56k
  smart_str_appends(str, op);
2212
6.56k
  if (ast->child[0]) {
2213
5.34k
    smart_str_appendc(str, ' ');
2214
5.34k
    ast = ast->child[0];
2215
5.34k
    goto tail_call;
2216
5.34k
  }
2217
1.22k
  return;
2218
2219
2.24k
append_str:
2220
2.24k
  smart_str_appends(str, op);
2221
2.24k
  return;
2222
2223
9.50k
append_default_value:
2224
9.50k
  if (ast->child[p]) {
2225
5.52k
    smart_str_appends(str, " = ");
2226
5.52k
    ast = ast->child[p];
2227
5.52k
    goto tail_call;
2228
5.52k
  }
2229
3.98k
  return;
2230
3.98k
}
2231
2232
ZEND_API ZEND_COLD zend_string *zend_ast_export(const char *prefix, zend_ast *ast, const char *suffix)
2233
17.5k
{
2234
17.5k
  smart_str str = {0};
2235
2236
17.5k
  smart_str_appends(&str, prefix);
2237
17.5k
  zend_ast_export_ex(&str, ast, 0, 0);
2238
17.5k
  smart_str_appends(&str, suffix);
2239
17.5k
  smart_str_0(&str);
2240
17.5k
  return str.s;
2241
17.5k
}
2242
2243
zend_ast * ZEND_FASTCALL zend_ast_with_attributes(zend_ast *ast, zend_ast *attr)
2244
15.1k
{
2245
15.1k
  ZEND_ASSERT(attr->kind == ZEND_AST_ATTRIBUTE_LIST);
2246
2247
15.1k
  switch (ast->kind) {
2248
261
  case ZEND_AST_FUNC_DECL:
2249
5.36k
  case ZEND_AST_CLOSURE:
2250
6.71k
  case ZEND_AST_METHOD:
2251
11.8k
  case ZEND_AST_CLASS:
2252
12.0k
  case ZEND_AST_ARROW_FUNC:
2253
12.0k
    ((zend_ast_decl *) ast)->child[4] = attr;
2254
12.0k
    break;
2255
1.85k
  case ZEND_AST_PROP_GROUP:
2256
1.85k
    ast->child[2] = attr;
2257
1.85k
    break;
2258
677
  case ZEND_AST_PARAM:
2259
677
    ast->child[3] = attr;
2260
677
    break;
2261
541
  case ZEND_AST_CLASS_CONST_GROUP:
2262
541
    ast->child[1] = attr;
2263
541
    break;
2264
0
  EMPTY_SWITCH_DEFAULT_CASE()
2265
15.1k
  }
2266
2267
15.1k
  return ast;
2268
15.1k
}