Coverage Report

Created: 2022-02-19 20: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
41.3M
static inline void *zend_ast_alloc(size_t size) {
31
41.3M
  return zend_arena_alloc(&CG(ast_arena), size);
32
41.3M
}
33
34
552k
static inline void *zend_ast_realloc(void *old, size_t old_size, size_t new_size) {
35
552k
  void *new = zend_ast_alloc(new_size);
36
552k
  memcpy(new, old, old_size);
37
552k
  return new;
38
552k
}
39
40
16.1M
static inline size_t zend_ast_size(uint32_t children) {
41
16.1M
  return sizeof(zend_ast) - sizeof(zend_ast *) + sizeof(zend_ast *) * children;
42
16.1M
}
43
44
9.37M
static inline size_t zend_ast_list_size(uint32_t children) {
45
9.37M
  return sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * children;
46
9.37M
}
47
48
21.6k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_znode(znode *node) {
49
21.6k
  zend_ast_znode *ast;
50
51
21.6k
  ast = zend_ast_alloc(sizeof(zend_ast_znode));
52
21.6k
  ast->kind = ZEND_AST_ZNODE;
53
21.6k
  ast->attr = 0;
54
21.6k
  ast->lineno = CG(zend_lineno);
55
21.6k
  ast->node = *node;
56
21.6k
  return (zend_ast *) ast;
57
21.6k
}
58
59
15.8M
static zend_always_inline zend_ast * zend_ast_create_zval_int(zval *zv, uint32_t attr, uint32_t lineno) {
60
15.8M
  zend_ast_zval *ast;
61
62
15.8M
  ast = zend_ast_alloc(sizeof(zend_ast_zval));
63
15.8M
  ast->kind = ZEND_AST_ZVAL;
64
15.8M
  ast->attr = attr;
65
15.8M
  ZVAL_COPY_VALUE(&ast->val, zv);
66
15.8M
  Z_LINENO(ast->val) = lineno;
67
15.8M
  return (zend_ast *) ast;
68
15.8M
}
69
70
15.5M
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_with_lineno(zval *zv, uint32_t lineno) {
71
15.5M
  return zend_ast_create_zval_int(zv, 0, lineno);
72
15.5M
}
73
74
3.96k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_ex(zval *zv, zend_ast_attr attr) {
75
3.96k
  return zend_ast_create_zval_int(zv, attr, CG(zend_lineno));
76
3.96k
}
77
78
323k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval(zval *zv) {
79
323k
  return zend_ast_create_zval_int(zv, 0, CG(zend_lineno));
80
323k
}
81
82
25.0k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_str(zend_string *str) {
83
25.0k
  zval zv;
84
25.0k
  ZVAL_STR(&zv, str);
85
25.0k
  return zend_ast_create_zval_int(&zv, 0, CG(zend_lineno));
86
25.0k
}
87
88
454
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_long(zend_long lval) {
89
454
  zval zv;
90
454
  ZVAL_LONG(&zv, lval);
91
454
  return zend_ast_create_zval_int(&zv, 0, CG(zend_lineno));
92
454
}
93
94
31.6k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_constant(zend_string *name, zend_ast_attr attr) {
95
31.6k
  zend_ast_zval *ast;
96
97
31.6k
  ast = zend_ast_alloc(sizeof(zend_ast_zval));
98
31.6k
  ast->kind = ZEND_AST_CONSTANT;
99
31.6k
  ast->attr = attr;
100
31.6k
  ZVAL_STR(&ast->val, name);
101
31.6k
  Z_LINENO(ast->val) = CG(zend_lineno);
102
31.6k
  return (zend_ast *) ast;
103
31.6k
}
104
105
98.4k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_class_const_or_name(zend_ast *class_name, zend_ast *name) {
106
98.4k
  zend_string *name_str = zend_ast_get_str(name);
107
98.4k
  if (zend_string_equals_literal_ci(name_str, "class")) {
108
22.9k
    zend_string_release(name_str);
109
22.9k
    return zend_ast_create(ZEND_AST_CLASS_NAME, class_name);
110
75.4k
  } else {
111
75.4k
    return zend_ast_create(ZEND_AST_CLASS_CONST, class_name, name);
112
75.4k
  }
113
98.4k
}
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
563k
) {
119
563k
  zend_ast_decl *ast;
120
121
563k
  ast = zend_ast_alloc(sizeof(zend_ast_decl));
122
563k
  ast->kind = kind;
123
563k
  ast->attr = 0;
124
563k
  ast->start_lineno = start_lineno;
125
563k
  ast->end_lineno = CG(zend_lineno);
126
563k
  ast->flags = flags;
127
563k
  ast->lex_pos = LANG_SCNG(yy_text);
128
563k
  ast->doc_comment = doc_comment;
129
563k
  ast->name = name;
130
563k
  ast->child[0] = child0;
131
563k
  ast->child[1] = child1;
132
563k
  ast->child[2] = child2;
133
563k
  ast->child[3] = child3;
134
563k
  ast->child[4] = child4;
135
136
563k
  return (zend_ast *) ast;
137
563k
}
138
139
#if ZEND_AST_SPEC
140
73.7k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_0(zend_ast_kind kind) {
141
73.7k
  zend_ast *ast;
142
143
73.7k
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 0);
144
73.7k
  ast = zend_ast_alloc(zend_ast_size(0));
145
73.7k
  ast->kind = kind;
146
73.7k
  ast->attr = 0;
147
73.7k
  ast->lineno = CG(zend_lineno);
148
149
73.7k
  return ast;
150
73.7k
}
151
152
7.27M
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_1(zend_ast_kind kind, zend_ast *child) {
153
7.27M
  zend_ast *ast;
154
7.27M
  uint32_t lineno;
155
156
7.27M
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 1);
157
7.27M
  ast = zend_ast_alloc(zend_ast_size(1));
158
7.27M
  ast->kind = kind;
159
7.27M
  ast->attr = 0;
160
7.27M
  ast->child[0] = child;
161
7.27M
  if (child) {
162
7.26M
    lineno = zend_ast_get_lineno(child);
163
13.1k
  } else {
164
13.1k
    lineno = CG(zend_lineno);
165
13.1k
  }
166
7.27M
  ast->lineno = lineno;
167
7.27M
  ast->lineno = lineno;
168
169
7.27M
  return ast;
170
7.27M
}
171
172
6.65M
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_2(zend_ast_kind kind, zend_ast *child1, zend_ast *child2) {
173
6.65M
  zend_ast *ast;
174
6.65M
  uint32_t lineno;
175
176
6.65M
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 2);
177
6.65M
  ast = zend_ast_alloc(zend_ast_size(2));
178
6.65M
  ast->kind = kind;
179
6.65M
  ast->attr = 0;
180
6.65M
  ast->child[0] = child1;
181
6.65M
  ast->child[1] = child2;
182
6.65M
  if (child1) {
183
6.62M
    lineno = zend_ast_get_lineno(child1);
184
26.9k
  } else if (child2) {
185
22.1k
    lineno = zend_ast_get_lineno(child2);
186
4.79k
  } else {
187
4.79k
    lineno = CG(zend_lineno);
188
4.79k
  }
189
6.65M
  ast->lineno = lineno;
190
191
6.65M
  return ast;
192
6.65M
}
193
194
1.69M
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.69M
  zend_ast *ast;
196
1.69M
  uint32_t lineno;
197
198
1.69M
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 3);
199
1.69M
  ast = zend_ast_alloc(zend_ast_size(3));
200
1.69M
  ast->kind = kind;
201
1.69M
  ast->attr = 0;
202
1.69M
  ast->child[0] = child1;
203
1.69M
  ast->child[1] = child2;
204
1.69M
  ast->child[2] = child3;
205
1.69M
  if (child1) {
206
1.62M
    lineno = zend_ast_get_lineno(child1);
207
64.5k
  } else if (child2) {
208
64.5k
    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.69M
  ast->lineno = lineno;
215
216
1.69M
  return ast;
217
1.69M
}
218
219
73.3k
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
73.3k
  zend_ast *ast;
221
73.3k
  uint32_t lineno;
222
223
73.3k
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 4);
224
73.3k
  ast = zend_ast_alloc(zend_ast_size(4));
225
73.3k
  ast->kind = kind;
226
73.3k
  ast->attr = 0;
227
73.3k
  ast->child[0] = child1;
228
73.3k
  ast->child[1] = child2;
229
73.3k
  ast->child[2] = child3;
230
73.3k
  ast->child[3] = child4;
231
73.3k
  if (child1) {
232
73.2k
    lineno = zend_ast_get_lineno(child1);
233
101
  } else if (child2) {
234
43
    lineno = zend_ast_get_lineno(child2);
235
58
  } else if (child3) {
236
9
    lineno = zend_ast_get_lineno(child3);
237
49
  } else if (child4) {
238
46
    lineno = zend_ast_get_lineno(child4);
239
3
  } else {
240
3
    lineno = CG(zend_lineno);
241
3
  }
242
73.3k
  ast->lineno = lineno;
243
244
73.3k
  return ast;
245
73.3k
}
246
247
316k
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
316k
  zend_ast *ast;
249
316k
  uint32_t lineno;
250
251
316k
  ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 5);
252
316k
  ast = zend_ast_alloc(zend_ast_size(5));
253
316k
  ast->kind = kind;
254
316k
  ast->attr = 0;
255
316k
  ast->child[0] = child1;
256
316k
  ast->child[1] = child2;
257
316k
  ast->child[2] = child3;
258
316k
  ast->child[3] = child4;
259
316k
  ast->child[4] = child5;
260
316k
  if (child1) {
261
67.5k
    lineno = zend_ast_get_lineno(child1);
262
248k
  } else if (child2) {
263
248k
    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
316k
  ast->lineno = lineno;
274
275
316k
  return ast;
276
316k
}
277
278
3.99M
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_0(zend_ast_kind kind) {
279
3.99M
  zend_ast *ast;
280
3.99M
  zend_ast_list *list;
281
282
3.99M
  ast = zend_ast_alloc(zend_ast_list_size(4));
283
3.99M
  list = (zend_ast_list *) ast;
284
3.99M
  list->kind = kind;
285
3.99M
  list->attr = 0;
286
3.99M
  list->lineno = CG(zend_lineno);
287
3.99M
  list->children = 0;
288
289
3.99M
  return ast;
290
3.99M
}
291
292
4.09M
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_1(zend_ast_kind kind, zend_ast *child) {
293
4.09M
  zend_ast *ast;
294
4.09M
  zend_ast_list *list;
295
4.09M
  uint32_t lineno;
296
297
4.09M
  ast = zend_ast_alloc(zend_ast_list_size(4));
298
4.09M
  list = (zend_ast_list *) ast;
299
4.09M
  list->kind = kind;
300
4.09M
  list->attr = 0;
301
4.09M
  list->children = 1;
302
4.09M
  list->child[0] = child;
303
4.09M
  if (child) {
304
4.04M
    lineno = zend_ast_get_lineno(child);
305
4.04M
    if (lineno > CG(zend_lineno)) {
306
895
      lineno = CG(zend_lineno);
307
895
    }
308
52.7k
  } else {
309
52.7k
    lineno = CG(zend_lineno);
310
52.7k
  }
311
4.09M
  list->lineno = lineno;
312
313
4.09M
  return ast;
314
4.09M
}
315
316
172k
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_2(zend_ast_kind kind, zend_ast *child1, zend_ast *child2) {
317
172k
  zend_ast *ast;
318
172k
  zend_ast_list *list;
319
172k
  uint32_t lineno;
320
321
172k
  ast = zend_ast_alloc(zend_ast_list_size(4));
322
172k
  list = (zend_ast_list *) ast;
323
172k
  list->kind = kind;
324
172k
  list->attr = 0;
325
172k
  list->children = 2;
326
172k
  list->child[0] = child1;
327
172k
  list->child[1] = child2;
328
172k
  if (child1) {
329
172k
    lineno = zend_ast_get_lineno(child1);
330
172k
    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
172k
  list->lineno = lineno;
343
344
172k
  return ast;
345
172k
}
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.22M
static inline zend_bool is_power_of_two(uint32_t n) {
428
3.22M
  return ((n != 0) && (n == (n & (~n + 1))));
429
3.22M
}
430
431
9.48M
ZEND_API zend_ast * ZEND_FASTCALL zend_ast_list_add(zend_ast *ast, zend_ast *op) {
432
9.48M
  zend_ast_list *list = zend_ast_get_list(ast);
433
9.48M
  if (list->children >= 4 && is_power_of_two(list->children)) {
434
552k
      list = zend_ast_realloc(list,
435
552k
      zend_ast_list_size(list->children), zend_ast_list_size(list->children * 2));
436
552k
  }
437
9.48M
  list->child[list->children++] = op;
438
9.48M
  return (zend_ast *) list;
439
9.48M
}
440
441
static zend_result zend_ast_add_array_element(zval *result, zval *offset, zval *expr)
442
3.81k
{
443
3.81k
  switch (Z_TYPE_P(offset)) {
444
615
    case IS_UNDEF:
445
615
      if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), expr)) {
446
19
        zend_throw_error(NULL,
447
19
          "Cannot add element to the array as the next element is already occupied");
448
19
        return FAILURE;
449
19
      }
450
596
      break;
451
718
    case IS_STRING:
452
718
      zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(offset), expr);
453
718
      zval_ptr_dtor_str(offset);
454
718
      break;
455
133
    case IS_NULL:
456
133
      zend_symtable_update(Z_ARRVAL_P(result), ZSTR_EMPTY_ALLOC(), expr);
457
133
      break;
458
1.97k
    case IS_LONG:
459
1.97k
      zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(offset), expr);
460
1.97k
      break;
461
111
    case IS_FALSE:
462
111
      zend_hash_index_update(Z_ARRVAL_P(result), 0, expr);
463
111
      break;
464
88
    case IS_TRUE:
465
88
      zend_hash_index_update(Z_ARRVAL_P(result), 1, expr);
466
88
      break;
467
143
    case IS_DOUBLE:
468
143
      zend_hash_index_update(Z_ARRVAL_P(result), zend_dval_to_lval(Z_DVAL_P(offset)), expr);
469
143
      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
35
    default:
475
35
      zend_type_error("Illegal offset type");
476
35
      return FAILURE;
477
3.75k
  }
478
3.75k
  return SUCCESS;
479
3.75k
}
480
481
147
static zend_result zend_ast_add_unpacked_element(zval *result, zval *expr) {
482
147
  if (EXPECTED(Z_TYPE_P(expr) == IS_ARRAY)) {
483
146
    HashTable *ht = Z_ARRVAL_P(expr);
484
146
    zval *val;
485
146
    zend_string *key;
486
487
912
    ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
488
383
      if (key) {
489
0
        zend_throw_error(NULL, "Cannot unpack array with string keys");
490
0
        return FAILURE;
491
383
      } else {
492
383
        if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
493
51
          zend_throw_error(NULL,
494
51
            "Cannot add element to the array as the next element is already occupied");
495
51
          return FAILURE;
496
51
        }
497
332
        Z_TRY_ADDREF_P(val);
498
332
      }
499
383
    } ZEND_HASH_FOREACH_END();
500
95
    return SUCCESS;
501
1
  }
502
503
  /* Objects or references cannot occur in a constant expression. */
504
1
  zend_throw_error(NULL, "Only arrays and Traversables can be unpacked");
505
1
  return FAILURE;
506
1
}
507
508
ZEND_API zend_result ZEND_FASTCALL zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope)
509
43.0k
{
510
43.0k
  zval op1, op2;
511
43.0k
  zend_result ret = SUCCESS;
512
513
43.0k
  switch (ast->kind) {
514
2.33k
    case ZEND_AST_BINARY_OP:
515
2.33k
      if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
516
173
        ret = FAILURE;
517
2.16k
      } else if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) {
518
71
        zval_ptr_dtor_nogc(&op1);
519
71
        ret = FAILURE;
520
2.09k
      } else {
521
2.09k
        binary_op_type op = get_binary_op(ast->attr);
522
2.09k
        ret = op(result, &op1, &op2);
523
2.09k
        zval_ptr_dtor_nogc(&op1);
524
2.09k
        zval_ptr_dtor_nogc(&op2);
525
2.09k
      }
526
2.33k
      break;
527
395
    case ZEND_AST_GREATER:
528
732
    case ZEND_AST_GREATER_EQUAL:
529
732
      if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
530
47
        ret = FAILURE;
531
685
      } else if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) {
532
37
        zval_ptr_dtor_nogc(&op1);
533
37
        ret = FAILURE;
534
648
      } else {
535
        /* op1 > op2 is the same as op2 < op1 */
536
648
        binary_op_type op = ast->kind == ZEND_AST_GREATER
537
336
          ? is_smaller_function : is_smaller_or_equal_function;
538
648
        ret = op(result, &op2, &op1);
539
648
        zval_ptr_dtor_nogc(&op1);
540
648
        zval_ptr_dtor_nogc(&op2);
541
648
      }
542
732
      break;
543
311
    case ZEND_AST_UNARY_OP:
544
311
      if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
545
49
        ret = FAILURE;
546
262
      } else {
547
262
        unary_op_type op = get_unary_op(ast->attr);
548
262
        ret = op(result, &op1);
549
262
        zval_ptr_dtor_nogc(&op1);
550
262
      }
551
311
      break;
552
13.3k
    case ZEND_AST_ZVAL:
553
13.3k
    {
554
13.3k
      zval *zv = zend_ast_get_zval(ast);
555
556
13.3k
      ZVAL_COPY(result, zv);
557
13.3k
      break;
558
395
    }
559
13.0k
    case ZEND_AST_CONSTANT:
560
13.0k
    {
561
13.0k
      zend_string *name = zend_ast_get_constant_name(ast);
562
13.0k
      zval *zv = zend_get_constant_ex(name, scope, ast->attr);
563
564
13.0k
      if (UNEXPECTED(zv == NULL)) {
565
1.46k
        ZVAL_UNDEF(result);
566
1.46k
        return FAILURE;
567
1.46k
      }
568
11.6k
      ZVAL_COPY_OR_DUP(result, zv);
569
11.6k
      break;
570
11.6k
    }
571
247
    case ZEND_AST_CONSTANT_CLASS:
572
247
      if (scope) {
573
247
        ZVAL_STR_COPY(result, scope->name);
574
0
      } else {
575
0
        ZVAL_EMPTY_STRING(result);
576
0
      }
577
247
      break;
578
344
    case ZEND_AST_CLASS_NAME:
579
344
      if (!scope) {
580
23
        zend_throw_error(NULL, "Cannot use \"self\" when no class scope is active");
581
23
        return FAILURE;
582
23
      }
583
321
      if (ast->attr == ZEND_FETCH_CLASS_SELF) {
584
321
        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
321
      break;
596
457
    case ZEND_AST_AND:
597
457
      if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
598
61
        ret = FAILURE;
599
61
        break;
600
61
      }
601
396
      if (zend_is_true(&op1)) {
602
207
        if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) {
603
6
          zval_ptr_dtor_nogc(&op1);
604
6
          ret = FAILURE;
605
6
          break;
606
6
        }
607
201
        ZVAL_BOOL(result, zend_is_true(&op2));
608
201
        zval_ptr_dtor_nogc(&op2);
609
189
      } else {
610
189
        ZVAL_FALSE(result);
611
189
      }
612
390
      zval_ptr_dtor_nogc(&op1);
613
390
      break;
614
494
    case ZEND_AST_OR:
615
494
      if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
616
44
        ret = FAILURE;
617
44
        break;
618
44
      }
619
450
      if (zend_is_true(&op1)) {
620
214
        ZVAL_TRUE(result);
621
236
      } else {
622
236
        if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) {
623
21
          zval_ptr_dtor_nogc(&op1);
624
21
          ret = FAILURE;
625
21
          break;
626
21
        }
627
215
        ZVAL_BOOL(result, zend_is_true(&op2));
628
215
        zval_ptr_dtor_nogc(&op2);
629
215
      }
630
429
      zval_ptr_dtor_nogc(&op1);
631
429
      break;
632
737
    case ZEND_AST_CONDITIONAL:
633
737
      if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
634
41
        ret = FAILURE;
635
41
        break;
636
41
      }
637
696
      if (zend_is_true(&op1)) {
638
365
        if (!ast->child[1]) {
639
180
          *result = op1;
640
185
        } else {
641
185
          if (UNEXPECTED(zend_ast_evaluate(result, ast->child[1], scope) != SUCCESS)) {
642
1
            zval_ptr_dtor_nogc(&op1);
643
1
            ret = FAILURE;
644
1
            break;
645
1
          }
646
184
          zval_ptr_dtor_nogc(&op1);
647
184
        }
648
331
      } else {
649
331
        if (UNEXPECTED(zend_ast_evaluate(result, ast->child[2], scope) != SUCCESS)) {
650
0
          zval_ptr_dtor_nogc(&op1);
651
0
          ret = FAILURE;
652
0
          break;
653
0
        }
654
331
        zval_ptr_dtor_nogc(&op1);
655
331
      }
656
695
      break;
657
1.69k
    case ZEND_AST_COALESCE:
658
1.69k
      if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
659
152
        ret = FAILURE;
660
152
        break;
661
152
      }
662
1.54k
      if (Z_TYPE(op1) > IS_NULL) {
663
11
        *result = op1;
664
1.53k
      } else {
665
1.53k
        if (UNEXPECTED(zend_ast_evaluate(result, ast->child[1], scope) != SUCCESS)) {
666
4
          zval_ptr_dtor_nogc(&op1);
667
4
          ret = FAILURE;
668
4
          break;
669
4
        }
670
1.52k
        zval_ptr_dtor_nogc(&op1);
671
1.52k
      }
672
1.53k
      break;
673
578
    case ZEND_AST_UNARY_PLUS:
674
578
      if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[0], scope) != SUCCESS)) {
675
29
        ret = FAILURE;
676
549
      } else {
677
549
        ZVAL_LONG(&op1, 0);
678
549
        ret = add_function(result, &op1, &op2);
679
549
        zval_ptr_dtor_nogc(&op2);
680
549
      }
681
578
      break;
682
325
    case ZEND_AST_UNARY_MINUS:
683
325
      if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[0], scope) != SUCCESS)) {
684
32
        ret = FAILURE;
685
293
      } else {
686
293
        ZVAL_LONG(&op1, 0);
687
293
        ret = sub_function(result, &op1, &op2);
688
293
        zval_ptr_dtor_nogc(&op2);
689
293
      }
690
325
      break;
691
3.14k
    case ZEND_AST_ARRAY:
692
3.14k
      {
693
3.14k
        uint32_t i;
694
3.14k
        zend_ast_list *list = zend_ast_get_list(ast);
695
696
3.14k
        if (!list->children) {
697
0
          ZVAL_EMPTY_ARRAY(result);
698
0
          break;
699
0
        }
700
3.14k
        array_init(result);
701
6.99k
        for (i = 0; i < list->children; i++) {
702
4.77k
          zend_ast *elem = list->child[i];
703
4.77k
          if (elem->kind == ZEND_AST_UNPACK) {
704
280
            if (UNEXPECTED(zend_ast_evaluate(&op1, elem->child[0], scope) != SUCCESS)) {
705
133
              zval_ptr_dtor_nogc(result);
706
133
              return FAILURE;
707
133
            }
708
147
            if (UNEXPECTED(zend_ast_add_unpacked_element(result, &op1) != SUCCESS)) {
709
52
              zval_ptr_dtor_nogc(&op1);
710
52
              zval_ptr_dtor_nogc(result);
711
52
              return FAILURE;
712
52
            }
713
95
            zval_ptr_dtor_nogc(&op1);
714
95
            continue;
715
95
          }
716
4.49k
          if (elem->child[1]) {
717
3.59k
            if (UNEXPECTED(zend_ast_evaluate(&op1, elem->child[1], scope) != SUCCESS)) {
718
264
              zval_ptr_dtor_nogc(result);
719
264
              return FAILURE;
720
264
            }
721
899
          } else {
722
899
            ZVAL_UNDEF(&op1);
723
899
          }
724
4.23k
          if (UNEXPECTED(zend_ast_evaluate(&op2, elem->child[0], scope) != SUCCESS)) {
725
419
            zval_ptr_dtor_nogc(&op1);
726
419
            zval_ptr_dtor_nogc(result);
727
419
            return FAILURE;
728
419
          }
729
3.81k
          if (UNEXPECTED(zend_ast_add_array_element(result, &op1, &op2) != SUCCESS)) {
730
54
            zval_ptr_dtor_nogc(&op1);
731
54
            zval_ptr_dtor_nogc(&op2);
732
54
            zval_ptr_dtor_nogc(result);
733
54
            return FAILURE;
734
54
          }
735
3.81k
        }
736
3.14k
      }
737
2.22k
      break;
738
5.27k
    case ZEND_AST_DIM:
739
5.27k
      if (ast->child[1] == NULL) {
740
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
741
0
      }
742
743
5.27k
      if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
744
238
        ret = FAILURE;
745
5.03k
      } else if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) {
746
37
        zval_ptr_dtor_nogc(&op1);
747
37
        ret = FAILURE;
748
4.99k
      } else {
749
4.99k
        zend_fetch_dimension_const(result, &op1, &op2, (ast->attr & ZEND_DIM_IS) ? BP_VAR_IS : BP_VAR_R);
750
751
4.99k
        zval_ptr_dtor_nogc(&op1);
752
4.99k
        zval_ptr_dtor_nogc(&op2);
753
4.99k
        if (UNEXPECTED(EG(exception))) {
754
19
          return FAILURE;
755
19
        }
756
5.25k
      }
757
5.25k
      break;
758
0
    default:
759
0
      zend_throw_error(NULL, "Unsupported constant expression");
760
0
      ret = FAILURE;
761
43.0k
  }
762
40.6k
  return ret;
763
43.0k
}
764
765
static size_t ZEND_FASTCALL zend_ast_tree_size(zend_ast *ast)
766
102k
{
767
102k
  size_t size;
768
769
102k
  if (ast->kind == ZEND_AST_ZVAL || ast->kind == ZEND_AST_CONSTANT) {
770
62.1k
    size = sizeof(zend_ast_zval);
771
40.4k
  } else if (zend_ast_is_list(ast)) {
772
6.30k
    uint32_t i;
773
6.30k
    zend_ast_list *list = zend_ast_get_list(ast);
774
775
6.30k
    size = zend_ast_list_size(list->children);
776
16.1k
    for (i = 0; i < list->children; i++) {
777
9.89k
      if (list->child[i]) {
778
9.89k
        size += zend_ast_tree_size(list->child[i]);
779
9.89k
      }
780
9.89k
    }
781
34.1k
  } else {
782
34.1k
    uint32_t i, children = zend_ast_get_num_children(ast);
783
784
34.1k
    size = zend_ast_size(children);
785
101k
    for (i = 0; i < children; i++) {
786
66.9k
      if (ast->child[i]) {
787
64.0k
        size += zend_ast_tree_size(ast->child[i]);
788
64.0k
      }
789
66.9k
    }
790
34.1k
  }
791
102k
  return size;
792
102k
}
793
794
static void* ZEND_FASTCALL zend_ast_tree_copy(zend_ast *ast, void *buf)
795
102k
{
796
102k
  if (ast->kind == ZEND_AST_ZVAL) {
797
30.4k
    zend_ast_zval *new = (zend_ast_zval*)buf;
798
30.4k
    new->kind = ZEND_AST_ZVAL;
799
30.4k
    new->attr = ast->attr;
800
30.4k
    ZVAL_COPY(&new->val, zend_ast_get_zval(ast));
801
30.4k
    buf = (void*)((char*)buf + sizeof(zend_ast_zval));
802
72.1k
  } else if (ast->kind == ZEND_AST_CONSTANT) {
803
31.6k
    zend_ast_zval *new = (zend_ast_zval*)buf;
804
31.6k
    new->kind = ZEND_AST_CONSTANT;
805
31.6k
    new->attr = ast->attr;
806
31.6k
    ZVAL_STR_COPY(&new->val, zend_ast_get_constant_name(ast));
807
31.6k
    buf = (void*)((char*)buf + sizeof(zend_ast_zval));
808
40.4k
  } else if (zend_ast_is_list(ast)) {
809
6.30k
    zend_ast_list *list = zend_ast_get_list(ast);
810
6.30k
    zend_ast_list *new = (zend_ast_list*)buf;
811
6.30k
    uint32_t i;
812
6.30k
    new->kind = list->kind;
813
6.30k
    new->attr = list->attr;
814
6.30k
    new->children = list->children;
815
6.30k
    buf = (void*)((char*)buf + zend_ast_list_size(list->children));
816
16.1k
    for (i = 0; i < list->children; i++) {
817
9.89k
      if (list->child[i]) {
818
9.89k
        new->child[i] = (zend_ast*)buf;
819
9.89k
        buf = zend_ast_tree_copy(list->child[i], buf);
820
0
      } else {
821
0
        new->child[i] = NULL;
822
0
      }
823
9.89k
    }
824
34.1k
  } else {
825
34.1k
    uint32_t i, children = zend_ast_get_num_children(ast);
826
34.1k
    zend_ast *new = (zend_ast*)buf;
827
34.1k
    new->kind = ast->kind;
828
34.1k
    new->attr = ast->attr;
829
34.1k
    buf = (void*)((char*)buf + zend_ast_size(children));
830
101k
    for (i = 0; i < children; i++) {
831
66.9k
      if (ast->child[i]) {
832
64.0k
        new->child[i] = (zend_ast*)buf;
833
64.0k
        buf = zend_ast_tree_copy(ast->child[i], buf);
834
2.89k
      } else {
835
2.89k
        new->child[i] = NULL;
836
2.89k
      }
837
66.9k
    }
838
34.1k
  }
839
102k
  return buf;
840
102k
}
841
842
ZEND_API zend_ast_ref * ZEND_FASTCALL zend_ast_copy(zend_ast *ast)
843
28.6k
{
844
28.6k
  size_t tree_size;
845
28.6k
  zend_ast_ref *ref;
846
847
28.6k
  ZEND_ASSERT(ast != NULL);
848
28.6k
  tree_size = zend_ast_tree_size(ast) + sizeof(zend_ast_ref);
849
28.6k
  ref = emalloc(tree_size);
850
28.6k
  zend_ast_tree_copy(ast, GC_AST(ref));
851
28.6k
  GC_SET_REFCOUNT(ref, 1);
852
28.6k
  GC_TYPE_INFO(ref) = GC_CONSTANT_AST;
853
28.6k
  return ref;
854
28.6k
}
855
856
ZEND_API void ZEND_FASTCALL zend_ast_destroy(zend_ast *ast)
857
21.9M
{
858
44.8M
tail_call:
859
44.8M
  if (!ast) {
860
5.16M
    return;
861
5.16M
  }
862
863
39.6M
  if (EXPECTED(ast->kind >= ZEND_AST_VAR)) {
864
15.7M
    uint32_t i, children = zend_ast_get_num_children(ast);
865
866
27.0M
    for (i = 1; i < children; i++) {
867
11.2M
      zend_ast_destroy(ast->child[i]);
868
11.2M
    }
869
15.7M
    ast = ast->child[0];
870
15.7M
    goto tail_call;
871
23.9M
  } else if (EXPECTED(ast->kind == ZEND_AST_ZVAL)) {
872
15.0M
    zval_ptr_dtor_nogc(zend_ast_get_zval(ast));
873
8.90M
  } else if (EXPECTED(zend_ast_is_list(ast))) {
874
8.09M
    zend_ast_list *list = zend_ast_get_list(ast);
875
8.09M
    if (list->children) {
876
6.59M
      uint32_t i;
877
878
13.6M
      for (i = 1; i < list->children; i++) {
879
7.01M
        zend_ast_destroy(list->child[i]);
880
7.01M
      }
881
6.59M
      ast = list->child[0];
882
6.59M
      goto tail_call;
883
6.59M
    }
884
810k
  } else if (EXPECTED(ast->kind == ZEND_AST_CONSTANT)) {
885
62.2k
    zend_string_release_ex(zend_ast_get_constant_name(ast), 0);
886
747k
  } else if (EXPECTED(ast->kind >= ZEND_AST_FUNC_DECL)) {
887
530k
    zend_ast_decl *decl = (zend_ast_decl *) ast;
888
889
530k
    if (decl->name) {
890
499k
        zend_string_release_ex(decl->name, 0);
891
499k
    }
892
530k
    if (decl->doc_comment) {
893
863
      zend_string_release_ex(decl->doc_comment, 0);
894
863
    }
895
530k
    zend_ast_destroy(decl->child[0]);
896
530k
    zend_ast_destroy(decl->child[1]);
897
530k
    zend_ast_destroy(decl->child[2]);
898
530k
    zend_ast_destroy(decl->child[3]);
899
530k
    ast = decl->child[4];
900
530k
    goto tail_call;
901
530k
  }
902
39.6M
}
903
904
ZEND_API void ZEND_FASTCALL zend_ast_ref_destroy(zend_ast_ref *ast)
905
27.7k
{
906
27.7k
  zend_ast_destroy(GC_AST(ast));
907
27.7k
  efree(ast);
908
27.7k
}
909
910
39.7k
ZEND_API void zend_ast_apply(zend_ast *ast, zend_ast_apply_func fn) {
911
39.7k
  if (zend_ast_is_list(ast)) {
912
6.14k
    zend_ast_list *list = zend_ast_get_list(ast);
913
6.14k
    uint32_t i;
914
15.9k
    for (i = 0; i < list->children; ++i) {
915
9.76k
      fn(&list->child[i]);
916
9.76k
    }
917
33.5k
  } else {
918
33.5k
    uint32_t i, children = zend_ast_get_num_children(ast);
919
100k
    for (i = 0; i < children; ++i) {
920
66.5k
      fn(&ast->child[i]);
921
66.5k
    }
922
33.5k
  }
923
39.7k
}
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.8k
{
965
14.8k
  size_t i;
966
967
522k
  for (i = 0; i < ZSTR_LEN(s); i++) {
968
507k
    unsigned char c = ZSTR_VAL(s)[i];
969
507k
    if (c == '\'' || c == '\\') {
970
94.3k
      smart_str_appendc(str, '\\');
971
94.3k
      smart_str_appendc(str, c);
972
413k
    } else {
973
413k
      smart_str_appendc(str, c);
974
413k
    }
975
507k
  }
976
14.8k
}
977
978
static ZEND_COLD void zend_ast_export_qstr(smart_str *str, char quote, zend_string *s)
979
26.3k
{
980
26.3k
  size_t i;
981
982
1.03M
  for (i = 0; i < ZSTR_LEN(s); i++) {
983
1.01M
    unsigned char c = ZSTR_VAL(s)[i];
984
1.01M
    if (c < ' ') {
985
336k
      switch (c) {
986
249k
        case '\n':
987
249k
          smart_str_appends(str, "\\n");
988
249k
          break;
989
9.58k
        case '\r':
990
9.58k
          smart_str_appends(str, "\\r");
991
9.58k
          break;
992
9.69k
        case '\t':
993
9.69k
          smart_str_appends(str, "\\t");
994
9.69k
          break;
995
7.74k
        case '\f':
996
7.74k
          smart_str_appends(str, "\\f");
997
7.74k
          break;
998
8.69k
        case '\v':
999
8.69k
          smart_str_appends(str, "\\v");
1000
8.69k
          break;
1001
#ifdef ZEND_WIN32
1002
        case VK_ESCAPE:
1003
#else
1004
5.82k
        case '\e':
1005
5.82k
#endif
1006
5.82k
          smart_str_appends(str, "\\e");
1007
5.82k
          break;
1008
45.7k
        default:
1009
45.7k
          smart_str_appends(str, "\\0");
1010
45.7k
          smart_str_appendc(str, '0' + (c / 8));
1011
45.7k
          smart_str_appendc(str, '0' + (c % 8));
1012
45.7k
          break;
1013
676k
      }
1014
676k
    } else {
1015
676k
      if (c == quote || c == '$' || c == '\\') {
1016
85.3k
        smart_str_appendc(str, '\\');
1017
85.3k
      }
1018
676k
      smart_str_appendc(str, c);
1019
676k
    }
1020
1.01M
  }
1021
26.3k
}
1022
1023
static ZEND_COLD void zend_ast_export_indent(smart_str *str, int indent)
1024
63.6k
{
1025
213k
  while (indent > 0) {
1026
149k
    smart_str_appends(str, "    ");
1027
149k
    indent--;
1028
149k
  }
1029
63.6k
}
1030
1031
static ZEND_COLD void zend_ast_export_name(smart_str *str, zend_ast *ast, int priority, int indent)
1032
27.4k
{
1033
27.4k
  if (ast->kind == ZEND_AST_ZVAL) {
1034
21.4k
    zval *zv = zend_ast_get_zval(ast);
1035
1036
21.4k
    if (Z_TYPE_P(zv) == IS_STRING) {
1037
21.4k
      smart_str_append(str, Z_STR_P(zv));
1038
21.4k
      return;
1039
21.4k
    }
1040
5.97k
  }
1041
5.97k
  zend_ast_export_ex(str, ast, priority, indent);
1042
5.97k
}
1043
1044
static ZEND_COLD void zend_ast_export_ns_name(smart_str *str, zend_ast *ast, int priority, int indent)
1045
35.5k
{
1046
35.5k
  if (ast->kind == ZEND_AST_ZVAL) {
1047
31.5k
    zval *zv = zend_ast_get_zval(ast);
1048
1049
31.5k
    if (Z_TYPE_P(zv) == IS_STRING) {
1050
31.5k
        if (ast->attr == ZEND_NAME_FQ) {
1051
6.62k
        smart_str_appendc(str, '\\');
1052
24.9k
        } else if (ast->attr == ZEND_NAME_RELATIVE) {
1053
1.25k
        smart_str_appends(str, "namespace\\");
1054
1.25k
        }
1055
31.5k
      smart_str_append(str, Z_STR_P(zv));
1056
31.5k
      return;
1057
31.5k
    }
1058
4.05k
  }
1059
4.05k
  zend_ast_export_ex(str, ast, priority, indent);
1060
4.05k
}
1061
1062
static ZEND_COLD bool zend_ast_valid_var_char(char ch)
1063
13.8k
{
1064
13.8k
  unsigned char c = (unsigned char)ch;
1065
1066
13.8k
  if (c != '_' && c < 127 &&
1067
13.7k
      (c < '0' || c > '9') &&
1068
13.7k
      (c < 'A' || c > 'Z') &&
1069
13.7k
      (c < 'a' || c > 'z')) {
1070
12.8k
    return 0;
1071
12.8k
  }
1072
1.00k
  return 1;
1073
1.00k
}
1074
1075
static ZEND_COLD bool zend_ast_valid_var_name(const char *s, size_t len)
1076
82.3k
{
1077
82.3k
  unsigned char c;
1078
82.3k
  size_t i;
1079
1080
82.3k
  if (len == 0) {
1081
0
    return 0;
1082
0
  }
1083
82.3k
  c = (unsigned char)s[0];
1084
82.3k
  if (c != '_' && c < 127 &&
1085
80.0k
      (c < 'A' || c > 'Z') &&
1086
79.7k
      (c < 'a' || c > 'z')) {
1087
423
    return 0;
1088
423
  }
1089
247k
  for (i = 1; i < len; i++) {
1090
165k
    c = (unsigned char)s[i];
1091
165k
    if (c != '_' && c < 127 &&
1092
108k
        (c < '0' || c > '9') &&
1093
108k
        (c < 'A' || c > 'Z') &&
1094
103k
        (c < 'a' || c > 'z')) {
1095
1
      return 0;
1096
1
    }
1097
165k
  }
1098
81.9k
  return 1;
1099
81.9k
}
1100
1101
static ZEND_COLD bool zend_ast_var_needs_braces(char ch)
1102
14.7k
{
1103
14.7k
  return ch == '[' || zend_ast_valid_var_char(ch);
1104
14.7k
}
1105
1106
static ZEND_COLD void zend_ast_export_var(smart_str *str, zend_ast *ast, int priority, int indent)
1107
88.2k
{
1108
88.2k
  if (ast->kind == ZEND_AST_ZVAL) {
1109
82.3k
    zval *zv = zend_ast_get_zval(ast);
1110
82.3k
    if (Z_TYPE_P(zv) == IS_STRING &&
1111
82.3k
        zend_ast_valid_var_name(Z_STRVAL_P(zv), Z_STRLEN_P(zv))) {
1112
81.9k
      smart_str_append(str, Z_STR_P(zv));
1113
81.9k
      return;
1114
81.9k
    }
1115
5.88k
  } else if (ast->kind == ZEND_AST_VAR) {
1116
1.12k
    zend_ast_export_ex(str, ast, 0, indent);
1117
1.12k
    return;
1118
1.12k
  }
1119
5.18k
  smart_str_appendc(str, '{');
1120
5.18k
  zend_ast_export_name(str, ast, 0, indent);
1121
5.18k
  smart_str_appendc(str, '}');
1122
5.18k
}
1123
1124
static ZEND_COLD void zend_ast_export_list(smart_str *str, zend_ast_list *list, bool separator, int priority, int indent)
1125
31.4k
{
1126
31.4k
  uint32_t i = 0;
1127
1128
68.6k
  while (i < list->children) {
1129
37.1k
    if (i != 0 && separator) {
1130
13.3k
      smart_str_appends(str, ", ");
1131
13.3k
    }
1132
37.1k
    zend_ast_export_ex(str, list->child[i], priority, indent);
1133
37.1k
    i++;
1134
37.1k
  }
1135
31.4k
}
1136
1137
static ZEND_COLD void zend_ast_export_encaps_list(smart_str *str, char quote, zend_ast_list *list, int indent)
1138
9.23k
{
1139
9.23k
  uint32_t i = 0;
1140
9.23k
  zend_ast *ast;
1141
1142
55.3k
  while (i < list->children) {
1143
46.1k
    ast = list->child[i];
1144
46.1k
    if (ast->kind == ZEND_AST_ZVAL) {
1145
26.0k
      zval *zv = zend_ast_get_zval(ast);
1146
1147
26.0k
      ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
1148
26.0k
      zend_ast_export_qstr(str, quote, Z_STR_P(zv));
1149
20.0k
    } else if (ast->kind == ZEND_AST_VAR &&
1150
16.5k
               ast->child[0]->kind == ZEND_AST_ZVAL &&
1151
16.0k
               (i + 1 == list->children ||
1152
14.9k
                list->child[i + 1]->kind != ZEND_AST_ZVAL ||
1153
14.7k
                !zend_ast_var_needs_braces(
1154
14.7k
                    *Z_STRVAL_P(
1155
14.1k
                        zend_ast_get_zval(list->child[i + 1]))))) {
1156
14.1k
      zend_ast_export_ex(str, ast, 0, indent);
1157
5.87k
    } else {
1158
5.87k
      smart_str_appendc(str, '{');
1159
5.87k
      zend_ast_export_ex(str, ast, 0, indent);
1160
5.87k
      smart_str_appendc(str, '}');
1161
5.87k
    }
1162
46.1k
    i++;
1163
46.1k
  }
1164
9.23k
}
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.52k
{
1168
2.52k
  uint32_t i = 0;
1169
1170
5.89k
  while (i < list->children) {
1171
3.37k
    if (i != 0) {
1172
849
      smart_str_appends(str, separator);
1173
849
    }
1174
3.37k
    zend_ast_export_name(str, list->child[i], 0, indent);
1175
3.37k
    i++;
1176
3.37k
  }
1177
2.52k
}
1178
1179
1.67k
#define zend_ast_export_name_list(s, l, i) zend_ast_export_name_list_ex(s, l, i, ", ")
1180
845
#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.28k
{
1184
1.28k
  uint32_t i = 0;
1185
1186
3.86k
  while (i < list->children) {
1187
2.57k
    if (i != 0) {
1188
1.28k
      smart_str_appends(str, ", ");
1189
1.28k
    }
1190
2.57k
    if (list->child[i]->attr & ZEND_BIND_REF) {
1191
1.28k
      smart_str_appendc(str, '&');
1192
1.28k
    }
1193
2.57k
    smart_str_appendc(str, '$');
1194
2.57k
    zend_ast_export_name(str, list->child[i], 20, indent);
1195
2.57k
    i++;
1196
2.57k
  }
1197
1.28k
}
1198
1199
static ZEND_COLD void zend_ast_export_stmt(smart_str *str, zend_ast *ast, int indent)
1200
65.2k
{
1201
65.2k
  if (!ast) {
1202
979
    return;
1203
979
  }
1204
1205
64.2k
  if (ast->kind == ZEND_AST_STMT_LIST ||
1206
44.7k
      ast->kind == ZEND_AST_TRAIT_ADAPTATIONS) {
1207
19.9k
    zend_ast_list *list = (zend_ast_list*)ast;
1208
19.9k
    uint32_t i = 0;
1209
1210
67.2k
    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
459
      case ZEND_AST_LABEL:
1220
2.64k
      case ZEND_AST_IF:
1221
3.08k
      case ZEND_AST_SWITCH:
1222
3.51k
      case ZEND_AST_WHILE:
1223
3.94k
      case ZEND_AST_TRY:
1224
4.37k
      case ZEND_AST_FOR:
1225
4.80k
      case ZEND_AST_FOREACH:
1226
4.80k
      case ZEND_AST_FUNC_DECL:
1227
6.20k
      case ZEND_AST_METHOD:
1228
7.82k
      case ZEND_AST_CLASS:
1229
8.65k
      case ZEND_AST_USE_TRAIT:
1230
8.65k
      case ZEND_AST_NAMESPACE:
1231
9.51k
      case ZEND_AST_DECLARE:
1232
9.51k
        break;
1233
34.7k
      default:
1234
34.7k
        smart_str_appendc(str, ';');
1235
34.7k
        break;
1236
44.2k
    }
1237
44.2k
    smart_str_appendc(str, '\n');
1238
44.2k
  }
1239
64.2k
}
1240
1241
static ZEND_COLD void zend_ast_export_if_stmt(smart_str *str, zend_ast_list *list, int indent)
1242
2.18k
{
1243
2.18k
  uint32_t i;
1244
2.18k
  zend_ast *ast;
1245
1246
3.06k
tail_call:
1247
3.06k
  i = 0;
1248
7.43k
  while (i < list->children) {
1249
5.24k
    ast = list->child[i];
1250
5.24k
    ZEND_ASSERT(ast->kind == ZEND_AST_IF_ELEM);
1251
5.24k
    if (ast->child[0]) {
1252
3.49k
      if (i == 0) {
1253
3.06k
        smart_str_appends(str, "if (");
1254
434
      } else {
1255
434
        zend_ast_export_indent(str, indent);
1256
434
        smart_str_appends(str, "} elseif (");
1257
434
      }
1258
3.49k
      zend_ast_export_ex(str, ast->child[0], 0, indent);
1259
3.49k
      smart_str_appends(str, ") {\n");
1260
3.49k
      zend_ast_export_stmt(str, ast->child[1], indent + 1);
1261
1.74k
    } else {
1262
1.74k
      zend_ast_export_indent(str, indent);
1263
1.74k
      smart_str_appends(str, "} else ");
1264
1.74k
      if (ast->child[1] && ast->child[1]->kind == ZEND_AST_IF) {
1265
876
        list = (zend_ast_list*)ast->child[1];
1266
876
        goto tail_call;
1267
869
      } else {
1268
869
        smart_str_appends(str, "{\n");
1269
869
        zend_ast_export_stmt(str, ast->child[1], indent + 1);
1270
869
      }
1271
1.74k
    }
1272
4.36k
    i++;
1273
4.36k
  }
1274
2.18k
  zend_ast_export_indent(str, indent);
1275
2.18k
  smart_str_appendc(str, '}');
1276
2.18k
}
1277
1278
static ZEND_COLD void zend_ast_export_zval(smart_str *str, zval *zv, int priority, int indent)
1279
43.7k
{
1280
43.7k
  zend_long idx;
1281
43.7k
  zend_string *key;
1282
43.7k
  zval *val;
1283
43.7k
  int first;
1284
1285
43.7k
  ZVAL_DEREF(zv);
1286
43.7k
  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
28.0k
    case IS_LONG:
1297
28.0k
      smart_str_append_long(str, Z_LVAL_P(zv));
1298
28.0k
      break;
1299
868
    case IS_DOUBLE:
1300
868
      key = zend_strpprintf(0, "%.*G", (int) EG(precision), Z_DVAL_P(zv));
1301
868
      smart_str_appendl(str, ZSTR_VAL(key), ZSTR_LEN(key));
1302
868
      zend_string_release_ex(key, 0);
1303
868
      break;
1304
14.8k
    case IS_STRING:
1305
14.8k
      smart_str_appendc(str, '\'');
1306
14.8k
      zend_ast_export_str(str, Z_STR_P(zv));
1307
14.8k
      smart_str_appendc(str, '\'');
1308
14.8k
      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
43.7k
  }
1335
43.7k
}
1336
1337
1.80k
static ZEND_COLD void zend_ast_export_class_no_header(smart_str *str, zend_ast_decl *decl, int indent) {
1338
1.80k
  if (decl->child[0]) {
1339
453
    smart_str_appends(str, " extends ");
1340
453
    zend_ast_export_ns_name(str, decl->child[0], 0, indent);
1341
453
  }
1342
1.80k
  if (decl->child[1]) {
1343
434
    smart_str_appends(str, " implements ");
1344
434
    zend_ast_export_ex(str, decl->child[1], 0, indent);
1345
434
  }
1346
1.80k
  smart_str_appends(str, " {\n");
1347
1.80k
  zend_ast_export_stmt(str, decl->child[2], indent + 1);
1348
1.80k
  zend_ast_export_indent(str, indent);
1349
1.80k
  smart_str_appends(str, "}");
1350
1.80k
}
1351
1352
987
static ZEND_COLD void zend_ast_export_attributes(smart_str *str, zend_ast *ast, int indent, zend_bool newlines) {
1353
987
  zend_ast_list *list = zend_ast_get_list(ast);
1354
987
  uint32_t i;
1355
1356
2.15k
  for (i = 0; i < list->children; i++) {
1357
1.16k
    zend_ast *attr = list->child[i];
1358
1359
1.16k
    smart_str_appends(str, "@@");
1360
1.16k
    zend_ast_export_ns_name(str, attr->child[0], 0, indent);
1361
1362
1.16k
    if (attr->child[1]) {
1363
185
      zend_ast_list *args = zend_ast_get_list(attr->child[1]);
1364
185
      uint32_t j;
1365
1366
185
      smart_str_appendc(str, '(');
1367
582
      for (j = 0; j < args->children; j++) {
1368
397
        if (j) {
1369
212
          smart_str_appends(str, ", ");
1370
212
        }
1371
397
        zend_ast_export_ex(str, args->child[j], 0, indent);
1372
397
      }
1373
185
      smart_str_appendc(str, ')');
1374
185
    }
1375
1376
1.16k
    if (newlines) {
1377
726
      smart_str_appendc(str, '\n');
1378
726
      zend_ast_export_indent(str, indent);
1379
437
    } else {
1380
437
      smart_str_appendc(str, ' ');
1381
437
    }
1382
1.16k
  }
1383
987
}
1384
1385
7.60k
static ZEND_COLD void zend_ast_export_visibility(smart_str *str, uint32_t flags) {
1386
7.60k
  if (flags & ZEND_ACC_PUBLIC) {
1387
2.15k
    smart_str_appends(str, "public ");
1388
5.44k
  } else if (flags & ZEND_ACC_PROTECTED) {
1389
871
    smart_str_appends(str, "protected ");
1390
4.57k
  } else if (flags & ZEND_ACC_PRIVATE) {
1391
825
    smart_str_appends(str, "private ");
1392
825
  }
1393
7.60k
}
1394
1395
6.43k
static ZEND_COLD void zend_ast_export_type(smart_str *str, zend_ast *ast, int indent) {
1396
6.43k
  if (ast->kind == ZEND_AST_TYPE_UNION) {
1397
438
    zend_ast_list *list = zend_ast_get_list(ast);
1398
1.31k
    for (uint32_t i = 0; i < list->children; i++) {
1399
876
      if (i != 0) {
1400
438
        smart_str_appendc(str, '|');
1401
438
      }
1402
876
      zend_ast_export_type(str, list->child[i], indent);
1403
876
    }
1404
438
    return;
1405
438
  }
1406
5.99k
  if (ast->attr & ZEND_TYPE_NULLABLE) {
1407
1.88k
    smart_str_appendc(str, '?');
1408
1.88k
  }
1409
5.99k
  zend_ast_export_ns_name(str, ast, 0, indent);
1410
5.99k
}
1411
1412
42.9k
#define BINARY_OP(_op, _p, _pl, _pr) do { \
1413
42.9k
    op = _op; \
1414
42.9k
    p = _p; \
1415
42.9k
    pl = _pl; \
1416
42.9k
    pr = _pr; \
1417
42.9k
    goto binary_op; \
1418
0
  } while (0)
1419
1420
6.57k
#define PREFIX_OP(_op, _p, _pl) do { \
1421
6.57k
    op = _op; \
1422
6.57k
    p = _p; \
1423
6.57k
    pl = _pl; \
1424
6.57k
    goto prefix_op; \
1425
0
  } while (0)
1426
1427
1.55k
#define FUNC_OP(_op) do { \
1428
1.55k
    op = _op; \
1429
1.55k
    goto func_op; \
1430
0
  } while (0)
1431
1432
1.70k
#define POSTFIX_OP(_op, _p, _pl) do { \
1433
1.70k
    op = _op; \
1434
1.70k
    p = _p; \
1435
1.70k
    pl = _pl; \
1436
1.70k
    goto postfix_op; \
1437
0
  } while (0)
1438
1439
6.78k
#define APPEND_NODE_1(_op) do { \
1440
6.78k
    op = _op; \
1441
6.78k
    goto append_node_1; \
1442
0
  } while (0)
1443
1444
2.09k
#define APPEND_STR(_op) do { \
1445
2.09k
    op = _op; \
1446
2.09k
    goto append_str; \
1447
0
  } while (0)
1448
1449
9.21k
#define APPEND_DEFAULT_VALUE(n) do { \
1450
9.21k
    p = n; \
1451
9.21k
    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
295k
{
1456
295k
  zend_ast_decl *decl;
1457
295k
  int p, pl, pr;
1458
295k
  const char *op;
1459
1460
307k
tail_call:
1461
307k
  if (!ast) {
1462
3.83k
    return;
1463
3.83k
  }
1464
304k
  switch (ast->kind) {
1465
    /* special nodes */
1466
43.7k
    case ZEND_AST_ZVAL:
1467
43.7k
      zend_ast_export_zval(str, zend_ast_get_zval(ast), priority, indent);
1468
43.7k
      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.53k
    case ZEND_AST_CLOSURE:
1485
3.71k
    case ZEND_AST_ARROW_FUNC:
1486
5.11k
    case ZEND_AST_METHOD:
1487
5.11k
      decl = (zend_ast_decl *) ast;
1488
5.11k
      if (decl->child[4]) {
1489
286
        zend_bool newlines = !(ast->kind == ZEND_AST_CLOSURE || ast->kind == ZEND_AST_ARROW_FUNC);
1490
286
        zend_ast_export_attributes(str, decl->child[4], indent, newlines);
1491
286
      }
1492
1493
5.11k
      zend_ast_export_visibility(str, decl->flags);
1494
1495
5.11k
      if (decl->flags & ZEND_ACC_STATIC) {
1496
436
        smart_str_appends(str, "static ");
1497
436
      }
1498
5.11k
      if (decl->flags & ZEND_ACC_ABSTRACT) {
1499
433
        smart_str_appends(str, "abstract ");
1500
433
      }
1501
5.11k
      if (decl->flags & ZEND_ACC_FINAL) {
1502
440
        smart_str_appends(str, "final ");
1503
440
      }
1504
5.11k
      if (decl->kind == ZEND_AST_ARROW_FUNC) {
1505
181
        smart_str_appends(str, "fn");
1506
4.93k
      } else {
1507
4.93k
        smart_str_appends(str, "function ");
1508
4.93k
      }
1509
5.11k
      if (decl->flags & ZEND_ACC_RETURN_REFERENCE) {
1510
1.76k
        smart_str_appendc(str, '&');
1511
1.76k
      }
1512
5.11k
      if (ast->kind != ZEND_AST_CLOSURE && ast->kind != ZEND_AST_ARROW_FUNC) {
1513
1.40k
        smart_str_appendl(str, ZSTR_VAL(decl->name), ZSTR_LEN(decl->name));
1514
1.40k
      }
1515
5.11k
      smart_str_appendc(str, '(');
1516
5.11k
      zend_ast_export_ex(str, decl->child[0], 0, indent);
1517
5.11k
      smart_str_appendc(str, ')');
1518
5.11k
      zend_ast_export_ex(str, decl->child[1], 0, indent);
1519
5.11k
      if (decl->child[3]) {
1520
1.82k
        smart_str_appends(str, ": ");
1521
1.82k
        zend_ast_export_type(str, decl->child[3], indent);
1522
1.82k
      }
1523
5.11k
      if (decl->child[2]) {
1524
4.68k
        if (decl->kind == ZEND_AST_ARROW_FUNC) {
1525
181
          ZEND_ASSERT(decl->child[2]->kind == ZEND_AST_RETURN);
1526
181
          smart_str_appends(str, " => ");
1527
181
          zend_ast_export_ex(str, decl->child[2]->child[0], 0, indent);
1528
181
          break;
1529
181
        }
1530
1531
4.50k
        smart_str_appends(str, " {\n");
1532
4.50k
        zend_ast_export_stmt(str, decl->child[2], indent + 1);
1533
4.50k
        zend_ast_export_indent(str, indent);
1534
4.50k
        smart_str_appendc(str, '}');
1535
4.50k
        if (ast->kind != ZEND_AST_CLOSURE) {
1536
974
          smart_str_appendc(str, '\n');
1537
974
        }
1538
431
      } else {
1539
431
        smart_str_appends(str, ";\n");
1540
431
      }
1541
4.93k
      break;
1542
1.61k
    case ZEND_AST_CLASS:
1543
1.61k
      decl = (zend_ast_decl *) ast;
1544
1.61k
      if (decl->child[4]) {
1545
327
        zend_ast_export_attributes(str, decl->child[4], indent, 1);
1546
327
      }
1547
1.61k
      if (decl->flags & ZEND_ACC_INTERFACE) {
1548
107
        smart_str_appends(str, "interface ");
1549
1.51k
      } else if (decl->flags & ZEND_ACC_TRAIT) {
1550
112
        smart_str_appends(str, "trait ");
1551
1.39k
      } else {
1552
1.39k
        if (decl->flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
1553
433
          smart_str_appends(str, "abstract ");
1554
433
        }
1555
1.39k
        if (decl->flags & ZEND_ACC_FINAL) {
1556
440
          smart_str_appends(str, "final ");
1557
440
        }
1558
1.39k
        smart_str_appends(str, "class ");
1559
1.39k
      }
1560
1.61k
      smart_str_appendl(str, ZSTR_VAL(decl->name), ZSTR_LEN(decl->name));
1561
1.61k
      zend_ast_export_class_no_header(str, decl, indent);
1562
1.61k
      smart_str_appendc(str, '\n');
1563
1.61k
      break;
1564
1565
    /* list nodes */
1566
19.2k
    case ZEND_AST_ARG_LIST:
1567
20.5k
    case ZEND_AST_EXPR_LIST:
1568
25.6k
    case ZEND_AST_PARAM_LIST:
1569
28.1k
simple_list:
1570
28.1k
      zend_ast_export_list(str, (zend_ast_list*)ast, 1, 20, indent);
1571
28.1k
      break;
1572
1.32k
    case ZEND_AST_ARRAY:
1573
1.32k
      smart_str_appendc(str, '[');
1574
1.32k
      zend_ast_export_list(str, (zend_ast_list*)ast, 1, 20, indent);
1575
1.32k
      smart_str_appendc(str, ']');
1576
1.32k
      break;
1577
8.80k
    case ZEND_AST_ENCAPS_LIST:
1578
8.80k
      smart_str_appendc(str, '"');
1579
8.80k
      zend_ast_export_encaps_list(str, '"', (zend_ast_list*)ast, indent);
1580
8.80k
      smart_str_appendc(str, '"');
1581
8.80k
      break;
1582
0
    case ZEND_AST_STMT_LIST:
1583
415
    case ZEND_AST_TRAIT_ADAPTATIONS:
1584
415
      zend_ast_export_stmt(str, ast, indent);
1585
415
      break;
1586
2.18k
    case ZEND_AST_IF:
1587
2.18k
      zend_ast_export_if_stmt(str, (zend_ast_list*)ast, indent);
1588
2.18k
      break;
1589
440
    case ZEND_AST_SWITCH_LIST:
1590
865
    case ZEND_AST_CATCH_LIST:
1591
940
    case ZEND_AST_MATCH_ARM_LIST:
1592
940
      zend_ast_export_list(str, (zend_ast_list*)ast, 0, 0, indent);
1593
940
      break;
1594
1.28k
    case ZEND_AST_CLOSURE_USES:
1595
1.28k
      smart_str_appends(str, " use(");
1596
1.28k
      zend_ast_export_var_list(str, (zend_ast_list*)ast, indent);
1597
1.28k
      smart_str_appendc(str, ')');
1598
1.28k
      break;
1599
1.52k
    case ZEND_AST_PROP_GROUP: {
1600
1.52k
      zend_ast *type_ast = ast->child[0];
1601
1.52k
      zend_ast *prop_ast = ast->child[1];
1602
1603
1.52k
      if (ast->child[2]) {
1604
98
        zend_ast_export_attributes(str, ast->child[2], indent, 1);
1605
98
      }
1606
1607
1.52k
      zend_ast_export_visibility(str, ast->attr);
1608
1609
1.52k
      if (ast->attr & ZEND_ACC_STATIC) {
1610
431
        smart_str_appends(str, "static ");
1611
431
      }
1612
1613
1.52k
      if (type_ast) {
1614
126
        zend_ast_export_type(str, type_ast, indent);
1615
126
        smart_str_appendc(str, ' ');
1616
126
      }
1617
1618
1.52k
      ast = prop_ast;
1619
1.52k
      goto simple_list;
1620
865
    }
1621
1622
0
    case ZEND_AST_CONST_DECL:
1623
0
      smart_str_appends(str, "const ");
1624
0
      goto simple_list;
1625
965
    case ZEND_AST_CLASS_CONST_GROUP:
1626
965
      if (ast->child[1]) {
1627
100
        zend_ast_export_attributes(str, ast->child[1], indent, 1);
1628
100
      }
1629
1630
965
      zend_ast_export_visibility(str, ast->attr);
1631
965
      smart_str_appends(str, "const ");
1632
1633
965
      ast = ast->child[0];
1634
1635
965
      goto simple_list;
1636
1.67k
    case ZEND_AST_NAME_LIST:
1637
1.67k
      zend_ast_export_name_list(str, (zend_ast_list*)ast, indent);
1638
1.67k
      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.09k
    case ZEND_AST_TYPE:
1663
2.09k
      switch (ast->attr & ~ZEND_TYPE_NULLABLE) {
1664
1.66k
        case IS_ARRAY:    APPEND_STR("array");
1665
0
        case IS_CALLABLE: APPEND_STR("callable");
1666
431
        case IS_STATIC:   APPEND_STR("static");
1667
0
        case IS_MIXED:    APPEND_STR("mixed");
1668
0
        EMPTY_SWITCH_DEFAULT_CASE();
1669
2.09k
      }
1670
0
      break;
1671
1672
    /* 1 child node */
1673
80.5k
    case ZEND_AST_VAR:
1674
80.5k
      smart_str_appendc(str, '$');
1675
80.5k
      zend_ast_export_var(str, ast->child[0], 0, indent);
1676
80.5k
      break;
1677
6.96k
    case ZEND_AST_CONST:
1678
6.96k
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
1679
6.96k
      break;
1680
442
    case ZEND_AST_UNPACK:
1681
442
      smart_str_appends(str, "...");
1682
442
      ast = ast->child[0];
1683
442
      goto tail_call;
1684
84
    case ZEND_AST_UNARY_PLUS:  PREFIX_OP("+", 240, 241);
1685
13
    case ZEND_AST_UNARY_MINUS: PREFIX_OP("-", 240, 241);
1686
2
    case ZEND_AST_CAST:
1687
2
      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
2
        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
2
      }
1697
0
      break;
1698
390
    case ZEND_AST_EMPTY:
1699
390
      FUNC_OP("empty");
1700
392
    case ZEND_AST_ISSET:
1701
392
      FUNC_OP("isset");
1702
507
    case ZEND_AST_SILENCE:
1703
507
      PREFIX_OP("@", 240, 241);
1704
691
    case ZEND_AST_SHELL_EXEC:
1705
691
      smart_str_appendc(str, '`');
1706
691
      if (ast->child[0]->kind == ZEND_AST_ENCAPS_LIST) {
1707
435
        zend_ast_export_encaps_list(str, '`', (zend_ast_list*)ast->child[0], indent);
1708
256
      } else {
1709
256
        zval *zv;
1710
256
        ZEND_ASSERT(ast->child[0]->kind == ZEND_AST_ZVAL);
1711
256
        zv = zend_ast_get_zval(ast->child[0]);
1712
256
        ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
1713
256
        zend_ast_export_qstr(str, '`', Z_STR_P(zv));
1714
256
      }
1715
691
      smart_str_appendc(str, '`');
1716
691
      break;
1717
395
    case ZEND_AST_CLONE:
1718
395
      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
425
    case ZEND_AST_PRINT:
1727
425
      PREFIX_OP("print ", 60, 61);
1728
390
    case ZEND_AST_INCLUDE_OR_EVAL:
1729
390
      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
390
        case ZEND_EVAL:         FUNC_OP("eval");
1735
0
        EMPTY_SWITCH_DEFAULT_CASE();
1736
390
      }
1737
0
      break;
1738
4.32k
    case ZEND_AST_UNARY_OP:
1739
4.32k
      switch (ast->attr) {
1740
2.04k
        case ZEND_BW_NOT:   PREFIX_OP("~", 240, 241);
1741
2.27k
        case ZEND_BOOL_NOT: PREFIX_OP("!", 240, 241);
1742
0
        EMPTY_SWITCH_DEFAULT_CASE();
1743
4.32k
      }
1744
0
      break;
1745
0
    case ZEND_AST_PRE_INC:
1746
0
      PREFIX_OP("++", 240, 241);
1747
429
    case ZEND_AST_PRE_DEC:
1748
429
      PREFIX_OP("--", 240, 241);
1749
1.28k
    case ZEND_AST_POST_INC:
1750
1.28k
      POSTFIX_OP("++", 240, 241);
1751
424
    case ZEND_AST_POST_DEC:
1752
424
      POSTFIX_OP("--", 240, 241);
1753
1754
793
    case ZEND_AST_GLOBAL:
1755
793
      APPEND_NODE_1("global");
1756
386
    case ZEND_AST_UNSET:
1757
386
      FUNC_OP("unset");
1758
2.63k
    case ZEND_AST_RETURN:
1759
2.63k
      APPEND_NODE_1("return");
1760
459
    case ZEND_AST_LABEL:
1761
459
      zend_ast_export_name(str, ast->child[0], 0, indent);
1762
459
      smart_str_appendc(str, ':');
1763
459
      break;
1764
427
    case ZEND_AST_REF:
1765
427
      smart_str_appendc(str, '&');
1766
427
      ast = ast->child[0];
1767
427
      goto tail_call;
1768
0
    case ZEND_AST_HALT_COMPILER:
1769
0
      APPEND_STR("__HALT_COMPILER()");
1770
1.70k
    case ZEND_AST_ECHO:
1771
1.70k
      APPEND_NODE_1("echo");
1772
0
    case ZEND_AST_THROW:
1773
0
      APPEND_NODE_1("throw");
1774
435
    case ZEND_AST_GOTO:
1775
435
      smart_str_appends(str, "goto ");
1776
435
      zend_ast_export_name(str, ast->child[0], 0, indent);
1777
435
      break;
1778
838
    case ZEND_AST_BREAK:
1779
838
      APPEND_NODE_1("break");
1780
809
    case ZEND_AST_CONTINUE:
1781
809
      APPEND_NODE_1("continue");
1782
1783
    /* 2 child nodes */
1784
5.42k
    case ZEND_AST_DIM:
1785
5.42k
      zend_ast_export_ex(str, ast->child[0], 260, indent);
1786
5.42k
      smart_str_appendc(str, '[');
1787
5.42k
      if (ast->child[1]) {
1788
5.38k
        zend_ast_export_ex(str, ast->child[1], 0, indent);
1789
5.38k
      }
1790
5.42k
      smart_str_appendc(str, ']');
1791
5.42k
      break;
1792
2.18k
    case ZEND_AST_PROP:
1793
2.63k
    case ZEND_AST_NULLSAFE_PROP:
1794
2.63k
      zend_ast_export_ex(str, ast->child[0], 0, indent);
1795
2.63k
      smart_str_appends(str, ast->kind == ZEND_AST_NULLSAFE_PROP ? "?->" : "->");
1796
2.63k
      zend_ast_export_var(str, ast->child[1], 0, indent);
1797
2.63k
      break;
1798
1.29k
    case ZEND_AST_STATIC_PROP:
1799
1.29k
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
1800
1.29k
      smart_str_appends(str, "::$");
1801
1.29k
      zend_ast_export_var(str, ast->child[1], 0, indent);
1802
1.29k
      break;
1803
14.1k
    case ZEND_AST_CALL:
1804
14.1k
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
1805
14.1k
      smart_str_appendc(str, '(');
1806
14.1k
      zend_ast_export_ex(str, ast->child[1], 0, indent);
1807
14.1k
      smart_str_appendc(str, ')');
1808
14.1k
      break;
1809
1.72k
    case ZEND_AST_CLASS_CONST:
1810
1.72k
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
1811
1.72k
      smart_str_appends(str, "::");
1812
1.72k
      zend_ast_export_name(str, ast->child[1], 0, indent);
1813
1.72k
      break;
1814
191
    case ZEND_AST_CLASS_NAME:
1815
191
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
1816
191
      smart_str_appends(str, "::class");
1817
191
      break;
1818
20.2k
    case ZEND_AST_ASSIGN:            BINARY_OP(" = ",   90, 91, 90);
1819
2
    case ZEND_AST_ASSIGN_REF:        BINARY_OP(" =& ",  90, 91, 90);
1820
31
    case ZEND_AST_ASSIGN_OP:
1821
31
      switch (ast->attr) {
1822
0
        case ZEND_ADD:    BINARY_OP(" += ",  90, 91, 90);
1823
0
        case ZEND_SUB:    BINARY_OP(" -= ",  90, 91, 90);
1824
0
        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
0
        case ZEND_CONCAT: BINARY_OP(" .= ",  90, 91, 90);
1830
0
        case ZEND_BW_OR:  BINARY_OP(" |= ",  90, 91, 90);
1831
2
        case ZEND_BW_AND: BINARY_OP(" &= ",  90, 91, 90);
1832
0
        case ZEND_BW_XOR: BINARY_OP(" ^= ",  90, 91, 90);
1833
28
        case ZEND_POW:    BINARY_OP(" **= ", 90, 91, 90);
1834
0
        EMPTY_SWITCH_DEFAULT_CASE();
1835
31
      }
1836
0
      break;
1837
0
    case ZEND_AST_ASSIGN_COALESCE: BINARY_OP(" \?\?= ", 90, 91, 90);
1838
15.5k
    case ZEND_AST_BINARY_OP:
1839
15.5k
      switch (ast->attr) {
1840
559
        case ZEND_ADD:                 BINARY_OP(" + ",   200, 200, 201);
1841
206
        case ZEND_SUB:                 BINARY_OP(" - ",   200, 200, 201);
1842
216
        case ZEND_MUL:                 BINARY_OP(" * ",   210, 210, 211);
1843
161
        case ZEND_DIV:                 BINARY_OP(" / ",   210, 210, 211);
1844
186
        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.53k
        case ZEND_CONCAT:              BINARY_OP(" . ",   185, 185, 186);
1848
75
        case ZEND_BW_OR:               BINARY_OP(" | ",   140, 140, 141);
1849
154
        case ZEND_BW_AND:              BINARY_OP(" & ",   160, 160, 161);
1850
152
        case ZEND_BW_XOR:              BINARY_OP(" ^ ",   150, 150, 151);
1851
2.51k
        case ZEND_IS_IDENTICAL:        BINARY_OP(" === ", 170, 171, 171);
1852
11
        case ZEND_IS_NOT_IDENTICAL:    BINARY_OP(" !== ", 170, 171, 171);
1853
6.16k
        case ZEND_IS_EQUAL:            BINARY_OP(" == ",  170, 171, 171);
1854
11
        case ZEND_IS_NOT_EQUAL:        BINARY_OP(" != ",  170, 171, 171);
1855
535
        case ZEND_IS_SMALLER:          BINARY_OP(" < ",   180, 181, 181);
1856
23
        case ZEND_IS_SMALLER_OR_EQUAL: BINARY_OP(" <= ",  180, 181, 181);
1857
2
        case ZEND_POW:                 BINARY_OP(" ** ",  250, 251, 250);
1858
0
        case ZEND_BOOL_XOR:            BINARY_OP(" xor ",  40,  40,  41);
1859
1
        case ZEND_SPACESHIP:           BINARY_OP(" <=> ", 180, 181, 181);
1860
0
        EMPTY_SWITCH_DEFAULT_CASE();
1861
15.5k
      }
1862
0
      break;
1863
134
    case ZEND_AST_GREATER:                 BINARY_OP(" > ",   180, 181, 181);
1864
12
    case ZEND_AST_GREATER_EQUAL:           BINARY_OP(" >= ",  180, 181, 181);
1865
4.29k
    case ZEND_AST_AND:                     BINARY_OP(" && ",  130, 130, 131);
1866
2.34k
    case ZEND_AST_OR:                      BINARY_OP(" || ",  120, 120, 121);
1867
3.76k
    case ZEND_AST_ARRAY_ELEM:
1868
3.76k
      if (ast->child[1]) {
1869
761
        zend_ast_export_ex(str, ast->child[1], 80, indent);
1870
761
        smart_str_appends(str, " => ");
1871
761
      }
1872
3.76k
      if (ast->attr)
1873
2
        smart_str_appendc(str, '&');
1874
3.76k
      zend_ast_export_ex(str, ast->child[0], 80, indent);
1875
3.76k
      break;
1876
2.32k
    case ZEND_AST_NEW:
1877
2.32k
      smart_str_appends(str, "new ");
1878
2.32k
      if (ast->child[0]->kind == ZEND_AST_CLASS) {
1879
187
        zend_ast_decl *decl = (zend_ast_decl *) ast->child[0];
1880
187
        if (decl->child[4]) {
1881
101
          zend_ast_export_attributes(str, decl->child[4], indent, 0);
1882
101
        }
1883
187
        smart_str_appends(str, "class");
1884
187
        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
187
        zend_ast_export_class_no_header(str, decl, indent);
1890
2.13k
      } else {
1891
2.13k
        zend_ast_export_ns_name(str, ast->child[0], 0, indent);
1892
2.13k
        smart_str_appendc(str, '(');
1893
2.13k
        zend_ast_export_ex(str, ast->child[1], 0, indent);
1894
2.13k
        smart_str_appendc(str, ')');
1895
2.13k
      }
1896
2.32k
      break;
1897
249
    case ZEND_AST_INSTANCEOF:
1898
249
      zend_ast_export_ex(str, ast->child[0], 0, indent);
1899
249
      smart_str_appends(str, " instanceof ");
1900
249
      zend_ast_export_ns_name(str, ast->child[1], 0, indent);
1901
249
      break;
1902
421
    case ZEND_AST_YIELD:
1903
421
      if (priority > 70) smart_str_appendc(str, '(');
1904
421
      smart_str_appends(str, "yield ");
1905
421
      if (ast->child[0]) {
1906
421
        if (ast->child[1]) {
1907
396
          zend_ast_export_ex(str, ast->child[1], 70, indent);
1908
396
          smart_str_appends(str, " => ");
1909
396
        }
1910
421
        zend_ast_export_ex(str, ast->child[0], 70, indent);
1911
421
      }
1912
421
      if (priority > 70) smart_str_appendc(str, ')');
1913
421
      break;
1914
397
    case ZEND_AST_YIELD_FROM:
1915
397
      PREFIX_OP("yield from ", 85, 86);
1916
394
    case ZEND_AST_COALESCE: BINARY_OP(" ?? ", 110, 111, 110);
1917
792
    case ZEND_AST_STATIC:
1918
792
      smart_str_appends(str, "static $");
1919
792
      zend_ast_export_name(str, ast->child[0], 0, indent);
1920
792
      APPEND_DEFAULT_VALUE(1);
1921
428
    case ZEND_AST_WHILE:
1922
428
      smart_str_appends(str, "while (");
1923
428
      zend_ast_export_ex(str, ast->child[0], 0, indent);
1924
428
      smart_str_appends(str, ") {\n");
1925
428
      zend_ast_export_stmt(str, ast->child[1], indent + 1);
1926
428
      zend_ast_export_indent(str, indent);
1927
428
      smart_str_appendc(str, '}');
1928
428
      break;
1929
865
    case ZEND_AST_DO_WHILE:
1930
865
      smart_str_appends(str, "do {\n");
1931
865
      zend_ast_export_stmt(str, ast->child[0], indent + 1);
1932
865
      zend_ast_export_indent(str, indent);
1933
865
      smart_str_appends(str, "} while (");
1934
865
      zend_ast_export_ex(str, ast->child[1], 0, indent);
1935
865
      smart_str_appendc(str, ')');
1936
865
      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
440
    case ZEND_AST_SWITCH:
1952
440
      smart_str_appends(str, "switch (");
1953
440
      zend_ast_export_ex(str, ast->child[0], 0, indent);
1954
440
      smart_str_appends(str, ") {\n");
1955
440
      zend_ast_export_ex(str, ast->child[1], 0, indent + 1);
1956
440
      zend_ast_export_indent(str, indent);
1957
440
      smart_str_appendc(str, '}');
1958
440
      break;
1959
2.54k
    case ZEND_AST_SWITCH_CASE:
1960
2.54k
      zend_ast_export_indent(str, indent);
1961
2.54k
      if (ast->child[0]) {
1962
2.12k
        smart_str_appends(str, "case ");
1963
2.12k
        zend_ast_export_ex(str, ast->child[0], 0, indent);
1964
2.12k
        smart_str_appends(str, ":\n");
1965
421
      } else {
1966
421
        smart_str_appends(str, "default:\n");
1967
421
      }
1968
2.54k
      zend_ast_export_stmt(str, ast->child[1], indent + 1);
1969
2.54k
      break;
1970
75
    case ZEND_AST_MATCH:
1971
75
      smart_str_appends(str, "match (");
1972
75
      zend_ast_export_ex(str, ast->child[0], 0, indent);
1973
75
      smart_str_appends(str, ") {\n");
1974
75
      zend_ast_export_ex(str, ast->child[1], 0, indent + 1);
1975
75
      zend_ast_export_indent(str, indent);
1976
75
      smart_str_appendc(str, '}');
1977
75
      break;
1978
199
    case ZEND_AST_MATCH_ARM:
1979
199
      zend_ast_export_indent(str, indent);
1980
199
      if (ast->child[0]) {
1981
148
        zend_ast_export_list(str, (zend_ast_list*)ast->child[0], 1, 0, indent);
1982
148
        smart_str_appends(str, " => ");
1983
51
      } else {
1984
51
        smart_str_appends(str, "default => ");
1985
51
      }
1986
199
      zend_ast_export_ex(str, ast->child[1], 0, 0);
1987
199
      smart_str_appends(str, ",\n");
1988
199
      break;
1989
859
    case ZEND_AST_DECLARE:
1990
859
      smart_str_appends(str, "declare(");
1991
859
      ZEND_ASSERT(ast->child[0]->kind == ZEND_AST_CONST_DECL);
1992
859
      zend_ast_export_list(str, (zend_ast_list*)ast->child[0], 1, 0, indent);
1993
859
      smart_str_appendc(str, ')');
1994
859
      if (ast->child[1]) {
1995
436
        smart_str_appends(str, " {\n");
1996
436
        zend_ast_export_stmt(str, ast->child[1], indent + 1);
1997
436
        zend_ast_export_indent(str, indent);
1998
436
        smart_str_appendc(str, '}');
1999
423
      } else {
2000
423
        smart_str_appendc(str, ';');
2001
423
      }
2002
859
      break;
2003
1.95k
    case ZEND_AST_PROP_ELEM:
2004
1.95k
      smart_str_appendc(str, '$');
2005
      /* break missing intentionally */
2006
4.63k
    case ZEND_AST_CONST_ELEM:
2007
4.63k
      zend_ast_export_name(str, ast->child[0], 0, indent);
2008
4.63k
      APPEND_DEFAULT_VALUE(1);
2009
830
    case ZEND_AST_USE_TRAIT:
2010
830
      smart_str_appends(str, "use ");
2011
830
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2012
830
      if (ast->child[1]) {
2013
415
        smart_str_appends(str, " {\n");
2014
415
        zend_ast_export_ex(str, ast->child[1], 0, indent + 1);
2015
415
        zend_ast_export_indent(str, indent);
2016
415
        smart_str_appends(str, "}");
2017
415
      } else {
2018
415
        smart_str_appends(str, ";");
2019
415
      }
2020
830
      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.62k
    case ZEND_AST_METHOD_REFERENCE:
2027
1.62k
      if (ast->child[0]) {
2028
823
        zend_ast_export_name(str, ast->child[0], 0, indent);
2029
823
        smart_str_appends(str, "::");
2030
823
      }
2031
1.62k
      zend_ast_export_name(str, ast->child[1], 0, indent);
2032
1.62k
      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.21k
    case ZEND_AST_TRAIT_ALIAS:
2050
1.21k
      zend_ast_export_name(str, ast->child[0], 0, indent);
2051
1.21k
      if (ast->attr & ZEND_ACC_PUBLIC) {
2052
389
        smart_str_appends(str, " as public");
2053
828
      } else if (ast->attr & ZEND_ACC_PROTECTED) {
2054
411
        smart_str_appends(str, " as protected");
2055
417
      } else if (ast->attr & ZEND_ACC_PRIVATE) {
2056
0
        smart_str_appends(str, " as private");
2057
417
      } else if (ast->child[1]) {
2058
417
        smart_str_appends(str, " as");
2059
417
      }
2060
1.21k
      if (ast->child[1]) {
2061
828
        smart_str_appendc(str, ' ');
2062
828
        zend_ast_export_name(str, ast->child[1], 0, indent);
2063
828
      }
2064
1.21k
      break;
2065
459
    case ZEND_AST_NAMED_ARG:
2066
459
      smart_str_append(str, zend_ast_get_str(ast->child[0]));
2067
459
      smart_str_appends(str, ": ");
2068
459
      ast = ast->child[1];
2069
459
      goto tail_call;
2070
2071
    /* 3 child nodes */
2072
1.23k
    case ZEND_AST_METHOD_CALL:
2073
1.67k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2074
1.67k
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2075
1.67k
      smart_str_appends(str, ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL ? "?->" : "->");
2076
1.67k
      zend_ast_export_var(str, ast->child[1], 0, indent);
2077
1.67k
      smart_str_appendc(str, '(');
2078
1.67k
      zend_ast_export_ex(str, ast->child[2], 0, indent);
2079
1.67k
      smart_str_appendc(str, ')');
2080
1.67k
      break;
2081
1.26k
    case ZEND_AST_STATIC_CALL:
2082
1.26k
      zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2083
1.26k
      smart_str_appends(str, "::");
2084
1.26k
      zend_ast_export_var(str, ast->child[1], 0, indent);
2085
1.26k
      smart_str_appendc(str, '(');
2086
1.26k
      zend_ast_export_ex(str, ast->child[2], 0, indent);
2087
1.26k
      smart_str_appendc(str, ')');
2088
1.26k
      break;
2089
809
    case ZEND_AST_CONDITIONAL:
2090
809
      if (priority > 100) smart_str_appendc(str, '(');
2091
809
      zend_ast_export_ex(str, ast->child[0], 100, indent);
2092
809
      if (ast->child[1]) {
2093
395
        smart_str_appends(str, " ? ");
2094
395
        zend_ast_export_ex(str, ast->child[1], 101, indent);
2095
395
        smart_str_appends(str, " : ");
2096
414
      } else {
2097
414
        smart_str_appends(str, " ?: ");
2098
414
      }
2099
809
      zend_ast_export_ex(str, ast->child[2], 101, indent);
2100
809
      if (priority > 100) smart_str_appendc(str, ')');
2101
809
      break;
2102
2103
425
    case ZEND_AST_TRY:
2104
425
      smart_str_appends(str, "try {\n");
2105
425
      zend_ast_export_stmt(str, ast->child[0], indent + 1);
2106
425
      zend_ast_export_indent(str, indent);
2107
425
      zend_ast_export_ex(str, ast->child[1], 0, indent);
2108
425
      if (ast->child[2]) {
2109
423
        smart_str_appends(str, "} finally {\n");
2110
423
        zend_ast_export_stmt(str, ast->child[2], indent + 1);
2111
423
        zend_ast_export_indent(str, indent);
2112
423
      }
2113
425
      smart_str_appendc(str, '}');
2114
425
      break;
2115
845
    case ZEND_AST_CATCH:
2116
845
      smart_str_appends(str, "} catch (");
2117
845
      zend_ast_export_catch_name_list(str, zend_ast_get_list(ast->child[0]), indent);
2118
845
      if (ast->child[1]) {
2119
844
        smart_str_appends(str, " $");
2120
844
        zend_ast_export_var(str, ast->child[1], 0, indent);
2121
844
      }
2122
845
      smart_str_appends(str, ") {\n");
2123
845
      zend_ast_export_stmt(str, ast->child[2], indent + 1);
2124
845
      zend_ast_export_indent(str, indent);
2125
845
      break;
2126
3.78k
    case ZEND_AST_PARAM:
2127
3.78k
      if (ast->child[3]) {
2128
75
        zend_ast_export_attributes(str, ast->child[3], indent, 0);
2129
75
      }
2130
3.78k
      if (ast->child[0]) {
2131
3.61k
        zend_ast_export_type(str, ast->child[0], indent);
2132
3.61k
        smart_str_appendc(str, ' ');
2133
3.61k
      }
2134
3.78k
      if (ast->attr & ZEND_PARAM_REF) {
2135
1.68k
        smart_str_appendc(str, '&');
2136
1.68k
      }
2137
3.78k
      if (ast->attr & ZEND_PARAM_VARIADIC) {
2138
475
        smart_str_appends(str, "...");
2139
475
      }
2140
3.78k
      smart_str_appendc(str, '$');
2141
3.78k
      zend_ast_export_name(str, ast->child[1], 0, indent);
2142
3.78k
      APPEND_DEFAULT_VALUE(2);
2143
2144
    /* 4 child nodes */
2145
434
    case ZEND_AST_FOR:
2146
434
      smart_str_appends(str, "for (");
2147
434
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2148
434
      smart_str_appendc(str, ';');
2149
434
      if (ast->child[1]) {
2150
434
        smart_str_appendc(str, ' ');
2151
434
        zend_ast_export_ex(str, ast->child[1], 0, indent);
2152
434
      }
2153
434
      smart_str_appendc(str, ';');
2154
434
      if (ast->child[2]) {
2155
434
        smart_str_appendc(str, ' ');
2156
434
        zend_ast_export_ex(str, ast->child[2], 0, indent);
2157
434
      }
2158
434
      smart_str_appends(str, ") {\n");
2159
434
      zend_ast_export_stmt(str, ast->child[3], indent + 1);
2160
434
      zend_ast_export_indent(str, indent);
2161
434
      smart_str_appendc(str, '}');
2162
434
      break;
2163
427
    case ZEND_AST_FOREACH:
2164
427
      smart_str_appends(str, "foreach (");
2165
427
      zend_ast_export_ex(str, ast->child[0], 0, indent);
2166
427
      smart_str_appends(str, " as ");
2167
427
      if (ast->child[2]) {
2168
427
        zend_ast_export_ex(str, ast->child[2], 0, indent);
2169
427
        smart_str_appends(str, " => ");
2170
427
      }
2171
427
      zend_ast_export_ex(str, ast->child[1], 0, indent);
2172
427
      smart_str_appends(str, ") {\n");
2173
427
      zend_ast_export_stmt(str, ast->child[3], indent + 1);
2174
427
      zend_ast_export_indent(str, indent);
2175
427
      smart_str_appendc(str, '}');
2176
427
      break;
2177
0
    EMPTY_SWITCH_DEFAULT_CASE();
2178
304k
  }
2179
231k
  return;
2180
2181
42.9k
binary_op:
2182
42.9k
  if (priority > p) smart_str_appendc(str, '(');
2183
42.9k
  zend_ast_export_ex(str, ast->child[0], pl, indent);
2184
42.9k
  smart_str_appends(str, op);
2185
42.9k
  zend_ast_export_ex(str, ast->child[1], pr, indent);
2186
42.9k
  if (priority > p) smart_str_appendc(str, ')');
2187
42.9k
  return;
2188
2189
6.57k
prefix_op:
2190
6.57k
  if (priority > p) smart_str_appendc(str, '(');
2191
6.57k
  smart_str_appends(str, op);
2192
6.57k
  zend_ast_export_ex(str, ast->child[0], pl, indent);
2193
6.57k
  if (priority > p) smart_str_appendc(str, ')');
2194
6.57k
  return;
2195
2196
1.70k
postfix_op:
2197
1.70k
  if (priority > p) smart_str_appendc(str, '(');
2198
1.70k
  zend_ast_export_ex(str, ast->child[0], pl, indent);
2199
1.70k
  smart_str_appends(str, op);
2200
1.70k
  if (priority > p) smart_str_appendc(str, ')');
2201
1.70k
  return;
2202
2203
1.55k
func_op:
2204
1.55k
  smart_str_appends(str, op);
2205
1.55k
  smart_str_appendc(str, '(');
2206
1.55k
  zend_ast_export_ex(str, ast->child[0], 0, indent);
2207
1.55k
  smart_str_appendc(str, ')');
2208
1.55k
  return;
2209
2210
6.78k
append_node_1:
2211
6.78k
  smart_str_appends(str, op);
2212
6.78k
  if (ast->child[0]) {
2213
5.52k
    smart_str_appendc(str, ' ');
2214
5.52k
    ast = ast->child[0];
2215
5.52k
    goto tail_call;
2216
5.52k
  }
2217
1.26k
  return;
2218
2219
2.09k
append_str:
2220
2.09k
  smart_str_appends(str, op);
2221
2.09k
  return;
2222
2223
9.21k
append_default_value:
2224
9.21k
  if (ast->child[p]) {
2225
5.27k
    smart_str_appends(str, " = ");
2226
5.27k
    ast = ast->child[p];
2227
5.27k
    goto tail_call;
2228
5.27k
  }
2229
3.93k
  return;
2230
3.93k
}
2231
2232
ZEND_API ZEND_COLD zend_string *zend_ast_export(const char *prefix, zend_ast *ast, const char *suffix)
2233
20.9k
{
2234
20.9k
  smart_str str = {0};
2235
2236
20.9k
  smart_str_appends(&str, prefix);
2237
20.9k
  zend_ast_export_ex(&str, ast, 0, 0);
2238
20.9k
  smart_str_appends(&str, suffix);
2239
20.9k
  smart_str_0(&str);
2240
20.9k
  return str.s;
2241
20.9k
}
2242
2243
zend_ast * ZEND_FASTCALL zend_ast_with_attributes(zend_ast *ast, zend_ast *attr)
2244
16.6k
{
2245
16.6k
  ZEND_ASSERT(attr->kind == ZEND_AST_ATTRIBUTE_LIST);
2246
2247
16.6k
  switch (ast->kind) {
2248
243
  case ZEND_AST_FUNC_DECL:
2249
6.24k
  case ZEND_AST_CLOSURE:
2250
7.68k
  case ZEND_AST_METHOD:
2251
13.1k
  case ZEND_AST_CLASS:
2252
13.3k
  case ZEND_AST_ARROW_FUNC:
2253
13.3k
    ((zend_ast_decl *) ast)->child[4] = attr;
2254
13.3k
    break;
2255
2.13k
  case ZEND_AST_PROP_GROUP:
2256
2.13k
    ast->child[2] = attr;
2257
2.13k
    break;
2258
647
  case ZEND_AST_PARAM:
2259
647
    ast->child[3] = attr;
2260
647
    break;
2261
514
  case ZEND_AST_CLASS_CONST_GROUP:
2262
514
    ast->child[1] = attr;
2263
514
    break;
2264
0
  EMPTY_SWITCH_DEFAULT_CASE()
2265
16.6k
  }
2266
2267
16.6k
  return ast;
2268
16.6k
}