Coverage Report

Created: 2025-07-23 06:33

/src/php-src/Zend/zend_compile.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to version 2.00 of the Zend license,     |
8
   | that is bundled with this package in the file LICENSE, and is        |
9
   | available through the world-wide-web at the following url:           |
10
   | http://www.zend.com/license/2_00.txt.                                |
11
   | If you did not receive a copy of the Zend license and are unable to  |
12
   | obtain it through the world-wide-web, please send a note to          |
13
   | license@zend.com so we can mail you a copy immediately.              |
14
   +----------------------------------------------------------------------+
15
   | Authors: Andi Gutmans <andi@php.net>                                 |
16
   |          Zeev Suraski <zeev@php.net>                                 |
17
   |          Nikita Popov <nikic@php.net>                                |
18
   +----------------------------------------------------------------------+
19
*/
20
21
#include <zend_language_parser.h>
22
#include "zend.h"
23
#include "zend_ast.h"
24
#include "zend_attributes.h"
25
#include "zend_compile.h"
26
#include "zend_constants.h"
27
#include "zend_llist.h"
28
#include "zend_API.h"
29
#include "zend_exceptions.h"
30
#include "zend_interfaces.h"
31
#include "zend_virtual_cwd.h"
32
#include "zend_multibyte.h"
33
#include "zend_language_scanner.h"
34
#include "zend_inheritance.h"
35
#include "zend_vm.h"
36
#include "zend_enum.h"
37
#include "zend_observer.h"
38
#include "zend_call_stack.h"
39
#include "zend_frameless_function.h"
40
#include "zend_property_hooks.h"
41
42
9.70M
#define SET_NODE(target, src) do { \
43
9.70M
    target ## _type = (src)->op_type; \
44
9.70M
    if ((src)->op_type == IS_CONST) { \
45
1.78M
      target.constant = zend_add_literal(&(src)->u.constant); \
46
7.92M
    } else { \
47
7.92M
      target = (src)->u.op; \
48
7.92M
    } \
49
9.70M
  } while (0)
50
51
5.17M
#define GET_NODE(target, src) do { \
52
5.17M
    (target)->op_type = src ## _type; \
53
5.17M
    if ((target)->op_type == IS_CONST) { \
54
222
      ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
55
5.17M
    } else { \
56
5.17M
      (target)->u.op = src; \
57
5.17M
    } \
58
5.17M
  } while (0)
59
60
11.5M
#define FC(member) (CG(file_context).member)
61
62
typedef struct _zend_loop_var {
63
  uint8_t opcode;
64
  uint8_t var_type;
65
  uint32_t   var_num;
66
  uint32_t   try_catch_offset;
67
} zend_loop_var;
68
69
1.48M
static inline uint32_t zend_alloc_cache_slots(unsigned count) {
70
1.48M
  if (count == 0) {
71
    /* Even if no cache slots are desired, the VM handler may still want to acquire
72
     * CACHE_ADDR() unconditionally. Returning zero makes sure that the address
73
     * calculation is still legal and ubsan does not complain. */
74
0
    return 0;
75
0
  }
76
77
1.48M
  zend_op_array *op_array = CG(active_op_array);
78
1.48M
  uint32_t ret = op_array->cache_size;
79
1.48M
  op_array->cache_size += count * sizeof(void*);
80
1.48M
  return ret;
81
1.48M
}
82
83
1.25M
static inline uint32_t zend_alloc_cache_slot(void) {
84
1.25M
  return zend_alloc_cache_slots(1);
85
1.25M
}
86
87
ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
88
ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename, zend_compile_position position);
89
90
#ifndef ZTS
91
ZEND_API zend_compiler_globals compiler_globals;
92
ZEND_API zend_executor_globals executor_globals;
93
#endif
94
95
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2);
96
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast);
97
static void zend_eval_const_expr(zend_ast **ast_ptr);
98
99
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
100
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
101
static void zend_compile_expr(znode *result, zend_ast *ast);
102
static void zend_compile_stmt(zend_ast *ast);
103
static void zend_compile_assign(znode *result, zend_ast *ast);
104
105
#ifdef ZEND_CHECK_STACK_LIMIT
106
zend_never_inline static void zend_stack_limit_error(void)
107
0
{
108
0
  size_t max_stack_size = 0;
109
0
  if ((uintptr_t) EG(stack_base) > (uintptr_t) EG(stack_limit)) {
110
0
    max_stack_size = (size_t) ((uintptr_t) EG(stack_base) - (uintptr_t) EG(stack_limit));
111
0
  }
112
113
0
  zend_error_noreturn(E_COMPILE_ERROR,
114
0
    "Maximum call stack size of %zu bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached during compilation. Try splitting expression",
115
0
    max_stack_size);
116
0
}
117
118
static void zend_check_stack_limit(void)
119
18.0M
{
120
18.0M
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
121
0
    zend_stack_limit_error();
122
0
  }
123
18.0M
}
124
#else /* ZEND_CHECK_STACK_LIMIT */
125
static void zend_check_stack_limit(void)
126
{
127
}
128
#endif /* ZEND_CHECK_STACK_LIMIT */
129
130
static void init_op(zend_op *op)
131
11.0M
{
132
11.0M
  MAKE_NOP(op);
133
11.0M
  op->extended_value = 0;
134
11.0M
  op->lineno = CG(zend_lineno);
135
#ifdef ZEND_VERIFY_TYPE_INFERENCE
136
  op->op1_use_type = 0;
137
  op->op2_use_type = 0;
138
  op->result_use_type = 0;
139
  op->op1_def_type = 0;
140
  op->op2_def_type = 0;
141
  op->result_def_type = 0;
142
#endif
143
11.0M
}
144
145
static zend_always_inline uint32_t get_next_op_number(void)
146
2.29M
{
147
2.29M
  return CG(active_op_array)->last;
148
2.29M
}
149
150
static zend_op *get_next_op(void)
151
10.7M
{
152
10.7M
  zend_op_array *op_array = CG(active_op_array);
153
10.7M
  uint32_t next_op_num = op_array->last++;
154
10.7M
  zend_op *next_op;
155
156
10.7M
  if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) {
157
40.2k
    CG(context).opcodes_size *= 4;
158
40.2k
    op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op));
159
40.2k
  }
160
161
10.7M
  next_op = &(op_array->opcodes[next_op_num]);
162
163
10.7M
  init_op(next_op);
164
165
10.7M
  return next_op;
166
10.7M
}
167
168
static zend_brk_cont_element *get_next_brk_cont_element(void)
169
81.2k
{
170
81.2k
  CG(context).last_brk_cont++;
171
81.2k
  CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont);
172
81.2k
  return &CG(context).brk_cont_array[CG(context).last_brk_cont-1];
173
81.2k
}
174
175
static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */
176
37.9k
{
177
37.9k
  zend_string *filename = CG(active_op_array)->filename;
178
37.9k
  zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32,
179
37.9k
    '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
180
37.9k
  return zend_new_interned_string(result);
181
37.9k
}
182
/* }}} */
183
184
static bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
185
1.20M
{
186
1.20M
  const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
187
1.20M
  if (ns_separator != NULL) {
188
751k
    *result = ns_separator + 1;
189
751k
    *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
190
751k
    return 1;
191
751k
  }
192
193
456k
  return 0;
194
1.20M
}
195
/* }}} */
196
197
struct reserved_class_name {
198
  const char *name;
199
  size_t len;
200
};
201
static const struct reserved_class_name reserved_class_names[] = {
202
  {ZEND_STRL("bool")},
203
  {ZEND_STRL("false")},
204
  {ZEND_STRL("float")},
205
  {ZEND_STRL("int")},
206
  {ZEND_STRL("null")},
207
  {ZEND_STRL("parent")},
208
  {ZEND_STRL("self")},
209
  {ZEND_STRL("static")},
210
  {ZEND_STRL("string")},
211
  {ZEND_STRL("true")},
212
  {ZEND_STRL("void")},
213
  {ZEND_STRL("never")},
214
  {ZEND_STRL("iterable")},
215
  {ZEND_STRL("object")},
216
  {ZEND_STRL("mixed")},
217
  /* These are not usable as class names because they're proper tokens,
218
   * but they are here for class aliases. */
219
  {ZEND_STRL("array")},
220
  {ZEND_STRL("callable")},
221
  {NULL, 0}
222
};
223
224
static bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */
225
158k
{
226
158k
  const struct reserved_class_name *reserved = reserved_class_names;
227
228
158k
  const char *uqname = ZSTR_VAL(name);
229
158k
  size_t uqname_len = ZSTR_LEN(name);
230
158k
  zend_get_unqualified_name(name, &uqname, &uqname_len);
231
232
2.84M
  for (; reserved->name; ++reserved) {
233
2.69M
    if (uqname_len == reserved->len
234
2.69M
      && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
235
2.69M
    ) {
236
111
      return 1;
237
111
    }
238
2.69M
  }
239
240
158k
  return 0;
241
158k
}
242
/* }}} */
243
244
void zend_assert_valid_class_name(const zend_string *name, const char *type) /* {{{ */
245
156k
{
246
156k
  if (zend_is_reserved_class_name(name)) {
247
85
    zend_error_noreturn(E_COMPILE_ERROR,
248
85
      "Cannot use \"%s\" as %s as it is reserved", ZSTR_VAL(name), type);
249
85
  }
250
156k
  if (zend_string_equals_literal(name, "_")) {
251
209
    zend_error(E_DEPRECATED, "Using \"_\" as %s is deprecated since 8.4", type);
252
209
  }
253
156k
}
254
/* }}} */
255
256
typedef struct _builtin_type_info {
257
  const char* name;
258
  const size_t name_len;
259
  const uint8_t type;
260
} builtin_type_info;
261
262
static const builtin_type_info builtin_types[] = {
263
  {ZEND_STRL("null"), IS_NULL},
264
  {ZEND_STRL("true"), IS_TRUE},
265
  {ZEND_STRL("false"), IS_FALSE},
266
  {ZEND_STRL("int"), IS_LONG},
267
  {ZEND_STRL("float"), IS_DOUBLE},
268
  {ZEND_STRL("string"), IS_STRING},
269
  {ZEND_STRL("bool"), _IS_BOOL},
270
  {ZEND_STRL("void"), IS_VOID},
271
  {ZEND_STRL("never"), IS_NEVER},
272
  {ZEND_STRL("iterable"), IS_ITERABLE},
273
  {ZEND_STRL("object"), IS_OBJECT},
274
  {ZEND_STRL("mixed"), IS_MIXED},
275
  {NULL, 0, IS_UNDEF}
276
};
277
278
typedef struct {
279
  const char *name;
280
  size_t name_len;
281
  const char *correct_name;
282
} confusable_type_info;
283
284
static const confusable_type_info confusable_types[] = {
285
  {ZEND_STRL("boolean"), "bool"},
286
  {ZEND_STRL("integer"), "int"},
287
  {ZEND_STRL("double"), "float"},
288
  {ZEND_STRL("resource"), NULL},
289
  {NULL, 0, NULL},
290
};
291
292
static zend_always_inline uint8_t zend_lookup_builtin_type_by_name(const zend_string *name) /* {{{ */
293
128k
{
294
128k
  const builtin_type_info *info = &builtin_types[0];
295
296
1.46M
  for (; info->name; ++info) {
297
1.37M
    if (ZSTR_LEN(name) == info->name_len
298
1.37M
      && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
299
1.37M
    ) {
300
37.1k
      return info->type;
301
37.1k
    }
302
1.37M
  }
303
304
91.5k
  return 0;
305
128k
}
306
/* }}} */
307
308
static zend_always_inline bool zend_is_confusable_type(const zend_string *name, const char **correct_name) /* {{{ */
309
91.0k
{
310
91.0k
  const confusable_type_info *info = confusable_types;
311
312
  /* Intentionally using case-sensitive comparison here, because "integer" is likely intended
313
   * as a scalar type, while "Integer" is likely a class type. */
314
453k
  for (; info->name; ++info) {
315
363k
    if (zend_string_equals_cstr(name, info->name, info->name_len)) {
316
898
      *correct_name = info->correct_name;
317
898
      return 1;
318
898
    }
319
363k
  }
320
321
90.1k
  return 0;
322
91.0k
}
323
/* }}} */
324
325
898
static bool zend_is_not_imported(zend_string *name) {
326
  /* Assuming "name" is unqualified here. */
327
898
  return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL;
328
898
}
329
330
void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_array *op_array) /* {{{ */
331
266k
{
332
266k
  *prev_context = CG(context);
333
266k
  CG(context).prev = CG(context).op_array ? prev_context : NULL;
334
266k
  CG(context).op_array = op_array;
335
266k
  CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
336
266k
  CG(context).vars_size = 0;
337
266k
  CG(context).literals_size = 0;
338
266k
  CG(context).fast_call_var = -1;
339
266k
  CG(context).try_catch_offset = -1;
340
266k
  CG(context).current_brk_cont = -1;
341
266k
  CG(context).last_brk_cont = 0;
342
266k
  CG(context).brk_cont_array = NULL;
343
266k
  CG(context).labels = NULL;
344
266k
  CG(context).in_jmp_frameless_branch = false;
345
266k
  CG(context).active_property_info_name = NULL;
346
266k
  CG(context).active_property_hook_kind = (zend_property_hook_kind)-1;
347
266k
}
348
/* }}} */
349
350
void zend_oparray_context_end(zend_oparray_context *prev_context) /* {{{ */
351
258k
{
352
258k
  if (CG(context).brk_cont_array) {
353
39.1k
    efree(CG(context).brk_cont_array);
354
39.1k
    CG(context).brk_cont_array = NULL;
355
39.1k
  }
356
258k
  if (CG(context).labels) {
357
3.99k
    zend_hash_destroy(CG(context).labels);
358
3.99k
    FREE_HASHTABLE(CG(context).labels);
359
3.99k
    CG(context).labels = NULL;
360
3.99k
  }
361
258k
  CG(context) = *prev_context;
362
258k
}
363
/* }}} */
364
365
static void zend_reset_import_tables(void) /* {{{ */
366
103k
{
367
103k
  if (FC(imports)) {
368
829
    zend_hash_destroy(FC(imports));
369
829
    efree(FC(imports));
370
829
    FC(imports) = NULL;
371
829
  }
372
373
103k
  if (FC(imports_function)) {
374
483
    zend_hash_destroy(FC(imports_function));
375
483
    efree(FC(imports_function));
376
483
    FC(imports_function) = NULL;
377
483
  }
378
379
103k
  if (FC(imports_const)) {
380
124
    zend_hash_destroy(FC(imports_const));
381
124
    efree(FC(imports_const));
382
124
    FC(imports_const) = NULL;
383
124
  }
384
385
103k
  zend_hash_clean(&FC(seen_symbols));
386
103k
}
387
/* }}} */
388
389
98.1k
static void zend_end_namespace(void) /* {{{ */ {
390
98.1k
  FC(in_namespace) = 0;
391
98.1k
  zend_reset_import_tables();
392
98.1k
  if (FC(current_namespace)) {
393
3.30k
    zend_string_release_ex(FC(current_namespace), 0);
394
3.30k
    FC(current_namespace) = NULL;
395
3.30k
  }
396
98.1k
}
397
/* }}} */
398
399
void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
400
102k
{
401
102k
  *prev_context = CG(file_context);
402
102k
  FC(imports) = NULL;
403
102k
  FC(imports_function) = NULL;
404
102k
  FC(imports_const) = NULL;
405
102k
  FC(current_namespace) = NULL;
406
102k
  FC(in_namespace) = 0;
407
102k
  FC(has_bracketed_namespaces) = 0;
408
102k
  FC(declarables).ticks = 0;
409
102k
  zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
410
102k
}
411
/* }}} */
412
413
void zend_file_context_end(zend_file_context *prev_context) /* {{{ */
414
96.3k
{
415
96.3k
  zend_end_namespace();
416
96.3k
  zend_hash_destroy(&FC(seen_symbols));
417
96.3k
  CG(file_context) = *prev_context;
418
96.3k
}
419
/* }}} */
420
421
void zend_init_compiler_data_structures(void) /* {{{ */
422
274k
{
423
274k
  zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
424
274k
  zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
425
274k
  zend_stack_init(&CG(short_circuiting_opnums), sizeof(uint32_t));
426
274k
  CG(active_class_entry) = NULL;
427
274k
  CG(in_compilation) = 0;
428
274k
  CG(skip_shebang) = 0;
429
430
274k
  CG(encoding_declared) = 0;
431
274k
  CG(memoized_exprs) = NULL;
432
274k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
433
274k
}
434
/* }}} */
435
436
198k
static void zend_register_seen_symbol(zend_string *name, uint32_t kind) {
437
198k
  zval *zv = zend_hash_find(&FC(seen_symbols), name);
438
198k
  if (zv) {
439
116k
    Z_LVAL_P(zv) |= kind;
440
116k
  } else {
441
81.4k
    zval tmp;
442
81.4k
    ZVAL_LONG(&tmp, kind);
443
81.4k
    zend_hash_add_new(&FC(seen_symbols), name, &tmp);
444
81.4k
  }
445
198k
}
446
447
2.32k
static bool zend_have_seen_symbol(zend_string *name, uint32_t kind) {
448
2.32k
  zval *zv = zend_hash_find(&FC(seen_symbols), name);
449
2.32k
  return zv && (Z_LVAL_P(zv) & kind) != 0;
450
2.32k
}
451
452
void init_compiler(void) /* {{{ */
453
268k
{
454
268k
  CG(arena) = zend_arena_create(64 * 1024);
455
268k
  CG(active_op_array) = NULL;
456
268k
  memset(&CG(context), 0, sizeof(CG(context)));
457
268k
  zend_init_compiler_data_structures();
458
268k
  zend_init_rsrc_list();
459
268k
  zend_stream_init();
460
268k
  CG(unclean_shutdown) = 0;
461
462
268k
  CG(delayed_variance_obligations) = NULL;
463
268k
  CG(delayed_autoloads) = NULL;
464
268k
  CG(unlinked_uses) = NULL;
465
268k
  CG(current_linking_class) = NULL;
466
268k
}
467
/* }}} */
468
469
void shutdown_compiler(void) /* {{{ */
470
274k
{
471
  /* Reset filename before destroying the arena, as file cache may use arena allocated strings. */
472
274k
  zend_restore_compiled_filename(NULL);
473
474
274k
  zend_stack_destroy(&CG(loop_var_stack));
475
274k
  zend_stack_destroy(&CG(delayed_oplines_stack));
476
274k
  zend_stack_destroy(&CG(short_circuiting_opnums));
477
478
274k
  if (CG(delayed_variance_obligations)) {
479
250
    zend_hash_destroy(CG(delayed_variance_obligations));
480
250
    FREE_HASHTABLE(CG(delayed_variance_obligations));
481
250
    CG(delayed_variance_obligations) = NULL;
482
250
  }
483
274k
  if (CG(delayed_autoloads)) {
484
252
    zend_hash_destroy(CG(delayed_autoloads));
485
252
    FREE_HASHTABLE(CG(delayed_autoloads));
486
252
    CG(delayed_autoloads) = NULL;
487
252
  }
488
274k
  if (CG(unlinked_uses)) {
489
229
    zend_hash_destroy(CG(unlinked_uses));
490
229
    FREE_HASHTABLE(CG(unlinked_uses));
491
229
    CG(unlinked_uses) = NULL;
492
229
  }
493
274k
  CG(current_linking_class) = NULL;
494
274k
}
495
/* }}} */
496
497
ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
498
171k
{
499
171k
  CG(compiled_filename) = zend_string_copy(new_compiled_filename);
500
171k
  return new_compiled_filename;
501
171k
}
502
/* }}} */
503
504
ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
505
657k
{
506
657k
  if (CG(compiled_filename)) {
507
171k
    zend_string_release(CG(compiled_filename));
508
171k
    CG(compiled_filename) = NULL;
509
171k
  }
510
657k
  CG(compiled_filename) = original_compiled_filename;
511
657k
}
512
/* }}} */
513
514
ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
515
768k
{
516
768k
  return CG(compiled_filename);
517
768k
}
518
/* }}} */
519
520
ZEND_API int zend_get_compiled_lineno(void) /* {{{ */
521
226k
{
522
226k
  return CG(zend_lineno);
523
226k
}
524
/* }}} */
525
526
ZEND_API bool zend_is_compiling(void) /* {{{ */
527
3.52M
{
528
3.52M
  return CG(in_compilation);
529
3.52M
}
530
/* }}} */
531
532
static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */
533
5.45M
{
534
5.45M
  return (uint32_t)CG(active_op_array)->T++;
535
5.45M
}
536
/* }}} */
537
538
1.58M
static int lookup_cv(zend_string *name) /* {{{ */{
539
1.58M
  zend_op_array *op_array = CG(active_op_array);
540
1.58M
  int i = 0;
541
1.58M
  zend_ulong hash_value = zend_string_hash_val(name);
542
543
8.32M
  while (i < op_array->last_var) {
544
7.54M
    if (ZSTR_H(op_array->vars[i]) == hash_value
545
7.54M
     && zend_string_equals(op_array->vars[i], name)) {
546
799k
      return EX_NUM_TO_VAR(i);
547
799k
    }
548
6.74M
    i++;
549
6.74M
  }
550
783k
  i = op_array->last_var;
551
783k
  op_array->last_var++;
552
783k
  if (op_array->last_var > CG(context).vars_size) {
553
200k
    CG(context).vars_size += 16; /* FIXME */
554
200k
    op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
555
200k
  }
556
557
783k
  op_array->vars[i] = zend_string_copy(name);
558
783k
  return EX_NUM_TO_VAR(i);
559
1.58M
}
560
/* }}} */
561
562
zend_string *zval_make_interned_string(zval *zv)
563
5.68M
{
564
5.68M
  ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
565
5.68M
  Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
566
5.68M
  if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
567
5.44M
    Z_TYPE_FLAGS_P(zv) = 0;
568
5.44M
  }
569
5.68M
  return Z_STR_P(zv);
570
5.68M
}
571
572
/* Common part of zend_add_literal and zend_append_individual_literal */
573
static inline void zend_insert_literal(zend_op_array *op_array, zval *zv, int literal_position) /* {{{ */
574
4.86M
{
575
4.86M
  zval *lit = CT_CONSTANT_EX(op_array, literal_position);
576
4.86M
  if (Z_TYPE_P(zv) == IS_STRING) {
577
4.26M
    zval_make_interned_string(zv);
578
4.26M
  }
579
4.86M
  ZVAL_COPY_VALUE(lit, zv);
580
4.86M
  Z_EXTRA_P(lit) = 0;
581
4.86M
}
582
/* }}} */
583
584
/* Is used while compiling a function, using the context to keep track
585
   of an approximate size to avoid to relocate to often.
586
   Literals are truncated to actual size in the second compiler pass (pass_two()). */
587
static int zend_add_literal(zval *zv) /* {{{ */
588
4.86M
{
589
4.86M
  zend_op_array *op_array = CG(active_op_array);
590
4.86M
  int i = op_array->last_literal;
591
4.86M
  op_array->last_literal++;
592
4.86M
  if (i >= CG(context).literals_size) {
593
945k
    while (i >= CG(context).literals_size) {
594
472k
      CG(context).literals_size += 16; /* FIXME */
595
472k
    }
596
472k
    op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
597
472k
  }
598
4.86M
  zend_insert_literal(op_array, zv, i);
599
4.86M
  return i;
600
4.86M
}
601
/* }}} */
602
603
static inline int zend_add_literal_string(zend_string **str) /* {{{ */
604
3.05M
{
605
3.05M
  int ret;
606
3.05M
  zval zv;
607
3.05M
  ZVAL_STR(&zv, *str);
608
3.05M
  ret = zend_add_literal(&zv);
609
3.05M
  *str = Z_STR(zv);
610
3.05M
  return ret;
611
3.05M
}
612
/* }}} */
613
614
static int zend_add_func_name_literal(zend_string *name) /* {{{ */
615
110k
{
616
  /* Original name */
617
110k
  int ret = zend_add_literal_string(&name);
618
619
  /* Lowercased name */
620
110k
  zend_string *lc_name = zend_string_tolower(name);
621
110k
  zend_add_literal_string(&lc_name);
622
623
110k
  return ret;
624
110k
}
625
/* }}} */
626
627
static int zend_add_ns_func_name_literal(zend_string *name) /* {{{ */
628
174k
{
629
174k
  const char *unqualified_name;
630
174k
  size_t unqualified_name_len;
631
632
  /* Original name */
633
174k
  int ret = zend_add_literal_string(&name);
634
635
  /* Lowercased name */
636
174k
  zend_string *lc_name = zend_string_tolower(name);
637
174k
  zend_add_literal_string(&lc_name);
638
639
  /* Lowercased unqualified name */
640
174k
  if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
641
174k
    lc_name = zend_string_alloc(unqualified_name_len, 0);
642
174k
    zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
643
174k
    zend_add_literal_string(&lc_name);
644
174k
  }
645
646
174k
  return ret;
647
174k
}
648
/* }}} */
649
650
static int zend_add_class_name_literal(zend_string *name) /* {{{ */
651
166k
{
652
  /* Original name */
653
166k
  int ret = zend_add_literal_string(&name);
654
655
  /* Lowercased name */
656
166k
  zend_string *lc_name = zend_string_tolower(name);
657
166k
  zend_add_literal_string(&lc_name);
658
659
166k
  return ret;
660
166k
}
661
/* }}} */
662
663
static int zend_add_const_name_literal(zend_string *name, bool unqualified) /* {{{ */
664
712k
{
665
712k
  zend_string *tmp_name;
666
667
712k
  int ret = zend_add_literal_string(&name);
668
669
712k
  size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
670
712k
  const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
671
712k
  if (after_ns) {
672
466k
    after_ns += 1;
673
466k
    ns_len = after_ns - ZSTR_VAL(name) - 1;
674
466k
    after_ns_len = ZSTR_LEN(name) - ns_len - 1;
675
676
    /* lowercased namespace name & original constant name */
677
466k
    tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
678
466k
    zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
679
466k
    zend_add_literal_string(&tmp_name);
680
681
466k
    if (!unqualified) {
682
5.90k
      return ret;
683
5.90k
    }
684
466k
  } else {
685
245k
    after_ns = ZSTR_VAL(name);
686
245k
  }
687
688
  /* original unqualified constant name */
689
706k
  tmp_name = zend_string_init(after_ns, after_ns_len, 0);
690
706k
  zend_add_literal_string(&tmp_name);
691
692
706k
  return ret;
693
712k
}
694
/* }}} */
695
696
18.7k
#define LITERAL_STR(op, str) do { \
697
18.7k
    zval _c; \
698
18.7k
    ZVAL_STR(&_c, str); \
699
18.7k
    op.constant = zend_add_literal(&_c); \
700
18.7k
  } while (0)
701
702
void zend_stop_lexing(void)
703
69
{
704
69
  if (LANG_SCNG(on_event)) {
705
0
    LANG_SCNG(on_event)(ON_STOP, END, 0, NULL, 0, LANG_SCNG(on_event_context));
706
0
  }
707
708
69
  LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
709
69
}
710
711
static inline void zend_begin_loop(
712
    uint8_t free_opcode, const znode *loop_var, bool is_switch) /* {{{ */
713
81.2k
{
714
81.2k
  zend_brk_cont_element *brk_cont_element;
715
81.2k
  int parent = CG(context).current_brk_cont;
716
81.2k
  zend_loop_var info = {0};
717
718
81.2k
  CG(context).current_brk_cont = CG(context).last_brk_cont;
719
81.2k
  brk_cont_element = get_next_brk_cont_element();
720
81.2k
  brk_cont_element->parent = parent;
721
81.2k
  brk_cont_element->is_switch = is_switch;
722
723
81.2k
  if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
724
56.4k
    uint32_t start = get_next_op_number();
725
726
56.4k
    info.opcode = free_opcode;
727
56.4k
    info.var_type = loop_var->op_type;
728
56.4k
    info.var_num = loop_var->u.op.var;
729
56.4k
    brk_cont_element->start = start;
730
56.4k
  } else {
731
24.7k
    info.opcode = ZEND_NOP;
732
    /* The start field is used to free temporary variables in case of exceptions.
733
     * We won't try to free something of we don't have loop variable.  */
734
24.7k
    brk_cont_element->start = -1;
735
24.7k
  }
736
737
81.2k
  zend_stack_push(&CG(loop_var_stack), &info);
738
81.2k
}
739
/* }}} */
740
741
static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
742
81.0k
{
743
81.0k
  uint32_t end = get_next_op_number();
744
81.0k
  zend_brk_cont_element *brk_cont_element
745
81.0k
    = &CG(context).brk_cont_array[CG(context).current_brk_cont];
746
81.0k
  brk_cont_element->cont = cont_addr;
747
81.0k
  brk_cont_element->brk = end;
748
81.0k
  CG(context).current_brk_cont = brk_cont_element->parent;
749
750
81.0k
  zend_stack_del_top(&CG(loop_var_stack));
751
81.0k
}
752
/* }}} */
753
754
static void zend_do_free(znode *op1) /* {{{ */
755
810k
{
756
810k
  if (op1->op_type == IS_TMP_VAR) {
757
377k
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
758
759
412k
    while (opline->opcode == ZEND_END_SILENCE ||
760
412k
           opline->opcode == ZEND_OP_DATA) {
761
35.1k
      opline--;
762
35.1k
    }
763
764
377k
    if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
765
374k
      switch (opline->opcode) {
766
1.89k
        case ZEND_BOOL:
767
6.57k
        case ZEND_BOOL_NOT:
768
          /* boolean results don't have to be freed */
769
6.57k
          return;
770
101
        case ZEND_POST_INC_STATIC_PROP:
771
220
        case ZEND_POST_DEC_STATIC_PROP:
772
556
        case ZEND_POST_INC_OBJ:
773
877
        case ZEND_POST_DEC_OBJ:
774
5.46k
        case ZEND_POST_INC:
775
5.76k
        case ZEND_POST_DEC:
776
          /* convert $i++ to ++$i */
777
5.76k
          opline->opcode -= 2;
778
5.76k
          SET_UNUSED(opline->result);
779
5.76k
          return;
780
130k
        case ZEND_ASSIGN:
781
140k
        case ZEND_ASSIGN_DIM:
782
149k
        case ZEND_ASSIGN_OBJ:
783
151k
        case ZEND_ASSIGN_STATIC_PROP:
784
200k
        case ZEND_ASSIGN_OP:
785
202k
        case ZEND_ASSIGN_DIM_OP:
786
203k
        case ZEND_ASSIGN_OBJ_OP:
787
204k
        case ZEND_ASSIGN_STATIC_PROP_OP:
788
204k
        case ZEND_PRE_INC_STATIC_PROP:
789
204k
        case ZEND_PRE_DEC_STATIC_PROP:
790
204k
        case ZEND_PRE_INC_OBJ:
791
204k
        case ZEND_PRE_DEC_OBJ:
792
207k
        case ZEND_PRE_INC:
793
207k
        case ZEND_PRE_DEC:
794
207k
          SET_UNUSED(opline->result);
795
207k
          return;
796
374k
      }
797
374k
    }
798
799
157k
    zend_emit_op(NULL, ZEND_FREE, op1, NULL);
800
432k
  } else if (op1->op_type == IS_VAR) {
801
343k
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
802
346k
    while (opline->opcode == ZEND_END_SILENCE ||
803
346k
        opline->opcode == ZEND_EXT_FCALL_END ||
804
346k
        opline->opcode == ZEND_OP_DATA) {
805
3.24k
      opline--;
806
3.24k
    }
807
343k
    if (opline->result_type == IS_VAR
808
343k
      && opline->result.var == op1->u.op.var) {
809
315k
      if (opline->opcode == ZEND_FETCH_THIS) {
810
0
        opline->opcode = ZEND_NOP;
811
0
      }
812
315k
      if (!ZEND_OP_IS_FRAMELESS_ICALL(opline->opcode)) {
813
287k
        SET_UNUSED(opline->result);
814
287k
      } else {
815
        /* Frameless calls usually use the return value, so always emit a free. This should be
816
         * faster than checking RETURN_VALUE_USED inside the handler. */
817
        // FIXME: We may actually look at the function signature to determine whether a free
818
        // is necessary.
819
27.4k
        zend_emit_op(NULL, ZEND_FREE, op1, NULL);
820
27.4k
      }
821
315k
    } else {
822
94.3k
      while (opline >= CG(active_op_array)->opcodes) {
823
94.3k
        if ((opline->opcode == ZEND_FETCH_LIST_R ||
824
94.3k
             opline->opcode == ZEND_FETCH_LIST_W) &&
825
94.3k
            opline->op1_type == IS_VAR &&
826
94.3k
            opline->op1.var == op1->u.op.var) {
827
12.0k
          zend_emit_op(NULL, ZEND_FREE, op1, NULL);
828
12.0k
          return;
829
12.0k
        }
830
82.2k
        if (opline->result_type == IS_VAR
831
82.2k
          && opline->result.var == op1->u.op.var) {
832
15.9k
          if (opline->opcode == ZEND_NEW) {
833
15.9k
            zend_emit_op(NULL, ZEND_FREE, op1, NULL);
834
15.9k
          }
835
15.9k
          break;
836
15.9k
        }
837
66.3k
        opline--;
838
66.3k
      }
839
27.9k
    }
840
343k
  } else if (op1->op_type == IS_CONST) {
841
    /* Destroy value without using GC: When opcache moves arrays into SHM it will
842
     * free the zend_array structure, so references to it from outside the op array
843
     * become invalid. GC would cause such a reference in the root buffer. */
844
85.4k
    zval_ptr_dtor_nogc(&op1->u.constant);
845
85.4k
  }
846
810k
}
847
/* }}} */
848
849
850
static const char *zend_modifier_token_to_string(uint32_t token)
851
102
{
852
102
  switch (token) {
853
6
    case T_PUBLIC:
854
6
      return "public";
855
6
    case T_PROTECTED:
856
6
      return "protected";
857
3
    case T_PRIVATE:
858
3
      return "private";
859
20
    case T_STATIC:
860
20
      return "static";
861
19
    case T_FINAL:
862
19
      return "final";
863
27
    case T_READONLY:
864
27
      return "readonly";
865
13
    case T_ABSTRACT:
866
13
      return "abstract";
867
1
    case T_PUBLIC_SET:
868
1
      return "public(set)";
869
1
    case T_PROTECTED_SET:
870
1
      return "protected(set)";
871
6
    case T_PRIVATE_SET:
872
6
      return "private(set)";
873
102
    EMPTY_SWITCH_DEFAULT_CASE()
874
102
  }
875
102
}
876
877
uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t token)
878
61.6k
{
879
61.6k
  switch (token) {
880
39.9k
    case T_PUBLIC:
881
39.9k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
882
39.9k
        return ZEND_ACC_PUBLIC;
883
39.9k
      }
884
6
      break;
885
2.25k
    case T_PROTECTED:
886
2.25k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
887
2.24k
        return ZEND_ACC_PROTECTED;
888
2.24k
      }
889
6
      break;
890
5.00k
    case T_PRIVATE:
891
5.00k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
892
5.00k
        return ZEND_ACC_PRIVATE;
893
5.00k
      }
894
3
      break;
895
1.01k
    case T_READONLY:
896
1.01k
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
897
1.00k
        return ZEND_ACC_READONLY;
898
1.00k
      }
899
17
      break;
900
1.39k
    case T_ABSTRACT:
901
1.39k
      if (target == ZEND_MODIFIER_TARGET_METHOD || target == ZEND_MODIFIER_TARGET_PROPERTY) {
902
1.39k
        return ZEND_ACC_ABSTRACT;
903
1.39k
      }
904
4
      break;
905
1.20k
    case T_FINAL:
906
1.20k
      return ZEND_ACC_FINAL;
907
9.97k
    case T_STATIC:
908
9.97k
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_METHOD) {
909
9.96k
        return ZEND_ACC_STATIC;
910
9.96k
      }
911
11
      break;
912
217
    case T_PUBLIC_SET:
913
217
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
914
216
        return ZEND_ACC_PUBLIC_SET;
915
216
      }
916
1
      break;
917
181
    case T_PROTECTED_SET:
918
181
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
919
180
        return ZEND_ACC_PROTECTED_SET;
920
180
      }
921
1
      break;
922
425
    case T_PRIVATE_SET:
923
425
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
924
419
        return ZEND_ACC_PRIVATE_SET;
925
419
      }
926
6
      break;
927
61.6k
  }
928
929
55
  char *member;
930
55
  if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
931
0
    member = "property";
932
55
  } else if (target == ZEND_MODIFIER_TARGET_METHOD) {
933
13
    member = "method";
934
42
  } else if (target == ZEND_MODIFIER_TARGET_CONSTANT) {
935
7
    member = "class constant";
936
35
  } else if (target == ZEND_MODIFIER_TARGET_CPP) {
937
8
    member = "parameter";
938
27
  } else if (target == ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
939
27
    member = "property hook";
940
27
  } else {
941
0
    ZEND_UNREACHABLE();
942
0
  }
943
944
55
  zend_throw_exception_ex(zend_ce_compile_error, 0,
945
55
    "Cannot use the %s modifier on a %s", zend_modifier_token_to_string(token), member);
946
55
  return 0;
947
55
}
948
949
uint32_t zend_modifier_list_to_flags(zend_modifier_target target, zend_ast *modifiers)
950
53.0k
{
951
53.0k
  uint32_t flags = 0;
952
53.0k
  zend_ast_list *modifier_list = zend_ast_get_list(modifiers);
953
954
114k
  for (uint32_t i = 0; i < modifier_list->children; i++) {
955
61.1k
    uint32_t token = (uint32_t) Z_LVAL_P(zend_ast_get_zval(modifier_list->child[i]));
956
61.1k
    uint32_t new_flag = zend_modifier_token_to_flag(target, token);
957
61.1k
    if (!new_flag) {
958
48
      return 0;
959
48
    }
960
    /* Don't error immediately for duplicate flags, we want to prioritize the errors from zend_add_member_modifier(). */
961
61.0k
    bool duplicate_flag = (flags & new_flag);
962
61.0k
    flags = zend_add_member_modifier(flags, new_flag, target);
963
61.0k
    if (!flags) {
964
72
      return 0;
965
72
    }
966
61.0k
    if (duplicate_flag) {
967
47
      zend_throw_exception_ex(zend_ce_compile_error, 0,
968
47
        "Multiple %s modifiers are not allowed", zend_modifier_token_to_string(token));
969
47
      return 0;
970
47
    }
971
61.0k
  }
972
973
52.8k
  return flags;
974
53.0k
}
975
976
uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
977
148
{
978
148
  uint32_t new_flags = flags | new_flag;
979
148
  if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
980
1
    zend_throw_exception(zend_ce_compile_error,
981
1
      "Multiple abstract modifiers are not allowed", 0);
982
1
    return 0;
983
1
  }
984
147
  if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
985
9
    zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
986
9
    return 0;
987
9
  }
988
138
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
989
6
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
990
6
    return 0;
991
6
  }
992
132
  if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
993
6
    zend_throw_exception(zend_ce_compile_error,
994
6
      "Cannot use the final modifier on an abstract class", 0);
995
6
    return 0;
996
6
  }
997
126
  return new_flags;
998
132
}
999
/* }}} */
1000
1001
uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag)
1002
90
{
1003
90
  uint32_t new_flags = flags | new_flag;
1004
90
  if (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
1005
6
    zend_throw_exception(zend_ce_compile_error,
1006
6
      "Cannot use the abstract modifier on an anonymous class", 0);
1007
6
    return 0;
1008
6
  }
1009
84
  if (new_flag & ZEND_ACC_FINAL) {
1010
6
    zend_throw_exception(zend_ce_compile_error, "Cannot use the final modifier on an anonymous class", 0);
1011
6
    return 0;
1012
6
  }
1013
78
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
1014
6
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
1015
6
    return 0;
1016
6
  }
1017
72
  return new_flags;
1018
78
}
1019
1020
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target) /* {{{ */
1021
61.0k
{
1022
61.0k
  uint32_t new_flags = flags | new_flag;
1023
61.0k
  if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
1024
42
    zend_throw_exception(zend_ce_compile_error,
1025
42
      "Multiple access type modifiers are not allowed", 0);
1026
42
    return 0;
1027
42
  }
1028
61.0k
  if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
1029
12
    if (target == ZEND_MODIFIER_TARGET_METHOD) {
1030
6
      zend_throw_exception(zend_ce_compile_error,
1031
6
        "Cannot use the final modifier on an abstract method", 0);
1032
6
      return 0;
1033
6
    }
1034
6
    if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
1035
6
      zend_throw_exception(zend_ce_compile_error,
1036
6
        "Cannot use the final modifier on an abstract property", 0);
1037
6
      return 0;
1038
6
    }
1039
6
  }
1040
61.0k
  if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
1041
29.3k
    if ((flags & ZEND_ACC_PPP_SET_MASK) && (new_flag & ZEND_ACC_PPP_SET_MASK)) {
1042
18
      zend_throw_exception(zend_ce_compile_error,
1043
18
        "Multiple access type modifiers are not allowed", 0);
1044
18
      return 0;
1045
18
    }
1046
29.3k
  }
1047
61.0k
  return new_flags;
1048
61.0k
}
1049
/* }}} */
1050
1051
19.2k
ZEND_API zend_string *zend_create_member_string(zend_string *class_name, zend_string *member_name) {
1052
19.2k
  return zend_string_concat3(
1053
19.2k
    ZSTR_VAL(class_name), ZSTR_LEN(class_name),
1054
19.2k
    "::", sizeof("::") - 1,
1055
19.2k
    ZSTR_VAL(member_name), ZSTR_LEN(member_name));
1056
19.2k
}
1057
1058
1.23M
static zend_string *zend_concat_names(char *name1, size_t name1_len, char *name2, size_t name2_len) {
1059
1.23M
  return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len);
1060
1.23M
}
1061
1062
2.09M
static zend_string *zend_prefix_with_ns(zend_string *name) {
1063
2.09M
  if (FC(current_namespace)) {
1064
1.23M
    zend_string *ns = FC(current_namespace);
1065
1.23M
    return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
1066
1.23M
  } else {
1067
856k
    return zend_string_copy(name);
1068
856k
  }
1069
2.09M
}
1070
1071
static zend_string *zend_resolve_non_class_name(
1072
  zend_string *name, uint32_t type, bool *is_fully_qualified,
1073
  bool case_sensitive, HashTable *current_import_sub
1074
1.27M
) {
1075
1.27M
  char *compound;
1076
1.27M
  *is_fully_qualified = 0;
1077
1078
1.27M
  if (ZSTR_VAL(name)[0] == '\\') {
1079
    /* Remove \ prefix (only relevant if this is a string rather than a label) */
1080
687
    *is_fully_qualified = 1;
1081
687
    return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1082
687
  }
1083
1084
1.27M
  if (type == ZEND_NAME_FQ) {
1085
14.1k
    *is_fully_qualified = 1;
1086
14.1k
    return zend_string_copy(name);
1087
14.1k
  }
1088
1089
1.26M
  if (type == ZEND_NAME_RELATIVE) {
1090
1.29k
    *is_fully_qualified = 1;
1091
1.29k
    return zend_prefix_with_ns(name);
1092
1.29k
  }
1093
1094
1.25M
  if (current_import_sub) {
1095
    /* If an unqualified name is a function/const alias, replace it. */
1096
2.16k
    zend_string *import_name;
1097
2.16k
    if (case_sensitive) {
1098
1.66k
      import_name = zend_hash_find_ptr(current_import_sub, name);
1099
1.66k
    } else {
1100
503
      import_name = zend_hash_find_ptr_lc(current_import_sub, name);
1101
503
    }
1102
1103
2.16k
    if (import_name) {
1104
905
      *is_fully_qualified = 1;
1105
905
      return zend_string_copy(import_name);
1106
905
    }
1107
2.16k
  }
1108
1109
1.25M
  compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1110
1.25M
  if (compound) {
1111
11.6k
    *is_fully_qualified = 1;
1112
11.6k
  }
1113
1114
1.25M
  if (compound && FC(imports)) {
1115
    /* If the first part of a qualified name is an alias, substitute it. */
1116
4.22k
    size_t len = compound - ZSTR_VAL(name);
1117
4.22k
    zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1118
1119
4.22k
    if (import_name) {
1120
253
      return zend_concat_names(
1121
253
        ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1122
253
    }
1123
4.22k
  }
1124
1125
1.25M
  return zend_prefix_with_ns(name);
1126
1.25M
}
1127
/* }}} */
1128
1129
static zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1130
386k
{
1131
386k
  return zend_resolve_non_class_name(
1132
386k
    name, type, is_fully_qualified, 0, FC(imports_function));
1133
386k
}
1134
1135
static zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1136
889k
{
1137
889k
  return zend_resolve_non_class_name(
1138
889k
    name, type, is_fully_qualified, 1, FC(imports_const));
1139
889k
}
1140
1141
static zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
1142
762k
{
1143
762k
  char *compound;
1144
1145
762k
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1146
3.59k
    if (type == ZEND_NAME_FQ) {
1147
6
      zend_error_noreturn(E_COMPILE_ERROR,
1148
6
        "'\\%s' is an invalid class name", ZSTR_VAL(name));
1149
6
    }
1150
3.58k
    if (type == ZEND_NAME_RELATIVE) {
1151
0
      zend_error_noreturn(E_COMPILE_ERROR,
1152
0
        "'namespace\\%s' is an invalid class name", ZSTR_VAL(name));
1153
0
    }
1154
3.58k
    ZEND_ASSERT(type == ZEND_NAME_NOT_FQ);
1155
3.58k
    return zend_string_copy(name);
1156
3.58k
  }
1157
1158
759k
  if (type == ZEND_NAME_RELATIVE) {
1159
460
    return zend_prefix_with_ns(name);
1160
460
  }
1161
1162
758k
  if (type == ZEND_NAME_FQ) {
1163
12.9k
    if (ZSTR_VAL(name)[0] == '\\') {
1164
      /* Remove \ prefix (only relevant if this is a string rather than a label) */
1165
90
      name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1166
90
      if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1167
1
        zend_error_noreturn(E_COMPILE_ERROR,
1168
1
          "'\\%s' is an invalid class name", ZSTR_VAL(name));
1169
1
      }
1170
89
      return name;
1171
90
    }
1172
1173
12.8k
    return zend_string_copy(name);
1174
12.9k
  }
1175
1176
745k
  if (FC(imports)) {
1177
4.03k
    compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1178
4.03k
    if (compound) {
1179
      /* If the first part of a qualified name is an alias, substitute it. */
1180
931
      size_t len = compound - ZSTR_VAL(name);
1181
931
      zend_string *import_name =
1182
931
        zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1183
1184
931
      if (import_name) {
1185
462
        return zend_concat_names(
1186
462
          ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1187
462
      }
1188
3.10k
    } else {
1189
      /* If an unqualified name is an alias, replace it. */
1190
3.10k
      zend_string *import_name
1191
3.10k
        = zend_hash_find_ptr_lc(FC(imports), name);
1192
1193
3.10k
      if (import_name) {
1194
970
        return zend_string_copy(import_name);
1195
970
      }
1196
3.10k
    }
1197
4.03k
  }
1198
1199
  /* If not fully qualified and not an alias, prepend the current namespace */
1200
744k
  return zend_prefix_with_ns(name);
1201
745k
}
1202
/* }}} */
1203
1204
static zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
1205
727k
{
1206
727k
  zval *class_name = zend_ast_get_zval(ast);
1207
727k
  if (Z_TYPE_P(class_name) != IS_STRING) {
1208
28
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1209
28
  }
1210
727k
  return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
1211
727k
}
1212
/* }}} */
1213
1214
static void label_ptr_dtor(zval *zv) /* {{{ */
1215
4.35k
{
1216
4.35k
  efree_size(Z_PTR_P(zv), sizeof(zend_label));
1217
4.35k
}
1218
/* }}} */
1219
1220
1.96k
static void str_dtor(zval *zv)  /* {{{ */ {
1221
1.96k
  zend_string_release_ex(Z_STR_P(zv), 0);
1222
1.96k
}
1223
/* }}} */
1224
1225
static bool zend_is_call(zend_ast *ast);
1226
1227
static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
1228
54.6k
{
1229
54.6k
  zend_op_array *op_array = CG(active_op_array);
1230
54.6k
  uint32_t try_catch_offset = op_array->last_try_catch++;
1231
54.6k
  zend_try_catch_element *elem;
1232
1233
54.6k
  op_array->try_catch_array = safe_erealloc(
1234
54.6k
    op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
1235
1236
54.6k
  elem = &op_array->try_catch_array[try_catch_offset];
1237
54.6k
  elem->try_op = try_op;
1238
54.6k
  elem->catch_op = 0;
1239
54.6k
  elem->finally_op = 0;
1240
54.6k
  elem->finally_end = 0;
1241
1242
54.6k
  return try_catch_offset;
1243
54.6k
}
1244
/* }}} */
1245
1246
ZEND_API void function_add_ref(zend_function *function) /* {{{ */
1247
1.70k
{
1248
1.70k
  if (function->type == ZEND_USER_FUNCTION) {
1249
1.70k
    zend_op_array *op_array = &function->op_array;
1250
1.70k
    if (op_array->refcount) {
1251
168
      (*op_array->refcount)++;
1252
168
    }
1253
1254
1.70k
    ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
1255
1.70k
    ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
1256
1.70k
  }
1257
1258
1.70k
  if (function->common.function_name) {
1259
1.70k
    zend_string_addref(function->common.function_name);
1260
1.70k
  }
1261
1.70k
}
1262
/* }}} */
1263
1264
static zend_never_inline ZEND_COLD ZEND_NORETURN void do_bind_function_error(zend_string *lcname, zend_op_array *op_array, bool compile_time) /* {{{ */
1265
111
{
1266
111
  zval *zv = zend_hash_find_known_hash(compile_time ? CG(function_table) : EG(function_table), lcname);
1267
111
  int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
1268
111
  zend_function *old_function;
1269
1270
111
  ZEND_ASSERT(zv != NULL);
1271
111
  old_function = (zend_function*)Z_PTR_P(zv);
1272
111
  if (old_function->type == ZEND_USER_FUNCTION
1273
111
    && old_function->op_array.last > 0) {
1274
105
    zend_error_noreturn(error_level, "Cannot redeclare function %s() (previously declared in %s:%d)",
1275
105
          op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name),
1276
105
          ZSTR_VAL(old_function->op_array.filename),
1277
105
          old_function->op_array.line_start);
1278
105
  } else {
1279
6
    zend_error_noreturn(error_level, "Cannot redeclare function %s()",
1280
6
      op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name));
1281
6
  }
1282
111
}
1283
1284
ZEND_API zend_result do_bind_function(zend_function *func, zval *lcname) /* {{{ */
1285
81
{
1286
81
  zend_function *added_func = zend_hash_add_ptr(EG(function_table), Z_STR_P(lcname), func);
1287
81
  if (UNEXPECTED(!added_func)) {
1288
0
    do_bind_function_error(Z_STR_P(lcname), &func->op_array, 0);
1289
0
    return FAILURE;
1290
0
  }
1291
1292
81
  if (func->op_array.refcount) {
1293
19
    ++*func->op_array.refcount;
1294
19
  }
1295
81
  if (func->common.function_name) {
1296
81
    zend_string_addref(func->common.function_name);
1297
81
  }
1298
81
  zend_observer_function_declared_notify(&func->op_array, Z_STR_P(lcname));
1299
81
  return SUCCESS;
1300
81
}
1301
/* }}} */
1302
1303
ZEND_API zend_class_entry *zend_bind_class_in_slot(
1304
    zval *class_table_slot, zval *lcname, zend_string *lc_parent_name)
1305
6.52k
{
1306
6.52k
  zend_class_entry *ce = Z_PTR_P(class_table_slot);
1307
6.52k
  bool is_preloaded =
1308
6.52k
    (ce->ce_flags & ZEND_ACC_PRELOADED) && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD);
1309
6.52k
  bool success;
1310
6.52k
  if (EXPECTED(!is_preloaded)) {
1311
6.52k
    success = zend_hash_set_bucket_key(EG(class_table), (Bucket*) class_table_slot, Z_STR_P(lcname)) != NULL;
1312
6.52k
  } else {
1313
    /* If preloading is used, don't replace the existing bucket, add a new one. */
1314
0
    success = zend_hash_add_ptr(EG(class_table), Z_STR_P(lcname), ce) != NULL;
1315
0
  }
1316
6.52k
  if (UNEXPECTED(!success)) {
1317
129
    zend_class_entry *old_class = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1318
129
    ZEND_ASSERT(old_class);
1319
129
    zend_class_redeclaration_error(E_COMPILE_ERROR, old_class);
1320
129
    return NULL;
1321
129
  }
1322
1323
6.39k
  if (ce->ce_flags & ZEND_ACC_LINKED) {
1324
244
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1325
244
    return ce;
1326
244
  }
1327
1328
6.14k
  ce = zend_do_link_class(ce, lc_parent_name, Z_STR_P(lcname));
1329
6.14k
  if (ce) {
1330
4.84k
    ZEND_ASSERT(!EG(exception));
1331
4.84k
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1332
4.84k
    return ce;
1333
4.84k
  }
1334
1335
1.30k
  if (!is_preloaded) {
1336
    /* Reload bucket pointer, the hash table may have been reallocated */
1337
290
    zval *zv = zend_hash_find(EG(class_table), Z_STR_P(lcname));
1338
290
    zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(lcname + 1));
1339
1.01k
  } else {
1340
1.01k
    zend_hash_del(EG(class_table), Z_STR_P(lcname));
1341
1.01k
  }
1342
1.30k
  return NULL;
1343
6.14k
}
1344
1345
ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */
1346
5.88k
{
1347
5.88k
  zend_class_entry *ce;
1348
5.88k
  zval *rtd_key, *zv;
1349
1350
5.88k
  rtd_key = lcname + 1;
1351
1352
5.88k
  zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(rtd_key));
1353
1354
5.88k
  if (UNEXPECTED(!zv)) {
1355
11
    ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1356
11
    ZEND_ASSERT(ce);
1357
11
    zend_class_redeclaration_error(E_COMPILE_ERROR, ce);
1358
11
    return FAILURE;
1359
11
  }
1360
1361
  /* Register the derived class */
1362
5.87k
  return zend_bind_class_in_slot(zv, lcname, lc_parent_name) ? SUCCESS : FAILURE;
1363
5.88k
}
1364
/* }}} */
1365
1366
146k
static zend_string *add_type_string(zend_string *type, zend_string *new_type, bool is_intersection) {
1367
146k
  zend_string *result;
1368
146k
  if (type == NULL) {
1369
130k
    return zend_string_copy(new_type);
1370
130k
  }
1371
1372
15.9k
  if (is_intersection) {
1373
2.62k
    result = zend_string_concat3(ZSTR_VAL(type), ZSTR_LEN(type),
1374
2.62k
      "&", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1375
2.62k
    zend_string_release(type);
1376
13.3k
  } else {
1377
13.3k
    result = zend_string_concat3(
1378
13.3k
      ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1379
13.3k
    zend_string_release(type);
1380
13.3k
  }
1381
15.9k
  return result;
1382
146k
}
1383
1384
16.6k
static zend_string *resolve_class_name(zend_string *name, zend_class_entry *scope) {
1385
16.6k
  if (scope) {
1386
2.14k
    if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1387
21
      name = scope->name;
1388
2.12k
    } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT)) && scope->parent) {
1389
0
      name = scope->parent->name;
1390
0
    }
1391
2.14k
  }
1392
1393
  /* The resolved name for anonymous classes contains null bytes. Cut off everything after the
1394
   * null byte here, to avoid larger parts of the type being omitted by printing code later. */
1395
16.6k
  size_t len = strlen(ZSTR_VAL(name));
1396
16.6k
  if (len != ZSTR_LEN(name)) {
1397
10
    ZEND_ASSERT(scope && "This should only happen with resolved types");
1398
10
    return zend_string_init(ZSTR_VAL(name), len, 0);
1399
10
  }
1400
16.6k
  return zend_string_copy(name);
1401
16.6k
}
1402
1403
static zend_string *add_intersection_type(zend_string *str,
1404
  const zend_type_list *intersection_type_list, zend_class_entry *scope,
1405
  bool is_bracketed)
1406
1.43k
{
1407
1.43k
  const zend_type *single_type;
1408
1.43k
  zend_string *intersection_str = NULL;
1409
1410
5.50k
  ZEND_TYPE_LIST_FOREACH(intersection_type_list, single_type) {
1411
5.50k
    ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*single_type));
1412
4.06k
    ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*single_type));
1413
1414
4.06k
    intersection_str = add_type_string(intersection_str, ZEND_TYPE_NAME(*single_type), /* is_intersection */ true);
1415
4.06k
  } ZEND_TYPE_LIST_FOREACH_END();
1416
1417
1.43k
  ZEND_ASSERT(intersection_str);
1418
1419
1.43k
  if (is_bracketed) {
1420
1.07k
    zend_string *result = zend_string_concat3("(", 1, ZSTR_VAL(intersection_str), ZSTR_LEN(intersection_str), ")", 1);
1421
1.07k
    zend_string_release(intersection_str);
1422
1.07k
    intersection_str = result;
1423
1.07k
  }
1424
1.43k
  str = add_type_string(str, intersection_str, /* is_intersection */ false);
1425
1.43k
  zend_string_release(intersection_str);
1426
1.43k
  return str;
1427
1.43k
}
1428
1429
144k
zend_string *zend_type_to_string_resolved(const zend_type type, zend_class_entry *scope) {
1430
144k
  zend_string *str = NULL;
1431
1432
  /* Pure intersection type */
1433
144k
  if (ZEND_TYPE_IS_INTERSECTION(type)) {
1434
358
    ZEND_ASSERT(!ZEND_TYPE_IS_UNION(type));
1435
358
    str = add_intersection_type(str, ZEND_TYPE_LIST(type), scope, /* is_bracketed */ false);
1436
144k
  } else if (ZEND_TYPE_HAS_LIST(type)) {
1437
    /* A union type might not be a list */
1438
567
    const zend_type *list_type;
1439
2.72k
    ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
1440
2.72k
      if (ZEND_TYPE_IS_INTERSECTION(*list_type)) {
1441
1.07k
        str = add_intersection_type(str, ZEND_TYPE_LIST(*list_type), scope, /* is_bracketed */ true);
1442
1.07k
        continue;
1443
1.07k
      }
1444
1.07k
      ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type));
1445
1.07k
      ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*list_type));
1446
1447
1.07k
      zend_string *name = ZEND_TYPE_NAME(*list_type);
1448
1.07k
      zend_string *resolved = resolve_class_name(name, scope);
1449
1.07k
      str = add_type_string(str, resolved, /* is_intersection */ false);
1450
1.07k
      zend_string_release(resolved);
1451
1.07k
    } ZEND_TYPE_LIST_FOREACH_END();
1452
143k
  } else if (ZEND_TYPE_HAS_NAME(type)) {
1453
15.5k
    str = resolve_class_name(ZEND_TYPE_NAME(type), scope);
1454
15.5k
  }
1455
1456
144k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
1457
1458
144k
  if (type_mask == MAY_BE_ANY) {
1459
5.50k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_MIXED), /* is_intersection */ false);
1460
1461
5.50k
    return str;
1462
5.50k
  }
1463
139k
  if (type_mask & MAY_BE_STATIC) {
1464
643
    zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC);
1465
    // During compilation of eval'd code the called scope refers to the scope calling the eval
1466
643
    if (scope && !zend_is_compiling()) {
1467
39
      zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
1468
39
      if (called_scope) {
1469
18
        name = called_scope->name;
1470
18
      }
1471
39
    }
1472
643
    str = add_type_string(str, name, /* is_intersection */ false);
1473
643
  }
1474
139k
  if (type_mask & MAY_BE_CALLABLE) {
1475
2.83k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE), /* is_intersection */ false);
1476
2.83k
  }
1477
139k
  if (type_mask & MAY_BE_OBJECT) {
1478
11.4k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT), /* is_intersection */ false);
1479
11.4k
  }
1480
139k
  if (type_mask & MAY_BE_ARRAY) {
1481
14.2k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY), /* is_intersection */ false);
1482
14.2k
  }
1483
139k
  if (type_mask & MAY_BE_STRING) {
1484
37.4k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING), /* is_intersection */ false);
1485
37.4k
  }
1486
139k
  if (type_mask & MAY_BE_LONG) {
1487
24.8k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT), /* is_intersection */ false);
1488
24.8k
  }
1489
139k
  if (type_mask & MAY_BE_DOUBLE) {
1490
264
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT), /* is_intersection */ false);
1491
264
  }
1492
139k
  if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
1493
30.1k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL), /* is_intersection */ false);
1494
109k
  } else if (type_mask & MAY_BE_FALSE) {
1495
7.39k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE), /* is_intersection */ false);
1496
101k
  } else if (type_mask & MAY_BE_TRUE) {
1497
48
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_TRUE), /* is_intersection */ false);
1498
48
  }
1499
139k
  if (type_mask & MAY_BE_VOID) {
1500
4.80k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID), /* is_intersection */ false);
1501
4.80k
  }
1502
139k
  if (type_mask & MAY_BE_NEVER) {
1503
19
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NEVER), /* is_intersection */ false);
1504
19
  }
1505
1506
139k
  if (type_mask & MAY_BE_NULL) {
1507
15.2k
    bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
1508
15.2k
    bool has_intersection = !str || memchr(ZSTR_VAL(str), '&', ZSTR_LEN(str)) != NULL;
1509
15.2k
    if (!is_union && !has_intersection) {
1510
15.0k
      zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
1511
15.0k
      zend_string_release(str);
1512
15.0k
      return nullable_str;
1513
15.0k
    }
1514
1515
156
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE), /* is_intersection */ false);
1516
156
  }
1517
124k
  return str;
1518
139k
}
1519
1520
139k
ZEND_API zend_string *zend_type_to_string(zend_type type) {
1521
139k
  return zend_type_to_string_resolved(type, NULL);
1522
139k
}
1523
1524
1.01k
static bool is_generator_compatible_class_type(const zend_string *name) {
1525
1.01k
  return zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_TRAVERSABLE))
1526
1.01k
    || zend_string_equals_literal_ci(name, "Iterator")
1527
1.01k
    || zend_string_equals_literal_ci(name, "Generator");
1528
1.01k
}
1529
1530
static void zend_mark_function_as_generator(void) /* {{{ */
1531
11.7k
{
1532
11.7k
  if (!CG(active_op_array)->function_name) {
1533
84
    zend_error_noreturn(E_COMPILE_ERROR,
1534
84
      "The \"yield\" expression can only be used inside a function");
1535
84
  }
1536
1537
11.6k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
1538
803
    const zend_type return_type = CG(active_op_array)->arg_info[-1].type;
1539
803
    bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_OBJECT) != 0;
1540
803
    if (!valid_type) {
1541
773
      const zend_type *single_type;
1542
1.79k
      ZEND_TYPE_FOREACH(return_type, single_type) {
1543
1.79k
        if (ZEND_TYPE_HAS_NAME(*single_type)
1544
1.01k
            && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) {
1545
724
          valid_type = 1;
1546
724
          break;
1547
724
        }
1548
1.79k
      } ZEND_TYPE_FOREACH_END();
1549
773
    }
1550
1551
803
    if (!valid_type) {
1552
49
      zend_string *str = zend_type_to_string(return_type);
1553
49
      zend_error_noreturn(E_COMPILE_ERROR,
1554
49
        "Generator return type must be a supertype of Generator, %s given",
1555
49
        ZSTR_VAL(str));
1556
49
    }
1557
803
  }
1558
1559
11.6k
  CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR;
1560
11.6k
}
1561
/* }}} */
1562
1563
ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal) /* {{{ */
1564
72.9k
{
1565
72.9k
  size_t prop_name_length = 1 + src1_length + 1 + src2_length;
1566
72.9k
  zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
1567
1568
72.9k
  ZSTR_VAL(prop_name)[0] = '\0';
1569
72.9k
  memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
1570
72.9k
  memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
1571
72.9k
  return prop_name;
1572
72.9k
}
1573
/* }}} */
1574
1575
ZEND_API zend_result zend_unmangle_property_name_ex(const zend_string *name, const char **class_name, const char **prop_name, size_t *prop_len) /* {{{ */
1576
1.08M
{
1577
1.08M
  size_t class_name_len;
1578
1.08M
  size_t anonclass_src_len;
1579
1580
1.08M
  *class_name = NULL;
1581
1582
1.08M
  if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
1583
983k
    *prop_name = ZSTR_VAL(name);
1584
983k
    if (prop_len) {
1585
662k
      *prop_len = ZSTR_LEN(name);
1586
662k
    }
1587
983k
    return SUCCESS;
1588
983k
  }
1589
104k
  if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
1590
2.39k
    zend_error(E_NOTICE, "Illegal member variable name");
1591
2.39k
    *prop_name = ZSTR_VAL(name);
1592
2.39k
    if (prop_len) {
1593
2.34k
      *prop_len = ZSTR_LEN(name);
1594
2.34k
    }
1595
2.39k
    return FAILURE;
1596
2.39k
  }
1597
1598
102k
  class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
1599
102k
  if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
1600
858
    zend_error(E_NOTICE, "Corrupt member variable name");
1601
858
    *prop_name = ZSTR_VAL(name);
1602
858
    if (prop_len) {
1603
747
      *prop_len = ZSTR_LEN(name);
1604
747
    }
1605
858
    return FAILURE;
1606
858
  }
1607
1608
101k
  *class_name = ZSTR_VAL(name) + 1;
1609
101k
  anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2);
1610
101k
  if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) {
1611
21.8k
    class_name_len += anonclass_src_len + 1;
1612
21.8k
  }
1613
101k
  *prop_name = ZSTR_VAL(name) + class_name_len + 2;
1614
101k
  if (prop_len) {
1615
63.0k
    *prop_len = ZSTR_LEN(name) - class_name_len - 2;
1616
63.0k
  }
1617
101k
  return SUCCESS;
1618
102k
}
1619
/* }}} */
1620
1621
static bool array_is_const_ex(zend_array *array, uint32_t *max_checks)
1622
41
{
1623
41
  if (zend_hash_num_elements(array) > *max_checks) {
1624
0
    return false;
1625
0
  }
1626
41
  *max_checks -= zend_hash_num_elements(array);
1627
1628
41
  zval *element;
1629
87
  ZEND_HASH_FOREACH_VAL(array, element) {
1630
87
    if (Z_TYPE_P(element) < IS_ARRAY) {
1631
23
      continue;
1632
23
    } else if (Z_TYPE_P(element) == IS_ARRAY) {
1633
0
      if (!array_is_const_ex(array, max_checks)) {
1634
0
        return false;
1635
0
      }
1636
0
    } else {
1637
0
      return false;
1638
0
    }
1639
87
  } ZEND_HASH_FOREACH_END();
1640
1641
41
  return true;
1642
41
}
1643
1644
static bool array_is_const(zend_array *array)
1645
41
{
1646
41
  uint32_t max_checks = 50;
1647
41
  return array_is_const_ex(array, &max_checks);
1648
41
}
1649
1650
11.4k
static bool can_ct_eval_const(zend_constant *c) {
1651
11.4k
  if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
1652
270
    return 0;
1653
270
  }
1654
11.1k
  if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
1655
11.1k
      && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
1656
11.1k
      && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
1657
10.4k
        && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
1658
10.4k
    return 1;
1659
10.4k
  }
1660
715
  if (Z_TYPE(c->value) < IS_ARRAY
1661
715
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1662
10
    return 1;
1663
705
  } else if (Z_TYPE(c->value) == IS_ARRAY
1664
705
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)
1665
705
      && array_is_const(Z_ARR(c->value))) {
1666
0
    return 1;
1667
0
  }
1668
705
  return 0;
1669
715
}
1670
1671
static bool zend_try_ct_eval_const(zval *zv, zend_string *name, bool is_fully_qualified) /* {{{ */
1672
889k
{
1673
  /* Substitute true, false and null (including unqualified usage in namespaces)
1674
   * before looking up the possibly namespaced name. */
1675
889k
  const char *lookup_name = ZSTR_VAL(name);
1676
889k
  size_t lookup_len = ZSTR_LEN(name);
1677
1678
889k
  if (!is_fully_qualified) {
1679
874k
    zend_get_unqualified_name(name, &lookup_name, &lookup_len);
1680
874k
  }
1681
1682
889k
  zend_constant *c;
1683
889k
  if ((c = zend_get_special_const(lookup_name, lookup_len))) {
1684
25.6k
    ZVAL_COPY_VALUE(zv, &c->value);
1685
25.6k
    return 1;
1686
25.6k
  }
1687
863k
  c = zend_hash_find_ptr(EG(zend_constants), name);
1688
863k
  if (c && can_ct_eval_const(c)) {
1689
10.4k
    ZVAL_COPY_OR_DUP(zv, &c->value);
1690
10.4k
    return 1;
1691
10.4k
  }
1692
853k
  return 0;
1693
863k
}
1694
/* }}} */
1695
1696
static inline bool zend_is_scope_known(void) /* {{{ */
1697
15.1k
{
1698
15.1k
  if (!CG(active_op_array)) {
1699
    /* This can only happen when evaluating a default value string. */
1700
0
    return 0;
1701
0
  }
1702
1703
15.1k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
1704
    /* Closures can be rebound to a different scope */
1705
3.21k
    return 0;
1706
3.21k
  }
1707
1708
11.9k
  if (!CG(active_class_entry)) {
1709
    /* The scope is known if we're in a free function (no scope), but not if we're in
1710
     * a file/eval (which inherits including/eval'ing scope). */
1711
2.63k
    return CG(active_op_array)->function_name != NULL;
1712
2.63k
  }
1713
1714
  /* For traits self etc refers to the using class, not the trait itself */
1715
9.29k
  return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
1716
11.9k
}
1717
/* }}} */
1718
1719
static inline bool class_name_refers_to_active_ce(zend_string *class_name, uint32_t fetch_type) /* {{{ */
1720
22.8k
{
1721
22.8k
  if (!CG(active_class_entry)) {
1722
16.4k
    return 0;
1723
16.4k
  }
1724
6.41k
  if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
1725
1.96k
    return 1;
1726
1.96k
  }
1727
4.44k
  return fetch_type == ZEND_FETCH_CLASS_DEFAULT
1728
4.44k
    && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
1729
6.41k
}
1730
/* }}} */
1731
1732
uint32_t zend_get_class_fetch_type(const zend_string *name) /* {{{ */
1733
1.08M
{
1734
1.08M
  if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1735
9.83k
    return ZEND_FETCH_CLASS_SELF;
1736
1.07M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT))) {
1737
6.72k
    return ZEND_FETCH_CLASS_PARENT;
1738
1.07M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_STATIC))) {
1739
2.61k
    return ZEND_FETCH_CLASS_STATIC;
1740
1.06M
  } else {
1741
1.06M
    return ZEND_FETCH_CLASS_DEFAULT;
1742
1.06M
  }
1743
1.08M
}
1744
/* }}} */
1745
1746
static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
1747
178k
{
1748
  /* Fully qualified names are always default refs */
1749
178k
  if (name_ast->attr == ZEND_NAME_FQ) {
1750
3.41k
    return ZEND_FETCH_CLASS_DEFAULT;
1751
3.41k
  }
1752
1753
175k
  return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
1754
178k
}
1755
/* }}} */
1756
1757
static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type)
1758
33.6k
{
1759
33.6k
  zend_string *class_name = zend_ast_get_str(ast);
1760
33.6k
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) {
1761
22
    zend_error_noreturn(E_COMPILE_ERROR,
1762
22
      "Cannot use \"%s\" as %s, as it is reserved",
1763
22
      ZSTR_VAL(class_name), type);
1764
22
  }
1765
33.6k
  return zend_resolve_class_name(class_name, ast->attr);
1766
33.6k
}
1767
1768
static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
1769
12.7k
{
1770
12.7k
  if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) {
1771
4.64k
    zend_class_entry *ce = CG(active_class_entry);
1772
4.64k
    if (!ce) {
1773
33
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
1774
33
        fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
1775
33
        fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
1776
4.60k
    } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) {
1777
24
      zend_error_noreturn(E_COMPILE_ERROR,
1778
24
        "Cannot use \"parent\" when current class scope has no parent");
1779
24
    }
1780
4.64k
  }
1781
12.7k
}
1782
/* }}} */
1783
1784
static bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */
1785
7.07k
{
1786
7.07k
  uint32_t fetch_type;
1787
7.07k
  zval *class_name;
1788
1789
7.07k
  if (class_ast->kind != ZEND_AST_ZVAL) {
1790
1.97k
    return 0;
1791
1.97k
  }
1792
1793
5.10k
  class_name = zend_ast_get_zval(class_ast);
1794
1795
5.10k
  if (Z_TYPE_P(class_name) != IS_STRING) {
1796
6
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1797
6
  }
1798
1799
5.09k
  fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name));
1800
5.09k
  zend_ensure_valid_class_fetch_type(fetch_type);
1801
1802
5.09k
  switch (fetch_type) {
1803
477
    case ZEND_FETCH_CLASS_SELF:
1804
477
      if (CG(active_class_entry) && zend_is_scope_known()) {
1805
151
        ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
1806
151
        return 1;
1807
151
      }
1808
326
      return 0;
1809
374
    case ZEND_FETCH_CLASS_PARENT:
1810
374
      if (CG(active_class_entry) && CG(active_class_entry)->parent_name
1811
374
          && zend_is_scope_known()) {
1812
140
        ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name);
1813
140
        return 1;
1814
140
      }
1815
234
      return 0;
1816
605
    case ZEND_FETCH_CLASS_STATIC:
1817
605
      return 0;
1818
3.62k
    case ZEND_FETCH_CLASS_DEFAULT:
1819
3.62k
      ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
1820
3.62k
      return 1;
1821
5.09k
    EMPTY_SWITCH_DEFAULT_CASE()
1822
5.09k
  }
1823
5.09k
}
1824
/* }}} */
1825
1826
/* We don't use zend_verify_const_access because we need to deal with unlinked classes. */
1827
static bool zend_verify_ct_const_access(zend_class_constant *c, zend_class_entry *scope)
1828
845
{
1829
845
  if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED) {
1830
14
    return 0;
1831
831
  } else if (c->ce->ce_flags & ZEND_ACC_TRAIT) {
1832
    /* This condition is only met on directly accessing trait constants,
1833
     * because the ce is replaced to the class entry of the composing class
1834
     * on binding. */
1835
0
    return 0;
1836
831
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) {
1837
651
    return 1;
1838
651
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) {
1839
180
    return c->ce == scope;
1840
180
  } else {
1841
0
    zend_class_entry *ce = c->ce;
1842
0
    while (1) {
1843
0
      if (ce == scope) {
1844
0
        return 1;
1845
0
      }
1846
0
      if (!ce->parent) {
1847
0
        break;
1848
0
      }
1849
0
      if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
1850
0
        ce = ce->parent;
1851
0
      } else {
1852
0
        ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name);
1853
0
        if (!ce) {
1854
0
          break;
1855
0
        }
1856
0
      }
1857
0
    }
1858
    /* Reverse case cannot be true during compilation */
1859
0
    return 0;
1860
0
  }
1861
845
}
1862
1863
static bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
1864
22.8k
{
1865
22.8k
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
1866
22.8k
  zend_class_constant *cc;
1867
22.8k
  zval *c;
1868
1869
22.8k
  if (class_name_refers_to_active_ce(class_name, fetch_type)) {
1870
2.16k
    cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
1871
20.6k
  } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1872
388
    zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name);
1873
388
    if (ce) {
1874
291
      cc = zend_hash_find_ptr(&ce->constants_table, name);
1875
291
    } else {
1876
97
      return 0;
1877
97
    }
1878
20.2k
  } else {
1879
20.2k
    return 0;
1880
20.2k
  }
1881
1882
2.46k
  if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
1883
1.33k
    return 0;
1884
1.33k
  }
1885
1886
1.12k
  if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) {
1887
293
    return 0;
1888
293
  }
1889
1890
831
  c = &cc->value;
1891
1892
  /* Substitute case-sensitive (or lowercase) persistent class constants */
1893
831
  if (Z_TYPE_P(c) < IS_ARRAY) {
1894
614
    ZVAL_COPY_OR_DUP(zv, c);
1895
614
    return 1;
1896
614
  } else if (Z_TYPE_P(c) == IS_ARRAY && array_is_const(Z_ARR_P(c))) {
1897
41
    ZVAL_COPY_OR_DUP(zv, c);
1898
41
    return 1;
1899
41
  }
1900
1901
176
  return 0;
1902
831
}
1903
/* }}} */
1904
1905
static void zend_add_to_list(void *result, void *item) /* {{{ */
1906
3.69k
{
1907
3.69k
  void** list = *(void**)result;
1908
3.69k
  size_t n = 0;
1909
1910
3.69k
  if (list) {
1911
165k
    while (list[n]) {
1912
162k
      n++;
1913
162k
    }
1914
2.94k
  }
1915
1916
3.69k
  list = erealloc(list, sizeof(void*) * (n+2));
1917
1918
3.69k
  list[n]   = item;
1919
3.69k
  list[n+1] = NULL;
1920
1921
3.69k
  *(void**)result = list;
1922
3.69k
}
1923
/* }}} */
1924
1925
static void zend_do_extended_stmt(void) /* {{{ */
1926
182k
{
1927
182k
  zend_op *opline;
1928
1929
182k
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) {
1930
182k
    return;
1931
182k
  }
1932
1933
0
  opline = get_next_op();
1934
1935
0
  opline->opcode = ZEND_EXT_STMT;
1936
0
}
1937
/* }}} */
1938
1939
static void zend_do_extended_fcall_begin(void) /* {{{ */
1940
549k
{
1941
549k
  zend_op *opline;
1942
1943
549k
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1944
549k
    return;
1945
549k
  }
1946
1947
0
  opline = get_next_op();
1948
1949
0
  opline->opcode = ZEND_EXT_FCALL_BEGIN;
1950
0
}
1951
/* }}} */
1952
1953
static void zend_do_extended_fcall_end(void) /* {{{ */
1954
549k
{
1955
549k
  zend_op *opline;
1956
1957
549k
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1958
549k
    return;
1959
549k
  }
1960
1961
0
  opline = get_next_op();
1962
1963
0
  opline->opcode = ZEND_EXT_FCALL_END;
1964
0
}
1965
/* }}} */
1966
1967
0
ZEND_API bool zend_is_auto_global_str(const char *name, size_t len) /* {{{ */ {
1968
0
  zend_auto_global *auto_global;
1969
1970
0
  if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) {
1971
0
    if (auto_global->armed) {
1972
0
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1973
0
    }
1974
0
    return 1;
1975
0
  }
1976
0
  return 0;
1977
0
}
1978
/* }}} */
1979
1980
ZEND_API bool zend_is_auto_global(zend_string *name) /* {{{ */
1981
1.69M
{
1982
1.69M
  zend_auto_global *auto_global;
1983
1984
1.69M
  if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) {
1985
4.94k
    if (auto_global->armed) {
1986
330
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1987
330
    }
1988
4.94k
    return 1;
1989
4.94k
  }
1990
1.68M
  return 0;
1991
1.69M
}
1992
/* }}} */
1993
1994
ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
1995
128
{
1996
128
  zend_auto_global auto_global;
1997
128
  zend_result retval;
1998
1999
128
  auto_global.name = name;
2000
128
  auto_global.auto_global_callback = auto_global_callback;
2001
128
  auto_global.jit = jit;
2002
2003
128
  retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
2004
2005
128
  return retval;
2006
128
}
2007
/* }}} */
2008
2009
ZEND_API void zend_activate_auto_globals(void) /* {{{ */
2010
268k
{
2011
268k
  zend_auto_global *auto_global;
2012
2013
4.82M
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2014
4.82M
    if (auto_global->jit) {
2015
1.07M
      auto_global->armed = 1;
2016
1.07M
    } else if (auto_global->auto_global_callback) {
2017
1.07M
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
2018
1.07M
    } else {
2019
0
      auto_global->armed = 0;
2020
0
    }
2021
4.82M
  } ZEND_HASH_FOREACH_END();
2022
268k
}
2023
/* }}} */
2024
2025
int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
2026
12.2M
{
2027
12.2M
  zval zv;
2028
12.2M
  int ret;
2029
2030
12.2M
  if (CG(increment_lineno)) {
2031
15.1k
    CG(zend_lineno)++;
2032
15.1k
    CG(increment_lineno) = 0;
2033
15.1k
  }
2034
2035
12.2M
  ret = lex_scan(&zv, elem);
2036
12.2M
  ZEND_ASSERT(!EG(exception) || ret == T_ERROR);
2037
12.2M
  return ret;
2038
2039
12.2M
}
2040
/* }}} */
2041
2042
ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers) /* {{{ */
2043
70.9k
{
2044
70.9k
  bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS;
2045
2046
70.9k
  ce->refcount = 1;
2047
70.9k
  ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
2048
2049
70.9k
  if (CG(compiler_options) & ZEND_COMPILE_GUARDS) {
2050
0
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2051
0
  }
2052
2053
70.9k
  ce->default_properties_table = NULL;
2054
70.9k
  ce->default_static_members_table = NULL;
2055
70.9k
  zend_hash_init(&ce->properties_info, 8, NULL, NULL, persistent_hashes);
2056
70.9k
  zend_hash_init(&ce->constants_table, 8, NULL, NULL, persistent_hashes);
2057
70.9k
  zend_hash_init(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes);
2058
2059
70.9k
  ce->doc_comment = NULL;
2060
2061
70.9k
  ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
2062
70.9k
  ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
2063
2064
70.9k
  ce->default_object_handlers = &std_object_handlers;
2065
70.9k
  ce->default_properties_count = 0;
2066
70.9k
  ce->default_static_members_count = 0;
2067
70.9k
  ce->properties_info_table = NULL;
2068
70.9k
  ce->attributes = NULL;
2069
70.9k
  ce->enum_backing_type = IS_UNDEF;
2070
70.9k
  ce->backed_enum_table = NULL;
2071
2072
70.9k
  if (nullify_handlers) {
2073
68.3k
    ce->constructor = NULL;
2074
68.3k
    ce->destructor = NULL;
2075
68.3k
    ce->clone = NULL;
2076
68.3k
    ce->__get = NULL;
2077
68.3k
    ce->__set = NULL;
2078
68.3k
    ce->__unset = NULL;
2079
68.3k
    ce->__isset = NULL;
2080
68.3k
    ce->__call = NULL;
2081
68.3k
    ce->__callstatic = NULL;
2082
68.3k
    ce->__tostring = NULL;
2083
68.3k
    ce->__serialize = NULL;
2084
68.3k
    ce->__unserialize = NULL;
2085
68.3k
    ce->__debugInfo = NULL;
2086
68.3k
    ce->create_object = NULL;
2087
68.3k
    ce->get_iterator = NULL;
2088
68.3k
    ce->iterator_funcs_ptr = NULL;
2089
68.3k
    ce->arrayaccess_funcs_ptr = NULL;
2090
68.3k
    ce->get_static_method = NULL;
2091
68.3k
    ce->parent = NULL;
2092
68.3k
    ce->parent_name = NULL;
2093
68.3k
    ce->num_interfaces = 0;
2094
68.3k
    ce->interfaces = NULL;
2095
68.3k
    ce->num_traits = 0;
2096
68.3k
    ce->num_hooked_props = 0;
2097
68.3k
    ce->num_hooked_prop_variance_checks = 0;
2098
68.3k
    ce->trait_names = NULL;
2099
68.3k
    ce->trait_aliases = NULL;
2100
68.3k
    ce->trait_precedences = NULL;
2101
68.3k
    ce->serialize = NULL;
2102
68.3k
    ce->unserialize = NULL;
2103
68.3k
    if (ce->type == ZEND_INTERNAL_CLASS) {
2104
0
      ce->info.internal.module = NULL;
2105
0
      ce->info.internal.builtin_functions = NULL;
2106
0
    }
2107
68.3k
  }
2108
70.9k
}
2109
/* }}} */
2110
2111
ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
2112
0
{
2113
0
  return op_array->vars[EX_VAR_TO_NUM(var)];
2114
0
}
2115
/* }}} */
2116
2117
zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
2118
0
{
2119
0
  zval *left_zv = zend_ast_get_zval(left_ast);
2120
0
  zend_string *left = Z_STR_P(left_zv);
2121
0
  zend_string *right = zend_ast_get_str(right_ast);
2122
2123
0
  zend_string *result;
2124
0
  size_t left_len = ZSTR_LEN(left);
2125
0
  size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
2126
2127
0
  result = zend_string_extend(left, len, 0);
2128
0
  ZSTR_VAL(result)[left_len] = '\\';
2129
0
  memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
2130
0
  ZSTR_VAL(result)[len] = '\0';
2131
0
  zend_string_release_ex(right, 0);
2132
2133
0
  ZVAL_STR(left_zv, result);
2134
0
  return left_ast;
2135
0
}
2136
/* }}} */
2137
2138
zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
2139
1.73k
{
2140
1.73k
  zval *zv = zend_ast_get_zval(ast);
2141
1.73k
  if (Z_TYPE_P(zv) == IS_LONG) {
2142
1.41k
    if (Z_LVAL_P(zv) == 0) {
2143
591
      ZVAL_NEW_STR(zv, ZSTR_INIT_LITERAL("-0", 0));
2144
820
    } else {
2145
820
      ZEND_ASSERT(Z_LVAL_P(zv) > 0);
2146
820
      Z_LVAL_P(zv) *= -1;
2147
820
    }
2148
1.41k
  } else if (Z_TYPE_P(zv) == IS_STRING) {
2149
327
    size_t orig_len = Z_STRLEN_P(zv);
2150
327
    Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0);
2151
327
    memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
2152
327
    Z_STRVAL_P(zv)[0] = '-';
2153
327
  } else {
2154
0
    ZEND_UNREACHABLE();
2155
0
  }
2156
1.73k
  return ast;
2157
1.73k
}
2158
/* }}} */
2159
2160
static void zend_verify_namespace(void) /* {{{ */
2161
541k
{
2162
541k
  if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
2163
42
    zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
2164
42
  }
2165
541k
}
2166
/* }}} */
2167
2168
/* {{{ zend_dirname
2169
   Returns directory name component of path */
2170
ZEND_API size_t zend_dirname(char *path, size_t len)
2171
1.84k
{
2172
1.84k
  char *end = path + len - 1;
2173
1.84k
  unsigned int len_adjust = 0;
2174
2175
#ifdef ZEND_WIN32
2176
  /* Note that on Win32 CWD is per drive (heritage from CP/M).
2177
   * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
2178
   */
2179
  if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
2180
    /* Skip over the drive spec (if any) so as not to change */
2181
    path += 2;
2182
    len_adjust += 2;
2183
    if (2 == len) {
2184
      /* Return "c:" on Win32 for dirname("c:").
2185
       * It would be more consistent to return "c:."
2186
       * but that would require making the string *longer*.
2187
       */
2188
      return len;
2189
    }
2190
  }
2191
#endif
2192
2193
1.84k
  if (len == 0) {
2194
    /* Illegal use of this function */
2195
0
    return 0;
2196
0
  }
2197
2198
  /* Strip trailing slashes */
2199
1.84k
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2200
0
    end--;
2201
0
  }
2202
1.84k
  if (end < path) {
2203
    /* The path only contained slashes */
2204
0
    path[0] = DEFAULT_SLASH;
2205
0
    path[1] = '\0';
2206
0
    return 1 + len_adjust;
2207
0
  }
2208
2209
  /* Strip filename */
2210
20.3k
  while (end >= path && !IS_SLASH_P_EX(end, end == path)) {
2211
18.4k
    end--;
2212
18.4k
  }
2213
1.84k
  if (end < path) {
2214
    /* No slash found, therefore return '.' */
2215
1.26k
    path[0] = '.';
2216
1.26k
    path[1] = '\0';
2217
1.26k
    return 1 + len_adjust;
2218
1.26k
  }
2219
2220
  /* Strip slashes which came before the file name */
2221
1.15k
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2222
578
    end--;
2223
578
  }
2224
578
  if (end < path) {
2225
5
    path[0] = DEFAULT_SLASH;
2226
5
    path[1] = '\0';
2227
5
    return 1 + len_adjust;
2228
5
  }
2229
573
  *(end+1) = '\0';
2230
2231
573
  return (size_t)(end + 1 - path) + len_adjust;
2232
578
}
2233
/* }}} */
2234
2235
static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */
2236
513k
{
2237
513k
  uint_fast8_t factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3;
2238
2239
513k
  switch (type) {
2240
283k
    case BP_VAR_R:
2241
283k
      opline->result_type = IS_TMP_VAR;
2242
283k
      result->op_type = IS_TMP_VAR;
2243
283k
      return;
2244
113k
    case BP_VAR_W:
2245
113k
      opline->opcode += 1 * factor;
2246
113k
      return;
2247
12.4k
    case BP_VAR_RW:
2248
12.4k
      opline->opcode += 2 * factor;
2249
12.4k
      return;
2250
83.2k
    case BP_VAR_IS:
2251
83.2k
      opline->result_type = IS_TMP_VAR;
2252
83.2k
      result->op_type = IS_TMP_VAR;
2253
83.2k
      opline->opcode += 3 * factor;
2254
83.2k
      return;
2255
7.14k
    case BP_VAR_FUNC_ARG:
2256
7.14k
      opline->opcode += 4 * factor;
2257
7.14k
      return;
2258
14.4k
    case BP_VAR_UNSET:
2259
14.4k
      opline->opcode += 5 * factor;
2260
14.4k
      return;
2261
513k
    EMPTY_SWITCH_DEFAULT_CASE()
2262
513k
  }
2263
513k
}
2264
/* }}} */
2265
2266
static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
2267
1.31M
{
2268
1.31M
  opline->result_type = IS_VAR;
2269
1.31M
  opline->result.var = get_temporary_variable();
2270
1.31M
  GET_NODE(result, opline->result);
2271
1.31M
}
2272
/* }}} */
2273
2274
static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
2275
3.85M
{
2276
3.85M
  opline->result_type = IS_TMP_VAR;
2277
3.85M
  opline->result.var = get_temporary_variable();
2278
3.85M
  GET_NODE(result, opline->result);
2279
3.85M
}
2280
/* }}} */
2281
2282
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2283
5.55M
{
2284
5.55M
  zend_op *opline = get_next_op();
2285
5.55M
  opline->opcode = opcode;
2286
2287
5.55M
  if (op1 != NULL) {
2288
4.17M
    SET_NODE(opline->op1, op1);
2289
4.17M
  }
2290
2291
5.55M
  if (op2 != NULL) {
2292
421k
    SET_NODE(opline->op2, op2);
2293
421k
  }
2294
2295
5.55M
  if (result) {
2296
947k
    zend_make_var_result(result, opline);
2297
947k
  }
2298
5.55M
  return opline;
2299
5.55M
}
2300
/* }}} */
2301
2302
static zend_op *zend_emit_op_tmp(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2303
3.92M
{
2304
3.92M
  zend_op *opline = get_next_op();
2305
3.92M
  opline->opcode = opcode;
2306
2307
3.92M
  if (op1 != NULL) {
2308
1.30M
    SET_NODE(opline->op1, op1);
2309
1.30M
  }
2310
2311
3.92M
  if (op2 != NULL) {
2312
896k
    SET_NODE(opline->op2, op2);
2313
896k
  }
2314
2315
3.92M
  if (result) {
2316
3.80M
    zend_make_tmp_result(result, opline);
2317
3.80M
  }
2318
2319
3.92M
  return opline;
2320
3.92M
}
2321
/* }}} */
2322
2323
static void zend_emit_tick(void) /* {{{ */
2324
2.45k
{
2325
2.45k
  zend_op *opline;
2326
2327
  /* This prevents a double TICK generated by the parser statement of "declare()" */
2328
2.45k
  if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
2329
949
    return;
2330
949
  }
2331
2332
1.50k
  opline = get_next_op();
2333
2334
1.50k
  opline->opcode = ZEND_TICKS;
2335
1.50k
  opline->extended_value = FC(declarables).ticks;
2336
1.50k
}
2337
/* }}} */
2338
2339
static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
2340
42.1k
{
2341
42.1k
  return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
2342
42.1k
}
2343
/* }}} */
2344
2345
static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
2346
185k
{
2347
185k
  uint32_t opnum = get_next_op_number();
2348
185k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
2349
185k
  opline->op1.opline_num = opnum_target;
2350
185k
  return opnum;
2351
185k
}
2352
/* }}} */
2353
2354
ZEND_API bool zend_is_smart_branch(const zend_op *opline) /* {{{ */
2355
106k
{
2356
106k
  switch (opline->opcode) {
2357
2.22k
    case ZEND_IS_IDENTICAL:
2358
2.53k
    case ZEND_IS_NOT_IDENTICAL:
2359
26.7k
    case ZEND_IS_EQUAL:
2360
27.7k
    case ZEND_IS_NOT_EQUAL:
2361
35.3k
    case ZEND_IS_SMALLER:
2362
37.0k
    case ZEND_IS_SMALLER_OR_EQUAL:
2363
40.1k
    case ZEND_CASE:
2364
41.7k
    case ZEND_CASE_STRICT:
2365
41.9k
    case ZEND_ISSET_ISEMPTY_CV:
2366
42.0k
    case ZEND_ISSET_ISEMPTY_VAR:
2367
42.4k
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2368
42.5k
    case ZEND_ISSET_ISEMPTY_PROP_OBJ:
2369
42.6k
    case ZEND_ISSET_ISEMPTY_STATIC_PROP:
2370
42.8k
    case ZEND_INSTANCEOF:
2371
43.3k
    case ZEND_TYPE_CHECK:
2372
43.4k
    case ZEND_DEFINED:
2373
43.4k
    case ZEND_IN_ARRAY:
2374
43.5k
    case ZEND_ARRAY_KEY_EXISTS:
2375
43.5k
      return 1;
2376
63.4k
    default:
2377
63.4k
      return 0;
2378
106k
  }
2379
106k
}
2380
/* }}} */
2381
2382
static inline uint32_t zend_emit_cond_jump(uint8_t opcode, znode *cond, uint32_t opnum_target) /* {{{ */
2383
69.4k
{
2384
69.4k
  uint32_t opnum = get_next_op_number();
2385
69.4k
  zend_op *opline;
2386
2387
69.4k
  if (cond->op_type == IS_TMP_VAR && opnum > 0) {
2388
59.4k
    opline = CG(active_op_array)->opcodes + opnum - 1;
2389
59.4k
    if (opline->result_type == IS_TMP_VAR
2390
59.4k
     && opline->result.var == cond->u.op.var
2391
59.4k
     && zend_is_smart_branch(opline)) {
2392
43.3k
      if (opcode == ZEND_JMPZ) {
2393
19.2k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ;
2394
24.0k
      } else {
2395
24.0k
        ZEND_ASSERT(opcode == ZEND_JMPNZ);
2396
24.0k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ;
2397
24.0k
      }
2398
43.3k
    }
2399
59.4k
  }
2400
69.4k
  opline = zend_emit_op(NULL, opcode, cond, NULL);
2401
69.4k
  opline->op2.opline_num = opnum_target;
2402
69.4k
  return opnum;
2403
69.4k
}
2404
/* }}} */
2405
2406
static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
2407
214k
{
2408
214k
  zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
2409
214k
  switch (opline->opcode) {
2410
141k
    case ZEND_JMP:
2411
141k
      opline->op1.opline_num = opnum_target;
2412
141k
      break;
2413
30.2k
    case ZEND_JMPZ:
2414
47.1k
    case ZEND_JMPNZ:
2415
54.0k
    case ZEND_JMPZ_EX:
2416
60.3k
    case ZEND_JMPNZ_EX:
2417
62.7k
    case ZEND_JMP_SET:
2418
72.4k
    case ZEND_COALESCE:
2419
72.4k
    case ZEND_JMP_NULL:
2420
72.8k
    case ZEND_BIND_INIT_STATIC_OR_JMP:
2421
72.8k
    case ZEND_JMP_FRAMELESS:
2422
72.8k
      opline->op2.opline_num = opnum_target;
2423
72.8k
      break;
2424
214k
    EMPTY_SWITCH_DEFAULT_CASE()
2425
214k
  }
2426
214k
}
2427
/* }}} */
2428
2429
static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
2430
212k
{
2431
212k
  zend_update_jump_target(opnum_jump, get_next_op_number());
2432
212k
}
2433
/* }}} */
2434
2435
static inline zend_op *zend_delayed_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2436
366k
{
2437
366k
  zend_op tmp_opline;
2438
2439
366k
  init_op(&tmp_opline);
2440
2441
366k
  tmp_opline.opcode = opcode;
2442
366k
  if (op1 != NULL) {
2443
366k
    SET_NODE(tmp_opline.op1, op1);
2444
366k
  }
2445
366k
  if (op2 != NULL) {
2446
345k
    SET_NODE(tmp_opline.op2, op2);
2447
345k
  }
2448
366k
  if (result) {
2449
360k
    zend_make_var_result(result, &tmp_opline);
2450
360k
  }
2451
2452
366k
  zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
2453
366k
  return zend_stack_top(&CG(delayed_oplines_stack));
2454
366k
}
2455
/* }}} */
2456
2457
static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
2458
665k
{
2459
665k
  return zend_stack_count(&CG(delayed_oplines_stack));
2460
665k
}
2461
/* }}} */
2462
2463
static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
2464
663k
{
2465
663k
  zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
2466
663k
  uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
2467
2468
663k
  ZEND_ASSERT(count >= offset);
2469
1.02M
  for (i = offset; i < count; ++i) {
2470
365k
    if (EXPECTED(oplines[i].opcode != ZEND_NOP)) {
2471
360k
      opline = get_next_op();
2472
360k
      memcpy(opline, &oplines[i], sizeof(zend_op));
2473
360k
    } else {
2474
4.89k
      opline = CG(active_op_array)->opcodes + oplines[i].extended_value;
2475
4.89k
    }
2476
365k
  }
2477
2478
663k
  CG(delayed_oplines_stack).top = offset;
2479
663k
  return opline;
2480
663k
}
2481
/* }}} */
2482
2483
static bool zend_ast_kind_is_short_circuited(zend_ast_kind ast_kind)
2484
8.61M
{
2485
8.61M
  switch (ast_kind) {
2486
277k
    case ZEND_AST_DIM:
2487
353k
    case ZEND_AST_PROP:
2488
443k
    case ZEND_AST_NULLSAFE_PROP:
2489
462k
    case ZEND_AST_STATIC_PROP:
2490
559k
    case ZEND_AST_METHOD_CALL:
2491
563k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2492
599k
    case ZEND_AST_STATIC_CALL:
2493
599k
      return 1;
2494
8.01M
    default:
2495
8.01M
      return 0;
2496
8.61M
  }
2497
8.61M
}
2498
2499
static bool zend_ast_is_short_circuited(const zend_ast *ast)
2500
882k
{
2501
882k
  switch (ast->kind) {
2502
102k
    case ZEND_AST_DIM:
2503
138k
    case ZEND_AST_PROP:
2504
149k
    case ZEND_AST_STATIC_PROP:
2505
150k
    case ZEND_AST_METHOD_CALL:
2506
152k
    case ZEND_AST_STATIC_CALL:
2507
152k
      return zend_ast_is_short_circuited(ast->child[0]);
2508
376
    case ZEND_AST_NULLSAFE_PROP:
2509
501
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2510
501
      return 1;
2511
729k
    default:
2512
729k
      return 0;
2513
882k
  }
2514
882k
}
2515
2516
static void zend_assert_not_short_circuited(const zend_ast *ast)
2517
24.8k
{
2518
24.8k
  if (zend_ast_is_short_circuited(ast)) {
2519
41
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain");
2520
41
  }
2521
24.8k
}
2522
2523
/* Mark nodes that are an inner part of a short-circuiting chain.
2524
 * We should not perform a "commit" on them, as it will be performed by the outer-most node.
2525
 * We do this to avoid passing down an argument in various compile functions. */
2526
2527
608k
#define ZEND_SHORT_CIRCUITING_INNER 0x8000
2528
2529
428k
static void zend_short_circuiting_mark_inner(zend_ast *ast) {
2530
428k
  if (zend_ast_kind_is_short_circuited(ast->kind)) {
2531
184k
    ast->attr |= ZEND_SHORT_CIRCUITING_INNER;
2532
184k
  }
2533
428k
}
2534
2535
static uint32_t zend_short_circuiting_checkpoint(void)
2536
8.26M
{
2537
8.26M
  return zend_stack_count(&CG(short_circuiting_opnums));
2538
8.26M
}
2539
2540
static void zend_short_circuiting_commit(uint32_t checkpoint, znode *result, zend_ast *ast)
2541
8.18M
{
2542
8.18M
  bool is_short_circuited = zend_ast_kind_is_short_circuited(ast->kind)
2543
8.18M
    || ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY;
2544
8.18M
  if (!is_short_circuited) {
2545
7.76M
    ZEND_ASSERT(zend_stack_count(&CG(short_circuiting_opnums)) == checkpoint
2546
7.76M
      && "Short circuiting stack should be empty");
2547
7.76M
    return;
2548
7.76M
  }
2549
2550
423k
  if (ast->attr & ZEND_SHORT_CIRCUITING_INNER) {
2551
    /* Outer-most node will commit. */
2552
39.1k
    return;
2553
39.1k
  }
2554
2555
432k
  while (zend_stack_count(&CG(short_circuiting_opnums)) != checkpoint) {
2556
48.3k
    uint32_t opnum = *(uint32_t *) zend_stack_top(&CG(short_circuiting_opnums));
2557
48.3k
    zend_op *opline = &CG(active_op_array)->opcodes[opnum];
2558
48.3k
    opline->op2.opline_num = get_next_op_number();
2559
48.3k
    SET_NODE(opline->result, result);
2560
48.3k
    opline->extended_value |=
2561
48.3k
      ast->kind == ZEND_AST_ISSET ? ZEND_SHORT_CIRCUITING_CHAIN_ISSET :
2562
48.3k
      ast->kind == ZEND_AST_EMPTY ? ZEND_SHORT_CIRCUITING_CHAIN_EMPTY :
2563
47.2k
                                    ZEND_SHORT_CIRCUITING_CHAIN_EXPR;
2564
48.3k
    zend_stack_del_top(&CG(short_circuiting_opnums));
2565
48.3k
  }
2566
384k
}
2567
2568
static void zend_emit_jmp_null(znode *obj_node, uint32_t bp_type)
2569
48.3k
{
2570
48.3k
  uint32_t jmp_null_opnum = get_next_op_number();
2571
48.3k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP_NULL, obj_node, NULL);
2572
48.3k
  if (opline->op1_type == IS_CONST) {
2573
1.70k
    Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
2574
1.70k
  }
2575
48.3k
  if (bp_type == BP_VAR_IS) {
2576
1.60k
    opline->extended_value |= ZEND_JMP_NULL_BP_VAR_IS;
2577
1.60k
  }
2578
48.3k
  zend_stack_push(&CG(short_circuiting_opnums), &jmp_null_opnum);
2579
48.3k
}
2580
2581
static void zend_compile_memoized_expr(znode *result, zend_ast *expr) /* {{{ */
2582
152k
{
2583
152k
  const zend_memoize_mode memoize_mode = CG(memoize_mode);
2584
152k
  if (memoize_mode == ZEND_MEMOIZE_COMPILE) {
2585
77.1k
    znode memoized_result;
2586
2587
    /* Go through normal compilation */
2588
77.1k
    CG(memoize_mode) = ZEND_MEMOIZE_NONE;
2589
77.1k
    zend_compile_expr(result, expr);
2590
77.1k
    CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
2591
2592
77.1k
    if (result->op_type == IS_VAR) {
2593
1.45k
      zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL);
2594
75.7k
    } else if (result->op_type == IS_TMP_VAR) {
2595
68.0k
      zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL);
2596
68.0k
    } else {
2597
7.62k
      if (result->op_type == IS_CONST) {
2598
6.83k
        Z_TRY_ADDREF(result->u.constant);
2599
6.83k
      }
2600
7.62k
      memoized_result = *result;
2601
7.62k
    }
2602
2603
77.1k
    zend_hash_index_update_mem(
2604
77.1k
      CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode));
2605
77.1k
  } else if (memoize_mode == ZEND_MEMOIZE_FETCH) {
2606
75.6k
    znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr);
2607
75.6k
    *result = *memoized_result;
2608
75.6k
    if (result->op_type == IS_CONST) {
2609
6.14k
      Z_TRY_ADDREF(result->u.constant);
2610
6.14k
    }
2611
75.6k
  } else {
2612
0
    ZEND_UNREACHABLE();
2613
0
  }
2614
152k
}
2615
/* }}} */
2616
2617
static void zend_emit_return_type_check(
2618
    znode *expr, zend_arg_info *return_info, bool implicit) /* {{{ */
2619
20.1k
{
2620
20.1k
  zend_type type = return_info->type;
2621
20.1k
  if (ZEND_TYPE_IS_SET(type)) {
2622
20.1k
    zend_op *opline;
2623
2624
    /* `return ...;` is illegal in a void function (but `return;` isn't) */
2625
20.1k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) {
2626
4.45k
      if (expr) {
2627
18
        if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
2628
6
          zend_error_noreturn(E_COMPILE_ERROR,
2629
6
            "A void %s must not return a value "
2630
6
            "(did you mean \"return;\" instead of \"return null;\"?)",
2631
6
            CG(active_class_entry) != NULL ? "method" : "function");
2632
12
        } else {
2633
12
          zend_error_noreturn(E_COMPILE_ERROR, "A void %s must not return a value",
2634
12
          CG(active_class_entry) != NULL ? "method" : "function");
2635
12
        }
2636
18
      }
2637
      /* we don't need run-time check */
2638
4.43k
      return;
2639
4.45k
    }
2640
2641
    /* `return` is illegal in a never-returning function */
2642
15.7k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_NEVER)) {
2643
      /* Implicit case handled separately using VERIFY_NEVER_TYPE opcode. */
2644
14
      ZEND_ASSERT(!implicit);
2645
14
      zend_error_noreturn(E_COMPILE_ERROR, "A never-returning %s must not return",
2646
14
        CG(active_class_entry) != NULL ? "method" : "function");
2647
0
      return;
2648
14
    }
2649
2650
15.6k
    if (!expr && !implicit) {
2651
14
      if (ZEND_TYPE_ALLOW_NULL(type)) {
2652
6
        zend_error_noreturn(E_COMPILE_ERROR,
2653
6
          "A %s with return type must return a value "
2654
6
          "(did you mean \"return null;\" instead of \"return;\"?)",
2655
6
          CG(active_class_entry) != NULL ? "method" : "function");
2656
8
      } else {
2657
8
        zend_error_noreturn(E_COMPILE_ERROR,
2658
8
          "A %s with return type must return a value",
2659
8
          CG(active_class_entry) != NULL ? "method" : "function");
2660
8
      }
2661
14
    }
2662
2663
15.6k
    if (expr && ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) {
2664
      /* we don't need run-time check for mixed return type */
2665
478
      return;
2666
478
    }
2667
2668
15.2k
    if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) {
2669
      /* we don't need run-time check */
2670
1.19k
      return;
2671
1.19k
    }
2672
2673
14.0k
    opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
2674
14.0k
    if (expr && expr->op_type == IS_CONST) {
2675
239
      opline->result_type = expr->op_type = IS_TMP_VAR;
2676
239
      opline->result.var = expr->u.op.var = get_temporary_variable();
2677
239
    }
2678
14.0k
  }
2679
20.1k
}
2680
/* }}} */
2681
2682
void zend_emit_final_return(bool return_one) /* {{{ */
2683
258k
{
2684
258k
  znode zn;
2685
258k
  zend_op *ret;
2686
258k
  bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
2687
2688
258k
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)
2689
258k
      && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) {
2690
15.4k
    zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
2691
2692
15.4k
    if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
2693
366
      zend_emit_op(NULL, ZEND_VERIFY_NEVER_TYPE, NULL, NULL);
2694
366
      return;
2695
366
    }
2696
2697
15.0k
    zend_emit_return_type_check(NULL, return_info, 1);
2698
15.0k
  }
2699
2700
258k
  zn.op_type = IS_CONST;
2701
258k
  if (return_one) {
2702
93.2k
    ZVAL_LONG(&zn.u.constant, 1);
2703
164k
  } else {
2704
164k
    ZVAL_NULL(&zn.u.constant);
2705
164k
  }
2706
2707
258k
  ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
2708
258k
  ret->extended_value = -1;
2709
258k
}
2710
/* }}} */
2711
2712
static inline bool zend_is_variable(zend_ast *ast) /* {{{ */
2713
661k
{
2714
661k
  return ast->kind == ZEND_AST_VAR
2715
661k
    || ast->kind == ZEND_AST_DIM
2716
661k
    || ast->kind == ZEND_AST_PROP
2717
661k
    || ast->kind == ZEND_AST_NULLSAFE_PROP
2718
661k
    || ast->kind == ZEND_AST_STATIC_PROP;
2719
661k
}
2720
/* }}} */
2721
2722
static inline bool zend_is_call(zend_ast *ast) /* {{{ */
2723
791k
{
2724
791k
  return ast->kind == ZEND_AST_CALL
2725
791k
    || ast->kind == ZEND_AST_METHOD_CALL
2726
791k
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
2727
791k
    || ast->kind == ZEND_AST_STATIC_CALL;
2728
791k
}
2729
/* }}} */
2730
2731
static inline bool zend_is_variable_or_call(zend_ast *ast) /* {{{ */
2732
41.4k
{
2733
41.4k
  return zend_is_variable(ast) || zend_is_call(ast);
2734
41.4k
}
2735
/* }}} */
2736
2737
static inline bool zend_is_unticked_stmt(zend_ast *ast) /* {{{ */
2738
3.17k
{
2739
3.17k
  return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL
2740
3.17k
    || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP
2741
3.17k
    || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD;
2742
3.17k
}
2743
/* }}} */
2744
2745
static inline bool zend_can_write_to_variable(zend_ast *ast) /* {{{ */
2746
37.4k
{
2747
37.4k
  while (
2748
40.7k
    ast->kind == ZEND_AST_DIM
2749
40.7k
    || ast->kind == ZEND_AST_PROP
2750
37.4k
  ) {
2751
3.24k
    ast = ast->child[0];
2752
3.24k
  }
2753
2754
37.4k
  return zend_is_variable_or_call(ast) && !zend_ast_is_short_circuited(ast);
2755
37.4k
}
2756
/* }}} */
2757
2758
static inline bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
2759
53.5k
{
2760
53.5k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2761
0
    return 0;
2762
0
  }
2763
2764
53.5k
  return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
2765
53.5k
}
2766
/* }}} */
2767
2768
static inline void zend_handle_numeric_op(znode *node) /* {{{ */
2769
4.69k
{
2770
4.69k
  if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
2771
2.61k
    zend_ulong index;
2772
2773
2.61k
    if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
2774
348
      zval_ptr_dtor(&node->u.constant);
2775
348
      ZVAL_LONG(&node->u.constant, index);
2776
348
    }
2777
2.61k
  }
2778
4.69k
}
2779
/* }}} */
2780
2781
static inline void zend_handle_numeric_dim(zend_op *opline, znode *dim_node) /* {{{ */
2782
101k
{
2783
101k
  if (Z_TYPE(dim_node->u.constant) == IS_STRING) {
2784
28.8k
    zend_ulong index;
2785
2786
28.8k
    if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) {
2787
      /* For numeric indexes we also keep the original value to use by ArrayAccess
2788
       * See bug #63217
2789
       */
2790
5.28k
      int c = zend_add_literal(&dim_node->u.constant);
2791
5.28k
      ZEND_ASSERT(opline->op2.constant + 1 == c);
2792
5.28k
      ZVAL_LONG(CT_CONSTANT(opline->op2), index);
2793
5.28k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE;
2794
5.28k
      return;
2795
5.28k
    }
2796
28.8k
  }
2797
101k
}
2798
/* }}} */
2799
2800
static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
2801
42.1k
{
2802
42.1k
  if (class_node->op_type == IS_CONST) {
2803
21.0k
    opline->op1_type = IS_CONST;
2804
21.0k
    opline->op1.constant = zend_add_class_name_literal(
2805
21.0k
      Z_STR(class_node->u.constant));
2806
21.0k
  } else {
2807
21.0k
    SET_NODE(opline->op1, class_node);
2808
21.0k
  }
2809
42.1k
}
2810
/* }}} */
2811
2812
static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */
2813
141k
{
2814
141k
  uint32_t fetch_type;
2815
2816
141k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2817
25.0k
    znode name_node;
2818
2819
25.0k
    zend_compile_expr(&name_node, name_ast);
2820
2821
25.0k
    if (name_node.op_type == IS_CONST) {
2822
2.79k
      zend_string *name;
2823
2824
2.79k
      if (Z_TYPE(name_node.u.constant) != IS_STRING) {
2825
6
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
2826
6
      }
2827
2828
2.78k
      name = Z_STR(name_node.u.constant);
2829
2.78k
      fetch_type = zend_get_class_fetch_type(name);
2830
2831
2.78k
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
2832
1.88k
        result->op_type = IS_CONST;
2833
1.88k
        ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ));
2834
1.88k
      } else {
2835
905
        zend_ensure_valid_class_fetch_type(fetch_type);
2836
905
        result->op_type = IS_UNUSED;
2837
905
        result->u.op.num = fetch_type | fetch_flags;
2838
905
      }
2839
2840
2.78k
      zend_string_release_ex(name, 0);
2841
22.2k
    } else {
2842
22.2k
      zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
2843
22.2k
      opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags;
2844
22.2k
    }
2845
25.0k
    return;
2846
25.0k
  }
2847
2848
  /* Fully qualified names are always default refs */
2849
116k
  if (name_ast->attr == ZEND_NAME_FQ) {
2850
3.41k
    result->op_type = IS_CONST;
2851
3.41k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2852
3.41k
    return;
2853
3.41k
  }
2854
2855
113k
  fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
2856
113k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
2857
107k
    result->op_type = IS_CONST;
2858
107k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2859
107k
  } else {
2860
5.72k
    zend_ensure_valid_class_fetch_type(fetch_type);
2861
5.72k
    result->op_type = IS_UNUSED;
2862
5.72k
    result->u.op.num = fetch_type | fetch_flags;
2863
5.72k
  }
2864
113k
}
2865
/* }}} */
2866
2867
static zend_result zend_try_compile_cv(znode *result, zend_ast *ast) /* {{{ */
2868
1.30M
{
2869
1.30M
  zend_ast *name_ast = ast->child[0];
2870
1.30M
  if (name_ast->kind == ZEND_AST_ZVAL) {
2871
1.17M
    zval *zv = zend_ast_get_zval(name_ast);
2872
1.17M
    zend_string *name;
2873
2874
1.17M
    if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
2875
1.16M
      name = zval_make_interned_string(zv);
2876
1.16M
    } else {
2877
3.69k
      name = zend_new_interned_string(zval_get_string_func(zv));
2878
3.69k
    }
2879
2880
1.17M
    if (zend_is_auto_global(name)) {
2881
1.45k
      return FAILURE;
2882
1.45k
    }
2883
2884
1.17M
    result->op_type = IS_CV;
2885
1.17M
    result->u.op.var = lookup_cv(name);
2886
2887
1.17M
    if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) {
2888
3.69k
      zend_string_release_ex(name, 0);
2889
3.69k
    }
2890
2891
1.17M
    return SUCCESS;
2892
1.17M
  }
2893
2894
130k
  return FAILURE;
2895
1.30M
}
2896
/* }}} */
2897
2898
static zend_op *zend_compile_simple_var_no_cv(znode *result, zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2899
148k
{
2900
148k
  zend_ast *name_ast = ast->child[0];
2901
148k
  znode name_node;
2902
148k
  zend_op *opline;
2903
2904
148k
  zend_compile_expr(&name_node, name_ast);
2905
148k
  if (name_node.op_type == IS_CONST) {
2906
21.4k
    convert_to_string(&name_node.u.constant);
2907
21.4k
  }
2908
2909
148k
  if (delayed) {
2910
10.2k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2911
138k
  } else {
2912
138k
    opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2913
138k
  }
2914
2915
148k
  if (name_node.op_type == IS_CONST &&
2916
148k
      zend_is_auto_global(Z_STR(name_node.u.constant))) {
2917
2918
1.43k
    opline->extended_value = ZEND_FETCH_GLOBAL;
2919
146k
  } else {
2920
146k
    opline->extended_value = ZEND_FETCH_LOCAL;
2921
146k
  }
2922
2923
148k
  zend_adjust_for_fetch_type(opline, result, type);
2924
148k
  return opline;
2925
148k
}
2926
/* }}} */
2927
2928
static bool is_this_fetch(zend_ast *ast) /* {{{ */
2929
1.78M
{
2930
1.78M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2931
1.55M
    zval *name = zend_ast_get_zval(ast->child[0]);
2932
1.55M
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals(Z_STR_P(name), ZSTR_KNOWN(ZEND_STR_THIS));
2933
1.55M
  }
2934
2935
227k
  return 0;
2936
1.78M
}
2937
/* }}} */
2938
2939
static bool is_globals_fetch(const zend_ast *ast)
2940
2.53M
{
2941
2.53M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2942
1.68M
    zval *name = zend_ast_get_zval(ast->child[0]);
2943
1.68M
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "GLOBALS");
2944
1.68M
  }
2945
2946
847k
  return 0;
2947
2.53M
}
2948
2949
static bool is_global_var_fetch(zend_ast *ast)
2950
514k
{
2951
514k
  return ast->kind == ZEND_AST_DIM && is_globals_fetch(ast->child[0]);
2952
514k
}
2953
2954
static bool this_guaranteed_exists(void) /* {{{ */
2955
10.4k
{
2956
10.4k
  zend_oparray_context *ctx = &CG(context);
2957
11.2k
  while (ctx) {
2958
    /* Instance methods always have a $this.
2959
     * This also includes closures that have a scope and use $this. */
2960
11.2k
    zend_op_array *op_array = ctx->op_array;
2961
11.2k
    if (op_array->fn_flags & ZEND_ACC_STATIC) {
2962
79
      return false;
2963
11.1k
    } else if (op_array->scope) {
2964
9.23k
      return true;
2965
9.23k
    } else if (!(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
2966
1.08k
      return false;
2967
1.08k
    }
2968
856
    ctx = ctx->prev;
2969
856
  }
2970
0
  return false;
2971
10.4k
}
2972
/* }}} */
2973
2974
static zend_op *zend_compile_simple_var(znode *result, zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2975
1.16M
{
2976
1.16M
  if (is_this_fetch(ast)) {
2977
2.42k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL);
2978
2.42k
    if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
2979
2.26k
      opline->result_type = IS_TMP_VAR;
2980
2.26k
      result->op_type = IS_TMP_VAR;
2981
2.26k
    }
2982
2.42k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
2983
2.42k
    return opline;
2984
1.15M
  } else if (is_globals_fetch(ast)) {
2985
2.28k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_GLOBALS, NULL, NULL);
2986
2.28k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
2987
2.21k
      opline->result_type = IS_TMP_VAR;
2988
2.21k
      result->op_type = IS_TMP_VAR;
2989
2.21k
    }
2990
2.28k
    return opline;
2991
1.15M
  } else if (zend_try_compile_cv(result, ast) == FAILURE) {
2992
129k
    return zend_compile_simple_var_no_cv(result, ast, type, delayed);
2993
129k
  }
2994
1.02M
  return NULL;
2995
1.16M
}
2996
/* }}} */
2997
2998
static void zend_separate_if_call_and_write(znode *node, zend_ast *ast, uint32_t type) /* {{{ */
2999
337k
{
3000
337k
  if (type != BP_VAR_R
3001
337k
   && type != BP_VAR_IS
3002
   /* Whether a FUNC_ARG is R may only be determined at runtime. */
3003
337k
   && type != BP_VAR_FUNC_ARG
3004
337k
   && zend_is_call(ast)) {
3005
2.71k
    if (node->op_type == IS_VAR) {
3006
2.69k
      zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
3007
2.69k
      opline->result_type = IS_VAR;
3008
2.69k
      opline->result.var = opline->op1.var;
3009
2.69k
    } else {
3010
17
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3011
17
    }
3012
2.71k
  }
3013
337k
}
3014
/* }}} */
3015
3016
static inline void zend_emit_assign_znode(zend_ast *var_ast, znode *value_node) /* {{{ */
3017
7.92k
{
3018
7.92k
  znode dummy_node;
3019
7.92k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
3020
7.92k
    zend_ast_create_znode(value_node));
3021
7.92k
  zend_compile_expr(&dummy_node, assign_ast);
3022
7.92k
  zend_do_free(&dummy_node);
3023
7.92k
}
3024
/* }}} */
3025
3026
static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
3027
235k
{
3028
235k
  zend_ast *var_ast = ast->child[0];
3029
235k
  zend_ast *dim_ast = ast->child[1];
3030
235k
  zend_op *opline;
3031
3032
235k
  znode var_node, dim_node;
3033
3034
235k
  if (is_globals_fetch(var_ast)) {
3035
4.52k
    if (dim_ast == NULL) {
3036
8
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot append to $GLOBALS");
3037
8
    }
3038
3039
4.51k
    zend_compile_expr(&dim_node, dim_ast);
3040
4.51k
    if (dim_node.op_type == IS_CONST) {
3041
3.95k
      convert_to_string(&dim_node.u.constant);
3042
3.95k
    }
3043
3044
4.51k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &dim_node, NULL);
3045
4.51k
    opline->extended_value = ZEND_FETCH_GLOBAL;
3046
4.51k
    zend_adjust_for_fetch_type(opline, result, type);
3047
4.51k
    return opline;
3048
230k
  } else {
3049
230k
    zend_short_circuiting_mark_inner(var_ast);
3050
230k
    opline = zend_delayed_compile_var(&var_node, var_ast, type, 0);
3051
230k
    if (opline) {
3052
138k
      if (type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) {
3053
1.54k
        opline->extended_value |= ZEND_FETCH_DIM_WRITE;
3054
137k
      } else if (opline->opcode == ZEND_FETCH_DIM_W
3055
137k
          || opline->opcode == ZEND_FETCH_DIM_RW
3056
137k
          || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3057
137k
          || opline->opcode == ZEND_FETCH_DIM_UNSET) {
3058
62.9k
        opline->extended_value = ZEND_FETCH_DIM_DIM;
3059
62.9k
      }
3060
138k
    }
3061
230k
  }
3062
3063
230k
  zend_separate_if_call_and_write(&var_node, var_ast, type);
3064
3065
230k
  if (dim_ast == NULL) {
3066
8.17k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3067
279
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
3068
279
    }
3069
7.89k
    if (type == BP_VAR_UNSET) {
3070
11
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
3071
11
    }
3072
7.88k
    dim_node.op_type = IS_UNUSED;
3073
222k
  } else {
3074
222k
    zend_compile_expr(&dim_node, dim_ast);
3075
222k
  }
3076
3077
230k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
3078
230k
  zend_adjust_for_fetch_type(opline, result, type);
3079
230k
  if (by_ref) {
3080
3.88k
    opline->extended_value = ZEND_FETCH_DIM_REF;
3081
3.88k
  }
3082
3083
230k
  if (dim_node.op_type == IS_CONST) {
3084
75.2k
    zend_handle_numeric_dim(opline, &dim_node);
3085
75.2k
  }
3086
230k
  return opline;
3087
230k
}
3088
3089
static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3090
84.5k
{
3091
84.5k
  uint32_t offset = zend_delayed_compile_begin();
3092
84.5k
  zend_delayed_compile_dim(result, ast, type, by_ref);
3093
84.5k
  return zend_delayed_compile_end(offset);
3094
84.5k
}
3095
/* }}} */
3096
3097
static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3098
115k
{
3099
115k
  zend_ast *obj_ast = ast->child[0];
3100
115k
  zend_ast *prop_ast = ast->child[1];
3101
3102
115k
  znode obj_node, prop_node;
3103
115k
  zend_op *opline;
3104
115k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_PROP;
3105
3106
115k
  if (is_this_fetch(obj_ast)) {
3107
9.50k
    if (this_guaranteed_exists()) {
3108
8.51k
      obj_node.op_type = IS_UNUSED;
3109
8.51k
    } else {
3110
996
      zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
3111
996
    }
3112
9.50k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3113
3114
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
3115
     * check for a nullsafe access. */
3116
105k
  } else {
3117
105k
    zend_short_circuiting_mark_inner(obj_ast);
3118
105k
    opline = zend_delayed_compile_var(&obj_node, obj_ast, type, 0);
3119
105k
    if (opline && (opline->opcode == ZEND_FETCH_DIM_W
3120
25.7k
        || opline->opcode == ZEND_FETCH_DIM_RW
3121
25.7k
        || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3122
25.7k
        || opline->opcode == ZEND_FETCH_DIM_UNSET)) {
3123
6.63k
      opline->extended_value = ZEND_FETCH_DIM_OBJ;
3124
6.63k
    }
3125
3126
105k
    zend_separate_if_call_and_write(&obj_node, obj_ast, type);
3127
105k
    if (nullsafe) {
3128
46.6k
      if (obj_node.op_type == IS_TMP_VAR) {
3129
        /* Flush delayed oplines */
3130
5.26k
        zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
3131
5.26k
        uint32_t var = obj_node.u.op.var;
3132
5.26k
        uint32_t count = zend_stack_count(&CG(delayed_oplines_stack));
3133
5.26k
        uint32_t i = count;
3134
3135
195k
        while (i > 0 && oplines[i-1].result_type == IS_TMP_VAR && oplines[i-1].result.var == var) {
3136
191k
          i--;
3137
191k
          if (oplines[i].op1_type == IS_TMP_VAR) {
3138
189k
            var = oplines[i].op1.var;
3139
189k
          } else {
3140
2.11k
            break;
3141
2.11k
          }
3142
191k
        }
3143
197k
        for (; i < count; ++i) {
3144
191k
          if (oplines[i].opcode != ZEND_NOP) {
3145
4.89k
            opline = get_next_op();
3146
4.89k
            memcpy(opline, &oplines[i], sizeof(zend_op));
3147
4.89k
            oplines[i].opcode = ZEND_NOP;
3148
4.89k
            oplines[i].extended_value = opline - CG(active_op_array)->opcodes;
3149
4.89k
          }
3150
191k
        }
3151
5.26k
      }
3152
46.6k
      zend_emit_jmp_null(&obj_node, type);
3153
46.6k
    }
3154
105k
  }
3155
3156
115k
  zend_compile_expr(&prop_node, prop_ast);
3157
3158
115k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
3159
115k
  if (opline->op2_type == IS_CONST) {
3160
112k
    convert_to_string(CT_CONSTANT(opline->op2));
3161
112k
    zend_string_hash_val(Z_STR_P(CT_CONSTANT(opline->op2)));
3162
112k
    opline->extended_value = zend_alloc_cache_slots(3);
3163
112k
  }
3164
3165
115k
  zend_adjust_for_fetch_type(opline, result, type);
3166
3167
115k
  return opline;
3168
115k
}
3169
/* }}} */
3170
3171
static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3172
80.1k
{
3173
80.1k
  uint32_t offset = zend_delayed_compile_begin();
3174
80.1k
  zend_op *opline = zend_delayed_compile_prop(result, ast, type);
3175
80.1k
  if (by_ref) { /* shared with cache_slot */
3176
2.69k
    opline->extended_value |= ZEND_FETCH_REF;
3177
2.69k
  }
3178
80.1k
  return zend_delayed_compile_end(offset);
3179
80.1k
}
3180
/* }}} */
3181
3182
static zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref, bool delayed) /* {{{ */
3183
16.3k
{
3184
16.3k
  zend_ast *class_ast = ast->child[0];
3185
16.3k
  zend_ast *prop_ast = ast->child[1];
3186
3187
16.3k
  znode class_node, prop_node;
3188
16.3k
  zend_op *opline;
3189
3190
16.3k
  zend_short_circuiting_mark_inner(class_ast);
3191
16.3k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
3192
3193
16.3k
  zend_compile_expr(&prop_node, prop_ast);
3194
3195
16.3k
  if (delayed) {
3196
6.37k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3197
9.92k
  } else {
3198
9.92k
    opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3199
9.92k
  }
3200
16.3k
  if (opline->op1_type == IS_CONST) {
3201
14.6k
    convert_to_string(CT_CONSTANT(opline->op1));
3202
14.6k
    opline->extended_value = zend_alloc_cache_slots(3);
3203
14.6k
  }
3204
16.3k
  if (class_node.op_type == IS_CONST) {
3205
10.6k
    opline->op2_type = IS_CONST;
3206
10.6k
    opline->op2.constant = zend_add_class_name_literal(
3207
10.6k
      Z_STR(class_node.u.constant));
3208
10.6k
    if (opline->op1_type != IS_CONST) {
3209
1.03k
      opline->extended_value = zend_alloc_cache_slot();
3210
1.03k
    }
3211
10.6k
  } else {
3212
5.66k
    SET_NODE(opline->op2, &class_node);
3213
5.66k
  }
3214
3215
16.3k
  if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */
3216
2.53k
    opline->extended_value |= ZEND_FETCH_REF;
3217
2.53k
  }
3218
3219
16.3k
  zend_adjust_for_fetch_type(opline, result, type);
3220
16.3k
  return opline;
3221
16.3k
}
3222
/* }}} */
3223
3224
26.3k
static void zend_verify_list_assign_target(zend_ast *var_ast, zend_ast_attr array_style) /* {{{ */ {
3225
26.3k
  if (var_ast->kind == ZEND_AST_ARRAY) {
3226
6.50k
    if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) {
3227
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead");
3228
6
    }
3229
6.49k
    if (array_style != var_ast->attr) {
3230
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()");
3231
6
    }
3232
19.8k
  } else if (!zend_can_write_to_variable(var_ast)) {
3233
97
    zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values");
3234
97
  }
3235
26.3k
}
3236
/* }}} */
3237
3238
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, znode *value_node);
3239
3240
/* Propagate refs used on leaf elements to the surrounding list() structures. */
3241
13.7k
static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
3242
13.7k
  zend_ast_list *list = zend_ast_get_list(ast);
3243
13.7k
  bool has_refs = 0;
3244
13.7k
  uint32_t i;
3245
3246
59.8k
  for (i = 0; i < list->children; ++i) {
3247
46.0k
    zend_ast *elem_ast = list->child[i];
3248
3249
46.0k
    if (elem_ast) {
3250
27.1k
      zend_ast *var_ast = elem_ast->child[0];
3251
27.1k
      if (var_ast->kind == ZEND_AST_ARRAY) {
3252
6.50k
        elem_ast->attr = zend_propagate_list_refs(var_ast);
3253
6.50k
      }
3254
27.1k
      has_refs |= elem_ast->attr;
3255
27.1k
    }
3256
46.0k
  }
3257
3258
13.7k
  return has_refs;
3259
13.7k
}
3260
/* }}} */
3261
3262
static bool list_is_keyed(zend_ast_list *list)
3263
13.4k
{
3264
16.6k
  for (uint32_t i = 0; i < list->children; i++) {
3265
16.6k
    zend_ast *child = list->child[i];
3266
16.6k
    if (child) {
3267
13.3k
      return child->kind == ZEND_AST_ARRAY_ELEM && child->child[1] != NULL;
3268
13.3k
    }
3269
16.6k
  }
3270
28
  return false;
3271
13.4k
}
3272
3273
static void zend_compile_list_assign(
3274
    znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style) /* {{{ */
3275
13.4k
{
3276
13.4k
  zend_ast_list *list = zend_ast_get_list(ast);
3277
13.4k
  uint32_t i;
3278
13.4k
  bool has_elems = 0;
3279
13.4k
  bool is_keyed = list_is_keyed(list);
3280
3281
13.4k
  if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
3282
76
    zval_make_interned_string(&expr_node->u.constant);
3283
76
  }
3284
3285
43.5k
  for (i = 0; i < list->children; ++i) {
3286
30.1k
    zend_ast *elem_ast = list->child[i];
3287
30.1k
    zend_ast *var_ast, *key_ast;
3288
30.1k
    znode fetch_result, dim_node;
3289
30.1k
    zend_op *opline;
3290
3291
30.1k
    if (elem_ast == NULL) {
3292
3.70k
      if (is_keyed) {
3293
16
        zend_error(E_COMPILE_ERROR,
3294
16
          "Cannot use empty array entries in keyed array assignment");
3295
3.69k
      } else {
3296
3.69k
        continue;
3297
3.69k
      }
3298
3.70k
    }
3299
3300
26.4k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
3301
20
      zend_error(E_COMPILE_ERROR,
3302
20
          "Spread operator is not supported in assignments");
3303
20
    }
3304
3305
26.4k
    var_ast = elem_ast->child[0];
3306
26.4k
    key_ast = elem_ast->child[1];
3307
26.4k
    has_elems = 1;
3308
3309
26.4k
    if (is_keyed) {
3310
13.4k
      if (key_ast == NULL) {
3311
13
        zend_error(E_COMPILE_ERROR,
3312
13
          "Cannot mix keyed and unkeyed array entries in assignments");
3313
13
      }
3314
3315
13.4k
      zend_compile_expr(&dim_node, key_ast);
3316
13.4k
    } else {
3317
13.0k
      if (key_ast != NULL) {
3318
6
        zend_error(E_COMPILE_ERROR,
3319
6
          "Cannot mix keyed and unkeyed array entries in assignments");
3320
6
      }
3321
3322
13.0k
      dim_node.op_type = IS_CONST;
3323
13.0k
      ZVAL_LONG(&dim_node.u.constant, i);
3324
13.0k
    }
3325
3326
26.4k
    if (expr_node->op_type == IS_CONST) {
3327
868
      Z_TRY_ADDREF(expr_node->u.constant);
3328
868
    }
3329
3330
26.4k
    zend_verify_list_assign_target(var_ast, array_style);
3331
3332
26.4k
    opline = zend_emit_op(&fetch_result,
3333
26.4k
      elem_ast->attr ? (expr_node->op_type == IS_CV ? ZEND_FETCH_DIM_W : ZEND_FETCH_LIST_W) : ZEND_FETCH_LIST_R, expr_node, &dim_node);
3334
3335
26.4k
    if (dim_node.op_type == IS_CONST) {
3336
26.1k
      zend_handle_numeric_dim(opline, &dim_node);
3337
26.1k
    }
3338
3339
26.4k
    if (elem_ast->attr) {
3340
19.4k
      zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL);
3341
19.4k
    }
3342
26.4k
    if (var_ast->kind == ZEND_AST_ARRAY) {
3343
6.49k
      zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr);
3344
19.9k
    } else if (elem_ast->attr) {
3345
13.2k
      zend_emit_assign_ref_znode(var_ast, &fetch_result);
3346
13.2k
    } else {
3347
6.70k
      zend_emit_assign_znode(var_ast, &fetch_result);
3348
6.70k
    }
3349
26.4k
  }
3350
3351
13.4k
  if (has_elems == 0) {
3352
28
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
3353
28
  }
3354
3355
13.3k
  if (result) {
3356
6.32k
    *result = *expr_node;
3357
7.05k
  } else {
3358
7.05k
    zend_do_free(expr_node);
3359
7.05k
  }
3360
13.3k
}
3361
/* }}} */
3362
3363
static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
3364
541k
{
3365
541k
  if (ast->kind == ZEND_AST_CALL) {
3366
57
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
3367
57
  }
3368
541k
  if (
3369
541k
    ast->kind == ZEND_AST_METHOD_CALL
3370
541k
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
3371
541k
    || ast->kind == ZEND_AST_STATIC_CALL
3372
541k
  ) {
3373
20
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
3374
20
  }
3375
541k
  if (zend_ast_is_short_circuited(ast)) {
3376
22
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use nullsafe operator in write context");
3377
22
  }
3378
541k
  if (is_globals_fetch(ast)) {
3379
26
    zend_error_noreturn(E_COMPILE_ERROR,
3380
26
      "$GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax");
3381
26
  }
3382
541k
}
3383
/* }}} */
3384
3385
/* Detects $a... = $a pattern */
3386
static bool zend_is_assign_to_self(zend_ast *var_ast, zend_ast *expr_ast) /* {{{ */
3387
22.2k
{
3388
22.2k
  if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
3389
19.4k
    return 0;
3390
19.4k
  }
3391
3392
6.71k
  while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
3393
3.93k
    var_ast = var_ast->child[0];
3394
3.93k
  }
3395
3396
2.77k
  if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
3397
467
    return 0;
3398
467
  }
3399
3400
2.31k
  {
3401
2.31k
    zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
3402
2.31k
    zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
3403
2.31k
    bool result = zend_string_equals(name1, name2);
3404
2.31k
    zend_string_release_ex(name1, 0);
3405
2.31k
    zend_string_release_ex(name2, 0);
3406
2.31k
    return result;
3407
2.77k
  }
3408
2.77k
}
3409
/* }}} */
3410
3411
static void zend_compile_expr_with_potential_assign_to_self(
3412
22.2k
    znode *expr_node, zend_ast *expr_ast, zend_ast *var_ast) {
3413
22.2k
  if (zend_is_assign_to_self(var_ast, expr_ast) && !is_this_fetch(expr_ast)) {
3414
    /* $a[0] = $a should evaluate the right $a first */
3415
360
    znode cv_node;
3416
3417
360
    if (zend_try_compile_cv(&cv_node, expr_ast) == FAILURE) {
3418
160
      zend_compile_simple_var_no_cv(expr_node, expr_ast, BP_VAR_R, 0);
3419
200
    } else {
3420
200
      zend_emit_op_tmp(expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3421
200
    }
3422
21.8k
  } else {
3423
21.8k
    zend_compile_expr(expr_node, expr_ast);
3424
21.8k
  }
3425
22.2k
}
3426
3427
static void zend_compile_assign(znode *result, zend_ast *ast) /* {{{ */
3428
279k
{
3429
279k
  zend_ast *var_ast = ast->child[0];
3430
279k
  zend_ast *expr_ast = ast->child[1];
3431
3432
279k
  znode var_node, expr_node;
3433
279k
  zend_op *opline;
3434
279k
  uint32_t offset;
3435
279k
  if (is_this_fetch(var_ast)) {
3436
11
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3437
11
  }
3438
3439
279k
  zend_ensure_writable_variable(var_ast);
3440
3441
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3442
279k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3443
279k
  switch (kind) {
3444
247k
    case ZEND_AST_VAR:
3445
247k
      offset = zend_delayed_compile_begin();
3446
247k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, 0);
3447
247k
      zend_compile_expr(&expr_node, expr_ast);
3448
247k
      zend_delayed_compile_end(offset);
3449
247k
      CG(zend_lineno) = zend_ast_get_lineno(var_ast);
3450
247k
      zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node);
3451
247k
      return;
3452
2.12k
    case ZEND_AST_STATIC_PROP:
3453
2.12k
      offset = zend_delayed_compile_begin();
3454
2.12k
      zend_delayed_compile_var(result, var_ast, BP_VAR_W, 0);
3455
2.12k
      zend_compile_expr(&expr_node, expr_ast);
3456
3457
2.12k
      opline = zend_delayed_compile_end(offset);
3458
2.12k
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
3459
2.12k
      opline->result_type = IS_TMP_VAR;
3460
2.12k
      result->op_type = IS_TMP_VAR;
3461
3462
2.12k
      zend_emit_op_data(&expr_node);
3463
2.12k
      return;
3464
12.6k
    case ZEND_AST_DIM:
3465
12.6k
      offset = zend_delayed_compile_begin();
3466
12.6k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_W, /* by_ref */ false);
3467
12.6k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3468
3469
12.6k
      opline = zend_delayed_compile_end(offset);
3470
12.6k
      opline->opcode = ZEND_ASSIGN_DIM;
3471
12.6k
      opline->result_type = IS_TMP_VAR;
3472
12.6k
      result->op_type = IS_TMP_VAR;
3473
3474
12.6k
      opline = zend_emit_op_data(&expr_node);
3475
12.6k
      return;
3476
10.5k
    case ZEND_AST_PROP:
3477
10.5k
    case ZEND_AST_NULLSAFE_PROP:
3478
10.5k
      offset = zend_delayed_compile_begin();
3479
10.5k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
3480
10.5k
      zend_compile_expr(&expr_node, expr_ast);
3481
3482
10.5k
      opline = zend_delayed_compile_end(offset);
3483
10.5k
      opline->opcode = ZEND_ASSIGN_OBJ;
3484
10.5k
      opline->result_type = IS_TMP_VAR;
3485
10.5k
      result->op_type = IS_TMP_VAR;
3486
3487
10.5k
      zend_emit_op_data(&expr_node);
3488
10.5k
      return;
3489
6.85k
    case ZEND_AST_ARRAY:
3490
6.85k
      if (zend_propagate_list_refs(var_ast)) {
3491
3.92k
        if (!zend_is_variable_or_call(expr_ast)) {
3492
26
          zend_error_noreturn(E_COMPILE_ERROR,
3493
26
            "Cannot assign reference to non referenceable value");
3494
3.90k
        } else {
3495
3.90k
          zend_assert_not_short_circuited(expr_ast);
3496
3.90k
        }
3497
3498
3.90k
        zend_compile_var(&expr_node, expr_ast, BP_VAR_W, 1);
3499
        /* MAKE_REF is usually not necessary for CVs. However, if there are
3500
         * self-assignments, this forces the RHS to evaluate first. */
3501
3.90k
        zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
3502
3.90k
      } else {
3503
2.92k
        if (expr_ast->kind == ZEND_AST_VAR) {
3504
          /* list($a, $b) = $a should evaluate the right $a first */
3505
485
          znode cv_node;
3506
3507
485
          if (zend_try_compile_cv(&cv_node, expr_ast) == FAILURE) {
3508
235
            zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, 0);
3509
250
          } else {
3510
250
            zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3511
250
          }
3512
2.44k
        } else {
3513
2.44k
          zend_compile_expr(&expr_node, expr_ast);
3514
2.44k
        }
3515
2.92k
      }
3516
3517
6.83k
      zend_compile_list_assign(result, var_ast, &expr_node, var_ast->attr);
3518
6.83k
      return;
3519
0
    EMPTY_SWITCH_DEFAULT_CASE();
3520
279k
  }
3521
279k
}
3522
/* }}} */
3523
3524
static void zend_compile_assign_ref(znode *result, zend_ast *ast) /* {{{ */
3525
18.9k
{
3526
18.9k
  zend_ast *target_ast = ast->child[0];
3527
18.9k
  zend_ast *source_ast = ast->child[1];
3528
3529
18.9k
  znode target_node, source_node;
3530
18.9k
  zend_op *opline;
3531
18.9k
  uint32_t offset, flags;
3532
3533
18.9k
  if (is_this_fetch(target_ast)) {
3534
6
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3535
6
  }
3536
18.8k
  zend_ensure_writable_variable(target_ast);
3537
18.8k
  zend_assert_not_short_circuited(source_ast);
3538
18.8k
  if (is_globals_fetch(source_ast)) {
3539
9
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot acquire reference to $GLOBALS");
3540
9
  }
3541
3542
18.8k
  offset = zend_delayed_compile_begin();
3543
18.8k
  zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, 1);
3544
18.8k
  zend_compile_var(&source_node, source_ast, BP_VAR_W, 1);
3545
3546
18.8k
  if ((target_ast->kind != ZEND_AST_VAR
3547
18.8k
    || target_ast->child[0]->kind != ZEND_AST_ZVAL)
3548
18.8k
   && source_ast->kind != ZEND_AST_ZNODE
3549
18.8k
   && source_node.op_type != IS_CV) {
3550
    /* Both LHS and RHS expressions may modify the same data structure,
3551
     * and the modification during RHS evaluation may dangle the pointer
3552
     * to the result of the LHS evaluation.
3553
     * Use MAKE_REF instruction to replace direct pointer with REFERENCE.
3554
     * See: Bug #71539
3555
     */
3556
1.13k
    zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL);
3557
1.13k
  }
3558
3559
18.8k
  opline = zend_delayed_compile_end(offset);
3560
3561
18.8k
  if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
3562
6
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3563
6
  }
3564
3565
18.8k
  flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0;
3566
3567
18.8k
  if (opline && opline->opcode == ZEND_FETCH_OBJ_W) {
3568
866
    opline->opcode = ZEND_ASSIGN_OBJ_REF;
3569
866
    opline->extended_value &= ~ZEND_FETCH_REF;
3570
866
    opline->extended_value |= flags;
3571
866
    zend_emit_op_data(&source_node);
3572
866
    *result = target_node;
3573
18.0k
  } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) {
3574
320
    opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF;
3575
320
    opline->extended_value &= ~ZEND_FETCH_REF;
3576
320
    opline->extended_value |= flags;
3577
320
    zend_emit_op_data(&source_node);
3578
320
    *result = target_node;
3579
17.6k
  } else {
3580
17.6k
    opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
3581
17.6k
    opline->extended_value = flags;
3582
17.6k
  }
3583
18.8k
}
3584
/* }}} */
3585
3586
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, znode *value_node) /* {{{ */
3587
13.6k
{
3588
13.6k
  znode dummy_node;
3589
13.6k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
3590
13.6k
    zend_ast_create_znode(value_node));
3591
13.6k
  zend_compile_expr(&dummy_node, assign_ast);
3592
13.6k
  zend_do_free(&dummy_node);
3593
13.6k
}
3594
/* }}} */
3595
3596
static void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
3597
208k
{
3598
208k
  zend_ast *var_ast = ast->child[0];
3599
208k
  zend_ast *expr_ast = ast->child[1];
3600
208k
  uint32_t opcode = ast->attr;
3601
3602
208k
  znode var_node, expr_node;
3603
208k
  zend_op *opline;
3604
208k
  uint32_t offset, cache_slot;
3605
3606
208k
  zend_ensure_writable_variable(var_ast);
3607
3608
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3609
208k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3610
208k
  switch (kind) {
3611
204k
    case ZEND_AST_VAR:
3612
204k
      offset = zend_delayed_compile_begin();
3613
204k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, 0);
3614
204k
      zend_compile_expr(&expr_node, expr_ast);
3615
204k
      zend_delayed_compile_end(offset);
3616
204k
      opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node);
3617
204k
      opline->extended_value = opcode;
3618
204k
      return;
3619
1.20k
    case ZEND_AST_STATIC_PROP:
3620
1.20k
      offset = zend_delayed_compile_begin();
3621
1.20k
      zend_delayed_compile_var(result, var_ast, BP_VAR_RW, 0);
3622
1.20k
      zend_compile_expr(&expr_node, expr_ast);
3623
3624
1.20k
      opline = zend_delayed_compile_end(offset);
3625
1.20k
      cache_slot = opline->extended_value;
3626
1.20k
      opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP;
3627
1.20k
      opline->extended_value = opcode;
3628
1.20k
      opline->result_type = IS_TMP_VAR;
3629
1.20k
      result->op_type = IS_TMP_VAR;
3630
3631
1.20k
      opline = zend_emit_op_data(&expr_node);
3632
1.20k
      opline->extended_value = cache_slot;
3633
1.20k
      return;
3634
2.36k
    case ZEND_AST_DIM:
3635
2.36k
      offset = zend_delayed_compile_begin();
3636
2.36k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_RW, /* by_ref */ false);
3637
2.36k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3638
3639
2.36k
      opline = zend_delayed_compile_end(offset);
3640
2.36k
      opline->opcode = ZEND_ASSIGN_DIM_OP;
3641
2.36k
      opline->extended_value = opcode;
3642
2.36k
      opline->result_type = IS_TMP_VAR;
3643
2.36k
      result->op_type = IS_TMP_VAR;
3644
3645
2.36k
      zend_emit_op_data(&expr_node);
3646
2.36k
      return;
3647
1.13k
    case ZEND_AST_PROP:
3648
1.13k
    case ZEND_AST_NULLSAFE_PROP:
3649
1.13k
      offset = zend_delayed_compile_begin();
3650
1.13k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
3651
1.13k
      zend_compile_expr(&expr_node, expr_ast);
3652
3653
1.13k
      opline = zend_delayed_compile_end(offset);
3654
1.13k
      cache_slot = opline->extended_value;
3655
1.13k
      opline->opcode = ZEND_ASSIGN_OBJ_OP;
3656
1.13k
      opline->extended_value = opcode;
3657
1.13k
      opline->result_type = IS_TMP_VAR;
3658
1.13k
      result->op_type = IS_TMP_VAR;
3659
3660
1.13k
      opline = zend_emit_op_data(&expr_node);
3661
1.13k
      opline->extended_value = cache_slot;
3662
1.13k
      return;
3663
208k
    EMPTY_SWITCH_DEFAULT_CASE()
3664
208k
  }
3665
208k
}
3666
/* }}} */
3667
3668
7.71k
static uint32_t zend_get_arg_num(zend_function *fn, zend_string *arg_name) {
3669
  // TODO: Caching?
3670
7.71k
  if (fn->type == ZEND_USER_FUNCTION) {
3671
8.48k
    for (uint32_t i = 0; i < fn->common.num_args; i++) {
3672
7.85k
      zend_arg_info *arg_info = &fn->op_array.arg_info[i];
3673
7.85k
      if (zend_string_equals(arg_info->name, arg_name)) {
3674
1.36k
        return i + 1;
3675
1.36k
      }
3676
7.85k
    }
3677
5.72k
  } else {
3678
5.72k
    ZEND_ASSERT(fn->common.num_args == 0 || fn->internal_function.arg_info);
3679
16.5k
    for (uint32_t i = 0; i < fn->common.num_args; i++) {
3680
12.8k
      zend_internal_arg_info *arg_info = &fn->internal_function.arg_info[i];
3681
12.8k
      size_t len = strlen(arg_info->name);
3682
12.8k
      if (zend_string_equals_cstr(arg_name, arg_info->name, len)) {
3683
2.02k
        return i + 1;
3684
2.02k
      }
3685
12.8k
    }
3686
5.72k
  }
3687
3688
  /* Either an invalid argument name, or collected into a variadic argument. */
3689
4.32k
  return (uint32_t) -1;
3690
7.71k
}
3691
3692
static uint32_t zend_compile_args(
3693
    zend_ast *ast, zend_function *fbc, bool *may_have_extra_named_args) /* {{{ */
3694
545k
{
3695
545k
  zend_ast_list *args = zend_ast_get_list(ast);
3696
545k
  uint32_t i;
3697
545k
  bool uses_arg_unpack = 0;
3698
545k
  uint32_t arg_count = 0; /* number of arguments not including unpacks */
3699
3700
  /* Whether named arguments are used syntactically, to enforce language level limitations.
3701
   * May not actually use named argument passing. */
3702
545k
  bool uses_named_args = 0;
3703
  /* Whether there may be any undef arguments due to the use of named arguments. */
3704
545k
  bool may_have_undef = 0;
3705
  /* Whether there may be any extra named arguments collected into a variadic. */
3706
545k
  *may_have_extra_named_args = 0;
3707
3708
1.18M
  for (i = 0; i < args->children; ++i) {
3709
642k
    zend_ast *arg = args->child[i];
3710
642k
    zend_string *arg_name = NULL;
3711
642k
    uint32_t arg_num = i + 1;
3712
3713
642k
    znode arg_node;
3714
642k
    zend_op *opline;
3715
642k
    uint8_t opcode;
3716
3717
642k
    if (arg->kind == ZEND_AST_UNPACK) {
3718
1.56k
      if (uses_named_args) {
3719
6
        zend_error_noreturn(E_COMPILE_ERROR,
3720
6
          "Cannot use argument unpacking after named arguments");
3721
6
      }
3722
3723
      /* Unpack may contain named arguments. */
3724
1.55k
      may_have_undef = 1;
3725
1.55k
      if (!fbc || (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3726
1.16k
        *may_have_extra_named_args = 1;
3727
1.16k
      }
3728
3729
1.55k
      uses_arg_unpack = 1;
3730
1.55k
      fbc = NULL;
3731
3732
1.55k
      zend_compile_expr(&arg_node, arg->child[0]);
3733
1.55k
      opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
3734
1.55k
      opline->op2.num = arg_count;
3735
1.55k
      opline->result.var = EX_NUM_TO_VAR(arg_count - 1);
3736
3737
1.55k
      continue;
3738
1.56k
    }
3739
3740
641k
    if (arg->kind == ZEND_AST_NAMED_ARG) {
3741
11.7k
      uses_named_args = 1;
3742
11.7k
      arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0]));
3743
11.7k
      arg = arg->child[1];
3744
3745
11.7k
      if (fbc && !uses_arg_unpack) {
3746
7.71k
        arg_num = zend_get_arg_num(fbc, arg_name);
3747
7.71k
        if (arg_num == arg_count + 1 && !may_have_undef) {
3748
          /* Using named arguments, but passing in order. */
3749
618
          arg_name = NULL;
3750
618
          arg_count++;
3751
7.09k
        } else {
3752
          // TODO: We could track which arguments were passed, even if out of order.
3753
7.09k
          may_have_undef = 1;
3754
7.09k
          if (arg_num == (uint32_t) -1 && (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3755
447
            *may_have_extra_named_args = 1;
3756
447
          }
3757
7.09k
        }
3758
7.71k
      } else {
3759
4.04k
        arg_num = (uint32_t) -1;
3760
4.04k
        may_have_undef = 1;
3761
4.04k
        *may_have_extra_named_args = 1;
3762
4.04k
      }
3763
629k
    } else {
3764
629k
      if (uses_arg_unpack) {
3765
12
        zend_error_noreturn(E_COMPILE_ERROR,
3766
12
          "Cannot use positional argument after argument unpacking");
3767
12
      }
3768
3769
629k
      if (uses_named_args) {
3770
63
        zend_error_noreturn(E_COMPILE_ERROR,
3771
63
          "Cannot use positional argument after named argument");
3772
63
      }
3773
3774
629k
      arg_count++;
3775
629k
    }
3776
3777
    /* Treat passing of $GLOBALS the same as passing a call.
3778
     * This will error at runtime if the argument is by-ref. */
3779
641k
    if (zend_is_call(arg) || is_globals_fetch(arg)) {
3780
97.8k
      zend_compile_var(&arg_node, arg, BP_VAR_R, 0);
3781
97.8k
      if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
3782
        /* Function call was converted into builtin instruction */
3783
5.58k
        if (!fbc || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3784
2.71k
          opcode = ZEND_SEND_VAL_EX;
3785
2.87k
        } else {
3786
2.87k
          opcode = ZEND_SEND_VAL;
3787
2.87k
        }
3788
92.2k
      } else {
3789
92.2k
        if (fbc && arg_num != (uint32_t) -1) {
3790
45.0k
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3791
701
            opcode = ZEND_SEND_VAR_NO_REF;
3792
44.3k
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3793
            /* For IS_VAR operands, SEND_VAL will pass through the operand without
3794
             * dereferencing, so it will use a by-ref pass if the call returned by-ref
3795
             * and a by-value pass if it returned by-value. */
3796
72
            opcode = ZEND_SEND_VAL;
3797
44.2k
          } else {
3798
44.2k
            opcode = ZEND_SEND_VAR;
3799
44.2k
          }
3800
47.1k
        } else {
3801
47.1k
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3802
47.1k
        }
3803
92.2k
      }
3804
543k
    } else if (zend_is_variable(arg) && !zend_ast_is_short_circuited(arg)) {
3805
125k
      if (fbc && arg_num != (uint32_t) -1) {
3806
34.3k
        if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3807
2.47k
          zend_compile_var(&arg_node, arg, BP_VAR_W, 1);
3808
2.47k
          opcode = ZEND_SEND_REF;
3809
31.9k
        } else {
3810
31.9k
          zend_compile_var(&arg_node, arg, BP_VAR_R, 0);
3811
31.9k
          opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR;
3812
31.9k
        }
3813
91.1k
      } else {
3814
91.1k
        do {
3815
91.1k
          if (arg->kind == ZEND_AST_VAR) {
3816
85.6k
            CG(zend_lineno) = zend_ast_get_lineno(ast);
3817
85.6k
            if (is_this_fetch(arg)) {
3818
1.67k
              zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL);
3819
1.67k
              opcode = ZEND_SEND_VAR_EX;
3820
1.67k
              CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3821
1.67k
              break;
3822
83.9k
            } else if (zend_try_compile_cv(&arg_node, arg) == SUCCESS) {
3823
83.4k
              opcode = ZEND_SEND_VAR_EX;
3824
83.4k
              break;
3825
83.4k
            }
3826
85.6k
          }
3827
6.04k
          opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL);
3828
6.04k
          if (arg_name) {
3829
1.55k
            opline->op2_type = IS_CONST;
3830
1.55k
            zend_string_addref(arg_name);
3831
1.55k
            opline->op2.constant = zend_add_literal_string(&arg_name);
3832
1.55k
            opline->result.num = zend_alloc_cache_slots(2);
3833
4.49k
          } else {
3834
4.49k
            opline->op2.num = arg_num;
3835
4.49k
          }
3836
6.04k
          zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, 1);
3837
6.04k
          opcode = ZEND_SEND_FUNC_ARG;
3838
6.04k
        } while (0);
3839
91.1k
      }
3840
417k
    } else {
3841
417k
      zend_compile_expr(&arg_node, arg);
3842
417k
      if (arg_node.op_type == IS_VAR) {
3843
        /* pass ++$a or something similar */
3844
35.1k
        if (fbc && arg_num != (uint32_t) -1) {
3845
4.82k
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3846
199
            opcode = ZEND_SEND_VAR_NO_REF;
3847
4.62k
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3848
67
            opcode = ZEND_SEND_VAL;
3849
4.55k
          } else {
3850
4.55k
            opcode = ZEND_SEND_VAR;
3851
4.55k
          }
3852
30.3k
        } else {
3853
30.3k
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3854
30.3k
        }
3855
382k
      } else if (arg_node.op_type == IS_CV) {
3856
0
        if (fbc && arg_num != (uint32_t) -1) {
3857
0
          if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3858
0
            opcode = ZEND_SEND_REF;
3859
0
          } else {
3860
0
            opcode = ZEND_SEND_VAR;
3861
0
          }
3862
0
        } else {
3863
0
          opcode = ZEND_SEND_VAR_EX;
3864
0
        }
3865
382k
      } else {
3866
        /* Delay "Only variables can be passed by reference" error to execution */
3867
382k
        if (fbc && arg_num != (uint32_t) -1 && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3868
122k
          opcode = ZEND_SEND_VAL;
3869
259k
        } else {
3870
259k
          opcode = ZEND_SEND_VAL_EX;
3871
259k
        }
3872
382k
      }
3873
417k
    }
3874
3875
0
    opline = zend_emit_op(NULL, opcode, &arg_node, NULL);
3876
641k
    if (arg_name) {
3877
11.1k
      opline->op2_type = IS_CONST;
3878
11.1k
      zend_string_addref(arg_name);
3879
11.1k
      opline->op2.constant = zend_add_literal_string(&arg_name);
3880
11.1k
      opline->result.num = zend_alloc_cache_slots(2);
3881
630k
    } else {
3882
630k
      opline->op2.opline_num = arg_num;
3883
630k
      opline->result.var = EX_NUM_TO_VAR(arg_num - 1);
3884
630k
    }
3885
641k
  }
3886
3887
545k
  if (may_have_undef) {
3888
7.35k
    zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
3889
7.35k
  }
3890
3891
545k
  return arg_count;
3892
545k
}
3893
/* }}} */
3894
3895
ZEND_API uint8_t zend_get_call_op(const zend_op *init_op, zend_function *fbc, bool result_used) /* {{{ */
3896
659k
{
3897
659k
  uint32_t no_discard = result_used ? 0 : ZEND_ACC_NODISCARD;
3898
3899
659k
  if (fbc && init_op->opcode != ZEND_NEW) {
3900
268k
    ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE));
3901
268k
    if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) {
3902
219k
      if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) {
3903
30.1k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3904
30.1k
          return ZEND_DO_ICALL;
3905
30.1k
        } else {
3906
10
          return ZEND_DO_FCALL_BY_NAME;
3907
10
        }
3908
30.1k
      }
3909
219k
    } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){
3910
48.6k
      if (zend_execute_ex == execute_ex) {
3911
23.4k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3912
23.1k
          return ZEND_DO_UCALL;
3913
23.1k
        } else {
3914
263
          return ZEND_DO_FCALL_BY_NAME;
3915
263
        }
3916
23.4k
      }
3917
48.6k
    }
3918
390k
  } else if (zend_execute_ex == execute_ex &&
3919
390k
             !zend_execute_internal &&
3920
390k
             (init_op->opcode == ZEND_INIT_FCALL_BY_NAME ||
3921
241k
              init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) {
3922
201k
    return ZEND_DO_FCALL_BY_NAME;
3923
201k
  }
3924
403k
  return ZEND_DO_FCALL;
3925
659k
}
3926
/* }}} */
3927
3928
static bool zend_compile_call_common(znode *result, zend_ast *args_ast, zend_function *fbc, uint32_t lineno) /* {{{ */
3929
546k
{
3930
546k
  zend_op *opline;
3931
546k
  uint32_t opnum_init = get_next_op_number() - 1;
3932
3933
546k
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
3934
1.36k
    opline = &CG(active_op_array)->opcodes[opnum_init];
3935
1.36k
    opline->extended_value = 0;
3936
3937
1.36k
    if (opline->opcode == ZEND_NEW) {
3938
20
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
3939
20
    }
3940
3941
1.34k
    if (opline->opcode == ZEND_INIT_FCALL) {
3942
251
      opline->op1.num = zend_vm_calc_used_stack(0, fbc);
3943
251
    }
3944
3945
1.34k
    zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT, NULL, NULL);
3946
1.34k
    return true;
3947
1.36k
  }
3948
3949
545k
  bool may_have_extra_named_args;
3950
545k
  uint32_t arg_count = zend_compile_args(args_ast, fbc, &may_have_extra_named_args);
3951
3952
545k
  zend_do_extended_fcall_begin();
3953
3954
545k
  opline = &CG(active_op_array)->opcodes[opnum_init];
3955
545k
  opline->extended_value = arg_count;
3956
3957
545k
  if (opline->opcode == ZEND_INIT_FCALL) {
3958
147k
    opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
3959
147k
  }
3960
3961
545k
  uint8_t call_op = zend_get_call_op(
3962
545k
    opline,
3963
545k
    fbc,
3964
    /* result_used: At this point we do not yet reliably
3965
     * know if the result is used. Deoptimize #[\NoDiscard]
3966
     * calls to be sure. The optimizer will fix this up.
3967
     */
3968
545k
    false
3969
545k
  );
3970
545k
  opline = zend_emit_op(result, call_op, NULL, NULL);
3971
545k
  if (may_have_extra_named_args) {
3972
3.99k
    opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
3973
3.99k
  }
3974
545k
  opline->lineno = lineno;
3975
545k
  zend_do_extended_fcall_end();
3976
545k
  return false;
3977
546k
}
3978
/* }}} */
3979
3980
static bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
3981
384k
{
3982
384k
  zend_string *orig_name = zend_ast_get_str(name_ast);
3983
384k
  bool is_fully_qualified;
3984
3985
384k
  name_node->op_type = IS_CONST;
3986
384k
  ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
3987
384k
    orig_name, name_ast->attr, &is_fully_qualified));
3988
3989
384k
  return !is_fully_qualified && FC(current_namespace);
3990
384k
}
3991
/* }}} */
3992
3993
static void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast, uint32_t lineno) /* {{{ */
3994
66.4k
{
3995
66.4k
  if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
3996
43.9k
    const char *colon;
3997
43.9k
    zend_string *str = Z_STR(name_node->u.constant);
3998
43.9k
    if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
3999
122
      zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
4000
122
      zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
4001
122
      zend_op *opline = get_next_op();
4002
4003
122
      opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
4004
122
      opline->op1_type = IS_CONST;
4005
122
      opline->op1.constant = zend_add_class_name_literal(class);
4006
122
      opline->op2_type = IS_CONST;
4007
122
      opline->op2.constant = zend_add_func_name_literal(method);
4008
      /* 2 slots, for class and method */
4009
122
      opline->result.num = zend_alloc_cache_slots(2);
4010
122
      zval_ptr_dtor(&name_node->u.constant);
4011
43.7k
    } else {
4012
43.7k
      zend_op *opline = get_next_op();
4013
4014
43.7k
      opline->opcode = ZEND_INIT_FCALL_BY_NAME;
4015
43.7k
      opline->op2_type = IS_CONST;
4016
43.7k
      opline->op2.constant = zend_add_func_name_literal(str);
4017
43.7k
      opline->result.num = zend_alloc_cache_slot();
4018
43.7k
    }
4019
43.9k
  } else {
4020
22.5k
    zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node);
4021
22.5k
  }
4022
4023
66.4k
  zend_compile_call_common(result, args_ast, NULL, lineno);
4024
66.4k
}
4025
/* }}} */
4026
4027
static inline bool zend_args_contain_unpack_or_named(zend_ast_list *args) /* {{{ */
4028
295k
{
4029
295k
  uint32_t i;
4030
711k
  for (i = 0; i < args->children; ++i) {
4031
418k
    zend_ast *arg = args->child[i];
4032
418k
    if (arg->kind == ZEND_AST_UNPACK || arg->kind == ZEND_AST_NAMED_ARG) {
4033
2.05k
      return 1;
4034
2.05k
    }
4035
418k
  }
4036
293k
  return 0;
4037
295k
}
4038
/* }}} */
4039
4040
static zend_result zend_compile_func_strlen(znode *result, zend_ast_list *args) /* {{{ */
4041
906
{
4042
906
  znode arg_node;
4043
4044
906
  if (args->children != 1) {
4045
32
    return FAILURE;
4046
32
  }
4047
4048
874
  zend_compile_expr(&arg_node, args->child[0]);
4049
874
  if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
4050
137
    result->op_type = IS_CONST;
4051
137
    ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
4052
137
    zval_ptr_dtor_str(&arg_node.u.constant);
4053
737
  } else {
4054
737
    zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
4055
737
  }
4056
874
  return SUCCESS;
4057
906
}
4058
/* }}} */
4059
4060
static zend_result zend_compile_func_typecheck(znode *result, zend_ast_list *args, uint32_t type) /* {{{ */
4061
1.44k
{
4062
1.44k
  znode arg_node;
4063
1.44k
  zend_op *opline;
4064
4065
1.44k
  if (args->children != 1) {
4066
326
    return FAILURE;
4067
326
  }
4068
4069
1.11k
  zend_compile_expr(&arg_node, args->child[0]);
4070
1.11k
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4071
1.11k
  if (type != _IS_BOOL) {
4072
1.03k
    opline->extended_value = (1 << type);
4073
1.03k
  } else {
4074
80
    opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE);
4075
80
  }
4076
1.11k
  return SUCCESS;
4077
1.44k
}
4078
/* }}} */
4079
4080
static zend_result zend_compile_func_is_scalar(znode *result, zend_ast_list *args) /* {{{ */
4081
57
{
4082
57
  znode arg_node;
4083
57
  zend_op *opline;
4084
4085
57
  if (args->children != 1) {
4086
10
    return FAILURE;
4087
10
  }
4088
4089
47
  zend_compile_expr(&arg_node, args->child[0]);
4090
47
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4091
47
  opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING);
4092
47
  return SUCCESS;
4093
57
}
4094
4095
static zend_result zend_compile_func_cast(znode *result, zend_ast_list *args, uint32_t type) /* {{{ */
4096
1.01k
{
4097
1.01k
  znode arg_node;
4098
1.01k
  zend_op *opline;
4099
4100
1.01k
  if (args->children != 1) {
4101
328
    return FAILURE;
4102
328
  }
4103
4104
687
  zend_compile_expr(&arg_node, args->child[0]);
4105
687
  if (type == _IS_BOOL) {
4106
75
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL);
4107
612
  } else {
4108
612
    opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
4109
612
    opline->extended_value = type;
4110
612
  }
4111
687
  return SUCCESS;
4112
1.01k
}
4113
/* }}} */
4114
4115
static zend_result zend_compile_func_defined(znode *result, zend_ast_list *args) /* {{{ */
4116
1.00k
{
4117
1.00k
  zend_string *name;
4118
1.00k
  zend_op *opline;
4119
4120
1.00k
  if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
4121
155
    return FAILURE;
4122
155
  }
4123
4124
852
  name = zval_get_string(zend_ast_get_zval(args->child[0]));
4125
852
  if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
4126
342
    zend_string_release_ex(name, 0);
4127
342
    return FAILURE;
4128
342
  }
4129
4130
510
  if (zend_try_ct_eval_const(&result->u.constant, name, 0)) {
4131
274
    zend_string_release_ex(name, 0);
4132
274
    zval_ptr_dtor(&result->u.constant);
4133
274
    ZVAL_TRUE(&result->u.constant);
4134
274
    result->op_type = IS_CONST;
4135
274
    return SUCCESS;
4136
274
  }
4137
4138
236
  opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
4139
236
  opline->op1_type = IS_CONST;
4140
236
  LITERAL_STR(opline->op1, name);
4141
236
  opline->extended_value = zend_alloc_cache_slot();
4142
4143
236
  return SUCCESS;
4144
510
}
4145
/* }}} */
4146
4147
static zend_result zend_compile_func_chr(znode *result, zend_ast_list *args) /* {{{ */
4148
768
{
4149
4150
768
  if (args->children == 1 &&
4151
768
      args->child[0]->kind == ZEND_AST_ZVAL &&
4152
768
      Z_TYPE_P(zend_ast_get_zval(args->child[0])) == IS_LONG) {
4153
4154
342
    zend_long c = Z_LVAL_P(zend_ast_get_zval(args->child[0])) & 0xff;
4155
4156
342
    result->op_type = IS_CONST;
4157
342
    ZVAL_CHAR(&result->u.constant, c);
4158
342
    return SUCCESS;
4159
426
  } else {
4160
426
    return FAILURE;
4161
426
  }
4162
768
}
4163
/* }}} */
4164
4165
static zend_result zend_compile_func_ord(znode *result, zend_ast_list *args) /* {{{ */
4166
668
{
4167
668
  if (args->children == 1 &&
4168
668
      args->child[0]->kind == ZEND_AST_ZVAL &&
4169
668
      Z_TYPE_P(zend_ast_get_zval(args->child[0])) == IS_STRING) {
4170
4171
228
    result->op_type = IS_CONST;
4172
228
    ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(zend_ast_get_zval(args->child[0]))[0]);
4173
228
    return SUCCESS;
4174
440
  } else {
4175
440
    return FAILURE;
4176
440
  }
4177
668
}
4178
/* }}} */
4179
4180
/* We can only calculate the stack size for functions that have been fully compiled, otherwise
4181
 * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for
4182
 * directly or indirectly recursive function calls. */
4183
168k
static bool fbc_is_finalized(zend_function *fbc) {
4184
168k
  return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO);
4185
168k
}
4186
4187
static bool zend_compile_ignore_class(zend_class_entry *ce, zend_string *filename)
4188
13.5k
{
4189
13.5k
  if (ce->type == ZEND_INTERNAL_CLASS) {
4190
3.00k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES;
4191
10.5k
  } else {
4192
10.5k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4193
10.5k
      && ce->info.user.filename != filename;
4194
10.5k
  }
4195
13.5k
}
4196
4197
static bool zend_compile_ignore_function(zend_function *fbc, zend_string *filename)
4198
144k
{
4199
144k
  if (fbc->type == ZEND_INTERNAL_FUNCTION) {
4200
121k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS;
4201
121k
  } else {
4202
23.1k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)
4203
23.1k
      || ((CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4204
23.1k
        && fbc->op_array.filename != filename);
4205
23.1k
  }
4206
144k
}
4207
4208
static zend_result zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
4209
3.08k
{
4210
3.08k
  zend_string *name, *lcname;
4211
3.08k
  zend_function *fbc;
4212
3.08k
  zend_op *opline;
4213
4214
3.08k
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
4215
1.61k
    return FAILURE;
4216
1.61k
  }
4217
4218
1.46k
  name = zend_ast_get_str(name_ast);
4219
1.46k
  lcname = zend_string_tolower(name);
4220
4221
1.46k
  fbc = zend_hash_find_ptr(CG(function_table), lcname);
4222
1.46k
  if (!fbc
4223
1.46k
   || !fbc_is_finalized(fbc)
4224
1.46k
   || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
4225
603
    zend_string_release_ex(lcname, 0);
4226
603
    return FAILURE;
4227
603
  }
4228
4229
865
  opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
4230
865
  opline->extended_value = num_args;
4231
865
  opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
4232
865
  opline->op2_type = IS_CONST;
4233
865
  LITERAL_STR(opline->op2, lcname);
4234
865
  opline->result.num = zend_alloc_cache_slot();
4235
4236
865
  return SUCCESS;
4237
1.46k
}
4238
/* }}} */
4239
4240
static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
4241
3.08k
{
4242
3.08k
  zend_op *opline;
4243
3.08k
  znode name_node;
4244
4245
3.08k
  if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
4246
865
    return;
4247
865
  }
4248
4249
2.21k
  zend_compile_expr(&name_node, name_ast);
4250
4251
2.21k
  opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
4252
2.21k
  opline->op1_type = IS_CONST;
4253
2.21k
  LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
4254
2.21k
  opline->extended_value = num_args;
4255
2.21k
}
4256
/* }}} */
4257
4258
/* cufa = call_user_func_array */
4259
static zend_result zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
4260
1.86k
{
4261
1.86k
  znode arg_node;
4262
1.86k
  zend_op *opline;
4263
4264
1.86k
  if (args->children != 2) {
4265
99
    return FAILURE;
4266
99
  }
4267
4268
1.76k
  zend_compile_init_user_func(args->child[0], 0, lcname);
4269
1.76k
  if (args->child[1]->kind == ZEND_AST_CALL
4270
1.76k
   && args->child[1]->child[0]->kind == ZEND_AST_ZVAL
4271
1.76k
   && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING
4272
1.76k
   && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) {
4273
797
    zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]);
4274
797
    zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]);
4275
797
    bool is_fully_qualified;
4276
797
    zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified);
4277
4278
797
    if (zend_string_equals_literal_ci(name, "array_slice")
4279
797
       && !zend_args_contain_unpack_or_named(list)
4280
797
     && list->children == 3
4281
797
     && list->child[1]->kind == ZEND_AST_ZVAL) {
4282
321
      zval *zv = zend_ast_get_zval(list->child[1]);
4283
4284
321
      if (Z_TYPE_P(zv) == IS_LONG
4285
321
       && Z_LVAL_P(zv) >= 0
4286
321
       && Z_LVAL_P(zv) <= 0x7fffffff) {
4287
209
        zend_op *opline;
4288
209
        znode len_node;
4289
4290
209
        zend_compile_expr(&arg_node, list->child[0]);
4291
209
        zend_compile_expr(&len_node, list->child[2]);
4292
209
        opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node);
4293
209
        opline->extended_value = Z_LVAL_P(zv);
4294
209
        zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4295
209
        zend_string_release_ex(name, 0);
4296
209
        return SUCCESS;
4297
209
      }
4298
321
    }
4299
588
    zend_string_release_ex(name, 0);
4300
588
  }
4301
1.55k
  zend_compile_expr(&arg_node, args->child[1]);
4302
1.55k
  zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
4303
1.55k
  zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
4304
1.55k
  opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4305
1.55k
  opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4306
4307
1.55k
  return SUCCESS;
4308
1.76k
}
4309
/* }}} */
4310
4311
/* cuf = call_user_func */
4312
static zend_result zend_compile_func_cuf(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
4313
1.39k
{
4314
1.39k
  uint32_t i;
4315
4316
1.39k
  if (args->children < 1) {
4317
67
    return FAILURE;
4318
67
  }
4319
4320
1.32k
  zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
4321
4.19k
  for (i = 1; i < args->children; ++i) {
4322
2.87k
    zend_ast *arg_ast = args->child[i];
4323
2.87k
    znode arg_node;
4324
2.87k
    zend_op *opline;
4325
4326
2.87k
    zend_compile_expr(&arg_node, arg_ast);
4327
4328
2.87k
    opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
4329
2.87k
    opline->op2.num = i;
4330
2.87k
    opline->result.var = EX_NUM_TO_VAR(i - 1);
4331
2.87k
  }
4332
1.32k
  zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4333
4334
1.32k
  return SUCCESS;
4335
1.39k
}
4336
/* }}} */
4337
4338
static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, zend_function *fbc, uint32_t lineno) /* {{{ */
4339
24.8k
{
4340
24.8k
  if (EG(assertions) >= 0) {
4341
24.8k
    znode name_node;
4342
24.8k
    zend_op *opline;
4343
24.8k
    uint32_t check_op_number = get_next_op_number();
4344
4345
24.8k
    zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
4346
4347
24.8k
    if (fbc && fbc_is_finalized(fbc)) {
4348
24.0k
      name_node.op_type = IS_CONST;
4349
24.0k
      ZVAL_STR_COPY(&name_node.u.constant, name);
4350
4351
24.0k
      opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
4352
24.0k
    } else {
4353
793
      opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
4354
793
      opline->op2_type = IS_CONST;
4355
793
      opline->op2.constant = zend_add_ns_func_name_literal(name);
4356
793
    }
4357
24.8k
    opline->result.num = zend_alloc_cache_slot();
4358
4359
24.8k
    if (args->children == 1) {
4360
      /* add "assert(condition) as assertion message */
4361
23.8k
      zend_ast *arg = zend_ast_create_zval_from_str(
4362
23.8k
        zend_ast_export("assert(", args->child[0], ")"));
4363
23.8k
      if (args->child[0]->kind == ZEND_AST_NAMED_ARG) {
4364
        /* If the original argument was named, add the new argument as named as well,
4365
         * as mixing named and positional is not allowed. */
4366
1.48k
        zend_ast *name = zend_ast_create_zval_from_str(
4367
1.48k
          ZSTR_INIT_LITERAL("description", 0));
4368
1.48k
        arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg);
4369
1.48k
      }
4370
23.8k
      zend_ast_list_add((zend_ast *) args, arg);
4371
23.8k
    }
4372
4373
24.8k
    zend_compile_call_common(result, (zend_ast*)args, fbc, lineno);
4374
4375
24.8k
    opline = &CG(active_op_array)->opcodes[check_op_number];
4376
24.8k
    opline->op2.opline_num = get_next_op_number();
4377
24.8k
    SET_NODE(opline->result, result);
4378
24.8k
  } else {
4379
0
    if (!fbc) {
4380
0
      zend_string_release_ex(name, 0);
4381
0
    }
4382
0
    result->op_type = IS_CONST;
4383
0
    ZVAL_TRUE(&result->u.constant);
4384
0
  }
4385
24.8k
}
4386
/* }}} */
4387
4388
static zend_result zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */
4389
3.36k
{
4390
3.36k
  bool strict = 0;
4391
3.36k
  znode array, needly;
4392
3.36k
  zend_op *opline;
4393
4394
3.36k
  if (args->children == 3) {
4395
1.81k
    if (args->child[2]->kind == ZEND_AST_ZVAL) {
4396
749
      strict = zend_is_true(zend_ast_get_zval(args->child[2]));
4397
1.07k
    } else if (args->child[2]->kind == ZEND_AST_CONST) {
4398
1.03k
      zval value;
4399
1.03k
      zend_ast *name_ast = args->child[2]->child[0];
4400
1.03k
      bool is_fully_qualified;
4401
1.03k
      zend_string *resolved_name = zend_resolve_const_name(
4402
1.03k
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
4403
4404
1.03k
      if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) {
4405
769
        zend_string_release_ex(resolved_name, 0);
4406
769
        return FAILURE;
4407
769
      }
4408
4409
263
      zend_string_release_ex(resolved_name, 0);
4410
263
      strict = zend_is_true(&value);
4411
263
      zval_ptr_dtor(&value);
4412
263
    } else {
4413
38
      return FAILURE;
4414
38
    }
4415
1.81k
  } else if (args->children != 2) {
4416
131
    return FAILURE;
4417
131
  }
4418
4419
2.43k
  if (args->child[1]->kind != ZEND_AST_ARRAY
4420
2.43k
   || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) {
4421
965
    return FAILURE;
4422
965
  }
4423
4424
1.46k
  if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) {
4425
1.20k
    bool ok = 1;
4426
1.20k
    zval *val, tmp;
4427
1.20k
    HashTable *src = Z_ARRVAL(array.u.constant);
4428
1.20k
    HashTable *dst = zend_new_array(zend_hash_num_elements(src));
4429
4430
1.20k
    ZVAL_TRUE(&tmp);
4431
4432
1.20k
    if (strict) {
4433
3.81k
      ZEND_HASH_FOREACH_VAL(src, val) {
4434
3.81k
        if (Z_TYPE_P(val) == IS_STRING) {
4435
18
          zend_hash_add(dst, Z_STR_P(val), &tmp);
4436
1.16k
        } else if (Z_TYPE_P(val) == IS_LONG) {
4437
1.02k
          zend_hash_index_add(dst, Z_LVAL_P(val), &tmp);
4438
1.02k
        } else {
4439
140
          zend_array_destroy(dst);
4440
140
          ok = 0;
4441
140
          break;
4442
140
        }
4443
3.81k
      } ZEND_HASH_FOREACH_END();
4444
619
    } else {
4445
3.28k
      ZEND_HASH_FOREACH_VAL(src, val) {
4446
3.28k
        if (Z_TYPE_P(val) != IS_STRING
4447
1.23k
         || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) {
4448
541
          zend_array_destroy(dst);
4449
541
          ok = 0;
4450
541
          break;
4451
541
        }
4452
698
        zend_hash_add(dst, Z_STR_P(val), &tmp);
4453
698
      } ZEND_HASH_FOREACH_END();
4454
585
    }
4455
4456
1.20k
    zend_array_destroy(src);
4457
1.20k
    if (!ok) {
4458
681
      return FAILURE;
4459
681
    }
4460
523
    Z_ARRVAL(array.u.constant) = dst;
4461
523
  }
4462
784
  array.op_type = IS_CONST;
4463
4464
784
  zend_compile_expr(&needly, args->child[0]);
4465
4466
784
  opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array);
4467
784
  opline->extended_value = strict;
4468
4469
784
  return SUCCESS;
4470
1.46k
}
4471
/* }}} */
4472
4473
static zend_result zend_compile_func_count(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
4474
1.01k
{
4475
1.01k
  znode arg_node;
4476
1.01k
  zend_op *opline;
4477
4478
1.01k
  if (args->children != 1) {
4479
76
    return FAILURE;
4480
76
  }
4481
4482
935
  zend_compile_expr(&arg_node, args->child[0]);
4483
935
  opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL);
4484
935
  opline->extended_value = zend_string_equals_literal(lcname, "sizeof");
4485
4486
935
  return SUCCESS;
4487
1.01k
}
4488
/* }}} */
4489
4490
static zend_result zend_compile_func_get_class(znode *result, zend_ast_list *args) /* {{{ */
4491
1.06k
{
4492
1.06k
  if (args->children == 0) {
4493
65
    zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL);
4494
1.00k
  } else {
4495
1.00k
    znode arg_node;
4496
4497
1.00k
    if (args->children != 1) {
4498
99
      return FAILURE;
4499
99
    }
4500
4501
903
    zend_compile_expr(&arg_node, args->child[0]);
4502
903
    zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL);
4503
903
  }
4504
968
  return SUCCESS;
4505
1.06k
}
4506
/* }}} */
4507
4508
static zend_result zend_compile_func_get_called_class(znode *result, zend_ast_list *args) /* {{{ */
4509
229
{
4510
229
  if (args->children != 0) {
4511
37
    return FAILURE;
4512
37
  }
4513
4514
192
  zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL);
4515
192
  return SUCCESS;
4516
229
}
4517
/* }}} */
4518
4519
static zend_result zend_compile_func_gettype(znode *result, zend_ast_list *args) /* {{{ */
4520
761
{
4521
761
  znode arg_node;
4522
4523
761
  if (args->children != 1) {
4524
22
    return FAILURE;
4525
22
  }
4526
4527
739
  zend_compile_expr(&arg_node, args->child[0]);
4528
739
  zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL);
4529
739
  return SUCCESS;
4530
761
}
4531
/* }}} */
4532
4533
static zend_result zend_compile_func_num_args(znode *result, zend_ast_list *args) /* {{{ */
4534
436
{
4535
436
  if (CG(active_op_array)->function_name && args->children == 0) {
4536
89
    zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL);
4537
89
    return SUCCESS;
4538
347
  } else {
4539
347
    return FAILURE;
4540
347
  }
4541
436
}
4542
/* }}} */
4543
4544
static zend_result zend_compile_func_get_args(znode *result, zend_ast_list *args) /* {{{ */
4545
409
{
4546
409
  if (CG(active_op_array)->function_name && args->children == 0) {
4547
289
    zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL);
4548
289
    return SUCCESS;
4549
289
  } else {
4550
120
    return FAILURE;
4551
120
  }
4552
409
}
4553
/* }}} */
4554
4555
static zend_result zend_compile_func_array_key_exists(znode *result, zend_ast_list *args) /* {{{ */
4556
382
{
4557
382
  znode subject, needle;
4558
4559
382
  if (args->children != 2) {
4560
214
    return FAILURE;
4561
214
  }
4562
4563
168
  zend_compile_expr(&needle, args->child[0]);
4564
168
  zend_compile_expr(&subject, args->child[1]);
4565
4566
168
  zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject);
4567
168
  return SUCCESS;
4568
382
}
4569
/* }}} */
4570
4571
static zend_result zend_compile_func_array_slice(znode *result, zend_ast_list *args) /* {{{ */
4572
1.19k
{
4573
1.19k
  if (CG(active_op_array)->function_name
4574
1.19k
   && args->children == 2
4575
1.19k
   && args->child[0]->kind == ZEND_AST_CALL
4576
1.19k
   && args->child[0]->child[0]->kind == ZEND_AST_ZVAL
4577
1.19k
   && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING
4578
1.19k
   && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST
4579
1.19k
   && args->child[1]->kind == ZEND_AST_ZVAL) {
4580
4581
358
    zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]);
4582
358
    bool is_fully_qualified;
4583
358
    zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified);
4584
358
    zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]);
4585
358
    zval *zv = zend_ast_get_zval(args->child[1]);
4586
358
    znode first;
4587
4588
358
    if (zend_string_equals_literal_ci(name, "func_get_args")
4589
358
     && list->children == 0
4590
358
     && Z_TYPE_P(zv) == IS_LONG
4591
358
     && Z_LVAL_P(zv) >= 0) {
4592
96
      first.op_type = IS_CONST;
4593
96
      ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv));
4594
96
      zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL);
4595
96
      zend_string_release_ex(name, 0);
4596
96
      return SUCCESS;
4597
96
    }
4598
262
    zend_string_release_ex(name, 0);
4599
262
  }
4600
1.09k
  return FAILURE;
4601
1.19k
}
4602
/* }}} */
4603
4604
static uint32_t find_frameless_function_offset(uint32_t arity, void *handler)
4605
60.2k
{
4606
60.2k
  void **handlers = zend_flf_handlers;
4607
60.2k
  void **current = handlers;
4608
484k
  while (current) {
4609
484k
    if (*current == handler) {
4610
60.2k
      return current - handlers;
4611
60.2k
    }
4612
424k
    current++;
4613
424k
  }
4614
4615
0
  return (uint32_t)-1;
4616
60.2k
}
4617
4618
static const zend_frameless_function_info *find_frameless_function_info(zend_ast_list *args, zend_function *fbc, uint32_t type)
4619
146k
{
4620
146k
  if (zend_execute_internal) {
4621
96.1k
    return NULL;
4622
96.1k
  }
4623
4624
50.0k
  if (type != BP_VAR_R) {
4625
483
    return NULL;
4626
483
  }
4627
4628
49.5k
  if (ZEND_USER_CODE(fbc->type)) {
4629
10
    return NULL;
4630
10
  }
4631
4632
49.5k
  const zend_frameless_function_info *frameless_function_info = fbc->internal_function.frameless_function_infos;
4633
49.5k
  if (!frameless_function_info) {
4634
12.4k
    return NULL;
4635
12.4k
  }
4636
4637
37.0k
  if (args->children > 3) {
4638
2.51k
    return NULL;
4639
2.51k
  }
4640
4641
44.1k
  while (frameless_function_info->handler) {
4642
39.8k
    if (frameless_function_info->num_args >= args->children
4643
39.8k
     && fbc->common.required_num_args <= args->children
4644
39.8k
     && (!(fbc->common.fn_flags & ZEND_ACC_VARIADIC)
4645
31.1k
      || frameless_function_info->num_args == args->children)) {
4646
30.1k
      uint32_t num_args = frameless_function_info->num_args;
4647
30.1k
      uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4648
30.1k
      if (offset == (uint32_t)-1) {
4649
0
        continue;
4650
0
      }
4651
30.1k
      return frameless_function_info;
4652
30.1k
    }
4653
9.64k
    frameless_function_info++;
4654
9.64k
  }
4655
4656
4.36k
  return NULL;
4657
34.5k
}
4658
4659
static uint32_t zend_compile_frameless_icall_ex(znode *result, zend_ast_list *args, zend_function *fbc, const zend_frameless_function_info *frameless_function_info, uint32_t type)
4660
30.1k
{
4661
30.1k
  int lineno = CG(zend_lineno);
4662
30.1k
  uint32_t num_args = frameless_function_info->num_args;
4663
30.1k
  uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4664
30.1k
  znode arg_zvs[3];
4665
92.2k
  for (uint32_t i = 0; i < num_args; i++) {
4666
62.1k
    if (i < args->children) {
4667
62.1k
      zend_compile_expr(&arg_zvs[i], args->child[i]);
4668
62.1k
    } else {
4669
0
      zend_internal_arg_info *arg_info = (zend_internal_arg_info *)&fbc->common.arg_info[i];
4670
0
      arg_zvs[i].op_type = IS_CONST;
4671
0
      if (zend_get_default_from_internal_arg_info(&arg_zvs[i].u.constant, arg_info) == FAILURE) {
4672
0
        ZEND_UNREACHABLE();
4673
0
      }
4674
0
    }
4675
62.1k
  }
4676
30.1k
  uint8_t opcode = ZEND_FRAMELESS_ICALL_0 + num_args;
4677
30.1k
  uint32_t opnum = get_next_op_number();
4678
30.1k
  zend_op *opline = zend_emit_op_tmp(result, opcode, NULL, NULL);
4679
30.1k
  opline->extended_value = offset;
4680
30.1k
  opline->lineno = lineno;
4681
30.1k
  if (num_args >= 1) {
4682
30.1k
    SET_NODE(opline->op1, &arg_zvs[0]);
4683
30.1k
  }
4684
30.1k
  if (num_args >= 2) {
4685
30.0k
    SET_NODE(opline->op2, &arg_zvs[1]);
4686
30.0k
  }
4687
30.1k
  if (num_args >= 3) {
4688
1.97k
    zend_emit_op_data(&arg_zvs[2]);
4689
1.97k
  }
4690
30.1k
  return opnum;
4691
30.1k
}
4692
4693
static uint32_t zend_compile_frameless_icall(znode *result, zend_ast_list *args, zend_function *fbc, uint32_t type)
4694
107k
{
4695
107k
  const zend_frameless_function_info *frameless_function_info = find_frameless_function_info(args, fbc, type);
4696
107k
  if (!frameless_function_info) {
4697
104k
    return (uint32_t)-1;
4698
104k
  }
4699
4700
2.54k
  return zend_compile_frameless_icall_ex(result, args, fbc, frameless_function_info, type);
4701
107k
}
4702
4703
static void zend_compile_ns_call(znode *result, znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */
4704
173k
{
4705
173k
  int name_constants = zend_add_ns_func_name_literal(Z_STR(name_node->u.constant));
4706
4707
  /* Find frameless function with same name. */
4708
173k
  zend_function *frameless_function = NULL;
4709
173k
  if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT
4710
173k
   && !zend_args_contain_unpack_or_named(zend_ast_get_list(args_ast))
4711
   /* Avoid blowing up op count with nested frameless branches. */
4712
173k
   && !CG(context).in_jmp_frameless_branch) {
4713
125k
    zend_string *lc_func_name = Z_STR_P(CT_CONSTANT_EX(CG(active_op_array), name_constants + 2));
4714
125k
    frameless_function = zend_hash_find_ptr(CG(function_table), lc_func_name);
4715
125k
  }
4716
4717
  /* Check whether any frameless handler may actually be used. */
4718
173k
  uint32_t jmp_fl_opnum = 0;
4719
173k
  const zend_frameless_function_info *frameless_function_info = NULL;
4720
173k
  if (frameless_function) {
4721
39.0k
    frameless_function_info = find_frameless_function_info(zend_ast_get_list(args_ast), frameless_function, type);
4722
39.0k
    if (frameless_function_info) {
4723
27.6k
      CG(context).in_jmp_frameless_branch = true;
4724
27.6k
      znode op1;
4725
27.6k
      op1.op_type = IS_CONST;
4726
27.6k
      ZVAL_COPY(&op1.u.constant, CT_CONSTANT_EX(CG(active_op_array), name_constants + 1));
4727
27.6k
      jmp_fl_opnum = get_next_op_number();
4728
27.6k
      zend_emit_op(NULL, ZEND_JMP_FRAMELESS, &op1, NULL);
4729
27.6k
    }
4730
39.0k
  }
4731
4732
  /* Compile ns call. */
4733
173k
  zend_op *opline = get_next_op();
4734
173k
  opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
4735
173k
  opline->op2_type = IS_CONST;
4736
173k
  opline->op2.constant = name_constants;
4737
173k
  opline->result.num = zend_alloc_cache_slot();
4738
173k
  zend_compile_call_common(result, args_ast, NULL, lineno);
4739
4740
  /* Compile frameless call. */
4741
173k
  if (frameless_function_info) {
4742
27.5k
    CG(zend_lineno) = lineno;
4743
4744
27.5k
    uint32_t jmp_end_opnum = zend_emit_jump(0);
4745
27.5k
    uint32_t jmp_fl_target = get_next_op_number();
4746
4747
27.5k
    uint32_t flf_icall_opnum = zend_compile_frameless_icall_ex(NULL, zend_ast_get_list(args_ast), frameless_function, frameless_function_info, type);
4748
4749
27.5k
    zend_op *jmp_fl = &CG(active_op_array)->opcodes[jmp_fl_opnum];
4750
27.5k
    jmp_fl->op2.opline_num = jmp_fl_target;
4751
27.5k
    jmp_fl->extended_value = zend_alloc_cache_slot();
4752
27.5k
    zend_op *flf_icall = &CG(active_op_array)->opcodes[flf_icall_opnum];
4753
27.5k
    SET_NODE(flf_icall->result, result);
4754
27.5k
    zend_update_jump_target_to_next(jmp_end_opnum);
4755
4756
27.5k
    CG(context).in_jmp_frameless_branch = false;
4757
27.5k
  }
4758
173k
}
4759
/* }}} */
4760
4761
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node);
4762
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node);
4763
static void zend_compile_rope_finalize(znode *result, uint32_t j, zend_op *init_opline, zend_op *opline);
4764
4765
static zend_result zend_compile_func_sprintf(znode *result, zend_ast_list *args) /* {{{ */
4766
1.44k
{
4767
  /* Bail out if we do not have a format string. */
4768
1.44k
  if (args->children < 1) {
4769
71
    return FAILURE;
4770
71
  }
4771
4772
1.36k
  zend_eval_const_expr(&args->child[0]);
4773
  /* Bail out if the format string is not constant. */
4774
1.36k
  if (args->child[0]->kind != ZEND_AST_ZVAL) {
4775
327
    return FAILURE;
4776
327
  }
4777
4778
1.04k
  zval *format_string = zend_ast_get_zval(args->child[0]);
4779
1.04k
  if (Z_TYPE_P(format_string) != IS_STRING) {
4780
69
    return FAILURE;
4781
69
  }
4782
973
  if (Z_STRLEN_P(format_string) >= 256) {
4783
46
    return FAILURE;
4784
46
  }
4785
4786
927
  char *p;
4787
927
  char *end;
4788
927
  uint32_t placeholder_count;
4789
4790
927
  placeholder_count = 0;
4791
927
  p = Z_STRVAL_P(format_string);
4792
927
  end = p + Z_STRLEN_P(format_string);
4793
4794
3.45k
  for (;;) {
4795
3.45k
    p = memchr(p, '%', end - p);
4796
3.45k
    if (!p) {
4797
745
      break;
4798
745
    }
4799
4800
2.70k
    char *q = p + 1;
4801
2.70k
    if (q == end) {
4802
65
      return FAILURE;
4803
65
    }
4804
4805
2.64k
    switch (*q) {
4806
478
      case 's':
4807
563
      case 'd':
4808
563
        placeholder_count++;
4809
563
        break;
4810
1.96k
      case '%':
4811
1.96k
        break;
4812
117
      default:
4813
117
        return FAILURE;
4814
2.64k
    }
4815
4816
2.52k
    p = q;
4817
2.52k
    p++;
4818
2.52k
  }
4819
4820
  /* Bail out if the number of placeholders does not match the number of values. */
4821
745
  if (placeholder_count != (args->children - 1)) {
4822
112
    return FAILURE;
4823
112
  }
4824
4825
  /* Handle empty format strings. */
4826
633
  if (Z_STRLEN_P(format_string) == 0) {
4827
75
    result->op_type = IS_CONST;
4828
75
    ZVAL_EMPTY_STRING(&result->u.constant);
4829
4830
75
    return SUCCESS;
4831
75
  }
4832
4833
558
  znode *elements = NULL;
4834
4835
558
  if (placeholder_count > 0) {
4836
306
    elements = safe_emalloc(sizeof(*elements), placeholder_count, 0);
4837
306
  }
4838
4839
  /* Compile the value expressions first for error handling that is consistent
4840
   * with a function call: Values that fail to convert to a string may emit errors.
4841
   */
4842
1.07k
  for (uint32_t i = 0; i < placeholder_count; i++) {
4843
515
    zend_compile_expr(elements + i, args->child[1 + i]);
4844
515
  }
4845
4846
558
  uint32_t rope_elements = 0;
4847
558
  uint32_t rope_init_lineno = -1;
4848
558
  zend_op *opline = NULL;
4849
4850
558
  placeholder_count = 0;
4851
558
  p = Z_STRVAL_P(format_string);
4852
558
  end = p + Z_STRLEN_P(format_string);
4853
558
  char *offset = p;
4854
1.90k
  for (;;) {
4855
1.90k
    p = memchr(p, '%', end - p);
4856
1.90k
    if (!p) {
4857
558
      break;
4858
558
    }
4859
4860
1.34k
    char *q = p + 1;
4861
1.34k
    ZEND_ASSERT(q < end);
4862
1.34k
    ZEND_ASSERT(*q == 's' || *q == 'd' || *q == '%');
4863
4864
1.34k
    if (*q == '%') {
4865
      /* Optimization to not create a dedicated rope element for the literal '%':
4866
       * Include the first '%' within the "constant" part instead of dropping the
4867
       * full placeholder.
4868
       */
4869
828
      p++;
4870
828
    }
4871
4872
1.34k
    if (p != offset) {
4873
1.00k
      znode const_node;
4874
1.00k
      const_node.op_type = IS_CONST;
4875
1.00k
      ZVAL_STRINGL(&const_node.u.constant, offset, p - offset);
4876
1.00k
      if (rope_elements == 0) {
4877
219
        rope_init_lineno = get_next_op_number();
4878
219
      }
4879
1.00k
      opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4880
1.00k
    }
4881
4882
1.34k
    if (*q != '%') {
4883
515
      switch (*q) {
4884
434
        case 's':
4885
          /* Perform the cast of constants when actually evaluating the corresponding placeholder
4886
           * for correct error reporting.
4887
           */
4888
434
          if (elements[placeholder_count].op_type == IS_CONST) {
4889
219
            if (Z_TYPE(elements[placeholder_count].u.constant) == IS_ARRAY) {
4890
46
              zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_STRING;
4891
173
            } else {
4892
173
              convert_to_string(&elements[placeholder_count].u.constant);
4893
173
            }
4894
219
          }
4895
434
          break;
4896
81
        case 'd':
4897
81
          zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_LONG;
4898
81
          break;
4899
0
        EMPTY_SWITCH_DEFAULT_CASE();
4900
515
      }
4901
4902
515
      if (rope_elements == 0) {
4903
217
        rope_init_lineno = get_next_op_number();
4904
217
      }
4905
515
      opline = zend_compile_rope_add(result, rope_elements++, &elements[placeholder_count]);
4906
4907
515
      placeholder_count++;
4908
515
    }
4909
4910
1.34k
    p = q;
4911
1.34k
    p++;
4912
1.34k
    offset = p;
4913
1.34k
  }
4914
558
  if (end != offset) {
4915
    /* Add the constant part after the last placeholder. */
4916
340
    znode const_node;
4917
340
    const_node.op_type = IS_CONST;
4918
340
    ZVAL_STRINGL(&const_node.u.constant, offset, end - offset);
4919
340
    if (rope_elements == 0) {
4920
122
      rope_init_lineno = get_next_op_number();
4921
122
    }
4922
340
    opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4923
340
  }
4924
558
  ZEND_ASSERT(opline != NULL);
4925
4926
558
  zend_op *init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
4927
558
  zend_compile_rope_finalize(result, rope_elements, init_opline, opline);
4928
558
  efree(elements);
4929
4930
558
  return SUCCESS;
4931
558
}
4932
4933
static zend_result zend_compile_func_clone(znode *result, zend_ast_list *args)
4934
1.70k
{
4935
1.70k
  znode arg_node;
4936
4937
1.70k
  if (args->children != 1) {
4938
328
    return FAILURE;
4939
328
  }
4940
4941
1.37k
  zend_compile_expr(&arg_node, args->child[0]);
4942
1.37k
  zend_emit_op_tmp(result, ZEND_CLONE, &arg_node, NULL);
4943
4944
1.37k
  return SUCCESS;
4945
1.70k
}
4946
4947
static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, zend_function *fbc, uint32_t type) /* {{{ */
4948
120k
{
4949
120k
  if (zend_string_equals_literal(lcname, "strlen")) {
4950
906
    return zend_compile_func_strlen(result, args);
4951
119k
  } else if (zend_string_equals_literal(lcname, "is_null")) {
4952
203
    return zend_compile_func_typecheck(result, args, IS_NULL);
4953
119k
  } else if (zend_string_equals_literal(lcname, "is_bool")) {
4954
80
    return zend_compile_func_typecheck(result, args, _IS_BOOL);
4955
119k
  } else if (zend_string_equals_literal(lcname, "is_long")
4956
119k
    || zend_string_equals_literal(lcname, "is_int")
4957
119k
    || zend_string_equals_literal(lcname, "is_integer")
4958
119k
  ) {
4959
390
    return zend_compile_func_typecheck(result, args, IS_LONG);
4960
118k
  } else if (zend_string_equals_literal(lcname, "is_float")
4961
118k
    || zend_string_equals_literal(lcname, "is_double")
4962
118k
  ) {
4963
455
    return zend_compile_func_typecheck(result, args, IS_DOUBLE);
4964
118k
  } else if (zend_string_equals_literal(lcname, "is_string")) {
4965
30
    return zend_compile_func_typecheck(result, args, IS_STRING);
4966
118k
  } else if (zend_string_equals_literal(lcname, "is_array")) {
4967
178
    return zend_compile_func_typecheck(result, args, IS_ARRAY);
4968
118k
  } else if (zend_string_equals_literal(lcname, "is_object")) {
4969
85
    return zend_compile_func_typecheck(result, args, IS_OBJECT);
4970
117k
  } else if (zend_string_equals_literal(lcname, "is_resource")) {
4971
24
    return zend_compile_func_typecheck(result, args, IS_RESOURCE);
4972
117k
  } else if (zend_string_equals_literal(lcname, "is_scalar")) {
4973
57
    return zend_compile_func_is_scalar(result, args);
4974
117k
  } else if (zend_string_equals_literal(lcname, "boolval")) {
4975
75
    return zend_compile_func_cast(result, args, _IS_BOOL);
4976
117k
  } else if (zend_string_equals_literal(lcname, "intval")) {
4977
94
    return zend_compile_func_cast(result, args, IS_LONG);
4978
117k
  } else if (zend_string_equals_literal(lcname, "floatval")
4979
117k
    || zend_string_equals_literal(lcname, "doubleval")
4980
117k
  ) {
4981
673
    return zend_compile_func_cast(result, args, IS_DOUBLE);
4982
117k
  } else if (zend_string_equals_literal(lcname, "strval")) {
4983
173
    return zend_compile_func_cast(result, args, IS_STRING);
4984
116k
  } else if (zend_string_equals_literal(lcname, "defined")) {
4985
1.00k
    return zend_compile_func_defined(result, args);
4986
115k
  } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) {
4987
768
    return zend_compile_func_chr(result, args);
4988
115k
  } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) {
4989
668
    return zend_compile_func_ord(result, args);
4990
114k
  } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
4991
1.86k
    return zend_compile_func_cufa(result, args, lcname);
4992
112k
  } else if (zend_string_equals_literal(lcname, "call_user_func")) {
4993
1.39k
    return zend_compile_func_cuf(result, args, lcname);
4994
111k
  } else if (zend_string_equals_literal(lcname, "in_array")) {
4995
3.36k
    return zend_compile_func_in_array(result, args);
4996
107k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_COUNT))
4997
107k
      || zend_string_equals_literal(lcname, "sizeof")) {
4998
1.01k
    return zend_compile_func_count(result, args, lcname);
4999
106k
  } else if (zend_string_equals_literal(lcname, "get_class")) {
5000
1.06k
    return zend_compile_func_get_class(result, args);
5001
105k
  } else if (zend_string_equals_literal(lcname, "get_called_class")) {
5002
229
    return zend_compile_func_get_called_class(result, args);
5003
105k
  } else if (zend_string_equals_literal(lcname, "gettype")) {
5004
761
    return zend_compile_func_gettype(result, args);
5005
104k
  } else if (zend_string_equals_literal(lcname, "func_num_args")) {
5006
436
    return zend_compile_func_num_args(result, args);
5007
104k
  } else if (zend_string_equals_literal(lcname, "func_get_args")) {
5008
409
    return zend_compile_func_get_args(result, args);
5009
103k
  } else if (zend_string_equals_literal(lcname, "array_slice")) {
5010
1.19k
    return zend_compile_func_array_slice(result, args);
5011
102k
  } else if (zend_string_equals_literal(lcname, "array_key_exists")) {
5012
382
    return zend_compile_func_array_key_exists(result, args);
5013
102k
  } else if (zend_string_equals_literal(lcname, "sprintf")) {
5014
1.44k
    return zend_compile_func_sprintf(result, args);
5015
100k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_CLONE))) {
5016
1.70k
    return zend_compile_func_clone(result, args);
5017
99.1k
  } else {
5018
99.1k
    return FAILURE;
5019
99.1k
  }
5020
120k
}
5021
5022
static zend_result zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, zend_function *fbc, uint32_t type) /* {{{ */
5023
143k
{
5024
143k
  if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
5025
0
    return FAILURE;
5026
0
  }
5027
5028
143k
  if (fbc->type != ZEND_INTERNAL_FUNCTION) {
5029
    /* If the function is part of disabled_functions, it may be redeclared as a userland
5030
     * function with a different implementation. Don't use the VM builtin in that case. */
5031
22.3k
    return FAILURE;
5032
22.3k
  }
5033
5034
121k
  if (zend_args_contain_unpack_or_named(args)) {
5035
1.26k
    return FAILURE;
5036
1.26k
  }
5037
5038
120k
  if (zend_try_compile_special_func_ex(result, lcname, args, fbc, type) == SUCCESS) {
5039
13.1k
    return SUCCESS;
5040
13.1k
  }
5041
5042
107k
  return zend_compile_frameless_icall(result, args, fbc, type) != (uint32_t)-1 ? SUCCESS : FAILURE;
5043
120k
}
5044
5045
7
static const char *zend_get_cstring_from_property_hook_kind(zend_property_hook_kind kind) {
5046
7
  switch (kind) {
5047
1
    case ZEND_PROPERTY_HOOK_GET:
5048
1
      return "get";
5049
6
    case ZEND_PROPERTY_HOOK_SET:
5050
6
      return "set";
5051
7
    EMPTY_SWITCH_DEFAULT_CASE()
5052
7
  }
5053
7
}
5054
5055
static zend_string *zend_copy_unmangled_prop_name(zend_string *prop_name)
5056
497
{
5057
497
  if (ZSTR_VAL(prop_name)[0] != '\0') {
5058
306
    return zend_string_copy(prop_name);
5059
306
  } else {
5060
191
    const char *unmangled = zend_get_unmangled_property_name(prop_name);
5061
191
    return zend_string_init(unmangled, strlen(unmangled), /* persistent */ false);
5062
191
  }
5063
497
}
5064
5065
static bool zend_compile_parent_property_hook_call(znode *result, zend_ast *ast, uint32_t type)
5066
17.3k
{
5067
17.3k
  ZEND_ASSERT(ast->kind == ZEND_AST_STATIC_CALL);
5068
5069
17.3k
  zend_ast *class_ast = ast->child[0];
5070
17.3k
  zend_ast *method_ast = ast->child[1];
5071
5072
  /* Recognize parent::$prop::get() pattern. */
5073
17.3k
  if (class_ast->kind != ZEND_AST_STATIC_PROP
5074
17.3k
   || (class_ast->attr & ZEND_PARENTHESIZED_STATIC_PROP)
5075
17.3k
   || class_ast->child[0]->kind != ZEND_AST_ZVAL
5076
17.3k
   || Z_TYPE_P(zend_ast_get_zval(class_ast->child[0])) != IS_STRING
5077
17.3k
   || zend_get_class_fetch_type(zend_ast_get_str(class_ast->child[0])) != ZEND_FETCH_CLASS_PARENT
5078
17.3k
   || class_ast->child[1]->kind != ZEND_AST_ZVAL
5079
17.3k
   || method_ast->kind != ZEND_AST_ZVAL
5080
17.3k
   || Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING
5081
17.3k
   || (!zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "get")
5082
16.9k
    && !zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "set"))) {
5083
16.9k
    return false;
5084
16.9k
  }
5085
5086
380
  zend_class_entry *ce = CG(active_class_entry);
5087
380
  if (!ce) {
5088
6
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"parent\" when no class scope is active");
5089
6
  }
5090
5091
374
  zend_ast *args_ast = ast->child[2];
5092
374
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
5093
6
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for parent property hook call");
5094
6
  }
5095
5096
368
  zval *property_hook_name_zv = zend_ast_get_zval(class_ast->child[1]);
5097
368
  zend_string *property_name = zval_get_string(property_hook_name_zv);
5098
368
  zend_string *hook_name = zend_ast_get_str(method_ast);
5099
368
  zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(hook_name);
5100
368
  ZEND_ASSERT(hook_kind != (uint32_t)-1);
5101
5102
368
  const zend_string *prop_info_name = CG(context).active_property_info_name;
5103
368
  if (!prop_info_name) {
5104
6
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() outside a property hook",
5105
6
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name));
5106
6
  }
5107
5108
362
  const char *unmangled_prop_name = zend_get_unmangled_property_name(prop_info_name);
5109
362
  if (!zend_string_equals_cstr(property_name, unmangled_prop_name, strlen(unmangled_prop_name))) {
5110
22
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property ($%s)",
5111
22
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), unmangled_prop_name);
5112
22
  }
5113
340
  if (hook_kind != CG(context).active_property_hook_kind) {
5114
7
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property hook (%s)",
5115
7
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), zend_get_cstring_from_property_hook_kind(CG(context).active_property_hook_kind));
5116
7
  }
5117
5118
333
  zend_op *opline = get_next_op();
5119
333
  opline->opcode = ZEND_INIT_PARENT_PROPERTY_HOOK_CALL;
5120
333
  opline->op1_type = IS_CONST;
5121
333
  opline->op1.constant = zend_add_literal_string(&property_name);
5122
333
  opline->op2.num = hook_kind;
5123
5124
333
  zend_function *fbc = NULL;
5125
333
  zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast));
5126
5127
333
  return true;
5128
340
}
5129
5130
static void zend_compile_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5131
408k
{
5132
408k
  zend_ast *name_ast = ast->child[0];
5133
408k
  zend_ast *args_ast = ast->child[1];
5134
408k
  bool is_callable_convert = args_ast->kind == ZEND_AST_CALLABLE_CONVERT;
5135
5136
408k
  znode name_node;
5137
5138
408k
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
5139
24.5k
    zend_compile_expr(&name_node, name_ast);
5140
24.5k
    zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno);
5141
24.5k
    return;
5142
24.5k
  }
5143
5144
384k
  {
5145
384k
    bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
5146
384k
    if (runtime_resolution) {
5147
174k
      if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")
5148
174k
          && !is_callable_convert) {
5149
793
        zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL, ast->lineno);
5150
173k
      } else {
5151
173k
        zend_compile_ns_call(result, &name_node, args_ast, ast->lineno, type);
5152
173k
      }
5153
174k
      return;
5154
174k
    }
5155
384k
  }
5156
5157
210k
  {
5158
210k
    zval *name = &name_node.u.constant;
5159
210k
    zend_string *lcname;
5160
210k
    zend_function *fbc;
5161
210k
    zend_op *opline;
5162
5163
210k
    lcname = zend_string_tolower(Z_STR_P(name));
5164
210k
    zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5165
210k
    fbc = fbc_zv ? Z_PTR_P(fbc_zv) : NULL;
5166
5167
    /* Special assert() handling should apply independently of compiler flags. */
5168
210k
    if (fbc && zend_string_equals_literal(lcname, "assert") && !is_callable_convert) {
5169
24.0k
      zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc, ast->lineno);
5170
24.0k
      zend_string_release(lcname);
5171
24.0k
      zval_ptr_dtor(&name_node.u.constant);
5172
24.0k
      return;
5173
24.0k
    }
5174
5175
186k
    if (!fbc
5176
186k
     || !fbc_is_finalized(fbc)
5177
186k
     || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
5178
41.9k
      zend_string_release_ex(lcname, 0);
5179
41.9k
      zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno);
5180
41.9k
      return;
5181
41.9k
    }
5182
5183
144k
    if (!is_callable_convert &&
5184
144k
        zend_try_compile_special_func(result, lcname,
5185
143k
        zend_ast_get_list(args_ast), fbc, type) == SUCCESS
5186
144k
    ) {
5187
15.6k
      zend_string_release_ex(lcname, 0);
5188
15.6k
      zval_ptr_dtor(&name_node.u.constant);
5189
15.6k
      return;
5190
15.6k
    }
5191
5192
128k
    zval_ptr_dtor(&name_node.u.constant);
5193
128k
    ZVAL_NEW_STR(&name_node.u.constant, lcname);
5194
5195
128k
    opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
5196
128k
    opline->result.num = zend_alloc_cache_slot();
5197
5198
    /* Store offset to function from symbol table in op2.extra. */
5199
128k
    if (fbc->type == ZEND_INTERNAL_FUNCTION) {
5200
105k
      Bucket *fbc_bucket = (Bucket*)((uintptr_t)fbc_zv - XtOffsetOf(Bucket, val));
5201
105k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = fbc_bucket - CG(function_table)->arData;
5202
105k
    }
5203
5204
128k
    zend_compile_call_common(result, args_ast, fbc, ast->lineno);
5205
128k
  }
5206
128k
}
5207
/* }}} */
5208
5209
static void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5210
51.7k
{
5211
51.7k
  zend_ast *obj_ast = ast->child[0];
5212
51.7k
  zend_ast *method_ast = ast->child[1];
5213
51.7k
  zend_ast *args_ast = ast->child[2];
5214
5215
51.7k
  znode obj_node, method_node;
5216
51.7k
  zend_op *opline;
5217
51.7k
  zend_function *fbc = NULL;
5218
51.7k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL;
5219
51.7k
  uint32_t short_circuiting_checkpoint = zend_short_circuiting_checkpoint();
5220
5221
51.7k
  if (is_this_fetch(obj_ast)) {
5222
898
    if (this_guaranteed_exists()) {
5223
726
      obj_node.op_type = IS_UNUSED;
5224
726
    } else {
5225
172
      zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
5226
172
    }
5227
898
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
5228
5229
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
5230
     * check for a nullsafe access. */
5231
50.8k
  } else {
5232
50.8k
    zend_short_circuiting_mark_inner(obj_ast);
5233
50.8k
    zend_compile_expr(&obj_node, obj_ast);
5234
50.8k
    if (nullsafe) {
5235
1.77k
      zend_emit_jmp_null(&obj_node, type);
5236
1.77k
    }
5237
50.8k
  }
5238
5239
51.7k
  zend_compile_expr(&method_node, method_ast);
5240
51.7k
  opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
5241
5242
51.7k
  if (method_node.op_type == IS_CONST) {
5243
51.1k
    if (Z_TYPE(method_node.u.constant) != IS_STRING) {
5244
6
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5245
6
    }
5246
5247
51.1k
    opline->op2_type = IS_CONST;
5248
51.1k
    opline->op2.constant = zend_add_func_name_literal(
5249
51.1k
      Z_STR(method_node.u.constant));
5250
51.1k
    opline->result.num = zend_alloc_cache_slots(2);
5251
51.1k
  } else {
5252
570
    SET_NODE(opline->op2, &method_node);
5253
570
  }
5254
5255
  /* Check if this calls a known method on $this */
5256
51.7k
  if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST &&
5257
51.7k
      CG(active_class_entry) && zend_is_scope_known()) {
5258
616
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5259
616
    fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname);
5260
5261
    /* We only know the exact method that is being called if it is either private or final.
5262
     * Otherwise an overriding method in a child class may be called. */
5263
616
    if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) {
5264
281
      fbc = NULL;
5265
281
    }
5266
616
  }
5267
5268
51.7k
  if (zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast))) {
5269
237
    if (short_circuiting_checkpoint != zend_short_circuiting_checkpoint()) {
5270
19
      zend_error_noreturn(E_COMPILE_ERROR,
5271
19
        "Cannot combine nullsafe operator with Closure creation");
5272
19
    }
5273
237
  }
5274
51.7k
}
5275
/* }}} */
5276
5277
static bool zend_is_constructor(zend_string *name) /* {{{ */
5278
17.0k
{
5279
17.0k
  return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
5280
17.0k
}
5281
/* }}} */
5282
5283
static zend_function *zend_get_compatible_func_or_null(zend_class_entry *ce, zend_string *lcname) /* {{{ */
5284
5.32k
{
5285
5.32k
  zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname);
5286
5.32k
  if (!fbc || (fbc->common.fn_flags & ZEND_ACC_PUBLIC) || ce == CG(active_class_entry)) {
5287
5.10k
    return fbc;
5288
5.10k
  }
5289
5290
217
  if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE)
5291
217
    && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED)
5292
217
    && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED))
5293
217
    && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) {
5294
0
    return fbc;
5295
0
  }
5296
5297
217
  return NULL;
5298
217
}
5299
/* }}} */
5300
5301
static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5302
17.3k
{
5303
17.3k
  zend_ast *class_ast = ast->child[0];
5304
17.3k
  zend_ast *method_ast = ast->child[1];
5305
17.3k
  zend_ast *args_ast = ast->child[2];
5306
5307
17.3k
  znode class_node, method_node;
5308
17.3k
  zend_op *opline;
5309
17.3k
  zend_function *fbc = NULL;
5310
5311
17.3k
  if (zend_compile_parent_property_hook_call(result, ast, type)) {
5312
333
    return;
5313
333
  }
5314
5315
17.0k
  zend_short_circuiting_mark_inner(class_ast);
5316
17.0k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5317
5318
17.0k
  zend_compile_expr(&method_node, method_ast);
5319
5320
17.0k
  if (method_node.op_type == IS_CONST) {
5321
15.8k
    zval *name = &method_node.u.constant;
5322
15.8k
    if (Z_TYPE_P(name) != IS_STRING) {
5323
3
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5324
3
    }
5325
15.8k
    if (zend_is_constructor(Z_STR_P(name))) {
5326
116
      zval_ptr_dtor(name);
5327
116
      method_node.op_type = IS_UNUSED;
5328
116
    }
5329
15.8k
  }
5330
5331
17.0k
  opline = get_next_op();
5332
17.0k
  opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
5333
5334
17.0k
  zend_set_class_name_op1(opline, &class_node);
5335
5336
17.0k
  if (method_node.op_type == IS_CONST) {
5337
15.7k
    opline->op2_type = IS_CONST;
5338
15.7k
    opline->op2.constant = zend_add_func_name_literal(
5339
15.7k
      Z_STR(method_node.u.constant));
5340
15.7k
    opline->result.num = zend_alloc_cache_slots(2);
5341
15.7k
  } else {
5342
1.25k
    if (opline->op1_type == IS_CONST) {
5343
423
      opline->result.num = zend_alloc_cache_slot();
5344
423
    }
5345
1.25k
    SET_NODE(opline->op2, &method_node);
5346
1.25k
  }
5347
5348
  /* Check if we already know which method we're calling */
5349
17.0k
  if (opline->op2_type == IS_CONST) {
5350
15.7k
    zend_class_entry *ce = NULL;
5351
15.7k
    if (opline->op1_type == IS_CONST) {
5352
8.97k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5353
8.97k
      ce = zend_hash_find_ptr(CG(class_table), lcname);
5354
8.97k
      if (ce) {
5355
4.67k
        if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5356
0
          ce = NULL;
5357
0
        }
5358
4.67k
      } else if (CG(active_class_entry)
5359
4.30k
          && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5360
157
        ce = CG(active_class_entry);
5361
157
      }
5362
8.97k
    } else if (opline->op1_type == IS_UNUSED
5363
6.80k
        && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5364
6.80k
        && zend_is_scope_known()) {
5365
492
      ce = CG(active_class_entry);
5366
492
    }
5367
15.7k
    if (ce) {
5368
5.32k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5369
5.32k
      fbc = zend_get_compatible_func_or_null(ce, lcname);
5370
5.32k
    }
5371
15.7k
  }
5372
5373
17.0k
  zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast));
5374
17.0k
}
5375
/* }}} */
5376
5377
static void zend_compile_class_decl(znode *result, zend_ast *ast, bool toplevel);
5378
5379
static void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
5380
85.0k
{
5381
85.0k
  zend_ast *class_ast = ast->child[0];
5382
85.0k
  zend_ast *args_ast = ast->child[1];
5383
5384
85.0k
  znode class_node, ctor_result;
5385
85.0k
  zend_op *opline;
5386
5387
85.0k
  if (class_ast->kind == ZEND_AST_CLASS) {
5388
    /* anon class declaration */
5389
2.31k
    zend_compile_class_decl(&class_node, class_ast, 0);
5390
82.6k
  } else {
5391
82.6k
    zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5392
82.6k
  }
5393
5394
85.0k
  opline = zend_emit_op(result, ZEND_NEW, NULL, NULL);
5395
5396
85.0k
  if (class_node.op_type == IS_CONST) {
5397
80.8k
    opline->op1_type = IS_CONST;
5398
80.8k
    opline->op1.constant = zend_add_class_name_literal(
5399
80.8k
      Z_STR(class_node.u.constant));
5400
80.8k
    opline->op2.num = zend_alloc_cache_slot();
5401
80.8k
  } else {
5402
4.12k
    SET_NODE(opline->op1, &class_node);
5403
4.12k
  }
5404
5405
85.0k
  zend_compile_call_common(&ctor_result, args_ast, NULL, ast->lineno);
5406
85.0k
  zend_do_free(&ctor_result);
5407
85.0k
}
5408
/* }}} */
5409
5410
static void zend_compile_global_var(zend_ast *ast) /* {{{ */
5411
1.45k
{
5412
1.45k
  zend_ast *var_ast = ast->child[0];
5413
1.45k
  zend_ast *name_ast = var_ast->child[0];
5414
5415
1.45k
  znode name_node, result;
5416
5417
1.45k
  zend_compile_expr(&name_node, name_ast);
5418
1.45k
  if (name_node.op_type == IS_CONST) {
5419
1.35k
    convert_to_string(&name_node.u.constant);
5420
1.35k
  }
5421
5422
  // TODO(GLOBALS) Forbid "global $GLOBALS"?
5423
1.45k
  if (is_this_fetch(var_ast)) {
5424
6
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable");
5425
1.45k
  } else if (zend_try_compile_cv(&result, var_ast) == SUCCESS) {
5426
1.22k
    zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
5427
1.22k
    opline->extended_value = zend_alloc_cache_slot();
5428
1.22k
  } else {
5429
    /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W
5430
     * to not free the name_node operand, so it can be reused in the following
5431
     * ASSIGN_REF, which then frees it. */
5432
228
    zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
5433
228
    opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
5434
5435
228
    if (name_node.op_type == IS_CONST) {
5436
121
      zend_string_addref(Z_STR(name_node.u.constant));
5437
121
    }
5438
5439
228
    zend_emit_assign_ref_znode(
5440
228
      zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)),
5441
228
      &result
5442
228
    );
5443
228
  }
5444
1.45k
}
5445
/* }}} */
5446
5447
static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */
5448
111k
{
5449
111k
  zend_op *opline;
5450
111k
  if (!CG(active_op_array)->static_variables) {
5451
0
    if (CG(active_op_array)->scope) {
5452
0
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5453
0
    }
5454
0
    CG(active_op_array)->static_variables = zend_new_array(8);
5455
0
  }
5456
5457
111k
  value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value);
5458
5459
111k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5460
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5461
0
  }
5462
5463
111k
  opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL);
5464
111k
  opline->op1_type = IS_CV;
5465
111k
  opline->op1.var = lookup_cv(var_name);
5466
111k
  opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode;
5467
111k
}
5468
/* }}} */
5469
5470
static void zend_compile_static_var(zend_ast *ast) /* {{{ */
5471
944
{
5472
944
  zend_ast *var_ast = ast->child[0];
5473
944
  zend_string *var_name = zend_ast_get_str(var_ast);
5474
5475
944
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5476
6
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5477
6
  }
5478
5479
938
  if (!CG(active_op_array)->static_variables) {
5480
793
    if (CG(active_op_array)->scope) {
5481
244
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5482
244
    }
5483
793
    CG(active_op_array)->static_variables = zend_new_array(8);
5484
793
  }
5485
5486
938
  if (zend_hash_exists(CG(active_op_array)->static_variables, var_name)) {
5487
13
    zend_error_noreturn_unchecked(E_COMPILE_ERROR, "Duplicate declaration of static variable $%S", var_name);
5488
13
  }
5489
5490
925
  zend_eval_const_expr(&ast->child[1]);
5491
925
  zend_ast *value_ast = ast->child[1];
5492
5493
925
  if (!value_ast || value_ast->kind == ZEND_AST_ZVAL) {
5494
557
    zval *value_zv = value_ast
5495
557
      ? zend_ast_get_zval(value_ast)
5496
557
      : &EG(uninitialized_zval);
5497
557
    Z_TRY_ADDREF_P(value_zv);
5498
557
    zend_compile_static_var_common(var_name, value_zv, ZEND_BIND_REF);
5499
557
  } else {
5500
368
    zend_op *opline;
5501
5502
368
    zval *placeholder_ptr = zend_hash_update(CG(active_op_array)->static_variables, var_name, &EG(uninitialized_zval));
5503
368
    uint32_t placeholder_offset = (uint32_t)((char*)placeholder_ptr - (char*)CG(active_op_array)->static_variables->arData);
5504
5505
368
    uint32_t static_def_jmp_opnum = get_next_op_number();
5506
368
    opline = zend_emit_op(NULL, ZEND_BIND_INIT_STATIC_OR_JMP, NULL, NULL);
5507
368
    opline->op1_type = IS_CV;
5508
368
    opline->op1.var = lookup_cv(var_name);
5509
368
    opline->extended_value = placeholder_offset;
5510
5511
368
    znode expr;
5512
368
    zend_compile_expr(&expr, value_ast);
5513
5514
368
    opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, &expr);
5515
368
    opline->op1_type = IS_CV;
5516
368
    opline->op1.var = lookup_cv(var_name);
5517
368
    opline->extended_value = placeholder_offset | ZEND_BIND_REF;
5518
5519
368
    zend_update_jump_target_to_next(static_def_jmp_opnum);
5520
368
  }
5521
925
}
5522
/* }}} */
5523
5524
static void zend_compile_unset(zend_ast *ast) /* {{{ */
5525
7.80k
{
5526
7.80k
  zend_ast *var_ast = ast->child[0];
5527
7.80k
  znode var_node;
5528
7.80k
  zend_op *opline;
5529
5530
7.80k
  zend_ensure_writable_variable(var_ast);
5531
5532
7.80k
  if (is_global_var_fetch(var_ast)) {
5533
352
    if (!var_ast->child[1]) {
5534
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
5535
6
    }
5536
5537
346
    zend_compile_expr(&var_node, var_ast->child[1]);
5538
346
    if (var_node.op_type == IS_CONST) {
5539
272
      convert_to_string(&var_node.u.constant);
5540
272
    }
5541
5542
346
    opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL);
5543
346
    opline->extended_value = ZEND_FETCH_GLOBAL;
5544
346
    return;
5545
352
  }
5546
5547
7.45k
  switch (var_ast->kind) {
5548
3.05k
    case ZEND_AST_VAR:
5549
3.05k
      if (is_this_fetch(var_ast)) {
5550
9
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this");
5551
3.05k
      } else if (zend_try_compile_cv(&var_node, var_ast) == SUCCESS) {
5552
2.91k
        opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL);
5553
2.91k
      } else {
5554
137
        opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, 0);
5555
137
        opline->opcode = ZEND_UNSET_VAR;
5556
137
      }
5557
3.05k
      return;
5558
3.05k
    case ZEND_AST_DIM:
5559
1.55k
      opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET, /* by_ref */ false);
5560
1.55k
      opline->opcode = ZEND_UNSET_DIM;
5561
1.55k
      return;
5562
2.78k
    case ZEND_AST_PROP:
5563
2.78k
    case ZEND_AST_NULLSAFE_PROP:
5564
2.78k
      opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, 0);
5565
2.78k
      opline->opcode = ZEND_UNSET_OBJ;
5566
2.78k
      return;
5567
51
    case ZEND_AST_STATIC_PROP:
5568
51
      opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, 0, 0);
5569
51
      opline->opcode = ZEND_UNSET_STATIC_PROP;
5570
51
      return;
5571
7.45k
    EMPTY_SWITCH_DEFAULT_CASE()
5572
7.45k
  }
5573
7.45k
}
5574
/* }}} */
5575
5576
static bool zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */
5577
40.0k
{
5578
40.0k
  zend_loop_var *base;
5579
40.0k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5580
5581
40.0k
  if (!loop_var) {
5582
1.07k
    return 1;
5583
1.07k
  }
5584
38.9k
  base = zend_stack_base(&CG(loop_var_stack));
5585
47.1k
  for (; loop_var >= base; loop_var--) {
5586
45.1k
    if (loop_var->opcode == ZEND_FAST_CALL) {
5587
2.79k
      zend_op *opline = get_next_op();
5588
5589
2.79k
      opline->opcode = ZEND_FAST_CALL;
5590
2.79k
      opline->result_type = IS_TMP_VAR;
5591
2.79k
      opline->result.var = loop_var->var_num;
5592
2.79k
      if (return_value) {
5593
784
        SET_NODE(opline->op2, return_value);
5594
784
      }
5595
2.79k
      opline->op1.num = loop_var->try_catch_offset;
5596
42.3k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5597
1.76k
      zend_op *opline = get_next_op();
5598
1.76k
      opline->opcode = ZEND_DISCARD_EXCEPTION;
5599
1.76k
      opline->op1_type = IS_TMP_VAR;
5600
1.76k
      opline->op1.var = loop_var->var_num;
5601
40.5k
    } else if (loop_var->opcode == ZEND_RETURN) {
5602
      /* Stack separator */
5603
35.3k
      break;
5604
35.3k
    } else if (depth <= 1) {
5605
1.59k
      return 1;
5606
3.63k
    } else if (loop_var->opcode == ZEND_NOP) {
5607
      /* Loop doesn't have freeable variable */
5608
2.18k
      depth--;
5609
2.18k
    } else {
5610
1.44k
      zend_op *opline;
5611
5612
1.44k
      ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR));
5613
1.44k
      opline = get_next_op();
5614
1.44k
      opline->opcode = loop_var->opcode;
5615
1.44k
      opline->op1_type = loop_var->var_type;
5616
1.44k
      opline->op1.var = loop_var->var_num;
5617
1.44k
      opline->extended_value = ZEND_FREE_ON_RETURN;
5618
1.44k
      depth--;
5619
1.44k
      }
5620
45.1k
  }
5621
37.3k
  return (depth == 0);
5622
38.9k
}
5623
/* }}} */
5624
5625
static bool zend_handle_loops_and_finally(znode *return_value) /* {{{ */
5626
38.3k
{
5627
38.3k
  return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value);
5628
38.3k
}
5629
/* }}} */
5630
5631
static bool zend_has_finally_ex(zend_long depth) /* {{{ */
5632
1.68k
{
5633
1.68k
  zend_loop_var *base;
5634
1.68k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5635
5636
1.68k
  if (!loop_var) {
5637
305
    return 0;
5638
305
  }
5639
1.38k
  base = zend_stack_base(&CG(loop_var_stack));
5640
3.07k
  for (; loop_var >= base; loop_var--) {
5641
2.97k
    if (loop_var->opcode == ZEND_FAST_CALL) {
5642
181
      return 1;
5643
2.79k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5644
1.55k
    } else if (loop_var->opcode == ZEND_RETURN) {
5645
      /* Stack separator */
5646
1.09k
      return 0;
5647
1.09k
    } else if (depth <= 1) {
5648
0
      return 0;
5649
456
    } else {
5650
456
      depth--;
5651
456
      }
5652
2.97k
  }
5653
100
  return 0;
5654
1.38k
}
5655
/* }}} */
5656
5657
static bool zend_has_finally(void) /* {{{ */
5658
1.68k
{
5659
1.68k
  return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1);
5660
1.68k
}
5661
/* }}} */
5662
5663
static void zend_compile_return(zend_ast *ast) /* {{{ */
5664
36.5k
{
5665
36.5k
  zend_ast *expr_ast = ast->child[0];
5666
36.5k
  bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0;
5667
36.5k
  bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
5668
5669
36.5k
  znode expr_node;
5670
36.5k
  zend_op *opline;
5671
5672
36.5k
  if (is_generator) {
5673
    /* For generators the by-ref flag refers to yields, not returns */
5674
1.80k
    by_ref = 0;
5675
1.80k
  }
5676
5677
36.5k
  if (!expr_ast) {
5678
858
    expr_node.op_type = IS_CONST;
5679
858
    ZVAL_NULL(&expr_node.u.constant);
5680
35.7k
  } else if (by_ref && zend_is_variable(expr_ast)) {
5681
1.84k
    zend_assert_not_short_circuited(expr_ast);
5682
1.84k
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, 1);
5683
33.8k
  } else {
5684
33.8k
    zend_compile_expr(&expr_node, expr_ast);
5685
33.8k
  }
5686
5687
36.5k
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
5688
36.5k
   && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
5689
36.5k
   && zend_has_finally()) {
5690
    /* Copy return value into temporary VAR to avoid modification in finally code */
5691
181
    if (by_ref) {
5692
40
      zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
5693
141
    } else {
5694
141
      zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL);
5695
141
    }
5696
181
  }
5697
5698
  /* Generator return types are handled separately */
5699
36.5k
  if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
5700
5.11k
    zend_emit_return_type_check(
5701
5.11k
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, 0);
5702
5.11k
  }
5703
5704
36.5k
  zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
5705
5706
36.5k
  opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
5707
36.5k
    &expr_node, NULL);
5708
5709
36.5k
  if (by_ref && expr_ast) {
5710
2.99k
    if (zend_is_call(expr_ast)) {
5711
554
      opline->extended_value = ZEND_RETURNS_FUNCTION;
5712
2.44k
    } else if (!zend_is_variable(expr_ast) || zend_ast_is_short_circuited(expr_ast)) {
5713
608
      opline->extended_value = ZEND_RETURNS_VALUE;
5714
608
    }
5715
2.99k
  }
5716
36.5k
}
5717
/* }}} */
5718
5719
static void zend_compile_void_cast(znode *result, zend_ast *ast)
5720
485
{
5721
485
  zend_ast *expr_ast = ast->child[0];
5722
485
  znode expr_node;
5723
485
  zend_op *opline;
5724
5725
485
  zend_compile_expr(&expr_node, expr_ast);
5726
5727
485
  switch (expr_node.op_type) {
5728
149
    case IS_TMP_VAR:
5729
290
    case IS_VAR:
5730
290
      opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
5731
290
      opline->extended_value = ZEND_FREE_VOID_CAST;
5732
290
      break;
5733
156
    case IS_CONST:
5734
156
      zend_do_free(&expr_node);
5735
156
      break;
5736
485
  }
5737
485
}
5738
5739
static void zend_compile_echo(zend_ast *ast) /* {{{ */
5740
295k
{
5741
295k
  zend_op *opline;
5742
295k
  zend_ast *expr_ast = ast->child[0];
5743
5744
295k
  znode expr_node;
5745
295k
  zend_compile_expr(&expr_node, expr_ast);
5746
5747
295k
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
5748
295k
  opline->extended_value = 0;
5749
295k
}
5750
/* }}} */
5751
5752
static void zend_compile_throw(znode *result, zend_ast *ast) /* {{{ */
5753
2.97k
{
5754
2.97k
  zend_ast *expr_ast = ast->child[0];
5755
5756
2.97k
  znode expr_node;
5757
2.97k
  zend_compile_expr(&expr_node, expr_ast);
5758
5759
2.97k
  zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
5760
2.97k
  if (result) {
5761
    /* Mark this as an "expression throw" for opcache. */
5762
569
    opline->extended_value = ZEND_THROW_IS_EXPR;
5763
569
    result->op_type = IS_CONST;
5764
569
    ZVAL_TRUE(&result->u.constant);
5765
569
  }
5766
2.97k
}
5767
/* }}} */
5768
5769
static void zend_compile_break_continue(zend_ast *ast) /* {{{ */
5770
1.75k
{
5771
1.75k
  zend_ast *depth_ast = ast->child[0];
5772
5773
1.75k
  zend_op *opline;
5774
1.75k
  zend_long depth;
5775
5776
1.75k
  ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
5777
5778
1.75k
  if (depth_ast) {
5779
412
    zval *depth_zv;
5780
412
    if (depth_ast->kind != ZEND_AST_ZVAL) {
5781
19
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand "
5782
19
        "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5783
19
    }
5784
5785
393
    depth_zv = zend_ast_get_zval(depth_ast);
5786
393
    if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
5787
7
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers",
5788
7
        ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5789
7
    }
5790
5791
386
    depth = Z_LVAL_P(depth_zv);
5792
1.34k
  } else {
5793
1.34k
    depth = 1;
5794
1.34k
  }
5795
5796
1.73k
  if (CG(context).current_brk_cont == -1) {
5797
42
    zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
5798
42
      ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5799
1.69k
  } else {
5800
1.69k
    if (!zend_handle_loops_and_finally_ex(depth, NULL)) {
5801
98
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s",
5802
98
        ast->kind == ZEND_AST_BREAK ? "break" : "continue",
5803
98
        depth, depth == 1 ? "" : "s");
5804
98
    }
5805
1.69k
  }
5806
5807
1.59k
  if (ast->kind == ZEND_AST_CONTINUE) {
5808
828
    int d, cur = CG(context).current_brk_cont;
5809
938
    for (d = depth - 1; d > 0; d--) {
5810
110
      cur = CG(context).brk_cont_array[cur].parent;
5811
110
      ZEND_ASSERT(cur != -1);
5812
110
    }
5813
5814
828
    if (CG(context).brk_cont_array[cur].is_switch) {
5815
167
      if (depth == 1) {
5816
91
        if (CG(context).brk_cont_array[cur].parent == -1) {
5817
74
          zend_error(E_WARNING,
5818
74
            "\"continue\" targeting switch is equivalent to \"break\"");
5819
74
        } else {
5820
17
          zend_error(E_WARNING,
5821
17
            "\"continue\" targeting switch is equivalent to \"break\". " \
5822
17
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
5823
17
            depth + 1);
5824
17
        }
5825
91
      } else {
5826
76
        if (CG(context).brk_cont_array[cur].parent == -1) {
5827
27
          zend_error(E_WARNING,
5828
27
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"",
5829
27
            depth, depth);
5830
49
        } else {
5831
49
          zend_error(E_WARNING,
5832
49
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
5833
49
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
5834
49
            depth, depth, depth + 1);
5835
49
        }
5836
76
      }
5837
167
    }
5838
828
  }
5839
5840
1.59k
  opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
5841
1.59k
  opline->op1.num = CG(context).current_brk_cont;
5842
1.59k
  opline->op2.num = depth;
5843
1.59k
}
5844
/* }}} */
5845
5846
void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */
5847
1.66k
{
5848
1.66k
  zend_label *dest;
5849
1.66k
  int current, remove_oplines = opline->op1.num;
5850
1.66k
  zval *label;
5851
1.66k
  uint32_t opnum = opline - op_array->opcodes;
5852
5853
1.66k
  label = CT_CONSTANT_EX(op_array, opline->op2.constant);
5854
1.66k
  if (CG(context).labels == NULL ||
5855
1.66k
      (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL
5856
1.66k
  ) {
5857
67
    CG(in_compilation) = 1;
5858
67
    CG(active_op_array) = op_array;
5859
67
    CG(zend_lineno) = opline->lineno;
5860
67
    zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
5861
67
  }
5862
5863
1.59k
  zval_ptr_dtor_str(label);
5864
1.59k
  ZVAL_NULL(label);
5865
5866
1.59k
  current = opline->extended_value;
5867
3.16k
  for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) {
5868
1.58k
    if (current == -1) {
5869
14
      CG(in_compilation) = 1;
5870
14
      CG(active_op_array) = op_array;
5871
14
      CG(zend_lineno) = opline->lineno;
5872
14
      zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
5873
14
    }
5874
1.56k
    if (CG(context).brk_cont_array[current].start >= 0) {
5875
822
      remove_oplines--;
5876
822
    }
5877
1.56k
  }
5878
5879
3.79k
  for (current = 0; current < op_array->last_try_catch; ++current) {
5880
2.38k
    zend_try_catch_element *elem = &op_array->try_catch_array[current];
5881
2.38k
    if (elem->try_op > opnum) {
5882
169
      break;
5883
169
    }
5884
2.21k
    if (elem->finally_op && opnum < elem->finally_op - 1
5885
2.21k
      && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op)
5886
2.21k
    ) {
5887
1.13k
      remove_oplines--;
5888
1.13k
    }
5889
2.21k
  }
5890
5891
1.58k
  opline->opcode = ZEND_JMP;
5892
1.58k
  SET_UNUSED(opline->op1);
5893
1.58k
  SET_UNUSED(opline->op2);
5894
1.58k
  SET_UNUSED(opline->result);
5895
1.58k
  opline->op1.opline_num = dest->opline_num;
5896
1.58k
  opline->extended_value = 0;
5897
5898
1.58k
  ZEND_ASSERT(remove_oplines >= 0);
5899
2.15k
  while (remove_oplines--) {
5900
575
    opline--;
5901
575
    MAKE_NOP(opline);
5902
575
    ZEND_VM_SET_OPCODE_HANDLER(opline);
5903
575
  }
5904
1.58k
}
5905
/* }}} */
5906
5907
static void zend_compile_goto(zend_ast *ast) /* {{{ */
5908
1.91k
{
5909
1.91k
  zend_ast *label_ast = ast->child[0];
5910
1.91k
  znode label_node;
5911
1.91k
  zend_op *opline;
5912
5913
1.91k
  zend_compile_expr(&label_node, label_ast);
5914
5915
  /* Label resolution and unwinding adjustments happen in pass two. */
5916
1.91k
  uint32_t opnum_start = get_next_op_number();
5917
1.91k
  zend_handle_loops_and_finally(NULL);
5918
1.91k
  opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
5919
1.91k
  opline->op1.num = get_next_op_number() - opnum_start - 1;
5920
1.91k
  opline->extended_value = CG(context).current_brk_cont;
5921
1.91k
}
5922
/* }}} */
5923
5924
static void zend_compile_label(zend_ast *ast) /* {{{ */
5925
4.67k
{
5926
4.67k
  zend_string *label = zend_ast_get_str(ast->child[0]);
5927
4.67k
  zend_label dest;
5928
5929
4.67k
  if (!CG(context).labels) {
5930
4.14k
    ALLOC_HASHTABLE(CG(context).labels);
5931
4.14k
    zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0);
5932
4.14k
  }
5933
5934
4.67k
  dest.brk_cont = CG(context).current_brk_cont;
5935
4.67k
  dest.opline_num = get_next_op_number();
5936
5937
4.67k
  if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
5938
56
    zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
5939
56
  }
5940
4.67k
}
5941
/* }}} */
5942
5943
static void zend_compile_while(zend_ast *ast) /* {{{ */
5944
1.25k
{
5945
1.25k
  zend_ast *cond_ast = ast->child[0];
5946
1.25k
  zend_ast *stmt_ast = ast->child[1];
5947
1.25k
  znode cond_node;
5948
1.25k
  uint32_t opnum_start, opnum_jmp, opnum_cond;
5949
5950
1.25k
  opnum_jmp = zend_emit_jump(0);
5951
5952
1.25k
  zend_begin_loop(ZEND_NOP, NULL, 0);
5953
5954
1.25k
  opnum_start = get_next_op_number();
5955
1.25k
  zend_compile_stmt(stmt_ast);
5956
5957
1.25k
  opnum_cond = get_next_op_number();
5958
1.25k
  zend_update_jump_target(opnum_jmp, opnum_cond);
5959
1.25k
  zend_compile_expr(&cond_node, cond_ast);
5960
5961
1.25k
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
5962
5963
1.25k
  zend_end_loop(opnum_cond, NULL);
5964
1.25k
}
5965
/* }}} */
5966
5967
static void zend_compile_do_while(zend_ast *ast) /* {{{ */
5968
478
{
5969
478
  zend_ast *stmt_ast = ast->child[0];
5970
478
  zend_ast *cond_ast = ast->child[1];
5971
5972
478
  znode cond_node;
5973
478
  uint32_t opnum_start, opnum_cond;
5974
5975
478
  zend_begin_loop(ZEND_NOP, NULL, 0);
5976
5977
478
  opnum_start = get_next_op_number();
5978
478
  zend_compile_stmt(stmt_ast);
5979
5980
478
  opnum_cond = get_next_op_number();
5981
478
  zend_compile_expr(&cond_node, cond_ast);
5982
5983
478
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
5984
5985
478
  zend_end_loop(opnum_cond, NULL);
5986
478
}
5987
/* }}} */
5988
5989
static void zend_compile_for_expr_list(znode *result, zend_ast *ast) /* {{{ */
5990
56.1k
{
5991
56.1k
  zend_ast_list *list;
5992
56.1k
  uint32_t i;
5993
5994
56.1k
  result->op_type = IS_CONST;
5995
56.1k
  ZVAL_TRUE(&result->u.constant);
5996
5997
56.1k
  if (!ast) {
5998
19.3k
    return;
5999
19.3k
  }
6000
6001
36.8k
  list = zend_ast_get_list(ast);
6002
76.7k
  for (i = 0; i < list->children; ++i) {
6003
39.9k
    zend_ast *expr_ast = list->child[i];
6004
6005
39.9k
    zend_do_free(result);
6006
39.9k
    if (expr_ast->kind == ZEND_AST_CAST_VOID) {
6007
79
      zend_compile_void_cast(NULL, expr_ast);
6008
79
      result->op_type = IS_CONST;
6009
79
      ZVAL_NULL(&result->u.constant);
6010
39.8k
    } else {
6011
39.8k
      zend_compile_expr(result, expr_ast);
6012
39.8k
    }
6013
39.9k
  }
6014
36.8k
}
6015
/* }}} */
6016
6017
static void zend_compile_for(zend_ast *ast) /* {{{ */
6018
18.7k
{
6019
18.7k
  zend_ast *init_ast = ast->child[0];
6020
18.7k
  zend_ast *cond_ast = ast->child[1];
6021
18.7k
  zend_ast *loop_ast = ast->child[2];
6022
18.7k
  zend_ast *stmt_ast = ast->child[3];
6023
6024
18.7k
  znode result;
6025
18.7k
  uint32_t opnum_start, opnum_jmp, opnum_loop;
6026
6027
18.7k
  zend_compile_for_expr_list(&result, init_ast);
6028
18.7k
  zend_do_free(&result);
6029
6030
18.7k
  opnum_jmp = zend_emit_jump(0);
6031
6032
18.7k
  zend_begin_loop(ZEND_NOP, NULL, 0);
6033
6034
18.7k
  opnum_start = get_next_op_number();
6035
18.7k
  zend_compile_stmt(stmt_ast);
6036
6037
18.7k
  opnum_loop = get_next_op_number();
6038
18.7k
  zend_compile_for_expr_list(&result, loop_ast);
6039
18.7k
  zend_do_free(&result);
6040
6041
18.7k
  zend_update_jump_target_to_next(opnum_jmp);
6042
18.7k
  zend_compile_for_expr_list(&result, cond_ast);
6043
18.7k
  zend_do_extended_stmt();
6044
6045
18.7k
  zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
6046
6047
18.7k
  zend_end_loop(opnum_loop, NULL);
6048
18.7k
}
6049
/* }}} */
6050
6051
static void zend_compile_foreach(zend_ast *ast) /* {{{ */
6052
54.9k
{
6053
54.9k
  zend_ast *expr_ast = ast->child[0];
6054
54.9k
  zend_ast *value_ast = ast->child[1];
6055
54.9k
  zend_ast *key_ast = ast->child[2];
6056
54.9k
  zend_ast *stmt_ast = ast->child[3];
6057
54.9k
  bool by_ref = value_ast->kind == ZEND_AST_REF;
6058
54.9k
  bool is_variable = zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast);
6059
6060
54.9k
  znode expr_node, reset_node, value_node, key_node;
6061
54.9k
  zend_op *opline;
6062
54.9k
  uint32_t opnum_reset, opnum_fetch;
6063
6064
54.9k
  if (key_ast) {
6065
1.28k
    if (key_ast->kind == ZEND_AST_REF) {
6066
11
      zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
6067
11
    }
6068
1.27k
    if (key_ast->kind == ZEND_AST_ARRAY) {
6069
14
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
6070
14
    }
6071
1.27k
  }
6072
6073
54.8k
  if (by_ref) {
6074
1.44k
    value_ast = value_ast->child[0];
6075
1.44k
  }
6076
6077
54.8k
  if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
6078
132
    by_ref = 1;
6079
132
  }
6080
6081
54.8k
  if (by_ref && is_variable) {
6082
1.01k
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, 1);
6083
53.8k
  } else {
6084
53.8k
    zend_compile_expr(&expr_node, expr_ast);
6085
53.8k
  }
6086
6087
54.8k
  if (by_ref) {
6088
1.58k
    zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
6089
1.58k
  }
6090
6091
54.8k
  opnum_reset = get_next_op_number();
6092
54.8k
  opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
6093
6094
54.8k
  zend_begin_loop(ZEND_FE_FREE, &reset_node, 0);
6095
6096
54.8k
  opnum_fetch = get_next_op_number();
6097
54.8k
  opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
6098
6099
54.8k
  if (is_this_fetch(value_ast)) {
6100
9
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6101
54.8k
  } else if (value_ast->kind == ZEND_AST_VAR &&
6102
54.8k
    zend_try_compile_cv(&value_node, value_ast) == SUCCESS) {
6103
54.1k
    SET_NODE(opline->op2, &value_node);
6104
54.1k
  } else {
6105
720
    opline->op2_type = IS_VAR;
6106
720
    opline->op2.var = get_temporary_variable();
6107
720
    GET_NODE(&value_node, opline->op2);
6108
720
    if (value_ast->kind == ZEND_AST_ARRAY) {
6109
394
      zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr);
6110
394
    } else if (by_ref) {
6111
177
      zend_emit_assign_ref_znode(value_ast, &value_node);
6112
177
    } else {
6113
149
      zend_emit_assign_znode(value_ast, &value_node);
6114
149
    }
6115
720
  }
6116
6117
54.8k
  if (key_ast) {
6118
1.25k
    opline = &CG(active_op_array)->opcodes[opnum_fetch];
6119
1.25k
    zend_make_tmp_result(&key_node, opline);
6120
1.25k
    zend_emit_assign_znode(key_ast, &key_node);
6121
1.25k
  }
6122
6123
54.8k
  zend_compile_stmt(stmt_ast);
6124
6125
  /* Place JMP and FE_FREE on the line where foreach starts. It would be
6126
   * better to use the end line, but this information is not available
6127
   * currently. */
6128
54.8k
  CG(zend_lineno) = ast->lineno;
6129
54.8k
  zend_emit_jump(opnum_fetch);
6130
6131
54.8k
  opline = &CG(active_op_array)->opcodes[opnum_reset];
6132
54.8k
  opline->op2.opline_num = get_next_op_number();
6133
6134
54.8k
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
6135
54.8k
  opline->extended_value = get_next_op_number();
6136
6137
54.8k
  zend_end_loop(opnum_fetch, &reset_node);
6138
6139
54.8k
  opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
6140
54.8k
}
6141
/* }}} */
6142
6143
static void zend_compile_if(zend_ast *ast) /* {{{ */
6144
20.6k
{
6145
20.6k
  zend_ast_list *list = zend_ast_get_list(ast);
6146
20.6k
  uint32_t i;
6147
20.6k
  uint32_t *jmp_opnums = NULL;
6148
6149
20.6k
  if (list->children > 1) {
6150
2.98k
    jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
6151
2.98k
  }
6152
6153
45.7k
  for (i = 0; i < list->children; ++i) {
6154
25.0k
    zend_ast *elem_ast = list->child[i];
6155
25.0k
    zend_ast *cond_ast = elem_ast->child[0];
6156
25.0k
    zend_ast *stmt_ast = elem_ast->child[1];
6157
6158
25.0k
    if (cond_ast) {
6159
22.3k
      znode cond_node;
6160
22.3k
      uint32_t opnum_jmpz;
6161
6162
22.3k
      if (i > 0) {
6163
1.66k
        CG(zend_lineno) = cond_ast->lineno;
6164
1.66k
        zend_do_extended_stmt();
6165
1.66k
      }
6166
6167
22.3k
      zend_compile_expr(&cond_node, cond_ast);
6168
22.3k
      opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6169
6170
22.3k
      zend_compile_stmt(stmt_ast);
6171
6172
22.3k
      if (i != list->children - 1) {
6173
        /* Set the lineno of JMP to the position of the if keyword, as we don't want to
6174
         * report the last line in the if branch as covered if it hasn't actually executed. */
6175
4.33k
        CG(zend_lineno) = elem_ast->lineno;
6176
4.33k
        jmp_opnums[i] = zend_emit_jump(0);
6177
4.33k
      }
6178
22.3k
      zend_update_jump_target_to_next(opnum_jmpz);
6179
22.3k
    } else {
6180
      /* "else" can only occur as last element. */
6181
2.67k
      ZEND_ASSERT(i == list->children - 1);
6182
2.67k
      zend_compile_stmt(stmt_ast);
6183
2.67k
    }
6184
25.0k
  }
6185
6186
20.6k
  if (list->children > 1) {
6187
7.28k
    for (i = 0; i < list->children - 1; ++i) {
6188
4.31k
      zend_update_jump_target_to_next(jmp_opnums[i]);
6189
4.31k
    }
6190
2.97k
    efree(jmp_opnums);
6191
2.97k
  }
6192
20.6k
}
6193
/* }}} */
6194
6195
5.83k
static uint8_t determine_switch_jumptable_type(zend_ast_list *cases) {
6196
5.83k
  uint32_t i;
6197
5.83k
  uint8_t common_type = IS_UNDEF;
6198
15.5k
  for (i = 0; i < cases->children; i++) {
6199
14.2k
    zend_ast *case_ast = cases->child[i];
6200
14.2k
    zend_ast **cond_ast = &case_ast->child[0];
6201
14.2k
    zval *cond_zv;
6202
14.2k
    if (!case_ast->child[0]) {
6203
      /* Skip default clause */
6204
263
      continue;
6205
263
    }
6206
6207
13.9k
    zend_eval_const_expr(cond_ast);
6208
13.9k
    if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6209
      /* Non-constant case */
6210
505
      return IS_UNDEF;
6211
505
    }
6212
6213
13.4k
    cond_zv = zend_ast_get_zval(case_ast->child[0]);
6214
13.4k
    if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6215
      /* We only optimize switched on integers and strings */
6216
3.85k
      return IS_UNDEF;
6217
3.85k
    }
6218
6219
9.61k
    if (common_type == IS_UNDEF) {
6220
5.30k
      common_type = Z_TYPE_P(cond_zv);
6221
5.30k
    } else if (common_type != Z_TYPE_P(cond_zv)) {
6222
      /* Non-uniform case types */
6223
35
      return IS_UNDEF;
6224
35
    }
6225
6226
9.57k
    if (Z_TYPE_P(cond_zv) == IS_STRING
6227
9.57k
        && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) {
6228
      /* Numeric strings cannot be compared with a simple hash lookup */
6229
88
      return IS_UNDEF;
6230
88
    }
6231
9.57k
  }
6232
6233
1.35k
  return common_type;
6234
5.83k
}
6235
6236
1.11k
static bool should_use_jumptable(zend_ast_list *cases, uint8_t jumptable_type) {
6237
1.11k
  if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) {
6238
0
    return 0;
6239
0
  }
6240
6241
  /* Thresholds are chosen based on when the average switch time for equidistributed
6242
   * input becomes smaller when using the jumptable optimization. */
6243
1.11k
  if (jumptable_type == IS_LONG) {
6244
244
    return cases->children >= 5;
6245
867
  } else {
6246
867
    ZEND_ASSERT(jumptable_type == IS_STRING);
6247
867
    return cases->children >= 2;
6248
867
  }
6249
1.11k
}
6250
6251
static void zend_compile_switch(zend_ast *ast) /* {{{ */
6252
5.83k
{
6253
5.83k
  zend_ast *expr_ast = ast->child[0];
6254
5.83k
  zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
6255
6256
5.83k
  uint32_t i;
6257
5.83k
  bool has_default_case = 0;
6258
6259
5.83k
  znode expr_node, case_node;
6260
5.83k
  zend_op *opline;
6261
5.83k
  uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1;
6262
5.83k
  uint8_t jumptable_type;
6263
5.83k
  HashTable *jumptable = NULL;
6264
6265
5.83k
  zend_compile_expr(&expr_node, expr_ast);
6266
6267
5.83k
  zend_begin_loop(ZEND_FREE, &expr_node, 1);
6268
6269
5.83k
  case_node.op_type = IS_TMP_VAR;
6270
5.83k
  case_node.u.op.var = get_temporary_variable();
6271
6272
5.83k
  jumptable_type = determine_switch_jumptable_type(cases);
6273
5.83k
  if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) {
6274
764
    znode jumptable_op;
6275
6276
764
    ALLOC_HASHTABLE(jumptable);
6277
764
    zend_hash_init(jumptable, cases->children, NULL, NULL, 0);
6278
764
    jumptable_op.op_type = IS_CONST;
6279
764
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6280
6281
764
    opline = zend_emit_op(NULL,
6282
764
      jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING,
6283
764
      &expr_node, &jumptable_op);
6284
764
    if (opline->op1_type == IS_CONST) {
6285
354
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6286
354
    }
6287
764
    opnum_switch = opline - CG(active_op_array)->opcodes;
6288
764
  }
6289
6290
5.83k
  jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
6291
23.1k
  for (i = 0; i < cases->children; ++i) {
6292
17.3k
    zend_ast *case_ast = cases->child[i];
6293
17.3k
    zend_ast *cond_ast = case_ast->child[0];
6294
17.3k
    znode cond_node;
6295
6296
17.3k
    if (!cond_ast) {
6297
261
      if (has_default_case) {
6298
13
        CG(zend_lineno) = case_ast->lineno;
6299
13
        zend_error_noreturn(E_COMPILE_ERROR,
6300
13
          "Switch statements may only contain one default clause");
6301
13
      }
6302
248
      has_default_case = 1;
6303
248
      continue;
6304
261
    }
6305
6306
17.0k
    zend_compile_expr(&cond_node, cond_ast);
6307
6308
17.0k
    if (expr_node.op_type == IS_CONST
6309
17.0k
      && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
6310
2.44k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6311
14.6k
    } else if (expr_node.op_type == IS_CONST
6312
14.6k
      && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
6313
1.19k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
6314
13.4k
    } else {
6315
13.4k
      opline = zend_emit_op(NULL,
6316
13.4k
        (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL,
6317
13.4k
        &expr_node, &cond_node);
6318
13.4k
      SET_NODE(opline->result, &case_node);
6319
13.4k
      if (opline->op1_type == IS_CONST) {
6320
9.48k
        Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6321
9.48k
      }
6322
6323
13.4k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6324
13.4k
    }
6325
17.0k
  }
6326
6327
5.82k
  opnum_default_jmp = zend_emit_jump(0);
6328
6329
21.2k
  for (i = 0; i < cases->children; ++i) {
6330
15.4k
    zend_ast *case_ast = cases->child[i];
6331
15.4k
    zend_ast *cond_ast = case_ast->child[0];
6332
15.4k
    zend_ast *stmt_ast = case_ast->child[1];
6333
6334
15.4k
    if (cond_ast) {
6335
15.2k
      zend_update_jump_target_to_next(jmpnz_opnums[i]);
6336
6337
15.2k
      if (jumptable) {
6338
1.88k
        zval *cond_zv = zend_ast_get_zval(cond_ast);
6339
1.88k
        zval jmp_target;
6340
1.88k
        ZVAL_LONG(&jmp_target, get_next_op_number());
6341
6342
1.88k
        ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type);
6343
1.88k
        if (Z_TYPE_P(cond_zv) == IS_LONG) {
6344
326
          zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6345
1.56k
        } else {
6346
1.56k
          ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6347
1.56k
          zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6348
1.56k
        }
6349
1.88k
      }
6350
15.2k
    } else {
6351
235
      zend_update_jump_target_to_next(opnum_default_jmp);
6352
6353
235
      if (jumptable) {
6354
42
        ZEND_ASSERT(opnum_switch != (uint32_t)-1);
6355
42
        opline = &CG(active_op_array)->opcodes[opnum_switch];
6356
42
        opline->extended_value = get_next_op_number();
6357
42
      }
6358
235
    }
6359
6360
15.4k
    zend_compile_stmt(stmt_ast);
6361
15.4k
  }
6362
6363
5.82k
  if (!has_default_case) {
6364
5.56k
    zend_update_jump_target_to_next(opnum_default_jmp);
6365
6366
5.56k
    if (jumptable) {
6367
706
      opline = &CG(active_op_array)->opcodes[opnum_switch];
6368
706
      opline->extended_value = get_next_op_number();
6369
706
    }
6370
5.56k
  }
6371
6372
5.82k
  zend_end_loop(get_next_op_number(), &expr_node);
6373
6374
5.82k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
6375
1.61k
    opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6376
1.61k
    opline->extended_value = ZEND_FREE_SWITCH;
6377
4.21k
  } else if (expr_node.op_type == IS_CONST) {
6378
3.85k
    zval_ptr_dtor_nogc(&expr_node.u.constant);
6379
3.85k
  }
6380
6381
5.82k
  efree(jmpnz_opnums);
6382
5.82k
}
6383
/* }}} */
6384
6385
static uint32_t count_match_conds(zend_ast_list *arms)
6386
2.35k
{
6387
2.35k
  uint32_t num_conds = 0;
6388
6389
6.77k
  for (uint32_t i = 0; i < arms->children; i++) {
6390
4.42k
    zend_ast *arm_ast = arms->child[i];
6391
4.42k
    if (arm_ast->child[0] == NULL) {
6392
738
      continue;
6393
738
    }
6394
6395
3.68k
    zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6396
3.68k
    num_conds += conds->children;
6397
3.68k
  }
6398
6399
2.35k
  return num_conds;
6400
2.35k
}
6401
6402
2.35k
static bool can_match_use_jumptable(zend_ast_list *arms) {
6403
5.25k
  for (uint32_t i = 0; i < arms->children; i++) {
6404
3.91k
    zend_ast *arm_ast = arms->child[i];
6405
3.91k
    if (!arm_ast->child[0]) {
6406
      /* Skip default arm */
6407
675
      continue;
6408
675
    }
6409
6410
3.23k
    zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6411
9.38k
    for (uint32_t j = 0; j < conds->children; j++) {
6412
7.16k
      zend_ast **cond_ast = &conds->child[j];
6413
6414
7.16k
      zend_eval_const_expr(cond_ast);
6415
7.16k
      if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6416
833
        return 0;
6417
833
      }
6418
6419
6.33k
      zval *cond_zv = zend_ast_get_zval(*cond_ast);
6420
6.33k
      if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6421
180
        return 0;
6422
180
      }
6423
6.33k
    }
6424
3.23k
  }
6425
6426
1.34k
  return 1;
6427
2.35k
}
6428
6429
static bool zend_is_pipe_optimizable_callable_name(zend_ast *ast)
6430
175
{
6431
175
  if (ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(ast)) == IS_STRING) {
6432
    /* Assert compilation adds a message operand, but this is incompatible with the
6433
     * pipe optimization that uses a temporary znode for the reference elimination.
6434
     * Therefore, disable the optimization for assert.
6435
     * Note that "assert" as a name is always treated as fully qualified. */
6436
147
    zend_string *str = zend_ast_get_str(ast);
6437
147
    return !zend_string_equals_literal_ci(str, "assert");
6438
147
  }
6439
6440
28
  return true;
6441
175
}
6442
6443
static void zend_compile_pipe(znode *result, zend_ast *ast)
6444
9.20k
{
6445
9.20k
  zend_ast *operand_ast = ast->child[0];
6446
9.20k
  zend_ast *callable_ast = ast->child[1];
6447
6448
  /* Compile the left hand side down to a value first. */
6449
9.20k
  znode operand_result;
6450
9.20k
  zend_compile_expr(&operand_result, operand_ast);
6451
6452
  /* Wrap simple values in a ZEND_QM_ASSIGN opcode to ensure references
6453
   * always fail. They will already fail in complex cases like arrays,
6454
   * so those don't need a wrapper. */
6455
9.20k
  znode wrapped_operand_result;
6456
9.20k
  if (operand_result.op_type & (IS_CV|IS_VAR)) {
6457
5.53k
    zend_emit_op_tmp(&wrapped_operand_result, ZEND_QM_ASSIGN, &operand_result, NULL);
6458
5.53k
  } else {
6459
3.67k
    wrapped_operand_result = operand_result;
6460
3.67k
  }
6461
6462
  /* Turn the operand into a function parameter list. */
6463
9.20k
  zend_ast *arg_list_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&wrapped_operand_result));
6464
6465
9.20k
  zend_ast *fcall_ast;
6466
9.20k
  znode callable_result;
6467
6468
  /* Turn $foo |> bar(...) into bar($foo). */
6469
9.20k
  if (callable_ast->kind == ZEND_AST_CALL
6470
9.20k
    && callable_ast->child[1]->kind == ZEND_AST_CALLABLE_CONVERT
6471
9.20k
    && zend_is_pipe_optimizable_callable_name(callable_ast->child[0])) {
6472
152
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6473
152
        callable_ast->child[0], arg_list_ast);
6474
  /* Turn $foo |> bar::baz(...) into bar::baz($foo). */
6475
9.05k
  } else if (callable_ast->kind == ZEND_AST_STATIC_CALL
6476
9.05k
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6477
14
    fcall_ast = zend_ast_create(ZEND_AST_STATIC_CALL,
6478
14
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6479
  /* Turn $foo |> $bar->baz(...) into $bar->baz($foo). */
6480
9.04k
  } else if (callable_ast->kind == ZEND_AST_METHOD_CALL
6481
9.04k
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6482
364
    fcall_ast = zend_ast_create(ZEND_AST_METHOD_CALL,
6483
364
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6484
  /* Turn $foo |> $expr into ($expr)($foo) */
6485
8.67k
  } else {
6486
8.67k
    zend_compile_expr(&callable_result, callable_ast);
6487
8.67k
    callable_ast = zend_ast_create_znode(&callable_result);
6488
8.67k
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6489
8.67k
      callable_ast, arg_list_ast);
6490
8.67k
  }
6491
6492
9.20k
  zend_compile_expr(result, fcall_ast);
6493
9.20k
}
6494
6495
static void zend_compile_match(znode *result, zend_ast *ast)
6496
2.35k
{
6497
2.35k
  zend_ast *expr_ast = ast->child[0];
6498
2.35k
  zend_ast_list *arms = zend_ast_get_list(ast->child[1]);
6499
2.35k
  bool has_default_arm = 0;
6500
2.35k
  uint32_t opnum_match = (uint32_t)-1;
6501
6502
2.35k
  znode expr_node;
6503
2.35k
  zend_compile_expr(&expr_node, expr_ast);
6504
6505
2.35k
  znode case_node;
6506
2.35k
  case_node.op_type = IS_TMP_VAR;
6507
2.35k
  case_node.u.op.var = get_temporary_variable();
6508
6509
2.35k
  uint32_t num_conds = count_match_conds(arms);
6510
2.35k
  uint8_t can_use_jumptable = can_match_use_jumptable(arms);
6511
2.35k
  bool uses_jumptable = can_use_jumptable && num_conds >= 2;
6512
2.35k
  HashTable *jumptable = NULL;
6513
2.35k
  uint32_t *jmpnz_opnums = NULL;
6514
6515
6.76k
  for (uint32_t i = 0; i < arms->children; ++i) {
6516
4.41k
    zend_ast *arm_ast = arms->child[i];
6517
6518
4.41k
    if (!arm_ast->child[0]) {
6519
735
      if (has_default_arm) {
6520
7
        CG(zend_lineno) = arm_ast->lineno;
6521
7
        zend_error_noreturn(E_COMPILE_ERROR,
6522
7
          "Match expressions may only contain one default arm");
6523
7
      }
6524
728
      has_default_arm = 1;
6525
728
    }
6526
4.41k
  }
6527
6528
2.34k
  if (uses_jumptable) {
6529
905
    znode jumptable_op;
6530
6531
905
    ALLOC_HASHTABLE(jumptable);
6532
905
    zend_hash_init(jumptable, num_conds, NULL, NULL, 0);
6533
905
    jumptable_op.op_type = IS_CONST;
6534
905
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6535
6536
905
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH, &expr_node, &jumptable_op);
6537
905
    if (opline->op1_type == IS_CONST) {
6538
87
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6539
87
    }
6540
905
    opnum_match = opline - CG(active_op_array)->opcodes;
6541
1.44k
  } else {
6542
1.44k
    jmpnz_opnums = safe_emalloc(sizeof(uint32_t), num_conds, 0);
6543
1.44k
    uint32_t cond_count = 0;
6544
3.74k
    for (uint32_t i = 0; i < arms->children; ++i) {
6545
2.30k
      zend_ast *arm_ast = arms->child[i];
6546
6547
2.30k
      if (!arm_ast->child[0]) {
6548
379
        continue;
6549
379
      }
6550
6551
1.92k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6552
5.08k
      for (uint32_t j = 0; j < conds->children; j++) {
6553
3.15k
        zend_ast *cond_ast = conds->child[j];
6554
6555
3.15k
        znode cond_node;
6556
3.15k
        zend_compile_expr(&cond_node, cond_ast);
6557
6558
3.15k
        uint32_t opcode = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE_STRICT : ZEND_IS_IDENTICAL;
6559
3.15k
        zend_op *opline = zend_emit_op(NULL, opcode, &expr_node, &cond_node);
6560
3.15k
        SET_NODE(opline->result, &case_node);
6561
3.15k
        if (opline->op1_type == IS_CONST) {
6562
1.09k
          Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6563
1.09k
        }
6564
6565
3.15k
        jmpnz_opnums[cond_count] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6566
6567
3.15k
        cond_count++;
6568
3.15k
      }
6569
1.92k
    }
6570
1.44k
  }
6571
6572
2.34k
  uint32_t opnum_default_jmp = 0;
6573
2.34k
  if (!uses_jumptable) {
6574
1.44k
    opnum_default_jmp = zend_emit_jump(0);
6575
1.44k
  }
6576
6577
2.34k
  bool is_first_case = 1;
6578
2.34k
  uint32_t cond_count = 0;
6579
2.34k
  uint32_t *jmp_end_opnums = safe_emalloc(sizeof(uint32_t), arms->children, 0);
6580
6581
  // The generated default arm is emitted first to avoid live range issues where the tmpvar
6582
  // for the arm result is freed even though it has not been initialized yet.
6583
2.34k
  if (!has_default_arm) {
6584
1.62k
    if (!uses_jumptable) {
6585
1.06k
      zend_update_jump_target_to_next(opnum_default_jmp);
6586
1.06k
    }
6587
6588
1.62k
    if (jumptable) {
6589
563
      zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6590
563
      opline->extended_value = get_next_op_number();
6591
563
    }
6592
6593
1.62k
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH_ERROR, &expr_node, NULL);
6594
1.62k
    if (opline->op1_type == IS_CONST) {
6595
275
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6596
275
    }
6597
1.62k
    if (arms->children == 0) {
6598
      /* Mark this as an "expression throw" for opcache. */
6599
81
      opline->extended_value = ZEND_THROW_IS_EXPR;
6600
81
    }
6601
1.62k
  }
6602
6603
6.73k
  for (uint32_t i = 0; i < arms->children; ++i) {
6604
4.39k
    zend_ast *arm_ast = arms->child[i];
6605
4.39k
    zend_ast *body_ast = arm_ast->child[1];
6606
6607
4.39k
    if (arm_ast->child[0] != NULL) {
6608
3.67k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6609
6610
12.0k
      for (uint32_t j = 0; j < conds->children; j++) {
6611
8.37k
        zend_ast *cond_ast = conds->child[j];
6612
6613
8.37k
        if (jmpnz_opnums != NULL) {
6614
3.15k
          zend_update_jump_target_to_next(jmpnz_opnums[cond_count]);
6615
3.15k
        }
6616
6617
8.37k
        if (jumptable) {
6618
5.21k
          zval *cond_zv = zend_ast_get_zval(cond_ast);
6619
5.21k
          zval jmp_target;
6620
5.21k
          ZVAL_LONG(&jmp_target, get_next_op_number());
6621
6622
5.21k
          if (Z_TYPE_P(cond_zv) == IS_LONG) {
6623
4.62k
            zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6624
4.62k
          } else {
6625
589
            ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6626
589
            zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6627
589
          }
6628
5.21k
        }
6629
6630
8.37k
        cond_count++;
6631
8.37k
      }
6632
3.67k
    } else {
6633
721
      if (!uses_jumptable) {
6634
379
        zend_update_jump_target_to_next(opnum_default_jmp);
6635
379
      }
6636
6637
721
      if (jumptable) {
6638
342
        ZEND_ASSERT(opnum_match != (uint32_t)-1);
6639
342
        zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6640
342
        opline->extended_value = get_next_op_number();
6641
342
      }
6642
721
    }
6643
6644
4.39k
    znode body_node;
6645
4.39k
    zend_compile_expr(&body_node, body_ast);
6646
6647
4.39k
    if (is_first_case) {
6648
2.26k
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL);
6649
2.26k
      is_first_case = 0;
6650
2.26k
    } else {
6651
2.12k
      zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL);
6652
2.12k
      SET_NODE(opline_qm_assign->result, result);
6653
2.12k
    }
6654
6655
4.39k
    jmp_end_opnums[i] = zend_emit_jump(0);
6656
4.39k
  }
6657
6658
  // Initialize result in case there is no arm
6659
2.34k
  if (arms->children == 0) {
6660
81
    result->op_type = IS_CONST;
6661
81
    ZVAL_NULL(&result->u.constant);
6662
81
  }
6663
6664
6.73k
  for (uint32_t i = 0; i < arms->children; ++i) {
6665
4.39k
    zend_update_jump_target_to_next(jmp_end_opnums[i]);
6666
4.39k
  }
6667
6668
2.34k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
6669
1.35k
    zend_op *opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6670
1.35k
    opline->extended_value = ZEND_FREE_SWITCH;
6671
1.35k
  } else if (expr_node.op_type == IS_CONST) {
6672
423
    zval_ptr_dtor_nogc(&expr_node.u.constant);
6673
423
  }
6674
6675
2.34k
  if (jmpnz_opnums != NULL) {
6676
1.44k
    efree(jmpnz_opnums);
6677
1.44k
  }
6678
2.34k
  efree(jmp_end_opnums);
6679
2.34k
}
6680
6681
static void zend_compile_try(zend_ast *ast) /* {{{ */
6682
54.6k
{
6683
54.6k
  zend_ast *try_ast = ast->child[0];
6684
54.6k
  zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
6685
54.6k
  zend_ast *finally_ast = ast->child[2];
6686
6687
54.6k
  uint32_t i, j;
6688
54.6k
  zend_op *opline;
6689
54.6k
  uint32_t try_catch_offset;
6690
54.6k
  uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
6691
54.6k
  uint32_t orig_fast_call_var = CG(context).fast_call_var;
6692
54.6k
  uint32_t orig_try_catch_offset = CG(context).try_catch_offset;
6693
6694
54.6k
  if (catches->children == 0 && !finally_ast) {
6695
70
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
6696
70
  }
6697
6698
  /* label: try { } must not be equal to try { label: } */
6699
54.6k
  if (CG(context).labels) {
6700
315
    zend_label *label;
6701
315
    ZEND_HASH_MAP_REVERSE_FOREACH_PTR(CG(context).labels, label) {
6702
315
      if (label->opline_num == get_next_op_number()) {
6703
98
        zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
6704
98
      }
6705
315
      break;
6706
945
    } ZEND_HASH_FOREACH_END();
6707
315
  }
6708
6709
54.6k
  try_catch_offset = zend_add_try_element(get_next_op_number());
6710
6711
54.6k
  if (finally_ast) {
6712
11.1k
    zend_loop_var fast_call;
6713
11.1k
    if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
6714
2.24k
      CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
6715
2.24k
    }
6716
11.1k
    CG(context).fast_call_var = get_temporary_variable();
6717
6718
    /* Push FAST_CALL on unwind stack */
6719
11.1k
    fast_call.opcode = ZEND_FAST_CALL;
6720
11.1k
    fast_call.var_type = IS_TMP_VAR;
6721
11.1k
    fast_call.var_num = CG(context).fast_call_var;
6722
11.1k
    fast_call.try_catch_offset = try_catch_offset;
6723
11.1k
    zend_stack_push(&CG(loop_var_stack), &fast_call);
6724
11.1k
  }
6725
6726
54.6k
  CG(context).try_catch_offset = try_catch_offset;
6727
6728
54.6k
  zend_compile_stmt(try_ast);
6729
6730
54.6k
  if (catches->children != 0) {
6731
43.5k
    jmp_opnums[0] = zend_emit_jump(0);
6732
43.5k
  }
6733
6734
103k
  for (i = 0; i < catches->children; ++i) {
6735
48.9k
    zend_ast *catch_ast = catches->child[i];
6736
48.9k
    zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]);
6737
48.9k
    zend_ast *var_ast = catch_ast->child[1];
6738
48.9k
    zend_ast *stmt_ast = catch_ast->child[2];
6739
48.9k
    zend_string *var_name = var_ast ? zval_make_interned_string(zend_ast_get_zval(var_ast)) : NULL;
6740
48.9k
    bool is_last_catch = (i + 1 == catches->children);
6741
6742
48.9k
    uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0);
6743
48.9k
    uint32_t opnum_catch = (uint32_t)-1;
6744
6745
48.9k
    CG(zend_lineno) = catch_ast->lineno;
6746
6747
102k
    for (j = 0; j < classes->children; j++) {
6748
53.5k
      zend_ast *class_ast = classes->child[j];
6749
53.5k
      bool is_last_class = (j + 1 == classes->children);
6750
6751
53.5k
      if (!zend_is_const_default_class_ref(class_ast)) {
6752
6
        zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
6753
6
      }
6754
6755
53.5k
      opnum_catch = get_next_op_number();
6756
53.5k
      if (i == 0 && j == 0) {
6757
43.5k
        CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
6758
43.5k
      }
6759
6760
53.5k
      opline = get_next_op();
6761
53.5k
      opline->opcode = ZEND_CATCH;
6762
53.5k
      opline->op1_type = IS_CONST;
6763
53.5k
      opline->op1.constant = zend_add_class_name_literal(
6764
53.5k
          zend_resolve_class_name_ast(class_ast));
6765
53.5k
      opline->extended_value = zend_alloc_cache_slot();
6766
6767
53.5k
      if (var_name && zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
6768
6
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6769
6
      }
6770
6771
53.5k
      opline->result_type = var_name ? IS_CV : IS_UNUSED;
6772
53.5k
      opline->result.var = var_name ? lookup_cv(var_name) : -1;
6773
6774
53.5k
      if (is_last_catch && is_last_class) {
6775
43.5k
        opline->extended_value |= ZEND_LAST_CATCH;
6776
43.5k
      }
6777
6778
53.5k
      if (!is_last_class) {
6779
4.61k
        jmp_multicatch[j] = zend_emit_jump(0);
6780
4.61k
        opline = &CG(active_op_array)->opcodes[opnum_catch];
6781
4.61k
        opline->op2.opline_num = get_next_op_number();
6782
4.61k
      }
6783
53.5k
    }
6784
6785
53.5k
    for (j = 0; j < classes->children - 1; j++) {
6786
4.61k
      zend_update_jump_target_to_next(jmp_multicatch[j]);
6787
4.61k
    }
6788
6789
48.9k
    efree(jmp_multicatch);
6790
6791
48.9k
    zend_compile_stmt(stmt_ast);
6792
6793
48.9k
    if (!is_last_catch) {
6794
5.37k
      jmp_opnums[i + 1] = zend_emit_jump(0);
6795
5.37k
    }
6796
6797
48.9k
    ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class");
6798
48.9k
    opline = &CG(active_op_array)->opcodes[opnum_catch];
6799
48.9k
    if (!is_last_catch) {
6800
5.37k
      opline->op2.opline_num = get_next_op_number();
6801
5.37k
    }
6802
48.9k
  }
6803
6804
103k
  for (i = 0; i < catches->children; ++i) {
6805
48.9k
    zend_update_jump_target_to_next(jmp_opnums[i]);
6806
48.9k
  }
6807
6808
54.5k
  if (finally_ast) {
6809
11.1k
    zend_loop_var discard_exception;
6810
11.1k
    uint32_t opnum_jmp = get_next_op_number() + 1;
6811
6812
    /* Pop FAST_CALL from unwind stack */
6813
11.1k
    zend_stack_del_top(&CG(loop_var_stack));
6814
6815
    /* Push DISCARD_EXCEPTION on unwind stack */
6816
11.1k
    discard_exception.opcode = ZEND_DISCARD_EXCEPTION;
6817
11.1k
    discard_exception.var_type = IS_TMP_VAR;
6818
11.1k
    discard_exception.var_num = CG(context).fast_call_var;
6819
11.1k
    zend_stack_push(&CG(loop_var_stack), &discard_exception);
6820
6821
11.1k
    CG(zend_lineno) = finally_ast->lineno;
6822
6823
11.1k
    opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
6824
11.1k
    opline->op1.num = try_catch_offset;
6825
11.1k
    opline->result_type = IS_TMP_VAR;
6826
11.1k
    opline->result.var = CG(context).fast_call_var;
6827
6828
11.1k
    zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
6829
6830
11.1k
    zend_compile_stmt(finally_ast);
6831
6832
11.1k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
6833
11.1k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
6834
11.1k
      = get_next_op_number();
6835
6836
11.1k
    opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
6837
11.1k
    opline->op1_type = IS_TMP_VAR;
6838
11.1k
    opline->op1.var = CG(context).fast_call_var;
6839
11.1k
    opline->op2.num = orig_try_catch_offset;
6840
6841
11.1k
    zend_update_jump_target_to_next(opnum_jmp);
6842
6843
11.1k
    CG(context).fast_call_var = orig_fast_call_var;
6844
6845
    /* Pop DISCARD_EXCEPTION from unwind stack */
6846
11.1k
    zend_stack_del_top(&CG(loop_var_stack));
6847
11.1k
  }
6848
6849
54.5k
  CG(context).try_catch_offset = orig_try_catch_offset;
6850
6851
54.5k
  efree(jmp_opnums);
6852
54.5k
}
6853
/* }}} */
6854
6855
/* Encoding declarations must already be handled during parsing */
6856
bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
6857
2.22k
{
6858
2.22k
  zend_ast_list *declares = zend_ast_get_list(ast);
6859
2.22k
  uint32_t i;
6860
5.68k
  for (i = 0; i < declares->children; ++i) {
6861
3.47k
    zend_ast *declare_ast = declares->child[i];
6862
3.47k
    zend_ast *name_ast = declare_ast->child[0];
6863
3.47k
    zend_ast *value_ast = declare_ast->child[1];
6864
3.47k
    zend_string *name = zend_ast_get_str(name_ast);
6865
6866
3.47k
    if (zend_string_equals_literal_ci(name, "encoding")) {
6867
124
      if (value_ast->kind != ZEND_AST_ZVAL) {
6868
18
        zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0);
6869
18
        return 0;
6870
18
      }
6871
6872
106
      if (CG(multibyte)) {
6873
0
        zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
6874
6875
0
        const zend_encoding *new_encoding, *old_encoding;
6876
0
        zend_encoding_filter old_input_filter;
6877
6878
0
        CG(encoding_declared) = 1;
6879
6880
0
        new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
6881
0
        if (!new_encoding) {
6882
0
          zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
6883
0
        } else {
6884
0
          old_input_filter = LANG_SCNG(input_filter);
6885
0
          old_encoding = LANG_SCNG(script_encoding);
6886
0
          zend_multibyte_set_filter(new_encoding);
6887
6888
          /* need to re-scan if input filter changed */
6889
0
          if (old_input_filter != LANG_SCNG(input_filter) ||
6890
0
             (old_input_filter && new_encoding != old_encoding)) {
6891
0
            zend_multibyte_yyinput_again(old_input_filter, old_encoding);
6892
0
          }
6893
0
        }
6894
6895
0
        zend_string_release_ex(encoding_name, 0);
6896
106
      } else {
6897
106
        zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
6898
106
          "Zend multibyte feature is turned off by settings");
6899
106
      }
6900
106
    }
6901
3.47k
  }
6902
6903
2.20k
  return 1;
6904
2.22k
}
6905
/* }}} */
6906
6907
/* Check whether this is the first statement, not counting declares. */
6908
static zend_result zend_is_first_statement(zend_ast *ast, bool allow_nop) /* {{{ */
6909
3.19k
{
6910
3.19k
  uint32_t i = 0;
6911
3.19k
  zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
6912
6913
4.77k
  while (i < file_ast->children) {
6914
4.76k
    if (file_ast->child[i] == ast) {
6915
3.15k
      return SUCCESS;
6916
3.15k
    } else if (file_ast->child[i] == NULL) {
6917
219
      if (!allow_nop) {
6918
1
        return FAILURE;
6919
1
      }
6920
1.39k
    } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
6921
45
      return FAILURE;
6922
45
    }
6923
1.57k
    i++;
6924
1.57k
  }
6925
2
  return FAILURE;
6926
3.19k
}
6927
/* }}} */
6928
6929
static void zend_compile_declare(zend_ast *ast) /* {{{ */
6930
1.91k
{
6931
1.91k
  zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
6932
1.91k
  zend_ast *stmt_ast = ast->child[1];
6933
1.91k
  zend_declarables orig_declarables = FC(declarables);
6934
1.91k
  uint32_t i;
6935
6936
4.91k
  for (i = 0; i < declares->children; ++i) {
6937
3.11k
    zend_ast *declare_ast = declares->child[i];
6938
3.11k
    zend_ast *name_ast = declare_ast->child[0];
6939
3.11k
    zend_ast **value_ast_ptr = &declare_ast->child[1];
6940
3.11k
    zend_string *name = zend_ast_get_str(name_ast);
6941
6942
3.11k
    if ((*value_ast_ptr)->kind != ZEND_AST_ZVAL) {
6943
24
      zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
6944
24
    }
6945
6946
3.09k
    if (zend_string_equals_literal_ci(name, "ticks")) {
6947
673
      zval value_zv;
6948
673
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
6949
673
      FC(declarables).ticks = zval_get_long(&value_zv);
6950
673
      zval_ptr_dtor_nogc(&value_zv);
6951
2.41k
    } else if (zend_string_equals_literal_ci(name, "encoding")) {
6952
6953
59
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ 0)) {
6954
9
        zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
6955
9
          "the very first statement in the script");
6956
9
      }
6957
2.36k
    } else if (zend_string_equals_literal_ci(name, "strict_types")) {
6958
356
      zval value_zv;
6959
6960
356
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ 0)) {
6961
11
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
6962
11
          "the very first statement in the script");
6963
11
      }
6964
6965
345
      if (ast->child[1] != NULL) {
6966
6
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
6967
6
          "use block mode");
6968
6
      }
6969
6970
339
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
6971
6972
339
      if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
6973
68
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
6974
68
      }
6975
6976
271
      if (Z_LVAL(value_zv) == 1) {
6977
193
        CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
6978
193
      }
6979
6980
2.00k
    } else {
6981
2.00k
      zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
6982
2.00k
    }
6983
3.09k
  }
6984
6985
1.80k
  if (stmt_ast) {
6986
567
    zend_compile_stmt(stmt_ast);
6987
6988
567
    FC(declarables) = orig_declarables;
6989
567
  }
6990
1.80k
}
6991
/* }}} */
6992
6993
static void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
6994
431k
{
6995
431k
  zend_ast_list *list = zend_ast_get_list(ast);
6996
431k
  uint32_t i;
6997
1.25M
  for (i = 0; i < list->children; ++i) {
6998
822k
    zend_compile_stmt(list->child[i]);
6999
822k
  }
7000
431k
}
7001
/* }}} */
7002
7003
ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
7004
145k
{
7005
145k
  uint32_t i, n;
7006
7007
145k
  func->common.arg_flags[0] = 0;
7008
145k
  func->common.arg_flags[1] = 0;
7009
145k
  func->common.arg_flags[2] = 0;
7010
145k
  if (func->common.arg_info) {
7011
145k
    n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
7012
145k
    i = 0;
7013
318k
    while (i < n) {
7014
172k
      ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i]));
7015
172k
      i++;
7016
172k
    }
7017
145k
    if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_VARIADIC) && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) {
7018
346
      uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]);
7019
4.39k
      while (i < MAX_ARG_FLAG_NUM) {
7020
4.05k
        ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
7021
4.05k
        i++;
7022
4.05k
      }
7023
346
    }
7024
145k
  }
7025
145k
}
7026
/* }}} */
7027
7028
static zend_type zend_compile_single_typename(zend_ast *ast)
7029
134k
{
7030
134k
  ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE));
7031
134k
  if (ast->kind == ZEND_AST_TYPE) {
7032
5.37k
    if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) {
7033
6
      zend_error_noreturn(E_COMPILE_ERROR,
7034
6
        "Cannot use \"static\" when no class scope is active");
7035
6
    }
7036
7037
5.37k
    return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0);
7038
128k
  } else {
7039
128k
    zend_string *type_name = zend_ast_get_str(ast);
7040
128k
    uint8_t type_code = zend_lookup_builtin_type_by_name(type_name);
7041
7042
128k
    if (type_code != 0) {
7043
37.1k
      if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) {
7044
6
        zend_error_noreturn(E_COMPILE_ERROR,
7045
6
          "Type declaration '%s' must be unqualified",
7046
6
          ZSTR_VAL(zend_string_tolower(type_name)));
7047
6
      }
7048
7049
      /* Transform iterable into a type union alias */
7050
37.1k
      if (type_code == IS_ITERABLE) {
7051
        /* Set iterable bit for BC compat during Reflection and string representation of type */
7052
11.5k
        zend_type iterable = (zend_type) ZEND_TYPE_INIT_CLASS_MASK(ZSTR_KNOWN(ZEND_STR_TRAVERSABLE),
7053
11.5k
                  (MAY_BE_ARRAY|_ZEND_TYPE_ITERABLE_BIT));
7054
11.5k
        return iterable;
7055
11.5k
      }
7056
7057
25.5k
      return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0);
7058
91.5k
    } else {
7059
91.5k
      const char *correct_name;
7060
91.5k
      uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
7061
91.5k
      zend_string *class_name = type_name;
7062
7063
91.5k
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
7064
90.5k
        class_name = zend_resolve_class_name_ast(ast);
7065
90.5k
        zend_assert_valid_class_name(class_name, "a type name");
7066
90.5k
      } else {
7067
974
        ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT);
7068
7069
974
        zend_ensure_valid_class_fetch_type(fetch_type);
7070
974
        if (fetch_type == ZEND_FETCH_CLASS_SELF) {
7071
          /* Scope might be unknown for unbound closures and traits */
7072
653
          if (zend_is_scope_known()) {
7073
299
            class_name = CG(active_class_entry)->name;
7074
299
            ZEND_ASSERT(class_name && "must know class name when resolving self type at compile time");
7075
299
          }
7076
653
        } else {
7077
321
          ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_PARENT);
7078
          /* Scope might be unknown for unbound closures and traits */
7079
300
          if (zend_is_scope_known()) {
7080
153
            class_name = CG(active_class_entry)->parent_name;
7081
153
            ZEND_ASSERT(class_name && "must know class name when resolving parent type at compile time");
7082
153
          }
7083
300
        }
7084
953
        zend_string_addref(class_name);
7085
953
      }
7086
7087
91.4k
      if (ast->attr == ZEND_NAME_NOT_FQ
7088
91.4k
          && zend_is_confusable_type(type_name, &correct_name)
7089
91.4k
          && zend_is_not_imported(type_name)) {
7090
884
        const char *extra =
7091
884
          FC(current_namespace) ? " or import the class with \"use\"" : "";
7092
884
        if (correct_name) {
7093
810
          zend_error(E_COMPILE_WARNING,
7094
810
            "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? "
7095
810
            "Write \"\\%s\"%s to suppress this warning",
7096
810
            ZSTR_VAL(type_name), correct_name, ZSTR_VAL(class_name), extra);
7097
810
        } else {
7098
74
          zend_error(E_COMPILE_WARNING,
7099
74
            "\"%s\" is not a supported builtin type "
7100
74
            "and will be interpreted as a class name. "
7101
74
            "Write \"\\%s\"%s to suppress this warning",
7102
74
            ZSTR_VAL(type_name), ZSTR_VAL(class_name), extra);
7103
74
        }
7104
884
      }
7105
7106
91.4k
      class_name = zend_new_interned_string(class_name);
7107
91.4k
      zend_alloc_ce_cache(class_name);
7108
91.4k
      return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, /* allow null */ false, 0);
7109
91.5k
    }
7110
128k
  }
7111
134k
}
7112
7113
static void zend_are_intersection_types_redundant(const zend_type left_type, const zend_type right_type)
7114
9.53k
{
7115
9.53k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(left_type));
7116
9.53k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(right_type));
7117
9.53k
  zend_type_list *l_type_list = ZEND_TYPE_LIST(left_type);
7118
9.53k
  zend_type_list *r_type_list = ZEND_TYPE_LIST(right_type);
7119
9.53k
  zend_type_list *smaller_type_list, *larger_type_list;
7120
9.53k
  bool flipped = false;
7121
7122
9.53k
  if (r_type_list->num_types < l_type_list->num_types) {
7123
2.72k
    smaller_type_list = r_type_list;
7124
2.72k
    larger_type_list = l_type_list;
7125
2.72k
    flipped = true;
7126
6.80k
  } else {
7127
6.80k
    smaller_type_list = l_type_list;
7128
6.80k
    larger_type_list = r_type_list;
7129
6.80k
  }
7130
7131
9.53k
  unsigned int sum = 0;
7132
9.53k
  const zend_type *outer_type;
7133
35.9k
  ZEND_TYPE_LIST_FOREACH(smaller_type_list, outer_type)
7134
35.9k
    const zend_type *inner_type;
7135
119k
    ZEND_TYPE_LIST_FOREACH(larger_type_list, inner_type)
7136
119k
      if (zend_string_equals_ci(ZEND_TYPE_NAME(*inner_type), ZEND_TYPE_NAME(*outer_type))) {
7137
8.25k
        sum++;
7138
8.25k
        break;
7139
8.25k
      }
7140
119k
    ZEND_TYPE_LIST_FOREACH_END();
7141
35.9k
  ZEND_TYPE_LIST_FOREACH_END();
7142
7143
9.53k
  if (sum == smaller_type_list->num_types) {
7144
26
    zend_string *smaller_type_str;
7145
26
    zend_string *larger_type_str;
7146
26
    if (flipped) {
7147
7
      smaller_type_str = zend_type_to_string(right_type);
7148
7
      larger_type_str = zend_type_to_string(left_type);
7149
19
    } else {
7150
19
      smaller_type_str = zend_type_to_string(left_type);
7151
19
      larger_type_str = zend_type_to_string(right_type);
7152
19
    }
7153
26
    if (smaller_type_list->num_types == larger_type_list->num_types) {
7154
16
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant with type %s",
7155
16
        ZSTR_VAL(smaller_type_str), ZSTR_VAL(larger_type_str));
7156
16
    } else {
7157
10
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7158
10
        ZSTR_VAL(larger_type_str), ZSTR_VAL(smaller_type_str));
7159
10
    }
7160
26
  }
7161
9.53k
}
7162
7163
static void zend_is_intersection_type_redundant_by_single_type(const zend_type intersection_type, const zend_type single_type)
7164
10.9k
{
7165
10.9k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(intersection_type));
7166
10.9k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(single_type));
7167
7168
10.9k
  const zend_type *single_intersection_type = NULL;
7169
41.7k
  ZEND_TYPE_FOREACH(intersection_type, single_intersection_type)
7170
41.7k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(*single_intersection_type), ZEND_TYPE_NAME(single_type))) {
7171
7
      zend_string *single_type_str = zend_type_to_string(single_type);
7172
7
      zend_string *complete_type = zend_type_to_string(intersection_type);
7173
7
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7174
7
          ZSTR_VAL(complete_type), ZSTR_VAL(single_type_str));
7175
7
    }
7176
41.7k
  ZEND_TYPE_FOREACH_END();
7177
10.9k
}
7178
7179
/* Used by both intersection and union types prior to transforming the type list to a full zend_type */
7180
static void zend_is_type_list_redundant_by_single_type(const zend_type_list *type_list, const zend_type type)
7181
35.1k
{
7182
35.1k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(type));
7183
73.2k
  for (size_t i = 0; i < type_list->num_types - 1; i++) {
7184
38.2k
    if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7185
7.95k
      zend_is_intersection_type_redundant_by_single_type(type_list->types[i], type);
7186
7.95k
      continue;
7187
7.95k
    }
7188
30.2k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(type_list->types[i]), ZEND_TYPE_NAME(type))) {
7189
51
      zend_string *single_type_str = zend_type_to_string(type);
7190
51
      zend_error_noreturn(E_COMPILE_ERROR, "Duplicate type %s is redundant", ZSTR_VAL(single_type_str));
7191
51
    }
7192
30.2k
  }
7193
35.1k
}
7194
7195
static zend_type zend_compile_typename(zend_ast *ast);
7196
7197
static zend_type zend_compile_typename_ex(
7198
    zend_ast *ast, bool force_allow_null, bool *forced_allow_null) /* {{{ */
7199
109k
{
7200
109k
  bool is_marked_nullable = ast->attr & ZEND_TYPE_NULLABLE;
7201
109k
  zend_ast_attr orig_ast_attr = ast->attr;
7202
109k
  zend_type type = ZEND_TYPE_INIT_NONE(0);
7203
7204
109k
  if (is_marked_nullable) {
7205
3.04k
    ast->attr &= ~ZEND_TYPE_NULLABLE;
7206
3.04k
  }
7207
7208
109k
  if (ast->kind == ZEND_AST_TYPE_UNION) {
7209
13.8k
    zend_ast_list *list = zend_ast_get_list(ast);
7210
13.8k
    zend_type_list *type_list;
7211
13.8k
    bool is_composite = false;
7212
13.8k
    bool has_only_iterable_class = true;
7213
13.8k
    ALLOCA_FLAG(use_heap)
7214
7215
13.8k
    type_list = do_alloca(ZEND_TYPE_LIST_SIZE(list->children), use_heap);
7216
13.8k
    type_list->num_types = 0;
7217
7218
46.2k
    for (uint32_t i = 0; i < list->children; i++) {
7219
32.3k
      zend_ast *type_ast = list->child[i];
7220
32.3k
      zend_type single_type;
7221
32.3k
      uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7222
7223
32.3k
      if (type_ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7224
7.39k
        has_only_iterable_class = false;
7225
7.39k
        is_composite = true;
7226
        /* The first class type can be stored directly as the type ptr payload. */
7227
7.39k
        if (ZEND_TYPE_IS_COMPLEX(type) && !ZEND_TYPE_HAS_LIST(type)) {
7228
          /* Switch from single name to name list. */
7229
408
          type_list->num_types = 1;
7230
408
          type_list->types[0] = type;
7231
          /* Clear MAY_BE_* type flags */
7232
408
          ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7233
408
        }
7234
        /* Mark type as list type */
7235
7.39k
        ZEND_TYPE_SET_LIST(type, type_list);
7236
7237
7.39k
        single_type = zend_compile_typename(type_ast);
7238
7.39k
        ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(single_type));
7239
7240
7.39k
        type_list->types[type_list->num_types++] = single_type;
7241
7242
        /* Check for trivially redundant class types */
7243
19.9k
        for (size_t i = 0; i < type_list->num_types - 1; i++) {
7244
12.5k
          if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7245
9.53k
            zend_are_intersection_types_redundant(single_type, type_list->types[i]);
7246
9.53k
            continue;
7247
9.53k
          }
7248
          /* Type from type list is a simple type */
7249
3.01k
          zend_is_intersection_type_redundant_by_single_type(single_type, type_list->types[i]);
7250
3.01k
        }
7251
7.39k
        continue;
7252
7.39k
      }
7253
7254
24.9k
      single_type = zend_compile_single_typename(type_ast);
7255
24.9k
      uint32_t single_type_mask = ZEND_TYPE_PURE_MASK(single_type);
7256
7257
24.9k
      if (single_type_mask == MAY_BE_ANY) {
7258
6
        zend_error_noreturn(E_COMPILE_ERROR, "Type mixed can only be used as a standalone type");
7259
6
      }
7260
24.9k
      if (ZEND_TYPE_IS_COMPLEX(single_type) && !ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7261
18.8k
        has_only_iterable_class = false;
7262
18.8k
      }
7263
7264
24.9k
      uint32_t type_mask_overlap = ZEND_TYPE_PURE_MASK(type) & single_type_mask;
7265
24.9k
      if (type_mask_overlap) {
7266
36
        zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap);
7267
36
        zend_string *overlap_type_str = zend_type_to_string(overlap_type);
7268
36
        zend_error_noreturn(E_COMPILE_ERROR,
7269
36
          "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str));
7270
36
      }
7271
7272
24.9k
      if ( ((type_mask & MAY_BE_TRUE) && (single_type_mask == MAY_BE_FALSE))
7273
24.9k
          || ((type_mask & MAY_BE_FALSE) && (single_type_mask == MAY_BE_TRUE)) ) {
7274
12
        zend_error_noreturn(E_COMPILE_ERROR,
7275
12
          "Type contains both true and false, bool must be used instead");
7276
12
      }
7277
24.9k
      ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type);
7278
      /* Clear MAY_BE_* type flags */
7279
24.9k
      ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK;
7280
7281
24.9k
      if (ZEND_TYPE_IS_COMPLEX(single_type)) {
7282
19.3k
        if (!ZEND_TYPE_IS_COMPLEX(type) && !is_composite) {
7283
          /* The first class type can be stored directly as the type ptr payload. */
7284
7.33k
          ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type));
7285
7.33k
          ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT;
7286
11.9k
        } else {
7287
11.9k
          if (type_list->num_types == 0) {
7288
            /* Switch from single name to name list. */
7289
5.75k
            type_list->num_types = 1;
7290
5.75k
            type_list->types[0] = type;
7291
            /* Clear MAY_BE_* type flags */
7292
5.75k
            ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7293
5.75k
            ZEND_TYPE_SET_LIST(type, type_list);
7294
5.75k
          }
7295
7296
11.9k
          type_list->types[type_list->num_types++] = single_type;
7297
7298
          /* Check for trivially redundant class types */
7299
11.9k
          zend_is_type_list_redundant_by_single_type(type_list, single_type);
7300
11.9k
        }
7301
19.3k
      }
7302
24.9k
    }
7303
7304
13.8k
    if (type_list->num_types) {
7305
11.0k
      zend_type_list *list = zend_arena_alloc(
7306
11.0k
        &CG(arena), ZEND_TYPE_LIST_SIZE(type_list->num_types));
7307
11.0k
      memcpy(list, type_list, ZEND_TYPE_LIST_SIZE(type_list->num_types));
7308
11.0k
      ZEND_TYPE_SET_LIST(type, list);
7309
11.0k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7310
      /* Inform that the type list is a union type */
7311
11.0k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7312
11.0k
    }
7313
7314
13.8k
    free_alloca(type_list, use_heap);
7315
7316
13.8k
    uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7317
13.8k
    if ((type_mask & MAY_BE_OBJECT) &&
7318
13.8k
        ((!has_only_iterable_class && ZEND_TYPE_IS_COMPLEX(type)) || (type_mask & MAY_BE_STATIC))) {
7319
38
      zend_string *type_str = zend_type_to_string(type);
7320
38
      zend_error_noreturn(E_COMPILE_ERROR,
7321
38
        "Type %s contains both object and a class type, which is redundant",
7322
38
        ZSTR_VAL(type_str));
7323
38
    }
7324
95.9k
  } else if (ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7325
10.0k
    zend_ast_list *list = zend_ast_get_list(ast);
7326
10.0k
    zend_type_list *type_list;
7327
7328
    /* Allocate the type list directly on the arena as it must be a type
7329
     * list of the same number of elements as the AST list has children */
7330
10.0k
    type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children));
7331
10.0k
    type_list->num_types = 0;
7332
7333
10.0k
    ZEND_ASSERT(list->children > 1);
7334
7335
33.2k
    for (uint32_t i = 0; i < list->children; i++) {
7336
23.2k
      zend_ast *type_ast = list->child[i];
7337
23.2k
      zend_type single_type = zend_compile_single_typename(type_ast);
7338
7339
      /* An intersection of union types cannot exist so invalidate it
7340
       * Currently only can happen with iterable getting canonicalized to Traversable|array */
7341
23.2k
      if (ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7342
6
        zend_string *standard_type_str = zend_type_to_string(single_type);
7343
6
        zend_error_noreturn(E_COMPILE_ERROR,
7344
6
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7345
0
        zend_string_release_ex(standard_type_str, false);
7346
0
      }
7347
      /* An intersection of standard types cannot exist so invalidate it */
7348
23.2k
      if (ZEND_TYPE_IS_ONLY_MASK(single_type)) {
7349
73
        zend_string *standard_type_str = zend_type_to_string(single_type);
7350
73
        zend_error_noreturn(E_COMPILE_ERROR,
7351
73
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7352
0
        zend_string_release_ex(standard_type_str, false);
7353
0
      }
7354
      /* Check for "self" and "parent" too */
7355
23.1k
      if (
7356
23.1k
        zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_SELF))
7357
23.1k
        || zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_PARENT))
7358
23.1k
      ) {
7359
12
        zend_error_noreturn(E_COMPILE_ERROR,
7360
12
          "Type %s cannot be part of an intersection type", ZSTR_VAL(ZEND_TYPE_NAME(single_type)));
7361
12
      }
7362
7363
      /* Add type to the type list */
7364
23.1k
      type_list->types[type_list->num_types++] = single_type;
7365
7366
      /* Check for trivially redundant class types */
7367
23.1k
      zend_is_type_list_redundant_by_single_type(type_list, single_type);
7368
23.1k
    }
7369
7370
9.98k
    ZEND_ASSERT(list->children == type_list->num_types);
7371
7372
    /* An implicitly nullable intersection type needs to be converted to a DNF type */
7373
9.97k
    if (force_allow_null) {
7374
67
      zend_type intersection_type = ZEND_TYPE_INIT_NONE(0);
7375
67
      ZEND_TYPE_SET_LIST(intersection_type, type_list);
7376
67
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_INTERSECTION_BIT;
7377
67
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_ARENA_BIT;
7378
7379
67
      zend_type_list *dnf_type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(1));
7380
67
      dnf_type_list->num_types = 1;
7381
67
      dnf_type_list->types[0] = intersection_type;
7382
67
      ZEND_TYPE_SET_LIST(type, dnf_type_list);
7383
      /* Inform that the type list is a DNF type */
7384
67
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7385
67
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7386
9.90k
    } else {
7387
9.90k
      ZEND_TYPE_SET_LIST(type, type_list);
7388
      /* Inform that the type list is an intersection type */
7389
9.90k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_INTERSECTION_BIT;
7390
9.90k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7391
9.90k
    }
7392
85.8k
  } else {
7393
85.8k
    type = zend_compile_single_typename(ast);
7394
85.8k
  }
7395
7396
109k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
7397
7398
109k
  if (type_mask == MAY_BE_ANY && is_marked_nullable) {
7399
19
    zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be marked as nullable since mixed already includes null");
7400
19
  }
7401
7402
109k
  if ((type_mask & MAY_BE_NULL) && is_marked_nullable) {
7403
6
    zend_error_noreturn(E_COMPILE_ERROR, "null cannot be marked as nullable");
7404
6
  }
7405
7406
109k
  if (force_allow_null && !is_marked_nullable && !(type_mask & MAY_BE_NULL)) {
7407
198
    *forced_allow_null = true;
7408
198
  }
7409
7410
109k
  if (is_marked_nullable || force_allow_null) {
7411
3.34k
    ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
7412
3.34k
    type_mask = ZEND_TYPE_PURE_MASK(type);
7413
3.34k
  }
7414
7415
109k
  if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_VOID)) {
7416
12
    zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type");
7417
12
  }
7418
7419
109k
  if ((type_mask & MAY_BE_NEVER) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_NEVER)) {
7420
7
    zend_error_noreturn(E_COMPILE_ERROR, "never can only be used as a standalone type");
7421
7
  }
7422
7423
109k
  ast->attr = orig_ast_attr;
7424
109k
  return type;
7425
109k
}
7426
/* }}} */
7427
7428
static zend_type zend_compile_typename(zend_ast *ast)
7429
44.2k
{
7430
44.2k
  bool forced_allow_null;
7431
44.2k
  return zend_compile_typename_ex(ast, false, &forced_allow_null);
7432
44.2k
}
7433
7434
/* May convert value from int to float. */
7435
static bool zend_is_valid_default_value(zend_type type, zval *value)
7436
3.55k
{
7437
3.55k
  ZEND_ASSERT(ZEND_TYPE_IS_SET(type));
7438
3.55k
  if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) {
7439
2.56k
    return 1;
7440
2.56k
  }
7441
993
  if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) {
7442
    /* Integers are allowed as initializers for floating-point values. */
7443
834
    convert_to_double(value);
7444
834
    return 1;
7445
834
  }
7446
159
  return 0;
7447
993
}
7448
7449
static void zend_compile_attributes(
7450
  HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target, uint32_t promoted
7451
105k
) /* {{{ */ {
7452
105k
  zend_attribute *attr;
7453
105k
  zend_internal_attribute *config;
7454
7455
105k
  zend_ast_list *list = zend_ast_get_list(ast);
7456
105k
  uint32_t g, i, j;
7457
7458
105k
  ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE_LIST);
7459
7460
212k
  for (g = 0; g < list->children; g++) {
7461
107k
    zend_ast_list *group = zend_ast_get_list(list->child[g]);
7462
7463
107k
    ZEND_ASSERT(group->kind == ZEND_AST_ATTRIBUTE_GROUP);
7464
7465
549k
    for (i = 0; i < group->children; i++) {
7466
442k
      ZEND_ASSERT(group->child[i]->kind == ZEND_AST_ATTRIBUTE);
7467
7468
442k
      zend_ast *el = group->child[i];
7469
7470
442k
      if (el->child[1] &&
7471
442k
          el->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) {
7472
12
          zend_error_noreturn(E_COMPILE_ERROR,
7473
12
              "Cannot create Closure as attribute argument");
7474
12
      }
7475
7476
442k
      zend_string *name = zend_resolve_class_name_ast(el->child[0]);
7477
442k
      zend_string *lcname = zend_string_tolower_ex(name, false);
7478
442k
      zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL;
7479
7480
442k
      config = zend_internal_attribute_get(lcname);
7481
442k
      zend_string_release(lcname);
7482
7483
      /* Exclude internal attributes that do not match on promoted properties. */
7484
442k
      if (config && !(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7485
244
        if (promoted & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL)) {
7486
0
          zend_string_release(name);
7487
0
          continue;
7488
0
        }
7489
244
      }
7490
7491
442k
      uint32_t flags = (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES)
7492
442k
        ? ZEND_ATTRIBUTE_STRICT_TYPES : 0;
7493
442k
      attr = zend_add_attribute(
7494
442k
        attributes, name, args ? args->children : 0, flags, offset, el->lineno);
7495
442k
      zend_string_release(name);
7496
7497
      /* Populate arguments */
7498
442k
      if (args) {
7499
25.8k
        ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST);
7500
7501
25.8k
        bool uses_named_args = 0;
7502
73.8k
        for (j = 0; j < args->children; j++) {
7503
47.9k
          zend_ast **arg_ast_ptr = &args->child[j];
7504
47.9k
          zend_ast *arg_ast = *arg_ast_ptr;
7505
7506
47.9k
          if (arg_ast->kind == ZEND_AST_UNPACK) {
7507
6
            zend_error_noreturn(E_COMPILE_ERROR,
7508
6
              "Cannot use unpacking in attribute argument list");
7509
6
          }
7510
7511
47.9k
          if (arg_ast->kind == ZEND_AST_NAMED_ARG) {
7512
1.87k
            attr->args[j].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0]));
7513
1.87k
            arg_ast_ptr = &arg_ast->child[1];
7514
1.87k
            uses_named_args = 1;
7515
7516
8.53k
            for (uint32_t k = 0; k < j; k++) {
7517
6.66k
              if (attr->args[k].name &&
7518
6.66k
                  zend_string_equals(attr->args[k].name, attr->args[j].name)) {
7519
7
                zend_error_noreturn(E_COMPILE_ERROR, "Duplicate named parameter $%s",
7520
7
                  ZSTR_VAL(attr->args[j].name));
7521
7
              }
7522
6.66k
            }
7523
46.0k
          } else if (uses_named_args) {
7524
46
            zend_error_noreturn(E_COMPILE_ERROR,
7525
46
              "Cannot use positional argument after named argument");
7526
46
          }
7527
7528
47.9k
          zend_const_expr_to_zval(
7529
47.9k
            &attr->args[j].value, arg_ast_ptr, /* allow_dynamic */ true);
7530
47.9k
        }
7531
25.8k
      }
7532
442k
    }
7533
107k
  }
7534
7535
105k
  if (*attributes != NULL) {
7536
    /* Validate attributes in a secondary loop (needed to detect repeated attributes). */
7537
1.74M
    ZEND_HASH_PACKED_FOREACH_PTR(*attributes, attr) {
7538
1.74M
      if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) {
7539
764k
        continue;
7540
764k
      }
7541
7542
1.99k
      if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7543
40
        zend_string *location = zend_get_attribute_target_names(target);
7544
40
        zend_string *allowed = zend_get_attribute_target_names(config->flags);
7545
7546
40
        zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
7547
40
          ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
7548
40
        );
7549
40
      }
7550
7551
1.95k
      if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
7552
1.95k
        if (zend_is_attribute_repeated(*attributes, attr)) {
7553
20
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name));
7554
20
        }
7555
1.95k
      }
7556
7557
1.93k
      if (config->validator != NULL) {
7558
531
        config->validator(attr, target, CG(active_class_entry));
7559
531
      }
7560
1.93k
    } ZEND_HASH_FOREACH_END();
7561
105k
  }
7562
105k
}
7563
/* }}} */
7564
7565
static void zend_compile_property_hooks(
7566
    zend_property_info *prop_info, zend_string *prop_name,
7567
    zend_ast *prop_type_ast, zend_ast_list *hooks);
7568
7569
typedef struct {
7570
  zend_string *property_name;
7571
  bool uses_property;
7572
} find_property_usage_context;
7573
7574
static void zend_property_hook_find_property_usage(zend_ast **ast_ptr, void *_context) /* {{{ */
7575
30.0k
{
7576
30.0k
  zend_ast *ast = *ast_ptr;
7577
30.0k
  find_property_usage_context *context = (find_property_usage_context *) _context;
7578
7579
30.0k
  if (ast == NULL) {
7580
480
    return;
7581
29.5k
  } else if (ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_NULLSAFE_PROP) {
7582
2.81k
    zend_ast *object_ast = ast->child[0];
7583
2.81k
    zend_ast *property_ast = ast->child[1];
7584
7585
2.81k
    if (object_ast->kind == ZEND_AST_VAR
7586
2.81k
     && object_ast->child[0]->kind == ZEND_AST_ZVAL
7587
2.81k
     && property_ast->kind == ZEND_AST_ZVAL) {
7588
2.32k
      zval *object = zend_ast_get_zval(object_ast->child[0]);
7589
2.32k
      zval *property = zend_ast_get_zval(property_ast);
7590
2.32k
      if (Z_TYPE_P(object) == IS_STRING
7591
2.32k
        && Z_TYPE_P(property) == IS_STRING
7592
2.32k
        && zend_string_equals_literal(Z_STR_P(object), "this")
7593
2.32k
        && zend_string_equals(Z_STR_P(property), context->property_name)) {
7594
647
        context->uses_property = true;
7595
        /* No need to look for references in this branch. */
7596
647
        return;
7597
647
      }
7598
2.32k
    }
7599
2.81k
  }
7600
7601
  /* Don't search across function/class boundaries. */
7602
28.9k
  if (!zend_ast_is_special(ast)) {
7603
18.8k
    zend_ast_apply(ast, zend_property_hook_find_property_usage, context);
7604
18.8k
  }
7605
28.9k
}
7606
7607
static bool zend_property_hook_uses_property(zend_string *property_name, zend_string *hook_name, zend_ast *hook_ast)
7608
5.47k
{
7609
5.47k
  if (zend_string_equals_literal_ci(hook_name, "set")
7610
5.47k
   && hook_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
7611
447
    return true;
7612
447
  }
7613
7614
5.02k
  find_property_usage_context context = { property_name, false };
7615
5.02k
  zend_property_hook_find_property_usage(&hook_ast, &context);
7616
5.02k
  return context.uses_property;
7617
5.47k
}
7618
7619
static bool zend_property_is_virtual(zend_class_entry *ce, zend_string *property_name, zend_ast *hooks_ast, uint32_t flags)
7620
31.5k
{
7621
31.5k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
7622
287
    return true;
7623
287
  }
7624
31.2k
  if (!hooks_ast) {
7625
27.4k
    return false;
7626
27.4k
  }
7627
7628
3.74k
  bool is_virtual = true;
7629
7630
3.74k
  zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
7631
10.0k
  for (uint32_t i = 0; i < hooks->children; i++) {
7632
6.31k
    zend_ast_decl *hook = (zend_ast_decl *) hooks->child[i];
7633
6.31k
    zend_ast *body = hook->child[2];
7634
6.31k
    if (body && zend_property_hook_uses_property(property_name, hook->name, body)) {
7635
1.06k
      is_virtual = false;
7636
1.06k
    }
7637
6.31k
  }
7638
7639
3.74k
  return is_virtual;
7640
31.2k
}
7641
7642
static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */
7643
164k
{
7644
164k
  zend_ast_list *list = zend_ast_get_list(ast);
7645
164k
  uint32_t i;
7646
164k
  zend_op_array *op_array = CG(active_op_array);
7647
164k
  zend_arg_info *arg_infos;
7648
7649
164k
  if (return_type_ast || fallback_return_type) {
7650
    /* Use op_array->arg_info[-1] for return type */
7651
16.2k
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
7652
16.2k
    arg_infos->name = NULL;
7653
16.2k
    if (return_type_ast) {
7654
15.8k
      arg_infos->type = zend_compile_typename(return_type_ast);
7655
15.8k
      ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS(
7656
15.8k
        (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0, /* is_tentative */ 0);
7657
15.8k
    } else {
7658
422
      arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0);
7659
422
    }
7660
16.2k
    arg_infos++;
7661
16.2k
    op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
7662
7663
16.2k
    if (ZEND_TYPE_CONTAINS_CODE(arg_infos[-1].type, IS_VOID)
7664
16.2k
        && (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
7665
236
      zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7666
236
      zend_error(E_DEPRECATED, "%s(): Returning by reference from a void function is deprecated", ZSTR_VAL(func_name));
7667
236
      zend_string_release(func_name);
7668
236
    }
7669
148k
  } else {
7670
148k
    if (list->children == 0) {
7671
44.3k
      return;
7672
44.3k
    }
7673
103k
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
7674
103k
  }
7675
7676
  /* Find last required parameter number for deprecation message. */
7677
119k
  uint32_t last_required_param = (uint32_t) -1;
7678
267k
  for (i = 0; i < list->children; ++i) {
7679
147k
    zend_ast *param_ast = list->child[i];
7680
147k
    zend_ast *default_ast_ptr = param_ast->child[2];
7681
147k
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
7682
147k
    if (!default_ast_ptr && !is_variadic) {
7683
139k
      last_required_param = i;
7684
139k
    }
7685
147k
  }
7686
7687
266k
  for (i = 0; i < list->children; ++i) {
7688
147k
    zend_ast *param_ast = list->child[i];
7689
147k
    zend_ast *type_ast = param_ast->child[0];
7690
147k
    zend_ast *var_ast = param_ast->child[1];
7691
147k
    zend_ast **default_ast_ptr = &param_ast->child[2];
7692
147k
    zend_ast *attributes_ast = param_ast->child[3];
7693
147k
    zend_ast *doc_comment_ast = param_ast->child[4];
7694
147k
    zend_ast *hooks_ast = param_ast->child[5];
7695
147k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast));
7696
147k
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
7697
147k
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
7698
147k
    uint32_t property_flags = param_ast->attr & (ZEND_ACC_PPP_MASK | ZEND_ACC_PPP_SET_MASK | ZEND_ACC_READONLY | ZEND_ACC_FINAL);
7699
147k
    bool is_promoted = property_flags || hooks_ast;
7700
7701
147k
    CG(zend_lineno) = param_ast->lineno;
7702
7703
147k
    znode var_node, default_node;
7704
147k
    uint8_t opcode;
7705
147k
    zend_op *opline;
7706
147k
    zend_arg_info *arg_info;
7707
7708
147k
    if (zend_is_auto_global(name)) {
7709
3
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
7710
3
        ZSTR_VAL(name));
7711
3
    }
7712
7713
147k
    var_node.op_type = IS_CV;
7714
147k
    var_node.u.op.var = lookup_cv(name);
7715
7716
147k
    if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
7717
32
      zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
7718
32
        ZSTR_VAL(name));
7719
147k
    } else if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
7720
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
7721
6
    }
7722
7723
147k
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
7724
9
      zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
7725
9
    }
7726
7727
147k
    if (is_variadic) {
7728
726
      opcode = ZEND_RECV_VARIADIC;
7729
726
      default_node.op_type = IS_UNUSED;
7730
726
      op_array->fn_flags |= ZEND_ACC_VARIADIC;
7731
7732
726
      if (*default_ast_ptr) {
7733
6
        zend_error_noreturn(E_COMPILE_ERROR,
7734
6
          "Variadic parameter cannot have a default value");
7735
6
      }
7736
146k
    } else if (*default_ast_ptr) {
7737
      /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
7738
6.61k
      uint32_t cops = CG(compiler_options);
7739
6.61k
      CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
7740
6.61k
      opcode = ZEND_RECV_INIT;
7741
6.61k
      default_node.op_type = IS_CONST;
7742
6.61k
      zend_const_expr_to_zval(
7743
6.61k
        &default_node.u.constant, default_ast_ptr, /* allow_dynamic */ true);
7744
6.61k
      CG(compiler_options) = cops;
7745
139k
    } else {
7746
139k
      opcode = ZEND_RECV;
7747
139k
      default_node.op_type = IS_UNUSED;
7748
139k
      op_array->required_num_args = i + 1;
7749
139k
    }
7750
7751
147k
    arg_info = &arg_infos[i];
7752
147k
    arg_info->name = zend_string_copy(name);
7753
147k
    arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0);
7754
7755
147k
    if (attributes_ast) {
7756
23.7k
      zend_compile_attributes(
7757
23.7k
        &op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER,
7758
23.7k
        is_promoted ? ZEND_ATTRIBUTE_TARGET_PROPERTY : 0
7759
23.7k
      );
7760
23.7k
    }
7761
7762
147k
    bool forced_allow_nullable = false;
7763
147k
    if (type_ast) {
7764
65.5k
      uint32_t default_type = *default_ast_ptr ? Z_TYPE(default_node.u.constant) : IS_UNDEF;
7765
65.5k
      bool force_nullable = default_type == IS_NULL && !is_promoted;
7766
7767
65.5k
      op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
7768
65.5k
      arg_info->type = zend_compile_typename_ex(type_ast, force_nullable, &forced_allow_nullable);
7769
65.5k
      if (forced_allow_nullable) {
7770
198
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7771
198
        zend_error(E_DEPRECATED,
7772
198
           "%s(): Implicitly marking parameter $%s as nullable is deprecated, the explicit nullable type "
7773
198
           "must be used instead", ZSTR_VAL(func_name), ZSTR_VAL(name));
7774
198
        zend_string_release(func_name);
7775
198
      }
7776
7777
65.5k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) {
7778
6
        zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");
7779
6
      }
7780
7781
65.5k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_NEVER) {
7782
6
        zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type");
7783
6
      }
7784
7785
65.5k
      if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable
7786
65.5k
          && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) {
7787
45
        zend_string *type_str = zend_type_to_string(arg_info->type);
7788
45
        zend_error_noreturn(E_COMPILE_ERROR,
7789
45
          "Cannot use %s as default value for parameter $%s of type %s",
7790
45
          zend_get_type_by_const(default_type),
7791
45
          ZSTR_VAL(name), ZSTR_VAL(type_str));
7792
45
      }
7793
65.5k
    }
7794
147k
    if (last_required_param != (uint32_t) -1
7795
147k
     && i < last_required_param
7796
147k
     && default_node.op_type == IS_CONST) {
7797
      /* Ignore parameters of the form "Type $param = null".
7798
       * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
7799
765
      if (!forced_allow_nullable) {
7800
659
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7801
659
        zend_ast *required_param_ast = list->child[last_required_param];
7802
659
        zend_error(E_DEPRECATED,
7803
659
          "%s(): Optional parameter $%s declared before required parameter $%s "
7804
659
          "is implicitly treated as a required parameter",
7805
659
          ZSTR_VAL(func_name), ZSTR_VAL(name), ZSTR_VAL(zend_ast_get_str(required_param_ast->child[1])));
7806
659
        zend_string_release(func_name);
7807
659
      }
7808
7809
      /* Regardless of whether we issue a deprecation, convert this parameter into
7810
       * a required parameter without a default value. This ensures that it cannot be
7811
       * used as an optional parameter even with named parameters. */
7812
765
      opcode = ZEND_RECV;
7813
765
      default_node.op_type = IS_UNUSED;
7814
765
      zval_ptr_dtor(&default_node.u.constant);
7815
765
    }
7816
7817
147k
    opline = zend_emit_op(NULL, opcode, NULL, &default_node);
7818
147k
    SET_NODE(opline->result, &var_node);
7819
147k
    opline->op1.num = i + 1;
7820
7821
147k
    uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic, /* is_tentative */ 0)
7822
147k
      | (is_promoted ? _ZEND_IS_PROMOTED_BIT : 0);
7823
147k
    ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags;
7824
147k
    if (opcode == ZEND_RECV) {
7825
140k
      opline->op2.num = type_ast ?
7826
78.1k
        ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY;
7827
140k
    }
7828
7829
147k
    if (is_promoted) {
7830
939
      zend_op_array *op_array = CG(active_op_array);
7831
939
      zend_class_entry *scope = op_array->scope;
7832
7833
939
      bool is_ctor =
7834
939
        scope && zend_is_constructor(op_array->function_name);
7835
939
      if (!is_ctor) {
7836
51
        zend_error_noreturn(E_COMPILE_ERROR,
7837
51
          "Cannot declare promoted property outside a constructor");
7838
51
      }
7839
888
      if ((op_array->fn_flags & ZEND_ACC_ABSTRACT)
7840
888
          || (scope->ce_flags & ZEND_ACC_INTERFACE)) {
7841
6
        zend_error_noreturn(E_COMPILE_ERROR,
7842
6
          "Cannot declare promoted property in an abstract constructor");
7843
6
      }
7844
882
      if (is_variadic) {
7845
6
        zend_error_noreturn(E_COMPILE_ERROR,
7846
6
          "Cannot declare variadic promoted property");
7847
6
      }
7848
876
      if (zend_hash_exists(&scope->properties_info, name)) {
7849
6
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
7850
6
          ZSTR_VAL(scope->name), ZSTR_VAL(name));
7851
6
      }
7852
870
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_CALLABLE) {
7853
6
        zend_string *str = zend_type_to_string(arg_info->type);
7854
6
        zend_error_noreturn(E_COMPILE_ERROR,
7855
6
          "Property %s::$%s cannot have type %s",
7856
6
          ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str));
7857
6
      }
7858
7859
864
      if (!(property_flags & ZEND_ACC_READONLY) && (scope->ce_flags & ZEND_ACC_READONLY_CLASS)) {
7860
23
        property_flags |= ZEND_ACC_READONLY;
7861
23
      }
7862
7863
      /* Recompile the type, as it has different memory management requirements. */
7864
864
      zend_type type = ZEND_TYPE_INIT_NONE(0);
7865
864
      if (type_ast) {
7866
665
        type = zend_compile_typename(type_ast);
7867
665
      }
7868
7869
      /* Don't give the property an explicit default value. For typed properties this means
7870
       * uninitialized, for untyped properties it means an implicit null default value.
7871
       * Properties with hooks get an implicit default value of undefined until inheritance,
7872
       * where it is changed to null only once we know it is not virtual. If we were to set it
7873
       * here, we couldn't verify that a true virtual property must not have an explicit
7874
       * default value. */
7875
864
      zval default_value;
7876
864
      if (ZEND_TYPE_IS_SET(type) || hooks_ast) {
7877
694
        ZVAL_UNDEF(&default_value);
7878
694
      } else {
7879
170
        if (property_flags & ZEND_ACC_READONLY) {
7880
12
          zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
7881
12
            ZSTR_VAL(scope->name), ZSTR_VAL(name));
7882
12
        }
7883
7884
158
        ZVAL_NULL(&default_value);
7885
158
      }
7886
7887
852
      zend_string *doc_comment =
7888
852
        doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
7889
852
      zend_property_info *prop = zend_declare_typed_property(
7890
852
        scope, name, &default_value,
7891
852
        property_flags | (zend_property_is_virtual(scope, name, hooks_ast, property_flags) ? ZEND_ACC_VIRTUAL : 0) | ZEND_ACC_PROMOTED,
7892
852
        doc_comment, type);
7893
852
      if (hooks_ast) {
7894
82
        zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
7895
82
        zend_compile_property_hooks(prop, name, type_ast, hooks);
7896
82
      }
7897
852
      if (attributes_ast) {
7898
56
        zend_compile_attributes(
7899
56
          &prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, ZEND_ATTRIBUTE_TARGET_PARAMETER);
7900
56
      }
7901
852
    }
7902
147k
  }
7903
7904
  /* These are assigned at the end to avoid uninitialized memory in case of an error */
7905
119k
  op_array->num_args = list->children;
7906
119k
  op_array->arg_info = arg_infos;
7907
7908
  /* Don't count the variadic argument */
7909
119k
  if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
7910
705
    op_array->num_args--;
7911
705
  }
7912
119k
  zend_set_function_arg_flags((zend_function*)op_array);
7913
7914
266k
  for (i = 0; i < list->children; i++) {
7915
146k
    zend_ast *param_ast = list->child[i];
7916
146k
    zend_ast *hooks_ast = param_ast->child[5];
7917
146k
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
7918
146k
    uint32_t flags = param_ast->attr & (ZEND_ACC_PPP_MASK | ZEND_ACC_PPP_SET_MASK | ZEND_ACC_READONLY);
7919
146k
    bool is_promoted = flags || hooks_ast;
7920
146k
    if (!is_promoted) {
7921
145k
      continue;
7922
145k
    }
7923
7924
826
    CG(zend_lineno) = param_ast->lineno;
7925
7926
    /* Emit $this->prop = $prop for promoted properties. */
7927
826
    zend_string *name = zend_ast_get_str(param_ast->child[1]);
7928
826
    znode name_node, value_node;
7929
826
    name_node.op_type = IS_CONST;
7930
826
    ZVAL_STR_COPY(&name_node.u.constant, name);
7931
826
    value_node.op_type = IS_CV;
7932
826
    value_node.u.op.var = lookup_cv(name);
7933
7934
826
    zend_op *opline = zend_emit_op(NULL,
7935
826
      is_ref ? ZEND_ASSIGN_OBJ_REF : ZEND_ASSIGN_OBJ, NULL, &name_node);
7936
826
    opline->extended_value = zend_alloc_cache_slots(3);
7937
826
    zend_emit_op_data(&value_node);
7938
826
  }
7939
119k
}
7940
/* }}} */
7941
7942
static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */
7943
1.64k
{
7944
1.64k
  zend_ast_list *list = zend_ast_get_list(uses_ast);
7945
1.64k
  uint32_t i;
7946
7947
1.64k
  if (!list->children) {
7948
0
    return;
7949
0
  }
7950
7951
1.64k
  if (!op_array->static_variables) {
7952
1.64k
    op_array->static_variables = zend_new_array(8);
7953
1.64k
  }
7954
7955
5.87k
  for (i = 0; i < list->children; ++i) {
7956
4.25k
    zend_ast *var_name_ast = list->child[i];
7957
4.25k
    zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast));
7958
4.25k
    uint32_t mode = var_name_ast->attr;
7959
4.25k
    zend_op *opline;
7960
4.25k
    zval *value;
7961
7962
4.25k
    if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
7963
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
7964
6
    }
7965
7966
4.25k
    if (zend_is_auto_global(var_name)) {
7967
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable");
7968
6
    }
7969
7970
4.24k
    value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval));
7971
4.24k
    if (!value) {
7972
14
      zend_error_noreturn_unchecked(E_COMPILE_ERROR,
7973
14
        "Cannot use variable $%S twice", var_name);
7974
14
    }
7975
7976
4.23k
    CG(zend_lineno) = zend_ast_get_lineno(var_name_ast);
7977
7978
4.23k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
7979
4.23k
    opline->op2_type = IS_CV;
7980
4.23k
    opline->op2.var = lookup_cv(var_name);
7981
4.23k
    opline->extended_value =
7982
4.23k
      (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode;
7983
4.23k
  }
7984
1.64k
}
7985
/* }}} */
7986
7987
typedef struct {
7988
  HashTable uses;
7989
  bool varvars_used;
7990
} closure_info;
7991
7992
3.02M
static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
7993
3.02M
  if (!ast) {
7994
2.74k
    return;
7995
2.74k
  }
7996
7997
3.02M
  if (ast->kind == ZEND_AST_VAR) {
7998
347k
    zend_ast *name_ast = ast->child[0];
7999
347k
    if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) {
8000
346k
      zend_string *name = zend_ast_get_str(name_ast);
8001
346k
      if (zend_is_auto_global(name)) {
8002
        /* These is no need to explicitly import auto-globals. */
8003
1.91k
        return;
8004
1.91k
      }
8005
8006
344k
      if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8007
        /* $this does not need to be explicitly imported. */
8008
891
        return;
8009
891
      }
8010
8011
343k
      zend_hash_add_empty_element(&info->uses, name);
8012
343k
    } else {
8013
1.11k
      info->varvars_used = 1;
8014
1.11k
      find_implicit_binds_recursively(info, name_ast);
8015
1.11k
    }
8016
2.67M
  } else if (zend_ast_is_list(ast)) {
8017
115k
    zend_ast_list *list = zend_ast_get_list(ast);
8018
115k
    uint32_t i;
8019
716k
    for (i = 0; i < list->children; i++) {
8020
601k
      find_implicit_binds_recursively(info, list->child[i]);
8021
601k
    }
8022
2.55M
  } else if (ast->kind == ZEND_AST_CLOSURE) {
8023
    /* For normal closures add the use() list. */
8024
583
    zend_ast_decl *closure_ast = (zend_ast_decl *) ast;
8025
583
    zend_ast *uses_ast = closure_ast->child[1];
8026
583
    if (uses_ast) {
8027
313
      zend_ast_list *uses_list = zend_ast_get_list(uses_ast);
8028
313
      uint32_t i;
8029
2.28k
      for (i = 0; i < uses_list->children; i++) {
8030
1.97k
        zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i]));
8031
1.97k
      }
8032
313
    }
8033
2.55M
  } else if (ast->kind == ZEND_AST_ARROW_FUNC) {
8034
    /* For arrow functions recursively check the expression. */
8035
1.02M
    zend_ast_decl *closure_ast = (zend_ast_decl *) ast;
8036
1.02M
    find_implicit_binds_recursively(info, closure_ast->child[2]);
8037
1.53M
  } else if (!zend_ast_is_special(ast)) {
8038
1.16M
    uint32_t i, children = zend_ast_get_num_children(ast);
8039
2.53M
    for (i = 0; i < children; i++) {
8040
1.37M
      find_implicit_binds_recursively(info, ast->child[i]);
8041
1.37M
    }
8042
1.16M
  }
8043
3.02M
}
8044
8045
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast)
8046
19.6k
{
8047
19.6k
  zend_ast_list *param_list = zend_ast_get_list(params_ast);
8048
19.6k
  uint32_t i;
8049
8050
19.6k
  zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0);
8051
8052
19.6k
  find_implicit_binds_recursively(info, stmt_ast);
8053
8054
  /* Remove variables that are parameters */
8055
30.8k
  for (i = 0; i < param_list->children; i++) {
8056
11.1k
    zend_ast *param_ast = param_list->child[i];
8057
11.1k
    zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1]));
8058
11.1k
  }
8059
19.6k
}
8060
8061
static void compile_implicit_lexical_binds(
8062
    closure_info *info, znode *closure, zend_op_array *op_array)
8063
19.6k
{
8064
19.6k
  zend_string *var_name;
8065
19.6k
  zend_op *opline;
8066
8067
  /* TODO We might want to use a special binding mode if varvars_used is set. */
8068
19.6k
  if (zend_hash_num_elements(&info->uses) == 0) {
8069
7.83k
    return;
8070
7.83k
  }
8071
8072
11.8k
  if (!op_array->static_variables) {
8073
11.8k
    op_array->static_variables = zend_new_array(8);
8074
11.8k
  }
8075
8076
238k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8077
238k
    zval *value = zend_hash_add(
8078
238k
      op_array->static_variables, var_name, &EG(uninitialized_zval));
8079
238k
    uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData);
8080
8081
238k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8082
238k
    opline->op2_type = IS_CV;
8083
238k
    opline->op2.var = lookup_cv(var_name);
8084
238k
    opline->extended_value = offset | ZEND_BIND_IMPLICIT;
8085
238k
  ZEND_HASH_FOREACH_END();
8086
11.8k
}
8087
8088
static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
8089
1.62k
{
8090
1.62k
  zend_op_array *op_array = CG(active_op_array);
8091
1.62k
  zend_ast_list *list = zend_ast_get_list(ast);
8092
1.62k
  uint32_t i;
8093
8094
5.77k
  for (i = 0; i < list->children; ++i) {
8095
4.16k
    uint32_t mode = ZEND_BIND_EXPLICIT;
8096
4.16k
    zend_ast *var_ast = list->child[i];
8097
4.16k
    zend_string *var_name = zend_ast_get_str(var_ast);
8098
4.16k
    zval zv;
8099
4.16k
    ZVAL_NULL(&zv);
8100
8101
4.16k
    {
8102
4.16k
      int i;
8103
12.7k
      for (i = 0; i < op_array->last_var; i++) {
8104
8.57k
        if (zend_string_equals(op_array->vars[i], var_name)) {
8105
8
          zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8106
8
            "Cannot use lexical variable $%S as a parameter name", var_name);
8107
8
        }
8108
8.57k
      }
8109
4.16k
    }
8110
8111
4.15k
    CG(zend_lineno) = zend_ast_get_lineno(var_ast);
8112
8113
4.15k
    if (var_ast->attr) {
8114
1.30k
      mode |= ZEND_BIND_REF;
8115
1.30k
    }
8116
8117
4.15k
    zend_compile_static_var_common(var_name, &zv, mode);
8118
4.15k
  }
8119
1.62k
}
8120
/* }}} */
8121
8122
static void zend_compile_implicit_closure_uses(closure_info *info)
8123
19.5k
{
8124
19.5k
  zend_string *var_name;
8125
254k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8126
254k
    zval zv;
8127
254k
    ZVAL_NULL(&zv);
8128
254k
    zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT);
8129
254k
  ZEND_HASH_FOREACH_END();
8130
19.5k
}
8131
8132
603
static void add_stringable_interface(zend_class_entry *ce) {
8133
691
  for (uint32_t i = 0; i < ce->num_interfaces; i++) {
8134
113
    if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) {
8135
      /* Interface already explicitly implemented */
8136
25
      return;
8137
25
    }
8138
113
  }
8139
8140
578
  ce->num_interfaces++;
8141
578
  ce->interface_names =
8142
578
    erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
8143
  // TODO: Add known interned strings instead?
8144
578
  ce->interface_names[ce->num_interfaces - 1].name =
8145
578
    ZSTR_INIT_LITERAL("Stringable", 0);
8146
578
  ce->interface_names[ce->num_interfaces - 1].lc_name =
8147
578
    ZSTR_INIT_LITERAL("stringable", 0);
8148
578
}
8149
8150
static zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, bool has_body) /* {{{ */
8151
32.3k
{
8152
32.3k
  zend_class_entry *ce = CG(active_class_entry);
8153
32.3k
  bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
8154
32.3k
  uint32_t fn_flags = op_array->fn_flags;
8155
8156
32.3k
  zend_string *lcname;
8157
8158
32.3k
  if (fn_flags & ZEND_ACC_READONLY) {
8159
0
    zend_error(E_COMPILE_ERROR, "Cannot use 'readonly' as method modifier");
8160
0
  }
8161
8162
32.3k
  if ((fn_flags & ZEND_ACC_PRIVATE) && (fn_flags & ZEND_ACC_FINAL) && !zend_is_constructor(name)) {
8163
223
    zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes");
8164
223
  }
8165
8166
32.3k
  if ((fn_flags & ZEND_ACC_ABSTRACT)
8167
32.3k
   && !(ce->ce_flags & (ZEND_ACC_EXPLICIT_ABSTRACT_CLASS|ZEND_ACC_TRAIT))) {
8168
    // Don't say that the class should be declared abstract if it is
8169
    // anonymous or an enum and can't be abstract
8170
30
    if (ce->ce_flags & ZEND_ACC_ANON_CLASS) {
8171
6
      zend_error_noreturn(E_COMPILE_ERROR, "Anonymous class method %s() must not be abstract",
8172
6
        ZSTR_VAL(name));
8173
24
    } else if (ce->ce_flags & (ZEND_ACC_ENUM|ZEND_ACC_INTERFACE)) {
8174
13
      zend_error_noreturn(E_COMPILE_ERROR, "%s method %s::%s() must not be abstract",
8175
13
        zend_get_object_type_case(ce, true), ZSTR_VAL(ce->name), ZSTR_VAL(name));
8176
13
    } else {
8177
11
      zend_error_noreturn(E_COMPILE_ERROR, "Class %s declares abstract method %s() and must therefore be declared abstract",
8178
11
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
8179
11
    }
8180
30
  }
8181
8182
32.3k
  if (in_interface) {
8183
831
    if (!(fn_flags & ZEND_ACC_PUBLIC)) {
8184
1
      zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
8185
1
        "%s::%s() must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8186
1
    }
8187
830
    if (fn_flags & ZEND_ACC_FINAL) {
8188
8
      zend_error_noreturn(E_COMPILE_ERROR, "Interface method "
8189
8
        "%s::%s() must not be final", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8190
8
    }
8191
822
    op_array->fn_flags |= ZEND_ACC_ABSTRACT;
8192
822
  }
8193
8194
32.3k
  if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
8195
1.21k
    if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8196
6
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
8197
6
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8198
6
    }
8199
8200
1.21k
    if (has_body) {
8201
8
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
8202
8
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8203
8
    }
8204
8205
1.20k
    ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
8206
31.1k
  } else if (!has_body) {
8207
8
    zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
8208
8
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8209
8
  }
8210
8211
32.3k
  op_array->scope = ce;
8212
32.3k
  op_array->function_name = zend_string_copy(name);
8213
8214
32.3k
  lcname = zend_string_tolower(name);
8215
32.3k
  lcname = zend_new_interned_string(lcname);
8216
8217
32.3k
  if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
8218
35
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
8219
35
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8220
35
  }
8221
8222
32.2k
  zend_add_magic_method(ce, (zend_function *) op_array, lcname);
8223
32.2k
  if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)
8224
32.2k
      && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8225
603
    add_stringable_interface(ce);
8226
603
  }
8227
8228
32.2k
  return lcname;
8229
32.3k
}
8230
/* }}} */
8231
8232
111k
static uint32_t zend_add_dynamic_func_def(zend_op_array *def) {
8233
111k
  zend_op_array *op_array = CG(active_op_array);
8234
111k
  uint32_t def_offset = op_array->num_dynamic_func_defs++;
8235
111k
  op_array->dynamic_func_defs = erealloc(
8236
111k
    op_array->dynamic_func_defs, op_array->num_dynamic_func_defs * sizeof(zend_op_array *));
8237
111k
  op_array->dynamic_func_defs[def_offset] = def;
8238
111k
  return def_offset;
8239
111k
}
8240
8241
enum func_decl_level {
8242
  FUNC_DECL_LEVEL_TOPLEVEL,
8243
  FUNC_DECL_LEVEL_NESTED,
8244
  FUNC_DECL_LEVEL_CONSTEXPR,
8245
};
8246
8247
static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array, zend_ast_decl *decl, enum func_decl_level level) /* {{{ */
8248
127k
{
8249
127k
  zend_string *unqualified_name, *name, *lcname;
8250
127k
  zend_op *opline;
8251
8252
127k
  if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8253
110k
    zend_string *filename = op_array->filename;
8254
110k
    uint32_t start_lineno = decl->start_lineno;
8255
8256
110k
    zend_string *class = zend_empty_string;
8257
110k
    zend_string *separator = zend_empty_string;
8258
110k
    zend_string *function = filename;
8259
110k
    char *parens = "";
8260
8261
110k
    if (CG(active_op_array) && CG(active_op_array)->function_name) {
8262
97.7k
      if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
8263
        /* If the parent function is a closure, don't redundantly
8264
         * add the classname and parentheses.
8265
         */
8266
95.5k
        function = CG(active_op_array)->function_name;
8267
95.5k
      } else {
8268
2.20k
        function = CG(active_op_array)->function_name;
8269
2.20k
        parens = "()";
8270
8271
2.20k
        if (CG(active_class_entry) && CG(active_class_entry)->name) {
8272
1.60k
          class = CG(active_class_entry)->name;
8273
1.60k
          separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM);
8274
1.60k
        }
8275
2.20k
      }
8276
97.7k
    }
8277
8278
110k
    unqualified_name = zend_strpprintf_unchecked(
8279
110k
      0,
8280
110k
      "{closure:%S%S%S%s:%" PRIu32 "}",
8281
110k
      class,
8282
110k
      separator,
8283
110k
      function,
8284
110k
      parens,
8285
110k
      start_lineno
8286
110k
    );
8287
8288
110k
    op_array->function_name = name = unqualified_name;
8289
110k
  } else {
8290
17.0k
    unqualified_name = decl->name;
8291
17.0k
    op_array->function_name = name = zend_prefix_with_ns(unqualified_name);
8292
17.0k
  }
8293
8294
127k
  lcname = zend_string_tolower(name);
8295
8296
127k
  if (FC(imports_function)) {
8297
110
    zend_string *import_name =
8298
110
      zend_hash_find_ptr_lc(FC(imports_function), unqualified_name);
8299
110
    if (import_name && !zend_string_equals_ci(lcname, import_name)) {
8300
11
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare function %s() (previously declared as local import)",
8301
11
        ZSTR_VAL(name));
8302
11
    }
8303
110
  }
8304
8305
127k
  if (zend_string_equals_literal(lcname, "__autoload")) {
8306
1
    zend_error_noreturn(E_COMPILE_ERROR,
8307
1
      "__autoload() is no longer supported, use spl_autoload_register() instead");
8308
1
  }
8309
8310
127k
  if (zend_string_equals_literal_ci(unqualified_name, "assert")) {
8311
6
    zend_error(E_COMPILE_ERROR,
8312
6
      "Defining a custom assert() function is not allowed, "
8313
6
      "as the function has special semantics");
8314
6
  }
8315
8316
127k
  zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
8317
127k
  switch (level) {
8318
111k
    case FUNC_DECL_LEVEL_NESTED: {
8319
111k
      uint32_t func_ref = zend_add_dynamic_func_def(op_array);
8320
111k
      if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8321
110k
        opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
8322
110k
        opline->op2.num = func_ref;
8323
110k
      } else {
8324
1.45k
        opline = get_next_op();
8325
1.45k
        opline->opcode = ZEND_DECLARE_FUNCTION;
8326
1.45k
        opline->op1_type = IS_CONST;
8327
1.45k
        LITERAL_STR(opline->op1, zend_string_copy(lcname));
8328
1.45k
        opline->op2.num = func_ref;
8329
1.45k
      }
8330
111k
      break;
8331
0
    }
8332
132
    case FUNC_DECL_LEVEL_CONSTEXPR:
8333
15.6k
    case FUNC_DECL_LEVEL_TOPLEVEL:
8334
      /* Nothing to do. */
8335
15.6k
      break;
8336
127k
  }
8337
127k
  return lcname;
8338
127k
}
8339
/* }}} */
8340
8341
static zend_op_array *zend_compile_func_decl_ex(
8342
  znode *result, zend_ast *ast, enum func_decl_level level,
8343
  zend_string *property_info_name,
8344
  zend_property_hook_kind hook_kind
8345
164k
) {
8346
164k
  zend_ast_decl *decl = (zend_ast_decl *) ast;
8347
164k
  zend_ast *params_ast = decl->child[0];
8348
164k
  zend_ast *uses_ast = decl->child[1];
8349
164k
  zend_ast *stmt_ast = decl->child[2];
8350
164k
  zend_ast *return_type_ast = decl->child[3];
8351
164k
  bool is_method = decl->kind == ZEND_AST_METHOD;
8352
164k
  zend_string *lcname = NULL;
8353
164k
  bool is_hook = decl->kind == ZEND_AST_PROPERTY_HOOK;
8354
8355
164k
  zend_class_entry *orig_class_entry = CG(active_class_entry);
8356
164k
  zend_op_array *orig_op_array = CG(active_op_array);
8357
164k
  zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
8358
164k
  zend_oparray_context orig_oparray_context;
8359
164k
  closure_info info;
8360
164k
  memset(&info, 0, sizeof(closure_info));
8361
8362
164k
  init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
8363
8364
164k
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
8365
0
    op_array->fn_flags |= ZEND_ACC_PRELOADED;
8366
0
  }
8367
8368
164k
  op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
8369
164k
  op_array->fn_flags |= decl->flags;
8370
164k
  op_array->line_start = decl->start_lineno;
8371
164k
  op_array->line_end = decl->end_lineno;
8372
164k
  if (decl->doc_comment) {
8373
455
    op_array->doc_comment = zend_string_copy(decl->doc_comment);
8374
455
  }
8375
8376
164k
  if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
8377
110k
    op_array->fn_flags |= ZEND_ACC_CLOSURE;
8378
110k
  }
8379
8380
164k
  if (is_hook) {
8381
4.85k
    zend_class_entry *ce = CG(active_class_entry);
8382
4.85k
    op_array->scope = ce;
8383
4.85k
    op_array->function_name = zend_string_copy(decl->name);
8384
159k
  } else if (is_method) {
8385
32.3k
    bool has_body = stmt_ast != NULL;
8386
32.3k
    lcname = zend_begin_method_decl(op_array, decl->name, has_body);
8387
127k
  } else {
8388
127k
    lcname = zend_begin_func_decl(result, op_array, decl, level);
8389
127k
    if (decl->kind == ZEND_AST_ARROW_FUNC) {
8390
19.6k
      find_implicit_binds(&info, params_ast, stmt_ast);
8391
19.6k
      compile_implicit_lexical_binds(&info, result, op_array);
8392
107k
    } else if (uses_ast) {
8393
1.64k
      zend_compile_closure_binding(result, op_array, uses_ast);
8394
1.64k
    }
8395
127k
  }
8396
8397
164k
  CG(active_op_array) = op_array;
8398
8399
164k
  if (decl->child[4]) {
8400
78.2k
    int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
8401
8402
78.2k
    if (is_method || is_hook) {
8403
960
      target = ZEND_ATTRIBUTE_TARGET_METHOD;
8404
960
    }
8405
8406
78.2k
    zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target, 0);
8407
8408
78.2k
    zend_attribute *override_attribute = zend_get_attribute_str(
8409
78.2k
      op_array->attributes,
8410
78.2k
      "override",
8411
78.2k
      sizeof("override")-1
8412
78.2k
    );
8413
8414
78.2k
    if (override_attribute) {
8415
327
      op_array->fn_flags |= ZEND_ACC_OVERRIDE;
8416
327
    }
8417
8418
78.2k
    zend_attribute *deprecated_attribute = zend_get_attribute_str(
8419
78.2k
      op_array->attributes,
8420
78.2k
      "deprecated",
8421
78.2k
      sizeof("deprecated")-1
8422
78.2k
    );
8423
8424
78.2k
    if (deprecated_attribute) {
8425
244
      op_array->fn_flags |= ZEND_ACC_DEPRECATED;
8426
244
    }
8427
8428
78.2k
    zend_attribute *nodiscard_attribute = zend_get_attribute_str(
8429
78.2k
      op_array->attributes,
8430
78.2k
      "nodiscard",
8431
78.2k
      sizeof("nodiscard")-1
8432
78.2k
    );
8433
8434
78.2k
    if (nodiscard_attribute) {
8435
235
      op_array->fn_flags |= ZEND_ACC_NODISCARD;
8436
235
    }
8437
78.2k
  }
8438
8439
  /* Do not leak the class scope into free standing functions, even if they are dynamically
8440
   * defined inside a class method. This is necessary for correct handling of magic constants.
8441
   * For example __CLASS__ should always be "" inside a free standing function. */
8442
164k
  if (decl->kind == ZEND_AST_FUNC_DECL) {
8443
16.9k
    CG(active_class_entry) = NULL;
8444
16.9k
  }
8445
8446
164k
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8447
15.5k
    op_array->fn_flags |= ZEND_ACC_TOP_LEVEL;
8448
15.5k
  }
8449
8450
164k
  zend_oparray_context_begin(&orig_oparray_context, op_array);
8451
164k
  CG(context).active_property_info_name = property_info_name;
8452
164k
  CG(context).active_property_hook_kind = hook_kind;
8453
8454
164k
  {
8455
    /* Push a separator to the loop variable stack */
8456
164k
    zend_loop_var dummy_var;
8457
164k
    dummy_var.opcode = ZEND_RETURN;
8458
8459
164k
    zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
8460
164k
  }
8461
8462
164k
  zend_compile_params(params_ast, return_type_ast,
8463
164k
    is_method && zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME) ? IS_STRING : 0);
8464
164k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
8465
5.11k
    zend_mark_function_as_generator();
8466
5.11k
    zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
8467
5.11k
  }
8468
164k
  if (decl->kind == ZEND_AST_ARROW_FUNC) {
8469
19.5k
    zend_compile_implicit_closure_uses(&info);
8470
19.5k
    zend_hash_destroy(&info.uses);
8471
144k
  } else if (uses_ast) {
8472
1.62k
    zend_compile_closure_uses(uses_ast);
8473
1.62k
  }
8474
8475
164k
  if (ast->kind == ZEND_AST_ARROW_FUNC && decl->child[2]->kind != ZEND_AST_RETURN) {
8476
15.6k
    bool needs_return = true;
8477
15.6k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8478
950
      zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8479
950
      needs_return = !ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER);
8480
950
    }
8481
15.6k
    if (needs_return) {
8482
15.4k
      stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
8483
15.4k
      decl->child[2] = stmt_ast;
8484
15.4k
    }
8485
15.6k
  }
8486
8487
164k
  if (op_array->fn_flags & ZEND_ACC_NODISCARD) {
8488
235
    if (is_hook) {
8489
6
      zend_error_noreturn(E_COMPILE_ERROR, "#[\\NoDiscard] is not supported for property hooks");
8490
6
    }
8491
8492
229
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8493
183
      zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8494
183
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_VOID)) {
8495
6
        zend_error_noreturn(E_COMPILE_ERROR,
8496
6
          "A void %s does not return a value, but #[\\NoDiscard] requires a return value",
8497
6
          CG(active_class_entry) != NULL ? "method" : "function");
8498
6
      }
8499
8500
177
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
8501
7
        zend_error_noreturn(E_COMPILE_ERROR,
8502
7
          "A never returning %s does not return a value, but #[\\NoDiscard] requires a return value",
8503
7
          CG(active_class_entry) != NULL ? "method" : "function");
8504
7
      }
8505
177
    }
8506
229
  }
8507
8508
164k
  zend_compile_stmt(stmt_ast);
8509
8510
164k
  if (is_method) {
8511
31.9k
    CG(zend_lineno) = decl->start_lineno;
8512
31.9k
    zend_check_magic_method_implementation(
8513
31.9k
      CG(active_class_entry), (zend_function *) op_array, lcname, E_COMPILE_ERROR);
8514
132k
  } else if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8515
    /* Only register the function after a successful compile */
8516
15.0k
    if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
8517
111
      CG(zend_lineno) = decl->start_lineno;
8518
111
      do_bind_function_error(lcname, op_array, true);
8519
111
    }
8520
15.0k
  }
8521
8522
  /* put the implicit return on the really last line */
8523
164k
  CG(zend_lineno) = decl->end_lineno;
8524
8525
164k
  zend_do_extended_stmt();
8526
164k
  zend_emit_final_return(0);
8527
8528
164k
  pass_two(CG(active_op_array));
8529
164k
  zend_oparray_context_end(&orig_oparray_context);
8530
8531
  /* Pop the loop variable stack separator */
8532
164k
  zend_stack_del_top(&CG(loop_var_stack));
8533
8534
164k
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8535
14.8k
    zend_observer_function_declared_notify(op_array, lcname);
8536
14.8k
  }
8537
8538
164k
  if (lcname != NULL) {
8539
157k
    zend_string_release_ex(lcname, 0);
8540
157k
  }
8541
8542
164k
  CG(active_op_array) = orig_op_array;
8543
164k
  CG(active_class_entry) = orig_class_entry;
8544
8545
164k
  return op_array;
8546
164k
}
8547
8548
static zend_op_array *zend_compile_func_decl(znode *result, zend_ast *ast, enum func_decl_level level)
8549
159k
{
8550
159k
  return zend_compile_func_decl_ex(result, ast, level, /* property_info */ NULL, (zend_property_hook_kind)-1);
8551
159k
}
8552
8553
5.38k
zend_property_hook_kind zend_get_property_hook_kind_from_name(zend_string *name) {
8554
5.38k
  if (zend_string_equals_literal_ci(name, "get")) {
8555
3.42k
    return ZEND_PROPERTY_HOOK_GET;
8556
3.42k
  } else if (zend_string_equals_literal_ci(name, "set")) {
8557
1.83k
    return ZEND_PROPERTY_HOOK_SET;
8558
1.83k
  } else {
8559
130
    return (zend_property_hook_kind)-1;
8560
130
  }
8561
5.38k
}
8562
8563
static void zend_compile_property_hooks(
8564
    zend_property_info *prop_info, zend_string *prop_name,
8565
    zend_ast *prop_type_ast, zend_ast_list *hooks)
8566
4.00k
{
8567
4.00k
  zend_class_entry *ce = CG(active_class_entry);
8568
8569
4.00k
  if (prop_info->flags & ZEND_ACC_READONLY) {
8570
16
    zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly");
8571
16
  }
8572
8573
3.98k
  if (hooks->children == 0) {
8574
14
    zend_error_noreturn(E_COMPILE_ERROR, "Property hook list must not be empty");
8575
14
  }
8576
8577
8.79k
  for (uint32_t i = 0; i < hooks->children; i++) {
8578
5.08k
    zend_ast_decl *hook = (zend_ast_decl *) hooks->child[i];
8579
5.08k
    zend_string *name = hook->name;
8580
5.08k
    zend_ast *stmt_ast = hook->child[2];
8581
5.08k
    zend_ast **return_type_ast_ptr = NULL;
8582
5.08k
    zend_ast **value_type_ast_ptr = NULL;
8583
5.08k
    CG(zend_lineno) = hook->start_lineno;
8584
8585
    /* Non-private hooks are always public. This avoids having to copy the hook when inheriting
8586
     * hooks from protected properties to public ones. */
8587
5.08k
    uint32_t hook_visibility = (prop_info->flags & ZEND_ACC_PPP_MASK) != ZEND_ACC_PRIVATE ? ZEND_ACC_PUBLIC : ZEND_ACC_PRIVATE;
8588
5.08k
    hook->flags |= hook_visibility;
8589
8590
5.08k
    if (prop_info->flags & ZEND_ACC_STATIC) {
8591
31
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare hooks for static property");
8592
31
    }
8593
5.05k
    if ((hook->flags & ZEND_ACC_FINAL) && (hook->flags & ZEND_ACC_PRIVATE)) {
8594
7
      zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both final and private");
8595
7
    }
8596
5.04k
    if ((ce->ce_flags & ZEND_ACC_INTERFACE)
8597
5.04k
     || ((prop_info->flags & ZEND_ACC_ABSTRACT) && !stmt_ast)) {
8598
1.02k
      hook->flags |= ZEND_ACC_ABSTRACT;
8599
8600
1.02k
      if (stmt_ast) {
8601
1
        zend_error_noreturn(E_COMPILE_ERROR, "Abstract property hook cannot have body");
8602
1
      }
8603
1.02k
      if (hook->flags & ZEND_ACC_PRIVATE) {
8604
6
        zend_error_noreturn(E_COMPILE_ERROR,
8605
6
          "Property hook cannot be both abstract and private");
8606
6
      }
8607
1.01k
      if (hook->flags & ZEND_ACC_FINAL) {
8608
7
        zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both abstract and final");
8609
7
      }
8610
4.02k
    } else if (!stmt_ast) {
8611
14
      zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract property hook must have a body");
8612
14
    }
8613
8614
5.01k
    zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(name);
8615
5.01k
    if (hook_kind == (zend_property_hook_kind)-1) {
8616
130
      zend_error_noreturn(E_COMPILE_ERROR,
8617
130
        "Unknown hook \"%s\" for property %s::$%s, expected \"get\" or \"set\"",
8618
130
        ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8619
130
    }
8620
8621
4.88k
    if (stmt_ast && stmt_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
8622
1.42k
      stmt_ast = stmt_ast->child[0];
8623
1.42k
      if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
8624
1.17k
        stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
8625
1.17k
      } else {
8626
255
        ZEND_ASSERT(hook_kind == ZEND_PROPERTY_HOOK_SET);
8627
255
        stmt_ast = zend_ast_create(ZEND_AST_ASSIGN,
8628
255
          zend_ast_create(ZEND_AST_PROP,
8629
255
            zend_ast_create(ZEND_AST_VAR, zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_THIS))),
8630
255
            zend_ast_create_zval_from_str(zend_copy_unmangled_prop_name(prop_info->name))),
8631
255
          stmt_ast);
8632
255
      }
8633
1.42k
      stmt_ast = zend_ast_create_list(1, ZEND_AST_STMT_LIST, stmt_ast);
8634
1.42k
      hook->child[2] = stmt_ast;
8635
1.42k
    }
8636
8637
4.88k
    if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
8638
3.11k
      if (hook->child[0]) {
8639
6
        zend_error_noreturn(E_COMPILE_ERROR, "get hook of property %s::$%s must not have a parameter list",
8640
6
          ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8641
6
      }
8642
8643
3.10k
      hook->child[0] = zend_ast_create_list(0, ZEND_AST_PARAM_LIST);
8644
8645
3.10k
      return_type_ast_ptr = &hook->child[3];
8646
3.10k
      *return_type_ast_ptr = prop_type_ast;
8647
3.10k
    } else if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
8648
1.77k
      if (hook->child[0]) {
8649
175
        zend_ast_list *param_list = zend_ast_get_list(hook->child[0]);
8650
175
        if (param_list->children != 1) {
8651
0
          zend_error_noreturn(E_COMPILE_ERROR, "%s hook of property %s::$%s must accept exactly one parameters",
8652
0
            ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8653
0
        }
8654
175
        zend_ast *value_param_ast = param_list->child[0];
8655
175
        if (value_param_ast->attr & ZEND_PARAM_REF) {
8656
6
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be pass-by-reference",
8657
6
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8658
6
        }
8659
169
        if (value_param_ast->attr & ZEND_PARAM_VARIADIC) {
8660
6
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be variadic",
8661
6
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8662
6
        }
8663
163
        if (value_param_ast->child[2]) {
8664
6
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not have a default value",
8665
6
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8666
6
        }
8667
157
        if ((prop_type_ast != NULL) != (value_param_ast->child[0] != NULL)) {
8668
6
          zend_hooked_property_variance_error_ex(zend_ast_get_str(value_param_ast->child[1]), ce->name, prop_info->name);
8669
6
        }
8670
1.60k
      } else {
8671
1.60k
        zend_ast *param_name_ast = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VALUE));
8672
1.60k
        zend_ast *param = zend_ast_create(
8673
1.60k
          ZEND_AST_PARAM, prop_type_ast, param_name_ast,
8674
1.60k
          /* expr */ NULL, /* doc_comment */ NULL, /* attributes */ NULL,
8675
1.60k
          /* hooks */ NULL);
8676
1.60k
        value_type_ast_ptr = &param->child[0];
8677
1.60k
        hook->child[0] = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, param);
8678
1.60k
      }
8679
1.75k
      zend_ast *return_type = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VOID));
8680
1.75k
      return_type->attr = ZEND_NAME_NOT_FQ;
8681
1.75k
      hook->child[3] = return_type;
8682
1.75k
    } else {
8683
0
      ZEND_UNREACHABLE();
8684
0
    }
8685
8686
4.85k
    hook->name = zend_strpprintf(0, "$%s::%s", ZSTR_VAL(prop_name), ZSTR_VAL(name));
8687
8688
4.85k
    zend_function *func = (zend_function *) zend_compile_func_decl_ex(
8689
4.85k
      NULL, (zend_ast *) hook, FUNC_DECL_LEVEL_NESTED, prop_info->name, hook_kind);
8690
8691
4.85k
    func->common.prop_info = prop_info;
8692
8693
4.85k
    if (!prop_info->hooks) {
8694
3.71k
      prop_info->hooks = zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE);
8695
3.71k
      memset(prop_info->hooks, 0, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
8696
3.71k
    }
8697
8698
4.85k
    if (prop_info->hooks[hook_kind]) {
8699
31
      zend_error_noreturn(E_COMPILE_ERROR,
8700
31
        "Cannot redeclare property hook \"%s\"", ZSTR_VAL(name));
8701
31
    }
8702
4.82k
    prop_info->hooks[hook_kind] = func;
8703
8704
4.82k
    if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
8705
1.72k
      switch (zend_verify_property_hook_variance(prop_info, func)) {
8706
1.66k
        case INHERITANCE_SUCCESS:
8707
1.66k
          break;
8708
53
        case INHERITANCE_UNRESOLVED:
8709
53
          ce->num_hooked_prop_variance_checks++;
8710
53
          break;
8711
8
        case INHERITANCE_ERROR:
8712
8
          zend_hooked_property_variance_error(prop_info);
8713
0
        case INHERITANCE_WARNING:
8714
0
          ZEND_UNREACHABLE();
8715
1.72k
      }
8716
1.72k
    }
8717
8718
4.81k
    zend_string_release(name);
8719
    /* Un-share type ASTs to avoid double-frees of zval nodes. */
8720
4.81k
    if (return_type_ast_ptr) {
8721
3.04k
      *return_type_ast_ptr = NULL;
8722
3.04k
    }
8723
4.81k
    if (value_type_ast_ptr) {
8724
1.57k
      *value_type_ast_ptr = NULL;
8725
1.57k
    }
8726
4.81k
  }
8727
8728
3.70k
  ce->num_hooked_props++;
8729
8730
  /* See zend_link_hooked_object_iter(). */
8731
3.70k
#ifndef ZEND_OPCACHE_SHM_REATTACHMENT
8732
3.70k
  if (!ce->get_iterator) {
8733
    /* Will be removed again, in case of Iterator or IteratorAggregate. */
8734
2.83k
    ce->get_iterator = zend_hooked_object_get_iterator;
8735
2.83k
  }
8736
3.70k
#endif
8737
8738
3.70k
  if (!prop_info->ce->parent_name) {
8739
2.47k
    zend_verify_hooked_property(ce, prop_info, prop_name);
8740
2.47k
  }
8741
3.70k
}
8742
8743
static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */
8744
30.6k
{
8745
30.6k
  zend_ast_list *list = zend_ast_get_list(ast);
8746
30.6k
  zend_class_entry *ce = CG(active_class_entry);
8747
30.6k
  uint32_t i, children = list->children;
8748
8749
30.6k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
8750
14
    zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", ZSTR_VAL(ce->name));
8751
14
  }
8752
8753
30.5k
  if ((flags & ZEND_ACC_FINAL) && (flags & ZEND_ACC_PRIVATE)) {
8754
6
    zend_error_noreturn(E_COMPILE_ERROR, "Property cannot be both final and private");
8755
6
  }
8756
8757
30.5k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
8758
310
    if (flags & ZEND_ACC_FINAL) {
8759
6
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be final");
8760
6
    }
8761
304
    if (flags & (ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE)) {
8762
11
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be protected or private");
8763
11
    }
8764
293
    if (flags & ZEND_ACC_ABSTRACT) {
8765
6
      zend_error_noreturn(E_COMPILE_ERROR,
8766
6
        "Property in interface cannot be explicitly abstract. "
8767
6
        "All interface members are implicitly abstract");
8768
6
    }
8769
287
    flags |= ZEND_ACC_ABSTRACT;
8770
287
  }
8771
8772
61.0k
  for (i = 0; i < children; ++i) {
8773
30.6k
    zend_property_info *info;
8774
30.6k
    zend_ast *prop_ast = list->child[i];
8775
30.6k
    zend_ast *name_ast = prop_ast->child[0];
8776
30.6k
    zend_ast **value_ast_ptr = &prop_ast->child[1];
8777
30.6k
    zend_ast *doc_comment_ast = prop_ast->child[2];
8778
30.6k
    zend_ast *hooks_ast = prop_ast->child[3];
8779
30.6k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
8780
30.6k
    zend_string *doc_comment = NULL;
8781
30.6k
    zval value_zv;
8782
30.6k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
8783
30.6k
    flags |= zend_property_is_virtual(ce, name, hooks_ast, flags) ? ZEND_ACC_VIRTUAL : 0;
8784
8785
30.6k
    zend_string *old_active_property_info_name = CG(context).active_property_info_name;
8786
30.6k
    CG(context).active_property_info_name = name;
8787
8788
30.6k
    if (!hooks_ast) {
8789
26.7k
      if (ce->ce_flags & ZEND_ACC_INTERFACE) {
8790
1
        zend_error_noreturn(E_COMPILE_ERROR,
8791
1
          "Interfaces may only include hooked properties");
8792
1
      }
8793
26.7k
      if (flags & ZEND_ACC_ABSTRACT) {
8794
7
        zend_error_noreturn(E_COMPILE_ERROR,
8795
7
          "Only hooked properties may be declared abstract");
8796
7
      }
8797
26.7k
    }
8798
30.6k
    if ((flags & ZEND_ACC_ABSTRACT)) {
8799
950
      ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
8800
950
    }
8801
8802
30.6k
    if (type_ast) {
8803
18.0k
      type = zend_compile_typename(type_ast);
8804
8805
18.0k
      if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_NEVER|MAY_BE_CALLABLE)) {
8806
6
        zend_string *str = zend_type_to_string(type);
8807
6
        zend_error_noreturn(E_COMPILE_ERROR,
8808
6
          "Property %s::$%s cannot have type %s",
8809
6
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
8810
6
      }
8811
18.0k
    }
8812
8813
    /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */
8814
30.6k
    if (doc_comment_ast) {
8815
414
      doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
8816
414
    }
8817
8818
30.6k
    if (zend_hash_exists(&ce->properties_info, name)) {
8819
53
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
8820
53
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
8821
53
    }
8822
8823
30.6k
    if (*value_ast_ptr) {
8824
10.6k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
8825
8826
10.6k
      if (ZEND_TYPE_IS_SET(type) && !Z_CONSTANT(value_zv)
8827
10.6k
          && !zend_is_valid_default_value(type, &value_zv)) {
8828
80
        zend_string *str = zend_type_to_string(type);
8829
80
        if (Z_TYPE(value_zv) == IS_NULL && !ZEND_TYPE_IS_INTERSECTION(type)) {
8830
22
          ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
8831
22
          zend_string *nullable_str = zend_type_to_string(type);
8832
8833
22
          zend_error_noreturn(E_COMPILE_ERROR,
8834
22
            "Default value for property of type %s may not be null. "
8835
22
            "Use the nullable type %s to allow null default value",
8836
22
            ZSTR_VAL(str), ZSTR_VAL(nullable_str));
8837
58
        } else {
8838
58
          zend_error_noreturn(E_COMPILE_ERROR,
8839
58
            "Cannot use %s as default value for property %s::$%s of type %s",
8840
58
            zend_zval_value_name(&value_zv),
8841
58
            ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
8842
58
        }
8843
80
      }
8844
19.9k
    } else if (!ZEND_TYPE_IS_SET(type) && !hooks_ast) {
8845
3.16k
      ZVAL_NULL(&value_zv);
8846
16.7k
    } else {
8847
16.7k
      ZVAL_UNDEF(&value_zv);
8848
16.7k
    }
8849
8850
30.5k
    if ((ce->ce_flags & ZEND_ACC_READONLY_CLASS)) {
8851
33
      flags |= ZEND_ACC_READONLY;
8852
33
    }
8853
8854
30.5k
    if (flags & ZEND_ACC_READONLY) {
8855
515
      if (!ZEND_TYPE_IS_SET(type)) {
8856
14
        zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
8857
14
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
8858
14
      }
8859
501
      if (!Z_ISUNDEF(value_zv)) {
8860
6
        zend_error_noreturn(E_COMPILE_ERROR,
8861
6
          "Readonly property %s::$%s cannot have default value",
8862
6
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
8863
6
      }
8864
495
      if (flags & ZEND_ACC_STATIC) {
8865
11
        zend_error_noreturn(E_COMPILE_ERROR,
8866
11
          "Static property %s::$%s cannot be readonly",
8867
11
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
8868
11
      }
8869
495
    }
8870
8871
30.4k
    info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type);
8872
8873
30.4k
    if (hooks_ast) {
8874
3.91k
      zend_compile_property_hooks(info, name, type_ast, zend_ast_get_list(hooks_ast));
8875
3.91k
    }
8876
8877
30.4k
    if (attr_ast) {
8878
185
      zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0);
8879
185
    }
8880
8881
30.4k
    CG(context).active_property_info_name = old_active_property_info_name;
8882
30.4k
  }
8883
30.5k
}
8884
/* }}} */
8885
8886
static void zend_compile_prop_group(zend_ast *ast) /* {{{ */
8887
30.6k
{
8888
30.6k
  zend_ast *type_ast = ast->child[0];
8889
30.6k
  zend_ast *prop_ast = ast->child[1];
8890
30.6k
  zend_ast *attr_ast = ast->child[2];
8891
8892
30.6k
  zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast);
8893
30.6k
}
8894
/* }}} */
8895
8896
static void zend_check_trait_alias_modifiers(uint32_t attr) /* {{{ */
8897
3.15k
{
8898
3.15k
  if (attr & ZEND_ACC_STATIC) {
8899
6
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"static\" as method modifier in trait alias");
8900
3.14k
  } else if (attr & ZEND_ACC_ABSTRACT) {
8901
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"abstract\" as method modifier in trait alias");
8902
7
  }
8903
3.15k
}
8904
/* }}} */
8905
8906
static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast, zend_ast *type_ast)
8907
5.53k
{
8908
5.53k
  zend_ast_list *list = zend_ast_get_list(ast);
8909
5.53k
  zend_class_entry *ce = CG(active_class_entry);
8910
5.53k
  uint32_t i, children = list->children;
8911
8912
11.0k
  for (i = 0; i < children; ++i) {
8913
5.57k
    zend_class_constant *c;
8914
5.57k
    zend_ast *const_ast = list->child[i];
8915
5.57k
    zend_ast *name_ast = const_ast->child[0];
8916
5.57k
    zend_ast **value_ast_ptr = &const_ast->child[1];
8917
5.57k
    zend_ast *doc_comment_ast = const_ast->child[2];
8918
5.57k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
8919
5.57k
    zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
8920
5.57k
    zval value_zv;
8921
5.57k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
8922
8923
5.57k
    if (type_ast) {
8924
1.36k
      type = zend_compile_typename(type_ast);
8925
8926
1.36k
      uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
8927
8928
1.36k
      if (type_mask != MAY_BE_ANY && (type_mask & (MAY_BE_CALLABLE|MAY_BE_VOID|MAY_BE_NEVER))) {
8929
6
        zend_string *type_str = zend_type_to_string(type);
8930
8931
6
        zend_error_noreturn(E_COMPILE_ERROR, "Class constant %s::%s cannot have type %s",
8932
6
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
8933
6
      }
8934
1.36k
    }
8935
8936
5.57k
    if (UNEXPECTED((flags & ZEND_ACC_PRIVATE) && (flags & ZEND_ACC_FINAL))) {
8937
6
      zend_error_noreturn(
8938
6
        E_COMPILE_ERROR, "Private constant %s::%s cannot be final as it is not visible to other classes",
8939
6
        ZSTR_VAL(ce->name), ZSTR_VAL(name)
8940
6
      );
8941
6
    }
8942
8943
5.56k
    zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
8944
8945
5.56k
    if (!Z_CONSTANT(value_zv) && ZEND_TYPE_IS_SET(type) && !zend_is_valid_default_value(type, &value_zv)) {
8946
34
      zend_string *type_str = zend_type_to_string(type);
8947
8948
34
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as value for class constant %s::%s of type %s",
8949
34
        zend_zval_type_name(&value_zv), ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
8950
34
    }
8951
8952
5.53k
    c = zend_declare_typed_class_constant(ce, name, &value_zv, flags, doc_comment, type);
8953
8954
5.53k
    if (attr_ast) {
8955
149
      zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
8956
8957
149
      zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
8958
8959
149
      if (deprecated) {
8960
71
        ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
8961
        /* For deprecated constants, we need to flag the zval for recursion
8962
         * detection. Make sure the zval is separated out of shm. */
8963
71
        ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
8964
71
        ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
8965
71
      }
8966
149
    }
8967
5.53k
  }
8968
5.53k
}
8969
8970
static void zend_compile_class_const_group(zend_ast *ast) /* {{{ */
8971
5.53k
{
8972
5.53k
  zend_ast *const_ast = ast->child[0];
8973
5.53k
  zend_ast *attr_ast = ast->child[1];
8974
5.53k
  zend_ast *type_ast = ast->child[2];
8975
8976
5.53k
  zend_compile_class_const_decl(const_ast, ast->attr, attr_ast, type_ast);
8977
5.53k
}
8978
/* }}} */
8979
8980
static void zend_compile_method_ref(zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */
8981
3.70k
{
8982
3.70k
  zend_ast *class_ast = ast->child[0];
8983
3.70k
  zend_ast *method_ast = ast->child[1];
8984
8985
3.70k
  method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
8986
8987
3.70k
  if (class_ast) {
8988
1.56k
    method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name");
8989
2.13k
  } else {
8990
2.13k
    method_ref->class_name = NULL;
8991
2.13k
  }
8992
3.70k
}
8993
/* }}} */
8994
8995
static void zend_compile_trait_precedence(zend_ast *ast) /* {{{ */
8996
566
{
8997
566
  zend_ast *method_ref_ast = ast->child[0];
8998
566
  zend_ast *insteadof_ast = ast->child[1];
8999
566
  zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast);
9000
566
  uint32_t i;
9001
9002
566
  zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*));
9003
566
  zend_compile_method_ref(method_ref_ast, &precedence->trait_method);
9004
566
  precedence->num_excludes = insteadof_list->children;
9005
9006
1.79k
  for (i = 0; i < insteadof_list->children; ++i) {
9007
1.22k
    zend_ast *name_ast = insteadof_list->child[i];
9008
1.22k
    precedence->exclude_class_names[i] =
9009
1.22k
      zend_resolve_const_class_name_reference(name_ast, "trait name");
9010
1.22k
  }
9011
9012
566
  zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
9013
566
}
9014
/* }}} */
9015
9016
static void zend_compile_trait_alias(zend_ast *ast) /* {{{ */
9017
3.15k
{
9018
3.15k
  zend_ast *method_ref_ast = ast->child[0];
9019
3.15k
  zend_ast *alias_ast = ast->child[1];
9020
3.15k
  uint32_t modifiers = ast->attr;
9021
9022
3.15k
  zend_trait_alias *alias;
9023
9024
3.15k
  zend_check_trait_alias_modifiers(modifiers);
9025
9026
3.15k
  alias = emalloc(sizeof(zend_trait_alias));
9027
3.15k
  zend_compile_method_ref(method_ref_ast, &alias->trait_method);
9028
3.15k
  alias->modifiers = modifiers;
9029
9030
3.15k
  if (alias_ast) {
9031
2.89k
    alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
9032
2.89k
  } else {
9033
253
    alias->alias = NULL;
9034
253
  }
9035
9036
3.15k
  zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
9037
3.15k
}
9038
/* }}} */
9039
9040
static void zend_compile_use_trait(zend_ast *ast) /* {{{ */
9041
5.46k
{
9042
5.46k
  zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
9043
5.46k
  zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
9044
5.46k
  zend_class_entry *ce = CG(active_class_entry);
9045
5.46k
  uint32_t i;
9046
9047
5.46k
  ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children));
9048
9049
12.8k
  for (i = 0; i < traits->children; ++i) {
9050
7.33k
    zend_ast *trait_ast = traits->child[i];
9051
9052
7.33k
    if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9053
6
      zend_string *name = zend_ast_get_str(trait_ast);
9054
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
9055
6
        "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
9056
6
    }
9057
9058
7.33k
    ce->trait_names[ce->num_traits].name =
9059
7.33k
      zend_resolve_const_class_name_reference(trait_ast, "trait name");
9060
7.33k
    ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name);
9061
7.33k
    ce->num_traits++;
9062
7.33k
  }
9063
9064
5.46k
  if (!adaptations) {
9065
4.68k
    return;
9066
4.68k
  }
9067
9068
4.47k
  for (i = 0; i < adaptations->children; ++i) {
9069
3.71k
    zend_ast *adaptation_ast = adaptations->child[i];
9070
3.71k
    switch (adaptation_ast->kind) {
9071
566
      case ZEND_AST_TRAIT_PRECEDENCE:
9072
566
        zend_compile_trait_precedence(adaptation_ast);
9073
566
        break;
9074
3.15k
      case ZEND_AST_TRAIT_ALIAS:
9075
3.15k
        zend_compile_trait_alias(adaptation_ast);
9076
3.15k
        break;
9077
3.71k
      EMPTY_SWITCH_DEFAULT_CASE()
9078
3.71k
    }
9079
3.71k
  }
9080
776
}
9081
/* }}} */
9082
9083
static void zend_compile_implements(zend_ast *ast) /* {{{ */
9084
2.69k
{
9085
2.69k
  zend_ast_list *list = zend_ast_get_list(ast);
9086
2.69k
  zend_class_entry *ce = CG(active_class_entry);
9087
2.69k
  zend_class_name *interface_names;
9088
2.69k
  uint32_t i;
9089
9090
2.69k
  interface_names = emalloc(sizeof(zend_class_name) * list->children);
9091
9092
6.67k
  for (i = 0; i < list->children; ++i) {
9093
3.98k
    zend_ast *class_ast = list->child[i];
9094
3.98k
    interface_names[i].name =
9095
3.98k
      zend_resolve_const_class_name_reference(class_ast, "interface name");
9096
3.98k
    interface_names[i].lc_name = zend_string_tolower(interface_names[i].name);
9097
3.98k
  }
9098
9099
2.69k
  ce->num_interfaces = list->children;
9100
2.69k
  ce->interface_names = interface_names;
9101
2.69k
}
9102
/* }}} */
9103
9104
static zend_string *zend_generate_anon_class_name(zend_ast_decl *decl)
9105
2.31k
{
9106
2.31k
  zend_string *filename = CG(active_op_array)->filename;
9107
2.31k
  uint32_t start_lineno = decl->start_lineno;
9108
9109
  /* Use parent or first interface as prefix. */
9110
2.31k
  zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS);
9111
2.31k
  if (decl->child[0]) {
9112
215
    prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name");
9113
2.10k
  } else if (decl->child[1]) {
9114
533
    zend_ast_list *list = zend_ast_get_list(decl->child[1]);
9115
533
    prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name");
9116
533
  }
9117
9118
2.31k
  zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32,
9119
2.31k
    ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
9120
2.31k
  zend_string_release(prefix);
9121
2.31k
  return zend_new_interned_string(result);
9122
2.31k
}
9123
9124
static void zend_compile_enum_backing_type(zend_class_entry *ce, zend_ast *enum_backing_type_ast)
9125
913
{
9126
913
  ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM);
9127
913
  zend_type type = zend_compile_typename(enum_backing_type_ast);
9128
913
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9129
913
  if (ZEND_TYPE_IS_COMPLEX(type) || (type_mask != MAY_BE_LONG && type_mask != MAY_BE_STRING)) {
9130
49
    zend_string *type_string = zend_type_to_string(type);
9131
49
    zend_error_noreturn(E_COMPILE_ERROR,
9132
49
      "Enum backing type must be int or string, %s given",
9133
49
      ZSTR_VAL(type_string));
9134
49
  }
9135
864
  if (type_mask == MAY_BE_LONG) {
9136
644
    ce->enum_backing_type = IS_LONG;
9137
644
  } else {
9138
220
    ZEND_ASSERT(type_mask == MAY_BE_STRING);
9139
220
    ce->enum_backing_type = IS_STRING;
9140
220
  }
9141
864
  zend_type_release(type, 0);
9142
864
}
9143
9144
static void zend_compile_class_decl(znode *result, zend_ast *ast, bool toplevel) /* {{{ */
9145
68.4k
{
9146
68.4k
  zend_ast_decl *decl = (zend_ast_decl *) ast;
9147
68.4k
  zend_ast *extends_ast = decl->child[0];
9148
68.4k
  zend_ast *implements_ast = decl->child[1];
9149
68.4k
  zend_ast *stmt_ast = decl->child[2];
9150
68.4k
  zend_ast *enum_backing_type_ast = decl->child[4];
9151
68.4k
  zend_string *name, *lcname;
9152
68.4k
  zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
9153
68.4k
  zend_op *opline;
9154
9155
68.4k
  zend_class_entry *original_ce = CG(active_class_entry);
9156
9157
68.4k
  if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
9158
66.1k
    zend_string *unqualified_name = decl->name;
9159
9160
66.1k
    if (CG(active_class_entry)) {
9161
9
      zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
9162
9
    }
9163
9164
66.1k
    const char *type = "a class name";
9165
66.1k
    if (decl->flags & ZEND_ACC_ENUM) {
9166
4.75k
      type = "an enum name";
9167
61.3k
    } else if (decl->flags & ZEND_ACC_INTERFACE) {
9168
5.12k
      type = "an interface name";
9169
56.2k
    } else if (decl->flags & ZEND_ACC_TRAIT) {
9170
3.51k
      type = "a trait name";
9171
3.51k
    }
9172
66.1k
    zend_assert_valid_class_name(unqualified_name, type);
9173
66.1k
    name = zend_prefix_with_ns(unqualified_name);
9174
66.1k
    name = zend_new_interned_string(name);
9175
66.1k
    lcname = zend_string_tolower(name);
9176
9177
66.1k
    if (FC(imports)) {
9178
474
      zend_string *import_name =
9179
474
        zend_hash_find_ptr_lc(FC(imports), unqualified_name);
9180
474
      if (import_name && !zend_string_equals_ci(lcname, import_name)) {
9181
12
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s "
9182
12
            "(previously declared as local import)", ZSTR_VAL(name));
9183
12
      }
9184
474
    }
9185
9186
66.1k
    zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS);
9187
66.1k
  } else {
9188
    /* Find an anon class name that is not in use yet. */
9189
2.31k
    name = NULL;
9190
2.31k
    lcname = NULL;
9191
2.31k
    do {
9192
2.31k
      zend_tmp_string_release(name);
9193
2.31k
      zend_tmp_string_release(lcname);
9194
2.31k
      name = zend_generate_anon_class_name(decl);
9195
2.31k
      lcname = zend_string_tolower(name);
9196
2.31k
    } while (zend_hash_exists(CG(class_table), lcname));
9197
2.31k
  }
9198
68.4k
  lcname = zend_new_interned_string(lcname);
9199
9200
68.4k
  ce->type = ZEND_USER_CLASS;
9201
68.4k
  ce->name = name;
9202
68.4k
  zend_initialize_class_data(ce, 1);
9203
68.4k
  if (!(decl->flags & ZEND_ACC_ANON_CLASS)) {
9204
66.0k
    zend_alloc_ce_cache(ce->name);
9205
66.0k
  }
9206
9207
68.4k
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
9208
0
    ce->ce_flags |= ZEND_ACC_PRELOADED;
9209
0
    ZEND_MAP_PTR_NEW(ce->static_members_table);
9210
0
    ZEND_MAP_PTR_NEW(ce->mutable_data);
9211
0
  }
9212
9213
68.4k
  ce->ce_flags |= decl->flags;
9214
68.4k
  ce->info.user.filename = zend_string_copy(zend_get_compiled_filename());
9215
68.4k
  ce->info.user.line_start = decl->start_lineno;
9216
68.4k
  ce->info.user.line_end = decl->end_lineno;
9217
9218
68.4k
  if (decl->doc_comment) {
9219
114
    ce->doc_comment = zend_string_copy(decl->doc_comment);
9220
114
  }
9221
9222
68.4k
  if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
9223
    /* Serialization is not supported for anonymous classes */
9224
2.31k
    ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
9225
2.31k
  }
9226
9227
68.4k
  if (extends_ast) {
9228
18.7k
    ce->parent_name =
9229
18.7k
      zend_resolve_const_class_name_reference(extends_ast, "class name");
9230
18.7k
  }
9231
9232
68.4k
  CG(active_class_entry) = ce;
9233
9234
68.4k
  if (decl->child[3]) {
9235
2.13k
    zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS, 0);
9236
2.13k
  }
9237
9238
68.4k
  if (implements_ast) {
9239
2.69k
    zend_compile_implements(implements_ast);
9240
2.69k
  }
9241
9242
68.4k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
9243
4.74k
    if (enum_backing_type_ast != NULL) {
9244
913
      zend_compile_enum_backing_type(ce, enum_backing_type_ast);
9245
913
    }
9246
4.74k
    zend_enum_add_interfaces(ce);
9247
4.74k
    zend_enum_register_props(ce);
9248
4.74k
  }
9249
9250
68.4k
  zend_compile_stmt(stmt_ast);
9251
9252
  /* Reset lineno for final opcodes and errors */
9253
68.4k
  CG(zend_lineno) = ast->lineno;
9254
9255
68.4k
  if ((ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) == ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) {
9256
47
    zend_verify_abstract_class(ce);
9257
47
  }
9258
9259
68.4k
  CG(active_class_entry) = original_ce;
9260
9261
68.4k
  if (toplevel) {
9262
44.0k
    ce->ce_flags |= ZEND_ACC_TOP_LEVEL;
9263
44.0k
  }
9264
9265
  /* We currently don't early-bind classes that implement interfaces or use traits */
9266
68.4k
  if (!ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9267
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
9268
   /* See zend_link_hooked_object_iter(). */
9269
   && !ce->num_hooked_props
9270
#endif
9271
68.4k
   && !(CG(compiler_options) & ZEND_COMPILE_WITHOUT_EXECUTION)) {
9272
55.7k
    if (toplevel) {
9273
35.5k
      if (extends_ast) {
9274
9.65k
        zend_class_entry *parent_ce = zend_lookup_class_ex(
9275
9.65k
          ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
9276
9277
9.65k
        if (parent_ce
9278
9.65k
         && !zend_compile_ignore_class(parent_ce, ce->info.user.filename)) {
9279
8.83k
          if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) {
9280
4.55k
            zend_string_release(lcname);
9281
4.55k
            return;
9282
4.55k
          }
9283
8.83k
        }
9284
25.9k
      } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) {
9285
20.9k
        zend_string_release(lcname);
9286
20.9k
        zend_build_properties_info_table(ce);
9287
20.9k
        zend_inheritance_check_override(ce);
9288
20.9k
        ce->ce_flags |= ZEND_ACC_LINKED;
9289
20.9k
        zend_observer_class_linked_notify(ce, lcname);
9290
20.9k
        return;
9291
20.9k
      } else {
9292
4.97k
        goto link_unbound;
9293
4.97k
      }
9294
35.5k
    } else if (!extends_ast) {
9295
17.7k
link_unbound:
9296
      /* Link unbound simple class */
9297
17.7k
      zend_build_properties_info_table(ce);
9298
17.7k
      zend_inheritance_check_override(ce);
9299
17.7k
      ce->ce_flags |= ZEND_ACC_LINKED;
9300
17.7k
    }
9301
55.7k
  }
9302
9303
42.9k
  opline = get_next_op();
9304
9305
42.9k
  if (ce->parent_name) {
9306
    /* Lowercased parent name */
9307
13.1k
    zend_string *lc_parent_name = zend_string_tolower(ce->parent_name);
9308
13.1k
    opline->op2_type = IS_CONST;
9309
13.1k
    LITERAL_STR(opline->op2, lc_parent_name);
9310
13.1k
  }
9311
9312
42.9k
  opline->op1_type = IS_CONST;
9313
  /* It's possible that `lcname` is not an interned string because it was not yet in the interned string table.
9314
   * However, by this point another thread may have caused `lcname` to be added in the interned string table.
9315
   * This will cause `lcname` to get freed once it is found in the interned string table. If we were to use
9316
   * LITERAL_STR() here we would not change the `lcname` pointer to the new value, and it would point to the
9317
   * now-freed string. This will cause issues when we use `lcname` in the code below. We solve this by using
9318
   * zend_add_literal_string() which gives us the new value. */
9319
42.9k
  opline->op1.constant = zend_add_literal_string(&lcname);
9320
9321
42.9k
  if (decl->flags & ZEND_ACC_ANON_CLASS) {
9322
2.28k
    opline->opcode = ZEND_DECLARE_ANON_CLASS;
9323
2.28k
    opline->extended_value = zend_alloc_cache_slot();
9324
2.28k
    zend_make_var_result(result, opline);
9325
2.28k
    if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) {
9326
      /* We checked above that the class name is not used. This really shouldn't happen. */
9327
0
      zend_error_noreturn(E_ERROR,
9328
0
        "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name));
9329
0
    }
9330
40.6k
  } else {
9331
    /* Generate RTD keys until we find one that isn't in use yet. */
9332
40.6k
    zend_string *key = NULL;
9333
40.6k
    do {
9334
40.6k
      zend_tmp_string_release(key);
9335
40.6k
      key = zend_build_runtime_definition_key(lcname, decl->start_lineno);
9336
40.6k
    } while (!zend_hash_add_ptr(CG(class_table), key, ce));
9337
9338
    /* RTD key is placed after lcname literal in op1 */
9339
40.6k
    zend_add_literal_string(&key);
9340
9341
40.6k
    opline->opcode = ZEND_DECLARE_CLASS;
9342
40.6k
    if (toplevel
9343
40.6k
       && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING)
9344
        /* We currently don't early-bind classes that implement interfaces or use traits */
9345
40.6k
       && !ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9346
40.6k
    ) {
9347
1.42k
      if (!extends_ast) {
9348
        /* Use empty string for classes without parents to avoid new handler, and special
9349
         * handling of zend_early_binding. */
9350
838
        opline->op2_type = IS_CONST;
9351
838
        LITERAL_STR(opline->op2, ZSTR_EMPTY_ALLOC());
9352
838
      }
9353
1.42k
      CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING;
9354
1.42k
      opline->opcode = ZEND_DECLARE_CLASS_DELAYED;
9355
1.42k
      opline->extended_value = zend_alloc_cache_slot();
9356
1.42k
      opline->result_type = IS_UNUSED;
9357
1.42k
      opline->result.opline_num = -1;
9358
1.42k
    }
9359
40.6k
  }
9360
42.9k
}
9361
/* }}} */
9362
9363
static void zend_compile_enum_case(zend_ast *ast)
9364
2.44k
{
9365
2.44k
  zend_class_entry *enum_class = CG(active_class_entry);
9366
2.44k
  if (!(enum_class->ce_flags & ZEND_ACC_ENUM)) {
9367
6
    zend_error_noreturn(E_COMPILE_ERROR, "Case can only be used in enums");
9368
6
  }
9369
9370
2.44k
  zend_string *enum_case_name = zval_make_interned_string(zend_ast_get_zval(ast->child[0]));
9371
2.44k
  zend_string *enum_class_name = enum_class->name;
9372
9373
2.44k
  zval class_name_zval;
9374
2.44k
  ZVAL_STR_COPY(&class_name_zval, enum_class_name);
9375
2.44k
  zend_ast *class_name_ast = zend_ast_create_zval(&class_name_zval);
9376
9377
2.44k
  zval case_name_zval;
9378
2.44k
  ZVAL_STR_COPY(&case_name_zval, enum_case_name);
9379
2.44k
  zend_ast *case_name_ast = zend_ast_create_zval(&case_name_zval);
9380
9381
2.44k
  zend_ast *case_value_ast = ast->child[1];
9382
  // Remove case_value_ast from the original AST to avoid freeing it, as it will be freed by zend_const_expr_to_zval
9383
2.44k
  ast->child[1] = NULL;
9384
2.44k
  if (enum_class->enum_backing_type != IS_UNDEF && case_value_ast == NULL) {
9385
6
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of backed enum %s must have a value",
9386
6
      ZSTR_VAL(enum_case_name),
9387
6
      ZSTR_VAL(enum_class_name));
9388
2.43k
  } else if (enum_class->enum_backing_type == IS_UNDEF && case_value_ast != NULL) {
9389
17
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-backed enum %s must not have a value",
9390
17
      ZSTR_VAL(enum_case_name),
9391
17
      ZSTR_VAL(enum_class_name));
9392
17
  }
9393
9394
2.41k
  zend_ast *const_enum_init_ast = zend_ast_create(ZEND_AST_CONST_ENUM_INIT, class_name_ast, case_name_ast, case_value_ast);
9395
9396
2.41k
  zval value_zv;
9397
2.41k
  zend_const_expr_to_zval(&value_zv, &const_enum_init_ast, /* allow_dynamic */ false);
9398
9399
  /* Doc comment has been appended as second last element in ZEND_AST_ENUM ast - attributes are conventionally last */
9400
2.41k
  zend_ast *doc_comment_ast = ast->child[2];
9401
2.41k
  zend_string *doc_comment = NULL;
9402
2.41k
  if (doc_comment_ast) {
9403
245
    doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9404
245
  }
9405
9406
2.41k
  zend_class_constant *c = zend_declare_class_constant_ex(enum_class, enum_case_name, &value_zv, ZEND_ACC_PUBLIC, doc_comment);
9407
2.41k
  ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE;
9408
2.41k
  zend_ast_destroy(const_enum_init_ast);
9409
9410
2.41k
  zend_ast *attr_ast = ast->child[3];
9411
2.41k
  if (attr_ast) {
9412
197
    zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9413
9414
197
    zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9415
9416
197
    if (deprecated) {
9417
24
      ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9418
24
    }
9419
197
  }
9420
2.41k
}
9421
9422
static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
9423
2.18k
{
9424
2.18k
  switch (type) {
9425
1.31k
    case ZEND_SYMBOL_CLASS:
9426
1.31k
      if (!FC(imports)) {
9427
979
        FC(imports) = emalloc(sizeof(HashTable));
9428
979
        zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
9429
979
      }
9430
1.31k
      return FC(imports);
9431
638
    case ZEND_SYMBOL_FUNCTION:
9432
638
      if (!FC(imports_function)) {
9433
525
        FC(imports_function) = emalloc(sizeof(HashTable));
9434
525
        zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
9435
525
      }
9436
638
      return FC(imports_function);
9437
232
    case ZEND_SYMBOL_CONST:
9438
232
      if (!FC(imports_const)) {
9439
154
        FC(imports_const) = emalloc(sizeof(HashTable));
9440
154
        zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
9441
154
      }
9442
232
      return FC(imports_const);
9443
2.18k
    EMPTY_SWITCH_DEFAULT_CASE()
9444
2.18k
  }
9445
9446
0
  return NULL;
9447
2.18k
}
9448
/* }}} */
9449
9450
static char *zend_get_use_type_str(uint32_t type) /* {{{ */
9451
90
{
9452
90
  switch (type) {
9453
52
    case ZEND_SYMBOL_CLASS:
9454
52
      return "";
9455
24
    case ZEND_SYMBOL_FUNCTION:
9456
24
      return " function";
9457
14
    case ZEND_SYMBOL_CONST:
9458
14
      return " const";
9459
90
    EMPTY_SWITCH_DEFAULT_CASE()
9460
90
  }
9461
9462
0
  return " unknown";
9463
90
}
9464
/* }}} */
9465
9466
static void zend_check_already_in_use(uint32_t type, zend_string *old_name, zend_string *new_name, zend_string *check_name) /* {{{ */
9467
59
{
9468
59
  if (zend_string_equals_ci(old_name, check_name)) {
9469
25
    return;
9470
25
  }
9471
9472
34
  zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9473
34
    "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9474
59
}
9475
/* }}} */
9476
9477
static void zend_compile_use(zend_ast *ast) /* {{{ */
9478
2.18k
{
9479
2.18k
  zend_ast_list *list = zend_ast_get_list(ast);
9480
2.18k
  uint32_t i;
9481
2.18k
  zend_string *current_ns = FC(current_namespace);
9482
2.18k
  uint32_t type = ast->attr;
9483
2.18k
  HashTable *current_import = zend_get_import_ht(type);
9484
2.18k
  bool case_sensitive = type == ZEND_SYMBOL_CONST;
9485
9486
4.44k
  for (i = 0; i < list->children; ++i) {
9487
2.35k
    zend_ast *use_ast = list->child[i];
9488
2.35k
    zend_ast *old_name_ast = use_ast->child[0];
9489
2.35k
    zend_ast *new_name_ast = use_ast->child[1];
9490
2.35k
    zend_string *old_name = zend_ast_get_str(old_name_ast);
9491
2.35k
    zend_string *new_name, *lookup_name;
9492
9493
2.35k
    if (new_name_ast) {
9494
395
      new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
9495
1.95k
    } else {
9496
1.95k
      const char *unqualified_name;
9497
1.95k
      size_t unqualified_name_len;
9498
1.95k
      if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
9499
        /* The form "use A\B" is equivalent to "use A\B as B" */
9500
792
        new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
9501
1.16k
      } else {
9502
1.16k
        new_name = zend_string_copy(old_name);
9503
9504
1.16k
        if (!current_ns) {
9505
569
          zend_error(E_WARNING, "The use statement with non-compound name '%s' "
9506
569
            "has no effect", ZSTR_VAL(new_name));
9507
569
        }
9508
1.16k
      }
9509
1.95k
    }
9510
9511
2.35k
    if (case_sensitive) {
9512
250
      lookup_name = zend_string_copy(new_name);
9513
2.10k
    } else {
9514
2.10k
      lookup_name = zend_string_tolower(new_name);
9515
2.10k
    }
9516
9517
2.35k
    if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) {
9518
26
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
9519
26
        "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
9520
26
    }
9521
9522
2.32k
    if (current_ns) {
9523
1.13k
      zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
9524
1.13k
      zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
9525
1.13k
      ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
9526
1.13k
      memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1);
9527
9528
1.13k
      if (zend_have_seen_symbol(ns_name, type)) {
9529
31
        zend_check_already_in_use(type, old_name, new_name, ns_name);
9530
31
      }
9531
9532
1.13k
      zend_string_efree(ns_name);
9533
1.18k
    } else if (zend_have_seen_symbol(lookup_name, type)) {
9534
28
      zend_check_already_in_use(type, old_name, new_name, lookup_name);
9535
28
    }
9536
9537
2.32k
    zend_string_addref(old_name);
9538
2.32k
    old_name = zend_new_interned_string(old_name);
9539
2.32k
    if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
9540
56
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9541
56
        "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9542
56
    }
9543
9544
2.26k
    zend_string_release_ex(lookup_name, 0);
9545
2.26k
    zend_string_release_ex(new_name, 0);
9546
2.26k
  }
9547
2.18k
}
9548
/* }}} */
9549
9550
static void zend_compile_group_use(zend_ast *ast) /* {{{ */
9551
187
{
9552
187
  uint32_t i;
9553
187
  zend_string *ns = zend_ast_get_str(ast->child[0]);
9554
187
  zend_ast_list *list = zend_ast_get_list(ast->child[1]);
9555
9556
702
  for (i = 0; i < list->children; i++) {
9557
515
    zend_ast *inline_use, *use = list->child[i];
9558
515
    zval *name_zval = zend_ast_get_zval(use->child[0]);
9559
515
    zend_string *name = Z_STR_P(name_zval);
9560
515
    zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
9561
515
    zend_string_release_ex(name, 0);
9562
515
    ZVAL_STR(name_zval, compound_ns);
9563
515
    inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
9564
515
    inline_use->attr = ast->attr ? ast->attr : use->attr;
9565
515
    zend_compile_use(inline_use);
9566
515
  }
9567
187
}
9568
/* }}} */
9569
9570
static void zend_compile_const_decl(zend_ast *ast) /* {{{ */
9571
4.93k
{
9572
4.93k
  zend_ast_list *list = zend_ast_get_list(ast);
9573
4.93k
  uint32_t i;
9574
4.93k
  zend_ast *attributes_ast = NULL;
9575
4.93k
  zend_op *last_op = NULL;
9576
10.3k
  for (i = 0; i < list->children; ++i) {
9577
5.39k
    zend_ast *const_ast = list->child[i];
9578
5.39k
    if (const_ast->kind == ZEND_AST_ATTRIBUTE_LIST) {
9579
391
      ZEND_ASSERT(i == list->children - 1);
9580
391
      attributes_ast = const_ast;
9581
391
      continue;
9582
391
    }
9583
5.00k
    ZEND_ASSERT(const_ast->kind == ZEND_AST_CONST_ELEM);
9584
5.00k
    zend_ast *name_ast = const_ast->child[0];
9585
5.00k
    zend_ast **value_ast_ptr = &const_ast->child[1];
9586
5.00k
    zend_string *unqualified_name = zend_ast_get_str(name_ast);
9587
9588
5.00k
    zend_string *name;
9589
5.00k
    znode name_node, value_node;
9590
5.00k
    zval *value_zv = &value_node.u.constant;
9591
9592
5.00k
    value_node.op_type = IS_CONST;
9593
5.00k
    zend_const_expr_to_zval(value_zv, value_ast_ptr, /* allow_dynamic */ true);
9594
9595
5.00k
    if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) {
9596
6
      zend_error_noreturn(E_COMPILE_ERROR,
9597
6
        "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name));
9598
6
    }
9599
9600
4.99k
    name = zend_prefix_with_ns(unqualified_name);
9601
4.99k
    name = zend_new_interned_string(name);
9602
9603
4.99k
    if (FC(imports_const)) {
9604
66
      zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name);
9605
66
      if (import_name && !zend_string_equals(import_name, name)) {
9606
7
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because "
9607
7
          "the name is already in use", ZSTR_VAL(name));
9608
7
      }
9609
66
    }
9610
9611
4.98k
    name_node.op_type = IS_CONST;
9612
4.98k
    ZVAL_STR(&name_node.u.constant, name);
9613
9614
4.98k
    last_op = zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
9615
9616
4.98k
    zend_register_seen_symbol(name, ZEND_SYMBOL_CONST);
9617
4.98k
  }
9618
4.91k
  if (attributes_ast == NULL) {
9619
4.43k
    return;
9620
4.43k
  }
9621
  /* Validate: attributes can only be applied to one constant at a time
9622
   * Since we store the AST for the attributes in the list of children,
9623
   * there should be exactly 2 children. */
9624
481
  if (list->children > 2) {
9625
6
    zend_error_noreturn(
9626
6
      E_COMPILE_ERROR,
9627
6
      "Cannot apply attributes to multiple constants at once"
9628
6
    );
9629
6
  }
9630
9631
475
  HashTable *attributes = NULL;
9632
475
  zend_compile_attributes(&attributes, list->child[1], 0, ZEND_ATTRIBUTE_TARGET_CONST, 0);
9633
9634
475
  ZEND_ASSERT(last_op != NULL);
9635
375
  last_op->opcode = ZEND_DECLARE_ATTRIBUTED_CONST;
9636
375
  znode attribs_node;
9637
375
  attribs_node.op_type = IS_CONST;
9638
375
  ZVAL_PTR(&attribs_node.u.constant, attributes);
9639
375
  zend_emit_op_data(&attribs_node);
9640
375
  CG(active_op_array)->fn_flags |= ZEND_ACC_PTR_OPS;
9641
375
}
9642
/* }}}*/
9643
9644
static void zend_compile_namespace(zend_ast *ast) /* {{{ */
9645
5.22k
{
9646
5.22k
  zend_ast *name_ast = ast->child[0];
9647
5.22k
  zend_ast *stmt_ast = ast->child[1];
9648
5.22k
  zend_string *name;
9649
5.22k
  bool with_bracket = stmt_ast != NULL;
9650
9651
  /* handle mixed syntax declaration or nested namespaces */
9652
5.22k
  if (!FC(has_bracketed_namespaces)) {
9653
3.86k
    if (FC(current_namespace)) {
9654
      /* previous namespace declarations were unbracketed */
9655
1.08k
      if (with_bracket) {
9656
6
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
9657
6
          "with unbracketed namespace declarations");
9658
6
      }
9659
1.08k
    }
9660
3.86k
  } else {
9661
    /* previous namespace declarations were bracketed */
9662
1.35k
    if (!with_bracket) {
9663
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
9664
7
        "with unbracketed namespace declarations");
9665
1.34k
    } else if (FC(current_namespace) || FC(in_namespace)) {
9666
7
      zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
9667
7
    }
9668
1.35k
  }
9669
9670
5.20k
  bool is_first_namespace = (!with_bracket && !FC(current_namespace))
9671
5.20k
    || (with_bracket && !FC(has_bracketed_namespaces));
9672
5.20k
  if (is_first_namespace && FAILURE == zend_is_first_statement(ast, /* allow_nop */ 1)) {
9673
28
    zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
9674
28
      "the very first statement or after any declare call in the script");
9675
28
  }
9676
9677
5.17k
  if (FC(current_namespace)) {
9678
1.07k
    zend_string_release_ex(FC(current_namespace), 0);
9679
1.07k
  }
9680
9681
5.17k
  if (name_ast) {
9682
4.52k
    name = zend_ast_get_str(name_ast);
9683
9684
4.52k
    if (zend_string_equals_literal_ci(name, "namespace")) {
9685
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name));
9686
6
    }
9687
9688
4.51k
    FC(current_namespace) = zend_string_copy(name);
9689
4.51k
  } else {
9690
647
    FC(current_namespace) = NULL;
9691
647
  }
9692
9693
5.16k
  zend_reset_import_tables();
9694
9695
5.16k
  FC(in_namespace) = 1;
9696
5.16k
  if (with_bracket) {
9697
1.86k
    FC(has_bracketed_namespaces) = 1;
9698
1.86k
  }
9699
9700
5.16k
  if (stmt_ast) {
9701
1.86k
    zend_compile_top_stmt(stmt_ast);
9702
1.86k
    zend_end_namespace();
9703
1.86k
  }
9704
5.16k
}
9705
/* }}} */
9706
9707
static void zend_compile_halt_compiler(zend_ast *ast) /* {{{ */
9708
64
{
9709
64
  zend_ast *offset_ast = ast->child[0];
9710
64
  zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
9711
9712
64
  zend_string *filename, *name;
9713
64
  const char const_name[] = "__COMPILER_HALT_OFFSET__";
9714
9715
64
  if (FC(has_bracketed_namespaces) && FC(in_namespace)) {
9716
0
    zend_error_noreturn(E_COMPILE_ERROR,
9717
0
      "__HALT_COMPILER() can only be used from the outermost scope");
9718
0
  }
9719
9720
64
  filename = zend_get_compiled_filename();
9721
64
  name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
9722
64
    ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
9723
9724
64
  zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0);
9725
64
  zend_string_release_ex(name, 0);
9726
64
}
9727
/* }}} */
9728
9729
static bool zend_try_ct_eval_magic_const(zval *zv, zend_ast *ast) /* {{{ */
9730
17.6k
{
9731
17.6k
  zend_op_array *op_array = CG(active_op_array);
9732
17.6k
  zend_class_entry *ce = CG(active_class_entry);
9733
9734
17.6k
  switch (ast->attr) {
9735
265
    case T_LINE:
9736
265
      ZVAL_LONG(zv, ast->lineno);
9737
265
      break;
9738
4.80k
    case T_FILE:
9739
4.80k
      ZVAL_STR_COPY(zv, CG(compiled_filename));
9740
4.80k
      break;
9741
1.77k
    case T_DIR:
9742
1.77k
    {
9743
1.77k
      zend_string *filename = CG(compiled_filename);
9744
1.77k
      zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
9745
#ifdef ZEND_WIN32
9746
      ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
9747
#else
9748
1.77k
      ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
9749
1.77k
#endif
9750
9751
1.77k
      if (zend_string_equals_literal(dirname, ".")) {
9752
1.26k
        dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
9753
1.26k
#ifdef HAVE_GETCWD
9754
1.26k
        ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN));
9755
#elif defined(HAVE_GETWD)
9756
        ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname)));
9757
#endif
9758
1.26k
        ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname));
9759
1.26k
      }
9760
9761
1.77k
      ZVAL_STR(zv, dirname);
9762
1.77k
      break;
9763
0
    }
9764
1.38k
    case T_FUNC_C:
9765
1.38k
      if (op_array && op_array->function_name) {
9766
1.15k
        ZVAL_STR_COPY(zv, op_array->function_name);
9767
1.15k
      } else {
9768
224
        ZVAL_EMPTY_STRING(zv);
9769
224
      }
9770
1.38k
      break;
9771
330
    case T_PROPERTY_C: {
9772
330
      zend_string *prop_info_name = CG(context).active_property_info_name;
9773
330
      if (prop_info_name) {
9774
242
        ZVAL_STR(zv, zend_copy_unmangled_prop_name(prop_info_name));
9775
242
      } else {
9776
88
        ZVAL_EMPTY_STRING(zv);
9777
88
      }
9778
330
      break;
9779
0
    }
9780
4.34k
    case T_METHOD_C:
9781
      /* Detect whether we are directly inside a class (e.g. a class constant) and treat
9782
       * this as not being inside a function. */
9783
4.34k
      if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
9784
231
        op_array = NULL;
9785
231
      }
9786
4.34k
      if (op_array && op_array->function_name) {
9787
3.85k
        if (op_array->scope) {
9788
3.32k
          ZVAL_NEW_STR(zv,
9789
3.32k
            zend_create_member_string(op_array->scope->name, op_array->function_name));
9790
3.32k
        } else {
9791
529
          ZVAL_STR_COPY(zv, op_array->function_name);
9792
529
        }
9793
3.85k
      } else {
9794
485
        ZVAL_EMPTY_STRING(zv);
9795
485
      }
9796
4.34k
      break;
9797
2.07k
    case T_CLASS_C:
9798
2.07k
      if (ce) {
9799
1.27k
        if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
9800
793
          return 0;
9801
793
        } else {
9802
485
          ZVAL_STR_COPY(zv, ce->name);
9803
485
        }
9804
1.27k
      } else {
9805
801
        ZVAL_EMPTY_STRING(zv);
9806
801
      }
9807
1.28k
      break;
9808
2.03k
    case T_TRAIT_C:
9809
2.03k
      if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
9810
199
        ZVAL_STR_COPY(zv, ce->name);
9811
1.83k
      } else {
9812
1.83k
        ZVAL_EMPTY_STRING(zv);
9813
1.83k
      }
9814
2.03k
      break;
9815
678
    case T_NS_C:
9816
678
      if (FC(current_namespace)) {
9817
399
        ZVAL_STR_COPY(zv, FC(current_namespace));
9818
399
      } else {
9819
279
        ZVAL_EMPTY_STRING(zv);
9820
279
      }
9821
678
      break;
9822
17.6k
    EMPTY_SWITCH_DEFAULT_CASE()
9823
17.6k
  }
9824
9825
16.8k
  return 1;
9826
17.6k
}
9827
/* }}} */
9828
9829
ZEND_API bool zend_is_op_long_compatible(const zval *op)
9830
68.1k
{
9831
68.1k
  if (Z_TYPE_P(op) == IS_ARRAY) {
9832
453
    return false;
9833
453
  }
9834
9835
67.7k
  if (Z_TYPE_P(op) == IS_DOUBLE
9836
67.7k
    && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval(Z_DVAL_P(op)))) {
9837
15.0k
    return false;
9838
15.0k
  }
9839
9840
52.6k
  if (Z_TYPE_P(op) == IS_STRING) {
9841
6.83k
    double dval = 0;
9842
6.83k
    uint8_t is_num = is_numeric_str_function(Z_STR_P(op), NULL, &dval);
9843
6.83k
    if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval(dval)))) {
9844
3.51k
      return false;
9845
3.51k
    }
9846
6.83k
  }
9847
9848
49.1k
  return true;
9849
52.6k
}
9850
9851
ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, const zval *op2) /* {{{ */
9852
270k
{
9853
270k
  if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) {
9854
    /* Array to string warning. */
9855
34.7k
    return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY;
9856
34.7k
  }
9857
9858
235k
  if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV
9859
235k
               || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR
9860
235k
               || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) {
9861
    /* Only the numeric operations throw errors. */
9862
24.9k
    return 0;
9863
24.9k
  }
9864
9865
210k
  if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) {
9866
8.36k
    if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) {
9867
      /* Adding two arrays is allowed. */
9868
5.38k
      return 0;
9869
5.38k
    }
9870
9871
    /* Numeric operators throw when one of the operands is an array. */
9872
2.98k
    return 1;
9873
8.36k
  }
9874
9875
  /* While basic arithmetic operators always produce numeric string errors,
9876
   * bitwise operators don't produce errors if both operands are strings */
9877
202k
  if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)
9878
202k
    && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
9879
30.3k
    return 0;
9880
30.3k
  }
9881
9882
171k
  if (Z_TYPE_P(op1) == IS_STRING
9883
171k
    && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) {
9884
38.1k
    return 1;
9885
38.1k
  }
9886
9887
133k
  if (Z_TYPE_P(op2) == IS_STRING
9888
133k
    && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) {
9889
6.05k
    return 1;
9890
6.05k
  }
9891
9892
127k
  if ((opcode == ZEND_MOD && zval_get_long(op2) == 0)
9893
127k
      || (opcode == ZEND_DIV && zval_get_double(op2) == 0.0)) {
9894
    /* Division by zero throws an error. */
9895
2.89k
    return 1;
9896
2.89k
  }
9897
124k
  if ((opcode == ZEND_POW) && zval_get_double(op1) == 0 && zval_get_double(op2) < 0) {
9898
    /* 0 ** (<0) throws a division by zero error. */
9899
375
    return 1;
9900
375
  }
9901
124k
  if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) {
9902
    /* Shift by negative number throws an error. */
9903
984
    return 1;
9904
984
  }
9905
9906
  /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */
9907
123k
  if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR
9908
123k
      || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR || opcode == ZEND_MOD) {
9909
35.3k
    return !zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2);
9910
35.3k
  }
9911
9912
88.1k
  return 0;
9913
123k
}
9914
/* }}} */
9915
9916
static inline bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
9917
210k
{
9918
210k
  if (zend_binary_op_produces_error(opcode, op1, op2)) {
9919
37.3k
    return 0;
9920
37.3k
  }
9921
9922
173k
  binary_op_type fn = get_binary_op(opcode);
9923
173k
  fn(result, op1, op2);
9924
173k
  return 1;
9925
210k
}
9926
/* }}} */
9927
9928
ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op)
9929
30.9k
{
9930
30.9k
  if (opcode == ZEND_BW_NOT) {
9931
    /* BW_NOT on string does not convert the string into an integer. */
9932
10.3k
    if (Z_TYPE_P(op) == IS_STRING) {
9933
5.08k
      return 0;
9934
5.08k
    }
9935
5.22k
    return Z_TYPE_P(op) <= IS_TRUE || !zend_is_op_long_compatible(op);
9936
10.3k
  }
9937
9938
20.6k
  return 0;
9939
30.9k
}
9940
9941
static inline bool zend_try_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */
9942
29.7k
{
9943
29.7k
  if (zend_unary_op_produces_error(opcode, op)) {
9944
1.34k
    return 0;
9945
1.34k
  }
9946
9947
28.3k
  unary_op_type fn = get_unary_op(opcode);
9948
28.3k
  fn(result, op);
9949
28.3k
  return 1;
9950
29.7k
}
9951
/* }}} */
9952
9953
static inline bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
9954
43.2k
{
9955
43.2k
  zval right;
9956
43.2k
  ZVAL_LONG(&right, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
9957
43.2k
  return zend_try_ct_eval_binary_op(result, ZEND_MUL, op, &right);
9958
43.2k
}
9959
/* }}} */
9960
9961
static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
9962
10.0k
{
9963
10.0k
  binary_op_type fn = kind == ZEND_AST_GREATER
9964
10.0k
    ? is_smaller_function : is_smaller_or_equal_function;
9965
10.0k
  fn(result, op2, op1);
9966
10.0k
}
9967
/* }}} */
9968
9969
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
9970
3.85M
{
9971
3.85M
  zend_ast_list *list = zend_ast_get_list(ast);
9972
3.85M
  zend_ast *last_elem_ast = NULL;
9973
3.85M
  uint32_t i;
9974
3.85M
  bool is_constant = 1;
9975
9976
3.85M
  if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
9977
6
    zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
9978
6
  }
9979
9980
  /* First ensure that *all* child nodes are constant and by-val */
9981
8.20M
  for (i = 0; i < list->children; ++i) {
9982
4.34M
    zend_ast *elem_ast = list->child[i];
9983
9984
4.34M
    if (elem_ast == NULL) {
9985
      /* Report error at line of last non-empty element */
9986
138
      if (last_elem_ast) {
9987
60
        CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast);
9988
60
      }
9989
138
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
9990
138
    }
9991
9992
4.34M
    if (elem_ast->kind != ZEND_AST_UNPACK) {
9993
4.33M
      zend_eval_const_expr(&elem_ast->child[0]);
9994
4.33M
      zend_eval_const_expr(&elem_ast->child[1]);
9995
9996
4.33M
      if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL
9997
4.33M
        || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
9998
4.33M
      ) {
9999
3.83M
        is_constant = 0;
10000
3.83M
      }
10001
4.33M
    } else {
10002
6.08k
      zend_eval_const_expr(&elem_ast->child[0]);
10003
10004
6.08k
      if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) {
10005
5.19k
        is_constant = 0;
10006
5.19k
      }
10007
6.08k
    }
10008
10009
4.34M
    last_elem_ast = elem_ast;
10010
4.34M
  }
10011
10012
3.85M
  if (!is_constant) {
10013
3.76M
    return 0;
10014
3.76M
  }
10015
10016
90.0k
  if (!list->children) {
10017
14.8k
    ZVAL_EMPTY_ARRAY(result);
10018
14.8k
    return 1;
10019
14.8k
  }
10020
10021
75.2k
  array_init_size(result, list->children);
10022
476k
  for (i = 0; i < list->children; ++i) {
10023
402k
    zend_ast *elem_ast = list->child[i];
10024
402k
    zend_ast *value_ast = elem_ast->child[0];
10025
402k
    zend_ast *key_ast;
10026
10027
402k
    zval *value = zend_ast_get_zval(value_ast);
10028
402k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
10029
691
      if (Z_TYPE_P(value) == IS_ARRAY) {
10030
683
        HashTable *ht = Z_ARRVAL_P(value);
10031
683
        zval *val;
10032
683
        zend_string *key;
10033
10034
14.1k
        ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
10035
14.1k
          if (key) {
10036
496
            zend_hash_update(Z_ARRVAL_P(result), key, val);
10037
5.91k
          } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
10038
12
            zval_ptr_dtor(result);
10039
12
            return 0;
10040
12
          }
10041
6.40k
          Z_TRY_ADDREF_P(val);
10042
6.40k
        } ZEND_HASH_FOREACH_END();
10043
10044
671
        continue;
10045
683
      } else {
10046
8
        zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked, %s given", zend_zval_value_name(value));
10047
8
      }
10048
691
    }
10049
10050
401k
    Z_TRY_ADDREF_P(value);
10051
10052
401k
    key_ast = elem_ast->child[1];
10053
401k
    if (key_ast) {
10054
13.3k
      zval *key = zend_ast_get_zval(key_ast);
10055
13.3k
      switch (Z_TYPE_P(key)) {
10056
6.01k
        case IS_LONG:
10057
6.01k
          zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value);
10058
6.01k
          break;
10059
6.12k
        case IS_STRING:
10060
6.12k
          zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value);
10061
6.12k
          break;
10062
643
        case IS_DOUBLE: {
10063
643
          zend_long lval = zend_dval_to_lval(Z_DVAL_P(key));
10064
          /* Incompatible float will generate an error, leave this to run-time */
10065
643
          if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) {
10066
297
            zval_ptr_dtor_nogc(value);
10067
297
            zval_ptr_dtor(result);
10068
297
            return 0;
10069
297
          }
10070
346
          zend_hash_index_update(Z_ARRVAL_P(result), lval, value);
10071
346
          break;
10072
643
        }
10073
250
        case IS_FALSE:
10074
250
          zend_hash_index_update(Z_ARRVAL_P(result), 0, value);
10075
250
          break;
10076
322
        case IS_TRUE:
10077
322
          zend_hash_index_update(Z_ARRVAL_P(result), 1, value);
10078
322
          break;
10079
27
        case IS_NULL:
10080
27
          zend_hash_update(Z_ARRVAL_P(result), ZSTR_EMPTY_ALLOC(), value);
10081
27
          break;
10082
4
        default:
10083
4
          zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type");
10084
0
          break;
10085
13.3k
      }
10086
387k
    } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) {
10087
105
      zval_ptr_dtor_nogc(value);
10088
105
      zval_ptr_dtor(result);
10089
105
      return 0;
10090
105
    }
10091
401k
  }
10092
10093
74.8k
  return 1;
10094
75.2k
}
10095
/* }}} */
10096
10097
static void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */
10098
479k
{
10099
479k
  zend_ast *left_ast = ast->child[0];
10100
479k
  zend_ast *right_ast = ast->child[1];
10101
479k
  uint32_t opcode = ast->attr;
10102
10103
479k
  znode left_node, right_node;
10104
10105
479k
  zend_compile_expr(&left_node, left_ast);
10106
479k
  zend_compile_expr(&right_node, right_ast);
10107
10108
479k
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10109
126k
    if (zend_try_ct_eval_binary_op(&result->u.constant, opcode,
10110
126k
        &left_node.u.constant, &right_node.u.constant)
10111
126k
    ) {
10112
112k
      result->op_type = IS_CONST;
10113
112k
      zval_ptr_dtor(&left_node.u.constant);
10114
112k
      zval_ptr_dtor(&right_node.u.constant);
10115
112k
      return;
10116
112k
    }
10117
126k
  }
10118
10119
367k
  do {
10120
367k
    if (opcode == ZEND_IS_EQUAL || opcode == ZEND_IS_NOT_EQUAL) {
10121
74.5k
      if (left_node.op_type == IS_CONST) {
10122
623
        if (Z_TYPE(left_node.u.constant) == IS_FALSE) {
10123
166
          opcode = (opcode == ZEND_IS_NOT_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
10124
166
          zend_emit_op_tmp(result, opcode, &right_node, NULL);
10125
166
          break;
10126
457
        } else if (Z_TYPE(left_node.u.constant) == IS_TRUE) {
10127
138
          opcode = (opcode == ZEND_IS_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
10128
138
          zend_emit_op_tmp(result, opcode, &right_node, NULL);
10129
138
          break;
10130
138
        }
10131
73.9k
      } else if (right_node.op_type == IS_CONST) {
10132
16.4k
        if (Z_TYPE(right_node.u.constant) == IS_FALSE) {
10133
1.04k
          opcode = (opcode == ZEND_IS_NOT_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
10134
1.04k
          zend_emit_op_tmp(result, opcode, &left_node, NULL);
10135
1.04k
          break;
10136
15.3k
        } else if (Z_TYPE(right_node.u.constant) == IS_TRUE) {
10137
180
          opcode = (opcode == ZEND_IS_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
10138
180
          zend_emit_op_tmp(result, opcode, &left_node, NULL);
10139
180
          break;
10140
180
        }
10141
16.4k
      }
10142
292k
    } else if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) {
10143
      /* convert $x === null to is_null($x) (i.e. ZEND_TYPE_CHECK opcode). Do the same thing for false/true. (covers IS_NULL, IS_FALSE, and IS_TRUE) */
10144
3.70k
      if (left_node.op_type == IS_CONST) {
10145
484
        if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) {
10146
180
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL);
10147
180
          opline->extended_value =
10148
180
            (opcode == ZEND_IS_IDENTICAL) ?
10149
54
              (1 << Z_TYPE(left_node.u.constant)) :
10150
180
              (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant)));
10151
180
          return;
10152
180
        }
10153
3.21k
      } else if (right_node.op_type == IS_CONST) {
10154
1.76k
        if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) {
10155
425
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL);
10156
425
          opline->extended_value =
10157
425
            (opcode == ZEND_IS_IDENTICAL) ?
10158
151
              (1 << Z_TYPE(right_node.u.constant)) :
10159
425
              (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant)));
10160
425
          return;
10161
425
        }
10162
1.76k
      }
10163
289k
    } else if (opcode == ZEND_CONCAT) {
10164
      /* convert constant operands to strings at compile-time */
10165
77.6k
      if (left_node.op_type == IS_CONST) {
10166
15.9k
        if (Z_TYPE(left_node.u.constant) == IS_ARRAY) {
10167
217
          zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING;
10168
15.6k
        } else {
10169
15.6k
          convert_to_string(&left_node.u.constant);
10170
15.6k
        }
10171
15.9k
      }
10172
77.6k
      if (right_node.op_type == IS_CONST) {
10173
23.7k
        if (Z_TYPE(right_node.u.constant) == IS_ARRAY) {
10174
303
          zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING;
10175
23.4k
        } else {
10176
23.4k
          convert_to_string(&right_node.u.constant);
10177
23.4k
        }
10178
23.7k
      }
10179
77.6k
      if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10180
0
        opcode = ZEND_FAST_CONCAT;
10181
0
      }
10182
77.6k
    }
10183
365k
    zend_emit_op_tmp(result, opcode, &left_node, &right_node);
10184
365k
  } while (0);
10185
367k
}
10186
/* }}} */
10187
10188
/* We do not use zend_compile_binary_op for this because we want to retain the left-to-right
10189
 * evaluation order. */
10190
static void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */
10191
40.3k
{
10192
40.3k
  zend_ast *left_ast = ast->child[0];
10193
40.3k
  zend_ast *right_ast = ast->child[1];
10194
40.3k
  znode left_node, right_node;
10195
10196
40.3k
  ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL);
10197
10198
40.3k
  zend_compile_expr(&left_node, left_ast);
10199
40.3k
  zend_compile_expr(&right_node, right_ast);
10200
10201
40.3k
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10202
7.95k
    result->op_type = IS_CONST;
10203
7.95k
    zend_ct_eval_greater(&result->u.constant, ast->kind,
10204
7.95k
      &left_node.u.constant, &right_node.u.constant);
10205
7.95k
    zval_ptr_dtor(&left_node.u.constant);
10206
7.95k
    zval_ptr_dtor(&right_node.u.constant);
10207
7.95k
    return;
10208
7.95k
  }
10209
10210
32.4k
  zend_emit_op_tmp(result,
10211
32.4k
    ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL,
10212
32.4k
    &right_node, &left_node);
10213
32.4k
}
10214
/* }}} */
10215
10216
static void zend_compile_unary_op(znode *result, zend_ast *ast) /* {{{ */
10217
138k
{
10218
138k
  zend_ast *expr_ast = ast->child[0];
10219
138k
  uint32_t opcode = ast->attr;
10220
10221
138k
  znode expr_node;
10222
138k
  zend_compile_expr(&expr_node, expr_ast);
10223
10224
138k
  if (expr_node.op_type == IS_CONST
10225
138k
      && zend_try_ct_eval_unary_op(&result->u.constant, opcode, &expr_node.u.constant)) {
10226
22.1k
    result->op_type = IS_CONST;
10227
22.1k
    zval_ptr_dtor(&expr_node.u.constant);
10228
22.1k
    return;
10229
22.1k
  }
10230
10231
115k
  zend_emit_op_tmp(result, opcode, &expr_node, NULL);
10232
115k
}
10233
/* }}} */
10234
10235
static void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */
10236
41.9k
{
10237
41.9k
  zend_ast *expr_ast = ast->child[0];
10238
41.9k
  znode expr_node, right_node;
10239
10240
41.9k
  ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS);
10241
10242
41.9k
  zend_compile_expr(&expr_node, expr_ast);
10243
10244
41.9k
  if (expr_node.op_type == IS_CONST
10245
41.9k
    && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) {
10246
22.0k
    result->op_type = IS_CONST;
10247
22.0k
    zval_ptr_dtor(&expr_node.u.constant);
10248
22.0k
    return;
10249
22.0k
  }
10250
10251
19.9k
  right_node.op_type = IS_CONST;
10252
19.9k
  ZVAL_LONG(&right_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10253
19.9k
  zend_emit_op_tmp(result, ZEND_MUL, &expr_node, &right_node);
10254
19.9k
}
10255
/* }}} */
10256
10257
static void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */
10258
22.8k
{
10259
22.8k
  zend_ast *left_ast = ast->child[0];
10260
22.8k
  zend_ast *right_ast = ast->child[1];
10261
10262
22.8k
  znode left_node, right_node;
10263
22.8k
  zend_op *opline_jmpz, *opline_bool;
10264
22.8k
  uint32_t opnum_jmpz;
10265
10266
22.8k
  ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR);
10267
10268
22.8k
  zend_compile_expr(&left_node, left_ast);
10269
10270
22.8k
  if (left_node.op_type == IS_CONST) {
10271
3.63k
    if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant))
10272
3.63k
     || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) {
10273
2.57k
      result->op_type = IS_CONST;
10274
2.57k
      ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant));
10275
2.57k
    } else {
10276
1.05k
      zend_compile_expr(&right_node, right_ast);
10277
10278
1.05k
      if (right_node.op_type == IS_CONST) {
10279
544
        result->op_type = IS_CONST;
10280
544
        ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant));
10281
10282
544
        zval_ptr_dtor(&right_node.u.constant);
10283
544
      } else {
10284
515
        zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL);
10285
515
      }
10286
1.05k
    }
10287
10288
3.63k
    zval_ptr_dtor(&left_node.u.constant);
10289
3.63k
    return;
10290
3.63k
  }
10291
10292
19.1k
  opnum_jmpz = get_next_op_number();
10293
19.1k
  opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
10294
19.1k
    &left_node, NULL);
10295
10296
19.1k
  if (left_node.op_type == IS_TMP_VAR) {
10297
12.8k
    SET_NODE(opline_jmpz->result, &left_node);
10298
12.8k
    GET_NODE(result, opline_jmpz->result);
10299
12.8k
  } else {
10300
6.36k
    zend_make_tmp_result(result, opline_jmpz);
10301
6.36k
  }
10302
10303
19.1k
  zend_compile_expr(&right_node, right_ast);
10304
10305
19.1k
  opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
10306
19.1k
  SET_NODE(opline_bool->result, result);
10307
10308
19.1k
  zend_update_jump_target_to_next(opnum_jmpz);
10309
19.1k
}
10310
/* }}} */
10311
10312
static void zend_compile_post_incdec(znode *result, zend_ast *ast) /* {{{ */
10313
9.62k
{
10314
9.62k
  zend_ast *var_ast = ast->child[0];
10315
9.62k
  ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
10316
10317
9.62k
  zend_ensure_writable_variable(var_ast);
10318
10319
9.62k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10320
1.12k
    zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, 0);
10321
1.12k
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
10322
1.12k
    zend_make_tmp_result(result, opline);
10323
8.50k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10324
546
    zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, 0, 0);
10325
546
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP;
10326
546
    zend_make_tmp_result(result, opline);
10327
7.95k
  } else {
10328
7.95k
    znode var_node;
10329
7.95k
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, 0);
10330
7.95k
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10331
188
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10332
188
    }
10333
7.95k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC,
10334
7.95k
      &var_node, NULL);
10335
7.95k
  }
10336
9.62k
}
10337
/* }}} */
10338
10339
static void zend_compile_pre_incdec(znode *result, zend_ast *ast) /* {{{ */
10340
5.93k
{
10341
5.93k
  zend_ast *var_ast = ast->child[0];
10342
5.93k
  ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
10343
10344
5.93k
  zend_ensure_writable_variable(var_ast);
10345
10346
5.93k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10347
1.11k
    zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, 0);
10348
1.11k
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;
10349
1.11k
    opline->result_type = IS_TMP_VAR;
10350
1.11k
    result->op_type = IS_TMP_VAR;
10351
4.82k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10352
226
    zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, 0, 0);
10353
226
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP;
10354
226
    opline->result_type = IS_TMP_VAR;
10355
226
    result->op_type = IS_TMP_VAR;
10356
4.59k
  } else {
10357
4.59k
    znode var_node;
10358
4.59k
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, 0);
10359
4.59k
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10360
139
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10361
139
    }
10362
4.59k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC,
10363
4.59k
      &var_node, NULL);
10364
4.59k
  }
10365
5.93k
}
10366
/* }}} */
10367
10368
static void zend_compile_cast(znode *result, zend_ast *ast) /* {{{ */
10369
2.45k
{
10370
2.45k
  zend_ast *expr_ast = ast->child[0];
10371
2.45k
  znode expr_node;
10372
2.45k
  zend_op *opline;
10373
10374
2.45k
  zend_compile_expr(&expr_node, expr_ast);
10375
10376
2.45k
  if (ast->attr == _IS_BOOL) {
10377
47
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL);
10378
2.40k
  } else if (ast->attr == IS_NULL) {
10379
12
    zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
10380
2.39k
  } else {
10381
2.39k
    opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
10382
2.39k
    opline->extended_value = ast->attr;
10383
2.39k
  }
10384
2.45k
}
10385
/* }}} */
10386
10387
static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */
10388
2.53k
{
10389
2.53k
  zend_ast *cond_ast = ast->child[0];
10390
2.53k
  zend_ast *false_ast = ast->child[2];
10391
10392
2.53k
  znode cond_node, false_node;
10393
2.53k
  zend_op *opline_qm_assign;
10394
2.53k
  uint32_t opnum_jmp_set;
10395
10396
2.53k
  ZEND_ASSERT(ast->child[1] == NULL);
10397
10398
2.53k
  zend_compile_expr(&cond_node, cond_ast);
10399
10400
2.53k
  opnum_jmp_set = get_next_op_number();
10401
2.53k
  zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL);
10402
10403
2.53k
  zend_compile_expr(&false_node, false_ast);
10404
10405
2.53k
  opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10406
2.53k
  SET_NODE(opline_qm_assign->result, result);
10407
10408
2.53k
  zend_update_jump_target_to_next(opnum_jmp_set);
10409
2.53k
}
10410
/* }}} */
10411
10412
static void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
10413
9.02k
{
10414
9.02k
  zend_ast *cond_ast = ast->child[0];
10415
9.02k
  zend_ast *true_ast = ast->child[1];
10416
9.02k
  zend_ast *false_ast = ast->child[2];
10417
10418
9.02k
  znode cond_node, true_node, false_node;
10419
9.02k
  zend_op *opline_qm_assign2;
10420
9.02k
  uint32_t opnum_jmpz, opnum_jmp;
10421
10422
9.02k
  if (cond_ast->kind == ZEND_AST_CONDITIONAL
10423
9.02k
      && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) {
10424
594
    if (cond_ast->child[1]) {
10425
36
      if (true_ast) {
10426
18
        zend_error(E_COMPILE_ERROR,
10427
18
          "Unparenthesized `a ? b : c ? d : e` is not supported. "
10428
18
          "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`");
10429
18
      } else {
10430
18
        zend_error(E_COMPILE_ERROR,
10431
18
          "Unparenthesized `a ? b : c ?: d` is not supported. "
10432
18
          "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`");
10433
18
      }
10434
558
    } else {
10435
558
      if (true_ast) {
10436
10
        zend_error(E_COMPILE_ERROR,
10437
10
          "Unparenthesized `a ?: b ? c : d` is not supported. "
10438
10
          "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`");
10439
548
      } else {
10440
        /* This case is harmless:  (a ?: b) ?: c always produces the same result
10441
         * as a ?: (b ?: c). */
10442
548
      }
10443
558
    }
10444
594
  }
10445
10446
9.02k
  if (!true_ast) {
10447
2.53k
    zend_compile_shorthand_conditional(result, ast);
10448
2.53k
    return;
10449
2.53k
  }
10450
10451
6.48k
  zend_compile_expr(&cond_node, cond_ast);
10452
10453
6.48k
  opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
10454
10455
6.48k
  zend_compile_expr(&true_node, true_ast);
10456
10457
6.48k
  zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL);
10458
10459
6.48k
  opnum_jmp = zend_emit_jump(0);
10460
10461
6.48k
  zend_update_jump_target_to_next(opnum_jmpz);
10462
10463
6.48k
  zend_compile_expr(&false_node, false_ast);
10464
10465
6.48k
  opline_qm_assign2 = zend_emit_op(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10466
6.48k
  SET_NODE(opline_qm_assign2->result, result);
10467
10468
6.48k
  zend_update_jump_target_to_next(opnum_jmp);
10469
6.48k
}
10470
/* }}} */
10471
10472
static void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
10473
79.9k
{
10474
79.9k
  zend_ast *expr_ast = ast->child[0];
10475
79.9k
  zend_ast *default_ast = ast->child[1];
10476
10477
79.9k
  znode expr_node, default_node;
10478
79.9k
  zend_op *opline;
10479
79.9k
  uint32_t opnum;
10480
10481
79.9k
  zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, 0);
10482
10483
79.9k
  opnum = get_next_op_number();
10484
79.9k
  zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL);
10485
10486
79.9k
  zend_compile_expr(&default_node, default_ast);
10487
10488
79.9k
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL);
10489
79.9k
  SET_NODE(opline->result, result);
10490
10491
79.9k
  opline = &CG(active_op_array)->opcodes[opnum];
10492
79.9k
  opline->op2.opline_num = get_next_op_number();
10493
79.9k
}
10494
/* }}} */
10495
10496
75.6k
static void znode_dtor(zval *zv) {
10497
75.6k
  znode *node = Z_PTR_P(zv);
10498
75.6k
  if (node->op_type == IS_CONST) {
10499
6.13k
    zval_ptr_dtor_nogc(&node->u.constant);
10500
6.13k
  }
10501
75.6k
  efree(node);
10502
75.6k
}
10503
10504
static void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */
10505
10.1k
{
10506
10.1k
  zend_ast *var_ast = ast->child[0];
10507
10.1k
  zend_ast *default_ast = ast->child[1];
10508
10509
10.1k
  znode var_node_is, var_node_w, default_node, assign_node, *node;
10510
10.1k
  zend_op *opline;
10511
10.1k
  uint32_t coalesce_opnum;
10512
10.1k
  bool need_frees = 0;
10513
10514
  /* Remember expressions compiled during the initial BP_VAR_IS lookup,
10515
   * to avoid double-evaluation when we compile again with BP_VAR_W. */
10516
10.1k
  HashTable *orig_memoized_exprs = CG(memoized_exprs);
10517
10.1k
  const zend_memoize_mode orig_memoize_mode = CG(memoize_mode);
10518
10519
10.1k
  zend_ensure_writable_variable(var_ast);
10520
10.1k
  if (is_this_fetch(var_ast)) {
10521
6
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
10522
6
  }
10523
10524
10.1k
  ALLOC_HASHTABLE(CG(memoized_exprs));
10525
10.1k
  zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0);
10526
10527
10.1k
  CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
10528
10.1k
  zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, 0);
10529
10530
10.1k
  coalesce_opnum = get_next_op_number();
10531
10.1k
  zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL);
10532
10533
10.1k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
10534
10.1k
  if (var_ast->kind == ZEND_AST_DIM) {
10535
7.33k
    zend_compile_expr_with_potential_assign_to_self(&default_node, default_ast, var_ast);
10536
7.33k
  } else {
10537
2.82k
    zend_compile_expr(&default_node, default_ast);
10538
2.82k
  }
10539
10540
10.1k
  CG(memoize_mode) = ZEND_MEMOIZE_FETCH;
10541
10.1k
  zend_compile_var(&var_node_w, var_ast, BP_VAR_W, 0);
10542
10543
  /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */
10544
10.1k
  opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
10545
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
10546
10.1k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
10547
10.1k
  switch (kind) {
10548
1.68k
    case ZEND_AST_VAR:
10549
1.68k
      zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node);
10550
1.68k
      break;
10551
323
    case ZEND_AST_STATIC_PROP:
10552
323
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
10553
323
      opline->result_type = IS_TMP_VAR;
10554
323
      var_node_w.op_type = IS_TMP_VAR;
10555
323
      zend_emit_op_data(&default_node);
10556
323
      assign_node = var_node_w;
10557
323
      break;
10558
6.64k
    case ZEND_AST_DIM:
10559
6.64k
      opline->opcode = ZEND_ASSIGN_DIM;
10560
6.64k
      opline->result_type = IS_TMP_VAR;
10561
6.64k
      var_node_w.op_type = IS_TMP_VAR;
10562
6.64k
      zend_emit_op_data(&default_node);
10563
6.64k
      assign_node = var_node_w;
10564
6.64k
      break;
10565
1.02k
    case ZEND_AST_PROP:
10566
1.02k
    case ZEND_AST_NULLSAFE_PROP:
10567
1.02k
      opline->opcode = ZEND_ASSIGN_OBJ;
10568
1.02k
      opline->result_type = IS_TMP_VAR;
10569
1.02k
      var_node_w.op_type = IS_TMP_VAR;
10570
1.02k
      zend_emit_op_data(&default_node);
10571
1.02k
      assign_node = var_node_w;
10572
1.02k
      break;
10573
0
    EMPTY_SWITCH_DEFAULT_CASE();
10574
10.1k
  }
10575
10576
9.67k
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL);
10577
9.67k
  SET_NODE(opline->result, result);
10578
10579
30.1k
  ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10580
30.1k
    if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10581
6.70k
      need_frees = 1;
10582
6.70k
      break;
10583
6.70k
    }
10584
30.1k
  } ZEND_HASH_FOREACH_END();
10585
10586
  /* Free DUPed expressions if there are any */
10587
9.67k
  if (need_frees) {
10588
6.70k
    uint32_t jump_opnum = zend_emit_jump(0);
10589
6.70k
    zend_update_jump_target_to_next(coalesce_opnum);
10590
151k
    ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10591
151k
      if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10592
68.7k
        zend_emit_op(NULL, ZEND_FREE, node, NULL);
10593
68.7k
      }
10594
151k
    } ZEND_HASH_FOREACH_END();
10595
6.70k
    zend_update_jump_target_to_next(jump_opnum);
10596
6.70k
  } else {
10597
2.97k
    zend_update_jump_target_to_next(coalesce_opnum);
10598
2.97k
  }
10599
10600
9.67k
  zend_hash_destroy(CG(memoized_exprs));
10601
9.67k
  FREE_HASHTABLE(CG(memoized_exprs));
10602
9.67k
  CG(memoized_exprs) = orig_memoized_exprs;
10603
9.67k
  CG(memoize_mode) = orig_memoize_mode;
10604
9.67k
}
10605
/* }}} */
10606
10607
static void zend_compile_print(znode *result, zend_ast *ast) /* {{{ */
10608
4.82k
{
10609
4.82k
  zend_op *opline;
10610
4.82k
  zend_ast *expr_ast = ast->child[0];
10611
10612
4.82k
  znode expr_node;
10613
4.82k
  zend_compile_expr(&expr_node, expr_ast);
10614
10615
4.82k
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
10616
4.82k
  opline->extended_value = 1;
10617
10618
4.82k
  result->op_type = IS_CONST;
10619
4.82k
  ZVAL_LONG(&result->u.constant, 1);
10620
4.82k
}
10621
/* }}} */
10622
10623
static void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
10624
5.76k
{
10625
5.76k
  zend_ast *value_ast = ast->child[0];
10626
5.76k
  zend_ast *key_ast = ast->child[1];
10627
10628
5.76k
  znode value_node, key_node;
10629
5.76k
  znode *value_node_ptr = NULL, *key_node_ptr = NULL;
10630
5.76k
  zend_op *opline;
10631
5.76k
  bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
10632
10633
5.76k
  zend_mark_function_as_generator();
10634
10635
5.76k
  if (key_ast) {
10636
452
    zend_compile_expr(&key_node, key_ast);
10637
452
    key_node_ptr = &key_node;
10638
452
  }
10639
10640
5.76k
  if (value_ast) {
10641
3.95k
    if (returns_by_ref && zend_is_variable(value_ast)) {
10642
195
      zend_assert_not_short_circuited(value_ast);
10643
195
      zend_compile_var(&value_node, value_ast, BP_VAR_W, 1);
10644
3.76k
    } else {
10645
3.76k
      zend_compile_expr(&value_node, value_ast);
10646
3.76k
    }
10647
3.95k
    value_node_ptr = &value_node;
10648
3.95k
  }
10649
10650
5.76k
  opline = zend_emit_op(result, ZEND_YIELD, value_node_ptr, key_node_ptr);
10651
10652
5.76k
  if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
10653
403
    opline->extended_value = ZEND_RETURNS_FUNCTION;
10654
403
  }
10655
5.76k
}
10656
/* }}} */
10657
10658
static void zend_compile_yield_from(znode *result, zend_ast *ast) /* {{{ */
10659
874
{
10660
874
  zend_ast *expr_ast = ast->child[0];
10661
874
  znode expr_node;
10662
10663
874
  zend_mark_function_as_generator();
10664
10665
874
  if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
10666
8
    zend_error_noreturn(E_COMPILE_ERROR,
10667
8
      "Cannot use \"yield from\" inside a by-reference generator");
10668
8
  }
10669
10670
866
  zend_compile_expr(&expr_node, expr_ast);
10671
866
  zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
10672
866
}
10673
/* }}} */
10674
10675
static void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */
10676
791
{
10677
791
  zend_ast *obj_ast = ast->child[0];
10678
791
  zend_ast *class_ast = ast->child[1];
10679
10680
791
  znode obj_node, class_node;
10681
791
  zend_op *opline;
10682
10683
791
  zend_compile_expr(&obj_node, obj_ast);
10684
791
  if (obj_node.op_type == IS_CONST) {
10685
25
    zend_do_free(&obj_node);
10686
25
    result->op_type = IS_CONST;
10687
25
    ZVAL_FALSE(&result->u.constant);
10688
25
    return;
10689
25
  }
10690
10691
766
  zend_compile_class_ref(&class_node, class_ast,
10692
766
    ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION | ZEND_FETCH_CLASS_SILENT);
10693
10694
766
  opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL);
10695
10696
766
  if (class_node.op_type == IS_CONST) {
10697
427
    opline->op2_type = IS_CONST;
10698
427
    opline->op2.constant = zend_add_class_name_literal(
10699
427
      Z_STR(class_node.u.constant));
10700
427
    opline->extended_value = zend_alloc_cache_slot();
10701
427
  } else {
10702
339
    SET_NODE(opline->op2, &class_node);
10703
339
  }
10704
766
}
10705
/* }}} */
10706
10707
static void zend_compile_include_or_eval(znode *result, zend_ast *ast) /* {{{ */
10708
9.84k
{
10709
9.84k
  zend_ast *expr_ast = ast->child[0];
10710
9.84k
  znode expr_node;
10711
9.84k
  zend_op *opline;
10712
10713
9.84k
  zend_do_extended_fcall_begin();
10714
9.84k
  zend_compile_expr(&expr_node, expr_ast);
10715
10716
9.84k
  opline = zend_emit_op(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL);
10717
9.84k
  opline->extended_value = ast->attr;
10718
10719
9.84k
  zend_do_extended_fcall_end();
10720
9.84k
}
10721
/* }}} */
10722
10723
static void zend_compile_isset_or_empty(znode *result, zend_ast *ast) /* {{{ */
10724
8.70k
{
10725
8.70k
  zend_ast *var_ast = ast->child[0];
10726
10727
8.70k
  znode var_node;
10728
8.70k
  zend_op *opline = NULL;
10729
10730
8.70k
  ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY);
10731
10732
8.70k
  if (!zend_is_variable(var_ast)) {
10733
199
    if (ast->kind == ZEND_AST_EMPTY) {
10734
      /* empty(expr) can be transformed to !expr */
10735
132
      zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast);
10736
132
      zend_compile_expr(result, not_ast);
10737
132
      return;
10738
132
    } else {
10739
67
      zend_error_noreturn(E_COMPILE_ERROR,
10740
67
        "Cannot use isset() on the result of an expression "
10741
67
        "(you can use \"null !== expression\" instead)");
10742
67
    }
10743
199
  }
10744
10745
8.50k
  if (is_globals_fetch(var_ast)) {
10746
366
    result->op_type = IS_CONST;
10747
366
    ZVAL_BOOL(&result->u.constant, ast->kind == ZEND_AST_ISSET);
10748
366
    return;
10749
366
  }
10750
10751
8.14k
  if (is_global_var_fetch(var_ast)) {
10752
288
    if (!var_ast->child[1]) {
10753
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
10754
7
    }
10755
10756
281
    zend_compile_expr(&var_node, var_ast->child[1]);
10757
281
    if (var_node.op_type == IS_CONST) {
10758
158
      convert_to_string(&var_node.u.constant);
10759
158
    }
10760
10761
281
    opline = zend_emit_op_tmp(result, ZEND_ISSET_ISEMPTY_VAR, &var_node, NULL);
10762
281
    opline->extended_value =
10763
281
      ZEND_FETCH_GLOBAL | (ast->kind == ZEND_AST_EMPTY ? ZEND_ISEMPTY : 0);
10764
281
    return;
10765
288
  }
10766
10767
7.85k
  zend_short_circuiting_mark_inner(var_ast);
10768
7.85k
  switch (var_ast->kind) {
10769
3.84k
    case ZEND_AST_VAR:
10770
3.84k
      if (is_this_fetch(var_ast)) {
10771
1.38k
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL);
10772
1.38k
        CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
10773
2.45k
      } else if (zend_try_compile_cv(&var_node, var_ast) == SUCCESS) {
10774
1.79k
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL);
10775
1.79k
      } else {
10776
658
        opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, 0);
10777
658
        opline->opcode = ZEND_ISSET_ISEMPTY_VAR;
10778
658
      }
10779
3.84k
      break;
10780
2.75k
    case ZEND_AST_DIM:
10781
2.75k
      opline = zend_compile_dim(result, var_ast, BP_VAR_IS, /* by_ref */ false);
10782
2.75k
      opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
10783
2.75k
      break;
10784
927
    case ZEND_AST_PROP:
10785
1.05k
    case ZEND_AST_NULLSAFE_PROP:
10786
1.05k
      opline = zend_compile_prop(result, var_ast, BP_VAR_IS, 0);
10787
1.05k
      opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
10788
1.05k
      break;
10789
205
    case ZEND_AST_STATIC_PROP:
10790
205
      opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, 0, 0);
10791
205
      opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP;
10792
205
      break;
10793
7.85k
    EMPTY_SWITCH_DEFAULT_CASE()
10794
7.85k
  }
10795
10796
7.84k
  result->op_type = opline->result_type = IS_TMP_VAR;
10797
7.84k
  if (!(ast->kind == ZEND_AST_ISSET)) {
10798
1.32k
    opline->extended_value |= ZEND_ISEMPTY;
10799
1.32k
  }
10800
7.84k
}
10801
/* }}} */
10802
10803
static void zend_compile_silence(znode *result, zend_ast *ast) /* {{{ */
10804
1.73M
{
10805
1.73M
  zend_ast *expr_ast = ast->child[0];
10806
1.73M
  znode silence_node;
10807
10808
1.73M
  zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL);
10809
10810
1.73M
  if (expr_ast->kind == ZEND_AST_VAR) {
10811
    /* For @$var we need to force a FETCH instruction, otherwise the CV access will
10812
     * happen outside the silenced section. */
10813
17.6k
    zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, 0 );
10814
1.71M
  } else {
10815
1.71M
    zend_compile_expr(result, expr_ast);
10816
1.71M
  }
10817
10818
1.73M
  zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL);
10819
1.73M
}
10820
/* }}} */
10821
10822
static void zend_compile_shell_exec(znode *result, zend_ast *ast) /* {{{ */
10823
3.65k
{
10824
3.65k
  zend_ast *expr_ast = ast->child[0];
10825
10826
3.65k
  zval fn_name;
10827
3.65k
  zend_ast *name_ast, *args_ast, *call_ast;
10828
10829
3.65k
  ZVAL_STRING(&fn_name, "shell_exec");
10830
3.65k
  name_ast = zend_ast_create_zval(&fn_name);
10831
3.65k
  args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast);
10832
3.65k
  call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast);
10833
10834
3.65k
  zend_compile_expr(result, call_ast);
10835
10836
3.65k
  zval_ptr_dtor(&fn_name);
10837
3.65k
}
10838
/* }}} */
10839
10840
static void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
10841
69.4k
{
10842
69.4k
  zend_ast_list *list = zend_ast_get_list(ast);
10843
69.4k
  zend_op *opline;
10844
69.4k
  uint32_t i, opnum_init = -1;
10845
69.4k
  bool packed = 1;
10846
10847
69.4k
  if (zend_try_ct_eval_array(&result->u.constant, ast)) {
10848
44.4k
    result->op_type = IS_CONST;
10849
44.4k
    return;
10850
44.4k
  }
10851
10852
  /* Empty arrays are handled at compile-time */
10853
25.0k
  ZEND_ASSERT(list->children > 0);
10854
10855
149k
  for (i = 0; i < list->children; ++i) {
10856
124k
    zend_ast *elem_ast = list->child[i];
10857
124k
    zend_ast *value_ast, *key_ast;
10858
124k
    bool by_ref;
10859
124k
    znode value_node, key_node, *key_node_ptr = NULL;
10860
10861
124k
    if (elem_ast == NULL) {
10862
0
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
10863
0
    }
10864
10865
124k
    value_ast = elem_ast->child[0];
10866
10867
124k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
10868
970
      zend_compile_expr(&value_node, value_ast);
10869
970
      if (i == 0) {
10870
428
        opnum_init = get_next_op_number();
10871
428
        opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
10872
428
      }
10873
970
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL);
10874
970
      SET_NODE(opline->result, result);
10875
970
      continue;
10876
970
    }
10877
10878
123k
    key_ast = elem_ast->child[1];
10879
123k
    by_ref = elem_ast->attr;
10880
10881
123k
    if (key_ast) {
10882
4.71k
      zend_compile_expr(&key_node, key_ast);
10883
4.71k
      zend_handle_numeric_op(&key_node);
10884
4.71k
      key_node_ptr = &key_node;
10885
4.71k
    }
10886
10887
123k
    if (by_ref) {
10888
438
      zend_ensure_writable_variable(value_ast);
10889
438
      zend_compile_var(&value_node, value_ast, BP_VAR_W, 1);
10890
122k
    } else {
10891
122k
      zend_compile_expr(&value_node, value_ast);
10892
122k
    }
10893
10894
123k
    if (i == 0) {
10895
24.3k
      opnum_init = get_next_op_number();
10896
24.3k
      opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr);
10897
24.3k
      opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT;
10898
99.0k
    } else {
10899
99.0k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT,
10900
99.0k
        &value_node, key_node_ptr);
10901
99.0k
      SET_NODE(opline->result, result);
10902
99.0k
    }
10903
123k
    opline->extended_value |= by_ref;
10904
10905
123k
    if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) {
10906
2.26k
      packed = 0;
10907
2.26k
    }
10908
123k
  }
10909
10910
  /* Add a flag to INIT_ARRAY if we know this array cannot be packed */
10911
24.8k
  if (!packed) {
10912
962
    ZEND_ASSERT(opnum_init != (uint32_t)-1);
10913
962
    opline = &CG(active_op_array)->opcodes[opnum_init];
10914
962
    opline->extended_value |= ZEND_ARRAY_NOT_PACKED;
10915
962
  }
10916
24.8k
}
10917
/* }}} */
10918
10919
static void zend_compile_const(znode *result, zend_ast *ast) /* {{{ */
10920
742k
{
10921
742k
  zend_ast *name_ast = ast->child[0];
10922
10923
742k
  zend_op *opline;
10924
10925
742k
  bool is_fully_qualified;
10926
742k
  zend_string *orig_name = zend_ast_get_str(name_ast);
10927
742k
  zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified);
10928
10929
742k
  if (zend_string_equals_literal(resolved_name, "__COMPILER_HALT_OFFSET__") || (name_ast->attr != ZEND_NAME_RELATIVE && zend_string_equals_literal(orig_name, "__COMPILER_HALT_OFFSET__"))) {
10930
777
    zend_ast *last = CG(ast);
10931
10932
1.55k
    while (last && last->kind == ZEND_AST_STMT_LIST) {
10933
843
      zend_ast_list *list = zend_ast_get_list(last);
10934
843
      if (list->children == 0) {
10935
66
        break;
10936
66
      }
10937
777
      last = list->child[list->children-1];
10938
777
    }
10939
777
    if (last && last->kind == ZEND_AST_HALT_COMPILER) {
10940
191
      result->op_type = IS_CONST;
10941
191
      ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
10942
191
      zend_string_release_ex(resolved_name, 0);
10943
191
      return;
10944
191
    }
10945
777
  }
10946
10947
742k
  if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
10948
30.0k
    result->op_type = IS_CONST;
10949
30.0k
    zend_string_release_ex(resolved_name, 0);
10950
30.0k
    return;
10951
30.0k
  }
10952
10953
712k
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
10954
712k
  opline->op2_type = IS_CONST;
10955
10956
712k
  if (is_fully_qualified || !FC(current_namespace)) {
10957
251k
    opline->op1.num = 0;
10958
251k
    opline->op2.constant = zend_add_const_name_literal(
10959
251k
      resolved_name, 0);
10960
460k
  } else {
10961
460k
    opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE;
10962
460k
    opline->op2.constant = zend_add_const_name_literal(
10963
460k
      resolved_name, 1);
10964
460k
  }
10965
712k
  opline->extended_value = zend_alloc_cache_slot();
10966
712k
}
10967
/* }}} */
10968
10969
static void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */
10970
25.5k
{
10971
25.5k
  zend_ast *class_ast;
10972
25.5k
  zend_ast *const_ast;
10973
25.5k
  znode class_node, const_node;
10974
25.5k
  zend_op *opline;
10975
10976
25.5k
  zend_eval_const_expr(&ast->child[0]);
10977
25.5k
  zend_eval_const_expr(&ast->child[1]);
10978
10979
25.5k
  class_ast = ast->child[0];
10980
25.5k
  const_ast = ast->child[1];
10981
10982
25.5k
  if (class_ast->kind == ZEND_AST_ZVAL && const_ast->kind == ZEND_AST_ZVAL) {
10983
11.3k
    zval *const_zv = zend_ast_get_zval(const_ast);
10984
11.3k
    if (Z_TYPE_P(const_zv) == IS_STRING) {
10985
11.1k
      zend_string *const_str = Z_STR_P(const_zv);
10986
11.1k
      zend_string *resolved_name = zend_resolve_class_name_ast(class_ast);
10987
11.1k
      if (zend_try_ct_eval_class_const(&result->u.constant, resolved_name, const_str)) {
10988
312
        result->op_type = IS_CONST;
10989
312
        zend_string_release_ex(resolved_name, 0);
10990
312
        return;
10991
312
      }
10992
10.8k
      zend_string_release_ex(resolved_name, 0);
10993
10.8k
    }
10994
11.3k
  }
10995
10996
25.2k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
10997
10998
25.2k
  zend_compile_expr(&const_node, const_ast);
10999
11000
25.2k
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node);
11001
11002
25.2k
  zend_set_class_name_op1(opline, &class_node);
11003
11004
25.2k
  if (opline->op1_type == IS_CONST || opline->op2_type == IS_CONST) {
11005
24.8k
    opline->extended_value = zend_alloc_cache_slots(2);
11006
24.8k
  }
11007
25.2k
}
11008
/* }}} */
11009
11010
static void zend_compile_class_name(znode *result, zend_ast *ast) /* {{{ */
11011
5.09k
{
11012
5.09k
  zend_ast *class_ast = ast->child[0];
11013
11014
5.09k
  if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) {
11015
2.80k
    result->op_type = IS_CONST;
11016
2.80k
    return;
11017
2.80k
  }
11018
11019
2.28k
  if (class_ast->kind == ZEND_AST_ZVAL) {
11020
878
    zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11021
878
    opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
11022
1.41k
  } else {
11023
1.41k
    znode expr_node;
11024
1.41k
    zend_compile_expr(&expr_node, class_ast);
11025
1.41k
    if (expr_node.op_type == IS_CONST) {
11026
      /* Unlikely case that happen if class_ast is constant folded.
11027
       * Handle it here, to avoid needing a CONST specialization in the VM. */
11028
8
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"::class\" on %s",
11029
8
        zend_zval_value_name(&expr_node.u.constant));
11030
8
    }
11031
11032
1.40k
    zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL);
11033
1.40k
  }
11034
2.28k
}
11035
/* }}} */
11036
11037
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */
11038
273k
{
11039
273k
  if (num == 0) {
11040
34.2k
    result->op_type = IS_TMP_VAR;
11041
34.2k
    result->u.op.var = -1;
11042
34.2k
    opline->opcode = ZEND_ROPE_INIT;
11043
238k
  } else {
11044
238k
    opline->opcode = ZEND_ROPE_ADD;
11045
238k
    SET_NODE(opline->op1, result);
11046
238k
  }
11047
273k
  SET_NODE(opline->op2, elem_node);
11048
273k
  SET_NODE(opline->result, result);
11049
273k
  opline->extended_value = num;
11050
273k
  return opline;
11051
273k
}
11052
/* }}} */
11053
11054
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */
11055
260k
{
11056
260k
  zend_op *opline = get_next_op();
11057
11058
260k
  if (num == 0) {
11059
12.9k
    result->op_type = IS_TMP_VAR;
11060
12.9k
    result->u.op.var = -1;
11061
12.9k
    opline->opcode = ZEND_ROPE_INIT;
11062
247k
  } else {
11063
247k
    opline->opcode = ZEND_ROPE_ADD;
11064
247k
    SET_NODE(opline->op1, result);
11065
247k
  }
11066
260k
  SET_NODE(opline->op2, elem_node);
11067
260k
  SET_NODE(opline->result, result);
11068
260k
  opline->extended_value = num;
11069
260k
  return opline;
11070
260k
}
11071
/* }}} */
11072
11073
static void zend_compile_rope_finalize(znode *result, uint32_t rope_elements, zend_op *init_opline, zend_op *opline)
11074
47.2k
{
11075
47.2k
  if (rope_elements == 1) {
11076
2.75k
    if (opline->op2_type == IS_CONST) {
11077
222
      GET_NODE(result, opline->op2);
11078
222
      ZVAL_UNDEF(CT_CONSTANT(opline->op2));
11079
222
      SET_UNUSED(opline->op2);
11080
222
      MAKE_NOP(opline);
11081
2.53k
    } else {
11082
2.53k
      opline->opcode = ZEND_CAST;
11083
2.53k
      opline->extended_value = IS_STRING;
11084
2.53k
      opline->op1_type = opline->op2_type;
11085
2.53k
      opline->op1 = opline->op2;
11086
2.53k
      SET_UNUSED(opline->op2);
11087
2.53k
      zend_make_tmp_result(result, opline);
11088
2.53k
    }
11089
44.4k
  } else if (rope_elements == 2) {
11090
9.56k
    opline->opcode = ZEND_FAST_CONCAT;
11091
9.56k
    opline->extended_value = 0;
11092
9.56k
    opline->op1_type = init_opline->op2_type;
11093
9.56k
    opline->op1 = init_opline->op2;
11094
9.56k
    zend_make_tmp_result(result, opline);
11095
9.56k
    MAKE_NOP(init_opline);
11096
34.9k
  } else {
11097
34.9k
    uint32_t var;
11098
11099
34.9k
    init_opline->extended_value = rope_elements;
11100
34.9k
    opline->opcode = ZEND_ROPE_END;
11101
34.9k
    zend_make_tmp_result(result, opline);
11102
34.9k
    var = opline->op1.var = get_temporary_variable();
11103
11104
    /* Allocates the necessary number of zval slots to keep the rope */
11105
34.9k
    uint32_t i = ((rope_elements * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
11106
271k
    while (i > 1) {
11107
236k
      get_temporary_variable();
11108
236k
      i--;
11109
236k
    }
11110
11111
    /* Update all the previous opcodes to use the same variable */
11112
638k
    while (opline != init_opline) {
11113
603k
      opline--;
11114
603k
      if (opline->opcode == ZEND_ROPE_ADD &&
11115
603k
          opline->result.var == (uint32_t)-1) {
11116
441k
        opline->op1.var = var;
11117
441k
        opline->result.var = var;
11118
441k
      } else if (opline->opcode == ZEND_ROPE_INIT &&
11119
162k
                 opline->result.var == (uint32_t)-1) {
11120
34.9k
        opline->result.var = var;
11121
34.9k
      }
11122
603k
    }
11123
34.9k
  }
11124
47.2k
}
11125
11126
static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
11127
46.6k
{
11128
46.6k
  uint32_t i, j;
11129
46.6k
  uint32_t rope_init_lineno = -1;
11130
46.6k
  zend_op *opline = NULL, *init_opline;
11131
46.6k
  znode elem_node, last_const_node;
11132
46.6k
  zend_ast_list *list = zend_ast_get_list(ast);
11133
46.6k
  uint32_t reserved_op_number = -1;
11134
11135
46.6k
  ZEND_ASSERT(list->children > 0);
11136
11137
46.6k
  j = 0;
11138
46.6k
  last_const_node.op_type = IS_UNUSED;
11139
580k
  for (i = 0; i < list->children; i++) {
11140
534k
    zend_ast *encaps_var = list->child[i];
11141
11142
534k
    if (encaps_var->attr & (ZEND_ENCAPS_VAR_DOLLAR_CURLY|ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11143
4.31k
      if ((encaps_var->kind == ZEND_AST_VAR || encaps_var->kind == ZEND_AST_DIM) && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY)) {
11144
660
        zend_error(E_DEPRECATED, "Using ${var} in strings is deprecated, use {$var} instead");
11145
3.65k
      } else if (encaps_var->kind == ZEND_AST_VAR && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11146
3.65k
        zend_error(E_DEPRECATED, "Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead");
11147
3.65k
      }
11148
4.31k
    }
11149
11150
534k
    zend_compile_expr(&elem_node, encaps_var);
11151
11152
534k
    if (elem_node.op_type == IS_CONST) {
11153
276k
      convert_to_string(&elem_node.u.constant);
11154
11155
276k
      if (Z_STRLEN(elem_node.u.constant) == 0) {
11156
2.72k
        zval_ptr_dtor(&elem_node.u.constant);
11157
273k
      } else if (last_const_node.op_type == IS_CONST) {
11158
0
        concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant);
11159
0
        zval_ptr_dtor(&elem_node.u.constant);
11160
273k
      } else {
11161
273k
        last_const_node.op_type = IS_CONST;
11162
273k
        ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant);
11163
        /* Reserve place for ZEND_ROPE_ADD instruction */
11164
273k
        reserved_op_number = get_next_op_number();
11165
273k
        opline = get_next_op();
11166
273k
        opline->opcode = ZEND_NOP;
11167
273k
      }
11168
276k
      continue;
11169
276k
    } else {
11170
258k
      if (j == 0) {
11171
46.6k
        if (last_const_node.op_type == IS_CONST) {
11172
34.2k
          rope_init_lineno = reserved_op_number;
11173
34.2k
        } else {
11174
12.3k
          rope_init_lineno = get_next_op_number();
11175
12.3k
        }
11176
46.6k
      }
11177
258k
      if (last_const_node.op_type == IS_CONST) {
11178
234k
        opline = &CG(active_op_array)->opcodes[reserved_op_number];
11179
234k
        zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11180
234k
        last_const_node.op_type = IS_UNUSED;
11181
234k
      }
11182
258k
      opline = zend_compile_rope_add(result, j++, &elem_node);
11183
258k
    }
11184
534k
  }
11185
11186
46.6k
  if (j == 0) {
11187
0
    result->op_type = IS_CONST;
11188
0
    if (last_const_node.op_type == IS_CONST) {
11189
0
      ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant);
11190
0
    } else {
11191
0
      ZVAL_EMPTY_STRING(&result->u.constant);
11192
      /* empty string */
11193
0
    }
11194
0
    CG(active_op_array)->last = reserved_op_number - 1;
11195
0
    return;
11196
46.6k
  } else if (last_const_node.op_type == IS_CONST) {
11197
38.8k
    opline = &CG(active_op_array)->opcodes[reserved_op_number];
11198
38.8k
    opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11199
38.8k
  }
11200
46.6k
  init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
11201
46.6k
  zend_compile_rope_finalize(result, j, init_opline, opline);
11202
46.6k
}
11203
/* }}} */
11204
11205
static void zend_compile_magic_const(znode *result, zend_ast *ast) /* {{{ */
11206
15.7k
{
11207
15.7k
  zend_op *opline;
11208
11209
15.7k
  if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) {
11210
15.5k
    result->op_type = IS_CONST;
11211
15.5k
    return;
11212
15.5k
  }
11213
11214
244
  ZEND_ASSERT(ast->attr == T_CLASS_C &&
11215
244
              CG(active_class_entry) &&
11216
244
              (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
11217
11218
244
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11219
244
  opline->op1.num = ZEND_FETCH_CLASS_SELF;
11220
244
}
11221
/* }}} */
11222
11223
static bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */
11224
54.5k
{
11225
54.5k
  return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP
11226
54.5k
    || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL
11227
54.5k
    || kind == ZEND_AST_AND || kind == ZEND_AST_OR
11228
54.5k
    || kind == ZEND_AST_UNARY_OP
11229
54.5k
    || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS
11230
54.5k
    || kind == ZEND_AST_CAST
11231
54.5k
    || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM
11232
54.5k
    || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM
11233
54.5k
    || kind == ZEND_AST_UNPACK
11234
54.5k
    || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST
11235
54.5k
    || kind == ZEND_AST_CLASS_NAME
11236
54.5k
    || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE
11237
54.5k
    || kind == ZEND_AST_CONST_ENUM_INIT
11238
54.5k
    || kind == ZEND_AST_NEW || kind == ZEND_AST_ARG_LIST
11239
54.5k
    || kind == ZEND_AST_NAMED_ARG
11240
54.5k
    || kind == ZEND_AST_PROP || kind == ZEND_AST_NULLSAFE_PROP
11241
54.5k
    || kind == ZEND_AST_CLOSURE
11242
54.5k
    || kind == ZEND_AST_CALL || kind == ZEND_AST_STATIC_CALL || kind == ZEND_AST_CALLABLE_CONVERT;
11243
54.5k
}
11244
/* }}} */
11245
11246
static void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
11247
3.37k
{
11248
3.37k
  zend_ast *ast = *ast_ptr;
11249
3.37k
  zend_ast *class_ast = ast->child[0];
11250
3.37k
  zend_string *class_name;
11251
3.37k
  int fetch_type;
11252
11253
3.37k
  if (class_ast->kind != ZEND_AST_ZVAL) {
11254
14
    zend_error_noreturn(E_COMPILE_ERROR,
11255
14
      "Dynamic class names are not allowed in compile-time class constant references");
11256
14
  }
11257
3.35k
  if (Z_TYPE_P(zend_ast_get_zval(class_ast)) != IS_STRING) {
11258
10
    zend_throw_error(NULL, "Class name must be a valid object or a string");
11259
10
  }
11260
11261
3.35k
  class_name = zend_ast_get_str(class_ast);
11262
3.35k
  fetch_type = zend_get_class_fetch_type(class_name);
11263
11264
3.35k
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11265
6
    zend_error_noreturn(E_COMPILE_ERROR,
11266
6
      "\"static::\" is not allowed in compile-time constants");
11267
6
  }
11268
11269
3.35k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
11270
2.63k
    zend_string *tmp = zend_resolve_class_name_ast(class_ast);
11271
11272
2.63k
    zend_string_release_ex(class_name, 0);
11273
2.63k
    if (tmp != class_name) {
11274
622
      zval *zv = zend_ast_get_zval(class_ast);
11275
622
      ZVAL_STR(zv, tmp);
11276
622
      class_ast->attr = ZEND_NAME_FQ;
11277
622
    }
11278
2.63k
  }
11279
11280
3.35k
  ast->attr |= ZEND_FETCH_CLASS_EXCEPTION;
11281
3.35k
}
11282
/* }}} */
11283
11284
static void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */
11285
171
{
11286
171
  zend_ast *ast = *ast_ptr;
11287
171
  zend_ast *class_ast = ast->child[0];
11288
171
  if (class_ast->kind != ZEND_AST_ZVAL) {
11289
6
    zend_error_noreturn(E_COMPILE_ERROR,
11290
6
      "(expression)::class cannot be used in constant expressions");
11291
6
  }
11292
11293
165
  zend_string *class_name = zend_ast_get_str(class_ast);
11294
165
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
11295
11296
165
  switch (fetch_type) {
11297
82
    case ZEND_FETCH_CLASS_SELF:
11298
159
    case ZEND_FETCH_CLASS_PARENT:
11299
      /* For the const-eval representation store the fetch type instead of the name. */
11300
159
      zend_string_release(class_name);
11301
159
      ast->child[0] = NULL;
11302
159
      ast->attr = fetch_type;
11303
159
      return;
11304
6
    case ZEND_FETCH_CLASS_STATIC:
11305
6
      zend_error_noreturn(E_COMPILE_ERROR,
11306
6
        "static::class cannot be used for compile-time class name resolution");
11307
0
      return;
11308
165
    EMPTY_SWITCH_DEFAULT_CASE()
11309
165
  }
11310
165
}
11311
11312
static void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */
11313
29.0k
{
11314
29.0k
  zend_ast *ast = *ast_ptr;
11315
29.0k
  zend_ast *name_ast = ast->child[0];
11316
29.0k
  zend_string *orig_name = zend_ast_get_str(name_ast);
11317
29.0k
  bool is_fully_qualified;
11318
29.0k
  zval result;
11319
29.0k
  zend_string *resolved_name;
11320
11321
29.0k
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11322
11323
29.0k
  resolved_name = zend_resolve_const_name(
11324
29.0k
    orig_name, name_ast->attr, &is_fully_qualified);
11325
11326
29.0k
  if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
11327
0
    zend_string_release_ex(resolved_name, 0);
11328
0
    zend_ast_destroy(ast);
11329
0
    *ast_ptr = zend_ast_create_zval(&result);
11330
0
    return;
11331
0
  }
11332
11333
29.0k
  zend_ast_destroy(ast);
11334
29.0k
  *ast_ptr = zend_ast_create_constant(resolved_name,
11335
29.0k
    !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0);
11336
29.0k
}
11337
/* }}} */
11338
11339
static void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
11340
549
{
11341
549
  zend_ast *ast = *ast_ptr;
11342
11343
  /* Other cases already resolved by constant folding */
11344
549
  ZEND_ASSERT(ast->attr == T_CLASS_C);
11345
11346
549
  zend_ast_destroy(ast);
11347
549
  *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS);
11348
549
}
11349
/* }}} */
11350
11351
static void zend_compile_const_expr_class_reference(zend_ast *class_ast)
11352
513
{
11353
513
  if (class_ast->kind == ZEND_AST_CLASS) {
11354
0
    zend_error_noreturn(E_COMPILE_ERROR,
11355
0
      "Cannot use anonymous class in constant expression");
11356
0
  }
11357
513
  if (class_ast->kind != ZEND_AST_ZVAL) {
11358
6
    zend_error_noreturn(E_COMPILE_ERROR,
11359
6
      "Cannot use dynamic class name in constant expression");
11360
6
  }
11361
11362
507
  zend_string *class_name = zend_resolve_class_name_ast(class_ast);
11363
507
  int fetch_type = zend_get_class_fetch_type(class_name);
11364
507
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11365
6
    zend_error_noreturn(E_COMPILE_ERROR,
11366
6
      "\"static\" is not allowed in compile-time constants");
11367
6
  }
11368
11369
501
  zval *class_ast_zv = zend_ast_get_zval(class_ast);
11370
501
  zval_ptr_dtor_nogc(class_ast_zv);
11371
501
  ZVAL_STR(class_ast_zv, class_name);
11372
501
  class_ast->attr = fetch_type << ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT;
11373
501
}
11374
11375
static void zend_compile_const_expr_new(zend_ast **ast_ptr)
11376
404
{
11377
404
  zend_ast *class_ast = (*ast_ptr)->child[0];
11378
404
  zend_compile_const_expr_class_reference(class_ast);
11379
404
}
11380
11381
static void zend_compile_const_expr_closure(zend_ast **ast_ptr)
11382
148
{
11383
148
  zend_ast_decl *closure_ast = (zend_ast_decl *) *ast_ptr;
11384
148
  zend_ast *uses_ast = closure_ast->child[1];
11385
148
  if (!(closure_ast->flags & ZEND_ACC_STATIC)) {
11386
10
    zend_error_noreturn(E_COMPILE_ERROR,
11387
10
      "Closures in constant expressions must be static");
11388
10
  }
11389
138
  if (uses_ast) {
11390
6
    zend_error_noreturn(E_COMPILE_ERROR,
11391
6
      "Cannot use(...) variables in constant expression");
11392
6
  }
11393
11394
132
  znode node;
11395
132
  zend_op_array *op = zend_compile_func_decl(&node, (zend_ast*)closure_ast, FUNC_DECL_LEVEL_CONSTEXPR);
11396
11397
132
  zend_ast_destroy(*ast_ptr);
11398
132
  *ast_ptr = zend_ast_create_op_array(op);
11399
132
}
11400
11401
static void zend_compile_const_expr_fcc(zend_ast **ast_ptr)
11402
1.08k
{
11403
1.08k
  zend_ast **args_ast;
11404
1.08k
  switch ((*ast_ptr)->kind) {
11405
978
    case ZEND_AST_CALL:
11406
978
      args_ast = &(*ast_ptr)->child[1];
11407
978
      break;
11408
109
    case ZEND_AST_STATIC_CALL:
11409
109
      args_ast = &(*ast_ptr)->child[2];
11410
109
      break;
11411
0
    EMPTY_SWITCH_DEFAULT_CASE();
11412
1.08k
  }
11413
1.08k
  if ((*args_ast)->kind != ZEND_AST_CALLABLE_CONVERT) {
11414
23
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11415
23
  }
11416
1.06k
  ZEND_MAP_PTR_NEW(((zend_ast_fcc *)*args_ast)->fptr);
11417
11418
1.06k
  switch ((*ast_ptr)->kind) {
11419
955
    case ZEND_AST_CALL: {
11420
955
      zend_ast *name_ast = (*ast_ptr)->child[0];
11421
955
      if (name_ast->kind != ZEND_AST_ZVAL) {
11422
16
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic function name in constant expression");
11423
16
      }
11424
939
      zval *name_ast_zv = zend_ast_get_zval(name_ast);
11425
939
      if (Z_TYPE_P(name_ast_zv) != IS_STRING) {
11426
6
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal function name");
11427
6
      }
11428
933
      bool is_fully_qualified;
11429
933
      zend_string *name = zend_resolve_function_name(Z_STR_P(name_ast_zv), name_ast->attr, &is_fully_qualified);
11430
933
      zval_ptr_dtor_nogc(name_ast_zv);
11431
933
      ZVAL_STR(name_ast_zv, name);
11432
933
      if (is_fully_qualified) {
11433
351
        name_ast->attr = ZEND_NAME_FQ;
11434
351
      }
11435
933
      break;
11436
939
    }
11437
109
    case ZEND_AST_STATIC_CALL: {
11438
109
      zend_ast *class_ast = (*ast_ptr)->child[0];
11439
109
      zend_compile_const_expr_class_reference(class_ast);
11440
109
      zend_ast *method_ast = (*ast_ptr)->child[1];
11441
109
      if (method_ast->kind != ZEND_AST_ZVAL) {
11442
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic method name in constant expression");
11443
1
      }
11444
108
      if (Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING) {
11445
0
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal method name");
11446
0
      }
11447
108
      break;
11448
108
    }
11449
108
    EMPTY_SWITCH_DEFAULT_CASE();
11450
1.06k
  }
11451
1.06k
}
11452
11453
static void zend_compile_const_expr_args(zend_ast **ast_ptr)
11454
401
{
11455
401
  zend_ast_list *list = zend_ast_get_list(*ast_ptr);
11456
401
  bool uses_named_args = false;
11457
728
  for (uint32_t i = 0; i < list->children; i++) {
11458
329
    zend_ast *arg = list->child[i];
11459
329
    if (arg->kind == ZEND_AST_UNPACK) {
11460
0
      zend_error_noreturn(E_COMPILE_ERROR,
11461
0
        "Argument unpacking in constant expressions is not supported");
11462
0
    }
11463
329
    if (arg->kind == ZEND_AST_NAMED_ARG) {
11464
33
      uses_named_args = true;
11465
296
    } else if (uses_named_args) {
11466
2
      zend_error_noreturn(E_COMPILE_ERROR,
11467
2
        "Cannot use positional argument after named argument");
11468
2
    }
11469
329
  }
11470
399
  if (uses_named_args) {
11471
31
    list->attr = 1;
11472
31
  }
11473
399
}
11474
11475
typedef struct {
11476
  /* Whether the value of this expression may differ on each evaluation. */
11477
  bool allow_dynamic;
11478
} const_expr_context;
11479
11480
static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */
11481
154k
{
11482
154k
  const_expr_context *ctx = (const_expr_context *) context;
11483
154k
  zend_ast *ast = *ast_ptr;
11484
154k
  if (ast == NULL || ast->kind == ZEND_AST_ZVAL) {
11485
99.5k
    return;
11486
99.5k
  }
11487
11488
54.5k
  if (!zend_is_allowed_in_const_expr(ast->kind)) {
11489
51
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11490
51
  }
11491
11492
54.5k
  switch (ast->kind) {
11493
3.37k
    case ZEND_AST_CLASS_CONST:
11494
3.37k
      zend_compile_const_expr_class_const(ast_ptr);
11495
3.37k
      break;
11496
171
    case ZEND_AST_CLASS_NAME:
11497
171
      zend_compile_const_expr_class_name(ast_ptr);
11498
171
      break;
11499
29.0k
    case ZEND_AST_CONST:
11500
29.0k
      zend_compile_const_expr_const(ast_ptr);
11501
29.0k
      break;
11502
549
    case ZEND_AST_MAGIC_CONST:
11503
549
      zend_compile_const_expr_magic_const(ast_ptr);
11504
549
      break;
11505
794
    case ZEND_AST_CAST:
11506
794
      if (ast->attr == IS_OBJECT && !ctx->allow_dynamic) {
11507
6
        zend_error_noreturn(E_COMPILE_ERROR,
11508
6
          "Object casts are not supported in this context");
11509
6
      }
11510
788
      break;
11511
788
    case ZEND_AST_NEW:
11512
410
      if (!ctx->allow_dynamic) {
11513
6
        zend_error_noreturn(E_COMPILE_ERROR,
11514
6
          "New expressions are not supported in this context");
11515
6
      }
11516
404
      zend_compile_const_expr_new(ast_ptr);
11517
404
      break;
11518
401
    case ZEND_AST_ARG_LIST:
11519
401
      zend_compile_const_expr_args(ast_ptr);
11520
401
      break;
11521
148
    case ZEND_AST_CLOSURE:
11522
148
      zend_compile_const_expr_closure(ast_ptr);
11523
      /* Return, because we do not want to traverse the children. */
11524
148
      return;
11525
978
    case ZEND_AST_CALL:
11526
1.08k
    case ZEND_AST_STATIC_CALL:
11527
1.08k
      zend_compile_const_expr_fcc(ast_ptr);
11528
1.08k
      break;
11529
54.5k
  }
11530
11531
54.2k
  zend_ast_apply(ast, zend_compile_const_expr, context);
11532
54.2k
}
11533
/* }}} */
11534
11535
void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynamic) /* {{{ */
11536
79.2k
{
11537
79.2k
  const_expr_context context;
11538
79.2k
  context.allow_dynamic = allow_dynamic;
11539
11540
79.2k
  zend_eval_const_expr(ast_ptr);
11541
79.2k
  zend_compile_const_expr(ast_ptr, &context);
11542
79.2k
  if ((*ast_ptr)->kind != ZEND_AST_ZVAL) {
11543
    /* Replace with compiled AST zval representation. */
11544
31.5k
    zval ast_zv;
11545
31.5k
    ZVAL_AST(&ast_zv, zend_ast_copy(*ast_ptr));
11546
31.5k
    zend_ast_destroy(*ast_ptr);
11547
31.5k
    *ast_ptr = zend_ast_create_zval(&ast_zv);
11548
31.5k
  }
11549
79.2k
  ZVAL_COPY(result, zend_ast_get_zval(*ast_ptr));
11550
79.2k
}
11551
/* }}} */
11552
11553
/* Same as compile_stmt, but with early binding */
11554
void zend_compile_top_stmt(zend_ast *ast) /* {{{ */
11555
859k
{
11556
859k
  if (!ast) {
11557
180k
    return;
11558
180k
  }
11559
11560
678k
  if (ast->kind == ZEND_AST_STMT_LIST) {
11561
126k
    zend_ast_list *list = zend_ast_get_list(ast);
11562
126k
    uint32_t i;
11563
882k
    for (i = 0; i < list->children; ++i) {
11564
755k
      zend_compile_top_stmt(list->child[i]);
11565
755k
    }
11566
126k
    return;
11567
126k
  }
11568
11569
552k
  if (ast->kind == ZEND_AST_FUNC_DECL) {
11570
15.5k
    CG(zend_lineno) = ast->lineno;
11571
15.5k
    zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_TOPLEVEL);
11572
15.5k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11573
536k
  } else if (ast->kind == ZEND_AST_CLASS) {
11574
45.7k
    CG(zend_lineno) = ast->lineno;
11575
45.7k
    zend_compile_class_decl(NULL, ast, 1);
11576
45.7k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11577
490k
  } else {
11578
490k
    zend_compile_stmt(ast);
11579
490k
  }
11580
552k
  if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) {
11581
541k
    zend_verify_namespace();
11582
541k
  }
11583
552k
}
11584
/* }}} */
11585
11586
static void zend_compile_stmt(zend_ast *ast) /* {{{ */
11587
1.77M
{
11588
1.77M
  if (!ast) {
11589
100k
    return;
11590
100k
  }
11591
11592
1.67M
  CG(zend_lineno) = ast->lineno;
11593
11594
1.67M
  if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) {
11595
0
    zend_do_extended_stmt();
11596
0
  }
11597
11598
1.67M
  switch (ast->kind) {
11599
431k
    case ZEND_AST_STMT_LIST:
11600
431k
      zend_compile_stmt_list(ast);
11601
431k
      break;
11602
1.45k
    case ZEND_AST_GLOBAL:
11603
1.45k
      zend_compile_global_var(ast);
11604
1.45k
      break;
11605
944
    case ZEND_AST_STATIC:
11606
944
      zend_compile_static_var(ast);
11607
944
      break;
11608
7.80k
    case ZEND_AST_UNSET:
11609
7.80k
      zend_compile_unset(ast);
11610
7.80k
      break;
11611
36.5k
    case ZEND_AST_RETURN:
11612
36.5k
      zend_compile_return(ast);
11613
36.5k
      break;
11614
295k
    case ZEND_AST_ECHO:
11615
295k
      zend_compile_echo(ast);
11616
295k
      break;
11617
911
    case ZEND_AST_BREAK:
11618
1.75k
    case ZEND_AST_CONTINUE:
11619
1.75k
      zend_compile_break_continue(ast);
11620
1.75k
      break;
11621
1.91k
    case ZEND_AST_GOTO:
11622
1.91k
      zend_compile_goto(ast);
11623
1.91k
      break;
11624
4.67k
    case ZEND_AST_LABEL:
11625
4.67k
      zend_compile_label(ast);
11626
4.67k
      break;
11627
1.25k
    case ZEND_AST_WHILE:
11628
1.25k
      zend_compile_while(ast);
11629
1.25k
      break;
11630
478
    case ZEND_AST_DO_WHILE:
11631
478
      zend_compile_do_while(ast);
11632
478
      break;
11633
18.7k
    case ZEND_AST_FOR:
11634
18.7k
      zend_compile_for(ast);
11635
18.7k
      break;
11636
54.9k
    case ZEND_AST_FOREACH:
11637
54.9k
      zend_compile_foreach(ast);
11638
54.9k
      break;
11639
20.6k
    case ZEND_AST_IF:
11640
20.6k
      zend_compile_if(ast);
11641
20.6k
      break;
11642
5.83k
    case ZEND_AST_SWITCH:
11643
5.83k
      zend_compile_switch(ast);
11644
5.83k
      break;
11645
54.6k
    case ZEND_AST_TRY:
11646
54.6k
      zend_compile_try(ast);
11647
54.6k
      break;
11648
1.91k
    case ZEND_AST_DECLARE:
11649
1.91k
      zend_compile_declare(ast);
11650
1.91k
      break;
11651
1.45k
    case ZEND_AST_FUNC_DECL:
11652
33.8k
    case ZEND_AST_METHOD:
11653
33.8k
      zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_NESTED);
11654
33.8k
      break;
11655
2.44k
    case ZEND_AST_ENUM_CASE:
11656
2.44k
      zend_compile_enum_case(ast);
11657
2.44k
      break;
11658
30.6k
    case ZEND_AST_PROP_GROUP:
11659
30.6k
      zend_compile_prop_group(ast);
11660
30.6k
      break;
11661
5.53k
    case ZEND_AST_CLASS_CONST_GROUP:
11662
5.53k
      zend_compile_class_const_group(ast);
11663
5.53k
      break;
11664
5.46k
    case ZEND_AST_USE_TRAIT:
11665
5.46k
      zend_compile_use_trait(ast);
11666
5.46k
      break;
11667
20.4k
    case ZEND_AST_CLASS:
11668
20.4k
      zend_compile_class_decl(NULL, ast, 0);
11669
20.4k
      break;
11670
187
    case ZEND_AST_GROUP_USE:
11671
187
      zend_compile_group_use(ast);
11672
187
      break;
11673
1.66k
    case ZEND_AST_USE:
11674
1.66k
      zend_compile_use(ast);
11675
1.66k
      break;
11676
4.93k
    case ZEND_AST_CONST_DECL:
11677
4.93k
      zend_compile_const_decl(ast);
11678
4.93k
      break;
11679
5.22k
    case ZEND_AST_NAMESPACE:
11680
5.22k
      zend_compile_namespace(ast);
11681
5.22k
      break;
11682
64
    case ZEND_AST_HALT_COMPILER:
11683
64
      zend_compile_halt_compiler(ast);
11684
64
      break;
11685
2.40k
    case ZEND_AST_THROW:
11686
2.40k
      zend_compile_expr(NULL, ast);
11687
2.40k
      break;
11688
406
    case ZEND_AST_CAST_VOID:
11689
406
      zend_compile_void_cast(NULL, ast);
11690
406
      break;
11691
621k
    default:
11692
621k
    {
11693
621k
      znode result;
11694
621k
      zend_compile_expr(&result, ast);
11695
621k
      zend_do_free(&result);
11696
621k
    }
11697
1.67M
  }
11698
11699
1.66M
  if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) {
11700
2.45k
    zend_emit_tick();
11701
2.45k
  }
11702
1.66M
}
11703
/* }}} */
11704
11705
static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */
11706
6.84M
{
11707
  /* CG(zend_lineno) = ast->lineno; */
11708
6.84M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11709
11710
6.84M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
11711
150k
    zend_compile_memoized_expr(result, ast);
11712
150k
    return;
11713
150k
  }
11714
11715
6.69M
  switch (ast->kind) {
11716
1.40M
    case ZEND_AST_ZVAL:
11717
1.40M
      ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast));
11718
1.40M
      result->op_type = IS_CONST;
11719
1.40M
      return;
11720
26.0k
    case ZEND_AST_ZNODE:
11721
26.0k
      *result = *zend_ast_get_znode(ast);
11722
26.0k
      return;
11723
529k
    case ZEND_AST_VAR:
11724
589k
    case ZEND_AST_DIM:
11725
611k
    case ZEND_AST_PROP:
11726
654k
    case ZEND_AST_NULLSAFE_PROP:
11727
659k
    case ZEND_AST_STATIC_PROP:
11728
975k
    case ZEND_AST_CALL:
11729
1.01M
    case ZEND_AST_METHOD_CALL:
11730
1.01M
    case ZEND_AST_NULLSAFE_METHOD_CALL:
11731
1.02M
    case ZEND_AST_STATIC_CALL:
11732
1.02M
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
11733
1.02M
      zend_compile_var(result, ast, BP_VAR_R, 0);
11734
1.02M
      return;
11735
279k
    case ZEND_AST_ASSIGN:
11736
279k
      zend_compile_assign(result, ast);
11737
279k
      return;
11738
18.9k
    case ZEND_AST_ASSIGN_REF:
11739
18.9k
      zend_compile_assign_ref(result, ast);
11740
18.9k
      return;
11741
85.0k
    case ZEND_AST_NEW:
11742
85.0k
      zend_compile_new(result, ast);
11743
85.0k
      return;
11744
208k
    case ZEND_AST_ASSIGN_OP:
11745
208k
      zend_compile_compound_assign(result, ast);
11746
208k
      return;
11747
479k
    case ZEND_AST_BINARY_OP:
11748
479k
      zend_compile_binary_op(result, ast);
11749
479k
      return;
11750
15.9k
    case ZEND_AST_GREATER:
11751
40.3k
    case ZEND_AST_GREATER_EQUAL:
11752
40.3k
      zend_compile_greater(result, ast);
11753
40.3k
      return;
11754
138k
    case ZEND_AST_UNARY_OP:
11755
138k
      zend_compile_unary_op(result, ast);
11756
138k
      return;
11757
19.7k
    case ZEND_AST_UNARY_PLUS:
11758
41.9k
    case ZEND_AST_UNARY_MINUS:
11759
41.9k
      zend_compile_unary_pm(result, ast);
11760
41.9k
      return;
11761
15.0k
    case ZEND_AST_AND:
11762
22.8k
    case ZEND_AST_OR:
11763
22.8k
      zend_compile_short_circuiting(result, ast);
11764
22.8k
      return;
11765
6.48k
    case ZEND_AST_POST_INC:
11766
9.62k
    case ZEND_AST_POST_DEC:
11767
9.62k
      zend_compile_post_incdec(result, ast);
11768
9.62k
      return;
11769
3.99k
    case ZEND_AST_PRE_INC:
11770
5.93k
    case ZEND_AST_PRE_DEC:
11771
5.93k
      zend_compile_pre_incdec(result, ast);
11772
5.93k
      return;
11773
2.45k
    case ZEND_AST_CAST:
11774
2.45k
      zend_compile_cast(result, ast);
11775
2.45k
      return;
11776
9.02k
    case ZEND_AST_CONDITIONAL:
11777
9.02k
      zend_compile_conditional(result, ast);
11778
9.02k
      return;
11779
79.9k
    case ZEND_AST_COALESCE:
11780
79.9k
      zend_compile_coalesce(result, ast);
11781
79.9k
      return;
11782
10.1k
    case ZEND_AST_ASSIGN_COALESCE:
11783
10.1k
      zend_compile_assign_coalesce(result, ast);
11784
10.1k
      return;
11785
4.82k
    case ZEND_AST_PRINT:
11786
4.82k
      zend_compile_print(result, ast);
11787
4.82k
      return;
11788
5.76k
    case ZEND_AST_YIELD:
11789
5.76k
      zend_compile_yield(result, ast);
11790
5.76k
      return;
11791
874
    case ZEND_AST_YIELD_FROM:
11792
874
      zend_compile_yield_from(result, ast);
11793
874
      return;
11794
791
    case ZEND_AST_INSTANCEOF:
11795
791
      zend_compile_instanceof(result, ast);
11796
791
      return;
11797
9.84k
    case ZEND_AST_INCLUDE_OR_EVAL:
11798
9.84k
      zend_compile_include_or_eval(result, ast);
11799
9.84k
      return;
11800
7.25k
    case ZEND_AST_ISSET:
11801
8.70k
    case ZEND_AST_EMPTY:
11802
8.70k
      zend_compile_isset_or_empty(result, ast);
11803
8.70k
      return;
11804
1.73M
    case ZEND_AST_SILENCE:
11805
1.73M
      zend_compile_silence(result, ast);
11806
1.73M
      return;
11807
3.65k
    case ZEND_AST_SHELL_EXEC:
11808
3.65k
      zend_compile_shell_exec(result, ast);
11809
3.65k
      return;
11810
69.4k
    case ZEND_AST_ARRAY:
11811
69.4k
      zend_compile_array(result, ast);
11812
69.4k
      return;
11813
742k
    case ZEND_AST_CONST:
11814
742k
      zend_compile_const(result, ast);
11815
742k
      return;
11816
25.5k
    case ZEND_AST_CLASS_CONST:
11817
25.5k
      zend_compile_class_const(result, ast);
11818
25.5k
      return;
11819
5.09k
    case ZEND_AST_CLASS_NAME:
11820
5.09k
      zend_compile_class_name(result, ast);
11821
5.09k
      return;
11822
46.6k
    case ZEND_AST_ENCAPS_LIST:
11823
46.6k
      zend_compile_encaps_list(result, ast);
11824
46.6k
      return;
11825
15.7k
    case ZEND_AST_MAGIC_CONST:
11826
15.7k
      zend_compile_magic_const(result, ast);
11827
15.7k
      return;
11828
90.3k
    case ZEND_AST_CLOSURE:
11829
110k
    case ZEND_AST_ARROW_FUNC:
11830
110k
      zend_compile_func_decl(result, ast, FUNC_DECL_LEVEL_NESTED);
11831
110k
      return;
11832
2.97k
    case ZEND_AST_THROW:
11833
2.97k
      zend_compile_throw(result, ast);
11834
2.97k
      return;
11835
2.35k
    case ZEND_AST_MATCH:
11836
2.35k
      zend_compile_match(result, ast);
11837
2.35k
      return;
11838
9.20k
    case ZEND_AST_PIPE:
11839
9.20k
      zend_compile_pipe(result, ast);
11840
9.20k
      return;
11841
0
    default:
11842
0
      ZEND_ASSERT(0 /* not supported */);
11843
6.69M
  }
11844
6.69M
}
11845
/* }}} */
11846
11847
static void zend_compile_expr(znode *result, zend_ast *ast)
11848
6.84M
{
11849
6.84M
  zend_check_stack_limit();
11850
11851
6.84M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
11852
6.84M
  zend_compile_expr_inner(result, ast);
11853
6.84M
  zend_short_circuiting_commit(checkpoint, result, ast);
11854
6.84M
}
11855
11856
static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
11857
1.36M
{
11858
1.36M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11859
11860
1.36M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
11861
22.7k
    switch (ast->kind) {
11862
1.74k
      case ZEND_AST_CALL:
11863
2.11k
      case ZEND_AST_METHOD_CALL:
11864
2.11k
      case ZEND_AST_NULLSAFE_METHOD_CALL:
11865
2.46k
      case ZEND_AST_STATIC_CALL:
11866
2.46k
        zend_compile_memoized_expr(result, ast);
11867
        /* This might not actually produce an opcode, e.g. for expressions evaluated at comptime. */
11868
2.46k
        return NULL;
11869
22.7k
    }
11870
22.7k
  }
11871
11872
1.36M
  switch (ast->kind) {
11873
579k
    case ZEND_AST_VAR:
11874
579k
      return zend_compile_simple_var(result, ast, type, 0);
11875
80.2k
    case ZEND_AST_DIM:
11876
80.2k
      return zend_compile_dim(result, ast, type, by_ref);
11877
30.9k
    case ZEND_AST_PROP:
11878
74.1k
    case ZEND_AST_NULLSAFE_PROP:
11879
74.1k
      return zend_compile_prop(result, ast, type, by_ref);
11880
8.82k
    case ZEND_AST_STATIC_PROP:
11881
8.82k
      return zend_compile_static_prop(result, ast, type, by_ref, 0);
11882
408k
    case ZEND_AST_CALL:
11883
408k
      zend_compile_call(result, ast, type);
11884
408k
      return NULL;
11885
0
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
11886
0
      zend_compile_parent_property_hook_call(result, ast, type);
11887
0
      return NULL;
11888
49.9k
    case ZEND_AST_METHOD_CALL:
11889
51.7k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
11890
51.7k
      zend_compile_method_call(result, ast, type);
11891
51.7k
      return NULL;
11892
17.3k
    case ZEND_AST_STATIC_CALL:
11893
17.3k
      zend_compile_static_call(result, ast, type);
11894
17.3k
      return NULL;
11895
13.6k
    case ZEND_AST_ZNODE:
11896
13.6k
      *result = *zend_ast_get_znode(ast);
11897
13.6k
      return NULL;
11898
130k
    default:
11899
130k
      if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
11900
224
        zend_error_noreturn(E_COMPILE_ERROR,
11901
224
          "Cannot use temporary expression in write context");
11902
224
      }
11903
11904
129k
      zend_compile_expr(result, ast);
11905
129k
      return NULL;
11906
1.36M
  }
11907
1.36M
}
11908
11909
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
11910
1.36M
{
11911
1.36M
  zend_check_stack_limit();
11912
11913
1.36M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
11914
1.36M
  zend_op *opcode = zend_compile_var_inner(result, ast, type, by_ref);
11915
1.36M
  zend_short_circuiting_commit(checkpoint, result, ast);
11916
1.36M
  return opcode;
11917
1.36M
}
11918
11919
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
11920
810k
{
11921
810k
  zend_check_stack_limit();
11922
11923
810k
  switch (ast->kind) {
11924
582k
    case ZEND_AST_VAR:
11925
582k
      return zend_compile_simple_var(result, ast, type, 1);
11926
135k
    case ZEND_AST_DIM:
11927
135k
      return zend_delayed_compile_dim(result, ast, type, by_ref);
11928
20.2k
    case ZEND_AST_PROP:
11929
23.6k
    case ZEND_AST_NULLSAFE_PROP:
11930
23.6k
    {
11931
23.6k
      zend_op *opline = zend_delayed_compile_prop(result, ast, type);
11932
23.6k
      if (by_ref) {
11933
866
        opline->extended_value |= ZEND_FETCH_REF;
11934
866
      }
11935
23.6k
      return opline;
11936
20.2k
    }
11937
6.44k
    case ZEND_AST_STATIC_PROP:
11938
6.44k
      return zend_compile_static_prop(result, ast, type, by_ref, 1);
11939
62.6k
    default:
11940
62.6k
      return zend_compile_var(result, ast, type, 0);
11941
810k
  }
11942
810k
}
11943
/* }}} */
11944
11945
bool zend_try_ct_eval_cast(zval *result, uint32_t type, zval *op1)
11946
14.0k
{
11947
14.0k
  switch (type) {
11948
46
    case _IS_BOOL:
11949
46
      ZVAL_BOOL(result, zval_is_true(op1));
11950
46
      return true;
11951
290
    case IS_LONG:
11952
290
      ZVAL_LONG(result, zval_get_long(op1));
11953
290
      return true;
11954
284
    case IS_DOUBLE:
11955
284
      ZVAL_DOUBLE(result, zval_get_double(op1));
11956
284
      return true;
11957
12.4k
    case IS_STRING:
11958
      /* Conversion from double to string takes into account run-time
11959
         'precision' setting and cannot be evaluated at compile-time */
11960
12.4k
      if (Z_TYPE_P(op1) != IS_ARRAY && Z_TYPE_P(op1) != IS_DOUBLE) {
11961
11.8k
        ZVAL_STR(result, zval_get_string(op1));
11962
11.8k
        return true;
11963
11.8k
      }
11964
614
      break;
11965
614
    case IS_ARRAY:
11966
252
      ZVAL_COPY(result, op1);
11967
252
      convert_to_array(result);
11968
252
      return true;
11969
14.0k
  }
11970
1.35k
  return false;
11971
14.0k
}
11972
11973
static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
11974
13.3M
{
11975
13.3M
  zend_ast *ast = *ast_ptr;
11976
13.3M
  zval result;
11977
11978
13.3M
  if (!ast) {
11979
4.28M
    return;
11980
4.28M
  }
11981
11982
9.03M
  zend_check_stack_limit();
11983
11984
9.03M
  switch (ast->kind) {
11985
117k
    case ZEND_AST_BINARY_OP:
11986
117k
      zend_eval_const_expr(&ast->child[0]);
11987
117k
      zend_eval_const_expr(&ast->child[1]);
11988
117k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
11989
76.8k
        return;
11990
76.8k
      }
11991
11992
40.7k
      if (!zend_try_ct_eval_binary_op(&result, ast->attr,
11993
40.7k
          zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
11994
40.7k
      ) {
11995
18.5k
        return;
11996
18.5k
      }
11997
22.2k
      break;
11998
22.2k
    case ZEND_AST_GREATER:
11999
14.3k
    case ZEND_AST_GREATER_EQUAL:
12000
14.3k
      zend_eval_const_expr(&ast->child[0]);
12001
14.3k
      zend_eval_const_expr(&ast->child[1]);
12002
14.3k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12003
12.2k
        return;
12004
12.2k
      }
12005
12006
2.05k
      zend_ct_eval_greater(&result, ast->kind,
12007
2.05k
        zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]));
12008
2.05k
      break;
12009
1.54k
    case ZEND_AST_AND:
12010
2.23k
    case ZEND_AST_OR:
12011
2.23k
    {
12012
2.23k
      bool child0_is_true, child1_is_true;
12013
2.23k
      zend_eval_const_expr(&ast->child[0]);
12014
2.23k
      zend_eval_const_expr(&ast->child[1]);
12015
2.23k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12016
1.30k
        return;
12017
1.30k
      }
12018
12019
931
      child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0]));
12020
931
      if (child0_is_true == (ast->kind == ZEND_AST_OR)) {
12021
444
        ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR);
12022
444
        break;
12023
444
      }
12024
12025
487
      if (ast->child[1]->kind != ZEND_AST_ZVAL) {
12026
111
        return;
12027
111
      }
12028
12029
376
      child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1]));
12030
376
      if (ast->kind == ZEND_AST_OR) {
12031
48
        ZVAL_BOOL(&result, child0_is_true || child1_is_true);
12032
328
      } else {
12033
328
        ZVAL_BOOL(&result, child0_is_true && child1_is_true);
12034
328
      }
12035
376
      break;
12036
487
    }
12037
30.1k
    case ZEND_AST_UNARY_OP:
12038
30.1k
      zend_eval_const_expr(&ast->child[0]);
12039
30.1k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12040
23.1k
        return;
12041
23.1k
      }
12042
12043
6.99k
      if (!zend_try_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12044
534
        return;
12045
534
      }
12046
6.45k
      break;
12047
18.2k
    case ZEND_AST_UNARY_PLUS:
12048
93.3k
    case ZEND_AST_UNARY_MINUS:
12049
93.3k
      zend_eval_const_expr(&ast->child[0]);
12050
93.3k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12051
73.8k
        return;
12052
73.8k
      }
12053
12054
19.5k
      if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) {
12055
3.06k
        return;
12056
3.06k
      }
12057
16.5k
      break;
12058
23.5k
    case ZEND_AST_COALESCE:
12059
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12060
23.5k
      if (ast->child[0]->kind == ZEND_AST_DIM) {
12061
650
        ast->child[0]->attr |= ZEND_DIM_IS;
12062
650
      }
12063
23.5k
      zend_eval_const_expr(&ast->child[0]);
12064
12065
23.5k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12066
        /* ensure everything was compile-time evaluated at least once */
12067
22.8k
        zend_eval_const_expr(&ast->child[1]);
12068
22.8k
        return;
12069
22.8k
      }
12070
12071
749
      if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) {
12072
230
        zend_eval_const_expr(&ast->child[1]);
12073
230
        *ast_ptr = ast->child[1];
12074
230
        ast->child[1] = NULL;
12075
230
        zend_ast_destroy(ast);
12076
519
      } else {
12077
519
        *ast_ptr = ast->child[0];
12078
519
        ast->child[0] = NULL;
12079
519
        zend_ast_destroy(ast);
12080
519
      }
12081
749
      return;
12082
1.91k
    case ZEND_AST_CONDITIONAL:
12083
1.91k
    {
12084
1.91k
      zend_ast **child, *child_ast;
12085
1.91k
      zend_eval_const_expr(&ast->child[0]);
12086
1.91k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12087
        /* ensure everything was compile-time evaluated at least once */
12088
1.74k
        if (ast->child[1]) {
12089
834
          zend_eval_const_expr(&ast->child[1]);
12090
834
        }
12091
1.74k
        zend_eval_const_expr(&ast->child[2]);
12092
1.74k
        return;
12093
1.74k
      }
12094
12095
163
      child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];
12096
163
      if (*child == NULL) {
12097
93
        child--;
12098
93
      }
12099
163
      child_ast = *child;
12100
163
      *child = NULL;
12101
163
      zend_ast_destroy(ast);
12102
163
      *ast_ptr = child_ast;
12103
163
      zend_eval_const_expr(ast_ptr);
12104
163
      return;
12105
1.91k
    }
12106
1.82M
    case ZEND_AST_DIM:
12107
1.82M
    {
12108
      /* constant expression should be always read context ... */
12109
1.82M
      zval *container, *dim;
12110
12111
1.82M
      if (ast->child[1] == NULL) {
12112
47
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
12113
47
      }
12114
12115
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12116
1.82M
      if ((ast->attr & ZEND_DIM_IS) && ast->child[0]->kind == ZEND_AST_DIM) {
12117
703
        ast->child[0]->attr |= ZEND_DIM_IS;
12118
703
      }
12119
12120
1.82M
      zend_eval_const_expr(&ast->child[0]);
12121
1.82M
      zend_eval_const_expr(&ast->child[1]);
12122
1.82M
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12123
1.81M
        return;
12124
1.81M
      }
12125
12126
15.6k
      container = zend_ast_get_zval(ast->child[0]);
12127
15.6k
      dim = zend_ast_get_zval(ast->child[1]);
12128
12129
15.6k
      if (Z_TYPE_P(container) == IS_ARRAY) {
12130
13.5k
        zval *el;
12131
13.5k
        if (Z_TYPE_P(dim) == IS_LONG) {
12132
875
          el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim));
12133
875
          if (el) {
12134
312
            ZVAL_COPY(&result, el);
12135
563
          } else {
12136
563
            return;
12137
563
          }
12138
12.6k
        } else if (Z_TYPE_P(dim) == IS_STRING) {
12139
12.3k
          el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim));
12140
12.3k
          if (el) {
12141
748
            ZVAL_COPY(&result, el);
12142
11.5k
          } else {
12143
11.5k
            return;
12144
11.5k
          }
12145
12.3k
        } else {
12146
336
          return; /* warning... handle at runtime */
12147
336
        }
12148
13.5k
      } else if (Z_TYPE_P(container) == IS_STRING) {
12149
1.79k
        zend_long offset;
12150
1.79k
        uint8_t c;
12151
1.79k
        if (Z_TYPE_P(dim) == IS_LONG) {
12152
575
          offset = Z_LVAL_P(dim);
12153
1.22k
        } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) {
12154
595
          return;
12155
595
        }
12156
1.20k
        if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
12157
1.11k
          return;
12158
1.11k
        }
12159
84
        c = (uint8_t) Z_STRVAL_P(container)[offset];
12160
84
        ZVAL_CHAR(&result, c);
12161
337
      } else if (Z_TYPE_P(container) <= IS_FALSE) {
12162
11
        return; /* warning... handle at runtime */
12163
326
      } else {
12164
326
        return;
12165
326
      }
12166
1.14k
      break;
12167
15.6k
    }
12168
3.78M
    case ZEND_AST_ARRAY:
12169
3.78M
      if (!zend_try_ct_eval_array(&result, ast)) {
12170
3.74M
        return;
12171
3.74M
      }
12172
43.5k
      break;
12173
43.5k
    case ZEND_AST_MAGIC_CONST:
12174
1.90k
      if (!zend_try_ct_eval_magic_const(&result, ast)) {
12175
549
        return;
12176
549
      }
12177
1.35k
      break;
12178
116k
    case ZEND_AST_CONST:
12179
116k
    {
12180
116k
      zend_ast *name_ast = ast->child[0];
12181
116k
      bool is_fully_qualified;
12182
116k
      zend_string *resolved_name = zend_resolve_const_name(
12183
116k
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
12184
12185
116k
      if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
12186
110k
        zend_string_release_ex(resolved_name, 0);
12187
110k
        return;
12188
110k
      }
12189
12190
5.59k
      zend_string_release_ex(resolved_name, 0);
12191
5.59k
      break;
12192
116k
    }
12193
183k
    case ZEND_AST_CLASS_CONST:
12194
183k
    {
12195
183k
      zend_ast *class_ast;
12196
183k
      zend_ast *name_ast;
12197
183k
      zend_string *resolved_name;
12198
12199
183k
      zend_eval_const_expr(&ast->child[0]);
12200
183k
      zend_eval_const_expr(&ast->child[1]);
12201
12202
183k
      if (UNEXPECTED(ast->child[1]->kind != ZEND_AST_ZVAL
12203
183k
        || Z_TYPE_P(zend_ast_get_zval(ast->child[1])) != IS_STRING)) {
12204
1.03k
        return;
12205
1.03k
      }
12206
12207
182k
      class_ast = ast->child[0];
12208
182k
      name_ast = ast->child[1];
12209
12210
182k
      if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) {
12211
171k
        return;
12212
171k
      }
12213
12214
11.6k
      resolved_name = zend_resolve_class_name_ast(class_ast);
12215
11.6k
      if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) {
12216
11.3k
        zend_string_release_ex(resolved_name, 0);
12217
11.3k
        return;
12218
11.3k
      }
12219
12220
363
      zend_string_release_ex(resolved_name, 0);
12221
363
      break;
12222
11.6k
    }
12223
1.98k
    case ZEND_AST_CLASS_NAME:
12224
1.98k
    {
12225
1.98k
      zend_ast *class_ast = ast->child[0];
12226
1.98k
      if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) {
12227
853
        return;
12228
853
      }
12229
1.12k
      break;
12230
1.98k
    }
12231
    // TODO: We should probably use zend_ast_apply to recursively walk nodes without
12232
    // special handling. It is required that all nodes that are part of a const expr
12233
    // are visited. Probably we should be distinguishing evaluation of const expr and
12234
    // normal exprs here.
12235
3.37k
    case ZEND_AST_ARG_LIST:
12236
3.37k
    {
12237
3.37k
      zend_ast_list *list = zend_ast_get_list(ast);
12238
4.20k
      for (uint32_t i = 0; i < list->children; i++) {
12239
833
        zend_eval_const_expr(&list->child[i]);
12240
833
      }
12241
3.37k
      return;
12242
1.98k
    }
12243
3.37k
    case ZEND_AST_NEW:
12244
3.37k
      zend_eval_const_expr(&ast->child[0]);
12245
3.37k
      zend_eval_const_expr(&ast->child[1]);
12246
3.37k
      return;
12247
101
    case ZEND_AST_NAMED_ARG:
12248
101
      zend_eval_const_expr(&ast->child[1]);
12249
101
      return;
12250
2.41k
    case ZEND_AST_CONST_ENUM_INIT:
12251
2.41k
      zend_eval_const_expr(&ast->child[2]);
12252
2.41k
      return;
12253
831
    case ZEND_AST_PROP:
12254
1.23k
    case ZEND_AST_NULLSAFE_PROP:
12255
1.23k
      zend_eval_const_expr(&ast->child[0]);
12256
1.23k
      zend_eval_const_expr(&ast->child[1]);
12257
1.23k
      return;
12258
1.66k
    case ZEND_AST_CAST:
12259
1.66k
      zend_eval_const_expr(&ast->child[0]);
12260
1.66k
      if (ast->child[0]->kind == ZEND_AST_ZVAL
12261
1.66k
       && zend_try_ct_eval_cast(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12262
734
        break;
12263
734
      }
12264
927
      return;
12265
2.82M
    default:
12266
2.82M
      return;
12267
9.03M
  }
12268
12269
101k
  zend_ast_destroy(ast);
12270
101k
  *ast_ptr = zend_ast_create_zval(&result);
12271
101k
}
12272
/* }}} */