Coverage Report

Created: 2026-10-28 06:20

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 © Zend Technologies Ltd., a subsidiary company of          |
6
   |     Perforce Software, Inc., and Contributors.                       |
7
   +----------------------------------------------------------------------+
8
   | This source file is subject to the Modified BSD License that is      |
9
   | bundled with this package in the file LICENSE, and is available      |
10
   | through the World Wide Web at <https://www.php.net/license/>.        |
11
   |                                                                      |
12
   | SPDX-License-Identifier: BSD-3-Clause                                |
13
   +----------------------------------------------------------------------+
14
   | Authors: Andi Gutmans <andi@php.net>                                 |
15
   |          Zeev Suraski <zeev@php.net>                                 |
16
   |          Nikita Popov <nikic@php.net>                                |
17
   +----------------------------------------------------------------------+
18
*/
19
20
#include <zend_language_parser.h>
21
#include "zend.h"
22
#include "zend_ast.h"
23
#include "zend_attributes.h"
24
#include "zend_compile.h"
25
#include "zend_constants.h"
26
#include "zend_llist.h"
27
#include "zend_API.h"
28
#include "zend_exceptions.h"
29
#include "zend_interfaces.h"
30
#include "zend_types.h"
31
#include "zend_portability.h"
32
#include "zend_string.h"
33
#include "zend_virtual_cwd.h"
34
#include "zend_multibyte.h"
35
#include "zend_language_scanner.h"
36
#include "zend_inheritance.h"
37
#include "zend_vm.h"
38
#include "zend_enum.h"
39
#include "zend_observer.h"
40
#include "zend_call_stack.h"
41
#include "zend_frameless_function.h"
42
#include "zend_property_hooks.h"
43
#include "zend_partial.h"
44
45
68.6M
#define SET_NODE(target, src) do { \
46
68.6M
    target ## _type = (src)->op_type; \
47
68.6M
    if ((src)->op_type == IS_CONST) { \
48
10.1M
      target.constant = zend_add_literal(&(src)->u.constant); \
49
58.4M
    } else { \
50
58.4M
      target = (src)->u.op; \
51
58.4M
    } \
52
68.6M
  } while (0)
53
54
51.0M
#define GET_NODE(target, src) do { \
55
51.0M
    (target)->op_type = src ## _type; \
56
51.0M
    if ((target)->op_type == IS_CONST) { \
57
682
      ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
58
51.0M
    } else { \
59
51.0M
      (target)->u.op = src; \
60
51.0M
    } \
61
51.0M
  } while (0)
62
63
107M
#define FC(member) (CG(file_context).member)
64
65
typedef struct _zend_loop_var {
66
  uint8_t opcode;
67
  uint8_t var_type;
68
  uint32_t   var_num;
69
  uint32_t   try_catch_offset;
70
} zend_loop_var;
71
72
18.4M
static inline uint32_t zend_alloc_cache_slots(unsigned count) {
73
18.4M
  if (count == 0) {
74
    /* Even if no cache slots are desired, the VM handler may still want to acquire
75
     * CACHE_ADDR() unconditionally. Returning zero makes sure that the address
76
     * calculation is still legal and ubsan does not complain. */
77
0
    return 0;
78
0
  }
79
80
18.4M
  zend_op_array *op_array = CG(active_op_array);
81
18.4M
  uint32_t ret = op_array->cache_size;
82
18.4M
  op_array->cache_size += count * sizeof(void*);
83
18.4M
  return ret;
84
18.4M
}
85
86
17.0M
static inline uint32_t zend_alloc_cache_slot(void) {
87
17.0M
  return zend_alloc_cache_slots(1);
88
17.0M
}
89
90
ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
91
ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename, zend_compile_position position);
92
93
#ifndef ZTS
94
ZEND_API zend_compiler_globals compiler_globals;
95
ZEND_API zend_executor_globals executor_globals;
96
#endif
97
98
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2);
99
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast);
100
static void zend_eval_const_expr(zend_ast **ast_ptr);
101
102
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
103
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
104
static void zend_compile_expr(znode *result, zend_ast *ast);
105
static void zend_compile_stmt(zend_ast *ast);
106
static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type);
107
108
static zend_ast *zend_partial_apply(zend_ast *callable_ast, zend_ast *pipe_arg);
109
110
#ifdef ZEND_CHECK_STACK_LIMIT
111
zend_never_inline static void zend_stack_limit_error(void)
112
0
{
113
0
  size_t max_stack_size = 0;
114
0
  if ((uintptr_t) EG(stack_base) > (uintptr_t) EG(stack_limit)) {
115
0
    max_stack_size = (size_t) ((uintptr_t) EG(stack_base) - (uintptr_t) EG(stack_limit));
116
0
  }
117
118
0
  zend_error_noreturn(E_COMPILE_ERROR,
119
0
    "Maximum call stack size of %zu bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached during compilation. Try splitting expression",
120
0
    max_stack_size);
121
0
}
122
123
static void zend_check_stack_limit(void)
124
76.5M
{
125
76.5M
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
126
0
    zend_stack_limit_error();
127
0
  }
128
76.5M
}
129
#else /* ZEND_CHECK_STACK_LIMIT */
130
static void zend_check_stack_limit(void)
131
{
132
}
133
#endif /* ZEND_CHECK_STACK_LIMIT */
134
135
static void init_op(zend_op *op)
136
100M
{
137
100M
  MAKE_NOP(op);
138
100M
  op->extended_value = 0;
139
100M
  op->lineno = CG(zend_lineno);
140
#ifdef ZEND_VERIFY_TYPE_INFERENCE
141
  op->op1_use_type = 0;
142
  op->op2_use_type = 0;
143
  op->result_use_type = 0;
144
  op->op1_def_type = 0;
145
  op->op2_def_type = 0;
146
  op->result_def_type = 0;
147
#endif
148
100M
}
149
150
static zend_always_inline uint32_t get_next_op_number(void)
151
14.9M
{
152
14.9M
  return CG(active_op_array)->last;
153
14.9M
}
154
155
static zend_op *get_next_op(void)
156
98.3M
{
157
98.3M
  zend_op_array *op_array = CG(active_op_array);
158
98.3M
  uint32_t next_op_num = op_array->last++;
159
98.3M
  zend_op *next_op;
160
161
98.3M
  if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) {
162
850k
    CG(context).opcodes_size *= 4;
163
850k
    op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op));
164
850k
  }
165
166
98.3M
  next_op = &(op_array->opcodes[next_op_num]);
167
168
98.3M
  init_op(next_op);
169
170
98.3M
  return next_op;
171
98.3M
}
172
173
static zend_brk_cont_element *get_next_brk_cont_element(void)
174
42.9k
{
175
42.9k
  CG(context).last_brk_cont++;
176
42.9k
  CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont);
177
42.9k
  return &CG(context).brk_cont_array[CG(context).last_brk_cont-1];
178
42.9k
}
179
180
static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */
181
249k
{
182
249k
  zend_string *filename = CG(active_op_array)->filename;
183
249k
  zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32,
184
249k
    '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
185
249k
  return zend_new_interned_string(result);
186
249k
}
187
/* }}} */
188
189
static bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
190
28.5M
{
191
28.5M
  const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
192
28.5M
  if (ns_separator != NULL) {
193
27.8M
    *result = ns_separator + 1;
194
27.8M
    *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
195
27.8M
    return true;
196
27.8M
  }
197
198
735k
  return false;
199
28.5M
}
200
/* }}} */
201
202
typedef struct builtin_type_info {
203
  const char* name;
204
  const size_t name_len;
205
  const uint8_t type;
206
} builtin_type_info;
207
208
static const builtin_type_info builtin_types[] = {
209
  {ZEND_STRL("null"), IS_NULL},
210
  {ZEND_STRL("true"), IS_TRUE},
211
  {ZEND_STRL("false"), IS_FALSE},
212
  {ZEND_STRL("int"), IS_LONG},
213
  {ZEND_STRL("float"), IS_DOUBLE},
214
  {ZEND_STRL("string"), IS_STRING},
215
  {ZEND_STRL("bool"), _IS_BOOL},
216
  {ZEND_STRL("void"), IS_VOID},
217
  {ZEND_STRL("never"), IS_NEVER},
218
  {ZEND_STRL("iterable"), IS_ITERABLE},
219
  {ZEND_STRL("object"), IS_OBJECT},
220
  {ZEND_STRL("mixed"), IS_MIXED},
221
  /* Should be handled as ZEND_TYPE_ASTs but it may have a namespace component */
222
  {ZEND_STRL("array"), IS_ARRAY},
223
  {ZEND_STRL("callable"), IS_CALLABLE},
224
  {ZEND_STRL("static"), IS_STATIC},
225
  /* self and parent are effectively built-in types (and thus reserved), but they allow namespace components */
226
  {ZEND_STRL("self"), IS_UNDEF},
227
  {ZEND_STRL("parent"), IS_UNDEF},
228
  {NULL, 0, IS_UNDEF}
229
};
230
231
static bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */
232
724k
{
233
724k
  const builtin_type_info *reserved = builtin_types;
234
235
724k
  const char *uqname = ZSTR_VAL(name);
236
724k
  size_t uqname_len = ZSTR_LEN(name);
237
724k
  zend_get_unqualified_name(name, &uqname, &uqname_len);
238
239
13.0M
  for (; reserved->name; ++reserved) {
240
12.3M
    if (uqname_len == reserved->name_len
241
703k
      && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->name_len) == 0
242
12.3M
    ) {
243
61
      return true;
244
61
    }
245
12.3M
  }
246
247
724k
  return false;
248
724k
}
249
/* }}} */
250
251
void zend_assert_valid_class_name(const zend_string *name, const char *type) /* {{{ */
252
722k
{
253
722k
  if (zend_is_reserved_class_name(name)) {
254
38
    zend_error_noreturn(E_COMPILE_ERROR,
255
38
      "Cannot use \"%s\" as %s as it is reserved", ZSTR_VAL(name), type);
256
38
  }
257
722k
  if (zend_string_equals_literal(name, "_") || zend_string_ends_with_literal(name, "\\_")) {
258
2.51k
    zend_error(E_DEPRECATED, "Using \"_\" as %s is deprecated since 8.4", type);
259
719k
  } else if (zend_string_equals_literal_ci(name, "let") || zend_string_ends_with_literal(name, "\\let")) {
260
638
    zend_error(E_DEPRECATED, "Using \"let\" as %s is deprecated since 8.6", type);
261
718k
  } else if (zend_string_equals_literal_ci(name, "is") || zend_string_ends_with_literal(name, "\\is")) {
262
1.07k
    zend_error(E_DEPRECATED, "Using \"is\" as %s is deprecated since 8.6", type);
263
1.07k
  }
264
722k
}
265
/* }}} */
266
267
typedef struct {
268
  const char *name;
269
  size_t name_len;
270
  const char *correct_name;
271
} confusable_type_info;
272
273
static const confusable_type_info confusable_types[] = {
274
  {ZEND_STRL("boolean"), "bool"},
275
  {ZEND_STRL("integer"), "int"},
276
  {ZEND_STRL("double"), "float"},
277
  {ZEND_STRL("resource"), NULL},
278
  {NULL, 0, NULL},
279
};
280
281
static zend_always_inline uint8_t zend_lookup_builtin_type_by_name(const zend_string *name) /* {{{ */
282
805k
{
283
805k
  const builtin_type_info *info = &builtin_types[0];
284
285
11.7M
  for (; info->name; ++info) {
286
11.2M
    if (zend_string_equals_cstr_ci(name, info->name, info->name_len)) {
287
342k
      return info->type;
288
342k
    }
289
11.2M
  }
290
291
463k
  return 0;
292
805k
}
293
/* }}} */
294
295
static zend_always_inline bool zend_is_confusable_type(const zend_string *name, const char **correct_name) /* {{{ */
296
462k
{
297
462k
  const confusable_type_info *info = confusable_types;
298
299
  /* Intentionally using case-sensitive comparison here, because "integer" is likely intended
300
   * as a scalar type, while "Integer" is likely a class type. */
301
2.21M
  for (; info->name; ++info) {
302
1.81M
    if (zend_string_equals_cstr(name, info->name, info->name_len)) {
303
67.8k
      *correct_name = info->correct_name;
304
67.8k
      return 1;
305
67.8k
    }
306
1.81M
  }
307
308
394k
  return 0;
309
462k
}
310
/* }}} */
311
312
67.8k
static bool zend_is_not_imported(zend_string *name) {
313
  /* Assuming "name" is unqualified here. */
314
67.8k
  return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL;
315
67.8k
}
316
317
void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_array *op_array) /* {{{ */
318
2.15M
{
319
2.15M
  *prev_context = CG(context);
320
2.15M
  CG(context).prev = CG(context).op_array ? prev_context : NULL;
321
2.15M
  CG(context).op_array = op_array;
322
2.15M
  CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
323
2.15M
  CG(context).vars_size = 0;
324
2.15M
  CG(context).literals_size = 0;
325
2.15M
  CG(context).fast_call_var = -1;
326
2.15M
  CG(context).try_catch_offset = -1;
327
2.15M
  CG(context).current_brk_cont = -1;
328
2.15M
  CG(context).last_brk_cont = 0;
329
2.15M
  CG(context).has_assigned_to_http_response_header = false;
330
2.15M
  CG(context).brk_cont_array = NULL;
331
2.15M
  CG(context).labels = NULL;
332
2.15M
  CG(context).in_jmp_frameless_branch = false;
333
2.15M
  CG(context).in_finally = false;
334
2.15M
  CG(context).active_property_info_name = NULL;
335
2.15M
  CG(context).active_property_hook_kind = (zend_property_hook_kind)-1;
336
2.15M
}
337
/* }}} */
338
339
void zend_oparray_context_end(const zend_oparray_context *prev_context) /* {{{ */
340
2.14M
{
341
2.14M
  if (CG(context).brk_cont_array) {
342
12.8k
    efree(CG(context).brk_cont_array);
343
12.8k
    CG(context).brk_cont_array = NULL;
344
12.8k
  }
345
2.14M
  if (CG(context).labels) {
346
25.9k
    zend_hash_destroy(CG(context).labels);
347
25.9k
    FREE_HASHTABLE(CG(context).labels);
348
25.9k
    CG(context).labels = NULL;
349
25.9k
  }
350
2.14M
  CG(context) = *prev_context;
351
2.14M
}
352
/* }}} */
353
354
static void zend_reset_import_tables(void) /* {{{ */
355
59.3k
{
356
59.3k
  if (FC(imports)) {
357
1.25k
    zend_hash_destroy(FC(imports));
358
1.25k
    efree(FC(imports));
359
1.25k
    FC(imports) = NULL;
360
1.25k
  }
361
362
59.3k
  if (FC(imports_function)) {
363
162
    zend_hash_destroy(FC(imports_function));
364
162
    efree(FC(imports_function));
365
162
    FC(imports_function) = NULL;
366
162
  }
367
368
59.3k
  if (FC(imports_const)) {
369
691
    zend_hash_destroy(FC(imports_const));
370
691
    efree(FC(imports_const));
371
691
    FC(imports_const) = NULL;
372
691
  }
373
374
59.3k
  zend_hash_clean(&FC(seen_symbols));
375
59.3k
}
376
/* }}} */
377
378
54.2k
static void zend_end_namespace(void) /* {{{ */ {
379
54.2k
  FC(in_namespace) = 0;
380
54.2k
  zend_reset_import_tables();
381
54.2k
  if (FC(current_namespace)) {
382
3.52k
    zend_string_release_ex(FC(current_namespace), 0);
383
3.52k
    FC(current_namespace) = NULL;
384
3.52k
  }
385
54.2k
}
386
/* }}} */
387
388
void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
389
57.6k
{
390
57.6k
  *prev_context = CG(file_context);
391
57.6k
  FC(imports) = NULL;
392
57.6k
  FC(imports_function) = NULL;
393
57.6k
  FC(imports_const) = NULL;
394
57.6k
  FC(current_namespace) = NULL;
395
57.6k
  FC(in_namespace) = 0;
396
57.6k
  FC(has_bracketed_namespaces) = 0;
397
57.6k
  FC(declarables).ticks = 0;
398
57.6k
  zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
399
57.6k
}
400
/* }}} */
401
402
void zend_file_context_end(const zend_file_context *prev_context) /* {{{ */
403
52.6k
{
404
52.6k
  zend_end_namespace();
405
52.6k
  zend_hash_destroy(&FC(seen_symbols));
406
52.6k
  CG(file_context) = *prev_context;
407
52.6k
}
408
/* }}} */
409
410
void zend_init_compiler_data_structures(void) /* {{{ */
411
311k
{
412
311k
  zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
413
311k
  zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
414
311k
  zend_stack_init(&CG(short_circuiting_opnums), sizeof(uint32_t));
415
311k
  CG(active_class_entry) = NULL;
416
311k
  CG(in_compilation) = 0;
417
311k
  CG(skip_shebang) = 0;
418
419
311k
  CG(encoding_declared) = 0;
420
311k
  CG(memoized_exprs) = NULL;
421
311k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
422
311k
}
423
/* }}} */
424
425
2.24M
static void zend_register_seen_symbol(zend_string *name, uint32_t kind) {
426
2.24M
  zval *zv = zend_hash_find(&FC(seen_symbols), name);
427
2.24M
  if (zv) {
428
2.20M
    Z_LVAL_P(zv) |= kind;
429
2.20M
  } else {
430
40.6k
    zval tmp;
431
40.6k
    ZVAL_LONG(&tmp, kind);
432
40.6k
    zend_hash_add_new(&FC(seen_symbols), name, &tmp);
433
40.6k
  }
434
2.24M
}
435
436
4.01k
static bool zend_have_seen_symbol(zend_string *name, uint32_t kind) {
437
4.01k
  const zval *zv = zend_hash_find(&FC(seen_symbols), name);
438
4.01k
  return zv && (Z_LVAL_P(zv) & kind) != 0;
439
4.01k
}
440
441
void init_compiler(void) /* {{{ */
442
306k
{
443
306k
  CG(arena) = zend_arena_create(64 * 1024);
444
306k
  CG(active_op_array) = NULL;
445
306k
  memset(&CG(context), 0, sizeof(CG(context)));
446
306k
  zend_init_compiler_data_structures();
447
306k
  zend_init_rsrc_list();
448
306k
  zend_stream_init();
449
306k
  CG(unclean_shutdown) = 0;
450
451
306k
  CG(delayed_variance_obligations) = NULL;
452
306k
  CG(delayed_autoloads) = NULL;
453
306k
  CG(unlinked_uses) = NULL;
454
306k
  CG(current_linking_class) = NULL;
455
306k
  CG(interned_strings_fallible) = false;
456
306k
}
457
/* }}} */
458
459
void shutdown_compiler(void) /* {{{ */
460
311k
{
461
  /* Reset filename before destroying the arena, as file cache may use arena allocated strings. */
462
311k
  zend_restore_compiled_filename(NULL);
463
464
311k
  zend_stack_destroy(&CG(loop_var_stack));
465
311k
  zend_stack_destroy(&CG(delayed_oplines_stack));
466
311k
  zend_stack_destroy(&CG(short_circuiting_opnums));
467
468
311k
  if (CG(delayed_variance_obligations)) {
469
13
    zend_hash_destroy(CG(delayed_variance_obligations));
470
13
    FREE_HASHTABLE(CG(delayed_variance_obligations));
471
13
    CG(delayed_variance_obligations) = NULL;
472
13
  }
473
311k
  if (CG(delayed_autoloads)) {
474
10
    zend_hash_destroy(CG(delayed_autoloads));
475
10
    FREE_HASHTABLE(CG(delayed_autoloads));
476
10
    CG(delayed_autoloads) = NULL;
477
10
  }
478
311k
  if (CG(unlinked_uses)) {
479
94
    zend_hash_destroy(CG(unlinked_uses));
480
94
    FREE_HASHTABLE(CG(unlinked_uses));
481
94
    CG(unlinked_uses) = NULL;
482
94
  }
483
311k
  CG(current_linking_class) = NULL;
484
311k
}
485
/* }}} */
486
487
ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
488
110k
{
489
110k
  CG(compiled_filename) = zend_string_copy(new_compiled_filename);
490
110k
  return new_compiled_filename;
491
110k
}
492
/* }}} */
493
494
ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
495
538k
{
496
538k
  if (CG(compiled_filename)) {
497
110k
    zend_string_release(CG(compiled_filename));
498
110k
    CG(compiled_filename) = NULL;
499
110k
  }
500
538k
  CG(compiled_filename) = original_compiled_filename;
501
538k
}
502
/* }}} */
503
504
ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
505
2.94M
{
506
2.94M
  return CG(compiled_filename);
507
2.94M
}
508
/* }}} */
509
510
ZEND_API uint32_t zend_get_compiled_lineno(void) /* {{{ */
511
358k
{
512
358k
  return CG(zend_lineno);
513
358k
}
514
/* }}} */
515
516
ZEND_API bool zend_is_compiling(void) /* {{{ */
517
2.07M
{
518
2.07M
  return CG(in_compilation);
519
2.07M
}
520
/* }}} */
521
522
static bool zend_is_constructor(const zend_string *name) /* {{{ */
523
36.8k
{
524
36.8k
  return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
525
36.8k
}
526
/* }}} */
527
528
static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */
529
51.2M
{
530
51.2M
  return (uint32_t)CG(active_op_array)->T++;
531
51.2M
}
532
/* }}} */
533
534
2.79M
static uint32_t lookup_cv(zend_string *name) /* {{{ */{
535
2.79M
  zend_op_array *op_array = CG(active_op_array);
536
2.79M
  int i = 0;
537
2.79M
  zend_ulong hash_value = zend_string_hash_val(name);
538
539
4.54M
  while (i < op_array->last_var) {
540
2.29M
    if (ZSTR_H(op_array->vars[i]) == hash_value
541
544k
     && zend_string_equals(op_array->vars[i], name)) {
542
543k
      return EX_NUM_TO_VAR(i);
543
543k
    }
544
1.75M
    i++;
545
1.75M
  }
546
2.24M
  i = op_array->last_var;
547
2.24M
  op_array->last_var++;
548
2.24M
  if (op_array->last_var > CG(context).vars_size) {
549
1.84M
    CG(context).vars_size += 16; /* FIXME */
550
1.84M
    op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
551
1.84M
  }
552
553
2.24M
  op_array->vars[i] = zend_string_copy(name);
554
2.24M
  return EX_NUM_TO_VAR(i);
555
2.79M
}
556
/* }}} */
557
558
zend_string *zval_make_interned_string(zval *zv)
559
57.4M
{
560
57.4M
  ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
561
57.4M
  Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
562
57.4M
  if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
563
6.98M
    Z_TYPE_FLAGS_P(zv) = 0;
564
6.98M
  }
565
57.4M
  return Z_STR_P(zv);
566
57.4M
}
567
568
/* Common part of zend_add_literal and zend_append_individual_literal */
569
static inline void zend_insert_literal(const zend_op_array *op_array, zval *zv, int literal_position) /* {{{ */
570
59.2M
{
571
59.2M
  zval *lit = CT_CONSTANT_EX(op_array, literal_position);
572
59.2M
  if (Z_TYPE_P(zv) == IS_STRING) {
573
54.6M
    zval_make_interned_string(zv);
574
54.6M
  }
575
59.2M
  ZVAL_COPY_VALUE(lit, zv);
576
59.2M
  Z_EXTRA_P(lit) = 0;
577
59.2M
}
578
/* }}} */
579
580
/* Is used while compiling a function, using the context to keep track
581
   of an approximate size to avoid to relocate to often.
582
   Literals are truncated to actual size in the second compiler pass (pass_two()). */
583
static int zend_add_literal(zval *zv) /* {{{ */
584
59.2M
{
585
59.2M
  zend_op_array *op_array = CG(active_op_array);
586
59.2M
  uint32_t i = op_array->last_literal;
587
59.2M
  op_array->last_literal++;
588
59.2M
  if (i >= CG(context).literals_size) {
589
9.88M
    while (i >= CG(context).literals_size) {
590
4.94M
      CG(context).literals_size += 16; /* FIXME */
591
4.94M
    }
592
4.94M
    op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
593
4.94M
  }
594
59.2M
  zend_insert_literal(op_array, zv, i);
595
59.2M
  return i;
596
59.2M
}
597
/* }}} */
598
599
static inline int zend_add_literal_string(zend_string **str) /* {{{ */
600
48.9M
{
601
48.9M
  int ret;
602
48.9M
  zval zv;
603
48.9M
  ZVAL_STR(&zv, *str);
604
48.9M
  ret = zend_add_literal(&zv);
605
48.9M
  *str = Z_STR(zv);
606
48.9M
  return ret;
607
48.9M
}
608
/* }}} */
609
610
static int zend_add_func_name_literal(zend_string *name) /* {{{ */
611
247k
{
612
  /* Original name */
613
247k
  int ret = zend_add_literal_string(&name);
614
615
  /* Lowercased name */
616
247k
  zend_string *lc_name = zend_string_tolower(name);
617
247k
  zend_add_literal_string(&lc_name);
618
619
247k
  return ret;
620
247k
}
621
/* }}} */
622
623
static int zend_add_ns_func_name_literal(zend_string *name) /* {{{ */
624
4.27M
{
625
4.27M
  const char *unqualified_name;
626
4.27M
  size_t unqualified_name_len;
627
628
  /* Original name */
629
4.27M
  int ret = zend_add_literal_string(&name);
630
631
  /* Lowercased name */
632
4.27M
  zend_string *lc_name = zend_string_tolower(name);
633
4.27M
  zend_add_literal_string(&lc_name);
634
635
  /* Lowercased unqualified name */
636
4.27M
  if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
637
4.27M
    lc_name = zend_string_alloc(unqualified_name_len, 0);
638
4.27M
    zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
639
4.27M
    zend_add_literal_string(&lc_name);
640
4.27M
  }
641
642
4.27M
  return ret;
643
4.27M
}
644
/* }}} */
645
646
static int zend_add_class_name_literal(zend_string *name) /* {{{ */
647
64.7k
{
648
  /* Original name */
649
64.7k
  int ret = zend_add_literal_string(&name);
650
651
  /* Lowercased name */
652
64.7k
  zend_string *lc_name = zend_string_tolower(name);
653
64.7k
  zend_add_literal_string(&lc_name);
654
655
64.7k
  return ret;
656
64.7k
}
657
/* }}} */
658
659
static int zend_add_const_name_literal(zend_string *name, bool unqualified) /* {{{ */
660
11.6M
{
661
11.6M
  zend_string *tmp_name;
662
663
11.6M
  int ret = zend_add_literal_string(&name);
664
665
11.6M
  const char *after_ns;
666
11.6M
  size_t after_ns_len;
667
11.6M
  if (zend_get_unqualified_name(name, &after_ns, &after_ns_len)) {
668
11.5M
    size_t ns_len = after_ns - ZSTR_VAL(name) - 1;
669
670
    /* lowercased namespace name & original constant name */
671
11.5M
    tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
672
11.5M
    zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
673
11.5M
    zend_add_literal_string(&tmp_name);
674
675
11.5M
    if (!unqualified) {
676
18.1k
      return ret;
677
18.1k
    }
678
11.5M
  } else {
679
173k
    after_ns = ZSTR_VAL(name);
680
173k
    after_ns_len = ZSTR_LEN(name);
681
173k
  }
682
683
  /* original unqualified constant name */
684
11.6M
  tmp_name = zend_string_init(after_ns, after_ns_len, 0);
685
11.6M
  zend_add_literal_string(&tmp_name);
686
687
11.6M
  return ret;
688
11.6M
}
689
/* }}} */
690
691
59.8k
#define LITERAL_STR(op, str) do { \
692
59.8k
    zval _c; \
693
59.8k
    ZVAL_STR(&_c, str); \
694
59.8k
    op.constant = zend_add_literal(&_c); \
695
59.8k
  } while (0)
696
697
void zend_stop_lexing(void)
698
17
{
699
17
  if (LANG_SCNG(on_event)) {
700
0
    LANG_SCNG(on_event)(ON_STOP, END, 0, NULL, 0, LANG_SCNG(on_event_context));
701
0
  }
702
703
17
  LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
704
17
}
705
706
static inline void zend_begin_loop(
707
    uint8_t free_opcode, const znode *loop_var, bool is_switch) /* {{{ */
708
42.9k
{
709
42.9k
  zend_brk_cont_element *brk_cont_element;
710
42.9k
  int parent = CG(context).current_brk_cont;
711
42.9k
  zend_loop_var info = {0};
712
713
42.9k
  CG(context).current_brk_cont = CG(context).last_brk_cont;
714
42.9k
  brk_cont_element = get_next_brk_cont_element();
715
42.9k
  brk_cont_element->parent = parent;
716
42.9k
  brk_cont_element->is_switch = is_switch;
717
718
42.9k
  if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
719
9.52k
    uint32_t start = get_next_op_number();
720
721
9.52k
    info.opcode = free_opcode;
722
9.52k
    info.var_type = loop_var->op_type;
723
9.52k
    info.var_num = loop_var->u.op.var;
724
9.52k
    brk_cont_element->start = start;
725
33.3k
  } else {
726
33.3k
    info.opcode = ZEND_NOP;
727
    /* The start field is used to free temporary variables in case of exceptions.
728
     * We won't try to free something of we don't have loop variable.  */
729
33.3k
    brk_cont_element->start = -1;
730
33.3k
  }
731
732
42.9k
  zend_stack_push(&CG(loop_var_stack), &info);
733
42.9k
}
734
/* }}} */
735
736
static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
737
42.6k
{
738
42.6k
  uint32_t end = get_next_op_number();
739
42.6k
  zend_brk_cont_element *brk_cont_element
740
42.6k
    = &CG(context).brk_cont_array[CG(context).current_brk_cont];
741
42.6k
  brk_cont_element->cont = cont_addr;
742
42.6k
  brk_cont_element->brk = end;
743
42.6k
  CG(context).current_brk_cont = brk_cont_element->parent;
744
745
42.6k
  zend_stack_del_top(&CG(loop_var_stack));
746
42.6k
}
747
/* }}} */
748
749
bool zend_op_may_elide_result(uint8_t opcode)
750
4.60M
{
751
4.60M
  switch (opcode) {
752
19.8k
    case ZEND_ASSIGN:
753
23.6k
    case ZEND_ASSIGN_DIM:
754
25.9k
    case ZEND_ASSIGN_OBJ:
755
26.6k
    case ZEND_ASSIGN_STATIC_PROP:
756
27.9k
    case ZEND_ASSIGN_OP:
757
29.5k
    case ZEND_ASSIGN_DIM_OP:
758
29.8k
    case ZEND_ASSIGN_OBJ_OP:
759
34.0k
    case ZEND_ASSIGN_STATIC_PROP_OP:
760
34.2k
    case ZEND_PRE_INC_STATIC_PROP:
761
34.3k
    case ZEND_PRE_DEC_STATIC_PROP:
762
34.3k
    case ZEND_PRE_INC_OBJ:
763
34.8k
    case ZEND_PRE_DEC_OBJ:
764
35.1k
    case ZEND_PRE_INC:
765
35.5k
    case ZEND_PRE_DEC:
766
61.2k
    case ZEND_DO_FCALL:
767
73.3k
    case ZEND_DO_ICALL:
768
74.2k
    case ZEND_DO_UCALL:
769
1.14M
    case ZEND_DO_FCALL_BY_NAME:
770
1.14M
    case ZEND_YIELD:
771
1.14M
    case ZEND_YIELD_FROM:
772
1.14M
    case ZEND_INCLUDE_OR_EVAL:
773
1.14M
      return true;
774
3.45M
    default:
775
3.45M
      return false;
776
4.60M
  }
777
4.60M
}
778
779
static void zend_do_free(znode *op1) /* {{{ */
780
5.01M
{
781
5.01M
  if (op1->op_type == IS_TMP_VAR) {
782
4.70M
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
783
784
4.82M
    while (opline->opcode == ZEND_END_SILENCE ||
785
4.82M
           opline->opcode == ZEND_OP_DATA) {
786
122k
      opline--;
787
122k
    }
788
789
4.70M
    if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
790
4.59M
      switch (opline->opcode) {
791
1.16k
        case ZEND_BOOL:
792
3.44k
        case ZEND_BOOL_NOT:
793
          /* boolean results don't have to be freed */
794
3.44k
          return;
795
198
        case ZEND_POST_INC_STATIC_PROP:
796
264
        case ZEND_POST_DEC_STATIC_PROP:
797
761
        case ZEND_POST_INC_OBJ:
798
974
        case ZEND_POST_DEC_OBJ:
799
1.83k
        case ZEND_POST_INC:
800
3.26k
        case ZEND_POST_DEC:
801
          /* convert $i++ to ++$i */
802
3.26k
          opline->opcode -= 2;
803
3.26k
          SET_UNUSED(opline->result);
804
3.26k
          return;
805
4.58M
        default:
806
4.58M
          if (zend_op_may_elide_result(opline->opcode)) {
807
1.14M
            SET_UNUSED(opline->result);
808
1.14M
            return;
809
1.14M
          }
810
3.43M
          break;
811
4.59M
      }
812
4.59M
    }
813
814
3.54M
    zend_emit_op(NULL, ZEND_FREE, op1, NULL);
815
3.54M
  } else if (op1->op_type == IS_VAR) {
816
134k
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
817
134k
    while (opline->opcode == ZEND_END_SILENCE ||
818
134k
        opline->opcode == ZEND_EXT_FCALL_END ||
819
134k
        opline->opcode == ZEND_OP_DATA) {
820
197
      opline--;
821
197
    }
822
134k
    if (opline->result_type == IS_VAR
823
81.3k
      && opline->result.var == op1->u.op.var) {
824
81.3k
      if (opline->opcode == ZEND_FETCH_THIS) {
825
0
        opline->opcode = ZEND_NOP;
826
0
      }
827
81.3k
      if (!ZEND_OP_IS_FRAMELESS_ICALL(opline->opcode)) {
828
81.3k
        SET_UNUSED(opline->result);
829
81.3k
      } else {
830
        /* Frameless calls usually use the return value, so always emit a free. This should be
831
         * faster than checking RETURN_VALUE_USED inside the handler. */
832
0
        zend_emit_op(NULL, ZEND_FREE, op1, NULL);
833
0
      }
834
81.3k
    } else {
835
172k
      while (opline >= CG(active_op_array)->opcodes) {
836
172k
        if ((opline->opcode == ZEND_FETCH_LIST_R ||
837
171k
             opline->opcode == ZEND_FETCH_LIST_W ||
838
115k
             opline->opcode == ZEND_EXT_STMT) &&
839
57.2k
            opline->op1_type == IS_VAR &&
840
56.8k
            opline->op1.var == op1->u.op.var) {
841
52.9k
          zend_emit_op(NULL, ZEND_FREE, op1, NULL);
842
52.9k
          return;
843
52.9k
        }
844
119k
        if (opline->result_type == IS_VAR
845
60.3k
          && opline->result.var == op1->u.op.var) {
846
0
          if (opline->opcode == ZEND_NEW) {
847
0
            zend_emit_op(NULL, ZEND_FREE, op1, NULL);
848
0
          }
849
0
          break;
850
0
        }
851
119k
        opline--;
852
119k
      }
853
52.9k
    }
854
174k
  } else if (op1->op_type == IS_CONST) {
855
    /* Destroy value without using GC: When opcache moves arrays into SHM it will
856
     * free the zend_array structure, so references to it from outside the op array
857
     * become invalid. GC would cause such a reference in the root buffer. */
858
114k
    zval_ptr_dtor_nogc(&op1->u.constant);
859
114k
  }
860
5.01M
}
861
/* }}} */
862
863
864
static const char *zend_modifier_token_to_string(uint32_t token)
865
38
{
866
38
  switch (token) {
867
1
    case T_PUBLIC:
868
1
      return "public";
869
1
    case T_PROTECTED:
870
1
      return "protected";
871
1
    case T_PRIVATE:
872
1
      return "private";
873
5
    case T_STATIC:
874
5
      return "static";
875
6
    case T_FINAL:
876
6
      return "final";
877
11
    case T_READONLY:
878
11
      return "readonly";
879
9
    case T_ABSTRACT:
880
9
      return "abstract";
881
1
    case T_PUBLIC_SET:
882
1
      return "public(set)";
883
1
    case T_PROTECTED_SET:
884
1
      return "protected(set)";
885
2
    case T_PRIVATE_SET:
886
2
      return "private(set)";
887
0
    default: ZEND_UNREACHABLE();
888
38
  }
889
38
}
890
891
uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t token)
892
26.2k
{
893
26.2k
  switch (token) {
894
14.7k
    case T_PUBLIC:
895
14.7k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
896
14.7k
        return ZEND_ACC_PUBLIC;
897
14.7k
      }
898
1
      break;
899
1.30k
    case T_PROTECTED:
900
1.30k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
901
1.29k
        return ZEND_ACC_PROTECTED;
902
1.29k
      }
903
1
      break;
904
2.63k
    case T_PRIVATE:
905
2.63k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
906
2.63k
        return ZEND_ACC_PRIVATE;
907
2.63k
      }
908
1
      break;
909
371
    case T_READONLY:
910
371
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
911
368
        return ZEND_ACC_READONLY;
912
368
      }
913
3
      break;
914
1.07k
    case T_ABSTRACT:
915
1.07k
      if (target == ZEND_MODIFIER_TARGET_METHOD || target == ZEND_MODIFIER_TARGET_PROPERTY) {
916
1.07k
        return ZEND_ACC_ABSTRACT;
917
1.07k
      }
918
2
      break;
919
1.89k
    case T_FINAL:
920
1.89k
      return ZEND_ACC_FINAL;
921
3.43k
    case T_STATIC:
922
3.43k
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_METHOD) {
923
3.43k
        return ZEND_ACC_STATIC;
924
3.43k
      }
925
1
      break;
926
260
    case T_PUBLIC_SET:
927
260
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
928
259
        return ZEND_ACC_PUBLIC_SET;
929
259
      }
930
1
      break;
931
306
    case T_PROTECTED_SET:
932
306
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
933
305
        return ZEND_ACC_PROTECTED_SET;
934
305
      }
935
1
      break;
936
181
    case T_PRIVATE_SET:
937
181
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
938
179
        return ZEND_ACC_PRIVATE_SET;
939
179
      }
940
2
      break;
941
26.2k
  }
942
943
13
  char *member;
944
13
  if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
945
0
    member = "property";
946
13
  } else if (target == ZEND_MODIFIER_TARGET_METHOD) {
947
3
    member = "method";
948
10
  } else if (target == ZEND_MODIFIER_TARGET_CONSTANT) {
949
2
    member = "class constant";
950
8
  } else if (target == ZEND_MODIFIER_TARGET_CPP) {
951
2
    member = "parameter";
952
6
  } else if (target == ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
953
6
    member = "property hook";
954
6
  } else {
955
0
    ZEND_UNREACHABLE();
956
0
  }
957
958
13
  zend_throw_exception_ex(zend_ce_compile_error, 0,
959
13
    "Cannot use the %s modifier on a %s", zend_modifier_token_to_string(token), member);
960
13
  return 0;
961
13
}
962
963
uint32_t zend_modifier_list_to_flags(zend_modifier_target target, zend_ast *modifiers)
964
21.9k
{
965
21.9k
  uint32_t flags = 0;
966
21.9k
  const zend_ast_list *modifier_list = zend_ast_get_list(modifiers);
967
968
47.7k
  for (uint32_t i = 0; i < modifier_list->children; i++) {
969
25.8k
    uint32_t token = (uint32_t) Z_LVAL_P(zend_ast_get_zval(modifier_list->child[i]));
970
25.8k
    uint32_t new_flag = zend_modifier_token_to_flag(target, token);
971
25.8k
    if (!new_flag) {
972
11
      return 0;
973
11
    }
974
    /* Don't error immediately for duplicate flags, we want to prioritize the errors from zend_add_member_modifier(). */
975
25.8k
    bool duplicate_flag = (flags & new_flag);
976
25.7k
    flags = zend_add_member_modifier(flags, new_flag, target);
977
25.7k
    if (!flags) {
978
28
      return 0;
979
28
    }
980
25.7k
    if (duplicate_flag) {
981
25
      zend_throw_exception_ex(zend_ce_compile_error, 0,
982
25
        "Multiple %s modifiers are not allowed", zend_modifier_token_to_string(token));
983
25
      return 0;
984
25
    }
985
25.7k
  }
986
987
21.9k
  return flags;
988
21.9k
}
989
990
uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
991
176
{
992
176
  uint32_t new_flags = flags | new_flag;
993
176
  if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
994
6
    zend_throw_exception(zend_ce_compile_error,
995
6
      "Multiple abstract modifiers are not allowed", 0);
996
6
    return 0;
997
6
  }
998
170
  if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
999
6
    zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
1000
6
    return 0;
1001
6
  }
1002
164
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
1003
6
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
1004
6
    return 0;
1005
6
  }
1006
158
  if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
1007
5
    zend_throw_exception(zend_ce_compile_error,
1008
5
      "Cannot use the final modifier on an abstract class", 0);
1009
5
    return 0;
1010
5
  }
1011
153
  return new_flags;
1012
158
}
1013
/* }}} */
1014
1015
uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag)
1016
43
{
1017
43
  uint32_t new_flags = flags | new_flag;
1018
43
  if (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
1019
6
    zend_throw_exception(zend_ce_compile_error,
1020
6
      "Cannot use the abstract modifier on an anonymous class", 0);
1021
6
    return 0;
1022
6
  }
1023
37
  if (new_flag & ZEND_ACC_FINAL) {
1024
6
    zend_throw_exception(zend_ce_compile_error, "Cannot use the final modifier on an anonymous class", 0);
1025
6
    return 0;
1026
6
  }
1027
31
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
1028
1
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
1029
1
    return 0;
1030
1
  }
1031
30
  return new_flags;
1032
31
}
1033
1034
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target) /* {{{ */
1035
25.7k
{
1036
25.7k
  uint32_t new_flags = flags | new_flag;
1037
25.7k
  if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
1038
18
    zend_throw_exception(zend_ce_compile_error,
1039
18
      "Multiple access type modifiers are not allowed", 0);
1040
18
    return 0;
1041
18
  }
1042
25.7k
  if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
1043
2
    if (target == ZEND_MODIFIER_TARGET_METHOD) {
1044
1
      zend_throw_exception(zend_ce_compile_error,
1045
1
        "Cannot use the final modifier on an abstract method", 0);
1046
1
      return 0;
1047
1
    }
1048
1
    if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
1049
1
      zend_throw_exception(zend_ce_compile_error,
1050
1
        "Cannot use the final modifier on an abstract property", 0);
1051
1
      return 0;
1052
1
    }
1053
1
  }
1054
25.7k
  if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
1055
16.7k
    if ((flags & ZEND_ACC_PPP_SET_MASK) && (new_flag & ZEND_ACC_PPP_SET_MASK)) {
1056
8
      zend_throw_exception(zend_ce_compile_error,
1057
8
        "Multiple access type modifiers are not allowed", 0);
1058
8
      return 0;
1059
8
    }
1060
16.7k
  }
1061
25.7k
  return new_flags;
1062
25.7k
}
1063
/* }}} */
1064
1065
2.45k
ZEND_API zend_string *zend_create_member_string(const zend_string *class_name, const zend_string *member_name) {
1066
2.45k
  return zend_string_concat3(
1067
2.45k
    ZSTR_VAL(class_name), ZSTR_LEN(class_name),
1068
2.45k
    "::", sizeof("::") - 1,
1069
2.45k
    ZSTR_VAL(member_name), ZSTR_LEN(member_name));
1070
2.45k
}
1071
1072
18.9M
static zend_string *zend_concat_names(const char *name1, size_t name1_len, const char *name2, size_t name2_len) {
1073
18.9M
  return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len);
1074
18.9M
}
1075
1076
19.4M
static zend_string *zend_prefix_with_ns(zend_string *name) {
1077
19.4M
  if (FC(current_namespace)) {
1078
18.9M
    const zend_string *ns = FC(current_namespace);
1079
18.9M
    return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
1080
18.9M
  } else {
1081
493k
    return zend_string_copy(name);
1082
493k
  }
1083
19.4M
}
1084
1085
static zend_string *zend_resolve_non_class_name(
1086
  zend_string *name, uint32_t type, bool *is_fully_qualified,
1087
  bool case_sensitive, const HashTable *current_import_sub
1088
16.3M
) {
1089
16.3M
  const char *compound;
1090
16.3M
  *is_fully_qualified = false;
1091
1092
16.3M
  if (ZSTR_VAL(name)[0] == '\\') {
1093
    /* Remove \ prefix (only relevant if this is a string rather than a label) */
1094
530
    *is_fully_qualified = true;
1095
530
    return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1096
530
  }
1097
1098
16.3M
  if (type == ZEND_NAME_FQ) {
1099
85.1k
    *is_fully_qualified = true;
1100
85.1k
    return zend_string_copy(name);
1101
85.1k
  }
1102
1103
16.2M
  if (type == ZEND_NAME_RELATIVE) {
1104
1.42k
    *is_fully_qualified = true;
1105
1.42k
    return zend_prefix_with_ns(name);
1106
1.42k
  }
1107
1108
16.2M
  if (current_import_sub) {
1109
    /* If an unqualified name is a function/const alias, replace it. */
1110
3.05k
    zend_string *import_name;
1111
3.05k
    if (case_sensitive) {
1112
1.64k
      import_name = zend_hash_find_ptr(current_import_sub, name);
1113
1.64k
    } else {
1114
1.40k
      import_name = zend_hash_find_ptr_lc(current_import_sub, name);
1115
1.40k
    }
1116
1117
3.05k
    if (import_name) {
1118
498
      *is_fully_qualified = true;
1119
498
      return zend_string_copy(import_name);
1120
498
    }
1121
3.05k
  }
1122
1123
16.2M
  compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1124
16.2M
  if (compound) {
1125
21.8k
    *is_fully_qualified = true;
1126
21.8k
  }
1127
1128
16.2M
  if (compound && FC(imports)) {
1129
    /* If the first part of a qualified name is an alias, substitute it. */
1130
2.67k
    size_t len = compound - ZSTR_VAL(name);
1131
2.67k
    const zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1132
1133
2.67k
    if (import_name) {
1134
539
      return zend_concat_names(
1135
539
        ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1136
539
    }
1137
2.67k
  }
1138
1139
16.2M
  return zend_prefix_with_ns(name);
1140
16.2M
}
1141
/* }}} */
1142
1143
static zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1144
4.45M
{
1145
4.45M
  return zend_resolve_non_class_name(
1146
4.45M
    name, type, is_fully_qualified, false, FC(imports_function));
1147
4.45M
}
1148
1149
static zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1150
11.9M
{
1151
11.9M
  return zend_resolve_non_class_name(
1152
11.9M
    name, type, is_fully_qualified, true, FC(imports_const));
1153
11.9M
}
1154
1155
static zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
1156
2.93M
{
1157
2.93M
  const char *compound;
1158
1159
2.93M
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1160
3.52k
    if (type == ZEND_NAME_FQ) {
1161
1
      zend_error_noreturn(E_COMPILE_ERROR,
1162
1
        "'\\%s' is an invalid class name", ZSTR_VAL(name));
1163
1
    }
1164
3.52k
    if (type == ZEND_NAME_RELATIVE) {
1165
1
      zend_error_noreturn(E_COMPILE_ERROR,
1166
1
        "'namespace\\%s' is an invalid class name", ZSTR_VAL(name));
1167
1
    }
1168
3.52k
    ZEND_ASSERT(type == ZEND_NAME_NOT_FQ);
1169
3.52k
    return zend_string_copy(name);
1170
3.52k
  }
1171
1172
2.93M
  if (type == ZEND_NAME_RELATIVE) {
1173
568
    return zend_prefix_with_ns(name);
1174
568
  }
1175
1176
2.93M
  if (type == ZEND_NAME_FQ) {
1177
18.7k
    if (ZSTR_VAL(name)[0] == '\\') {
1178
      /* Remove \ prefix (only relevant if this is a string rather than a label) */
1179
1.11k
      name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1180
1.11k
      if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1181
1
        zend_error_noreturn(E_COMPILE_ERROR,
1182
1
          "'\\%s' is an invalid class name", ZSTR_VAL(name));
1183
1
      }
1184
1.11k
      return name;
1185
1.11k
    }
1186
1187
17.6k
    return zend_string_copy(name);
1188
18.7k
  }
1189
1190
2.91M
  if (FC(imports)) {
1191
3.02k
    compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1192
3.02k
    if (compound) {
1193
      /* If the first part of a qualified name is an alias, substitute it. */
1194
775
      size_t len = compound - ZSTR_VAL(name);
1195
775
      const zend_string *import_name =
1196
775
        zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1197
1198
775
      if (import_name) {
1199
325
        return zend_concat_names(
1200
325
          ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1201
325
      }
1202
2.24k
    } else {
1203
      /* If an unqualified name is an alias, replace it. */
1204
2.24k
      zend_string *import_name
1205
2.24k
        = zend_hash_find_ptr_lc(FC(imports), name);
1206
1207
2.24k
      if (import_name) {
1208
182
        return zend_string_copy(import_name);
1209
182
      }
1210
2.24k
    }
1211
3.02k
  }
1212
1213
  /* If not fully qualified and not an alias, prepend the current namespace */
1214
2.91M
  return zend_prefix_with_ns(name);
1215
2.91M
}
1216
/* }}} */
1217
1218
static zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
1219
2.66M
{
1220
2.66M
  const zval *class_name = zend_ast_get_zval(ast);
1221
2.66M
  if (Z_TYPE_P(class_name) != IS_STRING) {
1222
82
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1223
82
  }
1224
2.66M
  return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
1225
2.66M
}
1226
/* }}} */
1227
1228
static void label_ptr_dtor(zval *zv) /* {{{ */
1229
28.5k
{
1230
28.5k
  efree_size(Z_PTR_P(zv), sizeof(zend_label));
1231
28.5k
}
1232
/* }}} */
1233
1234
3.77k
static void str_dtor(zval *zv)  /* {{{ */ {
1235
3.77k
  zend_string_release_ex(Z_STR_P(zv), 0);
1236
3.77k
}
1237
/* }}} */
1238
1239
static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
1240
8.19k
{
1241
8.19k
  zend_op_array *op_array = CG(active_op_array);
1242
8.19k
  uint32_t try_catch_offset = op_array->last_try_catch++;
1243
8.19k
  zend_try_catch_element *elem;
1244
1245
8.19k
  op_array->try_catch_array = safe_erealloc(
1246
8.19k
    op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
1247
1248
8.19k
  elem = &op_array->try_catch_array[try_catch_offset];
1249
8.19k
  elem->try_op = try_op;
1250
8.19k
  elem->catch_op = 0;
1251
8.19k
  elem->finally_op = 0;
1252
8.19k
  elem->finally_end = 0;
1253
1254
8.19k
  return try_catch_offset;
1255
8.19k
}
1256
/* }}} */
1257
1258
ZEND_API void function_add_ref(zend_function *function) /* {{{ */
1259
226
{
1260
226
  if (function->type == ZEND_USER_FUNCTION) {
1261
226
    zend_op_array *op_array = &function->op_array;
1262
226
    if (op_array->refcount) {
1263
226
      (*op_array->refcount)++;
1264
226
    }
1265
1266
226
    ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
1267
226
    ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
1268
226
  }
1269
1270
226
  if (function->common.function_name) {
1271
226
    zend_string_addref(function->common.function_name);
1272
226
  }
1273
226
}
1274
/* }}} */
1275
1276
ZEND_NORETURN static zend_never_inline ZEND_COLD void do_bind_function_error(const zend_string *lcname, const zend_op_array *op_array, bool compile_time) /* {{{ */
1277
50
{
1278
50
  const zval *zv = zend_hash_find_known_hash(compile_time ? CG(function_table) : EG(function_table), lcname);
1279
50
  int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
1280
50
  const zend_function *old_function;
1281
1282
50
  ZEND_ASSERT(zv != NULL);
1283
50
  old_function = Z_PTR_P(zv);
1284
50
  if (old_function->type == ZEND_USER_FUNCTION
1285
49
    && old_function->op_array.last > 0) {
1286
49
    zend_error_noreturn(error_level, "Cannot redeclare function %s() (previously declared in %s:%d)",
1287
49
          op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name),
1288
49
          ZSTR_VAL(old_function->op_array.filename),
1289
49
          old_function->op_array.line_start);
1290
49
  } else {
1291
1
    zend_error_noreturn(error_level, "Cannot redeclare function %s()",
1292
1
      op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name));
1293
1
  }
1294
50
}
1295
1296
ZEND_API zend_result do_bind_function(zend_function *func, const zval *lcname) /* {{{ */
1297
2
{
1298
2
  zend_function *added_func = zend_hash_add_ptr(EG(function_table), Z_STR_P(lcname), func);
1299
2
  if (UNEXPECTED(!added_func)) {
1300
0
    do_bind_function_error(Z_STR_P(lcname), &func->op_array, false);
1301
0
    return FAILURE;
1302
0
  }
1303
1304
2
  if (func->op_array.refcount) {
1305
2
    ++*func->op_array.refcount;
1306
2
  }
1307
2
  if (func->common.function_name) {
1308
2
    zend_string_addref(func->common.function_name);
1309
2
  }
1310
2
  zend_observer_function_declared_notify(&func->op_array, Z_STR_P(lcname));
1311
2
  return SUCCESS;
1312
2
}
1313
/* }}} */
1314
1315
ZEND_API zend_class_entry *zend_bind_class_in_slot(
1316
    zval *class_table_slot, const zval *lcname, zend_string *lc_parent_name)
1317
330
{
1318
330
  zend_class_entry *ce = Z_PTR_P(class_table_slot);
1319
330
  bool is_preloaded =
1320
330
    (ce->ce_flags & ZEND_ACC_PRELOADED) && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD);
1321
330
  bool success;
1322
330
  if (EXPECTED(!is_preloaded)) {
1323
330
    success = zend_hash_set_bucket_key(EG(class_table), (Bucket*) class_table_slot, Z_STR_P(lcname)) != NULL;
1324
330
  } else {
1325
    /* If preloading is used, don't replace the existing bucket, add a new one. */
1326
0
    success = zend_hash_add_ptr(EG(class_table), Z_STR_P(lcname), ce) != NULL;
1327
0
  }
1328
330
  if (UNEXPECTED(!success)) {
1329
44
    zend_class_entry *old_class = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1330
44
    ZEND_ASSERT(old_class);
1331
44
    zend_class_redeclaration_error(E_COMPILE_ERROR, old_class);
1332
44
    return NULL;
1333
44
  }
1334
1335
286
  if (ce->ce_flags & ZEND_ACC_LINKED) {
1336
3
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1337
3
    return ce;
1338
3
  }
1339
1340
283
  ce = zend_do_link_class(ce, lc_parent_name, Z_STR_P(lcname));
1341
283
  if (ce) {
1342
172
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1343
172
    return ce;
1344
172
  }
1345
1346
111
  if (!is_preloaded) {
1347
    /* Reload bucket pointer, the hash table may have been reallocated */
1348
61
    zval *zv = zend_hash_find(EG(class_table), Z_STR_P(lcname));
1349
61
    zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(lcname + 1));
1350
61
  } else {
1351
50
    zend_hash_del(EG(class_table), Z_STR_P(lcname));
1352
50
  }
1353
111
  return NULL;
1354
283
}
1355
1356
ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */
1357
274
{
1358
274
  zval *rtd_key, *zv;
1359
1360
274
  rtd_key = lcname + 1;
1361
1362
274
  zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(rtd_key));
1363
1364
274
  if (UNEXPECTED(!zv)) {
1365
0
    const zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1366
0
    ZEND_ASSERT(ce);
1367
0
    zend_class_redeclaration_error(E_COMPILE_ERROR, ce);
1368
0
    return FAILURE;
1369
0
  }
1370
1371
  /* Register the derived class */
1372
274
  return zend_bind_class_in_slot(zv, lcname, lc_parent_name) ? SUCCESS : FAILURE;
1373
274
}
1374
/* }}} */
1375
1376
10.3k
static zend_string *add_type_string(zend_string *type, zend_string *new_type, bool is_intersection) {
1377
10.3k
  zend_string *result;
1378
10.3k
  if (type == NULL) {
1379
4.23k
    return zend_string_copy(new_type);
1380
4.23k
  }
1381
1382
6.11k
  if (is_intersection) {
1383
3.70k
    result = zend_string_concat3(ZSTR_VAL(type), ZSTR_LEN(type),
1384
3.70k
      "&", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1385
3.70k
    zend_string_release(type);
1386
3.70k
  } else {
1387
2.41k
    result = zend_string_concat3(
1388
2.41k
      ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1389
2.41k
    zend_string_release(type);
1390
2.41k
  }
1391
6.11k
  return result;
1392
10.3k
}
1393
1394
2.19k
static zend_string *resolve_class_name(zend_string *name, const zend_class_entry *scope) {
1395
2.19k
  if (scope) {
1396
1.88k
    if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1397
2
      name = scope->name;
1398
1.88k
    } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT)) && scope->parent) {
1399
0
      name = scope->parent->name;
1400
0
    }
1401
1.88k
  }
1402
1403
  /* The resolved name for anonymous classes contains null bytes. Cut off everything after the
1404
   * null byte here, to avoid larger parts of the type being omitted by printing code later. */
1405
2.19k
  size_t len = strlen(ZSTR_VAL(name));
1406
2.19k
  if (len != ZSTR_LEN(name)) {
1407
0
    return zend_string_init(ZSTR_VAL(name), len, 0);
1408
0
  }
1409
2.19k
  return zend_string_copy(name);
1410
2.19k
}
1411
1412
static zend_string *add_intersection_type(zend_string *str,
1413
  const zend_type_list *intersection_type_list,
1414
  bool is_bracketed)
1415
1.68k
{
1416
1.68k
  const zend_type *single_type;
1417
1.68k
  zend_string *intersection_str = NULL;
1418
1419
7.08k
  ZEND_TYPE_LIST_FOREACH(intersection_type_list, single_type) {
1420
7.08k
    ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*single_type));
1421
7.08k
    ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*single_type));
1422
1423
5.39k
    intersection_str = add_type_string(intersection_str, ZEND_TYPE_NAME(*single_type), /* is_intersection */ true);
1424
5.39k
  } ZEND_TYPE_LIST_FOREACH_END();
1425
1426
1.68k
  ZEND_ASSERT(intersection_str);
1427
1428
1.68k
  if (is_bracketed) {
1429
1.50k
    zend_string *result = zend_string_concat3("(", 1, ZSTR_VAL(intersection_str), ZSTR_LEN(intersection_str), ")", 1);
1430
1.50k
    zend_string_release(intersection_str);
1431
1.50k
    intersection_str = result;
1432
1.50k
  }
1433
1.68k
  str = add_type_string(str, intersection_str, /* is_intersection */ false);
1434
1.68k
  zend_string_release(intersection_str);
1435
1.68k
  return str;
1436
1.68k
}
1437
1438
3.76k
zend_string *zend_type_to_string_resolved(const zend_type type, const zend_class_entry *scope) {
1439
3.76k
  zend_string *str = NULL;
1440
1441
  /* Pure intersection type */
1442
3.76k
  if (ZEND_TYPE_IS_INTERSECTION(type)) {
1443
180
    ZEND_ASSERT(!ZEND_TYPE_IS_UNION(type));
1444
180
    str = add_intersection_type(str, ZEND_TYPE_LIST(type), /* is_bracketed */ false);
1445
3.58k
  } else if (ZEND_TYPE_HAS_LIST(type)) {
1446
    /* A union type might not be a list */
1447
343
    const zend_type *list_type;
1448
2.82k
    ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
1449
2.82k
      if (ZEND_TYPE_IS_INTERSECTION(*list_type)) {
1450
1.50k
        str = add_intersection_type(str, ZEND_TYPE_LIST(*list_type), /* is_bracketed */ true);
1451
1.50k
        continue;
1452
1.50k
      }
1453
974
      ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type));
1454
974
      ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*list_type));
1455
1456
974
      zend_string *name = ZEND_TYPE_NAME(*list_type);
1457
974
      zend_string *resolved = resolve_class_name(name, scope);
1458
974
      str = add_type_string(str, resolved, /* is_intersection */ false);
1459
974
      zend_string_release(resolved);
1460
974
    } ZEND_TYPE_LIST_FOREACH_END();
1461
3.24k
  } else if (ZEND_TYPE_HAS_NAME(type)) {
1462
1.22k
    str = resolve_class_name(ZEND_TYPE_NAME(type), scope);
1463
1.22k
  }
1464
1465
3.76k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
1466
1467
3.76k
  if (type_mask == MAY_BE_ANY) {
1468
14
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_MIXED), /* is_intersection */ false);
1469
1470
14
    return str;
1471
14
  }
1472
3.75k
  if (type_mask & MAY_BE_STATIC) {
1473
43
    zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC);
1474
    // During compilation of eval'd code the called scope refers to the scope calling the eval
1475
43
    if (scope && !zend_is_compiling()) {
1476
0
      const zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
1477
0
      if (called_scope) {
1478
0
        name = called_scope->name;
1479
0
      }
1480
0
    }
1481
43
    str = add_type_string(str, name, /* is_intersection */ false);
1482
43
  }
1483
3.75k
  if (type_mask & MAY_BE_CALLABLE) {
1484
22
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE), /* is_intersection */ false);
1485
22
  }
1486
3.75k
  if (type_mask & MAY_BE_OBJECT) {
1487
20
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT), /* is_intersection */ false);
1488
20
  }
1489
3.75k
  if (type_mask & MAY_BE_ARRAY) {
1490
401
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY), /* is_intersection */ false);
1491
401
  }
1492
3.75k
  if (type_mask & MAY_BE_STRING) {
1493
793
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING), /* is_intersection */ false);
1494
793
  }
1495
3.75k
  if (type_mask & MAY_BE_LONG) {
1496
834
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT), /* is_intersection */ false);
1497
834
  }
1498
3.75k
  if (type_mask & MAY_BE_DOUBLE) {
1499
13
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT), /* is_intersection */ false);
1500
13
  }
1501
3.75k
  if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
1502
12
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL), /* is_intersection */ false);
1503
3.74k
  } else if (type_mask & MAY_BE_FALSE) {
1504
11
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE), /* is_intersection */ false);
1505
3.73k
  } else if (type_mask & MAY_BE_TRUE) {
1506
7
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_TRUE), /* is_intersection */ false);
1507
7
  }
1508
3.75k
  if (type_mask & MAY_BE_VOID) {
1509
27
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID), /* is_intersection */ false);
1510
27
  }
1511
3.75k
  if (type_mask & MAY_BE_NEVER) {
1512
5
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NEVER), /* is_intersection */ false);
1513
5
  }
1514
1515
3.75k
  if (type_mask & MAY_BE_NULL) {
1516
963
    bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
1517
963
    bool has_intersection = !str || memchr(ZSTR_VAL(str), '&', ZSTR_LEN(str)) != NULL;
1518
963
    if (!is_union && !has_intersection) {
1519
872
      zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
1520
872
      zend_string_release(str);
1521
872
      return nullable_str;
1522
872
    }
1523
1524
91
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE), /* is_intersection */ false);
1525
91
  }
1526
2.88k
  return str;
1527
3.75k
}
1528
1529
489
ZEND_API zend_string *zend_type_to_string(zend_type type) {
1530
489
  return zend_type_to_string_resolved(type, NULL);
1531
489
}
1532
1533
1.91k
static bool is_generator_compatible_class_type(const zend_string *name) {
1534
1.91k
  return zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_TRAVERSABLE))
1535
1.17k
    || zend_string_equals_literal_ci(name, "Iterator")
1536
598
    || zend_string_equals_literal_ci(name, "Generator");
1537
1.91k
}
1538
1539
static void zend_mark_function_as_generator(void) /* {{{ */
1540
1.08M
{
1541
1.08M
  if (!CG(active_op_array)->function_name) {
1542
113
    zend_error_noreturn(E_COMPILE_ERROR,
1543
113
      "The \"yield\" expression can only be used inside a function");
1544
113
  }
1545
1546
1.08M
  if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
1547
1.55k
    const zend_type return_type = CG(active_op_array)->arg_info[-1].type;
1548
1.55k
    bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_OBJECT) != 0;
1549
1.55k
    if (!valid_type) {
1550
1.43k
      const zend_type *single_type;
1551
3.34k
      ZEND_TYPE_FOREACH(return_type, single_type) {
1552
3.34k
        if (ZEND_TYPE_HAS_NAME(*single_type)
1553
1.91k
            && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) {
1554
1.41k
          valid_type = true;
1555
1.41k
          break;
1556
1.41k
        }
1557
3.34k
      } ZEND_TYPE_FOREACH_END();
1558
1.43k
    }
1559
1560
1.55k
    if (!valid_type) {
1561
21
      zend_string *str = zend_type_to_string(return_type);
1562
21
      zend_error_noreturn(E_COMPILE_ERROR,
1563
21
        "Generator return type must be a supertype of Generator, %s given",
1564
21
        ZSTR_VAL(str));
1565
21
    }
1566
1.55k
  }
1567
1568
1.08M
  CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR;
1569
1.08M
}
1570
/* }}} */
1571
1572
ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal) /* {{{ */
1573
28.5k
{
1574
28.5k
  size_t prop_name_length = 1 + src1_length + 1 + src2_length;
1575
28.5k
  zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
1576
1577
28.5k
  ZSTR_VAL(prop_name)[0] = '\0';
1578
28.5k
  memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
1579
28.5k
  memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
1580
28.5k
  return prop_name;
1581
28.5k
}
1582
/* }}} */
1583
1584
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) /* {{{ */
1585
1.44M
{
1586
1.44M
  size_t class_name_len;
1587
1.44M
  size_t anonclass_src_len;
1588
1589
1.44M
  *class_name = NULL;
1590
1591
1.44M
  if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
1592
1.29M
    *prop_name = ZSTR_VAL(name);
1593
1.29M
    if (prop_len) {
1594
879k
      *prop_len = ZSTR_LEN(name);
1595
879k
    }
1596
1.29M
    return SUCCESS;
1597
1.29M
  }
1598
149k
  if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
1599
1.50k
    zend_error(E_NOTICE, "Illegal member variable name");
1600
1.50k
    *prop_name = ZSTR_VAL(name);
1601
1.50k
    if (prop_len) {
1602
1.29k
      *prop_len = ZSTR_LEN(name);
1603
1.29k
    }
1604
1.50k
    return FAILURE;
1605
1.50k
  }
1606
1607
148k
  class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
1608
148k
  if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
1609
663
    zend_error(E_NOTICE, "Corrupt member variable name");
1610
663
    *prop_name = ZSTR_VAL(name);
1611
663
    if (prop_len) {
1612
332
      *prop_len = ZSTR_LEN(name);
1613
332
    }
1614
663
    return FAILURE;
1615
663
  }
1616
1617
147k
  *class_name = ZSTR_VAL(name) + 1;
1618
147k
  anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2);
1619
147k
  if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) {
1620
32.3k
    class_name_len += anonclass_src_len + 1;
1621
32.3k
  }
1622
147k
  *prop_name = ZSTR_VAL(name) + class_name_len + 2;
1623
147k
  if (prop_len) {
1624
91.5k
    *prop_len = ZSTR_LEN(name) - class_name_len - 2;
1625
91.5k
  }
1626
147k
  return SUCCESS;
1627
148k
}
1628
/* }}} */
1629
1630
static bool array_is_const_ex(const zend_array *array, uint32_t *max_checks)
1631
0
{
1632
0
  if (zend_hash_num_elements(array) > *max_checks) {
1633
0
    return false;
1634
0
  }
1635
0
  *max_checks -= zend_hash_num_elements(array);
1636
1637
0
  zval *element;
1638
0
  ZEND_HASH_FOREACH_VAL(array, element) {
1639
0
    if (Z_TYPE_P(element) < IS_ARRAY) {
1640
0
      continue;
1641
0
    } else if (Z_TYPE_P(element) == IS_ARRAY) {
1642
0
      if (!array_is_const_ex(array, max_checks)) {
1643
0
        return false;
1644
0
      }
1645
0
    } else {
1646
0
      return false;
1647
0
    }
1648
0
  } ZEND_HASH_FOREACH_END();
1649
1650
0
  return true;
1651
0
}
1652
1653
static bool array_is_const(const zend_array *array)
1654
0
{
1655
0
  uint32_t max_checks = 50;
1656
0
  return array_is_const_ex(array, &max_checks);
1657
0
}
1658
1659
955
static bool can_ct_eval_const(const zend_constant *c) {
1660
955
  if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
1661
82
    return false;
1662
82
  }
1663
873
  if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
1664
873
      && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
1665
135
      && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
1666
135
        && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
1667
135
    return true;
1668
135
  }
1669
738
  if (Z_TYPE(c->value) < IS_ARRAY
1670
738
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1671
0
    return 1;
1672
738
  } else if (Z_TYPE(c->value) == IS_ARRAY
1673
0
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)
1674
0
      && array_is_const(Z_ARR(c->value))) {
1675
0
    return 1;
1676
0
  }
1677
738
  return false;
1678
738
}
1679
1680
static bool zend_try_ct_eval_const(zval *zv, zend_string *name, bool is_fully_qualified) /* {{{ */
1681
11.9M
{
1682
  /* Substitute true, false and null (including unqualified usage in namespaces)
1683
   * before looking up the possibly namespaced name. */
1684
11.9M
  const char *lookup_name = ZSTR_VAL(name);
1685
11.9M
  size_t lookup_len = ZSTR_LEN(name);
1686
1687
11.9M
  if (!is_fully_qualified) {
1688
11.8M
    zend_get_unqualified_name(name, &lookup_name, &lookup_len);
1689
11.8M
  }
1690
1691
11.9M
  zend_constant *c;
1692
11.9M
  if ((c = zend_get_special_const(lookup_name, lookup_len))) {
1693
46.2k
    ZVAL_COPY_VALUE(zv, &c->value);
1694
46.2k
    return true;
1695
46.2k
  }
1696
11.8M
  c = zend_hash_find_ptr(EG(zend_constants), name);
1697
11.8M
  if (c && can_ct_eval_const(c)) {
1698
135
    ZVAL_COPY_OR_DUP(zv, &c->value);
1699
135
    return true;
1700
135
  }
1701
11.8M
  return false;
1702
11.8M
}
1703
/* }}} */
1704
1705
static inline bool zend_is_scope_known(void) /* {{{ */
1706
20.3k
{
1707
20.3k
  if (!CG(active_op_array)) {
1708
    /* This can only happen when evaluating a default value string. */
1709
0
    return false;
1710
0
  }
1711
1712
20.3k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
1713
    /* Closures can be rebound to a different scope */
1714
7.25k
    return false;
1715
7.25k
  }
1716
1717
13.1k
  if (!CG(active_class_entry)) {
1718
    /* The scope is known if we're in a free function (no scope), but not if we're in
1719
     * a file/eval (which inherits including/eval'ing scope). */
1720
3.20k
    return CG(active_op_array)->function_name != NULL;
1721
3.20k
  }
1722
1723
  /* For traits self etc refers to the using class, not the trait itself */
1724
9.92k
  return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
1725
13.1k
}
1726
/* }}} */
1727
1728
static inline bool class_name_refers_to_active_ce(const zend_string *class_name, uint32_t fetch_type) /* {{{ */
1729
29.7k
{
1730
29.7k
  if (!CG(active_class_entry)) {
1731
26.2k
    return false;
1732
26.2k
  }
1733
3.50k
  if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
1734
572
    return true;
1735
572
  }
1736
2.93k
  return fetch_type == ZEND_FETCH_CLASS_DEFAULT
1737
2.20k
    && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
1738
3.50k
}
1739
/* }}} */
1740
1741
uint32_t zend_get_class_fetch_type(const zend_string *name) /* {{{ */
1742
3.76M
{
1743
3.76M
  if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1744
10.9k
    return ZEND_FETCH_CLASS_SELF;
1745
3.75M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT))) {
1746
13.3k
    return ZEND_FETCH_CLASS_PARENT;
1747
3.74M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_STATIC))) {
1748
1.42k
    return ZEND_FETCH_CLASS_STATIC;
1749
3.74M
  } else {
1750
3.74M
    return ZEND_FETCH_CLASS_DEFAULT;
1751
3.74M
  }
1752
3.76M
}
1753
/* }}} */
1754
1755
static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
1756
740k
{
1757
  /* Fully qualified names are always default refs */
1758
740k
  if (name_ast->attr == ZEND_NAME_FQ) {
1759
4.14k
    return ZEND_FETCH_CLASS_DEFAULT;
1760
4.14k
  }
1761
1762
735k
  return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
1763
740k
}
1764
/* }}} */
1765
1766
static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type)
1767
264k
{
1768
264k
  zend_string *class_name = zend_ast_get_str(ast);
1769
264k
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) {
1770
12
    zend_error_noreturn(E_COMPILE_ERROR,
1771
12
      "Cannot use \"%s\" as %s, as it is reserved",
1772
12
      ZSTR_VAL(class_name), type);
1773
12
  }
1774
264k
  return zend_resolve_class_name(class_name, ast->attr);
1775
264k
}
1776
1777
static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
1778
12.0k
{
1779
12.0k
  if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) {
1780
2.26k
    zend_class_entry *ce = CG(active_class_entry);
1781
2.26k
    if (!ce) {
1782
19
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
1783
19
        fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
1784
19
        fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
1785
2.24k
    } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) {
1786
30
      zend_error_noreturn(E_COMPILE_ERROR,
1787
30
        "Cannot use \"parent\" when current class scope has no parent");
1788
30
    }
1789
2.26k
  }
1790
12.0k
}
1791
/* }}} */
1792
1793
static bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */
1794
7.17k
{
1795
7.17k
  uint32_t fetch_type;
1796
7.17k
  const zval *class_name;
1797
1798
7.17k
  if (class_ast->kind != ZEND_AST_ZVAL) {
1799
2.73k
    return false;
1800
2.73k
  }
1801
1802
4.44k
  class_name = zend_ast_get_zval(class_ast);
1803
1804
4.44k
  if (Z_TYPE_P(class_name) != IS_STRING) {
1805
6
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1806
6
  }
1807
1808
4.44k
  fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name));
1809
4.44k
  zend_ensure_valid_class_fetch_type(fetch_type);
1810
1811
4.44k
  switch (fetch_type) {
1812
325
    case ZEND_FETCH_CLASS_SELF:
1813
325
      if (CG(active_class_entry) && zend_is_scope_known()) {
1814
57
        ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
1815
57
        return true;
1816
57
      }
1817
268
      return false;
1818
2.41k
    case ZEND_FETCH_CLASS_PARENT:
1819
2.41k
      if (CG(active_class_entry) && CG(active_class_entry)->parent_name
1820
144
          && zend_is_scope_known()) {
1821
144
        ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name);
1822
144
        return true;
1823
144
      }
1824
2.26k
      return false;
1825
293
    case ZEND_FETCH_CLASS_STATIC:
1826
293
      return false;
1827
1.40k
    case ZEND_FETCH_CLASS_DEFAULT:
1828
1.40k
      ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
1829
1.40k
      return true;
1830
0
    default: ZEND_UNREACHABLE();
1831
4.44k
  }
1832
4.44k
}
1833
/* }}} */
1834
1835
/* We don't use zend_verify_const_access because we need to deal with unlinked classes. */
1836
static bool zend_verify_ct_const_access(const zend_class_constant *c, const zend_class_entry *scope)
1837
11
{
1838
11
  if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED) {
1839
0
    return 0;
1840
11
  } else if (c->ce->ce_flags & ZEND_ACC_TRAIT) {
1841
    /* This condition is only met on directly accessing trait constants,
1842
     * because the ce is replaced to the class entry of the composing class
1843
     * on binding. */
1844
0
    return 0;
1845
11
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) {
1846
10
    return 1;
1847
10
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) {
1848
0
    return c->ce == scope;
1849
1
  } else {
1850
1
    zend_class_entry *ce = c->ce;
1851
1
    while (1) {
1852
1
      if (ce == scope) {
1853
0
        return 1;
1854
0
      }
1855
1
      if (!ce->parent) {
1856
1
        break;
1857
1
      }
1858
0
      if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
1859
0
        ce = ce->parent;
1860
0
      } else {
1861
0
        ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name);
1862
0
        if (!ce) {
1863
0
          break;
1864
0
        }
1865
0
      }
1866
0
    }
1867
    /* Reverse case cannot be true during compilation */
1868
1
    return 0;
1869
1
  }
1870
11
}
1871
1872
static bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
1873
29.7k
{
1874
29.7k
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
1875
29.7k
  zend_class_constant *cc;
1876
29.7k
  zval *c;
1877
1878
29.7k
  if (class_name_refers_to_active_ce(class_name, fetch_type)) {
1879
659
    cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
1880
29.1k
  } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1881
488
    const zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name);
1882
488
    if (ce) {
1883
19
      cc = zend_hash_find_ptr(&ce->constants_table, name);
1884
469
    } else {
1885
469
      return 0;
1886
469
    }
1887
28.6k
  } else {
1888
28.6k
    return 0;
1889
28.6k
  }
1890
1891
678
  if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
1892
654
    return false;
1893
654
  }
1894
1895
24
  if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) {
1896
14
    return false;
1897
14
  }
1898
1899
10
  c = &cc->value;
1900
1901
  /* Substitute case-sensitive (or lowercase) persistent class constants */
1902
10
  if (Z_TYPE_P(c) < IS_ARRAY) {
1903
9
    ZVAL_COPY_OR_DUP(zv, c);
1904
9
    return 1;
1905
9
  } else if (Z_TYPE_P(c) == IS_ARRAY && array_is_const(Z_ARR_P(c))) {
1906
0
    ZVAL_COPY_OR_DUP(zv, c);
1907
0
    return 1;
1908
0
  }
1909
1910
1
  return false;
1911
10
}
1912
/* }}} */
1913
1914
static void zend_add_to_list(void *result, void *item) /* {{{ */
1915
7.26k
{
1916
7.26k
  void** list = *(void**)result;
1917
7.26k
  size_t n = 0;
1918
1919
7.26k
  if (list) {
1920
101k
    while (list[n]) {
1921
96.4k
      n++;
1922
96.4k
    }
1923
5.12k
  }
1924
1925
7.26k
  list = erealloc(list, sizeof(void*) * (n+2));
1926
1927
7.26k
  list[n]   = item;
1928
7.26k
  list[n+1] = NULL;
1929
1930
7.26k
  *(void**)result = list;
1931
7.26k
}
1932
/* }}} */
1933
1934
static void zend_do_extended_stmt(znode* result) /* {{{ */
1935
2.25M
{
1936
2.25M
  zend_op *opline;
1937
1938
2.25M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) {
1939
2.25M
    return;
1940
2.25M
  }
1941
1942
0
  opline = get_next_op();
1943
1944
0
  opline->opcode = ZEND_EXT_STMT;
1945
0
  if (result) {
1946
0
    if (result->op_type == IS_CONST) {
1947
0
      Z_TRY_ADDREF(result->u.constant);
1948
0
    }
1949
0
    SET_NODE(opline->op1, result);
1950
0
  }
1951
0
}
1952
/* }}} */
1953
1954
static void zend_do_extended_fcall_begin(void) /* {{{ */
1955
4.75M
{
1956
4.75M
  zend_op *opline;
1957
1958
4.75M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1959
4.75M
    return;
1960
4.75M
  }
1961
1962
0
  opline = get_next_op();
1963
1964
0
  opline->opcode = ZEND_EXT_FCALL_BEGIN;
1965
0
}
1966
/* }}} */
1967
1968
static void zend_do_extended_fcall_end(void) /* {{{ */
1969
4.75M
{
1970
4.75M
  zend_op *opline;
1971
1972
4.75M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1973
4.75M
    return;
1974
4.75M
  }
1975
1976
0
  opline = get_next_op();
1977
1978
0
  opline->opcode = ZEND_EXT_FCALL_END;
1979
0
}
1980
/* }}} */
1981
1982
static zend_always_inline bool zend_auto_global_check(zend_auto_global *auto_global)
1983
2.89M
{
1984
2.89M
  if (auto_global == NULL) {
1985
2.88M
    return false;
1986
2.88M
  }
1987
1988
2.54k
  if (auto_global->armed) {
1989
104
    auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1990
104
  }
1991
2.54k
  return true;
1992
2.89M
}
1993
1994
ZEND_API bool zend_is_auto_global_str(const char *name, size_t len) /* {{{ */
1995
0
{
1996
0
  return zend_auto_global_check(zend_hash_str_find_ptr(CG(auto_globals), name, len));
1997
0
}
1998
/* }}} */
1999
2000
ZEND_API bool zend_is_auto_global(zend_string *name) /* {{{ */
2001
2.89M
{
2002
2.89M
  return zend_auto_global_check(zend_hash_find_ptr(CG(auto_globals), name));
2003
2.89M
}
2004
/* }}} */
2005
2006
ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
2007
128
{
2008
128
  zend_auto_global auto_global;
2009
128
  zend_result retval;
2010
2011
128
  auto_global.name = name;
2012
128
  auto_global.auto_global_callback = auto_global_callback;
2013
128
  auto_global.jit = jit;
2014
2015
128
  retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
2016
2017
128
  return retval;
2018
128
}
2019
/* }}} */
2020
2021
ZEND_API void zend_activate_auto_globals(void) /* {{{ */
2022
306k
{
2023
306k
  zend_auto_global *auto_global;
2024
2025
5.52M
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2026
5.52M
    auto_global->armed = auto_global->jit || auto_global->auto_global_callback;
2027
5.52M
  } ZEND_HASH_FOREACH_END();
2028
2029
5.52M
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2030
5.52M
    if (auto_global->armed && !auto_global->jit) {
2031
1.22M
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
2032
1.22M
    }
2033
5.52M
  } ZEND_HASH_FOREACH_END();
2034
306k
}
2035
/* }}} */
2036
2037
int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
2038
5.35M
{
2039
5.35M
  zval zv;
2040
5.35M
  int ret;
2041
2042
5.35M
  if (CG(increment_lineno)) {
2043
5.82k
    CG(zend_lineno)++;
2044
5.82k
    CG(increment_lineno) = 0;
2045
5.82k
  }
2046
2047
5.35M
  ret = lex_scan(&zv, elem);
2048
5.35M
  ZEND_ASSERT(!EG(exception) || ret == T_ERROR);
2049
5.35M
  return ret;
2050
2051
5.35M
}
2052
/* }}} */
2053
2054
ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers) /* {{{ */
2055
319k
{
2056
319k
  bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS;
2057
2058
319k
  ce->refcount = 1;
2059
319k
  ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
2060
319k
  ce->ce_flags2 = 0;
2061
2062
319k
  if (CG(compiler_options) & ZEND_COMPILE_GUARDS) {
2063
0
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2064
0
  }
2065
2066
319k
  ce->default_properties_table = NULL;
2067
319k
  ce->default_static_members_table = NULL;
2068
319k
  zend_hash_init(&ce->properties_info, 8, NULL, NULL, persistent_hashes);
2069
319k
  zend_hash_init(&ce->constants_table, 8, NULL, NULL, persistent_hashes);
2070
319k
  zend_hash_init(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes);
2071
2072
319k
  ce->doc_comment = NULL;
2073
2074
319k
  ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
2075
319k
  ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
2076
2077
319k
  ce->default_object_handlers = &std_object_handlers;
2078
319k
  ce->default_properties_count = 0;
2079
319k
  ce->default_static_members_count = 0;
2080
319k
  ce->properties_info_table = NULL;
2081
319k
  ce->attributes = NULL;
2082
319k
  ce->enum_backing_type = IS_UNDEF;
2083
319k
  ce->backed_enum_table = NULL;
2084
2085
319k
  if (nullify_handlers) {
2086
316k
    ce->constructor = NULL;
2087
316k
    ce->destructor = NULL;
2088
316k
    ce->clone = NULL;
2089
316k
    ce->__get = NULL;
2090
316k
    ce->__set = NULL;
2091
316k
    ce->__unset = NULL;
2092
316k
    ce->__isset = NULL;
2093
316k
    ce->__call = NULL;
2094
316k
    ce->__callstatic = NULL;
2095
316k
    ce->__tostring = NULL;
2096
316k
    ce->__serialize = NULL;
2097
316k
    ce->__unserialize = NULL;
2098
316k
    ce->__debugInfo = NULL;
2099
316k
    ce->create_object = NULL;
2100
316k
    ce->get_iterator = NULL;
2101
316k
    ce->iterator_funcs_ptr = NULL;
2102
316k
    ce->arrayaccess_funcs_ptr = NULL;
2103
316k
    ce->get_static_method = NULL;
2104
316k
    ce->parent = NULL;
2105
316k
    ce->parent_name = NULL;
2106
316k
    ce->num_interfaces = 0;
2107
316k
    ce->interfaces = NULL;
2108
316k
    ce->num_traits = 0;
2109
316k
    ce->num_hooked_props = 0;
2110
316k
    ce->num_hooked_prop_variance_checks = 0;
2111
316k
    ce->trait_names = NULL;
2112
316k
    ce->trait_aliases = NULL;
2113
316k
    ce->trait_precedences = NULL;
2114
316k
    ce->serialize = NULL;
2115
316k
    ce->unserialize = NULL;
2116
316k
    if (ce->type == ZEND_INTERNAL_CLASS) {
2117
0
      ce->info.internal.module = NULL;
2118
0
      ce->info.internal.builtin_functions = NULL;
2119
0
    }
2120
316k
  }
2121
319k
}
2122
/* }}} */
2123
2124
ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
2125
0
{
2126
0
  return op_array->vars[EX_VAR_TO_NUM(var)];
2127
0
}
2128
/* }}} */
2129
2130
zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
2131
0
{
2132
0
  zval *left_zv = zend_ast_get_zval(left_ast);
2133
0
  zend_string *left = Z_STR_P(left_zv);
2134
0
  zend_string *right = zend_ast_get_str(right_ast);
2135
2136
0
  zend_string *result;
2137
0
  size_t left_len = ZSTR_LEN(left);
2138
0
  size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
2139
2140
0
  result = zend_string_extend(left, len, 0);
2141
0
  ZSTR_VAL(result)[left_len] = '\\';
2142
0
  memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
2143
0
  ZSTR_VAL(result)[len] = '\0';
2144
0
  zend_string_release_ex(right, 0);
2145
2146
0
  ZVAL_STR(left_zv, result);
2147
0
  return left_ast;
2148
0
}
2149
/* }}} */
2150
2151
zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
2152
1.30k
{
2153
1.30k
  zval *zv = zend_ast_get_zval(ast);
2154
1.30k
  if (Z_TYPE_P(zv) == IS_LONG) {
2155
1.05k
    if (Z_LVAL_P(zv) == 0) {
2156
616
      ZVAL_NEW_STR(zv, ZSTR_INIT_LITERAL("-0", 0));
2157
616
    } else {
2158
437
      ZEND_ASSERT(Z_LVAL_P(zv) > 0);
2159
437
      Z_LVAL_P(zv) *= -1;
2160
437
    }
2161
1.05k
  } else if (Z_TYPE_P(zv) == IS_STRING) {
2162
247
    size_t orig_len = Z_STRLEN_P(zv);
2163
247
    Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0);
2164
247
    memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
2165
247
    Z_STRVAL_P(zv)[0] = '-';
2166
247
  } else {
2167
0
    ZEND_UNREACHABLE();
2168
0
  }
2169
1.30k
  return ast;
2170
1.30k
}
2171
/* }}} */
2172
2173
static void zend_verify_namespace(void) /* {{{ */
2174
233k
{
2175
233k
  if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
2176
47
    zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
2177
47
  }
2178
233k
}
2179
/* }}} */
2180
2181
/* {{{ zend_dirname
2182
   Returns directory name component of path */
2183
ZEND_API size_t zend_dirname(char *path, size_t len)
2184
1.26k
{
2185
1.26k
  char *end = path + len - 1;
2186
1.26k
  unsigned int len_adjust = 0;
2187
2188
#ifdef ZEND_WIN32
2189
  /* Note that on Win32 CWD is per drive (heritage from CP/M).
2190
   * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
2191
   */
2192
  if ((2 <= len) && isalpha((unsigned char)path[0]) && (':' == path[1])) {
2193
    /* Skip over the drive spec (if any) so as not to change */
2194
    path += 2;
2195
    len_adjust += 2;
2196
    if (2 == len) {
2197
      /* Return "c:" on Win32 for dirname("c:").
2198
       * It would be more consistent to return "c:."
2199
       * but that would require making the string *longer*.
2200
       */
2201
      return len;
2202
    }
2203
  }
2204
#endif
2205
2206
1.26k
  if (len == 0) {
2207
    /* Illegal use of this function */
2208
0
    return 0;
2209
0
  }
2210
2211
  /* Strip trailing slashes */
2212
1.26k
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2213
0
    end--;
2214
0
  }
2215
1.26k
  if (end < path) {
2216
    /* The path only contained slashes */
2217
0
    path[0] = DEFAULT_SLASH;
2218
0
    path[1] = '\0';
2219
0
    return 1 + len_adjust;
2220
0
  }
2221
2222
  /* Strip filename */
2223
14.0k
  while (end >= path && !IS_SLASH_P_EX(end, end == path)) {
2224
12.7k
    end--;
2225
12.7k
  }
2226
1.26k
  if (end < path) {
2227
    /* No slash found, therefore return '.' */
2228
1.22k
    path[0] = '.';
2229
1.22k
    path[1] = '\0';
2230
1.22k
    return 1 + len_adjust;
2231
1.22k
  }
2232
2233
  /* Strip slashes which came before the file name */
2234
84
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2235
42
    end--;
2236
42
  }
2237
42
  if (end < path) {
2238
0
    path[0] = DEFAULT_SLASH;
2239
0
    path[1] = '\0';
2240
0
    return 1 + len_adjust;
2241
0
  }
2242
42
  *(end+1) = '\0';
2243
2244
42
  return (size_t)(end + 1 - path) + len_adjust;
2245
42
}
2246
/* }}} */
2247
2248
static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */
2249
1.99M
{
2250
1.99M
  uint_fast8_t factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3;
2251
2252
1.99M
  switch (type) {
2253
529k
    case BP_VAR_R:
2254
529k
      opline->result_type = IS_TMP_VAR;
2255
529k
      result->op_type = IS_TMP_VAR;
2256
529k
      return;
2257
688k
    case BP_VAR_W:
2258
688k
      opline->opcode += 1 * factor;
2259
688k
      return;
2260
16.7k
    case BP_VAR_RW:
2261
16.7k
      opline->opcode += 2 * factor;
2262
16.7k
      return;
2263
634k
    case BP_VAR_IS:
2264
634k
      opline->result_type = IS_TMP_VAR;
2265
634k
      result->op_type = IS_TMP_VAR;
2266
634k
      opline->opcode += 3 * factor;
2267
634k
      return;
2268
118k
    case BP_VAR_FUNC_ARG:
2269
118k
      opline->opcode += 4 * factor;
2270
118k
      return;
2271
3.01k
    case BP_VAR_UNSET:
2272
3.01k
      opline->opcode += 5 * factor;
2273
3.01k
      return;
2274
0
    default: ZEND_UNREACHABLE();
2275
1.99M
  }
2276
1.99M
}
2277
/* }}} */
2278
2279
static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
2280
18.8M
{
2281
18.8M
  opline->result_type = IS_VAR;
2282
18.8M
  opline->result.var = get_temporary_variable();
2283
18.8M
  GET_NODE(result, opline->result);
2284
18.8M
}
2285
/* }}} */
2286
2287
static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
2288
32.1M
{
2289
32.1M
  opline->result_type = IS_TMP_VAR;
2290
32.1M
  opline->result.var = get_temporary_variable();
2291
32.1M
  GET_NODE(result, opline->result);
2292
32.1M
}
2293
/* }}} */
2294
2295
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2296
56.5M
{
2297
56.5M
  zend_op *opline = get_next_op();
2298
56.5M
  opline->opcode = opcode;
2299
2300
56.5M
  if (op1 != NULL) {
2301
36.4M
    SET_NODE(opline->op1, op1);
2302
36.4M
  }
2303
2304
56.5M
  if (op2 != NULL) {
2305
1.82M
    SET_NODE(opline->op2, op2);
2306
1.82M
  }
2307
2308
56.5M
  if (result) {
2309
17.0M
    zend_make_var_result(result, opline);
2310
17.0M
  }
2311
56.5M
  return opline;
2312
56.5M
}
2313
/* }}} */
2314
2315
static zend_op *zend_emit_op_tmp(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2316
34.9M
{
2317
34.9M
  zend_op *opline = get_next_op();
2318
34.9M
  opline->opcode = opcode;
2319
2320
34.9M
  if (op1 != NULL) {
2321
13.0M
    SET_NODE(opline->op1, op1);
2322
13.0M
  }
2323
2324
34.9M
  if (op2 != NULL) {
2325
6.62M
    SET_NODE(opline->op2, op2);
2326
6.62M
  }
2327
2328
34.9M
  if (result) {
2329
32.1M
    zend_make_tmp_result(result, opline);
2330
32.1M
  }
2331
2332
34.9M
  return opline;
2333
34.9M
}
2334
/* }}} */
2335
2336
static void zend_emit_tick(void) /* {{{ */
2337
84.6k
{
2338
84.6k
  zend_op *opline;
2339
2340
  /* This prevents a double TICK generated by the parser statement of "declare()" */
2341
84.6k
  if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
2342
9.85k
    return;
2343
9.85k
  }
2344
2345
74.7k
  opline = get_next_op();
2346
2347
74.7k
  opline->opcode = ZEND_TICKS;
2348
74.7k
  opline->extended_value = FC(declarables).ticks;
2349
74.7k
}
2350
/* }}} */
2351
2352
static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
2353
231k
{
2354
231k
  return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
2355
231k
}
2356
/* }}} */
2357
2358
static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
2359
1.11M
{
2360
1.11M
  uint32_t opnum = get_next_op_number();
2361
1.11M
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
2362
1.11M
  opline->op1.opline_num = opnum_target;
2363
1.11M
  return opnum;
2364
1.11M
}
2365
/* }}} */
2366
2367
ZEND_API bool zend_is_smart_branch(const zend_op *opline) /* {{{ */
2368
166k
{
2369
166k
  switch (opline->opcode) {
2370
5.57k
    case ZEND_IS_IDENTICAL:
2371
5.88k
    case ZEND_IS_NOT_IDENTICAL:
2372
16.1k
    case ZEND_IS_EQUAL:
2373
16.3k
    case ZEND_IS_NOT_EQUAL:
2374
124k
    case ZEND_IS_SMALLER:
2375
124k
    case ZEND_IS_SMALLER_OR_EQUAL:
2376
131k
    case ZEND_CASE:
2377
134k
    case ZEND_CASE_STRICT:
2378
134k
    case ZEND_ISSET_ISEMPTY_CV:
2379
134k
    case ZEND_ISSET_ISEMPTY_VAR:
2380
135k
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2381
135k
    case ZEND_ISSET_ISEMPTY_PROP_OBJ:
2382
135k
    case ZEND_ISSET_ISEMPTY_STATIC_PROP:
2383
135k
    case ZEND_INSTANCEOF:
2384
135k
    case ZEND_TYPE_CHECK:
2385
136k
    case ZEND_DEFINED:
2386
136k
    case ZEND_IN_ARRAY:
2387
136k
    case ZEND_ARRAY_KEY_EXISTS:
2388
136k
      return 1;
2389
30.0k
    default:
2390
30.0k
      return 0;
2391
166k
  }
2392
166k
}
2393
/* }}} */
2394
2395
static inline uint32_t zend_emit_cond_jump(uint8_t opcode, znode *cond, uint32_t opnum_target) /* {{{ */
2396
282k
{
2397
282k
  uint32_t opnum = get_next_op_number();
2398
282k
  zend_op *opline;
2399
2400
282k
  if (cond->op_type == IS_TMP_VAR && opnum > 0) {
2401
167k
    opline = CG(active_op_array)->opcodes + opnum - 1;
2402
167k
    if (opline->result_type == IS_TMP_VAR
2403
154k
     && opline->result.var == cond->u.op.var
2404
154k
     && zend_is_smart_branch(opline)) {
2405
136k
      if (opcode == ZEND_JMPZ) {
2406
109k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ;
2407
109k
      } else {
2408
26.7k
        ZEND_ASSERT(opcode == ZEND_JMPNZ);
2409
26.7k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ;
2410
26.7k
      }
2411
136k
    }
2412
167k
  }
2413
282k
  opline = zend_emit_op(NULL, opcode, cond, NULL);
2414
282k
  opline->op2.opline_num = opnum_target;
2415
282k
  return opnum;
2416
282k
}
2417
/* }}} */
2418
2419
static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
2420
1.48M
{
2421
1.48M
  zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
2422
1.48M
  switch (opline->opcode) {
2423
1.11M
    case ZEND_JMP:
2424
1.11M
      opline->op1.opline_num = opnum_target;
2425
1.11M
      break;
2426
227k
    case ZEND_JMPZ:
2427
255k
    case ZEND_JMPNZ:
2428
259k
    case ZEND_JMPZ_EX:
2429
261k
    case ZEND_JMPNZ_EX:
2430
263k
    case ZEND_JMP_SET:
2431
368k
    case ZEND_COALESCE:
2432
368k
    case ZEND_JMP_NULL:
2433
369k
    case ZEND_BIND_INIT_STATIC_OR_JMP:
2434
369k
    case ZEND_JMP_FRAMELESS:
2435
369k
      opline->op2.opline_num = opnum_target;
2436
369k
      break;
2437
0
    default: ZEND_UNREACHABLE();
2438
1.48M
  }
2439
1.48M
}
2440
/* }}} */
2441
2442
static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
2443
1.48M
{
2444
1.48M
  zend_update_jump_target(opnum_jump, get_next_op_number());
2445
1.48M
}
2446
/* }}} */
2447
2448
static inline zend_op *zend_delayed_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2449
1.74M
{
2450
1.74M
  zend_op tmp_opline;
2451
2452
1.74M
  init_op(&tmp_opline);
2453
2454
1.74M
  tmp_opline.opcode = opcode;
2455
1.74M
  if (op1 != NULL) {
2456
1.74M
    SET_NODE(tmp_opline.op1, op1);
2457
1.74M
  }
2458
1.74M
  if (op2 != NULL) {
2459
1.66M
    SET_NODE(tmp_opline.op2, op2);
2460
1.66M
  }
2461
1.74M
  if (result) {
2462
1.74M
    zend_make_var_result(result, &tmp_opline);
2463
1.74M
  }
2464
2465
1.74M
  zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
2466
1.74M
  return zend_stack_top(&CG(delayed_oplines_stack));
2467
1.74M
}
2468
/* }}} */
2469
2470
static zend_always_inline uint32_t zend_delayed_oplines_stack_size(void)
2471
2.41M
{
2472
2.41M
  return zend_stack_count(&CG(delayed_oplines_stack));
2473
2.41M
}
2474
2475
static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
2476
1.23M
{
2477
1.23M
  return zend_delayed_oplines_stack_size();
2478
1.23M
}
2479
/* }}} */
2480
2481
static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
2482
1.23M
{
2483
1.23M
  zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
2484
1.23M
  uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
2485
2486
1.23M
  ZEND_ASSERT(count >= offset);
2487
2.97M
  for (i = offset; i < count; ++i) {
2488
1.74M
    if (EXPECTED(oplines[i].opcode != ZEND_NOP)) {
2489
1.69M
      opline = get_next_op();
2490
1.69M
      memcpy(opline, &oplines[i], sizeof(zend_op));
2491
1.69M
    } else {
2492
48.5k
      opline = CG(active_op_array)->opcodes + oplines[i].extended_value;
2493
48.5k
    }
2494
1.74M
  }
2495
2496
1.23M
  CG(delayed_oplines_stack).top = offset;
2497
1.23M
  return opline;
2498
1.23M
}
2499
/* }}} */
2500
2501
static bool zend_ast_kind_is_short_circuited(zend_ast_kind ast_kind)
2502
68.8M
{
2503
68.8M
  switch (ast_kind) {
2504
637k
    case ZEND_AST_DIM:
2505
1.66M
    case ZEND_AST_PROP:
2506
1.81M
    case ZEND_AST_NULLSAFE_PROP:
2507
1.84M
    case ZEND_AST_STATIC_PROP:
2508
2.31M
    case ZEND_AST_METHOD_CALL:
2509
2.33M
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2510
2.36M
    case ZEND_AST_STATIC_CALL:
2511
2.36M
      return true;
2512
66.4M
    default:
2513
66.4M
      return false;
2514
68.8M
  }
2515
68.8M
}
2516
2517
static bool zend_ast_is_short_circuited(const zend_ast *ast)
2518
2.11M
{
2519
2.11M
  switch (ast->kind) {
2520
285k
    case ZEND_AST_DIM:
2521
753k
    case ZEND_AST_PROP:
2522
765k
    case ZEND_AST_STATIC_PROP:
2523
846k
    case ZEND_AST_METHOD_CALL:
2524
851k
    case ZEND_AST_STATIC_CALL:
2525
851k
      return zend_ast_is_short_circuited(ast->child[0]);
2526
625
    case ZEND_AST_NULLSAFE_PROP:
2527
889
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2528
889
      return true;
2529
1.26M
    default:
2530
1.26M
      return false;
2531
2.11M
  }
2532
2.11M
}
2533
2534
static void zend_assert_not_short_circuited(const zend_ast *ast)
2535
129k
{
2536
129k
  if (zend_ast_is_short_circuited(ast)) {
2537
31
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain");
2538
31
  }
2539
129k
}
2540
2541
/* Mark nodes that are an inner part of a short-circuiting chain.
2542
 * We should not perform a "commit" on them, as it will be performed by the outer-most node.
2543
 * We do this to avoid passing down an argument in various compile functions. */
2544
2545
2.37M
#define ZEND_SHORT_CIRCUITING_INNER 0x8000
2546
2547
1.82M
static void zend_short_circuiting_mark_inner(zend_ast *ast) {
2548
1.82M
  if (zend_ast_kind_is_short_circuited(ast->kind)) {
2549
1.40M
    ast->attr |= ZEND_SHORT_CIRCUITING_INNER;
2550
1.40M
  }
2551
1.82M
}
2552
2553
static uint32_t zend_short_circuiting_checkpoint(void)
2554
67.1M
{
2555
67.1M
  return zend_stack_count(&CG(short_circuiting_opnums));
2556
67.1M
}
2557
2558
static void zend_short_circuiting_commit(uint32_t checkpoint, znode *result, const zend_ast *ast)
2559
66.9M
{
2560
66.9M
  bool is_short_circuited = zend_ast_kind_is_short_circuited(ast->kind)
2561
66.0M
    || ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY;
2562
66.9M
  if (!is_short_circuited) {
2563
66.0M
    ZEND_ASSERT(zend_stack_count(&CG(short_circuiting_opnums)) == checkpoint
2564
66.0M
      && "Short circuiting stack should be empty");
2565
66.0M
    return;
2566
66.0M
  }
2567
2568
972k
  if (ast->attr & ZEND_SHORT_CIRCUITING_INNER) {
2569
    /* Outer-most node will commit. */
2570
274k
    return;
2571
274k
  }
2572
2573
794k
  while (zend_stack_count(&CG(short_circuiting_opnums)) != checkpoint) {
2574
96.0k
    uint32_t opnum = *(uint32_t *) zend_stack_top(&CG(short_circuiting_opnums));
2575
96.0k
    zend_op *opline = &CG(active_op_array)->opcodes[opnum];
2576
96.0k
    opline->op2.opline_num = get_next_op_number();
2577
96.0k
    SET_NODE(opline->result, result);
2578
96.0k
    opline->extended_value |=
2579
96.0k
      ast->kind == ZEND_AST_ISSET ? ZEND_SHORT_CIRCUITING_CHAIN_ISSET :
2580
96.0k
      ast->kind == ZEND_AST_EMPTY ? ZEND_SHORT_CIRCUITING_CHAIN_EMPTY :
2581
95.8k
                                    ZEND_SHORT_CIRCUITING_CHAIN_EXPR;
2582
96.0k
    zend_stack_del_top(&CG(short_circuiting_opnums));
2583
96.0k
  }
2584
698k
}
2585
2586
/* Same value as ZEND_SHORT_CIRCUITING_INNER, AST kinds must not clash. */
2587
210k
#define ZEND_CONST_OBJECT_FETCH 0x8000
2588
2589
static void zend_mark_const_object_fetch(zend_ast *ast)
2590
1.08M
{
2591
1.08M
  while (ast->kind == ZEND_AST_DIM) {
2592
4.79k
    ast = ast->child[0];
2593
4.79k
  }
2594
2595
1.08M
  if (ast->kind == ZEND_AST_CONST || ast->kind == ZEND_AST_CLASS_CONST) {
2596
91.0k
    ast->attr |= ZEND_CONST_OBJECT_FETCH;
2597
91.0k
  }
2598
1.08M
}
2599
2600
static void zend_emit_jmp_null(znode *obj_node, uint32_t bp_type)
2601
96.0k
{
2602
96.0k
  uint32_t jmp_null_opnum = get_next_op_number();
2603
96.0k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP_NULL, obj_node, NULL);
2604
96.0k
  if (opline->op1_type == IS_CONST) {
2605
1.00k
    Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
2606
1.00k
  }
2607
96.0k
  if (bp_type == BP_VAR_IS) {
2608
16.3k
    opline->extended_value |= ZEND_JMP_NULL_BP_VAR_IS;
2609
16.3k
  }
2610
96.0k
  zend_stack_push(&CG(short_circuiting_opnums), &jmp_null_opnum);
2611
96.0k
}
2612
2613
static inline bool zend_is_variable_or_call(const zend_ast *ast);
2614
2615
static void zend_compile_memoized_expr(znode *result, zend_ast *expr, uint32_t type) /* {{{ */
2616
1.39M
{
2617
1.39M
  const zend_memoize_mode memoize_mode = CG(memoize_mode);
2618
1.39M
  if (memoize_mode == ZEND_MEMOIZE_COMPILE) {
2619
696k
    znode memoized_result;
2620
2621
    /* Go through normal compilation */
2622
696k
    CG(memoize_mode) = ZEND_MEMOIZE_NONE;
2623
696k
    if (zend_is_variable_or_call(expr)) {
2624
87.0k
      zend_compile_var(result, expr, type, /* by_ref */ false);
2625
609k
    } else {
2626
609k
      zend_compile_expr(result, expr);
2627
609k
    }
2628
696k
    CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
2629
2630
696k
    if (result->op_type == IS_VAR) {
2631
82.2k
      zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL);
2632
614k
    } else if (result->op_type == IS_TMP_VAR) {
2633
156k
      zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL);
2634
457k
    } else {
2635
457k
      if (result->op_type == IS_CONST) {
2636
456k
        Z_TRY_ADDREF(result->u.constant);
2637
456k
      }
2638
457k
      memoized_result = *result;
2639
457k
    }
2640
2641
696k
    zend_hash_index_update_mem(
2642
696k
      CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode));
2643
696k
  } else if (memoize_mode == ZEND_MEMOIZE_FETCH) {
2644
695k
    const znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr);
2645
695k
    *result = *memoized_result;
2646
695k
    if (result->op_type == IS_CONST) {
2647
456k
      Z_TRY_ADDREF(result->u.constant);
2648
456k
    }
2649
695k
  } else {
2650
0
    ZEND_UNREACHABLE();
2651
0
  }
2652
1.39M
}
2653
/* }}} */
2654
2655
static void zend_emit_return_type_check(
2656
    znode *expr, const zend_arg_info *return_info, bool implicit) /* {{{ */
2657
83.0k
{
2658
83.0k
  zend_type type = return_info->type;
2659
83.0k
  if (ZEND_TYPE_IS_SET(type)) {
2660
83.0k
    zend_op *opline;
2661
2662
    /* `return ...;` is illegal in a void function (but `return;` isn't) */
2663
83.0k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) {
2664
2.84k
      if (expr) {
2665
3
        if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
2666
1
          zend_error_noreturn(E_COMPILE_ERROR,
2667
1
            "A void %s must not return a value "
2668
1
            "(did you mean \"return;\" instead of \"return null;\"?)",
2669
1
            CG(active_class_entry) != NULL ? "method" : "function");
2670
2
        } else {
2671
2
          zend_error_noreturn(E_COMPILE_ERROR, "A void %s must not return a value",
2672
2
          CG(active_class_entry) != NULL ? "method" : "function");
2673
2
        }
2674
3
      }
2675
      /* we don't need run-time check */
2676
2.84k
      return;
2677
2.84k
    }
2678
2679
    /* `return` is illegal in a never-returning function */
2680
80.2k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_NEVER)) {
2681
      /* Implicit case handled separately using VERIFY_NEVER_TYPE opcode. */
2682
1
      ZEND_ASSERT(!implicit);
2683
1
      zend_error_noreturn(E_COMPILE_ERROR, "A never-returning %s must not return",
2684
1
        CG(active_class_entry) != NULL ? "method" : "function");
2685
1
    }
2686
2687
80.2k
    if (!expr && !implicit) {
2688
8
      if (ZEND_TYPE_ALLOW_NULL(type)) {
2689
1
        zend_error_noreturn(E_COMPILE_ERROR,
2690
1
          "A %s with return type must return a value "
2691
1
          "(did you mean \"return null;\" instead of \"return;\"?)",
2692
1
          CG(active_class_entry) != NULL ? "method" : "function");
2693
7
      } else {
2694
7
        zend_error_noreturn(E_COMPILE_ERROR,
2695
7
          "A %s with return type must return a value",
2696
7
          CG(active_class_entry) != NULL ? "method" : "function");
2697
7
      }
2698
8
    }
2699
2700
80.2k
    if (expr && ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) {
2701
      /* we don't need run-time check for mixed return type */
2702
113
      return;
2703
113
    }
2704
2705
80.1k
    if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) {
2706
      /* we don't need run-time check */
2707
1.49k
      return;
2708
1.49k
    }
2709
2710
78.6k
    opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
2711
78.6k
    if (expr && expr->op_type == IS_CONST) {
2712
603
      opline->result_type = expr->op_type = IS_TMP_VAR;
2713
603
      opline->result.var = expr->u.op.var = get_temporary_variable();
2714
603
    }
2715
78.6k
  }
2716
83.0k
}
2717
/* }}} */
2718
2719
void zend_emit_final_return(bool return_one) /* {{{ */
2720
2.14M
{
2721
2.14M
  znode zn;
2722
2.14M
  zend_op *ret;
2723
2.14M
  bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
2724
2725
2.14M
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)
2726
78.2k
      && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) {
2727
77.6k
    zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
2728
2729
77.6k
    if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
2730
925
      zend_emit_op(NULL, ZEND_VERIFY_NEVER_TYPE, NULL, NULL);
2731
925
      return;
2732
925
    }
2733
2734
76.7k
    zend_emit_return_type_check(NULL, return_info, true);
2735
76.7k
  }
2736
2737
2.14M
  zn.op_type = IS_CONST;
2738
2.14M
  if (return_one) {
2739
52.6k
    ZVAL_LONG(&zn.u.constant, 1);
2740
2.09M
  } else {
2741
2.09M
    ZVAL_NULL(&zn.u.constant);
2742
2.09M
  }
2743
2744
2.14M
  ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
2745
2.14M
  ret->extended_value = -1;
2746
2.14M
}
2747
/* }}} */
2748
2749
static inline bool zend_is_variable(const zend_ast *ast) /* {{{ */
2750
6.75M
{
2751
6.75M
  return ast->kind == ZEND_AST_VAR
2752
6.60M
    || ast->kind == ZEND_AST_DIM
2753
6.53M
    || ast->kind == ZEND_AST_PROP
2754
6.53M
    || ast->kind == ZEND_AST_NULLSAFE_PROP
2755
6.52M
    || ast->kind == ZEND_AST_STATIC_PROP;
2756
6.75M
}
2757
/* }}} */
2758
2759
static bool zend_propagate_list_refs(zend_ast *ast);
2760
2761
static inline bool zend_is_passable_by_ref(const zend_ast *ast)
2762
5.89M
{
2763
5.89M
  if (zend_is_variable(ast) || ast->kind == ZEND_AST_ASSIGN_REF) {
2764
101k
    return true;
2765
101k
  }
2766
5.79M
  if (ast->kind == ZEND_AST_ASSIGN
2767
13.9k
   && UNEXPECTED(ast->child[0]->kind == ZEND_AST_ARRAY)
2768
38
   && zend_propagate_list_refs(ast->child[0])) {
2769
10
    return true;
2770
10
  }
2771
5.79M
  return false;
2772
5.79M
}
2773
2774
static inline bool zend_is_call(const zend_ast *ast) /* {{{ */
2775
7.59M
{
2776
7.59M
  return ast->kind == ZEND_AST_CALL
2777
7.33M
    || ast->kind == ZEND_AST_METHOD_CALL
2778
7.17M
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
2779
7.17M
    || ast->kind == ZEND_AST_STATIC_CALL
2780
7.16M
    || ast->kind == ZEND_AST_PIPE;
2781
7.59M
}
2782
/* }}} */
2783
2784
static inline bool zend_is_variable_or_call(const zend_ast *ast) /* {{{ */
2785
826k
{
2786
826k
  return zend_is_variable(ast) || zend_is_call(ast);
2787
826k
}
2788
/* }}} */
2789
2790
static inline bool zend_is_unticked_stmt(const zend_ast *ast) /* {{{ */
2791
106k
{
2792
106k
  return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL
2793
85.6k
    || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP
2794
85.4k
    || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD;
2795
106k
}
2796
/* }}} */
2797
2798
static inline bool zend_can_write_to_variable(const zend_ast *ast) /* {{{ */
2799
63.8k
{
2800
63.8k
  bool via_prop = false;
2801
2802
63.8k
  while (
2803
74.2k
    ast->kind == ZEND_AST_DIM
2804
69.7k
    || ast->kind == ZEND_AST_PROP
2805
63.8k
  ) {
2806
10.3k
    via_prop |= ast->kind == ZEND_AST_PROP;
2807
10.3k
    ast = ast->child[0];
2808
10.3k
  }
2809
2810
63.8k
  if ((ast->kind == ZEND_AST_CONST || ast->kind == ZEND_AST_CLASS_CONST) && via_prop) {
2811
725
    return true;
2812
725
  }
2813
2814
63.1k
  return zend_is_variable_or_call(ast) && !zend_ast_is_short_circuited(ast);
2815
63.8k
}
2816
/* }}} */
2817
2818
static inline bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
2819
10.6k
{
2820
10.6k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2821
0
    return false;
2822
0
  }
2823
2824
10.6k
  return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
2825
10.6k
}
2826
/* }}} */
2827
2828
static inline void zend_handle_numeric_op(znode *node) /* {{{ */
2829
12.2k
{
2830
12.2k
  if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
2831
9.52k
    zend_ulong index;
2832
2833
9.52k
    if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
2834
3.68k
      zval_ptr_dtor(&node->u.constant);
2835
3.68k
      ZVAL_LONG(&node->u.constant, index);
2836
3.68k
    }
2837
9.52k
  }
2838
12.2k
}
2839
/* }}} */
2840
2841
static inline void zend_handle_numeric_dim(const zend_op *opline, znode *dim_node) /* {{{ */
2842
289k
{
2843
289k
  if (Z_TYPE(dim_node->u.constant) == IS_STRING) {
2844
186k
    zend_ulong index;
2845
2846
186k
    if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) {
2847
      /* For numeric indexes we also keep the original value to use by ArrayAccess
2848
       * See bug #63217
2849
       */
2850
22.4k
      int c = zend_add_literal(&dim_node->u.constant);
2851
22.4k
      ZEND_ASSERT(opline->op2.constant + 1 == c);
2852
22.4k
      ZVAL_LONG(CT_CONSTANT(opline->op2), index);
2853
22.4k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE;
2854
22.4k
      return;
2855
22.4k
    }
2856
186k
  }
2857
289k
}
2858
/* }}} */
2859
2860
static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
2861
139k
{
2862
139k
  if (class_node->op_type == IS_CONST) {
2863
41.3k
    opline->op1_type = IS_CONST;
2864
41.3k
    opline->op1.constant = zend_add_class_name_literal(
2865
41.3k
      Z_STR(class_node->u.constant));
2866
97.7k
  } else {
2867
97.7k
    SET_NODE(opline->op1, class_node);
2868
97.7k
  }
2869
139k
}
2870
/* }}} */
2871
2872
static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */
2873
101k
{
2874
101k
  uint32_t fetch_type;
2875
2876
101k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2877
45.2k
    znode name_node;
2878
2879
45.2k
    zend_compile_expr(&name_node, name_ast);
2880
2881
45.2k
    if (name_node.op_type == IS_CONST) {
2882
2.90k
      zend_string *name;
2883
2884
2.90k
      if (Z_TYPE(name_node.u.constant) != IS_STRING) {
2885
15
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
2886
15
      }
2887
2888
2.90k
      name = Z_STR(name_node.u.constant);
2889
2.90k
      fetch_type = zend_get_class_fetch_type(name);
2890
2891
2.90k
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
2892
1.98k
        result->op_type = IS_CONST;
2893
1.98k
        ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ));
2894
1.98k
      } else {
2895
911
        zend_ensure_valid_class_fetch_type(fetch_type);
2896
911
        result->op_type = IS_UNUSED;
2897
911
        result->u.op.num = fetch_type | fetch_flags;
2898
911
      }
2899
2900
2.90k
      zend_string_release_ex(name, 0);
2901
42.3k
    } else {
2902
42.3k
      zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
2903
42.3k
      opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags;
2904
42.3k
    }
2905
45.2k
    return;
2906
45.2k
  }
2907
2908
  /* Fully qualified names are always default refs */
2909
56.4k
  if (name_ast->attr == ZEND_NAME_FQ) {
2910
6.30k
    result->op_type = IS_CONST;
2911
6.30k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2912
6.30k
    return;
2913
6.30k
  }
2914
2915
50.1k
  fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
2916
50.1k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
2917
44.4k
    result->op_type = IS_CONST;
2918
44.4k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2919
44.4k
  } else {
2920
5.65k
    zend_ensure_valid_class_fetch_type(fetch_type);
2921
5.65k
    result->op_type = IS_UNUSED;
2922
5.65k
    result->u.op.num = fetch_type | fetch_flags;
2923
5.65k
  }
2924
50.1k
}
2925
/* }}} */
2926
2927
static zend_result zend_try_compile_cv(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
2928
1.47M
{
2929
1.47M
  zend_ast *name_ast = ast->child[0];
2930
1.47M
  if (name_ast->kind == ZEND_AST_ZVAL) {
2931
1.25M
    zval *zv = zend_ast_get_zval(name_ast);
2932
1.25M
    zend_string *name;
2933
2934
1.25M
    if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
2935
1.17M
      name = zval_make_interned_string(zv);
2936
1.17M
    } else {
2937
78.4k
      name = zend_new_interned_string(zval_get_string_func(zv));
2938
78.4k
    }
2939
2940
1.25M
    if (zend_is_auto_global(name)) {
2941
732
      return FAILURE;
2942
732
    }
2943
2944
1.25M
    if (!CG(context).has_assigned_to_http_response_header && zend_string_equals_literal(name, "http_response_header")) {
2945
461
      if (type == BP_VAR_R) {
2946
196
        zend_error(E_DEPRECATED,
2947
196
          "The predefined locally scoped $http_response_header variable is deprecated,"
2948
196
          " call http_get_last_response_headers() instead");
2949
265
      } else if (type == BP_VAR_W) {
2950
196
        CG(context).has_assigned_to_http_response_header = true;
2951
196
      }
2952
461
    }
2953
2954
1.25M
    result->op_type = IS_CV;
2955
1.25M
    result->u.op.var = lookup_cv(name);
2956
2957
1.25M
    if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) {
2958
78.4k
      zend_string_release_ex(name, 0);
2959
78.4k
    }
2960
2961
1.25M
    return SUCCESS;
2962
1.25M
  }
2963
2964
214k
  return FAILURE;
2965
1.47M
}
2966
/* }}} */
2967
2968
static zend_op *zend_compile_simple_var_no_cv(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2969
303k
{
2970
303k
  zend_ast *name_ast = ast->child[0];
2971
303k
  znode name_node;
2972
303k
  zend_op *opline;
2973
2974
303k
  zend_compile_expr(&name_node, name_ast);
2975
303k
  if (name_node.op_type == IS_CONST) {
2976
111k
    convert_to_string(&name_node.u.constant);
2977
111k
  }
2978
2979
303k
  if (delayed) {
2980
65.9k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2981
237k
  } else {
2982
237k
    opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2983
237k
  }
2984
2985
303k
  if (name_node.op_type == IS_CONST &&
2986
111k
      zend_is_auto_global(Z_STR(name_node.u.constant))) {
2987
2988
709
    opline->extended_value = ZEND_FETCH_GLOBAL;
2989
302k
  } else {
2990
302k
    if (name_node.op_type == IS_CONST
2991
110k
      && type == BP_VAR_R
2992
108k
      && zend_string_equals_literal(Z_STR(name_node.u.constant), "http_response_header")) {
2993
66
      zend_error(E_DEPRECATED,
2994
66
        "The predefined locally scoped $http_response_header variable is deprecated,"
2995
66
        " call http_get_last_response_headers() instead");
2996
66
    }
2997
302k
    opline->extended_value = ZEND_FETCH_LOCAL;
2998
302k
  }
2999
3000
303k
  zend_adjust_for_fetch_type(opline, result, type);
3001
303k
  return opline;
3002
303k
}
3003
/* }}} */
3004
3005
static bool is_this_fetch(const zend_ast *ast) /* {{{ */
3006
3.51M
{
3007
3.51M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
3008
1.92M
    const zval *name = zend_ast_get_zval(ast->child[0]);
3009
1.92M
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals(Z_STR_P(name), ZSTR_KNOWN(ZEND_STR_THIS));
3010
1.92M
  }
3011
3012
1.58M
  return false;
3013
3.51M
}
3014
/* }}} */
3015
3016
static bool is_globals_fetch(const zend_ast *ast)
3017
9.31M
{
3018
9.31M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
3019
2.08M
    const zval *name = zend_ast_get_zval(ast->child[0]);
3020
2.08M
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "GLOBALS");
3021
2.08M
  }
3022
3023
7.23M
  return false;
3024
9.31M
}
3025
3026
static bool is_global_var_fetch(const zend_ast *ast)
3027
898k
{
3028
898k
  return ast->kind == ZEND_AST_DIM && is_globals_fetch(ast->child[0]);
3029
898k
}
3030
3031
static bool this_guaranteed_exists(void) /* {{{ */
3032
14.7k
{
3033
14.7k
  const zend_oparray_context *ctx = &CG(context);
3034
79.7k
  while (ctx) {
3035
    /* Instance methods always have a $this.
3036
     * This also includes closures that have a scope and use $this. */
3037
79.7k
    const zend_op_array *op_array = ctx->op_array;
3038
79.7k
    if (op_array->fn_flags & ZEND_ACC_STATIC) {
3039
1.25k
      return false;
3040
78.4k
    } else if (op_array->scope) {
3041
5.28k
      return true;
3042
73.1k
    } else if (!(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
3043
8.25k
      return false;
3044
8.25k
    }
3045
64.9k
    ctx = ctx->prev;
3046
64.9k
  }
3047
0
  return false;
3048
14.7k
}
3049
/* }}} */
3050
3051
static zend_op *zend_compile_simple_var(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
3052
1.44M
{
3053
1.44M
  if (is_this_fetch(ast)) {
3054
4.81k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL);
3055
4.81k
    if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
3056
4.60k
      opline->result_type = IS_TMP_VAR;
3057
4.60k
      result->op_type = IS_TMP_VAR;
3058
4.60k
    }
3059
4.81k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3060
4.81k
    return opline;
3061
1.44M
  } else if (is_globals_fetch(ast)) {
3062
5.90k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_GLOBALS, NULL, NULL);
3063
5.90k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3064
5.83k
      opline->result_type = IS_TMP_VAR;
3065
5.83k
      result->op_type = IS_TMP_VAR;
3066
5.83k
    }
3067
5.90k
    return opline;
3068
1.43M
  } else if (zend_try_compile_cv(result, ast, type) == FAILURE) {
3069
212k
    return zend_compile_simple_var_no_cv(result, ast, type, delayed);
3070
212k
  }
3071
1.22M
  return NULL;
3072
1.44M
}
3073
/* }}} */
3074
3075
static void zend_separate_if_call_and_write(znode *node, const zend_ast *ast, uint32_t type) /* {{{ */
3076
1.65M
{
3077
1.65M
  if (type != BP_VAR_R
3078
1.37M
   && type != BP_VAR_IS
3079
   /* Whether a FUNC_ARG is R may only be determined at runtime. */
3080
747k
   && type != BP_VAR_FUNC_ARG
3081
631k
   && zend_is_call(ast)) {
3082
83.7k
    if (node->op_type == IS_VAR) {
3083
83.7k
      zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
3084
83.7k
      opline->result_type = IS_VAR;
3085
83.7k
      opline->result.var = opline->op1.var;
3086
83.7k
    } else {
3087
14
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3088
14
    }
3089
83.7k
  }
3090
1.65M
}
3091
/* }}} */
3092
3093
static inline void zend_emit_assign_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3094
7.74k
{
3095
7.74k
  znode dummy_node;
3096
7.74k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
3097
7.74k
    zend_ast_create_znode(value_node));
3098
7.74k
  zend_compile_expr(&dummy_node, assign_ast);
3099
7.74k
  zend_do_free(&dummy_node);
3100
7.74k
}
3101
/* }}} */
3102
3103
static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
3104
576k
{
3105
576k
  zend_ast *var_ast = ast->child[0];
3106
576k
  zend_ast *dim_ast = ast->child[1];
3107
576k
  zend_op *opline;
3108
3109
576k
  znode var_node, dim_node;
3110
3111
576k
  if (is_globals_fetch(var_ast)) {
3112
2.57k
    if (dim_ast == NULL) {
3113
11
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot append to $GLOBALS");
3114
11
    }
3115
3116
2.57k
    zend_compile_expr(&dim_node, dim_ast);
3117
2.57k
    if (dim_node.op_type == IS_CONST) {
3118
2.14k
      convert_to_string(&dim_node.u.constant);
3119
2.14k
    }
3120
3121
2.57k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &dim_node, NULL);
3122
2.57k
    opline->extended_value = ZEND_FETCH_GLOBAL;
3123
2.57k
    zend_adjust_for_fetch_type(opline, result, type);
3124
2.57k
    return opline;
3125
574k
  } else {
3126
574k
    zend_short_circuiting_mark_inner(var_ast);
3127
574k
    opline = zend_delayed_compile_var(&var_node, var_ast, type, false);
3128
574k
    if (opline) {
3129
266k
      if (type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) {
3130
861
        opline->extended_value |= ZEND_FETCH_DIM_WRITE;
3131
266k
      } else if (opline->opcode == ZEND_FETCH_DIM_W
3132
197k
          || opline->opcode == ZEND_FETCH_DIM_RW
3133
195k
          || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3134
143k
          || opline->opcode == ZEND_FETCH_DIM_UNSET) {
3135
122k
        opline->extended_value = ZEND_FETCH_DIM_DIM;
3136
122k
      }
3137
266k
    }
3138
574k
  }
3139
3140
574k
  zend_separate_if_call_and_write(&var_node, var_ast, type);
3141
3142
574k
  if (dim_ast == NULL) {
3143
15.5k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3144
459
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
3145
459
    }
3146
15.5k
    if (type == BP_VAR_UNSET) {
3147
12
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
3148
12
    }
3149
15.5k
    dim_node.op_type = IS_UNUSED;
3150
558k
  } else {
3151
558k
    zend_compile_expr(&dim_node, dim_ast);
3152
558k
  }
3153
3154
574k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
3155
574k
  zend_adjust_for_fetch_type(opline, result, type);
3156
574k
  if (by_ref) {
3157
62.1k
    opline->extended_value = ZEND_FETCH_DIM_REF;
3158
62.1k
  }
3159
3160
574k
  if (dim_node.op_type == IS_CONST) {
3161
228k
    zend_handle_numeric_dim(opline, &dim_node);
3162
228k
  }
3163
574k
  return opline;
3164
576k
}
3165
3166
static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3167
311k
{
3168
311k
  uint32_t offset = zend_delayed_compile_begin();
3169
311k
  zend_delayed_compile_dim(result, ast, type, by_ref);
3170
311k
  return zend_delayed_compile_end(offset);
3171
311k
}
3172
/* }}} */
3173
3174
static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3175
1.09M
{
3176
1.09M
  zend_ast *obj_ast = ast->child[0];
3177
1.09M
  zend_ast *prop_ast = ast->child[1];
3178
3179
1.09M
  znode obj_node, prop_node;
3180
1.09M
  zend_op *opline;
3181
1.09M
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_PROP;
3182
3183
1.09M
  if (is_this_fetch(obj_ast)) {
3184
9.32k
    if (this_guaranteed_exists()) {
3185
950
      obj_node.op_type = IS_UNUSED;
3186
8.37k
    } else {
3187
8.37k
      opline = zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
3188
8.37k
      if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
3189
7.84k
        opline->result_type = IS_TMP_VAR;
3190
7.84k
        obj_node.op_type = IS_TMP_VAR;
3191
7.84k
      }
3192
8.37k
    }
3193
9.32k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3194
3195
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
3196
     * check for a nullsafe access. */
3197
1.08M
  } else {
3198
1.08M
    uint32_t offset = zend_delayed_oplines_stack_size();
3199
1.08M
    zend_short_circuiting_mark_inner(obj_ast);
3200
1.08M
    zend_mark_const_object_fetch(obj_ast);
3201
1.08M
    opline = zend_delayed_compile_var(&obj_node, obj_ast, type, false);
3202
1.08M
    if (opline && (opline->opcode == ZEND_FETCH_DIM_W
3203
960k
        || opline->opcode == ZEND_FETCH_DIM_RW
3204
959k
        || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3205
958k
        || opline->opcode == ZEND_FETCH_DIM_UNSET)) {
3206
1.78k
      opline->extended_value = ZEND_FETCH_DIM_OBJ;
3207
1.78k
    }
3208
3209
1.08M
    zend_separate_if_call_and_write(&obj_node, obj_ast, type);
3210
1.08M
    if (nullsafe) {
3211
90.9k
      if (obj_node.op_type == IS_TMP_VAR) {
3212
        /* Flush delayed oplines */
3213
89.7k
        zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
3214
89.7k
        uint32_t var = obj_node.u.op.var;
3215
89.7k
        uint32_t count = zend_delayed_oplines_stack_size();
3216
89.7k
        uint32_t i = count;
3217
3218
        /* Only consider the oplines delayed while compiling obj_ast. */
3219
353k
        while (i > offset && oplines[i-1].result_type == IS_TMP_VAR && oplines[i-1].result.var == var) {
3220
265k
          i--;
3221
265k
          if (oplines[i].op1_type == IS_TMP_VAR) {
3222
264k
            var = oplines[i].op1.var;
3223
264k
          } else {
3224
1.90k
            break;
3225
1.90k
          }
3226
265k
        }
3227
355k
        for (; i < count; ++i) {
3228
265k
          if (oplines[i].opcode != ZEND_NOP) {
3229
48.5k
            opline = get_next_op();
3230
48.5k
            memcpy(opline, &oplines[i], sizeof(zend_op));
3231
48.5k
            oplines[i].opcode = ZEND_NOP;
3232
48.5k
            oplines[i].extended_value = opline - CG(active_op_array)->opcodes;
3233
48.5k
          }
3234
265k
        }
3235
89.7k
      }
3236
90.9k
      zend_emit_jmp_null(&obj_node, type);
3237
90.9k
    }
3238
1.08M
  }
3239
3240
1.09M
  zend_compile_expr(&prop_node, prop_ast);
3241
3242
1.09M
  opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
3243
1.09M
  if (opline->op2_type == IS_CONST) {
3244
1.09M
    convert_to_string(CT_CONSTANT(opline->op2));
3245
1.09M
    zend_string_hash_val(Z_STR_P(CT_CONSTANT(opline->op2)));
3246
1.09M
    opline->extended_value = zend_alloc_cache_slots(3);
3247
1.09M
  }
3248
3249
1.09M
  zend_adjust_for_fetch_type(opline, result, type);
3250
3251
1.09M
  return opline;
3252
1.09M
}
3253
/* }}} */
3254
3255
static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3256
129k
{
3257
129k
  uint32_t offset = zend_delayed_compile_begin();
3258
129k
  zend_op *opline = zend_delayed_compile_prop(result, ast, type);
3259
129k
  if (by_ref) { /* shared with cache_slot */
3260
3.76k
    opline->extended_value |= ZEND_FETCH_REF;
3261
3.76k
  }
3262
129k
  return zend_delayed_compile_end(offset);
3263
129k
}
3264
/* }}} */
3265
3266
static zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref, bool delayed) /* {{{ */
3267
20.3k
{
3268
20.3k
  zend_ast *class_ast = ast->child[0];
3269
20.3k
  zend_ast *prop_ast = ast->child[1];
3270
3271
20.3k
  znode class_node, prop_node;
3272
20.3k
  zend_op *opline;
3273
3274
20.3k
  zend_short_circuiting_mark_inner(class_ast);
3275
20.3k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
3276
3277
20.3k
  zend_compile_expr(&prop_node, prop_ast);
3278
3279
20.3k
  if (delayed) {
3280
9.52k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3281
10.8k
  } else {
3282
10.8k
    opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3283
10.8k
  }
3284
20.3k
  if (opline->op1_type == IS_CONST) {
3285
18.6k
    convert_to_string(CT_CONSTANT(opline->op1));
3286
18.6k
    opline->extended_value = zend_alloc_cache_slots(3);
3287
18.6k
  }
3288
20.3k
  if (class_node.op_type == IS_CONST) {
3289
11.3k
    opline->op2_type = IS_CONST;
3290
11.3k
    opline->op2.constant = zend_add_class_name_literal(
3291
11.3k
      Z_STR(class_node.u.constant));
3292
11.3k
    if (opline->op1_type != IS_CONST) {
3293
1.00k
      opline->extended_value = zend_alloc_cache_slot();
3294
1.00k
    }
3295
11.3k
  } else {
3296
9.02k
    SET_NODE(opline->op2, &class_node);
3297
9.02k
  }
3298
3299
20.3k
  if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */
3300
1.46k
    opline->extended_value |= ZEND_FETCH_REF;
3301
1.46k
  }
3302
3303
20.3k
  zend_adjust_for_fetch_type(opline, result, type);
3304
20.3k
  return opline;
3305
20.3k
}
3306
/* }}} */
3307
3308
61.7k
static void zend_verify_list_assign_target(const zend_ast *var_ast, zend_ast_attr array_style) /* {{{ */ {
3309
61.7k
  if (var_ast->kind == ZEND_AST_ARRAY) {
3310
2.10k
    if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) {
3311
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead");
3312
2
    }
3313
2.10k
    if (array_style != var_ast->attr) {
3314
4
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()");
3315
4
    }
3316
59.6k
  } else if (!zend_can_write_to_variable(var_ast)) {
3317
183
    zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values");
3318
183
  }
3319
61.7k
}
3320
/* }}} */
3321
3322
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node);
3323
3324
/* Propagate refs used on leaf elements to the surrounding list() structures. */
3325
59.0k
static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
3326
59.0k
  const zend_ast_list *list = zend_ast_get_list(ast);
3327
59.0k
  bool has_refs = false;
3328
59.0k
  uint32_t i;
3329
3330
122k
  for (i = 0; i < list->children; ++i) {
3331
63.7k
    zend_ast *elem_ast = list->child[i];
3332
3333
63.7k
    if (elem_ast) {
3334
62.4k
      zend_ast *var_ast = elem_ast->child[0];
3335
62.4k
      if (var_ast->kind == ZEND_AST_ARRAY) {
3336
2.13k
        elem_ast->attr = zend_propagate_list_refs(var_ast);
3337
2.13k
      }
3338
62.4k
      has_refs |= elem_ast->attr;
3339
62.4k
    }
3340
63.7k
  }
3341
3342
59.0k
  return has_refs;
3343
59.0k
}
3344
/* }}} */
3345
3346
static bool list_is_keyed(const zend_ast_list *list)
3347
57.9k
{
3348
58.8k
  for (uint32_t i = 0; i < list->children; i++) {
3349
58.7k
    const zend_ast *child = list->child[i];
3350
58.7k
    if (child) {
3351
57.8k
      return child->kind == ZEND_AST_ARRAY_ELEM && child->child[1] != NULL;
3352
57.8k
    }
3353
58.7k
  }
3354
64
  return false;
3355
57.9k
}
3356
3357
static void zend_compile_list_assign(
3358
    znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style, uint32_t type) /* {{{ */
3359
57.9k
{
3360
57.9k
  zend_ast_list *list = zend_ast_get_list(ast);
3361
57.9k
  uint32_t i;
3362
57.9k
  bool has_elems = false;
3363
57.9k
  bool is_keyed = list_is_keyed(list);
3364
3365
57.9k
  if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
3366
534
    zval_make_interned_string(&expr_node->u.constant);
3367
534
  }
3368
3369
120k
  for (i = 0; i < list->children; ++i) {
3370
62.5k
    zend_ast *elem_ast = list->child[i];
3371
62.5k
    zend_ast *var_ast, *key_ast;
3372
62.5k
    znode fetch_result, dim_node;
3373
62.5k
    zend_op *opline;
3374
3375
62.5k
    if (elem_ast == NULL) {
3376
1.09k
      if (is_keyed) {
3377
10
        zend_error(E_COMPILE_ERROR,
3378
10
          "Cannot use empty array entries in keyed array assignment");
3379
1.08k
      } else {
3380
1.08k
        continue;
3381
1.08k
      }
3382
1.09k
    }
3383
3384
61.4k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
3385
12
      zend_error(E_COMPILE_ERROR,
3386
12
          "Spread operator is not supported in assignments");
3387
12
    }
3388
3389
61.4k
    var_ast = elem_ast->child[0];
3390
61.4k
    key_ast = elem_ast->child[1];
3391
61.4k
    has_elems = true;
3392
3393
61.4k
    if (is_keyed) {
3394
1.88k
      if (key_ast == NULL) {
3395
1
        zend_error(E_COMPILE_ERROR,
3396
1
          "Cannot mix keyed and unkeyed array entries in assignments");
3397
1
      }
3398
3399
1.88k
      zend_compile_expr(&dim_node, key_ast);
3400
59.5k
    } else {
3401
59.5k
      if (key_ast != NULL) {
3402
2
        zend_error(E_COMPILE_ERROR,
3403
2
          "Cannot mix keyed and unkeyed array entries in assignments");
3404
2
      }
3405
3406
59.5k
      dim_node.op_type = IS_CONST;
3407
59.5k
      ZVAL_LONG(&dim_node.u.constant, i);
3408
59.5k
    }
3409
3410
61.4k
    if (expr_node->op_type == IS_CONST) {
3411
2.31k
      Z_TRY_ADDREF(expr_node->u.constant);
3412
2.31k
    }
3413
3414
61.4k
    zend_verify_list_assign_target(var_ast, array_style);
3415
3416
61.4k
    opline = zend_emit_op(&fetch_result,
3417
61.4k
      elem_ast->attr ? (expr_node->op_type == IS_CV ? ZEND_FETCH_DIM_W : ZEND_FETCH_LIST_W) : ZEND_FETCH_LIST_R, expr_node, &dim_node);
3418
61.4k
    if (opline->opcode == ZEND_FETCH_LIST_R) {
3419
7.00k
      opline->result_type = IS_TMP_VAR;
3420
7.00k
      fetch_result.op_type = IS_TMP_VAR;
3421
7.00k
    }
3422
3423
61.4k
    if (dim_node.op_type == IS_CONST) {
3424
60.4k
      zend_handle_numeric_dim(opline, &dim_node);
3425
60.4k
    }
3426
3427
61.4k
    if (elem_ast->attr) {
3428
54.2k
      zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL);
3429
54.2k
    }
3430
61.4k
    if (var_ast->kind == ZEND_AST_ARRAY) {
3431
2.10k
      zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr, type);
3432
59.3k
    } else if (elem_ast->attr) {
3433
52.7k
      zend_emit_assign_ref_znode(var_ast, &fetch_result);
3434
52.7k
    } else {
3435
6.57k
      zend_emit_assign_znode(var_ast, &fetch_result);
3436
6.57k
    }
3437
61.4k
  }
3438
3439
57.9k
  if (has_elems == 0) {
3440
64
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
3441
64
  }
3442
3443
57.9k
  if (result) {
3444
1.19k
    if ((type == BP_VAR_R || type == BP_VAR_IS) && expr_node->op_type == IS_VAR) {
3445
      /* Deref. */
3446
474
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, expr_node, NULL);
3447
716
    } else {
3448
716
      *result = *expr_node;
3449
716
    }
3450
56.7k
  } else {
3451
56.7k
    zend_do_free(expr_node);
3452
56.7k
  }
3453
57.9k
}
3454
/* }}} */
3455
3456
static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
3457
969k
{
3458
969k
  if (ast->kind == ZEND_AST_CALL || ast->kind == ZEND_AST_PIPE) {
3459
76
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
3460
76
  }
3461
969k
  if (
3462
969k
    ast->kind == ZEND_AST_METHOD_CALL
3463
969k
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
3464
969k
    || ast->kind == ZEND_AST_STATIC_CALL
3465
969k
  ) {
3466
5
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
3467
5
  }
3468
969k
  if (zend_ast_is_short_circuited(ast)) {
3469
23
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use nullsafe operator in write context");
3470
23
  }
3471
969k
  if (is_globals_fetch(ast)) {
3472
43
    zend_error_noreturn(E_COMPILE_ERROR,
3473
43
      "$GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax");
3474
43
  }
3475
969k
}
3476
/* }}} */
3477
3478
/* Detects $a... = $a pattern */
3479
static bool zend_is_assign_to_self(const zend_ast *var_ast, const zend_ast *expr_ast) /* {{{ */
3480
94.3k
{
3481
94.3k
  if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
3482
90.3k
    return false;
3483
90.3k
  }
3484
3485
9.26k
  while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
3486
5.25k
    var_ast = var_ast->child[0];
3487
5.25k
  }
3488
3489
4.01k
  if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
3490
1.03k
    return false;
3491
1.03k
  }
3492
3493
2.97k
  {
3494
2.97k
    zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
3495
2.97k
    zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
3496
2.97k
    bool result = zend_string_equals(name1, name2);
3497
2.97k
    zend_string_release_ex(name1, 0);
3498
2.97k
    zend_string_release_ex(name2, 0);
3499
2.97k
    return result;
3500
4.01k
  }
3501
4.01k
}
3502
/* }}} */
3503
3504
static void zend_compile_expr_with_potential_assign_to_self(
3505
94.3k
    znode *expr_node, zend_ast *expr_ast, const zend_ast *var_ast) {
3506
94.3k
  if (zend_is_assign_to_self(var_ast, expr_ast) && !is_this_fetch(expr_ast)) {
3507
    /* $a[0] = $a should evaluate the right $a first */
3508
1.33k
    znode cv_node;
3509
3510
1.33k
    if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3511
153
      zend_compile_simple_var_no_cv(expr_node, expr_ast, BP_VAR_R, false);
3512
1.18k
    } else {
3513
1.18k
      zend_emit_op_tmp(expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3514
1.18k
    }
3515
92.9k
  } else {
3516
92.9k
    zend_compile_expr(expr_node, expr_ast);
3517
92.9k
  }
3518
94.3k
}
3519
3520
/* Flat unkeyed list and literal without references or spread, one value per target */
3521
static bool zend_list_assign_from_array_applies(zend_ast *var_ast, zend_ast *expr_ast) /* {{{ */
3522
3.33k
{
3523
3.33k
  if (expr_ast->kind != ZEND_AST_ARRAY) {
3524
1.14k
    return false;
3525
1.14k
  }
3526
3527
2.19k
  const zend_ast_list *targets = zend_ast_get_list(var_ast);
3528
2.19k
  const zend_ast_list *values = zend_ast_get_list(expr_ast);
3529
2.19k
  if (targets->children == 0 || values->children != targets->children) {
3530
1.85k
    return false;
3531
1.85k
  }
3532
3533
683
  for (uint32_t i = 0; i < targets->children; i++) {
3534
412
    const zend_ast *target_ast = targets->child[i];
3535
412
    const zend_ast *value_ast = values->child[i];
3536
412
    if (target_ast == NULL || target_ast->kind != ZEND_AST_ARRAY_ELEM || target_ast->child[1] != NULL || target_ast->attr
3537
383
     || target_ast->child[0]->kind == ZEND_AST_ARRAY) {
3538
45
      return false;
3539
45
    }
3540
367
    if (value_ast == NULL || value_ast->kind != ZEND_AST_ARRAY_ELEM || value_ast->child[1] != NULL || value_ast->attr) {
3541
23
      return false;
3542
23
    }
3543
367
  }
3544
271
  return true;
3545
339
}
3546
/* }}} */
3547
3548
/* [$a, $b] = [$b, $a]; with an unused result: assigns the values directly, without the array.
3549
 * The values are compiled in order and a variable is copied before any assignment; each target
3550
 * then takes its value */
3551
static bool zend_try_compile_list_assign_from_array(zend_ast *var_ast, zend_ast *expr_ast) /* {{{ */
3552
3.33k
{
3553
3.33k
  if (!zend_list_assign_from_array_applies(var_ast, expr_ast)) {
3554
3.06k
    return false;
3555
3.06k
  }
3556
3557
271
  const zend_ast_list *targets = zend_ast_get_list(var_ast);
3558
271
  const zend_ast_list *values = zend_ast_get_list(expr_ast);
3559
271
  uint32_t count = values->children;
3560
271
  znode *value_nodes = safe_emalloc(count, sizeof(znode), 0);
3561
3562
607
  for (uint32_t i = 0; i < count; i++) {
3563
336
    znode *value = &value_nodes[i];
3564
336
    zend_compile_expr(value, values->child[i]->child[0]);
3565
336
    if (value->op_type == IS_CV) {
3566
36
      zend_emit_op_tmp(value, ZEND_QM_ASSIGN, value, NULL);
3567
36
    }
3568
336
  }
3569
3570
593
  for (uint32_t i = 0; i < count; i++) {
3571
322
    zend_ast *target_ast = targets->child[i]->child[0];
3572
3573
322
    zend_verify_list_assign_target(target_ast, var_ast->attr);
3574
322
    zend_emit_assign_znode(target_ast, &value_nodes[i]);
3575
322
  }
3576
3577
271
  efree(value_nodes);
3578
271
  return true;
3579
3.33k
}
3580
/* }}} */
3581
3582
static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type) /* {{{ */
3583
633k
{
3584
633k
  zend_ast *var_ast = ast->child[0];
3585
633k
  zend_ast *expr_ast = ast->child[1];
3586
3587
633k
  znode var_node, expr_node;
3588
633k
  zend_op *opline;
3589
633k
  uint32_t offset;
3590
633k
  if (is_this_fetch(var_ast)) {
3591
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3592
1
  }
3593
3594
633k
  zend_ensure_writable_variable(var_ast);
3595
3596
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3597
633k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3598
633k
  switch (kind) {
3599
567k
    case ZEND_AST_VAR:
3600
567k
      offset = zend_delayed_compile_begin();
3601
567k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, false);
3602
567k
      zend_compile_expr(&expr_node, expr_ast);
3603
567k
      zend_delayed_compile_end(offset);
3604
567k
      CG(zend_lineno) = zend_ast_get_lineno(var_ast);
3605
567k
      zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node);
3606
567k
      return;
3607
1.24k
    case ZEND_AST_STATIC_PROP:
3608
1.24k
      offset = zend_delayed_compile_begin();
3609
1.24k
      zend_delayed_compile_var(result, var_ast, BP_VAR_W, false);
3610
1.24k
      zend_compile_expr(&expr_node, expr_ast);
3611
3612
1.24k
      opline = zend_delayed_compile_end(offset);
3613
1.24k
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
3614
1.24k
      opline->result_type = IS_TMP_VAR;
3615
1.24k
      result->op_type = IS_TMP_VAR;
3616
3617
1.24k
      zend_emit_op_data(&expr_node);
3618
1.24k
      return;
3619
5.19k
    case ZEND_AST_DIM:
3620
5.19k
      offset = zend_delayed_compile_begin();
3621
5.19k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_W, /* by_ref */ false);
3622
5.19k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3623
3624
5.19k
      opline = zend_delayed_compile_end(offset);
3625
5.19k
      opline->opcode = ZEND_ASSIGN_DIM;
3626
5.19k
      opline->result_type = IS_TMP_VAR;
3627
5.19k
      result->op_type = IS_TMP_VAR;
3628
3629
5.19k
      opline = zend_emit_op_data(&expr_node);
3630
5.19k
      return;
3631
2.70k
    case ZEND_AST_PROP:
3632
2.70k
    case ZEND_AST_NULLSAFE_PROP:
3633
2.70k
      offset = zend_delayed_compile_begin();
3634
2.70k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
3635
2.70k
      zend_compile_expr(&expr_node, expr_ast);
3636
3637
2.70k
      opline = zend_delayed_compile_end(offset);
3638
2.70k
      opline->opcode = ZEND_ASSIGN_OBJ;
3639
2.70k
      opline->result_type = IS_TMP_VAR;
3640
2.70k
      result->op_type = IS_TMP_VAR;
3641
3642
2.70k
      zend_emit_op_data(&expr_node);
3643
2.70k
      return;
3644
56.7k
    case ZEND_AST_ARRAY:
3645
56.7k
      if (zend_propagate_list_refs(var_ast)) {
3646
51.9k
        if (!zend_is_variable_or_call(expr_ast)) {
3647
30
          zend_error_noreturn(E_COMPILE_ERROR,
3648
30
            "Cannot assign reference to non referenceable value");
3649
51.9k
        } else {
3650
51.9k
          zend_assert_not_short_circuited(expr_ast);
3651
51.9k
        }
3652
3653
51.9k
        zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
3654
        /* MAKE_REF is usually not necessary for CVs. However, if there are
3655
         * self-assignments, this forces the RHS to evaluate first. */
3656
51.9k
        zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
3657
51.9k
      } else {
3658
4.77k
        if (stmt && zend_try_compile_list_assign_from_array(var_ast, expr_ast)) {
3659
251
          result->op_type = IS_UNUSED;
3660
251
          return;
3661
251
        }
3662
4.52k
        if (expr_ast->kind == ZEND_AST_VAR) {
3663
          /* list($a, $b) = $a should evaluate the right $a first */
3664
301
          znode cv_node;
3665
3666
301
          if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3667
72
            zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, false);
3668
229
          } else {
3669
229
            zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3670
229
          }
3671
4.22k
        } else {
3672
4.22k
          zend_compile_expr(&expr_node, expr_ast);
3673
4.22k
        }
3674
4.52k
      }
3675
3676
56.5k
      zend_compile_list_assign(!stmt ? result : NULL, var_ast, &expr_node, var_ast->attr, type);
3677
56.5k
      if (stmt) {
3678
54.3k
        result->op_type = IS_UNUSED;
3679
54.3k
      }
3680
56.5k
      return;
3681
0
    default: ZEND_UNREACHABLE();
3682
633k
  }
3683
633k
}
3684
/* }}} */
3685
3686
static void zend_compile_assign_ref(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3687
70.0k
{
3688
70.0k
  zend_ast *target_ast = ast->child[0];
3689
70.0k
  zend_ast *source_ast = ast->child[1];
3690
3691
70.0k
  znode target_node, source_node;
3692
70.0k
  zend_op *opline;
3693
70.0k
  uint32_t offset, flags;
3694
3695
70.0k
  if (is_this_fetch(target_ast)) {
3696
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3697
1
  }
3698
70.0k
  zend_ensure_writable_variable(target_ast);
3699
70.0k
  zend_assert_not_short_circuited(source_ast);
3700
70.0k
  if (is_globals_fetch(source_ast)) {
3701
6
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot acquire reference to $GLOBALS");
3702
6
  }
3703
3704
70.0k
  offset = zend_delayed_compile_begin();
3705
70.0k
  zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, true);
3706
70.0k
  zend_compile_var(&source_node, source_ast, BP_VAR_W, true);
3707
3708
70.0k
  if ((target_ast->kind != ZEND_AST_VAR
3709
68.5k
    || target_ast->child[0]->kind != ZEND_AST_ZVAL)
3710
15.2k
   && source_ast->kind != ZEND_AST_ZNODE
3711
12.8k
   && source_node.op_type != IS_CV) {
3712
    /* Both LHS and RHS expressions may modify the same data structure,
3713
     * and the modification during RHS evaluation may dangle the pointer
3714
     * to the result of the LHS evaluation.
3715
     * Use MAKE_REF instruction to replace direct pointer with REFERENCE.
3716
     * See: Bug #71539
3717
     */
3718
378
    zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL);
3719
378
  }
3720
3721
70.0k
  opline = zend_delayed_compile_end(offset);
3722
3723
70.0k
  if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
3724
10
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3725
10
  }
3726
3727
70.0k
  flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0;
3728
3729
70.0k
  if (opline && opline->opcode == ZEND_FETCH_OBJ_W) {
3730
603
    opline->opcode = ZEND_ASSIGN_OBJ_REF;
3731
603
    opline->extended_value &= ~ZEND_FETCH_REF;
3732
603
    opline->extended_value |= flags;
3733
603
    if (result) {
3734
114
      *result = target_node;
3735
489
    } else {
3736
489
      SET_UNUSED(opline->result);
3737
489
    }
3738
603
    zend_emit_op_data(&source_node);
3739
69.4k
  } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) {
3740
573
    opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF;
3741
573
    opline->extended_value &= ~ZEND_FETCH_REF;
3742
573
    opline->extended_value |= flags;
3743
573
    if (result) {
3744
92
      *result = target_node;
3745
481
    } else {
3746
481
      SET_UNUSED(opline->result);
3747
481
    }
3748
573
    zend_emit_op_data(&source_node);
3749
68.8k
  } else {
3750
68.8k
    opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
3751
68.8k
    opline->extended_value = flags;
3752
68.8k
  }
3753
3754
70.0k
  if (result && (type == BP_VAR_R || type == BP_VAR_IS)) {
3755
    /* Deref. */
3756
1.97k
    znode tmp_result = *result;
3757
1.97k
    zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &tmp_result, NULL);
3758
1.97k
  }
3759
70.0k
}
3760
/* }}} */
3761
3762
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3763
54.2k
{
3764
54.2k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
3765
54.2k
    zend_ast_create_znode(value_node));
3766
54.2k
  zend_compile_stmt(assign_ast);
3767
54.2k
}
3768
/* }}} */
3769
3770
static void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
3771
150k
{
3772
150k
  zend_ast *var_ast = ast->child[0];
3773
150k
  zend_ast *expr_ast = ast->child[1];
3774
150k
  uint32_t opcode = ast->attr;
3775
3776
150k
  znode var_node, expr_node;
3777
150k
  zend_op *opline;
3778
150k
  uint32_t offset, cache_slot;
3779
3780
150k
  zend_ensure_writable_variable(var_ast);
3781
3782
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3783
150k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3784
150k
  switch (kind) {
3785
142k
    case ZEND_AST_VAR:
3786
142k
      offset = zend_delayed_compile_begin();
3787
142k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, false);
3788
142k
      zend_compile_expr(&expr_node, expr_ast);
3789
142k
      zend_delayed_compile_end(offset);
3790
142k
      opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node);
3791
142k
      opline->extended_value = opcode;
3792
142k
      return;
3793
4.46k
    case ZEND_AST_STATIC_PROP:
3794
4.46k
      offset = zend_delayed_compile_begin();
3795
4.46k
      zend_delayed_compile_var(result, var_ast, BP_VAR_RW, false);
3796
4.46k
      zend_compile_expr(&expr_node, expr_ast);
3797
3798
4.46k
      opline = zend_delayed_compile_end(offset);
3799
4.46k
      cache_slot = opline->extended_value;
3800
4.46k
      opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP;
3801
4.46k
      opline->extended_value = opcode;
3802
4.46k
      opline->result_type = IS_TMP_VAR;
3803
4.46k
      result->op_type = IS_TMP_VAR;
3804
3805
4.46k
      opline = zend_emit_op_data(&expr_node);
3806
4.46k
      opline->extended_value = cache_slot;
3807
4.46k
      return;
3808
2.17k
    case ZEND_AST_DIM:
3809
2.17k
      offset = zend_delayed_compile_begin();
3810
2.17k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_RW, /* by_ref */ false);
3811
2.17k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3812
3813
2.17k
      opline = zend_delayed_compile_end(offset);
3814
2.17k
      opline->opcode = ZEND_ASSIGN_DIM_OP;
3815
2.17k
      opline->extended_value = opcode;
3816
2.17k
      opline->result_type = IS_TMP_VAR;
3817
2.17k
      result->op_type = IS_TMP_VAR;
3818
3819
2.17k
      zend_emit_op_data(&expr_node);
3820
2.17k
      return;
3821
616
    case ZEND_AST_PROP:
3822
616
    case ZEND_AST_NULLSAFE_PROP:
3823
616
      offset = zend_delayed_compile_begin();
3824
616
      zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
3825
616
      zend_compile_expr(&expr_node, expr_ast);
3826
3827
616
      opline = zend_delayed_compile_end(offset);
3828
616
      cache_slot = opline->extended_value;
3829
616
      opline->opcode = ZEND_ASSIGN_OBJ_OP;
3830
616
      opline->extended_value = opcode;
3831
616
      opline->result_type = IS_TMP_VAR;
3832
616
      result->op_type = IS_TMP_VAR;
3833
3834
616
      opline = zend_emit_op_data(&expr_node);
3835
616
      opline->extended_value = cache_slot;
3836
616
      return;
3837
0
    default: ZEND_UNREACHABLE();
3838
150k
  }
3839
150k
}
3840
/* }}} */
3841
3842
4.41k
static uint32_t zend_get_arg_num(const zend_function *fn, const zend_string *arg_name) {
3843
  // TODO: Caching?
3844
12.3k
  for (uint32_t i = 0; i < fn->common.num_args; i++) {
3845
9.31k
    zend_arg_info *arg_info = &fn->op_array.arg_info[i];
3846
9.31k
    if (zend_string_equals(arg_info->name, arg_name)) {
3847
1.37k
      return i + 1;
3848
1.37k
    }
3849
9.31k
  }
3850
3851
  /* Either an invalid argument name, or collected into a variadic argument. */
3852
3.03k
  return (uint32_t) -1;
3853
4.41k
}
3854
3855
static uint32_t zend_compile_args_ex(
3856
    zend_ast *ast, const zend_function *fbc,
3857
    bool *may_have_extra_named_args,
3858
    bool is_call_partial, bool *uses_variadic_placeholder_p,
3859
    zval *named_positions) /* {{{ */
3860
4.76M
{
3861
4.76M
  const zend_ast_list *args = zend_ast_get_list(ast);
3862
4.76M
  uint32_t i;
3863
4.76M
  bool uses_arg_unpack = false;
3864
4.76M
  bool uses_variadic_placeholder = false;
3865
4.76M
  uint32_t arg_count = 0; /* number of arguments not including unpacks */
3866
3867
  /* Whether named arguments are used syntactically, to enforce language level limitations.
3868
   * May not actually use named argument passing. */
3869
4.76M
  bool uses_named_args = false;
3870
  /* Whether there may be any undef arguments due to the use of named arguments. */
3871
4.76M
  bool may_have_undef = false;
3872
  /* Whether there may be any extra named arguments collected into a variadic. */
3873
4.76M
  *may_have_extra_named_args = false;
3874
3875
10.9M
  for (i = 0; i < args->children; ++i) {
3876
6.16M
    zend_ast *arg = args->child[i];
3877
6.16M
    zend_string *arg_name = NULL;
3878
6.16M
    uint32_t arg_num = i + 1;
3879
3880
6.16M
    znode arg_node;
3881
6.16M
    zend_op *opline;
3882
6.16M
    uint8_t opcode;
3883
3884
6.16M
    if (arg->kind == ZEND_AST_UNPACK) {
3885
1.63k
      if (uses_named_args) {
3886
8
        zend_error_noreturn(E_COMPILE_ERROR,
3887
8
          "Cannot use argument unpacking after named arguments");
3888
8
      }
3889
3890
1.63k
      if (is_call_partial) {
3891
9
        zend_error_noreturn(E_COMPILE_ERROR,
3892
9
          "Cannot combine partial application and unpacking");
3893
9
      }
3894
3895
      /* Unpack may contain named arguments. */
3896
1.63k
      may_have_undef = true;
3897
1.63k
      if (!fbc || (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3898
1.42k
        *may_have_extra_named_args = true;
3899
1.42k
      }
3900
3901
1.63k
      uses_arg_unpack = true;
3902
1.63k
      fbc = NULL;
3903
3904
1.63k
      zend_compile_expr(&arg_node, arg->child[0]);
3905
1.63k
      opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
3906
1.63k
      opline->op2.num = arg_count;
3907
1.63k
      opline->result.var = EX_NUM_TO_VAR(arg_count - 1);
3908
3909
1.63k
      continue;
3910
1.63k
    }
3911
3912
6.16M
    if (arg->kind == ZEND_AST_NAMED_ARG) {
3913
44.9k
      uses_named_args = true;
3914
44.9k
      arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0]));
3915
44.9k
      arg = arg->child[1];
3916
3917
44.9k
      if (fbc && !uses_arg_unpack) {
3918
4.41k
        arg_num = zend_get_arg_num(fbc, arg_name);
3919
4.41k
        if (arg_num == arg_count + 1 && !may_have_undef) {
3920
          /* Using named arguments, but passing in order. */
3921
69
          arg_name = NULL;
3922
69
          arg_count++;
3923
4.34k
        } else {
3924
          // TODO: We could track which arguments were passed, even if out of order.
3925
4.34k
          may_have_undef = true;
3926
4.34k
          if (arg_num == (uint32_t) -1 && (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3927
127
            *may_have_extra_named_args = true;
3928
127
          }
3929
4.34k
        }
3930
40.5k
      } else {
3931
40.5k
        arg_num = (uint32_t) -1;
3932
40.5k
        may_have_undef = true;
3933
40.5k
        *may_have_extra_named_args = true;
3934
40.5k
      }
3935
3936
44.9k
      if (uses_variadic_placeholder) {
3937
6
        zend_error_noreturn(E_COMPILE_ERROR,
3938
6
          "Variadic placeholder must be last");
3939
6
      }
3940
6.11M
    } else {
3941
6.11M
      if (uses_arg_unpack) {
3942
29
        zend_error_noreturn(E_COMPILE_ERROR,
3943
29
          "Cannot use positional argument after argument unpacking");
3944
29
      }
3945
3946
6.11M
      bool is_variadic_placeholder = arg->kind == ZEND_AST_PLACEHOLDER_ARG
3947
4.53k
        && arg->attr == ZEND_PLACEHOLDER_VARIADIC;
3948
3949
6.11M
      if (uses_named_args && !is_variadic_placeholder) {
3950
46
        zend_error_noreturn(E_COMPILE_ERROR,
3951
46
          "Cannot use positional argument after named argument");
3952
46
      }
3953
3954
6.11M
      if (uses_variadic_placeholder) {
3955
17
        if (is_variadic_placeholder) {
3956
7
          zend_error_noreturn(E_COMPILE_ERROR,
3957
7
            "Variadic placeholder may only appear once");
3958
10
        } else {
3959
10
          zend_error_noreturn(E_COMPILE_ERROR,
3960
10
            "Variadic placeholder must be last");
3961
10
        }
3962
17
      }
3963
3964
6.11M
      if (!is_variadic_placeholder) {
3965
6.11M
        arg_count++;
3966
6.11M
      }
3967
6.11M
    }
3968
3969
6.16M
    if (arg->kind == ZEND_AST_PLACEHOLDER_ARG) {
3970
7.87k
      if (uses_arg_unpack) {
3971
0
        zend_error_noreturn(E_COMPILE_ERROR,
3972
0
          "Cannot combine partial application and unpacking");
3973
0
      }
3974
3975
7.87k
      if (arg->attr == ZEND_PLACEHOLDER_VARIADIC) {
3976
1.17k
        uses_variadic_placeholder = true;
3977
        /* Do not emit ZEND_SEND_PLACEHOLDER: We represent the variadic
3978
         * placeholder with a flag on the ZEND_CONVERT_CALLABLE_PARTIAL
3979
         * op instead. */
3980
1.17k
        continue;
3981
1.17k
      }
3982
3983
6.70k
      if (arg_name) {
3984
3.35k
        if (Z_ISUNDEF_P(named_positions)) {
3985
3.27k
          array_init(named_positions);
3986
3.27k
        }
3987
3.35k
        zval tmp;
3988
3.35k
        ZVAL_LONG(&tmp, zend_hash_num_elements(Z_ARRVAL_P(named_positions)));
3989
3.35k
        zend_hash_add(Z_ARRVAL_P(named_positions), arg_name, &tmp);
3990
3.35k
      }
3991
3992
6.70k
      opline = zend_emit_op(NULL, ZEND_SEND_PLACEHOLDER, NULL, NULL);
3993
6.70k
      if (arg_name) {
3994
3.35k
        opline->op2_type = IS_CONST;
3995
3.35k
        zend_string_addref(arg_name);
3996
3.35k
        opline->op2.constant = zend_add_literal_string(&arg_name);
3997
3.35k
        opline->result.num = zend_alloc_cache_slots(2);
3998
3.35k
      } else if (arg->attr != ZEND_PLACEHOLDER_VARIADIC) {
3999
3.34k
        opline->op2.opline_num = arg_num;
4000
3.34k
        opline->result.var = EX_NUM_TO_VAR(arg_num - 1);
4001
3.34k
      }
4002
4003
6.70k
      continue;
4004
7.87k
    }
4005
4006
    /* Treat passing of $GLOBALS the same as passing a call.
4007
     * This will error at runtime if the argument is by-ref. */
4008
6.15M
    if (zend_is_call(arg) || is_globals_fetch(arg)) {
4009
254k
      uint32_t type = is_globals_fetch(arg) || (fbc && !ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num))
4010
254k
        ? BP_VAR_R : BP_VAR_FUNC_ARG;
4011
254k
      zend_compile_var(&arg_node, arg, type, /* by_ref */ false);
4012
254k
      if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
4013
        /* Function call was converted into builtin instruction */
4014
31.4k
        if (!fbc || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
4015
9.16k
          opcode = ZEND_SEND_VAL_EX;
4016
22.2k
        } else {
4017
22.2k
          opcode = ZEND_SEND_VAL;
4018
22.2k
        }
4019
223k
      } else {
4020
223k
        if (fbc && arg_num != (uint32_t) -1) {
4021
469
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
4022
314
            opcode = ZEND_SEND_VAR_NO_REF;
4023
314
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
4024
            /* For IS_VAR operands, SEND_VAL will pass through the operand without
4025
             * dereferencing, so it will use a by-ref pass if the call returned by-ref
4026
             * and a by-value pass if it returned by-value. */
4027
155
            opcode = ZEND_SEND_VAL;
4028
155
          } else {
4029
0
            opcode = ZEND_SEND_VAR;
4030
0
          }
4031
222k
        } else {
4032
222k
          opcode = ZEND_SEND_VAR_NO_REF_EX;
4033
222k
        }
4034
223k
      }
4035
5.89M
    } else if (zend_is_passable_by_ref(arg) && !zend_ast_is_short_circuited(arg)) {
4036
101k
      if (fbc && arg_num != (uint32_t) -1) {
4037
2.42k
        if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
4038
314
          zend_compile_var(&arg_node, arg, BP_VAR_W, true);
4039
314
          opcode = ZEND_SEND_REF;
4040
2.10k
        } else {
4041
2.10k
          zend_compile_var(&arg_node, arg, BP_VAR_R, false);
4042
2.10k
          opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR;
4043
2.10k
        }
4044
98.8k
      } else {
4045
98.8k
        do {
4046
98.8k
          if (arg->kind == ZEND_AST_VAR) {
4047
22.6k
            CG(zend_lineno) = zend_ast_get_lineno(ast);
4048
22.6k
            if (is_this_fetch(arg)) {
4049
2.61k
              zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL);
4050
2.61k
              opcode = ZEND_SEND_VAR_EX;
4051
2.61k
              CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
4052
2.61k
              break;
4053
20.0k
            } else if (zend_try_compile_cv(&arg_node, arg, BP_VAR_R) == SUCCESS) {
4054
19.7k
              opcode = ZEND_SEND_VAR_EX;
4055
19.7k
              break;
4056
19.7k
            }
4057
22.6k
          }
4058
76.4k
          opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL);
4059
76.4k
          if (arg_name) {
4060
4.12k
            opline->op2_type = IS_CONST;
4061
4.12k
            zend_string_addref(arg_name);
4062
4.12k
            opline->op2.constant = zend_add_literal_string(&arg_name);
4063
4.12k
            opline->result.num = zend_alloc_cache_slots(2);
4064
72.3k
          } else {
4065
72.3k
            opline->op2.num = arg_num;
4066
72.3k
          }
4067
76.4k
          zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, true);
4068
76.4k
          opcode = ZEND_SEND_FUNC_ARG;
4069
76.4k
        } while (0);
4070
98.8k
      }
4071
5.79M
    } else {
4072
5.79M
      zend_compile_expr(&arg_node, arg);
4073
5.79M
      if (arg_node.op_type == IS_VAR) {
4074
        /* pass ++$a or something similar */
4075
0
        if (fbc && arg_num != (uint32_t) -1) {
4076
0
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
4077
0
            opcode = ZEND_SEND_VAR_NO_REF;
4078
0
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
4079
0
            opcode = ZEND_SEND_VAL;
4080
0
          } else {
4081
0
            opcode = ZEND_SEND_VAR;
4082
0
          }
4083
0
        } else {
4084
0
          opcode = ZEND_SEND_VAR_NO_REF_EX;
4085
0
        }
4086
5.79M
      } else if (arg_node.op_type == IS_CV) {
4087
0
        if (fbc && arg_num != (uint32_t) -1) {
4088
0
          if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
4089
0
            opcode = ZEND_SEND_REF;
4090
0
          } else {
4091
0
            opcode = ZEND_SEND_VAR;
4092
0
          }
4093
0
        } else {
4094
0
          opcode = ZEND_SEND_VAR_EX;
4095
0
        }
4096
5.79M
      } else {
4097
        /* Delay "Only variables can be passed by reference" error to execution */
4098
5.79M
        if (fbc && arg_num != (uint32_t) -1 && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
4099
64.1k
          opcode = ZEND_SEND_VAL;
4100
5.73M
        } else {
4101
5.73M
          opcode = ZEND_SEND_VAL_EX;
4102
5.73M
        }
4103
5.79M
      }
4104
5.79M
    }
4105
4106
6.15M
    opline = zend_emit_op(NULL, opcode, &arg_node, NULL);
4107
6.15M
    if (arg_name) {
4108
41.5k
      opline->op2_type = IS_CONST;
4109
41.5k
      zend_string_addref(arg_name);
4110
41.5k
      opline->op2.constant = zend_add_literal_string(&arg_name);
4111
41.5k
      opline->result.num = zend_alloc_cache_slots(2);
4112
6.11M
    } else {
4113
6.11M
      opline->op2.opline_num = arg_num;
4114
6.11M
      opline->result.var = EX_NUM_TO_VAR(arg_num - 1);
4115
6.11M
    }
4116
6.15M
  }
4117
4118
4.76M
  if (!is_call_partial) {
4119
4.74M
    if (may_have_undef) {
4120
35.2k
      zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
4121
35.2k
    }
4122
4.74M
  } else {
4123
11.2k
    *uses_variadic_placeholder_p = uses_variadic_placeholder;
4124
11.2k
  }
4125
4126
4.76M
  return arg_count;
4127
4.76M
}
4128
/* }}} */
4129
4130
static uint32_t zend_compile_args(zend_ast *ast, const zend_function *fbc, bool *may_have_extra_named_args)
4131
4.75M
{
4132
4.75M
  return zend_compile_args_ex(ast, fbc, may_have_extra_named_args, false, NULL, NULL);
4133
4.75M
}
4134
4135
ZEND_API uint8_t zend_get_call_op(const zend_op *init_op, const zend_function *fbc, bool result_used) /* {{{ */
4136
4.75M
{
4137
4.75M
  uint32_t no_discard = result_used ? 0 : ZEND_ACC_NODISCARD;
4138
4139
4.75M
  if (fbc && init_op->opcode != ZEND_NEW) {
4140
48.8k
    ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE));
4141
48.8k
    if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) {
4142
46.9k
      if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) {
4143
41.9k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
4144
41.8k
          return ZEND_DO_ICALL;
4145
41.8k
        } else {
4146
111
          return ZEND_DO_FCALL_BY_NAME;
4147
111
        }
4148
41.9k
      }
4149
46.9k
    } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){
4150
1.87k
      if (zend_execute_ex == execute_ex) {
4151
1.52k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
4152
1.43k
          return ZEND_DO_UCALL;
4153
1.43k
        } else {
4154
97
          return ZEND_DO_FCALL_BY_NAME;
4155
97
        }
4156
1.52k
      }
4157
1.87k
    }
4158
4.70M
  } else if (zend_execute_ex == execute_ex &&
4159
4.69M
             !zend_execute_internal &&
4160
4.69M
             (init_op->opcode == ZEND_INIT_FCALL_BY_NAME ||
4161
4.59M
              init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) {
4162
4.37M
    return ZEND_DO_FCALL_BY_NAME;
4163
4.37M
  }
4164
333k
  return ZEND_DO_FCALL;
4165
4.75M
}
4166
/* }}} */
4167
4168
static zend_string *zend_compile_partial_name(const zend_op_array *declaring_op_array,
4169
    const uint32_t declaring_lineno)
4170
7.30k
{
4171
  /* We attempt to generate a name that hints at where the PFA was created,
4172
   * similarly to Closures (GH-13550).
4173
   * We do not attempt to make the name unique. */
4174
4175
7.30k
  zend_string *filename = declaring_op_array->filename;
4176
7.30k
  uint32_t start_lineno = declaring_lineno;
4177
4178
7.30k
  zend_string *class = zend_empty_string;
4179
7.30k
  zend_string *separator = zend_empty_string;
4180
7.30k
  zend_string *function = filename;
4181
7.30k
  const char *parens = "";
4182
4183
7.30k
  if (declaring_op_array->function_name) {
4184
3.31k
    function = declaring_op_array->function_name;
4185
3.31k
    if (declaring_op_array->fn_flags & ZEND_ACC_CLOSURE) {
4186
      /* If the parent function is a closure, don't redundantly
4187
       * add the classname and parentheses. */
4188
2.20k
    } else {
4189
1.11k
      parens = "()";
4190
4191
1.11k
      if (declaring_op_array->scope && declaring_op_array->scope->name) {
4192
957
        class = declaring_op_array->scope->name;
4193
957
        separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM);
4194
957
      }
4195
1.11k
    }
4196
3.31k
  }
4197
4198
7.30k
  zend_string *name = zend_strpprintf_unchecked(
4199
7.30k
    0,
4200
7.30k
    "{closure:pfa:%S%S%S%s:%" PRIu32 "}",
4201
7.30k
    class,
4202
7.30k
    separator,
4203
7.30k
    function,
4204
7.30k
    parens,
4205
7.30k
    start_lineno
4206
7.30k
  );
4207
4208
7.30k
  return name;
4209
7.30k
}
4210
4211
static void zend_compile_call_partial(znode *result, zend_ast_fcc *fcc_ast, uint32_t arg_count,
4212
    bool may_have_extra_named_args, bool uses_variadic_placeholder,
4213
6.76k
    zval *named_positions, uint32_t opnum_init, const zend_function *fbc) {
4214
4215
6.76k
  zend_op *init_opline = &CG(active_op_array)->opcodes[opnum_init];
4216
4217
6.76k
  init_opline->extended_value = arg_count;
4218
4219
6.76k
  ZEND_ASSERT(init_opline->opcode != ZEND_NEW);
4220
4221
6.76k
  if (init_opline->opcode == ZEND_INIT_FCALL) {
4222
674
    init_opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
4223
674
  }
4224
4225
6.76k
  zend_op *opline = zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT_PARTIAL,
4226
6.76k
        NULL, NULL);
4227
4228
6.76k
  opline->extended_value = zend_alloc_cache_slots(2);
4229
4230
6.76k
  if (uses_variadic_placeholder) {
4231
1.14k
    opline->extended_value |= ZEND_PARTIAL_USES_VARIADIC_PLACEHOLDER;
4232
1.14k
  }
4233
4234
6.76k
  zend_string *name = zend_compile_partial_name(CG(active_op_array), opline->lineno);
4235
6.76k
  opline->op1.constant = zend_add_literal_string(&name);
4236
6.76k
  opline->op1_type = IS_CONST;
4237
4238
6.76k
  if (!Z_ISUNDEF_P(named_positions)) {
4239
3.27k
    opline->op2.constant = zend_add_literal(named_positions);
4240
3.27k
    opline->op2_type = IS_CONST;
4241
3.49k
  } else {
4242
3.49k
    opline->op2.num = 0;
4243
3.49k
  }
4244
6.76k
}
4245
4246
static bool zend_compile_call_common(znode *result, zend_ast *args_ast, const zend_function *fbc, uint32_t lineno, uint32_t type) /* {{{ */
4247
4.76M
{
4248
4.76M
  zend_op *opline;
4249
4.76M
  uint32_t opnum_init = get_next_op_number() - 1;
4250
4251
4.76M
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
4252
13.2k
    opline = &CG(active_op_array)->opcodes[opnum_init];
4253
13.2k
    opline->extended_value = 0;
4254
    /* opcode array may be reallocated, so don't access opcode field after zend_emit_op_tmp(). */
4255
13.2k
    uint8_t opcode = opline->opcode;
4256
4257
13.2k
    if (opcode == ZEND_NEW) {
4258
30
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
4259
30
    }
4260
4261
13.2k
    zend_ast_list *fcc_args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args);
4262
4263
    /* FCCs are a special case of PFAs with a single variadic placeholder */
4264
13.2k
    if (fcc_args->children == 1 && fcc_args->child[0]->attr == ZEND_PLACEHOLDER_VARIADIC) {
4265
4266
6.40k
      if (opline->opcode == ZEND_INIT_FCALL) {
4267
435
        opline->op1.num = zend_vm_calc_used_stack(0, fbc);
4268
435
      }
4269
4270
6.40k
      zend_op *callable_convert_op = zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT, NULL, NULL);
4271
6.40k
      if (opcode == ZEND_INIT_FCALL
4272
5.96k
       || opcode == ZEND_INIT_FCALL_BY_NAME
4273
5.41k
       || opcode == ZEND_INIT_NS_FCALL_BY_NAME) {
4274
5.41k
        callable_convert_op->extended_value = zend_alloc_cache_slot();
4275
5.41k
      } else {
4276
988
        callable_convert_op->extended_value = (uint32_t)-1;
4277
988
      }
4278
4279
6.40k
      return true;
4280
6.40k
    }
4281
4282
6.83k
    zend_ast_fcc *fcc_ast = (zend_ast_fcc*)args_ast;
4283
6.83k
    args_ast = fcc_ast->args;
4284
4285
6.83k
    bool may_have_extra_named_args;
4286
6.83k
    bool uses_variadic_placeholder;
4287
4288
6.83k
    zval named_positions;
4289
6.83k
    ZVAL_UNDEF(&named_positions);
4290
4291
6.83k
    uint32_t arg_count = zend_compile_args_ex(args_ast, fbc,
4292
6.83k
        &may_have_extra_named_args, true, &uses_variadic_placeholder,
4293
6.83k
        &named_positions);
4294
4295
6.83k
    zend_compile_call_partial(result, fcc_ast, arg_count,
4296
6.83k
        may_have_extra_named_args, uses_variadic_placeholder,
4297
6.83k
        &named_positions, opnum_init, fbc);
4298
4299
6.83k
    return true;
4300
13.2k
  }
4301
4302
4.76M
  bool may_have_extra_named_args;
4303
4.75M
  uint32_t arg_count = zend_compile_args(args_ast, fbc, &may_have_extra_named_args);
4304
4305
4.75M
  zend_do_extended_fcall_begin();
4306
4307
4.75M
  opline = &CG(active_op_array)->opcodes[opnum_init];
4308
4.75M
  opline->extended_value = arg_count;
4309
4.75M
  uint8_t init_opcode = opline->opcode;
4310
4311
4.75M
  if (init_opcode == ZEND_INIT_FCALL) {
4312
46.0k
    opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
4313
46.0k
  }
4314
4315
4.75M
  uint8_t call_op = zend_get_call_op(
4316
4.75M
    opline,
4317
4.75M
    fbc,
4318
    /* result_used: At this point we do not yet reliably
4319
     * know if the result is used. Deoptimize #[\NoDiscard]
4320
     * calls to be sure. The optimizer will fix this up.
4321
     */
4322
4.75M
    false
4323
4.75M
  );
4324
4.75M
  opline = zend_emit_op(result, call_op, NULL, NULL);
4325
4.75M
  if (type == BP_VAR_R || type == BP_VAR_IS) {
4326
4.44M
    if (init_opcode != ZEND_NEW && opline->result_type == IS_VAR) {
4327
4.35M
      opline->result_type = IS_TMP_VAR;
4328
4.35M
      result->op_type = IS_TMP_VAR;
4329
4.35M
    }
4330
4.44M
  }
4331
4.75M
  if (may_have_extra_named_args) {
4332
33.3k
    opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4333
33.3k
  }
4334
4.75M
  opline->lineno = lineno;
4335
4.75M
  zend_do_extended_fcall_end();
4336
4.75M
  return false;
4337
4.76M
}
4338
/* }}} */
4339
4340
static bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
4341
4.45M
{
4342
4.45M
  zend_string *orig_name = zend_ast_get_str(name_ast);
4343
4.45M
  bool is_fully_qualified;
4344
4345
4.45M
  name_node->op_type = IS_CONST;
4346
4.45M
  ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
4347
4.45M
    orig_name, name_ast->attr, &is_fully_qualified));
4348
4349
4.45M
  return !is_fully_qualified && FC(current_namespace);
4350
4.45M
}
4351
/* }}} */
4352
4353
static void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */
4354
214k
{
4355
214k
  if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
4356
109k
    const char *colon;
4357
109k
    zend_string *str = Z_STR(name_node->u.constant);
4358
109k
    if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
4359
1.33k
      zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
4360
1.33k
      zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
4361
1.33k
      zend_op *opline = get_next_op();
4362
4363
1.33k
      opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
4364
1.33k
      opline->op1_type = IS_CONST;
4365
1.33k
      opline->op1.constant = zend_add_class_name_literal(class);
4366
1.33k
      opline->op2_type = IS_CONST;
4367
1.33k
      opline->op2.constant = zend_add_func_name_literal(method);
4368
      /* 2 slots, for class and method */
4369
1.33k
      opline->result.num = zend_alloc_cache_slots(2);
4370
1.33k
      zval_ptr_dtor(&name_node->u.constant);
4371
108k
    } else {
4372
108k
      zend_op *opline = get_next_op();
4373
4374
108k
      opline->opcode = ZEND_INIT_FCALL_BY_NAME;
4375
108k
      opline->op2_type = IS_CONST;
4376
108k
      opline->op2.constant = zend_add_func_name_literal(str);
4377
108k
      opline->result.num = zend_alloc_cache_slot();
4378
108k
    }
4379
109k
  } else {
4380
105k
    zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node);
4381
105k
  }
4382
4383
214k
  zend_compile_call_common(result, args_ast, NULL, lineno, type);
4384
214k
}
4385
/* }}} */
4386
4387
static inline bool zend_args_contain_unpack_or_named(const zend_ast_list *args) /* {{{ */
4388
4.24M
{
4389
4.24M
  uint32_t i;
4390
9.98M
  for (i = 0; i < args->children; ++i) {
4391
5.74M
    const zend_ast *arg = args->child[i];
4392
5.74M
    if (arg->kind == ZEND_AST_UNPACK || arg->kind == ZEND_AST_NAMED_ARG) {
4393
4.07k
      return true;
4394
4.07k
    }
4395
5.74M
  }
4396
4.24M
  return false;
4397
4.24M
}
4398
/* }}} */
4399
4400
static zend_result zend_compile_func_strlen(znode *result, const zend_ast_list *args) /* {{{ */
4401
696
{
4402
696
  znode arg_node;
4403
4404
696
  if (args->children != 1) {
4405
196
    return FAILURE;
4406
196
  }
4407
4408
500
  zend_compile_expr(&arg_node, args->child[0]);
4409
500
  if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
4410
391
    result->op_type = IS_CONST;
4411
391
    ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
4412
391
    zval_ptr_dtor_str(&arg_node.u.constant);
4413
391
  } else {
4414
109
    zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
4415
109
  }
4416
500
  return SUCCESS;
4417
696
}
4418
/* }}} */
4419
4420
static zend_result zend_compile_func_typecheck(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4421
1.54k
{
4422
1.54k
  znode arg_node;
4423
1.54k
  zend_op *opline;
4424
4425
1.54k
  if (args->children != 1) {
4426
707
    return FAILURE;
4427
707
  }
4428
4429
840
  zend_compile_expr(&arg_node, args->child[0]);
4430
840
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4431
840
  if (type != _IS_BOOL) {
4432
490
    opline->extended_value = (1 << type);
4433
490
  } else {
4434
350
    opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE);
4435
350
  }
4436
840
  return SUCCESS;
4437
1.54k
}
4438
/* }}} */
4439
4440
static zend_result zend_compile_func_is_scalar(znode *result, const zend_ast_list *args) /* {{{ */
4441
515
{
4442
515
  znode arg_node;
4443
515
  zend_op *opline;
4444
4445
515
  if (args->children != 1) {
4446
230
    return FAILURE;
4447
230
  }
4448
4449
285
  zend_compile_expr(&arg_node, args->child[0]);
4450
285
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4451
285
  opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING);
4452
285
  return SUCCESS;
4453
515
}
4454
4455
static zend_result zend_compile_func_cast(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4456
939
{
4457
939
  znode arg_node;
4458
939
  zend_op *opline;
4459
4460
939
  if (args->children != 1) {
4461
329
    return FAILURE;
4462
329
  }
4463
4464
610
  zend_compile_expr(&arg_node, args->child[0]);
4465
610
  if (type == _IS_BOOL) {
4466
194
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL);
4467
416
  } else {
4468
416
    opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
4469
416
    opline->extended_value = type;
4470
416
  }
4471
610
  return SUCCESS;
4472
939
}
4473
/* }}} */
4474
4475
static zend_result zend_compile_func_defined(znode *result, const zend_ast_list *args) /* {{{ */
4476
2.33k
{
4477
2.33k
  zend_string *name;
4478
2.33k
  zend_op *opline;
4479
4480
2.33k
  if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
4481
168
    return FAILURE;
4482
168
  }
4483
4484
2.16k
  name = zval_get_string(zend_ast_get_zval(args->child[0]));
4485
2.16k
  if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
4486
591
    zend_string_release_ex(name, 0);
4487
591
    return FAILURE;
4488
591
  }
4489
4490
1.57k
  if (zend_try_ct_eval_const(&result->u.constant, name, false)) {
4491
80
    zend_string_release_ex(name, 0);
4492
80
    zval_ptr_dtor(&result->u.constant);
4493
80
    ZVAL_TRUE(&result->u.constant);
4494
80
    result->op_type = IS_CONST;
4495
80
    return SUCCESS;
4496
80
  }
4497
4498
1.49k
  opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
4499
1.49k
  opline->op1_type = IS_CONST;
4500
1.49k
  LITERAL_STR(opline->op1, name);
4501
1.49k
  opline->extended_value = zend_alloc_cache_slot();
4502
4503
1.49k
  return SUCCESS;
4504
1.57k
}
4505
/* }}} */
4506
4507
static zend_result zend_compile_func_chr(znode *result, const zend_ast_list *args) /* {{{ */
4508
704
{
4509
704
  zval *zint;
4510
704
  if (
4511
704
    args->children == 1
4512
509
    && args->child[0]->kind == ZEND_AST_ZVAL
4513
408
    && (zint = zend_ast_get_zval(args->child[0]))
4514
408
    && Z_TYPE_P(zint) == IS_LONG
4515
341
    && Z_LVAL_P(zint) >= 0
4516
341
    && Z_LVAL_P(zint) <= 255
4517
704
  ) {
4518
248
    result->op_type = IS_CONST;
4519
248
    ZVAL_CHAR(&result->u.constant, Z_LVAL_P(zint));
4520
248
    return SUCCESS;
4521
456
  } else {
4522
456
    return FAILURE;
4523
456
  }
4524
704
}
4525
/* }}} */
4526
4527
static zend_result zend_compile_func_ord(znode *result, const zend_ast_list *args) /* {{{ */
4528
1.42k
{
4529
1.42k
  zval *str;
4530
1.42k
  if (
4531
1.42k
    args->children == 1
4532
1.35k
    && args->child[0]->kind == ZEND_AST_ZVAL
4533
1.14k
    && (str = zend_ast_get_zval(args->child[0]))
4534
1.14k
    && Z_TYPE_P(str) == IS_STRING
4535
242
    && Z_STRLEN_P(str) == 1
4536
1.42k
  ) {
4537
194
    result->op_type = IS_CONST;
4538
194
    ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(str)[0]);
4539
194
    return SUCCESS;
4540
1.22k
  } else {
4541
1.22k
    return FAILURE;
4542
1.22k
  }
4543
1.42k
}
4544
/* }}} */
4545
4546
/* We can only calculate the stack size for functions that have been fully compiled, otherwise
4547
 * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for
4548
 * directly or indirectly recursive function calls. */
4549
66.8k
static bool fbc_is_finalized(const zend_function *fbc) {
4550
66.8k
  return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO);
4551
66.8k
}
4552
4553
static bool zend_compile_ignore_class(const zend_class_entry *ce, const zend_string *filename)
4554
14.0k
{
4555
14.0k
  if (ce->type == ZEND_INTERNAL_CLASS) {
4556
6.91k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES;
4557
7.13k
  } else {
4558
7.13k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4559
29
      && ce->info.user.filename != filename;
4560
7.13k
  }
4561
14.0k
}
4562
4563
static bool zend_compile_ignore_function(const zend_function *fbc, const zend_string *filename)
4564
37.0k
{
4565
37.0k
  if (fbc->type == ZEND_INTERNAL_FUNCTION) {
4566
35.5k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS;
4567
35.5k
  } else {
4568
1.42k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)
4569
1.42k
      || ((CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4570
172
        && fbc->op_array.filename != filename);
4571
1.42k
  }
4572
37.0k
}
4573
4574
static zend_result zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
4575
3.15k
{
4576
3.15k
  zend_string *name, *lcname;
4577
3.15k
  zend_function *fbc;
4578
3.15k
  zend_op *opline;
4579
4580
3.15k
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
4581
2.30k
    return FAILURE;
4582
2.30k
  }
4583
4584
852
  name = zend_ast_get_str(name_ast);
4585
852
  lcname = zend_string_tolower(name);
4586
4587
852
  fbc = zend_hash_find_ptr(CG(function_table), lcname);
4588
852
  if (!fbc
4589
286
   || !fbc_is_finalized(fbc)
4590
566
   || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
4591
566
    zend_string_release_ex(lcname, 0);
4592
566
    return FAILURE;
4593
566
  }
4594
4595
286
  opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
4596
286
  opline->extended_value = num_args;
4597
286
  opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
4598
286
  opline->op2_type = IS_CONST;
4599
286
  LITERAL_STR(opline->op2, lcname);
4600
286
  opline->result.num = zend_alloc_cache_slot();
4601
4602
286
  return SUCCESS;
4603
852
}
4604
/* }}} */
4605
4606
static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
4607
3.15k
{
4608
3.15k
  zend_op *opline;
4609
3.15k
  znode name_node;
4610
4611
3.15k
  if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
4612
286
    return;
4613
286
  }
4614
4615
2.86k
  zend_compile_expr(&name_node, name_ast);
4616
4617
2.86k
  opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
4618
2.86k
  opline->op1_type = IS_CONST;
4619
2.86k
  LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
4620
2.86k
  opline->extended_value = num_args;
4621
2.86k
}
4622
/* }}} */
4623
4624
/* cufa = call_user_func_array */
4625
static zend_result zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t type) /* {{{ */
4626
1.92k
{
4627
1.92k
  znode arg_node;
4628
1.92k
  zend_op *opline;
4629
4630
1.92k
  if (args->children != 2) {
4631
93
    return FAILURE;
4632
93
  }
4633
4634
1.83k
  zend_compile_init_user_func(args->child[0], 0, lcname);
4635
1.83k
  if (args->child[1]->kind == ZEND_AST_CALL
4636
1.73k
   && args->child[1]->child[0]->kind == ZEND_AST_ZVAL
4637
1.62k
   && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING
4638
1.50k
   && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) {
4639
1.41k
    zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]);
4640
1.41k
    zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]);
4641
1.41k
    bool is_fully_qualified;
4642
1.41k
    zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified);
4643
4644
1.41k
    if (zend_string_equals_literal_ci(name, "array_slice")
4645
756
       && !zend_args_contain_unpack_or_named(list)
4646
592
     && list->children == 3
4647
498
     && list->child[1]->kind == ZEND_AST_ZVAL) {
4648
399
      zval *zv = zend_ast_get_zval(list->child[1]);
4649
4650
399
      if (Z_TYPE_P(zv) == IS_LONG
4651
378
       && Z_LVAL_P(zv) >= 0
4652
378
       && Z_LVAL_P(zv) <= 0x7fffffff) {
4653
274
        zend_op *opline;
4654
274
        znode len_node;
4655
4656
274
        zend_compile_expr(&arg_node, list->child[0]);
4657
274
        zend_compile_expr(&len_node, list->child[2]);
4658
274
        opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node);
4659
274
        opline->extended_value = Z_LVAL_P(zv);
4660
274
        opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4661
274
        if (type == BP_VAR_R || type == BP_VAR_IS) {
4662
238
          opline->result_type = IS_TMP_VAR;
4663
238
          result->op_type = IS_TMP_VAR;
4664
238
        }
4665
274
        zend_string_release_ex(name, 0);
4666
274
        return SUCCESS;
4667
274
      }
4668
399
    }
4669
1.14k
    zend_string_release_ex(name, 0);
4670
1.14k
  }
4671
1.56k
  zend_compile_expr(&arg_node, args->child[1]);
4672
1.56k
  zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
4673
1.56k
  zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
4674
1.56k
  opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4675
1.56k
  if (type == BP_VAR_R || type == BP_VAR_IS) {
4676
1.49k
    opline->result_type = IS_TMP_VAR;
4677
1.49k
    result->op_type = IS_TMP_VAR;
4678
1.49k
  }
4679
1.56k
  opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4680
4681
1.56k
  return SUCCESS;
4682
1.83k
}
4683
/* }}} */
4684
4685
/* cuf = call_user_func */
4686
static zend_result zend_compile_func_cuf(znode *result, const zend_ast_list *args, zend_string *lcname, uint32_t type) /* {{{ */
4687
1.51k
{
4688
1.51k
  uint32_t i;
4689
4690
1.51k
  if (args->children < 1) {
4691
197
    return FAILURE;
4692
197
  }
4693
4694
1.31k
  zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
4695
2.58k
  for (i = 1; i < args->children; ++i) {
4696
1.26k
    zend_ast *arg_ast = args->child[i];
4697
1.26k
    znode arg_node;
4698
1.26k
    zend_op *opline;
4699
4700
1.26k
    zend_compile_expr(&arg_node, arg_ast);
4701
4702
1.26k
    opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
4703
1.26k
    opline->op2.num = i;
4704
1.26k
    opline->result.var = EX_NUM_TO_VAR(i - 1);
4705
1.26k
  }
4706
1.31k
  zend_op *opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4707
1.31k
  if (type == BP_VAR_R || type == BP_VAR_IS) {
4708
258
    opline->result_type = IS_TMP_VAR;
4709
258
    result->op_type = IS_TMP_VAR;
4710
258
  }
4711
4712
1.31k
  return SUCCESS;
4713
1.51k
}
4714
/* }}} */
4715
4716
static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, const zend_function *fbc, uint32_t lineno, uint32_t type) /* {{{ */
4717
88.0k
{
4718
88.0k
  if (EG(assertions) >= 0) {
4719
88.0k
    znode name_node;
4720
88.0k
    zend_op *opline;
4721
88.0k
    uint32_t check_op_number = get_next_op_number();
4722
4723
88.0k
    zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
4724
4725
88.0k
    if (fbc && fbc_is_finalized(fbc)) {
4726
29.8k
      name_node.op_type = IS_CONST;
4727
29.8k
      ZVAL_STR_COPY(&name_node.u.constant, name);
4728
4729
29.8k
      opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
4730
58.2k
    } else {
4731
58.2k
      opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
4732
58.2k
      opline->op2_type = IS_CONST;
4733
58.2k
      opline->op2.constant = zend_add_ns_func_name_literal(name);
4734
58.2k
    }
4735
88.0k
    opline->result.num = zend_alloc_cache_slot();
4736
4737
88.0k
    if (args->children == 1) {
4738
      /* add "assert(condition) as assertion message */
4739
29.9k
      zend_ast *arg = zend_ast_create_zval_from_str(
4740
29.9k
        zend_ast_export("assert(", args->child[0], ")"));
4741
29.9k
      if (args->child[0]->kind == ZEND_AST_NAMED_ARG) {
4742
        /* If the original argument was named, add the new argument as named as well,
4743
         * as mixing named and positional is not allowed. */
4744
1.38k
        zend_ast *name = zend_ast_create_zval_from_str(
4745
1.38k
          ZSTR_INIT_LITERAL("description", 0));
4746
1.38k
        arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg);
4747
1.38k
      }
4748
29.9k
      args = (zend_ast_list *)zend_ast_list_add((zend_ast *) args, arg);
4749
29.9k
    }
4750
4751
88.0k
    zend_compile_call_common(result, (zend_ast*)args, fbc, lineno, type);
4752
4753
88.0k
    opline = &CG(active_op_array)->opcodes[check_op_number];
4754
88.0k
    opline->op2.opline_num = get_next_op_number();
4755
88.0k
    SET_NODE(opline->result, result);
4756
88.0k
  } else {
4757
0
    if (!fbc) {
4758
0
      zend_string_release_ex(name, 0);
4759
0
    }
4760
0
    result->op_type = IS_CONST;
4761
0
    ZVAL_TRUE(&result->u.constant);
4762
0
  }
4763
88.0k
}
4764
/* }}} */
4765
4766
static zend_result zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */
4767
5.09k
{
4768
5.09k
  bool strict = false;
4769
5.09k
  znode array, needly;
4770
5.09k
  zend_op *opline;
4771
4772
5.09k
  if (args->children == 3) {
4773
2.87k
    if (args->child[2]->kind == ZEND_AST_ZVAL) {
4774
694
      strict = zend_is_true(zend_ast_get_zval(args->child[2]));
4775
2.18k
    } else if (args->child[2]->kind == ZEND_AST_CONST) {
4776
1.96k
      zval value;
4777
1.96k
      zend_ast *name_ast = args->child[2]->child[0];
4778
1.96k
      bool is_fully_qualified;
4779
1.96k
      zend_string *resolved_name = zend_resolve_const_name(
4780
1.96k
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
4781
4782
1.96k
      if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) {
4783
604
        zend_string_release_ex(resolved_name, 0);
4784
604
        return FAILURE;
4785
604
      }
4786
4787
1.36k
      zend_string_release_ex(resolved_name, 0);
4788
1.36k
      strict = zend_is_true(&value);
4789
1.36k
      zval_ptr_dtor(&value);
4790
1.36k
    } else {
4791
219
      return FAILURE;
4792
219
    }
4793
2.87k
  } else if (args->children != 2) {
4794
849
    return FAILURE;
4795
849
  }
4796
4797
3.42k
  if (args->child[1]->kind != ZEND_AST_ARRAY
4798
3.06k
   || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) {
4799
653
    return FAILURE;
4800
653
  }
4801
4802
2.76k
  if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) {
4803
1.41k
    bool ok = true;
4804
1.41k
    zval *val, tmp;
4805
1.41k
    HashTable *src = Z_ARRVAL(array.u.constant);
4806
1.41k
    HashTable *dst = zend_new_array(zend_hash_num_elements(src));
4807
4808
1.41k
    ZVAL_TRUE(&tmp);
4809
4810
1.41k
    if (strict) {
4811
4.44k
      ZEND_HASH_FOREACH_VAL(src, val) {
4812
4.44k
        if (Z_TYPE_P(val) == IS_STRING) {
4813
66
          zend_hash_add(dst, Z_STR_P(val), &tmp);
4814
1.19k
        } else if (Z_TYPE_P(val) == IS_LONG) {
4815
1.12k
          zend_hash_index_add(dst, Z_LVAL_P(val), &tmp);
4816
1.12k
        } else {
4817
69
          zend_array_destroy(dst);
4818
69
          ok = false;
4819
69
          break;
4820
69
        }
4821
4.44k
      } ZEND_HASH_FOREACH_END();
4822
733
    } else {
4823
4.74k
      ZEND_HASH_FOREACH_VAL(src, val) {
4824
4.74k
        if (Z_TYPE_P(val) != IS_STRING
4825
1.36k
         || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) {
4826
483
          zend_array_destroy(dst);
4827
483
          ok = false;
4828
483
          break;
4829
483
        }
4830
1.22k
        zend_hash_add(dst, Z_STR_P(val), &tmp);
4831
1.22k
      } ZEND_HASH_FOREACH_END();
4832
733
    }
4833
4834
1.41k
    zend_array_destroy(src);
4835
1.41k
    if (!ok) {
4836
552
      return FAILURE;
4837
552
    }
4838
860
    Z_ARRVAL(array.u.constant) = dst;
4839
860
  }
4840
2.21k
  array.op_type = IS_CONST;
4841
4842
2.21k
  zend_compile_expr(&needly, args->child[0]);
4843
4844
2.21k
  opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array);
4845
2.21k
  opline->extended_value = strict;
4846
4847
2.21k
  return SUCCESS;
4848
2.76k
}
4849
/* }}} */
4850
4851
static zend_result zend_compile_func_count(znode *result, const zend_ast_list *args, const zend_string *lcname) /* {{{ */
4852
358
{
4853
358
  znode arg_node;
4854
358
  zend_op *opline;
4855
4856
358
  if (args->children != 1) {
4857
198
    return FAILURE;
4858
198
  }
4859
4860
160
  zend_compile_expr(&arg_node, args->child[0]);
4861
160
  opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL);
4862
160
  opline->extended_value = zend_string_equals_literal(lcname, "sizeof");
4863
4864
160
  return SUCCESS;
4865
358
}
4866
/* }}} */
4867
4868
static zend_result zend_compile_func_get_class(znode *result, const zend_ast_list *args) /* {{{ */
4869
694
{
4870
694
  if (args->children == 0) {
4871
228
    zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL);
4872
466
  } else {
4873
466
    znode arg_node;
4874
4875
466
    if (args->children != 1) {
4876
195
      return FAILURE;
4877
195
    }
4878
4879
271
    zend_compile_expr(&arg_node, args->child[0]);
4880
271
    zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL);
4881
271
  }
4882
499
  return SUCCESS;
4883
694
}
4884
/* }}} */
4885
4886
static zend_result zend_compile_func_get_called_class(znode *result, const zend_ast_list *args) /* {{{ */
4887
375
{
4888
375
  if (args->children != 0) {
4889
233
    return FAILURE;
4890
233
  }
4891
4892
142
  zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL);
4893
142
  return SUCCESS;
4894
375
}
4895
/* }}} */
4896
4897
static zend_result zend_compile_func_gettype(znode *result, const zend_ast_list *args) /* {{{ */
4898
618
{
4899
618
  znode arg_node;
4900
4901
618
  if (args->children != 1) {
4902
565
    return FAILURE;
4903
565
  }
4904
4905
53
  zend_compile_expr(&arg_node, args->child[0]);
4906
53
  zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL);
4907
53
  return SUCCESS;
4908
618
}
4909
/* }}} */
4910
4911
static zend_result zend_compile_func_num_args(znode *result, const zend_ast_list *args) /* {{{ */
4912
227
{
4913
227
  if (CG(active_op_array)->function_name && args->children == 0) {
4914
57
    zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL);
4915
57
    return SUCCESS;
4916
170
  } else {
4917
170
    return FAILURE;
4918
170
  }
4919
227
}
4920
/* }}} */
4921
4922
static zend_result zend_compile_func_get_args(znode *result, const zend_ast_list *args) /* {{{ */
4923
726
{
4924
726
  if (CG(active_op_array)->function_name && args->children == 0) {
4925
379
    zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL);
4926
379
    return SUCCESS;
4927
379
  } else {
4928
347
    return FAILURE;
4929
347
  }
4930
726
}
4931
/* }}} */
4932
4933
static zend_result zend_compile_func_array_key_exists(znode *result, const zend_ast_list *args) /* {{{ */
4934
371
{
4935
371
  znode subject, needle;
4936
4937
371
  if (args->children != 2) {
4938
260
    return FAILURE;
4939
260
  }
4940
4941
111
  zend_compile_expr(&needle, args->child[0]);
4942
111
  zend_compile_expr(&subject, args->child[1]);
4943
4944
111
  zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject);
4945
111
  return SUCCESS;
4946
371
}
4947
/* }}} */
4948
4949
static zend_result zend_compile_func_array_slice(znode *result, const zend_ast_list *args) /* {{{ */
4950
1.84k
{
4951
1.84k
  if (CG(active_op_array)->function_name
4952
1.17k
   && args->children == 2
4953
884
   && args->child[0]->kind == ZEND_AST_CALL
4954
774
   && args->child[0]->child[0]->kind == ZEND_AST_ZVAL
4955
669
   && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING
4956
669
   && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST
4957
659
   && args->child[1]->kind == ZEND_AST_ZVAL) {
4958
4959
573
    zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]);
4960
573
    bool is_fully_qualified;
4961
573
    zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified);
4962
573
    const zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]);
4963
573
    const zval *zv = zend_ast_get_zval(args->child[1]);
4964
573
    znode first;
4965
4966
573
    if (zend_string_equals_literal_ci(name, "func_get_args")
4967
440
     && list->children == 0
4968
331
     && Z_TYPE_P(zv) == IS_LONG
4969
126
     && Z_LVAL_P(zv) >= 0) {
4970
126
      first.op_type = IS_CONST;
4971
126
      ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv));
4972
126
      zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL);
4973
126
      zend_string_release_ex(name, 0);
4974
126
      return SUCCESS;
4975
126
    }
4976
447
    zend_string_release_ex(name, 0);
4977
447
  }
4978
1.72k
  return FAILURE;
4979
1.84k
}
4980
/* }}} */
4981
4982
static uint32_t find_frameless_function_offset(uint32_t arity, const void *handler)
4983
1.71M
{
4984
1.71M
  void **handlers = zend_flf_handlers;
4985
1.71M
  void **current = handlers;
4986
15.5M
  while (*current) {
4987
15.5M
    if (*current == handler) {
4988
1.71M
      return current - handlers;
4989
1.71M
    }
4990
13.8M
    current++;
4991
13.8M
  }
4992
4993
  /* Unreachable in practice: zend_register_functions() records the handler
4994
   * of every frameless_function_infos entry at registration time. */
4995
0
  return (uint32_t)-1;
4996
1.71M
}
4997
4998
static const zend_frameless_function_info *find_frameless_function_info(const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4999
892k
{
5000
892k
  if (zend_execute_internal) {
5001
2.87k
    return NULL;
5002
2.87k
  }
5003
5004
889k
  if (ZEND_USER_CODE(fbc->type)) {
5005
35
    return NULL;
5006
35
  }
5007
5008
889k
  const zend_frameless_function_info *frameless_function_info = fbc->internal_function.frameless_function_infos;
5009
889k
  if (!frameless_function_info) {
5010
27.9k
    return NULL;
5011
27.9k
  }
5012
5013
861k
  if (args->children > 3) {
5014
835
    return NULL;
5015
835
  }
5016
5017
979k
  while (frameless_function_info->handler) {
5018
974k
    if (frameless_function_info->num_args >= args->children
5019
864k
     && fbc->common.required_num_args <= args->children
5020
855k
     && (!(fbc->common.fn_flags & ZEND_ACC_VARIADIC)
5021
855k
      || frameless_function_info->num_args == args->children)) {
5022
855k
      uint32_t num_args = frameless_function_info->num_args;
5023
855k
      uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
5024
855k
      if (offset != (uint32_t)-1) {
5025
855k
        return frameless_function_info;
5026
855k
      }
5027
855k
    }
5028
118k
    frameless_function_info++;
5029
118k
  }
5030
5031
4.78k
  return NULL;
5032
860k
}
5033
5034
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)
5035
855k
{
5036
855k
  uint32_t lineno = CG(zend_lineno);
5037
855k
  uint32_t num_args = frameless_function_info->num_args;
5038
855k
  uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
5039
855k
  znode arg_zvs[3];
5040
2.67M
  for (uint32_t i = 0; i < num_args; i++) {
5041
1.81M
    if (i < args->children) {
5042
1.81M
      zend_compile_expr(&arg_zvs[i], args->child[i]);
5043
1.81M
    } else {
5044
0
      const zend_arg_info *arg_info = &fbc->common.arg_info[i];
5045
0
      arg_zvs[i].op_type = IS_CONST;
5046
0
      if (zend_get_default_from_internal_arg_info(&arg_zvs[i].u.constant, arg_info) == FAILURE) {
5047
0
        ZEND_UNREACHABLE();
5048
0
      }
5049
0
    }
5050
1.81M
  }
5051
855k
  uint8_t opcode = ZEND_FRAMELESS_ICALL_0 + num_args;
5052
855k
  uint32_t opnum = get_next_op_number();
5053
855k
  zend_op *opline = zend_emit_op_tmp(result, opcode, NULL, NULL);
5054
855k
  opline->extended_value = offset;
5055
855k
  opline->lineno = lineno;
5056
855k
  if (num_args >= 1) {
5057
855k
    SET_NODE(opline->op1, &arg_zvs[0]);
5058
855k
  }
5059
855k
  if (num_args >= 2) {
5060
852k
    SET_NODE(opline->op2, &arg_zvs[1]);
5061
852k
  }
5062
855k
  if (num_args >= 3) {
5063
109k
    zend_emit_op_data(&arg_zvs[2]);
5064
109k
  }
5065
855k
  return opnum;
5066
855k
}
5067
5068
static uint32_t zend_compile_frameless_icall(znode *result, const zend_ast_list *args, const zend_function *fbc, uint32_t type)
5069
19.7k
{
5070
19.7k
  const zend_frameless_function_info *frameless_function_info = find_frameless_function_info(args, fbc, type);
5071
19.7k
  if (!frameless_function_info) {
5072
17.3k
    return (uint32_t)-1;
5073
17.3k
  }
5074
5075
2.35k
  return zend_compile_frameless_icall_ex(result, args, fbc, frameless_function_info, type);
5076
19.7k
}
5077
5078
static void zend_compile_ns_call(znode *result, const znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */
5079
4.21M
{
5080
4.21M
  int name_constants = zend_add_ns_func_name_literal(Z_STR(name_node->u.constant));
5081
5082
  /* Find frameless function with same name. */
5083
4.21M
  const zend_function *frameless_function = NULL;
5084
4.21M
  if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT
5085
4.21M
   && !zend_args_contain_unpack_or_named(zend_ast_get_list(args_ast))
5086
   /* Avoid blowing up op count with nested frameless branches. */
5087
4.20M
   && !CG(context).in_jmp_frameless_branch) {
5088
2.52M
    zend_string *lc_func_name = Z_STR_P(CT_CONSTANT_EX(CG(active_op_array), name_constants + 2));
5089
2.52M
    frameless_function = zend_hash_find_ptr(CG(function_table), lc_func_name);
5090
2.52M
  }
5091
5092
  /* Check whether any frameless handler may actually be used. */
5093
4.21M
  uint32_t jmp_fl_opnum = 0;
5094
4.21M
  const zend_frameless_function_info *frameless_function_info = NULL;
5095
4.21M
  if (frameless_function) {
5096
872k
    frameless_function_info = find_frameless_function_info(zend_ast_get_list(args_ast), frameless_function, type);
5097
872k
    if (frameless_function_info) {
5098
853k
      CG(context).in_jmp_frameless_branch = true;
5099
853k
      znode op1;
5100
853k
      op1.op_type = IS_CONST;
5101
853k
      ZVAL_COPY(&op1.u.constant, CT_CONSTANT_EX(CG(active_op_array), name_constants + 1));
5102
853k
      jmp_fl_opnum = get_next_op_number();
5103
853k
      zend_emit_op(NULL, ZEND_JMP_FRAMELESS, &op1, NULL);
5104
853k
    }
5105
872k
  }
5106
5107
  /* Compile ns call. */
5108
4.21M
  zend_op *opline = get_next_op();
5109
4.21M
  opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
5110
4.21M
  opline->op2_type = IS_CONST;
5111
4.21M
  opline->op2.constant = name_constants;
5112
4.21M
  opline->result.num = zend_alloc_cache_slot();
5113
4.21M
  zend_compile_call_common(result, args_ast, NULL, lineno, type);
5114
5115
  /* Compile frameless call. */
5116
4.21M
  if (frameless_function_info) {
5117
853k
    CG(zend_lineno) = lineno;
5118
5119
853k
    uint32_t jmp_end_opnum = zend_emit_jump(0);
5120
853k
    uint32_t jmp_fl_target = get_next_op_number();
5121
5122
853k
    uint32_t flf_icall_opnum = zend_compile_frameless_icall_ex(NULL, zend_ast_get_list(args_ast), frameless_function, frameless_function_info, type);
5123
5124
853k
    zend_op *jmp_fl = &CG(active_op_array)->opcodes[jmp_fl_opnum];
5125
853k
    jmp_fl->op2.opline_num = jmp_fl_target;
5126
853k
    jmp_fl->extended_value = zend_alloc_cache_slot();
5127
853k
    zend_op *flf_icall = &CG(active_op_array)->opcodes[flf_icall_opnum];
5128
853k
    SET_NODE(flf_icall->result, result);
5129
853k
    zend_update_jump_target_to_next(jmp_end_opnum);
5130
5131
853k
    CG(context).in_jmp_frameless_branch = false;
5132
853k
  }
5133
4.21M
}
5134
/* }}} */
5135
5136
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node);
5137
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node);
5138
static void zend_compile_rope_finalize(znode *result, uint32_t j, zend_op *init_opline, zend_op *opline);
5139
5140
static zend_result zend_compile_func_sprintf(znode *result, zend_ast_list *args) /* {{{ */
5141
2.87k
{
5142
  /* Bail out if we do not have a format string. */
5143
2.87k
  if (args->children < 1) {
5144
200
    return FAILURE;
5145
200
  }
5146
5147
2.67k
  zend_eval_const_expr(&args->child[0]);
5148
  /* Bail out if the format string is not constant. */
5149
2.67k
  if (args->child[0]->kind != ZEND_AST_ZVAL) {
5150
348
    return FAILURE;
5151
348
  }
5152
5153
2.32k
  zval *format_string = zend_ast_get_zval(args->child[0]);
5154
2.32k
  if (Z_TYPE_P(format_string) != IS_STRING) {
5155
66
    return FAILURE;
5156
66
  }
5157
2.25k
  if (Z_STRLEN_P(format_string) >= 256) {
5158
34
    return FAILURE;
5159
34
  }
5160
5161
2.22k
  char *p;
5162
2.22k
  char *end;
5163
2.22k
  uint32_t placeholder_count;
5164
5165
2.22k
  placeholder_count = 0;
5166
2.22k
  p = Z_STRVAL_P(format_string);
5167
2.22k
  end = p + Z_STRLEN_P(format_string);
5168
5169
5.59k
  for (;;) {
5170
5.59k
    p = memchr(p, '%', end - p);
5171
5.59k
    if (!p) {
5172
1.85k
      break;
5173
1.85k
    }
5174
5175
3.73k
    char *q = p + 1;
5176
3.73k
    if (q == end) {
5177
206
      return FAILURE;
5178
206
    }
5179
5180
3.53k
    switch (*q) {
5181
1.63k
      case 's':
5182
1.75k
      case 'd':
5183
1.75k
        placeholder_count++;
5184
1.75k
        break;
5185
1.61k
      case '%':
5186
1.61k
        break;
5187
163
      default:
5188
163
        return FAILURE;
5189
3.53k
    }
5190
5191
3.37k
    p = q;
5192
3.37k
    p++;
5193
3.37k
  }
5194
5195
  /* Bail out if the number of placeholders does not match the number of values. */
5196
1.85k
  if (placeholder_count != (args->children - 1)) {
5197
121
    return FAILURE;
5198
121
  }
5199
5200
  /* Handle empty format strings. */
5201
1.73k
  if (Z_STRLEN_P(format_string) == 0) {
5202
70
    result->op_type = IS_CONST;
5203
70
    ZVAL_EMPTY_STRING(&result->u.constant);
5204
5205
70
    return SUCCESS;
5206
70
  }
5207
5208
1.66k
  znode *elements = NULL;
5209
5210
1.66k
  if (placeholder_count > 0) {
5211
1.23k
    elements = safe_emalloc(sizeof(*elements), placeholder_count, 0);
5212
1.23k
  }
5213
5214
  /* Compile the value expressions first for error handling that is consistent
5215
   * with a function call: Values that fail to convert to a string may emit errors.
5216
   */
5217
3.22k
  for (uint32_t i = 0; i < placeholder_count; i++) {
5218
1.55k
    zend_compile_expr(elements + i, args->child[1 + i]);
5219
1.55k
  }
5220
5221
1.66k
  uint32_t rope_elements = 0;
5222
1.66k
  uint32_t rope_init_lineno = -1;
5223
1.66k
  zend_op *opline = NULL;
5224
5225
1.66k
  placeholder_count = 0;
5226
1.66k
  p = Z_STRVAL_P(format_string);
5227
1.66k
  end = p + Z_STRLEN_P(format_string);
5228
1.66k
  char *offset = p;
5229
4.66k
  for (;;) {
5230
4.66k
    p = memchr(p, '%', end - p);
5231
4.66k
    if (!p) {
5232
1.66k
      break;
5233
1.66k
    }
5234
5235
3.00k
    char *q = p + 1;
5236
3.00k
    ZEND_ASSERT(q < end);
5237
3.00k
    ZEND_ASSERT(*q == 's' || *q == 'd' || *q == '%');
5238
5239
3.00k
    if (*q == '%') {
5240
      /* Optimization to not create a dedicated rope element for the literal '%':
5241
       * Include the first '%' within the "constant" part instead of dropping the
5242
       * full placeholder.
5243
       */
5244
1.44k
      p++;
5245
1.44k
    }
5246
5247
3.00k
    if (p != offset) {
5248
2.14k
      znode const_node;
5249
2.14k
      const_node.op_type = IS_CONST;
5250
2.14k
      ZVAL_STRINGL(&const_node.u.constant, offset, p - offset);
5251
2.14k
      if (rope_elements == 0) {
5252
434
        rope_init_lineno = get_next_op_number();
5253
434
      }
5254
2.14k
      opline = zend_compile_rope_add(result, rope_elements++, &const_node);
5255
2.14k
    }
5256
5257
3.00k
    if (*q != '%') {
5258
1.55k
      switch (*q) {
5259
1.46k
        case 's':
5260
          /* Perform the cast of constants when actually evaluating the corresponding placeholder
5261
           * for correct error reporting.
5262
           */
5263
1.46k
          if (elements[placeholder_count].op_type == IS_CONST) {
5264
663
            if (Z_TYPE(elements[placeholder_count].u.constant) == IS_ARRAY) {
5265
67
              zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_STRING;
5266
596
            } else {
5267
596
              convert_to_string(&elements[placeholder_count].u.constant);
5268
596
            }
5269
663
          }
5270
1.46k
          break;
5271
93
        case 'd':
5272
93
          zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_LONG;
5273
93
          break;
5274
0
        default: ZEND_UNREACHABLE();
5275
1.55k
      }
5276
5277
1.55k
      if (rope_elements == 0) {
5278
861
        rope_init_lineno = get_next_op_number();
5279
861
      }
5280
1.55k
      opline = zend_compile_rope_add(result, rope_elements++, &elements[placeholder_count]);
5281
5282
1.55k
      placeholder_count++;
5283
1.55k
    }
5284
5285
3.00k
    p = q;
5286
3.00k
    p++;
5287
3.00k
    offset = p;
5288
3.00k
  }
5289
1.66k
  if (end != offset) {
5290
    /* Add the constant part after the last placeholder. */
5291
768
    znode const_node;
5292
768
    const_node.op_type = IS_CONST;
5293
768
    ZVAL_STRINGL(&const_node.u.constant, offset, end - offset);
5294
768
    if (rope_elements == 0) {
5295
369
      rope_init_lineno = get_next_op_number();
5296
369
    }
5297
768
    opline = zend_compile_rope_add(result, rope_elements++, &const_node);
5298
768
  }
5299
1.66k
  ZEND_ASSERT(opline != NULL);
5300
5301
1.66k
  zend_op *init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
5302
1.66k
  zend_compile_rope_finalize(result, rope_elements, init_opline, opline);
5303
1.66k
  efree(elements);
5304
5305
1.66k
  return SUCCESS;
5306
1.66k
}
5307
5308
static zend_result zend_compile_func_printf(znode *result, zend_ast_list *args) /* {{{ */
5309
1.93k
{
5310
  /* Special case: printf with a single constant string argument and no format specifiers.
5311
   * In this case, just emit ECHO and return the string length if needed. */
5312
1.93k
  if (args->children == 1) {
5313
956
    zend_eval_const_expr(&args->child[0]);
5314
956
    if (args->child[0]->kind != ZEND_AST_ZVAL) {
5315
237
      return FAILURE;
5316
237
    }
5317
719
    zval *format_string = zend_ast_get_zval(args->child[0]);
5318
719
    if (Z_TYPE_P(format_string) != IS_STRING) {
5319
195
      return FAILURE;
5320
195
    }
5321
    /* Check if there are any format specifiers */
5322
524
    if (!memchr(Z_STRVAL_P(format_string), '%', Z_STRLEN_P(format_string))) {
5323
      /* No format specifiers - just emit ECHO and return string length */
5324
74
      znode format_node;
5325
74
      zend_compile_expr(&format_node, args->child[0]);
5326
74
      zend_emit_op(NULL, ZEND_ECHO, &format_node, NULL);
5327
5328
      /* Return the string length as a constant if the result is used */
5329
74
      result->op_type = IS_CONST;
5330
74
      ZVAL_LONG(&result->u.constant, Z_STRLEN_P(format_string));
5331
74
      return SUCCESS;
5332
74
    }
5333
524
  }
5334
5335
  /* Fall back to sprintf optimization for format strings with specifiers */
5336
1.42k
  znode rope_result;
5337
1.42k
  if (zend_compile_func_sprintf(&rope_result, args) != SUCCESS) {
5338
476
    return FAILURE;
5339
476
  }
5340
5341
  /* printf() returns the amount of bytes written, so just an ECHO of the
5342
   * resulting sprintf() optimisation might not be enough. At this early
5343
   * stage we can't detect if the result is actually used, so we just emit
5344
   * the opcodes and let them be cleaned up by the dead code elimination
5345
   * pass in the Zend Optimizer if the result of the printf() is in fact
5346
   * unused */
5347
951
  znode copy;
5348
951
  if (rope_result.op_type != IS_CONST) {
5349
    /* Note: ZEND_COPY_TMP is only valid for TMPVAR. */
5350
638
    ZEND_ASSERT(rope_result.op_type == IS_TMP_VAR);
5351
638
    zend_emit_op_tmp(&copy, ZEND_COPY_TMP, &rope_result, NULL);
5352
638
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5353
638
    zend_emit_op_tmp(result, ZEND_STRLEN, &copy, NULL);
5354
638
  } else {
5355
313
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5356
313
    result->op_type = IS_CONST;
5357
313
    ZVAL_LONG(&result->u.constant, Z_STRLEN(rope_result.u.constant));
5358
313
  }
5359
5360
951
  return SUCCESS;
5361
951
}
5362
5363
static zend_result zend_compile_func_clone(znode *result, const zend_ast_list *args)
5364
1.18k
{
5365
1.18k
  znode arg_node;
5366
5367
1.18k
  if (args->children != 1) {
5368
198
    return FAILURE;
5369
198
  }
5370
5371
989
  zend_compile_expr(&arg_node, args->child[0]);
5372
989
  zend_emit_op_tmp(result, ZEND_CLONE, &arg_node, NULL);
5373
5374
989
  return SUCCESS;
5375
1.18k
}
5376
5377
static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t lineno) /* {{{ */
5378
526
{
5379
  /* array_map() as an internal function calls the callback as if strict_types=0,
5380
   * this optimization is therefore not legal if strict_types=1. */
5381
526
  if (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES) {
5382
71
    return FAILURE;
5383
71
  }
5384
5385
  /* Bail out if we do not have exactly two parameters. */
5386
455
  if (args->children != 2) {
5387
98
    return FAILURE;
5388
98
  }
5389
5390
  /* Bail out if callback is not a first class callable */
5391
357
  zend_ast *callback = args->child[0];
5392
357
  if (callback->kind != ZEND_AST_CALL && callback->kind != ZEND_AST_STATIC_CALL) {
5393
47
    return FAILURE;
5394
47
  }
5395
5396
310
  zend_ast *args_ast = zend_ast_call_get_args(callback);
5397
310
  if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT) {
5398
34
    return FAILURE;
5399
34
  }
5400
5401
  /* PFAs with non-literal pre-bound arguments are not optimizable because we
5402
   * can't memoize arguments without breaking pass-by-reference.
5403
   * TODO: Support PFAs when the function is known to not receive by ref. */
5404
276
  zend_ast_fcc *fcc = (zend_ast_fcc*)args_ast;
5405
276
  zend_ast_list *fcc_args = zend_ast_get_list(fcc->args);
5406
552
  for (uint32_t i = 0; i < fcc_args->children; i++) {
5407
276
    zend_ast *arg = fcc_args->child[i];
5408
276
    if (arg->kind == ZEND_AST_NAMED_ARG) {
5409
0
      arg = arg->child[1];
5410
0
    }
5411
5412
276
    if (arg->kind == ZEND_AST_PLACEHOLDER_ARG) {
5413
276
      continue;
5414
276
    }
5415
5416
0
    if (arg->kind != ZEND_AST_ZVAL) {
5417
0
      return FAILURE;
5418
0
    }
5419
0
  }
5420
5421
  /* Evaluate class name */
5422
276
  znode class_node;
5423
276
  if (callback->kind == ZEND_AST_STATIC_CALL
5424
36
   && callback->child[0]->kind == ZEND_AST_ZVAL
5425
35
   && Z_TYPE_P(zend_ast_get_zval(callback->child[0])) == IS_STRING
5426
35
   && zend_get_class_fetch_type(zend_ast_get_str(callback->child[0])) == ZEND_FETCH_CLASS_DEFAULT) {
5427
    /* Resolve the name against the namespace and imports, as compiling it
5428
     * as an expression would yield the unresolved string. */
5429
28
    class_node.op_type = IS_CONST;
5430
28
    ZVAL_STR(&class_node.u.constant, zend_resolve_class_name_ast(callback->child[0]));
5431
248
  } else if (callback->kind == ZEND_AST_STATIC_CALL) {
5432
8
    znode result;
5433
8
    zend_compile_expr(&result, callback->child[0]);
5434
8
    if (result.op_type == IS_CONST || result.op_type == IS_TMP_VAR) {
5435
8
      class_node = result;
5436
8
    } else {
5437
0
      class_node = result;
5438
0
      zend_emit_op_tmp(&class_node, ZEND_QM_ASSIGN, &result, NULL);
5439
0
    }
5440
240
  } else {
5441
240
    class_node.op_type = IS_UNUSED;
5442
240
  }
5443
5444
  /* Evaluate function name */
5445
276
  znode func_node;
5446
276
  {
5447
276
    znode result;
5448
276
    zend_compile_expr(&result, callback->child[callback->kind == ZEND_AST_CALL ? 0 : 1]);
5449
276
    if (result.op_type == IS_CONST || result.op_type == IS_TMP_VAR) {
5450
274
      func_node = result;
5451
274
    } else {
5452
2
      zend_emit_op_tmp(&func_node, ZEND_QM_ASSIGN, &result, NULL);
5453
2
    }
5454
276
  }
5455
5456
276
  znode value;
5457
276
  value.op_type = IS_TMP_VAR;
5458
276
  value.u.op.var = get_temporary_variable();
5459
5460
276
  zend_ast *call_args = zend_partial_apply(callback,
5461
276
      zend_ast_create_znode(&value));
5462
276
  if (!call_args) {
5463
0
    goto fail;
5464
0
  }
5465
5466
276
  zend_op *opline;
5467
5468
276
  znode array;
5469
276
  zend_compile_expr(&array, args->child[1]);
5470
  /* array is an argument to both ZEND_TYPE_ASSERT and to ZEND_FE_RESET_R. */
5471
276
  if (array.op_type == IS_CONST) {
5472
93
    if (Z_TYPE(array.u.constant) != IS_ARRAY) {
5473
49
      zval_ptr_dtor_nogc(&array.u.constant);
5474
49
      goto fail;
5475
49
    }
5476
44
    Z_TRY_ADDREF(array.u.constant);
5477
44
  }
5478
5479
  /* Verify that the input array actually is an array. */
5480
227
  znode name;
5481
227
  name.op_type = IS_CONST;
5482
227
  ZVAL_STR_COPY(&name.u.constant, lcname);
5483
227
  opline = zend_emit_op(NULL, ZEND_TYPE_ASSERT, &name, &array);
5484
227
  opline->lineno = lineno;
5485
227
  opline->extended_value = (2 << 16) | IS_ARRAY;
5486
227
  const zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5487
227
  const Bucket *fbc_bucket = ZEND_CONTAINER_OF(fbc_zv, Bucket, val);
5488
227
  Z_EXTRA_P(CT_CONSTANT(opline->op1)) = fbc_bucket - CG(function_table)->arData;
5489
5490
  /* Initialize the result array. */
5491
227
  zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
5492
5493
  /* foreach loop starts here. */
5494
227
  znode key;
5495
5496
227
  uint32_t opnum_reset = get_next_op_number();
5497
227
  znode reset_node;
5498
227
  zend_emit_op(&reset_node, ZEND_FE_RESET_R, &array, NULL);
5499
227
  zend_begin_loop(ZEND_FE_FREE, &reset_node, false);
5500
227
  uint32_t opnum_fetch = get_next_op_number();
5501
227
  zend_emit_op_tmp(&key, ZEND_FE_FETCH_R, &reset_node, &value);
5502
5503
  /* loop body */
5504
227
  znode call_result;
5505
227
  zend_ast *func_ast;
5506
227
  if (func_node.op_type == IS_CONST) {
5507
223
    func_ast = zend_ast_create_znode(&func_node);
5508
223
  } else {
5509
4
    znode copy_node;
5510
4
    zend_emit_op_tmp(&copy_node, ZEND_COPY_TMP, &func_node, NULL);
5511
4
    func_ast = zend_ast_create_znode(&copy_node);
5512
4
  }
5513
227
  switch (callback->kind) {
5514
198
    case ZEND_AST_CALL: {
5515
198
      zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_CALL, func_ast, call_args));
5516
198
      break;
5517
0
    }
5518
29
    case ZEND_AST_STATIC_CALL: {
5519
29
      zend_ast *class_ast;
5520
29
      if (class_node.op_type == IS_CONST) {
5521
28
        class_ast = zend_ast_create_znode(&class_node);
5522
28
      } else {
5523
1
        znode copy_node;
5524
1
        zend_emit_op_tmp(&copy_node, ZEND_COPY_TMP, &class_node, NULL);
5525
1
        class_ast = zend_ast_create_znode(&copy_node);
5526
1
      }
5527
5528
29
      zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_STATIC_CALL, class_ast, func_ast, call_args));
5529
29
      zend_ast_destroy(class_ast);
5530
29
      break;
5531
0
    }
5532
227
  }
5533
226
  zend_ast_destroy(func_ast);
5534
226
  opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT, &call_result, &key);
5535
226
  SET_NODE(opline->result, result);
5536
  /* end loop body */
5537
5538
226
  zend_emit_jump(opnum_fetch);
5539
5540
226
  uint32_t opnum_loop_end = get_next_op_number();
5541
226
  opline = &CG(active_op_array)->opcodes[opnum_reset];
5542
226
  opline->op2.opline_num = opnum_loop_end;
5543
226
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
5544
226
  opline->extended_value = opnum_loop_end;
5545
5546
226
  zend_end_loop(opnum_fetch, &reset_node);
5547
226
  zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
5548
226
  if (func_node.op_type != IS_CONST) {
5549
4
    zend_emit_op(NULL, ZEND_FREE, &func_node, NULL);
5550
4
  }
5551
226
  if (class_node.op_type != IS_UNUSED && class_node.op_type != IS_CONST) {
5552
1
    zend_emit_op(NULL, ZEND_FREE, &class_node, NULL);
5553
1
  }
5554
5555
226
  return SUCCESS;
5556
5557
49
fail:
5558
49
  CG(active_op_array)->T--;
5559
49
  if (func_node.op_type == IS_CONST) {
5560
49
    zval_ptr_dtor_nogc(&func_node.u.constant);
5561
49
  }
5562
49
  if (class_node.op_type == IS_CONST) {
5563
7
    zval_ptr_dtor_nogc(&class_node.u.constant);
5564
7
  }
5565
5566
49
  return FAILURE;
5567
227
}
5568
5569
static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type, uint32_t lineno) /* {{{ */
5570
33.8k
{
5571
33.8k
  if (zend_string_equals_literal(lcname, "strlen")) {
5572
696
    return zend_compile_func_strlen(result, args);
5573
33.1k
  } else if (zend_string_equals_literal(lcname, "is_null")) {
5574
263
    return zend_compile_func_typecheck(result, args, IS_NULL);
5575
32.9k
  } else if (zend_string_equals_literal(lcname, "is_bool")) {
5576
432
    return zend_compile_func_typecheck(result, args, _IS_BOOL);
5577
32.5k
  } else if (zend_string_equals_literal(lcname, "is_int")) {
5578
66
    return zend_compile_func_typecheck(result, args, IS_LONG);
5579
32.4k
  } else if (zend_string_equals_literal(lcname, "is_float")) {
5580
194
    return zend_compile_func_typecheck(result, args, IS_DOUBLE);
5581
32.2k
  } else if (zend_string_equals_literal(lcname, "is_string")) {
5582
74
    return zend_compile_func_typecheck(result, args, IS_STRING);
5583
32.1k
  } else if (zend_string_equals_literal(lcname, "is_array")) {
5584
125
    return zend_compile_func_typecheck(result, args, IS_ARRAY);
5585
32.0k
  } else if (zend_string_equals_literal(lcname, "is_object")) {
5586
66
    return zend_compile_func_typecheck(result, args, IS_OBJECT);
5587
31.9k
  } else if (zend_string_equals_literal(lcname, "is_resource")) {
5588
327
    return zend_compile_func_typecheck(result, args, IS_RESOURCE);
5589
31.6k
  } else if (zend_string_equals_literal(lcname, "is_scalar")) {
5590
515
    return zend_compile_func_is_scalar(result, args);
5591
31.1k
  } else if (zend_string_equals_literal(lcname, "boolval")) {
5592
194
    return zend_compile_func_cast(result, args, _IS_BOOL);
5593
30.9k
  } else if (zend_string_equals_literal(lcname, "intval")) {
5594
272
    return zend_compile_func_cast(result, args, IS_LONG);
5595
30.6k
  } else if (zend_string_equals_literal(lcname, "floatval")) {
5596
273
    return zend_compile_func_cast(result, args, IS_DOUBLE);
5597
30.3k
  } else if (zend_string_equals_literal(lcname, "strval")) {
5598
200
    return zend_compile_func_cast(result, args, IS_STRING);
5599
30.1k
  } else if (zend_string_equals_literal(lcname, "defined")) {
5600
2.33k
    return zend_compile_func_defined(result, args);
5601
27.8k
  } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) {
5602
704
    return zend_compile_func_chr(result, args);
5603
27.1k
  } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) {
5604
1.42k
    return zend_compile_func_ord(result, args);
5605
25.7k
  } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
5606
1.92k
    return zend_compile_func_cufa(result, args, lcname, type);
5607
23.8k
  } else if (zend_string_equals_literal(lcname, "call_user_func")) {
5608
1.51k
    return zend_compile_func_cuf(result, args, lcname, type);
5609
22.2k
  } else if (zend_string_equals_literal(lcname, "in_array")) {
5610
5.09k
    return zend_compile_func_in_array(result, args);
5611
17.1k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_COUNT))
5612
17.1k
      || zend_string_equals_literal(lcname, "sizeof")) {
5613
358
    return zend_compile_func_count(result, args, lcname);
5614
16.8k
  } else if (zend_string_equals_literal(lcname, "get_class")) {
5615
694
    return zend_compile_func_get_class(result, args);
5616
16.1k
  } else if (zend_string_equals_literal(lcname, "get_called_class")) {
5617
375
    return zend_compile_func_get_called_class(result, args);
5618
15.7k
  } else if (zend_string_equals_literal(lcname, "gettype")) {
5619
618
    return zend_compile_func_gettype(result, args);
5620
15.1k
  } else if (zend_string_equals_literal(lcname, "func_num_args")) {
5621
227
    return zend_compile_func_num_args(result, args);
5622
14.9k
  } else if (zend_string_equals_literal(lcname, "func_get_args")) {
5623
726
    return zend_compile_func_get_args(result, args);
5624
14.2k
  } else if (zend_string_equals_literal(lcname, "array_slice")) {
5625
1.84k
    return zend_compile_func_array_slice(result, args);
5626
12.3k
  } else if (zend_string_equals_literal(lcname, "array_key_exists")) {
5627
371
    return zend_compile_func_array_key_exists(result, args);
5628
11.9k
  } else if (zend_string_equals_literal(lcname, "sprintf")) {
5629
1.44k
    return zend_compile_func_sprintf(result, args);
5630
10.5k
  } else if (zend_string_equals_literal(lcname, "printf")) {
5631
1.93k
    return zend_compile_func_printf(result, args);
5632
8.60k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_CLONE))) {
5633
1.18k
    return zend_compile_func_clone(result, args);
5634
7.41k
  } else if (zend_string_equals_literal(lcname, "array_map")) {
5635
526
    return zend_compile_func_array_map(result, args, lcname, lineno);
5636
6.89k
  } else {
5637
6.89k
    return FAILURE;
5638
6.89k
  }
5639
33.8k
}
5640
5641
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) /* {{{ */
5642
35.6k
{
5643
35.6k
  if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
5644
0
    return FAILURE;
5645
0
  }
5646
5647
35.6k
  if (fbc->type != ZEND_INTERNAL_FUNCTION) {
5648
    /* If the function is part of disabled_functions, it may be redeclared as a userland
5649
     * function with a different implementation. Don't use the VM builtin in that case. */
5650
949
    return FAILURE;
5651
949
  }
5652
5653
34.6k
  if (zend_args_contain_unpack_or_named(args)) {
5654
763
    return FAILURE;
5655
763
  }
5656
5657
33.8k
  if (zend_try_compile_special_func_ex(result, lcname, args, type, lineno) == SUCCESS) {
5658
14.1k
    return SUCCESS;
5659
14.1k
  }
5660
5661
19.7k
  return zend_compile_frameless_icall(result, args, fbc, type) != (uint32_t)-1 ? SUCCESS : FAILURE;
5662
33.8k
}
5663
5664
2
static const char *zend_get_cstring_from_property_hook_kind(zend_property_hook_kind kind) {
5665
2
  switch (kind) {
5666
1
    case ZEND_PROPERTY_HOOK_GET:
5667
1
      return "get";
5668
1
    case ZEND_PROPERTY_HOOK_SET:
5669
1
      return "set";
5670
0
    default: ZEND_UNREACHABLE();
5671
2
  }
5672
2
}
5673
5674
static zend_string *zend_copy_unmangled_prop_name(zend_string *prop_name)
5675
729
{
5676
729
  if (ZSTR_VAL(prop_name)[0] != '\0') {
5677
424
    return zend_string_copy(prop_name);
5678
424
  } else {
5679
305
    const char *unmangled = zend_get_unmangled_property_name(prop_name);
5680
305
    return zend_string_init(unmangled, strlen(unmangled), /* persistent */ false);
5681
305
  }
5682
729
}
5683
5684
static bool zend_compile_parent_property_hook_call(znode *result, const zend_ast *ast, uint32_t type)
5685
17.8k
{
5686
17.8k
  ZEND_ASSERT(ast->kind == ZEND_AST_STATIC_CALL);
5687
5688
17.8k
  const zend_ast *class_ast = ast->child[0];
5689
17.8k
  zend_ast *method_ast = ast->child[1];
5690
5691
17.8k
  if (!zend_ast_is_parent_hook_call(ast)) {
5692
16.0k
    return false;
5693
16.0k
  }
5694
5695
1.85k
  zend_class_entry *ce = CG(active_class_entry);
5696
1.85k
  if (!ce) {
5697
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"parent\" when no class scope is active");
5698
7
  }
5699
5700
1.85k
  zend_ast *args_ast = ast->child[2];
5701
1.85k
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
5702
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for parent property hook call");
5703
1
  }
5704
5705
1.85k
  zval *property_hook_name_zv = zend_ast_get_zval(class_ast->child[1]);
5706
1.85k
  zend_string *property_name = zval_get_string(property_hook_name_zv);
5707
1.85k
  zend_string *hook_name = zend_ast_get_str(method_ast);
5708
1.85k
  zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(hook_name);
5709
1.85k
  ZEND_ASSERT(hook_kind != (uint32_t)-1);
5710
5711
1.85k
  const zend_string *prop_info_name = CG(context).active_property_info_name;
5712
1.84k
  if (!prop_info_name) {
5713
1
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() outside a property hook",
5714
1
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name));
5715
1
  }
5716
5717
1.84k
  const char *unmangled_prop_name = zend_get_unmangled_property_name(prop_info_name);
5718
1.84k
  if (!zend_string_equals_cstr(property_name, unmangled_prop_name, strlen(unmangled_prop_name))) {
5719
32
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property ($%s)",
5720
32
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), unmangled_prop_name);
5721
32
  }
5722
1.84k
  if (hook_kind != CG(context).active_property_hook_kind) {
5723
2
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property hook (%s)",
5724
2
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), zend_get_cstring_from_property_hook_kind(CG(context).active_property_hook_kind));
5725
2
  }
5726
5727
1.84k
  zend_op *opline = get_next_op();
5728
1.84k
  opline->opcode = ZEND_INIT_PARENT_PROPERTY_HOOK_CALL;
5729
1.84k
  opline->op1_type = IS_CONST;
5730
1.84k
  opline->op1.constant = zend_add_literal_string(&property_name);
5731
1.84k
  opline->op2.num = hook_kind;
5732
5733
1.84k
  zend_compile_call_common(result, args_ast, NULL, zend_ast_get_lineno(method_ast), BP_VAR_R);
5734
5735
1.84k
  return true;
5736
1.85k
}
5737
5738
static void zend_compile_call(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
5739
4.55M
{
5740
4.55M
  zend_ast *name_ast = ast->child[0];
5741
4.55M
  zend_ast *args_ast = ast->child[1];
5742
4.55M
  bool is_callable_convert = args_ast->kind == ZEND_AST_CALLABLE_CONVERT;
5743
5744
4.55M
  znode name_node;
5745
5746
4.55M
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
5747
105k
    zend_compile_expr(&name_node, name_ast);
5748
105k
    zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno, type);
5749
105k
    return;
5750
105k
  }
5751
5752
4.45M
  {
5753
4.45M
    bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
5754
4.45M
    if (runtime_resolution) {
5755
4.27M
      if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")
5756
58.4k
          && !is_callable_convert) {
5757
58.2k
        zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL, ast->lineno, type);
5758
4.21M
      } else {
5759
4.21M
        zend_compile_ns_call(result, &name_node, args_ast, ast->lineno, type);
5760
4.21M
      }
5761
4.27M
      return;
5762
4.27M
    }
5763
4.45M
  }
5764
5765
175k
  {
5766
175k
    const zval *name = &name_node.u.constant;
5767
175k
    zend_string *lcname = zend_string_tolower(Z_STR_P(name));
5768
175k
    zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5769
175k
    const zend_function *fbc = fbc_zv ? Z_PTR_P(fbc_zv) : NULL;
5770
175k
    zend_op *opline;
5771
5772
    /* Special assert() handling should apply independently of compiler flags. */
5773
175k
    if (fbc && zend_string_equals_literal(lcname, "assert") && !is_callable_convert) {
5774
29.8k
      zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc, ast->lineno, type);
5775
29.8k
      zend_string_release(lcname);
5776
29.8k
      zval_ptr_dtor(&name_node.u.constant);
5777
29.8k
      return;
5778
29.8k
    }
5779
5780
145k
    if (!fbc
5781
36.7k
     || !fbc_is_finalized(fbc)
5782
109k
     || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
5783
109k
      zend_string_release_ex(lcname, 0);
5784
109k
      zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno, type);
5785
109k
      return;
5786
109k
    }
5787
5788
36.7k
    if (!is_callable_convert &&
5789
35.6k
        zend_try_compile_special_func(result, lcname,
5790
35.6k
        zend_ast_get_list(args_ast), fbc, type, ast->lineno) == SUCCESS
5791
36.7k
    ) {
5792
16.5k
      zend_string_release_ex(lcname, 0);
5793
16.5k
      zval_ptr_dtor(&name_node.u.constant);
5794
16.5k
      return;
5795
16.5k
    }
5796
5797
20.1k
    zval_ptr_dtor(&name_node.u.constant);
5798
20.1k
    ZVAL_STR(&name_node.u.constant, lcname);
5799
5800
20.1k
    opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
5801
20.1k
    opline->result.num = zend_alloc_cache_slot();
5802
5803
    /* Store offset to function from symbol table in op2.extra. */
5804
20.1k
    if (fbc->type == ZEND_INTERNAL_FUNCTION) {
5805
18.8k
      const Bucket *fbc_bucket = ZEND_CONTAINER_OF(fbc_zv, Bucket, val);
5806
18.8k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = fbc_bucket - CG(function_table)->arData;
5807
18.8k
    }
5808
5809
20.1k
    zend_compile_call_common(result, args_ast, fbc, ast->lineno, type);
5810
20.1k
  }
5811
20.1k
}
5812
/* }}} */
5813
5814
static void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5815
127k
{
5816
127k
  zend_ast *obj_ast = ast->child[0];
5817
127k
  zend_ast *method_ast = ast->child[1];
5818
127k
  zend_ast *args_ast = ast->child[2];
5819
5820
127k
  znode obj_node, method_node;
5821
127k
  zend_op *opline;
5822
127k
  const zend_function *fbc = NULL;
5823
127k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL;
5824
127k
  uint32_t short_circuiting_checkpoint = zend_short_circuiting_checkpoint();
5825
5826
127k
  if (is_this_fetch(obj_ast)) {
5827
5.46k
    if (this_guaranteed_exists()) {
5828
4.33k
      obj_node.op_type = IS_UNUSED;
5829
4.33k
    } else {
5830
1.13k
      zend_emit_op_tmp(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
5831
1.13k
    }
5832
5.46k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
5833
5834
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
5835
     * check for a nullsafe access. */
5836
121k
  } else {
5837
121k
    zend_short_circuiting_mark_inner(obj_ast);
5838
121k
    zend_compile_expr(&obj_node, obj_ast);
5839
121k
    if (nullsafe) {
5840
5.10k
      zend_emit_jmp_null(&obj_node, type);
5841
5.10k
    }
5842
121k
  }
5843
5844
127k
  zend_compile_expr(&method_node, method_ast);
5845
127k
  opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
5846
5847
127k
  if (method_node.op_type == IS_CONST) {
5848
126k
    if (Z_TYPE(method_node.u.constant) != IS_STRING) {
5849
2
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5850
2
    }
5851
5852
126k
    opline->op2_type = IS_CONST;
5853
126k
    opline->op2.constant = zend_add_func_name_literal(
5854
126k
      Z_STR(method_node.u.constant));
5855
126k
    opline->result.num = zend_alloc_cache_slots(2);
5856
126k
  } else {
5857
875
    SET_NODE(opline->op2, &method_node);
5858
875
  }
5859
5860
  /* Check if this calls a known method on $this */
5861
127k
  if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST &&
5862
4.20k
      CG(active_class_entry) && zend_is_scope_known()) {
5863
3.69k
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5864
3.69k
    fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname);
5865
5866
    /* We only know the exact method that is being called if it is either private or final.
5867
     * Otherwise an overriding method in a child class may be called. */
5868
3.69k
    if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) {
5869
1.47k
      fbc = NULL;
5870
1.47k
    }
5871
3.69k
  }
5872
5873
127k
  if (zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast), type)) {
5874
337
    if (short_circuiting_checkpoint != zend_short_circuiting_checkpoint()) {
5875
15
      zend_error_noreturn(E_COMPILE_ERROR,
5876
15
        "Cannot combine nullsafe operator with Closure creation");
5877
15
    }
5878
337
  }
5879
127k
}
5880
/* }}} */
5881
5882
static bool is_func_accessible(const zend_function *fbc)
5883
6.27k
{
5884
6.27k
  if ((fbc->common.fn_flags & ZEND_ACC_PUBLIC) || fbc->common.scope == CG(active_class_entry)) {
5885
6.04k
    return true;
5886
6.04k
  }
5887
5888
234
  if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE)
5889
26
    && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED)
5890
26
    && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED))
5891
1
    && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) {
5892
0
    return true;
5893
0
  }
5894
5895
234
  return false;
5896
234
}
5897
5898
static const zend_function *zend_get_compatible_func_or_null(const zend_class_entry *ce, zend_string *lcname) /* {{{ */
5899
746
{
5900
746
  const zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname);
5901
5902
746
  if (!fbc || is_func_accessible(fbc)) {
5903
706
    return fbc;
5904
706
  }
5905
5906
40
  return NULL;
5907
746
}
5908
/* }}} */
5909
5910
static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5911
17.8k
{
5912
17.8k
  zend_ast *class_ast = ast->child[0];
5913
17.8k
  zend_ast *method_ast = ast->child[1];
5914
17.8k
  zend_ast *args_ast = ast->child[2];
5915
5916
17.8k
  znode class_node, method_node;
5917
17.8k
  zend_op *opline;
5918
17.8k
  const zend_function *fbc = NULL;
5919
5920
17.8k
  if (zend_compile_parent_property_hook_call(result, ast, type)) {
5921
1.79k
    return;
5922
1.79k
  }
5923
5924
16.0k
  zend_short_circuiting_mark_inner(class_ast);
5925
16.0k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5926
5927
16.0k
  zend_compile_expr(&method_node, method_ast);
5928
5929
16.0k
  if (method_node.op_type == IS_CONST) {
5930
12.0k
    zval *name = &method_node.u.constant;
5931
12.0k
    if (Z_TYPE_P(name) != IS_STRING) {
5932
0
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5933
0
    }
5934
12.0k
    if (zend_is_constructor(Z_STR_P(name))) {
5935
446
      zval_ptr_dtor(name);
5936
446
      method_node.op_type = IS_UNUSED;
5937
446
    }
5938
12.0k
  }
5939
5940
16.0k
  opline = get_next_op();
5941
16.0k
  opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
5942
5943
16.0k
  zend_set_class_name_op1(opline, &class_node);
5944
5945
16.0k
  if (method_node.op_type == IS_CONST) {
5946
11.5k
    opline->op2_type = IS_CONST;
5947
11.5k
    opline->op2.constant = zend_add_func_name_literal(
5948
11.5k
      Z_STR(method_node.u.constant));
5949
11.5k
    opline->result.num = zend_alloc_cache_slots(2);
5950
11.5k
  } else {
5951
4.48k
    if (opline->op1_type == IS_CONST) {
5952
527
      opline->result.num = zend_alloc_cache_slot();
5953
527
    }
5954
4.48k
    SET_NODE(opline->op2, &method_node);
5955
4.48k
  }
5956
5957
  /* Check if we already know which method we're calling */
5958
16.0k
  if (opline->op2_type == IS_CONST) {
5959
11.5k
    zend_class_entry *ce = NULL;
5960
11.5k
    if (opline->op1_type == IS_CONST) {
5961
3.59k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5962
3.59k
      ce = zend_hash_find_ptr(CG(class_table), lcname);
5963
3.59k
      if (ce) {
5964
357
        if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5965
0
          ce = NULL;
5966
0
        }
5967
3.23k
      } else if (CG(active_class_entry)
5968
400
          && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5969
119
        ce = CG(active_class_entry);
5970
119
      }
5971
7.98k
    } else if (opline->op1_type == IS_UNUSED
5972
1.46k
        && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5973
1.28k
        && zend_is_scope_known()) {
5974
270
      ce = CG(active_class_entry);
5975
270
    }
5976
11.5k
    if (ce) {
5977
746
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5978
746
      fbc = zend_get_compatible_func_or_null(ce, lcname);
5979
746
    }
5980
11.5k
  }
5981
5982
16.0k
  zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast), type);
5983
16.0k
}
5984
/* }}} */
5985
5986
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel);
5987
5988
static void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
5989
81.3k
{
5990
81.3k
  zend_ast *class_ast = ast->child[0];
5991
81.3k
  zend_ast *args_ast = ast->child[1];
5992
5993
81.3k
  znode class_node, ctor_result;
5994
81.3k
  zend_op *opline;
5995
5996
81.3k
  if (class_ast->kind == ZEND_AST_CLASS) {
5997
    /* anon class declaration */
5998
58.1k
    zend_compile_class_decl(&class_node, class_ast, false);
5999
58.1k
  } else {
6000
23.2k
    zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
6001
23.2k
  }
6002
6003
81.3k
  opline = zend_emit_op_tmp(result, ZEND_NEW, NULL, NULL);
6004
6005
81.3k
  zend_set_class_name_op1(opline, &class_node);
6006
6007
81.3k
  if (opline->op1_type == IS_CONST) {
6008
21.3k
    opline->op2.num = zend_alloc_cache_slot();
6009
21.3k
  }
6010
6011
81.3k
  zend_class_entry *ce = NULL;
6012
81.3k
  if (opline->op1_type == IS_CONST) {
6013
21.3k
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
6014
21.3k
    ce = zend_hash_find_ptr(CG(class_table), lcname);
6015
21.3k
    if (ce) {
6016
6.52k
      if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
6017
0
        ce = NULL;
6018
0
      }
6019
14.8k
    } else if (CG(active_class_entry)
6020
1.34k
        && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
6021
296
      ce = CG(active_class_entry);
6022
296
    }
6023
60.0k
  } else if (opline->op1_type == IS_UNUSED
6024
1.42k
      && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
6025
1.35k
      && zend_is_scope_known()) {
6026
206
    ce = CG(active_class_entry);
6027
206
  }
6028
6029
6030
81.3k
  const zend_function *fbc = NULL;
6031
81.3k
  if (ce
6032
7.02k
      && ce->default_object_handlers->get_constructor == zend_std_get_constructor
6033
6.95k
      && ce->constructor
6034
5.87k
      && is_func_accessible(ce->constructor)) {
6035
5.68k
    fbc = ce->constructor;
6036
5.68k
  }
6037
6038
81.3k
  zend_compile_call_common(&ctor_result, args_ast, fbc, ast->lineno, BP_VAR_R);
6039
81.3k
  zend_do_free(&ctor_result);
6040
81.3k
}
6041
/* }}} */
6042
6043
static void zend_compile_global_var(zend_ast *ast) /* {{{ */
6044
3.44k
{
6045
3.44k
  zend_ast *var_ast = ast->child[0];
6046
3.44k
  zend_ast *name_ast = var_ast->child[0];
6047
6048
3.44k
  znode name_node, result;
6049
6050
3.44k
  zend_compile_expr(&name_node, name_ast);
6051
3.44k
  if (name_node.op_type == IS_CONST) {
6052
3.17k
    convert_to_string(&name_node.u.constant);
6053
3.17k
  }
6054
6055
  // TODO(GLOBALS) Forbid "global $GLOBALS"?
6056
3.44k
  if (is_this_fetch(var_ast)) {
6057
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable");
6058
3.44k
  } else if (zend_try_compile_cv(&result, var_ast, BP_VAR_R) == SUCCESS) {
6059
2.45k
    zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
6060
2.45k
    opline->extended_value = zend_alloc_cache_slot();
6061
2.45k
  } else {
6062
    /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W
6063
     * to not free the name_node operand, so it can be reused in the following
6064
     * ASSIGN_REF, which then frees it. */
6065
987
    zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
6066
987
    opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
6067
6068
987
    if (name_node.op_type == IS_CONST) {
6069
719
      zend_string_addref(Z_STR(name_node.u.constant));
6070
719
    }
6071
6072
987
    zend_emit_assign_ref_znode(
6073
987
      zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)),
6074
987
      &result
6075
987
    );
6076
987
  }
6077
3.44k
}
6078
/* }}} */
6079
6080
static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */
6081
61.9k
{
6082
61.9k
  zend_op *opline;
6083
61.9k
  if (!CG(active_op_array)->static_variables) {
6084
0
    if (CG(active_op_array)->scope) {
6085
0
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
6086
0
    }
6087
0
    CG(active_op_array)->static_variables = zend_new_array(8);
6088
0
  }
6089
6090
61.9k
  value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value);
6091
6092
61.9k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
6093
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
6094
0
  }
6095
6096
61.9k
  opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL);
6097
61.9k
  opline->op1_type = IS_CV;
6098
61.9k
  opline->op1.var = lookup_cv(var_name);
6099
6100
61.9k
  static_assert(sizeof(Bucket) % 8 == 0, "Bucket size not compatible with storing flags in lower three bits");
6101
61.9k
  opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode;
6102
61.9k
}
6103
/* }}} */
6104
6105
static void zend_compile_static_var(zend_ast *ast) /* {{{ */
6106
4.04k
{
6107
4.04k
  zend_ast *var_ast = ast->child[0];
6108
4.04k
  zend_string *var_name = zend_ast_get_str(var_ast);
6109
6110
4.04k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
6111
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
6112
1
  }
6113
6114
4.04k
  if (!CG(active_op_array)->static_variables) {
6115
3.58k
    if (CG(active_op_array)->scope) {
6116
2.14k
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
6117
2.14k
    }
6118
3.58k
    CG(active_op_array)->static_variables = zend_new_array(8);
6119
3.58k
  }
6120
6121
4.04k
  if (zend_hash_exists(CG(active_op_array)->static_variables, var_name)) {
6122
18
    zend_error_noreturn_unchecked(E_COMPILE_ERROR, "Duplicate declaration of static variable $%S", var_name);
6123
18
  }
6124
6125
4.04k
  zend_eval_const_expr(&ast->child[1]);
6126
4.04k
  zend_ast *value_ast = ast->child[1];
6127
6128
4.04k
  if (!value_ast || value_ast->kind == ZEND_AST_ZVAL) {
6129
3.30k
    zval *value_zv = value_ast
6130
3.30k
      ? zend_ast_get_zval(value_ast)
6131
3.30k
      : &EG(uninitialized_zval);
6132
3.30k
    Z_TRY_ADDREF_P(value_zv);
6133
3.30k
    zend_compile_static_var_common(var_name, value_zv, ZEND_BIND_REF);
6134
3.30k
  } else {
6135
743
    zend_op *opline;
6136
6137
743
    zval *placeholder_ptr = zend_hash_update(CG(active_op_array)->static_variables, var_name, &EG(uninitialized_zval));
6138
743
    uint32_t placeholder_offset = (uint32_t)((char*)placeholder_ptr - (char*)CG(active_op_array)->static_variables->arData);
6139
6140
743
    uint32_t static_def_jmp_opnum = get_next_op_number();
6141
743
    opline = zend_emit_op(NULL, ZEND_BIND_INIT_STATIC_OR_JMP, NULL, NULL);
6142
743
    opline->op1_type = IS_CV;
6143
743
    opline->op1.var = lookup_cv(var_name);
6144
743
    opline->extended_value = placeholder_offset;
6145
6146
743
    znode expr;
6147
743
    zend_compile_expr(&expr, value_ast);
6148
6149
743
    opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, &expr);
6150
743
    opline->op1_type = IS_CV;
6151
743
    opline->op1.var = lookup_cv(var_name);
6152
743
    opline->extended_value = placeholder_offset | ZEND_BIND_REF;
6153
6154
743
    zend_update_jump_target_to_next(static_def_jmp_opnum);
6155
743
  }
6156
4.04k
}
6157
/* }}} */
6158
6159
static void zend_compile_unset(const zend_ast *ast) /* {{{ */
6160
3.78k
{
6161
3.78k
  zend_ast *var_ast = ast->child[0];
6162
3.78k
  znode var_node;
6163
3.78k
  zend_op *opline;
6164
6165
3.78k
  zend_ensure_writable_variable(var_ast);
6166
6167
3.78k
  if (is_global_var_fetch(var_ast)) {
6168
172
    if (!var_ast->child[1]) {
6169
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
6170
6
    }
6171
6172
172
    zend_compile_expr(&var_node, var_ast->child[1]);
6173
172
    if (var_node.op_type == IS_CONST) {
6174
147
      convert_to_string(&var_node.u.constant);
6175
147
    }
6176
6177
172
    opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL);
6178
172
    opline->extended_value = ZEND_FETCH_GLOBAL;
6179
172
    return;
6180
172
  }
6181
6182
3.61k
  switch (var_ast->kind) {
6183
1.89k
    case ZEND_AST_VAR:
6184
1.89k
      if (is_this_fetch(var_ast)) {
6185
6
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this");
6186
1.89k
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_UNSET) == SUCCESS) {
6187
1.37k
        opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL);
6188
1.37k
      } else {
6189
520
        opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, false);
6190
520
        opline->opcode = ZEND_UNSET_VAR;
6191
520
      }
6192
1.89k
      return;
6193
994
    case ZEND_AST_DIM:
6194
994
      opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET, /* by_ref */ false);
6195
994
      opline->opcode = ZEND_UNSET_DIM;
6196
994
      return;
6197
428
    case ZEND_AST_PROP:
6198
428
    case ZEND_AST_NULLSAFE_PROP:
6199
428
      opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, false);
6200
428
      opline->opcode = ZEND_UNSET_OBJ;
6201
428
      return;
6202
286
    case ZEND_AST_STATIC_PROP:
6203
286
      opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, false, false);
6204
286
      opline->opcode = ZEND_UNSET_STATIC_PROP;
6205
286
      return;
6206
0
    default: ZEND_UNREACHABLE();
6207
3.61k
  }
6208
3.61k
}
6209
/* }}} */
6210
6211
static bool zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */
6212
53.0k
{
6213
53.0k
  const zend_loop_var *base;
6214
53.0k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
6215
6216
53.0k
  if (!loop_var) {
6217
607
    return true;
6218
607
  }
6219
52.4k
  base = zend_stack_base(&CG(loop_var_stack));
6220
74.1k
  for (; loop_var >= base; loop_var--) {
6221
70.9k
    if (loop_var->opcode == ZEND_FAST_CALL) {
6222
10.7k
      zend_op *opline = get_next_op();
6223
6224
10.7k
      opline->opcode = ZEND_FAST_CALL;
6225
10.7k
      opline->result_type = IS_TMP_VAR;
6226
10.7k
      opline->result.var = loop_var->var_num;
6227
10.7k
      if (return_value) {
6228
6.04k
        SET_NODE(opline->op2, return_value);
6229
6.04k
      }
6230
10.7k
      opline->op1.num = loop_var->try_catch_offset;
6231
60.2k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
6232
2.95k
      zend_op *opline = get_next_op();
6233
2.95k
      opline->opcode = ZEND_DISCARD_EXCEPTION;
6234
2.95k
      opline->op1_type = IS_TMP_VAR;
6235
2.95k
      opline->op1.var = loop_var->var_num;
6236
57.2k
    } else if (loop_var->opcode == ZEND_RETURN) {
6237
      /* Stack separator */
6238
45.0k
      break;
6239
45.0k
    } else if (depth <= 1) {
6240
4.21k
      return 1;
6241
8.04k
    } else if (loop_var->opcode == ZEND_NOP) {
6242
      /* Loop doesn't have freeable variable */
6243
4.25k
      depth--;
6244
4.25k
    } else {
6245
3.78k
      zend_op *opline;
6246
6247
3.78k
      ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR));
6248
3.78k
      opline = get_next_op();
6249
3.78k
      opline->opcode = loop_var->opcode;
6250
3.78k
      opline->op1_type = loop_var->var_type;
6251
3.78k
      opline->op1.var = loop_var->var_num;
6252
3.78k
      opline->extended_value = ZEND_FREE_ON_RETURN;
6253
3.78k
      depth--;
6254
3.78k
      }
6255
70.9k
  }
6256
48.2k
  return (depth == 0);
6257
52.4k
}
6258
/* }}} */
6259
6260
static bool zend_handle_loops_and_finally(znode *return_value) /* {{{ */
6261
48.7k
{
6262
48.7k
  return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value);
6263
48.7k
}
6264
/* }}} */
6265
6266
static bool zend_has_finally_ex(zend_long depth) /* {{{ */
6267
2.39k
{
6268
2.39k
  const zend_loop_var *base;
6269
2.39k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
6270
6271
2.39k
  if (!loop_var) {
6272
258
    return false;
6273
258
  }
6274
2.13k
  base = zend_stack_base(&CG(loop_var_stack));
6275
3.19k
  for (; loop_var >= base; loop_var--) {
6276
3.02k
    if (loop_var->opcode == ZEND_FAST_CALL) {
6277
1.45k
      return 1;
6278
1.57k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
6279
831
    } else if (loop_var->opcode == ZEND_RETURN) {
6280
      /* Stack separator */
6281
509
      return 0;
6282
509
    } else if (depth <= 1) {
6283
0
      return 0;
6284
230
    } else {
6285
230
      depth--;
6286
230
      }
6287
3.02k
  }
6288
170
  return false;
6289
2.13k
}
6290
/* }}} */
6291
6292
static bool zend_has_finally(void) /* {{{ */
6293
2.39k
{
6294
2.39k
  return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1);
6295
2.39k
}
6296
/* }}} */
6297
6298
static void zend_compile_return(const zend_ast *ast) /* {{{ */
6299
44.5k
{
6300
44.5k
  zend_ast *expr_ast = ast->child[0];
6301
44.5k
  bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0;
6302
44.5k
  bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
6303
6304
44.5k
  znode expr_node;
6305
44.5k
  zend_op *opline;
6306
6307
44.5k
  if (is_generator) {
6308
    /* For generators the by-ref flag refers to yields, not returns */
6309
7.89k
    by_ref = false;
6310
7.89k
  }
6311
6312
44.5k
  if (!expr_ast) {
6313
2.25k
    expr_node.op_type = IS_CONST;
6314
2.25k
    ZVAL_NULL(&expr_node.u.constant);
6315
42.3k
  } else if (by_ref && zend_is_variable_or_call(expr_ast)) {
6316
2.66k
    zend_assert_not_short_circuited(expr_ast);
6317
2.66k
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
6318
39.6k
  } else {
6319
39.6k
    zend_compile_expr(&expr_node, expr_ast);
6320
39.6k
  }
6321
6322
44.5k
  if (expr_ast) {
6323
42.1k
    if (CG(active_class_entry) != NULL) {
6324
5.52k
      if (zend_is_constructor(CG(active_op_array)->function_name)) {
6325
12
        zend_error(E_DEPRECATED, "Returning a value from a constructor is deprecated");
6326
5.51k
      } else if (zend_string_equals_literal_ci(CG(active_op_array)->function_name, ZEND_DESTRUCTOR_FUNC_NAME)) {
6327
16
        zend_error(E_DEPRECATED, "Returning a value from a destructor is deprecated");
6328
16
      }
6329
5.52k
    }
6330
42.1k
  }
6331
6332
44.5k
  if (CG(context).in_finally) {
6333
2.82k
    zend_error(E_DEPRECATED, "Returning from a finally block is deprecated");
6334
2.82k
  }
6335
6336
44.5k
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
6337
4.18k
   && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
6338
2.39k
   && zend_has_finally()) {
6339
    /* Copy return value into temporary VAR to avoid modification in finally code */
6340
1.45k
    if (by_ref) {
6341
795
      zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
6342
795
    } else {
6343
663
      zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL);
6344
663
    }
6345
1.45k
  }
6346
6347
  /* Generator return types are handled separately */
6348
44.5k
  if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
6349
5.91k
    zend_emit_return_type_check(
6350
5.91k
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
6351
5.91k
  }
6352
6353
44.5k
  uint32_t opnum_before_finally = get_next_op_number();
6354
6355
44.5k
  zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
6356
6357
  /* Content of reference might have changed in finally, repeat type check. */
6358
44.5k
  if (by_ref
6359
   /* Check if any opcodes were emitted since the last return type check. */
6360
10.2k
   && opnum_before_finally != get_next_op_number()
6361
2.01k
   && !is_generator
6362
2.01k
   && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
6363
446
    zend_emit_return_type_check(
6364
446
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
6365
446
  }
6366
6367
44.5k
  opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
6368
44.5k
    &expr_node, NULL);
6369
6370
44.5k
  if (by_ref && expr_ast) {
6371
9.17k
    if (zend_is_call(expr_ast)) {
6372
607
      opline->extended_value = ZEND_RETURNS_FUNCTION;
6373
8.56k
    } else if (!zend_is_variable(expr_ast) || zend_ast_is_short_circuited(expr_ast)) {
6374
6.51k
      opline->extended_value = ZEND_RETURNS_VALUE;
6375
6.51k
    }
6376
9.17k
  }
6377
44.5k
}
6378
/* }}} */
6379
6380
static void zend_compile_void_cast(znode *result, const zend_ast *ast)
6381
3.97k
{
6382
3.97k
  zend_ast *expr_ast = ast->child[0];
6383
3.97k
  znode expr_node;
6384
3.97k
  zend_op *opline;
6385
6386
3.97k
  zend_compile_expr(&expr_node, expr_ast);
6387
6388
3.97k
  switch (expr_node.op_type) {
6389
2.44k
    case IS_TMP_VAR:
6390
2.44k
    case IS_VAR:
6391
2.44k
      opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6392
2.44k
      opline->extended_value = ZEND_FREE_VOID_CAST;
6393
2.44k
      break;
6394
1.25k
    case IS_CONST:
6395
1.25k
      zend_do_free(&expr_node);
6396
1.25k
      break;
6397
3.97k
  }
6398
3.97k
}
6399
6400
static void zend_compile_echo(const zend_ast *ast) /* {{{ */
6401
2.81M
{
6402
2.81M
  zend_op *opline;
6403
2.81M
  zend_ast *expr_ast = ast->child[0];
6404
6405
2.81M
  znode expr_node;
6406
2.81M
  zend_compile_expr(&expr_node, expr_ast);
6407
6408
2.81M
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
6409
2.81M
  opline->extended_value = 0;
6410
2.81M
}
6411
/* }}} */
6412
6413
static void zend_compile_throw(znode *result, const zend_ast *ast) /* {{{ */
6414
982
{
6415
982
  zend_ast *expr_ast = ast->child[0];
6416
6417
982
  znode expr_node;
6418
982
  zend_compile_expr(&expr_node, expr_ast);
6419
6420
982
  zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
6421
982
  if (result) {
6422
    /* Mark this as an "expression throw" for opcache. */
6423
501
    opline->extended_value = ZEND_THROW_IS_EXPR;
6424
501
    result->op_type = IS_CONST;
6425
501
    ZVAL_TRUE(&result->u.constant);
6426
501
  }
6427
982
}
6428
/* }}} */
6429
6430
static void zend_compile_break_continue(const zend_ast *ast) /* {{{ */
6431
4.42k
{
6432
4.42k
  zend_ast *depth_ast = ast->child[0];
6433
6434
4.42k
  zend_op *opline;
6435
4.42k
  zend_long depth;
6436
6437
4.42k
  ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
6438
6439
4.42k
  if (depth_ast) {
6440
2.03k
    const zval *depth_zv;
6441
2.03k
    if (depth_ast->kind != ZEND_AST_ZVAL) {
6442
26
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand "
6443
26
        "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
6444
26
    }
6445
6446
2.03k
    depth_zv = zend_ast_get_zval(depth_ast);
6447
2.03k
    if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
6448
11
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers",
6449
11
        ast->kind == ZEND_AST_BREAK ? "break" : "continue");
6450
11
    }
6451
6452
2.03k
    depth = Z_LVAL_P(depth_zv);
6453
2.38k
  } else {
6454
2.38k
    depth = 1;
6455
2.38k
  }
6456
6457
4.42k
  if (CG(context).current_brk_cont == -1) {
6458
42
    zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
6459
42
      ast->kind == ZEND_AST_BREAK ? "break" : "continue");
6460
4.38k
  } else {
6461
4.38k
    if (!zend_handle_loops_and_finally_ex(depth, NULL)) {
6462
137
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s",
6463
137
        ast->kind == ZEND_AST_BREAK ? "break" : "continue",
6464
137
        depth, depth == 1 ? "" : "s");
6465
137
    }
6466
4.38k
  }
6467
6468
4.42k
  if (ast->kind == ZEND_AST_CONTINUE) {
6469
3.13k
    int d, cur = CG(context).current_brk_cont;
6470
3.91k
    for (d = depth - 1; d > 0; d--) {
6471
785
      cur = CG(context).brk_cont_array[cur].parent;
6472
785
      ZEND_ASSERT(cur != -1);
6473
785
    }
6474
6475
3.13k
    if (CG(context).brk_cont_array[cur].is_switch) {
6476
1.13k
      if (depth == 1) {
6477
359
        if (CG(context).brk_cont_array[cur].parent == -1) {
6478
202
          zend_error(E_WARNING,
6479
202
            "\"continue\" targeting switch is equivalent to \"break\"");
6480
202
        } else {
6481
157
          zend_error(E_WARNING,
6482
157
            "\"continue\" targeting switch is equivalent to \"break\". " \
6483
157
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
6484
157
            depth + 1);
6485
157
        }
6486
772
      } else {
6487
772
        if (CG(context).brk_cont_array[cur].parent == -1) {
6488
666
          zend_error(E_WARNING,
6489
666
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"",
6490
666
            depth, depth);
6491
666
        } else {
6492
106
          zend_error(E_WARNING,
6493
106
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
6494
106
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
6495
106
            depth, depth, depth + 1);
6496
106
        }
6497
772
      }
6498
1.13k
    }
6499
3.13k
  }
6500
6501
4.42k
  opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
6502
4.42k
  opline->op1.num = CG(context).current_brk_cont;
6503
4.42k
  opline->op2.num = depth;
6504
4.42k
}
6505
/* }}} */
6506
6507
void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */
6508
4.10k
{
6509
4.10k
  zend_label *dest;
6510
4.10k
  int remove_oplines = opline->op1.num;
6511
4.10k
  zval *label;
6512
4.10k
  uint32_t opnum = opline - op_array->opcodes;
6513
6514
4.10k
  label = CT_CONSTANT_EX(op_array, opline->op2.constant);
6515
4.10k
  if (CG(context).labels == NULL ||
6516
4.08k
      (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL
6517
4.10k
  ) {
6518
75
    CG(in_compilation) = 1;
6519
75
    CG(active_op_array) = op_array;
6520
75
    CG(zend_lineno) = opline->lineno;
6521
75
    zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
6522
75
  }
6523
6524
4.10k
  zval_ptr_dtor_str(label);
6525
4.10k
  ZVAL_NULL(label);
6526
6527
4.10k
  uint32_t current = opline->extended_value;
6528
8.29k
  for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) {
6529
4.18k
    if (current == -1) {
6530
1
      CG(in_compilation) = 1;
6531
1
      CG(active_op_array) = op_array;
6532
1
      CG(zend_lineno) = opline->lineno;
6533
1
      zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
6534
1
    }
6535
4.18k
    if (CG(context).brk_cont_array[current].start >= 0) {
6536
2.56k
      remove_oplines--;
6537
2.56k
    }
6538
4.18k
  }
6539
6540
8.85k
  for (current = 0; current < op_array->last_try_catch; ++current) {
6541
6.75k
    const zend_try_catch_element *elem = &op_array->try_catch_array[current];
6542
6.75k
    if (elem->try_op > opnum) {
6543
2.00k
      break;
6544
2.00k
    }
6545
4.75k
    if (elem->finally_op && opnum < elem->finally_op - 1
6546
3.69k
      && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op)
6547
4.75k
    ) {
6548
2.46k
      remove_oplines--;
6549
2.46k
    }
6550
4.75k
  }
6551
6552
4.10k
  opline->opcode = ZEND_JMP;
6553
4.10k
  SET_UNUSED(opline->op1);
6554
4.10k
  SET_UNUSED(opline->op2);
6555
4.10k
  SET_UNUSED(opline->result);
6556
4.10k
  opline->op1.opline_num = dest->opline_num;
6557
4.10k
  opline->extended_value = 0;
6558
6559
4.10k
  ZEND_ASSERT(remove_oplines >= 0);
6560
5.52k
  while (remove_oplines--) {
6561
1.49k
    opline--;
6562
1.49k
    MAKE_NOP(opline);
6563
1.49k
    ZEND_VM_SET_OPCODE_HANDLER(opline);
6564
1.49k
  }
6565
4.03k
}
6566
/* }}} */
6567
6568
static void zend_compile_goto(const zend_ast *ast) /* {{{ */
6569
4.31k
{
6570
4.31k
  zend_ast *label_ast = ast->child[0];
6571
4.31k
  znode label_node;
6572
4.31k
  zend_op *opline;
6573
6574
4.31k
  zend_compile_expr(&label_node, label_ast);
6575
6576
  /* Label resolution and unwinding adjustments happen in pass two. */
6577
4.31k
  uint32_t opnum_start = get_next_op_number();
6578
4.31k
  zend_handle_loops_and_finally(NULL);
6579
4.31k
  opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
6580
4.31k
  opline->op1.num = get_next_op_number() - opnum_start - 1;
6581
4.31k
  opline->extended_value = CG(context).current_brk_cont;
6582
4.31k
}
6583
/* }}} */
6584
6585
static void zend_compile_label(const zend_ast *ast) /* {{{ */
6586
28.9k
{
6587
28.9k
  zend_string *label = zend_ast_get_str(ast->child[0]);
6588
28.9k
  zend_label dest;
6589
6590
28.9k
  if (!CG(context).labels) {
6591
26.1k
    ALLOC_HASHTABLE(CG(context).labels);
6592
26.1k
    zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0);
6593
26.1k
  }
6594
6595
28.9k
  dest.brk_cont = CG(context).current_brk_cont;
6596
28.9k
  dest.opline_num = get_next_op_number();
6597
6598
28.9k
  if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
6599
61
    zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
6600
61
  }
6601
28.9k
}
6602
/* }}} */
6603
6604
static void zend_compile_while(const zend_ast *ast) /* {{{ */
6605
2.35k
{
6606
2.35k
  zend_ast *cond_ast = ast->child[0];
6607
2.35k
  zend_ast *stmt_ast = ast->child[1];
6608
2.35k
  znode cond_node;
6609
2.35k
  uint32_t opnum_start, opnum_jmp, opnum_cond;
6610
6611
2.35k
  opnum_jmp = zend_emit_jump(0);
6612
6613
2.35k
  zend_begin_loop(ZEND_NOP, NULL, false);
6614
6615
2.35k
  opnum_start = get_next_op_number();
6616
2.35k
  zend_compile_stmt(stmt_ast);
6617
6618
2.35k
  opnum_cond = get_next_op_number();
6619
2.35k
  zend_update_jump_target(opnum_jmp, opnum_cond);
6620
2.35k
  zend_compile_expr(&cond_node, cond_ast);
6621
6622
2.35k
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6623
6624
2.35k
  zend_end_loop(opnum_cond, NULL);
6625
2.35k
}
6626
/* }}} */
6627
6628
static void zend_compile_do_while(const zend_ast *ast) /* {{{ */
6629
1.98k
{
6630
1.98k
  zend_ast *stmt_ast = ast->child[0];
6631
1.98k
  zend_ast *cond_ast = ast->child[1];
6632
6633
1.98k
  znode cond_node;
6634
1.98k
  uint32_t opnum_start, opnum_cond;
6635
6636
1.98k
  zend_begin_loop(ZEND_NOP, NULL, false);
6637
6638
1.98k
  opnum_start = get_next_op_number();
6639
1.98k
  zend_compile_stmt(stmt_ast);
6640
6641
1.98k
  opnum_cond = get_next_op_number();
6642
1.98k
  zend_compile_expr(&cond_node, cond_ast);
6643
6644
1.98k
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6645
6646
1.98k
  zend_end_loop(opnum_cond, NULL);
6647
1.98k
}
6648
/* }}} */
6649
6650
/* An assignment whose result is unused is compiled as a statement */
6651
static void zend_compile_for_expr_list(znode *result, zend_ast *ast, bool last_result_used) /* {{{ */
6652
69.4k
{
6653
69.4k
  const zend_ast_list *list;
6654
69.4k
  uint32_t i;
6655
6656
69.4k
  result->op_type = IS_CONST;
6657
69.4k
  ZVAL_TRUE(&result->u.constant);
6658
6659
69.4k
  if (!ast) {
6660
44.4k
    return;
6661
44.4k
  }
6662
6663
24.9k
  list = zend_ast_get_list(ast);
6664
55.2k
  for (i = 0; i < list->children; ++i) {
6665
30.2k
    zend_ast *expr_ast = list->child[i];
6666
6667
30.2k
    zend_do_free(result);
6668
30.2k
    if (expr_ast->kind == ZEND_AST_CAST_VOID) {
6669
2.97k
      zend_compile_void_cast(NULL, expr_ast);
6670
2.97k
      result->op_type = IS_CONST;
6671
2.97k
      ZVAL_NULL(&result->u.constant);
6672
27.2k
    } else if (expr_ast->kind == ZEND_AST_ASSIGN && (i + 1 < list->children || !last_result_used)) {
6673
546
      zend_compile_assign(result, expr_ast, /* stmt */ true, BP_VAR_R);
6674
26.7k
    } else {
6675
26.7k
      zend_compile_expr(result, expr_ast);
6676
26.7k
    }
6677
30.2k
  }
6678
24.9k
}
6679
/* }}} */
6680
6681
static void zend_compile_for(const zend_ast *ast) /* {{{ */
6682
23.2k
{
6683
23.2k
  zend_ast *init_ast = ast->child[0];
6684
23.2k
  zend_ast *cond_ast = ast->child[1];
6685
23.2k
  zend_ast *loop_ast = ast->child[2];
6686
23.2k
  zend_ast *stmt_ast = ast->child[3];
6687
6688
23.2k
  znode result;
6689
23.2k
  uint32_t opnum_start, opnum_jmp, opnum_loop;
6690
6691
23.2k
  zend_compile_for_expr_list(&result, init_ast, false);
6692
23.2k
  zend_do_free(&result);
6693
6694
23.2k
  opnum_jmp = zend_emit_jump(0);
6695
6696
23.2k
  zend_begin_loop(ZEND_NOP, NULL, false);
6697
6698
23.2k
  opnum_start = get_next_op_number();
6699
23.2k
  zend_compile_stmt(stmt_ast);
6700
6701
23.2k
  opnum_loop = get_next_op_number();
6702
23.2k
  zend_compile_for_expr_list(&result, loop_ast, false);
6703
23.2k
  zend_do_free(&result);
6704
6705
23.2k
  zend_update_jump_target_to_next(opnum_jmp);
6706
23.2k
  zend_compile_for_expr_list(&result, cond_ast, true);
6707
23.2k
  zend_do_extended_stmt(NULL);
6708
6709
23.2k
  zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
6710
6711
23.2k
  zend_end_loop(opnum_loop, NULL);
6712
23.2k
}
6713
/* }}} */
6714
6715
static void zend_compile_foreach(zend_ast *ast) /* {{{ */
6716
6.50k
{
6717
6.50k
  zend_ast *expr_ast = ast->child[0];
6718
6.50k
  zend_ast *value_ast = ast->child[1];
6719
6.50k
  zend_ast *key_ast = ast->child[2];
6720
6.50k
  zend_ast *stmt_ast = ast->child[3];
6721
6.50k
  bool by_ref = value_ast->kind == ZEND_AST_REF;
6722
6.50k
  bool is_variable = (zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast))
6723
3.83k
    || zend_is_call(expr_ast);
6724
6725
6.50k
  znode expr_node, reset_node, value_node, key_node;
6726
6.50k
  zend_op *opline;
6727
6.50k
  uint32_t opnum_reset, opnum_fetch;
6728
6729
6.50k
  if (key_ast) {
6730
702
    if (key_ast->kind == ZEND_AST_REF) {
6731
2
      zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
6732
2
    }
6733
702
    if (key_ast->kind == ZEND_AST_ARRAY) {
6734
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
6735
5
    }
6736
702
  }
6737
6738
6.50k
  if (by_ref) {
6739
947
    value_ast = value_ast->child[0];
6740
947
  }
6741
6742
6.50k
  if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
6743
65
    by_ref = true;
6744
65
  }
6745
6746
6.50k
  if (by_ref && is_variable) {
6747
308
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
6748
6.19k
  } else {
6749
6.19k
    zend_compile_expr(&expr_node, expr_ast);
6750
6.19k
  }
6751
6752
6.50k
  if (by_ref) {
6753
1.01k
    zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
6754
1.01k
  }
6755
6756
6.50k
  opnum_reset = get_next_op_number();
6757
6.50k
  opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
6758
6.50k
  if (!by_ref) {
6759
5.48k
    opline->result_type = IS_TMP_VAR;
6760
5.48k
    reset_node.op_type = IS_TMP_VAR;
6761
5.48k
  }
6762
6763
6.50k
  zend_begin_loop(ZEND_FE_FREE, &reset_node, false);
6764
6765
6.50k
  opnum_fetch = get_next_op_number();
6766
6.50k
  opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
6767
6768
6.50k
  if (is_this_fetch(value_ast)) {
6769
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6770
6.50k
  } else if (value_ast->kind == ZEND_AST_VAR &&
6771
5.88k
    zend_try_compile_cv(&value_node, value_ast, BP_VAR_R) == SUCCESS) {
6772
5.54k
    SET_NODE(opline->op2, &value_node);
6773
5.54k
  } else {
6774
963
    opline->op2_type = by_ref ? IS_VAR : IS_TMP_VAR;
6775
963
    opline->op2.var = get_temporary_variable();
6776
963
    GET_NODE(&value_node, opline->op2);
6777
963
    if (value_ast->kind == ZEND_AST_ARRAY) {
6778
85
      zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr, BP_VAR_R);
6779
878
    } else if (by_ref) {
6780
484
      zend_emit_assign_ref_znode(value_ast, &value_node);
6781
484
    } else {
6782
394
      zend_emit_assign_znode(value_ast, &value_node);
6783
394
    }
6784
963
  }
6785
6786
6.50k
  if (key_ast) {
6787
684
    opline = &CG(active_op_array)->opcodes[opnum_fetch];
6788
684
    zend_make_tmp_result(&key_node, opline);
6789
684
    zend_emit_assign_znode(key_ast, &key_node);
6790
684
  }
6791
6792
6.50k
  zend_compile_stmt(stmt_ast);
6793
6794
  /* Place JMP and FE_FREE on the line where foreach starts. It would be
6795
   * better to use the end line, but this information is not available
6796
   * currently. */
6797
6.50k
  CG(zend_lineno) = ast->lineno;
6798
6.50k
  zend_emit_jump(opnum_fetch);
6799
6800
6.50k
  opline = &CG(active_op_array)->opcodes[opnum_reset];
6801
6.50k
  opline->op2.opline_num = get_next_op_number();
6802
6803
6.50k
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
6804
6.50k
  opline->extended_value = get_next_op_number();
6805
6806
6.50k
  zend_end_loop(opnum_fetch, &reset_node);
6807
6808
6.50k
  opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
6809
6.50k
}
6810
/* }}} */
6811
6812
static void zend_compile_if(zend_ast *ast) /* {{{ */
6813
167k
{
6814
167k
  const zend_ast_list *list = zend_ast_get_list(ast);
6815
167k
  uint32_t i;
6816
167k
  uint32_t *jmp_opnums = NULL;
6817
6818
167k
  if (list->children > 1) {
6819
56.3k
    jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
6820
56.3k
  }
6821
6822
443k
  for (i = 0; i < list->children; ++i) {
6823
276k
    const zend_ast *elem_ast = list->child[i];
6824
276k
    zend_ast *cond_ast = elem_ast->child[0];
6825
276k
    zend_ast *stmt_ast = elem_ast->child[1];
6826
6827
276k
    if (cond_ast) {
6828
221k
      znode cond_node;
6829
221k
      uint32_t opnum_jmpz;
6830
6831
221k
      if (i > 0) {
6832
54.2k
        CG(zend_lineno) = cond_ast->lineno;
6833
54.2k
        zend_do_extended_stmt(NULL);
6834
54.2k
      }
6835
6836
221k
      zend_compile_expr(&cond_node, cond_ast);
6837
221k
      opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6838
6839
221k
      zend_compile_stmt(stmt_ast);
6840
6841
221k
      if (i != list->children - 1) {
6842
        /* Set the lineno of JMP to the position of the if keyword, as we don't want to
6843
         * report the last line in the if branch as covered if it hasn't actually executed. */
6844
109k
        CG(zend_lineno) = elem_ast->lineno;
6845
109k
        jmp_opnums[i] = zend_emit_jump(0);
6846
109k
      }
6847
221k
      zend_update_jump_target_to_next(opnum_jmpz);
6848
221k
    } else {
6849
      /* "else" can only occur as last element. */
6850
55.2k
      ZEND_ASSERT(i == list->children - 1);
6851
55.2k
      zend_compile_stmt(stmt_ast);
6852
55.2k
    }
6853
276k
  }
6854
6855
167k
  if (list->children > 1) {
6856
165k
    for (i = 0; i < list->children - 1; ++i) {
6857
109k
      zend_update_jump_target_to_next(jmp_opnums[i]);
6858
109k
    }
6859
56.3k
    efree(jmp_opnums);
6860
56.3k
  }
6861
167k
}
6862
/* }}} */
6863
6864
8.63k
static uint8_t determine_switch_jumptable_type(const zend_ast_list *cases) {
6865
8.63k
  uint32_t i;
6866
8.63k
  uint8_t common_type = IS_UNDEF;
6867
21.8k
  for (i = 0; i < cases->children; i++) {
6868
18.7k
    zend_ast *case_ast = cases->child[i];
6869
18.7k
    zend_ast **cond_ast = &case_ast->child[0];
6870
18.7k
    const zval *cond_zv;
6871
18.7k
    if (!case_ast->child[0]) {
6872
      /* Skip default clause */
6873
540
      continue;
6874
540
    }
6875
6876
18.1k
    zend_eval_const_expr(cond_ast);
6877
18.1k
    if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6878
      /* Non-constant case */
6879
1.83k
      return IS_UNDEF;
6880
1.83k
    }
6881
6882
16.3k
    cond_zv = zend_ast_get_zval(case_ast->child[0]);
6883
16.3k
    if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6884
      /* We only optimize switched on integers and strings */
6885
3.00k
      return IS_UNDEF;
6886
3.00k
    }
6887
6888
13.3k
    if (common_type == IS_UNDEF) {
6889
6.57k
      common_type = Z_TYPE_P(cond_zv);
6890
6.76k
    } else if (common_type != Z_TYPE_P(cond_zv)) {
6891
      /* Non-uniform case types */
6892
290
      return IS_UNDEF;
6893
290
    }
6894
6895
13.0k
    if (Z_TYPE_P(cond_zv) == IS_STRING
6896
11.9k
        && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) {
6897
      /* Numeric strings cannot be compared with a simple hash lookup */
6898
339
      return IS_UNDEF;
6899
339
    }
6900
13.0k
  }
6901
6902
3.16k
  return common_type;
6903
8.63k
}
6904
6905
2.57k
static bool should_use_jumptable(const zend_ast_list *cases, uint8_t jumptable_type) {
6906
2.57k
  if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) {
6907
0
    return false;
6908
0
  }
6909
6910
  /* Thresholds are chosen based on when the average switch time for equidistributed
6911
   * input becomes smaller when using the jumptable optimization. */
6912
2.57k
  if (jumptable_type == IS_LONG) {
6913
332
    return cases->children >= 5;
6914
2.24k
  } else {
6915
2.24k
    ZEND_ASSERT(jumptable_type == IS_STRING);
6916
2.24k
    return cases->children >= 2;
6917
2.24k
  }
6918
2.57k
}
6919
6920
static void zend_compile_switch(zend_ast *ast) /* {{{ */
6921
8.63k
{
6922
8.63k
  zend_ast *expr_ast = ast->child[0];
6923
8.63k
  zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
6924
6925
8.63k
  uint32_t i;
6926
8.63k
  bool has_default_case = false;
6927
6928
8.63k
  znode expr_node, case_node;
6929
8.63k
  zend_op *opline;
6930
8.63k
  uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1;
6931
8.63k
  uint8_t jumptable_type;
6932
8.63k
  HashTable *jumptable = NULL;
6933
6934
8.63k
  zend_compile_expr(&expr_node, expr_ast);
6935
6936
8.63k
  zend_begin_loop(ZEND_FREE, &expr_node, true);
6937
6938
8.63k
  case_node.op_type = IS_TMP_VAR;
6939
8.63k
  case_node.u.op.var = get_temporary_variable();
6940
6941
8.63k
  jumptable_type = determine_switch_jumptable_type(cases);
6942
8.63k
  if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) {
6943
2.02k
    znode jumptable_op;
6944
6945
2.02k
    ALLOC_HASHTABLE(jumptable);
6946
2.02k
    zend_hash_init(jumptable, cases->children, NULL, NULL, 0);
6947
2.02k
    jumptable_op.op_type = IS_CONST;
6948
2.02k
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6949
6950
2.02k
    opline = zend_emit_op(NULL,
6951
2.02k
      jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING,
6952
2.02k
      &expr_node, &jumptable_op);
6953
2.02k
    if (opline->op1_type == IS_CONST) {
6954
1.49k
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6955
1.49k
    }
6956
2.02k
    opnum_switch = opline - CG(active_op_array)->opcodes;
6957
2.02k
  }
6958
6959
8.63k
  jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
6960
30.7k
  for (i = 0; i < cases->children; ++i) {
6961
22.0k
    zend_ast *case_ast = cases->child[i];
6962
22.0k
    zend_ast *cond_ast = case_ast->child[0];
6963
22.0k
    znode cond_node;
6964
6965
22.0k
    if (case_ast->attr == ZEND_ALT_CASE_SYNTAX) {
6966
1.01k
      CG(zend_lineno) = case_ast->lineno;
6967
1.01k
      zend_error(E_DEPRECATED, "Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead");
6968
1.01k
    }
6969
6970
22.0k
    if (!cond_ast) {
6971
486
      if (has_default_case) {
6972
12
        CG(zend_lineno) = case_ast->lineno;
6973
12
        zend_error_noreturn(E_COMPILE_ERROR,
6974
12
          "Switch statements may only contain one default clause");
6975
12
      }
6976
486
      has_default_case = true;
6977
486
      continue;
6978
486
    }
6979
6980
21.6k
    zend_compile_expr(&cond_node, cond_ast);
6981
6982
21.6k
    if (expr_node.op_type == IS_CONST
6983
13.7k
      && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
6984
2.58k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6985
19.0k
    } else if (expr_node.op_type == IS_CONST
6986
11.1k
      && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
6987
2.11k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
6988
16.9k
    } else {
6989
16.9k
      opline = zend_emit_op(NULL,
6990
16.9k
        (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL,
6991
16.9k
        &expr_node, &cond_node);
6992
16.9k
      SET_NODE(opline->result, &case_node);
6993
16.9k
      if (opline->op1_type == IS_CONST) {
6994
9.01k
        Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6995
9.01k
      }
6996
6997
16.9k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6998
16.9k
    }
6999
21.6k
  }
7000
7001
8.63k
  opnum_default_jmp = zend_emit_jump(0);
7002
7003
30.4k
  for (i = 0; i < cases->children; ++i) {
7004
21.8k
    zend_ast *case_ast = cases->child[i];
7005
21.8k
    zend_ast *cond_ast = case_ast->child[0];
7006
21.8k
    zend_ast *stmt_ast = case_ast->child[1];
7007
7008
21.8k
    if (cond_ast) {
7009
21.4k
      zend_update_jump_target_to_next(jmpnz_opnums[i]);
7010
7011
21.4k
      if (jumptable) {
7012
5.68k
        zval *cond_zv = zend_ast_get_zval(cond_ast);
7013
5.68k
        zval jmp_target;
7014
5.68k
        ZVAL_LONG(&jmp_target, get_next_op_number());
7015
7016
5.68k
        ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type);
7017
5.68k
        if (Z_TYPE_P(cond_zv) == IS_LONG) {
7018
418
          zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
7019
5.26k
        } else {
7020
5.26k
          ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
7021
5.26k
          zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
7022
5.26k
        }
7023
5.68k
      }
7024
21.4k
    } else {
7025
462
      zend_update_jump_target_to_next(opnum_default_jmp);
7026
7027
462
      if (jumptable) {
7028
31
        ZEND_ASSERT(opnum_switch != (uint32_t)-1);
7029
31
        opline = &CG(active_op_array)->opcodes[opnum_switch];
7030
31
        opline->extended_value = get_next_op_number();
7031
31
      }
7032
462
    }
7033
7034
21.8k
    zend_compile_stmt(stmt_ast);
7035
21.8k
  }
7036
7037
8.63k
  if (!has_default_case) {
7038
8.13k
    zend_update_jump_target_to_next(opnum_default_jmp);
7039
7040
8.13k
    if (jumptable) {
7041
1.98k
      opline = &CG(active_op_array)->opcodes[opnum_switch];
7042
1.98k
      opline->extended_value = get_next_op_number();
7043
1.98k
    }
7044
8.13k
  }
7045
7046
8.63k
  zend_end_loop(get_next_op_number(), &expr_node);
7047
7048
8.63k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
7049
2.80k
    opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
7050
2.80k
    opline->extended_value = ZEND_FREE_SWITCH;
7051
5.82k
  } else if (expr_node.op_type == IS_CONST) {
7052
5.35k
    zval_ptr_dtor_nogc(&expr_node.u.constant);
7053
5.35k
  }
7054
7055
8.63k
  efree(jmpnz_opnums);
7056
8.63k
}
7057
/* }}} */
7058
7059
static uint32_t count_match_conds(const zend_ast_list *arms)
7060
7.20k
{
7061
7.20k
  uint32_t num_conds = 0;
7062
7063
14.7k
  for (uint32_t i = 0; i < arms->children; i++) {
7064
7.51k
    const zend_ast *arm_ast = arms->child[i];
7065
7.51k
    if (arm_ast->child[0] == NULL) {
7066
942
      continue;
7067
942
    }
7068
7069
6.56k
    const zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
7070
6.56k
    num_conds += conds->children;
7071
6.56k
  }
7072
7073
7.20k
  return num_conds;
7074
7.20k
}
7075
7076
7.20k
static bool can_match_use_jumptable(const zend_ast_list *arms) {
7077
10.7k
  for (uint32_t i = 0; i < arms->children; i++) {
7078
7.08k
    const zend_ast *arm_ast = arms->child[i];
7079
7.08k
    if (!arm_ast->child[0]) {
7080
      /* Skip default arm */
7081
938
      continue;
7082
938
    }
7083
7084
6.14k
    zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
7085
12.3k
    for (uint32_t j = 0; j < conds->children; j++) {
7086
9.79k
      zend_ast **cond_ast = &conds->child[j];
7087
7088
9.79k
      zend_eval_const_expr(cond_ast);
7089
9.79k
      if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
7090
3.28k
        return false;
7091
3.28k
      }
7092
7093
6.50k
      const zval *cond_zv = zend_ast_get_zval(*cond_ast);
7094
6.50k
      if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
7095
297
        return false;
7096
297
      }
7097
6.50k
    }
7098
6.14k
  }
7099
7100
3.62k
  return true;
7101
7.20k
}
7102
7103
static bool zend_is_pipe_optimizable_callable_name(zend_ast *ast)
7104
2.34k
{
7105
2.34k
  if (ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(ast)) == IS_STRING) {
7106
    /* Assert compilation adds a message operand, but this is incompatible with the
7107
     * pipe optimization that uses a temporary znode for the reference elimination.
7108
     * Therefore, disable the optimization for assert.
7109
     * Note that "assert" as a name is always treated as fully qualified. */
7110
2.09k
    return !zend_string_equals_literal_ci(zend_ast_get_str(ast), "assert");
7111
2.09k
  }
7112
7113
244
  return true;
7114
2.34k
}
7115
7116
static zend_ast *zend_partial_apply(zend_ast *callable_ast, zend_ast *pipe_arg)
7117
77.1k
{
7118
77.1k
  if (callable_ast->kind != ZEND_AST_CALL
7119
73.2k
      && callable_ast->kind != ZEND_AST_STATIC_CALL
7120
72.6k
      && callable_ast->kind != ZEND_AST_METHOD_CALL) {
7121
71.0k
    return NULL;
7122
71.0k
  }
7123
7124
6.00k
  zend_ast *args_ast = zend_ast_call_get_args(callable_ast);
7125
6.00k
  if (!args_ast || args_ast->kind != ZEND_AST_CALLABLE_CONVERT) {
7126
2.95k
    return NULL;
7127
2.95k
  }
7128
7129
3.04k
  if (callable_ast->kind == ZEND_AST_CALL &&
7130
2.34k
      !zend_is_pipe_optimizable_callable_name(callable_ast->child[0])) {
7131
205
    return NULL;
7132
205
  }
7133
7134
2.84k
  zend_ast_list *arg_list = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args);
7135
7136
2.84k
  zend_ast *first_placeholder = NULL;
7137
2.84k
  bool uses_named_args = false;
7138
7139
6.20k
  for (uint32_t i = 0; i < arg_list->children; i++) {
7140
4.53k
    zend_ast *arg = arg_list->child[i];
7141
4.53k
    if (arg->kind == ZEND_AST_NAMED_ARG) {
7142
348
      uses_named_args = true;
7143
348
      arg = arg->child[1];
7144
348
    }
7145
7146
4.53k
    if (arg->kind == ZEND_AST_PLACEHOLDER_ARG) {
7147
3.80k
      if (first_placeholder == NULL) {
7148
2.84k
        first_placeholder = arg;
7149
2.84k
      } else {
7150
        /* A PFA with multiple placeholders is unexpected in this
7151
         * context, and will usually error due to a missing argument,
7152
         * so we don't optimize those. */
7153
959
        return NULL;
7154
959
      }
7155
2.84k
      if (arg->attr == ZEND_PLACEHOLDER_VARIADIC && uses_named_args) {
7156
        /* A PFA with both a variadic placeholder and named args can not
7157
         * be optimized because this would result in a positional arg
7158
         * after a named arg: f(name: $v, ...) -> f(name: $v, pipe_arg).
7159
         * Arg placeholders ('?') are safe since they are not allowed
7160
         * after named args. */
7161
211
        return NULL;
7162
211
      }
7163
2.84k
    }
7164
4.53k
  }
7165
7166
1.67k
  ZEND_ASSERT(first_placeholder);
7167
7168
1.67k
  zend_ast *new_arg_list = zend_ast_create_list(0, arg_list->kind);
7169
3.86k
  for (uint32_t i = 0; i < arg_list->children; i++) {
7170
2.19k
    zend_ast *arg = arg_list->child[i];
7171
2.19k
    if (arg == first_placeholder) {
7172
1.60k
      new_arg_list = zend_ast_list_add(new_arg_list, pipe_arg);
7173
1.60k
    } else if (arg->kind == ZEND_AST_NAMED_ARG
7174
133
        && arg->child[1] == first_placeholder) {
7175
67
      zend_ast *name = arg->child[0];
7176
67
      new_arg_list = zend_ast_list_add(new_arg_list,
7177
67
          zend_ast_create(ZEND_AST_NAMED_ARG, name, pipe_arg));
7178
520
    } else {
7179
520
      new_arg_list = zend_ast_list_add(new_arg_list, arg);
7180
520
    }
7181
2.19k
  }
7182
7183
1.67k
  return new_arg_list;
7184
1.67k
}
7185
7186
static void zend_compile_pipe(znode *result, zend_ast *ast, uint32_t type)
7187
76.8k
{
7188
76.8k
  zend_ast *operand_ast = ast->child[0];
7189
76.8k
  zend_ast *callable_ast = ast->child[1];
7190
7191
76.8k
  if (callable_ast->kind == ZEND_AST_ARROW_FUNC && !(callable_ast->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) {
7192
1
    zend_error_noreturn(E_COMPILE_ERROR, "Arrow functions on the right hand side of |> must be parenthesized");
7193
1
  }
7194
7195
  /* Compile the left hand side down to a value first. */
7196
76.8k
  znode operand_result;
7197
76.8k
  zend_compile_expr(&operand_result, operand_ast);
7198
7199
  /* Wrap simple values in a ZEND_QM_ASSIGN opcode to ensure references
7200
   * always fail. They will already fail in complex cases like arrays,
7201
   * so those don't need a wrapper. */
7202
76.8k
  znode wrapped_operand_result;
7203
76.8k
  if (operand_result.op_type & (IS_CV|IS_VAR)) {
7204
755
    zend_emit_op_tmp(&wrapped_operand_result, ZEND_QM_ASSIGN, &operand_result, NULL);
7205
76.0k
  } else {
7206
76.0k
    wrapped_operand_result = operand_result;
7207
76.0k
  }
7208
7209
  /* Turn the operand into a function parameter list. */
7210
76.8k
  zend_ast *arg = zend_ast_create_znode(&wrapped_operand_result);
7211
7212
76.8k
  zend_ast *fcall_ast;
7213
76.8k
  znode callable_result;
7214
76.8k
  zend_ast *pfa_arg_list_ast = NULL;
7215
7216
  /* Turn $foo |> PFA into plain function call if possible */
7217
76.8k
  if ((pfa_arg_list_ast = zend_partial_apply(callable_ast, arg))) {
7218
1.39k
    switch (callable_ast->kind) {
7219
725
      case ZEND_AST_CALL:
7220
725
        fcall_ast = zend_ast_create(ZEND_AST_CALL,
7221
725
            callable_ast->child[0], pfa_arg_list_ast);
7222
725
        break;
7223
604
      case ZEND_AST_STATIC_CALL:
7224
604
        fcall_ast = zend_ast_create(ZEND_AST_STATIC_CALL,
7225
604
            callable_ast->child[0], callable_ast->child[1],
7226
604
            pfa_arg_list_ast);
7227
604
        break;
7228
68
      case ZEND_AST_METHOD_CALL:
7229
68
        fcall_ast = zend_ast_create(ZEND_AST_METHOD_CALL,
7230
68
            callable_ast->child[0], callable_ast->child[1],
7231
68
            pfa_arg_list_ast);
7232
68
        break;
7233
0
      default:
7234
0
        ZEND_UNREACHABLE();
7235
1.39k
    }
7236
  /* Turn $foo |> $expr into ($expr)($foo) */
7237
75.4k
  } else {
7238
75.4k
    zend_ast *arg_list_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, arg);
7239
75.4k
    zend_compile_expr(&callable_result, callable_ast);
7240
75.4k
    callable_ast = zend_ast_create_znode(&callable_result);
7241
75.4k
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
7242
75.4k
      callable_ast, arg_list_ast);
7243
75.4k
  }
7244
7245
76.8k
  zend_do_extended_stmt(&operand_result);
7246
7247
76.8k
  zend_compile_var(result, fcall_ast, type, /* by_ref */ false);
7248
76.8k
}
7249
7250
static void zend_compile_match(znode *result, zend_ast *ast)
7251
7.20k
{
7252
7.20k
  zend_ast *expr_ast = ast->child[0];
7253
7.20k
  zend_ast_list *arms = zend_ast_get_list(ast->child[1]);
7254
7.20k
  bool has_default_arm = false;
7255
7.20k
  uint32_t opnum_match = (uint32_t)-1;
7256
7257
7.20k
  znode expr_node;
7258
7.20k
  zend_compile_expr(&expr_node, expr_ast);
7259
7260
7.20k
  znode case_node;
7261
7.20k
  case_node.op_type = IS_TMP_VAR;
7262
7.20k
  case_node.u.op.var = get_temporary_variable();
7263
7264
7.20k
  uint32_t num_conds = count_match_conds(arms);
7265
7.20k
  uint8_t can_use_jumptable = can_match_use_jumptable(arms);
7266
7.20k
  bool uses_jumptable = can_use_jumptable && num_conds >= 2;
7267
7.20k
  HashTable *jumptable = NULL;
7268
7.20k
  uint32_t *jmpnz_opnums = NULL;
7269
7270
14.7k
  for (uint32_t i = 0; i < arms->children; ++i) {
7271
7.51k
    zend_ast *arm_ast = arms->child[i];
7272
7273
7.51k
    if (!arm_ast->child[0]) {
7274
942
      if (has_default_arm) {
7275
1
        CG(zend_lineno) = arm_ast->lineno;
7276
1
        zend_error_noreturn(E_COMPILE_ERROR,
7277
1
          "Match expressions may only contain one default arm");
7278
1
      }
7279
942
      has_default_arm = true;
7280
942
    }
7281
7.51k
  }
7282
7283
7.20k
  if (uses_jumptable) {
7284
1.94k
    znode jumptable_op;
7285
7286
1.94k
    ALLOC_HASHTABLE(jumptable);
7287
1.94k
    zend_hash_init(jumptable, num_conds, NULL, NULL, 0);
7288
1.94k
    jumptable_op.op_type = IS_CONST;
7289
1.94k
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
7290
7291
1.94k
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH, &expr_node, &jumptable_op);
7292
1.94k
    if (opline->op1_type == IS_CONST) {
7293
1.25k
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
7294
1.25k
    }
7295
1.94k
    opnum_match = opline - CG(active_op_array)->opcodes;
7296
5.26k
  } else {
7297
5.26k
    jmpnz_opnums = safe_emalloc(sizeof(uint32_t), num_conds, 0);
7298
5.26k
    uint32_t cond_count = 0;
7299
10.3k
    for (uint32_t i = 0; i < arms->children; ++i) {
7300
5.08k
      zend_ast *arm_ast = arms->child[i];
7301
7302
5.08k
      if (!arm_ast->child[0]) {
7303
915
        continue;
7304
915
      }
7305
7306
4.16k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
7307
12.5k
      for (uint32_t j = 0; j < conds->children; j++) {
7308
8.39k
        zend_ast *cond_ast = conds->child[j];
7309
7310
8.39k
        znode cond_node;
7311
8.39k
        zend_compile_expr(&cond_node, cond_ast);
7312
7313
8.39k
        uint32_t opcode = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE_STRICT : ZEND_IS_IDENTICAL;
7314
8.39k
        zend_op *opline = zend_emit_op(NULL, opcode, &expr_node, &cond_node);
7315
8.39k
        SET_NODE(opline->result, &case_node);
7316
8.39k
        if (opline->op1_type == IS_CONST) {
7317
5.25k
          Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
7318
5.25k
        }
7319
7320
8.39k
        jmpnz_opnums[cond_count] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
7321
7322
8.39k
        cond_count++;
7323
8.39k
      }
7324
4.16k
    }
7325
5.26k
  }
7326
7327
7.20k
  uint32_t opnum_default_jmp = 0;
7328
7.20k
  if (!uses_jumptable) {
7329
5.26k
    opnum_default_jmp = zend_emit_jump(0);
7330
5.26k
  }
7331
7332
7.20k
  bool is_first_case = true;
7333
7.20k
  uint32_t cond_count = 0;
7334
7.20k
  uint32_t *jmp_end_opnums = safe_emalloc(sizeof(uint32_t), arms->children, 0);
7335
7336
  // The generated default arm is emitted first to avoid live range issues where the tmpvar
7337
  // for the arm result is freed even though it has not been initialized yet.
7338
7.20k
  if (!has_default_arm) {
7339
6.26k
    if (!uses_jumptable) {
7340
4.34k
      zend_update_jump_target_to_next(opnum_default_jmp);
7341
4.34k
    }
7342
7343
6.26k
    if (jumptable) {
7344
1.91k
      zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
7345
1.91k
      opline->extended_value = get_next_op_number();
7346
1.91k
    }
7347
7348
6.26k
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH_ERROR, &expr_node, NULL);
7349
6.26k
    if (opline->op1_type == IS_CONST) {
7350
2.96k
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
7351
2.96k
    }
7352
6.26k
    if (arms->children == 0) {
7353
      /* Mark this as an "expression throw" for opcache. */
7354
727
      opline->extended_value = ZEND_THROW_IS_EXPR;
7355
727
    }
7356
6.26k
  }
7357
7358
14.7k
  for (uint32_t i = 0; i < arms->children; ++i) {
7359
7.50k
    zend_ast *arm_ast = arms->child[i];
7360
7.50k
    zend_ast *body_ast = arm_ast->child[1];
7361
7362
7.50k
    if (arm_ast->child[0] != NULL) {
7363
6.56k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
7364
7365
19.7k
      for (uint32_t j = 0; j < conds->children; j++) {
7366
13.2k
        zend_ast *cond_ast = conds->child[j];
7367
7368
13.2k
        if (jmpnz_opnums != NULL) {
7369
8.39k
          zend_update_jump_target_to_next(jmpnz_opnums[cond_count]);
7370
8.39k
        }
7371
7372
13.2k
        if (jumptable) {
7373
4.81k
          zval *cond_zv = zend_ast_get_zval(cond_ast);
7374
4.81k
          zval jmp_target;
7375
4.81k
          ZVAL_LONG(&jmp_target, get_next_op_number());
7376
7377
4.81k
          if (Z_TYPE_P(cond_zv) == IS_LONG) {
7378
4.42k
            zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
7379
4.42k
          } else {
7380
394
            ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
7381
394
            zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
7382
394
          }
7383
4.81k
        }
7384
7385
13.2k
        cond_count++;
7386
13.2k
      }
7387
6.56k
    } else {
7388
940
      if (!uses_jumptable) {
7389
915
        zend_update_jump_target_to_next(opnum_default_jmp);
7390
915
      }
7391
7392
940
      if (jumptable) {
7393
25
        ZEND_ASSERT(opnum_match != (uint32_t)-1);
7394
25
        zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
7395
25
        opline->extended_value = get_next_op_number();
7396
25
      }
7397
940
    }
7398
7399
7.50k
    znode body_node;
7400
7.50k
    zend_compile_expr(&body_node, body_ast);
7401
7402
7.50k
    if (is_first_case) {
7403
6.47k
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL);
7404
6.47k
      is_first_case = false;
7405
6.47k
    } else {
7406
1.02k
      zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL);
7407
1.02k
      SET_NODE(opline_qm_assign->result, result);
7408
1.02k
    }
7409
7410
7.50k
    jmp_end_opnums[i] = zend_emit_jump(0);
7411
7.50k
  }
7412
7413
  // Initialize result in case there is no arm
7414
7.20k
  if (arms->children == 0) {
7415
727
    result->op_type = IS_CONST;
7416
727
    ZVAL_NULL(&result->u.constant);
7417
727
  }
7418
7419
14.7k
  for (uint32_t i = 0; i < arms->children; ++i) {
7420
7.50k
    zend_update_jump_target_to_next(jmp_end_opnums[i]);
7421
7.50k
  }
7422
7423
7.20k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
7424
3.94k
    zend_op *opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
7425
3.94k
    opline->extended_value = ZEND_FREE_SWITCH;
7426
3.94k
  } else if (expr_node.op_type == IS_CONST) {
7427
2.98k
    zval_ptr_dtor_nogc(&expr_node.u.constant);
7428
2.98k
  }
7429
7430
7.20k
  if (jmpnz_opnums != NULL) {
7431
5.26k
    efree(jmpnz_opnums);
7432
5.26k
  }
7433
7.20k
  efree(jmp_end_opnums);
7434
7.20k
}
7435
7436
static void zend_compile_try(const zend_ast *ast) /* {{{ */
7437
8.22k
{
7438
8.22k
  zend_ast *try_ast = ast->child[0];
7439
8.22k
  const zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
7440
8.22k
  zend_ast *finally_ast = ast->child[2];
7441
7442
8.22k
  uint32_t i, j;
7443
8.22k
  zend_op *opline;
7444
8.22k
  uint32_t try_catch_offset;
7445
8.22k
  uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
7446
8.22k
  uint32_t orig_fast_call_var = CG(context).fast_call_var;
7447
8.22k
  uint32_t orig_try_catch_offset = CG(context).try_catch_offset;
7448
7449
8.22k
  if (catches->children == 0 && !finally_ast) {
7450
36
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
7451
36
  }
7452
7453
  /* label: try { } must not be equal to try { label: } */
7454
8.22k
  if (CG(context).labels) {
7455
2.12k
    zend_label *label;
7456
2.12k
    ZEND_HASH_MAP_REVERSE_FOREACH_PTR(CG(context).labels, label) {
7457
2.12k
      if (label->opline_num == get_next_op_number()) {
7458
1.16k
        zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
7459
1.16k
      }
7460
2.12k
      break;
7461
6.37k
    } ZEND_HASH_FOREACH_END();
7462
2.12k
  }
7463
7464
8.22k
  try_catch_offset = zend_add_try_element(get_next_op_number());
7465
7466
8.22k
  if (finally_ast) {
7467
5.73k
    zend_loop_var fast_call;
7468
5.73k
    if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
7469
2.26k
      CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
7470
2.26k
    }
7471
5.73k
    CG(context).fast_call_var = get_temporary_variable();
7472
7473
    /* Push FAST_CALL on unwind stack */
7474
5.73k
    fast_call.opcode = ZEND_FAST_CALL;
7475
5.73k
    fast_call.var_type = IS_TMP_VAR;
7476
5.73k
    fast_call.var_num = CG(context).fast_call_var;
7477
5.73k
    fast_call.try_catch_offset = try_catch_offset;
7478
5.73k
    zend_stack_push(&CG(loop_var_stack), &fast_call);
7479
5.73k
  }
7480
7481
8.22k
  CG(context).try_catch_offset = try_catch_offset;
7482
7483
8.22k
  zend_compile_stmt(try_ast);
7484
7485
8.22k
  if (catches->children != 0) {
7486
2.44k
    jmp_opnums[0] = zend_emit_jump(0);
7487
2.44k
  }
7488
7489
11.5k
  for (i = 0; i < catches->children; ++i) {
7490
3.29k
    const zend_ast *catch_ast = catches->child[i];
7491
3.29k
    const zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]);
7492
3.29k
    zend_ast *var_ast = catch_ast->child[1];
7493
3.29k
    zend_ast *stmt_ast = catch_ast->child[2];
7494
3.29k
    zend_string *var_name = var_ast ? zval_make_interned_string(zend_ast_get_zval(var_ast)) : NULL;
7495
3.29k
    bool is_last_catch = (i + 1 == catches->children);
7496
7497
3.29k
    uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0);
7498
3.29k
    uint32_t opnum_catch = (uint32_t)-1;
7499
7500
3.29k
    CG(zend_lineno) = catch_ast->lineno;
7501
7502
13.9k
    for (j = 0; j < classes->children; j++) {
7503
10.6k
      zend_ast *class_ast = classes->child[j];
7504
10.6k
      bool is_last_class = (j + 1 == classes->children);
7505
7506
10.6k
      if (!zend_is_const_default_class_ref(class_ast)) {
7507
2
        zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
7508
2
      }
7509
7510
10.6k
      opnum_catch = get_next_op_number();
7511
10.6k
      if (i == 0 && j == 0) {
7512
2.43k
        CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
7513
2.43k
      }
7514
7515
10.6k
      opline = get_next_op();
7516
10.6k
      opline->opcode = ZEND_CATCH;
7517
10.6k
      opline->op1_type = IS_CONST;
7518
10.6k
      opline->op1.constant = zend_add_class_name_literal(
7519
10.6k
          zend_resolve_class_name_ast(class_ast));
7520
10.6k
      opline->extended_value = zend_alloc_cache_slot();
7521
7522
10.6k
      if (var_name && zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
7523
2
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
7524
2
      }
7525
7526
10.6k
      opline->result_type = var_name ? IS_CV : IS_UNUSED;
7527
10.6k
      opline->result.var = var_name ? lookup_cv(var_name) : -1;
7528
7529
10.6k
      if (is_last_catch && is_last_class) {
7530
2.43k
        opline->extended_value |= ZEND_LAST_CATCH;
7531
2.43k
      }
7532
7533
10.6k
      if (!is_last_class) {
7534
7.38k
        jmp_multicatch[j] = zend_emit_jump(0);
7535
7.38k
        opline = &CG(active_op_array)->opcodes[opnum_catch];
7536
7.38k
        opline->op2.opline_num = get_next_op_number();
7537
7.38k
      }
7538
10.6k
    }
7539
7540
10.6k
    for (j = 0; j < classes->children - 1; j++) {
7541
7.37k
      zend_update_jump_target_to_next(jmp_multicatch[j]);
7542
7.37k
    }
7543
7544
3.29k
    efree(jmp_multicatch);
7545
7546
3.29k
    zend_compile_stmt(stmt_ast);
7547
7548
3.29k
    if (!is_last_catch) {
7549
852
      jmp_opnums[i + 1] = zend_emit_jump(0);
7550
852
    }
7551
7552
3.29k
    ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class");
7553
3.29k
    opline = &CG(active_op_array)->opcodes[opnum_catch];
7554
3.28k
    if (!is_last_catch) {
7555
852
      opline->op2.opline_num = get_next_op_number();
7556
852
    }
7557
3.28k
  }
7558
7559
11.5k
  for (i = 0; i < catches->children; ++i) {
7560
3.28k
    zend_update_jump_target_to_next(jmp_opnums[i]);
7561
3.28k
  }
7562
7563
8.22k
  if (finally_ast) {
7564
5.73k
    zend_loop_var discard_exception;
7565
5.73k
    uint32_t opnum_jmp = get_next_op_number() + 1;
7566
7567
    /* Pop FAST_CALL from unwind stack */
7568
5.73k
    zend_stack_del_top(&CG(loop_var_stack));
7569
7570
    /* Push DISCARD_EXCEPTION on unwind stack */
7571
5.73k
    discard_exception.opcode = ZEND_DISCARD_EXCEPTION;
7572
5.73k
    discard_exception.var_type = IS_TMP_VAR;
7573
5.73k
    discard_exception.var_num = CG(context).fast_call_var;
7574
5.73k
    zend_stack_push(&CG(loop_var_stack), &discard_exception);
7575
7576
5.73k
    CG(zend_lineno) = finally_ast->lineno;
7577
7578
5.73k
    opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
7579
5.73k
    opline->op1.num = try_catch_offset;
7580
5.73k
    opline->result_type = IS_TMP_VAR;
7581
5.73k
    opline->result.var = CG(context).fast_call_var;
7582
7583
5.73k
    zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
7584
7585
5.73k
    bool orig_in_finally = CG(context).in_finally;
7586
5.73k
    CG(context).in_finally = true;
7587
5.73k
    zend_compile_stmt(finally_ast);
7588
5.73k
    CG(context).in_finally = orig_in_finally;
7589
7590
5.73k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
7591
5.73k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
7592
5.73k
      = get_next_op_number();
7593
7594
5.73k
    opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
7595
5.73k
    opline->op1_type = IS_TMP_VAR;
7596
5.73k
    opline->op1.var = CG(context).fast_call_var;
7597
5.73k
    opline->op2.num = orig_try_catch_offset;
7598
7599
5.73k
    zend_update_jump_target_to_next(opnum_jmp);
7600
7601
5.73k
    CG(context).fast_call_var = orig_fast_call_var;
7602
7603
    /* Pop DISCARD_EXCEPTION from unwind stack */
7604
5.73k
    zend_stack_del_top(&CG(loop_var_stack));
7605
5.73k
  }
7606
7607
8.22k
  CG(context).try_catch_offset = orig_try_catch_offset;
7608
7609
8.22k
  efree(jmp_opnums);
7610
8.22k
}
7611
/* }}} */
7612
7613
/* Encoding declarations must already be handled during parsing */
7614
bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
7615
3.51k
{
7616
3.51k
  const zend_ast_list *declares = zend_ast_get_list(ast);
7617
3.51k
  uint32_t i;
7618
8.19k
  for (i = 0; i < declares->children; ++i) {
7619
4.68k
    const zend_ast *declare_ast = declares->child[i];
7620
4.68k
    zend_ast *name_ast = declare_ast->child[0];
7621
4.68k
    zend_ast *value_ast = declare_ast->child[1];
7622
4.68k
    const zend_string *name = zend_ast_get_str(name_ast);
7623
7624
4.68k
    if (zend_string_equals_literal_ci(name, "encoding")) {
7625
145
      if (value_ast->kind != ZEND_AST_ZVAL) {
7626
4
        zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0);
7627
4
        return false;
7628
4
      }
7629
7630
141
      if (CG(multibyte)) {
7631
0
        zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
7632
7633
0
        const zend_encoding *new_encoding, *old_encoding;
7634
0
        zend_encoding_filter old_input_filter;
7635
7636
0
        CG(encoding_declared) = 1;
7637
7638
0
        new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
7639
0
        if (!new_encoding) {
7640
0
          zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
7641
0
        } else {
7642
0
          old_input_filter = LANG_SCNG(input_filter);
7643
0
          old_encoding = LANG_SCNG(script_encoding);
7644
0
          zend_multibyte_set_filter(new_encoding);
7645
7646
          /* need to re-scan if input filter changed */
7647
0
          if (old_input_filter != LANG_SCNG(input_filter) ||
7648
0
             (old_input_filter && new_encoding != old_encoding)) {
7649
0
            zend_multibyte_yyinput_again(old_input_filter, old_encoding);
7650
0
          }
7651
0
        }
7652
7653
0
        zend_string_release_ex(encoding_name, 0);
7654
141
      } else {
7655
141
        zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
7656
141
          "Zend multibyte feature is turned off by settings");
7657
141
      }
7658
141
    }
7659
4.68k
  }
7660
7661
3.50k
  return true;
7662
3.51k
}
7663
/* }}} */
7664
7665
/* Check whether this is the first statement, not counting declares. */
7666
static zend_result zend_is_first_statement(const zend_ast *ast, bool allow_nop) /* {{{ */
7667
3.19k
{
7668
3.19k
  uint32_t i = 0;
7669
3.19k
  const zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
7670
7671
12.7k
  while (i < file_ast->children) {
7672
12.7k
    if (file_ast->child[i] == ast) {
7673
3.15k
      return SUCCESS;
7674
9.60k
    } else if (file_ast->child[i] == NULL) {
7675
4.67k
      if (!allow_nop) {
7676
1
        return FAILURE;
7677
1
      }
7678
4.93k
    } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
7679
37
      return FAILURE;
7680
37
    }
7681
9.56k
    i++;
7682
9.56k
  }
7683
3
  return FAILURE;
7684
3.19k
}
7685
/* }}} */
7686
7687
static void zend_compile_declare(const zend_ast *ast) /* {{{ */
7688
14.8k
{
7689
14.8k
  const zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
7690
14.8k
  zend_ast *stmt_ast = ast->child[1];
7691
14.8k
  zend_declarables orig_declarables = FC(declarables);
7692
14.8k
  uint32_t i;
7693
7694
33.3k
  for (i = 0; i < declares->children; ++i) {
7695
18.4k
    zend_ast *declare_ast = declares->child[i];
7696
18.4k
    zend_ast *name_ast = declare_ast->child[0];
7697
18.4k
    zend_ast **value_ast_ptr = &declare_ast->child[1];
7698
18.4k
    zend_string *name = zend_ast_get_str(name_ast);
7699
7700
18.4k
    if ((*value_ast_ptr)->kind != ZEND_AST_ZVAL) {
7701
37
      zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
7702
37
    }
7703
7704
18.4k
    if (zend_string_equals_literal_ci(name, "ticks")) {
7705
12.8k
      zval value_zv;
7706
12.8k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7707
12.8k
      FC(declarables).ticks = zval_get_long(&value_zv);
7708
12.8k
      zval_ptr_dtor_nogc(&value_zv);
7709
12.8k
    } else if (zend_string_equals_literal_ci(name, "encoding")) {
7710
7711
133
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ false)) {
7712
7
        zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
7713
7
          "the very first statement in the script");
7714
7
      }
7715
5.48k
    } else if (zend_string_equals_literal_ci(name, "strict_types")) {
7716
290
      zval value_zv;
7717
7718
290
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
7719
5
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
7720
5
          "the very first statement in the script");
7721
5
      }
7722
7723
290
      if (ast->child[1] != NULL) {
7724
1
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
7725
1
          "use block mode");
7726
1
      }
7727
7728
290
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7729
7730
290
      if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
7731
77
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
7732
77
      }
7733
7734
290
      if (Z_LVAL(value_zv) == 1) {
7735
77
        CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
7736
77
      }
7737
7738
5.19k
    } else {
7739
5.19k
      zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
7740
5.19k
    }
7741
18.4k
  }
7742
7743
14.8k
  if (stmt_ast) {
7744
3.13k
    zend_compile_stmt(stmt_ast);
7745
7746
3.13k
    FC(declarables) = orig_declarables;
7747
3.13k
  }
7748
14.8k
}
7749
/* }}} */
7750
7751
static void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
7752
2.54M
{
7753
2.54M
  const zend_ast_list *list = zend_ast_get_list(ast);
7754
2.54M
  uint32_t i;
7755
11.2M
  for (i = 0; i < list->children; ++i) {
7756
8.71M
    zend_compile_stmt(list->child[i]);
7757
8.71M
  }
7758
2.54M
}
7759
/* }}} */
7760
7761
ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
7762
1.40M
{
7763
1.40M
  uint32_t i, n;
7764
7765
1.40M
  func->common.arg_flags[0] = 0;
7766
1.40M
  func->common.arg_flags[1] = 0;
7767
1.40M
  func->common.arg_flags[2] = 0;
7768
1.40M
  if (func->common.arg_info) {
7769
1.40M
    n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
7770
1.40M
    i = 0;
7771
2.83M
    while (i < n) {
7772
1.42M
      ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i]));
7773
1.42M
      i++;
7774
1.42M
    }
7775
1.40M
    if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_VARIADIC) && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) {
7776
883
      uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]);
7777
11.3k
      while (i < MAX_ARG_FLAG_NUM) {
7778
10.4k
        ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
7779
10.4k
        i++;
7780
10.4k
      }
7781
883
    }
7782
1.40M
  }
7783
1.40M
}
7784
/* }}} */
7785
7786
static zend_type zend_compile_single_typename(zend_ast *ast)
7787
811k
{
7788
811k
  ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE));
7789
811k
  if (ast->kind == ZEND_AST_TYPE) {
7790
6.42k
    if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) {
7791
6
      zend_error_noreturn(E_COMPILE_ERROR,
7792
6
        "Cannot use \"static\" when no class scope is active");
7793
6
    }
7794
7795
6.42k
    return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0);
7796
805k
  } else {
7797
805k
    zend_string *type_name = zend_ast_get_str(ast);
7798
805k
    uint8_t type_code = zend_lookup_builtin_type_by_name(type_name);
7799
7800
805k
    if (type_code != 0) {
7801
340k
      if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) {
7802
7
        zend_error_noreturn(E_COMPILE_ERROR,
7803
7
          "Type declaration '%s' must be unqualified",
7804
7
          ZSTR_VAL(zend_string_tolower(type_name)));
7805
7
      }
7806
7807
340k
      ZEND_ASSERT(type_code != IS_STATIC && type_code != IS_ARRAY && type_code != IS_CALLABLE
7808
340k
        && "unqualified array/callable/static type should have been handled by ZEND_AST_TYPE branch");
7809
7810
      /* Transform iterable into a type union alias */
7811
340k
      if (type_code == IS_ITERABLE) {
7812
        /* Set iterable bit for BC compat during Reflection and string representation of type */
7813
323k
        zend_type iterable = (zend_type) ZEND_TYPE_INIT_CLASS_MASK(ZSTR_KNOWN(ZEND_STR_TRAVERSABLE),
7814
323k
                  (MAY_BE_ARRAY|_ZEND_TYPE_ITERABLE_BIT));
7815
323k
        return iterable;
7816
323k
      }
7817
7818
17.6k
      return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0);
7819
464k
    } else {
7820
464k
      const char *correct_name;
7821
464k
      uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
7822
464k
      zend_string *class_name = type_name;
7823
7824
464k
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
7825
463k
        class_name = zend_resolve_class_name_ast(ast);
7826
463k
        zend_assert_valid_class_name(class_name, "a type name");
7827
463k
      } else {
7828
1.10k
        ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT);
7829
7830
1.10k
        zend_ensure_valid_class_fetch_type(fetch_type);
7831
7832
1.10k
        bool substitute_self_parent = zend_is_scope_known()
7833
578
          && !(CG(active_class_entry)->ce_flags & ZEND_ACC_ANON_CLASS);
7834
7835
1.10k
        if (fetch_type == ZEND_FETCH_CLASS_SELF) {
7836
          /* Scope might be unknown for unbound closures and traits */
7837
1.04k
          if (substitute_self_parent) {
7838
558
            class_name = CG(active_class_entry)->name;
7839
558
            ZEND_ASSERT(class_name && "must know class name when resolving self type at compile time");
7840
558
          }
7841
1.04k
        } else {
7842
54
          ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_PARENT);
7843
          /* Scope might be unknown for unbound closures and traits */
7844
54
          if (substitute_self_parent) {
7845
20
            class_name = CG(active_class_entry)->parent_name;
7846
20
            ZEND_ASSERT(class_name && "must know class name when resolving parent type at compile time");
7847
20
          }
7848
39
        }
7849
1.10k
        zend_string_addref(class_name);
7850
1.08k
      }
7851
7852
464k
      if (ast->attr == ZEND_NAME_NOT_FQ
7853
462k
          && zend_is_confusable_type(type_name, &correct_name)
7854
67.8k
          && zend_is_not_imported(type_name)) {
7855
67.8k
        const char *extra =
7856
67.8k
          FC(current_namespace) ? " or import the class with \"use\"" : "";
7857
67.8k
        if (correct_name) {
7858
15.3k
          zend_error(E_COMPILE_WARNING,
7859
15.3k
            "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? "
7860
15.3k
            "Write \"\\%s\"%s to suppress this warning",
7861
15.3k
            ZSTR_VAL(type_name), correct_name, ZSTR_VAL(class_name), extra);
7862
52.4k
        } else {
7863
52.4k
          zend_error(E_COMPILE_WARNING,
7864
52.4k
            "\"%s\" is not a supported builtin type "
7865
52.4k
            "and will be interpreted as a class name. "
7866
52.4k
            "Write \"\\%s\"%s to suppress this warning",
7867
52.4k
            ZSTR_VAL(type_name), ZSTR_VAL(class_name), extra);
7868
52.4k
        }
7869
67.8k
      }
7870
7871
464k
      class_name = zend_new_interned_string(class_name);
7872
464k
      zend_alloc_ce_cache(class_name);
7873
464k
      return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, /* allow null */ false, 0);
7874
464k
    }
7875
805k
  }
7876
811k
}
7877
7878
static void zend_are_intersection_types_redundant(const zend_type left_type, const zend_type right_type)
7879
16.0k
{
7880
16.0k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(left_type));
7881
16.0k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(right_type));
7882
16.0k
  const zend_type_list *l_type_list = ZEND_TYPE_LIST(left_type);
7883
16.0k
  const zend_type_list *r_type_list = ZEND_TYPE_LIST(right_type);
7884
16.0k
  const zend_type_list *smaller_type_list, *larger_type_list;
7885
16.0k
  bool flipped = false;
7886
7887
16.0k
  if (r_type_list->num_types < l_type_list->num_types) {
7888
5.74k
    smaller_type_list = r_type_list;
7889
5.74k
    larger_type_list = l_type_list;
7890
5.74k
    flipped = true;
7891
10.3k
  } else {
7892
10.3k
    smaller_type_list = l_type_list;
7893
10.3k
    larger_type_list = r_type_list;
7894
10.3k
  }
7895
7896
16.0k
  unsigned int sum = 0;
7897
16.0k
  const zend_type *outer_type;
7898
61.9k
  ZEND_TYPE_LIST_FOREACH(smaller_type_list, outer_type) {
7899
61.9k
    const zend_type *inner_type;
7900
208k
    ZEND_TYPE_LIST_FOREACH(larger_type_list, inner_type) {
7901
208k
      if (zend_string_equals_ci(ZEND_TYPE_NAME(*inner_type), ZEND_TYPE_NAME(*outer_type))) {
7902
16.0k
        sum++;
7903
16.0k
        break;
7904
16.0k
      }
7905
208k
    } ZEND_TYPE_LIST_FOREACH_END();
7906
61.9k
  } ZEND_TYPE_LIST_FOREACH_END();
7907
7908
16.0k
  if (sum == smaller_type_list->num_types) {
7909
16
    zend_string *smaller_type_str;
7910
16
    zend_string *larger_type_str;
7911
16
    if (flipped) {
7912
4
      smaller_type_str = zend_type_to_string(right_type);
7913
4
      larger_type_str = zend_type_to_string(left_type);
7914
12
    } else {
7915
12
      smaller_type_str = zend_type_to_string(left_type);
7916
12
      larger_type_str = zend_type_to_string(right_type);
7917
12
    }
7918
16
    if (smaller_type_list->num_types == larger_type_list->num_types) {
7919
10
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant with type %s",
7920
10
        ZSTR_VAL(smaller_type_str), ZSTR_VAL(larger_type_str));
7921
10
    } else {
7922
6
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7923
6
        ZSTR_VAL(larger_type_str), ZSTR_VAL(smaller_type_str));
7924
6
    }
7925
16
  }
7926
16.0k
}
7927
7928
static void zend_is_intersection_type_redundant_by_single_type(const zend_type intersection_type, const zend_type single_type)
7929
12.0k
{
7930
12.0k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(intersection_type));
7931
12.0k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(single_type));
7932
7933
12.0k
  const zend_type *single_intersection_type = NULL;
7934
47.3k
  ZEND_TYPE_FOREACH(intersection_type, single_intersection_type) {
7935
47.3k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(*single_intersection_type), ZEND_TYPE_NAME(single_type))) {
7936
3
      zend_string *single_type_str = zend_type_to_string(single_type);
7937
3
      zend_string *complete_type = zend_type_to_string(intersection_type);
7938
3
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7939
3
          ZSTR_VAL(complete_type), ZSTR_VAL(single_type_str));
7940
3
    }
7941
47.3k
  } ZEND_TYPE_FOREACH_END();
7942
12.0k
}
7943
7944
/* Used by both intersection and union types prior to transforming the type list to a full zend_type */
7945
static void zend_is_type_list_redundant_by_single_type(const zend_type_list *type_list, const zend_type type)
7946
172k
{
7947
172k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(type));
7948
347k
  for (size_t i = 0; i < type_list->num_types - 1; i++) {
7949
174k
    if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7950
6.97k
      zend_is_intersection_type_redundant_by_single_type(type_list->types[i], type);
7951
6.97k
      continue;
7952
6.97k
    }
7953
167k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(type_list->types[i]), ZEND_TYPE_NAME(type))) {
7954
23
      zend_string *single_type_str = zend_type_to_string(type);
7955
23
      zend_error_noreturn(E_COMPILE_ERROR, "Duplicate type %s is redundant", ZSTR_VAL(single_type_str));
7956
23
    }
7957
167k
  }
7958
172k
}
7959
7960
static zend_type zend_compile_typename(zend_ast *ast);
7961
7962
static zend_type zend_compile_typename_ex(
7963
    zend_ast *ast, bool force_allow_null, bool *forced_allow_null) /* {{{ */
7964
655k
{
7965
655k
  bool is_marked_nullable = ast->attr & ZEND_TYPE_NULLABLE;
7966
655k
  zend_ast_attr orig_ast_attr = ast->attr;
7967
655k
  zend_type type = ZEND_TYPE_INIT_NONE(0);
7968
7969
655k
  if (is_marked_nullable) {
7970
4.15k
    ast->attr &= ~ZEND_TYPE_NULLABLE;
7971
4.15k
  }
7972
7973
655k
  if (ast->kind == ZEND_AST_TYPE_UNION) {
7974
138k
    const zend_ast_list *list = zend_ast_get_list(ast);
7975
138k
    zend_type_list *type_list;
7976
138k
    bool is_composite = false;
7977
138k
    bool has_only_iterable_class = true;
7978
138k
    ALLOCA_FLAG(use_heap)
7979
7980
138k
    type_list = do_alloca(ZEND_TYPE_LIST_SIZE(list->children), use_heap);
7981
138k
    type_list->num_types = 0;
7982
7983
421k
    for (uint32_t i = 0; i < list->children; i++) {
7984
282k
      zend_ast *type_ast = list->child[i];
7985
282k
      zend_type single_type;
7986
282k
      uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7987
7988
282k
      if (type_ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7989
7.27k
        has_only_iterable_class = false;
7990
7.27k
        is_composite = true;
7991
        /* The first class type can be stored directly as the type ptr payload. */
7992
7.27k
        if (ZEND_TYPE_IS_COMPLEX(type) && !ZEND_TYPE_HAS_LIST(type)) {
7993
          /* Switch from single name to name list. */
7994
958
          type_list->num_types = 1;
7995
958
          type_list->types[0] = type;
7996
          /* Clear MAY_BE_* type flags */
7997
958
          ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7998
958
        }
7999
        /* Mark type as list type */
8000
7.27k
        ZEND_TYPE_SET_LIST(type, type_list);
8001
8002
7.27k
        single_type = zend_compile_typename(type_ast);
8003
7.27k
        ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(single_type));
8004
8005
7.27k
        type_list->types[type_list->num_types++] = single_type;
8006
8007
        /* Check for trivially redundant class types */
8008
28.3k
        for (size_t i = 0; i < type_list->num_types - 1; i++) {
8009
21.1k
          if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
8010
16.0k
            zend_are_intersection_types_redundant(single_type, type_list->types[i]);
8011
16.0k
            continue;
8012
16.0k
          }
8013
          /* Type from type list is a simple type */
8014
5.03k
          zend_is_intersection_type_redundant_by_single_type(single_type, type_list->types[i]);
8015
5.03k
        }
8016
7.27k
        continue;
8017
7.27k
      }
8018
8019
275k
      single_type = zend_compile_single_typename(type_ast);
8020
275k
      uint32_t single_type_mask = ZEND_TYPE_PURE_MASK(single_type);
8021
8022
275k
      if (single_type_mask == MAY_BE_ANY) {
8023
1
        zend_error_noreturn(E_COMPILE_ERROR, "Type mixed can only be used as a standalone type");
8024
1
      }
8025
275k
      if (ZEND_TYPE_IS_COMPLEX(single_type) && !ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
8026
237k
        has_only_iterable_class = false;
8027
237k
      }
8028
8029
275k
      uint32_t type_mask_overlap = ZEND_TYPE_PURE_MASK(type) & single_type_mask;
8030
275k
      if (type_mask_overlap) {
8031
17
        zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap);
8032
17
        zend_string *overlap_type_str = zend_type_to_string(overlap_type);
8033
17
        zend_error_noreturn(E_COMPILE_ERROR,
8034
17
          "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str));
8035
17
      }
8036
8037
275k
      if ( ((type_mask & MAY_BE_TRUE) && (single_type_mask == MAY_BE_FALSE))
8038
275k
          || ((type_mask & MAY_BE_FALSE) && (single_type_mask == MAY_BE_TRUE)) ) {
8039
10
        zend_error_noreturn(E_COMPILE_ERROR,
8040
10
          "Type contains both true and false, bool must be used instead");
8041
10
      }
8042
275k
      ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type);
8043
      /* Clear MAY_BE_* type flags */
8044
275k
      ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK;
8045
8046
275k
      if (ZEND_TYPE_IS_COMPLEX(single_type)) {
8047
272k
        if (!ZEND_TYPE_IS_COMPLEX(type) && !is_composite) {
8048
          /* The first class type can be stored directly as the type ptr payload. */
8049
134k
          ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type));
8050
134k
          ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT;
8051
138k
        } else {
8052
138k
          if (type_list->num_types == 0) {
8053
            /* Switch from single name to name list. */
8054
132k
            type_list->num_types = 1;
8055
132k
            type_list->types[0] = type;
8056
            /* Clear MAY_BE_* type flags */
8057
132k
            ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
8058
132k
            ZEND_TYPE_SET_LIST(type, type_list);
8059
132k
          }
8060
8061
138k
          type_list->types[type_list->num_types++] = single_type;
8062
8063
          /* Check for trivially redundant class types */
8064
138k
          zend_is_type_list_redundant_by_single_type(type_list, single_type);
8065
138k
        }
8066
272k
      }
8067
275k
    }
8068
8069
138k
    if (type_list->num_types) {
8070
136k
      zend_type_list *list = zend_arena_alloc(
8071
136k
        &CG(arena), ZEND_TYPE_LIST_SIZE(type_list->num_types));
8072
136k
      memcpy(list, type_list, ZEND_TYPE_LIST_SIZE(type_list->num_types));
8073
136k
      ZEND_TYPE_SET_LIST(type, list);
8074
136k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
8075
      /* Inform that the type list is a union type */
8076
136k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
8077
136k
    }
8078
8079
138k
    free_alloca(type_list, use_heap);
8080
8081
138k
    uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
8082
138k
    if ((type_mask & MAY_BE_OBJECT) &&
8083
511
        ((!has_only_iterable_class && ZEND_TYPE_IS_COMPLEX(type)) || (type_mask & MAY_BE_STATIC))) {
8084
6
      zend_string *type_str = zend_type_to_string(type);
8085
6
      zend_error_noreturn(E_COMPILE_ERROR,
8086
6
        "Type %s contains both object and a class type, which is redundant",
8087
6
        ZSTR_VAL(type_str));
8088
6
    }
8089
517k
  } else if (ast->kind == ZEND_AST_TYPE_INTERSECTION) {
8090
15.2k
    const zend_ast_list *list = zend_ast_get_list(ast);
8091
15.2k
    zend_type_list *type_list;
8092
8093
    /* Allocate the type list directly on the arena as it must be a type
8094
     * list of the same number of elements as the AST list has children */
8095
15.2k
    type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children));
8096
15.2k
    type_list->num_types = 0;
8097
8098
15.2k
    ZEND_ASSERT(list->children > 1);
8099
8100
49.6k
    for (uint32_t i = 0; i < list->children; i++) {
8101
34.4k
      zend_ast *type_ast = list->child[i];
8102
34.4k
      zend_type single_type = zend_compile_single_typename(type_ast);
8103
8104
      /* An intersection of union types cannot exist so invalidate it
8105
       * Currently only can happen with iterable getting canonicalized to Traversable|array */
8106
34.4k
      if (ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
8107
1
        zend_string *standard_type_str = zend_type_to_string(single_type);
8108
1
        zend_error_noreturn(E_COMPILE_ERROR,
8109
1
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
8110
1
      }
8111
      /* An intersection of standard types cannot exist so invalidate it */
8112
34.4k
      if (ZEND_TYPE_IS_ONLY_MASK(single_type)) {
8113
36
        zend_string *standard_type_str = zend_type_to_string(single_type);
8114
36
        zend_error_noreturn(E_COMPILE_ERROR,
8115
36
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
8116
36
      }
8117
      /* Check for "self" and "parent" too */
8118
34.4k
      if (
8119
34.4k
        zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_SELF))
8120
34.3k
        || zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_PARENT))
8121
34.4k
      ) {
8122
2
        zend_error_noreturn(E_COMPILE_ERROR,
8123
2
          "Type %s cannot be part of an intersection type", ZSTR_VAL(ZEND_TYPE_NAME(single_type)));
8124
2
      }
8125
8126
      /* Add type to the type list */
8127
34.4k
      type_list->types[type_list->num_types++] = single_type;
8128
8129
      /* Check for trivially redundant class types */
8130
34.4k
      zend_is_type_list_redundant_by_single_type(type_list, single_type);
8131
34.4k
    }
8132
8133
15.2k
    ZEND_ASSERT(list->children == type_list->num_types);
8134
8135
    /* An implicitly nullable intersection type needs to be converted to a DNF type */
8136
15.2k
    if (force_allow_null) {
8137
98
      zend_type intersection_type = ZEND_TYPE_INIT_NONE(0);
8138
98
      ZEND_TYPE_SET_LIST(intersection_type, type_list);
8139
98
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_INTERSECTION_BIT;
8140
98
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_ARENA_BIT;
8141
8142
98
      zend_type_list *dnf_type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(1));
8143
98
      dnf_type_list->num_types = 1;
8144
98
      dnf_type_list->types[0] = intersection_type;
8145
98
      ZEND_TYPE_SET_LIST(type, dnf_type_list);
8146
      /* Inform that the type list is a DNF type */
8147
98
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
8148
98
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
8149
15.0k
    } else {
8150
15.0k
      ZEND_TYPE_SET_LIST(type, type_list);
8151
      /* Inform that the type list is an intersection type */
8152
15.0k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_INTERSECTION_BIT;
8153
15.0k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
8154
15.0k
    }
8155
501k
  } else {
8156
501k
    type = zend_compile_single_typename(ast);
8157
501k
  }
8158
8159
655k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
8160
8161
655k
  if (type_mask == MAY_BE_ANY && is_marked_nullable) {
8162
9
    zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be marked as nullable since mixed already includes null");
8163
9
  }
8164
8165
655k
  if ((type_mask & MAY_BE_NULL) && is_marked_nullable) {
8166
6
    zend_error_noreturn(E_COMPILE_ERROR, "null cannot be marked as nullable");
8167
6
  }
8168
8169
655k
  if (force_allow_null && !is_marked_nullable && !(type_mask & MAY_BE_NULL)) {
8170
668
    *forced_allow_null = true;
8171
668
  }
8172
8173
655k
  if (is_marked_nullable || force_allow_null) {
8174
4.94k
    ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
8175
4.94k
    type_mask = ZEND_TYPE_PURE_MASK(type);
8176
4.94k
  }
8177
8178
655k
  if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_VOID)) {
8179
12
    zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type");
8180
12
  }
8181
8182
655k
  if ((type_mask & MAY_BE_NEVER) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_NEVER)) {
8183
7
    zend_error_noreturn(E_COMPILE_ERROR, "never can only be used as a standalone type");
8184
7
  }
8185
8186
655k
  ast->attr = orig_ast_attr;
8187
655k
  return type;
8188
655k
}
8189
/* }}} */
8190
8191
static zend_type zend_compile_typename(zend_ast *ast)
8192
104k
{
8193
104k
  bool forced_allow_null;
8194
104k
  return zend_compile_typename_ex(ast, false, &forced_allow_null);
8195
104k
}
8196
8197
/* May convert value from int to float. */
8198
static bool zend_is_valid_default_value(zend_type type, zval *value)
8199
6.26k
{
8200
6.26k
  ZEND_ASSERT(ZEND_TYPE_IS_SET(type));
8201
6.26k
  if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) {
8202
2.13k
    return true;
8203
2.13k
  }
8204
4.12k
  if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) {
8205
    /* Integers are allowed as initializers for floating-point values. */
8206
4.06k
    convert_to_double(value);
8207
4.06k
    return true;
8208
4.06k
  }
8209
65
  return false;
8210
4.12k
}
8211
8212
static void zend_compile_attributes(
8213
  HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target, uint32_t promoted
8214
1.30M
) /* {{{ */ {
8215
1.30M
  zend_attribute *attr;
8216
1.30M
  zend_internal_attribute *config;
8217
8218
1.30M
  const zend_ast_list *list = zend_ast_get_list(ast);
8219
1.30M
  uint32_t g, i, j;
8220
8221
1.30M
  ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE_LIST);
8222
8223
2.61M
  for (g = 0; g < list->children; g++) {
8224
1.30M
    const zend_ast_list *group = zend_ast_get_list(list->child[g]);
8225
8226
1.30M
    ZEND_ASSERT(group->kind == ZEND_AST_ATTRIBUTE_GROUP);
8227
8228
3.41M
    for (i = 0; i < group->children; i++) {
8229
2.10M
      ZEND_ASSERT(group->child[i]->kind == ZEND_AST_ATTRIBUTE);
8230
8231
2.10M
      const zend_ast *el = group->child[i];
8232
8233
2.10M
      if (el->child[1] &&
8234
28.2k
          el->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) {
8235
8
          zend_error_noreturn(E_COMPILE_ERROR,
8236
8
              "Cannot create Closure as attribute argument");
8237
8
      }
8238
8239
2.10M
      zend_string *name = zend_resolve_class_name_ast(el->child[0]);
8240
2.10M
      zend_string *lcname = zend_string_tolower_ex(name, false);
8241
2.10M
      zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL;
8242
8243
2.10M
      config = zend_internal_attribute_get(lcname);
8244
2.10M
      zend_string_release(lcname);
8245
8246
      /* Exclude internal attributes that do not match on promoted properties. */
8247
2.10M
      if (config && !(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
8248
116
        if (promoted & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL)) {
8249
2
          zend_string_release(name);
8250
2
          continue;
8251
2
        }
8252
116
      }
8253
8254
2.10M
      uint32_t flags = (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES)
8255
2.10M
        ? ZEND_ATTRIBUTE_STRICT_TYPES : 0;
8256
2.10M
      attr = zend_add_attribute(
8257
2.10M
        attributes, name, args ? args->children : 0, flags, offset, el->lineno);
8258
2.10M
      zend_string_release(name);
8259
8260
      /* Populate arguments */
8261
2.10M
      if (args) {
8262
28.2k
        ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST);
8263
8264
28.2k
        bool uses_named_args = false;
8265
60.2k
        for (j = 0; j < args->children; j++) {
8266
31.9k
          zend_ast **arg_ast_ptr = &args->child[j];
8267
31.9k
          zend_ast *arg_ast = *arg_ast_ptr;
8268
8269
31.9k
          if (arg_ast->kind == ZEND_AST_UNPACK) {
8270
1
            zend_error_noreturn(E_COMPILE_ERROR,
8271
1
              "Cannot use unpacking in attribute argument list");
8272
1
          }
8273
8274
31.9k
          if (arg_ast->kind == ZEND_AST_NAMED_ARG) {
8275
1.09k
            attr->args[j].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0]));
8276
1.09k
            arg_ast_ptr = &arg_ast->child[1];
8277
1.09k
            uses_named_args = true;
8278
8279
5.96k
            for (uint32_t k = 0; k < j; k++) {
8280
4.87k
              if (attr->args[k].name &&
8281
3.86k
                  zend_string_equals(attr->args[k].name, attr->args[j].name)) {
8282
6
                zend_error_noreturn(E_COMPILE_ERROR, "Duplicate named parameter $%s",
8283
6
                  ZSTR_VAL(attr->args[j].name));
8284
6
              }
8285
4.87k
            }
8286
30.8k
          } else if (uses_named_args) {
8287
28
            zend_error_noreturn(E_COMPILE_ERROR,
8288
28
              "Cannot use positional argument after named argument");
8289
28
          }
8290
8291
31.9k
          zend_const_expr_to_zval(
8292
31.9k
            &attr->args[j].value, arg_ast_ptr, /* allow_dynamic */ true);
8293
31.9k
        }
8294
28.2k
      }
8295
2.10M
    }
8296
1.30M
  }
8297
8298
1.30M
  if (*attributes != NULL) {
8299
    /* Allow delaying target validation for forward compatibility. */
8300
1.30M
    const zend_attribute *delayed_target_validation = NULL;
8301
1.30M
    if (target == ZEND_ATTRIBUTE_TARGET_PARAMETER) {
8302
21.8k
      ZEND_ASSERT(offset >= 1);
8303
      /* zend_get_parameter_attribute_str will add 1 too */
8304
21.8k
      delayed_target_validation = zend_get_parameter_attribute_str(
8305
21.8k
        *attributes,
8306
21.8k
        "delayedtargetvalidation",
8307
21.8k
        strlen("delayedtargetvalidation"),
8308
21.8k
        offset - 1
8309
21.8k
      );
8310
1.28M
    } else {
8311
1.28M
      delayed_target_validation = zend_get_attribute_str(
8312
1.28M
        *attributes,
8313
1.28M
        "delayedtargetvalidation",
8314
1.28M
        strlen("delayedtargetvalidation")
8315
1.28M
      );
8316
1.28M
    }
8317
    /* Validate attributes in a secondary loop (needed to detect repeated attributes). */
8318
7.38M
    ZEND_HASH_PACKED_FOREACH_PTR(*attributes, attr) {
8319
7.38M
      if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) {
8320
2.38M
        continue;
8321
2.38M
      }
8322
8323
7.38M
      bool run_validator = true;
8324
2.96k
      if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
8325
55
        if (delayed_target_validation == NULL) {
8326
24
          zend_string *location = zend_get_attribute_target_names(target);
8327
24
          zend_string *allowed = zend_get_attribute_target_names(config->flags);
8328
8329
24
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
8330
24
            ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
8331
24
          );
8332
24
        }
8333
55
        run_validator = false;
8334
55
      }
8335
8336
2.96k
      if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
8337
2.93k
        if (zend_is_attribute_repeated(*attributes, attr)) {
8338
5
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name));
8339
5
        }
8340
2.93k
      }
8341
8342
      /* Validators are not run if the target is already invalid */
8343
2.96k
      if (run_validator && config->validator != NULL) {
8344
1.94k
        zend_string *error = config->validator(attr, target, CG(active_class_entry));
8345
1.94k
        if (error != NULL) {
8346
369
          if (delayed_target_validation == NULL) {
8347
56
            zend_error_noreturn(E_COMPILE_ERROR, "%s", ZSTR_VAL(error));
8348
313
          } else {
8349
313
            attr->validation_error = error;
8350
313
          }
8351
369
        }
8352
1.94k
      }
8353
2.96k
    } ZEND_HASH_FOREACH_END();
8354
1.30M
  }
8355
1.30M
}
8356
/* }}} */
8357
8358
static void zend_compile_property_hooks(
8359
    zend_property_info *prop_info, zend_string *prop_name,
8360
    zend_ast *prop_type_ast, const zend_ast_list *hooks);
8361
8362
typedef struct {
8363
  const zend_string *property_name;
8364
  bool uses_property;
8365
} find_property_usage_context;
8366
8367
static void zend_property_hook_find_property_usage(zend_ast **ast_ptr, void *_context) /* {{{ */
8368
50.1k
{
8369
50.1k
  zend_ast *ast = *ast_ptr;
8370
50.1k
  find_property_usage_context *context = (find_property_usage_context *) _context;
8371
8372
50.1k
  if (ast == NULL) {
8373
1.39k
    return;
8374
48.7k
  } else if (ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_NULLSAFE_PROP) {
8375
2.83k
    const zend_ast *object_ast = ast->child[0];
8376
2.83k
    zend_ast *property_ast = ast->child[1];
8377
8378
2.83k
    if (object_ast->kind == ZEND_AST_VAR
8379
2.09k
     && object_ast->child[0]->kind == ZEND_AST_ZVAL
8380
1.98k
     && property_ast->kind == ZEND_AST_ZVAL) {
8381
1.78k
      const zval *object = zend_ast_get_zval(object_ast->child[0]);
8382
1.78k
      const zval *property = zend_ast_get_zval(property_ast);
8383
1.78k
      if (Z_TYPE_P(object) == IS_STRING
8384
1.78k
        && Z_TYPE_P(property) == IS_STRING
8385
1.78k
        && zend_string_equals_literal(Z_STR_P(object), "this")
8386
1.10k
        && zend_string_equals(Z_STR_P(property), context->property_name)) {
8387
177
        context->uses_property = true;
8388
        /* No need to look for references in this branch. */
8389
177
        return;
8390
177
      }
8391
1.78k
    }
8392
2.83k
  }
8393
8394
  /* Don't search across function/class boundaries. */
8395
48.6k
  if (!zend_ast_is_special(ast)) {
8396
28.2k
    zend_ast_apply(ast, zend_property_hook_find_property_usage, context);
8397
28.2k
  }
8398
48.6k
}
8399
8400
static bool zend_property_hook_uses_property(const zend_string *property_name, const zend_string *hook_name, zend_ast *hook_ast)
8401
3.32k
{
8402
3.32k
  if (zend_string_equals_literal_ci(hook_name, "set")
8403
1.19k
   && hook_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
8404
538
    return true;
8405
538
  }
8406
8407
2.78k
  find_property_usage_context context = { property_name, false };
8408
2.78k
  zend_property_hook_find_property_usage(&hook_ast, &context);
8409
2.78k
  return context.uses_property;
8410
3.32k
}
8411
8412
static bool zend_property_is_virtual(const zend_class_entry *ce, const zend_string *property_name, zend_ast *hooks_ast)
8413
25.2k
{
8414
25.2k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
8415
384
    return true;
8416
384
  }
8417
24.8k
  if (!hooks_ast) {
8418
21.9k
    return false;
8419
21.9k
  }
8420
8421
24.8k
  bool is_virtual = true;
8422
8423
2.90k
  const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
8424
6.80k
  for (uint32_t i = 0; i < hooks->children; i++) {
8425
3.90k
    const zend_ast_decl *hook = (const zend_ast_decl *) hooks->child[i];
8426
3.90k
    zend_ast *body = hook->child[2];
8427
3.90k
    if (body && zend_property_hook_uses_property(property_name, hook->name, body)) {
8428
637
      is_virtual = false;
8429
637
    }
8430
3.90k
  }
8431
8432
2.90k
  return is_virtual;
8433
24.8k
}
8434
8435
static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */
8436
2.09M
{
8437
2.09M
  zend_ast_list *list = zend_ast_get_list(ast);
8438
2.09M
  uint32_t i;
8439
2.09M
  zend_op_array *op_array = CG(active_op_array);
8440
2.09M
  zend_arg_info *arg_infos;
8441
8442
2.09M
  if (return_type_ast || fallback_return_type) {
8443
    /* Use op_array->arg_info[-1] for return type */
8444
78.4k
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
8445
78.4k
    arg_infos->name = NULL;
8446
78.4k
    if (return_type_ast) {
8447
77.7k
      arg_infos->type = zend_compile_typename(return_type_ast);
8448
77.7k
      ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS(
8449
77.7k
        (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0, /* is_tentative */ 0);
8450
77.7k
    } else {
8451
736
      arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0);
8452
736
    }
8453
78.4k
    arg_infos->doc_comment = NULL;
8454
78.4k
    arg_infos++;
8455
78.4k
    op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
8456
8457
78.4k
    if (ZEND_TYPE_CONTAINS_CODE(arg_infos[-1].type, IS_VOID)
8458
1.97k
        && (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
8459
406
      zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
8460
406
      zend_error(E_DEPRECATED, "%s(): Returning by reference from a void function is deprecated", ZSTR_VAL(func_name));
8461
406
      zend_string_release(func_name);
8462
406
    }
8463
2.01M
  } else {
8464
2.01M
    if (list->children == 0) {
8465
716k
      return;
8466
716k
    }
8467
1.30M
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
8468
1.30M
  }
8469
8470
  /* Find last required parameter number for deprecation message. */
8471
1.38M
  uint32_t last_required_param = (uint32_t) -1;
8472
2.78M
  for (i = 0; i < list->children; ++i) {
8473
1.40M
    zend_ast *param_ast = list->child[i];
8474
1.40M
    zend_ast *default_ast_ptr = param_ast->child[2];
8475
1.40M
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
8476
1.40M
    if (!default_ast_ptr && !is_variadic) {
8477
1.39M
      last_required_param = i;
8478
1.39M
    }
8479
1.40M
  }
8480
8481
1.38M
  const uint32_t promotion_flags = ZEND_ACC_PPP_MASK | ZEND_ACC_PPP_SET_MASK | ZEND_ACC_READONLY | ZEND_ACC_FINAL;
8482
2.78M
  for (i = 0; i < list->children; ++i) {
8483
1.40M
    zend_ast *param_ast = list->child[i];
8484
1.40M
    zend_ast *type_ast = param_ast->child[0];
8485
1.40M
    zend_ast *var_ast = param_ast->child[1];
8486
1.40M
    zend_ast **default_ast_ptr = &param_ast->child[2];
8487
1.40M
    zend_ast *attributes_ast = param_ast->child[3];
8488
1.40M
    zend_ast *doc_comment_ast = param_ast->child[4];
8489
1.40M
    zend_ast *hooks_ast = param_ast->child[5];
8490
1.40M
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast));
8491
1.40M
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
8492
1.40M
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
8493
1.40M
    uint32_t property_flags = param_ast->attr & promotion_flags;
8494
1.40M
    bool is_promoted = property_flags || hooks_ast;
8495
8496
1.40M
    CG(zend_lineno) = param_ast->lineno;
8497
8498
1.40M
    znode var_node, default_node;
8499
1.40M
    uint8_t opcode;
8500
1.40M
    zend_op *opline;
8501
1.40M
    zend_arg_info *arg_info;
8502
8503
1.40M
    if (zend_is_auto_global(name)) {
8504
3
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
8505
3
        ZSTR_VAL(name));
8506
3
    }
8507
8508
1.40M
    var_node.op_type = IS_CV;
8509
1.40M
    var_node.u.op.var = lookup_cv(name);
8510
8511
1.40M
    if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
8512
30
      zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
8513
30
        ZSTR_VAL(name));
8514
1.40M
    } else if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8515
3
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
8516
1.40M
    } else if (zend_string_equals_literal(name, "http_response_header")) {
8517
353
      CG(context).has_assigned_to_http_response_header = true;
8518
353
    }
8519
8520
1.40M
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
8521
3
      zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
8522
3
    }
8523
8524
1.40M
    if (is_variadic) {
8525
1.27k
      opcode = ZEND_RECV_VARIADIC;
8526
1.27k
      default_node.op_type = IS_UNUSED;
8527
1.27k
      op_array->fn_flags |= ZEND_ACC_VARIADIC;
8528
8529
1.27k
      if (*default_ast_ptr) {
8530
4
        zend_error_noreturn(E_COMPILE_ERROR,
8531
4
          "Variadic parameter cannot have a default value");
8532
4
      }
8533
1.40M
    } else if (*default_ast_ptr) {
8534
      /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
8535
7.85k
      uint32_t cops = CG(compiler_options);
8536
7.85k
      CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
8537
7.85k
      opcode = ZEND_RECV_INIT;
8538
7.85k
      default_node.op_type = IS_CONST;
8539
7.85k
      zend_const_expr_to_zval(
8540
7.85k
        &default_node.u.constant, default_ast_ptr, /* allow_dynamic */ true);
8541
7.85k
      CG(compiler_options) = cops;
8542
1.39M
    } else {
8543
1.39M
      opcode = ZEND_RECV;
8544
1.39M
      default_node.op_type = IS_UNUSED;
8545
1.39M
      op_array->required_num_args = i + 1;
8546
1.39M
    }
8547
8548
1.40M
    arg_info = &arg_infos[i];
8549
1.40M
    arg_info->name = zend_string_copy(name);
8550
1.40M
    arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0);
8551
1.40M
    arg_info->default_value = NULL;
8552
1.40M
    arg_info->doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
8553
8554
1.40M
    if (attributes_ast) {
8555
21.8k
      zend_compile_attributes(
8556
21.8k
        &op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER,
8557
21.8k
        is_promoted ? ZEND_ATTRIBUTE_TARGET_PROPERTY : 0
8558
21.8k
      );
8559
21.8k
    }
8560
8561
1.40M
    bool forced_allow_nullable = false;
8562
1.40M
    if (type_ast) {
8563
550k
      uint32_t default_type = *default_ast_ptr ? Z_TYPE(default_node.u.constant) : IS_UNDEF;
8564
550k
      bool force_nullable = default_type == IS_NULL && !is_promoted;
8565
8566
550k
      op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
8567
550k
      arg_info->type = zend_compile_typename_ex(type_ast, force_nullable, &forced_allow_nullable);
8568
550k
      if (forced_allow_nullable) {
8569
668
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
8570
668
        zend_error(E_DEPRECATED,
8571
668
           "%s(): Implicitly marking parameter $%s as nullable is deprecated, the explicit nullable type "
8572
668
           "must be used instead", ZSTR_VAL(func_name), ZSTR_VAL(name));
8573
668
        zend_string_release(func_name);
8574
668
      }
8575
8576
550k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) {
8577
9
        zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");
8578
9
      }
8579
8580
550k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_NEVER) {
8581
9
        zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type");
8582
9
      }
8583
8584
550k
      if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable
8585
1.15k
          && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) {
8586
18
        zend_string *type_str = zend_type_to_string(arg_info->type);
8587
18
        zend_error_noreturn(E_COMPILE_ERROR,
8588
18
          "Cannot use %s as default value for parameter $%s of type %s",
8589
18
          zend_get_type_by_const(default_type),
8590
18
          ZSTR_VAL(name), ZSTR_VAL(type_str));
8591
18
      }
8592
550k
    }
8593
1.40M
    if (last_required_param != (uint32_t) -1
8594
1.39M
     && i < last_required_param
8595
30.5k
     && default_node.op_type == IS_CONST) {
8596
      /* Ignore parameters of the form "Type $param = null".
8597
       * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
8598
2.57k
      if (!forced_allow_nullable) {
8599
2.20k
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
8600
2.20k
        zend_ast *required_param_ast = list->child[last_required_param];
8601
2.20k
        zend_error(E_DEPRECATED,
8602
2.20k
          "%s(): Optional parameter $%s declared before required parameter $%s "
8603
2.20k
          "is implicitly treated as a required parameter",
8604
2.20k
          ZSTR_VAL(func_name), ZSTR_VAL(name), ZSTR_VAL(zend_ast_get_str(required_param_ast->child[1])));
8605
2.20k
        zend_string_release(func_name);
8606
2.20k
      }
8607
8608
      /* Regardless of whether we issue a deprecation, convert this parameter into
8609
       * a required parameter without a default value. This ensures that it cannot be
8610
       * used as an optional parameter even with named parameters. */
8611
2.57k
      opcode = ZEND_RECV;
8612
2.57k
      default_node.op_type = IS_UNUSED;
8613
2.57k
      zval_ptr_dtor(&default_node.u.constant);
8614
2.57k
    }
8615
8616
1.40M
    opline = zend_emit_op(NULL, opcode, NULL, &default_node);
8617
1.40M
    SET_NODE(opline->result, &var_node);
8618
1.40M
    opline->op1.num = i + 1;
8619
8620
1.40M
    uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic, /* is_tentative */ 0)
8621
1.40M
      | (is_promoted ? _ZEND_IS_PROMOTED_BIT : 0);
8622
1.40M
    ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags;
8623
1.40M
    if (opcode == ZEND_RECV) {
8624
1.39M
      opline->op2.num = type_ast ?
8625
850k
        ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY;
8626
1.39M
    }
8627
8628
1.40M
    if (is_promoted) {
8629
1.92k
      const zend_op_array *active_op_array = CG(active_op_array);
8630
1.92k
      zend_class_entry *scope = active_op_array->scope;
8631
8632
1.92k
      bool is_ctor =
8633
1.92k
        scope && zend_is_constructor(active_op_array->function_name);
8634
1.92k
      if (!is_ctor) {
8635
27
        zend_error_noreturn(E_COMPILE_ERROR,
8636
27
          "Cannot declare promoted property outside a constructor");
8637
27
      }
8638
1.92k
      if ((active_op_array->fn_flags & ZEND_ACC_ABSTRACT)
8639
1.90k
          || (scope->ce_flags & ZEND_ACC_INTERFACE)) {
8640
1
        zend_error_noreturn(E_COMPILE_ERROR,
8641
1
          "Cannot declare promoted property in an abstract constructor");
8642
1
      }
8643
1.92k
      if (is_variadic) {
8644
1
        zend_error_noreturn(E_COMPILE_ERROR,
8645
1
          "Cannot declare variadic promoted property");
8646
1
      }
8647
1.92k
      if (zend_hash_exists(&scope->properties_info, name)) {
8648
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
8649
1
          ZSTR_VAL(scope->name), ZSTR_VAL(name));
8650
1
      }
8651
1.92k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_CALLABLE) {
8652
1
        zend_string *str = zend_type_to_string(arg_info->type);
8653
1
        zend_error_noreturn(E_COMPILE_ERROR,
8654
1
          "Property %s::$%s cannot have type %s",
8655
1
          ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str));
8656
1
      }
8657
8658
1.92k
      if (!(property_flags & ZEND_ACC_READONLY) && (scope->ce_flags & ZEND_ACC_READONLY_CLASS)) {
8659
3
        property_flags |= ZEND_ACC_READONLY;
8660
3
      }
8661
8662
      /* Recompile the type, as it has different memory management requirements. */
8663
1.92k
      zend_type type = ZEND_TYPE_INIT_NONE(0);
8664
1.92k
      if (type_ast) {
8665
775
        type = zend_compile_typename(type_ast);
8666
775
      }
8667
8668
      /* Don't give the property an explicit default value. For typed properties this means
8669
       * uninitialized, for untyped properties it means an implicit null default value.
8670
       * Properties with hooks get an implicit default value of undefined until inheritance,
8671
       * where it is changed to null only once we know it is not virtual. If we were to set it
8672
       * here, we couldn't verify that a true virtual property must not have an explicit
8673
       * default value. */
8674
1.92k
      zval default_value;
8675
1.92k
      if (ZEND_TYPE_IS_SET(type) || hooks_ast) {
8676
868
        ZVAL_UNDEF(&default_value);
8677
1.06k
      } else {
8678
1.06k
        if (property_flags & ZEND_ACC_READONLY) {
8679
1
          zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
8680
1
            ZSTR_VAL(scope->name), ZSTR_VAL(name));
8681
1
        }
8682
8683
1.06k
        ZVAL_NULL(&default_value);
8684
1.06k
      }
8685
8686
1.92k
      zend_string *doc_comment =
8687
1.92k
        doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
8688
1.92k
      zend_property_info *prop = zend_declare_typed_property(
8689
1.92k
        scope, name, &default_value,
8690
1.92k
        property_flags | (zend_property_is_virtual(scope, name, hooks_ast) ? ZEND_ACC_VIRTUAL : 0) | ZEND_ACC_PROMOTED,
8691
1.92k
        doc_comment, type);
8692
1.92k
      if (hooks_ast) {
8693
166
        const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
8694
166
        zend_compile_property_hooks(prop, name, type_ast, hooks);
8695
166
      }
8696
1.92k
      if (attributes_ast) {
8697
226
        zend_compile_attributes(
8698
226
          &prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, ZEND_ATTRIBUTE_TARGET_PARAMETER);
8699
8700
226
        zend_attribute *override_attribute = zend_get_attribute_str(prop->attributes, "override", sizeof("override")-1);
8701
226
        if (override_attribute) {
8702
1
          prop->flags |= ZEND_ACC_OVERRIDE;
8703
1
        }
8704
226
      }
8705
1.92k
    }
8706
1.40M
  }
8707
8708
  /* These are assigned at the end to avoid uninitialized memory in case of an error */
8709
1.38M
  op_array->num_args = list->children;
8710
1.38M
  op_array->arg_info = arg_infos;
8711
8712
  /* Don't count the variadic argument */
8713
1.38M
  if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
8714
1.26k
    op_array->num_args--;
8715
1.26k
  }
8716
1.38M
  zend_set_function_arg_flags((zend_function*)op_array);
8717
8718
2.78M
  for (i = 0; i < list->children; i++) {
8719
1.40M
    zend_ast *param_ast = list->child[i];
8720
1.40M
    zend_ast *hooks_ast = param_ast->child[5];
8721
1.40M
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
8722
1.40M
    uint32_t flags = param_ast->attr & promotion_flags;
8723
1.40M
    bool is_promoted = flags || hooks_ast;
8724
1.40M
    if (!is_promoted) {
8725
1.40M
      continue;
8726
1.40M
    }
8727
8728
1.89k
    CG(zend_lineno) = param_ast->lineno;
8729
8730
    /* Emit $this->prop = $prop for promoted properties. */
8731
1.89k
    zend_string *name = zend_ast_get_str(param_ast->child[1]);
8732
1.89k
    znode name_node, value_node;
8733
1.89k
    name_node.op_type = IS_CONST;
8734
1.89k
    ZVAL_STR_COPY(&name_node.u.constant, name);
8735
1.89k
    value_node.op_type = IS_CV;
8736
1.89k
    value_node.u.op.var = lookup_cv(name);
8737
8738
1.89k
    zend_op *opline = zend_emit_op(NULL,
8739
1.89k
      is_ref ? ZEND_ASSIGN_OBJ_REF : ZEND_ASSIGN_OBJ, NULL, &name_node);
8740
1.89k
    opline->extended_value = zend_alloc_cache_slots(3);
8741
1.89k
    zend_emit_op_data(&value_node);
8742
1.89k
  }
8743
1.38M
}
8744
/* }}} */
8745
8746
static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */
8747
1.40k
{
8748
1.40k
  const zend_ast_list *list = zend_ast_get_list(uses_ast);
8749
1.40k
  uint32_t i;
8750
8751
1.40k
  if (!list->children) {
8752
23
    return;
8753
23
  }
8754
8755
1.38k
  if (!op_array->static_variables) {
8756
1.38k
    op_array->static_variables = zend_new_array(8);
8757
1.38k
  }
8758
8759
6.60k
  for (i = 0; i < list->children; ++i) {
8760
5.22k
    zend_ast *var_name_ast = list->child[i];
8761
5.22k
    zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast));
8762
5.22k
    uint32_t mode = var_name_ast->attr;
8763
5.22k
    zend_op *opline;
8764
5.22k
    zval *value;
8765
8766
5.22k
    if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8767
1
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
8768
1
    }
8769
8770
5.22k
    if (zend_is_auto_global(var_name)) {
8771
1
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable");
8772
1
    }
8773
8774
5.22k
    value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval));
8775
5.22k
    if (!value) {
8776
8
      zend_error_noreturn(E_COMPILE_ERROR,
8777
8
        "Cannot use variable $%pS twice", var_name);
8778
8
    }
8779
8780
5.22k
    CG(zend_lineno) = zend_ast_get_lineno(var_name_ast);
8781
8782
5.22k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8783
5.22k
    opline->op2_type = IS_CV;
8784
5.22k
    opline->op2.var = lookup_cv(var_name);
8785
5.22k
    opline->extended_value =
8786
5.22k
      (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode;
8787
5.22k
  }
8788
1.38k
}
8789
/* }}} */
8790
8791
typedef struct {
8792
  HashTable uses;
8793
  bool varvars_used;
8794
} closure_info;
8795
8796
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast);
8797
8798
1.02M
static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
8799
1.02M
  if (!ast) {
8800
9.16k
    return;
8801
9.16k
  }
8802
8803
1.01M
  if (ast->kind == ZEND_AST_VAR) {
8804
118k
    zend_ast *name_ast = ast->child[0];
8805
118k
    if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) {
8806
115k
      zend_string *name = zend_ast_get_str(name_ast);
8807
115k
      if (zend_is_auto_global(name)) {
8808
        /* These is no need to explicitly import auto-globals. */
8809
1.09k
        return;
8810
1.09k
      }
8811
8812
114k
      if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8813
        /* $this does not need to be explicitly imported. */
8814
13.7k
        return;
8815
13.7k
      }
8816
8817
100k
      zend_hash_add_empty_element(&info->uses, name);
8818
100k
    } else {
8819
2.57k
      info->varvars_used = true;
8820
2.57k
      find_implicit_binds_recursively(info, name_ast);
8821
2.57k
    }
8822
897k
  } else if (zend_ast_is_list(ast)) {
8823
36.4k
    const zend_ast_list *list = zend_ast_get_list(ast);
8824
36.4k
    uint32_t i;
8825
218k
    for (i = 0; i < list->children; i++) {
8826
182k
      find_implicit_binds_recursively(info, list->child[i]);
8827
182k
    }
8828
861k
  } else if (ast->kind == ZEND_AST_CLOSURE) {
8829
    /* For normal closures add the use() list. */
8830
333
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8831
333
    zend_ast *uses_ast = closure_ast->child[1];
8832
333
    if (uses_ast) {
8833
134
      const zend_ast_list *uses_list = zend_ast_get_list(uses_ast);
8834
134
      uint32_t i;
8835
903
      for (i = 0; i < uses_list->children; i++) {
8836
769
        zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i]));
8837
769
      }
8838
134
    }
8839
860k
  } else if (ast->kind == ZEND_AST_ARROW_FUNC) {
8840
    /* For arrow functions recursively check the expression. */
8841
309k
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8842
309k
    closure_info inner_info;
8843
309k
    find_implicit_binds(&inner_info, closure_ast->child[0], closure_ast->child[2]);
8844
309k
    if (inner_info.varvars_used) {
8845
1.92k
      info->varvars_used = true;
8846
1.92k
    }
8847
309k
    if (zend_hash_num_elements(&inner_info.uses)) {
8848
270k
      zend_hash_copy(&info->uses, &inner_info.uses, NULL);
8849
270k
    }
8850
309k
    zend_hash_destroy(&inner_info.uses);
8851
550k
  } else if (!zend_ast_is_special(ast)) {
8852
393k
    uint32_t i, children = zend_ast_get_num_children(ast);
8853
890k
    for (i = 0; i < children; i++) {
8854
497k
      find_implicit_binds_recursively(info, ast->child[i]);
8855
497k
    }
8856
393k
  }
8857
1.01M
}
8858
8859
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast)
8860
342k
{
8861
342k
  const zend_ast_list *param_list = zend_ast_get_list(params_ast);
8862
342k
  uint32_t i;
8863
8864
342k
  zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0);
8865
342k
  info->varvars_used = false;
8866
8867
342k
  find_implicit_binds_recursively(info, stmt_ast);
8868
8869
  /* Remove variables that are parameters */
8870
399k
  for (i = 0; i < param_list->children; i++) {
8871
57.0k
    const zend_ast *param_ast = param_list->child[i];
8872
57.0k
    zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1]));
8873
57.0k
  }
8874
342k
}
8875
8876
static void compile_implicit_lexical_binds(
8877
    const closure_info *info, znode *closure, zend_op_array *op_array)
8878
32.9k
{
8879
32.9k
  zend_string *var_name;
8880
32.9k
  zend_op *opline;
8881
8882
  /* TODO We might want to use a special binding mode if varvars_used is set. */
8883
32.9k
  if (zend_hash_num_elements(&info->uses) == 0) {
8884
23.4k
    return;
8885
23.4k
  }
8886
8887
9.50k
  if (!op_array->static_variables) {
8888
9.50k
    op_array->static_variables = zend_new_array(8);
8889
9.50k
  }
8890
8891
126k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name) {
8892
126k
    zval *value = zend_hash_add(
8893
126k
      op_array->static_variables, var_name, &EG(uninitialized_zval));
8894
126k
    uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData);
8895
8896
126k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8897
126k
    opline->op2_type = IS_CV;
8898
126k
    opline->op2.var = lookup_cv(var_name);
8899
126k
    opline->extended_value = offset | ZEND_BIND_IMPLICIT;
8900
126k
  } ZEND_HASH_FOREACH_END();
8901
9.50k
}
8902
8903
static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
8904
1.39k
{
8905
1.39k
  const zend_op_array *op_array = CG(active_op_array);
8906
1.39k
  const zend_ast_list *list = zend_ast_get_list(ast);
8907
1.39k
  uint32_t i;
8908
8909
6.57k
  for (i = 0; i < list->children; ++i) {
8910
5.17k
    uint32_t mode = ZEND_BIND_EXPLICIT;
8911
5.17k
    zend_ast *var_ast = list->child[i];
8912
5.17k
    zend_string *var_name = zend_ast_get_str(var_ast);
8913
5.17k
    zval zv;
8914
5.17k
    ZVAL_NULL(&zv);
8915
8916
5.17k
    {
8917
5.17k
      int i;
8918
16.2k
      for (i = 0; i < op_array->last_var; i++) {
8919
11.0k
        if (zend_string_equals(op_array->vars[i], var_name)) {
8920
1
          zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8921
1
            "Cannot use lexical variable $%S as a parameter name", var_name);
8922
1
        }
8923
11.0k
      }
8924
5.17k
    }
8925
8926
5.17k
    CG(zend_lineno) = zend_ast_get_lineno(var_ast);
8927
8928
5.17k
    if (var_ast->attr) {
8929
3.71k
      mode |= ZEND_BIND_REF;
8930
3.71k
    }
8931
8932
5.17k
    zend_compile_static_var_common(var_name, &zv, mode);
8933
5.17k
  }
8934
1.39k
}
8935
/* }}} */
8936
8937
static void zend_compile_implicit_closure_uses(const closure_info *info)
8938
32.8k
{
8939
32.8k
  zend_string *var_name;
8940
173k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name) {
8941
173k
    zval zv;
8942
173k
    ZVAL_NULL(&zv);
8943
173k
    zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT);
8944
173k
  } ZEND_HASH_FOREACH_END();
8945
32.8k
}
8946
8947
797
static void add_stringable_interface(zend_class_entry *ce) {
8948
1.57k
  for (uint32_t i = 0; i < ce->num_interfaces; i++) {
8949
813
    if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) {
8950
      /* Interface already explicitly implemented */
8951
40
      return;
8952
40
    }
8953
813
  }
8954
8955
757
  ce->num_interfaces++;
8956
757
  ce->interface_names =
8957
757
    erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
8958
  // TODO: Add known interned strings instead?
8959
757
  ce->interface_names[ce->num_interfaces - 1].name =
8960
757
    ZSTR_INIT_LITERAL("Stringable", 0);
8961
757
  ce->interface_names[ce->num_interfaces - 1].lc_name =
8962
757
    ZSTR_INIT_LITERAL("stringable", 0);
8963
757
}
8964
8965
static zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, bool has_body) /* {{{ */
8966
110k
{
8967
110k
  zend_class_entry *ce = CG(active_class_entry);
8968
110k
  bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
8969
110k
  uint32_t fn_flags = op_array->fn_flags;
8970
8971
110k
  zend_string *lcname;
8972
8973
110k
  if (fn_flags & ZEND_ACC_READONLY) {
8974
0
    zend_error(E_COMPILE_ERROR, "Cannot use 'readonly' as method modifier");
8975
0
  }
8976
8977
110k
  if ((fn_flags & ZEND_ACC_PRIVATE) && (fn_flags & ZEND_ACC_FINAL) && !zend_is_constructor(name)) {
8978
9.66k
    zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes");
8979
9.66k
  }
8980
8981
110k
  if ((fn_flags & ZEND_ACC_ABSTRACT)
8982
862
   && !(ce->ce_flags & (ZEND_ACC_EXPLICIT_ABSTRACT_CLASS|ZEND_ACC_TRAIT))) {
8983
    // Don't say that the class should be declared abstract if it is
8984
    // anonymous or an enum and can't be abstract
8985
18
    if (ce->ce_flags & ZEND_ACC_ANON_CLASS) {
8986
1
      zend_error_noreturn(E_COMPILE_ERROR, "Anonymous class method %s() must not be abstract",
8987
1
        ZSTR_VAL(name));
8988
17
    } else if (ce->ce_flags & (ZEND_ACC_ENUM|ZEND_ACC_INTERFACE)) {
8989
16
      zend_error_noreturn(E_COMPILE_ERROR, "%s method %s::%s() must not be abstract",
8990
16
        zend_get_object_type_case(ce, true), ZSTR_VAL(ce->name), ZSTR_VAL(name));
8991
16
    } else {
8992
1
      zend_error_noreturn(E_COMPILE_ERROR, "Class %s declares abstract method %s() and must therefore be declared abstract",
8993
1
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
8994
1
    }
8995
18
  }
8996
8997
110k
  if (in_interface) {
8998
2.19k
    if (!(fn_flags & ZEND_ACC_PUBLIC)) {
8999
1
      zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
9000
1
        "%s::%s() must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
9001
1
    }
9002
2.19k
    if (fn_flags & ZEND_ACC_FINAL) {
9003
1
      zend_error_noreturn(E_COMPILE_ERROR, "Interface method "
9004
1
        "%s::%s() must not be final", ZSTR_VAL(ce->name), ZSTR_VAL(name));
9005
1
    }
9006
2.19k
    op_array->fn_flags |= ZEND_ACC_ABSTRACT;
9007
2.19k
  }
9008
9009
110k
  if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
9010
3.03k
    if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
9011
1
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
9012
1
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
9013
1
    }
9014
9015
3.03k
    if (has_body) {
9016
1
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
9017
1
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
9018
1
    }
9019
9020
3.03k
    ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
9021
107k
  } else if (!has_body) {
9022
1
    zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
9023
1
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
9024
1
  }
9025
9026
110k
  op_array->scope = ce;
9027
110k
  op_array->function_name = zend_string_copy(name);
9028
9029
110k
  lcname = zend_string_tolower(name);
9030
110k
  lcname = zend_new_interned_string(lcname);
9031
9032
110k
  if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
9033
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
9034
7
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
9035
7
  }
9036
9037
110k
  zend_add_magic_method(ce, (zend_function *) op_array, lcname);
9038
110k
  if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_LCNAME)
9039
1.00k
      && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
9040
797
    add_stringable_interface(ce);
9041
797
  }
9042
9043
110k
  return lcname;
9044
110k
}
9045
/* }}} */
9046
9047
1.98M
static uint32_t zend_add_dynamic_func_def(zend_op_array *def) {
9048
1.98M
  zend_op_array *op_array = CG(active_op_array);
9049
1.98M
  uint32_t def_offset = op_array->num_dynamic_func_defs++;
9050
1.98M
  op_array->dynamic_func_defs = erealloc(
9051
1.98M
    op_array->dynamic_func_defs, op_array->num_dynamic_func_defs * sizeof(zend_op_array *));
9052
1.98M
  op_array->dynamic_func_defs[def_offset] = def;
9053
1.98M
  return def_offset;
9054
1.98M
}
9055
9056
enum func_decl_level {
9057
  FUNC_DECL_LEVEL_TOPLEVEL,
9058
  FUNC_DECL_LEVEL_NESTED,
9059
  FUNC_DECL_LEVEL_CONSTEXPR,
9060
};
9061
9062
static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array, const zend_ast_decl *decl, enum func_decl_level level) /* {{{ */
9063
1.98M
{
9064
1.98M
  zend_string *unqualified_name, *name, *lcname;
9065
1.98M
  zend_op *opline;
9066
9067
1.98M
  if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
9068
1.97M
    zend_string *filename = op_array->filename;
9069
1.97M
    uint32_t start_lineno = decl->start_lineno;
9070
9071
1.97M
    zend_string *class = zend_empty_string;
9072
1.97M
    zend_string *separator = zend_empty_string;
9073
1.97M
    zend_string *function = filename;
9074
1.97M
    const char *parens = "";
9075
9076
1.97M
    if (CG(active_op_array) && CG(active_op_array)->function_name) {
9077
1.96M
      if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
9078
        /* If the parent function is a closure, don't redundantly
9079
         * add the classname and parentheses.
9080
         */
9081
1.96M
        function = CG(active_op_array)->function_name;
9082
1.96M
      } else {
9083
785
        function = CG(active_op_array)->function_name;
9084
785
        parens = "()";
9085
9086
785
        if (CG(active_class_entry) && CG(active_class_entry)->name) {
9087
597
          class = CG(active_class_entry)->name;
9088
597
          separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM);
9089
597
        }
9090
785
      }
9091
1.96M
    }
9092
9093
1.97M
    unqualified_name = zend_strpprintf_unchecked(
9094
1.97M
      0,
9095
1.97M
      "{closure:%S%S%S%s:%" PRIu32 "}",
9096
1.97M
      class,
9097
1.97M
      separator,
9098
1.97M
      function,
9099
1.97M
      parens,
9100
1.97M
      start_lineno
9101
1.97M
    );
9102
9103
1.97M
    op_array->function_name = name = unqualified_name;
9104
1.97M
  } else {
9105
6.52k
    unqualified_name = decl->name;
9106
6.52k
    op_array->function_name = name = zend_prefix_with_ns(unqualified_name);
9107
6.52k
  }
9108
9109
1.98M
  lcname = zend_string_tolower(name);
9110
9111
1.98M
  if (FC(imports_function)) {
9112
587
    const zend_string *import_name =
9113
587
      zend_hash_find_ptr_lc(FC(imports_function), unqualified_name);
9114
587
    if (import_name && !zend_string_equals_ci(lcname, import_name)) {
9115
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare function %s() (previously declared as local import)",
9116
2
        ZSTR_VAL(name));
9117
2
    }
9118
587
  }
9119
9120
1.98M
  if (zend_string_equals_literal(lcname, "__autoload")) {
9121
1
    zend_error_noreturn(E_COMPILE_ERROR,
9122
1
      "__autoload() is no longer supported, use spl_autoload_register() instead");
9123
1
  }
9124
9125
1.98M
  if (
9126
1.98M
    zend_string_equals_literal_ci(unqualified_name, "readonly")
9127
1.98M
    || zend_string_equals_literal_ci(unqualified_name, "let")
9128
1.98M
    || zend_string_equals_literal_ci(unqualified_name, "is")
9129
1.98M
  ) {
9130
23
    zend_error(E_DEPRECATED, "Calling a function \"%pS\" is deprecated since 8.6", unqualified_name);
9131
23
  }
9132
9133
1.98M
  if (zend_string_equals_literal_ci(unqualified_name, "assert")) {
9134
1
    zend_error(E_COMPILE_ERROR,
9135
1
      "Defining a custom assert() function is not allowed, "
9136
1
      "as the function has special semantics");
9137
1
  }
9138
9139
1.98M
  zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
9140
1.98M
  switch (level) {
9141
1.98M
    case FUNC_DECL_LEVEL_NESTED: {
9142
1.98M
      uint32_t func_ref = zend_add_dynamic_func_def(op_array);
9143
1.98M
      if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
9144
1.97M
        opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
9145
1.97M
        opline->op2.num = func_ref;
9146
1.97M
        opline->extended_value = (uint32_t)-1;
9147
1.97M
      } else {
9148
4.77k
        opline = get_next_op();
9149
4.77k
        opline->opcode = ZEND_DECLARE_FUNCTION;
9150
4.77k
        opline->op1_type = IS_CONST;
9151
4.77k
        LITERAL_STR(opline->op1, zend_string_copy(lcname));
9152
4.77k
        opline->op2.num = func_ref;
9153
4.77k
      }
9154
1.98M
      break;
9155
0
    }
9156
202
    case FUNC_DECL_LEVEL_CONSTEXPR:
9157
1.94k
    case FUNC_DECL_LEVEL_TOPLEVEL:
9158
      /* Nothing to do. */
9159
1.94k
      break;
9160
1.98M
  }
9161
1.98M
  return lcname;
9162
1.98M
}
9163
/* }}} */
9164
9165
static zend_op_array *zend_compile_func_decl_ex(
9166
  znode *result, zend_ast *ast, enum func_decl_level level,
9167
  zend_string *property_info_name,
9168
  zend_property_hook_kind hook_kind
9169
2.09M
) {
9170
2.09M
  zend_ast_decl *decl = (zend_ast_decl *) ast;
9171
2.09M
  zend_ast *params_ast = decl->child[0];
9172
2.09M
  zend_ast *uses_ast = decl->child[1];
9173
2.09M
  zend_ast *stmt_ast = decl->child[2];
9174
2.09M
  zend_ast *return_type_ast = decl->child[3];
9175
2.09M
  bool is_method = decl->kind == ZEND_AST_METHOD;
9176
2.09M
  zend_string *lcname = NULL;
9177
2.09M
  bool is_hook = decl->kind == ZEND_AST_PROPERTY_HOOK;
9178
9179
2.09M
  zend_class_entry *orig_class_entry = CG(active_class_entry);
9180
2.09M
  zend_op_array *orig_op_array = CG(active_op_array);
9181
2.09M
  zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
9182
2.09M
  zend_oparray_context orig_oparray_context;
9183
2.09M
  closure_info info;
9184
9185
2.09M
  init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
9186
9187
2.09M
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
9188
0
    op_array->fn_flags |= ZEND_ACC_PRELOADED;
9189
0
  }
9190
9191
2.09M
  op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
9192
2.09M
  op_array->fn_flags |= decl->flags;
9193
2.09M
  op_array->line_start = decl->start_lineno;
9194
2.09M
  op_array->line_end = decl->end_lineno;
9195
2.09M
  if (decl->doc_comment) {
9196
327
    op_array->doc_comment = zend_string_copy(decl->doc_comment);
9197
327
  }
9198
9199
2.09M
  if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
9200
1.97M
    op_array->fn_flags |= ZEND_ACC_CLOSURE;
9201
1.97M
  }
9202
9203
2.09M
  if (is_hook) {
9204
3.35k
    zend_class_entry *ce = CG(active_class_entry);
9205
3.35k
    op_array->scope = ce;
9206
3.35k
    op_array->function_name = zend_string_copy(decl->name);
9207
2.09M
  } else if (is_method) {
9208
110k
    bool has_body = stmt_ast != NULL;
9209
110k
    lcname = zend_begin_method_decl(op_array, decl->name, has_body);
9210
1.98M
  } else {
9211
1.98M
    lcname = zend_begin_func_decl(result, op_array, decl, level);
9212
1.98M
    if (decl->kind == ZEND_AST_ARROW_FUNC) {
9213
32.9k
      find_implicit_binds(&info, params_ast, stmt_ast);
9214
32.9k
      compile_implicit_lexical_binds(&info, result, op_array);
9215
1.95M
    } else if (uses_ast) {
9216
1.40k
      zend_compile_closure_binding(result, op_array, uses_ast);
9217
1.40k
    }
9218
1.98M
  }
9219
9220
2.09M
  CG(active_op_array) = op_array;
9221
9222
2.09M
  zend_oparray_context_begin(&orig_oparray_context, op_array);
9223
2.09M
  CG(context).active_property_info_name = property_info_name;
9224
2.09M
  CG(context).active_property_hook_kind = hook_kind;
9225
9226
2.09M
  if (decl->child[4]) {
9227
1.27M
    int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
9228
9229
1.27M
    if (is_method || is_hook) {
9230
691
      target = ZEND_ATTRIBUTE_TARGET_METHOD;
9231
691
    }
9232
9233
1.27M
    zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target, 0);
9234
9235
1.27M
    const zend_attribute *override_attribute = zend_get_attribute_str(
9236
1.27M
      op_array->attributes,
9237
1.27M
      "override",
9238
1.27M
      sizeof("override")-1
9239
1.27M
    );
9240
9241
1.27M
    if (override_attribute) {
9242
215
      op_array->fn_flags |= ZEND_ACC_OVERRIDE;
9243
215
    }
9244
9245
1.27M
    const zend_attribute *deprecated_attribute = zend_get_attribute_str(
9246
1.27M
      op_array->attributes,
9247
1.27M
      "deprecated",
9248
1.27M
      sizeof("deprecated")-1
9249
1.27M
    );
9250
9251
1.27M
    if (deprecated_attribute) {
9252
21
      op_array->fn_flags |= ZEND_ACC_DEPRECATED;
9253
21
    }
9254
9255
    // ZEND_ACC_NODISCARD is added via an attribute validator
9256
1.27M
  }
9257
9258
  /* Do not leak the class scope into free standing functions, even if they are dynamically
9259
   * defined inside a class method. This is necessary for correct handling of magic constants.
9260
   * For example __CLASS__ should always be "" inside a free standing function. */
9261
2.09M
  if (decl->kind == ZEND_AST_FUNC_DECL) {
9262
6.51k
    CG(active_class_entry) = NULL;
9263
6.51k
  }
9264
9265
2.09M
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
9266
1.73k
    op_array->fn_flags |= ZEND_ACC_TOP_LEVEL;
9267
1.73k
  }
9268
9269
2.09M
  {
9270
    /* Push a separator to the loop variable stack */
9271
2.09M
    zend_loop_var dummy_var;
9272
2.09M
    dummy_var.opcode = ZEND_RETURN;
9273
9274
2.09M
    zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
9275
2.09M
  }
9276
9277
2.09M
  zend_compile_params(params_ast, return_type_ast,
9278
2.09M
    is_method && zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_LCNAME) ? IS_STRING : 0);
9279
2.09M
  if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
9280
409k
    if (CG(active_class_entry) != NULL) {
9281
762
      if (zend_is_constructor(CG(active_op_array)->function_name)) {
9282
73
        zend_error(E_DEPRECATED, "Making a constructor a Generator is deprecated");
9283
689
      } else if (zend_string_equals_literal_ci(CG(active_op_array)->function_name, ZEND_DESTRUCTOR_FUNC_NAME)) {
9284
2
        zend_error(E_DEPRECATED, "Making a destructor a Generator is deprecated");
9285
2
      }
9286
762
    }
9287
9288
409k
    zend_mark_function_as_generator();
9289
409k
    zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
9290
409k
  }
9291
2.09M
  if (decl->kind == ZEND_AST_ARROW_FUNC) {
9292
32.8k
    zend_compile_implicit_closure_uses(&info);
9293
32.8k
    zend_hash_destroy(&info.uses);
9294
2.06M
  } else if (uses_ast) {
9295
1.39k
    zend_compile_closure_uses(uses_ast);
9296
1.39k
  }
9297
9298
2.09M
  if (ast->kind == ZEND_AST_ARROW_FUNC && decl->child[2]->kind != ZEND_AST_RETURN) {
9299
13.5k
    bool needs_return = true;
9300
13.5k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
9301
2.46k
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
9302
2.46k
      needs_return = !ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER);
9303
2.46k
    }
9304
13.5k
    if (needs_return) {
9305
12.9k
      stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
9306
12.9k
      decl->child[2] = stmt_ast;
9307
12.9k
    }
9308
13.5k
  }
9309
9310
2.09M
  if (op_array->fn_flags & ZEND_ACC_NODISCARD) {
9311
    /* ZEND_ACC_NODISCARD gets added by the attribute validator, but only
9312
     * if the method is not a hook; if it is a hook, then the validator
9313
     * will have returned an error message, even if the error message was
9314
     * delayed with #[\DelayedTargetValidation] that ZEND_ACC_NODISCARD
9315
     * flag should not have been added. */
9316
560
    ZEND_ASSERT(!is_hook);
9317
9318
560
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
9319
421
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
9320
421
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_VOID)) {
9321
1
        zend_error_noreturn(E_COMPILE_ERROR,
9322
1
          "A void %s does not return a value, but #[\\NoDiscard] requires a return value",
9323
1
          CG(active_class_entry) != NULL ? "method" : "function");
9324
1
      }
9325
9326
421
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
9327
1
        zend_error_noreturn(E_COMPILE_ERROR,
9328
1
          "A never returning %s does not return a value, but #[\\NoDiscard] requires a return value",
9329
1
          CG(active_class_entry) != NULL ? "method" : "function");
9330
1
      }
9331
421
    }
9332
560
  }
9333
9334
2.09M
  zend_compile_stmt(stmt_ast);
9335
9336
2.09M
  if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
9337
1.97M
    zend_op_array *declaring_op_array = orig_oparray_context.op_array;
9338
9339
1.97M
    if ((op_array->fn_flags & ZEND_ACC_STATIC)
9340
505
     && !op_array->static_variables
9341
     /* Don't cache closures in main, as those would leak without a proper
9342
      * cleanup mechanism. */
9343
470
     && declaring_op_array->function_name
9344
274
     && declaring_op_array->last) {
9345
239
      zend_op *declare_lambda_op = &declaring_op_array->opcodes[declaring_op_array->last - 1];
9346
239
      if (declare_lambda_op->opcode == ZEND_DECLARE_LAMBDA_FUNCTION) {
9347
239
        declare_lambda_op->extended_value = declaring_op_array->cache_size;
9348
239
        declaring_op_array->cache_size += sizeof(void *);
9349
239
      }
9350
239
    }
9351
1.97M
  }
9352
9353
2.09M
  if (is_method) {
9354
110k
    CG(zend_lineno) = decl->start_lineno;
9355
110k
    zend_check_magic_method_implementation(
9356
110k
      CG(active_class_entry), (zend_function *) op_array, lcname, E_COMPILE_ERROR);
9357
1.98M
  } else if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
9358
    /* Only register the function after a successful compile */
9359
1.52k
    if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
9360
50
      CG(zend_lineno) = decl->start_lineno;
9361
50
      do_bind_function_error(lcname, op_array, true);
9362
50
    }
9363
1.52k
  }
9364
9365
  /* put the implicit return on the really last line */
9366
2.09M
  CG(zend_lineno) = decl->end_lineno;
9367
9368
2.09M
  zend_do_extended_stmt(NULL);
9369
2.09M
  zend_emit_final_return(false);
9370
9371
2.09M
  pass_two(CG(active_op_array));
9372
2.09M
  zend_oparray_context_end(&orig_oparray_context);
9373
9374
  /* Pop the loop variable stack separator */
9375
2.09M
  zend_stack_del_top(&CG(loop_var_stack));
9376
9377
2.09M
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
9378
1.46k
    zend_observer_function_declared_notify(op_array, lcname);
9379
1.46k
  }
9380
9381
2.09M
  if (lcname != NULL) {
9382
2.09M
    zend_string_release_ex(lcname, 0);
9383
2.09M
  }
9384
9385
2.09M
  CG(active_op_array) = orig_op_array;
9386
2.09M
  CG(active_class_entry) = orig_class_entry;
9387
9388
2.09M
  return op_array;
9389
2.09M
}
9390
9391
static zend_op_array *zend_compile_func_decl(znode *result, zend_ast *ast, enum func_decl_level level)
9392
2.09M
{
9393
2.09M
  return zend_compile_func_decl_ex(result, ast, level, /* property_info */ NULL, (zend_property_hook_kind)-1);
9394
2.09M
}
9395
9396
5.41k
zend_property_hook_kind zend_get_property_hook_kind_from_name(const zend_string *name) {
9397
5.41k
  if (zend_string_equals_literal_ci(name, "get")) {
9398
2.83k
    return ZEND_PROPERTY_HOOK_GET;
9399
2.83k
  } else if (zend_string_equals_literal_ci(name, "set")) {
9400
2.37k
    return ZEND_PROPERTY_HOOK_SET;
9401
2.37k
  } else {
9402
209
    return (zend_property_hook_kind)-1;
9403
209
  }
9404
5.41k
}
9405
9406
static void zend_compile_property_hooks(
9407
    zend_property_info *prop_info, zend_string *prop_name,
9408
    zend_ast *prop_type_ast, const zend_ast_list *hooks)
9409
3.26k
{
9410
3.26k
  zend_class_entry *ce = CG(active_class_entry);
9411
9412
3.26k
  if (prop_info->flags & ZEND_ACC_READONLY) {
9413
7
    zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly");
9414
7
  }
9415
9416
3.26k
  if (hooks->children == 0) {
9417
14
    zend_error_noreturn(E_COMPILE_ERROR, "Property hook list must not be empty");
9418
14
  }
9419
9420
6.62k
  for (uint32_t i = 0; i < hooks->children; i++) {
9421
3.67k
    zend_ast_decl *hook = (zend_ast_decl *) hooks->child[i];
9422
3.67k
    zend_string *name = hook->name;
9423
3.67k
    zend_ast *stmt_ast = hook->child[2];
9424
3.67k
    zend_ast **return_type_ast_ptr = NULL;
9425
3.67k
    zend_ast **value_type_ast_ptr = NULL;
9426
3.67k
    CG(zend_lineno) = hook->start_lineno;
9427
9428
    /* Non-private hooks are always public. This avoids having to copy the hook when inheriting
9429
     * hooks from protected properties to public ones. */
9430
3.67k
    uint32_t hook_visibility = (prop_info->flags & ZEND_ACC_PPP_MASK) != ZEND_ACC_PRIVATE ? ZEND_ACC_PUBLIC : ZEND_ACC_PRIVATE;
9431
3.67k
    hook->flags |= hook_visibility;
9432
9433
3.67k
    if (prop_info->flags & ZEND_ACC_STATIC) {
9434
4
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare hooks for static property");
9435
4
    }
9436
3.67k
    if ((hook->flags & ZEND_ACC_FINAL) && (hook->flags & ZEND_ACC_PRIVATE)) {
9437
1
      zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both final and private");
9438
1
    }
9439
3.67k
    if ((ce->ce_flags & ZEND_ACC_INTERFACE)
9440
3.30k
     || ((prop_info->flags & ZEND_ACC_ABSTRACT) && !stmt_ast)) {
9441
820
      hook->flags |= ZEND_ACC_ABSTRACT;
9442
9443
820
      if (stmt_ast) {
9444
70
        zend_error_noreturn(E_COMPILE_ERROR, "Abstract property hook cannot have body");
9445
70
      }
9446
820
      if (hook->flags & ZEND_ACC_PRIVATE) {
9447
1
        zend_error_noreturn(E_COMPILE_ERROR,
9448
1
          "Property hook cannot be both abstract and private");
9449
1
      }
9450
820
      if (hook->flags & ZEND_ACC_FINAL) {
9451
5
        zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both abstract and final");
9452
5
      }
9453
2.85k
    } else if (!stmt_ast) {
9454
23
      zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract property hook must have a body");
9455
23
    }
9456
9457
3.67k
    zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(name);
9458
3.67k
    if (hook_kind == (zend_property_hook_kind)-1) {
9459
209
      zend_error_noreturn(E_COMPILE_ERROR,
9460
209
        "Unknown hook \"%s\" for property %s::$%s, expected \"get\" or \"set\"",
9461
209
        ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9462
209
    }
9463
9464
3.67k
    if (stmt_ast && stmt_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
9465
1.72k
      stmt_ast = stmt_ast->child[0];
9466
1.72k
      if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
9467
1.37k
        stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
9468
1.37k
      } else {
9469
348
        ZEND_ASSERT(hook_kind == ZEND_PROPERTY_HOOK_SET);
9470
348
        stmt_ast = zend_ast_create(ZEND_AST_ASSIGN,
9471
348
          zend_ast_create(ZEND_AST_PROP,
9472
348
            zend_ast_create(ZEND_AST_VAR, zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_THIS))),
9473
348
            zend_ast_create_zval_from_str(zend_copy_unmangled_prop_name(prop_info->name))),
9474
348
          stmt_ast);
9475
348
      }
9476
1.72k
      stmt_ast = zend_ast_create_list(1, ZEND_AST_STMT_LIST, stmt_ast);
9477
1.72k
      hook->child[2] = stmt_ast;
9478
1.72k
    }
9479
9480
3.67k
    if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
9481
2.16k
      if (hook->child[0]) {
9482
1
        zend_error_noreturn(E_COMPILE_ERROR, "get hook of property %s::$%s must not have a parameter list",
9483
1
          ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9484
1
      }
9485
9486
2.16k
      hook->child[0] = zend_ast_create_list(0, ZEND_AST_PARAM_LIST);
9487
9488
2.16k
      return_type_ast_ptr = &hook->child[3];
9489
2.16k
      *return_type_ast_ptr = prop_type_ast;
9490
2.16k
    } else if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
9491
1.20k
      if (hook->child[0]) {
9492
83
        const zend_ast_list *param_list = zend_ast_get_list(hook->child[0]);
9493
83
        if (param_list->children != 1) {
9494
1
          zend_error_noreturn(E_COMPILE_ERROR, "%s hook of property %s::$%s must accept exactly one parameters",
9495
1
            ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9496
1
        }
9497
83
        const zend_ast *value_param_ast = param_list->child[0];
9498
83
        if (value_param_ast->attr & ZEND_PARAM_REF) {
9499
1
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be pass-by-reference",
9500
1
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9501
1
        }
9502
83
        if (value_param_ast->attr & ZEND_PARAM_VARIADIC) {
9503
1
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be variadic",
9504
1
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9505
1
        }
9506
83
        if (value_param_ast->child[2]) {
9507
1
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not have a default value",
9508
1
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9509
1
        }
9510
83
        if ((prop_type_ast != NULL) != (value_param_ast->child[0] != NULL)) {
9511
1
          zend_hooked_property_variance_error_ex(zend_ast_get_str(value_param_ast->child[1]), ce->name, prop_info->name);
9512
1
        }
9513
1.11k
      } else {
9514
1.11k
        zend_ast *param_name_ast = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VALUE));
9515
1.11k
        zend_ast *param = zend_ast_create(
9516
1.11k
          ZEND_AST_PARAM, prop_type_ast, param_name_ast,
9517
1.11k
          /* expr */ NULL, /* doc_comment */ NULL, /* attributes */ NULL,
9518
1.11k
          /* hooks */ NULL);
9519
1.11k
        value_type_ast_ptr = &param->child[0];
9520
1.11k
        hook->child[0] = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, param);
9521
1.11k
      }
9522
1.20k
      zend_ast *return_type = zend_ast_create(ZEND_AST_TYPE);
9523
1.20k
      return_type->attr = IS_VOID;
9524
1.20k
      hook->child[3] = return_type;
9525
1.20k
    } else {
9526
313
      ZEND_UNREACHABLE();
9527
313
    }
9528
9529
3.36k
    hook->name = zend_strpprintf(0, "$%s::%s", ZSTR_VAL(prop_name), ZSTR_VAL(name));
9530
9531
3.36k
    zend_function *func = (zend_function *) zend_compile_func_decl_ex(
9532
3.36k
      NULL, (zend_ast *) hook, FUNC_DECL_LEVEL_NESTED, prop_info->name, hook_kind);
9533
9534
3.36k
    func->common.prop_info = prop_info;
9535
9536
3.36k
    if (!prop_info->hooks) {
9537
2.87k
      prop_info->hooks = zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE);
9538
2.87k
      memset(prop_info->hooks, 0, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
9539
2.87k
    }
9540
9541
3.36k
    if (prop_info->hooks[hook_kind]) {
9542
21
      zend_error_noreturn(E_COMPILE_ERROR,
9543
21
        "Cannot redeclare property hook \"%s\"", ZSTR_VAL(name));
9544
21
    }
9545
3.36k
    prop_info->hooks[hook_kind] = func;
9546
9547
3.36k
    if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
9548
1.13k
      switch (zend_verify_property_hook_variance(prop_info, func)) {
9549
1.06k
        case INHERITANCE_SUCCESS:
9550
1.06k
          break;
9551
71
        case INHERITANCE_UNRESOLVED:
9552
71
          ce->num_hooked_prop_variance_checks++;
9553
71
          break;
9554
3
        case INHERITANCE_ERROR:
9555
3
          zend_hooked_property_variance_error(prop_info);
9556
3
        case INHERITANCE_WARNING:
9557
3
          ZEND_UNREACHABLE();
9558
1.13k
      }
9559
1.13k
    }
9560
9561
3.36k
    zend_string_release(name);
9562
    /* Un-share type ASTs to avoid double-frees of zval nodes. */
9563
3.36k
    if (return_type_ast_ptr) {
9564
2.13k
      *return_type_ast_ptr = NULL;
9565
2.13k
    }
9566
3.36k
    if (value_type_ast_ptr) {
9567
1.05k
      *value_type_ast_ptr = NULL;
9568
1.05k
    }
9569
3.36k
  }
9570
9571
2.94k
  ce->num_hooked_props++;
9572
9573
  /* See zend_link_hooked_object_iter(). */
9574
2.94k
#ifndef ZEND_OPCACHE_SHM_REATTACHMENT
9575
2.94k
  if (!ce->get_iterator) {
9576
    /* Will be removed again, in case of Iterator or IteratorAggregate. */
9577
2.24k
    ce->get_iterator = zend_hooked_object_get_iterator;
9578
2.24k
  }
9579
2.94k
#endif
9580
9581
2.94k
  if (!prop_info->ce->parent_name) {
9582
1.56k
    zend_verify_hooked_property(ce, prop_info, prop_name);
9583
1.56k
  }
9584
2.94k
}
9585
9586
static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */
9587
20.7k
{
9588
20.7k
  const zend_ast_list *list = zend_ast_get_list(ast);
9589
20.7k
  zend_class_entry *ce = CG(active_class_entry);
9590
20.7k
  uint32_t i, children = list->children;
9591
9592
20.7k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
9593
6
    zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", ZSTR_VAL(ce->name));
9594
6
  }
9595
9596
20.7k
  if ((flags & ZEND_ACC_FINAL) && (flags & ZEND_ACC_PRIVATE)) {
9597
1
    zend_error_noreturn(E_COMPILE_ERROR, "Property cannot be both final and private");
9598
1
  }
9599
9600
20.7k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9601
387
    if (flags & ZEND_ACC_FINAL) {
9602
1
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be final");
9603
1
    }
9604
387
    if (flags & (ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE)) {
9605
1
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be protected or private");
9606
1
    }
9607
387
    if (flags & ZEND_ACC_ABSTRACT) {
9608
1
      zend_error_noreturn(E_COMPILE_ERROR,
9609
1
        "Property in interface cannot be explicitly abstract. "
9610
1
        "All interface members are implicitly abstract");
9611
1
    }
9612
387
    flags |= ZEND_ACC_ABSTRACT;
9613
387
  }
9614
9615
44.1k
  for (i = 0; i < children; ++i) {
9616
23.3k
    zend_property_info *info;
9617
23.3k
    zend_ast *prop_ast = list->child[i];
9618
23.3k
    zend_ast *name_ast = prop_ast->child[0];
9619
23.3k
    zend_ast **value_ast_ptr = &prop_ast->child[1];
9620
23.3k
    zend_ast *doc_comment_ast = prop_ast->child[2];
9621
23.3k
    zend_ast *hooks_ast = prop_ast->child[3];
9622
23.3k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
9623
23.3k
    zend_string *doc_comment = NULL;
9624
23.3k
    zval value_zv;
9625
23.3k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
9626
23.3k
    flags |= zend_property_is_virtual(ce, name, hooks_ast) ? ZEND_ACC_VIRTUAL : 0;
9627
9628
23.3k
    zend_string *old_active_property_info_name = CG(context).active_property_info_name;
9629
23.3k
    CG(context).active_property_info_name = name;
9630
9631
23.3k
    if (!hooks_ast) {
9632
20.2k
      if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9633
8
        zend_error_noreturn(E_COMPILE_ERROR,
9634
8
          "Interfaces may only include hooked properties");
9635
8
      }
9636
20.2k
      if (flags & ZEND_ACC_ABSTRACT) {
9637
1
        zend_error_noreturn(E_COMPILE_ERROR,
9638
1
          "Only hooked properties may be declared abstract");
9639
1
      }
9640
20.2k
    }
9641
23.3k
    if ((flags & ZEND_ACC_ABSTRACT)) {
9642
794
      ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
9643
794
    }
9644
9645
23.3k
    if (type_ast) {
9646
11.9k
      type = zend_compile_typename(type_ast);
9647
9648
11.9k
      if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_NEVER|MAY_BE_CALLABLE)) {
9649
1
        zend_string *str = zend_type_to_string(type);
9650
1
        zend_error_noreturn(E_COMPILE_ERROR,
9651
1
          "Property %s::$%s cannot have type %s",
9652
1
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9653
1
      }
9654
11.9k
    }
9655
9656
    /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */
9657
23.3k
    if (doc_comment_ast) {
9658
188
      doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9659
188
    }
9660
9661
23.3k
    if (zend_hash_exists(&ce->properties_info, name)) {
9662
28
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
9663
28
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
9664
28
    }
9665
9666
23.3k
    if (*value_ast_ptr) {
9667
6.20k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9668
9669
6.20k
      if (ZEND_TYPE_IS_SET(type) && Z_TYPE(value_zv) != IS_CONSTANT_AST
9670
1.11k
          && !zend_is_valid_default_value(type, &value_zv)) {
9671
33
        zend_string *str = zend_type_to_string(type);
9672
33
        if (Z_TYPE(value_zv) == IS_NULL && !ZEND_TYPE_IS_INTERSECTION(type)) {
9673
4
          ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
9674
4
          zend_string *nullable_str = zend_type_to_string(type);
9675
9676
4
          zend_error_noreturn(E_COMPILE_ERROR,
9677
4
            "Default value for property of type %s may not be null. "
9678
4
            "Use the nullable type %s to allow null default value",
9679
4
            ZSTR_VAL(str), ZSTR_VAL(nullable_str));
9680
29
        } else {
9681
29
          zend_error_noreturn(E_COMPILE_ERROR,
9682
29
            "Cannot use %s as default value for property %s::$%s of type %s",
9683
29
            zend_zval_value_name(&value_zv),
9684
29
            ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9685
29
        }
9686
33
      }
9687
17.1k
    } else if (!ZEND_TYPE_IS_SET(type) && !hooks_ast) {
9688
5.43k
      ZVAL_NULL(&value_zv);
9689
11.7k
    } else {
9690
11.7k
      ZVAL_UNDEF(&value_zv);
9691
11.7k
    }
9692
9693
23.3k
    if ((ce->ce_flags & ZEND_ACC_READONLY_CLASS)) {
9694
66
      flags |= ZEND_ACC_READONLY;
9695
66
    }
9696
9697
23.3k
    if (flags & ZEND_ACC_READONLY) {
9698
169
      if (!ZEND_TYPE_IS_SET(type)) {
9699
5
        zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
9700
5
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9701
5
      }
9702
169
      if (flags & ZEND_ACC_STATIC) {
9703
1
        zend_error_noreturn(E_COMPILE_ERROR,
9704
1
          "Static property %s::$%s cannot be readonly",
9705
1
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9706
1
      }
9707
169
    }
9708
9709
23.3k
    info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type);
9710
9711
23.3k
    if (hooks_ast) {
9712
3.09k
      zend_compile_property_hooks(info, name, type_ast, zend_ast_get_list(hooks_ast));
9713
3.09k
    }
9714
9715
23.3k
    if (attr_ast) {
9716
5.26k
      zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0);
9717
9718
5.26k
      const zend_attribute *override_attribute = zend_get_attribute_str(info->attributes, "override", sizeof("override")-1);
9719
5.26k
      if (override_attribute) {
9720
68
        info->flags |= ZEND_ACC_OVERRIDE;
9721
68
      }
9722
5.26k
    }
9723
9724
23.3k
    CG(context).active_property_info_name = old_active_property_info_name;
9725
23.3k
  }
9726
20.7k
}
9727
/* }}} */
9728
9729
static void zend_compile_prop_group(const zend_ast *ast) /* {{{ */
9730
20.7k
{
9731
20.7k
  zend_ast *type_ast = ast->child[0];
9732
20.7k
  zend_ast *prop_ast = ast->child[1];
9733
20.7k
  zend_ast *attr_ast = ast->child[2];
9734
9735
20.7k
  zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast);
9736
20.7k
}
9737
/* }}} */
9738
9739
static void zend_check_trait_alias_modifiers(uint32_t attr) /* {{{ */
9740
5.27k
{
9741
5.27k
  if (attr & ZEND_ACC_STATIC) {
9742
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"static\" as method modifier in trait alias");
9743
5.27k
  } else if (attr & ZEND_ACC_ABSTRACT) {
9744
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"abstract\" as method modifier in trait alias");
9745
1
  }
9746
5.27k
}
9747
/* }}} */
9748
9749
static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast, zend_ast *type_ast)
9750
13.1k
{
9751
13.1k
  const zend_ast_list *list = zend_ast_get_list(ast);
9752
13.1k
  zend_class_entry *ce = CG(active_class_entry);
9753
13.1k
  uint32_t i, children = list->children;
9754
9755
26.4k
  for (i = 0; i < children; ++i) {
9756
13.3k
    zend_class_constant *c;
9757
13.3k
    zend_ast *const_ast = list->child[i];
9758
13.3k
    zend_ast *name_ast = const_ast->child[0];
9759
13.3k
    zend_ast **value_ast_ptr = &const_ast->child[1];
9760
13.3k
    zend_ast *doc_comment_ast = const_ast->child[2];
9761
13.3k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
9762
13.3k
    zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
9763
13.3k
    zval value_zv;
9764
13.3k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
9765
9766
13.3k
    if (type_ast) {
9767
6.40k
      type = zend_compile_typename(type_ast);
9768
9769
6.40k
      uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9770
9771
6.40k
      if (type_mask != MAY_BE_ANY && (type_mask & (MAY_BE_CALLABLE|MAY_BE_VOID|MAY_BE_NEVER))) {
9772
1
        zend_string *type_str = zend_type_to_string(type);
9773
9774
1
        zend_error_noreturn(E_COMPILE_ERROR, "Class constant %s::%s cannot have type %s",
9775
1
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9776
1
      }
9777
6.40k
    }
9778
9779
13.3k
    if (UNEXPECTED((flags & ZEND_ACC_PRIVATE) && (flags & ZEND_ACC_FINAL))) {
9780
1
      zend_error_noreturn(
9781
1
        E_COMPILE_ERROR, "Private constant %s::%s cannot be final as it is not visible to other classes",
9782
1
        ZSTR_VAL(ce->name), ZSTR_VAL(name)
9783
1
      );
9784
1
    }
9785
9786
13.3k
    zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9787
9788
13.3k
    if (Z_TYPE(value_zv) != IS_CONSTANT_AST && ZEND_TYPE_IS_SET(type) && !zend_is_valid_default_value(type, &value_zv)) {
9789
14
      zend_string *type_str = zend_type_to_string(type);
9790
9791
14
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as value for class constant %s::%s of type %s",
9792
14
        zend_zval_type_name(&value_zv), ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9793
14
    }
9794
9795
13.3k
    c = zend_declare_typed_class_constant(ce, name, &value_zv, flags, doc_comment, type);
9796
9797
13.3k
    if (attr_ast) {
9798
450
      zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9799
9800
450
      const zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9801
9802
450
      if (deprecated) {
9803
124
        ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9804
        /* For deprecated constants, we need to flag the zval for recursion
9805
         * detection. Make sure the zval is separated out of shm. */
9806
124
        ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
9807
124
        ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
9808
124
      }
9809
9810
450
      const zend_attribute *override = zend_get_attribute_str(c->attributes, "override", sizeof("override") - 1);
9811
450
      if (override) {
9812
240
        ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_OVERRIDE;
9813
        /* We need to be able to remove the flag once the override is
9814
         * resolved. See ZEND_ACC_DEPRECATED above. */
9815
240
        ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
9816
240
        ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
9817
240
      }
9818
450
    }
9819
13.3k
  }
9820
13.1k
}
9821
9822
static void zend_compile_class_const_group(const zend_ast *ast) /* {{{ */
9823
13.1k
{
9824
13.1k
  zend_ast *const_ast = ast->child[0];
9825
13.1k
  zend_ast *attr_ast = ast->child[1];
9826
13.1k
  zend_ast *type_ast = ast->child[2];
9827
9828
13.1k
  zend_compile_class_const_decl(const_ast, ast->attr, attr_ast, type_ast);
9829
13.1k
}
9830
/* }}} */
9831
9832
static void zend_compile_method_ref(const zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */
9833
7.26k
{
9834
7.26k
  zend_ast *class_ast = ast->child[0];
9835
7.26k
  zend_ast *method_ast = ast->child[1];
9836
9837
7.26k
  method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
9838
9839
7.26k
  if (class_ast) {
9840
3.61k
    method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name");
9841
3.65k
  } else {
9842
3.65k
    method_ref->class_name = NULL;
9843
3.65k
  }
9844
7.26k
}
9845
/* }}} */
9846
9847
static void zend_compile_trait_precedence(const zend_ast *ast) /* {{{ */
9848
1.99k
{
9849
1.99k
  const zend_ast *method_ref_ast = ast->child[0];
9850
1.99k
  zend_ast *insteadof_ast = ast->child[1];
9851
1.99k
  const zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast);
9852
1.99k
  uint32_t i;
9853
9854
1.99k
  zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*));
9855
1.99k
  zend_compile_method_ref(method_ref_ast, &precedence->trait_method);
9856
1.99k
  precedence->num_excludes = insteadof_list->children;
9857
9858
26.7k
  for (i = 0; i < insteadof_list->children; ++i) {
9859
24.7k
    zend_ast *name_ast = insteadof_list->child[i];
9860
24.7k
    precedence->exclude_class_names[i] =
9861
24.7k
      zend_resolve_const_class_name_reference(name_ast, "trait name");
9862
24.7k
  }
9863
9864
1.99k
  zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
9865
1.99k
}
9866
/* }}} */
9867
9868
static void zend_compile_trait_alias(const zend_ast *ast) /* {{{ */
9869
5.27k
{
9870
5.27k
  const zend_ast *method_ref_ast = ast->child[0];
9871
5.27k
  zend_ast *alias_ast = ast->child[1];
9872
5.27k
  uint32_t modifiers = ast->attr;
9873
9874
5.27k
  zend_trait_alias *alias;
9875
9876
5.27k
  zend_check_trait_alias_modifiers(modifiers);
9877
9878
5.27k
  alias = emalloc(sizeof(zend_trait_alias));
9879
5.27k
  zend_compile_method_ref(method_ref_ast, &alias->trait_method);
9880
5.27k
  alias->modifiers = modifiers;
9881
9882
5.27k
  if (alias_ast) {
9883
4.98k
    alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
9884
4.98k
  } else {
9885
291
    alias->alias = NULL;
9886
291
  }
9887
9888
5.27k
  zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
9889
5.27k
}
9890
/* }}} */
9891
9892
static void zend_compile_use_trait(const zend_ast *ast) /* {{{ */
9893
10.9k
{
9894
10.9k
  const zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
9895
10.9k
  zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
9896
10.9k
  zend_class_entry *ce = CG(active_class_entry);
9897
10.9k
  uint32_t i;
9898
9899
10.9k
  ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children));
9900
9901
24.3k
  for (i = 0; i < traits->children; ++i) {
9902
13.4k
    zend_ast *trait_ast = traits->child[i];
9903
9904
13.4k
    if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9905
1
      zend_string *name = zend_ast_get_str(trait_ast);
9906
1
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
9907
1
        "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
9908
1
    }
9909
9910
13.4k
    ce->trait_names[ce->num_traits].name =
9911
13.4k
      zend_resolve_const_class_name_reference(trait_ast, "trait name");
9912
13.4k
    ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name);
9913
13.4k
    ce->num_traits++;
9914
13.4k
  }
9915
9916
10.9k
  if (!adaptations) {
9917
8.80k
    return;
9918
8.80k
  }
9919
9920
9.40k
  for (i = 0; i < adaptations->children; ++i) {
9921
7.27k
    const zend_ast *adaptation_ast = adaptations->child[i];
9922
7.27k
    switch (adaptation_ast->kind) {
9923
1.99k
      case ZEND_AST_TRAIT_PRECEDENCE:
9924
1.99k
        zend_compile_trait_precedence(adaptation_ast);
9925
1.99k
        break;
9926
5.27k
      case ZEND_AST_TRAIT_ALIAS:
9927
5.27k
        zend_compile_trait_alias(adaptation_ast);
9928
5.27k
        break;
9929
0
      default: ZEND_UNREACHABLE();
9930
7.27k
    }
9931
7.27k
  }
9932
2.13k
}
9933
/* }}} */
9934
9935
static void zend_compile_implements(zend_ast *ast) /* {{{ */
9936
60.2k
{
9937
60.2k
  const zend_ast_list *list = zend_ast_get_list(ast);
9938
60.2k
  zend_class_entry *ce = CG(active_class_entry);
9939
60.2k
  zend_class_name *interface_names;
9940
60.2k
  uint32_t i;
9941
9942
60.2k
  interface_names = emalloc(sizeof(zend_class_name) * list->children);
9943
9944
171k
  for (i = 0; i < list->children; ++i) {
9945
111k
    zend_ast *class_ast = list->child[i];
9946
111k
    interface_names[i].name =
9947
111k
      zend_resolve_const_class_name_reference(class_ast, "interface name");
9948
111k
    interface_names[i].lc_name = zend_string_tolower(interface_names[i].name);
9949
111k
  }
9950
9951
60.2k
  ce->num_interfaces = list->children;
9952
60.2k
  ce->interface_names = interface_names;
9953
60.2k
}
9954
/* }}} */
9955
9956
static zend_string *zend_generate_anon_class_name(const zend_ast_decl *decl)
9957
58.1k
{
9958
58.1k
  zend_string *filename = CG(active_op_array)->filename;
9959
58.1k
  uint32_t start_lineno = decl->start_lineno;
9960
9961
  /* Use parent or first interface as prefix. */
9962
58.1k
  zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS);
9963
58.1k
  if (decl->child[0]) {
9964
32
    prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name");
9965
58.1k
  } else if (decl->child[1]) {
9966
57.9k
    const zend_ast_list *list = zend_ast_get_list(decl->child[1]);
9967
57.9k
    prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name");
9968
57.9k
  }
9969
9970
58.1k
  zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32,
9971
58.1k
    ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
9972
58.1k
  zend_string_release(prefix);
9973
58.1k
  return zend_new_interned_string(result);
9974
58.1k
}
9975
9976
static void zend_compile_enum_backing_type(zend_class_entry *ce, zend_ast *enum_backing_type_ast)
9977
691
{
9978
691
  ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM);
9979
691
  zend_type type = zend_compile_typename(enum_backing_type_ast);
9980
691
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9981
691
  if (ZEND_TYPE_IS_COMPLEX(type) || (type_mask != MAY_BE_LONG && type_mask != MAY_BE_STRING)) {
9982
142
    zend_string *type_string = zend_type_to_string(type);
9983
142
    zend_error_noreturn(E_COMPILE_ERROR,
9984
142
      "Enum backing type must be int or string, %s given",
9985
142
      ZSTR_VAL(type_string));
9986
142
  }
9987
691
  if (type_mask == MAY_BE_LONG) {
9988
347
    ce->enum_backing_type = IS_LONG;
9989
347
  } else {
9990
344
    ZEND_ASSERT(type_mask == MAY_BE_STRING);
9991
344
    ce->enum_backing_type = IS_STRING;
9992
197
  }
9993
691
  zend_type_release(type, 0);
9994
544
}
9995
9996
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel) /* {{{ */
9997
316k
{
9998
316k
  const zend_ast_decl *decl = (const zend_ast_decl *) ast;
9999
316k
  zend_ast *extends_ast = decl->child[0];
10000
316k
  zend_ast *implements_ast = decl->child[1];
10001
316k
  zend_ast *stmt_ast = decl->child[2];
10002
316k
  zend_ast *enum_backing_type_ast = decl->child[4];
10003
316k
  zend_string *name, *lcname;
10004
316k
  zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
10005
316k
  zend_op *opline;
10006
10007
316k
  zend_class_entry *original_ce = CG(active_class_entry);
10008
10009
316k
  if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
10010
258k
    zend_string *unqualified_name = decl->name;
10011
10012
258k
    if (CG(active_class_entry)) {
10013
1
      zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
10014
1
    }
10015
10016
258k
    const char *type = "a class name";
10017
258k
    if (decl->flags & ZEND_ACC_ENUM) {
10018
90.2k
      type = "an enum name";
10019
168k
    } else if (decl->flags & ZEND_ACC_INTERFACE) {
10020
6.79k
      type = "an interface name";
10021
161k
    } else if (decl->flags & ZEND_ACC_TRAIT) {
10022
13.3k
      type = "a trait name";
10023
13.3k
    }
10024
258k
    zend_assert_valid_class_name(unqualified_name, type);
10025
258k
    name = zend_prefix_with_ns(unqualified_name);
10026
258k
    name = zend_new_interned_string(name);
10027
258k
    lcname = zend_string_tolower(name);
10028
10029
258k
    if (FC(imports)) {
10030
762
      zend_string *import_name =
10031
762
        zend_hash_find_ptr_lc(FC(imports), unqualified_name);
10032
762
      if (import_name && !zend_string_equals_ci(lcname, import_name)) {
10033
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s "
10034
1
            "(previously declared as local import)", ZSTR_VAL(name));
10035
1
      }
10036
762
    }
10037
10038
258k
    zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS);
10039
258k
  } else {
10040
    /* Find an anon class name that is not in use yet. */
10041
58.1k
    name = NULL;
10042
58.1k
    lcname = NULL;
10043
58.1k
    do {
10044
58.1k
      zend_tmp_string_release(name);
10045
58.1k
      zend_tmp_string_release(lcname);
10046
58.1k
      name = zend_generate_anon_class_name(decl);
10047
58.1k
      lcname = zend_string_tolower(name);
10048
58.1k
    } while (zend_hash_exists(CG(class_table), lcname));
10049
58.1k
  }
10050
316k
  lcname = zend_new_interned_string(lcname);
10051
10052
316k
  ce->type = ZEND_USER_CLASS;
10053
316k
  ce->name = name;
10054
316k
  zend_initialize_class_data(ce, true);
10055
316k
  if (!(decl->flags & ZEND_ACC_ANON_CLASS)) {
10056
258k
    zend_alloc_ce_cache(ce->name);
10057
258k
  }
10058
10059
316k
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
10060
0
    ce->ce_flags |= ZEND_ACC_PRELOADED;
10061
0
    ZEND_MAP_PTR_NEW(ce->static_members_table);
10062
0
    ZEND_MAP_PTR_NEW(ce->mutable_data);
10063
0
  }
10064
10065
316k
  ce->ce_flags |= decl->flags;
10066
316k
  ce->info.user.filename = zend_string_copy(zend_get_compiled_filename());
10067
316k
  ce->info.user.line_start = decl->start_lineno;
10068
316k
  ce->info.user.line_end = decl->end_lineno;
10069
10070
316k
  if (decl->doc_comment) {
10071
68
    ce->doc_comment = zend_string_copy(decl->doc_comment);
10072
68
  }
10073
10074
316k
  if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
10075
    /* Serialization is not supported for anonymous classes */
10076
58.1k
    ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
10077
58.1k
  }
10078
10079
316k
  if (extends_ast) {
10080
53.8k
    ce->parent_name =
10081
53.8k
      zend_resolve_const_class_name_reference(extends_ast, "class name");
10082
53.8k
  }
10083
10084
316k
  CG(active_class_entry) = ce;
10085
10086
316k
  if (decl->child[3]) {
10087
2.72k
    zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS, 0);
10088
2.72k
  }
10089
10090
316k
  if (implements_ast) {
10091
60.2k
    zend_compile_implements(implements_ast);
10092
60.2k
  }
10093
10094
316k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
10095
90.2k
    if (enum_backing_type_ast != NULL) {
10096
691
      zend_compile_enum_backing_type(ce, enum_backing_type_ast);
10097
691
    }
10098
90.2k
    zend_enum_add_interfaces(ce);
10099
90.2k
    zend_enum_register_props(ce);
10100
90.2k
  }
10101
10102
316k
  zend_compile_stmt(stmt_ast);
10103
10104
  /* Reset lineno for final opcodes and errors */
10105
316k
  CG(zend_lineno) = ast->lineno;
10106
10107
316k
  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) {
10108
145
    zend_verify_abstract_class(ce);
10109
145
  }
10110
10111
316k
  CG(active_class_entry) = original_ce;
10112
10113
316k
  if (toplevel) {
10114
22.3k
    ce->ce_flags |= ZEND_ACC_TOP_LEVEL;
10115
22.3k
  }
10116
10117
  /* We currently don't early-bind classes that implement interfaces or use traits */
10118
316k
  if (!ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
10119
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
10120
   /* See zend_link_hooked_object_iter(). */
10121
   && !ce->num_hooked_props
10122
#endif
10123
154k
   && !(CG(compiler_options) & ZEND_COMPILE_WITHOUT_EXECUTION)) {
10124
154k
    if (toplevel) {
10125
17.4k
      if (extends_ast) {
10126
7.96k
        zend_class_entry *parent_ce = zend_lookup_class_ex(
10127
7.96k
          ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
10128
10129
7.96k
        if (parent_ce
10130
7.16k
         && !zend_compile_ignore_class(parent_ce, ce->info.user.filename)) {
10131
7.16k
          if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) {
10132
2.75k
            zend_string_release(lcname);
10133
2.75k
            return;
10134
2.75k
          }
10135
7.16k
        }
10136
9.50k
      } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) {
10137
4.36k
        zend_string_release(lcname);
10138
4.36k
        zend_build_properties_info_table(ce);
10139
4.36k
        zend_inheritance_check_override(ce);
10140
4.36k
        ce->ce_flags |= ZEND_ACC_LINKED;
10141
4.36k
        zend_observer_class_linked_notify(ce, lcname);
10142
4.36k
        return;
10143
5.14k
      } else {
10144
5.14k
        goto link_unbound;
10145
5.14k
      }
10146
136k
    } else if (!extends_ast) {
10147
96.3k
link_unbound:
10148
      /* Link unbound simple class */
10149
96.3k
      zend_build_properties_info_table(ce);
10150
96.3k
      zend_inheritance_check_override(ce);
10151
96.3k
      ce->ce_flags |= ZEND_ACC_LINKED;
10152
96.3k
    }
10153
154k
  }
10154
10155
309k
  opline = get_next_op();
10156
10157
309k
  if (ce->parent_name) {
10158
    /* Lowercased parent name */
10159
50.4k
    zend_string *lc_parent_name = zend_string_tolower(ce->parent_name);
10160
50.4k
    opline->op2_type = IS_CONST;
10161
50.4k
    LITERAL_STR(opline->op2, lc_parent_name);
10162
50.4k
  }
10163
10164
309k
  opline->op1_type = IS_CONST;
10165
  /* It's possible that `lcname` is not an interned string because it was not yet in the interned string table.
10166
   * However, by this point another thread may have caused `lcname` to be added in the interned string table.
10167
   * This will cause `lcname` to get freed once it is found in the interned string table. If we were to use
10168
   * LITERAL_STR() here we would not change the `lcname` pointer to the new value, and it would point to the
10169
   * now-freed string. This will cause issues when we use `lcname` in the code below. We solve this by using
10170
   * zend_add_literal_string() which gives us the new value. */
10171
309k
  opline->op1.constant = zend_add_literal_string(&lcname);
10172
10173
309k
  if (decl->flags & ZEND_ACC_ANON_CLASS) {
10174
58.1k
    opline->opcode = ZEND_DECLARE_ANON_CLASS;
10175
58.1k
    opline->extended_value = zend_alloc_cache_slot();
10176
58.1k
    zend_make_var_result(result, opline);
10177
58.1k
    if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) {
10178
      /* We checked above that the class name is not used. This really shouldn't happen. */
10179
0
      zend_error_noreturn(E_ERROR,
10180
0
        "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name));
10181
0
    }
10182
251k
  } else {
10183
    /* Generate RTD keys until we find one that isn't in use yet. */
10184
251k
    zend_string *key = NULL;
10185
251k
    do {
10186
251k
      zend_tmp_string_release(key);
10187
251k
      key = zend_build_runtime_definition_key(lcname, decl->start_lineno);
10188
251k
    } while (!zend_hash_add_ptr(CG(class_table), key, ce));
10189
10190
    /* RTD key is placed after lcname literal in op1 */
10191
251k
    zend_add_literal_string(&key);
10192
10193
251k
    opline->opcode = ZEND_DECLARE_CLASS;
10194
251k
    if (toplevel
10195
14.6k
       && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING)
10196
        /* We currently don't early-bind classes that implement interfaces or use traits */
10197
191
       && !ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
10198
251k
    ) {
10199
58
      if (!extends_ast) {
10200
        /* Use empty string for classes without parents to avoid new handler, and special
10201
         * handling of zend_early_binding. */
10202
28
        opline->op2_type = IS_CONST;
10203
28
        LITERAL_STR(opline->op2, ZSTR_EMPTY_ALLOC());
10204
28
      }
10205
58
      CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING;
10206
58
      opline->opcode = ZEND_DECLARE_CLASS_DELAYED;
10207
58
      opline->extended_value = zend_alloc_cache_slot();
10208
58
      opline->result_type = IS_UNUSED;
10209
58
      opline->result.opline_num = -1;
10210
58
    }
10211
251k
  }
10212
309k
}
10213
/* }}} */
10214
10215
static void zend_compile_enum_case(zend_ast *ast)
10216
197k
{
10217
197k
  zend_class_entry *enum_class = CG(active_class_entry);
10218
197k
  if (!(enum_class->ce_flags & ZEND_ACC_ENUM)) {
10219
7
    zend_error_noreturn(E_COMPILE_ERROR, "Case can only be used in enums");
10220
7
  }
10221
10222
197k
  zend_string *enum_case_name = zval_make_interned_string(zend_ast_get_zval(ast->child[0]));
10223
197k
  zend_string *enum_class_name = enum_class->name;
10224
10225
197k
  zval class_name_zval;
10226
197k
  ZVAL_STR_COPY(&class_name_zval, enum_class_name);
10227
197k
  zend_ast *class_name_ast = zend_ast_create_zval(&class_name_zval);
10228
10229
197k
  zval case_id_zval;
10230
197k
  int case_id = zend_enum_next_case_id(enum_class);
10231
197k
  ZVAL_LONG(&case_id_zval, case_id);
10232
197k
  zend_ast *case_id_ast = zend_ast_create_zval(&case_id_zval);
10233
10234
197k
  zval case_name_zval;
10235
197k
  ZVAL_STR_COPY(&case_name_zval, enum_case_name);
10236
197k
  zend_ast *case_name_ast = zend_ast_create_zval(&case_name_zval);
10237
10238
197k
  zend_ast *case_value_ast = ast->child[1];
10239
  // Remove case_value_ast from the original AST to avoid freeing it, as it will be freed by zend_const_expr_to_zval
10240
197k
  ast->child[1] = NULL;
10241
197k
  if (enum_class->enum_backing_type != IS_UNDEF && case_value_ast == NULL) {
10242
3
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of backed enum %s must have a value",
10243
3
      ZSTR_VAL(enum_case_name),
10244
3
      ZSTR_VAL(enum_class_name));
10245
197k
  } else if (enum_class->enum_backing_type == IS_UNDEF && case_value_ast != NULL) {
10246
11
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-backed enum %s must not have a value",
10247
11
      ZSTR_VAL(enum_case_name),
10248
11
      ZSTR_VAL(enum_class_name));
10249
11
  }
10250
10251
197k
  zend_ast *const_enum_init_ast = zend_ast_create(ZEND_AST_CONST_ENUM_INIT,
10252
197k
      class_name_ast, case_id_ast, case_name_ast, case_value_ast);
10253
10254
197k
  zval value_zv;
10255
197k
  zend_const_expr_to_zval(&value_zv, &const_enum_init_ast, /* allow_dynamic */ false);
10256
10257
  /* Doc comment has been appended as second last element in ZEND_AST_ENUM ast - attributes are conventionally last */
10258
197k
  zend_ast *doc_comment_ast = ast->child[2];
10259
197k
  zend_string *doc_comment = NULL;
10260
197k
  if (doc_comment_ast) {
10261
181k
    doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
10262
181k
  }
10263
10264
197k
  zend_class_constant *c = zend_declare_class_constant_ex(enum_class, enum_case_name, &value_zv, ZEND_ACC_PUBLIC, doc_comment);
10265
197k
  ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE;
10266
197k
  zend_ast_destroy(const_enum_init_ast);
10267
10268
197k
  zend_ast *attr_ast = ast->child[3];
10269
197k
  if (attr_ast) {
10270
738
    zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
10271
10272
738
    zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
10273
10274
738
    if (deprecated) {
10275
266
      ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
10276
266
    }
10277
10278
738
    const zend_attribute *override = zend_get_attribute_str(c->attributes, "override", sizeof("override") - 1);
10279
738
    if (override) {
10280
30
      ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_OVERRIDE;
10281
      /* We need to be able to remove the flag once the override is
10282
       * resolved. See ZEND_ACC_DEPRECATED handling in
10283
       * zend_compile_class_const_decl(). */
10284
30
      enum_class->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
10285
30
      enum_class->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
10286
30
    }
10287
738
  }
10288
197k
}
10289
10290
static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
10291
3.60k
{
10292
3.60k
  switch (type) {
10293
2.59k
    case ZEND_SYMBOL_CLASS:
10294
2.59k
      if (!FC(imports)) {
10295
1.34k
        FC(imports) = emalloc(sizeof(HashTable));
10296
1.34k
        zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
10297
1.34k
      }
10298
2.59k
      return FC(imports);
10299
261
    case ZEND_SYMBOL_FUNCTION:
10300
261
      if (!FC(imports_function)) {
10301
176
        FC(imports_function) = emalloc(sizeof(HashTable));
10302
176
        zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
10303
176
      }
10304
261
      return FC(imports_function);
10305
746
    case ZEND_SYMBOL_CONST:
10306
746
      if (!FC(imports_const)) {
10307
698
        FC(imports_const) = emalloc(sizeof(HashTable));
10308
698
        zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
10309
698
      }
10310
746
      return FC(imports_const);
10311
0
    default: ZEND_UNREACHABLE();
10312
3.60k
  }
10313
10314
0
  return NULL;
10315
3.60k
}
10316
/* }}} */
10317
10318
static char *zend_get_use_type_str(uint32_t type) /* {{{ */
10319
62
{
10320
62
  switch (type) {
10321
48
    case ZEND_SYMBOL_CLASS:
10322
48
      return "";
10323
8
    case ZEND_SYMBOL_FUNCTION:
10324
8
      return " function";
10325
6
    case ZEND_SYMBOL_CONST:
10326
6
      return " const";
10327
0
    default: ZEND_UNREACHABLE();
10328
62
  }
10329
10330
0
  return " unknown";
10331
62
}
10332
/* }}} */
10333
10334
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) /* {{{ */
10335
13
{
10336
13
  if (zend_string_equals_ci(old_name, check_name)) {
10337
10
    return;
10338
10
  }
10339
10340
3
  zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
10341
3
    "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
10342
3
}
10343
/* }}} */
10344
10345
static void zend_compile_use(zend_ast *ast) /* {{{ */
10346
3.60k
{
10347
3.60k
  const zend_ast_list *list = zend_ast_get_list(ast);
10348
3.60k
  uint32_t i;
10349
3.60k
  zend_string *current_ns = FC(current_namespace);
10350
3.60k
  uint32_t type = ast->attr;
10351
3.60k
  HashTable *current_import = zend_get_import_ht(type);
10352
3.60k
  bool case_sensitive = type == ZEND_SYMBOL_CONST;
10353
10354
7.64k
  for (i = 0; i < list->children; ++i) {
10355
4.03k
    const zend_ast *use_ast = list->child[i];
10356
4.03k
    zend_ast *old_name_ast = use_ast->child[0];
10357
4.03k
    zend_ast *new_name_ast = use_ast->child[1];
10358
4.03k
    zend_string *old_name = zend_ast_get_str(old_name_ast);
10359
4.03k
    zend_string *new_name, *lookup_name;
10360
10361
4.03k
    if (new_name_ast) {
10362
489
      new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
10363
3.54k
    } else {
10364
3.54k
      const char *unqualified_name;
10365
3.54k
      size_t unqualified_name_len;
10366
3.54k
      if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
10367
        /* The form "use A\B" is equivalent to "use A\B as B" */
10368
2.17k
        new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
10369
2.17k
      } else {
10370
1.37k
        new_name = zend_string_copy(old_name);
10371
10372
1.37k
        if (!current_ns) {
10373
827
          zend_error(E_WARNING, "The use statement with non-compound name '%s' "
10374
827
            "has no effect", ZSTR_VAL(new_name));
10375
827
        }
10376
1.37k
      }
10377
3.54k
    }
10378
10379
4.03k
    if (case_sensitive) {
10380
948
      lookup_name = zend_string_copy(new_name);
10381
3.08k
    } else {
10382
3.08k
      lookup_name = zend_string_tolower(new_name);
10383
3.08k
    }
10384
10385
4.03k
    if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) {
10386
23
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
10387
23
        "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
10388
23
    }
10389
10390
4.03k
    if (
10391
4.03k
      zend_string_equals(new_name, ZSTR_CHAR('_'))
10392
3.96k
      || zend_string_equals_literal_ci(new_name, "let")
10393
3.95k
      || zend_string_equals_literal_ci(new_name, "is")
10394
4.03k
    ) {
10395
68
      switch (type) {
10396
43
        case ZEND_SYMBOL_CLASS:
10397
43
          zend_error(E_DEPRECATED, "Using \"%pS\" as a class name is deprecated", new_name);
10398
43
          break;
10399
12
        case ZEND_SYMBOL_CONST:
10400
12
          zend_error(E_DEPRECATED, "Using \"%pS\" as a constant name is deprecated since 8.6", new_name);
10401
12
          break;
10402
13
        case ZEND_SYMBOL_FUNCTION:
10403
13
          if (zend_string_equals(new_name, ZSTR_CHAR('_'))) {
10404
7
            break;
10405
7
          }
10406
6
          zend_error(E_DEPRECATED, "Using \"%pS\" as a function name is deprecated since 8.6", new_name);
10407
6
          break;
10408
0
        default: ZEND_UNREACHABLE();
10409
68
      }
10410
68
    }
10411
10412
4.03k
    if (current_ns) {
10413
2.85k
      zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
10414
2.85k
      zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
10415
2.85k
      ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
10416
2.85k
      memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1);
10417
10418
2.85k
      if (zend_have_seen_symbol(ns_name, type)) {
10419
9
        zend_check_already_in_use(type, old_name, new_name, ns_name);
10420
9
      }
10421
10422
2.85k
      zend_string_efree(ns_name);
10423
2.85k
    } else if (zend_have_seen_symbol(lookup_name, type)) {
10424
4
      zend_check_already_in_use(type, old_name, new_name, lookup_name);
10425
4
    }
10426
10427
4.03k
    zend_string_addref(old_name);
10428
4.03k
    old_name = zend_new_interned_string(old_name);
10429
4.03k
    if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
10430
59
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
10431
59
        "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
10432
59
    }
10433
10434
4.03k
    zend_string_release_ex(lookup_name, 0);
10435
4.03k
    zend_string_release_ex(new_name, 0);
10436
4.03k
  }
10437
3.60k
}
10438
/* }}} */
10439
10440
static void zend_compile_group_use(const zend_ast *ast) /* {{{ */
10441
651
{
10442
651
  uint32_t i;
10443
651
  const zend_string *ns = zend_ast_get_str(ast->child[0]);
10444
651
  const zend_ast_list *list = zend_ast_get_list(ast->child[1]);
10445
10446
2.78k
  for (i = 0; i < list->children; i++) {
10447
2.13k
    zend_ast *inline_use, *use = list->child[i];
10448
2.13k
    zval *name_zval = zend_ast_get_zval(use->child[0]);
10449
2.13k
    zend_string *name = Z_STR_P(name_zval);
10450
2.13k
    zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
10451
2.13k
    zend_string_release_ex(name, 0);
10452
2.13k
    ZVAL_STR(name_zval, compound_ns);
10453
2.13k
    inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
10454
2.13k
    inline_use->attr = ast->attr ? ast->attr : use->attr;
10455
2.13k
    zend_compile_use(inline_use);
10456
2.13k
  }
10457
651
}
10458
/* }}} */
10459
10460
static void zend_compile_const_decl(zend_ast *ast) /* {{{ */
10461
3.99k
{
10462
3.99k
  zend_ast_list *list = zend_ast_get_list(ast);
10463
3.99k
  uint32_t i;
10464
3.99k
  zend_ast *attributes_ast = NULL;
10465
3.99k
  zend_op *last_op = NULL;
10466
8.87k
  for (i = 0; i < list->children; ++i) {
10467
4.88k
    zend_ast *const_ast = list->child[i];
10468
4.88k
    if (const_ast->kind == ZEND_AST_ATTRIBUTE_LIST) {
10469
388
      ZEND_ASSERT(i == list->children - 1);
10470
388
      attributes_ast = const_ast;
10471
388
      continue;
10472
388
    }
10473
4.49k
    ZEND_ASSERT(const_ast->kind == ZEND_AST_CONST_ELEM);
10474
4.49k
    zend_ast *name_ast = const_ast->child[0];
10475
4.49k
    zend_ast **value_ast_ptr = &const_ast->child[1];
10476
4.49k
    zend_string *unqualified_name = zend_ast_get_str(name_ast);
10477
10478
4.49k
    zend_string *name;
10479
4.49k
    znode name_node, value_node;
10480
4.49k
    zval *value_zv = &value_node.u.constant;
10481
10482
4.49k
    value_node.op_type = IS_CONST;
10483
4.49k
    zend_const_expr_to_zval(value_zv, value_ast_ptr, /* allow_dynamic */ true);
10484
10485
4.49k
    if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) {
10486
1
      zend_error_noreturn(E_COMPILE_ERROR,
10487
1
        "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name));
10488
1
    }
10489
10490
4.49k
    name = zend_prefix_with_ns(unqualified_name);
10491
4.49k
    name = zend_new_interned_string(name);
10492
10493
4.49k
    if (FC(imports_const)) {
10494
651
      zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name);
10495
651
      if (import_name && !zend_string_equals(import_name, name)) {
10496
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because "
10497
1
          "the name is already in use", ZSTR_VAL(name));
10498
1
      }
10499
651
    }
10500
10501
4.49k
    name_node.op_type = IS_CONST;
10502
4.49k
    ZVAL_STR(&name_node.u.constant, name);
10503
10504
4.49k
    last_op = zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
10505
10506
4.49k
    zend_register_seen_symbol(name, ZEND_SYMBOL_CONST);
10507
4.49k
  }
10508
3.99k
  if (attributes_ast == NULL) {
10509
3.52k
    return;
10510
3.52k
  }
10511
  /* Validate: attributes can only be applied to one constant at a time
10512
   * Since we store the AST for the attributes in the list of children,
10513
   * there should be exactly 2 children. */
10514
466
  if (list->children > 2) {
10515
1
    zend_error_noreturn(
10516
1
      E_COMPILE_ERROR,
10517
1
      "Cannot apply attributes to multiple constants at once"
10518
1
    );
10519
1
  }
10520
10521
466
  HashTable *attributes = NULL;
10522
466
  zend_compile_attributes(&attributes, list->child[1], 0, ZEND_ATTRIBUTE_TARGET_CONST, 0);
10523
10524
466
  ZEND_ASSERT(last_op != NULL);
10525
466
  last_op->opcode = ZEND_DECLARE_ATTRIBUTED_CONST;
10526
380
  znode attribs_node;
10527
380
  attribs_node.op_type = IS_CONST;
10528
380
  ZVAL_PTR(&attribs_node.u.constant, attributes);
10529
380
  zend_emit_op_data(&attribs_node);
10530
380
  CG(active_op_array)->fn_flags |= ZEND_ACC_PTR_OPS;
10531
380
}
10532
/* }}}*/
10533
10534
static void zend_compile_namespace(const zend_ast *ast) /* {{{ */
10535
5.09k
{
10536
5.09k
  zend_ast *name_ast = ast->child[0];
10537
5.09k
  zend_ast *stmt_ast = ast->child[1];
10538
5.09k
  zend_string *name;
10539
5.09k
  bool with_bracket = stmt_ast != NULL;
10540
10541
  /* handle mixed syntax declaration or nested namespaces */
10542
5.09k
  if (!FC(has_bracketed_namespaces)) {
10543
3.69k
    if (FC(current_namespace)) {
10544
      /* previous namespace declarations were unbracketed */
10545
926
      if (with_bracket) {
10546
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
10547
1
          "with unbracketed namespace declarations");
10548
1
      }
10549
926
    }
10550
3.69k
  } else {
10551
    /* previous namespace declarations were bracketed */
10552
1.39k
    if (!with_bracket) {
10553
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
10554
5
        "with unbracketed namespace declarations");
10555
1.39k
    } else if (FC(current_namespace) || FC(in_namespace)) {
10556
2
      zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
10557
2
    }
10558
1.39k
  }
10559
10560
5.09k
  bool is_first_namespace = (!with_bracket && !FC(current_namespace))
10561
2.54k
    || (with_bracket && !FC(has_bracketed_namespaces));
10562
5.09k
  if (is_first_namespace && FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
10563
29
    zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
10564
29
      "the very first statement or after any declare call in the script");
10565
29
  }
10566
10567
5.09k
  if (FC(current_namespace)) {
10568
925
    zend_string_release_ex(FC(current_namespace), 0);
10569
925
  }
10570
10571
5.09k
  if (name_ast) {
10572
4.52k
    name = zend_ast_get_str(name_ast);
10573
10574
4.52k
    if (zend_string_equals_literal_ci(name, "namespace")) {
10575
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name));
10576
6
    }
10577
10578
4.52k
    FC(current_namespace) = zend_string_copy(name);
10579
4.52k
  } else {
10580
576
    FC(current_namespace) = NULL;
10581
576
  }
10582
10583
5.09k
  zend_reset_import_tables();
10584
10585
5.09k
  FC(in_namespace) = 1;
10586
5.09k
  if (with_bracket) {
10587
1.62k
    FC(has_bracketed_namespaces) = 1;
10588
1.62k
  }
10589
10590
5.09k
  if (stmt_ast) {
10591
1.62k
    zend_compile_top_stmt(stmt_ast);
10592
1.62k
    zend_end_namespace();
10593
1.62k
  }
10594
5.09k
}
10595
/* }}} */
10596
10597
static void zend_compile_halt_compiler(const zend_ast *ast) /* {{{ */
10598
17
{
10599
17
  zend_ast *offset_ast = ast->child[0];
10600
17
  zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
10601
10602
17
  const char const_name[] = "__COMPILER_HALT_OFFSET__";
10603
10604
17
  if (FC(has_bracketed_namespaces) && FC(in_namespace)) {
10605
0
    zend_error_noreturn(E_COMPILE_ERROR,
10606
0
      "__HALT_COMPILER() can only be used from the outermost scope");
10607
0
  }
10608
10609
17
  const zend_string *filename = zend_get_compiled_filename();
10610
17
  zend_string *name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
10611
17
    ZSTR_VAL(filename), ZSTR_LEN(filename), false);
10612
10613
  /* Avoid repeated declaration of the __COMPILER_HALT_OFFSET__ constant in
10614
   * case this file was already included. */
10615
17
  if (!zend_hash_find(EG(zend_constants), name)) {
10616
17
    zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0);
10617
17
  }
10618
17
  zend_string_release_ex(name, 0);
10619
17
}
10620
/* }}} */
10621
10622
static bool zend_try_ct_eval_magic_const(zval *zv, const zend_ast *ast) /* {{{ */
10623
13.5k
{
10624
13.5k
  const zend_op_array *op_array = CG(active_op_array);
10625
13.5k
  const zend_class_entry *ce = CG(active_class_entry);
10626
10627
13.5k
  switch (ast->attr) {
10628
2.09k
    case T_LINE:
10629
2.09k
      ZVAL_LONG(zv, ast->lineno);
10630
2.09k
      break;
10631
945
    case T_FILE:
10632
945
      ZVAL_STR_COPY(zv, CG(compiled_filename));
10633
945
      break;
10634
1.26k
    case T_DIR:
10635
1.26k
    {
10636
1.26k
      const zend_string *filename = CG(compiled_filename);
10637
1.26k
      zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
10638
#ifdef ZEND_WIN32
10639
      ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
10640
#else
10641
1.26k
      ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
10642
1.26k
#endif
10643
10644
1.26k
      if (zend_string_equals_literal(dirname, ".")) {
10645
1.22k
        dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
10646
1.22k
#ifdef HAVE_GETCWD
10647
1.22k
        ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN));
10648
#elif defined(HAVE_GETWD)
10649
        ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname)));
10650
#endif
10651
1.22k
        ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname));
10652
1.22k
      }
10653
10654
1.26k
      ZVAL_STR(zv, dirname);
10655
1.26k
      break;
10656
0
    }
10657
1.43k
    case T_FUNC_C:
10658
1.43k
      if (op_array && op_array->function_name) {
10659
1.19k
        ZVAL_STR_COPY(zv, op_array->function_name);
10660
1.19k
      } else {
10661
234
        ZVAL_EMPTY_STRING(zv);
10662
234
      }
10663
1.43k
      break;
10664
704
    case T_PROPERTY_C: {
10665
704
      zend_string *prop_info_name = CG(context).active_property_info_name;
10666
704
      if (prop_info_name) {
10667
381
        ZVAL_STR(zv, zend_copy_unmangled_prop_name(prop_info_name));
10668
381
      } else {
10669
323
        ZVAL_EMPTY_STRING(zv);
10670
323
      }
10671
704
      break;
10672
0
    }
10673
3.06k
    case T_METHOD_C:
10674
      /* Detect whether we are directly inside a class (e.g. a class constant) and treat
10675
       * this as not being inside a function. */
10676
3.06k
      if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
10677
273
        op_array = NULL;
10678
273
      }
10679
3.06k
      if (op_array && op_array->function_name) {
10680
2.54k
        if (op_array->scope) {
10681
447
          ZVAL_NEW_STR(zv,
10682
447
            zend_create_member_string(op_array->scope->name, op_array->function_name));
10683
2.10k
        } else {
10684
2.10k
          ZVAL_STR_COPY(zv, op_array->function_name);
10685
2.10k
        }
10686
2.54k
      } else {
10687
519
        ZVAL_EMPTY_STRING(zv);
10688
519
      }
10689
3.06k
      break;
10690
1.73k
    case T_CLASS_C:
10691
1.73k
      if (ce) {
10692
1.18k
        if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10693
746
          return 0;
10694
746
        } else {
10695
436
          ZVAL_STR_COPY(zv, ce->name);
10696
436
        }
10697
1.18k
      } else {
10698
549
        ZVAL_EMPTY_STRING(zv);
10699
549
      }
10700
985
      break;
10701
1.63k
    case T_TRAIT_C:
10702
1.63k
      if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10703
270
        ZVAL_STR_COPY(zv, ce->name);
10704
1.36k
      } else {
10705
1.36k
        ZVAL_EMPTY_STRING(zv);
10706
1.36k
      }
10707
1.63k
      break;
10708
666
    case T_NS_C:
10709
666
      if (FC(current_namespace)) {
10710
455
        ZVAL_STR_COPY(zv, FC(current_namespace));
10711
455
      } else {
10712
211
        ZVAL_EMPTY_STRING(zv);
10713
211
      }
10714
666
      break;
10715
0
    default: ZEND_UNREACHABLE();
10716
13.5k
  }
10717
10718
12.7k
  return true;
10719
13.5k
}
10720
/* }}} */
10721
10722
ZEND_API bool zend_is_op_long_compatible(const zval *op)
10723
143k
{
10724
143k
  if (Z_TYPE_P(op) == IS_ARRAY) {
10725
970
    return false;
10726
970
  }
10727
10728
142k
  if (Z_TYPE_P(op) == IS_DOUBLE
10729
40.1k
    && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval_silent(Z_DVAL_P(op)))) {
10730
32.2k
    return false;
10731
32.2k
  }
10732
10733
110k
  if (Z_TYPE_P(op) == IS_STRING) {
10734
27.9k
    double dval = 0;
10735
27.9k
    uint8_t is_num = is_numeric_str_function(Z_STR_P(op), NULL, &dval);
10736
27.9k
    if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval_silent(dval)))) {
10737
6.21k
      return false;
10738
6.21k
    }
10739
27.9k
  }
10740
10741
103k
  return true;
10742
110k
}
10743
10744
ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, const zval *op2) /* {{{ */
10745
1.19M
{
10746
1.19M
  if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) {
10747
    /* Array to string warning. */
10748
25.4k
    return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY;
10749
25.4k
  }
10750
10751
1.16M
  if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV
10752
965k
               || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR
10753
917k
               || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) {
10754
    /* Only the numeric operations throw errors. */
10755
869k
    return 0;
10756
869k
  }
10757
10758
296k
  if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) {
10759
11.0k
    if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) {
10760
      /* Adding two arrays is allowed. */
10761
7.21k
      return 0;
10762
7.21k
    }
10763
10764
    /* Numeric operators throw when one of the operands is an array. */
10765
3.83k
    return 1;
10766
11.0k
  }
10767
10768
  /* While basic arithmetic operators always produce numeric string errors,
10769
   * bitwise operators don't produce errors if both operands are strings */
10770
285k
  if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)
10771
47.3k
    && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
10772
7.28k
    return 0;
10773
7.28k
  }
10774
10775
277k
  if (Z_TYPE_P(op1) == IS_STRING
10776
34.8k
    && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) {
10777
16.4k
    return 1;
10778
16.4k
  }
10779
10780
261k
  if (Z_TYPE_P(op2) == IS_STRING
10781
24.7k
    && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) {
10782
6.13k
    return 1;
10783
6.13k
  }
10784
10785
  /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */
10786
255k
  if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR
10787
245k
      || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) {
10788
45.1k
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2)) {
10789
26.3k
      return 1;
10790
26.3k
    }
10791
45.1k
  }
10792
10793
229k
  if (opcode == ZEND_DIV && zval_get_double(op2) == 0.0) {
10794
    /* Division by zero throws an error. */
10795
1.59k
    return 1;
10796
1.59k
  }
10797
10798
  /* Mod is an operation that will cast float/float-strings to integers which might
10799
     produce float to int incompatible errors, and also cannot be divided by 0 */
10800
227k
  if (opcode == ZEND_MOD) {
10801
26.2k
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2) || zval_get_long(op2) == 0) {
10802
23.0k
      return 1;
10803
23.0k
    }
10804
26.2k
  }
10805
10806
204k
  if ((opcode == ZEND_POW) && zval_get_double(op1) == 0 && zval_get_double(op2) < 0) {
10807
    /* 0 ** (<0) throws a division by zero error. */
10808
227
    return 1;
10809
227
  }
10810
204k
  if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) {
10811
    /* Shift by negative number throws an error. */
10812
1.04k
    return 1;
10813
1.04k
  }
10814
10815
203k
  return 0;
10816
204k
}
10817
/* }}} */
10818
10819
static inline bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
10820
1.17M
{
10821
1.17M
  if (zend_binary_op_produces_error(opcode, op1, op2)) {
10822
72.6k
    return false;
10823
72.6k
  }
10824
10825
1.10M
  const binary_op_type fn = get_binary_op(opcode);
10826
1.10M
  fn(result, op1, op2);
10827
1.10M
  return true;
10828
1.17M
}
10829
/* }}} */
10830
10831
ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op)
10832
655k
{
10833
655k
  if (opcode == ZEND_BW_NOT) {
10834
    /* BW_NOT on string does not convert the string into an integer. */
10835
11.1k
    if (Z_TYPE_P(op) == IS_STRING) {
10836
1.36k
      return 0;
10837
1.36k
    }
10838
9.80k
    return Z_TYPE_P(op) <= IS_TRUE || !zend_is_op_long_compatible(op);
10839
11.1k
  }
10840
  /* Can happen when called from zend_optimizer_eval_unary_op() */
10841
644k
  if (opcode == ZEND_BOOL || opcode == ZEND_BOOL_NOT) {
10842
    /* ZEND_BOOL/ZEND_BOOL_NOT warns when casting NAN. */
10843
644k
    return Z_TYPE_P(op) == IS_DOUBLE;
10844
644k
  }
10845
10846
0
  return 0;
10847
644k
}
10848
10849
static inline bool zend_try_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */
10850
654k
{
10851
654k
  if (zend_unary_op_produces_error(opcode, op)) {
10852
45.4k
    return false;
10853
45.4k
  }
10854
10855
608k
  const unary_op_type fn = get_unary_op(opcode);
10856
608k
  fn(result, op);
10857
608k
  return true;
10858
654k
}
10859
/* }}} */
10860
10861
static inline bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
10862
123k
{
10863
123k
  zval right;
10864
123k
  ZVAL_LONG(&right, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10865
123k
  return zend_try_ct_eval_binary_op(result, ZEND_MUL, op, &right);
10866
123k
}
10867
/* }}} */
10868
10869
static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
10870
58.6k
{
10871
58.6k
  const binary_op_type fn = kind == ZEND_AST_GREATER
10872
58.6k
    ? is_smaller_function : is_smaller_or_equal_function;
10873
58.6k
  fn(result, op2, op1);
10874
58.6k
}
10875
/* }}} */
10876
10877
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
10878
2.89M
{
10879
2.89M
  const zend_ast_list *list = zend_ast_get_list(ast);
10880
2.89M
  zend_ast *last_elem_ast = NULL;
10881
2.89M
  uint32_t i;
10882
2.89M
  bool is_constant = true;
10883
10884
2.89M
  if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
10885
11
    zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
10886
11
  }
10887
10888
  /* First ensure that *all* child nodes are constant and by-val */
10889
5.97M
  for (i = 0; i < list->children; ++i) {
10890
3.07M
    zend_ast *elem_ast = list->child[i];
10891
10892
3.07M
    if (elem_ast == NULL) {
10893
      /* Report error at line of last non-empty element */
10894
143
      if (last_elem_ast) {
10895
85
        CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast);
10896
85
      }
10897
143
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
10898
143
    }
10899
10900
3.07M
    if (elem_ast->kind != ZEND_AST_UNPACK) {
10901
2.88M
      zend_eval_const_expr(&elem_ast->child[0]);
10902
2.88M
      zend_eval_const_expr(&elem_ast->child[1]);
10903
10904
2.88M
      if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL
10905
236k
        || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
10906
2.88M
      ) {
10907
2.64M
        is_constant = false;
10908
2.64M
      }
10909
2.88M
    } else {
10910
194k
      zend_eval_const_expr(&elem_ast->child[0]);
10911
10912
194k
      if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) {
10913
192k
        is_constant = false;
10914
192k
      }
10915
194k
    }
10916
10917
3.07M
    last_elem_ast = elem_ast;
10918
3.07M
  }
10919
10920
2.89M
  if (!is_constant) {
10921
2.78M
    return false;
10922
2.78M
  }
10923
10924
119k
  if (!list->children) {
10925
21.8k
    ZVAL_EMPTY_ARRAY(result);
10926
21.8k
    return true;
10927
21.8k
  }
10928
10929
97.6k
  array_init_size(result, list->children);
10930
258k
  for (i = 0; i < list->children; ++i) {
10931
163k
    const zend_ast *elem_ast = list->child[i];
10932
163k
    zend_ast *value_ast = elem_ast->child[0];
10933
163k
    zend_ast *key_ast;
10934
10935
163k
    zval *value = zend_ast_get_zval(value_ast);
10936
163k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
10937
1.46k
      if (Z_TYPE_P(value) == IS_ARRAY) {
10938
1.45k
        const HashTable *ht = Z_ARRVAL_P(value);
10939
1.45k
        zval *val;
10940
1.45k
        zend_string *key;
10941
10942
6.54k
        ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
10943
6.54k
          if (key) {
10944
452
            zend_hash_update(Z_ARRVAL_P(result), key, val);
10945
722
          } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
10946
0
            zval_ptr_dtor(result);
10947
0
            return 0;
10948
0
          }
10949
1.17k
          Z_TRY_ADDREF_P(val);
10950
1.17k
        } ZEND_HASH_FOREACH_END();
10951
10952
1.45k
        continue;
10953
1.45k
      } else {
10954
14
        zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked, %s given", zend_zval_value_name(value));
10955
14
      }
10956
1.46k
    }
10957
10958
162k
    Z_TRY_ADDREF_P(value);
10959
10960
162k
    key_ast = elem_ast->child[1];
10961
162k
    if (key_ast) {
10962
26.4k
      const zval *key = zend_ast_get_zval(key_ast);
10963
26.4k
      switch (Z_TYPE_P(key)) {
10964
9.40k
        case IS_LONG:
10965
9.40k
          zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value);
10966
9.40k
          break;
10967
13.2k
        case IS_STRING:
10968
13.2k
          zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value);
10969
13.2k
          break;
10970
1.64k
        case IS_DOUBLE: {
10971
1.64k
          zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key));
10972
          /* Incompatible float will generate an error, leave this to run-time */
10973
1.64k
          if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) {
10974
1.02k
            goto fail;
10975
1.02k
          }
10976
618
          zend_hash_index_update(Z_ARRVAL_P(result), lval, value);
10977
618
          break;
10978
1.64k
        }
10979
351
        case IS_FALSE:
10980
351
          zend_hash_index_update(Z_ARRVAL_P(result), 0, value);
10981
351
          break;
10982
245
        case IS_TRUE:
10983
245
          zend_hash_index_update(Z_ARRVAL_P(result), 1, value);
10984
245
          break;
10985
1.47k
        case IS_NULL:
10986
          /* Null key will generate a warning at run-time. */
10987
1.47k
          goto fail;
10988
2
        default:
10989
2
          zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type");
10990
26.4k
      }
10991
135k
    } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) {
10992
2.54k
fail:
10993
2.54k
      zval_ptr_dtor_nogc(value);
10994
2.54k
      zval_ptr_dtor(result);
10995
2.54k
      return 0;
10996
34
    }
10997
162k
  }
10998
10999
95.1k
  return true;
11000
97.6k
}
11001
/* }}} */
11002
11003
static void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */
11004
5.87M
{
11005
5.87M
  zend_ast *left_ast = ast->child[0];
11006
5.87M
  zend_ast *right_ast = ast->child[1];
11007
5.87M
  uint32_t opcode = ast->attr;
11008
11009
5.87M
  znode left_node, right_node;
11010
11011
5.87M
  zend_compile_expr(&left_node, left_ast);
11012
5.87M
  zend_compile_expr(&right_node, right_ast);
11013
11014
5.87M
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
11015
1.00M
    if (zend_try_ct_eval_binary_op(&result->u.constant, opcode,
11016
1.00M
        &left_node.u.constant, &right_node.u.constant)
11017
1.00M
    ) {
11018
978k
      result->op_type = IS_CONST;
11019
978k
      zval_ptr_dtor(&left_node.u.constant);
11020
978k
      zval_ptr_dtor(&right_node.u.constant);
11021
978k
      return;
11022
978k
    }
11023
1.00M
  }
11024
11025
4.90M
  do {
11026
4.90M
    if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) {
11027
      /* 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) */
11028
259k
      if (left_node.op_type == IS_CONST) {
11029
92.0k
        if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) {
11030
44.6k
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL);
11031
44.6k
          opline->extended_value =
11032
44.6k
            (opcode == ZEND_IS_IDENTICAL) ?
11033
2.30k
              (1 << Z_TYPE(left_node.u.constant)) :
11034
44.6k
              (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant)));
11035
44.6k
          return;
11036
44.6k
        }
11037
167k
      } else if (right_node.op_type == IS_CONST) {
11038
167k
        if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) {
11039
108k
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL);
11040
108k
          opline->extended_value =
11041
108k
            (opcode == ZEND_IS_IDENTICAL) ?
11042
58.8k
              (1 << Z_TYPE(right_node.u.constant)) :
11043
108k
              (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant)));
11044
108k
          return;
11045
108k
        }
11046
167k
      }
11047
4.64M
    } else if (opcode == ZEND_CONCAT) {
11048
      /* convert constant operands to strings at compile-time */
11049
64.2k
      if (left_node.op_type == IS_CONST) {
11050
5.69k
        if (Z_TYPE(left_node.u.constant) == IS_ARRAY) {
11051
533
          zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING;
11052
5.16k
        } else {
11053
5.16k
          convert_to_string(&left_node.u.constant);
11054
5.16k
        }
11055
5.69k
      }
11056
64.2k
      if (right_node.op_type == IS_CONST) {
11057
10.5k
        if (Z_TYPE(right_node.u.constant) == IS_ARRAY) {
11058
870
          zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING;
11059
9.72k
        } else {
11060
9.72k
          convert_to_string(&right_node.u.constant);
11061
9.72k
        }
11062
10.5k
      }
11063
64.2k
      if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
11064
0
        opcode = ZEND_FAST_CONCAT;
11065
0
      }
11066
64.2k
    }
11067
4.74M
    zend_emit_op_tmp(result, opcode, &left_node, &right_node);
11068
4.74M
  } while (0);
11069
4.90M
}
11070
/* }}} */
11071
11072
/* We do not use zend_compile_binary_op for this because we want to retain the left-to-right
11073
 * evaluation order. */
11074
static void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */
11075
473k
{
11076
473k
  zend_ast *left_ast = ast->child[0];
11077
473k
  zend_ast *right_ast = ast->child[1];
11078
473k
  znode left_node, right_node;
11079
11080
473k
  ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL);
11081
11082
473k
  zend_compile_expr(&left_node, left_ast);
11083
473k
  zend_compile_expr(&right_node, right_ast);
11084
11085
473k
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
11086
57.0k
    result->op_type = IS_CONST;
11087
57.0k
    zend_ct_eval_greater(&result->u.constant, ast->kind,
11088
57.0k
      &left_node.u.constant, &right_node.u.constant);
11089
57.0k
    zval_ptr_dtor(&left_node.u.constant);
11090
57.0k
    zval_ptr_dtor(&right_node.u.constant);
11091
57.0k
    return;
11092
57.0k
  }
11093
11094
416k
  zend_emit_op_tmp(result,
11095
416k
    ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL,
11096
416k
    &right_node, &left_node);
11097
416k
}
11098
/* }}} */
11099
11100
static void zend_compile_unary_op(znode *result, const zend_ast *ast) /* {{{ */
11101
1.96M
{
11102
1.96M
  zend_ast *expr_ast = ast->child[0];
11103
1.96M
  uint32_t opcode = ast->attr;
11104
11105
1.96M
  znode expr_node;
11106
1.96M
  zend_compile_expr(&expr_node, expr_ast);
11107
11108
1.96M
  if (expr_node.op_type == IS_CONST
11109
647k
      && zend_try_ct_eval_unary_op(&result->u.constant, opcode, &expr_node.u.constant)) {
11110
603k
    result->op_type = IS_CONST;
11111
603k
    zval_ptr_dtor(&expr_node.u.constant);
11112
603k
    return;
11113
603k
  }
11114
11115
1.36M
  zend_emit_op_tmp(result, opcode, &expr_node, NULL);
11116
1.36M
}
11117
/* }}} */
11118
11119
static void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */
11120
854k
{
11121
854k
  zend_ast *expr_ast = ast->child[0];
11122
854k
  znode expr_node, right_node;
11123
11124
854k
  ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS);
11125
11126
854k
  zend_compile_expr(&expr_node, expr_ast);
11127
11128
854k
  if (expr_node.op_type == IS_CONST
11129
117k
    && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) {
11130
106k
    result->op_type = IS_CONST;
11131
106k
    zval_ptr_dtor(&expr_node.u.constant);
11132
106k
    return;
11133
106k
  }
11134
11135
747k
  right_node.op_type = IS_CONST;
11136
747k
  ZVAL_LONG(&right_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
11137
747k
  zend_emit_op_tmp(result, ZEND_MUL, &expr_node, &right_node);
11138
747k
}
11139
/* }}} */
11140
11141
static void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */
11142
10.9k
{
11143
10.9k
  zend_ast *left_ast = ast->child[0];
11144
10.9k
  zend_ast *right_ast = ast->child[1];
11145
11146
10.9k
  znode left_node, right_node;
11147
10.9k
  zend_op *opline_jmpz, *opline_bool;
11148
10.9k
  uint32_t opnum_jmpz;
11149
11150
10.9k
  ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR);
11151
11152
10.9k
  zend_compile_expr(&left_node, left_ast);
11153
11154
10.9k
  if (left_node.op_type == IS_CONST) {
11155
4.11k
    if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant))
11156
2.63k
     || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) {
11157
1.91k
      result->op_type = IS_CONST;
11158
1.91k
      ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant));
11159
2.20k
    } else {
11160
2.20k
      zend_compile_expr(&right_node, right_ast);
11161
11162
2.20k
      if (right_node.op_type == IS_CONST) {
11163
1.25k
        result->op_type = IS_CONST;
11164
1.25k
        ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant));
11165
11166
1.25k
        zval_ptr_dtor(&right_node.u.constant);
11167
1.25k
      } else {
11168
947
        zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL);
11169
947
      }
11170
2.20k
    }
11171
11172
4.11k
    zval_ptr_dtor(&left_node.u.constant);
11173
4.11k
    return;
11174
4.11k
  }
11175
11176
6.85k
  opnum_jmpz = get_next_op_number();
11177
6.85k
  opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
11178
6.85k
    &left_node, NULL);
11179
11180
6.85k
  if (left_node.op_type == IS_TMP_VAR) {
11181
6.59k
    SET_NODE(opline_jmpz->result, &left_node);
11182
6.59k
    GET_NODE(result, opline_jmpz->result);
11183
6.59k
  } else {
11184
253
    zend_make_tmp_result(result, opline_jmpz);
11185
253
  }
11186
11187
6.85k
  zend_compile_expr(&right_node, right_ast);
11188
11189
6.85k
  opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
11190
6.85k
  SET_NODE(opline_bool->result, result);
11191
11192
6.85k
  zend_update_jump_target_to_next(opnum_jmpz);
11193
6.85k
}
11194
/* }}} */
11195
11196
static void zend_compile_post_incdec(znode *result, const zend_ast *ast) /* {{{ */
11197
3.94k
{
11198
3.94k
  zend_ast *var_ast = ast->child[0];
11199
3.94k
  ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
11200
11201
3.94k
  zend_ensure_writable_variable(var_ast);
11202
11203
3.94k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
11204
833
    zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, false);
11205
833
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
11206
833
    zend_make_tmp_result(result, opline);
11207
3.11k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
11208
402
    zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, false, false);
11209
402
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP;
11210
402
    zend_make_tmp_result(result, opline);
11211
2.71k
  } else {
11212
2.71k
    znode var_node;
11213
2.71k
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
11214
2.71k
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
11215
404
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
11216
404
    }
11217
2.71k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC,
11218
2.71k
      &var_node, NULL);
11219
2.71k
  }
11220
3.94k
}
11221
/* }}} */
11222
11223
static void zend_compile_pre_incdec(znode *result, const zend_ast *ast) /* {{{ */
11224
2.03k
{
11225
2.03k
  zend_ast *var_ast = ast->child[0];
11226
2.03k
  ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
11227
11228
2.03k
  zend_ensure_writable_variable(var_ast);
11229
11230
2.03k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
11231
463
    zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, false);
11232
463
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;
11233
463
    opline->result_type = IS_TMP_VAR;
11234
463
    result->op_type = IS_TMP_VAR;
11235
1.56k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
11236
314
    zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, false, false);
11237
314
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP;
11238
314
    opline->result_type = IS_TMP_VAR;
11239
314
    result->op_type = IS_TMP_VAR;
11240
1.25k
  } else {
11241
1.25k
    znode var_node;
11242
1.25k
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
11243
1.25k
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
11244
80
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
11245
80
    }
11246
1.25k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC,
11247
1.25k
      &var_node, NULL);
11248
1.25k
  }
11249
2.03k
}
11250
/* }}} */
11251
11252
static void zend_compile_cast(znode *result, const zend_ast *ast) /* {{{ */
11253
1.28k
{
11254
1.28k
  zend_ast *expr_ast = ast->child[0];
11255
1.28k
  znode expr_node;
11256
1.28k
  zend_op *opline;
11257
11258
1.28k
  zend_compile_expr(&expr_node, expr_ast);
11259
11260
1.28k
  if (ast->attr == _IS_BOOL) {
11261
129
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL);
11262
1.15k
  } else if (ast->attr == IS_NULL) {
11263
17
    zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
11264
1.14k
  } else {
11265
1.14k
    opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
11266
1.14k
    opline->extended_value = ast->attr;
11267
1.14k
  }
11268
1.28k
}
11269
/* }}} */
11270
11271
static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */
11272
1.92k
{
11273
1.92k
  zend_ast *cond_ast = ast->child[0];
11274
1.92k
  zend_ast *false_ast = ast->child[2];
11275
11276
1.92k
  znode cond_node, false_node;
11277
1.92k
  zend_op *opline_qm_assign;
11278
1.92k
  uint32_t opnum_jmp_set;
11279
11280
1.92k
  ZEND_ASSERT(ast->child[1] == NULL);
11281
11282
1.92k
  zend_compile_expr(&cond_node, cond_ast);
11283
11284
1.92k
  opnum_jmp_set = get_next_op_number();
11285
1.92k
  zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL);
11286
11287
1.92k
  zend_compile_expr(&false_node, false_ast);
11288
11289
1.92k
  opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
11290
1.92k
  SET_NODE(opline_qm_assign->result, result);
11291
11292
1.92k
  zend_update_jump_target_to_next(opnum_jmp_set);
11293
1.92k
}
11294
/* }}} */
11295
11296
static void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
11297
5.92k
{
11298
5.92k
  zend_ast *cond_ast = ast->child[0];
11299
5.92k
  zend_ast *true_ast = ast->child[1];
11300
5.92k
  zend_ast *false_ast = ast->child[2];
11301
11302
5.92k
  znode cond_node, true_node, false_node;
11303
5.92k
  zend_op *opline_qm_assign2;
11304
5.92k
  uint32_t opnum_jmpz, opnum_jmp;
11305
11306
5.92k
  if (cond_ast->kind == ZEND_AST_CONDITIONAL
11307
1.17k
      && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) {
11308
612
    if (cond_ast->child[1]) {
11309
34
      if (true_ast) {
11310
24
        zend_error(E_COMPILE_ERROR,
11311
24
          "Unparenthesized `a ? b : c ? d : e` is not supported. "
11312
24
          "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`");
11313
24
      } else {
11314
10
        zend_error(E_COMPILE_ERROR,
11315
10
          "Unparenthesized `a ? b : c ?: d` is not supported. "
11316
10
          "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`");
11317
10
      }
11318
578
    } else {
11319
578
      if (true_ast) {
11320
6
        zend_error(E_COMPILE_ERROR,
11321
6
          "Unparenthesized `a ?: b ? c : d` is not supported. "
11322
6
          "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`");
11323
572
      } else {
11324
        /* This case is harmless:  (a ?: b) ?: c always produces the same result
11325
         * as a ?: (b ?: c). */
11326
572
      }
11327
578
    }
11328
612
  }
11329
11330
5.92k
  if (!true_ast) {
11331
1.92k
    zend_compile_shorthand_conditional(result, ast);
11332
1.92k
    return;
11333
1.92k
  }
11334
11335
3.99k
  zend_compile_expr(&cond_node, cond_ast);
11336
11337
3.99k
  opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
11338
11339
3.99k
  zend_compile_expr(&true_node, true_ast);
11340
11341
3.99k
  zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL);
11342
11343
3.99k
  opnum_jmp = zend_emit_jump(0);
11344
11345
3.99k
  zend_update_jump_target_to_next(opnum_jmpz);
11346
11347
3.99k
  zend_compile_expr(&false_node, false_ast);
11348
11349
3.99k
  opline_qm_assign2 = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
11350
3.99k
  SET_NODE(opline_qm_assign2->result, result);
11351
11352
3.99k
  zend_update_jump_target_to_next(opnum_jmp);
11353
3.99k
}
11354
/* }}} */
11355
11356
static void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
11357
1.88M
{
11358
1.88M
  zend_ast *expr_ast = ast->child[0];
11359
1.88M
  zend_ast *default_ast = ast->child[1];
11360
11361
1.88M
  znode expr_node, default_node;
11362
1.88M
  zend_op *opline;
11363
1.88M
  uint32_t opnum;
11364
11365
1.88M
  zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, false);
11366
11367
1.88M
  opnum = get_next_op_number();
11368
1.88M
  zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL);
11369
11370
1.88M
  zend_compile_expr(&default_node, default_ast);
11371
11372
1.88M
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL);
11373
1.88M
  SET_NODE(opline->result, result);
11374
11375
1.88M
  opline = &CG(active_op_array)->opcodes[opnum];
11376
1.88M
  opline->op2.opline_num = get_next_op_number();
11377
1.88M
}
11378
/* }}} */
11379
11380
695k
static void znode_dtor(zval *zv) {
11381
695k
  znode *node = Z_PTR_P(zv);
11382
695k
  if (node->op_type == IS_CONST) {
11383
456k
    zval_ptr_dtor_nogc(&node->u.constant);
11384
456k
  }
11385
695k
  efree(node);
11386
695k
}
11387
11388
static void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */
11389
104k
{
11390
104k
  zend_ast *var_ast = ast->child[0];
11391
104k
  zend_ast *default_ast = ast->child[1];
11392
11393
104k
  znode var_node_is, var_node_w, default_node, assign_node, *node;
11394
104k
  zend_op *opline;
11395
104k
  uint32_t coalesce_opnum;
11396
104k
  bool need_frees = false;
11397
11398
  /* Remember expressions compiled during the initial BP_VAR_IS lookup,
11399
   * to avoid double-evaluation when we compile again with BP_VAR_W. */
11400
104k
  HashTable *orig_memoized_exprs = CG(memoized_exprs);
11401
104k
  const zend_memoize_mode orig_memoize_mode = CG(memoize_mode);
11402
11403
104k
  zend_ensure_writable_variable(var_ast);
11404
104k
  if (is_this_fetch(var_ast)) {
11405
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
11406
1
  }
11407
11408
104k
  ALLOC_HASHTABLE(CG(memoized_exprs));
11409
104k
  zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0);
11410
11411
104k
  CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
11412
104k
  zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, false);
11413
11414
104k
  coalesce_opnum = get_next_op_number();
11415
104k
  zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL);
11416
11417
104k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
11418
104k
  if (var_ast->kind == ZEND_AST_DIM) {
11419
87.1k
    zend_compile_expr_with_potential_assign_to_self(&default_node, default_ast, var_ast);
11420
87.1k
  } else {
11421
17.5k
    zend_compile_expr(&default_node, default_ast);
11422
17.5k
  }
11423
11424
104k
  CG(memoize_mode) = ZEND_MEMOIZE_FETCH;
11425
104k
  zend_compile_var(&var_node_w, var_ast, BP_VAR_W, false);
11426
11427
  /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */
11428
104k
  opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
11429
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
11430
104k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
11431
104k
  switch (kind) {
11432
2.23k
    case ZEND_AST_VAR:
11433
2.23k
      zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node);
11434
2.23k
      break;
11435
974
    case ZEND_AST_STATIC_PROP:
11436
974
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
11437
974
      opline->result_type = IS_TMP_VAR;
11438
974
      var_node_w.op_type = IS_TMP_VAR;
11439
974
      zend_emit_op_data(&default_node);
11440
974
      assign_node = var_node_w;
11441
974
      break;
11442
86.5k
    case ZEND_AST_DIM:
11443
86.5k
      opline->opcode = ZEND_ASSIGN_DIM;
11444
86.5k
      opline->result_type = IS_TMP_VAR;
11445
86.5k
      var_node_w.op_type = IS_TMP_VAR;
11446
86.5k
      zend_emit_op_data(&default_node);
11447
86.5k
      assign_node = var_node_w;
11448
86.5k
      break;
11449
14.6k
    case ZEND_AST_PROP:
11450
14.6k
    case ZEND_AST_NULLSAFE_PROP:
11451
14.6k
      opline->opcode = ZEND_ASSIGN_OBJ;
11452
14.6k
      opline->result_type = IS_TMP_VAR;
11453
14.6k
      var_node_w.op_type = IS_TMP_VAR;
11454
14.6k
      zend_emit_op_data(&default_node);
11455
14.6k
      assign_node = var_node_w;
11456
14.6k
      break;
11457
0
    default: ZEND_UNREACHABLE();
11458
104k
  }
11459
11460
104k
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL);
11461
104k
  SET_NODE(opline->result, result);
11462
11463
1.19M
  ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
11464
1.19M
    if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
11465
86.9k
      need_frees = true;
11466
86.9k
      break;
11467
86.9k
    }
11468
1.19M
  } ZEND_HASH_FOREACH_END();
11469
11470
  /* Free DUPed expressions if there are any */
11471
104k
  if (need_frees) {
11472
86.9k
    uint32_t jump_opnum = zend_emit_jump(0);
11473
86.9k
    zend_update_jump_target_to_next(coalesce_opnum);
11474
568k
    ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
11475
568k
      if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
11476
238k
        zend_emit_op(NULL, ZEND_FREE, node, NULL);
11477
238k
      }
11478
568k
    } ZEND_HASH_FOREACH_END();
11479
86.9k
    zend_update_jump_target_to_next(jump_opnum);
11480
86.9k
  } else {
11481
17.5k
    zend_update_jump_target_to_next(coalesce_opnum);
11482
17.5k
  }
11483
11484
104k
  zend_hash_destroy(CG(memoized_exprs));
11485
104k
  FREE_HASHTABLE(CG(memoized_exprs));
11486
104k
  CG(memoized_exprs) = orig_memoized_exprs;
11487
104k
  CG(memoize_mode) = orig_memoize_mode;
11488
104k
}
11489
/* }}} */
11490
11491
static void zend_compile_print(znode *result, const zend_ast *ast) /* {{{ */
11492
7.23k
{
11493
7.23k
  zend_op *opline;
11494
7.23k
  zend_ast *expr_ast = ast->child[0];
11495
11496
7.23k
  znode expr_node;
11497
7.23k
  zend_compile_expr(&expr_node, expr_ast);
11498
11499
7.23k
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
11500
7.23k
  opline->extended_value = 1;
11501
11502
7.23k
  result->op_type = IS_CONST;
11503
7.23k
  ZVAL_LONG(&result->u.constant, 1);
11504
7.23k
}
11505
/* }}} */
11506
11507
static void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
11508
672k
{
11509
672k
  zend_ast *value_ast = ast->child[0];
11510
672k
  zend_ast *key_ast = ast->child[1];
11511
11512
672k
  znode value_node, key_node;
11513
672k
  znode *value_node_ptr = NULL, *key_node_ptr = NULL;
11514
672k
  zend_op *opline;
11515
672k
  bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
11516
11517
672k
  zend_mark_function_as_generator();
11518
11519
672k
  if (key_ast) {
11520
396
    zend_compile_expr(&key_node, key_ast);
11521
396
    key_node_ptr = &key_node;
11522
396
  }
11523
11524
672k
  if (value_ast) {
11525
668k
    if (returns_by_ref && zend_is_variable_or_call(value_ast)) {
11526
4.71k
      zend_assert_not_short_circuited(value_ast);
11527
4.71k
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
11528
663k
    } else {
11529
663k
      zend_compile_expr(&value_node, value_ast);
11530
663k
    }
11531
668k
    value_node_ptr = &value_node;
11532
668k
  }
11533
11534
672k
  opline = zend_emit_op_tmp(result, ZEND_YIELD, value_node_ptr, key_node_ptr);
11535
11536
672k
  if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
11537
3.59k
    opline->extended_value = ZEND_RETURNS_FUNCTION;
11538
3.59k
  }
11539
672k
}
11540
/* }}} */
11541
11542
static void zend_compile_yield_from(znode *result, const zend_ast *ast) /* {{{ */
11543
1.00k
{
11544
1.00k
  zend_ast *expr_ast = ast->child[0];
11545
1.00k
  znode expr_node;
11546
11547
1.00k
  zend_mark_function_as_generator();
11548
11549
1.00k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
11550
4
    zend_error_noreturn(E_COMPILE_ERROR,
11551
4
      "Cannot use \"yield from\" inside a by-reference generator");
11552
4
  }
11553
11554
1.00k
  zend_compile_expr(&expr_node, expr_ast);
11555
1.00k
  zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
11556
1.00k
}
11557
/* }}} */
11558
11559
static void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */
11560
467
{
11561
467
  zend_ast *obj_ast = ast->child[0];
11562
467
  zend_ast *class_ast = ast->child[1];
11563
11564
467
  znode obj_node, class_node;
11565
467
  zend_op *opline;
11566
11567
467
  zend_compile_expr(&obj_node, obj_ast);
11568
467
  if (obj_node.op_type == IS_CONST) {
11569
104
    zend_do_free(&obj_node);
11570
104
    result->op_type = IS_CONST;
11571
104
    ZVAL_FALSE(&result->u.constant);
11572
104
    return;
11573
104
  }
11574
11575
363
  zend_compile_class_ref(&class_node, class_ast,
11576
363
    ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION | ZEND_FETCH_CLASS_SILENT);
11577
11578
363
  opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL);
11579
11580
363
  if (class_node.op_type == IS_CONST) {
11581
110
    opline->op2_type = IS_CONST;
11582
110
    opline->op2.constant = zend_add_class_name_literal(
11583
110
      Z_STR(class_node.u.constant));
11584
110
    opline->extended_value = zend_alloc_cache_slot();
11585
253
  } else {
11586
253
    SET_NODE(opline->op2, &class_node);
11587
253
  }
11588
363
}
11589
/* }}} */
11590
11591
static void zend_compile_include_or_eval(znode *result, const zend_ast *ast) /* {{{ */
11592
3.07k
{
11593
3.07k
  zend_ast *expr_ast = ast->child[0];
11594
3.07k
  znode expr_node;
11595
3.07k
  zend_op *opline;
11596
11597
3.07k
  zend_do_extended_fcall_begin();
11598
3.07k
  zend_compile_expr(&expr_node, expr_ast);
11599
11600
3.07k
  opline = zend_emit_op_tmp(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL);
11601
3.07k
  opline->extended_value = ast->attr;
11602
11603
3.07k
  zend_do_extended_fcall_end();
11604
3.07k
}
11605
/* }}} */
11606
11607
static void zend_compile_isset_or_empty(znode *result, const zend_ast *ast) /* {{{ */
11608
7.19k
{
11609
7.19k
  zend_ast *var_ast = ast->child[0];
11610
11611
7.19k
  znode var_node;
11612
7.19k
  zend_op *opline = NULL;
11613
11614
7.19k
  ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY);
11615
11616
7.19k
  if (!zend_is_variable(var_ast)) {
11617
109
    if (ast->kind == ZEND_AST_EMPTY) {
11618
      /* empty(expr) can be transformed to !expr */
11619
76
      zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast);
11620
76
      zend_compile_expr(result, not_ast);
11621
76
      return;
11622
76
    } else {
11623
33
      zend_error_noreturn(E_COMPILE_ERROR,
11624
33
        "Cannot use isset() on the result of an expression "
11625
33
        "(you can use \"null !== expression\" instead)");
11626
33
    }
11627
109
  }
11628
11629
7.11k
  if (is_globals_fetch(var_ast)) {
11630
527
    result->op_type = IS_CONST;
11631
527
    ZVAL_BOOL(&result->u.constant, ast->kind == ZEND_AST_ISSET);
11632
527
    return;
11633
527
  }
11634
11635
6.59k
  if (is_global_var_fetch(var_ast)) {
11636
1.40k
    if (!var_ast->child[1]) {
11637
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
11638
7
    }
11639
11640
1.40k
    zend_compile_expr(&var_node, var_ast->child[1]);
11641
1.40k
    if (var_node.op_type == IS_CONST) {
11642
1.08k
      convert_to_string(&var_node.u.constant);
11643
1.08k
    }
11644
11645
1.40k
    opline = zend_emit_op_tmp(result, ZEND_ISSET_ISEMPTY_VAR, &var_node, NULL);
11646
1.40k
    opline->extended_value =
11647
1.40k
      ZEND_FETCH_GLOBAL | (ast->kind == ZEND_AST_EMPTY ? ZEND_ISEMPTY : 0);
11648
1.40k
    return;
11649
1.40k
  }
11650
11651
5.18k
  zend_short_circuiting_mark_inner(var_ast);
11652
5.18k
  switch (var_ast->kind) {
11653
3.48k
    case ZEND_AST_VAR:
11654
3.48k
      if (is_this_fetch(var_ast)) {
11655
67
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL);
11656
67
        CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
11657
3.41k
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_IS) == SUCCESS) {
11658
2.86k
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL);
11659
2.86k
      } else {
11660
547
        opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, false);
11661
547
        opline->opcode = ZEND_ISSET_ISEMPTY_VAR;
11662
547
      }
11663
3.48k
      break;
11664
1.16k
    case ZEND_AST_DIM:
11665
1.16k
      opline = zend_compile_dim(result, var_ast, BP_VAR_IS, /* by_ref */ false);
11666
1.16k
      opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
11667
1.16k
      break;
11668
66
    case ZEND_AST_PROP:
11669
171
    case ZEND_AST_NULLSAFE_PROP:
11670
171
      opline = zend_compile_prop(result, var_ast, BP_VAR_IS, false);
11671
171
      opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
11672
171
      break;
11673
336
    case ZEND_AST_STATIC_PROP:
11674
336
      opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, false, false);
11675
336
      opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP;
11676
336
      break;
11677
0
    default: ZEND_UNREACHABLE();
11678
5.18k
  }
11679
11680
5.13k
  result->op_type = opline->result_type = IS_TMP_VAR;
11681
5.13k
  if (!(ast->kind == ZEND_AST_ISSET)) {
11682
919
    opline->extended_value |= ZEND_ISEMPTY;
11683
919
  }
11684
5.13k
}
11685
/* }}} */
11686
11687
static void zend_compile_silence(znode *result, const zend_ast *ast) /* {{{ */
11688
18.9M
{
11689
18.9M
  zend_ast *expr_ast = ast->child[0];
11690
18.9M
  znode silence_node;
11691
11692
18.9M
  zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL);
11693
11694
18.9M
  if (expr_ast->kind == ZEND_AST_VAR) {
11695
    /* For @$var we need to force a FETCH instruction, otherwise the CV access will
11696
     * happen outside the silenced section. */
11697
89.5k
    zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, false );
11698
18.9M
  } else {
11699
18.9M
    zend_compile_expr(result, expr_ast);
11700
18.9M
  }
11701
11702
18.9M
  zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL);
11703
18.9M
}
11704
/* }}} */
11705
11706
static void zend_compile_shell_exec(znode *result, const zend_ast *ast) /* {{{ */
11707
72.2k
{
11708
72.2k
  zend_ast *expr_ast = ast->child[0];
11709
11710
72.2k
  zval fn_name;
11711
72.2k
  zend_ast *name_ast, *args_ast, *call_ast;
11712
11713
72.2k
  zend_error(E_DEPRECATED, "The backtick (`) operator is deprecated, use shell_exec() instead");
11714
11715
72.2k
  ZVAL_STRING(&fn_name, "shell_exec");
11716
72.2k
  name_ast = zend_ast_create_zval(&fn_name);
11717
72.2k
  args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast);
11718
72.2k
  call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast);
11719
11720
72.2k
  zend_compile_expr(result, call_ast);
11721
11722
72.2k
  zval_ptr_dtor(&fn_name);
11723
72.2k
}
11724
/* }}} */
11725
11726
static void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
11727
117k
{
11728
117k
  zend_ast_list *list = zend_ast_get_list(ast);
11729
117k
  zend_op *opline;
11730
117k
  uint32_t i, opnum_init = -1;
11731
117k
  bool packed = true;
11732
11733
117k
  if (zend_try_ct_eval_array(&result->u.constant, ast)) {
11734
68.3k
    result->op_type = IS_CONST;
11735
68.3k
    return;
11736
68.3k
  }
11737
11738
  /* Empty arrays are handled at compile-time */
11739
49.6k
  ZEND_ASSERT(list->children > 0);
11740
11741
163k
  for (i = 0; i < list->children; ++i) {
11742
113k
    zend_ast *elem_ast = list->child[i];
11743
113k
    zend_ast *value_ast, *key_ast;
11744
113k
    bool by_ref;
11745
113k
    znode value_node, key_node, *key_node_ptr = NULL;
11746
11747
113k
    if (elem_ast == NULL) {
11748
0
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
11749
0
    }
11750
11751
113k
    value_ast = elem_ast->child[0];
11752
11753
113k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
11754
4.00k
      zend_compile_expr(&value_node, value_ast);
11755
4.00k
      if (i == 0) {
11756
3.08k
        opnum_init = get_next_op_number();
11757
3.08k
        opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
11758
3.08k
      }
11759
4.00k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL);
11760
4.00k
      SET_NODE(opline->result, result);
11761
4.00k
      continue;
11762
4.00k
    }
11763
11764
109k
    key_ast = elem_ast->child[1];
11765
109k
    by_ref = elem_ast->attr;
11766
11767
109k
    if (key_ast) {
11768
12.2k
      zend_compile_expr(&key_node, key_ast);
11769
12.2k
      zend_handle_numeric_op(&key_node);
11770
12.2k
      key_node_ptr = &key_node;
11771
12.2k
    }
11772
11773
109k
    if (by_ref) {
11774
1.55k
      zend_ensure_writable_variable(value_ast);
11775
1.55k
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
11776
108k
    } else {
11777
108k
      zend_compile_expr(&value_node, value_ast);
11778
108k
    }
11779
11780
109k
    if (i == 0) {
11781
46.3k
      opnum_init = get_next_op_number();
11782
46.3k
      opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr);
11783
46.3k
      opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT;
11784
63.6k
    } else {
11785
63.6k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT,
11786
63.6k
        &value_node, key_node_ptr);
11787
63.6k
      SET_NODE(opline->result, result);
11788
63.6k
    }
11789
109k
    opline->extended_value |= by_ref;
11790
11791
109k
    if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) {
11792
5.83k
      packed = false;
11793
5.83k
    }
11794
109k
  }
11795
11796
  /* Add a flag to INIT_ARRAY if we know this array cannot be packed */
11797
49.4k
  if (!packed) {
11798
2.69k
    ZEND_ASSERT(opnum_init != (uint32_t)-1);
11799
2.69k
    opline = &CG(active_op_array)->opcodes[opnum_init];
11800
2.69k
    opline->extended_value |= ZEND_ARRAY_NOT_PACKED;
11801
2.69k
  }
11802
49.4k
}
11803
/* }}} */
11804
11805
static void zend_emit_fetch_constant(znode *result, zend_string *resolved_name, bool unqualified_in_namespace, uint8_t type)
11806
11.6M
{
11807
11.6M
  zend_op *opline = zend_emit_op(result, ZEND_FETCH_CONSTANT, NULL, NULL);
11808
11.6M
  if (type == BP_VAR_R || type == BP_VAR_IS) {
11809
11.6M
    opline->result_type = IS_TMP_VAR;
11810
11.6M
    result->op_type = IS_TMP_VAR;
11811
11.6M
  }
11812
11813
11.6M
  opline->op2_type = IS_CONST;
11814
11815
11.6M
  if (unqualified_in_namespace) {
11816
11.4M
    opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE;
11817
11.4M
    opline->op2.constant = zend_add_const_name_literal(resolved_name, true);
11818
11.4M
  } else {
11819
191k
    opline->op1.num = 0;
11820
191k
    opline->op2.constant = zend_add_const_name_literal(resolved_name, false);
11821
191k
  }
11822
11.6M
  opline->extended_value = zend_alloc_cache_slot();
11823
11.6M
}
11824
11825
static void zend_compile_const(znode *result, const zend_ast *ast, uint8_t type) /* {{{ */
11826
11.7M
{
11827
11.7M
  zend_ast *name_ast = ast->child[0];
11828
11829
11.7M
  bool is_fully_qualified;
11830
11.7M
  zend_string *orig_name = zend_ast_get_str(name_ast);
11831
11.7M
  zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified);
11832
11833
11.7M
  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__"))) {
11834
1.14k
    zend_ast *last = CG(ast);
11835
11836
2.28k
    while (last && last->kind == ZEND_AST_STMT_LIST) {
11837
1.21k
      const zend_ast_list *list = zend_ast_get_list(last);
11838
1.21k
      if (list->children == 0) {
11839
69
        break;
11840
69
      }
11841
1.14k
      last = list->child[list->children-1];
11842
1.14k
    }
11843
1.14k
    if (last && last->kind == ZEND_AST_HALT_COMPILER) {
11844
132
      result->op_type = IS_CONST;
11845
132
      ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
11846
132
      zend_string_release_ex(resolved_name, 0);
11847
132
      return;
11848
132
    }
11849
1.14k
  }
11850
11851
11.7M
  if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
11852
42.8k
    result->op_type = IS_CONST;
11853
42.8k
    zend_string_release_ex(resolved_name, 0);
11854
42.8k
    return;
11855
42.8k
  }
11856
11857
11.6M
  zend_emit_fetch_constant(result, resolved_name,
11858
11.6M
    !is_fully_qualified && FC(current_namespace), type);
11859
11.6M
}
11860
/* }}} */
11861
11862
static void zend_compile_constant(znode *result, zend_ast *ast, uint8_t type)
11863
6
{
11864
6
  zend_string *name = zend_ast_get_constant_name(ast);
11865
11866
6
  zend_emit_fetch_constant(result, zend_string_copy(name),
11867
6
    (ast->attr & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) != 0, type);
11868
6
}
11869
11870
static void zend_compile_class_const(znode *result, zend_ast *ast, uint8_t type) /* {{{ */
11871
41.8k
{
11872
41.8k
  zend_ast *class_ast;
11873
41.8k
  zend_ast *const_ast;
11874
41.8k
  znode class_node, const_node;
11875
41.8k
  zend_op *opline;
11876
11877
41.8k
  zend_eval_const_expr(&ast->child[0]);
11878
41.8k
  zend_eval_const_expr(&ast->child[1]);
11879
11880
41.8k
  class_ast = ast->child[0];
11881
41.8k
  const_ast = ast->child[1];
11882
11883
41.8k
  if (class_ast->kind == ZEND_AST_ZVAL && const_ast->kind == ZEND_AST_ZVAL) {
11884
15.3k
    zval *const_zv = zend_ast_get_zval(const_ast);
11885
15.3k
    if (Z_TYPE_P(const_zv) == IS_STRING) {
11886
14.8k
      zend_string *const_str = Z_STR_P(const_zv);
11887
14.8k
      zend_string *resolved_name = zend_resolve_class_name_ast(class_ast);
11888
14.8k
      if (zend_try_ct_eval_class_const(&result->u.constant, resolved_name, const_str)) {
11889
5
        result->op_type = IS_CONST;
11890
5
        zend_string_release_ex(resolved_name, 0);
11891
5
        return;
11892
5
      }
11893
14.8k
      zend_string_release_ex(resolved_name, 0);
11894
14.8k
    }
11895
15.3k
  }
11896
11897
41.8k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
11898
11899
41.8k
  zend_compile_expr(&const_node, const_ast);
11900
11901
41.8k
  opline = zend_emit_op(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node);
11902
41.8k
  if (type == BP_VAR_R || type == BP_VAR_IS) {
11903
41.3k
    opline->result_type = IS_TMP_VAR;
11904
41.3k
    result->op_type = IS_TMP_VAR;
11905
41.3k
  }
11906
11907
41.8k
  zend_set_class_name_op1(opline, &class_node);
11908
11909
41.8k
  if (opline->op1_type == IS_CONST || opline->op2_type == IS_CONST) {
11910
40.9k
    opline->extended_value = zend_alloc_cache_slots(2);
11911
40.9k
  }
11912
41.8k
}
11913
/* }}} */
11914
11915
static void zend_compile_class_name(znode *result, const zend_ast *ast) /* {{{ */
11916
3.36k
{
11917
3.36k
  zend_ast *class_ast = ast->child[0];
11918
11919
3.36k
  if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) {
11920
512
    result->op_type = IS_CONST;
11921
512
    return;
11922
512
  }
11923
11924
2.84k
  if (class_ast->kind == ZEND_AST_ZVAL) {
11925
1.37k
    zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11926
1.37k
    opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
11927
1.47k
  } else {
11928
1.47k
    znode expr_node;
11929
1.47k
    zend_compile_expr(&expr_node, class_ast);
11930
1.47k
    if (expr_node.op_type == IS_CONST) {
11931
      /* Unlikely case that happen if class_ast is constant folded.
11932
       * Handle it here, to avoid needing a CONST specialization in the VM. */
11933
11
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"::class\" on %s",
11934
11
        zend_zval_value_name(&expr_node.u.constant));
11935
11
    }
11936
11937
1.47k
    zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL);
11938
1.47k
  }
11939
2.84k
}
11940
/* }}} */
11941
11942
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */
11943
143k
{
11944
143k
  if (num == 0) {
11945
27.7k
    result->op_type = IS_TMP_VAR;
11946
27.7k
    result->u.op.var = -1;
11947
27.7k
    opline->opcode = ZEND_ROPE_INIT;
11948
115k
  } else {
11949
115k
    opline->opcode = ZEND_ROPE_ADD;
11950
115k
    SET_NODE(opline->op1, result);
11951
115k
  }
11952
143k
  SET_NODE(opline->op2, elem_node);
11953
143k
  SET_NODE(opline->result, result);
11954
143k
  opline->extended_value = num;
11955
143k
  return opline;
11956
143k
}
11957
/* }}} */
11958
11959
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */
11960
171k
{
11961
171k
  zend_op *opline = get_next_op();
11962
11963
171k
  if (num == 0) {
11964
47.6k
    result->op_type = IS_TMP_VAR;
11965
47.6k
    result->u.op.var = -1;
11966
47.6k
    opline->opcode = ZEND_ROPE_INIT;
11967
123k
  } else {
11968
123k
    opline->opcode = ZEND_ROPE_ADD;
11969
123k
    SET_NODE(opline->op1, result);
11970
123k
  }
11971
171k
  SET_NODE(opline->op2, elem_node);
11972
171k
  SET_NODE(opline->result, result);
11973
171k
  opline->extended_value = num;
11974
171k
  return opline;
11975
171k
}
11976
/* }}} */
11977
11978
static void zend_compile_rope_finalize(znode *result, uint32_t rope_elements, zend_op *init_opline, zend_op *opline)
11979
75.4k
{
11980
75.4k
  if (rope_elements == 1) {
11981
2.43k
    if (opline->op2_type == IS_CONST) {
11982
682
      GET_NODE(result, opline->op2);
11983
682
      ZVAL_UNDEF(CT_CONSTANT(opline->op2));
11984
682
      SET_UNUSED(opline->op2);
11985
682
      MAKE_NOP(opline);
11986
1.75k
    } else {
11987
1.75k
      opline->opcode = ZEND_CAST;
11988
1.75k
      opline->extended_value = IS_STRING;
11989
1.75k
      opline->op1_type = opline->op2_type;
11990
1.75k
      opline->op1 = opline->op2;
11991
1.75k
      SET_UNUSED(opline->op2);
11992
1.75k
      zend_make_tmp_result(result, opline);
11993
1.75k
    }
11994
73.0k
  } else if (rope_elements == 2) {
11995
1.80k
    opline->opcode = ZEND_FAST_CONCAT;
11996
1.80k
    opline->extended_value = 0;
11997
1.80k
    opline->op1_type = init_opline->op2_type;
11998
1.80k
    opline->op1 = init_opline->op2;
11999
1.80k
    zend_make_tmp_result(result, opline);
12000
1.80k
    MAKE_NOP(init_opline);
12001
71.2k
  } else {
12002
71.2k
    uint32_t var;
12003
12004
71.2k
    init_opline->extended_value = rope_elements;
12005
71.2k
    opline->opcode = ZEND_ROPE_END;
12006
71.2k
    zend_make_tmp_result(result, opline);
12007
71.2k
    var = opline->op1.var = get_temporary_variable();
12008
12009
    /* Allocates the necessary number of zval slots to keep the rope */
12010
71.2k
    uint32_t i = ((rope_elements * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
12011
188k
    while (i > 1) {
12012
117k
      get_temporary_variable();
12013
117k
      i--;
12014
117k
    }
12015
12016
    /* Update all the previous opcodes to use the same variable */
12017
393k
    while (opline != init_opline) {
12018
322k
      opline--;
12019
322k
      if (opline->opcode == ZEND_ROPE_ADD &&
12020
167k
          opline->result.var == (uint32_t)-1) {
12021
165k
        opline->op1.var = var;
12022
165k
        opline->result.var = var;
12023
165k
      } else if (opline->opcode == ZEND_ROPE_INIT &&
12024
71.7k
                 opline->result.var == (uint32_t)-1) {
12025
71.2k
        opline->result.var = var;
12026
71.2k
      }
12027
322k
    }
12028
71.2k
  }
12029
75.4k
}
12030
12031
static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
12032
73.7k
{
12033
73.7k
  uint32_t i, j;
12034
73.7k
  uint32_t rope_init_lineno = -1;
12035
73.7k
  zend_op *opline = NULL, *init_opline;
12036
73.7k
  znode elem_node, last_const_node;
12037
73.7k
  zend_ast_list *list = zend_ast_get_list(ast);
12038
73.7k
  uint32_t reserved_op_number = -1;
12039
12040
73.7k
  ZEND_ASSERT(list->children > 0);
12041
12042
73.7k
  j = 0;
12043
73.7k
  last_const_node.op_type = IS_UNUSED;
12044
384k
  for (i = 0; i < list->children; i++) {
12045
310k
    zend_ast *encaps_var = list->child[i];
12046
12047
310k
    if (encaps_var->attr & (ZEND_ENCAPS_VAR_DOLLAR_CURLY|ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
12048
110k
      if ((encaps_var->kind == ZEND_AST_VAR || encaps_var->kind == ZEND_AST_DIM) && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY)) {
12049
855
        zend_error(E_DEPRECATED, "Using ${var} in strings is deprecated, use {$var} instead");
12050
109k
      } else if (encaps_var->kind == ZEND_AST_VAR && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
12051
109k
        zend_error(E_DEPRECATED, "Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead");
12052
109k
      }
12053
110k
    }
12054
12055
310k
    zend_compile_expr(&elem_node, encaps_var);
12056
12057
310k
    if (elem_node.op_type == IS_CONST) {
12058
143k
      convert_to_string(&elem_node.u.constant);
12059
12060
143k
      if (Z_STRLEN(elem_node.u.constant) == 0) {
12061
443
        zval_ptr_dtor(&elem_node.u.constant);
12062
143k
      } else if (last_const_node.op_type == IS_CONST) {
12063
0
        concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant);
12064
0
        zval_ptr_dtor(&elem_node.u.constant);
12065
143k
      } else {
12066
143k
        last_const_node.op_type = IS_CONST;
12067
143k
        ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant);
12068
        /* Reserve place for ZEND_ROPE_ADD instruction */
12069
143k
        reserved_op_number = get_next_op_number();
12070
143k
        opline = get_next_op();
12071
143k
        opline->opcode = ZEND_NOP;
12072
143k
      }
12073
143k
      continue;
12074
166k
    } else {
12075
166k
      if (j == 0) {
12076
73.7k
        if (last_const_node.op_type == IS_CONST) {
12077
27.7k
          rope_init_lineno = reserved_op_number;
12078
46.0k
        } else {
12079
46.0k
          rope_init_lineno = get_next_op_number();
12080
46.0k
        }
12081
73.7k
      }
12082
166k
      if (last_const_node.op_type == IS_CONST) {
12083
72.9k
        opline = &CG(active_op_array)->opcodes[reserved_op_number];
12084
72.9k
        zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
12085
72.9k
        last_const_node.op_type = IS_UNUSED;
12086
72.9k
      }
12087
166k
      opline = zend_compile_rope_add(result, j++, &elem_node);
12088
166k
    }
12089
310k
  }
12090
12091
73.7k
  if (j == 0) {
12092
0
    result->op_type = IS_CONST;
12093
0
    if (last_const_node.op_type == IS_CONST) {
12094
0
      ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant);
12095
0
    } else {
12096
0
      ZVAL_EMPTY_STRING(&result->u.constant);
12097
      /* empty string */
12098
0
    }
12099
0
    CG(active_op_array)->last = reserved_op_number - 1;
12100
0
    return;
12101
73.7k
  } else if (last_const_node.op_type == IS_CONST) {
12102
70.1k
    opline = &CG(active_op_array)->opcodes[reserved_op_number];
12103
70.1k
    opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
12104
70.1k
  }
12105
73.7k
  init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
12106
73.7k
  zend_compile_rope_finalize(result, j, init_opline, opline);
12107
73.7k
}
12108
/* }}} */
12109
12110
static void zend_compile_magic_const(znode *result, const zend_ast *ast) /* {{{ */
12111
11.4k
{
12112
11.4k
  zend_op *opline;
12113
12114
11.4k
  if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) {
12115
10.9k
    result->op_type = IS_CONST;
12116
10.9k
    return;
12117
10.9k
  }
12118
12119
552
  ZEND_ASSERT(ast->attr == T_CLASS_C &&
12120
552
              CG(active_class_entry) &&
12121
552
              (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
12122
12123
552
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
12124
552
  opline->op1.num = ZEND_FETCH_CLASS_SELF;
12125
552
}
12126
/* }}} */
12127
12128
static bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */
12129
240k
{
12130
240k
  return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP
12131
233k
    || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL
12132
232k
    || kind == ZEND_AST_AND || kind == ZEND_AST_OR
12133
232k
    || kind == ZEND_AST_UNARY_OP
12134
232k
    || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS
12135
231k
    || kind == ZEND_AST_CAST
12136
230k
    || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM
12137
229k
    || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM
12138
227k
    || kind == ZEND_AST_UNPACK
12139
227k
    || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST
12140
206k
    || kind == ZEND_AST_CLASS_NAME
12141
206k
    || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE
12142
205k
    || kind == ZEND_AST_CONST_ENUM_INIT
12143
8.64k
    || kind == ZEND_AST_NEW || kind == ZEND_AST_ARG_LIST
12144
5.33k
    || kind == ZEND_AST_NAMED_ARG
12145
5.21k
    || kind == ZEND_AST_PROP || kind == ZEND_AST_NULLSAFE_PROP
12146
4.75k
    || kind == ZEND_AST_CLOSURE
12147
4.55k
    || kind == ZEND_AST_CALL || kind == ZEND_AST_STATIC_CALL || kind == ZEND_AST_CALLABLE_CONVERT
12148
1.52k
    || kind == ZEND_AST_PLACEHOLDER_ARG;
12149
240k
}
12150
/* }}} */
12151
12152
static void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
12153
2.17k
{
12154
2.17k
  zend_ast *ast = *ast_ptr;
12155
2.17k
  zend_ast *class_ast = ast->child[0];
12156
2.17k
  zend_string *class_name;
12157
2.17k
  int fetch_type;
12158
12159
2.17k
  if (class_ast->kind != ZEND_AST_ZVAL) {
12160
15
    zend_error_noreturn(E_COMPILE_ERROR,
12161
15
      "Dynamic class names are not allowed in compile-time class constant references");
12162
15
  }
12163
2.17k
  if (Z_TYPE_P(zend_ast_get_zval(class_ast)) != IS_STRING) {
12164
7
    zend_throw_error(NULL, "Class name must be a valid object or a string");
12165
7
  }
12166
12167
2.17k
  class_name = zend_ast_get_str(class_ast);
12168
2.17k
  fetch_type = zend_get_class_fetch_type(class_name);
12169
12170
2.17k
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
12171
6
    zend_error_noreturn(E_COMPILE_ERROR,
12172
6
      "\"static::\" is not allowed in compile-time constants");
12173
6
  }
12174
12175
2.17k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
12176
1.56k
    zend_string *tmp = zend_resolve_class_name_ast(class_ast);
12177
12178
1.56k
    zend_string_release_ex(class_name, 0);
12179
1.56k
    if (tmp != class_name) {
12180
554
      zval *zv = zend_ast_get_zval(class_ast);
12181
554
      ZVAL_STR(zv, tmp);
12182
554
      class_ast->attr = ZEND_NAME_FQ;
12183
554
    }
12184
1.56k
  }
12185
12186
2.17k
  ast->attr |= ZEND_FETCH_CLASS_EXCEPTION;
12187
2.17k
}
12188
/* }}} */
12189
12190
static void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */
12191
51
{
12192
51
  zend_ast *ast = *ast_ptr;
12193
51
  zend_ast *class_ast = ast->child[0];
12194
51
  if (class_ast->kind != ZEND_AST_ZVAL) {
12195
6
    zend_error_noreturn(E_COMPILE_ERROR,
12196
6
      "(expression)::class cannot be used in constant expressions");
12197
6
  }
12198
12199
51
  zend_string *class_name = zend_ast_get_str(class_ast);
12200
51
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
12201
12202
51
  switch (fetch_type) {
12203
34
    case ZEND_FETCH_CLASS_SELF:
12204
44
    case ZEND_FETCH_CLASS_PARENT:
12205
      /* For the const-eval representation store the fetch type instead of the name. */
12206
44
      zend_string_release(class_name);
12207
44
      ast->child[0] = NULL;
12208
44
      ast->attr = fetch_type;
12209
44
      return;
12210
1
    case ZEND_FETCH_CLASS_STATIC:
12211
1
      zend_error_noreturn(E_COMPILE_ERROR,
12212
1
        "static::class cannot be used for compile-time class name resolution");
12213
1
    default: ZEND_UNREACHABLE();
12214
51
  }
12215
51
}
12216
12217
static void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */
12218
18.3k
{
12219
18.3k
  zend_ast *ast = *ast_ptr;
12220
18.3k
  zend_ast *name_ast = ast->child[0];
12221
18.3k
  zend_string *orig_name = zend_ast_get_str(name_ast);
12222
18.3k
  bool is_fully_qualified;
12223
18.3k
  zval result;
12224
18.3k
  zend_string *resolved_name;
12225
12226
18.3k
  CG(zend_lineno) = zend_ast_get_lineno(ast);
12227
12228
18.3k
  resolved_name = zend_resolve_const_name(
12229
18.3k
    orig_name, name_ast->attr, &is_fully_qualified);
12230
12231
18.3k
  if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
12232
0
    zend_string_release_ex(resolved_name, 0);
12233
0
    zend_ast_destroy(ast);
12234
0
    *ast_ptr = zend_ast_create_zval(&result);
12235
0
    return;
12236
0
  }
12237
12238
18.3k
  zend_ast_destroy(ast);
12239
18.3k
  *ast_ptr = zend_ast_create_constant(resolved_name,
12240
18.3k
    !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0);
12241
18.3k
}
12242
/* }}} */
12243
12244
static void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
12245
194
{
12246
194
  zend_ast *ast = *ast_ptr;
12247
12248
  /* Other cases already resolved by constant folding */
12249
194
  ZEND_ASSERT(ast->attr == T_CLASS_C);
12250
12251
194
  zend_ast_destroy(ast);
12252
194
  *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS);
12253
194
}
12254
/* }}} */
12255
12256
static void zend_compile_const_expr_class_reference(zend_ast *class_ast)
12257
985
{
12258
985
  if (class_ast->kind == ZEND_AST_CLASS) {
12259
0
    zend_error_noreturn(E_COMPILE_ERROR,
12260
0
      "Cannot use anonymous class in constant expression");
12261
0
  }
12262
985
  if (class_ast->kind != ZEND_AST_ZVAL) {
12263
1
    zend_error_noreturn(E_COMPILE_ERROR,
12264
1
      "Cannot use dynamic class name in constant expression");
12265
1
  }
12266
12267
985
  zend_string *class_name = zend_resolve_class_name_ast(class_ast);
12268
985
  int fetch_type = zend_get_class_fetch_type(class_name);
12269
985
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
12270
1
    zend_error_noreturn(E_COMPILE_ERROR,
12271
1
      "\"static\" is not allowed in compile-time constants");
12272
1
  }
12273
12274
985
  zval *class_ast_zv = zend_ast_get_zval(class_ast);
12275
985
  zval_ptr_dtor_nogc(class_ast_zv);
12276
985
  ZVAL_STR(class_ast_zv, class_name);
12277
985
  class_ast->attr = fetch_type << ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT;
12278
985
}
12279
12280
static void zend_compile_const_expr_new(zend_ast **ast_ptr)
12281
912
{
12282
912
  zend_ast *class_ast = (*ast_ptr)->child[0];
12283
912
  zend_compile_const_expr_class_reference(class_ast);
12284
12285
912
  const zend_ast *args_ast = (*ast_ptr)->child[1];
12286
912
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
12287
6
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
12288
6
  }
12289
912
}
12290
12291
static void zend_compile_const_expr_closure(zend_ast **ast_ptr)
12292
205
{
12293
205
  zend_ast_decl *closure_ast = (zend_ast_decl *) *ast_ptr;
12294
205
  const zend_ast *uses_ast = closure_ast->child[1];
12295
205
  if (!(closure_ast->flags & ZEND_ACC_STATIC)) {
12296
2
    zend_error_noreturn(E_COMPILE_ERROR,
12297
2
      "Closures in constant expressions must be static");
12298
2
  }
12299
205
  if (uses_ast) {
12300
1
    zend_error_noreturn(E_COMPILE_ERROR,
12301
1
      "Cannot use(...) variables in constant expression");
12302
1
  }
12303
12304
205
  znode node;
12305
205
  zend_op_array *op = zend_compile_func_decl(&node, (zend_ast*)closure_ast, FUNC_DECL_LEVEL_CONSTEXPR);
12306
12307
205
  zend_ast_destroy(*ast_ptr);
12308
205
  *ast_ptr = zend_ast_create_op_array(op);
12309
205
}
12310
12311
static void zend_compile_const_expr_fcc(zend_ast **ast_ptr)
12312
1.53k
{
12313
1.53k
  zend_ast *args_ast = zend_ast_call_get_args(*ast_ptr);
12314
12315
1.53k
  if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT) {
12316
32
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
12317
32
  }
12318
12319
1.53k
  zend_ast_fcc *fcc = (zend_ast_fcc*)args_ast;
12320
12321
1.53k
  zend_ast_list *args = zend_ast_get_list(fcc->args);
12322
1.53k
  bool is_fcc = args->children == 1 && args->child[0]->attr == ZEND_PLACEHOLDER_VARIADIC;
12323
12324
1.53k
  if (!is_fcc) {
12325
548
    fcc->filename = zend_string_copy(CG(active_op_array)->filename);
12326
548
    fcc->name = zend_compile_partial_name(CG(active_op_array), fcc->lineno);
12327
548
  }
12328
12329
1.53k
  switch ((*ast_ptr)->kind) {
12330
1.42k
    case ZEND_AST_CALL: {
12331
1.42k
      zend_ast *name_ast = (*ast_ptr)->child[0];
12332
1.42k
      if (name_ast->kind != ZEND_AST_ZVAL) {
12333
3
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic function name in constant expression");
12334
3
      }
12335
1.42k
      zval *name_ast_zv = zend_ast_get_zval(name_ast);
12336
1.42k
      if (Z_TYPE_P(name_ast_zv) != IS_STRING) {
12337
3
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal function name");
12338
3
      }
12339
1.42k
      bool is_fully_qualified;
12340
1.42k
      zend_string *name = zend_resolve_function_name(Z_STR_P(name_ast_zv), name_ast->attr, &is_fully_qualified);
12341
1.42k
      zval_ptr_dtor_nogc(name_ast_zv);
12342
1.42k
      ZVAL_STR(name_ast_zv, name);
12343
1.42k
      if (is_fully_qualified) {
12344
695
        name_ast->attr = ZEND_NAME_FQ;
12345
695
      }
12346
1.42k
      break;
12347
0
    }
12348
73
    case ZEND_AST_STATIC_CALL: {
12349
73
      zend_ast *class_ast = (*ast_ptr)->child[0];
12350
73
      zend_compile_const_expr_class_reference(class_ast);
12351
73
      zend_ast *method_ast = (*ast_ptr)->child[1];
12352
73
      if (method_ast->kind != ZEND_AST_ZVAL) {
12353
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic method name in constant expression");
12354
1
      }
12355
73
      if (Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING) {
12356
0
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal method name");
12357
0
      }
12358
73
      break;
12359
0
    }
12360
0
    default: ZEND_UNREACHABLE();
12361
1.53k
  }
12362
1.53k
}
12363
12364
static void zend_compile_const_expr_args(zend_ast **ast_ptr)
12365
2.39k
{
12366
2.39k
  zend_ast_list *list = zend_ast_get_list(*ast_ptr);
12367
2.39k
  bool uses_named_args = false;
12368
2.39k
  bool uses_variadic_placeholder = false;
12369
4.73k
  for (uint32_t i = 0; i < list->children; i++) {
12370
2.33k
    const zend_ast *arg = list->child[i];
12371
2.33k
    if (arg->kind == ZEND_AST_UNPACK) {
12372
1
      zend_error_noreturn(E_COMPILE_ERROR,
12373
1
        "Argument unpacking in constant expressions is not supported");
12374
1
    }
12375
2.33k
    if (arg->kind == ZEND_AST_NAMED_ARG) {
12376
127
      uses_named_args = true;
12377
127
      if (uses_variadic_placeholder) {
12378
1
        zend_error_noreturn(E_COMPILE_ERROR, "Variadic placeholder must be last");
12379
1
      }
12380
2.21k
    } else {
12381
2.21k
      bool is_variadic_placeholder = arg->kind == ZEND_AST_PLACEHOLDER_ARG
12382
1.49k
        && arg->attr == ZEND_PLACEHOLDER_VARIADIC;
12383
12384
2.21k
      if (uses_named_args && !is_variadic_placeholder) {
12385
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use positional argument after named argument");
12386
1
      }
12387
12388
2.21k
      if (uses_variadic_placeholder) {
12389
2
        if (is_variadic_placeholder) {
12390
1
          zend_error_noreturn(E_COMPILE_ERROR, "Variadic placeholder may only appear once");
12391
1
        } else {
12392
1
          zend_error_noreturn(E_COMPILE_ERROR, "Variadic placeholder must be last");
12393
1
        }
12394
2
      }
12395
12396
2.21k
      if (is_variadic_placeholder) {
12397
983
        uses_variadic_placeholder = true;
12398
983
      }
12399
2.21k
    }
12400
2.33k
  }
12401
12402
2.39k
  if (uses_named_args) {
12403
117
    list->attr = 1;
12404
117
  }
12405
2.39k
}
12406
12407
typedef struct {
12408
  /* Whether the value of this expression may differ on each evaluation. */
12409
  bool allow_dynamic;
12410
} const_expr_context;
12411
12412
static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */
12413
1.12M
{
12414
1.12M
  const const_expr_context *ctx = context;
12415
1.12M
  zend_ast *ast = *ast_ptr;
12416
1.12M
  if (ast == NULL || ast->kind == ZEND_AST_ZVAL) {
12417
880k
    return;
12418
880k
  }
12419
12420
240k
  if (!zend_is_allowed_in_const_expr(ast->kind)) {
12421
40
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
12422
40
  }
12423
12424
240k
  switch (ast->kind) {
12425
2.17k
    case ZEND_AST_CLASS_CONST:
12426
2.17k
      zend_compile_const_expr_class_const(ast_ptr);
12427
2.17k
      break;
12428
51
    case ZEND_AST_CLASS_NAME:
12429
51
      zend_compile_const_expr_class_name(ast_ptr);
12430
51
      break;
12431
18.3k
    case ZEND_AST_CONST:
12432
18.3k
      zend_compile_const_expr_const(ast_ptr);
12433
18.3k
      break;
12434
194
    case ZEND_AST_MAGIC_CONST:
12435
194
      zend_compile_const_expr_magic_const(ast_ptr);
12436
194
      break;
12437
600
    case ZEND_AST_CAST:
12438
600
      if (ast->attr == IS_OBJECT && !ctx->allow_dynamic) {
12439
9
        zend_error_noreturn(E_COMPILE_ERROR,
12440
9
          "Object casts are not supported in this context");
12441
9
      }
12442
600
      break;
12443
913
    case ZEND_AST_NEW:
12444
913
      if (!ctx->allow_dynamic) {
12445
1
        zend_error_noreturn(E_COMPILE_ERROR,
12446
1
          "New expressions are not supported in this context");
12447
1
      }
12448
913
      zend_compile_const_expr_new(ast_ptr);
12449
913
      break;
12450
2.39k
    case ZEND_AST_ARG_LIST:
12451
2.39k
      zend_compile_const_expr_args(ast_ptr);
12452
2.39k
      break;
12453
205
    case ZEND_AST_CLOSURE:
12454
205
      zend_compile_const_expr_closure(ast_ptr);
12455
      /* Return, because we do not want to traverse the children. */
12456
205
      return;
12457
1.45k
    case ZEND_AST_CALL:
12458
1.53k
    case ZEND_AST_STATIC_CALL:
12459
1.53k
      zend_compile_const_expr_fcc(ast_ptr);
12460
1.53k
      break;
12461
240k
  }
12462
12463
239k
  zend_ast_apply(ast, zend_compile_const_expr, context);
12464
239k
}
12465
/* }}} */
12466
12467
void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynamic) /* {{{ */
12468
274k
{
12469
274k
  const_expr_context context;
12470
274k
  context.allow_dynamic = allow_dynamic;
12471
12472
274k
  zend_eval_const_expr(ast_ptr);
12473
274k
  zend_compile_const_expr(ast_ptr, &context);
12474
274k
  if ((*ast_ptr)->kind != ZEND_AST_ZVAL) {
12475
    /* Replace with compiled AST zval representation. */
12476
212k
    zval ast_zv;
12477
212k
    ZVAL_AST(&ast_zv, zend_ast_copy(*ast_ptr));
12478
212k
    zend_ast_destroy(*ast_ptr);
12479
212k
    *ast_ptr = zend_ast_create_zval(&ast_zv);
12480
212k
  }
12481
274k
  ZVAL_COPY(result, zend_ast_get_zval(*ast_ptr));
12482
274k
}
12483
/* }}} */
12484
12485
/* Same as compile_stmt, but with early binding */
12486
void zend_compile_top_stmt(zend_ast *ast) /* {{{ */
12487
344k
{
12488
344k
  if (!ast) {
12489
32.5k
    return;
12490
32.5k
  }
12491
12492
311k
  if (ast->kind == ZEND_AST_STMT_LIST) {
12493
67.8k
    const zend_ast_list *list = zend_ast_get_list(ast);
12494
67.8k
    uint32_t i;
12495
352k
    for (i = 0; i < list->children; ++i) {
12496
284k
      zend_compile_top_stmt(list->child[i]);
12497
284k
    }
12498
67.8k
    return;
12499
67.8k
  }
12500
12501
243k
  if (ast->kind == ZEND_AST_FUNC_DECL) {
12502
1.74k
    CG(zend_lineno) = ast->lineno;
12503
1.74k
    zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_TOPLEVEL);
12504
1.74k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
12505
241k
  } else if (ast->kind == ZEND_AST_CLASS) {
12506
23.1k
    CG(zend_lineno) = ast->lineno;
12507
23.1k
    zend_compile_class_decl(NULL, ast, true);
12508
23.1k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
12509
218k
  } else {
12510
218k
    zend_compile_stmt(ast);
12511
218k
  }
12512
243k
  if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) {
12513
233k
    zend_verify_namespace();
12514
233k
  }
12515
243k
}
12516
/* }}} */
12517
12518
static void zend_compile_stmt(zend_ast *ast) /* {{{ */
12519
11.7M
{
12520
11.7M
  if (!ast) {
12521
617k
    return;
12522
617k
  }
12523
12524
11.1M
  CG(zend_lineno) = ast->lineno;
12525
12526
11.1M
  if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) {
12527
0
    zend_do_extended_stmt(NULL);
12528
0
  }
12529
12530
11.1M
  switch (ast->kind) {
12531
2.54M
    case ZEND_AST_STMT_LIST:
12532
2.54M
      zend_compile_stmt_list(ast);
12533
2.54M
      break;
12534
3.44k
    case ZEND_AST_GLOBAL:
12535
3.44k
      zend_compile_global_var(ast);
12536
3.44k
      break;
12537
4.04k
    case ZEND_AST_STATIC:
12538
4.04k
      zend_compile_static_var(ast);
12539
4.04k
      break;
12540
3.78k
    case ZEND_AST_UNSET:
12541
3.78k
      zend_compile_unset(ast);
12542
3.78k
      break;
12543
44.5k
    case ZEND_AST_RETURN:
12544
44.5k
      zend_compile_return(ast);
12545
44.5k
      break;
12546
2.81M
    case ZEND_AST_ECHO:
12547
2.81M
      zend_compile_echo(ast);
12548
2.81M
      break;
12549
1.26k
    case ZEND_AST_BREAK:
12550
4.42k
    case ZEND_AST_CONTINUE:
12551
4.42k
      zend_compile_break_continue(ast);
12552
4.42k
      break;
12553
4.31k
    case ZEND_AST_GOTO:
12554
4.31k
      zend_compile_goto(ast);
12555
4.31k
      break;
12556
28.9k
    case ZEND_AST_LABEL:
12557
28.9k
      zend_compile_label(ast);
12558
28.9k
      break;
12559
2.35k
    case ZEND_AST_WHILE:
12560
2.35k
      zend_compile_while(ast);
12561
2.35k
      break;
12562
1.98k
    case ZEND_AST_DO_WHILE:
12563
1.98k
      zend_compile_do_while(ast);
12564
1.98k
      break;
12565
23.2k
    case ZEND_AST_FOR:
12566
23.2k
      zend_compile_for(ast);
12567
23.2k
      break;
12568
6.50k
    case ZEND_AST_FOREACH:
12569
6.50k
      zend_compile_foreach(ast);
12570
6.50k
      break;
12571
167k
    case ZEND_AST_IF:
12572
167k
      zend_compile_if(ast);
12573
167k
      break;
12574
8.63k
    case ZEND_AST_SWITCH:
12575
8.63k
      zend_compile_switch(ast);
12576
8.63k
      break;
12577
8.22k
    case ZEND_AST_TRY:
12578
8.22k
      zend_compile_try(ast);
12579
8.22k
      break;
12580
14.8k
    case ZEND_AST_DECLARE:
12581
14.8k
      zend_compile_declare(ast);
12582
14.8k
      break;
12583
4.77k
    case ZEND_AST_FUNC_DECL:
12584
114k
    case ZEND_AST_METHOD:
12585
114k
      zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_NESTED);
12586
114k
      break;
12587
197k
    case ZEND_AST_ENUM_CASE:
12588
197k
      zend_compile_enum_case(ast);
12589
197k
      break;
12590
20.7k
    case ZEND_AST_PROP_GROUP:
12591
20.7k
      zend_compile_prop_group(ast);
12592
20.7k
      break;
12593
13.1k
    case ZEND_AST_CLASS_CONST_GROUP:
12594
13.1k
      zend_compile_class_const_group(ast);
12595
13.1k
      break;
12596
10.9k
    case ZEND_AST_USE_TRAIT:
12597
10.9k
      zend_compile_use_trait(ast);
12598
10.9k
      break;
12599
235k
    case ZEND_AST_CLASS:
12600
235k
      zend_compile_class_decl(NULL, ast, false);
12601
235k
      break;
12602
651
    case ZEND_AST_GROUP_USE:
12603
651
      zend_compile_group_use(ast);
12604
651
      break;
12605
1.46k
    case ZEND_AST_USE:
12606
1.46k
      zend_compile_use(ast);
12607
1.46k
      break;
12608
3.99k
    case ZEND_AST_CONST_DECL:
12609
3.99k
      zend_compile_const_decl(ast);
12610
3.99k
      break;
12611
5.09k
    case ZEND_AST_NAMESPACE:
12612
5.09k
      zend_compile_namespace(ast);
12613
5.09k
      break;
12614
17
    case ZEND_AST_HALT_COMPILER:
12615
17
      zend_compile_halt_compiler(ast);
12616
17
      break;
12617
474
    case ZEND_AST_THROW:
12618
474
      zend_compile_expr(NULL, ast);
12619
474
      break;
12620
1.00k
    case ZEND_AST_CAST_VOID:
12621
1.00k
      zend_compile_void_cast(NULL, ast);
12622
1.00k
      break;
12623
73.1k
    case ZEND_AST_ASSIGN: {
12624
73.1k
      znode result;
12625
73.1k
      zend_compile_assign(&result, ast, /* stmt */ true, BP_VAR_R);
12626
73.1k
      zend_do_free(&result);
12627
73.1k
      return;
12628
4.77k
    }
12629
56.4k
    case ZEND_AST_ASSIGN_REF:
12630
56.4k
      zend_compile_assign_ref(NULL, ast, BP_VAR_R);
12631
56.4k
      return;
12632
4.71M
    default:
12633
4.71M
    {
12634
4.71M
      znode result;
12635
4.71M
      zend_compile_expr(&result, ast);
12636
4.71M
      zend_do_free(&result);
12637
4.71M
    }
12638
11.1M
  }
12639
12640
11.0M
  if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) {
12641
84.6k
    zend_emit_tick();
12642
84.6k
  }
12643
11.0M
}
12644
/* }}} */
12645
12646
/* Keep this out of zend_compile_expr(): the two form a recursion cycle, so inlining merges this
12647
 * frame into every nesting level of an expression, tripling the stack needed to compile it. */
12648
static zend_never_inline void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */
12649
59.0M
{
12650
  /* CG(zend_lineno) = ast->lineno; */
12651
59.0M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
12652
12653
59.0M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
12654
1.22M
    zend_compile_memoized_expr(result, ast, BP_VAR_R);
12655
1.22M
    return;
12656
1.22M
  }
12657
12658
57.8M
  switch (ast->kind) {
12659
7.05M
    case ZEND_AST_ZVAL:
12660
7.05M
      ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast));
12661
7.05M
      result->op_type = IS_CONST;
12662
7.05M
      return;
12663
161k
    case ZEND_AST_ZNODE:
12664
161k
      *result = *zend_ast_get_znode(ast);
12665
161k
      return;
12666
478k
    case ZEND_AST_VAR:
12667
548k
    case ZEND_AST_DIM:
12668
581k
    case ZEND_AST_PROP:
12669
635k
    case ZEND_AST_NULLSAFE_PROP:
12670
641k
    case ZEND_AST_STATIC_PROP:
12671
4.85M
    case ZEND_AST_CALL:
12672
4.89M
    case ZEND_AST_METHOD_CALL:
12673
4.90M
    case ZEND_AST_NULLSAFE_METHOD_CALL:
12674
4.91M
    case ZEND_AST_STATIC_CALL:
12675
4.98M
    case ZEND_AST_PIPE:
12676
4.98M
      zend_compile_var(result, ast, BP_VAR_R, false);
12677
4.98M
      return;
12678
560k
    case ZEND_AST_ASSIGN:
12679
560k
      zend_compile_assign(result, ast, /* stmt */ false, BP_VAR_R);
12680
560k
      return;
12681
954
    case ZEND_AST_ASSIGN_REF:
12682
954
      zend_compile_assign_ref(result, ast, BP_VAR_R);
12683
954
      return;
12684
81.3k
    case ZEND_AST_NEW:
12685
81.3k
      zend_compile_new(result, ast);
12686
81.3k
      return;
12687
150k
    case ZEND_AST_ASSIGN_OP:
12688
150k
      zend_compile_compound_assign(result, ast);
12689
150k
      return;
12690
5.87M
    case ZEND_AST_BINARY_OP:
12691
5.87M
      zend_compile_binary_op(result, ast);
12692
5.87M
      return;
12693
371k
    case ZEND_AST_GREATER:
12694
473k
    case ZEND_AST_GREATER_EQUAL:
12695
473k
      zend_compile_greater(result, ast);
12696
473k
      return;
12697
1.96M
    case ZEND_AST_UNARY_OP:
12698
1.96M
      zend_compile_unary_op(result, ast);
12699
1.96M
      return;
12700
48.7k
    case ZEND_AST_UNARY_PLUS:
12701
854k
    case ZEND_AST_UNARY_MINUS:
12702
854k
      zend_compile_unary_pm(result, ast);
12703
854k
      return;
12704
8.03k
    case ZEND_AST_AND:
12705
10.9k
    case ZEND_AST_OR:
12706
10.9k
      zend_compile_short_circuiting(result, ast);
12707
10.9k
      return;
12708
1.90k
    case ZEND_AST_POST_INC:
12709
3.94k
    case ZEND_AST_POST_DEC:
12710
3.94k
      zend_compile_post_incdec(result, ast);
12711
3.94k
      return;
12712
914
    case ZEND_AST_PRE_INC:
12713
2.03k
    case ZEND_AST_PRE_DEC:
12714
2.03k
      zend_compile_pre_incdec(result, ast);
12715
2.03k
      return;
12716
1.28k
    case ZEND_AST_CAST:
12717
1.28k
      zend_compile_cast(result, ast);
12718
1.28k
      return;
12719
5.92k
    case ZEND_AST_CONDITIONAL:
12720
5.92k
      zend_compile_conditional(result, ast);
12721
5.92k
      return;
12722
1.88M
    case ZEND_AST_COALESCE:
12723
1.88M
      zend_compile_coalesce(result, ast);
12724
1.88M
      return;
12725
104k
    case ZEND_AST_ASSIGN_COALESCE:
12726
104k
      zend_compile_assign_coalesce(result, ast);
12727
104k
      return;
12728
7.23k
    case ZEND_AST_PRINT:
12729
7.23k
      zend_compile_print(result, ast);
12730
7.23k
      return;
12731
672k
    case ZEND_AST_YIELD:
12732
672k
      zend_compile_yield(result, ast);
12733
672k
      return;
12734
1.00k
    case ZEND_AST_YIELD_FROM:
12735
1.00k
      zend_compile_yield_from(result, ast);
12736
1.00k
      return;
12737
467
    case ZEND_AST_INSTANCEOF:
12738
467
      zend_compile_instanceof(result, ast);
12739
467
      return;
12740
3.07k
    case ZEND_AST_INCLUDE_OR_EVAL:
12741
3.07k
      zend_compile_include_or_eval(result, ast);
12742
3.07k
      return;
12743
6.19k
    case ZEND_AST_ISSET:
12744
7.19k
    case ZEND_AST_EMPTY:
12745
7.19k
      zend_compile_isset_or_empty(result, ast);
12746
7.19k
      return;
12747
18.9M
    case ZEND_AST_SILENCE:
12748
18.9M
      zend_compile_silence(result, ast);
12749
18.9M
      return;
12750
72.2k
    case ZEND_AST_SHELL_EXEC:
12751
72.2k
      zend_compile_shell_exec(result, ast);
12752
72.2k
      return;
12753
117k
    case ZEND_AST_ARRAY:
12754
117k
      zend_compile_array(result, ast);
12755
117k
      return;
12756
11.6M
    case ZEND_AST_CONST:
12757
11.6M
      zend_compile_const(result, ast, BP_VAR_R);
12758
11.6M
      return;
12759
6
    case ZEND_AST_CONSTANT:
12760
6
      zend_compile_constant(result, ast, BP_VAR_R);
12761
6
      return;
12762
41.0k
    case ZEND_AST_CLASS_CONST:
12763
41.0k
      zend_compile_class_const(result, ast, BP_VAR_R);
12764
41.0k
      return;
12765
3.36k
    case ZEND_AST_CLASS_NAME:
12766
3.36k
      zend_compile_class_name(result, ast);
12767
3.36k
      return;
12768
73.7k
    case ZEND_AST_ENCAPS_LIST:
12769
73.7k
      zend_compile_encaps_list(result, ast);
12770
73.7k
      return;
12771
11.4k
    case ZEND_AST_MAGIC_CONST:
12772
11.4k
      zend_compile_magic_const(result, ast);
12773
11.4k
      return;
12774
1.94M
    case ZEND_AST_CLOSURE:
12775
1.97M
    case ZEND_AST_ARROW_FUNC:
12776
1.97M
      zend_compile_func_decl(result, ast, FUNC_DECL_LEVEL_NESTED);
12777
1.97M
      return;
12778
982
    case ZEND_AST_THROW:
12779
982
      zend_compile_throw(result, ast);
12780
982
      return;
12781
7.20k
    case ZEND_AST_MATCH:
12782
7.20k
      zend_compile_match(result, ast);
12783
7.20k
      return;
12784
0
    default:
12785
0
      ZEND_ASSERT(0 /* not supported */);
12786
57.8M
  }
12787
57.8M
}
12788
/* }}} */
12789
12790
static void zend_compile_expr(znode *result, zend_ast *ast)
12791
59.0M
{
12792
59.0M
  zend_check_stack_limit();
12793
12794
59.0M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12795
59.0M
  zend_compile_expr_inner(result, ast);
12796
59.0M
  zend_short_circuiting_commit(checkpoint, result, ast);
12797
59.0M
#if ZEND_DEBUG
12798
59.0M
  if (result) {
12799
    /* BP_VAR_R is not allowed to produce IS_VAR. */
12800
59.0M
    ZEND_ASSERT(result->op_type != IS_VAR);
12801
59.0M
  }
12802
59.0M
#endif
12803
59.0M
}
12804
12805
static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
12806
7.96M
{
12807
7.96M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
12808
12809
7.96M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
12810
373k
    switch (ast->kind) {
12811
9.18k
      case ZEND_AST_CALL:
12812
163k
      case ZEND_AST_METHOD_CALL:
12813
163k
      case ZEND_AST_NULLSAFE_METHOD_CALL:
12814
164k
      case ZEND_AST_STATIC_CALL:
12815
164k
      case ZEND_AST_PIPE:
12816
164k
        zend_compile_memoized_expr(result, ast, BP_VAR_W);
12817
        /* This might not actually produce an opcode, e.g. for expressions evaluated at comptime. */
12818
164k
        return NULL;
12819
373k
    }
12820
373k
  }
12821
12822
7.79M
  switch (ast->kind) {
12823
564k
    case ZEND_AST_VAR:
12824
564k
      return zend_compile_simple_var(result, ast, type, false);
12825
308k
    case ZEND_AST_DIM:
12826
308k
      return zend_compile_dim(result, ast, type, by_ref);
12827
67.0k
    case ZEND_AST_PROP:
12828
127k
    case ZEND_AST_NULLSAFE_PROP:
12829
127k
      return zend_compile_prop(result, ast, type, by_ref);
12830
9.40k
    case ZEND_AST_STATIC_PROP:
12831
9.40k
      return zend_compile_static_prop(result, ast, type, by_ref, false);
12832
4.55M
    case ZEND_AST_CALL:
12833
4.55M
      zend_compile_call(result, ast, type);
12834
4.55M
      return NULL;
12835
122k
    case ZEND_AST_METHOD_CALL:
12836
127k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
12837
127k
      zend_compile_method_call(result, ast, type);
12838
127k
      return NULL;
12839
17.8k
    case ZEND_AST_STATIC_CALL:
12840
17.8k
      zend_compile_static_call(result, ast, type);
12841
17.8k
      return NULL;
12842
76.8k
    case ZEND_AST_PIPE:
12843
76.8k
      zend_compile_pipe(result, ast, type);
12844
76.8k
      return NULL;
12845
54.2k
    case ZEND_AST_ZNODE:
12846
54.2k
      *result = *zend_ast_get_znode(ast);
12847
54.2k
      return NULL;
12848
12.5k
    case ZEND_AST_ASSIGN_REF:
12849
12.5k
      zend_compile_assign_ref(result, ast, type);
12850
12.5k
      return NULL;
12851
10
    case ZEND_AST_ASSIGN:
12852
10
      zend_compile_assign(result, ast, false, type);
12853
10
      return NULL;
12854
1.93M
    default:
12855
1.93M
      if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
12856
272
        zend_error_noreturn(E_COMPILE_ERROR,
12857
272
          "Cannot use temporary expression in write context");
12858
272
      }
12859
12860
1.93M
      zend_compile_expr(result, ast);
12861
1.93M
      return NULL;
12862
7.79M
  }
12863
7.79M
}
12864
12865
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12866
7.96M
{
12867
7.96M
  zend_check_stack_limit();
12868
12869
7.96M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12870
7.96M
  zend_op *opcode = zend_compile_var_inner(result, ast, type, by_ref);
12871
7.96M
  zend_short_circuiting_commit(checkpoint, result, ast);
12872
7.96M
#if ZEND_DEBUG
12873
7.96M
  if (result
12874
7.95M
   && (type == BP_VAR_R || type == BP_VAR_IS)
12875
   /* Don't check memoized result, as it will force BP_VAR_W even for BP_VAR_IS. */
12876
7.24M
   && CG(memoize_mode) == ZEND_MEMOIZE_NONE
12877
7.96M
  ) {
12878
    /* BP_VAR_{R,IS} is not allowed to produce IS_VAR. */
12879
7.05M
    ZEND_ASSERT(result->op_type != IS_VAR);
12880
7.05M
  }
12881
7.96M
#endif
12882
7.96M
  return opcode;
12883
7.96M
}
12884
12885
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12886
2.44M
{
12887
2.44M
  zend_check_stack_limit();
12888
12889
2.44M
  switch (ast->kind) {
12890
880k
    case ZEND_AST_VAR:
12891
880k
      return zend_compile_simple_var(result, ast, type, true);
12892
258k
    case ZEND_AST_DIM:
12893
258k
      return zend_delayed_compile_dim(result, ast, type, by_ref);
12894
929k
    case ZEND_AST_PROP:
12895
959k
    case ZEND_AST_NULLSAFE_PROP:
12896
959k
    {
12897
959k
      zend_op *opline = zend_delayed_compile_prop(result, ast, type);
12898
959k
      if (by_ref) {
12899
603
        opline->extended_value |= ZEND_FETCH_REF;
12900
603
      }
12901
959k
      return opline;
12902
929k
    }
12903
9.59k
    case ZEND_AST_STATIC_PROP:
12904
9.59k
      return zend_compile_static_prop(result, ast, type, by_ref, true);
12905
117k
    case ZEND_AST_CONST:
12906
117k
      if (!(ast->attr & ZEND_CONST_OBJECT_FETCH)) {
12907
27.0k
        break;
12908
27.0k
      }
12909
90.1k
      zend_compile_const(result, ast, type);
12910
90.1k
      if (!(type == BP_VAR_R || type == BP_VAR_IS) && result->op_type == IS_CONST) {
12911
89
        znode op1 = *result;
12912
        /* Intentionally IS_VAR result. */
12913
89
        zend_emit_op(result, ZEND_QM_ASSIGN, &op1, NULL);
12914
89
      }
12915
90.1k
      return NULL;
12916
2.73k
    case ZEND_AST_CLASS_CONST:
12917
2.73k
      if (!(ast->attr & ZEND_CONST_OBJECT_FETCH)) {
12918
1.88k
        break;
12919
1.88k
      }
12920
854
      zend_compile_class_const(result, ast, type);
12921
854
      if (!(type == BP_VAR_R || type == BP_VAR_IS) && result->op_type == IS_CONST) {
12922
0
        znode op1 = *result;
12923
        /* Intentionally IS_VAR result. */
12924
0
        zend_emit_op(result, ZEND_QM_ASSIGN, &op1, NULL);
12925
0
      }
12926
854
      return NULL;
12927
2.44M
  }
12928
12929
244k
  return zend_compile_var(result, ast, type, false);
12930
2.44M
}
12931
/* }}} */
12932
12933
bool zend_try_ct_eval_cast(zval *result, uint32_t type, zval *op1)
12934
5.10k
{
12935
  /* NAN warns when casting */
12936
5.10k
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))) {
12937
0
    return false;
12938
0
  }
12939
5.10k
  switch (type) {
12940
38
    case _IS_BOOL:
12941
38
      ZVAL_BOOL(result, zend_is_true(op1));
12942
38
      return true;
12943
1.48k
    case IS_LONG:
12944
1.48k
      if (Z_TYPE_P(op1) == IS_DOUBLE && !ZEND_DOUBLE_FITS_LONG(Z_DVAL_P((op1)))) {
12945
274
        return false;
12946
274
      }
12947
1.20k
      ZVAL_LONG(result, zval_get_long(op1));
12948
1.20k
      return true;
12949
1.45k
    case IS_DOUBLE:
12950
1.45k
      ZVAL_DOUBLE(result, zval_get_double(op1));
12951
1.45k
      return true;
12952
1.44k
    case IS_STRING:
12953
      /* Conversion from double to string takes into account run-time
12954
         'precision' setting and cannot be evaluated at compile-time */
12955
1.44k
      if (Z_TYPE_P(op1) != IS_ARRAY && Z_TYPE_P(op1) != IS_DOUBLE) {
12956
1.35k
        ZVAL_STR(result, zval_get_string(op1));
12957
1.35k
        return true;
12958
1.35k
      }
12959
99
      break;
12960
592
    case IS_ARRAY:
12961
592
      ZVAL_COPY(result, op1);
12962
592
      convert_to_array(result);
12963
592
      return true;
12964
5.10k
  }
12965
188
  return false;
12966
5.10k
}
12967
12968
static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
12969
10.1M
{
12970
10.1M
  zend_ast *ast = *ast_ptr;
12971
10.1M
  zval result;
12972
12973
10.1M
  if (!ast) {
12974
3.03M
    return;
12975
3.03M
  }
12976
12977
7.07M
  zend_check_stack_limit();
12978
12979
7.07M
  switch (ast->kind) {
12980
246k
    case ZEND_AST_BINARY_OP:
12981
246k
      zend_eval_const_expr(&ast->child[0]);
12982
246k
      zend_eval_const_expr(&ast->child[1]);
12983
246k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12984
202k
        return;
12985
202k
      }
12986
12987
44.0k
      if (!zend_try_ct_eval_binary_op(&result, ast->attr,
12988
44.0k
          zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
12989
44.0k
      ) {
12990
28.3k
        return;
12991
28.3k
      }
12992
15.6k
      break;
12993
15.6k
    case ZEND_AST_GREATER:
12994
6.33k
    case ZEND_AST_GREATER_EQUAL:
12995
6.33k
      zend_eval_const_expr(&ast->child[0]);
12996
6.33k
      zend_eval_const_expr(&ast->child[1]);
12997
6.33k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12998
4.68k
        return;
12999
4.68k
      }
13000
13001
1.65k
      zend_ct_eval_greater(&result, ast->kind,
13002
1.65k
        zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]));
13003
1.65k
      break;
13004
4.73k
    case ZEND_AST_AND:
13005
7.36k
    case ZEND_AST_OR:
13006
7.36k
    {
13007
7.36k
      bool child0_is_true, child1_is_true;
13008
7.36k
      zend_eval_const_expr(&ast->child[0]);
13009
7.36k
      zend_eval_const_expr(&ast->child[1]);
13010
7.36k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
13011
4.11k
        return;
13012
4.11k
      }
13013
13014
3.25k
      child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0]));
13015
3.25k
      if (child0_is_true == (ast->kind == ZEND_AST_OR)) {
13016
250
        ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR);
13017
250
        break;
13018
250
      }
13019
13020
3.00k
      if (ast->child[1]->kind != ZEND_AST_ZVAL) {
13021
1.56k
        return;
13022
1.56k
      }
13023
13024
1.44k
      child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1]));
13025
1.44k
      if (ast->kind == ZEND_AST_OR) {
13026
372
        ZVAL_BOOL(&result, child0_is_true || child1_is_true);
13027
1.06k
      } else {
13028
1.06k
        ZVAL_BOOL(&result, child0_is_true && child1_is_true);
13029
1.06k
      }
13030
1.44k
      break;
13031
3.00k
    }
13032
8.53k
    case ZEND_AST_UNARY_OP:
13033
8.53k
      zend_eval_const_expr(&ast->child[0]);
13034
8.53k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
13035
2.35k
        return;
13036
2.35k
      }
13037
13038
6.18k
      if (!zend_try_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
13039
1.17k
        return;
13040
1.17k
      }
13041
5.00k
      break;
13042
5.00k
    case ZEND_AST_UNARY_PLUS:
13043
30.0k
    case ZEND_AST_UNARY_MINUS:
13044
30.0k
      zend_eval_const_expr(&ast->child[0]);
13045
30.0k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
13046
23.9k
        return;
13047
23.9k
      }
13048
13049
6.10k
      if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) {
13050
3.37k
        return;
13051
3.37k
      }
13052
2.72k
      break;
13053
3.08k
    case ZEND_AST_COALESCE:
13054
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
13055
3.08k
      if (ast->child[0]->kind == ZEND_AST_DIM) {
13056
499
        ast->child[0]->attr |= ZEND_DIM_IS;
13057
499
      }
13058
3.08k
      zend_eval_const_expr(&ast->child[0]);
13059
13060
3.08k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
13061
        /* ensure everything was compile-time evaluated at least once */
13062
2.73k
        zend_eval_const_expr(&ast->child[1]);
13063
2.73k
        return;
13064
2.73k
      }
13065
13066
355
      if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) {
13067
307
        zend_eval_const_expr(&ast->child[1]);
13068
307
        *ast_ptr = ast->child[1];
13069
307
        ast->child[1] = NULL;
13070
307
        zend_ast_destroy(ast);
13071
307
      } else {
13072
48
        *ast_ptr = ast->child[0];
13073
48
        ast->child[0] = NULL;
13074
48
        zend_ast_destroy(ast);
13075
48
      }
13076
355
      return;
13077
2.33k
    case ZEND_AST_CONDITIONAL:
13078
2.33k
    {
13079
2.33k
      zend_ast **child, *child_ast;
13080
2.33k
      zend_eval_const_expr(&ast->child[0]);
13081
2.33k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
13082
        /* ensure everything was compile-time evaluated at least once */
13083
2.15k
        if (ast->child[1]) {
13084
1.31k
          zend_eval_const_expr(&ast->child[1]);
13085
1.31k
        }
13086
2.15k
        zend_eval_const_expr(&ast->child[2]);
13087
2.15k
        return;
13088
2.15k
      }
13089
13090
181
      child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];
13091
181
      if (*child == NULL) {
13092
106
        child--;
13093
106
      }
13094
181
      child_ast = *child;
13095
181
      *child = NULL;
13096
181
      zend_ast_destroy(ast);
13097
181
      *ast_ptr = child_ast;
13098
181
      zend_eval_const_expr(ast_ptr);
13099
181
      return;
13100
2.33k
    }
13101
1.19M
    case ZEND_AST_DIM:
13102
1.19M
    {
13103
      /* constant expression should be always read context ... */
13104
1.19M
      const zval *container, *dim;
13105
13106
1.19M
      if (ast->child[1] == NULL) {
13107
43
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
13108
43
      }
13109
13110
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
13111
1.19M
      if ((ast->attr & ZEND_DIM_IS) && ast->child[0]->kind == ZEND_AST_DIM) {
13112
523
        ast->child[0]->attr |= ZEND_DIM_IS;
13113
523
      }
13114
13115
1.19M
      zend_eval_const_expr(&ast->child[0]);
13116
1.19M
      zend_eval_const_expr(&ast->child[1]);
13117
1.19M
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
13118
1.18M
        return;
13119
1.18M
      }
13120
13121
17.5k
      container = zend_ast_get_zval(ast->child[0]);
13122
17.5k
      dim = zend_ast_get_zval(ast->child[1]);
13123
13124
17.5k
      if (Z_TYPE_P(container) == IS_ARRAY) {
13125
13.2k
        zval *el;
13126
13.2k
        if (Z_TYPE_P(dim) == IS_LONG) {
13127
1.53k
          el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim));
13128
1.53k
          if (el) {
13129
712
            ZVAL_COPY(&result, el);
13130
823
          } else {
13131
823
            return;
13132
823
          }
13133
11.7k
        } else if (Z_TYPE_P(dim) == IS_STRING) {
13134
10.8k
          el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim));
13135
10.8k
          if (el) {
13136
619
            ZVAL_COPY(&result, el);
13137
10.2k
          } else {
13138
10.2k
            return;
13139
10.2k
          }
13140
10.8k
        } else {
13141
826
          return; /* warning... handle at runtime */
13142
826
        }
13143
13.2k
      } else if (Z_TYPE_P(container) == IS_STRING) {
13144
3.59k
        zend_long offset;
13145
3.59k
        uint8_t c;
13146
3.59k
        if (Z_TYPE_P(dim) == IS_LONG) {
13147
2.52k
          offset = Z_LVAL_P(dim);
13148
2.52k
        } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) {
13149
615
          return;
13150
615
        }
13151
2.97k
        if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
13152
2.69k
          return;
13153
2.69k
        }
13154
288
        c = (uint8_t) Z_STRVAL_P(container)[offset];
13155
288
        ZVAL_CHAR(&result, c);
13156
732
      } else if (Z_TYPE_P(container) <= IS_FALSE) {
13157
30
        return; /* warning... handle at runtime */
13158
702
      } else {
13159
702
        return;
13160
702
      }
13161
1.61k
      break;
13162
17.5k
    }
13163
2.77M
    case ZEND_AST_ARRAY:
13164
2.77M
      if (!zend_try_ct_eval_array(&result, ast)) {
13165
2.73M
        return;
13166
2.73M
      }
13167
45.7k
      break;
13168
45.7k
    case ZEND_AST_MAGIC_CONST:
13169
2.05k
      if (!zend_try_ct_eval_magic_const(&result, ast)) {
13170
194
        return;
13171
194
      }
13172
1.85k
      break;
13173
145k
    case ZEND_AST_CONST:
13174
145k
    {
13175
145k
      zend_ast *name_ast = ast->child[0];
13176
145k
      bool is_fully_qualified;
13177
145k
      zend_string *resolved_name = zend_resolve_const_name(
13178
145k
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
13179
13180
145k
      if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
13181
143k
        zend_string_release_ex(resolved_name, 0);
13182
143k
        return;
13183
143k
      }
13184
13185
2.10k
      zend_string_release_ex(resolved_name, 0);
13186
2.10k
      break;
13187
145k
    }
13188
279k
    case ZEND_AST_CLASS_CONST:
13189
279k
    {
13190
279k
      zend_ast *class_ast;
13191
279k
      zend_ast *name_ast;
13192
279k
      zend_string *resolved_name;
13193
13194
279k
      zend_eval_const_expr(&ast->child[0]);
13195
279k
      zend_eval_const_expr(&ast->child[1]);
13196
13197
279k
      if (UNEXPECTED(ast->child[1]->kind != ZEND_AST_ZVAL
13198
279k
        || Z_TYPE_P(zend_ast_get_zval(ast->child[1])) != IS_STRING)) {
13199
4.43k
        return;
13200
4.43k
      }
13201
13202
275k
      class_ast = ast->child[0];
13203
275k
      name_ast = ast->child[1];
13204
13205
275k
      if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) {
13206
260k
        return;
13207
260k
      }
13208
13209
14.9k
      resolved_name = zend_resolve_class_name_ast(class_ast);
13210
14.9k
      if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) {
13211
14.9k
        zend_string_release_ex(resolved_name, 0);
13212
14.9k
        return;
13213
14.9k
      }
13214
13215
28
      zend_string_release_ex(resolved_name, 0);
13216
28
      break;
13217
14.9k
    }
13218
3.81k
    case ZEND_AST_CLASS_NAME:
13219
3.81k
    {
13220
3.81k
      zend_ast *class_ast = ast->child[0];
13221
3.81k
      if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) {
13222
2.71k
        return;
13223
2.71k
      }
13224
1.09k
      break;
13225
3.81k
    }
13226
    // TODO: We should probably use zend_ast_apply to recursively walk nodes without
13227
    // special handling. It is required that all nodes that are part of a const expr
13228
    // are visited. Probably we should be distinguishing evaluation of const expr and
13229
    // normal exprs here.
13230
2.98k
    case ZEND_AST_ARG_LIST:
13231
2.98k
    {
13232
2.98k
      zend_ast_list *list = zend_ast_get_list(ast);
13233
5.42k
      for (uint32_t i = 0; i < list->children; i++) {
13234
2.43k
        zend_eval_const_expr(&list->child[i]);
13235
2.43k
      }
13236
2.98k
      return;
13237
3.81k
    }
13238
1.49k
    case ZEND_AST_NEW:
13239
1.49k
      zend_eval_const_expr(&ast->child[0]);
13240
1.49k
      zend_eval_const_expr(&ast->child[1]);
13241
1.49k
      return;
13242
    // Only FCC and PFA can appear in constant expressions.
13243
3.43k
    case ZEND_AST_CALL:
13244
4.54k
    case ZEND_AST_STATIC_CALL:
13245
4.54k
    {
13246
4.54k
      zend_ast *args_ast = zend_ast_call_get_args(ast);
13247
4.54k
      if (args_ast && args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
13248
1.50k
        zend_eval_const_expr(&((zend_ast_fcc *) args_ast)->args);
13249
1.50k
      }
13250
4.54k
      return;
13251
3.43k
    }
13252
160
    case ZEND_AST_NAMED_ARG:
13253
160
      zend_eval_const_expr(&ast->child[1]);
13254
160
      return;
13255
197k
    case ZEND_AST_CONST_ENUM_INIT:
13256
197k
      zend_eval_const_expr(&ast->child[3]);
13257
197k
      return;
13258
1.26k
    case ZEND_AST_PROP:
13259
3.81k
    case ZEND_AST_NULLSAFE_PROP:
13260
3.81k
      zend_eval_const_expr(&ast->child[0]);
13261
3.81k
      zend_eval_const_expr(&ast->child[1]);
13262
3.81k
      return;
13263
13.2k
    case ZEND_AST_CAST:
13264
13.2k
      if (ast->attr == IS_NULL) {
13265
1
        zend_error_noreturn(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
13266
1
      }
13267
13.2k
      zend_eval_const_expr(&ast->child[0]);
13268
13.2k
      if (ast->child[0]->kind == ZEND_AST_ZVAL
13269
4.30k
       && zend_try_ct_eval_cast(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
13270
3.94k
        break;
13271
3.94k
      }
13272
9.26k
      return;
13273
2.13M
    default:
13274
2.13M
      return;
13275
7.07M
  }
13276
13277
82.9k
  zend_ast_destroy(ast);
13278
82.9k
  *ast_ptr = zend_ast_create_zval(&result);
13279
82.9k
}
13280
/* }}} */