Coverage Report

Created: 2026-04-01 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_compile.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to version 2.00 of the Zend license,     |
8
   | that is bundled with this package in the file LICENSE, and is        |
9
   | available through the world-wide-web at the following url:           |
10
   | http://www.zend.com/license/2_00.txt.                                |
11
   | If you did not receive a copy of the Zend license and are unable to  |
12
   | obtain it through the world-wide-web, please send a note to          |
13
   | license@zend.com so we can mail you a copy immediately.              |
14
   +----------------------------------------------------------------------+
15
   | Authors: 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_types.h"
32
#include "zend_virtual_cwd.h"
33
#include "zend_multibyte.h"
34
#include "zend_language_scanner.h"
35
#include "zend_inheritance.h"
36
#include "zend_vm.h"
37
#include "zend_enum.h"
38
#include "zend_observer.h"
39
#include "zend_call_stack.h"
40
#include "zend_frameless_function.h"
41
#include "zend_property_hooks.h"
42
43
35.2M
#define SET_NODE(target, src) do { \
44
35.2M
    target ## _type = (src)->op_type; \
45
35.2M
    if ((src)->op_type == IS_CONST) { \
46
4.65M
      target.constant = zend_add_literal(&(src)->u.constant); \
47
30.5M
    } else { \
48
30.5M
      target = (src)->u.op; \
49
30.5M
    } \
50
35.2M
  } while (0)
51
52
25.6M
#define GET_NODE(target, src) do { \
53
25.6M
    (target)->op_type = src ## _type; \
54
25.6M
    if ((target)->op_type == IS_CONST) { \
55
515
      ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
56
25.6M
    } else { \
57
25.6M
      (target)->u.op = src; \
58
25.6M
    } \
59
25.6M
  } while (0)
60
61
52.8M
#define FC(member) (CG(file_context).member)
62
63
typedef struct _zend_loop_var {
64
  uint8_t opcode;
65
  uint8_t var_type;
66
  uint32_t   var_num;
67
  uint32_t   try_catch_offset;
68
} zend_loop_var;
69
70
9.48M
static inline uint32_t zend_alloc_cache_slots(unsigned count) {
71
9.48M
  if (count == 0) {
72
    /* Even if no cache slots are desired, the VM handler may still want to acquire
73
     * CACHE_ADDR() unconditionally. Returning zero makes sure that the address
74
     * calculation is still legal and ubsan does not complain. */
75
0
    return 0;
76
0
  }
77
78
9.48M
  zend_op_array *op_array = CG(active_op_array);
79
9.48M
  uint32_t ret = op_array->cache_size;
80
9.48M
  op_array->cache_size += count * sizeof(void*);
81
9.48M
  return ret;
82
9.48M
}
83
84
9.11M
static inline uint32_t zend_alloc_cache_slot(void) {
85
9.11M
  return zend_alloc_cache_slots(1);
86
9.11M
}
87
88
ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
89
ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename, zend_compile_position position);
90
91
#ifndef ZTS
92
ZEND_API zend_compiler_globals compiler_globals;
93
ZEND_API zend_executor_globals executor_globals;
94
#endif
95
96
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2);
97
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast);
98
static void zend_eval_const_expr(zend_ast **ast_ptr);
99
100
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
101
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
102
static void zend_compile_expr(znode *result, zend_ast *ast);
103
static void zend_compile_stmt(zend_ast *ast);
104
static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type);
105
106
#ifdef ZEND_CHECK_STACK_LIMIT
107
zend_never_inline static void zend_stack_limit_error(void)
108
0
{
109
0
  size_t max_stack_size = 0;
110
0
  if ((uintptr_t) EG(stack_base) > (uintptr_t) EG(stack_limit)) {
111
0
    max_stack_size = (size_t) ((uintptr_t) EG(stack_base) - (uintptr_t) EG(stack_limit));
112
0
  }
113
114
0
  zend_error_noreturn(E_COMPILE_ERROR,
115
0
    "Maximum call stack size of %zu bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached during compilation. Try splitting expression",
116
0
    max_stack_size);
117
0
}
118
119
static void zend_check_stack_limit(void)
120
42.3M
{
121
42.3M
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
122
0
    zend_stack_limit_error();
123
0
  }
124
42.3M
}
125
#else /* ZEND_CHECK_STACK_LIMIT */
126
static void zend_check_stack_limit(void)
127
{
128
}
129
#endif /* ZEND_CHECK_STACK_LIMIT */
130
131
static void init_op(zend_op *op)
132
50.9M
{
133
50.9M
  MAKE_NOP(op);
134
50.9M
  op->extended_value = 0;
135
50.9M
  op->lineno = CG(zend_lineno);
136
#ifdef ZEND_VERIFY_TYPE_INFERENCE
137
  op->op1_use_type = 0;
138
  op->op2_use_type = 0;
139
  op->result_use_type = 0;
140
  op->op1_def_type = 0;
141
  op->op2_def_type = 0;
142
  op->result_def_type = 0;
143
#endif
144
50.9M
}
145
146
static zend_always_inline uint32_t get_next_op_number(void)
147
8.51M
{
148
8.51M
  return CG(active_op_array)->last;
149
8.51M
}
150
151
static zend_op *get_next_op(void)
152
50.5M
{
153
50.5M
  zend_op_array *op_array = CG(active_op_array);
154
50.5M
  uint32_t next_op_num = op_array->last++;
155
50.5M
  zend_op *next_op;
156
157
50.5M
  if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) {
158
428k
    CG(context).opcodes_size *= 4;
159
428k
    op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op));
160
428k
  }
161
162
50.5M
  next_op = &(op_array->opcodes[next_op_num]);
163
164
50.5M
  init_op(next_op);
165
166
50.5M
  return next_op;
167
50.5M
}
168
169
static zend_brk_cont_element *get_next_brk_cont_element(void)
170
38.1k
{
171
38.1k
  CG(context).last_brk_cont++;
172
38.1k
  CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont);
173
38.1k
  return &CG(context).brk_cont_array[CG(context).last_brk_cont-1];
174
38.1k
}
175
176
static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */
177
143k
{
178
143k
  zend_string *filename = CG(active_op_array)->filename;
179
143k
  zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32,
180
143k
    '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
181
143k
  return zend_new_interned_string(result);
182
143k
}
183
/* }}} */
184
185
static bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
186
8.88M
{
187
8.88M
  const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
188
8.88M
  if (ns_separator != NULL) {
189
8.43M
    *result = ns_separator + 1;
190
8.43M
    *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
191
8.43M
    return 1;
192
8.43M
  }
193
194
449k
  return 0;
195
8.88M
}
196
/* }}} */
197
198
struct reserved_class_name {
199
  const char *name;
200
  size_t len;
201
};
202
static const struct reserved_class_name reserved_class_names[] = {
203
  {ZEND_STRL("bool")},
204
  {ZEND_STRL("false")},
205
  {ZEND_STRL("float")},
206
  {ZEND_STRL("int")},
207
  {ZEND_STRL("null")},
208
  {ZEND_STRL("parent")},
209
  {ZEND_STRL("self")},
210
  {ZEND_STRL("static")},
211
  {ZEND_STRL("string")},
212
  {ZEND_STRL("true")},
213
  {ZEND_STRL("void")},
214
  {ZEND_STRL("never")},
215
  {ZEND_STRL("iterable")},
216
  {ZEND_STRL("object")},
217
  {ZEND_STRL("mixed")},
218
  /* These are not usable as class names because they're proper tokens,
219
   * but they are here for class aliases. */
220
  {ZEND_STRL("array")},
221
  {ZEND_STRL("callable")},
222
  {NULL, 0}
223
};
224
225
static bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */
226
366k
{
227
366k
  const struct reserved_class_name *reserved = reserved_class_names;
228
229
366k
  const char *uqname = ZSTR_VAL(name);
230
366k
  size_t uqname_len = ZSTR_LEN(name);
231
366k
  zend_get_unqualified_name(name, &uqname, &uqname_len);
232
233
6.58M
  for (; reserved->name; ++reserved) {
234
6.22M
    if (uqname_len == reserved->len
235
117k
      && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
236
6.22M
    ) {
237
99
      return 1;
238
99
    }
239
6.22M
  }
240
241
366k
  return 0;
242
366k
}
243
/* }}} */
244
245
void zend_assert_valid_class_name(const zend_string *name, const char *type) /* {{{ */
246
364k
{
247
364k
  if (zend_is_reserved_class_name(name)) {
248
74
    zend_error_noreturn(E_COMPILE_ERROR,
249
74
      "Cannot use \"%s\" as %s as it is reserved", ZSTR_VAL(name), type);
250
74
  }
251
364k
  if (zend_string_equals_literal(name, "_")) {
252
354
    zend_error(E_DEPRECATED, "Using \"_\" as %s is deprecated since 8.4", type);
253
354
  }
254
364k
}
255
/* }}} */
256
257
typedef struct _builtin_type_info {
258
  const char* name;
259
  const size_t name_len;
260
  const uint8_t type;
261
} builtin_type_info;
262
263
static const builtin_type_info builtin_types[] = {
264
  {ZEND_STRL("null"), IS_NULL},
265
  {ZEND_STRL("true"), IS_TRUE},
266
  {ZEND_STRL("false"), IS_FALSE},
267
  {ZEND_STRL("int"), IS_LONG},
268
  {ZEND_STRL("float"), IS_DOUBLE},
269
  {ZEND_STRL("string"), IS_STRING},
270
  {ZEND_STRL("bool"), _IS_BOOL},
271
  {ZEND_STRL("void"), IS_VOID},
272
  {ZEND_STRL("never"), IS_NEVER},
273
  {ZEND_STRL("iterable"), IS_ITERABLE},
274
  {ZEND_STRL("object"), IS_OBJECT},
275
  {ZEND_STRL("mixed"), IS_MIXED},
276
  {NULL, 0, IS_UNDEF}
277
};
278
279
typedef struct {
280
  const char *name;
281
  size_t name_len;
282
  const char *correct_name;
283
} confusable_type_info;
284
285
static const confusable_type_info confusable_types[] = {
286
  {ZEND_STRL("boolean"), "bool"},
287
  {ZEND_STRL("integer"), "int"},
288
  {ZEND_STRL("double"), "float"},
289
  {ZEND_STRL("resource"), NULL},
290
  {NULL, 0, NULL},
291
};
292
293
static zend_always_inline uint8_t zend_lookup_builtin_type_by_name(const zend_string *name) /* {{{ */
294
306k
{
295
306k
  const builtin_type_info *info = &builtin_types[0];
296
297
3.55M
  for (; info->name; ++info) {
298
3.36M
    if (ZSTR_LEN(name) == info->name_len
299
162k
      && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
300
3.36M
    ) {
301
111k
      return info->type;
302
111k
    }
303
3.36M
  }
304
305
194k
  return 0;
306
306k
}
307
/* }}} */
308
309
static zend_always_inline bool zend_is_confusable_type(const zend_string *name, const char **correct_name) /* {{{ */
310
194k
{
311
194k
  const confusable_type_info *info = confusable_types;
312
313
  /* Intentionally using case-sensitive comparison here, because "integer" is likely intended
314
   * as a scalar type, while "Integer" is likely a class type. */
315
923k
  for (; info->name; ++info) {
316
745k
    if (zend_string_equals_cstr(name, info->name, info->name_len)) {
317
16.3k
      *correct_name = info->correct_name;
318
16.3k
      return 1;
319
16.3k
    }
320
745k
  }
321
322
177k
  return 0;
323
194k
}
324
/* }}} */
325
326
16.3k
static bool zend_is_not_imported(zend_string *name) {
327
  /* Assuming "name" is unqualified here. */
328
16.3k
  return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL;
329
16.3k
}
330
331
void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_array *op_array) /* {{{ */
332
1.09M
{
333
1.09M
  *prev_context = CG(context);
334
1.09M
  CG(context).prev = CG(context).op_array ? prev_context : NULL;
335
1.09M
  CG(context).op_array = op_array;
336
1.09M
  CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
337
1.09M
  CG(context).vars_size = 0;
338
1.09M
  CG(context).literals_size = 0;
339
1.09M
  CG(context).fast_call_var = -1;
340
1.09M
  CG(context).try_catch_offset = -1;
341
1.09M
  CG(context).current_brk_cont = -1;
342
1.09M
  CG(context).last_brk_cont = 0;
343
1.09M
  CG(context).has_assigned_to_http_response_header = false;
344
1.09M
  CG(context).brk_cont_array = NULL;
345
1.09M
  CG(context).labels = NULL;
346
1.09M
  CG(context).in_jmp_frameless_branch = false;
347
1.09M
  CG(context).active_property_info_name = NULL;
348
1.09M
  CG(context).active_property_hook_kind = (zend_property_hook_kind)-1;
349
1.09M
}
350
/* }}} */
351
352
void zend_oparray_context_end(const zend_oparray_context *prev_context) /* {{{ */
353
1.08M
{
354
1.08M
  if (CG(context).brk_cont_array) {
355
26.2k
    efree(CG(context).brk_cont_array);
356
26.2k
    CG(context).brk_cont_array = NULL;
357
26.2k
  }
358
1.08M
  if (CG(context).labels) {
359
1.78k
    zend_hash_destroy(CG(context).labels);
360
1.78k
    FREE_HASHTABLE(CG(context).labels);
361
1.78k
    CG(context).labels = NULL;
362
1.78k
  }
363
1.08M
  CG(context) = *prev_context;
364
1.08M
}
365
/* }}} */
366
367
static void zend_reset_import_tables(void) /* {{{ */
368
82.6k
{
369
82.6k
  if (FC(imports)) {
370
771
    zend_hash_destroy(FC(imports));
371
771
    efree(FC(imports));
372
771
    FC(imports) = NULL;
373
771
  }
374
375
82.6k
  if (FC(imports_function)) {
376
462
    zend_hash_destroy(FC(imports_function));
377
462
    efree(FC(imports_function));
378
462
    FC(imports_function) = NULL;
379
462
  }
380
381
82.6k
  if (FC(imports_const)) {
382
283
    zend_hash_destroy(FC(imports_const));
383
283
    efree(FC(imports_const));
384
283
    FC(imports_const) = NULL;
385
283
  }
386
387
82.6k
  zend_hash_clean(&FC(seen_symbols));
388
82.6k
}
389
/* }}} */
390
391
78.0k
static void zend_end_namespace(void) /* {{{ */ {
392
78.0k
  FC(in_namespace) = 0;
393
78.0k
  zend_reset_import_tables();
394
78.0k
  if (FC(current_namespace)) {
395
2.95k
    zend_string_release_ex(FC(current_namespace), 0);
396
2.95k
    FC(current_namespace) = NULL;
397
2.95k
  }
398
78.0k
}
399
/* }}} */
400
401
void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
402
81.8k
{
403
81.8k
  *prev_context = CG(file_context);
404
81.8k
  FC(imports) = NULL;
405
81.8k
  FC(imports_function) = NULL;
406
81.8k
  FC(imports_const) = NULL;
407
81.8k
  FC(current_namespace) = NULL;
408
81.8k
  FC(in_namespace) = 0;
409
81.8k
  FC(has_bracketed_namespaces) = 0;
410
81.8k
  FC(declarables).ticks = 0;
411
81.8k
  zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
412
81.8k
}
413
/* }}} */
414
415
void zend_file_context_end(const zend_file_context *prev_context) /* {{{ */
416
76.4k
{
417
76.4k
  zend_end_namespace();
418
76.4k
  zend_hash_destroy(&FC(seen_symbols));
419
76.4k
  CG(file_context) = *prev_context;
420
76.4k
}
421
/* }}} */
422
423
void zend_init_compiler_data_structures(void) /* {{{ */
424
229k
{
425
229k
  zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
426
229k
  zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
427
229k
  zend_stack_init(&CG(short_circuiting_opnums), sizeof(uint32_t));
428
229k
  CG(active_class_entry) = NULL;
429
229k
  CG(in_compilation) = 0;
430
229k
  CG(skip_shebang) = 0;
431
432
229k
  CG(encoding_declared) = 0;
433
229k
  CG(memoized_exprs) = NULL;
434
229k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
435
229k
}
436
/* }}} */
437
438
1.12M
static void zend_register_seen_symbol(zend_string *name, uint32_t kind) {
439
1.12M
  zval *zv = zend_hash_find(&FC(seen_symbols), name);
440
1.12M
  if (zv) {
441
1.04M
    Z_LVAL_P(zv) |= kind;
442
1.04M
  } else {
443
73.4k
    zval tmp;
444
73.4k
    ZVAL_LONG(&tmp, kind);
445
73.4k
    zend_hash_add_new(&FC(seen_symbols), name, &tmp);
446
73.4k
  }
447
1.12M
}
448
449
2.49k
static bool zend_have_seen_symbol(zend_string *name, uint32_t kind) {
450
2.49k
  const zval *zv = zend_hash_find(&FC(seen_symbols), name);
451
2.49k
  return zv && (Z_LVAL_P(zv) & kind) != 0;
452
2.49k
}
453
454
void init_compiler(void) /* {{{ */
455
224k
{
456
224k
  CG(arena) = zend_arena_create(64 * 1024);
457
224k
  CG(active_op_array) = NULL;
458
224k
  memset(&CG(context), 0, sizeof(CG(context)));
459
224k
  zend_init_compiler_data_structures();
460
224k
  zend_init_rsrc_list();
461
224k
  zend_stream_init();
462
224k
  CG(unclean_shutdown) = 0;
463
464
224k
  CG(delayed_variance_obligations) = NULL;
465
224k
  CG(delayed_autoloads) = NULL;
466
224k
  CG(unlinked_uses) = NULL;
467
224k
  CG(current_linking_class) = NULL;
468
224k
}
469
/* }}} */
470
471
void shutdown_compiler(void) /* {{{ */
472
229k
{
473
  /* Reset filename before destroying the arena, as file cache may use arena allocated strings. */
474
229k
  zend_restore_compiled_filename(NULL);
475
476
229k
  zend_stack_destroy(&CG(loop_var_stack));
477
229k
  zend_stack_destroy(&CG(delayed_oplines_stack));
478
229k
  zend_stack_destroy(&CG(short_circuiting_opnums));
479
480
229k
  if (CG(delayed_variance_obligations)) {
481
251
    zend_hash_destroy(CG(delayed_variance_obligations));
482
251
    FREE_HASHTABLE(CG(delayed_variance_obligations));
483
251
    CG(delayed_variance_obligations) = NULL;
484
251
  }
485
229k
  if (CG(delayed_autoloads)) {
486
259
    zend_hash_destroy(CG(delayed_autoloads));
487
259
    FREE_HASHTABLE(CG(delayed_autoloads));
488
259
    CG(delayed_autoloads) = NULL;
489
259
  }
490
229k
  if (CG(unlinked_uses)) {
491
224
    zend_hash_destroy(CG(unlinked_uses));
492
224
    FREE_HASHTABLE(CG(unlinked_uses));
493
224
    CG(unlinked_uses) = NULL;
494
224
  }
495
229k
  CG(current_linking_class) = NULL;
496
229k
}
497
/* }}} */
498
499
ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
500
147k
{
501
147k
  CG(compiled_filename) = zend_string_copy(new_compiled_filename);
502
147k
  return new_compiled_filename;
503
147k
}
504
/* }}} */
505
506
ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
507
531k
{
508
531k
  if (CG(compiled_filename)) {
509
147k
    zend_string_release(CG(compiled_filename));
510
147k
    CG(compiled_filename) = NULL;
511
147k
  }
512
531k
  CG(compiled_filename) = original_compiled_filename;
513
531k
}
514
/* }}} */
515
516
ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
517
1.68M
{
518
1.68M
  return CG(compiled_filename);
519
1.68M
}
520
/* }}} */
521
522
ZEND_API uint32_t zend_get_compiled_lineno(void) /* {{{ */
523
272k
{
524
272k
  return CG(zend_lineno);
525
272k
}
526
/* }}} */
527
528
ZEND_API bool zend_is_compiling(void) /* {{{ */
529
2.90M
{
530
2.90M
  return CG(in_compilation);
531
2.90M
}
532
/* }}} */
533
534
static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */
535
25.9M
{
536
25.9M
  return (uint32_t)CG(active_op_array)->T++;
537
25.9M
}
538
/* }}} */
539
540
2.02M
static uint32_t lookup_cv(zend_string *name) /* {{{ */{
541
2.02M
  zend_op_array *op_array = CG(active_op_array);
542
2.02M
  int i = 0;
543
2.02M
  zend_ulong hash_value = zend_string_hash_val(name);
544
545
6.17M
  while (i < op_array->last_var) {
546
4.74M
    if (ZSTR_H(op_array->vars[i]) == hash_value
547
592k
     && zend_string_equals(op_array->vars[i], name)) {
548
590k
      return EX_NUM_TO_VAR(i);
549
590k
    }
550
4.15M
    i++;
551
4.15M
  }
552
1.43M
  i = op_array->last_var;
553
1.43M
  op_array->last_var++;
554
1.43M
  if (op_array->last_var > CG(context).vars_size) {
555
1.01M
    CG(context).vars_size += 16; /* FIXME */
556
1.01M
    op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
557
1.01M
  }
558
559
1.43M
  op_array->vars[i] = zend_string_copy(name);
560
1.43M
  return EX_NUM_TO_VAR(i);
561
2.02M
}
562
/* }}} */
563
564
zend_string *zval_make_interned_string(zval *zv)
565
30.6M
{
566
30.6M
  ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
567
30.6M
  Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
568
30.6M
  if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
569
4.13M
    Z_TYPE_FLAGS_P(zv) = 0;
570
4.13M
  }
571
30.6M
  return Z_STR_P(zv);
572
30.6M
}
573
574
/* Common part of zend_add_literal and zend_append_individual_literal */
575
static inline void zend_insert_literal(const zend_op_array *op_array, zval *zv, int literal_position) /* {{{ */
576
30.6M
{
577
30.6M
  zval *lit = CT_CONSTANT_EX(op_array, literal_position);
578
30.6M
  if (Z_TYPE_P(zv) == IS_STRING) {
579
28.7M
    zval_make_interned_string(zv);
580
28.7M
  }
581
30.6M
  ZVAL_COPY_VALUE(lit, zv);
582
30.6M
  Z_EXTRA_P(lit) = 0;
583
30.6M
}
584
/* }}} */
585
586
/* Is used while compiling a function, using the context to keep track
587
   of an approximate size to avoid to relocate to often.
588
   Literals are truncated to actual size in the second compiler pass (pass_two()). */
589
static int zend_add_literal(zval *zv) /* {{{ */
590
30.6M
{
591
30.6M
  zend_op_array *op_array = CG(active_op_array);
592
30.6M
  uint32_t i = op_array->last_literal;
593
30.6M
  op_array->last_literal++;
594
30.6M
  if (i >= CG(context).literals_size) {
595
4.81M
    while (i >= CG(context).literals_size) {
596
2.40M
      CG(context).literals_size += 16; /* FIXME */
597
2.40M
    }
598
2.40M
    op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
599
2.40M
  }
600
30.6M
  zend_insert_literal(op_array, zv, i);
601
30.6M
  return i;
602
30.6M
}
603
/* }}} */
604
605
static inline int zend_add_literal_string(zend_string **str) /* {{{ */
606
25.9M
{
607
25.9M
  int ret;
608
25.9M
  zval zv;
609
25.9M
  ZVAL_STR(&zv, *str);
610
25.9M
  ret = zend_add_literal(&zv);
611
25.9M
  *str = Z_STR(zv);
612
25.9M
  return ret;
613
25.9M
}
614
/* }}} */
615
616
static int zend_add_func_name_literal(zend_string *name) /* {{{ */
617
161k
{
618
  /* Original name */
619
161k
  int ret = zend_add_literal_string(&name);
620
621
  /* Lowercased name */
622
161k
  zend_string *lc_name = zend_string_tolower(name);
623
161k
  zend_add_literal_string(&lc_name);
624
625
161k
  return ret;
626
161k
}
627
/* }}} */
628
629
static int zend_add_ns_func_name_literal(zend_string *name) /* {{{ */
630
2.19M
{
631
2.19M
  const char *unqualified_name;
632
2.19M
  size_t unqualified_name_len;
633
634
  /* Original name */
635
2.19M
  int ret = zend_add_literal_string(&name);
636
637
  /* Lowercased name */
638
2.19M
  zend_string *lc_name = zend_string_tolower(name);
639
2.19M
  zend_add_literal_string(&lc_name);
640
641
  /* Lowercased unqualified name */
642
2.19M
  if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
643
2.19M
    lc_name = zend_string_alloc(unqualified_name_len, 0);
644
2.19M
    zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
645
2.19M
    zend_add_literal_string(&lc_name);
646
2.19M
  }
647
648
2.19M
  return ret;
649
2.19M
}
650
/* }}} */
651
652
static int zend_add_class_name_literal(zend_string *name) /* {{{ */
653
173k
{
654
  /* Original name */
655
173k
  int ret = zend_add_literal_string(&name);
656
657
  /* Lowercased name */
658
173k
  zend_string *lc_name = zend_string_tolower(name);
659
173k
  zend_add_literal_string(&lc_name);
660
661
173k
  return ret;
662
173k
}
663
/* }}} */
664
665
static int zend_add_const_name_literal(zend_string *name, bool unqualified) /* {{{ */
666
6.17M
{
667
6.17M
  zend_string *tmp_name;
668
669
6.17M
  int ret = zend_add_literal_string(&name);
670
671
6.17M
  size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
672
6.17M
  const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
673
6.17M
  if (after_ns) {
674
6.02M
    after_ns += 1;
675
6.02M
    ns_len = after_ns - ZSTR_VAL(name) - 1;
676
6.02M
    after_ns_len = ZSTR_LEN(name) - ns_len - 1;
677
678
    /* lowercased namespace name & original constant name */
679
6.02M
    tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
680
6.02M
    zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
681
6.02M
    zend_add_literal_string(&tmp_name);
682
683
6.02M
    if (!unqualified) {
684
1.91k
      return ret;
685
1.91k
    }
686
6.02M
  } else {
687
150k
    after_ns = ZSTR_VAL(name);
688
150k
  }
689
690
  /* original unqualified constant name */
691
6.16M
  tmp_name = zend_string_init(after_ns, after_ns_len, 0);
692
6.16M
  zend_add_literal_string(&tmp_name);
693
694
6.16M
  return ret;
695
6.17M
}
696
/* }}} */
697
698
40.9k
#define LITERAL_STR(op, str) do { \
699
40.9k
    zval _c; \
700
40.9k
    ZVAL_STR(&_c, str); \
701
40.9k
    op.constant = zend_add_literal(&_c); \
702
40.9k
  } while (0)
703
704
void zend_stop_lexing(void)
705
74
{
706
74
  if (LANG_SCNG(on_event)) {
707
0
    LANG_SCNG(on_event)(ON_STOP, END, 0, NULL, 0, LANG_SCNG(on_event_context));
708
0
  }
709
710
74
  LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
711
74
}
712
713
static inline void zend_begin_loop(
714
    uint8_t free_opcode, const znode *loop_var, bool is_switch) /* {{{ */
715
38.1k
{
716
38.1k
  zend_brk_cont_element *brk_cont_element;
717
38.1k
  int parent = CG(context).current_brk_cont;
718
38.1k
  zend_loop_var info = {0};
719
720
38.1k
  CG(context).current_brk_cont = CG(context).last_brk_cont;
721
38.1k
  brk_cont_element = get_next_brk_cont_element();
722
38.1k
  brk_cont_element->parent = parent;
723
38.1k
  brk_cont_element->is_switch = is_switch;
724
725
38.1k
  if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
726
19.7k
    uint32_t start = get_next_op_number();
727
728
19.7k
    info.opcode = free_opcode;
729
19.7k
    info.var_type = loop_var->op_type;
730
19.7k
    info.var_num = loop_var->u.op.var;
731
19.7k
    brk_cont_element->start = start;
732
19.7k
  } else {
733
18.3k
    info.opcode = ZEND_NOP;
734
    /* The start field is used to free temporary variables in case of exceptions.
735
     * We won't try to free something of we don't have loop variable.  */
736
18.3k
    brk_cont_element->start = -1;
737
18.3k
  }
738
739
38.1k
  zend_stack_push(&CG(loop_var_stack), &info);
740
38.1k
}
741
/* }}} */
742
743
static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
744
37.8k
{
745
37.8k
  uint32_t end = get_next_op_number();
746
37.8k
  zend_brk_cont_element *brk_cont_element
747
37.8k
    = &CG(context).brk_cont_array[CG(context).current_brk_cont];
748
37.8k
  brk_cont_element->cont = cont_addr;
749
37.8k
  brk_cont_element->brk = end;
750
37.8k
  CG(context).current_brk_cont = brk_cont_element->parent;
751
752
37.8k
  zend_stack_del_top(&CG(loop_var_stack));
753
37.8k
}
754
/* }}} */
755
756
bool zend_op_may_elide_result(uint8_t opcode)
757
2.44M
{
758
2.44M
  switch (opcode) {
759
113k
    case ZEND_ASSIGN:
760
122k
    case ZEND_ASSIGN_DIM:
761
133k
    case ZEND_ASSIGN_OBJ:
762
135k
    case ZEND_ASSIGN_STATIC_PROP:
763
162k
    case ZEND_ASSIGN_OP:
764
164k
    case ZEND_ASSIGN_DIM_OP:
765
165k
    case ZEND_ASSIGN_OBJ_OP:
766
166k
    case ZEND_ASSIGN_STATIC_PROP_OP:
767
166k
    case ZEND_PRE_INC_STATIC_PROP:
768
166k
    case ZEND_PRE_DEC_STATIC_PROP:
769
166k
    case ZEND_PRE_INC_OBJ:
770
166k
    case ZEND_PRE_DEC_OBJ:
771
167k
    case ZEND_PRE_INC:
772
167k
    case ZEND_PRE_DEC:
773
276k
    case ZEND_DO_FCALL:
774
284k
    case ZEND_DO_ICALL:
775
291k
    case ZEND_DO_UCALL:
776
1.02M
    case ZEND_DO_FCALL_BY_NAME:
777
1.02M
    case ZEND_YIELD:
778
1.02M
    case ZEND_YIELD_FROM:
779
1.03M
    case ZEND_INCLUDE_OR_EVAL:
780
1.03M
      return true;
781
1.41M
    default:
782
1.41M
      return false;
783
2.44M
  }
784
2.44M
}
785
786
static void zend_do_free(znode *op1) /* {{{ */
787
2.62M
{
788
2.62M
  if (op1->op_type == IS_TMP_VAR) {
789
2.46M
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
790
791
2.61M
    while (opline->opcode == ZEND_END_SILENCE ||
792
2.60M
           opline->opcode == ZEND_OP_DATA) {
793
144k
      opline--;
794
144k
    }
795
796
2.46M
    if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
797
2.43M
      switch (opline->opcode) {
798
1.38k
        case ZEND_BOOL:
799
5.63k
        case ZEND_BOOL_NOT:
800
          /* boolean results don't have to be freed */
801
5.63k
          return;
802
129
        case ZEND_POST_INC_STATIC_PROP:
803
363
        case ZEND_POST_DEC_STATIC_PROP:
804
704
        case ZEND_POST_INC_OBJ:
805
819
        case ZEND_POST_DEC_OBJ:
806
5.86k
        case ZEND_POST_INC:
807
7.26k
        case ZEND_POST_DEC:
808
          /* convert $i++ to ++$i */
809
7.26k
          opline->opcode -= 2;
810
7.26k
          SET_UNUSED(opline->result);
811
7.26k
          return;
812
2.42M
        default:
813
2.42M
          if (zend_op_may_elide_result(opline->opcode)) {
814
1.02M
            SET_UNUSED(opline->result);
815
1.02M
            return;
816
1.02M
          }
817
1.39M
          break;
818
2.43M
      }
819
2.43M
    }
820
821
1.42M
    zend_emit_op(NULL, ZEND_FREE, op1, NULL);
822
1.42M
  } else if (op1->op_type == IS_VAR) {
823
83.1k
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
824
83.1k
    while (opline->opcode == ZEND_END_SILENCE ||
825
83.1k
        opline->opcode == ZEND_EXT_FCALL_END ||
826
83.1k
        opline->opcode == ZEND_OP_DATA) {
827
0
      opline--;
828
0
    }
829
83.1k
    if (opline->result_type == IS_VAR
830
81.7k
      && opline->result.var == op1->u.op.var) {
831
81.7k
      if (opline->opcode == ZEND_FETCH_THIS) {
832
0
        opline->opcode = ZEND_NOP;
833
0
      }
834
81.7k
      if (!ZEND_OP_IS_FRAMELESS_ICALL(opline->opcode)) {
835
81.7k
        SET_UNUSED(opline->result);
836
81.7k
      } else {
837
        /* Frameless calls usually use the return value, so always emit a free. This should be
838
         * faster than checking RETURN_VALUE_USED inside the handler. */
839
0
        zend_emit_op(NULL, ZEND_FREE, op1, NULL);
840
0
      }
841
81.7k
    } else {
842
4.32k
      while (opline >= CG(active_op_array)->opcodes) {
843
4.32k
        if ((opline->opcode == ZEND_FETCH_LIST_R ||
844
4.15k
             opline->opcode == ZEND_FETCH_LIST_W ||
845
2.87k
             opline->opcode == ZEND_EXT_STMT) &&
846
1.45k
            opline->op1_type == IS_VAR &&
847
1.45k
            opline->op1.var == op1->u.op.var) {
848
1.39k
          zend_emit_op(NULL, ZEND_FREE, op1, NULL);
849
1.39k
          return;
850
1.39k
        }
851
2.93k
        if (opline->result_type == IS_VAR
852
1.43k
          && opline->result.var == op1->u.op.var) {
853
0
          if (opline->opcode == ZEND_NEW) {
854
0
            zend_emit_op(NULL, ZEND_FREE, op1, NULL);
855
0
          }
856
0
          break;
857
0
        }
858
2.93k
        opline--;
859
2.93k
      }
860
1.39k
    }
861
83.1k
  } else if (op1->op_type == IS_CONST) {
862
    /* Destroy value without using GC: When opcache moves arrays into SHM it will
863
     * free the zend_array structure, so references to it from outside the op array
864
     * become invalid. GC would cause such a reference in the root buffer. */
865
66.3k
    zval_ptr_dtor_nogc(&op1->u.constant);
866
66.3k
  }
867
2.62M
}
868
/* }}} */
869
870
871
static const char *zend_modifier_token_to_string(uint32_t token)
872
78
{
873
78
  switch (token) {
874
5
    case T_PUBLIC:
875
5
      return "public";
876
5
    case T_PROTECTED:
877
5
      return "protected";
878
7
    case T_PRIVATE:
879
7
      return "private";
880
14
    case T_STATIC:
881
14
      return "static";
882
13
    case T_FINAL:
883
13
      return "final";
884
21
    case T_READONLY:
885
21
      return "readonly";
886
8
    case T_ABSTRACT:
887
8
      return "abstract";
888
3
    case T_PUBLIC_SET:
889
3
      return "public(set)";
890
1
    case T_PROTECTED_SET:
891
1
      return "protected(set)";
892
1
    case T_PRIVATE_SET:
893
1
      return "private(set)";
894
78
    EMPTY_SWITCH_DEFAULT_CASE()
895
78
  }
896
78
}
897
898
uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t token)
899
56.6k
{
900
56.6k
  switch (token) {
901
36.8k
    case T_PUBLIC:
902
36.8k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
903
36.8k
        return ZEND_ACC_PUBLIC;
904
36.8k
      }
905
5
      break;
906
2.23k
    case T_PROTECTED:
907
2.23k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
908
2.23k
        return ZEND_ACC_PROTECTED;
909
2.23k
      }
910
5
      break;
911
4.82k
    case T_PRIVATE:
912
4.82k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
913
4.82k
        return ZEND_ACC_PRIVATE;
914
4.82k
      }
915
7
      break;
916
869
    case T_READONLY:
917
869
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
918
857
        return ZEND_ACC_READONLY;
919
857
      }
920
12
      break;
921
957
    case T_ABSTRACT:
922
957
      if (target == ZEND_MODIFIER_TARGET_METHOD || target == ZEND_MODIFIER_TARGET_PROPERTY) {
923
955
        return ZEND_ACC_ABSTRACT;
924
955
      }
925
2
      break;
926
1.37k
    case T_FINAL:
927
1.37k
      return ZEND_ACC_FINAL;
928
8.53k
    case T_STATIC:
929
8.53k
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_METHOD) {
930
8.53k
        return ZEND_ACC_STATIC;
931
8.53k
      }
932
7
      break;
933
381
    case T_PUBLIC_SET:
934
381
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
935
378
        return ZEND_ACC_PUBLIC_SET;
936
378
      }
937
3
      break;
938
225
    case T_PROTECTED_SET:
939
225
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
940
224
        return ZEND_ACC_PROTECTED_SET;
941
224
      }
942
1
      break;
943
368
    case T_PRIVATE_SET:
944
368
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
945
367
        return ZEND_ACC_PRIVATE_SET;
946
367
      }
947
1
      break;
948
56.6k
  }
949
950
43
  char *member;
951
43
  if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
952
0
    member = "property";
953
43
  } else if (target == ZEND_MODIFIER_TARGET_METHOD) {
954
11
    member = "method";
955
32
  } else if (target == ZEND_MODIFIER_TARGET_CONSTANT) {
956
6
    member = "class constant";
957
26
  } else if (target == ZEND_MODIFIER_TARGET_CPP) {
958
6
    member = "parameter";
959
20
  } else if (target == ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
960
20
    member = "property hook";
961
20
  } else {
962
0
    ZEND_UNREACHABLE();
963
0
  }
964
965
43
  zend_throw_exception_ex(zend_ce_compile_error, 0,
966
43
    "Cannot use the %s modifier on a %s", zend_modifier_token_to_string(token), member);
967
43
  return 0;
968
43
}
969
970
uint32_t zend_modifier_list_to_flags(zend_modifier_target target, zend_ast *modifiers)
971
47.6k
{
972
47.6k
  uint32_t flags = 0;
973
47.6k
  const zend_ast_list *modifier_list = zend_ast_get_list(modifiers);
974
975
103k
  for (uint32_t i = 0; i < modifier_list->children; i++) {
976
55.7k
    uint32_t token = (uint32_t) Z_LVAL_P(zend_ast_get_zval(modifier_list->child[i]));
977
55.7k
    uint32_t new_flag = zend_modifier_token_to_flag(target, token);
978
55.7k
    if (!new_flag) {
979
37
      return 0;
980
37
    }
981
    /* Don't error immediately for duplicate flags, we want to prioritize the errors from zend_add_member_modifier(). */
982
55.7k
    bool duplicate_flag = (flags & new_flag);
983
55.6k
    flags = zend_add_member_modifier(flags, new_flag, target);
984
55.6k
    if (!flags) {
985
58
      return 0;
986
58
    }
987
55.6k
    if (duplicate_flag) {
988
35
      zend_throw_exception_ex(zend_ce_compile_error, 0,
989
35
        "Multiple %s modifiers are not allowed", zend_modifier_token_to_string(token));
990
35
      return 0;
991
35
    }
992
55.6k
  }
993
994
47.5k
  return flags;
995
47.6k
}
996
997
uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
998
200
{
999
200
  uint32_t new_flags = flags | new_flag;
1000
200
  if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
1001
3
    zend_throw_exception(zend_ce_compile_error,
1002
3
      "Multiple abstract modifiers are not allowed", 0);
1003
3
    return 0;
1004
3
  }
1005
197
  if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
1006
5
    zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
1007
5
    return 0;
1008
5
  }
1009
192
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
1010
5
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
1011
5
    return 0;
1012
5
  }
1013
187
  if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
1014
5
    zend_throw_exception(zend_ce_compile_error,
1015
5
      "Cannot use the final modifier on an abstract class", 0);
1016
5
    return 0;
1017
5
  }
1018
182
  return new_flags;
1019
187
}
1020
/* }}} */
1021
1022
uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag)
1023
51
{
1024
51
  uint32_t new_flags = flags | new_flag;
1025
51
  if (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
1026
5
    zend_throw_exception(zend_ce_compile_error,
1027
5
      "Cannot use the abstract modifier on an anonymous class", 0);
1028
5
    return 0;
1029
5
  }
1030
46
  if (new_flag & ZEND_ACC_FINAL) {
1031
6
    zend_throw_exception(zend_ce_compile_error, "Cannot use the final modifier on an anonymous class", 0);
1032
6
    return 0;
1033
6
  }
1034
40
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
1035
5
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
1036
5
    return 0;
1037
5
  }
1038
35
  return new_flags;
1039
40
}
1040
1041
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target) /* {{{ */
1042
55.6k
{
1043
55.6k
  uint32_t new_flags = flags | new_flag;
1044
55.6k
  if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
1045
37
    zend_throw_exception(zend_ce_compile_error,
1046
37
      "Multiple access type modifiers are not allowed", 0);
1047
37
    return 0;
1048
37
  }
1049
55.6k
  if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
1050
10
    if (target == ZEND_MODIFIER_TARGET_METHOD) {
1051
5
      zend_throw_exception(zend_ce_compile_error,
1052
5
        "Cannot use the final modifier on an abstract method", 0);
1053
5
      return 0;
1054
5
    }
1055
5
    if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
1056
5
      zend_throw_exception(zend_ce_compile_error,
1057
5
        "Cannot use the final modifier on an abstract property", 0);
1058
5
      return 0;
1059
5
    }
1060
5
  }
1061
55.6k
  if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
1062
27.3k
    if ((flags & ZEND_ACC_PPP_SET_MASK) && (new_flag & ZEND_ACC_PPP_SET_MASK)) {
1063
11
      zend_throw_exception(zend_ce_compile_error,
1064
11
        "Multiple access type modifiers are not allowed", 0);
1065
11
      return 0;
1066
11
    }
1067
27.3k
  }
1068
55.6k
  return new_flags;
1069
55.6k
}
1070
/* }}} */
1071
1072
16.3k
ZEND_API zend_string *zend_create_member_string(const zend_string *class_name, const zend_string *member_name) {
1073
16.3k
  return zend_string_concat3(
1074
16.3k
    ZSTR_VAL(class_name), ZSTR_LEN(class_name),
1075
16.3k
    "::", sizeof("::") - 1,
1076
16.3k
    ZSTR_VAL(member_name), ZSTR_LEN(member_name));
1077
16.3k
}
1078
1079
10.1M
static zend_string *zend_concat_names(const char *name1, size_t name1_len, const char *name2, size_t name2_len) {
1080
10.1M
  return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len);
1081
10.1M
}
1082
1083
10.8M
static zend_string *zend_prefix_with_ns(zend_string *name) {
1084
10.8M
  if (FC(current_namespace)) {
1085
10.1M
    const zend_string *ns = FC(current_namespace);
1086
10.1M
    return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
1087
10.1M
  } else {
1088
670k
    return zend_string_copy(name);
1089
670k
  }
1090
10.8M
}
1091
1092
static zend_string *zend_resolve_non_class_name(
1093
  zend_string *name, uint32_t type, bool *is_fully_qualified,
1094
  bool case_sensitive, const HashTable *current_import_sub
1095
8.75M
) {
1096
8.75M
  const char *compound;
1097
8.75M
  *is_fully_qualified = false;
1098
1099
8.75M
  if (ZSTR_VAL(name)[0] == '\\') {
1100
    /* Remove \ prefix (only relevant if this is a string rather than a label) */
1101
521
    *is_fully_qualified = true;
1102
521
    return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1103
521
  }
1104
1105
8.75M
  if (type == ZEND_NAME_FQ) {
1106
47.7k
    *is_fully_qualified = true;
1107
47.7k
    return zend_string_copy(name);
1108
47.7k
  }
1109
1110
8.70M
  if (type == ZEND_NAME_RELATIVE) {
1111
1.15k
    *is_fully_qualified = true;
1112
1.15k
    return zend_prefix_with_ns(name);
1113
1.15k
  }
1114
1115
8.70M
  if (current_import_sub) {
1116
    /* If an unqualified name is a function/const alias, replace it. */
1117
2.74k
    zend_string *import_name;
1118
2.74k
    if (case_sensitive) {
1119
1.79k
      import_name = zend_hash_find_ptr(current_import_sub, name);
1120
1.79k
    } else {
1121
953
      import_name = zend_hash_find_ptr_lc(current_import_sub, name);
1122
953
    }
1123
1124
2.74k
    if (import_name) {
1125
681
      *is_fully_qualified = true;
1126
681
      return zend_string_copy(import_name);
1127
681
    }
1128
2.74k
  }
1129
1130
8.70M
  compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1131
8.70M
  if (compound) {
1132
6.57k
    *is_fully_qualified = true;
1133
6.57k
  }
1134
1135
8.70M
  if (compound && FC(imports)) {
1136
    /* If the first part of a qualified name is an alias, substitute it. */
1137
3.78k
    size_t len = compound - ZSTR_VAL(name);
1138
3.78k
    const zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1139
1140
3.78k
    if (import_name) {
1141
310
      return zend_concat_names(
1142
310
        ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1143
310
    }
1144
3.78k
  }
1145
1146
8.70M
  return zend_prefix_with_ns(name);
1147
8.70M
}
1148
/* }}} */
1149
1150
static zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1151
2.41M
{
1152
2.41M
  return zend_resolve_non_class_name(
1153
2.41M
    name, type, is_fully_qualified, false, FC(imports_function));
1154
2.41M
}
1155
1156
static zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1157
6.33M
{
1158
6.33M
  return zend_resolve_non_class_name(
1159
6.33M
    name, type, is_fully_qualified, true, FC(imports_const));
1160
6.33M
}
1161
1162
static zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
1163
1.99M
{
1164
1.99M
  const char *compound;
1165
1166
1.99M
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1167
3.45k
    if (type == ZEND_NAME_FQ) {
1168
7
      zend_error_noreturn(E_COMPILE_ERROR,
1169
7
        "'\\%s' is an invalid class name", ZSTR_VAL(name));
1170
7
    }
1171
3.45k
    if (type == ZEND_NAME_RELATIVE) {
1172
0
      zend_error_noreturn(E_COMPILE_ERROR,
1173
0
        "'namespace\\%s' is an invalid class name", ZSTR_VAL(name));
1174
0
    }
1175
3.45k
    ZEND_ASSERT(type == ZEND_NAME_NOT_FQ);
1176
3.45k
    return zend_string_copy(name);
1177
3.45k
  }
1178
1179
1.98M
  if (type == ZEND_NAME_RELATIVE) {
1180
254
    return zend_prefix_with_ns(name);
1181
254
  }
1182
1183
1.98M
  if (type == ZEND_NAME_FQ) {
1184
18.8k
    if (ZSTR_VAL(name)[0] == '\\') {
1185
      /* Remove \ prefix (only relevant if this is a string rather than a label) */
1186
80
      name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1187
80
      if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1188
1
        zend_error_noreturn(E_COMPILE_ERROR,
1189
1
          "'\\%s' is an invalid class name", ZSTR_VAL(name));
1190
1
      }
1191
79
      return name;
1192
80
    }
1193
1194
18.7k
    return zend_string_copy(name);
1195
18.8k
  }
1196
1197
1.97M
  if (FC(imports)) {
1198
2.93k
    compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1199
2.93k
    if (compound) {
1200
      /* If the first part of a qualified name is an alias, substitute it. */
1201
804
      size_t len = compound - ZSTR_VAL(name);
1202
804
      const zend_string *import_name =
1203
804
        zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1204
1205
804
      if (import_name) {
1206
253
        return zend_concat_names(
1207
253
          ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1208
253
      }
1209
2.13k
    } else {
1210
      /* If an unqualified name is an alias, replace it. */
1211
2.13k
      zend_string *import_name
1212
2.13k
        = zend_hash_find_ptr_lc(FC(imports), name);
1213
1214
2.13k
      if (import_name) {
1215
921
        return zend_string_copy(import_name);
1216
921
      }
1217
2.13k
    }
1218
2.93k
  }
1219
1220
  /* If not fully qualified and not an alias, prepend the current namespace */
1221
1.96M
  return zend_prefix_with_ns(name);
1222
1.97M
}
1223
/* }}} */
1224
1225
static zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
1226
1.86M
{
1227
1.86M
  const zval *class_name = zend_ast_get_zval(ast);
1228
1.86M
  if (Z_TYPE_P(class_name) != IS_STRING) {
1229
25
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1230
25
  }
1231
1.86M
  return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
1232
1.86M
}
1233
/* }}} */
1234
1235
static void label_ptr_dtor(zval *zv) /* {{{ */
1236
2.34k
{
1237
2.34k
  efree_size(Z_PTR_P(zv), sizeof(zend_label));
1238
2.34k
}
1239
/* }}} */
1240
1241
2.24k
static void str_dtor(zval *zv)  /* {{{ */ {
1242
2.24k
  zend_string_release_ex(Z_STR_P(zv), 0);
1243
2.24k
}
1244
/* }}} */
1245
1246
static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
1247
30.1k
{
1248
30.1k
  zend_op_array *op_array = CG(active_op_array);
1249
30.1k
  uint32_t try_catch_offset = op_array->last_try_catch++;
1250
30.1k
  zend_try_catch_element *elem;
1251
1252
30.1k
  op_array->try_catch_array = safe_erealloc(
1253
30.1k
    op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
1254
1255
30.1k
  elem = &op_array->try_catch_array[try_catch_offset];
1256
30.1k
  elem->try_op = try_op;
1257
30.1k
  elem->catch_op = 0;
1258
30.1k
  elem->finally_op = 0;
1259
30.1k
  elem->finally_end = 0;
1260
1261
30.1k
  return try_catch_offset;
1262
30.1k
}
1263
/* }}} */
1264
1265
ZEND_API void function_add_ref(zend_function *function) /* {{{ */
1266
1.68k
{
1267
1.68k
  if (function->type == ZEND_USER_FUNCTION) {
1268
1.68k
    zend_op_array *op_array = &function->op_array;
1269
1.68k
    if (op_array->refcount) {
1270
299
      (*op_array->refcount)++;
1271
299
    }
1272
1273
1.68k
    ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
1274
1.68k
    ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
1275
1.68k
  }
1276
1277
1.68k
  if (function->common.function_name) {
1278
1.68k
    zend_string_addref(function->common.function_name);
1279
1.68k
  }
1280
1.68k
}
1281
/* }}} */
1282
1283
static zend_never_inline ZEND_COLD ZEND_NORETURN void do_bind_function_error(const zend_string *lcname, const zend_op_array *op_array, bool compile_time) /* {{{ */
1284
106
{
1285
106
  const zval *zv = zend_hash_find_known_hash(compile_time ? CG(function_table) : EG(function_table), lcname);
1286
106
  int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
1287
106
  const zend_function *old_function;
1288
1289
106
  ZEND_ASSERT(zv != NULL);
1290
106
  old_function = Z_PTR_P(zv);
1291
106
  if (old_function->type == ZEND_USER_FUNCTION
1292
101
    && old_function->op_array.last > 0) {
1293
101
    zend_error_noreturn(error_level, "Cannot redeclare function %s() (previously declared in %s:%d)",
1294
101
          op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name),
1295
101
          ZSTR_VAL(old_function->op_array.filename),
1296
101
          old_function->op_array.line_start);
1297
101
  } else {
1298
5
    zend_error_noreturn(error_level, "Cannot redeclare function %s()",
1299
5
      op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name));
1300
5
  }
1301
106
}
1302
1303
ZEND_API zend_result do_bind_function(zend_function *func, const zval *lcname) /* {{{ */
1304
115
{
1305
115
  zend_function *added_func = zend_hash_add_ptr(EG(function_table), Z_STR_P(lcname), func);
1306
115
  if (UNEXPECTED(!added_func)) {
1307
6
    do_bind_function_error(Z_STR_P(lcname), &func->op_array, false);
1308
0
    return FAILURE;
1309
6
  }
1310
1311
109
  if (func->op_array.refcount) {
1312
20
    ++*func->op_array.refcount;
1313
20
  }
1314
109
  if (func->common.function_name) {
1315
109
    zend_string_addref(func->common.function_name);
1316
109
  }
1317
109
  zend_observer_function_declared_notify(&func->op_array, Z_STR_P(lcname));
1318
109
  return SUCCESS;
1319
115
}
1320
/* }}} */
1321
1322
ZEND_API zend_class_entry *zend_bind_class_in_slot(
1323
    zval *class_table_slot, const zval *lcname, zend_string *lc_parent_name)
1324
6.74k
{
1325
6.74k
  zend_class_entry *ce = Z_PTR_P(class_table_slot);
1326
6.74k
  bool is_preloaded =
1327
6.74k
    (ce->ce_flags & ZEND_ACC_PRELOADED) && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD);
1328
6.74k
  bool success;
1329
6.74k
  if (EXPECTED(!is_preloaded)) {
1330
6.74k
    success = zend_hash_set_bucket_key(EG(class_table), (Bucket*) class_table_slot, Z_STR_P(lcname)) != NULL;
1331
6.74k
  } else {
1332
    /* If preloading is used, don't replace the existing bucket, add a new one. */
1333
0
    success = zend_hash_add_ptr(EG(class_table), Z_STR_P(lcname), ce) != NULL;
1334
0
  }
1335
6.74k
  if (UNEXPECTED(!success)) {
1336
120
    zend_class_entry *old_class = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1337
120
    ZEND_ASSERT(old_class);
1338
120
    zend_class_redeclaration_error(E_COMPILE_ERROR, old_class);
1339
120
    return NULL;
1340
120
  }
1341
1342
6.62k
  if (ce->ce_flags & ZEND_ACC_LINKED) {
1343
266
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1344
266
    return ce;
1345
266
  }
1346
1347
6.35k
  ce = zend_do_link_class(ce, lc_parent_name, Z_STR_P(lcname));
1348
6.35k
  if (ce) {
1349
5.08k
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1350
5.08k
    return ce;
1351
5.08k
  }
1352
1353
1.27k
  if (!is_preloaded) {
1354
    /* Reload bucket pointer, the hash table may have been reallocated */
1355
225
    zval *zv = zend_hash_find(EG(class_table), Z_STR_P(lcname));
1356
225
    zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(lcname + 1));
1357
1.05k
  } else {
1358
1.05k
    zend_hash_del(EG(class_table), Z_STR_P(lcname));
1359
1.05k
  }
1360
1.27k
  return NULL;
1361
6.35k
}
1362
1363
ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */
1364
6.20k
{
1365
6.20k
  zval *rtd_key, *zv;
1366
1367
6.20k
  rtd_key = lcname + 1;
1368
1369
6.20k
  zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(rtd_key));
1370
1371
6.20k
  if (UNEXPECTED(!zv)) {
1372
11
    const zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1373
11
    ZEND_ASSERT(ce);
1374
11
    zend_class_redeclaration_error(E_COMPILE_ERROR, ce);
1375
11
    return FAILURE;
1376
11
  }
1377
1378
  /* Register the derived class */
1379
6.19k
  return zend_bind_class_in_slot(zv, lcname, lc_parent_name) ? SUCCESS : FAILURE;
1380
6.20k
}
1381
/* }}} */
1382
1383
19.3k
static zend_string *add_type_string(zend_string *type, zend_string *new_type, bool is_intersection) {
1384
19.3k
  zend_string *result;
1385
19.3k
  if (type == NULL) {
1386
14.2k
    return zend_string_copy(new_type);
1387
14.2k
  }
1388
1389
5.09k
  if (is_intersection) {
1390
2.37k
    result = zend_string_concat3(ZSTR_VAL(type), ZSTR_LEN(type),
1391
2.37k
      "&", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1392
2.37k
    zend_string_release(type);
1393
2.71k
  } else {
1394
2.71k
    result = zend_string_concat3(
1395
2.71k
      ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1396
2.71k
    zend_string_release(type);
1397
2.71k
  }
1398
5.09k
  return result;
1399
19.3k
}
1400
1401
3.34k
static zend_string *resolve_class_name(zend_string *name, const zend_class_entry *scope) {
1402
3.34k
  if (scope) {
1403
2.00k
    if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1404
31
      name = scope->name;
1405
1.97k
    } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT)) && scope->parent) {
1406
0
      name = scope->parent->name;
1407
0
    }
1408
2.00k
  }
1409
1410
  /* The resolved name for anonymous classes contains null bytes. Cut off everything after the
1411
   * null byte here, to avoid larger parts of the type being omitted by printing code later. */
1412
3.34k
  size_t len = strlen(ZSTR_VAL(name));
1413
3.34k
  if (len != ZSTR_LEN(name)) {
1414
12
    return zend_string_init(ZSTR_VAL(name), len, 0);
1415
12
  }
1416
3.33k
  return zend_string_copy(name);
1417
3.34k
}
1418
1419
static zend_string *add_intersection_type(zend_string *str,
1420
  const zend_type_list *intersection_type_list,
1421
  bool is_bracketed)
1422
1.30k
{
1423
1.30k
  const zend_type *single_type;
1424
1.30k
  zend_string *intersection_str = NULL;
1425
1426
4.97k
  ZEND_TYPE_LIST_FOREACH(intersection_type_list, single_type) {
1427
4.97k
    ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*single_type));
1428
4.97k
    ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*single_type));
1429
1430
3.67k
    intersection_str = add_type_string(intersection_str, ZEND_TYPE_NAME(*single_type), /* is_intersection */ true);
1431
3.67k
  } ZEND_TYPE_LIST_FOREACH_END();
1432
1433
1.30k
  ZEND_ASSERT(intersection_str);
1434
1435
1.30k
  if (is_bracketed) {
1436
945
    zend_string *result = zend_string_concat3("(", 1, ZSTR_VAL(intersection_str), ZSTR_LEN(intersection_str), ")", 1);
1437
945
    zend_string_release(intersection_str);
1438
945
    intersection_str = result;
1439
945
  }
1440
1.30k
  str = add_type_string(str, intersection_str, /* is_intersection */ false);
1441
1.30k
  zend_string_release(intersection_str);
1442
1.30k
  return str;
1443
1.30k
}
1444
1445
15.4k
zend_string *zend_type_to_string_resolved(const zend_type type, const zend_class_entry *scope) {
1446
15.4k
  zend_string *str = NULL;
1447
1448
  /* Pure intersection type */
1449
15.4k
  if (ZEND_TYPE_IS_INTERSECTION(type)) {
1450
355
    ZEND_ASSERT(!ZEND_TYPE_IS_UNION(type));
1451
355
    str = add_intersection_type(str, ZEND_TYPE_LIST(type), /* is_bracketed */ false);
1452
15.0k
  } else if (ZEND_TYPE_HAS_LIST(type)) {
1453
    /* A union type might not be a list */
1454
532
    const zend_type *list_type;
1455
2.39k
    ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
1456
2.39k
      if (ZEND_TYPE_IS_INTERSECTION(*list_type)) {
1457
945
        str = add_intersection_type(str, ZEND_TYPE_LIST(*list_type), /* is_bracketed */ true);
1458
945
        continue;
1459
945
      }
1460
914
      ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type));
1461
914
      ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*list_type));
1462
1463
914
      zend_string *name = ZEND_TYPE_NAME(*list_type);
1464
914
      zend_string *resolved = resolve_class_name(name, scope);
1465
914
      str = add_type_string(str, resolved, /* is_intersection */ false);
1466
914
      zend_string_release(resolved);
1467
914
    } ZEND_TYPE_LIST_FOREACH_END();
1468
14.5k
  } else if (ZEND_TYPE_HAS_NAME(type)) {
1469
2.43k
    str = resolve_class_name(ZEND_TYPE_NAME(type), scope);
1470
2.43k
  }
1471
1472
15.4k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
1473
1474
15.4k
  if (type_mask == MAY_BE_ANY) {
1475
163
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_MIXED), /* is_intersection */ false);
1476
1477
163
    return str;
1478
163
  }
1479
15.2k
  if (type_mask & MAY_BE_STATIC) {
1480
148
    zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC);
1481
    // During compilation of eval'd code the called scope refers to the scope calling the eval
1482
148
    if (scope && !zend_is_compiling()) {
1483
43
      const zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
1484
43
      if (called_scope) {
1485
16
        name = called_scope->name;
1486
16
      }
1487
43
    }
1488
148
    str = add_type_string(str, name, /* is_intersection */ false);
1489
148
  }
1490
15.2k
  if (type_mask & MAY_BE_CALLABLE) {
1491
62
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE), /* is_intersection */ false);
1492
62
  }
1493
15.2k
  if (type_mask & MAY_BE_OBJECT) {
1494
226
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT), /* is_intersection */ false);
1495
226
  }
1496
15.2k
  if (type_mask & MAY_BE_ARRAY) {
1497
1.54k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY), /* is_intersection */ false);
1498
1.54k
  }
1499
15.2k
  if (type_mask & MAY_BE_STRING) {
1500
6.35k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING), /* is_intersection */ false);
1501
6.35k
  }
1502
15.2k
  if (type_mask & MAY_BE_LONG) {
1503
3.39k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT), /* is_intersection */ false);
1504
3.39k
  }
1505
15.2k
  if (type_mask & MAY_BE_DOUBLE) {
1506
426
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT), /* is_intersection */ false);
1507
426
  }
1508
15.2k
  if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
1509
588
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL), /* is_intersection */ false);
1510
14.6k
  } else if (type_mask & MAY_BE_FALSE) {
1511
140
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE), /* is_intersection */ false);
1512
14.5k
  } else if (type_mask & MAY_BE_TRUE) {
1513
42
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_TRUE), /* is_intersection */ false);
1514
42
  }
1515
15.2k
  if (type_mask & MAY_BE_VOID) {
1516
193
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID), /* is_intersection */ false);
1517
193
  }
1518
15.2k
  if (type_mask & MAY_BE_NEVER) {
1519
18
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NEVER), /* is_intersection */ false);
1520
18
  }
1521
1522
15.2k
  if (type_mask & MAY_BE_NULL) {
1523
1.42k
    bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
1524
1.42k
    bool has_intersection = !str || memchr(ZSTR_VAL(str), '&', ZSTR_LEN(str)) != NULL;
1525
1.42k
    if (!is_union && !has_intersection) {
1526
1.27k
      zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
1527
1.27k
      zend_string_release(str);
1528
1.27k
      return nullable_str;
1529
1.27k
    }
1530
1531
155
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE), /* is_intersection */ false);
1532
155
  }
1533
13.9k
  return str;
1534
15.2k
}
1535
1536
10.5k
ZEND_API zend_string *zend_type_to_string(zend_type type) {
1537
10.5k
  return zend_type_to_string_resolved(type, NULL);
1538
10.5k
}
1539
1540
1.17k
static bool is_generator_compatible_class_type(const zend_string *name) {
1541
1.17k
  return zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_TRAVERSABLE))
1542
897
    || zend_string_equals_literal_ci(name, "Iterator")
1543
686
    || zend_string_equals_literal_ci(name, "Generator");
1544
1.17k
}
1545
1546
static void zend_mark_function_as_generator(void) /* {{{ */
1547
72.7k
{
1548
72.7k
  if (!CG(active_op_array)->function_name) {
1549
84
    zend_error_noreturn(E_COMPILE_ERROR,
1550
84
      "The \"yield\" expression can only be used inside a function");
1551
84
  }
1552
1553
72.6k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
1554
843
    const zend_type return_type = CG(active_op_array)->arg_info[-1].type;
1555
843
    bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_OBJECT) != 0;
1556
843
    if (!valid_type) {
1557
799
      const zend_type *single_type;
1558
1.97k
      ZEND_TYPE_FOREACH(return_type, single_type) {
1559
1.97k
        if (ZEND_TYPE_HAS_NAME(*single_type)
1560
1.17k
            && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) {
1561
767
          valid_type = true;
1562
767
          break;
1563
767
        }
1564
1.97k
      } ZEND_TYPE_FOREACH_END();
1565
799
    }
1566
1567
843
    if (!valid_type) {
1568
32
      zend_string *str = zend_type_to_string(return_type);
1569
32
      zend_error_noreturn(E_COMPILE_ERROR,
1570
32
        "Generator return type must be a supertype of Generator, %s given",
1571
32
        ZSTR_VAL(str));
1572
32
    }
1573
843
  }
1574
1575
72.6k
  CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR;
1576
72.6k
}
1577
/* }}} */
1578
1579
ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal) /* {{{ */
1580
53.0k
{
1581
53.0k
  size_t prop_name_length = 1 + src1_length + 1 + src2_length;
1582
53.0k
  zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
1583
1584
53.0k
  ZSTR_VAL(prop_name)[0] = '\0';
1585
53.0k
  memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
1586
53.0k
  memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
1587
53.0k
  return prop_name;
1588
53.0k
}
1589
/* }}} */
1590
1591
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) /* {{{ */
1592
893k
{
1593
893k
  size_t class_name_len;
1594
893k
  size_t anonclass_src_len;
1595
1596
893k
  *class_name = NULL;
1597
1598
893k
  if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
1599
802k
    *prop_name = ZSTR_VAL(name);
1600
802k
    if (prop_len) {
1601
530k
      *prop_len = ZSTR_LEN(name);
1602
530k
    }
1603
802k
    return SUCCESS;
1604
802k
  }
1605
91.0k
  if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
1606
1.16k
    zend_error(E_NOTICE, "Illegal member variable name");
1607
1.16k
    *prop_name = ZSTR_VAL(name);
1608
1.16k
    if (prop_len) {
1609
1.13k
      *prop_len = ZSTR_LEN(name);
1610
1.13k
    }
1611
1.16k
    return FAILURE;
1612
1.16k
  }
1613
1614
89.9k
  class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
1615
89.9k
  if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
1616
445
    zend_error(E_NOTICE, "Corrupt member variable name");
1617
445
    *prop_name = ZSTR_VAL(name);
1618
445
    if (prop_len) {
1619
313
      *prop_len = ZSTR_LEN(name);
1620
313
    }
1621
445
    return FAILURE;
1622
445
  }
1623
1624
89.4k
  *class_name = ZSTR_VAL(name) + 1;
1625
89.4k
  anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2);
1626
89.4k
  if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) {
1627
17.9k
    class_name_len += anonclass_src_len + 1;
1628
17.9k
  }
1629
89.4k
  *prop_name = ZSTR_VAL(name) + class_name_len + 2;
1630
89.4k
  if (prop_len) {
1631
58.3k
    *prop_len = ZSTR_LEN(name) - class_name_len - 2;
1632
58.3k
  }
1633
89.4k
  return SUCCESS;
1634
89.9k
}
1635
/* }}} */
1636
1637
static bool array_is_const_ex(const zend_array *array, uint32_t *max_checks)
1638
207
{
1639
207
  if (zend_hash_num_elements(array) > *max_checks) {
1640
3
    return false;
1641
3
  }
1642
204
  *max_checks -= zend_hash_num_elements(array);
1643
1644
204
  zval *element;
1645
580
  ZEND_HASH_FOREACH_VAL(array, element) {
1646
580
    if (Z_TYPE_P(element) < IS_ARRAY) {
1647
38
      continue;
1648
150
    } else if (Z_TYPE_P(element) == IS_ARRAY) {
1649
150
      if (!array_is_const_ex(array, max_checks)) {
1650
150
        return false;
1651
150
      }
1652
150
    } else {
1653
0
      return false;
1654
0
    }
1655
580
  } ZEND_HASH_FOREACH_END();
1656
1657
54
  return true;
1658
204
}
1659
1660
static bool array_is_const(const zend_array *array)
1661
57
{
1662
57
  uint32_t max_checks = 50;
1663
57
  return array_is_const_ex(array, &max_checks);
1664
57
}
1665
1666
9.72k
static bool can_ct_eval_const(const zend_constant *c) {
1667
9.72k
  if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
1668
139
    return 0;
1669
139
  }
1670
9.58k
  if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
1671
9.56k
      && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
1672
8.28k
      && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
1673
8.28k
        && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
1674
8.28k
    return 1;
1675
8.28k
  }
1676
1.30k
  if (Z_TYPE(c->value) < IS_ARRAY
1677
1.30k
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1678
16
    return 1;
1679
1.28k
  } else if (Z_TYPE(c->value) == IS_ARRAY
1680
0
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)
1681
0
      && array_is_const(Z_ARR(c->value))) {
1682
0
    return 1;
1683
0
  }
1684
1.28k
  return 0;
1685
1.30k
}
1686
1687
static bool zend_try_ct_eval_const(zval *zv, zend_string *name, bool is_fully_qualified) /* {{{ */
1688
6.33M
{
1689
  /* Substitute true, false and null (including unqualified usage in namespaces)
1690
   * before looking up the possibly namespaced name. */
1691
6.33M
  const char *lookup_name = ZSTR_VAL(name);
1692
6.33M
  size_t lookup_len = ZSTR_LEN(name);
1693
1694
6.33M
  if (!is_fully_qualified) {
1695
6.32M
    zend_get_unqualified_name(name, &lookup_name, &lookup_len);
1696
6.32M
  }
1697
1698
6.33M
  zend_constant *c;
1699
6.33M
  if ((c = zend_get_special_const(lookup_name, lookup_len))) {
1700
29.2k
    ZVAL_COPY_VALUE(zv, &c->value);
1701
29.2k
    return 1;
1702
29.2k
  }
1703
6.30M
  c = zend_hash_find_ptr(EG(zend_constants), name);
1704
6.30M
  if (c && can_ct_eval_const(c)) {
1705
8.29k
    ZVAL_COPY_OR_DUP(zv, &c->value);
1706
8.29k
    return 1;
1707
8.29k
  }
1708
6.30M
  return 0;
1709
6.30M
}
1710
/* }}} */
1711
1712
static inline bool zend_is_scope_known(void) /* {{{ */
1713
35.5k
{
1714
35.5k
  if (!CG(active_op_array)) {
1715
    /* This can only happen when evaluating a default value string. */
1716
0
    return 0;
1717
0
  }
1718
1719
35.5k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
1720
    /* Closures can be rebound to a different scope */
1721
25.3k
    return 0;
1722
25.3k
  }
1723
1724
10.2k
  if (!CG(active_class_entry)) {
1725
    /* The scope is known if we're in a free function (no scope), but not if we're in
1726
     * a file/eval (which inherits including/eval'ing scope). */
1727
1.10k
    return CG(active_op_array)->function_name != NULL;
1728
1.10k
  }
1729
1730
  /* For traits self etc refers to the using class, not the trait itself */
1731
9.12k
  return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
1732
10.2k
}
1733
/* }}} */
1734
1735
static inline bool class_name_refers_to_active_ce(const zend_string *class_name, uint32_t fetch_type) /* {{{ */
1736
106k
{
1737
106k
  if (!CG(active_class_entry)) {
1738
101k
    return 0;
1739
101k
  }
1740
5.03k
  if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
1741
1.09k
    return 1;
1742
1.09k
  }
1743
3.94k
  return fetch_type == ZEND_FETCH_CLASS_DEFAULT
1744
3.04k
    && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
1745
5.03k
}
1746
/* }}} */
1747
1748
uint32_t zend_get_class_fetch_type(const zend_string *name) /* {{{ */
1749
2.60M
{
1750
2.60M
  if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1751
21.7k
    return ZEND_FETCH_CLASS_SELF;
1752
2.58M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT))) {
1753
4.41k
    return ZEND_FETCH_CLASS_PARENT;
1754
2.58M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_STATIC))) {
1755
2.06k
    return ZEND_FETCH_CLASS_STATIC;
1756
2.57M
  } else {
1757
2.57M
    return ZEND_FETCH_CLASS_DEFAULT;
1758
2.57M
  }
1759
2.60M
}
1760
/* }}} */
1761
1762
static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
1763
355k
{
1764
  /* Fully qualified names are always default refs */
1765
355k
  if (name_ast->attr == ZEND_NAME_FQ) {
1766
3.26k
    return ZEND_FETCH_CLASS_DEFAULT;
1767
3.26k
  }
1768
1769
352k
  return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
1770
355k
}
1771
/* }}} */
1772
1773
static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type)
1774
125k
{
1775
125k
  zend_string *class_name = zend_ast_get_str(ast);
1776
125k
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) {
1777
16
    zend_error_noreturn(E_COMPILE_ERROR,
1778
16
      "Cannot use \"%s\" as %s, as it is reserved",
1779
16
      ZSTR_VAL(class_name), type);
1780
16
  }
1781
125k
  return zend_resolve_class_name(class_name, ast->attr);
1782
125k
}
1783
1784
static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
1785
22.7k
{
1786
22.7k
  if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) {
1787
4.30k
    zend_class_entry *ce = CG(active_class_entry);
1788
4.30k
    if (!ce) {
1789
22
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
1790
22
        fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
1791
22
        fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
1792
4.28k
    } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) {
1793
24
      zend_error_noreturn(E_COMPILE_ERROR,
1794
24
        "Cannot use \"parent\" when current class scope has no parent");
1795
24
    }
1796
4.30k
  }
1797
22.7k
}
1798
/* }}} */
1799
1800
static bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */
1801
7.41k
{
1802
7.41k
  uint32_t fetch_type;
1803
7.41k
  const zval *class_name;
1804
1805
7.41k
  if (class_ast->kind != ZEND_AST_ZVAL) {
1806
1.95k
    return 0;
1807
1.95k
  }
1808
1809
5.45k
  class_name = zend_ast_get_zval(class_ast);
1810
1811
5.45k
  if (Z_TYPE_P(class_name) != IS_STRING) {
1812
5
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1813
5
  }
1814
1815
5.45k
  fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name));
1816
5.45k
  zend_ensure_valid_class_fetch_type(fetch_type);
1817
1818
5.45k
  switch (fetch_type) {
1819
517
    case ZEND_FETCH_CLASS_SELF:
1820
517
      if (CG(active_class_entry) && zend_is_scope_known()) {
1821
168
        ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
1822
168
        return 1;
1823
168
      }
1824
349
      return 0;
1825
602
    case ZEND_FETCH_CLASS_PARENT:
1826
602
      if (CG(active_class_entry) && CG(active_class_entry)->parent_name
1827
335
          && zend_is_scope_known()) {
1828
335
        ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name);
1829
335
        return 1;
1830
335
      }
1831
267
      return 0;
1832
451
    case ZEND_FETCH_CLASS_STATIC:
1833
451
      return 0;
1834
3.87k
    case ZEND_FETCH_CLASS_DEFAULT:
1835
3.87k
      ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
1836
3.87k
      return 1;
1837
5.45k
    EMPTY_SWITCH_DEFAULT_CASE()
1838
5.45k
  }
1839
5.45k
}
1840
/* }}} */
1841
1842
/* We don't use zend_verify_const_access because we need to deal with unlinked classes. */
1843
static bool zend_verify_ct_const_access(const zend_class_constant *c, const zend_class_entry *scope)
1844
638
{
1845
638
  if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED) {
1846
15
    return 0;
1847
623
  } else if (c->ce->ce_flags & ZEND_ACC_TRAIT) {
1848
    /* This condition is only met on directly accessing trait constants,
1849
     * because the ce is replaced to the class entry of the composing class
1850
     * on binding. */
1851
1
    return 0;
1852
622
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) {
1853
404
    return 1;
1854
404
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) {
1855
218
    return c->ce == scope;
1856
218
  } else {
1857
0
    zend_class_entry *ce = c->ce;
1858
0
    while (1) {
1859
0
      if (ce == scope) {
1860
0
        return 1;
1861
0
      }
1862
0
      if (!ce->parent) {
1863
0
        break;
1864
0
      }
1865
0
      if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
1866
0
        ce = ce->parent;
1867
0
      } else {
1868
0
        ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name);
1869
0
        if (!ce) {
1870
0
          break;
1871
0
        }
1872
0
      }
1873
0
    }
1874
    /* Reverse case cannot be true during compilation */
1875
0
    return 0;
1876
0
  }
1877
638
}
1878
1879
static bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
1880
106k
{
1881
106k
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
1882
106k
  zend_class_constant *cc;
1883
106k
  zval *c;
1884
1885
106k
  if (class_name_refers_to_active_ce(class_name, fetch_type)) {
1886
1.20k
    cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
1887
104k
  } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1888
644
    const zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name);
1889
644
    if (ce) {
1890
71
      cc = zend_hash_find_ptr(&ce->constants_table, name);
1891
573
    } else {
1892
573
      return 0;
1893
573
    }
1894
104k
  } else {
1895
104k
    return 0;
1896
104k
  }
1897
1898
1.27k
  if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
1899
375
    return 0;
1900
375
  }
1901
1902
899
  if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) {
1903
277
    return 0;
1904
277
  }
1905
1906
622
  c = &cc->value;
1907
1908
  /* Substitute case-sensitive (or lowercase) persistent class constants */
1909
622
  if (Z_TYPE_P(c) < IS_ARRAY) {
1910
377
    ZVAL_COPY_OR_DUP(zv, c);
1911
377
    return 1;
1912
377
  } else if (Z_TYPE_P(c) == IS_ARRAY && array_is_const(Z_ARR_P(c))) {
1913
54
    ZVAL_COPY_OR_DUP(zv, c);
1914
54
    return 1;
1915
54
  }
1916
1917
191
  return 0;
1918
622
}
1919
/* }}} */
1920
1921
static void zend_add_to_list(void *result, void *item) /* {{{ */
1922
208k
{
1923
208k
  void** list = *(void**)result;
1924
208k
  size_t n = 0;
1925
1926
208k
  if (list) {
1927
403k
    while (list[n]) {
1928
264k
      n++;
1929
264k
    }
1930
139k
  }
1931
1932
208k
  list = erealloc(list, sizeof(void*) * (n+2));
1933
1934
208k
  list[n]   = item;
1935
208k
  list[n+1] = NULL;
1936
1937
208k
  *(void**)result = list;
1938
208k
}
1939
/* }}} */
1940
1941
static void zend_do_extended_stmt(znode* result) /* {{{ */
1942
1.09M
{
1943
1.09M
  zend_op *opline;
1944
1945
1.09M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) {
1946
1.09M
    return;
1947
1.09M
  }
1948
1949
0
  opline = get_next_op();
1950
1951
0
  opline->opcode = ZEND_EXT_STMT;
1952
0
  if (result) {
1953
0
    if (result->op_type == IS_CONST) {
1954
0
      Z_TRY_ADDREF(result->u.constant);
1955
0
    }
1956
0
    SET_NODE(opline->op1, result);
1957
0
  }
1958
0
}
1959
/* }}} */
1960
1961
static void zend_do_extended_fcall_begin(void) /* {{{ */
1962
2.64M
{
1963
2.64M
  zend_op *opline;
1964
1965
2.64M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1966
2.64M
    return;
1967
2.64M
  }
1968
1969
0
  opline = get_next_op();
1970
1971
0
  opline->opcode = ZEND_EXT_FCALL_BEGIN;
1972
0
}
1973
/* }}} */
1974
1975
static void zend_do_extended_fcall_end(void) /* {{{ */
1976
2.64M
{
1977
2.64M
  zend_op *opline;
1978
1979
2.64M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1980
2.64M
    return;
1981
2.64M
  }
1982
1983
0
  opline = get_next_op();
1984
1985
0
  opline->opcode = ZEND_EXT_FCALL_END;
1986
0
}
1987
/* }}} */
1988
1989
0
ZEND_API bool zend_is_auto_global_str(const char *name, size_t len) /* {{{ */ {
1990
0
  zend_auto_global *auto_global;
1991
1992
0
  if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) {
1993
0
    if (auto_global->armed) {
1994
0
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1995
0
    }
1996
0
    return 1;
1997
0
  }
1998
0
  return 0;
1999
0
}
2000
/* }}} */
2001
2002
ZEND_API bool zend_is_auto_global(zend_string *name) /* {{{ */
2003
2.10M
{
2004
2.10M
  zend_auto_global *auto_global;
2005
2006
2.10M
  if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) {
2007
4.28k
    if (auto_global->armed) {
2008
185
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
2009
185
    }
2010
4.28k
    return 1;
2011
4.28k
  }
2012
2.10M
  return 0;
2013
2.10M
}
2014
/* }}} */
2015
2016
ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
2017
128
{
2018
128
  zend_auto_global auto_global;
2019
128
  zend_result retval;
2020
2021
128
  auto_global.name = name;
2022
128
  auto_global.auto_global_callback = auto_global_callback;
2023
128
  auto_global.jit = jit;
2024
2025
128
  retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
2026
2027
128
  return retval;
2028
128
}
2029
/* }}} */
2030
2031
ZEND_API void zend_activate_auto_globals(void) /* {{{ */
2032
224k
{
2033
224k
  zend_auto_global *auto_global;
2034
2035
4.04M
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2036
4.04M
    auto_global->armed = auto_global->jit || auto_global->auto_global_callback;
2037
4.04M
  } ZEND_HASH_FOREACH_END();
2038
2039
4.04M
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2040
4.04M
    if (auto_global->armed && !auto_global->jit) {
2041
898k
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
2042
898k
    }
2043
4.04M
  } ZEND_HASH_FOREACH_END();
2044
224k
}
2045
/* }}} */
2046
2047
int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
2048
9.46M
{
2049
9.46M
  zval zv;
2050
9.46M
  int ret;
2051
2052
9.46M
  if (CG(increment_lineno)) {
2053
12.7k
    CG(zend_lineno)++;
2054
12.7k
    CG(increment_lineno) = 0;
2055
12.7k
  }
2056
2057
9.46M
  ret = lex_scan(&zv, elem);
2058
9.46M
  ZEND_ASSERT(!EG(exception) || ret == T_ERROR);
2059
9.46M
  return ret;
2060
2061
9.46M
}
2062
/* }}} */
2063
2064
ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers) /* {{{ */
2065
174k
{
2066
174k
  bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS;
2067
2068
174k
  ce->refcount = 1;
2069
174k
  ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
2070
174k
  ce->ce_flags2 = 0;
2071
2072
174k
  if (CG(compiler_options) & ZEND_COMPILE_GUARDS) {
2073
0
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2074
0
  }
2075
2076
174k
  ce->default_properties_table = NULL;
2077
174k
  ce->default_static_members_table = NULL;
2078
174k
  zend_hash_init(&ce->properties_info, 8, NULL, NULL, persistent_hashes);
2079
174k
  zend_hash_init(&ce->constants_table, 8, NULL, NULL, persistent_hashes);
2080
174k
  zend_hash_init(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes);
2081
2082
174k
  ce->doc_comment = NULL;
2083
2084
174k
  ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
2085
174k
  ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
2086
2087
174k
  ce->default_object_handlers = &std_object_handlers;
2088
174k
  ce->default_properties_count = 0;
2089
174k
  ce->default_static_members_count = 0;
2090
174k
  ce->properties_info_table = NULL;
2091
174k
  ce->attributes = NULL;
2092
174k
  ce->enum_backing_type = IS_UNDEF;
2093
174k
  ce->backed_enum_table = NULL;
2094
2095
174k
  if (nullify_handlers) {
2096
172k
    ce->constructor = NULL;
2097
172k
    ce->destructor = NULL;
2098
172k
    ce->clone = NULL;
2099
172k
    ce->__get = NULL;
2100
172k
    ce->__set = NULL;
2101
172k
    ce->__unset = NULL;
2102
172k
    ce->__isset = NULL;
2103
172k
    ce->__call = NULL;
2104
172k
    ce->__callstatic = NULL;
2105
172k
    ce->__tostring = NULL;
2106
172k
    ce->__serialize = NULL;
2107
172k
    ce->__unserialize = NULL;
2108
172k
    ce->__debugInfo = NULL;
2109
172k
    ce->create_object = NULL;
2110
172k
    ce->get_iterator = NULL;
2111
172k
    ce->iterator_funcs_ptr = NULL;
2112
172k
    ce->arrayaccess_funcs_ptr = NULL;
2113
172k
    ce->get_static_method = NULL;
2114
172k
    ce->parent = NULL;
2115
172k
    ce->parent_name = NULL;
2116
172k
    ce->num_interfaces = 0;
2117
172k
    ce->interfaces = NULL;
2118
172k
    ce->num_traits = 0;
2119
172k
    ce->num_hooked_props = 0;
2120
172k
    ce->num_hooked_prop_variance_checks = 0;
2121
172k
    ce->trait_names = NULL;
2122
172k
    ce->trait_aliases = NULL;
2123
172k
    ce->trait_precedences = NULL;
2124
172k
    ce->serialize = NULL;
2125
172k
    ce->unserialize = NULL;
2126
172k
    if (ce->type == ZEND_INTERNAL_CLASS) {
2127
0
      ce->info.internal.module = NULL;
2128
0
      ce->info.internal.builtin_functions = NULL;
2129
0
    }
2130
172k
  }
2131
174k
}
2132
/* }}} */
2133
2134
ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
2135
0
{
2136
0
  return op_array->vars[EX_VAR_TO_NUM(var)];
2137
0
}
2138
/* }}} */
2139
2140
zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
2141
0
{
2142
0
  zval *left_zv = zend_ast_get_zval(left_ast);
2143
0
  zend_string *left = Z_STR_P(left_zv);
2144
0
  zend_string *right = zend_ast_get_str(right_ast);
2145
2146
0
  zend_string *result;
2147
0
  size_t left_len = ZSTR_LEN(left);
2148
0
  size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
2149
2150
0
  result = zend_string_extend(left, len, 0);
2151
0
  ZSTR_VAL(result)[left_len] = '\\';
2152
0
  memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
2153
0
  ZSTR_VAL(result)[len] = '\0';
2154
0
  zend_string_release_ex(right, 0);
2155
2156
0
  ZVAL_STR(left_zv, result);
2157
0
  return left_ast;
2158
0
}
2159
/* }}} */
2160
2161
zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
2162
1.46k
{
2163
1.46k
  zval *zv = zend_ast_get_zval(ast);
2164
1.46k
  if (Z_TYPE_P(zv) == IS_LONG) {
2165
1.14k
    if (Z_LVAL_P(zv) == 0) {
2166
580
      ZVAL_NEW_STR(zv, ZSTR_INIT_LITERAL("-0", 0));
2167
580
    } else {
2168
565
      ZEND_ASSERT(Z_LVAL_P(zv) > 0);
2169
565
      Z_LVAL_P(zv) *= -1;
2170
565
    }
2171
1.14k
  } else if (Z_TYPE_P(zv) == IS_STRING) {
2172
324
    size_t orig_len = Z_STRLEN_P(zv);
2173
324
    Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0);
2174
324
    memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
2175
324
    Z_STRVAL_P(zv)[0] = '-';
2176
324
  } else {
2177
0
    ZEND_UNREACHABLE();
2178
0
  }
2179
1.46k
  return ast;
2180
1.46k
}
2181
/* }}} */
2182
2183
static void zend_verify_namespace(void) /* {{{ */
2184
447k
{
2185
447k
  if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
2186
44
    zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
2187
44
  }
2188
447k
}
2189
/* }}} */
2190
2191
/* {{{ zend_dirname
2192
   Returns directory name component of path */
2193
ZEND_API size_t zend_dirname(char *path, size_t len)
2194
1.50k
{
2195
1.50k
  char *end = path + len - 1;
2196
1.50k
  unsigned int len_adjust = 0;
2197
2198
#ifdef ZEND_WIN32
2199
  /* Note that on Win32 CWD is per drive (heritage from CP/M).
2200
   * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
2201
   */
2202
  if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
2203
    /* Skip over the drive spec (if any) so as not to change */
2204
    path += 2;
2205
    len_adjust += 2;
2206
    if (2 == len) {
2207
      /* Return "c:" on Win32 for dirname("c:").
2208
       * It would be more consistent to return "c:."
2209
       * but that would require making the string *longer*.
2210
       */
2211
      return len;
2212
    }
2213
  }
2214
#endif
2215
2216
1.50k
  if (len == 0) {
2217
    /* Illegal use of this function */
2218
0
    return 0;
2219
0
  }
2220
2221
  /* Strip trailing slashes */
2222
1.50k
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2223
0
    end--;
2224
0
  }
2225
1.50k
  if (end < path) {
2226
    /* The path only contained slashes */
2227
0
    path[0] = DEFAULT_SLASH;
2228
0
    path[1] = '\0';
2229
0
    return 1 + len_adjust;
2230
0
  }
2231
2232
  /* Strip filename */
2233
16.5k
  while (end >= path && !IS_SLASH_P_EX(end, end == path)) {
2234
15.0k
    end--;
2235
15.0k
  }
2236
1.50k
  if (end < path) {
2237
    /* No slash found, therefore return '.' */
2238
961
    path[0] = '.';
2239
961
    path[1] = '\0';
2240
961
    return 1 + len_adjust;
2241
961
  }
2242
2243
  /* Strip slashes which came before the file name */
2244
1.08k
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2245
540
    end--;
2246
540
  }
2247
540
  if (end < path) {
2248
5
    path[0] = DEFAULT_SLASH;
2249
5
    path[1] = '\0';
2250
5
    return 1 + len_adjust;
2251
5
  }
2252
535
  *(end+1) = '\0';
2253
2254
535
  return (size_t)(end + 1 - path) + len_adjust;
2255
540
}
2256
/* }}} */
2257
2258
static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */
2259
569k
{
2260
569k
  uint_fast8_t factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3;
2261
2262
569k
  switch (type) {
2263
291k
    case BP_VAR_R:
2264
291k
      opline->result_type = IS_TMP_VAR;
2265
291k
      result->op_type = IS_TMP_VAR;
2266
291k
      return;
2267
136k
    case BP_VAR_W:
2268
136k
      opline->opcode += 1 * factor;
2269
136k
      return;
2270
12.8k
    case BP_VAR_RW:
2271
12.8k
      opline->opcode += 2 * factor;
2272
12.8k
      return;
2273
103k
    case BP_VAR_IS:
2274
103k
      opline->result_type = IS_TMP_VAR;
2275
103k
      result->op_type = IS_TMP_VAR;
2276
103k
      opline->opcode += 3 * factor;
2277
103k
      return;
2278
20.4k
    case BP_VAR_FUNC_ARG:
2279
20.4k
      opline->opcode += 4 * factor;
2280
20.4k
      return;
2281
4.37k
    case BP_VAR_UNSET:
2282
4.37k
      opline->opcode += 5 * factor;
2283
4.37k
      return;
2284
569k
    EMPTY_SWITCH_DEFAULT_CASE()
2285
569k
  }
2286
569k
}
2287
/* }}} */
2288
2289
static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
2290
3.39M
{
2291
3.39M
  opline->result_type = IS_VAR;
2292
3.39M
  opline->result.var = get_temporary_variable();
2293
3.39M
  GET_NODE(result, opline->result);
2294
3.39M
}
2295
/* }}} */
2296
2297
static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
2298
22.2M
{
2299
22.2M
  opline->result_type = IS_TMP_VAR;
2300
22.2M
  opline->result.var = get_temporary_variable();
2301
22.2M
  GET_NODE(result, opline->result);
2302
22.2M
}
2303
/* }}} */
2304
2305
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2306
23.2M
{
2307
23.2M
  zend_op *opline = get_next_op();
2308
23.2M
  opline->opcode = opcode;
2309
2310
23.2M
  if (op1 != NULL) {
2311
18.6M
    SET_NODE(opline->op1, op1);
2312
18.6M
  }
2313
2314
23.2M
  if (op2 != NULL) {
2315
1.29M
    SET_NODE(opline->op2, op2);
2316
1.29M
  }
2317
2318
23.2M
  if (result) {
2319
2.99M
    zend_make_var_result(result, opline);
2320
2.99M
  }
2321
23.2M
  return opline;
2322
23.2M
}
2323
/* }}} */
2324
2325
static zend_op *zend_emit_op_tmp(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2326
23.8M
{
2327
23.8M
  zend_op *opline = get_next_op();
2328
23.8M
  opline->opcode = opcode;
2329
2330
23.8M
  if (op1 != NULL) {
2331
6.44M
    SET_NODE(opline->op1, op1);
2332
6.44M
  }
2333
2334
23.8M
  if (op2 != NULL) {
2335
2.94M
    SET_NODE(opline->op2, op2);
2336
2.94M
  }
2337
2338
23.8M
  if (result) {
2339
22.2M
    zend_make_tmp_result(result, opline);
2340
22.2M
  }
2341
2342
23.8M
  return opline;
2343
23.8M
}
2344
/* }}} */
2345
2346
static void zend_emit_tick(void) /* {{{ */
2347
12.6k
{
2348
12.6k
  zend_op *opline;
2349
2350
  /* This prevents a double TICK generated by the parser statement of "declare()" */
2351
12.6k
  if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
2352
1.27k
    return;
2353
1.27k
  }
2354
2355
11.3k
  opline = get_next_op();
2356
2357
11.3k
  opline->opcode = ZEND_TICKS;
2358
11.3k
  opline->extended_value = FC(declarables).ticks;
2359
11.3k
}
2360
/* }}} */
2361
2362
static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
2363
191k
{
2364
191k
  return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
2365
191k
}
2366
/* }}} */
2367
2368
static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
2369
582k
{
2370
582k
  uint32_t opnum = get_next_op_number();
2371
582k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
2372
582k
  opline->op1.opline_num = opnum_target;
2373
582k
  return opnum;
2374
582k
}
2375
/* }}} */
2376
2377
ZEND_API bool zend_is_smart_branch(const zend_op *opline) /* {{{ */
2378
94.1k
{
2379
94.1k
  switch (opline->opcode) {
2380
2.93k
    case ZEND_IS_IDENTICAL:
2381
3.38k
    case ZEND_IS_NOT_IDENTICAL:
2382
22.8k
    case ZEND_IS_EQUAL:
2383
23.7k
    case ZEND_IS_NOT_EQUAL:
2384
46.7k
    case ZEND_IS_SMALLER:
2385
48.2k
    case ZEND_IS_SMALLER_OR_EQUAL:
2386
51.8k
    case ZEND_CASE:
2387
52.7k
    case ZEND_CASE_STRICT:
2388
52.8k
    case ZEND_ISSET_ISEMPTY_CV:
2389
52.9k
    case ZEND_ISSET_ISEMPTY_VAR:
2390
53.3k
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2391
53.5k
    case ZEND_ISSET_ISEMPTY_PROP_OBJ:
2392
53.7k
    case ZEND_ISSET_ISEMPTY_STATIC_PROP:
2393
53.8k
    case ZEND_INSTANCEOF:
2394
54.2k
    case ZEND_TYPE_CHECK:
2395
54.3k
    case ZEND_DEFINED:
2396
54.3k
    case ZEND_IN_ARRAY:
2397
54.4k
    case ZEND_ARRAY_KEY_EXISTS:
2398
54.4k
      return 1;
2399
39.6k
    default:
2400
39.6k
      return 0;
2401
94.1k
  }
2402
94.1k
}
2403
/* }}} */
2404
2405
static inline uint32_t zend_emit_cond_jump(uint8_t opcode, znode *cond, uint32_t opnum_target) /* {{{ */
2406
93.9k
{
2407
93.9k
  uint32_t opnum = get_next_op_number();
2408
93.9k
  zend_op *opline;
2409
2410
93.9k
  if (cond->op_type == IS_TMP_VAR && opnum > 0) {
2411
66.2k
    opline = CG(active_op_array)->opcodes + opnum - 1;
2412
66.2k
    if (opline->result_type == IS_TMP_VAR
2413
65.0k
     && opline->result.var == cond->u.op.var
2414
65.0k
     && zend_is_smart_branch(opline)) {
2415
54.3k
      if (opcode == ZEND_JMPZ) {
2416
34.0k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ;
2417
34.0k
      } else {
2418
20.3k
        ZEND_ASSERT(opcode == ZEND_JMPNZ);
2419
20.3k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ;
2420
20.3k
      }
2421
54.3k
    }
2422
66.2k
  }
2423
93.9k
  opline = zend_emit_op(NULL, opcode, cond, NULL);
2424
93.9k
  opline->op2.opline_num = opnum_target;
2425
93.9k
  return opnum;
2426
93.9k
}
2427
/* }}} */
2428
2429
static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
2430
707k
{
2431
707k
  zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
2432
707k
  switch (opline->opcode) {
2433
567k
    case ZEND_JMP:
2434
567k
      opline->op1.opline_num = opnum_target;
2435
567k
      break;
2436
63.1k
    case ZEND_JMPZ:
2437
79.5k
    case ZEND_JMPNZ:
2438
83.9k
    case ZEND_JMPZ_EX:
2439
88.2k
    case ZEND_JMPNZ_EX:
2440
90.8k
    case ZEND_JMP_SET:
2441
140k
    case ZEND_COALESCE:
2442
140k
    case ZEND_JMP_NULL:
2443
140k
    case ZEND_BIND_INIT_STATIC_OR_JMP:
2444
140k
    case ZEND_JMP_FRAMELESS:
2445
140k
      opline->op2.opline_num = opnum_target;
2446
140k
      break;
2447
707k
    EMPTY_SWITCH_DEFAULT_CASE()
2448
707k
  }
2449
707k
}
2450
/* }}} */
2451
2452
static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
2453
706k
{
2454
706k
  zend_update_jump_target(opnum_jump, get_next_op_number());
2455
706k
}
2456
/* }}} */
2457
2458
static inline zend_op *zend_delayed_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2459
403k
{
2460
403k
  zend_op tmp_opline;
2461
2462
403k
  init_op(&tmp_opline);
2463
2464
403k
  tmp_opline.opcode = opcode;
2465
403k
  if (op1 != NULL) {
2466
403k
    SET_NODE(tmp_opline.op1, op1);
2467
403k
  }
2468
403k
  if (op2 != NULL) {
2469
385k
    SET_NODE(tmp_opline.op2, op2);
2470
385k
  }
2471
403k
  if (result) {
2472
399k
    zend_make_var_result(result, &tmp_opline);
2473
399k
  }
2474
2475
403k
  zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
2476
403k
  return zend_stack_top(&CG(delayed_oplines_stack));
2477
403k
}
2478
/* }}} */
2479
2480
static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
2481
594k
{
2482
594k
  return zend_stack_count(&CG(delayed_oplines_stack));
2483
594k
}
2484
/* }}} */
2485
2486
static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
2487
593k
{
2488
593k
  zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
2489
593k
  uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
2490
2491
593k
  ZEND_ASSERT(count >= offset);
2492
996k
  for (i = offset; i < count; ++i) {
2493
403k
    if (EXPECTED(oplines[i].opcode != ZEND_NOP)) {
2494
392k
      opline = get_next_op();
2495
392k
      memcpy(opline, &oplines[i], sizeof(zend_op));
2496
392k
    } else {
2497
10.9k
      opline = CG(active_op_array)->opcodes + oplines[i].extended_value;
2498
10.9k
    }
2499
403k
  }
2500
2501
593k
  CG(delayed_oplines_stack).top = offset;
2502
593k
  return opline;
2503
593k
}
2504
/* }}} */
2505
2506
static bool zend_ast_kind_is_short_circuited(zend_ast_kind ast_kind)
2507
35.4M
{
2508
35.4M
  switch (ast_kind) {
2509
279k
    case ZEND_AST_DIM:
2510
368k
    case ZEND_AST_PROP:
2511
457k
    case ZEND_AST_NULLSAFE_PROP:
2512
476k
    case ZEND_AST_STATIC_PROP:
2513
722k
    case ZEND_AST_METHOD_CALL:
2514
727k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2515
796k
    case ZEND_AST_STATIC_CALL:
2516
796k
      return 1;
2517
34.6M
    default:
2518
34.6M
      return 0;
2519
35.4M
  }
2520
35.4M
}
2521
2522
static bool zend_ast_is_short_circuited(const zend_ast *ast)
2523
801k
{
2524
801k
  switch (ast->kind) {
2525
136k
    case ZEND_AST_DIM:
2526
168k
    case ZEND_AST_PROP:
2527
180k
    case ZEND_AST_STATIC_PROP:
2528
214k
    case ZEND_AST_METHOD_CALL:
2529
217k
    case ZEND_AST_STATIC_CALL:
2530
217k
      return zend_ast_is_short_circuited(ast->child[0]);
2531
569
    case ZEND_AST_NULLSAFE_PROP:
2532
651
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2533
651
      return 1;
2534
583k
    default:
2535
583k
      return 0;
2536
801k
  }
2537
801k
}
2538
2539
static void zend_assert_not_short_circuited(const zend_ast *ast)
2540
14.2k
{
2541
14.2k
  if (zend_ast_is_short_circuited(ast)) {
2542
36
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain");
2543
36
  }
2544
14.2k
}
2545
2546
/* Mark nodes that are an inner part of a short-circuiting chain.
2547
 * We should not perform a "commit" on them, as it will be performed by the outer-most node.
2548
 * We do this to avoid passing down an argument in various compile functions. */
2549
2550
804k
#define ZEND_SHORT_CIRCUITING_INNER 0x8000
2551
2552
505k
static void zend_short_circuiting_mark_inner(zend_ast *ast) {
2553
505k
  if (zend_ast_kind_is_short_circuited(ast->kind)) {
2554
227k
    ast->attr |= ZEND_SHORT_CIRCUITING_INNER;
2555
227k
  }
2556
505k
}
2557
2558
static uint32_t zend_short_circuiting_checkpoint(void)
2559
35.0M
{
2560
35.0M
  return zend_stack_count(&CG(short_circuiting_opnums));
2561
35.0M
}
2562
2563
static void zend_short_circuiting_commit(uint32_t checkpoint, znode *result, const zend_ast *ast)
2564
34.9M
{
2565
34.9M
  bool is_short_circuited = zend_ast_kind_is_short_circuited(ast->kind)
2566
34.3M
    || ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY;
2567
34.9M
  if (!is_short_circuited) {
2568
34.3M
    ZEND_ASSERT(zend_stack_count(&CG(short_circuiting_opnums)) == checkpoint
2569
34.3M
      && "Short circuiting stack should be empty");
2570
34.3M
    return;
2571
34.3M
  }
2572
2573
577k
  if (ast->attr & ZEND_SHORT_CIRCUITING_INNER) {
2574
    /* Outer-most node will commit. */
2575
131k
    return;
2576
131k
  }
2577
2578
494k
  while (zend_stack_count(&CG(short_circuiting_opnums)) != checkpoint) {
2579
49.0k
    uint32_t opnum = *(uint32_t *) zend_stack_top(&CG(short_circuiting_opnums));
2580
49.0k
    zend_op *opline = &CG(active_op_array)->opcodes[opnum];
2581
49.0k
    opline->op2.opline_num = get_next_op_number();
2582
49.0k
    SET_NODE(opline->result, result);
2583
49.0k
    opline->extended_value |=
2584
49.0k
      ast->kind == ZEND_AST_ISSET ? ZEND_SHORT_CIRCUITING_CHAIN_ISSET :
2585
49.0k
      ast->kind == ZEND_AST_EMPTY ? ZEND_SHORT_CIRCUITING_CHAIN_EMPTY :
2586
48.9k
                                    ZEND_SHORT_CIRCUITING_CHAIN_EXPR;
2587
49.0k
    zend_stack_del_top(&CG(short_circuiting_opnums));
2588
49.0k
  }
2589
445k
}
2590
2591
static void zend_emit_jmp_null(znode *obj_node, uint32_t bp_type)
2592
49.1k
{
2593
49.1k
  uint32_t jmp_null_opnum = get_next_op_number();
2594
49.1k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP_NULL, obj_node, NULL);
2595
49.1k
  if (opline->op1_type == IS_CONST) {
2596
973
    Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
2597
973
  }
2598
49.1k
  if (bp_type == BP_VAR_IS) {
2599
755
    opline->extended_value |= ZEND_JMP_NULL_BP_VAR_IS;
2600
755
  }
2601
49.1k
  zend_stack_push(&CG(short_circuiting_opnums), &jmp_null_opnum);
2602
49.1k
}
2603
2604
static inline bool zend_is_variable_or_call(const zend_ast *ast);
2605
2606
static void zend_compile_memoized_expr(znode *result, zend_ast *expr, uint32_t type) /* {{{ */
2607
268k
{
2608
268k
  const zend_memoize_mode memoize_mode = CG(memoize_mode);
2609
268k
  if (memoize_mode == ZEND_MEMOIZE_COMPILE) {
2610
134k
    znode memoized_result;
2611
2612
    /* Go through normal compilation */
2613
134k
    CG(memoize_mode) = ZEND_MEMOIZE_NONE;
2614
134k
    if (zend_is_variable_or_call(expr)) {
2615
42.4k
      zend_compile_var(result, expr, type, /* by_ref */ false);
2616
92.3k
    } else {
2617
92.3k
      zend_compile_expr(result, expr);
2618
92.3k
    }
2619
134k
    CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
2620
2621
134k
    if (result->op_type == IS_VAR) {
2622
39.0k
      zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL);
2623
95.7k
    } else if (result->op_type == IS_TMP_VAR) {
2624
87.7k
      zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL);
2625
87.7k
    } else {
2626
7.96k
      if (result->op_type == IS_CONST) {
2627
6.73k
        Z_TRY_ADDREF(result->u.constant);
2628
6.73k
      }
2629
7.96k
      memoized_result = *result;
2630
7.96k
    }
2631
2632
134k
    zend_hash_index_update_mem(
2633
134k
      CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode));
2634
134k
  } else if (memoize_mode == ZEND_MEMOIZE_FETCH) {
2635
134k
    const znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr);
2636
134k
    *result = *memoized_result;
2637
134k
    if (result->op_type == IS_CONST) {
2638
6.45k
      Z_TRY_ADDREF(result->u.constant);
2639
6.45k
    }
2640
134k
  } else {
2641
0
    ZEND_UNREACHABLE();
2642
0
  }
2643
268k
}
2644
/* }}} */
2645
2646
static void zend_emit_return_type_check(
2647
    znode *expr, const zend_arg_info *return_info, bool implicit) /* {{{ */
2648
28.3k
{
2649
28.3k
  zend_type type = return_info->type;
2650
28.3k
  if (ZEND_TYPE_IS_SET(type)) {
2651
28.3k
    zend_op *opline;
2652
2653
    /* `return ...;` is illegal in a void function (but `return;` isn't) */
2654
28.3k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) {
2655
4.78k
      if (expr) {
2656
21
        if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
2657
7
          zend_error_noreturn(E_COMPILE_ERROR,
2658
7
            "A void %s must not return a value "
2659
7
            "(did you mean \"return;\" instead of \"return null;\"?)",
2660
7
            CG(active_class_entry) != NULL ? "method" : "function");
2661
14
        } else {
2662
14
          zend_error_noreturn(E_COMPILE_ERROR, "A void %s must not return a value",
2663
14
          CG(active_class_entry) != NULL ? "method" : "function");
2664
14
        }
2665
21
      }
2666
      /* we don't need run-time check */
2667
4.76k
      return;
2668
4.78k
    }
2669
2670
    /* `return` is illegal in a never-returning function */
2671
23.5k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_NEVER)) {
2672
      /* Implicit case handled separately using VERIFY_NEVER_TYPE opcode. */
2673
9
      ZEND_ASSERT(!implicit);
2674
9
      zend_error_noreturn(E_COMPILE_ERROR, "A never-returning %s must not return",
2675
9
        CG(active_class_entry) != NULL ? "method" : "function");
2676
9
    }
2677
2678
23.5k
    if (!expr && !implicit) {
2679
12
      if (ZEND_TYPE_ALLOW_NULL(type)) {
2680
5
        zend_error_noreturn(E_COMPILE_ERROR,
2681
5
          "A %s with return type must return a value "
2682
5
          "(did you mean \"return null;\" instead of \"return;\"?)",
2683
5
          CG(active_class_entry) != NULL ? "method" : "function");
2684
7
      } else {
2685
7
        zend_error_noreturn(E_COMPILE_ERROR,
2686
7
          "A %s with return type must return a value",
2687
7
          CG(active_class_entry) != NULL ? "method" : "function");
2688
7
      }
2689
12
    }
2690
2691
23.5k
    if (expr && ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) {
2692
      /* we don't need run-time check for mixed return type */
2693
494
      return;
2694
494
    }
2695
2696
23.0k
    if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) {
2697
      /* we don't need run-time check */
2698
1.43k
      return;
2699
1.43k
    }
2700
2701
21.6k
    opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
2702
21.6k
    if (expr && expr->op_type == IS_CONST) {
2703
190
      opline->result_type = expr->op_type = IS_TMP_VAR;
2704
190
      opline->result.var = expr->u.op.var = get_temporary_variable();
2705
190
    }
2706
21.6k
  }
2707
28.3k
}
2708
/* }}} */
2709
2710
void zend_emit_final_return(bool return_one) /* {{{ */
2711
1.08M
{
2712
1.08M
  znode zn;
2713
1.08M
  zend_op *ret;
2714
1.08M
  bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
2715
2716
1.08M
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)
2717
22.6k
      && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) {
2718
22.3k
    zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
2719
2720
22.3k
    if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
2721
461
      zend_emit_op(NULL, ZEND_VERIFY_NEVER_TYPE, NULL, NULL);
2722
461
      return;
2723
461
    }
2724
2725
21.8k
    zend_emit_return_type_check(NULL, return_info, true);
2726
21.8k
  }
2727
2728
1.08M
  zn.op_type = IS_CONST;
2729
1.08M
  if (return_one) {
2730
73.2k
    ZVAL_LONG(&zn.u.constant, 1);
2731
1.01M
  } else {
2732
1.01M
    ZVAL_NULL(&zn.u.constant);
2733
1.01M
  }
2734
2735
1.08M
  ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
2736
1.08M
  ret->extended_value = -1;
2737
1.08M
}
2738
/* }}} */
2739
2740
static inline bool zend_is_variable(const zend_ast *ast) /* {{{ */
2741
3.58M
{
2742
3.58M
  return ast->kind == ZEND_AST_VAR
2743
3.45M
    || ast->kind == ZEND_AST_DIM
2744
3.43M
    || ast->kind == ZEND_AST_PROP
2745
3.42M
    || ast->kind == ZEND_AST_NULLSAFE_PROP
2746
3.42M
    || ast->kind == ZEND_AST_STATIC_PROP;
2747
3.58M
}
2748
/* }}} */
2749
2750
static bool zend_propagate_list_refs(zend_ast *ast);
2751
2752
static inline bool zend_is_passable_by_ref(const zend_ast *ast)
2753
3.38M
{
2754
3.38M
  if (zend_is_variable(ast) || ast->kind == ZEND_AST_ASSIGN_REF) {
2755
102k
    return true;
2756
102k
  }
2757
3.28M
  if (ast->kind == ZEND_AST_ASSIGN
2758
4.06k
   && UNEXPECTED(ast->child[0]->kind == ZEND_AST_ARRAY)
2759
33
   && zend_propagate_list_refs(ast->child[0])) {
2760
17
    return true;
2761
17
  }
2762
3.28M
  return false;
2763
3.28M
}
2764
2765
static inline bool zend_is_call(const zend_ast *ast) /* {{{ */
2766
3.81M
{
2767
3.81M
  return ast->kind == ZEND_AST_CALL
2768
3.67M
    || ast->kind == ZEND_AST_METHOD_CALL
2769
3.59M
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
2770
3.59M
    || ast->kind == ZEND_AST_STATIC_CALL
2771
3.59M
    || ast->kind == ZEND_AST_PIPE;
2772
3.81M
}
2773
/* }}} */
2774
2775
static inline bool zend_is_variable_or_call(const zend_ast *ast) /* {{{ */
2776
160k
{
2777
160k
  return zend_is_variable(ast) || zend_is_call(ast);
2778
160k
}
2779
/* }}} */
2780
2781
static inline bool zend_is_unticked_stmt(const zend_ast *ast) /* {{{ */
2782
17.3k
{
2783
17.3k
  return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL
2784
13.7k
    || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP
2785
12.9k
    || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD;
2786
17.3k
}
2787
/* }}} */
2788
2789
static inline bool zend_can_write_to_variable(const zend_ast *ast) /* {{{ */
2790
20.7k
{
2791
20.7k
  while (
2792
23.8k
    ast->kind == ZEND_AST_DIM
2793
22.4k
    || ast->kind == ZEND_AST_PROP
2794
20.7k
  ) {
2795
3.14k
    ast = ast->child[0];
2796
3.14k
  }
2797
2798
20.7k
  return zend_is_variable_or_call(ast) && !zend_ast_is_short_circuited(ast);
2799
20.7k
}
2800
/* }}} */
2801
2802
static inline bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
2803
34.9k
{
2804
34.9k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2805
0
    return 0;
2806
0
  }
2807
2808
34.9k
  return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
2809
34.9k
}
2810
/* }}} */
2811
2812
static inline void zend_handle_numeric_op(znode *node) /* {{{ */
2813
6.40k
{
2814
6.40k
  if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
2815
4.48k
    zend_ulong index;
2816
2817
4.48k
    if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
2818
1.17k
      zval_ptr_dtor(&node->u.constant);
2819
1.17k
      ZVAL_LONG(&node->u.constant, index);
2820
1.17k
    }
2821
4.48k
  }
2822
6.40k
}
2823
/* }}} */
2824
2825
static inline void zend_handle_numeric_dim(const zend_op *opline, znode *dim_node) /* {{{ */
2826
68.8k
{
2827
68.8k
  if (Z_TYPE(dim_node->u.constant) == IS_STRING) {
2828
41.7k
    zend_ulong index;
2829
2830
41.7k
    if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) {
2831
      /* For numeric indexes we also keep the original value to use by ArrayAccess
2832
       * See bug #63217
2833
       */
2834
9.96k
      int c = zend_add_literal(&dim_node->u.constant);
2835
9.96k
      ZEND_ASSERT(opline->op2.constant + 1 == c);
2836
9.96k
      ZVAL_LONG(CT_CONSTANT(opline->op2), index);
2837
9.96k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE;
2838
9.96k
      return;
2839
9.96k
    }
2840
41.7k
  }
2841
68.8k
}
2842
/* }}} */
2843
2844
static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
2845
228k
{
2846
228k
  if (class_node->op_type == IS_CONST) {
2847
126k
    opline->op1_type = IS_CONST;
2848
126k
    opline->op1.constant = zend_add_class_name_literal(
2849
126k
      Z_STR(class_node->u.constant));
2850
126k
  } else {
2851
101k
    SET_NODE(opline->op1, class_node);
2852
101k
  }
2853
228k
}
2854
/* }}} */
2855
2856
static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */
2857
245k
{
2858
245k
  uint32_t fetch_type;
2859
2860
245k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2861
91.3k
    znode name_node;
2862
2863
91.3k
    zend_compile_expr(&name_node, name_ast);
2864
2865
91.3k
    if (name_node.op_type == IS_CONST) {
2866
621
      zend_string *name;
2867
2868
621
      if (Z_TYPE(name_node.u.constant) != IS_STRING) {
2869
6
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
2870
6
      }
2871
2872
615
      name = Z_STR(name_node.u.constant);
2873
615
      fetch_type = zend_get_class_fetch_type(name);
2874
2875
615
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
2876
462
        result->op_type = IS_CONST;
2877
462
        ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ));
2878
462
      } else {
2879
153
        zend_ensure_valid_class_fetch_type(fetch_type);
2880
153
        result->op_type = IS_UNUSED;
2881
153
        result->u.op.num = fetch_type | fetch_flags;
2882
153
      }
2883
2884
615
      zend_string_release_ex(name, 0);
2885
90.7k
    } else {
2886
90.7k
      zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
2887
90.7k
      opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags;
2888
90.7k
    }
2889
91.3k
    return;
2890
91.3k
  }
2891
2892
  /* Fully qualified names are always default refs */
2893
153k
  if (name_ast->attr == ZEND_NAME_FQ) {
2894
10.4k
    result->op_type = IS_CONST;
2895
10.4k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2896
10.4k
    return;
2897
10.4k
  }
2898
2899
143k
  fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
2900
143k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
2901
126k
    result->op_type = IS_CONST;
2902
126k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2903
126k
  } else {
2904
16.3k
    zend_ensure_valid_class_fetch_type(fetch_type);
2905
16.3k
    result->op_type = IS_UNUSED;
2906
16.3k
    result->u.op.num = fetch_type | fetch_flags;
2907
16.3k
  }
2908
143k
}
2909
/* }}} */
2910
2911
static zend_result zend_try_compile_cv(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
2912
1.01M
{
2913
1.01M
  zend_ast *name_ast = ast->child[0];
2914
1.01M
  if (name_ast->kind == ZEND_AST_ZVAL) {
2915
944k
    zval *zv = zend_ast_get_zval(name_ast);
2916
944k
    zend_string *name;
2917
2918
944k
    if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
2919
930k
      name = zval_make_interned_string(zv);
2920
930k
    } else {
2921
14.4k
      name = zend_new_interned_string(zval_get_string_func(zv));
2922
14.4k
    }
2923
2924
944k
    if (zend_is_auto_global(name)) {
2925
817
      return FAILURE;
2926
817
    }
2927
2928
944k
    if (!CG(context).has_assigned_to_http_response_header && zend_string_equals_literal(name, "http_response_header")) {
2929
111
      if (type == BP_VAR_R) {
2930
83
        zend_error(E_DEPRECATED,
2931
83
          "The predefined locally scoped $http_response_header variable is deprecated,"
2932
83
          " call http_get_last_response_headers() instead");
2933
83
      } else if (type == BP_VAR_W) {
2934
17
        CG(context).has_assigned_to_http_response_header = true;
2935
17
      }
2936
111
    }
2937
2938
944k
    result->op_type = IS_CV;
2939
944k
    result->u.op.var = lookup_cv(name);
2940
2941
944k
    if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) {
2942
14.4k
      zend_string_release_ex(name, 0);
2943
14.4k
    }
2944
2945
944k
    return SUCCESS;
2946
944k
  }
2947
2948
72.7k
  return FAILURE;
2949
1.01M
}
2950
/* }}} */
2951
2952
static zend_op *zend_compile_simple_var_no_cv(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2953
163k
{
2954
163k
  zend_ast *name_ast = ast->child[0];
2955
163k
  znode name_node;
2956
163k
  zend_op *opline;
2957
2958
163k
  zend_compile_expr(&name_node, name_ast);
2959
163k
  if (name_node.op_type == IS_CONST) {
2960
93.1k
    convert_to_string(&name_node.u.constant);
2961
93.1k
  }
2962
2963
163k
  if (delayed) {
2964
8.76k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2965
155k
  } else {
2966
155k
    opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2967
155k
  }
2968
2969
163k
  if (name_node.op_type == IS_CONST &&
2970
93.1k
      zend_is_auto_global(Z_STR(name_node.u.constant))) {
2971
2972
785
    opline->extended_value = ZEND_FETCH_GLOBAL;
2973
162k
  } else {
2974
    // TODO: Have a test case for this?
2975
162k
    if (name_node.op_type == IS_CONST
2976
92.4k
      && type == BP_VAR_R
2977
91.6k
      && zend_string_equals_literal(Z_STR(name_node.u.constant), "http_response_header")) {
2978
100
      zend_error(E_DEPRECATED,
2979
100
        "The predefined locally scoped $http_response_header variable is deprecated,"
2980
100
        " call http_get_last_response_headers() instead");
2981
100
    }
2982
162k
    opline->extended_value = ZEND_FETCH_LOCAL;
2983
162k
  }
2984
2985
163k
  zend_adjust_for_fetch_type(opline, result, type);
2986
163k
  return opline;
2987
163k
}
2988
/* }}} */
2989
2990
static bool is_this_fetch(const zend_ast *ast) /* {{{ */
2991
1.55M
{
2992
1.55M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2993
1.32M
    const zval *name = zend_ast_get_zval(ast->child[0]);
2994
1.32M
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals(Z_STR_P(name), ZSTR_KNOWN(ZEND_STR_THIS));
2995
1.32M
  }
2996
2997
228k
  return 0;
2998
1.55M
}
2999
/* }}} */
3000
3001
static bool is_globals_fetch(const zend_ast *ast)
3002
5.30M
{
3003
5.30M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
3004
1.40M
    const zval *name = zend_ast_get_zval(ast->child[0]);
3005
1.40M
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "GLOBALS");
3006
1.40M
  }
3007
3008
3.89M
  return 0;
3009
5.30M
}
3010
3011
static bool is_global_var_fetch(const zend_ast *ast)
3012
425k
{
3013
425k
  return ast->kind == ZEND_AST_DIM && is_globals_fetch(ast->child[0]);
3014
425k
}
3015
3016
static bool this_guaranteed_exists(void) /* {{{ */
3017
19.1k
{
3018
19.1k
  const zend_oparray_context *ctx = &CG(context);
3019
79.1k
  while (ctx) {
3020
    /* Instance methods always have a $this.
3021
     * This also includes closures that have a scope and use $this. */
3022
79.1k
    const zend_op_array *op_array = ctx->op_array;
3023
79.1k
    if (op_array->fn_flags & ZEND_ACC_STATIC) {
3024
77
      return false;
3025
79.1k
    } else if (op_array->scope) {
3026
10.8k
      return true;
3027
68.2k
    } else if (!(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
3028
8.26k
      return false;
3029
8.26k
    }
3030
60.0k
    ctx = ctx->prev;
3031
60.0k
  }
3032
0
  return false;
3033
19.1k
}
3034
/* }}} */
3035
3036
static zend_op *zend_compile_simple_var(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
3037
986k
{
3038
986k
  if (is_this_fetch(ast)) {
3039
6.45k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL);
3040
6.45k
    if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
3041
6.34k
      opline->result_type = IS_TMP_VAR;
3042
6.34k
      result->op_type = IS_TMP_VAR;
3043
6.34k
    }
3044
6.45k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3045
6.45k
    return opline;
3046
980k
  } else if (is_globals_fetch(ast)) {
3047
2.62k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_GLOBALS, NULL, NULL);
3048
2.62k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3049
2.32k
      opline->result_type = IS_TMP_VAR;
3050
2.32k
      result->op_type = IS_TMP_VAR;
3051
2.32k
    }
3052
2.62k
    return opline;
3053
977k
  } else if (zend_try_compile_cv(result, ast, type) == FAILURE) {
3054
72.0k
    return zend_compile_simple_var_no_cv(result, ast, type, delayed);
3055
72.0k
  }
3056
905k
  return NULL;
3057
986k
}
3058
/* }}} */
3059
3060
static void zend_separate_if_call_and_write(znode *node, const zend_ast *ast, uint32_t type) /* {{{ */
3061
369k
{
3062
369k
  if (type != BP_VAR_R
3063
248k
   && type != BP_VAR_IS
3064
   /* Whether a FUNC_ARG is R may only be determined at runtime. */
3065
150k
   && type != BP_VAR_FUNC_ARG
3066
131k
   && zend_is_call(ast)) {
3067
40.1k
    if (node->op_type == IS_VAR) {
3068
40.1k
      zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
3069
40.1k
      opline->result_type = IS_VAR;
3070
40.1k
      opline->result.var = opline->op1.var;
3071
40.1k
    } else {
3072
16
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3073
16
    }
3074
40.1k
  }
3075
369k
}
3076
/* }}} */
3077
3078
static inline void zend_emit_assign_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3079
5.99k
{
3080
5.99k
  znode dummy_node;
3081
5.99k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
3082
5.99k
    zend_ast_create_znode(value_node));
3083
5.99k
  zend_compile_expr(&dummy_node, assign_ast);
3084
5.99k
  zend_do_free(&dummy_node);
3085
5.99k
}
3086
/* }}} */
3087
3088
static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
3089
268k
{
3090
268k
  zend_ast *var_ast = ast->child[0];
3091
268k
  zend_ast *dim_ast = ast->child[1];
3092
268k
  zend_op *opline;
3093
3094
268k
  znode var_node, dim_node;
3095
3096
268k
  if (is_globals_fetch(var_ast)) {
3097
2.39k
    if (dim_ast == NULL) {
3098
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot append to $GLOBALS");
3099
7
    }
3100
3101
2.38k
    zend_compile_expr(&dim_node, dim_ast);
3102
2.38k
    if (dim_node.op_type == IS_CONST) {
3103
1.90k
      convert_to_string(&dim_node.u.constant);
3104
1.90k
    }
3105
3106
2.38k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &dim_node, NULL);
3107
2.38k
    opline->extended_value = ZEND_FETCH_GLOBAL;
3108
2.38k
    zend_adjust_for_fetch_type(opline, result, type);
3109
2.38k
    return opline;
3110
265k
  } else {
3111
265k
    zend_short_circuiting_mark_inner(var_ast);
3112
265k
    opline = zend_delayed_compile_var(&var_node, var_ast, type, false);
3113
265k
    if (opline) {
3114
120k
      if (type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) {
3115
3.74k
        opline->extended_value |= ZEND_FETCH_DIM_WRITE;
3116
116k
      } else if (opline->opcode == ZEND_FETCH_DIM_W
3117
73.7k
          || opline->opcode == ZEND_FETCH_DIM_RW
3118
72.3k
          || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3119
64.9k
          || opline->opcode == ZEND_FETCH_DIM_UNSET) {
3120
52.1k
        opline->extended_value = ZEND_FETCH_DIM_DIM;
3121
52.1k
      }
3122
120k
    }
3123
265k
  }
3124
3125
265k
  zend_separate_if_call_and_write(&var_node, var_ast, type);
3126
3127
265k
  if (dim_ast == NULL) {
3128
10.4k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3129
235
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
3130
235
    }
3131
10.2k
    if (type == BP_VAR_UNSET) {
3132
8
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
3133
8
    }
3134
10.2k
    dim_node.op_type = IS_UNUSED;
3135
255k
  } else {
3136
255k
    zend_compile_expr(&dim_node, dim_ast);
3137
255k
  }
3138
3139
265k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
3140
265k
  zend_adjust_for_fetch_type(opline, result, type);
3141
265k
  if (by_ref) {
3142
12.0k
    opline->extended_value = ZEND_FETCH_DIM_REF;
3143
12.0k
  }
3144
3145
265k
  if (dim_node.op_type == IS_CONST) {
3146
62.9k
    zend_handle_numeric_dim(opline, &dim_node);
3147
62.9k
  }
3148
265k
  return opline;
3149
265k
}
3150
3151
static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3152
141k
{
3153
141k
  uint32_t offset = zend_delayed_compile_begin();
3154
141k
  zend_delayed_compile_dim(result, ast, type, by_ref);
3155
141k
  return zend_delayed_compile_end(offset);
3156
141k
}
3157
/* }}} */
3158
3159
static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3160
120k
{
3161
120k
  zend_ast *obj_ast = ast->child[0];
3162
120k
  zend_ast *prop_ast = ast->child[1];
3163
3164
120k
  znode obj_node, prop_node;
3165
120k
  zend_op *opline;
3166
120k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_PROP;
3167
3168
120k
  if (is_this_fetch(obj_ast)) {
3169
17.8k
    if (this_guaranteed_exists()) {
3170
9.59k
      obj_node.op_type = IS_UNUSED;
3171
9.59k
    } else {
3172
8.25k
      opline = zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
3173
8.25k
      if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
3174
8.20k
        opline->result_type = IS_TMP_VAR;
3175
8.20k
        obj_node.op_type = IS_TMP_VAR;
3176
8.20k
      }
3177
8.25k
    }
3178
17.8k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3179
3180
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
3181
     * check for a nullsafe access. */
3182
102k
  } else {
3183
102k
    zend_short_circuiting_mark_inner(obj_ast);
3184
102k
    opline = zend_delayed_compile_var(&obj_node, obj_ast, type, false);
3185
102k
    if (opline && (opline->opcode == ZEND_FETCH_DIM_W
3186
21.0k
        || opline->opcode == ZEND_FETCH_DIM_RW
3187
20.8k
        || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3188
20.7k
        || opline->opcode == ZEND_FETCH_DIM_UNSET)) {
3189
1.14k
      opline->extended_value = ZEND_FETCH_DIM_OBJ;
3190
1.14k
    }
3191
3192
102k
    zend_separate_if_call_and_write(&obj_node, obj_ast, type);
3193
102k
    if (nullsafe) {
3194
46.3k
      if (obj_node.op_type == IS_TMP_VAR) {
3195
        /* Flush delayed oplines */
3196
13.2k
        zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
3197
13.2k
        uint32_t var = obj_node.u.op.var;
3198
13.2k
        uint32_t count = zend_stack_count(&CG(delayed_oplines_stack));
3199
13.2k
        uint32_t i = count;
3200
3201
46.5k
        while (i > 0 && oplines[i-1].result_type == IS_TMP_VAR && oplines[i-1].result.var == var) {
3202
35.0k
          i--;
3203
35.0k
          if (oplines[i].op1_type == IS_TMP_VAR) {
3204
33.3k
            var = oplines[i].op1.var;
3205
33.3k
          } else {
3206
1.70k
            break;
3207
1.70k
          }
3208
35.0k
        }
3209
48.2k
        for (; i < count; ++i) {
3210
35.0k
          if (oplines[i].opcode != ZEND_NOP) {
3211
10.9k
            opline = get_next_op();
3212
10.9k
            memcpy(opline, &oplines[i], sizeof(zend_op));
3213
10.9k
            oplines[i].opcode = ZEND_NOP;
3214
10.9k
            oplines[i].extended_value = opline - CG(active_op_array)->opcodes;
3215
10.9k
          }
3216
35.0k
        }
3217
13.2k
      }
3218
46.3k
      zend_emit_jmp_null(&obj_node, type);
3219
46.3k
    }
3220
102k
  }
3221
3222
120k
  zend_compile_expr(&prop_node, prop_ast);
3223
3224
120k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
3225
120k
  if (opline->op2_type == IS_CONST) {
3226
117k
    convert_to_string(CT_CONSTANT(opline->op2));
3227
117k
    zend_string_hash_val(Z_STR_P(CT_CONSTANT(opline->op2)));
3228
117k
    opline->extended_value = zend_alloc_cache_slots(3);
3229
117k
  }
3230
3231
120k
  zend_adjust_for_fetch_type(opline, result, type);
3232
3233
120k
  return opline;
3234
120k
}
3235
/* }}} */
3236
3237
static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3238
82.0k
{
3239
82.0k
  uint32_t offset = zend_delayed_compile_begin();
3240
82.0k
  zend_op *opline = zend_delayed_compile_prop(result, ast, type);
3241
82.0k
  if (by_ref) { /* shared with cache_slot */
3242
2.77k
    opline->extended_value |= ZEND_FETCH_REF;
3243
2.77k
  }
3244
82.0k
  return zend_delayed_compile_end(offset);
3245
82.0k
}
3246
/* }}} */
3247
3248
static zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref, bool delayed) /* {{{ */
3249
17.4k
{
3250
17.4k
  zend_ast *class_ast = ast->child[0];
3251
17.4k
  zend_ast *prop_ast = ast->child[1];
3252
3253
17.4k
  znode class_node, prop_node;
3254
17.4k
  zend_op *opline;
3255
3256
17.4k
  zend_short_circuiting_mark_inner(class_ast);
3257
17.4k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
3258
3259
17.4k
  zend_compile_expr(&prop_node, prop_ast);
3260
3261
17.4k
  if (delayed) {
3262
6.57k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3263
10.8k
  } else {
3264
10.8k
    opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3265
10.8k
  }
3266
17.4k
  if (opline->op1_type == IS_CONST) {
3267
14.8k
    convert_to_string(CT_CONSTANT(opline->op1));
3268
14.8k
    opline->extended_value = zend_alloc_cache_slots(3);
3269
14.8k
  }
3270
17.4k
  if (class_node.op_type == IS_CONST) {
3271
10.5k
    opline->op2_type = IS_CONST;
3272
10.5k
    opline->op2.constant = zend_add_class_name_literal(
3273
10.5k
      Z_STR(class_node.u.constant));
3274
10.5k
    if (opline->op1_type != IS_CONST) {
3275
2.06k
      opline->extended_value = zend_alloc_cache_slot();
3276
2.06k
    }
3277
10.5k
  } else {
3278
6.84k
    SET_NODE(opline->op2, &class_node);
3279
6.84k
  }
3280
3281
17.4k
  if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */
3282
1.62k
    opline->extended_value |= ZEND_FETCH_REF;
3283
1.62k
  }
3284
3285
17.4k
  zend_adjust_for_fetch_type(opline, result, type);
3286
17.4k
  return opline;
3287
17.4k
}
3288
/* }}} */
3289
3290
6.26k
static void zend_verify_list_assign_target(const zend_ast *var_ast, zend_ast_attr array_style) /* {{{ */ {
3291
6.26k
  if (var_ast->kind == ZEND_AST_ARRAY) {
3292
350
    if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) {
3293
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead");
3294
5
    }
3295
345
    if (array_style != var_ast->attr) {
3296
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()");
3297
5
    }
3298
5.91k
  } else if (!zend_can_write_to_variable(var_ast)) {
3299
104
    zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values");
3300
104
  }
3301
6.26k
}
3302
/* }}} */
3303
3304
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node);
3305
3306
/* Propagate refs used on leaf elements to the surrounding list() structures. */
3307
3.84k
static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
3308
3.84k
  const zend_ast_list *list = zend_ast_get_list(ast);
3309
3.84k
  bool has_refs = false;
3310
3.84k
  uint32_t i;
3311
3312
18.3k
  for (i = 0; i < list->children; ++i) {
3313
14.4k
    zend_ast *elem_ast = list->child[i];
3314
3315
14.4k
    if (elem_ast) {
3316
6.89k
      zend_ast *var_ast = elem_ast->child[0];
3317
6.89k
      if (var_ast->kind == ZEND_AST_ARRAY) {
3318
351
        elem_ast->attr = zend_propagate_list_refs(var_ast);
3319
351
      }
3320
6.89k
      has_refs |= elem_ast->attr;
3321
6.89k
    }
3322
14.4k
  }
3323
3324
3.84k
  return has_refs;
3325
3.84k
}
3326
/* }}} */
3327
3328
static bool list_is_keyed(const zend_ast_list *list)
3329
3.49k
{
3330
3.86k
  for (uint32_t i = 0; i < list->children; i++) {
3331
3.83k
    const zend_ast *child = list->child[i];
3332
3.83k
    if (child) {
3333
3.47k
      return child->kind == ZEND_AST_ARRAY_ELEM && child->child[1] != NULL;
3334
3.47k
    }
3335
3.83k
  }
3336
24
  return false;
3337
3.49k
}
3338
3339
static void zend_compile_list_assign(
3340
    znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style, uint32_t type) /* {{{ */
3341
3.49k
{
3342
3.49k
  zend_ast_list *list = zend_ast_get_list(ast);
3343
3.49k
  uint32_t i;
3344
3.49k
  bool has_elems = false;
3345
3.49k
  bool is_keyed = list_is_keyed(list);
3346
3347
3.49k
  if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
3348
214
    zval_make_interned_string(&expr_node->u.constant);
3349
214
  }
3350
3351
10.3k
  for (i = 0; i < list->children; ++i) {
3352
6.87k
    zend_ast *elem_ast = list->child[i];
3353
6.87k
    zend_ast *var_ast, *key_ast;
3354
6.87k
    znode fetch_result, dim_node;
3355
6.87k
    zend_op *opline;
3356
3357
6.87k
    if (elem_ast == NULL) {
3358
583
      if (is_keyed) {
3359
11
        zend_error(E_COMPILE_ERROR,
3360
11
          "Cannot use empty array entries in keyed array assignment");
3361
572
      } else {
3362
572
        continue;
3363
572
      }
3364
583
    }
3365
3366
6.29k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
3367
10
      zend_error(E_COMPILE_ERROR,
3368
10
          "Spread operator is not supported in assignments");
3369
10
    }
3370
3371
6.29k
    var_ast = elem_ast->child[0];
3372
6.29k
    key_ast = elem_ast->child[1];
3373
6.29k
    has_elems = true;
3374
3375
6.29k
    if (is_keyed) {
3376
884
      if (key_ast == NULL) {
3377
7
        zend_error(E_COMPILE_ERROR,
3378
7
          "Cannot mix keyed and unkeyed array entries in assignments");
3379
7
      }
3380
3381
884
      zend_compile_expr(&dim_node, key_ast);
3382
5.41k
    } else {
3383
5.41k
      if (key_ast != NULL) {
3384
7
        zend_error(E_COMPILE_ERROR,
3385
7
          "Cannot mix keyed and unkeyed array entries in assignments");
3386
7
      }
3387
3388
5.41k
      dim_node.op_type = IS_CONST;
3389
5.41k
      ZVAL_LONG(&dim_node.u.constant, i);
3390
5.41k
    }
3391
3392
6.29k
    if (expr_node->op_type == IS_CONST) {
3393
605
      Z_TRY_ADDREF(expr_node->u.constant);
3394
605
    }
3395
3396
6.29k
    zend_verify_list_assign_target(var_ast, array_style);
3397
3398
6.29k
    opline = zend_emit_op(&fetch_result,
3399
6.29k
      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);
3400
6.29k
    if (opline->opcode == ZEND_FETCH_LIST_R) {
3401
4.49k
      opline->result_type = IS_TMP_VAR;
3402
4.49k
      fetch_result.op_type = IS_TMP_VAR;
3403
4.49k
    }
3404
3405
6.29k
    if (dim_node.op_type == IS_CONST) {
3406
5.95k
      zend_handle_numeric_dim(opline, &dim_node);
3407
5.95k
    }
3408
3409
6.29k
    if (elem_ast->attr) {
3410
1.64k
      zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL);
3411
1.64k
    }
3412
6.29k
    if (var_ast->kind == ZEND_AST_ARRAY) {
3413
340
      zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr, type);
3414
5.95k
    } else if (elem_ast->attr) {
3415
1.58k
      zend_emit_assign_ref_znode(var_ast, &fetch_result);
3416
4.37k
    } else {
3417
4.37k
      zend_emit_assign_znode(var_ast, &fetch_result);
3418
4.37k
    }
3419
6.29k
  }
3420
3421
3.49k
  if (has_elems == 0) {
3422
24
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
3423
24
  }
3424
3425
3.47k
  if (result) {
3426
289
    if ((type == BP_VAR_R || type == BP_VAR_IS) && expr_node->op_type == IS_VAR) {
3427
      /* Deref. */
3428
124
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, expr_node, NULL);
3429
165
    } else {
3430
165
      *result = *expr_node;
3431
165
    }
3432
3.18k
  } else {
3433
3.18k
    zend_do_free(expr_node);
3434
3.18k
  }
3435
3.47k
}
3436
/* }}} */
3437
3438
static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
3439
445k
{
3440
445k
  if (ast->kind == ZEND_AST_CALL || ast->kind == ZEND_AST_PIPE) {
3441
101
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
3442
101
  }
3443
445k
  if (
3444
445k
    ast->kind == ZEND_AST_METHOD_CALL
3445
445k
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
3446
445k
    || ast->kind == ZEND_AST_STATIC_CALL
3447
445k
  ) {
3448
21
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
3449
21
  }
3450
445k
  if (zend_ast_is_short_circuited(ast)) {
3451
21
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use nullsafe operator in write context");
3452
21
  }
3453
445k
  if (is_globals_fetch(ast)) {
3454
39
    zend_error_noreturn(E_COMPILE_ERROR,
3455
39
      "$GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax");
3456
39
  }
3457
445k
}
3458
/* }}} */
3459
3460
/* Detects $a... = $a pattern */
3461
static bool zend_is_assign_to_self(const zend_ast *var_ast, const zend_ast *expr_ast) /* {{{ */
3462
61.5k
{
3463
61.5k
  if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
3464
57.3k
    return 0;
3465
57.3k
  }
3466
3467
9.82k
  while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
3468
5.62k
    var_ast = var_ast->child[0];
3469
5.62k
  }
3470
3471
4.20k
  if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
3472
905
    return 0;
3473
905
  }
3474
3475
3.29k
  {
3476
3.29k
    zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
3477
3.29k
    zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
3478
3.29k
    bool result = zend_string_equals(name1, name2);
3479
3.29k
    zend_string_release_ex(name1, 0);
3480
3.29k
    zend_string_release_ex(name2, 0);
3481
3.29k
    return result;
3482
4.20k
  }
3483
4.20k
}
3484
/* }}} */
3485
3486
static void zend_compile_expr_with_potential_assign_to_self(
3487
61.5k
    znode *expr_node, zend_ast *expr_ast, const zend_ast *var_ast) {
3488
61.5k
  if (zend_is_assign_to_self(var_ast, expr_ast) && !is_this_fetch(expr_ast)) {
3489
    /* $a[0] = $a should evaluate the right $a first */
3490
702
    znode cv_node;
3491
3492
702
    if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3493
57
      zend_compile_simple_var_no_cv(expr_node, expr_ast, BP_VAR_R, false);
3494
645
    } else {
3495
645
      zend_emit_op_tmp(expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3496
645
    }
3497
60.8k
  } else {
3498
60.8k
    zend_compile_expr(expr_node, expr_ast);
3499
60.8k
  }
3500
61.5k
}
3501
3502
static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type) /* {{{ */
3503
264k
{
3504
264k
  zend_ast *var_ast = ast->child[0];
3505
264k
  zend_ast *expr_ast = ast->child[1];
3506
3507
264k
  znode var_node, expr_node;
3508
264k
  zend_op *opline;
3509
264k
  uint32_t offset;
3510
264k
  if (is_this_fetch(var_ast)) {
3511
10
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3512
10
  }
3513
3514
264k
  zend_ensure_writable_variable(var_ast);
3515
3516
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3517
264k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3518
264k
  switch (kind) {
3519
234k
    case ZEND_AST_VAR:
3520
234k
      offset = zend_delayed_compile_begin();
3521
234k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, false);
3522
234k
      zend_compile_expr(&expr_node, expr_ast);
3523
234k
      zend_delayed_compile_end(offset);
3524
234k
      CG(zend_lineno) = zend_ast_get_lineno(var_ast);
3525
234k
      zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node);
3526
234k
      return;
3527
3.19k
    case ZEND_AST_STATIC_PROP:
3528
3.19k
      offset = zend_delayed_compile_begin();
3529
3.19k
      zend_delayed_compile_var(result, var_ast, BP_VAR_W, false);
3530
3.19k
      zend_compile_expr(&expr_node, expr_ast);
3531
3532
3.19k
      opline = zend_delayed_compile_end(offset);
3533
3.19k
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
3534
3.19k
      opline->result_type = IS_TMP_VAR;
3535
3.19k
      result->op_type = IS_TMP_VAR;
3536
3537
3.19k
      zend_emit_op_data(&expr_node);
3538
3.19k
      return;
3539
12.0k
    case ZEND_AST_DIM:
3540
12.0k
      offset = zend_delayed_compile_begin();
3541
12.0k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_W, /* by_ref */ false);
3542
12.0k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3543
3544
12.0k
      opline = zend_delayed_compile_end(offset);
3545
12.0k
      opline->opcode = ZEND_ASSIGN_DIM;
3546
12.0k
      opline->result_type = IS_TMP_VAR;
3547
12.0k
      result->op_type = IS_TMP_VAR;
3548
3549
12.0k
      opline = zend_emit_op_data(&expr_node);
3550
12.0k
      return;
3551
11.5k
    case ZEND_AST_PROP:
3552
11.5k
    case ZEND_AST_NULLSAFE_PROP:
3553
11.5k
      offset = zend_delayed_compile_begin();
3554
11.5k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
3555
11.5k
      zend_compile_expr(&expr_node, expr_ast);
3556
3557
11.5k
      opline = zend_delayed_compile_end(offset);
3558
11.5k
      opline->opcode = ZEND_ASSIGN_OBJ;
3559
11.5k
      opline->result_type = IS_TMP_VAR;
3560
11.5k
      result->op_type = IS_TMP_VAR;
3561
3562
11.5k
      zend_emit_op_data(&expr_node);
3563
11.5k
      return;
3564
2.87k
    case ZEND_AST_ARRAY:
3565
2.87k
      if (zend_propagate_list_refs(var_ast)) {
3566
1.38k
        if (!zend_is_variable_or_call(expr_ast)) {
3567
27
          zend_error_noreturn(E_COMPILE_ERROR,
3568
27
            "Cannot assign reference to non referenceable value");
3569
1.35k
        } else {
3570
1.35k
          zend_assert_not_short_circuited(expr_ast);
3571
1.35k
        }
3572
3573
1.35k
        zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
3574
        /* MAKE_REF is usually not necessary for CVs. However, if there are
3575
         * self-assignments, this forces the RHS to evaluate first. */
3576
1.35k
        zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
3577
1.49k
      } else {
3578
1.49k
        if (expr_ast->kind == ZEND_AST_VAR) {
3579
          /* list($a, $b) = $a should evaluate the right $a first */
3580
351
          znode cv_node;
3581
3582
351
          if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3583
125
            zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, false);
3584
226
          } else {
3585
226
            zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3586
226
          }
3587
1.14k
        } else {
3588
1.14k
          zend_compile_expr(&expr_node, expr_ast);
3589
1.14k
        }
3590
1.49k
      }
3591
3592
2.84k
      zend_compile_list_assign(!stmt ? result : NULL, var_ast, &expr_node, var_ast->attr, type);
3593
2.84k
      if (stmt) {
3594
2.10k
        result->op_type = IS_UNUSED;
3595
2.10k
      }
3596
2.84k
      return;
3597
0
    EMPTY_SWITCH_DEFAULT_CASE();
3598
264k
  }
3599
264k
}
3600
/* }}} */
3601
3602
static void zend_compile_assign_ref(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3603
10.6k
{
3604
10.6k
  zend_ast *target_ast = ast->child[0];
3605
10.6k
  zend_ast *source_ast = ast->child[1];
3606
3607
10.6k
  znode target_node, source_node;
3608
10.6k
  zend_op *opline;
3609
10.6k
  uint32_t offset, flags;
3610
3611
10.6k
  if (is_this_fetch(target_ast)) {
3612
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3613
5
  }
3614
10.6k
  zend_ensure_writable_variable(target_ast);
3615
10.6k
  zend_assert_not_short_circuited(source_ast);
3616
10.6k
  if (is_globals_fetch(source_ast)) {
3617
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot acquire reference to $GLOBALS");
3618
5
  }
3619
3620
10.6k
  offset = zend_delayed_compile_begin();
3621
10.6k
  zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, true);
3622
10.6k
  zend_compile_var(&source_node, source_ast, BP_VAR_W, true);
3623
3624
10.6k
  if ((target_ast->kind != ZEND_AST_VAR
3625
7.94k
    || target_ast->child[0]->kind != ZEND_AST_ZVAL)
3626
3.56k
   && source_ast->kind != ZEND_AST_ZNODE
3627
2.39k
   && source_node.op_type != IS_CV) {
3628
    /* Both LHS and RHS expressions may modify the same data structure,
3629
     * and the modification during RHS evaluation may dangle the pointer
3630
     * to the result of the LHS evaluation.
3631
     * Use MAKE_REF instruction to replace direct pointer with REFERENCE.
3632
     * See: Bug #71539
3633
     */
3634
940
    zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL);
3635
940
  }
3636
3637
10.6k
  opline = zend_delayed_compile_end(offset);
3638
3639
10.6k
  if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
3640
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3641
5
  }
3642
3643
10.6k
  flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0;
3644
3645
10.6k
  if (opline && opline->opcode == ZEND_FETCH_OBJ_W) {
3646
1.11k
    opline->opcode = ZEND_ASSIGN_OBJ_REF;
3647
1.11k
    opline->extended_value &= ~ZEND_FETCH_REF;
3648
1.11k
    opline->extended_value |= flags;
3649
1.11k
    if (result) {
3650
109
      *result = target_node;
3651
1.00k
    } else {
3652
1.00k
      SET_UNUSED(opline->result);
3653
1.00k
    }
3654
1.11k
    zend_emit_op_data(&source_node);
3655
9.54k
  } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) {
3656
557
    opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF;
3657
557
    opline->extended_value &= ~ZEND_FETCH_REF;
3658
557
    opline->extended_value |= flags;
3659
557
    if (result) {
3660
81
      *result = target_node;
3661
476
    } else {
3662
476
      SET_UNUSED(opline->result);
3663
476
    }
3664
557
    zend_emit_op_data(&source_node);
3665
8.98k
  } else {
3666
8.98k
    opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
3667
8.98k
    opline->extended_value = flags;
3668
8.98k
  }
3669
3670
10.6k
  if (result && (type == BP_VAR_R || type == BP_VAR_IS)) {
3671
    /* Deref. */
3672
3.44k
    znode tmp_result = *result;
3673
3.44k
    zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &tmp_result, NULL);
3674
3.44k
  }
3675
10.6k
}
3676
/* }}} */
3677
3678
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3679
2.68k
{
3680
2.68k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
3681
2.68k
    zend_ast_create_znode(value_node));
3682
2.68k
  zend_compile_stmt(assign_ast);
3683
2.68k
}
3684
/* }}} */
3685
3686
static void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
3687
98.7k
{
3688
98.7k
  zend_ast *var_ast = ast->child[0];
3689
98.7k
  zend_ast *expr_ast = ast->child[1];
3690
98.7k
  uint32_t opcode = ast->attr;
3691
3692
98.7k
  znode var_node, expr_node;
3693
98.7k
  zend_op *opline;
3694
98.7k
  uint32_t offset, cache_slot;
3695
3696
98.7k
  zend_ensure_writable_variable(var_ast);
3697
3698
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3699
98.7k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3700
98.7k
  switch (kind) {
3701
94.3k
    case ZEND_AST_VAR:
3702
94.3k
      offset = zend_delayed_compile_begin();
3703
94.3k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, false);
3704
94.3k
      zend_compile_expr(&expr_node, expr_ast);
3705
94.3k
      zend_delayed_compile_end(offset);
3706
94.3k
      opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node);
3707
94.3k
      opline->extended_value = opcode;
3708
94.3k
      return;
3709
439
    case ZEND_AST_STATIC_PROP:
3710
439
      offset = zend_delayed_compile_begin();
3711
439
      zend_delayed_compile_var(result, var_ast, BP_VAR_RW, false);
3712
439
      zend_compile_expr(&expr_node, expr_ast);
3713
3714
439
      opline = zend_delayed_compile_end(offset);
3715
439
      cache_slot = opline->extended_value;
3716
439
      opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP;
3717
439
      opline->extended_value = opcode;
3718
439
      opline->result_type = IS_TMP_VAR;
3719
439
      result->op_type = IS_TMP_VAR;
3720
3721
439
      opline = zend_emit_op_data(&expr_node);
3722
439
      opline->extended_value = cache_slot;
3723
439
      return;
3724
2.76k
    case ZEND_AST_DIM:
3725
2.76k
      offset = zend_delayed_compile_begin();
3726
2.76k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_RW, /* by_ref */ false);
3727
2.76k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3728
3729
2.76k
      opline = zend_delayed_compile_end(offset);
3730
2.76k
      opline->opcode = ZEND_ASSIGN_DIM_OP;
3731
2.76k
      opline->extended_value = opcode;
3732
2.76k
      opline->result_type = IS_TMP_VAR;
3733
2.76k
      result->op_type = IS_TMP_VAR;
3734
3735
2.76k
      zend_emit_op_data(&expr_node);
3736
2.76k
      return;
3737
1.18k
    case ZEND_AST_PROP:
3738
1.18k
    case ZEND_AST_NULLSAFE_PROP:
3739
1.18k
      offset = zend_delayed_compile_begin();
3740
1.18k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
3741
1.18k
      zend_compile_expr(&expr_node, expr_ast);
3742
3743
1.18k
      opline = zend_delayed_compile_end(offset);
3744
1.18k
      cache_slot = opline->extended_value;
3745
1.18k
      opline->opcode = ZEND_ASSIGN_OBJ_OP;
3746
1.18k
      opline->extended_value = opcode;
3747
1.18k
      opline->result_type = IS_TMP_VAR;
3748
1.18k
      result->op_type = IS_TMP_VAR;
3749
3750
1.18k
      opline = zend_emit_op_data(&expr_node);
3751
1.18k
      opline->extended_value = cache_slot;
3752
1.18k
      return;
3753
98.7k
    EMPTY_SWITCH_DEFAULT_CASE()
3754
98.7k
  }
3755
98.7k
}
3756
/* }}} */
3757
3758
4.80k
static uint32_t zend_get_arg_num(const zend_function *fn, const zend_string *arg_name) {
3759
  // TODO: Caching?
3760
16.9k
  for (uint32_t i = 0; i < fn->common.num_args; i++) {
3761
14.2k
    zend_arg_info *arg_info = &fn->op_array.arg_info[i];
3762
14.2k
    if (zend_string_equals(arg_info->name, arg_name)) {
3763
2.10k
      return i + 1;
3764
2.10k
    }
3765
14.2k
  }
3766
3767
  /* Either an invalid argument name, or collected into a variadic argument. */
3768
2.69k
  return (uint32_t) -1;
3769
4.80k
}
3770
3771
static uint32_t zend_compile_args(
3772
    zend_ast *ast, const zend_function *fbc, bool *may_have_extra_named_args) /* {{{ */
3773
2.64M
{
3774
2.64M
  const zend_ast_list *args = zend_ast_get_list(ast);
3775
2.64M
  uint32_t i;
3776
2.64M
  bool uses_arg_unpack = false;
3777
2.64M
  uint32_t arg_count = 0; /* number of arguments not including unpacks */
3778
3779
  /* Whether named arguments are used syntactically, to enforce language level limitations.
3780
   * May not actually use named argument passing. */
3781
2.64M
  bool uses_named_args = false;
3782
  /* Whether there may be any undef arguments due to the use of named arguments. */
3783
2.64M
  bool may_have_undef = false;
3784
  /* Whether there may be any extra named arguments collected into a variadic. */
3785
2.64M
  *may_have_extra_named_args = false;
3786
3787
6.16M
  for (i = 0; i < args->children; ++i) {
3788
3.52M
    zend_ast *arg = args->child[i];
3789
3.52M
    zend_string *arg_name = NULL;
3790
3.52M
    uint32_t arg_num = i + 1;
3791
3792
3.52M
    znode arg_node;
3793
3.52M
    zend_op *opline;
3794
3.52M
    uint8_t opcode;
3795
3796
3.52M
    if (arg->kind == ZEND_AST_UNPACK) {
3797
1.68k
      if (uses_named_args) {
3798
5
        zend_error_noreturn(E_COMPILE_ERROR,
3799
5
          "Cannot use argument unpacking after named arguments");
3800
5
      }
3801
3802
      /* Unpack may contain named arguments. */
3803
1.67k
      may_have_undef = true;
3804
1.67k
      if (!fbc || (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3805
1.31k
        *may_have_extra_named_args = true;
3806
1.31k
      }
3807
3808
1.67k
      uses_arg_unpack = true;
3809
1.67k
      fbc = NULL;
3810
3811
1.67k
      zend_compile_expr(&arg_node, arg->child[0]);
3812
1.67k
      opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
3813
1.67k
      opline->op2.num = arg_count;
3814
1.67k
      opline->result.var = EX_NUM_TO_VAR(arg_count - 1);
3815
3816
1.67k
      continue;
3817
1.68k
    }
3818
3819
3.52M
    if (arg->kind == ZEND_AST_NAMED_ARG) {
3820
20.4k
      uses_named_args = true;
3821
20.4k
      arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0]));
3822
20.4k
      arg = arg->child[1];
3823
3824
20.4k
      if (fbc && !uses_arg_unpack) {
3825
4.80k
        arg_num = zend_get_arg_num(fbc, arg_name);
3826
4.80k
        if (arg_num == arg_count + 1 && !may_have_undef) {
3827
          /* Using named arguments, but passing in order. */
3828
516
          arg_name = NULL;
3829
516
          arg_count++;
3830
4.28k
        } else {
3831
          // TODO: We could track which arguments were passed, even if out of order.
3832
4.28k
          may_have_undef = true;
3833
4.28k
          if (arg_num == (uint32_t) -1 && (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3834
303
            *may_have_extra_named_args = true;
3835
303
          }
3836
4.28k
        }
3837
15.6k
      } else {
3838
15.6k
        arg_num = (uint32_t) -1;
3839
15.6k
        may_have_undef = true;
3840
15.6k
        *may_have_extra_named_args = true;
3841
15.6k
      }
3842
3.50M
    } else {
3843
3.50M
      if (uses_arg_unpack) {
3844
10
        zend_error_noreturn(E_COMPILE_ERROR,
3845
10
          "Cannot use positional argument after argument unpacking");
3846
10
      }
3847
3848
3.50M
      if (uses_named_args) {
3849
47
        zend_error_noreturn(E_COMPILE_ERROR,
3850
47
          "Cannot use positional argument after named argument");
3851
47
      }
3852
3853
3.50M
      arg_count++;
3854
3.50M
    }
3855
3856
    /* Treat passing of $GLOBALS the same as passing a call.
3857
     * This will error at runtime if the argument is by-ref. */
3858
3.52M
    if (zend_is_call(arg) || is_globals_fetch(arg)) {
3859
134k
      uint32_t type = is_globals_fetch(arg) || (fbc && !ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num))
3860
134k
        ? BP_VAR_R : BP_VAR_FUNC_ARG;
3861
134k
      zend_compile_var(&arg_node, arg, type, /* by_ref */ false);
3862
134k
      if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
3863
        /* Function call was converted into builtin instruction */
3864
46.9k
        if (!fbc || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3865
7.09k
          opcode = ZEND_SEND_VAL_EX;
3866
39.8k
        } else {
3867
39.8k
          opcode = ZEND_SEND_VAL;
3868
39.8k
        }
3869
87.6k
      } else {
3870
87.6k
        if (fbc && arg_num != (uint32_t) -1) {
3871
477
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3872
391
            opcode = ZEND_SEND_VAR_NO_REF;
3873
391
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3874
            /* For IS_VAR operands, SEND_VAL will pass through the operand without
3875
             * dereferencing, so it will use a by-ref pass if the call returned by-ref
3876
             * and a by-value pass if it returned by-value. */
3877
86
            opcode = ZEND_SEND_VAL;
3878
86
          } else {
3879
0
            opcode = ZEND_SEND_VAR;
3880
0
          }
3881
87.1k
        } else {
3882
87.1k
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3883
87.1k
        }
3884
87.6k
      }
3885
3.38M
    } else if (zend_is_passable_by_ref(arg) && !zend_ast_is_short_circuited(arg)) {
3886
102k
      if (fbc && arg_num != (uint32_t) -1) {
3887
74.5k
        if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3888
2.64k
          zend_compile_var(&arg_node, arg, BP_VAR_W, true);
3889
2.64k
          opcode = ZEND_SEND_REF;
3890
71.9k
        } else {
3891
71.9k
          zend_compile_var(&arg_node, arg, BP_VAR_R, false);
3892
71.9k
          opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR;
3893
71.9k
        }
3894
74.5k
      } else {
3895
27.8k
        do {
3896
27.8k
          if (arg->kind == ZEND_AST_VAR) {
3897
15.5k
            CG(zend_lineno) = zend_ast_get_lineno(ast);
3898
15.5k
            if (is_this_fetch(arg)) {
3899
671
              zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL);
3900
671
              opcode = ZEND_SEND_VAR_EX;
3901
671
              CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3902
671
              break;
3903
14.8k
            } else if (zend_try_compile_cv(&arg_node, arg, BP_VAR_R) == SUCCESS) {
3904
14.6k
              opcode = ZEND_SEND_VAR_EX;
3905
14.6k
              break;
3906
14.6k
            }
3907
15.5k
          }
3908
12.4k
          opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL);
3909
12.4k
          if (arg_name) {
3910
2.00k
            opline->op2_type = IS_CONST;
3911
2.00k
            zend_string_addref(arg_name);
3912
2.00k
            opline->op2.constant = zend_add_literal_string(&arg_name);
3913
2.00k
            opline->result.num = zend_alloc_cache_slots(2);
3914
10.4k
          } else {
3915
10.4k
            opline->op2.num = arg_num;
3916
10.4k
          }
3917
12.4k
          zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, true);
3918
12.4k
          opcode = ZEND_SEND_FUNC_ARG;
3919
12.4k
        } while (0);
3920
27.8k
      }
3921
3.28M
    } else {
3922
3.28M
      zend_compile_expr(&arg_node, arg);
3923
3.28M
      if (arg_node.op_type == IS_VAR) {
3924
        /* pass ++$a or something similar */
3925
0
        if (fbc && arg_num != (uint32_t) -1) {
3926
0
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3927
0
            opcode = ZEND_SEND_VAR_NO_REF;
3928
0
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3929
0
            opcode = ZEND_SEND_VAL;
3930
0
          } else {
3931
0
            opcode = ZEND_SEND_VAR;
3932
0
          }
3933
0
        } else {
3934
0
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3935
0
        }
3936
3.28M
      } else if (arg_node.op_type == IS_CV) {
3937
0
        if (fbc && arg_num != (uint32_t) -1) {
3938
0
          if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3939
0
            opcode = ZEND_SEND_REF;
3940
0
          } else {
3941
0
            opcode = ZEND_SEND_VAR;
3942
0
          }
3943
0
        } else {
3944
0
          opcode = ZEND_SEND_VAR_EX;
3945
0
        }
3946
3.28M
      } else {
3947
        /* Delay "Only variables can be passed by reference" error to execution */
3948
3.28M
        if (fbc && arg_num != (uint32_t) -1 && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3949
119k
          opcode = ZEND_SEND_VAL;
3950
3.16M
        } else {
3951
3.16M
          opcode = ZEND_SEND_VAL_EX;
3952
3.16M
        }
3953
3.28M
      }
3954
3.28M
    }
3955
3956
3.52M
    opline = zend_emit_op(NULL, opcode, &arg_node, NULL);
3957
3.52M
    if (arg_name) {
3958
19.8k
      opline->op2_type = IS_CONST;
3959
19.8k
      zend_string_addref(arg_name);
3960
19.8k
      opline->op2.constant = zend_add_literal_string(&arg_name);
3961
19.8k
      opline->result.num = zend_alloc_cache_slots(2);
3962
3.50M
    } else {
3963
3.50M
      opline->op2.opline_num = arg_num;
3964
3.50M
      opline->result.var = EX_NUM_TO_VAR(arg_num - 1);
3965
3.50M
    }
3966
3.52M
  }
3967
3968
2.64M
  if (may_have_undef) {
3969
16.8k
    zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
3970
16.8k
  }
3971
3972
2.64M
  return arg_count;
3973
2.64M
}
3974
/* }}} */
3975
3976
ZEND_API uint8_t zend_get_call_op(const zend_op *init_op, const zend_function *fbc, bool result_used) /* {{{ */
3977
2.74M
{
3978
2.74M
  uint32_t no_discard = result_used ? 0 : ZEND_ACC_NODISCARD;
3979
3980
2.74M
  if (fbc && init_op->opcode != ZEND_NEW) {
3981
242k
    ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE));
3982
242k
    if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) {
3983
198k
      if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) {
3984
22.8k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3985
22.8k
          return ZEND_DO_ICALL;
3986
22.8k
        } else {
3987
61
          return ZEND_DO_FCALL_BY_NAME;
3988
61
        }
3989
22.8k
      }
3990
198k
    } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){
3991
44.4k
      if (zend_execute_ex == execute_ex) {
3992
20.7k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3993
20.6k
          return ZEND_DO_UCALL;
3994
20.6k
        } else {
3995
182
          return ZEND_DO_FCALL_BY_NAME;
3996
182
        }
3997
20.7k
      }
3998
44.4k
    }
3999
2.50M
  } else if (zend_execute_ex == execute_ex &&
4000
2.43M
             !zend_execute_internal &&
4001
2.39M
             (init_op->opcode == ZEND_INIT_FCALL_BY_NAME ||
4002
2.35M
              init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) {
4003
2.22M
    return ZEND_DO_FCALL_BY_NAME;
4004
2.22M
  }
4005
481k
  return ZEND_DO_FCALL;
4006
2.74M
}
4007
/* }}} */
4008
4009
static bool zend_compile_call_common(znode *result, zend_ast *args_ast, const zend_function *fbc, uint32_t lineno, uint32_t type) /* {{{ */
4010
2.65M
{
4011
2.65M
  zend_op *opline;
4012
2.65M
  uint32_t opnum_init = get_next_op_number() - 1;
4013
4014
2.65M
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
4015
7.84k
    opline = &CG(active_op_array)->opcodes[opnum_init];
4016
7.84k
    opline->extended_value = 0;
4017
    /* opcode array may be reallocated, so don't access opcode field after zend_emit_op_tmp(). */
4018
7.84k
    uint8_t opcode = opline->opcode;
4019
4020
7.84k
    if (opcode == ZEND_NEW) {
4021
15
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
4022
15
    }
4023
4024
7.82k
    zend_ast_list *args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args);
4025
7.82k
    if (args->children != 1 || args->child[0]->attr != ZEND_PLACEHOLDER_VARIADIC) {
4026
23
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot create a Closure for call expression with more than one argument, or non-variadic placeholders");
4027
23
    }
4028
4029
7.80k
    if (opcode == ZEND_INIT_FCALL) {
4030
490
      opline->op1.num = zend_vm_calc_used_stack(0, fbc);
4031
490
    }
4032
4033
7.80k
    zend_op *callable_convert_op = zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT, NULL, NULL);
4034
7.80k
    if (opcode == ZEND_INIT_FCALL
4035
7.31k
     || opcode == ZEND_INIT_FCALL_BY_NAME
4036
7.20k
     || opcode == ZEND_INIT_NS_FCALL_BY_NAME) {
4037
7.20k
      callable_convert_op->extended_value = zend_alloc_cache_slot();
4038
7.20k
    } else {
4039
603
      callable_convert_op->extended_value = (uint32_t)-1;
4040
603
    }
4041
7.80k
    return true;
4042
7.82k
  }
4043
4044
2.65M
  bool may_have_extra_named_args;
4045
2.64M
  uint32_t arg_count = zend_compile_args(args_ast, fbc, &may_have_extra_named_args);
4046
4047
2.64M
  zend_do_extended_fcall_begin();
4048
4049
2.64M
  opline = &CG(active_op_array)->opcodes[opnum_init];
4050
2.64M
  opline->extended_value = arg_count;
4051
2.64M
  uint8_t init_opcode = opline->opcode;
4052
4053
2.64M
  if (init_opcode == ZEND_INIT_FCALL) {
4054
133k
    opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
4055
133k
  }
4056
4057
2.64M
  uint8_t call_op = zend_get_call_op(
4058
2.64M
    opline,
4059
2.64M
    fbc,
4060
    /* result_used: At this point we do not yet reliably
4061
     * know if the result is used. Deoptimize #[\NoDiscard]
4062
     * calls to be sure. The optimizer will fix this up.
4063
     */
4064
2.64M
    false
4065
2.64M
  );
4066
2.64M
  opline = zend_emit_op(result, call_op, NULL, NULL);
4067
2.64M
  if (type == BP_VAR_R || type == BP_VAR_IS) {
4068
2.51M
    if (init_opcode != ZEND_NEW && opline->result_type == IS_VAR) {
4069
2.43M
      opline->result_type = IS_TMP_VAR;
4070
2.43M
      result->op_type = IS_TMP_VAR;
4071
2.43M
    }
4072
2.51M
  }
4073
2.64M
  if (may_have_extra_named_args) {
4074
15.0k
    opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4075
15.0k
  }
4076
2.64M
  opline->lineno = lineno;
4077
2.64M
  zend_do_extended_fcall_end();
4078
2.64M
  return false;
4079
2.65M
}
4080
/* }}} */
4081
4082
static bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
4083
2.41M
{
4084
2.41M
  zend_string *orig_name = zend_ast_get_str(name_ast);
4085
2.41M
  bool is_fully_qualified;
4086
4087
2.41M
  name_node->op_type = IS_CONST;
4088
2.41M
  ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
4089
2.41M
    orig_name, name_ast->attr, &is_fully_qualified));
4090
4091
2.41M
  return !is_fully_qualified && FC(current_namespace);
4092
2.41M
}
4093
/* }}} */
4094
4095
static void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */
4096
126k
{
4097
126k
  if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
4098
49.8k
    const char *colon;
4099
49.8k
    zend_string *str = Z_STR(name_node->u.constant);
4100
49.8k
    if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
4101
615
      zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
4102
615
      zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
4103
615
      zend_op *opline = get_next_op();
4104
4105
615
      opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
4106
615
      opline->op1_type = IS_CONST;
4107
615
      opline->op1.constant = zend_add_class_name_literal(class);
4108
615
      opline->op2_type = IS_CONST;
4109
615
      opline->op2.constant = zend_add_func_name_literal(method);
4110
      /* 2 slots, for class and method */
4111
615
      opline->result.num = zend_alloc_cache_slots(2);
4112
615
      zval_ptr_dtor(&name_node->u.constant);
4113
49.2k
    } else {
4114
49.2k
      zend_op *opline = get_next_op();
4115
4116
49.2k
      opline->opcode = ZEND_INIT_FCALL_BY_NAME;
4117
49.2k
      opline->op2_type = IS_CONST;
4118
49.2k
      opline->op2.constant = zend_add_func_name_literal(str);
4119
49.2k
      opline->result.num = zend_alloc_cache_slot();
4120
49.2k
    }
4121
77.0k
  } else {
4122
77.0k
    zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node);
4123
77.0k
  }
4124
4125
126k
  zend_compile_call_common(result, args_ast, NULL, lineno, type);
4126
126k
}
4127
/* }}} */
4128
4129
static inline bool zend_args_contain_unpack_or_named(const zend_ast_list *args) /* {{{ */
4130
2.31M
{
4131
2.31M
  uint32_t i;
4132
5.60M
  for (i = 0; i < args->children; ++i) {
4133
3.30M
    const zend_ast *arg = args->child[i];
4134
3.30M
    if (arg->kind == ZEND_AST_UNPACK || arg->kind == ZEND_AST_NAMED_ARG) {
4135
12.8k
      return 1;
4136
12.8k
    }
4137
3.30M
  }
4138
2.30M
  return 0;
4139
2.31M
}
4140
/* }}} */
4141
4142
static zend_result zend_compile_func_strlen(znode *result, const zend_ast_list *args) /* {{{ */
4143
845
{
4144
845
  znode arg_node;
4145
4146
845
  if (args->children != 1) {
4147
53
    return FAILURE;
4148
53
  }
4149
4150
792
  zend_compile_expr(&arg_node, args->child[0]);
4151
792
  if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
4152
237
    result->op_type = IS_CONST;
4153
237
    ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
4154
237
    zval_ptr_dtor_str(&arg_node.u.constant);
4155
555
  } else {
4156
555
    zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
4157
555
  }
4158
792
  return SUCCESS;
4159
845
}
4160
/* }}} */
4161
4162
static zend_result zend_compile_func_typecheck(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4163
1.58k
{
4164
1.58k
  znode arg_node;
4165
1.58k
  zend_op *opline;
4166
4167
1.58k
  if (args->children != 1) {
4168
299
    return FAILURE;
4169
299
  }
4170
4171
1.28k
  zend_compile_expr(&arg_node, args->child[0]);
4172
1.28k
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4173
1.28k
  if (type != _IS_BOOL) {
4174
1.19k
    opline->extended_value = (1 << type);
4175
1.19k
  } else {
4176
92
    opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE);
4177
92
  }
4178
1.28k
  return SUCCESS;
4179
1.58k
}
4180
/* }}} */
4181
4182
static zend_result zend_compile_func_is_scalar(znode *result, const zend_ast_list *args) /* {{{ */
4183
303
{
4184
303
  znode arg_node;
4185
303
  zend_op *opline;
4186
4187
303
  if (args->children != 1) {
4188
15
    return FAILURE;
4189
15
  }
4190
4191
288
  zend_compile_expr(&arg_node, args->child[0]);
4192
288
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4193
288
  opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING);
4194
288
  return SUCCESS;
4195
303
}
4196
4197
static zend_result zend_compile_func_cast(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4198
1.16k
{
4199
1.16k
  znode arg_node;
4200
1.16k
  zend_op *opline;
4201
4202
1.16k
  if (args->children != 1) {
4203
285
    return FAILURE;
4204
285
  }
4205
4206
882
  zend_compile_expr(&arg_node, args->child[0]);
4207
882
  if (type == _IS_BOOL) {
4208
303
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL);
4209
579
  } else {
4210
579
    opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
4211
579
    opline->extended_value = type;
4212
579
  }
4213
882
  return SUCCESS;
4214
1.16k
}
4215
/* }}} */
4216
4217
static zend_result zend_compile_func_defined(znode *result, const zend_ast_list *args) /* {{{ */
4218
1.68k
{
4219
1.68k
  zend_string *name;
4220
1.68k
  zend_op *opline;
4221
4222
1.68k
  if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
4223
247
    return FAILURE;
4224
247
  }
4225
4226
1.43k
  name = zval_get_string(zend_ast_get_zval(args->child[0]));
4227
1.43k
  if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
4228
339
    zend_string_release_ex(name, 0);
4229
339
    return FAILURE;
4230
339
  }
4231
4232
1.10k
  if (zend_try_ct_eval_const(&result->u.constant, name, false)) {
4233
110
    zend_string_release_ex(name, 0);
4234
110
    zval_ptr_dtor(&result->u.constant);
4235
110
    ZVAL_TRUE(&result->u.constant);
4236
110
    result->op_type = IS_CONST;
4237
110
    return SUCCESS;
4238
110
  }
4239
4240
990
  opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
4241
990
  opline->op1_type = IS_CONST;
4242
990
  LITERAL_STR(opline->op1, name);
4243
990
  opline->extended_value = zend_alloc_cache_slot();
4244
4245
990
  return SUCCESS;
4246
1.10k
}
4247
/* }}} */
4248
4249
static zend_result zend_compile_func_chr(znode *result, const zend_ast_list *args) /* {{{ */
4250
1.14k
{
4251
1.14k
  zval *zint;
4252
1.14k
  if (
4253
1.14k
    args->children == 1
4254
974
    && args->child[0]->kind == ZEND_AST_ZVAL
4255
766
    && (zint = zend_ast_get_zval(args->child[0]))
4256
766
    && Z_TYPE_P(zint) == IS_LONG
4257
662
    && Z_LVAL_P(zint) >= 0
4258
662
    && Z_LVAL_P(zint) <= 255
4259
1.14k
  ) {
4260
432
    result->op_type = IS_CONST;
4261
432
    ZVAL_CHAR(&result->u.constant, Z_LVAL_P(zint));
4262
432
    return SUCCESS;
4263
711
  } else {
4264
711
    return FAILURE;
4265
711
  }
4266
1.14k
}
4267
/* }}} */
4268
4269
static zend_result zend_compile_func_ord(znode *result, const zend_ast_list *args) /* {{{ */
4270
533
{
4271
533
  zval *str;
4272
533
  if (
4273
533
    args->children == 1
4274
454
    && args->child[0]->kind == ZEND_AST_ZVAL
4275
309
    && (str = zend_ast_get_zval(args->child[0]))
4276
309
    && Z_TYPE_P(str) == IS_STRING
4277
231
    && Z_STRLEN_P(str) == 1
4278
533
  ) {
4279
28
    result->op_type = IS_CONST;
4280
28
    ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(str)[0]);
4281
28
    return SUCCESS;
4282
505
  } else {
4283
505
    return FAILURE;
4284
505
  }
4285
533
}
4286
/* }}} */
4287
4288
/* We can only calculate the stack size for functions that have been fully compiled, otherwise
4289
 * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for
4290
 * directly or indirectly recursive function calls. */
4291
176k
static bool fbc_is_finalized(const zend_function *fbc) {
4292
176k
  return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO);
4293
176k
}
4294
4295
static bool zend_compile_ignore_class(const zend_class_entry *ce, const zend_string *filename)
4296
60.1k
{
4297
60.1k
  if (ce->type == ZEND_INTERNAL_CLASS) {
4298
39.7k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES;
4299
39.7k
  } else {
4300
20.3k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4301
15.1k
      && ce->info.user.filename != filename;
4302
20.3k
  }
4303
60.1k
}
4304
4305
static bool zend_compile_ignore_function(const zend_function *fbc, const zend_string *filename)
4306
159k
{
4307
159k
  if (fbc->type == ZEND_INTERNAL_FUNCTION) {
4308
138k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS;
4309
138k
  } else {
4310
21.2k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)
4311
21.2k
      || ((CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4312
18.6k
        && fbc->op_array.filename != filename);
4313
21.2k
  }
4314
159k
}
4315
4316
static zend_result zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
4317
9.62k
{
4318
9.62k
  zend_string *name, *lcname;
4319
9.62k
  zend_function *fbc;
4320
9.62k
  zend_op *opline;
4321
4322
9.62k
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
4323
2.22k
    return FAILURE;
4324
2.22k
  }
4325
4326
7.40k
  name = zend_ast_get_str(name_ast);
4327
7.40k
  lcname = zend_string_tolower(name);
4328
4329
7.40k
  fbc = zend_hash_find_ptr(CG(function_table), lcname);
4330
7.40k
  if (!fbc
4331
894
   || !fbc_is_finalized(fbc)
4332
6.51k
   || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
4333
6.51k
    zend_string_release_ex(lcname, 0);
4334
6.51k
    return FAILURE;
4335
6.51k
  }
4336
4337
894
  opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
4338
894
  opline->extended_value = num_args;
4339
894
  opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
4340
894
  opline->op2_type = IS_CONST;
4341
894
  LITERAL_STR(opline->op2, lcname);
4342
894
  opline->result.num = zend_alloc_cache_slot();
4343
4344
894
  return SUCCESS;
4345
7.40k
}
4346
/* }}} */
4347
4348
static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
4349
9.62k
{
4350
9.62k
  zend_op *opline;
4351
9.62k
  znode name_node;
4352
4353
9.62k
  if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
4354
894
    return;
4355
894
  }
4356
4357
8.73k
  zend_compile_expr(&name_node, name_ast);
4358
4359
8.73k
  opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
4360
8.73k
  opline->op1_type = IS_CONST;
4361
8.73k
  LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
4362
8.73k
  opline->extended_value = num_args;
4363
8.73k
}
4364
/* }}} */
4365
4366
/* cufa = call_user_func_array */
4367
static zend_result zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t type) /* {{{ */
4368
2.20k
{
4369
2.20k
  znode arg_node;
4370
2.20k
  zend_op *opline;
4371
4372
2.20k
  if (args->children != 2) {
4373
31
    return FAILURE;
4374
31
  }
4375
4376
2.16k
  zend_compile_init_user_func(args->child[0], 0, lcname);
4377
2.16k
  if (args->child[1]->kind == ZEND_AST_CALL
4378
1.53k
   && args->child[1]->child[0]->kind == ZEND_AST_ZVAL
4379
1.45k
   && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING
4380
1.41k
   && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) {
4381
1.40k
    zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]);
4382
1.40k
    zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]);
4383
1.40k
    bool is_fully_qualified;
4384
1.40k
    zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified);
4385
4386
1.40k
    if (zend_string_equals_literal_ci(name, "array_slice")
4387
708
       && !zend_args_contain_unpack_or_named(list)
4388
603
     && list->children == 3
4389
454
     && list->child[1]->kind == ZEND_AST_ZVAL) {
4390
382
      zval *zv = zend_ast_get_zval(list->child[1]);
4391
4392
382
      if (Z_TYPE_P(zv) == IS_LONG
4393
359
       && Z_LVAL_P(zv) >= 0
4394
359
       && Z_LVAL_P(zv) <= 0x7fffffff) {
4395
124
        zend_op *opline;
4396
124
        znode len_node;
4397
4398
124
        zend_compile_expr(&arg_node, list->child[0]);
4399
124
        zend_compile_expr(&len_node, list->child[2]);
4400
124
        opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node);
4401
124
        opline->extended_value = Z_LVAL_P(zv);
4402
124
        opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4403
124
        if (type == BP_VAR_R || type == BP_VAR_IS) {
4404
124
          opline->result_type = IS_TMP_VAR;
4405
124
          result->op_type = IS_TMP_VAR;
4406
124
        }
4407
124
        zend_string_release_ex(name, 0);
4408
124
        return SUCCESS;
4409
124
      }
4410
382
    }
4411
1.28k
    zend_string_release_ex(name, 0);
4412
1.28k
  }
4413
2.04k
  zend_compile_expr(&arg_node, args->child[1]);
4414
2.04k
  zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
4415
2.04k
  zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
4416
2.04k
  opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4417
2.04k
  if (type == BP_VAR_R || type == BP_VAR_IS) {
4418
1.83k
    opline->result_type = IS_TMP_VAR;
4419
1.83k
    result->op_type = IS_TMP_VAR;
4420
1.83k
  }
4421
2.04k
  opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4422
4423
2.04k
  return SUCCESS;
4424
2.16k
}
4425
/* }}} */
4426
4427
/* cuf = call_user_func */
4428
static zend_result zend_compile_func_cuf(znode *result, const zend_ast_list *args, zend_string *lcname, uint32_t type) /* {{{ */
4429
7.65k
{
4430
7.65k
  uint32_t i;
4431
4432
7.65k
  if (args->children < 1) {
4433
194
    return FAILURE;
4434
194
  }
4435
4436
7.45k
  zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
4437
10.4k
  for (i = 1; i < args->children; ++i) {
4438
3.00k
    zend_ast *arg_ast = args->child[i];
4439
3.00k
    znode arg_node;
4440
3.00k
    zend_op *opline;
4441
4442
3.00k
    zend_compile_expr(&arg_node, arg_ast);
4443
4444
3.00k
    opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
4445
3.00k
    opline->op2.num = i;
4446
3.00k
    opline->result.var = EX_NUM_TO_VAR(i - 1);
4447
3.00k
  }
4448
7.45k
  zend_op *opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4449
7.45k
  if (type == BP_VAR_R || type == BP_VAR_IS) {
4450
895
    opline->result_type = IS_TMP_VAR;
4451
895
    result->op_type = IS_TMP_VAR;
4452
895
  }
4453
4454
7.45k
  return SUCCESS;
4455
7.65k
}
4456
/* }}} */
4457
4458
static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, const zend_function *fbc, uint32_t lineno, uint32_t type) /* {{{ */
4459
25.4k
{
4460
25.4k
  if (EG(assertions) >= 0) {
4461
25.4k
    znode name_node;
4462
25.4k
    zend_op *opline;
4463
25.4k
    uint32_t check_op_number = get_next_op_number();
4464
4465
25.4k
    zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
4466
4467
25.4k
    if (fbc && fbc_is_finalized(fbc)) {
4468
17.4k
      name_node.op_type = IS_CONST;
4469
17.4k
      ZVAL_STR_COPY(&name_node.u.constant, name);
4470
4471
17.4k
      opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
4472
17.4k
    } else {
4473
7.93k
      opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
4474
7.93k
      opline->op2_type = IS_CONST;
4475
7.93k
      opline->op2.constant = zend_add_ns_func_name_literal(name);
4476
7.93k
    }
4477
25.4k
    opline->result.num = zend_alloc_cache_slot();
4478
4479
25.4k
    if (args->children == 1) {
4480
      /* add "assert(condition) as assertion message */
4481
17.6k
      zend_ast *arg = zend_ast_create_zval_from_str(
4482
17.6k
        zend_ast_export("assert(", args->child[0], ")"));
4483
17.6k
      if (args->child[0]->kind == ZEND_AST_NAMED_ARG) {
4484
        /* If the original argument was named, add the new argument as named as well,
4485
         * as mixing named and positional is not allowed. */
4486
789
        zend_ast *name = zend_ast_create_zval_from_str(
4487
789
          ZSTR_INIT_LITERAL("description", 0));
4488
789
        arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg);
4489
789
      }
4490
17.6k
      args = (zend_ast_list *)zend_ast_list_add((zend_ast *) args, arg);
4491
17.6k
    }
4492
4493
25.4k
    zend_compile_call_common(result, (zend_ast*)args, fbc, lineno, type);
4494
4495
25.4k
    opline = &CG(active_op_array)->opcodes[check_op_number];
4496
25.4k
    opline->op2.opline_num = get_next_op_number();
4497
25.4k
    SET_NODE(opline->result, result);
4498
25.4k
  } else {
4499
0
    if (!fbc) {
4500
0
      zend_string_release_ex(name, 0);
4501
0
    }
4502
0
    result->op_type = IS_CONST;
4503
0
    ZVAL_TRUE(&result->u.constant);
4504
0
  }
4505
25.4k
}
4506
/* }}} */
4507
4508
static zend_result zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */
4509
10.8k
{
4510
10.8k
  bool strict = false;
4511
10.8k
  znode array, needly;
4512
10.8k
  zend_op *opline;
4513
4514
10.8k
  if (args->children == 3) {
4515
9.31k
    if (args->child[2]->kind == ZEND_AST_ZVAL) {
4516
592
      strict = zend_is_true(zend_ast_get_zval(args->child[2]));
4517
8.72k
    } else if (args->child[2]->kind == ZEND_AST_CONST) {
4518
821
      zval value;
4519
821
      zend_ast *name_ast = args->child[2]->child[0];
4520
821
      bool is_fully_qualified;
4521
821
      zend_string *resolved_name = zend_resolve_const_name(
4522
821
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
4523
4524
821
      if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) {
4525
640
        zend_string_release_ex(resolved_name, 0);
4526
640
        return FAILURE;
4527
640
      }
4528
4529
181
      zend_string_release_ex(resolved_name, 0);
4530
181
      strict = zend_is_true(&value);
4531
181
      zval_ptr_dtor(&value);
4532
7.90k
    } else {
4533
7.90k
      return FAILURE;
4534
7.90k
    }
4535
9.31k
  } else if (args->children != 2) {
4536
214
    return FAILURE;
4537
214
  }
4538
4539
2.09k
  if (args->child[1]->kind != ZEND_AST_ARRAY
4540
1.52k
   || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) {
4541
628
    return FAILURE;
4542
628
  }
4543
4544
1.46k
  if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) {
4545
1.30k
    bool ok = true;
4546
1.30k
    zval *val, tmp;
4547
1.30k
    HashTable *src = Z_ARRVAL(array.u.constant);
4548
1.30k
    HashTable *dst = zend_new_array(zend_hash_num_elements(src));
4549
4550
1.30k
    ZVAL_TRUE(&tmp);
4551
4552
1.30k
    if (strict) {
4553
3.46k
      ZEND_HASH_FOREACH_VAL(src, val) {
4554
3.46k
        if (Z_TYPE_P(val) == IS_STRING) {
4555
75
          zend_hash_add(dst, Z_STR_P(val), &tmp);
4556
942
        } else if (Z_TYPE_P(val) == IS_LONG) {
4557
901
          zend_hash_index_add(dst, Z_LVAL_P(val), &tmp);
4558
901
        } else {
4559
41
          zend_array_destroy(dst);
4560
41
          ok = false;
4561
41
          break;
4562
41
        }
4563
3.46k
      } ZEND_HASH_FOREACH_END();
4564
747
    } else {
4565
3.72k
      ZEND_HASH_FOREACH_VAL(src, val) {
4566
3.72k
        if (Z_TYPE_P(val) != IS_STRING
4567
1.12k
         || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) {
4568
402
          zend_array_destroy(dst);
4569
402
          ok = false;
4570
402
          break;
4571
402
        }
4572
903
        zend_hash_add(dst, Z_STR_P(val), &tmp);
4573
903
      } ZEND_HASH_FOREACH_END();
4574
747
    }
4575
4576
1.30k
    zend_array_destroy(src);
4577
1.30k
    if (!ok) {
4578
443
      return FAILURE;
4579
443
    }
4580
865
    Z_ARRVAL(array.u.constant) = dst;
4581
865
  }
4582
1.02k
  array.op_type = IS_CONST;
4583
4584
1.02k
  zend_compile_expr(&needly, args->child[0]);
4585
4586
1.02k
  opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array);
4587
1.02k
  opline->extended_value = strict;
4588
4589
1.02k
  return SUCCESS;
4590
1.46k
}
4591
/* }}} */
4592
4593
static zend_result zend_compile_func_count(znode *result, const zend_ast_list *args, const zend_string *lcname) /* {{{ */
4594
1.06k
{
4595
1.06k
  znode arg_node;
4596
1.06k
  zend_op *opline;
4597
4598
1.06k
  if (args->children != 1) {
4599
68
    return FAILURE;
4600
68
  }
4601
4602
994
  zend_compile_expr(&arg_node, args->child[0]);
4603
994
  opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL);
4604
994
  opline->extended_value = zend_string_equals_literal(lcname, "sizeof");
4605
4606
994
  return SUCCESS;
4607
1.06k
}
4608
/* }}} */
4609
4610
static zend_result zend_compile_func_get_class(znode *result, const zend_ast_list *args) /* {{{ */
4611
1.26k
{
4612
1.26k
  if (args->children == 0) {
4613
89
    zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL);
4614
1.17k
  } else {
4615
1.17k
    znode arg_node;
4616
4617
1.17k
    if (args->children != 1) {
4618
194
      return FAILURE;
4619
194
    }
4620
4621
985
    zend_compile_expr(&arg_node, args->child[0]);
4622
985
    zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL);
4623
985
  }
4624
1.07k
  return SUCCESS;
4625
1.26k
}
4626
/* }}} */
4627
4628
static zend_result zend_compile_func_get_called_class(znode *result, const zend_ast_list *args) /* {{{ */
4629
208
{
4630
208
  if (args->children != 0) {
4631
43
    return FAILURE;
4632
43
  }
4633
4634
165
  zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL);
4635
165
  return SUCCESS;
4636
208
}
4637
/* }}} */
4638
4639
static zend_result zend_compile_func_gettype(znode *result, const zend_ast_list *args) /* {{{ */
4640
242
{
4641
242
  znode arg_node;
4642
4643
242
  if (args->children != 1) {
4644
45
    return FAILURE;
4645
45
  }
4646
4647
197
  zend_compile_expr(&arg_node, args->child[0]);
4648
197
  zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL);
4649
197
  return SUCCESS;
4650
242
}
4651
/* }}} */
4652
4653
static zend_result zend_compile_func_num_args(znode *result, const zend_ast_list *args) /* {{{ */
4654
210
{
4655
210
  if (CG(active_op_array)->function_name && args->children == 0) {
4656
61
    zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL);
4657
61
    return SUCCESS;
4658
149
  } else {
4659
149
    return FAILURE;
4660
149
  }
4661
210
}
4662
/* }}} */
4663
4664
static zend_result zend_compile_func_get_args(znode *result, const zend_ast_list *args) /* {{{ */
4665
675
{
4666
675
  if (CG(active_op_array)->function_name && args->children == 0) {
4667
454
    zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL);
4668
454
    return SUCCESS;
4669
454
  } else {
4670
221
    return FAILURE;
4671
221
  }
4672
675
}
4673
/* }}} */
4674
4675
static zend_result zend_compile_func_array_key_exists(znode *result, const zend_ast_list *args) /* {{{ */
4676
286
{
4677
286
  znode subject, needle;
4678
4679
286
  if (args->children != 2) {
4680
69
    return FAILURE;
4681
69
  }
4682
4683
217
  zend_compile_expr(&needle, args->child[0]);
4684
217
  zend_compile_expr(&subject, args->child[1]);
4685
4686
217
  zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject);
4687
217
  return SUCCESS;
4688
286
}
4689
/* }}} */
4690
4691
static zend_result zend_compile_func_array_slice(znode *result, const zend_ast_list *args) /* {{{ */
4692
2.15k
{
4693
2.15k
  if (CG(active_op_array)->function_name
4694
1.31k
   && args->children == 2
4695
698
   && args->child[0]->kind == ZEND_AST_CALL
4696
599
   && args->child[0]->child[0]->kind == ZEND_AST_ZVAL
4697
440
   && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING
4698
440
   && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST
4699
439
   && args->child[1]->kind == ZEND_AST_ZVAL) {
4700
4701
423
    zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]);
4702
423
    bool is_fully_qualified;
4703
423
    zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified);
4704
423
    const zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]);
4705
423
    const zval *zv = zend_ast_get_zval(args->child[1]);
4706
423
    znode first;
4707
4708
423
    if (zend_string_equals_literal_ci(name, "func_get_args")
4709
284
     && list->children == 0
4710
239
     && Z_TYPE_P(zv) == IS_LONG
4711
54
     && Z_LVAL_P(zv) >= 0) {
4712
54
      first.op_type = IS_CONST;
4713
54
      ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv));
4714
54
      zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL);
4715
54
      zend_string_release_ex(name, 0);
4716
54
      return SUCCESS;
4717
54
    }
4718
369
    zend_string_release_ex(name, 0);
4719
369
  }
4720
2.10k
  return FAILURE;
4721
2.15k
}
4722
/* }}} */
4723
4724
static uint32_t find_frameless_function_offset(uint32_t arity, const void *handler)
4725
879k
{
4726
879k
  void **handlers = zend_flf_handlers;
4727
879k
  void **current = handlers;
4728
8.11M
  while (current) {
4729
8.11M
    if (*current == handler) {
4730
879k
      return current - handlers;
4731
879k
    }
4732
7.23M
    current++;
4733
7.23M
  }
4734
4735
0
  return (uint32_t)-1;
4736
879k
}
4737
4738
static const zend_frameless_function_info *find_frameless_function_info(const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4739
572k
{
4740
572k
  if (zend_execute_internal) {
4741
91.1k
    return NULL;
4742
91.1k
  }
4743
4744
481k
  if (ZEND_USER_CODE(fbc->type)) {
4745
6
    return NULL;
4746
6
  }
4747
4748
481k
  const zend_frameless_function_info *frameless_function_info = fbc->internal_function.frameless_function_infos;
4749
481k
  if (!frameless_function_info) {
4750
14.0k
    return NULL;
4751
14.0k
  }
4752
4753
467k
  if (args->children > 3) {
4754
251
    return NULL;
4755
251
  }
4756
4757
631k
  while (frameless_function_info->handler) {
4758
603k
    if (frameless_function_info->num_args >= args->children
4759
494k
     && fbc->common.required_num_args <= args->children
4760
440k
     && (!(fbc->common.fn_flags & ZEND_ACC_VARIADIC)
4761
439k
      || frameless_function_info->num_args == args->children)) {
4762
439k
      uint32_t num_args = frameless_function_info->num_args;
4763
439k
      uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4764
439k
      if (offset == (uint32_t)-1) {
4765
0
        continue;
4766
0
      }
4767
439k
      return frameless_function_info;
4768
439k
    }
4769
163k
    frameless_function_info++;
4770
163k
  }
4771
4772
27.4k
  return NULL;
4773
467k
}
4774
4775
static uint32_t zend_compile_frameless_icall_ex(znode *result, const zend_ast_list *args, const zend_function *fbc, const zend_frameless_function_info *frameless_function_info, uint32_t type)
4776
439k
{
4777
439k
  uint32_t lineno = CG(zend_lineno);
4778
439k
  uint32_t num_args = frameless_function_info->num_args;
4779
439k
  uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4780
439k
  znode arg_zvs[3];
4781
1.42M
  for (uint32_t i = 0; i < num_args; i++) {
4782
987k
    if (i < args->children) {
4783
987k
      zend_compile_expr(&arg_zvs[i], args->child[i]);
4784
987k
    } else {
4785
0
      const zend_arg_info *arg_info = &fbc->common.arg_info[i];
4786
0
      arg_zvs[i].op_type = IS_CONST;
4787
0
      if (zend_get_default_from_internal_arg_info(&arg_zvs[i].u.constant, arg_info) == FAILURE) {
4788
0
        ZEND_UNREACHABLE();
4789
0
      }
4790
0
    }
4791
987k
  }
4792
439k
  uint8_t opcode = ZEND_FRAMELESS_ICALL_0 + num_args;
4793
439k
  uint32_t opnum = get_next_op_number();
4794
439k
  zend_op *opline = zend_emit_op_tmp(result, opcode, NULL, NULL);
4795
439k
  opline->extended_value = offset;
4796
439k
  opline->lineno = lineno;
4797
439k
  if (num_args >= 1) {
4798
439k
    SET_NODE(opline->op1, &arg_zvs[0]);
4799
439k
  }
4800
439k
  if (num_args >= 2) {
4801
437k
    SET_NODE(opline->op2, &arg_zvs[1]);
4802
437k
  }
4803
439k
  if (num_args >= 3) {
4804
109k
    zend_emit_op_data(&arg_zvs[2]);
4805
109k
  }
4806
439k
  return opnum;
4807
439k
}
4808
4809
static uint32_t zend_compile_frameless_icall(znode *result, const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4810
108k
{
4811
108k
  const zend_frameless_function_info *frameless_function_info = find_frameless_function_info(args, fbc, type);
4812
108k
  if (!frameless_function_info) {
4813
98.6k
    return (uint32_t)-1;
4814
98.6k
  }
4815
4816
9.75k
  return zend_compile_frameless_icall_ex(result, args, fbc, frameless_function_info, type);
4817
108k
}
4818
4819
static void zend_compile_ns_call(znode *result, const znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */
4820
2.18M
{
4821
2.18M
  int name_constants = zend_add_ns_func_name_literal(Z_STR(name_node->u.constant));
4822
4823
  /* Find frameless function with same name. */
4824
2.18M
  const zend_function *frameless_function = NULL;
4825
2.18M
  if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT
4826
2.17M
   && !zend_args_contain_unpack_or_named(zend_ast_get_list(args_ast))
4827
   /* Avoid blowing up op count with nested frameless branches. */
4828
2.16M
   && !CG(context).in_jmp_frameless_branch) {
4829
1.32M
    zend_string *lc_func_name = Z_STR_P(CT_CONSTANT_EX(CG(active_op_array), name_constants + 2));
4830
1.32M
    frameless_function = zend_hash_find_ptr(CG(function_table), lc_func_name);
4831
1.32M
  }
4832
4833
  /* Check whether any frameless handler may actually be used. */
4834
2.18M
  uint32_t jmp_fl_opnum = 0;
4835
2.18M
  const zend_frameless_function_info *frameless_function_info = NULL;
4836
2.18M
  if (frameless_function) {
4837
464k
    frameless_function_info = find_frameless_function_info(zend_ast_get_list(args_ast), frameless_function, type);
4838
464k
    if (frameless_function_info) {
4839
430k
      CG(context).in_jmp_frameless_branch = true;
4840
430k
      znode op1;
4841
430k
      op1.op_type = IS_CONST;
4842
430k
      ZVAL_COPY(&op1.u.constant, CT_CONSTANT_EX(CG(active_op_array), name_constants + 1));
4843
430k
      jmp_fl_opnum = get_next_op_number();
4844
430k
      zend_emit_op(NULL, ZEND_JMP_FRAMELESS, &op1, NULL);
4845
430k
    }
4846
464k
  }
4847
4848
  /* Compile ns call. */
4849
2.18M
  zend_op *opline = get_next_op();
4850
2.18M
  opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
4851
2.18M
  opline->op2_type = IS_CONST;
4852
2.18M
  opline->op2.constant = name_constants;
4853
2.18M
  opline->result.num = zend_alloc_cache_slot();
4854
2.18M
  zend_compile_call_common(result, args_ast, NULL, lineno, type);
4855
4856
  /* Compile frameless call. */
4857
2.18M
  if (frameless_function_info) {
4858
430k
    CG(zend_lineno) = lineno;
4859
4860
430k
    uint32_t jmp_end_opnum = zend_emit_jump(0);
4861
430k
    uint32_t jmp_fl_target = get_next_op_number();
4862
4863
430k
    uint32_t flf_icall_opnum = zend_compile_frameless_icall_ex(NULL, zend_ast_get_list(args_ast), frameless_function, frameless_function_info, type);
4864
4865
430k
    zend_op *jmp_fl = &CG(active_op_array)->opcodes[jmp_fl_opnum];
4866
430k
    jmp_fl->op2.opline_num = jmp_fl_target;
4867
430k
    jmp_fl->extended_value = zend_alloc_cache_slot();
4868
430k
    zend_op *flf_icall = &CG(active_op_array)->opcodes[flf_icall_opnum];
4869
430k
    SET_NODE(flf_icall->result, result);
4870
430k
    zend_update_jump_target_to_next(jmp_end_opnum);
4871
4872
430k
    CG(context).in_jmp_frameless_branch = false;
4873
430k
  }
4874
2.18M
}
4875
/* }}} */
4876
4877
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node);
4878
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node);
4879
static void zend_compile_rope_finalize(znode *result, uint32_t j, zend_op *init_opline, zend_op *opline);
4880
4881
static zend_result zend_compile_func_sprintf(znode *result, zend_ast_list *args) /* {{{ */
4882
3.71k
{
4883
  /* Bail out if we do not have a format string. */
4884
3.71k
  if (args->children < 1) {
4885
102
    return FAILURE;
4886
102
  }
4887
4888
3.61k
  zend_eval_const_expr(&args->child[0]);
4889
  /* Bail out if the format string is not constant. */
4890
3.61k
  if (args->child[0]->kind != ZEND_AST_ZVAL) {
4891
266
    return FAILURE;
4892
266
  }
4893
4894
3.34k
  zval *format_string = zend_ast_get_zval(args->child[0]);
4895
3.34k
  if (Z_TYPE_P(format_string) != IS_STRING) {
4896
52
    return FAILURE;
4897
52
  }
4898
3.29k
  if (Z_STRLEN_P(format_string) >= 256) {
4899
60
    return FAILURE;
4900
60
  }
4901
4902
3.23k
  char *p;
4903
3.23k
  char *end;
4904
3.23k
  uint32_t placeholder_count;
4905
4906
3.23k
  placeholder_count = 0;
4907
3.23k
  p = Z_STRVAL_P(format_string);
4908
3.23k
  end = p + Z_STRLEN_P(format_string);
4909
4910
6.72k
  for (;;) {
4911
6.72k
    p = memchr(p, '%', end - p);
4912
6.72k
    if (!p) {
4913
2.73k
      break;
4914
2.73k
    }
4915
4916
3.99k
    char *q = p + 1;
4917
3.99k
    if (q == end) {
4918
217
      return FAILURE;
4919
217
    }
4920
4921
3.77k
    switch (*q) {
4922
2.84k
      case 's':
4923
3.21k
      case 'd':
4924
3.21k
        placeholder_count++;
4925
3.21k
        break;
4926
277
      case '%':
4927
277
        break;
4928
286
      default:
4929
286
        return FAILURE;
4930
3.77k
    }
4931
4932
3.49k
    p = q;
4933
3.49k
    p++;
4934
3.49k
  }
4935
4936
  /* Bail out if the number of placeholders does not match the number of values. */
4937
2.73k
  if (placeholder_count != (args->children - 1)) {
4938
102
    return FAILURE;
4939
102
  }
4940
4941
  /* Handle empty format strings. */
4942
2.62k
  if (Z_STRLEN_P(format_string) == 0) {
4943
79
    result->op_type = IS_CONST;
4944
79
    ZVAL_EMPTY_STRING(&result->u.constant);
4945
4946
79
    return SUCCESS;
4947
79
  }
4948
4949
2.55k
  znode *elements = NULL;
4950
4951
2.55k
  if (placeholder_count > 0) {
4952
2.18k
    elements = safe_emalloc(sizeof(*elements), placeholder_count, 0);
4953
2.18k
  }
4954
4955
  /* Compile the value expressions first for error handling that is consistent
4956
   * with a function call: Values that fail to convert to a string may emit errors.
4957
   */
4958
5.62k
  for (uint32_t i = 0; i < placeholder_count; i++) {
4959
3.07k
    zend_compile_expr(elements + i, args->child[1 + i]);
4960
3.07k
  }
4961
4962
2.55k
  uint32_t rope_elements = 0;
4963
2.55k
  uint32_t rope_init_lineno = -1;
4964
2.55k
  zend_op *opline = NULL;
4965
4966
2.55k
  placeholder_count = 0;
4967
2.55k
  p = Z_STRVAL_P(format_string);
4968
2.55k
  end = p + Z_STRLEN_P(format_string);
4969
2.55k
  char *offset = p;
4970
5.80k
  for (;;) {
4971
5.80k
    p = memchr(p, '%', end - p);
4972
5.80k
    if (!p) {
4973
2.55k
      break;
4974
2.55k
    }
4975
4976
3.25k
    char *q = p + 1;
4977
3.25k
    ZEND_ASSERT(q < end);
4978
3.25k
    ZEND_ASSERT(*q == 's' || *q == 'd' || *q == '%');
4979
4980
3.25k
    if (*q == '%') {
4981
      /* Optimization to not create a dedicated rope element for the literal '%':
4982
       * Include the first '%' within the "constant" part instead of dropping the
4983
       * full placeholder.
4984
       */
4985
182
      p++;
4986
182
    }
4987
4988
3.25k
    if (p != offset) {
4989
2.21k
      znode const_node;
4990
2.21k
      const_node.op_type = IS_CONST;
4991
2.21k
      ZVAL_STRINGL(&const_node.u.constant, offset, p - offset);
4992
2.21k
      if (rope_elements == 0) {
4993
1.21k
        rope_init_lineno = get_next_op_number();
4994
1.21k
      }
4995
2.21k
      opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4996
2.21k
    }
4997
4998
3.25k
    if (*q != '%') {
4999
3.07k
      switch (*q) {
5000
2.78k
        case 's':
5001
          /* Perform the cast of constants when actually evaluating the corresponding placeholder
5002
           * for correct error reporting.
5003
           */
5004
2.78k
          if (elements[placeholder_count].op_type == IS_CONST) {
5005
600
            if (Z_TYPE(elements[placeholder_count].u.constant) == IS_ARRAY) {
5006
94
              zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_STRING;
5007
506
            } else {
5008
506
              convert_to_string(&elements[placeholder_count].u.constant);
5009
506
            }
5010
600
          }
5011
2.78k
          break;
5012
289
        case 'd':
5013
289
          zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_LONG;
5014
289
          break;
5015
0
        EMPTY_SWITCH_DEFAULT_CASE();
5016
3.07k
      }
5017
5018
3.07k
      if (rope_elements == 0) {
5019
984
        rope_init_lineno = get_next_op_number();
5020
984
      }
5021
3.07k
      opline = zend_compile_rope_add(result, rope_elements++, &elements[placeholder_count]);
5022
5023
3.07k
      placeholder_count++;
5024
3.07k
    }
5025
5026
3.25k
    p = q;
5027
3.25k
    p++;
5028
3.25k
    offset = p;
5029
3.25k
  }
5030
2.55k
  if (end != offset) {
5031
    /* Add the constant part after the last placeholder. */
5032
2.29k
    znode const_node;
5033
2.29k
    const_node.op_type = IS_CONST;
5034
2.29k
    ZVAL_STRINGL(&const_node.u.constant, offset, end - offset);
5035
2.29k
    if (rope_elements == 0) {
5036
347
      rope_init_lineno = get_next_op_number();
5037
347
    }
5038
2.29k
    opline = zend_compile_rope_add(result, rope_elements++, &const_node);
5039
2.29k
  }
5040
2.55k
  ZEND_ASSERT(opline != NULL);
5041
5042
2.55k
  zend_op *init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
5043
2.55k
  zend_compile_rope_finalize(result, rope_elements, init_opline, opline);
5044
2.55k
  efree(elements);
5045
5046
2.55k
  return SUCCESS;
5047
2.55k
}
5048
5049
static zend_result zend_compile_func_printf(znode *result, zend_ast_list *args) /* {{{ */
5050
2.84k
{
5051
  /* Special case: printf with a single constant string argument and no format specifiers.
5052
   * In this case, just emit ECHO and return the string length if needed. */
5053
2.84k
  if (args->children == 1) {
5054
726
    zend_eval_const_expr(&args->child[0]);
5055
726
    if (args->child[0]->kind != ZEND_AST_ZVAL) {
5056
46
      return FAILURE;
5057
46
    }
5058
680
    zval *format_string = zend_ast_get_zval(args->child[0]);
5059
680
    if (Z_TYPE_P(format_string) != IS_STRING) {
5060
127
      return FAILURE;
5061
127
    }
5062
    /* Check if there are any format specifiers */
5063
553
    if (!memchr(Z_STRVAL_P(format_string), '%', Z_STRLEN_P(format_string))) {
5064
      /* No format specifiers - just emit ECHO and return string length */
5065
316
      znode format_node;
5066
316
      zend_compile_expr(&format_node, args->child[0]);
5067
316
      zend_emit_op(NULL, ZEND_ECHO, &format_node, NULL);
5068
5069
      /* Return the string length as a constant if the result is used */
5070
316
      result->op_type = IS_CONST;
5071
316
      ZVAL_LONG(&result->u.constant, Z_STRLEN_P(format_string));
5072
316
      return SUCCESS;
5073
316
    }
5074
553
  }
5075
5076
  /* Fall back to sprintf optimization for format strings with specifiers */
5077
2.35k
  znode rope_result;
5078
2.35k
  if (zend_compile_func_sprintf(&rope_result, args) != SUCCESS) {
5079
450
    return FAILURE;
5080
450
  }
5081
5082
  /* printf() returns the amount of bytes written, so just an ECHO of the
5083
   * resulting sprintf() optimisation might not be enough. At this early
5084
   * stage we can't detect if the result is actually used, so we just emit
5085
   * the opcodes and let them be cleaned up by the dead code elimination
5086
   * pass in the Zend Optimizer if the result of the printf() is in fact
5087
   * unused */
5088
1.90k
  znode copy;
5089
1.90k
  if (rope_result.op_type != IS_CONST) {
5090
    /* Note: ZEND_COPY_TMP is only valid for TMPVAR. */
5091
1.73k
    ZEND_ASSERT(rope_result.op_type == IS_TMP_VAR);
5092
1.73k
    zend_emit_op_tmp(&copy, ZEND_COPY_TMP, &rope_result, NULL);
5093
1.73k
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5094
1.73k
    zend_emit_op_tmp(result, ZEND_STRLEN, &copy, NULL);
5095
1.73k
  } else {
5096
168
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5097
168
    result->op_type = IS_CONST;
5098
168
    ZVAL_LONG(&result->u.constant, Z_STRLEN(rope_result.u.constant));
5099
168
  }
5100
5101
1.90k
  return SUCCESS;
5102
1.90k
}
5103
5104
static zend_result zend_compile_func_clone(znode *result, const zend_ast_list *args)
5105
6.85k
{
5106
6.85k
  znode arg_node;
5107
5108
6.85k
  if (args->children != 1) {
5109
241
    return FAILURE;
5110
241
  }
5111
5112
6.61k
  zend_compile_expr(&arg_node, args->child[0]);
5113
6.61k
  zend_emit_op_tmp(result, ZEND_CLONE, &arg_node, NULL);
5114
5115
6.61k
  return SUCCESS;
5116
6.85k
}
5117
5118
static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t lineno) /* {{{ */
5119
369
{
5120
  /* Bail out if we do not have exactly two parameters. */
5121
369
  if (args->children != 2) {
5122
28
    return FAILURE;
5123
28
  }
5124
5125
341
  zend_ast *callback = args->child[0];
5126
5127
  /* Bail out if the callback is not a FCC/PFA. */
5128
341
  zend_ast *args_ast;
5129
341
  switch (callback->kind) {
5130
53
    case ZEND_AST_CALL:
5131
57
    case ZEND_AST_STATIC_CALL:
5132
57
      args_ast = zend_ast_call_get_args(callback);
5133
57
      if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT) {
5134
7
        return FAILURE;
5135
7
      }
5136
5137
50
      break;
5138
284
    default:
5139
284
      return FAILURE;
5140
341
  }
5141
5142
  /* Bail out if the callback is assert() due to the AST stringification logic
5143
   * breaking for the generated call.
5144
   */
5145
50
  if (callback->kind == ZEND_AST_CALL
5146
46
   && callback->child[0]->kind == ZEND_AST_ZVAL 
5147
44
   && Z_TYPE_P(zend_ast_get_zval(callback->child[0])) == IS_STRING
5148
44
   && zend_string_equals_literal_ci(zend_ast_get_str(callback->child[0]), "assert")) {
5149
0
    return FAILURE;
5150
0
  }
5151
5152
50
  zend_ast_list *callback_args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args);
5153
50
  if (callback_args->children != 1 || callback_args->child[0]->attr != ZEND_PLACEHOLDER_VARIADIC) {
5154
    /* Full PFA is not yet implemented, will fail in zend_compile_call_common(). */
5155
1
    return FAILURE;
5156
1
  }
5157
5158
49
  znode value;
5159
49
  value.op_type = IS_TMP_VAR;
5160
49
  value.u.op.var = get_temporary_variable();
5161
49
  zend_ast *call_args = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&value));
5162
5163
49
  zend_op *opline;
5164
5165
49
  znode array;
5166
49
  zend_compile_expr(&array, args->child[1]);
5167
  /* array is an argument to both ZEND_TYPE_ASSERT and to ZEND_FE_RESET_R. */
5168
49
  if (array.op_type == IS_CONST) {
5169
25
    Z_TRY_ADDREF(array.u.constant);
5170
25
  }
5171
5172
  /* Verify that the input array actually is an array. */
5173
49
  znode name;
5174
49
  name.op_type = IS_CONST;
5175
49
  ZVAL_STR_COPY(&name.u.constant, lcname);
5176
49
  opline = zend_emit_op(NULL, ZEND_TYPE_ASSERT, &name, &array);
5177
49
  opline->lineno = lineno;
5178
49
  opline->extended_value = (2 << 16) | IS_ARRAY;
5179
49
  const zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5180
49
  const Bucket *fbc_bucket = (const Bucket*)((uintptr_t)fbc_zv - XtOffsetOf(Bucket, val));
5181
49
  Z_EXTRA_P(CT_CONSTANT(opline->op1)) = fbc_bucket - CG(function_table)->arData;
5182
5183
  /* Initialize the result array. */
5184
49
  zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
5185
5186
  /* foreach loop starts here. */
5187
49
  znode key;
5188
5189
49
  uint32_t opnum_reset = get_next_op_number();
5190
49
  znode reset_node;
5191
49
  zend_emit_op(&reset_node, ZEND_FE_RESET_R, &array, NULL);
5192
49
  zend_begin_loop(ZEND_FE_FREE, &reset_node, false);
5193
49
  uint32_t opnum_fetch = get_next_op_number();
5194
49
  zend_emit_op_tmp(&key, ZEND_FE_FETCH_R, &reset_node, &value);
5195
5196
  /* loop body */
5197
49
  znode call_result;
5198
49
  switch (callback->kind) {
5199
45
    case ZEND_AST_CALL:
5200
45
      zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_CALL, callback->child[0], call_args));
5201
45
      break;
5202
4
    case ZEND_AST_STATIC_CALL:
5203
4
      zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_STATIC_CALL, callback->child[0], callback->child[1], call_args));
5204
4
      break;
5205
49
  }
5206
49
  opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT, &call_result, &key);
5207
49
  SET_NODE(opline->result, result);
5208
  /* end loop body */
5209
5210
49
  zend_emit_jump(opnum_fetch);
5211
5212
49
  uint32_t opnum_loop_end = get_next_op_number();
5213
49
  opline = &CG(active_op_array)->opcodes[opnum_reset];
5214
49
  opline->op2.opline_num = opnum_loop_end;
5215
49
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
5216
49
  opline->extended_value = opnum_loop_end;
5217
5218
49
  zend_end_loop(opnum_fetch, &reset_node);
5219
49
  zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
5220
5221
49
  return SUCCESS;
5222
49
}
5223
5224
static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type, uint32_t lineno) /* {{{ */
5225
136k
{
5226
136k
  if (zend_string_equals_literal(lcname, "strlen")) {
5227
845
    return zend_compile_func_strlen(result, args);
5228
135k
  } else if (zend_string_equals_literal(lcname, "is_null")) {
5229
219
    return zend_compile_func_typecheck(result, args, IS_NULL);
5230
135k
  } else if (zend_string_equals_literal(lcname, "is_bool")) {
5231
93
    return zend_compile_func_typecheck(result, args, _IS_BOOL);
5232
135k
  } else if (zend_string_equals_literal(lcname, "is_long")
5233
135k
    || zend_string_equals_literal(lcname, "is_int")
5234
135k
    || zend_string_equals_literal(lcname, "is_integer")
5235
135k
  ) {
5236
334
    return zend_compile_func_typecheck(result, args, IS_LONG);
5237
135k
  } else if (zend_string_equals_literal(lcname, "is_float")
5238
134k
    || zend_string_equals_literal(lcname, "is_double")
5239
135k
  ) {
5240
495
    return zend_compile_func_typecheck(result, args, IS_DOUBLE);
5241
134k
  } else if (zend_string_equals_literal(lcname, "is_string")) {
5242
39
    return zend_compile_func_typecheck(result, args, IS_STRING);
5243
134k
  } else if (zend_string_equals_literal(lcname, "is_array")) {
5244
200
    return zend_compile_func_typecheck(result, args, IS_ARRAY);
5245
134k
  } else if (zend_string_equals_literal(lcname, "is_object")) {
5246
122
    return zend_compile_func_typecheck(result, args, IS_OBJECT);
5247
134k
  } else if (zend_string_equals_literal(lcname, "is_resource")) {
5248
81
    return zend_compile_func_typecheck(result, args, IS_RESOURCE);
5249
134k
  } else if (zend_string_equals_literal(lcname, "is_scalar")) {
5250
303
    return zend_compile_func_is_scalar(result, args);
5251
133k
  } else if (zend_string_equals_literal(lcname, "boolval")) {
5252
303
    return zend_compile_func_cast(result, args, _IS_BOOL);
5253
133k
  } else if (zend_string_equals_literal(lcname, "intval")) {
5254
96
    return zend_compile_func_cast(result, args, IS_LONG);
5255
133k
  } else if (zend_string_equals_literal(lcname, "floatval")
5256
133k
    || zend_string_equals_literal(lcname, "doubleval")
5257
133k
  ) {
5258
444
    return zend_compile_func_cast(result, args, IS_DOUBLE);
5259
133k
  } else if (zend_string_equals_literal(lcname, "strval")) {
5260
324
    return zend_compile_func_cast(result, args, IS_STRING);
5261
132k
  } else if (zend_string_equals_literal(lcname, "defined")) {
5262
1.68k
    return zend_compile_func_defined(result, args);
5263
131k
  } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) {
5264
1.14k
    return zend_compile_func_chr(result, args);
5265
129k
  } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) {
5266
533
    return zend_compile_func_ord(result, args);
5267
129k
  } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
5268
2.20k
    return zend_compile_func_cufa(result, args, lcname, type);
5269
127k
  } else if (zend_string_equals_literal(lcname, "call_user_func")) {
5270
7.65k
    return zend_compile_func_cuf(result, args, lcname, type);
5271
119k
  } else if (zend_string_equals_literal(lcname, "in_array")) {
5272
10.8k
    return zend_compile_func_in_array(result, args);
5273
108k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_COUNT))
5274
107k
      || zend_string_equals_literal(lcname, "sizeof")) {
5275
1.06k
    return zend_compile_func_count(result, args, lcname);
5276
107k
  } else if (zend_string_equals_literal(lcname, "get_class")) {
5277
1.26k
    return zend_compile_func_get_class(result, args);
5278
106k
  } else if (zend_string_equals_literal(lcname, "get_called_class")) {
5279
208
    return zend_compile_func_get_called_class(result, args);
5280
106k
  } else if (zend_string_equals_literal(lcname, "gettype")) {
5281
242
    return zend_compile_func_gettype(result, args);
5282
105k
  } else if (zend_string_equals_literal(lcname, "func_num_args")) {
5283
210
    return zend_compile_func_num_args(result, args);
5284
105k
  } else if (zend_string_equals_literal(lcname, "func_get_args")) {
5285
675
    return zend_compile_func_get_args(result, args);
5286
105k
  } else if (zend_string_equals_literal(lcname, "array_slice")) {
5287
2.15k
    return zend_compile_func_array_slice(result, args);
5288
102k
  } else if (zend_string_equals_literal(lcname, "array_key_exists")) {
5289
286
    return zend_compile_func_array_key_exists(result, args);
5290
102k
  } else if (zend_string_equals_literal(lcname, "sprintf")) {
5291
1.36k
    return zend_compile_func_sprintf(result, args);
5292
101k
  } else if (zend_string_equals_literal(lcname, "printf")) {
5293
2.84k
    return zend_compile_func_printf(result, args);
5294
98.4k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_CLONE))) {
5295
6.85k
    return zend_compile_func_clone(result, args);
5296
91.5k
  } else if (zend_string_equals_literal(lcname, "array_map")) {
5297
369
    return zend_compile_func_array_map(result, args, lcname, lineno);
5298
91.2k
  } else {
5299
91.2k
    return FAILURE;
5300
91.2k
  }
5301
136k
}
5302
5303
static zend_result zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, const zend_function *fbc, uint32_t type, uint32_t lineno) /* {{{ */
5304
157k
{
5305
157k
  if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
5306
0
    return FAILURE;
5307
0
  }
5308
5309
157k
  if (fbc->type != ZEND_INTERNAL_FUNCTION) {
5310
    /* If the function is part of disabled_functions, it may be redeclared as a userland
5311
     * function with a different implementation. Don't use the VM builtin in that case. */
5312
20.5k
    return FAILURE;
5313
20.5k
  }
5314
5315
137k
  if (zend_args_contain_unpack_or_named(args)) {
5316
742
    return FAILURE;
5317
742
  }
5318
5319
136k
  if (zend_try_compile_special_func_ex(result, lcname, args, type, lineno) == SUCCESS) {
5320
28.2k
    return SUCCESS;
5321
28.2k
  }
5322
5323
108k
  return zend_compile_frameless_icall(result, args, fbc, type) != (uint32_t)-1 ? SUCCESS : FAILURE;
5324
136k
}
5325
5326
5
static const char *zend_get_cstring_from_property_hook_kind(zend_property_hook_kind kind) {
5327
5
  switch (kind) {
5328
0
    case ZEND_PROPERTY_HOOK_GET:
5329
0
      return "get";
5330
5
    case ZEND_PROPERTY_HOOK_SET:
5331
5
      return "set";
5332
5
    EMPTY_SWITCH_DEFAULT_CASE()
5333
5
  }
5334
5
}
5335
5336
static zend_string *zend_copy_unmangled_prop_name(zend_string *prop_name)
5337
481
{
5338
481
  if (ZSTR_VAL(prop_name)[0] != '\0') {
5339
393
    return zend_string_copy(prop_name);
5340
393
  } else {
5341
88
    const char *unmangled = zend_get_unmangled_property_name(prop_name);
5342
88
    return zend_string_init(unmangled, strlen(unmangled), /* persistent */ false);
5343
88
  }
5344
481
}
5345
5346
static bool zend_compile_parent_property_hook_call(znode *result, const zend_ast *ast, uint32_t type)
5347
34.1k
{
5348
34.1k
  ZEND_ASSERT(ast->kind == ZEND_AST_STATIC_CALL);
5349
5350
34.1k
  const zend_ast *class_ast = ast->child[0];
5351
34.1k
  zend_ast *method_ast = ast->child[1];
5352
5353
  /* Recognize parent::$prop::get() pattern. */
5354
34.1k
  if (class_ast->kind != ZEND_AST_STATIC_PROP
5355
1.53k
   || (class_ast->attr & ZEND_PARENTHESIZED_STATIC_PROP)
5356
1.45k
   || class_ast->child[0]->kind != ZEND_AST_ZVAL
5357
1.27k
   || Z_TYPE_P(zend_ast_get_zval(class_ast->child[0])) != IS_STRING
5358
1.27k
   || zend_get_class_fetch_type(zend_ast_get_str(class_ast->child[0])) != ZEND_FETCH_CLASS_PARENT
5359
853
   || class_ast->child[1]->kind != ZEND_AST_ZVAL
5360
778
   || method_ast->kind != ZEND_AST_ZVAL
5361
729
   || Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING
5362
729
   || (!zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "get")
5363
33.6k
    && !zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "set"))) {
5364
33.6k
    return false;
5365
33.6k
  }
5366
5367
542
  zend_class_entry *ce = CG(active_class_entry);
5368
542
  if (!ce) {
5369
9
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"parent\" when no class scope is active");
5370
9
  }
5371
5372
533
  zend_ast *args_ast = ast->child[2];
5373
533
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
5374
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for parent property hook call");
5375
7
  }
5376
5377
526
  zval *property_hook_name_zv = zend_ast_get_zval(class_ast->child[1]);
5378
526
  zend_string *property_name = zval_get_string(property_hook_name_zv);
5379
526
  zend_string *hook_name = zend_ast_get_str(method_ast);
5380
526
  zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(hook_name);
5381
526
  ZEND_ASSERT(hook_kind != (uint32_t)-1);
5382
5383
526
  const zend_string *prop_info_name = CG(context).active_property_info_name;
5384
526
  if (!prop_info_name) {
5385
5
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() outside a property hook",
5386
5
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name));
5387
5
  }
5388
5389
521
  const char *unmangled_prop_name = zend_get_unmangled_property_name(prop_info_name);
5390
521
  if (!zend_string_equals_cstr(property_name, unmangled_prop_name, strlen(unmangled_prop_name))) {
5391
26
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property ($%s)",
5392
26
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), unmangled_prop_name);
5393
26
  }
5394
495
  if (hook_kind != CG(context).active_property_hook_kind) {
5395
5
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property hook (%s)",
5396
5
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), zend_get_cstring_from_property_hook_kind(CG(context).active_property_hook_kind));
5397
5
  }
5398
5399
490
  zend_op *opline = get_next_op();
5400
490
  opline->opcode = ZEND_INIT_PARENT_PROPERTY_HOOK_CALL;
5401
490
  opline->op1_type = IS_CONST;
5402
490
  opline->op1.constant = zend_add_literal_string(&property_name);
5403
490
  opline->op2.num = hook_kind;
5404
5405
490
  zend_compile_call_common(result, args_ast, NULL, zend_ast_get_lineno(method_ast), BP_VAR_R);
5406
5407
490
  return true;
5408
495
}
5409
5410
static void zend_compile_call(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
5411
2.49M
{
5412
2.49M
  zend_ast *name_ast = ast->child[0];
5413
2.49M
  zend_ast *args_ast = ast->child[1];
5414
2.49M
  bool is_callable_convert = args_ast->kind == ZEND_AST_CALLABLE_CONVERT;
5415
5416
2.49M
  znode name_node;
5417
5418
2.49M
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
5419
78.1k
    zend_compile_expr(&name_node, name_ast);
5420
78.1k
    zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno, type);
5421
78.1k
    return;
5422
78.1k
  }
5423
5424
2.41M
  {
5425
2.41M
    bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
5426
2.41M
    if (runtime_resolution) {
5427
2.19M
      if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")
5428
8.05k
          && !is_callable_convert) {
5429
7.93k
        zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL, ast->lineno, type);
5430
2.18M
      } else {
5431
2.18M
        zend_compile_ns_call(result, &name_node, args_ast, ast->lineno, type);
5432
2.18M
      }
5433
2.19M
      return;
5434
2.19M
    }
5435
2.41M
  }
5436
5437
224k
  {
5438
224k
    const zval *name = &name_node.u.constant;
5439
224k
    zend_string *lcname = zend_string_tolower(Z_STR_P(name));
5440
224k
    zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5441
224k
    const zend_function *fbc = fbc_zv ? Z_PTR_P(fbc_zv) : NULL;
5442
224k
    zend_op *opline;
5443
5444
    /* Special assert() handling should apply independently of compiler flags. */
5445
224k
    if (fbc && zend_string_equals_literal(lcname, "assert") && !is_callable_convert) {
5446
17.4k
      zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc, ast->lineno, type);
5447
17.4k
      zend_string_release(lcname);
5448
17.4k
      zval_ptr_dtor(&name_node.u.constant);
5449
17.4k
      return;
5450
17.4k
    }
5451
5452
207k
    if (!fbc
5453
158k
     || !fbc_is_finalized(fbc)
5454
158k
     || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
5455
48.9k
      zend_string_release_ex(lcname, 0);
5456
48.9k
      zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno, type);
5457
48.9k
      return;
5458
48.9k
    }
5459
5460
158k
    if (!is_callable_convert &&
5461
157k
        zend_try_compile_special_func(result, lcname,
5462
157k
        zend_ast_get_list(args_ast), fbc, type, ast->lineno) == SUCCESS
5463
158k
    ) {
5464
38.0k
      zend_string_release_ex(lcname, 0);
5465
38.0k
      zval_ptr_dtor(&name_node.u.constant);
5466
38.0k
      return;
5467
38.0k
    }
5468
5469
120k
    zval_ptr_dtor(&name_node.u.constant);
5470
120k
    ZVAL_NEW_STR(&name_node.u.constant, lcname);
5471
5472
120k
    opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
5473
120k
    opline->result.num = zend_alloc_cache_slot();
5474
5475
    /* Store offset to function from symbol table in op2.extra. */
5476
120k
    if (fbc->type == ZEND_INTERNAL_FUNCTION) {
5477
99.7k
      const Bucket *fbc_bucket = (const Bucket*)((uintptr_t)fbc_zv - XtOffsetOf(Bucket, val));
5478
99.7k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = fbc_bucket - CG(function_table)->arData;
5479
99.7k
    }
5480
5481
120k
    zend_compile_call_common(result, args_ast, fbc, ast->lineno, type);
5482
120k
  }
5483
120k
}
5484
/* }}} */
5485
5486
static void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5487
80.6k
{
5488
80.6k
  zend_ast *obj_ast = ast->child[0];
5489
80.6k
  zend_ast *method_ast = ast->child[1];
5490
80.6k
  zend_ast *args_ast = ast->child[2];
5491
5492
80.6k
  znode obj_node, method_node;
5493
80.6k
  zend_op *opline;
5494
80.6k
  const zend_function *fbc = NULL;
5495
80.6k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL;
5496
80.6k
  uint32_t short_circuiting_checkpoint = zend_short_circuiting_checkpoint();
5497
5498
80.6k
  if (is_this_fetch(obj_ast)) {
5499
1.31k
    if (this_guaranteed_exists()) {
5500
1.22k
      obj_node.op_type = IS_UNUSED;
5501
1.22k
    } else {
5502
87
      zend_emit_op_tmp(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
5503
87
    }
5504
1.31k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
5505
5506
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
5507
     * check for a nullsafe access. */
5508
79.3k
  } else {
5509
79.3k
    zend_short_circuiting_mark_inner(obj_ast);
5510
79.3k
    zend_compile_expr(&obj_node, obj_ast);
5511
79.3k
    if (nullsafe) {
5512
2.74k
      zend_emit_jmp_null(&obj_node, type);
5513
2.74k
    }
5514
79.3k
  }
5515
5516
80.6k
  zend_compile_expr(&method_node, method_ast);
5517
80.6k
  opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
5518
5519
80.6k
  if (method_node.op_type == IS_CONST) {
5520
78.6k
    if (Z_TYPE(method_node.u.constant) != IS_STRING) {
5521
10
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5522
10
    }
5523
5524
78.6k
    opline->op2_type = IS_CONST;
5525
78.6k
    opline->op2.constant = zend_add_func_name_literal(
5526
78.6k
      Z_STR(method_node.u.constant));
5527
78.6k
    opline->result.num = zend_alloc_cache_slots(2);
5528
78.6k
  } else {
5529
1.98k
    SET_NODE(opline->op2, &method_node);
5530
1.98k
  }
5531
5532
  /* Check if this calls a known method on $this */
5533
80.6k
  if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST &&
5534
1.15k
      CG(active_class_entry) && zend_is_scope_known()) {
5535
725
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5536
725
    fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname);
5537
5538
    /* We only know the exact method that is being called if it is either private or final.
5539
     * Otherwise an overriding method in a child class may be called. */
5540
725
    if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) {
5541
283
      fbc = NULL;
5542
283
    }
5543
725
  }
5544
5545
80.6k
  if (zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast), type)) {
5546
241
    if (short_circuiting_checkpoint != zend_short_circuiting_checkpoint()) {
5547
12
      zend_error_noreturn(E_COMPILE_ERROR,
5548
12
        "Cannot combine nullsafe operator with Closure creation");
5549
12
    }
5550
241
  }
5551
80.6k
}
5552
/* }}} */
5553
5554
static bool zend_is_constructor(const zend_string *name) /* {{{ */
5555
33.8k
{
5556
33.8k
  return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
5557
33.8k
}
5558
/* }}} */
5559
5560
static bool is_func_accessible(const zend_function *fbc)
5561
39.5k
{
5562
39.5k
  if ((fbc->common.fn_flags & ZEND_ACC_PUBLIC) || fbc->common.scope == CG(active_class_entry)) {
5563
39.3k
    return true;
5564
39.3k
  }
5565
5566
166
  if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE)
5567
84
    && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED)
5568
84
    && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED))
5569
17
    && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) {
5570
0
    return true;
5571
0
  }
5572
5573
166
  return false;
5574
166
}
5575
5576
static const zend_function *zend_get_compatible_func_or_null(const zend_class_entry *ce, zend_string *lcname) /* {{{ */
5577
4.51k
{
5578
4.51k
  const zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname);
5579
5580
4.51k
  if (!fbc || is_func_accessible(fbc)) {
5581
4.35k
    return fbc;
5582
4.35k
  }
5583
5584
158
  return NULL;
5585
4.51k
}
5586
/* }}} */
5587
5588
static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5589
34.1k
{
5590
34.1k
  zend_ast *class_ast = ast->child[0];
5591
34.1k
  zend_ast *method_ast = ast->child[1];
5592
34.1k
  zend_ast *args_ast = ast->child[2];
5593
5594
34.1k
  znode class_node, method_node;
5595
34.1k
  zend_op *opline;
5596
34.1k
  const zend_function *fbc = NULL;
5597
5598
34.1k
  if (zend_compile_parent_property_hook_call(result, ast, type)) {
5599
490
    return;
5600
490
  }
5601
5602
33.6k
  zend_short_circuiting_mark_inner(class_ast);
5603
33.6k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5604
5605
33.6k
  zend_compile_expr(&method_node, method_ast);
5606
5607
33.6k
  if (method_node.op_type == IS_CONST) {
5608
32.8k
    zval *name = &method_node.u.constant;
5609
32.8k
    if (Z_TYPE_P(name) != IS_STRING) {
5610
5
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5611
5
    }
5612
32.8k
    if (zend_is_constructor(Z_STR_P(name))) {
5613
331
      zval_ptr_dtor(name);
5614
331
      method_node.op_type = IS_UNUSED;
5615
331
    }
5616
32.8k
  }
5617
5618
33.6k
  opline = get_next_op();
5619
33.6k
  opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
5620
5621
33.6k
  zend_set_class_name_op1(opline, &class_node);
5622
5623
33.6k
  if (method_node.op_type == IS_CONST) {
5624
32.5k
    opline->op2_type = IS_CONST;
5625
32.5k
    opline->op2.constant = zend_add_func_name_literal(
5626
32.5k
      Z_STR(method_node.u.constant));
5627
32.5k
    opline->result.num = zend_alloc_cache_slots(2);
5628
32.5k
  } else {
5629
1.18k
    if (opline->op1_type == IS_CONST) {
5630
308
      opline->result.num = zend_alloc_cache_slot();
5631
308
    }
5632
1.18k
    SET_NODE(opline->op2, &method_node);
5633
1.18k
  }
5634
5635
  /* Check if we already know which method we're calling */
5636
33.6k
  if (opline->op2_type == IS_CONST) {
5637
32.5k
    zend_class_entry *ce = NULL;
5638
32.5k
    if (opline->op1_type == IS_CONST) {
5639
15.3k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5640
15.3k
      ce = zend_hash_find_ptr(CG(class_table), lcname);
5641
15.3k
      if (ce) {
5642
4.09k
        if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5643
0
          ce = NULL;
5644
0
        }
5645
11.2k
      } else if (CG(active_class_entry)
5646
550
          && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5647
248
        ce = CG(active_class_entry);
5648
248
      }
5649
17.1k
    } else if (opline->op1_type == IS_UNUSED
5650
12.9k
        && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5651
12.1k
        && zend_is_scope_known()) {
5652
164
      ce = CG(active_class_entry);
5653
164
    }
5654
32.5k
    if (ce) {
5655
4.51k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5656
4.51k
      fbc = zend_get_compatible_func_or_null(ce, lcname);
5657
4.51k
    }
5658
32.5k
  }
5659
5660
33.6k
  zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast), type);
5661
33.6k
}
5662
/* }}} */
5663
5664
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel);
5665
5666
static void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
5667
81.8k
{
5668
81.8k
  zend_ast *class_ast = ast->child[0];
5669
81.8k
  zend_ast *args_ast = ast->child[1];
5670
5671
81.8k
  znode class_node, ctor_result;
5672
81.8k
  zend_op *opline;
5673
5674
81.8k
  if (class_ast->kind == ZEND_AST_CLASS) {
5675
    /* anon class declaration */
5676
1.83k
    zend_compile_class_decl(&class_node, class_ast, false);
5677
79.9k
  } else {
5678
79.9k
    zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5679
79.9k
  }
5680
5681
81.8k
  opline = zend_emit_op_tmp(result, ZEND_NEW, NULL, NULL);
5682
5683
81.8k
  zend_set_class_name_op1(opline, &class_node);
5684
5685
81.8k
  if (opline->op1_type == IS_CONST) {
5686
78.6k
    opline->op2.num = zend_alloc_cache_slot();
5687
78.6k
  }
5688
5689
81.8k
  zend_class_entry *ce = NULL;
5690
81.8k
  if (opline->op1_type == IS_CONST) {
5691
78.6k
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5692
78.6k
    ce = zend_hash_find_ptr(CG(class_table), lcname);
5693
78.6k
    if (ce) {
5694
48.5k
      if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5695
0
        ce = NULL;
5696
0
      }
5697
48.5k
    } else if (CG(active_class_entry)
5698
1.10k
        && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5699
450
      ce = CG(active_class_entry);
5700
450
    }
5701
78.6k
  } else if (opline->op1_type == IS_UNUSED
5702
385
      && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5703
199
      && zend_is_scope_known()) {
5704
117
    ce = CG(active_class_entry);
5705
117
  }
5706
5707
5708
81.8k
  const zend_function *fbc = NULL;
5709
81.8k
  if (ce
5710
49.0k
      && ce->default_object_handlers->get_constructor == zend_std_get_constructor
5711
49.0k
      && ce->constructor
5712
35.5k
      && is_func_accessible(ce->constructor)) {
5713
35.5k
    fbc = ce->constructor;
5714
35.5k
  }
5715
5716
81.8k
  zend_compile_call_common(&ctor_result, args_ast, fbc, ast->lineno, BP_VAR_R);
5717
81.8k
  zend_do_free(&ctor_result);
5718
81.8k
}
5719
/* }}} */
5720
5721
static void zend_compile_global_var(zend_ast *ast) /* {{{ */
5722
2.90k
{
5723
2.90k
  zend_ast *var_ast = ast->child[0];
5724
2.90k
  zend_ast *name_ast = var_ast->child[0];
5725
5726
2.90k
  znode name_node, result;
5727
5728
2.90k
  zend_compile_expr(&name_node, name_ast);
5729
2.90k
  if (name_node.op_type == IS_CONST) {
5730
2.56k
    convert_to_string(&name_node.u.constant);
5731
2.56k
  }
5732
5733
  // TODO(GLOBALS) Forbid "global $GLOBALS"?
5734
2.90k
  if (is_this_fetch(var_ast)) {
5735
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable");
5736
2.90k
  } else if (zend_try_compile_cv(&result, var_ast, BP_VAR_R) == SUCCESS) {
5737
2.20k
    zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
5738
2.20k
    opline->extended_value = zend_alloc_cache_slot();
5739
2.20k
  } else {
5740
    /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W
5741
     * to not free the name_node operand, so it can be reused in the following
5742
     * ASSIGN_REF, which then frees it. */
5743
692
    zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
5744
692
    opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
5745
5746
692
    if (name_node.op_type == IS_CONST) {
5747
349
      zend_string_addref(Z_STR(name_node.u.constant));
5748
349
    }
5749
5750
692
    zend_emit_assign_ref_znode(
5751
692
      zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)),
5752
692
      &result
5753
692
    );
5754
692
  }
5755
2.90k
}
5756
/* }}} */
5757
5758
static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */
5759
61.0k
{
5760
61.0k
  zend_op *opline;
5761
61.0k
  if (!CG(active_op_array)->static_variables) {
5762
0
    if (CG(active_op_array)->scope) {
5763
0
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5764
0
    }
5765
0
    CG(active_op_array)->static_variables = zend_new_array(8);
5766
0
  }
5767
5768
61.0k
  value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value);
5769
5770
61.0k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5771
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5772
0
  }
5773
5774
61.0k
  opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL);
5775
61.0k
  opline->op1_type = IS_CV;
5776
61.0k
  opline->op1.var = lookup_cv(var_name);
5777
61.0k
  opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode;
5778
61.0k
}
5779
/* }}} */
5780
5781
static void zend_compile_static_var(zend_ast *ast) /* {{{ */
5782
14.8k
{
5783
14.8k
  zend_ast *var_ast = ast->child[0];
5784
14.8k
  zend_string *var_name = zend_ast_get_str(var_ast);
5785
5786
14.8k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5787
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5788
5
  }
5789
5790
14.8k
  if (!CG(active_op_array)->static_variables) {
5791
14.2k
    if (CG(active_op_array)->scope) {
5792
13.3k
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5793
13.3k
    }
5794
14.2k
    CG(active_op_array)->static_variables = zend_new_array(8);
5795
14.2k
  }
5796
5797
14.8k
  if (zend_hash_exists(CG(active_op_array)->static_variables, var_name)) {
5798
13
    zend_error_noreturn_unchecked(E_COMPILE_ERROR, "Duplicate declaration of static variable $%S", var_name);
5799
13
  }
5800
5801
14.8k
  zend_eval_const_expr(&ast->child[1]);
5802
14.8k
  zend_ast *value_ast = ast->child[1];
5803
5804
14.8k
  if (!value_ast || value_ast->kind == ZEND_AST_ZVAL) {
5805
14.4k
    zval *value_zv = value_ast
5806
14.4k
      ? zend_ast_get_zval(value_ast)
5807
14.4k
      : &EG(uninitialized_zval);
5808
14.4k
    Z_TRY_ADDREF_P(value_zv);
5809
14.4k
    zend_compile_static_var_common(var_name, value_zv, ZEND_BIND_REF);
5810
14.4k
  } else {
5811
381
    zend_op *opline;
5812
5813
381
    zval *placeholder_ptr = zend_hash_update(CG(active_op_array)->static_variables, var_name, &EG(uninitialized_zval));
5814
381
    uint32_t placeholder_offset = (uint32_t)((char*)placeholder_ptr - (char*)CG(active_op_array)->static_variables->arData);
5815
5816
381
    uint32_t static_def_jmp_opnum = get_next_op_number();
5817
381
    opline = zend_emit_op(NULL, ZEND_BIND_INIT_STATIC_OR_JMP, NULL, NULL);
5818
381
    opline->op1_type = IS_CV;
5819
381
    opline->op1.var = lookup_cv(var_name);
5820
381
    opline->extended_value = placeholder_offset;
5821
5822
381
    znode expr;
5823
381
    zend_compile_expr(&expr, value_ast);
5824
5825
381
    opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, &expr);
5826
381
    opline->op1_type = IS_CV;
5827
381
    opline->op1.var = lookup_cv(var_name);
5828
381
    opline->extended_value = placeholder_offset | ZEND_BIND_REF;
5829
5830
381
    zend_update_jump_target_to_next(static_def_jmp_opnum);
5831
381
  }
5832
14.8k
}
5833
/* }}} */
5834
5835
static void zend_compile_unset(const zend_ast *ast) /* {{{ */
5836
6.75k
{
5837
6.75k
  zend_ast *var_ast = ast->child[0];
5838
6.75k
  znode var_node;
5839
6.75k
  zend_op *opline;
5840
5841
6.75k
  zend_ensure_writable_variable(var_ast);
5842
5843
6.75k
  if (is_global_var_fetch(var_ast)) {
5844
582
    if (!var_ast->child[1]) {
5845
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
5846
5
    }
5847
5848
577
    zend_compile_expr(&var_node, var_ast->child[1]);
5849
577
    if (var_node.op_type == IS_CONST) {
5850
554
      convert_to_string(&var_node.u.constant);
5851
554
    }
5852
5853
577
    opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL);
5854
577
    opline->extended_value = ZEND_FETCH_GLOBAL;
5855
577
    return;
5856
582
  }
5857
5858
6.17k
  switch (var_ast->kind) {
5859
3.05k
    case ZEND_AST_VAR:
5860
3.05k
      if (is_this_fetch(var_ast)) {
5861
8
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this");
5862
3.04k
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_UNSET) == SUCCESS) {
5863
2.84k
        opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL);
5864
2.84k
      } else {
5865
196
        opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, false);
5866
196
        opline->opcode = ZEND_UNSET_VAR;
5867
196
      }
5868
3.04k
      return;
5869
3.04k
    case ZEND_AST_DIM:
5870
2.03k
      opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET, /* by_ref */ false);
5871
2.03k
      opline->opcode = ZEND_UNSET_DIM;
5872
2.03k
      return;
5873
1.05k
    case ZEND_AST_PROP:
5874
1.05k
    case ZEND_AST_NULLSAFE_PROP:
5875
1.05k
      opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, false);
5876
1.05k
      opline->opcode = ZEND_UNSET_OBJ;
5877
1.05k
      return;
5878
32
    case ZEND_AST_STATIC_PROP:
5879
32
      opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, false, false);
5880
32
      opline->opcode = ZEND_UNSET_STATIC_PROP;
5881
32
      return;
5882
6.17k
    EMPTY_SWITCH_DEFAULT_CASE()
5883
6.17k
  }
5884
6.17k
}
5885
/* }}} */
5886
5887
static bool zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */
5888
37.9k
{
5889
37.9k
  const zend_loop_var *base;
5890
37.9k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5891
5892
37.9k
  if (!loop_var) {
5893
1.22k
    return 1;
5894
1.22k
  }
5895
36.7k
  base = zend_stack_base(&CG(loop_var_stack));
5896
42.7k
  for (; loop_var >= base; loop_var--) {
5897
41.4k
    if (loop_var->opcode == ZEND_FAST_CALL) {
5898
1.91k
      zend_op *opline = get_next_op();
5899
5900
1.91k
      opline->opcode = ZEND_FAST_CALL;
5901
1.91k
      opline->result_type = IS_TMP_VAR;
5902
1.91k
      opline->result.var = loop_var->var_num;
5903
1.91k
      if (return_value) {
5904
660
        SET_NODE(opline->op2, return_value);
5905
660
      }
5906
1.91k
      opline->op1.num = loop_var->try_catch_offset;
5907
39.5k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5908
1.05k
      zend_op *opline = get_next_op();
5909
1.05k
      opline->opcode = ZEND_DISCARD_EXCEPTION;
5910
1.05k
      opline->op1_type = IS_TMP_VAR;
5911
1.05k
      opline->op1.var = loop_var->var_num;
5912
38.4k
    } else if (loop_var->opcode == ZEND_RETURN) {
5913
      /* Stack separator */
5914
33.7k
      break;
5915
33.7k
    } else if (depth <= 1) {
5916
1.68k
      return 1;
5917
3.01k
    } else if (loop_var->opcode == ZEND_NOP) {
5918
      /* Loop doesn't have freeable variable */
5919
1.79k
      depth--;
5920
1.79k
    } else {
5921
1.22k
      zend_op *opline;
5922
5923
1.22k
      ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR));
5924
1.22k
      opline = get_next_op();
5925
1.22k
      opline->opcode = loop_var->opcode;
5926
1.22k
      opline->op1_type = loop_var->var_type;
5927
1.22k
      opline->op1.var = loop_var->var_num;
5928
1.22k
      opline->extended_value = ZEND_FREE_ON_RETURN;
5929
1.22k
      depth--;
5930
1.22k
      }
5931
41.4k
  }
5932
35.0k
  return (depth == 0);
5933
36.7k
}
5934
/* }}} */
5935
5936
static bool zend_handle_loops_and_finally(znode *return_value) /* {{{ */
5937
36.2k
{
5938
36.2k
  return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value);
5939
36.2k
}
5940
/* }}} */
5941
5942
static bool zend_has_finally_ex(zend_long depth) /* {{{ */
5943
937
{
5944
937
  const zend_loop_var *base;
5945
937
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5946
5947
937
  if (!loop_var) {
5948
225
    return 0;
5949
225
  }
5950
712
  base = zend_stack_base(&CG(loop_var_stack));
5951
1.32k
  for (; loop_var >= base; loop_var--) {
5952
1.30k
    if (loop_var->opcode == ZEND_FAST_CALL) {
5953
219
      return 1;
5954
1.08k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5955
595
    } else if (loop_var->opcode == ZEND_RETURN) {
5956
      /* Stack separator */
5957
476
      return 0;
5958
476
    } else if (depth <= 1) {
5959
0
      return 0;
5960
119
    } else {
5961
119
      depth--;
5962
119
      }
5963
1.30k
  }
5964
17
  return 0;
5965
712
}
5966
/* }}} */
5967
5968
static bool zend_has_finally(void) /* {{{ */
5969
937
{
5970
937
  return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1);
5971
937
}
5972
/* }}} */
5973
5974
static void zend_compile_return(const zend_ast *ast) /* {{{ */
5975
34.9k
{
5976
34.9k
  zend_ast *expr_ast = ast->child[0];
5977
34.9k
  bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0;
5978
34.9k
  bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
5979
5980
34.9k
  znode expr_node;
5981
34.9k
  zend_op *opline;
5982
5983
34.9k
  if (is_generator) {
5984
    /* For generators the by-ref flag refers to yields, not returns */
5985
2.57k
    by_ref = false;
5986
2.57k
  }
5987
5988
34.9k
  if (!expr_ast) {
5989
1.65k
    expr_node.op_type = IS_CONST;
5990
1.65k
    ZVAL_NULL(&expr_node.u.constant);
5991
33.2k
  } else if (by_ref && zend_is_variable_or_call(expr_ast)) {
5992
1.94k
    zend_assert_not_short_circuited(expr_ast);
5993
1.94k
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
5994
31.3k
  } else {
5995
31.3k
    zend_compile_expr(&expr_node, expr_ast);
5996
31.3k
  }
5997
5998
34.9k
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
5999
1.51k
   && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
6000
937
   && zend_has_finally()) {
6001
    /* Copy return value into temporary VAR to avoid modification in finally code */
6002
219
    if (by_ref) {
6003
70
      zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
6004
149
    } else {
6005
149
      zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL);
6006
149
    }
6007
219
  }
6008
6009
  /* Generator return types are handled separately */
6010
34.9k
  if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
6011
6.50k
    zend_emit_return_type_check(
6012
6.50k
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
6013
6.50k
  }
6014
6015
34.9k
  uint32_t opnum_before_finally = get_next_op_number();
6016
6017
34.9k
  zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
6018
6019
  /* Content of reference might have changed in finally, repeat type check. */
6020
34.9k
  if (by_ref
6021
   /* Check if any opcodes were emitted since the last return type check. */
6022
4.17k
   && opnum_before_finally != get_next_op_number()
6023
783
   && !is_generator
6024
783
   && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
6025
18
    zend_emit_return_type_check(
6026
18
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
6027
18
  }
6028
6029
34.9k
  opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
6030
34.9k
    &expr_node, NULL);
6031
6032
34.9k
  if (by_ref && expr_ast) {
6033
2.95k
    if (zend_is_call(expr_ast)) {
6034
308
      opline->extended_value = ZEND_RETURNS_FUNCTION;
6035
2.64k
    } else if (!zend_is_variable(expr_ast) || zend_ast_is_short_circuited(expr_ast)) {
6036
1.02k
      opline->extended_value = ZEND_RETURNS_VALUE;
6037
1.02k
    }
6038
2.95k
  }
6039
34.9k
}
6040
/* }}} */
6041
6042
static void zend_compile_void_cast(znode *result, const zend_ast *ast)
6043
611
{
6044
611
  zend_ast *expr_ast = ast->child[0];
6045
611
  znode expr_node;
6046
611
  zend_op *opline;
6047
6048
611
  zend_compile_expr(&expr_node, expr_ast);
6049
6050
611
  switch (expr_node.op_type) {
6051
468
    case IS_TMP_VAR:
6052
468
    case IS_VAR:
6053
468
      opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6054
468
      opline->extended_value = ZEND_FREE_VOID_CAST;
6055
468
      break;
6056
122
    case IS_CONST:
6057
122
      zend_do_free(&expr_node);
6058
122
      break;
6059
611
  }
6060
611
}
6061
6062
static void zend_compile_echo(const zend_ast *ast) /* {{{ */
6063
1.35M
{
6064
1.35M
  zend_op *opline;
6065
1.35M
  zend_ast *expr_ast = ast->child[0];
6066
6067
1.35M
  znode expr_node;
6068
1.35M
  zend_compile_expr(&expr_node, expr_ast);
6069
6070
1.35M
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
6071
1.35M
  opline->extended_value = 0;
6072
1.35M
}
6073
/* }}} */
6074
6075
static void zend_compile_throw(znode *result, const zend_ast *ast) /* {{{ */
6076
3.39k
{
6077
3.39k
  zend_ast *expr_ast = ast->child[0];
6078
6079
3.39k
  znode expr_node;
6080
3.39k
  zend_compile_expr(&expr_node, expr_ast);
6081
6082
3.39k
  zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
6083
3.39k
  if (result) {
6084
    /* Mark this as an "expression throw" for opcache. */
6085
759
    opline->extended_value = ZEND_THROW_IS_EXPR;
6086
759
    result->op_type = IS_CONST;
6087
759
    ZVAL_TRUE(&result->u.constant);
6088
759
  }
6089
3.39k
}
6090
/* }}} */
6091
6092
static void zend_compile_break_continue(const zend_ast *ast) /* {{{ */
6093
1.82k
{
6094
1.82k
  zend_ast *depth_ast = ast->child[0];
6095
6096
1.82k
  zend_op *opline;
6097
1.82k
  zend_long depth;
6098
6099
1.82k
  ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
6100
6101
1.82k
  if (depth_ast) {
6102
319
    const zval *depth_zv;
6103
319
    if (depth_ast->kind != ZEND_AST_ZVAL) {
6104
18
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand "
6105
18
        "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
6106
18
    }
6107
6108
301
    depth_zv = zend_ast_get_zval(depth_ast);
6109
301
    if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
6110
9
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers",
6111
9
        ast->kind == ZEND_AST_BREAK ? "break" : "continue");
6112
9
    }
6113
6114
292
    depth = Z_LVAL_P(depth_zv);
6115
1.50k
  } else {
6116
1.50k
    depth = 1;
6117
1.50k
  }
6118
6119
1.79k
  if (CG(context).current_brk_cont == -1) {
6120
34
    zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
6121
34
      ast->kind == ZEND_AST_BREAK ? "break" : "continue");
6122
1.76k
  } else {
6123
1.76k
    if (!zend_handle_loops_and_finally_ex(depth, NULL)) {
6124
79
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s",
6125
79
        ast->kind == ZEND_AST_BREAK ? "break" : "continue",
6126
79
        depth, depth == 1 ? "" : "s");
6127
79
    }
6128
1.76k
  }
6129
6130
1.68k
  if (ast->kind == ZEND_AST_CONTINUE) {
6131
847
    int d, cur = CG(context).current_brk_cont;
6132
913
    for (d = depth - 1; d > 0; d--) {
6133
66
      cur = CG(context).brk_cont_array[cur].parent;
6134
66
      ZEND_ASSERT(cur != -1);
6135
66
    }
6136
6137
847
    if (CG(context).brk_cont_array[cur].is_switch) {
6138
207
      if (depth == 1) {
6139
179
        if (CG(context).brk_cont_array[cur].parent == -1) {
6140
98
          zend_error(E_WARNING,
6141
98
            "\"continue\" targeting switch is equivalent to \"break\"");
6142
98
        } else {
6143
81
          zend_error(E_WARNING,
6144
81
            "\"continue\" targeting switch is equivalent to \"break\". " \
6145
81
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
6146
81
            depth + 1);
6147
81
        }
6148
179
      } else {
6149
28
        if (CG(context).brk_cont_array[cur].parent == -1) {
6150
15
          zend_error(E_WARNING,
6151
15
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"",
6152
15
            depth, depth);
6153
15
        } else {
6154
13
          zend_error(E_WARNING,
6155
13
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
6156
13
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
6157
13
            depth, depth, depth + 1);
6158
13
        }
6159
28
      }
6160
207
    }
6161
847
  }
6162
6163
1.68k
  opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
6164
1.68k
  opline->op1.num = CG(context).current_brk_cont;
6165
1.68k
  opline->op2.num = depth;
6166
1.68k
}
6167
/* }}} */
6168
6169
void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */
6170
1.17k
{
6171
1.17k
  zend_label *dest;
6172
1.17k
  int remove_oplines = opline->op1.num;
6173
1.17k
  zval *label;
6174
1.17k
  uint32_t opnum = opline - op_array->opcodes;
6175
6176
1.17k
  label = CT_CONSTANT_EX(op_array, opline->op2.constant);
6177
1.17k
  if (CG(context).labels == NULL ||
6178
1.15k
      (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL
6179
1.17k
  ) {
6180
57
    CG(in_compilation) = 1;
6181
57
    CG(active_op_array) = op_array;
6182
57
    CG(zend_lineno) = opline->lineno;
6183
57
    zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
6184
57
  }
6185
6186
1.11k
  zval_ptr_dtor_str(label);
6187
1.11k
  ZVAL_NULL(label);
6188
6189
1.11k
  uint32_t current = opline->extended_value;
6190
2.33k
  for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) {
6191
1.22k
    if (current == -1) {
6192
8
      CG(in_compilation) = 1;
6193
8
      CG(active_op_array) = op_array;
6194
8
      CG(zend_lineno) = opline->lineno;
6195
8
      zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
6196
8
    }
6197
1.21k
    if (CG(context).brk_cont_array[current].start >= 0) {
6198
725
      remove_oplines--;
6199
725
    }
6200
1.21k
  }
6201
6202
2.41k
  for (current = 0; current < op_array->last_try_catch; ++current) {
6203
1.46k
    const zend_try_catch_element *elem = &op_array->try_catch_array[current];
6204
1.46k
    if (elem->try_op > opnum) {
6205
157
      break;
6206
157
    }
6207
1.31k
    if (elem->finally_op && opnum < elem->finally_op - 1
6208
800
      && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op)
6209
1.31k
    ) {
6210
376
      remove_oplines--;
6211
376
    }
6212
1.31k
  }
6213
6214
1.10k
  opline->opcode = ZEND_JMP;
6215
1.10k
  SET_UNUSED(opline->op1);
6216
1.10k
  SET_UNUSED(opline->op2);
6217
1.10k
  SET_UNUSED(opline->result);
6218
1.10k
  opline->op1.opline_num = dest->opline_num;
6219
1.10k
  opline->extended_value = 0;
6220
6221
1.10k
  ZEND_ASSERT(remove_oplines >= 0);
6222
1.58k
  while (remove_oplines--) {
6223
482
    opline--;
6224
482
    MAKE_NOP(opline);
6225
482
    ZEND_VM_SET_OPCODE_HANDLER(opline);
6226
482
  }
6227
1.10k
}
6228
/* }}} */
6229
6230
static void zend_compile_goto(const zend_ast *ast) /* {{{ */
6231
1.42k
{
6232
1.42k
  zend_ast *label_ast = ast->child[0];
6233
1.42k
  znode label_node;
6234
1.42k
  zend_op *opline;
6235
6236
1.42k
  zend_compile_expr(&label_node, label_ast);
6237
6238
  /* Label resolution and unwinding adjustments happen in pass two. */
6239
1.42k
  uint32_t opnum_start = get_next_op_number();
6240
1.42k
  zend_handle_loops_and_finally(NULL);
6241
1.42k
  opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
6242
1.42k
  opline->op1.num = get_next_op_number() - opnum_start - 1;
6243
1.42k
  opline->extended_value = CG(context).current_brk_cont;
6244
1.42k
}
6245
/* }}} */
6246
6247
static void zend_compile_label(const zend_ast *ast) /* {{{ */
6248
2.59k
{
6249
2.59k
  zend_string *label = zend_ast_get_str(ast->child[0]);
6250
2.59k
  zend_label dest;
6251
6252
2.59k
  if (!CG(context).labels) {
6253
1.90k
    ALLOC_HASHTABLE(CG(context).labels);
6254
1.90k
    zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0);
6255
1.90k
  }
6256
6257
2.59k
  dest.brk_cont = CG(context).current_brk_cont;
6258
2.59k
  dest.opline_num = get_next_op_number();
6259
6260
2.59k
  if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
6261
39
    zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
6262
39
  }
6263
2.59k
}
6264
/* }}} */
6265
6266
static void zend_compile_while(const zend_ast *ast) /* {{{ */
6267
1.74k
{
6268
1.74k
  zend_ast *cond_ast = ast->child[0];
6269
1.74k
  zend_ast *stmt_ast = ast->child[1];
6270
1.74k
  znode cond_node;
6271
1.74k
  uint32_t opnum_start, opnum_jmp, opnum_cond;
6272
6273
1.74k
  opnum_jmp = zend_emit_jump(0);
6274
6275
1.74k
  zend_begin_loop(ZEND_NOP, NULL, false);
6276
6277
1.74k
  opnum_start = get_next_op_number();
6278
1.74k
  zend_compile_stmt(stmt_ast);
6279
6280
1.74k
  opnum_cond = get_next_op_number();
6281
1.74k
  zend_update_jump_target(opnum_jmp, opnum_cond);
6282
1.74k
  zend_compile_expr(&cond_node, cond_ast);
6283
6284
1.74k
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6285
6286
1.74k
  zend_end_loop(opnum_cond, NULL);
6287
1.74k
}
6288
/* }}} */
6289
6290
static void zend_compile_do_while(const zend_ast *ast) /* {{{ */
6291
484
{
6292
484
  zend_ast *stmt_ast = ast->child[0];
6293
484
  zend_ast *cond_ast = ast->child[1];
6294
6295
484
  znode cond_node;
6296
484
  uint32_t opnum_start, opnum_cond;
6297
6298
484
  zend_begin_loop(ZEND_NOP, NULL, false);
6299
6300
484
  opnum_start = get_next_op_number();
6301
484
  zend_compile_stmt(stmt_ast);
6302
6303
484
  opnum_cond = get_next_op_number();
6304
484
  zend_compile_expr(&cond_node, cond_ast);
6305
6306
484
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6307
6308
484
  zend_end_loop(opnum_cond, NULL);
6309
484
}
6310
/* }}} */
6311
6312
static void zend_compile_for_expr_list(znode *result, zend_ast *ast) /* {{{ */
6313
36.5k
{
6314
36.5k
  const zend_ast_list *list;
6315
36.5k
  uint32_t i;
6316
6317
36.5k
  result->op_type = IS_CONST;
6318
36.5k
  ZVAL_TRUE(&result->u.constant);
6319
6320
36.5k
  if (!ast) {
6321
9.19k
    return;
6322
9.19k
  }
6323
6324
27.4k
  list = zend_ast_get_list(ast);
6325
56.8k
  for (i = 0; i < list->children; ++i) {
6326
29.4k
    zend_ast *expr_ast = list->child[i];
6327
6328
29.4k
    zend_do_free(result);
6329
29.4k
    if (expr_ast->kind == ZEND_AST_CAST_VOID) {
6330
187
      zend_compile_void_cast(NULL, expr_ast);
6331
187
      result->op_type = IS_CONST;
6332
187
      ZVAL_NULL(&result->u.constant);
6333
29.3k
    } else {
6334
29.3k
      zend_compile_expr(result, expr_ast);
6335
29.3k
    }
6336
29.4k
  }
6337
27.4k
}
6338
/* }}} */
6339
6340
static void zend_compile_for(const zend_ast *ast) /* {{{ */
6341
12.2k
{
6342
12.2k
  zend_ast *init_ast = ast->child[0];
6343
12.2k
  zend_ast *cond_ast = ast->child[1];
6344
12.2k
  zend_ast *loop_ast = ast->child[2];
6345
12.2k
  zend_ast *stmt_ast = ast->child[3];
6346
6347
12.2k
  znode result;
6348
12.2k
  uint32_t opnum_start, opnum_jmp, opnum_loop;
6349
6350
12.2k
  zend_compile_for_expr_list(&result, init_ast);
6351
12.2k
  zend_do_free(&result);
6352
6353
12.2k
  opnum_jmp = zend_emit_jump(0);
6354
6355
12.2k
  zend_begin_loop(ZEND_NOP, NULL, false);
6356
6357
12.2k
  opnum_start = get_next_op_number();
6358
12.2k
  zend_compile_stmt(stmt_ast);
6359
6360
12.2k
  opnum_loop = get_next_op_number();
6361
12.2k
  zend_compile_for_expr_list(&result, loop_ast);
6362
12.2k
  zend_do_free(&result);
6363
6364
12.2k
  zend_update_jump_target_to_next(opnum_jmp);
6365
12.2k
  zend_compile_for_expr_list(&result, cond_ast);
6366
12.2k
  zend_do_extended_stmt(NULL);
6367
6368
12.2k
  zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
6369
6370
12.2k
  zend_end_loop(opnum_loop, NULL);
6371
12.2k
}
6372
/* }}} */
6373
6374
static void zend_compile_foreach(zend_ast *ast) /* {{{ */
6375
18.0k
{
6376
18.0k
  zend_ast *expr_ast = ast->child[0];
6377
18.0k
  zend_ast *value_ast = ast->child[1];
6378
18.0k
  zend_ast *key_ast = ast->child[2];
6379
18.0k
  zend_ast *stmt_ast = ast->child[3];
6380
18.0k
  bool by_ref = value_ast->kind == ZEND_AST_REF;
6381
18.0k
  bool is_variable = (zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast))
6382
3.99k
    || zend_is_call(expr_ast);
6383
6384
18.0k
  znode expr_node, reset_node, value_node, key_node;
6385
18.0k
  zend_op *opline;
6386
18.0k
  uint32_t opnum_reset, opnum_fetch;
6387
6388
18.0k
  if (key_ast) {
6389
1.59k
    if (key_ast->kind == ZEND_AST_REF) {
6390
9
      zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
6391
9
    }
6392
1.58k
    if (key_ast->kind == ZEND_AST_ARRAY) {
6393
8
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
6394
8
    }
6395
1.58k
  }
6396
6397
18.0k
  if (by_ref) {
6398
1.55k
    value_ast = value_ast->child[0];
6399
1.55k
  }
6400
6401
18.0k
  if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
6402
140
    by_ref = true;
6403
140
  }
6404
6405
18.0k
  if (by_ref && is_variable) {
6406
1.12k
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
6407
16.8k
  } else {
6408
16.8k
    zend_compile_expr(&expr_node, expr_ast);
6409
16.8k
  }
6410
6411
18.0k
  if (by_ref) {
6412
1.69k
    zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
6413
1.69k
  }
6414
6415
18.0k
  opnum_reset = get_next_op_number();
6416
18.0k
  opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
6417
18.0k
  if (!by_ref) {
6418
16.3k
    opline->result_type = IS_TMP_VAR;
6419
16.3k
    reset_node.op_type = IS_TMP_VAR;
6420
16.3k
  }
6421
6422
18.0k
  zend_begin_loop(ZEND_FE_FREE, &reset_node, false);
6423
6424
18.0k
  opnum_fetch = get_next_op_number();
6425
18.0k
  opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
6426
6427
18.0k
  if (is_this_fetch(value_ast)) {
6428
8
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6429
18.0k
  } else if (value_ast->kind == ZEND_AST_VAR &&
6430
16.9k
    zend_try_compile_cv(&value_node, value_ast, BP_VAR_R) == SUCCESS) {
6431
16.7k
    SET_NODE(opline->op2, &value_node);
6432
16.7k
  } else {
6433
1.21k
    opline->op2_type = by_ref ? IS_VAR : IS_TMP_VAR;
6434
1.21k
    opline->op2.var = get_temporary_variable();
6435
1.21k
    GET_NODE(&value_node, opline->op2);
6436
1.21k
    if (value_ast->kind == ZEND_AST_ARRAY) {
6437
583
      zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr, BP_VAR_R);
6438
630
    } else if (by_ref) {
6439
407
      zend_emit_assign_ref_znode(value_ast, &value_node);
6440
407
    } else {
6441
223
      zend_emit_assign_znode(value_ast, &value_node);
6442
223
    }
6443
1.21k
  }
6444
6445
18.0k
  if (key_ast) {
6446
1.57k
    opline = &CG(active_op_array)->opcodes[opnum_fetch];
6447
1.57k
    zend_make_tmp_result(&key_node, opline);
6448
1.57k
    zend_emit_assign_znode(key_ast, &key_node);
6449
1.57k
  }
6450
6451
18.0k
  zend_compile_stmt(stmt_ast);
6452
6453
  /* Place JMP and FE_FREE on the line where foreach starts. It would be
6454
   * better to use the end line, but this information is not available
6455
   * currently. */
6456
18.0k
  CG(zend_lineno) = ast->lineno;
6457
18.0k
  zend_emit_jump(opnum_fetch);
6458
6459
18.0k
  opline = &CG(active_op_array)->opcodes[opnum_reset];
6460
18.0k
  opline->op2.opline_num = get_next_op_number();
6461
6462
18.0k
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
6463
18.0k
  opline->extended_value = get_next_op_number();
6464
6465
18.0k
  zend_end_loop(opnum_fetch, &reset_node);
6466
6467
18.0k
  opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
6468
18.0k
}
6469
/* }}} */
6470
6471
static void zend_compile_if(zend_ast *ast) /* {{{ */
6472
46.6k
{
6473
46.6k
  const zend_ast_list *list = zend_ast_get_list(ast);
6474
46.6k
  uint32_t i;
6475
46.6k
  uint32_t *jmp_opnums = NULL;
6476
6477
46.6k
  if (list->children > 1) {
6478
12.9k
    jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
6479
12.9k
  }
6480
6481
117k
  for (i = 0; i < list->children; ++i) {
6482
70.4k
    const zend_ast *elem_ast = list->child[i];
6483
70.4k
    zend_ast *cond_ast = elem_ast->child[0];
6484
70.4k
    zend_ast *stmt_ast = elem_ast->child[1];
6485
6486
70.4k
    if (cond_ast) {
6487
58.0k
      znode cond_node;
6488
58.0k
      uint32_t opnum_jmpz;
6489
6490
58.0k
      if (i > 0) {
6491
11.4k
        CG(zend_lineno) = cond_ast->lineno;
6492
11.4k
        zend_do_extended_stmt(NULL);
6493
11.4k
      }
6494
6495
58.0k
      zend_compile_expr(&cond_node, cond_ast);
6496
58.0k
      opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6497
6498
58.0k
      zend_compile_stmt(stmt_ast);
6499
6500
58.0k
      if (i != list->children - 1) {
6501
        /* Set the lineno of JMP to the position of the if keyword, as we don't want to
6502
         * report the last line in the if branch as covered if it hasn't actually executed. */
6503
23.8k
        CG(zend_lineno) = elem_ast->lineno;
6504
23.8k
        jmp_opnums[i] = zend_emit_jump(0);
6505
23.8k
      }
6506
58.0k
      zend_update_jump_target_to_next(opnum_jmpz);
6507
58.0k
    } else {
6508
      /* "else" can only occur as last element. */
6509
12.4k
      ZEND_ASSERT(i == list->children - 1);
6510
12.4k
      zend_compile_stmt(stmt_ast);
6511
12.4k
    }
6512
70.4k
  }
6513
6514
46.6k
  if (list->children > 1) {
6515
36.7k
    for (i = 0; i < list->children - 1; ++i) {
6516
23.8k
      zend_update_jump_target_to_next(jmp_opnums[i]);
6517
23.8k
    }
6518
12.9k
    efree(jmp_opnums);
6519
12.9k
  }
6520
46.6k
}
6521
/* }}} */
6522
6523
5.59k
static uint8_t determine_switch_jumptable_type(const zend_ast_list *cases) {
6524
5.59k
  uint32_t i;
6525
5.59k
  uint8_t common_type = IS_UNDEF;
6526
15.6k
  for (i = 0; i < cases->children; i++) {
6527
12.7k
    zend_ast *case_ast = cases->child[i];
6528
12.7k
    zend_ast **cond_ast = &case_ast->child[0];
6529
12.7k
    const zval *cond_zv;
6530
12.7k
    if (!case_ast->child[0]) {
6531
      /* Skip default clause */
6532
288
      continue;
6533
288
    }
6534
6535
12.5k
    zend_eval_const_expr(cond_ast);
6536
12.5k
    if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6537
      /* Non-constant case */
6538
327
      return IS_UNDEF;
6539
327
    }
6540
6541
12.1k
    cond_zv = zend_ast_get_zval(case_ast->child[0]);
6542
12.1k
    if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6543
      /* We only optimize switched on integers and strings */
6544
2.05k
      return IS_UNDEF;
6545
2.05k
    }
6546
6547
10.1k
    if (common_type == IS_UNDEF) {
6548
4.88k
      common_type = Z_TYPE_P(cond_zv);
6549
5.24k
    } else if (common_type != Z_TYPE_P(cond_zv)) {
6550
      /* Non-uniform case types */
6551
79
      return IS_UNDEF;
6552
79
    }
6553
6554
10.0k
    if (Z_TYPE_P(cond_zv) == IS_STRING
6555
9.08k
        && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) {
6556
      /* Numeric strings cannot be compared with a simple hash lookup */
6557
266
      return IS_UNDEF;
6558
266
    }
6559
10.0k
  }
6560
6561
2.86k
  return common_type;
6562
5.59k
}
6563
6564
2.53k
static bool should_use_jumptable(const zend_ast_list *cases, uint8_t jumptable_type) {
6565
2.53k
  if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) {
6566
0
    return 0;
6567
0
  }
6568
6569
  /* Thresholds are chosen based on when the average switch time for equidistributed
6570
   * input becomes smaller when using the jumptable optimization. */
6571
2.53k
  if (jumptable_type == IS_LONG) {
6572
164
    return cases->children >= 5;
6573
2.37k
  } else {
6574
2.37k
    ZEND_ASSERT(jumptable_type == IS_STRING);
6575
2.37k
    return cases->children >= 2;
6576
2.37k
  }
6577
2.53k
}
6578
6579
static void zend_compile_switch(zend_ast *ast) /* {{{ */
6580
5.59k
{
6581
5.59k
  zend_ast *expr_ast = ast->child[0];
6582
5.59k
  zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
6583
6584
5.59k
  uint32_t i;
6585
5.59k
  bool has_default_case = false;
6586
6587
5.59k
  znode expr_node, case_node;
6588
5.59k
  zend_op *opline;
6589
5.59k
  uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1;
6590
5.59k
  uint8_t jumptable_type;
6591
5.59k
  HashTable *jumptable = NULL;
6592
6593
5.59k
  zend_compile_expr(&expr_node, expr_ast);
6594
6595
5.59k
  zend_begin_loop(ZEND_FREE, &expr_node, true);
6596
6597
5.59k
  case_node.op_type = IS_TMP_VAR;
6598
5.59k
  case_node.u.op.var = get_temporary_variable();
6599
6600
5.59k
  jumptable_type = determine_switch_jumptable_type(cases);
6601
5.59k
  if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) {
6602
2.04k
    znode jumptable_op;
6603
6604
2.04k
    ALLOC_HASHTABLE(jumptable);
6605
2.04k
    zend_hash_init(jumptable, cases->children, NULL, NULL, 0);
6606
2.04k
    jumptable_op.op_type = IS_CONST;
6607
2.04k
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6608
6609
2.04k
    opline = zend_emit_op(NULL,
6610
2.04k
      jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING,
6611
2.04k
      &expr_node, &jumptable_op);
6612
2.04k
    if (opline->op1_type == IS_CONST) {
6613
1.28k
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6614
1.28k
    }
6615
2.04k
    opnum_switch = opline - CG(active_op_array)->opcodes;
6616
2.04k
  }
6617
6618
5.59k
  jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
6619
19.6k
  for (i = 0; i < cases->children; ++i) {
6620
14.1k
    zend_ast *case_ast = cases->child[i];
6621
14.1k
    zend_ast *cond_ast = case_ast->child[0];
6622
14.1k
    znode cond_node;
6623
6624
14.1k
    if (case_ast->attr == ZEND_ALT_CASE_SYNTAX) {
6625
151
      CG(zend_lineno) = case_ast->lineno;
6626
151
      zend_error(E_DEPRECATED, "Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead");
6627
151
    }
6628
6629
14.1k
    if (!cond_ast) {
6630
303
      if (has_default_case) {
6631
20
        CG(zend_lineno) = case_ast->lineno;
6632
20
        zend_error_noreturn(E_COMPILE_ERROR,
6633
20
          "Switch statements may only contain one default clause");
6634
20
      }
6635
283
      has_default_case = true;
6636
283
      continue;
6637
303
    }
6638
6639
13.8k
    zend_compile_expr(&cond_node, cond_ast);
6640
6641
13.8k
    if (expr_node.op_type == IS_CONST
6642
9.13k
      && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
6643
630
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6644
13.1k
    } else if (expr_node.op_type == IS_CONST
6645
8.50k
      && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
6646
1.06k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
6647
12.1k
    } else {
6648
12.1k
      opline = zend_emit_op(NULL,
6649
12.1k
        (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL,
6650
12.1k
        &expr_node, &cond_node);
6651
12.1k
      SET_NODE(opline->result, &case_node);
6652
12.1k
      if (opline->op1_type == IS_CONST) {
6653
7.43k
        Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6654
7.43k
      }
6655
6656
12.1k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6657
12.1k
    }
6658
13.8k
  }
6659
6660
5.57k
  opnum_default_jmp = zend_emit_jump(0);
6661
6662
19.6k
  for (i = 0; i < cases->children; ++i) {
6663
14.0k
    zend_ast *case_ast = cases->child[i];
6664
14.0k
    zend_ast *cond_ast = case_ast->child[0];
6665
14.0k
    zend_ast *stmt_ast = case_ast->child[1];
6666
6667
14.0k
    if (cond_ast) {
6668
13.8k
      zend_update_jump_target_to_next(jmpnz_opnums[i]);
6669
6670
13.8k
      if (jumptable) {
6671
5.50k
        zval *cond_zv = zend_ast_get_zval(cond_ast);
6672
5.50k
        zval jmp_target;
6673
5.50k
        ZVAL_LONG(&jmp_target, get_next_op_number());
6674
6675
5.50k
        ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type);
6676
5.50k
        if (Z_TYPE_P(cond_zv) == IS_LONG) {
6677
603
          zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6678
4.90k
        } else {
6679
4.90k
          ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6680
4.90k
          zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6681
4.90k
        }
6682
5.50k
      }
6683
13.8k
    } else {
6684
263
      zend_update_jump_target_to_next(opnum_default_jmp);
6685
6686
263
      if (jumptable) {
6687
40
        ZEND_ASSERT(opnum_switch != (uint32_t)-1);
6688
40
        opline = &CG(active_op_array)->opcodes[opnum_switch];
6689
40
        opline->extended_value = get_next_op_number();
6690
40
      }
6691
263
    }
6692
6693
14.0k
    zend_compile_stmt(stmt_ast);
6694
14.0k
  }
6695
6696
5.57k
  if (!has_default_case) {
6697
5.30k
    zend_update_jump_target_to_next(opnum_default_jmp);
6698
6699
5.30k
    if (jumptable) {
6700
1.99k
      opline = &CG(active_op_array)->opcodes[opnum_switch];
6701
1.99k
      opline->extended_value = get_next_op_number();
6702
1.99k
    }
6703
5.30k
  }
6704
6705
5.57k
  zend_end_loop(get_next_op_number(), &expr_node);
6706
6707
5.57k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
6708
1.74k
    opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6709
1.74k
    opline->extended_value = ZEND_FREE_SWITCH;
6710
3.83k
  } else if (expr_node.op_type == IS_CONST) {
6711
3.52k
    zval_ptr_dtor_nogc(&expr_node.u.constant);
6712
3.52k
  }
6713
6714
5.57k
  efree(jmpnz_opnums);
6715
5.57k
}
6716
/* }}} */
6717
6718
static uint32_t count_match_conds(const zend_ast_list *arms)
6719
2.53k
{
6720
2.53k
  uint32_t num_conds = 0;
6721
6722
6.30k
  for (uint32_t i = 0; i < arms->children; i++) {
6723
3.77k
    const zend_ast *arm_ast = arms->child[i];
6724
3.77k
    if (arm_ast->child[0] == NULL) {
6725
651
      continue;
6726
651
    }
6727
6728
3.11k
    const zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6729
3.11k
    num_conds += conds->children;
6730
3.11k
  }
6731
6732
2.53k
  return num_conds;
6733
2.53k
}
6734
6735
2.53k
static bool can_match_use_jumptable(const zend_ast_list *arms) {
6736
4.72k
  for (uint32_t i = 0; i < arms->children; i++) {
6737
3.15k
    const zend_ast *arm_ast = arms->child[i];
6738
3.15k
    if (!arm_ast->child[0]) {
6739
      /* Skip default arm */
6740
503
      continue;
6741
503
    }
6742
6743
2.65k
    zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6744
7.06k
    for (uint32_t j = 0; j < conds->children; j++) {
6745
5.38k
      zend_ast **cond_ast = &conds->child[j];
6746
6747
5.38k
      zend_eval_const_expr(cond_ast);
6748
5.38k
      if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6749
813
        return 0;
6750
813
      }
6751
6752
4.56k
      const zval *cond_zv = zend_ast_get_zval(*cond_ast);
6753
4.56k
      if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6754
155
        return 0;
6755
155
      }
6756
4.56k
    }
6757
2.65k
  }
6758
6759
1.56k
  return 1;
6760
2.53k
}
6761
6762
static bool zend_is_pipe_optimizable_callable_name(zend_ast *ast)
6763
519
{
6764
519
  if (ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(ast)) == IS_STRING) {
6765
    /* Assert compilation adds a message operand, but this is incompatible with the
6766
     * pipe optimization that uses a temporary znode for the reference elimination.
6767
     * Therefore, disable the optimization for assert.
6768
     * Note that "assert" as a name is always treated as fully qualified. */
6769
438
    return !zend_string_equals_literal_ci(zend_ast_get_str(ast), "assert");
6770
438
  }
6771
6772
81
  return true;
6773
519
}
6774
6775
static void zend_compile_pipe(znode *result, zend_ast *ast, uint32_t type)
6776
64.5k
{
6777
64.5k
  zend_ast *operand_ast = ast->child[0];
6778
64.5k
  zend_ast *callable_ast = ast->child[1];
6779
6780
64.5k
  if (callable_ast->kind == ZEND_AST_ARROW_FUNC && !(callable_ast->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) {
6781
7
    zend_error_noreturn(E_COMPILE_ERROR, "Arrow functions on the right hand side of |> must be parenthesized");
6782
7
  }
6783
6784
  /* Compile the left hand side down to a value first. */
6785
64.5k
  znode operand_result;
6786
64.5k
  zend_compile_expr(&operand_result, operand_ast);
6787
6788
  /* Wrap simple values in a ZEND_QM_ASSIGN opcode to ensure references
6789
   * always fail. They will already fail in complex cases like arrays,
6790
   * so those don't need a wrapper. */
6791
64.5k
  znode wrapped_operand_result;
6792
64.5k
  if (operand_result.op_type & (IS_CV|IS_VAR)) {
6793
65
    zend_emit_op_tmp(&wrapped_operand_result, ZEND_QM_ASSIGN, &operand_result, NULL);
6794
64.4k
  } else {
6795
64.4k
    wrapped_operand_result = operand_result;
6796
64.4k
  }
6797
6798
  /* Turn the operand into a function parameter list. */
6799
64.5k
  zend_ast *arg_list_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&wrapped_operand_result));
6800
6801
64.5k
  zend_ast *fcall_ast;
6802
64.5k
  znode callable_result;
6803
6804
  /* Turn $foo |> bar(...) into bar($foo). */
6805
64.5k
  if (callable_ast->kind == ZEND_AST_CALL
6806
833
    && callable_ast->child[1]->kind == ZEND_AST_CALLABLE_CONVERT
6807
519
    && zend_is_pipe_optimizable_callable_name(callable_ast->child[0])) {
6808
496
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6809
496
        callable_ast->child[0], arg_list_ast);
6810
  /* Turn $foo |> bar::baz(...) into bar::baz($foo). */
6811
64.0k
  } else if (callable_ast->kind == ZEND_AST_STATIC_CALL
6812
205
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6813
103
    fcall_ast = zend_ast_create(ZEND_AST_STATIC_CALL,
6814
103
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6815
  /* Turn $foo |> $bar->baz(...) into $bar->baz($foo). */
6816
63.9k
  } else if (callable_ast->kind == ZEND_AST_METHOD_CALL
6817
1.21k
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6818
321
    fcall_ast = zend_ast_create(ZEND_AST_METHOD_CALL,
6819
321
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6820
  /* Turn $foo |> $expr into ($expr)($foo) */
6821
63.5k
  } else {
6822
63.5k
    zend_compile_expr(&callable_result, callable_ast);
6823
63.5k
    callable_ast = zend_ast_create_znode(&callable_result);
6824
63.5k
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6825
63.5k
      callable_ast, arg_list_ast);
6826
63.5k
  }
6827
6828
64.5k
  zend_do_extended_stmt(&operand_result);
6829
6830
64.5k
  zend_compile_var(result, fcall_ast, type, /* by_ref */ false);
6831
64.5k
}
6832
6833
static void zend_compile_match(znode *result, zend_ast *ast)
6834
2.53k
{
6835
2.53k
  zend_ast *expr_ast = ast->child[0];
6836
2.53k
  zend_ast_list *arms = zend_ast_get_list(ast->child[1]);
6837
2.53k
  bool has_default_arm = false;
6838
2.53k
  uint32_t opnum_match = (uint32_t)-1;
6839
6840
2.53k
  znode expr_node;
6841
2.53k
  zend_compile_expr(&expr_node, expr_ast);
6842
6843
2.53k
  znode case_node;
6844
2.53k
  case_node.op_type = IS_TMP_VAR;
6845
2.53k
  case_node.u.op.var = get_temporary_variable();
6846
6847
2.53k
  uint32_t num_conds = count_match_conds(arms);
6848
2.53k
  uint8_t can_use_jumptable = can_match_use_jumptable(arms);
6849
2.53k
  bool uses_jumptable = can_use_jumptable && num_conds >= 2;
6850
2.53k
  HashTable *jumptable = NULL;
6851
2.53k
  uint32_t *jmpnz_opnums = NULL;
6852
6853
6.29k
  for (uint32_t i = 0; i < arms->children; ++i) {
6854
3.77k
    zend_ast *arm_ast = arms->child[i];
6855
6856
3.77k
    if (!arm_ast->child[0]) {
6857
651
      if (has_default_arm) {
6858
8
        CG(zend_lineno) = arm_ast->lineno;
6859
8
        zend_error_noreturn(E_COMPILE_ERROR,
6860
8
          "Match expressions may only contain one default arm");
6861
8
      }
6862
643
      has_default_arm = true;
6863
643
    }
6864
3.77k
  }
6865
6866
2.52k
  if (uses_jumptable) {
6867
812
    znode jumptable_op;
6868
6869
812
    ALLOC_HASHTABLE(jumptable);
6870
812
    zend_hash_init(jumptable, num_conds, NULL, NULL, 0);
6871
812
    jumptable_op.op_type = IS_CONST;
6872
812
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6873
6874
812
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH, &expr_node, &jumptable_op);
6875
812
    if (opline->op1_type == IS_CONST) {
6876
456
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6877
456
    }
6878
812
    opnum_match = opline - CG(active_op_array)->opcodes;
6879
1.71k
  } else {
6880
1.71k
    jmpnz_opnums = safe_emalloc(sizeof(uint32_t), num_conds, 0);
6881
1.71k
    uint32_t cond_count = 0;
6882
3.93k
    for (uint32_t i = 0; i < arms->children; ++i) {
6883
2.22k
      zend_ast *arm_ast = arms->child[i];
6884
6885
2.22k
      if (!arm_ast->child[0]) {
6886
431
        continue;
6887
431
      }
6888
6889
1.79k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6890
4.96k
      for (uint32_t j = 0; j < conds->children; j++) {
6891
3.17k
        zend_ast *cond_ast = conds->child[j];
6892
6893
3.17k
        znode cond_node;
6894
3.17k
        zend_compile_expr(&cond_node, cond_ast);
6895
6896
3.17k
        uint32_t opcode = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE_STRICT : ZEND_IS_IDENTICAL;
6897
3.17k
        zend_op *opline = zend_emit_op(NULL, opcode, &expr_node, &cond_node);
6898
3.17k
        SET_NODE(opline->result, &case_node);
6899
3.17k
        if (opline->op1_type == IS_CONST) {
6900
1.47k
          Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6901
1.47k
        }
6902
6903
3.17k
        jmpnz_opnums[cond_count] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6904
6905
3.17k
        cond_count++;
6906
3.17k
      }
6907
1.79k
    }
6908
1.71k
  }
6909
6910
2.52k
  uint32_t opnum_default_jmp = 0;
6911
2.52k
  if (!uses_jumptable) {
6912
1.71k
    opnum_default_jmp = zend_emit_jump(0);
6913
1.71k
  }
6914
6915
2.52k
  bool is_first_case = true;
6916
2.52k
  uint32_t cond_count = 0;
6917
2.52k
  uint32_t *jmp_end_opnums = safe_emalloc(sizeof(uint32_t), arms->children, 0);
6918
6919
  // The generated default arm is emitted first to avoid live range issues where the tmpvar
6920
  // for the arm result is freed even though it has not been initialized yet.
6921
2.52k
  if (!has_default_arm) {
6922
1.89k
    if (!uses_jumptable) {
6923
1.28k
      zend_update_jump_target_to_next(opnum_default_jmp);
6924
1.28k
    }
6925
6926
1.89k
    if (jumptable) {
6927
608
      zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6928
608
      opline->extended_value = get_next_op_number();
6929
608
    }
6930
6931
1.89k
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH_ERROR, &expr_node, NULL);
6932
1.89k
    if (opline->op1_type == IS_CONST) {
6933
758
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6934
758
    }
6935
1.89k
    if (arms->children == 0) {
6936
      /* Mark this as an "expression throw" for opcache. */
6937
406
      opline->extended_value = ZEND_THROW_IS_EXPR;
6938
406
    }
6939
1.89k
  }
6940
6941
6.26k
  for (uint32_t i = 0; i < arms->children; ++i) {
6942
3.74k
    zend_ast *arm_ast = arms->child[i];
6943
3.74k
    zend_ast *body_ast = arm_ast->child[1];
6944
6945
3.74k
    if (arm_ast->child[0] != NULL) {
6946
3.10k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6947
6948
9.80k
      for (uint32_t j = 0; j < conds->children; j++) {
6949
6.69k
        zend_ast *cond_ast = conds->child[j];
6950
6951
6.69k
        if (jmpnz_opnums != NULL) {
6952
3.17k
          zend_update_jump_target_to_next(jmpnz_opnums[cond_count]);
6953
3.17k
        }
6954
6955
6.69k
        if (jumptable) {
6956
3.52k
          zval *cond_zv = zend_ast_get_zval(cond_ast);
6957
3.52k
          zval jmp_target;
6958
3.52k
          ZVAL_LONG(&jmp_target, get_next_op_number());
6959
6960
3.52k
          if (Z_TYPE_P(cond_zv) == IS_LONG) {
6961
3.16k
            zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6962
3.16k
          } else {
6963
359
            ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6964
359
            zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6965
359
          }
6966
3.52k
        }
6967
6968
6.69k
        cond_count++;
6969
6.69k
      }
6970
3.10k
    } else {
6971
635
      if (!uses_jumptable) {
6972
431
        zend_update_jump_target_to_next(opnum_default_jmp);
6973
431
      }
6974
6975
635
      if (jumptable) {
6976
204
        ZEND_ASSERT(opnum_match != (uint32_t)-1);
6977
204
        zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6978
204
        opline->extended_value = get_next_op_number();
6979
204
      }
6980
635
    }
6981
6982
3.74k
    znode body_node;
6983
3.74k
    zend_compile_expr(&body_node, body_ast);
6984
6985
3.74k
    if (is_first_case) {
6986
2.12k
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL);
6987
2.12k
      is_first_case = false;
6988
2.12k
    } else {
6989
1.62k
      zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL);
6990
1.62k
      SET_NODE(opline_qm_assign->result, result);
6991
1.62k
    }
6992
6993
3.74k
    jmp_end_opnums[i] = zend_emit_jump(0);
6994
3.74k
  }
6995
6996
  // Initialize result in case there is no arm
6997
2.52k
  if (arms->children == 0) {
6998
406
    result->op_type = IS_CONST;
6999
406
    ZVAL_NULL(&result->u.constant);
7000
406
  }
7001
7002
6.26k
  for (uint32_t i = 0; i < arms->children; ++i) {
7003
3.74k
    zend_update_jump_target_to_next(jmp_end_opnums[i]);
7004
3.74k
  }
7005
7006
2.52k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
7007
1.02k
    zend_op *opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
7008
1.02k
    opline->extended_value = ZEND_FREE_SWITCH;
7009
1.50k
  } else if (expr_node.op_type == IS_CONST) {
7010
1.11k
    zval_ptr_dtor_nogc(&expr_node.u.constant);
7011
1.11k
  }
7012
7013
2.52k
  if (jmpnz_opnums != NULL) {
7014
1.71k
    efree(jmpnz_opnums);
7015
1.71k
  }
7016
2.52k
  efree(jmp_end_opnums);
7017
2.52k
}
7018
7019
static void zend_compile_try(const zend_ast *ast) /* {{{ */
7020
30.1k
{
7021
30.1k
  zend_ast *try_ast = ast->child[0];
7022
30.1k
  const zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
7023
30.1k
  zend_ast *finally_ast = ast->child[2];
7024
7025
30.1k
  uint32_t i, j;
7026
30.1k
  zend_op *opline;
7027
30.1k
  uint32_t try_catch_offset;
7028
30.1k
  uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
7029
30.1k
  uint32_t orig_fast_call_var = CG(context).fast_call_var;
7030
30.1k
  uint32_t orig_try_catch_offset = CG(context).try_catch_offset;
7031
7032
30.1k
  if (catches->children == 0 && !finally_ast) {
7033
67
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
7034
67
  }
7035
7036
  /* label: try { } must not be equal to try { label: } */
7037
30.1k
  if (CG(context).labels) {
7038
447
    zend_label *label;
7039
447
    ZEND_HASH_MAP_REVERSE_FOREACH_PTR(CG(context).labels, label) {
7040
447
      if (label->opline_num == get_next_op_number()) {
7041
165
        zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
7042
165
      }
7043
447
      break;
7044
1.34k
    } ZEND_HASH_FOREACH_END();
7045
447
  }
7046
7047
30.1k
  try_catch_offset = zend_add_try_element(get_next_op_number());
7048
7049
30.1k
  if (finally_ast) {
7050
2.77k
    zend_loop_var fast_call;
7051
2.77k
    if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
7052
1.07k
      CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
7053
1.07k
    }
7054
2.77k
    CG(context).fast_call_var = get_temporary_variable();
7055
7056
    /* Push FAST_CALL on unwind stack */
7057
2.77k
    fast_call.opcode = ZEND_FAST_CALL;
7058
2.77k
    fast_call.var_type = IS_TMP_VAR;
7059
2.77k
    fast_call.var_num = CG(context).fast_call_var;
7060
2.77k
    fast_call.try_catch_offset = try_catch_offset;
7061
2.77k
    zend_stack_push(&CG(loop_var_stack), &fast_call);
7062
2.77k
  }
7063
7064
30.1k
  CG(context).try_catch_offset = try_catch_offset;
7065
7066
30.1k
  zend_compile_stmt(try_ast);
7067
7068
30.1k
  if (catches->children != 0) {
7069
27.4k
    jmp_opnums[0] = zend_emit_jump(0);
7070
27.4k
  }
7071
7072
62.3k
  for (i = 0; i < catches->children; ++i) {
7073
32.2k
    const zend_ast *catch_ast = catches->child[i];
7074
32.2k
    const zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]);
7075
32.2k
    zend_ast *var_ast = catch_ast->child[1];
7076
32.2k
    zend_ast *stmt_ast = catch_ast->child[2];
7077
32.2k
    zend_string *var_name = var_ast ? zval_make_interned_string(zend_ast_get_zval(var_ast)) : NULL;
7078
32.2k
    bool is_last_catch = (i + 1 == catches->children);
7079
7080
32.2k
    uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0);
7081
32.2k
    uint32_t opnum_catch = (uint32_t)-1;
7082
7083
32.2k
    CG(zend_lineno) = catch_ast->lineno;
7084
7085
67.1k
    for (j = 0; j < classes->children; j++) {
7086
34.9k
      zend_ast *class_ast = classes->child[j];
7087
34.9k
      bool is_last_class = (j + 1 == classes->children);
7088
7089
34.9k
      if (!zend_is_const_default_class_ref(class_ast)) {
7090
7
        zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
7091
7
      }
7092
7093
34.8k
      opnum_catch = get_next_op_number();
7094
34.8k
      if (i == 0 && j == 0) {
7095
27.4k
        CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
7096
27.4k
      }
7097
7098
34.8k
      opline = get_next_op();
7099
34.8k
      opline->opcode = ZEND_CATCH;
7100
34.8k
      opline->op1_type = IS_CONST;
7101
34.8k
      opline->op1.constant = zend_add_class_name_literal(
7102
34.8k
          zend_resolve_class_name_ast(class_ast));
7103
34.8k
      opline->extended_value = zend_alloc_cache_slot();
7104
7105
34.8k
      if (var_name && zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
7106
7
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
7107
7
      }
7108
7109
34.8k
      opline->result_type = var_name ? IS_CV : IS_UNUSED;
7110
34.8k
      opline->result.var = var_name ? lookup_cv(var_name) : -1;
7111
7112
34.8k
      if (is_last_catch && is_last_class) {
7113
27.4k
        opline->extended_value |= ZEND_LAST_CATCH;
7114
27.4k
      }
7115
7116
34.8k
      if (!is_last_class) {
7117
2.62k
        jmp_multicatch[j] = zend_emit_jump(0);
7118
2.62k
        opline = &CG(active_op_array)->opcodes[opnum_catch];
7119
2.62k
        opline->op2.opline_num = get_next_op_number();
7120
2.62k
      }
7121
34.8k
    }
7122
7123
34.8k
    for (j = 0; j < classes->children - 1; j++) {
7124
2.62k
      zend_update_jump_target_to_next(jmp_multicatch[j]);
7125
2.62k
    }
7126
7127
32.2k
    efree(jmp_multicatch);
7128
7129
32.2k
    zend_compile_stmt(stmt_ast);
7130
7131
32.2k
    if (!is_last_catch) {
7132
4.77k
      jmp_opnums[i + 1] = zend_emit_jump(0);
7133
4.77k
    }
7134
7135
32.2k
    ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class");
7136
32.2k
    opline = &CG(active_op_array)->opcodes[opnum_catch];
7137
32.2k
    if (!is_last_catch) {
7138
4.77k
      opline->op2.opline_num = get_next_op_number();
7139
4.77k
    }
7140
32.2k
  }
7141
7142
62.3k
  for (i = 0; i < catches->children; ++i) {
7143
32.2k
    zend_update_jump_target_to_next(jmp_opnums[i]);
7144
32.2k
  }
7145
7146
30.0k
  if (finally_ast) {
7147
2.76k
    zend_loop_var discard_exception;
7148
2.76k
    uint32_t opnum_jmp = get_next_op_number() + 1;
7149
7150
    /* Pop FAST_CALL from unwind stack */
7151
2.76k
    zend_stack_del_top(&CG(loop_var_stack));
7152
7153
    /* Push DISCARD_EXCEPTION on unwind stack */
7154
2.76k
    discard_exception.opcode = ZEND_DISCARD_EXCEPTION;
7155
2.76k
    discard_exception.var_type = IS_TMP_VAR;
7156
2.76k
    discard_exception.var_num = CG(context).fast_call_var;
7157
2.76k
    zend_stack_push(&CG(loop_var_stack), &discard_exception);
7158
7159
2.76k
    CG(zend_lineno) = finally_ast->lineno;
7160
7161
2.76k
    opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
7162
2.76k
    opline->op1.num = try_catch_offset;
7163
2.76k
    opline->result_type = IS_TMP_VAR;
7164
2.76k
    opline->result.var = CG(context).fast_call_var;
7165
7166
2.76k
    zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
7167
7168
2.76k
    zend_compile_stmt(finally_ast);
7169
7170
2.76k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
7171
2.76k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
7172
2.76k
      = get_next_op_number();
7173
7174
2.76k
    opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
7175
2.76k
    opline->op1_type = IS_TMP_VAR;
7176
2.76k
    opline->op1.var = CG(context).fast_call_var;
7177
2.76k
    opline->op2.num = orig_try_catch_offset;
7178
7179
2.76k
    zend_update_jump_target_to_next(opnum_jmp);
7180
7181
2.76k
    CG(context).fast_call_var = orig_fast_call_var;
7182
7183
    /* Pop DISCARD_EXCEPTION from unwind stack */
7184
2.76k
    zend_stack_del_top(&CG(loop_var_stack));
7185
2.76k
  }
7186
7187
30.0k
  CG(context).try_catch_offset = orig_try_catch_offset;
7188
7189
30.0k
  efree(jmp_opnums);
7190
30.0k
}
7191
/* }}} */
7192
7193
/* Encoding declarations must already be handled during parsing */
7194
bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
7195
2.60k
{
7196
2.60k
  const zend_ast_list *declares = zend_ast_get_list(ast);
7197
2.60k
  uint32_t i;
7198
6.53k
  for (i = 0; i < declares->children; ++i) {
7199
3.94k
    const zend_ast *declare_ast = declares->child[i];
7200
3.94k
    zend_ast *name_ast = declare_ast->child[0];
7201
3.94k
    zend_ast *value_ast = declare_ast->child[1];
7202
3.94k
    const zend_string *name = zend_ast_get_str(name_ast);
7203
7204
3.94k
    if (zend_string_equals_literal_ci(name, "encoding")) {
7205
193
      if (value_ast->kind != ZEND_AST_ZVAL) {
7206
25
        zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0);
7207
25
        return 0;
7208
25
      }
7209
7210
168
      if (CG(multibyte)) {
7211
0
        zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
7212
7213
0
        const zend_encoding *new_encoding, *old_encoding;
7214
0
        zend_encoding_filter old_input_filter;
7215
7216
0
        CG(encoding_declared) = 1;
7217
7218
0
        new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
7219
0
        if (!new_encoding) {
7220
0
          zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
7221
0
        } else {
7222
0
          old_input_filter = LANG_SCNG(input_filter);
7223
0
          old_encoding = LANG_SCNG(script_encoding);
7224
0
          zend_multibyte_set_filter(new_encoding);
7225
7226
          /* need to re-scan if input filter changed */
7227
0
          if (old_input_filter != LANG_SCNG(input_filter) ||
7228
0
             (old_input_filter && new_encoding != old_encoding)) {
7229
0
            zend_multibyte_yyinput_again(old_input_filter, old_encoding);
7230
0
          }
7231
0
        }
7232
7233
0
        zend_string_release_ex(encoding_name, 0);
7234
168
      } else {
7235
168
        zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
7236
168
          "Zend multibyte feature is turned off by settings");
7237
168
      }
7238
168
    }
7239
3.94k
  }
7240
7241
2.58k
  return 1;
7242
2.60k
}
7243
/* }}} */
7244
7245
/* Check whether this is the first statement, not counting declares. */
7246
static zend_result zend_is_first_statement(const zend_ast *ast, bool allow_nop) /* {{{ */
7247
3.24k
{
7248
3.24k
  uint32_t i = 0;
7249
3.24k
  const zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
7250
7251
14.4k
  while (i < file_ast->children) {
7252
14.4k
    if (file_ast->child[i] == ast) {
7253
3.18k
      return SUCCESS;
7254
11.2k
    } else if (file_ast->child[i] == NULL) {
7255
268
      if (!allow_nop) {
7256
0
        return FAILURE;
7257
0
      }
7258
10.9k
    } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
7259
60
      return FAILURE;
7260
60
    }
7261
11.1k
    i++;
7262
11.1k
  }
7263
1
  return FAILURE;
7264
3.24k
}
7265
/* }}} */
7266
7267
static void zend_compile_declare(const zend_ast *ast) /* {{{ */
7268
4.40k
{
7269
4.40k
  const zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
7270
4.40k
  zend_ast *stmt_ast = ast->child[1];
7271
4.40k
  zend_declarables orig_declarables = FC(declarables);
7272
4.40k
  uint32_t i;
7273
7274
11.3k
  for (i = 0; i < declares->children; ++i) {
7275
7.02k
    zend_ast *declare_ast = declares->child[i];
7276
7.02k
    zend_ast *name_ast = declare_ast->child[0];
7277
7.02k
    zend_ast **value_ast_ptr = &declare_ast->child[1];
7278
7.02k
    zend_string *name = zend_ast_get_str(name_ast);
7279
7280
7.02k
    if ((*value_ast_ptr)->kind != ZEND_AST_ZVAL) {
7281
30
      zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
7282
30
    }
7283
7284
6.99k
    if (zend_string_equals_literal_ci(name, "ticks")) {
7285
4.65k
      zval value_zv;
7286
4.65k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7287
4.65k
      FC(declarables).ticks = zval_get_long(&value_zv);
7288
4.65k
      zval_ptr_dtor_nogc(&value_zv);
7289
4.65k
    } else if (zend_string_equals_literal_ci(name, "encoding")) {
7290
7291
95
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ false)) {
7292
7
        zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
7293
7
          "the very first statement in the script");
7294
7
      }
7295
2.23k
    } else if (zend_string_equals_literal_ci(name, "strict_types")) {
7296
440
      zval value_zv;
7297
7298
440
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
7299
20
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
7300
20
          "the very first statement in the script");
7301
20
      }
7302
7303
420
      if (ast->child[1] != NULL) {
7304
9
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
7305
9
          "use block mode");
7306
9
      }
7307
7308
411
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7309
7310
411
      if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
7311
53
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
7312
53
      }
7313
7314
358
      if (Z_LVAL(value_zv) == 1) {
7315
273
        CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
7316
273
      }
7317
7318
1.79k
    } else {
7319
1.79k
      zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
7320
1.79k
    }
7321
6.99k
  }
7322
7323
4.28k
  if (stmt_ast) {
7324
661
    zend_compile_stmt(stmt_ast);
7325
7326
661
    FC(declarables) = orig_declarables;
7327
661
  }
7328
4.28k
}
7329
/* }}} */
7330
7331
static void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
7332
1.35M
{
7333
1.35M
  const zend_ast_list *list = zend_ast_get_list(ast);
7334
1.35M
  uint32_t i;
7335
5.68M
  for (i = 0; i < list->children; ++i) {
7336
4.32M
    zend_compile_stmt(list->child[i]);
7337
4.32M
  }
7338
1.35M
}
7339
/* }}} */
7340
7341
ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
7342
941k
{
7343
941k
  uint32_t i, n;
7344
7345
941k
  func->common.arg_flags[0] = 0;
7346
941k
  func->common.arg_flags[1] = 0;
7347
941k
  func->common.arg_flags[2] = 0;
7348
941k
  if (func->common.arg_info) {
7349
941k
    n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
7350
941k
    i = 0;
7351
1.90M
    while (i < n) {
7352
962k
      ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i]));
7353
962k
      i++;
7354
962k
    }
7355
941k
    if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_VARIADIC) && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) {
7356
484
      uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]);
7357
6.18k
      while (i < MAX_ARG_FLAG_NUM) {
7358
5.70k
        ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
7359
5.70k
        i++;
7360
5.70k
      }
7361
484
    }
7362
941k
  }
7363
941k
}
7364
/* }}} */
7365
7366
static zend_type zend_compile_single_typename(zend_ast *ast)
7367
311k
{
7368
311k
  ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE));
7369
311k
  if (ast->kind == ZEND_AST_TYPE) {
7370
5.52k
    if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) {
7371
5
      zend_error_noreturn(E_COMPILE_ERROR,
7372
5
        "Cannot use \"static\" when no class scope is active");
7373
5
    }
7374
7375
5.51k
    return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0);
7376
306k
  } else {
7377
306k
    zend_string *type_name = zend_ast_get_str(ast);
7378
306k
    uint8_t type_code = zend_lookup_builtin_type_by_name(type_name);
7379
7380
306k
    if (type_code != 0) {
7381
111k
      if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) {
7382
5
        zend_error_noreturn(E_COMPILE_ERROR,
7383
5
          "Type declaration '%s' must be unqualified",
7384
5
          ZSTR_VAL(zend_string_tolower(type_name)));
7385
5
      }
7386
7387
      /* Transform iterable into a type union alias */
7388
111k
      if (type_code == IS_ITERABLE) {
7389
        /* Set iterable bit for BC compat during Reflection and string representation of type */
7390
86.8k
        zend_type iterable = (zend_type) ZEND_TYPE_INIT_CLASS_MASK(ZSTR_KNOWN(ZEND_STR_TRAVERSABLE),
7391
86.8k
                  (MAY_BE_ARRAY|_ZEND_TYPE_ITERABLE_BIT));
7392
86.8k
        return iterable;
7393
86.8k
      }
7394
7395
24.3k
      return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0);
7396
194k
    } else {
7397
194k
      const char *correct_name;
7398
194k
      uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
7399
194k
      zend_string *class_name = type_name;
7400
7401
194k
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
7402
194k
        class_name = zend_resolve_class_name_ast(ast);
7403
194k
        zend_assert_valid_class_name(class_name, "a type name");
7404
194k
      } else {
7405
747
        ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT);
7406
7407
747
        zend_ensure_valid_class_fetch_type(fetch_type);
7408
7409
747
        bool substitute_self_parent = zend_is_scope_known()
7410
514
          && !(CG(active_class_entry)->ce_flags & ZEND_ACC_ANON_CLASS);
7411
7412
747
        if (fetch_type == ZEND_FETCH_CLASS_SELF) {
7413
          /* Scope might be unknown for unbound closures and traits */
7414
564
          if (substitute_self_parent) {
7415
381
            class_name = CG(active_class_entry)->name;
7416
381
            ZEND_ASSERT(class_name && "must know class name when resolving self type at compile time");
7417
381
          }
7418
564
        } else {
7419
183
          ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_PARENT);
7420
          /* Scope might be unknown for unbound closures and traits */
7421
183
          if (substitute_self_parent) {
7422
120
            class_name = CG(active_class_entry)->parent_name;
7423
120
            ZEND_ASSERT(class_name && "must know class name when resolving parent type at compile time");
7424
120
          }
7425
165
        }
7426
747
        zend_string_addref(class_name);
7427
729
      }
7428
7429
194k
      if (ast->attr == ZEND_NAME_NOT_FQ
7430
194k
          && zend_is_confusable_type(type_name, &correct_name)
7431
16.3k
          && zend_is_not_imported(type_name)) {
7432
16.3k
        const char *extra =
7433
16.3k
          FC(current_namespace) ? " or import the class with \"use\"" : "";
7434
16.3k
        if (correct_name) {
7435
16.0k
          zend_error(E_COMPILE_WARNING,
7436
16.0k
            "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? "
7437
16.0k
            "Write \"\\%s\"%s to suppress this warning",
7438
16.0k
            ZSTR_VAL(type_name), correct_name, ZSTR_VAL(class_name), extra);
7439
16.0k
        } else {
7440
263
          zend_error(E_COMPILE_WARNING,
7441
263
            "\"%s\" is not a supported builtin type "
7442
263
            "and will be interpreted as a class name. "
7443
263
            "Write \"\\%s\"%s to suppress this warning",
7444
263
            ZSTR_VAL(type_name), ZSTR_VAL(class_name), extra);
7445
263
        }
7446
16.3k
      }
7447
7448
194k
      class_name = zend_new_interned_string(class_name);
7449
194k
      zend_alloc_ce_cache(class_name);
7450
194k
      return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, /* allow null */ false, 0);
7451
194k
    }
7452
306k
  }
7453
311k
}
7454
7455
static void zend_are_intersection_types_redundant(const zend_type left_type, const zend_type right_type)
7456
5.81k
{
7457
5.81k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(left_type));
7458
5.81k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(right_type));
7459
5.81k
  const zend_type_list *l_type_list = ZEND_TYPE_LIST(left_type);
7460
5.81k
  const zend_type_list *r_type_list = ZEND_TYPE_LIST(right_type);
7461
5.81k
  const zend_type_list *smaller_type_list, *larger_type_list;
7462
5.81k
  bool flipped = false;
7463
7464
5.81k
  if (r_type_list->num_types < l_type_list->num_types) {
7465
1.98k
    smaller_type_list = r_type_list;
7466
1.98k
    larger_type_list = l_type_list;
7467
1.98k
    flipped = true;
7468
3.82k
  } else {
7469
3.82k
    smaller_type_list = l_type_list;
7470
3.82k
    larger_type_list = r_type_list;
7471
3.82k
  }
7472
7473
5.81k
  unsigned int sum = 0;
7474
5.81k
  const zend_type *outer_type;
7475
22.9k
  ZEND_TYPE_LIST_FOREACH(smaller_type_list, outer_type)
7476
22.9k
    const zend_type *inner_type;
7477
81.3k
    ZEND_TYPE_LIST_FOREACH(larger_type_list, inner_type)
7478
81.3k
      if (zend_string_equals_ci(ZEND_TYPE_NAME(*inner_type), ZEND_TYPE_NAME(*outer_type))) {
7479
6.01k
        sum++;
7480
6.01k
        break;
7481
6.01k
      }
7482
81.3k
    ZEND_TYPE_LIST_FOREACH_END();
7483
22.9k
  ZEND_TYPE_LIST_FOREACH_END();
7484
7485
5.81k
  if (sum == smaller_type_list->num_types) {
7486
26
    zend_string *smaller_type_str;
7487
26
    zend_string *larger_type_str;
7488
26
    if (flipped) {
7489
8
      smaller_type_str = zend_type_to_string(right_type);
7490
8
      larger_type_str = zend_type_to_string(left_type);
7491
18
    } else {
7492
18
      smaller_type_str = zend_type_to_string(left_type);
7493
18
      larger_type_str = zend_type_to_string(right_type);
7494
18
    }
7495
26
    if (smaller_type_list->num_types == larger_type_list->num_types) {
7496
10
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant with type %s",
7497
10
        ZSTR_VAL(smaller_type_str), ZSTR_VAL(larger_type_str));
7498
16
    } else {
7499
16
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7500
16
        ZSTR_VAL(larger_type_str), ZSTR_VAL(smaller_type_str));
7501
16
    }
7502
26
  }
7503
5.81k
}
7504
7505
static void zend_is_intersection_type_redundant_by_single_type(const zend_type intersection_type, const zend_type single_type)
7506
12.3k
{
7507
12.3k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(intersection_type));
7508
12.3k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(single_type));
7509
7510
12.3k
  const zend_type *single_intersection_type = NULL;
7511
42.0k
  ZEND_TYPE_FOREACH(intersection_type, single_intersection_type)
7512
42.0k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(*single_intersection_type), ZEND_TYPE_NAME(single_type))) {
7513
8
      zend_string *single_type_str = zend_type_to_string(single_type);
7514
8
      zend_string *complete_type = zend_type_to_string(intersection_type);
7515
8
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7516
8
          ZSTR_VAL(complete_type), ZSTR_VAL(single_type_str));
7517
8
    }
7518
42.0k
  ZEND_TYPE_FOREACH_END();
7519
12.3k
}
7520
7521
/* Used by both intersection and union types prior to transforming the type list to a full zend_type */
7522
static void zend_is_type_list_redundant_by_single_type(const zend_type_list *type_list, const zend_type type)
7523
36.5k
{
7524
36.5k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(type));
7525
70.5k
  for (size_t i = 0; i < type_list->num_types - 1; i++) {
7526
34.1k
    if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7527
7.12k
      zend_is_intersection_type_redundant_by_single_type(type_list->types[i], type);
7528
7.12k
      continue;
7529
7.12k
    }
7530
27.0k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(type_list->types[i]), ZEND_TYPE_NAME(type))) {
7531
50
      zend_string *single_type_str = zend_type_to_string(type);
7532
50
      zend_error_noreturn(E_COMPILE_ERROR, "Duplicate type %s is redundant", ZSTR_VAL(single_type_str));
7533
50
    }
7534
27.0k
  }
7535
36.5k
}
7536
7537
static zend_type zend_compile_typename(zend_ast *ast);
7538
7539
static zend_type zend_compile_typename_ex(
7540
    zend_ast *ast, bool force_allow_null, bool *forced_allow_null) /* {{{ */
7541
289k
{
7542
289k
  bool is_marked_nullable = ast->attr & ZEND_TYPE_NULLABLE;
7543
289k
  zend_ast_attr orig_ast_attr = ast->attr;
7544
289k
  zend_type type = ZEND_TYPE_INIT_NONE(0);
7545
7546
289k
  if (is_marked_nullable) {
7547
2.84k
    ast->attr &= ~ZEND_TYPE_NULLABLE;
7548
2.84k
  }
7549
7550
289k
  if (ast->kind == ZEND_AST_TYPE_UNION) {
7551
15.3k
    const zend_ast_list *list = zend_ast_get_list(ast);
7552
15.3k
    zend_type_list *type_list;
7553
15.3k
    bool is_composite = false;
7554
15.3k
    bool has_only_iterable_class = true;
7555
15.3k
    ALLOCA_FLAG(use_heap)
7556
7557
15.3k
    type_list = do_alloca(ZEND_TYPE_LIST_SIZE(list->children), use_heap);
7558
15.3k
    type_list->num_types = 0;
7559
7560
49.1k
    for (uint32_t i = 0; i < list->children; i++) {
7561
33.8k
      zend_ast *type_ast = list->child[i];
7562
33.8k
      zend_type single_type;
7563
33.8k
      uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7564
7565
33.8k
      if (type_ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7566
10.4k
        has_only_iterable_class = false;
7567
10.4k
        is_composite = true;
7568
        /* The first class type can be stored directly as the type ptr payload. */
7569
10.4k
        if (ZEND_TYPE_IS_COMPLEX(type) && !ZEND_TYPE_HAS_LIST(type)) {
7570
          /* Switch from single name to name list. */
7571
3.32k
          type_list->num_types = 1;
7572
3.32k
          type_list->types[0] = type;
7573
          /* Clear MAY_BE_* type flags */
7574
3.32k
          ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7575
3.32k
        }
7576
        /* Mark type as list type */
7577
10.4k
        ZEND_TYPE_SET_LIST(type, type_list);
7578
7579
10.4k
        single_type = zend_compile_typename(type_ast);
7580
10.4k
        ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(single_type));
7581
7582
10.4k
        type_list->types[type_list->num_types++] = single_type;
7583
7584
        /* Check for trivially redundant class types */
7585
21.4k
        for (size_t i = 0; i < type_list->num_types - 1; i++) {
7586
11.0k
          if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7587
5.81k
            zend_are_intersection_types_redundant(single_type, type_list->types[i]);
7588
5.81k
            continue;
7589
5.81k
          }
7590
          /* Type from type list is a simple type */
7591
5.18k
          zend_is_intersection_type_redundant_by_single_type(single_type, type_list->types[i]);
7592
5.18k
        }
7593
10.4k
        continue;
7594
10.4k
      }
7595
7596
23.3k
      single_type = zend_compile_single_typename(type_ast);
7597
23.3k
      uint32_t single_type_mask = ZEND_TYPE_PURE_MASK(single_type);
7598
7599
23.3k
      if (single_type_mask == MAY_BE_ANY) {
7600
5
        zend_error_noreturn(E_COMPILE_ERROR, "Type mixed can only be used as a standalone type");
7601
5
      }
7602
23.3k
      if (ZEND_TYPE_IS_COMPLEX(single_type) && !ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7603
18.3k
        has_only_iterable_class = false;
7604
18.3k
      }
7605
7606
23.3k
      uint32_t type_mask_overlap = ZEND_TYPE_PURE_MASK(type) & single_type_mask;
7607
23.3k
      if (type_mask_overlap) {
7608
29
        zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap);
7609
29
        zend_string *overlap_type_str = zend_type_to_string(overlap_type);
7610
29
        zend_error_noreturn(E_COMPILE_ERROR,
7611
29
          "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str));
7612
29
      }
7613
7614
23.3k
      if ( ((type_mask & MAY_BE_TRUE) && (single_type_mask == MAY_BE_FALSE))
7615
23.3k
          || ((type_mask & MAY_BE_FALSE) && (single_type_mask == MAY_BE_TRUE)) ) {
7616
10
        zend_error_noreturn(E_COMPILE_ERROR,
7617
10
          "Type contains both true and false, bool must be used instead");
7618
10
      }
7619
23.3k
      ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type);
7620
      /* Clear MAY_BE_* type flags */
7621
23.3k
      ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK;
7622
7623
23.3k
      if (ZEND_TYPE_IS_COMPLEX(single_type)) {
7624
18.7k
        if (!ZEND_TYPE_IS_COMPLEX(type) && !is_composite) {
7625
          /* The first class type can be stored directly as the type ptr payload. */
7626
8.15k
          ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type));
7627
8.15k
          ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT;
7628
10.5k
        } else {
7629
10.5k
          if (type_list->num_types == 0) {
7630
            /* Switch from single name to name list. */
7631
3.68k
            type_list->num_types = 1;
7632
3.68k
            type_list->types[0] = type;
7633
            /* Clear MAY_BE_* type flags */
7634
3.68k
            ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7635
3.68k
            ZEND_TYPE_SET_LIST(type, type_list);
7636
3.68k
          }
7637
7638
10.5k
          type_list->types[type_list->num_types++] = single_type;
7639
7640
          /* Check for trivially redundant class types */
7641
10.5k
          zend_is_type_list_redundant_by_single_type(type_list, single_type);
7642
10.5k
        }
7643
18.7k
      }
7644
23.3k
    }
7645
7646
15.3k
    if (type_list->num_types) {
7647
12.8k
      zend_type_list *list = zend_arena_alloc(
7648
12.8k
        &CG(arena), ZEND_TYPE_LIST_SIZE(type_list->num_types));
7649
12.8k
      memcpy(list, type_list, ZEND_TYPE_LIST_SIZE(type_list->num_types));
7650
12.8k
      ZEND_TYPE_SET_LIST(type, list);
7651
12.8k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7652
      /* Inform that the type list is a union type */
7653
12.8k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7654
12.8k
    }
7655
7656
15.3k
    free_alloca(type_list, use_heap);
7657
7658
15.3k
    uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7659
15.3k
    if ((type_mask & MAY_BE_OBJECT) &&
7660
467
        ((!has_only_iterable_class && ZEND_TYPE_IS_COMPLEX(type)) || (type_mask & MAY_BE_STATIC))) {
7661
27
      zend_string *type_str = zend_type_to_string(type);
7662
27
      zend_error_noreturn(E_COMPILE_ERROR,
7663
27
        "Type %s contains both object and a class type, which is redundant",
7664
27
        ZSTR_VAL(type_str));
7665
27
    }
7666
274k
  } else if (ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7667
11.9k
    const zend_ast_list *list = zend_ast_get_list(ast);
7668
11.9k
    zend_type_list *type_list;
7669
7670
    /* Allocate the type list directly on the arena as it must be a type
7671
     * list of the same number of elements as the AST list has children */
7672
11.9k
    type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children));
7673
11.9k
    type_list->num_types = 0;
7674
7675
11.9k
    ZEND_ASSERT(list->children > 1);
7676
7677
37.8k
    for (uint32_t i = 0; i < list->children; i++) {
7678
25.9k
      zend_ast *type_ast = list->child[i];
7679
25.9k
      zend_type single_type = zend_compile_single_typename(type_ast);
7680
7681
      /* An intersection of union types cannot exist so invalidate it
7682
       * Currently only can happen with iterable getting canonicalized to Traversable|array */
7683
25.9k
      if (ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7684
7
        zend_string *standard_type_str = zend_type_to_string(single_type);
7685
7
        zend_error_noreturn(E_COMPILE_ERROR,
7686
7
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7687
7
      }
7688
      /* An intersection of standard types cannot exist so invalidate it */
7689
25.9k
      if (ZEND_TYPE_IS_ONLY_MASK(single_type)) {
7690
62
        zend_string *standard_type_str = zend_type_to_string(single_type);
7691
62
        zend_error_noreturn(E_COMPILE_ERROR,
7692
62
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7693
62
      }
7694
      /* Check for "self" and "parent" too */
7695
25.9k
      if (
7696
25.9k
        zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_SELF))
7697
25.9k
        || zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_PARENT))
7698
25.9k
      ) {
7699
10
        zend_error_noreturn(E_COMPILE_ERROR,
7700
10
          "Type %s cannot be part of an intersection type", ZSTR_VAL(ZEND_TYPE_NAME(single_type)));
7701
10
      }
7702
7703
      /* Add type to the type list */
7704
25.9k
      type_list->types[type_list->num_types++] = single_type;
7705
7706
      /* Check for trivially redundant class types */
7707
25.9k
      zend_is_type_list_redundant_by_single_type(type_list, single_type);
7708
25.9k
    }
7709
7710
11.8k
    ZEND_ASSERT(list->children == type_list->num_types);
7711
7712
    /* An implicitly nullable intersection type needs to be converted to a DNF type */
7713
11.8k
    if (force_allow_null) {
7714
218
      zend_type intersection_type = ZEND_TYPE_INIT_NONE(0);
7715
218
      ZEND_TYPE_SET_LIST(intersection_type, type_list);
7716
218
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_INTERSECTION_BIT;
7717
218
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_ARENA_BIT;
7718
7719
218
      zend_type_list *dnf_type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(1));
7720
218
      dnf_type_list->num_types = 1;
7721
218
      dnf_type_list->types[0] = intersection_type;
7722
218
      ZEND_TYPE_SET_LIST(type, dnf_type_list);
7723
      /* Inform that the type list is a DNF type */
7724
218
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7725
218
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7726
11.6k
    } else {
7727
11.6k
      ZEND_TYPE_SET_LIST(type, type_list);
7728
      /* Inform that the type list is an intersection type */
7729
11.6k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_INTERSECTION_BIT;
7730
11.6k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7731
11.6k
    }
7732
262k
  } else {
7733
262k
    type = zend_compile_single_typename(ast);
7734
262k
  }
7735
7736
289k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
7737
7738
289k
  if (type_mask == MAY_BE_ANY && is_marked_nullable) {
7739
13
    zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be marked as nullable since mixed already includes null");
7740
13
  }
7741
7742
289k
  if ((type_mask & MAY_BE_NULL) && is_marked_nullable) {
7743
5
    zend_error_noreturn(E_COMPILE_ERROR, "null cannot be marked as nullable");
7744
5
  }
7745
7746
289k
  if (force_allow_null && !is_marked_nullable && !(type_mask & MAY_BE_NULL)) {
7747
369
    *forced_allow_null = true;
7748
369
  }
7749
7750
289k
  if (is_marked_nullable || force_allow_null) {
7751
3.30k
    ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
7752
3.30k
    type_mask = ZEND_TYPE_PURE_MASK(type);
7753
3.30k
  }
7754
7755
289k
  if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_VOID)) {
7756
10
    zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type");
7757
10
  }
7758
7759
289k
  if ((type_mask & MAY_BE_NEVER) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_NEVER)) {
7760
5
    zend_error_noreturn(E_COMPILE_ERROR, "never can only be used as a standalone type");
7761
5
  }
7762
7763
289k
  ast->attr = orig_ast_attr;
7764
289k
  return type;
7765
289k
}
7766
/* }}} */
7767
7768
static zend_type zend_compile_typename(zend_ast *ast)
7769
47.2k
{
7770
47.2k
  bool forced_allow_null;
7771
47.2k
  return zend_compile_typename_ex(ast, false, &forced_allow_null);
7772
47.2k
}
7773
7774
/* May convert value from int to float. */
7775
static bool zend_is_valid_default_value(zend_type type, zval *value)
7776
4.09k
{
7777
4.09k
  ZEND_ASSERT(ZEND_TYPE_IS_SET(type));
7778
4.09k
  if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) {
7779
2.78k
    return 1;
7780
2.78k
  }
7781
1.31k
  if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) {
7782
    /* Integers are allowed as initializers for floating-point values. */
7783
1.16k
    convert_to_double(value);
7784
1.16k
    return 1;
7785
1.16k
  }
7786
144
  return 0;
7787
1.31k
}
7788
7789
static void zend_compile_attributes(
7790
  HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target, uint32_t promoted
7791
897k
) /* {{{ */ {
7792
897k
  zend_attribute *attr;
7793
897k
  zend_internal_attribute *config;
7794
7795
897k
  const zend_ast_list *list = zend_ast_get_list(ast);
7796
897k
  uint32_t g, i, j;
7797
7798
897k
  ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE_LIST);
7799
7800
1.79M
  for (g = 0; g < list->children; g++) {
7801
899k
    const zend_ast_list *group = zend_ast_get_list(list->child[g]);
7802
7803
899k
    ZEND_ASSERT(group->kind == ZEND_AST_ATTRIBUTE_GROUP);
7804
7805
2.28M
    for (i = 0; i < group->children; i++) {
7806
1.38M
      ZEND_ASSERT(group->child[i]->kind == ZEND_AST_ATTRIBUTE);
7807
7808
1.38M
      const zend_ast *el = group->child[i];
7809
7810
1.38M
      if (el->child[1] &&
7811
28.0k
          el->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) {
7812
10
          zend_error_noreturn(E_COMPILE_ERROR,
7813
10
              "Cannot create Closure as attribute argument");
7814
10
      }
7815
7816
1.38M
      zend_string *name = zend_resolve_class_name_ast(el->child[0]);
7817
1.38M
      zend_string *lcname = zend_string_tolower_ex(name, false);
7818
1.38M
      zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL;
7819
7820
1.38M
      config = zend_internal_attribute_get(lcname);
7821
1.38M
      zend_string_release(lcname);
7822
7823
      /* Exclude internal attributes that do not match on promoted properties. */
7824
1.38M
      if (config && !(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7825
831
        if (promoted & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL)) {
7826
9
          zend_string_release(name);
7827
9
          continue;
7828
9
        }
7829
831
      }
7830
7831
1.38M
      uint32_t flags = (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES)
7832
1.38M
        ? ZEND_ATTRIBUTE_STRICT_TYPES : 0;
7833
1.38M
      attr = zend_add_attribute(
7834
1.38M
        attributes, name, args ? args->children : 0, flags, offset, el->lineno);
7835
1.38M
      zend_string_release(name);
7836
7837
      /* Populate arguments */
7838
1.38M
      if (args) {
7839
28.0k
        ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST);
7840
7841
28.0k
        bool uses_named_args = false;
7842
69.5k
        for (j = 0; j < args->children; j++) {
7843
41.4k
          zend_ast **arg_ast_ptr = &args->child[j];
7844
41.4k
          zend_ast *arg_ast = *arg_ast_ptr;
7845
7846
41.4k
          if (arg_ast->kind == ZEND_AST_UNPACK) {
7847
5
            zend_error_noreturn(E_COMPILE_ERROR,
7848
5
              "Cannot use unpacking in attribute argument list");
7849
5
          }
7850
7851
41.4k
          if (arg_ast->kind == ZEND_AST_NAMED_ARG) {
7852
2.57k
            attr->args[j].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0]));
7853
2.57k
            arg_ast_ptr = &arg_ast->child[1];
7854
2.57k
            uses_named_args = true;
7855
7856
14.6k
            for (uint32_t k = 0; k < j; k++) {
7857
12.1k
              if (attr->args[k].name &&
7858
9.57k
                  zend_string_equals(attr->args[k].name, attr->args[j].name)) {
7859
14
                zend_error_noreturn(E_COMPILE_ERROR, "Duplicate named parameter $%s",
7860
14
                  ZSTR_VAL(attr->args[j].name));
7861
14
              }
7862
12.1k
            }
7863
38.9k
          } else if (uses_named_args) {
7864
30
            zend_error_noreturn(E_COMPILE_ERROR,
7865
30
              "Cannot use positional argument after named argument");
7866
30
          }
7867
7868
41.4k
          zend_const_expr_to_zval(
7869
41.4k
            &attr->args[j].value, arg_ast_ptr, /* allow_dynamic */ true);
7870
41.4k
        }
7871
28.0k
      }
7872
1.38M
    }
7873
899k
  }
7874
7875
897k
  if (*attributes != NULL) {
7876
    /* Allow delaying target validation for forward compatibility. */
7877
897k
    const zend_attribute *delayed_target_validation = NULL;
7878
897k
    if (target == ZEND_ATTRIBUTE_TARGET_PARAMETER) {
7879
23.5k
      ZEND_ASSERT(offset >= 1);
7880
      /* zend_get_parameter_attribute_str will add 1 too */
7881
23.5k
      delayed_target_validation = zend_get_parameter_attribute_str(
7882
23.5k
        *attributes,
7883
23.5k
        "delayedtargetvalidation",
7884
23.5k
        strlen("delayedtargetvalidation"),
7885
23.5k
        offset - 1
7886
23.5k
      );
7887
873k
    } else {
7888
873k
      delayed_target_validation = zend_get_attribute_str(
7889
873k
        *attributes,
7890
873k
        "delayedtargetvalidation",
7891
873k
        strlen("delayedtargetvalidation")
7892
873k
      );
7893
873k
    }
7894
    /* Validate attributes in a secondary loop (needed to detect repeated attributes). */
7895
5.24M
    ZEND_HASH_PACKED_FOREACH_PTR(*attributes, attr) {
7896
5.24M
      if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) {
7897
1.71M
        continue;
7898
1.71M
      }
7899
7900
5.24M
      bool run_validator = true;
7901
3.69k
      if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7902
351
        if (delayed_target_validation == NULL) {
7903
36
          zend_string *location = zend_get_attribute_target_names(target);
7904
36
          zend_string *allowed = zend_get_attribute_target_names(config->flags);
7905
7906
36
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
7907
36
            ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
7908
36
          );
7909
36
        }
7910
315
        run_validator = false;
7911
315
      }
7912
7913
3.65k
      if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
7914
3.65k
        if (zend_is_attribute_repeated(*attributes, attr)) {
7915
21
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name));
7916
21
        }
7917
3.65k
      }
7918
7919
      /* Validators are not run if the target is already invalid */
7920
3.63k
      if (run_validator && config->validator != NULL) {
7921
1.87k
        zend_string *error = config->validator(attr, target, CG(active_class_entry));
7922
1.87k
        if (error != NULL) {
7923
258
          if (delayed_target_validation == NULL) {
7924
85
            zend_error_noreturn(E_COMPILE_ERROR, "%s", ZSTR_VAL(error));
7925
173
          } else {
7926
173
            attr->validation_error = error;
7927
173
          }
7928
258
        }
7929
1.87k
      }
7930
3.63k
    } ZEND_HASH_FOREACH_END();
7931
897k
  }
7932
897k
}
7933
/* }}} */
7934
7935
static void zend_compile_property_hooks(
7936
    zend_property_info *prop_info, zend_string *prop_name,
7937
    zend_ast *prop_type_ast, const zend_ast_list *hooks);
7938
7939
typedef struct {
7940
  const zend_string *property_name;
7941
  bool uses_property;
7942
} find_property_usage_context;
7943
7944
static void zend_property_hook_find_property_usage(zend_ast **ast_ptr, void *_context) /* {{{ */
7945
35.2k
{
7946
35.2k
  zend_ast *ast = *ast_ptr;
7947
35.2k
  find_property_usage_context *context = (find_property_usage_context *) _context;
7948
7949
35.2k
  if (ast == NULL) {
7950
310
    return;
7951
34.9k
  } else if (ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_NULLSAFE_PROP) {
7952
3.14k
    const zend_ast *object_ast = ast->child[0];
7953
3.14k
    zend_ast *property_ast = ast->child[1];
7954
7955
3.14k
    if (object_ast->kind == ZEND_AST_VAR
7956
2.70k
     && object_ast->child[0]->kind == ZEND_AST_ZVAL
7957
2.60k
     && property_ast->kind == ZEND_AST_ZVAL) {
7958
2.54k
      const zval *object = zend_ast_get_zval(object_ast->child[0]);
7959
2.54k
      const zval *property = zend_ast_get_zval(property_ast);
7960
2.54k
      if (Z_TYPE_P(object) == IS_STRING
7961
2.47k
        && Z_TYPE_P(property) == IS_STRING
7962
2.47k
        && zend_string_equals_literal(Z_STR_P(object), "this")
7963
2.08k
        && zend_string_equals(Z_STR_P(property), context->property_name)) {
7964
749
        context->uses_property = true;
7965
        /* No need to look for references in this branch. */
7966
749
        return;
7967
749
      }
7968
2.54k
    }
7969
3.14k
  }
7970
7971
  /* Don't search across function/class boundaries. */
7972
34.1k
  if (!zend_ast_is_special(ast)) {
7973
21.7k
    zend_ast_apply(ast, zend_property_hook_find_property_usage, context);
7974
21.7k
  }
7975
34.1k
}
7976
7977
static bool zend_property_hook_uses_property(const zend_string *property_name, const zend_string *hook_name, zend_ast *hook_ast)
7978
4.26k
{
7979
4.26k
  if (zend_string_equals_literal_ci(hook_name, "set")
7980
1.66k
   && hook_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
7981
406
    return true;
7982
406
  }
7983
7984
3.85k
  find_property_usage_context context = { property_name, false };
7985
3.85k
  zend_property_hook_find_property_usage(&hook_ast, &context);
7986
3.85k
  return context.uses_property;
7987
4.26k
}
7988
7989
static bool zend_property_is_virtual(const zend_class_entry *ce, const zend_string *property_name, zend_ast *hooks_ast)
7990
31.1k
{
7991
31.1k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
7992
292
    return true;
7993
292
  }
7994
30.9k
  if (!hooks_ast) {
7995
27.5k
    return false;
7996
27.5k
  }
7997
7998
30.9k
  bool is_virtual = true;
7999
8000
3.33k
  const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
8001
8.10k
  for (uint32_t i = 0; i < hooks->children; i++) {
8002
4.76k
    const zend_ast_decl *hook = (const zend_ast_decl *) hooks->child[i];
8003
4.76k
    zend_ast *body = hook->child[2];
8004
4.76k
    if (body && zend_property_hook_uses_property(property_name, hook->name, body)) {
8005
1.11k
      is_virtual = false;
8006
1.11k
    }
8007
4.76k
  }
8008
8009
3.33k
  return is_virtual;
8010
30.9k
}
8011
8012
static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */
8013
1.01M
{
8014
1.01M
  zend_ast_list *list = zend_ast_get_list(ast);
8015
1.01M
  uint32_t i;
8016
1.01M
  zend_op_array *op_array = CG(active_op_array);
8017
1.01M
  zend_arg_info *arg_infos;
8018
8019
1.01M
  if (return_type_ast || fallback_return_type) {
8020
    /* Use op_array->arg_info[-1] for return type */
8021
23.1k
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
8022
23.1k
    arg_infos->name = NULL;
8023
23.1k
    if (return_type_ast) {
8024
17.2k
      arg_infos->type = zend_compile_typename(return_type_ast);
8025
17.2k
      ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS(
8026
17.2k
        (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0, /* is_tentative */ 0);
8027
17.2k
    } else {
8028
5.86k
      arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0);
8029
5.86k
    }
8030
23.1k
    arg_infos++;
8031
23.1k
    op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
8032
8033
23.1k
    if (ZEND_TYPE_CONTAINS_CODE(arg_infos[-1].type, IS_VOID)
8034
3.75k
        && (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
8035
408
      zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
8036
408
      zend_error(E_DEPRECATED, "%s(): Returning by reference from a void function is deprecated", ZSTR_VAL(func_name));
8037
408
      zend_string_release(func_name);
8038
408
    }
8039
990k
  } else {
8040
990k
    if (list->children == 0) {
8041
97.7k
      return;
8042
97.7k
    }
8043
892k
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
8044
892k
  }
8045
8046
  /* Find last required parameter number for deprecation message. */
8047
915k
  uint32_t last_required_param = (uint32_t) -1;
8048
1.85M
  for (i = 0; i < list->children; ++i) {
8049
936k
    zend_ast *param_ast = list->child[i];
8050
936k
    zend_ast *default_ast_ptr = param_ast->child[2];
8051
936k
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
8052
936k
    if (!default_ast_ptr && !is_variadic) {
8053
929k
      last_required_param = i;
8054
929k
    }
8055
936k
  }
8056
8057
915k
  const uint32_t promotion_flags = ZEND_ACC_PPP_MASK | ZEND_ACC_PPP_SET_MASK | ZEND_ACC_READONLY | ZEND_ACC_FINAL;
8058
1.85M
  for (i = 0; i < list->children; ++i) {
8059
936k
    zend_ast *param_ast = list->child[i];
8060
936k
    zend_ast *type_ast = param_ast->child[0];
8061
936k
    zend_ast *var_ast = param_ast->child[1];
8062
936k
    zend_ast **default_ast_ptr = &param_ast->child[2];
8063
936k
    zend_ast *attributes_ast = param_ast->child[3];
8064
936k
    zend_ast *doc_comment_ast = param_ast->child[4];
8065
936k
    zend_ast *hooks_ast = param_ast->child[5];
8066
936k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast));
8067
936k
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
8068
936k
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
8069
936k
    uint32_t property_flags = param_ast->attr & promotion_flags;
8070
936k
    bool is_promoted = property_flags || hooks_ast;
8071
8072
936k
    CG(zend_lineno) = param_ast->lineno;
8073
8074
936k
    znode var_node, default_node;
8075
936k
    uint8_t opcode;
8076
936k
    zend_op *opline;
8077
936k
    zend_arg_info *arg_info;
8078
8079
936k
    if (zend_is_auto_global(name)) {
8080
3
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
8081
3
        ZSTR_VAL(name));
8082
3
    }
8083
8084
936k
    var_node.op_type = IS_CV;
8085
936k
    var_node.u.op.var = lookup_cv(name);
8086
8087
936k
    if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
8088
27
      zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
8089
27
        ZSTR_VAL(name));
8090
936k
    } else if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8091
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
8092
936k
    } else if (zend_string_equals_literal(name, "http_response_header")) {
8093
37
      CG(context).has_assigned_to_http_response_header = true;
8094
37
    }
8095
8096
936k
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
8097
6
      zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
8098
6
    }
8099
8100
936k
    if (is_variadic) {
8101
882
      opcode = ZEND_RECV_VARIADIC;
8102
882
      default_node.op_type = IS_UNUSED;
8103
882
      op_array->fn_flags |= ZEND_ACC_VARIADIC;
8104
8105
882
      if (*default_ast_ptr) {
8106
5
        zend_error_noreturn(E_COMPILE_ERROR,
8107
5
          "Variadic parameter cannot have a default value");
8108
5
      }
8109
935k
    } else if (*default_ast_ptr) {
8110
      /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
8111
5.71k
      uint32_t cops = CG(compiler_options);
8112
5.71k
      CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
8113
5.71k
      opcode = ZEND_RECV_INIT;
8114
5.71k
      default_node.op_type = IS_CONST;
8115
5.71k
      zend_const_expr_to_zval(
8116
5.71k
        &default_node.u.constant, default_ast_ptr, /* allow_dynamic */ true);
8117
5.71k
      CG(compiler_options) = cops;
8118
929k
    } else {
8119
929k
      opcode = ZEND_RECV;
8120
929k
      default_node.op_type = IS_UNUSED;
8121
929k
      op_array->required_num_args = i + 1;
8122
929k
    }
8123
8124
936k
    arg_info = &arg_infos[i];
8125
936k
    arg_info->name = zend_string_copy(name);
8126
936k
    arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0);
8127
936k
    arg_info->default_value = NULL;
8128
8129
936k
    if (attributes_ast) {
8130
23.5k
      zend_compile_attributes(
8131
23.5k
        &op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER,
8132
23.5k
        is_promoted ? ZEND_ATTRIBUTE_TARGET_PROPERTY : 0
8133
23.5k
      );
8134
23.5k
    }
8135
8136
936k
    bool forced_allow_nullable = false;
8137
936k
    if (type_ast) {
8138
242k
      uint32_t default_type = *default_ast_ptr ? Z_TYPE(default_node.u.constant) : IS_UNDEF;
8139
242k
      bool force_nullable = default_type == IS_NULL && !is_promoted;
8140
8141
242k
      op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
8142
242k
      arg_info->type = zend_compile_typename_ex(type_ast, force_nullable, &forced_allow_nullable);
8143
242k
      if (forced_allow_nullable) {
8144
369
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
8145
369
        zend_error(E_DEPRECATED,
8146
369
           "%s(): Implicitly marking parameter $%s as nullable is deprecated, the explicit nullable type "
8147
369
           "must be used instead", ZSTR_VAL(func_name), ZSTR_VAL(name));
8148
369
        zend_string_release(func_name);
8149
369
      }
8150
8151
242k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) {
8152
5
        zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");
8153
5
      }
8154
8155
242k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_NEVER) {
8156
5
        zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type");
8157
5
      }
8158
8159
242k
      if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable
8160
583
          && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) {
8161
44
        zend_string *type_str = zend_type_to_string(arg_info->type);
8162
44
        zend_error_noreturn(E_COMPILE_ERROR,
8163
44
          "Cannot use %s as default value for parameter $%s of type %s",
8164
44
          zend_get_type_by_const(default_type),
8165
44
          ZSTR_VAL(name), ZSTR_VAL(type_str));
8166
44
      }
8167
242k
    }
8168
936k
    if (last_required_param != (uint32_t) -1
8169
931k
     && i < last_required_param
8170
32.5k
     && default_node.op_type == IS_CONST) {
8171
      /* Ignore parameters of the form "Type $param = null".
8172
       * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
8173
659
      if (!forced_allow_nullable) {
8174
363
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
8175
363
        zend_ast *required_param_ast = list->child[last_required_param];
8176
363
        zend_error(E_DEPRECATED,
8177
363
          "%s(): Optional parameter $%s declared before required parameter $%s "
8178
363
          "is implicitly treated as a required parameter",
8179
363
          ZSTR_VAL(func_name), ZSTR_VAL(name), ZSTR_VAL(zend_ast_get_str(required_param_ast->child[1])));
8180
363
        zend_string_release(func_name);
8181
363
      }
8182
8183
      /* Regardless of whether we issue a deprecation, convert this parameter into
8184
       * a required parameter without a default value. This ensures that it cannot be
8185
       * used as an optional parameter even with named parameters. */
8186
659
      opcode = ZEND_RECV;
8187
659
      default_node.op_type = IS_UNUSED;
8188
659
      zval_ptr_dtor(&default_node.u.constant);
8189
659
    }
8190
8191
936k
    opline = zend_emit_op(NULL, opcode, NULL, &default_node);
8192
936k
    SET_NODE(opline->result, &var_node);
8193
936k
    opline->op1.num = i + 1;
8194
8195
936k
    uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic, /* is_tentative */ 0)
8196
936k
      | (is_promoted ? _ZEND_IS_PROMOTED_BIT : 0);
8197
936k
    ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags;
8198
936k
    if (opcode == ZEND_RECV) {
8199
930k
      opline->op2.num = type_ast ?
8200
690k
        ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY;
8201
930k
    }
8202
8203
936k
    if (is_promoted) {
8204
985
      const zend_op_array *active_op_array = CG(active_op_array);
8205
985
      zend_class_entry *scope = active_op_array->scope;
8206
8207
985
      bool is_ctor =
8208
985
        scope && zend_is_constructor(active_op_array->function_name);
8209
985
      if (!is_ctor) {
8210
24
        zend_error_noreturn(E_COMPILE_ERROR,
8211
24
          "Cannot declare promoted property outside a constructor");
8212
24
      }
8213
961
      if ((active_op_array->fn_flags & ZEND_ACC_ABSTRACT)
8214
956
          || (scope->ce_flags & ZEND_ACC_INTERFACE)) {
8215
5
        zend_error_noreturn(E_COMPILE_ERROR,
8216
5
          "Cannot declare promoted property in an abstract constructor");
8217
5
      }
8218
956
      if (is_variadic) {
8219
5
        zend_error_noreturn(E_COMPILE_ERROR,
8220
5
          "Cannot declare variadic promoted property");
8221
5
      }
8222
951
      if (zend_hash_exists(&scope->properties_info, name)) {
8223
5
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
8224
5
          ZSTR_VAL(scope->name), ZSTR_VAL(name));
8225
5
      }
8226
946
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_CALLABLE) {
8227
5
        zend_string *str = zend_type_to_string(arg_info->type);
8228
5
        zend_error_noreturn(E_COMPILE_ERROR,
8229
5
          "Property %s::$%s cannot have type %s",
8230
5
          ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str));
8231
5
      }
8232
8233
941
      if (!(property_flags & ZEND_ACC_READONLY) && (scope->ce_flags & ZEND_ACC_READONLY_CLASS)) {
8234
22
        property_flags |= ZEND_ACC_READONLY;
8235
22
      }
8236
8237
      /* Recompile the type, as it has different memory management requirements. */
8238
941
      zend_type type = ZEND_TYPE_INIT_NONE(0);
8239
941
      if (type_ast) {
8240
730
        type = zend_compile_typename(type_ast);
8241
730
      }
8242
8243
      /* Don't give the property an explicit default value. For typed properties this means
8244
       * uninitialized, for untyped properties it means an implicit null default value.
8245
       * Properties with hooks get an implicit default value of undefined until inheritance,
8246
       * where it is changed to null only once we know it is not virtual. If we were to set it
8247
       * here, we couldn't verify that a true virtual property must not have an explicit
8248
       * default value. */
8249
941
      zval default_value;
8250
941
      if (ZEND_TYPE_IS_SET(type) || hooks_ast) {
8251
762
        ZVAL_UNDEF(&default_value);
8252
762
      } else {
8253
179
        if (property_flags & ZEND_ACC_READONLY) {
8254
10
          zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
8255
10
            ZSTR_VAL(scope->name), ZSTR_VAL(name));
8256
10
        }
8257
8258
169
        ZVAL_NULL(&default_value);
8259
169
      }
8260
8261
931
      zend_string *doc_comment =
8262
931
        doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
8263
931
      zend_property_info *prop = zend_declare_typed_property(
8264
931
        scope, name, &default_value,
8265
931
        property_flags | (zend_property_is_virtual(scope, name, hooks_ast) ? ZEND_ACC_VIRTUAL : 0) | ZEND_ACC_PROMOTED,
8266
931
        doc_comment, type);
8267
931
      if (hooks_ast) {
8268
82
        const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
8269
82
        zend_compile_property_hooks(prop, name, type_ast, hooks);
8270
82
      }
8271
931
      if (attributes_ast) {
8272
55
        zend_compile_attributes(
8273
55
          &prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, ZEND_ATTRIBUTE_TARGET_PARAMETER);
8274
8275
55
        zend_attribute *override_attribute = zend_get_attribute_str(prop->attributes, "override", sizeof("override")-1);
8276
55
        if (override_attribute) {
8277
9
          prop->flags |= ZEND_ACC_OVERRIDE;
8278
9
        }
8279
55
      }
8280
931
    }
8281
936k
  }
8282
8283
  /* These are assigned at the end to avoid uninitialized memory in case of an error */
8284
915k
  op_array->num_args = list->children;
8285
915k
  op_array->arg_info = arg_infos;
8286
8287
  /* Don't count the variadic argument */
8288
915k
  if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
8289
866
    op_array->num_args--;
8290
866
  }
8291
915k
  zend_set_function_arg_flags((zend_function*)op_array);
8292
8293
1.85M
  for (i = 0; i < list->children; i++) {
8294
936k
    zend_ast *param_ast = list->child[i];
8295
936k
    zend_ast *hooks_ast = param_ast->child[5];
8296
936k
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
8297
936k
    uint32_t flags = param_ast->attr & promotion_flags;
8298
936k
    bool is_promoted = flags || hooks_ast;
8299
936k
    if (!is_promoted) {
8300
935k
      continue;
8301
935k
    }
8302
8303
911
    CG(zend_lineno) = param_ast->lineno;
8304
8305
    /* Emit $this->prop = $prop for promoted properties. */
8306
911
    zend_string *name = zend_ast_get_str(param_ast->child[1]);
8307
911
    znode name_node, value_node;
8308
911
    name_node.op_type = IS_CONST;
8309
911
    ZVAL_STR_COPY(&name_node.u.constant, name);
8310
911
    value_node.op_type = IS_CV;
8311
911
    value_node.u.op.var = lookup_cv(name);
8312
8313
911
    zend_op *opline = zend_emit_op(NULL,
8314
911
      is_ref ? ZEND_ASSIGN_OBJ_REF : ZEND_ASSIGN_OBJ, NULL, &name_node);
8315
911
    opline->extended_value = zend_alloc_cache_slots(3);
8316
911
    zend_emit_op_data(&value_node);
8317
911
  }
8318
915k
}
8319
/* }}} */
8320
8321
static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */
8322
1.69k
{
8323
1.69k
  const zend_ast_list *list = zend_ast_get_list(uses_ast);
8324
1.69k
  uint32_t i;
8325
8326
1.69k
  if (!list->children) {
8327
0
    return;
8328
0
  }
8329
8330
1.69k
  if (!op_array->static_variables) {
8331
1.69k
    op_array->static_variables = zend_new_array(8);
8332
1.69k
  }
8333
8334
6.00k
  for (i = 0; i < list->children; ++i) {
8335
4.32k
    zend_ast *var_name_ast = list->child[i];
8336
4.32k
    zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast));
8337
4.32k
    uint32_t mode = var_name_ast->attr;
8338
4.32k
    zend_op *opline;
8339
4.32k
    zval *value;
8340
8341
4.32k
    if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8342
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
8343
5
    }
8344
8345
4.32k
    if (zend_is_auto_global(var_name)) {
8346
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable");
8347
5
    }
8348
8349
4.31k
    value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval));
8350
4.31k
    if (!value) {
8351
10
      zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8352
10
        "Cannot use variable $%S twice", var_name);
8353
10
    }
8354
8355
4.30k
    CG(zend_lineno) = zend_ast_get_lineno(var_name_ast);
8356
8357
4.30k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8358
4.30k
    opline->op2_type = IS_CV;
8359
4.30k
    opline->op2.var = lookup_cv(var_name);
8360
4.30k
    opline->extended_value =
8361
4.30k
      (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode;
8362
4.30k
  }
8363
1.69k
}
8364
/* }}} */
8365
8366
typedef struct {
8367
  HashTable uses;
8368
  bool varvars_used;
8369
} closure_info;
8370
8371
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast);
8372
8373
852k
static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
8374
852k
  if (!ast) {
8375
4.57k
    return;
8376
4.57k
  }
8377
8378
848k
  if (ast->kind == ZEND_AST_VAR) {
8379
130k
    zend_ast *name_ast = ast->child[0];
8380
130k
    if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) {
8381
128k
      zend_string *name = zend_ast_get_str(name_ast);
8382
128k
      if (zend_is_auto_global(name)) {
8383
        /* These is no need to explicitly import auto-globals. */
8384
2.59k
        return;
8385
2.59k
      }
8386
8387
125k
      if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8388
        /* $this does not need to be explicitly imported. */
8389
9.56k
        return;
8390
9.56k
      }
8391
8392
116k
      zend_hash_add_empty_element(&info->uses, name);
8393
116k
    } else {
8394
2.14k
      info->varvars_used = true;
8395
2.14k
      find_implicit_binds_recursively(info, name_ast);
8396
2.14k
    }
8397
717k
  } else if (zend_ast_is_list(ast)) {
8398
23.5k
    const zend_ast_list *list = zend_ast_get_list(ast);
8399
23.5k
    uint32_t i;
8400
242k
    for (i = 0; i < list->children; i++) {
8401
218k
      find_implicit_binds_recursively(info, list->child[i]);
8402
218k
    }
8403
694k
  } else if (ast->kind == ZEND_AST_CLOSURE) {
8404
    /* For normal closures add the use() list. */
8405
346
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8406
346
    zend_ast *uses_ast = closure_ast->child[1];
8407
346
    if (uses_ast) {
8408
127
      const zend_ast_list *uses_list = zend_ast_get_list(uses_ast);
8409
127
      uint32_t i;
8410
971
      for (i = 0; i < uses_list->children; i++) {
8411
844
        zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i]));
8412
844
      }
8413
127
    }
8414
693k
  } else if (ast->kind == ZEND_AST_ARROW_FUNC) {
8415
    /* For arrow functions recursively check the expression. */
8416
235k
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8417
235k
    closure_info inner_info;
8418
235k
    find_implicit_binds(&inner_info, closure_ast->child[0], closure_ast->child[2]);
8419
235k
    if (inner_info.varvars_used) {
8420
10.4k
      info->varvars_used = true;
8421
10.4k
    }
8422
235k
    if (zend_hash_num_elements(&inner_info.uses)) {
8423
222k
      zend_hash_copy(&info->uses, &inner_info.uses, NULL);
8424
222k
    }
8425
235k
    zend_hash_destroy(&inner_info.uses);
8426
458k
  } else if (!zend_ast_is_special(ast)) {
8427
305k
    uint32_t i, children = zend_ast_get_num_children(ast);
8428
685k
    for (i = 0; i < children; i++) {
8429
379k
      find_implicit_binds_recursively(info, ast->child[i]);
8430
379k
    }
8431
305k
  }
8432
848k
}
8433
8434
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast)
8435
252k
{
8436
252k
  const zend_ast_list *param_list = zend_ast_get_list(params_ast);
8437
252k
  uint32_t i;
8438
8439
252k
  zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0);
8440
252k
  info->varvars_used = false;
8441
8442
252k
  find_implicit_binds_recursively(info, stmt_ast);
8443
8444
  /* Remove variables that are parameters */
8445
292k
  for (i = 0; i < param_list->children; i++) {
8446
39.7k
    const zend_ast *param_ast = param_list->child[i];
8447
39.7k
    zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1]));
8448
39.7k
  }
8449
252k
}
8450
8451
static void compile_implicit_lexical_binds(
8452
    const closure_info *info, znode *closure, zend_op_array *op_array)
8453
17.0k
{
8454
17.0k
  zend_string *var_name;
8455
17.0k
  zend_op *opline;
8456
8457
  /* TODO We might want to use a special binding mode if varvars_used is set. */
8458
17.0k
  if (zend_hash_num_elements(&info->uses) == 0) {
8459
10.1k
    return;
8460
10.1k
  }
8461
8462
6.86k
  if (!op_array->static_variables) {
8463
6.86k
    op_array->static_variables = zend_new_array(8);
8464
6.86k
  }
8465
8466
98.7k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8467
98.7k
    zval *value = zend_hash_add(
8468
98.7k
      op_array->static_variables, var_name, &EG(uninitialized_zval));
8469
98.7k
    uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData);
8470
8471
98.7k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8472
98.7k
    opline->op2_type = IS_CV;
8473
98.7k
    opline->op2.var = lookup_cv(var_name);
8474
98.7k
    opline->extended_value = offset | ZEND_BIND_IMPLICIT;
8475
98.7k
  ZEND_HASH_FOREACH_END();
8476
6.86k
}
8477
8478
static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
8479
1.67k
{
8480
1.67k
  const zend_op_array *op_array = CG(active_op_array);
8481
1.67k
  const zend_ast_list *list = zend_ast_get_list(ast);
8482
1.67k
  uint32_t i;
8483
8484
5.94k
  for (i = 0; i < list->children; ++i) {
8485
4.27k
    uint32_t mode = ZEND_BIND_EXPLICIT;
8486
4.27k
    zend_ast *var_ast = list->child[i];
8487
4.27k
    zend_string *var_name = zend_ast_get_str(var_ast);
8488
4.27k
    zval zv;
8489
4.27k
    ZVAL_NULL(&zv);
8490
8491
4.27k
    {
8492
4.27k
      int i;
8493
13.0k
      for (i = 0; i < op_array->last_var; i++) {
8494
8.79k
        if (zend_string_equals(op_array->vars[i], var_name)) {
8495
5
          zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8496
5
            "Cannot use lexical variable $%S as a parameter name", var_name);
8497
5
        }
8498
8.79k
      }
8499
4.27k
    }
8500
8501
4.26k
    CG(zend_lineno) = zend_ast_get_lineno(var_ast);
8502
8503
4.26k
    if (var_ast->attr) {
8504
1.75k
      mode |= ZEND_BIND_REF;
8505
1.75k
    }
8506
8507
4.26k
    zend_compile_static_var_common(var_name, &zv, mode);
8508
4.26k
  }
8509
1.67k
}
8510
/* }}} */
8511
8512
static void zend_compile_implicit_closure_uses(const closure_info *info)
8513
17.0k
{
8514
17.0k
  zend_string *var_name;
8515
119k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8516
119k
    zval zv;
8517
119k
    ZVAL_NULL(&zv);
8518
119k
    zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT);
8519
119k
  ZEND_HASH_FOREACH_END();
8520
17.0k
}
8521
8522
5.94k
static void add_stringable_interface(zend_class_entry *ce) {
8523
16.7k
  for (uint32_t i = 0; i < ce->num_interfaces; i++) {
8524
10.8k
    if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) {
8525
      /* Interface already explicitly implemented */
8526
29
      return;
8527
29
    }
8528
10.8k
  }
8529
8530
5.91k
  ce->num_interfaces++;
8531
5.91k
  ce->interface_names =
8532
5.91k
    erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
8533
  // TODO: Add known interned strings instead?
8534
5.91k
  ce->interface_names[ce->num_interfaces - 1].name =
8535
5.91k
    ZSTR_INIT_LITERAL("Stringable", 0);
8536
5.91k
  ce->interface_names[ce->num_interfaces - 1].lc_name =
8537
5.91k
    ZSTR_INIT_LITERAL("stringable", 0);
8538
5.91k
}
8539
8540
static zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, bool has_body) /* {{{ */
8541
60.4k
{
8542
60.4k
  zend_class_entry *ce = CG(active_class_entry);
8543
60.4k
  bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
8544
60.4k
  uint32_t fn_flags = op_array->fn_flags;
8545
8546
60.4k
  zend_string *lcname;
8547
8548
60.4k
  if (fn_flags & ZEND_ACC_READONLY) {
8549
0
    zend_error(E_COMPILE_ERROR, "Cannot use 'readonly' as method modifier");
8550
0
  }
8551
8552
60.4k
  if ((fn_flags & ZEND_ACC_PRIVATE) && (fn_flags & ZEND_ACC_FINAL) && !zend_is_constructor(name)) {
8553
66
    zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes");
8554
66
  }
8555
8556
60.4k
  if ((fn_flags & ZEND_ACC_ABSTRACT)
8557
452
   && !(ce->ce_flags & (ZEND_ACC_EXPLICIT_ABSTRACT_CLASS|ZEND_ACC_TRAIT))) {
8558
    // Don't say that the class should be declared abstract if it is
8559
    // anonymous or an enum and can't be abstract
8560
36
    if (ce->ce_flags & ZEND_ACC_ANON_CLASS) {
8561
6
      zend_error_noreturn(E_COMPILE_ERROR, "Anonymous class method %s() must not be abstract",
8562
6
        ZSTR_VAL(name));
8563
30
    } else if (ce->ce_flags & (ZEND_ACC_ENUM|ZEND_ACC_INTERFACE)) {
8564
19
      zend_error_noreturn(E_COMPILE_ERROR, "%s method %s::%s() must not be abstract",
8565
19
        zend_get_object_type_case(ce, true), ZSTR_VAL(ce->name), ZSTR_VAL(name));
8566
19
    } else {
8567
11
      zend_error_noreturn(E_COMPILE_ERROR, "Class %s declares abstract method %s() and must therefore be declared abstract",
8568
11
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
8569
11
    }
8570
36
  }
8571
8572
60.4k
  if (in_interface) {
8573
825
    if (!(fn_flags & ZEND_ACC_PUBLIC)) {
8574
1
      zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
8575
1
        "%s::%s() must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8576
1
    }
8577
824
    if (fn_flags & ZEND_ACC_FINAL) {
8578
5
      zend_error_noreturn(E_COMPILE_ERROR, "Interface method "
8579
5
        "%s::%s() must not be final", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8580
5
    }
8581
819
    op_array->fn_flags |= ZEND_ACC_ABSTRACT;
8582
819
  }
8583
8584
60.4k
  if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
8585
1.23k
    if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8586
5
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
8587
5
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8588
5
    }
8589
8590
1.23k
    if (has_body) {
8591
5
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
8592
5
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8593
5
    }
8594
8595
1.22k
    ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
8596
59.2k
  } else if (!has_body) {
8597
9
    zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
8598
9
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8599
9
  }
8600
8601
60.4k
  op_array->scope = ce;
8602
60.4k
  op_array->function_name = zend_string_copy(name);
8603
8604
60.4k
  lcname = zend_string_tolower(name);
8605
60.4k
  lcname = zend_new_interned_string(lcname);
8606
8607
60.4k
  if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
8608
31
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
8609
31
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8610
31
  }
8611
8612
60.3k
  zend_add_magic_method(ce, (zend_function *) op_array, lcname);
8613
60.3k
  if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)
8614
5.96k
      && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8615
5.94k
    add_stringable_interface(ce);
8616
5.94k
  }
8617
8618
60.3k
  return lcname;
8619
60.4k
}
8620
/* }}} */
8621
8622
933k
static uint32_t zend_add_dynamic_func_def(zend_op_array *def) {
8623
933k
  zend_op_array *op_array = CG(active_op_array);
8624
933k
  uint32_t def_offset = op_array->num_dynamic_func_defs++;
8625
933k
  op_array->dynamic_func_defs = erealloc(
8626
933k
    op_array->dynamic_func_defs, op_array->num_dynamic_func_defs * sizeof(zend_op_array *));
8627
933k
  op_array->dynamic_func_defs[def_offset] = def;
8628
933k
  return def_offset;
8629
933k
}
8630
8631
enum func_decl_level {
8632
  FUNC_DECL_LEVEL_TOPLEVEL,
8633
  FUNC_DECL_LEVEL_NESTED,
8634
  FUNC_DECL_LEVEL_CONSTEXPR,
8635
};
8636
8637
static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array, const zend_ast_decl *decl, enum func_decl_level level) /* {{{ */
8638
948k
{
8639
948k
  zend_string *unqualified_name, *name, *lcname;
8640
948k
  zend_op *opline;
8641
8642
948k
  if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8643
931k
    zend_string *filename = op_array->filename;
8644
931k
    uint32_t start_lineno = decl->start_lineno;
8645
8646
931k
    zend_string *class = zend_empty_string;
8647
931k
    zend_string *separator = zend_empty_string;
8648
931k
    zend_string *function = filename;
8649
931k
    const char *parens = "";
8650
8651
931k
    if (CG(active_op_array) && CG(active_op_array)->function_name) {
8652
919k
      if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
8653
        /* If the parent function is a closure, don't redundantly
8654
         * add the classname and parentheses.
8655
         */
8656
918k
        function = CG(active_op_array)->function_name;
8657
918k
      } else {
8658
1.56k
        function = CG(active_op_array)->function_name;
8659
1.56k
        parens = "()";
8660
8661
1.56k
        if (CG(active_class_entry) && CG(active_class_entry)->name) {
8662
1.04k
          class = CG(active_class_entry)->name;
8663
1.04k
          separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM);
8664
1.04k
        }
8665
1.56k
      }
8666
919k
    }
8667
8668
931k
    unqualified_name = zend_strpprintf_unchecked(
8669
931k
      0,
8670
931k
      "{closure:%S%S%S%s:%" PRIu32 "}",
8671
931k
      class,
8672
931k
      separator,
8673
931k
      function,
8674
931k
      parens,
8675
931k
      start_lineno
8676
931k
    );
8677
8678
931k
    op_array->function_name = name = unqualified_name;
8679
931k
  } else {
8680
16.8k
    unqualified_name = decl->name;
8681
16.8k
    op_array->function_name = name = zend_prefix_with_ns(unqualified_name);
8682
16.8k
  }
8683
8684
948k
  lcname = zend_string_tolower(name);
8685
8686
948k
  if (FC(imports_function)) {
8687
97
    const zend_string *import_name =
8688
97
      zend_hash_find_ptr_lc(FC(imports_function), unqualified_name);
8689
97
    if (import_name && !zend_string_equals_ci(lcname, import_name)) {
8690
9
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare function %s() (previously declared as local import)",
8691
9
        ZSTR_VAL(name));
8692
9
    }
8693
97
  }
8694
8695
948k
  if (zend_string_equals_literal(lcname, "__autoload")) {
8696
1
    zend_error_noreturn(E_COMPILE_ERROR,
8697
1
      "__autoload() is no longer supported, use spl_autoload_register() instead");
8698
1
  }
8699
8700
948k
  if (zend_string_equals_literal_ci(unqualified_name, "assert")) {
8701
6
    zend_error(E_COMPILE_ERROR,
8702
6
      "Defining a custom assert() function is not allowed, "
8703
6
      "as the function has special semantics");
8704
6
  }
8705
8706
948k
  zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
8707
948k
  switch (level) {
8708
933k
    case FUNC_DECL_LEVEL_NESTED: {
8709
933k
      uint32_t func_ref = zend_add_dynamic_func_def(op_array);
8710
933k
      if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8711
931k
        opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
8712
931k
        opline->op2.num = func_ref;
8713
931k
      } else {
8714
1.55k
        opline = get_next_op();
8715
1.55k
        opline->opcode = ZEND_DECLARE_FUNCTION;
8716
1.55k
        opline->op1_type = IS_CONST;
8717
1.55k
        LITERAL_STR(opline->op1, zend_string_copy(lcname));
8718
1.55k
        opline->op2.num = func_ref;
8719
1.55k
      }
8720
933k
      break;
8721
0
    }
8722
139
    case FUNC_DECL_LEVEL_CONSTEXPR:
8723
15.4k
    case FUNC_DECL_LEVEL_TOPLEVEL:
8724
      /* Nothing to do. */
8725
15.4k
      break;
8726
948k
  }
8727
948k
  return lcname;
8728
948k
}
8729
/* }}} */
8730
8731
static zend_op_array *zend_compile_func_decl_ex(
8732
  znode *result, zend_ast *ast, enum func_decl_level level,
8733
  zend_string *property_info_name,
8734
  zend_property_hook_kind hook_kind
8735
1.01M
) {
8736
1.01M
  zend_ast_decl *decl = (zend_ast_decl *) ast;
8737
1.01M
  zend_ast *params_ast = decl->child[0];
8738
1.01M
  zend_ast *uses_ast = decl->child[1];
8739
1.01M
  zend_ast *stmt_ast = decl->child[2];
8740
1.01M
  zend_ast *return_type_ast = decl->child[3];
8741
1.01M
  bool is_method = decl->kind == ZEND_AST_METHOD;
8742
1.01M
  zend_string *lcname = NULL;
8743
1.01M
  bool is_hook = decl->kind == ZEND_AST_PROPERTY_HOOK;
8744
8745
1.01M
  zend_class_entry *orig_class_entry = CG(active_class_entry);
8746
1.01M
  zend_op_array *orig_op_array = CG(active_op_array);
8747
1.01M
  zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
8748
1.01M
  zend_oparray_context orig_oparray_context;
8749
1.01M
  closure_info info;
8750
8751
1.01M
  init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
8752
8753
1.01M
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
8754
0
    op_array->fn_flags |= ZEND_ACC_PRELOADED;
8755
0
  }
8756
8757
1.01M
  op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
8758
1.01M
  op_array->fn_flags |= decl->flags;
8759
1.01M
  op_array->line_start = decl->start_lineno;
8760
1.01M
  op_array->line_end = decl->end_lineno;
8761
1.01M
  if (decl->doc_comment) {
8762
193
    op_array->doc_comment = zend_string_copy(decl->doc_comment);
8763
193
  }
8764
8765
1.01M
  if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
8766
931k
    op_array->fn_flags |= ZEND_ACC_CLOSURE;
8767
931k
  }
8768
8769
1.01M
  if (is_hook) {
8770
4.16k
    zend_class_entry *ce = CG(active_class_entry);
8771
4.16k
    op_array->scope = ce;
8772
4.16k
    op_array->function_name = zend_string_copy(decl->name);
8773
1.00M
  } else if (is_method) {
8774
60.4k
    bool has_body = stmt_ast != NULL;
8775
60.4k
    lcname = zend_begin_method_decl(op_array, decl->name, has_body);
8776
948k
  } else {
8777
948k
    lcname = zend_begin_func_decl(result, op_array, decl, level);
8778
948k
    if (decl->kind == ZEND_AST_ARROW_FUNC) {
8779
17.0k
      find_implicit_binds(&info, params_ast, stmt_ast);
8780
17.0k
      compile_implicit_lexical_binds(&info, result, op_array);
8781
931k
    } else if (uses_ast) {
8782
1.69k
      zend_compile_closure_binding(result, op_array, uses_ast);
8783
1.69k
    }
8784
948k
  }
8785
8786
1.01M
  CG(active_op_array) = op_array;
8787
8788
1.01M
  zend_oparray_context_begin(&orig_oparray_context, op_array);
8789
1.01M
  CG(context).active_property_info_name = property_info_name;
8790
1.01M
  CG(context).active_property_hook_kind = hook_kind;
8791
8792
1.01M
  if (decl->child[4]) {
8793
867k
    int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
8794
8795
867k
    if (is_method || is_hook) {
8796
1.01k
      target = ZEND_ATTRIBUTE_TARGET_METHOD;
8797
1.01k
    }
8798
8799
867k
    zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target, 0);
8800
8801
867k
    const zend_attribute *override_attribute = zend_get_attribute_str(
8802
867k
      op_array->attributes,
8803
867k
      "override",
8804
867k
      sizeof("override")-1
8805
867k
    );
8806
8807
867k
    if (override_attribute) {
8808
527
      op_array->fn_flags |= ZEND_ACC_OVERRIDE;
8809
527
    }
8810
8811
867k
    const zend_attribute *deprecated_attribute = zend_get_attribute_str(
8812
867k
      op_array->attributes,
8813
867k
      "deprecated",
8814
867k
      sizeof("deprecated")-1
8815
867k
    );
8816
8817
867k
    if (deprecated_attribute) {
8818
249
      op_array->fn_flags |= ZEND_ACC_DEPRECATED;
8819
249
    }
8820
8821
    // ZEND_ACC_NODISCARD is added via an attribute validator
8822
867k
  }
8823
8824
  /* Do not leak the class scope into free standing functions, even if they are dynamically
8825
   * defined inside a class method. This is necessary for correct handling of magic constants.
8826
   * For example __CLASS__ should always be "" inside a free standing function. */
8827
1.01M
  if (decl->kind == ZEND_AST_FUNC_DECL) {
8828
16.8k
    CG(active_class_entry) = NULL;
8829
16.8k
  }
8830
8831
1.01M
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8832
15.2k
    op_array->fn_flags |= ZEND_ACC_TOP_LEVEL;
8833
15.2k
  }
8834
8835
1.01M
  {
8836
    /* Push a separator to the loop variable stack */
8837
1.01M
    zend_loop_var dummy_var;
8838
1.01M
    dummy_var.opcode = ZEND_RETURN;
8839
8840
1.01M
    zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
8841
1.01M
  }
8842
8843
1.01M
  zend_compile_params(params_ast, return_type_ast,
8844
1.01M
    is_method && zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME) ? IS_STRING : 0);
8845
1.01M
  if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
8846
27.7k
    zend_mark_function_as_generator();
8847
27.7k
    zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
8848
27.7k
  }
8849
1.01M
  if (decl->kind == ZEND_AST_ARROW_FUNC) {
8850
17.0k
    zend_compile_implicit_closure_uses(&info);
8851
17.0k
    zend_hash_destroy(&info.uses);
8852
996k
  } else if (uses_ast) {
8853
1.67k
    zend_compile_closure_uses(uses_ast);
8854
1.67k
  }
8855
8856
1.01M
  if (ast->kind == ZEND_AST_ARROW_FUNC && decl->child[2]->kind != ZEND_AST_RETURN) {
8857
9.14k
    bool needs_return = true;
8858
9.14k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8859
1.35k
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8860
1.35k
      needs_return = !ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER);
8861
1.35k
    }
8862
9.14k
    if (needs_return) {
8863
8.76k
      stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
8864
8.76k
      decl->child[2] = stmt_ast;
8865
8.76k
    }
8866
9.14k
  }
8867
8868
1.01M
  if (op_array->fn_flags & ZEND_ACC_NODISCARD) {
8869
    /* ZEND_ACC_NODISCARD gets added by the attribute validator, but only
8870
     * if the method is not a hook; if it is a hook, then the validator
8871
     * will have returned an error message, even if the error message was
8872
     * delayed with #[\DelayedTargetValidation] that ZEND_ACC_NODISCARD
8873
     * flag should not have been added. */
8874
246
    ZEND_ASSERT(!is_hook);
8875
8876
246
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8877
201
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8878
201
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_VOID)) {
8879
5
        zend_error_noreturn(E_COMPILE_ERROR,
8880
5
          "A void %s does not return a value, but #[\\NoDiscard] requires a return value",
8881
5
          CG(active_class_entry) != NULL ? "method" : "function");
8882
5
      }
8883
8884
196
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
8885
5
        zend_error_noreturn(E_COMPILE_ERROR,
8886
5
          "A never returning %s does not return a value, but #[\\NoDiscard] requires a return value",
8887
5
          CG(active_class_entry) != NULL ? "method" : "function");
8888
5
      }
8889
196
    }
8890
246
  }
8891
8892
1.01M
  zend_compile_stmt(stmt_ast);
8893
8894
1.01M
  if (is_method) {
8895
60.1k
    CG(zend_lineno) = decl->start_lineno;
8896
60.1k
    zend_check_magic_method_implementation(
8897
60.1k
      CG(active_class_entry), (zend_function *) op_array, lcname, E_COMPILE_ERROR);
8898
953k
  } else if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8899
    /* Only register the function after a successful compile */
8900
14.8k
    if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
8901
100
      CG(zend_lineno) = decl->start_lineno;
8902
100
      do_bind_function_error(lcname, op_array, true);
8903
100
    }
8904
14.8k
  }
8905
8906
  /* put the implicit return on the really last line */
8907
1.01M
  CG(zend_lineno) = decl->end_lineno;
8908
8909
1.01M
  zend_do_extended_stmt(NULL);
8910
1.01M
  zend_emit_final_return(false);
8911
8912
1.01M
  pass_two(CG(active_op_array));
8913
1.01M
  zend_oparray_context_end(&orig_oparray_context);
8914
8915
  /* Pop the loop variable stack separator */
8916
1.01M
  zend_stack_del_top(&CG(loop_var_stack));
8917
8918
1.01M
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8919
14.7k
    zend_observer_function_declared_notify(op_array, lcname);
8920
14.7k
  }
8921
8922
1.01M
  if (lcname != NULL) {
8923
1.00M
    zend_string_release_ex(lcname, 0);
8924
1.00M
  }
8925
8926
1.01M
  CG(active_op_array) = orig_op_array;
8927
1.01M
  CG(active_class_entry) = orig_class_entry;
8928
8929
1.01M
  return op_array;
8930
1.01M
}
8931
8932
static zend_op_array *zend_compile_func_decl(znode *result, zend_ast *ast, enum func_decl_level level)
8933
1.00M
{
8934
1.00M
  return zend_compile_func_decl_ex(result, ast, level, /* property_info */ NULL, (zend_property_hook_kind)-1);
8935
1.00M
}
8936
8937
4.84k
zend_property_hook_kind zend_get_property_hook_kind_from_name(const zend_string *name) {
8938
4.84k
  if (zend_string_equals_literal_ci(name, "get")) {
8939
3.15k
    return ZEND_PROPERTY_HOOK_GET;
8940
3.15k
  } else if (zend_string_equals_literal_ci(name, "set")) {
8941
1.56k
    return ZEND_PROPERTY_HOOK_SET;
8942
1.56k
  } else {
8943
126
    return (zend_property_hook_kind)-1;
8944
126
  }
8945
4.84k
}
8946
8947
static void zend_compile_property_hooks(
8948
    zend_property_info *prop_info, zend_string *prop_name,
8949
    zend_ast *prop_type_ast, const zend_ast_list *hooks)
8950
3.61k
{
8951
3.61k
  zend_class_entry *ce = CG(active_class_entry);
8952
8953
3.61k
  if (prop_info->flags & ZEND_ACC_READONLY) {
8954
15
    zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly");
8955
15
  }
8956
8957
3.60k
  if (hooks->children == 0) {
8958
23
    zend_error_noreturn(E_COMPILE_ERROR, "Property hook list must not be empty");
8959
23
  }
8960
8961
7.70k
  for (uint32_t i = 0; i < hooks->children; i++) {
8962
4.37k
    zend_ast_decl *hook = (zend_ast_decl *) hooks->child[i];
8963
4.37k
    zend_string *name = hook->name;
8964
4.37k
    zend_ast *stmt_ast = hook->child[2];
8965
4.37k
    zend_ast **return_type_ast_ptr = NULL;
8966
4.37k
    zend_ast **value_type_ast_ptr = NULL;
8967
4.37k
    CG(zend_lineno) = hook->start_lineno;
8968
8969
    /* Non-private hooks are always public. This avoids having to copy the hook when inheriting
8970
     * hooks from protected properties to public ones. */
8971
4.37k
    uint32_t hook_visibility = (prop_info->flags & ZEND_ACC_PPP_MASK) != ZEND_ACC_PRIVATE ? ZEND_ACC_PUBLIC : ZEND_ACC_PRIVATE;
8972
4.37k
    hook->flags |= hook_visibility;
8973
8974
4.37k
    if (prop_info->flags & ZEND_ACC_STATIC) {
8975
9
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare hooks for static property");
8976
9
    }
8977
4.36k
    if ((hook->flags & ZEND_ACC_FINAL) && (hook->flags & ZEND_ACC_PRIVATE)) {
8978
5
      zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both final and private");
8979
5
    }
8980
4.35k
    if ((ce->ce_flags & ZEND_ACC_INTERFACE)
8981
4.06k
     || ((prop_info->flags & ZEND_ACC_ABSTRACT) && !stmt_ast)) {
8982
690
      hook->flags |= ZEND_ACC_ABSTRACT;
8983
8984
690
      if (stmt_ast) {
8985
19
        zend_error_noreturn(E_COMPILE_ERROR, "Abstract property hook cannot have body");
8986
19
      }
8987
671
      if (hook->flags & ZEND_ACC_PRIVATE) {
8988
5
        zend_error_noreturn(E_COMPILE_ERROR,
8989
5
          "Property hook cannot be both abstract and private");
8990
5
      }
8991
666
      if (hook->flags & ZEND_ACC_FINAL) {
8992
6
        zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both abstract and final");
8993
6
      }
8994
3.66k
    } else if (!stmt_ast) {
8995
13
      zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract property hook must have a body");
8996
13
    }
8997
8998
4.31k
    zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(name);
8999
4.31k
    if (hook_kind == (zend_property_hook_kind)-1) {
9000
126
      zend_error_noreturn(E_COMPILE_ERROR,
9001
126
        "Unknown hook \"%s\" for property %s::$%s, expected \"get\" or \"set\"",
9002
126
        ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9003
126
    }
9004
9005
4.18k
    if (stmt_ast && stmt_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
9006
1.45k
      stmt_ast = stmt_ast->child[0];
9007
1.45k
      if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
9008
1.19k
        stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
9009
1.19k
      } else {
9010
265
        ZEND_ASSERT(hook_kind == ZEND_PROPERTY_HOOK_SET);
9011
265
        stmt_ast = zend_ast_create(ZEND_AST_ASSIGN,
9012
265
          zend_ast_create(ZEND_AST_PROP,
9013
265
            zend_ast_create(ZEND_AST_VAR, zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_THIS))),
9014
265
            zend_ast_create_zval_from_str(zend_copy_unmangled_prop_name(prop_info->name))),
9015
265
          stmt_ast);
9016
265
      }
9017
1.45k
      stmt_ast = zend_ast_create_list(1, ZEND_AST_STMT_LIST, stmt_ast);
9018
1.45k
      hook->child[2] = stmt_ast;
9019
1.45k
    }
9020
9021
4.18k
    if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
9022
2.71k
      if (hook->child[0]) {
9023
7
        zend_error_noreturn(E_COMPILE_ERROR, "get hook of property %s::$%s must not have a parameter list",
9024
7
          ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9025
7
      }
9026
9027
2.70k
      hook->child[0] = zend_ast_create_list(0, ZEND_AST_PARAM_LIST);
9028
9029
2.70k
      return_type_ast_ptr = &hook->child[3];
9030
2.70k
      *return_type_ast_ptr = prop_type_ast;
9031
2.70k
    } else if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
9032
1.47k
      if (hook->child[0]) {
9033
164
        const zend_ast_list *param_list = zend_ast_get_list(hook->child[0]);
9034
164
        if (param_list->children != 1) {
9035
0
          zend_error_noreturn(E_COMPILE_ERROR, "%s hook of property %s::$%s must accept exactly one parameters",
9036
0
            ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9037
0
        }
9038
164
        const zend_ast *value_param_ast = param_list->child[0];
9039
164
        if (value_param_ast->attr & ZEND_PARAM_REF) {
9040
5
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be pass-by-reference",
9041
5
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9042
5
        }
9043
159
        if (value_param_ast->attr & ZEND_PARAM_VARIADIC) {
9044
5
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be variadic",
9045
5
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9046
5
        }
9047
154
        if (value_param_ast->child[2]) {
9048
5
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not have a default value",
9049
5
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9050
5
        }
9051
149
        if ((prop_type_ast != NULL) != (value_param_ast->child[0] != NULL)) {
9052
5
          zend_hooked_property_variance_error_ex(zend_ast_get_str(value_param_ast->child[1]), ce->name, prop_info->name);
9053
5
        }
9054
1.31k
      } else {
9055
1.31k
        zend_ast *param_name_ast = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VALUE));
9056
1.31k
        zend_ast *param = zend_ast_create(
9057
1.31k
          ZEND_AST_PARAM, prop_type_ast, param_name_ast,
9058
1.31k
          /* expr */ NULL, /* doc_comment */ NULL, /* attributes */ NULL,
9059
1.31k
          /* hooks */ NULL);
9060
1.31k
        value_type_ast_ptr = &param->child[0];
9061
1.31k
        hook->child[0] = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, param);
9062
1.31k
      }
9063
1.45k
      zend_ast *return_type = zend_ast_create(ZEND_AST_TYPE);
9064
1.45k
      return_type->attr = IS_VOID;
9065
1.45k
      hook->child[3] = return_type;
9066
1.45k
    } else {
9067
0
      ZEND_UNREACHABLE();
9068
0
    }
9069
9070
4.16k
    hook->name = zend_strpprintf(0, "$%s::%s", ZSTR_VAL(prop_name), ZSTR_VAL(name));
9071
9072
4.16k
    zend_function *func = (zend_function *) zend_compile_func_decl_ex(
9073
4.16k
      NULL, (zend_ast *) hook, FUNC_DECL_LEVEL_NESTED, prop_info->name, hook_kind);
9074
9075
4.16k
    func->common.prop_info = prop_info;
9076
9077
4.16k
    if (!prop_info->hooks) {
9078
3.32k
      prop_info->hooks = zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE);
9079
3.32k
      memset(prop_info->hooks, 0, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
9080
3.32k
    }
9081
9082
4.16k
    if (prop_info->hooks[hook_kind]) {
9083
27
      zend_error_noreturn(E_COMPILE_ERROR,
9084
27
        "Cannot redeclare property hook \"%s\"", ZSTR_VAL(name));
9085
27
    }
9086
4.13k
    prop_info->hooks[hook_kind] = func;
9087
9088
4.13k
    if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
9089
1.42k
      switch (zend_verify_property_hook_variance(prop_info, func)) {
9090
1.37k
        case INHERITANCE_SUCCESS:
9091
1.37k
          break;
9092
42
        case INHERITANCE_UNRESOLVED:
9093
42
          ce->num_hooked_prop_variance_checks++;
9094
42
          break;
9095
7
        case INHERITANCE_ERROR:
9096
7
          zend_hooked_property_variance_error(prop_info);
9097
0
        case INHERITANCE_WARNING:
9098
0
          ZEND_UNREACHABLE();
9099
1.42k
      }
9100
1.42k
    }
9101
9102
4.12k
    zend_string_release(name);
9103
    /* Un-share type ASTs to avoid double-frees of zval nodes. */
9104
4.12k
    if (return_type_ast_ptr) {
9105
2.64k
      *return_type_ast_ptr = NULL;
9106
2.64k
    }
9107
4.12k
    if (value_type_ast_ptr) {
9108
1.28k
      *value_type_ast_ptr = NULL;
9109
1.28k
    }
9110
4.12k
  }
9111
9112
3.33k
  ce->num_hooked_props++;
9113
9114
  /* See zend_link_hooked_object_iter(). */
9115
3.33k
#ifndef ZEND_OPCACHE_SHM_REATTACHMENT
9116
3.33k
  if (!ce->get_iterator) {
9117
    /* Will be removed again, in case of Iterator or IteratorAggregate. */
9118
2.64k
    ce->get_iterator = zend_hooked_object_get_iterator;
9119
2.64k
  }
9120
3.33k
#endif
9121
9122
3.33k
  if (!prop_info->ce->parent_name) {
9123
2.21k
    zend_verify_hooked_property(ce, prop_info, prop_name);
9124
2.21k
  }
9125
3.33k
}
9126
9127
static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */
9128
28.6k
{
9129
28.6k
  const zend_ast_list *list = zend_ast_get_list(ast);
9130
28.6k
  zend_class_entry *ce = CG(active_class_entry);
9131
28.6k
  uint32_t i, children = list->children;
9132
9133
28.6k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
9134
13
    zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", ZSTR_VAL(ce->name));
9135
13
  }
9136
9137
28.6k
  if ((flags & ZEND_ACC_FINAL) && (flags & ZEND_ACC_PRIVATE)) {
9138
5
    zend_error_noreturn(E_COMPILE_ERROR, "Property cannot be both final and private");
9139
5
  }
9140
9141
28.6k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9142
307
    if (flags & ZEND_ACC_FINAL) {
9143
5
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be final");
9144
5
    }
9145
302
    if (flags & (ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE)) {
9146
5
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be protected or private");
9147
5
    }
9148
297
    if (flags & ZEND_ACC_ABSTRACT) {
9149
5
      zend_error_noreturn(E_COMPILE_ERROR,
9150
5
        "Property in interface cannot be explicitly abstract. "
9151
5
        "All interface members are implicitly abstract");
9152
5
    }
9153
292
    flags |= ZEND_ACC_ABSTRACT;
9154
292
  }
9155
9156
58.7k
  for (i = 0; i < children; ++i) {
9157
30.2k
    zend_property_info *info;
9158
30.2k
    zend_ast *prop_ast = list->child[i];
9159
30.2k
    zend_ast *name_ast = prop_ast->child[0];
9160
30.2k
    zend_ast **value_ast_ptr = &prop_ast->child[1];
9161
30.2k
    zend_ast *doc_comment_ast = prop_ast->child[2];
9162
30.2k
    zend_ast *hooks_ast = prop_ast->child[3];
9163
30.2k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
9164
30.2k
    zend_string *doc_comment = NULL;
9165
30.2k
    zval value_zv;
9166
30.2k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
9167
30.2k
    flags |= zend_property_is_virtual(ce, name, hooks_ast) ? ZEND_ACC_VIRTUAL : 0;
9168
9169
30.2k
    zend_string *old_active_property_info_name = CG(context).active_property_info_name;
9170
30.2k
    CG(context).active_property_info_name = name;
9171
9172
30.2k
    if (!hooks_ast) {
9173
26.7k
      if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9174
2
        zend_error_noreturn(E_COMPILE_ERROR,
9175
2
          "Interfaces may only include hooked properties");
9176
2
      }
9177
26.7k
      if (flags & ZEND_ACC_ABSTRACT) {
9178
5
        zend_error_noreturn(E_COMPILE_ERROR,
9179
5
          "Only hooked properties may be declared abstract");
9180
5
      }
9181
26.7k
    }
9182
30.2k
    if ((flags & ZEND_ACC_ABSTRACT)) {
9183
653
      ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
9184
653
    }
9185
9186
30.2k
    if (type_ast) {
9187
15.4k
      type = zend_compile_typename(type_ast);
9188
9189
15.4k
      if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_NEVER|MAY_BE_CALLABLE)) {
9190
5
        zend_string *str = zend_type_to_string(type);
9191
5
        zend_error_noreturn(E_COMPILE_ERROR,
9192
5
          "Property %s::$%s cannot have type %s",
9193
5
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9194
5
      }
9195
15.4k
    }
9196
9197
    /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */
9198
30.2k
    if (doc_comment_ast) {
9199
373
      doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9200
373
    }
9201
9202
30.2k
    if (zend_hash_exists(&ce->properties_info, name)) {
9203
42
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
9204
42
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
9205
42
    }
9206
9207
30.2k
    if (*value_ast_ptr) {
9208
8.23k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9209
9210
8.23k
      if (ZEND_TYPE_IS_SET(type) && !Z_CONSTANT(value_zv)
9211
2.02k
          && !zend_is_valid_default_value(type, &value_zv)) {
9212
73
        zend_string *str = zend_type_to_string(type);
9213
73
        if (Z_TYPE(value_zv) == IS_NULL && !ZEND_TYPE_IS_INTERSECTION(type)) {
9214
21
          ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
9215
21
          zend_string *nullable_str = zend_type_to_string(type);
9216
9217
21
          zend_error_noreturn(E_COMPILE_ERROR,
9218
21
            "Default value for property of type %s may not be null. "
9219
21
            "Use the nullable type %s to allow null default value",
9220
21
            ZSTR_VAL(str), ZSTR_VAL(nullable_str));
9221
52
        } else {
9222
52
          zend_error_noreturn(E_COMPILE_ERROR,
9223
52
            "Cannot use %s as default value for property %s::$%s of type %s",
9224
52
            zend_zval_value_name(&value_zv),
9225
52
            ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9226
52
        }
9227
73
      }
9228
21.9k
    } else if (!ZEND_TYPE_IS_SET(type) && !hooks_ast) {
9229
7.01k
      ZVAL_NULL(&value_zv);
9230
14.9k
    } else {
9231
14.9k
      ZVAL_UNDEF(&value_zv);
9232
14.9k
    }
9233
9234
30.1k
    if ((ce->ce_flags & ZEND_ACC_READONLY_CLASS)) {
9235
38
      flags |= ZEND_ACC_READONLY;
9236
38
    }
9237
9238
30.1k
    if (flags & ZEND_ACC_READONLY) {
9239
508
      if (!ZEND_TYPE_IS_SET(type)) {
9240
14
        zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
9241
14
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9242
14
      }
9243
494
      if (!Z_ISUNDEF(value_zv)) {
9244
7
        zend_error_noreturn(E_COMPILE_ERROR,
9245
7
          "Readonly property %s::$%s cannot have default value",
9246
7
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9247
7
      }
9248
487
      if (flags & ZEND_ACC_STATIC) {
9249
9
        zend_error_noreturn(E_COMPILE_ERROR,
9250
9
          "Static property %s::$%s cannot be readonly",
9251
9
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9252
9
      }
9253
487
    }
9254
9255
30.1k
    info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type);
9256
9257
30.1k
    if (hooks_ast) {
9258
3.53k
      zend_compile_property_hooks(info, name, type_ast, zend_ast_get_list(hooks_ast));
9259
3.53k
    }
9260
9261
30.1k
    if (attr_ast) {
9262
3.25k
      zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0);
9263
9264
3.25k
      const zend_attribute *override_attribute = zend_get_attribute_str(info->attributes, "override", sizeof("override")-1);
9265
3.25k
      if (override_attribute) {
9266
119
        info->flags |= ZEND_ACC_OVERRIDE;
9267
119
      }
9268
3.25k
    }
9269
9270
30.1k
    CG(context).active_property_info_name = old_active_property_info_name;
9271
30.1k
  }
9272
28.6k
}
9273
/* }}} */
9274
9275
static void zend_compile_prop_group(const zend_ast *ast) /* {{{ */
9276
28.6k
{
9277
28.6k
  zend_ast *type_ast = ast->child[0];
9278
28.6k
  zend_ast *prop_ast = ast->child[1];
9279
28.6k
  zend_ast *attr_ast = ast->child[2];
9280
9281
28.6k
  zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast);
9282
28.6k
}
9283
/* }}} */
9284
9285
static void zend_check_trait_alias_modifiers(uint32_t attr) /* {{{ */
9286
207k
{
9287
207k
  if (attr & ZEND_ACC_STATIC) {
9288
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"static\" as method modifier in trait alias");
9289
207k
  } else if (attr & ZEND_ACC_ABSTRACT) {
9290
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"abstract\" as method modifier in trait alias");
9291
5
  }
9292
207k
}
9293
/* }}} */
9294
9295
static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast, zend_ast *type_ast)
9296
6.69k
{
9297
6.69k
  const zend_ast_list *list = zend_ast_get_list(ast);
9298
6.69k
  zend_class_entry *ce = CG(active_class_entry);
9299
6.69k
  uint32_t i, children = list->children;
9300
9301
13.4k
  for (i = 0; i < children; ++i) {
9302
6.74k
    zend_class_constant *c;
9303
6.74k
    zend_ast *const_ast = list->child[i];
9304
6.74k
    zend_ast *name_ast = const_ast->child[0];
9305
6.74k
    zend_ast **value_ast_ptr = &const_ast->child[1];
9306
6.74k
    zend_ast *doc_comment_ast = const_ast->child[2];
9307
6.74k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
9308
6.74k
    zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
9309
6.74k
    zval value_zv;
9310
6.74k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
9311
9312
6.74k
    if (type_ast) {
9313
2.74k
      type = zend_compile_typename(type_ast);
9314
9315
2.74k
      uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9316
9317
2.74k
      if (type_mask != MAY_BE_ANY && (type_mask & (MAY_BE_CALLABLE|MAY_BE_VOID|MAY_BE_NEVER))) {
9318
5
        zend_string *type_str = zend_type_to_string(type);
9319
9320
5
        zend_error_noreturn(E_COMPILE_ERROR, "Class constant %s::%s cannot have type %s",
9321
5
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9322
5
      }
9323
2.74k
    }
9324
9325
6.74k
    if (UNEXPECTED((flags & ZEND_ACC_PRIVATE) && (flags & ZEND_ACC_FINAL))) {
9326
5
      zend_error_noreturn(
9327
5
        E_COMPILE_ERROR, "Private constant %s::%s cannot be final as it is not visible to other classes",
9328
5
        ZSTR_VAL(ce->name), ZSTR_VAL(name)
9329
5
      );
9330
5
    }
9331
9332
6.73k
    zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9333
9334
6.73k
    if (!Z_CONSTANT(value_zv) && ZEND_TYPE_IS_SET(type) && !zend_is_valid_default_value(type, &value_zv)) {
9335
27
      zend_string *type_str = zend_type_to_string(type);
9336
9337
27
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as value for class constant %s::%s of type %s",
9338
27
        zend_zval_type_name(&value_zv), ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9339
27
    }
9340
9341
6.71k
    c = zend_declare_typed_class_constant(ce, name, &value_zv, flags, doc_comment, type);
9342
9343
6.71k
    if (attr_ast) {
9344
309
      zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9345
9346
309
      const zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9347
9348
309
      if (deprecated) {
9349
164
        ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9350
        /* For deprecated constants, we need to flag the zval for recursion
9351
         * detection. Make sure the zval is separated out of shm. */
9352
164
        ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
9353
164
        ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
9354
164
      }
9355
309
    }
9356
6.71k
  }
9357
6.69k
}
9358
9359
static void zend_compile_class_const_group(const zend_ast *ast) /* {{{ */
9360
6.69k
{
9361
6.69k
  zend_ast *const_ast = ast->child[0];
9362
6.69k
  zend_ast *attr_ast = ast->child[1];
9363
6.69k
  zend_ast *type_ast = ast->child[2];
9364
9365
6.69k
  zend_compile_class_const_decl(const_ast, ast->attr, attr_ast, type_ast);
9366
6.69k
}
9367
/* }}} */
9368
9369
static void zend_compile_method_ref(const zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */
9370
208k
{
9371
208k
  zend_ast *class_ast = ast->child[0];
9372
208k
  zend_ast *method_ast = ast->child[1];
9373
9374
208k
  method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
9375
9376
208k
  if (class_ast) {
9377
1.69k
    method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name");
9378
207k
  } else {
9379
207k
    method_ref->class_name = NULL;
9380
207k
  }
9381
208k
}
9382
/* }}} */
9383
9384
static void zend_compile_trait_precedence(const zend_ast *ast) /* {{{ */
9385
1.12k
{
9386
1.12k
  const zend_ast *method_ref_ast = ast->child[0];
9387
1.12k
  zend_ast *insteadof_ast = ast->child[1];
9388
1.12k
  const zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast);
9389
1.12k
  uint32_t i;
9390
9391
1.12k
  zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*));
9392
1.12k
  zend_compile_method_ref(method_ref_ast, &precedence->trait_method);
9393
1.12k
  precedence->num_excludes = insteadof_list->children;
9394
9395
2.79k
  for (i = 0; i < insteadof_list->children; ++i) {
9396
1.66k
    zend_ast *name_ast = insteadof_list->child[i];
9397
1.66k
    precedence->exclude_class_names[i] =
9398
1.66k
      zend_resolve_const_class_name_reference(name_ast, "trait name");
9399
1.66k
  }
9400
9401
1.12k
  zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
9402
1.12k
}
9403
/* }}} */
9404
9405
static void zend_compile_trait_alias(const zend_ast *ast) /* {{{ */
9406
207k
{
9407
207k
  const zend_ast *method_ref_ast = ast->child[0];
9408
207k
  zend_ast *alias_ast = ast->child[1];
9409
207k
  uint32_t modifiers = ast->attr;
9410
9411
207k
  zend_trait_alias *alias;
9412
9413
207k
  zend_check_trait_alias_modifiers(modifiers);
9414
9415
207k
  alias = emalloc(sizeof(zend_trait_alias));
9416
207k
  zend_compile_method_ref(method_ref_ast, &alias->trait_method);
9417
207k
  alias->modifiers = modifiers;
9418
9419
207k
  if (alias_ast) {
9420
207k
    alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
9421
207k
  } else {
9422
359
    alias->alias = NULL;
9423
359
  }
9424
9425
207k
  zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
9426
207k
}
9427
/* }}} */
9428
9429
static void zend_compile_use_trait(const zend_ast *ast) /* {{{ */
9430
71.3k
{
9431
71.3k
  const zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
9432
71.3k
  zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
9433
71.3k
  zend_class_entry *ce = CG(active_class_entry);
9434
71.3k
  uint32_t i;
9435
9436
71.3k
  ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children));
9437
9438
144k
  for (i = 0; i < traits->children; ++i) {
9439
73.5k
    zend_ast *trait_ast = traits->child[i];
9440
9441
73.5k
    if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9442
5
      zend_string *name = zend_ast_get_str(trait_ast);
9443
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
9444
5
        "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
9445
5
    }
9446
9447
73.5k
    ce->trait_names[ce->num_traits].name =
9448
73.5k
      zend_resolve_const_class_name_reference(trait_ast, "trait name");
9449
73.5k
    ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name);
9450
73.5k
    ce->num_traits++;
9451
73.5k
  }
9452
9453
71.3k
  if (!adaptations) {
9454
1.95k
    return;
9455
1.95k
  }
9456
9457
278k
  for (i = 0; i < adaptations->children; ++i) {
9458
208k
    const zend_ast *adaptation_ast = adaptations->child[i];
9459
208k
    switch (adaptation_ast->kind) {
9460
1.12k
      case ZEND_AST_TRAIT_PRECEDENCE:
9461
1.12k
        zend_compile_trait_precedence(adaptation_ast);
9462
1.12k
        break;
9463
207k
      case ZEND_AST_TRAIT_ALIAS:
9464
207k
        zend_compile_trait_alias(adaptation_ast);
9465
207k
        break;
9466
208k
      EMPTY_SWITCH_DEFAULT_CASE()
9467
208k
    }
9468
208k
  }
9469
69.3k
}
9470
/* }}} */
9471
9472
static void zend_compile_implements(zend_ast *ast) /* {{{ */
9473
8.24k
{
9474
8.24k
  const zend_ast_list *list = zend_ast_get_list(ast);
9475
8.24k
  zend_class_entry *ce = CG(active_class_entry);
9476
8.24k
  zend_class_name *interface_names;
9477
8.24k
  uint32_t i;
9478
9479
8.24k
  interface_names = emalloc(sizeof(zend_class_name) * list->children);
9480
9481
22.9k
  for (i = 0; i < list->children; ++i) {
9482
14.6k
    zend_ast *class_ast = list->child[i];
9483
14.6k
    interface_names[i].name =
9484
14.6k
      zend_resolve_const_class_name_reference(class_ast, "interface name");
9485
14.6k
    interface_names[i].lc_name = zend_string_tolower(interface_names[i].name);
9486
14.6k
  }
9487
9488
8.24k
  ce->num_interfaces = list->children;
9489
8.24k
  ce->interface_names = interface_names;
9490
8.24k
}
9491
/* }}} */
9492
9493
static zend_string *zend_generate_anon_class_name(const zend_ast_decl *decl)
9494
1.83k
{
9495
1.83k
  zend_string *filename = CG(active_op_array)->filename;
9496
1.83k
  uint32_t start_lineno = decl->start_lineno;
9497
9498
  /* Use parent or first interface as prefix. */
9499
1.83k
  zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS);
9500
1.83k
  if (decl->child[0]) {
9501
105
    prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name");
9502
1.73k
  } else if (decl->child[1]) {
9503
620
    const zend_ast_list *list = zend_ast_get_list(decl->child[1]);
9504
620
    prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name");
9505
620
  }
9506
9507
1.83k
  zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32,
9508
1.83k
    ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
9509
1.83k
  zend_string_release(prefix);
9510
1.83k
  return zend_new_interned_string(result);
9511
1.83k
}
9512
9513
static void zend_compile_enum_backing_type(zend_class_entry *ce, zend_ast *enum_backing_type_ast)
9514
643
{
9515
643
  ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM);
9516
643
  zend_type type = zend_compile_typename(enum_backing_type_ast);
9517
643
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9518
643
  if (ZEND_TYPE_IS_COMPLEX(type) || (type_mask != MAY_BE_LONG && type_mask != MAY_BE_STRING)) {
9519
50
    zend_string *type_string = zend_type_to_string(type);
9520
50
    zend_error_noreturn(E_COMPILE_ERROR,
9521
50
      "Enum backing type must be int or string, %s given",
9522
50
      ZSTR_VAL(type_string));
9523
50
  }
9524
593
  if (type_mask == MAY_BE_LONG) {
9525
318
    ce->enum_backing_type = IS_LONG;
9526
318
  } else {
9527
275
    ZEND_ASSERT(type_mask == MAY_BE_STRING);
9528
275
    ce->enum_backing_type = IS_STRING;
9529
274
  }
9530
593
  zend_type_release(type, 0);
9531
592
}
9532
9533
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel) /* {{{ */
9534
172k
{
9535
172k
  const zend_ast_decl *decl = (const zend_ast_decl *) ast;
9536
172k
  zend_ast *extends_ast = decl->child[0];
9537
172k
  zend_ast *implements_ast = decl->child[1];
9538
172k
  zend_ast *stmt_ast = decl->child[2];
9539
172k
  zend_ast *enum_backing_type_ast = decl->child[4];
9540
172k
  zend_string *name, *lcname;
9541
172k
  zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
9542
172k
  zend_op *opline;
9543
9544
172k
  zend_class_entry *original_ce = CG(active_class_entry);
9545
9546
172k
  if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
9547
170k
    zend_string *unqualified_name = decl->name;
9548
9549
170k
    if (CG(active_class_entry)) {
9550
8
      zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
9551
8
    }
9552
9553
170k
    const char *type = "a class name";
9554
170k
    if (decl->flags & ZEND_ACC_ENUM) {
9555
4.64k
      type = "an enum name";
9556
165k
    } else if (decl->flags & ZEND_ACC_INTERFACE) {
9557
7.34k
      type = "an interface name";
9558
158k
    } else if (decl->flags & ZEND_ACC_TRAIT) {
9559
2.44k
      type = "a trait name";
9560
2.44k
    }
9561
170k
    zend_assert_valid_class_name(unqualified_name, type);
9562
170k
    name = zend_prefix_with_ns(unqualified_name);
9563
170k
    name = zend_new_interned_string(name);
9564
170k
    lcname = zend_string_tolower(name);
9565
9566
170k
    if (FC(imports)) {
9567
494
      zend_string *import_name =
9568
494
        zend_hash_find_ptr_lc(FC(imports), unqualified_name);
9569
494
      if (import_name && !zend_string_equals_ci(lcname, import_name)) {
9570
11
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s "
9571
11
            "(previously declared as local import)", ZSTR_VAL(name));
9572
11
      }
9573
494
    }
9574
9575
170k
    zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS);
9576
170k
  } else {
9577
    /* Find an anon class name that is not in use yet. */
9578
1.83k
    name = NULL;
9579
1.83k
    lcname = NULL;
9580
1.83k
    do {
9581
1.83k
      zend_tmp_string_release(name);
9582
1.83k
      zend_tmp_string_release(lcname);
9583
1.83k
      name = zend_generate_anon_class_name(decl);
9584
1.83k
      lcname = zend_string_tolower(name);
9585
1.83k
    } while (zend_hash_exists(CG(class_table), lcname));
9586
1.83k
  }
9587
172k
  lcname = zend_new_interned_string(lcname);
9588
9589
172k
  ce->type = ZEND_USER_CLASS;
9590
172k
  ce->name = name;
9591
172k
  zend_initialize_class_data(ce, true);
9592
172k
  if (!(decl->flags & ZEND_ACC_ANON_CLASS)) {
9593
170k
    zend_alloc_ce_cache(ce->name);
9594
170k
  }
9595
9596
172k
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
9597
0
    ce->ce_flags |= ZEND_ACC_PRELOADED;
9598
0
    ZEND_MAP_PTR_NEW(ce->static_members_table);
9599
0
    ZEND_MAP_PTR_NEW(ce->mutable_data);
9600
0
  }
9601
9602
172k
  ce->ce_flags |= decl->flags;
9603
172k
  ce->info.user.filename = zend_string_copy(zend_get_compiled_filename());
9604
172k
  ce->info.user.line_start = decl->start_lineno;
9605
172k
  ce->info.user.line_end = decl->end_lineno;
9606
9607
172k
  if (decl->doc_comment) {
9608
93
    ce->doc_comment = zend_string_copy(decl->doc_comment);
9609
93
  }
9610
9611
172k
  if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
9612
    /* Serialization is not supported for anonymous classes */
9613
1.83k
    ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
9614
1.83k
  }
9615
9616
172k
  if (extends_ast) {
9617
33.4k
    ce->parent_name =
9618
33.4k
      zend_resolve_const_class_name_reference(extends_ast, "class name");
9619
33.4k
  }
9620
9621
172k
  CG(active_class_entry) = ce;
9622
9623
172k
  if (decl->child[3]) {
9624
2.25k
    zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS, 0);
9625
2.25k
  }
9626
9627
172k
  if (implements_ast) {
9628
8.24k
    zend_compile_implements(implements_ast);
9629
8.24k
  }
9630
9631
172k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
9632
4.61k
    if (enum_backing_type_ast != NULL) {
9633
643
      zend_compile_enum_backing_type(ce, enum_backing_type_ast);
9634
643
    }
9635
4.61k
    zend_enum_add_interfaces(ce);
9636
4.61k
    zend_enum_register_props(ce);
9637
4.61k
  }
9638
9639
172k
  zend_compile_stmt(stmt_ast);
9640
9641
  /* Reset lineno for final opcodes and errors */
9642
172k
  CG(zend_lineno) = ast->lineno;
9643
9644
172k
  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) {
9645
61
    zend_verify_abstract_class(ce);
9646
61
  }
9647
9648
172k
  CG(active_class_entry) = original_ce;
9649
9650
172k
  if (toplevel) {
9651
38.4k
    ce->ce_flags |= ZEND_ACC_TOP_LEVEL;
9652
38.4k
  }
9653
9654
  /* We currently don't early-bind classes that implement interfaces or use traits */
9655
172k
  if (!ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9656
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
9657
   /* See zend_link_hooked_object_iter(). */
9658
   && !ce->num_hooked_props
9659
#endif
9660
86.6k
   && !(CG(compiler_options) & ZEND_COMPILE_WITHOUT_EXECUTION)) {
9661
86.6k
    if (toplevel) {
9662
31.4k
      if (extends_ast) {
9663
8.19k
        zend_class_entry *parent_ce = zend_lookup_class_ex(
9664
8.19k
          ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
9665
9666
8.19k
        if (parent_ce
9667
7.48k
         && !zend_compile_ignore_class(parent_ce, ce->info.user.filename)) {
9668
7.48k
          if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) {
9669
4.55k
            zend_string_release(lcname);
9670
4.55k
            return;
9671
4.55k
          }
9672
7.48k
        }
9673
23.2k
      } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) {
9674
20.1k
        zend_string_release(lcname);
9675
20.1k
        zend_build_properties_info_table(ce);
9676
20.1k
        zend_inheritance_check_override(ce);
9677
20.1k
        ce->ce_flags |= ZEND_ACC_LINKED;
9678
20.1k
        zend_observer_class_linked_notify(ce, lcname);
9679
20.1k
        return;
9680
20.1k
      } else {
9681
3.12k
        goto link_unbound;
9682
3.12k
      }
9683
55.1k
    } else if (!extends_ast) {
9684
33.6k
link_unbound:
9685
      /* Link unbound simple class */
9686
33.6k
      zend_build_properties_info_table(ce);
9687
33.6k
      zend_inheritance_check_override(ce);
9688
33.6k
      ce->ce_flags |= ZEND_ACC_LINKED;
9689
33.6k
    }
9690
86.6k
  }
9691
9692
147k
  opline = get_next_op();
9693
9694
147k
  if (ce->parent_name) {
9695
    /* Lowercased parent name */
9696
27.9k
    zend_string *lc_parent_name = zend_string_tolower(ce->parent_name);
9697
27.9k
    opline->op2_type = IS_CONST;
9698
27.9k
    LITERAL_STR(opline->op2, lc_parent_name);
9699
27.9k
  }
9700
9701
147k
  opline->op1_type = IS_CONST;
9702
  /* It's possible that `lcname` is not an interned string because it was not yet in the interned string table.
9703
   * However, by this point another thread may have caused `lcname` to be added in the interned string table.
9704
   * This will cause `lcname` to get freed once it is found in the interned string table. If we were to use
9705
   * LITERAL_STR() here we would not change the `lcname` pointer to the new value, and it would point to the
9706
   * now-freed string. This will cause issues when we use `lcname` in the code below. We solve this by using
9707
   * zend_add_literal_string() which gives us the new value. */
9708
147k
  opline->op1.constant = zend_add_literal_string(&lcname);
9709
9710
147k
  if (decl->flags & ZEND_ACC_ANON_CLASS) {
9711
1.81k
    opline->opcode = ZEND_DECLARE_ANON_CLASS;
9712
1.81k
    opline->extended_value = zend_alloc_cache_slot();
9713
1.81k
    zend_make_var_result(result, opline);
9714
1.81k
    if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) {
9715
      /* We checked above that the class name is not used. This really shouldn't happen. */
9716
0
      zend_error_noreturn(E_ERROR,
9717
0
        "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name));
9718
0
    }
9719
145k
  } else {
9720
    /* Generate RTD keys until we find one that isn't in use yet. */
9721
145k
    zend_string *key = NULL;
9722
145k
    do {
9723
145k
      zend_tmp_string_release(key);
9724
145k
      key = zend_build_runtime_definition_key(lcname, decl->start_lineno);
9725
145k
    } while (!zend_hash_add_ptr(CG(class_table), key, ce));
9726
9727
    /* RTD key is placed after lcname literal in op1 */
9728
145k
    zend_add_literal_string(&key);
9729
9730
145k
    opline->opcode = ZEND_DECLARE_CLASS;
9731
145k
    if (toplevel
9732
12.8k
       && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING)
9733
        /* We currently don't early-bind classes that implement interfaces or use traits */
9734
5.19k
       && !ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9735
145k
    ) {
9736
1.30k
      if (!extends_ast) {
9737
        /* Use empty string for classes without parents to avoid new handler, and special
9738
         * handling of zend_early_binding. */
9739
850
        opline->op2_type = IS_CONST;
9740
850
        LITERAL_STR(opline->op2, ZSTR_EMPTY_ALLOC());
9741
850
      }
9742
1.30k
      CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING;
9743
1.30k
      opline->opcode = ZEND_DECLARE_CLASS_DELAYED;
9744
1.30k
      opline->extended_value = zend_alloc_cache_slot();
9745
1.30k
      opline->result_type = IS_UNUSED;
9746
1.30k
      opline->result.opline_num = -1;
9747
1.30k
    }
9748
145k
  }
9749
147k
}
9750
/* }}} */
9751
9752
static void zend_compile_enum_case(zend_ast *ast)
9753
6.39k
{
9754
6.39k
  zend_class_entry *enum_class = CG(active_class_entry);
9755
6.39k
  if (!(enum_class->ce_flags & ZEND_ACC_ENUM)) {
9756
7
    zend_error_noreturn(E_COMPILE_ERROR, "Case can only be used in enums");
9757
7
  }
9758
9759
6.38k
  zend_string *enum_case_name = zval_make_interned_string(zend_ast_get_zval(ast->child[0]));
9760
6.38k
  zend_string *enum_class_name = enum_class->name;
9761
9762
6.38k
  zval class_name_zval;
9763
6.38k
  ZVAL_STR_COPY(&class_name_zval, enum_class_name);
9764
6.38k
  zend_ast *class_name_ast = zend_ast_create_zval(&class_name_zval);
9765
9766
6.38k
  zval case_id_zval;
9767
6.38k
  int case_id = zend_enum_next_case_id(enum_class);
9768
6.38k
  ZVAL_LONG(&case_id_zval, case_id);
9769
6.38k
  zend_ast *case_id_ast = zend_ast_create_zval(&case_id_zval);
9770
9771
6.38k
  zval case_name_zval;
9772
6.38k
  ZVAL_STR_COPY(&case_name_zval, enum_case_name);
9773
6.38k
  zend_ast *case_name_ast = zend_ast_create_zval(&case_name_zval);
9774
9775
6.38k
  zend_ast *case_value_ast = ast->child[1];
9776
  // Remove case_value_ast from the original AST to avoid freeing it, as it will be freed by zend_const_expr_to_zval
9777
6.38k
  ast->child[1] = NULL;
9778
6.38k
  if (enum_class->enum_backing_type != IS_UNDEF && case_value_ast == NULL) {
9779
5
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of backed enum %s must have a value",
9780
5
      ZSTR_VAL(enum_case_name),
9781
5
      ZSTR_VAL(enum_class_name));
9782
6.38k
  } else if (enum_class->enum_backing_type == IS_UNDEF && case_value_ast != NULL) {
9783
10
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-backed enum %s must not have a value",
9784
10
      ZSTR_VAL(enum_case_name),
9785
10
      ZSTR_VAL(enum_class_name));
9786
10
  }
9787
9788
6.37k
  zend_ast *const_enum_init_ast = zend_ast_create(ZEND_AST_CONST_ENUM_INIT,
9789
6.37k
      class_name_ast, case_id_ast, case_name_ast, case_value_ast);
9790
9791
6.37k
  zval value_zv;
9792
6.37k
  zend_const_expr_to_zval(&value_zv, &const_enum_init_ast, /* allow_dynamic */ false);
9793
9794
  /* Doc comment has been appended as second last element in ZEND_AST_ENUM ast - attributes are conventionally last */
9795
6.37k
  zend_ast *doc_comment_ast = ast->child[2];
9796
6.37k
  zend_string *doc_comment = NULL;
9797
6.37k
  if (doc_comment_ast) {
9798
3.89k
    doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9799
3.89k
  }
9800
9801
6.37k
  zend_class_constant *c = zend_declare_class_constant_ex(enum_class, enum_case_name, &value_zv, ZEND_ACC_PUBLIC, doc_comment);
9802
6.37k
  ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE;
9803
6.37k
  zend_ast_destroy(const_enum_init_ast);
9804
9805
6.37k
  zend_ast *attr_ast = ast->child[3];
9806
6.37k
  if (attr_ast) {
9807
122
    zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9808
9809
122
    zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9810
9811
122
    if (deprecated) {
9812
42
      ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9813
42
    }
9814
122
  }
9815
6.37k
}
9816
9817
static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
9818
2.27k
{
9819
2.27k
  switch (type) {
9820
1.29k
    case ZEND_SYMBOL_CLASS:
9821
1.29k
      if (!FC(imports)) {
9822
906
        FC(imports) = emalloc(sizeof(HashTable));
9823
906
        zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
9824
906
      }
9825
1.29k
      return FC(imports);
9826
595
    case ZEND_SYMBOL_FUNCTION:
9827
595
      if (!FC(imports_function)) {
9828
486
        FC(imports_function) = emalloc(sizeof(HashTable));
9829
486
        zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
9830
486
      }
9831
595
      return FC(imports_function);
9832
385
    case ZEND_SYMBOL_CONST:
9833
385
      if (!FC(imports_const)) {
9834
317
        FC(imports_const) = emalloc(sizeof(HashTable));
9835
317
        zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
9836
317
      }
9837
385
      return FC(imports_const);
9838
2.27k
    EMPTY_SWITCH_DEFAULT_CASE()
9839
2.27k
  }
9840
9841
0
  return NULL;
9842
2.27k
}
9843
/* }}} */
9844
9845
static char *zend_get_use_type_str(uint32_t type) /* {{{ */
9846
67
{
9847
67
  switch (type) {
9848
37
    case ZEND_SYMBOL_CLASS:
9849
37
      return "";
9850
15
    case ZEND_SYMBOL_FUNCTION:
9851
15
      return " function";
9852
15
    case ZEND_SYMBOL_CONST:
9853
15
      return " const";
9854
67
    EMPTY_SWITCH_DEFAULT_CASE()
9855
67
  }
9856
9857
0
  return " unknown";
9858
67
}
9859
/* }}} */
9860
9861
static void zend_check_already_in_use(uint32_t type, const zend_string *old_name, const zend_string *new_name, const zend_string *check_name) /* {{{ */
9862
46
{
9863
46
  if (zend_string_equals_ci(old_name, check_name)) {
9864
21
    return;
9865
21
  }
9866
9867
25
  zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9868
25
    "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9869
46
}
9870
/* }}} */
9871
9872
static void zend_compile_use(zend_ast *ast) /* {{{ */
9873
2.27k
{
9874
2.27k
  const zend_ast_list *list = zend_ast_get_list(ast);
9875
2.27k
  uint32_t i;
9876
2.27k
  zend_string *current_ns = FC(current_namespace);
9877
2.27k
  uint32_t type = ast->attr;
9878
2.27k
  HashTable *current_import = zend_get_import_ht(type);
9879
2.27k
  bool case_sensitive = type == ZEND_SYMBOL_CONST;
9880
9881
4.72k
  for (i = 0; i < list->children; ++i) {
9882
2.51k
    const zend_ast *use_ast = list->child[i];
9883
2.51k
    zend_ast *old_name_ast = use_ast->child[0];
9884
2.51k
    zend_ast *new_name_ast = use_ast->child[1];
9885
2.51k
    zend_string *old_name = zend_ast_get_str(old_name_ast);
9886
2.51k
    zend_string *new_name, *lookup_name;
9887
9888
2.51k
    if (new_name_ast) {
9889
587
      new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
9890
1.92k
    } else {
9891
1.92k
      const char *unqualified_name;
9892
1.92k
      size_t unqualified_name_len;
9893
1.92k
      if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
9894
        /* The form "use A\B" is equivalent to "use A\B as B" */
9895
891
        new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
9896
1.03k
      } else {
9897
1.03k
        new_name = zend_string_copy(old_name);
9898
9899
1.03k
        if (!current_ns) {
9900
561
          zend_error(E_WARNING, "The use statement with non-compound name '%s' "
9901
561
            "has no effect", ZSTR_VAL(new_name));
9902
561
        }
9903
1.03k
      }
9904
1.92k
    }
9905
9906
2.51k
    if (case_sensitive) {
9907
469
      lookup_name = zend_string_copy(new_name);
9908
2.04k
    } else {
9909
2.04k
      lookup_name = zend_string_tolower(new_name);
9910
2.04k
    }
9911
9912
2.51k
    if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) {
9913
25
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
9914
25
        "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
9915
25
    }
9916
9917
2.49k
    if (current_ns) {
9918
1.32k
      zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
9919
1.32k
      zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
9920
1.32k
      ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
9921
1.32k
      memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1);
9922
9923
1.32k
      if (zend_have_seen_symbol(ns_name, type)) {
9924
17
        zend_check_already_in_use(type, old_name, new_name, ns_name);
9925
17
      }
9926
9927
1.32k
      zend_string_efree(ns_name);
9928
1.32k
    } else if (zend_have_seen_symbol(lookup_name, type)) {
9929
29
      zend_check_already_in_use(type, old_name, new_name, lookup_name);
9930
29
    }
9931
9932
2.49k
    zend_string_addref(old_name);
9933
2.49k
    old_name = zend_new_interned_string(old_name);
9934
2.49k
    if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
9935
42
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9936
42
        "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9937
42
    }
9938
9939
2.44k
    zend_string_release_ex(lookup_name, 0);
9940
2.44k
    zend_string_release_ex(new_name, 0);
9941
2.44k
  }
9942
2.27k
}
9943
/* }}} */
9944
9945
static void zend_compile_group_use(const zend_ast *ast) /* {{{ */
9946
242
{
9947
242
  uint32_t i;
9948
242
  const zend_string *ns = zend_ast_get_str(ast->child[0]);
9949
242
  const zend_ast_list *list = zend_ast_get_list(ast->child[1]);
9950
9951
943
  for (i = 0; i < list->children; i++) {
9952
701
    zend_ast *inline_use, *use = list->child[i];
9953
701
    zval *name_zval = zend_ast_get_zval(use->child[0]);
9954
701
    zend_string *name = Z_STR_P(name_zval);
9955
701
    zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
9956
701
    zend_string_release_ex(name, 0);
9957
701
    ZVAL_STR(name_zval, compound_ns);
9958
701
    inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
9959
701
    inline_use->attr = ast->attr ? ast->attr : use->attr;
9960
701
    zend_compile_use(inline_use);
9961
701
  }
9962
242
}
9963
/* }}} */
9964
9965
static void zend_compile_const_decl(zend_ast *ast) /* {{{ */
9966
4.12k
{
9967
4.12k
  zend_ast_list *list = zend_ast_get_list(ast);
9968
4.12k
  uint32_t i;
9969
4.12k
  zend_ast *attributes_ast = NULL;
9970
4.12k
  zend_op *last_op = NULL;
9971
9.26k
  for (i = 0; i < list->children; ++i) {
9972
5.15k
    zend_ast *const_ast = list->child[i];
9973
5.15k
    if (const_ast->kind == ZEND_AST_ATTRIBUTE_LIST) {
9974
681
      ZEND_ASSERT(i == list->children - 1);
9975
681
      attributes_ast = const_ast;
9976
681
      continue;
9977
681
    }
9978
4.47k
    ZEND_ASSERT(const_ast->kind == ZEND_AST_CONST_ELEM);
9979
4.47k
    zend_ast *name_ast = const_ast->child[0];
9980
4.47k
    zend_ast **value_ast_ptr = &const_ast->child[1];
9981
4.47k
    zend_string *unqualified_name = zend_ast_get_str(name_ast);
9982
9983
4.47k
    zend_string *name;
9984
4.47k
    znode name_node, value_node;
9985
4.47k
    zval *value_zv = &value_node.u.constant;
9986
9987
4.47k
    value_node.op_type = IS_CONST;
9988
4.47k
    zend_const_expr_to_zval(value_zv, value_ast_ptr, /* allow_dynamic */ true);
9989
9990
4.47k
    if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) {
9991
6
      zend_error_noreturn(E_COMPILE_ERROR,
9992
6
        "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name));
9993
6
    }
9994
9995
4.46k
    name = zend_prefix_with_ns(unqualified_name);
9996
4.46k
    name = zend_new_interned_string(name);
9997
9998
4.46k
    if (FC(imports_const)) {
9999
807
      zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name);
10000
807
      if (import_name && !zend_string_equals(import_name, name)) {
10001
9
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because "
10002
9
          "the name is already in use", ZSTR_VAL(name));
10003
9
      }
10004
807
    }
10005
10006
4.46k
    name_node.op_type = IS_CONST;
10007
4.46k
    ZVAL_STR(&name_node.u.constant, name);
10008
10009
4.46k
    last_op = zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
10010
10011
4.46k
    zend_register_seen_symbol(name, ZEND_SYMBOL_CONST);
10012
4.46k
  }
10013
4.11k
  if (attributes_ast == NULL) {
10014
3.33k
    return;
10015
3.33k
  }
10016
  /* Validate: attributes can only be applied to one constant at a time
10017
   * Since we store the AST for the attributes in the list of children,
10018
   * there should be exactly 2 children. */
10019
779
  if (list->children > 2) {
10020
9
    zend_error_noreturn(
10021
9
      E_COMPILE_ERROR,
10022
9
      "Cannot apply attributes to multiple constants at once"
10023
9
    );
10024
9
  }
10025
10026
770
  HashTable *attributes = NULL;
10027
770
  zend_compile_attributes(&attributes, list->child[1], 0, ZEND_ATTRIBUTE_TARGET_CONST, 0);
10028
10029
770
  ZEND_ASSERT(last_op != NULL);
10030
770
  last_op->opcode = ZEND_DECLARE_ATTRIBUTED_CONST;
10031
664
  znode attribs_node;
10032
664
  attribs_node.op_type = IS_CONST;
10033
664
  ZVAL_PTR(&attribs_node.u.constant, attributes);
10034
664
  zend_emit_op_data(&attribs_node);
10035
664
  CG(active_op_array)->fn_flags |= ZEND_ACC_PTR_OPS;
10036
664
}
10037
/* }}}*/
10038
10039
static void zend_compile_namespace(const zend_ast *ast) /* {{{ */
10040
4.74k
{
10041
4.74k
  zend_ast *name_ast = ast->child[0];
10042
4.74k
  zend_ast *stmt_ast = ast->child[1];
10043
4.74k
  zend_string *name;
10044
4.74k
  bool with_bracket = stmt_ast != NULL;
10045
10046
  /* handle mixed syntax declaration or nested namespaces */
10047
4.74k
  if (!FC(has_bracketed_namespaces)) {
10048
3.62k
    if (FC(current_namespace)) {
10049
      /* previous namespace declarations were unbracketed */
10050
915
      if (with_bracket) {
10051
7
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
10052
7
          "with unbracketed namespace declarations");
10053
7
      }
10054
915
    }
10055
3.62k
  } else {
10056
    /* previous namespace declarations were bracketed */
10057
1.11k
    if (!with_bracket) {
10058
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
10059
7
        "with unbracketed namespace declarations");
10060
1.10k
    } else if (FC(current_namespace) || FC(in_namespace)) {
10061
6
      zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
10062
6
    }
10063
1.11k
  }
10064
10065
4.74k
  bool is_first_namespace = (!with_bracket && !FC(current_namespace))
10066
2.56k
    || (with_bracket && !FC(has_bracketed_namespaces));
10067
4.72k
  if (is_first_namespace && FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
10068
34
    zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
10069
34
      "the very first statement or after any declare call in the script");
10070
34
  }
10071
10072
4.68k
  if (FC(current_namespace)) {
10073
908
    zend_string_release_ex(FC(current_namespace), 0);
10074
908
  }
10075
10076
4.68k
  if (name_ast) {
10077
4.01k
    name = zend_ast_get_str(name_ast);
10078
10079
4.01k
    if (zend_string_equals_literal_ci(name, "namespace")) {
10080
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name));
10081
5
    }
10082
10083
4.01k
    FC(current_namespace) = zend_string_copy(name);
10084
4.01k
  } else {
10085
667
    FC(current_namespace) = NULL;
10086
667
  }
10087
10088
4.68k
  zend_reset_import_tables();
10089
10090
4.68k
  FC(in_namespace) = 1;
10091
4.68k
  if (with_bracket) {
10092
1.64k
    FC(has_bracketed_namespaces) = 1;
10093
1.64k
  }
10094
10095
4.68k
  if (stmt_ast) {
10096
1.64k
    zend_compile_top_stmt(stmt_ast);
10097
1.64k
    zend_end_namespace();
10098
1.64k
  }
10099
4.68k
}
10100
/* }}} */
10101
10102
static void zend_compile_halt_compiler(const zend_ast *ast) /* {{{ */
10103
70
{
10104
70
  zend_ast *offset_ast = ast->child[0];
10105
70
  zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
10106
10107
70
  const char const_name[] = "__COMPILER_HALT_OFFSET__";
10108
10109
70
  if (FC(has_bracketed_namespaces) && FC(in_namespace)) {
10110
0
    zend_error_noreturn(E_COMPILE_ERROR,
10111
0
      "__HALT_COMPILER() can only be used from the outermost scope");
10112
0
  }
10113
10114
70
  const zend_string *filename = zend_get_compiled_filename();
10115
70
  zend_string *name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
10116
70
    ZSTR_VAL(filename), ZSTR_LEN(filename), false);
10117
10118
  /* Avoid repeated declaration of the __COMPILER_HALT_OFFSET__ constant in
10119
   * case this file was already included. */
10120
70
  if (!zend_hash_find(EG(zend_constants), name)) {
10121
70
    zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0);
10122
70
  }
10123
70
  zend_string_release_ex(name, 0);
10124
70
}
10125
/* }}} */
10126
10127
static bool zend_try_ct_eval_magic_const(zval *zv, const zend_ast *ast) /* {{{ */
10128
19.2k
{
10129
19.2k
  const zend_op_array *op_array = CG(active_op_array);
10130
19.2k
  const zend_class_entry *ce = CG(active_class_entry);
10131
10132
19.2k
  switch (ast->attr) {
10133
158
    case T_LINE:
10134
158
      ZVAL_LONG(zv, ast->lineno);
10135
158
      break;
10136
4.53k
    case T_FILE:
10137
4.53k
      ZVAL_STR_COPY(zv, CG(compiled_filename));
10138
4.53k
      break;
10139
1.43k
    case T_DIR:
10140
1.43k
    {
10141
1.43k
      const zend_string *filename = CG(compiled_filename);
10142
1.43k
      zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
10143
#ifdef ZEND_WIN32
10144
      ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
10145
#else
10146
1.43k
      ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
10147
1.43k
#endif
10148
10149
1.43k
      if (zend_string_equals_literal(dirname, ".")) {
10150
961
        dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
10151
961
#ifdef HAVE_GETCWD
10152
961
        ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN));
10153
#elif defined(HAVE_GETWD)
10154
        ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname)));
10155
#endif
10156
961
        ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname));
10157
961
      }
10158
10159
1.43k
      ZVAL_STR(zv, dirname);
10160
1.43k
      break;
10161
0
    }
10162
1.76k
    case T_FUNC_C:
10163
1.76k
      if (op_array && op_array->function_name) {
10164
1.55k
        ZVAL_STR_COPY(zv, op_array->function_name);
10165
1.55k
      } else {
10166
216
        ZVAL_EMPTY_STRING(zv);
10167
216
      }
10168
1.76k
      break;
10169
265
    case T_PROPERTY_C: {
10170
265
      zend_string *prop_info_name = CG(context).active_property_info_name;
10171
265
      if (prop_info_name) {
10172
216
        ZVAL_STR(zv, zend_copy_unmangled_prop_name(prop_info_name));
10173
216
      } else {
10174
49
        ZVAL_EMPTY_STRING(zv);
10175
49
      }
10176
265
      break;
10177
0
    }
10178
4.32k
    case T_METHOD_C:
10179
      /* Detect whether we are directly inside a class (e.g. a class constant) and treat
10180
       * this as not being inside a function. */
10181
4.32k
      if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
10182
200
        op_array = NULL;
10183
200
      }
10184
4.32k
      if (op_array && op_array->function_name) {
10185
3.75k
        if (op_array->scope) {
10186
3.11k
          ZVAL_NEW_STR(zv,
10187
3.11k
            zend_create_member_string(op_array->scope->name, op_array->function_name));
10188
3.11k
        } else {
10189
642
          ZVAL_STR_COPY(zv, op_array->function_name);
10190
642
        }
10191
3.75k
      } else {
10192
575
        ZVAL_EMPTY_STRING(zv);
10193
575
      }
10194
4.32k
      break;
10195
4.16k
    case T_CLASS_C:
10196
4.16k
      if (ce) {
10197
2.81k
        if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10198
1.97k
          return 0;
10199
1.97k
        } else {
10200
839
          ZVAL_STR_COPY(zv, ce->name);
10201
839
        }
10202
2.81k
      } else {
10203
1.34k
        ZVAL_EMPTY_STRING(zv);
10204
1.34k
      }
10205
2.18k
      break;
10206
2.18k
    case T_TRAIT_C:
10207
1.91k
      if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10208
174
        ZVAL_STR_COPY(zv, ce->name);
10209
1.74k
      } else {
10210
1.74k
        ZVAL_EMPTY_STRING(zv);
10211
1.74k
      }
10212
1.91k
      break;
10213
701
    case T_NS_C:
10214
701
      if (FC(current_namespace)) {
10215
457
        ZVAL_STR_COPY(zv, FC(current_namespace));
10216
457
      } else {
10217
244
        ZVAL_EMPTY_STRING(zv);
10218
244
      }
10219
701
      break;
10220
19.2k
    EMPTY_SWITCH_DEFAULT_CASE()
10221
19.2k
  }
10222
10223
17.2k
  return 1;
10224
19.2k
}
10225
/* }}} */
10226
10227
ZEND_API bool zend_is_op_long_compatible(const zval *op)
10228
69.2k
{
10229
69.2k
  if (Z_TYPE_P(op) == IS_ARRAY) {
10230
250
    return false;
10231
250
  }
10232
10233
69.0k
  if (Z_TYPE_P(op) == IS_DOUBLE
10234
17.4k
    && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval_silent(Z_DVAL_P(op)))) {
10235
12.7k
    return false;
10236
12.7k
  }
10237
10238
56.3k
  if (Z_TYPE_P(op) == IS_STRING) {
10239
11.8k
    double dval = 0;
10240
11.8k
    uint8_t is_num = is_numeric_str_function(Z_STR_P(op), NULL, &dval);
10241
11.8k
    if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval_silent(dval)))) {
10242
3.68k
      return false;
10243
3.68k
    }
10244
11.8k
  }
10245
10246
52.6k
  return true;
10247
56.3k
}
10248
10249
ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, const zval *op2) /* {{{ */
10250
833k
{
10251
833k
  if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) {
10252
    /* Array to string warning. */
10253
33.5k
    return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY;
10254
33.5k
  }
10255
10256
799k
  if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV
10257
703k
               || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR
10258
658k
               || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) {
10259
    /* Only the numeric operations throw errors. */
10260
618k
    return 0;
10261
618k
  }
10262
10263
181k
  if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) {
10264
10.3k
    if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) {
10265
      /* Adding two arrays is allowed. */
10266
4.25k
      return 0;
10267
4.25k
    }
10268
10269
    /* Numeric operators throw when one of the operands is an array. */
10270
6.12k
    return 1;
10271
10.3k
  }
10272
10273
  /* While basic arithmetic operators always produce numeric string errors,
10274
   * bitwise operators don't produce errors if both operands are strings */
10275
170k
  if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)
10276
40.1k
    && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
10277
20.0k
    return 0;
10278
20.0k
  }
10279
10280
150k
  if (Z_TYPE_P(op1) == IS_STRING
10281
37.9k
    && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) {
10282
22.8k
    return 1;
10283
22.8k
  }
10284
10285
127k
  if (Z_TYPE_P(op2) == IS_STRING
10286
23.3k
    && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) {
10287
15.1k
    return 1;
10288
15.1k
  }
10289
10290
  /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */
10291
112k
  if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR
10292
105k
      || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) {
10293
23.1k
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2)) {
10294
8.03k
      return 1;
10295
8.03k
    }
10296
23.1k
  }
10297
10298
104k
  if (opcode == ZEND_DIV && zval_get_double(op2) == 0.0) {
10299
    /* Division by zero throws an error. */
10300
1.06k
    return 1;
10301
1.06k
  }
10302
10303
  /* Mod is an operation that will cast float/float-strings to integers which might
10304
     produce float to int incompatible errors, and also cannot be divided by 0 */
10305
103k
  if (opcode == ZEND_MOD) {
10306
13.7k
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2) || zval_get_long(op2) == 0) {
10307
10.8k
      return 1;
10308
10.8k
    }
10309
13.7k
  }
10310
10311
92.8k
  if ((opcode == ZEND_POW) && zval_get_double(op1) == 0 && zval_get_double(op2) < 0) {
10312
    /* 0 ** (<0) throws a division by zero error. */
10313
81
    return 1;
10314
81
  }
10315
92.7k
  if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) {
10316
    /* Shift by negative number throws an error. */
10317
241
    return 1;
10318
241
  }
10319
10320
92.5k
  return 0;
10321
92.7k
}
10322
/* }}} */
10323
10324
static inline bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
10325
782k
{
10326
782k
  if (zend_binary_op_produces_error(opcode, op1, op2)) {
10327
41.1k
    return 0;
10328
41.1k
  }
10329
10330
741k
  const binary_op_type fn = get_binary_op(opcode);
10331
741k
  fn(result, op1, op2);
10332
741k
  return 1;
10333
782k
}
10334
/* }}} */
10335
10336
ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op)
10337
360k
{
10338
360k
  if (opcode == ZEND_BW_NOT) {
10339
    /* BW_NOT on string does not convert the string into an integer. */
10340
4.43k
    if (Z_TYPE_P(op) == IS_STRING) {
10341
898
      return 0;
10342
898
    }
10343
3.53k
    return Z_TYPE_P(op) <= IS_TRUE || !zend_is_op_long_compatible(op);
10344
4.43k
  }
10345
  /* Can happen when called from zend_optimizer_eval_unary_op() */
10346
356k
  if (opcode == ZEND_BOOL || opcode == ZEND_BOOL_NOT) {
10347
    /* ZEND_BOOL/ZEND_BOOL_NOT warns when casting NAN. */
10348
356k
    return Z_TYPE_P(op) == IS_DOUBLE;
10349
356k
  }
10350
10351
0
  return 0;
10352
356k
}
10353
10354
static inline bool zend_try_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */
10355
360k
{
10356
360k
  if (zend_unary_op_produces_error(opcode, op)) {
10357
986
    return 0;
10358
986
  }
10359
10360
359k
  const unary_op_type fn = get_unary_op(opcode);
10361
359k
  fn(result, op);
10362
359k
  return 1;
10363
360k
}
10364
/* }}} */
10365
10366
static inline bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
10367
40.9k
{
10368
40.9k
  zval right;
10369
40.9k
  ZVAL_LONG(&right, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10370
40.9k
  return zend_try_ct_eval_binary_op(result, ZEND_MUL, op, &right);
10371
40.9k
}
10372
/* }}} */
10373
10374
static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
10375
8.01k
{
10376
8.01k
  const binary_op_type fn = kind == ZEND_AST_GREATER
10377
8.01k
    ? is_smaller_function : is_smaller_or_equal_function;
10378
8.01k
  fn(result, op2, op1);
10379
8.01k
}
10380
/* }}} */
10381
10382
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
10383
2.02M
{
10384
2.02M
  const zend_ast_list *list = zend_ast_get_list(ast);
10385
2.02M
  zend_ast *last_elem_ast = NULL;
10386
2.02M
  uint32_t i;
10387
2.02M
  bool is_constant = true;
10388
10389
2.02M
  if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
10390
5
    zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
10391
5
  }
10392
10393
  /* First ensure that *all* child nodes are constant and by-val */
10394
4.42M
  for (i = 0; i < list->children; ++i) {
10395
2.39M
    zend_ast *elem_ast = list->child[i];
10396
10397
2.39M
    if (elem_ast == NULL) {
10398
      /* Report error at line of last non-empty element */
10399
99
      if (last_elem_ast) {
10400
63
        CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast);
10401
63
      }
10402
99
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
10403
99
    }
10404
10405
2.39M
    if (elem_ast->kind != ZEND_AST_UNPACK) {
10406
2.26M
      zend_eval_const_expr(&elem_ast->child[0]);
10407
2.26M
      zend_eval_const_expr(&elem_ast->child[1]);
10408
10409
2.26M
      if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL
10410
384k
        || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
10411
2.26M
      ) {
10412
1.88M
        is_constant = false;
10413
1.88M
      }
10414
2.26M
    } else {
10415
132k
      zend_eval_const_expr(&elem_ast->child[0]);
10416
10417
132k
      if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) {
10418
131k
        is_constant = false;
10419
131k
      }
10420
132k
    }
10421
10422
2.39M
    last_elem_ast = elem_ast;
10423
2.39M
  }
10424
10425
2.02M
  if (!is_constant) {
10426
1.93M
    return 0;
10427
1.93M
  }
10428
10429
86.9k
  if (!list->children) {
10430
22.5k
    ZVAL_EMPTY_ARRAY(result);
10431
22.5k
    return 1;
10432
22.5k
  }
10433
10434
64.3k
  array_init_size(result, list->children);
10435
387k
  for (i = 0; i < list->children; ++i) {
10436
325k
    const zend_ast *elem_ast = list->child[i];
10437
325k
    zend_ast *value_ast = elem_ast->child[0];
10438
325k
    zend_ast *key_ast;
10439
10440
325k
    zval *value = zend_ast_get_zval(value_ast);
10441
325k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
10442
1.33k
      if (Z_TYPE_P(value) == IS_ARRAY) {
10443
1.33k
        const HashTable *ht = Z_ARRVAL_P(value);
10444
1.33k
        zval *val;
10445
1.33k
        zend_string *key;
10446
10447
5.17k
        ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
10448
5.17k
          if (key) {
10449
187
            zend_hash_update(Z_ARRVAL_P(result), key, val);
10450
728
          } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
10451
14
            zval_ptr_dtor(result);
10452
14
            return 0;
10453
14
          }
10454
901
          Z_TRY_ADDREF_P(val);
10455
901
        } ZEND_HASH_FOREACH_END();
10456
10457
1.31k
        continue;
10458
1.33k
      } else {
10459
7
        zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked, %s given", zend_zval_value_name(value));
10460
7
      }
10461
1.33k
    }
10462
10463
324k
    Z_TRY_ADDREF_P(value);
10464
10465
324k
    key_ast = elem_ast->child[1];
10466
324k
    if (key_ast) {
10467
17.2k
      const zval *key = zend_ast_get_zval(key_ast);
10468
17.2k
      switch (Z_TYPE_P(key)) {
10469
6.13k
        case IS_LONG:
10470
6.13k
          zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value);
10471
6.13k
          break;
10472
8.35k
        case IS_STRING:
10473
8.35k
          zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value);
10474
8.35k
          break;
10475
2.26k
        case IS_DOUBLE: {
10476
2.26k
          zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key));
10477
          /* Incompatible float will generate an error, leave this to run-time */
10478
2.26k
          if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) {
10479
1.72k
            goto fail;
10480
1.72k
          }
10481
535
          zend_hash_index_update(Z_ARRVAL_P(result), lval, value);
10482
535
          break;
10483
2.26k
        }
10484
279
        case IS_FALSE:
10485
279
          zend_hash_index_update(Z_ARRVAL_P(result), 0, value);
10486
279
          break;
10487
70
        case IS_TRUE:
10488
70
          zend_hash_index_update(Z_ARRVAL_P(result), 1, value);
10489
70
          break;
10490
102
        case IS_NULL:
10491
          /* Null key will generate a warning at run-time. */
10492
102
          goto fail;
10493
3
        default:
10494
3
          zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type");
10495
17.2k
      }
10496
306k
    } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) {
10497
1.89k
fail:
10498
1.89k
      zval_ptr_dtor_nogc(value);
10499
1.89k
      zval_ptr_dtor(result);
10500
1.89k
      return 0;
10501
66
    }
10502
324k
  }
10503
10504
62.4k
  return 1;
10505
64.3k
}
10506
/* }}} */
10507
10508
static void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */
10509
2.91M
{
10510
2.91M
  zend_ast *left_ast = ast->child[0];
10511
2.91M
  zend_ast *right_ast = ast->child[1];
10512
2.91M
  uint32_t opcode = ast->attr;
10513
10514
2.91M
  znode left_node, right_node;
10515
10516
2.91M
  zend_compile_expr(&left_node, left_ast);
10517
2.91M
  zend_compile_expr(&right_node, right_ast);
10518
10519
2.91M
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10520
705k
    if (zend_try_ct_eval_binary_op(&result->u.constant, opcode,
10521
705k
        &left_node.u.constant, &right_node.u.constant)
10522
705k
    ) {
10523
691k
      result->op_type = IS_CONST;
10524
691k
      zval_ptr_dtor(&left_node.u.constant);
10525
691k
      zval_ptr_dtor(&right_node.u.constant);
10526
691k
      return;
10527
691k
    }
10528
705k
  }
10529
10530
2.22M
  do {
10531
2.22M
    if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) {
10532
      /* 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) */
10533
40.5k
      if (left_node.op_type == IS_CONST) {
10534
911
        if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) {
10535
412
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL);
10536
412
          opline->extended_value =
10537
412
            (opcode == ZEND_IS_IDENTICAL) ?
10538
281
              (1 << Z_TYPE(left_node.u.constant)) :
10539
412
              (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant)));
10540
412
          return;
10541
412
        }
10542
39.6k
      } else if (right_node.op_type == IS_CONST) {
10543
37.5k
        if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) {
10544
36.1k
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL);
10545
36.1k
          opline->extended_value =
10546
36.1k
            (opcode == ZEND_IS_IDENTICAL) ?
10547
14.5k
              (1 << Z_TYPE(right_node.u.constant)) :
10548
36.1k
              (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant)));
10549
36.1k
          return;
10550
36.1k
        }
10551
37.5k
      }
10552
2.18M
    } else if (opcode == ZEND_CONCAT) {
10553
      /* convert constant operands to strings at compile-time */
10554
98.3k
      if (left_node.op_type == IS_CONST) {
10555
15.3k
        if (Z_TYPE(left_node.u.constant) == IS_ARRAY) {
10556
166
          zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING;
10557
15.2k
        } else {
10558
15.2k
          convert_to_string(&left_node.u.constant);
10559
15.2k
        }
10560
15.3k
      }
10561
98.3k
      if (right_node.op_type == IS_CONST) {
10562
23.0k
        if (Z_TYPE(right_node.u.constant) == IS_ARRAY) {
10563
320
          zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING;
10564
22.7k
        } else {
10565
22.7k
          convert_to_string(&right_node.u.constant);
10566
22.7k
        }
10567
23.0k
      }
10568
98.3k
      if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10569
0
        opcode = ZEND_FAST_CONCAT;
10570
0
      }
10571
98.3k
    }
10572
2.18M
    zend_emit_op_tmp(result, opcode, &left_node, &right_node);
10573
2.18M
  } while (0);
10574
2.22M
}
10575
/* }}} */
10576
10577
/* We do not use zend_compile_binary_op for this because we want to retain the left-to-right
10578
 * evaluation order. */
10579
static void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */
10580
255k
{
10581
255k
  zend_ast *left_ast = ast->child[0];
10582
255k
  zend_ast *right_ast = ast->child[1];
10583
255k
  znode left_node, right_node;
10584
10585
255k
  ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL);
10586
10587
255k
  zend_compile_expr(&left_node, left_ast);
10588
255k
  zend_compile_expr(&right_node, right_ast);
10589
10590
255k
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10591
6.45k
    result->op_type = IS_CONST;
10592
6.45k
    zend_ct_eval_greater(&result->u.constant, ast->kind,
10593
6.45k
      &left_node.u.constant, &right_node.u.constant);
10594
6.45k
    zval_ptr_dtor(&left_node.u.constant);
10595
6.45k
    zval_ptr_dtor(&right_node.u.constant);
10596
6.45k
    return;
10597
6.45k
  }
10598
10599
248k
  zend_emit_op_tmp(result,
10600
248k
    ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL,
10601
248k
    &right_node, &left_node);
10602
248k
}
10603
/* }}} */
10604
10605
static void zend_compile_unary_op(znode *result, const zend_ast *ast) /* {{{ */
10606
1.22M
{
10607
1.22M
  zend_ast *expr_ast = ast->child[0];
10608
1.22M
  uint32_t opcode = ast->attr;
10609
10610
1.22M
  znode expr_node;
10611
1.22M
  zend_compile_expr(&expr_node, expr_ast);
10612
10613
1.22M
  if (expr_node.op_type == IS_CONST
10614
354k
      && zend_try_ct_eval_unary_op(&result->u.constant, opcode, &expr_node.u.constant)) {
10615
353k
    result->op_type = IS_CONST;
10616
353k
    zval_ptr_dtor(&expr_node.u.constant);
10617
353k
    return;
10618
353k
  }
10619
10620
868k
  zend_emit_op_tmp(result, opcode, &expr_node, NULL);
10621
868k
}
10622
/* }}} */
10623
10624
static void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */
10625
83.3k
{
10626
83.3k
  zend_ast *expr_ast = ast->child[0];
10627
83.3k
  znode expr_node, right_node;
10628
10629
83.3k
  ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS);
10630
10631
83.3k
  zend_compile_expr(&expr_node, expr_ast);
10632
10633
83.3k
  if (expr_node.op_type == IS_CONST
10634
24.0k
    && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) {
10635
19.5k
    result->op_type = IS_CONST;
10636
19.5k
    zval_ptr_dtor(&expr_node.u.constant);
10637
19.5k
    return;
10638
19.5k
  }
10639
10640
63.7k
  right_node.op_type = IS_CONST;
10641
63.7k
  ZVAL_LONG(&right_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10642
63.7k
  zend_emit_op_tmp(result, ZEND_MUL, &expr_node, &right_node);
10643
63.7k
}
10644
/* }}} */
10645
10646
static void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */
10647
15.2k
{
10648
15.2k
  zend_ast *left_ast = ast->child[0];
10649
15.2k
  zend_ast *right_ast = ast->child[1];
10650
10651
15.2k
  znode left_node, right_node;
10652
15.2k
  zend_op *opline_jmpz, *opline_bool;
10653
15.2k
  uint32_t opnum_jmpz;
10654
10655
15.2k
  ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR);
10656
10657
15.2k
  zend_compile_expr(&left_node, left_ast);
10658
10659
15.2k
  if (left_node.op_type == IS_CONST) {
10660
2.71k
    if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant))
10661
1.64k
     || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) {
10662
1.57k
      result->op_type = IS_CONST;
10663
1.57k
      ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant));
10664
1.57k
    } else {
10665
1.14k
      zend_compile_expr(&right_node, right_ast);
10666
10667
1.14k
      if (right_node.op_type == IS_CONST) {
10668
700
        result->op_type = IS_CONST;
10669
700
        ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant));
10670
10671
700
        zval_ptr_dtor(&right_node.u.constant);
10672
700
      } else {
10673
446
        zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL);
10674
446
      }
10675
1.14k
    }
10676
10677
2.71k
    zval_ptr_dtor(&left_node.u.constant);
10678
2.71k
    return;
10679
2.71k
  }
10680
10681
12.4k
  opnum_jmpz = get_next_op_number();
10682
12.4k
  opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
10683
12.4k
    &left_node, NULL);
10684
10685
12.4k
  if (left_node.op_type == IS_TMP_VAR) {
10686
8.60k
    SET_NODE(opline_jmpz->result, &left_node);
10687
8.60k
    GET_NODE(result, opline_jmpz->result);
10688
8.60k
  } else {
10689
3.89k
    zend_make_tmp_result(result, opline_jmpz);
10690
3.89k
  }
10691
10692
12.4k
  zend_compile_expr(&right_node, right_ast);
10693
10694
12.4k
  opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
10695
12.4k
  SET_NODE(opline_bool->result, result);
10696
10697
12.4k
  zend_update_jump_target_to_next(opnum_jmpz);
10698
12.4k
}
10699
/* }}} */
10700
10701
static void zend_compile_post_incdec(znode *result, const zend_ast *ast) /* {{{ */
10702
11.7k
{
10703
11.7k
  zend_ast *var_ast = ast->child[0];
10704
11.7k
  ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
10705
10706
11.7k
  zend_ensure_writable_variable(var_ast);
10707
10708
11.7k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10709
787
    zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, false);
10710
787
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
10711
787
    zend_make_tmp_result(result, opline);
10712
10.9k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10713
1.48k
    zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, false, false);
10714
1.48k
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP;
10715
1.48k
    zend_make_tmp_result(result, opline);
10716
9.50k
  } else {
10717
9.50k
    znode var_node;
10718
9.50k
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10719
9.50k
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10720
161
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10721
161
    }
10722
9.50k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC,
10723
9.50k
      &var_node, NULL);
10724
9.50k
  }
10725
11.7k
}
10726
/* }}} */
10727
10728
static void zend_compile_pre_incdec(znode *result, const zend_ast *ast) /* {{{ */
10729
3.24k
{
10730
3.24k
  zend_ast *var_ast = ast->child[0];
10731
3.24k
  ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
10732
10733
3.24k
  zend_ensure_writable_variable(var_ast);
10734
10735
3.24k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10736
660
    zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, false);
10737
660
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;
10738
660
    opline->result_type = IS_TMP_VAR;
10739
660
    result->op_type = IS_TMP_VAR;
10740
2.58k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10741
237
    zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, false, false);
10742
237
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP;
10743
237
    opline->result_type = IS_TMP_VAR;
10744
237
    result->op_type = IS_TMP_VAR;
10745
2.34k
  } else {
10746
2.34k
    znode var_node;
10747
2.34k
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10748
2.34k
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10749
102
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10750
102
    }
10751
2.34k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC,
10752
2.34k
      &var_node, NULL);
10753
2.34k
  }
10754
3.24k
}
10755
/* }}} */
10756
10757
static void zend_compile_cast(znode *result, const zend_ast *ast) /* {{{ */
10758
2.90k
{
10759
2.90k
  zend_ast *expr_ast = ast->child[0];
10760
2.90k
  znode expr_node;
10761
2.90k
  zend_op *opline;
10762
10763
2.90k
  zend_compile_expr(&expr_node, expr_ast);
10764
10765
2.90k
  if (ast->attr == _IS_BOOL) {
10766
293
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL);
10767
2.61k
  } else if (ast->attr == IS_NULL) {
10768
14
    zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
10769
2.59k
  } else {
10770
2.59k
    opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
10771
2.59k
    opline->extended_value = ast->attr;
10772
2.59k
  }
10773
2.90k
}
10774
/* }}} */
10775
10776
static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */
10777
2.68k
{
10778
2.68k
  zend_ast *cond_ast = ast->child[0];
10779
2.68k
  zend_ast *false_ast = ast->child[2];
10780
10781
2.68k
  znode cond_node, false_node;
10782
2.68k
  zend_op *opline_qm_assign;
10783
2.68k
  uint32_t opnum_jmp_set;
10784
10785
2.68k
  ZEND_ASSERT(ast->child[1] == NULL);
10786
10787
2.68k
  zend_compile_expr(&cond_node, cond_ast);
10788
10789
2.68k
  opnum_jmp_set = get_next_op_number();
10790
2.68k
  zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL);
10791
10792
2.68k
  zend_compile_expr(&false_node, false_ast);
10793
10794
2.68k
  opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10795
2.68k
  SET_NODE(opline_qm_assign->result, result);
10796
10797
2.68k
  zend_update_jump_target_to_next(opnum_jmp_set);
10798
2.68k
}
10799
/* }}} */
10800
10801
static void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
10802
7.25k
{
10803
7.25k
  zend_ast *cond_ast = ast->child[0];
10804
7.25k
  zend_ast *true_ast = ast->child[1];
10805
7.25k
  zend_ast *false_ast = ast->child[2];
10806
10807
7.25k
  znode cond_node, true_node, false_node;
10808
7.25k
  zend_op *opline_qm_assign2;
10809
7.25k
  uint32_t opnum_jmpz, opnum_jmp;
10810
10811
7.25k
  if (cond_ast->kind == ZEND_AST_CONDITIONAL
10812
1.00k
      && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) {
10813
860
    if (cond_ast->child[1]) {
10814
35
      if (true_ast) {
10815
17
        zend_error(E_COMPILE_ERROR,
10816
17
          "Unparenthesized `a ? b : c ? d : e` is not supported. "
10817
17
          "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`");
10818
18
      } else {
10819
18
        zend_error(E_COMPILE_ERROR,
10820
18
          "Unparenthesized `a ? b : c ?: d` is not supported. "
10821
18
          "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`");
10822
18
      }
10823
825
    } else {
10824
825
      if (true_ast) {
10825
8
        zend_error(E_COMPILE_ERROR,
10826
8
          "Unparenthesized `a ?: b ? c : d` is not supported. "
10827
8
          "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`");
10828
817
      } else {
10829
        /* This case is harmless:  (a ?: b) ?: c always produces the same result
10830
         * as a ?: (b ?: c). */
10831
817
      }
10832
825
    }
10833
860
  }
10834
10835
7.25k
  if (!true_ast) {
10836
2.68k
    zend_compile_shorthand_conditional(result, ast);
10837
2.68k
    return;
10838
2.68k
  }
10839
10840
4.57k
  zend_compile_expr(&cond_node, cond_ast);
10841
10842
4.57k
  opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
10843
10844
4.57k
  zend_compile_expr(&true_node, true_ast);
10845
10846
4.57k
  zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL);
10847
10848
4.57k
  opnum_jmp = zend_emit_jump(0);
10849
10850
4.57k
  zend_update_jump_target_to_next(opnum_jmpz);
10851
10852
4.57k
  zend_compile_expr(&false_node, false_ast);
10853
10854
4.57k
  opline_qm_assign2 = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10855
4.57k
  SET_NODE(opline_qm_assign2->result, result);
10856
10857
4.57k
  zend_update_jump_target_to_next(opnum_jmp);
10858
4.57k
}
10859
/* }}} */
10860
10861
static void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
10862
1.19M
{
10863
1.19M
  zend_ast *expr_ast = ast->child[0];
10864
1.19M
  zend_ast *default_ast = ast->child[1];
10865
10866
1.19M
  znode expr_node, default_node;
10867
1.19M
  zend_op *opline;
10868
1.19M
  uint32_t opnum;
10869
10870
1.19M
  zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, false);
10871
10872
1.19M
  opnum = get_next_op_number();
10873
1.19M
  zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL);
10874
10875
1.19M
  zend_compile_expr(&default_node, default_ast);
10876
10877
1.19M
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL);
10878
1.19M
  SET_NODE(opline->result, result);
10879
10880
1.19M
  opline = &CG(active_op_array)->opcodes[opnum];
10881
1.19M
  opline->op2.opline_num = get_next_op_number();
10882
1.19M
}
10883
/* }}} */
10884
10885
134k
static void znode_dtor(zval *zv) {
10886
134k
  znode *node = Z_PTR_P(zv);
10887
134k
  if (node->op_type == IS_CONST) {
10888
6.44k
    zval_ptr_dtor_nogc(&node->u.constant);
10889
6.44k
  }
10890
134k
  efree(node);
10891
134k
}
10892
10893
static void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */
10894
49.7k
{
10895
49.7k
  zend_ast *var_ast = ast->child[0];
10896
49.7k
  zend_ast *default_ast = ast->child[1];
10897
10898
49.7k
  znode var_node_is, var_node_w, default_node, assign_node, *node;
10899
49.7k
  zend_op *opline;
10900
49.7k
  uint32_t coalesce_opnum;
10901
49.7k
  bool need_frees = false;
10902
10903
  /* Remember expressions compiled during the initial BP_VAR_IS lookup,
10904
   * to avoid double-evaluation when we compile again with BP_VAR_W. */
10905
49.7k
  HashTable *orig_memoized_exprs = CG(memoized_exprs);
10906
49.7k
  const zend_memoize_mode orig_memoize_mode = CG(memoize_mode);
10907
10908
49.7k
  zend_ensure_writable_variable(var_ast);
10909
49.7k
  if (is_this_fetch(var_ast)) {
10910
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
10911
5
  }
10912
10913
49.7k
  ALLOC_HASHTABLE(CG(memoized_exprs));
10914
49.7k
  zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0);
10915
10916
49.7k
  CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
10917
49.7k
  zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, false);
10918
10919
49.7k
  coalesce_opnum = get_next_op_number();
10920
49.7k
  zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL);
10921
10922
49.7k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
10923
49.7k
  if (var_ast->kind == ZEND_AST_DIM) {
10924
46.7k
    zend_compile_expr_with_potential_assign_to_self(&default_node, default_ast, var_ast);
10925
46.7k
  } else {
10926
2.95k
    zend_compile_expr(&default_node, default_ast);
10927
2.95k
  }
10928
10929
49.7k
  CG(memoize_mode) = ZEND_MEMOIZE_FETCH;
10930
49.7k
  zend_compile_var(&var_node_w, var_ast, BP_VAR_W, false);
10931
10932
  /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */
10933
49.7k
  opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
10934
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
10935
49.7k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
10936
49.7k
  switch (kind) {
10937
1.70k
    case ZEND_AST_VAR:
10938
1.70k
      zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node);
10939
1.70k
      break;
10940
900
    case ZEND_AST_STATIC_PROP:
10941
900
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
10942
900
      opline->result_type = IS_TMP_VAR;
10943
900
      var_node_w.op_type = IS_TMP_VAR;
10944
900
      zend_emit_op_data(&default_node);
10945
900
      assign_node = var_node_w;
10946
900
      break;
10947
46.4k
    case ZEND_AST_DIM:
10948
46.4k
      opline->opcode = ZEND_ASSIGN_DIM;
10949
46.4k
      opline->result_type = IS_TMP_VAR;
10950
46.4k
      var_node_w.op_type = IS_TMP_VAR;
10951
46.4k
      zend_emit_op_data(&default_node);
10952
46.4k
      assign_node = var_node_w;
10953
46.4k
      break;
10954
496
    case ZEND_AST_PROP:
10955
496
    case ZEND_AST_NULLSAFE_PROP:
10956
496
      opline->opcode = ZEND_ASSIGN_OBJ;
10957
496
      opline->result_type = IS_TMP_VAR;
10958
496
      var_node_w.op_type = IS_TMP_VAR;
10959
496
      zend_emit_op_data(&default_node);
10960
496
      assign_node = var_node_w;
10961
496
      break;
10962
0
    EMPTY_SWITCH_DEFAULT_CASE();
10963
49.7k
  }
10964
10965
49.5k
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL);
10966
49.5k
  SET_NODE(opline->result, result);
10967
10968
154k
  ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10969
154k
    if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10970
46.5k
      need_frees = true;
10971
46.5k
      break;
10972
46.5k
    }
10973
154k
  } ZEND_HASH_FOREACH_END();
10974
10975
  /* Free DUPed expressions if there are any */
10976
49.5k
  if (need_frees) {
10977
46.5k
    uint32_t jump_opnum = zend_emit_jump(0);
10978
46.5k
    zend_update_jump_target_to_next(coalesce_opnum);
10979
309k
    ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10980
309k
      if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10981
126k
        zend_emit_op(NULL, ZEND_FREE, node, NULL);
10982
126k
      }
10983
309k
    } ZEND_HASH_FOREACH_END();
10984
46.5k
    zend_update_jump_target_to_next(jump_opnum);
10985
46.5k
  } else {
10986
2.98k
    zend_update_jump_target_to_next(coalesce_opnum);
10987
2.98k
  }
10988
10989
49.5k
  zend_hash_destroy(CG(memoized_exprs));
10990
49.5k
  FREE_HASHTABLE(CG(memoized_exprs));
10991
49.5k
  CG(memoized_exprs) = orig_memoized_exprs;
10992
49.5k
  CG(memoize_mode) = orig_memoize_mode;
10993
49.5k
}
10994
/* }}} */
10995
10996
static void zend_compile_print(znode *result, const zend_ast *ast) /* {{{ */
10997
5.00k
{
10998
5.00k
  zend_op *opline;
10999
5.00k
  zend_ast *expr_ast = ast->child[0];
11000
11001
5.00k
  znode expr_node;
11002
5.00k
  zend_compile_expr(&expr_node, expr_ast);
11003
11004
5.00k
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
11005
5.00k
  opline->extended_value = 1;
11006
11007
5.00k
  result->op_type = IS_CONST;
11008
5.00k
  ZVAL_LONG(&result->u.constant, 1);
11009
5.00k
}
11010
/* }}} */
11011
11012
static void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
11013
43.4k
{
11014
43.4k
  zend_ast *value_ast = ast->child[0];
11015
43.4k
  zend_ast *key_ast = ast->child[1];
11016
11017
43.4k
  znode value_node, key_node;
11018
43.4k
  znode *value_node_ptr = NULL, *key_node_ptr = NULL;
11019
43.4k
  zend_op *opline;
11020
43.4k
  bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
11021
11022
43.4k
  zend_mark_function_as_generator();
11023
11024
43.4k
  if (key_ast) {
11025
417
    zend_compile_expr(&key_node, key_ast);
11026
417
    key_node_ptr = &key_node;
11027
417
  }
11028
11029
43.4k
  if (value_ast) {
11030
40.5k
    if (returns_by_ref && zend_is_variable_or_call(value_ast)) {
11031
324
      zend_assert_not_short_circuited(value_ast);
11032
324
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
11033
40.1k
    } else {
11034
40.1k
      zend_compile_expr(&value_node, value_ast);
11035
40.1k
    }
11036
40.5k
    value_node_ptr = &value_node;
11037
40.5k
  }
11038
11039
43.4k
  opline = zend_emit_op_tmp(result, ZEND_YIELD, value_node_ptr, key_node_ptr);
11040
11041
43.4k
  if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
11042
136
    opline->extended_value = ZEND_RETURNS_FUNCTION;
11043
136
  }
11044
43.4k
}
11045
/* }}} */
11046
11047
static void zend_compile_yield_from(znode *result, const zend_ast *ast) /* {{{ */
11048
1.53k
{
11049
1.53k
  zend_ast *expr_ast = ast->child[0];
11050
1.53k
  znode expr_node;
11051
11052
1.53k
  zend_mark_function_as_generator();
11053
11054
1.53k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
11055
5
    zend_error_noreturn(E_COMPILE_ERROR,
11056
5
      "Cannot use \"yield from\" inside a by-reference generator");
11057
5
  }
11058
11059
1.53k
  zend_compile_expr(&expr_node, expr_ast);
11060
1.53k
  zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
11061
1.53k
}
11062
/* }}} */
11063
11064
static void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */
11065
661
{
11066
661
  zend_ast *obj_ast = ast->child[0];
11067
661
  zend_ast *class_ast = ast->child[1];
11068
11069
661
  znode obj_node, class_node;
11070
661
  zend_op *opline;
11071
11072
661
  zend_compile_expr(&obj_node, obj_ast);
11073
661
  if (obj_node.op_type == IS_CONST) {
11074
51
    zend_do_free(&obj_node);
11075
51
    result->op_type = IS_CONST;
11076
51
    ZVAL_FALSE(&result->u.constant);
11077
51
    return;
11078
51
  }
11079
11080
610
  zend_compile_class_ref(&class_node, class_ast,
11081
610
    ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION | ZEND_FETCH_CLASS_SILENT);
11082
11083
610
  opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL);
11084
11085
610
  if (class_node.op_type == IS_CONST) {
11086
430
    opline->op2_type = IS_CONST;
11087
430
    opline->op2.constant = zend_add_class_name_literal(
11088
430
      Z_STR(class_node.u.constant));
11089
430
    opline->extended_value = zend_alloc_cache_slot();
11090
430
  } else {
11091
180
    SET_NODE(opline->op2, &class_node);
11092
180
  }
11093
610
}
11094
/* }}} */
11095
11096
static void zend_compile_include_or_eval(znode *result, const zend_ast *ast) /* {{{ */
11097
7.87k
{
11098
7.87k
  zend_ast *expr_ast = ast->child[0];
11099
7.87k
  znode expr_node;
11100
7.87k
  zend_op *opline;
11101
11102
7.87k
  zend_do_extended_fcall_begin();
11103
7.87k
  zend_compile_expr(&expr_node, expr_ast);
11104
11105
7.87k
  opline = zend_emit_op_tmp(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL);
11106
7.87k
  opline->extended_value = ast->attr;
11107
11108
7.87k
  zend_do_extended_fcall_end();
11109
7.87k
}
11110
/* }}} */
11111
11112
static void zend_compile_isset_or_empty(znode *result, const zend_ast *ast) /* {{{ */
11113
7.12k
{
11114
7.12k
  zend_ast *var_ast = ast->child[0];
11115
11116
7.12k
  znode var_node;
11117
7.12k
  zend_op *opline = NULL;
11118
11119
7.12k
  ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY);
11120
11121
7.12k
  if (!zend_is_variable(var_ast)) {
11122
341
    if (ast->kind == ZEND_AST_EMPTY) {
11123
      /* empty(expr) can be transformed to !expr */
11124
291
      zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast);
11125
291
      zend_compile_expr(result, not_ast);
11126
291
      return;
11127
291
    } else {
11128
50
      zend_error_noreturn(E_COMPILE_ERROR,
11129
50
        "Cannot use isset() on the result of an expression "
11130
50
        "(you can use \"null !== expression\" instead)");
11131
50
    }
11132
341
  }
11133
11134
6.78k
  if (is_globals_fetch(var_ast)) {
11135
182
    result->op_type = IS_CONST;
11136
182
    ZVAL_BOOL(&result->u.constant, ast->kind == ZEND_AST_ISSET);
11137
182
    return;
11138
182
  }
11139
11140
6.59k
  if (is_global_var_fetch(var_ast)) {
11141
502
    if (!var_ast->child[1]) {
11142
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
11143
6
    }
11144
11145
496
    zend_compile_expr(&var_node, var_ast->child[1]);
11146
496
    if (var_node.op_type == IS_CONST) {
11147
360
      convert_to_string(&var_node.u.constant);
11148
360
    }
11149
11150
496
    opline = zend_emit_op_tmp(result, ZEND_ISSET_ISEMPTY_VAR, &var_node, NULL);
11151
496
    opline->extended_value =
11152
496
      ZEND_FETCH_GLOBAL | (ast->kind == ZEND_AST_EMPTY ? ZEND_ISEMPTY : 0);
11153
496
    return;
11154
502
  }
11155
11156
6.09k
  zend_short_circuiting_mark_inner(var_ast);
11157
6.09k
  switch (var_ast->kind) {
11158
1.21k
    case ZEND_AST_VAR:
11159
1.21k
      if (is_this_fetch(var_ast)) {
11160
127
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL);
11161
127
        CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
11162
1.08k
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_IS) == SUCCESS) {
11163
942
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL);
11164
942
      } else {
11165
144
        opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, false);
11166
144
        opline->opcode = ZEND_ISSET_ISEMPTY_VAR;
11167
144
      }
11168
1.21k
      break;
11169
3.47k
    case ZEND_AST_DIM:
11170
3.47k
      opline = zend_compile_dim(result, var_ast, BP_VAR_IS, /* by_ref */ false);
11171
3.47k
      opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
11172
3.47k
      break;
11173
948
    case ZEND_AST_PROP:
11174
1.06k
    case ZEND_AST_NULLSAFE_PROP:
11175
1.06k
      opline = zend_compile_prop(result, var_ast, BP_VAR_IS, false);
11176
1.06k
      opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
11177
1.06k
      break;
11178
342
    case ZEND_AST_STATIC_PROP:
11179
342
      opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, false, false);
11180
342
      opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP;
11181
342
      break;
11182
6.09k
    EMPTY_SWITCH_DEFAULT_CASE()
11183
6.09k
  }
11184
11185
6.08k
  result->op_type = opline->result_type = IS_TMP_VAR;
11186
6.08k
  if (!(ast->kind == ZEND_AST_ISSET)) {
11187
979
    opline->extended_value |= ZEND_ISEMPTY;
11188
979
  }
11189
6.08k
}
11190
/* }}} */
11191
11192
static void zend_compile_silence(znode *result, const zend_ast *ast) /* {{{ */
11193
9.69M
{
11194
9.69M
  zend_ast *expr_ast = ast->child[0];
11195
9.69M
  znode silence_node;
11196
11197
9.69M
  zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL);
11198
11199
9.69M
  if (expr_ast->kind == ZEND_AST_VAR) {
11200
    /* For @$var we need to force a FETCH instruction, otherwise the CV access will
11201
     * happen outside the silenced section. */
11202
91.2k
    zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, false );
11203
9.59M
  } else {
11204
9.59M
    zend_compile_expr(result, expr_ast);
11205
9.59M
  }
11206
11207
9.69M
  zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL);
11208
9.69M
}
11209
/* }}} */
11210
11211
static void zend_compile_shell_exec(znode *result, const zend_ast *ast) /* {{{ */
11212
13.2k
{
11213
13.2k
  zend_ast *expr_ast = ast->child[0];
11214
11215
13.2k
  zval fn_name;
11216
13.2k
  zend_ast *name_ast, *args_ast, *call_ast;
11217
11218
13.2k
  zend_error(E_DEPRECATED, "The backtick (`) operator is deprecated, use shell_exec() instead");
11219
11220
13.2k
  ZVAL_STRING(&fn_name, "shell_exec");
11221
13.2k
  name_ast = zend_ast_create_zval(&fn_name);
11222
13.2k
  args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast);
11223
13.2k
  call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast);
11224
11225
13.2k
  zend_compile_expr(result, call_ast);
11226
11227
13.2k
  zval_ptr_dtor(&fn_name);
11228
13.2k
}
11229
/* }}} */
11230
11231
static void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
11232
85.6k
{
11233
85.6k
  zend_ast_list *list = zend_ast_get_list(ast);
11234
85.6k
  zend_op *opline;
11235
85.6k
  uint32_t i, opnum_init = -1;
11236
85.6k
  bool packed = true;
11237
11238
85.6k
  if (zend_try_ct_eval_array(&result->u.constant, ast)) {
11239
50.2k
    result->op_type = IS_CONST;
11240
50.2k
    return;
11241
50.2k
  }
11242
11243
  /* Empty arrays are handled at compile-time */
11244
35.4k
  ZEND_ASSERT(list->children > 0);
11245
11246
155k
  for (i = 0; i < list->children; ++i) {
11247
120k
    zend_ast *elem_ast = list->child[i];
11248
120k
    zend_ast *value_ast, *key_ast;
11249
120k
    bool by_ref;
11250
120k
    znode value_node, key_node, *key_node_ptr = NULL;
11251
11252
120k
    if (elem_ast == NULL) {
11253
0
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
11254
0
    }
11255
11256
120k
    value_ast = elem_ast->child[0];
11257
11258
120k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
11259
1.45k
      zend_compile_expr(&value_node, value_ast);
11260
1.45k
      if (i == 0) {
11261
1.03k
        opnum_init = get_next_op_number();
11262
1.03k
        opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
11263
1.03k
      }
11264
1.45k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL);
11265
1.45k
      SET_NODE(opline->result, result);
11266
1.45k
      continue;
11267
1.45k
    }
11268
11269
119k
    key_ast = elem_ast->child[1];
11270
119k
    by_ref = elem_ast->attr;
11271
11272
119k
    if (key_ast) {
11273
6.40k
      zend_compile_expr(&key_node, key_ast);
11274
6.40k
      zend_handle_numeric_op(&key_node);
11275
6.40k
      key_node_ptr = &key_node;
11276
6.40k
    }
11277
11278
119k
    if (by_ref) {
11279
445
      zend_ensure_writable_variable(value_ast);
11280
445
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
11281
118k
    } else {
11282
118k
      zend_compile_expr(&value_node, value_ast);
11283
118k
    }
11284
11285
119k
    if (i == 0) {
11286
34.2k
      opnum_init = get_next_op_number();
11287
34.2k
      opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr);
11288
34.2k
      opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT;
11289
84.9k
    } else {
11290
84.9k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT,
11291
84.9k
        &value_node, key_node_ptr);
11292
84.9k
      SET_NODE(opline->result, result);
11293
84.9k
    }
11294
119k
    opline->extended_value |= by_ref;
11295
11296
119k
    if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) {
11297
3.31k
      packed = false;
11298
3.31k
    }
11299
119k
  }
11300
11301
  /* Add a flag to INIT_ARRAY if we know this array cannot be packed */
11302
35.2k
  if (!packed) {
11303
1.47k
    ZEND_ASSERT(opnum_init != (uint32_t)-1);
11304
1.47k
    opline = &CG(active_op_array)->opcodes[opnum_init];
11305
1.47k
    opline->extended_value |= ZEND_ARRAY_NOT_PACKED;
11306
1.47k
  }
11307
35.2k
}
11308
/* }}} */
11309
11310
static void zend_compile_const(znode *result, const zend_ast *ast) /* {{{ */
11311
6.20M
{
11312
6.20M
  zend_ast *name_ast = ast->child[0];
11313
11314
6.20M
  zend_op *opline;
11315
11316
6.20M
  bool is_fully_qualified;
11317
6.20M
  zend_string *orig_name = zend_ast_get_str(name_ast);
11318
6.20M
  zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified);
11319
11320
6.20M
  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__"))) {
11321
661
    zend_ast *last = CG(ast);
11322
11323
1.32k
    while (last && last->kind == ZEND_AST_STMT_LIST) {
11324
740
      const zend_ast_list *list = zend_ast_get_list(last);
11325
740
      if (list->children == 0) {
11326
75
        break;
11327
75
      }
11328
665
      last = list->child[list->children-1];
11329
665
    }
11330
661
    if (last && last->kind == ZEND_AST_HALT_COMPILER) {
11331
156
      result->op_type = IS_CONST;
11332
156
      ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
11333
156
      zend_string_release_ex(resolved_name, 0);
11334
156
      return;
11335
156
    }
11336
661
  }
11337
11338
6.20M
  if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
11339
31.8k
    result->op_type = IS_CONST;
11340
31.8k
    zend_string_release_ex(resolved_name, 0);
11341
31.8k
    return;
11342
31.8k
  }
11343
11344
6.17M
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
11345
6.17M
  opline->op2_type = IS_CONST;
11346
11347
6.17M
  if (is_fully_qualified || !FC(current_namespace)) {
11348
152k
    opline->op1.num = 0;
11349
152k
    opline->op2.constant = zend_add_const_name_literal(
11350
152k
      resolved_name, false);
11351
6.01M
  } else {
11352
6.01M
    opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE;
11353
6.01M
    opline->op2.constant = zend_add_const_name_literal(
11354
6.01M
      resolved_name, true);
11355
6.01M
  }
11356
6.17M
  opline->extended_value = zend_alloc_cache_slot();
11357
6.17M
}
11358
/* }}} */
11359
11360
static void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */
11361
113k
{
11362
113k
  zend_ast *class_ast;
11363
113k
  zend_ast *const_ast;
11364
113k
  znode class_node, const_node;
11365
113k
  zend_op *opline;
11366
11367
113k
  zend_eval_const_expr(&ast->child[0]);
11368
113k
  zend_eval_const_expr(&ast->child[1]);
11369
11370
113k
  class_ast = ast->child[0];
11371
113k
  const_ast = ast->child[1];
11372
11373
113k
  if (class_ast->kind == ZEND_AST_ZVAL && const_ast->kind == ZEND_AST_ZVAL) {
11374
32.7k
    zval *const_zv = zend_ast_get_zval(const_ast);
11375
32.7k
    if (Z_TYPE_P(const_zv) == IS_STRING) {
11376
32.0k
      zend_string *const_str = Z_STR_P(const_zv);
11377
32.0k
      zend_string *resolved_name = zend_resolve_class_name_ast(class_ast);
11378
32.0k
      if (zend_try_ct_eval_class_const(&result->u.constant, resolved_name, const_str)) {
11379
255
        result->op_type = IS_CONST;
11380
255
        zend_string_release_ex(resolved_name, 0);
11381
255
        return;
11382
255
      }
11383
31.8k
      zend_string_release_ex(resolved_name, 0);
11384
31.8k
    }
11385
32.7k
  }
11386
11387
113k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
11388
11389
113k
  zend_compile_expr(&const_node, const_ast);
11390
11391
113k
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node);
11392
11393
113k
  zend_set_class_name_op1(opline, &class_node);
11394
11395
113k
  if (opline->op1_type == IS_CONST || opline->op2_type == IS_CONST) {
11396
112k
    opline->extended_value = zend_alloc_cache_slots(2);
11397
112k
  }
11398
113k
}
11399
/* }}} */
11400
11401
static void zend_compile_class_name(znode *result, const zend_ast *ast) /* {{{ */
11402
4.65k
{
11403
4.65k
  zend_ast *class_ast = ast->child[0];
11404
11405
4.65k
  if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) {
11406
2.81k
    result->op_type = IS_CONST;
11407
2.81k
    return;
11408
2.81k
  }
11409
11410
1.83k
  if (class_ast->kind == ZEND_AST_ZVAL) {
11411
640
    zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11412
640
    opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
11413
1.19k
  } else {
11414
1.19k
    znode expr_node;
11415
1.19k
    zend_compile_expr(&expr_node, class_ast);
11416
1.19k
    if (expr_node.op_type == IS_CONST) {
11417
      /* Unlikely case that happen if class_ast is constant folded.
11418
       * Handle it here, to avoid needing a CONST specialization in the VM. */
11419
9
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"::class\" on %s",
11420
9
        zend_zval_value_name(&expr_node.u.constant));
11421
9
    }
11422
11423
1.18k
    zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL);
11424
1.18k
  }
11425
1.83k
}
11426
/* }}} */
11427
11428
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */
11429
231k
{
11430
231k
  if (num == 0) {
11431
30.8k
    result->op_type = IS_TMP_VAR;
11432
30.8k
    result->u.op.var = -1;
11433
30.8k
    opline->opcode = ZEND_ROPE_INIT;
11434
200k
  } else {
11435
200k
    opline->opcode = ZEND_ROPE_ADD;
11436
200k
    SET_NODE(opline->op1, result);
11437
200k
  }
11438
231k
  SET_NODE(opline->op2, elem_node);
11439
231k
  SET_NODE(opline->result, result);
11440
231k
  opline->extended_value = num;
11441
231k
  return opline;
11442
231k
}
11443
/* }}} */
11444
11445
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */
11446
223k
{
11447
223k
  zend_op *opline = get_next_op();
11448
11449
223k
  if (num == 0) {
11450
18.1k
    result->op_type = IS_TMP_VAR;
11451
18.1k
    result->u.op.var = -1;
11452
18.1k
    opline->opcode = ZEND_ROPE_INIT;
11453
204k
  } else {
11454
204k
    opline->opcode = ZEND_ROPE_ADD;
11455
204k
    SET_NODE(opline->op1, result);
11456
204k
  }
11457
223k
  SET_NODE(opline->op2, elem_node);
11458
223k
  SET_NODE(opline->result, result);
11459
223k
  opline->extended_value = num;
11460
223k
  return opline;
11461
223k
}
11462
/* }}} */
11463
11464
static void zend_compile_rope_finalize(znode *result, uint32_t rope_elements, zend_op *init_opline, zend_op *opline)
11465
49.0k
{
11466
49.0k
  if (rope_elements == 1) {
11467
1.72k
    if (opline->op2_type == IS_CONST) {
11468
515
      GET_NODE(result, opline->op2);
11469
515
      ZVAL_UNDEF(CT_CONSTANT(opline->op2));
11470
515
      SET_UNUSED(opline->op2);
11471
515
      MAKE_NOP(opline);
11472
1.20k
    } else {
11473
1.20k
      opline->opcode = ZEND_CAST;
11474
1.20k
      opline->extended_value = IS_STRING;
11475
1.20k
      opline->op1_type = opline->op2_type;
11476
1.20k
      opline->op1 = opline->op2;
11477
1.20k
      SET_UNUSED(opline->op2);
11478
1.20k
      zend_make_tmp_result(result, opline);
11479
1.20k
    }
11480
47.3k
  } else if (rope_elements == 2) {
11481
10.8k
    opline->opcode = ZEND_FAST_CONCAT;
11482
10.8k
    opline->extended_value = 0;
11483
10.8k
    opline->op1_type = init_opline->op2_type;
11484
10.8k
    opline->op1 = init_opline->op2;
11485
10.8k
    zend_make_tmp_result(result, opline);
11486
10.8k
    MAKE_NOP(init_opline);
11487
36.4k
  } else {
11488
36.4k
    uint32_t var;
11489
11490
36.4k
    init_opline->extended_value = rope_elements;
11491
36.4k
    opline->opcode = ZEND_ROPE_END;
11492
36.4k
    zend_make_tmp_result(result, opline);
11493
36.4k
    var = opline->op1.var = get_temporary_variable();
11494
11495
    /* Allocates the necessary number of zval slots to keep the rope */
11496
36.4k
    uint32_t i = ((rope_elements * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
11497
232k
    while (i > 1) {
11498
195k
      get_temporary_variable();
11499
195k
      i--;
11500
195k
    }
11501
11502
    /* Update all the previous opcodes to use the same variable */
11503
538k
    while (opline != init_opline) {
11504
501k
      opline--;
11505
501k
      if (opline->opcode == ZEND_ROPE_ADD &&
11506
359k
          opline->result.var == (uint32_t)-1) {
11507
358k
        opline->op1.var = var;
11508
358k
        opline->result.var = var;
11509
358k
      } else if (opline->opcode == ZEND_ROPE_INIT &&
11510
36.7k
                 opline->result.var == (uint32_t)-1) {
11511
36.4k
        opline->result.var = var;
11512
36.4k
      }
11513
501k
    }
11514
36.4k
  }
11515
49.0k
}
11516
11517
static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
11518
46.4k
{
11519
46.4k
  uint32_t i, j;
11520
46.4k
  uint32_t rope_init_lineno = -1;
11521
46.4k
  zend_op *opline = NULL, *init_opline;
11522
46.4k
  znode elem_node, last_const_node;
11523
46.4k
  zend_ast_list *list = zend_ast_get_list(ast);
11524
46.4k
  uint32_t reserved_op_number = -1;
11525
11526
46.4k
  ZEND_ASSERT(list->children > 0);
11527
11528
46.4k
  j = 0;
11529
46.4k
  last_const_node.op_type = IS_UNUSED;
11530
494k
  for (i = 0; i < list->children; i++) {
11531
447k
    zend_ast *encaps_var = list->child[i];
11532
11533
447k
    if (encaps_var->attr & (ZEND_ENCAPS_VAR_DOLLAR_CURLY|ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11534
12.9k
      if ((encaps_var->kind == ZEND_AST_VAR || encaps_var->kind == ZEND_AST_DIM) && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY)) {
11535
459
        zend_error(E_DEPRECATED, "Using ${var} in strings is deprecated, use {$var} instead");
11536
12.4k
      } else if (encaps_var->kind == ZEND_AST_VAR && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11537
12.4k
        zend_error(E_DEPRECATED, "Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead");
11538
12.4k
      }
11539
12.9k
    }
11540
11541
447k
    zend_compile_expr(&elem_node, encaps_var);
11542
11543
447k
    if (elem_node.op_type == IS_CONST) {
11544
232k
      convert_to_string(&elem_node.u.constant);
11545
11546
232k
      if (Z_STRLEN(elem_node.u.constant) == 0) {
11547
660
        zval_ptr_dtor(&elem_node.u.constant);
11548
231k
      } else if (last_const_node.op_type == IS_CONST) {
11549
0
        concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant);
11550
0
        zval_ptr_dtor(&elem_node.u.constant);
11551
231k
      } else {
11552
231k
        last_const_node.op_type = IS_CONST;
11553
231k
        ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant);
11554
        /* Reserve place for ZEND_ROPE_ADD instruction */
11555
231k
        reserved_op_number = get_next_op_number();
11556
231k
        opline = get_next_op();
11557
231k
        opline->opcode = ZEND_NOP;
11558
231k
      }
11559
232k
      continue;
11560
232k
    } else {
11561
215k
      if (j == 0) {
11562
46.4k
        if (last_const_node.op_type == IS_CONST) {
11563
30.8k
          rope_init_lineno = reserved_op_number;
11564
30.8k
        } else {
11565
15.6k
          rope_init_lineno = get_next_op_number();
11566
15.6k
        }
11567
46.4k
      }
11568
215k
      if (last_const_node.op_type == IS_CONST) {
11569
189k
        opline = &CG(active_op_array)->opcodes[reserved_op_number];
11570
189k
        zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11571
189k
        last_const_node.op_type = IS_UNUSED;
11572
189k
      }
11573
215k
      opline = zend_compile_rope_add(result, j++, &elem_node);
11574
215k
    }
11575
447k
  }
11576
11577
46.4k
  if (j == 0) {
11578
0
    result->op_type = IS_CONST;
11579
0
    if (last_const_node.op_type == IS_CONST) {
11580
0
      ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant);
11581
0
    } else {
11582
0
      ZVAL_EMPTY_STRING(&result->u.constant);
11583
      /* empty string */
11584
0
    }
11585
0
    CG(active_op_array)->last = reserved_op_number - 1;
11586
0
    return;
11587
46.4k
  } else if (last_const_node.op_type == IS_CONST) {
11588
41.6k
    opline = &CG(active_op_array)->opcodes[reserved_op_number];
11589
41.6k
    opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11590
41.6k
  }
11591
46.4k
  init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
11592
46.4k
  zend_compile_rope_finalize(result, j, init_opline, opline);
11593
46.4k
}
11594
/* }}} */
11595
11596
static void zend_compile_magic_const(znode *result, const zend_ast *ast) /* {{{ */
11597
17.7k
{
11598
17.7k
  zend_op *opline;
11599
11600
17.7k
  if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) {
11601
16.1k
    result->op_type = IS_CONST;
11602
16.1k
    return;
11603
16.1k
  }
11604
11605
1.59k
  ZEND_ASSERT(ast->attr == T_CLASS_C &&
11606
1.59k
              CG(active_class_entry) &&
11607
1.59k
              (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
11608
11609
1.59k
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11610
1.59k
  opline->op1.num = ZEND_FETCH_CLASS_SELF;
11611
1.59k
}
11612
/* }}} */
11613
11614
static bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */
11615
49.9k
{
11616
49.9k
  return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP
11617
43.7k
    || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL
11618
42.8k
    || kind == ZEND_AST_AND || kind == ZEND_AST_OR
11619
42.6k
    || kind == ZEND_AST_UNARY_OP
11620
41.3k
    || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS
11621
40.3k
    || kind == ZEND_AST_CAST
11622
40.0k
    || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM
11623
38.6k
    || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM
11624
35.7k
    || kind == ZEND_AST_UNPACK
11625
35.6k
    || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST
11626
10.5k
    || kind == ZEND_AST_CLASS_NAME
11627
10.5k
    || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE
11628
9.57k
    || kind == ZEND_AST_CONST_ENUM_INIT
11629
3.20k
    || kind == ZEND_AST_NEW || kind == ZEND_AST_ARG_LIST
11630
1.59k
    || kind == ZEND_AST_NAMED_ARG
11631
1.52k
    || kind == ZEND_AST_PROP || kind == ZEND_AST_NULLSAFE_PROP
11632
995
    || kind == ZEND_AST_CLOSURE
11633
846
    || kind == ZEND_AST_CALL || kind == ZEND_AST_STATIC_CALL || kind == ZEND_AST_CALLABLE_CONVERT;
11634
49.9k
}
11635
/* }}} */
11636
11637
static void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
11638
3.09k
{
11639
3.09k
  zend_ast *ast = *ast_ptr;
11640
3.09k
  zend_ast *class_ast = ast->child[0];
11641
3.09k
  zend_string *class_name;
11642
3.09k
  int fetch_type;
11643
11644
3.09k
  if (class_ast->kind != ZEND_AST_ZVAL) {
11645
16
    zend_error_noreturn(E_COMPILE_ERROR,
11646
16
      "Dynamic class names are not allowed in compile-time class constant references");
11647
16
  }
11648
3.08k
  if (Z_TYPE_P(zend_ast_get_zval(class_ast)) != IS_STRING) {
11649
9
    zend_throw_error(NULL, "Class name must be a valid object or a string");
11650
9
  }
11651
11652
3.08k
  class_name = zend_ast_get_str(class_ast);
11653
3.08k
  fetch_type = zend_get_class_fetch_type(class_name);
11654
11655
3.08k
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11656
5
    zend_error_noreturn(E_COMPILE_ERROR,
11657
5
      "\"static::\" is not allowed in compile-time constants");
11658
5
  }
11659
11660
3.07k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
11661
2.26k
    zend_string *tmp = zend_resolve_class_name_ast(class_ast);
11662
11663
2.26k
    zend_string_release_ex(class_name, 0);
11664
2.26k
    if (tmp != class_name) {
11665
477
      zval *zv = zend_ast_get_zval(class_ast);
11666
477
      ZVAL_STR(zv, tmp);
11667
477
      class_ast->attr = ZEND_NAME_FQ;
11668
477
    }
11669
2.26k
  }
11670
11671
3.07k
  ast->attr |= ZEND_FETCH_CLASS_EXCEPTION;
11672
3.07k
}
11673
/* }}} */
11674
11675
static void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */
11676
60
{
11677
60
  zend_ast *ast = *ast_ptr;
11678
60
  zend_ast *class_ast = ast->child[0];
11679
60
  if (class_ast->kind != ZEND_AST_ZVAL) {
11680
5
    zend_error_noreturn(E_COMPILE_ERROR,
11681
5
      "(expression)::class cannot be used in constant expressions");
11682
5
  }
11683
11684
55
  zend_string *class_name = zend_ast_get_str(class_ast);
11685
55
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
11686
11687
55
  switch (fetch_type) {
11688
50
    case ZEND_FETCH_CLASS_SELF:
11689
50
    case ZEND_FETCH_CLASS_PARENT:
11690
      /* For the const-eval representation store the fetch type instead of the name. */
11691
50
      zend_string_release(class_name);
11692
50
      ast->child[0] = NULL;
11693
50
      ast->attr = fetch_type;
11694
50
      return;
11695
5
    case ZEND_FETCH_CLASS_STATIC:
11696
5
      zend_error_noreturn(E_COMPILE_ERROR,
11697
5
        "static::class cannot be used for compile-time class name resolution");
11698
55
    EMPTY_SWITCH_DEFAULT_CASE()
11699
55
  }
11700
55
}
11701
11702
static void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */
11703
22.0k
{
11704
22.0k
  zend_ast *ast = *ast_ptr;
11705
22.0k
  zend_ast *name_ast = ast->child[0];
11706
22.0k
  zend_string *orig_name = zend_ast_get_str(name_ast);
11707
22.0k
  bool is_fully_qualified;
11708
22.0k
  zval result;
11709
22.0k
  zend_string *resolved_name;
11710
11711
22.0k
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11712
11713
22.0k
  resolved_name = zend_resolve_const_name(
11714
22.0k
    orig_name, name_ast->attr, &is_fully_qualified);
11715
11716
22.0k
  if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
11717
0
    zend_string_release_ex(resolved_name, 0);
11718
0
    zend_ast_destroy(ast);
11719
0
    *ast_ptr = zend_ast_create_zval(&result);
11720
0
    return;
11721
0
  }
11722
11723
22.0k
  zend_ast_destroy(ast);
11724
22.0k
  *ast_ptr = zend_ast_create_constant(resolved_name,
11725
22.0k
    !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0);
11726
22.0k
}
11727
/* }}} */
11728
11729
static void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
11730
386
{
11731
386
  zend_ast *ast = *ast_ptr;
11732
11733
  /* Other cases already resolved by constant folding */
11734
386
  ZEND_ASSERT(ast->attr == T_CLASS_C);
11735
11736
386
  zend_ast_destroy(ast);
11737
386
  *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS);
11738
386
}
11739
/* }}} */
11740
11741
static void zend_compile_const_expr_class_reference(zend_ast *class_ast)
11742
940
{
11743
940
  if (class_ast->kind == ZEND_AST_CLASS) {
11744
0
    zend_error_noreturn(E_COMPILE_ERROR,
11745
0
      "Cannot use anonymous class in constant expression");
11746
0
  }
11747
940
  if (class_ast->kind != ZEND_AST_ZVAL) {
11748
3
    zend_error_noreturn(E_COMPILE_ERROR,
11749
3
      "Cannot use dynamic class name in constant expression");
11750
3
  }
11751
11752
937
  zend_string *class_name = zend_resolve_class_name_ast(class_ast);
11753
937
  int fetch_type = zend_get_class_fetch_type(class_name);
11754
937
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11755
7
    zend_error_noreturn(E_COMPILE_ERROR,
11756
7
      "\"static\" is not allowed in compile-time constants");
11757
7
  }
11758
11759
930
  zval *class_ast_zv = zend_ast_get_zval(class_ast);
11760
930
  zval_ptr_dtor_nogc(class_ast_zv);
11761
930
  ZVAL_STR(class_ast_zv, class_name);
11762
930
  class_ast->attr = fetch_type << ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT;
11763
930
}
11764
11765
static void zend_compile_const_expr_new(zend_ast **ast_ptr)
11766
805
{
11767
805
  zend_ast *class_ast = (*ast_ptr)->child[0];
11768
805
  zend_compile_const_expr_class_reference(class_ast);
11769
11770
805
  const zend_ast *args_ast = (*ast_ptr)->child[1];
11771
805
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
11772
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
11773
5
  }
11774
805
}
11775
11776
static void zend_compile_const_expr_closure(zend_ast **ast_ptr)
11777
149
{
11778
149
  zend_ast_decl *closure_ast = (zend_ast_decl *) *ast_ptr;
11779
149
  const zend_ast *uses_ast = closure_ast->child[1];
11780
149
  if (!(closure_ast->flags & ZEND_ACC_STATIC)) {
11781
5
    zend_error_noreturn(E_COMPILE_ERROR,
11782
5
      "Closures in constant expressions must be static");
11783
5
  }
11784
144
  if (uses_ast) {
11785
5
    zend_error_noreturn(E_COMPILE_ERROR,
11786
5
      "Cannot use(...) variables in constant expression");
11787
5
  }
11788
11789
139
  znode node;
11790
139
  zend_op_array *op = zend_compile_func_decl(&node, (zend_ast*)closure_ast, FUNC_DECL_LEVEL_CONSTEXPR);
11791
11792
139
  zend_ast_destroy(*ast_ptr);
11793
139
  *ast_ptr = zend_ast_create_op_array(op);
11794
139
}
11795
11796
static void zend_compile_const_expr_fcc(zend_ast **ast_ptr)
11797
424
{
11798
424
  zend_ast **args_ast;
11799
424
  switch ((*ast_ptr)->kind) {
11800
287
    case ZEND_AST_CALL:
11801
287
      args_ast = &(*ast_ptr)->child[1];
11802
287
      break;
11803
137
    case ZEND_AST_STATIC_CALL:
11804
137
      args_ast = &(*ast_ptr)->child[2];
11805
137
      break;
11806
0
    EMPTY_SWITCH_DEFAULT_CASE();
11807
424
  }
11808
424
  if ((*args_ast)->kind != ZEND_AST_CALLABLE_CONVERT) {
11809
34
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11810
34
  }
11811
390
  ZEND_MAP_PTR_NEW(((zend_ast_fcc *)*args_ast)->fptr);
11812
11813
390
  switch ((*ast_ptr)->kind) {
11814
255
    case ZEND_AST_CALL: {
11815
255
      zend_ast *name_ast = (*ast_ptr)->child[0];
11816
255
      if (name_ast->kind != ZEND_AST_ZVAL) {
11817
13
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic function name in constant expression");
11818
13
      }
11819
242
      zval *name_ast_zv = zend_ast_get_zval(name_ast);
11820
242
      if (Z_TYPE_P(name_ast_zv) != IS_STRING) {
11821
5
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal function name");
11822
5
      }
11823
242
      bool is_fully_qualified;
11824
237
      zend_string *name = zend_resolve_function_name(Z_STR_P(name_ast_zv), name_ast->attr, &is_fully_qualified);
11825
237
      zval_ptr_dtor_nogc(name_ast_zv);
11826
237
      ZVAL_STR(name_ast_zv, name);
11827
237
      if (is_fully_qualified) {
11828
54
        name_ast->attr = ZEND_NAME_FQ;
11829
54
      }
11830
237
      break;
11831
242
    }
11832
135
    case ZEND_AST_STATIC_CALL: {
11833
135
      zend_ast *class_ast = (*ast_ptr)->child[0];
11834
135
      zend_compile_const_expr_class_reference(class_ast);
11835
135
      zend_ast *method_ast = (*ast_ptr)->child[1];
11836
135
      if (method_ast->kind != ZEND_AST_ZVAL) {
11837
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic method name in constant expression");
11838
0
      }
11839
135
      if (Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING) {
11840
0
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal method name");
11841
0
      }
11842
135
      break;
11843
135
    }
11844
135
    EMPTY_SWITCH_DEFAULT_CASE();
11845
390
  }
11846
390
}
11847
11848
static void zend_compile_const_expr_args(zend_ast **ast_ptr)
11849
796
{
11850
796
  zend_ast_list *list = zend_ast_get_list(*ast_ptr);
11851
796
  bool uses_named_args = false;
11852
1.37k
  for (uint32_t i = 0; i < list->children; i++) {
11853
577
    const zend_ast *arg = list->child[i];
11854
577
    if (arg->kind == ZEND_AST_UNPACK) {
11855
1
      zend_error_noreturn(E_COMPILE_ERROR,
11856
1
        "Argument unpacking in constant expressions is not supported");
11857
1
    }
11858
576
    if (arg->kind == ZEND_AST_NAMED_ARG) {
11859
69
      uses_named_args = true;
11860
507
    } else if (uses_named_args) {
11861
1
      zend_error_noreturn(E_COMPILE_ERROR,
11862
1
        "Cannot use positional argument after named argument");
11863
1
    }
11864
576
  }
11865
794
  if (uses_named_args) {
11866
67
    list->attr = 1;
11867
67
  }
11868
794
}
11869
11870
typedef struct {
11871
  /* Whether the value of this expression may differ on each evaluation. */
11872
  bool allow_dynamic;
11873
} const_expr_context;
11874
11875
static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */
11876
162k
{
11877
162k
  const const_expr_context *ctx = context;
11878
162k
  zend_ast *ast = *ast_ptr;
11879
162k
  if (ast == NULL || ast->kind == ZEND_AST_ZVAL) {
11880
112k
    return;
11881
112k
  }
11882
11883
49.9k
  if (!zend_is_allowed_in_const_expr(ast->kind)) {
11884
57
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11885
57
  }
11886
11887
49.8k
  switch (ast->kind) {
11888
3.09k
    case ZEND_AST_CLASS_CONST:
11889
3.09k
      zend_compile_const_expr_class_const(ast_ptr);
11890
3.09k
      break;
11891
60
    case ZEND_AST_CLASS_NAME:
11892
60
      zend_compile_const_expr_class_name(ast_ptr);
11893
60
      break;
11894
22.0k
    case ZEND_AST_CONST:
11895
22.0k
      zend_compile_const_expr_const(ast_ptr);
11896
22.0k
      break;
11897
386
    case ZEND_AST_MAGIC_CONST:
11898
386
      zend_compile_const_expr_magic_const(ast_ptr);
11899
386
      break;
11900
268
    case ZEND_AST_CAST:
11901
268
      if (ast->attr == IS_OBJECT && !ctx->allow_dynamic) {
11902
8
        zend_error_noreturn(E_COMPILE_ERROR,
11903
8
          "Object casts are not supported in this context");
11904
8
      }
11905
260
      break;
11906
811
    case ZEND_AST_NEW:
11907
811
      if (!ctx->allow_dynamic) {
11908
6
        zend_error_noreturn(E_COMPILE_ERROR,
11909
6
          "New expressions are not supported in this context");
11910
6
      }
11911
805
      zend_compile_const_expr_new(ast_ptr);
11912
805
      break;
11913
796
    case ZEND_AST_ARG_LIST:
11914
796
      zend_compile_const_expr_args(ast_ptr);
11915
796
      break;
11916
149
    case ZEND_AST_CLOSURE:
11917
149
      zend_compile_const_expr_closure(ast_ptr);
11918
      /* Return, because we do not want to traverse the children. */
11919
149
      return;
11920
287
    case ZEND_AST_CALL:
11921
424
    case ZEND_AST_STATIC_CALL:
11922
424
      zend_compile_const_expr_fcc(ast_ptr);
11923
424
      break;
11924
49.8k
  }
11925
11926
49.6k
  zend_ast_apply(ast, zend_compile_const_expr, context);
11927
49.6k
}
11928
/* }}} */
11929
11930
void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynamic) /* {{{ */
11931
78.0k
{
11932
78.0k
  const_expr_context context;
11933
78.0k
  context.allow_dynamic = allow_dynamic;
11934
11935
78.0k
  zend_eval_const_expr(ast_ptr);
11936
78.0k
  zend_compile_const_expr(ast_ptr, &context);
11937
78.0k
  if ((*ast_ptr)->kind != ZEND_AST_ZVAL) {
11938
    /* Replace with compiled AST zval representation. */
11939
26.5k
    zval ast_zv;
11940
26.5k
    ZVAL_AST(&ast_zv, zend_ast_copy(*ast_ptr));
11941
26.5k
    zend_ast_destroy(*ast_ptr);
11942
26.5k
    *ast_ptr = zend_ast_create_zval(&ast_zv);
11943
26.5k
  }
11944
78.0k
  ZVAL_COPY(result, zend_ast_get_zval(*ast_ptr));
11945
78.0k
}
11946
/* }}} */
11947
11948
/* Same as compile_stmt, but with early binding */
11949
void zend_compile_top_stmt(zend_ast *ast) /* {{{ */
11950
646k
{
11951
646k
  if (!ast) {
11952
85.0k
    return;
11953
85.0k
  }
11954
11955
561k
  if (ast->kind == ZEND_AST_STMT_LIST) {
11956
104k
    const zend_ast_list *list = zend_ast_get_list(ast);
11957
104k
    uint32_t i;
11958
667k
    for (i = 0; i < list->children; ++i) {
11959
563k
      zend_compile_top_stmt(list->child[i]);
11960
563k
    }
11961
104k
    return;
11962
104k
  }
11963
11964
457k
  if (ast->kind == ZEND_AST_FUNC_DECL) {
11965
15.3k
    CG(zend_lineno) = ast->lineno;
11966
15.3k
    zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_TOPLEVEL);
11967
15.3k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11968
442k
  } else if (ast->kind == ZEND_AST_CLASS) {
11969
39.9k
    CG(zend_lineno) = ast->lineno;
11970
39.9k
    zend_compile_class_decl(NULL, ast, true);
11971
39.9k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11972
402k
  } else {
11973
402k
    zend_compile_stmt(ast);
11974
402k
  }
11975
457k
  if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) {
11976
447k
    zend_verify_namespace();
11977
447k
  }
11978
457k
}
11979
/* }}} */
11980
11981
static void zend_compile_stmt(zend_ast *ast) /* {{{ */
11982
6.10M
{
11983
6.10M
  if (!ast) {
11984
393k
    return;
11985
393k
  }
11986
11987
5.70M
  CG(zend_lineno) = ast->lineno;
11988
11989
5.70M
  if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) {
11990
0
    zend_do_extended_stmt(NULL);
11991
0
  }
11992
11993
5.70M
  switch (ast->kind) {
11994
1.35M
    case ZEND_AST_STMT_LIST:
11995
1.35M
      zend_compile_stmt_list(ast);
11996
1.35M
      break;
11997
2.90k
    case ZEND_AST_GLOBAL:
11998
2.90k
      zend_compile_global_var(ast);
11999
2.90k
      break;
12000
14.8k
    case ZEND_AST_STATIC:
12001
14.8k
      zend_compile_static_var(ast);
12002
14.8k
      break;
12003
6.75k
    case ZEND_AST_UNSET:
12004
6.75k
      zend_compile_unset(ast);
12005
6.75k
      break;
12006
34.9k
    case ZEND_AST_RETURN:
12007
34.9k
      zend_compile_return(ast);
12008
34.9k
      break;
12009
1.35M
    case ZEND_AST_ECHO:
12010
1.35M
      zend_compile_echo(ast);
12011
1.35M
      break;
12012
959
    case ZEND_AST_BREAK:
12013
1.82k
    case ZEND_AST_CONTINUE:
12014
1.82k
      zend_compile_break_continue(ast);
12015
1.82k
      break;
12016
1.42k
    case ZEND_AST_GOTO:
12017
1.42k
      zend_compile_goto(ast);
12018
1.42k
      break;
12019
2.59k
    case ZEND_AST_LABEL:
12020
2.59k
      zend_compile_label(ast);
12021
2.59k
      break;
12022
1.74k
    case ZEND_AST_WHILE:
12023
1.74k
      zend_compile_while(ast);
12024
1.74k
      break;
12025
484
    case ZEND_AST_DO_WHILE:
12026
484
      zend_compile_do_while(ast);
12027
484
      break;
12028
12.2k
    case ZEND_AST_FOR:
12029
12.2k
      zend_compile_for(ast);
12030
12.2k
      break;
12031
18.0k
    case ZEND_AST_FOREACH:
12032
18.0k
      zend_compile_foreach(ast);
12033
18.0k
      break;
12034
46.6k
    case ZEND_AST_IF:
12035
46.6k
      zend_compile_if(ast);
12036
46.6k
      break;
12037
5.59k
    case ZEND_AST_SWITCH:
12038
5.59k
      zend_compile_switch(ast);
12039
5.59k
      break;
12040
30.1k
    case ZEND_AST_TRY:
12041
30.1k
      zend_compile_try(ast);
12042
30.1k
      break;
12043
4.40k
    case ZEND_AST_DECLARE:
12044
4.40k
      zend_compile_declare(ast);
12045
4.40k
      break;
12046
1.55k
    case ZEND_AST_FUNC_DECL:
12047
62.0k
    case ZEND_AST_METHOD:
12048
62.0k
      zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_NESTED);
12049
62.0k
      break;
12050
6.39k
    case ZEND_AST_ENUM_CASE:
12051
6.39k
      zend_compile_enum_case(ast);
12052
6.39k
      break;
12053
28.6k
    case ZEND_AST_PROP_GROUP:
12054
28.6k
      zend_compile_prop_group(ast);
12055
28.6k
      break;
12056
6.69k
    case ZEND_AST_CLASS_CONST_GROUP:
12057
6.69k
      zend_compile_class_const_group(ast);
12058
6.69k
      break;
12059
71.3k
    case ZEND_AST_USE_TRAIT:
12060
71.3k
      zend_compile_use_trait(ast);
12061
71.3k
      break;
12062
130k
    case ZEND_AST_CLASS:
12063
130k
      zend_compile_class_decl(NULL, ast, false);
12064
130k
      break;
12065
242
    case ZEND_AST_GROUP_USE:
12066
242
      zend_compile_group_use(ast);
12067
242
      break;
12068
1.57k
    case ZEND_AST_USE:
12069
1.57k
      zend_compile_use(ast);
12070
1.57k
      break;
12071
4.12k
    case ZEND_AST_CONST_DECL:
12072
4.12k
      zend_compile_const_decl(ast);
12073
4.12k
      break;
12074
4.74k
    case ZEND_AST_NAMESPACE:
12075
4.74k
      zend_compile_namespace(ast);
12076
4.74k
      break;
12077
70
    case ZEND_AST_HALT_COMPILER:
12078
70
      zend_compile_halt_compiler(ast);
12079
70
      break;
12080
2.64k
    case ZEND_AST_THROW:
12081
2.64k
      zend_compile_expr(NULL, ast);
12082
2.64k
      break;
12083
424
    case ZEND_AST_CAST_VOID:
12084
424
      zend_compile_void_cast(NULL, ast);
12085
424
      break;
12086
123k
    case ZEND_AST_ASSIGN: {
12087
123k
      znode result;
12088
123k
      zend_compile_assign(&result, ast, /* stmt */ true, BP_VAR_R);
12089
123k
      zend_do_free(&result);
12090
123k
      return;
12091
1.55k
    }
12092
7.21k
    case ZEND_AST_ASSIGN_REF:
12093
7.21k
      zend_compile_assign_ref(NULL, ast, BP_VAR_R);
12094
7.21k
      return;
12095
2.35M
    default:
12096
2.35M
    {
12097
2.35M
      znode result;
12098
2.35M
      zend_compile_expr(&result, ast);
12099
2.35M
      zend_do_free(&result);
12100
2.35M
    }
12101
5.70M
  }
12102
12103
5.56M
  if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) {
12104
12.6k
    zend_emit_tick();
12105
12.6k
  }
12106
5.56M
}
12107
/* }}} */
12108
12109
static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */
12110
30.2M
{
12111
  /* CG(zend_lineno) = ast->lineno; */
12112
30.2M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
12113
12114
30.2M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
12115
190k
    zend_compile_memoized_expr(result, ast, BP_VAR_R);
12116
190k
    return;
12117
190k
  }
12118
12119
30.0M
  switch (ast->kind) {
12120
3.66M
    case ZEND_AST_ZVAL:
12121
3.66M
      ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast));
12122
3.66M
      result->op_type = IS_CONST;
12123
3.66M
      return;
12124
134k
    case ZEND_AST_ZNODE:
12125
134k
      *result = *zend_ast_get_znode(ast);
12126
134k
      return;
12127
441k
    case ZEND_AST_VAR:
12128
469k
    case ZEND_AST_DIM:
12129
497k
    case ZEND_AST_PROP:
12130
539k
    case ZEND_AST_NULLSAFE_PROP:
12131
543k
    case ZEND_AST_STATIC_PROP:
12132
2.83M
    case ZEND_AST_CALL:
12133
2.86M
    case ZEND_AST_METHOD_CALL:
12134
2.86M
    case ZEND_AST_NULLSAFE_METHOD_CALL:
12135
2.89M
    case ZEND_AST_STATIC_CALL:
12136
2.89M
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
12137
2.96M
    case ZEND_AST_PIPE:
12138
2.96M
      zend_compile_var(result, ast, BP_VAR_R, false);
12139
2.96M
      return;
12140
140k
    case ZEND_AST_ASSIGN:
12141
140k
      zend_compile_assign(result, ast, /* stmt */ false, BP_VAR_R);
12142
140k
      return;
12143
1.03k
    case ZEND_AST_ASSIGN_REF:
12144
1.03k
      zend_compile_assign_ref(result, ast, BP_VAR_R);
12145
1.03k
      return;
12146
81.8k
    case ZEND_AST_NEW:
12147
81.8k
      zend_compile_new(result, ast);
12148
81.8k
      return;
12149
98.7k
    case ZEND_AST_ASSIGN_OP:
12150
98.7k
      zend_compile_compound_assign(result, ast);
12151
98.7k
      return;
12152
2.91M
    case ZEND_AST_BINARY_OP:
12153
2.91M
      zend_compile_binary_op(result, ast);
12154
2.91M
      return;
12155
211k
    case ZEND_AST_GREATER:
12156
255k
    case ZEND_AST_GREATER_EQUAL:
12157
255k
      zend_compile_greater(result, ast);
12158
255k
      return;
12159
1.22M
    case ZEND_AST_UNARY_OP:
12160
1.22M
      zend_compile_unary_op(result, ast);
12161
1.22M
      return;
12162
19.9k
    case ZEND_AST_UNARY_PLUS:
12163
83.3k
    case ZEND_AST_UNARY_MINUS:
12164
83.3k
      zend_compile_unary_pm(result, ast);
12165
83.3k
      return;
12166
9.79k
    case ZEND_AST_AND:
12167
15.2k
    case ZEND_AST_OR:
12168
15.2k
      zend_compile_short_circuiting(result, ast);
12169
15.2k
      return;
12170
7.16k
    case ZEND_AST_POST_INC:
12171
11.7k
    case ZEND_AST_POST_DEC:
12172
11.7k
      zend_compile_post_incdec(result, ast);
12173
11.7k
      return;
12174
2.01k
    case ZEND_AST_PRE_INC:
12175
3.24k
    case ZEND_AST_PRE_DEC:
12176
3.24k
      zend_compile_pre_incdec(result, ast);
12177
3.24k
      return;
12178
2.90k
    case ZEND_AST_CAST:
12179
2.90k
      zend_compile_cast(result, ast);
12180
2.90k
      return;
12181
7.25k
    case ZEND_AST_CONDITIONAL:
12182
7.25k
      zend_compile_conditional(result, ast);
12183
7.25k
      return;
12184
1.19M
    case ZEND_AST_COALESCE:
12185
1.19M
      zend_compile_coalesce(result, ast);
12186
1.19M
      return;
12187
49.7k
    case ZEND_AST_ASSIGN_COALESCE:
12188
49.7k
      zend_compile_assign_coalesce(result, ast);
12189
49.7k
      return;
12190
5.00k
    case ZEND_AST_PRINT:
12191
5.00k
      zend_compile_print(result, ast);
12192
5.00k
      return;
12193
43.4k
    case ZEND_AST_YIELD:
12194
43.4k
      zend_compile_yield(result, ast);
12195
43.4k
      return;
12196
1.53k
    case ZEND_AST_YIELD_FROM:
12197
1.53k
      zend_compile_yield_from(result, ast);
12198
1.53k
      return;
12199
661
    case ZEND_AST_INSTANCEOF:
12200
661
      zend_compile_instanceof(result, ast);
12201
661
      return;
12202
7.87k
    case ZEND_AST_INCLUDE_OR_EVAL:
12203
7.87k
      zend_compile_include_or_eval(result, ast);
12204
7.87k
      return;
12205
5.85k
    case ZEND_AST_ISSET:
12206
7.12k
    case ZEND_AST_EMPTY:
12207
7.12k
      zend_compile_isset_or_empty(result, ast);
12208
7.12k
      return;
12209
9.69M
    case ZEND_AST_SILENCE:
12210
9.69M
      zend_compile_silence(result, ast);
12211
9.69M
      return;
12212
13.2k
    case ZEND_AST_SHELL_EXEC:
12213
13.2k
      zend_compile_shell_exec(result, ast);
12214
13.2k
      return;
12215
85.6k
    case ZEND_AST_ARRAY:
12216
85.6k
      zend_compile_array(result, ast);
12217
85.6k
      return;
12218
6.20M
    case ZEND_AST_CONST:
12219
6.20M
      zend_compile_const(result, ast);
12220
6.20M
      return;
12221
113k
    case ZEND_AST_CLASS_CONST:
12222
113k
      zend_compile_class_const(result, ast);
12223
113k
      return;
12224
4.65k
    case ZEND_AST_CLASS_NAME:
12225
4.65k
      zend_compile_class_name(result, ast);
12226
4.65k
      return;
12227
46.4k
    case ZEND_AST_ENCAPS_LIST:
12228
46.4k
      zend_compile_encaps_list(result, ast);
12229
46.4k
      return;
12230
17.7k
    case ZEND_AST_MAGIC_CONST:
12231
17.7k
      zend_compile_magic_const(result, ast);
12232
17.7k
      return;
12233
914k
    case ZEND_AST_CLOSURE:
12234
931k
    case ZEND_AST_ARROW_FUNC:
12235
931k
      zend_compile_func_decl(result, ast, FUNC_DECL_LEVEL_NESTED);
12236
931k
      return;
12237
3.39k
    case ZEND_AST_THROW:
12238
3.39k
      zend_compile_throw(result, ast);
12239
3.39k
      return;
12240
2.53k
    case ZEND_AST_MATCH:
12241
2.53k
      zend_compile_match(result, ast);
12242
2.53k
      return;
12243
0
    default:
12244
0
      ZEND_ASSERT(0 /* not supported */);
12245
30.0M
  }
12246
30.0M
}
12247
/* }}} */
12248
12249
static void zend_compile_expr(znode *result, zend_ast *ast)
12250
30.2M
{
12251
30.2M
  zend_check_stack_limit();
12252
12253
30.2M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12254
30.2M
  zend_compile_expr_inner(result, ast);
12255
30.2M
  zend_short_circuiting_commit(checkpoint, result, ast);
12256
30.2M
#if ZEND_DEBUG
12257
30.2M
  if (result) {
12258
    /* BP_VAR_R is not allowed to produce IS_VAR. */
12259
30.1M
    ZEND_ASSERT(result->op_type != IS_VAR);
12260
30.1M
  }
12261
30.2M
#endif
12262
30.2M
}
12263
12264
static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
12265
4.72M
{
12266
4.72M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
12267
12268
4.72M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
12269
177k
    switch (ast->kind) {
12270
12.2k
      case ZEND_AST_CALL:
12271
76.7k
      case ZEND_AST_METHOD_CALL:
12272
76.7k
      case ZEND_AST_NULLSAFE_METHOD_CALL:
12273
78.1k
      case ZEND_AST_STATIC_CALL:
12274
78.1k
        zend_compile_memoized_expr(result, ast, BP_VAR_W);
12275
        /* This might not actually produce an opcode, e.g. for expressions evaluated at comptime. */
12276
78.1k
        return NULL;
12277
177k
    }
12278
177k
  }
12279
12280
4.65M
  switch (ast->kind) {
12281
535k
    case ZEND_AST_VAR:
12282
535k
      return zend_compile_simple_var(result, ast, type, false);
12283
136k
    case ZEND_AST_DIM:
12284
136k
      return zend_compile_dim(result, ast, type, by_ref);
12285
36.2k
    case ZEND_AST_PROP:
12286
78.4k
    case ZEND_AST_NULLSAFE_PROP:
12287
78.4k
      return zend_compile_prop(result, ast, type, by_ref);
12288
8.72k
    case ZEND_AST_STATIC_PROP:
12289
8.72k
      return zend_compile_static_prop(result, ast, type, by_ref, false);
12290
2.49M
    case ZEND_AST_CALL:
12291
2.49M
      zend_compile_call(result, ast, type);
12292
2.49M
      return NULL;
12293
0
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
12294
0
      zend_compile_parent_property_hook_call(result, ast, type);
12295
0
      return NULL;
12296
77.9k
    case ZEND_AST_METHOD_CALL:
12297
80.6k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
12298
80.6k
      zend_compile_method_call(result, ast, type);
12299
80.6k
      return NULL;
12300
34.1k
    case ZEND_AST_STATIC_CALL:
12301
34.1k
      zend_compile_static_call(result, ast, type);
12302
34.1k
      return NULL;
12303
64.5k
    case ZEND_AST_PIPE:
12304
64.5k
      zend_compile_pipe(result, ast, type);
12305
64.5k
      return NULL;
12306
2.66k
    case ZEND_AST_ZNODE:
12307
2.66k
      *result = *zend_ast_get_znode(ast);
12308
2.66k
      return NULL;
12309
2.43k
    case ZEND_AST_ASSIGN_REF:
12310
2.43k
      zend_compile_assign_ref(result, ast, type);
12311
2.43k
      return NULL;
12312
23
    case ZEND_AST_ASSIGN:
12313
23
      zend_compile_assign(result, ast, false, type);
12314
23
      return NULL;
12315
1.21M
    default:
12316
1.21M
      if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
12317
180
        zend_error_noreturn(E_COMPILE_ERROR,
12318
180
          "Cannot use temporary expression in write context");
12319
180
      }
12320
12321
1.21M
      zend_compile_expr(result, ast);
12322
1.21M
      return NULL;
12323
4.65M
  }
12324
4.65M
}
12325
12326
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12327
4.72M
{
12328
4.72M
  zend_check_stack_limit();
12329
12330
4.72M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12331
4.72M
  zend_op *opcode = zend_compile_var_inner(result, ast, type, by_ref);
12332
4.72M
  zend_short_circuiting_commit(checkpoint, result, ast);
12333
4.72M
#if ZEND_DEBUG
12334
4.72M
  if (result
12335
4.72M
   && (type == BP_VAR_R || type == BP_VAR_IS)
12336
   /* Don't check memoized result, as it will force BP_VAR_W even for BP_VAR_IS. */
12337
4.45M
   && CG(memoize_mode) == ZEND_MEMOIZE_NONE
12338
4.72M
  ) {
12339
    /* BP_VAR_{R,IS} is not allowed to produce IS_VAR. */
12340
4.36M
    ZEND_ASSERT(result->op_type != IS_VAR);
12341
4.36M
  }
12342
4.72M
#endif
12343
4.72M
  return opcode;
12344
4.72M
}
12345
12346
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12347
711k
{
12348
711k
  zend_check_stack_limit();
12349
12350
711k
  switch (ast->kind) {
12351
451k
    case ZEND_AST_VAR:
12352
451k
      return zend_compile_simple_var(result, ast, type, true);
12353
111k
    case ZEND_AST_DIM:
12354
111k
      return zend_delayed_compile_dim(result, ast, type, by_ref);
12355
21.6k
    case ZEND_AST_PROP:
12356
25.7k
    case ZEND_AST_NULLSAFE_PROP:
12357
25.7k
    {
12358
25.7k
      zend_op *opline = zend_delayed_compile_prop(result, ast, type);
12359
25.7k
      if (by_ref) {
12360
1.11k
        opline->extended_value |= ZEND_FETCH_REF;
12361
1.11k
      }
12362
25.7k
      return opline;
12363
21.6k
    }
12364
6.59k
    case ZEND_AST_STATIC_PROP:
12365
6.59k
      return zend_compile_static_prop(result, ast, type, by_ref, true);
12366
115k
    default:
12367
115k
      return zend_compile_var(result, ast, type, false);
12368
711k
  }
12369
711k
}
12370
/* }}} */
12371
12372
bool zend_try_ct_eval_cast(zval *result, uint32_t type, zval *op1)
12373
15.8k
{
12374
  /* NAN warns when casting */
12375
15.8k
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))) {
12376
0
    return false;
12377
0
  }
12378
15.8k
  switch (type) {
12379
47
    case _IS_BOOL:
12380
47
      ZVAL_BOOL(result, zend_is_true(op1));
12381
47
      return true;
12382
289
    case IS_LONG:
12383
289
      if (Z_TYPE_P(op1) == IS_DOUBLE && !ZEND_DOUBLE_FITS_LONG(Z_DVAL_P((op1)))) {
12384
34
        return false;
12385
34
      }
12386
255
      ZVAL_LONG(result, zval_get_long(op1));
12387
255
      return true;
12388
598
    case IS_DOUBLE:
12389
598
      ZVAL_DOUBLE(result, zval_get_double(op1));
12390
598
      return true;
12391
14.1k
    case IS_STRING:
12392
      /* Conversion from double to string takes into account run-time
12393
         'precision' setting and cannot be evaluated at compile-time */
12394
14.1k
      if (Z_TYPE_P(op1) != IS_ARRAY && Z_TYPE_P(op1) != IS_DOUBLE) {
12395
13.3k
        ZVAL_STR(result, zval_get_string(op1));
12396
13.3k
        return true;
12397
13.3k
      }
12398
746
      break;
12399
746
    case IS_ARRAY:
12400
96
      ZVAL_COPY(result, op1);
12401
96
      convert_to_array(result);
12402
96
      return true;
12403
15.8k
  }
12404
1.40k
  return false;
12405
15.8k
}
12406
12407
static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
12408
8.95M
{
12409
8.95M
  zend_ast *ast = *ast_ptr;
12410
8.95M
  zval result;
12411
12412
8.95M
  if (!ast) {
12413
2.25M
    return;
12414
2.25M
  }
12415
12416
6.69M
  zend_check_stack_limit();
12417
12418
6.69M
  switch (ast->kind) {
12419
63.7k
    case ZEND_AST_BINARY_OP:
12420
63.7k
      zend_eval_const_expr(&ast->child[0]);
12421
63.7k
      zend_eval_const_expr(&ast->child[1]);
12422
63.7k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12423
27.6k
        return;
12424
27.6k
      }
12425
12426
36.0k
      if (!zend_try_ct_eval_binary_op(&result, ast->attr,
12427
36.0k
          zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
12428
36.0k
      ) {
12429
18.3k
        return;
12430
18.3k
      }
12431
17.6k
      break;
12432
17.6k
    case ZEND_AST_GREATER:
12433
4.48k
    case ZEND_AST_GREATER_EQUAL:
12434
4.48k
      zend_eval_const_expr(&ast->child[0]);
12435
4.48k
      zend_eval_const_expr(&ast->child[1]);
12436
4.48k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12437
2.92k
        return;
12438
2.92k
      }
12439
12440
1.56k
      zend_ct_eval_greater(&result, ast->kind,
12441
1.56k
        zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]));
12442
1.56k
      break;
12443
1.74k
    case ZEND_AST_AND:
12444
3.04k
    case ZEND_AST_OR:
12445
3.04k
    {
12446
3.04k
      bool child0_is_true, child1_is_true;
12447
3.04k
      zend_eval_const_expr(&ast->child[0]);
12448
3.04k
      zend_eval_const_expr(&ast->child[1]);
12449
3.04k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12450
1.40k
        return;
12451
1.40k
      }
12452
12453
1.64k
      child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0]));
12454
1.64k
      if (child0_is_true == (ast->kind == ZEND_AST_OR)) {
12455
245
        ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR);
12456
245
        break;
12457
245
      }
12458
12459
1.39k
      if (ast->child[1]->kind != ZEND_AST_ZVAL) {
12460
377
        return;
12461
377
      }
12462
12463
1.01k
      child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1]));
12464
1.01k
      if (ast->kind == ZEND_AST_OR) {
12465
346
        ZVAL_BOOL(&result, child0_is_true || child1_is_true);
12466
673
      } else {
12467
673
        ZVAL_BOOL(&result, child0_is_true && child1_is_true);
12468
673
      }
12469
1.01k
      break;
12470
1.39k
    }
12471
8.50k
    case ZEND_AST_UNARY_OP:
12472
8.50k
      zend_eval_const_expr(&ast->child[0]);
12473
8.50k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12474
2.38k
        return;
12475
2.38k
      }
12476
12477
6.11k
      if (!zend_try_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12478
456
        return;
12479
456
      }
12480
5.65k
      break;
12481
5.65k
    case ZEND_AST_UNARY_PLUS:
12482
21.0k
    case ZEND_AST_UNARY_MINUS:
12483
21.0k
      zend_eval_const_expr(&ast->child[0]);
12484
21.0k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12485
4.04k
        return;
12486
4.04k
      }
12487
12488
16.9k
      if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) {
12489
4.20k
        return;
12490
4.20k
      }
12491
12.7k
      break;
12492
22.6k
    case ZEND_AST_COALESCE:
12493
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12494
22.6k
      if (ast->child[0]->kind == ZEND_AST_DIM) {
12495
514
        ast->child[0]->attr |= ZEND_DIM_IS;
12496
514
      }
12497
22.6k
      zend_eval_const_expr(&ast->child[0]);
12498
12499
22.6k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12500
        /* ensure everything was compile-time evaluated at least once */
12501
22.3k
        zend_eval_const_expr(&ast->child[1]);
12502
22.3k
        return;
12503
22.3k
      }
12504
12505
327
      if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) {
12506
255
        zend_eval_const_expr(&ast->child[1]);
12507
255
        *ast_ptr = ast->child[1];
12508
255
        ast->child[1] = NULL;
12509
255
        zend_ast_destroy(ast);
12510
255
      } else {
12511
72
        *ast_ptr = ast->child[0];
12512
72
        ast->child[0] = NULL;
12513
72
        zend_ast_destroy(ast);
12514
72
      }
12515
327
      return;
12516
2.09k
    case ZEND_AST_CONDITIONAL:
12517
2.09k
    {
12518
2.09k
      zend_ast **child, *child_ast;
12519
2.09k
      zend_eval_const_expr(&ast->child[0]);
12520
2.09k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12521
        /* ensure everything was compile-time evaluated at least once */
12522
1.83k
        if (ast->child[1]) {
12523
1.42k
          zend_eval_const_expr(&ast->child[1]);
12524
1.42k
        }
12525
1.83k
        zend_eval_const_expr(&ast->child[2]);
12526
1.83k
        return;
12527
1.83k
      }
12528
12529
258
      child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];
12530
258
      if (*child == NULL) {
12531
202
        child--;
12532
202
      }
12533
258
      child_ast = *child;
12534
258
      *child = NULL;
12535
258
      zend_ast_destroy(ast);
12536
258
      *ast_ptr = child_ast;
12537
258
      zend_eval_const_expr(ast_ptr);
12538
258
      return;
12539
2.09k
    }
12540
910k
    case ZEND_AST_DIM:
12541
910k
    {
12542
      /* constant expression should be always read context ... */
12543
910k
      const zval *container, *dim;
12544
12545
910k
      if (ast->child[1] == NULL) {
12546
43
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
12547
43
      }
12548
12549
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12550
910k
      if ((ast->attr & ZEND_DIM_IS) && ast->child[0]->kind == ZEND_AST_DIM) {
12551
982
        ast->child[0]->attr |= ZEND_DIM_IS;
12552
982
      }
12553
12554
910k
      zend_eval_const_expr(&ast->child[0]);
12555
910k
      zend_eval_const_expr(&ast->child[1]);
12556
910k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12557
894k
        return;
12558
894k
      }
12559
12560
16.1k
      container = zend_ast_get_zval(ast->child[0]);
12561
16.1k
      dim = zend_ast_get_zval(ast->child[1]);
12562
12563
16.1k
      if (Z_TYPE_P(container) == IS_ARRAY) {
12564
9.28k
        zval *el;
12565
9.28k
        if (Z_TYPE_P(dim) == IS_LONG) {
12566
1.73k
          el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim));
12567
1.73k
          if (el) {
12568
486
            ZVAL_COPY(&result, el);
12569
1.25k
          } else {
12570
1.25k
            return;
12571
1.25k
          }
12572
7.55k
        } else if (Z_TYPE_P(dim) == IS_STRING) {
12573
7.27k
          el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim));
12574
7.27k
          if (el) {
12575
351
            ZVAL_COPY(&result, el);
12576
6.92k
          } else {
12577
6.92k
            return;
12578
6.92k
          }
12579
7.27k
        } else {
12580
276
          return; /* warning... handle at runtime */
12581
276
        }
12582
9.28k
      } else if (Z_TYPE_P(container) == IS_STRING) {
12583
6.58k
        zend_long offset;
12584
6.58k
        uint8_t c;
12585
6.58k
        if (Z_TYPE_P(dim) == IS_LONG) {
12586
5.39k
          offset = Z_LVAL_P(dim);
12587
5.39k
        } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) {
12588
857
          return;
12589
857
        }
12590
5.73k
        if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
12591
5.60k
          return;
12592
5.60k
        }
12593
131
        c = (uint8_t) Z_STRVAL_P(container)[offset];
12594
131
        ZVAL_CHAR(&result, c);
12595
303
      } else if (Z_TYPE_P(container) <= IS_FALSE) {
12596
12
        return; /* warning... handle at runtime */
12597
291
      } else {
12598
291
        return;
12599
291
      }
12600
968
      break;
12601
16.1k
    }
12602
1.93M
    case ZEND_AST_ARRAY:
12603
1.93M
      if (!zend_try_ct_eval_array(&result, ast)) {
12604
1.90M
        return;
12605
1.90M
      }
12606
33.1k
      break;
12607
33.1k
    case ZEND_AST_MAGIC_CONST:
12608
1.47k
      if (!zend_try_ct_eval_magic_const(&result, ast)) {
12609
386
        return;
12610
386
      }
12611
1.09k
      break;
12612
110k
    case ZEND_AST_CONST:
12613
110k
    {
12614
110k
      zend_ast *name_ast = ast->child[0];
12615
110k
      bool is_fully_qualified;
12616
110k
      zend_string *resolved_name = zend_resolve_const_name(
12617
110k
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
12618
12619
110k
      if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
12620
104k
        zend_string_release_ex(resolved_name, 0);
12621
104k
        return;
12622
104k
      }
12623
12624
5.37k
      zend_string_release_ex(resolved_name, 0);
12625
5.37k
      break;
12626
110k
    }
12627
937k
    case ZEND_AST_CLASS_CONST:
12628
937k
    {
12629
937k
      zend_ast *class_ast;
12630
937k
      zend_ast *name_ast;
12631
937k
      zend_string *resolved_name;
12632
12633
937k
      zend_eval_const_expr(&ast->child[0]);
12634
937k
      zend_eval_const_expr(&ast->child[1]);
12635
12636
937k
      if (UNEXPECTED(ast->child[1]->kind != ZEND_AST_ZVAL
12637
937k
        || Z_TYPE_P(zend_ast_get_zval(ast->child[1])) != IS_STRING)) {
12638
2.76k
        return;
12639
2.76k
      }
12640
12641
935k
      class_ast = ast->child[0];
12642
935k
      name_ast = ast->child[1];
12643
12644
935k
      if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) {
12645
860k
        return;
12646
860k
      }
12647
12648
74.1k
      resolved_name = zend_resolve_class_name_ast(class_ast);
12649
74.1k
      if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) {
12650
73.9k
        zend_string_release_ex(resolved_name, 0);
12651
73.9k
        return;
12652
73.9k
      }
12653
12654
196
      zend_string_release_ex(resolved_name, 0);
12655
196
      break;
12656
74.1k
    }
12657
2.76k
    case ZEND_AST_CLASS_NAME:
12658
2.76k
    {
12659
2.76k
      zend_ast *class_ast = ast->child[0];
12660
2.76k
      if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) {
12661
1.19k
        return;
12662
1.19k
      }
12663
1.56k
      break;
12664
2.76k
    }
12665
    // TODO: We should probably use zend_ast_apply to recursively walk nodes without
12666
    // special handling. It is required that all nodes that are part of a const expr
12667
    // are visited. Probably we should be distinguishing evaluation of const expr and
12668
    // normal exprs here.
12669
3.29k
    case ZEND_AST_ARG_LIST:
12670
3.29k
    {
12671
3.29k
      zend_ast_list *list = zend_ast_get_list(ast);
12672
4.52k
      for (uint32_t i = 0; i < list->children; i++) {
12673
1.22k
        zend_eval_const_expr(&list->child[i]);
12674
1.22k
      }
12675
3.29k
      return;
12676
2.76k
    }
12677
3.30k
    case ZEND_AST_NEW:
12678
3.30k
      zend_eval_const_expr(&ast->child[0]);
12679
3.30k
      zend_eval_const_expr(&ast->child[1]);
12680
3.30k
      return;
12681
158
    case ZEND_AST_NAMED_ARG:
12682
158
      zend_eval_const_expr(&ast->child[1]);
12683
158
      return;
12684
6.37k
    case ZEND_AST_CONST_ENUM_INIT:
12685
6.37k
      zend_eval_const_expr(&ast->child[3]);
12686
6.37k
      return;
12687
2.47k
    case ZEND_AST_PROP:
12688
2.92k
    case ZEND_AST_NULLSAFE_PROP:
12689
2.92k
      zend_eval_const_expr(&ast->child[0]);
12690
2.92k
      zend_eval_const_expr(&ast->child[1]);
12691
2.92k
      return;
12692
2.26k
    case ZEND_AST_CAST:
12693
2.26k
      if (ast->attr == IS_NULL) {
12694
5
        zend_error_noreturn(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
12695
5
      }
12696
2.25k
      zend_eval_const_expr(&ast->child[0]);
12697
2.25k
      if (ast->child[0]->kind == ZEND_AST_ZVAL
12698
1.66k
       && zend_try_ct_eval_cast(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12699
1.30k
        break;
12700
1.30k
      }
12701
953
      return;
12702
2.64M
    default:
12703
2.64M
      return;
12704
6.69M
  }
12705
12706
82.0k
  zend_ast_destroy(ast);
12707
82.0k
  *ast_ptr = zend_ast_create_zval(&result);
12708
82.0k
}
12709
/* }}} */