Coverage Report

Created: 2025-12-31 07:28

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
47.1M
#define SET_NODE(target, src) do { \
44
47.1M
    target ## _type = (src)->op_type; \
45
47.1M
    if ((src)->op_type == IS_CONST) { \
46
5.27M
      target.constant = zend_add_literal(&(src)->u.constant); \
47
41.9M
    } else { \
48
41.9M
      target = (src)->u.op; \
49
41.9M
    } \
50
47.1M
  } while (0)
51
52
36.2M
#define GET_NODE(target, src) do { \
53
36.2M
    (target)->op_type = src ## _type; \
54
36.2M
    if ((target)->op_type == IS_CONST) { \
55
601
      ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
56
36.2M
    } else { \
57
36.2M
      (target)->u.op = src; \
58
36.2M
    } \
59
36.2M
  } while (0)
60
61
74.7M
#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
13.4M
static inline uint32_t zend_alloc_cache_slots(unsigned count) {
71
13.4M
  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
13.4M
  zend_op_array *op_array = CG(active_op_array);
79
13.4M
  uint32_t ret = op_array->cache_size;
80
13.4M
  op_array->cache_size += count * sizeof(void*);
81
13.4M
  return ret;
82
13.4M
}
83
84
13.0M
static inline uint32_t zend_alloc_cache_slot(void) {
85
13.0M
  return zend_alloc_cache_slots(1);
86
13.0M
}
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);
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
57.3M
{
121
57.3M
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
122
0
    zend_stack_limit_error();
123
0
  }
124
57.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
72.6M
{
133
72.6M
  MAKE_NOP(op);
134
72.6M
  op->extended_value = 0;
135
72.6M
  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
72.6M
}
145
146
static zend_always_inline uint32_t get_next_op_number(void)
147
11.8M
{
148
11.8M
  return CG(active_op_array)->last;
149
11.8M
}
150
151
static zend_op *get_next_op(void)
152
72.3M
{
153
72.3M
  zend_op_array *op_array = CG(active_op_array);
154
72.3M
  uint32_t next_op_num = op_array->last++;
155
72.3M
  zend_op *next_op;
156
157
72.3M
  if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) {
158
647k
    CG(context).opcodes_size *= 4;
159
647k
    op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op));
160
647k
  }
161
162
72.3M
  next_op = &(op_array->opcodes[next_op_num]);
163
164
72.3M
  init_op(next_op);
165
166
72.3M
  return next_op;
167
72.3M
}
168
169
static zend_brk_cont_element *get_next_brk_cont_element(void)
170
54.2k
{
171
54.2k
  CG(context).last_brk_cont++;
172
54.2k
  CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont);
173
54.2k
  return &CG(context).brk_cont_array[CG(context).last_brk_cont-1];
174
54.2k
}
175
176
static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */
177
115k
{
178
115k
  zend_string *filename = CG(active_op_array)->filename;
179
115k
  zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32,
180
115k
    '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
181
115k
  return zend_new_interned_string(result);
182
115k
}
183
/* }}} */
184
185
static bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
186
12.8M
{
187
12.8M
  const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
188
12.8M
  if (ns_separator != NULL) {
189
12.4M
    *result = ns_separator + 1;
190
12.4M
    *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
191
12.4M
    return 1;
192
12.4M
  }
193
194
408k
  return 0;
195
12.8M
}
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
451k
{
227
451k
  const struct reserved_class_name *reserved = reserved_class_names;
228
229
451k
  const char *uqname = ZSTR_VAL(name);
230
451k
  size_t uqname_len = ZSTR_LEN(name);
231
451k
  zend_get_unqualified_name(name, &uqname, &uqname_len);
232
233
8.12M
  for (; reserved->name; ++reserved) {
234
7.67M
    if (uqname_len == reserved->len
235
191k
      && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
236
7.67M
    ) {
237
31
      return 1;
238
31
    }
239
7.67M
  }
240
241
451k
  return 0;
242
451k
}
243
/* }}} */
244
245
void zend_assert_valid_class_name(const zend_string *name, const char *type) /* {{{ */
246
450k
{
247
450k
  if (zend_is_reserved_class_name(name)) {
248
18
    zend_error_noreturn(E_COMPILE_ERROR,
249
18
      "Cannot use \"%s\" as %s as it is reserved", ZSTR_VAL(name), type);
250
18
  }
251
450k
  if (zend_string_equals_literal(name, "_")) {
252
833
    zend_error(E_DEPRECATED, "Using \"_\" as %s is deprecated since 8.4", type);
253
833
  }
254
450k
}
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
444k
{
295
444k
  const builtin_type_info *info = &builtin_types[0];
296
297
5.39M
  for (; info->name; ++info) {
298
5.05M
    if (ZSTR_LEN(name) == info->name_len
299
218k
      && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
300
5.05M
    ) {
301
111k
      return info->type;
302
111k
    }
303
5.05M
  }
304
305
333k
  return 0;
306
444k
}
307
/* }}} */
308
309
static zend_always_inline bool zend_is_confusable_type(const zend_string *name, const char **correct_name) /* {{{ */
310
333k
{
311
333k
  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
1.54M
  for (; info->name; ++info) {
316
1.25M
    if (zend_string_equals_cstr(name, info->name, info->name_len)) {
317
39.3k
      *correct_name = info->correct_name;
318
39.3k
      return 1;
319
39.3k
    }
320
1.25M
  }
321
322
293k
  return 0;
323
333k
}
324
/* }}} */
325
326
39.3k
static bool zend_is_not_imported(zend_string *name) {
327
  /* Assuming "name" is unqualified here. */
328
39.3k
  return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL;
329
39.3k
}
330
331
void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_array *op_array) /* {{{ */
332
1.46M
{
333
1.46M
  *prev_context = CG(context);
334
1.46M
  CG(context).prev = CG(context).op_array ? prev_context : NULL;
335
1.46M
  CG(context).op_array = op_array;
336
1.46M
  CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
337
1.46M
  CG(context).vars_size = 0;
338
1.46M
  CG(context).literals_size = 0;
339
1.46M
  CG(context).fast_call_var = -1;
340
1.46M
  CG(context).try_catch_offset = -1;
341
1.46M
  CG(context).current_brk_cont = -1;
342
1.46M
  CG(context).last_brk_cont = 0;
343
1.46M
  CG(context).has_assigned_to_http_response_header = false;
344
1.46M
  CG(context).brk_cont_array = NULL;
345
1.46M
  CG(context).labels = NULL;
346
1.46M
  CG(context).in_jmp_frameless_branch = false;
347
1.46M
  CG(context).active_property_info_name = NULL;
348
1.46M
  CG(context).active_property_hook_kind = (zend_property_hook_kind)-1;
349
1.46M
}
350
/* }}} */
351
352
void zend_oparray_context_end(const zend_oparray_context *prev_context) /* {{{ */
353
1.45M
{
354
1.45M
  if (CG(context).brk_cont_array) {
355
26.3k
    efree(CG(context).brk_cont_array);
356
26.3k
    CG(context).brk_cont_array = NULL;
357
26.3k
  }
358
1.45M
  if (CG(context).labels) {
359
3.81k
    zend_hash_destroy(CG(context).labels);
360
3.81k
    FREE_HASHTABLE(CG(context).labels);
361
3.81k
    CG(context).labels = NULL;
362
3.81k
  }
363
1.45M
  CG(context) = *prev_context;
364
1.45M
}
365
/* }}} */
366
367
static void zend_reset_import_tables(void) /* {{{ */
368
44.5k
{
369
44.5k
  if (FC(imports)) {
370
588
    zend_hash_destroy(FC(imports));
371
588
    efree(FC(imports));
372
588
    FC(imports) = NULL;
373
588
  }
374
375
44.5k
  if (FC(imports_function)) {
376
286
    zend_hash_destroy(FC(imports_function));
377
286
    efree(FC(imports_function));
378
286
    FC(imports_function) = NULL;
379
286
  }
380
381
44.5k
  if (FC(imports_const)) {
382
361
    zend_hash_destroy(FC(imports_const));
383
361
    efree(FC(imports_const));
384
361
    FC(imports_const) = NULL;
385
361
  }
386
387
44.5k
  zend_hash_clean(&FC(seen_symbols));
388
44.5k
}
389
/* }}} */
390
391
40.3k
static void zend_end_namespace(void) /* {{{ */ {
392
40.3k
  FC(in_namespace) = 0;
393
40.3k
  zend_reset_import_tables();
394
40.3k
  if (FC(current_namespace)) {
395
2.55k
    zend_string_release_ex(FC(current_namespace), 0);
396
2.55k
    FC(current_namespace) = NULL;
397
2.55k
  }
398
40.3k
}
399
/* }}} */
400
401
void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
402
42.3k
{
403
42.3k
  *prev_context = CG(file_context);
404
42.3k
  FC(imports) = NULL;
405
42.3k
  FC(imports_function) = NULL;
406
42.3k
  FC(imports_const) = NULL;
407
42.3k
  FC(current_namespace) = NULL;
408
42.3k
  FC(in_namespace) = 0;
409
42.3k
  FC(has_bracketed_namespaces) = 0;
410
42.3k
  FC(declarables).ticks = 0;
411
42.3k
  zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
412
42.3k
}
413
/* }}} */
414
415
void zend_file_context_end(const zend_file_context *prev_context) /* {{{ */
416
39.1k
{
417
39.1k
  zend_end_namespace();
418
39.1k
  zend_hash_destroy(&FC(seen_symbols));
419
39.1k
  CG(file_context) = *prev_context;
420
39.1k
}
421
/* }}} */
422
423
void zend_init_compiler_data_structures(void) /* {{{ */
424
225k
{
425
225k
  zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
426
225k
  zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
427
225k
  zend_stack_init(&CG(short_circuiting_opnums), sizeof(uint32_t));
428
225k
  CG(active_class_entry) = NULL;
429
225k
  CG(in_compilation) = 0;
430
225k
  CG(skip_shebang) = 0;
431
432
225k
  CG(encoding_declared) = 0;
433
225k
  CG(memoized_exprs) = NULL;
434
225k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
435
225k
}
436
/* }}} */
437
438
1.51M
static void zend_register_seen_symbol(zend_string *name, uint32_t kind) {
439
1.51M
  zval *zv = zend_hash_find(&FC(seen_symbols), name);
440
1.51M
  if (zv) {
441
1.48M
    Z_LVAL_P(zv) |= kind;
442
1.48M
  } else {
443
37.2k
    zval tmp;
444
37.2k
    ZVAL_LONG(&tmp, kind);
445
37.2k
    zend_hash_add_new(&FC(seen_symbols), name, &tmp);
446
37.2k
  }
447
1.51M
}
448
449
1.86k
static bool zend_have_seen_symbol(zend_string *name, uint32_t kind) {
450
1.86k
  const zval *zv = zend_hash_find(&FC(seen_symbols), name);
451
1.86k
  return zv && (Z_LVAL_P(zv) & kind) != 0;
452
1.86k
}
453
454
void init_compiler(void) /* {{{ */
455
222k
{
456
222k
  CG(arena) = zend_arena_create(64 * 1024);
457
222k
  CG(active_op_array) = NULL;
458
222k
  memset(&CG(context), 0, sizeof(CG(context)));
459
222k
  zend_init_compiler_data_structures();
460
222k
  zend_init_rsrc_list();
461
222k
  zend_stream_init();
462
222k
  CG(unclean_shutdown) = 0;
463
464
222k
  CG(delayed_variance_obligations) = NULL;
465
222k
  CG(delayed_autoloads) = NULL;
466
222k
  CG(unlinked_uses) = NULL;
467
222k
  CG(current_linking_class) = NULL;
468
222k
}
469
/* }}} */
470
471
void shutdown_compiler(void) /* {{{ */
472
225k
{
473
  /* Reset filename before destroying the arena, as file cache may use arena allocated strings. */
474
225k
  zend_restore_compiled_filename(NULL);
475
476
225k
  zend_stack_destroy(&CG(loop_var_stack));
477
225k
  zend_stack_destroy(&CG(delayed_oplines_stack));
478
225k
  zend_stack_destroy(&CG(short_circuiting_opnums));
479
480
225k
  if (CG(delayed_variance_obligations)) {
481
23
    zend_hash_destroy(CG(delayed_variance_obligations));
482
23
    FREE_HASHTABLE(CG(delayed_variance_obligations));
483
23
    CG(delayed_variance_obligations) = NULL;
484
23
  }
485
225k
  if (CG(delayed_autoloads)) {
486
22
    zend_hash_destroy(CG(delayed_autoloads));
487
22
    FREE_HASHTABLE(CG(delayed_autoloads));
488
22
    CG(delayed_autoloads) = NULL;
489
22
  }
490
225k
  if (CG(unlinked_uses)) {
491
81
    zend_hash_destroy(CG(unlinked_uses));
492
81
    FREE_HASHTABLE(CG(unlinked_uses));
493
81
    CG(unlinked_uses) = NULL;
494
81
  }
495
225k
  CG(current_linking_class) = NULL;
496
225k
}
497
/* }}} */
498
499
ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
500
77.4k
{
501
77.4k
  CG(compiled_filename) = zend_string_copy(new_compiled_filename);
502
77.4k
  return new_compiled_filename;
503
77.4k
}
504
/* }}} */
505
506
ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
507
443k
{
508
443k
  if (CG(compiled_filename)) {
509
77.3k
    zend_string_release(CG(compiled_filename));
510
77.3k
    CG(compiled_filename) = NULL;
511
77.3k
  }
512
443k
  CG(compiled_filename) = original_compiled_filename;
513
443k
}
514
/* }}} */
515
516
ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
517
1.86M
{
518
1.86M
  return CG(compiled_filename);
519
1.86M
}
520
/* }}} */
521
522
ZEND_API uint32_t zend_get_compiled_lineno(void) /* {{{ */
523
140k
{
524
140k
  return CG(zend_lineno);
525
140k
}
526
/* }}} */
527
528
ZEND_API bool zend_is_compiling(void) /* {{{ */
529
2.10M
{
530
2.10M
  return CG(in_compilation);
531
2.10M
}
532
/* }}} */
533
534
static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */
535
36.4M
{
536
36.4M
  return (uint32_t)CG(active_op_array)->T++;
537
36.4M
}
538
/* }}} */
539
540
2.12M
static uint32_t lookup_cv(zend_string *name) /* {{{ */{
541
2.12M
  zend_op_array *op_array = CG(active_op_array);
542
2.12M
  int i = 0;
543
2.12M
  zend_ulong hash_value = zend_string_hash_val(name);
544
545
5.86M
  while (i < op_array->last_var) {
546
4.10M
    if (ZSTR_H(op_array->vars[i]) == hash_value
547
364k
     && zend_string_equals(op_array->vars[i], name)) {
548
362k
      return EX_NUM_TO_VAR(i);
549
362k
    }
550
3.74M
    i++;
551
3.74M
  }
552
1.76M
  i = op_array->last_var;
553
1.76M
  op_array->last_var++;
554
1.76M
  if (op_array->last_var > CG(context).vars_size) {
555
1.39M
    CG(context).vars_size += 16; /* FIXME */
556
1.39M
    op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
557
1.39M
  }
558
559
1.76M
  op_array->vars[i] = zend_string_copy(name);
560
1.76M
  return EX_NUM_TO_VAR(i);
561
2.12M
}
562
/* }}} */
563
564
zend_string *zval_make_interned_string(zval *zv)
565
42.9M
{
566
42.9M
  ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
567
42.9M
  Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
568
42.9M
  if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
569
4.23M
    Z_TYPE_FLAGS_P(zv) = 0;
570
4.23M
  }
571
42.9M
  return Z_STR_P(zv);
572
42.9M
}
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
42.7M
{
577
42.7M
  zval *lit = CT_CONSTANT_EX(op_array, literal_position);
578
42.7M
  if (Z_TYPE_P(zv) == IS_STRING) {
579
40.9M
    zval_make_interned_string(zv);
580
40.9M
  }
581
42.7M
  ZVAL_COPY_VALUE(lit, zv);
582
42.7M
  Z_EXTRA_P(lit) = 0;
583
42.7M
}
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
42.7M
{
591
42.7M
  zend_op_array *op_array = CG(active_op_array);
592
42.7M
  uint32_t i = op_array->last_literal;
593
42.7M
  op_array->last_literal++;
594
42.7M
  if (i >= CG(context).literals_size) {
595
6.57M
    while (i >= CG(context).literals_size) {
596
3.28M
      CG(context).literals_size += 16; /* FIXME */
597
3.28M
    }
598
3.28M
    op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
599
3.28M
  }
600
42.7M
  zend_insert_literal(op_array, zv, i);
601
42.7M
  return i;
602
42.7M
}
603
/* }}} */
604
605
static inline int zend_add_literal_string(zend_string **str) /* {{{ */
606
37.4M
{
607
37.4M
  int ret;
608
37.4M
  zval zv;
609
37.4M
  ZVAL_STR(&zv, *str);
610
37.4M
  ret = zend_add_literal(&zv);
611
37.4M
  *str = Z_STR(zv);
612
37.4M
  return ret;
613
37.4M
}
614
/* }}} */
615
616
static int zend_add_func_name_literal(zend_string *name) /* {{{ */
617
155k
{
618
  /* Original name */
619
155k
  int ret = zend_add_literal_string(&name);
620
621
  /* Lowercased name */
622
155k
  zend_string *lc_name = zend_string_tolower(name);
623
155k
  zend_add_literal_string(&lc_name);
624
625
155k
  return ret;
626
155k
}
627
/* }}} */
628
629
static int zend_add_ns_func_name_literal(zend_string *name) /* {{{ */
630
3.35M
{
631
3.35M
  const char *unqualified_name;
632
3.35M
  size_t unqualified_name_len;
633
634
  /* Original name */
635
3.35M
  int ret = zend_add_literal_string(&name);
636
637
  /* Lowercased name */
638
3.35M
  zend_string *lc_name = zend_string_tolower(name);
639
3.35M
  zend_add_literal_string(&lc_name);
640
641
  /* Lowercased unqualified name */
642
3.35M
  if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
643
3.35M
    lc_name = zend_string_alloc(unqualified_name_len, 0);
644
3.35M
    zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
645
3.35M
    zend_add_literal_string(&lc_name);
646
3.35M
  }
647
648
3.35M
  return ret;
649
3.35M
}
650
/* }}} */
651
652
static int zend_add_class_name_literal(zend_string *name) /* {{{ */
653
187k
{
654
  /* Original name */
655
187k
  int ret = zend_add_literal_string(&name);
656
657
  /* Lowercased name */
658
187k
  zend_string *lc_name = zend_string_tolower(name);
659
187k
  zend_add_literal_string(&lc_name);
660
661
187k
  return ret;
662
187k
}
663
/* }}} */
664
665
static int zend_add_const_name_literal(zend_string *name, bool unqualified) /* {{{ */
666
8.86M
{
667
8.86M
  zend_string *tmp_name;
668
669
8.86M
  int ret = zend_add_literal_string(&name);
670
671
8.86M
  size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
672
8.86M
  const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
673
8.86M
  if (after_ns) {
674
8.69M
    after_ns += 1;
675
8.69M
    ns_len = after_ns - ZSTR_VAL(name) - 1;
676
8.69M
    after_ns_len = ZSTR_LEN(name) - ns_len - 1;
677
678
    /* lowercased namespace name & original constant name */
679
8.69M
    tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
680
8.69M
    zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
681
8.69M
    zend_add_literal_string(&tmp_name);
682
683
8.69M
    if (!unqualified) {
684
2.90k
      return ret;
685
2.90k
    }
686
8.69M
  } else {
687
169k
    after_ns = ZSTR_VAL(name);
688
169k
  }
689
690
  /* original unqualified constant name */
691
8.86M
  tmp_name = zend_string_init(after_ns, after_ns_len, 0);
692
8.86M
  zend_add_literal_string(&tmp_name);
693
694
8.86M
  return ret;
695
8.86M
}
696
/* }}} */
697
698
55.8k
#define LITERAL_STR(op, str) do { \
699
55.8k
    zval _c; \
700
55.8k
    ZVAL_STR(&_c, str); \
701
55.8k
    op.constant = zend_add_literal(&_c); \
702
55.8k
  } while (0)
703
704
void zend_stop_lexing(void)
705
22
{
706
22
  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
22
  LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
711
22
}
712
713
static inline void zend_begin_loop(
714
    uint8_t free_opcode, const znode *loop_var, bool is_switch) /* {{{ */
715
54.2k
{
716
54.2k
  zend_brk_cont_element *brk_cont_element;
717
54.2k
  int parent = CG(context).current_brk_cont;
718
54.2k
  zend_loop_var info = {0};
719
720
54.2k
  CG(context).current_brk_cont = CG(context).last_brk_cont;
721
54.2k
  brk_cont_element = get_next_brk_cont_element();
722
54.2k
  brk_cont_element->parent = parent;
723
54.2k
  brk_cont_element->is_switch = is_switch;
724
725
54.2k
  if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
726
24.2k
    uint32_t start = get_next_op_number();
727
728
24.2k
    info.opcode = free_opcode;
729
24.2k
    info.var_type = loop_var->op_type;
730
24.2k
    info.var_num = loop_var->u.op.var;
731
24.2k
    brk_cont_element->start = start;
732
29.9k
  } else {
733
29.9k
    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
29.9k
    brk_cont_element->start = -1;
737
29.9k
  }
738
739
54.2k
  zend_stack_push(&CG(loop_var_stack), &info);
740
54.2k
}
741
/* }}} */
742
743
static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
744
54.0k
{
745
54.0k
  uint32_t end = get_next_op_number();
746
54.0k
  zend_brk_cont_element *brk_cont_element
747
54.0k
    = &CG(context).brk_cont_array[CG(context).current_brk_cont];
748
54.0k
  brk_cont_element->cont = cont_addr;
749
54.0k
  brk_cont_element->brk = end;
750
54.0k
  CG(context).current_brk_cont = brk_cont_element->parent;
751
752
54.0k
  zend_stack_del_top(&CG(loop_var_stack));
753
54.0k
}
754
/* }}} */
755
756
static void zend_do_free(znode *op1) /* {{{ */
757
3.47M
{
758
3.47M
  if (op1->op_type == IS_TMP_VAR) {
759
1.40M
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
760
761
1.43M
    while (opline->opcode == ZEND_END_SILENCE ||
762
1.42M
           opline->opcode == ZEND_OP_DATA) {
763
34.0k
      opline--;
764
34.0k
    }
765
766
1.40M
    if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
767
1.39M
      switch (opline->opcode) {
768
3.01k
        case ZEND_BOOL:
769
6.39k
        case ZEND_BOOL_NOT:
770
          /* boolean results don't have to be freed */
771
6.39k
          return;
772
70
        case ZEND_POST_INC_STATIC_PROP:
773
264
        case ZEND_POST_DEC_STATIC_PROP:
774
304
        case ZEND_POST_INC_OBJ:
775
375
        case ZEND_POST_DEC_OBJ:
776
1.42k
        case ZEND_POST_INC:
777
1.77k
        case ZEND_POST_DEC:
778
          /* convert $i++ to ++$i */
779
1.77k
          opline->opcode -= 2;
780
1.77k
          SET_UNUSED(opline->result);
781
1.77k
          return;
782
54.2k
        case ZEND_ASSIGN:
783
57.9k
        case ZEND_ASSIGN_DIM:
784
62.3k
        case ZEND_ASSIGN_OBJ:
785
63.3k
        case ZEND_ASSIGN_STATIC_PROP:
786
79.5k
        case ZEND_ASSIGN_OP:
787
81.3k
        case ZEND_ASSIGN_DIM_OP:
788
81.7k
        case ZEND_ASSIGN_OBJ_OP:
789
82.3k
        case ZEND_ASSIGN_STATIC_PROP_OP:
790
82.3k
        case ZEND_PRE_INC_STATIC_PROP:
791
82.5k
        case ZEND_PRE_DEC_STATIC_PROP:
792
82.6k
        case ZEND_PRE_INC_OBJ:
793
82.7k
        case ZEND_PRE_DEC_OBJ:
794
83.0k
        case ZEND_PRE_INC:
795
83.3k
        case ZEND_PRE_DEC:
796
83.3k
          SET_UNUSED(opline->result);
797
83.3k
          return;
798
1.39M
      }
799
1.39M
    }
800
801
1.31M
    zend_emit_op(NULL, ZEND_FREE, op1, NULL);
802
2.06M
  } else if (op1->op_type == IS_VAR) {
803
2.01M
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
804
2.16M
    while (opline->opcode == ZEND_END_SILENCE ||
805
2.16M
        opline->opcode == ZEND_EXT_FCALL_END ||
806
2.16M
        opline->opcode == ZEND_OP_DATA) {
807
146k
      opline--;
808
146k
    }
809
2.01M
    if (opline->result_type == IS_VAR
810
1.98M
      && opline->result.var == op1->u.op.var) {
811
1.98M
      if (opline->opcode == ZEND_FETCH_THIS) {
812
0
        opline->opcode = ZEND_NOP;
813
0
      }
814
1.98M
      if (!ZEND_OP_IS_FRAMELESS_ICALL(opline->opcode)) {
815
1.35M
        SET_UNUSED(opline->result);
816
1.35M
      } else {
817
        /* Frameless calls usually use the return value, so always emit a free. This should be
818
         * faster than checking RETURN_VALUE_USED inside the handler. */
819
        // FIXME: We may actually look at the function signature to determine whether a free
820
        // is necessary.
821
625k
        zend_emit_op(NULL, ZEND_FREE, op1, NULL);
822
625k
      }
823
1.98M
    } else {
824
153k
      while (opline >= CG(active_op_array)->opcodes) {
825
153k
        if ((opline->opcode == ZEND_FETCH_LIST_R ||
826
151k
             opline->opcode == ZEND_FETCH_LIST_W ||
827
150k
             opline->opcode == ZEND_EXT_STMT) &&
828
2.59k
            opline->op1_type == IS_VAR &&
829
2.59k
            opline->op1.var == op1->u.op.var) {
830
2.00k
          zend_emit_op(NULL, ZEND_FREE, op1, NULL);
831
2.00k
          return;
832
2.00k
        }
833
151k
        if (opline->result_type == IS_VAR
834
56.7k
          && opline->result.var == op1->u.op.var) {
835
28.1k
          if (opline->opcode == ZEND_NEW) {
836
28.1k
            zend_emit_op(NULL, ZEND_FREE, op1, NULL);
837
28.1k
          }
838
28.1k
          break;
839
28.1k
        }
840
123k
        opline--;
841
123k
      }
842
30.1k
    }
843
2.01M
  } else if (op1->op_type == IS_CONST) {
844
    /* Destroy value without using GC: When opcache moves arrays into SHM it will
845
     * free the zend_array structure, so references to it from outside the op array
846
     * become invalid. GC would cause such a reference in the root buffer. */
847
52.1k
    zval_ptr_dtor_nogc(&op1->u.constant);
848
52.1k
  }
849
3.47M
}
850
/* }}} */
851
852
853
static const char *zend_modifier_token_to_string(uint32_t token)
854
38
{
855
38
  switch (token) {
856
3
    case T_PUBLIC:
857
3
      return "public";
858
1
    case T_PROTECTED:
859
1
      return "protected";
860
1
    case T_PRIVATE:
861
1
      return "private";
862
8
    case T_STATIC:
863
8
      return "static";
864
6
    case T_FINAL:
865
6
      return "final";
866
8
    case T_READONLY:
867
8
      return "readonly";
868
8
    case T_ABSTRACT:
869
8
      return "abstract";
870
1
    case T_PUBLIC_SET:
871
1
      return "public(set)";
872
1
    case T_PROTECTED_SET:
873
1
      return "protected(set)";
874
1
    case T_PRIVATE_SET:
875
1
      return "private(set)";
876
38
    EMPTY_SWITCH_DEFAULT_CASE()
877
38
  }
878
38
}
879
880
uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t token)
881
24.9k
{
882
24.9k
  switch (token) {
883
14.9k
    case T_PUBLIC:
884
14.9k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
885
14.9k
        return ZEND_ACC_PUBLIC;
886
14.9k
      }
887
3
      break;
888
1.11k
    case T_PROTECTED:
889
1.11k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
890
1.11k
        return ZEND_ACC_PROTECTED;
891
1.11k
      }
892
1
      break;
893
1.89k
    case T_PRIVATE:
894
1.89k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
895
1.89k
        return ZEND_ACC_PRIVATE;
896
1.89k
      }
897
1
      break;
898
337
    case T_READONLY:
899
337
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
900
332
        return ZEND_ACC_READONLY;
901
332
      }
902
5
      break;
903
566
    case T_ABSTRACT:
904
566
      if (target == ZEND_MODIFIER_TARGET_METHOD || target == ZEND_MODIFIER_TARGET_PROPERTY) {
905
565
        return ZEND_ACC_ABSTRACT;
906
565
      }
907
1
      break;
908
983
    case T_FINAL:
909
983
      return ZEND_ACC_FINAL;
910
4.16k
    case T_STATIC:
911
4.16k
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_METHOD) {
912
4.15k
        return ZEND_ACC_STATIC;
913
4.15k
      }
914
3
      break;
915
458
    case T_PUBLIC_SET:
916
458
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
917
457
        return ZEND_ACC_PUBLIC_SET;
918
457
      }
919
1
      break;
920
99
    case T_PROTECTED_SET:
921
99
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
922
98
        return ZEND_ACC_PROTECTED_SET;
923
98
      }
924
1
      break;
925
432
    case T_PRIVATE_SET:
926
432
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
927
431
        return ZEND_ACC_PRIVATE_SET;
928
431
      }
929
1
      break;
930
24.9k
  }
931
932
17
  char *member;
933
17
  if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
934
0
    member = "property";
935
17
  } else if (target == ZEND_MODIFIER_TARGET_METHOD) {
936
4
    member = "method";
937
13
  } else if (target == ZEND_MODIFIER_TARGET_CONSTANT) {
938
3
    member = "class constant";
939
10
  } else if (target == ZEND_MODIFIER_TARGET_CPP) {
940
3
    member = "parameter";
941
7
  } else if (target == ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
942
7
    member = "property hook";
943
7
  } else {
944
0
    ZEND_UNREACHABLE();
945
0
  }
946
947
17
  zend_throw_exception_ex(zend_ce_compile_error, 0,
948
17
    "Cannot use the %s modifier on a %s", zend_modifier_token_to_string(token), member);
949
17
  return 0;
950
17
}
951
952
uint32_t zend_modifier_list_to_flags(zend_modifier_target target, zend_ast *modifiers)
953
20.8k
{
954
20.8k
  uint32_t flags = 0;
955
20.8k
  const zend_ast_list *modifier_list = zend_ast_get_list(modifiers);
956
957
44.9k
  for (uint32_t i = 0; i < modifier_list->children; i++) {
958
24.2k
    uint32_t token = (uint32_t) Z_LVAL_P(zend_ast_get_zval(modifier_list->child[i]));
959
24.2k
    uint32_t new_flag = zend_modifier_token_to_flag(target, token);
960
24.2k
    if (!new_flag) {
961
14
      return 0;
962
14
    }
963
    /* Don't error immediately for duplicate flags, we want to prioritize the errors from zend_add_member_modifier(). */
964
24.2k
    bool duplicate_flag = (flags & new_flag);
965
24.2k
    flags = zend_add_member_modifier(flags, new_flag, target);
966
24.2k
    if (!flags) {
967
21
      return 0;
968
21
    }
969
24.1k
    if (duplicate_flag) {
970
21
      zend_throw_exception_ex(zend_ce_compile_error, 0,
971
21
        "Multiple %s modifiers are not allowed", zend_modifier_token_to_string(token));
972
21
      return 0;
973
21
    }
974
24.1k
  }
975
976
20.7k
  return flags;
977
20.8k
}
978
979
uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
980
152
{
981
152
  uint32_t new_flags = flags | new_flag;
982
152
  if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
983
1
    zend_throw_exception(zend_ce_compile_error,
984
1
      "Multiple abstract modifiers are not allowed", 0);
985
1
    return 0;
986
1
  }
987
151
  if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
988
2
    zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
989
2
    return 0;
990
2
  }
991
149
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
992
2
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
993
2
    return 0;
994
2
  }
995
147
  if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
996
3
    zend_throw_exception(zend_ce_compile_error,
997
3
      "Cannot use the final modifier on an abstract class", 0);
998
3
    return 0;
999
3
  }
1000
144
  return new_flags;
1001
147
}
1002
/* }}} */
1003
1004
uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag)
1005
80
{
1006
80
  uint32_t new_flags = flags | new_flag;
1007
80
  if (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
1008
2
    zend_throw_exception(zend_ce_compile_error,
1009
2
      "Cannot use the abstract modifier on an anonymous class", 0);
1010
2
    return 0;
1011
2
  }
1012
78
  if (new_flag & ZEND_ACC_FINAL) {
1013
2
    zend_throw_exception(zend_ce_compile_error, "Cannot use the final modifier on an anonymous class", 0);
1014
2
    return 0;
1015
2
  }
1016
76
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
1017
2
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
1018
2
    return 0;
1019
2
  }
1020
74
  return new_flags;
1021
76
}
1022
1023
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target) /* {{{ */
1024
24.2k
{
1025
24.2k
  uint32_t new_flags = flags | new_flag;
1026
24.2k
  if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
1027
8
    zend_throw_exception(zend_ce_compile_error,
1028
8
      "Multiple access type modifiers are not allowed", 0);
1029
8
    return 0;
1030
8
  }
1031
24.1k
  if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
1032
4
    if (target == ZEND_MODIFIER_TARGET_METHOD) {
1033
2
      zend_throw_exception(zend_ce_compile_error,
1034
2
        "Cannot use the final modifier on an abstract method", 0);
1035
2
      return 0;
1036
2
    }
1037
2
    if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
1038
2
      zend_throw_exception(zend_ce_compile_error,
1039
2
        "Cannot use the final modifier on an abstract property", 0);
1040
2
      return 0;
1041
2
    }
1042
2
  }
1043
24.1k
  if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
1044
14.4k
    if ((flags & ZEND_ACC_PPP_SET_MASK) && (new_flag & ZEND_ACC_PPP_SET_MASK)) {
1045
9
      zend_throw_exception(zend_ce_compile_error,
1046
9
        "Multiple access type modifiers are not allowed", 0);
1047
9
      return 0;
1048
9
    }
1049
14.4k
  }
1050
24.1k
  return new_flags;
1051
24.1k
}
1052
/* }}} */
1053
1054
7.23k
ZEND_API zend_string *zend_create_member_string(const zend_string *class_name, const zend_string *member_name) {
1055
7.23k
  return zend_string_concat3(
1056
7.23k
    ZSTR_VAL(class_name), ZSTR_LEN(class_name),
1057
7.23k
    "::", sizeof("::") - 1,
1058
7.23k
    ZSTR_VAL(member_name), ZSTR_LEN(member_name));
1059
7.23k
}
1060
1061
15.3M
static zend_string *zend_concat_names(const char *name1, size_t name1_len, const char *name2, size_t name2_len) {
1062
15.3M
  return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len);
1063
15.3M
}
1064
1065
15.8M
static zend_string *zend_prefix_with_ns(zend_string *name) {
1066
15.8M
  if (FC(current_namespace)) {
1067
15.3M
    const zend_string *ns = FC(current_namespace);
1068
15.3M
    return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
1069
15.3M
  } else {
1070
490k
    return zend_string_copy(name);
1071
490k
  }
1072
15.8M
}
1073
1074
static zend_string *zend_resolve_non_class_name(
1075
  zend_string *name, uint32_t type, bool *is_fully_qualified,
1076
  bool case_sensitive, const HashTable *current_import_sub
1077
12.5M
) {
1078
12.5M
  const char *compound;
1079
12.5M
  *is_fully_qualified = false;
1080
1081
12.5M
  if (ZSTR_VAL(name)[0] == '\\') {
1082
    /* Remove \ prefix (only relevant if this is a string rather than a label) */
1083
81
    *is_fully_qualified = true;
1084
81
    return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1085
81
  }
1086
1087
12.5M
  if (type == ZEND_NAME_FQ) {
1088
67.7k
    *is_fully_qualified = true;
1089
67.7k
    return zend_string_copy(name);
1090
67.7k
  }
1091
1092
12.4M
  if (type == ZEND_NAME_RELATIVE) {
1093
1.24k
    *is_fully_qualified = true;
1094
1.24k
    return zend_prefix_with_ns(name);
1095
1.24k
  }
1096
1097
12.4M
  if (current_import_sub) {
1098
    /* If an unqualified name is a function/const alias, replace it. */
1099
1.71k
    zend_string *import_name;
1100
1.71k
    if (case_sensitive) {
1101
1.40k
      import_name = zend_hash_find_ptr(current_import_sub, name);
1102
1.40k
    } else {
1103
313
      import_name = zend_hash_find_ptr_lc(current_import_sub, name);
1104
313
    }
1105
1106
1.71k
    if (import_name) {
1107
569
      *is_fully_qualified = true;
1108
569
      return zend_string_copy(import_name);
1109
569
    }
1110
1.71k
  }
1111
1112
12.4M
  compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1113
12.4M
  if (compound) {
1114
7.15k
    *is_fully_qualified = true;
1115
7.15k
  }
1116
1117
12.4M
  if (compound && FC(imports)) {
1118
    /* If the first part of a qualified name is an alias, substitute it. */
1119
2.31k
    size_t len = compound - ZSTR_VAL(name);
1120
2.31k
    const zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1121
1122
2.31k
    if (import_name) {
1123
500
      return zend_concat_names(
1124
500
        ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1125
500
    }
1126
2.31k
  }
1127
1128
12.4M
  return zend_prefix_with_ns(name);
1129
12.4M
}
1130
/* }}} */
1131
1132
static zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1133
3.51M
{
1134
3.51M
  return zend_resolve_non_class_name(
1135
3.51M
    name, type, is_fully_qualified, false, FC(imports_function));
1136
3.51M
}
1137
1138
static zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1139
9.03M
{
1140
9.03M
  return zend_resolve_non_class_name(
1141
9.03M
    name, type, is_fully_qualified, true, FC(imports_const));
1142
9.03M
}
1143
1144
static zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
1145
3.22M
{
1146
3.22M
  const char *compound;
1147
1148
3.22M
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1149
2.24k
    if (type == ZEND_NAME_FQ) {
1150
2
      zend_error_noreturn(E_COMPILE_ERROR,
1151
2
        "'\\%s' is an invalid class name", ZSTR_VAL(name));
1152
2
    }
1153
2.24k
    if (type == ZEND_NAME_RELATIVE) {
1154
0
      zend_error_noreturn(E_COMPILE_ERROR,
1155
0
        "'namespace\\%s' is an invalid class name", ZSTR_VAL(name));
1156
0
    }
1157
2.24k
    ZEND_ASSERT(type == ZEND_NAME_NOT_FQ);
1158
2.24k
    return zend_string_copy(name);
1159
2.24k
  }
1160
1161
3.22M
  if (type == ZEND_NAME_RELATIVE) {
1162
125
    return zend_prefix_with_ns(name);
1163
125
  }
1164
1165
3.22M
  if (type == ZEND_NAME_FQ) {
1166
11.2k
    if (ZSTR_VAL(name)[0] == '\\') {
1167
      /* Remove \ prefix (only relevant if this is a string rather than a label) */
1168
106
      name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1169
106
      if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1170
0
        zend_error_noreturn(E_COMPILE_ERROR,
1171
0
          "'\\%s' is an invalid class name", ZSTR_VAL(name));
1172
0
      }
1173
106
      return name;
1174
106
    }
1175
1176
11.1k
    return zend_string_copy(name);
1177
11.2k
  }
1178
1179
3.21M
  if (FC(imports)) {
1180
3.64k
    compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1181
3.64k
    if (compound) {
1182
      /* If the first part of a qualified name is an alias, substitute it. */
1183
953
      size_t len = compound - ZSTR_VAL(name);
1184
953
      const zend_string *import_name =
1185
953
        zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1186
1187
953
      if (import_name) {
1188
109
        return zend_concat_names(
1189
109
          ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1190
109
      }
1191
2.69k
    } else {
1192
      /* If an unqualified name is an alias, replace it. */
1193
2.69k
      zend_string *import_name
1194
2.69k
        = zend_hash_find_ptr_lc(FC(imports), name);
1195
1196
2.69k
      if (import_name) {
1197
884
        return zend_string_copy(import_name);
1198
884
      }
1199
2.69k
    }
1200
3.64k
  }
1201
1202
  /* If not fully qualified and not an alias, prepend the current namespace */
1203
3.21M
  return zend_prefix_with_ns(name);
1204
3.21M
}
1205
/* }}} */
1206
1207
static zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
1208
3.14M
{
1209
3.14M
  const zval *class_name = zend_ast_get_zval(ast);
1210
3.14M
  if (Z_TYPE_P(class_name) != IS_STRING) {
1211
14
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1212
14
  }
1213
3.14M
  return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
1214
3.14M
}
1215
/* }}} */
1216
1217
static void label_ptr_dtor(zval *zv) /* {{{ */
1218
4.62k
{
1219
4.62k
  efree_size(Z_PTR_P(zv), sizeof(zend_label));
1220
4.62k
}
1221
/* }}} */
1222
1223
1.69k
static void str_dtor(zval *zv)  /* {{{ */ {
1224
1.69k
  zend_string_release_ex(Z_STR_P(zv), 0);
1225
1.69k
}
1226
/* }}} */
1227
1228
static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
1229
20.0k
{
1230
20.0k
  zend_op_array *op_array = CG(active_op_array);
1231
20.0k
  uint32_t try_catch_offset = op_array->last_try_catch++;
1232
20.0k
  zend_try_catch_element *elem;
1233
1234
20.0k
  op_array->try_catch_array = safe_erealloc(
1235
20.0k
    op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
1236
1237
20.0k
  elem = &op_array->try_catch_array[try_catch_offset];
1238
20.0k
  elem->try_op = try_op;
1239
20.0k
  elem->catch_op = 0;
1240
20.0k
  elem->finally_op = 0;
1241
20.0k
  elem->finally_end = 0;
1242
1243
20.0k
  return try_catch_offset;
1244
20.0k
}
1245
/* }}} */
1246
1247
ZEND_API void function_add_ref(zend_function *function) /* {{{ */
1248
258
{
1249
258
  if (function->type == ZEND_USER_FUNCTION) {
1250
258
    zend_op_array *op_array = &function->op_array;
1251
258
    if (op_array->refcount) {
1252
74
      (*op_array->refcount)++;
1253
74
    }
1254
1255
258
    ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
1256
258
    ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
1257
258
  }
1258
1259
258
  if (function->common.function_name) {
1260
258
    zend_string_addref(function->common.function_name);
1261
258
  }
1262
258
}
1263
/* }}} */
1264
1265
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) /* {{{ */
1266
48
{
1267
48
  const zval *zv = zend_hash_find_known_hash(compile_time ? CG(function_table) : EG(function_table), lcname);
1268
48
  int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
1269
48
  const zend_function *old_function;
1270
1271
48
  ZEND_ASSERT(zv != NULL);
1272
48
  old_function = Z_PTR_P(zv);
1273
48
  if (old_function->type == ZEND_USER_FUNCTION
1274
46
    && old_function->op_array.last > 0) {
1275
46
    zend_error_noreturn(error_level, "Cannot redeclare function %s() (previously declared in %s:%d)",
1276
46
          op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name),
1277
46
          ZSTR_VAL(old_function->op_array.filename),
1278
46
          old_function->op_array.line_start);
1279
46
  } else {
1280
2
    zend_error_noreturn(error_level, "Cannot redeclare function %s()",
1281
2
      op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name));
1282
2
  }
1283
48
}
1284
1285
ZEND_API zend_result do_bind_function(zend_function *func, const zval *lcname) /* {{{ */
1286
9
{
1287
9
  zend_function *added_func = zend_hash_add_ptr(EG(function_table), Z_STR_P(lcname), func);
1288
9
  if (UNEXPECTED(!added_func)) {
1289
0
    do_bind_function_error(Z_STR_P(lcname), &func->op_array, false);
1290
0
    return FAILURE;
1291
0
  }
1292
1293
9
  if (func->op_array.refcount) {
1294
3
    ++*func->op_array.refcount;
1295
3
  }
1296
9
  if (func->common.function_name) {
1297
9
    zend_string_addref(func->common.function_name);
1298
9
  }
1299
9
  zend_observer_function_declared_notify(&func->op_array, Z_STR_P(lcname));
1300
9
  return SUCCESS;
1301
9
}
1302
/* }}} */
1303
1304
ZEND_API zend_class_entry *zend_bind_class_in_slot(
1305
    zval *class_table_slot, const zval *lcname, zend_string *lc_parent_name)
1306
734
{
1307
734
  zend_class_entry *ce = Z_PTR_P(class_table_slot);
1308
734
  bool is_preloaded =
1309
734
    (ce->ce_flags & ZEND_ACC_PRELOADED) && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD);
1310
734
  bool success;
1311
734
  if (EXPECTED(!is_preloaded)) {
1312
734
    success = zend_hash_set_bucket_key(EG(class_table), (Bucket*) class_table_slot, Z_STR_P(lcname)) != NULL;
1313
734
  } else {
1314
    /* If preloading is used, don't replace the existing bucket, add a new one. */
1315
0
    success = zend_hash_add_ptr(EG(class_table), Z_STR_P(lcname), ce) != NULL;
1316
0
  }
1317
734
  if (UNEXPECTED(!success)) {
1318
23
    zend_class_entry *old_class = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1319
23
    ZEND_ASSERT(old_class);
1320
23
    zend_class_redeclaration_error(E_COMPILE_ERROR, old_class);
1321
23
    return NULL;
1322
23
  }
1323
1324
711
  if (ce->ce_flags & ZEND_ACC_LINKED) {
1325
8
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1326
8
    return ce;
1327
8
  }
1328
1329
703
  ce = zend_do_link_class(ce, lc_parent_name, Z_STR_P(lcname));
1330
703
  if (ce) {
1331
530
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1332
530
    return ce;
1333
530
  }
1334
1335
173
  if (!is_preloaded) {
1336
    /* Reload bucket pointer, the hash table may have been reallocated */
1337
39
    zval *zv = zend_hash_find(EG(class_table), Z_STR_P(lcname));
1338
39
    zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(lcname + 1));
1339
134
  } else {
1340
134
    zend_hash_del(EG(class_table), Z_STR_P(lcname));
1341
134
  }
1342
173
  return NULL;
1343
703
}
1344
1345
ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */
1346
678
{
1347
678
  zval *rtd_key, *zv;
1348
1349
678
  rtd_key = lcname + 1;
1350
1351
678
  zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(rtd_key));
1352
1353
678
  if (UNEXPECTED(!zv)) {
1354
1
    const zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1355
1
    ZEND_ASSERT(ce);
1356
1
    zend_class_redeclaration_error(E_COMPILE_ERROR, ce);
1357
1
    return FAILURE;
1358
1
  }
1359
1360
  /* Register the derived class */
1361
677
  return zend_bind_class_in_slot(zv, lcname, lc_parent_name) ? SUCCESS : FAILURE;
1362
678
}
1363
/* }}} */
1364
1365
14.4k
static zend_string *add_type_string(zend_string *type, zend_string *new_type, bool is_intersection) {
1366
14.4k
  zend_string *result;
1367
14.4k
  if (type == NULL) {
1368
7.76k
    return zend_string_copy(new_type);
1369
7.76k
  }
1370
1371
6.64k
  if (is_intersection) {
1372
4.06k
    result = zend_string_concat3(ZSTR_VAL(type), ZSTR_LEN(type),
1373
4.06k
      "&", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1374
4.06k
    zend_string_release(type);
1375
4.06k
  } else {
1376
2.57k
    result = zend_string_concat3(
1377
2.57k
      ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1378
2.57k
    zend_string_release(type);
1379
2.57k
  }
1380
6.64k
  return result;
1381
14.4k
}
1382
1383
2.01k
static zend_string *resolve_class_name(zend_string *name, const zend_class_entry *scope) {
1384
2.01k
  if (scope) {
1385
1.56k
    if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1386
4
      name = scope->name;
1387
1.56k
    } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT)) && scope->parent) {
1388
0
      name = scope->parent->name;
1389
0
    }
1390
1.56k
  }
1391
1392
  /* The resolved name for anonymous classes contains null bytes. Cut off everything after the
1393
   * null byte here, to avoid larger parts of the type being omitted by printing code later. */
1394
2.01k
  size_t len = strlen(ZSTR_VAL(name));
1395
2.01k
  if (len != ZSTR_LEN(name)) {
1396
0
    return zend_string_init(ZSTR_VAL(name), len, 0);
1397
0
  }
1398
2.01k
  return zend_string_copy(name);
1399
2.01k
}
1400
1401
static zend_string *add_intersection_type(zend_string *str,
1402
  const zend_type_list *intersection_type_list, zend_class_entry *scope,
1403
  bool is_bracketed)
1404
1.87k
{
1405
1.87k
  const zend_type *single_type;
1406
1.87k
  zend_string *intersection_str = NULL;
1407
1408
7.82k
  ZEND_TYPE_LIST_FOREACH(intersection_type_list, single_type) {
1409
7.82k
    ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*single_type));
1410
7.82k
    ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*single_type));
1411
1412
5.94k
    intersection_str = add_type_string(intersection_str, ZEND_TYPE_NAME(*single_type), /* is_intersection */ true);
1413
5.94k
  } ZEND_TYPE_LIST_FOREACH_END();
1414
1415
1.87k
  ZEND_ASSERT(intersection_str);
1416
1417
1.87k
  if (is_bracketed) {
1418
1.59k
    zend_string *result = zend_string_concat3("(", 1, ZSTR_VAL(intersection_str), ZSTR_LEN(intersection_str), ")", 1);
1419
1.59k
    zend_string_release(intersection_str);
1420
1.59k
    intersection_str = result;
1421
1.59k
  }
1422
1.87k
  str = add_type_string(str, intersection_str, /* is_intersection */ false);
1423
1.87k
  zend_string_release(intersection_str);
1424
1.87k
  return str;
1425
1.87k
}
1426
1427
7.02k
zend_string *zend_type_to_string_resolved(const zend_type type, zend_class_entry *scope) {
1428
7.02k
  zend_string *str = NULL;
1429
1430
  /* Pure intersection type */
1431
7.02k
  if (ZEND_TYPE_IS_INTERSECTION(type)) {
1432
285
    ZEND_ASSERT(!ZEND_TYPE_IS_UNION(type));
1433
285
    str = add_intersection_type(str, ZEND_TYPE_LIST(type), scope, /* is_bracketed */ false);
1434
6.74k
  } else if (ZEND_TYPE_HAS_LIST(type)) {
1435
    /* A union type might not be a list */
1436
330
    const zend_type *list_type;
1437
2.79k
    ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
1438
2.79k
      if (ZEND_TYPE_IS_INTERSECTION(*list_type)) {
1439
1.59k
        str = add_intersection_type(str, ZEND_TYPE_LIST(*list_type), scope, /* is_bracketed */ true);
1440
1.59k
        continue;
1441
1.59k
      }
1442
870
      ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type));
1443
870
      ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*list_type));
1444
1445
870
      zend_string *name = ZEND_TYPE_NAME(*list_type);
1446
870
      zend_string *resolved = resolve_class_name(name, scope);
1447
870
      str = add_type_string(str, resolved, /* is_intersection */ false);
1448
870
      zend_string_release(resolved);
1449
870
    } ZEND_TYPE_LIST_FOREACH_END();
1450
6.41k
  } else if (ZEND_TYPE_HAS_NAME(type)) {
1451
1.14k
    str = resolve_class_name(ZEND_TYPE_NAME(type), scope);
1452
1.14k
  }
1453
1454
7.02k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
1455
1456
7.02k
  if (type_mask == MAY_BE_ANY) {
1457
23
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_MIXED), /* is_intersection */ false);
1458
1459
23
    return str;
1460
23
  }
1461
7.00k
  if (type_mask & MAY_BE_STATIC) {
1462
43
    zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC);
1463
    // During compilation of eval'd code the called scope refers to the scope calling the eval
1464
43
    if (scope && !zend_is_compiling()) {
1465
0
      const zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
1466
0
      if (called_scope) {
1467
0
        name = called_scope->name;
1468
0
      }
1469
0
    }
1470
43
    str = add_type_string(str, name, /* is_intersection */ false);
1471
43
  }
1472
7.00k
  if (type_mask & MAY_BE_CALLABLE) {
1473
30
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE), /* is_intersection */ false);
1474
30
  }
1475
7.00k
  if (type_mask & MAY_BE_OBJECT) {
1476
44
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT), /* is_intersection */ false);
1477
44
  }
1478
7.00k
  if (type_mask & MAY_BE_ARRAY) {
1479
601
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY), /* is_intersection */ false);
1480
601
  }
1481
7.00k
  if (type_mask & MAY_BE_STRING) {
1482
3.02k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING), /* is_intersection */ false);
1483
3.02k
  }
1484
7.00k
  if (type_mask & MAY_BE_LONG) {
1485
1.38k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT), /* is_intersection */ false);
1486
1.38k
  }
1487
7.00k
  if (type_mask & MAY_BE_DOUBLE) {
1488
91
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT), /* is_intersection */ false);
1489
91
  }
1490
7.00k
  if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
1491
315
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL), /* is_intersection */ false);
1492
6.69k
  } else if (type_mask & MAY_BE_FALSE) {
1493
29
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE), /* is_intersection */ false);
1494
6.66k
  } else if (type_mask & MAY_BE_TRUE) {
1495
7
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_TRUE), /* is_intersection */ false);
1496
7
  }
1497
7.00k
  if (type_mask & MAY_BE_VOID) {
1498
32
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID), /* is_intersection */ false);
1499
32
  }
1500
7.00k
  if (type_mask & MAY_BE_NEVER) {
1501
4
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NEVER), /* is_intersection */ false);
1502
4
  }
1503
1504
7.00k
  if (type_mask & MAY_BE_NULL) {
1505
1.03k
    bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
1506
1.03k
    bool has_intersection = !str || memchr(ZSTR_VAL(str), '&', ZSTR_LEN(str)) != NULL;
1507
1.03k
    if (!is_union && !has_intersection) {
1508
955
      zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
1509
955
      zend_string_release(str);
1510
955
      return nullable_str;
1511
955
    }
1512
1513
83
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE), /* is_intersection */ false);
1514
83
  }
1515
6.05k
  return str;
1516
7.00k
}
1517
1518
3.42k
ZEND_API zend_string *zend_type_to_string(zend_type type) {
1519
3.42k
  return zend_type_to_string_resolved(type, NULL);
1520
3.42k
}
1521
1522
2.56k
static bool is_generator_compatible_class_type(const zend_string *name) {
1523
2.56k
  return zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_TRAVERSABLE))
1524
2.13k
    || zend_string_equals_literal_ci(name, "Iterator")
1525
1.45k
    || zend_string_equals_literal_ci(name, "Generator");
1526
2.56k
}
1527
1528
static void zend_mark_function_as_generator(void) /* {{{ */
1529
25.9k
{
1530
25.9k
  if (!CG(active_op_array)->function_name) {
1531
76
    zend_error_noreturn(E_COMPILE_ERROR,
1532
76
      "The \"yield\" expression can only be used inside a function");
1533
76
  }
1534
1535
25.8k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
1536
1.23k
    const zend_type return_type = CG(active_op_array)->arg_info[-1].type;
1537
1.23k
    bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_OBJECT) != 0;
1538
1.23k
    if (!valid_type) {
1539
1.22k
      const zend_type *single_type;
1540
3.79k
      ZEND_TYPE_FOREACH(return_type, single_type) {
1541
3.79k
        if (ZEND_TYPE_HAS_NAME(*single_type)
1542
2.56k
            && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) {
1543
1.20k
          valid_type = true;
1544
1.20k
          break;
1545
1.20k
        }
1546
3.79k
      } ZEND_TYPE_FOREACH_END();
1547
1.22k
    }
1548
1549
1.23k
    if (!valid_type) {
1550
25
      zend_string *str = zend_type_to_string(return_type);
1551
25
      zend_error_noreturn(E_COMPILE_ERROR,
1552
25
        "Generator return type must be a supertype of Generator, %s given",
1553
25
        ZSTR_VAL(str));
1554
25
    }
1555
1.23k
  }
1556
1557
25.8k
  CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR;
1558
25.8k
}
1559
/* }}} */
1560
1561
ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal) /* {{{ */
1562
13.7k
{
1563
13.7k
  size_t prop_name_length = 1 + src1_length + 1 + src2_length;
1564
13.7k
  zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
1565
1566
13.7k
  ZSTR_VAL(prop_name)[0] = '\0';
1567
13.7k
  memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
1568
13.7k
  memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
1569
13.7k
  return prop_name;
1570
13.7k
}
1571
/* }}} */
1572
1573
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) /* {{{ */
1574
1.08M
{
1575
1.08M
  size_t class_name_len;
1576
1.08M
  size_t anonclass_src_len;
1577
1578
1.08M
  *class_name = NULL;
1579
1580
1.08M
  if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
1581
971k
    *prop_name = ZSTR_VAL(name);
1582
971k
    if (prop_len) {
1583
665k
      *prop_len = ZSTR_LEN(name);
1584
665k
    }
1585
971k
    return SUCCESS;
1586
971k
  }
1587
115k
  if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
1588
1.07k
    zend_error(E_NOTICE, "Illegal member variable name");
1589
1.07k
    *prop_name = ZSTR_VAL(name);
1590
1.07k
    if (prop_len) {
1591
1.02k
      *prop_len = ZSTR_LEN(name);
1592
1.02k
    }
1593
1.07k
    return FAILURE;
1594
1.07k
  }
1595
1596
114k
  class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
1597
114k
  if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
1598
619
    zend_error(E_NOTICE, "Corrupt member variable name");
1599
619
    *prop_name = ZSTR_VAL(name);
1600
619
    if (prop_len) {
1601
595
      *prop_len = ZSTR_LEN(name);
1602
595
    }
1603
619
    return FAILURE;
1604
619
  }
1605
1606
113k
  *class_name = ZSTR_VAL(name) + 1;
1607
113k
  anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2);
1608
113k
  if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) {
1609
24.3k
    class_name_len += anonclass_src_len + 1;
1610
24.3k
  }
1611
113k
  *prop_name = ZSTR_VAL(name) + class_name_len + 2;
1612
113k
  if (prop_len) {
1613
74.8k
    *prop_len = ZSTR_LEN(name) - class_name_len - 2;
1614
74.8k
  }
1615
113k
  return SUCCESS;
1616
114k
}
1617
/* }}} */
1618
1619
static bool array_is_const_ex(const zend_array *array, uint32_t *max_checks)
1620
169
{
1621
169
  if (zend_hash_num_elements(array) > *max_checks) {
1622
3
    return false;
1623
3
  }
1624
166
  *max_checks -= zend_hash_num_elements(array);
1625
1626
166
  zval *element;
1627
484
  ZEND_HASH_FOREACH_VAL(array, element) {
1628
484
    if (Z_TYPE_P(element) < IS_ARRAY) {
1629
9
      continue;
1630
150
    } else if (Z_TYPE_P(element) == IS_ARRAY) {
1631
150
      if (!array_is_const_ex(array, max_checks)) {
1632
150
        return false;
1633
150
      }
1634
150
    } else {
1635
0
      return false;
1636
0
    }
1637
484
  } ZEND_HASH_FOREACH_END();
1638
1639
16
  return true;
1640
166
}
1641
1642
static bool array_is_const(const zend_array *array)
1643
19
{
1644
19
  uint32_t max_checks = 50;
1645
19
  return array_is_const_ex(array, &max_checks);
1646
19
}
1647
1648
3.29k
static bool can_ct_eval_const(const zend_constant *c) {
1649
3.29k
  if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
1650
35
    return 0;
1651
35
  }
1652
3.25k
  if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
1653
3.25k
      && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
1654
2.68k
      && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
1655
2.68k
        && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
1656
2.68k
    return 1;
1657
2.68k
  }
1658
571
  if (Z_TYPE(c->value) < IS_ARRAY
1659
571
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1660
2
    return 1;
1661
569
  } else if (Z_TYPE(c->value) == IS_ARRAY
1662
0
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)
1663
0
      && array_is_const(Z_ARR(c->value))) {
1664
0
    return 1;
1665
0
  }
1666
569
  return 0;
1667
571
}
1668
1669
static bool zend_try_ct_eval_const(zval *zv, zend_string *name, bool is_fully_qualified) /* {{{ */
1670
9.04M
{
1671
  /* Substitute true, false and null (including unqualified usage in namespaces)
1672
   * before looking up the possibly namespaced name. */
1673
9.04M
  const char *lookup_name = ZSTR_VAL(name);
1674
9.04M
  size_t lookup_len = ZSTR_LEN(name);
1675
1676
9.04M
  if (!is_fully_qualified) {
1677
9.03M
    zend_get_unqualified_name(name, &lookup_name, &lookup_len);
1678
9.03M
  }
1679
1680
9.04M
  zend_constant *c;
1681
9.04M
  if ((c = zend_get_special_const(lookup_name, lookup_len))) {
1682
31.5k
    ZVAL_COPY_VALUE(zv, &c->value);
1683
31.5k
    return 1;
1684
31.5k
  }
1685
9.00M
  c = zend_hash_find_ptr(EG(zend_constants), name);
1686
9.00M
  if (c && can_ct_eval_const(c)) {
1687
2.68k
    ZVAL_COPY_OR_DUP(zv, &c->value);
1688
2.68k
    return 1;
1689
2.68k
  }
1690
9.00M
  return 0;
1691
9.00M
}
1692
/* }}} */
1693
1694
static inline bool zend_is_scope_known(void) /* {{{ */
1695
83.9k
{
1696
83.9k
  if (!CG(active_op_array)) {
1697
    /* This can only happen when evaluating a default value string. */
1698
0
    return 0;
1699
0
  }
1700
1701
83.9k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
1702
    /* Closures can be rebound to a different scope */
1703
76.7k
    return 0;
1704
76.7k
  }
1705
1706
7.27k
  if (!CG(active_class_entry)) {
1707
    /* The scope is known if we're in a free function (no scope), but not if we're in
1708
     * a file/eval (which inherits including/eval'ing scope). */
1709
1.79k
    return CG(active_op_array)->function_name != NULL;
1710
1.79k
  }
1711
1712
  /* For traits self etc refers to the using class, not the trait itself */
1713
5.47k
  return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
1714
7.27k
}
1715
/* }}} */
1716
1717
static inline bool class_name_refers_to_active_ce(const zend_string *class_name, uint32_t fetch_type) /* {{{ */
1718
184k
{
1719
184k
  if (!CG(active_class_entry)) {
1720
180k
    return 0;
1721
180k
  }
1722
3.23k
  if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
1723
368
    return 1;
1724
368
  }
1725
2.86k
  return fetch_type == ZEND_FETCH_CLASS_DEFAULT
1726
2.17k
    && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
1727
3.23k
}
1728
/* }}} */
1729
1730
uint32_t zend_get_class_fetch_type(const zend_string *name) /* {{{ */
1731
4.05M
{
1732
4.05M
  if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1733
43.2k
    return ZEND_FETCH_CLASS_SELF;
1734
4.01M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT))) {
1735
4.73k
    return ZEND_FETCH_CLASS_PARENT;
1736
4.00M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_STATIC))) {
1737
1.22k
    return ZEND_FETCH_CLASS_STATIC;
1738
4.00M
  } else {
1739
4.00M
    return ZEND_FETCH_CLASS_DEFAULT;
1740
4.00M
  }
1741
4.05M
}
1742
/* }}} */
1743
1744
static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
1745
437k
{
1746
  /* Fully qualified names are always default refs */
1747
437k
  if (name_ast->attr == ZEND_NAME_FQ) {
1748
1.51k
    return ZEND_FETCH_CLASS_DEFAULT;
1749
1.51k
  }
1750
1751
436k
  return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
1752
437k
}
1753
/* }}} */
1754
1755
static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type)
1756
82.4k
{
1757
82.4k
  zend_string *class_name = zend_ast_get_str(ast);
1758
82.4k
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) {
1759
4
    zend_error_noreturn(E_COMPILE_ERROR,
1760
4
      "Cannot use \"%s\" as %s, as it is reserved",
1761
4
      ZSTR_VAL(class_name), type);
1762
4
  }
1763
82.4k
  return zend_resolve_class_name(class_name, ast->attr);
1764
82.4k
}
1765
1766
static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
1767
43.8k
{
1768
43.8k
  if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) {
1769
1.53k
    zend_class_entry *ce = CG(active_class_entry);
1770
1.53k
    if (!ce) {
1771
8
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
1772
8
        fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
1773
8
        fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
1774
1.52k
    } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) {
1775
11
      zend_error_noreturn(E_COMPILE_ERROR,
1776
11
        "Cannot use \"parent\" when current class scope has no parent");
1777
11
    }
1778
1.53k
  }
1779
43.8k
}
1780
/* }}} */
1781
1782
static bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */
1783
5.76k
{
1784
5.76k
  uint32_t fetch_type;
1785
5.76k
  const zval *class_name;
1786
1787
5.76k
  if (class_ast->kind != ZEND_AST_ZVAL) {
1788
1.77k
    return 0;
1789
1.77k
  }
1790
1791
3.99k
  class_name = zend_ast_get_zval(class_ast);
1792
1793
3.99k
  if (Z_TYPE_P(class_name) != IS_STRING) {
1794
2
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1795
2
  }
1796
1797
3.98k
  fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name));
1798
3.98k
  zend_ensure_valid_class_fetch_type(fetch_type);
1799
1800
3.98k
  switch (fetch_type) {
1801
1.06k
    case ZEND_FETCH_CLASS_SELF:
1802
1.06k
      if (CG(active_class_entry) && zend_is_scope_known()) {
1803
256
        ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
1804
256
        return 1;
1805
256
      }
1806
810
      return 0;
1807
1.25k
    case ZEND_FETCH_CLASS_PARENT:
1808
1.25k
      if (CG(active_class_entry) && CG(active_class_entry)->parent_name
1809
381
          && zend_is_scope_known()) {
1810
381
        ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name);
1811
381
        return 1;
1812
381
      }
1813
871
      return 0;
1814
159
    case ZEND_FETCH_CLASS_STATIC:
1815
159
      return 0;
1816
1.50k
    case ZEND_FETCH_CLASS_DEFAULT:
1817
1.50k
      ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
1818
1.50k
      return 1;
1819
3.98k
    EMPTY_SWITCH_DEFAULT_CASE()
1820
3.98k
  }
1821
3.98k
}
1822
/* }}} */
1823
1824
/* We don't use zend_verify_const_access because we need to deal with unlinked classes. */
1825
static bool zend_verify_ct_const_access(const zend_class_constant *c, const zend_class_entry *scope)
1826
122
{
1827
122
  if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED) {
1828
1
    return 0;
1829
121
  } else if (c->ce->ce_flags & ZEND_ACC_TRAIT) {
1830
    /* This condition is only met on directly accessing trait constants,
1831
     * because the ce is replaced to the class entry of the composing class
1832
     * on binding. */
1833
1
    return 0;
1834
120
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) {
1835
53
    return 1;
1836
67
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) {
1837
66
    return c->ce == scope;
1838
66
  } else {
1839
1
    zend_class_entry *ce = c->ce;
1840
1
    while (1) {
1841
1
      if (ce == scope) {
1842
0
        return 1;
1843
0
      }
1844
1
      if (!ce->parent) {
1845
1
        break;
1846
1
      }
1847
0
      if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
1848
0
        ce = ce->parent;
1849
0
      } else {
1850
0
        ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name);
1851
0
        if (!ce) {
1852
0
          break;
1853
0
        }
1854
0
      }
1855
0
    }
1856
    /* Reverse case cannot be true during compilation */
1857
1
    return 0;
1858
1
  }
1859
122
}
1860
1861
static bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
1862
184k
{
1863
184k
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
1864
184k
  zend_class_constant *cc;
1865
184k
  zval *c;
1866
1867
184k
  if (class_name_refers_to_active_ce(class_name, fetch_type)) {
1868
403
    cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
1869
183k
  } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1870
500
    const zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name);
1871
500
    if (ce) {
1872
47
      cc = zend_hash_find_ptr(&ce->constants_table, name);
1873
453
    } else {
1874
453
      return 0;
1875
453
    }
1876
183k
  } else {
1877
183k
    return 0;
1878
183k
  }
1879
1880
450
  if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
1881
291
    return 0;
1882
291
  }
1883
1884
159
  if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) {
1885
40
    return 0;
1886
40
  }
1887
1888
119
  c = &cc->value;
1889
1890
  /* Substitute case-sensitive (or lowercase) persistent class constants */
1891
119
  if (Z_TYPE_P(c) < IS_ARRAY) {
1892
82
    ZVAL_COPY_OR_DUP(zv, c);
1893
82
    return 1;
1894
82
  } else if (Z_TYPE_P(c) == IS_ARRAY && array_is_const(Z_ARR_P(c))) {
1895
16
    ZVAL_COPY_OR_DUP(zv, c);
1896
16
    return 1;
1897
16
  }
1898
1899
21
  return 0;
1900
119
}
1901
/* }}} */
1902
1903
static void zend_add_to_list(void *result, void *item) /* {{{ */
1904
134k
{
1905
134k
  void** list = *(void**)result;
1906
134k
  size_t n = 0;
1907
1908
134k
  if (list) {
1909
295k
    while (list[n]) {
1910
204k
      n++;
1911
204k
    }
1912
90.6k
  }
1913
1914
134k
  list = erealloc(list, sizeof(void*) * (n+2));
1915
1916
134k
  list[n]   = item;
1917
134k
  list[n+1] = NULL;
1918
1919
134k
  *(void**)result = list;
1920
134k
}
1921
/* }}} */
1922
1923
static void zend_do_extended_stmt(znode* result) /* {{{ */
1924
1.62M
{
1925
1.62M
  zend_op *opline;
1926
1927
1.62M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) {
1928
1.62M
    return;
1929
1.62M
  }
1930
1931
0
  opline = get_next_op();
1932
1933
0
  opline->opcode = ZEND_EXT_STMT;
1934
0
  if (result) {
1935
0
    SET_NODE(opline->op1, result);
1936
0
  }
1937
0
}
1938
/* }}} */
1939
1940
static void zend_do_extended_fcall_begin(void) /* {{{ */
1941
3.84M
{
1942
3.84M
  zend_op *opline;
1943
1944
3.84M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1945
3.84M
    return;
1946
3.84M
  }
1947
1948
0
  opline = get_next_op();
1949
1950
0
  opline->opcode = ZEND_EXT_FCALL_BEGIN;
1951
0
}
1952
/* }}} */
1953
1954
static void zend_do_extended_fcall_end(void) /* {{{ */
1955
3.84M
{
1956
3.84M
  zend_op *opline;
1957
1958
3.84M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1959
3.84M
    return;
1960
3.84M
  }
1961
1962
0
  opline = get_next_op();
1963
1964
0
  opline->opcode = ZEND_EXT_FCALL_END;
1965
0
}
1966
/* }}} */
1967
1968
0
ZEND_API bool zend_is_auto_global_str(const char *name, size_t len) /* {{{ */ {
1969
0
  zend_auto_global *auto_global;
1970
1971
0
  if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) {
1972
0
    if (auto_global->armed) {
1973
0
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1974
0
    }
1975
0
    return 1;
1976
0
  }
1977
0
  return 0;
1978
0
}
1979
/* }}} */
1980
1981
ZEND_API bool zend_is_auto_global(zend_string *name) /* {{{ */
1982
2.25M
{
1983
2.25M
  zend_auto_global *auto_global;
1984
1985
2.25M
  if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) {
1986
5.77k
    if (auto_global->armed) {
1987
115
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1988
115
    }
1989
5.77k
    return 1;
1990
5.77k
  }
1991
2.24M
  return 0;
1992
2.25M
}
1993
/* }}} */
1994
1995
ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
1996
112
{
1997
112
  zend_auto_global auto_global;
1998
112
  zend_result retval;
1999
2000
112
  auto_global.name = name;
2001
112
  auto_global.auto_global_callback = auto_global_callback;
2002
112
  auto_global.jit = jit;
2003
2004
112
  retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
2005
2006
112
  return retval;
2007
112
}
2008
/* }}} */
2009
2010
ZEND_API void zend_activate_auto_globals(void) /* {{{ */
2011
222k
{
2012
222k
  zend_auto_global *auto_global;
2013
2014
4.00M
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2015
4.00M
    auto_global->armed = auto_global->jit || auto_global->auto_global_callback;
2016
4.00M
  } ZEND_HASH_FOREACH_END();
2017
2018
4.00M
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2019
4.00M
    if (auto_global->armed && !auto_global->jit) {
2020
890k
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
2021
890k
    }
2022
4.00M
  } ZEND_HASH_FOREACH_END();
2023
222k
}
2024
/* }}} */
2025
2026
int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
2027
6.60M
{
2028
6.60M
  zval zv;
2029
6.60M
  int ret;
2030
2031
6.60M
  if (CG(increment_lineno)) {
2032
7.63k
    CG(zend_lineno)++;
2033
7.63k
    CG(increment_lineno) = 0;
2034
7.63k
  }
2035
2036
6.60M
  ret = lex_scan(&zv, elem);
2037
6.60M
  ZEND_ASSERT(!EG(exception) || ret == T_ERROR);
2038
6.60M
  return ret;
2039
2040
6.60M
}
2041
/* }}} */
2042
2043
ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers) /* {{{ */
2044
128k
{
2045
128k
  bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS;
2046
2047
128k
  ce->refcount = 1;
2048
128k
  ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
2049
128k
  ce->ce_flags2 = 0;
2050
2051
128k
  if (CG(compiler_options) & ZEND_COMPILE_GUARDS) {
2052
0
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2053
0
  }
2054
2055
128k
  ce->default_properties_table = NULL;
2056
128k
  ce->default_static_members_table = NULL;
2057
128k
  zend_hash_init(&ce->properties_info, 8, NULL, NULL, persistent_hashes);
2058
128k
  zend_hash_init(&ce->constants_table, 8, NULL, NULL, persistent_hashes);
2059
128k
  zend_hash_init(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes);
2060
2061
128k
  ce->doc_comment = NULL;
2062
2063
128k
  ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
2064
128k
  ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
2065
2066
128k
  ce->default_object_handlers = &std_object_handlers;
2067
128k
  ce->default_properties_count = 0;
2068
128k
  ce->default_static_members_count = 0;
2069
128k
  ce->properties_info_table = NULL;
2070
128k
  ce->attributes = NULL;
2071
128k
  ce->enum_backing_type = IS_UNDEF;
2072
128k
  ce->backed_enum_table = NULL;
2073
2074
128k
  if (nullify_handlers) {
2075
125k
    ce->constructor = NULL;
2076
125k
    ce->destructor = NULL;
2077
125k
    ce->clone = NULL;
2078
125k
    ce->__get = NULL;
2079
125k
    ce->__set = NULL;
2080
125k
    ce->__unset = NULL;
2081
125k
    ce->__isset = NULL;
2082
125k
    ce->__call = NULL;
2083
125k
    ce->__callstatic = NULL;
2084
125k
    ce->__tostring = NULL;
2085
125k
    ce->__serialize = NULL;
2086
125k
    ce->__unserialize = NULL;
2087
125k
    ce->__debugInfo = NULL;
2088
125k
    ce->create_object = NULL;
2089
125k
    ce->get_iterator = NULL;
2090
125k
    ce->iterator_funcs_ptr = NULL;
2091
125k
    ce->arrayaccess_funcs_ptr = NULL;
2092
125k
    ce->get_static_method = NULL;
2093
125k
    ce->parent = NULL;
2094
125k
    ce->parent_name = NULL;
2095
125k
    ce->num_interfaces = 0;
2096
125k
    ce->interfaces = NULL;
2097
125k
    ce->num_traits = 0;
2098
125k
    ce->num_hooked_props = 0;
2099
125k
    ce->num_hooked_prop_variance_checks = 0;
2100
125k
    ce->trait_names = NULL;
2101
125k
    ce->trait_aliases = NULL;
2102
125k
    ce->trait_precedences = NULL;
2103
125k
    ce->serialize = NULL;
2104
125k
    ce->unserialize = NULL;
2105
125k
    if (ce->type == ZEND_INTERNAL_CLASS) {
2106
0
      ce->info.internal.module = NULL;
2107
0
      ce->info.internal.builtin_functions = NULL;
2108
0
    }
2109
125k
  }
2110
128k
}
2111
/* }}} */
2112
2113
ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
2114
0
{
2115
0
  return op_array->vars[EX_VAR_TO_NUM(var)];
2116
0
}
2117
/* }}} */
2118
2119
zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
2120
0
{
2121
0
  zval *left_zv = zend_ast_get_zval(left_ast);
2122
0
  zend_string *left = Z_STR_P(left_zv);
2123
0
  zend_string *right = zend_ast_get_str(right_ast);
2124
2125
0
  zend_string *result;
2126
0
  size_t left_len = ZSTR_LEN(left);
2127
0
  size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
2128
2129
0
  result = zend_string_extend(left, len, 0);
2130
0
  ZSTR_VAL(result)[left_len] = '\\';
2131
0
  memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
2132
0
  ZSTR_VAL(result)[len] = '\0';
2133
0
  zend_string_release_ex(right, 0);
2134
2135
0
  ZVAL_STR(left_zv, result);
2136
0
  return left_ast;
2137
0
}
2138
/* }}} */
2139
2140
zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
2141
1.46k
{
2142
1.46k
  zval *zv = zend_ast_get_zval(ast);
2143
1.46k
  if (Z_TYPE_P(zv) == IS_LONG) {
2144
1.20k
    if (Z_LVAL_P(zv) == 0) {
2145
535
      ZVAL_NEW_STR(zv, ZSTR_INIT_LITERAL("-0", 0));
2146
673
    } else {
2147
673
      ZEND_ASSERT(Z_LVAL_P(zv) > 0);
2148
673
      Z_LVAL_P(zv) *= -1;
2149
673
    }
2150
1.20k
  } else if (Z_TYPE_P(zv) == IS_STRING) {
2151
254
    size_t orig_len = Z_STRLEN_P(zv);
2152
254
    Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0);
2153
254
    memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
2154
254
    Z_STRVAL_P(zv)[0] = '-';
2155
254
  } else {
2156
0
    ZEND_UNREACHABLE();
2157
0
  }
2158
1.46k
  return ast;
2159
1.46k
}
2160
/* }}} */
2161
2162
static void zend_verify_namespace(void) /* {{{ */
2163
258k
{
2164
258k
  if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
2165
23
    zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
2166
23
  }
2167
258k
}
2168
/* }}} */
2169
2170
/* {{{ zend_dirname
2171
   Returns directory name component of path */
2172
ZEND_API size_t zend_dirname(char *path, size_t len)
2173
933
{
2174
933
  char *end = path + len - 1;
2175
933
  unsigned int len_adjust = 0;
2176
2177
#ifdef ZEND_WIN32
2178
  /* Note that on Win32 CWD is per drive (heritage from CP/M).
2179
   * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
2180
   */
2181
  if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
2182
    /* Skip over the drive spec (if any) so as not to change */
2183
    path += 2;
2184
    len_adjust += 2;
2185
    if (2 == len) {
2186
      /* Return "c:" on Win32 for dirname("c:").
2187
       * It would be more consistent to return "c:."
2188
       * but that would require making the string *longer*.
2189
       */
2190
      return len;
2191
    }
2192
  }
2193
#endif
2194
2195
933
  if (len == 0) {
2196
    /* Illegal use of this function */
2197
0
    return 0;
2198
0
  }
2199
2200
  /* Strip trailing slashes */
2201
933
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2202
0
    end--;
2203
0
  }
2204
933
  if (end < path) {
2205
    /* The path only contained slashes */
2206
0
    path[0] = DEFAULT_SLASH;
2207
0
    path[1] = '\0';
2208
0
    return 1 + len_adjust;
2209
0
  }
2210
2211
  /* Strip filename */
2212
10.2k
  while (end >= path && !IS_SLASH_P_EX(end, end == path)) {
2213
9.34k
    end--;
2214
9.34k
  }
2215
933
  if (end < path) {
2216
    /* No slash found, therefore return '.' */
2217
809
    path[0] = '.';
2218
809
    path[1] = '\0';
2219
809
    return 1 + len_adjust;
2220
809
  }
2221
2222
  /* Strip slashes which came before the file name */
2223
248
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2224
124
    end--;
2225
124
  }
2226
124
  if (end < path) {
2227
0
    path[0] = DEFAULT_SLASH;
2228
0
    path[1] = '\0';
2229
0
    return 1 + len_adjust;
2230
0
  }
2231
124
  *(end+1) = '\0';
2232
2233
124
  return (size_t)(end + 1 - path) + len_adjust;
2234
124
}
2235
/* }}} */
2236
2237
static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */
2238
535k
{
2239
535k
  uint_fast8_t factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3;
2240
2241
535k
  switch (type) {
2242
306k
    case BP_VAR_R:
2243
306k
      opline->result_type = IS_TMP_VAR;
2244
306k
      result->op_type = IS_TMP_VAR;
2245
306k
      return;
2246
107k
    case BP_VAR_W:
2247
107k
      opline->opcode += 1 * factor;
2248
107k
      return;
2249
11.6k
    case BP_VAR_RW:
2250
11.6k
      opline->opcode += 2 * factor;
2251
11.6k
      return;
2252
88.9k
    case BP_VAR_IS:
2253
88.9k
      opline->result_type = IS_TMP_VAR;
2254
88.9k
      result->op_type = IS_TMP_VAR;
2255
88.9k
      opline->opcode += 3 * factor;
2256
88.9k
      return;
2257
15.1k
    case BP_VAR_FUNC_ARG:
2258
15.1k
      opline->opcode += 4 * factor;
2259
15.1k
      return;
2260
6.11k
    case BP_VAR_UNSET:
2261
6.11k
      opline->opcode += 5 * factor;
2262
6.11k
      return;
2263
535k
    EMPTY_SWITCH_DEFAULT_CASE()
2264
535k
  }
2265
535k
}
2266
/* }}} */
2267
2268
static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
2269
4.63M
{
2270
4.63M
  opline->result_type = IS_VAR;
2271
4.63M
  opline->result.var = get_temporary_variable();
2272
4.63M
  GET_NODE(result, opline->result);
2273
4.63M
}
2274
/* }}} */
2275
2276
static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
2277
31.5M
{
2278
31.5M
  opline->result_type = IS_TMP_VAR;
2279
31.5M
  opline->result.var = get_temporary_variable();
2280
31.5M
  GET_NODE(result, opline->result);
2281
31.5M
}
2282
/* }}} */
2283
2284
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2285
33.8M
{
2286
33.8M
  zend_op *opline = get_next_op();
2287
33.8M
  opline->opcode = opcode;
2288
2289
33.8M
  if (op1 != NULL) {
2290
27.1M
    SET_NODE(opline->op1, op1);
2291
27.1M
  }
2292
2293
33.8M
  if (op2 != NULL) {
2294
1.94M
    SET_NODE(opline->op2, op2);
2295
1.94M
  }
2296
2297
33.8M
  if (result) {
2298
4.32M
    zend_make_var_result(result, opline);
2299
4.32M
  }
2300
33.8M
  return opline;
2301
33.8M
}
2302
/* }}} */
2303
2304
static zend_op *zend_emit_op_tmp(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2305
34.0M
{
2306
34.0M
  zend_op *opline = get_next_op();
2307
34.0M
  opline->opcode = opcode;
2308
2309
34.0M
  if (op1 != NULL) {
2310
8.14M
    SET_NODE(opline->op1, op1);
2311
8.14M
  }
2312
2313
34.0M
  if (op2 != NULL) {
2314
2.93M
    SET_NODE(opline->op2, op2);
2315
2.93M
  }
2316
2317
34.0M
  if (result) {
2318
31.5M
    zend_make_tmp_result(result, opline);
2319
31.5M
  }
2320
2321
34.0M
  return opline;
2322
34.0M
}
2323
/* }}} */
2324
2325
static void zend_emit_tick(void) /* {{{ */
2326
150k
{
2327
150k
  zend_op *opline;
2328
2329
  /* This prevents a double TICK generated by the parser statement of "declare()" */
2330
150k
  if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
2331
3.06k
    return;
2332
3.06k
  }
2333
2334
147k
  opline = get_next_op();
2335
2336
147k
  opline->opcode = ZEND_TICKS;
2337
147k
  opline->extended_value = FC(declarables).ticks;
2338
147k
}
2339
/* }}} */
2340
2341
static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
2342
187k
{
2343
187k
  return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
2344
187k
}
2345
/* }}} */
2346
2347
static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
2348
735k
{
2349
735k
  uint32_t opnum = get_next_op_number();
2350
735k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
2351
735k
  opline->op1.opline_num = opnum_target;
2352
735k
  return opnum;
2353
735k
}
2354
/* }}} */
2355
2356
ZEND_API bool zend_is_smart_branch(const zend_op *opline) /* {{{ */
2357
101k
{
2358
101k
  switch (opline->opcode) {
2359
1.65k
    case ZEND_IS_IDENTICAL:
2360
1.89k
    case ZEND_IS_NOT_IDENTICAL:
2361
45.3k
    case ZEND_IS_EQUAL:
2362
45.9k
    case ZEND_IS_NOT_EQUAL:
2363
51.7k
    case ZEND_IS_SMALLER:
2364
52.2k
    case ZEND_IS_SMALLER_OR_EQUAL:
2365
84.4k
    case ZEND_CASE:
2366
86.1k
    case ZEND_CASE_STRICT:
2367
86.4k
    case ZEND_ISSET_ISEMPTY_CV:
2368
86.4k
    case ZEND_ISSET_ISEMPTY_VAR:
2369
86.5k
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2370
86.5k
    case ZEND_ISSET_ISEMPTY_PROP_OBJ:
2371
86.6k
    case ZEND_ISSET_ISEMPTY_STATIC_PROP:
2372
86.9k
    case ZEND_INSTANCEOF:
2373
87.1k
    case ZEND_TYPE_CHECK:
2374
87.1k
    case ZEND_DEFINED:
2375
87.1k
    case ZEND_IN_ARRAY:
2376
87.2k
    case ZEND_ARRAY_KEY_EXISTS:
2377
87.2k
      return 1;
2378
13.8k
    default:
2379
13.8k
      return 0;
2380
101k
  }
2381
101k
}
2382
/* }}} */
2383
2384
static inline uint32_t zend_emit_cond_jump(uint8_t opcode, znode *cond, uint32_t opnum_target) /* {{{ */
2385
119k
{
2386
119k
  uint32_t opnum = get_next_op_number();
2387
119k
  zend_op *opline;
2388
2389
119k
  if (cond->op_type == IS_TMP_VAR && opnum > 0) {
2390
99.6k
    opline = CG(active_op_array)->opcodes + opnum - 1;
2391
99.6k
    if (opline->result_type == IS_TMP_VAR
2392
97.0k
     && opline->result.var == cond->u.op.var
2393
97.0k
     && zend_is_smart_branch(opline)) {
2394
87.1k
      if (opcode == ZEND_JMPZ) {
2395
12.3k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ;
2396
74.8k
      } else {
2397
74.8k
        ZEND_ASSERT(opcode == ZEND_JMPNZ);
2398
74.8k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ;
2399
74.8k
      }
2400
87.1k
    }
2401
99.6k
  }
2402
119k
  opline = zend_emit_op(NULL, opcode, cond, NULL);
2403
119k
  opline->op2.opline_num = opnum_target;
2404
119k
  return opnum;
2405
119k
}
2406
/* }}} */
2407
2408
static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
2409
861k
{
2410
861k
  zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
2411
861k
  switch (opline->opcode) {
2412
726k
    case ZEND_JMP:
2413
726k
      opline->op1.opline_num = opnum_target;
2414
726k
      break;
2415
29.5k
    case ZEND_JMPZ:
2416
103k
    case ZEND_JMPNZ:
2417
109k
    case ZEND_JMPZ_EX:
2418
116k
    case ZEND_JMPNZ_EX:
2419
119k
    case ZEND_JMP_SET:
2420
134k
    case ZEND_COALESCE:
2421
134k
    case ZEND_JMP_NULL:
2422
135k
    case ZEND_BIND_INIT_STATIC_OR_JMP:
2423
135k
    case ZEND_JMP_FRAMELESS:
2424
135k
      opline->op2.opline_num = opnum_target;
2425
135k
      break;
2426
861k
    EMPTY_SWITCH_DEFAULT_CASE()
2427
861k
  }
2428
861k
}
2429
/* }}} */
2430
2431
static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
2432
853k
{
2433
853k
  zend_update_jump_target(opnum_jump, get_next_op_number());
2434
853k
}
2435
/* }}} */
2436
2437
static inline zend_op *zend_delayed_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2438
314k
{
2439
314k
  zend_op tmp_opline;
2440
2441
314k
  init_op(&tmp_opline);
2442
2443
314k
  tmp_opline.opcode = opcode;
2444
314k
  if (op1 != NULL) {
2445
314k
    SET_NODE(tmp_opline.op1, op1);
2446
314k
  }
2447
314k
  if (op2 != NULL) {
2448
295k
    SET_NODE(tmp_opline.op2, op2);
2449
295k
  }
2450
314k
  if (result) {
2451
311k
    zend_make_var_result(result, &tmp_opline);
2452
311k
  }
2453
2454
314k
  zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
2455
314k
  return zend_stack_top(&CG(delayed_oplines_stack));
2456
314k
}
2457
/* }}} */
2458
2459
static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
2460
316k
{
2461
316k
  return zend_stack_count(&CG(delayed_oplines_stack));
2462
316k
}
2463
/* }}} */
2464
2465
static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
2466
315k
{
2467
315k
  zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
2468
315k
  uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
2469
2470
315k
  ZEND_ASSERT(count >= offset);
2471
629k
  for (i = offset; i < count; ++i) {
2472
313k
    if (EXPECTED(oplines[i].opcode != ZEND_NOP)) {
2473
306k
      opline = get_next_op();
2474
306k
      memcpy(opline, &oplines[i], sizeof(zend_op));
2475
306k
    } else {
2476
7.59k
      opline = CG(active_op_array)->opcodes + oplines[i].extended_value;
2477
7.59k
    }
2478
313k
  }
2479
2480
315k
  CG(delayed_oplines_stack).top = offset;
2481
315k
  return opline;
2482
315k
}
2483
/* }}} */
2484
2485
static bool zend_ast_kind_is_short_circuited(zend_ast_kind ast_kind)
2486
46.4M
{
2487
46.4M
  switch (ast_kind) {
2488
244k
    case ZEND_AST_DIM:
2489
281k
    case ZEND_AST_PROP:
2490
359k
    case ZEND_AST_NULLSAFE_PROP:
2491
377k
    case ZEND_AST_STATIC_PROP:
2492
435k
    case ZEND_AST_METHOD_CALL:
2493
441k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2494
571k
    case ZEND_AST_STATIC_CALL:
2495
571k
      return 1;
2496
45.9M
    default:
2497
45.9M
      return 0;
2498
46.4M
  }
2499
46.4M
}
2500
2501
static bool zend_ast_is_short_circuited(const zend_ast *ast)
2502
436k
{
2503
436k
  switch (ast->kind) {
2504
108k
    case ZEND_AST_DIM:
2505
125k
    case ZEND_AST_PROP:
2506
134k
    case ZEND_AST_STATIC_PROP:
2507
136k
    case ZEND_AST_METHOD_CALL:
2508
140k
    case ZEND_AST_STATIC_CALL:
2509
140k
      return zend_ast_is_short_circuited(ast->child[0]);
2510
296
    case ZEND_AST_NULLSAFE_PROP:
2511
388
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2512
388
      return 1;
2513
296k
    default:
2514
296k
      return 0;
2515
436k
  }
2516
436k
}
2517
2518
static void zend_assert_not_short_circuited(const zend_ast *ast)
2519
7.10k
{
2520
7.10k
  if (zend_ast_is_short_circuited(ast)) {
2521
11
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain");
2522
11
  }
2523
7.10k
}
2524
2525
/* Mark nodes that are an inner part of a short-circuiting chain.
2526
 * We should not perform a "commit" on them, as it will be performed by the outer-most node.
2527
 * We do this to avoid passing down an argument in various compile functions. */
2528
2529
582k
#define ZEND_SHORT_CIRCUITING_INNER 0x8000
2530
2531
404k
static void zend_short_circuiting_mark_inner(zend_ast *ast) {
2532
404k
  if (zend_ast_kind_is_short_circuited(ast->kind)) {
2533
188k
    ast->attr |= ZEND_SHORT_CIRCUITING_INNER;
2534
188k
  }
2535
404k
}
2536
2537
static uint32_t zend_short_circuiting_checkpoint(void)
2538
46.1M
{
2539
46.1M
  return zend_stack_count(&CG(short_circuiting_opnums));
2540
46.1M
}
2541
2542
static void zend_short_circuiting_commit(uint32_t checkpoint, znode *result, const zend_ast *ast)
2543
46.0M
{
2544
46.0M
  bool is_short_circuited = zend_ast_kind_is_short_circuited(ast->kind)
2545
45.6M
    || ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY;
2546
46.0M
  if (!is_short_circuited) {
2547
45.6M
    ZEND_ASSERT(zend_stack_count(&CG(short_circuiting_opnums)) == checkpoint
2548
45.6M
      && "Short circuiting stack should be empty");
2549
45.6M
    return;
2550
45.6M
  }
2551
2552
393k
  if (ast->attr & ZEND_SHORT_CIRCUITING_INNER) {
2553
    /* Outer-most node will commit. */
2554
45.7k
    return;
2555
45.7k
  }
2556
2557
390k
  while (zend_stack_count(&CG(short_circuiting_opnums)) != checkpoint) {
2558
43.3k
    uint32_t opnum = *(uint32_t *) zend_stack_top(&CG(short_circuiting_opnums));
2559
43.3k
    zend_op *opline = &CG(active_op_array)->opcodes[opnum];
2560
43.3k
    opline->op2.opline_num = get_next_op_number();
2561
43.3k
    SET_NODE(opline->result, result);
2562
43.3k
    opline->extended_value |=
2563
43.3k
      ast->kind == ZEND_AST_ISSET ? ZEND_SHORT_CIRCUITING_CHAIN_ISSET :
2564
43.3k
      ast->kind == ZEND_AST_EMPTY ? ZEND_SHORT_CIRCUITING_CHAIN_EMPTY :
2565
43.0k
                                    ZEND_SHORT_CIRCUITING_CHAIN_EXPR;
2566
43.3k
    zend_stack_del_top(&CG(short_circuiting_opnums));
2567
43.3k
  }
2568
347k
}
2569
2570
static void zend_emit_jmp_null(znode *obj_node, uint32_t bp_type)
2571
43.4k
{
2572
43.4k
  uint32_t jmp_null_opnum = get_next_op_number();
2573
43.4k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP_NULL, obj_node, NULL);
2574
43.4k
  if (opline->op1_type == IS_CONST) {
2575
696
    Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
2576
696
  }
2577
43.4k
  if (bp_type == BP_VAR_IS) {
2578
1.27k
    opline->extended_value |= ZEND_JMP_NULL_BP_VAR_IS;
2579
1.27k
  }
2580
43.4k
  zend_stack_push(&CG(short_circuiting_opnums), &jmp_null_opnum);
2581
43.4k
}
2582
2583
static void zend_compile_memoized_expr(znode *result, zend_ast *expr) /* {{{ */
2584
170k
{
2585
170k
  const zend_memoize_mode memoize_mode = CG(memoize_mode);
2586
170k
  if (memoize_mode == ZEND_MEMOIZE_COMPILE) {
2587
85.8k
    znode memoized_result;
2588
2589
    /* Go through normal compilation */
2590
85.8k
    CG(memoize_mode) = ZEND_MEMOIZE_NONE;
2591
85.8k
    zend_compile_expr(result, expr);
2592
85.8k
    CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
2593
2594
85.8k
    if (result->op_type == IS_VAR) {
2595
2.01k
      zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL);
2596
83.8k
    } else if (result->op_type == IS_TMP_VAR) {
2597
77.2k
      zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL);
2598
77.2k
    } else {
2599
6.58k
      if (result->op_type == IS_CONST) {
2600
5.06k
        Z_TRY_ADDREF(result->u.constant);
2601
5.06k
      }
2602
6.58k
      memoized_result = *result;
2603
6.58k
    }
2604
2605
85.8k
    zend_hash_index_update_mem(
2606
85.8k
      CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode));
2607
85.8k
  } else if (memoize_mode == ZEND_MEMOIZE_FETCH) {
2608
84.7k
    const znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr);
2609
84.7k
    *result = *memoized_result;
2610
84.7k
    if (result->op_type == IS_CONST) {
2611
4.37k
      Z_TRY_ADDREF(result->u.constant);
2612
4.37k
    }
2613
84.7k
  } else {
2614
0
    ZEND_UNREACHABLE();
2615
0
  }
2616
170k
}
2617
/* }}} */
2618
2619
static void zend_emit_return_type_check(
2620
    znode *expr, const zend_arg_info *return_info, bool implicit) /* {{{ */
2621
22.6k
{
2622
22.6k
  zend_type type = return_info->type;
2623
22.6k
  if (ZEND_TYPE_IS_SET(type)) {
2624
22.6k
    zend_op *opline;
2625
2626
    /* `return ...;` is illegal in a void function (but `return;` isn't) */
2627
22.6k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) {
2628
3.21k
      if (expr) {
2629
5
        if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
2630
2
          zend_error_noreturn(E_COMPILE_ERROR,
2631
2
            "A void %s must not return a value "
2632
2
            "(did you mean \"return;\" instead of \"return null;\"?)",
2633
2
            CG(active_class_entry) != NULL ? "method" : "function");
2634
3
        } else {
2635
3
          zend_error_noreturn(E_COMPILE_ERROR, "A void %s must not return a value",
2636
3
          CG(active_class_entry) != NULL ? "method" : "function");
2637
3
        }
2638
5
      }
2639
      /* we don't need run-time check */
2640
3.20k
      return;
2641
3.21k
    }
2642
2643
    /* `return` is illegal in a never-returning function */
2644
19.4k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_NEVER)) {
2645
      /* Implicit case handled separately using VERIFY_NEVER_TYPE opcode. */
2646
3
      ZEND_ASSERT(!implicit);
2647
3
      zend_error_noreturn(E_COMPILE_ERROR, "A never-returning %s must not return",
2648
3
        CG(active_class_entry) != NULL ? "method" : "function");
2649
0
      return;
2650
3
    }
2651
2652
19.4k
    if (!expr && !implicit) {
2653
4
      if (ZEND_TYPE_ALLOW_NULL(type)) {
2654
2
        zend_error_noreturn(E_COMPILE_ERROR,
2655
2
          "A %s with return type must return a value "
2656
2
          "(did you mean \"return null;\" instead of \"return;\"?)",
2657
2
          CG(active_class_entry) != NULL ? "method" : "function");
2658
2
      } else {
2659
2
        zend_error_noreturn(E_COMPILE_ERROR,
2660
2
          "A %s with return type must return a value",
2661
2
          CG(active_class_entry) != NULL ? "method" : "function");
2662
2
      }
2663
4
    }
2664
2665
19.4k
    if (expr && ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) {
2666
      /* we don't need run-time check for mixed return type */
2667
187
      return;
2668
187
    }
2669
2670
19.2k
    if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) {
2671
      /* we don't need run-time check */
2672
531
      return;
2673
531
    }
2674
2675
18.6k
    opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
2676
18.6k
    if (expr && expr->op_type == IS_CONST) {
2677
404
      opline->result_type = expr->op_type = IS_TMP_VAR;
2678
404
      opline->result.var = expr->u.op.var = get_temporary_variable();
2679
404
    }
2680
18.6k
  }
2681
22.6k
}
2682
/* }}} */
2683
2684
void zend_emit_final_return(bool return_one) /* {{{ */
2685
1.45M
{
2686
1.45M
  znode zn;
2687
1.45M
  zend_op *ret;
2688
1.45M
  bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
2689
2690
1.45M
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)
2691
20.3k
      && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) {
2692
20.1k
    zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
2693
2694
20.1k
    if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
2695
320
      zend_emit_op(NULL, ZEND_VERIFY_NEVER_TYPE, NULL, NULL);
2696
320
      return;
2697
320
    }
2698
2699
19.7k
    zend_emit_return_type_check(NULL, return_info, true);
2700
19.7k
  }
2701
2702
1.45M
  zn.op_type = IS_CONST;
2703
1.45M
  if (return_one) {
2704
38.7k
    ZVAL_LONG(&zn.u.constant, 1);
2705
1.41M
  } else {
2706
1.41M
    ZVAL_NULL(&zn.u.constant);
2707
1.41M
  }
2708
2709
1.45M
  ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
2710
1.45M
  ret->extended_value = -1;
2711
1.45M
}
2712
/* }}} */
2713
2714
static inline bool zend_is_variable(const zend_ast *ast) /* {{{ */
2715
5.12M
{
2716
5.12M
  return ast->kind == ZEND_AST_VAR
2717
5.05M
    || ast->kind == ZEND_AST_DIM
2718
5.03M
    || ast->kind == ZEND_AST_PROP
2719
5.03M
    || ast->kind == ZEND_AST_NULLSAFE_PROP
2720
5.02M
    || ast->kind == ZEND_AST_STATIC_PROP;
2721
5.12M
}
2722
/* }}} */
2723
2724
static inline bool zend_is_call(const zend_ast *ast) /* {{{ */
2725
5.32M
{
2726
5.32M
  return ast->kind == ZEND_AST_CALL
2727
5.19M
    || ast->kind == ZEND_AST_METHOD_CALL
2728
5.18M
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
2729
5.18M
    || ast->kind == ZEND_AST_STATIC_CALL
2730
5.18M
    || ast->kind == ZEND_AST_PIPE;
2731
5.32M
}
2732
/* }}} */
2733
2734
static inline bool zend_is_variable_or_call(const zend_ast *ast) /* {{{ */
2735
14.8k
{
2736
14.8k
  return zend_is_variable(ast) || zend_is_call(ast);
2737
14.8k
}
2738
/* }}} */
2739
2740
static inline bool zend_is_unticked_stmt(const zend_ast *ast) /* {{{ */
2741
223k
{
2742
223k
  return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL
2743
165k
    || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP
2744
163k
    || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD;
2745
223k
}
2746
/* }}} */
2747
2748
static inline bool zend_can_write_to_variable(const zend_ast *ast) /* {{{ */
2749
13.7k
{
2750
13.7k
  while (
2751
19.5k
    ast->kind == ZEND_AST_DIM
2752
17.2k
    || ast->kind == ZEND_AST_PROP
2753
13.7k
  ) {
2754
5.82k
    ast = ast->child[0];
2755
5.82k
  }
2756
2757
13.7k
  return zend_is_variable_or_call(ast) && !zend_ast_is_short_circuited(ast);
2758
13.7k
}
2759
/* }}} */
2760
2761
static inline bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
2762
21.9k
{
2763
21.9k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2764
0
    return 0;
2765
0
  }
2766
2767
21.9k
  return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
2768
21.9k
}
2769
/* }}} */
2770
2771
static inline void zend_handle_numeric_op(znode *node) /* {{{ */
2772
6.94k
{
2773
6.94k
  if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
2774
3.44k
    zend_ulong index;
2775
2776
3.44k
    if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
2777
831
      zval_ptr_dtor(&node->u.constant);
2778
831
      ZVAL_LONG(&node->u.constant, index);
2779
831
    }
2780
3.44k
  }
2781
6.94k
}
2782
/* }}} */
2783
2784
static inline void zend_handle_numeric_dim(const zend_op *opline, znode *dim_node) /* {{{ */
2785
46.2k
{
2786
46.2k
  if (Z_TYPE(dim_node->u.constant) == IS_STRING) {
2787
14.0k
    zend_ulong index;
2788
2789
14.0k
    if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) {
2790
      /* For numeric indexes we also keep the original value to use by ArrayAccess
2791
       * See bug #63217
2792
       */
2793
5.81k
      int c = zend_add_literal(&dim_node->u.constant);
2794
5.81k
      ZEND_ASSERT(opline->op2.constant + 1 == c);
2795
5.81k
      ZVAL_LONG(CT_CONSTANT(opline->op2), index);
2796
5.81k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE;
2797
5.81k
      return;
2798
5.81k
    }
2799
14.0k
  }
2800
46.2k
}
2801
/* }}} */
2802
2803
static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
2804
311k
{
2805
311k
  if (class_node->op_type == IS_CONST) {
2806
155k
    opline->op1_type = IS_CONST;
2807
155k
    opline->op1.constant = zend_add_class_name_literal(
2808
155k
      Z_STR(class_node->u.constant));
2809
155k
  } else {
2810
155k
    SET_NODE(opline->op1, class_node);
2811
155k
  }
2812
311k
}
2813
/* }}} */
2814
2815
static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */
2816
325k
{
2817
325k
  uint32_t fetch_type;
2818
2819
325k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2820
129k
    znode name_node;
2821
2822
129k
    zend_compile_expr(&name_node, name_ast);
2823
2824
129k
    if (name_node.op_type == IS_CONST) {
2825
1.31k
      zend_string *name;
2826
2827
1.31k
      if (Z_TYPE(name_node.u.constant) != IS_STRING) {
2828
4
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
2829
4
      }
2830
2831
1.31k
      name = Z_STR(name_node.u.constant);
2832
1.31k
      fetch_type = zend_get_class_fetch_type(name);
2833
2834
1.31k
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
2835
924
        result->op_type = IS_CONST;
2836
924
        ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ));
2837
924
      } else {
2838
387
        zend_ensure_valid_class_fetch_type(fetch_type);
2839
387
        result->op_type = IS_UNUSED;
2840
387
        result->u.op.num = fetch_type | fetch_flags;
2841
387
      }
2842
2843
1.31k
      zend_string_release_ex(name, 0);
2844
128k
    } else {
2845
128k
      zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
2846
128k
      opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags;
2847
128k
    }
2848
129k
    return;
2849
129k
  }
2850
2851
  /* Fully qualified names are always default refs */
2852
196k
  if (name_ast->attr == ZEND_NAME_FQ) {
2853
2.99k
    result->op_type = IS_CONST;
2854
2.99k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2855
2.99k
    return;
2856
2.99k
  }
2857
2858
193k
  fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
2859
193k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
2860
161k
    result->op_type = IS_CONST;
2861
161k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2862
161k
  } else {
2863
32.0k
    zend_ensure_valid_class_fetch_type(fetch_type);
2864
32.0k
    result->op_type = IS_UNUSED;
2865
32.0k
    result->u.op.num = fetch_type | fetch_flags;
2866
32.0k
  }
2867
193k
}
2868
/* }}} */
2869
2870
static zend_result zend_try_compile_cv(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
2871
639k
{
2872
639k
  zend_ast *name_ast = ast->child[0];
2873
639k
  if (name_ast->kind == ZEND_AST_ZVAL) {
2874
547k
    zval *zv = zend_ast_get_zval(name_ast);
2875
547k
    zend_string *name;
2876
2877
547k
    if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
2878
540k
      name = zval_make_interned_string(zv);
2879
540k
    } else {
2880
7.47k
      name = zend_new_interned_string(zval_get_string_func(zv));
2881
7.47k
    }
2882
2883
547k
    if (zend_is_auto_global(name)) {
2884
551
      return FAILURE;
2885
551
    }
2886
2887
547k
    if (!CG(context).has_assigned_to_http_response_header && zend_string_equals_literal(name, "http_response_header")) {
2888
120
      if (type == BP_VAR_R) {
2889
75
        zend_error(E_DEPRECATED,
2890
75
          "The predefined locally scoped $http_response_header variable is deprecated,"
2891
75
          " call http_get_last_response_headers() instead");
2892
75
      } else if (type == BP_VAR_W) {
2893
7
        CG(context).has_assigned_to_http_response_header = true;
2894
7
      }
2895
120
    }
2896
2897
547k
    result->op_type = IS_CV;
2898
547k
    result->u.op.var = lookup_cv(name);
2899
2900
547k
    if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) {
2901
7.47k
      zend_string_release_ex(name, 0);
2902
7.47k
    }
2903
2904
547k
    return SUCCESS;
2905
547k
  }
2906
2907
92.0k
  return FAILURE;
2908
639k
}
2909
/* }}} */
2910
2911
static zend_op *zend_compile_simple_var_no_cv(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2912
222k
{
2913
222k
  zend_ast *name_ast = ast->child[0];
2914
222k
  znode name_node;
2915
222k
  zend_op *opline;
2916
2917
222k
  zend_compile_expr(&name_node, name_ast);
2918
222k
  if (name_node.op_type == IS_CONST) {
2919
133k
    convert_to_string(&name_node.u.constant);
2920
133k
  }
2921
2922
222k
  if (delayed) {
2923
11.1k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2924
211k
  } else {
2925
211k
    opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2926
211k
  }
2927
2928
222k
  if (name_node.op_type == IS_CONST &&
2929
133k
      zend_is_auto_global(Z_STR(name_node.u.constant))) {
2930
2931
543
    opline->extended_value = ZEND_FETCH_GLOBAL;
2932
222k
  } else {
2933
    // TODO: Have a test case for this?
2934
222k
    if (name_node.op_type == IS_CONST
2935
133k
      && type == BP_VAR_R
2936
132k
      && zend_string_equals_literal(Z_STR(name_node.u.constant), "http_response_header")) {
2937
171
      zend_error(E_DEPRECATED,
2938
171
        "The predefined locally scoped $http_response_header variable is deprecated,"
2939
171
        " call http_get_last_response_headers() instead");
2940
171
    }
2941
222k
    opline->extended_value = ZEND_FETCH_LOCAL;
2942
222k
  }
2943
2944
222k
  zend_adjust_for_fetch_type(opline, result, type);
2945
222k
  return opline;
2946
222k
}
2947
/* }}} */
2948
2949
static bool is_this_fetch(const zend_ast *ast) /* {{{ */
2950
895k
{
2951
895k
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2952
715k
    const zval *name = zend_ast_get_zval(ast->child[0]);
2953
715k
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals(Z_STR_P(name), ZSTR_KNOWN(ZEND_STR_THIS));
2954
715k
  }
2955
2956
179k
  return 0;
2957
895k
}
2958
/* }}} */
2959
2960
static bool is_globals_fetch(const zend_ast *ast)
2961
6.17M
{
2962
6.17M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2963
795k
    const zval *name = zend_ast_get_zval(ast->child[0]);
2964
795k
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "GLOBALS");
2965
795k
  }
2966
2967
5.37M
  return 0;
2968
6.17M
}
2969
2970
static bool is_global_var_fetch(const zend_ast *ast)
2971
222k
{
2972
222k
  return ast->kind == ZEND_AST_DIM && is_globals_fetch(ast->child[0]);
2973
222k
}
2974
2975
static bool this_guaranteed_exists(void) /* {{{ */
2976
5.41k
{
2977
5.41k
  const zend_oparray_context *ctx = &CG(context);
2978
8.75k
  while (ctx) {
2979
    /* Instance methods always have a $this.
2980
     * This also includes closures that have a scope and use $this. */
2981
8.75k
    const zend_op_array *op_array = ctx->op_array;
2982
8.75k
    if (op_array->fn_flags & ZEND_ACC_STATIC) {
2983
39
      return false;
2984
8.71k
    } else if (op_array->scope) {
2985
3.41k
      return true;
2986
5.29k
    } else if (!(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
2987
1.96k
      return false;
2988
1.96k
    }
2989
3.33k
    ctx = ctx->prev;
2990
3.33k
  }
2991
0
  return false;
2992
5.41k
}
2993
/* }}} */
2994
2995
static zend_op *zend_compile_simple_var(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2996
615k
{
2997
615k
  if (is_this_fetch(ast)) {
2998
1.32k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL);
2999
1.32k
    if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
3000
1.24k
      opline->result_type = IS_TMP_VAR;
3001
1.24k
      result->op_type = IS_TMP_VAR;
3002
1.24k
    }
3003
1.32k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3004
1.32k
    return opline;
3005
614k
  } else if (is_globals_fetch(ast)) {
3006
2.46k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_GLOBALS, NULL, NULL);
3007
2.46k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3008
2.26k
      opline->result_type = IS_TMP_VAR;
3009
2.26k
      result->op_type = IS_TMP_VAR;
3010
2.26k
    }
3011
2.46k
    return opline;
3012
612k
  } else if (zend_try_compile_cv(result, ast, type) == FAILURE) {
3013
89.1k
    return zend_compile_simple_var_no_cv(result, ast, type, delayed);
3014
89.1k
  }
3015
523k
  return NULL;
3016
615k
}
3017
/* }}} */
3018
3019
static void zend_separate_if_call_and_write(znode *node, const zend_ast *ast, uint32_t type) /* {{{ */
3020
292k
{
3021
292k
  if (type != BP_VAR_R
3022
202k
   && type != BP_VAR_IS
3023
   /* Whether a FUNC_ARG is R may only be determined at runtime. */
3024
119k
   && type != BP_VAR_FUNC_ARG
3025
106k
   && zend_is_call(ast)) {
3026
2.69k
    if (node->op_type == IS_VAR) {
3027
2.68k
      zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
3028
2.68k
      opline->result_type = IS_VAR;
3029
2.68k
      opline->result.var = opline->op1.var;
3030
2.68k
    } else {
3031
10
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3032
10
    }
3033
2.69k
  }
3034
292k
}
3035
/* }}} */
3036
3037
static inline void zend_emit_assign_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3038
5.52k
{
3039
5.52k
  znode dummy_node;
3040
5.52k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
3041
5.52k
    zend_ast_create_znode(value_node));
3042
5.52k
  zend_compile_expr(&dummy_node, assign_ast);
3043
5.52k
  zend_do_free(&dummy_node);
3044
5.52k
}
3045
/* }}} */
3046
3047
static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
3048
222k
{
3049
222k
  zend_ast *var_ast = ast->child[0];
3050
222k
  zend_ast *dim_ast = ast->child[1];
3051
222k
  zend_op *opline;
3052
3053
222k
  znode var_node, dim_node;
3054
3055
222k
  if (is_globals_fetch(var_ast)) {
3056
1.89k
    if (dim_ast == NULL) {
3057
4
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot append to $GLOBALS");
3058
4
    }
3059
3060
1.88k
    zend_compile_expr(&dim_node, dim_ast);
3061
1.88k
    if (dim_node.op_type == IS_CONST) {
3062
1.61k
      convert_to_string(&dim_node.u.constant);
3063
1.61k
    }
3064
3065
1.88k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &dim_node, NULL);
3066
1.88k
    opline->extended_value = ZEND_FETCH_GLOBAL;
3067
1.88k
    zend_adjust_for_fetch_type(opline, result, type);
3068
1.88k
    return opline;
3069
220k
  } else {
3070
220k
    zend_short_circuiting_mark_inner(var_ast);
3071
220k
    opline = zend_delayed_compile_var(&var_node, var_ast, type, false);
3072
220k
    if (opline) {
3073
145k
      if (type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) {
3074
833
        opline->extended_value |= ZEND_FETCH_DIM_WRITE;
3075
144k
      } else if (opline->opcode == ZEND_FETCH_DIM_W
3076
80.1k
          || opline->opcode == ZEND_FETCH_DIM_RW
3077
79.1k
          || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3078
78.2k
          || opline->opcode == ZEND_FETCH_DIM_UNSET) {
3079
66.5k
        opline->extended_value = ZEND_FETCH_DIM_DIM;
3080
66.5k
      }
3081
145k
    }
3082
220k
  }
3083
3084
220k
  zend_separate_if_call_and_write(&var_node, var_ast, type);
3085
3086
220k
  if (dim_ast == NULL) {
3087
14.0k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3088
229
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
3089
229
    }
3090
13.8k
    if (type == BP_VAR_UNSET) {
3091
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
3092
2
    }
3093
13.8k
    dim_node.op_type = IS_UNUSED;
3094
206k
  } else {
3095
206k
    zend_compile_expr(&dim_node, dim_ast);
3096
206k
  }
3097
3098
220k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
3099
220k
  zend_adjust_for_fetch_type(opline, result, type);
3100
220k
  if (by_ref) {
3101
9.72k
    opline->extended_value = ZEND_FETCH_DIM_REF;
3102
9.72k
  }
3103
3104
220k
  if (dim_node.op_type == IS_CONST) {
3105
40.6k
    zend_handle_numeric_dim(opline, &dim_node);
3106
40.6k
  }
3107
220k
  return opline;
3108
220k
}
3109
3110
static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3111
72.3k
{
3112
72.3k
  uint32_t offset = zend_delayed_compile_begin();
3113
72.3k
  zend_delayed_compile_dim(result, ast, type, by_ref);
3114
72.3k
  return zend_delayed_compile_end(offset);
3115
72.3k
}
3116
/* }}} */
3117
3118
static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3119
75.1k
{
3120
75.1k
  zend_ast *obj_ast = ast->child[0];
3121
75.1k
  zend_ast *prop_ast = ast->child[1];
3122
3123
75.1k
  znode obj_node, prop_node;
3124
75.1k
  zend_op *opline;
3125
75.1k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_PROP;
3126
3127
75.1k
  if (is_this_fetch(obj_ast)) {
3128
3.49k
    if (this_guaranteed_exists()) {
3129
1.67k
      obj_node.op_type = IS_UNUSED;
3130
1.82k
    } else {
3131
1.82k
      zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
3132
1.82k
    }
3133
3.49k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3134
3135
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
3136
     * check for a nullsafe access. */
3137
71.6k
  } else {
3138
71.6k
    zend_short_circuiting_mark_inner(obj_ast);
3139
71.6k
    opline = zend_delayed_compile_var(&obj_node, obj_ast, type, false);
3140
71.6k
    if (opline && (opline->opcode == ZEND_FETCH_DIM_W
3141
19.5k
        || opline->opcode == ZEND_FETCH_DIM_RW
3142
19.3k
        || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3143
18.6k
        || opline->opcode == ZEND_FETCH_DIM_UNSET)) {
3144
4.41k
      opline->extended_value = ZEND_FETCH_DIM_OBJ;
3145
4.41k
    }
3146
3147
71.6k
    zend_separate_if_call_and_write(&obj_node, obj_ast, type);
3148
71.6k
    if (nullsafe) {
3149
40.6k
      if (obj_node.op_type == IS_TMP_VAR) {
3150
        /* Flush delayed oplines */
3151
7.92k
        zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
3152
7.92k
        uint32_t var = obj_node.u.op.var;
3153
7.92k
        uint32_t count = zend_stack_count(&CG(delayed_oplines_stack));
3154
7.92k
        uint32_t i = count;
3155
3156
31.4k
        while (i > 0 && oplines[i-1].result_type == IS_TMP_VAR && oplines[i-1].result.var == var) {
3157
24.9k
          i--;
3158
24.9k
          if (oplines[i].op1_type == IS_TMP_VAR) {
3159
23.5k
            var = oplines[i].op1.var;
3160
23.5k
          } else {
3161
1.38k
            break;
3162
1.38k
          }
3163
24.9k
        }
3164
32.8k
        for (; i < count; ++i) {
3165
24.9k
          if (oplines[i].opcode != ZEND_NOP) {
3166
7.59k
            opline = get_next_op();
3167
7.59k
            memcpy(opline, &oplines[i], sizeof(zend_op));
3168
7.59k
            oplines[i].opcode = ZEND_NOP;
3169
7.59k
            oplines[i].extended_value = opline - CG(active_op_array)->opcodes;
3170
7.59k
          }
3171
24.9k
        }
3172
7.92k
      }
3173
40.6k
      zend_emit_jmp_null(&obj_node, type);
3174
40.6k
    }
3175
71.6k
  }
3176
3177
75.1k
  zend_compile_expr(&prop_node, prop_ast);
3178
3179
75.1k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
3180
75.1k
  if (opline->op2_type == IS_CONST) {
3181
73.0k
    convert_to_string(CT_CONSTANT(opline->op2));
3182
73.0k
    zend_string_hash_val(Z_STR_P(CT_CONSTANT(opline->op2)));
3183
73.0k
    opline->extended_value = zend_alloc_cache_slots(3);
3184
73.0k
  }
3185
3186
75.1k
  zend_adjust_for_fetch_type(opline, result, type);
3187
3188
75.1k
  return opline;
3189
75.1k
}
3190
/* }}} */
3191
3192
static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3193
51.0k
{
3194
51.0k
  uint32_t offset = zend_delayed_compile_begin();
3195
51.0k
  zend_op *opline = zend_delayed_compile_prop(result, ast, type);
3196
51.0k
  if (by_ref) { /* shared with cache_slot */
3197
2.05k
    opline->extended_value |= ZEND_FETCH_REF;
3198
2.05k
  }
3199
51.0k
  return zend_delayed_compile_end(offset);
3200
51.0k
}
3201
/* }}} */
3202
3203
static zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref, bool delayed) /* {{{ */
3204
15.3k
{
3205
15.3k
  zend_ast *class_ast = ast->child[0];
3206
15.3k
  zend_ast *prop_ast = ast->child[1];
3207
3208
15.3k
  znode class_node, prop_node;
3209
15.3k
  zend_op *opline;
3210
3211
15.3k
  zend_short_circuiting_mark_inner(class_ast);
3212
15.3k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
3213
3214
15.3k
  zend_compile_expr(&prop_node, prop_ast);
3215
3216
15.3k
  if (delayed) {
3217
5.87k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3218
9.47k
  } else {
3219
9.47k
    opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3220
9.47k
  }
3221
15.3k
  if (opline->op1_type == IS_CONST) {
3222
13.8k
    convert_to_string(CT_CONSTANT(opline->op1));
3223
13.8k
    opline->extended_value = zend_alloc_cache_slots(3);
3224
13.8k
  }
3225
15.3k
  if (class_node.op_type == IS_CONST) {
3226
10.0k
    opline->op2_type = IS_CONST;
3227
10.0k
    opline->op2.constant = zend_add_class_name_literal(
3228
10.0k
      Z_STR(class_node.u.constant));
3229
10.0k
    if (opline->op1_type != IS_CONST) {
3230
1.02k
      opline->extended_value = zend_alloc_cache_slot();
3231
1.02k
    }
3232
10.0k
  } else {
3233
5.30k
    SET_NODE(opline->op2, &class_node);
3234
5.30k
  }
3235
3236
15.3k
  if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */
3237
1.48k
    opline->extended_value |= ZEND_FETCH_REF;
3238
1.48k
  }
3239
3240
15.3k
  zend_adjust_for_fetch_type(opline, result, type);
3241
15.3k
  return opline;
3242
15.3k
}
3243
/* }}} */
3244
3245
5.74k
static void zend_verify_list_assign_target(const zend_ast *var_ast, zend_ast_attr array_style) /* {{{ */ {
3246
5.74k
  if (var_ast->kind == ZEND_AST_ARRAY) {
3247
328
    if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) {
3248
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead");
3249
2
    }
3250
326
    if (array_style != var_ast->attr) {
3251
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()");
3252
2
    }
3253
5.42k
  } else if (!zend_can_write_to_variable(var_ast)) {
3254
69
    zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values");
3255
69
  }
3256
5.74k
}
3257
/* }}} */
3258
3259
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node);
3260
3261
/* Propagate refs used on leaf elements to the surrounding list() structures. */
3262
3.37k
static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
3263
3.37k
  const zend_ast_list *list = zend_ast_get_list(ast);
3264
3.37k
  bool has_refs = false;
3265
3.37k
  uint32_t i;
3266
3267
35.4k
  for (i = 0; i < list->children; ++i) {
3268
32.0k
    zend_ast *elem_ast = list->child[i];
3269
3270
32.0k
    if (elem_ast) {
3271
6.51k
      zend_ast *var_ast = elem_ast->child[0];
3272
6.51k
      if (var_ast->kind == ZEND_AST_ARRAY) {
3273
328
        elem_ast->attr = zend_propagate_list_refs(var_ast);
3274
328
      }
3275
6.51k
      has_refs |= elem_ast->attr;
3276
6.51k
    }
3277
32.0k
  }
3278
3279
3.37k
  return has_refs;
3280
3.37k
}
3281
/* }}} */
3282
3283
static bool list_is_keyed(const zend_ast_list *list)
3284
2.98k
{
3285
3.86k
  for (uint32_t i = 0; i < list->children; i++) {
3286
3.84k
    const zend_ast *child = list->child[i];
3287
3.84k
    if (child) {
3288
2.95k
      return child->kind == ZEND_AST_ARRAY_ELEM && child->child[1] != NULL;
3289
2.95k
    }
3290
3.84k
  }
3291
29
  return false;
3292
2.98k
}
3293
3294
static void zend_compile_list_assign(
3295
    znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style) /* {{{ */
3296
2.98k
{
3297
2.98k
  zend_ast_list *list = zend_ast_get_list(ast);
3298
2.98k
  uint32_t i;
3299
2.98k
  bool has_elems = false;
3300
2.98k
  bool is_keyed = list_is_keyed(list);
3301
3302
2.98k
  if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
3303
235
    zval_make_interned_string(&expr_node->u.constant);
3304
235
  }
3305
3306
9.95k
  for (i = 0; i < list->children; ++i) {
3307
6.97k
    zend_ast *elem_ast = list->child[i];
3308
6.97k
    zend_ast *var_ast, *key_ast;
3309
6.97k
    znode fetch_result, dim_node;
3310
6.97k
    zend_op *opline;
3311
3312
6.97k
    if (elem_ast == NULL) {
3313
1.21k
      if (is_keyed) {
3314
4
        zend_error(E_COMPILE_ERROR,
3315
4
          "Cannot use empty array entries in keyed array assignment");
3316
1.21k
      } else {
3317
1.21k
        continue;
3318
1.21k
      }
3319
1.21k
    }
3320
3321
5.75k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
3322
3
      zend_error(E_COMPILE_ERROR,
3323
3
          "Spread operator is not supported in assignments");
3324
3
    }
3325
3326
5.75k
    var_ast = elem_ast->child[0];
3327
5.75k
    key_ast = elem_ast->child[1];
3328
5.75k
    has_elems = true;
3329
3330
5.75k
    if (is_keyed) {
3331
500
      if (key_ast == NULL) {
3332
2
        zend_error(E_COMPILE_ERROR,
3333
2
          "Cannot mix keyed and unkeyed array entries in assignments");
3334
2
      }
3335
3336
500
      zend_compile_expr(&dim_node, key_ast);
3337
5.25k
    } else {
3338
5.25k
      if (key_ast != NULL) {
3339
2
        zend_error(E_COMPILE_ERROR,
3340
2
          "Cannot mix keyed and unkeyed array entries in assignments");
3341
2
      }
3342
3343
5.25k
      dim_node.op_type = IS_CONST;
3344
5.25k
      ZVAL_LONG(&dim_node.u.constant, i);
3345
5.25k
    }
3346
3347
5.75k
    if (expr_node->op_type == IS_CONST) {
3348
1.65k
      Z_TRY_ADDREF(expr_node->u.constant);
3349
1.65k
    }
3350
3351
5.75k
    zend_verify_list_assign_target(var_ast, array_style);
3352
3353
5.75k
    opline = zend_emit_op(&fetch_result,
3354
5.75k
      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);
3355
3356
5.75k
    if (dim_node.op_type == IS_CONST) {
3357
5.63k
      zend_handle_numeric_dim(opline, &dim_node);
3358
5.63k
    }
3359
3360
5.75k
    if (elem_ast->attr) {
3361
1.22k
      zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL);
3362
1.22k
    }
3363
5.75k
    if (var_ast->kind == ZEND_AST_ARRAY) {
3364
324
      zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr);
3365
5.43k
    } else if (elem_ast->attr) {
3366
1.22k
      zend_emit_assign_ref_znode(var_ast, &fetch_result);
3367
4.21k
    } else {
3368
4.21k
      zend_emit_assign_znode(var_ast, &fetch_result);
3369
4.21k
    }
3370
5.75k
  }
3371
3372
2.98k
  if (has_elems == 0) {
3373
29
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
3374
29
  }
3375
3376
2.95k
  if (result) {
3377
2.42k
    *result = *expr_node;
3378
2.42k
  } else {
3379
535
    zend_do_free(expr_node);
3380
535
  }
3381
2.95k
}
3382
/* }}} */
3383
3384
static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
3385
224k
{
3386
224k
  if (ast->kind == ZEND_AST_CALL || ast->kind == ZEND_AST_PIPE) {
3387
32
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
3388
32
  }
3389
224k
  if (
3390
224k
    ast->kind == ZEND_AST_METHOD_CALL
3391
224k
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
3392
224k
    || ast->kind == ZEND_AST_STATIC_CALL
3393
224k
  ) {
3394
8
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
3395
8
  }
3396
224k
  if (zend_ast_is_short_circuited(ast)) {
3397
8
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use nullsafe operator in write context");
3398
8
  }
3399
224k
  if (is_globals_fetch(ast)) {
3400
15
    zend_error_noreturn(E_COMPILE_ERROR,
3401
15
      "$GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax");
3402
15
  }
3403
224k
}
3404
/* }}} */
3405
3406
/* Detects $a... = $a pattern */
3407
static bool zend_is_assign_to_self(const zend_ast *var_ast, const zend_ast *expr_ast) /* {{{ */
3408
22.1k
{
3409
22.1k
  if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
3410
18.0k
    return 0;
3411
18.0k
  }
3412
3413
9.02k
  while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
3414
4.92k
    var_ast = var_ast->child[0];
3415
4.92k
  }
3416
3417
4.10k
  if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
3418
947
    return 0;
3419
947
  }
3420
3421
3.15k
  {
3422
3.15k
    zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
3423
3.15k
    zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
3424
3.15k
    bool result = zend_string_equals(name1, name2);
3425
3.15k
    zend_string_release_ex(name1, 0);
3426
3.15k
    zend_string_release_ex(name2, 0);
3427
3.15k
    return result;
3428
4.10k
  }
3429
4.10k
}
3430
/* }}} */
3431
3432
static void zend_compile_expr_with_potential_assign_to_self(
3433
22.1k
    znode *expr_node, zend_ast *expr_ast, const zend_ast *var_ast) {
3434
22.1k
  if (zend_is_assign_to_self(var_ast, expr_ast) && !is_this_fetch(expr_ast)) {
3435
    /* $a[0] = $a should evaluate the right $a first */
3436
725
    znode cv_node;
3437
3438
725
    if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3439
103
      zend_compile_simple_var_no_cv(expr_node, expr_ast, BP_VAR_R, false);
3440
622
    } else {
3441
622
      zend_emit_op_tmp(expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3442
622
    }
3443
21.4k
  } else {
3444
21.4k
    zend_compile_expr(expr_node, expr_ast);
3445
21.4k
  }
3446
22.1k
}
3447
3448
static void zend_compile_assign(znode *result, zend_ast *ast) /* {{{ */
3449
124k
{
3450
124k
  zend_ast *var_ast = ast->child[0];
3451
124k
  zend_ast *expr_ast = ast->child[1];
3452
3453
124k
  znode var_node, expr_node;
3454
124k
  zend_op *opline;
3455
124k
  uint32_t offset;
3456
124k
  if (is_this_fetch(var_ast)) {
3457
3
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3458
3
  }
3459
3460
124k
  zend_ensure_writable_variable(var_ast);
3461
3462
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3463
124k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3464
124k
  switch (kind) {
3465
108k
    case ZEND_AST_VAR:
3466
108k
      offset = zend_delayed_compile_begin();
3467
108k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, false);
3468
108k
      zend_compile_expr(&expr_node, expr_ast);
3469
108k
      zend_delayed_compile_end(offset);
3470
108k
      CG(zend_lineno) = zend_ast_get_lineno(var_ast);
3471
108k
      zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node);
3472
108k
      return;
3473
2.54k
    case ZEND_AST_STATIC_PROP:
3474
2.54k
      offset = zend_delayed_compile_begin();
3475
2.54k
      zend_delayed_compile_var(result, var_ast, BP_VAR_W, false);
3476
2.54k
      zend_compile_expr(&expr_node, expr_ast);
3477
3478
2.54k
      opline = zend_delayed_compile_end(offset);
3479
2.54k
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
3480
2.54k
      opline->result_type = IS_TMP_VAR;
3481
2.54k
      result->op_type = IS_TMP_VAR;
3482
3483
2.54k
      zend_emit_op_data(&expr_node);
3484
2.54k
      return;
3485
5.91k
    case ZEND_AST_DIM:
3486
5.91k
      offset = zend_delayed_compile_begin();
3487
5.91k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_W, /* by_ref */ false);
3488
5.91k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3489
3490
5.91k
      opline = zend_delayed_compile_end(offset);
3491
5.91k
      opline->opcode = ZEND_ASSIGN_DIM;
3492
5.91k
      opline->result_type = IS_TMP_VAR;
3493
5.91k
      result->op_type = IS_TMP_VAR;
3494
3495
5.91k
      opline = zend_emit_op_data(&expr_node);
3496
5.91k
      return;
3497
4.74k
    case ZEND_AST_PROP:
3498
4.74k
    case ZEND_AST_NULLSAFE_PROP:
3499
4.74k
      offset = zend_delayed_compile_begin();
3500
4.74k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
3501
4.74k
      zend_compile_expr(&expr_node, expr_ast);
3502
3503
4.74k
      opline = zend_delayed_compile_end(offset);
3504
4.74k
      opline->opcode = ZEND_ASSIGN_OBJ;
3505
4.74k
      opline->result_type = IS_TMP_VAR;
3506
4.74k
      result->op_type = IS_TMP_VAR;
3507
3508
4.74k
      zend_emit_op_data(&expr_node);
3509
4.74k
      return;
3510
2.92k
    case ZEND_AST_ARRAY:
3511
2.92k
      if (zend_propagate_list_refs(var_ast)) {
3512
1.13k
        if (!zend_is_variable_or_call(expr_ast)) {
3513
8
          zend_error_noreturn(E_COMPILE_ERROR,
3514
8
            "Cannot assign reference to non referenceable value");
3515
1.12k
        } else {
3516
1.12k
          zend_assert_not_short_circuited(expr_ast);
3517
1.12k
        }
3518
3519
1.12k
        zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
3520
        /* MAKE_REF is usually not necessary for CVs. However, if there are
3521
         * self-assignments, this forces the RHS to evaluate first. */
3522
1.12k
        zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
3523
1.78k
      } else {
3524
1.78k
        if (expr_ast->kind == ZEND_AST_VAR) {
3525
          /* list($a, $b) = $a should evaluate the right $a first */
3526
336
          znode cv_node;
3527
3528
336
          if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3529
248
            zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, false);
3530
248
          } else {
3531
88
            zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3532
88
          }
3533
1.45k
        } else {
3534
1.45k
          zend_compile_expr(&expr_node, expr_ast);
3535
1.45k
        }
3536
1.78k
      }
3537
3538
2.91k
      zend_compile_list_assign(result, var_ast, &expr_node, var_ast->attr);
3539
2.91k
      return;
3540
0
    EMPTY_SWITCH_DEFAULT_CASE();
3541
124k
  }
3542
124k
}
3543
/* }}} */
3544
3545
static void zend_compile_assign_ref(znode *result, zend_ast *ast) /* {{{ */
3546
4.28k
{
3547
4.28k
  zend_ast *target_ast = ast->child[0];
3548
4.28k
  zend_ast *source_ast = ast->child[1];
3549
3550
4.28k
  znode target_node, source_node;
3551
4.28k
  zend_op *opline;
3552
4.28k
  uint32_t offset, flags;
3553
3554
4.28k
  if (is_this_fetch(target_ast)) {
3555
2
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3556
2
  }
3557
4.28k
  zend_ensure_writable_variable(target_ast);
3558
4.28k
  zend_assert_not_short_circuited(source_ast);
3559
4.28k
  if (is_globals_fetch(source_ast)) {
3560
2
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot acquire reference to $GLOBALS");
3561
2
  }
3562
3563
4.28k
  offset = zend_delayed_compile_begin();
3564
4.28k
  zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, true);
3565
4.28k
  zend_compile_var(&source_node, source_ast, BP_VAR_W, true);
3566
3567
4.28k
  if ((target_ast->kind != ZEND_AST_VAR
3568
3.49k
    || target_ast->child[0]->kind != ZEND_AST_ZVAL)
3569
1.68k
   && source_ast->kind != ZEND_AST_ZNODE
3570
708
   && source_node.op_type != IS_CV) {
3571
    /* Both LHS and RHS expressions may modify the same data structure,
3572
     * and the modification during RHS evaluation may dangle the pointer
3573
     * to the result of the LHS evaluation.
3574
     * Use MAKE_REF instruction to replace direct pointer with REFERENCE.
3575
     * See: Bug #71539
3576
     */
3577
358
    zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL);
3578
358
  }
3579
3580
4.28k
  opline = zend_delayed_compile_end(offset);
3581
3582
4.28k
  if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
3583
2
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3584
2
  }
3585
3586
4.28k
  flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0;
3587
3588
4.28k
  if (opline && opline->opcode == ZEND_FETCH_OBJ_W) {
3589
346
    opline->opcode = ZEND_ASSIGN_OBJ_REF;
3590
346
    opline->extended_value &= ~ZEND_FETCH_REF;
3591
346
    opline->extended_value |= flags;
3592
346
    zend_emit_op_data(&source_node);
3593
346
    *result = target_node;
3594
3.93k
  } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) {
3595
208
    opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF;
3596
208
    opline->extended_value &= ~ZEND_FETCH_REF;
3597
208
    opline->extended_value |= flags;
3598
208
    zend_emit_op_data(&source_node);
3599
208
    *result = target_node;
3600
3.72k
  } else {
3601
3.72k
    opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
3602
3.72k
    opline->extended_value = flags;
3603
3.72k
  }
3604
4.28k
}
3605
/* }}} */
3606
3607
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3608
1.97k
{
3609
1.97k
  znode dummy_node;
3610
1.97k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
3611
1.97k
    zend_ast_create_znode(value_node));
3612
1.97k
  zend_compile_expr(&dummy_node, assign_ast);
3613
1.97k
  zend_do_free(&dummy_node);
3614
1.97k
}
3615
/* }}} */
3616
3617
static void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
3618
67.8k
{
3619
67.8k
  zend_ast *var_ast = ast->child[0];
3620
67.8k
  zend_ast *expr_ast = ast->child[1];
3621
67.8k
  uint32_t opcode = ast->attr;
3622
3623
67.8k
  znode var_node, expr_node;
3624
67.8k
  zend_op *opline;
3625
67.8k
  uint32_t offset, cache_slot;
3626
3627
67.8k
  zend_ensure_writable_variable(var_ast);
3628
3629
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3630
67.8k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3631
67.8k
  switch (kind) {
3632
62.7k
    case ZEND_AST_VAR:
3633
62.7k
      offset = zend_delayed_compile_begin();
3634
62.7k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, false);
3635
62.7k
      zend_compile_expr(&expr_node, expr_ast);
3636
62.7k
      zend_delayed_compile_end(offset);
3637
62.7k
      opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node);
3638
62.7k
      opline->extended_value = opcode;
3639
62.7k
      return;
3640
759
    case ZEND_AST_STATIC_PROP:
3641
759
      offset = zend_delayed_compile_begin();
3642
759
      zend_delayed_compile_var(result, var_ast, BP_VAR_RW, false);
3643
759
      zend_compile_expr(&expr_node, expr_ast);
3644
3645
759
      opline = zend_delayed_compile_end(offset);
3646
759
      cache_slot = opline->extended_value;
3647
759
      opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP;
3648
759
      opline->extended_value = opcode;
3649
759
      opline->result_type = IS_TMP_VAR;
3650
759
      result->op_type = IS_TMP_VAR;
3651
3652
759
      opline = zend_emit_op_data(&expr_node);
3653
759
      opline->extended_value = cache_slot;
3654
759
      return;
3655
3.07k
    case ZEND_AST_DIM:
3656
3.07k
      offset = zend_delayed_compile_begin();
3657
3.07k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_RW, /* by_ref */ false);
3658
3.07k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3659
3660
3.07k
      opline = zend_delayed_compile_end(offset);
3661
3.07k
      opline->opcode = ZEND_ASSIGN_DIM_OP;
3662
3.07k
      opline->extended_value = opcode;
3663
3.07k
      opline->result_type = IS_TMP_VAR;
3664
3.07k
      result->op_type = IS_TMP_VAR;
3665
3666
3.07k
      zend_emit_op_data(&expr_node);
3667
3.07k
      return;
3668
1.25k
    case ZEND_AST_PROP:
3669
1.25k
    case ZEND_AST_NULLSAFE_PROP:
3670
1.25k
      offset = zend_delayed_compile_begin();
3671
1.25k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
3672
1.25k
      zend_compile_expr(&expr_node, expr_ast);
3673
3674
1.25k
      opline = zend_delayed_compile_end(offset);
3675
1.25k
      cache_slot = opline->extended_value;
3676
1.25k
      opline->opcode = ZEND_ASSIGN_OBJ_OP;
3677
1.25k
      opline->extended_value = opcode;
3678
1.25k
      opline->result_type = IS_TMP_VAR;
3679
1.25k
      result->op_type = IS_TMP_VAR;
3680
3681
1.25k
      opline = zend_emit_op_data(&expr_node);
3682
1.25k
      opline->extended_value = cache_slot;
3683
1.25k
      return;
3684
67.8k
    EMPTY_SWITCH_DEFAULT_CASE()
3685
67.8k
  }
3686
67.8k
}
3687
/* }}} */
3688
3689
3.93k
static uint32_t zend_get_arg_num(const zend_function *fn, const zend_string *arg_name) {
3690
  // TODO: Caching?
3691
13.4k
  for (uint32_t i = 0; i < fn->common.num_args; i++) {
3692
10.8k
    zend_arg_info *arg_info = &fn->op_array.arg_info[i];
3693
10.8k
    if (zend_string_equals(arg_info->name, arg_name)) {
3694
1.36k
      return i + 1;
3695
1.36k
    }
3696
10.8k
  }
3697
3698
  /* Either an invalid argument name, or collected into a variadic argument. */
3699
2.56k
  return (uint32_t) -1;
3700
3.93k
}
3701
3702
static uint32_t zend_compile_args(
3703
    zend_ast *ast, const zend_function *fbc, bool *may_have_extra_named_args) /* {{{ */
3704
3.84M
{
3705
3.84M
  const zend_ast_list *args = zend_ast_get_list(ast);
3706
3.84M
  uint32_t i;
3707
3.84M
  bool uses_arg_unpack = false;
3708
3.84M
  uint32_t arg_count = 0; /* number of arguments not including unpacks */
3709
3710
  /* Whether named arguments are used syntactically, to enforce language level limitations.
3711
   * May not actually use named argument passing. */
3712
3.84M
  bool uses_named_args = false;
3713
  /* Whether there may be any undef arguments due to the use of named arguments. */
3714
3.84M
  bool may_have_undef = false;
3715
  /* Whether there may be any extra named arguments collected into a variadic. */
3716
3.84M
  *may_have_extra_named_args = false;
3717
3718
9.05M
  for (i = 0; i < args->children; ++i) {
3719
5.21M
    zend_ast *arg = args->child[i];
3720
5.21M
    zend_string *arg_name = NULL;
3721
5.21M
    uint32_t arg_num = i + 1;
3722
3723
5.21M
    znode arg_node;
3724
5.21M
    zend_op *opline;
3725
5.21M
    uint8_t opcode;
3726
3727
5.21M
    if (arg->kind == ZEND_AST_UNPACK) {
3728
1.14k
      if (uses_named_args) {
3729
2
        zend_error_noreturn(E_COMPILE_ERROR,
3730
2
          "Cannot use argument unpacking after named arguments");
3731
2
      }
3732
3733
      /* Unpack may contain named arguments. */
3734
1.14k
      may_have_undef = true;
3735
1.14k
      if (!fbc || (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3736
973
        *may_have_extra_named_args = true;
3737
973
      }
3738
3739
1.14k
      uses_arg_unpack = true;
3740
1.14k
      fbc = NULL;
3741
3742
1.14k
      zend_compile_expr(&arg_node, arg->child[0]);
3743
1.14k
      opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
3744
1.14k
      opline->op2.num = arg_count;
3745
1.14k
      opline->result.var = EX_NUM_TO_VAR(arg_count - 1);
3746
3747
1.14k
      continue;
3748
1.14k
    }
3749
3750
5.20M
    if (arg->kind == ZEND_AST_NAMED_ARG) {
3751
45.3k
      uses_named_args = true;
3752
45.3k
      arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0]));
3753
45.3k
      arg = arg->child[1];
3754
3755
45.3k
      if (fbc && !uses_arg_unpack) {
3756
3.93k
        arg_num = zend_get_arg_num(fbc, arg_name);
3757
3.93k
        if (arg_num == arg_count + 1 && !may_have_undef) {
3758
          /* Using named arguments, but passing in order. */
3759
346
          arg_name = NULL;
3760
346
          arg_count++;
3761
3.58k
        } else {
3762
          // TODO: We could track which arguments were passed, even if out of order.
3763
3.58k
          may_have_undef = true;
3764
3.58k
          if (arg_num == (uint32_t) -1 && (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3765
113
            *may_have_extra_named_args = true;
3766
113
          }
3767
3.58k
        }
3768
41.4k
      } else {
3769
41.4k
        arg_num = (uint32_t) -1;
3770
41.4k
        may_have_undef = true;
3771
41.4k
        *may_have_extra_named_args = true;
3772
41.4k
      }
3773
5.16M
    } else {
3774
5.16M
      if (uses_arg_unpack) {
3775
9
        zend_error_noreturn(E_COMPILE_ERROR,
3776
9
          "Cannot use positional argument after argument unpacking");
3777
9
      }
3778
3779
5.16M
      if (uses_named_args) {
3780
32
        zend_error_noreturn(E_COMPILE_ERROR,
3781
32
          "Cannot use positional argument after named argument");
3782
32
      }
3783
3784
5.16M
      arg_count++;
3785
5.16M
    }
3786
3787
    /* Treat passing of $GLOBALS the same as passing a call.
3788
     * This will error at runtime if the argument is by-ref. */
3789
5.20M
    if (zend_is_call(arg) || is_globals_fetch(arg)) {
3790
141k
      zend_compile_var(&arg_node, arg, BP_VAR_R, false);
3791
141k
      if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
3792
        /* Function call was converted into builtin instruction */
3793
3.65k
        if (!fbc || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3794
2.34k
          opcode = ZEND_SEND_VAL_EX;
3795
2.34k
        } else {
3796
1.31k
          opcode = ZEND_SEND_VAL;
3797
1.31k
        }
3798
138k
      } else {
3799
138k
        if (fbc && arg_num != (uint32_t) -1) {
3800
22.6k
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3801
578
            opcode = ZEND_SEND_VAR_NO_REF;
3802
22.0k
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3803
            /* For IS_VAR operands, SEND_VAL will pass through the operand without
3804
             * dereferencing, so it will use a by-ref pass if the call returned by-ref
3805
             * and a by-value pass if it returned by-value. */
3806
18
            opcode = ZEND_SEND_VAL;
3807
22.0k
          } else {
3808
22.0k
            opcode = ZEND_SEND_VAR;
3809
22.0k
          }
3810
115k
        } else {
3811
115k
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3812
115k
        }
3813
138k
      }
3814
5.06M
    } else if (zend_is_variable(arg) && !zend_ast_is_short_circuited(arg)) {
3815
51.6k
      if (fbc && arg_num != (uint32_t) -1) {
3816
30.6k
        if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3817
801
          zend_compile_var(&arg_node, arg, BP_VAR_W, true);
3818
801
          opcode = ZEND_SEND_REF;
3819
29.8k
        } else {
3820
29.8k
          zend_compile_var(&arg_node, arg, BP_VAR_R, false);
3821
29.8k
          opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR;
3822
29.8k
        }
3823
30.6k
      } else {
3824
21.0k
        do {
3825
21.0k
          if (arg->kind == ZEND_AST_VAR) {
3826
8.77k
            CG(zend_lineno) = zend_ast_get_lineno(ast);
3827
8.77k
            if (is_this_fetch(arg)) {
3828
1.09k
              zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL);
3829
1.09k
              opcode = ZEND_SEND_VAR_EX;
3830
1.09k
              CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3831
1.09k
              break;
3832
7.68k
            } else if (zend_try_compile_cv(&arg_node, arg, BP_VAR_R) == SUCCESS) {
3833
7.12k
              opcode = ZEND_SEND_VAR_EX;
3834
7.12k
              break;
3835
7.12k
            }
3836
8.77k
          }
3837
12.7k
          opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL);
3838
12.7k
          if (arg_name) {
3839
8.24k
            opline->op2_type = IS_CONST;
3840
8.24k
            zend_string_addref(arg_name);
3841
8.24k
            opline->op2.constant = zend_add_literal_string(&arg_name);
3842
8.24k
            opline->result.num = zend_alloc_cache_slots(2);
3843
8.24k
          } else {
3844
4.55k
            opline->op2.num = arg_num;
3845
4.55k
          }
3846
12.7k
          zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, true);
3847
12.7k
          opcode = ZEND_SEND_FUNC_ARG;
3848
12.7k
        } while (0);
3849
21.0k
      }
3850
5.01M
    } else {
3851
5.01M
      zend_compile_expr(&arg_node, arg);
3852
5.01M
      if (arg_node.op_type == IS_VAR) {
3853
        /* pass ++$a or something similar */
3854
634k
        if (fbc && arg_num != (uint32_t) -1) {
3855
1.74k
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3856
18
            opcode = ZEND_SEND_VAR_NO_REF;
3857
1.72k
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3858
18
            opcode = ZEND_SEND_VAL;
3859
1.70k
          } else {
3860
1.70k
            opcode = ZEND_SEND_VAR;
3861
1.70k
          }
3862
633k
        } else {
3863
633k
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3864
633k
        }
3865
4.38M
      } else if (arg_node.op_type == IS_CV) {
3866
0
        if (fbc && arg_num != (uint32_t) -1) {
3867
0
          if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3868
0
            opcode = ZEND_SEND_REF;
3869
0
          } else {
3870
0
            opcode = ZEND_SEND_VAR;
3871
0
          }
3872
0
        } else {
3873
0
          opcode = ZEND_SEND_VAR_EX;
3874
0
        }
3875
4.38M
      } else {
3876
        /* Delay "Only variables can be passed by reference" error to execution */
3877
4.38M
        if (fbc && arg_num != (uint32_t) -1 && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3878
61.8k
          opcode = ZEND_SEND_VAL;
3879
4.31M
        } else {
3880
4.31M
          opcode = ZEND_SEND_VAL_EX;
3881
4.31M
        }
3882
4.38M
      }
3883
5.01M
    }
3884
3885
5.20M
    opline = zend_emit_op(NULL, opcode, &arg_node, NULL);
3886
5.20M
    if (arg_name) {
3887
44.9k
      opline->op2_type = IS_CONST;
3888
44.9k
      zend_string_addref(arg_name);
3889
44.9k
      opline->op2.constant = zend_add_literal_string(&arg_name);
3890
44.9k
      opline->result.num = zend_alloc_cache_slots(2);
3891
5.16M
    } else {
3892
5.16M
      opline->op2.opline_num = arg_num;
3893
5.16M
      opline->result.var = EX_NUM_TO_VAR(arg_num - 1);
3894
5.16M
    }
3895
5.20M
  }
3896
3897
3.84M
  if (may_have_undef) {
3898
37.5k
    zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
3899
37.5k
  }
3900
3901
3.84M
  return arg_count;
3902
3.84M
}
3903
/* }}} */
3904
3905
ZEND_API uint8_t zend_get_call_op(const zend_op *init_op, const zend_function *fbc, bool result_used) /* {{{ */
3906
3.86M
{
3907
3.86M
  uint32_t no_discard = result_used ? 0 : ZEND_ACC_NODISCARD;
3908
3909
3.86M
  if (fbc && init_op->opcode != ZEND_NEW) {
3910
72.6k
    ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE));
3911
72.6k
    if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) {
3912
63.5k
      if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) {
3913
25.2k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3914
25.2k
          return ZEND_DO_ICALL;
3915
25.2k
        } else {
3916
22
          return ZEND_DO_FCALL_BY_NAME;
3917
22
        }
3918
25.2k
      }
3919
63.5k
    } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){
3920
9.08k
      if (zend_execute_ex == execute_ex) {
3921
4.82k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3922
4.80k
          return ZEND_DO_UCALL;
3923
4.80k
        } else {
3924
24
          return ZEND_DO_FCALL_BY_NAME;
3925
24
        }
3926
4.82k
      }
3927
9.08k
    }
3928
3.78M
  } else if (zend_execute_ex == execute_ex &&
3929
3.76M
             !zend_execute_internal &&
3930
3.75M
             (init_op->opcode == ZEND_INIT_FCALL_BY_NAME ||
3931
3.68M
              init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) {
3932
3.41M
    return ZEND_DO_FCALL_BY_NAME;
3933
3.41M
  }
3934
417k
  return ZEND_DO_FCALL;
3935
3.86M
}
3936
/* }}} */
3937
3938
static bool zend_compile_call_common(znode *result, zend_ast *args_ast, const zend_function *fbc, uint32_t lineno) /* {{{ */
3939
3.84M
{
3940
3.84M
  zend_op *opline;
3941
3.84M
  uint32_t opnum_init = get_next_op_number() - 1;
3942
3943
3.84M
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
3944
2.38k
    opline = &CG(active_op_array)->opcodes[opnum_init];
3945
2.38k
    opline->extended_value = 0;
3946
    /* opcode array may be reallocated, so don't access opcode field after zend_emit_op_tmp(). */
3947
2.38k
    uint8_t opcode = opline->opcode;
3948
3949
2.38k
    if (opcode == ZEND_NEW) {
3950
10
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
3951
10
    }
3952
3953
2.37k
    if (opcode == ZEND_INIT_FCALL) {
3954
520
      opline->op1.num = zend_vm_calc_used_stack(0, fbc);
3955
520
    }
3956
3957
2.37k
    zend_op *callable_convert_op = zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT, NULL, NULL);
3958
2.37k
    if (opcode == ZEND_INIT_FCALL
3959
1.85k
     || opcode == ZEND_INIT_FCALL_BY_NAME
3960
2.13k
     || opcode == ZEND_INIT_NS_FCALL_BY_NAME) {
3961
2.13k
      callable_convert_op->extended_value = zend_alloc_cache_slot();
3962
2.13k
    } else {
3963
232
      callable_convert_op->extended_value = (uint32_t)-1;
3964
232
    }
3965
2.37k
    return true;
3966
2.38k
  }
3967
3968
3.84M
  bool may_have_extra_named_args;
3969
3.84M
  uint32_t arg_count = zend_compile_args(args_ast, fbc, &may_have_extra_named_args);
3970
3971
3.84M
  zend_do_extended_fcall_begin();
3972
3973
3.84M
  opline = &CG(active_op_array)->opcodes[opnum_init];
3974
3.84M
  opline->extended_value = arg_count;
3975
3976
3.84M
  if (opline->opcode == ZEND_INIT_FCALL) {
3977
50.7k
    opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
3978
50.7k
  }
3979
3980
3.84M
  uint8_t call_op = zend_get_call_op(
3981
3.84M
    opline,
3982
3.84M
    fbc,
3983
    /* result_used: At this point we do not yet reliably
3984
     * know if the result is used. Deoptimize #[\NoDiscard]
3985
     * calls to be sure. The optimizer will fix this up.
3986
     */
3987
3.84M
    false
3988
3.84M
  );
3989
3.84M
  opline = zend_emit_op(result, call_op, NULL, NULL);
3990
3.84M
  if (may_have_extra_named_args) {
3991
35.9k
    opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
3992
35.9k
  }
3993
3.84M
  opline->lineno = lineno;
3994
3.84M
  zend_do_extended_fcall_end();
3995
3.84M
  return false;
3996
3.84M
}
3997
/* }}} */
3998
3999
static bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
4000
3.51M
{
4001
3.51M
  zend_string *orig_name = zend_ast_get_str(name_ast);
4002
3.51M
  bool is_fully_qualified;
4003
4004
3.51M
  name_node->op_type = IS_CONST;
4005
3.51M
  ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
4006
3.51M
    orig_name, name_ast->attr, &is_fully_qualified));
4007
4008
3.51M
  return !is_fully_qualified && FC(current_namespace);
4009
3.51M
}
4010
/* }}} */
4011
4012
static void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast, uint32_t lineno) /* {{{ */
4013
286k
{
4014
286k
  if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
4015
65.2k
    const char *colon;
4016
65.2k
    zend_string *str = Z_STR(name_node->u.constant);
4017
65.2k
    if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
4018
495
      zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
4019
495
      zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
4020
495
      zend_op *opline = get_next_op();
4021
4022
495
      opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
4023
495
      opline->op1_type = IS_CONST;
4024
495
      opline->op1.constant = zend_add_class_name_literal(class);
4025
495
      opline->op2_type = IS_CONST;
4026
495
      opline->op2.constant = zend_add_func_name_literal(method);
4027
      /* 2 slots, for class and method */
4028
495
      opline->result.num = zend_alloc_cache_slots(2);
4029
495
      zval_ptr_dtor(&name_node->u.constant);
4030
64.7k
    } else {
4031
64.7k
      zend_op *opline = get_next_op();
4032
4033
64.7k
      opline->opcode = ZEND_INIT_FCALL_BY_NAME;
4034
64.7k
      opline->op2_type = IS_CONST;
4035
64.7k
      opline->op2.constant = zend_add_func_name_literal(str);
4036
64.7k
      opline->result.num = zend_alloc_cache_slot();
4037
64.7k
    }
4038
221k
  } else {
4039
221k
    zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node);
4040
221k
  }
4041
4042
286k
  zend_compile_call_common(result, args_ast, NULL, lineno);
4043
286k
}
4044
/* }}} */
4045
4046
static inline bool zend_args_contain_unpack_or_named(const zend_ast_list *args) /* {{{ */
4047
3.42M
{
4048
3.42M
  uint32_t i;
4049
8.24M
  for (i = 0; i < args->children; ++i) {
4050
4.86M
    const zend_ast *arg = args->child[i];
4051
4.86M
    if (arg->kind == ZEND_AST_UNPACK || arg->kind == ZEND_AST_NAMED_ARG) {
4052
33.7k
      return 1;
4053
33.7k
    }
4054
4.86M
  }
4055
3.38M
  return 0;
4056
3.42M
}
4057
/* }}} */
4058
4059
static zend_result zend_compile_func_strlen(znode *result, const zend_ast_list *args) /* {{{ */
4060
813
{
4061
813
  znode arg_node;
4062
4063
813
  if (args->children != 1) {
4064
79
    return FAILURE;
4065
79
  }
4066
4067
734
  zend_compile_expr(&arg_node, args->child[0]);
4068
734
  if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
4069
149
    result->op_type = IS_CONST;
4070
149
    ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
4071
149
    zval_ptr_dtor_str(&arg_node.u.constant);
4072
585
  } else {
4073
585
    zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
4074
585
  }
4075
734
  return SUCCESS;
4076
813
}
4077
/* }}} */
4078
4079
static zend_result zend_compile_func_typecheck(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4080
1.13k
{
4081
1.13k
  znode arg_node;
4082
1.13k
  zend_op *opline;
4083
4084
1.13k
  if (args->children != 1) {
4085
549
    return FAILURE;
4086
549
  }
4087
4088
590
  zend_compile_expr(&arg_node, args->child[0]);
4089
590
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4090
590
  if (type != _IS_BOOL) {
4091
573
    opline->extended_value = (1 << type);
4092
573
  } else {
4093
17
    opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE);
4094
17
  }
4095
590
  return SUCCESS;
4096
1.13k
}
4097
/* }}} */
4098
4099
static zend_result zend_compile_func_is_scalar(znode *result, const zend_ast_list *args) /* {{{ */
4100
78
{
4101
78
  znode arg_node;
4102
78
  zend_op *opline;
4103
4104
78
  if (args->children != 1) {
4105
10
    return FAILURE;
4106
10
  }
4107
4108
68
  zend_compile_expr(&arg_node, args->child[0]);
4109
68
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4110
68
  opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING);
4111
68
  return SUCCESS;
4112
78
}
4113
4114
static zend_result zend_compile_func_cast(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4115
536
{
4116
536
  znode arg_node;
4117
536
  zend_op *opline;
4118
4119
536
  if (args->children != 1) {
4120
313
    return FAILURE;
4121
313
  }
4122
4123
223
  zend_compile_expr(&arg_node, args->child[0]);
4124
223
  if (type == _IS_BOOL) {
4125
41
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL);
4126
182
  } else {
4127
182
    opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
4128
182
    opline->extended_value = type;
4129
182
  }
4130
223
  return SUCCESS;
4131
536
}
4132
/* }}} */
4133
4134
static zend_result zend_compile_func_defined(znode *result, const zend_ast_list *args) /* {{{ */
4135
1.86k
{
4136
1.86k
  zend_string *name;
4137
1.86k
  zend_op *opline;
4138
4139
1.86k
  if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
4140
276
    return FAILURE;
4141
276
  }
4142
4143
1.59k
  name = zval_get_string(zend_ast_get_zval(args->child[0]));
4144
1.59k
  if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
4145
142
    zend_string_release_ex(name, 0);
4146
142
    return FAILURE;
4147
142
  }
4148
4149
1.45k
  if (zend_try_ct_eval_const(&result->u.constant, name, false)) {
4150
67
    zend_string_release_ex(name, 0);
4151
67
    zval_ptr_dtor(&result->u.constant);
4152
67
    ZVAL_TRUE(&result->u.constant);
4153
67
    result->op_type = IS_CONST;
4154
67
    return SUCCESS;
4155
67
  }
4156
4157
1.38k
  opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
4158
1.38k
  opline->op1_type = IS_CONST;
4159
1.38k
  LITERAL_STR(opline->op1, name);
4160
1.38k
  opline->extended_value = zend_alloc_cache_slot();
4161
4162
1.38k
  return SUCCESS;
4163
1.45k
}
4164
/* }}} */
4165
4166
static zend_result zend_compile_func_chr(znode *result, const zend_ast_list *args) /* {{{ */
4167
1.29k
{
4168
1.29k
  zval *zint;
4169
1.29k
  if (
4170
1.29k
    args->children == 1
4171
1.06k
    && args->child[0]->kind == ZEND_AST_ZVAL
4172
897
    && (zint = zend_ast_get_zval(args->child[0]))
4173
897
    && Z_TYPE_P(zint) == IS_LONG
4174
827
    && Z_LVAL_P(zint) >= 0
4175
827
    && Z_LVAL_P(zint) <= 255
4176
1.29k
  ) {
4177
530
    result->op_type = IS_CONST;
4178
530
    ZVAL_CHAR(&result->u.constant, Z_LVAL_P(zint));
4179
530
    return SUCCESS;
4180
760
  } else {
4181
760
    return FAILURE;
4182
760
  }
4183
1.29k
}
4184
/* }}} */
4185
4186
static zend_result zend_compile_func_ord(znode *result, const zend_ast_list *args) /* {{{ */
4187
528
{
4188
528
  zval *str;
4189
528
  if (
4190
528
    args->children == 1
4191
319
    && args->child[0]->kind == ZEND_AST_ZVAL
4192
276
    && (str = zend_ast_get_zval(args->child[0]))
4193
276
    && Z_TYPE_P(str) == IS_STRING
4194
206
    && Z_STRLEN_P(str) == 1
4195
528
  ) {
4196
10
    result->op_type = IS_CONST;
4197
10
    ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(str)[0]);
4198
10
    return SUCCESS;
4199
518
  } else {
4200
518
    return FAILURE;
4201
518
  }
4202
528
}
4203
/* }}} */
4204
4205
/* We can only calculate the stack size for functions that have been fully compiled, otherwise
4206
 * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for
4207
 * directly or indirectly recursive function calls. */
4208
93.4k
static bool fbc_is_finalized(const zend_function *fbc) {
4209
93.4k
  return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO);
4210
93.4k
}
4211
4212
static bool zend_compile_ignore_class(const zend_class_entry *ce, const zend_string *filename)
4213
27.1k
{
4214
27.1k
  if (ce->type == ZEND_INTERNAL_CLASS) {
4215
19.8k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES;
4216
19.8k
  } else {
4217
7.23k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4218
1.71k
      && ce->info.user.filename != filename;
4219
7.23k
  }
4220
27.1k
}
4221
4222
static bool zend_compile_ignore_function(const zend_function *fbc, const zend_string *filename)
4223
74.3k
{
4224
74.3k
  if (fbc->type == ZEND_INTERNAL_FUNCTION) {
4225
68.7k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS;
4226
68.7k
  } else {
4227
5.65k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)
4228
5.65k
      || ((CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4229
3.31k
        && fbc->op_array.filename != filename);
4230
5.65k
  }
4231
74.3k
}
4232
4233
static zend_result zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
4234
20.0k
{
4235
20.0k
  zend_string *name, *lcname;
4236
20.0k
  zend_function *fbc;
4237
20.0k
  zend_op *opline;
4238
4239
20.0k
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
4240
2.11k
    return FAILURE;
4241
2.11k
  }
4242
4243
17.9k
  name = zend_ast_get_str(name_ast);
4244
17.9k
  lcname = zend_string_tolower(name);
4245
4246
17.9k
  fbc = zend_hash_find_ptr(CG(function_table), lcname);
4247
17.9k
  if (!fbc
4248
531
   || !fbc_is_finalized(fbc)
4249
17.4k
   || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
4250
17.4k
    zend_string_release_ex(lcname, 0);
4251
17.4k
    return FAILURE;
4252
17.4k
  }
4253
4254
531
  opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
4255
531
  opline->extended_value = num_args;
4256
531
  opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
4257
531
  opline->op2_type = IS_CONST;
4258
531
  LITERAL_STR(opline->op2, lcname);
4259
531
  opline->result.num = zend_alloc_cache_slot();
4260
4261
531
  return SUCCESS;
4262
17.9k
}
4263
/* }}} */
4264
4265
static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
4266
20.0k
{
4267
20.0k
  zend_op *opline;
4268
20.0k
  znode name_node;
4269
4270
20.0k
  if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
4271
531
    return;
4272
531
  }
4273
4274
19.5k
  zend_compile_expr(&name_node, name_ast);
4275
4276
19.5k
  opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
4277
19.5k
  opline->op1_type = IS_CONST;
4278
19.5k
  LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
4279
19.5k
  opline->extended_value = num_args;
4280
19.5k
}
4281
/* }}} */
4282
4283
/* cufa = call_user_func_array */
4284
static zend_result zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
4285
1.98k
{
4286
1.98k
  znode arg_node;
4287
1.98k
  zend_op *opline;
4288
4289
1.98k
  if (args->children != 2) {
4290
87
    return FAILURE;
4291
87
  }
4292
4293
1.89k
  zend_compile_init_user_func(args->child[0], 0, lcname);
4294
1.89k
  if (args->child[1]->kind == ZEND_AST_CALL
4295
1.61k
   && args->child[1]->child[0]->kind == ZEND_AST_ZVAL
4296
1.54k
   && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING
4297
1.46k
   && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) {
4298
1.45k
    zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]);
4299
1.45k
    zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]);
4300
1.45k
    bool is_fully_qualified;
4301
1.45k
    zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified);
4302
4303
1.45k
    if (zend_string_equals_literal_ci(name, "array_slice")
4304
931
       && !zend_args_contain_unpack_or_named(list)
4305
757
     && list->children == 3
4306
338
     && list->child[1]->kind == ZEND_AST_ZVAL) {
4307
274
      zval *zv = zend_ast_get_zval(list->child[1]);
4308
4309
274
      if (Z_TYPE_P(zv) == IS_LONG
4310
246
       && Z_LVAL_P(zv) >= 0
4311
246
       && Z_LVAL_P(zv) <= 0x7fffffff) {
4312
67
        zend_op *opline;
4313
67
        znode len_node;
4314
4315
67
        zend_compile_expr(&arg_node, list->child[0]);
4316
67
        zend_compile_expr(&len_node, list->child[2]);
4317
67
        opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node);
4318
67
        opline->extended_value = Z_LVAL_P(zv);
4319
67
        zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4320
67
        zend_string_release_ex(name, 0);
4321
67
        return SUCCESS;
4322
67
      }
4323
274
    }
4324
1.38k
    zend_string_release_ex(name, 0);
4325
1.38k
  }
4326
1.83k
  zend_compile_expr(&arg_node, args->child[1]);
4327
1.83k
  zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
4328
1.83k
  zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
4329
1.83k
  opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4330
1.83k
  opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4331
4332
1.83k
  return SUCCESS;
4333
1.89k
}
4334
/* }}} */
4335
4336
/* cuf = call_user_func */
4337
static zend_result zend_compile_func_cuf(znode *result, const zend_ast_list *args, zend_string *lcname) /* {{{ */
4338
18.2k
{
4339
18.2k
  uint32_t i;
4340
4341
18.2k
  if (args->children < 1) {
4342
66
    return FAILURE;
4343
66
  }
4344
4345
18.1k
  zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
4346
19.0k
  for (i = 1; i < args->children; ++i) {
4347
825
    zend_ast *arg_ast = args->child[i];
4348
825
    znode arg_node;
4349
825
    zend_op *opline;
4350
4351
825
    zend_compile_expr(&arg_node, arg_ast);
4352
4353
825
    opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
4354
825
    opline->op2.num = i;
4355
825
    opline->result.var = EX_NUM_TO_VAR(i - 1);
4356
825
  }
4357
18.1k
  zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4358
4359
18.1k
  return SUCCESS;
4360
18.2k
}
4361
/* }}} */
4362
4363
static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, const zend_function *fbc, uint32_t lineno) /* {{{ */
4364
21.9k
{
4365
21.9k
  if (EG(assertions) >= 0) {
4366
21.9k
    znode name_node;
4367
21.9k
    zend_op *opline;
4368
21.9k
    uint32_t check_op_number = get_next_op_number();
4369
4370
21.9k
    zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
4371
4372
21.9k
    if (fbc && fbc_is_finalized(fbc)) {
4373
19.0k
      name_node.op_type = IS_CONST;
4374
19.0k
      ZVAL_STR_COPY(&name_node.u.constant, name);
4375
4376
19.0k
      opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
4377
19.0k
    } else {
4378
2.89k
      opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
4379
2.89k
      opline->op2_type = IS_CONST;
4380
2.89k
      opline->op2.constant = zend_add_ns_func_name_literal(name);
4381
2.89k
    }
4382
21.9k
    opline->result.num = zend_alloc_cache_slot();
4383
4384
21.9k
    if (args->children == 1) {
4385
      /* add "assert(condition) as assertion message */
4386
19.3k
      zend_ast *arg = zend_ast_create_zval_from_str(
4387
19.3k
        zend_ast_export("assert(", args->child[0], ")"));
4388
19.3k
      if (args->child[0]->kind == ZEND_AST_NAMED_ARG) {
4389
        /* If the original argument was named, add the new argument as named as well,
4390
         * as mixing named and positional is not allowed. */
4391
341
        zend_ast *name = zend_ast_create_zval_from_str(
4392
341
          ZSTR_INIT_LITERAL("description", 0));
4393
341
        arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg);
4394
341
      }
4395
19.3k
      args = (zend_ast_list *)zend_ast_list_add((zend_ast *) args, arg);
4396
19.3k
    }
4397
4398
21.9k
    zend_compile_call_common(result, (zend_ast*)args, fbc, lineno);
4399
4400
21.9k
    opline = &CG(active_op_array)->opcodes[check_op_number];
4401
21.9k
    opline->op2.opline_num = get_next_op_number();
4402
21.9k
    SET_NODE(opline->result, result);
4403
21.9k
  } else {
4404
0
    if (!fbc) {
4405
0
      zend_string_release_ex(name, 0);
4406
0
    }
4407
0
    result->op_type = IS_CONST;
4408
0
    ZVAL_TRUE(&result->u.constant);
4409
0
  }
4410
21.9k
}
4411
/* }}} */
4412
4413
static zend_result zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */
4414
10.5k
{
4415
10.5k
  bool strict = false;
4416
10.5k
  znode array, needly;
4417
10.5k
  zend_op *opline;
4418
4419
10.5k
  if (args->children == 3) {
4420
8.76k
    if (args->child[2]->kind == ZEND_AST_ZVAL) {
4421
690
      strict = zend_is_true(zend_ast_get_zval(args->child[2]));
4422
8.07k
    } else if (args->child[2]->kind == ZEND_AST_CONST) {
4423
663
      zval value;
4424
663
      zend_ast *name_ast = args->child[2]->child[0];
4425
663
      bool is_fully_qualified;
4426
663
      zend_string *resolved_name = zend_resolve_const_name(
4427
663
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
4428
4429
663
      if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) {
4430
455
        zend_string_release_ex(resolved_name, 0);
4431
455
        return FAILURE;
4432
455
      }
4433
4434
208
      zend_string_release_ex(resolved_name, 0);
4435
208
      strict = zend_is_true(&value);
4436
208
      zval_ptr_dtor(&value);
4437
7.41k
    } else {
4438
7.41k
      return FAILURE;
4439
7.41k
    }
4440
8.76k
  } else if (args->children != 2) {
4441
351
    return FAILURE;
4442
351
  }
4443
4444
2.32k
  if (args->child[1]->kind != ZEND_AST_ARRAY
4445
1.61k
   || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) {
4446
746
    return FAILURE;
4447
746
  }
4448
4449
1.57k
  if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) {
4450
1.36k
    bool ok = true;
4451
1.36k
    zval *val, tmp;
4452
1.36k
    HashTable *src = Z_ARRVAL(array.u.constant);
4453
1.36k
    HashTable *dst = zend_new_array(zend_hash_num_elements(src));
4454
4455
1.36k
    ZVAL_TRUE(&tmp);
4456
4457
1.36k
    if (strict) {
4458
4.26k
      ZEND_HASH_FOREACH_VAL(src, val) {
4459
4.26k
        if (Z_TYPE_P(val) == IS_STRING) {
4460
22
          zend_hash_add(dst, Z_STR_P(val), &tmp);
4461
1.24k
        } else if (Z_TYPE_P(val) == IS_LONG) {
4462
1.15k
          zend_hash_index_add(dst, Z_LVAL_P(val), &tmp);
4463
1.15k
        } else {
4464
95
          zend_array_destroy(dst);
4465
95
          ok = false;
4466
95
          break;
4467
95
        }
4468
4.26k
      } ZEND_HASH_FOREACH_END();
4469
707
    } else {
4470
3.57k
      ZEND_HASH_FOREACH_VAL(src, val) {
4471
3.57k
        if (Z_TYPE_P(val) != IS_STRING
4472
604
         || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) {
4473
495
          zend_array_destroy(dst);
4474
495
          ok = false;
4475
495
          break;
4476
495
        }
4477
530
        zend_hash_add(dst, Z_STR_P(val), &tmp);
4478
530
      } ZEND_HASH_FOREACH_END();
4479
707
    }
4480
4481
1.36k
    zend_array_destroy(src);
4482
1.36k
    if (!ok) {
4483
590
      return FAILURE;
4484
590
    }
4485
776
    Z_ARRVAL(array.u.constant) = dst;
4486
776
  }
4487
986
  array.op_type = IS_CONST;
4488
4489
986
  zend_compile_expr(&needly, args->child[0]);
4490
4491
986
  opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array);
4492
986
  opline->extended_value = strict;
4493
4494
986
  return SUCCESS;
4495
1.57k
}
4496
/* }}} */
4497
4498
static zend_result zend_compile_func_count(znode *result, const zend_ast_list *args, const zend_string *lcname) /* {{{ */
4499
388
{
4500
388
  znode arg_node;
4501
388
  zend_op *opline;
4502
4503
388
  if (args->children != 1) {
4504
70
    return FAILURE;
4505
70
  }
4506
4507
318
  zend_compile_expr(&arg_node, args->child[0]);
4508
318
  opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL);
4509
318
  opline->extended_value = zend_string_equals_literal(lcname, "sizeof");
4510
4511
318
  return SUCCESS;
4512
388
}
4513
/* }}} */
4514
4515
static zend_result zend_compile_func_get_class(znode *result, const zend_ast_list *args) /* {{{ */
4516
305
{
4517
305
  if (args->children == 0) {
4518
18
    zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL);
4519
287
  } else {
4520
287
    znode arg_node;
4521
4522
287
    if (args->children != 1) {
4523
67
      return FAILURE;
4524
67
    }
4525
4526
220
    zend_compile_expr(&arg_node, args->child[0]);
4527
220
    zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL);
4528
220
  }
4529
238
  return SUCCESS;
4530
305
}
4531
/* }}} */
4532
4533
static zend_result zend_compile_func_get_called_class(znode *result, const zend_ast_list *args) /* {{{ */
4534
252
{
4535
252
  if (args->children != 0) {
4536
54
    return FAILURE;
4537
54
  }
4538
4539
198
  zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL);
4540
198
  return SUCCESS;
4541
252
}
4542
/* }}} */
4543
4544
static zend_result zend_compile_func_gettype(znode *result, const zend_ast_list *args) /* {{{ */
4545
97
{
4546
97
  znode arg_node;
4547
4548
97
  if (args->children != 1) {
4549
18
    return FAILURE;
4550
18
  }
4551
4552
79
  zend_compile_expr(&arg_node, args->child[0]);
4553
79
  zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL);
4554
79
  return SUCCESS;
4555
97
}
4556
/* }}} */
4557
4558
static zend_result zend_compile_func_num_args(znode *result, const zend_ast_list *args) /* {{{ */
4559
121
{
4560
121
  if (CG(active_op_array)->function_name && args->children == 0) {
4561
32
    zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL);
4562
32
    return SUCCESS;
4563
89
  } else {
4564
89
    return FAILURE;
4565
89
  }
4566
121
}
4567
/* }}} */
4568
4569
static zend_result zend_compile_func_get_args(znode *result, const zend_ast_list *args) /* {{{ */
4570
520
{
4571
520
  if (CG(active_op_array)->function_name && args->children == 0) {
4572
425
    zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL);
4573
425
    return SUCCESS;
4574
425
  } else {
4575
95
    return FAILURE;
4576
95
  }
4577
520
}
4578
/* }}} */
4579
4580
static zend_result zend_compile_func_array_key_exists(znode *result, const zend_ast_list *args) /* {{{ */
4581
158
{
4582
158
  znode subject, needle;
4583
4584
158
  if (args->children != 2) {
4585
68
    return FAILURE;
4586
68
  }
4587
4588
90
  zend_compile_expr(&needle, args->child[0]);
4589
90
  zend_compile_expr(&subject, args->child[1]);
4590
4591
90
  zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject);
4592
90
  return SUCCESS;
4593
158
}
4594
/* }}} */
4595
4596
static zend_result zend_compile_func_array_slice(znode *result, const zend_ast_list *args) /* {{{ */
4597
1.46k
{
4598
1.46k
  if (CG(active_op_array)->function_name
4599
642
   && args->children == 2
4600
535
   && args->child[0]->kind == ZEND_AST_CALL
4601
473
   && args->child[0]->child[0]->kind == ZEND_AST_ZVAL
4602
355
   && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING
4603
355
   && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST
4604
355
   && args->child[1]->kind == ZEND_AST_ZVAL) {
4605
4606
314
    zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]);
4607
314
    bool is_fully_qualified;
4608
314
    zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified);
4609
314
    const zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]);
4610
314
    const zval *zv = zend_ast_get_zval(args->child[1]);
4611
314
    znode first;
4612
4613
314
    if (zend_string_equals_literal_ci(name, "func_get_args")
4614
185
     && list->children == 0
4615
175
     && Z_TYPE_P(zv) == IS_LONG
4616
105
     && Z_LVAL_P(zv) >= 0) {
4617
105
      first.op_type = IS_CONST;
4618
105
      ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv));
4619
105
      zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL);
4620
105
      zend_string_release_ex(name, 0);
4621
105
      return SUCCESS;
4622
105
    }
4623
209
    zend_string_release_ex(name, 0);
4624
209
  }
4625
1.36k
  return FAILURE;
4626
1.46k
}
4627
/* }}} */
4628
4629
static uint32_t find_frameless_function_offset(uint32_t arity, const void *handler)
4630
1.27M
{
4631
1.27M
  void **handlers = zend_flf_handlers;
4632
1.27M
  void **current = handlers;
4633
11.7M
  while (current) {
4634
11.7M
    if (*current == handler) {
4635
1.27M
      return current - handlers;
4636
1.27M
    }
4637
10.4M
    current++;
4638
10.4M
  }
4639
4640
0
  return (uint32_t)-1;
4641
1.27M
}
4642
4643
static const zend_frameless_function_info *find_frameless_function_info(const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4644
762k
{
4645
762k
  if (zend_execute_internal) {
4646
20.3k
    return NULL;
4647
20.3k
  }
4648
4649
742k
  if (type != BP_VAR_R) {
4650
674
    return NULL;
4651
674
  }
4652
4653
741k
  if (ZEND_USER_CODE(fbc->type)) {
4654
10
    return NULL;
4655
10
  }
4656
4657
741k
  const zend_frameless_function_info *frameless_function_info = fbc->internal_function.frameless_function_infos;
4658
741k
  if (!frameless_function_info) {
4659
16.7k
    return NULL;
4660
16.7k
  }
4661
4662
724k
  if (args->children > 3) {
4663
291
    return NULL;
4664
291
  }
4665
4666
1.05M
  while (frameless_function_info->handler) {
4667
967k
    if (frameless_function_info->num_args >= args->children
4668
813k
     && fbc->common.required_num_args <= args->children
4669
635k
     && (!(fbc->common.fn_flags & ZEND_ACC_VARIADIC)
4670
635k
      || frameless_function_info->num_args == args->children)) {
4671
635k
      uint32_t num_args = frameless_function_info->num_args;
4672
635k
      uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4673
635k
      if (offset == (uint32_t)-1) {
4674
0
        continue;
4675
0
      }
4676
635k
      return frameless_function_info;
4677
635k
    }
4678
331k
    frameless_function_info++;
4679
331k
  }
4680
4681
89.2k
  return NULL;
4682
724k
}
4683
4684
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)
4685
635k
{
4686
635k
  uint32_t lineno = CG(zend_lineno);
4687
635k
  uint32_t num_args = frameless_function_info->num_args;
4688
635k
  uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4689
635k
  znode arg_zvs[3];
4690
2.05M
  for (uint32_t i = 0; i < num_args; i++) {
4691
1.42M
    if (i < args->children) {
4692
1.42M
      zend_compile_expr(&arg_zvs[i], args->child[i]);
4693
1.42M
    } else {
4694
0
      const zend_arg_info *arg_info = &fbc->common.arg_info[i];
4695
0
      arg_zvs[i].op_type = IS_CONST;
4696
0
      if (zend_get_default_from_internal_arg_info(&arg_zvs[i].u.constant, arg_info) == FAILURE) {
4697
0
        ZEND_UNREACHABLE();
4698
0
      }
4699
0
    }
4700
1.42M
  }
4701
635k
  uint8_t opcode = ZEND_FRAMELESS_ICALL_0 + num_args;
4702
635k
  uint32_t opnum = get_next_op_number();
4703
635k
  zend_op *opline = zend_emit_op_tmp(result, opcode, NULL, NULL);
4704
635k
  opline->extended_value = offset;
4705
635k
  opline->lineno = lineno;
4706
635k
  if (num_args >= 1) {
4707
634k
    SET_NODE(opline->op1, &arg_zvs[0]);
4708
634k
  }
4709
635k
  if (num_args >= 2) {
4710
634k
    SET_NODE(opline->op2, &arg_zvs[1]);
4711
634k
  }
4712
635k
  if (num_args >= 3) {
4713
153k
    zend_emit_op_data(&arg_zvs[2]);
4714
153k
  }
4715
635k
  return opnum;
4716
635k
}
4717
4718
static uint32_t zend_compile_frameless_icall(znode *result, const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4719
38.5k
{
4720
38.5k
  const zend_frameless_function_info *frameless_function_info = find_frameless_function_info(args, fbc, type);
4721
38.5k
  if (!frameless_function_info) {
4722
29.3k
    return (uint32_t)-1;
4723
29.3k
  }
4724
4725
9.24k
  return zend_compile_frameless_icall_ex(result, args, fbc, frameless_function_info, type);
4726
38.5k
}
4727
4728
static void zend_compile_ns_call(znode *result, const znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */
4729
3.35M
{
4730
3.35M
  int name_constants = zend_add_ns_func_name_literal(Z_STR(name_node->u.constant));
4731
4732
  /* Find frameless function with same name. */
4733
3.35M
  const zend_function *frameless_function = NULL;
4734
3.35M
  if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT
4735
3.35M
   && !zend_args_contain_unpack_or_named(zend_ast_get_list(args_ast))
4736
   /* Avoid blowing up op count with nested frameless branches. */
4737
3.31M
   && !CG(context).in_jmp_frameless_branch) {
4738
2.09M
    zend_string *lc_func_name = Z_STR_P(CT_CONSTANT_EX(CG(active_op_array), name_constants + 2));
4739
2.09M
    frameless_function = zend_hash_find_ptr(CG(function_table), lc_func_name);
4740
2.09M
  }
4741
4742
  /* Check whether any frameless handler may actually be used. */
4743
3.35M
  uint32_t jmp_fl_opnum = 0;
4744
3.35M
  const zend_frameless_function_info *frameless_function_info = NULL;
4745
3.35M
  if (frameless_function) {
4746
724k
    frameless_function_info = find_frameless_function_info(zend_ast_get_list(args_ast), frameless_function, type);
4747
724k
    if (frameless_function_info) {
4748
626k
      CG(context).in_jmp_frameless_branch = true;
4749
626k
      znode op1;
4750
626k
      op1.op_type = IS_CONST;
4751
626k
      ZVAL_COPY(&op1.u.constant, CT_CONSTANT_EX(CG(active_op_array), name_constants + 1));
4752
626k
      jmp_fl_opnum = get_next_op_number();
4753
626k
      zend_emit_op(NULL, ZEND_JMP_FRAMELESS, &op1, NULL);
4754
626k
    }
4755
724k
  }
4756
4757
  /* Compile ns call. */
4758
3.35M
  zend_op *opline = get_next_op();
4759
3.35M
  opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
4760
3.35M
  opline->op2_type = IS_CONST;
4761
3.35M
  opline->op2.constant = name_constants;
4762
3.35M
  opline->result.num = zend_alloc_cache_slot();
4763
3.35M
  zend_compile_call_common(result, args_ast, NULL, lineno);
4764
4765
  /* Compile frameless call. */
4766
3.35M
  if (frameless_function_info) {
4767
625k
    CG(zend_lineno) = lineno;
4768
4769
625k
    uint32_t jmp_end_opnum = zend_emit_jump(0);
4770
625k
    uint32_t jmp_fl_target = get_next_op_number();
4771
4772
625k
    uint32_t flf_icall_opnum = zend_compile_frameless_icall_ex(NULL, zend_ast_get_list(args_ast), frameless_function, frameless_function_info, type);
4773
4774
625k
    zend_op *jmp_fl = &CG(active_op_array)->opcodes[jmp_fl_opnum];
4775
625k
    jmp_fl->op2.opline_num = jmp_fl_target;
4776
625k
    jmp_fl->extended_value = zend_alloc_cache_slot();
4777
625k
    zend_op *flf_icall = &CG(active_op_array)->opcodes[flf_icall_opnum];
4778
625k
    SET_NODE(flf_icall->result, result);
4779
625k
    zend_update_jump_target_to_next(jmp_end_opnum);
4780
4781
625k
    CG(context).in_jmp_frameless_branch = false;
4782
625k
  }
4783
3.35M
}
4784
/* }}} */
4785
4786
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node);
4787
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node);
4788
static void zend_compile_rope_finalize(znode *result, uint32_t j, zend_op *init_opline, zend_op *opline);
4789
4790
static zend_result zend_compile_func_sprintf(znode *result, zend_ast_list *args) /* {{{ */
4791
2.86k
{
4792
  /* Bail out if we do not have a format string. */
4793
2.86k
  if (args->children < 1) {
4794
241
    return FAILURE;
4795
241
  }
4796
4797
2.61k
  zend_eval_const_expr(&args->child[0]);
4798
  /* Bail out if the format string is not constant. */
4799
2.61k
  if (args->child[0]->kind != ZEND_AST_ZVAL) {
4800
262
    return FAILURE;
4801
262
  }
4802
4803
2.35k
  zval *format_string = zend_ast_get_zval(args->child[0]);
4804
2.35k
  if (Z_TYPE_P(format_string) != IS_STRING) {
4805
6
    return FAILURE;
4806
6
  }
4807
2.35k
  if (Z_STRLEN_P(format_string) >= 256) {
4808
18
    return FAILURE;
4809
18
  }
4810
4811
2.33k
  char *p;
4812
2.33k
  char *end;
4813
2.33k
  uint32_t placeholder_count;
4814
4815
2.33k
  placeholder_count = 0;
4816
2.33k
  p = Z_STRVAL_P(format_string);
4817
2.33k
  end = p + Z_STRLEN_P(format_string);
4818
4819
5.58k
  for (;;) {
4820
5.58k
    p = memchr(p, '%', end - p);
4821
5.58k
    if (!p) {
4822
2.01k
      break;
4823
2.01k
    }
4824
4825
3.57k
    char *q = p + 1;
4826
3.57k
    if (q == end) {
4827
219
      return FAILURE;
4828
219
    }
4829
4830
3.35k
    switch (*q) {
4831
1.78k
      case 's':
4832
2.10k
      case 'd':
4833
2.10k
        placeholder_count++;
4834
2.10k
        break;
4835
1.14k
      case '%':
4836
1.14k
        break;
4837
103
      default:
4838
103
        return FAILURE;
4839
3.35k
    }
4840
4841
3.24k
    p = q;
4842
3.24k
    p++;
4843
3.24k
  }
4844
4845
  /* Bail out if the number of placeholders does not match the number of values. */
4846
2.01k
  if (placeholder_count != (args->children - 1)) {
4847
154
    return FAILURE;
4848
154
  }
4849
4850
  /* Handle empty format strings. */
4851
1.85k
  if (Z_STRLEN_P(format_string) == 0) {
4852
68
    result->op_type = IS_CONST;
4853
68
    ZVAL_EMPTY_STRING(&result->u.constant);
4854
4855
68
    return SUCCESS;
4856
68
  }
4857
4858
1.78k
  znode *elements = NULL;
4859
4860
1.78k
  if (placeholder_count > 0) {
4861
1.21k
    elements = safe_emalloc(sizeof(*elements), placeholder_count, 0);
4862
1.21k
  }
4863
4864
  /* Compile the value expressions first for error handling that is consistent
4865
   * with a function call: Values that fail to convert to a string may emit errors.
4866
   */
4867
3.57k
  for (uint32_t i = 0; i < placeholder_count; i++) {
4868
1.78k
    zend_compile_expr(elements + i, args->child[1 + i]);
4869
1.78k
  }
4870
4871
1.78k
  uint32_t rope_elements = 0;
4872
1.78k
  uint32_t rope_init_lineno = -1;
4873
1.78k
  zend_op *opline = NULL;
4874
4875
1.78k
  placeholder_count = 0;
4876
1.78k
  p = Z_STRVAL_P(format_string);
4877
1.78k
  end = p + Z_STRLEN_P(format_string);
4878
1.78k
  char *offset = p;
4879
4.55k
  for (;;) {
4880
4.55k
    p = memchr(p, '%', end - p);
4881
4.55k
    if (!p) {
4882
1.78k
      break;
4883
1.78k
    }
4884
4885
2.76k
    char *q = p + 1;
4886
2.76k
    ZEND_ASSERT(q < end);
4887
2.76k
    ZEND_ASSERT(*q == 's' || *q == 'd' || *q == '%');
4888
4889
2.76k
    if (*q == '%') {
4890
      /* Optimization to not create a dedicated rope element for the literal '%':
4891
       * Include the first '%' within the "constant" part instead of dropping the
4892
       * full placeholder.
4893
       */
4894
979
      p++;
4895
979
    }
4896
4897
2.76k
    if (p != offset) {
4898
2.15k
      znode const_node;
4899
2.15k
      const_node.op_type = IS_CONST;
4900
2.15k
      ZVAL_STRINGL(&const_node.u.constant, offset, p - offset);
4901
2.15k
      if (rope_elements == 0) {
4902
1.01k
        rope_init_lineno = get_next_op_number();
4903
1.01k
      }
4904
2.15k
      opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4905
2.15k
    }
4906
4907
2.76k
    if (*q != '%') {
4908
1.78k
      switch (*q) {
4909
1.51k
        case 's':
4910
          /* Perform the cast of constants when actually evaluating the corresponding placeholder
4911
           * for correct error reporting.
4912
           */
4913
1.51k
          if (elements[placeholder_count].op_type == IS_CONST) {
4914
559
            if (Z_TYPE(elements[placeholder_count].u.constant) == IS_ARRAY) {
4915
108
              zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_STRING;
4916
451
            } else {
4917
451
              convert_to_string(&elements[placeholder_count].u.constant);
4918
451
            }
4919
559
          }
4920
1.51k
          break;
4921
268
        case 'd':
4922
268
          zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_LONG;
4923
268
          break;
4924
0
        EMPTY_SWITCH_DEFAULT_CASE();
4925
1.78k
      }
4926
4927
1.78k
      if (rope_elements == 0) {
4928
396
        rope_init_lineno = get_next_op_number();
4929
396
      }
4930
1.78k
      opline = zend_compile_rope_add(result, rope_elements++, &elements[placeholder_count]);
4931
4932
1.78k
      placeholder_count++;
4933
1.78k
    }
4934
4935
2.76k
    p = q;
4936
2.76k
    p++;
4937
2.76k
    offset = p;
4938
2.76k
  }
4939
1.78k
  if (end != offset) {
4940
    /* Add the constant part after the last placeholder. */
4941
1.29k
    znode const_node;
4942
1.29k
    const_node.op_type = IS_CONST;
4943
1.29k
    ZVAL_STRINGL(&const_node.u.constant, offset, end - offset);
4944
1.29k
    if (rope_elements == 0) {
4945
380
      rope_init_lineno = get_next_op_number();
4946
380
    }
4947
1.29k
    opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4948
1.29k
  }
4949
1.78k
  ZEND_ASSERT(opline != NULL);
4950
4951
1.78k
  zend_op *init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
4952
1.78k
  zend_compile_rope_finalize(result, rope_elements, init_opline, opline);
4953
1.78k
  efree(elements);
4954
4955
1.78k
  return SUCCESS;
4956
1.78k
}
4957
4958
static zend_result zend_compile_func_printf(znode *result, zend_ast_list *args) /* {{{ */
4959
1.30k
{
4960
  /* Special case: printf with a single constant string argument and no format specifiers.
4961
   * In this case, just emit ECHO and return the string length if needed. */
4962
1.30k
  if (args->children == 1) {
4963
778
    zend_eval_const_expr(&args->child[0]);
4964
778
    if (args->child[0]->kind != ZEND_AST_ZVAL) {
4965
212
      return FAILURE;
4966
212
    }
4967
566
    zval *format_string = zend_ast_get_zval(args->child[0]);
4968
566
    if (Z_TYPE_P(format_string) != IS_STRING) {
4969
66
      return FAILURE;
4970
66
    }
4971
    /* Check if there are any format specifiers */
4972
500
    if (!memchr(Z_STRVAL_P(format_string), '%', Z_STRLEN_P(format_string))) {
4973
      /* No format specifiers - just emit ECHO and return string length */
4974
120
      znode format_node;
4975
120
      zend_compile_expr(&format_node, args->child[0]);
4976
120
      zend_emit_op(NULL, ZEND_ECHO, &format_node, NULL);
4977
4978
      /* Return the string length as a constant if the result is used */
4979
120
      result->op_type = IS_CONST;
4980
120
      ZVAL_LONG(&result->u.constant, Z_STRLEN_P(format_string));
4981
120
      return SUCCESS;
4982
120
    }
4983
500
  }
4984
4985
  /* Fall back to sprintf optimization for format strings with specifiers */
4986
906
  znode rope_result;
4987
906
  if (zend_compile_func_sprintf(&rope_result, args) != SUCCESS) {
4988
222
    return FAILURE;
4989
222
  }
4990
4991
  /* printf() returns the amount of bytes written, so just an ECHO of the
4992
   * resulting sprintf() optimisation might not be enough. At this early
4993
   * stage we can't detect if the result is actually used, so we just emit
4994
   * the opcodes and let them be cleaned up by the dead code elimination
4995
   * pass in the Zend Optimizer if the result of the printf() is in fact
4996
   * unused */
4997
684
  znode copy;
4998
684
  if (rope_result.op_type != IS_CONST) {
4999
    /* Note: ZEND_COPY_TMP is only valid for TMPVAR. */
5000
582
    ZEND_ASSERT(rope_result.op_type == IS_TMP_VAR);
5001
582
    zend_emit_op_tmp(&copy, ZEND_COPY_TMP, &rope_result, NULL);
5002
582
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5003
582
    zend_emit_op_tmp(result, ZEND_STRLEN, &copy, NULL);
5004
582
  } else {
5005
102
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5006
102
    result->op_type = IS_CONST;
5007
102
    ZVAL_LONG(&result->u.constant, Z_STRLEN(rope_result.u.constant));
5008
102
  }
5009
5010
684
  return SUCCESS;
5011
684
}
5012
5013
static zend_result zend_compile_func_clone(znode *result, const zend_ast_list *args)
5014
810
{
5015
810
  znode arg_node;
5016
5017
810
  if (args->children != 1) {
5018
233
    return FAILURE;
5019
233
  }
5020
5021
577
  zend_compile_expr(&arg_node, args->child[0]);
5022
577
  zend_emit_op_tmp(result, ZEND_CLONE, &arg_node, NULL);
5023
5024
577
  return SUCCESS;
5025
810
}
5026
5027
static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type) /* {{{ */
5028
67.2k
{
5029
67.2k
  if (zend_string_equals_literal(lcname, "strlen")) {
5030
813
    return zend_compile_func_strlen(result, args);
5031
66.4k
  } else if (zend_string_equals_literal(lcname, "is_null")) {
5032
204
    return zend_compile_func_typecheck(result, args, IS_NULL);
5033
66.2k
  } else if (zend_string_equals_literal(lcname, "is_bool")) {
5034
17
    return zend_compile_func_typecheck(result, args, _IS_BOOL);
5035
66.2k
  } else if (zend_string_equals_literal(lcname, "is_long")
5036
66.1k
    || zend_string_equals_literal(lcname, "is_int")
5037
65.9k
    || zend_string_equals_literal(lcname, "is_integer")
5038
66.2k
  ) {
5039
474
    return zend_compile_func_typecheck(result, args, IS_LONG);
5040
65.7k
  } else if (zend_string_equals_literal(lcname, "is_float")
5041
65.6k
    || zend_string_equals_literal(lcname, "is_double")
5042
65.7k
  ) {
5043
199
    return zend_compile_func_typecheck(result, args, IS_DOUBLE);
5044
65.5k
  } else if (zend_string_equals_literal(lcname, "is_string")) {
5045
8
    return zend_compile_func_typecheck(result, args, IS_STRING);
5046
65.5k
  } else if (zend_string_equals_literal(lcname, "is_array")) {
5047
110
    return zend_compile_func_typecheck(result, args, IS_ARRAY);
5048
65.4k
  } else if (zend_string_equals_literal(lcname, "is_object")) {
5049
88
    return zend_compile_func_typecheck(result, args, IS_OBJECT);
5050
65.3k
  } else if (zend_string_equals_literal(lcname, "is_resource")) {
5051
39
    return zend_compile_func_typecheck(result, args, IS_RESOURCE);
5052
65.3k
  } else if (zend_string_equals_literal(lcname, "is_scalar")) {
5053
78
    return zend_compile_func_is_scalar(result, args);
5054
65.2k
  } else if (zend_string_equals_literal(lcname, "boolval")) {
5055
41
    return zend_compile_func_cast(result, args, _IS_BOOL);
5056
65.2k
  } else if (zend_string_equals_literal(lcname, "intval")) {
5057
73
    return zend_compile_func_cast(result, args, IS_LONG);
5058
65.1k
  } else if (zend_string_equals_literal(lcname, "floatval")
5059
64.9k
    || zend_string_equals_literal(lcname, "doubleval")
5060
65.1k
  ) {
5061
213
    return zend_compile_func_cast(result, args, IS_DOUBLE);
5062
64.9k
  } else if (zend_string_equals_literal(lcname, "strval")) {
5063
209
    return zend_compile_func_cast(result, args, IS_STRING);
5064
64.7k
  } else if (zend_string_equals_literal(lcname, "defined")) {
5065
1.86k
    return zend_compile_func_defined(result, args);
5066
62.8k
  } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) {
5067
1.29k
    return zend_compile_func_chr(result, args);
5068
61.5k
  } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) {
5069
528
    return zend_compile_func_ord(result, args);
5070
61.0k
  } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
5071
1.98k
    return zend_compile_func_cufa(result, args, lcname);
5072
59.0k
  } else if (zend_string_equals_literal(lcname, "call_user_func")) {
5073
18.2k
    return zend_compile_func_cuf(result, args, lcname);
5074
40.7k
  } else if (zend_string_equals_literal(lcname, "in_array")) {
5075
10.5k
    return zend_compile_func_in_array(result, args);
5076
30.2k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_COUNT))
5077
30.0k
      || zend_string_equals_literal(lcname, "sizeof")) {
5078
388
    return zend_compile_func_count(result, args, lcname);
5079
29.8k
  } else if (zend_string_equals_literal(lcname, "get_class")) {
5080
305
    return zend_compile_func_get_class(result, args);
5081
29.5k
  } else if (zend_string_equals_literal(lcname, "get_called_class")) {
5082
252
    return zend_compile_func_get_called_class(result, args);
5083
29.3k
  } else if (zend_string_equals_literal(lcname, "gettype")) {
5084
97
    return zend_compile_func_gettype(result, args);
5085
29.2k
  } else if (zend_string_equals_literal(lcname, "func_num_args")) {
5086
121
    return zend_compile_func_num_args(result, args);
5087
29.0k
  } else if (zend_string_equals_literal(lcname, "func_get_args")) {
5088
520
    return zend_compile_func_get_args(result, args);
5089
28.5k
  } else if (zend_string_equals_literal(lcname, "array_slice")) {
5090
1.46k
    return zend_compile_func_array_slice(result, args);
5091
27.1k
  } else if (zend_string_equals_literal(lcname, "array_key_exists")) {
5092
158
    return zend_compile_func_array_key_exists(result, args);
5093
26.9k
  } else if (zend_string_equals_literal(lcname, "sprintf")) {
5094
1.95k
    return zend_compile_func_sprintf(result, args);
5095
24.9k
  } else if (zend_string_equals_literal(lcname, "printf")) {
5096
1.30k
    return zend_compile_func_printf(result, args);
5097
23.6k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_CLONE))) {
5098
810
    return zend_compile_func_clone(result, args);
5099
22.8k
  } else {
5100
22.8k
    return FAILURE;
5101
22.8k
  }
5102
67.2k
}
5103
5104
static zend_result zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, const zend_function *fbc, uint32_t type) /* {{{ */
5105
73.3k
{
5106
73.3k
  if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
5107
0
    return FAILURE;
5108
0
  }
5109
5110
73.3k
  if (fbc->type != ZEND_INTERNAL_FUNCTION) {
5111
    /* If the function is part of disabled_functions, it may be redeclared as a userland
5112
     * function with a different implementation. Don't use the VM builtin in that case. */
5113
5.19k
    return FAILURE;
5114
5.19k
  }
5115
5116
68.1k
  if (zend_args_contain_unpack_or_named(args)) {
5117
859
    return FAILURE;
5118
859
  }
5119
5120
67.2k
  if (zend_try_compile_special_func_ex(result, lcname, args, type) == SUCCESS) {
5121
28.7k
    return SUCCESS;
5122
28.7k
  }
5123
5124
38.5k
  return zend_compile_frameless_icall(result, args, fbc, type) != (uint32_t)-1 ? SUCCESS : FAILURE;
5125
67.2k
}
5126
5127
3
static const char *zend_get_cstring_from_property_hook_kind(zend_property_hook_kind kind) {
5128
3
  switch (kind) {
5129
1
    case ZEND_PROPERTY_HOOK_GET:
5130
1
      return "get";
5131
2
    case ZEND_PROPERTY_HOOK_SET:
5132
2
      return "set";
5133
3
    EMPTY_SWITCH_DEFAULT_CASE()
5134
3
  }
5135
3
}
5136
5137
static zend_string *zend_copy_unmangled_prop_name(zend_string *prop_name)
5138
538
{
5139
538
  if (ZSTR_VAL(prop_name)[0] != '\0') {
5140
287
    return zend_string_copy(prop_name);
5141
287
  } else {
5142
251
    const char *unmangled = zend_get_unmangled_property_name(prop_name);
5143
251
    return zend_string_init(unmangled, strlen(unmangled), /* persistent */ false);
5144
251
  }
5145
538
}
5146
5147
static bool zend_compile_parent_property_hook_call(znode *result, const zend_ast *ast, uint32_t type)
5148
63.2k
{
5149
63.2k
  ZEND_ASSERT(ast->kind == ZEND_AST_STATIC_CALL);
5150
5151
63.2k
  const zend_ast *class_ast = ast->child[0];
5152
63.2k
  zend_ast *method_ast = ast->child[1];
5153
5154
  /* Recognize parent::$prop::get() pattern. */
5155
63.2k
  if (class_ast->kind != ZEND_AST_STATIC_PROP
5156
1.72k
   || (class_ast->attr & ZEND_PARENTHESIZED_STATIC_PROP)
5157
1.62k
   || class_ast->child[0]->kind != ZEND_AST_ZVAL
5158
1.44k
   || Z_TYPE_P(zend_ast_get_zval(class_ast->child[0])) != IS_STRING
5159
1.44k
   || zend_get_class_fetch_type(zend_ast_get_str(class_ast->child[0])) != ZEND_FETCH_CLASS_PARENT
5160
963
   || class_ast->child[1]->kind != ZEND_AST_ZVAL
5161
801
   || method_ast->kind != ZEND_AST_ZVAL
5162
722
   || Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING
5163
722
   || (!zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "get")
5164
62.7k
    && !zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "set"))) {
5165
62.7k
    return false;
5166
62.7k
  }
5167
5168
561
  zend_class_entry *ce = CG(active_class_entry);
5169
561
  if (!ce) {
5170
2
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"parent\" when no class scope is active");
5171
2
  }
5172
5173
559
  zend_ast *args_ast = ast->child[2];
5174
559
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
5175
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for parent property hook call");
5176
1
  }
5177
5178
558
  zval *property_hook_name_zv = zend_ast_get_zval(class_ast->child[1]);
5179
558
  zend_string *property_name = zval_get_string(property_hook_name_zv);
5180
558
  zend_string *hook_name = zend_ast_get_str(method_ast);
5181
558
  zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(hook_name);
5182
558
  ZEND_ASSERT(hook_kind != (uint32_t)-1);
5183
5184
558
  const zend_string *prop_info_name = CG(context).active_property_info_name;
5185
558
  if (!prop_info_name) {
5186
2
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() outside a property hook",
5187
2
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name));
5188
2
  }
5189
5190
556
  const char *unmangled_prop_name = zend_get_unmangled_property_name(prop_info_name);
5191
556
  if (!zend_string_equals_cstr(property_name, unmangled_prop_name, strlen(unmangled_prop_name))) {
5192
13
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property ($%s)",
5193
13
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), unmangled_prop_name);
5194
13
  }
5195
543
  if (hook_kind != CG(context).active_property_hook_kind) {
5196
3
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property hook (%s)",
5197
3
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), zend_get_cstring_from_property_hook_kind(CG(context).active_property_hook_kind));
5198
3
  }
5199
5200
540
  zend_op *opline = get_next_op();
5201
540
  opline->opcode = ZEND_INIT_PARENT_PROPERTY_HOOK_CALL;
5202
540
  opline->op1_type = IS_CONST;
5203
540
  opline->op1.constant = zend_add_literal_string(&property_name);
5204
540
  opline->op2.num = hook_kind;
5205
5206
540
  zend_compile_call_common(result, args_ast, NULL, zend_ast_get_lineno(method_ast));
5207
5208
540
  return true;
5209
543
}
5210
5211
static void zend_compile_call(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
5212
3.73M
{
5213
3.73M
  zend_ast *name_ast = ast->child[0];
5214
3.73M
  zend_ast *args_ast = ast->child[1];
5215
3.73M
  bool is_callable_convert = args_ast->kind == ZEND_AST_CALLABLE_CONVERT;
5216
5217
3.73M
  znode name_node;
5218
5219
3.73M
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
5220
222k
    zend_compile_expr(&name_node, name_ast);
5221
222k
    zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno);
5222
222k
    return;
5223
222k
  }
5224
5225
3.51M
  {
5226
3.51M
    bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
5227
3.51M
    if (runtime_resolution) {
5228
3.35M
      if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")
5229
3.10k
          && !is_callable_convert) {
5230
2.89k
        zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL, ast->lineno);
5231
3.35M
      } else {
5232
3.35M
        zend_compile_ns_call(result, &name_node, args_ast, ast->lineno, type);
5233
3.35M
      }
5234
3.35M
      return;
5235
3.35M
    }
5236
3.51M
  }
5237
5238
157k
  {
5239
157k
    const zval *name = &name_node.u.constant;
5240
157k
    zend_string *lcname = zend_string_tolower(Z_STR_P(name));
5241
157k
    zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5242
157k
    const zend_function *fbc = fbc_zv ? Z_PTR_P(fbc_zv) : NULL;
5243
157k
    zend_op *opline;
5244
5245
    /* Special assert() handling should apply independently of compiler flags. */
5246
157k
    if (fbc && zend_string_equals_literal(lcname, "assert") && !is_callable_convert) {
5247
19.0k
      zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc, ast->lineno);
5248
19.0k
      zend_string_release(lcname);
5249
19.0k
      zval_ptr_dtor(&name_node.u.constant);
5250
19.0k
      return;
5251
19.0k
    }
5252
5253
138k
    if (!fbc
5254
73.8k
     || !fbc_is_finalized(fbc)
5255
73.8k
     || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
5256
64.6k
      zend_string_release_ex(lcname, 0);
5257
64.6k
      zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno);
5258
64.6k
      return;
5259
64.6k
    }
5260
5261
73.8k
    if (!is_callable_convert &&
5262
73.3k
        zend_try_compile_special_func(result, lcname,
5263
73.3k
        zend_ast_get_list(args_ast), fbc, type) == SUCCESS
5264
73.8k
    ) {
5265
37.9k
      zend_string_release_ex(lcname, 0);
5266
37.9k
      zval_ptr_dtor(&name_node.u.constant);
5267
37.9k
      return;
5268
37.9k
    }
5269
5270
35.9k
    zval_ptr_dtor(&name_node.u.constant);
5271
35.9k
    ZVAL_NEW_STR(&name_node.u.constant, lcname);
5272
5273
35.9k
    opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
5274
35.9k
    opline->result.num = zend_alloc_cache_slot();
5275
5276
    /* Store offset to function from symbol table in op2.extra. */
5277
35.9k
    if (fbc->type == ZEND_INTERNAL_FUNCTION) {
5278
30.6k
      const Bucket *fbc_bucket = (const Bucket*)((uintptr_t)fbc_zv - XtOffsetOf(Bucket, val));
5279
30.6k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = fbc_bucket - CG(function_table)->arData;
5280
30.6k
    }
5281
5282
35.9k
    zend_compile_call_common(result, args_ast, fbc, ast->lineno);
5283
35.9k
  }
5284
35.9k
}
5285
/* }}} */
5286
5287
static void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5288
30.2k
{
5289
30.2k
  zend_ast *obj_ast = ast->child[0];
5290
30.2k
  zend_ast *method_ast = ast->child[1];
5291
30.2k
  zend_ast *args_ast = ast->child[2];
5292
5293
30.2k
  znode obj_node, method_node;
5294
30.2k
  zend_op *opline;
5295
30.2k
  const zend_function *fbc = NULL;
5296
30.2k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL;
5297
30.2k
  uint32_t short_circuiting_checkpoint = zend_short_circuiting_checkpoint();
5298
5299
30.2k
  if (is_this_fetch(obj_ast)) {
5300
1.92k
    if (this_guaranteed_exists()) {
5301
1.74k
      obj_node.op_type = IS_UNUSED;
5302
1.74k
    } else {
5303
179
      zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
5304
179
    }
5305
1.92k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
5306
5307
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
5308
     * check for a nullsafe access. */
5309
28.2k
  } else {
5310
28.2k
    zend_short_circuiting_mark_inner(obj_ast);
5311
28.2k
    zend_compile_expr(&obj_node, obj_ast);
5312
28.2k
    if (nullsafe) {
5313
2.74k
      zend_emit_jmp_null(&obj_node, type);
5314
2.74k
    }
5315
28.2k
  }
5316
5317
30.2k
  zend_compile_expr(&method_node, method_ast);
5318
30.2k
  opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
5319
5320
30.2k
  if (method_node.op_type == IS_CONST) {
5321
29.3k
    if (Z_TYPE(method_node.u.constant) != IS_STRING) {
5322
2
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5323
2
    }
5324
5325
29.3k
    opline->op2_type = IS_CONST;
5326
29.3k
    opline->op2.constant = zend_add_func_name_literal(
5327
29.3k
      Z_STR(method_node.u.constant));
5328
29.3k
    opline->result.num = zend_alloc_cache_slots(2);
5329
29.3k
  } else {
5330
892
    SET_NODE(opline->op2, &method_node);
5331
892
  }
5332
5333
  /* Check if this calls a known method on $this */
5334
30.2k
  if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST &&
5335
1.66k
      CG(active_class_entry) && zend_is_scope_known()) {
5336
1.59k
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5337
1.59k
    fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname);
5338
5339
    /* We only know the exact method that is being called if it is either private or final.
5340
     * Otherwise an overriding method in a child class may be called. */
5341
1.59k
    if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) {
5342
128
      fbc = NULL;
5343
128
    }
5344
1.59k
  }
5345
5346
30.2k
  if (zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast))) {
5347
178
    if (short_circuiting_checkpoint != zend_short_circuiting_checkpoint()) {
5348
3
      zend_error_noreturn(E_COMPILE_ERROR,
5349
3
        "Cannot combine nullsafe operator with Closure creation");
5350
3
    }
5351
178
  }
5352
30.2k
}
5353
/* }}} */
5354
5355
static bool zend_is_constructor(const zend_string *name) /* {{{ */
5356
61.6k
{
5357
61.6k
  return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
5358
61.6k
}
5359
/* }}} */
5360
5361
static bool is_func_accessible(const zend_function *fbc)
5362
18.2k
{
5363
18.2k
  if ((fbc->common.fn_flags & ZEND_ACC_PUBLIC) || fbc->common.scope == CG(active_class_entry)) {
5364
18.1k
    return true;
5365
18.1k
  }
5366
5367
173
  if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE)
5368
81
    && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED)
5369
81
    && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED))
5370
10
    && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) {
5371
0
    return true;
5372
0
  }
5373
5374
173
  return false;
5375
173
}
5376
5377
static const zend_function *zend_get_compatible_func_or_null(const zend_class_entry *ce, zend_string *lcname) /* {{{ */
5378
1.87k
{
5379
1.87k
  const zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname);
5380
5381
1.87k
  if (!fbc || is_func_accessible(fbc)) {
5382
1.70k
    return fbc;
5383
1.70k
  }
5384
5385
171
  return NULL;
5386
1.87k
}
5387
/* }}} */
5388
5389
static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5390
63.2k
{
5391
63.2k
  zend_ast *class_ast = ast->child[0];
5392
63.2k
  zend_ast *method_ast = ast->child[1];
5393
63.2k
  zend_ast *args_ast = ast->child[2];
5394
5395
63.2k
  znode class_node, method_node;
5396
63.2k
  zend_op *opline;
5397
63.2k
  const zend_function *fbc = NULL;
5398
5399
63.2k
  if (zend_compile_parent_property_hook_call(result, ast, type)) {
5400
540
    return;
5401
540
  }
5402
5403
62.7k
  zend_short_circuiting_mark_inner(class_ast);
5404
62.7k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5405
5406
62.7k
  zend_compile_expr(&method_node, method_ast);
5407
5408
62.7k
  if (method_node.op_type == IS_CONST) {
5409
61.2k
    zval *name = &method_node.u.constant;
5410
61.2k
    if (Z_TYPE_P(name) != IS_STRING) {
5411
1
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5412
1
    }
5413
61.2k
    if (zend_is_constructor(Z_STR_P(name))) {
5414
143
      zval_ptr_dtor(name);
5415
143
      method_node.op_type = IS_UNUSED;
5416
143
    }
5417
61.2k
  }
5418
5419
62.7k
  opline = get_next_op();
5420
62.7k
  opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
5421
5422
62.7k
  zend_set_class_name_op1(opline, &class_node);
5423
5424
62.7k
  if (method_node.op_type == IS_CONST) {
5425
61.1k
    opline->op2_type = IS_CONST;
5426
61.1k
    opline->op2.constant = zend_add_func_name_literal(
5427
61.1k
      Z_STR(method_node.u.constant));
5428
61.1k
    opline->result.num = zend_alloc_cache_slots(2);
5429
61.1k
  } else {
5430
1.59k
    if (opline->op1_type == IS_CONST) {
5431
340
      opline->result.num = zend_alloc_cache_slot();
5432
340
    }
5433
1.59k
    SET_NODE(opline->op2, &method_node);
5434
1.59k
  }
5435
5436
  /* Check if we already know which method we're calling */
5437
62.7k
  if (opline->op2_type == IS_CONST) {
5438
61.1k
    zend_class_entry *ce = NULL;
5439
61.1k
    if (opline->op1_type == IS_CONST) {
5440
25.3k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5441
25.3k
      ce = zend_hash_find_ptr(CG(class_table), lcname);
5442
25.3k
      if (ce) {
5443
1.68k
        if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5444
0
          ce = NULL;
5445
0
        }
5446
23.7k
      } else if (CG(active_class_entry)
5447
511
          && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5448
116
        ce = CG(active_class_entry);
5449
116
      }
5450
35.7k
    } else if (opline->op1_type == IS_UNUSED
5451
30.5k
        && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5452
30.3k
        && zend_is_scope_known()) {
5453
77
      ce = CG(active_class_entry);
5454
77
    }
5455
61.1k
    if (ce) {
5456
1.87k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5457
1.87k
      fbc = zend_get_compatible_func_or_null(ce, lcname);
5458
1.87k
    }
5459
61.1k
  }
5460
5461
62.7k
  zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast));
5462
62.7k
}
5463
/* }}} */
5464
5465
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel);
5466
5467
static void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
5468
58.1k
{
5469
58.1k
  zend_ast *class_ast = ast->child[0];
5470
58.1k
  zend_ast *args_ast = ast->child[1];
5471
5472
58.1k
  znode class_node, ctor_result;
5473
58.1k
  zend_op *opline;
5474
5475
58.1k
  if (class_ast->kind == ZEND_AST_CLASS) {
5476
    /* anon class declaration */
5477
1.39k
    zend_compile_class_decl(&class_node, class_ast, false);
5478
56.7k
  } else {
5479
56.7k
    zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5480
56.7k
  }
5481
5482
58.1k
  opline = zend_emit_op(result, ZEND_NEW, NULL, NULL);
5483
5484
58.1k
  zend_set_class_name_op1(opline, &class_node);
5485
5486
58.1k
  if (opline->op1_type == IS_CONST) {
5487
55.6k
    opline->op2.num = zend_alloc_cache_slot();
5488
55.6k
  }
5489
5490
58.1k
  zend_class_entry *ce = NULL;
5491
58.1k
  if (opline->op1_type == IS_CONST) {
5492
55.6k
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5493
55.6k
    ce = zend_hash_find_ptr(CG(class_table), lcname);
5494
55.6k
    if (ce) {
5495
19.4k
      if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5496
0
        ce = NULL;
5497
0
      }
5498
36.1k
    } else if (CG(active_class_entry)
5499
547
        && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5500
254
      ce = CG(active_class_entry);
5501
254
    }
5502
55.6k
  } else if (opline->op1_type == IS_UNUSED
5503
58
      && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5504
34
      && zend_is_scope_known()) {
5505
14
    ce = CG(active_class_entry);
5506
14
  }
5507
5508
5509
58.1k
  const zend_function *fbc = NULL;
5510
58.1k
  if (ce
5511
19.7k
      && ce->default_object_handlers->get_constructor == zend_std_get_constructor
5512
19.7k
      && ce->constructor
5513
16.8k
      && is_func_accessible(ce->constructor)) {
5514
16.8k
    fbc = ce->constructor;
5515
16.8k
  }
5516
5517
58.1k
  zend_compile_call_common(&ctor_result, args_ast, fbc, ast->lineno);
5518
58.1k
  zend_do_free(&ctor_result);
5519
58.1k
}
5520
/* }}} */
5521
5522
static void zend_compile_global_var(zend_ast *ast) /* {{{ */
5523
1.60k
{
5524
1.60k
  zend_ast *var_ast = ast->child[0];
5525
1.60k
  zend_ast *name_ast = var_ast->child[0];
5526
5527
1.60k
  znode name_node, result;
5528
5529
1.60k
  zend_compile_expr(&name_node, name_ast);
5530
1.60k
  if (name_node.op_type == IS_CONST) {
5531
1.17k
    convert_to_string(&name_node.u.constant);
5532
1.17k
  }
5533
5534
  // TODO(GLOBALS) Forbid "global $GLOBALS"?
5535
1.60k
  if (is_this_fetch(var_ast)) {
5536
2
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable");
5537
1.60k
  } else if (zend_try_compile_cv(&result, var_ast, BP_VAR_R) == SUCCESS) {
5538
1.01k
    zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
5539
1.01k
    opline->extended_value = zend_alloc_cache_slot();
5540
1.01k
  } else {
5541
    /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W
5542
     * to not free the name_node operand, so it can be reused in the following
5543
     * ASSIGN_REF, which then frees it. */
5544
585
    zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
5545
585
    opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
5546
5547
585
    if (name_node.op_type == IS_CONST) {
5548
157
      zend_string_addref(Z_STR(name_node.u.constant));
5549
157
    }
5550
5551
585
    zend_emit_assign_ref_znode(
5552
585
      zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)),
5553
585
      &result
5554
585
    );
5555
585
  }
5556
1.60k
}
5557
/* }}} */
5558
5559
static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */
5560
68.7k
{
5561
68.7k
  zend_op *opline;
5562
68.7k
  if (!CG(active_op_array)->static_variables) {
5563
0
    if (CG(active_op_array)->scope) {
5564
0
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5565
0
    }
5566
0
    CG(active_op_array)->static_variables = zend_new_array(8);
5567
0
  }
5568
5569
68.7k
  value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value);
5570
5571
68.7k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5572
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5573
0
  }
5574
5575
68.7k
  opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL);
5576
68.7k
  opline->op1_type = IS_CV;
5577
68.7k
  opline->op1.var = lookup_cv(var_name);
5578
68.7k
  opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode;
5579
68.7k
}
5580
/* }}} */
5581
5582
static void zend_compile_static_var(zend_ast *ast) /* {{{ */
5583
1.12k
{
5584
1.12k
  zend_ast *var_ast = ast->child[0];
5585
1.12k
  zend_string *var_name = zend_ast_get_str(var_ast);
5586
5587
1.12k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5588
2
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5589
2
  }
5590
5591
1.12k
  if (!CG(active_op_array)->static_variables) {
5592
620
    if (CG(active_op_array)->scope) {
5593
174
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5594
174
    }
5595
620
    CG(active_op_array)->static_variables = zend_new_array(8);
5596
620
  }
5597
5598
1.12k
  if (zend_hash_exists(CG(active_op_array)->static_variables, var_name)) {
5599
12
    zend_error_noreturn_unchecked(E_COMPILE_ERROR, "Duplicate declaration of static variable $%S", var_name);
5600
12
  }
5601
5602
1.11k
  zend_eval_const_expr(&ast->child[1]);
5603
1.11k
  zend_ast *value_ast = ast->child[1];
5604
5605
1.11k
  if (!value_ast || value_ast->kind == ZEND_AST_ZVAL) {
5606
981
    zval *value_zv = value_ast
5607
981
      ? zend_ast_get_zval(value_ast)
5608
981
      : &EG(uninitialized_zval);
5609
981
    Z_TRY_ADDREF_P(value_zv);
5610
981
    zend_compile_static_var_common(var_name, value_zv, ZEND_BIND_REF);
5611
981
  } else {
5612
133
    zend_op *opline;
5613
5614
133
    zval *placeholder_ptr = zend_hash_update(CG(active_op_array)->static_variables, var_name, &EG(uninitialized_zval));
5615
133
    uint32_t placeholder_offset = (uint32_t)((char*)placeholder_ptr - (char*)CG(active_op_array)->static_variables->arData);
5616
5617
133
    uint32_t static_def_jmp_opnum = get_next_op_number();
5618
133
    opline = zend_emit_op(NULL, ZEND_BIND_INIT_STATIC_OR_JMP, NULL, NULL);
5619
133
    opline->op1_type = IS_CV;
5620
133
    opline->op1.var = lookup_cv(var_name);
5621
133
    opline->extended_value = placeholder_offset;
5622
5623
133
    znode expr;
5624
133
    zend_compile_expr(&expr, value_ast);
5625
5626
133
    opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, &expr);
5627
133
    opline->op1_type = IS_CV;
5628
133
    opline->op1.var = lookup_cv(var_name);
5629
133
    opline->extended_value = placeholder_offset | ZEND_BIND_REF;
5630
5631
133
    zend_update_jump_target_to_next(static_def_jmp_opnum);
5632
133
  }
5633
1.11k
}
5634
/* }}} */
5635
5636
static void zend_compile_unset(const zend_ast *ast) /* {{{ */
5637
5.04k
{
5638
5.04k
  zend_ast *var_ast = ast->child[0];
5639
5.04k
  znode var_node;
5640
5.04k
  zend_op *opline;
5641
5642
5.04k
  zend_ensure_writable_variable(var_ast);
5643
5644
5.04k
  if (is_global_var_fetch(var_ast)) {
5645
645
    if (!var_ast->child[1]) {
5646
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
5647
2
    }
5648
5649
643
    zend_compile_expr(&var_node, var_ast->child[1]);
5650
643
    if (var_node.op_type == IS_CONST) {
5651
447
      convert_to_string(&var_node.u.constant);
5652
447
    }
5653
5654
643
    opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL);
5655
643
    opline->extended_value = ZEND_FETCH_GLOBAL;
5656
643
    return;
5657
645
  }
5658
5659
4.40k
  switch (var_ast->kind) {
5660
2.09k
    case ZEND_AST_VAR:
5661
2.09k
      if (is_this_fetch(var_ast)) {
5662
2
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this");
5663
2.09k
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_UNSET) == SUCCESS) {
5664
1.76k
        opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL);
5665
1.76k
      } else {
5666
333
        opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, false);
5667
333
        opline->opcode = ZEND_UNSET_VAR;
5668
333
      }
5669
2.09k
      return;
5670
2.09k
    case ZEND_AST_DIM:
5671
1.45k
      opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET, /* by_ref */ false);
5672
1.45k
      opline->opcode = ZEND_UNSET_DIM;
5673
1.45k
      return;
5674
841
    case ZEND_AST_PROP:
5675
841
    case ZEND_AST_NULLSAFE_PROP:
5676
841
      opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, false);
5677
841
      opline->opcode = ZEND_UNSET_OBJ;
5678
841
      return;
5679
11
    case ZEND_AST_STATIC_PROP:
5680
11
      opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, false, false);
5681
11
      opline->opcode = ZEND_UNSET_STATIC_PROP;
5682
11
      return;
5683
4.40k
    EMPTY_SWITCH_DEFAULT_CASE()
5684
4.40k
  }
5685
4.40k
}
5686
/* }}} */
5687
5688
static bool zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */
5689
34.4k
{
5690
34.4k
  const zend_loop_var *base;
5691
34.4k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5692
5693
34.4k
  if (!loop_var) {
5694
549
    return 1;
5695
549
  }
5696
33.8k
  base = zend_stack_base(&CG(loop_var_stack));
5697
63.2k
  for (; loop_var >= base; loop_var--) {
5698
61.3k
    if (loop_var->opcode == ZEND_FAST_CALL) {
5699
3.22k
      zend_op *opline = get_next_op();
5700
5701
3.22k
      opline->opcode = ZEND_FAST_CALL;
5702
3.22k
      opline->result_type = IS_TMP_VAR;
5703
3.22k
      opline->result.var = loop_var->var_num;
5704
3.22k
      if (return_value) {
5705
2.05k
        SET_NODE(opline->op2, return_value);
5706
2.05k
      }
5707
3.22k
      opline->op1.num = loop_var->try_catch_offset;
5708
58.1k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5709
24.6k
      zend_op *opline = get_next_op();
5710
24.6k
      opline->opcode = ZEND_DISCARD_EXCEPTION;
5711
24.6k
      opline->op1_type = IS_TMP_VAR;
5712
24.6k
      opline->op1.var = loop_var->var_num;
5713
33.5k
    } else if (loop_var->opcode == ZEND_RETURN) {
5714
      /* Stack separator */
5715
30.9k
      break;
5716
30.9k
    } else if (depth <= 1) {
5717
1.01k
      return 1;
5718
1.53k
    } else if (loop_var->opcode == ZEND_NOP) {
5719
      /* Loop doesn't have freeable variable */
5720
1.16k
      depth--;
5721
1.16k
    } else {
5722
364
      zend_op *opline;
5723
5724
364
      ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR));
5725
364
      opline = get_next_op();
5726
364
      opline->opcode = loop_var->opcode;
5727
364
      opline->op1_type = loop_var->var_type;
5728
364
      opline->op1.var = loop_var->var_num;
5729
364
      opline->extended_value = ZEND_FREE_ON_RETURN;
5730
364
      depth--;
5731
364
      }
5732
61.3k
  }
5733
32.8k
  return (depth == 0);
5734
33.8k
}
5735
/* }}} */
5736
5737
static bool zend_handle_loops_and_finally(znode *return_value) /* {{{ */
5738
33.3k
{
5739
33.3k
  return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value);
5740
33.3k
}
5741
/* }}} */
5742
5743
static bool zend_has_finally_ex(zend_long depth) /* {{{ */
5744
2.50k
{
5745
2.50k
  const zend_loop_var *base;
5746
2.50k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5747
5748
2.50k
  if (!loop_var) {
5749
269
    return 0;
5750
269
  }
5751
2.23k
  base = zend_stack_base(&CG(loop_var_stack));
5752
4.51k
  for (; loop_var >= base; loop_var--) {
5753
4.35k
    if (loop_var->opcode == ZEND_FAST_CALL) {
5754
762
      return 1;
5755
3.59k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5756
1.81k
    } else if (loop_var->opcode == ZEND_RETURN) {
5757
      /* Stack separator */
5758
1.30k
      return 0;
5759
1.30k
    } else if (depth <= 1) {
5760
0
      return 0;
5761
508
    } else {
5762
508
      depth--;
5763
508
      }
5764
4.35k
  }
5765
164
  return 0;
5766
2.23k
}
5767
/* }}} */
5768
5769
static bool zend_has_finally(void) /* {{{ */
5770
2.50k
{
5771
2.50k
  return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1);
5772
2.50k
}
5773
/* }}} */
5774
5775
static void zend_compile_return(const zend_ast *ast) /* {{{ */
5776
32.1k
{
5777
32.1k
  zend_ast *expr_ast = ast->child[0];
5778
32.1k
  bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0;
5779
32.1k
  bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
5780
5781
32.1k
  znode expr_node;
5782
32.1k
  zend_op *opline;
5783
5784
32.1k
  if (is_generator) {
5785
    /* For generators the by-ref flag refers to yields, not returns */
5786
3.61k
    by_ref = false;
5787
3.61k
  }
5788
5789
32.1k
  if (!expr_ast) {
5790
491
    expr_node.op_type = IS_CONST;
5791
491
    ZVAL_NULL(&expr_node.u.constant);
5792
31.6k
  } else if (by_ref && zend_is_variable(expr_ast)) {
5793
1.65k
    zend_assert_not_short_circuited(expr_ast);
5794
1.65k
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
5795
30.0k
  } else {
5796
30.0k
    zend_compile_expr(&expr_node, expr_ast);
5797
30.0k
  }
5798
5799
32.1k
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
5800
2.89k
   && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
5801
2.50k
   && zend_has_finally()) {
5802
    /* Copy return value into temporary VAR to avoid modification in finally code */
5803
762
    if (by_ref) {
5804
293
      zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
5805
469
    } else {
5806
469
      zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL);
5807
469
    }
5808
762
  }
5809
5810
  /* Generator return types are handled separately */
5811
32.1k
  if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
5812
2.84k
    zend_emit_return_type_check(
5813
2.84k
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
5814
2.84k
  }
5815
5816
32.1k
  uint32_t opnum_before_finally = get_next_op_number();
5817
5818
32.1k
  zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
5819
5820
  /* Content of reference might have changed in finally, repeat type check. */
5821
32.1k
  if (by_ref
5822
   /* Check if any opcodes were emitted since the last return type check. */
5823
3.79k
   && opnum_before_finally != get_next_op_number()
5824
1.84k
   && !is_generator
5825
1.84k
   && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
5826
3
    zend_emit_return_type_check(
5827
3
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
5828
3
  }
5829
5830
32.1k
  opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
5831
32.1k
    &expr_node, NULL);
5832
5833
32.1k
  if (by_ref && expr_ast) {
5834
3.43k
    if (zend_is_call(expr_ast)) {
5835
714
      opline->extended_value = ZEND_RETURNS_FUNCTION;
5836
2.72k
    } else if (!zend_is_variable(expr_ast) || zend_ast_is_short_circuited(expr_ast)) {
5837
1.07k
      opline->extended_value = ZEND_RETURNS_VALUE;
5838
1.07k
    }
5839
3.43k
  }
5840
32.1k
}
5841
/* }}} */
5842
5843
static void zend_compile_void_cast(znode *result, const zend_ast *ast)
5844
903
{
5845
903
  zend_ast *expr_ast = ast->child[0];
5846
903
  znode expr_node;
5847
903
  zend_op *opline;
5848
5849
903
  zend_compile_expr(&expr_node, expr_ast);
5850
5851
903
  switch (expr_node.op_type) {
5852
128
    case IS_TMP_VAR:
5853
262
    case IS_VAR:
5854
262
      opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
5855
262
      opline->extended_value = ZEND_FREE_VOID_CAST;
5856
262
      break;
5857
629
    case IS_CONST:
5858
629
      zend_do_free(&expr_node);
5859
629
      break;
5860
903
  }
5861
903
}
5862
5863
static void zend_compile_echo(const zend_ast *ast) /* {{{ */
5864
1.93M
{
5865
1.93M
  zend_op *opline;
5866
1.93M
  zend_ast *expr_ast = ast->child[0];
5867
5868
1.93M
  znode expr_node;
5869
1.93M
  zend_compile_expr(&expr_node, expr_ast);
5870
5871
1.93M
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
5872
1.93M
  opline->extended_value = 0;
5873
1.93M
}
5874
/* }}} */
5875
5876
static void zend_compile_throw(znode *result, const zend_ast *ast) /* {{{ */
5877
1.28k
{
5878
1.28k
  zend_ast *expr_ast = ast->child[0];
5879
5880
1.28k
  znode expr_node;
5881
1.28k
  zend_compile_expr(&expr_node, expr_ast);
5882
5883
1.28k
  zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
5884
1.28k
  if (result) {
5885
    /* Mark this as an "expression throw" for opcache. */
5886
664
    opline->extended_value = ZEND_THROW_IS_EXPR;
5887
664
    result->op_type = IS_CONST;
5888
664
    ZVAL_TRUE(&result->u.constant);
5889
664
  }
5890
1.28k
}
5891
/* }}} */
5892
5893
static void zend_compile_break_continue(const zend_ast *ast) /* {{{ */
5894
1.16k
{
5895
1.16k
  zend_ast *depth_ast = ast->child[0];
5896
5897
1.16k
  zend_op *opline;
5898
1.16k
  zend_long depth;
5899
5900
1.16k
  ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
5901
5902
1.16k
  if (depth_ast) {
5903
332
    const zval *depth_zv;
5904
332
    if (depth_ast->kind != ZEND_AST_ZVAL) {
5905
20
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand "
5906
20
        "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5907
20
    }
5908
5909
312
    depth_zv = zend_ast_get_zval(depth_ast);
5910
312
    if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
5911
9
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers",
5912
9
        ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5913
9
    }
5914
5915
303
    depth = Z_LVAL_P(depth_zv);
5916
830
  } else {
5917
830
    depth = 1;
5918
830
  }
5919
5920
1.13k
  if (CG(context).current_brk_cont == -1) {
5921
35
    zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
5922
35
      ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5923
1.09k
  } else {
5924
1.09k
    if (!zend_handle_loops_and_finally_ex(depth, NULL)) {
5925
86
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s",
5926
86
        ast->kind == ZEND_AST_BREAK ? "break" : "continue",
5927
86
        depth, depth == 1 ? "" : "s");
5928
86
    }
5929
1.09k
  }
5930
5931
1.01k
  if (ast->kind == ZEND_AST_CONTINUE) {
5932
760
    int d, cur = CG(context).current_brk_cont;
5933
864
    for (d = depth - 1; d > 0; d--) {
5934
104
      cur = CG(context).brk_cont_array[cur].parent;
5935
104
      ZEND_ASSERT(cur != -1);
5936
104
    }
5937
5938
760
    if (CG(context).brk_cont_array[cur].is_switch) {
5939
185
      if (depth == 1) {
5940
90
        if (CG(context).brk_cont_array[cur].parent == -1) {
5941
87
          zend_error(E_WARNING,
5942
87
            "\"continue\" targeting switch is equivalent to \"break\"");
5943
87
        } else {
5944
3
          zend_error(E_WARNING,
5945
3
            "\"continue\" targeting switch is equivalent to \"break\". " \
5946
3
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
5947
3
            depth + 1);
5948
3
        }
5949
95
      } else {
5950
95
        if (CG(context).brk_cont_array[cur].parent == -1) {
5951
54
          zend_error(E_WARNING,
5952
54
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"",
5953
54
            depth, depth);
5954
54
        } else {
5955
41
          zend_error(E_WARNING,
5956
41
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
5957
41
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
5958
41
            depth, depth, depth + 1);
5959
41
        }
5960
95
      }
5961
185
    }
5962
760
  }
5963
5964
1.01k
  opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
5965
1.01k
  opline->op1.num = CG(context).current_brk_cont;
5966
1.01k
  opline->op2.num = depth;
5967
1.01k
}
5968
/* }}} */
5969
5970
void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */
5971
919
{
5972
919
  zend_label *dest;
5973
919
  int remove_oplines = opline->op1.num;
5974
919
  zval *label;
5975
919
  uint32_t opnum = opline - op_array->opcodes;
5976
5977
919
  label = CT_CONSTANT_EX(op_array, opline->op2.constant);
5978
919
  if (CG(context).labels == NULL ||
5979
911
      (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL
5980
919
  ) {
5981
37
    CG(in_compilation) = 1;
5982
37
    CG(active_op_array) = op_array;
5983
37
    CG(zend_lineno) = opline->lineno;
5984
37
    zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
5985
37
  }
5986
5987
882
  zval_ptr_dtor_str(label);
5988
882
  ZVAL_NULL(label);
5989
5990
882
  uint32_t current = opline->extended_value;
5991
1.38k
  for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) {
5992
508
    if (current == -1) {
5993
2
      CG(in_compilation) = 1;
5994
2
      CG(active_op_array) = op_array;
5995
2
      CG(zend_lineno) = opline->lineno;
5996
2
      zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
5997
2
    }
5998
506
    if (CG(context).brk_cont_array[current].start >= 0) {
5999
265
      remove_oplines--;
6000
265
    }
6001
506
  }
6002
6003
2.14k
  for (current = 0; current < op_array->last_try_catch; ++current) {
6004
1.50k
    const zend_try_catch_element *elem = &op_array->try_catch_array[current];
6005
1.50k
    if (elem->try_op > opnum) {
6006
241
      break;
6007
241
    }
6008
1.26k
    if (elem->finally_op && opnum < elem->finally_op - 1
6009
741
      && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op)
6010
1.26k
    ) {
6011
377
      remove_oplines--;
6012
377
    }
6013
1.26k
  }
6014
6015
880
  opline->opcode = ZEND_JMP;
6016
880
  SET_UNUSED(opline->op1);
6017
880
  SET_UNUSED(opline->op2);
6018
880
  SET_UNUSED(opline->result);
6019
880
  opline->op1.opline_num = dest->opline_num;
6020
880
  opline->extended_value = 0;
6021
6022
880
  ZEND_ASSERT(remove_oplines >= 0);
6023
1.26k
  while (remove_oplines--) {
6024
381
    opline--;
6025
381
    MAKE_NOP(opline);
6026
381
    ZEND_VM_SET_OPCODE_HANDLER(opline);
6027
381
  }
6028
880
}
6029
/* }}} */
6030
6031
static void zend_compile_goto(const zend_ast *ast) /* {{{ */
6032
1.26k
{
6033
1.26k
  zend_ast *label_ast = ast->child[0];
6034
1.26k
  znode label_node;
6035
1.26k
  zend_op *opline;
6036
6037
1.26k
  zend_compile_expr(&label_node, label_ast);
6038
6039
  /* Label resolution and unwinding adjustments happen in pass two. */
6040
1.26k
  uint32_t opnum_start = get_next_op_number();
6041
1.26k
  zend_handle_loops_and_finally(NULL);
6042
1.26k
  opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
6043
1.26k
  opline->op1.num = get_next_op_number() - opnum_start - 1;
6044
1.26k
  opline->extended_value = CG(context).current_brk_cont;
6045
1.26k
}
6046
/* }}} */
6047
6048
static void zend_compile_label(const zend_ast *ast) /* {{{ */
6049
4.91k
{
6050
4.91k
  zend_string *label = zend_ast_get_str(ast->child[0]);
6051
4.91k
  zend_label dest;
6052
6053
4.91k
  if (!CG(context).labels) {
6054
3.92k
    ALLOC_HASHTABLE(CG(context).labels);
6055
3.92k
    zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0);
6056
3.92k
  }
6057
6058
4.91k
  dest.brk_cont = CG(context).current_brk_cont;
6059
4.91k
  dest.opline_num = get_next_op_number();
6060
6061
4.91k
  if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
6062
55
    zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
6063
55
  }
6064
4.91k
}
6065
/* }}} */
6066
6067
static void zend_compile_while(const zend_ast *ast) /* {{{ */
6068
8.44k
{
6069
8.44k
  zend_ast *cond_ast = ast->child[0];
6070
8.44k
  zend_ast *stmt_ast = ast->child[1];
6071
8.44k
  znode cond_node;
6072
8.44k
  uint32_t opnum_start, opnum_jmp, opnum_cond;
6073
6074
8.44k
  opnum_jmp = zend_emit_jump(0);
6075
6076
8.44k
  zend_begin_loop(ZEND_NOP, NULL, false);
6077
6078
8.44k
  opnum_start = get_next_op_number();
6079
8.44k
  zend_compile_stmt(stmt_ast);
6080
6081
8.44k
  opnum_cond = get_next_op_number();
6082
8.44k
  zend_update_jump_target(opnum_jmp, opnum_cond);
6083
8.44k
  zend_compile_expr(&cond_node, cond_ast);
6084
6085
8.44k
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6086
6087
8.44k
  zend_end_loop(opnum_cond, NULL);
6088
8.44k
}
6089
/* }}} */
6090
6091
static void zend_compile_do_while(const zend_ast *ast) /* {{{ */
6092
569
{
6093
569
  zend_ast *stmt_ast = ast->child[0];
6094
569
  zend_ast *cond_ast = ast->child[1];
6095
6096
569
  znode cond_node;
6097
569
  uint32_t opnum_start, opnum_cond;
6098
6099
569
  zend_begin_loop(ZEND_NOP, NULL, false);
6100
6101
569
  opnum_start = get_next_op_number();
6102
569
  zend_compile_stmt(stmt_ast);
6103
6104
569
  opnum_cond = get_next_op_number();
6105
569
  zend_compile_expr(&cond_node, cond_ast);
6106
6107
569
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6108
6109
569
  zend_end_loop(opnum_cond, NULL);
6110
569
}
6111
/* }}} */
6112
6113
static void zend_compile_for_expr_list(znode *result, zend_ast *ast) /* {{{ */
6114
18.2k
{
6115
18.2k
  const zend_ast_list *list;
6116
18.2k
  uint32_t i;
6117
6118
18.2k
  result->op_type = IS_CONST;
6119
18.2k
  ZVAL_TRUE(&result->u.constant);
6120
6121
18.2k
  if (!ast) {
6122
8.56k
    return;
6123
8.56k
  }
6124
6125
9.67k
  list = zend_ast_get_list(ast);
6126
20.8k
  for (i = 0; i < list->children; ++i) {
6127
11.2k
    zend_ast *expr_ast = list->child[i];
6128
6129
11.2k
    zend_do_free(result);
6130
11.2k
    if (expr_ast->kind == ZEND_AST_CAST_VOID) {
6131
113
      zend_compile_void_cast(NULL, expr_ast);
6132
113
      result->op_type = IS_CONST;
6133
113
      ZVAL_NULL(&result->u.constant);
6134
11.1k
    } else {
6135
11.1k
      zend_compile_expr(result, expr_ast);
6136
11.1k
    }
6137
11.2k
  }
6138
9.67k
}
6139
/* }}} */
6140
6141
static void zend_compile_for(const zend_ast *ast) /* {{{ */
6142
6.14k
{
6143
6.14k
  zend_ast *init_ast = ast->child[0];
6144
6.14k
  zend_ast *cond_ast = ast->child[1];
6145
6.14k
  zend_ast *loop_ast = ast->child[2];
6146
6.14k
  zend_ast *stmt_ast = ast->child[3];
6147
6148
6.14k
  znode result;
6149
6.14k
  uint32_t opnum_start, opnum_jmp, opnum_loop;
6150
6151
6.14k
  zend_compile_for_expr_list(&result, init_ast);
6152
6.14k
  zend_do_free(&result);
6153
6154
6.14k
  opnum_jmp = zend_emit_jump(0);
6155
6156
6.14k
  zend_begin_loop(ZEND_NOP, NULL, false);
6157
6158
6.14k
  opnum_start = get_next_op_number();
6159
6.14k
  zend_compile_stmt(stmt_ast);
6160
6161
6.14k
  opnum_loop = get_next_op_number();
6162
6.14k
  zend_compile_for_expr_list(&result, loop_ast);
6163
6.14k
  zend_do_free(&result);
6164
6165
6.14k
  zend_update_jump_target_to_next(opnum_jmp);
6166
6.14k
  zend_compile_for_expr_list(&result, cond_ast);
6167
6.14k
  zend_do_extended_stmt(NULL);
6168
6169
6.14k
  zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
6170
6171
6.14k
  zend_end_loop(opnum_loop, NULL);
6172
6.14k
}
6173
/* }}} */
6174
6175
static void zend_compile_foreach(zend_ast *ast) /* {{{ */
6176
13.1k
{
6177
13.1k
  zend_ast *expr_ast = ast->child[0];
6178
13.1k
  zend_ast *value_ast = ast->child[1];
6179
13.1k
  zend_ast *key_ast = ast->child[2];
6180
13.1k
  zend_ast *stmt_ast = ast->child[3];
6181
13.1k
  bool by_ref = value_ast->kind == ZEND_AST_REF;
6182
13.1k
  bool is_variable = zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast);
6183
6184
13.1k
  znode expr_node, reset_node, value_node, key_node;
6185
13.1k
  zend_op *opline;
6186
13.1k
  uint32_t opnum_reset, opnum_fetch;
6187
6188
13.1k
  if (key_ast) {
6189
484
    if (key_ast->kind == ZEND_AST_REF) {
6190
5
      zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
6191
5
    }
6192
479
    if (key_ast->kind == ZEND_AST_ARRAY) {
6193
10
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
6194
10
    }
6195
479
  }
6196
6197
13.0k
  if (by_ref) {
6198
457
    value_ast = value_ast->child[0];
6199
457
  }
6200
6201
13.0k
  if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
6202
91
    by_ref = true;
6203
91
  }
6204
6205
13.0k
  if (by_ref && is_variable) {
6206
256
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
6207
12.8k
  } else {
6208
12.8k
    zend_compile_expr(&expr_node, expr_ast);
6209
12.8k
  }
6210
6211
13.0k
  if (by_ref) {
6212
548
    zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
6213
548
  }
6214
6215
13.0k
  opnum_reset = get_next_op_number();
6216
13.0k
  opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
6217
6218
13.0k
  zend_begin_loop(ZEND_FE_FREE, &reset_node, false);
6219
6220
13.0k
  opnum_fetch = get_next_op_number();
6221
13.0k
  opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
6222
6223
13.0k
  if (is_this_fetch(value_ast)) {
6224
2
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6225
13.0k
  } else if (value_ast->kind == ZEND_AST_VAR &&
6226
12.7k
    zend_try_compile_cv(&value_node, value_ast, BP_VAR_R) == SUCCESS) {
6227
11.8k
    SET_NODE(opline->op2, &value_node);
6228
11.8k
  } else {
6229
1.22k
    opline->op2_type = IS_VAR;
6230
1.22k
    opline->op2.var = get_temporary_variable();
6231
1.22k
    GET_NODE(&value_node, opline->op2);
6232
1.22k
    if (value_ast->kind == ZEND_AST_ARRAY) {
6233
125
      zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr);
6234
1.10k
    } else if (by_ref) {
6235
172
      zend_emit_assign_ref_znode(value_ast, &value_node);
6236
932
    } else {
6237
932
      zend_emit_assign_znode(value_ast, &value_node);
6238
932
    }
6239
1.22k
  }
6240
6241
13.0k
  if (key_ast) {
6242
466
    opline = &CG(active_op_array)->opcodes[opnum_fetch];
6243
466
    zend_make_tmp_result(&key_node, opline);
6244
466
    zend_emit_assign_znode(key_ast, &key_node);
6245
466
  }
6246
6247
13.0k
  zend_compile_stmt(stmt_ast);
6248
6249
  /* Place JMP and FE_FREE on the line where foreach starts. It would be
6250
   * better to use the end line, but this information is not available
6251
   * currently. */
6252
13.0k
  CG(zend_lineno) = ast->lineno;
6253
13.0k
  zend_emit_jump(opnum_fetch);
6254
6255
13.0k
  opline = &CG(active_op_array)->opcodes[opnum_reset];
6256
13.0k
  opline->op2.opline_num = get_next_op_number();
6257
6258
13.0k
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
6259
13.0k
  opline->extended_value = get_next_op_number();
6260
6261
13.0k
  zend_end_loop(opnum_fetch, &reset_node);
6262
6263
13.0k
  opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
6264
13.0k
}
6265
/* }}} */
6266
6267
static void zend_compile_if(zend_ast *ast) /* {{{ */
6268
17.8k
{
6269
17.8k
  const zend_ast_list *list = zend_ast_get_list(ast);
6270
17.8k
  uint32_t i;
6271
17.8k
  uint32_t *jmp_opnums = NULL;
6272
6273
17.8k
  if (list->children > 1) {
6274
7.62k
    jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
6275
7.62k
  }
6276
6277
47.0k
  for (i = 0; i < list->children; ++i) {
6278
29.1k
    const zend_ast *elem_ast = list->child[i];
6279
29.1k
    zend_ast *cond_ast = elem_ast->child[0];
6280
29.1k
    zend_ast *stmt_ast = elem_ast->child[1];
6281
6282
29.1k
    if (cond_ast) {
6283
22.0k
      znode cond_node;
6284
22.0k
      uint32_t opnum_jmpz;
6285
6286
22.0k
      if (i > 0) {
6287
4.22k
        CG(zend_lineno) = cond_ast->lineno;
6288
4.22k
        zend_do_extended_stmt(NULL);
6289
4.22k
      }
6290
6291
22.0k
      zend_compile_expr(&cond_node, cond_ast);
6292
22.0k
      opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6293
6294
22.0k
      zend_compile_stmt(stmt_ast);
6295
6296
22.0k
      if (i != list->children - 1) {
6297
        /* Set the lineno of JMP to the position of the if keyword, as we don't want to
6298
         * report the last line in the if branch as covered if it hasn't actually executed. */
6299
11.3k
        CG(zend_lineno) = elem_ast->lineno;
6300
11.3k
        jmp_opnums[i] = zend_emit_jump(0);
6301
11.3k
      }
6302
22.0k
      zend_update_jump_target_to_next(opnum_jmpz);
6303
22.0k
    } else {
6304
      /* "else" can only occur as last element. */
6305
7.08k
      ZEND_ASSERT(i == list->children - 1);
6306
7.08k
      zend_compile_stmt(stmt_ast);
6307
7.08k
    }
6308
29.1k
  }
6309
6310
17.8k
  if (list->children > 1) {
6311
18.8k
    for (i = 0; i < list->children - 1; ++i) {
6312
11.2k
      zend_update_jump_target_to_next(jmp_opnums[i]);
6313
11.2k
    }
6314
7.60k
    efree(jmp_opnums);
6315
7.60k
  }
6316
17.8k
}
6317
/* }}} */
6318
6319
25.9k
static uint8_t determine_switch_jumptable_type(const zend_ast_list *cases) {
6320
25.9k
  uint32_t i;
6321
25.9k
  uint8_t common_type = IS_UNDEF;
6322
75.9k
  for (i = 0; i < cases->children; i++) {
6323
71.5k
    zend_ast *case_ast = cases->child[i];
6324
71.5k
    zend_ast **cond_ast = &case_ast->child[0];
6325
71.5k
    const zval *cond_zv;
6326
71.5k
    if (!case_ast->child[0]) {
6327
      /* Skip default clause */
6328
289
      continue;
6329
289
    }
6330
6331
71.2k
    zend_eval_const_expr(cond_ast);
6332
71.2k
    if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6333
      /* Non-constant case */
6334
1.04k
      return IS_UNDEF;
6335
1.04k
    }
6336
6337
70.2k
    cond_zv = zend_ast_get_zval(case_ast->child[0]);
6338
70.2k
    if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6339
      /* We only optimize switched on integers and strings */
6340
20.1k
      return IS_UNDEF;
6341
20.1k
    }
6342
6343
50.0k
    if (common_type == IS_UNDEF) {
6344
24.7k
      common_type = Z_TYPE_P(cond_zv);
6345
25.2k
    } else if (common_type != Z_TYPE_P(cond_zv)) {
6346
      /* Non-uniform case types */
6347
259
      return IS_UNDEF;
6348
259
    }
6349
6350
49.8k
    if (Z_TYPE_P(cond_zv) == IS_STRING
6351
47.1k
        && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) {
6352
      /* Numeric strings cannot be compared with a simple hash lookup */
6353
104
      return IS_UNDEF;
6354
104
    }
6355
49.8k
  }
6356
6357
4.40k
  return common_type;
6358
25.9k
}
6359
6360
4.02k
static bool should_use_jumptable(const zend_ast_list *cases, uint8_t jumptable_type) {
6361
4.02k
  if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) {
6362
0
    return 0;
6363
0
  }
6364
6365
  /* Thresholds are chosen based on when the average switch time for equidistributed
6366
   * input becomes smaller when using the jumptable optimization. */
6367
4.02k
  if (jumptable_type == IS_LONG) {
6368
347
    return cases->children >= 5;
6369
3.67k
  } else {
6370
3.67k
    ZEND_ASSERT(jumptable_type == IS_STRING);
6371
3.67k
    return cases->children >= 2;
6372
3.67k
  }
6373
4.02k
}
6374
6375
static void zend_compile_switch(zend_ast *ast) /* {{{ */
6376
25.9k
{
6377
25.9k
  zend_ast *expr_ast = ast->child[0];
6378
25.9k
  zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
6379
6380
25.9k
  uint32_t i;
6381
25.9k
  bool has_default_case = false;
6382
6383
25.9k
  znode expr_node, case_node;
6384
25.9k
  zend_op *opline;
6385
25.9k
  uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1;
6386
25.9k
  uint8_t jumptable_type;
6387
25.9k
  HashTable *jumptable = NULL;
6388
6389
25.9k
  zend_compile_expr(&expr_node, expr_ast);
6390
6391
25.9k
  zend_begin_loop(ZEND_FREE, &expr_node, true);
6392
6393
25.9k
  case_node.op_type = IS_TMP_VAR;
6394
25.9k
  case_node.u.op.var = get_temporary_variable();
6395
6396
25.9k
  jumptable_type = determine_switch_jumptable_type(cases);
6397
25.9k
  if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) {
6398
3.14k
    znode jumptable_op;
6399
6400
3.14k
    ALLOC_HASHTABLE(jumptable);
6401
3.14k
    zend_hash_init(jumptable, cases->children, NULL, NULL, 0);
6402
3.14k
    jumptable_op.op_type = IS_CONST;
6403
3.14k
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6404
6405
3.14k
    opline = zend_emit_op(NULL,
6406
3.14k
      jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING,
6407
3.14k
      &expr_node, &jumptable_op);
6408
3.14k
    if (opline->op1_type == IS_CONST) {
6409
2.67k
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6410
2.67k
    }
6411
3.14k
    opnum_switch = opline - CG(active_op_array)->opcodes;
6412
3.14k
  }
6413
6414
25.9k
  jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
6415
101k
  for (i = 0; i < cases->children; ++i) {
6416
75.6k
    zend_ast *case_ast = cases->child[i];
6417
75.6k
    zend_ast *cond_ast = case_ast->child[0];
6418
75.6k
    znode cond_node;
6419
6420
75.6k
    if (case_ast->attr == ZEND_ALT_CASE_SYNTAX) {
6421
778
      CG(zend_lineno) = case_ast->lineno;
6422
778
      zend_error(E_DEPRECATED, "Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead");
6423
778
    }
6424
6425
75.6k
    if (!cond_ast) {
6426
286
      if (has_default_case) {
6427
4
        CG(zend_lineno) = case_ast->lineno;
6428
4
        zend_error_noreturn(E_COMPILE_ERROR,
6429
4
          "Switch statements may only contain one default clause");
6430
4
      }
6431
282
      has_default_case = true;
6432
282
      continue;
6433
286
    }
6434
6435
75.3k
    zend_compile_expr(&cond_node, cond_ast);
6436
6437
75.3k
    if (expr_node.op_type == IS_CONST
6438
42.9k
      && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
6439
3.96k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6440
71.4k
    } else if (expr_node.op_type == IS_CONST
6441
38.9k
      && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
6442
1.51k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
6443
69.9k
    } else {
6444
69.9k
      opline = zend_emit_op(NULL,
6445
69.9k
        (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL,
6446
69.9k
        &expr_node, &cond_node);
6447
69.9k
      SET_NODE(opline->result, &case_node);
6448
69.9k
      if (opline->op1_type == IS_CONST) {
6449
37.4k
        Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6450
37.4k
      }
6451
6452
69.9k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6453
69.9k
    }
6454
75.3k
  }
6455
6456
25.9k
  opnum_default_jmp = zend_emit_jump(0);
6457
6458
100k
  for (i = 0; i < cases->children; ++i) {
6459
74.5k
    zend_ast *case_ast = cases->child[i];
6460
74.5k
    zend_ast *cond_ast = case_ast->child[0];
6461
74.5k
    zend_ast *stmt_ast = case_ast->child[1];
6462
6463
74.5k
    if (cond_ast) {
6464
74.2k
      zend_update_jump_target_to_next(jmpnz_opnums[i]);
6465
6466
74.2k
      if (jumptable) {
6467
9.24k
        zval *cond_zv = zend_ast_get_zval(cond_ast);
6468
9.24k
        zval jmp_target;
6469
9.24k
        ZVAL_LONG(&jmp_target, get_next_op_number());
6470
6471
9.24k
        ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type);
6472
9.24k
        if (Z_TYPE_P(cond_zv) == IS_LONG) {
6473
1.71k
          zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6474
7.52k
        } else {
6475
7.52k
          ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6476
7.52k
          zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6477
7.52k
        }
6478
9.24k
      }
6479
74.2k
    } else {
6480
278
      zend_update_jump_target_to_next(opnum_default_jmp);
6481
6482
278
      if (jumptable) {
6483
10
        ZEND_ASSERT(opnum_switch != (uint32_t)-1);
6484
10
        opline = &CG(active_op_array)->opcodes[opnum_switch];
6485
10
        opline->extended_value = get_next_op_number();
6486
10
      }
6487
278
    }
6488
6489
74.5k
    zend_compile_stmt(stmt_ast);
6490
74.5k
  }
6491
6492
25.9k
  if (!has_default_case) {
6493
25.6k
    zend_update_jump_target_to_next(opnum_default_jmp);
6494
6495
25.6k
    if (jumptable) {
6496
3.11k
      opline = &CG(active_op_array)->opcodes[opnum_switch];
6497
3.11k
      opline->extended_value = get_next_op_number();
6498
3.11k
    }
6499
25.6k
  }
6500
6501
25.9k
  zend_end_loop(get_next_op_number(), &expr_node);
6502
6503
25.9k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
6504
11.1k
    opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6505
11.1k
    opline->extended_value = ZEND_FREE_SWITCH;
6506
14.7k
  } else if (expr_node.op_type == IS_CONST) {
6507
14.3k
    zval_ptr_dtor_nogc(&expr_node.u.constant);
6508
14.3k
  }
6509
6510
25.9k
  efree(jmpnz_opnums);
6511
25.9k
}
6512
/* }}} */
6513
6514
static uint32_t count_match_conds(const zend_ast_list *arms)
6515
2.82k
{
6516
2.82k
  uint32_t num_conds = 0;
6517
6518
6.90k
  for (uint32_t i = 0; i < arms->children; i++) {
6519
4.08k
    const zend_ast *arm_ast = arms->child[i];
6520
4.08k
    if (arm_ast->child[0] == NULL) {
6521
624
      continue;
6522
624
    }
6523
6524
3.46k
    const zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6525
3.46k
    num_conds += conds->children;
6526
3.46k
  }
6527
6528
2.82k
  return num_conds;
6529
2.82k
}
6530
6531
2.82k
static bool can_match_use_jumptable(const zend_ast_list *arms) {
6532
5.37k
  for (uint32_t i = 0; i < arms->children; i++) {
6533
3.65k
    const zend_ast *arm_ast = arms->child[i];
6534
3.65k
    if (!arm_ast->child[0]) {
6535
      /* Skip default arm */
6536
498
      continue;
6537
498
    }
6538
6539
3.15k
    zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6540
9.02k
    for (uint32_t j = 0; j < conds->children; j++) {
6541
6.97k
      zend_ast **cond_ast = &conds->child[j];
6542
6543
6.97k
      zend_eval_const_expr(cond_ast);
6544
6.97k
      if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6545
940
        return 0;
6546
940
      }
6547
6548
6.03k
      const zval *cond_zv = zend_ast_get_zval(*cond_ast);
6549
6.03k
      if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6550
163
        return 0;
6551
163
      }
6552
6.03k
    }
6553
3.15k
  }
6554
6555
1.72k
  return 1;
6556
2.82k
}
6557
6558
static bool zend_is_pipe_optimizable_callable_name(zend_ast *ast)
6559
207
{
6560
207
  if (ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(ast)) == IS_STRING) {
6561
    /* Assert compilation adds a message operand, but this is incompatible with the
6562
     * pipe optimization that uses a temporary znode for the reference elimination.
6563
     * Therefore, disable the optimization for assert.
6564
     * Note that "assert" as a name is always treated as fully qualified. */
6565
125
    return !zend_string_equals_literal_ci(zend_ast_get_str(ast), "assert");
6566
125
  }
6567
6568
82
  return true;
6569
207
}
6570
6571
static void zend_compile_pipe(znode *result, zend_ast *ast)
6572
202k
{
6573
202k
  zend_ast *operand_ast = ast->child[0];
6574
202k
  zend_ast *callable_ast = ast->child[1];
6575
6576
202k
  if (callable_ast->kind == ZEND_AST_ARROW_FUNC && !(callable_ast->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) {
6577
2
    zend_error_noreturn(E_COMPILE_ERROR, "Arrow functions on the right hand side of |> must be parenthesized");
6578
2
  }
6579
6580
  /* Compile the left hand side down to a value first. */
6581
202k
  znode operand_result;
6582
202k
  zend_compile_expr(&operand_result, operand_ast);
6583
6584
  /* Wrap simple values in a ZEND_QM_ASSIGN opcode to ensure references
6585
   * always fail. They will already fail in complex cases like arrays,
6586
   * so those don't need a wrapper. */
6587
202k
  znode wrapped_operand_result;
6588
202k
  if (operand_result.op_type & (IS_CV|IS_VAR)) {
6589
159k
    zend_emit_op_tmp(&wrapped_operand_result, ZEND_QM_ASSIGN, &operand_result, NULL);
6590
159k
  } else {
6591
43.1k
    wrapped_operand_result = operand_result;
6592
43.1k
  }
6593
6594
  /* Turn the operand into a function parameter list. */
6595
202k
  zend_ast *arg_list_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&wrapped_operand_result));
6596
6597
202k
  zend_ast *fcall_ast;
6598
202k
  znode callable_result;
6599
6600
  /* Turn $foo |> bar(...) into bar($foo). */
6601
202k
  if (callable_ast->kind == ZEND_AST_CALL
6602
340
    && callable_ast->child[1]->kind == ZEND_AST_CALLABLE_CONVERT
6603
207
    && zend_is_pipe_optimizable_callable_name(callable_ast->child[0])) {
6604
193
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6605
193
        callable_ast->child[0], arg_list_ast);
6606
  /* Turn $foo |> bar::baz(...) into bar::baz($foo). */
6607
202k
  } else if (callable_ast->kind == ZEND_AST_STATIC_CALL
6608
493
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6609
97
    fcall_ast = zend_ast_create(ZEND_AST_STATIC_CALL,
6610
97
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6611
  /* Turn $foo |> $bar->baz(...) into $bar->baz($foo). */
6612
201k
  } else if (callable_ast->kind == ZEND_AST_METHOD_CALL
6613
1.13k
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6614
714
    fcall_ast = zend_ast_create(ZEND_AST_METHOD_CALL,
6615
714
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6616
  /* Turn $foo |> $expr into ($expr)($foo) */
6617
201k
  } else {
6618
201k
    zend_compile_expr(&callable_result, callable_ast);
6619
201k
    callable_ast = zend_ast_create_znode(&callable_result);
6620
201k
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6621
201k
      callable_ast, arg_list_ast);
6622
201k
  }
6623
6624
202k
  zend_do_extended_stmt(&operand_result);
6625
6626
202k
  zend_compile_expr(result, fcall_ast);
6627
202k
}
6628
6629
static void zend_compile_match(znode *result, zend_ast *ast)
6630
2.82k
{
6631
2.82k
  zend_ast *expr_ast = ast->child[0];
6632
2.82k
  zend_ast_list *arms = zend_ast_get_list(ast->child[1]);
6633
2.82k
  bool has_default_arm = false;
6634
2.82k
  uint32_t opnum_match = (uint32_t)-1;
6635
6636
2.82k
  znode expr_node;
6637
2.82k
  zend_compile_expr(&expr_node, expr_ast);
6638
6639
2.82k
  znode case_node;
6640
2.82k
  case_node.op_type = IS_TMP_VAR;
6641
2.82k
  case_node.u.op.var = get_temporary_variable();
6642
6643
2.82k
  uint32_t num_conds = count_match_conds(arms);
6644
2.82k
  uint8_t can_use_jumptable = can_match_use_jumptable(arms);
6645
2.82k
  bool uses_jumptable = can_use_jumptable && num_conds >= 2;
6646
2.82k
  HashTable *jumptable = NULL;
6647
2.82k
  uint32_t *jmpnz_opnums = NULL;
6648
6649
6.88k
  for (uint32_t i = 0; i < arms->children; ++i) {
6650
4.07k
    zend_ast *arm_ast = arms->child[i];
6651
6652
4.07k
    if (!arm_ast->child[0]) {
6653
610
      if (has_default_arm) {
6654
5
        CG(zend_lineno) = arm_ast->lineno;
6655
5
        zend_error_noreturn(E_COMPILE_ERROR,
6656
5
          "Match expressions may only contain one default arm");
6657
5
      }
6658
605
      has_default_arm = true;
6659
605
    }
6660
4.07k
  }
6661
6662
2.81k
  if (uses_jumptable) {
6663
1.31k
    znode jumptable_op;
6664
6665
1.31k
    ALLOC_HASHTABLE(jumptable);
6666
1.31k
    zend_hash_init(jumptable, num_conds, NULL, NULL, 0);
6667
1.31k
    jumptable_op.op_type = IS_CONST;
6668
1.31k
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6669
6670
1.31k
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH, &expr_node, &jumptable_op);
6671
1.31k
    if (opline->op1_type == IS_CONST) {
6672
534
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6673
534
    }
6674
1.31k
    opnum_match = opline - CG(active_op_array)->opcodes;
6675
1.50k
  } else {
6676
1.50k
    jmpnz_opnums = safe_emalloc(sizeof(uint32_t), num_conds, 0);
6677
1.50k
    uint32_t cond_count = 0;
6678
3.50k
    for (uint32_t i = 0; i < arms->children; ++i) {
6679
2.00k
      zend_ast *arm_ast = arms->child[i];
6680
6681
2.00k
      if (!arm_ast->child[0]) {
6682
417
        continue;
6683
417
      }
6684
6685
1.58k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6686
4.84k
      for (uint32_t j = 0; j < conds->children; j++) {
6687
3.25k
        zend_ast *cond_ast = conds->child[j];
6688
6689
3.25k
        znode cond_node;
6690
3.25k
        zend_compile_expr(&cond_node, cond_ast);
6691
6692
3.25k
        uint32_t opcode = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE_STRICT : ZEND_IS_IDENTICAL;
6693
3.25k
        zend_op *opline = zend_emit_op(NULL, opcode, &expr_node, &cond_node);
6694
3.25k
        SET_NODE(opline->result, &case_node);
6695
3.25k
        if (opline->op1_type == IS_CONST) {
6696
1.49k
          Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6697
1.49k
        }
6698
6699
3.25k
        jmpnz_opnums[cond_count] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6700
6701
3.25k
        cond_count++;
6702
3.25k
      }
6703
1.58k
    }
6704
1.50k
  }
6705
6706
2.81k
  uint32_t opnum_default_jmp = 0;
6707
2.81k
  if (!uses_jumptable) {
6708
1.50k
    opnum_default_jmp = zend_emit_jump(0);
6709
1.50k
  }
6710
6711
2.81k
  bool is_first_case = true;
6712
2.81k
  uint32_t cond_count = 0;
6713
2.81k
  uint32_t *jmp_end_opnums = safe_emalloc(sizeof(uint32_t), arms->children, 0);
6714
6715
  // The generated default arm is emitted first to avoid live range issues where the tmpvar
6716
  // for the arm result is freed even though it has not been initialized yet.
6717
2.81k
  if (!has_default_arm) {
6718
2.21k
    if (!uses_jumptable) {
6719
1.08k
      zend_update_jump_target_to_next(opnum_default_jmp);
6720
1.08k
    }
6721
6722
2.21k
    if (jumptable) {
6723
1.13k
      zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6724
1.13k
      opline->extended_value = get_next_op_number();
6725
1.13k
    }
6726
6727
2.21k
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH_ERROR, &expr_node, NULL);
6728
2.21k
    if (opline->op1_type == IS_CONST) {
6729
603
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6730
603
    }
6731
2.21k
    if (arms->children == 0) {
6732
      /* Mark this as an "expression throw" for opcache. */
6733
70
      opline->extended_value = ZEND_THROW_IS_EXPR;
6734
70
    }
6735
2.21k
  }
6736
6737
6.86k
  for (uint32_t i = 0; i < arms->children; ++i) {
6738
4.04k
    zend_ast *arm_ast = arms->child[i];
6739
4.04k
    zend_ast *body_ast = arm_ast->child[1];
6740
6741
4.04k
    if (arm_ast->child[0] != NULL) {
6742
3.44k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6743
6744
11.8k
      for (uint32_t j = 0; j < conds->children; j++) {
6745
8.37k
        zend_ast *cond_ast = conds->child[j];
6746
6747
8.37k
        if (jmpnz_opnums != NULL) {
6748
3.25k
          zend_update_jump_target_to_next(jmpnz_opnums[cond_count]);
6749
3.25k
        }
6750
6751
8.37k
        if (jumptable) {
6752
5.11k
          zval *cond_zv = zend_ast_get_zval(cond_ast);
6753
5.11k
          zval jmp_target;
6754
5.11k
          ZVAL_LONG(&jmp_target, get_next_op_number());
6755
6756
5.11k
          if (Z_TYPE_P(cond_zv) == IS_LONG) {
6757
4.72k
            zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6758
4.72k
          } else {
6759
388
            ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6760
388
            zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6761
388
          }
6762
5.11k
        }
6763
6764
8.37k
        cond_count++;
6765
8.37k
      }
6766
3.44k
    } else {
6767
600
      if (!uses_jumptable) {
6768
417
        zend_update_jump_target_to_next(opnum_default_jmp);
6769
417
      }
6770
6771
600
      if (jumptable) {
6772
183
        ZEND_ASSERT(opnum_match != (uint32_t)-1);
6773
183
        zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6774
183
        opline->extended_value = get_next_op_number();
6775
183
      }
6776
600
    }
6777
6778
4.04k
    znode body_node;
6779
4.04k
    zend_compile_expr(&body_node, body_ast);
6780
6781
4.04k
    if (is_first_case) {
6782
2.74k
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL);
6783
2.74k
      is_first_case = false;
6784
2.74k
    } else {
6785
1.30k
      zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL);
6786
1.30k
      SET_NODE(opline_qm_assign->result, result);
6787
1.30k
    }
6788
6789
4.04k
    jmp_end_opnums[i] = zend_emit_jump(0);
6790
4.04k
  }
6791
6792
  // Initialize result in case there is no arm
6793
2.81k
  if (arms->children == 0) {
6794
70
    result->op_type = IS_CONST;
6795
70
    ZVAL_NULL(&result->u.constant);
6796
70
  }
6797
6798
6.86k
  for (uint32_t i = 0; i < arms->children; ++i) {
6799
4.04k
    zend_update_jump_target_to_next(jmp_end_opnums[i]);
6800
4.04k
  }
6801
6802
2.81k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
6803
1.80k
    zend_op *opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6804
1.80k
    opline->extended_value = ZEND_FREE_SWITCH;
6805
1.80k
  } else if (expr_node.op_type == IS_CONST) {
6806
969
    zval_ptr_dtor_nogc(&expr_node.u.constant);
6807
969
  }
6808
6809
2.81k
  if (jmpnz_opnums != NULL) {
6810
1.50k
    efree(jmpnz_opnums);
6811
1.50k
  }
6812
2.81k
  efree(jmp_end_opnums);
6813
2.81k
}
6814
6815
static void zend_compile_try(const zend_ast *ast) /* {{{ */
6816
20.1k
{
6817
20.1k
  zend_ast *try_ast = ast->child[0];
6818
20.1k
  const zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
6819
20.1k
  zend_ast *finally_ast = ast->child[2];
6820
6821
20.1k
  uint32_t i, j;
6822
20.1k
  zend_op *opline;
6823
20.1k
  uint32_t try_catch_offset;
6824
20.1k
  uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
6825
20.1k
  uint32_t orig_fast_call_var = CG(context).fast_call_var;
6826
20.1k
  uint32_t orig_try_catch_offset = CG(context).try_catch_offset;
6827
6828
20.1k
  if (catches->children == 0 && !finally_ast) {
6829
33
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
6830
33
  }
6831
6832
  /* label: try { } must not be equal to try { label: } */
6833
20.0k
  if (CG(context).labels) {
6834
395
    zend_label *label;
6835
395
    ZEND_HASH_MAP_REVERSE_FOREACH_PTR(CG(context).labels, label) {
6836
395
      if (label->opline_num == get_next_op_number()) {
6837
88
        zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
6838
88
      }
6839
395
      break;
6840
1.18k
    } ZEND_HASH_FOREACH_END();
6841
395
  }
6842
6843
20.0k
  try_catch_offset = zend_add_try_element(get_next_op_number());
6844
6845
20.0k
  if (finally_ast) {
6846
5.16k
    zend_loop_var fast_call;
6847
5.16k
    if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
6848
971
      CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
6849
971
    }
6850
5.16k
    CG(context).fast_call_var = get_temporary_variable();
6851
6852
    /* Push FAST_CALL on unwind stack */
6853
5.16k
    fast_call.opcode = ZEND_FAST_CALL;
6854
5.16k
    fast_call.var_type = IS_TMP_VAR;
6855
5.16k
    fast_call.var_num = CG(context).fast_call_var;
6856
5.16k
    fast_call.try_catch_offset = try_catch_offset;
6857
5.16k
    zend_stack_push(&CG(loop_var_stack), &fast_call);
6858
5.16k
  }
6859
6860
20.0k
  CG(context).try_catch_offset = try_catch_offset;
6861
6862
20.0k
  zend_compile_stmt(try_ast);
6863
6864
20.0k
  if (catches->children != 0) {
6865
14.9k
    jmp_opnums[0] = zend_emit_jump(0);
6866
14.9k
  }
6867
6868
38.0k
  for (i = 0; i < catches->children; ++i) {
6869
17.9k
    const zend_ast *catch_ast = catches->child[i];
6870
17.9k
    const zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]);
6871
17.9k
    zend_ast *var_ast = catch_ast->child[1];
6872
17.9k
    zend_ast *stmt_ast = catch_ast->child[2];
6873
17.9k
    zend_string *var_name = var_ast ? zval_make_interned_string(zend_ast_get_zval(var_ast)) : NULL;
6874
17.9k
    bool is_last_catch = (i + 1 == catches->children);
6875
6876
17.9k
    uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0);
6877
17.9k
    uint32_t opnum_catch = (uint32_t)-1;
6878
6879
17.9k
    CG(zend_lineno) = catch_ast->lineno;
6880
6881
39.9k
    for (j = 0; j < classes->children; j++) {
6882
21.9k
      zend_ast *class_ast = classes->child[j];
6883
21.9k
      bool is_last_class = (j + 1 == classes->children);
6884
6885
21.9k
      if (!zend_is_const_default_class_ref(class_ast)) {
6886
2
        zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
6887
2
      }
6888
6889
21.9k
      opnum_catch = get_next_op_number();
6890
21.9k
      if (i == 0 && j == 0) {
6891
14.9k
        CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
6892
14.9k
      }
6893
6894
21.9k
      opline = get_next_op();
6895
21.9k
      opline->opcode = ZEND_CATCH;
6896
21.9k
      opline->op1_type = IS_CONST;
6897
21.9k
      opline->op1.constant = zend_add_class_name_literal(
6898
21.9k
          zend_resolve_class_name_ast(class_ast));
6899
21.9k
      opline->extended_value = zend_alloc_cache_slot();
6900
6901
21.9k
      if (var_name && zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
6902
2
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6903
2
      }
6904
6905
21.9k
      opline->result_type = var_name ? IS_CV : IS_UNUSED;
6906
21.9k
      opline->result.var = var_name ? lookup_cv(var_name) : -1;
6907
6908
21.9k
      if (is_last_catch && is_last_class) {
6909
14.8k
        opline->extended_value |= ZEND_LAST_CATCH;
6910
14.8k
      }
6911
6912
21.9k
      if (!is_last_class) {
6913
3.97k
        jmp_multicatch[j] = zend_emit_jump(0);
6914
3.97k
        opline = &CG(active_op_array)->opcodes[opnum_catch];
6915
3.97k
        opline->op2.opline_num = get_next_op_number();
6916
3.97k
      }
6917
21.9k
    }
6918
6919
21.9k
    for (j = 0; j < classes->children - 1; j++) {
6920
3.96k
      zend_update_jump_target_to_next(jmp_multicatch[j]);
6921
3.96k
    }
6922
6923
17.9k
    efree(jmp_multicatch);
6924
6925
17.9k
    zend_compile_stmt(stmt_ast);
6926
6927
17.9k
    if (!is_last_catch) {
6928
3.06k
      jmp_opnums[i + 1] = zend_emit_jump(0);
6929
3.06k
    }
6930
6931
17.9k
    ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class");
6932
17.9k
    opline = &CG(active_op_array)->opcodes[opnum_catch];
6933
17.9k
    if (!is_last_catch) {
6934
3.06k
      opline->op2.opline_num = get_next_op_number();
6935
3.06k
    }
6936
17.9k
  }
6937
6938
38.0k
  for (i = 0; i < catches->children; ++i) {
6939
17.9k
    zend_update_jump_target_to_next(jmp_opnums[i]);
6940
17.9k
  }
6941
6942
20.0k
  if (finally_ast) {
6943
5.15k
    zend_loop_var discard_exception;
6944
5.15k
    uint32_t opnum_jmp = get_next_op_number() + 1;
6945
6946
    /* Pop FAST_CALL from unwind stack */
6947
5.15k
    zend_stack_del_top(&CG(loop_var_stack));
6948
6949
    /* Push DISCARD_EXCEPTION on unwind stack */
6950
5.15k
    discard_exception.opcode = ZEND_DISCARD_EXCEPTION;
6951
5.15k
    discard_exception.var_type = IS_TMP_VAR;
6952
5.15k
    discard_exception.var_num = CG(context).fast_call_var;
6953
5.15k
    zend_stack_push(&CG(loop_var_stack), &discard_exception);
6954
6955
5.15k
    CG(zend_lineno) = finally_ast->lineno;
6956
6957
5.15k
    opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
6958
5.15k
    opline->op1.num = try_catch_offset;
6959
5.15k
    opline->result_type = IS_TMP_VAR;
6960
5.15k
    opline->result.var = CG(context).fast_call_var;
6961
6962
5.15k
    zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
6963
6964
5.15k
    zend_compile_stmt(finally_ast);
6965
6966
5.15k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
6967
5.15k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
6968
5.15k
      = get_next_op_number();
6969
6970
5.15k
    opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
6971
5.15k
    opline->op1_type = IS_TMP_VAR;
6972
5.15k
    opline->op1.var = CG(context).fast_call_var;
6973
5.15k
    opline->op2.num = orig_try_catch_offset;
6974
6975
5.15k
    zend_update_jump_target_to_next(opnum_jmp);
6976
6977
5.15k
    CG(context).fast_call_var = orig_fast_call_var;
6978
6979
    /* Pop DISCARD_EXCEPTION from unwind stack */
6980
5.15k
    zend_stack_del_top(&CG(loop_var_stack));
6981
5.15k
  }
6982
6983
20.0k
  CG(context).try_catch_offset = orig_try_catch_offset;
6984
6985
20.0k
  efree(jmp_opnums);
6986
20.0k
}
6987
/* }}} */
6988
6989
/* Encoding declarations must already be handled during parsing */
6990
bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
6991
2.83k
{
6992
2.83k
  const zend_ast_list *declares = zend_ast_get_list(ast);
6993
2.83k
  uint32_t i;
6994
7.73k
  for (i = 0; i < declares->children; ++i) {
6995
4.90k
    const zend_ast *declare_ast = declares->child[i];
6996
4.90k
    zend_ast *name_ast = declare_ast->child[0];
6997
4.90k
    zend_ast *value_ast = declare_ast->child[1];
6998
4.90k
    const zend_string *name = zend_ast_get_str(name_ast);
6999
7000
4.90k
    if (zend_string_equals_literal_ci(name, "encoding")) {
7001
145
      if (value_ast->kind != ZEND_AST_ZVAL) {
7002
3
        zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0);
7003
3
        return 0;
7004
3
      }
7005
7006
142
      if (CG(multibyte)) {
7007
0
        zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
7008
7009
0
        const zend_encoding *new_encoding, *old_encoding;
7010
0
        zend_encoding_filter old_input_filter;
7011
7012
0
        CG(encoding_declared) = 1;
7013
7014
0
        new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
7015
0
        if (!new_encoding) {
7016
0
          zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
7017
0
        } else {
7018
0
          old_input_filter = LANG_SCNG(input_filter);
7019
0
          old_encoding = LANG_SCNG(script_encoding);
7020
0
          zend_multibyte_set_filter(new_encoding);
7021
7022
          /* need to re-scan if input filter changed */
7023
0
          if (old_input_filter != LANG_SCNG(input_filter) ||
7024
0
             (old_input_filter && new_encoding != old_encoding)) {
7025
0
            zend_multibyte_yyinput_again(old_input_filter, old_encoding);
7026
0
          }
7027
0
        }
7028
7029
0
        zend_string_release_ex(encoding_name, 0);
7030
142
      } else {
7031
142
        zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
7032
142
          "Zend multibyte feature is turned off by settings");
7033
142
      }
7034
142
    }
7035
4.90k
  }
7036
7037
2.83k
  return 1;
7038
2.83k
}
7039
/* }}} */
7040
7041
/* Check whether this is the first statement, not counting declares. */
7042
static zend_result zend_is_first_statement(const zend_ast *ast, bool allow_nop) /* {{{ */
7043
2.61k
{
7044
2.61k
  uint32_t i = 0;
7045
2.61k
  const zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
7046
7047
12.9k
  while (i < file_ast->children) {
7048
12.9k
    if (file_ast->child[i] == ast) {
7049
2.58k
      return SUCCESS;
7050
10.4k
    } else if (file_ast->child[i] == NULL) {
7051
502
      if (!allow_nop) {
7052
1
        return FAILURE;
7053
1
      }
7054
9.90k
    } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
7055
25
      return FAILURE;
7056
25
    }
7057
10.3k
    i++;
7058
10.3k
  }
7059
2
  return FAILURE;
7060
2.61k
}
7061
/* }}} */
7062
7063
static void zend_compile_declare(const zend_ast *ast) /* {{{ */
7064
5.52k
{
7065
5.52k
  const zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
7066
5.52k
  zend_ast *stmt_ast = ast->child[1];
7067
5.52k
  zend_declarables orig_declarables = FC(declarables);
7068
5.52k
  uint32_t i;
7069
7070
22.9k
  for (i = 0; i < declares->children; ++i) {
7071
17.4k
    zend_ast *declare_ast = declares->child[i];
7072
17.4k
    zend_ast *name_ast = declare_ast->child[0];
7073
17.4k
    zend_ast **value_ast_ptr = &declare_ast->child[1];
7074
17.4k
    zend_string *name = zend_ast_get_str(name_ast);
7075
7076
17.4k
    if ((*value_ast_ptr)->kind != ZEND_AST_ZVAL) {
7077
32
      zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
7078
32
    }
7079
7080
17.4k
    if (zend_string_equals_literal_ci(name, "ticks")) {
7081
9.11k
      zval value_zv;
7082
9.11k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7083
9.11k
      FC(declarables).ticks = zval_get_long(&value_zv);
7084
9.11k
      zval_ptr_dtor_nogc(&value_zv);
7085
9.11k
    } else if (zend_string_equals_literal_ci(name, "encoding")) {
7086
7087
108
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ false)) {
7088
4
        zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
7089
4
          "the very first statement in the script");
7090
4
      }
7091
8.22k
    } else if (zend_string_equals_literal_ci(name, "strict_types")) {
7092
365
      zval value_zv;
7093
7094
365
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
7095
5
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
7096
5
          "the very first statement in the script");
7097
5
      }
7098
7099
360
      if (ast->child[1] != NULL) {
7100
2
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
7101
2
          "use block mode");
7102
2
      }
7103
7104
358
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7105
7106
358
      if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
7107
52
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
7108
52
      }
7109
7110
306
      if (Z_LVAL(value_zv) == 1) {
7111
165
        CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
7112
165
      }
7113
7114
7.86k
    } else {
7115
7.86k
      zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
7116
7.86k
    }
7117
17.4k
  }
7118
7119
5.43k
  if (stmt_ast) {
7120
471
    zend_compile_stmt(stmt_ast);
7121
7122
471
    FC(declarables) = orig_declarables;
7123
471
  }
7124
5.43k
}
7125
/* }}} */
7126
7127
static void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
7128
1.69M
{
7129
1.69M
  const zend_ast_list *list = zend_ast_get_list(ast);
7130
1.69M
  uint32_t i;
7131
7.68M
  for (i = 0; i < list->children; ++i) {
7132
5.98M
    zend_compile_stmt(list->child[i]);
7133
5.98M
  }
7134
1.69M
}
7135
/* }}} */
7136
7137
ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
7138
1.38M
{
7139
1.38M
  uint32_t i, n;
7140
7141
1.38M
  func->common.arg_flags[0] = 0;
7142
1.38M
  func->common.arg_flags[1] = 0;
7143
1.38M
  func->common.arg_flags[2] = 0;
7144
1.38M
  if (func->common.arg_info) {
7145
1.38M
    n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
7146
1.38M
    i = 0;
7147
2.83M
    while (i < n) {
7148
1.44M
      ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i]));
7149
1.44M
      i++;
7150
1.44M
    }
7151
1.38M
    if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_VARIADIC) && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) {
7152
739
      uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]);
7153
9.52k
      while (i < MAX_ARG_FLAG_NUM) {
7154
8.78k
        ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
7155
8.78k
        i++;
7156
8.78k
      }
7157
739
    }
7158
1.38M
  }
7159
1.38M
}
7160
/* }}} */
7161
7162
static zend_type zend_compile_single_typename(zend_ast *ast)
7163
448k
{
7164
448k
  ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE));
7165
448k
  if (ast->kind == ZEND_AST_TYPE) {
7166
3.46k
    if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) {
7167
2
      zend_error_noreturn(E_COMPILE_ERROR,
7168
2
        "Cannot use \"static\" when no class scope is active");
7169
2
    }
7170
7171
3.46k
    return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0);
7172
444k
  } else {
7173
444k
    zend_string *type_name = zend_ast_get_str(ast);
7174
444k
    uint8_t type_code = zend_lookup_builtin_type_by_name(type_name);
7175
7176
444k
    if (type_code != 0) {
7177
111k
      if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) {
7178
2
        zend_error_noreturn(E_COMPILE_ERROR,
7179
2
          "Type declaration '%s' must be unqualified",
7180
2
          ZSTR_VAL(zend_string_tolower(type_name)));
7181
2
      }
7182
7183
      /* Transform iterable into a type union alias */
7184
111k
      if (type_code == IS_ITERABLE) {
7185
        /* Set iterable bit for BC compat during Reflection and string representation of type */
7186
96.3k
        zend_type iterable = (zend_type) ZEND_TYPE_INIT_CLASS_MASK(ZSTR_KNOWN(ZEND_STR_TRAVERSABLE),
7187
96.3k
                  (MAY_BE_ARRAY|_ZEND_TYPE_ITERABLE_BIT));
7188
96.3k
        return iterable;
7189
96.3k
      }
7190
7191
15.2k
      return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0);
7192
333k
    } else {
7193
333k
      const char *correct_name;
7194
333k
      uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
7195
333k
      zend_string *class_name = type_name;
7196
7197
333k
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
7198
325k
        class_name = zend_resolve_class_name_ast(ast);
7199
325k
        zend_assert_valid_class_name(class_name, "a type name");
7200
325k
      } else {
7201
7.47k
        ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT);
7202
7203
7.47k
        zend_ensure_valid_class_fetch_type(fetch_type);
7204
7205
7.47k
        bool substitute_self_parent = zend_is_scope_known()
7206
235
          && !(CG(active_class_entry)->ce_flags & ZEND_ACC_ANON_CLASS);
7207
7208
7.47k
        if (fetch_type == ZEND_FETCH_CLASS_SELF) {
7209
          /* Scope might be unknown for unbound closures and traits */
7210
7.41k
          if (substitute_self_parent) {
7211
207
            class_name = CG(active_class_entry)->name;
7212
207
            ZEND_ASSERT(class_name && "must know class name when resolving self type at compile time");
7213
207
          }
7214
7.41k
        } else {
7215
62
          ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_PARENT);
7216
          /* Scope might be unknown for unbound closures and traits */
7217
62
          if (substitute_self_parent) {
7218
27
            class_name = CG(active_class_entry)->parent_name;
7219
27
            ZEND_ASSERT(class_name && "must know class name when resolving parent type at compile time");
7220
27
          }
7221
56
        }
7222
7.47k
        zend_string_addref(class_name);
7223
7.47k
      }
7224
7225
333k
      if (ast->attr == ZEND_NAME_NOT_FQ
7226
333k
          && zend_is_confusable_type(type_name, &correct_name)
7227
39.3k
          && zend_is_not_imported(type_name)) {
7228
39.3k
        const char *extra =
7229
39.3k
          FC(current_namespace) ? " or import the class with \"use\"" : "";
7230
39.3k
        if (correct_name) {
7231
38.8k
          zend_error(E_COMPILE_WARNING,
7232
38.8k
            "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? "
7233
38.8k
            "Write \"\\%s\"%s to suppress this warning",
7234
38.8k
            ZSTR_VAL(type_name), correct_name, ZSTR_VAL(class_name), extra);
7235
38.8k
        } else {
7236
586
          zend_error(E_COMPILE_WARNING,
7237
586
            "\"%s\" is not a supported builtin type "
7238
586
            "and will be interpreted as a class name. "
7239
586
            "Write \"\\%s\"%s to suppress this warning",
7240
586
            ZSTR_VAL(type_name), ZSTR_VAL(class_name), extra);
7241
586
        }
7242
39.3k
      }
7243
7244
333k
      class_name = zend_new_interned_string(class_name);
7245
333k
      zend_alloc_ce_cache(class_name);
7246
333k
      return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, /* allow null */ false, 0);
7247
333k
    }
7248
444k
  }
7249
448k
}
7250
7251
static void zend_are_intersection_types_redundant(const zend_type left_type, const zend_type right_type)
7252
17.1k
{
7253
17.1k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(left_type));
7254
17.1k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(right_type));
7255
17.1k
  const zend_type_list *l_type_list = ZEND_TYPE_LIST(left_type);
7256
17.1k
  const zend_type_list *r_type_list = ZEND_TYPE_LIST(right_type);
7257
17.1k
  const zend_type_list *smaller_type_list, *larger_type_list;
7258
17.1k
  bool flipped = false;
7259
7260
17.1k
  if (r_type_list->num_types < l_type_list->num_types) {
7261
5.81k
    smaller_type_list = r_type_list;
7262
5.81k
    larger_type_list = l_type_list;
7263
5.81k
    flipped = true;
7264
11.3k
  } else {
7265
11.3k
    smaller_type_list = l_type_list;
7266
11.3k
    larger_type_list = r_type_list;
7267
11.3k
  }
7268
7269
17.1k
  unsigned int sum = 0;
7270
17.1k
  const zend_type *outer_type;
7271
67.8k
  ZEND_TYPE_LIST_FOREACH(smaller_type_list, outer_type)
7272
67.8k
    const zend_type *inner_type;
7273
232k
    ZEND_TYPE_LIST_FOREACH(larger_type_list, inner_type)
7274
232k
      if (zend_string_equals_ci(ZEND_TYPE_NAME(*inner_type), ZEND_TYPE_NAME(*outer_type))) {
7275
18.6k
        sum++;
7276
18.6k
        break;
7277
18.6k
      }
7278
232k
    ZEND_TYPE_LIST_FOREACH_END();
7279
67.8k
  ZEND_TYPE_LIST_FOREACH_END();
7280
7281
17.1k
  if (sum == smaller_type_list->num_types) {
7282
30
    zend_string *smaller_type_str;
7283
30
    zend_string *larger_type_str;
7284
30
    if (flipped) {
7285
5
      smaller_type_str = zend_type_to_string(right_type);
7286
5
      larger_type_str = zend_type_to_string(left_type);
7287
25
    } else {
7288
25
      smaller_type_str = zend_type_to_string(left_type);
7289
25
      larger_type_str = zend_type_to_string(right_type);
7290
25
    }
7291
30
    if (smaller_type_list->num_types == larger_type_list->num_types) {
7292
17
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant with type %s",
7293
17
        ZSTR_VAL(smaller_type_str), ZSTR_VAL(larger_type_str));
7294
17
    } else {
7295
13
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7296
13
        ZSTR_VAL(larger_type_str), ZSTR_VAL(smaller_type_str));
7297
13
    }
7298
30
  }
7299
17.1k
}
7300
7301
static void zend_is_intersection_type_redundant_by_single_type(const zend_type intersection_type, const zend_type single_type)
7302
18.0k
{
7303
18.0k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(intersection_type));
7304
18.0k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(single_type));
7305
7306
18.0k
  const zend_type *single_intersection_type = NULL;
7307
66.3k
  ZEND_TYPE_FOREACH(intersection_type, single_intersection_type)
7308
66.3k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(*single_intersection_type), ZEND_TYPE_NAME(single_type))) {
7309
3
      zend_string *single_type_str = zend_type_to_string(single_type);
7310
3
      zend_string *complete_type = zend_type_to_string(intersection_type);
7311
3
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7312
3
          ZSTR_VAL(complete_type), ZSTR_VAL(single_type_str));
7313
3
    }
7314
66.3k
  ZEND_TYPE_FOREACH_END();
7315
18.0k
}
7316
7317
/* Used by both intersection and union types prior to transforming the type list to a full zend_type */
7318
static void zend_is_type_list_redundant_by_single_type(const zend_type_list *type_list, const zend_type type)
7319
58.9k
{
7320
58.9k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(type));
7321
117k
  for (size_t i = 0; i < type_list->num_types - 1; i++) {
7322
58.0k
    if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7323
13.4k
      zend_is_intersection_type_redundant_by_single_type(type_list->types[i], type);
7324
13.4k
      continue;
7325
13.4k
    }
7326
44.6k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(type_list->types[i]), ZEND_TYPE_NAME(type))) {
7327
16
      zend_string *single_type_str = zend_type_to_string(type);
7328
16
      zend_error_noreturn(E_COMPILE_ERROR, "Duplicate type %s is redundant", ZSTR_VAL(single_type_str));
7329
16
    }
7330
44.6k
  }
7331
58.9k
}
7332
7333
static zend_type zend_compile_typename(zend_ast *ast);
7334
7335
static zend_type zend_compile_typename_ex(
7336
    zend_ast *ast, bool force_allow_null, bool *forced_allow_null) /* {{{ */
7337
412k
{
7338
412k
  bool is_marked_nullable = ast->attr & ZEND_TYPE_NULLABLE;
7339
412k
  zend_ast_attr orig_ast_attr = ast->attr;
7340
412k
  zend_type type = ZEND_TYPE_INIT_NONE(0);
7341
7342
412k
  if (is_marked_nullable) {
7343
5.00k
    ast->attr &= ~ZEND_TYPE_NULLABLE;
7344
5.00k
  }
7345
7346
412k
  if (ast->kind == ZEND_AST_TYPE_UNION) {
7347
21.2k
    const zend_ast_list *list = zend_ast_get_list(ast);
7348
21.2k
    zend_type_list *type_list;
7349
21.2k
    bool is_composite = false;
7350
21.2k
    bool has_only_iterable_class = true;
7351
21.2k
    ALLOCA_FLAG(use_heap)
7352
7353
21.2k
    type_list = do_alloca(ZEND_TYPE_LIST_SIZE(list->children), use_heap);
7354
21.2k
    type_list->num_types = 0;
7355
7356
68.8k
    for (uint32_t i = 0; i < list->children; i++) {
7357
47.6k
      zend_ast *type_ast = list->child[i];
7358
47.6k
      zend_type single_type;
7359
47.6k
      uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7360
7361
47.6k
      if (type_ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7362
12.0k
        has_only_iterable_class = false;
7363
12.0k
        is_composite = true;
7364
        /* The first class type can be stored directly as the type ptr payload. */
7365
12.0k
        if (ZEND_TYPE_IS_COMPLEX(type) && !ZEND_TYPE_HAS_LIST(type)) {
7366
          /* Switch from single name to name list. */
7367
226
          type_list->num_types = 1;
7368
226
          type_list->types[0] = type;
7369
          /* Clear MAY_BE_* type flags */
7370
226
          ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7371
226
        }
7372
        /* Mark type as list type */
7373
12.0k
        ZEND_TYPE_SET_LIST(type, type_list);
7374
7375
12.0k
        single_type = zend_compile_typename(type_ast);
7376
12.0k
        ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(single_type));
7377
7378
12.0k
        type_list->types[type_list->num_types++] = single_type;
7379
7380
        /* Check for trivially redundant class types */
7381
33.7k
        for (size_t i = 0; i < type_list->num_types - 1; i++) {
7382
21.7k
          if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7383
17.1k
            zend_are_intersection_types_redundant(single_type, type_list->types[i]);
7384
17.1k
            continue;
7385
17.1k
          }
7386
          /* Type from type list is a simple type */
7387
4.57k
          zend_is_intersection_type_redundant_by_single_type(single_type, type_list->types[i]);
7388
4.57k
        }
7389
12.0k
        continue;
7390
12.0k
      }
7391
7392
35.5k
      single_type = zend_compile_single_typename(type_ast);
7393
35.5k
      uint32_t single_type_mask = ZEND_TYPE_PURE_MASK(single_type);
7394
7395
35.5k
      if (single_type_mask == MAY_BE_ANY) {
7396
2
        zend_error_noreturn(E_COMPILE_ERROR, "Type mixed can only be used as a standalone type");
7397
2
      }
7398
35.5k
      if (ZEND_TYPE_IS_COMPLEX(single_type) && !ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7399
31.2k
        has_only_iterable_class = false;
7400
31.2k
      }
7401
7402
35.5k
      uint32_t type_mask_overlap = ZEND_TYPE_PURE_MASK(type) & single_type_mask;
7403
35.5k
      if (type_mask_overlap) {
7404
11
        zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap);
7405
11
        zend_string *overlap_type_str = zend_type_to_string(overlap_type);
7406
11
        zend_error_noreturn(E_COMPILE_ERROR,
7407
11
          "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str));
7408
11
      }
7409
7410
35.5k
      if ( ((type_mask & MAY_BE_TRUE) && (single_type_mask == MAY_BE_FALSE))
7411
35.5k
          || ((type_mask & MAY_BE_FALSE) && (single_type_mask == MAY_BE_TRUE)) ) {
7412
4
        zend_error_noreturn(E_COMPILE_ERROR,
7413
4
          "Type contains both true and false, bool must be used instead");
7414
4
      }
7415
35.5k
      ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type);
7416
      /* Clear MAY_BE_* type flags */
7417
35.5k
      ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK;
7418
7419
35.5k
      if (ZEND_TYPE_IS_COMPLEX(single_type)) {
7420
32.0k
        if (!ZEND_TYPE_IS_COMPLEX(type) && !is_composite) {
7421
          /* The first class type can be stored directly as the type ptr payload. */
7422
11.2k
          ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type));
7423
11.2k
          ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT;
7424
20.7k
        } else {
7425
20.7k
          if (type_list->num_types == 0) {
7426
            /* Switch from single name to name list. */
7427
9.91k
            type_list->num_types = 1;
7428
9.91k
            type_list->types[0] = type;
7429
            /* Clear MAY_BE_* type flags */
7430
9.91k
            ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7431
9.91k
            ZEND_TYPE_SET_LIST(type, type_list);
7432
9.91k
          }
7433
7434
20.7k
          type_list->types[type_list->num_types++] = single_type;
7435
7436
          /* Check for trivially redundant class types */
7437
20.7k
          zend_is_type_list_redundant_by_single_type(type_list, single_type);
7438
20.7k
        }
7439
32.0k
      }
7440
35.5k
    }
7441
7442
21.2k
    if (type_list->num_types) {
7443
19.1k
      zend_type_list *list = zend_arena_alloc(
7444
19.1k
        &CG(arena), ZEND_TYPE_LIST_SIZE(type_list->num_types));
7445
19.1k
      memcpy(list, type_list, ZEND_TYPE_LIST_SIZE(type_list->num_types));
7446
19.1k
      ZEND_TYPE_SET_LIST(type, list);
7447
19.1k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7448
      /* Inform that the type list is a union type */
7449
19.1k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7450
19.1k
    }
7451
7452
21.2k
    free_alloca(type_list, use_heap);
7453
7454
21.2k
    uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7455
21.2k
    if ((type_mask & MAY_BE_OBJECT) &&
7456
757
        ((!has_only_iterable_class && ZEND_TYPE_IS_COMPLEX(type)) || (type_mask & MAY_BE_STATIC))) {
7457
9
      zend_string *type_str = zend_type_to_string(type);
7458
9
      zend_error_noreturn(E_COMPILE_ERROR,
7459
9
        "Type %s contains both object and a class type, which is redundant",
7460
9
        ZSTR_VAL(type_str));
7461
9
    }
7462
391k
  } else if (ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7463
16.9k
    const zend_ast_list *list = zend_ast_get_list(ast);
7464
16.9k
    zend_type_list *type_list;
7465
7466
    /* Allocate the type list directly on the arena as it must be a type
7467
     * list of the same number of elements as the AST list has children */
7468
16.9k
    type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children));
7469
16.9k
    type_list->num_types = 0;
7470
7471
16.9k
    ZEND_ASSERT(list->children > 1);
7472
7473
55.2k
    for (uint32_t i = 0; i < list->children; i++) {
7474
38.2k
      zend_ast *type_ast = list->child[i];
7475
38.2k
      zend_type single_type = zend_compile_single_typename(type_ast);
7476
7477
      /* An intersection of union types cannot exist so invalidate it
7478
       * Currently only can happen with iterable getting canonicalized to Traversable|array */
7479
38.2k
      if (ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7480
2
        zend_string *standard_type_str = zend_type_to_string(single_type);
7481
2
        zend_error_noreturn(E_COMPILE_ERROR,
7482
2
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7483
0
        zend_string_release_ex(standard_type_str, false);
7484
0
      }
7485
      /* An intersection of standard types cannot exist so invalidate it */
7486
38.2k
      if (ZEND_TYPE_IS_ONLY_MASK(single_type)) {
7487
21
        zend_string *standard_type_str = zend_type_to_string(single_type);
7488
21
        zend_error_noreturn(E_COMPILE_ERROR,
7489
21
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7490
0
        zend_string_release_ex(standard_type_str, false);
7491
0
      }
7492
      /* Check for "self" and "parent" too */
7493
38.2k
      if (
7494
38.2k
        zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_SELF))
7495
38.2k
        || zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_PARENT))
7496
38.2k
      ) {
7497
4
        zend_error_noreturn(E_COMPILE_ERROR,
7498
4
          "Type %s cannot be part of an intersection type", ZSTR_VAL(ZEND_TYPE_NAME(single_type)));
7499
4
      }
7500
7501
      /* Add type to the type list */
7502
38.2k
      type_list->types[type_list->num_types++] = single_type;
7503
7504
      /* Check for trivially redundant class types */
7505
38.2k
      zend_is_type_list_redundant_by_single_type(type_list, single_type);
7506
38.2k
    }
7507
7508
16.9k
    ZEND_ASSERT(list->children == type_list->num_types);
7509
7510
    /* An implicitly nullable intersection type needs to be converted to a DNF type */
7511
16.9k
    if (force_allow_null) {
7512
3.69k
      zend_type intersection_type = ZEND_TYPE_INIT_NONE(0);
7513
3.69k
      ZEND_TYPE_SET_LIST(intersection_type, type_list);
7514
3.69k
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_INTERSECTION_BIT;
7515
3.69k
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_ARENA_BIT;
7516
7517
3.69k
      zend_type_list *dnf_type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(1));
7518
3.69k
      dnf_type_list->num_types = 1;
7519
3.69k
      dnf_type_list->types[0] = intersection_type;
7520
3.69k
      ZEND_TYPE_SET_LIST(type, dnf_type_list);
7521
      /* Inform that the type list is a DNF type */
7522
3.69k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7523
3.69k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7524
13.2k
    } else {
7525
13.2k
      ZEND_TYPE_SET_LIST(type, type_list);
7526
      /* Inform that the type list is an intersection type */
7527
13.2k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_INTERSECTION_BIT;
7528
13.2k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7529
13.2k
    }
7530
374k
  } else {
7531
374k
    type = zend_compile_single_typename(ast);
7532
374k
  }
7533
7534
412k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
7535
7536
412k
  if (type_mask == MAY_BE_ANY && is_marked_nullable) {
7537
4
    zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be marked as nullable since mixed already includes null");
7538
4
  }
7539
7540
412k
  if ((type_mask & MAY_BE_NULL) && is_marked_nullable) {
7541
2
    zend_error_noreturn(E_COMPILE_ERROR, "null cannot be marked as nullable");
7542
2
  }
7543
7544
412k
  if (force_allow_null && !is_marked_nullable && !(type_mask & MAY_BE_NULL)) {
7545
4.78k
    *forced_allow_null = true;
7546
4.78k
  }
7547
7548
412k
  if (is_marked_nullable || force_allow_null) {
7549
9.87k
    ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
7550
9.87k
    type_mask = ZEND_TYPE_PURE_MASK(type);
7551
9.87k
  }
7552
7553
412k
  if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_VOID)) {
7554
4
    zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type");
7555
4
  }
7556
7557
412k
  if ((type_mask & MAY_BE_NEVER) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_NEVER)) {
7558
3
    zend_error_noreturn(E_COMPILE_ERROR, "never can only be used as a standalone type");
7559
3
  }
7560
7561
412k
  ast->attr = orig_ast_attr;
7562
412k
  return type;
7563
412k
}
7564
/* }}} */
7565
7566
static zend_type zend_compile_typename(zend_ast *ast)
7567
59.4k
{
7568
59.4k
  bool forced_allow_null;
7569
59.4k
  return zend_compile_typename_ex(ast, false, &forced_allow_null);
7570
59.4k
}
7571
7572
/* May convert value from int to float. */
7573
static bool zend_is_valid_default_value(zend_type type, zval *value)
7574
2.74k
{
7575
2.74k
  ZEND_ASSERT(ZEND_TYPE_IS_SET(type));
7576
2.74k
  if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) {
7577
1.38k
    return 1;
7578
1.38k
  }
7579
1.36k
  if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) {
7580
    /* Integers are allowed as initializers for floating-point values. */
7581
1.28k
    convert_to_double(value);
7582
1.28k
    return 1;
7583
1.28k
  }
7584
76
  return 0;
7585
1.36k
}
7586
7587
static void zend_compile_attributes(
7588
  HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target, uint32_t promoted
7589
1.39M
) /* {{{ */ {
7590
1.39M
  zend_attribute *attr;
7591
1.39M
  zend_internal_attribute *config;
7592
7593
1.39M
  const zend_ast_list *list = zend_ast_get_list(ast);
7594
1.39M
  uint32_t g, i, j;
7595
7596
1.39M
  ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE_LIST);
7597
7598
2.79M
  for (g = 0; g < list->children; g++) {
7599
1.40M
    const zend_ast_list *group = zend_ast_get_list(list->child[g]);
7600
7601
1.40M
    ZEND_ASSERT(group->kind == ZEND_AST_ATTRIBUTE_GROUP);
7602
7603
3.84M
    for (i = 0; i < group->children; i++) {
7604
2.44M
      ZEND_ASSERT(group->child[i]->kind == ZEND_AST_ATTRIBUTE);
7605
7606
2.44M
      const zend_ast *el = group->child[i];
7607
7608
2.44M
      if (el->child[1] &&
7609
64.6k
          el->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) {
7610
4
          zend_error_noreturn(E_COMPILE_ERROR,
7611
4
              "Cannot create Closure as attribute argument");
7612
4
      }
7613
7614
2.44M
      zend_string *name = zend_resolve_class_name_ast(el->child[0]);
7615
2.44M
      zend_string *lcname = zend_string_tolower_ex(name, false);
7616
2.44M
      zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL;
7617
7618
2.44M
      config = zend_internal_attribute_get(lcname);
7619
2.44M
      zend_string_release(lcname);
7620
7621
      /* Exclude internal attributes that do not match on promoted properties. */
7622
2.44M
      if (config && !(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7623
463
        if (promoted & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL)) {
7624
2
          zend_string_release(name);
7625
2
          continue;
7626
2
        }
7627
463
      }
7628
7629
2.44M
      uint32_t flags = (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES)
7630
2.44M
        ? ZEND_ATTRIBUTE_STRICT_TYPES : 0;
7631
2.44M
      attr = zend_add_attribute(
7632
2.44M
        attributes, name, args ? args->children : 0, flags, offset, el->lineno);
7633
2.44M
      zend_string_release(name);
7634
7635
      /* Populate arguments */
7636
2.44M
      if (args) {
7637
64.6k
        ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST);
7638
7639
64.6k
        bool uses_named_args = false;
7640
144k
        for (j = 0; j < args->children; j++) {
7641
80.2k
          zend_ast **arg_ast_ptr = &args->child[j];
7642
80.2k
          zend_ast *arg_ast = *arg_ast_ptr;
7643
7644
80.2k
          if (arg_ast->kind == ZEND_AST_UNPACK) {
7645
2
            zend_error_noreturn(E_COMPILE_ERROR,
7646
2
              "Cannot use unpacking in attribute argument list");
7647
2
          }
7648
7649
80.2k
          if (arg_ast->kind == ZEND_AST_NAMED_ARG) {
7650
3.28k
            attr->args[j].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0]));
7651
3.28k
            arg_ast_ptr = &arg_ast->child[1];
7652
3.28k
            uses_named_args = true;
7653
7654
18.6k
            for (uint32_t k = 0; k < j; k++) {
7655
15.4k
              if (attr->args[k].name &&
7656
12.8k
                  zend_string_equals(attr->args[k].name, attr->args[j].name)) {
7657
5
                zend_error_noreturn(E_COMPILE_ERROR, "Duplicate named parameter $%s",
7658
5
                  ZSTR_VAL(attr->args[j].name));
7659
5
              }
7660
15.4k
            }
7661
76.9k
          } else if (uses_named_args) {
7662
26
            zend_error_noreturn(E_COMPILE_ERROR,
7663
26
              "Cannot use positional argument after named argument");
7664
26
          }
7665
7666
80.2k
          zend_const_expr_to_zval(
7667
80.2k
            &attr->args[j].value, arg_ast_ptr, /* allow_dynamic */ true);
7668
80.2k
        }
7669
64.6k
      }
7670
2.44M
    }
7671
1.40M
  }
7672
7673
1.39M
  if (*attributes != NULL) {
7674
    /* Allow delaying target validation for forward compatibility. */
7675
1.39M
    const zend_attribute *delayed_target_validation = NULL;
7676
1.39M
    if (target == ZEND_ATTRIBUTE_TARGET_PARAMETER) {
7677
63.0k
      ZEND_ASSERT(offset >= 1);
7678
      /* zend_get_parameter_attribute_str will add 1 too */
7679
63.0k
      delayed_target_validation = zend_get_parameter_attribute_str(
7680
63.0k
        *attributes,
7681
63.0k
        "delayedtargetvalidation",
7682
63.0k
        strlen("delayedtargetvalidation"),
7683
63.0k
        offset - 1
7684
63.0k
      );
7685
1.33M
    } else {
7686
1.33M
      delayed_target_validation = zend_get_attribute_str(
7687
1.33M
        *attributes,
7688
1.33M
        "delayedtargetvalidation",
7689
1.33M
        strlen("delayedtargetvalidation")
7690
1.33M
      );
7691
1.33M
    }
7692
    /* Validate attributes in a secondary loop (needed to detect repeated attributes). */
7693
9.64M
    ZEND_HASH_PACKED_FOREACH_PTR(*attributes, attr) {
7694
9.64M
      if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) {
7695
3.42M
        continue;
7696
3.42M
      }
7697
7698
9.64M
      bool run_validator = true;
7699
3.60k
      if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7700
145
        if (delayed_target_validation == NULL) {
7701
16
          zend_string *location = zend_get_attribute_target_names(target);
7702
16
          zend_string *allowed = zend_get_attribute_target_names(config->flags);
7703
7704
16
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
7705
16
            ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
7706
16
          );
7707
16
        }
7708
129
        run_validator = false;
7709
129
      }
7710
7711
3.58k
      if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
7712
3.58k
        if (zend_is_attribute_repeated(*attributes, attr)) {
7713
11
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name));
7714
11
        }
7715
3.58k
      }
7716
7717
      /* Validators are not run if the target is already invalid */
7718
3.57k
      if (run_validator && config->validator != NULL) {
7719
1.30k
        zend_string *error = config->validator(attr, target, CG(active_class_entry));
7720
1.30k
        if (error != NULL) {
7721
229
          if (delayed_target_validation == NULL) {
7722
41
            zend_error_noreturn(E_COMPILE_ERROR, "%s", ZSTR_VAL(error));
7723
0
            zend_string_efree(error);
7724
188
          } else {
7725
188
            attr->validation_error = error;
7726
188
          }
7727
229
        }
7728
1.30k
      }
7729
3.57k
    } ZEND_HASH_FOREACH_END();
7730
1.39M
  }
7731
1.39M
}
7732
/* }}} */
7733
7734
static void zend_compile_property_hooks(
7735
    zend_property_info *prop_info, zend_string *prop_name,
7736
    zend_ast *prop_type_ast, const zend_ast_list *hooks);
7737
7738
typedef struct {
7739
  const zend_string *property_name;
7740
  bool uses_property;
7741
} find_property_usage_context;
7742
7743
static void zend_property_hook_find_property_usage(zend_ast **ast_ptr, void *_context) /* {{{ */
7744
23.9k
{
7745
23.9k
  zend_ast *ast = *ast_ptr;
7746
23.9k
  find_property_usage_context *context = (find_property_usage_context *) _context;
7747
7748
23.9k
  if (ast == NULL) {
7749
430
    return;
7750
23.5k
  } else if (ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_NULLSAFE_PROP) {
7751
1.92k
    const zend_ast *object_ast = ast->child[0];
7752
1.92k
    zend_ast *property_ast = ast->child[1];
7753
7754
1.92k
    if (object_ast->kind == ZEND_AST_VAR
7755
1.53k
     && object_ast->child[0]->kind == ZEND_AST_ZVAL
7756
1.51k
     && property_ast->kind == ZEND_AST_ZVAL) {
7757
1.30k
      const zval *object = zend_ast_get_zval(object_ast->child[0]);
7758
1.30k
      const zval *property = zend_ast_get_zval(property_ast);
7759
1.30k
      if (Z_TYPE_P(object) == IS_STRING
7760
1.23k
        && Z_TYPE_P(property) == IS_STRING
7761
1.23k
        && zend_string_equals_literal(Z_STR_P(object), "this")
7762
748
        && zend_string_equals(Z_STR_P(property), context->property_name)) {
7763
233
        context->uses_property = true;
7764
        /* No need to look for references in this branch. */
7765
233
        return;
7766
233
      }
7767
1.30k
    }
7768
1.92k
  }
7769
7770
  /* Don't search across function/class boundaries. */
7771
23.3k
  if (!zend_ast_is_special(ast)) {
7772
13.9k
    zend_ast_apply(ast, zend_property_hook_find_property_usage, context);
7773
13.9k
  }
7774
23.3k
}
7775
7776
static bool zend_property_hook_uses_property(const zend_string *property_name, const zend_string *hook_name, zend_ast *hook_ast)
7777
3.32k
{
7778
3.32k
  if (zend_string_equals_literal_ci(hook_name, "set")
7779
956
   && hook_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
7780
374
    return true;
7781
374
  }
7782
7783
2.95k
  find_property_usage_context context = { property_name, false };
7784
2.95k
  zend_property_hook_find_property_usage(&hook_ast, &context);
7785
2.95k
  return context.uses_property;
7786
3.32k
}
7787
7788
static bool zend_property_is_virtual(const zend_class_entry *ce, const zend_string *property_name, zend_ast *hooks_ast, uint32_t flags)
7789
31.3k
{
7790
31.3k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
7791
202
    return true;
7792
202
  }
7793
31.1k
  if (!hooks_ast) {
7794
28.8k
    return false;
7795
28.8k
  }
7796
7797
31.1k
  bool is_virtual = true;
7798
7799
2.27k
  const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
7800
5.96k
  for (uint32_t i = 0; i < hooks->children; i++) {
7801
3.69k
    const zend_ast_decl *hook = (const zend_ast_decl *) hooks->child[i];
7802
3.69k
    zend_ast *body = hook->child[2];
7803
3.69k
    if (body && zend_property_hook_uses_property(property_name, hook->name, body)) {
7804
585
      is_virtual = false;
7805
585
    }
7806
3.69k
  }
7807
7808
2.27k
  return is_virtual;
7809
31.1k
}
7810
7811
static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */
7812
1.41M
{
7813
1.41M
  zend_ast_list *list = zend_ast_get_list(ast);
7814
1.41M
  uint32_t i;
7815
1.41M
  zend_op_array *op_array = CG(active_op_array);
7816
1.41M
  zend_arg_info *arg_infos;
7817
7818
1.41M
  if (return_type_ast || fallback_return_type) {
7819
    /* Use op_array->arg_info[-1] for return type */
7820
20.5k
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
7821
20.5k
    arg_infos->name = NULL;
7822
20.5k
    if (return_type_ast) {
7823
19.8k
      arg_infos->type = zend_compile_typename(return_type_ast);
7824
19.8k
      ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS(
7825
19.8k
        (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0, /* is_tentative */ 0);
7826
19.8k
    } else {
7827
749
      arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0);
7828
749
    }
7829
20.5k
    arg_infos++;
7830
20.5k
    op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
7831
7832
20.5k
    if (ZEND_TYPE_CONTAINS_CODE(arg_infos[-1].type, IS_VOID)
7833
3.06k
        && (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
7834
150
      zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7835
150
      zend_error(E_DEPRECATED, "%s(): Returning by reference from a void function is deprecated", ZSTR_VAL(func_name));
7836
150
      zend_string_release(func_name);
7837
150
    }
7838
1.39M
  } else {
7839
1.39M
    if (list->children == 0) {
7840
51.4k
      return;
7841
51.4k
    }
7842
1.34M
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
7843
1.34M
  }
7844
7845
  /* Find last required parameter number for deprecation message. */
7846
1.36M
  uint32_t last_required_param = (uint32_t) -1;
7847
2.79M
  for (i = 0; i < list->children; ++i) {
7848
1.42M
    zend_ast *param_ast = list->child[i];
7849
1.42M
    zend_ast *default_ast_ptr = param_ast->child[2];
7850
1.42M
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
7851
1.42M
    if (!default_ast_ptr && !is_variadic) {
7852
1.41M
      last_required_param = i;
7853
1.41M
    }
7854
1.42M
  }
7855
7856
1.36M
  const uint32_t promotion_flags = ZEND_ACC_PPP_MASK | ZEND_ACC_PPP_SET_MASK | ZEND_ACC_READONLY | ZEND_ACC_FINAL;
7857
2.79M
  for (i = 0; i < list->children; ++i) {
7858
1.42M
    zend_ast *param_ast = list->child[i];
7859
1.42M
    zend_ast *type_ast = param_ast->child[0];
7860
1.42M
    zend_ast *var_ast = param_ast->child[1];
7861
1.42M
    zend_ast **default_ast_ptr = &param_ast->child[2];
7862
1.42M
    zend_ast *attributes_ast = param_ast->child[3];
7863
1.42M
    zend_ast *doc_comment_ast = param_ast->child[4];
7864
1.42M
    zend_ast *hooks_ast = param_ast->child[5];
7865
1.42M
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast));
7866
1.42M
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
7867
1.42M
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
7868
1.42M
    uint32_t property_flags = param_ast->attr & promotion_flags;
7869
1.42M
    bool is_promoted = property_flags || hooks_ast;
7870
7871
1.42M
    CG(zend_lineno) = param_ast->lineno;
7872
7873
1.42M
    znode var_node, default_node;
7874
1.42M
    uint8_t opcode;
7875
1.42M
    zend_op *opline;
7876
1.42M
    zend_arg_info *arg_info;
7877
7878
1.42M
    if (zend_is_auto_global(name)) {
7879
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
7880
2
        ZSTR_VAL(name));
7881
2
    }
7882
7883
1.42M
    var_node.op_type = IS_CV;
7884
1.42M
    var_node.u.op.var = lookup_cv(name);
7885
7886
1.42M
    if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
7887
16
      zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
7888
16
        ZSTR_VAL(name));
7889
1.42M
    } else if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
7890
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
7891
1.42M
    } else if (zend_string_equals_literal(name, "http_response_header")) {
7892
105
      CG(context).has_assigned_to_http_response_header = true;
7893
105
    }
7894
7895
1.42M
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
7896
3
      zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
7897
3
    }
7898
7899
1.42M
    if (is_variadic) {
7900
842
      opcode = ZEND_RECV_VARIADIC;
7901
842
      default_node.op_type = IS_UNUSED;
7902
842
      op_array->fn_flags |= ZEND_ACC_VARIADIC;
7903
7904
842
      if (*default_ast_ptr) {
7905
2
        zend_error_noreturn(E_COMPILE_ERROR,
7906
2
          "Variadic parameter cannot have a default value");
7907
2
      }
7908
1.42M
    } else if (*default_ast_ptr) {
7909
      /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
7910
10.3k
      uint32_t cops = CG(compiler_options);
7911
10.3k
      CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
7912
10.3k
      opcode = ZEND_RECV_INIT;
7913
10.3k
      default_node.op_type = IS_CONST;
7914
10.3k
      zend_const_expr_to_zval(
7915
10.3k
        &default_node.u.constant, default_ast_ptr, /* allow_dynamic */ true);
7916
10.3k
      CG(compiler_options) = cops;
7917
1.41M
    } else {
7918
1.41M
      opcode = ZEND_RECV;
7919
1.41M
      default_node.op_type = IS_UNUSED;
7920
1.41M
      op_array->required_num_args = i + 1;
7921
1.41M
    }
7922
7923
1.42M
    arg_info = &arg_infos[i];
7924
1.42M
    arg_info->name = zend_string_copy(name);
7925
1.42M
    arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0);
7926
1.42M
    arg_info->default_value = NULL;
7927
7928
1.42M
    if (attributes_ast) {
7929
63.0k
      zend_compile_attributes(
7930
63.0k
        &op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER,
7931
63.0k
        is_promoted ? ZEND_ATTRIBUTE_TARGET_PROPERTY : 0
7932
63.0k
      );
7933
63.0k
    }
7934
7935
1.42M
    bool forced_allow_nullable = false;
7936
1.42M
    if (type_ast) {
7937
353k
      uint32_t default_type = *default_ast_ptr ? Z_TYPE(default_node.u.constant) : IS_UNDEF;
7938
353k
      bool force_nullable = default_type == IS_NULL && !is_promoted;
7939
7940
353k
      op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
7941
353k
      arg_info->type = zend_compile_typename_ex(type_ast, force_nullable, &forced_allow_nullable);
7942
353k
      if (forced_allow_nullable) {
7943
4.78k
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7944
4.78k
        zend_error(E_DEPRECATED,
7945
4.78k
           "%s(): Implicitly marking parameter $%s as nullable is deprecated, the explicit nullable type "
7946
4.78k
           "must be used instead", ZSTR_VAL(func_name), ZSTR_VAL(name));
7947
4.78k
        zend_string_release(func_name);
7948
4.78k
      }
7949
7950
353k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) {
7951
2
        zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");
7952
2
      }
7953
7954
353k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_NEVER) {
7955
3
        zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type");
7956
3
      }
7957
7958
353k
      if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable
7959
707
          && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) {
7960
14
        zend_string *type_str = zend_type_to_string(arg_info->type);
7961
14
        zend_error_noreturn(E_COMPILE_ERROR,
7962
14
          "Cannot use %s as default value for parameter $%s of type %s",
7963
14
          zend_get_type_by_const(default_type),
7964
14
          ZSTR_VAL(name), ZSTR_VAL(type_str));
7965
14
      }
7966
353k
    }
7967
1.42M
    if (last_required_param != (uint32_t) -1
7968
1.42M
     && i < last_required_param
7969
73.0k
     && default_node.op_type == IS_CONST) {
7970
      /* Ignore parameters of the form "Type $param = null".
7971
       * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
7972
5.92k
      if (!forced_allow_nullable) {
7973
1.69k
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7974
1.69k
        zend_ast *required_param_ast = list->child[last_required_param];
7975
1.69k
        zend_error(E_DEPRECATED,
7976
1.69k
          "%s(): Optional parameter $%s declared before required parameter $%s "
7977
1.69k
          "is implicitly treated as a required parameter",
7978
1.69k
          ZSTR_VAL(func_name), ZSTR_VAL(name), ZSTR_VAL(zend_ast_get_str(required_param_ast->child[1])));
7979
1.69k
        zend_string_release(func_name);
7980
1.69k
      }
7981
7982
      /* Regardless of whether we issue a deprecation, convert this parameter into
7983
       * a required parameter without a default value. This ensures that it cannot be
7984
       * used as an optional parameter even with named parameters. */
7985
5.92k
      opcode = ZEND_RECV;
7986
5.92k
      default_node.op_type = IS_UNUSED;
7987
5.92k
      zval_ptr_dtor(&default_node.u.constant);
7988
5.92k
    }
7989
7990
1.42M
    opline = zend_emit_op(NULL, opcode, NULL, &default_node);
7991
1.42M
    SET_NODE(opline->result, &var_node);
7992
1.42M
    opline->op1.num = i + 1;
7993
7994
1.42M
    uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic, /* is_tentative */ 0)
7995
1.42M
      | (is_promoted ? _ZEND_IS_PROMOTED_BIT : 0);
7996
1.42M
    ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags;
7997
1.42M
    if (opcode == ZEND_RECV) {
7998
1.41M
      opline->op2.num = type_ast ?
7999
1.07M
        ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY;
8000
1.41M
    }
8001
8002
1.42M
    if (is_promoted) {
8003
288
      const zend_op_array *active_op_array = CG(active_op_array);
8004
288
      zend_class_entry *scope = active_op_array->scope;
8005
8006
288
      bool is_ctor =
8007
288
        scope && zend_is_constructor(active_op_array->function_name);
8008
288
      if (!is_ctor) {
8009
28
        zend_error_noreturn(E_COMPILE_ERROR,
8010
28
          "Cannot declare promoted property outside a constructor");
8011
28
      }
8012
260
      if ((active_op_array->fn_flags & ZEND_ACC_ABSTRACT)
8013
258
          || (scope->ce_flags & ZEND_ACC_INTERFACE)) {
8014
2
        zend_error_noreturn(E_COMPILE_ERROR,
8015
2
          "Cannot declare promoted property in an abstract constructor");
8016
2
      }
8017
258
      if (is_variadic) {
8018
2
        zend_error_noreturn(E_COMPILE_ERROR,
8019
2
          "Cannot declare variadic promoted property");
8020
2
      }
8021
256
      if (zend_hash_exists(&scope->properties_info, name)) {
8022
2
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
8023
2
          ZSTR_VAL(scope->name), ZSTR_VAL(name));
8024
2
      }
8025
254
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_CALLABLE) {
8026
2
        zend_string *str = zend_type_to_string(arg_info->type);
8027
2
        zend_error_noreturn(E_COMPILE_ERROR,
8028
2
          "Property %s::$%s cannot have type %s",
8029
2
          ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str));
8030
2
      }
8031
8032
252
      if (!(property_flags & ZEND_ACC_READONLY) && (scope->ce_flags & ZEND_ACC_READONLY_CLASS)) {
8033
6
        property_flags |= ZEND_ACC_READONLY;
8034
6
      }
8035
8036
      /* Recompile the type, as it has different memory management requirements. */
8037
252
      zend_type type = ZEND_TYPE_INIT_NONE(0);
8038
252
      if (type_ast) {
8039
172
        type = zend_compile_typename(type_ast);
8040
172
      }
8041
8042
      /* Don't give the property an explicit default value. For typed properties this means
8043
       * uninitialized, for untyped properties it means an implicit null default value.
8044
       * Properties with hooks get an implicit default value of undefined until inheritance,
8045
       * where it is changed to null only once we know it is not virtual. If we were to set it
8046
       * here, we couldn't verify that a true virtual property must not have an explicit
8047
       * default value. */
8048
252
      zval default_value;
8049
252
      if (ZEND_TYPE_IS_SET(type) || hooks_ast) {
8050
213
        ZVAL_UNDEF(&default_value);
8051
213
      } else {
8052
39
        if (property_flags & ZEND_ACC_READONLY) {
8053
4
          zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
8054
4
            ZSTR_VAL(scope->name), ZSTR_VAL(name));
8055
4
        }
8056
8057
35
        ZVAL_NULL(&default_value);
8058
35
      }
8059
8060
248
      zend_string *doc_comment =
8061
248
        doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
8062
248
      zend_property_info *prop = zend_declare_typed_property(
8063
248
        scope, name, &default_value,
8064
248
        property_flags | (zend_property_is_virtual(scope, name, hooks_ast, property_flags) ? ZEND_ACC_VIRTUAL : 0) | ZEND_ACC_PROMOTED,
8065
248
        doc_comment, type);
8066
248
      if (hooks_ast) {
8067
108
        const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
8068
108
        zend_compile_property_hooks(prop, name, type_ast, hooks);
8069
108
      }
8070
248
      if (attributes_ast) {
8071
46
        zend_compile_attributes(
8072
46
          &prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, ZEND_ATTRIBUTE_TARGET_PARAMETER);
8073
8074
46
        zend_attribute *override_attribute = zend_get_attribute_str(prop->attributes, "override", sizeof("override")-1);
8075
46
        if (override_attribute) {
8076
2
          prop->flags |= ZEND_ACC_OVERRIDE;
8077
2
        }
8078
46
      }
8079
248
    }
8080
1.42M
  }
8081
8082
  /* These are assigned at the end to avoid uninitialized memory in case of an error */
8083
1.36M
  op_array->num_args = list->children;
8084
1.36M
  op_array->arg_info = arg_infos;
8085
8086
  /* Don't count the variadic argument */
8087
1.36M
  if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
8088
835
    op_array->num_args--;
8089
835
  }
8090
1.36M
  zend_set_function_arg_flags((zend_function*)op_array);
8091
8092
2.79M
  for (i = 0; i < list->children; i++) {
8093
1.42M
    zend_ast *param_ast = list->child[i];
8094
1.42M
    zend_ast *hooks_ast = param_ast->child[5];
8095
1.42M
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
8096
1.42M
    uint32_t flags = param_ast->attr & promotion_flags;
8097
1.42M
    bool is_promoted = flags || hooks_ast;
8098
1.42M
    if (!is_promoted) {
8099
1.42M
      continue;
8100
1.42M
    }
8101
8102
243
    CG(zend_lineno) = param_ast->lineno;
8103
8104
    /* Emit $this->prop = $prop for promoted properties. */
8105
243
    zend_string *name = zend_ast_get_str(param_ast->child[1]);
8106
243
    znode name_node, value_node;
8107
243
    name_node.op_type = IS_CONST;
8108
243
    ZVAL_STR_COPY(&name_node.u.constant, name);
8109
243
    value_node.op_type = IS_CV;
8110
243
    value_node.u.op.var = lookup_cv(name);
8111
8112
243
    zend_op *opline = zend_emit_op(NULL,
8113
243
      is_ref ? ZEND_ASSIGN_OBJ_REF : ZEND_ASSIGN_OBJ, NULL, &name_node);
8114
243
    opline->extended_value = zend_alloc_cache_slots(3);
8115
243
    zend_emit_op_data(&value_node);
8116
243
  }
8117
1.36M
}
8118
/* }}} */
8119
8120
static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */
8121
791
{
8122
791
  const zend_ast_list *list = zend_ast_get_list(uses_ast);
8123
791
  uint32_t i;
8124
8125
791
  if (!list->children) {
8126
0
    return;
8127
0
  }
8128
8129
791
  if (!op_array->static_variables) {
8130
791
    op_array->static_variables = zend_new_array(8);
8131
791
  }
8132
8133
4.23k
  for (i = 0; i < list->children; ++i) {
8134
3.45k
    zend_ast *var_name_ast = list->child[i];
8135
3.45k
    zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast));
8136
3.45k
    uint32_t mode = var_name_ast->attr;
8137
3.45k
    zend_op *opline;
8138
3.45k
    zval *value;
8139
8140
3.45k
    if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8141
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
8142
2
    }
8143
8144
3.45k
    if (zend_is_auto_global(var_name)) {
8145
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable");
8146
2
    }
8147
8148
3.45k
    value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval));
8149
3.45k
    if (!value) {
8150
7
      zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8151
7
        "Cannot use variable $%S twice", var_name);
8152
7
    }
8153
8154
3.44k
    CG(zend_lineno) = zend_ast_get_lineno(var_name_ast);
8155
8156
3.44k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8157
3.44k
    opline->op2_type = IS_CV;
8158
3.44k
    opline->op2.var = lookup_cv(var_name);
8159
3.44k
    opline->extended_value =
8160
3.44k
      (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode;
8161
3.44k
  }
8162
791
}
8163
/* }}} */
8164
8165
typedef struct {
8166
  HashTable uses;
8167
  bool varvars_used;
8168
} closure_info;
8169
8170
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast);
8171
8172
1.34M
static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
8173
1.34M
  if (!ast) {
8174
6.38k
    return;
8175
6.38k
  }
8176
8177
1.33M
  if (ast->kind == ZEND_AST_VAR) {
8178
144k
    zend_ast *name_ast = ast->child[0];
8179
144k
    if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) {
8180
142k
      zend_string *name = zend_ast_get_str(name_ast);
8181
142k
      if (zend_is_auto_global(name)) {
8182
        /* These is no need to explicitly import auto-globals. */
8183
4.66k
        return;
8184
4.66k
      }
8185
8186
138k
      if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8187
        /* $this does not need to be explicitly imported. */
8188
4.17k
        return;
8189
4.17k
      }
8190
8191
133k
      zend_hash_add_empty_element(&info->uses, name);
8192
133k
    } else {
8193
1.89k
      info->varvars_used = true;
8194
1.89k
      find_implicit_binds_recursively(info, name_ast);
8195
1.89k
    }
8196
1.19M
  } else if (zend_ast_is_list(ast)) {
8197
28.4k
    const zend_ast_list *list = zend_ast_get_list(ast);
8198
28.4k
    uint32_t i;
8199
255k
    for (i = 0; i < list->children; i++) {
8200
226k
      find_implicit_binds_recursively(info, list->child[i]);
8201
226k
    }
8202
1.16M
  } else if (ast->kind == ZEND_AST_CLOSURE) {
8203
    /* For normal closures add the use() list. */
8204
406
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8205
406
    zend_ast *uses_ast = closure_ast->child[1];
8206
406
    if (uses_ast) {
8207
337
      const zend_ast_list *uses_list = zend_ast_get_list(uses_ast);
8208
337
      uint32_t i;
8209
2.27k
      for (i = 0; i < uses_list->children; i++) {
8210
1.93k
        zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i]));
8211
1.93k
      }
8212
337
    }
8213
1.16M
  } else if (ast->kind == ZEND_AST_ARROW_FUNC) {
8214
    /* For arrow functions recursively check the expression. */
8215
436k
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8216
436k
    closure_info inner_info;
8217
436k
    find_implicit_binds(&inner_info, closure_ast->child[0], closure_ast->child[2]);
8218
436k
    if (inner_info.varvars_used) {
8219
3.93k
      info->varvars_used = true;
8220
3.93k
    }
8221
436k
    if (zend_hash_num_elements(&inner_info.uses)) {
8222
382k
      zend_hash_copy(&info->uses, &inner_info.uses, NULL);
8223
382k
    }
8224
436k
    zend_hash_destroy(&inner_info.uses);
8225
728k
  } else if (!zend_ast_is_special(ast)) {
8226
554k
    uint32_t i, children = zend_ast_get_num_children(ast);
8227
1.20M
    for (i = 0; i < children; i++) {
8228
653k
      find_implicit_binds_recursively(info, ast->child[i]);
8229
653k
    }
8230
554k
  }
8231
1.33M
}
8232
8233
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast)
8234
462k
{
8235
462k
  const zend_ast_list *param_list = zend_ast_get_list(params_ast);
8236
462k
  uint32_t i;
8237
8238
462k
  zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0);
8239
462k
  info->varvars_used = false;
8240
8241
462k
  find_implicit_binds_recursively(info, stmt_ast);
8242
8243
  /* Remove variables that are parameters */
8244
522k
  for (i = 0; i < param_list->children; i++) {
8245
59.8k
    const zend_ast *param_ast = param_list->child[i];
8246
59.8k
    zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1]));
8247
59.8k
  }
8248
462k
}
8249
8250
static void compile_implicit_lexical_binds(
8251
    const closure_info *info, znode *closure, zend_op_array *op_array)
8252
25.4k
{
8253
25.4k
  zend_string *var_name;
8254
25.4k
  zend_op *opline;
8255
8256
  /* TODO We might want to use a special binding mode if varvars_used is set. */
8257
25.4k
  if (zend_hash_num_elements(&info->uses) == 0) {
8258
17.0k
    return;
8259
17.0k
  }
8260
8261
8.46k
  if (!op_array->static_variables) {
8262
8.46k
    op_array->static_variables = zend_new_array(8);
8263
8.46k
  }
8264
8265
146k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8266
146k
    zval *value = zend_hash_add(
8267
146k
      op_array->static_variables, var_name, &EG(uninitialized_zval));
8268
146k
    uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData);
8269
8270
146k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8271
146k
    opline->op2_type = IS_CV;
8272
146k
    opline->op2.var = lookup_cv(var_name);
8273
146k
    opline->extended_value = offset | ZEND_BIND_IMPLICIT;
8274
146k
  ZEND_HASH_FOREACH_END();
8275
8.46k
}
8276
8277
static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
8278
780
{
8279
780
  const zend_op_array *op_array = CG(active_op_array);
8280
780
  const zend_ast_list *list = zend_ast_get_list(ast);
8281
780
  uint32_t i;
8282
8283
4.19k
  for (i = 0; i < list->children; ++i) {
8284
3.41k
    uint32_t mode = ZEND_BIND_EXPLICIT;
8285
3.41k
    zend_ast *var_ast = list->child[i];
8286
3.41k
    zend_string *var_name = zend_ast_get_str(var_ast);
8287
3.41k
    zval zv;
8288
3.41k
    ZVAL_NULL(&zv);
8289
8290
3.41k
    {
8291
3.41k
      int i;
8292
12.4k
      for (i = 0; i < op_array->last_var; i++) {
8293
9.01k
        if (zend_string_equals(op_array->vars[i], var_name)) {
8294
2
          zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8295
2
            "Cannot use lexical variable $%S as a parameter name", var_name);
8296
2
        }
8297
9.01k
      }
8298
3.41k
    }
8299
8300
3.41k
    CG(zend_lineno) = zend_ast_get_lineno(var_ast);
8301
8302
3.41k
    if (var_ast->attr) {
8303
514
      mode |= ZEND_BIND_REF;
8304
514
    }
8305
8306
3.41k
    zend_compile_static_var_common(var_name, &zv, mode);
8307
3.41k
  }
8308
780
}
8309
/* }}} */
8310
8311
static void zend_compile_implicit_closure_uses(const closure_info *info)
8312
25.4k
{
8313
25.4k
  zend_string *var_name;
8314
180k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8315
180k
    zval zv;
8316
180k
    ZVAL_NULL(&zv);
8317
180k
    zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT);
8318
180k
  ZEND_HASH_FOREACH_END();
8319
25.4k
}
8320
8321
840
static void add_stringable_interface(zend_class_entry *ce) {
8322
1.49k
  for (uint32_t i = 0; i < ce->num_interfaces; i++) {
8323
657
    if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) {
8324
      /* Interface already explicitly implemented */
8325
1
      return;
8326
1
    }
8327
657
  }
8328
8329
839
  ce->num_interfaces++;
8330
839
  ce->interface_names =
8331
839
    erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
8332
  // TODO: Add known interned strings instead?
8333
839
  ce->interface_names[ce->num_interfaces - 1].name =
8334
839
    ZSTR_INIT_LITERAL("Stringable", 0);
8335
839
  ce->interface_names[ce->num_interfaces - 1].lc_name =
8336
839
    ZSTR_INIT_LITERAL("stringable", 0);
8337
839
}
8338
8339
static zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, bool has_body) /* {{{ */
8340
25.7k
{
8341
25.7k
  zend_class_entry *ce = CG(active_class_entry);
8342
25.7k
  bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
8343
25.7k
  uint32_t fn_flags = op_array->fn_flags;
8344
8345
25.7k
  zend_string *lcname;
8346
8347
25.7k
  if (fn_flags & ZEND_ACC_READONLY) {
8348
0
    zend_error(E_COMPILE_ERROR, "Cannot use 'readonly' as method modifier");
8349
0
  }
8350
8351
25.7k
  if ((fn_flags & ZEND_ACC_PRIVATE) && (fn_flags & ZEND_ACC_FINAL) && !zend_is_constructor(name)) {
8352
112
    zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes");
8353
112
  }
8354
8355
25.7k
  if ((fn_flags & ZEND_ACC_ABSTRACT)
8356
122
   && !(ce->ce_flags & (ZEND_ACC_EXPLICIT_ABSTRACT_CLASS|ZEND_ACC_TRAIT))) {
8357
    // Don't say that the class should be declared abstract if it is
8358
    // anonymous or an enum and can't be abstract
8359
13
    if (ce->ce_flags & ZEND_ACC_ANON_CLASS) {
8360
2
      zend_error_noreturn(E_COMPILE_ERROR, "Anonymous class method %s() must not be abstract",
8361
2
        ZSTR_VAL(name));
8362
11
    } else if (ce->ce_flags & (ZEND_ACC_ENUM|ZEND_ACC_INTERFACE)) {
8363
9
      zend_error_noreturn(E_COMPILE_ERROR, "%s method %s::%s() must not be abstract",
8364
9
        zend_get_object_type_case(ce, true), ZSTR_VAL(ce->name), ZSTR_VAL(name));
8365
9
    } else {
8366
2
      zend_error_noreturn(E_COMPILE_ERROR, "Class %s declares abstract method %s() and must therefore be declared abstract",
8367
2
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
8368
2
    }
8369
13
  }
8370
8371
25.7k
  if (in_interface) {
8372
278
    if (!(fn_flags & ZEND_ACC_PUBLIC)) {
8373
1
      zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
8374
1
        "%s::%s() must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8375
1
    }
8376
277
    if (fn_flags & ZEND_ACC_FINAL) {
8377
2
      zend_error_noreturn(E_COMPILE_ERROR, "Interface method "
8378
2
        "%s::%s() must not be final", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8379
2
    }
8380
275
    op_array->fn_flags |= ZEND_ACC_ABSTRACT;
8381
275
  }
8382
8383
25.7k
  if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
8384
384
    if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8385
2
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
8386
2
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8387
2
    }
8388
8389
382
    if (has_body) {
8390
3
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
8391
3
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8392
3
    }
8393
8394
379
    ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
8395
25.3k
  } else if (!has_body) {
8396
3
    zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
8397
3
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8398
3
  }
8399
8400
25.7k
  op_array->scope = ce;
8401
25.7k
  op_array->function_name = zend_string_copy(name);
8402
8403
25.7k
  lcname = zend_string_tolower(name);
8404
25.7k
  lcname = zend_new_interned_string(lcname);
8405
8406
25.7k
  if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
8407
15
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
8408
15
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8409
15
  }
8410
8411
25.7k
  zend_add_magic_method(ce, (zend_function *) op_array, lcname);
8412
25.7k
  if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)
8413
842
      && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8414
840
    add_stringable_interface(ce);
8415
840
  }
8416
8417
25.7k
  return lcname;
8418
25.7k
}
8419
/* }}} */
8420
8421
1.38M
static uint32_t zend_add_dynamic_func_def(zend_op_array *def) {
8422
1.38M
  zend_op_array *op_array = CG(active_op_array);
8423
1.38M
  uint32_t def_offset = op_array->num_dynamic_func_defs++;
8424
1.38M
  op_array->dynamic_func_defs = erealloc(
8425
1.38M
    op_array->dynamic_func_defs, op_array->num_dynamic_func_defs * sizeof(zend_op_array *));
8426
1.38M
  op_array->dynamic_func_defs[def_offset] = def;
8427
1.38M
  return def_offset;
8428
1.38M
}
8429
8430
enum func_decl_level {
8431
  FUNC_DECL_LEVEL_TOPLEVEL,
8432
  FUNC_DECL_LEVEL_NESTED,
8433
  FUNC_DECL_LEVEL_CONSTEXPR,
8434
};
8435
8436
static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array, const zend_ast_decl *decl, enum func_decl_level level) /* {{{ */
8437
1.38M
{
8438
1.38M
  zend_string *unqualified_name, *name, *lcname;
8439
1.38M
  zend_op *opline;
8440
8441
1.38M
  if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8442
1.37M
    zend_string *filename = op_array->filename;
8443
1.37M
    uint32_t start_lineno = decl->start_lineno;
8444
8445
1.37M
    zend_string *class = zend_empty_string;
8446
1.37M
    zend_string *separator = zend_empty_string;
8447
1.37M
    zend_string *function = filename;
8448
1.37M
    const char *parens = "";
8449
8450
1.37M
    if (CG(active_op_array) && CG(active_op_array)->function_name) {
8451
1.37M
      if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
8452
        /* If the parent function is a closure, don't redundantly
8453
         * add the classname and parentheses.
8454
         */
8455
1.36M
        function = CG(active_op_array)->function_name;
8456
1.36M
      } else {
8457
3.79k
        function = CG(active_op_array)->function_name;
8458
3.79k
        parens = "()";
8459
8460
3.79k
        if (CG(active_class_entry) && CG(active_class_entry)->name) {
8461
3.58k
          class = CG(active_class_entry)->name;
8462
3.58k
          separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM);
8463
3.58k
        }
8464
3.79k
      }
8465
1.37M
    }
8466
8467
1.37M
    unqualified_name = zend_strpprintf_unchecked(
8468
1.37M
      0,
8469
1.37M
      "{closure:%S%S%S%s:%" PRIu32 "}",
8470
1.37M
      class,
8471
1.37M
      separator,
8472
1.37M
      function,
8473
1.37M
      parens,
8474
1.37M
      start_lineno
8475
1.37M
    );
8476
8477
1.37M
    op_array->function_name = name = unqualified_name;
8478
1.37M
  } else {
8479
11.2k
    unqualified_name = decl->name;
8480
11.2k
    op_array->function_name = name = zend_prefix_with_ns(unqualified_name);
8481
11.2k
  }
8482
8483
1.38M
  lcname = zend_string_tolower(name);
8484
8485
1.38M
  if (FC(imports_function)) {
8486
16
    const zend_string *import_name =
8487
16
      zend_hash_find_ptr_lc(FC(imports_function), unqualified_name);
8488
16
    if (import_name && !zend_string_equals_ci(lcname, import_name)) {
8489
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare function %s() (previously declared as local import)",
8490
5
        ZSTR_VAL(name));
8491
5
    }
8492
16
  }
8493
8494
1.38M
  if (zend_string_equals_literal(lcname, "__autoload")) {
8495
1
    zend_error_noreturn(E_COMPILE_ERROR,
8496
1
      "__autoload() is no longer supported, use spl_autoload_register() instead");
8497
1
  }
8498
8499
1.38M
  if (zend_string_equals_literal_ci(unqualified_name, "assert")) {
8500
2
    zend_error(E_COMPILE_ERROR,
8501
2
      "Defining a custom assert() function is not allowed, "
8502
2
      "as the function has special semantics");
8503
2
  }
8504
8505
1.38M
  zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
8506
1.38M
  switch (level) {
8507
1.38M
    case FUNC_DECL_LEVEL_NESTED: {
8508
1.38M
      uint32_t func_ref = zend_add_dynamic_func_def(op_array);
8509
1.38M
      if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8510
1.37M
        opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
8511
1.37M
        opline->op2.num = func_ref;
8512
1.37M
      } else {
8513
8.34k
        opline = get_next_op();
8514
8.34k
        opline->opcode = ZEND_DECLARE_FUNCTION;
8515
8.34k
        opline->op1_type = IS_CONST;
8516
8.34k
        LITERAL_STR(opline->op1, zend_string_copy(lcname));
8517
8.34k
        opline->op2.num = func_ref;
8518
8.34k
      }
8519
1.38M
      break;
8520
0
    }
8521
22
    case FUNC_DECL_LEVEL_CONSTEXPR:
8522
2.94k
    case FUNC_DECL_LEVEL_TOPLEVEL:
8523
      /* Nothing to do. */
8524
2.94k
      break;
8525
1.38M
  }
8526
1.38M
  return lcname;
8527
1.38M
}
8528
/* }}} */
8529
8530
static zend_op_array *zend_compile_func_decl_ex(
8531
  znode *result, zend_ast *ast, enum func_decl_level level,
8532
  zend_string *property_info_name,
8533
  zend_property_hook_kind hook_kind
8534
1.41M
) {
8535
1.41M
  zend_ast_decl *decl = (zend_ast_decl *) ast;
8536
1.41M
  zend_ast *params_ast = decl->child[0];
8537
1.41M
  zend_ast *uses_ast = decl->child[1];
8538
1.41M
  zend_ast *stmt_ast = decl->child[2];
8539
1.41M
  zend_ast *return_type_ast = decl->child[3];
8540
1.41M
  bool is_method = decl->kind == ZEND_AST_METHOD;
8541
1.41M
  zend_string *lcname = NULL;
8542
1.41M
  bool is_hook = decl->kind == ZEND_AST_PROPERTY_HOOK;
8543
8544
1.41M
  zend_class_entry *orig_class_entry = CG(active_class_entry);
8545
1.41M
  zend_op_array *orig_op_array = CG(active_op_array);
8546
1.41M
  zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
8547
1.41M
  zend_oparray_context orig_oparray_context;
8548
1.41M
  closure_info info;
8549
8550
1.41M
  init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
8551
8552
1.41M
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
8553
0
    op_array->fn_flags |= ZEND_ACC_PRELOADED;
8554
0
  }
8555
8556
1.41M
  op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
8557
1.41M
  op_array->fn_flags |= decl->flags;
8558
1.41M
  op_array->line_start = decl->start_lineno;
8559
1.41M
  op_array->line_end = decl->end_lineno;
8560
1.41M
  if (decl->doc_comment) {
8561
895
    op_array->doc_comment = zend_string_copy(decl->doc_comment);
8562
895
  }
8563
8564
1.41M
  if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
8565
1.37M
    op_array->fn_flags |= ZEND_ACC_CLOSURE;
8566
1.37M
  }
8567
8568
1.41M
  if (is_hook) {
8569
2.62k
    zend_class_entry *ce = CG(active_class_entry);
8570
2.62k
    op_array->scope = ce;
8571
2.62k
    op_array->function_name = zend_string_copy(decl->name);
8572
1.41M
  } else if (is_method) {
8573
25.7k
    bool has_body = stmt_ast != NULL;
8574
25.7k
    lcname = zend_begin_method_decl(op_array, decl->name, has_body);
8575
1.38M
  } else {
8576
1.38M
    lcname = zend_begin_func_decl(result, op_array, decl, level);
8577
1.38M
    if (decl->kind == ZEND_AST_ARROW_FUNC) {
8578
25.4k
      find_implicit_binds(&info, params_ast, stmt_ast);
8579
25.4k
      compile_implicit_lexical_binds(&info, result, op_array);
8580
1.36M
    } else if (uses_ast) {
8581
791
      zend_compile_closure_binding(result, op_array, uses_ast);
8582
791
    }
8583
1.38M
  }
8584
8585
1.41M
  CG(active_op_array) = op_array;
8586
8587
1.41M
  zend_oparray_context_begin(&orig_oparray_context, op_array);
8588
1.41M
  CG(context).active_property_info_name = property_info_name;
8589
1.41M
  CG(context).active_property_hook_kind = hook_kind;
8590
8591
1.41M
  if (decl->child[4]) {
8592
1.33M
    int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
8593
8594
1.33M
    if (is_method || is_hook) {
8595
1.86k
      target = ZEND_ATTRIBUTE_TARGET_METHOD;
8596
1.86k
    }
8597
8598
1.33M
    zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target, 0);
8599
8600
1.33M
    const zend_attribute *override_attribute = zend_get_attribute_str(
8601
1.33M
      op_array->attributes,
8602
1.33M
      "override",
8603
1.33M
      sizeof("override")-1
8604
1.33M
    );
8605
8606
1.33M
    if (override_attribute) {
8607
1.63k
      op_array->fn_flags |= ZEND_ACC_OVERRIDE;
8608
1.63k
    }
8609
8610
1.33M
    const zend_attribute *deprecated_attribute = zend_get_attribute_str(
8611
1.33M
      op_array->attributes,
8612
1.33M
      "deprecated",
8613
1.33M
      sizeof("deprecated")-1
8614
1.33M
    );
8615
8616
1.33M
    if (deprecated_attribute) {
8617
53
      op_array->fn_flags |= ZEND_ACC_DEPRECATED;
8618
53
    }
8619
8620
    // ZEND_ACC_NODISCARD is added via an attribute validator
8621
1.33M
  }
8622
8623
  /* Do not leak the class scope into free standing functions, even if they are dynamically
8624
   * defined inside a class method. This is necessary for correct handling of magic constants.
8625
   * For example __CLASS__ should always be "" inside a free standing function. */
8626
1.41M
  if (decl->kind == ZEND_AST_FUNC_DECL) {
8627
11.2k
    CG(active_class_entry) = NULL;
8628
11.2k
  }
8629
8630
1.41M
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8631
2.91k
    op_array->fn_flags |= ZEND_ACC_TOP_LEVEL;
8632
2.91k
  }
8633
8634
1.41M
  {
8635
    /* Push a separator to the loop variable stack */
8636
1.41M
    zend_loop_var dummy_var;
8637
1.41M
    dummy_var.opcode = ZEND_RETURN;
8638
8639
1.41M
    zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
8640
1.41M
  }
8641
8642
1.41M
  zend_compile_params(params_ast, return_type_ast,
8643
1.41M
    is_method && zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME) ? IS_STRING : 0);
8644
1.41M
  if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
8645
10.6k
    zend_mark_function_as_generator();
8646
10.6k
    zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
8647
10.6k
  }
8648
1.41M
  if (decl->kind == ZEND_AST_ARROW_FUNC) {
8649
25.4k
    zend_compile_implicit_closure_uses(&info);
8650
25.4k
    zend_hash_destroy(&info.uses);
8651
1.39M
  } else if (uses_ast) {
8652
780
    zend_compile_closure_uses(uses_ast);
8653
780
  }
8654
8655
1.41M
  if (ast->kind == ZEND_AST_ARROW_FUNC && decl->child[2]->kind != ZEND_AST_RETURN) {
8656
11.0k
    bool needs_return = true;
8657
11.0k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8658
1.18k
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8659
1.18k
      needs_return = !ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER);
8660
1.18k
    }
8661
11.0k
    if (needs_return) {
8662
10.8k
      stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
8663
10.8k
      decl->child[2] = stmt_ast;
8664
10.8k
    }
8665
11.0k
  }
8666
8667
1.41M
  if (op_array->fn_flags & ZEND_ACC_NODISCARD) {
8668
    /* ZEND_ACC_NODISCARD gets added by the attribute validator, but only
8669
     * if the method is not a hook; if it is a hook, then the validator
8670
     * will have returned an error message, even if the error message was
8671
     * delayed with #[\DelayedTargetValidation] that ZEND_ACC_NODISCARD
8672
     * flag should not have been added. */
8673
766
    ZEND_ASSERT(!is_hook);
8674
8675
766
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8676
738
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8677
738
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_VOID)) {
8678
2
        zend_error_noreturn(E_COMPILE_ERROR,
8679
2
          "A void %s does not return a value, but #[\\NoDiscard] requires a return value",
8680
2
          CG(active_class_entry) != NULL ? "method" : "function");
8681
2
      }
8682
8683
736
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
8684
2
        zend_error_noreturn(E_COMPILE_ERROR,
8685
2
          "A never returning %s does not return a value, but #[\\NoDiscard] requires a return value",
8686
2
          CG(active_class_entry) != NULL ? "method" : "function");
8687
2
      }
8688
736
    }
8689
766
  }
8690
8691
1.41M
  zend_compile_stmt(stmt_ast);
8692
8693
1.41M
  if (is_method) {
8694
25.6k
    CG(zend_lineno) = decl->start_lineno;
8695
25.6k
    zend_check_magic_method_implementation(
8696
25.6k
      CG(active_class_entry), (zend_function *) op_array, lcname, E_COMPILE_ERROR);
8697
1.39M
  } else if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8698
    /* Only register the function after a successful compile */
8699
2.76k
    if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
8700
48
      CG(zend_lineno) = decl->start_lineno;
8701
48
      do_bind_function_error(lcname, op_array, true);
8702
48
    }
8703
2.76k
  }
8704
8705
  /* put the implicit return on the really last line */
8706
1.41M
  CG(zend_lineno) = decl->end_lineno;
8707
8708
1.41M
  zend_do_extended_stmt(NULL);
8709
1.41M
  zend_emit_final_return(false);
8710
8711
1.41M
  pass_two(CG(active_op_array));
8712
1.41M
  zend_oparray_context_end(&orig_oparray_context);
8713
8714
  /* Pop the loop variable stack separator */
8715
1.41M
  zend_stack_del_top(&CG(loop_var_stack));
8716
8717
1.41M
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8718
2.71k
    zend_observer_function_declared_notify(op_array, lcname);
8719
2.71k
  }
8720
8721
1.41M
  if (lcname != NULL) {
8722
1.41M
    zend_string_release_ex(lcname, 0);
8723
1.41M
  }
8724
8725
1.41M
  CG(active_op_array) = orig_op_array;
8726
1.41M
  CG(active_class_entry) = orig_class_entry;
8727
8728
1.41M
  return op_array;
8729
1.41M
}
8730
8731
static zend_op_array *zend_compile_func_decl(znode *result, zend_ast *ast, enum func_decl_level level)
8732
1.41M
{
8733
1.41M
  return zend_compile_func_decl_ex(result, ast, level, /* property_info */ NULL, (zend_property_hook_kind)-1);
8734
1.41M
}
8735
8736
3.34k
zend_property_hook_kind zend_get_property_hook_kind_from_name(const zend_string *name) {
8737
3.34k
  if (zend_string_equals_literal_ci(name, "get")) {
8738
2.31k
    return ZEND_PROPERTY_HOOK_GET;
8739
2.31k
  } else if (zend_string_equals_literal_ci(name, "set")) {
8740
882
    return ZEND_PROPERTY_HOOK_SET;
8741
882
  } else {
8742
153
    return (zend_property_hook_kind)-1;
8743
153
  }
8744
3.34k
}
8745
8746
static void zend_compile_property_hooks(
8747
    zend_property_info *prop_info, zend_string *prop_name,
8748
    zend_ast *prop_type_ast, const zend_ast_list *hooks)
8749
2.45k
{
8750
2.45k
  zend_class_entry *ce = CG(active_class_entry);
8751
8752
2.45k
  if (prop_info->flags & ZEND_ACC_READONLY) {
8753
5
    zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly");
8754
5
  }
8755
8756
2.45k
  if (hooks->children == 0) {
8757
9
    zend_error_noreturn(E_COMPILE_ERROR, "Property hook list must not be empty");
8758
9
  }
8759
8760
5.04k
  for (uint32_t i = 0; i < hooks->children; i++) {
8761
2.85k
    zend_ast_decl *hook = (zend_ast_decl *) hooks->child[i];
8762
2.85k
    zend_string *name = hook->name;
8763
2.85k
    zend_ast *stmt_ast = hook->child[2];
8764
2.85k
    zend_ast **return_type_ast_ptr = NULL;
8765
2.85k
    zend_ast **value_type_ast_ptr = NULL;
8766
2.85k
    CG(zend_lineno) = hook->start_lineno;
8767
8768
    /* Non-private hooks are always public. This avoids having to copy the hook when inheriting
8769
     * hooks from protected properties to public ones. */
8770
2.85k
    uint32_t hook_visibility = (prop_info->flags & ZEND_ACC_PPP_MASK) != ZEND_ACC_PRIVATE ? ZEND_ACC_PUBLIC : ZEND_ACC_PRIVATE;
8771
2.85k
    hook->flags |= hook_visibility;
8772
8773
2.85k
    if (prop_info->flags & ZEND_ACC_STATIC) {
8774
32
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare hooks for static property");
8775
32
    }
8776
2.81k
    if ((hook->flags & ZEND_ACC_FINAL) && (hook->flags & ZEND_ACC_PRIVATE)) {
8777
2
      zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both final and private");
8778
2
    }
8779
2.81k
    if ((ce->ce_flags & ZEND_ACC_INTERFACE)
8780
2.64k
     || ((prop_info->flags & ZEND_ACC_ABSTRACT) && !stmt_ast)) {
8781
436
      hook->flags |= ZEND_ACC_ABSTRACT;
8782
8783
436
      if (stmt_ast) {
8784
9
        zend_error_noreturn(E_COMPILE_ERROR, "Abstract property hook cannot have body");
8785
9
      }
8786
427
      if (hook->flags & ZEND_ACC_PRIVATE) {
8787
2
        zend_error_noreturn(E_COMPILE_ERROR,
8788
2
          "Property hook cannot be both abstract and private");
8789
2
      }
8790
425
      if (hook->flags & ZEND_ACC_FINAL) {
8791
3
        zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both abstract and final");
8792
3
      }
8793
2.38k
    } else if (!stmt_ast) {
8794
13
      zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract property hook must have a body");
8795
13
    }
8796
8797
2.78k
    zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(name);
8798
2.78k
    if (hook_kind == (zend_property_hook_kind)-1) {
8799
153
      zend_error_noreturn(E_COMPILE_ERROR,
8800
153
        "Unknown hook \"%s\" for property %s::$%s, expected \"get\" or \"set\"",
8801
153
        ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8802
153
    }
8803
8804
2.63k
    if (stmt_ast && stmt_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
8805
1.29k
      stmt_ast = stmt_ast->child[0];
8806
1.29k
      if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
8807
1.09k
        stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
8808
1.09k
      } else {
8809
194
        ZEND_ASSERT(hook_kind == ZEND_PROPERTY_HOOK_SET);
8810
194
        stmt_ast = zend_ast_create(ZEND_AST_ASSIGN,
8811
194
          zend_ast_create(ZEND_AST_PROP,
8812
194
            zend_ast_create(ZEND_AST_VAR, zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_THIS))),
8813
194
            zend_ast_create_zval_from_str(zend_copy_unmangled_prop_name(prop_info->name))),
8814
194
          stmt_ast);
8815
194
      }
8816
1.29k
      stmt_ast = zend_ast_create_list(1, ZEND_AST_STMT_LIST, stmt_ast);
8817
1.29k
      hook->child[2] = stmt_ast;
8818
1.29k
    }
8819
8820
2.63k
    if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
8821
1.78k
      if (hook->child[0]) {
8822
2
        zend_error_noreturn(E_COMPILE_ERROR, "get hook of property %s::$%s must not have a parameter list",
8823
2
          ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8824
2
      }
8825
8826
1.77k
      hook->child[0] = zend_ast_create_list(0, ZEND_AST_PARAM_LIST);
8827
8828
1.77k
      return_type_ast_ptr = &hook->child[3];
8829
1.77k
      *return_type_ast_ptr = prop_type_ast;
8830
1.77k
    } else if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
8831
855
      if (hook->child[0]) {
8832
39
        const zend_ast_list *param_list = zend_ast_get_list(hook->child[0]);
8833
39
        if (param_list->children != 1) {
8834
0
          zend_error_noreturn(E_COMPILE_ERROR, "%s hook of property %s::$%s must accept exactly one parameters",
8835
0
            ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8836
0
        }
8837
39
        const zend_ast *value_param_ast = param_list->child[0];
8838
39
        if (value_param_ast->attr & ZEND_PARAM_REF) {
8839
2
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be pass-by-reference",
8840
2
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8841
2
        }
8842
37
        if (value_param_ast->attr & ZEND_PARAM_VARIADIC) {
8843
2
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be variadic",
8844
2
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8845
2
        }
8846
35
        if (value_param_ast->child[2]) {
8847
2
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not have a default value",
8848
2
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8849
2
        }
8850
33
        if ((prop_type_ast != NULL) != (value_param_ast->child[0] != NULL)) {
8851
2
          zend_hooked_property_variance_error_ex(zend_ast_get_str(value_param_ast->child[1]), ce->name, prop_info->name);
8852
2
        }
8853
816
      } else {
8854
816
        zend_ast *param_name_ast = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VALUE));
8855
816
        zend_ast *param = zend_ast_create(
8856
816
          ZEND_AST_PARAM, prop_type_ast, param_name_ast,
8857
816
          /* expr */ NULL, /* doc_comment */ NULL, /* attributes */ NULL,
8858
816
          /* hooks */ NULL);
8859
816
        value_type_ast_ptr = &param->child[0];
8860
816
        hook->child[0] = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, param);
8861
816
      }
8862
847
      zend_ast *return_type = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VOID));
8863
847
      return_type->attr = ZEND_NAME_NOT_FQ;
8864
847
      hook->child[3] = return_type;
8865
847
    } else {
8866
0
      ZEND_UNREACHABLE();
8867
0
    }
8868
8869
2.62k
    hook->name = zend_strpprintf(0, "$%s::%s", ZSTR_VAL(prop_name), ZSTR_VAL(name));
8870
8871
2.62k
    zend_function *func = (zend_function *) zend_compile_func_decl_ex(
8872
2.62k
      NULL, (zend_ast *) hook, FUNC_DECL_LEVEL_NESTED, prop_info->name, hook_kind);
8873
8874
2.62k
    func->common.prop_info = prop_info;
8875
8876
2.62k
    if (!prop_info->hooks) {
8877
2.20k
      prop_info->hooks = zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE);
8878
2.20k
      memset(prop_info->hooks, 0, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
8879
2.20k
    }
8880
8881
2.62k
    if (prop_info->hooks[hook_kind]) {
8882
22
      zend_error_noreturn(E_COMPILE_ERROR,
8883
22
        "Cannot redeclare property hook \"%s\"", ZSTR_VAL(name));
8884
22
    }
8885
2.60k
    prop_info->hooks[hook_kind] = func;
8886
8887
2.60k
    if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
8888
826
      switch (zend_verify_property_hook_variance(prop_info, func)) {
8889
807
        case INHERITANCE_SUCCESS:
8890
807
          break;
8891
16
        case INHERITANCE_UNRESOLVED:
8892
16
          ce->num_hooked_prop_variance_checks++;
8893
16
          break;
8894
3
        case INHERITANCE_ERROR:
8895
3
          zend_hooked_property_variance_error(prop_info);
8896
0
        case INHERITANCE_WARNING:
8897
0
          ZEND_UNREACHABLE();
8898
826
      }
8899
826
    }
8900
8901
2.60k
    zend_string_release(name);
8902
    /* Un-share type ASTs to avoid double-frees of zval nodes. */
8903
2.60k
    if (return_type_ast_ptr) {
8904
1.74k
      *return_type_ast_ptr = NULL;
8905
1.74k
    }
8906
2.60k
    if (value_type_ast_ptr) {
8907
795
      *value_type_ast_ptr = NULL;
8908
795
    }
8909
2.60k
  }
8910
8911
2.19k
  ce->num_hooked_props++;
8912
8913
  /* See zend_link_hooked_object_iter(). */
8914
2.19k
#ifndef ZEND_OPCACHE_SHM_REATTACHMENT
8915
2.19k
  if (!ce->get_iterator) {
8916
    /* Will be removed again, in case of Iterator or IteratorAggregate. */
8917
1.56k
    ce->get_iterator = zend_hooked_object_get_iterator;
8918
1.56k
  }
8919
2.19k
#endif
8920
8921
2.19k
  if (!prop_info->ce->parent_name) {
8922
1.26k
    zend_verify_hooked_property(ce, prop_info, prop_name);
8923
1.26k
  }
8924
2.19k
}
8925
8926
static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */
8927
30.8k
{
8928
30.8k
  const zend_ast_list *list = zend_ast_get_list(ast);
8929
30.8k
  zend_class_entry *ce = CG(active_class_entry);
8930
30.8k
  uint32_t i, children = list->children;
8931
8932
30.8k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
8933
3
    zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", ZSTR_VAL(ce->name));
8934
3
  }
8935
8936
30.8k
  if ((flags & ZEND_ACC_FINAL) && (flags & ZEND_ACC_PRIVATE)) {
8937
2
    zend_error_noreturn(E_COMPILE_ERROR, "Property cannot be both final and private");
8938
2
  }
8939
8940
30.8k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
8941
208
    if (flags & ZEND_ACC_FINAL) {
8942
2
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be final");
8943
2
    }
8944
206
    if (flags & (ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE)) {
8945
2
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be protected or private");
8946
2
    }
8947
204
    if (flags & ZEND_ACC_ABSTRACT) {
8948
2
      zend_error_noreturn(E_COMPILE_ERROR,
8949
2
        "Property in interface cannot be explicitly abstract. "
8950
2
        "All interface members are implicitly abstract");
8951
2
    }
8952
202
    flags |= ZEND_ACC_ABSTRACT;
8953
202
  }
8954
8955
61.8k
  for (i = 0; i < children; ++i) {
8956
31.1k
    zend_property_info *info;
8957
31.1k
    zend_ast *prop_ast = list->child[i];
8958
31.1k
    zend_ast *name_ast = prop_ast->child[0];
8959
31.1k
    zend_ast **value_ast_ptr = &prop_ast->child[1];
8960
31.1k
    zend_ast *doc_comment_ast = prop_ast->child[2];
8961
31.1k
    zend_ast *hooks_ast = prop_ast->child[3];
8962
31.1k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
8963
31.1k
    zend_string *doc_comment = NULL;
8964
31.1k
    zval value_zv;
8965
31.1k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
8966
31.1k
    flags |= zend_property_is_virtual(ce, name, hooks_ast, flags) ? ZEND_ACC_VIRTUAL : 0;
8967
8968
31.1k
    zend_string *old_active_property_info_name = CG(context).active_property_info_name;
8969
31.1k
    CG(context).active_property_info_name = name;
8970
8971
31.1k
    if (!hooks_ast) {
8972
28.7k
      if (ce->ce_flags & ZEND_ACC_INTERFACE) {
8973
2
        zend_error_noreturn(E_COMPILE_ERROR,
8974
2
          "Interfaces may only include hooked properties");
8975
2
      }
8976
28.7k
      if (flags & ZEND_ACC_ABSTRACT) {
8977
2
        zend_error_noreturn(E_COMPILE_ERROR,
8978
2
          "Only hooked properties may be declared abstract");
8979
2
      }
8980
28.7k
    }
8981
31.1k
    if ((flags & ZEND_ACC_ABSTRACT)) {
8982
486
      ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
8983
486
    }
8984
8985
31.1k
    if (type_ast) {
8986
22.5k
      type = zend_compile_typename(type_ast);
8987
8988
22.5k
      if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_NEVER|MAY_BE_CALLABLE)) {
8989
2
        zend_string *str = zend_type_to_string(type);
8990
2
        zend_error_noreturn(E_COMPILE_ERROR,
8991
2
          "Property %s::$%s cannot have type %s",
8992
2
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
8993
2
      }
8994
22.5k
    }
8995
8996
    /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */
8997
31.0k
    if (doc_comment_ast) {
8998
225
      doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
8999
225
    }
9000
9001
31.0k
    if (zend_hash_exists(&ce->properties_info, name)) {
9002
41
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
9003
41
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
9004
41
    }
9005
9006
31.0k
    if (*value_ast_ptr) {
9007
7.64k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9008
9009
7.64k
      if (ZEND_TYPE_IS_SET(type) && !Z_CONSTANT(value_zv)
9010
1.32k
          && !zend_is_valid_default_value(type, &value_zv)) {
9011
45
        zend_string *str = zend_type_to_string(type);
9012
45
        if (Z_TYPE(value_zv) == IS_NULL && !ZEND_TYPE_IS_INTERSECTION(type)) {
9013
6
          ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
9014
6
          zend_string *nullable_str = zend_type_to_string(type);
9015
9016
6
          zend_error_noreturn(E_COMPILE_ERROR,
9017
6
            "Default value for property of type %s may not be null. "
9018
6
            "Use the nullable type %s to allow null default value",
9019
6
            ZSTR_VAL(str), ZSTR_VAL(nullable_str));
9020
39
        } else {
9021
39
          zend_error_noreturn(E_COMPILE_ERROR,
9022
39
            "Cannot use %s as default value for property %s::$%s of type %s",
9023
39
            zend_zval_value_name(&value_zv),
9024
39
            ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9025
39
        }
9026
45
      }
9027
23.4k
    } else if (!ZEND_TYPE_IS_SET(type) && !hooks_ast) {
9028
1.22k
      ZVAL_NULL(&value_zv);
9029
22.1k
    } else {
9030
22.1k
      ZVAL_UNDEF(&value_zv);
9031
22.1k
    }
9032
9033
31.0k
    if ((ce->ce_flags & ZEND_ACC_READONLY_CLASS)) {
9034
53
      flags |= ZEND_ACC_READONLY;
9035
53
    }
9036
9037
31.0k
    if (flags & ZEND_ACC_READONLY) {
9038
233
      if (!ZEND_TYPE_IS_SET(type)) {
9039
11
        zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
9040
11
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9041
11
      }
9042
222
      if (!Z_ISUNDEF(value_zv)) {
9043
2
        zend_error_noreturn(E_COMPILE_ERROR,
9044
2
          "Readonly property %s::$%s cannot have default value",
9045
2
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9046
2
      }
9047
220
      if (flags & ZEND_ACC_STATIC) {
9048
3
        zend_error_noreturn(E_COMPILE_ERROR,
9049
3
          "Static property %s::$%s cannot be readonly",
9050
3
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9051
3
      }
9052
220
    }
9053
9054
30.9k
    info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type);
9055
9056
30.9k
    if (hooks_ast) {
9057
2.35k
      zend_compile_property_hooks(info, name, type_ast, zend_ast_get_list(hooks_ast));
9058
2.35k
    }
9059
9060
30.9k
    if (attr_ast) {
9061
548
      zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0);
9062
9063
548
      const zend_attribute *override_attribute = zend_get_attribute_str(info->attributes, "override", sizeof("override")-1);
9064
548
      if (override_attribute) {
9065
24
        info->flags |= ZEND_ACC_OVERRIDE;
9066
24
      }
9067
548
    }
9068
9069
30.9k
    CG(context).active_property_info_name = old_active_property_info_name;
9070
30.9k
  }
9071
30.8k
}
9072
/* }}} */
9073
9074
static void zend_compile_prop_group(const zend_ast *ast) /* {{{ */
9075
30.8k
{
9076
30.8k
  zend_ast *type_ast = ast->child[0];
9077
30.8k
  zend_ast *prop_ast = ast->child[1];
9078
30.8k
  zend_ast *attr_ast = ast->child[2];
9079
9080
30.8k
  zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast);
9081
30.8k
}
9082
/* }}} */
9083
9084
static void zend_check_trait_alias_modifiers(uint32_t attr) /* {{{ */
9085
134k
{
9086
134k
  if (attr & ZEND_ACC_STATIC) {
9087
2
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"static\" as method modifier in trait alias");
9088
134k
  } else if (attr & ZEND_ACC_ABSTRACT) {
9089
2
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"abstract\" as method modifier in trait alias");
9090
2
  }
9091
134k
}
9092
/* }}} */
9093
9094
static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast, zend_ast *type_ast)
9095
12.4k
{
9096
12.4k
  const zend_ast_list *list = zend_ast_get_list(ast);
9097
12.4k
  zend_class_entry *ce = CG(active_class_entry);
9098
12.4k
  uint32_t i, children = list->children;
9099
9100
24.8k
  for (i = 0; i < children; ++i) {
9101
12.4k
    zend_class_constant *c;
9102
12.4k
    zend_ast *const_ast = list->child[i];
9103
12.4k
    zend_ast *name_ast = const_ast->child[0];
9104
12.4k
    zend_ast **value_ast_ptr = &const_ast->child[1];
9105
12.4k
    zend_ast *doc_comment_ast = const_ast->child[2];
9106
12.4k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
9107
12.4k
    zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
9108
12.4k
    zval value_zv;
9109
12.4k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
9110
9111
12.4k
    if (type_ast) {
9112
4.39k
      type = zend_compile_typename(type_ast);
9113
9114
4.39k
      uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9115
9116
4.39k
      if (type_mask != MAY_BE_ANY && (type_mask & (MAY_BE_CALLABLE|MAY_BE_VOID|MAY_BE_NEVER))) {
9117
2
        zend_string *type_str = zend_type_to_string(type);
9118
9119
2
        zend_error_noreturn(E_COMPILE_ERROR, "Class constant %s::%s cannot have type %s",
9120
2
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9121
2
      }
9122
4.39k
    }
9123
9124
12.4k
    if (UNEXPECTED((flags & ZEND_ACC_PRIVATE) && (flags & ZEND_ACC_FINAL))) {
9125
2
      zend_error_noreturn(
9126
2
        E_COMPILE_ERROR, "Private constant %s::%s cannot be final as it is not visible to other classes",
9127
2
        ZSTR_VAL(ce->name), ZSTR_VAL(name)
9128
2
      );
9129
2
    }
9130
9131
12.4k
    zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9132
9133
12.4k
    if (!Z_CONSTANT(value_zv) && ZEND_TYPE_IS_SET(type) && !zend_is_valid_default_value(type, &value_zv)) {
9134
17
      zend_string *type_str = zend_type_to_string(type);
9135
9136
17
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as value for class constant %s::%s of type %s",
9137
17
        zend_zval_type_name(&value_zv), ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9138
17
    }
9139
9140
12.4k
    c = zend_declare_typed_class_constant(ce, name, &value_zv, flags, doc_comment, type);
9141
9142
12.4k
    if (attr_ast) {
9143
51
      zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9144
9145
51
      const zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9146
9147
51
      if (deprecated) {
9148
21
        ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9149
        /* For deprecated constants, we need to flag the zval for recursion
9150
         * detection. Make sure the zval is separated out of shm. */
9151
21
        ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
9152
21
        ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
9153
21
      }
9154
51
    }
9155
12.4k
  }
9156
12.4k
}
9157
9158
static void zend_compile_class_const_group(const zend_ast *ast) /* {{{ */
9159
12.4k
{
9160
12.4k
  zend_ast *const_ast = ast->child[0];
9161
12.4k
  zend_ast *attr_ast = ast->child[1];
9162
12.4k
  zend_ast *type_ast = ast->child[2];
9163
9164
12.4k
  zend_compile_class_const_decl(const_ast, ast->attr, attr_ast, type_ast);
9165
12.4k
}
9166
/* }}} */
9167
9168
static void zend_compile_method_ref(const zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */
9169
134k
{
9170
134k
  zend_ast *class_ast = ast->child[0];
9171
134k
  zend_ast *method_ast = ast->child[1];
9172
9173
134k
  method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
9174
9175
134k
  if (class_ast) {
9176
1.84k
    method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name");
9177
133k
  } else {
9178
133k
    method_ref->class_name = NULL;
9179
133k
  }
9180
134k
}
9181
/* }}} */
9182
9183
static void zend_compile_trait_precedence(const zend_ast *ast) /* {{{ */
9184
655
{
9185
655
  const zend_ast *method_ref_ast = ast->child[0];
9186
655
  zend_ast *insteadof_ast = ast->child[1];
9187
655
  const zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast);
9188
655
  uint32_t i;
9189
9190
655
  zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*));
9191
655
  zend_compile_method_ref(method_ref_ast, &precedence->trait_method);
9192
655
  precedence->num_excludes = insteadof_list->children;
9193
9194
1.76k
  for (i = 0; i < insteadof_list->children; ++i) {
9195
1.11k
    zend_ast *name_ast = insteadof_list->child[i];
9196
1.11k
    precedence->exclude_class_names[i] =
9197
1.11k
      zend_resolve_const_class_name_reference(name_ast, "trait name");
9198
1.11k
  }
9199
9200
655
  zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
9201
655
}
9202
/* }}} */
9203
9204
static void zend_compile_trait_alias(const zend_ast *ast) /* {{{ */
9205
134k
{
9206
134k
  const zend_ast *method_ref_ast = ast->child[0];
9207
134k
  zend_ast *alias_ast = ast->child[1];
9208
134k
  uint32_t modifiers = ast->attr;
9209
9210
134k
  zend_trait_alias *alias;
9211
9212
134k
  zend_check_trait_alias_modifiers(modifiers);
9213
9214
134k
  alias = emalloc(sizeof(zend_trait_alias));
9215
134k
  zend_compile_method_ref(method_ref_ast, &alias->trait_method);
9216
134k
  alias->modifiers = modifiers;
9217
9218
134k
  if (alias_ast) {
9219
133k
    alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
9220
133k
  } else {
9221
361
    alias->alias = NULL;
9222
361
  }
9223
9224
134k
  zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
9225
134k
}
9226
/* }}} */
9227
9228
static void zend_compile_use_trait(const zend_ast *ast) /* {{{ */
9229
45.1k
{
9230
45.1k
  const zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
9231
45.1k
  zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
9232
45.1k
  zend_class_entry *ce = CG(active_class_entry);
9233
45.1k
  uint32_t i;
9234
9235
45.1k
  ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children));
9236
9237
92.5k
  for (i = 0; i < traits->children; ++i) {
9238
47.3k
    zend_ast *trait_ast = traits->child[i];
9239
9240
47.3k
    if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9241
2
      zend_string *name = zend_ast_get_str(trait_ast);
9242
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
9243
2
        "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
9244
2
    }
9245
9246
47.3k
    ce->trait_names[ce->num_traits].name =
9247
47.3k
      zend_resolve_const_class_name_reference(trait_ast, "trait name");
9248
47.3k
    ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name);
9249
47.3k
    ce->num_traits++;
9250
47.3k
  }
9251
9252
45.1k
  if (!adaptations) {
9253
940
    return;
9254
940
  }
9255
9256
179k
  for (i = 0; i < adaptations->children; ++i) {
9257
134k
    const zend_ast *adaptation_ast = adaptations->child[i];
9258
134k
    switch (adaptation_ast->kind) {
9259
655
      case ZEND_AST_TRAIT_PRECEDENCE:
9260
655
        zend_compile_trait_precedence(adaptation_ast);
9261
655
        break;
9262
134k
      case ZEND_AST_TRAIT_ALIAS:
9263
134k
        zend_compile_trait_alias(adaptation_ast);
9264
134k
        break;
9265
134k
      EMPTY_SWITCH_DEFAULT_CASE()
9266
134k
    }
9267
134k
  }
9268
44.2k
}
9269
/* }}} */
9270
9271
static void zend_compile_implements(zend_ast *ast) /* {{{ */
9272
1.81k
{
9273
1.81k
  const zend_ast_list *list = zend_ast_get_list(ast);
9274
1.81k
  zend_class_entry *ce = CG(active_class_entry);
9275
1.81k
  zend_class_name *interface_names;
9276
1.81k
  uint32_t i;
9277
9278
1.81k
  interface_names = emalloc(sizeof(zend_class_name) * list->children);
9279
9280
4.95k
  for (i = 0; i < list->children; ++i) {
9281
3.13k
    zend_ast *class_ast = list->child[i];
9282
3.13k
    interface_names[i].name =
9283
3.13k
      zend_resolve_const_class_name_reference(class_ast, "interface name");
9284
3.13k
    interface_names[i].lc_name = zend_string_tolower(interface_names[i].name);
9285
3.13k
  }
9286
9287
1.81k
  ce->num_interfaces = list->children;
9288
1.81k
  ce->interface_names = interface_names;
9289
1.81k
}
9290
/* }}} */
9291
9292
static zend_string *zend_generate_anon_class_name(const zend_ast_decl *decl)
9293
1.39k
{
9294
1.39k
  zend_string *filename = CG(active_op_array)->filename;
9295
1.39k
  uint32_t start_lineno = decl->start_lineno;
9296
9297
  /* Use parent or first interface as prefix. */
9298
1.39k
  zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS);
9299
1.39k
  if (decl->child[0]) {
9300
148
    prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name");
9301
1.25k
  } else if (decl->child[1]) {
9302
707
    const zend_ast_list *list = zend_ast_get_list(decl->child[1]);
9303
707
    prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name");
9304
707
  }
9305
9306
1.39k
  zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32,
9307
1.39k
    ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
9308
1.39k
  zend_string_release(prefix);
9309
1.39k
  return zend_new_interned_string(result);
9310
1.39k
}
9311
9312
static void zend_compile_enum_backing_type(zend_class_entry *ce, zend_ast *enum_backing_type_ast)
9313
448
{
9314
448
  ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM);
9315
448
  zend_type type = zend_compile_typename(enum_backing_type_ast);
9316
448
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9317
448
  if (ZEND_TYPE_IS_COMPLEX(type) || (type_mask != MAY_BE_LONG && type_mask != MAY_BE_STRING)) {
9318
46
    zend_string *type_string = zend_type_to_string(type);
9319
46
    zend_error_noreturn(E_COMPILE_ERROR,
9320
46
      "Enum backing type must be int or string, %s given",
9321
46
      ZSTR_VAL(type_string));
9322
46
  }
9323
402
  if (type_mask == MAY_BE_LONG) {
9324
311
    ce->enum_backing_type = IS_LONG;
9325
311
  } else {
9326
91
    ZEND_ASSERT(type_mask == MAY_BE_STRING);
9327
91
    ce->enum_backing_type = IS_STRING;
9328
91
  }
9329
402
  zend_type_release(type, 0);
9330
402
}
9331
9332
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel) /* {{{ */
9333
125k
{
9334
125k
  const zend_ast_decl *decl = (const zend_ast_decl *) ast;
9335
125k
  zend_ast *extends_ast = decl->child[0];
9336
125k
  zend_ast *implements_ast = decl->child[1];
9337
125k
  zend_ast *stmt_ast = decl->child[2];
9338
125k
  zend_ast *enum_backing_type_ast = decl->child[4];
9339
125k
  zend_string *name, *lcname;
9340
125k
  zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
9341
125k
  zend_op *opline;
9342
9343
125k
  zend_class_entry *original_ce = CG(active_class_entry);
9344
9345
125k
  if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
9346
124k
    zend_string *unqualified_name = decl->name;
9347
9348
124k
    if (CG(active_class_entry)) {
9349
2
      zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
9350
2
    }
9351
9352
124k
    const char *type = "a class name";
9353
124k
    if (decl->flags & ZEND_ACC_ENUM) {
9354
4.57k
      type = "an enum name";
9355
119k
    } else if (decl->flags & ZEND_ACC_INTERFACE) {
9356
5.24k
      type = "an interface name";
9357
114k
    } else if (decl->flags & ZEND_ACC_TRAIT) {
9358
1.42k
      type = "a trait name";
9359
1.42k
    }
9360
124k
    zend_assert_valid_class_name(unqualified_name, type);
9361
124k
    name = zend_prefix_with_ns(unqualified_name);
9362
124k
    name = zend_new_interned_string(name);
9363
124k
    lcname = zend_string_tolower(name);
9364
9365
124k
    if (FC(imports)) {
9366
655
      zend_string *import_name =
9367
655
        zend_hash_find_ptr_lc(FC(imports), unqualified_name);
9368
655
      if (import_name && !zend_string_equals_ci(lcname, import_name)) {
9369
4
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s "
9370
4
            "(previously declared as local import)", ZSTR_VAL(name));
9371
4
      }
9372
655
    }
9373
9374
124k
    zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS);
9375
124k
  } else {
9376
    /* Find an anon class name that is not in use yet. */
9377
1.39k
    name = NULL;
9378
1.39k
    lcname = NULL;
9379
1.39k
    do {
9380
1.39k
      zend_tmp_string_release(name);
9381
1.39k
      zend_tmp_string_release(lcname);
9382
1.39k
      name = zend_generate_anon_class_name(decl);
9383
1.39k
      lcname = zend_string_tolower(name);
9384
1.39k
    } while (zend_hash_exists(CG(class_table), lcname));
9385
1.39k
  }
9386
125k
  lcname = zend_new_interned_string(lcname);
9387
9388
125k
  ce->type = ZEND_USER_CLASS;
9389
125k
  ce->name = name;
9390
125k
  zend_initialize_class_data(ce, true);
9391
125k
  if (!(decl->flags & ZEND_ACC_ANON_CLASS)) {
9392
124k
    zend_alloc_ce_cache(ce->name);
9393
124k
  }
9394
9395
125k
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
9396
0
    ce->ce_flags |= ZEND_ACC_PRELOADED;
9397
0
    ZEND_MAP_PTR_NEW(ce->static_members_table);
9398
0
    ZEND_MAP_PTR_NEW(ce->mutable_data);
9399
0
  }
9400
9401
125k
  ce->ce_flags |= decl->flags;
9402
125k
  ce->info.user.filename = zend_string_copy(zend_get_compiled_filename());
9403
125k
  ce->info.user.line_start = decl->start_lineno;
9404
125k
  ce->info.user.line_end = decl->end_lineno;
9405
9406
125k
  if (decl->doc_comment) {
9407
80
    ce->doc_comment = zend_string_copy(decl->doc_comment);
9408
80
  }
9409
9410
125k
  if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
9411
    /* Serialization is not supported for anonymous classes */
9412
1.39k
    ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
9413
1.39k
  }
9414
9415
125k
  if (extends_ast) {
9416
28.0k
    ce->parent_name =
9417
28.0k
      zend_resolve_const_class_name_reference(extends_ast, "class name");
9418
28.0k
  }
9419
9420
125k
  CG(active_class_entry) = ce;
9421
9422
125k
  if (decl->child[3]) {
9423
1.62k
    zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS, 0);
9424
1.62k
  }
9425
9426
125k
  if (implements_ast) {
9427
1.81k
    zend_compile_implements(implements_ast);
9428
1.81k
  }
9429
9430
125k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
9431
4.56k
    if (enum_backing_type_ast != NULL) {
9432
448
      zend_compile_enum_backing_type(ce, enum_backing_type_ast);
9433
448
    }
9434
4.56k
    zend_enum_add_interfaces(ce);
9435
4.56k
    zend_enum_register_props(ce);
9436
4.56k
  }
9437
9438
125k
  zend_compile_stmt(stmt_ast);
9439
9440
  /* Reset lineno for final opcodes and errors */
9441
125k
  CG(zend_lineno) = ast->lineno;
9442
9443
125k
  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) {
9444
85
    zend_verify_abstract_class(ce);
9445
85
  }
9446
9447
125k
  CG(active_class_entry) = original_ce;
9448
9449
125k
  if (toplevel) {
9450
19.7k
    ce->ce_flags |= ZEND_ACC_TOP_LEVEL;
9451
19.7k
  }
9452
9453
  /* We currently don't early-bind classes that implement interfaces or use traits */
9454
125k
  if (!ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9455
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
9456
   /* See zend_link_hooked_object_iter(). */
9457
   && !ce->num_hooked_props
9458
#endif
9459
73.2k
   && !(CG(compiler_options) & ZEND_COMPILE_WITHOUT_EXECUTION)) {
9460
73.2k
    if (toplevel) {
9461
15.2k
      if (extends_ast) {
9462
6.60k
        zend_class_entry *parent_ce = zend_lookup_class_ex(
9463
6.60k
          ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
9464
9465
6.60k
        if (parent_ce
9466
5.96k
         && !zend_compile_ignore_class(parent_ce, ce->info.user.filename)) {
9467
5.96k
          if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) {
9468
2.25k
            zend_string_release(lcname);
9469
2.25k
            return;
9470
2.25k
          }
9471
5.96k
        }
9472
8.66k
      } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) {
9473
4.82k
        zend_string_release(lcname);
9474
4.82k
        zend_build_properties_info_table(ce);
9475
4.82k
        zend_inheritance_check_override(ce);
9476
4.82k
        ce->ce_flags |= ZEND_ACC_LINKED;
9477
4.82k
        zend_observer_class_linked_notify(ce, lcname);
9478
4.82k
        return;
9479
4.82k
      } else {
9480
3.83k
        goto link_unbound;
9481
3.83k
      }
9482
58.0k
    } else if (!extends_ast) {
9483
40.5k
link_unbound:
9484
      /* Link unbound simple class */
9485
40.5k
      zend_build_properties_info_table(ce);
9486
40.5k
      zend_inheritance_check_override(ce);
9487
40.5k
      ce->ce_flags |= ZEND_ACC_LINKED;
9488
40.5k
    }
9489
73.2k
  }
9490
9491
118k
  opline = get_next_op();
9492
9493
118k
  if (ce->parent_name) {
9494
    /* Lowercased parent name */
9495
25.3k
    zend_string *lc_parent_name = zend_string_tolower(ce->parent_name);
9496
25.3k
    opline->op2_type = IS_CONST;
9497
25.3k
    LITERAL_STR(opline->op2, lc_parent_name);
9498
25.3k
  }
9499
9500
118k
  opline->op1_type = IS_CONST;
9501
  /* It's possible that `lcname` is not an interned string because it was not yet in the interned string table.
9502
   * However, by this point another thread may have caused `lcname` to be added in the interned string table.
9503
   * This will cause `lcname` to get freed once it is found in the interned string table. If we were to use
9504
   * LITERAL_STR() here we would not change the `lcname` pointer to the new value, and it would point to the
9505
   * now-freed string. This will cause issues when we use `lcname` in the code below. We solve this by using
9506
   * zend_add_literal_string() which gives us the new value. */
9507
118k
  opline->op1.constant = zend_add_literal_string(&lcname);
9508
9509
118k
  if (decl->flags & ZEND_ACC_ANON_CLASS) {
9510
1.37k
    opline->opcode = ZEND_DECLARE_ANON_CLASS;
9511
1.37k
    opline->extended_value = zend_alloc_cache_slot();
9512
1.37k
    zend_make_var_result(result, opline);
9513
1.37k
    if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) {
9514
      /* We checked above that the class name is not used. This really shouldn't happen. */
9515
0
      zend_error_noreturn(E_ERROR,
9516
0
        "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name));
9517
0
    }
9518
117k
  } else {
9519
    /* Generate RTD keys until we find one that isn't in use yet. */
9520
117k
    zend_string *key = NULL;
9521
117k
    do {
9522
117k
      zend_tmp_string_release(key);
9523
117k
      key = zend_build_runtime_definition_key(lcname, decl->start_lineno);
9524
117k
    } while (!zend_hash_add_ptr(CG(class_table), key, ce));
9525
9526
    /* RTD key is placed after lcname literal in op1 */
9527
117k
    zend_add_literal_string(&key);
9528
9529
117k
    opline->opcode = ZEND_DECLARE_CLASS;
9530
117k
    if (toplevel
9531
12.1k
       && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING)
9532
        /* We currently don't early-bind classes that implement interfaces or use traits */
9533
987
       && !ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9534
117k
    ) {
9535
770
      if (!extends_ast) {
9536
        /* Use empty string for classes without parents to avoid new handler, and special
9537
         * handling of zend_early_binding. */
9538
727
        opline->op2_type = IS_CONST;
9539
727
        LITERAL_STR(opline->op2, ZSTR_EMPTY_ALLOC());
9540
727
      }
9541
770
      CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING;
9542
770
      opline->opcode = ZEND_DECLARE_CLASS_DELAYED;
9543
770
      opline->extended_value = zend_alloc_cache_slot();
9544
770
      opline->result_type = IS_UNUSED;
9545
770
      opline->result.opline_num = -1;
9546
770
    }
9547
117k
  }
9548
118k
}
9549
/* }}} */
9550
9551
static void zend_compile_enum_case(zend_ast *ast)
9552
899
{
9553
899
  zend_class_entry *enum_class = CG(active_class_entry);
9554
899
  if (!(enum_class->ce_flags & ZEND_ACC_ENUM)) {
9555
2
    zend_error_noreturn(E_COMPILE_ERROR, "Case can only be used in enums");
9556
2
  }
9557
9558
897
  zend_string *enum_case_name = zval_make_interned_string(zend_ast_get_zval(ast->child[0]));
9559
897
  zend_string *enum_class_name = enum_class->name;
9560
9561
897
  zval class_name_zval;
9562
897
  ZVAL_STR_COPY(&class_name_zval, enum_class_name);
9563
897
  zend_ast *class_name_ast = zend_ast_create_zval(&class_name_zval);
9564
9565
897
  zval case_name_zval;
9566
897
  ZVAL_STR_COPY(&case_name_zval, enum_case_name);
9567
897
  zend_ast *case_name_ast = zend_ast_create_zval(&case_name_zval);
9568
9569
897
  zend_ast *case_value_ast = ast->child[1];
9570
  // Remove case_value_ast from the original AST to avoid freeing it, as it will be freed by zend_const_expr_to_zval
9571
897
  ast->child[1] = NULL;
9572
897
  if (enum_class->enum_backing_type != IS_UNDEF && case_value_ast == NULL) {
9573
2
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of backed enum %s must have a value",
9574
2
      ZSTR_VAL(enum_case_name),
9575
2
      ZSTR_VAL(enum_class_name));
9576
895
  } else if (enum_class->enum_backing_type == IS_UNDEF && case_value_ast != NULL) {
9577
4
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-backed enum %s must not have a value",
9578
4
      ZSTR_VAL(enum_case_name),
9579
4
      ZSTR_VAL(enum_class_name));
9580
4
  }
9581
9582
891
  zend_ast *const_enum_init_ast = zend_ast_create(ZEND_AST_CONST_ENUM_INIT, class_name_ast, case_name_ast, case_value_ast);
9583
9584
891
  zval value_zv;
9585
891
  zend_const_expr_to_zval(&value_zv, &const_enum_init_ast, /* allow_dynamic */ false);
9586
9587
  /* Doc comment has been appended as second last element in ZEND_AST_ENUM ast - attributes are conventionally last */
9588
891
  zend_ast *doc_comment_ast = ast->child[2];
9589
891
  zend_string *doc_comment = NULL;
9590
891
  if (doc_comment_ast) {
9591
240
    doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9592
240
  }
9593
9594
891
  zend_class_constant *c = zend_declare_class_constant_ex(enum_class, enum_case_name, &value_zv, ZEND_ACC_PUBLIC, doc_comment);
9595
891
  ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE;
9596
891
  zend_ast_destroy(const_enum_init_ast);
9597
9598
891
  zend_ast *attr_ast = ast->child[3];
9599
891
  if (attr_ast) {
9600
109
    zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9601
9602
109
    zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9603
9604
109
    if (deprecated) {
9605
28
      ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9606
28
    }
9607
109
  }
9608
891
}
9609
9610
static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
9611
1.62k
{
9612
1.62k
  switch (type) {
9613
871
    case ZEND_SYMBOL_CLASS:
9614
871
      if (!FC(imports)) {
9615
642
        FC(imports) = emalloc(sizeof(HashTable));
9616
642
        zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
9617
642
      }
9618
871
      return FC(imports);
9619
317
    case ZEND_SYMBOL_FUNCTION:
9620
317
      if (!FC(imports_function)) {
9621
296
        FC(imports_function) = emalloc(sizeof(HashTable));
9622
296
        zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
9623
296
      }
9624
317
      return FC(imports_function);
9625
435
    case ZEND_SYMBOL_CONST:
9626
435
      if (!FC(imports_const)) {
9627
370
        FC(imports_const) = emalloc(sizeof(HashTable));
9628
370
        zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
9629
370
      }
9630
435
      return FC(imports_const);
9631
1.62k
    EMPTY_SWITCH_DEFAULT_CASE()
9632
1.62k
  }
9633
9634
0
  return NULL;
9635
1.62k
}
9636
/* }}} */
9637
9638
static char *zend_get_use_type_str(uint32_t type) /* {{{ */
9639
35
{
9640
35
  switch (type) {
9641
28
    case ZEND_SYMBOL_CLASS:
9642
28
      return "";
9643
4
    case ZEND_SYMBOL_FUNCTION:
9644
4
      return " function";
9645
3
    case ZEND_SYMBOL_CONST:
9646
3
      return " const";
9647
35
    EMPTY_SWITCH_DEFAULT_CASE()
9648
35
  }
9649
9650
0
  return " unknown";
9651
35
}
9652
/* }}} */
9653
9654
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) /* {{{ */
9655
28
{
9656
28
  if (zend_string_equals_ci(old_name, check_name)) {
9657
17
    return;
9658
17
  }
9659
9660
11
  zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9661
11
    "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9662
28
}
9663
/* }}} */
9664
9665
static void zend_compile_use(zend_ast *ast) /* {{{ */
9666
1.62k
{
9667
1.62k
  const zend_ast_list *list = zend_ast_get_list(ast);
9668
1.62k
  uint32_t i;
9669
1.62k
  zend_string *current_ns = FC(current_namespace);
9670
1.62k
  uint32_t type = ast->attr;
9671
1.62k
  HashTable *current_import = zend_get_import_ht(type);
9672
1.62k
  bool case_sensitive = type == ZEND_SYMBOL_CONST;
9673
9674
3.45k
  for (i = 0; i < list->children; ++i) {
9675
1.87k
    const zend_ast *use_ast = list->child[i];
9676
1.87k
    zend_ast *old_name_ast = use_ast->child[0];
9677
1.87k
    zend_ast *new_name_ast = use_ast->child[1];
9678
1.87k
    zend_string *old_name = zend_ast_get_str(old_name_ast);
9679
1.87k
    zend_string *new_name, *lookup_name;
9680
9681
1.87k
    if (new_name_ast) {
9682
421
      new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
9683
1.45k
    } else {
9684
1.45k
      const char *unqualified_name;
9685
1.45k
      size_t unqualified_name_len;
9686
1.45k
      if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
9687
        /* The form "use A\B" is equivalent to "use A\B as B" */
9688
540
        new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
9689
912
      } else {
9690
912
        new_name = zend_string_copy(old_name);
9691
9692
912
        if (!current_ns) {
9693
708
          zend_error(E_WARNING, "The use statement with non-compound name '%s' "
9694
708
            "has no effect", ZSTR_VAL(new_name));
9695
708
        }
9696
912
      }
9697
1.45k
    }
9698
9699
1.87k
    if (case_sensitive) {
9700
474
      lookup_name = zend_string_copy(new_name);
9701
1.39k
    } else {
9702
1.39k
      lookup_name = zend_string_tolower(new_name);
9703
1.39k
    }
9704
9705
1.87k
    if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) {
9706
13
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
9707
13
        "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
9708
13
    }
9709
9710
1.86k
    if (current_ns) {
9711
748
      zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
9712
748
      zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
9713
748
      ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
9714
748
      memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1);
9715
9716
748
      if (zend_have_seen_symbol(ns_name, type)) {
9717
17
        zend_check_already_in_use(type, old_name, new_name, ns_name);
9718
17
      }
9719
9720
748
      zend_string_efree(ns_name);
9721
1.11k
    } else if (zend_have_seen_symbol(lookup_name, type)) {
9722
11
      zend_check_already_in_use(type, old_name, new_name, lookup_name);
9723
11
    }
9724
9725
1.86k
    zend_string_addref(old_name);
9726
1.86k
    old_name = zend_new_interned_string(old_name);
9727
1.86k
    if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
9728
24
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9729
24
        "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9730
24
    }
9731
9732
1.83k
    zend_string_release_ex(lookup_name, 0);
9733
1.83k
    zend_string_release_ex(new_name, 0);
9734
1.83k
  }
9735
1.62k
}
9736
/* }}} */
9737
9738
static void zend_compile_group_use(const zend_ast *ast) /* {{{ */
9739
220
{
9740
220
  uint32_t i;
9741
220
  const zend_string *ns = zend_ast_get_str(ast->child[0]);
9742
220
  const zend_ast_list *list = zend_ast_get_list(ast->child[1]);
9743
9744
787
  for (i = 0; i < list->children; i++) {
9745
567
    zend_ast *inline_use, *use = list->child[i];
9746
567
    zval *name_zval = zend_ast_get_zval(use->child[0]);
9747
567
    zend_string *name = Z_STR_P(name_zval);
9748
567
    zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
9749
567
    zend_string_release_ex(name, 0);
9750
567
    ZVAL_STR(name_zval, compound_ns);
9751
567
    inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
9752
567
    inline_use->attr = ast->attr ? ast->attr : use->attr;
9753
567
    zend_compile_use(inline_use);
9754
567
  }
9755
220
}
9756
/* }}} */
9757
9758
static void zend_compile_const_decl(zend_ast *ast) /* {{{ */
9759
2.61k
{
9760
2.61k
  zend_ast_list *list = zend_ast_get_list(ast);
9761
2.61k
  uint32_t i;
9762
2.61k
  zend_ast *attributes_ast = NULL;
9763
2.61k
  zend_op *last_op = NULL;
9764
6.25k
  for (i = 0; i < list->children; ++i) {
9765
3.64k
    zend_ast *const_ast = list->child[i];
9766
3.64k
    if (const_ast->kind == ZEND_AST_ATTRIBUTE_LIST) {
9767
577
      ZEND_ASSERT(i == list->children - 1);
9768
577
      attributes_ast = const_ast;
9769
577
      continue;
9770
577
    }
9771
3.06k
    ZEND_ASSERT(const_ast->kind == ZEND_AST_CONST_ELEM);
9772
3.06k
    zend_ast *name_ast = const_ast->child[0];
9773
3.06k
    zend_ast **value_ast_ptr = &const_ast->child[1];
9774
3.06k
    zend_string *unqualified_name = zend_ast_get_str(name_ast);
9775
9776
3.06k
    zend_string *name;
9777
3.06k
    znode name_node, value_node;
9778
3.06k
    zval *value_zv = &value_node.u.constant;
9779
9780
3.06k
    value_node.op_type = IS_CONST;
9781
3.06k
    zend_const_expr_to_zval(value_zv, value_ast_ptr, /* allow_dynamic */ true);
9782
9783
3.06k
    if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) {
9784
2
      zend_error_noreturn(E_COMPILE_ERROR,
9785
2
        "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name));
9786
2
    }
9787
9788
3.06k
    name = zend_prefix_with_ns(unqualified_name);
9789
3.06k
    name = zend_new_interned_string(name);
9790
9791
3.06k
    if (FC(imports_const)) {
9792
117
      zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name);
9793
117
      if (import_name && !zend_string_equals(import_name, name)) {
9794
3
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because "
9795
3
          "the name is already in use", ZSTR_VAL(name));
9796
3
      }
9797
117
    }
9798
9799
3.06k
    name_node.op_type = IS_CONST;
9800
3.06k
    ZVAL_STR(&name_node.u.constant, name);
9801
9802
3.06k
    last_op = zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
9803
9804
3.06k
    zend_register_seen_symbol(name, ZEND_SYMBOL_CONST);
9805
3.06k
  }
9806
2.60k
  if (attributes_ast == NULL) {
9807
2.00k
    return;
9808
2.00k
  }
9809
  /* Validate: attributes can only be applied to one constant at a time
9810
   * Since we store the AST for the attributes in the list of children,
9811
   * there should be exactly 2 children. */
9812
607
  if (list->children > 2) {
9813
3
    zend_error_noreturn(
9814
3
      E_COMPILE_ERROR,
9815
3
      "Cannot apply attributes to multiple constants at once"
9816
3
    );
9817
3
  }
9818
9819
604
  HashTable *attributes = NULL;
9820
604
  zend_compile_attributes(&attributes, list->child[1], 0, ZEND_ATTRIBUTE_TARGET_CONST, 0);
9821
9822
604
  ZEND_ASSERT(last_op != NULL);
9823
604
  last_op->opcode = ZEND_DECLARE_ATTRIBUTED_CONST;
9824
574
  znode attribs_node;
9825
574
  attribs_node.op_type = IS_CONST;
9826
574
  ZVAL_PTR(&attribs_node.u.constant, attributes);
9827
574
  zend_emit_op_data(&attribs_node);
9828
574
  CG(active_op_array)->fn_flags |= ZEND_ACC_PTR_OPS;
9829
574
}
9830
/* }}}*/
9831
9832
static void zend_compile_namespace(const zend_ast *ast) /* {{{ */
9833
4.26k
{
9834
4.26k
  zend_ast *name_ast = ast->child[0];
9835
4.26k
  zend_ast *stmt_ast = ast->child[1];
9836
4.26k
  zend_string *name;
9837
4.26k
  bool with_bracket = stmt_ast != NULL;
9838
9839
  /* handle mixed syntax declaration or nested namespaces */
9840
4.26k
  if (!FC(has_bracketed_namespaces)) {
9841
3.21k
    if (FC(current_namespace)) {
9842
      /* previous namespace declarations were unbracketed */
9843
1.06k
      if (with_bracket) {
9844
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
9845
1
          "with unbracketed namespace declarations");
9846
1
      }
9847
1.06k
    }
9848
3.21k
  } else {
9849
    /* previous namespace declarations were bracketed */
9850
1.05k
    if (!with_bracket) {
9851
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
9852
2
        "with unbracketed namespace declarations");
9853
1.05k
    } else if (FC(current_namespace) || FC(in_namespace)) {
9854
4
      zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
9855
4
    }
9856
1.05k
  }
9857
9858
4.26k
  bool is_first_namespace = (!with_bracket && !FC(current_namespace))
9859
2.31k
    || (with_bracket && !FC(has_bracketed_namespaces));
9860
4.26k
  if (is_first_namespace && FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
9861
19
    zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
9862
19
      "the very first statement or after any declare call in the script");
9863
19
  }
9864
9865
4.24k
  if (FC(current_namespace)) {
9866
1.06k
    zend_string_release_ex(FC(current_namespace), 0);
9867
1.06k
  }
9868
9869
4.24k
  if (name_ast) {
9870
3.70k
    name = zend_ast_get_str(name_ast);
9871
9872
3.70k
    if (zend_string_equals_literal_ci(name, "namespace")) {
9873
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name));
9874
2
    }
9875
9876
3.70k
    FC(current_namespace) = zend_string_copy(name);
9877
3.70k
  } else {
9878
536
    FC(current_namespace) = NULL;
9879
536
  }
9880
9881
4.24k
  zend_reset_import_tables();
9882
9883
4.24k
  FC(in_namespace) = 1;
9884
4.24k
  if (with_bracket) {
9885
1.24k
    FC(has_bracketed_namespaces) = 1;
9886
1.24k
  }
9887
9888
4.24k
  if (stmt_ast) {
9889
1.24k
    zend_compile_top_stmt(stmt_ast);
9890
1.24k
    zend_end_namespace();
9891
1.24k
  }
9892
4.24k
}
9893
/* }}} */
9894
9895
static void zend_compile_halt_compiler(const zend_ast *ast) /* {{{ */
9896
21
{
9897
21
  zend_ast *offset_ast = ast->child[0];
9898
21
  zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
9899
9900
21
  const char const_name[] = "__COMPILER_HALT_OFFSET__";
9901
9902
21
  if (FC(has_bracketed_namespaces) && FC(in_namespace)) {
9903
0
    zend_error_noreturn(E_COMPILE_ERROR,
9904
0
      "__HALT_COMPILER() can only be used from the outermost scope");
9905
0
  }
9906
9907
21
  const zend_string *filename = zend_get_compiled_filename();
9908
21
  zend_string *name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
9909
21
    ZSTR_VAL(filename), ZSTR_LEN(filename), false);
9910
9911
  /* Avoid repeated declaration of the __COMPILER_HALT_OFFSET__ constant in
9912
   * case this file was already included. */
9913
21
  if (!zend_hash_find(EG(zend_constants), name)) {
9914
21
    zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0);
9915
21
  }
9916
21
  zend_string_release_ex(name, 0);
9917
21
}
9918
/* }}} */
9919
9920
static bool zend_try_ct_eval_magic_const(zval *zv, const zend_ast *ast) /* {{{ */
9921
15.6k
{
9922
15.6k
  const zend_op_array *op_array = CG(active_op_array);
9923
15.6k
  const zend_class_entry *ce = CG(active_class_entry);
9924
9925
15.6k
  switch (ast->attr) {
9926
672
    case T_LINE:
9927
672
      ZVAL_LONG(zv, ast->lineno);
9928
672
      break;
9929
2.67k
    case T_FILE:
9930
2.67k
      ZVAL_STR_COPY(zv, CG(compiled_filename));
9931
2.67k
      break;
9932
891
    case T_DIR:
9933
891
    {
9934
891
      const zend_string *filename = CG(compiled_filename);
9935
891
      zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
9936
#ifdef ZEND_WIN32
9937
      ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
9938
#else
9939
891
      ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
9940
891
#endif
9941
9942
891
      if (zend_string_equals_literal(dirname, ".")) {
9943
809
        dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
9944
809
#ifdef HAVE_GETCWD
9945
809
        ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN));
9946
#elif defined(HAVE_GETWD)
9947
        ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname)));
9948
#endif
9949
809
        ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname));
9950
809
      }
9951
9952
891
      ZVAL_STR(zv, dirname);
9953
891
      break;
9954
0
    }
9955
1.01k
    case T_FUNC_C:
9956
1.01k
      if (op_array && op_array->function_name) {
9957
746
        ZVAL_STR_COPY(zv, op_array->function_name);
9958
746
      } else {
9959
272
        ZVAL_EMPTY_STRING(zv);
9960
272
      }
9961
1.01k
      break;
9962
681
    case T_PROPERTY_C: {
9963
681
      zend_string *prop_info_name = CG(context).active_property_info_name;
9964
681
      if (prop_info_name) {
9965
344
        ZVAL_STR(zv, zend_copy_unmangled_prop_name(prop_info_name));
9966
344
      } else {
9967
337
        ZVAL_EMPTY_STRING(zv);
9968
337
      }
9969
681
      break;
9970
0
    }
9971
4.56k
    case T_METHOD_C:
9972
      /* Detect whether we are directly inside a class (e.g. a class constant) and treat
9973
       * this as not being inside a function. */
9974
4.56k
      if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
9975
197
        op_array = NULL;
9976
197
      }
9977
4.56k
      if (op_array && op_array->function_name) {
9978
4.13k
        if (op_array->scope) {
9979
3.41k
          ZVAL_NEW_STR(zv,
9980
3.41k
            zend_create_member_string(op_array->scope->name, op_array->function_name));
9981
3.41k
        } else {
9982
715
          ZVAL_STR_COPY(zv, op_array->function_name);
9983
715
        }
9984
4.13k
      } else {
9985
426
        ZVAL_EMPTY_STRING(zv);
9986
426
      }
9987
4.56k
      break;
9988
1.80k
    case T_CLASS_C:
9989
1.80k
      if (ce) {
9990
1.06k
        if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
9991
657
          return 0;
9992
657
        } else {
9993
410
          ZVAL_STR_COPY(zv, ce->name);
9994
410
        }
9995
1.06k
      } else {
9996
738
        ZVAL_EMPTY_STRING(zv);
9997
738
      }
9998
1.14k
      break;
9999
2.67k
    case T_TRAIT_C:
10000
2.67k
      if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10001
452
        ZVAL_STR_COPY(zv, ce->name);
10002
2.22k
      } else {
10003
2.22k
        ZVAL_EMPTY_STRING(zv);
10004
2.22k
      }
10005
2.67k
      break;
10006
638
    case T_NS_C:
10007
638
      if (FC(current_namespace)) {
10008
421
        ZVAL_STR_COPY(zv, FC(current_namespace));
10009
421
      } else {
10010
217
        ZVAL_EMPTY_STRING(zv);
10011
217
      }
10012
638
      break;
10013
15.6k
    EMPTY_SWITCH_DEFAULT_CASE()
10014
15.6k
  }
10015
10016
14.9k
  return 1;
10017
15.6k
}
10018
/* }}} */
10019
10020
ZEND_API bool zend_is_op_long_compatible(const zval *op)
10021
60.0k
{
10022
60.0k
  if (Z_TYPE_P(op) == IS_ARRAY) {
10023
318
    return false;
10024
318
  }
10025
10026
59.7k
  if (Z_TYPE_P(op) == IS_DOUBLE
10027
9.00k
    && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval_silent(Z_DVAL_P(op)))) {
10028
2.74k
    return false;
10029
2.74k
  }
10030
10031
57.0k
  if (Z_TYPE_P(op) == IS_STRING) {
10032
15.2k
    double dval = 0;
10033
15.2k
    uint8_t is_num = is_numeric_str_function(Z_STR_P(op), NULL, &dval);
10034
15.2k
    if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval_silent(dval)))) {
10035
5.89k
      return false;
10036
5.89k
    }
10037
15.2k
  }
10038
10039
51.1k
  return true;
10040
57.0k
}
10041
10042
ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, const zval *op2) /* {{{ */
10043
222k
{
10044
222k
  if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) {
10045
    /* Array to string warning. */
10046
27.8k
    return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY;
10047
27.8k
  }
10048
10049
195k
  if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV
10050
109k
               || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR
10051
59.3k
               || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) {
10052
    /* Only the numeric operations throw errors. */
10053
31.7k
    return 0;
10054
31.7k
  }
10055
10056
163k
  if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) {
10057
9.18k
    if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) {
10058
      /* Adding two arrays is allowed. */
10059
4.93k
      return 0;
10060
4.93k
    }
10061
10062
    /* Numeric operators throw when one of the operands is an array. */
10063
4.25k
    return 1;
10064
9.18k
  }
10065
10066
  /* While basic arithmetic operators always produce numeric string errors,
10067
   * bitwise operators don't produce errors if both operands are strings */
10068
154k
  if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)
10069
27.5k
    && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
10070
12.6k
    return 0;
10071
12.6k
  }
10072
10073
141k
  if (Z_TYPE_P(op1) == IS_STRING
10074
41.2k
    && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) {
10075
24.5k
    return 1;
10076
24.5k
  }
10077
10078
117k
  if (Z_TYPE_P(op2) == IS_STRING
10079
17.5k
    && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) {
10080
7.82k
    return 1;
10081
7.82k
  }
10082
10083
  /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */
10084
109k
  if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR
10085
102k
      || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) {
10086
18.0k
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2)) {
10087
1.39k
      return 1;
10088
1.39k
    }
10089
18.0k
  }
10090
10091
107k
  if (opcode == ZEND_DIV && zval_get_double(op2) == 0.0) {
10092
    /* Division by zero throws an error. */
10093
1.06k
    return 1;
10094
1.06k
  }
10095
10096
  /* Mod is an operation that will cast float/float-strings to integers which might
10097
     produce float to int incompatible errors, and also cannot be divided by 0 */
10098
106k
  if (opcode == ZEND_MOD) {
10099
12.3k
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2) || zval_get_long(op2) == 0) {
10100
10.0k
      return 1;
10101
10.0k
    }
10102
12.3k
  }
10103
10104
96.7k
  if ((opcode == ZEND_POW) && zval_get_double(op1) == 0 && zval_get_double(op2) < 0) {
10105
    /* 0 ** (<0) throws a division by zero error. */
10106
115
    return 1;
10107
115
  }
10108
96.6k
  if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) {
10109
    /* Shift by negative number throws an error. */
10110
451
    return 1;
10111
451
  }
10112
10113
96.1k
  return 0;
10114
96.6k
}
10115
/* }}} */
10116
10117
static inline bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
10118
193k
{
10119
193k
  if (zend_binary_op_produces_error(opcode, op1, op2)) {
10120
36.8k
    return 0;
10121
36.8k
  }
10122
10123
156k
  const binary_op_type fn = get_binary_op(opcode);
10124
156k
  fn(result, op1, op2);
10125
156k
  return 1;
10126
193k
}
10127
/* }}} */
10128
10129
ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op)
10130
91.5k
{
10131
91.5k
  if (opcode == ZEND_BW_NOT) {
10132
    /* BW_NOT on string does not convert the string into an integer. */
10133
6.10k
    if (Z_TYPE_P(op) == IS_STRING) {
10134
1.36k
      return 0;
10135
1.36k
    }
10136
4.74k
    return Z_TYPE_P(op) <= IS_TRUE || !zend_is_op_long_compatible(op);
10137
6.10k
  }
10138
  /* Can happen when called from zend_optimizer_eval_unary_op() */
10139
85.4k
  if (opcode == ZEND_BOOL || opcode == ZEND_BOOL_NOT) {
10140
    /* ZEND_BOOL/ZEND_BOOL_NOT warns when casting NAN. */
10141
85.4k
    return Z_TYPE_P(op) == IS_DOUBLE;
10142
85.4k
  }
10143
10144
0
  return 0;
10145
85.4k
}
10146
10147
static inline bool zend_try_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */
10148
91.3k
{
10149
91.3k
  if (zend_unary_op_produces_error(opcode, op)) {
10150
1.16k
    return 0;
10151
1.16k
  }
10152
10153
90.2k
  const unary_op_type fn = get_unary_op(opcode);
10154
90.2k
  fn(result, op);
10155
90.2k
  return 1;
10156
91.3k
}
10157
/* }}} */
10158
10159
static inline bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
10160
36.3k
{
10161
36.3k
  zval right;
10162
36.3k
  ZVAL_LONG(&right, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10163
36.3k
  return zend_try_ct_eval_binary_op(result, ZEND_MUL, op, &right);
10164
36.3k
}
10165
/* }}} */
10166
10167
static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
10168
9.61k
{
10169
9.61k
  const binary_op_type fn = kind == ZEND_AST_GREATER
10170
9.61k
    ? is_smaller_function : is_smaller_or_equal_function;
10171
9.61k
  fn(result, op2, op1);
10172
9.61k
}
10173
/* }}} */
10174
10175
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
10176
3.49M
{
10177
3.49M
  const zend_ast_list *list = zend_ast_get_list(ast);
10178
3.49M
  zend_ast *last_elem_ast = NULL;
10179
3.49M
  uint32_t i;
10180
3.49M
  bool is_constant = true;
10181
10182
3.49M
  if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
10183
2
    zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
10184
2
  }
10185
10186
  /* First ensure that *all* child nodes are constant and by-val */
10187
7.22M
  for (i = 0; i < list->children; ++i) {
10188
3.73M
    zend_ast *elem_ast = list->child[i];
10189
10190
3.73M
    if (elem_ast == NULL) {
10191
      /* Report error at line of last non-empty element */
10192
116
      if (last_elem_ast) {
10193
65
        CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast);
10194
65
      }
10195
116
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
10196
116
    }
10197
10198
3.73M
    if (elem_ast->kind != ZEND_AST_UNPACK) {
10199
3.56M
      zend_eval_const_expr(&elem_ast->child[0]);
10200
3.56M
      zend_eval_const_expr(&elem_ast->child[1]);
10201
10202
3.56M
      if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL
10203
270k
        || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
10204
3.56M
      ) {
10205
3.30M
        is_constant = false;
10206
3.30M
      }
10207
3.56M
    } else {
10208
161k
      zend_eval_const_expr(&elem_ast->child[0]);
10209
10210
161k
      if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) {
10211
160k
        is_constant = false;
10212
160k
      }
10213
161k
    }
10214
10215
3.73M
    last_elem_ast = elem_ast;
10216
3.73M
  }
10217
10218
3.49M
  if (!is_constant) {
10219
3.39M
    return 0;
10220
3.39M
  }
10221
10222
97.3k
  if (!list->children) {
10223
21.3k
    ZVAL_EMPTY_ARRAY(result);
10224
21.3k
    return 1;
10225
21.3k
  }
10226
10227
75.9k
  array_init_size(result, list->children);
10228
295k
  for (i = 0; i < list->children; ++i) {
10229
223k
    const zend_ast *elem_ast = list->child[i];
10230
223k
    zend_ast *value_ast = elem_ast->child[0];
10231
223k
    zend_ast *key_ast;
10232
10233
223k
    zval *value = zend_ast_get_zval(value_ast);
10234
223k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
10235
831
      if (Z_TYPE_P(value) == IS_ARRAY) {
10236
826
        const HashTable *ht = Z_ARRVAL_P(value);
10237
826
        zval *val;
10238
826
        zend_string *key;
10239
10240
2.75k
        ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
10241
2.75k
          if (key) {
10242
115
            zend_hash_update(Z_ARRVAL_P(result), key, val);
10243
403
          } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
10244
0
            zval_ptr_dtor(result);
10245
0
            return 0;
10246
0
          }
10247
518
          Z_TRY_ADDREF_P(val);
10248
518
        } ZEND_HASH_FOREACH_END();
10249
10250
826
        continue;
10251
826
      } else {
10252
5
        zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked, %s given", zend_zval_value_name(value));
10253
5
      }
10254
831
    }
10255
10256
222k
    Z_TRY_ADDREF_P(value);
10257
10258
222k
    key_ast = elem_ast->child[1];
10259
222k
    if (key_ast) {
10260
22.8k
      const zval *key = zend_ast_get_zval(key_ast);
10261
22.8k
      switch (Z_TYPE_P(key)) {
10262
6.91k
        case IS_LONG:
10263
6.91k
          zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value);
10264
6.91k
          break;
10265
10.7k
        case IS_STRING:
10266
10.7k
          zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value);
10267
10.7k
          break;
10268
3.97k
        case IS_DOUBLE: {
10269
3.97k
          zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key));
10270
          /* Incompatible float will generate an error, leave this to run-time */
10271
3.97k
          if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) {
10272
3.47k
            goto fail;
10273
3.47k
          }
10274
491
          zend_hash_index_update(Z_ARRVAL_P(result), lval, value);
10275
491
          break;
10276
3.97k
        }
10277
1.14k
        case IS_FALSE:
10278
1.14k
          zend_hash_index_update(Z_ARRVAL_P(result), 0, value);
10279
1.14k
          break;
10280
77
        case IS_TRUE:
10281
77
          zend_hash_index_update(Z_ARRVAL_P(result), 1, value);
10282
77
          break;
10283
11
        case IS_NULL:
10284
          /* Null key will generate a warning at run-time. */
10285
11
          goto fail;
10286
4
        default:
10287
4
          zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type");
10288
0
          break;
10289
22.8k
      }
10290
199k
    } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) {
10291
3.58k
fail:
10292
3.58k
      zval_ptr_dtor_nogc(value);
10293
3.58k
      zval_ptr_dtor(result);
10294
3.58k
      return 0;
10295
95
    }
10296
222k
  }
10297
10298
72.3k
  return 1;
10299
75.9k
}
10300
/* }}} */
10301
10302
static void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */
10303
2.23M
{
10304
2.23M
  zend_ast *left_ast = ast->child[0];
10305
2.23M
  zend_ast *right_ast = ast->child[1];
10306
2.23M
  uint32_t opcode = ast->attr;
10307
10308
2.23M
  znode left_node, right_node;
10309
10310
2.23M
  zend_compile_expr(&left_node, left_ast);
10311
2.23M
  zend_compile_expr(&right_node, right_ast);
10312
10313
2.23M
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10314
123k
    if (zend_try_ct_eval_binary_op(&result->u.constant, opcode,
10315
123k
        &left_node.u.constant, &right_node.u.constant)
10316
123k
    ) {
10317
114k
      result->op_type = IS_CONST;
10318
114k
      zval_ptr_dtor(&left_node.u.constant);
10319
114k
      zval_ptr_dtor(&right_node.u.constant);
10320
114k
      return;
10321
114k
    }
10322
123k
  }
10323
10324
2.11M
  do {
10325
2.11M
    if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) {
10326
      /* 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) */
10327
3.69k
      if (left_node.op_type == IS_CONST) {
10328
1.85k
        if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) {
10329
776
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL);
10330
776
          opline->extended_value =
10331
776
            (opcode == ZEND_IS_IDENTICAL) ?
10332
637
              (1 << Z_TYPE(left_node.u.constant)) :
10333
776
              (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant)));
10334
776
          return;
10335
776
        }
10336
1.85k
      } else if (right_node.op_type == IS_CONST) {
10337
640
        if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) {
10338
159
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL);
10339
159
          opline->extended_value =
10340
159
            (opcode == ZEND_IS_IDENTICAL) ?
10341
104
              (1 << Z_TYPE(right_node.u.constant)) :
10342
159
              (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant)));
10343
159
          return;
10344
159
        }
10345
640
      }
10346
2.11M
    } else if (opcode == ZEND_CONCAT) {
10347
      /* convert constant operands to strings at compile-time */
10348
51.5k
      if (left_node.op_type == IS_CONST) {
10349
8.18k
        if (Z_TYPE(left_node.u.constant) == IS_ARRAY) {
10350
22
          zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING;
10351
8.15k
        } else {
10352
8.15k
          convert_to_string(&left_node.u.constant);
10353
8.15k
        }
10354
8.18k
      }
10355
51.5k
      if (right_node.op_type == IS_CONST) {
10356
14.6k
        if (Z_TYPE(right_node.u.constant) == IS_ARRAY) {
10357
105
          zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING;
10358
14.5k
        } else {
10359
14.5k
          convert_to_string(&right_node.u.constant);
10360
14.5k
        }
10361
14.6k
      }
10362
51.5k
      if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10363
0
        opcode = ZEND_FAST_CONCAT;
10364
0
      }
10365
51.5k
    }
10366
2.11M
    zend_emit_op_tmp(result, opcode, &left_node, &right_node);
10367
2.11M
  } while (0);
10368
2.11M
}
10369
/* }}} */
10370
10371
/* We do not use zend_compile_binary_op for this because we want to retain the left-to-right
10372
 * evaluation order. */
10373
static void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */
10374
427k
{
10375
427k
  zend_ast *left_ast = ast->child[0];
10376
427k
  zend_ast *right_ast = ast->child[1];
10377
427k
  znode left_node, right_node;
10378
10379
427k
  ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL);
10380
10381
427k
  zend_compile_expr(&left_node, left_ast);
10382
427k
  zend_compile_expr(&right_node, right_ast);
10383
10384
427k
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10385
8.16k
    result->op_type = IS_CONST;
10386
8.16k
    zend_ct_eval_greater(&result->u.constant, ast->kind,
10387
8.16k
      &left_node.u.constant, &right_node.u.constant);
10388
8.16k
    zval_ptr_dtor(&left_node.u.constant);
10389
8.16k
    zval_ptr_dtor(&right_node.u.constant);
10390
8.16k
    return;
10391
8.16k
  }
10392
10393
419k
  zend_emit_op_tmp(result,
10394
419k
    ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL,
10395
419k
    &right_node, &left_node);
10396
419k
}
10397
/* }}} */
10398
10399
static void zend_compile_unary_op(znode *result, const zend_ast *ast) /* {{{ */
10400
1.39M
{
10401
1.39M
  zend_ast *expr_ast = ast->child[0];
10402
1.39M
  uint32_t opcode = ast->attr;
10403
10404
1.39M
  znode expr_node;
10405
1.39M
  zend_compile_expr(&expr_node, expr_ast);
10406
10407
1.39M
  if (expr_node.op_type == IS_CONST
10408
87.5k
      && zend_try_ct_eval_unary_op(&result->u.constant, opcode, &expr_node.u.constant)) {
10409
86.9k
    result->op_type = IS_CONST;
10410
86.9k
    zval_ptr_dtor(&expr_node.u.constant);
10411
86.9k
    return;
10412
86.9k
  }
10413
10414
1.30M
  zend_emit_op_tmp(result, opcode, &expr_node, NULL);
10415
1.30M
}
10416
/* }}} */
10417
10418
static void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */
10419
50.3k
{
10420
50.3k
  zend_ast *expr_ast = ast->child[0];
10421
50.3k
  znode expr_node, right_node;
10422
10423
50.3k
  ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS);
10424
10425
50.3k
  zend_compile_expr(&expr_node, expr_ast);
10426
10427
50.3k
  if (expr_node.op_type == IS_CONST
10428
22.8k
    && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) {
10429
20.3k
    result->op_type = IS_CONST;
10430
20.3k
    zval_ptr_dtor(&expr_node.u.constant);
10431
20.3k
    return;
10432
20.3k
  }
10433
10434
30.0k
  right_node.op_type = IS_CONST;
10435
30.0k
  ZVAL_LONG(&right_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10436
30.0k
  zend_emit_op_tmp(result, ZEND_MUL, &expr_node, &right_node);
10437
30.0k
}
10438
/* }}} */
10439
10440
static void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */
10441
17.5k
{
10442
17.5k
  zend_ast *left_ast = ast->child[0];
10443
17.5k
  zend_ast *right_ast = ast->child[1];
10444
10445
17.5k
  znode left_node, right_node;
10446
17.5k
  zend_op *opline_jmpz, *opline_bool;
10447
17.5k
  uint32_t opnum_jmpz;
10448
10449
17.5k
  ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR);
10450
10451
17.5k
  zend_compile_expr(&left_node, left_ast);
10452
10453
17.5k
  if (left_node.op_type == IS_CONST) {
10454
2.35k
    if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant))
10455
1.62k
     || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) {
10456
1.62k
      result->op_type = IS_CONST;
10457
1.62k
      ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant));
10458
1.62k
    } else {
10459
734
      zend_compile_expr(&right_node, right_ast);
10460
10461
734
      if (right_node.op_type == IS_CONST) {
10462
355
        result->op_type = IS_CONST;
10463
355
        ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant));
10464
10465
355
        zval_ptr_dtor(&right_node.u.constant);
10466
379
      } else {
10467
379
        zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL);
10468
379
      }
10469
734
    }
10470
10471
2.35k
    zval_ptr_dtor(&left_node.u.constant);
10472
2.35k
    return;
10473
2.35k
  }
10474
10475
15.1k
  opnum_jmpz = get_next_op_number();
10476
15.1k
  opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
10477
15.1k
    &left_node, NULL);
10478
10479
15.1k
  if (left_node.op_type == IS_TMP_VAR) {
10480
12.2k
    SET_NODE(opline_jmpz->result, &left_node);
10481
12.2k
    GET_NODE(result, opline_jmpz->result);
10482
12.2k
  } else {
10483
2.90k
    zend_make_tmp_result(result, opline_jmpz);
10484
2.90k
  }
10485
10486
15.1k
  zend_compile_expr(&right_node, right_ast);
10487
10488
15.1k
  opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
10489
15.1k
  SET_NODE(opline_bool->result, result);
10490
10491
15.1k
  zend_update_jump_target_to_next(opnum_jmpz);
10492
15.1k
}
10493
/* }}} */
10494
10495
static void zend_compile_post_incdec(znode *result, const zend_ast *ast) /* {{{ */
10496
4.65k
{
10497
4.65k
  zend_ast *var_ast = ast->child[0];
10498
4.65k
  ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
10499
10500
4.65k
  zend_ensure_writable_variable(var_ast);
10501
10502
4.65k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10503
256
    zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, false);
10504
256
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
10505
256
    zend_make_tmp_result(result, opline);
10506
4.39k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10507
1.77k
    zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, false, false);
10508
1.77k
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP;
10509
1.77k
    zend_make_tmp_result(result, opline);
10510
2.62k
  } else {
10511
2.62k
    znode var_node;
10512
2.62k
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10513
2.62k
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10514
29
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10515
29
    }
10516
2.62k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC,
10517
2.62k
      &var_node, NULL);
10518
2.62k
  }
10519
4.65k
}
10520
/* }}} */
10521
10522
static void zend_compile_pre_incdec(znode *result, const zend_ast *ast) /* {{{ */
10523
1.90k
{
10524
1.90k
  zend_ast *var_ast = ast->child[0];
10525
1.90k
  ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
10526
10527
1.90k
  zend_ensure_writable_variable(var_ast);
10528
10529
1.90k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10530
299
    zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, false);
10531
299
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;
10532
299
    opline->result_type = IS_TMP_VAR;
10533
299
    result->op_type = IS_TMP_VAR;
10534
1.60k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10535
244
    zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, false, false);
10536
244
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP;
10537
244
    opline->result_type = IS_TMP_VAR;
10538
244
    result->op_type = IS_TMP_VAR;
10539
1.36k
  } else {
10540
1.36k
    znode var_node;
10541
1.36k
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10542
1.36k
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10543
40
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10544
40
    }
10545
1.36k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC,
10546
1.36k
      &var_node, NULL);
10547
1.36k
  }
10548
1.90k
}
10549
/* }}} */
10550
10551
static void zend_compile_cast(znode *result, const zend_ast *ast) /* {{{ */
10552
2.15k
{
10553
2.15k
  zend_ast *expr_ast = ast->child[0];
10554
2.15k
  znode expr_node;
10555
2.15k
  zend_op *opline;
10556
10557
2.15k
  zend_compile_expr(&expr_node, expr_ast);
10558
10559
2.15k
  if (ast->attr == _IS_BOOL) {
10560
253
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL);
10561
1.89k
  } else if (ast->attr == IS_NULL) {
10562
14
    zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
10563
1.88k
  } else {
10564
1.88k
    opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
10565
1.88k
    opline->extended_value = ast->attr;
10566
1.88k
  }
10567
2.15k
}
10568
/* }}} */
10569
10570
static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */
10571
3.05k
{
10572
3.05k
  zend_ast *cond_ast = ast->child[0];
10573
3.05k
  zend_ast *false_ast = ast->child[2];
10574
10575
3.05k
  znode cond_node, false_node;
10576
3.05k
  zend_op *opline_qm_assign;
10577
3.05k
  uint32_t opnum_jmp_set;
10578
10579
3.05k
  ZEND_ASSERT(ast->child[1] == NULL);
10580
10581
3.05k
  zend_compile_expr(&cond_node, cond_ast);
10582
10583
3.05k
  opnum_jmp_set = get_next_op_number();
10584
3.05k
  zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL);
10585
10586
3.05k
  zend_compile_expr(&false_node, false_ast);
10587
10588
3.05k
  opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10589
3.05k
  SET_NODE(opline_qm_assign->result, result);
10590
10591
3.05k
  zend_update_jump_target_to_next(opnum_jmp_set);
10592
3.05k
}
10593
/* }}} */
10594
10595
static void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
10596
7.06k
{
10597
7.06k
  zend_ast *cond_ast = ast->child[0];
10598
7.06k
  zend_ast *true_ast = ast->child[1];
10599
7.06k
  zend_ast *false_ast = ast->child[2];
10600
10601
7.06k
  znode cond_node, true_node, false_node;
10602
7.06k
  zend_op *opline_qm_assign2;
10603
7.06k
  uint32_t opnum_jmpz, opnum_jmp;
10604
10605
7.06k
  if (cond_ast->kind == ZEND_AST_CONDITIONAL
10606
1.99k
      && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) {
10607
1.75k
    if (cond_ast->child[1]) {
10608
23
      if (true_ast) {
10609
10
        zend_error(E_COMPILE_ERROR,
10610
10
          "Unparenthesized `a ? b : c ? d : e` is not supported. "
10611
10
          "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`");
10612
13
      } else {
10613
13
        zend_error(E_COMPILE_ERROR,
10614
13
          "Unparenthesized `a ? b : c ?: d` is not supported. "
10615
13
          "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`");
10616
13
      }
10617
1.73k
    } else {
10618
1.73k
      if (true_ast) {
10619
6
        zend_error(E_COMPILE_ERROR,
10620
6
          "Unparenthesized `a ?: b ? c : d` is not supported. "
10621
6
          "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`");
10622
1.72k
      } else {
10623
        /* This case is harmless:  (a ?: b) ?: c always produces the same result
10624
         * as a ?: (b ?: c). */
10625
1.72k
      }
10626
1.73k
    }
10627
1.75k
  }
10628
10629
7.06k
  if (!true_ast) {
10630
3.05k
    zend_compile_shorthand_conditional(result, ast);
10631
3.05k
    return;
10632
3.05k
  }
10633
10634
4.01k
  zend_compile_expr(&cond_node, cond_ast);
10635
10636
4.01k
  opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
10637
10638
4.01k
  zend_compile_expr(&true_node, true_ast);
10639
10640
4.01k
  zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL);
10641
10642
4.01k
  opnum_jmp = zend_emit_jump(0);
10643
10644
4.01k
  zend_update_jump_target_to_next(opnum_jmpz);
10645
10646
4.01k
  zend_compile_expr(&false_node, false_ast);
10647
10648
4.01k
  opline_qm_assign2 = zend_emit_op(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10649
4.01k
  SET_NODE(opline_qm_assign2->result, result);
10650
10651
4.01k
  zend_update_jump_target_to_next(opnum_jmp);
10652
4.01k
}
10653
/* }}} */
10654
10655
static void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
10656
1.87M
{
10657
1.87M
  zend_ast *expr_ast = ast->child[0];
10658
1.87M
  zend_ast *default_ast = ast->child[1];
10659
10660
1.87M
  znode expr_node, default_node;
10661
1.87M
  zend_op *opline;
10662
1.87M
  uint32_t opnum;
10663
10664
1.87M
  zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, false);
10665
10666
1.87M
  opnum = get_next_op_number();
10667
1.87M
  zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL);
10668
10669
1.87M
  zend_compile_expr(&default_node, default_ast);
10670
10671
1.87M
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL);
10672
1.87M
  SET_NODE(opline->result, result);
10673
10674
1.87M
  opline = &CG(active_op_array)->opcodes[opnum];
10675
1.87M
  opline->op2.opline_num = get_next_op_number();
10676
1.87M
}
10677
/* }}} */
10678
10679
84.7k
static void znode_dtor(zval *zv) {
10680
84.7k
  znode *node = Z_PTR_P(zv);
10681
84.7k
  if (node->op_type == IS_CONST) {
10682
4.36k
    zval_ptr_dtor_nogc(&node->u.constant);
10683
4.36k
  }
10684
84.7k
  efree(node);
10685
84.7k
}
10686
10687
static void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */
10688
16.0k
{
10689
16.0k
  zend_ast *var_ast = ast->child[0];
10690
16.0k
  zend_ast *default_ast = ast->child[1];
10691
10692
16.0k
  znode var_node_is, var_node_w, default_node, assign_node, *node;
10693
16.0k
  zend_op *opline;
10694
16.0k
  uint32_t coalesce_opnum;
10695
16.0k
  bool need_frees = false;
10696
10697
  /* Remember expressions compiled during the initial BP_VAR_IS lookup,
10698
   * to avoid double-evaluation when we compile again with BP_VAR_W. */
10699
16.0k
  HashTable *orig_memoized_exprs = CG(memoized_exprs);
10700
16.0k
  const zend_memoize_mode orig_memoize_mode = CG(memoize_mode);
10701
10702
16.0k
  zend_ensure_writable_variable(var_ast);
10703
16.0k
  if (is_this_fetch(var_ast)) {
10704
2
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
10705
2
  }
10706
10707
16.0k
  ALLOC_HASHTABLE(CG(memoized_exprs));
10708
16.0k
  zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0);
10709
10710
16.0k
  CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
10711
16.0k
  zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, false);
10712
10713
16.0k
  coalesce_opnum = get_next_op_number();
10714
16.0k
  zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL);
10715
10716
16.0k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
10717
16.0k
  if (var_ast->kind == ZEND_AST_DIM) {
10718
13.2k
    zend_compile_expr_with_potential_assign_to_self(&default_node, default_ast, var_ast);
10719
13.2k
  } else {
10720
2.85k
    zend_compile_expr(&default_node, default_ast);
10721
2.85k
  }
10722
10723
16.0k
  CG(memoize_mode) = ZEND_MEMOIZE_FETCH;
10724
16.0k
  zend_compile_var(&var_node_w, var_ast, BP_VAR_W, false);
10725
10726
  /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */
10727
16.0k
  opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
10728
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
10729
16.0k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
10730
16.0k
  switch (kind) {
10731
1.62k
    case ZEND_AST_VAR:
10732
1.62k
      zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node);
10733
1.62k
      break;
10734
575
    case ZEND_AST_STATIC_PROP:
10735
575
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
10736
575
      opline->result_type = IS_TMP_VAR;
10737
575
      var_node_w.op_type = IS_TMP_VAR;
10738
575
      zend_emit_op_data(&default_node);
10739
575
      assign_node = var_node_w;
10740
575
      break;
10741
12.9k
    case ZEND_AST_DIM:
10742
12.9k
      opline->opcode = ZEND_ASSIGN_DIM;
10743
12.9k
      opline->result_type = IS_TMP_VAR;
10744
12.9k
      var_node_w.op_type = IS_TMP_VAR;
10745
12.9k
      zend_emit_op_data(&default_node);
10746
12.9k
      assign_node = var_node_w;
10747
12.9k
      break;
10748
742
    case ZEND_AST_PROP:
10749
742
    case ZEND_AST_NULLSAFE_PROP:
10750
742
      opline->opcode = ZEND_ASSIGN_OBJ;
10751
742
      opline->result_type = IS_TMP_VAR;
10752
742
      var_node_w.op_type = IS_TMP_VAR;
10753
742
      zend_emit_op_data(&default_node);
10754
742
      assign_node = var_node_w;
10755
742
      break;
10756
0
    EMPTY_SWITCH_DEFAULT_CASE();
10757
16.0k
  }
10758
10759
15.9k
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL);
10760
15.9k
  SET_NODE(opline->result, result);
10761
10762
50.9k
  ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10763
50.9k
    if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10764
12.8k
      need_frees = true;
10765
12.8k
      break;
10766
12.8k
    }
10767
50.9k
  } ZEND_HASH_FOREACH_END();
10768
10769
  /* Free DUPed expressions if there are any */
10770
15.9k
  if (need_frees) {
10771
12.8k
    uint32_t jump_opnum = zend_emit_jump(0);
10772
12.8k
    zend_update_jump_target_to_next(coalesce_opnum);
10773
174k
    ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10774
174k
      if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10775
78.8k
        zend_emit_op(NULL, ZEND_FREE, node, NULL);
10776
78.8k
      }
10777
174k
    } ZEND_HASH_FOREACH_END();
10778
12.8k
    zend_update_jump_target_to_next(jump_opnum);
10779
12.8k
  } else {
10780
3.02k
    zend_update_jump_target_to_next(coalesce_opnum);
10781
3.02k
  }
10782
10783
15.9k
  zend_hash_destroy(CG(memoized_exprs));
10784
15.9k
  FREE_HASHTABLE(CG(memoized_exprs));
10785
15.9k
  CG(memoized_exprs) = orig_memoized_exprs;
10786
15.9k
  CG(memoize_mode) = orig_memoize_mode;
10787
15.9k
}
10788
/* }}} */
10789
10790
static void zend_compile_print(znode *result, const zend_ast *ast) /* {{{ */
10791
2.05k
{
10792
2.05k
  zend_op *opline;
10793
2.05k
  zend_ast *expr_ast = ast->child[0];
10794
10795
2.05k
  znode expr_node;
10796
2.05k
  zend_compile_expr(&expr_node, expr_ast);
10797
10798
2.05k
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
10799
2.05k
  opline->extended_value = 1;
10800
10801
2.05k
  result->op_type = IS_CONST;
10802
2.05k
  ZVAL_LONG(&result->u.constant, 1);
10803
2.05k
}
10804
/* }}} */
10805
10806
static void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
10807
13.4k
{
10808
13.4k
  zend_ast *value_ast = ast->child[0];
10809
13.4k
  zend_ast *key_ast = ast->child[1];
10810
10811
13.4k
  znode value_node, key_node;
10812
13.4k
  znode *value_node_ptr = NULL, *key_node_ptr = NULL;
10813
13.4k
  zend_op *opline;
10814
13.4k
  bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
10815
10816
13.4k
  zend_mark_function_as_generator();
10817
10818
13.4k
  if (key_ast) {
10819
245
    zend_compile_expr(&key_node, key_ast);
10820
245
    key_node_ptr = &key_node;
10821
245
  }
10822
10823
13.4k
  if (value_ast) {
10824
10.3k
    if (returns_by_ref && zend_is_variable(value_ast)) {
10825
44
      zend_assert_not_short_circuited(value_ast);
10826
44
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
10827
10.3k
    } else {
10828
10.3k
      zend_compile_expr(&value_node, value_ast);
10829
10.3k
    }
10830
10.3k
    value_node_ptr = &value_node;
10831
10.3k
  }
10832
10833
13.4k
  opline = zend_emit_op(result, ZEND_YIELD, value_node_ptr, key_node_ptr);
10834
10835
13.4k
  if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
10836
764
    opline->extended_value = ZEND_RETURNS_FUNCTION;
10837
764
  }
10838
13.4k
}
10839
/* }}} */
10840
10841
static void zend_compile_yield_from(znode *result, const zend_ast *ast) /* {{{ */
10842
1.78k
{
10843
1.78k
  zend_ast *expr_ast = ast->child[0];
10844
1.78k
  znode expr_node;
10845
10846
1.78k
  zend_mark_function_as_generator();
10847
10848
1.78k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
10849
2
    zend_error_noreturn(E_COMPILE_ERROR,
10850
2
      "Cannot use \"yield from\" inside a by-reference generator");
10851
2
  }
10852
10853
1.78k
  zend_compile_expr(&expr_node, expr_ast);
10854
1.78k
  zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
10855
1.78k
}
10856
/* }}} */
10857
10858
static void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */
10859
666
{
10860
666
  zend_ast *obj_ast = ast->child[0];
10861
666
  zend_ast *class_ast = ast->child[1];
10862
10863
666
  znode obj_node, class_node;
10864
666
  zend_op *opline;
10865
10866
666
  zend_compile_expr(&obj_node, obj_ast);
10867
666
  if (obj_node.op_type == IS_CONST) {
10868
84
    zend_do_free(&obj_node);
10869
84
    result->op_type = IS_CONST;
10870
84
    ZVAL_FALSE(&result->u.constant);
10871
84
    return;
10872
84
  }
10873
10874
582
  zend_compile_class_ref(&class_node, class_ast,
10875
582
    ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION | ZEND_FETCH_CLASS_SILENT);
10876
10877
582
  opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL);
10878
10879
582
  if (class_node.op_type == IS_CONST) {
10880
113
    opline->op2_type = IS_CONST;
10881
113
    opline->op2.constant = zend_add_class_name_literal(
10882
113
      Z_STR(class_node.u.constant));
10883
113
    opline->extended_value = zend_alloc_cache_slot();
10884
469
  } else {
10885
469
    SET_NODE(opline->op2, &class_node);
10886
469
  }
10887
582
}
10888
/* }}} */
10889
10890
static void zend_compile_include_or_eval(znode *result, const zend_ast *ast) /* {{{ */
10891
4.63k
{
10892
4.63k
  zend_ast *expr_ast = ast->child[0];
10893
4.63k
  znode expr_node;
10894
4.63k
  zend_op *opline;
10895
10896
4.63k
  zend_do_extended_fcall_begin();
10897
4.63k
  zend_compile_expr(&expr_node, expr_ast);
10898
10899
4.63k
  opline = zend_emit_op(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL);
10900
4.63k
  opline->extended_value = ast->attr;
10901
10902
4.63k
  zend_do_extended_fcall_end();
10903
4.63k
}
10904
/* }}} */
10905
10906
static void zend_compile_isset_or_empty(znode *result, const zend_ast *ast) /* {{{ */
10907
10.3k
{
10908
10.3k
  zend_ast *var_ast = ast->child[0];
10909
10910
10.3k
  znode var_node;
10911
10.3k
  zend_op *opline = NULL;
10912
10913
10.3k
  ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY);
10914
10915
10.3k
  if (!zend_is_variable(var_ast)) {
10916
121
    if (ast->kind == ZEND_AST_EMPTY) {
10917
      /* empty(expr) can be transformed to !expr */
10918
86
      zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast);
10919
86
      zend_compile_expr(result, not_ast);
10920
86
      return;
10921
86
    } else {
10922
35
      zend_error_noreturn(E_COMPILE_ERROR,
10923
35
        "Cannot use isset() on the result of an expression "
10924
35
        "(you can use \"null !== expression\" instead)");
10925
35
    }
10926
121
  }
10927
10928
10.1k
  if (is_globals_fetch(var_ast)) {
10929
162
    result->op_type = IS_CONST;
10930
162
    ZVAL_BOOL(&result->u.constant, ast->kind == ZEND_AST_ISSET);
10931
162
    return;
10932
162
  }
10933
10934
10.0k
  if (is_global_var_fetch(var_ast)) {
10935
4.35k
    if (!var_ast->child[1]) {
10936
3
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
10937
3
    }
10938
10939
4.34k
    zend_compile_expr(&var_node, var_ast->child[1]);
10940
4.34k
    if (var_node.op_type == IS_CONST) {
10941
3.94k
      convert_to_string(&var_node.u.constant);
10942
3.94k
    }
10943
10944
4.34k
    opline = zend_emit_op_tmp(result, ZEND_ISSET_ISEMPTY_VAR, &var_node, NULL);
10945
4.34k
    opline->extended_value =
10946
4.34k
      ZEND_FETCH_GLOBAL | (ast->kind == ZEND_AST_EMPTY ? ZEND_ISEMPTY : 0);
10947
4.34k
    return;
10948
4.35k
  }
10949
10950
5.67k
  zend_short_circuiting_mark_inner(var_ast);
10951
5.67k
  switch (var_ast->kind) {
10952
2.99k
    case ZEND_AST_VAR:
10953
2.99k
      if (is_this_fetch(var_ast)) {
10954
496
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL);
10955
496
        CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
10956
2.49k
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_IS) == SUCCESS) {
10957
1.76k
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL);
10958
1.76k
      } else {
10959
728
        opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, false);
10960
728
        opline->opcode = ZEND_ISSET_ISEMPTY_VAR;
10961
728
      }
10962
2.99k
      break;
10963
2.07k
    case ZEND_AST_DIM:
10964
2.07k
      opline = zend_compile_dim(result, var_ast, BP_VAR_IS, /* by_ref */ false);
10965
2.07k
      opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
10966
2.07k
      break;
10967
206
    case ZEND_AST_PROP:
10968
466
    case ZEND_AST_NULLSAFE_PROP:
10969
466
      opline = zend_compile_prop(result, var_ast, BP_VAR_IS, false);
10970
466
      opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
10971
466
      break;
10972
137
    case ZEND_AST_STATIC_PROP:
10973
137
      opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, false, false);
10974
137
      opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP;
10975
137
      break;
10976
5.67k
    EMPTY_SWITCH_DEFAULT_CASE()
10977
5.67k
  }
10978
10979
5.66k
  result->op_type = opline->result_type = IS_TMP_VAR;
10980
5.66k
  if (!(ast->kind == ZEND_AST_ISSET)) {
10981
587
    opline->extended_value |= ZEND_ISEMPTY;
10982
587
  }
10983
5.66k
}
10984
/* }}} */
10985
10986
static void zend_compile_silence(znode *result, const zend_ast *ast) /* {{{ */
10987
14.8M
{
10988
14.8M
  zend_ast *expr_ast = ast->child[0];
10989
14.8M
  znode silence_node;
10990
10991
14.8M
  zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL);
10992
10993
14.8M
  if (expr_ast->kind == ZEND_AST_VAR) {
10994
    /* For @$var we need to force a FETCH instruction, otherwise the CV access will
10995
     * happen outside the silenced section. */
10996
132k
    zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, false );
10997
14.7M
  } else {
10998
14.7M
    zend_compile_expr(result, expr_ast);
10999
14.7M
  }
11000
11001
14.8M
  zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL);
11002
14.8M
}
11003
/* }}} */
11004
11005
static void zend_compile_shell_exec(znode *result, const zend_ast *ast) /* {{{ */
11006
29.2k
{
11007
29.2k
  zend_ast *expr_ast = ast->child[0];
11008
11009
29.2k
  zval fn_name;
11010
29.2k
  zend_ast *name_ast, *args_ast, *call_ast;
11011
11012
29.2k
  zend_error(E_DEPRECATED, "The backtick (`) operator is deprecated, use shell_exec() instead");
11013
11014
29.2k
  ZVAL_STRING(&fn_name, "shell_exec");
11015
29.2k
  name_ast = zend_ast_create_zval(&fn_name);
11016
29.2k
  args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast);
11017
29.2k
  call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast);
11018
11019
29.2k
  zend_compile_expr(result, call_ast);
11020
11021
29.2k
  zval_ptr_dtor(&fn_name);
11022
29.2k
}
11023
/* }}} */
11024
11025
static void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
11026
106k
{
11027
106k
  zend_ast_list *list = zend_ast_get_list(ast);
11028
106k
  zend_op *opline;
11029
106k
  uint32_t i, opnum_init = -1;
11030
106k
  bool packed = true;
11031
11032
106k
  if (zend_try_ct_eval_array(&result->u.constant, ast)) {
11033
53.8k
    result->op_type = IS_CONST;
11034
53.8k
    return;
11035
53.8k
  }
11036
11037
  /* Empty arrays are handled at compile-time */
11038
52.2k
  ZEND_ASSERT(list->children > 0);
11039
11040
176k
  for (i = 0; i < list->children; ++i) {
11041
124k
    zend_ast *elem_ast = list->child[i];
11042
124k
    zend_ast *value_ast, *key_ast;
11043
124k
    bool by_ref;
11044
124k
    znode value_node, key_node, *key_node_ptr = NULL;
11045
11046
124k
    if (elem_ast == NULL) {
11047
0
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
11048
0
    }
11049
11050
124k
    value_ast = elem_ast->child[0];
11051
11052
124k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
11053
1.02k
      zend_compile_expr(&value_node, value_ast);
11054
1.02k
      if (i == 0) {
11055
954
        opnum_init = get_next_op_number();
11056
954
        opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
11057
954
      }
11058
1.02k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL);
11059
1.02k
      SET_NODE(opline->result, result);
11060
1.02k
      continue;
11061
1.02k
    }
11062
11063
123k
    key_ast = elem_ast->child[1];
11064
123k
    by_ref = elem_ast->attr;
11065
11066
123k
    if (key_ast) {
11067
7.03k
      zend_compile_expr(&key_node, key_ast);
11068
7.03k
      zend_handle_numeric_op(&key_node);
11069
7.03k
      key_node_ptr = &key_node;
11070
7.03k
    }
11071
11072
123k
    if (by_ref) {
11073
141
      zend_ensure_writable_variable(value_ast);
11074
141
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
11075
123k
    } else {
11076
123k
      zend_compile_expr(&value_node, value_ast);
11077
123k
    }
11078
11079
123k
    if (i == 0) {
11080
50.9k
      opnum_init = get_next_op_number();
11081
50.9k
      opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr);
11082
50.9k
      opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT;
11083
72.2k
    } else {
11084
72.2k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT,
11085
72.2k
        &value_node, key_node_ptr);
11086
72.2k
      SET_NODE(opline->result, result);
11087
72.2k
    }
11088
123k
    opline->extended_value |= by_ref;
11089
11090
123k
    if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) {
11091
2.61k
      packed = false;
11092
2.61k
    }
11093
123k
  }
11094
11095
  /* Add a flag to INIT_ARRAY if we know this array cannot be packed */
11096
52.0k
  if (!packed) {
11097
1.42k
    ZEND_ASSERT(opnum_init != (uint32_t)-1);
11098
1.42k
    opline = &CG(active_op_array)->opcodes[opnum_init];
11099
1.42k
    opline->extended_value |= ZEND_ARRAY_NOT_PACKED;
11100
1.42k
  }
11101
52.0k
}
11102
/* }}} */
11103
11104
static void zend_compile_const(znode *result, const zend_ast *ast) /* {{{ */
11105
8.89M
{
11106
8.89M
  zend_ast *name_ast = ast->child[0];
11107
11108
8.89M
  zend_op *opline;
11109
11110
8.89M
  bool is_fully_qualified;
11111
8.89M
  zend_string *orig_name = zend_ast_get_str(name_ast);
11112
8.89M
  zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified);
11113
11114
8.89M
  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__"))) {
11115
534
    zend_ast *last = CG(ast);
11116
11117
1.06k
    while (last && last->kind == ZEND_AST_STMT_LIST) {
11118
873
      const zend_ast_list *list = zend_ast_get_list(last);
11119
873
      if (list->children == 0) {
11120
338
        break;
11121
338
      }
11122
535
      last = list->child[list->children-1];
11123
535
    }
11124
534
    if (last && last->kind == ZEND_AST_HALT_COMPILER) {
11125
49
      result->op_type = IS_CONST;
11126
49
      ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
11127
49
      zend_string_release_ex(resolved_name, 0);
11128
49
      return;
11129
49
    }
11130
534
  }
11131
11132
8.89M
  if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
11133
31.7k
    result->op_type = IS_CONST;
11134
31.7k
    zend_string_release_ex(resolved_name, 0);
11135
31.7k
    return;
11136
31.7k
  }
11137
11138
8.86M
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
11139
8.86M
  opline->op2_type = IS_CONST;
11140
11141
8.86M
  if (is_fully_qualified || !FC(current_namespace)) {
11142
172k
    opline->op1.num = 0;
11143
172k
    opline->op2.constant = zend_add_const_name_literal(
11144
172k
      resolved_name, false);
11145
8.69M
  } else {
11146
8.69M
    opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE;
11147
8.69M
    opline->op2.constant = zend_add_const_name_literal(
11148
8.69M
      resolved_name, true);
11149
8.69M
  }
11150
8.86M
  opline->extended_value = zend_alloc_cache_slot();
11151
8.86M
}
11152
/* }}} */
11153
11154
static void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */
11155
190k
{
11156
190k
  zend_ast *class_ast;
11157
190k
  zend_ast *const_ast;
11158
190k
  znode class_node, const_node;
11159
190k
  zend_op *opline;
11160
11161
190k
  zend_eval_const_expr(&ast->child[0]);
11162
190k
  zend_eval_const_expr(&ast->child[1]);
11163
11164
190k
  class_ast = ast->child[0];
11165
190k
  const_ast = ast->child[1];
11166
11167
190k
  if (class_ast->kind == ZEND_AST_ZVAL && const_ast->kind == ZEND_AST_ZVAL) {
11168
73.5k
    zval *const_zv = zend_ast_get_zval(const_ast);
11169
73.5k
    if (Z_TYPE_P(const_zv) == IS_STRING) {
11170
72.7k
      zend_string *const_str = Z_STR_P(const_zv);
11171
72.7k
      zend_string *resolved_name = zend_resolve_class_name_ast(class_ast);
11172
72.7k
      if (zend_try_ct_eval_class_const(&result->u.constant, resolved_name, const_str)) {
11173
70
        result->op_type = IS_CONST;
11174
70
        zend_string_release_ex(resolved_name, 0);
11175
70
        return;
11176
70
      }
11177
72.7k
      zend_string_release_ex(resolved_name, 0);
11178
72.7k
    }
11179
73.5k
  }
11180
11181
190k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
11182
11183
190k
  zend_compile_expr(&const_node, const_ast);
11184
11185
190k
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node);
11186
11187
190k
  zend_set_class_name_op1(opline, &class_node);
11188
11189
190k
  if (opline->op1_type == IS_CONST || opline->op2_type == IS_CONST) {
11190
190k
    opline->extended_value = zend_alloc_cache_slots(2);
11191
190k
  }
11192
190k
}
11193
/* }}} */
11194
11195
static void zend_compile_class_name(znode *result, const zend_ast *ast) /* {{{ */
11196
2.53k
{
11197
2.53k
  zend_ast *class_ast = ast->child[0];
11198
11199
2.53k
  if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) {
11200
733
    result->op_type = IS_CONST;
11201
733
    return;
11202
733
  }
11203
11204
1.80k
  if (class_ast->kind == ZEND_AST_ZVAL) {
11205
981
    zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11206
981
    opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
11207
981
  } else {
11208
825
    znode expr_node;
11209
825
    zend_compile_expr(&expr_node, class_ast);
11210
825
    if (expr_node.op_type == IS_CONST) {
11211
      /* Unlikely case that happen if class_ast is constant folded.
11212
       * Handle it here, to avoid needing a CONST specialization in the VM. */
11213
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"::class\" on %s",
11214
7
        zend_zval_value_name(&expr_node.u.constant));
11215
7
    }
11216
11217
818
    zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL);
11218
818
  }
11219
1.80k
}
11220
/* }}} */
11221
11222
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */
11223
139k
{
11224
139k
  if (num == 0) {
11225
15.8k
    result->op_type = IS_TMP_VAR;
11226
15.8k
    result->u.op.var = -1;
11227
15.8k
    opline->opcode = ZEND_ROPE_INIT;
11228
124k
  } else {
11229
124k
    opline->opcode = ZEND_ROPE_ADD;
11230
124k
    SET_NODE(opline->op1, result);
11231
124k
  }
11232
139k
  SET_NODE(opline->op2, elem_node);
11233
139k
  SET_NODE(opline->result, result);
11234
139k
  opline->extended_value = num;
11235
139k
  return opline;
11236
139k
}
11237
/* }}} */
11238
11239
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */
11240
140k
{
11241
140k
  zend_op *opline = get_next_op();
11242
11243
140k
  if (num == 0) {
11244
8.10k
    result->op_type = IS_TMP_VAR;
11245
8.10k
    result->u.op.var = -1;
11246
8.10k
    opline->opcode = ZEND_ROPE_INIT;
11247
132k
  } else {
11248
132k
    opline->opcode = ZEND_ROPE_ADD;
11249
132k
    SET_NODE(opline->op1, result);
11250
132k
  }
11251
140k
  SET_NODE(opline->op2, elem_node);
11252
140k
  SET_NODE(opline->result, result);
11253
140k
  opline->extended_value = num;
11254
140k
  return opline;
11255
140k
}
11256
/* }}} */
11257
11258
static void zend_compile_rope_finalize(znode *result, uint32_t rope_elements, zend_op *init_opline, zend_op *opline)
11259
23.9k
{
11260
23.9k
  if (rope_elements == 1) {
11261
2.48k
    if (opline->op2_type == IS_CONST) {
11262
601
      GET_NODE(result, opline->op2);
11263
601
      ZVAL_UNDEF(CT_CONSTANT(opline->op2));
11264
601
      SET_UNUSED(opline->op2);
11265
601
      MAKE_NOP(opline);
11266
1.88k
    } else {
11267
1.88k
      opline->opcode = ZEND_CAST;
11268
1.88k
      opline->extended_value = IS_STRING;
11269
1.88k
      opline->op1_type = opline->op2_type;
11270
1.88k
      opline->op1 = opline->op2;
11271
1.88k
      SET_UNUSED(opline->op2);
11272
1.88k
      zend_make_tmp_result(result, opline);
11273
1.88k
    }
11274
21.4k
  } else if (rope_elements == 2) {
11275
3.30k
    opline->opcode = ZEND_FAST_CONCAT;
11276
3.30k
    opline->extended_value = 0;
11277
3.30k
    opline->op1_type = init_opline->op2_type;
11278
3.30k
    opline->op1 = init_opline->op2;
11279
3.30k
    zend_make_tmp_result(result, opline);
11280
3.30k
    MAKE_NOP(init_opline);
11281
18.1k
  } else {
11282
18.1k
    uint32_t var;
11283
11284
18.1k
    init_opline->extended_value = rope_elements;
11285
18.1k
    opline->opcode = ZEND_ROPE_END;
11286
18.1k
    zend_make_tmp_result(result, opline);
11287
18.1k
    var = opline->op1.var = get_temporary_variable();
11288
11289
    /* Allocates the necessary number of zval slots to keep the rope */
11290
18.1k
    uint32_t i = ((rope_elements * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
11291
143k
    while (i > 1) {
11292
125k
      get_temporary_variable();
11293
125k
      i--;
11294
125k
    }
11295
11296
    /* Update all the previous opcodes to use the same variable */
11297
384k
    while (opline != init_opline) {
11298
366k
      opline--;
11299
366k
      if (opline->opcode == ZEND_ROPE_ADD &&
11300
236k
          opline->result.var == (uint32_t)-1) {
11301
235k
        opline->op1.var = var;
11302
235k
        opline->result.var = var;
11303
235k
      } else if (opline->opcode == ZEND_ROPE_INIT &&
11304
18.8k
                 opline->result.var == (uint32_t)-1) {
11305
18.1k
        opline->result.var = var;
11306
18.1k
      }
11307
366k
    }
11308
18.1k
  }
11309
23.9k
}
11310
11311
static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
11312
22.1k
{
11313
22.1k
  uint32_t i, j;
11314
22.1k
  uint32_t rope_init_lineno = -1;
11315
22.1k
  zend_op *opline = NULL, *init_opline;
11316
22.1k
  znode elem_node, last_const_node;
11317
22.1k
  zend_ast_list *list = zend_ast_get_list(ast);
11318
22.1k
  uint32_t reserved_op_number = -1;
11319
11320
22.1k
  ZEND_ASSERT(list->children > 0);
11321
11322
22.1k
  j = 0;
11323
22.1k
  last_const_node.op_type = IS_UNUSED;
11324
298k
  for (i = 0; i < list->children; i++) {
11325
275k
    zend_ast *encaps_var = list->child[i];
11326
11327
275k
    if (encaps_var->attr & (ZEND_ENCAPS_VAR_DOLLAR_CURLY|ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11328
4.09k
      if ((encaps_var->kind == ZEND_AST_VAR || encaps_var->kind == ZEND_AST_DIM) && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY)) {
11329
743
        zend_error(E_DEPRECATED, "Using ${var} in strings is deprecated, use {$var} instead");
11330
3.34k
      } else if (encaps_var->kind == ZEND_AST_VAR && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11331
3.34k
        zend_error(E_DEPRECATED, "Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead");
11332
3.34k
      }
11333
4.09k
    }
11334
11335
275k
    zend_compile_expr(&elem_node, encaps_var);
11336
11337
275k
    if (elem_node.op_type == IS_CONST) {
11338
140k
      convert_to_string(&elem_node.u.constant);
11339
11340
140k
      if (Z_STRLEN(elem_node.u.constant) == 0) {
11341
799
        zval_ptr_dtor(&elem_node.u.constant);
11342
139k
      } else if (last_const_node.op_type == IS_CONST) {
11343
0
        concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant);
11344
0
        zval_ptr_dtor(&elem_node.u.constant);
11345
139k
      } else {
11346
139k
        last_const_node.op_type = IS_CONST;
11347
139k
        ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant);
11348
        /* Reserve place for ZEND_ROPE_ADD instruction */
11349
139k
        reserved_op_number = get_next_op_number();
11350
139k
        opline = get_next_op();
11351
139k
        opline->opcode = ZEND_NOP;
11352
139k
      }
11353
140k
      continue;
11354
140k
    } else {
11355
135k
      if (j == 0) {
11356
22.1k
        if (last_const_node.op_type == IS_CONST) {
11357
15.8k
          rope_init_lineno = reserved_op_number;
11358
15.8k
        } else {
11359
6.31k
          rope_init_lineno = get_next_op_number();
11360
6.31k
        }
11361
22.1k
      }
11362
135k
      if (last_const_node.op_type == IS_CONST) {
11363
121k
        opline = &CG(active_op_array)->opcodes[reserved_op_number];
11364
121k
        zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11365
121k
        last_const_node.op_type = IS_UNUSED;
11366
121k
      }
11367
135k
      opline = zend_compile_rope_add(result, j++, &elem_node);
11368
135k
    }
11369
275k
  }
11370
11371
22.1k
  if (j == 0) {
11372
0
    result->op_type = IS_CONST;
11373
0
    if (last_const_node.op_type == IS_CONST) {
11374
0
      ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant);
11375
0
    } else {
11376
0
      ZVAL_EMPTY_STRING(&result->u.constant);
11377
      /* empty string */
11378
0
    }
11379
0
    CG(active_op_array)->last = reserved_op_number - 1;
11380
0
    return;
11381
22.1k
  } else if (last_const_node.op_type == IS_CONST) {
11382
18.4k
    opline = &CG(active_op_array)->opcodes[reserved_op_number];
11383
18.4k
    opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11384
18.4k
  }
11385
22.1k
  init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
11386
22.1k
  zend_compile_rope_finalize(result, j, init_opline, opline);
11387
22.1k
}
11388
/* }}} */
11389
11390
static void zend_compile_magic_const(znode *result, const zend_ast *ast) /* {{{ */
11391
13.8k
{
11392
13.8k
  zend_op *opline;
11393
11394
13.8k
  if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) {
11395
13.8k
    result->op_type = IS_CONST;
11396
13.8k
    return;
11397
13.8k
  }
11398
11399
44
  ZEND_ASSERT(ast->attr == T_CLASS_C &&
11400
44
              CG(active_class_entry) &&
11401
44
              (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
11402
11403
44
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11404
44
  opline->op1.num = ZEND_FETCH_CLASS_SELF;
11405
44
}
11406
/* }}} */
11407
11408
static bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */
11409
42.2k
{
11410
42.2k
  return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP
11411
35.8k
    || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL
11412
34.7k
    || kind == ZEND_AST_AND || kind == ZEND_AST_OR
11413
34.3k
    || kind == ZEND_AST_UNARY_OP
11414
33.1k
    || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS
11415
32.5k
    || kind == ZEND_AST_CAST
11416
32.2k
    || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM
11417
30.9k
    || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM
11418
28.6k
    || kind == ZEND_AST_UNPACK
11419
28.6k
    || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST
11420
5.38k
    || kind == ZEND_AST_CLASS_NAME
11421
5.37k
    || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE
11422
4.56k
    || kind == ZEND_AST_CONST_ENUM_INIT
11423
3.67k
    || kind == ZEND_AST_NEW || kind == ZEND_AST_ARG_LIST
11424
1.39k
    || kind == ZEND_AST_NAMED_ARG
11425
1.38k
    || kind == ZEND_AST_PROP || kind == ZEND_AST_NULLSAFE_PROP
11426
867
    || kind == ZEND_AST_CLOSURE
11427
837
    || kind == ZEND_AST_CALL || kind == ZEND_AST_STATIC_CALL || kind == ZEND_AST_CALLABLE_CONVERT;
11428
42.2k
}
11429
/* }}} */
11430
11431
static void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
11432
1.85k
{
11433
1.85k
  zend_ast *ast = *ast_ptr;
11434
1.85k
  zend_ast *class_ast = ast->child[0];
11435
1.85k
  zend_string *class_name;
11436
1.85k
  int fetch_type;
11437
11438
1.85k
  if (class_ast->kind != ZEND_AST_ZVAL) {
11439
11
    zend_error_noreturn(E_COMPILE_ERROR,
11440
11
      "Dynamic class names are not allowed in compile-time class constant references");
11441
11
  }
11442
1.84k
  if (Z_TYPE_P(zend_ast_get_zval(class_ast)) != IS_STRING) {
11443
3
    zend_throw_error(NULL, "Class name must be a valid object or a string");
11444
3
  }
11445
11446
1.84k
  class_name = zend_ast_get_str(class_ast);
11447
1.84k
  fetch_type = zend_get_class_fetch_type(class_name);
11448
11449
1.84k
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11450
2
    zend_error_noreturn(E_COMPILE_ERROR,
11451
2
      "\"static::\" is not allowed in compile-time constants");
11452
2
  }
11453
11454
1.84k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
11455
1.48k
    zend_string *tmp = zend_resolve_class_name_ast(class_ast);
11456
11457
1.48k
    zend_string_release_ex(class_name, 0);
11458
1.48k
    if (tmp != class_name) {
11459
483
      zval *zv = zend_ast_get_zval(class_ast);
11460
483
      ZVAL_STR(zv, tmp);
11461
483
      class_ast->attr = ZEND_NAME_FQ;
11462
483
    }
11463
1.48k
  }
11464
11465
1.84k
  ast->attr |= ZEND_FETCH_CLASS_EXCEPTION;
11466
1.84k
}
11467
/* }}} */
11468
11469
static void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */
11470
11
{
11471
11
  zend_ast *ast = *ast_ptr;
11472
11
  zend_ast *class_ast = ast->child[0];
11473
11
  if (class_ast->kind != ZEND_AST_ZVAL) {
11474
2
    zend_error_noreturn(E_COMPILE_ERROR,
11475
2
      "(expression)::class cannot be used in constant expressions");
11476
2
  }
11477
11478
9
  zend_string *class_name = zend_ast_get_str(class_ast);
11479
9
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
11480
11481
9
  switch (fetch_type) {
11482
6
    case ZEND_FETCH_CLASS_SELF:
11483
7
    case ZEND_FETCH_CLASS_PARENT:
11484
      /* For the const-eval representation store the fetch type instead of the name. */
11485
7
      zend_string_release(class_name);
11486
7
      ast->child[0] = NULL;
11487
7
      ast->attr = fetch_type;
11488
7
      return;
11489
2
    case ZEND_FETCH_CLASS_STATIC:
11490
2
      zend_error_noreturn(E_COMPILE_ERROR,
11491
2
        "static::class cannot be used for compile-time class name resolution");
11492
0
      return;
11493
9
    EMPTY_SWITCH_DEFAULT_CASE()
11494
9
  }
11495
9
}
11496
11497
static void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */
11498
21.3k
{
11499
21.3k
  zend_ast *ast = *ast_ptr;
11500
21.3k
  zend_ast *name_ast = ast->child[0];
11501
21.3k
  zend_string *orig_name = zend_ast_get_str(name_ast);
11502
21.3k
  bool is_fully_qualified;
11503
21.3k
  zval result;
11504
21.3k
  zend_string *resolved_name;
11505
11506
21.3k
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11507
11508
21.3k
  resolved_name = zend_resolve_const_name(
11509
21.3k
    orig_name, name_ast->attr, &is_fully_qualified);
11510
11511
21.3k
  if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
11512
0
    zend_string_release_ex(resolved_name, 0);
11513
0
    zend_ast_destroy(ast);
11514
0
    *ast_ptr = zend_ast_create_zval(&result);
11515
0
    return;
11516
0
  }
11517
11518
21.3k
  zend_ast_destroy(ast);
11519
21.3k
  *ast_ptr = zend_ast_create_constant(resolved_name,
11520
21.3k
    !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0);
11521
21.3k
}
11522
/* }}} */
11523
11524
static void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
11525
613
{
11526
613
  zend_ast *ast = *ast_ptr;
11527
11528
  /* Other cases already resolved by constant folding */
11529
613
  ZEND_ASSERT(ast->attr == T_CLASS_C);
11530
11531
613
  zend_ast_destroy(ast);
11532
613
  *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS);
11533
613
}
11534
/* }}} */
11535
11536
static void zend_compile_const_expr_class_reference(zend_ast *class_ast)
11537
1.18k
{
11538
1.18k
  if (class_ast->kind == ZEND_AST_CLASS) {
11539
0
    zend_error_noreturn(E_COMPILE_ERROR,
11540
0
      "Cannot use anonymous class in constant expression");
11541
0
  }
11542
1.18k
  if (class_ast->kind != ZEND_AST_ZVAL) {
11543
5
    zend_error_noreturn(E_COMPILE_ERROR,
11544
5
      "Cannot use dynamic class name in constant expression");
11545
5
  }
11546
11547
1.18k
  zend_string *class_name = zend_resolve_class_name_ast(class_ast);
11548
1.18k
  int fetch_type = zend_get_class_fetch_type(class_name);
11549
1.18k
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11550
1
    zend_error_noreturn(E_COMPILE_ERROR,
11551
1
      "\"static\" is not allowed in compile-time constants");
11552
1
  }
11553
11554
1.18k
  zval *class_ast_zv = zend_ast_get_zval(class_ast);
11555
1.18k
  zval_ptr_dtor_nogc(class_ast_zv);
11556
1.18k
  ZVAL_STR(class_ast_zv, class_name);
11557
1.18k
  class_ast->attr = fetch_type << ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT;
11558
1.18k
}
11559
11560
static void zend_compile_const_expr_new(zend_ast **ast_ptr)
11561
1.14k
{
11562
1.14k
  zend_ast *class_ast = (*ast_ptr)->child[0];
11563
1.14k
  zend_compile_const_expr_class_reference(class_ast);
11564
11565
1.14k
  const zend_ast *args_ast = (*ast_ptr)->child[1];
11566
1.14k
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
11567
2
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
11568
2
  }
11569
1.14k
}
11570
11571
static void zend_compile_const_expr_closure(zend_ast **ast_ptr)
11572
30
{
11573
30
  zend_ast_decl *closure_ast = (zend_ast_decl *) *ast_ptr;
11574
30
  const zend_ast *uses_ast = closure_ast->child[1];
11575
30
  if (!(closure_ast->flags & ZEND_ACC_STATIC)) {
11576
6
    zend_error_noreturn(E_COMPILE_ERROR,
11577
6
      "Closures in constant expressions must be static");
11578
6
  }
11579
24
  if (uses_ast) {
11580
2
    zend_error_noreturn(E_COMPILE_ERROR,
11581
2
      "Cannot use(...) variables in constant expression");
11582
2
  }
11583
11584
22
  znode node;
11585
22
  zend_op_array *op = zend_compile_func_decl(&node, (zend_ast*)closure_ast, FUNC_DECL_LEVEL_CONSTEXPR);
11586
11587
22
  zend_ast_destroy(*ast_ptr);
11588
22
  *ast_ptr = zend_ast_create_op_array(op);
11589
22
}
11590
11591
static void zend_compile_const_expr_fcc(zend_ast **ast_ptr)
11592
419
{
11593
419
  zend_ast **args_ast;
11594
419
  switch ((*ast_ptr)->kind) {
11595
374
    case ZEND_AST_CALL:
11596
374
      args_ast = &(*ast_ptr)->child[1];
11597
374
      break;
11598
45
    case ZEND_AST_STATIC_CALL:
11599
45
      args_ast = &(*ast_ptr)->child[2];
11600
45
      break;
11601
0
    EMPTY_SWITCH_DEFAULT_CASE();
11602
419
  }
11603
419
  if ((*args_ast)->kind != ZEND_AST_CALLABLE_CONVERT) {
11604
9
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11605
9
  }
11606
410
  ZEND_MAP_PTR_NEW(((zend_ast_fcc *)*args_ast)->fptr);
11607
11608
410
  switch ((*ast_ptr)->kind) {
11609
366
    case ZEND_AST_CALL: {
11610
366
      zend_ast *name_ast = (*ast_ptr)->child[0];
11611
366
      if (name_ast->kind != ZEND_AST_ZVAL) {
11612
4
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic function name in constant expression");
11613
4
      }
11614
362
      zval *name_ast_zv = zend_ast_get_zval(name_ast);
11615
362
      if (Z_TYPE_P(name_ast_zv) != IS_STRING) {
11616
2
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal function name");
11617
2
      }
11618
362
      bool is_fully_qualified;
11619
360
      zend_string *name = zend_resolve_function_name(Z_STR_P(name_ast_zv), name_ast->attr, &is_fully_qualified);
11620
360
      zval_ptr_dtor_nogc(name_ast_zv);
11621
360
      ZVAL_STR(name_ast_zv, name);
11622
360
      if (is_fully_qualified) {
11623
75
        name_ast->attr = ZEND_NAME_FQ;
11624
75
      }
11625
360
      break;
11626
362
    }
11627
44
    case ZEND_AST_STATIC_CALL: {
11628
44
      zend_ast *class_ast = (*ast_ptr)->child[0];
11629
44
      zend_compile_const_expr_class_reference(class_ast);
11630
44
      zend_ast *method_ast = (*ast_ptr)->child[1];
11631
44
      if (method_ast->kind != ZEND_AST_ZVAL) {
11632
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic method name in constant expression");
11633
1
      }
11634
43
      if (Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING) {
11635
0
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal method name");
11636
0
      }
11637
43
      break;
11638
43
    }
11639
43
    EMPTY_SWITCH_DEFAULT_CASE();
11640
410
  }
11641
410
}
11642
11643
static void zend_compile_const_expr_args(zend_ast **ast_ptr)
11644
1.13k
{
11645
1.13k
  zend_ast_list *list = zend_ast_get_list(*ast_ptr);
11646
1.13k
  bool uses_named_args = false;
11647
1.18k
  for (uint32_t i = 0; i < list->children; i++) {
11648
45
    const zend_ast *arg = list->child[i];
11649
45
    if (arg->kind == ZEND_AST_UNPACK) {
11650
1
      zend_error_noreturn(E_COMPILE_ERROR,
11651
1
        "Argument unpacking in constant expressions is not supported");
11652
1
    }
11653
44
    if (arg->kind == ZEND_AST_NAMED_ARG) {
11654
11
      uses_named_args = true;
11655
33
    } else if (uses_named_args) {
11656
1
      zend_error_noreturn(E_COMPILE_ERROR,
11657
1
        "Cannot use positional argument after named argument");
11658
1
    }
11659
44
  }
11660
1.13k
  if (uses_named_args) {
11661
9
    list->attr = 1;
11662
9
  }
11663
1.13k
}
11664
11665
typedef struct {
11666
  /* Whether the value of this expression may differ on each evaluation. */
11667
  bool allow_dynamic;
11668
} const_expr_context;
11669
11670
static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */
11671
181k
{
11672
181k
  const const_expr_context *ctx = context;
11673
181k
  zend_ast *ast = *ast_ptr;
11674
181k
  if (ast == NULL || ast->kind == ZEND_AST_ZVAL) {
11675
139k
    return;
11676
139k
  }
11677
11678
42.2k
  if (!zend_is_allowed_in_const_expr(ast->kind)) {
11679
18
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11680
18
  }
11681
11682
42.2k
  switch (ast->kind) {
11683
1.85k
    case ZEND_AST_CLASS_CONST:
11684
1.85k
      zend_compile_const_expr_class_const(ast_ptr);
11685
1.85k
      break;
11686
11
    case ZEND_AST_CLASS_NAME:
11687
11
      zend_compile_const_expr_class_name(ast_ptr);
11688
11
      break;
11689
21.3k
    case ZEND_AST_CONST:
11690
21.3k
      zend_compile_const_expr_const(ast_ptr);
11691
21.3k
      break;
11692
613
    case ZEND_AST_MAGIC_CONST:
11693
613
      zend_compile_const_expr_magic_const(ast_ptr);
11694
613
      break;
11695
324
    case ZEND_AST_CAST:
11696
324
      if (ast->attr == IS_OBJECT && !ctx->allow_dynamic) {
11697
3
        zend_error_noreturn(E_COMPILE_ERROR,
11698
3
          "Object casts are not supported in this context");
11699
3
      }
11700
321
      break;
11701
1.14k
    case ZEND_AST_NEW:
11702
1.14k
      if (!ctx->allow_dynamic) {
11703
1
        zend_error_noreturn(E_COMPILE_ERROR,
11704
1
          "New expressions are not supported in this context");
11705
1
      }
11706
1.14k
      zend_compile_const_expr_new(ast_ptr);
11707
1.14k
      break;
11708
1.13k
    case ZEND_AST_ARG_LIST:
11709
1.13k
      zend_compile_const_expr_args(ast_ptr);
11710
1.13k
      break;
11711
30
    case ZEND_AST_CLOSURE:
11712
30
      zend_compile_const_expr_closure(ast_ptr);
11713
      /* Return, because we do not want to traverse the children. */
11714
30
      return;
11715
374
    case ZEND_AST_CALL:
11716
419
    case ZEND_AST_STATIC_CALL:
11717
419
      zend_compile_const_expr_fcc(ast_ptr);
11718
419
      break;
11719
42.2k
  }
11720
11721
42.1k
  zend_ast_apply(ast, zend_compile_const_expr, context);
11722
42.1k
}
11723
/* }}} */
11724
11725
void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynamic) /* {{{ */
11726
124k
{
11727
124k
  const_expr_context context;
11728
124k
  context.allow_dynamic = allow_dynamic;
11729
11730
124k
  zend_eval_const_expr(ast_ptr);
11731
124k
  zend_compile_const_expr(ast_ptr, &context);
11732
124k
  if ((*ast_ptr)->kind != ZEND_AST_ZVAL) {
11733
    /* Replace with compiled AST zval representation. */
11734
18.6k
    zval ast_zv;
11735
18.6k
    ZVAL_AST(&ast_zv, zend_ast_copy(*ast_ptr));
11736
18.6k
    zend_ast_destroy(*ast_ptr);
11737
18.6k
    *ast_ptr = zend_ast_create_zval(&ast_zv);
11738
18.6k
  }
11739
124k
  ZVAL_COPY(result, zend_ast_get_zval(*ast_ptr));
11740
124k
}
11741
/* }}} */
11742
11743
/* Same as compile_stmt, but with early binding */
11744
void zend_compile_top_stmt(zend_ast *ast) /* {{{ */
11745
372k
{
11746
372k
  if (!ast) {
11747
54.0k
    return;
11748
54.0k
  }
11749
11750
318k
  if (ast->kind == ZEND_AST_STMT_LIST) {
11751
52.7k
    const zend_ast_list *list = zend_ast_get_list(ast);
11752
52.7k
    uint32_t i;
11753
381k
    for (i = 0; i < list->children; ++i) {
11754
328k
      zend_compile_top_stmt(list->child[i]);
11755
328k
    }
11756
52.7k
    return;
11757
52.7k
  }
11758
11759
265k
  if (ast->kind == ZEND_AST_FUNC_DECL) {
11760
2.92k
    CG(zend_lineno) = ast->lineno;
11761
2.92k
    zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_TOPLEVEL);
11762
2.92k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11763
262k
  } else if (ast->kind == ZEND_AST_CLASS) {
11764
20.5k
    CG(zend_lineno) = ast->lineno;
11765
20.5k
    zend_compile_class_decl(NULL, ast, true);
11766
20.5k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11767
242k
  } else {
11768
242k
    zend_compile_stmt(ast);
11769
242k
  }
11770
265k
  if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) {
11771
258k
    zend_verify_namespace();
11772
258k
  }
11773
265k
}
11774
/* }}} */
11775
11776
static void zend_compile_stmt(zend_ast *ast) /* {{{ */
11777
7.94M
{
11778
7.94M
  if (!ast) {
11779
547k
    return;
11780
547k
  }
11781
11782
7.40M
  CG(zend_lineno) = ast->lineno;
11783
11784
7.40M
  if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) {
11785
0
    zend_do_extended_stmt(NULL);
11786
0
  }
11787
11788
7.40M
  switch (ast->kind) {
11789
1.69M
    case ZEND_AST_STMT_LIST:
11790
1.69M
      zend_compile_stmt_list(ast);
11791
1.69M
      break;
11792
1.60k
    case ZEND_AST_GLOBAL:
11793
1.60k
      zend_compile_global_var(ast);
11794
1.60k
      break;
11795
1.12k
    case ZEND_AST_STATIC:
11796
1.12k
      zend_compile_static_var(ast);
11797
1.12k
      break;
11798
5.04k
    case ZEND_AST_UNSET:
11799
5.04k
      zend_compile_unset(ast);
11800
5.04k
      break;
11801
32.1k
    case ZEND_AST_RETURN:
11802
32.1k
      zend_compile_return(ast);
11803
32.1k
      break;
11804
1.93M
    case ZEND_AST_ECHO:
11805
1.93M
      zend_compile_echo(ast);
11806
1.93M
      break;
11807
369
    case ZEND_AST_BREAK:
11808
1.16k
    case ZEND_AST_CONTINUE:
11809
1.16k
      zend_compile_break_continue(ast);
11810
1.16k
      break;
11811
1.26k
    case ZEND_AST_GOTO:
11812
1.26k
      zend_compile_goto(ast);
11813
1.26k
      break;
11814
4.91k
    case ZEND_AST_LABEL:
11815
4.91k
      zend_compile_label(ast);
11816
4.91k
      break;
11817
8.44k
    case ZEND_AST_WHILE:
11818
8.44k
      zend_compile_while(ast);
11819
8.44k
      break;
11820
569
    case ZEND_AST_DO_WHILE:
11821
569
      zend_compile_do_while(ast);
11822
569
      break;
11823
6.14k
    case ZEND_AST_FOR:
11824
6.14k
      zend_compile_for(ast);
11825
6.14k
      break;
11826
13.1k
    case ZEND_AST_FOREACH:
11827
13.1k
      zend_compile_foreach(ast);
11828
13.1k
      break;
11829
17.8k
    case ZEND_AST_IF:
11830
17.8k
      zend_compile_if(ast);
11831
17.8k
      break;
11832
25.9k
    case ZEND_AST_SWITCH:
11833
25.9k
      zend_compile_switch(ast);
11834
25.9k
      break;
11835
20.1k
    case ZEND_AST_TRY:
11836
20.1k
      zend_compile_try(ast);
11837
20.1k
      break;
11838
5.52k
    case ZEND_AST_DECLARE:
11839
5.52k
      zend_compile_declare(ast);
11840
5.52k
      break;
11841
8.34k
    case ZEND_AST_FUNC_DECL:
11842
34.1k
    case ZEND_AST_METHOD:
11843
34.1k
      zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_NESTED);
11844
34.1k
      break;
11845
899
    case ZEND_AST_ENUM_CASE:
11846
899
      zend_compile_enum_case(ast);
11847
899
      break;
11848
30.8k
    case ZEND_AST_PROP_GROUP:
11849
30.8k
      zend_compile_prop_group(ast);
11850
30.8k
      break;
11851
12.4k
    case ZEND_AST_CLASS_CONST_GROUP:
11852
12.4k
      zend_compile_class_const_group(ast);
11853
12.4k
      break;
11854
45.1k
    case ZEND_AST_USE_TRAIT:
11855
45.1k
      zend_compile_use_trait(ast);
11856
45.1k
      break;
11857
103k
    case ZEND_AST_CLASS:
11858
103k
      zend_compile_class_decl(NULL, ast, false);
11859
103k
      break;
11860
220
    case ZEND_AST_GROUP_USE:
11861
220
      zend_compile_group_use(ast);
11862
220
      break;
11863
1.05k
    case ZEND_AST_USE:
11864
1.05k
      zend_compile_use(ast);
11865
1.05k
      break;
11866
2.61k
    case ZEND_AST_CONST_DECL:
11867
2.61k
      zend_compile_const_decl(ast);
11868
2.61k
      break;
11869
4.26k
    case ZEND_AST_NAMESPACE:
11870
4.26k
      zend_compile_namespace(ast);
11871
4.26k
      break;
11872
21
    case ZEND_AST_HALT_COMPILER:
11873
21
      zend_compile_halt_compiler(ast);
11874
21
      break;
11875
618
    case ZEND_AST_THROW:
11876
618
      zend_compile_expr(NULL, ast);
11877
618
      break;
11878
790
    case ZEND_AST_CAST_VOID:
11879
790
      zend_compile_void_cast(NULL, ast);
11880
790
      break;
11881
3.38M
    default:
11882
3.38M
    {
11883
3.38M
      znode result;
11884
3.38M
      zend_compile_expr(&result, ast);
11885
3.38M
      zend_do_free(&result);
11886
3.38M
    }
11887
7.40M
  }
11888
11889
7.39M
  if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) {
11890
150k
    zend_emit_tick();
11891
150k
  }
11892
7.39M
}
11893
/* }}} */
11894
11895
static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */
11896
39.8M
{
11897
  /* CG(zend_lineno) = ast->lineno; */
11898
39.8M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11899
11900
39.8M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
11901
166k
    zend_compile_memoized_expr(result, ast);
11902
166k
    return;
11903
166k
  }
11904
11905
39.7M
  switch (ast->kind) {
11906
3.19M
    case ZEND_AST_ZVAL:
11907
3.19M
      ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast));
11908
3.19M
      result->op_type = IS_CONST;
11909
3.19M
      return;
11910
409k
    case ZEND_AST_ZNODE:
11911
409k
      *result = *zend_ast_get_znode(ast);
11912
409k
      return;
11913
320k
    case ZEND_AST_VAR:
11914
351k
    case ZEND_AST_DIM:
11915
359k
    case ZEND_AST_PROP:
11916
396k
    case ZEND_AST_NULLSAFE_PROP:
11917
400k
    case ZEND_AST_STATIC_PROP:
11918
3.99M
    case ZEND_AST_CALL:
11919
4.01M
    case ZEND_AST_METHOD_CALL:
11920
4.01M
    case ZEND_AST_NULLSAFE_METHOD_CALL:
11921
4.07M
    case ZEND_AST_STATIC_CALL:
11922
4.07M
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
11923
4.07M
      zend_compile_var(result, ast, BP_VAR_R, false);
11924
4.07M
      return;
11925
124k
    case ZEND_AST_ASSIGN:
11926
124k
      zend_compile_assign(result, ast);
11927
124k
      return;
11928
4.28k
    case ZEND_AST_ASSIGN_REF:
11929
4.28k
      zend_compile_assign_ref(result, ast);
11930
4.28k
      return;
11931
58.1k
    case ZEND_AST_NEW:
11932
58.1k
      zend_compile_new(result, ast);
11933
58.1k
      return;
11934
67.8k
    case ZEND_AST_ASSIGN_OP:
11935
67.8k
      zend_compile_compound_assign(result, ast);
11936
67.8k
      return;
11937
2.23M
    case ZEND_AST_BINARY_OP:
11938
2.23M
      zend_compile_binary_op(result, ast);
11939
2.23M
      return;
11940
356k
    case ZEND_AST_GREATER:
11941
427k
    case ZEND_AST_GREATER_EQUAL:
11942
427k
      zend_compile_greater(result, ast);
11943
427k
      return;
11944
1.39M
    case ZEND_AST_UNARY_OP:
11945
1.39M
      zend_compile_unary_op(result, ast);
11946
1.39M
      return;
11947
16.0k
    case ZEND_AST_UNARY_PLUS:
11948
50.3k
    case ZEND_AST_UNARY_MINUS:
11949
50.3k
      zend_compile_unary_pm(result, ast);
11950
50.3k
      return;
11951
9.62k
    case ZEND_AST_AND:
11952
17.5k
    case ZEND_AST_OR:
11953
17.5k
      zend_compile_short_circuiting(result, ast);
11954
17.5k
      return;
11955
1.69k
    case ZEND_AST_POST_INC:
11956
4.65k
    case ZEND_AST_POST_DEC:
11957
4.65k
      zend_compile_post_incdec(result, ast);
11958
4.65k
      return;
11959
977
    case ZEND_AST_PRE_INC:
11960
1.90k
    case ZEND_AST_PRE_DEC:
11961
1.90k
      zend_compile_pre_incdec(result, ast);
11962
1.90k
      return;
11963
2.15k
    case ZEND_AST_CAST:
11964
2.15k
      zend_compile_cast(result, ast);
11965
2.15k
      return;
11966
7.06k
    case ZEND_AST_CONDITIONAL:
11967
7.06k
      zend_compile_conditional(result, ast);
11968
7.06k
      return;
11969
1.87M
    case ZEND_AST_COALESCE:
11970
1.87M
      zend_compile_coalesce(result, ast);
11971
1.87M
      return;
11972
16.0k
    case ZEND_AST_ASSIGN_COALESCE:
11973
16.0k
      zend_compile_assign_coalesce(result, ast);
11974
16.0k
      return;
11975
2.05k
    case ZEND_AST_PRINT:
11976
2.05k
      zend_compile_print(result, ast);
11977
2.05k
      return;
11978
13.4k
    case ZEND_AST_YIELD:
11979
13.4k
      zend_compile_yield(result, ast);
11980
13.4k
      return;
11981
1.78k
    case ZEND_AST_YIELD_FROM:
11982
1.78k
      zend_compile_yield_from(result, ast);
11983
1.78k
      return;
11984
666
    case ZEND_AST_INSTANCEOF:
11985
666
      zend_compile_instanceof(result, ast);
11986
666
      return;
11987
4.63k
    case ZEND_AST_INCLUDE_OR_EVAL:
11988
4.63k
      zend_compile_include_or_eval(result, ast);
11989
4.63k
      return;
11990
9.63k
    case ZEND_AST_ISSET:
11991
10.3k
    case ZEND_AST_EMPTY:
11992
10.3k
      zend_compile_isset_or_empty(result, ast);
11993
10.3k
      return;
11994
14.8M
    case ZEND_AST_SILENCE:
11995
14.8M
      zend_compile_silence(result, ast);
11996
14.8M
      return;
11997
29.2k
    case ZEND_AST_SHELL_EXEC:
11998
29.2k
      zend_compile_shell_exec(result, ast);
11999
29.2k
      return;
12000
106k
    case ZEND_AST_ARRAY:
12001
106k
      zend_compile_array(result, ast);
12002
106k
      return;
12003
8.89M
    case ZEND_AST_CONST:
12004
8.89M
      zend_compile_const(result, ast);
12005
8.89M
      return;
12006
190k
    case ZEND_AST_CLASS_CONST:
12007
190k
      zend_compile_class_const(result, ast);
12008
190k
      return;
12009
2.53k
    case ZEND_AST_CLASS_NAME:
12010
2.53k
      zend_compile_class_name(result, ast);
12011
2.53k
      return;
12012
22.1k
    case ZEND_AST_ENCAPS_LIST:
12013
22.1k
      zend_compile_encaps_list(result, ast);
12014
22.1k
      return;
12015
13.8k
    case ZEND_AST_MAGIC_CONST:
12016
13.8k
      zend_compile_magic_const(result, ast);
12017
13.8k
      return;
12018
1.35M
    case ZEND_AST_CLOSURE:
12019
1.37M
    case ZEND_AST_ARROW_FUNC:
12020
1.37M
      zend_compile_func_decl(result, ast, FUNC_DECL_LEVEL_NESTED);
12021
1.37M
      return;
12022
1.28k
    case ZEND_AST_THROW:
12023
1.28k
      zend_compile_throw(result, ast);
12024
1.28k
      return;
12025
2.82k
    case ZEND_AST_MATCH:
12026
2.82k
      zend_compile_match(result, ast);
12027
2.82k
      return;
12028
202k
    case ZEND_AST_PIPE:
12029
202k
      zend_compile_pipe(result, ast);
12030
202k
      return;
12031
0
    default:
12032
0
      ZEND_ASSERT(0 /* not supported */);
12033
39.7M
  }
12034
39.7M
}
12035
/* }}} */
12036
12037
static void zend_compile_expr(znode *result, zend_ast *ast)
12038
39.8M
{
12039
39.8M
  zend_check_stack_limit();
12040
12041
39.8M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12042
39.8M
  zend_compile_expr_inner(result, ast);
12043
39.8M
  zend_short_circuiting_commit(checkpoint, result, ast);
12044
39.8M
}
12045
12046
static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
12047
6.22M
{
12048
6.22M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
12049
12050
6.22M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
12051
35.9k
    switch (ast->kind) {
12052
1.56k
      case ZEND_AST_CALL:
12053
2.30k
      case ZEND_AST_METHOD_CALL:
12054
2.30k
      case ZEND_AST_NULLSAFE_METHOD_CALL:
12055
3.81k
      case ZEND_AST_STATIC_CALL:
12056
3.81k
        zend_compile_memoized_expr(result, ast);
12057
        /* This might not actually produce an opcode, e.g. for expressions evaluated at comptime. */
12058
3.81k
        return NULL;
12059
35.9k
    }
12060
35.9k
  }
12061
12062
6.22M
  switch (ast->kind) {
12063
361k
    case ZEND_AST_VAR:
12064
361k
      return zend_compile_simple_var(result, ast, type, false);
12065
68.7k
    case ZEND_AST_DIM:
12066
68.7k
      return zend_compile_dim(result, ast, type, by_ref);
12067
12.0k
    case ZEND_AST_PROP:
12068
49.1k
    case ZEND_AST_NULLSAFE_PROP:
12069
49.1k
      return zend_compile_prop(result, ast, type, by_ref);
12070
7.20k
    case ZEND_AST_STATIC_PROP:
12071
7.20k
      return zend_compile_static_prop(result, ast, type, by_ref, false);
12072
3.73M
    case ZEND_AST_CALL:
12073
3.73M
      zend_compile_call(result, ast, type);
12074
3.73M
      return NULL;
12075
0
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
12076
0
      zend_compile_parent_property_hook_call(result, ast, type);
12077
0
      return NULL;
12078
27.4k
    case ZEND_AST_METHOD_CALL:
12079
30.2k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
12080
30.2k
      zend_compile_method_call(result, ast, type);
12081
30.2k
      return NULL;
12082
63.2k
    case ZEND_AST_STATIC_CALL:
12083
63.2k
      zend_compile_static_call(result, ast, type);
12084
63.2k
      return NULL;
12085
1.97k
    case ZEND_AST_ZNODE:
12086
1.97k
      *result = *zend_ast_get_znode(ast);
12087
1.97k
      return NULL;
12088
1.90M
    default:
12089
1.90M
      if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
12090
115
        zend_error_noreturn(E_COMPILE_ERROR,
12091
115
          "Cannot use temporary expression in write context");
12092
115
      }
12093
12094
1.90M
      zend_compile_expr(result, ast);
12095
1.90M
      return NULL;
12096
6.22M
  }
12097
6.22M
}
12098
12099
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12100
6.22M
{
12101
6.22M
  zend_check_stack_limit();
12102
12103
6.22M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12104
6.22M
  zend_op *opcode = zend_compile_var_inner(result, ast, type, by_ref);
12105
6.22M
  zend_short_circuiting_commit(checkpoint, result, ast);
12106
6.22M
  return opcode;
12107
6.22M
}
12108
12109
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12110
470k
{
12111
470k
  zend_check_stack_limit();
12112
12113
470k
  switch (ast->kind) {
12114
254k
    case ZEND_AST_VAR:
12115
254k
      return zend_compile_simple_var(result, ast, type, true);
12116
141k
    case ZEND_AST_DIM:
12117
141k
      return zend_delayed_compile_dim(result, ast, type, by_ref);
12118
14.8k
    case ZEND_AST_PROP:
12119
18.1k
    case ZEND_AST_NULLSAFE_PROP:
12120
18.1k
    {
12121
18.1k
      zend_op *opline = zend_delayed_compile_prop(result, ast, type);
12122
18.1k
      if (by_ref) {
12123
346
        opline->extended_value |= ZEND_FETCH_REF;
12124
346
      }
12125
18.1k
      return opline;
12126
14.8k
    }
12127
5.98k
    case ZEND_AST_STATIC_PROP:
12128
5.98k
      return zend_compile_static_prop(result, ast, type, by_ref, true);
12129
51.2k
    default:
12130
51.2k
      return zend_compile_var(result, ast, type, false);
12131
470k
  }
12132
470k
}
12133
/* }}} */
12134
12135
bool zend_try_ct_eval_cast(zval *result, uint32_t type, zval *op1)
12136
8.28k
{
12137
  /* NAN warns when casting */
12138
8.28k
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))) {
12139
0
    return false;
12140
0
  }
12141
8.28k
  switch (type) {
12142
86
    case _IS_BOOL:
12143
86
      ZVAL_BOOL(result, zend_is_true(op1));
12144
86
      return true;
12145
150
    case IS_LONG:
12146
150
      if (Z_TYPE_P(op1) == IS_DOUBLE && !ZEND_DOUBLE_FITS_LONG(Z_DVAL_P((op1)))) {
12147
2
        return false;
12148
2
      }
12149
148
      ZVAL_LONG(result, zval_get_long(op1));
12150
148
      return true;
12151
906
    case IS_DOUBLE:
12152
906
      ZVAL_DOUBLE(result, zval_get_double(op1));
12153
906
      return true;
12154
6.73k
    case IS_STRING:
12155
      /* Conversion from double to string takes into account run-time
12156
         'precision' setting and cannot be evaluated at compile-time */
12157
6.73k
      if (Z_TYPE_P(op1) != IS_ARRAY && Z_TYPE_P(op1) != IS_DOUBLE) {
12158
6.06k
        ZVAL_STR(result, zval_get_string(op1));
12159
6.06k
        return true;
12160
6.06k
      }
12161
670
      break;
12162
670
    case IS_ARRAY:
12163
257
      ZVAL_COPY(result, op1);
12164
257
      convert_to_array(result);
12165
257
      return true;
12166
8.28k
  }
12167
824
  return false;
12168
8.28k
}
12169
12170
static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
12171
13.8M
{
12172
13.8M
  zend_ast *ast = *ast_ptr;
12173
13.8M
  zval result;
12174
12175
13.8M
  if (!ast) {
12176
3.06M
    return;
12177
3.06M
  }
12178
12179
10.7M
  zend_check_stack_limit();
12180
12181
10.7M
  switch (ast->kind) {
12182
193k
    case ZEND_AST_BINARY_OP:
12183
193k
      zend_eval_const_expr(&ast->child[0]);
12184
193k
      zend_eval_const_expr(&ast->child[1]);
12185
193k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12186
159k
        return;
12187
159k
      }
12188
12189
33.6k
      if (!zend_try_ct_eval_binary_op(&result, ast->attr,
12190
33.6k
          zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
12191
33.6k
      ) {
12192
19.7k
        return;
12193
19.7k
      }
12194
13.8k
      break;
12195
13.8k
    case ZEND_AST_GREATER:
12196
6.42k
    case ZEND_AST_GREATER_EQUAL:
12197
6.42k
      zend_eval_const_expr(&ast->child[0]);
12198
6.42k
      zend_eval_const_expr(&ast->child[1]);
12199
6.42k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12200
4.97k
        return;
12201
4.97k
      }
12202
12203
1.45k
      zend_ct_eval_greater(&result, ast->kind,
12204
1.45k
        zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]));
12205
1.45k
      break;
12206
1.00k
    case ZEND_AST_AND:
12207
2.61k
    case ZEND_AST_OR:
12208
2.61k
    {
12209
2.61k
      bool child0_is_true, child1_is_true;
12210
2.61k
      zend_eval_const_expr(&ast->child[0]);
12211
2.61k
      zend_eval_const_expr(&ast->child[1]);
12212
2.61k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12213
783
        return;
12214
783
      }
12215
12216
1.83k
      child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0]));
12217
1.83k
      if (child0_is_true == (ast->kind == ZEND_AST_OR)) {
12218
103
        ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR);
12219
103
        break;
12220
103
      }
12221
12222
1.72k
      if (ast->child[1]->kind != ZEND_AST_ZVAL) {
12223
217
        return;
12224
217
      }
12225
12226
1.51k
      child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1]));
12227
1.51k
      if (ast->kind == ZEND_AST_OR) {
12228
800
        ZVAL_BOOL(&result, child0_is_true || child1_is_true);
12229
800
      } else {
12230
710
        ZVAL_BOOL(&result, child0_is_true && child1_is_true);
12231
710
      }
12232
1.51k
      break;
12233
1.72k
    }
12234
6.21k
    case ZEND_AST_UNARY_OP:
12235
6.21k
      zend_eval_const_expr(&ast->child[0]);
12236
6.21k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12237
2.41k
        return;
12238
2.41k
      }
12239
12240
3.80k
      if (!zend_try_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12241
558
        return;
12242
558
      }
12243
3.24k
      break;
12244
4.25k
    case ZEND_AST_UNARY_PLUS:
12245
21.1k
    case ZEND_AST_UNARY_MINUS:
12246
21.1k
      zend_eval_const_expr(&ast->child[0]);
12247
21.1k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12248
7.58k
        return;
12249
7.58k
      }
12250
12251
13.5k
      if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) {
12252
5.81k
        return;
12253
5.81k
      }
12254
7.73k
      break;
12255
22.0k
    case ZEND_AST_COALESCE:
12256
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12257
22.0k
      if (ast->child[0]->kind == ZEND_AST_DIM) {
12258
265
        ast->child[0]->attr |= ZEND_DIM_IS;
12259
265
      }
12260
22.0k
      zend_eval_const_expr(&ast->child[0]);
12261
12262
22.0k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12263
        /* ensure everything was compile-time evaluated at least once */
12264
21.9k
        zend_eval_const_expr(&ast->child[1]);
12265
21.9k
        return;
12266
21.9k
      }
12267
12268
62
      if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) {
12269
23
        zend_eval_const_expr(&ast->child[1]);
12270
23
        *ast_ptr = ast->child[1];
12271
23
        ast->child[1] = NULL;
12272
23
        zend_ast_destroy(ast);
12273
39
      } else {
12274
39
        *ast_ptr = ast->child[0];
12275
39
        ast->child[0] = NULL;
12276
39
        zend_ast_destroy(ast);
12277
39
      }
12278
62
      return;
12279
2.43k
    case ZEND_AST_CONDITIONAL:
12280
2.43k
    {
12281
2.43k
      zend_ast **child, *child_ast;
12282
2.43k
      zend_eval_const_expr(&ast->child[0]);
12283
2.43k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12284
        /* ensure everything was compile-time evaluated at least once */
12285
2.29k
        if (ast->child[1]) {
12286
1.58k
          zend_eval_const_expr(&ast->child[1]);
12287
1.58k
        }
12288
2.29k
        zend_eval_const_expr(&ast->child[2]);
12289
2.29k
        return;
12290
2.29k
      }
12291
12292
139
      child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];
12293
139
      if (*child == NULL) {
12294
80
        child--;
12295
80
      }
12296
139
      child_ast = *child;
12297
139
      *child = NULL;
12298
139
      zend_ast_destroy(ast);
12299
139
      *ast_ptr = child_ast;
12300
139
      zend_eval_const_expr(ast_ptr);
12301
139
      return;
12302
2.43k
    }
12303
1.32M
    case ZEND_AST_DIM:
12304
1.32M
    {
12305
      /* constant expression should be always read context ... */
12306
1.32M
      const zval *container, *dim;
12307
12308
1.32M
      if (ast->child[1] == NULL) {
12309
30
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
12310
30
      }
12311
12312
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12313
1.32M
      if ((ast->attr & ZEND_DIM_IS) && ast->child[0]->kind == ZEND_AST_DIM) {
12314
533
        ast->child[0]->attr |= ZEND_DIM_IS;
12315
533
      }
12316
12317
1.32M
      zend_eval_const_expr(&ast->child[0]);
12318
1.32M
      zend_eval_const_expr(&ast->child[1]);
12319
1.32M
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12320
1.30M
        return;
12321
1.30M
      }
12322
12323
14.8k
      container = zend_ast_get_zval(ast->child[0]);
12324
14.8k
      dim = zend_ast_get_zval(ast->child[1]);
12325
12326
14.8k
      if (Z_TYPE_P(container) == IS_ARRAY) {
12327
10.8k
        zval *el;
12328
10.8k
        if (Z_TYPE_P(dim) == IS_LONG) {
12329
1.13k
          el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim));
12330
1.13k
          if (el) {
12331
746
            ZVAL_COPY(&result, el);
12332
746
          } else {
12333
389
            return;
12334
389
          }
12335
9.69k
        } else if (Z_TYPE_P(dim) == IS_STRING) {
12336
8.85k
          el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim));
12337
8.85k
          if (el) {
12338
313
            ZVAL_COPY(&result, el);
12339
8.53k
          } else {
12340
8.53k
            return;
12341
8.53k
          }
12342
8.85k
        } else {
12343
844
          return; /* warning... handle at runtime */
12344
844
        }
12345
10.8k
      } else if (Z_TYPE_P(container) == IS_STRING) {
12346
3.69k
        zend_long offset;
12347
3.69k
        uint8_t c;
12348
3.69k
        if (Z_TYPE_P(dim) == IS_LONG) {
12349
2.11k
          offset = Z_LVAL_P(dim);
12350
2.11k
        } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) {
12351
1.29k
          return;
12352
1.29k
        }
12353
2.40k
        if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
12354
2.22k
          return;
12355
2.22k
        }
12356
180
        c = (uint8_t) Z_STRVAL_P(container)[offset];
12357
180
        ZVAL_CHAR(&result, c);
12358
312
      } else if (Z_TYPE_P(container) <= IS_FALSE) {
12359
2
        return; /* warning... handle at runtime */
12360
310
      } else {
12361
310
        return;
12362
310
      }
12363
1.23k
      break;
12364
14.8k
    }
12365
3.38M
    case ZEND_AST_ARRAY:
12366
3.38M
      if (!zend_try_ct_eval_array(&result, ast)) {
12367
3.34M
        return;
12368
3.34M
      }
12369
38.2k
      break;
12370
38.2k
    case ZEND_AST_MAGIC_CONST:
12371
1.75k
      if (!zend_try_ct_eval_magic_const(&result, ast)) {
12372
613
        return;
12373
613
      }
12374
1.14k
      break;
12375
121k
    case ZEND_AST_CONST:
12376
121k
    {
12377
121k
      zend_ast *name_ast = ast->child[0];
12378
121k
      bool is_fully_qualified;
12379
121k
      zend_string *resolved_name = zend_resolve_const_name(
12380
121k
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
12381
12382
121k
      if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
12383
119k
        zend_string_release_ex(resolved_name, 0);
12384
119k
        return;
12385
119k
      }
12386
12387
2.17k
      zend_string_release_ex(resolved_name, 0);
12388
2.17k
      break;
12389
121k
    }
12390
1.37M
    case ZEND_AST_CLASS_CONST:
12391
1.37M
    {
12392
1.37M
      zend_ast *class_ast;
12393
1.37M
      zend_ast *name_ast;
12394
1.37M
      zend_string *resolved_name;
12395
12396
1.37M
      zend_eval_const_expr(&ast->child[0]);
12397
1.37M
      zend_eval_const_expr(&ast->child[1]);
12398
12399
1.37M
      if (UNEXPECTED(ast->child[1]->kind != ZEND_AST_ZVAL
12400
1.37M
        || Z_TYPE_P(zend_ast_get_zval(ast->child[1])) != IS_STRING)) {
12401
1.85k
        return;
12402
1.85k
      }
12403
12404
1.37M
      class_ast = ast->child[0];
12405
1.37M
      name_ast = ast->child[1];
12406
12407
1.37M
      if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) {
12408
1.26M
        return;
12409
1.26M
      }
12410
12411
111k
      resolved_name = zend_resolve_class_name_ast(class_ast);
12412
111k
      if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) {
12413
111k
        zend_string_release_ex(resolved_name, 0);
12414
111k
        return;
12415
111k
      }
12416
12417
35
      zend_string_release_ex(resolved_name, 0);
12418
35
      break;
12419
111k
    }
12420
3.22k
    case ZEND_AST_CLASS_NAME:
12421
3.22k
    {
12422
3.22k
      zend_ast *class_ast = ast->child[0];
12423
3.22k
      if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) {
12424
1.80k
        return;
12425
1.80k
      }
12426
1.41k
      break;
12427
3.22k
    }
12428
    // TODO: We should probably use zend_ast_apply to recursively walk nodes without
12429
    // special handling. It is required that all nodes that are part of a const expr
12430
    // are visited. Probably we should be distinguishing evaluation of const expr and
12431
    // normal exprs here.
12432
1.86k
    case ZEND_AST_ARG_LIST:
12433
1.86k
    {
12434
1.86k
      zend_ast_list *list = zend_ast_get_list(ast);
12435
2.08k
      for (uint32_t i = 0; i < list->children; i++) {
12436
219
        zend_eval_const_expr(&list->child[i]);
12437
219
      }
12438
1.86k
      return;
12439
3.22k
    }
12440
1.86k
    case ZEND_AST_NEW:
12441
1.86k
      zend_eval_const_expr(&ast->child[0]);
12442
1.86k
      zend_eval_const_expr(&ast->child[1]);
12443
1.86k
      return;
12444
26
    case ZEND_AST_NAMED_ARG:
12445
26
      zend_eval_const_expr(&ast->child[1]);
12446
26
      return;
12447
891
    case ZEND_AST_CONST_ENUM_INIT:
12448
891
      zend_eval_const_expr(&ast->child[2]);
12449
891
      return;
12450
686
    case ZEND_AST_PROP:
12451
1.04k
    case ZEND_AST_NULLSAFE_PROP:
12452
1.04k
      zend_eval_const_expr(&ast->child[0]);
12453
1.04k
      zend_eval_const_expr(&ast->child[1]);
12454
1.04k
      return;
12455
27.4k
    case ZEND_AST_CAST:
12456
27.4k
      zend_eval_const_expr(&ast->child[0]);
12457
27.4k
      if (ast->child[0]->kind == ZEND_AST_ZVAL
12458
2.25k
       && zend_try_ct_eval_cast(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12459
1.47k
        break;
12460
1.47k
      }
12461
25.9k
      return;
12462
4.24M
    default:
12463
4.24M
      return;
12464
10.7M
  }
12465
12466
73.2k
  zend_ast_destroy(ast);
12467
73.2k
  *ast_ptr = zend_ast_create_zval(&result);
12468
73.2k
}
12469
/* }}} */