Coverage Report

Created: 2026-02-09 07:07

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
37.5M
#define SET_NODE(target, src) do { \
44
37.5M
    target ## _type = (src)->op_type; \
45
37.5M
    if ((src)->op_type == IS_CONST) { \
46
5.31M
      target.constant = zend_add_literal(&(src)->u.constant); \
47
32.2M
    } else { \
48
32.2M
      target = (src)->u.op; \
49
32.2M
    } \
50
37.5M
  } while (0)
51
52
27.2M
#define GET_NODE(target, src) do { \
53
27.2M
    (target)->op_type = src ## _type; \
54
27.2M
    if ((target)->op_type == IS_CONST) { \
55
552
      ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
56
27.2M
    } else { \
57
27.2M
      (target)->u.op = src; \
58
27.2M
    } \
59
27.2M
  } while (0)
60
61
55.9M
#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
10.0M
static inline uint32_t zend_alloc_cache_slots(unsigned count) {
71
10.0M
  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
10.0M
  zend_op_array *op_array = CG(active_op_array);
79
10.0M
  uint32_t ret = op_array->cache_size;
80
10.0M
  op_array->cache_size += count * sizeof(void*);
81
10.0M
  return ret;
82
10.0M
}
83
84
9.73M
static inline uint32_t zend_alloc_cache_slot(void) {
85
9.73M
  return zend_alloc_cache_slots(1);
86
9.73M
}
87
88
ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
89
ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename, zend_compile_position position);
90
91
#ifndef ZTS
92
ZEND_API zend_compiler_globals compiler_globals;
93
ZEND_API zend_executor_globals executor_globals;
94
#endif
95
96
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2);
97
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast);
98
static void zend_eval_const_expr(zend_ast **ast_ptr);
99
100
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
101
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
102
static void zend_compile_expr(znode *result, zend_ast *ast);
103
static void zend_compile_stmt(zend_ast *ast);
104
static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type);
105
106
#ifdef ZEND_CHECK_STACK_LIMIT
107
zend_never_inline static void zend_stack_limit_error(void)
108
0
{
109
0
  size_t max_stack_size = 0;
110
0
  if ((uintptr_t) EG(stack_base) > (uintptr_t) EG(stack_limit)) {
111
0
    max_stack_size = (size_t) ((uintptr_t) EG(stack_base) - (uintptr_t) EG(stack_limit));
112
0
  }
113
114
0
  zend_error_noreturn(E_COMPILE_ERROR,
115
0
    "Maximum call stack size of %zu bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached during compilation. Try splitting expression",
116
0
    max_stack_size);
117
0
}
118
119
static void zend_check_stack_limit(void)
120
43.9M
{
121
43.9M
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
122
0
    zend_stack_limit_error();
123
0
  }
124
43.9M
}
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
54.9M
{
133
54.9M
  MAKE_NOP(op);
134
54.9M
  op->extended_value = 0;
135
54.9M
  op->lineno = CG(zend_lineno);
136
#ifdef ZEND_VERIFY_TYPE_INFERENCE
137
  op->op1_use_type = 0;
138
  op->op2_use_type = 0;
139
  op->result_use_type = 0;
140
  op->op1_def_type = 0;
141
  op->op2_def_type = 0;
142
  op->result_def_type = 0;
143
#endif
144
54.9M
}
145
146
static zend_always_inline uint32_t get_next_op_number(void)
147
9.64M
{
148
9.64M
  return CG(active_op_array)->last;
149
9.64M
}
150
151
static zend_op *get_next_op(void)
152
54.4M
{
153
54.4M
  zend_op_array *op_array = CG(active_op_array);
154
54.4M
  uint32_t next_op_num = op_array->last++;
155
54.4M
  zend_op *next_op;
156
157
54.4M
  if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) {
158
466k
    CG(context).opcodes_size *= 4;
159
466k
    op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op));
160
466k
  }
161
162
54.4M
  next_op = &(op_array->opcodes[next_op_num]);
163
164
54.4M
  init_op(next_op);
165
166
54.4M
  return next_op;
167
54.4M
}
168
169
static zend_brk_cont_element *get_next_brk_cont_element(void)
170
40.3k
{
171
40.3k
  CG(context).last_brk_cont++;
172
40.3k
  CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont);
173
40.3k
  return &CG(context).brk_cont_array[CG(context).last_brk_cont-1];
174
40.3k
}
175
176
static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */
177
164k
{
178
164k
  zend_string *filename = CG(active_op_array)->filename;
179
164k
  zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32,
180
164k
    '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
181
164k
  return zend_new_interned_string(result);
182
164k
}
183
/* }}} */
184
185
static bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
186
9.43M
{
187
9.43M
  const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
188
9.43M
  if (ns_separator != NULL) {
189
9.01M
    *result = ns_separator + 1;
190
9.01M
    *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
191
9.01M
    return 1;
192
9.01M
  }
193
194
419k
  return 0;
195
9.43M
}
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
388k
{
227
388k
  const struct reserved_class_name *reserved = reserved_class_names;
228
229
388k
  const char *uqname = ZSTR_VAL(name);
230
388k
  size_t uqname_len = ZSTR_LEN(name);
231
388k
  zend_get_unqualified_name(name, &uqname, &uqname_len);
232
233
6.98M
  for (; reserved->name; ++reserved) {
234
6.59M
    if (uqname_len == reserved->len
235
169k
      && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
236
6.59M
    ) {
237
93
      return 1;
238
93
    }
239
6.59M
  }
240
241
387k
  return 0;
242
388k
}
243
/* }}} */
244
245
void zend_assert_valid_class_name(const zend_string *name, const char *type) /* {{{ */
246
386k
{
247
386k
  if (zend_is_reserved_class_name(name)) {
248
67
    zend_error_noreturn(E_COMPILE_ERROR,
249
67
      "Cannot use \"%s\" as %s as it is reserved", ZSTR_VAL(name), type);
250
67
  }
251
386k
  if (zend_string_equals_literal(name, "_")) {
252
372
    zend_error(E_DEPRECATED, "Using \"_\" as %s is deprecated since 8.4", type);
253
372
  }
254
386k
}
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
277k
{
295
277k
  const builtin_type_info *info = &builtin_types[0];
296
297
3.27M
  for (; info->name; ++info) {
298
3.08M
    if (ZSTR_LEN(name) == info->name_len
299
144k
      && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
300
3.08M
    ) {
301
79.3k
      return info->type;
302
79.3k
    }
303
3.08M
  }
304
305
197k
  return 0;
306
277k
}
307
/* }}} */
308
309
static zend_always_inline bool zend_is_confusable_type(const zend_string *name, const char **correct_name) /* {{{ */
310
197k
{
311
197k
  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
925k
  for (; info->name; ++info) {
316
748k
    if (zend_string_equals_cstr(name, info->name, info->name_len)) {
317
20.4k
      *correct_name = info->correct_name;
318
20.4k
      return 1;
319
20.4k
    }
320
748k
  }
321
322
176k
  return 0;
323
197k
}
324
/* }}} */
325
326
20.4k
static bool zend_is_not_imported(zend_string *name) {
327
  /* Assuming "name" is unqualified here. */
328
20.4k
  return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL;
329
20.4k
}
330
331
void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_array *op_array) /* {{{ */
332
1.21M
{
333
1.21M
  *prev_context = CG(context);
334
1.21M
  CG(context).prev = CG(context).op_array ? prev_context : NULL;
335
1.21M
  CG(context).op_array = op_array;
336
1.21M
  CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
337
1.21M
  CG(context).vars_size = 0;
338
1.21M
  CG(context).literals_size = 0;
339
1.21M
  CG(context).fast_call_var = -1;
340
1.21M
  CG(context).try_catch_offset = -1;
341
1.21M
  CG(context).current_brk_cont = -1;
342
1.21M
  CG(context).last_brk_cont = 0;
343
1.21M
  CG(context).has_assigned_to_http_response_header = false;
344
1.21M
  CG(context).brk_cont_array = NULL;
345
1.21M
  CG(context).labels = NULL;
346
1.21M
  CG(context).in_jmp_frameless_branch = false;
347
1.21M
  CG(context).active_property_info_name = NULL;
348
1.21M
  CG(context).active_property_hook_kind = (zend_property_hook_kind)-1;
349
1.21M
}
350
/* }}} */
351
352
void zend_oparray_context_end(const zend_oparray_context *prev_context) /* {{{ */
353
1.20M
{
354
1.20M
  if (CG(context).brk_cont_array) {
355
29.4k
    efree(CG(context).brk_cont_array);
356
29.4k
    CG(context).brk_cont_array = NULL;
357
29.4k
  }
358
1.20M
  if (CG(context).labels) {
359
1.78k
    zend_hash_destroy(CG(context).labels);
360
1.78k
    FREE_HASHTABLE(CG(context).labels);
361
1.78k
    CG(context).labels = NULL;
362
1.78k
  }
363
1.20M
  CG(context) = *prev_context;
364
1.20M
}
365
/* }}} */
366
367
static void zend_reset_import_tables(void) /* {{{ */
368
77.9k
{
369
77.9k
  if (FC(imports)) {
370
728
    zend_hash_destroy(FC(imports));
371
728
    efree(FC(imports));
372
728
    FC(imports) = NULL;
373
728
  }
374
375
77.9k
  if (FC(imports_function)) {
376
425
    zend_hash_destroy(FC(imports_function));
377
425
    efree(FC(imports_function));
378
425
    FC(imports_function) = NULL;
379
425
  }
380
381
77.9k
  if (FC(imports_const)) {
382
292
    zend_hash_destroy(FC(imports_const));
383
292
    efree(FC(imports_const));
384
292
    FC(imports_const) = NULL;
385
292
  }
386
387
77.9k
  zend_hash_clean(&FC(seen_symbols));
388
77.9k
}
389
/* }}} */
390
391
73.7k
static void zend_end_namespace(void) /* {{{ */ {
392
73.7k
  FC(in_namespace) = 0;
393
73.7k
  zend_reset_import_tables();
394
73.7k
  if (FC(current_namespace)) {
395
2.60k
    zend_string_release_ex(FC(current_namespace), 0);
396
2.60k
    FC(current_namespace) = NULL;
397
2.60k
  }
398
73.7k
}
399
/* }}} */
400
401
void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
402
77.3k
{
403
77.3k
  *prev_context = CG(file_context);
404
77.3k
  FC(imports) = NULL;
405
77.3k
  FC(imports_function) = NULL;
406
77.3k
  FC(imports_const) = NULL;
407
77.3k
  FC(current_namespace) = NULL;
408
77.3k
  FC(in_namespace) = 0;
409
77.3k
  FC(has_bracketed_namespaces) = 0;
410
77.3k
  FC(declarables).ticks = 0;
411
77.3k
  zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
412
77.3k
}
413
/* }}} */
414
415
void zend_file_context_end(const zend_file_context *prev_context) /* {{{ */
416
72.2k
{
417
72.2k
  zend_end_namespace();
418
72.2k
  zend_hash_destroy(&FC(seen_symbols));
419
72.2k
  CG(file_context) = *prev_context;
420
72.2k
}
421
/* }}} */
422
423
void zend_init_compiler_data_structures(void) /* {{{ */
424
249k
{
425
249k
  zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
426
249k
  zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
427
249k
  zend_stack_init(&CG(short_circuiting_opnums), sizeof(uint32_t));
428
249k
  CG(active_class_entry) = NULL;
429
249k
  CG(in_compilation) = 0;
430
249k
  CG(skip_shebang) = 0;
431
432
249k
  CG(encoding_declared) = 0;
433
249k
  CG(memoized_exprs) = NULL;
434
249k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
435
249k
}
436
/* }}} */
437
438
1.22M
static void zend_register_seen_symbol(zend_string *name, uint32_t kind) {
439
1.22M
  zval *zv = zend_hash_find(&FC(seen_symbols), name);
440
1.22M
  if (zv) {
441
1.15M
    Z_LVAL_P(zv) |= kind;
442
1.15M
  } else {
443
69.0k
    zval tmp;
444
69.0k
    ZVAL_LONG(&tmp, kind);
445
69.0k
    zend_hash_add_new(&FC(seen_symbols), name, &tmp);
446
69.0k
  }
447
1.22M
}
448
449
2.31k
static bool zend_have_seen_symbol(zend_string *name, uint32_t kind) {
450
2.31k
  const zval *zv = zend_hash_find(&FC(seen_symbols), name);
451
2.31k
  return zv && (Z_LVAL_P(zv) & kind) != 0;
452
2.31k
}
453
454
void init_compiler(void) /* {{{ */
455
244k
{
456
244k
  CG(arena) = zend_arena_create(64 * 1024);
457
244k
  CG(active_op_array) = NULL;
458
244k
  memset(&CG(context), 0, sizeof(CG(context)));
459
244k
  zend_init_compiler_data_structures();
460
244k
  zend_init_rsrc_list();
461
244k
  zend_stream_init();
462
244k
  CG(unclean_shutdown) = 0;
463
464
244k
  CG(delayed_variance_obligations) = NULL;
465
244k
  CG(delayed_autoloads) = NULL;
466
244k
  CG(unlinked_uses) = NULL;
467
244k
  CG(current_linking_class) = NULL;
468
244k
}
469
/* }}} */
470
471
void shutdown_compiler(void) /* {{{ */
472
249k
{
473
  /* Reset filename before destroying the arena, as file cache may use arena allocated strings. */
474
249k
  zend_restore_compiled_filename(NULL);
475
476
249k
  zend_stack_destroy(&CG(loop_var_stack));
477
249k
  zend_stack_destroy(&CG(delayed_oplines_stack));
478
249k
  zend_stack_destroy(&CG(short_circuiting_opnums));
479
480
249k
  if (CG(delayed_variance_obligations)) {
481
256
    zend_hash_destroy(CG(delayed_variance_obligations));
482
256
    FREE_HASHTABLE(CG(delayed_variance_obligations));
483
256
    CG(delayed_variance_obligations) = NULL;
484
256
  }
485
249k
  if (CG(delayed_autoloads)) {
486
264
    zend_hash_destroy(CG(delayed_autoloads));
487
264
    FREE_HASHTABLE(CG(delayed_autoloads));
488
264
    CG(delayed_autoloads) = NULL;
489
264
  }
490
249k
  if (CG(unlinked_uses)) {
491
226
    zend_hash_destroy(CG(unlinked_uses));
492
226
    FREE_HASHTABLE(CG(unlinked_uses));
493
226
    CG(unlinked_uses) = NULL;
494
226
  }
495
249k
  CG(current_linking_class) = NULL;
496
249k
}
497
/* }}} */
498
499
ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
500
135k
{
501
135k
  CG(compiled_filename) = zend_string_copy(new_compiled_filename);
502
135k
  return new_compiled_filename;
503
135k
}
504
/* }}} */
505
506
ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
507
517k
{
508
517k
  if (CG(compiled_filename)) {
509
135k
    zend_string_release(CG(compiled_filename));
510
135k
    CG(compiled_filename) = NULL;
511
135k
  }
512
517k
  CG(compiled_filename) = original_compiled_filename;
513
517k
}
514
/* }}} */
515
516
ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
517
1.88M
{
518
1.88M
  return CG(compiled_filename);
519
1.88M
}
520
/* }}} */
521
522
ZEND_API uint32_t zend_get_compiled_lineno(void) /* {{{ */
523
352k
{
524
352k
  return CG(zend_lineno);
525
352k
}
526
/* }}} */
527
528
ZEND_API bool zend_is_compiling(void) /* {{{ */
529
3.08M
{
530
3.08M
  return CG(in_compilation);
531
3.08M
}
532
/* }}} */
533
534
static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */
535
27.4M
{
536
27.4M
  return (uint32_t)CG(active_op_array)->T++;
537
27.4M
}
538
/* }}} */
539
540
2.18M
static uint32_t lookup_cv(zend_string *name) /* {{{ */{
541
2.18M
  zend_op_array *op_array = CG(active_op_array);
542
2.18M
  int i = 0;
543
2.18M
  zend_ulong hash_value = zend_string_hash_val(name);
544
545
6.32M
  while (i < op_array->last_var) {
546
4.74M
    if (ZSTR_H(op_array->vars[i]) == hash_value
547
617k
     && zend_string_equals(op_array->vars[i], name)) {
548
615k
      return EX_NUM_TO_VAR(i);
549
615k
    }
550
4.13M
    i++;
551
4.13M
  }
552
1.57M
  i = op_array->last_var;
553
1.57M
  op_array->last_var++;
554
1.57M
  if (op_array->last_var > CG(context).vars_size) {
555
1.11M
    CG(context).vars_size += 16; /* FIXME */
556
1.11M
    op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
557
1.11M
  }
558
559
1.57M
  op_array->vars[i] = zend_string_copy(name);
560
1.57M
  return EX_NUM_TO_VAR(i);
561
2.18M
}
562
/* }}} */
563
564
zend_string *zval_make_interned_string(zval *zv)
565
33.0M
{
566
33.0M
  ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
567
33.0M
  Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
568
33.0M
  if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
569
4.46M
    Z_TYPE_FLAGS_P(zv) = 0;
570
4.46M
  }
571
33.0M
  return Z_STR_P(zv);
572
33.0M
}
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
33.1M
{
577
33.1M
  zval *lit = CT_CONSTANT_EX(op_array, literal_position);
578
33.1M
  if (Z_TYPE_P(zv) == IS_STRING) {
579
31.0M
    zval_make_interned_string(zv);
580
31.0M
  }
581
33.1M
  ZVAL_COPY_VALUE(lit, zv);
582
33.1M
  Z_EXTRA_P(lit) = 0;
583
33.1M
}
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
33.1M
{
591
33.1M
  zend_op_array *op_array = CG(active_op_array);
592
33.1M
  uint32_t i = op_array->last_literal;
593
33.1M
  op_array->last_literal++;
594
33.1M
  if (i >= CG(context).literals_size) {
595
5.26M
    while (i >= CG(context).literals_size) {
596
2.63M
      CG(context).literals_size += 16; /* FIXME */
597
2.63M
    }
598
2.63M
    op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
599
2.63M
  }
600
33.1M
  zend_insert_literal(op_array, zv, i);
601
33.1M
  return i;
602
33.1M
}
603
/* }}} */
604
605
static inline int zend_add_literal_string(zend_string **str) /* {{{ */
606
27.7M
{
607
27.7M
  int ret;
608
27.7M
  zval zv;
609
27.7M
  ZVAL_STR(&zv, *str);
610
27.7M
  ret = zend_add_literal(&zv);
611
27.7M
  *str = Z_STR(zv);
612
27.7M
  return ret;
613
27.7M
}
614
/* }}} */
615
616
static int zend_add_func_name_literal(zend_string *name) /* {{{ */
617
167k
{
618
  /* Original name */
619
167k
  int ret = zend_add_literal_string(&name);
620
621
  /* Lowercased name */
622
167k
  zend_string *lc_name = zend_string_tolower(name);
623
167k
  zend_add_literal_string(&lc_name);
624
625
167k
  return ret;
626
167k
}
627
/* }}} */
628
629
static int zend_add_ns_func_name_literal(zend_string *name) /* {{{ */
630
2.42M
{
631
2.42M
  const char *unqualified_name;
632
2.42M
  size_t unqualified_name_len;
633
634
  /* Original name */
635
2.42M
  int ret = zend_add_literal_string(&name);
636
637
  /* Lowercased name */
638
2.42M
  zend_string *lc_name = zend_string_tolower(name);
639
2.42M
  zend_add_literal_string(&lc_name);
640
641
  /* Lowercased unqualified name */
642
2.42M
  if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
643
2.42M
    lc_name = zend_string_alloc(unqualified_name_len, 0);
644
2.42M
    zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
645
2.42M
    zend_add_literal_string(&lc_name);
646
2.42M
  }
647
648
2.42M
  return ret;
649
2.42M
}
650
/* }}} */
651
652
static int zend_add_class_name_literal(zend_string *name) /* {{{ */
653
177k
{
654
  /* Original name */
655
177k
  int ret = zend_add_literal_string(&name);
656
657
  /* Lowercased name */
658
177k
  zend_string *lc_name = zend_string_tolower(name);
659
177k
  zend_add_literal_string(&lc_name);
660
661
177k
  return ret;
662
177k
}
663
/* }}} */
664
665
static int zend_add_const_name_literal(zend_string *name, bool unqualified) /* {{{ */
666
6.50M
{
667
6.50M
  zend_string *tmp_name;
668
669
6.50M
  int ret = zend_add_literal_string(&name);
670
671
6.50M
  size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
672
6.50M
  const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
673
6.50M
  if (after_ns) {
674
6.37M
    after_ns += 1;
675
6.37M
    ns_len = after_ns - ZSTR_VAL(name) - 1;
676
6.37M
    after_ns_len = ZSTR_LEN(name) - ns_len - 1;
677
678
    /* lowercased namespace name & original constant name */
679
6.37M
    tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
680
6.37M
    zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
681
6.37M
    zend_add_literal_string(&tmp_name);
682
683
6.37M
    if (!unqualified) {
684
1.57k
      return ret;
685
1.57k
    }
686
6.37M
  } else {
687
132k
    after_ns = ZSTR_VAL(name);
688
132k
  }
689
690
  /* original unqualified constant name */
691
6.50M
  tmp_name = zend_string_init(after_ns, after_ns_len, 0);
692
6.50M
  zend_add_literal_string(&tmp_name);
693
694
6.50M
  return ret;
695
6.50M
}
696
/* }}} */
697
698
68.5k
#define LITERAL_STR(op, str) do { \
699
68.5k
    zval _c; \
700
68.5k
    ZVAL_STR(&_c, str); \
701
68.5k
    op.constant = zend_add_literal(&_c); \
702
68.5k
  } while (0)
703
704
void zend_stop_lexing(void)
705
59
{
706
59
  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
59
  LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
711
59
}
712
713
static inline void zend_begin_loop(
714
    uint8_t free_opcode, const znode *loop_var, bool is_switch) /* {{{ */
715
40.3k
{
716
40.3k
  zend_brk_cont_element *brk_cont_element;
717
40.3k
  int parent = CG(context).current_brk_cont;
718
40.3k
  zend_loop_var info = {0};
719
720
40.3k
  CG(context).current_brk_cont = CG(context).last_brk_cont;
721
40.3k
  brk_cont_element = get_next_brk_cont_element();
722
40.3k
  brk_cont_element->parent = parent;
723
40.3k
  brk_cont_element->is_switch = is_switch;
724
725
40.3k
  if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
726
19.8k
    uint32_t start = get_next_op_number();
727
728
19.8k
    info.opcode = free_opcode;
729
19.8k
    info.var_type = loop_var->op_type;
730
19.8k
    info.var_num = loop_var->u.op.var;
731
19.8k
    brk_cont_element->start = start;
732
20.4k
  } else {
733
20.4k
    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
20.4k
    brk_cont_element->start = -1;
737
20.4k
  }
738
739
40.3k
  zend_stack_push(&CG(loop_var_stack), &info);
740
40.3k
}
741
/* }}} */
742
743
static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
744
40.2k
{
745
40.2k
  uint32_t end = get_next_op_number();
746
40.2k
  zend_brk_cont_element *brk_cont_element
747
40.2k
    = &CG(context).brk_cont_array[CG(context).current_brk_cont];
748
40.2k
  brk_cont_element->cont = cont_addr;
749
40.2k
  brk_cont_element->brk = end;
750
40.2k
  CG(context).current_brk_cont = brk_cont_element->parent;
751
752
40.2k
  zend_stack_del_top(&CG(loop_var_stack));
753
40.2k
}
754
/* }}} */
755
756
bool zend_op_may_elide_result(uint8_t opcode)
757
2.76M
{
758
2.76M
  switch (opcode) {
759
126k
    case ZEND_ASSIGN:
760
135k
    case ZEND_ASSIGN_DIM:
761
145k
    case ZEND_ASSIGN_OBJ:
762
146k
    case ZEND_ASSIGN_STATIC_PROP:
763
176k
    case ZEND_ASSIGN_OP:
764
177k
    case ZEND_ASSIGN_DIM_OP:
765
178k
    case ZEND_ASSIGN_OBJ_OP:
766
179k
    case ZEND_ASSIGN_STATIC_PROP_OP:
767
179k
    case ZEND_PRE_INC_STATIC_PROP:
768
179k
    case ZEND_PRE_DEC_STATIC_PROP:
769
179k
    case ZEND_PRE_INC_OBJ:
770
179k
    case ZEND_PRE_DEC_OBJ:
771
180k
    case ZEND_PRE_INC:
772
180k
    case ZEND_PRE_DEC:
773
284k
    case ZEND_DO_FCALL:
774
292k
    case ZEND_DO_ICALL:
775
299k
    case ZEND_DO_UCALL:
776
1.16M
    case ZEND_DO_FCALL_BY_NAME:
777
1.16M
    case ZEND_YIELD:
778
1.16M
    case ZEND_YIELD_FROM:
779
1.17M
    case ZEND_INCLUDE_OR_EVAL:
780
1.17M
      return true;
781
1.59M
    default:
782
1.59M
      return false;
783
2.76M
  }
784
2.76M
}
785
786
static void zend_do_free(znode *op1) /* {{{ */
787
2.90M
{
788
2.90M
  if (op1->op_type == IS_TMP_VAR) {
789
2.76M
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
790
791
2.91M
    while (opline->opcode == ZEND_END_SILENCE ||
792
2.90M
           opline->opcode == ZEND_OP_DATA) {
793
146k
      opline--;
794
146k
    }
795
796
2.76M
    if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
797
2.74M
      switch (opline->opcode) {
798
1.35k
        case ZEND_BOOL:
799
5.90k
        case ZEND_BOOL_NOT:
800
          /* boolean results don't have to be freed */
801
5.90k
          return;
802
189
        case ZEND_POST_INC_STATIC_PROP:
803
198
        case ZEND_POST_DEC_STATIC_PROP:
804
494
        case ZEND_POST_INC_OBJ:
805
608
        case ZEND_POST_DEC_OBJ:
806
5.14k
        case ZEND_POST_INC:
807
6.14k
        case ZEND_POST_DEC:
808
          /* convert $i++ to ++$i */
809
6.14k
          opline->opcode -= 2;
810
6.14k
          SET_UNUSED(opline->result);
811
6.14k
          return;
812
2.73M
        default:
813
2.73M
          if (zend_op_may_elide_result(opline->opcode)) {
814
1.16M
            SET_UNUSED(opline->result);
815
1.16M
            return;
816
1.16M
          }
817
1.56M
          break;
818
2.74M
      }
819
2.74M
    }
820
821
1.58M
    zend_emit_op(NULL, ZEND_FREE, op1, NULL);
822
1.58M
  } else if (op1->op_type == IS_VAR) {
823
76.8k
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
824
76.8k
    while (opline->opcode == ZEND_END_SILENCE ||
825
76.8k
        opline->opcode == ZEND_EXT_FCALL_END ||
826
76.8k
        opline->opcode == ZEND_OP_DATA) {
827
0
      opline--;
828
0
    }
829
76.8k
    if (opline->result_type == IS_VAR
830
75.6k
      && opline->result.var == op1->u.op.var) {
831
75.6k
      if (opline->opcode == ZEND_FETCH_THIS) {
832
0
        opline->opcode = ZEND_NOP;
833
0
      }
834
75.6k
      if (!ZEND_OP_IS_FRAMELESS_ICALL(opline->opcode)) {
835
75.6k
        SET_UNUSED(opline->result);
836
75.6k
      } else {
837
        /* Frameless calls usually use the return value, so always emit a free. This should be
838
         * faster than checking RETURN_VALUE_USED inside the handler. */
839
0
        zend_emit_op(NULL, ZEND_FREE, op1, NULL);
840
0
      }
841
75.6k
    } else {
842
3.79k
      while (opline >= CG(active_op_array)->opcodes) {
843
3.79k
        if ((opline->opcode == ZEND_FETCH_LIST_R ||
844
3.68k
             opline->opcode == ZEND_FETCH_LIST_W ||
845
2.56k
             opline->opcode == ZEND_EXT_STMT) &&
846
1.23k
            opline->op1_type == IS_VAR &&
847
1.23k
            opline->op1.var == op1->u.op.var) {
848
1.19k
          zend_emit_op(NULL, ZEND_FREE, op1, NULL);
849
1.19k
          return;
850
1.19k
        }
851
2.59k
        if (opline->result_type == IS_VAR
852
1.33k
          && opline->result.var == op1->u.op.var) {
853
0
          if (opline->opcode == ZEND_NEW) {
854
0
            zend_emit_op(NULL, ZEND_FREE, op1, NULL);
855
0
          }
856
0
          break;
857
0
        }
858
2.59k
        opline--;
859
2.59k
      }
860
1.19k
    }
861
76.8k
  } else if (op1->op_type == IS_CONST) {
862
    /* Destroy value without using GC: When opcache moves arrays into SHM it will
863
     * free the zend_array structure, so references to it from outside the op array
864
     * become invalid. GC would cause such a reference in the root buffer. */
865
59.9k
    zval_ptr_dtor_nogc(&op1->u.constant);
866
59.9k
  }
867
2.90M
}
868
/* }}} */
869
870
871
static const char *zend_modifier_token_to_string(uint32_t token)
872
79
{
873
79
  switch (token) {
874
5
    case T_PUBLIC:
875
5
      return "public";
876
1
    case T_PROTECTED:
877
1
      return "protected";
878
6
    case T_PRIVATE:
879
6
      return "private";
880
16
    case T_STATIC:
881
16
      return "static";
882
19
    case T_FINAL:
883
19
      return "final";
884
21
    case T_READONLY:
885
21
      return "readonly";
886
8
    case T_ABSTRACT:
887
8
      return "abstract";
888
0
    case T_PUBLIC_SET:
889
0
      return "public(set)";
890
1
    case T_PROTECTED_SET:
891
1
      return "protected(set)";
892
2
    case T_PRIVATE_SET:
893
2
      return "private(set)";
894
79
    EMPTY_SWITCH_DEFAULT_CASE()
895
79
  }
896
79
}
897
898
uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t token)
899
54.2k
{
900
54.2k
  switch (token) {
901
35.4k
    case T_PUBLIC:
902
35.4k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
903
35.4k
        return ZEND_ACC_PUBLIC;
904
35.4k
      }
905
5
      break;
906
2.12k
    case T_PROTECTED:
907
2.12k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
908
2.12k
        return ZEND_ACC_PROTECTED;
909
2.12k
      }
910
1
      break;
911
4.40k
    case T_PRIVATE:
912
4.40k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
913
4.39k
        return ZEND_ACC_PRIVATE;
914
4.39k
      }
915
6
      break;
916
774
    case T_READONLY:
917
774
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
918
761
        return ZEND_ACC_READONLY;
919
761
      }
920
13
      break;
921
874
    case T_ABSTRACT:
922
874
      if (target == ZEND_MODIFIER_TARGET_METHOD || target == ZEND_MODIFIER_TARGET_PROPERTY) {
923
873
        return ZEND_ACC_ABSTRACT;
924
873
      }
925
1
      break;
926
1.40k
    case T_FINAL:
927
1.40k
      return ZEND_ACC_FINAL;
928
8.30k
    case T_STATIC:
929
8.30k
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_METHOD) {
930
8.29k
        return ZEND_ACC_STATIC;
931
8.29k
      }
932
10
      break;
933
365
    case T_PUBLIC_SET:
934
365
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
935
365
        return ZEND_ACC_PUBLIC_SET;
936
365
      }
937
0
      break;
938
200
    case T_PROTECTED_SET:
939
200
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
940
199
        return ZEND_ACC_PROTECTED_SET;
941
199
      }
942
1
      break;
943
350
    case T_PRIVATE_SET:
944
350
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
945
348
        return ZEND_ACC_PRIVATE_SET;
946
348
      }
947
2
      break;
948
54.2k
  }
949
950
39
  char *member;
951
39
  if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
952
0
    member = "property";
953
39
  } else if (target == ZEND_MODIFIER_TARGET_METHOD) {
954
10
    member = "method";
955
29
  } else if (target == ZEND_MODIFIER_TARGET_CONSTANT) {
956
6
    member = "class constant";
957
23
  } else if (target == ZEND_MODIFIER_TARGET_CPP) {
958
6
    member = "parameter";
959
17
  } else if (target == ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
960
17
    member = "property hook";
961
17
  } else {
962
0
    ZEND_UNREACHABLE();
963
0
  }
964
965
39
  zend_throw_exception_ex(zend_ce_compile_error, 0,
966
39
    "Cannot use the %s modifier on a %s", zend_modifier_token_to_string(token), member);
967
39
  return 0;
968
39
}
969
970
uint32_t zend_modifier_list_to_flags(zend_modifier_target target, zend_ast *modifiers)
971
45.9k
{
972
45.9k
  uint32_t flags = 0;
973
45.9k
  const zend_ast_list *modifier_list = zend_ast_get_list(modifiers);
974
975
99.1k
  for (uint32_t i = 0; i < modifier_list->children; i++) {
976
53.3k
    uint32_t token = (uint32_t) Z_LVAL_P(zend_ast_get_zval(modifier_list->child[i]));
977
53.3k
    uint32_t new_flag = zend_modifier_token_to_flag(target, token);
978
53.3k
    if (!new_flag) {
979
34
      return 0;
980
34
    }
981
    /* Don't error immediately for duplicate flags, we want to prioritize the errors from zend_add_member_modifier(). */
982
53.3k
    bool duplicate_flag = (flags & new_flag);
983
53.3k
    flags = zend_add_member_modifier(flags, new_flag, target);
984
53.3k
    if (!flags) {
985
63
      return 0;
986
63
    }
987
53.2k
    if (duplicate_flag) {
988
40
      zend_throw_exception_ex(zend_ce_compile_error, 0,
989
40
        "Multiple %s modifiers are not allowed", zend_modifier_token_to_string(token));
990
40
      return 0;
991
40
    }
992
53.2k
  }
993
994
45.8k
  return flags;
995
45.9k
}
996
997
uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
998
155
{
999
155
  uint32_t new_flags = flags | new_flag;
1000
155
  if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
1001
1
    zend_throw_exception(zend_ce_compile_error,
1002
1
      "Multiple abstract modifiers are not allowed", 0);
1003
1
    return 0;
1004
1
  }
1005
154
  if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
1006
5
    zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
1007
5
    return 0;
1008
5
  }
1009
149
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
1010
6
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
1011
6
    return 0;
1012
6
  }
1013
143
  if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
1014
6
    zend_throw_exception(zend_ce_compile_error,
1015
6
      "Cannot use the final modifier on an abstract class", 0);
1016
6
    return 0;
1017
6
  }
1018
137
  return new_flags;
1019
143
}
1020
/* }}} */
1021
1022
uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag)
1023
64
{
1024
64
  uint32_t new_flags = flags | new_flag;
1025
64
  if (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
1026
5
    zend_throw_exception(zend_ce_compile_error,
1027
5
      "Cannot use the abstract modifier on an anonymous class", 0);
1028
5
    return 0;
1029
5
  }
1030
59
  if (new_flag & ZEND_ACC_FINAL) {
1031
6
    zend_throw_exception(zend_ce_compile_error, "Cannot use the final modifier on an anonymous class", 0);
1032
6
    return 0;
1033
6
  }
1034
53
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
1035
5
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
1036
5
    return 0;
1037
5
  }
1038
48
  return new_flags;
1039
53
}
1040
1041
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target) /* {{{ */
1042
53.3k
{
1043
53.3k
  uint32_t new_flags = flags | new_flag;
1044
53.3k
  if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
1045
43
    zend_throw_exception(zend_ce_compile_error,
1046
43
      "Multiple access type modifiers are not allowed", 0);
1047
43
    return 0;
1048
43
  }
1049
53.2k
  if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
1050
10
    if (target == ZEND_MODIFIER_TARGET_METHOD) {
1051
5
      zend_throw_exception(zend_ce_compile_error,
1052
5
        "Cannot use the final modifier on an abstract method", 0);
1053
5
      return 0;
1054
5
    }
1055
5
    if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
1056
5
      zend_throw_exception(zend_ce_compile_error,
1057
5
        "Cannot use the final modifier on an abstract property", 0);
1058
5
      return 0;
1059
5
    }
1060
5
  }
1061
53.2k
  if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
1062
26.1k
    if ((flags & ZEND_ACC_PPP_SET_MASK) && (new_flag & ZEND_ACC_PPP_SET_MASK)) {
1063
10
      zend_throw_exception(zend_ce_compile_error,
1064
10
        "Multiple access type modifiers are not allowed", 0);
1065
10
      return 0;
1066
10
    }
1067
26.1k
  }
1068
53.2k
  return new_flags;
1069
53.2k
}
1070
/* }}} */
1071
1072
17.8k
ZEND_API zend_string *zend_create_member_string(const zend_string *class_name, const zend_string *member_name) {
1073
17.8k
  return zend_string_concat3(
1074
17.8k
    ZSTR_VAL(class_name), ZSTR_LEN(class_name),
1075
17.8k
    "::", sizeof("::") - 1,
1076
17.8k
    ZSTR_VAL(member_name), ZSTR_LEN(member_name));
1077
17.8k
}
1078
1079
10.6M
static zend_string *zend_concat_names(const char *name1, size_t name1_len, const char *name2, size_t name2_len) {
1080
10.6M
  return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len);
1081
10.6M
}
1082
1083
11.2M
static zend_string *zend_prefix_with_ns(zend_string *name) {
1084
11.2M
  if (FC(current_namespace)) {
1085
10.6M
    const zend_string *ns = FC(current_namespace);
1086
10.6M
    return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
1087
10.6M
  } else {
1088
599k
    return zend_string_copy(name);
1089
599k
  }
1090
11.2M
}
1091
1092
static zend_string *zend_resolve_non_class_name(
1093
  zend_string *name, uint32_t type, bool *is_fully_qualified,
1094
  bool case_sensitive, const HashTable *current_import_sub
1095
9.28M
) {
1096
9.28M
  const char *compound;
1097
9.28M
  *is_fully_qualified = false;
1098
1099
9.28M
  if (ZSTR_VAL(name)[0] == '\\') {
1100
    /* Remove \ prefix (only relevant if this is a string rather than a label) */
1101
292
    *is_fully_qualified = true;
1102
292
    return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1103
292
  }
1104
1105
9.28M
  if (type == ZEND_NAME_FQ) {
1106
70.1k
    *is_fully_qualified = true;
1107
70.1k
    return zend_string_copy(name);
1108
70.1k
  }
1109
1110
9.21M
  if (type == ZEND_NAME_RELATIVE) {
1111
704
    *is_fully_qualified = true;
1112
704
    return zend_prefix_with_ns(name);
1113
704
  }
1114
1115
9.21M
  if (current_import_sub) {
1116
    /* If an unqualified name is a function/const alias, replace it. */
1117
1.49k
    zend_string *import_name;
1118
1.49k
    if (case_sensitive) {
1119
893
      import_name = zend_hash_find_ptr(current_import_sub, name);
1120
893
    } else {
1121
597
      import_name = zend_hash_find_ptr_lc(current_import_sub, name);
1122
597
    }
1123
1124
1.49k
    if (import_name) {
1125
440
      *is_fully_qualified = true;
1126
440
      return zend_string_copy(import_name);
1127
440
    }
1128
1.49k
  }
1129
1130
9.21M
  compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1131
9.21M
  if (compound) {
1132
4.19k
    *is_fully_qualified = true;
1133
4.19k
  }
1134
1135
9.21M
  if (compound && FC(imports)) {
1136
    /* If the first part of a qualified name is an alias, substitute it. */
1137
2.14k
    size_t len = compound - ZSTR_VAL(name);
1138
2.14k
    const zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1139
1140
2.14k
    if (import_name) {
1141
347
      return zend_concat_names(
1142
347
        ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1143
347
    }
1144
2.14k
  }
1145
1146
9.21M
  return zend_prefix_with_ns(name);
1147
9.21M
}
1148
/* }}} */
1149
1150
static zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1151
2.66M
{
1152
2.66M
  return zend_resolve_non_class_name(
1153
2.66M
    name, type, is_fully_qualified, false, FC(imports_function));
1154
2.66M
}
1155
1156
static zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1157
6.62M
{
1158
6.62M
  return zend_resolve_non_class_name(
1159
6.62M
    name, type, is_fully_qualified, true, FC(imports_const));
1160
6.62M
}
1161
1162
static zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
1163
1.87M
{
1164
1.87M
  const char *compound;
1165
1166
1.87M
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1167
3.15k
    if (type == ZEND_NAME_FQ) {
1168
7
      zend_error_noreturn(E_COMPILE_ERROR,
1169
7
        "'\\%s' is an invalid class name", ZSTR_VAL(name));
1170
7
    }
1171
3.14k
    if (type == ZEND_NAME_RELATIVE) {
1172
0
      zend_error_noreturn(E_COMPILE_ERROR,
1173
0
        "'namespace\\%s' is an invalid class name", ZSTR_VAL(name));
1174
0
    }
1175
3.14k
    ZEND_ASSERT(type == ZEND_NAME_NOT_FQ);
1176
3.14k
    return zend_string_copy(name);
1177
3.14k
  }
1178
1179
1.87M
  if (type == ZEND_NAME_RELATIVE) {
1180
330
    return zend_prefix_with_ns(name);
1181
330
  }
1182
1183
1.87M
  if (type == ZEND_NAME_FQ) {
1184
14.7k
    if (ZSTR_VAL(name)[0] == '\\') {
1185
      /* Remove \ prefix (only relevant if this is a string rather than a label) */
1186
59
      name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1187
59
      if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1188
1
        zend_error_noreturn(E_COMPILE_ERROR,
1189
1
          "'\\%s' is an invalid class name", ZSTR_VAL(name));
1190
1
      }
1191
58
      return name;
1192
59
    }
1193
1194
14.6k
    return zend_string_copy(name);
1195
14.7k
  }
1196
1197
1.86M
  if (FC(imports)) {
1198
3.36k
    compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1199
3.36k
    if (compound) {
1200
      /* If the first part of a qualified name is an alias, substitute it. */
1201
810
      size_t len = compound - ZSTR_VAL(name);
1202
810
      const zend_string *import_name =
1203
810
        zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1204
1205
810
      if (import_name) {
1206
303
        return zend_concat_names(
1207
303
          ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1208
303
      }
1209
2.55k
    } else {
1210
      /* If an unqualified name is an alias, replace it. */
1211
2.55k
      zend_string *import_name
1212
2.55k
        = zend_hash_find_ptr_lc(FC(imports), name);
1213
1214
2.55k
      if (import_name) {
1215
1.07k
        return zend_string_copy(import_name);
1216
1.07k
      }
1217
2.55k
    }
1218
3.36k
  }
1219
1220
  /* If not fully qualified and not an alias, prepend the current namespace */
1221
1.86M
  return zend_prefix_with_ns(name);
1222
1.86M
}
1223
/* }}} */
1224
1225
static zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
1226
1.77M
{
1227
1.77M
  const zval *class_name = zend_ast_get_zval(ast);
1228
1.77M
  if (Z_TYPE_P(class_name) != IS_STRING) {
1229
27
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1230
27
  }
1231
1.77M
  return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
1232
1.77M
}
1233
/* }}} */
1234
1235
static void label_ptr_dtor(zval *zv) /* {{{ */
1236
2.39k
{
1237
2.39k
  efree_size(Z_PTR_P(zv), sizeof(zend_label));
1238
2.39k
}
1239
/* }}} */
1240
1241
2.08k
static void str_dtor(zval *zv)  /* {{{ */ {
1242
2.08k
  zend_string_release_ex(Z_STR_P(zv), 0);
1243
2.08k
}
1244
/* }}} */
1245
1246
static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
1247
30.9k
{
1248
30.9k
  zend_op_array *op_array = CG(active_op_array);
1249
30.9k
  uint32_t try_catch_offset = op_array->last_try_catch++;
1250
30.9k
  zend_try_catch_element *elem;
1251
1252
30.9k
  op_array->try_catch_array = safe_erealloc(
1253
30.9k
    op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
1254
1255
30.9k
  elem = &op_array->try_catch_array[try_catch_offset];
1256
30.9k
  elem->try_op = try_op;
1257
30.9k
  elem->catch_op = 0;
1258
30.9k
  elem->finally_op = 0;
1259
30.9k
  elem->finally_end = 0;
1260
1261
30.9k
  return try_catch_offset;
1262
30.9k
}
1263
/* }}} */
1264
1265
ZEND_API void function_add_ref(zend_function *function) /* {{{ */
1266
1.59k
{
1267
1.59k
  if (function->type == ZEND_USER_FUNCTION) {
1268
1.59k
    zend_op_array *op_array = &function->op_array;
1269
1.59k
    if (op_array->refcount) {
1270
176
      (*op_array->refcount)++;
1271
176
    }
1272
1273
1.59k
    ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
1274
1.59k
    ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
1275
1.59k
  }
1276
1277
1.59k
  if (function->common.function_name) {
1278
1.59k
    zend_string_addref(function->common.function_name);
1279
1.59k
  }
1280
1.59k
}
1281
/* }}} */
1282
1283
static zend_never_inline ZEND_COLD ZEND_NORETURN void do_bind_function_error(const zend_string *lcname, const zend_op_array *op_array, bool compile_time) /* {{{ */
1284
113
{
1285
113
  const zval *zv = zend_hash_find_known_hash(compile_time ? CG(function_table) : EG(function_table), lcname);
1286
113
  int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
1287
113
  const zend_function *old_function;
1288
1289
113
  ZEND_ASSERT(zv != NULL);
1290
113
  old_function = Z_PTR_P(zv);
1291
113
  if (old_function->type == ZEND_USER_FUNCTION
1292
108
    && old_function->op_array.last > 0) {
1293
108
    zend_error_noreturn(error_level, "Cannot redeclare function %s() (previously declared in %s:%d)",
1294
108
          op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name),
1295
108
          ZSTR_VAL(old_function->op_array.filename),
1296
108
          old_function->op_array.line_start);
1297
108
  } else {
1298
5
    zend_error_noreturn(error_level, "Cannot redeclare function %s()",
1299
5
      op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name));
1300
5
  }
1301
113
}
1302
1303
ZEND_API zend_result do_bind_function(zend_function *func, const zval *lcname) /* {{{ */
1304
100
{
1305
100
  zend_function *added_func = zend_hash_add_ptr(EG(function_table), Z_STR_P(lcname), func);
1306
100
  if (UNEXPECTED(!added_func)) {
1307
7
    do_bind_function_error(Z_STR_P(lcname), &func->op_array, false);
1308
0
    return FAILURE;
1309
7
  }
1310
1311
93
  if (func->op_array.refcount) {
1312
16
    ++*func->op_array.refcount;
1313
16
  }
1314
93
  if (func->common.function_name) {
1315
93
    zend_string_addref(func->common.function_name);
1316
93
  }
1317
93
  zend_observer_function_declared_notify(&func->op_array, Z_STR_P(lcname));
1318
93
  return SUCCESS;
1319
100
}
1320
/* }}} */
1321
1322
ZEND_API zend_class_entry *zend_bind_class_in_slot(
1323
    zval *class_table_slot, const zval *lcname, zend_string *lc_parent_name)
1324
6.48k
{
1325
6.48k
  zend_class_entry *ce = Z_PTR_P(class_table_slot);
1326
6.48k
  bool is_preloaded =
1327
6.48k
    (ce->ce_flags & ZEND_ACC_PRELOADED) && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD);
1328
6.48k
  bool success;
1329
6.48k
  if (EXPECTED(!is_preloaded)) {
1330
6.48k
    success = zend_hash_set_bucket_key(EG(class_table), (Bucket*) class_table_slot, Z_STR_P(lcname)) != NULL;
1331
6.48k
  } else {
1332
    /* If preloading is used, don't replace the existing bucket, add a new one. */
1333
0
    success = zend_hash_add_ptr(EG(class_table), Z_STR_P(lcname), ce) != NULL;
1334
0
  }
1335
6.48k
  if (UNEXPECTED(!success)) {
1336
126
    zend_class_entry *old_class = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1337
126
    ZEND_ASSERT(old_class);
1338
126
    zend_class_redeclaration_error(E_COMPILE_ERROR, old_class);
1339
126
    return NULL;
1340
126
  }
1341
1342
6.35k
  if (ce->ce_flags & ZEND_ACC_LINKED) {
1343
254
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1344
254
    return ce;
1345
254
  }
1346
1347
6.10k
  ce = zend_do_link_class(ce, lc_parent_name, Z_STR_P(lcname));
1348
6.10k
  if (ce) {
1349
4.85k
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1350
4.85k
    return ce;
1351
4.85k
  }
1352
1353
1.24k
  if (!is_preloaded) {
1354
    /* Reload bucket pointer, the hash table may have been reallocated */
1355
227
    zval *zv = zend_hash_find(EG(class_table), Z_STR_P(lcname));
1356
227
    zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(lcname + 1));
1357
1.02k
  } else {
1358
1.02k
    zend_hash_del(EG(class_table), Z_STR_P(lcname));
1359
1.02k
  }
1360
1.24k
  return NULL;
1361
6.10k
}
1362
1363
ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */
1364
5.94k
{
1365
5.94k
  zval *rtd_key, *zv;
1366
1367
5.94k
  rtd_key = lcname + 1;
1368
1369
5.94k
  zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(rtd_key));
1370
1371
5.94k
  if (UNEXPECTED(!zv)) {
1372
11
    const zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1373
11
    ZEND_ASSERT(ce);
1374
11
    zend_class_redeclaration_error(E_COMPILE_ERROR, ce);
1375
11
    return FAILURE;
1376
11
  }
1377
1378
  /* Register the derived class */
1379
5.93k
  return zend_bind_class_in_slot(zv, lcname, lc_parent_name) ? SUCCESS : FAILURE;
1380
5.94k
}
1381
/* }}} */
1382
1383
17.4k
static zend_string *add_type_string(zend_string *type, zend_string *new_type, bool is_intersection) {
1384
17.4k
  zend_string *result;
1385
17.4k
  if (type == NULL) {
1386
13.0k
    return zend_string_copy(new_type);
1387
13.0k
  }
1388
1389
4.43k
  if (is_intersection) {
1390
1.97k
    result = zend_string_concat3(ZSTR_VAL(type), ZSTR_LEN(type),
1391
1.97k
      "&", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1392
1.97k
    zend_string_release(type);
1393
2.45k
  } else {
1394
2.45k
    result = zend_string_concat3(
1395
2.45k
      ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1396
2.45k
    zend_string_release(type);
1397
2.45k
  }
1398
4.43k
  return result;
1399
17.4k
}
1400
1401
2.91k
static zend_string *resolve_class_name(zend_string *name, const zend_class_entry *scope) {
1402
2.91k
  if (scope) {
1403
1.85k
    if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1404
31
      name = scope->name;
1405
1.81k
    } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT)) && scope->parent) {
1406
0
      name = scope->parent->name;
1407
0
    }
1408
1.85k
  }
1409
1410
  /* The resolved name for anonymous classes contains null bytes. Cut off everything after the
1411
   * null byte here, to avoid larger parts of the type being omitted by printing code later. */
1412
2.91k
  size_t len = strlen(ZSTR_VAL(name));
1413
2.91k
  if (len != ZSTR_LEN(name)) {
1414
12
    return zend_string_init(ZSTR_VAL(name), len, 0);
1415
12
  }
1416
2.90k
  return zend_string_copy(name);
1417
2.91k
}
1418
1419
static zend_string *add_intersection_type(zend_string *str,
1420
  const zend_type_list *intersection_type_list, zend_class_entry *scope,
1421
  bool is_bracketed)
1422
1.16k
{
1423
1.16k
  const zend_type *single_type;
1424
1.16k
  zend_string *intersection_str = NULL;
1425
1426
4.30k
  ZEND_TYPE_LIST_FOREACH(intersection_type_list, single_type) {
1427
4.30k
    ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*single_type));
1428
4.30k
    ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*single_type));
1429
1430
3.13k
    intersection_str = add_type_string(intersection_str, ZEND_TYPE_NAME(*single_type), /* is_intersection */ true);
1431
3.13k
  } ZEND_TYPE_LIST_FOREACH_END();
1432
1433
1.16k
  ZEND_ASSERT(intersection_str);
1434
1435
1.16k
  if (is_bracketed) {
1436
837
    zend_string *result = zend_string_concat3("(", 1, ZSTR_VAL(intersection_str), ZSTR_LEN(intersection_str), ")", 1);
1437
837
    zend_string_release(intersection_str);
1438
837
    intersection_str = result;
1439
837
  }
1440
1.16k
  str = add_type_string(str, intersection_str, /* is_intersection */ false);
1441
1.16k
  zend_string_release(intersection_str);
1442
1.16k
  return str;
1443
1.16k
}
1444
1445
13.8k
zend_string *zend_type_to_string_resolved(const zend_type type, zend_class_entry *scope) {
1446
13.8k
  zend_string *str = NULL;
1447
1448
  /* Pure intersection type */
1449
13.8k
  if (ZEND_TYPE_IS_INTERSECTION(type)) {
1450
327
    ZEND_ASSERT(!ZEND_TYPE_IS_UNION(type));
1451
327
    str = add_intersection_type(str, ZEND_TYPE_LIST(type), scope, /* is_bracketed */ false);
1452
13.5k
  } else if (ZEND_TYPE_HAS_LIST(type)) {
1453
    /* A union type might not be a list */
1454
515
    const zend_type *list_type;
1455
2.26k
    ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
1456
2.26k
      if (ZEND_TYPE_IS_INTERSECTION(*list_type)) {
1457
837
        str = add_intersection_type(str, ZEND_TYPE_LIST(*list_type), scope, /* is_bracketed */ true);
1458
837
        continue;
1459
837
      }
1460
908
      ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type));
1461
908
      ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*list_type));
1462
1463
908
      zend_string *name = ZEND_TYPE_NAME(*list_type);
1464
908
      zend_string *resolved = resolve_class_name(name, scope);
1465
908
      str = add_type_string(str, resolved, /* is_intersection */ false);
1466
908
      zend_string_release(resolved);
1467
908
    } ZEND_TYPE_LIST_FOREACH_END();
1468
13.0k
  } else if (ZEND_TYPE_HAS_NAME(type)) {
1469
2.00k
    str = resolve_class_name(ZEND_TYPE_NAME(type), scope);
1470
2.00k
  }
1471
1472
13.8k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
1473
1474
13.8k
  if (type_mask == MAY_BE_ANY) {
1475
191
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_MIXED), /* is_intersection */ false);
1476
1477
191
    return str;
1478
191
  }
1479
13.6k
  if (type_mask & MAY_BE_STATIC) {
1480
95
    zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC);
1481
    // During compilation of eval'd code the called scope refers to the scope calling the eval
1482
95
    if (scope && !zend_is_compiling()) {
1483
36
      const zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
1484
36
      if (called_scope) {
1485
15
        name = called_scope->name;
1486
15
      }
1487
36
    }
1488
95
    str = add_type_string(str, name, /* is_intersection */ false);
1489
95
  }
1490
13.6k
  if (type_mask & MAY_BE_CALLABLE) {
1491
69
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE), /* is_intersection */ false);
1492
69
  }
1493
13.6k
  if (type_mask & MAY_BE_OBJECT) {
1494
179
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT), /* is_intersection */ false);
1495
179
  }
1496
13.6k
  if (type_mask & MAY_BE_ARRAY) {
1497
1.25k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY), /* is_intersection */ false);
1498
1.25k
  }
1499
13.6k
  if (type_mask & MAY_BE_STRING) {
1500
5.89k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING), /* is_intersection */ false);
1501
5.89k
  }
1502
13.6k
  if (type_mask & MAY_BE_LONG) {
1503
3.11k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT), /* is_intersection */ false);
1504
3.11k
  }
1505
13.6k
  if (type_mask & MAY_BE_DOUBLE) {
1506
379
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT), /* is_intersection */ false);
1507
379
  }
1508
13.6k
  if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
1509
522
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL), /* is_intersection */ false);
1510
13.1k
  } else if (type_mask & MAY_BE_FALSE) {
1511
214
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE), /* is_intersection */ false);
1512
12.9k
  } else if (type_mask & MAY_BE_TRUE) {
1513
63
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_TRUE), /* is_intersection */ false);
1514
63
  }
1515
13.6k
  if (type_mask & MAY_BE_VOID) {
1516
126
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID), /* is_intersection */ false);
1517
126
  }
1518
13.6k
  if (type_mask & MAY_BE_NEVER) {
1519
18
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NEVER), /* is_intersection */ false);
1520
18
  }
1521
1522
13.6k
  if (type_mask & MAY_BE_NULL) {
1523
1.07k
    bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
1524
1.07k
    bool has_intersection = !str || memchr(ZSTR_VAL(str), '&', ZSTR_LEN(str)) != NULL;
1525
1.07k
    if (!is_union && !has_intersection) {
1526
935
      zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
1527
935
      zend_string_release(str);
1528
935
      return nullable_str;
1529
935
    }
1530
1531
139
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE), /* is_intersection */ false);
1532
139
  }
1533
12.7k
  return str;
1534
13.6k
}
1535
1536
9.77k
ZEND_API zend_string *zend_type_to_string(zend_type type) {
1537
9.77k
  return zend_type_to_string_resolved(type, NULL);
1538
9.77k
}
1539
1540
1.21k
static bool is_generator_compatible_class_type(const zend_string *name) {
1541
1.21k
  return zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_TRAVERSABLE))
1542
951
    || zend_string_equals_literal_ci(name, "Iterator")
1543
706
    || zend_string_equals_literal_ci(name, "Generator");
1544
1.21k
}
1545
1546
static void zend_mark_function_as_generator(void) /* {{{ */
1547
221k
{
1548
221k
  if (!CG(active_op_array)->function_name) {
1549
60
    zend_error_noreturn(E_COMPILE_ERROR,
1550
60
      "The \"yield\" expression can only be used inside a function");
1551
60
  }
1552
1553
220k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
1554
833
    const zend_type return_type = CG(active_op_array)->arg_info[-1].type;
1555
833
    bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_OBJECT) != 0;
1556
833
    if (!valid_type) {
1557
795
      const zend_type *single_type;
1558
2.01k
      ZEND_TYPE_FOREACH(return_type, single_type) {
1559
2.01k
        if (ZEND_TYPE_HAS_NAME(*single_type)
1560
1.21k
            && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) {
1561
766
          valid_type = true;
1562
766
          break;
1563
766
        }
1564
2.01k
      } ZEND_TYPE_FOREACH_END();
1565
795
    }
1566
1567
833
    if (!valid_type) {
1568
29
      zend_string *str = zend_type_to_string(return_type);
1569
29
      zend_error_noreturn(E_COMPILE_ERROR,
1570
29
        "Generator return type must be a supertype of Generator, %s given",
1571
29
        ZSTR_VAL(str));
1572
29
    }
1573
833
  }
1574
1575
220k
  CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR;
1576
220k
}
1577
/* }}} */
1578
1579
ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal) /* {{{ */
1580
53.9k
{
1581
53.9k
  size_t prop_name_length = 1 + src1_length + 1 + src2_length;
1582
53.9k
  zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
1583
1584
53.9k
  ZSTR_VAL(prop_name)[0] = '\0';
1585
53.9k
  memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
1586
53.9k
  memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
1587
53.9k
  return prop_name;
1588
53.9k
}
1589
/* }}} */
1590
1591
ZEND_API zend_result zend_unmangle_property_name_ex(const zend_string *name, const char **class_name, const char **prop_name, size_t *prop_len) /* {{{ */
1592
1.07M
{
1593
1.07M
  size_t class_name_len;
1594
1.07M
  size_t anonclass_src_len;
1595
1596
1.07M
  *class_name = NULL;
1597
1598
1.07M
  if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
1599
975k
    *prop_name = ZSTR_VAL(name);
1600
975k
    if (prop_len) {
1601
651k
      *prop_len = ZSTR_LEN(name);
1602
651k
    }
1603
975k
    return SUCCESS;
1604
975k
  }
1605
97.3k
  if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
1606
1.08k
    zend_error(E_NOTICE, "Illegal member variable name");
1607
1.08k
    *prop_name = ZSTR_VAL(name);
1608
1.08k
    if (prop_len) {
1609
1.03k
      *prop_len = ZSTR_LEN(name);
1610
1.03k
    }
1611
1.08k
    return FAILURE;
1612
1.08k
  }
1613
1614
96.2k
  class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
1615
96.2k
  if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
1616
639
    zend_error(E_NOTICE, "Corrupt member variable name");
1617
639
    *prop_name = ZSTR_VAL(name);
1618
639
    if (prop_len) {
1619
423
      *prop_len = ZSTR_LEN(name);
1620
423
    }
1621
639
    return FAILURE;
1622
639
  }
1623
1624
95.6k
  *class_name = ZSTR_VAL(name) + 1;
1625
95.6k
  anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2);
1626
95.6k
  if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) {
1627
19.9k
    class_name_len += anonclass_src_len + 1;
1628
19.9k
  }
1629
95.6k
  *prop_name = ZSTR_VAL(name) + class_name_len + 2;
1630
95.6k
  if (prop_len) {
1631
62.1k
    *prop_len = ZSTR_LEN(name) - class_name_len - 2;
1632
62.1k
  }
1633
95.6k
  return SUCCESS;
1634
96.2k
}
1635
/* }}} */
1636
1637
static bool array_is_const_ex(const zend_array *array, uint32_t *max_checks)
1638
49
{
1639
49
  if (zend_hash_num_elements(array) > *max_checks) {
1640
0
    return false;
1641
0
  }
1642
49
  *max_checks -= zend_hash_num_elements(array);
1643
1644
49
  zval *element;
1645
115
  ZEND_HASH_FOREACH_VAL(array, element) {
1646
115
    if (Z_TYPE_P(element) < IS_ARRAY) {
1647
33
      continue;
1648
33
    } else if (Z_TYPE_P(element) == IS_ARRAY) {
1649
0
      if (!array_is_const_ex(array, max_checks)) {
1650
0
        return false;
1651
0
      }
1652
0
    } else {
1653
0
      return false;
1654
0
    }
1655
115
  } ZEND_HASH_FOREACH_END();
1656
1657
49
  return true;
1658
49
}
1659
1660
static bool array_is_const(const zend_array *array)
1661
49
{
1662
49
  uint32_t max_checks = 50;
1663
49
  return array_is_const_ex(array, &max_checks);
1664
49
}
1665
1666
9.47k
static bool can_ct_eval_const(const zend_constant *c) {
1667
9.47k
  if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
1668
188
    return 0;
1669
188
  }
1670
9.28k
  if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
1671
9.26k
      && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
1672
8.18k
      && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
1673
8.18k
        && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
1674
8.18k
    return 1;
1675
8.18k
  }
1676
1.09k
  if (Z_TYPE(c->value) < IS_ARRAY
1677
1.09k
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1678
14
    return 1;
1679
1.08k
  } else if (Z_TYPE(c->value) == IS_ARRAY
1680
0
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)
1681
0
      && array_is_const(Z_ARR(c->value))) {
1682
0
    return 1;
1683
0
  }
1684
1.08k
  return 0;
1685
1.09k
}
1686
1687
static bool zend_try_ct_eval_const(zval *zv, zend_string *name, bool is_fully_qualified) /* {{{ */
1688
6.62M
{
1689
  /* Substitute true, false and null (including unqualified usage in namespaces)
1690
   * before looking up the possibly namespaced name. */
1691
6.62M
  const char *lookup_name = ZSTR_VAL(name);
1692
6.62M
  size_t lookup_len = ZSTR_LEN(name);
1693
1694
6.62M
  if (!is_fully_qualified) {
1695
6.62M
    zend_get_unqualified_name(name, &lookup_name, &lookup_len);
1696
6.62M
  }
1697
1698
6.62M
  zend_constant *c;
1699
6.62M
  if ((c = zend_get_special_const(lookup_name, lookup_len))) {
1700
34.1k
    ZVAL_COPY_VALUE(zv, &c->value);
1701
34.1k
    return 1;
1702
34.1k
  }
1703
6.59M
  c = zend_hash_find_ptr(EG(zend_constants), name);
1704
6.59M
  if (c && can_ct_eval_const(c)) {
1705
8.19k
    ZVAL_COPY_OR_DUP(zv, &c->value);
1706
8.19k
    return 1;
1707
8.19k
  }
1708
6.58M
  return 0;
1709
6.59M
}
1710
/* }}} */
1711
1712
static inline bool zend_is_scope_known(void) /* {{{ */
1713
34.9k
{
1714
34.9k
  if (!CG(active_op_array)) {
1715
    /* This can only happen when evaluating a default value string. */
1716
0
    return 0;
1717
0
  }
1718
1719
34.9k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
1720
    /* Closures can be rebound to a different scope */
1721
24.0k
    return 0;
1722
24.0k
  }
1723
1724
10.9k
  if (!CG(active_class_entry)) {
1725
    /* The scope is known if we're in a free function (no scope), but not if we're in
1726
     * a file/eval (which inherits including/eval'ing scope). */
1727
1.37k
    return CG(active_op_array)->function_name != NULL;
1728
1.37k
  }
1729
1730
  /* For traits self etc refers to the using class, not the trait itself */
1731
9.53k
  return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
1732
10.9k
}
1733
/* }}} */
1734
1735
static inline bool class_name_refers_to_active_ce(const zend_string *class_name, uint32_t fetch_type) /* {{{ */
1736
80.2k
{
1737
80.2k
  if (!CG(active_class_entry)) {
1738
75.8k
    return 0;
1739
75.8k
  }
1740
4.39k
  if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
1741
1.01k
    return 1;
1742
1.01k
  }
1743
3.38k
  return fetch_type == ZEND_FETCH_CLASS_DEFAULT
1744
2.79k
    && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
1745
4.39k
}
1746
/* }}} */
1747
1748
uint32_t zend_get_class_fetch_type(const zend_string *name) /* {{{ */
1749
2.46M
{
1750
2.46M
  if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1751
20.6k
    return ZEND_FETCH_CLASS_SELF;
1752
2.44M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT))) {
1753
5.28k
    return ZEND_FETCH_CLASS_PARENT;
1754
2.43M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_STATIC))) {
1755
1.71k
    return ZEND_FETCH_CLASS_STATIC;
1756
2.43M
  } else {
1757
2.43M
    return ZEND_FETCH_CLASS_DEFAULT;
1758
2.43M
  }
1759
2.46M
}
1760
/* }}} */
1761
1762
static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
1763
345k
{
1764
  /* Fully qualified names are always default refs */
1765
345k
  if (name_ast->attr == ZEND_NAME_FQ) {
1766
3.14k
    return ZEND_FETCH_CLASS_DEFAULT;
1767
3.14k
  }
1768
1769
342k
  return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
1770
345k
}
1771
/* }}} */
1772
1773
static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type)
1774
108k
{
1775
108k
  zend_string *class_name = zend_ast_get_str(ast);
1776
108k
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) {
1777
18
    zend_error_noreturn(E_COMPILE_ERROR,
1778
18
      "Cannot use \"%s\" as %s, as it is reserved",
1779
18
      ZSTR_VAL(class_name), type);
1780
18
  }
1781
108k
  return zend_resolve_class_name(class_name, ast->attr);
1782
108k
}
1783
1784
static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
1785
22.6k
{
1786
22.6k
  if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) {
1787
4.50k
    zend_class_entry *ce = CG(active_class_entry);
1788
4.50k
    if (!ce) {
1789
25
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
1790
25
        fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
1791
25
        fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
1792
4.48k
    } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) {
1793
23
      zend_error_noreturn(E_COMPILE_ERROR,
1794
23
        "Cannot use \"parent\" when current class scope has no parent");
1795
23
    }
1796
4.50k
  }
1797
22.6k
}
1798
/* }}} */
1799
1800
static bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */
1801
7.56k
{
1802
7.56k
  uint32_t fetch_type;
1803
7.56k
  const zval *class_name;
1804
1805
7.56k
  if (class_ast->kind != ZEND_AST_ZVAL) {
1806
1.96k
    return 0;
1807
1.96k
  }
1808
1809
5.60k
  class_name = zend_ast_get_zval(class_ast);
1810
1811
5.60k
  if (Z_TYPE_P(class_name) != IS_STRING) {
1812
5
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1813
5
  }
1814
1815
5.59k
  fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name));
1816
5.59k
  zend_ensure_valid_class_fetch_type(fetch_type);
1817
1818
5.59k
  switch (fetch_type) {
1819
645
    case ZEND_FETCH_CLASS_SELF:
1820
645
      if (CG(active_class_entry) && zend_is_scope_known()) {
1821
366
        ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
1822
366
        return 1;
1823
366
      }
1824
279
      return 0;
1825
790
    case ZEND_FETCH_CLASS_PARENT:
1826
790
      if (CG(active_class_entry) && CG(active_class_entry)->parent_name
1827
415
          && zend_is_scope_known()) {
1828
415
        ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name);
1829
415
        return 1;
1830
415
      }
1831
375
      return 0;
1832
316
    case ZEND_FETCH_CLASS_STATIC:
1833
316
      return 0;
1834
3.83k
    case ZEND_FETCH_CLASS_DEFAULT:
1835
3.83k
      ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
1836
3.83k
      return 1;
1837
5.59k
    EMPTY_SWITCH_DEFAULT_CASE()
1838
5.59k
  }
1839
5.59k
}
1840
/* }}} */
1841
1842
/* We don't use zend_verify_const_access because we need to deal with unlinked classes. */
1843
static bool zend_verify_ct_const_access(const zend_class_constant *c, const zend_class_entry *scope)
1844
568
{
1845
568
  if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED) {
1846
14
    return 0;
1847
554
  } else if (c->ce->ce_flags & ZEND_ACC_TRAIT) {
1848
    /* This condition is only met on directly accessing trait constants,
1849
     * because the ce is replaced to the class entry of the composing class
1850
     * on binding. */
1851
0
    return 0;
1852
554
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) {
1853
370
    return 1;
1854
370
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) {
1855
184
    return c->ce == scope;
1856
184
  } else {
1857
0
    zend_class_entry *ce = c->ce;
1858
0
    while (1) {
1859
0
      if (ce == scope) {
1860
0
        return 1;
1861
0
      }
1862
0
      if (!ce->parent) {
1863
0
        break;
1864
0
      }
1865
0
      if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
1866
0
        ce = ce->parent;
1867
0
      } else {
1868
0
        ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name);
1869
0
        if (!ce) {
1870
0
          break;
1871
0
        }
1872
0
      }
1873
0
    }
1874
    /* Reverse case cannot be true during compilation */
1875
0
    return 0;
1876
0
  }
1877
568
}
1878
1879
static bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
1880
80.2k
{
1881
80.2k
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
1882
80.2k
  zend_class_constant *cc;
1883
80.2k
  zval *c;
1884
1885
80.2k
  if (class_name_refers_to_active_ce(class_name, fetch_type)) {
1886
1.13k
    cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
1887
79.1k
  } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1888
496
    const zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name);
1889
496
    if (ce) {
1890
45
      cc = zend_hash_find_ptr(&ce->constants_table, name);
1891
451
    } else {
1892
451
      return 0;
1893
451
    }
1894
78.6k
  } else {
1895
78.6k
    return 0;
1896
78.6k
  }
1897
1898
1.17k
  if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
1899
358
    return 0;
1900
358
  }
1901
1902
818
  if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) {
1903
264
    return 0;
1904
264
  }
1905
1906
554
  c = &cc->value;
1907
1908
  /* Substitute case-sensitive (or lowercase) persistent class constants */
1909
554
  if (Z_TYPE_P(c) < IS_ARRAY) {
1910
344
    ZVAL_COPY_OR_DUP(zv, c);
1911
344
    return 1;
1912
344
  } else if (Z_TYPE_P(c) == IS_ARRAY && array_is_const(Z_ARR_P(c))) {
1913
49
    ZVAL_COPY_OR_DUP(zv, c);
1914
49
    return 1;
1915
49
  }
1916
1917
161
  return 0;
1918
554
}
1919
/* }}} */
1920
1921
static void zend_add_to_list(void *result, void *item) /* {{{ */
1922
112k
{
1923
112k
  void** list = *(void**)result;
1924
112k
  size_t n = 0;
1925
1926
112k
  if (list) {
1927
230k
    while (list[n]) {
1928
155k
      n++;
1929
155k
    }
1930
75.2k
  }
1931
1932
112k
  list = erealloc(list, sizeof(void*) * (n+2));
1933
1934
112k
  list[n]   = item;
1935
112k
  list[n+1] = NULL;
1936
1937
112k
  *(void**)result = list;
1938
112k
}
1939
/* }}} */
1940
1941
static void zend_do_extended_stmt(znode* result) /* {{{ */
1942
1.29M
{
1943
1.29M
  zend_op *opline;
1944
1945
1.29M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) {
1946
1.29M
    return;
1947
1.29M
  }
1948
1949
0
  opline = get_next_op();
1950
1951
0
  opline->opcode = ZEND_EXT_STMT;
1952
0
  if (result) {
1953
0
    SET_NODE(opline->op1, result);
1954
0
  }
1955
0
}
1956
/* }}} */
1957
1958
static void zend_do_extended_fcall_begin(void) /* {{{ */
1959
2.92M
{
1960
2.92M
  zend_op *opline;
1961
1962
2.92M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1963
2.92M
    return;
1964
2.92M
  }
1965
1966
0
  opline = get_next_op();
1967
1968
0
  opline->opcode = ZEND_EXT_FCALL_BEGIN;
1969
0
}
1970
/* }}} */
1971
1972
static void zend_do_extended_fcall_end(void) /* {{{ */
1973
2.92M
{
1974
2.92M
  zend_op *opline;
1975
1976
2.92M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1977
2.92M
    return;
1978
2.92M
  }
1979
1980
0
  opline = get_next_op();
1981
1982
0
  opline->opcode = ZEND_EXT_FCALL_END;
1983
0
}
1984
/* }}} */
1985
1986
0
ZEND_API bool zend_is_auto_global_str(const char *name, size_t len) /* {{{ */ {
1987
0
  zend_auto_global *auto_global;
1988
1989
0
  if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) {
1990
0
    if (auto_global->armed) {
1991
0
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1992
0
    }
1993
0
    return 1;
1994
0
  }
1995
0
  return 0;
1996
0
}
1997
/* }}} */
1998
1999
ZEND_API bool zend_is_auto_global(zend_string *name) /* {{{ */
2000
2.22M
{
2001
2.22M
  zend_auto_global *auto_global;
2002
2003
2.22M
  if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) {
2004
5.43k
    if (auto_global->armed) {
2005
150
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
2006
150
    }
2007
5.43k
    return 1;
2008
5.43k
  }
2009
2.21M
  return 0;
2010
2.22M
}
2011
/* }}} */
2012
2013
ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
2014
128
{
2015
128
  zend_auto_global auto_global;
2016
128
  zend_result retval;
2017
2018
128
  auto_global.name = name;
2019
128
  auto_global.auto_global_callback = auto_global_callback;
2020
128
  auto_global.jit = jit;
2021
2022
128
  retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
2023
2024
128
  return retval;
2025
128
}
2026
/* }}} */
2027
2028
ZEND_API void zend_activate_auto_globals(void) /* {{{ */
2029
244k
{
2030
244k
  zend_auto_global *auto_global;
2031
2032
4.39M
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2033
4.39M
    auto_global->armed = auto_global->jit || auto_global->auto_global_callback;
2034
4.39M
  } ZEND_HASH_FOREACH_END();
2035
2036
4.39M
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2037
4.39M
    if (auto_global->armed && !auto_global->jit) {
2038
977k
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
2039
977k
    }
2040
4.39M
  } ZEND_HASH_FOREACH_END();
2041
244k
}
2042
/* }}} */
2043
2044
int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
2045
8.82M
{
2046
8.82M
  zval zv;
2047
8.82M
  int ret;
2048
2049
8.82M
  if (CG(increment_lineno)) {
2050
12.5k
    CG(zend_lineno)++;
2051
12.5k
    CG(increment_lineno) = 0;
2052
12.5k
  }
2053
2054
8.82M
  ret = lex_scan(&zv, elem);
2055
8.82M
  ZEND_ASSERT(!EG(exception) || ret == T_ERROR);
2056
8.82M
  return ret;
2057
2058
8.82M
}
2059
/* }}} */
2060
2061
ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers) /* {{{ */
2062
193k
{
2063
193k
  bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS;
2064
2065
193k
  ce->refcount = 1;
2066
193k
  ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
2067
193k
  ce->ce_flags2 = 0;
2068
2069
193k
  if (CG(compiler_options) & ZEND_COMPILE_GUARDS) {
2070
0
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2071
0
  }
2072
2073
193k
  ce->default_properties_table = NULL;
2074
193k
  ce->default_static_members_table = NULL;
2075
193k
  zend_hash_init(&ce->properties_info, 8, NULL, NULL, persistent_hashes);
2076
193k
  zend_hash_init(&ce->constants_table, 8, NULL, NULL, persistent_hashes);
2077
193k
  zend_hash_init(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes);
2078
2079
193k
  ce->doc_comment = NULL;
2080
2081
193k
  ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
2082
193k
  ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
2083
2084
193k
  ce->default_object_handlers = &std_object_handlers;
2085
193k
  ce->default_properties_count = 0;
2086
193k
  ce->default_static_members_count = 0;
2087
193k
  ce->properties_info_table = NULL;
2088
193k
  ce->attributes = NULL;
2089
193k
  ce->enum_backing_type = IS_UNDEF;
2090
193k
  ce->backed_enum_table = NULL;
2091
2092
193k
  if (nullify_handlers) {
2093
191k
    ce->constructor = NULL;
2094
191k
    ce->destructor = NULL;
2095
191k
    ce->clone = NULL;
2096
191k
    ce->__get = NULL;
2097
191k
    ce->__set = NULL;
2098
191k
    ce->__unset = NULL;
2099
191k
    ce->__isset = NULL;
2100
191k
    ce->__call = NULL;
2101
191k
    ce->__callstatic = NULL;
2102
191k
    ce->__tostring = NULL;
2103
191k
    ce->__serialize = NULL;
2104
191k
    ce->__unserialize = NULL;
2105
191k
    ce->__debugInfo = NULL;
2106
191k
    ce->create_object = NULL;
2107
191k
    ce->get_iterator = NULL;
2108
191k
    ce->iterator_funcs_ptr = NULL;
2109
191k
    ce->arrayaccess_funcs_ptr = NULL;
2110
191k
    ce->get_static_method = NULL;
2111
191k
    ce->parent = NULL;
2112
191k
    ce->parent_name = NULL;
2113
191k
    ce->num_interfaces = 0;
2114
191k
    ce->interfaces = NULL;
2115
191k
    ce->num_traits = 0;
2116
191k
    ce->num_hooked_props = 0;
2117
191k
    ce->num_hooked_prop_variance_checks = 0;
2118
191k
    ce->trait_names = NULL;
2119
191k
    ce->trait_aliases = NULL;
2120
191k
    ce->trait_precedences = NULL;
2121
191k
    ce->serialize = NULL;
2122
191k
    ce->unserialize = NULL;
2123
191k
    if (ce->type == ZEND_INTERNAL_CLASS) {
2124
0
      ce->info.internal.module = NULL;
2125
0
      ce->info.internal.builtin_functions = NULL;
2126
0
    }
2127
191k
  }
2128
193k
}
2129
/* }}} */
2130
2131
ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
2132
0
{
2133
0
  return op_array->vars[EX_VAR_TO_NUM(var)];
2134
0
}
2135
/* }}} */
2136
2137
zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
2138
0
{
2139
0
  zval *left_zv = zend_ast_get_zval(left_ast);
2140
0
  zend_string *left = Z_STR_P(left_zv);
2141
0
  zend_string *right = zend_ast_get_str(right_ast);
2142
2143
0
  zend_string *result;
2144
0
  size_t left_len = ZSTR_LEN(left);
2145
0
  size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
2146
2147
0
  result = zend_string_extend(left, len, 0);
2148
0
  ZSTR_VAL(result)[left_len] = '\\';
2149
0
  memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
2150
0
  ZSTR_VAL(result)[len] = '\0';
2151
0
  zend_string_release_ex(right, 0);
2152
2153
0
  ZVAL_STR(left_zv, result);
2154
0
  return left_ast;
2155
0
}
2156
/* }}} */
2157
2158
zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
2159
1.45k
{
2160
1.45k
  zval *zv = zend_ast_get_zval(ast);
2161
1.45k
  if (Z_TYPE_P(zv) == IS_LONG) {
2162
1.15k
    if (Z_LVAL_P(zv) == 0) {
2163
518
      ZVAL_NEW_STR(zv, ZSTR_INIT_LITERAL("-0", 0));
2164
633
    } else {
2165
633
      ZEND_ASSERT(Z_LVAL_P(zv) > 0);
2166
633
      Z_LVAL_P(zv) *= -1;
2167
633
    }
2168
1.15k
  } else if (Z_TYPE_P(zv) == IS_STRING) {
2169
305
    size_t orig_len = Z_STRLEN_P(zv);
2170
305
    Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0);
2171
305
    memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
2172
305
    Z_STRVAL_P(zv)[0] = '-';
2173
305
  } else {
2174
0
    ZEND_UNREACHABLE();
2175
0
  }
2176
1.45k
  return ast;
2177
1.45k
}
2178
/* }}} */
2179
2180
static void zend_verify_namespace(void) /* {{{ */
2181
423k
{
2182
423k
  if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
2183
37
    zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
2184
37
  }
2185
423k
}
2186
/* }}} */
2187
2188
/* {{{ zend_dirname
2189
   Returns directory name component of path */
2190
ZEND_API size_t zend_dirname(char *path, size_t len)
2191
1.58k
{
2192
1.58k
  char *end = path + len - 1;
2193
1.58k
  unsigned int len_adjust = 0;
2194
2195
#ifdef ZEND_WIN32
2196
  /* Note that on Win32 CWD is per drive (heritage from CP/M).
2197
   * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
2198
   */
2199
  if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
2200
    /* Skip over the drive spec (if any) so as not to change */
2201
    path += 2;
2202
    len_adjust += 2;
2203
    if (2 == len) {
2204
      /* Return "c:" on Win32 for dirname("c:").
2205
       * It would be more consistent to return "c:."
2206
       * but that would require making the string *longer*.
2207
       */
2208
      return len;
2209
    }
2210
  }
2211
#endif
2212
2213
1.58k
  if (len == 0) {
2214
    /* Illegal use of this function */
2215
0
    return 0;
2216
0
  }
2217
2218
  /* Strip trailing slashes */
2219
1.58k
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2220
0
    end--;
2221
0
  }
2222
1.58k
  if (end < path) {
2223
    /* The path only contained slashes */
2224
0
    path[0] = DEFAULT_SLASH;
2225
0
    path[1] = '\0';
2226
0
    return 1 + len_adjust;
2227
0
  }
2228
2229
  /* Strip filename */
2230
17.4k
  while (end >= path && !IS_SLASH_P_EX(end, end == path)) {
2231
15.8k
    end--;
2232
15.8k
  }
2233
1.58k
  if (end < path) {
2234
    /* No slash found, therefore return '.' */
2235
1.03k
    path[0] = '.';
2236
1.03k
    path[1] = '\0';
2237
1.03k
    return 1 + len_adjust;
2238
1.03k
  }
2239
2240
  /* Strip slashes which came before the file name */
2241
1.09k
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2242
546
    end--;
2243
546
  }
2244
546
  if (end < path) {
2245
5
    path[0] = DEFAULT_SLASH;
2246
5
    path[1] = '\0';
2247
5
    return 1 + len_adjust;
2248
5
  }
2249
541
  *(end+1) = '\0';
2250
2251
541
  return (size_t)(end + 1 - path) + len_adjust;
2252
546
}
2253
/* }}} */
2254
2255
static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */
2256
582k
{
2257
582k
  uint_fast8_t factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3;
2258
2259
582k
  switch (type) {
2260
326k
    case BP_VAR_R:
2261
326k
      opline->result_type = IS_TMP_VAR;
2262
326k
      result->op_type = IS_TMP_VAR;
2263
326k
      return;
2264
91.4k
    case BP_VAR_W:
2265
91.4k
      opline->opcode += 1 * factor;
2266
91.4k
      return;
2267
11.6k
    case BP_VAR_RW:
2268
11.6k
      opline->opcode += 2 * factor;
2269
11.6k
      return;
2270
61.1k
    case BP_VAR_IS:
2271
61.1k
      opline->result_type = IS_TMP_VAR;
2272
61.1k
      result->op_type = IS_TMP_VAR;
2273
61.1k
      opline->opcode += 3 * factor;
2274
61.1k
      return;
2275
87.3k
    case BP_VAR_FUNC_ARG:
2276
87.3k
      opline->opcode += 4 * factor;
2277
87.3k
      return;
2278
3.91k
    case BP_VAR_UNSET:
2279
3.91k
      opline->opcode += 5 * factor;
2280
3.91k
      return;
2281
582k
    EMPTY_SWITCH_DEFAULT_CASE()
2282
582k
  }
2283
582k
}
2284
/* }}} */
2285
2286
static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
2287
3.61M
{
2288
3.61M
  opline->result_type = IS_VAR;
2289
3.61M
  opline->result.var = get_temporary_variable();
2290
3.61M
  GET_NODE(result, opline->result);
2291
3.61M
}
2292
/* }}} */
2293
2294
static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
2295
23.6M
{
2296
23.6M
  opline->result_type = IS_TMP_VAR;
2297
23.6M
  opline->result.var = get_temporary_variable();
2298
23.6M
  GET_NODE(result, opline->result);
2299
23.6M
}
2300
/* }}} */
2301
2302
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2303
25.4M
{
2304
25.4M
  zend_op *opline = get_next_op();
2305
25.4M
  opline->opcode = opcode;
2306
2307
25.4M
  if (op1 != NULL) {
2308
20.2M
    SET_NODE(opline->op1, op1);
2309
20.2M
  }
2310
2311
25.4M
  if (op2 != NULL) {
2312
1.28M
    SET_NODE(opline->op2, op2);
2313
1.28M
  }
2314
2315
25.4M
  if (result) {
2316
3.16M
    zend_make_var_result(result, opline);
2317
3.16M
  }
2318
25.4M
  return opline;
2319
25.4M
}
2320
/* }}} */
2321
2322
static zend_op *zend_emit_op_tmp(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2323
25.3M
{
2324
25.3M
  zend_op *opline = get_next_op();
2325
25.3M
  opline->opcode = opcode;
2326
2327
25.3M
  if (op1 != NULL) {
2328
6.68M
    SET_NODE(opline->op1, op1);
2329
6.68M
  }
2330
2331
25.3M
  if (op2 != NULL) {
2332
3.02M
    SET_NODE(opline->op2, op2);
2333
3.02M
  }
2334
2335
25.3M
  if (result) {
2336
23.5M
    zend_make_tmp_result(result, opline);
2337
23.5M
  }
2338
2339
25.3M
  return opline;
2340
25.3M
}
2341
/* }}} */
2342
2343
static void zend_emit_tick(void) /* {{{ */
2344
34.3k
{
2345
34.3k
  zend_op *opline;
2346
2347
  /* This prevents a double TICK generated by the parser statement of "declare()" */
2348
34.3k
  if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
2349
2.34k
    return;
2350
2.34k
  }
2351
2352
32.0k
  opline = get_next_op();
2353
2354
32.0k
  opline->opcode = ZEND_TICKS;
2355
32.0k
  opline->extended_value = FC(declarables).ticks;
2356
32.0k
}
2357
/* }}} */
2358
2359
static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
2360
164k
{
2361
164k
  return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
2362
164k
}
2363
/* }}} */
2364
2365
static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
2366
675k
{
2367
675k
  uint32_t opnum = get_next_op_number();
2368
675k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
2369
675k
  opline->op1.opline_num = opnum_target;
2370
675k
  return opnum;
2371
675k
}
2372
/* }}} */
2373
2374
ZEND_API bool zend_is_smart_branch(const zend_op *opline) /* {{{ */
2375
170k
{
2376
170k
  switch (opline->opcode) {
2377
2.18k
    case ZEND_IS_IDENTICAL:
2378
2.50k
    case ZEND_IS_NOT_IDENTICAL:
2379
28.3k
    case ZEND_IS_EQUAL:
2380
29.1k
    case ZEND_IS_NOT_EQUAL:
2381
116k
    case ZEND_IS_SMALLER:
2382
117k
    case ZEND_IS_SMALLER_OR_EQUAL:
2383
121k
    case ZEND_CASE:
2384
123k
    case ZEND_CASE_STRICT:
2385
123k
    case ZEND_ISSET_ISEMPTY_CV:
2386
123k
    case ZEND_ISSET_ISEMPTY_VAR:
2387
123k
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2388
123k
    case ZEND_ISSET_ISEMPTY_PROP_OBJ:
2389
124k
    case ZEND_ISSET_ISEMPTY_STATIC_PROP:
2390
124k
    case ZEND_INSTANCEOF:
2391
124k
    case ZEND_TYPE_CHECK:
2392
124k
    case ZEND_DEFINED:
2393
124k
    case ZEND_IN_ARRAY:
2394
124k
    case ZEND_ARRAY_KEY_EXISTS:
2395
124k
      return 1;
2396
45.5k
    default:
2397
45.5k
      return 0;
2398
170k
  }
2399
170k
}
2400
/* }}} */
2401
2402
static inline uint32_t zend_emit_cond_jump(uint8_t opcode, znode *cond, uint32_t opnum_target) /* {{{ */
2403
234k
{
2404
234k
  uint32_t opnum = get_next_op_number();
2405
234k
  zend_op *opline;
2406
2407
234k
  if (cond->op_type == IS_TMP_VAR && opnum > 0) {
2408
143k
    opline = CG(active_op_array)->opcodes + opnum - 1;
2409
143k
    if (opline->result_type == IS_TMP_VAR
2410
142k
     && opline->result.var == cond->u.op.var
2411
142k
     && zend_is_smart_branch(opline)) {
2412
124k
      if (opcode == ZEND_JMPZ) {
2413
98.8k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ;
2414
98.8k
      } else {
2415
25.8k
        ZEND_ASSERT(opcode == ZEND_JMPNZ);
2416
25.8k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ;
2417
25.8k
      }
2418
124k
    }
2419
143k
  }
2420
234k
  opline = zend_emit_op(NULL, opcode, cond, NULL);
2421
234k
  opline->op2.opline_num = opnum_target;
2422
234k
  return opnum;
2423
234k
}
2424
/* }}} */
2425
2426
static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
2427
913k
{
2428
913k
  zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
2429
913k
  switch (opline->opcode) {
2430
659k
    case ZEND_JMP:
2431
659k
      opline->op1.opline_num = opnum_target;
2432
659k
      break;
2433
198k
    case ZEND_JMPZ:
2434
220k
    case ZEND_JMPNZ:
2435
225k
    case ZEND_JMPZ_EX:
2436
229k
    case ZEND_JMPNZ_EX:
2437
231k
    case ZEND_JMP_SET:
2438
254k
    case ZEND_COALESCE:
2439
254k
    case ZEND_JMP_NULL:
2440
254k
    case ZEND_BIND_INIT_STATIC_OR_JMP:
2441
254k
    case ZEND_JMP_FRAMELESS:
2442
254k
      opline->op2.opline_num = opnum_target;
2443
254k
      break;
2444
913k
    EMPTY_SWITCH_DEFAULT_CASE()
2445
913k
  }
2446
913k
}
2447
/* }}} */
2448
2449
static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
2450
911k
{
2451
911k
  zend_update_jump_target(opnum_jump, get_next_op_number());
2452
911k
}
2453
/* }}} */
2454
2455
static inline zend_op *zend_delayed_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2456
450k
{
2457
450k
  zend_op tmp_opline;
2458
2459
450k
  init_op(&tmp_opline);
2460
2461
450k
  tmp_opline.opcode = opcode;
2462
450k
  if (op1 != NULL) {
2463
450k
    SET_NODE(tmp_opline.op1, op1);
2464
450k
  }
2465
450k
  if (op2 != NULL) {
2466
435k
    SET_NODE(tmp_opline.op2, op2);
2467
435k
  }
2468
450k
  if (result) {
2469
446k
    zend_make_var_result(result, &tmp_opline);
2470
446k
  }
2471
2472
450k
  zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
2473
450k
  return zend_stack_top(&CG(delayed_oplines_stack));
2474
450k
}
2475
/* }}} */
2476
2477
static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
2478
702k
{
2479
702k
  return zend_stack_count(&CG(delayed_oplines_stack));
2480
702k
}
2481
/* }}} */
2482
2483
static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
2484
701k
{
2485
701k
  zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
2486
701k
  uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
2487
2488
701k
  ZEND_ASSERT(count >= offset);
2489
1.15M
  for (i = offset; i < count; ++i) {
2490
449k
    if (EXPECTED(oplines[i].opcode != ZEND_NOP)) {
2491
440k
      opline = get_next_op();
2492
440k
      memcpy(opline, &oplines[i], sizeof(zend_op));
2493
440k
    } else {
2494
8.92k
      opline = CG(active_op_array)->opcodes + oplines[i].extended_value;
2495
8.92k
    }
2496
449k
  }
2497
2498
701k
  CG(delayed_oplines_stack).top = offset;
2499
701k
  return opline;
2500
701k
}
2501
/* }}} */
2502
2503
static bool zend_ast_kind_is_short_circuited(zend_ast_kind ast_kind)
2504
37.6M
{
2505
37.6M
  switch (ast_kind) {
2506
341k
    case ZEND_AST_DIM:
2507
416k
    case ZEND_AST_PROP:
2508
535k
    case ZEND_AST_NULLSAFE_PROP:
2509
553k
    case ZEND_AST_STATIC_PROP:
2510
692k
    case ZEND_AST_METHOD_CALL:
2511
697k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2512
766k
    case ZEND_AST_STATIC_CALL:
2513
766k
      return 1;
2514
36.8M
    default:
2515
36.8M
      return 0;
2516
37.6M
  }
2517
37.6M
}
2518
2519
static bool zend_ast_is_short_circuited(const zend_ast *ast)
2520
901k
{
2521
901k
  switch (ast->kind) {
2522
161k
    case ZEND_AST_DIM:
2523
190k
    case ZEND_AST_PROP:
2524
200k
    case ZEND_AST_STATIC_PROP:
2525
213k
    case ZEND_AST_METHOD_CALL:
2526
216k
    case ZEND_AST_STATIC_CALL:
2527
216k
      return zend_ast_is_short_circuited(ast->child[0]);
2528
582
    case ZEND_AST_NULLSAFE_PROP:
2529
661
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2530
661
      return 1;
2531
684k
    default:
2532
684k
      return 0;
2533
901k
  }
2534
901k
}
2535
2536
static void zend_assert_not_short_circuited(const zend_ast *ast)
2537
10.4k
{
2538
10.4k
  if (zend_ast_is_short_circuited(ast)) {
2539
42
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain");
2540
42
  }
2541
10.4k
}
2542
2543
/* Mark nodes that are an inner part of a short-circuiting chain.
2544
 * We should not perform a "commit" on them, as it will be performed by the outer-most node.
2545
 * We do this to avoid passing down an argument in various compile functions. */
2546
2547
780k
#define ZEND_SHORT_CIRCUITING_INNER 0x8000
2548
2549
535k
static void zend_short_circuiting_mark_inner(zend_ast *ast) {
2550
535k
  if (zend_ast_kind_is_short_circuited(ast->kind)) {
2551
227k
    ast->attr |= ZEND_SHORT_CIRCUITING_INNER;
2552
227k
  }
2553
535k
}
2554
2555
static uint32_t zend_short_circuiting_checkpoint(void)
2556
37.1M
{
2557
37.1M
  return zend_stack_count(&CG(short_circuiting_opnums));
2558
37.1M
}
2559
2560
static void zend_short_circuiting_commit(uint32_t checkpoint, znode *result, const zend_ast *ast)
2561
37.0M
{
2562
37.0M
  bool is_short_circuited = zend_ast_kind_is_short_circuited(ast->kind)
2563
36.5M
    || ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY;
2564
37.0M
  if (!is_short_circuited) {
2565
36.5M
    ZEND_ASSERT(zend_stack_count(&CG(short_circuiting_opnums)) == checkpoint
2566
36.5M
      && "Short circuiting stack should be empty");
2567
36.5M
    return;
2568
36.5M
  }
2569
2570
553k
  if (ast->attr & ZEND_SHORT_CIRCUITING_INNER) {
2571
    /* Outer-most node will commit. */
2572
66.6k
    return;
2573
66.6k
  }
2574
2575
550k
  while (zend_stack_count(&CG(short_circuiting_opnums)) != checkpoint) {
2576
63.5k
    uint32_t opnum = *(uint32_t *) zend_stack_top(&CG(short_circuiting_opnums));
2577
63.5k
    zend_op *opline = &CG(active_op_array)->opcodes[opnum];
2578
63.5k
    opline->op2.opline_num = get_next_op_number();
2579
63.5k
    SET_NODE(opline->result, result);
2580
63.5k
    opline->extended_value |=
2581
63.5k
      ast->kind == ZEND_AST_ISSET ? ZEND_SHORT_CIRCUITING_CHAIN_ISSET :
2582
63.5k
      ast->kind == ZEND_AST_EMPTY ? ZEND_SHORT_CIRCUITING_CHAIN_EMPTY :
2583
63.3k
                                    ZEND_SHORT_CIRCUITING_CHAIN_EXPR;
2584
63.5k
    zend_stack_del_top(&CG(short_circuiting_opnums));
2585
63.5k
  }
2586
486k
}
2587
2588
static void zend_emit_jmp_null(znode *obj_node, uint32_t bp_type)
2589
63.5k
{
2590
63.5k
  uint32_t jmp_null_opnum = get_next_op_number();
2591
63.5k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP_NULL, obj_node, NULL);
2592
63.5k
  if (opline->op1_type == IS_CONST) {
2593
801
    Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
2594
801
  }
2595
63.5k
  if (bp_type == BP_VAR_IS) {
2596
855
    opline->extended_value |= ZEND_JMP_NULL_BP_VAR_IS;
2597
855
  }
2598
63.5k
  zend_stack_push(&CG(short_circuiting_opnums), &jmp_null_opnum);
2599
63.5k
}
2600
2601
static inline bool zend_is_variable_or_call(const zend_ast *ast);
2602
2603
static void zend_compile_memoized_expr(znode *result, zend_ast *expr, uint32_t type) /* {{{ */
2604
136k
{
2605
136k
  const zend_memoize_mode memoize_mode = CG(memoize_mode);
2606
136k
  if (memoize_mode == ZEND_MEMOIZE_COMPILE) {
2607
68.8k
    znode memoized_result;
2608
2609
    /* Go through normal compilation */
2610
68.8k
    CG(memoize_mode) = ZEND_MEMOIZE_NONE;
2611
68.8k
    if (zend_is_variable_or_call(expr)) {
2612
16.8k
      zend_compile_var(result, expr, type, /* by_ref */ false);
2613
52.0k
    } else {
2614
52.0k
      zend_compile_expr(result, expr);
2615
52.0k
    }
2616
68.8k
    CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
2617
2618
68.8k
    if (result->op_type == IS_VAR) {
2619
14.4k
      zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL);
2620
54.4k
    } else if (result->op_type == IS_TMP_VAR) {
2621
48.1k
      zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL);
2622
48.1k
    } else {
2623
6.29k
      if (result->op_type == IS_CONST) {
2624
5.32k
        Z_TRY_ADDREF(result->u.constant);
2625
5.32k
      }
2626
6.29k
      memoized_result = *result;
2627
6.29k
    }
2628
2629
68.8k
    zend_hash_index_update_mem(
2630
68.8k
      CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode));
2631
68.8k
  } else if (memoize_mode == ZEND_MEMOIZE_FETCH) {
2632
67.1k
    const znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr);
2633
67.1k
    *result = *memoized_result;
2634
67.1k
    if (result->op_type == IS_CONST) {
2635
5.04k
      Z_TRY_ADDREF(result->u.constant);
2636
5.04k
    }
2637
67.1k
  } else {
2638
0
    ZEND_UNREACHABLE();
2639
0
  }
2640
136k
}
2641
/* }}} */
2642
2643
static void zend_emit_return_type_check(
2644
    znode *expr, const zend_arg_info *return_info, bool implicit) /* {{{ */
2645
21.3k
{
2646
21.3k
  zend_type type = return_info->type;
2647
21.3k
  if (ZEND_TYPE_IS_SET(type)) {
2648
21.3k
    zend_op *opline;
2649
2650
    /* `return ...;` is illegal in a void function (but `return;` isn't) */
2651
21.3k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) {
2652
3.85k
      if (expr) {
2653
17
        if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
2654
7
          zend_error_noreturn(E_COMPILE_ERROR,
2655
7
            "A void %s must not return a value "
2656
7
            "(did you mean \"return;\" instead of \"return null;\"?)",
2657
7
            CG(active_class_entry) != NULL ? "method" : "function");
2658
10
        } else {
2659
10
          zend_error_noreturn(E_COMPILE_ERROR, "A void %s must not return a value",
2660
10
          CG(active_class_entry) != NULL ? "method" : "function");
2661
10
        }
2662
17
      }
2663
      /* we don't need run-time check */
2664
3.83k
      return;
2665
3.85k
    }
2666
2667
    /* `return` is illegal in a never-returning function */
2668
17.5k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_NEVER)) {
2669
      /* Implicit case handled separately using VERIFY_NEVER_TYPE opcode. */
2670
9
      ZEND_ASSERT(!implicit);
2671
9
      zend_error_noreturn(E_COMPILE_ERROR, "A never-returning %s must not return",
2672
9
        CG(active_class_entry) != NULL ? "method" : "function");
2673
9
    }
2674
2675
17.5k
    if (!expr && !implicit) {
2676
14
      if (ZEND_TYPE_ALLOW_NULL(type)) {
2677
8
        zend_error_noreturn(E_COMPILE_ERROR,
2678
8
          "A %s with return type must return a value "
2679
8
          "(did you mean \"return null;\" instead of \"return;\"?)",
2680
8
          CG(active_class_entry) != NULL ? "method" : "function");
2681
8
      } else {
2682
6
        zend_error_noreturn(E_COMPILE_ERROR,
2683
6
          "A %s with return type must return a value",
2684
6
          CG(active_class_entry) != NULL ? "method" : "function");
2685
6
      }
2686
14
    }
2687
2688
17.5k
    if (expr && ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) {
2689
      /* we don't need run-time check for mixed return type */
2690
523
      return;
2691
523
    }
2692
2693
16.9k
    if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) {
2694
      /* we don't need run-time check */
2695
1.42k
      return;
2696
1.42k
    }
2697
2698
15.5k
    opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
2699
15.5k
    if (expr && expr->op_type == IS_CONST) {
2700
194
      opline->result_type = expr->op_type = IS_TMP_VAR;
2701
194
      opline->result.var = expr->u.op.var = get_temporary_variable();
2702
194
    }
2703
15.5k
  }
2704
21.3k
}
2705
/* }}} */
2706
2707
void zend_emit_final_return(bool return_one) /* {{{ */
2708
1.20M
{
2709
1.20M
  znode zn;
2710
1.20M
  zend_op *ret;
2711
1.20M
  bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
2712
2713
1.20M
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)
2714
16.9k
      && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) {
2715
16.6k
    zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
2716
2717
16.6k
    if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
2718
332
      zend_emit_op(NULL, ZEND_VERIFY_NEVER_TYPE, NULL, NULL);
2719
332
      return;
2720
332
    }
2721
2722
16.3k
    zend_emit_return_type_check(NULL, return_info, true);
2723
16.3k
  }
2724
2725
1.20M
  zn.op_type = IS_CONST;
2726
1.20M
  if (return_one) {
2727
69.2k
    ZVAL_LONG(&zn.u.constant, 1);
2728
1.13M
  } else {
2729
1.13M
    ZVAL_NULL(&zn.u.constant);
2730
1.13M
  }
2731
2732
1.20M
  ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
2733
1.20M
  ret->extended_value = -1;
2734
1.20M
}
2735
/* }}} */
2736
2737
static inline bool zend_is_variable(const zend_ast *ast) /* {{{ */
2738
3.93M
{
2739
3.93M
  return ast->kind == ZEND_AST_VAR
2740
3.80M
    || ast->kind == ZEND_AST_DIM
2741
3.74M
    || ast->kind == ZEND_AST_PROP
2742
3.73M
    || ast->kind == ZEND_AST_NULLSAFE_PROP
2743
3.73M
    || ast->kind == ZEND_AST_STATIC_PROP;
2744
3.93M
}
2745
/* }}} */
2746
2747
static bool zend_propagate_list_refs(zend_ast *ast);
2748
2749
static inline bool zend_is_passable_by_ref(const zend_ast *ast)
2750
3.79M
{
2751
3.79M
  if (zend_is_variable(ast) || ast->kind == ZEND_AST_ASSIGN_REF) {
2752
136k
    return true;
2753
136k
  }
2754
3.65M
  if (ast->kind == ZEND_AST_ASSIGN
2755
3.96k
   && UNEXPECTED(ast->child[0]->kind == ZEND_AST_ARRAY)
2756
26
   && zend_propagate_list_refs(ast->child[0])) {
2757
11
    return true;
2758
11
  }
2759
3.65M
  return false;
2760
3.65M
}
2761
2762
static inline bool zend_is_call(const zend_ast *ast) /* {{{ */
2763
4.08M
{
2764
4.08M
  return ast->kind == ZEND_AST_CALL
2765
3.97M
    || ast->kind == ZEND_AST_METHOD_CALL
2766
3.94M
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
2767
3.94M
    || ast->kind == ZEND_AST_STATIC_CALL
2768
3.93M
    || ast->kind == ZEND_AST_PIPE;
2769
4.08M
}
2770
/* }}} */
2771
2772
static inline bool zend_is_variable_or_call(const zend_ast *ast) /* {{{ */
2773
94.0k
{
2774
94.0k
  return zend_is_variable(ast) || zend_is_call(ast);
2775
94.0k
}
2776
/* }}} */
2777
2778
static inline bool zend_is_unticked_stmt(const zend_ast *ast) /* {{{ */
2779
49.6k
{
2780
49.6k
  return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL
2781
36.7k
    || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP
2782
36.0k
    || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD;
2783
49.6k
}
2784
/* }}} */
2785
2786
static inline bool zend_can_write_to_variable(const zend_ast *ast) /* {{{ */
2787
19.9k
{
2788
19.9k
  while (
2789
22.8k
    ast->kind == ZEND_AST_DIM
2790
21.6k
    || ast->kind == ZEND_AST_PROP
2791
19.9k
  ) {
2792
2.95k
    ast = ast->child[0];
2793
2.95k
  }
2794
2795
19.9k
  return zend_is_variable_or_call(ast) && !zend_ast_is_short_circuited(ast);
2796
19.9k
}
2797
/* }}} */
2798
2799
static inline bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
2800
38.6k
{
2801
38.6k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2802
0
    return 0;
2803
0
  }
2804
2805
38.6k
  return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
2806
38.6k
}
2807
/* }}} */
2808
2809
static inline void zend_handle_numeric_op(znode *node) /* {{{ */
2810
4.79k
{
2811
4.79k
  if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
2812
2.31k
    zend_ulong index;
2813
2814
2.31k
    if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
2815
225
      zval_ptr_dtor(&node->u.constant);
2816
225
      ZVAL_LONG(&node->u.constant, index);
2817
225
    }
2818
2.31k
  }
2819
4.79k
}
2820
/* }}} */
2821
2822
static inline void zend_handle_numeric_dim(const zend_op *opline, znode *dim_node) /* {{{ */
2823
193k
{
2824
193k
  if (Z_TYPE(dim_node->u.constant) == IS_STRING) {
2825
169k
    zend_ulong index;
2826
2827
169k
    if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) {
2828
      /* For numeric indexes we also keep the original value to use by ArrayAccess
2829
       * See bug #63217
2830
       */
2831
52.2k
      int c = zend_add_literal(&dim_node->u.constant);
2832
52.2k
      ZEND_ASSERT(opline->op2.constant + 1 == c);
2833
52.2k
      ZVAL_LONG(CT_CONSTANT(opline->op2), index);
2834
52.2k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE;
2835
52.2k
      return;
2836
52.2k
    }
2837
169k
  }
2838
193k
}
2839
/* }}} */
2840
2841
static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
2842
197k
{
2843
197k
  if (class_node->op_type == IS_CONST) {
2844
128k
    opline->op1_type = IS_CONST;
2845
128k
    opline->op1.constant = zend_add_class_name_literal(
2846
128k
      Z_STR(class_node->u.constant));
2847
128k
  } else {
2848
69.1k
    SET_NODE(opline->op1, class_node);
2849
69.1k
  }
2850
197k
}
2851
/* }}} */
2852
2853
static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */
2854
212k
{
2855
212k
  uint32_t fetch_type;
2856
2857
212k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2858
57.7k
    znode name_node;
2859
2860
57.7k
    zend_compile_expr(&name_node, name_ast);
2861
2862
57.7k
    if (name_node.op_type == IS_CONST) {
2863
590
      zend_string *name;
2864
2865
590
      if (Z_TYPE(name_node.u.constant) != IS_STRING) {
2866
6
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
2867
6
      }
2868
2869
584
      name = Z_STR(name_node.u.constant);
2870
584
      fetch_type = zend_get_class_fetch_type(name);
2871
2872
584
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
2873
426
        result->op_type = IS_CONST;
2874
426
        ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ));
2875
426
      } else {
2876
158
        zend_ensure_valid_class_fetch_type(fetch_type);
2877
158
        result->op_type = IS_UNUSED;
2878
158
        result->u.op.num = fetch_type | fetch_flags;
2879
158
      }
2880
2881
584
      zend_string_release_ex(name, 0);
2882
57.2k
    } else {
2883
57.2k
      zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
2884
57.2k
      opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags;
2885
57.2k
    }
2886
57.7k
    return;
2887
57.7k
  }
2888
2889
  /* Fully qualified names are always default refs */
2890
154k
  if (name_ast->attr == ZEND_NAME_FQ) {
2891
4.82k
    result->op_type = IS_CONST;
2892
4.82k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2893
4.82k
    return;
2894
4.82k
  }
2895
2896
149k
  fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
2897
149k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
2898
133k
    result->op_type = IS_CONST;
2899
133k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2900
133k
  } else {
2901
16.1k
    zend_ensure_valid_class_fetch_type(fetch_type);
2902
16.1k
    result->op_type = IS_UNUSED;
2903
16.1k
    result->u.op.num = fetch_type | fetch_flags;
2904
16.1k
  }
2905
149k
}
2906
/* }}} */
2907
2908
static zend_result zend_try_compile_cv(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
2909
1.15M
{
2910
1.15M
  zend_ast *name_ast = ast->child[0];
2911
1.15M
  if (name_ast->kind == ZEND_AST_ZVAL) {
2912
1.09M
    zval *zv = zend_ast_get_zval(name_ast);
2913
1.09M
    zend_string *name;
2914
2915
1.09M
    if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
2916
1.04M
      name = zval_make_interned_string(zv);
2917
1.04M
    } else {
2918
47.3k
      name = zend_new_interned_string(zval_get_string_func(zv));
2919
47.3k
    }
2920
2921
1.09M
    if (zend_is_auto_global(name)) {
2922
613
      return FAILURE;
2923
613
    }
2924
2925
1.09M
    if (!CG(context).has_assigned_to_http_response_header && zend_string_equals_literal(name, "http_response_header")) {
2926
226
      if (type == BP_VAR_R) {
2927
146
        zend_error(E_DEPRECATED,
2928
146
          "The predefined locally scoped $http_response_header variable is deprecated,"
2929
146
          " call http_get_last_response_headers() instead");
2930
146
      } else if (type == BP_VAR_W) {
2931
11
        CG(context).has_assigned_to_http_response_header = true;
2932
11
      }
2933
226
    }
2934
2935
1.09M
    result->op_type = IS_CV;
2936
1.09M
    result->u.op.var = lookup_cv(name);
2937
2938
1.09M
    if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) {
2939
47.3k
      zend_string_release_ex(name, 0);
2940
47.3k
    }
2941
2942
1.09M
    return SUCCESS;
2943
1.09M
  }
2944
2945
60.7k
  return FAILURE;
2946
1.15M
}
2947
/* }}} */
2948
2949
static zend_op *zend_compile_simple_var_no_cv(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2950
130k
{
2951
130k
  zend_ast *name_ast = ast->child[0];
2952
130k
  znode name_node;
2953
130k
  zend_op *opline;
2954
2955
130k
  zend_compile_expr(&name_node, name_ast);
2956
130k
  if (name_node.op_type == IS_CONST) {
2957
70.6k
    convert_to_string(&name_node.u.constant);
2958
70.6k
  }
2959
2960
130k
  if (delayed) {
2961
7.36k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2962
122k
  } else {
2963
122k
    opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2964
122k
  }
2965
2966
130k
  if (name_node.op_type == IS_CONST &&
2967
70.6k
      zend_is_auto_global(Z_STR(name_node.u.constant))) {
2968
2969
595
    opline->extended_value = ZEND_FETCH_GLOBAL;
2970
129k
  } else {
2971
    // TODO: Have a test case for this?
2972
129k
    if (name_node.op_type == IS_CONST
2973
70.0k
      && type == BP_VAR_R
2974
69.7k
      && zend_string_equals_literal(Z_STR(name_node.u.constant), "http_response_header")) {
2975
127
      zend_error(E_DEPRECATED,
2976
127
        "The predefined locally scoped $http_response_header variable is deprecated,"
2977
127
        " call http_get_last_response_headers() instead");
2978
127
    }
2979
129k
    opline->extended_value = ZEND_FETCH_LOCAL;
2980
129k
  }
2981
2982
130k
  zend_adjust_for_fetch_type(opline, result, type);
2983
130k
  return opline;
2984
130k
}
2985
/* }}} */
2986
2987
static bool is_this_fetch(const zend_ast *ast) /* {{{ */
2988
1.74M
{
2989
1.74M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2990
1.55M
    const zval *name = zend_ast_get_zval(ast->child[0]);
2991
1.55M
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals(Z_STR_P(name), ZSTR_KNOWN(ZEND_STR_THIS));
2992
1.55M
  }
2993
2994
184k
  return 0;
2995
1.74M
}
2996
/* }}} */
2997
2998
static bool is_globals_fetch(const zend_ast *ast)
2999
5.93M
{
3000
5.93M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
3001
1.69M
    const zval *name = zend_ast_get_zval(ast->child[0]);
3002
1.69M
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "GLOBALS");
3003
1.69M
  }
3004
3005
4.23M
  return 0;
3006
5.93M
}
3007
3008
static bool is_global_var_fetch(const zend_ast *ast)
3009
509k
{
3010
509k
  return ast->kind == ZEND_AST_DIM && is_globals_fetch(ast->child[0]);
3011
509k
}
3012
3013
static bool this_guaranteed_exists(void) /* {{{ */
3014
14.5k
{
3015
14.5k
  const zend_oparray_context *ctx = &CG(context);
3016
37.9k
  while (ctx) {
3017
    /* Instance methods always have a $this.
3018
     * This also includes closures that have a scope and use $this. */
3019
37.9k
    const zend_op_array *op_array = ctx->op_array;
3020
37.9k
    if (op_array->fn_flags & ZEND_ACC_STATIC) {
3021
42
      return false;
3022
37.8k
    } else if (op_array->scope) {
3023
10.6k
      return true;
3024
27.1k
    } else if (!(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
3025
3.82k
      return false;
3026
3.82k
    }
3027
23.3k
    ctx = ctx->prev;
3028
23.3k
  }
3029
0
  return false;
3030
14.5k
}
3031
/* }}} */
3032
3033
static zend_op *zend_compile_simple_var(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
3034
1.12M
{
3035
1.12M
  if (is_this_fetch(ast)) {
3036
4.20k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL);
3037
4.20k
    if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
3038
4.09k
      opline->result_type = IS_TMP_VAR;
3039
4.09k
      result->op_type = IS_TMP_VAR;
3040
4.09k
    }
3041
4.20k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3042
4.20k
    return opline;
3043
1.12M
  } else if (is_globals_fetch(ast)) {
3044
3.18k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_GLOBALS, NULL, NULL);
3045
3.18k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3046
3.02k
      opline->result_type = IS_TMP_VAR;
3047
3.02k
      result->op_type = IS_TMP_VAR;
3048
3.02k
    }
3049
3.18k
    return opline;
3050
1.11M
  } else if (zend_try_compile_cv(result, ast, type) == FAILURE) {
3051
59.8k
    return zend_compile_simple_var_no_cv(result, ast, type, delayed);
3052
59.8k
  }
3053
1.05M
  return NULL;
3054
1.12M
}
3055
/* }}} */
3056
3057
static void zend_separate_if_call_and_write(znode *node, const zend_ast *ast, uint32_t type) /* {{{ */
3058
423k
{
3059
423k
  if (type != BP_VAR_R
3060
230k
   && type != BP_VAR_IS
3061
   /* Whether a FUNC_ARG is R may only be determined at runtime. */
3062
173k
   && type != BP_VAR_FUNC_ARG
3063
87.1k
   && zend_is_call(ast)) {
3064
15.4k
    if (node->op_type == IS_VAR) {
3065
15.4k
      zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
3066
15.4k
      opline->result_type = IS_VAR;
3067
15.4k
      opline->result.var = opline->op1.var;
3068
15.4k
    } else {
3069
16
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3070
16
    }
3071
15.4k
  }
3072
423k
}
3073
/* }}} */
3074
3075
static inline void zend_emit_assign_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3076
5.02k
{
3077
5.02k
  znode dummy_node;
3078
5.02k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
3079
5.02k
    zend_ast_create_znode(value_node));
3080
5.02k
  zend_compile_expr(&dummy_node, assign_ast);
3081
5.02k
  zend_do_free(&dummy_node);
3082
5.02k
}
3083
/* }}} */
3084
3085
static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
3086
311k
{
3087
311k
  zend_ast *var_ast = ast->child[0];
3088
311k
  zend_ast *dim_ast = ast->child[1];
3089
311k
  zend_op *opline;
3090
3091
311k
  znode var_node, dim_node;
3092
3093
311k
  if (is_globals_fetch(var_ast)) {
3094
1.77k
    if (dim_ast == NULL) {
3095
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot append to $GLOBALS");
3096
7
    }
3097
3098
1.76k
    zend_compile_expr(&dim_node, dim_ast);
3099
1.76k
    if (dim_node.op_type == IS_CONST) {
3100
1.45k
      convert_to_string(&dim_node.u.constant);
3101
1.45k
    }
3102
3103
1.76k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &dim_node, NULL);
3104
1.76k
    opline->extended_value = ZEND_FETCH_GLOBAL;
3105
1.76k
    zend_adjust_for_fetch_type(opline, result, type);
3106
1.76k
    return opline;
3107
309k
  } else {
3108
309k
    zend_short_circuiting_mark_inner(var_ast);
3109
309k
    opline = zend_delayed_compile_var(&var_node, var_ast, type, false);
3110
309k
    if (opline) {
3111
164k
      if (type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) {
3112
2.71k
        opline->extended_value |= ZEND_FETCH_DIM_WRITE;
3113
161k
      } else if (opline->opcode == ZEND_FETCH_DIM_W
3114
132k
          || opline->opcode == ZEND_FETCH_DIM_RW
3115
130k
          || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3116
90.1k
          || opline->opcode == ZEND_FETCH_DIM_UNSET) {
3117
71.4k
        opline->extended_value = ZEND_FETCH_DIM_DIM;
3118
71.4k
      }
3119
164k
    }
3120
309k
  }
3121
3122
309k
  zend_separate_if_call_and_write(&var_node, var_ast, type);
3123
3124
309k
  if (dim_ast == NULL) {
3125
10.8k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3126
186
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
3127
186
    }
3128
10.6k
    if (type == BP_VAR_UNSET) {
3129
8
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
3130
8
    }
3131
10.6k
    dim_node.op_type = IS_UNUSED;
3132
298k
  } else {
3133
298k
    zend_compile_expr(&dim_node, dim_ast);
3134
298k
  }
3135
3136
309k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
3137
309k
  zend_adjust_for_fetch_type(opline, result, type);
3138
309k
  if (by_ref) {
3139
45.3k
    opline->extended_value = ZEND_FETCH_DIM_REF;
3140
45.3k
  }
3141
3142
309k
  if (dim_node.op_type == IS_CONST) {
3143
189k
    zend_handle_numeric_dim(opline, &dim_node);
3144
189k
  }
3145
309k
  return opline;
3146
309k
}
3147
3148
static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3149
138k
{
3150
138k
  uint32_t offset = zend_delayed_compile_begin();
3151
138k
  zend_delayed_compile_dim(result, ast, type, by_ref);
3152
138k
  return zend_delayed_compile_end(offset);
3153
138k
}
3154
/* }}} */
3155
3156
static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3157
126k
{
3158
126k
  zend_ast *obj_ast = ast->child[0];
3159
126k
  zend_ast *prop_ast = ast->child[1];
3160
3161
126k
  znode obj_node, prop_node;
3162
126k
  zend_op *opline;
3163
126k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_PROP;
3164
3165
126k
  if (is_this_fetch(obj_ast)) {
3166
13.1k
    if (this_guaranteed_exists()) {
3167
9.37k
      obj_node.op_type = IS_UNUSED;
3168
9.37k
    } else {
3169
3.76k
      opline = zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
3170
3.76k
      if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
3171
3.71k
        opline->result_type = IS_TMP_VAR;
3172
3.71k
        obj_node.op_type = IS_TMP_VAR;
3173
3.71k
      }
3174
3.76k
    }
3175
13.1k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3176
3177
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
3178
     * check for a nullsafe access. */
3179
112k
  } else {
3180
112k
    zend_short_circuiting_mark_inner(obj_ast);
3181
112k
    opline = zend_delayed_compile_var(&obj_node, obj_ast, type, false);
3182
112k
    if (opline && (opline->opcode == ZEND_FETCH_DIM_W
3183
19.4k
        || opline->opcode == ZEND_FETCH_DIM_RW
3184
19.1k
        || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3185
19.0k
        || opline->opcode == ZEND_FETCH_DIM_UNSET)) {
3186
1.39k
      opline->extended_value = ZEND_FETCH_DIM_OBJ;
3187
1.39k
    }
3188
3189
112k
    zend_separate_if_call_and_write(&obj_node, obj_ast, type);
3190
112k
    if (nullsafe) {
3191
61.1k
      if (obj_node.op_type == IS_TMP_VAR) {
3192
        /* Flush delayed oplines */
3193
26.3k
        zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
3194
26.3k
        uint32_t var = obj_node.u.op.var;
3195
26.3k
        uint32_t count = zend_stack_count(&CG(delayed_oplines_stack));
3196
26.3k
        uint32_t i = count;
3197
3198
64.5k
        while (i > 0 && oplines[i-1].result_type == IS_TMP_VAR && oplines[i-1].result.var == var) {
3199
39.4k
          i--;
3200
39.4k
          if (oplines[i].op1_type == IS_TMP_VAR) {
3201
38.2k
            var = oplines[i].op1.var;
3202
38.2k
          } else {
3203
1.25k
            break;
3204
1.25k
          }
3205
39.4k
        }
3206
65.7k
        for (; i < count; ++i) {
3207
39.4k
          if (oplines[i].opcode != ZEND_NOP) {
3208
8.92k
            opline = get_next_op();
3209
8.92k
            memcpy(opline, &oplines[i], sizeof(zend_op));
3210
8.92k
            oplines[i].opcode = ZEND_NOP;
3211
8.92k
            oplines[i].extended_value = opline - CG(active_op_array)->opcodes;
3212
8.92k
          }
3213
39.4k
        }
3214
26.3k
      }
3215
61.1k
      zend_emit_jmp_null(&obj_node, type);
3216
61.1k
    }
3217
112k
  }
3218
3219
126k
  zend_compile_expr(&prop_node, prop_ast);
3220
3221
126k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
3222
126k
  if (opline->op2_type == IS_CONST) {
3223
123k
    convert_to_string(CT_CONSTANT(opline->op2));
3224
123k
    zend_string_hash_val(Z_STR_P(CT_CONSTANT(opline->op2)));
3225
123k
    opline->extended_value = zend_alloc_cache_slots(3);
3226
123k
  }
3227
3228
126k
  zend_adjust_for_fetch_type(opline, result, type);
3229
3230
126k
  return opline;
3231
126k
}
3232
/* }}} */
3233
3234
static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3235
91.7k
{
3236
91.7k
  uint32_t offset = zend_delayed_compile_begin();
3237
91.7k
  zend_op *opline = zend_delayed_compile_prop(result, ast, type);
3238
91.7k
  if (by_ref) { /* shared with cache_slot */
3239
2.89k
    opline->extended_value |= ZEND_FETCH_REF;
3240
2.89k
  }
3241
91.7k
  return zend_delayed_compile_end(offset);
3242
91.7k
}
3243
/* }}} */
3244
3245
static zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref, bool delayed) /* {{{ */
3246
15.2k
{
3247
15.2k
  zend_ast *class_ast = ast->child[0];
3248
15.2k
  zend_ast *prop_ast = ast->child[1];
3249
3250
15.2k
  znode class_node, prop_node;
3251
15.2k
  zend_op *opline;
3252
3253
15.2k
  zend_short_circuiting_mark_inner(class_ast);
3254
15.2k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
3255
3256
15.2k
  zend_compile_expr(&prop_node, prop_ast);
3257
3258
15.2k
  if (delayed) {
3259
5.89k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3260
9.39k
  } else {
3261
9.39k
    opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3262
9.39k
  }
3263
15.2k
  if (opline->op1_type == IS_CONST) {
3264
13.4k
    convert_to_string(CT_CONSTANT(opline->op1));
3265
13.4k
    opline->extended_value = zend_alloc_cache_slots(3);
3266
13.4k
  }
3267
15.2k
  if (class_node.op_type == IS_CONST) {
3268
9.61k
    opline->op2_type = IS_CONST;
3269
9.61k
    opline->op2.constant = zend_add_class_name_literal(
3270
9.61k
      Z_STR(class_node.u.constant));
3271
9.61k
    if (opline->op1_type != IS_CONST) {
3272
1.43k
      opline->extended_value = zend_alloc_cache_slot();
3273
1.43k
    }
3274
9.61k
  } else {
3275
5.67k
    SET_NODE(opline->op2, &class_node);
3276
5.67k
  }
3277
3278
15.2k
  if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */
3279
1.16k
    opline->extended_value |= ZEND_FETCH_REF;
3280
1.16k
  }
3281
3282
15.2k
  zend_adjust_for_fetch_type(opline, result, type);
3283
15.2k
  return opline;
3284
15.2k
}
3285
/* }}} */
3286
3287
4.96k
static void zend_verify_list_assign_target(const zend_ast *var_ast, zend_ast_attr array_style) /* {{{ */ {
3288
4.96k
  if (var_ast->kind == ZEND_AST_ARRAY) {
3289
272
    if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) {
3290
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead");
3291
5
    }
3292
267
    if (array_style != var_ast->attr) {
3293
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()");
3294
6
    }
3295
4.68k
  } else if (!zend_can_write_to_variable(var_ast)) {
3296
86
    zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values");
3297
86
  }
3298
4.96k
}
3299
/* }}} */
3300
3301
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node);
3302
3303
/* Propagate refs used on leaf elements to the surrounding list() structures. */
3304
3.53k
static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
3305
3.53k
  const zend_ast_list *list = zend_ast_get_list(ast);
3306
3.53k
  bool has_refs = false;
3307
3.53k
  uint32_t i;
3308
3309
15.8k
  for (i = 0; i < list->children; ++i) {
3310
12.2k
    zend_ast *elem_ast = list->child[i];
3311
3312
12.2k
    if (elem_ast) {
3313
5.35k
      zend_ast *var_ast = elem_ast->child[0];
3314
5.35k
      if (var_ast->kind == ZEND_AST_ARRAY) {
3315
274
        elem_ast->attr = zend_propagate_list_refs(var_ast);
3316
274
      }
3317
5.35k
      has_refs |= elem_ast->attr;
3318
5.35k
    }
3319
12.2k
  }
3320
3321
3.53k
  return has_refs;
3322
3.53k
}
3323
/* }}} */
3324
3325
static bool list_is_keyed(const zend_ast_list *list)
3326
3.08k
{
3327
3.81k
  for (uint32_t i = 0; i < list->children; i++) {
3328
3.79k
    const zend_ast *child = list->child[i];
3329
3.79k
    if (child) {
3330
3.06k
      return child->kind == ZEND_AST_ARRAY_ELEM && child->child[1] != NULL;
3331
3.06k
    }
3332
3.79k
  }
3333
26
  return false;
3334
3.08k
}
3335
3336
static void zend_compile_list_assign(
3337
    znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style, uint32_t type) /* {{{ */
3338
3.08k
{
3339
3.08k
  zend_ast_list *list = zend_ast_get_list(ast);
3340
3.08k
  uint32_t i;
3341
3.08k
  bool has_elems = false;
3342
3.08k
  bool is_keyed = list_is_keyed(list);
3343
3344
3.08k
  if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
3345
167
    zval_make_interned_string(&expr_node->u.constant);
3346
167
  }
3347
3348
8.90k
  for (i = 0; i < list->children; ++i) {
3349
5.81k
    zend_ast *elem_ast = list->child[i];
3350
5.81k
    zend_ast *var_ast, *key_ast;
3351
5.81k
    znode fetch_result, dim_node;
3352
5.81k
    zend_op *opline;
3353
3354
5.81k
    if (elem_ast == NULL) {
3355
833
      if (is_keyed) {
3356
13
        zend_error(E_COMPILE_ERROR,
3357
13
          "Cannot use empty array entries in keyed array assignment");
3358
820
      } else {
3359
820
        continue;
3360
820
      }
3361
833
    }
3362
3363
4.99k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
3364
9
      zend_error(E_COMPILE_ERROR,
3365
9
          "Spread operator is not supported in assignments");
3366
9
    }
3367
3368
4.99k
    var_ast = elem_ast->child[0];
3369
4.99k
    key_ast = elem_ast->child[1];
3370
4.99k
    has_elems = true;
3371
3372
4.99k
    if (is_keyed) {
3373
861
      if (key_ast == NULL) {
3374
5
        zend_error(E_COMPILE_ERROR,
3375
5
          "Cannot mix keyed and unkeyed array entries in assignments");
3376
5
      }
3377
3378
861
      zend_compile_expr(&dim_node, key_ast);
3379
4.13k
    } else {
3380
4.13k
      if (key_ast != NULL) {
3381
7
        zend_error(E_COMPILE_ERROR,
3382
7
          "Cannot mix keyed and unkeyed array entries in assignments");
3383
7
      }
3384
3385
4.13k
      dim_node.op_type = IS_CONST;
3386
4.13k
      ZVAL_LONG(&dim_node.u.constant, i);
3387
4.13k
    }
3388
3389
4.99k
    if (expr_node->op_type == IS_CONST) {
3390
602
      Z_TRY_ADDREF(expr_node->u.constant);
3391
602
    }
3392
3393
4.99k
    zend_verify_list_assign_target(var_ast, array_style);
3394
3395
4.99k
    opline = zend_emit_op(&fetch_result,
3396
4.99k
      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);
3397
4.99k
    if (opline->opcode == ZEND_FETCH_LIST_R) {
3398
3.54k
      opline->result_type = IS_TMP_VAR;
3399
3.54k
      fetch_result.op_type = IS_TMP_VAR;
3400
3.54k
    }
3401
3402
4.99k
    if (dim_node.op_type == IS_CONST) {
3403
4.71k
      zend_handle_numeric_dim(opline, &dim_node);
3404
4.71k
    }
3405
3406
4.99k
    if (elem_ast->attr) {
3407
1.31k
      zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL);
3408
1.31k
    }
3409
4.99k
    if (var_ast->kind == ZEND_AST_ARRAY) {
3410
261
      zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr, type);
3411
4.73k
    } else if (elem_ast->attr) {
3412
1.27k
      zend_emit_assign_ref_znode(var_ast, &fetch_result);
3413
3.45k
    } else {
3414
3.45k
      zend_emit_assign_znode(var_ast, &fetch_result);
3415
3.45k
    }
3416
4.99k
  }
3417
3418
3.08k
  if (has_elems == 0) {
3419
26
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
3420
26
  }
3421
3422
3.06k
  if (result) {
3423
159
    if ((type == BP_VAR_R || type == BP_VAR_IS) && expr_node->op_type == IS_VAR) {
3424
      /* Deref. */
3425
22
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, expr_node, NULL);
3426
137
    } else {
3427
137
      *result = *expr_node;
3428
137
    }
3429
2.90k
  } else {
3430
2.90k
    zend_do_free(expr_node);
3431
2.90k
  }
3432
3.06k
}
3433
/* }}} */
3434
3435
static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
3436
516k
{
3437
516k
  if (ast->kind == ZEND_AST_CALL || ast->kind == ZEND_AST_PIPE) {
3438
73
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
3439
73
  }
3440
516k
  if (
3441
516k
    ast->kind == ZEND_AST_METHOD_CALL
3442
516k
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
3443
516k
    || ast->kind == ZEND_AST_STATIC_CALL
3444
516k
  ) {
3445
14
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
3446
14
  }
3447
516k
  if (zend_ast_is_short_circuited(ast)) {
3448
21
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use nullsafe operator in write context");
3449
21
  }
3450
516k
  if (is_globals_fetch(ast)) {
3451
32
    zend_error_noreturn(E_COMPILE_ERROR,
3452
32
      "$GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax");
3453
32
  }
3454
516k
}
3455
/* }}} */
3456
3457
/* Detects $a... = $a pattern */
3458
static bool zend_is_assign_to_self(const zend_ast *var_ast, const zend_ast *expr_ast) /* {{{ */
3459
34.2k
{
3460
34.2k
  if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
3461
29.5k
    return 0;
3462
29.5k
  }
3463
3464
10.9k
  while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
3465
6.27k
    var_ast = var_ast->child[0];
3466
6.27k
  }
3467
3468
4.70k
  if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
3469
755
    return 0;
3470
755
  }
3471
3472
3.94k
  {
3473
3.94k
    zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
3474
3.94k
    zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
3475
3.94k
    bool result = zend_string_equals(name1, name2);
3476
3.94k
    zend_string_release_ex(name1, 0);
3477
3.94k
    zend_string_release_ex(name2, 0);
3478
3.94k
    return result;
3479
4.70k
  }
3480
4.70k
}
3481
/* }}} */
3482
3483
static void zend_compile_expr_with_potential_assign_to_self(
3484
34.2k
    znode *expr_node, zend_ast *expr_ast, const zend_ast *var_ast) {
3485
34.2k
  if (zend_is_assign_to_self(var_ast, expr_ast) && !is_this_fetch(expr_ast)) {
3486
    /* $a[0] = $a should evaluate the right $a first */
3487
874
    znode cv_node;
3488
3489
874
    if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3490
55
      zend_compile_simple_var_no_cv(expr_node, expr_ast, BP_VAR_R, false);
3491
819
    } else {
3492
819
      zend_emit_op_tmp(expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3493
819
    }
3494
33.3k
  } else {
3495
33.3k
    zend_compile_expr(expr_node, expr_ast);
3496
33.3k
  }
3497
34.2k
}
3498
3499
static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type) /* {{{ */
3500
363k
{
3501
363k
  zend_ast *var_ast = ast->child[0];
3502
363k
  zend_ast *expr_ast = ast->child[1];
3503
3504
363k
  znode var_node, expr_node;
3505
363k
  zend_op *opline;
3506
363k
  uint32_t offset;
3507
363k
  if (is_this_fetch(var_ast)) {
3508
9
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3509
9
  }
3510
3511
363k
  zend_ensure_writable_variable(var_ast);
3512
3513
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3514
363k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3515
363k
  switch (kind) {
3516
335k
    case ZEND_AST_VAR:
3517
335k
      offset = zend_delayed_compile_begin();
3518
335k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, false);
3519
335k
      zend_compile_expr(&expr_node, expr_ast);
3520
335k
      zend_delayed_compile_end(offset);
3521
335k
      CG(zend_lineno) = zend_ast_get_lineno(var_ast);
3522
335k
      zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node);
3523
335k
      return;
3524
2.57k
    case ZEND_AST_STATIC_PROP:
3525
2.57k
      offset = zend_delayed_compile_begin();
3526
2.57k
      zend_delayed_compile_var(result, var_ast, BP_VAR_W, false);
3527
2.57k
      zend_compile_expr(&expr_node, expr_ast);
3528
3529
2.57k
      opline = zend_delayed_compile_end(offset);
3530
2.57k
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
3531
2.57k
      opline->result_type = IS_TMP_VAR;
3532
2.57k
      result->op_type = IS_TMP_VAR;
3533
3534
2.57k
      zend_emit_op_data(&expr_node);
3535
2.57k
      return;
3536
11.3k
    case ZEND_AST_DIM:
3537
11.3k
      offset = zend_delayed_compile_begin();
3538
11.3k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_W, /* by_ref */ false);
3539
11.3k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3540
3541
11.3k
      opline = zend_delayed_compile_end(offset);
3542
11.3k
      opline->opcode = ZEND_ASSIGN_DIM;
3543
11.3k
      opline->result_type = IS_TMP_VAR;
3544
11.3k
      result->op_type = IS_TMP_VAR;
3545
3546
11.3k
      opline = zend_emit_op_data(&expr_node);
3547
11.3k
      return;
3548
10.7k
    case ZEND_AST_PROP:
3549
10.7k
    case ZEND_AST_NULLSAFE_PROP:
3550
10.7k
      offset = zend_delayed_compile_begin();
3551
10.7k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
3552
10.7k
      zend_compile_expr(&expr_node, expr_ast);
3553
3554
10.7k
      opline = zend_delayed_compile_end(offset);
3555
10.7k
      opline->opcode = ZEND_ASSIGN_OBJ;
3556
10.7k
      opline->result_type = IS_TMP_VAR;
3557
10.7k
      result->op_type = IS_TMP_VAR;
3558
3559
10.7k
      zend_emit_op_data(&expr_node);
3560
10.7k
      return;
3561
2.89k
    case ZEND_AST_ARRAY:
3562
2.89k
      if (zend_propagate_list_refs(var_ast)) {
3563
1.13k
        if (!zend_is_variable_or_call(expr_ast)) {
3564
20
          zend_error_noreturn(E_COMPILE_ERROR,
3565
20
            "Cannot assign reference to non referenceable value");
3566
1.11k
        } else {
3567
1.11k
          zend_assert_not_short_circuited(expr_ast);
3568
1.11k
        }
3569
3570
1.11k
        zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
3571
        /* MAKE_REF is usually not necessary for CVs. However, if there are
3572
         * self-assignments, this forces the RHS to evaluate first. */
3573
1.11k
        zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
3574
1.75k
      } else {
3575
1.75k
        if (expr_ast->kind == ZEND_AST_VAR) {
3576
          /* list($a, $b) = $a should evaluate the right $a first */
3577
432
          znode cv_node;
3578
3579
432
          if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3580
201
            zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, false);
3581
231
          } else {
3582
231
            zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3583
231
          }
3584
1.32k
        } else {
3585
1.32k
          zend_compile_expr(&expr_node, expr_ast);
3586
1.32k
        }
3587
1.75k
      }
3588
3589
2.87k
      zend_compile_list_assign(!stmt ? result : NULL, var_ast, &expr_node, var_ast->attr, type);
3590
2.87k
      if (stmt) {
3591
2.16k
        result->op_type = IS_UNUSED;
3592
2.16k
      }
3593
2.87k
      return;
3594
0
    EMPTY_SWITCH_DEFAULT_CASE();
3595
363k
  }
3596
363k
}
3597
/* }}} */
3598
3599
static void zend_compile_assign_ref(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3600
6.73k
{
3601
6.73k
  zend_ast *target_ast = ast->child[0];
3602
6.73k
  zend_ast *source_ast = ast->child[1];
3603
3604
6.73k
  znode target_node, source_node;
3605
6.73k
  zend_op *opline;
3606
6.73k
  uint32_t offset, flags;
3607
3608
6.73k
  if (is_this_fetch(target_ast)) {
3609
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3610
5
  }
3611
6.73k
  zend_ensure_writable_variable(target_ast);
3612
6.73k
  zend_assert_not_short_circuited(source_ast);
3613
6.73k
  if (is_globals_fetch(source_ast)) {
3614
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot acquire reference to $GLOBALS");
3615
5
  }
3616
3617
6.72k
  offset = zend_delayed_compile_begin();
3618
6.72k
  zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, true);
3619
6.72k
  zend_compile_var(&source_node, source_ast, BP_VAR_W, true);
3620
3621
6.72k
  if ((target_ast->kind != ZEND_AST_VAR
3622
4.62k
    || target_ast->child[0]->kind != ZEND_AST_ZVAL)
3623
2.70k
   && source_ast->kind != ZEND_AST_ZNODE
3624
1.93k
   && source_node.op_type != IS_CV) {
3625
    /* Both LHS and RHS expressions may modify the same data structure,
3626
     * and the modification during RHS evaluation may dangle the pointer
3627
     * to the result of the LHS evaluation.
3628
     * Use MAKE_REF instruction to replace direct pointer with REFERENCE.
3629
     * See: Bug #71539
3630
     */
3631
902
    zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL);
3632
902
  }
3633
3634
6.72k
  opline = zend_delayed_compile_end(offset);
3635
3636
6.72k
  if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
3637
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3638
5
  }
3639
3640
6.72k
  flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0;
3641
3642
6.72k
  if (opline && opline->opcode == ZEND_FETCH_OBJ_W) {
3643
869
    opline->opcode = ZEND_ASSIGN_OBJ_REF;
3644
869
    opline->extended_value &= ~ZEND_FETCH_REF;
3645
869
    opline->extended_value |= flags;
3646
869
    if (result) {
3647
63
      *result = target_node;
3648
806
    } else {
3649
806
      SET_UNUSED(opline->result);
3650
806
    }
3651
869
    zend_emit_op_data(&source_node);
3652
5.85k
  } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) {
3653
238
    opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF;
3654
238
    opline->extended_value &= ~ZEND_FETCH_REF;
3655
238
    opline->extended_value |= flags;
3656
238
    if (result) {
3657
41
      *result = target_node;
3658
197
    } else {
3659
197
      SET_UNUSED(opline->result);
3660
197
    }
3661
238
    zend_emit_op_data(&source_node);
3662
5.61k
  } else {
3663
5.61k
    opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
3664
5.61k
    opline->extended_value = flags;
3665
5.61k
  }
3666
3667
6.72k
  if (result && (type == BP_VAR_R || type == BP_VAR_IS)) {
3668
    /* Deref. */
3669
1.28k
    znode tmp_result = *result;
3670
1.28k
    zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &tmp_result, NULL);
3671
1.28k
  }
3672
6.72k
}
3673
/* }}} */
3674
3675
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3676
1.88k
{
3677
1.88k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
3678
1.88k
    zend_ast_create_znode(value_node));
3679
1.88k
  zend_compile_stmt(assign_ast);
3680
1.88k
}
3681
/* }}} */
3682
3683
static void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
3684
104k
{
3685
104k
  zend_ast *var_ast = ast->child[0];
3686
104k
  zend_ast *expr_ast = ast->child[1];
3687
104k
  uint32_t opcode = ast->attr;
3688
3689
104k
  znode var_node, expr_node;
3690
104k
  zend_op *opline;
3691
104k
  uint32_t offset, cache_slot;
3692
3693
104k
  zend_ensure_writable_variable(var_ast);
3694
3695
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3696
104k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3697
104k
  switch (kind) {
3698
100k
    case ZEND_AST_VAR:
3699
100k
      offset = zend_delayed_compile_begin();
3700
100k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, false);
3701
100k
      zend_compile_expr(&expr_node, expr_ast);
3702
100k
      zend_delayed_compile_end(offset);
3703
100k
      opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node);
3704
100k
      opline->extended_value = opcode;
3705
100k
      return;
3706
834
    case ZEND_AST_STATIC_PROP:
3707
834
      offset = zend_delayed_compile_begin();
3708
834
      zend_delayed_compile_var(result, var_ast, BP_VAR_RW, false);
3709
834
      zend_compile_expr(&expr_node, expr_ast);
3710
3711
834
      opline = zend_delayed_compile_end(offset);
3712
834
      cache_slot = opline->extended_value;
3713
834
      opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP;
3714
834
      opline->extended_value = opcode;
3715
834
      opline->result_type = IS_TMP_VAR;
3716
834
      result->op_type = IS_TMP_VAR;
3717
3718
834
      opline = zend_emit_op_data(&expr_node);
3719
834
      opline->extended_value = cache_slot;
3720
834
      return;
3721
2.37k
    case ZEND_AST_DIM:
3722
2.37k
      offset = zend_delayed_compile_begin();
3723
2.37k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_RW, /* by_ref */ false);
3724
2.37k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3725
3726
2.37k
      opline = zend_delayed_compile_end(offset);
3727
2.37k
      opline->opcode = ZEND_ASSIGN_DIM_OP;
3728
2.37k
      opline->extended_value = opcode;
3729
2.37k
      opline->result_type = IS_TMP_VAR;
3730
2.37k
      result->op_type = IS_TMP_VAR;
3731
3732
2.37k
      zend_emit_op_data(&expr_node);
3733
2.37k
      return;
3734
1.13k
    case ZEND_AST_PROP:
3735
1.13k
    case ZEND_AST_NULLSAFE_PROP:
3736
1.13k
      offset = zend_delayed_compile_begin();
3737
1.13k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
3738
1.13k
      zend_compile_expr(&expr_node, expr_ast);
3739
3740
1.13k
      opline = zend_delayed_compile_end(offset);
3741
1.13k
      cache_slot = opline->extended_value;
3742
1.13k
      opline->opcode = ZEND_ASSIGN_OBJ_OP;
3743
1.13k
      opline->extended_value = opcode;
3744
1.13k
      opline->result_type = IS_TMP_VAR;
3745
1.13k
      result->op_type = IS_TMP_VAR;
3746
3747
1.13k
      opline = zend_emit_op_data(&expr_node);
3748
1.13k
      opline->extended_value = cache_slot;
3749
1.13k
      return;
3750
104k
    EMPTY_SWITCH_DEFAULT_CASE()
3751
104k
  }
3752
104k
}
3753
/* }}} */
3754
3755
4.84k
static uint32_t zend_get_arg_num(const zend_function *fn, const zend_string *arg_name) {
3756
  // TODO: Caching?
3757
17.3k
  for (uint32_t i = 0; i < fn->common.num_args; i++) {
3758
14.1k
    zend_arg_info *arg_info = &fn->op_array.arg_info[i];
3759
14.1k
    if (zend_string_equals(arg_info->name, arg_name)) {
3760
1.56k
      return i + 1;
3761
1.56k
    }
3762
14.1k
  }
3763
3764
  /* Either an invalid argument name, or collected into a variadic argument. */
3765
3.27k
  return (uint32_t) -1;
3766
4.84k
}
3767
3768
static uint32_t zend_compile_args(
3769
    zend_ast *ast, const zend_function *fbc, bool *may_have_extra_named_args) /* {{{ */
3770
2.92M
{
3771
2.92M
  const zend_ast_list *args = zend_ast_get_list(ast);
3772
2.92M
  uint32_t i;
3773
2.92M
  bool uses_arg_unpack = false;
3774
2.92M
  uint32_t arg_count = 0; /* number of arguments not including unpacks */
3775
3776
  /* Whether named arguments are used syntactically, to enforce language level limitations.
3777
   * May not actually use named argument passing. */
3778
2.92M
  bool uses_named_args = false;
3779
  /* Whether there may be any undef arguments due to the use of named arguments. */
3780
2.92M
  bool may_have_undef = false;
3781
  /* Whether there may be any extra named arguments collected into a variadic. */
3782
2.92M
  *may_have_extra_named_args = false;
3783
3784
6.83M
  for (i = 0; i < args->children; ++i) {
3785
3.91M
    zend_ast *arg = args->child[i];
3786
3.91M
    zend_string *arg_name = NULL;
3787
3.91M
    uint32_t arg_num = i + 1;
3788
3789
3.91M
    znode arg_node;
3790
3.91M
    zend_op *opline;
3791
3.91M
    uint8_t opcode;
3792
3793
3.91M
    if (arg->kind == ZEND_AST_UNPACK) {
3794
1.19k
      if (uses_named_args) {
3795
5
        zend_error_noreturn(E_COMPILE_ERROR,
3796
5
          "Cannot use argument unpacking after named arguments");
3797
5
      }
3798
3799
      /* Unpack may contain named arguments. */
3800
1.18k
      may_have_undef = true;
3801
1.18k
      if (!fbc || (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3802
893
        *may_have_extra_named_args = true;
3803
893
      }
3804
3805
1.18k
      uses_arg_unpack = true;
3806
1.18k
      fbc = NULL;
3807
3808
1.18k
      zend_compile_expr(&arg_node, arg->child[0]);
3809
1.18k
      opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
3810
1.18k
      opline->op2.num = arg_count;
3811
1.18k
      opline->result.var = EX_NUM_TO_VAR(arg_count - 1);
3812
3813
1.18k
      continue;
3814
1.19k
    }
3815
3816
3.91M
    if (arg->kind == ZEND_AST_NAMED_ARG) {
3817
24.7k
      uses_named_args = true;
3818
24.7k
      arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0]));
3819
24.7k
      arg = arg->child[1];
3820
3821
24.7k
      if (fbc && !uses_arg_unpack) {
3822
4.84k
        arg_num = zend_get_arg_num(fbc, arg_name);
3823
4.84k
        if (arg_num == arg_count + 1 && !may_have_undef) {
3824
          /* Using named arguments, but passing in order. */
3825
459
          arg_name = NULL;
3826
459
          arg_count++;
3827
4.38k
        } else {
3828
          // TODO: We could track which arguments were passed, even if out of order.
3829
4.38k
          may_have_undef = true;
3830
4.38k
          if (arg_num == (uint32_t) -1 && (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3831
269
            *may_have_extra_named_args = true;
3832
269
          }
3833
4.38k
        }
3834
19.9k
      } else {
3835
19.9k
        arg_num = (uint32_t) -1;
3836
19.9k
        may_have_undef = true;
3837
19.9k
        *may_have_extra_named_args = true;
3838
19.9k
      }
3839
3.88M
    } else {
3840
3.88M
      if (uses_arg_unpack) {
3841
11
        zend_error_noreturn(E_COMPILE_ERROR,
3842
11
          "Cannot use positional argument after argument unpacking");
3843
11
      }
3844
3845
3.88M
      if (uses_named_args) {
3846
36
        zend_error_noreturn(E_COMPILE_ERROR,
3847
36
          "Cannot use positional argument after named argument");
3848
36
      }
3849
3850
3.88M
      arg_count++;
3851
3.88M
    }
3852
3853
    /* Treat passing of $GLOBALS the same as passing a call.
3854
     * This will error at runtime if the argument is by-ref. */
3855
3.91M
    if (zend_is_call(arg) || is_globals_fetch(arg)) {
3856
117k
      uint32_t type = is_globals_fetch(arg) || (fbc && !ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num))
3857
117k
        ? BP_VAR_R : BP_VAR_FUNC_ARG;
3858
117k
      zend_compile_var(&arg_node, arg, type, /* by_ref */ false);
3859
117k
      if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
3860
        /* Function call was converted into builtin instruction */
3861
41.2k
        if (!fbc || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3862
2.58k
          opcode = ZEND_SEND_VAL_EX;
3863
38.6k
        } else {
3864
38.6k
          opcode = ZEND_SEND_VAL;
3865
38.6k
        }
3866
76.1k
      } else {
3867
76.1k
        if (fbc && arg_num != (uint32_t) -1) {
3868
520
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3869
448
            opcode = ZEND_SEND_VAR_NO_REF;
3870
448
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3871
            /* For IS_VAR operands, SEND_VAL will pass through the operand without
3872
             * dereferencing, so it will use a by-ref pass if the call returned by-ref
3873
             * and a by-value pass if it returned by-value. */
3874
72
            opcode = ZEND_SEND_VAL;
3875
72
          } else {
3876
0
            opcode = ZEND_SEND_VAR;
3877
0
          }
3878
75.5k
        } else {
3879
75.5k
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3880
75.5k
        }
3881
76.1k
      }
3882
3.79M
    } else if (zend_is_passable_by_ref(arg) && !zend_ast_is_short_circuited(arg)) {
3883
136k
      if (fbc && arg_num != (uint32_t) -1) {
3884
74.8k
        if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3885
2.39k
          zend_compile_var(&arg_node, arg, BP_VAR_W, true);
3886
2.39k
          opcode = ZEND_SEND_REF;
3887
72.4k
        } else {
3888
72.4k
          zend_compile_var(&arg_node, arg, BP_VAR_R, false);
3889
72.4k
          opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR;
3890
72.4k
        }
3891
74.8k
      } else {
3892
61.3k
        do {
3893
61.3k
          if (arg->kind == ZEND_AST_VAR) {
3894
15.4k
            CG(zend_lineno) = zend_ast_get_lineno(ast);
3895
15.4k
            if (is_this_fetch(arg)) {
3896
1.48k
              zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL);
3897
1.48k
              opcode = ZEND_SEND_VAR_EX;
3898
1.48k
              CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3899
1.48k
              break;
3900
13.9k
            } else if (zend_try_compile_cv(&arg_node, arg, BP_VAR_R) == SUCCESS) {
3901
13.7k
              opcode = ZEND_SEND_VAR_EX;
3902
13.7k
              break;
3903
13.7k
            }
3904
15.4k
          }
3905
46.1k
          opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL);
3906
46.1k
          if (arg_name) {
3907
2.34k
            opline->op2_type = IS_CONST;
3908
2.34k
            zend_string_addref(arg_name);
3909
2.34k
            opline->op2.constant = zend_add_literal_string(&arg_name);
3910
2.34k
            opline->result.num = zend_alloc_cache_slots(2);
3911
43.7k
          } else {
3912
43.7k
            opline->op2.num = arg_num;
3913
43.7k
          }
3914
46.1k
          zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, true);
3915
46.1k
          opcode = ZEND_SEND_FUNC_ARG;
3916
46.1k
        } while (0);
3917
61.3k
      }
3918
3.65M
    } else {
3919
3.65M
      zend_compile_expr(&arg_node, arg);
3920
3.65M
      if (arg_node.op_type == IS_VAR) {
3921
        /* pass ++$a or something similar */
3922
0
        if (fbc && arg_num != (uint32_t) -1) {
3923
0
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3924
0
            opcode = ZEND_SEND_VAR_NO_REF;
3925
0
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3926
0
            opcode = ZEND_SEND_VAL;
3927
0
          } else {
3928
0
            opcode = ZEND_SEND_VAR;
3929
0
          }
3930
0
        } else {
3931
0
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3932
0
        }
3933
3.65M
      } else if (arg_node.op_type == IS_CV) {
3934
0
        if (fbc && arg_num != (uint32_t) -1) {
3935
0
          if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3936
0
            opcode = ZEND_SEND_REF;
3937
0
          } else {
3938
0
            opcode = ZEND_SEND_VAR;
3939
0
          }
3940
0
        } else {
3941
0
          opcode = ZEND_SEND_VAR_EX;
3942
0
        }
3943
3.65M
      } else {
3944
        /* Delay "Only variables can be passed by reference" error to execution */
3945
3.65M
        if (fbc && arg_num != (uint32_t) -1 && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3946
112k
          opcode = ZEND_SEND_VAL;
3947
3.54M
        } else {
3948
3.54M
          opcode = ZEND_SEND_VAL_EX;
3949
3.54M
        }
3950
3.65M
      }
3951
3.65M
    }
3952
3953
3.91M
    opline = zend_emit_op(NULL, opcode, &arg_node, NULL);
3954
3.91M
    if (arg_name) {
3955
24.3k
      opline->op2_type = IS_CONST;
3956
24.3k
      zend_string_addref(arg_name);
3957
24.3k
      opline->op2.constant = zend_add_literal_string(&arg_name);
3958
24.3k
      opline->result.num = zend_alloc_cache_slots(2);
3959
3.88M
    } else {
3960
3.88M
      opline->op2.opline_num = arg_num;
3961
3.88M
      opline->result.var = EX_NUM_TO_VAR(arg_num - 1);
3962
3.88M
    }
3963
3.91M
  }
3964
3965
2.92M
  if (may_have_undef) {
3966
21.3k
    zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
3967
21.3k
  }
3968
3969
2.92M
  return arg_count;
3970
2.92M
}
3971
/* }}} */
3972
3973
ZEND_API uint8_t zend_get_call_op(const zend_op *init_op, const zend_function *fbc, bool result_used) /* {{{ */
3974
3.02M
{
3975
3.02M
  uint32_t no_discard = result_used ? 0 : ZEND_ACC_NODISCARD;
3976
3977
3.02M
  if (fbc && init_op->opcode != ZEND_NEW) {
3978
237k
    ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE));
3979
237k
    if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) {
3980
192k
      if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) {
3981
20.2k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3982
20.1k
          return ZEND_DO_ICALL;
3983
20.1k
        } else {
3984
88
          return ZEND_DO_FCALL_BY_NAME;
3985
88
        }
3986
20.2k
      }
3987
192k
    } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){
3988
45.5k
      if (zend_execute_ex == execute_ex) {
3989
22.9k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3990
22.7k
          return ZEND_DO_UCALL;
3991
22.7k
        } else {
3992
201
          return ZEND_DO_FCALL_BY_NAME;
3993
201
        }
3994
22.9k
      }
3995
45.5k
    }
3996
2.78M
  } else if (zend_execute_ex == execute_ex &&
3997
2.71M
             !zend_execute_internal &&
3998
2.67M
             (init_op->opcode == ZEND_INIT_FCALL_BY_NAME ||
3999
2.60M
              init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) {
4000
2.48M
    return ZEND_DO_FCALL_BY_NAME;
4001
2.48M
  }
4002
494k
  return ZEND_DO_FCALL;
4003
3.02M
}
4004
/* }}} */
4005
4006
static bool zend_compile_call_common(znode *result, zend_ast *args_ast, const zend_function *fbc, uint32_t lineno, uint32_t type) /* {{{ */
4007
2.92M
{
4008
2.92M
  zend_op *opline;
4009
2.92M
  uint32_t opnum_init = get_next_op_number() - 1;
4010
4011
2.92M
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
4012
3.65k
    opline = &CG(active_op_array)->opcodes[opnum_init];
4013
3.65k
    opline->extended_value = 0;
4014
    /* opcode array may be reallocated, so don't access opcode field after zend_emit_op_tmp(). */
4015
3.65k
    uint8_t opcode = opline->opcode;
4016
4017
3.65k
    if (opcode == ZEND_NEW) {
4018
16
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
4019
16
    }
4020
4021
3.64k
    zend_ast_list *args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args);
4022
3.64k
    if (args->children != 1 || args->child[0]->attr != ZEND_PLACEHOLDER_VARIADIC) {
4023
18
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot create a Closure for call expression with more than one argument, or non-variadic placeholders");
4024
18
    }
4025
4026
3.62k
    if (opcode == ZEND_INIT_FCALL) {
4027
596
      opline->op1.num = zend_vm_calc_used_stack(0, fbc);
4028
596
    }
4029
4030
3.62k
    zend_op *callable_convert_op = zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT, NULL, NULL);
4031
3.62k
    if (opcode == ZEND_INIT_FCALL
4032
3.02k
     || opcode == ZEND_INIT_FCALL_BY_NAME
4033
2.92k
     || opcode == ZEND_INIT_NS_FCALL_BY_NAME) {
4034
2.92k
      callable_convert_op->extended_value = zend_alloc_cache_slot();
4035
2.92k
    } else {
4036
705
      callable_convert_op->extended_value = (uint32_t)-1;
4037
705
    }
4038
3.62k
    return true;
4039
3.64k
  }
4040
4041
2.92M
  bool may_have_extra_named_args;
4042
2.92M
  uint32_t arg_count = zend_compile_args(args_ast, fbc, &may_have_extra_named_args);
4043
4044
2.92M
  zend_do_extended_fcall_begin();
4045
4046
2.92M
  opline = &CG(active_op_array)->opcodes[opnum_init];
4047
2.92M
  opline->extended_value = arg_count;
4048
2.92M
  uint8_t init_opcode = opline->opcode;
4049
4050
2.92M
  if (init_opcode == ZEND_INIT_FCALL) {
4051
128k
    opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
4052
128k
  }
4053
4054
2.92M
  uint8_t call_op = zend_get_call_op(
4055
2.92M
    opline,
4056
2.92M
    fbc,
4057
    /* result_used: At this point we do not yet reliably
4058
     * know if the result is used. Deoptimize #[\NoDiscard]
4059
     * calls to be sure. The optimizer will fix this up.
4060
     */
4061
2.92M
    false
4062
2.92M
  );
4063
2.92M
  opline = zend_emit_op(result, call_op, NULL, NULL);
4064
2.92M
  if (type == BP_VAR_R || type == BP_VAR_IS) {
4065
2.83M
    if (init_opcode != ZEND_NEW && opline->result_type == IS_VAR) {
4066
2.75M
      opline->result_type = IS_TMP_VAR;
4067
2.75M
      result->op_type = IS_TMP_VAR;
4068
2.75M
    }
4069
2.83M
  }
4070
2.92M
  if (may_have_extra_named_args) {
4071
19.4k
    opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4072
19.4k
  }
4073
2.92M
  opline->lineno = lineno;
4074
2.92M
  zend_do_extended_fcall_end();
4075
2.92M
  return false;
4076
2.92M
}
4077
/* }}} */
4078
4079
static bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
4080
2.65M
{
4081
2.65M
  zend_string *orig_name = zend_ast_get_str(name_ast);
4082
2.65M
  bool is_fully_qualified;
4083
4084
2.65M
  name_node->op_type = IS_CONST;
4085
2.65M
  ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
4086
2.65M
    orig_name, name_ast->attr, &is_fully_qualified));
4087
4088
2.65M
  return !is_fully_qualified && FC(current_namespace);
4089
2.65M
}
4090
/* }}} */
4091
4092
static void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */
4093
200k
{
4094
200k
  if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
4095
77.1k
    const char *colon;
4096
77.1k
    zend_string *str = Z_STR(name_node->u.constant);
4097
77.1k
    if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
4098
291
      zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
4099
291
      zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
4100
291
      zend_op *opline = get_next_op();
4101
4102
291
      opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
4103
291
      opline->op1_type = IS_CONST;
4104
291
      opline->op1.constant = zend_add_class_name_literal(class);
4105
291
      opline->op2_type = IS_CONST;
4106
291
      opline->op2.constant = zend_add_func_name_literal(method);
4107
      /* 2 slots, for class and method */
4108
291
      opline->result.num = zend_alloc_cache_slots(2);
4109
291
      zval_ptr_dtor(&name_node->u.constant);
4110
76.8k
    } else {
4111
76.8k
      zend_op *opline = get_next_op();
4112
4113
76.8k
      opline->opcode = ZEND_INIT_FCALL_BY_NAME;
4114
76.8k
      opline->op2_type = IS_CONST;
4115
76.8k
      opline->op2.constant = zend_add_func_name_literal(str);
4116
76.8k
      opline->result.num = zend_alloc_cache_slot();
4117
76.8k
    }
4118
123k
  } else {
4119
123k
    zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node);
4120
123k
  }
4121
4122
200k
  zend_compile_call_common(result, args_ast, NULL, lineno, type);
4123
200k
}
4124
/* }}} */
4125
4126
static inline bool zend_args_contain_unpack_or_named(const zend_ast_list *args) /* {{{ */
4127
2.50M
{
4128
2.50M
  uint32_t i;
4129
6.04M
  for (i = 0; i < args->children; ++i) {
4130
3.55M
    const zend_ast *arg = args->child[i];
4131
3.55M
    if (arg->kind == ZEND_AST_UNPACK || arg->kind == ZEND_AST_NAMED_ARG) {
4132
17.5k
      return 1;
4133
17.5k
    }
4134
3.55M
  }
4135
2.48M
  return 0;
4136
2.50M
}
4137
/* }}} */
4138
4139
static zend_result zend_compile_func_strlen(znode *result, const zend_ast_list *args) /* {{{ */
4140
745
{
4141
745
  znode arg_node;
4142
4143
745
  if (args->children != 1) {
4144
62
    return FAILURE;
4145
62
  }
4146
4147
683
  zend_compile_expr(&arg_node, args->child[0]);
4148
683
  if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
4149
132
    result->op_type = IS_CONST;
4150
132
    ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
4151
132
    zval_ptr_dtor_str(&arg_node.u.constant);
4152
551
  } else {
4153
551
    zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
4154
551
  }
4155
683
  return SUCCESS;
4156
745
}
4157
/* }}} */
4158
4159
static zend_result zend_compile_func_typecheck(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4160
1.65k
{
4161
1.65k
  znode arg_node;
4162
1.65k
  zend_op *opline;
4163
4164
1.65k
  if (args->children != 1) {
4165
459
    return FAILURE;
4166
459
  }
4167
4168
1.19k
  zend_compile_expr(&arg_node, args->child[0]);
4169
1.19k
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4170
1.19k
  if (type != _IS_BOOL) {
4171
1.16k
    opline->extended_value = (1 << type);
4172
1.16k
  } else {
4173
29
    opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE);
4174
29
  }
4175
1.19k
  return SUCCESS;
4176
1.65k
}
4177
/* }}} */
4178
4179
static zend_result zend_compile_func_is_scalar(znode *result, const zend_ast_list *args) /* {{{ */
4180
179
{
4181
179
  znode arg_node;
4182
179
  zend_op *opline;
4183
4184
179
  if (args->children != 1) {
4185
9
    return FAILURE;
4186
9
  }
4187
4188
170
  zend_compile_expr(&arg_node, args->child[0]);
4189
170
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4190
170
  opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING);
4191
170
  return SUCCESS;
4192
179
}
4193
4194
static zend_result zend_compile_func_cast(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4195
1.04k
{
4196
1.04k
  znode arg_node;
4197
1.04k
  zend_op *opline;
4198
4199
1.04k
  if (args->children != 1) {
4200
263
    return FAILURE;
4201
263
  }
4202
4203
780
  zend_compile_expr(&arg_node, args->child[0]);
4204
780
  if (type == _IS_BOOL) {
4205
193
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL);
4206
587
  } else {
4207
587
    opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
4208
587
    opline->extended_value = type;
4209
587
  }
4210
780
  return SUCCESS;
4211
1.04k
}
4212
/* }}} */
4213
4214
static zend_result zend_compile_func_defined(znode *result, const zend_ast_list *args) /* {{{ */
4215
1.75k
{
4216
1.75k
  zend_string *name;
4217
1.75k
  zend_op *opline;
4218
4219
1.75k
  if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
4220
237
    return FAILURE;
4221
237
  }
4222
4223
1.51k
  name = zval_get_string(zend_ast_get_zval(args->child[0]));
4224
1.51k
  if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
4225
729
    zend_string_release_ex(name, 0);
4226
729
    return FAILURE;
4227
729
  }
4228
4229
790
  if (zend_try_ct_eval_const(&result->u.constant, name, false)) {
4230
40
    zend_string_release_ex(name, 0);
4231
40
    zval_ptr_dtor(&result->u.constant);
4232
40
    ZVAL_TRUE(&result->u.constant);
4233
40
    result->op_type = IS_CONST;
4234
40
    return SUCCESS;
4235
40
  }
4236
4237
750
  opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
4238
750
  opline->op1_type = IS_CONST;
4239
750
  LITERAL_STR(opline->op1, name);
4240
750
  opline->extended_value = zend_alloc_cache_slot();
4241
4242
750
  return SUCCESS;
4243
790
}
4244
/* }}} */
4245
4246
static zend_result zend_compile_func_chr(znode *result, const zend_ast_list *args) /* {{{ */
4247
1.20k
{
4248
1.20k
  zval *zint;
4249
1.20k
  if (
4250
1.20k
    args->children == 1
4251
1.02k
    && args->child[0]->kind == ZEND_AST_ZVAL
4252
833
    && (zint = zend_ast_get_zval(args->child[0]))
4253
833
    && Z_TYPE_P(zint) == IS_LONG
4254
782
    && Z_LVAL_P(zint) >= 0
4255
782
    && Z_LVAL_P(zint) <= 255
4256
1.20k
  ) {
4257
493
    result->op_type = IS_CONST;
4258
493
    ZVAL_CHAR(&result->u.constant, Z_LVAL_P(zint));
4259
493
    return SUCCESS;
4260
713
  } else {
4261
713
    return FAILURE;
4262
713
  }
4263
1.20k
}
4264
/* }}} */
4265
4266
static zend_result zend_compile_func_ord(znode *result, const zend_ast_list *args) /* {{{ */
4267
529
{
4268
529
  zval *str;
4269
529
  if (
4270
529
    args->children == 1
4271
332
    && args->child[0]->kind == ZEND_AST_ZVAL
4272
220
    && (str = zend_ast_get_zval(args->child[0]))
4273
220
    && Z_TYPE_P(str) == IS_STRING
4274
156
    && Z_STRLEN_P(str) == 1
4275
529
  ) {
4276
9
    result->op_type = IS_CONST;
4277
9
    ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(str)[0]);
4278
9
    return SUCCESS;
4279
520
  } else {
4280
520
    return FAILURE;
4281
520
  }
4282
529
}
4283
/* }}} */
4284
4285
/* We can only calculate the stack size for functions that have been fully compiled, otherwise
4286
 * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for
4287
 * directly or indirectly recursive function calls. */
4288
160k
static bool fbc_is_finalized(const zend_function *fbc) {
4289
160k
  return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO);
4290
160k
}
4291
4292
static bool zend_compile_ignore_class(const zend_class_entry *ce, const zend_string *filename)
4293
60.3k
{
4294
60.3k
  if (ce->type == ZEND_INTERNAL_CLASS) {
4295
40.6k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES;
4296
40.6k
  } else {
4297
19.7k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4298
15.3k
      && ce->info.user.filename != filename;
4299
19.7k
  }
4300
60.3k
}
4301
4302
static bool zend_compile_ignore_function(const zend_function *fbc, const zend_string *filename)
4303
146k
{
4304
146k
  if (fbc->type == ZEND_INTERNAL_FUNCTION) {
4305
123k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS;
4306
123k
  } else {
4307
22.4k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)
4308
22.4k
      || ((CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4309
18.1k
        && fbc->op_array.filename != filename);
4310
22.4k
  }
4311
146k
}
4312
4313
static zend_result zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
4314
6.65k
{
4315
6.65k
  zend_string *name, *lcname;
4316
6.65k
  zend_function *fbc;
4317
6.65k
  zend_op *opline;
4318
4319
6.65k
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
4320
2.27k
    return FAILURE;
4321
2.27k
  }
4322
4323
4.37k
  name = zend_ast_get_str(name_ast);
4324
4.37k
  lcname = zend_string_tolower(name);
4325
4326
4.37k
  fbc = zend_hash_find_ptr(CG(function_table), lcname);
4327
4.37k
  if (!fbc
4328
639
   || !fbc_is_finalized(fbc)
4329
3.73k
   || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
4330
3.73k
    zend_string_release_ex(lcname, 0);
4331
3.73k
    return FAILURE;
4332
3.73k
  }
4333
4334
639
  opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
4335
639
  opline->extended_value = num_args;
4336
639
  opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
4337
639
  opline->op2_type = IS_CONST;
4338
639
  LITERAL_STR(opline->op2, lcname);
4339
639
  opline->result.num = zend_alloc_cache_slot();
4340
4341
639
  return SUCCESS;
4342
4.37k
}
4343
/* }}} */
4344
4345
static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
4346
6.65k
{
4347
6.65k
  zend_op *opline;
4348
6.65k
  znode name_node;
4349
4350
6.65k
  if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
4351
639
    return;
4352
639
  }
4353
4354
6.01k
  zend_compile_expr(&name_node, name_ast);
4355
4356
6.01k
  opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
4357
6.01k
  opline->op1_type = IS_CONST;
4358
6.01k
  LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
4359
6.01k
  opline->extended_value = num_args;
4360
6.01k
}
4361
/* }}} */
4362
4363
/* cufa = call_user_func_array */
4364
static zend_result zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t type) /* {{{ */
4365
1.50k
{
4366
1.50k
  znode arg_node;
4367
1.50k
  zend_op *opline;
4368
4369
1.50k
  if (args->children != 2) {
4370
24
    return FAILURE;
4371
24
  }
4372
4373
1.47k
  zend_compile_init_user_func(args->child[0], 0, lcname);
4374
1.47k
  if (args->child[1]->kind == ZEND_AST_CALL
4375
1.02k
   && args->child[1]->child[0]->kind == ZEND_AST_ZVAL
4376
991
   && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING
4377
964
   && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) {
4378
950
    zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]);
4379
950
    zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]);
4380
950
    bool is_fully_qualified;
4381
950
    zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified);
4382
4383
950
    if (zend_string_equals_literal_ci(name, "array_slice")
4384
620
       && !zend_args_contain_unpack_or_named(list)
4385
562
     && list->children == 3
4386
322
     && list->child[1]->kind == ZEND_AST_ZVAL) {
4387
271
      zval *zv = zend_ast_get_zval(list->child[1]);
4388
4389
271
      if (Z_TYPE_P(zv) == IS_LONG
4390
261
       && Z_LVAL_P(zv) >= 0
4391
261
       && Z_LVAL_P(zv) <= 0x7fffffff) {
4392
126
        zend_op *opline;
4393
126
        znode len_node;
4394
4395
126
        zend_compile_expr(&arg_node, list->child[0]);
4396
126
        zend_compile_expr(&len_node, list->child[2]);
4397
126
        opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node);
4398
126
        opline->extended_value = Z_LVAL_P(zv);
4399
126
        opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4400
126
        if (type == BP_VAR_R || type == BP_VAR_IS) {
4401
126
          opline->result_type = IS_TMP_VAR;
4402
126
          result->op_type = IS_TMP_VAR;
4403
126
        }
4404
126
        zend_string_release_ex(name, 0);
4405
126
        return SUCCESS;
4406
126
      }
4407
271
    }
4408
824
    zend_string_release_ex(name, 0);
4409
824
  }
4410
1.35k
  zend_compile_expr(&arg_node, args->child[1]);
4411
1.35k
  zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
4412
1.35k
  zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
4413
1.35k
  opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4414
1.35k
  if (type == BP_VAR_R || type == BP_VAR_IS) {
4415
1.25k
    opline->result_type = IS_TMP_VAR;
4416
1.25k
    result->op_type = IS_TMP_VAR;
4417
1.25k
  }
4418
1.35k
  opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4419
4420
1.35k
  return SUCCESS;
4421
1.47k
}
4422
/* }}} */
4423
4424
/* cuf = call_user_func */
4425
static zend_result zend_compile_func_cuf(znode *result, const zend_ast_list *args, zend_string *lcname, uint32_t type) /* {{{ */
4426
5.20k
{
4427
5.20k
  uint32_t i;
4428
4429
5.20k
  if (args->children < 1) {
4430
33
    return FAILURE;
4431
33
  }
4432
4433
5.17k
  zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
4434
9.08k
  for (i = 1; i < args->children; ++i) {
4435
3.91k
    zend_ast *arg_ast = args->child[i];
4436
3.91k
    znode arg_node;
4437
3.91k
    zend_op *opline;
4438
4439
3.91k
    zend_compile_expr(&arg_node, arg_ast);
4440
4441
3.91k
    opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
4442
3.91k
    opline->op2.num = i;
4443
3.91k
    opline->result.var = EX_NUM_TO_VAR(i - 1);
4444
3.91k
  }
4445
5.17k
  zend_op *opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4446
5.17k
  if (type == BP_VAR_R || type == BP_VAR_IS) {
4447
809
    opline->result_type = IS_TMP_VAR;
4448
809
    result->op_type = IS_TMP_VAR;
4449
809
  }
4450
4451
5.17k
  return SUCCESS;
4452
5.20k
}
4453
/* }}} */
4454
4455
static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, const zend_function *fbc, uint32_t lineno, uint32_t type) /* {{{ */
4456
54.8k
{
4457
54.8k
  if (EG(assertions) >= 0) {
4458
54.8k
    znode name_node;
4459
54.8k
    zend_op *opline;
4460
54.8k
    uint32_t check_op_number = get_next_op_number();
4461
4462
54.8k
    zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
4463
4464
54.8k
    if (fbc && fbc_is_finalized(fbc)) {
4465
13.8k
      name_node.op_type = IS_CONST;
4466
13.8k
      ZVAL_STR_COPY(&name_node.u.constant, name);
4467
4468
13.8k
      opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
4469
40.9k
    } else {
4470
40.9k
      opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
4471
40.9k
      opline->op2_type = IS_CONST;
4472
40.9k
      opline->op2.constant = zend_add_ns_func_name_literal(name);
4473
40.9k
    }
4474
54.8k
    opline->result.num = zend_alloc_cache_slot();
4475
4476
54.8k
    if (args->children == 1) {
4477
      /* add "assert(condition) as assertion message */
4478
13.9k
      zend_ast *arg = zend_ast_create_zval_from_str(
4479
13.9k
        zend_ast_export("assert(", args->child[0], ")"));
4480
13.9k
      if (args->child[0]->kind == ZEND_AST_NAMED_ARG) {
4481
        /* If the original argument was named, add the new argument as named as well,
4482
         * as mixing named and positional is not allowed. */
4483
341
        zend_ast *name = zend_ast_create_zval_from_str(
4484
341
          ZSTR_INIT_LITERAL("description", 0));
4485
341
        arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg);
4486
341
      }
4487
13.9k
      args = (zend_ast_list *)zend_ast_list_add((zend_ast *) args, arg);
4488
13.9k
    }
4489
4490
54.8k
    zend_compile_call_common(result, (zend_ast*)args, fbc, lineno, type);
4491
4492
54.8k
    opline = &CG(active_op_array)->opcodes[check_op_number];
4493
54.8k
    opline->op2.opline_num = get_next_op_number();
4494
54.8k
    SET_NODE(opline->result, result);
4495
54.8k
  } else {
4496
0
    if (!fbc) {
4497
0
      zend_string_release_ex(name, 0);
4498
0
    }
4499
0
    result->op_type = IS_CONST;
4500
0
    ZVAL_TRUE(&result->u.constant);
4501
0
  }
4502
54.8k
}
4503
/* }}} */
4504
4505
static zend_result zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */
4506
9.75k
{
4507
9.75k
  bool strict = false;
4508
9.75k
  znode array, needly;
4509
9.75k
  zend_op *opline;
4510
4511
9.75k
  if (args->children == 3) {
4512
8.39k
    if (args->child[2]->kind == ZEND_AST_ZVAL) {
4513
607
      strict = zend_is_true(zend_ast_get_zval(args->child[2]));
4514
7.78k
    } else if (args->child[2]->kind == ZEND_AST_CONST) {
4515
333
      zval value;
4516
333
      zend_ast *name_ast = args->child[2]->child[0];
4517
333
      bool is_fully_qualified;
4518
333
      zend_string *resolved_name = zend_resolve_const_name(
4519
333
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
4520
4521
333
      if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) {
4522
188
        zend_string_release_ex(resolved_name, 0);
4523
188
        return FAILURE;
4524
188
      }
4525
4526
145
      zend_string_release_ex(resolved_name, 0);
4527
145
      strict = zend_is_true(&value);
4528
145
      zval_ptr_dtor(&value);
4529
7.45k
    } else {
4530
7.45k
      return FAILURE;
4531
7.45k
    }
4532
8.39k
  } else if (args->children != 2) {
4533
146
    return FAILURE;
4534
146
  }
4535
4536
1.96k
  if (args->child[1]->kind != ZEND_AST_ARRAY
4537
1.55k
   || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) {
4538
488
    return FAILURE;
4539
488
  }
4540
4541
1.47k
  if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) {
4542
1.34k
    bool ok = true;
4543
1.34k
    zval *val, tmp;
4544
1.34k
    HashTable *src = Z_ARRVAL(array.u.constant);
4545
1.34k
    HashTable *dst = zend_new_array(zend_hash_num_elements(src));
4546
4547
1.34k
    ZVAL_TRUE(&tmp);
4548
4549
1.34k
    if (strict) {
4550
4.00k
      ZEND_HASH_FOREACH_VAL(src, val) {
4551
4.00k
        if (Z_TYPE_P(val) == IS_STRING) {
4552
13
          zend_hash_add(dst, Z_STR_P(val), &tmp);
4553
1.13k
        } else if (Z_TYPE_P(val) == IS_LONG) {
4554
1.08k
          zend_hash_index_add(dst, Z_LVAL_P(val), &tmp);
4555
1.08k
        } else {
4556
42
          zend_array_destroy(dst);
4557
42
          ok = false;
4558
42
          break;
4559
42
        }
4560
4.00k
      } ZEND_HASH_FOREACH_END();
4561
760
    } else {
4562
3.37k
      ZEND_HASH_FOREACH_VAL(src, val) {
4563
3.37k
        if (Z_TYPE_P(val) != IS_STRING
4564
543
         || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) {
4565
543
          zend_array_destroy(dst);
4566
543
          ok = false;
4567
543
          break;
4568
543
        }
4569
333
        zend_hash_add(dst, Z_STR_P(val), &tmp);
4570
333
      } ZEND_HASH_FOREACH_END();
4571
760
    }
4572
4573
1.34k
    zend_array_destroy(src);
4574
1.34k
    if (!ok) {
4575
585
      return FAILURE;
4576
585
    }
4577
764
    Z_ARRVAL(array.u.constant) = dst;
4578
764
  }
4579
892
  array.op_type = IS_CONST;
4580
4581
892
  zend_compile_expr(&needly, args->child[0]);
4582
4583
892
  opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array);
4584
892
  opline->extended_value = strict;
4585
4586
892
  return SUCCESS;
4587
1.47k
}
4588
/* }}} */
4589
4590
static zend_result zend_compile_func_count(znode *result, const zend_ast_list *args, const zend_string *lcname) /* {{{ */
4591
971
{
4592
971
  znode arg_node;
4593
971
  zend_op *opline;
4594
4595
971
  if (args->children != 1) {
4596
63
    return FAILURE;
4597
63
  }
4598
4599
908
  zend_compile_expr(&arg_node, args->child[0]);
4600
908
  opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL);
4601
908
  opline->extended_value = zend_string_equals_literal(lcname, "sizeof");
4602
4603
908
  return SUCCESS;
4604
971
}
4605
/* }}} */
4606
4607
static zend_result zend_compile_func_get_class(znode *result, const zend_ast_list *args) /* {{{ */
4608
1.33k
{
4609
1.33k
  if (args->children == 0) {
4610
196
    zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL);
4611
1.14k
  } else {
4612
1.14k
    znode arg_node;
4613
4614
1.14k
    if (args->children != 1) {
4615
206
      return FAILURE;
4616
206
    }
4617
4618
937
    zend_compile_expr(&arg_node, args->child[0]);
4619
937
    zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL);
4620
937
  }
4621
1.13k
  return SUCCESS;
4622
1.33k
}
4623
/* }}} */
4624
4625
static zend_result zend_compile_func_get_called_class(znode *result, const zend_ast_list *args) /* {{{ */
4626
157
{
4627
157
  if (args->children != 0) {
4628
48
    return FAILURE;
4629
48
  }
4630
4631
109
  zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL);
4632
109
  return SUCCESS;
4633
157
}
4634
/* }}} */
4635
4636
static zend_result zend_compile_func_gettype(znode *result, const zend_ast_list *args) /* {{{ */
4637
457
{
4638
457
  znode arg_node;
4639
4640
457
  if (args->children != 1) {
4641
6
    return FAILURE;
4642
6
  }
4643
4644
451
  zend_compile_expr(&arg_node, args->child[0]);
4645
451
  zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL);
4646
451
  return SUCCESS;
4647
457
}
4648
/* }}} */
4649
4650
static zend_result zend_compile_func_num_args(znode *result, const zend_ast_list *args) /* {{{ */
4651
188
{
4652
188
  if (CG(active_op_array)->function_name && args->children == 0) {
4653
56
    zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL);
4654
56
    return SUCCESS;
4655
132
  } else {
4656
132
    return FAILURE;
4657
132
  }
4658
188
}
4659
/* }}} */
4660
4661
static zend_result zend_compile_func_get_args(znode *result, const zend_ast_list *args) /* {{{ */
4662
544
{
4663
544
  if (CG(active_op_array)->function_name && args->children == 0) {
4664
249
    zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL);
4665
249
    return SUCCESS;
4666
295
  } else {
4667
295
    return FAILURE;
4668
295
  }
4669
544
}
4670
/* }}} */
4671
4672
static zend_result zend_compile_func_array_key_exists(znode *result, const zend_ast_list *args) /* {{{ */
4673
244
{
4674
244
  znode subject, needle;
4675
4676
244
  if (args->children != 2) {
4677
75
    return FAILURE;
4678
75
  }
4679
4680
169
  zend_compile_expr(&needle, args->child[0]);
4681
169
  zend_compile_expr(&subject, args->child[1]);
4682
4683
169
  zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject);
4684
169
  return SUCCESS;
4685
244
}
4686
/* }}} */
4687
4688
static zend_result zend_compile_func_array_slice(znode *result, const zend_ast_list *args) /* {{{ */
4689
1.45k
{
4690
1.45k
  if (CG(active_op_array)->function_name
4691
730
   && args->children == 2
4692
335
   && args->child[0]->kind == ZEND_AST_CALL
4693
247
   && args->child[0]->child[0]->kind == ZEND_AST_ZVAL
4694
210
   && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING
4695
210
   && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST
4696
209
   && args->child[1]->kind == ZEND_AST_ZVAL) {
4697
4698
197
    zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]);
4699
197
    bool is_fully_qualified;
4700
197
    zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified);
4701
197
    const zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]);
4702
197
    const zval *zv = zend_ast_get_zval(args->child[1]);
4703
197
    znode first;
4704
4705
197
    if (zend_string_equals_literal_ci(name, "func_get_args")
4706
114
     && list->children == 0
4707
96
     && Z_TYPE_P(zv) == IS_LONG
4708
49
     && Z_LVAL_P(zv) >= 0) {
4709
49
      first.op_type = IS_CONST;
4710
49
      ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv));
4711
49
      zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL);
4712
49
      zend_string_release_ex(name, 0);
4713
49
      return SUCCESS;
4714
49
    }
4715
148
    zend_string_release_ex(name, 0);
4716
148
  }
4717
1.40k
  return FAILURE;
4718
1.45k
}
4719
/* }}} */
4720
4721
static uint32_t find_frameless_function_offset(uint32_t arity, const void *handler)
4722
963k
{
4723
963k
  void **handlers = zend_flf_handlers;
4724
963k
  void **current = handlers;
4725
8.75M
  while (current) {
4726
8.75M
    if (*current == handler) {
4727
963k
      return current - handlers;
4728
963k
    }
4729
7.79M
    current++;
4730
7.79M
  }
4731
4732
0
  return (uint32_t)-1;
4733
963k
}
4734
4735
static const zend_frameless_function_info *find_frameless_function_info(const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4736
615k
{
4737
615k
  if (zend_execute_internal) {
4738
87.6k
    return NULL;
4739
87.6k
  }
4740
4741
528k
  if (ZEND_USER_CODE(fbc->type)) {
4742
3
    return NULL;
4743
3
  }
4744
4745
528k
  const zend_frameless_function_info *frameless_function_info = fbc->internal_function.frameless_function_infos;
4746
528k
  if (!frameless_function_info) {
4747
22.9k
    return NULL;
4748
22.9k
  }
4749
4750
505k
  if (args->children > 3) {
4751
165
    return NULL;
4752
165
  }
4753
4754
663k
  while (frameless_function_info->handler) {
4755
639k
    if (frameless_function_info->num_args >= args->children
4756
527k
     && fbc->common.required_num_args <= args->children
4757
482k
     && (!(fbc->common.fn_flags & ZEND_ACC_VARIADIC)
4758
482k
      || frameless_function_info->num_args == args->children)) {
4759
482k
      uint32_t num_args = frameless_function_info->num_args;
4760
482k
      uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4761
482k
      if (offset == (uint32_t)-1) {
4762
0
        continue;
4763
0
      }
4764
482k
      return frameless_function_info;
4765
482k
    }
4766
157k
    frameless_function_info++;
4767
157k
  }
4768
4769
23.0k
  return NULL;
4770
505k
}
4771
4772
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)
4773
481k
{
4774
481k
  uint32_t lineno = CG(zend_lineno);
4775
481k
  uint32_t num_args = frameless_function_info->num_args;
4776
481k
  uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4777
481k
  znode arg_zvs[3];
4778
1.54M
  for (uint32_t i = 0; i < num_args; i++) {
4779
1.06M
    if (i < args->children) {
4780
1.06M
      zend_compile_expr(&arg_zvs[i], args->child[i]);
4781
1.06M
    } else {
4782
0
      const zend_arg_info *arg_info = &fbc->common.arg_info[i];
4783
0
      arg_zvs[i].op_type = IS_CONST;
4784
0
      if (zend_get_default_from_internal_arg_info(&arg_zvs[i].u.constant, arg_info) == FAILURE) {
4785
0
        ZEND_UNREACHABLE();
4786
0
      }
4787
0
    }
4788
1.06M
  }
4789
481k
  uint8_t opcode = ZEND_FRAMELESS_ICALL_0 + num_args;
4790
481k
  uint32_t opnum = get_next_op_number();
4791
481k
  zend_op *opline = zend_emit_op_tmp(result, opcode, NULL, NULL);
4792
481k
  opline->extended_value = offset;
4793
481k
  opline->lineno = lineno;
4794
481k
  if (num_args >= 1) {
4795
481k
    SET_NODE(opline->op1, &arg_zvs[0]);
4796
481k
  }
4797
481k
  if (num_args >= 2) {
4798
471k
    SET_NODE(opline->op2, &arg_zvs[1]);
4799
471k
  }
4800
481k
  if (num_args >= 3) {
4801
112k
    zend_emit_op_data(&arg_zvs[2]);
4802
112k
  }
4803
481k
  return opnum;
4804
481k
}
4805
4806
static uint32_t zend_compile_frameless_icall(znode *result, const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4807
103k
{
4808
103k
  const zend_frameless_function_info *frameless_function_info = find_frameless_function_info(args, fbc, type);
4809
103k
  if (!frameless_function_info) {
4810
95.0k
    return (uint32_t)-1;
4811
95.0k
  }
4812
4813
8.80k
  return zend_compile_frameless_icall_ex(result, args, fbc, frameless_function_info, type);
4814
103k
}
4815
4816
static void zend_compile_ns_call(znode *result, const znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */
4817
2.38M
{
4818
2.38M
  int name_constants = zend_add_ns_func_name_literal(Z_STR(name_node->u.constant));
4819
4820
  /* Find frameless function with same name. */
4821
2.38M
  const zend_function *frameless_function = NULL;
4822
2.38M
  if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT
4823
2.38M
   && !zend_args_contain_unpack_or_named(zend_ast_get_list(args_ast))
4824
   /* Avoid blowing up op count with nested frameless branches. */
4825
2.36M
   && !CG(context).in_jmp_frameless_branch) {
4826
1.45M
    zend_string *lc_func_name = Z_STR_P(CT_CONSTANT_EX(CG(active_op_array), name_constants + 2));
4827
1.45M
    frameless_function = zend_hash_find_ptr(CG(function_table), lc_func_name);
4828
1.45M
  }
4829
4830
  /* Check whether any frameless handler may actually be used. */
4831
2.38M
  uint32_t jmp_fl_opnum = 0;
4832
2.38M
  const zend_frameless_function_info *frameless_function_info = NULL;
4833
2.38M
  if (frameless_function) {
4834
511k
    frameless_function_info = find_frameless_function_info(zend_ast_get_list(args_ast), frameless_function, type);
4835
511k
    if (frameless_function_info) {
4836
473k
      CG(context).in_jmp_frameless_branch = true;
4837
473k
      znode op1;
4838
473k
      op1.op_type = IS_CONST;
4839
473k
      ZVAL_COPY(&op1.u.constant, CT_CONSTANT_EX(CG(active_op_array), name_constants + 1));
4840
473k
      jmp_fl_opnum = get_next_op_number();
4841
473k
      zend_emit_op(NULL, ZEND_JMP_FRAMELESS, &op1, NULL);
4842
473k
    }
4843
511k
  }
4844
4845
  /* Compile ns call. */
4846
2.38M
  zend_op *opline = get_next_op();
4847
2.38M
  opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
4848
2.38M
  opline->op2_type = IS_CONST;
4849
2.38M
  opline->op2.constant = name_constants;
4850
2.38M
  opline->result.num = zend_alloc_cache_slot();
4851
2.38M
  zend_compile_call_common(result, args_ast, NULL, lineno, type);
4852
4853
  /* Compile frameless call. */
4854
2.38M
  if (frameless_function_info) {
4855
473k
    CG(zend_lineno) = lineno;
4856
4857
473k
    uint32_t jmp_end_opnum = zend_emit_jump(0);
4858
473k
    uint32_t jmp_fl_target = get_next_op_number();
4859
4860
473k
    uint32_t flf_icall_opnum = zend_compile_frameless_icall_ex(NULL, zend_ast_get_list(args_ast), frameless_function, frameless_function_info, type);
4861
4862
473k
    zend_op *jmp_fl = &CG(active_op_array)->opcodes[jmp_fl_opnum];
4863
473k
    jmp_fl->op2.opline_num = jmp_fl_target;
4864
473k
    jmp_fl->extended_value = zend_alloc_cache_slot();
4865
473k
    zend_op *flf_icall = &CG(active_op_array)->opcodes[flf_icall_opnum];
4866
473k
    SET_NODE(flf_icall->result, result);
4867
473k
    zend_update_jump_target_to_next(jmp_end_opnum);
4868
4869
473k
    CG(context).in_jmp_frameless_branch = false;
4870
473k
  }
4871
2.38M
}
4872
/* }}} */
4873
4874
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node);
4875
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node);
4876
static void zend_compile_rope_finalize(znode *result, uint32_t j, zend_op *init_opline, zend_op *opline);
4877
4878
static zend_result zend_compile_func_sprintf(znode *result, zend_ast_list *args) /* {{{ */
4879
3.69k
{
4880
  /* Bail out if we do not have a format string. */
4881
3.69k
  if (args->children < 1) {
4882
213
    return FAILURE;
4883
213
  }
4884
4885
3.48k
  zend_eval_const_expr(&args->child[0]);
4886
  /* Bail out if the format string is not constant. */
4887
3.48k
  if (args->child[0]->kind != ZEND_AST_ZVAL) {
4888
261
    return FAILURE;
4889
261
  }
4890
4891
3.22k
  zval *format_string = zend_ast_get_zval(args->child[0]);
4892
3.22k
  if (Z_TYPE_P(format_string) != IS_STRING) {
4893
24
    return FAILURE;
4894
24
  }
4895
3.20k
  if (Z_STRLEN_P(format_string) >= 256) {
4896
53
    return FAILURE;
4897
53
  }
4898
4899
3.14k
  char *p;
4900
3.14k
  char *end;
4901
3.14k
  uint32_t placeholder_count;
4902
4903
3.14k
  placeholder_count = 0;
4904
3.14k
  p = Z_STRVAL_P(format_string);
4905
3.14k
  end = p + Z_STRLEN_P(format_string);
4906
4907
6.67k
  for (;;) {
4908
6.67k
    p = memchr(p, '%', end - p);
4909
6.67k
    if (!p) {
4910
2.76k
      break;
4911
2.76k
    }
4912
4913
3.91k
    char *q = p + 1;
4914
3.91k
    if (q == end) {
4915
202
      return FAILURE;
4916
202
    }
4917
4918
3.71k
    switch (*q) {
4919
2.83k
      case 's':
4920
3.15k
      case 'd':
4921
3.15k
        placeholder_count++;
4922
3.15k
        break;
4923
379
      case '%':
4924
379
        break;
4925
181
      default:
4926
181
        return FAILURE;
4927
3.71k
    }
4928
4929
3.52k
    p = q;
4930
3.52k
    p++;
4931
3.52k
  }
4932
4933
  /* Bail out if the number of placeholders does not match the number of values. */
4934
2.76k
  if (placeholder_count != (args->children - 1)) {
4935
126
    return FAILURE;
4936
126
  }
4937
4938
  /* Handle empty format strings. */
4939
2.63k
  if (Z_STRLEN_P(format_string) == 0) {
4940
63
    result->op_type = IS_CONST;
4941
63
    ZVAL_EMPTY_STRING(&result->u.constant);
4942
4943
63
    return SUCCESS;
4944
63
  }
4945
4946
2.57k
  znode *elements = NULL;
4947
4948
2.57k
  if (placeholder_count > 0) {
4949
2.06k
    elements = safe_emalloc(sizeof(*elements), placeholder_count, 0);
4950
2.06k
  }
4951
4952
  /* Compile the value expressions first for error handling that is consistent
4953
   * with a function call: Values that fail to convert to a string may emit errors.
4954
   */
4955
5.55k
  for (uint32_t i = 0; i < placeholder_count; i++) {
4956
2.97k
    zend_compile_expr(elements + i, args->child[1 + i]);
4957
2.97k
  }
4958
4959
2.57k
  uint32_t rope_elements = 0;
4960
2.57k
  uint32_t rope_init_lineno = -1;
4961
2.57k
  zend_op *opline = NULL;
4962
4963
2.57k
  placeholder_count = 0;
4964
2.57k
  p = Z_STRVAL_P(format_string);
4965
2.57k
  end = p + Z_STRLEN_P(format_string);
4966
2.57k
  char *offset = p;
4967
5.71k
  for (;;) {
4968
5.71k
    p = memchr(p, '%', end - p);
4969
5.71k
    if (!p) {
4970
2.57k
      break;
4971
2.57k
    }
4972
4973
3.13k
    char *q = p + 1;
4974
3.13k
    ZEND_ASSERT(q < end);
4975
3.13k
    ZEND_ASSERT(*q == 's' || *q == 'd' || *q == '%');
4976
4977
3.13k
    if (*q == '%') {
4978
      /* Optimization to not create a dedicated rope element for the literal '%':
4979
       * Include the first '%' within the "constant" part instead of dropping the
4980
       * full placeholder.
4981
       */
4982
159
      p++;
4983
159
    }
4984
4985
3.13k
    if (p != offset) {
4986
2.16k
      znode const_node;
4987
2.16k
      const_node.op_type = IS_CONST;
4988
2.16k
      ZVAL_STRINGL(&const_node.u.constant, offset, p - offset);
4989
2.16k
      if (rope_elements == 0) {
4990
1.22k
        rope_init_lineno = get_next_op_number();
4991
1.22k
      }
4992
2.16k
      opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4993
2.16k
    }
4994
4995
3.13k
    if (*q != '%') {
4996
2.97k
      switch (*q) {
4997
2.73k
        case 's':
4998
          /* Perform the cast of constants when actually evaluating the corresponding placeholder
4999
           * for correct error reporting.
5000
           */
5001
2.73k
          if (elements[placeholder_count].op_type == IS_CONST) {
5002
461
            if (Z_TYPE(elements[placeholder_count].u.constant) == IS_ARRAY) {
5003
69
              zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_STRING;
5004
392
            } else {
5005
392
              convert_to_string(&elements[placeholder_count].u.constant);
5006
392
            }
5007
461
          }
5008
2.73k
          break;
5009
241
        case 'd':
5010
241
          zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_LONG;
5011
241
          break;
5012
0
        EMPTY_SWITCH_DEFAULT_CASE();
5013
2.97k
      }
5014
5015
2.97k
      if (rope_elements == 0) {
5016
889
        rope_init_lineno = get_next_op_number();
5017
889
      }
5018
2.97k
      opline = zend_compile_rope_add(result, rope_elements++, &elements[placeholder_count]);
5019
5020
2.97k
      placeholder_count++;
5021
2.97k
    }
5022
5023
3.13k
    p = q;
5024
3.13k
    p++;
5025
3.13k
    offset = p;
5026
3.13k
  }
5027
2.57k
  if (end != offset) {
5028
    /* Add the constant part after the last placeholder. */
5029
2.41k
    znode const_node;
5030
2.41k
    const_node.op_type = IS_CONST;
5031
2.41k
    ZVAL_STRINGL(&const_node.u.constant, offset, end - offset);
5032
2.41k
    if (rope_elements == 0) {
5033
460
      rope_init_lineno = get_next_op_number();
5034
460
    }
5035
2.41k
    opline = zend_compile_rope_add(result, rope_elements++, &const_node);
5036
2.41k
  }
5037
2.57k
  ZEND_ASSERT(opline != NULL);
5038
5039
2.57k
  zend_op *init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
5040
2.57k
  zend_compile_rope_finalize(result, rope_elements, init_opline, opline);
5041
2.57k
  efree(elements);
5042
5043
2.57k
  return SUCCESS;
5044
2.57k
}
5045
5046
static zend_result zend_compile_func_printf(znode *result, zend_ast_list *args) /* {{{ */
5047
2.64k
{
5048
  /* Special case: printf with a single constant string argument and no format specifiers.
5049
   * In this case, just emit ECHO and return the string length if needed. */
5050
2.64k
  if (args->children == 1) {
5051
697
    zend_eval_const_expr(&args->child[0]);
5052
697
    if (args->child[0]->kind != ZEND_AST_ZVAL) {
5053
34
      return FAILURE;
5054
34
    }
5055
663
    zval *format_string = zend_ast_get_zval(args->child[0]);
5056
663
    if (Z_TYPE_P(format_string) != IS_STRING) {
5057
74
      return FAILURE;
5058
74
    }
5059
    /* Check if there are any format specifiers */
5060
589
    if (!memchr(Z_STRVAL_P(format_string), '%', Z_STRLEN_P(format_string))) {
5061
      /* No format specifiers - just emit ECHO and return string length */
5062
351
      znode format_node;
5063
351
      zend_compile_expr(&format_node, args->child[0]);
5064
351
      zend_emit_op(NULL, ZEND_ECHO, &format_node, NULL);
5065
5066
      /* Return the string length as a constant if the result is used */
5067
351
      result->op_type = IS_CONST;
5068
351
      ZVAL_LONG(&result->u.constant, Z_STRLEN_P(format_string));
5069
351
      return SUCCESS;
5070
351
    }
5071
589
  }
5072
5073
  /* Fall back to sprintf optimization for format strings with specifiers */
5074
2.18k
  znode rope_result;
5075
2.18k
  if (zend_compile_func_sprintf(&rope_result, args) != SUCCESS) {
5076
341
    return FAILURE;
5077
341
  }
5078
5079
  /* printf() returns the amount of bytes written, so just an ECHO of the
5080
   * resulting sprintf() optimisation might not be enough. At this early
5081
   * stage we can't detect if the result is actually used, so we just emit
5082
   * the opcodes and let them be cleaned up by the dead code elimination
5083
   * pass in the Zend Optimizer if the result of the printf() is in fact
5084
   * unused */
5085
1.84k
  znode copy;
5086
1.84k
  if (rope_result.op_type != IS_CONST) {
5087
    /* Note: ZEND_COPY_TMP is only valid for TMPVAR. */
5088
1.75k
    ZEND_ASSERT(rope_result.op_type == IS_TMP_VAR);
5089
1.75k
    zend_emit_op_tmp(&copy, ZEND_COPY_TMP, &rope_result, NULL);
5090
1.75k
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5091
1.75k
    zend_emit_op_tmp(result, ZEND_STRLEN, &copy, NULL);
5092
1.75k
  } else {
5093
91
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5094
91
    result->op_type = IS_CONST;
5095
91
    ZVAL_LONG(&result->u.constant, Z_STRLEN(rope_result.u.constant));
5096
91
  }
5097
5098
1.84k
  return SUCCESS;
5099
1.84k
}
5100
5101
static zend_result zend_compile_func_clone(znode *result, const zend_ast_list *args)
5102
989
{
5103
989
  znode arg_node;
5104
5105
989
  if (args->children != 1) {
5106
310
    return FAILURE;
5107
310
  }
5108
5109
679
  zend_compile_expr(&arg_node, args->child[0]);
5110
679
  zend_emit_op_tmp(result, ZEND_CLONE, &arg_node, NULL);
5111
5112
679
  return SUCCESS;
5113
989
}
5114
5115
static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t lineno) /* {{{ */
5116
354
{
5117
  /* Bail out if we do not have exactly two parameters. */
5118
354
  if (args->children != 2) {
5119
47
    return FAILURE;
5120
47
  }
5121
5122
307
  zend_ast *callback = args->child[0];
5123
5124
  /* Bail out if the callback is not a FCC/PFA. */
5125
307
  zend_ast *args_ast;
5126
307
  switch (callback->kind) {
5127
46
    case ZEND_AST_CALL:
5128
46
    case ZEND_AST_STATIC_CALL:
5129
46
      args_ast = zend_ast_call_get_args(callback);
5130
46
      if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT) {
5131
9
        return FAILURE;
5132
9
      }
5133
5134
37
      break;
5135
261
    default:
5136
261
      return FAILURE;
5137
307
  }
5138
5139
  /* Bail out if the callback is assert() due to the AST stringification logic
5140
   * breaking for the generated call.
5141
   */
5142
37
  if (callback->kind == ZEND_AST_CALL
5143
37
   && callback->child[0]->kind == ZEND_AST_ZVAL 
5144
35
   && Z_TYPE_P(zend_ast_get_zval(callback->child[0])) == IS_STRING
5145
35
   && zend_string_equals_literal_ci(zend_ast_get_str(callback->child[0]), "assert")) {
5146
0
    return FAILURE;
5147
0
  }
5148
5149
37
  zend_ast_list *callback_args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args);
5150
37
  if (callback_args->children != 1 || callback_args->child[0]->attr != ZEND_PLACEHOLDER_VARIADIC) {
5151
    /* Full PFA is not yet implemented, will fail in zend_compile_call_common(). */
5152
0
    return FAILURE;
5153
0
  }
5154
5155
37
  znode value;
5156
37
  value.op_type = IS_TMP_VAR;
5157
37
  value.u.op.var = get_temporary_variable();
5158
37
  zend_ast *call_args = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&value));
5159
5160
37
  zend_op *opline;
5161
5162
37
  znode array;
5163
37
  zend_compile_expr(&array, args->child[1]);
5164
  /* array is an argument to both ZEND_TYPE_ASSERT and to ZEND_FE_RESET_R. */
5165
37
  if (array.op_type == IS_CONST) {
5166
11
    Z_TRY_ADDREF(array.u.constant);
5167
11
  }
5168
5169
  /* Verify that the input array actually is an array. */
5170
37
  znode name;
5171
37
  name.op_type = IS_CONST;
5172
37
  ZVAL_STR_COPY(&name.u.constant, lcname);
5173
37
  opline = zend_emit_op(NULL, ZEND_TYPE_ASSERT, &name, &array);
5174
37
  opline->lineno = lineno;
5175
37
  opline->extended_value = (2 << 16) | IS_ARRAY;
5176
37
  const zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5177
37
  const Bucket *fbc_bucket = (const Bucket*)((uintptr_t)fbc_zv - XtOffsetOf(Bucket, val));
5178
37
  Z_EXTRA_P(CT_CONSTANT(opline->op1)) = fbc_bucket - CG(function_table)->arData;
5179
5180
  /* Initialize the result array. */
5181
37
  zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
5182
5183
  /* foreach loop starts here. */
5184
37
  znode key;
5185
5186
37
  uint32_t opnum_reset = get_next_op_number();
5187
37
  znode reset_node;
5188
37
  zend_emit_op(&reset_node, ZEND_FE_RESET_R, &array, NULL);
5189
37
  zend_begin_loop(ZEND_FE_FREE, &reset_node, false);
5190
37
  uint32_t opnum_fetch = get_next_op_number();
5191
37
  zend_emit_op_tmp(&key, ZEND_FE_FETCH_R, &reset_node, &value);
5192
5193
  /* loop body */
5194
37
  znode call_result;
5195
37
  switch (callback->kind) {
5196
37
    case ZEND_AST_CALL:
5197
37
      zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_CALL, callback->child[0], call_args));
5198
37
      break;
5199
0
    case ZEND_AST_STATIC_CALL:
5200
0
      zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_STATIC_CALL, callback->child[0], callback->child[1], call_args));
5201
0
      break;
5202
37
  }
5203
37
  opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT, &call_result, &key);
5204
37
  SET_NODE(opline->result, result);
5205
  /* end loop body */
5206
5207
37
  zend_emit_jump(opnum_fetch);
5208
5209
37
  uint32_t opnum_loop_end = get_next_op_number();
5210
37
  opline = &CG(active_op_array)->opcodes[opnum_reset];
5211
37
  opline->op2.opline_num = opnum_loop_end;
5212
37
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
5213
37
  opline->extended_value = opnum_loop_end;
5214
5215
37
  zend_end_loop(opnum_fetch, &reset_node);
5216
37
  zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
5217
5218
37
  return SUCCESS;
5219
37
}
5220
5221
static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type, uint32_t lineno) /* {{{ */
5222
122k
{
5223
122k
  if (zend_string_equals_literal(lcname, "strlen")) {
5224
745
    return zend_compile_func_strlen(result, args);
5225
121k
  } else if (zend_string_equals_literal(lcname, "is_null")) {
5226
386
    return zend_compile_func_typecheck(result, args, IS_NULL);
5227
121k
  } else if (zend_string_equals_literal(lcname, "is_bool")) {
5228
30
    return zend_compile_func_typecheck(result, args, _IS_BOOL);
5229
121k
  } else if (zend_string_equals_literal(lcname, "is_long")
5230
121k
    || zend_string_equals_literal(lcname, "is_int")
5231
120k
    || zend_string_equals_literal(lcname, "is_integer")
5232
121k
  ) {
5233
396
    return zend_compile_func_typecheck(result, args, IS_LONG);
5234
120k
  } else if (zend_string_equals_literal(lcname, "is_float")
5235
120k
    || zend_string_equals_literal(lcname, "is_double")
5236
120k
  ) {
5237
422
    return zend_compile_func_typecheck(result, args, IS_DOUBLE);
5238
120k
  } else if (zend_string_equals_literal(lcname, "is_string")) {
5239
21
    return zend_compile_func_typecheck(result, args, IS_STRING);
5240
120k
  } else if (zend_string_equals_literal(lcname, "is_array")) {
5241
222
    return zend_compile_func_typecheck(result, args, IS_ARRAY);
5242
120k
  } else if (zend_string_equals_literal(lcname, "is_object")) {
5243
108
    return zend_compile_func_typecheck(result, args, IS_OBJECT);
5244
119k
  } else if (zend_string_equals_literal(lcname, "is_resource")) {
5245
69
    return zend_compile_func_typecheck(result, args, IS_RESOURCE);
5246
119k
  } else if (zend_string_equals_literal(lcname, "is_scalar")) {
5247
179
    return zend_compile_func_is_scalar(result, args);
5248
119k
  } else if (zend_string_equals_literal(lcname, "boolval")) {
5249
193
    return zend_compile_func_cast(result, args, _IS_BOOL);
5250
119k
  } else if (zend_string_equals_literal(lcname, "intval")) {
5251
115
    return zend_compile_func_cast(result, args, IS_LONG);
5252
119k
  } else if (zend_string_equals_literal(lcname, "floatval")
5253
119k
    || zend_string_equals_literal(lcname, "doubleval")
5254
119k
  ) {
5255
445
    return zend_compile_func_cast(result, args, IS_DOUBLE);
5256
118k
  } else if (zend_string_equals_literal(lcname, "strval")) {
5257
290
    return zend_compile_func_cast(result, args, IS_STRING);
5258
118k
  } else if (zend_string_equals_literal(lcname, "defined")) {
5259
1.75k
    return zend_compile_func_defined(result, args);
5260
116k
  } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) {
5261
1.20k
    return zend_compile_func_chr(result, args);
5262
115k
  } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) {
5263
529
    return zend_compile_func_ord(result, args);
5264
115k
  } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
5265
1.50k
    return zend_compile_func_cufa(result, args, lcname, type);
5266
113k
  } else if (zend_string_equals_literal(lcname, "call_user_func")) {
5267
5.20k
    return zend_compile_func_cuf(result, args, lcname, type);
5268
108k
  } else if (zend_string_equals_literal(lcname, "in_array")) {
5269
9.75k
    return zend_compile_func_in_array(result, args);
5270
98.7k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_COUNT))
5271
97.9k
      || zend_string_equals_literal(lcname, "sizeof")) {
5272
971
    return zend_compile_func_count(result, args, lcname);
5273
97.7k
  } else if (zend_string_equals_literal(lcname, "get_class")) {
5274
1.33k
    return zend_compile_func_get_class(result, args);
5275
96.4k
  } else if (zend_string_equals_literal(lcname, "get_called_class")) {
5276
157
    return zend_compile_func_get_called_class(result, args);
5277
96.2k
  } else if (zend_string_equals_literal(lcname, "gettype")) {
5278
457
    return zend_compile_func_gettype(result, args);
5279
95.8k
  } else if (zend_string_equals_literal(lcname, "func_num_args")) {
5280
188
    return zend_compile_func_num_args(result, args);
5281
95.6k
  } else if (zend_string_equals_literal(lcname, "func_get_args")) {
5282
544
    return zend_compile_func_get_args(result, args);
5283
95.0k
  } else if (zend_string_equals_literal(lcname, "array_slice")) {
5284
1.45k
    return zend_compile_func_array_slice(result, args);
5285
93.6k
  } else if (zend_string_equals_literal(lcname, "array_key_exists")) {
5286
244
    return zend_compile_func_array_key_exists(result, args);
5287
93.3k
  } else if (zend_string_equals_literal(lcname, "sprintf")) {
5288
1.51k
    return zend_compile_func_sprintf(result, args);
5289
91.8k
  } else if (zend_string_equals_literal(lcname, "printf")) {
5290
2.64k
    return zend_compile_func_printf(result, args);
5291
89.2k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_CLONE))) {
5292
989
    return zend_compile_func_clone(result, args);
5293
88.2k
  } else if (zend_string_equals_literal(lcname, "array_map")) {
5294
354
    return zend_compile_func_array_map(result, args, lcname, lineno);
5295
87.8k
  } else {
5296
87.8k
    return FAILURE;
5297
87.8k
  }
5298
122k
}
5299
5300
static zend_result zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, const zend_function *fbc, uint32_t type, uint32_t lineno) /* {{{ */
5301
145k
{
5302
145k
  if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
5303
0
    return FAILURE;
5304
0
  }
5305
5306
145k
  if (fbc->type != ZEND_INTERNAL_FUNCTION) {
5307
    /* If the function is part of disabled_functions, it may be redeclared as a userland
5308
     * function with a different implementation. Don't use the VM builtin in that case. */
5309
21.8k
    return FAILURE;
5310
21.8k
  }
5311
5312
123k
  if (zend_args_contain_unpack_or_named(args)) {
5313
969
    return FAILURE;
5314
969
  }
5315
5316
122k
  if (zend_try_compile_special_func_ex(result, lcname, args, type, lineno) == SUCCESS) {
5317
18.4k
    return SUCCESS;
5318
18.4k
  }
5319
5320
103k
  return zend_compile_frameless_icall(result, args, fbc, type) != (uint32_t)-1 ? SUCCESS : FAILURE;
5321
122k
}
5322
5323
6
static const char *zend_get_cstring_from_property_hook_kind(zend_property_hook_kind kind) {
5324
6
  switch (kind) {
5325
1
    case ZEND_PROPERTY_HOOK_GET:
5326
1
      return "get";
5327
5
    case ZEND_PROPERTY_HOOK_SET:
5328
5
      return "set";
5329
6
    EMPTY_SWITCH_DEFAULT_CASE()
5330
6
  }
5331
6
}
5332
5333
static zend_string *zend_copy_unmangled_prop_name(zend_string *prop_name)
5334
512
{
5335
512
  if (ZSTR_VAL(prop_name)[0] != '\0') {
5336
405
    return zend_string_copy(prop_name);
5337
405
  } else {
5338
107
    const char *unmangled = zend_get_unmangled_property_name(prop_name);
5339
107
    return zend_string_init(unmangled, strlen(unmangled), /* persistent */ false);
5340
107
  }
5341
512
}
5342
5343
static bool zend_compile_parent_property_hook_call(znode *result, const zend_ast *ast, uint32_t type)
5344
34.4k
{
5345
34.4k
  ZEND_ASSERT(ast->kind == ZEND_AST_STATIC_CALL);
5346
5347
34.4k
  const zend_ast *class_ast = ast->child[0];
5348
34.4k
  zend_ast *method_ast = ast->child[1];
5349
5350
  /* Recognize parent::$prop::get() pattern. */
5351
34.4k
  if (class_ast->kind != ZEND_AST_STATIC_PROP
5352
2.03k
   || (class_ast->attr & ZEND_PARENTHESIZED_STATIC_PROP)
5353
1.98k
   || class_ast->child[0]->kind != ZEND_AST_ZVAL
5354
1.84k
   || Z_TYPE_P(zend_ast_get_zval(class_ast->child[0])) != IS_STRING
5355
1.84k
   || zend_get_class_fetch_type(zend_ast_get_str(class_ast->child[0])) != ZEND_FETCH_CLASS_PARENT
5356
1.17k
   || class_ast->child[1]->kind != ZEND_AST_ZVAL
5357
1.02k
   || method_ast->kind != ZEND_AST_ZVAL
5358
958
   || Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING
5359
958
   || (!zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "get")
5360
33.8k
    && !zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "set"))) {
5361
33.8k
    return false;
5362
33.8k
  }
5363
5364
619
  zend_class_entry *ce = CG(active_class_entry);
5365
619
  if (!ce) {
5366
8
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"parent\" when no class scope is active");
5367
8
  }
5368
5369
611
  zend_ast *args_ast = ast->child[2];
5370
611
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
5371
6
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for parent property hook call");
5372
6
  }
5373
5374
605
  zval *property_hook_name_zv = zend_ast_get_zval(class_ast->child[1]);
5375
605
  zend_string *property_name = zval_get_string(property_hook_name_zv);
5376
605
  zend_string *hook_name = zend_ast_get_str(method_ast);
5377
605
  zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(hook_name);
5378
605
  ZEND_ASSERT(hook_kind != (uint32_t)-1);
5379
5380
605
  const zend_string *prop_info_name = CG(context).active_property_info_name;
5381
605
  if (!prop_info_name) {
5382
5
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() outside a property hook",
5383
5
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name));
5384
5
  }
5385
5386
600
  const char *unmangled_prop_name = zend_get_unmangled_property_name(prop_info_name);
5387
600
  if (!zend_string_equals_cstr(property_name, unmangled_prop_name, strlen(unmangled_prop_name))) {
5388
28
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property ($%s)",
5389
28
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), unmangled_prop_name);
5390
28
  }
5391
572
  if (hook_kind != CG(context).active_property_hook_kind) {
5392
6
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property hook (%s)",
5393
6
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), zend_get_cstring_from_property_hook_kind(CG(context).active_property_hook_kind));
5394
6
  }
5395
5396
566
  zend_op *opline = get_next_op();
5397
566
  opline->opcode = ZEND_INIT_PARENT_PROPERTY_HOOK_CALL;
5398
566
  opline->op1_type = IS_CONST;
5399
566
  opline->op1.constant = zend_add_literal_string(&property_name);
5400
566
  opline->op2.num = hook_kind;
5401
5402
566
  zend_compile_call_common(result, args_ast, NULL, zend_ast_get_lineno(method_ast), BP_VAR_R);
5403
5404
566
  return true;
5405
572
}
5406
5407
static void zend_compile_call(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
5408
2.78M
{
5409
2.78M
  zend_ast *name_ast = ast->child[0];
5410
2.78M
  zend_ast *args_ast = ast->child[1];
5411
2.78M
  bool is_callable_convert = args_ast->kind == ZEND_AST_CALLABLE_CONVERT;
5412
5413
2.78M
  znode name_node;
5414
5415
2.78M
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
5416
124k
    zend_compile_expr(&name_node, name_ast);
5417
124k
    zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno, type);
5418
124k
    return;
5419
124k
  }
5420
5421
2.65M
  {
5422
2.65M
    bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
5423
2.65M
    if (runtime_resolution) {
5424
2.42M
      if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")
5425
41.2k
          && !is_callable_convert) {
5426
40.9k
        zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL, ast->lineno, type);
5427
2.38M
      } else {
5428
2.38M
        zend_compile_ns_call(result, &name_node, args_ast, ast->lineno, type);
5429
2.38M
      }
5430
2.42M
      return;
5431
2.42M
    }
5432
2.65M
  }
5433
5434
235k
  {
5435
235k
    const zval *name = &name_node.u.constant;
5436
235k
    zend_string *lcname = zend_string_tolower(Z_STR_P(name));
5437
235k
    zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5438
235k
    const zend_function *fbc = fbc_zv ? Z_PTR_P(fbc_zv) : NULL;
5439
235k
    zend_op *opline;
5440
5441
    /* Special assert() handling should apply independently of compiler flags. */
5442
235k
    if (fbc && zend_string_equals_literal(lcname, "assert") && !is_callable_convert) {
5443
13.8k
      zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc, ast->lineno, type);
5444
13.8k
      zend_string_release(lcname);
5445
13.8k
      zval_ptr_dtor(&name_node.u.constant);
5446
13.8k
      return;
5447
13.8k
    }
5448
5449
221k
    if (!fbc
5450
145k
     || !fbc_is_finalized(fbc)
5451
145k
     || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
5452
75.9k
      zend_string_release_ex(lcname, 0);
5453
75.9k
      zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno, type);
5454
75.9k
      return;
5455
75.9k
    }
5456
5457
145k
    if (!is_callable_convert &&
5458
145k
        zend_try_compile_special_func(result, lcname,
5459
145k
        zend_ast_get_list(args_ast), fbc, type, ast->lineno) == SUCCESS
5460
145k
    ) {
5461
27.2k
      zend_string_release_ex(lcname, 0);
5462
27.2k
      zval_ptr_dtor(&name_node.u.constant);
5463
27.2k
      return;
5464
27.2k
    }
5465
5466
118k
    zval_ptr_dtor(&name_node.u.constant);
5467
118k
    ZVAL_NEW_STR(&name_node.u.constant, lcname);
5468
5469
118k
    opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
5470
118k
    opline->result.num = zend_alloc_cache_slot();
5471
5472
    /* Store offset to function from symbol table in op2.extra. */
5473
118k
    if (fbc->type == ZEND_INTERNAL_FUNCTION) {
5474
96.4k
      const Bucket *fbc_bucket = (const Bucket*)((uintptr_t)fbc_zv - XtOffsetOf(Bucket, val));
5475
96.4k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = fbc_bucket - CG(function_table)->arData;
5476
96.4k
    }
5477
5478
118k
    zend_compile_call_common(result, args_ast, fbc, ast->lineno, type);
5479
118k
  }
5480
118k
}
5481
/* }}} */
5482
5483
static void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5484
58.9k
{
5485
58.9k
  zend_ast *obj_ast = ast->child[0];
5486
58.9k
  zend_ast *method_ast = ast->child[1];
5487
58.9k
  zend_ast *args_ast = ast->child[2];
5488
5489
58.9k
  znode obj_node, method_node;
5490
58.9k
  zend_op *opline;
5491
58.9k
  const zend_function *fbc = NULL;
5492
58.9k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL;
5493
58.9k
  uint32_t short_circuiting_checkpoint = zend_short_circuiting_checkpoint();
5494
5495
58.9k
  if (is_this_fetch(obj_ast)) {
5496
1.42k
    if (this_guaranteed_exists()) {
5497
1.32k
      obj_node.op_type = IS_UNUSED;
5498
1.32k
    } else {
5499
100
      zend_emit_op_tmp(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
5500
100
    }
5501
1.42k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
5502
5503
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
5504
     * check for a nullsafe access. */
5505
57.5k
  } else {
5506
57.5k
    zend_short_circuiting_mark_inner(obj_ast);
5507
57.5k
    zend_compile_expr(&obj_node, obj_ast);
5508
57.5k
    if (nullsafe) {
5509
2.44k
      zend_emit_jmp_null(&obj_node, type);
5510
2.44k
    }
5511
57.5k
  }
5512
5513
58.9k
  zend_compile_expr(&method_node, method_ast);
5514
58.9k
  opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
5515
5516
58.9k
  if (method_node.op_type == IS_CONST) {
5517
58.1k
    if (Z_TYPE(method_node.u.constant) != IS_STRING) {
5518
12
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5519
12
    }
5520
5521
58.0k
    opline->op2_type = IS_CONST;
5522
58.0k
    opline->op2.constant = zend_add_func_name_literal(
5523
58.0k
      Z_STR(method_node.u.constant));
5524
58.0k
    opline->result.num = zend_alloc_cache_slots(2);
5525
58.0k
  } else {
5526
890
    SET_NODE(opline->op2, &method_node);
5527
890
  }
5528
5529
  /* Check if this calls a known method on $this */
5530
58.9k
  if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST &&
5531
1.25k
      CG(active_class_entry) && zend_is_scope_known()) {
5532
911
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5533
911
    fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname);
5534
5535
    /* We only know the exact method that is being called if it is either private or final.
5536
     * Otherwise an overriding method in a child class may be called. */
5537
911
    if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) {
5538
288
      fbc = NULL;
5539
288
    }
5540
911
  }
5541
5542
58.9k
  if (zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast), type)) {
5543
475
    if (short_circuiting_checkpoint != zend_short_circuiting_checkpoint()) {
5544
13
      zend_error_noreturn(E_COMPILE_ERROR,
5545
13
        "Cannot combine nullsafe operator with Closure creation");
5546
13
    }
5547
475
  }
5548
58.9k
}
5549
/* }}} */
5550
5551
static bool zend_is_constructor(const zend_string *name) /* {{{ */
5552
33.8k
{
5553
33.8k
  return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
5554
33.8k
}
5555
/* }}} */
5556
5557
static bool is_func_accessible(const zend_function *fbc)
5558
41.4k
{
5559
41.4k
  if ((fbc->common.fn_flags & ZEND_ACC_PUBLIC) || fbc->common.scope == CG(active_class_entry)) {
5560
41.2k
    return true;
5561
41.2k
  }
5562
5563
156
  if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE)
5564
83
    && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED)
5565
83
    && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED))
5566
21
    && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) {
5567
0
    return true;
5568
0
  }
5569
5570
156
  return false;
5571
156
}
5572
5573
static const zend_function *zend_get_compatible_func_or_null(const zend_class_entry *ce, zend_string *lcname) /* {{{ */
5574
4.90k
{
5575
4.90k
  const zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname);
5576
5577
4.90k
  if (!fbc || is_func_accessible(fbc)) {
5578
4.75k
    return fbc;
5579
4.75k
  }
5580
5581
149
  return NULL;
5582
4.90k
}
5583
/* }}} */
5584
5585
static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5586
34.4k
{
5587
34.4k
  zend_ast *class_ast = ast->child[0];
5588
34.4k
  zend_ast *method_ast = ast->child[1];
5589
34.4k
  zend_ast *args_ast = ast->child[2];
5590
5591
34.4k
  znode class_node, method_node;
5592
34.4k
  zend_op *opline;
5593
34.4k
  const zend_function *fbc = NULL;
5594
5595
34.4k
  if (zend_compile_parent_property_hook_call(result, ast, type)) {
5596
566
    return;
5597
566
  }
5598
5599
33.9k
  zend_short_circuiting_mark_inner(class_ast);
5600
33.9k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5601
5602
33.9k
  zend_compile_expr(&method_node, method_ast);
5603
5604
33.9k
  if (method_node.op_type == IS_CONST) {
5605
32.8k
    zval *name = &method_node.u.constant;
5606
32.8k
    if (Z_TYPE_P(name) != IS_STRING) {
5607
3
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5608
3
    }
5609
32.8k
    if (zend_is_constructor(Z_STR_P(name))) {
5610
383
      zval_ptr_dtor(name);
5611
383
      method_node.op_type = IS_UNUSED;
5612
383
    }
5613
32.8k
  }
5614
5615
33.9k
  opline = get_next_op();
5616
33.9k
  opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
5617
5618
33.9k
  zend_set_class_name_op1(opline, &class_node);
5619
5620
33.9k
  if (method_node.op_type == IS_CONST) {
5621
32.5k
    opline->op2_type = IS_CONST;
5622
32.5k
    opline->op2.constant = zend_add_func_name_literal(
5623
32.5k
      Z_STR(method_node.u.constant));
5624
32.5k
    opline->result.num = zend_alloc_cache_slots(2);
5625
32.5k
  } else {
5626
1.40k
    if (opline->op1_type == IS_CONST) {
5627
462
      opline->result.num = zend_alloc_cache_slot();
5628
462
    }
5629
1.40k
    SET_NODE(opline->op2, &method_node);
5630
1.40k
  }
5631
5632
  /* Check if we already know which method we're calling */
5633
33.9k
  if (opline->op2_type == IS_CONST) {
5634
32.5k
    zend_class_entry *ce = NULL;
5635
32.5k
    if (opline->op1_type == IS_CONST) {
5636
15.7k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5637
15.7k
      ce = zend_hash_find_ptr(CG(class_table), lcname);
5638
15.7k
      if (ce) {
5639
4.16k
        if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5640
0
          ce = NULL;
5641
0
        }
5642
11.5k
      } else if (CG(active_class_entry)
5643
1.00k
          && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5644
553
        ce = CG(active_class_entry);
5645
553
      }
5646
16.7k
    } else if (opline->op1_type == IS_UNUSED
5647
12.5k
        && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5648
11.5k
        && zend_is_scope_known()) {
5649
186
      ce = CG(active_class_entry);
5650
186
    }
5651
32.5k
    if (ce) {
5652
4.90k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5653
4.90k
      fbc = zend_get_compatible_func_or_null(ce, lcname);
5654
4.90k
    }
5655
32.5k
  }
5656
5657
33.9k
  zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast), type);
5658
33.9k
}
5659
/* }}} */
5660
5661
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel);
5662
5663
static void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
5664
75.7k
{
5665
75.7k
  zend_ast *class_ast = ast->child[0];
5666
75.7k
  zend_ast *args_ast = ast->child[1];
5667
5668
75.7k
  znode class_node, ctor_result;
5669
75.7k
  zend_op *opline;
5670
5671
75.7k
  if (class_ast->kind == ZEND_AST_CLASS) {
5672
    /* anon class declaration */
5673
1.70k
    zend_compile_class_decl(&class_node, class_ast, false);
5674
74.0k
  } else {
5675
74.0k
    zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5676
74.0k
  }
5677
5678
75.7k
  opline = zend_emit_op_tmp(result, ZEND_NEW, NULL, NULL);
5679
5680
75.7k
  zend_set_class_name_op1(opline, &class_node);
5681
5682
75.7k
  if (opline->op1_type == IS_CONST) {
5683
73.1k
    opline->op2.num = zend_alloc_cache_slot();
5684
73.1k
  }
5685
5686
75.7k
  zend_class_entry *ce = NULL;
5687
75.7k
  if (opline->op1_type == IS_CONST) {
5688
73.1k
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5689
73.1k
    ce = zend_hash_find_ptr(CG(class_table), lcname);
5690
73.1k
    if (ce) {
5691
49.1k
      if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5692
0
        ce = NULL;
5693
0
      }
5694
49.1k
    } else if (CG(active_class_entry)
5695
998
        && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5696
471
      ce = CG(active_class_entry);
5697
471
    }
5698
73.1k
  } else if (opline->op1_type == IS_UNUSED
5699
336
      && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5700
167
      && zend_is_scope_known()) {
5701
102
    ce = CG(active_class_entry);
5702
102
  }
5703
5704
5705
75.7k
  const zend_function *fbc = NULL;
5706
75.7k
  if (ce
5707
49.6k
      && ce->default_object_handlers->get_constructor == zend_std_get_constructor
5708
49.6k
      && ce->constructor
5709
37.0k
      && is_func_accessible(ce->constructor)) {
5710
37.0k
    fbc = ce->constructor;
5711
37.0k
  }
5712
5713
75.7k
  zend_compile_call_common(&ctor_result, args_ast, fbc, ast->lineno, BP_VAR_R);
5714
75.7k
  zend_do_free(&ctor_result);
5715
75.7k
}
5716
/* }}} */
5717
5718
static void zend_compile_global_var(zend_ast *ast) /* {{{ */
5719
2.01k
{
5720
2.01k
  zend_ast *var_ast = ast->child[0];
5721
2.01k
  zend_ast *name_ast = var_ast->child[0];
5722
5723
2.01k
  znode name_node, result;
5724
5725
2.01k
  zend_compile_expr(&name_node, name_ast);
5726
2.01k
  if (name_node.op_type == IS_CONST) {
5727
1.74k
    convert_to_string(&name_node.u.constant);
5728
1.74k
  }
5729
5730
  // TODO(GLOBALS) Forbid "global $GLOBALS"?
5731
2.01k
  if (is_this_fetch(var_ast)) {
5732
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable");
5733
2.01k
  } else if (zend_try_compile_cv(&result, var_ast, BP_VAR_R) == SUCCESS) {
5734
1.64k
    zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
5735
1.64k
    opline->extended_value = zend_alloc_cache_slot();
5736
1.64k
  } else {
5737
    /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W
5738
     * to not free the name_node operand, so it can be reused in the following
5739
     * ASSIGN_REF, which then frees it. */
5740
366
    zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
5741
366
    opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
5742
5743
366
    if (name_node.op_type == IS_CONST) {
5744
92
      zend_string_addref(Z_STR(name_node.u.constant));
5745
92
    }
5746
5747
366
    zend_emit_assign_ref_znode(
5748
366
      zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)),
5749
366
      &result
5750
366
    );
5751
366
  }
5752
2.01k
}
5753
/* }}} */
5754
5755
static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */
5756
83.5k
{
5757
83.5k
  zend_op *opline;
5758
83.5k
  if (!CG(active_op_array)->static_variables) {
5759
0
    if (CG(active_op_array)->scope) {
5760
0
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5761
0
    }
5762
0
    CG(active_op_array)->static_variables = zend_new_array(8);
5763
0
  }
5764
5765
83.5k
  value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value);
5766
5767
83.5k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5768
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5769
0
  }
5770
5771
83.5k
  opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL);
5772
83.5k
  opline->op1_type = IS_CV;
5773
83.5k
  opline->op1.var = lookup_cv(var_name);
5774
83.5k
  opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode;
5775
83.5k
}
5776
/* }}} */
5777
5778
static void zend_compile_static_var(zend_ast *ast) /* {{{ */
5779
36.7k
{
5780
36.7k
  zend_ast *var_ast = ast->child[0];
5781
36.7k
  zend_string *var_name = zend_ast_get_str(var_ast);
5782
5783
36.7k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5784
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5785
5
  }
5786
5787
36.7k
  if (!CG(active_op_array)->static_variables) {
5788
36.1k
    if (CG(active_op_array)->scope) {
5789
35.1k
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5790
35.1k
    }
5791
36.1k
    CG(active_op_array)->static_variables = zend_new_array(8);
5792
36.1k
  }
5793
5794
36.7k
  if (zend_hash_exists(CG(active_op_array)->static_variables, var_name)) {
5795
12
    zend_error_noreturn_unchecked(E_COMPILE_ERROR, "Duplicate declaration of static variable $%S", var_name);
5796
12
  }
5797
5798
36.7k
  zend_eval_const_expr(&ast->child[1]);
5799
36.7k
  zend_ast *value_ast = ast->child[1];
5800
5801
36.7k
  if (!value_ast || value_ast->kind == ZEND_AST_ZVAL) {
5802
36.3k
    zval *value_zv = value_ast
5803
36.3k
      ? zend_ast_get_zval(value_ast)
5804
36.3k
      : &EG(uninitialized_zval);
5805
36.3k
    Z_TRY_ADDREF_P(value_zv);
5806
36.3k
    zend_compile_static_var_common(var_name, value_zv, ZEND_BIND_REF);
5807
36.3k
  } else {
5808
359
    zend_op *opline;
5809
5810
359
    zval *placeholder_ptr = zend_hash_update(CG(active_op_array)->static_variables, var_name, &EG(uninitialized_zval));
5811
359
    uint32_t placeholder_offset = (uint32_t)((char*)placeholder_ptr - (char*)CG(active_op_array)->static_variables->arData);
5812
5813
359
    uint32_t static_def_jmp_opnum = get_next_op_number();
5814
359
    opline = zend_emit_op(NULL, ZEND_BIND_INIT_STATIC_OR_JMP, NULL, NULL);
5815
359
    opline->op1_type = IS_CV;
5816
359
    opline->op1.var = lookup_cv(var_name);
5817
359
    opline->extended_value = placeholder_offset;
5818
5819
359
    znode expr;
5820
359
    zend_compile_expr(&expr, value_ast);
5821
5822
359
    opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, &expr);
5823
359
    opline->op1_type = IS_CV;
5824
359
    opline->op1.var = lookup_cv(var_name);
5825
359
    opline->extended_value = placeholder_offset | ZEND_BIND_REF;
5826
5827
359
    zend_update_jump_target_to_next(static_def_jmp_opnum);
5828
359
  }
5829
36.7k
}
5830
/* }}} */
5831
5832
static void zend_compile_unset(const zend_ast *ast) /* {{{ */
5833
5.77k
{
5834
5.77k
  zend_ast *var_ast = ast->child[0];
5835
5.77k
  znode var_node;
5836
5.77k
  zend_op *opline;
5837
5838
5.77k
  zend_ensure_writable_variable(var_ast);
5839
5840
5.77k
  if (is_global_var_fetch(var_ast)) {
5841
277
    if (!var_ast->child[1]) {
5842
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
5843
5
    }
5844
5845
272
    zend_compile_expr(&var_node, var_ast->child[1]);
5846
272
    if (var_node.op_type == IS_CONST) {
5847
251
      convert_to_string(&var_node.u.constant);
5848
251
    }
5849
5850
272
    opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL);
5851
272
    opline->extended_value = ZEND_FETCH_GLOBAL;
5852
272
    return;
5853
277
  }
5854
5855
5.50k
  switch (var_ast->kind) {
5856
2.83k
    case ZEND_AST_VAR:
5857
2.83k
      if (is_this_fetch(var_ast)) {
5858
8
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this");
5859
2.82k
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_UNSET) == SUCCESS) {
5860
2.65k
        opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL);
5861
2.65k
      } else {
5862
176
        opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, false);
5863
176
        opline->opcode = ZEND_UNSET_VAR;
5864
176
      }
5865
2.82k
      return;
5866
2.82k
    case ZEND_AST_DIM:
5867
1.63k
      opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET, /* by_ref */ false);
5868
1.63k
      opline->opcode = ZEND_UNSET_DIM;
5869
1.63k
      return;
5870
1.00k
    case ZEND_AST_PROP:
5871
1.00k
    case ZEND_AST_NULLSAFE_PROP:
5872
1.00k
      opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, false);
5873
1.00k
      opline->opcode = ZEND_UNSET_OBJ;
5874
1.00k
      return;
5875
21
    case ZEND_AST_STATIC_PROP:
5876
21
      opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, false, false);
5877
21
      opline->opcode = ZEND_UNSET_STATIC_PROP;
5878
21
      return;
5879
5.50k
    EMPTY_SWITCH_DEFAULT_CASE()
5880
5.50k
  }
5881
5.50k
}
5882
/* }}} */
5883
5884
static bool zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */
5885
35.6k
{
5886
35.6k
  const zend_loop_var *base;
5887
35.6k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5888
5889
35.6k
  if (!loop_var) {
5890
1.19k
    return 1;
5891
1.19k
  }
5892
34.4k
  base = zend_stack_base(&CG(loop_var_stack));
5893
41.3k
  for (; loop_var >= base; loop_var--) {
5894
39.9k
    if (loop_var->opcode == ZEND_FAST_CALL) {
5895
1.95k
      zend_op *opline = get_next_op();
5896
5897
1.95k
      opline->opcode = ZEND_FAST_CALL;
5898
1.95k
      opline->result_type = IS_TMP_VAR;
5899
1.95k
      opline->result.var = loop_var->var_num;
5900
1.95k
      if (return_value) {
5901
1.00k
        SET_NODE(opline->op2, return_value);
5902
1.00k
      }
5903
1.95k
      opline->op1.num = loop_var->try_catch_offset;
5904
38.0k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5905
1.91k
      zend_op *opline = get_next_op();
5906
1.91k
      opline->opcode = ZEND_DISCARD_EXCEPTION;
5907
1.91k
      opline->op1_type = IS_TMP_VAR;
5908
1.91k
      opline->op1.var = loop_var->var_num;
5909
36.1k
    } else if (loop_var->opcode == ZEND_RETURN) {
5910
      /* Stack separator */
5911
31.7k
      break;
5912
31.7k
    } else if (depth <= 1) {
5913
1.22k
      return 1;
5914
3.09k
    } else if (loop_var->opcode == ZEND_NOP) {
5915
      /* Loop doesn't have freeable variable */
5916
1.91k
      depth--;
5917
1.91k
    } else {
5918
1.18k
      zend_op *opline;
5919
5920
1.18k
      ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR));
5921
1.18k
      opline = get_next_op();
5922
1.18k
      opline->opcode = loop_var->opcode;
5923
1.18k
      opline->op1_type = loop_var->var_type;
5924
1.18k
      opline->op1.var = loop_var->var_num;
5925
1.18k
      opline->extended_value = ZEND_FREE_ON_RETURN;
5926
1.18k
      depth--;
5927
1.18k
      }
5928
39.9k
  }
5929
33.1k
  return (depth == 0);
5930
34.4k
}
5931
/* }}} */
5932
5933
static bool zend_handle_loops_and_finally(znode *return_value) /* {{{ */
5934
34.3k
{
5935
34.3k
  return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value);
5936
34.3k
}
5937
/* }}} */
5938
5939
static bool zend_has_finally_ex(zend_long depth) /* {{{ */
5940
1.48k
{
5941
1.48k
  const zend_loop_var *base;
5942
1.48k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5943
5944
1.48k
  if (!loop_var) {
5945
213
    return 0;
5946
213
  }
5947
1.27k
  base = zend_stack_base(&CG(loop_var_stack));
5948
2.46k
  for (; loop_var >= base; loop_var--) {
5949
2.24k
    if (loop_var->opcode == ZEND_FAST_CALL) {
5950
350
      return 1;
5951
1.89k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5952
1.00k
    } else if (loop_var->opcode == ZEND_RETURN) {
5953
      /* Stack separator */
5954
705
      return 0;
5955
705
    } else if (depth <= 1) {
5956
0
      return 0;
5957
189
    } else {
5958
189
      depth--;
5959
189
      }
5960
2.24k
  }
5961
220
  return 0;
5962
1.27k
}
5963
/* }}} */
5964
5965
static bool zend_has_finally(void) /* {{{ */
5966
1.48k
{
5967
1.48k
  return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1);
5968
1.48k
}
5969
/* }}} */
5970
5971
static void zend_compile_return(const zend_ast *ast) /* {{{ */
5972
33.3k
{
5973
33.3k
  zend_ast *expr_ast = ast->child[0];
5974
33.3k
  bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0;
5975
33.3k
  bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
5976
5977
33.3k
  znode expr_node;
5978
33.3k
  zend_op *opline;
5979
5980
33.3k
  if (is_generator) {
5981
    /* For generators the by-ref flag refers to yields, not returns */
5982
2.21k
    by_ref = false;
5983
2.21k
  }
5984
5985
33.3k
  if (!expr_ast) {
5986
860
    expr_node.op_type = IS_CONST;
5987
860
    ZVAL_NULL(&expr_node.u.constant);
5988
32.4k
  } else if (by_ref && zend_is_variable_or_call(expr_ast)) {
5989
2.19k
    zend_assert_not_short_circuited(expr_ast);
5990
2.19k
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
5991
30.2k
  } else {
5992
30.2k
    zend_compile_expr(&expr_node, expr_ast);
5993
30.2k
  }
5994
5995
33.3k
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
5996
2.32k
   && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
5997
1.48k
   && zend_has_finally()) {
5998
    /* Copy return value into temporary VAR to avoid modification in finally code */
5999
350
    if (by_ref) {
6000
155
      zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
6001
195
    } else {
6002
195
      zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL);
6003
195
    }
6004
350
  }
6005
6006
  /* Generator return types are handled separately */
6007
33.3k
  if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
6008
5.03k
    zend_emit_return_type_check(
6009
5.03k
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
6010
5.03k
  }
6011
6012
33.3k
  uint32_t opnum_before_finally = get_next_op_number();
6013
6014
33.3k
  zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
6015
6016
  /* Content of reference might have changed in finally, repeat type check. */
6017
33.3k
  if (by_ref
6018
   /* Check if any opcodes were emitted since the last return type check. */
6019
3.90k
   && opnum_before_finally != get_next_op_number()
6020
1.27k
   && !is_generator
6021
1.27k
   && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
6022
17
    zend_emit_return_type_check(
6023
17
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
6024
17
  }
6025
6026
33.3k
  opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
6027
33.3k
    &expr_node, NULL);
6028
6029
33.3k
  if (by_ref && expr_ast) {
6030
3.48k
    if (zend_is_call(expr_ast)) {
6031
289
      opline->extended_value = ZEND_RETURNS_FUNCTION;
6032
3.19k
    } else if (!zend_is_variable(expr_ast) || zend_ast_is_short_circuited(expr_ast)) {
6033
1.30k
      opline->extended_value = ZEND_RETURNS_VALUE;
6034
1.30k
    }
6035
3.48k
  }
6036
33.3k
}
6037
/* }}} */
6038
6039
static void zend_compile_void_cast(znode *result, const zend_ast *ast)
6040
412
{
6041
412
  zend_ast *expr_ast = ast->child[0];
6042
412
  znode expr_node;
6043
412
  zend_op *opline;
6044
6045
412
  zend_compile_expr(&expr_node, expr_ast);
6046
6047
412
  switch (expr_node.op_type) {
6048
365
    case IS_TMP_VAR:
6049
365
    case IS_VAR:
6050
365
      opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6051
365
      opline->extended_value = ZEND_FREE_VOID_CAST;
6052
365
      break;
6053
39
    case IS_CONST:
6054
39
      zend_do_free(&expr_node);
6055
39
      break;
6056
412
  }
6057
412
}
6058
6059
static void zend_compile_echo(const zend_ast *ast) /* {{{ */
6060
1.54M
{
6061
1.54M
  zend_op *opline;
6062
1.54M
  zend_ast *expr_ast = ast->child[0];
6063
6064
1.54M
  znode expr_node;
6065
1.54M
  zend_compile_expr(&expr_node, expr_ast);
6066
6067
1.54M
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
6068
1.54M
  opline->extended_value = 0;
6069
1.54M
}
6070
/* }}} */
6071
6072
static void zend_compile_throw(znode *result, const zend_ast *ast) /* {{{ */
6073
3.26k
{
6074
3.26k
  zend_ast *expr_ast = ast->child[0];
6075
6076
3.26k
  znode expr_node;
6077
3.26k
  zend_compile_expr(&expr_node, expr_ast);
6078
6079
3.26k
  zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
6080
3.26k
  if (result) {
6081
    /* Mark this as an "expression throw" for opcache. */
6082
721
    opline->extended_value = ZEND_THROW_IS_EXPR;
6083
721
    result->op_type = IS_CONST;
6084
721
    ZVAL_TRUE(&result->u.constant);
6085
721
  }
6086
3.26k
}
6087
/* }}} */
6088
6089
static void zend_compile_break_continue(const zend_ast *ast) /* {{{ */
6090
1.35k
{
6091
1.35k
  zend_ast *depth_ast = ast->child[0];
6092
6093
1.35k
  zend_op *opline;
6094
1.35k
  zend_long depth;
6095
6096
1.35k
  ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
6097
6098
1.35k
  if (depth_ast) {
6099
266
    const zval *depth_zv;
6100
266
    if (depth_ast->kind != ZEND_AST_ZVAL) {
6101
15
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand "
6102
15
        "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
6103
15
    }
6104
6105
251
    depth_zv = zend_ast_get_zval(depth_ast);
6106
251
    if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
6107
7
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers",
6108
7
        ast->kind == ZEND_AST_BREAK ? "break" : "continue");
6109
7
    }
6110
6111
244
    depth = Z_LVAL_P(depth_zv);
6112
1.09k
  } else {
6113
1.09k
    depth = 1;
6114
1.09k
  }
6115
6116
1.33k
  if (CG(context).current_brk_cont == -1) {
6117
30
    zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
6118
30
      ast->kind == ZEND_AST_BREAK ? "break" : "continue");
6119
1.30k
  } else {
6120
1.30k
    if (!zend_handle_loops_and_finally_ex(depth, NULL)) {
6121
80
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s",
6122
80
        ast->kind == ZEND_AST_BREAK ? "break" : "continue",
6123
80
        depth, depth == 1 ? "" : "s");
6124
80
    }
6125
1.30k
  }
6126
6127
1.22k
  if (ast->kind == ZEND_AST_CONTINUE) {
6128
526
    int d, cur = CG(context).current_brk_cont;
6129
588
    for (d = depth - 1; d > 0; d--) {
6130
62
      cur = CG(context).brk_cont_array[cur].parent;
6131
62
      ZEND_ASSERT(cur != -1);
6132
62
    }
6133
6134
526
    if (CG(context).brk_cont_array[cur].is_switch) {
6135
156
      if (depth == 1) {
6136
133
        if (CG(context).brk_cont_array[cur].parent == -1) {
6137
44
          zend_error(E_WARNING,
6138
44
            "\"continue\" targeting switch is equivalent to \"break\"");
6139
89
        } else {
6140
89
          zend_error(E_WARNING,
6141
89
            "\"continue\" targeting switch is equivalent to \"break\". " \
6142
89
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
6143
89
            depth + 1);
6144
89
        }
6145
133
      } else {
6146
23
        if (CG(context).brk_cont_array[cur].parent == -1) {
6147
11
          zend_error(E_WARNING,
6148
11
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"",
6149
11
            depth, depth);
6150
12
        } else {
6151
12
          zend_error(E_WARNING,
6152
12
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
6153
12
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
6154
12
            depth, depth, depth + 1);
6155
12
        }
6156
23
      }
6157
156
    }
6158
526
  }
6159
6160
1.22k
  opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
6161
1.22k
  opline->op1.num = CG(context).current_brk_cont;
6162
1.22k
  opline->op2.num = depth;
6163
1.22k
}
6164
/* }}} */
6165
6166
void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */
6167
1.00k
{
6168
1.00k
  zend_label *dest;
6169
1.00k
  int remove_oplines = opline->op1.num;
6170
1.00k
  zval *label;
6171
1.00k
  uint32_t opnum = opline - op_array->opcodes;
6172
6173
1.00k
  label = CT_CONSTANT_EX(op_array, opline->op2.constant);
6174
1.00k
  if (CG(context).labels == NULL ||
6175
985
      (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL
6176
1.00k
  ) {
6177
58
    CG(in_compilation) = 1;
6178
58
    CG(active_op_array) = op_array;
6179
58
    CG(zend_lineno) = opline->lineno;
6180
58
    zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
6181
58
  }
6182
6183
943
  zval_ptr_dtor_str(label);
6184
943
  ZVAL_NULL(label);
6185
6186
943
  uint32_t current = opline->extended_value;
6187
2.33k
  for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) {
6188
1.39k
    if (current == -1) {
6189
9
      CG(in_compilation) = 1;
6190
9
      CG(active_op_array) = op_array;
6191
9
      CG(zend_lineno) = opline->lineno;
6192
9
      zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
6193
9
    }
6194
1.38k
    if (CG(context).brk_cont_array[current].start >= 0) {
6195
845
      remove_oplines--;
6196
845
    }
6197
1.38k
  }
6198
6199
1.89k
  for (current = 0; current < op_array->last_try_catch; ++current) {
6200
1.06k
    const zend_try_catch_element *elem = &op_array->try_catch_array[current];
6201
1.06k
    if (elem->try_op > opnum) {
6202
101
      break;
6203
101
    }
6204
961
    if (elem->finally_op && opnum < elem->finally_op - 1
6205
628
      && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op)
6206
961
    ) {
6207
374
      remove_oplines--;
6208
374
    }
6209
961
  }
6210
6211
934
  opline->opcode = ZEND_JMP;
6212
934
  SET_UNUSED(opline->op1);
6213
934
  SET_UNUSED(opline->op2);
6214
934
  SET_UNUSED(opline->result);
6215
934
  opline->op1.opline_num = dest->opline_num;
6216
934
  opline->extended_value = 0;
6217
6218
934
  ZEND_ASSERT(remove_oplines >= 0);
6219
1.24k
  while (remove_oplines--) {
6220
310
    opline--;
6221
310
    MAKE_NOP(opline);
6222
310
    ZEND_VM_SET_OPCODE_HANDLER(opline);
6223
310
  }
6224
934
}
6225
/* }}} */
6226
6227
static void zend_compile_goto(const zend_ast *ast) /* {{{ */
6228
1.13k
{
6229
1.13k
  zend_ast *label_ast = ast->child[0];
6230
1.13k
  znode label_node;
6231
1.13k
  zend_op *opline;
6232
6233
1.13k
  zend_compile_expr(&label_node, label_ast);
6234
6235
  /* Label resolution and unwinding adjustments happen in pass two. */
6236
1.13k
  uint32_t opnum_start = get_next_op_number();
6237
1.13k
  zend_handle_loops_and_finally(NULL);
6238
1.13k
  opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
6239
1.13k
  opline->op1.num = get_next_op_number() - opnum_start - 1;
6240
1.13k
  opline->extended_value = CG(context).current_brk_cont;
6241
1.13k
}
6242
/* }}} */
6243
6244
static void zend_compile_label(const zend_ast *ast) /* {{{ */
6245
2.67k
{
6246
2.67k
  zend_string *label = zend_ast_get_str(ast->child[0]);
6247
2.67k
  zend_label dest;
6248
6249
2.67k
  if (!CG(context).labels) {
6250
1.92k
    ALLOC_HASHTABLE(CG(context).labels);
6251
1.92k
    zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0);
6252
1.92k
  }
6253
6254
2.67k
  dest.brk_cont = CG(context).current_brk_cont;
6255
2.67k
  dest.opline_num = get_next_op_number();
6256
6257
2.67k
  if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
6258
43
    zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
6259
43
  }
6260
2.67k
}
6261
/* }}} */
6262
6263
static void zend_compile_while(const zend_ast *ast) /* {{{ */
6264
2.64k
{
6265
2.64k
  zend_ast *cond_ast = ast->child[0];
6266
2.64k
  zend_ast *stmt_ast = ast->child[1];
6267
2.64k
  znode cond_node;
6268
2.64k
  uint32_t opnum_start, opnum_jmp, opnum_cond;
6269
6270
2.64k
  opnum_jmp = zend_emit_jump(0);
6271
6272
2.64k
  zend_begin_loop(ZEND_NOP, NULL, false);
6273
6274
2.64k
  opnum_start = get_next_op_number();
6275
2.64k
  zend_compile_stmt(stmt_ast);
6276
6277
2.64k
  opnum_cond = get_next_op_number();
6278
2.64k
  zend_update_jump_target(opnum_jmp, opnum_cond);
6279
2.64k
  zend_compile_expr(&cond_node, cond_ast);
6280
6281
2.64k
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6282
6283
2.64k
  zend_end_loop(opnum_cond, NULL);
6284
2.64k
}
6285
/* }}} */
6286
6287
static void zend_compile_do_while(const zend_ast *ast) /* {{{ */
6288
784
{
6289
784
  zend_ast *stmt_ast = ast->child[0];
6290
784
  zend_ast *cond_ast = ast->child[1];
6291
6292
784
  znode cond_node;
6293
784
  uint32_t opnum_start, opnum_cond;
6294
6295
784
  zend_begin_loop(ZEND_NOP, NULL, false);
6296
6297
784
  opnum_start = get_next_op_number();
6298
784
  zend_compile_stmt(stmt_ast);
6299
6300
784
  opnum_cond = get_next_op_number();
6301
784
  zend_compile_expr(&cond_node, cond_ast);
6302
6303
784
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6304
6305
784
  zend_end_loop(opnum_cond, NULL);
6306
784
}
6307
/* }}} */
6308
6309
static void zend_compile_for_expr_list(znode *result, zend_ast *ast) /* {{{ */
6310
31.3k
{
6311
31.3k
  const zend_ast_list *list;
6312
31.3k
  uint32_t i;
6313
6314
31.3k
  result->op_type = IS_CONST;
6315
31.3k
  ZVAL_TRUE(&result->u.constant);
6316
6317
31.3k
  if (!ast) {
6318
6.29k
    return;
6319
6.29k
  }
6320
6321
25.0k
  list = zend_ast_get_list(ast);
6322
51.9k
  for (i = 0; i < list->children; ++i) {
6323
26.9k
    zend_ast *expr_ast = list->child[i];
6324
6325
26.9k
    zend_do_free(result);
6326
26.9k
    if (expr_ast->kind == ZEND_AST_CAST_VOID) {
6327
118
      zend_compile_void_cast(NULL, expr_ast);
6328
118
      result->op_type = IS_CONST;
6329
118
      ZVAL_NULL(&result->u.constant);
6330
26.7k
    } else {
6331
26.7k
      zend_compile_expr(result, expr_ast);
6332
26.7k
    }
6333
26.9k
  }
6334
25.0k
}
6335
/* }}} */
6336
6337
static void zend_compile_for(const zend_ast *ast) /* {{{ */
6338
10.5k
{
6339
10.5k
  zend_ast *init_ast = ast->child[0];
6340
10.5k
  zend_ast *cond_ast = ast->child[1];
6341
10.5k
  zend_ast *loop_ast = ast->child[2];
6342
10.5k
  zend_ast *stmt_ast = ast->child[3];
6343
6344
10.5k
  znode result;
6345
10.5k
  uint32_t opnum_start, opnum_jmp, opnum_loop;
6346
6347
10.5k
  zend_compile_for_expr_list(&result, init_ast);
6348
10.5k
  zend_do_free(&result);
6349
6350
10.5k
  opnum_jmp = zend_emit_jump(0);
6351
6352
10.5k
  zend_begin_loop(ZEND_NOP, NULL, false);
6353
6354
10.5k
  opnum_start = get_next_op_number();
6355
10.5k
  zend_compile_stmt(stmt_ast);
6356
6357
10.5k
  opnum_loop = get_next_op_number();
6358
10.5k
  zend_compile_for_expr_list(&result, loop_ast);
6359
10.5k
  zend_do_free(&result);
6360
6361
10.5k
  zend_update_jump_target_to_next(opnum_jmp);
6362
10.5k
  zend_compile_for_expr_list(&result, cond_ast);
6363
10.5k
  zend_do_extended_stmt(NULL);
6364
6365
10.5k
  zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
6366
6367
10.5k
  zend_end_loop(opnum_loop, NULL);
6368
10.5k
}
6369
/* }}} */
6370
6371
static void zend_compile_foreach(zend_ast *ast) /* {{{ */
6372
18.0k
{
6373
18.0k
  zend_ast *expr_ast = ast->child[0];
6374
18.0k
  zend_ast *value_ast = ast->child[1];
6375
18.0k
  zend_ast *key_ast = ast->child[2];
6376
18.0k
  zend_ast *stmt_ast = ast->child[3];
6377
18.0k
  bool by_ref = value_ast->kind == ZEND_AST_REF;
6378
18.0k
  bool is_variable = (zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast))
6379
3.67k
    || zend_is_call(expr_ast);
6380
6381
18.0k
  znode expr_node, reset_node, value_node, key_node;
6382
18.0k
  zend_op *opline;
6383
18.0k
  uint32_t opnum_reset, opnum_fetch;
6384
6385
18.0k
  if (key_ast) {
6386
1.40k
    if (key_ast->kind == ZEND_AST_REF) {
6387
8
      zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
6388
8
    }
6389
1.39k
    if (key_ast->kind == ZEND_AST_ARRAY) {
6390
8
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
6391
8
    }
6392
1.39k
  }
6393
6394
18.0k
  if (by_ref) {
6395
1.25k
    value_ast = value_ast->child[0];
6396
1.25k
  }
6397
6398
18.0k
  if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
6399
88
    by_ref = true;
6400
88
  }
6401
6402
18.0k
  if (by_ref && is_variable) {
6403
983
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
6404
17.0k
  } else {
6405
17.0k
    zend_compile_expr(&expr_node, expr_ast);
6406
17.0k
  }
6407
6408
18.0k
  if (by_ref) {
6409
1.34k
    zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
6410
1.34k
  }
6411
6412
18.0k
  opnum_reset = get_next_op_number();
6413
18.0k
  opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
6414
18.0k
  if (!by_ref) {
6415
16.7k
    opline->result_type = IS_TMP_VAR;
6416
16.7k
    reset_node.op_type = IS_TMP_VAR;
6417
16.7k
  }
6418
6419
18.0k
  zend_begin_loop(ZEND_FE_FREE, &reset_node, false);
6420
6421
18.0k
  opnum_fetch = get_next_op_number();
6422
18.0k
  opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
6423
6424
18.0k
  if (is_this_fetch(value_ast)) {
6425
8
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6426
18.0k
  } else if (value_ast->kind == ZEND_AST_VAR &&
6427
17.4k
    zend_try_compile_cv(&value_node, value_ast, BP_VAR_R) == SUCCESS) {
6428
17.1k
    SET_NODE(opline->op2, &value_node);
6429
17.1k
  } else {
6430
911
    opline->op2_type = by_ref ? IS_VAR : IS_TMP_VAR;
6431
911
    opline->op2.var = get_temporary_variable();
6432
911
    GET_NODE(&value_node, opline->op2);
6433
911
    if (value_ast->kind == ZEND_AST_ARRAY) {
6434
339
      zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr, BP_VAR_R);
6435
572
    } else if (by_ref) {
6436
240
      zend_emit_assign_ref_znode(value_ast, &value_node);
6437
332
    } else {
6438
332
      zend_emit_assign_znode(value_ast, &value_node);
6439
332
    }
6440
911
  }
6441
6442
18.0k
  if (key_ast) {
6443
1.38k
    opline = &CG(active_op_array)->opcodes[opnum_fetch];
6444
1.38k
    zend_make_tmp_result(&key_node, opline);
6445
1.38k
    zend_emit_assign_znode(key_ast, &key_node);
6446
1.38k
  }
6447
6448
18.0k
  zend_compile_stmt(stmt_ast);
6449
6450
  /* Place JMP and FE_FREE on the line where foreach starts. It would be
6451
   * better to use the end line, but this information is not available
6452
   * currently. */
6453
18.0k
  CG(zend_lineno) = ast->lineno;
6454
18.0k
  zend_emit_jump(opnum_fetch);
6455
6456
18.0k
  opline = &CG(active_op_array)->opcodes[opnum_reset];
6457
18.0k
  opline->op2.opline_num = get_next_op_number();
6458
6459
18.0k
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
6460
18.0k
  opline->extended_value = get_next_op_number();
6461
6462
18.0k
  zend_end_loop(opnum_fetch, &reset_node);
6463
6464
18.0k
  opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
6465
18.0k
}
6466
/* }}} */
6467
6468
static void zend_compile_if(zend_ast *ast) /* {{{ */
6469
150k
{
6470
150k
  const zend_ast_list *list = zend_ast_get_list(ast);
6471
150k
  uint32_t i;
6472
150k
  uint32_t *jmp_opnums = NULL;
6473
6474
150k
  if (list->children > 1) {
6475
51.8k
    jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
6476
51.8k
  }
6477
6478
395k
  for (i = 0; i < list->children; ++i) {
6479
244k
    const zend_ast *elem_ast = list->child[i];
6480
244k
    zend_ast *cond_ast = elem_ast->child[0];
6481
244k
    zend_ast *stmt_ast = elem_ast->child[1];
6482
6483
244k
    if (cond_ast) {
6484
193k
      znode cond_node;
6485
193k
      uint32_t opnum_jmpz;
6486
6487
193k
      if (i > 0) {
6488
43.1k
        CG(zend_lineno) = cond_ast->lineno;
6489
43.1k
        zend_do_extended_stmt(NULL);
6490
43.1k
      }
6491
6492
193k
      zend_compile_expr(&cond_node, cond_ast);
6493
193k
      opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6494
6495
193k
      zend_compile_stmt(stmt_ast);
6496
6497
193k
      if (i != list->children - 1) {
6498
        /* Set the lineno of JMP to the position of the if keyword, as we don't want to
6499
         * report the last line in the if branch as covered if it hasn't actually executed. */
6500
94.6k
        CG(zend_lineno) = elem_ast->lineno;
6501
94.6k
        jmp_opnums[i] = zend_emit_jump(0);
6502
94.6k
      }
6503
193k
      zend_update_jump_target_to_next(opnum_jmpz);
6504
193k
    } else {
6505
      /* "else" can only occur as last element. */
6506
51.5k
      ZEND_ASSERT(i == list->children - 1);
6507
51.5k
      zend_compile_stmt(stmt_ast);
6508
51.5k
    }
6509
244k
  }
6510
6511
150k
  if (list->children > 1) {
6512
146k
    for (i = 0; i < list->children - 1; ++i) {
6513
94.6k
      zend_update_jump_target_to_next(jmp_opnums[i]);
6514
94.6k
    }
6515
51.7k
    efree(jmp_opnums);
6516
51.7k
  }
6517
150k
}
6518
/* }}} */
6519
6520
8.34k
static uint8_t determine_switch_jumptable_type(const zend_ast_list *cases) {
6521
8.34k
  uint32_t i;
6522
8.34k
  uint8_t common_type = IS_UNDEF;
6523
23.5k
  for (i = 0; i < cases->children; i++) {
6524
18.8k
    zend_ast *case_ast = cases->child[i];
6525
18.8k
    zend_ast **cond_ast = &case_ast->child[0];
6526
18.8k
    const zval *cond_zv;
6527
18.8k
    if (!case_ast->child[0]) {
6528
      /* Skip default clause */
6529
198
      continue;
6530
198
    }
6531
6532
18.6k
    zend_eval_const_expr(cond_ast);
6533
18.6k
    if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6534
      /* Non-constant case */
6535
446
      return IS_UNDEF;
6536
446
    }
6537
6538
18.2k
    cond_zv = zend_ast_get_zval(case_ast->child[0]);
6539
18.2k
    if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6540
      /* We only optimize switched on integers and strings */
6541
3.06k
      return IS_UNDEF;
6542
3.06k
    }
6543
6544
15.1k
    if (common_type == IS_UNDEF) {
6545
7.72k
      common_type = Z_TYPE_P(cond_zv);
6546
7.72k
    } else if (common_type != Z_TYPE_P(cond_zv)) {
6547
      /* Non-uniform case types */
6548
41
      return IS_UNDEF;
6549
41
    }
6550
6551
15.1k
    if (Z_TYPE_P(cond_zv) == IS_STRING
6552
14.2k
        && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) {
6553
      /* Numeric strings cannot be compared with a simple hash lookup */
6554
123
      return IS_UNDEF;
6555
123
    }
6556
15.1k
  }
6557
6558
4.66k
  return common_type;
6559
8.34k
}
6560
6561
4.52k
static bool should_use_jumptable(const zend_ast_list *cases, uint8_t jumptable_type) {
6562
4.52k
  if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) {
6563
0
    return 0;
6564
0
  }
6565
6566
  /* Thresholds are chosen based on when the average switch time for equidistributed
6567
   * input becomes smaller when using the jumptable optimization. */
6568
4.52k
  if (jumptable_type == IS_LONG) {
6569
164
    return cases->children >= 5;
6570
4.35k
  } else {
6571
4.35k
    ZEND_ASSERT(jumptable_type == IS_STRING);
6572
4.35k
    return cases->children >= 2;
6573
4.35k
  }
6574
4.52k
}
6575
6576
static void zend_compile_switch(zend_ast *ast) /* {{{ */
6577
8.34k
{
6578
8.34k
  zend_ast *expr_ast = ast->child[0];
6579
8.34k
  zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
6580
6581
8.34k
  uint32_t i;
6582
8.34k
  bool has_default_case = false;
6583
6584
8.34k
  znode expr_node, case_node;
6585
8.34k
  zend_op *opline;
6586
8.34k
  uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1;
6587
8.34k
  uint8_t jumptable_type;
6588
8.34k
  HashTable *jumptable = NULL;
6589
6590
8.34k
  zend_compile_expr(&expr_node, expr_ast);
6591
6592
8.34k
  zend_begin_loop(ZEND_FREE, &expr_node, true);
6593
6594
8.34k
  case_node.op_type = IS_TMP_VAR;
6595
8.34k
  case_node.u.op.var = get_temporary_variable();
6596
6597
8.34k
  jumptable_type = determine_switch_jumptable_type(cases);
6598
8.34k
  if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) {
6599
3.96k
    znode jumptable_op;
6600
6601
3.96k
    ALLOC_HASHTABLE(jumptable);
6602
3.96k
    zend_hash_init(jumptable, cases->children, NULL, NULL, 0);
6603
3.96k
    jumptable_op.op_type = IS_CONST;
6604
3.96k
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6605
6606
3.96k
    opline = zend_emit_op(NULL,
6607
3.96k
      jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING,
6608
3.96k
      &expr_node, &jumptable_op);
6609
3.96k
    if (opline->op1_type == IS_CONST) {
6610
3.54k
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6611
3.54k
    }
6612
3.96k
    opnum_switch = opline - CG(active_op_array)->opcodes;
6613
3.96k
  }
6614
6615
8.34k
  jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
6616
28.7k
  for (i = 0; i < cases->children; ++i) {
6617
20.4k
    zend_ast *case_ast = cases->child[i];
6618
20.4k
    zend_ast *cond_ast = case_ast->child[0];
6619
20.4k
    znode cond_node;
6620
6621
20.4k
    if (case_ast->attr == ZEND_ALT_CASE_SYNTAX) {
6622
95
      CG(zend_lineno) = case_ast->lineno;
6623
95
      zend_error(E_DEPRECATED, "Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead");
6624
95
    }
6625
6626
20.4k
    if (!cond_ast) {
6627
207
      if (has_default_case) {
6628
13
        CG(zend_lineno) = case_ast->lineno;
6629
13
        zend_error_noreturn(E_COMPILE_ERROR,
6630
13
          "Switch statements may only contain one default clause");
6631
13
      }
6632
194
      has_default_case = true;
6633
194
      continue;
6634
207
    }
6635
6636
20.2k
    zend_compile_expr(&cond_node, cond_ast);
6637
6638
20.2k
    if (expr_node.op_type == IS_CONST
6639
15.2k
      && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
6640
986
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6641
19.2k
    } else if (expr_node.op_type == IS_CONST
6642
14.2k
      && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
6643
1.08k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
6644
18.1k
    } else {
6645
18.1k
      opline = zend_emit_op(NULL,
6646
18.1k
        (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL,
6647
18.1k
        &expr_node, &cond_node);
6648
18.1k
      SET_NODE(opline->result, &case_node);
6649
18.1k
      if (opline->op1_type == IS_CONST) {
6650
13.1k
        Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6651
13.1k
      }
6652
6653
18.1k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6654
18.1k
    }
6655
20.2k
  }
6656
6657
8.33k
  opnum_default_jmp = zend_emit_jump(0);
6658
6659
28.6k
  for (i = 0; i < cases->children; ++i) {
6660
20.3k
    zend_ast *case_ast = cases->child[i];
6661
20.3k
    zend_ast *cond_ast = case_ast->child[0];
6662
20.3k
    zend_ast *stmt_ast = case_ast->child[1];
6663
6664
20.3k
    if (cond_ast) {
6665
20.1k
      zend_update_jump_target_to_next(jmpnz_opnums[i]);
6666
6667
20.1k
      if (jumptable) {
6668
9.61k
        zval *cond_zv = zend_ast_get_zval(cond_ast);
6669
9.61k
        zval jmp_target;
6670
9.61k
        ZVAL_LONG(&jmp_target, get_next_op_number());
6671
6672
9.61k
        ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type);
6673
9.61k
        if (Z_TYPE_P(cond_zv) == IS_LONG) {
6674
586
          zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6675
9.02k
        } else {
6676
9.02k
          ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6677
9.02k
          zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6678
9.02k
        }
6679
9.61k
      }
6680
20.1k
    } else {
6681
181
      zend_update_jump_target_to_next(opnum_default_jmp);
6682
6683
181
      if (jumptable) {
6684
43
        ZEND_ASSERT(opnum_switch != (uint32_t)-1);
6685
43
        opline = &CG(active_op_array)->opcodes[opnum_switch];
6686
43
        opline->extended_value = get_next_op_number();
6687
43
      }
6688
181
    }
6689
6690
20.3k
    zend_compile_stmt(stmt_ast);
6691
20.3k
  }
6692
6693
8.33k
  if (!has_default_case) {
6694
8.14k
    zend_update_jump_target_to_next(opnum_default_jmp);
6695
6696
8.14k
    if (jumptable) {
6697
3.91k
      opline = &CG(active_op_array)->opcodes[opnum_switch];
6698
3.91k
      opline->extended_value = get_next_op_number();
6699
3.91k
    }
6700
8.14k
  }
6701
6702
8.33k
  zend_end_loop(get_next_op_number(), &expr_node);
6703
6704
8.33k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
6705
1.80k
    opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6706
1.80k
    opline->extended_value = ZEND_FREE_SWITCH;
6707
6.53k
  } else if (expr_node.op_type == IS_CONST) {
6708
6.22k
    zval_ptr_dtor_nogc(&expr_node.u.constant);
6709
6.22k
  }
6710
6711
8.33k
  efree(jmpnz_opnums);
6712
8.33k
}
6713
/* }}} */
6714
6715
static uint32_t count_match_conds(const zend_ast_list *arms)
6716
2.29k
{
6717
2.29k
  uint32_t num_conds = 0;
6718
6719
5.71k
  for (uint32_t i = 0; i < arms->children; i++) {
6720
3.42k
    const zend_ast *arm_ast = arms->child[i];
6721
3.42k
    if (arm_ast->child[0] == NULL) {
6722
602
      continue;
6723
602
    }
6724
6725
2.82k
    const zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6726
2.82k
    num_conds += conds->children;
6727
2.82k
  }
6728
6729
2.29k
  return num_conds;
6730
2.29k
}
6731
6732
2.29k
static bool can_match_use_jumptable(const zend_ast_list *arms) {
6733
4.11k
  for (uint32_t i = 0; i < arms->children; i++) {
6734
2.89k
    const zend_ast *arm_ast = arms->child[i];
6735
2.89k
    if (!arm_ast->child[0]) {
6736
      /* Skip default arm */
6737
474
      continue;
6738
474
    }
6739
6740
2.42k
    zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6741
6.08k
    for (uint32_t j = 0; j < conds->children; j++) {
6742
4.73k
      zend_ast **cond_ast = &conds->child[j];
6743
6744
4.73k
      zend_eval_const_expr(cond_ast);
6745
4.73k
      if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6746
930
        return 0;
6747
930
      }
6748
6749
3.80k
      const zval *cond_zv = zend_ast_get_zval(*cond_ast);
6750
3.80k
      if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6751
144
        return 0;
6752
144
      }
6753
3.80k
    }
6754
2.42k
  }
6755
6756
1.21k
  return 1;
6757
2.29k
}
6758
6759
static bool zend_is_pipe_optimizable_callable_name(zend_ast *ast)
6760
376
{
6761
376
  if (ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(ast)) == IS_STRING) {
6762
    /* Assert compilation adds a message operand, but this is incompatible with the
6763
     * pipe optimization that uses a temporary znode for the reference elimination.
6764
     * Therefore, disable the optimization for assert.
6765
     * Note that "assert" as a name is always treated as fully qualified. */
6766
312
    return !zend_string_equals_literal_ci(zend_ast_get_str(ast), "assert");
6767
312
  }
6768
6769
64
  return true;
6770
376
}
6771
6772
static void zend_compile_pipe(znode *result, zend_ast *ast, uint32_t type)
6773
110k
{
6774
110k
  zend_ast *operand_ast = ast->child[0];
6775
110k
  zend_ast *callable_ast = ast->child[1];
6776
6777
110k
  if (callable_ast->kind == ZEND_AST_ARROW_FUNC && !(callable_ast->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) {
6778
7
    zend_error_noreturn(E_COMPILE_ERROR, "Arrow functions on the right hand side of |> must be parenthesized");
6779
7
  }
6780
6781
  /* Compile the left hand side down to a value first. */
6782
110k
  znode operand_result;
6783
110k
  zend_compile_expr(&operand_result, operand_ast);
6784
6785
  /* Wrap simple values in a ZEND_QM_ASSIGN opcode to ensure references
6786
   * always fail. They will already fail in complex cases like arrays,
6787
   * so those don't need a wrapper. */
6788
110k
  znode wrapped_operand_result;
6789
110k
  if (operand_result.op_type & (IS_CV|IS_VAR)) {
6790
60
    zend_emit_op_tmp(&wrapped_operand_result, ZEND_QM_ASSIGN, &operand_result, NULL);
6791
110k
  } else {
6792
110k
    wrapped_operand_result = operand_result;
6793
110k
  }
6794
6795
  /* Turn the operand into a function parameter list. */
6796
110k
  zend_ast *arg_list_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&wrapped_operand_result));
6797
6798
110k
  zend_ast *fcall_ast;
6799
110k
  znode callable_result;
6800
6801
  /* Turn $foo |> bar(...) into bar($foo). */
6802
110k
  if (callable_ast->kind == ZEND_AST_CALL
6803
499
    && callable_ast->child[1]->kind == ZEND_AST_CALLABLE_CONVERT
6804
376
    && zend_is_pipe_optimizable_callable_name(callable_ast->child[0])) {
6805
356
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6806
356
        callable_ast->child[0], arg_list_ast);
6807
  /* Turn $foo |> bar::baz(...) into bar::baz($foo). */
6808
110k
  } else if (callable_ast->kind == ZEND_AST_STATIC_CALL
6809
347
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6810
95
    fcall_ast = zend_ast_create(ZEND_AST_STATIC_CALL,
6811
95
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6812
  /* Turn $foo |> $bar->baz(...) into $bar->baz($foo). */
6813
110k
  } else if (callable_ast->kind == ZEND_AST_METHOD_CALL
6814
1.83k
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6815
1.27k
    fcall_ast = zend_ast_create(ZEND_AST_METHOD_CALL,
6816
1.27k
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6817
  /* Turn $foo |> $expr into ($expr)($foo) */
6818
109k
  } else {
6819
109k
    zend_compile_expr(&callable_result, callable_ast);
6820
109k
    callable_ast = zend_ast_create_znode(&callable_result);
6821
109k
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6822
109k
      callable_ast, arg_list_ast);
6823
109k
  }
6824
6825
110k
  zend_do_extended_stmt(&operand_result);
6826
6827
110k
  zend_compile_var(result, fcall_ast, type, /* by_ref */ false);
6828
110k
}
6829
6830
static void zend_compile_match(znode *result, zend_ast *ast)
6831
2.29k
{
6832
2.29k
  zend_ast *expr_ast = ast->child[0];
6833
2.29k
  zend_ast_list *arms = zend_ast_get_list(ast->child[1]);
6834
2.29k
  bool has_default_arm = false;
6835
2.29k
  uint32_t opnum_match = (uint32_t)-1;
6836
6837
2.29k
  znode expr_node;
6838
2.29k
  zend_compile_expr(&expr_node, expr_ast);
6839
6840
2.29k
  znode case_node;
6841
2.29k
  case_node.op_type = IS_TMP_VAR;
6842
2.29k
  case_node.u.op.var = get_temporary_variable();
6843
6844
2.29k
  uint32_t num_conds = count_match_conds(arms);
6845
2.29k
  uint8_t can_use_jumptable = can_match_use_jumptable(arms);
6846
2.29k
  bool uses_jumptable = can_use_jumptable && num_conds >= 2;
6847
2.29k
  HashTable *jumptable = NULL;
6848
2.29k
  uint32_t *jmpnz_opnums = NULL;
6849
6850
5.71k
  for (uint32_t i = 0; i < arms->children; ++i) {
6851
3.42k
    zend_ast *arm_ast = arms->child[i];
6852
6853
3.42k
    if (!arm_ast->child[0]) {
6854
602
      if (has_default_arm) {
6855
8
        CG(zend_lineno) = arm_ast->lineno;
6856
8
        zend_error_noreturn(E_COMPILE_ERROR,
6857
8
          "Match expressions may only contain one default arm");
6858
8
      }
6859
594
      has_default_arm = true;
6860
594
    }
6861
3.42k
  }
6862
6863
2.28k
  if (uses_jumptable) {
6864
652
    znode jumptable_op;
6865
6866
652
    ALLOC_HASHTABLE(jumptable);
6867
652
    zend_hash_init(jumptable, num_conds, NULL, NULL, 0);
6868
652
    jumptable_op.op_type = IS_CONST;
6869
652
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6870
6871
652
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH, &expr_node, &jumptable_op);
6872
652
    if (opline->op1_type == IS_CONST) {
6873
322
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6874
322
    }
6875
652
    opnum_match = opline - CG(active_op_array)->opcodes;
6876
1.63k
  } else {
6877
1.63k
    jmpnz_opnums = safe_emalloc(sizeof(uint32_t), num_conds, 0);
6878
1.63k
    uint32_t cond_count = 0;
6879
3.81k
    for (uint32_t i = 0; i < arms->children; ++i) {
6880
2.18k
      zend_ast *arm_ast = arms->child[i];
6881
6882
2.18k
      if (!arm_ast->child[0]) {
6883
436
        continue;
6884
436
      }
6885
6886
1.74k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6887
4.67k
      for (uint32_t j = 0; j < conds->children; j++) {
6888
2.92k
        zend_ast *cond_ast = conds->child[j];
6889
6890
2.92k
        znode cond_node;
6891
2.92k
        zend_compile_expr(&cond_node, cond_ast);
6892
6893
2.92k
        uint32_t opcode = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE_STRICT : ZEND_IS_IDENTICAL;
6894
2.92k
        zend_op *opline = zend_emit_op(NULL, opcode, &expr_node, &cond_node);
6895
2.92k
        SET_NODE(opline->result, &case_node);
6896
2.92k
        if (opline->op1_type == IS_CONST) {
6897
1.28k
          Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6898
1.28k
        }
6899
6900
2.92k
        jmpnz_opnums[cond_count] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6901
6902
2.92k
        cond_count++;
6903
2.92k
      }
6904
1.74k
    }
6905
1.63k
  }
6906
6907
2.28k
  uint32_t opnum_default_jmp = 0;
6908
2.28k
  if (!uses_jumptable) {
6909
1.62k
    opnum_default_jmp = zend_emit_jump(0);
6910
1.62k
  }
6911
6912
2.28k
  bool is_first_case = true;
6913
2.28k
  uint32_t cond_count = 0;
6914
2.28k
  uint32_t *jmp_end_opnums = safe_emalloc(sizeof(uint32_t), arms->children, 0);
6915
6916
  // The generated default arm is emitted first to avoid live range issues where the tmpvar
6917
  // for the arm result is freed even though it has not been initialized yet.
6918
2.28k
  if (!has_default_arm) {
6919
1.69k
    if (!uses_jumptable) {
6920
1.19k
      zend_update_jump_target_to_next(opnum_default_jmp);
6921
1.19k
    }
6922
6923
1.69k
    if (jumptable) {
6924
502
      zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6925
502
      opline->extended_value = get_next_op_number();
6926
502
    }
6927
6928
1.69k
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH_ERROR, &expr_node, NULL);
6929
1.69k
    if (opline->op1_type == IS_CONST) {
6930
457
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6931
457
    }
6932
1.69k
    if (arms->children == 0) {
6933
      /* Mark this as an "expression throw" for opcache. */
6934
192
      opline->extended_value = ZEND_THROW_IS_EXPR;
6935
192
    }
6936
1.69k
  }
6937
6938
5.68k
  for (uint32_t i = 0; i < arms->children; ++i) {
6939
3.39k
    zend_ast *arm_ast = arms->child[i];
6940
3.39k
    zend_ast *body_ast = arm_ast->child[1];
6941
6942
3.39k
    if (arm_ast->child[0] != NULL) {
6943
2.81k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6944
6945
8.57k
      for (uint32_t j = 0; j < conds->children; j++) {
6946
5.76k
        zend_ast *cond_ast = conds->child[j];
6947
6948
5.76k
        if (jmpnz_opnums != NULL) {
6949
2.92k
          zend_update_jump_target_to_next(jmpnz_opnums[cond_count]);
6950
2.92k
        }
6951
6952
5.76k
        if (jumptable) {
6953
2.83k
          zval *cond_zv = zend_ast_get_zval(cond_ast);
6954
2.83k
          zval jmp_target;
6955
2.83k
          ZVAL_LONG(&jmp_target, get_next_op_number());
6956
6957
2.83k
          if (Z_TYPE_P(cond_zv) == IS_LONG) {
6958
2.53k
            zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6959
2.53k
          } else {
6960
301
            ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6961
301
            zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6962
301
          }
6963
2.83k
        }
6964
6965
5.76k
        cond_count++;
6966
5.76k
      }
6967
2.81k
    } else {
6968
586
      if (!uses_jumptable) {
6969
436
        zend_update_jump_target_to_next(opnum_default_jmp);
6970
436
      }
6971
6972
586
      if (jumptable) {
6973
150
        ZEND_ASSERT(opnum_match != (uint32_t)-1);
6974
150
        zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6975
150
        opline->extended_value = get_next_op_number();
6976
150
      }
6977
586
    }
6978
6979
3.39k
    znode body_node;
6980
3.39k
    zend_compile_expr(&body_node, body_ast);
6981
6982
3.39k
    if (is_first_case) {
6983
2.08k
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL);
6984
2.08k
      is_first_case = false;
6985
2.08k
    } else {
6986
1.30k
      zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL);
6987
1.30k
      SET_NODE(opline_qm_assign->result, result);
6988
1.30k
    }
6989
6990
3.39k
    jmp_end_opnums[i] = zend_emit_jump(0);
6991
3.39k
  }
6992
6993
  // Initialize result in case there is no arm
6994
2.28k
  if (arms->children == 0) {
6995
192
    result->op_type = IS_CONST;
6996
192
    ZVAL_NULL(&result->u.constant);
6997
192
  }
6998
6999
5.68k
  for (uint32_t i = 0; i < arms->children; ++i) {
7000
3.39k
    zend_update_jump_target_to_next(jmp_end_opnums[i]);
7001
3.39k
  }
7002
7003
2.28k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
7004
1.22k
    zend_op *opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
7005
1.22k
    opline->extended_value = ZEND_FREE_SWITCH;
7006
1.22k
  } else if (expr_node.op_type == IS_CONST) {
7007
790
    zval_ptr_dtor_nogc(&expr_node.u.constant);
7008
790
  }
7009
7010
2.28k
  if (jmpnz_opnums != NULL) {
7011
1.62k
    efree(jmpnz_opnums);
7012
1.62k
  }
7013
2.28k
  efree(jmp_end_opnums);
7014
2.28k
}
7015
7016
static void zend_compile_try(const zend_ast *ast) /* {{{ */
7017
30.9k
{
7018
30.9k
  zend_ast *try_ast = ast->child[0];
7019
30.9k
  const zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
7020
30.9k
  zend_ast *finally_ast = ast->child[2];
7021
7022
30.9k
  uint32_t i, j;
7023
30.9k
  zend_op *opline;
7024
30.9k
  uint32_t try_catch_offset;
7025
30.9k
  uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
7026
30.9k
  uint32_t orig_fast_call_var = CG(context).fast_call_var;
7027
30.9k
  uint32_t orig_try_catch_offset = CG(context).try_catch_offset;
7028
7029
30.9k
  if (catches->children == 0 && !finally_ast) {
7030
73
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
7031
73
  }
7032
7033
  /* label: try { } must not be equal to try { label: } */
7034
30.9k
  if (CG(context).labels) {
7035
324
    zend_label *label;
7036
324
    ZEND_HASH_MAP_REVERSE_FOREACH_PTR(CG(context).labels, label) {
7037
324
      if (label->opline_num == get_next_op_number()) {
7038
104
        zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
7039
104
      }
7040
324
      break;
7041
972
    } ZEND_HASH_FOREACH_END();
7042
324
  }
7043
7044
30.9k
  try_catch_offset = zend_add_try_element(get_next_op_number());
7045
7046
30.9k
  if (finally_ast) {
7047
2.59k
    zend_loop_var fast_call;
7048
2.59k
    if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
7049
969
      CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
7050
969
    }
7051
2.59k
    CG(context).fast_call_var = get_temporary_variable();
7052
7053
    /* Push FAST_CALL on unwind stack */
7054
2.59k
    fast_call.opcode = ZEND_FAST_CALL;
7055
2.59k
    fast_call.var_type = IS_TMP_VAR;
7056
2.59k
    fast_call.var_num = CG(context).fast_call_var;
7057
2.59k
    fast_call.try_catch_offset = try_catch_offset;
7058
2.59k
    zend_stack_push(&CG(loop_var_stack), &fast_call);
7059
2.59k
  }
7060
7061
30.9k
  CG(context).try_catch_offset = try_catch_offset;
7062
7063
30.9k
  zend_compile_stmt(try_ast);
7064
7065
30.9k
  if (catches->children != 0) {
7066
28.4k
    jmp_opnums[0] = zend_emit_jump(0);
7067
28.4k
  }
7068
7069
64.4k
  for (i = 0; i < catches->children; ++i) {
7070
33.5k
    const zend_ast *catch_ast = catches->child[i];
7071
33.5k
    const zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]);
7072
33.5k
    zend_ast *var_ast = catch_ast->child[1];
7073
33.5k
    zend_ast *stmt_ast = catch_ast->child[2];
7074
33.5k
    zend_string *var_name = var_ast ? zval_make_interned_string(zend_ast_get_zval(var_ast)) : NULL;
7075
33.5k
    bool is_last_catch = (i + 1 == catches->children);
7076
7077
33.5k
    uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0);
7078
33.5k
    uint32_t opnum_catch = (uint32_t)-1;
7079
7080
33.5k
    CG(zend_lineno) = catch_ast->lineno;
7081
7082
72.1k
    for (j = 0; j < classes->children; j++) {
7083
38.6k
      zend_ast *class_ast = classes->child[j];
7084
38.6k
      bool is_last_class = (j + 1 == classes->children);
7085
7086
38.6k
      if (!zend_is_const_default_class_ref(class_ast)) {
7087
7
        zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
7088
7
      }
7089
7090
38.6k
      opnum_catch = get_next_op_number();
7091
38.6k
      if (i == 0 && j == 0) {
7092
28.4k
        CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
7093
28.4k
      }
7094
7095
38.6k
      opline = get_next_op();
7096
38.6k
      opline->opcode = ZEND_CATCH;
7097
38.6k
      opline->op1_type = IS_CONST;
7098
38.6k
      opline->op1.constant = zend_add_class_name_literal(
7099
38.6k
          zend_resolve_class_name_ast(class_ast));
7100
38.6k
      opline->extended_value = zend_alloc_cache_slot();
7101
7102
38.6k
      if (var_name && zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
7103
7
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
7104
7
      }
7105
7106
38.6k
      opline->result_type = var_name ? IS_CV : IS_UNUSED;
7107
38.6k
      opline->result.var = var_name ? lookup_cv(var_name) : -1;
7108
7109
38.6k
      if (is_last_catch && is_last_class) {
7110
28.4k
        opline->extended_value |= ZEND_LAST_CATCH;
7111
28.4k
      }
7112
7113
38.6k
      if (!is_last_class) {
7114
5.05k
        jmp_multicatch[j] = zend_emit_jump(0);
7115
5.05k
        opline = &CG(active_op_array)->opcodes[opnum_catch];
7116
5.05k
        opline->op2.opline_num = get_next_op_number();
7117
5.05k
      }
7118
38.6k
    }
7119
7120
38.6k
    for (j = 0; j < classes->children - 1; j++) {
7121
5.05k
      zend_update_jump_target_to_next(jmp_multicatch[j]);
7122
5.05k
    }
7123
7124
33.5k
    efree(jmp_multicatch);
7125
7126
33.5k
    zend_compile_stmt(stmt_ast);
7127
7128
33.5k
    if (!is_last_catch) {
7129
5.13k
      jmp_opnums[i + 1] = zend_emit_jump(0);
7130
5.13k
    }
7131
7132
33.5k
    ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class");
7133
33.5k
    opline = &CG(active_op_array)->opcodes[opnum_catch];
7134
33.5k
    if (!is_last_catch) {
7135
5.13k
      opline->op2.opline_num = get_next_op_number();
7136
5.13k
    }
7137
33.5k
  }
7138
7139
64.4k
  for (i = 0; i < catches->children; ++i) {
7140
33.5k
    zend_update_jump_target_to_next(jmp_opnums[i]);
7141
33.5k
  }
7142
7143
30.8k
  if (finally_ast) {
7144
2.58k
    zend_loop_var discard_exception;
7145
2.58k
    uint32_t opnum_jmp = get_next_op_number() + 1;
7146
7147
    /* Pop FAST_CALL from unwind stack */
7148
2.58k
    zend_stack_del_top(&CG(loop_var_stack));
7149
7150
    /* Push DISCARD_EXCEPTION on unwind stack */
7151
2.58k
    discard_exception.opcode = ZEND_DISCARD_EXCEPTION;
7152
2.58k
    discard_exception.var_type = IS_TMP_VAR;
7153
2.58k
    discard_exception.var_num = CG(context).fast_call_var;
7154
2.58k
    zend_stack_push(&CG(loop_var_stack), &discard_exception);
7155
7156
2.58k
    CG(zend_lineno) = finally_ast->lineno;
7157
7158
2.58k
    opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
7159
2.58k
    opline->op1.num = try_catch_offset;
7160
2.58k
    opline->result_type = IS_TMP_VAR;
7161
2.58k
    opline->result.var = CG(context).fast_call_var;
7162
7163
2.58k
    zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
7164
7165
2.58k
    zend_compile_stmt(finally_ast);
7166
7167
2.58k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
7168
2.58k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
7169
2.58k
      = get_next_op_number();
7170
7171
2.58k
    opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
7172
2.58k
    opline->op1_type = IS_TMP_VAR;
7173
2.58k
    opline->op1.var = CG(context).fast_call_var;
7174
2.58k
    opline->op2.num = orig_try_catch_offset;
7175
7176
2.58k
    zend_update_jump_target_to_next(opnum_jmp);
7177
7178
2.58k
    CG(context).fast_call_var = orig_fast_call_var;
7179
7180
    /* Pop DISCARD_EXCEPTION from unwind stack */
7181
2.58k
    zend_stack_del_top(&CG(loop_var_stack));
7182
2.58k
  }
7183
7184
30.8k
  CG(context).try_catch_offset = orig_try_catch_offset;
7185
7186
30.8k
  efree(jmp_opnums);
7187
30.8k
}
7188
/* }}} */
7189
7190
/* Encoding declarations must already be handled during parsing */
7191
bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
7192
2.59k
{
7193
2.59k
  const zend_ast_list *declares = zend_ast_get_list(ast);
7194
2.59k
  uint32_t i;
7195
5.90k
  for (i = 0; i < declares->children; ++i) {
7196
3.34k
    const zend_ast *declare_ast = declares->child[i];
7197
3.34k
    zend_ast *name_ast = declare_ast->child[0];
7198
3.34k
    zend_ast *value_ast = declare_ast->child[1];
7199
3.34k
    const zend_string *name = zend_ast_get_str(name_ast);
7200
7201
3.34k
    if (zend_string_equals_literal_ci(name, "encoding")) {
7202
170
      if (value_ast->kind != ZEND_AST_ZVAL) {
7203
25
        zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0);
7204
25
        return 0;
7205
25
      }
7206
7207
145
      if (CG(multibyte)) {
7208
0
        zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
7209
7210
0
        const zend_encoding *new_encoding, *old_encoding;
7211
0
        zend_encoding_filter old_input_filter;
7212
7213
0
        CG(encoding_declared) = 1;
7214
7215
0
        new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
7216
0
        if (!new_encoding) {
7217
0
          zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
7218
0
        } else {
7219
0
          old_input_filter = LANG_SCNG(input_filter);
7220
0
          old_encoding = LANG_SCNG(script_encoding);
7221
0
          zend_multibyte_set_filter(new_encoding);
7222
7223
          /* need to re-scan if input filter changed */
7224
0
          if (old_input_filter != LANG_SCNG(input_filter) ||
7225
0
             (old_input_filter && new_encoding != old_encoding)) {
7226
0
            zend_multibyte_yyinput_again(old_input_filter, old_encoding);
7227
0
          }
7228
0
        }
7229
7230
0
        zend_string_release_ex(encoding_name, 0);
7231
145
      } else {
7232
145
        zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
7233
145
          "Zend multibyte feature is turned off by settings");
7234
145
      }
7235
145
    }
7236
3.34k
  }
7237
7238
2.56k
  return 1;
7239
2.59k
}
7240
/* }}} */
7241
7242
/* Check whether this is the first statement, not counting declares. */
7243
static zend_result zend_is_first_statement(const zend_ast *ast, bool allow_nop) /* {{{ */
7244
2.91k
{
7245
2.91k
  uint32_t i = 0;
7246
2.91k
  const zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
7247
7248
15.2k
  while (i < file_ast->children) {
7249
15.2k
    if (file_ast->child[i] == ast) {
7250
2.86k
      return SUCCESS;
7251
12.3k
    } else if (file_ast->child[i] == NULL) {
7252
1.18k
      if (!allow_nop) {
7253
0
        return FAILURE;
7254
0
      }
7255
11.1k
    } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
7256
49
      return FAILURE;
7257
49
    }
7258
12.2k
    i++;
7259
12.2k
  }
7260
1
  return FAILURE;
7261
2.91k
}
7262
/* }}} */
7263
7264
static void zend_compile_declare(const zend_ast *ast) /* {{{ */
7265
5.75k
{
7266
5.75k
  const zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
7267
5.75k
  zend_ast *stmt_ast = ast->child[1];
7268
5.75k
  zend_declarables orig_declarables = FC(declarables);
7269
5.75k
  uint32_t i;
7270
7271
16.4k
  for (i = 0; i < declares->children; ++i) {
7272
10.8k
    zend_ast *declare_ast = declares->child[i];
7273
10.8k
    zend_ast *name_ast = declare_ast->child[0];
7274
10.8k
    zend_ast **value_ast_ptr = &declare_ast->child[1];
7275
10.8k
    zend_string *name = zend_ast_get_str(name_ast);
7276
7277
10.8k
    if ((*value_ast_ptr)->kind != ZEND_AST_ZVAL) {
7278
28
      zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
7279
28
    }
7280
7281
10.7k
    if (zend_string_equals_literal_ci(name, "ticks")) {
7282
6.73k
      zval value_zv;
7283
6.73k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7284
6.73k
      FC(declarables).ticks = zval_get_long(&value_zv);
7285
6.73k
      zval_ptr_dtor_nogc(&value_zv);
7286
6.73k
    } else if (zend_string_equals_literal_ci(name, "encoding")) {
7287
7288
88
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ false)) {
7289
6
        zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
7290
6
          "the very first statement in the script");
7291
6
      }
7292
3.97k
    } else if (zend_string_equals_literal_ci(name, "strict_types")) {
7293
466
      zval value_zv;
7294
7295
466
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
7296
16
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
7297
16
          "the very first statement in the script");
7298
16
      }
7299
7300
450
      if (ast->child[1] != NULL) {
7301
8
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
7302
8
          "use block mode");
7303
8
      }
7304
7305
442
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7306
7307
442
      if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
7308
48
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
7309
48
      }
7310
7311
394
      if (Z_LVAL(value_zv) == 1) {
7312
282
        CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
7313
282
      }
7314
7315
3.50k
    } else {
7316
3.50k
      zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
7317
3.50k
    }
7318
10.7k
  }
7319
7320
5.64k
  if (stmt_ast) {
7321
356
    zend_compile_stmt(stmt_ast);
7322
7323
356
    FC(declarables) = orig_declarables;
7324
356
  }
7325
5.64k
}
7326
/* }}} */
7327
7328
static void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
7329
1.61M
{
7330
1.61M
  const zend_ast_list *list = zend_ast_get_list(ast);
7331
1.61M
  uint32_t i;
7332
6.60M
  for (i = 0; i < list->children; ++i) {
7333
4.98M
    zend_compile_stmt(list->child[i]);
7334
4.98M
  }
7335
1.61M
}
7336
/* }}} */
7337
7338
ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
7339
926k
{
7340
926k
  uint32_t i, n;
7341
7342
926k
  func->common.arg_flags[0] = 0;
7343
926k
  func->common.arg_flags[1] = 0;
7344
926k
  func->common.arg_flags[2] = 0;
7345
926k
  if (func->common.arg_info) {
7346
926k
    n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
7347
926k
    i = 0;
7348
1.87M
    while (i < n) {
7349
950k
      ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i]));
7350
950k
      i++;
7351
950k
    }
7352
926k
    if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_VARIADIC) && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) {
7353
713
      uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]);
7354
9.16k
      while (i < MAX_ARG_FLAG_NUM) {
7355
8.45k
        ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
7356
8.45k
        i++;
7357
8.45k
      }
7358
713
    }
7359
926k
  }
7360
926k
}
7361
/* }}} */
7362
7363
static zend_type zend_compile_single_typename(zend_ast *ast)
7364
280k
{
7365
280k
  ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE));
7366
280k
  if (ast->kind == ZEND_AST_TYPE) {
7367
3.91k
    if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) {
7368
5
      zend_error_noreturn(E_COMPILE_ERROR,
7369
5
        "Cannot use \"static\" when no class scope is active");
7370
5
    }
7371
7372
3.90k
    return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0);
7373
277k
  } else {
7374
277k
    zend_string *type_name = zend_ast_get_str(ast);
7375
277k
    uint8_t type_code = zend_lookup_builtin_type_by_name(type_name);
7376
7377
277k
    if (type_code != 0) {
7378
79.3k
      if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) {
7379
5
        zend_error_noreturn(E_COMPILE_ERROR,
7380
5
          "Type declaration '%s' must be unqualified",
7381
5
          ZSTR_VAL(zend_string_tolower(type_name)));
7382
5
      }
7383
7384
      /* Transform iterable into a type union alias */
7385
79.3k
      if (type_code == IS_ITERABLE) {
7386
        /* Set iterable bit for BC compat during Reflection and string representation of type */
7387
55.1k
        zend_type iterable = (zend_type) ZEND_TYPE_INIT_CLASS_MASK(ZSTR_KNOWN(ZEND_STR_TRAVERSABLE),
7388
55.1k
                  (MAY_BE_ARRAY|_ZEND_TYPE_ITERABLE_BIT));
7389
55.1k
        return iterable;
7390
55.1k
      }
7391
7392
24.2k
      return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0);
7393
197k
    } else {
7394
197k
      const char *correct_name;
7395
197k
      uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
7396
197k
      zend_string *class_name = type_name;
7397
7398
197k
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
7399
196k
        class_name = zend_resolve_class_name_ast(ast);
7400
196k
        zend_assert_valid_class_name(class_name, "a type name");
7401
196k
      } else {
7402
824
        ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT);
7403
7404
824
        zend_ensure_valid_class_fetch_type(fetch_type);
7405
7406
824
        bool substitute_self_parent = zend_is_scope_known()
7407
508
          && !(CG(active_class_entry)->ce_flags & ZEND_ACC_ANON_CLASS);
7408
7409
824
        if (fetch_type == ZEND_FETCH_CLASS_SELF) {
7410
          /* Scope might be unknown for unbound closures and traits */
7411
642
          if (substitute_self_parent) {
7412
377
            class_name = CG(active_class_entry)->name;
7413
377
            ZEND_ASSERT(class_name && "must know class name when resolving self type at compile time");
7414
377
          }
7415
642
        } else {
7416
182
          ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_PARENT);
7417
          /* Scope might be unknown for unbound closures and traits */
7418
182
          if (substitute_self_parent) {
7419
118
            class_name = CG(active_class_entry)->parent_name;
7420
118
            ZEND_ASSERT(class_name && "must know class name when resolving parent type at compile time");
7421
118
          }
7422
164
        }
7423
824
        zend_string_addref(class_name);
7424
806
      }
7425
7426
197k
      if (ast->attr == ZEND_NAME_NOT_FQ
7427
197k
          && zend_is_confusable_type(type_name, &correct_name)
7428
20.4k
          && zend_is_not_imported(type_name)) {
7429
20.4k
        const char *extra =
7430
20.4k
          FC(current_namespace) ? " or import the class with \"use\"" : "";
7431
20.4k
        if (correct_name) {
7432
20.1k
          zend_error(E_COMPILE_WARNING,
7433
20.1k
            "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? "
7434
20.1k
            "Write \"\\%s\"%s to suppress this warning",
7435
20.1k
            ZSTR_VAL(type_name), correct_name, ZSTR_VAL(class_name), extra);
7436
20.1k
        } else {
7437
245
          zend_error(E_COMPILE_WARNING,
7438
245
            "\"%s\" is not a supported builtin type "
7439
245
            "and will be interpreted as a class name. "
7440
245
            "Write \"\\%s\"%s to suppress this warning",
7441
245
            ZSTR_VAL(type_name), ZSTR_VAL(class_name), extra);
7442
245
        }
7443
20.4k
      }
7444
7445
197k
      class_name = zend_new_interned_string(class_name);
7446
197k
      zend_alloc_ce_cache(class_name);
7447
197k
      return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, /* allow null */ false, 0);
7448
197k
    }
7449
277k
  }
7450
280k
}
7451
7452
static void zend_are_intersection_types_redundant(const zend_type left_type, const zend_type right_type)
7453
4.28k
{
7454
4.28k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(left_type));
7455
4.28k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(right_type));
7456
4.28k
  const zend_type_list *l_type_list = ZEND_TYPE_LIST(left_type);
7457
4.28k
  const zend_type_list *r_type_list = ZEND_TYPE_LIST(right_type);
7458
4.28k
  const zend_type_list *smaller_type_list, *larger_type_list;
7459
4.28k
  bool flipped = false;
7460
7461
4.28k
  if (r_type_list->num_types < l_type_list->num_types) {
7462
1.47k
    smaller_type_list = r_type_list;
7463
1.47k
    larger_type_list = l_type_list;
7464
1.47k
    flipped = true;
7465
2.80k
  } else {
7466
2.80k
    smaller_type_list = l_type_list;
7467
2.80k
    larger_type_list = r_type_list;
7468
2.80k
  }
7469
7470
4.28k
  unsigned int sum = 0;
7471
4.28k
  const zend_type *outer_type;
7472
16.6k
  ZEND_TYPE_LIST_FOREACH(smaller_type_list, outer_type)
7473
16.6k
    const zend_type *inner_type;
7474
58.8k
    ZEND_TYPE_LIST_FOREACH(larger_type_list, inner_type)
7475
58.8k
      if (zend_string_equals_ci(ZEND_TYPE_NAME(*inner_type), ZEND_TYPE_NAME(*outer_type))) {
7476
4.28k
        sum++;
7477
4.28k
        break;
7478
4.28k
      }
7479
58.8k
    ZEND_TYPE_LIST_FOREACH_END();
7480
16.6k
  ZEND_TYPE_LIST_FOREACH_END();
7481
7482
4.28k
  if (sum == smaller_type_list->num_types) {
7483
21
    zend_string *smaller_type_str;
7484
21
    zend_string *larger_type_str;
7485
21
    if (flipped) {
7486
5
      smaller_type_str = zend_type_to_string(right_type);
7487
5
      larger_type_str = zend_type_to_string(left_type);
7488
16
    } else {
7489
16
      smaller_type_str = zend_type_to_string(left_type);
7490
16
      larger_type_str = zend_type_to_string(right_type);
7491
16
    }
7492
21
    if (smaller_type_list->num_types == larger_type_list->num_types) {
7493
8
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant with type %s",
7494
8
        ZSTR_VAL(smaller_type_str), ZSTR_VAL(larger_type_str));
7495
13
    } else {
7496
13
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7497
13
        ZSTR_VAL(larger_type_str), ZSTR_VAL(smaller_type_str));
7498
13
    }
7499
21
  }
7500
4.28k
}
7501
7502
static void zend_is_intersection_type_redundant_by_single_type(const zend_type intersection_type, const zend_type single_type)
7503
9.70k
{
7504
9.70k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(intersection_type));
7505
9.70k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(single_type));
7506
7507
9.70k
  const zend_type *single_intersection_type = NULL;
7508
33.2k
  ZEND_TYPE_FOREACH(intersection_type, single_intersection_type)
7509
33.2k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(*single_intersection_type), ZEND_TYPE_NAME(single_type))) {
7510
10
      zend_string *single_type_str = zend_type_to_string(single_type);
7511
10
      zend_string *complete_type = zend_type_to_string(intersection_type);
7512
10
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7513
10
          ZSTR_VAL(complete_type), ZSTR_VAL(single_type_str));
7514
10
    }
7515
33.2k
  ZEND_TYPE_FOREACH_END();
7516
9.70k
}
7517
7518
/* Used by both intersection and union types prior to transforming the type list to a full zend_type */
7519
static void zend_is_type_list_redundant_by_single_type(const zend_type_list *type_list, const zend_type type)
7520
29.6k
{
7521
29.6k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(type));
7522
55.6k
  for (size_t i = 0; i < type_list->num_types - 1; i++) {
7523
26.1k
    if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7524
5.56k
      zend_is_intersection_type_redundant_by_single_type(type_list->types[i], type);
7525
5.56k
      continue;
7526
5.56k
    }
7527
20.5k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(type_list->types[i]), ZEND_TYPE_NAME(type))) {
7528
43
      zend_string *single_type_str = zend_type_to_string(type);
7529
43
      zend_error_noreturn(E_COMPILE_ERROR, "Duplicate type %s is redundant", ZSTR_VAL(single_type_str));
7530
43
    }
7531
20.5k
  }
7532
29.6k
}
7533
7534
static zend_type zend_compile_typename(zend_ast *ast);
7535
7536
static zend_type zend_compile_typename_ex(
7537
    zend_ast *ast, bool force_allow_null, bool *forced_allow_null) /* {{{ */
7538
262k
{
7539
262k
  bool is_marked_nullable = ast->attr & ZEND_TYPE_NULLABLE;
7540
262k
  zend_ast_attr orig_ast_attr = ast->attr;
7541
262k
  zend_type type = ZEND_TYPE_INIT_NONE(0);
7542
7543
262k
  if (is_marked_nullable) {
7544
2.58k
    ast->attr &= ~ZEND_TYPE_NULLABLE;
7545
2.58k
  }
7546
7547
262k
  if (ast->kind == ZEND_AST_TYPE_UNION) {
7548
12.6k
    const zend_ast_list *list = zend_ast_get_list(ast);
7549
12.6k
    zend_type_list *type_list;
7550
12.6k
    bool is_composite = false;
7551
12.6k
    bool has_only_iterable_class = true;
7552
12.6k
    ALLOCA_FLAG(use_heap)
7553
7554
12.6k
    type_list = do_alloca(ZEND_TYPE_LIST_SIZE(list->children), use_heap);
7555
12.6k
    type_list->num_types = 0;
7556
7557
40.2k
    for (uint32_t i = 0; i < list->children; i++) {
7558
27.6k
      zend_ast *type_ast = list->child[i];
7559
27.6k
      zend_type single_type;
7560
27.6k
      uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7561
7562
27.6k
      if (type_ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7563
8.17k
        has_only_iterable_class = false;
7564
8.17k
        is_composite = true;
7565
        /* The first class type can be stored directly as the type ptr payload. */
7566
8.17k
        if (ZEND_TYPE_IS_COMPLEX(type) && !ZEND_TYPE_HAS_LIST(type)) {
7567
          /* Switch from single name to name list. */
7568
2.54k
          type_list->num_types = 1;
7569
2.54k
          type_list->types[0] = type;
7570
          /* Clear MAY_BE_* type flags */
7571
2.54k
          ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7572
2.54k
        }
7573
        /* Mark type as list type */
7574
8.17k
        ZEND_TYPE_SET_LIST(type, type_list);
7575
7576
8.17k
        single_type = zend_compile_typename(type_ast);
7577
8.17k
        ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(single_type));
7578
7579
8.17k
        type_list->types[type_list->num_types++] = single_type;
7580
7581
        /* Check for trivially redundant class types */
7582
16.6k
        for (size_t i = 0; i < type_list->num_types - 1; i++) {
7583
8.43k
          if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7584
4.28k
            zend_are_intersection_types_redundant(single_type, type_list->types[i]);
7585
4.28k
            continue;
7586
4.28k
          }
7587
          /* Type from type list is a simple type */
7588
4.14k
          zend_is_intersection_type_redundant_by_single_type(single_type, type_list->types[i]);
7589
4.14k
        }
7590
8.17k
        continue;
7591
8.17k
      }
7592
7593
19.5k
      single_type = zend_compile_single_typename(type_ast);
7594
19.5k
      uint32_t single_type_mask = ZEND_TYPE_PURE_MASK(single_type);
7595
7596
19.5k
      if (single_type_mask == MAY_BE_ANY) {
7597
5
        zend_error_noreturn(E_COMPILE_ERROR, "Type mixed can only be used as a standalone type");
7598
5
      }
7599
19.5k
      if (ZEND_TYPE_IS_COMPLEX(single_type) && !ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7600
14.8k
        has_only_iterable_class = false;
7601
14.8k
      }
7602
7603
19.5k
      uint32_t type_mask_overlap = ZEND_TYPE_PURE_MASK(type) & single_type_mask;
7604
19.5k
      if (type_mask_overlap) {
7605
29
        zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap);
7606
29
        zend_string *overlap_type_str = zend_type_to_string(overlap_type);
7607
29
        zend_error_noreturn(E_COMPILE_ERROR,
7608
29
          "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str));
7609
29
      }
7610
7611
19.4k
      if ( ((type_mask & MAY_BE_TRUE) && (single_type_mask == MAY_BE_FALSE))
7612
19.4k
          || ((type_mask & MAY_BE_FALSE) && (single_type_mask == MAY_BE_TRUE)) ) {
7613
10
        zend_error_noreturn(E_COMPILE_ERROR,
7614
10
          "Type contains both true and false, bool must be used instead");
7615
10
      }
7616
19.4k
      ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type);
7617
      /* Clear MAY_BE_* type flags */
7618
19.4k
      ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK;
7619
7620
19.4k
      if (ZEND_TYPE_IS_COMPLEX(single_type)) {
7621
15.2k
        if (!ZEND_TYPE_IS_COMPLEX(type) && !is_composite) {
7622
          /* The first class type can be stored directly as the type ptr payload. */
7623
6.79k
          ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type));
7624
6.79k
          ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT;
7625
8.46k
        } else {
7626
8.46k
          if (type_list->num_types == 0) {
7627
            /* Switch from single name to name list. */
7628
3.41k
            type_list->num_types = 1;
7629
3.41k
            type_list->types[0] = type;
7630
            /* Clear MAY_BE_* type flags */
7631
3.41k
            ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7632
3.41k
            ZEND_TYPE_SET_LIST(type, type_list);
7633
3.41k
          }
7634
7635
8.46k
          type_list->types[type_list->num_types++] = single_type;
7636
7637
          /* Check for trivially redundant class types */
7638
8.46k
          zend_is_type_list_redundant_by_single_type(type_list, single_type);
7639
8.46k
        }
7640
15.2k
      }
7641
19.4k
    }
7642
7643
12.5k
    if (type_list->num_types) {
7644
10.4k
      zend_type_list *list = zend_arena_alloc(
7645
10.4k
        &CG(arena), ZEND_TYPE_LIST_SIZE(type_list->num_types));
7646
10.4k
      memcpy(list, type_list, ZEND_TYPE_LIST_SIZE(type_list->num_types));
7647
10.4k
      ZEND_TYPE_SET_LIST(type, list);
7648
10.4k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7649
      /* Inform that the type list is a union type */
7650
10.4k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7651
10.4k
    }
7652
7653
12.5k
    free_alloca(type_list, use_heap);
7654
7655
12.5k
    uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7656
12.5k
    if ((type_mask & MAY_BE_OBJECT) &&
7657
603
        ((!has_only_iterable_class && ZEND_TYPE_IS_COMPLEX(type)) || (type_mask & MAY_BE_STATIC))) {
7658
25
      zend_string *type_str = zend_type_to_string(type);
7659
25
      zend_error_noreturn(E_COMPILE_ERROR,
7660
25
        "Type %s contains both object and a class type, which is redundant",
7661
25
        ZSTR_VAL(type_str));
7662
25
    }
7663
250k
  } else if (ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7664
9.84k
    const zend_ast_list *list = zend_ast_get_list(ast);
7665
9.84k
    zend_type_list *type_list;
7666
7667
    /* Allocate the type list directly on the arena as it must be a type
7668
     * list of the same number of elements as the AST list has children */
7669
9.84k
    type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children));
7670
9.84k
    type_list->num_types = 0;
7671
7672
9.84k
    ZEND_ASSERT(list->children > 1);
7673
7674
31.0k
    for (uint32_t i = 0; i < list->children; i++) {
7675
21.2k
      zend_ast *type_ast = list->child[i];
7676
21.2k
      zend_type single_type = zend_compile_single_typename(type_ast);
7677
7678
      /* An intersection of union types cannot exist so invalidate it
7679
       * Currently only can happen with iterable getting canonicalized to Traversable|array */
7680
21.2k
      if (ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7681
5
        zend_string *standard_type_str = zend_type_to_string(single_type);
7682
5
        zend_error_noreturn(E_COMPILE_ERROR,
7683
5
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7684
5
      }
7685
      /* An intersection of standard types cannot exist so invalidate it */
7686
21.2k
      if (ZEND_TYPE_IS_ONLY_MASK(single_type)) {
7687
62
        zend_string *standard_type_str = zend_type_to_string(single_type);
7688
62
        zend_error_noreturn(E_COMPILE_ERROR,
7689
62
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7690
62
      }
7691
      /* Check for "self" and "parent" too */
7692
21.1k
      if (
7693
21.1k
        zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_SELF))
7694
21.1k
        || zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_PARENT))
7695
21.1k
      ) {
7696
13
        zend_error_noreturn(E_COMPILE_ERROR,
7697
13
          "Type %s cannot be part of an intersection type", ZSTR_VAL(ZEND_TYPE_NAME(single_type)));
7698
13
      }
7699
7700
      /* Add type to the type list */
7701
21.1k
      type_list->types[type_list->num_types++] = single_type;
7702
7703
      /* Check for trivially redundant class types */
7704
21.1k
      zend_is_type_list_redundant_by_single_type(type_list, single_type);
7705
21.1k
    }
7706
7707
9.76k
    ZEND_ASSERT(list->children == type_list->num_types);
7708
7709
    /* An implicitly nullable intersection type needs to be converted to a DNF type */
7710
9.76k
    if (force_allow_null) {
7711
650
      zend_type intersection_type = ZEND_TYPE_INIT_NONE(0);
7712
650
      ZEND_TYPE_SET_LIST(intersection_type, type_list);
7713
650
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_INTERSECTION_BIT;
7714
650
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_ARENA_BIT;
7715
7716
650
      zend_type_list *dnf_type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(1));
7717
650
      dnf_type_list->num_types = 1;
7718
650
      dnf_type_list->types[0] = intersection_type;
7719
650
      ZEND_TYPE_SET_LIST(type, dnf_type_list);
7720
      /* Inform that the type list is a DNF type */
7721
650
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7722
650
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7723
9.10k
    } else {
7724
9.10k
      ZEND_TYPE_SET_LIST(type, type_list);
7725
      /* Inform that the type list is an intersection type */
7726
9.10k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_INTERSECTION_BIT;
7727
9.10k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7728
9.10k
    }
7729
240k
  } else {
7730
240k
    type = zend_compile_single_typename(ast);
7731
240k
  }
7732
7733
262k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
7734
7735
262k
  if (type_mask == MAY_BE_ANY && is_marked_nullable) {
7736
13
    zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be marked as nullable since mixed already includes null");
7737
13
  }
7738
7739
262k
  if ((type_mask & MAY_BE_NULL) && is_marked_nullable) {
7740
5
    zend_error_noreturn(E_COMPILE_ERROR, "null cannot be marked as nullable");
7741
5
  }
7742
7743
262k
  if (force_allow_null && !is_marked_nullable && !(type_mask & MAY_BE_NULL)) {
7744
864
    *forced_allow_null = true;
7745
864
  }
7746
7747
262k
  if (is_marked_nullable || force_allow_null) {
7748
3.49k
    ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
7749
3.49k
    type_mask = ZEND_TYPE_PURE_MASK(type);
7750
3.49k
  }
7751
7752
262k
  if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_VOID)) {
7753
10
    zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type");
7754
10
  }
7755
7756
262k
  if ((type_mask & MAY_BE_NEVER) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_NEVER)) {
7757
6
    zend_error_noreturn(E_COMPILE_ERROR, "never can only be used as a standalone type");
7758
6
  }
7759
7760
262k
  ast->attr = orig_ast_attr;
7761
262k
  return type;
7762
262k
}
7763
/* }}} */
7764
7765
static zend_type zend_compile_typename(zend_ast *ast)
7766
42.4k
{
7767
42.4k
  bool forced_allow_null;
7768
42.4k
  return zend_compile_typename_ex(ast, false, &forced_allow_null);
7769
42.4k
}
7770
7771
/* May convert value from int to float. */
7772
static bool zend_is_valid_default_value(zend_type type, zval *value)
7773
3.55k
{
7774
3.55k
  ZEND_ASSERT(ZEND_TYPE_IS_SET(type));
7775
3.55k
  if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) {
7776
2.75k
    return 1;
7777
2.75k
  }
7778
806
  if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) {
7779
    /* Integers are allowed as initializers for floating-point values. */
7780
675
    convert_to_double(value);
7781
675
    return 1;
7782
675
  }
7783
131
  return 0;
7784
806
}
7785
7786
static void zend_compile_attributes(
7787
  HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target, uint32_t promoted
7788
887k
) /* {{{ */ {
7789
887k
  zend_attribute *attr;
7790
887k
  zend_internal_attribute *config;
7791
7792
887k
  const zend_ast_list *list = zend_ast_get_list(ast);
7793
887k
  uint32_t g, i, j;
7794
7795
887k
  ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE_LIST);
7796
7797
1.77M
  for (g = 0; g < list->children; g++) {
7798
888k
    const zend_ast_list *group = zend_ast_get_list(list->child[g]);
7799
7800
888k
    ZEND_ASSERT(group->kind == ZEND_AST_ATTRIBUTE_GROUP);
7801
7802
2.19M
    for (i = 0; i < group->children; i++) {
7803
1.30M
      ZEND_ASSERT(group->child[i]->kind == ZEND_AST_ATTRIBUTE);
7804
7805
1.30M
      const zend_ast *el = group->child[i];
7806
7807
1.30M
      if (el->child[1] &&
7808
23.5k
          el->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) {
7809
10
          zend_error_noreturn(E_COMPILE_ERROR,
7810
10
              "Cannot create Closure as attribute argument");
7811
10
      }
7812
7813
1.30M
      zend_string *name = zend_resolve_class_name_ast(el->child[0]);
7814
1.30M
      zend_string *lcname = zend_string_tolower_ex(name, false);
7815
1.30M
      zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL;
7816
7817
1.30M
      config = zend_internal_attribute_get(lcname);
7818
1.30M
      zend_string_release(lcname);
7819
7820
      /* Exclude internal attributes that do not match on promoted properties. */
7821
1.30M
      if (config && !(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7822
568
        if (promoted & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL)) {
7823
8
          zend_string_release(name);
7824
8
          continue;
7825
8
        }
7826
568
      }
7827
7828
1.30M
      uint32_t flags = (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES)
7829
1.30M
        ? ZEND_ATTRIBUTE_STRICT_TYPES : 0;
7830
1.30M
      attr = zend_add_attribute(
7831
1.30M
        attributes, name, args ? args->children : 0, flags, offset, el->lineno);
7832
1.30M
      zend_string_release(name);
7833
7834
      /* Populate arguments */
7835
1.30M
      if (args) {
7836
23.5k
        ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST);
7837
7838
23.5k
        bool uses_named_args = false;
7839
56.5k
        for (j = 0; j < args->children; j++) {
7840
32.9k
          zend_ast **arg_ast_ptr = &args->child[j];
7841
32.9k
          zend_ast *arg_ast = *arg_ast_ptr;
7842
7843
32.9k
          if (arg_ast->kind == ZEND_AST_UNPACK) {
7844
5
            zend_error_noreturn(E_COMPILE_ERROR,
7845
5
              "Cannot use unpacking in attribute argument list");
7846
5
          }
7847
7848
32.9k
          if (arg_ast->kind == ZEND_AST_NAMED_ARG) {
7849
1.98k
            attr->args[j].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0]));
7850
1.98k
            arg_ast_ptr = &arg_ast->child[1];
7851
1.98k
            uses_named_args = true;
7852
7853
10.7k
            for (uint32_t k = 0; k < j; k++) {
7854
8.81k
              if (attr->args[k].name &&
7855
7.43k
                  zend_string_equals(attr->args[k].name, attr->args[j].name)) {
7856
10
                zend_error_noreturn(E_COMPILE_ERROR, "Duplicate named parameter $%s",
7857
10
                  ZSTR_VAL(attr->args[j].name));
7858
10
              }
7859
8.81k
            }
7860
30.9k
          } else if (uses_named_args) {
7861
20
            zend_error_noreturn(E_COMPILE_ERROR,
7862
20
              "Cannot use positional argument after named argument");
7863
20
          }
7864
7865
32.9k
          zend_const_expr_to_zval(
7866
32.9k
            &attr->args[j].value, arg_ast_ptr, /* allow_dynamic */ true);
7867
32.9k
        }
7868
23.5k
      }
7869
1.30M
    }
7870
888k
  }
7871
7872
887k
  if (*attributes != NULL) {
7873
    /* Allow delaying target validation for forward compatibility. */
7874
887k
    const zend_attribute *delayed_target_validation = NULL;
7875
887k
    if (target == ZEND_ATTRIBUTE_TARGET_PARAMETER) {
7876
21.4k
      ZEND_ASSERT(offset >= 1);
7877
      /* zend_get_parameter_attribute_str will add 1 too */
7878
21.4k
      delayed_target_validation = zend_get_parameter_attribute_str(
7879
21.4k
        *attributes,
7880
21.4k
        "delayedtargetvalidation",
7881
21.4k
        strlen("delayedtargetvalidation"),
7882
21.4k
        offset - 1
7883
21.4k
      );
7884
865k
    } else {
7885
865k
      delayed_target_validation = zend_get_attribute_str(
7886
865k
        *attributes,
7887
865k
        "delayedtargetvalidation",
7888
865k
        strlen("delayedtargetvalidation")
7889
865k
      );
7890
865k
    }
7891
    /* Validate attributes in a secondary loop (needed to detect repeated attributes). */
7892
5.04M
    ZEND_HASH_PACKED_FOREACH_PTR(*attributes, attr) {
7893
5.04M
      if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) {
7894
1.62M
        continue;
7895
1.62M
      }
7896
7897
5.04M
      bool run_validator = true;
7898
4.47k
      if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7899
300
        if (delayed_target_validation == NULL) {
7900
35
          zend_string *location = zend_get_attribute_target_names(target);
7901
35
          zend_string *allowed = zend_get_attribute_target_names(config->flags);
7902
7903
35
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
7904
35
            ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
7905
35
          );
7906
35
        }
7907
265
        run_validator = false;
7908
265
      }
7909
7910
4.44k
      if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
7911
4.44k
        if (zend_is_attribute_repeated(*attributes, attr)) {
7912
20
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name));
7913
20
        }
7914
4.44k
      }
7915
7916
      /* Validators are not run if the target is already invalid */
7917
4.42k
      if (run_validator && config->validator != NULL) {
7918
2.66k
        zend_string *error = config->validator(attr, target, CG(active_class_entry));
7919
2.66k
        if (error != NULL) {
7920
236
          if (delayed_target_validation == NULL) {
7921
86
            zend_error_noreturn(E_COMPILE_ERROR, "%s", ZSTR_VAL(error));
7922
150
          } else {
7923
150
            attr->validation_error = error;
7924
150
          }
7925
236
        }
7926
2.66k
      }
7927
4.42k
    } ZEND_HASH_FOREACH_END();
7928
887k
  }
7929
887k
}
7930
/* }}} */
7931
7932
static void zend_compile_property_hooks(
7933
    zend_property_info *prop_info, zend_string *prop_name,
7934
    zend_ast *prop_type_ast, const zend_ast_list *hooks);
7935
7936
typedef struct {
7937
  const zend_string *property_name;
7938
  bool uses_property;
7939
} find_property_usage_context;
7940
7941
static void zend_property_hook_find_property_usage(zend_ast **ast_ptr, void *_context) /* {{{ */
7942
30.4k
{
7943
30.4k
  zend_ast *ast = *ast_ptr;
7944
30.4k
  find_property_usage_context *context = (find_property_usage_context *) _context;
7945
7946
30.4k
  if (ast == NULL) {
7947
231
    return;
7948
30.1k
  } else if (ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_NULLSAFE_PROP) {
7949
2.57k
    const zend_ast *object_ast = ast->child[0];
7950
2.57k
    zend_ast *property_ast = ast->child[1];
7951
7952
2.57k
    if (object_ast->kind == ZEND_AST_VAR
7953
2.31k
     && object_ast->child[0]->kind == ZEND_AST_ZVAL
7954
2.22k
     && property_ast->kind == ZEND_AST_ZVAL) {
7955
2.14k
      const zval *object = zend_ast_get_zval(object_ast->child[0]);
7956
2.14k
      const zval *property = zend_ast_get_zval(property_ast);
7957
2.14k
      if (Z_TYPE_P(object) == IS_STRING
7958
2.11k
        && Z_TYPE_P(property) == IS_STRING
7959
2.11k
        && zend_string_equals_literal(Z_STR_P(object), "this")
7960
1.83k
        && zend_string_equals(Z_STR_P(property), context->property_name)) {
7961
653
        context->uses_property = true;
7962
        /* No need to look for references in this branch. */
7963
653
        return;
7964
653
      }
7965
2.14k
    }
7966
2.57k
  }
7967
7968
  /* Don't search across function/class boundaries. */
7969
29.5k
  if (!zend_ast_is_special(ast)) {
7970
19.0k
    zend_ast_apply(ast, zend_property_hook_find_property_usage, context);
7971
19.0k
  }
7972
29.5k
}
7973
7974
static bool zend_property_hook_uses_property(const zend_string *property_name, const zend_string *hook_name, zend_ast *hook_ast)
7975
4.09k
{
7976
4.09k
  if (zend_string_equals_literal_ci(hook_name, "set")
7977
1.66k
   && hook_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
7978
472
    return true;
7979
472
  }
7980
7981
3.62k
  find_property_usage_context context = { property_name, false };
7982
3.62k
  zend_property_hook_find_property_usage(&hook_ast, &context);
7983
3.62k
  return context.uses_property;
7984
4.09k
}
7985
7986
static bool zend_property_is_virtual(const zend_class_entry *ce, const zend_string *property_name, zend_ast *hooks_ast)
7987
34.2k
{
7988
34.2k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
7989
211
    return true;
7990
211
  }
7991
34.0k
  if (!hooks_ast) {
7992
30.8k
    return false;
7993
30.8k
  }
7994
7995
34.0k
  bool is_virtual = true;
7996
7997
3.17k
  const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
7998
7.71k
  for (uint32_t i = 0; i < hooks->children; i++) {
7999
4.53k
    const zend_ast_decl *hook = (const zend_ast_decl *) hooks->child[i];
8000
4.53k
    zend_ast *body = hook->child[2];
8001
4.53k
    if (body && zend_property_hook_uses_property(property_name, hook->name, body)) {
8002
1.08k
      is_virtual = false;
8003
1.08k
    }
8004
4.53k
  }
8005
8006
3.17k
  return is_virtual;
8007
34.0k
}
8008
8009
static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */
8010
1.13M
{
8011
1.13M
  zend_ast_list *list = zend_ast_get_list(ast);
8012
1.13M
  uint32_t i;
8013
1.13M
  zend_op_array *op_array = CG(active_op_array);
8014
1.13M
  zend_arg_info *arg_infos;
8015
8016
1.13M
  if (return_type_ast || fallback_return_type) {
8017
    /* Use op_array->arg_info[-1] for return type */
8018
17.4k
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
8019
17.4k
    arg_infos->name = NULL;
8020
17.4k
    if (return_type_ast) {
8021
16.7k
      arg_infos->type = zend_compile_typename(return_type_ast);
8022
16.7k
      ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS(
8023
16.7k
        (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0, /* is_tentative */ 0);
8024
16.7k
    } else {
8025
644
      arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0);
8026
644
    }
8027
17.4k
    arg_infos++;
8028
17.4k
    op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
8029
8030
17.4k
    if (ZEND_TYPE_CONTAINS_CODE(arg_infos[-1].type, IS_VOID)
8031
3.64k
        && (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
8032
107
      zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
8033
107
      zend_error(E_DEPRECATED, "%s(): Returning by reference from a void function is deprecated", ZSTR_VAL(func_name));
8034
107
      zend_string_release(func_name);
8035
107
    }
8036
1.11M
  } else {
8037
1.11M
    if (list->children == 0) {
8038
236k
      return;
8039
236k
    }
8040
883k
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
8041
883k
  }
8042
8043
  /* Find last required parameter number for deprecation message. */
8044
900k
  uint32_t last_required_param = (uint32_t) -1;
8045
1.82M
  for (i = 0; i < list->children; ++i) {
8046
924k
    zend_ast *param_ast = list->child[i];
8047
924k
    zend_ast *default_ast_ptr = param_ast->child[2];
8048
924k
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
8049
924k
    if (!default_ast_ptr && !is_variadic) {
8050
917k
      last_required_param = i;
8051
917k
    }
8052
924k
  }
8053
8054
900k
  const uint32_t promotion_flags = ZEND_ACC_PPP_MASK | ZEND_ACC_PPP_SET_MASK | ZEND_ACC_READONLY | ZEND_ACC_FINAL;
8055
1.82M
  for (i = 0; i < list->children; ++i) {
8056
924k
    zend_ast *param_ast = list->child[i];
8057
924k
    zend_ast *type_ast = param_ast->child[0];
8058
924k
    zend_ast *var_ast = param_ast->child[1];
8059
924k
    zend_ast **default_ast_ptr = &param_ast->child[2];
8060
924k
    zend_ast *attributes_ast = param_ast->child[3];
8061
924k
    zend_ast *doc_comment_ast = param_ast->child[4];
8062
924k
    zend_ast *hooks_ast = param_ast->child[5];
8063
924k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast));
8064
924k
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
8065
924k
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
8066
924k
    uint32_t property_flags = param_ast->attr & promotion_flags;
8067
924k
    bool is_promoted = property_flags || hooks_ast;
8068
8069
924k
    CG(zend_lineno) = param_ast->lineno;
8070
8071
924k
    znode var_node, default_node;
8072
924k
    uint8_t opcode;
8073
924k
    zend_op *opline;
8074
924k
    zend_arg_info *arg_info;
8075
8076
924k
    if (zend_is_auto_global(name)) {
8077
1
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
8078
1
        ZSTR_VAL(name));
8079
1
    }
8080
8081
924k
    var_node.op_type = IS_CV;
8082
924k
    var_node.u.op.var = lookup_cv(name);
8083
8084
924k
    if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
8085
24
      zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
8086
24
        ZSTR_VAL(name));
8087
924k
    } else if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8088
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
8089
924k
    } else if (zend_string_equals_literal(name, "http_response_header")) {
8090
78
      CG(context).has_assigned_to_http_response_header = true;
8091
78
    }
8092
8093
924k
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
8094
6
      zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
8095
6
    }
8096
8097
924k
    if (is_variadic) {
8098
1.08k
      opcode = ZEND_RECV_VARIADIC;
8099
1.08k
      default_node.op_type = IS_UNUSED;
8100
1.08k
      op_array->fn_flags |= ZEND_ACC_VARIADIC;
8101
8102
1.08k
      if (*default_ast_ptr) {
8103
5
        zend_error_noreturn(E_COMPILE_ERROR,
8104
5
          "Variadic parameter cannot have a default value");
8105
5
      }
8106
923k
    } else if (*default_ast_ptr) {
8107
      /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
8108
6.03k
      uint32_t cops = CG(compiler_options);
8109
6.03k
      CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
8110
6.03k
      opcode = ZEND_RECV_INIT;
8111
6.03k
      default_node.op_type = IS_CONST;
8112
6.03k
      zend_const_expr_to_zval(
8113
6.03k
        &default_node.u.constant, default_ast_ptr, /* allow_dynamic */ true);
8114
6.03k
      CG(compiler_options) = cops;
8115
917k
    } else {
8116
917k
      opcode = ZEND_RECV;
8117
917k
      default_node.op_type = IS_UNUSED;
8118
917k
      op_array->required_num_args = i + 1;
8119
917k
    }
8120
8121
924k
    arg_info = &arg_infos[i];
8122
924k
    arg_info->name = zend_string_copy(name);
8123
924k
    arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0);
8124
924k
    arg_info->default_value = NULL;
8125
8126
924k
    if (attributes_ast) {
8127
21.4k
      zend_compile_attributes(
8128
21.4k
        &op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER,
8129
21.4k
        is_promoted ? ZEND_ATTRIBUTE_TARGET_PROPERTY : 0
8130
21.4k
      );
8131
21.4k
    }
8132
8133
924k
    bool forced_allow_nullable = false;
8134
924k
    if (type_ast) {
8135
220k
      uint32_t default_type = *default_ast_ptr ? Z_TYPE(default_node.u.constant) : IS_UNDEF;
8136
220k
      bool force_nullable = default_type == IS_NULL && !is_promoted;
8137
8138
220k
      op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
8139
220k
      arg_info->type = zend_compile_typename_ex(type_ast, force_nullable, &forced_allow_nullable);
8140
220k
      if (forced_allow_nullable) {
8141
863
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
8142
863
        zend_error(E_DEPRECATED,
8143
863
           "%s(): Implicitly marking parameter $%s as nullable is deprecated, the explicit nullable type "
8144
863
           "must be used instead", ZSTR_VAL(func_name), ZSTR_VAL(name));
8145
863
        zend_string_release(func_name);
8146
863
      }
8147
8148
220k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) {
8149
6
        zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");
8150
6
      }
8151
8152
220k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_NEVER) {
8153
5
        zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type");
8154
5
      }
8155
8156
220k
      if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable
8157
697
          && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) {
8158
35
        zend_string *type_str = zend_type_to_string(arg_info->type);
8159
35
        zend_error_noreturn(E_COMPILE_ERROR,
8160
35
          "Cannot use %s as default value for parameter $%s of type %s",
8161
35
          zend_get_type_by_const(default_type),
8162
35
          ZSTR_VAL(name), ZSTR_VAL(type_str));
8163
35
      }
8164
220k
    }
8165
924k
    if (last_required_param != (uint32_t) -1
8166
919k
     && i < last_required_param
8167
31.0k
     && default_node.op_type == IS_CONST) {
8168
      /* Ignore parameters of the form "Type $param = null".
8169
       * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
8170
1.46k
      if (!forced_allow_nullable) {
8171
663
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
8172
663
        zend_ast *required_param_ast = list->child[last_required_param];
8173
663
        zend_error(E_DEPRECATED,
8174
663
          "%s(): Optional parameter $%s declared before required parameter $%s "
8175
663
          "is implicitly treated as a required parameter",
8176
663
          ZSTR_VAL(func_name), ZSTR_VAL(name), ZSTR_VAL(zend_ast_get_str(required_param_ast->child[1])));
8177
663
        zend_string_release(func_name);
8178
663
      }
8179
8180
      /* Regardless of whether we issue a deprecation, convert this parameter into
8181
       * a required parameter without a default value. This ensures that it cannot be
8182
       * used as an optional parameter even with named parameters. */
8183
1.46k
      opcode = ZEND_RECV;
8184
1.46k
      default_node.op_type = IS_UNUSED;
8185
1.46k
      zval_ptr_dtor(&default_node.u.constant);
8186
1.46k
    }
8187
8188
924k
    opline = zend_emit_op(NULL, opcode, NULL, &default_node);
8189
924k
    SET_NODE(opline->result, &var_node);
8190
924k
    opline->op1.num = i + 1;
8191
8192
924k
    uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic, /* is_tentative */ 0)
8193
924k
      | (is_promoted ? _ZEND_IS_PROMOTED_BIT : 0);
8194
924k
    ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags;
8195
924k
    if (opcode == ZEND_RECV) {
8196
918k
      opline->op2.num = type_ast ?
8197
700k
        ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY;
8198
918k
    }
8199
8200
924k
    if (is_promoted) {
8201
941
      const zend_op_array *active_op_array = CG(active_op_array);
8202
941
      zend_class_entry *scope = active_op_array->scope;
8203
8204
941
      bool is_ctor =
8205
941
        scope && zend_is_constructor(active_op_array->function_name);
8206
941
      if (!is_ctor) {
8207
28
        zend_error_noreturn(E_COMPILE_ERROR,
8208
28
          "Cannot declare promoted property outside a constructor");
8209
28
      }
8210
913
      if ((active_op_array->fn_flags & ZEND_ACC_ABSTRACT)
8211
908
          || (scope->ce_flags & ZEND_ACC_INTERFACE)) {
8212
5
        zend_error_noreturn(E_COMPILE_ERROR,
8213
5
          "Cannot declare promoted property in an abstract constructor");
8214
5
      }
8215
908
      if (is_variadic) {
8216
5
        zend_error_noreturn(E_COMPILE_ERROR,
8217
5
          "Cannot declare variadic promoted property");
8218
5
      }
8219
903
      if (zend_hash_exists(&scope->properties_info, name)) {
8220
5
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
8221
5
          ZSTR_VAL(scope->name), ZSTR_VAL(name));
8222
5
      }
8223
898
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_CALLABLE) {
8224
5
        zend_string *str = zend_type_to_string(arg_info->type);
8225
5
        zend_error_noreturn(E_COMPILE_ERROR,
8226
5
          "Property %s::$%s cannot have type %s",
8227
5
          ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str));
8228
5
      }
8229
8230
893
      if (!(property_flags & ZEND_ACC_READONLY) && (scope->ce_flags & ZEND_ACC_READONLY_CLASS)) {
8231
22
        property_flags |= ZEND_ACC_READONLY;
8232
22
      }
8233
8234
      /* Recompile the type, as it has different memory management requirements. */
8235
893
      zend_type type = ZEND_TYPE_INIT_NONE(0);
8236
893
      if (type_ast) {
8237
693
        type = zend_compile_typename(type_ast);
8238
693
      }
8239
8240
      /* Don't give the property an explicit default value. For typed properties this means
8241
       * uninitialized, for untyped properties it means an implicit null default value.
8242
       * Properties with hooks get an implicit default value of undefined until inheritance,
8243
       * where it is changed to null only once we know it is not virtual. If we were to set it
8244
       * here, we couldn't verify that a true virtual property must not have an explicit
8245
       * default value. */
8246
893
      zval default_value;
8247
893
      if (ZEND_TYPE_IS_SET(type) || hooks_ast) {
8248
722
        ZVAL_UNDEF(&default_value);
8249
722
      } else {
8250
171
        if (property_flags & ZEND_ACC_READONLY) {
8251
10
          zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
8252
10
            ZSTR_VAL(scope->name), ZSTR_VAL(name));
8253
10
        }
8254
8255
161
        ZVAL_NULL(&default_value);
8256
161
      }
8257
8258
883
      zend_string *doc_comment =
8259
883
        doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
8260
883
      zend_property_info *prop = zend_declare_typed_property(
8261
883
        scope, name, &default_value,
8262
883
        property_flags | (zend_property_is_virtual(scope, name, hooks_ast) ? ZEND_ACC_VIRTUAL : 0) | ZEND_ACC_PROMOTED,
8263
883
        doc_comment, type);
8264
883
      if (hooks_ast) {
8265
84
        const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
8266
84
        zend_compile_property_hooks(prop, name, type_ast, hooks);
8267
84
      }
8268
883
      if (attributes_ast) {
8269
75
        zend_compile_attributes(
8270
75
          &prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, ZEND_ATTRIBUTE_TARGET_PARAMETER);
8271
8272
75
        zend_attribute *override_attribute = zend_get_attribute_str(prop->attributes, "override", sizeof("override")-1);
8273
75
        if (override_attribute) {
8274
8
          prop->flags |= ZEND_ACC_OVERRIDE;
8275
8
        }
8276
75
      }
8277
883
    }
8278
924k
  }
8279
8280
  /* These are assigned at the end to avoid uninitialized memory in case of an error */
8281
900k
  op_array->num_args = list->children;
8282
900k
  op_array->arg_info = arg_infos;
8283
8284
  /* Don't count the variadic argument */
8285
900k
  if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
8286
1.06k
    op_array->num_args--;
8287
1.06k
  }
8288
900k
  zend_set_function_arg_flags((zend_function*)op_array);
8289
8290
1.82M
  for (i = 0; i < list->children; i++) {
8291
923k
    zend_ast *param_ast = list->child[i];
8292
923k
    zend_ast *hooks_ast = param_ast->child[5];
8293
923k
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
8294
923k
    uint32_t flags = param_ast->attr & promotion_flags;
8295
923k
    bool is_promoted = flags || hooks_ast;
8296
923k
    if (!is_promoted) {
8297
923k
      continue;
8298
923k
    }
8299
8300
866
    CG(zend_lineno) = param_ast->lineno;
8301
8302
    /* Emit $this->prop = $prop for promoted properties. */
8303
866
    zend_string *name = zend_ast_get_str(param_ast->child[1]);
8304
866
    znode name_node, value_node;
8305
866
    name_node.op_type = IS_CONST;
8306
866
    ZVAL_STR_COPY(&name_node.u.constant, name);
8307
866
    value_node.op_type = IS_CV;
8308
866
    value_node.u.op.var = lookup_cv(name);
8309
8310
866
    zend_op *opline = zend_emit_op(NULL,
8311
866
      is_ref ? ZEND_ASSIGN_OBJ_REF : ZEND_ASSIGN_OBJ, NULL, &name_node);
8312
866
    opline->extended_value = zend_alloc_cache_slots(3);
8313
866
    zend_emit_op_data(&value_node);
8314
866
  }
8315
900k
}
8316
/* }}} */
8317
8318
static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */
8319
1.48k
{
8320
1.48k
  const zend_ast_list *list = zend_ast_get_list(uses_ast);
8321
1.48k
  uint32_t i;
8322
8323
1.48k
  if (!list->children) {
8324
0
    return;
8325
0
  }
8326
8327
1.48k
  if (!op_array->static_variables) {
8328
1.48k
    op_array->static_variables = zend_new_array(8);
8329
1.48k
  }
8330
8331
4.96k
  for (i = 0; i < list->children; ++i) {
8332
3.50k
    zend_ast *var_name_ast = list->child[i];
8333
3.50k
    zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast));
8334
3.50k
    uint32_t mode = var_name_ast->attr;
8335
3.50k
    zend_op *opline;
8336
3.50k
    zval *value;
8337
8338
3.50k
    if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8339
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
8340
5
    }
8341
8342
3.49k
    if (zend_is_auto_global(var_name)) {
8343
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable");
8344
5
    }
8345
8346
3.49k
    value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval));
8347
3.49k
    if (!value) {
8348
8
      zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8349
8
        "Cannot use variable $%S twice", var_name);
8350
8
    }
8351
8352
3.48k
    CG(zend_lineno) = zend_ast_get_lineno(var_name_ast);
8353
8354
3.48k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8355
3.48k
    opline->op2_type = IS_CV;
8356
3.48k
    opline->op2.var = lookup_cv(var_name);
8357
3.48k
    opline->extended_value =
8358
3.48k
      (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode;
8359
3.48k
  }
8360
1.48k
}
8361
/* }}} */
8362
8363
typedef struct {
8364
  HashTable uses;
8365
  bool varvars_used;
8366
} closure_info;
8367
8368
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast);
8369
8370
1.19M
static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
8371
1.19M
  if (!ast) {
8372
3.79k
    return;
8373
3.79k
  }
8374
8375
1.18M
  if (ast->kind == ZEND_AST_VAR) {
8376
128k
    zend_ast *name_ast = ast->child[0];
8377
128k
    if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) {
8378
126k
      zend_string *name = zend_ast_get_str(name_ast);
8379
126k
      if (zend_is_auto_global(name)) {
8380
        /* These is no need to explicitly import auto-globals. */
8381
4.13k
        return;
8382
4.13k
      }
8383
8384
121k
      if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8385
        /* $this does not need to be explicitly imported. */
8386
5.47k
        return;
8387
5.47k
      }
8388
8389
116k
      zend_hash_add_empty_element(&info->uses, name);
8390
116k
    } else {
8391
1.96k
      info->varvars_used = true;
8392
1.96k
      find_implicit_binds_recursively(info, name_ast);
8393
1.96k
    }
8394
1.06M
  } else if (zend_ast_is_list(ast)) {
8395
28.3k
    const zend_ast_list *list = zend_ast_get_list(ast);
8396
28.3k
    uint32_t i;
8397
239k
    for (i = 0; i < list->children; i++) {
8398
211k
      find_implicit_binds_recursively(info, list->child[i]);
8399
211k
    }
8400
1.03M
  } else if (ast->kind == ZEND_AST_CLOSURE) {
8401
    /* For normal closures add the use() list. */
8402
169
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8403
169
    zend_ast *uses_ast = closure_ast->child[1];
8404
169
    if (uses_ast) {
8405
110
      const zend_ast_list *uses_list = zend_ast_get_list(uses_ast);
8406
110
      uint32_t i;
8407
859
      for (i = 0; i < uses_list->children; i++) {
8408
749
        zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i]));
8409
749
      }
8410
110
    }
8411
1.03M
  } else if (ast->kind == ZEND_AST_ARROW_FUNC) {
8412
    /* For arrow functions recursively check the expression. */
8413
398k
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8414
398k
    closure_info inner_info;
8415
398k
    find_implicit_binds(&inner_info, closure_ast->child[0], closure_ast->child[2]);
8416
398k
    if (inner_info.varvars_used) {
8417
4.51k
      info->varvars_used = true;
8418
4.51k
    }
8419
398k
    if (zend_hash_num_elements(&inner_info.uses)) {
8420
366k
      zend_hash_copy(&info->uses, &inner_info.uses, NULL);
8421
366k
    }
8422
398k
    zend_hash_destroy(&inner_info.uses);
8423
634k
  } else if (!zend_ast_is_special(ast)) {
8424
479k
    uint32_t i, children = zend_ast_get_num_children(ast);
8425
1.04M
    for (i = 0; i < children; i++) {
8426
565k
      find_implicit_binds_recursively(info, ast->child[i]);
8427
565k
    }
8428
479k
  }
8429
1.18M
}
8430
8431
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast)
8432
414k
{
8433
414k
  const zend_ast_list *param_list = zend_ast_get_list(params_ast);
8434
414k
  uint32_t i;
8435
8436
414k
  zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0);
8437
414k
  info->varvars_used = false;
8438
8439
414k
  find_implicit_binds_recursively(info, stmt_ast);
8440
8441
  /* Remove variables that are parameters */
8442
461k
  for (i = 0; i < param_list->children; i++) {
8443
46.3k
    const zend_ast *param_ast = param_list->child[i];
8444
46.3k
    zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1]));
8445
46.3k
  }
8446
414k
}
8447
8448
static void compile_implicit_lexical_binds(
8449
    const closure_info *info, znode *closure, zend_op_array *op_array)
8450
16.1k
{
8451
16.1k
  zend_string *var_name;
8452
16.1k
  zend_op *opline;
8453
8454
  /* TODO We might want to use a special binding mode if varvars_used is set. */
8455
16.1k
  if (zend_hash_num_elements(&info->uses) == 0) {
8456
9.11k
    return;
8457
9.11k
  }
8458
8459
7.05k
  if (!op_array->static_variables) {
8460
7.05k
    op_array->static_variables = zend_new_array(8);
8461
7.05k
  }
8462
8463
102k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8464
102k
    zval *value = zend_hash_add(
8465
102k
      op_array->static_variables, var_name, &EG(uninitialized_zval));
8466
102k
    uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData);
8467
8468
102k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8469
102k
    opline->op2_type = IS_CV;
8470
102k
    opline->op2.var = lookup_cv(var_name);
8471
102k
    opline->extended_value = offset | ZEND_BIND_IMPLICIT;
8472
102k
  ZEND_HASH_FOREACH_END();
8473
7.05k
}
8474
8475
static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
8476
1.46k
{
8477
1.46k
  const zend_op_array *op_array = CG(active_op_array);
8478
1.46k
  const zend_ast_list *list = zend_ast_get_list(ast);
8479
1.46k
  uint32_t i;
8480
8481
4.91k
  for (i = 0; i < list->children; ++i) {
8482
3.45k
    uint32_t mode = ZEND_BIND_EXPLICIT;
8483
3.45k
    zend_ast *var_ast = list->child[i];
8484
3.45k
    zend_string *var_name = zend_ast_get_str(var_ast);
8485
3.45k
    zval zv;
8486
3.45k
    ZVAL_NULL(&zv);
8487
8488
3.45k
    {
8489
3.45k
      int i;
8490
10.2k
      for (i = 0; i < op_array->last_var; i++) {
8491
6.82k
        if (zend_string_equals(op_array->vars[i], var_name)) {
8492
5
          zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8493
5
            "Cannot use lexical variable $%S as a parameter name", var_name);
8494
5
        }
8495
6.82k
      }
8496
3.45k
    }
8497
8498
3.45k
    CG(zend_lineno) = zend_ast_get_lineno(var_ast);
8499
8500
3.45k
    if (var_ast->attr) {
8501
1.36k
      mode |= ZEND_BIND_REF;
8502
1.36k
    }
8503
8504
3.45k
    zend_compile_static_var_common(var_name, &zv, mode);
8505
3.45k
  }
8506
1.46k
}
8507
/* }}} */
8508
8509
static void zend_compile_implicit_closure_uses(const closure_info *info)
8510
16.1k
{
8511
16.1k
  zend_string *var_name;
8512
120k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8513
120k
    zval zv;
8514
120k
    ZVAL_NULL(&zv);
8515
120k
    zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT);
8516
120k
  ZEND_HASH_FOREACH_END();
8517
16.1k
}
8518
8519
748
static void add_stringable_interface(zend_class_entry *ce) {
8520
1.16k
  for (uint32_t i = 0; i < ce->num_interfaces; i++) {
8521
450
    if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) {
8522
      /* Interface already explicitly implemented */
8523
29
      return;
8524
29
    }
8525
450
  }
8526
8527
719
  ce->num_interfaces++;
8528
719
  ce->interface_names =
8529
719
    erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
8530
  // TODO: Add known interned strings instead?
8531
719
  ce->interface_names[ce->num_interfaces - 1].name =
8532
719
    ZSTR_INIT_LITERAL("Stringable", 0);
8533
719
  ce->interface_names[ce->num_interfaces - 1].lc_name =
8534
719
    ZSTR_INIT_LITERAL("stringable", 0);
8535
719
}
8536
8537
static zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, bool has_body) /* {{{ */
8538
98.0k
{
8539
98.0k
  zend_class_entry *ce = CG(active_class_entry);
8540
98.0k
  bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
8541
98.0k
  uint32_t fn_flags = op_array->fn_flags;
8542
8543
98.0k
  zend_string *lcname;
8544
8545
98.0k
  if (fn_flags & ZEND_ACC_READONLY) {
8546
0
    zend_error(E_COMPILE_ERROR, "Cannot use 'readonly' as method modifier");
8547
0
  }
8548
8549
98.0k
  if ((fn_flags & ZEND_ACC_PRIVATE) && (fn_flags & ZEND_ACC_FINAL) && !zend_is_constructor(name)) {
8550
65
    zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes");
8551
65
  }
8552
8553
98.0k
  if ((fn_flags & ZEND_ACC_ABSTRACT)
8554
429
   && !(ce->ce_flags & (ZEND_ACC_EXPLICIT_ABSTRACT_CLASS|ZEND_ACC_TRAIT))) {
8555
    // Don't say that the class should be declared abstract if it is
8556
    // anonymous or an enum and can't be abstract
8557
30
    if (ce->ce_flags & ZEND_ACC_ANON_CLASS) {
8558
5
      zend_error_noreturn(E_COMPILE_ERROR, "Anonymous class method %s() must not be abstract",
8559
5
        ZSTR_VAL(name));
8560
25
    } else if (ce->ce_flags & (ZEND_ACC_ENUM|ZEND_ACC_INTERFACE)) {
8561
15
      zend_error_noreturn(E_COMPILE_ERROR, "%s method %s::%s() must not be abstract",
8562
15
        zend_get_object_type_case(ce, true), ZSTR_VAL(ce->name), ZSTR_VAL(name));
8563
15
    } else {
8564
10
      zend_error_noreturn(E_COMPILE_ERROR, "Class %s declares abstract method %s() and must therefore be declared abstract",
8565
10
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
8566
10
    }
8567
30
  }
8568
8569
97.9k
  if (in_interface) {
8570
685
    if (!(fn_flags & ZEND_ACC_PUBLIC)) {
8571
1
      zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
8572
1
        "%s::%s() must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8573
1
    }
8574
684
    if (fn_flags & ZEND_ACC_FINAL) {
8575
5
      zend_error_noreturn(E_COMPILE_ERROR, "Interface method "
8576
5
        "%s::%s() must not be final", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8577
5
    }
8578
679
    op_array->fn_flags |= ZEND_ACC_ABSTRACT;
8579
679
  }
8580
8581
97.9k
  if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
8582
1.07k
    if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8583
5
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
8584
5
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8585
5
    }
8586
8587
1.07k
    if (has_body) {
8588
4
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
8589
4
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8590
4
    }
8591
8592
1.06k
    ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
8593
96.9k
  } else if (!has_body) {
8594
10
    zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
8595
10
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8596
10
  }
8597
8598
97.9k
  op_array->scope = ce;
8599
97.9k
  op_array->function_name = zend_string_copy(name);
8600
8601
97.9k
  lcname = zend_string_tolower(name);
8602
97.9k
  lcname = zend_new_interned_string(lcname);
8603
8604
97.9k
  if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
8605
29
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
8606
29
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8607
29
  }
8608
8609
97.9k
  zend_add_magic_method(ce, (zend_function *) op_array, lcname);
8610
97.9k
  if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)
8611
765
      && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8612
748
    add_stringable_interface(ce);
8613
748
  }
8614
8615
97.9k
  return lcname;
8616
97.9k
}
8617
/* }}} */
8618
8619
1.02M
static uint32_t zend_add_dynamic_func_def(zend_op_array *def) {
8620
1.02M
  zend_op_array *op_array = CG(active_op_array);
8621
1.02M
  uint32_t def_offset = op_array->num_dynamic_func_defs++;
8622
1.02M
  op_array->dynamic_func_defs = erealloc(
8623
1.02M
    op_array->dynamic_func_defs, op_array->num_dynamic_func_defs * sizeof(zend_op_array *));
8624
1.02M
  op_array->dynamic_func_defs[def_offset] = def;
8625
1.02M
  return def_offset;
8626
1.02M
}
8627
8628
enum func_decl_level {
8629
  FUNC_DECL_LEVEL_TOPLEVEL,
8630
  FUNC_DECL_LEVEL_NESTED,
8631
  FUNC_DECL_LEVEL_CONSTEXPR,
8632
};
8633
8634
static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array, const zend_ast_decl *decl, enum func_decl_level level) /* {{{ */
8635
1.03M
{
8636
1.03M
  zend_string *unqualified_name, *name, *lcname;
8637
1.03M
  zend_op *opline;
8638
8639
1.03M
  if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8640
1.01M
    zend_string *filename = op_array->filename;
8641
1.01M
    uint32_t start_lineno = decl->start_lineno;
8642
8643
1.01M
    zend_string *class = zend_empty_string;
8644
1.01M
    zend_string *separator = zend_empty_string;
8645
1.01M
    zend_string *function = filename;
8646
1.01M
    const char *parens = "";
8647
8648
1.01M
    if (CG(active_op_array) && CG(active_op_array)->function_name) {
8649
1.00M
      if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
8650
        /* If the parent function is a closure, don't redundantly
8651
         * add the classname and parentheses.
8652
         */
8653
1.00M
        function = CG(active_op_array)->function_name;
8654
1.00M
      } else {
8655
1.80k
        function = CG(active_op_array)->function_name;
8656
1.80k
        parens = "()";
8657
8658
1.80k
        if (CG(active_class_entry) && CG(active_class_entry)->name) {
8659
1.32k
          class = CG(active_class_entry)->name;
8660
1.32k
          separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM);
8661
1.32k
        }
8662
1.80k
      }
8663
1.00M
    }
8664
8665
1.01M
    unqualified_name = zend_strpprintf_unchecked(
8666
1.01M
      0,
8667
1.01M
      "{closure:%S%S%S%s:%" PRIu32 "}",
8668
1.01M
      class,
8669
1.01M
      separator,
8670
1.01M
      function,
8671
1.01M
      parens,
8672
1.01M
      start_lineno
8673
1.01M
    );
8674
8675
1.01M
    op_array->function_name = name = unqualified_name;
8676
1.01M
  } else {
8677
18.1k
    unqualified_name = decl->name;
8678
18.1k
    op_array->function_name = name = zend_prefix_with_ns(unqualified_name);
8679
18.1k
  }
8680
8681
1.03M
  lcname = zend_string_tolower(name);
8682
8683
1.03M
  if (FC(imports_function)) {
8684
57
    const zend_string *import_name =
8685
57
      zend_hash_find_ptr_lc(FC(imports_function), unqualified_name);
8686
57
    if (import_name && !zend_string_equals_ci(lcname, import_name)) {
8687
9
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare function %s() (previously declared as local import)",
8688
9
        ZSTR_VAL(name));
8689
9
    }
8690
57
  }
8691
8692
1.03M
  if (zend_string_equals_literal(lcname, "__autoload")) {
8693
1
    zend_error_noreturn(E_COMPILE_ERROR,
8694
1
      "__autoload() is no longer supported, use spl_autoload_register() instead");
8695
1
  }
8696
8697
1.03M
  if (zend_string_equals_literal_ci(unqualified_name, "assert")) {
8698
5
    zend_error(E_COMPILE_ERROR,
8699
5
      "Defining a custom assert() function is not allowed, "
8700
5
      "as the function has special semantics");
8701
5
  }
8702
8703
1.03M
  zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
8704
1.03M
  switch (level) {
8705
1.02M
    case FUNC_DECL_LEVEL_NESTED: {
8706
1.02M
      uint32_t func_ref = zend_add_dynamic_func_def(op_array);
8707
1.02M
      if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8708
1.01M
        opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
8709
1.01M
        opline->op2.num = func_ref;
8710
1.01M
      } else {
8711
3.75k
        opline = get_next_op();
8712
3.75k
        opline->opcode = ZEND_DECLARE_FUNCTION;
8713
3.75k
        opline->op1_type = IS_CONST;
8714
3.75k
        LITERAL_STR(opline->op1, zend_string_copy(lcname));
8715
3.75k
        opline->op2.num = func_ref;
8716
3.75k
      }
8717
1.02M
      break;
8718
0
    }
8719
97
    case FUNC_DECL_LEVEL_CONSTEXPR:
8720
14.4k
    case FUNC_DECL_LEVEL_TOPLEVEL:
8721
      /* Nothing to do. */
8722
14.4k
      break;
8723
1.03M
  }
8724
1.03M
  return lcname;
8725
1.03M
}
8726
/* }}} */
8727
8728
static zend_op_array *zend_compile_func_decl_ex(
8729
  znode *result, zend_ast *ast, enum func_decl_level level,
8730
  zend_string *property_info_name,
8731
  zend_property_hook_kind hook_kind
8732
1.13M
) {
8733
1.13M
  zend_ast_decl *decl = (zend_ast_decl *) ast;
8734
1.13M
  zend_ast *params_ast = decl->child[0];
8735
1.13M
  zend_ast *uses_ast = decl->child[1];
8736
1.13M
  zend_ast *stmt_ast = decl->child[2];
8737
1.13M
  zend_ast *return_type_ast = decl->child[3];
8738
1.13M
  bool is_method = decl->kind == ZEND_AST_METHOD;
8739
1.13M
  zend_string *lcname = NULL;
8740
1.13M
  bool is_hook = decl->kind == ZEND_AST_PROPERTY_HOOK;
8741
8742
1.13M
  zend_class_entry *orig_class_entry = CG(active_class_entry);
8743
1.13M
  zend_op_array *orig_op_array = CG(active_op_array);
8744
1.13M
  zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
8745
1.13M
  zend_oparray_context orig_oparray_context;
8746
1.13M
  closure_info info;
8747
8748
1.13M
  init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
8749
8750
1.13M
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
8751
0
    op_array->fn_flags |= ZEND_ACC_PRELOADED;
8752
0
  }
8753
8754
1.13M
  op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
8755
1.13M
  op_array->fn_flags |= decl->flags;
8756
1.13M
  op_array->line_start = decl->start_lineno;
8757
1.13M
  op_array->line_end = decl->end_lineno;
8758
1.13M
  if (decl->doc_comment) {
8759
139
    op_array->doc_comment = zend_string_copy(decl->doc_comment);
8760
139
  }
8761
8762
1.13M
  if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
8763
1.01M
    op_array->fn_flags |= ZEND_ACC_CLOSURE;
8764
1.01M
  }
8765
8766
1.13M
  if (is_hook) {
8767
3.91k
    zend_class_entry *ce = CG(active_class_entry);
8768
3.91k
    op_array->scope = ce;
8769
3.91k
    op_array->function_name = zend_string_copy(decl->name);
8770
1.13M
  } else if (is_method) {
8771
98.0k
    bool has_body = stmt_ast != NULL;
8772
98.0k
    lcname = zend_begin_method_decl(op_array, decl->name, has_body);
8773
1.03M
  } else {
8774
1.03M
    lcname = zend_begin_func_decl(result, op_array, decl, level);
8775
1.03M
    if (decl->kind == ZEND_AST_ARROW_FUNC) {
8776
16.1k
      find_implicit_binds(&info, params_ast, stmt_ast);
8777
16.1k
      compile_implicit_lexical_binds(&info, result, op_array);
8778
1.01M
    } else if (uses_ast) {
8779
1.48k
      zend_compile_closure_binding(result, op_array, uses_ast);
8780
1.48k
    }
8781
1.03M
  }
8782
8783
1.13M
  CG(active_op_array) = op_array;
8784
8785
1.13M
  zend_oparray_context_begin(&orig_oparray_context, op_array);
8786
1.13M
  CG(context).active_property_info_name = property_info_name;
8787
1.13M
  CG(context).active_property_hook_kind = hook_kind;
8788
8789
1.13M
  if (decl->child[4]) {
8790
862k
    int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
8791
8792
862k
    if (is_method || is_hook) {
8793
1.12k
      target = ZEND_ATTRIBUTE_TARGET_METHOD;
8794
1.12k
    }
8795
8796
862k
    zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target, 0);
8797
8798
862k
    const zend_attribute *override_attribute = zend_get_attribute_str(
8799
862k
      op_array->attributes,
8800
862k
      "override",
8801
862k
      sizeof("override")-1
8802
862k
    );
8803
8804
862k
    if (override_attribute) {
8805
710
      op_array->fn_flags |= ZEND_ACC_OVERRIDE;
8806
710
    }
8807
8808
862k
    const zend_attribute *deprecated_attribute = zend_get_attribute_str(
8809
862k
      op_array->attributes,
8810
862k
      "deprecated",
8811
862k
      sizeof("deprecated")-1
8812
862k
    );
8813
8814
862k
    if (deprecated_attribute) {
8815
252
      op_array->fn_flags |= ZEND_ACC_DEPRECATED;
8816
252
    }
8817
8818
    // ZEND_ACC_NODISCARD is added via an attribute validator
8819
862k
  }
8820
8821
  /* Do not leak the class scope into free standing functions, even if they are dynamically
8822
   * defined inside a class method. This is necessary for correct handling of magic constants.
8823
   * For example __CLASS__ should always be "" inside a free standing function. */
8824
1.13M
  if (decl->kind == ZEND_AST_FUNC_DECL) {
8825
18.1k
    CG(active_class_entry) = NULL;
8826
18.1k
  }
8827
8828
1.13M
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8829
14.3k
    op_array->fn_flags |= ZEND_ACC_TOP_LEVEL;
8830
14.3k
  }
8831
8832
1.13M
  {
8833
    /* Push a separator to the loop variable stack */
8834
1.13M
    zend_loop_var dummy_var;
8835
1.13M
    dummy_var.opcode = ZEND_RETURN;
8836
8837
1.13M
    zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
8838
1.13M
  }
8839
8840
1.13M
  zend_compile_params(params_ast, return_type_ast,
8841
1.13M
    is_method && zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME) ? IS_STRING : 0);
8842
1.13M
  if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
8843
83.5k
    zend_mark_function_as_generator();
8844
83.5k
    zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
8845
83.5k
  }
8846
1.13M
  if (decl->kind == ZEND_AST_ARROW_FUNC) {
8847
16.1k
    zend_compile_implicit_closure_uses(&info);
8848
16.1k
    zend_hash_destroy(&info.uses);
8849
1.12M
  } else if (uses_ast) {
8850
1.46k
    zend_compile_closure_uses(uses_ast);
8851
1.46k
  }
8852
8853
1.13M
  if (ast->kind == ZEND_AST_ARROW_FUNC && decl->child[2]->kind != ZEND_AST_RETURN) {
8854
9.32k
    bool needs_return = true;
8855
9.32k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8856
675
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8857
675
      needs_return = !ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER);
8858
675
    }
8859
9.32k
    if (needs_return) {
8860
9.10k
      stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
8861
9.10k
      decl->child[2] = stmt_ast;
8862
9.10k
    }
8863
9.32k
  }
8864
8865
1.13M
  if (op_array->fn_flags & ZEND_ACC_NODISCARD) {
8866
    /* ZEND_ACC_NODISCARD gets added by the attribute validator, but only
8867
     * if the method is not a hook; if it is a hook, then the validator
8868
     * will have returned an error message, even if the error message was
8869
     * delayed with #[\DelayedTargetValidation] that ZEND_ACC_NODISCARD
8870
     * flag should not have been added. */
8871
1.08k
    ZEND_ASSERT(!is_hook);
8872
8873
1.08k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8874
1.04k
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8875
1.04k
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_VOID)) {
8876
5
        zend_error_noreturn(E_COMPILE_ERROR,
8877
5
          "A void %s does not return a value, but #[\\NoDiscard] requires a return value",
8878
5
          CG(active_class_entry) != NULL ? "method" : "function");
8879
5
      }
8880
8881
1.03k
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
8882
5
        zend_error_noreturn(E_COMPILE_ERROR,
8883
5
          "A never returning %s does not return a value, but #[\\NoDiscard] requires a return value",
8884
5
          CG(active_class_entry) != NULL ? "method" : "function");
8885
5
      }
8886
1.03k
    }
8887
1.08k
  }
8888
8889
1.13M
  zend_compile_stmt(stmt_ast);
8890
8891
1.13M
  if (is_method) {
8892
97.6k
    CG(zend_lineno) = decl->start_lineno;
8893
97.6k
    zend_check_magic_method_implementation(
8894
97.6k
      CG(active_class_entry), (zend_function *) op_array, lcname, E_COMPILE_ERROR);
8895
1.03M
  } else if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8896
    /* Only register the function after a successful compile */
8897
13.9k
    if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
8898
106
      CG(zend_lineno) = decl->start_lineno;
8899
106
      do_bind_function_error(lcname, op_array, true);
8900
106
    }
8901
13.9k
  }
8902
8903
  /* put the implicit return on the really last line */
8904
1.13M
  CG(zend_lineno) = decl->end_lineno;
8905
8906
1.13M
  zend_do_extended_stmt(NULL);
8907
1.13M
  zend_emit_final_return(false);
8908
8909
1.13M
  pass_two(CG(active_op_array));
8910
1.13M
  zend_oparray_context_end(&orig_oparray_context);
8911
8912
  /* Pop the loop variable stack separator */
8913
1.13M
  zend_stack_del_top(&CG(loop_var_stack));
8914
8915
1.13M
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8916
13.7k
    zend_observer_function_declared_notify(op_array, lcname);
8917
13.7k
  }
8918
8919
1.13M
  if (lcname != NULL) {
8920
1.13M
    zend_string_release_ex(lcname, 0);
8921
1.13M
  }
8922
8923
1.13M
  CG(active_op_array) = orig_op_array;
8924
1.13M
  CG(active_class_entry) = orig_class_entry;
8925
8926
1.13M
  return op_array;
8927
1.13M
}
8928
8929
static zend_op_array *zend_compile_func_decl(znode *result, zend_ast *ast, enum func_decl_level level)
8930
1.13M
{
8931
1.13M
  return zend_compile_func_decl_ex(result, ast, level, /* property_info */ NULL, (zend_property_hook_kind)-1);
8932
1.13M
}
8933
8934
4.64k
zend_property_hook_kind zend_get_property_hook_kind_from_name(const zend_string *name) {
8935
4.64k
  if (zend_string_equals_literal_ci(name, "get")) {
8936
3.06k
    return ZEND_PROPERTY_HOOK_GET;
8937
3.06k
  } else if (zend_string_equals_literal_ci(name, "set")) {
8938
1.48k
    return ZEND_PROPERTY_HOOK_SET;
8939
1.48k
  } else {
8940
99
    return (zend_property_hook_kind)-1;
8941
99
  }
8942
4.64k
}
8943
8944
static void zend_compile_property_hooks(
8945
    zend_property_info *prop_info, zend_string *prop_name,
8946
    zend_ast *prop_type_ast, const zend_ast_list *hooks)
8947
3.38k
{
8948
3.38k
  zend_class_entry *ce = CG(active_class_entry);
8949
8950
3.38k
  if (prop_info->flags & ZEND_ACC_READONLY) {
8951
15
    zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly");
8952
15
  }
8953
8954
3.36k
  if (hooks->children == 0) {
8955
14
    zend_error_noreturn(E_COMPILE_ERROR, "Property hook list must not be empty");
8956
14
  }
8957
8958
7.24k
  for (uint32_t i = 0; i < hooks->children; i++) {
8959
4.10k
    zend_ast_decl *hook = (zend_ast_decl *) hooks->child[i];
8960
4.10k
    zend_string *name = hook->name;
8961
4.10k
    zend_ast *stmt_ast = hook->child[2];
8962
4.10k
    zend_ast **return_type_ast_ptr = NULL;
8963
4.10k
    zend_ast **value_type_ast_ptr = NULL;
8964
4.10k
    CG(zend_lineno) = hook->start_lineno;
8965
8966
    /* Non-private hooks are always public. This avoids having to copy the hook when inheriting
8967
     * hooks from protected properties to public ones. */
8968
4.10k
    uint32_t hook_visibility = (prop_info->flags & ZEND_ACC_PPP_MASK) != ZEND_ACC_PRIVATE ? ZEND_ACC_PUBLIC : ZEND_ACC_PRIVATE;
8969
4.10k
    hook->flags |= hook_visibility;
8970
8971
4.10k
    if (prop_info->flags & ZEND_ACC_STATIC) {
8972
14
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare hooks for static property");
8973
14
    }
8974
4.08k
    if ((hook->flags & ZEND_ACC_FINAL) && (hook->flags & ZEND_ACC_PRIVATE)) {
8975
5
      zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both final and private");
8976
5
    }
8977
4.08k
    if ((ce->ce_flags & ZEND_ACC_INTERFACE)
8978
3.87k
     || ((prop_info->flags & ZEND_ACC_ABSTRACT) && !stmt_ast)) {
8979
556
      hook->flags |= ZEND_ACC_ABSTRACT;
8980
8981
556
      if (stmt_ast) {
8982
13
        zend_error_noreturn(E_COMPILE_ERROR, "Abstract property hook cannot have body");
8983
13
      }
8984
543
      if (hook->flags & ZEND_ACC_PRIVATE) {
8985
5
        zend_error_noreturn(E_COMPILE_ERROR,
8986
5
          "Property hook cannot be both abstract and private");
8987
5
      }
8988
538
      if (hook->flags & ZEND_ACC_FINAL) {
8989
6
        zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both abstract and final");
8990
6
      }
8991
3.52k
    } else if (!stmt_ast) {
8992
16
      zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract property hook must have a body");
8993
16
    }
8994
8995
4.04k
    zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(name);
8996
4.04k
    if (hook_kind == (zend_property_hook_kind)-1) {
8997
99
      zend_error_noreturn(E_COMPILE_ERROR,
8998
99
        "Unknown hook \"%s\" for property %s::$%s, expected \"get\" or \"set\"",
8999
99
        ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9000
99
    }
9001
9002
3.94k
    if (stmt_ast && stmt_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
9003
1.50k
      stmt_ast = stmt_ast->child[0];
9004
1.50k
      if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
9005
1.23k
        stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
9006
1.23k
      } else {
9007
269
        ZEND_ASSERT(hook_kind == ZEND_PROPERTY_HOOK_SET);
9008
269
        stmt_ast = zend_ast_create(ZEND_AST_ASSIGN,
9009
269
          zend_ast_create(ZEND_AST_PROP,
9010
269
            zend_ast_create(ZEND_AST_VAR, zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_THIS))),
9011
269
            zend_ast_create_zval_from_str(zend_copy_unmangled_prop_name(prop_info->name))),
9012
269
          stmt_ast);
9013
269
      }
9014
1.50k
      stmt_ast = zend_ast_create_list(1, ZEND_AST_STMT_LIST, stmt_ast);
9015
1.50k
      hook->child[2] = stmt_ast;
9016
1.50k
    }
9017
9018
3.94k
    if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
9019
2.55k
      if (hook->child[0]) {
9020
7
        zend_error_noreturn(E_COMPILE_ERROR, "get hook of property %s::$%s must not have a parameter list",
9021
7
          ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9022
7
      }
9023
9024
2.54k
      hook->child[0] = zend_ast_create_list(0, ZEND_AST_PARAM_LIST);
9025
9026
2.54k
      return_type_ast_ptr = &hook->child[3];
9027
2.54k
      *return_type_ast_ptr = prop_type_ast;
9028
2.54k
    } else if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
9029
1.39k
      if (hook->child[0]) {
9030
170
        const zend_ast_list *param_list = zend_ast_get_list(hook->child[0]);
9031
170
        if (param_list->children != 1) {
9032
0
          zend_error_noreturn(E_COMPILE_ERROR, "%s hook of property %s::$%s must accept exactly one parameters",
9033
0
            ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9034
0
        }
9035
170
        const zend_ast *value_param_ast = param_list->child[0];
9036
170
        if (value_param_ast->attr & ZEND_PARAM_REF) {
9037
5
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be pass-by-reference",
9038
5
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9039
5
        }
9040
165
        if (value_param_ast->attr & ZEND_PARAM_VARIADIC) {
9041
5
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be variadic",
9042
5
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9043
5
        }
9044
160
        if (value_param_ast->child[2]) {
9045
5
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not have a default value",
9046
5
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9047
5
        }
9048
155
        if ((prop_type_ast != NULL) != (value_param_ast->child[0] != NULL)) {
9049
5
          zend_hooked_property_variance_error_ex(zend_ast_get_str(value_param_ast->child[1]), ce->name, prop_info->name);
9050
5
        }
9051
1.22k
      } else {
9052
1.22k
        zend_ast *param_name_ast = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VALUE));
9053
1.22k
        zend_ast *param = zend_ast_create(
9054
1.22k
          ZEND_AST_PARAM, prop_type_ast, param_name_ast,
9055
1.22k
          /* expr */ NULL, /* doc_comment */ NULL, /* attributes */ NULL,
9056
1.22k
          /* hooks */ NULL);
9057
1.22k
        value_type_ast_ptr = &param->child[0];
9058
1.22k
        hook->child[0] = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, param);
9059
1.22k
      }
9060
1.37k
      zend_ast *return_type = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VOID));
9061
1.37k
      return_type->attr = ZEND_NAME_NOT_FQ;
9062
1.37k
      hook->child[3] = return_type;
9063
1.37k
    } else {
9064
0
      ZEND_UNREACHABLE();
9065
0
    }
9066
9067
3.91k
    hook->name = zend_strpprintf(0, "$%s::%s", ZSTR_VAL(prop_name), ZSTR_VAL(name));
9068
9069
3.91k
    zend_function *func = (zend_function *) zend_compile_func_decl_ex(
9070
3.91k
      NULL, (zend_ast *) hook, FUNC_DECL_LEVEL_NESTED, prop_info->name, hook_kind);
9071
9072
3.91k
    func->common.prop_info = prop_info;
9073
9074
3.91k
    if (!prop_info->hooks) {
9075
3.12k
      prop_info->hooks = zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE);
9076
3.12k
      memset(prop_info->hooks, 0, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
9077
3.12k
    }
9078
9079
3.91k
    if (prop_info->hooks[hook_kind]) {
9080
23
      zend_error_noreturn(E_COMPILE_ERROR,
9081
23
        "Cannot redeclare property hook \"%s\"", ZSTR_VAL(name));
9082
23
    }
9083
3.89k
    prop_info->hooks[hook_kind] = func;
9084
9085
3.89k
    if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
9086
1.33k
      switch (zend_verify_property_hook_variance(prop_info, func)) {
9087
1.27k
        case INHERITANCE_SUCCESS:
9088
1.27k
          break;
9089
52
        case INHERITANCE_UNRESOLVED:
9090
52
          ce->num_hooked_prop_variance_checks++;
9091
52
          break;
9092
7
        case INHERITANCE_ERROR:
9093
7
          zend_hooked_property_variance_error(prop_info);
9094
0
        case INHERITANCE_WARNING:
9095
0
          ZEND_UNREACHABLE();
9096
1.33k
      }
9097
1.33k
    }
9098
9099
3.88k
    zend_string_release(name);
9100
    /* Un-share type ASTs to avoid double-frees of zval nodes. */
9101
3.88k
    if (return_type_ast_ptr) {
9102
2.49k
      *return_type_ast_ptr = NULL;
9103
2.49k
    }
9104
3.88k
    if (value_type_ast_ptr) {
9105
1.19k
      *value_type_ast_ptr = NULL;
9106
1.19k
    }
9107
3.88k
  }
9108
9109
3.13k
  ce->num_hooked_props++;
9110
9111
  /* See zend_link_hooked_object_iter(). */
9112
3.13k
#ifndef ZEND_OPCACHE_SHM_REATTACHMENT
9113
3.13k
  if (!ce->get_iterator) {
9114
    /* Will be removed again, in case of Iterator or IteratorAggregate. */
9115
2.38k
    ce->get_iterator = zend_hooked_object_get_iterator;
9116
2.38k
  }
9117
3.13k
#endif
9118
9119
3.13k
  if (!prop_info->ce->parent_name) {
9120
2.02k
    zend_verify_hooked_property(ce, prop_info, prop_name);
9121
2.02k
  }
9122
3.13k
}
9123
9124
static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */
9125
32.8k
{
9126
32.8k
  const zend_ast_list *list = zend_ast_get_list(ast);
9127
32.8k
  zend_class_entry *ce = CG(active_class_entry);
9128
32.8k
  uint32_t i, children = list->children;
9129
9130
32.8k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
9131
12
    zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", ZSTR_VAL(ce->name));
9132
12
  }
9133
9134
32.8k
  if ((flags & ZEND_ACC_FINAL) && (flags & ZEND_ACC_PRIVATE)) {
9135
5
    zend_error_noreturn(E_COMPILE_ERROR, "Property cannot be both final and private");
9136
5
  }
9137
9138
32.8k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9139
226
    if (flags & ZEND_ACC_FINAL) {
9140
5
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be final");
9141
5
    }
9142
221
    if (flags & (ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE)) {
9143
5
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be protected or private");
9144
5
    }
9145
216
    if (flags & ZEND_ACC_ABSTRACT) {
9146
5
      zend_error_noreturn(E_COMPILE_ERROR,
9147
5
        "Property in interface cannot be explicitly abstract. "
9148
5
        "All interface members are implicitly abstract");
9149
5
    }
9150
211
    flags |= ZEND_ACC_ABSTRACT;
9151
211
  }
9152
9153
66.0k
  for (i = 0; i < children; ++i) {
9154
33.3k
    zend_property_info *info;
9155
33.3k
    zend_ast *prop_ast = list->child[i];
9156
33.3k
    zend_ast *name_ast = prop_ast->child[0];
9157
33.3k
    zend_ast **value_ast_ptr = &prop_ast->child[1];
9158
33.3k
    zend_ast *doc_comment_ast = prop_ast->child[2];
9159
33.3k
    zend_ast *hooks_ast = prop_ast->child[3];
9160
33.3k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
9161
33.3k
    zend_string *doc_comment = NULL;
9162
33.3k
    zval value_zv;
9163
33.3k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
9164
33.3k
    flags |= zend_property_is_virtual(ce, name, hooks_ast) ? ZEND_ACC_VIRTUAL : 0;
9165
9166
33.3k
    zend_string *old_active_property_info_name = CG(context).active_property_info_name;
9167
33.3k
    CG(context).active_property_info_name = name;
9168
9169
33.3k
    if (!hooks_ast) {
9170
30.0k
      if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9171
0
        zend_error_noreturn(E_COMPILE_ERROR,
9172
0
          "Interfaces may only include hooked properties");
9173
0
      }
9174
30.0k
      if (flags & ZEND_ACC_ABSTRACT) {
9175
5
        zend_error_noreturn(E_COMPILE_ERROR,
9176
5
          "Only hooked properties may be declared abstract");
9177
5
      }
9178
30.0k
    }
9179
33.3k
    if ((flags & ZEND_ACC_ABSTRACT)) {
9180
511
      ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
9181
511
    }
9182
9183
33.3k
    if (type_ast) {
9184
14.1k
      type = zend_compile_typename(type_ast);
9185
9186
14.1k
      if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_NEVER|MAY_BE_CALLABLE)) {
9187
5
        zend_string *str = zend_type_to_string(type);
9188
5
        zend_error_noreturn(E_COMPILE_ERROR,
9189
5
          "Property %s::$%s cannot have type %s",
9190
5
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9191
5
      }
9192
14.1k
    }
9193
9194
    /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */
9195
33.3k
    if (doc_comment_ast) {
9196
419
      doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9197
419
    }
9198
9199
33.3k
    if (zend_hash_exists(&ce->properties_info, name)) {
9200
41
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
9201
41
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
9202
41
    }
9203
9204
33.2k
    if (*value_ast_ptr) {
9205
15.3k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9206
9207
15.3k
      if (ZEND_TYPE_IS_SET(type) && !Z_CONSTANT(value_zv)
9208
2.03k
          && !zend_is_valid_default_value(type, &value_zv)) {
9209
73
        zend_string *str = zend_type_to_string(type);
9210
73
        if (Z_TYPE(value_zv) == IS_NULL && !ZEND_TYPE_IS_INTERSECTION(type)) {
9211
20
          ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
9212
20
          zend_string *nullable_str = zend_type_to_string(type);
9213
9214
20
          zend_error_noreturn(E_COMPILE_ERROR,
9215
20
            "Default value for property of type %s may not be null. "
9216
20
            "Use the nullable type %s to allow null default value",
9217
20
            ZSTR_VAL(str), ZSTR_VAL(nullable_str));
9218
53
        } else {
9219
53
          zend_error_noreturn(E_COMPILE_ERROR,
9220
53
            "Cannot use %s as default value for property %s::$%s of type %s",
9221
53
            zend_zval_value_name(&value_zv),
9222
53
            ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9223
53
        }
9224
73
      }
9225
17.8k
    } else if (!ZEND_TYPE_IS_SET(type) && !hooks_ast) {
9226
4.36k
      ZVAL_NULL(&value_zv);
9227
13.5k
    } else {
9228
13.5k
      ZVAL_UNDEF(&value_zv);
9229
13.5k
    }
9230
9231
33.2k
    if ((ce->ce_flags & ZEND_ACC_READONLY_CLASS)) {
9232
34
      flags |= ZEND_ACC_READONLY;
9233
34
    }
9234
9235
33.2k
    if (flags & ZEND_ACC_READONLY) {
9236
446
      if (!ZEND_TYPE_IS_SET(type)) {
9237
14
        zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
9238
14
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9239
14
      }
9240
432
      if (!Z_ISUNDEF(value_zv)) {
9241
7
        zend_error_noreturn(E_COMPILE_ERROR,
9242
7
          "Readonly property %s::$%s cannot have default value",
9243
7
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9244
7
      }
9245
425
      if (flags & ZEND_ACC_STATIC) {
9246
9
        zend_error_noreturn(E_COMPILE_ERROR,
9247
9
          "Static property %s::$%s cannot be readonly",
9248
9
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9249
9
      }
9250
425
    }
9251
9252
33.1k
    info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type);
9253
9254
33.1k
    if (hooks_ast) {
9255
3.29k
      zend_compile_property_hooks(info, name, type_ast, zend_ast_get_list(hooks_ast));
9256
3.29k
    }
9257
9258
33.1k
    if (attr_ast) {
9259
954
      zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0);
9260
9261
954
      const zend_attribute *override_attribute = zend_get_attribute_str(info->attributes, "override", sizeof("override")-1);
9262
954
      if (override_attribute) {
9263
95
        info->flags |= ZEND_ACC_OVERRIDE;
9264
95
      }
9265
954
    }
9266
9267
33.1k
    CG(context).active_property_info_name = old_active_property_info_name;
9268
33.1k
  }
9269
32.8k
}
9270
/* }}} */
9271
9272
static void zend_compile_prop_group(const zend_ast *ast) /* {{{ */
9273
32.8k
{
9274
32.8k
  zend_ast *type_ast = ast->child[0];
9275
32.8k
  zend_ast *prop_ast = ast->child[1];
9276
32.8k
  zend_ast *attr_ast = ast->child[2];
9277
9278
32.8k
  zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast);
9279
32.8k
}
9280
/* }}} */
9281
9282
static void zend_check_trait_alias_modifiers(uint32_t attr) /* {{{ */
9283
112k
{
9284
112k
  if (attr & ZEND_ACC_STATIC) {
9285
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"static\" as method modifier in trait alias");
9286
112k
  } else if (attr & ZEND_ACC_ABSTRACT) {
9287
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"abstract\" as method modifier in trait alias");
9288
5
  }
9289
112k
}
9290
/* }}} */
9291
9292
static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast, zend_ast *type_ast)
9293
7.43k
{
9294
7.43k
  const zend_ast_list *list = zend_ast_get_list(ast);
9295
7.43k
  zend_class_entry *ce = CG(active_class_entry);
9296
7.43k
  uint32_t i, children = list->children;
9297
9298
14.8k
  for (i = 0; i < children; ++i) {
9299
7.47k
    zend_class_constant *c;
9300
7.47k
    zend_ast *const_ast = list->child[i];
9301
7.47k
    zend_ast *name_ast = const_ast->child[0];
9302
7.47k
    zend_ast **value_ast_ptr = &const_ast->child[1];
9303
7.47k
    zend_ast *doc_comment_ast = const_ast->child[2];
9304
7.47k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
9305
7.47k
    zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
9306
7.47k
    zval value_zv;
9307
7.47k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
9308
9309
7.47k
    if (type_ast) {
9310
2.02k
      type = zend_compile_typename(type_ast);
9311
9312
2.02k
      uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9313
9314
2.02k
      if (type_mask != MAY_BE_ANY && (type_mask & (MAY_BE_CALLABLE|MAY_BE_VOID|MAY_BE_NEVER))) {
9315
5
        zend_string *type_str = zend_type_to_string(type);
9316
9317
5
        zend_error_noreturn(E_COMPILE_ERROR, "Class constant %s::%s cannot have type %s",
9318
5
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9319
5
      }
9320
2.02k
    }
9321
9322
7.47k
    if (UNEXPECTED((flags & ZEND_ACC_PRIVATE) && (flags & ZEND_ACC_FINAL))) {
9323
5
      zend_error_noreturn(
9324
5
        E_COMPILE_ERROR, "Private constant %s::%s cannot be final as it is not visible to other classes",
9325
5
        ZSTR_VAL(ce->name), ZSTR_VAL(name)
9326
5
      );
9327
5
    }
9328
9329
7.46k
    zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9330
9331
7.46k
    if (!Z_CONSTANT(value_zv) && ZEND_TYPE_IS_SET(type) && !zend_is_valid_default_value(type, &value_zv)) {
9332
23
      zend_string *type_str = zend_type_to_string(type);
9333
9334
23
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as value for class constant %s::%s of type %s",
9335
23
        zend_zval_type_name(&value_zv), ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9336
23
    }
9337
9338
7.44k
    c = zend_declare_typed_class_constant(ce, name, &value_zv, flags, doc_comment, type);
9339
9340
7.44k
    if (attr_ast) {
9341
311
      zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9342
9343
311
      const zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9344
9345
311
      if (deprecated) {
9346
178
        ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9347
        /* For deprecated constants, we need to flag the zval for recursion
9348
         * detection. Make sure the zval is separated out of shm. */
9349
178
        ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
9350
178
        ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
9351
178
      }
9352
311
    }
9353
7.44k
  }
9354
7.43k
}
9355
9356
static void zend_compile_class_const_group(const zend_ast *ast) /* {{{ */
9357
7.43k
{
9358
7.43k
  zend_ast *const_ast = ast->child[0];
9359
7.43k
  zend_ast *attr_ast = ast->child[1];
9360
7.43k
  zend_ast *type_ast = ast->child[2];
9361
9362
7.43k
  zend_compile_class_const_decl(const_ast, ast->attr, attr_ast, type_ast);
9363
7.43k
}
9364
/* }}} */
9365
9366
static void zend_compile_method_ref(const zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */
9367
112k
{
9368
112k
  zend_ast *class_ast = ast->child[0];
9369
112k
  zend_ast *method_ast = ast->child[1];
9370
9371
112k
  method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
9372
9373
112k
  if (class_ast) {
9374
1.14k
    method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name");
9375
111k
  } else {
9376
111k
    method_ref->class_name = NULL;
9377
111k
  }
9378
112k
}
9379
/* }}} */
9380
9381
static void zend_compile_trait_precedence(const zend_ast *ast) /* {{{ */
9382
567
{
9383
567
  const zend_ast *method_ref_ast = ast->child[0];
9384
567
  zend_ast *insteadof_ast = ast->child[1];
9385
567
  const zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast);
9386
567
  uint32_t i;
9387
9388
567
  zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*));
9389
567
  zend_compile_method_ref(method_ref_ast, &precedence->trait_method);
9390
567
  precedence->num_excludes = insteadof_list->children;
9391
9392
1.63k
  for (i = 0; i < insteadof_list->children; ++i) {
9393
1.06k
    zend_ast *name_ast = insteadof_list->child[i];
9394
1.06k
    precedence->exclude_class_names[i] =
9395
1.06k
      zend_resolve_const_class_name_reference(name_ast, "trait name");
9396
1.06k
  }
9397
9398
567
  zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
9399
567
}
9400
/* }}} */
9401
9402
static void zend_compile_trait_alias(const zend_ast *ast) /* {{{ */
9403
112k
{
9404
112k
  const zend_ast *method_ref_ast = ast->child[0];
9405
112k
  zend_ast *alias_ast = ast->child[1];
9406
112k
  uint32_t modifiers = ast->attr;
9407
9408
112k
  zend_trait_alias *alias;
9409
9410
112k
  zend_check_trait_alias_modifiers(modifiers);
9411
9412
112k
  alias = emalloc(sizeof(zend_trait_alias));
9413
112k
  zend_compile_method_ref(method_ref_ast, &alias->trait_method);
9414
112k
  alias->modifiers = modifiers;
9415
9416
112k
  if (alias_ast) {
9417
111k
    alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
9418
111k
  } else {
9419
450
    alias->alias = NULL;
9420
450
  }
9421
9422
112k
  zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
9423
112k
}
9424
/* }}} */
9425
9426
static void zend_compile_use_trait(const zend_ast *ast) /* {{{ */
9427
39.0k
{
9428
39.0k
  const zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
9429
39.0k
  zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
9430
39.0k
  zend_class_entry *ce = CG(active_class_entry);
9431
39.0k
  uint32_t i;
9432
9433
39.0k
  ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children));
9434
9435
79.6k
  for (i = 0; i < traits->children; ++i) {
9436
40.6k
    zend_ast *trait_ast = traits->child[i];
9437
9438
40.6k
    if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9439
5
      zend_string *name = zend_ast_get_str(trait_ast);
9440
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
9441
5
        "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
9442
5
    }
9443
9444
40.6k
    ce->trait_names[ce->num_traits].name =
9445
40.6k
      zend_resolve_const_class_name_reference(trait_ast, "trait name");
9446
40.6k
    ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name);
9447
40.6k
    ce->num_traits++;
9448
40.6k
  }
9449
9450
39.0k
  if (!adaptations) {
9451
1.73k
    return;
9452
1.73k
  }
9453
9454
149k
  for (i = 0; i < adaptations->children; ++i) {
9455
112k
    const zend_ast *adaptation_ast = adaptations->child[i];
9456
112k
    switch (adaptation_ast->kind) {
9457
567
      case ZEND_AST_TRAIT_PRECEDENCE:
9458
567
        zend_compile_trait_precedence(adaptation_ast);
9459
567
        break;
9460
112k
      case ZEND_AST_TRAIT_ALIAS:
9461
112k
        zend_compile_trait_alias(adaptation_ast);
9462
112k
        break;
9463
112k
      EMPTY_SWITCH_DEFAULT_CASE()
9464
112k
    }
9465
112k
  }
9466
37.3k
}
9467
/* }}} */
9468
9469
static void zend_compile_implements(zend_ast *ast) /* {{{ */
9470
2.92k
{
9471
2.92k
  const zend_ast_list *list = zend_ast_get_list(ast);
9472
2.92k
  zend_class_entry *ce = CG(active_class_entry);
9473
2.92k
  zend_class_name *interface_names;
9474
2.92k
  uint32_t i;
9475
9476
2.92k
  interface_names = emalloc(sizeof(zend_class_name) * list->children);
9477
9478
6.65k
  for (i = 0; i < list->children; ++i) {
9479
3.73k
    zend_ast *class_ast = list->child[i];
9480
3.73k
    interface_names[i].name =
9481
3.73k
      zend_resolve_const_class_name_reference(class_ast, "interface name");
9482
3.73k
    interface_names[i].lc_name = zend_string_tolower(interface_names[i].name);
9483
3.73k
  }
9484
9485
2.92k
  ce->num_interfaces = list->children;
9486
2.92k
  ce->interface_names = interface_names;
9487
2.92k
}
9488
/* }}} */
9489
9490
static zend_string *zend_generate_anon_class_name(const zend_ast_decl *decl)
9491
1.70k
{
9492
1.70k
  zend_string *filename = CG(active_op_array)->filename;
9493
1.70k
  uint32_t start_lineno = decl->start_lineno;
9494
9495
  /* Use parent or first interface as prefix. */
9496
1.70k
  zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS);
9497
1.70k
  if (decl->child[0]) {
9498
79
    prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name");
9499
1.63k
  } else if (decl->child[1]) {
9500
518
    const zend_ast_list *list = zend_ast_get_list(decl->child[1]);
9501
518
    prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name");
9502
518
  }
9503
9504
1.70k
  zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32,
9505
1.70k
    ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
9506
1.70k
  zend_string_release(prefix);
9507
1.70k
  return zend_new_interned_string(result);
9508
1.70k
}
9509
9510
static void zend_compile_enum_backing_type(zend_class_entry *ce, zend_ast *enum_backing_type_ast)
9511
698
{
9512
698
  ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM);
9513
698
  zend_type type = zend_compile_typename(enum_backing_type_ast);
9514
698
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9515
698
  if (ZEND_TYPE_IS_COMPLEX(type) || (type_mask != MAY_BE_LONG && type_mask != MAY_BE_STRING)) {
9516
53
    zend_string *type_string = zend_type_to_string(type);
9517
53
    zend_error_noreturn(E_COMPILE_ERROR,
9518
53
      "Enum backing type must be int or string, %s given",
9519
53
      ZSTR_VAL(type_string));
9520
53
  }
9521
645
  if (type_mask == MAY_BE_LONG) {
9522
380
    ce->enum_backing_type = IS_LONG;
9523
380
  } else {
9524
265
    ZEND_ASSERT(type_mask == MAY_BE_STRING);
9525
265
    ce->enum_backing_type = IS_STRING;
9526
263
  }
9527
645
  zend_type_release(type, 0);
9528
643
}
9529
9530
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel) /* {{{ */
9531
191k
{
9532
191k
  const zend_ast_decl *decl = (const zend_ast_decl *) ast;
9533
191k
  zend_ast *extends_ast = decl->child[0];
9534
191k
  zend_ast *implements_ast = decl->child[1];
9535
191k
  zend_ast *stmt_ast = decl->child[2];
9536
191k
  zend_ast *enum_backing_type_ast = decl->child[4];
9537
191k
  zend_string *name, *lcname;
9538
191k
  zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
9539
191k
  zend_op *opline;
9540
9541
191k
  zend_class_entry *original_ce = CG(active_class_entry);
9542
9543
191k
  if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
9544
189k
    zend_string *unqualified_name = decl->name;
9545
9546
189k
    if (CG(active_class_entry)) {
9547
12
      zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
9548
12
    }
9549
9550
189k
    const char *type = "a class name";
9551
189k
    if (decl->flags & ZEND_ACC_ENUM) {
9552
4.56k
      type = "an enum name";
9553
185k
    } else if (decl->flags & ZEND_ACC_INTERFACE) {
9554
5.72k
      type = "an interface name";
9555
179k
    } else if (decl->flags & ZEND_ACC_TRAIT) {
9556
2.24k
      type = "a trait name";
9557
2.24k
    }
9558
189k
    zend_assert_valid_class_name(unqualified_name, type);
9559
189k
    name = zend_prefix_with_ns(unqualified_name);
9560
189k
    name = zend_new_interned_string(name);
9561
189k
    lcname = zend_string_tolower(name);
9562
9563
189k
    if (FC(imports)) {
9564
604
      zend_string *import_name =
9565
604
        zend_hash_find_ptr_lc(FC(imports), unqualified_name);
9566
604
      if (import_name && !zend_string_equals_ci(lcname, import_name)) {
9567
11
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s "
9568
11
            "(previously declared as local import)", ZSTR_VAL(name));
9569
11
      }
9570
604
    }
9571
9572
189k
    zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS);
9573
189k
  } else {
9574
    /* Find an anon class name that is not in use yet. */
9575
1.71k
    name = NULL;
9576
1.71k
    lcname = NULL;
9577
1.71k
    do {
9578
1.71k
      zend_tmp_string_release(name);
9579
1.71k
      zend_tmp_string_release(lcname);
9580
1.71k
      name = zend_generate_anon_class_name(decl);
9581
1.71k
      lcname = zend_string_tolower(name);
9582
1.71k
    } while (zend_hash_exists(CG(class_table), lcname));
9583
1.71k
  }
9584
191k
  lcname = zend_new_interned_string(lcname);
9585
9586
191k
  ce->type = ZEND_USER_CLASS;
9587
191k
  ce->name = name;
9588
191k
  zend_initialize_class_data(ce, true);
9589
191k
  if (!(decl->flags & ZEND_ACC_ANON_CLASS)) {
9590
189k
    zend_alloc_ce_cache(ce->name);
9591
189k
  }
9592
9593
191k
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
9594
0
    ce->ce_flags |= ZEND_ACC_PRELOADED;
9595
0
    ZEND_MAP_PTR_NEW(ce->static_members_table);
9596
0
    ZEND_MAP_PTR_NEW(ce->mutable_data);
9597
0
  }
9598
9599
191k
  ce->ce_flags |= decl->flags;
9600
191k
  ce->info.user.filename = zend_string_copy(zend_get_compiled_filename());
9601
191k
  ce->info.user.line_start = decl->start_lineno;
9602
191k
  ce->info.user.line_end = decl->end_lineno;
9603
9604
191k
  if (decl->doc_comment) {
9605
39
    ce->doc_comment = zend_string_copy(decl->doc_comment);
9606
39
  }
9607
9608
191k
  if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
9609
    /* Serialization is not supported for anonymous classes */
9610
1.70k
    ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
9611
1.70k
  }
9612
9613
191k
  if (extends_ast) {
9614
61.7k
    ce->parent_name =
9615
61.7k
      zend_resolve_const_class_name_reference(extends_ast, "class name");
9616
61.7k
  }
9617
9618
191k
  CG(active_class_entry) = ce;
9619
9620
191k
  if (decl->child[3]) {
9621
1.86k
    zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS, 0);
9622
1.86k
  }
9623
9624
191k
  if (implements_ast) {
9625
2.92k
    zend_compile_implements(implements_ast);
9626
2.92k
  }
9627
9628
191k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
9629
4.54k
    if (enum_backing_type_ast != NULL) {
9630
698
      zend_compile_enum_backing_type(ce, enum_backing_type_ast);
9631
698
    }
9632
4.54k
    zend_enum_add_interfaces(ce);
9633
4.54k
    zend_enum_register_props(ce);
9634
4.54k
  }
9635
9636
191k
  zend_compile_stmt(stmt_ast);
9637
9638
  /* Reset lineno for final opcodes and errors */
9639
191k
  CG(zend_lineno) = ast->lineno;
9640
9641
191k
  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) {
9642
35
    zend_verify_abstract_class(ce);
9643
35
  }
9644
9645
191k
  CG(active_class_entry) = original_ce;
9646
9647
191k
  if (toplevel) {
9648
35.5k
    ce->ce_flags |= ZEND_ACC_TOP_LEVEL;
9649
35.5k
  }
9650
9651
  /* We currently don't early-bind classes that implement interfaces or use traits */
9652
191k
  if (!ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9653
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
9654
   /* See zend_link_hooked_object_iter(). */
9655
   && !ce->num_hooked_props
9656
#endif
9657
143k
   && !(CG(compiler_options) & ZEND_COMPILE_WITHOUT_EXECUTION)) {
9658
143k
    if (toplevel) {
9659
29.3k
      if (extends_ast) {
9660
7.78k
        zend_class_entry *parent_ce = zend_lookup_class_ex(
9661
7.78k
          ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
9662
9663
7.78k
        if (parent_ce
9664
7.11k
         && !zend_compile_ignore_class(parent_ce, ce->info.user.filename)) {
9665
7.11k
          if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) {
9666
4.29k
            zend_string_release(lcname);
9667
4.29k
            return;
9668
4.29k
          }
9669
7.11k
        }
9670
21.5k
      } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) {
9671
18.6k
        zend_string_release(lcname);
9672
18.6k
        zend_build_properties_info_table(ce);
9673
18.6k
        zend_inheritance_check_override(ce);
9674
18.6k
        ce->ce_flags |= ZEND_ACC_LINKED;
9675
18.6k
        zend_observer_class_linked_notify(ce, lcname);
9676
18.6k
        return;
9677
18.6k
      } else {
9678
2.91k
        goto link_unbound;
9679
2.91k
      }
9680
114k
    } else if (!extends_ast) {
9681
63.6k
link_unbound:
9682
      /* Link unbound simple class */
9683
63.6k
      zend_build_properties_info_table(ce);
9684
63.6k
      zend_inheritance_check_override(ce);
9685
63.6k
      ce->ce_flags |= ZEND_ACC_LINKED;
9686
63.6k
    }
9687
143k
  }
9688
9689
168k
  opline = get_next_op();
9690
9691
168k
  if (ce->parent_name) {
9692
    /* Lowercased parent name */
9693
56.5k
    zend_string *lc_parent_name = zend_string_tolower(ce->parent_name);
9694
56.5k
    opline->op2_type = IS_CONST;
9695
56.5k
    LITERAL_STR(opline->op2, lc_parent_name);
9696
56.5k
  }
9697
9698
168k
  opline->op1_type = IS_CONST;
9699
  /* It's possible that `lcname` is not an interned string because it was not yet in the interned string table.
9700
   * However, by this point another thread may have caused `lcname` to be added in the interned string table.
9701
   * This will cause `lcname` to get freed once it is found in the interned string table. If we were to use
9702
   * LITERAL_STR() here we would not change the `lcname` pointer to the new value, and it would point to the
9703
   * now-freed string. This will cause issues when we use `lcname` in the code below. We solve this by using
9704
   * zend_add_literal_string() which gives us the new value. */
9705
168k
  opline->op1.constant = zend_add_literal_string(&lcname);
9706
9707
168k
  if (decl->flags & ZEND_ACC_ANON_CLASS) {
9708
1.68k
    opline->opcode = ZEND_DECLARE_ANON_CLASS;
9709
1.68k
    opline->extended_value = zend_alloc_cache_slot();
9710
1.68k
    zend_make_var_result(result, opline);
9711
1.68k
    if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) {
9712
      /* We checked above that the class name is not used. This really shouldn't happen. */
9713
0
      zend_error_noreturn(E_ERROR,
9714
0
        "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name));
9715
0
    }
9716
166k
  } else {
9717
    /* Generate RTD keys until we find one that isn't in use yet. */
9718
166k
    zend_string *key = NULL;
9719
166k
    do {
9720
166k
      zend_tmp_string_release(key);
9721
166k
      key = zend_build_runtime_definition_key(lcname, decl->start_lineno);
9722
166k
    } while (!zend_hash_add_ptr(CG(class_table), key, ce));
9723
9724
    /* RTD key is placed after lcname literal in op1 */
9725
166k
    zend_add_literal_string(&key);
9726
9727
166k
    opline->opcode = ZEND_DECLARE_CLASS;
9728
166k
    if (toplevel
9729
11.7k
       && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING)
9730
        /* We currently don't early-bind classes that implement interfaces or use traits */
9731
5.31k
       && !ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9732
166k
    ) {
9733
1.32k
      if (!extends_ast) {
9734
        /* Use empty string for classes without parents to avoid new handler, and special
9735
         * handling of zend_early_binding. */
9736
852
        opline->op2_type = IS_CONST;
9737
852
        LITERAL_STR(opline->op2, ZSTR_EMPTY_ALLOC());
9738
852
      }
9739
1.32k
      CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING;
9740
1.32k
      opline->opcode = ZEND_DECLARE_CLASS_DELAYED;
9741
1.32k
      opline->extended_value = zend_alloc_cache_slot();
9742
1.32k
      opline->result_type = IS_UNUSED;
9743
1.32k
      opline->result.opline_num = -1;
9744
1.32k
    }
9745
166k
  }
9746
168k
}
9747
/* }}} */
9748
9749
static void zend_compile_enum_case(zend_ast *ast)
9750
6.70k
{
9751
6.70k
  zend_class_entry *enum_class = CG(active_class_entry);
9752
6.70k
  if (!(enum_class->ce_flags & ZEND_ACC_ENUM)) {
9753
5
    zend_error_noreturn(E_COMPILE_ERROR, "Case can only be used in enums");
9754
5
  }
9755
9756
6.69k
  zend_string *enum_case_name = zval_make_interned_string(zend_ast_get_zval(ast->child[0]));
9757
6.69k
  zend_string *enum_class_name = enum_class->name;
9758
9759
6.69k
  zval class_name_zval;
9760
6.69k
  ZVAL_STR_COPY(&class_name_zval, enum_class_name);
9761
6.69k
  zend_ast *class_name_ast = zend_ast_create_zval(&class_name_zval);
9762
9763
6.69k
  zval case_id_zval;
9764
6.69k
  int case_id = zend_enum_next_case_id(enum_class);
9765
6.69k
  ZVAL_LONG(&case_id_zval, case_id);
9766
6.69k
  zend_ast *case_id_ast = zend_ast_create_zval(&case_id_zval);
9767
9768
6.69k
  zval case_name_zval;
9769
6.69k
  ZVAL_STR_COPY(&case_name_zval, enum_case_name);
9770
6.69k
  zend_ast *case_name_ast = zend_ast_create_zval(&case_name_zval);
9771
9772
6.69k
  zend_ast *case_value_ast = ast->child[1];
9773
  // Remove case_value_ast from the original AST to avoid freeing it, as it will be freed by zend_const_expr_to_zval
9774
6.69k
  ast->child[1] = NULL;
9775
6.69k
  if (enum_class->enum_backing_type != IS_UNDEF && case_value_ast == NULL) {
9776
5
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of backed enum %s must have a value",
9777
5
      ZSTR_VAL(enum_case_name),
9778
5
      ZSTR_VAL(enum_class_name));
9779
6.69k
  } else if (enum_class->enum_backing_type == IS_UNDEF && case_value_ast != NULL) {
9780
8
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-backed enum %s must not have a value",
9781
8
      ZSTR_VAL(enum_case_name),
9782
8
      ZSTR_VAL(enum_class_name));
9783
8
  }
9784
9785
6.68k
  zend_ast *const_enum_init_ast = zend_ast_create(ZEND_AST_CONST_ENUM_INIT,
9786
6.68k
      class_name_ast, case_id_ast, case_name_ast, case_value_ast);
9787
9788
6.68k
  zval value_zv;
9789
6.68k
  zend_const_expr_to_zval(&value_zv, &const_enum_init_ast, /* allow_dynamic */ false);
9790
9791
  /* Doc comment has been appended as second last element in ZEND_AST_ENUM ast - attributes are conventionally last */
9792
6.68k
  zend_ast *doc_comment_ast = ast->child[2];
9793
6.68k
  zend_string *doc_comment = NULL;
9794
6.68k
  if (doc_comment_ast) {
9795
4.11k
    doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9796
4.11k
  }
9797
9798
6.68k
  zend_class_constant *c = zend_declare_class_constant_ex(enum_class, enum_case_name, &value_zv, ZEND_ACC_PUBLIC, doc_comment);
9799
6.68k
  ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE;
9800
6.68k
  zend_ast_destroy(const_enum_init_ast);
9801
9802
6.68k
  zend_ast *attr_ast = ast->child[3];
9803
6.68k
  if (attr_ast) {
9804
141
    zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9805
9806
141
    zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9807
9808
141
    if (deprecated) {
9809
47
      ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9810
47
    }
9811
141
  }
9812
6.68k
}
9813
9814
static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
9815
2.12k
{
9816
2.12k
  switch (type) {
9817
1.19k
    case ZEND_SYMBOL_CLASS:
9818
1.19k
      if (!FC(imports)) {
9819
854
        FC(imports) = emalloc(sizeof(HashTable));
9820
854
        zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
9821
854
      }
9822
1.19k
      return FC(imports);
9823
537
    case ZEND_SYMBOL_FUNCTION:
9824
537
      if (!FC(imports_function)) {
9825
450
        FC(imports_function) = emalloc(sizeof(HashTable));
9826
450
        zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
9827
450
      }
9828
537
      return FC(imports_function);
9829
391
    case ZEND_SYMBOL_CONST:
9830
391
      if (!FC(imports_const)) {
9831
323
        FC(imports_const) = emalloc(sizeof(HashTable));
9832
323
        zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
9833
323
      }
9834
391
      return FC(imports_const);
9835
2.12k
    EMPTY_SWITCH_DEFAULT_CASE()
9836
2.12k
  }
9837
9838
0
  return NULL;
9839
2.12k
}
9840
/* }}} */
9841
9842
static char *zend_get_use_type_str(uint32_t type) /* {{{ */
9843
70
{
9844
70
  switch (type) {
9845
39
    case ZEND_SYMBOL_CLASS:
9846
39
      return "";
9847
15
    case ZEND_SYMBOL_FUNCTION:
9848
15
      return " function";
9849
16
    case ZEND_SYMBOL_CONST:
9850
16
      return " const";
9851
70
    EMPTY_SWITCH_DEFAULT_CASE()
9852
70
  }
9853
9854
0
  return " unknown";
9855
70
}
9856
/* }}} */
9857
9858
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) /* {{{ */
9859
41
{
9860
41
  if (zend_string_equals_ci(old_name, check_name)) {
9861
14
    return;
9862
14
  }
9863
9864
27
  zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9865
27
    "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9866
41
}
9867
/* }}} */
9868
9869
static void zend_compile_use(zend_ast *ast) /* {{{ */
9870
2.12k
{
9871
2.12k
  const zend_ast_list *list = zend_ast_get_list(ast);
9872
2.12k
  uint32_t i;
9873
2.12k
  zend_string *current_ns = FC(current_namespace);
9874
2.12k
  uint32_t type = ast->attr;
9875
2.12k
  HashTable *current_import = zend_get_import_ht(type);
9876
2.12k
  bool case_sensitive = type == ZEND_SYMBOL_CONST;
9877
9878
4.39k
  for (i = 0; i < list->children; ++i) {
9879
2.34k
    const zend_ast *use_ast = list->child[i];
9880
2.34k
    zend_ast *old_name_ast = use_ast->child[0];
9881
2.34k
    zend_ast *new_name_ast = use_ast->child[1];
9882
2.34k
    zend_string *old_name = zend_ast_get_str(old_name_ast);
9883
2.34k
    zend_string *new_name, *lookup_name;
9884
9885
2.34k
    if (new_name_ast) {
9886
571
      new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
9887
1.77k
    } else {
9888
1.77k
      const char *unqualified_name;
9889
1.77k
      size_t unqualified_name_len;
9890
1.77k
      if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
9891
        /* The form "use A\B" is equivalent to "use A\B as B" */
9892
863
        new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
9893
908
      } else {
9894
908
        new_name = zend_string_copy(old_name);
9895
9896
908
        if (!current_ns) {
9897
547
          zend_error(E_WARNING, "The use statement with non-compound name '%s' "
9898
547
            "has no effect", ZSTR_VAL(new_name));
9899
547
        }
9900
908
      }
9901
1.77k
    }
9902
9903
2.34k
    if (case_sensitive) {
9904
470
      lookup_name = zend_string_copy(new_name);
9905
1.87k
    } else {
9906
1.87k
      lookup_name = zend_string_tolower(new_name);
9907
1.87k
    }
9908
9909
2.34k
    if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) {
9910
26
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
9911
26
        "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
9912
26
    }
9913
9914
2.31k
    if (current_ns) {
9915
1.19k
      zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
9916
1.19k
      zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
9917
1.19k
      ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
9918
1.19k
      memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1);
9919
9920
1.19k
      if (zend_have_seen_symbol(ns_name, type)) {
9921
16
        zend_check_already_in_use(type, old_name, new_name, ns_name);
9922
16
      }
9923
9924
1.19k
      zend_string_efree(ns_name);
9925
1.19k
    } else if (zend_have_seen_symbol(lookup_name, type)) {
9926
25
      zend_check_already_in_use(type, old_name, new_name, lookup_name);
9927
25
    }
9928
9929
2.31k
    zend_string_addref(old_name);
9930
2.31k
    old_name = zend_new_interned_string(old_name);
9931
2.31k
    if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
9932
43
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9933
43
        "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9934
43
    }
9935
9936
2.27k
    zend_string_release_ex(lookup_name, 0);
9937
2.27k
    zend_string_release_ex(new_name, 0);
9938
2.27k
  }
9939
2.12k
}
9940
/* }}} */
9941
9942
static void zend_compile_group_use(const zend_ast *ast) /* {{{ */
9943
259
{
9944
259
  uint32_t i;
9945
259
  const zend_string *ns = zend_ast_get_str(ast->child[0]);
9946
259
  const zend_ast_list *list = zend_ast_get_list(ast->child[1]);
9947
9948
960
  for (i = 0; i < list->children; i++) {
9949
701
    zend_ast *inline_use, *use = list->child[i];
9950
701
    zval *name_zval = zend_ast_get_zval(use->child[0]);
9951
701
    zend_string *name = Z_STR_P(name_zval);
9952
701
    zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
9953
701
    zend_string_release_ex(name, 0);
9954
701
    ZVAL_STR(name_zval, compound_ns);
9955
701
    inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
9956
701
    inline_use->attr = ast->attr ? ast->attr : use->attr;
9957
701
    zend_compile_use(inline_use);
9958
701
  }
9959
259
}
9960
/* }}} */
9961
9962
static void zend_compile_const_decl(zend_ast *ast) /* {{{ */
9963
3.31k
{
9964
3.31k
  zend_ast_list *list = zend_ast_get_list(ast);
9965
3.31k
  uint32_t i;
9966
3.31k
  zend_ast *attributes_ast = NULL;
9967
3.31k
  zend_op *last_op = NULL;
9968
7.19k
  for (i = 0; i < list->children; ++i) {
9969
3.89k
    zend_ast *const_ast = list->child[i];
9970
3.89k
    if (const_ast->kind == ZEND_AST_ATTRIBUTE_LIST) {
9971
306
      ZEND_ASSERT(i == list->children - 1);
9972
306
      attributes_ast = const_ast;
9973
306
      continue;
9974
306
    }
9975
3.58k
    ZEND_ASSERT(const_ast->kind == ZEND_AST_CONST_ELEM);
9976
3.58k
    zend_ast *name_ast = const_ast->child[0];
9977
3.58k
    zend_ast **value_ast_ptr = &const_ast->child[1];
9978
3.58k
    zend_string *unqualified_name = zend_ast_get_str(name_ast);
9979
9980
3.58k
    zend_string *name;
9981
3.58k
    znode name_node, value_node;
9982
3.58k
    zval *value_zv = &value_node.u.constant;
9983
9984
3.58k
    value_node.op_type = IS_CONST;
9985
3.58k
    zend_const_expr_to_zval(value_zv, value_ast_ptr, /* allow_dynamic */ true);
9986
9987
3.58k
    if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) {
9988
5
      zend_error_noreturn(E_COMPILE_ERROR,
9989
5
        "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name));
9990
5
    }
9991
9992
3.58k
    name = zend_prefix_with_ns(unqualified_name);
9993
3.58k
    name = zend_new_interned_string(name);
9994
9995
3.58k
    if (FC(imports_const)) {
9996
337
      zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name);
9997
337
      if (import_name && !zend_string_equals(import_name, name)) {
9998
8
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because "
9999
8
          "the name is already in use", ZSTR_VAL(name));
10000
8
      }
10001
337
    }
10002
10003
3.57k
    name_node.op_type = IS_CONST;
10004
3.57k
    ZVAL_STR(&name_node.u.constant, name);
10005
10006
3.57k
    last_op = zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
10007
10008
3.57k
    zend_register_seen_symbol(name, ZEND_SYMBOL_CONST);
10009
3.57k
  }
10010
3.29k
  if (attributes_ast == NULL) {
10011
2.89k
    return;
10012
2.89k
  }
10013
  /* Validate: attributes can only be applied to one constant at a time
10014
   * Since we store the AST for the attributes in the list of children,
10015
   * there should be exactly 2 children. */
10016
401
  if (list->children > 2) {
10017
6
    zend_error_noreturn(
10018
6
      E_COMPILE_ERROR,
10019
6
      "Cannot apply attributes to multiple constants at once"
10020
6
    );
10021
6
  }
10022
10023
395
  HashTable *attributes = NULL;
10024
395
  zend_compile_attributes(&attributes, list->child[1], 0, ZEND_ATTRIBUTE_TARGET_CONST, 0);
10025
10026
395
  ZEND_ASSERT(last_op != NULL);
10027
395
  last_op->opcode = ZEND_DECLARE_ATTRIBUTED_CONST;
10028
291
  znode attribs_node;
10029
291
  attribs_node.op_type = IS_CONST;
10030
291
  ZVAL_PTR(&attribs_node.u.constant, attributes);
10031
291
  zend_emit_op_data(&attribs_node);
10032
291
  CG(active_op_array)->fn_flags |= ZEND_ACC_PTR_OPS;
10033
291
}
10034
/* }}}*/
10035
10036
static void zend_compile_namespace(const zend_ast *ast) /* {{{ */
10037
4.20k
{
10038
4.20k
  zend_ast *name_ast = ast->child[0];
10039
4.20k
  zend_ast *stmt_ast = ast->child[1];
10040
4.20k
  zend_string *name;
10041
4.20k
  bool with_bracket = stmt_ast != NULL;
10042
10043
  /* handle mixed syntax declaration or nested namespaces */
10044
4.20k
  if (!FC(has_bracketed_namespaces)) {
10045
3.12k
    if (FC(current_namespace)) {
10046
      /* previous namespace declarations were unbracketed */
10047
760
      if (with_bracket) {
10048
7
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
10049
7
          "with unbracketed namespace declarations");
10050
7
      }
10051
760
    }
10052
3.12k
  } else {
10053
    /* previous namespace declarations were bracketed */
10054
1.08k
    if (!with_bracket) {
10055
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
10056
6
        "with unbracketed namespace declarations");
10057
1.08k
    } else if (FC(current_namespace) || FC(in_namespace)) {
10058
7
      zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
10059
7
    }
10060
1.08k
  }
10061
10062
4.20k
  bool is_first_namespace = (!with_bracket && !FC(current_namespace))
10063
2.32k
    || (with_bracket && !FC(has_bracketed_namespaces));
10064
4.18k
  if (is_first_namespace && FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
10065
28
    zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
10066
28
      "the very first statement or after any declare call in the script");
10067
28
  }
10068
10069
4.16k
  if (FC(current_namespace)) {
10070
753
    zend_string_release_ex(FC(current_namespace), 0);
10071
753
  }
10072
10073
4.16k
  if (name_ast) {
10074
3.49k
    name = zend_ast_get_str(name_ast);
10075
10076
3.49k
    if (zend_string_equals_literal_ci(name, "namespace")) {
10077
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name));
10078
5
    }
10079
10080
3.49k
    FC(current_namespace) = zend_string_copy(name);
10081
3.49k
  } else {
10082
663
    FC(current_namespace) = NULL;
10083
663
  }
10084
10085
4.15k
  zend_reset_import_tables();
10086
10087
4.15k
  FC(in_namespace) = 1;
10088
4.15k
  if (with_bracket) {
10089
1.56k
    FC(has_bracketed_namespaces) = 1;
10090
1.56k
  }
10091
10092
4.15k
  if (stmt_ast) {
10093
1.56k
    zend_compile_top_stmt(stmt_ast);
10094
1.56k
    zend_end_namespace();
10095
1.56k
  }
10096
4.15k
}
10097
/* }}} */
10098
10099
static void zend_compile_halt_compiler(const zend_ast *ast) /* {{{ */
10100
55
{
10101
55
  zend_ast *offset_ast = ast->child[0];
10102
55
  zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
10103
10104
55
  const char const_name[] = "__COMPILER_HALT_OFFSET__";
10105
10106
55
  if (FC(has_bracketed_namespaces) && FC(in_namespace)) {
10107
0
    zend_error_noreturn(E_COMPILE_ERROR,
10108
0
      "__HALT_COMPILER() can only be used from the outermost scope");
10109
0
  }
10110
10111
55
  const zend_string *filename = zend_get_compiled_filename();
10112
55
  zend_string *name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
10113
55
    ZSTR_VAL(filename), ZSTR_LEN(filename), false);
10114
10115
  /* Avoid repeated declaration of the __COMPILER_HALT_OFFSET__ constant in
10116
   * case this file was already included. */
10117
55
  if (!zend_hash_find(EG(zend_constants), name)) {
10118
55
    zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0);
10119
55
  }
10120
55
  zend_string_release_ex(name, 0);
10121
55
}
10122
/* }}} */
10123
10124
static bool zend_try_ct_eval_magic_const(zval *zv, const zend_ast *ast) /* {{{ */
10125
25.3k
{
10126
25.3k
  const zend_op_array *op_array = CG(active_op_array);
10127
25.3k
  const zend_class_entry *ce = CG(active_class_entry);
10128
10129
25.3k
  switch (ast->attr) {
10130
301
    case T_LINE:
10131
301
      ZVAL_LONG(zv, ast->lineno);
10132
301
      break;
10133
4.70k
    case T_FILE:
10134
4.70k
      ZVAL_STR_COPY(zv, CG(compiled_filename));
10135
4.70k
      break;
10136
1.51k
    case T_DIR:
10137
1.51k
    {
10138
1.51k
      const zend_string *filename = CG(compiled_filename);
10139
1.51k
      zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
10140
#ifdef ZEND_WIN32
10141
      ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
10142
#else
10143
1.51k
      ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
10144
1.51k
#endif
10145
10146
1.51k
      if (zend_string_equals_literal(dirname, ".")) {
10147
1.03k
        dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
10148
1.03k
#ifdef HAVE_GETCWD
10149
1.03k
        ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN));
10150
#elif defined(HAVE_GETWD)
10151
        ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname)));
10152
#endif
10153
1.03k
        ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname));
10154
1.03k
      }
10155
10156
1.51k
      ZVAL_STR(zv, dirname);
10157
1.51k
      break;
10158
0
    }
10159
8.12k
    case T_FUNC_C:
10160
8.12k
      if (op_array && op_array->function_name) {
10161
7.94k
        ZVAL_STR_COPY(zv, op_array->function_name);
10162
7.94k
      } else {
10163
183
        ZVAL_EMPTY_STRING(zv);
10164
183
      }
10165
8.12k
      break;
10166
304
    case T_PROPERTY_C: {
10167
304
      zend_string *prop_info_name = CG(context).active_property_info_name;
10168
304
      if (prop_info_name) {
10169
243
        ZVAL_STR(zv, zend_copy_unmangled_prop_name(prop_info_name));
10170
243
      } else {
10171
61
        ZVAL_EMPTY_STRING(zv);
10172
61
      }
10173
304
      break;
10174
0
    }
10175
4.36k
    case T_METHOD_C:
10176
      /* Detect whether we are directly inside a class (e.g. a class constant) and treat
10177
       * this as not being inside a function. */
10178
4.36k
      if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
10179
200
        op_array = NULL;
10180
200
      }
10181
4.36k
      if (op_array && op_array->function_name) {
10182
4.10k
        if (op_array->scope) {
10183
3.57k
          ZVAL_NEW_STR(zv,
10184
3.57k
            zend_create_member_string(op_array->scope->name, op_array->function_name));
10185
3.57k
        } else {
10186
536
          ZVAL_STR_COPY(zv, op_array->function_name);
10187
536
        }
10188
4.10k
      } else {
10189
252
        ZVAL_EMPTY_STRING(zv);
10190
252
      }
10191
4.36k
      break;
10192
4.20k
    case T_CLASS_C:
10193
4.20k
      if (ce) {
10194
1.77k
        if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10195
1.30k
          return 0;
10196
1.30k
        } else {
10197
476
          ZVAL_STR_COPY(zv, ce->name);
10198
476
        }
10199
2.42k
      } else {
10200
2.42k
        ZVAL_EMPTY_STRING(zv);
10201
2.42k
      }
10202
2.90k
      break;
10203
2.90k
    case T_TRAIT_C:
10204
1.31k
      if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10205
255
        ZVAL_STR_COPY(zv, ce->name);
10206
1.05k
      } else {
10207
1.05k
        ZVAL_EMPTY_STRING(zv);
10208
1.05k
      }
10209
1.31k
      break;
10210
548
    case T_NS_C:
10211
548
      if (FC(current_namespace)) {
10212
339
        ZVAL_STR_COPY(zv, FC(current_namespace));
10213
339
      } else {
10214
209
        ZVAL_EMPTY_STRING(zv);
10215
209
      }
10216
548
      break;
10217
25.3k
    EMPTY_SWITCH_DEFAULT_CASE()
10218
25.3k
  }
10219
10220
24.0k
  return 1;
10221
25.3k
}
10222
/* }}} */
10223
10224
ZEND_API bool zend_is_op_long_compatible(const zval *op)
10225
58.0k
{
10226
58.0k
  if (Z_TYPE_P(op) == IS_ARRAY) {
10227
345
    return false;
10228
345
  }
10229
10230
57.6k
  if (Z_TYPE_P(op) == IS_DOUBLE
10231
16.8k
    && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval_silent(Z_DVAL_P(op)))) {
10232
12.7k
    return false;
10233
12.7k
  }
10234
10235
44.8k
  if (Z_TYPE_P(op) == IS_STRING) {
10236
7.47k
    double dval = 0;
10237
7.47k
    uint8_t is_num = is_numeric_str_function(Z_STR_P(op), NULL, &dval);
10238
7.47k
    if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval_silent(dval)))) {
10239
2.22k
      return false;
10240
2.22k
    }
10241
7.47k
  }
10242
10243
42.6k
  return true;
10244
44.8k
}
10245
10246
ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, const zval *op2) /* {{{ */
10247
675k
{
10248
675k
  if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) {
10249
    /* Array to string warning. */
10250
33.1k
    return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY;
10251
33.1k
  }
10252
10253
642k
  if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV
10254
555k
               || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR
10255
506k
               || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) {
10256
    /* Only the numeric operations throw errors. */
10257
473k
    return 0;
10258
473k
  }
10259
10260
169k
  if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) {
10261
7.90k
    if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) {
10262
      /* Adding two arrays is allowed. */
10263
3.64k
      return 0;
10264
3.64k
    }
10265
10266
    /* Numeric operators throw when one of the operands is an array. */
10267
4.26k
    return 1;
10268
7.90k
  }
10269
10270
  /* While basic arithmetic operators always produce numeric string errors,
10271
   * bitwise operators don't produce errors if both operands are strings */
10272
161k
  if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)
10273
33.4k
    && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
10274
16.3k
    return 0;
10275
16.3k
  }
10276
10277
145k
  if (Z_TYPE_P(op1) == IS_STRING
10278
36.5k
    && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) {
10279
21.6k
    return 1;
10280
21.6k
  }
10281
10282
123k
  if (Z_TYPE_P(op2) == IS_STRING
10283
26.4k
    && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) {
10284
20.9k
    return 1;
10285
20.9k
  }
10286
10287
  /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */
10288
102k
  if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR
10289
97.6k
      || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) {
10290
19.3k
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2)) {
10291
8.33k
      return 1;
10292
8.33k
    }
10293
19.3k
  }
10294
10295
94.5k
  if (opcode == ZEND_DIV && zval_get_double(op2) == 0.0) {
10296
    /* Division by zero throws an error. */
10297
943
    return 1;
10298
943
  }
10299
10300
  /* Mod is an operation that will cast float/float-strings to integers which might
10301
     produce float to int incompatible errors, and also cannot be divided by 0 */
10302
93.5k
  if (opcode == ZEND_MOD) {
10303
11.0k
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2) || zval_get_long(op2) == 0) {
10304
7.85k
      return 1;
10305
7.85k
    }
10306
11.0k
  }
10307
10308
85.7k
  if ((opcode == ZEND_POW) && zval_get_double(op1) == 0 && zval_get_double(op2) < 0) {
10309
    /* 0 ** (<0) throws a division by zero error. */
10310
149
    return 1;
10311
149
  }
10312
85.5k
  if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) {
10313
    /* Shift by negative number throws an error. */
10314
223
    return 1;
10315
223
  }
10316
10317
85.3k
  return 0;
10318
85.5k
}
10319
/* }}} */
10320
10321
static inline bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
10322
624k
{
10323
624k
  if (zend_binary_op_produces_error(opcode, op1, op2)) {
10324
40.7k
    return 0;
10325
40.7k
  }
10326
10327
583k
  const binary_op_type fn = get_binary_op(opcode);
10328
583k
  fn(result, op1, op2);
10329
583k
  return 1;
10330
624k
}
10331
/* }}} */
10332
10333
ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op)
10334
277k
{
10335
277k
  if (opcode == ZEND_BW_NOT) {
10336
    /* BW_NOT on string does not convert the string into an integer. */
10337
4.96k
    if (Z_TYPE_P(op) == IS_STRING) {
10338
1.09k
      return 0;
10339
1.09k
    }
10340
3.87k
    return Z_TYPE_P(op) <= IS_TRUE || !zend_is_op_long_compatible(op);
10341
4.96k
  }
10342
  /* Can happen when called from zend_optimizer_eval_unary_op() */
10343
272k
  if (opcode == ZEND_BOOL || opcode == ZEND_BOOL_NOT) {
10344
    /* ZEND_BOOL/ZEND_BOOL_NOT warns when casting NAN. */
10345
272k
    return Z_TYPE_P(op) == IS_DOUBLE;
10346
272k
  }
10347
10348
0
  return 0;
10349
272k
}
10350
10351
static inline bool zend_try_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */
10352
276k
{
10353
276k
  if (zend_unary_op_produces_error(opcode, op)) {
10354
967
    return 0;
10355
967
  }
10356
10357
275k
  const unary_op_type fn = get_unary_op(opcode);
10358
275k
  fn(result, op);
10359
275k
  return 1;
10360
276k
}
10361
/* }}} */
10362
10363
static inline bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
10364
42.1k
{
10365
42.1k
  zval right;
10366
42.1k
  ZVAL_LONG(&right, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10367
42.1k
  return zend_try_ct_eval_binary_op(result, ZEND_MUL, op, &right);
10368
42.1k
}
10369
/* }}} */
10370
10371
static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
10372
6.96k
{
10373
6.96k
  const binary_op_type fn = kind == ZEND_AST_GREATER
10374
6.96k
    ? is_smaller_function : is_smaller_or_equal_function;
10375
6.96k
  fn(result, op2, op1);
10376
6.96k
}
10377
/* }}} */
10378
10379
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
10380
2.08M
{
10381
2.08M
  const zend_ast_list *list = zend_ast_get_list(ast);
10382
2.08M
  zend_ast *last_elem_ast = NULL;
10383
2.08M
  uint32_t i;
10384
2.08M
  bool is_constant = true;
10385
10386
2.08M
  if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
10387
5
    zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
10388
5
  }
10389
10390
  /* First ensure that *all* child nodes are constant and by-val */
10391
4.51M
  for (i = 0; i < list->children; ++i) {
10392
2.42M
    zend_ast *elem_ast = list->child[i];
10393
10394
2.42M
    if (elem_ast == NULL) {
10395
      /* Report error at line of last non-empty element */
10396
100
      if (last_elem_ast) {
10397
55
        CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast);
10398
55
      }
10399
100
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
10400
100
    }
10401
10402
2.42M
    if (elem_ast->kind != ZEND_AST_UNPACK) {
10403
2.29M
      zend_eval_const_expr(&elem_ast->child[0]);
10404
2.29M
      zend_eval_const_expr(&elem_ast->child[1]);
10405
10406
2.29M
      if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL
10407
380k
        || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
10408
2.29M
      ) {
10409
1.91M
        is_constant = false;
10410
1.91M
      }
10411
2.29M
    } else {
10412
132k
      zend_eval_const_expr(&elem_ast->child[0]);
10413
10414
132k
      if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) {
10415
130k
        is_constant = false;
10416
130k
      }
10417
132k
    }
10418
10419
2.42M
    last_elem_ast = elem_ast;
10420
2.42M
  }
10421
10422
2.08M
  if (!is_constant) {
10423
2.00M
    return 0;
10424
2.00M
  }
10425
10426
87.7k
  if (!list->children) {
10427
22.8k
    ZVAL_EMPTY_ARRAY(result);
10428
22.8k
    return 1;
10429
22.8k
  }
10430
10431
64.9k
  array_init_size(result, list->children);
10432
395k
  for (i = 0; i < list->children; ++i) {
10433
332k
    const zend_ast *elem_ast = list->child[i];
10434
332k
    zend_ast *value_ast = elem_ast->child[0];
10435
332k
    zend_ast *key_ast;
10436
10437
332k
    zval *value = zend_ast_get_zval(value_ast);
10438
332k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
10439
877
      if (Z_TYPE_P(value) == IS_ARRAY) {
10440
871
        const HashTable *ht = Z_ARRVAL_P(value);
10441
871
        zval *val;
10442
871
        zend_string *key;
10443
10444
2.67k
        ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
10445
2.67k
          if (key) {
10446
143
            zend_hash_update(Z_ARRVAL_P(result), key, val);
10447
373
          } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
10448
12
            zval_ptr_dtor(result);
10449
12
            return 0;
10450
12
          }
10451
504
          Z_TRY_ADDREF_P(val);
10452
504
        } ZEND_HASH_FOREACH_END();
10453
10454
859
        continue;
10455
871
      } else {
10456
6
        zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked, %s given", zend_zval_value_name(value));
10457
6
      }
10458
877
    }
10459
10460
331k
    Z_TRY_ADDREF_P(value);
10461
10462
331k
    key_ast = elem_ast->child[1];
10463
331k
    if (key_ast) {
10464
15.7k
      const zval *key = zend_ast_get_zval(key_ast);
10465
15.7k
      switch (Z_TYPE_P(key)) {
10466
5.48k
        case IS_LONG:
10467
5.48k
          zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value);
10468
5.48k
          break;
10469
7.78k
        case IS_STRING:
10470
7.78k
          zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value);
10471
7.78k
          break;
10472
2.20k
        case IS_DOUBLE: {
10473
2.20k
          zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key));
10474
          /* Incompatible float will generate an error, leave this to run-time */
10475
2.20k
          if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) {
10476
1.99k
            goto fail;
10477
1.99k
          }
10478
218
          zend_hash_index_update(Z_ARRVAL_P(result), lval, value);
10479
218
          break;
10480
2.20k
        }
10481
233
        case IS_FALSE:
10482
233
          zend_hash_index_update(Z_ARRVAL_P(result), 0, value);
10483
233
          break;
10484
52
        case IS_TRUE:
10485
52
          zend_hash_index_update(Z_ARRVAL_P(result), 1, value);
10486
52
          break;
10487
31
        case IS_NULL:
10488
          /* Null key will generate a warning at run-time. */
10489
31
          goto fail;
10490
4
        default:
10491
4
          zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type");
10492
15.7k
      }
10493
315k
    } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) {
10494
2.08k
fail:
10495
2.08k
      zval_ptr_dtor_nogc(value);
10496
2.08k
      zval_ptr_dtor(result);
10497
2.08k
      return 0;
10498
66
    }
10499
331k
  }
10500
10501
62.8k
  return 1;
10502
64.9k
}
10503
/* }}} */
10504
10505
static void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */
10506
2.67M
{
10507
2.67M
  zend_ast *left_ast = ast->child[0];
10508
2.67M
  zend_ast *right_ast = ast->child[1];
10509
2.67M
  uint32_t opcode = ast->attr;
10510
10511
2.67M
  znode left_node, right_node;
10512
10513
2.67M
  zend_compile_expr(&left_node, left_ast);
10514
2.67M
  zend_compile_expr(&right_node, right_ast);
10515
10516
2.67M
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10517
551k
    if (zend_try_ct_eval_binary_op(&result->u.constant, opcode,
10518
551k
        &left_node.u.constant, &right_node.u.constant)
10519
551k
    ) {
10520
535k
      result->op_type = IS_CONST;
10521
535k
      zval_ptr_dtor(&left_node.u.constant);
10522
535k
      zval_ptr_dtor(&right_node.u.constant);
10523
535k
      return;
10524
535k
    }
10525
551k
  }
10526
10527
2.14M
  do {
10528
2.14M
    if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) {
10529
      /* 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) */
10530
18.7k
      if (left_node.op_type == IS_CONST) {
10531
1.14k
        if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) {
10532
494
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL);
10533
494
          opline->extended_value =
10534
494
            (opcode == ZEND_IS_IDENTICAL) ?
10535
342
              (1 << Z_TYPE(left_node.u.constant)) :
10536
494
              (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant)));
10537
494
          return;
10538
494
        }
10539
17.5k
      } else if (right_node.op_type == IS_CONST) {
10540
15.7k
        if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) {
10541
14.4k
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL);
10542
14.4k
          opline->extended_value =
10543
14.4k
            (opcode == ZEND_IS_IDENTICAL) ?
10544
7.17k
              (1 << Z_TYPE(right_node.u.constant)) :
10545
14.4k
              (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant)));
10546
14.4k
          return;
10547
14.4k
        }
10548
15.7k
      }
10549
2.12M
    } else if (opcode == ZEND_CONCAT) {
10550
      /* convert constant operands to strings at compile-time */
10551
109k
      if (left_node.op_type == IS_CONST) {
10552
15.5k
        if (Z_TYPE(left_node.u.constant) == IS_ARRAY) {
10553
312
          zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING;
10554
15.2k
        } else {
10555
15.2k
          convert_to_string(&left_node.u.constant);
10556
15.2k
        }
10557
15.5k
      }
10558
109k
      if (right_node.op_type == IS_CONST) {
10559
22.8k
        if (Z_TYPE(right_node.u.constant) == IS_ARRAY) {
10560
337
          zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING;
10561
22.5k
        } else {
10562
22.5k
          convert_to_string(&right_node.u.constant);
10563
22.5k
        }
10564
22.8k
      }
10565
109k
      if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10566
0
        opcode = ZEND_FAST_CONCAT;
10567
0
      }
10568
109k
    }
10569
2.12M
    zend_emit_op_tmp(result, opcode, &left_node, &right_node);
10570
2.12M
  } while (0);
10571
2.14M
}
10572
/* }}} */
10573
10574
/* We do not use zend_compile_binary_op for this because we want to retain the left-to-right
10575
 * evaluation order. */
10576
static void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */
10577
228k
{
10578
228k
  zend_ast *left_ast = ast->child[0];
10579
228k
  zend_ast *right_ast = ast->child[1];
10580
228k
  znode left_node, right_node;
10581
10582
228k
  ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL);
10583
10584
228k
  zend_compile_expr(&left_node, left_ast);
10585
228k
  zend_compile_expr(&right_node, right_ast);
10586
10587
228k
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10588
5.95k
    result->op_type = IS_CONST;
10589
5.95k
    zend_ct_eval_greater(&result->u.constant, ast->kind,
10590
5.95k
      &left_node.u.constant, &right_node.u.constant);
10591
5.95k
    zval_ptr_dtor(&left_node.u.constant);
10592
5.95k
    zval_ptr_dtor(&right_node.u.constant);
10593
5.95k
    return;
10594
5.95k
  }
10595
10596
222k
  zend_emit_op_tmp(result,
10597
222k
    ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL,
10598
222k
    &right_node, &left_node);
10599
222k
}
10600
/* }}} */
10601
10602
static void zend_compile_unary_op(znode *result, const zend_ast *ast) /* {{{ */
10603
1.14M
{
10604
1.14M
  zend_ast *expr_ast = ast->child[0];
10605
1.14M
  uint32_t opcode = ast->attr;
10606
10607
1.14M
  znode expr_node;
10608
1.14M
  zend_compile_expr(&expr_node, expr_ast);
10609
10610
1.14M
  if (expr_node.op_type == IS_CONST
10611
273k
      && zend_try_ct_eval_unary_op(&result->u.constant, opcode, &expr_node.u.constant)) {
10612
272k
    result->op_type = IS_CONST;
10613
272k
    zval_ptr_dtor(&expr_node.u.constant);
10614
272k
    return;
10615
272k
  }
10616
10617
873k
  zend_emit_op_tmp(result, opcode, &expr_node, NULL);
10618
873k
}
10619
/* }}} */
10620
10621
static void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */
10622
172k
{
10623
172k
  zend_ast *expr_ast = ast->child[0];
10624
172k
  znode expr_node, right_node;
10625
10626
172k
  ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS);
10627
10628
172k
  zend_compile_expr(&expr_node, expr_ast);
10629
10630
172k
  if (expr_node.op_type == IS_CONST
10631
25.9k
    && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) {
10632
21.9k
    result->op_type = IS_CONST;
10633
21.9k
    zval_ptr_dtor(&expr_node.u.constant);
10634
21.9k
    return;
10635
21.9k
  }
10636
10637
150k
  right_node.op_type = IS_CONST;
10638
150k
  ZVAL_LONG(&right_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10639
150k
  zend_emit_op_tmp(result, ZEND_MUL, &expr_node, &right_node);
10640
150k
}
10641
/* }}} */
10642
10643
static void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */
10644
10.8k
{
10645
10.8k
  zend_ast *left_ast = ast->child[0];
10646
10.8k
  zend_ast *right_ast = ast->child[1];
10647
10648
10.8k
  znode left_node, right_node;
10649
10.8k
  zend_op *opline_jmpz, *opline_bool;
10650
10.8k
  uint32_t opnum_jmpz;
10651
10652
10.8k
  ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR);
10653
10654
10.8k
  zend_compile_expr(&left_node, left_ast);
10655
10656
10.8k
  if (left_node.op_type == IS_CONST) {
10657
1.97k
    if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant))
10658
1.21k
     || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) {
10659
1.21k
      result->op_type = IS_CONST;
10660
1.21k
      ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant));
10661
1.21k
    } else {
10662
761
      zend_compile_expr(&right_node, right_ast);
10663
10664
761
      if (right_node.op_type == IS_CONST) {
10665
360
        result->op_type = IS_CONST;
10666
360
        ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant));
10667
10668
360
        zval_ptr_dtor(&right_node.u.constant);
10669
401
      } else {
10670
401
        zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL);
10671
401
      }
10672
761
    }
10673
10674
1.97k
    zval_ptr_dtor(&left_node.u.constant);
10675
1.97k
    return;
10676
1.97k
  }
10677
10678
8.87k
  opnum_jmpz = get_next_op_number();
10679
8.87k
  opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
10680
8.87k
    &left_node, NULL);
10681
10682
8.87k
  if (left_node.op_type == IS_TMP_VAR) {
10683
8.16k
    SET_NODE(opline_jmpz->result, &left_node);
10684
8.16k
    GET_NODE(result, opline_jmpz->result);
10685
8.16k
  } else {
10686
706
    zend_make_tmp_result(result, opline_jmpz);
10687
706
  }
10688
10689
8.87k
  zend_compile_expr(&right_node, right_ast);
10690
10691
8.87k
  opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
10692
8.87k
  SET_NODE(opline_bool->result, result);
10693
10694
8.87k
  zend_update_jump_target_to_next(opnum_jmpz);
10695
8.87k
}
10696
/* }}} */
10697
10698
static void zend_compile_post_incdec(znode *result, const zend_ast *ast) /* {{{ */
10699
10.1k
{
10700
10.1k
  zend_ast *var_ast = ast->child[0];
10701
10.1k
  ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
10702
10703
10.1k
  zend_ensure_writable_variable(var_ast);
10704
10705
10.1k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10706
698
    zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, false);
10707
698
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
10708
698
    zend_make_tmp_result(result, opline);
10709
9.44k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10710
1.07k
    zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, false, false);
10711
1.07k
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP;
10712
1.07k
    zend_make_tmp_result(result, opline);
10713
8.37k
  } else {
10714
8.37k
    znode var_node;
10715
8.37k
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10716
8.37k
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10717
135
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10718
135
    }
10719
8.37k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC,
10720
8.37k
      &var_node, NULL);
10721
8.37k
  }
10722
10.1k
}
10723
/* }}} */
10724
10725
static void zend_compile_pre_incdec(znode *result, const zend_ast *ast) /* {{{ */
10726
2.97k
{
10727
2.97k
  zend_ast *var_ast = ast->child[0];
10728
2.97k
  ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
10729
10730
2.97k
  zend_ensure_writable_variable(var_ast);
10731
10732
2.97k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10733
585
    zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, false);
10734
585
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;
10735
585
    opline->result_type = IS_TMP_VAR;
10736
585
    result->op_type = IS_TMP_VAR;
10737
2.39k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10738
307
    zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, false, false);
10739
307
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP;
10740
307
    opline->result_type = IS_TMP_VAR;
10741
307
    result->op_type = IS_TMP_VAR;
10742
2.08k
  } else {
10743
2.08k
    znode var_node;
10744
2.08k
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10745
2.08k
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10746
90
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10747
90
    }
10748
2.08k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC,
10749
2.08k
      &var_node, NULL);
10750
2.08k
  }
10751
2.97k
}
10752
/* }}} */
10753
10754
static void zend_compile_cast(znode *result, const zend_ast *ast) /* {{{ */
10755
2.65k
{
10756
2.65k
  zend_ast *expr_ast = ast->child[0];
10757
2.65k
  znode expr_node;
10758
2.65k
  zend_op *opline;
10759
10760
2.65k
  zend_compile_expr(&expr_node, expr_ast);
10761
10762
2.65k
  if (ast->attr == _IS_BOOL) {
10763
146
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL);
10764
2.50k
  } else if (ast->attr == IS_NULL) {
10765
16
    zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
10766
2.48k
  } else {
10767
2.48k
    opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
10768
2.48k
    opline->extended_value = ast->attr;
10769
2.48k
  }
10770
2.65k
}
10771
/* }}} */
10772
10773
static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */
10774
2.69k
{
10775
2.69k
  zend_ast *cond_ast = ast->child[0];
10776
2.69k
  zend_ast *false_ast = ast->child[2];
10777
10778
2.69k
  znode cond_node, false_node;
10779
2.69k
  zend_op *opline_qm_assign;
10780
2.69k
  uint32_t opnum_jmp_set;
10781
10782
2.69k
  ZEND_ASSERT(ast->child[1] == NULL);
10783
10784
2.69k
  zend_compile_expr(&cond_node, cond_ast);
10785
10786
2.69k
  opnum_jmp_set = get_next_op_number();
10787
2.69k
  zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL);
10788
10789
2.69k
  zend_compile_expr(&false_node, false_ast);
10790
10791
2.69k
  opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10792
2.69k
  SET_NODE(opline_qm_assign->result, result);
10793
10794
2.69k
  zend_update_jump_target_to_next(opnum_jmp_set);
10795
2.69k
}
10796
/* }}} */
10797
10798
static void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
10799
6.97k
{
10800
6.97k
  zend_ast *cond_ast = ast->child[0];
10801
6.97k
  zend_ast *true_ast = ast->child[1];
10802
6.97k
  zend_ast *false_ast = ast->child[2];
10803
10804
6.97k
  znode cond_node, true_node, false_node;
10805
6.97k
  zend_op *opline_qm_assign2;
10806
6.97k
  uint32_t opnum_jmpz, opnum_jmp;
10807
10808
6.97k
  if (cond_ast->kind == ZEND_AST_CONDITIONAL
10809
1.04k
      && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) {
10810
883
    if (cond_ast->child[1]) {
10811
26
      if (true_ast) {
10812
13
        zend_error(E_COMPILE_ERROR,
10813
13
          "Unparenthesized `a ? b : c ? d : e` is not supported. "
10814
13
          "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`");
10815
13
      } else {
10816
13
        zend_error(E_COMPILE_ERROR,
10817
13
          "Unparenthesized `a ? b : c ?: d` is not supported. "
10818
13
          "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`");
10819
13
      }
10820
857
    } else {
10821
857
      if (true_ast) {
10822
6
        zend_error(E_COMPILE_ERROR,
10823
6
          "Unparenthesized `a ?: b ? c : d` is not supported. "
10824
6
          "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`");
10825
851
      } else {
10826
        /* This case is harmless:  (a ?: b) ?: c always produces the same result
10827
         * as a ?: (b ?: c). */
10828
851
      }
10829
857
    }
10830
883
  }
10831
10832
6.97k
  if (!true_ast) {
10833
2.69k
    zend_compile_shorthand_conditional(result, ast);
10834
2.69k
    return;
10835
2.69k
  }
10836
10837
4.28k
  zend_compile_expr(&cond_node, cond_ast);
10838
10839
4.28k
  opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
10840
10841
4.28k
  zend_compile_expr(&true_node, true_ast);
10842
10843
4.28k
  zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL);
10844
10845
4.28k
  opnum_jmp = zend_emit_jump(0);
10846
10847
4.28k
  zend_update_jump_target_to_next(opnum_jmpz);
10848
10849
4.28k
  zend_compile_expr(&false_node, false_ast);
10850
10851
4.28k
  opline_qm_assign2 = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10852
4.28k
  SET_NODE(opline_qm_assign2->result, result);
10853
10854
4.28k
  zend_update_jump_target_to_next(opnum_jmp);
10855
4.28k
}
10856
/* }}} */
10857
10858
static void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
10859
1.26M
{
10860
1.26M
  zend_ast *expr_ast = ast->child[0];
10861
1.26M
  zend_ast *default_ast = ast->child[1];
10862
10863
1.26M
  znode expr_node, default_node;
10864
1.26M
  zend_op *opline;
10865
1.26M
  uint32_t opnum;
10866
10867
1.26M
  zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, false);
10868
10869
1.26M
  opnum = get_next_op_number();
10870
1.26M
  zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL);
10871
10872
1.26M
  zend_compile_expr(&default_node, default_ast);
10873
10874
1.26M
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL);
10875
1.26M
  SET_NODE(opline->result, result);
10876
10877
1.26M
  opline = &CG(active_op_array)->opcodes[opnum];
10878
1.26M
  opline->op2.opline_num = get_next_op_number();
10879
1.26M
}
10880
/* }}} */
10881
10882
67.1k
static void znode_dtor(zval *zv) {
10883
67.1k
  znode *node = Z_PTR_P(zv);
10884
67.1k
  if (node->op_type == IS_CONST) {
10885
5.03k
    zval_ptr_dtor_nogc(&node->u.constant);
10886
5.03k
  }
10887
67.1k
  efree(node);
10888
67.1k
}
10889
10890
static void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */
10891
22.7k
{
10892
22.7k
  zend_ast *var_ast = ast->child[0];
10893
22.7k
  zend_ast *default_ast = ast->child[1];
10894
10895
22.7k
  znode var_node_is, var_node_w, default_node, assign_node, *node;
10896
22.7k
  zend_op *opline;
10897
22.7k
  uint32_t coalesce_opnum;
10898
22.7k
  bool need_frees = false;
10899
10900
  /* Remember expressions compiled during the initial BP_VAR_IS lookup,
10901
   * to avoid double-evaluation when we compile again with BP_VAR_W. */
10902
22.7k
  HashTable *orig_memoized_exprs = CG(memoized_exprs);
10903
22.7k
  const zend_memoize_mode orig_memoize_mode = CG(memoize_mode);
10904
10905
22.7k
  zend_ensure_writable_variable(var_ast);
10906
22.7k
  if (is_this_fetch(var_ast)) {
10907
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
10908
5
  }
10909
10910
22.7k
  ALLOC_HASHTABLE(CG(memoized_exprs));
10911
22.7k
  zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0);
10912
10913
22.7k
  CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
10914
22.7k
  zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, false);
10915
10916
22.7k
  coalesce_opnum = get_next_op_number();
10917
22.7k
  zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL);
10918
10919
22.7k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
10920
22.7k
  if (var_ast->kind == ZEND_AST_DIM) {
10921
20.5k
    zend_compile_expr_with_potential_assign_to_self(&default_node, default_ast, var_ast);
10922
20.5k
  } else {
10923
2.18k
    zend_compile_expr(&default_node, default_ast);
10924
2.18k
  }
10925
10926
22.7k
  CG(memoize_mode) = ZEND_MEMOIZE_FETCH;
10927
22.7k
  zend_compile_var(&var_node_w, var_ast, BP_VAR_W, false);
10928
10929
  /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */
10930
22.7k
  opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
10931
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
10932
22.7k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
10933
22.7k
  switch (kind) {
10934
1.17k
    case ZEND_AST_VAR:
10935
1.17k
      zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node);
10936
1.17k
      break;
10937
534
    case ZEND_AST_STATIC_PROP:
10938
534
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
10939
534
      opline->result_type = IS_TMP_VAR;
10940
534
      var_node_w.op_type = IS_TMP_VAR;
10941
534
      zend_emit_op_data(&default_node);
10942
534
      assign_node = var_node_w;
10943
534
      break;
10944
20.0k
    case ZEND_AST_DIM:
10945
20.0k
      opline->opcode = ZEND_ASSIGN_DIM;
10946
20.0k
      opline->result_type = IS_TMP_VAR;
10947
20.0k
      var_node_w.op_type = IS_TMP_VAR;
10948
20.0k
      zend_emit_op_data(&default_node);
10949
20.0k
      assign_node = var_node_w;
10950
20.0k
      break;
10951
558
    case ZEND_AST_PROP:
10952
558
    case ZEND_AST_NULLSAFE_PROP:
10953
558
      opline->opcode = ZEND_ASSIGN_OBJ;
10954
558
      opline->result_type = IS_TMP_VAR;
10955
558
      var_node_w.op_type = IS_TMP_VAR;
10956
558
      zend_emit_op_data(&default_node);
10957
558
      assign_node = var_node_w;
10958
558
      break;
10959
0
    EMPTY_SWITCH_DEFAULT_CASE();
10960
22.7k
  }
10961
10962
22.3k
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL);
10963
22.3k
  SET_NODE(opline->result, result);
10964
10965
71.3k
  ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10966
71.3k
    if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10967
19.9k
      need_frees = true;
10968
19.9k
      break;
10969
19.9k
    }
10970
71.3k
  } ZEND_HASH_FOREACH_END();
10971
10972
  /* Free DUPed expressions if there are any */
10973
22.3k
  if (need_frees) {
10974
19.9k
    uint32_t jump_opnum = zend_emit_jump(0);
10975
19.9k
    zend_update_jump_target_to_next(coalesce_opnum);
10976
148k
    ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10977
148k
      if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10978
61.1k
        zend_emit_op(NULL, ZEND_FREE, node, NULL);
10979
61.1k
      }
10980
148k
    } ZEND_HASH_FOREACH_END();
10981
19.9k
    zend_update_jump_target_to_next(jump_opnum);
10982
19.9k
  } else {
10983
2.38k
    zend_update_jump_target_to_next(coalesce_opnum);
10984
2.38k
  }
10985
10986
22.3k
  zend_hash_destroy(CG(memoized_exprs));
10987
22.3k
  FREE_HASHTABLE(CG(memoized_exprs));
10988
22.3k
  CG(memoized_exprs) = orig_memoized_exprs;
10989
22.3k
  CG(memoize_mode) = orig_memoize_mode;
10990
22.3k
}
10991
/* }}} */
10992
10993
static void zend_compile_print(znode *result, const zend_ast *ast) /* {{{ */
10994
3.90k
{
10995
3.90k
  zend_op *opline;
10996
3.90k
  zend_ast *expr_ast = ast->child[0];
10997
10998
3.90k
  znode expr_node;
10999
3.90k
  zend_compile_expr(&expr_node, expr_ast);
11000
11001
3.90k
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
11002
3.90k
  opline->extended_value = 1;
11003
11004
3.90k
  result->op_type = IS_CONST;
11005
3.90k
  ZVAL_LONG(&result->u.constant, 1);
11006
3.90k
}
11007
/* }}} */
11008
11009
static void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
11010
135k
{
11011
135k
  zend_ast *value_ast = ast->child[0];
11012
135k
  zend_ast *key_ast = ast->child[1];
11013
11014
135k
  znode value_node, key_node;
11015
135k
  znode *value_node_ptr = NULL, *key_node_ptr = NULL;
11016
135k
  zend_op *opline;
11017
135k
  bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
11018
11019
135k
  zend_mark_function_as_generator();
11020
11021
135k
  if (key_ast) {
11022
411
    zend_compile_expr(&key_node, key_ast);
11023
411
    key_node_ptr = &key_node;
11024
411
  }
11025
11026
135k
  if (value_ast) {
11027
133k
    if (returns_by_ref && zend_is_variable_or_call(value_ast)) {
11028
456
      zend_assert_not_short_circuited(value_ast);
11029
456
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
11030
132k
    } else {
11031
132k
      zend_compile_expr(&value_node, value_ast);
11032
132k
    }
11033
133k
    value_node_ptr = &value_node;
11034
133k
  }
11035
11036
135k
  opline = zend_emit_op_tmp(result, ZEND_YIELD, value_node_ptr, key_node_ptr);
11037
11038
135k
  if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
11039
172
    opline->extended_value = ZEND_RETURNS_FUNCTION;
11040
172
  }
11041
135k
}
11042
/* }}} */
11043
11044
static void zend_compile_yield_from(znode *result, const zend_ast *ast) /* {{{ */
11045
1.79k
{
11046
1.79k
  zend_ast *expr_ast = ast->child[0];
11047
1.79k
  znode expr_node;
11048
11049
1.79k
  zend_mark_function_as_generator();
11050
11051
1.79k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
11052
5
    zend_error_noreturn(E_COMPILE_ERROR,
11053
5
      "Cannot use \"yield from\" inside a by-reference generator");
11054
5
  }
11055
11056
1.78k
  zend_compile_expr(&expr_node, expr_ast);
11057
1.78k
  zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
11058
1.78k
}
11059
/* }}} */
11060
11061
static void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */
11062
1.08k
{
11063
1.08k
  zend_ast *obj_ast = ast->child[0];
11064
1.08k
  zend_ast *class_ast = ast->child[1];
11065
11066
1.08k
  znode obj_node, class_node;
11067
1.08k
  zend_op *opline;
11068
11069
1.08k
  zend_compile_expr(&obj_node, obj_ast);
11070
1.08k
  if (obj_node.op_type == IS_CONST) {
11071
29
    zend_do_free(&obj_node);
11072
29
    result->op_type = IS_CONST;
11073
29
    ZVAL_FALSE(&result->u.constant);
11074
29
    return;
11075
29
  }
11076
11077
1.05k
  zend_compile_class_ref(&class_node, class_ast,
11078
1.05k
    ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION | ZEND_FETCH_CLASS_SILENT);
11079
11080
1.05k
  opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL);
11081
11082
1.05k
  if (class_node.op_type == IS_CONST) {
11083
735
    opline->op2_type = IS_CONST;
11084
735
    opline->op2.constant = zend_add_class_name_literal(
11085
735
      Z_STR(class_node.u.constant));
11086
735
    opline->extended_value = zend_alloc_cache_slot();
11087
735
  } else {
11088
324
    SET_NODE(opline->op2, &class_node);
11089
324
  }
11090
1.05k
}
11091
/* }}} */
11092
11093
static void zend_compile_include_or_eval(znode *result, const zend_ast *ast) /* {{{ */
11094
8.84k
{
11095
8.84k
  zend_ast *expr_ast = ast->child[0];
11096
8.84k
  znode expr_node;
11097
8.84k
  zend_op *opline;
11098
11099
8.84k
  zend_do_extended_fcall_begin();
11100
8.84k
  zend_compile_expr(&expr_node, expr_ast);
11101
11102
8.84k
  opline = zend_emit_op_tmp(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL);
11103
8.84k
  opline->extended_value = ast->attr;
11104
11105
8.84k
  zend_do_extended_fcall_end();
11106
8.84k
}
11107
/* }}} */
11108
11109
static void zend_compile_isset_or_empty(znode *result, const zend_ast *ast) /* {{{ */
11110
14.1k
{
11111
14.1k
  zend_ast *var_ast = ast->child[0];
11112
11113
14.1k
  znode var_node;
11114
14.1k
  zend_op *opline = NULL;
11115
11116
14.1k
  ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY);
11117
11118
14.1k
  if (!zend_is_variable(var_ast)) {
11119
164
    if (ast->kind == ZEND_AST_EMPTY) {
11120
      /* empty(expr) can be transformed to !expr */
11121
129
      zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast);
11122
129
      zend_compile_expr(result, not_ast);
11123
129
      return;
11124
129
    } else {
11125
35
      zend_error_noreturn(E_COMPILE_ERROR,
11126
35
        "Cannot use isset() on the result of an expression "
11127
35
        "(you can use \"null !== expression\" instead)");
11128
35
    }
11129
164
  }
11130
11131
13.9k
  if (is_globals_fetch(var_ast)) {
11132
242
    result->op_type = IS_CONST;
11133
242
    ZVAL_BOOL(&result->u.constant, ast->kind == ZEND_AST_ISSET);
11134
242
    return;
11135
242
  }
11136
11137
13.7k
  if (is_global_var_fetch(var_ast)) {
11138
7.92k
    if (!var_ast->child[1]) {
11139
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
11140
6
    }
11141
11142
7.92k
    zend_compile_expr(&var_node, var_ast->child[1]);
11143
7.92k
    if (var_node.op_type == IS_CONST) {
11144
7.89k
      convert_to_string(&var_node.u.constant);
11145
7.89k
    }
11146
11147
7.92k
    opline = zend_emit_op_tmp(result, ZEND_ISSET_ISEMPTY_VAR, &var_node, NULL);
11148
7.92k
    opline->extended_value =
11149
7.92k
      ZEND_FETCH_GLOBAL | (ast->kind == ZEND_AST_EMPTY ? ZEND_ISEMPTY : 0);
11150
7.92k
    return;
11151
7.92k
  }
11152
11153
5.78k
  zend_short_circuiting_mark_inner(var_ast);
11154
5.78k
  switch (var_ast->kind) {
11155
1.15k
    case ZEND_AST_VAR:
11156
1.15k
      if (is_this_fetch(var_ast)) {
11157
117
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL);
11158
117
        CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
11159
1.03k
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_IS) == SUCCESS) {
11160
881
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL);
11161
881
      } else {
11162
154
        opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, false);
11163
154
        opline->opcode = ZEND_ISSET_ISEMPTY_VAR;
11164
154
      }
11165
1.15k
      break;
11166
3.43k
    case ZEND_AST_DIM:
11167
3.43k
      opline = zend_compile_dim(result, var_ast, BP_VAR_IS, /* by_ref */ false);
11168
3.43k
      opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
11169
3.43k
      break;
11170
858
    case ZEND_AST_PROP:
11171
959
    case ZEND_AST_NULLSAFE_PROP:
11172
959
      opline = zend_compile_prop(result, var_ast, BP_VAR_IS, false);
11173
959
      opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
11174
959
      break;
11175
243
    case ZEND_AST_STATIC_PROP:
11176
243
      opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, false, false);
11177
243
      opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP;
11178
243
      break;
11179
5.78k
    EMPTY_SWITCH_DEFAULT_CASE()
11180
5.78k
  }
11181
11182
5.77k
  result->op_type = opline->result_type = IS_TMP_VAR;
11183
5.77k
  if (!(ast->kind == ZEND_AST_ISSET)) {
11184
750
    opline->extended_value |= ZEND_ISEMPTY;
11185
750
  }
11186
5.77k
}
11187
/* }}} */
11188
11189
static void zend_compile_silence(znode *result, const zend_ast *ast) /* {{{ */
11190
10.4M
{
11191
10.4M
  zend_ast *expr_ast = ast->child[0];
11192
10.4M
  znode silence_node;
11193
11194
10.4M
  zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL);
11195
11196
10.4M
  if (expr_ast->kind == ZEND_AST_VAR) {
11197
    /* For @$var we need to force a FETCH instruction, otherwise the CV access will
11198
     * happen outside the silenced section. */
11199
69.6k
    zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, false );
11200
10.3M
  } else {
11201
10.3M
    zend_compile_expr(result, expr_ast);
11202
10.3M
  }
11203
11204
10.4M
  zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL);
11205
10.4M
}
11206
/* }}} */
11207
11208
static void zend_compile_shell_exec(znode *result, const zend_ast *ast) /* {{{ */
11209
48.9k
{
11210
48.9k
  zend_ast *expr_ast = ast->child[0];
11211
11212
48.9k
  zval fn_name;
11213
48.9k
  zend_ast *name_ast, *args_ast, *call_ast;
11214
11215
48.9k
  zend_error(E_DEPRECATED, "The backtick (`) operator is deprecated, use shell_exec() instead");
11216
11217
48.9k
  ZVAL_STRING(&fn_name, "shell_exec");
11218
48.9k
  name_ast = zend_ast_create_zval(&fn_name);
11219
48.9k
  args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast);
11220
48.9k
  call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast);
11221
11222
48.9k
  zend_compile_expr(result, call_ast);
11223
11224
48.9k
  zval_ptr_dtor(&fn_name);
11225
48.9k
}
11226
/* }}} */
11227
11228
static void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
11229
95.9k
{
11230
95.9k
  zend_ast_list *list = zend_ast_get_list(ast);
11231
95.9k
  zend_op *opline;
11232
95.9k
  uint32_t i, opnum_init = -1;
11233
95.9k
  bool packed = true;
11234
11235
95.9k
  if (zend_try_ct_eval_array(&result->u.constant, ast)) {
11236
55.2k
    result->op_type = IS_CONST;
11237
55.2k
    return;
11238
55.2k
  }
11239
11240
  /* Empty arrays are handled at compile-time */
11241
40.6k
  ZEND_ASSERT(list->children > 0);
11242
11243
142k
  for (i = 0; i < list->children; ++i) {
11244
101k
    zend_ast *elem_ast = list->child[i];
11245
101k
    zend_ast *value_ast, *key_ast;
11246
101k
    bool by_ref;
11247
101k
    znode value_node, key_node, *key_node_ptr = NULL;
11248
11249
101k
    if (elem_ast == NULL) {
11250
0
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
11251
0
    }
11252
11253
101k
    value_ast = elem_ast->child[0];
11254
11255
101k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
11256
1.31k
      zend_compile_expr(&value_node, value_ast);
11257
1.31k
      if (i == 0) {
11258
997
        opnum_init = get_next_op_number();
11259
997
        opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
11260
997
      }
11261
1.31k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL);
11262
1.31k
      SET_NODE(opline->result, result);
11263
1.31k
      continue;
11264
1.31k
    }
11265
11266
100k
    key_ast = elem_ast->child[1];
11267
100k
    by_ref = elem_ast->attr;
11268
11269
100k
    if (key_ast) {
11270
4.79k
      zend_compile_expr(&key_node, key_ast);
11271
4.79k
      zend_handle_numeric_op(&key_node);
11272
4.79k
      key_node_ptr = &key_node;
11273
4.79k
    }
11274
11275
100k
    if (by_ref) {
11276
406
      zend_ensure_writable_variable(value_ast);
11277
406
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
11278
99.7k
    } else {
11279
99.7k
      zend_compile_expr(&value_node, value_ast);
11280
99.7k
    }
11281
11282
100k
    if (i == 0) {
11283
39.4k
      opnum_init = get_next_op_number();
11284
39.4k
      opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr);
11285
39.4k
      opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT;
11286
60.6k
    } else {
11287
60.6k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT,
11288
60.6k
        &value_node, key_node_ptr);
11289
60.6k
      SET_NODE(opline->result, result);
11290
60.6k
    }
11291
100k
    opline->extended_value |= by_ref;
11292
11293
100k
    if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) {
11294
2.08k
      packed = false;
11295
2.08k
    }
11296
100k
  }
11297
11298
  /* Add a flag to INIT_ARRAY if we know this array cannot be packed */
11299
40.5k
  if (!packed) {
11300
1.09k
    ZEND_ASSERT(opnum_init != (uint32_t)-1);
11301
1.09k
    opline = &CG(active_op_array)->opcodes[opnum_init];
11302
1.09k
    opline->extended_value |= ZEND_ARRAY_NOT_PACKED;
11303
1.09k
  }
11304
40.5k
}
11305
/* }}} */
11306
11307
static void zend_compile_const(znode *result, const zend_ast *ast) /* {{{ */
11308
6.54M
{
11309
6.54M
  zend_ast *name_ast = ast->child[0];
11310
11311
6.54M
  zend_op *opline;
11312
11313
6.54M
  bool is_fully_qualified;
11314
6.54M
  zend_string *orig_name = zend_ast_get_str(name_ast);
11315
6.54M
  zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified);
11316
11317
6.54M
  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__"))) {
11318
559
    zend_ast *last = CG(ast);
11319
11320
1.12k
    while (last && last->kind == ZEND_AST_STMT_LIST) {
11321
733
      const zend_ast_list *list = zend_ast_get_list(last);
11322
733
      if (list->children == 0) {
11323
168
        break;
11324
168
      }
11325
565
      last = list->child[list->children-1];
11326
565
    }
11327
559
    if (last && last->kind == ZEND_AST_HALT_COMPILER) {
11328
145
      result->op_type = IS_CONST;
11329
145
      ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
11330
145
      zend_string_release_ex(resolved_name, 0);
11331
145
      return;
11332
145
    }
11333
559
  }
11334
11335
6.54M
  if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
11336
37.2k
    result->op_type = IS_CONST;
11337
37.2k
    zend_string_release_ex(resolved_name, 0);
11338
37.2k
    return;
11339
37.2k
  }
11340
11341
6.50M
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
11342
6.50M
  opline->op2_type = IS_CONST;
11343
11344
6.50M
  if (is_fully_qualified || !FC(current_namespace)) {
11345
134k
    opline->op1.num = 0;
11346
134k
    opline->op2.constant = zend_add_const_name_literal(
11347
134k
      resolved_name, false);
11348
6.37M
  } else {
11349
6.37M
    opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE;
11350
6.37M
    opline->op2.constant = zend_add_const_name_literal(
11351
6.37M
      resolved_name, true);
11352
6.37M
  }
11353
6.50M
  opline->extended_value = zend_alloc_cache_slot();
11354
6.50M
}
11355
/* }}} */
11356
11357
static void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */
11358
88.3k
{
11359
88.3k
  zend_ast *class_ast;
11360
88.3k
  zend_ast *const_ast;
11361
88.3k
  znode class_node, const_node;
11362
88.3k
  zend_op *opline;
11363
11364
88.3k
  zend_eval_const_expr(&ast->child[0]);
11365
88.3k
  zend_eval_const_expr(&ast->child[1]);
11366
11367
88.3k
  class_ast = ast->child[0];
11368
88.3k
  const_ast = ast->child[1];
11369
11370
88.3k
  if (class_ast->kind == ZEND_AST_ZVAL && const_ast->kind == ZEND_AST_ZVAL) {
11371
39.3k
    zval *const_zv = zend_ast_get_zval(const_ast);
11372
39.3k
    if (Z_TYPE_P(const_zv) == IS_STRING) {
11373
38.1k
      zend_string *const_str = Z_STR_P(const_zv);
11374
38.1k
      zend_string *resolved_name = zend_resolve_class_name_ast(class_ast);
11375
38.1k
      if (zend_try_ct_eval_class_const(&result->u.constant, resolved_name, const_str)) {
11376
227
        result->op_type = IS_CONST;
11377
227
        zend_string_release_ex(resolved_name, 0);
11378
227
        return;
11379
227
      }
11380
37.9k
      zend_string_release_ex(resolved_name, 0);
11381
37.9k
    }
11382
39.3k
  }
11383
11384
88.1k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
11385
11386
88.1k
  zend_compile_expr(&const_node, const_ast);
11387
11388
88.1k
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node);
11389
11390
88.1k
  zend_set_class_name_op1(opline, &class_node);
11391
11392
88.1k
  if (opline->op1_type == IS_CONST || opline->op2_type == IS_CONST) {
11393
87.6k
    opline->extended_value = zend_alloc_cache_slots(2);
11394
87.6k
  }
11395
88.1k
}
11396
/* }}} */
11397
11398
static void zend_compile_class_name(znode *result, const zend_ast *ast) /* {{{ */
11399
4.61k
{
11400
4.61k
  zend_ast *class_ast = ast->child[0];
11401
11402
4.61k
  if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) {
11403
2.83k
    result->op_type = IS_CONST;
11404
2.83k
    return;
11405
2.83k
  }
11406
11407
1.77k
  if (class_ast->kind == ZEND_AST_ZVAL) {
11408
560
    zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11409
560
    opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
11410
1.21k
  } else {
11411
1.21k
    znode expr_node;
11412
1.21k
    zend_compile_expr(&expr_node, class_ast);
11413
1.21k
    if (expr_node.op_type == IS_CONST) {
11414
      /* Unlikely case that happen if class_ast is constant folded.
11415
       * Handle it here, to avoid needing a CONST specialization in the VM. */
11416
9
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"::class\" on %s",
11417
9
        zend_zval_value_name(&expr_node.u.constant));
11418
9
    }
11419
11420
1.21k
    zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL);
11421
1.21k
  }
11422
1.77k
}
11423
/* }}} */
11424
11425
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */
11426
263k
{
11427
263k
  if (num == 0) {
11428
30.5k
    result->op_type = IS_TMP_VAR;
11429
30.5k
    result->u.op.var = -1;
11430
30.5k
    opline->opcode = ZEND_ROPE_INIT;
11431
233k
  } else {
11432
233k
    opline->opcode = ZEND_ROPE_ADD;
11433
233k
    SET_NODE(opline->op1, result);
11434
233k
  }
11435
263k
  SET_NODE(opline->op2, elem_node);
11436
263k
  SET_NODE(opline->result, result);
11437
263k
  opline->extended_value = num;
11438
263k
  return opline;
11439
263k
}
11440
/* }}} */
11441
11442
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */
11443
253k
{
11444
253k
  zend_op *opline = get_next_op();
11445
11446
253k
  if (num == 0) {
11447
51.1k
    result->op_type = IS_TMP_VAR;
11448
51.1k
    result->u.op.var = -1;
11449
51.1k
    opline->opcode = ZEND_ROPE_INIT;
11450
202k
  } else {
11451
202k
    opline->opcode = ZEND_ROPE_ADD;
11452
202k
    SET_NODE(opline->op1, result);
11453
202k
  }
11454
253k
  SET_NODE(opline->op2, elem_node);
11455
253k
  SET_NODE(opline->result, result);
11456
253k
  opline->extended_value = num;
11457
253k
  return opline;
11458
253k
}
11459
/* }}} */
11460
11461
static void zend_compile_rope_finalize(znode *result, uint32_t rope_elements, zend_op *init_opline, zend_op *opline)
11462
81.7k
{
11463
81.7k
  if (rope_elements == 1) {
11464
1.86k
    if (opline->op2_type == IS_CONST) {
11465
552
      GET_NODE(result, opline->op2);
11466
552
      ZVAL_UNDEF(CT_CONSTANT(opline->op2));
11467
552
      SET_UNUSED(opline->op2);
11468
552
      MAKE_NOP(opline);
11469
1.31k
    } else {
11470
1.31k
      opline->opcode = ZEND_CAST;
11471
1.31k
      opline->extended_value = IS_STRING;
11472
1.31k
      opline->op1_type = opline->op2_type;
11473
1.31k
      opline->op1 = opline->op2;
11474
1.31k
      SET_UNUSED(opline->op2);
11475
1.31k
      zend_make_tmp_result(result, opline);
11476
1.31k
    }
11477
79.8k
  } else if (rope_elements == 2) {
11478
44.6k
    opline->opcode = ZEND_FAST_CONCAT;
11479
44.6k
    opline->extended_value = 0;
11480
44.6k
    opline->op1_type = init_opline->op2_type;
11481
44.6k
    opline->op1 = init_opline->op2;
11482
44.6k
    zend_make_tmp_result(result, opline);
11483
44.6k
    MAKE_NOP(init_opline);
11484
44.6k
  } else {
11485
35.2k
    uint32_t var;
11486
11487
35.2k
    init_opline->extended_value = rope_elements;
11488
35.2k
    opline->opcode = ZEND_ROPE_END;
11489
35.2k
    zend_make_tmp_result(result, opline);
11490
35.2k
    var = opline->op1.var = get_temporary_variable();
11491
11492
    /* Allocates the necessary number of zval slots to keep the rope */
11493
35.2k
    uint32_t i = ((rope_elements * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
11494
229k
    while (i > 1) {
11495
194k
      get_temporary_variable();
11496
194k
      i--;
11497
194k
    }
11498
11499
    /* Update all the previous opcodes to use the same variable */
11500
528k
    while (opline != init_opline) {
11501
493k
      opline--;
11502
493k
      if (opline->opcode == ZEND_ROPE_ADD &&
11503
357k
          opline->result.var == (uint32_t)-1) {
11504
355k
        opline->op1.var = var;
11505
355k
        opline->result.var = var;
11506
355k
      } else if (opline->opcode == ZEND_ROPE_INIT &&
11507
35.6k
                 opline->result.var == (uint32_t)-1) {
11508
35.2k
        opline->result.var = var;
11509
35.2k
      }
11510
493k
    }
11511
35.2k
  }
11512
81.7k
}
11513
11514
static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
11515
79.1k
{
11516
79.1k
  uint32_t i, j;
11517
79.1k
  uint32_t rope_init_lineno = -1;
11518
79.1k
  zend_op *opline = NULL, *init_opline;
11519
79.1k
  znode elem_node, last_const_node;
11520
79.1k
  zend_ast_list *list = zend_ast_get_list(ast);
11521
79.1k
  uint32_t reserved_op_number = -1;
11522
11523
79.1k
  ZEND_ASSERT(list->children > 0);
11524
11525
79.1k
  j = 0;
11526
79.1k
  last_const_node.op_type = IS_UNUSED;
11527
590k
  for (i = 0; i < list->children; i++) {
11528
510k
    zend_ast *encaps_var = list->child[i];
11529
11530
510k
    if (encaps_var->attr & (ZEND_ENCAPS_VAR_DOLLAR_CURLY|ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11531
44.8k
      if ((encaps_var->kind == ZEND_AST_VAR || encaps_var->kind == ZEND_AST_DIM) && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY)) {
11532
527
        zend_error(E_DEPRECATED, "Using ${var} in strings is deprecated, use {$var} instead");
11533
44.3k
      } else if (encaps_var->kind == ZEND_AST_VAR && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11534
44.3k
        zend_error(E_DEPRECATED, "Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead");
11535
44.3k
      }
11536
44.8k
    }
11537
11538
510k
    zend_compile_expr(&elem_node, encaps_var);
11539
11540
510k
    if (elem_node.op_type == IS_CONST) {
11541
264k
      convert_to_string(&elem_node.u.constant);
11542
11543
264k
      if (Z_STRLEN(elem_node.u.constant) == 0) {
11544
988
        zval_ptr_dtor(&elem_node.u.constant);
11545
263k
      } else if (last_const_node.op_type == IS_CONST) {
11546
0
        concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant);
11547
0
        zval_ptr_dtor(&elem_node.u.constant);
11548
263k
      } else {
11549
263k
        last_const_node.op_type = IS_CONST;
11550
263k
        ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant);
11551
        /* Reserve place for ZEND_ROPE_ADD instruction */
11552
263k
        reserved_op_number = get_next_op_number();
11553
263k
        opline = get_next_op();
11554
263k
        opline->opcode = ZEND_NOP;
11555
263k
      }
11556
264k
      continue;
11557
264k
    } else {
11558
246k
      if (j == 0) {
11559
79.1k
        if (last_const_node.op_type == IS_CONST) {
11560
30.5k
          rope_init_lineno = reserved_op_number;
11561
48.6k
        } else {
11562
48.6k
          rope_init_lineno = get_next_op_number();
11563
48.6k
        }
11564
79.1k
      }
11565
246k
      if (last_const_node.op_type == IS_CONST) {
11566
188k
        opline = &CG(active_op_array)->opcodes[reserved_op_number];
11567
188k
        zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11568
188k
        last_const_node.op_type = IS_UNUSED;
11569
188k
      }
11570
246k
      opline = zend_compile_rope_add(result, j++, &elem_node);
11571
246k
    }
11572
510k
  }
11573
11574
79.1k
  if (j == 0) {
11575
0
    result->op_type = IS_CONST;
11576
0
    if (last_const_node.op_type == IS_CONST) {
11577
0
      ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant);
11578
0
    } else {
11579
0
      ZVAL_EMPTY_STRING(&result->u.constant);
11580
      /* empty string */
11581
0
    }
11582
0
    CG(active_op_array)->last = reserved_op_number - 1;
11583
0
    return;
11584
79.1k
  } else if (last_const_node.op_type == IS_CONST) {
11585
74.9k
    opline = &CG(active_op_array)->opcodes[reserved_op_number];
11586
74.9k
    opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11587
74.9k
  }
11588
79.1k
  init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
11589
79.1k
  zend_compile_rope_finalize(result, j, init_opline, opline);
11590
79.1k
}
11591
/* }}} */
11592
11593
static void zend_compile_magic_const(znode *result, const zend_ast *ast) /* {{{ */
11594
24.0k
{
11595
24.0k
  zend_op *opline;
11596
11597
24.0k
  if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) {
11598
23.1k
    result->op_type = IS_CONST;
11599
23.1k
    return;
11600
23.1k
  }
11601
11602
920
  ZEND_ASSERT(ast->attr == T_CLASS_C &&
11603
920
              CG(active_class_entry) &&
11604
920
              (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
11605
11606
920
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11607
920
  opline->op1.num = ZEND_FETCH_CLASS_SELF;
11608
920
}
11609
/* }}} */
11610
11611
static bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */
11612
37.8k
{
11613
37.8k
  return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP
11614
34.2k
    || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL
11615
33.6k
    || kind == ZEND_AST_AND || kind == ZEND_AST_OR
11616
33.1k
    || kind == ZEND_AST_UNARY_OP
11617
32.4k
    || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS
11618
31.7k
    || kind == ZEND_AST_CAST
11619
31.3k
    || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM
11620
30.1k
    || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM
11621
27.6k
    || kind == ZEND_AST_UNPACK
11622
27.6k
    || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST
11623
10.2k
    || kind == ZEND_AST_CLASS_NAME
11624
10.2k
    || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE
11625
9.51k
    || kind == ZEND_AST_CONST_ENUM_INIT
11626
2.83k
    || kind == ZEND_AST_NEW || kind == ZEND_AST_ARG_LIST
11627
1.33k
    || kind == ZEND_AST_NAMED_ARG
11628
1.28k
    || kind == ZEND_AST_PROP || kind == ZEND_AST_NULLSAFE_PROP
11629
885
    || kind == ZEND_AST_CLOSURE
11630
778
    || kind == ZEND_AST_CALL || kind == ZEND_AST_STATIC_CALL || kind == ZEND_AST_CALLABLE_CONVERT;
11631
37.8k
}
11632
/* }}} */
11633
11634
static void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
11635
2.67k
{
11636
2.67k
  zend_ast *ast = *ast_ptr;
11637
2.67k
  zend_ast *class_ast = ast->child[0];
11638
2.67k
  zend_string *class_name;
11639
2.67k
  int fetch_type;
11640
11641
2.67k
  if (class_ast->kind != ZEND_AST_ZVAL) {
11642
14
    zend_error_noreturn(E_COMPILE_ERROR,
11643
14
      "Dynamic class names are not allowed in compile-time class constant references");
11644
14
  }
11645
2.65k
  if (Z_TYPE_P(zend_ast_get_zval(class_ast)) != IS_STRING) {
11646
9
    zend_throw_error(NULL, "Class name must be a valid object or a string");
11647
9
  }
11648
11649
2.65k
  class_name = zend_ast_get_str(class_ast);
11650
2.65k
  fetch_type = zend_get_class_fetch_type(class_name);
11651
11652
2.65k
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11653
5
    zend_error_noreturn(E_COMPILE_ERROR,
11654
5
      "\"static::\" is not allowed in compile-time constants");
11655
5
  }
11656
11657
2.65k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
11658
2.04k
    zend_string *tmp = zend_resolve_class_name_ast(class_ast);
11659
11660
2.04k
    zend_string_release_ex(class_name, 0);
11661
2.04k
    if (tmp != class_name) {
11662
481
      zval *zv = zend_ast_get_zval(class_ast);
11663
481
      ZVAL_STR(zv, tmp);
11664
481
      class_ast->attr = ZEND_NAME_FQ;
11665
481
    }
11666
2.04k
  }
11667
11668
2.65k
  ast->attr |= ZEND_FETCH_CLASS_EXCEPTION;
11669
2.65k
}
11670
/* }}} */
11671
11672
static void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */
11673
47
{
11674
47
  zend_ast *ast = *ast_ptr;
11675
47
  zend_ast *class_ast = ast->child[0];
11676
47
  if (class_ast->kind != ZEND_AST_ZVAL) {
11677
5
    zend_error_noreturn(E_COMPILE_ERROR,
11678
5
      "(expression)::class cannot be used in constant expressions");
11679
5
  }
11680
11681
42
  zend_string *class_name = zend_ast_get_str(class_ast);
11682
42
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
11683
11684
42
  switch (fetch_type) {
11685
37
    case ZEND_FETCH_CLASS_SELF:
11686
37
    case ZEND_FETCH_CLASS_PARENT:
11687
      /* For the const-eval representation store the fetch type instead of the name. */
11688
37
      zend_string_release(class_name);
11689
37
      ast->child[0] = NULL;
11690
37
      ast->attr = fetch_type;
11691
37
      return;
11692
5
    case ZEND_FETCH_CLASS_STATIC:
11693
5
      zend_error_noreturn(E_COMPILE_ERROR,
11694
5
        "static::class cannot be used for compile-time class name resolution");
11695
42
    EMPTY_SWITCH_DEFAULT_CASE()
11696
42
  }
11697
42
}
11698
11699
static void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */
11700
14.6k
{
11701
14.6k
  zend_ast *ast = *ast_ptr;
11702
14.6k
  zend_ast *name_ast = ast->child[0];
11703
14.6k
  zend_string *orig_name = zend_ast_get_str(name_ast);
11704
14.6k
  bool is_fully_qualified;
11705
14.6k
  zval result;
11706
14.6k
  zend_string *resolved_name;
11707
11708
14.6k
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11709
11710
14.6k
  resolved_name = zend_resolve_const_name(
11711
14.6k
    orig_name, name_ast->attr, &is_fully_qualified);
11712
11713
14.6k
  if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
11714
0
    zend_string_release_ex(resolved_name, 0);
11715
0
    zend_ast_destroy(ast);
11716
0
    *ast_ptr = zend_ast_create_zval(&result);
11717
0
    return;
11718
0
  }
11719
11720
14.6k
  zend_ast_destroy(ast);
11721
14.6k
  *ast_ptr = zend_ast_create_constant(resolved_name,
11722
14.6k
    !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0);
11723
14.6k
}
11724
/* }}} */
11725
11726
static void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
11727
382
{
11728
382
  zend_ast *ast = *ast_ptr;
11729
11730
  /* Other cases already resolved by constant folding */
11731
382
  ZEND_ASSERT(ast->attr == T_CLASS_C);
11732
11733
382
  zend_ast_destroy(ast);
11734
382
  *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS);
11735
382
}
11736
/* }}} */
11737
11738
static void zend_compile_const_expr_class_reference(zend_ast *class_ast)
11739
876
{
11740
876
  if (class_ast->kind == ZEND_AST_CLASS) {
11741
0
    zend_error_noreturn(E_COMPILE_ERROR,
11742
0
      "Cannot use anonymous class in constant expression");
11743
0
  }
11744
876
  if (class_ast->kind != ZEND_AST_ZVAL) {
11745
3
    zend_error_noreturn(E_COMPILE_ERROR,
11746
3
      "Cannot use dynamic class name in constant expression");
11747
3
  }
11748
11749
873
  zend_string *class_name = zend_resolve_class_name_ast(class_ast);
11750
873
  int fetch_type = zend_get_class_fetch_type(class_name);
11751
873
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11752
6
    zend_error_noreturn(E_COMPILE_ERROR,
11753
6
      "\"static\" is not allowed in compile-time constants");
11754
6
  }
11755
11756
867
  zval *class_ast_zv = zend_ast_get_zval(class_ast);
11757
867
  zval_ptr_dtor_nogc(class_ast_zv);
11758
867
  ZVAL_STR(class_ast_zv, class_name);
11759
867
  class_ast->attr = fetch_type << ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT;
11760
867
}
11761
11762
static void zend_compile_const_expr_new(zend_ast **ast_ptr)
11763
750
{
11764
750
  zend_ast *class_ast = (*ast_ptr)->child[0];
11765
750
  zend_compile_const_expr_class_reference(class_ast);
11766
11767
750
  const zend_ast *args_ast = (*ast_ptr)->child[1];
11768
750
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
11769
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
11770
5
  }
11771
750
}
11772
11773
static void zend_compile_const_expr_closure(zend_ast **ast_ptr)
11774
107
{
11775
107
  zend_ast_decl *closure_ast = (zend_ast_decl *) *ast_ptr;
11776
107
  const zend_ast *uses_ast = closure_ast->child[1];
11777
107
  if (!(closure_ast->flags & ZEND_ACC_STATIC)) {
11778
5
    zend_error_noreturn(E_COMPILE_ERROR,
11779
5
      "Closures in constant expressions must be static");
11780
5
  }
11781
102
  if (uses_ast) {
11782
5
    zend_error_noreturn(E_COMPILE_ERROR,
11783
5
      "Cannot use(...) variables in constant expression");
11784
5
  }
11785
11786
97
  znode node;
11787
97
  zend_op_array *op = zend_compile_func_decl(&node, (zend_ast*)closure_ast, FUNC_DECL_LEVEL_CONSTEXPR);
11788
11789
97
  zend_ast_destroy(*ast_ptr);
11790
97
  *ast_ptr = zend_ast_create_op_array(op);
11791
97
}
11792
11793
static void zend_compile_const_expr_fcc(zend_ast **ast_ptr)
11794
396
{
11795
396
  zend_ast **args_ast;
11796
396
  switch ((*ast_ptr)->kind) {
11797
269
    case ZEND_AST_CALL:
11798
269
      args_ast = &(*ast_ptr)->child[1];
11799
269
      break;
11800
127
    case ZEND_AST_STATIC_CALL:
11801
127
      args_ast = &(*ast_ptr)->child[2];
11802
127
      break;
11803
0
    EMPTY_SWITCH_DEFAULT_CASE();
11804
396
  }
11805
396
  if ((*args_ast)->kind != ZEND_AST_CALLABLE_CONVERT) {
11806
32
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11807
32
  }
11808
364
  ZEND_MAP_PTR_NEW(((zend_ast_fcc *)*args_ast)->fptr);
11809
11810
364
  switch ((*ast_ptr)->kind) {
11811
238
    case ZEND_AST_CALL: {
11812
238
      zend_ast *name_ast = (*ast_ptr)->child[0];
11813
238
      if (name_ast->kind != ZEND_AST_ZVAL) {
11814
13
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic function name in constant expression");
11815
13
      }
11816
225
      zval *name_ast_zv = zend_ast_get_zval(name_ast);
11817
225
      if (Z_TYPE_P(name_ast_zv) != IS_STRING) {
11818
5
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal function name");
11819
5
      }
11820
225
      bool is_fully_qualified;
11821
220
      zend_string *name = zend_resolve_function_name(Z_STR_P(name_ast_zv), name_ast->attr, &is_fully_qualified);
11822
220
      zval_ptr_dtor_nogc(name_ast_zv);
11823
220
      ZVAL_STR(name_ast_zv, name);
11824
220
      if (is_fully_qualified) {
11825
42
        name_ast->attr = ZEND_NAME_FQ;
11826
42
      }
11827
220
      break;
11828
225
    }
11829
126
    case ZEND_AST_STATIC_CALL: {
11830
126
      zend_ast *class_ast = (*ast_ptr)->child[0];
11831
126
      zend_compile_const_expr_class_reference(class_ast);
11832
126
      zend_ast *method_ast = (*ast_ptr)->child[1];
11833
126
      if (method_ast->kind != ZEND_AST_ZVAL) {
11834
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic method name in constant expression");
11835
0
      }
11836
126
      if (Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING) {
11837
0
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal method name");
11838
0
      }
11839
126
      break;
11840
126
    }
11841
126
    EMPTY_SWITCH_DEFAULT_CASE();
11842
364
  }
11843
364
}
11844
11845
static void zend_compile_const_expr_args(zend_ast **ast_ptr)
11846
741
{
11847
741
  zend_ast_list *list = zend_ast_get_list(*ast_ptr);
11848
741
  bool uses_named_args = false;
11849
1.12k
  for (uint32_t i = 0; i < list->children; i++) {
11850
383
    const zend_ast *arg = list->child[i];
11851
383
    if (arg->kind == ZEND_AST_UNPACK) {
11852
1
      zend_error_noreturn(E_COMPILE_ERROR,
11853
1
        "Argument unpacking in constant expressions is not supported");
11854
1
    }
11855
382
    if (arg->kind == ZEND_AST_NAMED_ARG) {
11856
49
      uses_named_args = true;
11857
333
    } else if (uses_named_args) {
11858
0
      zend_error_noreturn(E_COMPILE_ERROR,
11859
0
        "Cannot use positional argument after named argument");
11860
0
    }
11861
382
  }
11862
740
  if (uses_named_args) {
11863
48
    list->attr = 1;
11864
48
  }
11865
740
}
11866
11867
typedef struct {
11868
  /* Whether the value of this expression may differ on each evaluation. */
11869
  bool allow_dynamic;
11870
} const_expr_context;
11871
11872
static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */
11873
148k
{
11874
148k
  const const_expr_context *ctx = context;
11875
148k
  zend_ast *ast = *ast_ptr;
11876
148k
  if (ast == NULL || ast->kind == ZEND_AST_ZVAL) {
11877
110k
    return;
11878
110k
  }
11879
11880
37.8k
  if (!zend_is_allowed_in_const_expr(ast->kind)) {
11881
42
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11882
42
  }
11883
11884
37.7k
  switch (ast->kind) {
11885
2.67k
    case ZEND_AST_CLASS_CONST:
11886
2.67k
      zend_compile_const_expr_class_const(ast_ptr);
11887
2.67k
      break;
11888
47
    case ZEND_AST_CLASS_NAME:
11889
47
      zend_compile_const_expr_class_name(ast_ptr);
11890
47
      break;
11891
14.6k
    case ZEND_AST_CONST:
11892
14.6k
      zend_compile_const_expr_const(ast_ptr);
11893
14.6k
      break;
11894
382
    case ZEND_AST_MAGIC_CONST:
11895
382
      zend_compile_const_expr_magic_const(ast_ptr);
11896
382
      break;
11897
349
    case ZEND_AST_CAST:
11898
349
      if (ast->attr == IS_OBJECT && !ctx->allow_dynamic) {
11899
6
        zend_error_noreturn(E_COMPILE_ERROR,
11900
6
          "Object casts are not supported in this context");
11901
6
      }
11902
343
      break;
11903
756
    case ZEND_AST_NEW:
11904
756
      if (!ctx->allow_dynamic) {
11905
6
        zend_error_noreturn(E_COMPILE_ERROR,
11906
6
          "New expressions are not supported in this context");
11907
6
      }
11908
750
      zend_compile_const_expr_new(ast_ptr);
11909
750
      break;
11910
741
    case ZEND_AST_ARG_LIST:
11911
741
      zend_compile_const_expr_args(ast_ptr);
11912
741
      break;
11913
107
    case ZEND_AST_CLOSURE:
11914
107
      zend_compile_const_expr_closure(ast_ptr);
11915
      /* Return, because we do not want to traverse the children. */
11916
107
      return;
11917
269
    case ZEND_AST_CALL:
11918
396
    case ZEND_AST_STATIC_CALL:
11919
396
      zend_compile_const_expr_fcc(ast_ptr);
11920
396
      break;
11921
37.7k
  }
11922
11923
37.5k
  zend_ast_apply(ast, zend_compile_const_expr, context);
11924
37.5k
}
11925
/* }}} */
11926
11927
void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynamic) /* {{{ */
11928
79.3k
{
11929
79.3k
  const_expr_context context;
11930
79.3k
  context.allow_dynamic = allow_dynamic;
11931
11932
79.3k
  zend_eval_const_expr(ast_ptr);
11933
79.3k
  zend_compile_const_expr(ast_ptr, &context);
11934
79.3k
  if ((*ast_ptr)->kind != ZEND_AST_ZVAL) {
11935
    /* Replace with compiled AST zval representation. */
11936
20.4k
    zval ast_zv;
11937
20.4k
    ZVAL_AST(&ast_zv, zend_ast_copy(*ast_ptr));
11938
20.4k
    zend_ast_destroy(*ast_ptr);
11939
20.4k
    *ast_ptr = zend_ast_create_zval(&ast_zv);
11940
20.4k
  }
11941
79.3k
  ZVAL_COPY(result, zend_ast_get_zval(*ast_ptr));
11942
79.3k
}
11943
/* }}} */
11944
11945
/* Same as compile_stmt, but with early binding */
11946
void zend_compile_top_stmt(zend_ast *ast) /* {{{ */
11947
612k
{
11948
612k
  if (!ast) {
11949
81.6k
    return;
11950
81.6k
  }
11951
11952
531k
  if (ast->kind == ZEND_AST_STMT_LIST) {
11953
98.2k
    const zend_ast_list *list = zend_ast_get_list(ast);
11954
98.2k
    uint32_t i;
11955
632k
    for (i = 0; i < list->children; ++i) {
11956
534k
      zend_compile_top_stmt(list->child[i]);
11957
534k
    }
11958
98.2k
    return;
11959
98.2k
  }
11960
11961
433k
  if (ast->kind == ZEND_AST_FUNC_DECL) {
11962
14.3k
    CG(zend_lineno) = ast->lineno;
11963
14.3k
    zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_TOPLEVEL);
11964
14.3k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11965
418k
  } else if (ast->kind == ZEND_AST_CLASS) {
11966
36.9k
    CG(zend_lineno) = ast->lineno;
11967
36.9k
    zend_compile_class_decl(NULL, ast, true);
11968
36.9k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11969
381k
  } else {
11970
381k
    zend_compile_stmt(ast);
11971
381k
  }
11972
433k
  if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) {
11973
423k
    zend_verify_namespace();
11974
423k
  }
11975
433k
}
11976
/* }}} */
11977
11978
static void zend_compile_stmt(zend_ast *ast) /* {{{ */
11979
7.06M
{
11980
7.06M
  if (!ast) {
11981
448k
    return;
11982
448k
  }
11983
11984
6.61M
  CG(zend_lineno) = ast->lineno;
11985
11986
6.61M
  if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) {
11987
0
    zend_do_extended_stmt(NULL);
11988
0
  }
11989
11990
6.61M
  switch (ast->kind) {
11991
1.61M
    case ZEND_AST_STMT_LIST:
11992
1.61M
      zend_compile_stmt_list(ast);
11993
1.61M
      break;
11994
2.01k
    case ZEND_AST_GLOBAL:
11995
2.01k
      zend_compile_global_var(ast);
11996
2.01k
      break;
11997
36.7k
    case ZEND_AST_STATIC:
11998
36.7k
      zend_compile_static_var(ast);
11999
36.7k
      break;
12000
5.77k
    case ZEND_AST_UNSET:
12001
5.77k
      zend_compile_unset(ast);
12002
5.77k
      break;
12003
33.3k
    case ZEND_AST_RETURN:
12004
33.3k
      zend_compile_return(ast);
12005
33.3k
      break;
12006
1.54M
    case ZEND_AST_ECHO:
12007
1.54M
      zend_compile_echo(ast);
12008
1.54M
      break;
12009
820
    case ZEND_AST_BREAK:
12010
1.35k
    case ZEND_AST_CONTINUE:
12011
1.35k
      zend_compile_break_continue(ast);
12012
1.35k
      break;
12013
1.13k
    case ZEND_AST_GOTO:
12014
1.13k
      zend_compile_goto(ast);
12015
1.13k
      break;
12016
2.67k
    case ZEND_AST_LABEL:
12017
2.67k
      zend_compile_label(ast);
12018
2.67k
      break;
12019
2.64k
    case ZEND_AST_WHILE:
12020
2.64k
      zend_compile_while(ast);
12021
2.64k
      break;
12022
784
    case ZEND_AST_DO_WHILE:
12023
784
      zend_compile_do_while(ast);
12024
784
      break;
12025
10.5k
    case ZEND_AST_FOR:
12026
10.5k
      zend_compile_for(ast);
12027
10.5k
      break;
12028
18.0k
    case ZEND_AST_FOREACH:
12029
18.0k
      zend_compile_foreach(ast);
12030
18.0k
      break;
12031
150k
    case ZEND_AST_IF:
12032
150k
      zend_compile_if(ast);
12033
150k
      break;
12034
8.34k
    case ZEND_AST_SWITCH:
12035
8.34k
      zend_compile_switch(ast);
12036
8.34k
      break;
12037
30.9k
    case ZEND_AST_TRY:
12038
30.9k
      zend_compile_try(ast);
12039
30.9k
      break;
12040
5.75k
    case ZEND_AST_DECLARE:
12041
5.75k
      zend_compile_declare(ast);
12042
5.75k
      break;
12043
3.75k
    case ZEND_AST_FUNC_DECL:
12044
101k
    case ZEND_AST_METHOD:
12045
101k
      zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_NESTED);
12046
101k
      break;
12047
6.70k
    case ZEND_AST_ENUM_CASE:
12048
6.70k
      zend_compile_enum_case(ast);
12049
6.70k
      break;
12050
32.8k
    case ZEND_AST_PROP_GROUP:
12051
32.8k
      zend_compile_prop_group(ast);
12052
32.8k
      break;
12053
7.43k
    case ZEND_AST_CLASS_CONST_GROUP:
12054
7.43k
      zend_compile_class_const_group(ast);
12055
7.43k
      break;
12056
39.0k
    case ZEND_AST_USE_TRAIT:
12057
39.0k
      zend_compile_use_trait(ast);
12058
39.0k
      break;
12059
152k
    case ZEND_AST_CLASS:
12060
152k
      zend_compile_class_decl(NULL, ast, false);
12061
152k
      break;
12062
259
    case ZEND_AST_GROUP_USE:
12063
259
      zend_compile_group_use(ast);
12064
259
      break;
12065
1.42k
    case ZEND_AST_USE:
12066
1.42k
      zend_compile_use(ast);
12067
1.42k
      break;
12068
3.31k
    case ZEND_AST_CONST_DECL:
12069
3.31k
      zend_compile_const_decl(ast);
12070
3.31k
      break;
12071
4.20k
    case ZEND_AST_NAMESPACE:
12072
4.20k
      zend_compile_namespace(ast);
12073
4.20k
      break;
12074
55
    case ZEND_AST_HALT_COMPILER:
12075
55
      zend_compile_halt_compiler(ast);
12076
55
      break;
12077
2.54k
    case ZEND_AST_THROW:
12078
2.54k
      zend_compile_expr(NULL, ast);
12079
2.54k
      break;
12080
294
    case ZEND_AST_CAST_VOID:
12081
294
      zend_compile_void_cast(NULL, ast);
12082
294
      break;
12083
135k
    case ZEND_AST_ASSIGN: {
12084
135k
      znode result;
12085
135k
      zend_compile_assign(&result, ast, /* stmt */ true, BP_VAR_R);
12086
135k
      zend_do_free(&result);
12087
135k
      return;
12088
3.75k
    }
12089
5.43k
    case ZEND_AST_ASSIGN_REF:
12090
5.43k
      zend_compile_assign_ref(NULL, ast, BP_VAR_R);
12091
5.43k
      return;
12092
2.64M
    default:
12093
2.64M
    {
12094
2.64M
      znode result;
12095
2.64M
      zend_compile_expr(&result, ast);
12096
2.64M
      zend_do_free(&result);
12097
2.64M
    }
12098
6.61M
  }
12099
12100
6.46M
  if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) {
12101
34.3k
    zend_emit_tick();
12102
34.3k
  }
12103
6.46M
}
12104
/* }}} */
12105
12106
static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */
12107
31.9M
{
12108
  /* CG(zend_lineno) = ast->lineno; */
12109
31.9M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
12110
12111
31.9M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
12112
107k
    zend_compile_memoized_expr(result, ast, BP_VAR_R);
12113
107k
    return;
12114
107k
  }
12115
12116
31.8M
  switch (ast->kind) {
12117
3.85M
    case ZEND_AST_ZVAL:
12118
3.85M
      ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast));
12119
3.85M
      result->op_type = IS_CONST;
12120
3.85M
      return;
12121
225k
    case ZEND_AST_ZNODE:
12122
225k
      *result = *zend_ast_get_znode(ast);
12123
225k
      return;
12124
453k
    case ZEND_AST_VAR:
12125
498k
    case ZEND_AST_DIM:
12126
521k
    case ZEND_AST_PROP:
12127
579k
    case ZEND_AST_NULLSAFE_PROP:
12128
583k
    case ZEND_AST_STATIC_PROP:
12129
3.14M
    case ZEND_AST_CALL:
12130
3.17M
    case ZEND_AST_METHOD_CALL:
12131
3.17M
    case ZEND_AST_NULLSAFE_METHOD_CALL:
12132
3.20M
    case ZEND_AST_STATIC_CALL:
12133
3.20M
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
12134
3.31M
    case ZEND_AST_PIPE:
12135
3.31M
      zend_compile_var(result, ast, BP_VAR_R, false);
12136
3.31M
      return;
12137
227k
    case ZEND_AST_ASSIGN:
12138
227k
      zend_compile_assign(result, ast, /* stmt */ false, BP_VAR_R);
12139
227k
      return;
12140
772
    case ZEND_AST_ASSIGN_REF:
12141
772
      zend_compile_assign_ref(result, ast, BP_VAR_R);
12142
772
      return;
12143
75.7k
    case ZEND_AST_NEW:
12144
75.7k
      zend_compile_new(result, ast);
12145
75.7k
      return;
12146
104k
    case ZEND_AST_ASSIGN_OP:
12147
104k
      zend_compile_compound_assign(result, ast);
12148
104k
      return;
12149
2.67M
    case ZEND_AST_BINARY_OP:
12150
2.67M
      zend_compile_binary_op(result, ast);
12151
2.67M
      return;
12152
193k
    case ZEND_AST_GREATER:
12153
228k
    case ZEND_AST_GREATER_EQUAL:
12154
228k
      zend_compile_greater(result, ast);
12155
228k
      return;
12156
1.14M
    case ZEND_AST_UNARY_OP:
12157
1.14M
      zend_compile_unary_op(result, ast);
12158
1.14M
      return;
12159
21.2k
    case ZEND_AST_UNARY_PLUS:
12160
172k
    case ZEND_AST_UNARY_MINUS:
12161
172k
      zend_compile_unary_pm(result, ast);
12162
172k
      return;
12163
6.39k
    case ZEND_AST_AND:
12164
10.8k
    case ZEND_AST_OR:
12165
10.8k
      zend_compile_short_circuiting(result, ast);
12166
10.8k
      return;
12167
6.40k
    case ZEND_AST_POST_INC:
12168
10.1k
    case ZEND_AST_POST_DEC:
12169
10.1k
      zend_compile_post_incdec(result, ast);
12170
10.1k
      return;
12171
1.70k
    case ZEND_AST_PRE_INC:
12172
2.97k
    case ZEND_AST_PRE_DEC:
12173
2.97k
      zend_compile_pre_incdec(result, ast);
12174
2.97k
      return;
12175
2.65k
    case ZEND_AST_CAST:
12176
2.65k
      zend_compile_cast(result, ast);
12177
2.65k
      return;
12178
6.97k
    case ZEND_AST_CONDITIONAL:
12179
6.97k
      zend_compile_conditional(result, ast);
12180
6.97k
      return;
12181
1.26M
    case ZEND_AST_COALESCE:
12182
1.26M
      zend_compile_coalesce(result, ast);
12183
1.26M
      return;
12184
22.7k
    case ZEND_AST_ASSIGN_COALESCE:
12185
22.7k
      zend_compile_assign_coalesce(result, ast);
12186
22.7k
      return;
12187
3.90k
    case ZEND_AST_PRINT:
12188
3.90k
      zend_compile_print(result, ast);
12189
3.90k
      return;
12190
135k
    case ZEND_AST_YIELD:
12191
135k
      zend_compile_yield(result, ast);
12192
135k
      return;
12193
1.79k
    case ZEND_AST_YIELD_FROM:
12194
1.79k
      zend_compile_yield_from(result, ast);
12195
1.79k
      return;
12196
1.08k
    case ZEND_AST_INSTANCEOF:
12197
1.08k
      zend_compile_instanceof(result, ast);
12198
1.08k
      return;
12199
8.84k
    case ZEND_AST_INCLUDE_OR_EVAL:
12200
8.84k
      zend_compile_include_or_eval(result, ast);
12201
8.84k
      return;
12202
13.2k
    case ZEND_AST_ISSET:
12203
14.1k
    case ZEND_AST_EMPTY:
12204
14.1k
      zend_compile_isset_or_empty(result, ast);
12205
14.1k
      return;
12206
10.4M
    case ZEND_AST_SILENCE:
12207
10.4M
      zend_compile_silence(result, ast);
12208
10.4M
      return;
12209
48.9k
    case ZEND_AST_SHELL_EXEC:
12210
48.9k
      zend_compile_shell_exec(result, ast);
12211
48.9k
      return;
12212
95.9k
    case ZEND_AST_ARRAY:
12213
95.9k
      zend_compile_array(result, ast);
12214
95.9k
      return;
12215
6.54M
    case ZEND_AST_CONST:
12216
6.54M
      zend_compile_const(result, ast);
12217
6.54M
      return;
12218
88.3k
    case ZEND_AST_CLASS_CONST:
12219
88.3k
      zend_compile_class_const(result, ast);
12220
88.3k
      return;
12221
4.61k
    case ZEND_AST_CLASS_NAME:
12222
4.61k
      zend_compile_class_name(result, ast);
12223
4.61k
      return;
12224
79.1k
    case ZEND_AST_ENCAPS_LIST:
12225
79.1k
      zend_compile_encaps_list(result, ast);
12226
79.1k
      return;
12227
24.0k
    case ZEND_AST_MAGIC_CONST:
12228
24.0k
      zend_compile_magic_const(result, ast);
12229
24.0k
      return;
12230
1.00M
    case ZEND_AST_CLOSURE:
12231
1.01M
    case ZEND_AST_ARROW_FUNC:
12232
1.01M
      zend_compile_func_decl(result, ast, FUNC_DECL_LEVEL_NESTED);
12233
1.01M
      return;
12234
3.26k
    case ZEND_AST_THROW:
12235
3.26k
      zend_compile_throw(result, ast);
12236
3.26k
      return;
12237
2.29k
    case ZEND_AST_MATCH:
12238
2.29k
      zend_compile_match(result, ast);
12239
2.29k
      return;
12240
0
    default:
12241
0
      ZEND_ASSERT(0 /* not supported */);
12242
31.8M
  }
12243
31.8M
}
12244
/* }}} */
12245
12246
static void zend_compile_expr(znode *result, zend_ast *ast)
12247
31.9M
{
12248
31.9M
  zend_check_stack_limit();
12249
12250
31.9M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12251
31.9M
  zend_compile_expr_inner(result, ast);
12252
31.9M
  zend_short_circuiting_commit(checkpoint, result, ast);
12253
31.9M
#if ZEND_DEBUG
12254
31.9M
  if (result) {
12255
    /* BP_VAR_R is not allowed to produce IS_VAR. */
12256
31.9M
    ZEND_ASSERT(result->op_type != IS_VAR);
12257
31.9M
  }
12258
31.9M
#endif
12259
31.9M
}
12260
12261
static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
12262
5.11M
{
12263
5.11M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
12264
12265
5.11M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
12266
74.2k
    switch (ast->kind) {
12267
5.04k
      case ZEND_AST_CALL:
12268
27.7k
      case ZEND_AST_METHOD_CALL:
12269
27.7k
      case ZEND_AST_NULLSAFE_METHOD_CALL:
12270
28.9k
      case ZEND_AST_STATIC_CALL:
12271
28.9k
        zend_compile_memoized_expr(result, ast, BP_VAR_W);
12272
        /* This might not actually produce an opcode, e.g. for expressions evaluated at comptime. */
12273
28.9k
        return NULL;
12274
74.2k
    }
12275
74.2k
  }
12276
12277
5.08M
  switch (ast->kind) {
12278
542k
    case ZEND_AST_VAR:
12279
542k
      return zend_compile_simple_var(result, ast, type, false);
12280
133k
    case ZEND_AST_DIM:
12281
133k
      return zend_compile_dim(result, ast, type, by_ref);
12282
30.7k
    case ZEND_AST_PROP:
12283
88.4k
    case ZEND_AST_NULLSAFE_PROP:
12284
88.4k
      return zend_compile_prop(result, ast, type, by_ref);
12285
7.69k
    case ZEND_AST_STATIC_PROP:
12286
7.69k
      return zend_compile_static_prop(result, ast, type, by_ref, false);
12287
2.78M
    case ZEND_AST_CALL:
12288
2.78M
      zend_compile_call(result, ast, type);
12289
2.78M
      return NULL;
12290
0
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
12291
0
      zend_compile_parent_property_hook_call(result, ast, type);
12292
0
      return NULL;
12293
56.5k
    case ZEND_AST_METHOD_CALL:
12294
58.9k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
12295
58.9k
      zend_compile_method_call(result, ast, type);
12296
58.9k
      return NULL;
12297
34.4k
    case ZEND_AST_STATIC_CALL:
12298
34.4k
      zend_compile_static_call(result, ast, type);
12299
34.4k
      return NULL;
12300
110k
    case ZEND_AST_PIPE:
12301
110k
      zend_compile_pipe(result, ast, type);
12302
110k
      return NULL;
12303
1.87k
    case ZEND_AST_ZNODE:
12304
1.87k
      *result = *zend_ast_get_znode(ast);
12305
1.87k
      return NULL;
12306
534
    case ZEND_AST_ASSIGN_REF:
12307
534
      zend_compile_assign_ref(result, ast, type);
12308
534
      return NULL;
12309
19
    case ZEND_AST_ASSIGN:
12310
19
      zend_compile_assign(result, ast, false, type);
12311
19
      return NULL;
12312
1.32M
    default:
12313
1.32M
      if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
12314
192
        zend_error_noreturn(E_COMPILE_ERROR,
12315
192
          "Cannot use temporary expression in write context");
12316
192
      }
12317
12318
1.32M
      zend_compile_expr(result, ast);
12319
1.32M
      return NULL;
12320
5.08M
  }
12321
5.08M
}
12322
12323
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12324
5.11M
{
12325
5.11M
  zend_check_stack_limit();
12326
12327
5.11M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12328
5.11M
  zend_op *opcode = zend_compile_var_inner(result, ast, type, by_ref);
12329
5.11M
  zend_short_circuiting_commit(checkpoint, result, ast);
12330
5.11M
#if ZEND_DEBUG
12331
5.11M
  if (result
12332
5.10M
   && (type == BP_VAR_R || type == BP_VAR_IS)
12333
   /* Don't check memoized result, as it will force BP_VAR_W even for BP_VAR_IS. */
12334
4.90M
   && CG(memoize_mode) == ZEND_MEMOIZE_NONE
12335
5.11M
  ) {
12336
    /* BP_VAR_{R,IS} is not allowed to produce IS_VAR. */
12337
4.86M
    ZEND_ASSERT(result->op_type != IS_VAR);
12338
4.86M
  }
12339
5.11M
#endif
12340
5.11M
  return opcode;
12341
5.11M
}
12342
12343
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12344
868k
{
12345
868k
  zend_check_stack_limit();
12346
12347
868k
  switch (ast->kind) {
12348
582k
    case ZEND_AST_VAR:
12349
582k
      return zend_compile_simple_var(result, ast, type, true);
12350
158k
    case ZEND_AST_DIM:
12351
158k
      return zend_delayed_compile_dim(result, ast, type, by_ref);
12352
19.1k
    case ZEND_AST_PROP:
12353
22.4k
    case ZEND_AST_NULLSAFE_PROP:
12354
22.4k
    {
12355
22.4k
      zend_op *opline = zend_delayed_compile_prop(result, ast, type);
12356
22.4k
      if (by_ref) {
12357
870
        opline->extended_value |= ZEND_FETCH_REF;
12358
870
      }
12359
22.4k
      return opline;
12360
19.1k
    }
12361
5.94k
    case ZEND_AST_STATIC_PROP:
12362
5.94k
      return zend_compile_static_prop(result, ast, type, by_ref, true);
12363
99.0k
    default:
12364
99.0k
      return zend_compile_var(result, ast, type, false);
12365
868k
  }
12366
868k
}
12367
/* }}} */
12368
12369
bool zend_try_ct_eval_cast(zval *result, uint32_t type, zval *op1)
12370
15.6k
{
12371
  /* NAN warns when casting */
12372
15.6k
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))) {
12373
0
    return false;
12374
0
  }
12375
15.6k
  switch (type) {
12376
49
    case _IS_BOOL:
12377
49
      ZVAL_BOOL(result, zend_is_true(op1));
12378
49
      return true;
12379
333
    case IS_LONG:
12380
333
      if (Z_TYPE_P(op1) == IS_DOUBLE && !ZEND_DOUBLE_FITS_LONG(Z_DVAL_P((op1)))) {
12381
23
        return false;
12382
23
      }
12383
310
      ZVAL_LONG(result, zval_get_long(op1));
12384
310
      return true;
12385
252
    case IS_DOUBLE:
12386
252
      ZVAL_DOUBLE(result, zval_get_double(op1));
12387
252
      return true;
12388
14.2k
    case IS_STRING:
12389
      /* Conversion from double to string takes into account run-time
12390
         'precision' setting and cannot be evaluated at compile-time */
12391
14.2k
      if (Z_TYPE_P(op1) != IS_ARRAY && Z_TYPE_P(op1) != IS_DOUBLE) {
12392
13.3k
        ZVAL_STR(result, zval_get_string(op1));
12393
13.3k
        return true;
12394
13.3k
      }
12395
919
      break;
12396
919
    case IS_ARRAY:
12397
99
      ZVAL_COPY(result, op1);
12398
99
      convert_to_array(result);
12399
99
      return true;
12400
15.6k
  }
12401
1.54k
  return false;
12402
15.6k
}
12403
12404
static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
12405
8.19M
{
12406
8.19M
  zend_ast *ast = *ast_ptr;
12407
8.19M
  zval result;
12408
12409
8.19M
  if (!ast) {
12410
2.23M
    return;
12411
2.23M
  }
12412
12413
5.95M
  zend_check_stack_limit();
12414
12415
5.95M
  switch (ast->kind) {
12416
57.3k
    case ZEND_AST_BINARY_OP:
12417
57.3k
      zend_eval_const_expr(&ast->child[0]);
12418
57.3k
      zend_eval_const_expr(&ast->child[1]);
12419
57.3k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12420
25.9k
        return;
12421
25.9k
      }
12422
12423
31.4k
      if (!zend_try_ct_eval_binary_op(&result, ast->attr,
12424
31.4k
          zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
12425
31.4k
      ) {
12426
17.9k
        return;
12427
17.9k
      }
12428
13.5k
      break;
12429
13.5k
    case ZEND_AST_GREATER:
12430
3.80k
    case ZEND_AST_GREATER_EQUAL:
12431
3.80k
      zend_eval_const_expr(&ast->child[0]);
12432
3.80k
      zend_eval_const_expr(&ast->child[1]);
12433
3.80k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12434
2.79k
        return;
12435
2.79k
      }
12436
12437
1.01k
      zend_ct_eval_greater(&result, ast->kind,
12438
1.01k
        zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]));
12439
1.01k
      break;
12440
1.31k
    case ZEND_AST_AND:
12441
2.25k
    case ZEND_AST_OR:
12442
2.25k
    {
12443
2.25k
      bool child0_is_true, child1_is_true;
12444
2.25k
      zend_eval_const_expr(&ast->child[0]);
12445
2.25k
      zend_eval_const_expr(&ast->child[1]);
12446
2.25k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12447
1.12k
        return;
12448
1.12k
      }
12449
12450
1.13k
      child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0]));
12451
1.13k
      if (child0_is_true == (ast->kind == ZEND_AST_OR)) {
12452
251
        ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR);
12453
251
        break;
12454
251
      }
12455
12456
881
      if (ast->child[1]->kind != ZEND_AST_ZVAL) {
12457
162
        return;
12458
162
      }
12459
12460
719
      child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1]));
12461
719
      if (ast->kind == ZEND_AST_OR) {
12462
42
        ZVAL_BOOL(&result, child0_is_true || child1_is_true);
12463
677
      } else {
12464
677
        ZVAL_BOOL(&result, child0_is_true && child1_is_true);
12465
677
      }
12466
719
      break;
12467
881
    }
12468
5.60k
    case ZEND_AST_UNARY_OP:
12469
5.60k
      zend_eval_const_expr(&ast->child[0]);
12470
5.60k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12471
1.98k
        return;
12472
1.98k
      }
12473
12474
3.62k
      if (!zend_try_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12475
295
        return;
12476
295
      }
12477
3.32k
      break;
12478
3.42k
    case ZEND_AST_UNARY_PLUS:
12479
18.5k
    case ZEND_AST_UNARY_MINUS:
12480
18.5k
      zend_eval_const_expr(&ast->child[0]);
12481
18.5k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12482
2.40k
        return;
12483
2.40k
      }
12484
12485
16.1k
      if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) {
12486
3.38k
        return;
12487
3.38k
      }
12488
12.7k
      break;
12489
22.4k
    case ZEND_AST_COALESCE:
12490
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12491
22.4k
      if (ast->child[0]->kind == ZEND_AST_DIM) {
12492
489
        ast->child[0]->attr |= ZEND_DIM_IS;
12493
489
      }
12494
22.4k
      zend_eval_const_expr(&ast->child[0]);
12495
12496
22.4k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12497
        /* ensure everything was compile-time evaluated at least once */
12498
22.2k
        zend_eval_const_expr(&ast->child[1]);
12499
22.2k
        return;
12500
22.2k
      }
12501
12502
208
      if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) {
12503
161
        zend_eval_const_expr(&ast->child[1]);
12504
161
        *ast_ptr = ast->child[1];
12505
161
        ast->child[1] = NULL;
12506
161
        zend_ast_destroy(ast);
12507
161
      } else {
12508
47
        *ast_ptr = ast->child[0];
12509
47
        ast->child[0] = NULL;
12510
47
        zend_ast_destroy(ast);
12511
47
      }
12512
208
      return;
12513
2.09k
    case ZEND_AST_CONDITIONAL:
12514
2.09k
    {
12515
2.09k
      zend_ast **child, *child_ast;
12516
2.09k
      zend_eval_const_expr(&ast->child[0]);
12517
2.09k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12518
        /* ensure everything was compile-time evaluated at least once */
12519
1.80k
        if (ast->child[1]) {
12520
1.31k
          zend_eval_const_expr(&ast->child[1]);
12521
1.31k
        }
12522
1.80k
        zend_eval_const_expr(&ast->child[2]);
12523
1.80k
        return;
12524
1.80k
      }
12525
12526
283
      child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];
12527
283
      if (*child == NULL) {
12528
228
        child--;
12529
228
      }
12530
283
      child_ast = *child;
12531
283
      *child = NULL;
12532
283
      zend_ast_destroy(ast);
12533
283
      *ast_ptr = child_ast;
12534
283
      zend_eval_const_expr(ast_ptr);
12535
283
      return;
12536
2.09k
    }
12537
904k
    case ZEND_AST_DIM:
12538
904k
    {
12539
      /* constant expression should be always read context ... */
12540
904k
      const zval *container, *dim;
12541
12542
904k
      if (ast->child[1] == NULL) {
12543
38
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
12544
38
      }
12545
12546
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12547
904k
      if ((ast->attr & ZEND_DIM_IS) && ast->child[0]->kind == ZEND_AST_DIM) {
12548
1.38k
        ast->child[0]->attr |= ZEND_DIM_IS;
12549
1.38k
      }
12550
12551
904k
      zend_eval_const_expr(&ast->child[0]);
12552
904k
      zend_eval_const_expr(&ast->child[1]);
12553
904k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12554
891k
        return;
12555
891k
      }
12556
12557
12.2k
      container = zend_ast_get_zval(ast->child[0]);
12558
12.2k
      dim = zend_ast_get_zval(ast->child[1]);
12559
12560
12.2k
      if (Z_TYPE_P(container) == IS_ARRAY) {
12561
7.61k
        zval *el;
12562
7.61k
        if (Z_TYPE_P(dim) == IS_LONG) {
12563
728
          el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim));
12564
728
          if (el) {
12565
355
            ZVAL_COPY(&result, el);
12566
373
          } else {
12567
373
            return;
12568
373
          }
12569
6.88k
        } else if (Z_TYPE_P(dim) == IS_STRING) {
12570
6.71k
          el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim));
12571
6.71k
          if (el) {
12572
253
            ZVAL_COPY(&result, el);
12573
6.46k
          } else {
12574
6.46k
            return;
12575
6.46k
          }
12576
6.71k
        } else {
12577
173
          return; /* warning... handle at runtime */
12578
173
        }
12579
7.61k
      } else if (Z_TYPE_P(container) == IS_STRING) {
12580
4.33k
        zend_long offset;
12581
4.33k
        uint8_t c;
12582
4.33k
        if (Z_TYPE_P(dim) == IS_LONG) {
12583
3.76k
          offset = Z_LVAL_P(dim);
12584
3.76k
        } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) {
12585
415
          return;
12586
415
        }
12587
3.92k
        if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
12588
3.82k
          return;
12589
3.82k
        }
12590
97
        c = (uint8_t) Z_STRVAL_P(container)[offset];
12591
97
        ZVAL_CHAR(&result, c);
12592
280
      } else if (Z_TYPE_P(container) <= IS_FALSE) {
12593
8
        return; /* warning... handle at runtime */
12594
272
      } else {
12595
272
        return;
12596
272
      }
12597
705
      break;
12598
12.2k
    }
12599
1.99M
    case ZEND_AST_ARRAY:
12600
1.99M
      if (!zend_try_ct_eval_array(&result, ast)) {
12601
1.96M
        return;
12602
1.96M
      }
12603
28.7k
      break;
12604
28.7k
    case ZEND_AST_MAGIC_CONST:
12605
1.30k
      if (!zend_try_ct_eval_magic_const(&result, ast)) {
12606
382
        return;
12607
382
      }
12608
921
      break;
12609
69.8k
    case ZEND_AST_CONST:
12610
69.8k
    {
12611
69.8k
      zend_ast *name_ast = ast->child[0];
12612
69.8k
      bool is_fully_qualified;
12613
69.8k
      zend_string *resolved_name = zend_resolve_const_name(
12614
69.8k
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
12615
12616
69.8k
      if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
12617
64.9k
        zend_string_release_ex(resolved_name, 0);
12618
64.9k
        return;
12619
64.9k
      }
12620
12621
4.90k
      zend_string_release_ex(resolved_name, 0);
12622
4.90k
      break;
12623
69.8k
    }
12624
565k
    case ZEND_AST_CLASS_CONST:
12625
565k
    {
12626
565k
      zend_ast *class_ast;
12627
565k
      zend_ast *name_ast;
12628
565k
      zend_string *resolved_name;
12629
12630
565k
      zend_eval_const_expr(&ast->child[0]);
12631
565k
      zend_eval_const_expr(&ast->child[1]);
12632
12633
565k
      if (UNEXPECTED(ast->child[1]->kind != ZEND_AST_ZVAL
12634
565k
        || Z_TYPE_P(zend_ast_get_zval(ast->child[1])) != IS_STRING)) {
12635
846
        return;
12636
846
      }
12637
12638
564k
      class_ast = ast->child[0];
12639
564k
      name_ast = ast->child[1];
12640
12641
564k
      if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) {
12642
522k
        return;
12643
522k
      }
12644
12645
42.1k
      resolved_name = zend_resolve_class_name_ast(class_ast);
12646
42.1k
      if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) {
12647
41.9k
        zend_string_release_ex(resolved_name, 0);
12648
41.9k
        return;
12649
41.9k
      }
12650
12651
185
      zend_string_release_ex(resolved_name, 0);
12652
185
      break;
12653
42.1k
    }
12654
2.95k
    case ZEND_AST_CLASS_NAME:
12655
2.95k
    {
12656
2.95k
      zend_ast *class_ast = ast->child[0];
12657
2.95k
      if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) {
12658
1.15k
        return;
12659
1.15k
      }
12660
1.79k
      break;
12661
2.95k
    }
12662
    // TODO: We should probably use zend_ast_apply to recursively walk nodes without
12663
    // special handling. It is required that all nodes that are part of a const expr
12664
    // are visited. Probably we should be distinguishing evaluation of const expr and
12665
    // normal exprs here.
12666
3.16k
    case ZEND_AST_ARG_LIST:
12667
3.16k
    {
12668
3.16k
      zend_ast_list *list = zend_ast_get_list(ast);
12669
4.20k
      for (uint32_t i = 0; i < list->children; i++) {
12670
1.04k
        zend_eval_const_expr(&list->child[i]);
12671
1.04k
      }
12672
3.16k
      return;
12673
2.95k
    }
12674
3.16k
    case ZEND_AST_NEW:
12675
3.16k
      zend_eval_const_expr(&ast->child[0]);
12676
3.16k
      zend_eval_const_expr(&ast->child[1]);
12677
3.16k
      return;
12678
153
    case ZEND_AST_NAMED_ARG:
12679
153
      zend_eval_const_expr(&ast->child[1]);
12680
153
      return;
12681
6.68k
    case ZEND_AST_CONST_ENUM_INIT:
12682
6.68k
      zend_eval_const_expr(&ast->child[3]);
12683
6.68k
      return;
12684
480
    case ZEND_AST_PROP:
12685
738
    case ZEND_AST_NULLSAFE_PROP:
12686
738
      zend_eval_const_expr(&ast->child[0]);
12687
738
      zend_eval_const_expr(&ast->child[1]);
12688
738
      return;
12689
3.52k
    case ZEND_AST_CAST:
12690
3.52k
      if (ast->attr == IS_NULL) {
12691
5
        zend_error_noreturn(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
12692
5
      }
12693
3.51k
      zend_eval_const_expr(&ast->child[0]);
12694
3.51k
      if (ast->child[0]->kind == ZEND_AST_ZVAL
12695
1.42k
       && zend_try_ct_eval_cast(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12696
792
        break;
12697
792
      }
12698
2.72k
      return;
12699
2.29M
    default:
12700
2.29M
      return;
12701
5.95M
  }
12702
12703
69.0k
  zend_ast_destroy(ast);
12704
69.0k
  *ast_ptr = zend_ast_create_zval(&result);
12705
69.0k
}
12706
/* }}} */