Coverage Report

Created: 2025-12-14 06:06

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