Coverage Report

Created: 2026-06-13 07:01

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_virtual_cwd.h"
32
#include "zend_multibyte.h"
33
#include "zend_language_scanner.h"
34
#include "zend_inheritance.h"
35
#include "zend_vm.h"
36
#include "zend_enum.h"
37
#include "zend_observer.h"
38
#include "zend_call_stack.h"
39
#include "zend_frameless_function.h"
40
#include "zend_property_hooks.h"
41
42
56.5M
#define SET_NODE(target, src) do { \
43
56.5M
    target ## _type = (src)->op_type; \
44
56.5M
    if ((src)->op_type == IS_CONST) { \
45
7.56M
      target.constant = zend_add_literal(&(src)->u.constant); \
46
48.9M
    } else { \
47
48.9M
      target = (src)->u.op; \
48
48.9M
    } \
49
56.5M
  } while (0)
50
51
40.8M
#define GET_NODE(target, src) do { \
52
40.8M
    (target)->op_type = src ## _type; \
53
40.8M
    if ((target)->op_type == IS_CONST) { \
54
615
      ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
55
40.8M
    } else { \
56
40.8M
      (target)->u.op = src; \
57
40.8M
    } \
58
40.8M
  } while (0)
59
60
86.1M
#define FC(member) (CG(file_context).member)
61
62
typedef struct _zend_loop_var {
63
  uint8_t opcode;
64
  uint8_t var_type;
65
  uint32_t   var_num;
66
  uint32_t   try_catch_offset;
67
} zend_loop_var;
68
69
15.1M
static inline uint32_t zend_alloc_cache_slots(unsigned count) {
70
15.1M
  if (count == 0) {
71
    /* Even if no cache slots are desired, the VM handler may still want to acquire
72
     * CACHE_ADDR() unconditionally. Returning zero makes sure that the address
73
     * calculation is still legal and ubsan does not complain. */
74
0
    return 0;
75
0
  }
76
77
15.1M
  zend_op_array *op_array = CG(active_op_array);
78
15.1M
  uint32_t ret = op_array->cache_size;
79
15.1M
  op_array->cache_size += count * sizeof(void*);
80
15.1M
  return ret;
81
15.1M
}
82
83
14.4M
static inline uint32_t zend_alloc_cache_slot(void) {
84
14.4M
  return zend_alloc_cache_slots(1);
85
14.4M
}
86
87
ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
88
ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename, zend_compile_position position);
89
90
#ifndef ZTS
91
ZEND_API zend_compiler_globals compiler_globals;
92
ZEND_API zend_executor_globals executor_globals;
93
#endif
94
95
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2);
96
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast);
97
static void zend_eval_const_expr(zend_ast **ast_ptr);
98
99
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
100
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
101
static void zend_compile_expr(znode *result, zend_ast *ast);
102
static void zend_compile_stmt(zend_ast *ast);
103
static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type);
104
105
#ifdef ZEND_CHECK_STACK_LIMIT
106
zend_never_inline static void zend_stack_limit_error(void)
107
0
{
108
0
  size_t max_stack_size = 0;
109
0
  if ((uintptr_t) EG(stack_base) > (uintptr_t) EG(stack_limit)) {
110
0
    max_stack_size = (size_t) ((uintptr_t) EG(stack_base) - (uintptr_t) EG(stack_limit));
111
0
  }
112
113
0
  zend_error_noreturn(E_COMPILE_ERROR,
114
0
    "Maximum call stack size of %zu bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached during compilation. Try splitting expression",
115
0
    max_stack_size);
116
0
}
117
118
static void zend_check_stack_limit(void)
119
60.9M
{
120
60.9M
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
121
0
    zend_stack_limit_error();
122
0
  }
123
60.9M
}
124
#else /* ZEND_CHECK_STACK_LIMIT */
125
static void zend_check_stack_limit(void)
126
{
127
}
128
#endif /* ZEND_CHECK_STACK_LIMIT */
129
130
static void init_op(zend_op *op)
131
82.5M
{
132
82.5M
  MAKE_NOP(op);
133
82.5M
  op->extended_value = 0;
134
82.5M
  op->lineno = CG(zend_lineno);
135
#ifdef ZEND_VERIFY_TYPE_INFERENCE
136
  op->op1_use_type = 0;
137
  op->op2_use_type = 0;
138
  op->result_use_type = 0;
139
  op->op1_def_type = 0;
140
  op->op2_def_type = 0;
141
  op->result_def_type = 0;
142
#endif
143
82.5M
}
144
145
static zend_always_inline uint32_t get_next_op_number(void)
146
13.7M
{
147
13.7M
  return CG(active_op_array)->last;
148
13.7M
}
149
150
static zend_op *get_next_op(void)
151
81.5M
{
152
81.5M
  zend_op_array *op_array = CG(active_op_array);
153
81.5M
  uint32_t next_op_num = op_array->last++;
154
81.5M
  zend_op *next_op;
155
156
81.5M
  if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) {
157
651k
    CG(context).opcodes_size *= 4;
158
651k
    op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op));
159
651k
  }
160
161
81.5M
  next_op = &(op_array->opcodes[next_op_num]);
162
163
81.5M
  init_op(next_op);
164
165
81.5M
  return next_op;
166
81.5M
}
167
168
static zend_brk_cont_element *get_next_brk_cont_element(void)
169
52.6k
{
170
52.6k
  CG(context).last_brk_cont++;
171
52.6k
  CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont);
172
52.6k
  return &CG(context).brk_cont_array[CG(context).last_brk_cont-1];
173
52.6k
}
174
175
static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */
176
297k
{
177
297k
  zend_string *filename = CG(active_op_array)->filename;
178
297k
  zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32,
179
297k
    '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
180
297k
  return zend_new_interned_string(result);
181
297k
}
182
/* }}} */
183
184
static bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
185
14.5M
{
186
14.5M
  const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
187
14.5M
  if (ns_separator != NULL) {
188
13.9M
    *result = ns_separator + 1;
189
13.9M
    *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
190
13.9M
    return true;
191
13.9M
  }
192
193
625k
  return false;
194
14.5M
}
195
/* }}} */
196
197
struct reserved_class_name {
198
  const char *name;
199
  size_t len;
200
};
201
static const struct reserved_class_name reserved_class_names[] = {
202
  {ZEND_STRL("bool")},
203
  {ZEND_STRL("false")},
204
  {ZEND_STRL("float")},
205
  {ZEND_STRL("int")},
206
  {ZEND_STRL("null")},
207
  {ZEND_STRL("parent")},
208
  {ZEND_STRL("self")},
209
  {ZEND_STRL("static")},
210
  {ZEND_STRL("string")},
211
  {ZEND_STRL("true")},
212
  {ZEND_STRL("void")},
213
  {ZEND_STRL("never")},
214
  {ZEND_STRL("iterable")},
215
  {ZEND_STRL("object")},
216
  {ZEND_STRL("mixed")},
217
  /* These are not usable as class names because they're proper tokens,
218
   * but they are here for class aliases. */
219
  {ZEND_STRL("array")},
220
  {ZEND_STRL("callable")},
221
  {NULL, 0}
222
};
223
224
static bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */
225
904k
{
226
904k
  const struct reserved_class_name *reserved = reserved_class_names;
227
228
904k
  const char *uqname = ZSTR_VAL(name);
229
904k
  size_t uqname_len = ZSTR_LEN(name);
230
904k
  zend_get_unqualified_name(name, &uqname, &uqname_len);
231
232
16.2M
  for (; reserved->name; ++reserved) {
233
15.3M
    if (uqname_len == reserved->len
234
228k
      && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
235
15.3M
    ) {
236
96
      return true;
237
96
    }
238
15.3M
  }
239
240
903k
  return false;
241
904k
}
242
/* }}} */
243
244
void zend_assert_valid_class_name(const zend_string *name, const char *type) /* {{{ */
245
902k
{
246
902k
  if (zend_is_reserved_class_name(name)) {
247
73
    zend_error_noreturn(E_COMPILE_ERROR,
248
73
      "Cannot use \"%s\" as %s as it is reserved", ZSTR_VAL(name), type);
249
73
  }
250
902k
  if (zend_string_equals_literal(name, "_")) {
251
1.62k
    zend_error(E_DEPRECATED, "Using \"_\" as %s is deprecated since 8.4", type);
252
1.62k
  }
253
902k
}
254
/* }}} */
255
256
typedef struct _builtin_type_info {
257
  const char* name;
258
  const size_t name_len;
259
  const uint8_t type;
260
} builtin_type_info;
261
262
static const builtin_type_info builtin_types[] = {
263
  {ZEND_STRL("null"), IS_NULL},
264
  {ZEND_STRL("true"), IS_TRUE},
265
  {ZEND_STRL("false"), IS_FALSE},
266
  {ZEND_STRL("int"), IS_LONG},
267
  {ZEND_STRL("float"), IS_DOUBLE},
268
  {ZEND_STRL("string"), IS_STRING},
269
  {ZEND_STRL("bool"), _IS_BOOL},
270
  {ZEND_STRL("void"), IS_VOID},
271
  {ZEND_STRL("never"), IS_NEVER},
272
  {ZEND_STRL("iterable"), IS_ITERABLE},
273
  {ZEND_STRL("object"), IS_OBJECT},
274
  {ZEND_STRL("mixed"), IS_MIXED},
275
  {NULL, 0, IS_UNDEF}
276
};
277
278
typedef struct {
279
  const char *name;
280
  size_t name_len;
281
  const char *correct_name;
282
} confusable_type_info;
283
284
static const confusable_type_info confusable_types[] = {
285
  {ZEND_STRL("boolean"), "bool"},
286
  {ZEND_STRL("integer"), "int"},
287
  {ZEND_STRL("double"), "float"},
288
  {ZEND_STRL("resource"), NULL},
289
  {NULL, 0, NULL},
290
};
291
292
static zend_always_inline uint8_t zend_lookup_builtin_type_by_name(const zend_string *name) /* {{{ */
293
904k
{
294
904k
  const builtin_type_info *info = &builtin_types[0];
295
296
10.6M
  for (; info->name; ++info) {
297
10.1M
    if (ZSTR_LEN(name) == info->name_len
298
411k
      && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
299
10.1M
    ) {
300
326k
      return info->type;
301
326k
    }
302
10.1M
  }
303
304
577k
  return 0;
305
904k
}
306
/* }}} */
307
308
static zend_always_inline bool zend_is_confusable_type(const zend_string *name, const char **correct_name) /* {{{ */
309
576k
{
310
576k
  const confusable_type_info *info = confusable_types;
311
312
  /* Intentionally using case-sensitive comparison here, because "integer" is likely intended
313
   * as a scalar type, while "Integer" is likely a class type. */
314
2.76M
  for (; info->name; ++info) {
315
2.23M
    if (zend_string_equals_cstr(name, info->name, info->name_len)) {
316
48.9k
      *correct_name = info->correct_name;
317
48.9k
      return 1;
318
48.9k
    }
319
2.23M
  }
320
321
527k
  return 0;
322
576k
}
323
/* }}} */
324
325
48.9k
static bool zend_is_not_imported(zend_string *name) {
326
  /* Assuming "name" is unqualified here. */
327
48.9k
  return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL;
328
48.9k
}
329
330
void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_array *op_array) /* {{{ */
331
1.93M
{
332
1.93M
  *prev_context = CG(context);
333
1.93M
  CG(context).prev = CG(context).op_array ? prev_context : NULL;
334
1.93M
  CG(context).op_array = op_array;
335
1.93M
  CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
336
1.93M
  CG(context).vars_size = 0;
337
1.93M
  CG(context).literals_size = 0;
338
1.93M
  CG(context).fast_call_var = -1;
339
1.93M
  CG(context).try_catch_offset = -1;
340
1.93M
  CG(context).current_brk_cont = -1;
341
1.93M
  CG(context).last_brk_cont = 0;
342
1.93M
  CG(context).has_assigned_to_http_response_header = false;
343
1.93M
  CG(context).brk_cont_array = NULL;
344
1.93M
  CG(context).labels = NULL;
345
1.93M
  CG(context).in_jmp_frameless_branch = false;
346
1.93M
  CG(context).active_property_info_name = NULL;
347
1.93M
  CG(context).active_property_hook_kind = (zend_property_hook_kind)-1;
348
1.93M
}
349
/* }}} */
350
351
void zend_oparray_context_end(const zend_oparray_context *prev_context) /* {{{ */
352
1.92M
{
353
1.92M
  if (CG(context).brk_cont_array) {
354
26.5k
    efree(CG(context).brk_cont_array);
355
26.5k
    CG(context).brk_cont_array = NULL;
356
26.5k
  }
357
1.92M
  if (CG(context).labels) {
358
1.92k
    zend_hash_destroy(CG(context).labels);
359
1.92k
    FREE_HASHTABLE(CG(context).labels);
360
1.92k
    CG(context).labels = NULL;
361
1.92k
  }
362
1.92M
  CG(context) = *prev_context;
363
1.92M
}
364
/* }}} */
365
366
static void zend_reset_import_tables(void) /* {{{ */
367
82.8k
{
368
82.8k
  if (FC(imports)) {
369
556
    zend_hash_destroy(FC(imports));
370
556
    efree(FC(imports));
371
556
    FC(imports) = NULL;
372
556
  }
373
374
82.8k
  if (FC(imports_function)) {
375
367
    zend_hash_destroy(FC(imports_function));
376
367
    efree(FC(imports_function));
377
367
    FC(imports_function) = NULL;
378
367
  }
379
380
82.8k
  if (FC(imports_const)) {
381
163
    zend_hash_destroy(FC(imports_const));
382
163
    efree(FC(imports_const));
383
163
    FC(imports_const) = NULL;
384
163
  }
385
386
82.8k
  zend_hash_clean(&FC(seen_symbols));
387
82.8k
}
388
/* }}} */
389
390
78.5k
static void zend_end_namespace(void) /* {{{ */ {
391
78.5k
  FC(in_namespace) = 0;
392
78.5k
  zend_reset_import_tables();
393
78.5k
  if (FC(current_namespace)) {
394
2.94k
    zend_string_release_ex(FC(current_namespace), 0);
395
2.94k
    FC(current_namespace) = NULL;
396
2.94k
  }
397
78.5k
}
398
/* }}} */
399
400
void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
401
82.7k
{
402
82.7k
  *prev_context = CG(file_context);
403
82.7k
  FC(imports) = NULL;
404
82.7k
  FC(imports_function) = NULL;
405
82.7k
  FC(imports_const) = NULL;
406
82.7k
  FC(current_namespace) = NULL;
407
82.7k
  FC(in_namespace) = 0;
408
82.7k
  FC(has_bracketed_namespaces) = 0;
409
82.7k
  FC(declarables).ticks = 0;
410
82.7k
  zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
411
82.7k
}
412
/* }}} */
413
414
void zend_file_context_end(const zend_file_context *prev_context) /* {{{ */
415
77.3k
{
416
77.3k
  zend_end_namespace();
417
77.3k
  zend_hash_destroy(&FC(seen_symbols));
418
77.3k
  CG(file_context) = *prev_context;
419
77.3k
}
420
/* }}} */
421
422
void zend_init_compiler_data_structures(void) /* {{{ */
423
234k
{
424
234k
  zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
425
234k
  zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
426
234k
  zend_stack_init(&CG(short_circuiting_opnums), sizeof(uint32_t));
427
234k
  CG(active_class_entry) = NULL;
428
234k
  CG(in_compilation) = 0;
429
234k
  CG(skip_shebang) = 0;
430
431
234k
  CG(encoding_declared) = 0;
432
234k
  CG(memoized_exprs) = NULL;
433
234k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
434
234k
}
435
/* }}} */
436
437
2.04M
static void zend_register_seen_symbol(zend_string *name, uint32_t kind) {
438
2.04M
  zval *zv = zend_hash_find(&FC(seen_symbols), name);
439
2.04M
  if (zv) {
440
1.96M
    Z_LVAL_P(zv) |= kind;
441
1.96M
  } else {
442
79.9k
    zval tmp;
443
79.9k
    ZVAL_LONG(&tmp, kind);
444
79.9k
    zend_hash_add_new(&FC(seen_symbols), name, &tmp);
445
79.9k
  }
446
2.04M
}
447
448
1.92k
static bool zend_have_seen_symbol(zend_string *name, uint32_t kind) {
449
1.92k
  const zval *zv = zend_hash_find(&FC(seen_symbols), name);
450
1.92k
  return zv && (Z_LVAL_P(zv) & kind) != 0;
451
1.92k
}
452
453
void init_compiler(void) /* {{{ */
454
229k
{
455
229k
  CG(arena) = zend_arena_create(64 * 1024);
456
229k
  CG(active_op_array) = NULL;
457
229k
  memset(&CG(context), 0, sizeof(CG(context)));
458
229k
  zend_init_compiler_data_structures();
459
229k
  zend_init_rsrc_list();
460
229k
  zend_stream_init();
461
229k
  CG(unclean_shutdown) = 0;
462
463
229k
  CG(delayed_variance_obligations) = NULL;
464
229k
  CG(delayed_autoloads) = NULL;
465
229k
  CG(unlinked_uses) = NULL;
466
229k
  CG(current_linking_class) = NULL;
467
229k
}
468
/* }}} */
469
470
void shutdown_compiler(void) /* {{{ */
471
234k
{
472
  /* Reset filename before destroying the arena, as file cache may use arena allocated strings. */
473
234k
  zend_restore_compiled_filename(NULL);
474
475
234k
  zend_stack_destroy(&CG(loop_var_stack));
476
234k
  zend_stack_destroy(&CG(delayed_oplines_stack));
477
234k
  zend_stack_destroy(&CG(short_circuiting_opnums));
478
479
234k
  if (CG(delayed_variance_obligations)) {
480
263
    zend_hash_destroy(CG(delayed_variance_obligations));
481
263
    FREE_HASHTABLE(CG(delayed_variance_obligations));
482
263
    CG(delayed_variance_obligations) = NULL;
483
263
  }
484
234k
  if (CG(delayed_autoloads)) {
485
271
    zend_hash_destroy(CG(delayed_autoloads));
486
271
    FREE_HASHTABLE(CG(delayed_autoloads));
487
271
    CG(delayed_autoloads) = NULL;
488
271
  }
489
234k
  if (CG(unlinked_uses)) {
490
225
    zend_hash_destroy(CG(unlinked_uses));
491
225
    FREE_HASHTABLE(CG(unlinked_uses));
492
225
    CG(unlinked_uses) = NULL;
493
225
  }
494
234k
  CG(current_linking_class) = NULL;
495
234k
}
496
/* }}} */
497
498
ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
499
138k
{
500
138k
  CG(compiled_filename) = zend_string_copy(new_compiled_filename);
501
138k
  return new_compiled_filename;
502
138k
}
503
/* }}} */
504
505
ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
506
515k
{
507
515k
  if (CG(compiled_filename)) {
508
138k
    zend_string_release(CG(compiled_filename));
509
138k
    CG(compiled_filename) = NULL;
510
138k
  }
511
515k
  CG(compiled_filename) = original_compiled_filename;
512
515k
}
513
/* }}} */
514
515
ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
516
2.79M
{
517
2.79M
  return CG(compiled_filename);
518
2.79M
}
519
/* }}} */
520
521
ZEND_API uint32_t zend_get_compiled_lineno(void) /* {{{ */
522
397k
{
523
397k
  return CG(zend_lineno);
524
397k
}
525
/* }}} */
526
527
ZEND_API bool zend_is_compiling(void) /* {{{ */
528
2.92M
{
529
2.92M
  return CG(in_compilation);
530
2.92M
}
531
/* }}} */
532
533
static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */
534
41.2M
{
535
41.2M
  return (uint32_t)CG(active_op_array)->T++;
536
41.2M
}
537
/* }}} */
538
539
2.92M
static uint32_t lookup_cv(zend_string *name) /* {{{ */{
540
2.92M
  zend_op_array *op_array = CG(active_op_array);
541
2.92M
  int i = 0;
542
2.92M
  zend_ulong hash_value = zend_string_hash_val(name);
543
544
7.49M
  while (i < op_array->last_var) {
545
5.27M
    if (ZSTR_H(op_array->vars[i]) == hash_value
546
707k
     && zend_string_equals(op_array->vars[i], name)) {
547
706k
      return EX_NUM_TO_VAR(i);
548
706k
    }
549
4.56M
    i++;
550
4.56M
  }
551
2.22M
  i = op_array->last_var;
552
2.22M
  op_array->last_var++;
553
2.22M
  if (op_array->last_var > CG(context).vars_size) {
554
1.64M
    CG(context).vars_size += 16; /* FIXME */
555
1.64M
    op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
556
1.64M
  }
557
558
2.22M
  op_array->vars[i] = zend_string_copy(name);
559
2.22M
  return EX_NUM_TO_VAR(i);
560
2.92M
}
561
/* }}} */
562
563
zend_string *zval_make_interned_string(zval *zv)
564
49.7M
{
565
49.7M
  ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
566
49.7M
  Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
567
49.7M
  if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
568
6.57M
    Z_TYPE_FLAGS_P(zv) = 0;
569
6.57M
  }
570
49.7M
  return Z_STR_P(zv);
571
49.7M
}
572
573
/* Common part of zend_add_literal and zend_append_individual_literal */
574
static inline void zend_insert_literal(const zend_op_array *op_array, zval *zv, int literal_position) /* {{{ */
575
49.6M
{
576
49.6M
  zval *lit = CT_CONSTANT_EX(op_array, literal_position);
577
49.6M
  if (Z_TYPE_P(zv) == IS_STRING) {
578
46.8M
    zval_make_interned_string(zv);
579
46.8M
  }
580
49.6M
  ZVAL_COPY_VALUE(lit, zv);
581
49.6M
  Z_EXTRA_P(lit) = 0;
582
49.6M
}
583
/* }}} */
584
585
/* Is used while compiling a function, using the context to keep track
586
   of an approximate size to avoid to relocate to often.
587
   Literals are truncated to actual size in the second compiler pass (pass_two()). */
588
static int zend_add_literal(zval *zv) /* {{{ */
589
49.6M
{
590
49.6M
  zend_op_array *op_array = CG(active_op_array);
591
49.6M
  uint32_t i = op_array->last_literal;
592
49.6M
  op_array->last_literal++;
593
49.6M
  if (i >= CG(context).literals_size) {
594
8.06M
    while (i >= CG(context).literals_size) {
595
4.03M
      CG(context).literals_size += 16; /* FIXME */
596
4.03M
    }
597
4.03M
    op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
598
4.03M
  }
599
49.6M
  zend_insert_literal(op_array, zv, i);
600
49.6M
  return i;
601
49.6M
}
602
/* }}} */
603
604
static inline int zend_add_literal_string(zend_string **str) /* {{{ */
605
42.0M
{
606
42.0M
  int ret;
607
42.0M
  zval zv;
608
42.0M
  ZVAL_STR(&zv, *str);
609
42.0M
  ret = zend_add_literal(&zv);
610
42.0M
  *str = Z_STR(zv);
611
42.0M
  return ret;
612
42.0M
}
613
/* }}} */
614
615
static int zend_add_func_name_literal(zend_string *name) /* {{{ */
616
390k
{
617
  /* Original name */
618
390k
  int ret = zend_add_literal_string(&name);
619
620
  /* Lowercased name */
621
390k
  zend_string *lc_name = zend_string_tolower(name);
622
390k
  zend_add_literal_string(&lc_name);
623
624
390k
  return ret;
625
390k
}
626
/* }}} */
627
628
static int zend_add_ns_func_name_literal(zend_string *name) /* {{{ */
629
3.71M
{
630
3.71M
  const char *unqualified_name;
631
3.71M
  size_t unqualified_name_len;
632
633
  /* Original name */
634
3.71M
  int ret = zend_add_literal_string(&name);
635
636
  /* Lowercased name */
637
3.71M
  zend_string *lc_name = zend_string_tolower(name);
638
3.71M
  zend_add_literal_string(&lc_name);
639
640
  /* Lowercased unqualified name */
641
3.71M
  if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
642
3.71M
    lc_name = zend_string_alloc(unqualified_name_len, 0);
643
3.71M
    zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
644
3.71M
    zend_add_literal_string(&lc_name);
645
3.71M
  }
646
647
3.71M
  return ret;
648
3.71M
}
649
/* }}} */
650
651
static int zend_add_class_name_literal(zend_string *name) /* {{{ */
652
164k
{
653
  /* Original name */
654
164k
  int ret = zend_add_literal_string(&name);
655
656
  /* Lowercased name */
657
164k
  zend_string *lc_name = zend_string_tolower(name);
658
164k
  zend_add_literal_string(&lc_name);
659
660
164k
  return ret;
661
164k
}
662
/* }}} */
663
664
static int zend_add_const_name_literal(zend_string *name, bool unqualified) /* {{{ */
665
9.75M
{
666
9.75M
  zend_string *tmp_name;
667
668
9.75M
  int ret = zend_add_literal_string(&name);
669
670
9.75M
  size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
671
9.75M
  const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
672
9.75M
  if (after_ns) {
673
9.59M
    after_ns += 1;
674
9.59M
    ns_len = after_ns - ZSTR_VAL(name) - 1;
675
9.59M
    after_ns_len = ZSTR_LEN(name) - ns_len - 1;
676
677
    /* lowercased namespace name & original constant name */
678
9.59M
    tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
679
9.59M
    zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
680
9.59M
    zend_add_literal_string(&tmp_name);
681
682
9.59M
    if (!unqualified) {
683
3.39k
      return ret;
684
3.39k
    }
685
9.59M
  } else {
686
155k
    after_ns = ZSTR_VAL(name);
687
155k
  }
688
689
  /* original unqualified constant name */
690
9.74M
  tmp_name = zend_string_init(after_ns, after_ns_len, 0);
691
9.74M
  zend_add_literal_string(&tmp_name);
692
693
9.74M
  return ret;
694
9.75M
}
695
/* }}} */
696
697
71.1k
#define LITERAL_STR(op, str) do { \
698
71.1k
    zval _c; \
699
71.1k
    ZVAL_STR(&_c, str); \
700
71.1k
    op.constant = zend_add_literal(&_c); \
701
71.1k
  } while (0)
702
703
void zend_stop_lexing(void)
704
85
{
705
85
  if (LANG_SCNG(on_event)) {
706
0
    LANG_SCNG(on_event)(ON_STOP, END, 0, NULL, 0, LANG_SCNG(on_event_context));
707
0
  }
708
709
85
  LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
710
85
}
711
712
static inline void zend_begin_loop(
713
    uint8_t free_opcode, const znode *loop_var, bool is_switch) /* {{{ */
714
52.6k
{
715
52.6k
  zend_brk_cont_element *brk_cont_element;
716
52.6k
  int parent = CG(context).current_brk_cont;
717
52.6k
  zend_loop_var info = {0};
718
719
52.6k
  CG(context).current_brk_cont = CG(context).last_brk_cont;
720
52.6k
  brk_cont_element = get_next_brk_cont_element();
721
52.6k
  brk_cont_element->parent = parent;
722
52.6k
  brk_cont_element->is_switch = is_switch;
723
724
52.6k
  if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
725
18.2k
    uint32_t start = get_next_op_number();
726
727
18.2k
    info.opcode = free_opcode;
728
18.2k
    info.var_type = loop_var->op_type;
729
18.2k
    info.var_num = loop_var->u.op.var;
730
18.2k
    brk_cont_element->start = start;
731
34.3k
  } else {
732
34.3k
    info.opcode = ZEND_NOP;
733
    /* The start field is used to free temporary variables in case of exceptions.
734
     * We won't try to free something of we don't have loop variable.  */
735
34.3k
    brk_cont_element->start = -1;
736
34.3k
  }
737
738
52.6k
  zend_stack_push(&CG(loop_var_stack), &info);
739
52.6k
}
740
/* }}} */
741
742
static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
743
52.4k
{
744
52.4k
  uint32_t end = get_next_op_number();
745
52.4k
  zend_brk_cont_element *brk_cont_element
746
52.4k
    = &CG(context).brk_cont_array[CG(context).current_brk_cont];
747
52.4k
  brk_cont_element->cont = cont_addr;
748
52.4k
  brk_cont_element->brk = end;
749
52.4k
  CG(context).current_brk_cont = brk_cont_element->parent;
750
751
52.4k
  zend_stack_del_top(&CG(loop_var_stack));
752
52.4k
}
753
/* }}} */
754
755
bool zend_op_may_elide_result(uint8_t opcode)
756
4.04M
{
757
4.04M
  switch (opcode) {
758
116k
    case ZEND_ASSIGN:
759
127k
    case ZEND_ASSIGN_DIM:
760
138k
    case ZEND_ASSIGN_OBJ:
761
140k
    case ZEND_ASSIGN_STATIC_PROP:
762
167k
    case ZEND_ASSIGN_OP:
763
169k
    case ZEND_ASSIGN_DIM_OP:
764
170k
    case ZEND_ASSIGN_OBJ_OP:
765
170k
    case ZEND_ASSIGN_STATIC_PROP_OP:
766
170k
    case ZEND_PRE_INC_STATIC_PROP:
767
170k
    case ZEND_PRE_DEC_STATIC_PROP:
768
171k
    case ZEND_PRE_INC_OBJ:
769
171k
    case ZEND_PRE_DEC_OBJ:
770
172k
    case ZEND_PRE_INC:
771
172k
    case ZEND_PRE_DEC:
772
287k
    case ZEND_DO_FCALL:
773
296k
    case ZEND_DO_ICALL:
774
303k
    case ZEND_DO_UCALL:
775
1.38M
    case ZEND_DO_FCALL_BY_NAME:
776
1.38M
    case ZEND_YIELD:
777
1.38M
    case ZEND_YIELD_FROM:
778
1.39M
    case ZEND_INCLUDE_OR_EVAL:
779
1.39M
      return true;
780
2.65M
    default:
781
2.65M
      return false;
782
4.04M
  }
783
4.04M
}
784
785
static void zend_do_free(znode *op1) /* {{{ */
786
4.37M
{
787
4.37M
  if (op1->op_type == IS_TMP_VAR) {
788
4.12M
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
789
790
4.29M
    while (opline->opcode == ZEND_END_SILENCE ||
791
4.27M
           opline->opcode == ZEND_OP_DATA) {
792
163k
      opline--;
793
163k
    }
794
795
4.12M
    if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
796
4.03M
      switch (opline->opcode) {
797
1.70k
        case ZEND_BOOL:
798
12.1k
        case ZEND_BOOL_NOT:
799
          /* boolean results don't have to be freed */
800
12.1k
          return;
801
84
        case ZEND_POST_INC_STATIC_PROP:
802
251
        case ZEND_POST_DEC_STATIC_PROP:
803
636
        case ZEND_POST_INC_OBJ:
804
748
        case ZEND_POST_DEC_OBJ:
805
6.27k
        case ZEND_POST_INC:
806
6.66k
        case ZEND_POST_DEC:
807
          /* convert $i++ to ++$i */
808
6.66k
          opline->opcode -= 2;
809
6.66k
          SET_UNUSED(opline->result);
810
6.66k
          return;
811
4.01M
        default:
812
4.01M
          if (zend_op_may_elide_result(opline->opcode)) {
813
1.38M
            SET_UNUSED(opline->result);
814
1.38M
            return;
815
1.38M
          }
816
2.62M
          break;
817
4.03M
      }
818
4.03M
    }
819
820
2.71M
    zend_emit_op(NULL, ZEND_FREE, op1, NULL);
821
2.71M
  } else if (op1->op_type == IS_VAR) {
822
77.3k
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
823
77.3k
    while (opline->opcode == ZEND_END_SILENCE ||
824
77.3k
        opline->opcode == ZEND_EXT_FCALL_END ||
825
77.3k
        opline->opcode == ZEND_OP_DATA) {
826
34
      opline--;
827
34
    }
828
77.3k
    if (opline->result_type == IS_VAR
829
75.8k
      && opline->result.var == op1->u.op.var) {
830
75.8k
      if (opline->opcode == ZEND_FETCH_THIS) {
831
0
        opline->opcode = ZEND_NOP;
832
0
      }
833
75.8k
      if (!ZEND_OP_IS_FRAMELESS_ICALL(opline->opcode)) {
834
75.8k
        SET_UNUSED(opline->result);
835
75.8k
      } else {
836
        /* Frameless calls usually use the return value, so always emit a free. This should be
837
         * faster than checking RETURN_VALUE_USED inside the handler. */
838
0
        zend_emit_op(NULL, ZEND_FREE, op1, NULL);
839
0
      }
840
75.8k
    } else {
841
7.24k
      while (opline >= CG(active_op_array)->opcodes) {
842
7.24k
        if ((opline->opcode == ZEND_FETCH_LIST_R ||
843
7.09k
             opline->opcode == ZEND_FETCH_LIST_W ||
844
5.02k
             opline->opcode == ZEND_EXT_STMT) &&
845
2.22k
            opline->op1_type == IS_VAR &&
846
2.22k
            opline->op1.var == op1->u.op.var) {
847
1.46k
          zend_emit_op(NULL, ZEND_FREE, op1, NULL);
848
1.46k
          return;
849
1.46k
        }
850
5.78k
        if (opline->result_type == IS_VAR
851
3.27k
          && opline->result.var == op1->u.op.var) {
852
0
          if (opline->opcode == ZEND_NEW) {
853
0
            zend_emit_op(NULL, ZEND_FREE, op1, NULL);
854
0
          }
855
0
          break;
856
0
        }
857
5.78k
        opline--;
858
5.78k
      }
859
1.46k
    }
860
169k
  } else if (op1->op_type == IS_CONST) {
861
    /* Destroy value without using GC: When opcache moves arrays into SHM it will
862
     * free the zend_array structure, so references to it from outside the op array
863
     * become invalid. GC would cause such a reference in the root buffer. */
864
158k
    zval_ptr_dtor_nogc(&op1->u.constant);
865
158k
  }
866
4.37M
}
867
/* }}} */
868
869
870
static const char *zend_modifier_token_to_string(uint32_t token)
871
71
{
872
71
  switch (token) {
873
5
    case T_PUBLIC:
874
5
      return "public";
875
2
    case T_PROTECTED:
876
2
      return "protected";
877
9
    case T_PRIVATE:
878
9
      return "private";
879
13
    case T_STATIC:
880
13
      return "static";
881
10
    case T_FINAL:
882
10
      return "final";
883
21
    case T_READONLY:
884
21
      return "readonly";
885
7
    case T_ABSTRACT:
886
7
      return "abstract";
887
2
    case T_PUBLIC_SET:
888
2
      return "public(set)";
889
1
    case T_PROTECTED_SET:
890
1
      return "protected(set)";
891
1
    case T_PRIVATE_SET:
892
1
      return "private(set)";
893
0
    default: ZEND_UNREACHABLE();
894
71
  }
895
71
}
896
897
uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t token)
898
60.6k
{
899
60.6k
  switch (token) {
900
39.6k
    case T_PUBLIC:
901
39.6k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
902
39.6k
        return ZEND_ACC_PUBLIC;
903
39.6k
      }
904
5
      break;
905
2.26k
    case T_PROTECTED:
906
2.26k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
907
2.26k
        return ZEND_ACC_PROTECTED;
908
2.26k
      }
909
2
      break;
910
5.41k
    case T_PRIVATE:
911
5.41k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
912
5.40k
        return ZEND_ACC_PRIVATE;
913
5.40k
      }
914
9
      break;
915
911
    case T_READONLY:
916
911
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
917
897
        return ZEND_ACC_READONLY;
918
897
      }
919
14
      break;
920
1.03k
    case T_ABSTRACT:
921
1.03k
      if (target == ZEND_MODIFIER_TARGET_METHOD || target == ZEND_MODIFIER_TARGET_PROPERTY) {
922
1.03k
        return ZEND_ACC_ABSTRACT;
923
1.03k
      }
924
2
      break;
925
1.45k
    case T_FINAL:
926
1.45k
      return ZEND_ACC_FINAL;
927
8.85k
    case T_STATIC:
928
8.85k
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_METHOD) {
929
8.85k
        return ZEND_ACC_STATIC;
930
8.85k
      }
931
9
      break;
932
342
    case T_PUBLIC_SET:
933
342
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
934
340
        return ZEND_ACC_PUBLIC_SET;
935
340
      }
936
2
      break;
937
272
    case T_PROTECTED_SET:
938
272
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
939
271
        return ZEND_ACC_PROTECTED_SET;
940
271
      }
941
1
      break;
942
362
    case T_PRIVATE_SET:
943
362
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
944
361
        return ZEND_ACC_PRIVATE_SET;
945
361
      }
946
1
      break;
947
60.6k
  }
948
949
45
  char *member;
950
45
  if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
951
0
    member = "property";
952
45
  } else if (target == ZEND_MODIFIER_TARGET_METHOD) {
953
12
    member = "method";
954
33
  } else if (target == ZEND_MODIFIER_TARGET_CONSTANT) {
955
5
    member = "class constant";
956
28
  } else if (target == ZEND_MODIFIER_TARGET_CPP) {
957
6
    member = "parameter";
958
22
  } else if (target == ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
959
22
    member = "property hook";
960
22
  } else {
961
0
    ZEND_UNREACHABLE();
962
0
  }
963
964
45
  zend_throw_exception_ex(zend_ce_compile_error, 0,
965
45
    "Cannot use the %s modifier on a %s", zend_modifier_token_to_string(token), member);
966
45
  return 0;
967
45
}
968
969
uint32_t zend_modifier_list_to_flags(zend_modifier_target target, zend_ast *modifiers)
970
51.5k
{
971
51.5k
  uint32_t flags = 0;
972
51.5k
  const zend_ast_list *modifier_list = zend_ast_get_list(modifiers);
973
974
111k
  for (uint32_t i = 0; i < modifier_list->children; i++) {
975
59.7k
    uint32_t token = (uint32_t) Z_LVAL_P(zend_ast_get_zval(modifier_list->child[i]));
976
59.7k
    uint32_t new_flag = zend_modifier_token_to_flag(target, token);
977
59.7k
    if (!new_flag) {
978
40
      return 0;
979
40
    }
980
    /* Don't error immediately for duplicate flags, we want to prioritize the errors from zend_add_member_modifier(). */
981
59.7k
    bool duplicate_flag = (flags & new_flag);
982
59.7k
    flags = zend_add_member_modifier(flags, new_flag, target);
983
59.7k
    if (!flags) {
984
62
      return 0;
985
62
    }
986
59.6k
    if (duplicate_flag) {
987
26
      zend_throw_exception_ex(zend_ce_compile_error, 0,
988
26
        "Multiple %s modifiers are not allowed", zend_modifier_token_to_string(token));
989
26
      return 0;
990
26
    }
991
59.6k
  }
992
993
51.3k
  return flags;
994
51.5k
}
995
996
uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
997
200
{
998
200
  uint32_t new_flags = flags | new_flag;
999
200
  if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
1000
4
    zend_throw_exception(zend_ce_compile_error,
1001
4
      "Multiple abstract modifiers are not allowed", 0);
1002
4
    return 0;
1003
4
  }
1004
196
  if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
1005
6
    zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
1006
6
    return 0;
1007
6
  }
1008
190
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
1009
6
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
1010
6
    return 0;
1011
6
  }
1012
184
  if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
1013
5
    zend_throw_exception(zend_ce_compile_error,
1014
5
      "Cannot use the final modifier on an abstract class", 0);
1015
5
    return 0;
1016
5
  }
1017
179
  return new_flags;
1018
184
}
1019
/* }}} */
1020
1021
uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag)
1022
41
{
1023
41
  uint32_t new_flags = flags | new_flag;
1024
41
  if (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
1025
5
    zend_throw_exception(zend_ce_compile_error,
1026
5
      "Cannot use the abstract modifier on an anonymous class", 0);
1027
5
    return 0;
1028
5
  }
1029
36
  if (new_flag & ZEND_ACC_FINAL) {
1030
6
    zend_throw_exception(zend_ce_compile_error, "Cannot use the final modifier on an anonymous class", 0);
1031
6
    return 0;
1032
6
  }
1033
30
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
1034
5
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
1035
5
    return 0;
1036
5
  }
1037
25
  return new_flags;
1038
30
}
1039
1040
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target) /* {{{ */
1041
59.7k
{
1042
59.7k
  uint32_t new_flags = flags | new_flag;
1043
59.7k
  if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
1044
42
    zend_throw_exception(zend_ce_compile_error,
1045
42
      "Multiple access type modifiers are not allowed", 0);
1046
42
    return 0;
1047
42
  }
1048
59.6k
  if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
1049
10
    if (target == ZEND_MODIFIER_TARGET_METHOD) {
1050
5
      zend_throw_exception(zend_ce_compile_error,
1051
5
        "Cannot use the final modifier on an abstract method", 0);
1052
5
      return 0;
1053
5
    }
1054
5
    if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
1055
5
      zend_throw_exception(zend_ce_compile_error,
1056
5
        "Cannot use the final modifier on an abstract property", 0);
1057
5
      return 0;
1058
5
    }
1059
5
  }
1060
59.6k
  if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
1061
29.2k
    if ((flags & ZEND_ACC_PPP_SET_MASK) && (new_flag & ZEND_ACC_PPP_SET_MASK)) {
1062
10
      zend_throw_exception(zend_ce_compile_error,
1063
10
        "Multiple access type modifiers are not allowed", 0);
1064
10
      return 0;
1065
10
    }
1066
29.2k
  }
1067
59.6k
  return new_flags;
1068
59.6k
}
1069
/* }}} */
1070
1071
17.1k
ZEND_API zend_string *zend_create_member_string(const zend_string *class_name, const zend_string *member_name) {
1072
17.1k
  return zend_string_concat3(
1073
17.1k
    ZSTR_VAL(class_name), ZSTR_LEN(class_name),
1074
17.1k
    "::", sizeof("::") - 1,
1075
17.1k
    ZSTR_VAL(member_name), ZSTR_LEN(member_name));
1076
17.1k
}
1077
1078
17.0M
static zend_string *zend_concat_names(const char *name1, size_t name1_len, const char *name2, size_t name2_len) {
1079
17.0M
  return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len);
1080
17.0M
}
1081
1082
17.7M
static zend_string *zend_prefix_with_ns(zend_string *name) {
1083
17.7M
  if (FC(current_namespace)) {
1084
17.0M
    const zend_string *ns = FC(current_namespace);
1085
17.0M
    return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
1086
17.0M
  } else {
1087
712k
    return zend_string_copy(name);
1088
712k
  }
1089
17.7M
}
1090
1091
static zend_string *zend_resolve_non_class_name(
1092
  zend_string *name, uint32_t type, bool *is_fully_qualified,
1093
  bool case_sensitive, const HashTable *current_import_sub
1094
13.9M
) {
1095
13.9M
  const char *compound;
1096
13.9M
  *is_fully_qualified = false;
1097
1098
13.9M
  if (ZSTR_VAL(name)[0] == '\\') {
1099
    /* Remove \ prefix (only relevant if this is a string rather than a label) */
1100
84
    *is_fully_qualified = true;
1101
84
    return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1102
84
  }
1103
1104
13.9M
  if (type == ZEND_NAME_FQ) {
1105
94.0k
    *is_fully_qualified = true;
1106
94.0k
    return zend_string_copy(name);
1107
94.0k
  }
1108
1109
13.8M
  if (type == ZEND_NAME_RELATIVE) {
1110
5.28k
    *is_fully_qualified = true;
1111
5.28k
    return zend_prefix_with_ns(name);
1112
5.28k
  }
1113
1114
13.8M
  if (current_import_sub) {
1115
    /* If an unqualified name is a function/const alias, replace it. */
1116
1.95k
    zend_string *import_name;
1117
1.95k
    if (case_sensitive) {
1118
1.46k
      import_name = zend_hash_find_ptr(current_import_sub, name);
1119
1.46k
    } else {
1120
497
      import_name = zend_hash_find_ptr_lc(current_import_sub, name);
1121
497
    }
1122
1123
1.95k
    if (import_name) {
1124
654
      *is_fully_qualified = true;
1125
654
      return zend_string_copy(import_name);
1126
654
    }
1127
1.95k
  }
1128
1129
13.8M
  compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1130
13.8M
  if (compound) {
1131
8.50k
    *is_fully_qualified = true;
1132
8.50k
  }
1133
1134
13.8M
  if (compound && FC(imports)) {
1135
    /* If the first part of a qualified name is an alias, substitute it. */
1136
2.74k
    size_t len = compound - ZSTR_VAL(name);
1137
2.74k
    const zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1138
1139
2.74k
    if (import_name) {
1140
705
      return zend_concat_names(
1141
705
        ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1142
705
    }
1143
2.74k
  }
1144
1145
13.8M
  return zend_prefix_with_ns(name);
1146
13.8M
}
1147
/* }}} */
1148
1149
static zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1150
3.99M
{
1151
3.99M
  return zend_resolve_non_class_name(
1152
3.99M
    name, type, is_fully_qualified, false, FC(imports_function));
1153
3.99M
}
1154
1155
static zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1156
9.96M
{
1157
9.96M
  return zend_resolve_non_class_name(
1158
9.96M
    name, type, is_fully_qualified, true, FC(imports_const));
1159
9.96M
}
1160
1161
static zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
1162
3.54M
{
1163
3.54M
  const char *compound;
1164
1165
3.54M
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1166
3.48k
    if (type == ZEND_NAME_FQ) {
1167
7
      zend_error_noreturn(E_COMPILE_ERROR,
1168
7
        "'\\%s' is an invalid class name", ZSTR_VAL(name));
1169
7
    }
1170
3.47k
    if (type == ZEND_NAME_RELATIVE) {
1171
0
      zend_error_noreturn(E_COMPILE_ERROR,
1172
0
        "'namespace\\%s' is an invalid class name", ZSTR_VAL(name));
1173
0
    }
1174
3.47k
    ZEND_ASSERT(type == ZEND_NAME_NOT_FQ);
1175
3.47k
    return zend_string_copy(name);
1176
3.47k
  }
1177
1178
3.54M
  if (type == ZEND_NAME_RELATIVE) {
1179
374
    return zend_prefix_with_ns(name);
1180
374
  }
1181
1182
3.54M
  if (type == ZEND_NAME_FQ) {
1183
18.8k
    if (ZSTR_VAL(name)[0] == '\\') {
1184
      /* Remove \ prefix (only relevant if this is a string rather than a label) */
1185
305
      name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1186
305
      if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1187
1
        zend_error_noreturn(E_COMPILE_ERROR,
1188
1
          "'\\%s' is an invalid class name", ZSTR_VAL(name));
1189
1
      }
1190
304
      return name;
1191
305
    }
1192
1193
18.5k
    return zend_string_copy(name);
1194
18.8k
  }
1195
1196
3.52M
  if (FC(imports)) {
1197
2.90k
    compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1198
2.90k
    if (compound) {
1199
      /* If the first part of a qualified name is an alias, substitute it. */
1200
688
      size_t len = compound - ZSTR_VAL(name);
1201
688
      const zend_string *import_name =
1202
688
        zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1203
1204
688
      if (import_name) {
1205
281
        return zend_concat_names(
1206
281
          ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1207
281
      }
1208
2.21k
    } else {
1209
      /* If an unqualified name is an alias, replace it. */
1210
2.21k
      zend_string *import_name
1211
2.21k
        = zend_hash_find_ptr_lc(FC(imports), name);
1212
1213
2.21k
      if (import_name) {
1214
806
        return zend_string_copy(import_name);
1215
806
      }
1216
2.21k
    }
1217
2.90k
  }
1218
1219
  /* If not fully qualified and not an alias, prepend the current namespace */
1220
3.52M
  return zend_prefix_with_ns(name);
1221
3.52M
}
1222
/* }}} */
1223
1224
static zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
1225
3.34M
{
1226
3.34M
  const zval *class_name = zend_ast_get_zval(ast);
1227
3.34M
  if (Z_TYPE_P(class_name) != IS_STRING) {
1228
29
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1229
29
  }
1230
3.34M
  return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
1231
3.34M
}
1232
/* }}} */
1233
1234
static void label_ptr_dtor(zval *zv) /* {{{ */
1235
2.81k
{
1236
2.81k
  efree_size(Z_PTR_P(zv), sizeof(zend_label));
1237
2.81k
}
1238
/* }}} */
1239
1240
1.67k
static void str_dtor(zval *zv)  /* {{{ */ {
1241
1.67k
  zend_string_release_ex(Z_STR_P(zv), 0);
1242
1.67k
}
1243
/* }}} */
1244
1245
static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
1246
30.0k
{
1247
30.0k
  zend_op_array *op_array = CG(active_op_array);
1248
30.0k
  uint32_t try_catch_offset = op_array->last_try_catch++;
1249
30.0k
  zend_try_catch_element *elem;
1250
1251
30.0k
  op_array->try_catch_array = safe_erealloc(
1252
30.0k
    op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
1253
1254
30.0k
  elem = &op_array->try_catch_array[try_catch_offset];
1255
30.0k
  elem->try_op = try_op;
1256
30.0k
  elem->catch_op = 0;
1257
30.0k
  elem->finally_op = 0;
1258
30.0k
  elem->finally_end = 0;
1259
1260
30.0k
  return try_catch_offset;
1261
30.0k
}
1262
/* }}} */
1263
1264
ZEND_API void function_add_ref(zend_function *function) /* {{{ */
1265
1.75k
{
1266
1.75k
  if (function->type == ZEND_USER_FUNCTION) {
1267
1.75k
    zend_op_array *op_array = &function->op_array;
1268
1.75k
    if (op_array->refcount) {
1269
290
      (*op_array->refcount)++;
1270
290
    }
1271
1272
1.75k
    ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
1273
1.75k
    ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
1274
1.75k
  }
1275
1276
1.75k
  if (function->common.function_name) {
1277
1.75k
    zend_string_addref(function->common.function_name);
1278
1.75k
  }
1279
1.75k
}
1280
/* }}} */
1281
1282
static zend_never_inline ZEND_COLD ZEND_NORETURN void do_bind_function_error(const zend_string *lcname, const zend_op_array *op_array, bool compile_time) /* {{{ */
1283
103
{
1284
103
  const zval *zv = zend_hash_find_known_hash(compile_time ? CG(function_table) : EG(function_table), lcname);
1285
103
  int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
1286
103
  const zend_function *old_function;
1287
1288
103
  ZEND_ASSERT(zv != NULL);
1289
103
  old_function = Z_PTR_P(zv);
1290
103
  if (old_function->type == ZEND_USER_FUNCTION
1291
98
    && old_function->op_array.last > 0) {
1292
98
    zend_error_noreturn(error_level, "Cannot redeclare function %s() (previously declared in %s:%d)",
1293
98
          op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name),
1294
98
          ZSTR_VAL(old_function->op_array.filename),
1295
98
          old_function->op_array.line_start);
1296
98
  } else {
1297
5
    zend_error_noreturn(error_level, "Cannot redeclare function %s()",
1298
5
      op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name));
1299
5
  }
1300
103
}
1301
1302
ZEND_API zend_result do_bind_function(zend_function *func, const zval *lcname) /* {{{ */
1303
117
{
1304
117
  zend_function *added_func = zend_hash_add_ptr(EG(function_table), Z_STR_P(lcname), func);
1305
117
  if (UNEXPECTED(!added_func)) {
1306
6
    do_bind_function_error(Z_STR_P(lcname), &func->op_array, false);
1307
0
    return FAILURE;
1308
6
  }
1309
1310
111
  if (func->op_array.refcount) {
1311
24
    ++*func->op_array.refcount;
1312
24
  }
1313
111
  if (func->common.function_name) {
1314
111
    zend_string_addref(func->common.function_name);
1315
111
  }
1316
111
  zend_observer_function_declared_notify(&func->op_array, Z_STR_P(lcname));
1317
111
  return SUCCESS;
1318
117
}
1319
/* }}} */
1320
1321
ZEND_API zend_class_entry *zend_bind_class_in_slot(
1322
    zval *class_table_slot, const zval *lcname, zend_string *lc_parent_name)
1323
7.10k
{
1324
7.10k
  zend_class_entry *ce = Z_PTR_P(class_table_slot);
1325
7.10k
  bool is_preloaded =
1326
7.10k
    (ce->ce_flags & ZEND_ACC_PRELOADED) && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD);
1327
7.10k
  bool success;
1328
7.10k
  if (EXPECTED(!is_preloaded)) {
1329
7.10k
    success = zend_hash_set_bucket_key(EG(class_table), (Bucket*) class_table_slot, Z_STR_P(lcname)) != NULL;
1330
7.10k
  } else {
1331
    /* If preloading is used, don't replace the existing bucket, add a new one. */
1332
0
    success = zend_hash_add_ptr(EG(class_table), Z_STR_P(lcname), ce) != NULL;
1333
0
  }
1334
7.10k
  if (UNEXPECTED(!success)) {
1335
131
    zend_class_entry *old_class = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1336
131
    ZEND_ASSERT(old_class);
1337
131
    zend_class_redeclaration_error(E_COMPILE_ERROR, old_class);
1338
131
    return NULL;
1339
131
  }
1340
1341
6.97k
  if (ce->ce_flags & ZEND_ACC_LINKED) {
1342
255
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1343
255
    return ce;
1344
255
  }
1345
1346
6.71k
  ce = zend_do_link_class(ce, lc_parent_name, Z_STR_P(lcname));
1347
6.71k
  if (ce) {
1348
5.37k
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1349
5.37k
    return ce;
1350
5.37k
  }
1351
1352
1.34k
  if (!is_preloaded) {
1353
    /* Reload bucket pointer, the hash table may have been reallocated */
1354
261
    zval *zv = zend_hash_find(EG(class_table), Z_STR_P(lcname));
1355
261
    zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(lcname + 1));
1356
1.08k
  } else {
1357
1.08k
    zend_hash_del(EG(class_table), Z_STR_P(lcname));
1358
1.08k
  }
1359
1.34k
  return NULL;
1360
6.71k
}
1361
1362
ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */
1363
6.52k
{
1364
6.52k
  zval *rtd_key, *zv;
1365
1366
6.52k
  rtd_key = lcname + 1;
1367
1368
6.52k
  zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(rtd_key));
1369
1370
6.52k
  if (UNEXPECTED(!zv)) {
1371
11
    const zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1372
11
    ZEND_ASSERT(ce);
1373
11
    zend_class_redeclaration_error(E_COMPILE_ERROR, ce);
1374
11
    return FAILURE;
1375
11
  }
1376
1377
  /* Register the derived class */
1378
6.51k
  return zend_bind_class_in_slot(zv, lcname, lc_parent_name) ? SUCCESS : FAILURE;
1379
6.52k
}
1380
/* }}} */
1381
1382
19.7k
static zend_string *add_type_string(zend_string *type, zend_string *new_type, bool is_intersection) {
1383
19.7k
  zend_string *result;
1384
19.7k
  if (type == NULL) {
1385
14.2k
    return zend_string_copy(new_type);
1386
14.2k
  }
1387
1388
5.51k
  if (is_intersection) {
1389
2.52k
    result = zend_string_concat3(ZSTR_VAL(type), ZSTR_LEN(type),
1390
2.52k
      "&", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1391
2.52k
    zend_string_release(type);
1392
2.99k
  } else {
1393
2.99k
    result = zend_string_concat3(
1394
2.99k
      ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1395
2.99k
    zend_string_release(type);
1396
2.99k
  }
1397
5.51k
  return result;
1398
19.7k
}
1399
1400
3.54k
static zend_string *resolve_class_name(zend_string *name, const zend_class_entry *scope) {
1401
3.54k
  if (scope) {
1402
2.07k
    if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1403
33
      name = scope->name;
1404
2.04k
    } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT)) && scope->parent) {
1405
0
      name = scope->parent->name;
1406
0
    }
1407
2.07k
  }
1408
1409
  /* The resolved name for anonymous classes contains null bytes. Cut off everything after the
1410
   * null byte here, to avoid larger parts of the type being omitted by printing code later. */
1411
3.54k
  size_t len = strlen(ZSTR_VAL(name));
1412
3.54k
  if (len != ZSTR_LEN(name)) {
1413
12
    return zend_string_init(ZSTR_VAL(name), len, 0);
1414
12
  }
1415
3.52k
  return zend_string_copy(name);
1416
3.54k
}
1417
1418
static zend_string *add_intersection_type(zend_string *str,
1419
  const zend_type_list *intersection_type_list,
1420
  bool is_bracketed)
1421
1.38k
{
1422
1.38k
  const zend_type *single_type;
1423
1.38k
  zend_string *intersection_str = NULL;
1424
1425
5.28k
  ZEND_TYPE_LIST_FOREACH(intersection_type_list, single_type) {
1426
5.28k
    ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*single_type));
1427
5.28k
    ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*single_type));
1428
1429
3.90k
    intersection_str = add_type_string(intersection_str, ZEND_TYPE_NAME(*single_type), /* is_intersection */ true);
1430
3.90k
  } ZEND_TYPE_LIST_FOREACH_END();
1431
1432
1.38k
  ZEND_ASSERT(intersection_str);
1433
1434
1.38k
  if (is_bracketed) {
1435
1.04k
    zend_string *result = zend_string_concat3("(", 1, ZSTR_VAL(intersection_str), ZSTR_LEN(intersection_str), ")", 1);
1436
1.04k
    zend_string_release(intersection_str);
1437
1.04k
    intersection_str = result;
1438
1.04k
  }
1439
1.38k
  str = add_type_string(str, intersection_str, /* is_intersection */ false);
1440
1.38k
  zend_string_release(intersection_str);
1441
1.38k
  return str;
1442
1.38k
}
1443
1444
15.3k
zend_string *zend_type_to_string_resolved(const zend_type type, const zend_class_entry *scope) {
1445
15.3k
  zend_string *str = NULL;
1446
1447
  /* Pure intersection type */
1448
15.3k
  if (ZEND_TYPE_IS_INTERSECTION(type)) {
1449
336
    ZEND_ASSERT(!ZEND_TYPE_IS_UNION(type));
1450
336
    str = add_intersection_type(str, ZEND_TYPE_LIST(type), /* is_bracketed */ false);
1451
14.9k
  } else if (ZEND_TYPE_HAS_LIST(type)) {
1452
    /* A union type might not be a list */
1453
564
    const zend_type *list_type;
1454
2.65k
    ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
1455
2.65k
      if (ZEND_TYPE_IS_INTERSECTION(*list_type)) {
1456
1.04k
        str = add_intersection_type(str, ZEND_TYPE_LIST(*list_type), /* is_bracketed */ true);
1457
1.04k
        continue;
1458
1.04k
      }
1459
1.04k
      ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type));
1460
1.04k
      ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*list_type));
1461
1462
1.04k
      zend_string *name = ZEND_TYPE_NAME(*list_type);
1463
1.04k
      zend_string *resolved = resolve_class_name(name, scope);
1464
1.04k
      str = add_type_string(str, resolved, /* is_intersection */ false);
1465
1.04k
      zend_string_release(resolved);
1466
1.04k
    } ZEND_TYPE_LIST_FOREACH_END();
1467
14.4k
  } else if (ZEND_TYPE_HAS_NAME(type)) {
1468
2.49k
    str = resolve_class_name(ZEND_TYPE_NAME(type), scope);
1469
2.49k
  }
1470
1471
15.3k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
1472
1473
15.3k
  if (type_mask == MAY_BE_ANY) {
1474
170
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_MIXED), /* is_intersection */ false);
1475
1476
170
    return str;
1477
170
  }
1478
15.1k
  if (type_mask & MAY_BE_STATIC) {
1479
148
    zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC);
1480
    // During compilation of eval'd code the called scope refers to the scope calling the eval
1481
148
    if (scope && !zend_is_compiling()) {
1482
50
      const zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
1483
50
      if (called_scope) {
1484
20
        name = called_scope->name;
1485
20
      }
1486
50
    }
1487
148
    str = add_type_string(str, name, /* is_intersection */ false);
1488
148
  }
1489
15.1k
  if (type_mask & MAY_BE_CALLABLE) {
1490
61
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE), /* is_intersection */ false);
1491
61
  }
1492
15.1k
  if (type_mask & MAY_BE_OBJECT) {
1493
234
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT), /* is_intersection */ false);
1494
234
  }
1495
15.1k
  if (type_mask & MAY_BE_ARRAY) {
1496
1.49k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY), /* is_intersection */ false);
1497
1.49k
  }
1498
15.1k
  if (type_mask & MAY_BE_STRING) {
1499
6.09k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING), /* is_intersection */ false);
1500
6.09k
  }
1501
15.1k
  if (type_mask & MAY_BE_LONG) {
1502
3.54k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT), /* is_intersection */ false);
1503
3.54k
  }
1504
15.1k
  if (type_mask & MAY_BE_DOUBLE) {
1505
511
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT), /* is_intersection */ false);
1506
511
  }
1507
15.1k
  if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
1508
551
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL), /* is_intersection */ false);
1509
14.5k
  } else if (type_mask & MAY_BE_FALSE) {
1510
144
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE), /* is_intersection */ false);
1511
14.4k
  } else if (type_mask & MAY_BE_TRUE) {
1512
41
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_TRUE), /* is_intersection */ false);
1513
41
  }
1514
15.1k
  if (type_mask & MAY_BE_VOID) {
1515
201
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID), /* is_intersection */ false);
1516
201
  }
1517
15.1k
  if (type_mask & MAY_BE_NEVER) {
1518
19
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NEVER), /* is_intersection */ false);
1519
19
  }
1520
1521
15.1k
  if (type_mask & MAY_BE_NULL) {
1522
1.41k
    bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
1523
1.41k
    bool has_intersection = !str || memchr(ZSTR_VAL(str), '&', ZSTR_LEN(str)) != NULL;
1524
1.41k
    if (!is_union && !has_intersection) {
1525
1.23k
      zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
1526
1.23k
      zend_string_release(str);
1527
1.23k
      return nullable_str;
1528
1.23k
    }
1529
1530
176
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE), /* is_intersection */ false);
1531
176
  }
1532
13.9k
  return str;
1533
15.1k
}
1534
1535
10.5k
ZEND_API zend_string *zend_type_to_string(zend_type type) {
1536
10.5k
  return zend_type_to_string_resolved(type, NULL);
1537
10.5k
}
1538
1539
1.62k
static bool is_generator_compatible_class_type(const zend_string *name) {
1540
1.62k
  return zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_TRAVERSABLE))
1541
1.29k
    || zend_string_equals_literal_ci(name, "Iterator")
1542
964
    || zend_string_equals_literal_ci(name, "Generator");
1543
1.62k
}
1544
1545
static void zend_mark_function_as_generator(void) /* {{{ */
1546
659k
{
1547
659k
  if (!CG(active_op_array)->function_name) {
1548
73
    zend_error_noreturn(E_COMPILE_ERROR,
1549
73
      "The \"yield\" expression can only be used inside a function");
1550
73
  }
1551
1552
659k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
1553
1.05k
    const zend_type return_type = CG(active_op_array)->arg_info[-1].type;
1554
1.05k
    bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_OBJECT) != 0;
1555
1.05k
    if (!valid_type) {
1556
1.02k
      const zend_type *single_type;
1557
2.65k
      ZEND_TYPE_FOREACH(return_type, single_type) {
1558
2.65k
        if (ZEND_TYPE_HAS_NAME(*single_type)
1559
1.62k
            && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) {
1560
994
          valid_type = true;
1561
994
          break;
1562
994
        }
1563
2.65k
      } ZEND_TYPE_FOREACH_END();
1564
1.02k
    }
1565
1566
1.05k
    if (!valid_type) {
1567
29
      zend_string *str = zend_type_to_string(return_type);
1568
29
      zend_error_noreturn(E_COMPILE_ERROR,
1569
29
        "Generator return type must be a supertype of Generator, %s given",
1570
29
        ZSTR_VAL(str));
1571
29
    }
1572
1.05k
  }
1573
1574
659k
  CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR;
1575
659k
}
1576
/* }}} */
1577
1578
ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal) /* {{{ */
1579
55.1k
{
1580
55.1k
  size_t prop_name_length = 1 + src1_length + 1 + src2_length;
1581
55.1k
  zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
1582
1583
55.1k
  ZSTR_VAL(prop_name)[0] = '\0';
1584
55.1k
  memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
1585
55.1k
  memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
1586
55.1k
  return prop_name;
1587
55.1k
}
1588
/* }}} */
1589
1590
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) /* {{{ */
1591
1.11M
{
1592
1.11M
  size_t class_name_len;
1593
1.11M
  size_t anonclass_src_len;
1594
1595
1.11M
  *class_name = NULL;
1596
1597
1.11M
  if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
1598
1.00M
    *prop_name = ZSTR_VAL(name);
1599
1.00M
    if (prop_len) {
1600
677k
      *prop_len = ZSTR_LEN(name);
1601
677k
    }
1602
1.00M
    return SUCCESS;
1603
1.00M
  }
1604
112k
  if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
1605
1.16k
    zend_error(E_NOTICE, "Illegal member variable name");
1606
1.16k
    *prop_name = ZSTR_VAL(name);
1607
1.16k
    if (prop_len) {
1608
961
      *prop_len = ZSTR_LEN(name);
1609
961
    }
1610
1.16k
    return FAILURE;
1611
1.16k
  }
1612
1613
111k
  class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
1614
111k
  if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
1615
389
    zend_error(E_NOTICE, "Corrupt member variable name");
1616
389
    *prop_name = ZSTR_VAL(name);
1617
389
    if (prop_len) {
1618
209
      *prop_len = ZSTR_LEN(name);
1619
209
    }
1620
389
    return FAILURE;
1621
389
  }
1622
1623
111k
  *class_name = ZSTR_VAL(name) + 1;
1624
111k
  anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2);
1625
111k
  if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) {
1626
25.1k
    class_name_len += anonclass_src_len + 1;
1627
25.1k
  }
1628
111k
  *prop_name = ZSTR_VAL(name) + class_name_len + 2;
1629
111k
  if (prop_len) {
1630
66.4k
    *prop_len = ZSTR_LEN(name) - class_name_len - 2;
1631
66.4k
  }
1632
111k
  return SUCCESS;
1633
111k
}
1634
/* }}} */
1635
1636
static bool array_is_const_ex(const zend_array *array, uint32_t *max_checks)
1637
226
{
1638
226
  if (zend_hash_num_elements(array) > *max_checks) {
1639
3
    return false;
1640
3
  }
1641
223
  *max_checks -= zend_hash_num_elements(array);
1642
1643
223
  zval *element;
1644
625
  ZEND_HASH_FOREACH_VAL(array, element) {
1645
625
    if (Z_TYPE_P(element) < IS_ARRAY) {
1646
51
      continue;
1647
150
    } else if (Z_TYPE_P(element) == IS_ARRAY) {
1648
150
      if (!array_is_const_ex(array, max_checks)) {
1649
150
        return false;
1650
150
      }
1651
150
    } else {
1652
0
      return false;
1653
0
    }
1654
625
  } ZEND_HASH_FOREACH_END();
1655
1656
73
  return true;
1657
223
}
1658
1659
static bool array_is_const(const zend_array *array)
1660
76
{
1661
76
  uint32_t max_checks = 50;
1662
76
  return array_is_const_ex(array, &max_checks);
1663
76
}
1664
1665
10.4k
static bool can_ct_eval_const(const zend_constant *c) {
1666
10.4k
  if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
1667
182
    return false;
1668
182
  }
1669
10.3k
  if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
1670
10.2k
      && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
1671
8.76k
      && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
1672
8.76k
        && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
1673
8.76k
    return true;
1674
8.76k
  }
1675
1.53k
  if (Z_TYPE(c->value) < IS_ARRAY
1676
1.53k
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1677
18
    return 1;
1678
1.51k
  } else if (Z_TYPE(c->value) == IS_ARRAY
1679
0
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)
1680
0
      && array_is_const(Z_ARR(c->value))) {
1681
0
    return 1;
1682
0
  }
1683
1.51k
  return false;
1684
1.53k
}
1685
1686
static bool zend_try_ct_eval_const(zval *zv, zend_string *name, bool is_fully_qualified) /* {{{ */
1687
9.96M
{
1688
  /* Substitute true, false and null (including unqualified usage in namespaces)
1689
   * before looking up the possibly namespaced name. */
1690
9.96M
  const char *lookup_name = ZSTR_VAL(name);
1691
9.96M
  size_t lookup_len = ZSTR_LEN(name);
1692
1693
9.96M
  if (!is_fully_qualified) {
1694
9.94M
    zend_get_unqualified_name(name, &lookup_name, &lookup_len);
1695
9.94M
  }
1696
1697
9.96M
  zend_constant *c;
1698
9.96M
  if ((c = zend_get_special_const(lookup_name, lookup_len))) {
1699
28.5k
    ZVAL_COPY_VALUE(zv, &c->value);
1700
28.5k
    return true;
1701
28.5k
  }
1702
9.93M
  c = zend_hash_find_ptr(EG(zend_constants), name);
1703
9.93M
  if (c && can_ct_eval_const(c)) {
1704
8.78k
    ZVAL_COPY_OR_DUP(zv, &c->value);
1705
8.78k
    return true;
1706
8.78k
  }
1707
9.92M
  return false;
1708
9.93M
}
1709
/* }}} */
1710
1711
static inline bool zend_is_scope_known(void) /* {{{ */
1712
38.1k
{
1713
38.1k
  if (!CG(active_op_array)) {
1714
    /* This can only happen when evaluating a default value string. */
1715
0
    return false;
1716
0
  }
1717
1718
38.1k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
1719
    /* Closures can be rebound to a different scope */
1720
25.1k
    return false;
1721
25.1k
  }
1722
1723
12.9k
  if (!CG(active_class_entry)) {
1724
    /* The scope is known if we're in a free function (no scope), but not if we're in
1725
     * a file/eval (which inherits including/eval'ing scope). */
1726
1.65k
    return CG(active_op_array)->function_name != NULL;
1727
1.65k
  }
1728
1729
  /* For traits self etc refers to the using class, not the trait itself */
1730
11.2k
  return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
1731
12.9k
}
1732
/* }}} */
1733
1734
static inline bool class_name_refers_to_active_ce(const zend_string *class_name, uint32_t fetch_type) /* {{{ */
1735
71.2k
{
1736
71.2k
  if (!CG(active_class_entry)) {
1737
65.2k
    return false;
1738
65.2k
  }
1739
5.99k
  if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
1740
1.62k
    return true;
1741
1.62k
  }
1742
4.37k
  return fetch_type == ZEND_FETCH_CLASS_DEFAULT
1743
3.41k
    && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
1744
5.99k
}
1745
/* }}} */
1746
1747
uint32_t zend_get_class_fetch_type(const zend_string *name) /* {{{ */
1748
4.57M
{
1749
4.57M
  if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1750
22.6k
    return ZEND_FETCH_CLASS_SELF;
1751
4.55M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT))) {
1752
5.74k
    return ZEND_FETCH_CLASS_PARENT;
1753
4.54M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_STATIC))) {
1754
1.73k
    return ZEND_FETCH_CLASS_STATIC;
1755
4.54M
  } else {
1756
4.54M
    return ZEND_FETCH_CLASS_DEFAULT;
1757
4.54M
  }
1758
4.57M
}
1759
/* }}} */
1760
1761
static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
1762
814k
{
1763
  /* Fully qualified names are always default refs */
1764
814k
  if (name_ast->attr == ZEND_NAME_FQ) {
1765
4.31k
    return ZEND_FETCH_CLASS_DEFAULT;
1766
4.31k
  }
1767
1768
809k
  return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
1769
814k
}
1770
/* }}} */
1771
1772
static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type)
1773
198k
{
1774
198k
  zend_string *class_name = zend_ast_get_str(ast);
1775
198k
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) {
1776
20
    zend_error_noreturn(E_COMPILE_ERROR,
1777
20
      "Cannot use \"%s\" as %s, as it is reserved",
1778
20
      ZSTR_VAL(class_name), type);
1779
20
  }
1780
198k
  return zend_resolve_class_name(class_name, ast->attr);
1781
198k
}
1782
1783
static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
1784
24.8k
{
1785
24.8k
  if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) {
1786
5.48k
    zend_class_entry *ce = CG(active_class_entry);
1787
5.48k
    if (!ce) {
1788
22
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
1789
22
        fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
1790
22
        fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
1791
5.45k
    } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) {
1792
19
      zend_error_noreturn(E_COMPILE_ERROR,
1793
19
        "Cannot use \"parent\" when current class scope has no parent");
1794
19
    }
1795
5.48k
  }
1796
24.8k
}
1797
/* }}} */
1798
1799
static bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */
1800
8.52k
{
1801
8.52k
  uint32_t fetch_type;
1802
8.52k
  const zval *class_name;
1803
1804
8.52k
  if (class_ast->kind != ZEND_AST_ZVAL) {
1805
2.28k
    return false;
1806
2.28k
  }
1807
1808
6.24k
  class_name = zend_ast_get_zval(class_ast);
1809
1810
6.24k
  if (Z_TYPE_P(class_name) != IS_STRING) {
1811
5
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1812
5
  }
1813
1814
6.23k
  fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name));
1815
6.23k
  zend_ensure_valid_class_fetch_type(fetch_type);
1816
1817
6.23k
  switch (fetch_type) {
1818
637
    case ZEND_FETCH_CLASS_SELF:
1819
637
      if (CG(active_class_entry) && zend_is_scope_known()) {
1820
233
        ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
1821
233
        return true;
1822
233
      }
1823
404
      return false;
1824
811
    case ZEND_FETCH_CLASS_PARENT:
1825
811
      if (CG(active_class_entry) && CG(active_class_entry)->parent_name
1826
486
          && zend_is_scope_known()) {
1827
486
        ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name);
1828
486
        return true;
1829
486
      }
1830
325
      return false;
1831
409
    case ZEND_FETCH_CLASS_STATIC:
1832
409
      return false;
1833
4.37k
    case ZEND_FETCH_CLASS_DEFAULT:
1834
4.37k
      ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
1835
4.37k
      return true;
1836
0
    default: ZEND_UNREACHABLE();
1837
6.23k
  }
1838
6.23k
}
1839
/* }}} */
1840
1841
/* We don't use zend_verify_const_access because we need to deal with unlinked classes. */
1842
static bool zend_verify_ct_const_access(const zend_class_constant *c, const zend_class_entry *scope)
1843
714
{
1844
714
  if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED) {
1845
14
    return 0;
1846
700
  } else if (c->ce->ce_flags & ZEND_ACC_TRAIT) {
1847
    /* This condition is only met on directly accessing trait constants,
1848
     * because the ce is replaced to the class entry of the composing class
1849
     * on binding. */
1850
1
    return 0;
1851
699
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) {
1852
445
    return 1;
1853
445
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) {
1854
254
    return c->ce == scope;
1855
254
  } else {
1856
0
    zend_class_entry *ce = c->ce;
1857
0
    while (1) {
1858
0
      if (ce == scope) {
1859
0
        return 1;
1860
0
      }
1861
0
      if (!ce->parent) {
1862
0
        break;
1863
0
      }
1864
0
      if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
1865
0
        ce = ce->parent;
1866
0
      } else {
1867
0
        ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name);
1868
0
        if (!ce) {
1869
0
          break;
1870
0
        }
1871
0
      }
1872
0
    }
1873
    /* Reverse case cannot be true during compilation */
1874
0
    return 0;
1875
0
  }
1876
714
}
1877
1878
static bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
1879
71.2k
{
1880
71.2k
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
1881
71.2k
  zend_class_constant *cc;
1882
71.2k
  zval *c;
1883
1884
71.2k
  if (class_name_refers_to_active_ce(class_name, fetch_type)) {
1885
1.73k
    cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
1886
69.5k
  } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1887
423
    const zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name);
1888
423
    if (ce) {
1889
83
      cc = zend_hash_find_ptr(&ce->constants_table, name);
1890
340
    } else {
1891
340
      return 0;
1892
340
    }
1893
69.1k
  } else {
1894
69.1k
    return 0;
1895
69.1k
  }
1896
1897
1.81k
  if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
1898
815
    return false;
1899
815
  }
1900
1901
1.00k
  if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) {
1902
305
    return false;
1903
305
  }
1904
1905
699
  c = &cc->value;
1906
1907
  /* Substitute case-sensitive (or lowercase) persistent class constants */
1908
699
  if (Z_TYPE_P(c) < IS_ARRAY) {
1909
434
    ZVAL_COPY_OR_DUP(zv, c);
1910
434
    return 1;
1911
434
  } else if (Z_TYPE_P(c) == IS_ARRAY && array_is_const(Z_ARR_P(c))) {
1912
73
    ZVAL_COPY_OR_DUP(zv, c);
1913
73
    return 1;
1914
73
  }
1915
1916
192
  return false;
1917
699
}
1918
/* }}} */
1919
1920
static void zend_add_to_list(void *result, void *item) /* {{{ */
1921
332k
{
1922
332k
  void** list = *(void**)result;
1923
332k
  size_t n = 0;
1924
1925
332k
  if (list) {
1926
619k
    while (list[n]) {
1927
397k
      n++;
1928
397k
    }
1929
221k
  }
1930
1931
332k
  list = erealloc(list, sizeof(void*) * (n+2));
1932
1933
332k
  list[n]   = item;
1934
332k
  list[n+1] = NULL;
1935
1936
332k
  *(void**)result = list;
1937
332k
}
1938
/* }}} */
1939
1940
static void zend_do_extended_stmt(znode* result) /* {{{ */
1941
1.90M
{
1942
1.90M
  zend_op *opline;
1943
1944
1.90M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) {
1945
1.90M
    return;
1946
1.90M
  }
1947
1948
0
  opline = get_next_op();
1949
1950
0
  opline->opcode = ZEND_EXT_STMT;
1951
0
  if (result) {
1952
0
    if (result->op_type == IS_CONST) {
1953
0
      Z_TRY_ADDREF(result->u.constant);
1954
0
    }
1955
0
    SET_NODE(opline->op1, result);
1956
0
  }
1957
0
}
1958
/* }}} */
1959
1960
static void zend_do_extended_fcall_begin(void) /* {{{ */
1961
4.36M
{
1962
4.36M
  zend_op *opline;
1963
1964
4.36M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1965
4.36M
    return;
1966
4.36M
  }
1967
1968
0
  opline = get_next_op();
1969
1970
0
  opline->opcode = ZEND_EXT_FCALL_BEGIN;
1971
0
}
1972
/* }}} */
1973
1974
static void zend_do_extended_fcall_end(void) /* {{{ */
1975
4.36M
{
1976
4.36M
  zend_op *opline;
1977
1978
4.36M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1979
4.36M
    return;
1980
4.36M
  }
1981
1982
0
  opline = get_next_op();
1983
1984
0
  opline->opcode = ZEND_EXT_FCALL_END;
1985
0
}
1986
/* }}} */
1987
1988
0
ZEND_API bool zend_is_auto_global_str(const char *name, size_t len) /* {{{ */ {
1989
0
  zend_auto_global *auto_global;
1990
1991
0
  if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) {
1992
0
    if (auto_global->armed) {
1993
0
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1994
0
    }
1995
0
    return 1;
1996
0
  }
1997
0
  return 0;
1998
0
}
1999
/* }}} */
2000
2001
ZEND_API bool zend_is_auto_global(zend_string *name) /* {{{ */
2002
3.15M
{
2003
3.15M
  zend_auto_global *auto_global;
2004
2005
3.15M
  if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) {
2006
3.53k
    if (auto_global->armed) {
2007
164
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
2008
164
    }
2009
3.53k
    return 1;
2010
3.53k
  }
2011
3.15M
  return 0;
2012
3.15M
}
2013
/* }}} */
2014
2015
ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
2016
128
{
2017
128
  zend_auto_global auto_global;
2018
128
  zend_result retval;
2019
2020
128
  auto_global.name = name;
2021
128
  auto_global.auto_global_callback = auto_global_callback;
2022
128
  auto_global.jit = jit;
2023
2024
128
  retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
2025
2026
128
  return retval;
2027
128
}
2028
/* }}} */
2029
2030
ZEND_API void zend_activate_auto_globals(void) /* {{{ */
2031
229k
{
2032
229k
  zend_auto_global *auto_global;
2033
2034
4.13M
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2035
4.13M
    auto_global->armed = auto_global->jit || auto_global->auto_global_callback;
2036
4.13M
  } ZEND_HASH_FOREACH_END();
2037
2038
4.13M
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2039
4.13M
    if (auto_global->armed && !auto_global->jit) {
2040
917k
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
2041
917k
    }
2042
4.13M
  } ZEND_HASH_FOREACH_END();
2043
229k
}
2044
/* }}} */
2045
2046
int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
2047
9.84M
{
2048
9.84M
  zval zv;
2049
9.84M
  int ret;
2050
2051
9.84M
  if (CG(increment_lineno)) {
2052
13.2k
    CG(zend_lineno)++;
2053
13.2k
    CG(increment_lineno) = 0;
2054
13.2k
  }
2055
2056
9.84M
  ret = lex_scan(&zv, elem);
2057
9.84M
  ZEND_ASSERT(!EG(exception) || ret == T_ERROR);
2058
9.84M
  return ret;
2059
2060
9.84M
}
2061
/* }}} */
2062
2063
ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers) /* {{{ */
2064
330k
{
2065
330k
  bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS;
2066
2067
330k
  ce->refcount = 1;
2068
330k
  ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
2069
330k
  ce->ce_flags2 = 0;
2070
2071
330k
  if (CG(compiler_options) & ZEND_COMPILE_GUARDS) {
2072
0
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2073
0
  }
2074
2075
330k
  ce->default_properties_table = NULL;
2076
330k
  ce->default_static_members_table = NULL;
2077
330k
  zend_hash_init(&ce->properties_info, 8, NULL, NULL, persistent_hashes);
2078
330k
  zend_hash_init(&ce->constants_table, 8, NULL, NULL, persistent_hashes);
2079
330k
  zend_hash_init(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes);
2080
2081
330k
  ce->doc_comment = NULL;
2082
2083
330k
  ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
2084
330k
  ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
2085
2086
330k
  ce->default_object_handlers = &std_object_handlers;
2087
330k
  ce->default_properties_count = 0;
2088
330k
  ce->default_static_members_count = 0;
2089
330k
  ce->properties_info_table = NULL;
2090
330k
  ce->attributes = NULL;
2091
330k
  ce->enum_backing_type = IS_UNDEF;
2092
330k
  ce->backed_enum_table = NULL;
2093
2094
330k
  if (nullify_handlers) {
2095
327k
    ce->constructor = NULL;
2096
327k
    ce->destructor = NULL;
2097
327k
    ce->clone = NULL;
2098
327k
    ce->__get = NULL;
2099
327k
    ce->__set = NULL;
2100
327k
    ce->__unset = NULL;
2101
327k
    ce->__isset = NULL;
2102
327k
    ce->__call = NULL;
2103
327k
    ce->__callstatic = NULL;
2104
327k
    ce->__tostring = NULL;
2105
327k
    ce->__serialize = NULL;
2106
327k
    ce->__unserialize = NULL;
2107
327k
    ce->__debugInfo = NULL;
2108
327k
    ce->create_object = NULL;
2109
327k
    ce->get_iterator = NULL;
2110
327k
    ce->iterator_funcs_ptr = NULL;
2111
327k
    ce->arrayaccess_funcs_ptr = NULL;
2112
327k
    ce->get_static_method = NULL;
2113
327k
    ce->parent = NULL;
2114
327k
    ce->parent_name = NULL;
2115
327k
    ce->num_interfaces = 0;
2116
327k
    ce->interfaces = NULL;
2117
327k
    ce->num_traits = 0;
2118
327k
    ce->num_hooked_props = 0;
2119
327k
    ce->num_hooked_prop_variance_checks = 0;
2120
327k
    ce->trait_names = NULL;
2121
327k
    ce->trait_aliases = NULL;
2122
327k
    ce->trait_precedences = NULL;
2123
327k
    ce->serialize = NULL;
2124
327k
    ce->unserialize = NULL;
2125
327k
    if (ce->type == ZEND_INTERNAL_CLASS) {
2126
0
      ce->info.internal.module = NULL;
2127
0
      ce->info.internal.builtin_functions = NULL;
2128
0
    }
2129
327k
  }
2130
330k
}
2131
/* }}} */
2132
2133
ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
2134
0
{
2135
0
  return op_array->vars[EX_VAR_TO_NUM(var)];
2136
0
}
2137
/* }}} */
2138
2139
zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
2140
0
{
2141
0
  zval *left_zv = zend_ast_get_zval(left_ast);
2142
0
  zend_string *left = Z_STR_P(left_zv);
2143
0
  zend_string *right = zend_ast_get_str(right_ast);
2144
2145
0
  zend_string *result;
2146
0
  size_t left_len = ZSTR_LEN(left);
2147
0
  size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
2148
2149
0
  result = zend_string_extend(left, len, 0);
2150
0
  ZSTR_VAL(result)[left_len] = '\\';
2151
0
  memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
2152
0
  ZSTR_VAL(result)[len] = '\0';
2153
0
  zend_string_release_ex(right, 0);
2154
2155
0
  ZVAL_STR(left_zv, result);
2156
0
  return left_ast;
2157
0
}
2158
/* }}} */
2159
2160
zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
2161
1.30k
{
2162
1.30k
  zval *zv = zend_ast_get_zval(ast);
2163
1.30k
  if (Z_TYPE_P(zv) == IS_LONG) {
2164
1.19k
    if (Z_LVAL_P(zv) == 0) {
2165
543
      ZVAL_NEW_STR(zv, ZSTR_INIT_LITERAL("-0", 0));
2166
656
    } else {
2167
656
      ZEND_ASSERT(Z_LVAL_P(zv) > 0);
2168
656
      Z_LVAL_P(zv) *= -1;
2169
656
    }
2170
1.19k
  } else if (Z_TYPE_P(zv) == IS_STRING) {
2171
107
    size_t orig_len = Z_STRLEN_P(zv);
2172
107
    Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0);
2173
107
    memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
2174
107
    Z_STRVAL_P(zv)[0] = '-';
2175
107
  } else {
2176
0
    ZEND_UNREACHABLE();
2177
0
  }
2178
1.30k
  return ast;
2179
1.30k
}
2180
/* }}} */
2181
2182
static void zend_verify_namespace(void) /* {{{ */
2183
461k
{
2184
461k
  if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
2185
48
    zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
2186
48
  }
2187
461k
}
2188
/* }}} */
2189
2190
/* {{{ zend_dirname
2191
   Returns directory name component of path */
2192
ZEND_API size_t zend_dirname(char *path, size_t len)
2193
855
{
2194
855
  char *end = path + len - 1;
2195
855
  unsigned int len_adjust = 0;
2196
2197
#ifdef ZEND_WIN32
2198
  /* Note that on Win32 CWD is per drive (heritage from CP/M).
2199
   * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
2200
   */
2201
  if ((2 <= len) && isalpha((unsigned char)path[0]) && (':' == path[1])) {
2202
    /* Skip over the drive spec (if any) so as not to change */
2203
    path += 2;
2204
    len_adjust += 2;
2205
    if (2 == len) {
2206
      /* Return "c:" on Win32 for dirname("c:").
2207
       * It would be more consistent to return "c:."
2208
       * but that would require making the string *longer*.
2209
       */
2210
      return len;
2211
    }
2212
  }
2213
#endif
2214
2215
855
  if (len == 0) {
2216
    /* Illegal use of this function */
2217
0
    return 0;
2218
0
  }
2219
2220
  /* Strip trailing slashes */
2221
855
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2222
0
    end--;
2223
0
  }
2224
855
  if (end < path) {
2225
    /* The path only contained slashes */
2226
0
    path[0] = DEFAULT_SLASH;
2227
0
    path[1] = '\0';
2228
0
    return 1 + len_adjust;
2229
0
  }
2230
2231
  /* Strip filename */
2232
9.44k
  while (end >= path && !IS_SLASH_P_EX(end, end == path)) {
2233
8.59k
    end--;
2234
8.59k
  }
2235
855
  if (end < path) {
2236
    /* No slash found, therefore return '.' */
2237
256
    path[0] = '.';
2238
256
    path[1] = '\0';
2239
256
    return 1 + len_adjust;
2240
256
  }
2241
2242
  /* Strip slashes which came before the file name */
2243
1.19k
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2244
599
    end--;
2245
599
  }
2246
599
  if (end < path) {
2247
5
    path[0] = DEFAULT_SLASH;
2248
5
    path[1] = '\0';
2249
5
    return 1 + len_adjust;
2250
5
  }
2251
594
  *(end+1) = '\0';
2252
2253
594
  return (size_t)(end + 1 - path) + len_adjust;
2254
599
}
2255
/* }}} */
2256
2257
static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */
2258
1.42M
{
2259
1.42M
  uint_fast8_t factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3;
2260
2261
1.42M
  switch (type) {
2262
638k
    case BP_VAR_R:
2263
638k
      opline->result_type = IS_TMP_VAR;
2264
638k
      result->op_type = IS_TMP_VAR;
2265
638k
      return;
2266
336k
    case BP_VAR_W:
2267
336k
      opline->opcode += 1 * factor;
2268
336k
      return;
2269
13.1k
    case BP_VAR_RW:
2270
13.1k
      opline->opcode += 2 * factor;
2271
13.1k
      return;
2272
307k
    case BP_VAR_IS:
2273
307k
      opline->result_type = IS_TMP_VAR;
2274
307k
      result->op_type = IS_TMP_VAR;
2275
307k
      opline->opcode += 3 * factor;
2276
307k
      return;
2277
122k
    case BP_VAR_FUNC_ARG:
2278
122k
      opline->opcode += 4 * factor;
2279
122k
      return;
2280
4.67k
    case BP_VAR_UNSET:
2281
4.67k
      opline->opcode += 5 * factor;
2282
4.67k
      return;
2283
0
    default: ZEND_UNREACHABLE();
2284
1.42M
  }
2285
1.42M
}
2286
/* }}} */
2287
2288
static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
2289
6.08M
{
2290
6.08M
  opline->result_type = IS_VAR;
2291
6.08M
  opline->result.var = get_temporary_variable();
2292
6.08M
  GET_NODE(result, opline->result);
2293
6.08M
}
2294
/* }}} */
2295
2296
static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
2297
34.7M
{
2298
34.7M
  opline->result_type = IS_TMP_VAR;
2299
34.7M
  opline->result.var = get_temporary_variable();
2300
34.7M
  GET_NODE(result, opline->result);
2301
34.7M
}
2302
/* }}} */
2303
2304
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2305
38.4M
{
2306
38.4M
  zend_op *opline = get_next_op();
2307
38.4M
  opline->opcode = opcode;
2308
2309
38.4M
  if (op1 != NULL) {
2310
30.6M
    SET_NODE(opline->op1, op1);
2311
30.6M
  }
2312
2313
38.4M
  if (op2 != NULL) {
2314
1.74M
    SET_NODE(opline->op2, op2);
2315
1.74M
  }
2316
2317
38.4M
  if (result) {
2318
5.03M
    zend_make_var_result(result, opline);
2319
5.03M
  }
2320
38.4M
  return opline;
2321
38.4M
}
2322
/* }}} */
2323
2324
static zend_op *zend_emit_op_tmp(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2325
37.2M
{
2326
37.2M
  zend_op *opline = get_next_op();
2327
37.2M
  opline->opcode = opcode;
2328
2329
37.2M
  if (op1 != NULL) {
2330
10.1M
    SET_NODE(opline->op1, op1);
2331
10.1M
  }
2332
2333
37.2M
  if (op2 != NULL) {
2334
4.36M
    SET_NODE(opline->op2, op2);
2335
4.36M
  }
2336
2337
37.2M
  if (result) {
2338
34.6M
    zend_make_tmp_result(result, opline);
2339
34.6M
  }
2340
2341
37.2M
  return opline;
2342
37.2M
}
2343
/* }}} */
2344
2345
static void zend_emit_tick(void) /* {{{ */
2346
24.7k
{
2347
24.7k
  zend_op *opline;
2348
2349
  /* This prevents a double TICK generated by the parser statement of "declare()" */
2350
24.7k
  if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
2351
3.20k
    return;
2352
3.20k
  }
2353
2354
21.5k
  opline = get_next_op();
2355
2356
21.5k
  opline->opcode = ZEND_TICKS;
2357
21.5k
  opline->extended_value = FC(declarables).ticks;
2358
21.5k
}
2359
/* }}} */
2360
2361
static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
2362
348k
{
2363
348k
  return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
2364
348k
}
2365
/* }}} */
2366
2367
static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
2368
970k
{
2369
970k
  uint32_t opnum = get_next_op_number();
2370
970k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
2371
970k
  opline->op1.opline_num = opnum_target;
2372
970k
  return opnum;
2373
970k
}
2374
/* }}} */
2375
2376
ZEND_API bool zend_is_smart_branch(const zend_op *opline) /* {{{ */
2377
141k
{
2378
141k
  switch (opline->opcode) {
2379
3.68k
    case ZEND_IS_IDENTICAL:
2380
4.21k
    case ZEND_IS_NOT_IDENTICAL:
2381
22.4k
    case ZEND_IS_EQUAL:
2382
23.5k
    case ZEND_IS_NOT_EQUAL:
2383
73.2k
    case ZEND_IS_SMALLER:
2384
75.1k
    case ZEND_IS_SMALLER_OR_EQUAL:
2385
78.3k
    case ZEND_CASE:
2386
79.1k
    case ZEND_CASE_STRICT:
2387
79.2k
    case ZEND_ISSET_ISEMPTY_CV:
2388
79.3k
    case ZEND_ISSET_ISEMPTY_VAR:
2389
79.8k
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2390
80.0k
    case ZEND_ISSET_ISEMPTY_PROP_OBJ:
2391
80.1k
    case ZEND_ISSET_ISEMPTY_STATIC_PROP:
2392
80.3k
    case ZEND_INSTANCEOF:
2393
80.9k
    case ZEND_TYPE_CHECK:
2394
81.0k
    case ZEND_DEFINED:
2395
81.0k
    case ZEND_IN_ARRAY:
2396
81.1k
    case ZEND_ARRAY_KEY_EXISTS:
2397
81.1k
      return 1;
2398
60.7k
    default:
2399
60.7k
      return 0;
2400
141k
  }
2401
141k
}
2402
/* }}} */
2403
2404
static inline uint32_t zend_emit_cond_jump(uint8_t opcode, znode *cond, uint32_t opnum_target) /* {{{ */
2405
155k
{
2406
155k
  uint32_t opnum = get_next_op_number();
2407
155k
  zend_op *opline;
2408
2409
155k
  if (cond->op_type == IS_TMP_VAR && opnum > 0) {
2410
114k
    opline = CG(active_op_array)->opcodes + opnum - 1;
2411
114k
    if (opline->result_type == IS_TMP_VAR
2412
94.7k
     && opline->result.var == cond->u.op.var
2413
94.7k
     && zend_is_smart_branch(opline)) {
2414
80.9k
      if (opcode == ZEND_JMPZ) {
2415
51.4k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ;
2416
51.4k
      } else {
2417
29.5k
        ZEND_ASSERT(opcode == ZEND_JMPNZ);
2418
29.5k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ;
2419
29.5k
      }
2420
80.9k
    }
2421
114k
  }
2422
155k
  opline = zend_emit_op(NULL, opcode, cond, NULL);
2423
155k
  opline->op2.opline_num = opnum_target;
2424
155k
  return opnum;
2425
155k
}
2426
/* }}} */
2427
2428
static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
2429
1.28M
{
2430
1.28M
  zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
2431
1.28M
  switch (opline->opcode) {
2432
955k
    case ZEND_JMP:
2433
955k
      opline->op1.opline_num = opnum_target;
2434
955k
      break;
2435
106k
    case ZEND_JMPZ:
2436
130k
    case ZEND_JMPNZ:
2437
135k
    case ZEND_JMPZ_EX:
2438
140k
    case ZEND_JMPNZ_EX:
2439
144k
    case ZEND_JMP_SET:
2440
332k
    case ZEND_COALESCE:
2441
332k
    case ZEND_JMP_NULL:
2442
333k
    case ZEND_BIND_INIT_STATIC_OR_JMP:
2443
333k
    case ZEND_JMP_FRAMELESS:
2444
333k
      opline->op2.opline_num = opnum_target;
2445
333k
      break;
2446
0
    default: ZEND_UNREACHABLE();
2447
1.28M
  }
2448
1.28M
}
2449
/* }}} */
2450
2451
static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
2452
1.28M
{
2453
1.28M
  zend_update_jump_target(opnum_jump, get_next_op_number());
2454
1.28M
}
2455
/* }}} */
2456
2457
static inline zend_op *zend_delayed_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2458
1.05M
{
2459
1.05M
  zend_op tmp_opline;
2460
2461
1.05M
  init_op(&tmp_opline);
2462
2463
1.05M
  tmp_opline.opcode = opcode;
2464
1.05M
  if (op1 != NULL) {
2465
1.05M
    SET_NODE(tmp_opline.op1, op1);
2466
1.05M
  }
2467
1.05M
  if (op2 != NULL) {
2468
1.03M
    SET_NODE(tmp_opline.op2, op2);
2469
1.03M
  }
2470
1.05M
  if (result) {
2471
1.04M
    zend_make_var_result(result, &tmp_opline);
2472
1.04M
  }
2473
2474
1.05M
  zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
2475
1.05M
  return zend_stack_top(&CG(delayed_oplines_stack));
2476
1.05M
}
2477
/* }}} */
2478
2479
static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
2480
1.15M
{
2481
1.15M
  return zend_stack_count(&CG(delayed_oplines_stack));
2482
1.15M
}
2483
/* }}} */
2484
2485
static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
2486
1.15M
{
2487
1.15M
  zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
2488
1.15M
  uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
2489
2490
1.15M
  ZEND_ASSERT(count >= offset);
2491
2.20M
  for (i = offset; i < count; ++i) {
2492
1.05M
    if (EXPECTED(oplines[i].opcode != ZEND_NOP)) {
2493
1.03M
      opline = get_next_op();
2494
1.03M
      memcpy(opline, &oplines[i], sizeof(zend_op));
2495
1.03M
    } else {
2496
18.0k
      opline = CG(active_op_array)->opcodes + oplines[i].extended_value;
2497
18.0k
    }
2498
1.05M
  }
2499
2500
1.15M
  CG(delayed_oplines_stack).top = offset;
2501
1.15M
  return opline;
2502
1.15M
}
2503
/* }}} */
2504
2505
static bool zend_ast_kind_is_short_circuited(zend_ast_kind ast_kind)
2506
55.2M
{
2507
55.2M
  switch (ast_kind) {
2508
911k
    case ZEND_AST_DIM:
2509
1.02M
    case ZEND_AST_PROP:
2510
1.16M
    case ZEND_AST_NULLSAFE_PROP:
2511
1.18M
    case ZEND_AST_STATIC_PROP:
2512
2.18M
    case ZEND_AST_METHOD_CALL:
2513
2.19M
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2514
2.26M
    case ZEND_AST_STATIC_CALL:
2515
2.26M
      return true;
2516
52.9M
    default:
2517
52.9M
      return false;
2518
55.2M
  }
2519
55.2M
}
2520
2521
static bool zend_ast_is_short_circuited(const zend_ast *ast)
2522
1.77M
{
2523
1.77M
  switch (ast->kind) {
2524
432k
    case ZEND_AST_DIM:
2525
471k
    case ZEND_AST_PROP:
2526
481k
    case ZEND_AST_STATIC_PROP:
2527
647k
    case ZEND_AST_METHOD_CALL:
2528
649k
    case ZEND_AST_STATIC_CALL:
2529
649k
      return zend_ast_is_short_circuited(ast->child[0]);
2530
412
    case ZEND_AST_NULLSAFE_PROP:
2531
536
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2532
536
      return true;
2533
1.12M
    default:
2534
1.12M
      return false;
2535
1.77M
  }
2536
1.77M
}
2537
2538
static void zend_assert_not_short_circuited(const zend_ast *ast)
2539
204k
{
2540
204k
  if (zend_ast_is_short_circuited(ast)) {
2541
40
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain");
2542
40
  }
2543
204k
}
2544
2545
/* Mark nodes that are an inner part of a short-circuiting chain.
2546
 * We should not perform a "commit" on them, as it will be performed by the outer-most node.
2547
 * We do this to avoid passing down an argument in various compile functions. */
2548
2549
2.27M
#define ZEND_SHORT_CIRCUITING_INNER 0x8000
2550
2551
1.33M
static void zend_short_circuiting_mark_inner(zend_ast *ast) {
2552
1.33M
  if (zend_ast_kind_is_short_circuited(ast->kind)) {
2553
733k
    ast->attr |= ZEND_SHORT_CIRCUITING_INNER;
2554
733k
  }
2555
1.33M
}
2556
2557
static uint32_t zend_short_circuiting_checkpoint(void)
2558
54.2M
{
2559
54.2M
  return zend_stack_count(&CG(short_circuiting_opnums));
2560
54.2M
}
2561
2562
static void zend_short_circuiting_commit(uint32_t checkpoint, znode *result, const zend_ast *ast)
2563
53.9M
{
2564
53.9M
  bool is_short_circuited = zend_ast_kind_is_short_circuited(ast->kind)
2565
52.4M
    || ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY;
2566
53.9M
  if (!is_short_circuited) {
2567
52.3M
    ZEND_ASSERT(zend_stack_count(&CG(short_circuiting_opnums)) == checkpoint
2568
52.3M
      && "Short circuiting stack should be empty");
2569
52.3M
    return;
2570
52.3M
  }
2571
2572
1.53M
  if (ast->attr & ZEND_SHORT_CIRCUITING_INNER) {
2573
    /* Outer-most node will commit. */
2574
526k
    return;
2575
526k
  }
2576
2577
1.09M
  while (zend_stack_count(&CG(short_circuiting_opnums)) != checkpoint) {
2578
80.7k
    uint32_t opnum = *(uint32_t *) zend_stack_top(&CG(short_circuiting_opnums));
2579
80.7k
    zend_op *opline = &CG(active_op_array)->opcodes[opnum];
2580
80.7k
    opline->op2.opline_num = get_next_op_number();
2581
80.7k
    SET_NODE(opline->result, result);
2582
80.7k
    opline->extended_value |=
2583
80.7k
      ast->kind == ZEND_AST_ISSET ? ZEND_SHORT_CIRCUITING_CHAIN_ISSET :
2584
80.7k
      ast->kind == ZEND_AST_EMPTY ? ZEND_SHORT_CIRCUITING_CHAIN_EMPTY :
2585
80.6k
                                    ZEND_SHORT_CIRCUITING_CHAIN_EXPR;
2586
80.7k
    zend_stack_del_top(&CG(short_circuiting_opnums));
2587
80.7k
  }
2588
1.01M
}
2589
2590
static void zend_emit_jmp_null(znode *obj_node, uint32_t bp_type)
2591
80.7k
{
2592
80.7k
  uint32_t jmp_null_opnum = get_next_op_number();
2593
80.7k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP_NULL, obj_node, NULL);
2594
80.7k
  if (opline->op1_type == IS_CONST) {
2595
704
    Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
2596
704
  }
2597
80.7k
  if (bp_type == BP_VAR_IS) {
2598
8.06k
    opline->extended_value |= ZEND_JMP_NULL_BP_VAR_IS;
2599
8.06k
  }
2600
80.7k
  zend_stack_push(&CG(short_circuiting_opnums), &jmp_null_opnum);
2601
80.7k
}
2602
2603
static inline bool zend_is_variable_or_call(const zend_ast *ast);
2604
2605
static void zend_compile_memoized_expr(znode *result, zend_ast *expr, uint32_t type) /* {{{ */
2606
935k
{
2607
935k
  const zend_memoize_mode memoize_mode = CG(memoize_mode);
2608
935k
  if (memoize_mode == ZEND_MEMOIZE_COMPILE) {
2609
468k
    znode memoized_result;
2610
2611
    /* Go through normal compilation */
2612
468k
    CG(memoize_mode) = ZEND_MEMOIZE_NONE;
2613
468k
    if (zend_is_variable_or_call(expr)) {
2614
183k
      zend_compile_var(result, expr, type, /* by_ref */ false);
2615
285k
    } else {
2616
285k
      zend_compile_expr(result, expr);
2617
285k
    }
2618
468k
    CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
2619
2620
468k
    if (result->op_type == IS_VAR) {
2621
179k
      zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL);
2622
288k
    } else if (result->op_type == IS_TMP_VAR) {
2623
277k
      zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL);
2624
277k
    } else {
2625
11.4k
      if (result->op_type == IS_CONST) {
2626
9.73k
        Z_TRY_ADDREF(result->u.constant);
2627
9.73k
      }
2628
11.4k
      memoized_result = *result;
2629
11.4k
    }
2630
2631
468k
    zend_hash_index_update_mem(
2632
468k
      CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode));
2633
468k
  } else if (memoize_mode == ZEND_MEMOIZE_FETCH) {
2634
467k
    const znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr);
2635
467k
    *result = *memoized_result;
2636
467k
    if (result->op_type == IS_CONST) {
2637
9.47k
      Z_TRY_ADDREF(result->u.constant);
2638
9.47k
    }
2639
467k
  } else {
2640
0
    ZEND_UNREACHABLE();
2641
0
  }
2642
935k
}
2643
/* }}} */
2644
2645
static void zend_emit_return_type_check(
2646
    znode *expr, const zend_arg_info *return_info, bool implicit) /* {{{ */
2647
112k
{
2648
112k
  zend_type type = return_info->type;
2649
112k
  if (ZEND_TYPE_IS_SET(type)) {
2650
112k
    zend_op *opline;
2651
2652
    /* `return ...;` is illegal in a void function (but `return;` isn't) */
2653
112k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) {
2654
5.47k
      if (expr) {
2655
20
        if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
2656
7
          zend_error_noreturn(E_COMPILE_ERROR,
2657
7
            "A void %s must not return a value "
2658
7
            "(did you mean \"return;\" instead of \"return null;\"?)",
2659
7
            CG(active_class_entry) != NULL ? "method" : "function");
2660
13
        } else {
2661
13
          zend_error_noreturn(E_COMPILE_ERROR, "A void %s must not return a value",
2662
13
          CG(active_class_entry) != NULL ? "method" : "function");
2663
13
        }
2664
20
      }
2665
      /* we don't need run-time check */
2666
5.45k
      return;
2667
5.47k
    }
2668
2669
    /* `return` is illegal in a never-returning function */
2670
106k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_NEVER)) {
2671
      /* Implicit case handled separately using VERIFY_NEVER_TYPE opcode. */
2672
9
      ZEND_ASSERT(!implicit);
2673
9
      zend_error_noreturn(E_COMPILE_ERROR, "A never-returning %s must not return",
2674
9
        CG(active_class_entry) != NULL ? "method" : "function");
2675
9
    }
2676
2677
106k
    if (!expr && !implicit) {
2678
15
      if (ZEND_TYPE_ALLOW_NULL(type)) {
2679
8
        zend_error_noreturn(E_COMPILE_ERROR,
2680
8
          "A %s with return type must return a value "
2681
8
          "(did you mean \"return null;\" instead of \"return;\"?)",
2682
8
          CG(active_class_entry) != NULL ? "method" : "function");
2683
8
      } else {
2684
7
        zend_error_noreturn(E_COMPILE_ERROR,
2685
7
          "A %s with return type must return a value",
2686
7
          CG(active_class_entry) != NULL ? "method" : "function");
2687
7
      }
2688
15
    }
2689
2690
106k
    if (expr && ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) {
2691
      /* we don't need run-time check for mixed return type */
2692
587
      return;
2693
587
    }
2694
2695
105k
    if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) {
2696
      /* we don't need run-time check */
2697
1.70k
      return;
2698
1.70k
    }
2699
2700
104k
    opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
2701
104k
    if (expr && expr->op_type == IS_CONST) {
2702
262
      opline->result_type = expr->op_type = IS_TMP_VAR;
2703
262
      opline->result.var = expr->u.op.var = get_temporary_variable();
2704
262
    }
2705
104k
  }
2706
112k
}
2707
/* }}} */
2708
2709
void zend_emit_final_return(bool return_one) /* {{{ */
2710
1.92M
{
2711
1.92M
  znode zn;
2712
1.92M
  zend_op *ret;
2713
1.92M
  bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
2714
2715
1.92M
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)
2716
105k
      && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) {
2717
104k
    zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
2718
2719
104k
    if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
2720
531
      zend_emit_op(NULL, ZEND_VERIFY_NEVER_TYPE, NULL, NULL);
2721
531
      return;
2722
531
    }
2723
2724
104k
    zend_emit_return_type_check(NULL, return_info, true);
2725
104k
  }
2726
2727
1.92M
  zn.op_type = IS_CONST;
2728
1.92M
  if (return_one) {
2729
73.8k
    ZVAL_LONG(&zn.u.constant, 1);
2730
1.84M
  } else {
2731
1.84M
    ZVAL_NULL(&zn.u.constant);
2732
1.84M
  }
2733
2734
1.92M
  ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
2735
1.92M
  ret->extended_value = -1;
2736
1.92M
}
2737
/* }}} */
2738
2739
static inline bool zend_is_variable(const zend_ast *ast) /* {{{ */
2740
5.89M
{
2741
5.89M
  return ast->kind == ZEND_AST_VAR
2742
5.77M
    || ast->kind == ZEND_AST_DIM
2743
5.69M
    || ast->kind == ZEND_AST_PROP
2744
5.68M
    || ast->kind == ZEND_AST_NULLSAFE_PROP
2745
5.68M
    || ast->kind == ZEND_AST_STATIC_PROP;
2746
5.89M
}
2747
/* }}} */
2748
2749
static bool zend_propagate_list_refs(zend_ast *ast);
2750
2751
static inline bool zend_is_passable_by_ref(const zend_ast *ast)
2752
5.17M
{
2753
5.17M
  if (zend_is_variable(ast) || ast->kind == ZEND_AST_ASSIGN_REF) {
2754
154k
    return true;
2755
154k
  }
2756
5.02M
  if (ast->kind == ZEND_AST_ASSIGN
2757
4.34k
   && UNEXPECTED(ast->child[0]->kind == ZEND_AST_ARRAY)
2758
32
   && zend_propagate_list_refs(ast->child[0])) {
2759
18
    return true;
2760
18
  }
2761
5.02M
  return false;
2762
5.02M
}
2763
2764
static inline bool zend_is_call(const zend_ast *ast) /* {{{ */
2765
6.62M
{
2766
6.62M
  return ast->kind == ZEND_AST_CALL
2767
5.98M
    || ast->kind == ZEND_AST_METHOD_CALL
2768
5.64M
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
2769
5.64M
    || ast->kind == ZEND_AST_STATIC_CALL
2770
5.63M
    || ast->kind == ZEND_AST_PIPE;
2771
6.62M
}
2772
/* }}} */
2773
2774
static inline bool zend_is_variable_or_call(const zend_ast *ast) /* {{{ */
2775
682k
{
2776
682k
  return zend_is_variable(ast) || zend_is_call(ast);
2777
682k
}
2778
/* }}} */
2779
2780
static inline bool zend_is_unticked_stmt(const zend_ast *ast) /* {{{ */
2781
31.8k
{
2782
31.8k
  return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL
2783
25.5k
    || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP
2784
25.2k
    || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD;
2785
31.8k
}
2786
/* }}} */
2787
2788
static inline bool zend_can_write_to_variable(const zend_ast *ast) /* {{{ */
2789
19.6k
{
2790
19.6k
  while (
2791
21.9k
    ast->kind == ZEND_AST_DIM
2792
20.7k
    || ast->kind == ZEND_AST_PROP
2793
19.6k
  ) {
2794
2.34k
    ast = ast->child[0];
2795
2.34k
  }
2796
2797
19.6k
  return zend_is_variable_or_call(ast) && !zend_ast_is_short_circuited(ast);
2798
19.6k
}
2799
/* }}} */
2800
2801
static inline bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
2802
38.1k
{
2803
38.1k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2804
0
    return false;
2805
0
  }
2806
2807
38.1k
  return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
2808
38.1k
}
2809
/* }}} */
2810
2811
static inline void zend_handle_numeric_op(znode *node) /* {{{ */
2812
9.29k
{
2813
9.29k
  if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
2814
7.42k
    zend_ulong index;
2815
2816
7.42k
    if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
2817
2.26k
      zval_ptr_dtor(&node->u.constant);
2818
2.26k
      ZVAL_LONG(&node->u.constant, index);
2819
2.26k
    }
2820
7.42k
  }
2821
9.29k
}
2822
/* }}} */
2823
2824
static inline void zend_handle_numeric_dim(const zend_op *opline, znode *dim_node) /* {{{ */
2825
267k
{
2826
267k
  if (Z_TYPE(dim_node->u.constant) == IS_STRING) {
2827
237k
    zend_ulong index;
2828
2829
237k
    if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) {
2830
      /* For numeric indexes we also keep the original value to use by ArrayAccess
2831
       * See bug #63217
2832
       */
2833
48.4k
      int c = zend_add_literal(&dim_node->u.constant);
2834
48.4k
      ZEND_ASSERT(opline->op2.constant + 1 == c);
2835
48.4k
      ZVAL_LONG(CT_CONSTANT(opline->op2), index);
2836
48.4k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE;
2837
48.4k
      return;
2838
48.4k
    }
2839
237k
  }
2840
267k
}
2841
/* }}} */
2842
2843
static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
2844
190k
{
2845
190k
  if (class_node->op_type == IS_CONST) {
2846
116k
    opline->op1_type = IS_CONST;
2847
116k
    opline->op1.constant = zend_add_class_name_literal(
2848
116k
      Z_STR(class_node->u.constant));
2849
116k
  } else {
2850
74.5k
    SET_NODE(opline->op1, class_node);
2851
74.5k
  }
2852
190k
}
2853
/* }}} */
2854
2855
static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */
2856
205k
{
2857
205k
  uint32_t fetch_type;
2858
2859
205k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2860
62.6k
    znode name_node;
2861
2862
62.6k
    zend_compile_expr(&name_node, name_ast);
2863
2864
62.6k
    if (name_node.op_type == IS_CONST) {
2865
798
      zend_string *name;
2866
2867
798
      if (Z_TYPE(name_node.u.constant) != IS_STRING) {
2868
7
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
2869
7
      }
2870
2871
791
      name = Z_STR(name_node.u.constant);
2872
791
      fetch_type = zend_get_class_fetch_type(name);
2873
2874
791
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
2875
553
        result->op_type = IS_CONST;
2876
553
        ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ));
2877
553
      } else {
2878
238
        zend_ensure_valid_class_fetch_type(fetch_type);
2879
238
        result->op_type = IS_UNUSED;
2880
238
        result->u.op.num = fetch_type | fetch_flags;
2881
238
      }
2882
2883
791
      zend_string_release_ex(name, 0);
2884
61.8k
    } else {
2885
61.8k
      zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
2886
61.8k
      opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags;
2887
61.8k
    }
2888
62.6k
    return;
2889
62.6k
  }
2890
2891
  /* Fully qualified names are always default refs */
2892
142k
  if (name_ast->attr == ZEND_NAME_FQ) {
2893
8.98k
    result->op_type = IS_CONST;
2894
8.98k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2895
8.98k
    return;
2896
8.98k
  }
2897
2898
133k
  fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
2899
133k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
2900
116k
    result->op_type = IS_CONST;
2901
116k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2902
116k
  } else {
2903
17.3k
    zend_ensure_valid_class_fetch_type(fetch_type);
2904
17.3k
    result->op_type = IS_UNUSED;
2905
17.3k
    result->u.op.num = fetch_type | fetch_flags;
2906
17.3k
  }
2907
133k
}
2908
/* }}} */
2909
2910
static zend_result zend_try_compile_cv(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
2911
1.47M
{
2912
1.47M
  zend_ast *name_ast = ast->child[0];
2913
1.47M
  if (name_ast->kind == ZEND_AST_ZVAL) {
2914
1.32M
    zval *zv = zend_ast_get_zval(name_ast);
2915
1.32M
    zend_string *name;
2916
2917
1.32M
    if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
2918
1.31M
      name = zval_make_interned_string(zv);
2919
1.31M
    } else {
2920
11.7k
      name = zend_new_interned_string(zval_get_string_func(zv));
2921
11.7k
    }
2922
2923
1.32M
    if (zend_is_auto_global(name)) {
2924
592
      return FAILURE;
2925
592
    }
2926
2927
1.32M
    if (!CG(context).has_assigned_to_http_response_header && zend_string_equals_literal(name, "http_response_header")) {
2928
294
      if (type == BP_VAR_R) {
2929
228
        zend_error(E_DEPRECATED,
2930
228
          "The predefined locally scoped $http_response_header variable is deprecated,"
2931
228
          " call http_get_last_response_headers() instead");
2932
228
      } else if (type == BP_VAR_W) {
2933
7
        CG(context).has_assigned_to_http_response_header = true;
2934
7
      }
2935
294
    }
2936
2937
1.32M
    result->op_type = IS_CV;
2938
1.32M
    result->u.op.var = lookup_cv(name);
2939
2940
1.32M
    if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) {
2941
11.7k
      zend_string_release_ex(name, 0);
2942
11.7k
    }
2943
2944
1.32M
    return SUCCESS;
2945
1.32M
  }
2946
2947
148k
  return FAILURE;
2948
1.47M
}
2949
/* }}} */
2950
2951
static zend_op *zend_compile_simple_var_no_cv(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2952
371k
{
2953
371k
  zend_ast *name_ast = ast->child[0];
2954
371k
  znode name_node;
2955
371k
  zend_op *opline;
2956
2957
371k
  zend_compile_expr(&name_node, name_ast);
2958
371k
  if (name_node.op_type == IS_CONST) {
2959
226k
    convert_to_string(&name_node.u.constant);
2960
226k
  }
2961
2962
371k
  if (delayed) {
2963
10.4k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2964
361k
  } else {
2965
361k
    opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2966
361k
  }
2967
2968
371k
  if (name_node.op_type == IS_CONST &&
2969
226k
      zend_is_auto_global(Z_STR(name_node.u.constant))) {
2970
2971
571
    opline->extended_value = ZEND_FETCH_GLOBAL;
2972
371k
  } else {
2973
371k
    if (name_node.op_type == IS_CONST
2974
225k
      && type == BP_VAR_R
2975
225k
      && zend_string_equals_literal(Z_STR(name_node.u.constant), "http_response_header")) {
2976
183
      zend_error(E_DEPRECATED,
2977
183
        "The predefined locally scoped $http_response_header variable is deprecated,"
2978
183
        " call http_get_last_response_headers() instead");
2979
183
    }
2980
371k
    opline->extended_value = ZEND_FETCH_LOCAL;
2981
371k
  }
2982
2983
371k
  zend_adjust_for_fetch_type(opline, result, type);
2984
371k
  return opline;
2985
371k
}
2986
/* }}} */
2987
2988
static bool is_this_fetch(const zend_ast *ast) /* {{{ */
2989
2.54M
{
2990
2.54M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2991
1.93M
    const zval *name = zend_ast_get_zval(ast->child[0]);
2992
1.93M
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals(Z_STR_P(name), ZSTR_KNOWN(ZEND_STR_THIS));
2993
1.93M
  }
2994
2995
608k
  return false;
2996
2.54M
}
2997
/* }}} */
2998
2999
static bool is_globals_fetch(const zend_ast *ast)
3000
8.70M
{
3001
8.70M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
3002
2.00M
    const zval *name = zend_ast_get_zval(ast->child[0]);
3003
2.00M
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "GLOBALS");
3004
2.00M
  }
3005
3006
6.70M
  return false;
3007
8.70M
}
3008
3009
static bool is_global_var_fetch(const zend_ast *ast)
3010
726k
{
3011
726k
  return ast->kind == ZEND_AST_DIM && is_globals_fetch(ast->child[0]);
3012
726k
}
3013
3014
static bool this_guaranteed_exists(void) /* {{{ */
3015
21.4k
{
3016
21.4k
  const zend_oparray_context *ctx = &CG(context);
3017
91.8k
  while (ctx) {
3018
    /* Instance methods always have a $this.
3019
     * This also includes closures that have a scope and use $this. */
3020
91.8k
    const zend_op_array *op_array = ctx->op_array;
3021
91.8k
    if (op_array->fn_flags & ZEND_ACC_STATIC) {
3022
28
      return false;
3023
91.8k
    } else if (op_array->scope) {
3024
12.1k
      return true;
3025
79.6k
    } else if (!(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
3026
9.29k
      return false;
3027
9.29k
    }
3028
70.3k
    ctx = ctx->prev;
3029
70.3k
  }
3030
0
  return false;
3031
21.4k
}
3032
/* }}} */
3033
3034
static zend_op *zend_compile_simple_var(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
3035
1.44M
{
3036
1.44M
  if (is_this_fetch(ast)) {
3037
7.71k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL);
3038
7.71k
    if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
3039
7.56k
      opline->result_type = IS_TMP_VAR;
3040
7.56k
      result->op_type = IS_TMP_VAR;
3041
7.56k
    }
3042
7.71k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3043
7.71k
    return opline;
3044
1.43M
  } else if (is_globals_fetch(ast)) {
3045
2.66k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_GLOBALS, NULL, NULL);
3046
2.66k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3047
2.46k
      opline->result_type = IS_TMP_VAR;
3048
2.46k
      result->op_type = IS_TMP_VAR;
3049
2.46k
    }
3050
2.66k
    return opline;
3051
1.43M
  } else if (zend_try_compile_cv(result, ast, type) == FAILURE) {
3052
147k
    return zend_compile_simple_var_no_cv(result, ast, type, delayed);
3053
147k
  }
3054
1.28M
  return NULL;
3055
1.44M
}
3056
/* }}} */
3057
3058
static void zend_separate_if_call_and_write(znode *node, const zend_ast *ast, uint32_t type) /* {{{ */
3059
1.01M
{
3060
1.01M
  if (type != BP_VAR_R
3061
754k
   && type != BP_VAR_IS
3062
   /* Whether a FUNC_ARG is R may only be determined at runtime. */
3063
451k
   && type != BP_VAR_FUNC_ARG
3064
330k
   && zend_is_call(ast)) {
3065
180k
    if (node->op_type == IS_VAR) {
3066
180k
      zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
3067
180k
      opline->result_type = IS_VAR;
3068
180k
      opline->result.var = opline->op1.var;
3069
180k
    } else {
3070
16
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3071
16
    }
3072
180k
  }
3073
1.01M
}
3074
/* }}} */
3075
3076
static inline void zend_emit_assign_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3077
5.80k
{
3078
5.80k
  znode dummy_node;
3079
5.80k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
3080
5.80k
    zend_ast_create_znode(value_node));
3081
5.80k
  zend_compile_expr(&dummy_node, assign_ast);
3082
5.80k
  zend_do_free(&dummy_node);
3083
5.80k
}
3084
/* }}} */
3085
3086
static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
3087
865k
{
3088
865k
  zend_ast *var_ast = ast->child[0];
3089
865k
  zend_ast *dim_ast = ast->child[1];
3090
865k
  zend_op *opline;
3091
3092
865k
  znode var_node, dim_node;
3093
3094
865k
  if (is_globals_fetch(var_ast)) {
3095
2.52k
    if (dim_ast == NULL) {
3096
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot append to $GLOBALS");
3097
7
    }
3098
3099
2.52k
    zend_compile_expr(&dim_node, dim_ast);
3100
2.52k
    if (dim_node.op_type == IS_CONST) {
3101
2.02k
      convert_to_string(&dim_node.u.constant);
3102
2.02k
    }
3103
3104
2.52k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &dim_node, NULL);
3105
2.52k
    opline->extended_value = ZEND_FETCH_GLOBAL;
3106
2.52k
    zend_adjust_for_fetch_type(opline, result, type);
3107
2.52k
    return opline;
3108
863k
  } else {
3109
863k
    zend_short_circuiting_mark_inner(var_ast);
3110
863k
    opline = zend_delayed_compile_var(&var_node, var_ast, type, false);
3111
863k
    if (opline) {
3112
344k
      if (type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) {
3113
3.20k
        opline->extended_value |= ZEND_FETCH_DIM_WRITE;
3114
341k
      } else if (opline->opcode == ZEND_FETCH_DIM_W
3115
245k
          || opline->opcode == ZEND_FETCH_DIM_RW
3116
244k
          || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3117
186k
          || opline->opcode == ZEND_FETCH_DIM_UNSET) {
3118
154k
        opline->extended_value = ZEND_FETCH_DIM_DIM;
3119
154k
      }
3120
344k
    }
3121
863k
  }
3122
3123
863k
  zend_separate_if_call_and_write(&var_node, var_ast, type);
3124
3125
863k
  if (dim_ast == NULL) {
3126
11.3k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3127
192
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
3128
192
    }
3129
11.1k
    if (type == BP_VAR_UNSET) {
3130
8
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
3131
8
    }
3132
11.1k
    dim_node.op_type = IS_UNUSED;
3133
852k
  } else {
3134
852k
    zend_compile_expr(&dim_node, dim_ast);
3135
852k
  }
3136
3137
863k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
3138
863k
  zend_adjust_for_fetch_type(opline, result, type);
3139
863k
  if (by_ref) {
3140
62.9k
    opline->extended_value = ZEND_FETCH_DIM_REF;
3141
62.9k
  }
3142
3143
863k
  if (dim_node.op_type == IS_CONST) {
3144
262k
    zend_handle_numeric_dim(opline, &dim_node);
3145
262k
  }
3146
863k
  return opline;
3147
863k
}
3148
3149
static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3150
510k
{
3151
510k
  uint32_t offset = zend_delayed_compile_begin();
3152
510k
  zend_delayed_compile_dim(result, ast, type, by_ref);
3153
510k
  return zend_delayed_compile_end(offset);
3154
510k
}
3155
/* }}} */
3156
3157
static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3158
170k
{
3159
170k
  zend_ast *obj_ast = ast->child[0];
3160
170k
  zend_ast *prop_ast = ast->child[1];
3161
3162
170k
  znode obj_node, prop_node;
3163
170k
  zend_op *opline;
3164
170k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_PROP;
3165
3166
170k
  if (is_this_fetch(obj_ast)) {
3167
20.0k
    if (this_guaranteed_exists()) {
3168
10.7k
      obj_node.op_type = IS_UNUSED;
3169
10.7k
    } else {
3170
9.23k
      opline = zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
3171
9.23k
      if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
3172
9.03k
        opline->result_type = IS_TMP_VAR;
3173
9.03k
        obj_node.op_type = IS_TMP_VAR;
3174
9.03k
      }
3175
9.23k
    }
3176
20.0k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3177
3178
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
3179
     * check for a nullsafe access. */
3180
150k
  } else {
3181
150k
    zend_short_circuiting_mark_inner(obj_ast);
3182
150k
    opline = zend_delayed_compile_var(&obj_node, obj_ast, type, false);
3183
150k
    if (opline && (opline->opcode == ZEND_FETCH_DIM_W
3184
40.0k
        || opline->opcode == ZEND_FETCH_DIM_RW
3185
39.8k
        || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3186
39.4k
        || opline->opcode == ZEND_FETCH_DIM_UNSET)) {
3187
1.56k
      opline->extended_value = ZEND_FETCH_DIM_OBJ;
3188
1.56k
    }
3189
3190
150k
    zend_separate_if_call_and_write(&obj_node, obj_ast, type);
3191
150k
    if (nullsafe) {
3192
77.5k
      if (obj_node.op_type == IS_TMP_VAR) {
3193
        /* Flush delayed oplines */
3194
48.0k
        zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
3195
48.0k
        uint32_t var = obj_node.u.op.var;
3196
48.0k
        uint32_t count = zend_stack_count(&CG(delayed_oplines_stack));
3197
48.0k
        uint32_t i = count;
3198
3199
133k
        while (i > 0 && oplines[i-1].result_type == IS_TMP_VAR && oplines[i-1].result.var == var) {
3200
86.0k
          i--;
3201
86.0k
          if (oplines[i].op1_type == IS_TMP_VAR) {
3202
84.9k
            var = oplines[i].op1.var;
3203
84.9k
          } else {
3204
1.03k
            break;
3205
1.03k
          }
3206
86.0k
        }
3207
134k
        for (; i < count; ++i) {
3208
86.0k
          if (oplines[i].opcode != ZEND_NOP) {
3209
18.0k
            opline = get_next_op();
3210
18.0k
            memcpy(opline, &oplines[i], sizeof(zend_op));
3211
18.0k
            oplines[i].opcode = ZEND_NOP;
3212
18.0k
            oplines[i].extended_value = opline - CG(active_op_array)->opcodes;
3213
18.0k
          }
3214
86.0k
        }
3215
48.0k
      }
3216
77.5k
      zend_emit_jmp_null(&obj_node, type);
3217
77.5k
    }
3218
150k
  }
3219
3220
170k
  zend_compile_expr(&prop_node, prop_ast);
3221
3222
170k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
3223
170k
  if (opline->op2_type == IS_CONST) {
3224
166k
    convert_to_string(CT_CONSTANT(opline->op2));
3225
166k
    zend_string_hash_val(Z_STR_P(CT_CONSTANT(opline->op2)));
3226
166k
    opline->extended_value = zend_alloc_cache_slots(3);
3227
166k
  }
3228
3229
170k
  zend_adjust_for_fetch_type(opline, result, type);
3230
3231
170k
  return opline;
3232
170k
}
3233
/* }}} */
3234
3235
static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3236
113k
{
3237
113k
  uint32_t offset = zend_delayed_compile_begin();
3238
113k
  zend_op *opline = zend_delayed_compile_prop(result, ast, type);
3239
113k
  if (by_ref) { /* shared with cache_slot */
3240
3.42k
    opline->extended_value |= ZEND_FETCH_REF;
3241
3.42k
  }
3242
113k
  return zend_delayed_compile_end(offset);
3243
113k
}
3244
/* }}} */
3245
3246
static zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref, bool delayed) /* {{{ */
3247
15.5k
{
3248
15.5k
  zend_ast *class_ast = ast->child[0];
3249
15.5k
  zend_ast *prop_ast = ast->child[1];
3250
3251
15.5k
  znode class_node, prop_node;
3252
15.5k
  zend_op *opline;
3253
3254
15.5k
  zend_short_circuiting_mark_inner(class_ast);
3255
15.5k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
3256
3257
15.5k
  zend_compile_expr(&prop_node, prop_ast);
3258
3259
15.5k
  if (delayed) {
3260
5.89k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3261
9.69k
  } else {
3262
9.69k
    opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3263
9.69k
  }
3264
15.5k
  if (opline->op1_type == IS_CONST) {
3265
13.9k
    convert_to_string(CT_CONSTANT(opline->op1));
3266
13.9k
    opline->extended_value = zend_alloc_cache_slots(3);
3267
13.9k
  }
3268
15.5k
  if (class_node.op_type == IS_CONST) {
3269
9.27k
    opline->op2_type = IS_CONST;
3270
9.27k
    opline->op2.constant = zend_add_class_name_literal(
3271
9.27k
      Z_STR(class_node.u.constant));
3272
9.27k
    if (opline->op1_type != IS_CONST) {
3273
1.08k
      opline->extended_value = zend_alloc_cache_slot();
3274
1.08k
    }
3275
9.27k
  } else {
3276
6.31k
    SET_NODE(opline->op2, &class_node);
3277
6.31k
  }
3278
3279
15.5k
  if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */
3280
1.46k
    opline->extended_value |= ZEND_FETCH_REF;
3281
1.46k
  }
3282
3283
15.5k
  zend_adjust_for_fetch_type(opline, result, type);
3284
15.5k
  return opline;
3285
15.5k
}
3286
/* }}} */
3287
3288
6.14k
static void zend_verify_list_assign_target(const zend_ast *var_ast, zend_ast_attr array_style) /* {{{ */ {
3289
6.14k
  if (var_ast->kind == ZEND_AST_ARRAY) {
3290
465
    if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) {
3291
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead");
3292
5
    }
3293
460
    if (array_style != var_ast->attr) {
3294
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()");
3295
5
    }
3296
5.68k
  } else if (!zend_can_write_to_variable(var_ast)) {
3297
93
    zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values");
3298
93
  }
3299
6.14k
}
3300
/* }}} */
3301
3302
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node);
3303
3304
/* Propagate refs used on leaf elements to the surrounding list() structures. */
3305
4.17k
static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
3306
4.17k
  const zend_ast_list *list = zend_ast_get_list(ast);
3307
4.17k
  bool has_refs = false;
3308
4.17k
  uint32_t i;
3309
3310
15.2k
  for (i = 0; i < list->children; ++i) {
3311
11.0k
    zend_ast *elem_ast = list->child[i];
3312
3313
11.0k
    if (elem_ast) {
3314
6.65k
      zend_ast *var_ast = elem_ast->child[0];
3315
6.65k
      if (var_ast->kind == ZEND_AST_ARRAY) {
3316
466
        elem_ast->attr = zend_propagate_list_refs(var_ast);
3317
466
      }
3318
6.65k
      has_refs |= elem_ast->attr;
3319
6.65k
    }
3320
11.0k
  }
3321
3322
4.17k
  return has_refs;
3323
4.17k
}
3324
/* }}} */
3325
3326
static bool list_is_keyed(const zend_ast_list *list)
3327
3.84k
{
3328
4.04k
  for (uint32_t i = 0; i < list->children; i++) {
3329
4.02k
    const zend_ast *child = list->child[i];
3330
4.02k
    if (child) {
3331
3.82k
      return child->kind == ZEND_AST_ARRAY_ELEM && child->child[1] != NULL;
3332
3.82k
    }
3333
4.02k
  }
3334
24
  return false;
3335
3.84k
}
3336
3337
static void zend_compile_list_assign(
3338
    znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style, uint32_t type) /* {{{ */
3339
3.84k
{
3340
3.84k
  zend_ast_list *list = zend_ast_get_list(ast);
3341
3.84k
  uint32_t i;
3342
3.84k
  bool has_elems = false;
3343
3.84k
  bool is_keyed = list_is_keyed(list);
3344
3345
3.84k
  if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
3346
61
    zval_make_interned_string(&expr_node->u.constant);
3347
61
  }
3348
3349
10.3k
  for (i = 0; i < list->children; ++i) {
3350
6.52k
    zend_ast *elem_ast = list->child[i];
3351
6.52k
    zend_ast *var_ast, *key_ast;
3352
6.52k
    znode fetch_result, dim_node;
3353
6.52k
    zend_op *opline;
3354
3355
6.52k
    if (elem_ast == NULL) {
3356
357
      if (is_keyed) {
3357
11
        zend_error(E_COMPILE_ERROR,
3358
11
          "Cannot use empty array entries in keyed array assignment");
3359
346
      } else {
3360
346
        continue;
3361
346
      }
3362
357
    }
3363
3364
6.18k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
3365
9
      zend_error(E_COMPILE_ERROR,
3366
9
          "Spread operator is not supported in assignments");
3367
9
    }
3368
3369
6.18k
    var_ast = elem_ast->child[0];
3370
6.18k
    key_ast = elem_ast->child[1];
3371
6.18k
    has_elems = true;
3372
3373
6.18k
    if (is_keyed) {
3374
1.04k
      if (key_ast == NULL) {
3375
6
        zend_error(E_COMPILE_ERROR,
3376
6
          "Cannot mix keyed and unkeyed array entries in assignments");
3377
6
      }
3378
3379
1.04k
      zend_compile_expr(&dim_node, key_ast);
3380
5.14k
    } else {
3381
5.14k
      if (key_ast != NULL) {
3382
7
        zend_error(E_COMPILE_ERROR,
3383
7
          "Cannot mix keyed and unkeyed array entries in assignments");
3384
7
      }
3385
3386
5.14k
      dim_node.op_type = IS_CONST;
3387
5.14k
      ZVAL_LONG(&dim_node.u.constant, i);
3388
5.14k
    }
3389
3390
6.18k
    if (expr_node->op_type == IS_CONST) {
3391
903
      Z_TRY_ADDREF(expr_node->u.constant);
3392
903
    }
3393
3394
6.18k
    zend_verify_list_assign_target(var_ast, array_style);
3395
3396
6.18k
    opline = zend_emit_op(&fetch_result,
3397
6.18k
      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);
3398
6.18k
    if (opline->opcode == ZEND_FETCH_LIST_R) {
3399
4.09k
      opline->result_type = IS_TMP_VAR;
3400
4.09k
      fetch_result.op_type = IS_TMP_VAR;
3401
4.09k
    }
3402
3403
6.18k
    if (dim_node.op_type == IS_CONST) {
3404
5.76k
      zend_handle_numeric_dim(opline, &dim_node);
3405
5.76k
    }
3406
3407
6.18k
    if (elem_ast->attr) {
3408
1.94k
      zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL);
3409
1.94k
    }
3410
6.18k
    if (var_ast->kind == ZEND_AST_ARRAY) {
3411
455
      zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr, type);
3412
5.72k
    } else if (elem_ast->attr) {
3413
1.85k
      zend_emit_assign_ref_znode(var_ast, &fetch_result);
3414
3.87k
    } else {
3415
3.87k
      zend_emit_assign_znode(var_ast, &fetch_result);
3416
3.87k
    }
3417
6.18k
  }
3418
3419
3.84k
  if (has_elems == 0) {
3420
24
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
3421
24
  }
3422
3423
3.82k
  if (result) {
3424
444
    if ((type == BP_VAR_R || type == BP_VAR_IS) && expr_node->op_type == IS_VAR) {
3425
      /* Deref. */
3426
311
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, expr_node, NULL);
3427
311
    } else {
3428
133
      *result = *expr_node;
3429
133
    }
3430
3.37k
  } else {
3431
3.37k
    zend_do_free(expr_node);
3432
3.37k
  }
3433
3.82k
}
3434
/* }}} */
3435
3436
static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
3437
747k
{
3438
747k
  if (ast->kind == ZEND_AST_CALL || ast->kind == ZEND_AST_PIPE) {
3439
83
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
3440
83
  }
3441
747k
  if (
3442
747k
    ast->kind == ZEND_AST_METHOD_CALL
3443
747k
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
3444
747k
    || ast->kind == ZEND_AST_STATIC_CALL
3445
747k
  ) {
3446
17
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
3447
17
  }
3448
747k
  if (zend_ast_is_short_circuited(ast)) {
3449
26
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use nullsafe operator in write context");
3450
26
  }
3451
747k
  if (is_globals_fetch(ast)) {
3452
36
    zend_error_noreturn(E_COMPILE_ERROR,
3453
36
      "$GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax");
3454
36
  }
3455
747k
}
3456
/* }}} */
3457
3458
/* Detects $a... = $a pattern */
3459
static bool zend_is_assign_to_self(const zend_ast *var_ast, const zend_ast *expr_ast) /* {{{ */
3460
203k
{
3461
203k
  if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
3462
198k
    return false;
3463
198k
  }
3464
3465
11.0k
  while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
3466
6.28k
    var_ast = var_ast->child[0];
3467
6.28k
  }
3468
3469
4.74k
  if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
3470
1.00k
    return false;
3471
1.00k
  }
3472
3473
3.73k
  {
3474
3.73k
    zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
3475
3.73k
    zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
3476
3.73k
    bool result = zend_string_equals(name1, name2);
3477
3.73k
    zend_string_release_ex(name1, 0);
3478
3.73k
    zend_string_release_ex(name2, 0);
3479
3.73k
    return result;
3480
4.74k
  }
3481
4.74k
}
3482
/* }}} */
3483
3484
static void zend_compile_expr_with_potential_assign_to_self(
3485
203k
    znode *expr_node, zend_ast *expr_ast, const zend_ast *var_ast) {
3486
203k
  if (zend_is_assign_to_self(var_ast, expr_ast) && !is_this_fetch(expr_ast)) {
3487
    /* $a[0] = $a should evaluate the right $a first */
3488
705
    znode cv_node;
3489
3490
705
    if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3491
101
      zend_compile_simple_var_no_cv(expr_node, expr_ast, BP_VAR_R, false);
3492
604
    } else {
3493
604
      zend_emit_op_tmp(expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3494
604
    }
3495
202k
  } else {
3496
202k
    zend_compile_expr(expr_node, expr_ast);
3497
202k
  }
3498
203k
}
3499
3500
static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type) /* {{{ */
3501
427k
{
3502
427k
  zend_ast *var_ast = ast->child[0];
3503
427k
  zend_ast *expr_ast = ast->child[1];
3504
3505
427k
  znode var_node, expr_node;
3506
427k
  zend_op *opline;
3507
427k
  uint32_t offset;
3508
427k
  if (is_this_fetch(var_ast)) {
3509
9
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3510
9
  }
3511
3512
427k
  zend_ensure_writable_variable(var_ast);
3513
3514
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3515
427k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3516
427k
  switch (kind) {
3517
394k
    case ZEND_AST_VAR:
3518
394k
      offset = zend_delayed_compile_begin();
3519
394k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, false);
3520
394k
      zend_compile_expr(&expr_node, expr_ast);
3521
394k
      zend_delayed_compile_end(offset);
3522
394k
      CG(zend_lineno) = zend_ast_get_lineno(var_ast);
3523
394k
      zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node);
3524
394k
      return;
3525
2.15k
    case ZEND_AST_STATIC_PROP:
3526
2.15k
      offset = zend_delayed_compile_begin();
3527
2.15k
      zend_delayed_compile_var(result, var_ast, BP_VAR_W, false);
3528
2.15k
      zend_compile_expr(&expr_node, expr_ast);
3529
3530
2.15k
      opline = zend_delayed_compile_end(offset);
3531
2.15k
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
3532
2.15k
      opline->result_type = IS_TMP_VAR;
3533
2.15k
      result->op_type = IS_TMP_VAR;
3534
3535
2.15k
      zend_emit_op_data(&expr_node);
3536
2.15k
      return;
3537
14.1k
    case ZEND_AST_DIM:
3538
14.1k
      offset = zend_delayed_compile_begin();
3539
14.1k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_W, /* by_ref */ false);
3540
14.1k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3541
3542
14.1k
      opline = zend_delayed_compile_end(offset);
3543
14.1k
      opline->opcode = ZEND_ASSIGN_DIM;
3544
14.1k
      opline->result_type = IS_TMP_VAR;
3545
14.1k
      result->op_type = IS_TMP_VAR;
3546
3547
14.1k
      opline = zend_emit_op_data(&expr_node);
3548
14.1k
      return;
3549
12.5k
    case ZEND_AST_PROP:
3550
12.5k
    case ZEND_AST_NULLSAFE_PROP:
3551
12.5k
      offset = zend_delayed_compile_begin();
3552
12.5k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
3553
12.5k
      zend_compile_expr(&expr_node, expr_ast);
3554
3555
12.5k
      opline = zend_delayed_compile_end(offset);
3556
12.5k
      opline->opcode = ZEND_ASSIGN_OBJ;
3557
12.5k
      opline->result_type = IS_TMP_VAR;
3558
12.5k
      result->op_type = IS_TMP_VAR;
3559
3560
12.5k
      zend_emit_op_data(&expr_node);
3561
12.5k
      return;
3562
3.33k
    case ZEND_AST_ARRAY:
3563
3.33k
      if (zend_propagate_list_refs(var_ast)) {
3564
1.61k
        if (!zend_is_variable_or_call(expr_ast)) {
3565
27
          zend_error_noreturn(E_COMPILE_ERROR,
3566
27
            "Cannot assign reference to non referenceable value");
3567
1.59k
        } else {
3568
1.59k
          zend_assert_not_short_circuited(expr_ast);
3569
1.59k
        }
3570
3571
1.59k
        zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
3572
        /* MAKE_REF is usually not necessary for CVs. However, if there are
3573
         * self-assignments, this forces the RHS to evaluate first. */
3574
1.59k
        zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
3575
1.72k
      } else {
3576
1.72k
        if (expr_ast->kind == ZEND_AST_VAR) {
3577
          /* list($a, $b) = $a should evaluate the right $a first */
3578
454
          znode cv_node;
3579
3580
454
          if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3581
159
            zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, false);
3582
295
          } else {
3583
295
            zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3584
295
          }
3585
1.26k
        } else {
3586
1.26k
          zend_compile_expr(&expr_node, expr_ast);
3587
1.26k
        }
3588
1.72k
      }
3589
3590
3.31k
      zend_compile_list_assign(!stmt ? result : NULL, var_ast, &expr_node, var_ast->attr, type);
3591
3.31k
      if (stmt) {
3592
2.43k
        result->op_type = IS_UNUSED;
3593
2.43k
      }
3594
3.31k
      return;
3595
0
    default: ZEND_UNREACHABLE();
3596
427k
  }
3597
427k
}
3598
/* }}} */
3599
3600
static void zend_compile_assign_ref(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3601
11.7k
{
3602
11.7k
  zend_ast *target_ast = ast->child[0];
3603
11.7k
  zend_ast *source_ast = ast->child[1];
3604
3605
11.7k
  znode target_node, source_node;
3606
11.7k
  zend_op *opline;
3607
11.7k
  uint32_t offset, flags;
3608
3609
11.7k
  if (is_this_fetch(target_ast)) {
3610
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3611
5
  }
3612
11.7k
  zend_ensure_writable_variable(target_ast);
3613
11.7k
  zend_assert_not_short_circuited(source_ast);
3614
11.7k
  if (is_globals_fetch(source_ast)) {
3615
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot acquire reference to $GLOBALS");
3616
5
  }
3617
3618
11.7k
  offset = zend_delayed_compile_begin();
3619
11.7k
  zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, true);
3620
11.7k
  zend_compile_var(&source_node, source_ast, BP_VAR_W, true);
3621
3622
11.7k
  if ((target_ast->kind != ZEND_AST_VAR
3623
8.94k
    || target_ast->child[0]->kind != ZEND_AST_ZVAL)
3624
4.51k
   && source_ast->kind != ZEND_AST_ZNODE
3625
3.25k
   && source_node.op_type != IS_CV) {
3626
    /* Both LHS and RHS expressions may modify the same data structure,
3627
     * and the modification during RHS evaluation may dangle the pointer
3628
     * to the result of the LHS evaluation.
3629
     * Use MAKE_REF instruction to replace direct pointer with REFERENCE.
3630
     * See: Bug #71539
3631
     */
3632
1.39k
    zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL);
3633
1.39k
  }
3634
3635
11.7k
  opline = zend_delayed_compile_end(offset);
3636
3637
11.7k
  if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
3638
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3639
5
  }
3640
3641
11.7k
  flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0;
3642
3643
11.7k
  if (opline && opline->opcode == ZEND_FETCH_OBJ_W) {
3644
1.03k
    opline->opcode = ZEND_ASSIGN_OBJ_REF;
3645
1.03k
    opline->extended_value &= ~ZEND_FETCH_REF;
3646
1.03k
    opline->extended_value |= flags;
3647
1.03k
    if (result) {
3648
161
      *result = target_node;
3649
870
    } else {
3650
870
      SET_UNUSED(opline->result);
3651
870
    }
3652
1.03k
    zend_emit_op_data(&source_node);
3653
10.7k
  } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) {
3654
586
    opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF;
3655
586
    opline->extended_value &= ~ZEND_FETCH_REF;
3656
586
    opline->extended_value |= flags;
3657
586
    if (result) {
3658
126
      *result = target_node;
3659
460
    } else {
3660
460
      SET_UNUSED(opline->result);
3661
460
    }
3662
586
    zend_emit_op_data(&source_node);
3663
10.1k
  } else {
3664
10.1k
    opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
3665
10.1k
    opline->extended_value = flags;
3666
10.1k
  }
3667
3668
11.7k
  if (result && (type == BP_VAR_R || type == BP_VAR_IS)) {
3669
    /* Deref. */
3670
3.82k
    znode tmp_result = *result;
3671
3.82k
    zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &tmp_result, NULL);
3672
3.82k
  }
3673
11.7k
}
3674
/* }}} */
3675
3676
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3677
2.68k
{
3678
2.68k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
3679
2.68k
    zend_ast_create_znode(value_node));
3680
2.68k
  zend_compile_stmt(assign_ast);
3681
2.68k
}
3682
/* }}} */
3683
3684
static void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
3685
95.7k
{
3686
95.7k
  zend_ast *var_ast = ast->child[0];
3687
95.7k
  zend_ast *expr_ast = ast->child[1];
3688
95.7k
  uint32_t opcode = ast->attr;
3689
3690
95.7k
  znode var_node, expr_node;
3691
95.7k
  zend_op *opline;
3692
95.7k
  uint32_t offset, cache_slot;
3693
3694
95.7k
  zend_ensure_writable_variable(var_ast);
3695
3696
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3697
95.7k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3698
95.7k
  switch (kind) {
3699
90.1k
    case ZEND_AST_VAR:
3700
90.1k
      offset = zend_delayed_compile_begin();
3701
90.1k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, false);
3702
90.1k
      zend_compile_expr(&expr_node, expr_ast);
3703
90.1k
      zend_delayed_compile_end(offset);
3704
90.1k
      opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node);
3705
90.1k
      opline->extended_value = opcode;
3706
90.1k
      return;
3707
498
    case ZEND_AST_STATIC_PROP:
3708
498
      offset = zend_delayed_compile_begin();
3709
498
      zend_delayed_compile_var(result, var_ast, BP_VAR_RW, false);
3710
498
      zend_compile_expr(&expr_node, expr_ast);
3711
3712
498
      opline = zend_delayed_compile_end(offset);
3713
498
      cache_slot = opline->extended_value;
3714
498
      opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP;
3715
498
      opline->extended_value = opcode;
3716
498
      opline->result_type = IS_TMP_VAR;
3717
498
      result->op_type = IS_TMP_VAR;
3718
3719
498
      opline = zend_emit_op_data(&expr_node);
3720
498
      opline->extended_value = cache_slot;
3721
498
      return;
3722
3.89k
    case ZEND_AST_DIM:
3723
3.89k
      offset = zend_delayed_compile_begin();
3724
3.89k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_RW, /* by_ref */ false);
3725
3.89k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3726
3727
3.89k
      opline = zend_delayed_compile_end(offset);
3728
3.89k
      opline->opcode = ZEND_ASSIGN_DIM_OP;
3729
3.89k
      opline->extended_value = opcode;
3730
3.89k
      opline->result_type = IS_TMP_VAR;
3731
3.89k
      result->op_type = IS_TMP_VAR;
3732
3733
3.89k
      zend_emit_op_data(&expr_node);
3734
3.89k
      return;
3735
1.13k
    case ZEND_AST_PROP:
3736
1.13k
    case ZEND_AST_NULLSAFE_PROP:
3737
1.13k
      offset = zend_delayed_compile_begin();
3738
1.13k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
3739
1.13k
      zend_compile_expr(&expr_node, expr_ast);
3740
3741
1.13k
      opline = zend_delayed_compile_end(offset);
3742
1.13k
      cache_slot = opline->extended_value;
3743
1.13k
      opline->opcode = ZEND_ASSIGN_OBJ_OP;
3744
1.13k
      opline->extended_value = opcode;
3745
1.13k
      opline->result_type = IS_TMP_VAR;
3746
1.13k
      result->op_type = IS_TMP_VAR;
3747
3748
1.13k
      opline = zend_emit_op_data(&expr_node);
3749
1.13k
      opline->extended_value = cache_slot;
3750
1.13k
      return;
3751
0
    default: ZEND_UNREACHABLE();
3752
95.7k
  }
3753
95.7k
}
3754
/* }}} */
3755
3756
5.12k
static uint32_t zend_get_arg_num(const zend_function *fn, const zend_string *arg_name) {
3757
  // TODO: Caching?
3758
18.7k
  for (uint32_t i = 0; i < fn->common.num_args; i++) {
3759
16.4k
    zend_arg_info *arg_info = &fn->op_array.arg_info[i];
3760
16.4k
    if (zend_string_equals(arg_info->name, arg_name)) {
3761
2.75k
      return i + 1;
3762
2.75k
    }
3763
16.4k
  }
3764
3765
  /* Either an invalid argument name, or collected into a variadic argument. */
3766
2.36k
  return (uint32_t) -1;
3767
5.12k
}
3768
3769
static uint32_t zend_compile_args(
3770
    zend_ast *ast, const zend_function *fbc, bool *may_have_extra_named_args) /* {{{ */
3771
4.36M
{
3772
4.36M
  const zend_ast_list *args = zend_ast_get_list(ast);
3773
4.36M
  uint32_t i;
3774
4.36M
  bool uses_arg_unpack = false;
3775
4.36M
  uint32_t arg_count = 0; /* number of arguments not including unpacks */
3776
3777
  /* Whether named arguments are used syntactically, to enforce language level limitations.
3778
   * May not actually use named argument passing. */
3779
4.36M
  bool uses_named_args = false;
3780
  /* Whether there may be any undef arguments due to the use of named arguments. */
3781
4.36M
  bool may_have_undef = false;
3782
  /* Whether there may be any extra named arguments collected into a variadic. */
3783
4.36M
  *may_have_extra_named_args = false;
3784
3785
9.79M
  for (i = 0; i < args->children; ++i) {
3786
5.42M
    zend_ast *arg = args->child[i];
3787
5.42M
    zend_string *arg_name = NULL;
3788
5.42M
    uint32_t arg_num = i + 1;
3789
3790
5.42M
    znode arg_node;
3791
5.42M
    zend_op *opline;
3792
5.42M
    uint8_t opcode;
3793
3794
5.42M
    if (arg->kind == ZEND_AST_UNPACK) {
3795
2.34k
      if (uses_named_args) {
3796
5
        zend_error_noreturn(E_COMPILE_ERROR,
3797
5
          "Cannot use argument unpacking after named arguments");
3798
5
      }
3799
3800
      /* Unpack may contain named arguments. */
3801
2.34k
      may_have_undef = true;
3802
2.34k
      if (!fbc || (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3803
1.82k
        *may_have_extra_named_args = true;
3804
1.82k
      }
3805
3806
2.34k
      uses_arg_unpack = true;
3807
2.34k
      fbc = NULL;
3808
3809
2.34k
      zend_compile_expr(&arg_node, arg->child[0]);
3810
2.34k
      opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
3811
2.34k
      opline->op2.num = arg_count;
3812
2.34k
      opline->result.var = EX_NUM_TO_VAR(arg_count - 1);
3813
3814
2.34k
      continue;
3815
2.34k
    }
3816
3817
5.42M
    if (arg->kind == ZEND_AST_NAMED_ARG) {
3818
74.3k
      uses_named_args = true;
3819
74.3k
      arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0]));
3820
74.3k
      arg = arg->child[1];
3821
3822
74.3k
      if (fbc && !uses_arg_unpack) {
3823
5.12k
        arg_num = zend_get_arg_num(fbc, arg_name);
3824
5.12k
        if (arg_num == arg_count + 1 && !may_have_undef) {
3825
          /* Using named arguments, but passing in order. */
3826
624
          arg_name = NULL;
3827
624
          arg_count++;
3828
4.50k
        } else {
3829
          // TODO: We could track which arguments were passed, even if out of order.
3830
4.50k
          may_have_undef = true;
3831
4.50k
          if (arg_num == (uint32_t) -1 && (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3832
356
            *may_have_extra_named_args = true;
3833
356
          }
3834
4.50k
        }
3835
69.2k
      } else {
3836
69.2k
        arg_num = (uint32_t) -1;
3837
69.2k
        may_have_undef = true;
3838
69.2k
        *may_have_extra_named_args = true;
3839
69.2k
      }
3840
5.35M
    } else {
3841
5.35M
      if (uses_arg_unpack) {
3842
11
        zend_error_noreturn(E_COMPILE_ERROR,
3843
11
          "Cannot use positional argument after argument unpacking");
3844
11
      }
3845
3846
5.35M
      if (uses_named_args) {
3847
38
        zend_error_noreturn(E_COMPILE_ERROR,
3848
38
          "Cannot use positional argument after named argument");
3849
38
      }
3850
3851
5.35M
      arg_count++;
3852
5.35M
    }
3853
3854
    /* Treat passing of $GLOBALS the same as passing a call.
3855
     * This will error at runtime if the argument is by-ref. */
3856
5.42M
    if (zend_is_call(arg) || is_globals_fetch(arg)) {
3857
250k
      uint32_t type = is_globals_fetch(arg) || (fbc && !ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num))
3858
250k
        ? BP_VAR_R : BP_VAR_FUNC_ARG;
3859
250k
      zend_compile_var(&arg_node, arg, type, /* by_ref */ false);
3860
250k
      if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
3861
        /* Function call was converted into builtin instruction */
3862
46.0k
        if (!fbc || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3863
5.83k
          opcode = ZEND_SEND_VAL_EX;
3864
40.2k
        } else {
3865
40.2k
          opcode = ZEND_SEND_VAL;
3866
40.2k
        }
3867
203k
      } else {
3868
203k
        if (fbc && arg_num != (uint32_t) -1) {
3869
542
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3870
447
            opcode = ZEND_SEND_VAR_NO_REF;
3871
447
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3872
            /* For IS_VAR operands, SEND_VAL will pass through the operand without
3873
             * dereferencing, so it will use a by-ref pass if the call returned by-ref
3874
             * and a by-value pass if it returned by-value. */
3875
95
            opcode = ZEND_SEND_VAL;
3876
95
          } else {
3877
0
            opcode = ZEND_SEND_VAR;
3878
0
          }
3879
203k
        } else {
3880
203k
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3881
203k
        }
3882
203k
      }
3883
5.17M
    } else if (zend_is_passable_by_ref(arg) && !zend_ast_is_short_circuited(arg)) {
3884
154k
      if (fbc && arg_num != (uint32_t) -1) {
3885
75.9k
        if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3886
2.97k
          zend_compile_var(&arg_node, arg, BP_VAR_W, true);
3887
2.97k
          opcode = ZEND_SEND_REF;
3888
72.9k
        } else {
3889
72.9k
          zend_compile_var(&arg_node, arg, BP_VAR_R, false);
3890
72.9k
          opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR;
3891
72.9k
        }
3892
78.4k
      } else {
3893
78.4k
        do {
3894
78.4k
          if (arg->kind == ZEND_AST_VAR) {
3895
15.5k
            CG(zend_lineno) = zend_ast_get_lineno(ast);
3896
15.5k
            if (is_this_fetch(arg)) {
3897
396
              zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL);
3898
396
              opcode = ZEND_SEND_VAR_EX;
3899
396
              CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3900
396
              break;
3901
15.1k
            } else if (zend_try_compile_cv(&arg_node, arg, BP_VAR_R) == SUCCESS) {
3902
15.0k
              opcode = ZEND_SEND_VAR_EX;
3903
15.0k
              break;
3904
15.0k
            }
3905
15.5k
          }
3906
63.0k
          opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL);
3907
63.0k
          if (arg_name) {
3908
1.69k
            opline->op2_type = IS_CONST;
3909
1.69k
            zend_string_addref(arg_name);
3910
1.69k
            opline->op2.constant = zend_add_literal_string(&arg_name);
3911
1.69k
            opline->result.num = zend_alloc_cache_slots(2);
3912
61.3k
          } else {
3913
61.3k
            opline->op2.num = arg_num;
3914
61.3k
          }
3915
63.0k
          zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, true);
3916
63.0k
          opcode = ZEND_SEND_FUNC_ARG;
3917
63.0k
        } while (0);
3918
78.4k
      }
3919
5.02M
    } else {
3920
5.02M
      zend_compile_expr(&arg_node, arg);
3921
5.02M
      if (arg_node.op_type == IS_VAR) {
3922
        /* pass ++$a or something similar */
3923
0
        if (fbc && arg_num != (uint32_t) -1) {
3924
0
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3925
0
            opcode = ZEND_SEND_VAR_NO_REF;
3926
0
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3927
0
            opcode = ZEND_SEND_VAL;
3928
0
          } else {
3929
0
            opcode = ZEND_SEND_VAR;
3930
0
          }
3931
0
        } else {
3932
0
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3933
0
        }
3934
5.02M
      } else if (arg_node.op_type == IS_CV) {
3935
0
        if (fbc && arg_num != (uint32_t) -1) {
3936
0
          if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3937
0
            opcode = ZEND_SEND_REF;
3938
0
          } else {
3939
0
            opcode = ZEND_SEND_VAR;
3940
0
          }
3941
0
        } else {
3942
0
          opcode = ZEND_SEND_VAR_EX;
3943
0
        }
3944
5.02M
      } else {
3945
        /* Delay "Only variables can be passed by reference" error to execution */
3946
5.02M
        if (fbc && arg_num != (uint32_t) -1 && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3947
121k
          opcode = ZEND_SEND_VAL;
3948
4.90M
        } else {
3949
4.90M
          opcode = ZEND_SEND_VAL_EX;
3950
4.90M
        }
3951
5.02M
      }
3952
5.02M
    }
3953
3954
5.42M
    opline = zend_emit_op(NULL, opcode, &arg_node, NULL);
3955
5.42M
    if (arg_name) {
3956
73.7k
      opline->op2_type = IS_CONST;
3957
73.7k
      zend_string_addref(arg_name);
3958
73.7k
      opline->op2.constant = zend_add_literal_string(&arg_name);
3959
73.7k
      opline->result.num = zend_alloc_cache_slots(2);
3960
5.35M
    } else {
3961
5.35M
      opline->op2.opline_num = arg_num;
3962
5.35M
      opline->result.var = EX_NUM_TO_VAR(arg_num - 1);
3963
5.35M
    }
3964
5.42M
  }
3965
3966
4.36M
  if (may_have_undef) {
3967
70.6k
    zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
3968
70.6k
  }
3969
3970
4.36M
  return arg_count;
3971
4.36M
}
3972
/* }}} */
3973
3974
ZEND_API uint8_t zend_get_call_op(const zend_op *init_op, const zend_function *fbc, bool result_used) /* {{{ */
3975
4.47M
{
3976
4.47M
  uint32_t no_discard = result_used ? 0 : ZEND_ACC_NODISCARD;
3977
3978
4.47M
  if (fbc && init_op->opcode != ZEND_NEW) {
3979
259k
    ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE));
3980
259k
    if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) {
3981
208k
      if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) {
3982
22.9k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3983
22.8k
          return ZEND_DO_ICALL;
3984
22.8k
        } else {
3985
63
          return ZEND_DO_FCALL_BY_NAME;
3986
63
        }
3987
22.9k
      }
3988
208k
    } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){
3989
50.8k
      if (zend_execute_ex == execute_ex) {
3990
23.9k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3991
23.7k
          return ZEND_DO_UCALL;
3992
23.7k
        } else {
3993
240
          return ZEND_DO_FCALL_BY_NAME;
3994
240
        }
3995
23.9k
      }
3996
50.8k
    }
3997
4.21M
  } else if (zend_execute_ex == execute_ex &&
3998
4.14M
             !zend_execute_internal &&
3999
4.09M
             (init_op->opcode == ZEND_INIT_FCALL_BY_NAME ||
4000
4.01M
              init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) {
4001
3.79M
    return ZEND_DO_FCALL_BY_NAME;
4002
3.79M
  }
4003
636k
  return ZEND_DO_FCALL;
4004
4.47M
}
4005
/* }}} */
4006
4007
static bool zend_compile_call_common(znode *result, zend_ast *args_ast, const zend_function *fbc, uint32_t lineno, uint32_t type) /* {{{ */
4008
4.37M
{
4009
4.37M
  zend_op *opline;
4010
4.37M
  uint32_t opnum_init = get_next_op_number() - 1;
4011
4012
4.37M
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
4013
7.45k
    opline = &CG(active_op_array)->opcodes[opnum_init];
4014
7.45k
    opline->extended_value = 0;
4015
    /* opcode array may be reallocated, so don't access opcode field after zend_emit_op_tmp(). */
4016
7.45k
    uint8_t opcode = opline->opcode;
4017
4018
7.45k
    if (opcode == ZEND_NEW) {
4019
18
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
4020
18
    }
4021
4022
7.43k
    zend_ast_list *args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args);
4023
7.43k
    if (args->children != 1 || args->child[0]->attr != ZEND_PLACEHOLDER_VARIADIC) {
4024
28
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot create a Closure for call expression with more than one argument, or non-variadic placeholders");
4025
28
    }
4026
4027
7.40k
    if (opcode == ZEND_INIT_FCALL) {
4028
521
      opline->op1.num = zend_vm_calc_used_stack(0, fbc);
4029
521
    }
4030
4031
7.40k
    zend_op *callable_convert_op = zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT, NULL, NULL);
4032
7.40k
    if (opcode == ZEND_INIT_FCALL
4033
6.88k
     || opcode == ZEND_INIT_FCALL_BY_NAME
4034
6.40k
     || opcode == ZEND_INIT_NS_FCALL_BY_NAME) {
4035
6.40k
      callable_convert_op->extended_value = zend_alloc_cache_slot();
4036
6.40k
    } else {
4037
1.00k
      callable_convert_op->extended_value = (uint32_t)-1;
4038
1.00k
    }
4039
7.40k
    return true;
4040
7.43k
  }
4041
4042
4.37M
  bool may_have_extra_named_args;
4043
4.36M
  uint32_t arg_count = zend_compile_args(args_ast, fbc, &may_have_extra_named_args);
4044
4045
4.36M
  zend_do_extended_fcall_begin();
4046
4047
4.36M
  opline = &CG(active_op_array)->opcodes[opnum_init];
4048
4.36M
  opline->extended_value = arg_count;
4049
4.36M
  uint8_t init_opcode = opline->opcode;
4050
4051
4.36M
  if (init_opcode == ZEND_INIT_FCALL) {
4052
141k
    opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
4053
141k
  }
4054
4055
4.36M
  uint8_t call_op = zend_get_call_op(
4056
4.36M
    opline,
4057
4.36M
    fbc,
4058
    /* result_used: At this point we do not yet reliably
4059
     * know if the result is used. Deoptimize #[\NoDiscard]
4060
     * calls to be sure. The optimizer will fix this up.
4061
     */
4062
4.36M
    false
4063
4.36M
  );
4064
4.36M
  opline = zend_emit_op(result, call_op, NULL, NULL);
4065
4.36M
  if (type == BP_VAR_R || type == BP_VAR_IS) {
4066
3.79M
    if (init_opcode != ZEND_NEW && opline->result_type == IS_VAR) {
4067
3.71M
      opline->result_type = IS_TMP_VAR;
4068
3.71M
      result->op_type = IS_TMP_VAR;
4069
3.71M
    }
4070
3.79M
  }
4071
4.36M
  if (may_have_extra_named_args) {
4072
68.6k
    opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4073
68.6k
  }
4074
4.36M
  opline->lineno = lineno;
4075
4.36M
  zend_do_extended_fcall_end();
4076
4.36M
  return false;
4077
4.37M
}
4078
/* }}} */
4079
4080
static bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
4081
3.99M
{
4082
3.99M
  zend_string *orig_name = zend_ast_get_str(name_ast);
4083
3.99M
  bool is_fully_qualified;
4084
4085
3.99M
  name_node->op_type = IS_CONST;
4086
3.99M
  ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
4087
3.99M
    orig_name, name_ast->attr, &is_fully_qualified));
4088
4089
3.99M
  return !is_fully_qualified && FC(current_namespace);
4090
3.99M
}
4091
/* }}} */
4092
4093
static void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */
4094
142k
{
4095
142k
  if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
4096
98.9k
    const char *colon;
4097
98.9k
    zend_string *str = Z_STR(name_node->u.constant);
4098
98.9k
    if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
4099
499
      zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
4100
499
      zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
4101
499
      zend_op *opline = get_next_op();
4102
4103
499
      opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
4104
499
      opline->op1_type = IS_CONST;
4105
499
      opline->op1.constant = zend_add_class_name_literal(class);
4106
499
      opline->op2_type = IS_CONST;
4107
499
      opline->op2.constant = zend_add_func_name_literal(method);
4108
      /* 2 slots, for class and method */
4109
499
      opline->result.num = zend_alloc_cache_slots(2);
4110
499
      zval_ptr_dtor(&name_node->u.constant);
4111
98.4k
    } else {
4112
98.4k
      zend_op *opline = get_next_op();
4113
4114
98.4k
      opline->opcode = ZEND_INIT_FCALL_BY_NAME;
4115
98.4k
      opline->op2_type = IS_CONST;
4116
98.4k
      opline->op2.constant = zend_add_func_name_literal(str);
4117
98.4k
      opline->result.num = zend_alloc_cache_slot();
4118
98.4k
    }
4119
98.9k
  } else {
4120
43.5k
    zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node);
4121
43.5k
  }
4122
4123
142k
  zend_compile_call_common(result, args_ast, NULL, lineno, type);
4124
142k
}
4125
/* }}} */
4126
4127
static inline bool zend_args_contain_unpack_or_named(const zend_ast_list *args) /* {{{ */
4128
3.79M
{
4129
3.79M
  uint32_t i;
4130
8.81M
  for (i = 0; i < args->children; ++i) {
4131
5.04M
    const zend_ast *arg = args->child[i];
4132
5.04M
    if (arg->kind == ZEND_AST_UNPACK || arg->kind == ZEND_AST_NAMED_ARG) {
4133
20.3k
      return true;
4134
20.3k
    }
4135
5.04M
  }
4136
3.77M
  return false;
4137
3.79M
}
4138
/* }}} */
4139
4140
static zend_result zend_compile_func_strlen(znode *result, const zend_ast_list *args) /* {{{ */
4141
1.06k
{
4142
1.06k
  znode arg_node;
4143
4144
1.06k
  if (args->children != 1) {
4145
37
    return FAILURE;
4146
37
  }
4147
4148
1.02k
  zend_compile_expr(&arg_node, args->child[0]);
4149
1.02k
  if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
4150
285
    result->op_type = IS_CONST;
4151
285
    ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
4152
285
    zval_ptr_dtor_str(&arg_node.u.constant);
4153
741
  } else {
4154
741
    zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
4155
741
  }
4156
1.02k
  return SUCCESS;
4157
1.06k
}
4158
/* }}} */
4159
4160
static zend_result zend_compile_func_typecheck(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4161
2.48k
{
4162
2.48k
  znode arg_node;
4163
2.48k
  zend_op *opline;
4164
4165
2.48k
  if (args->children != 1) {
4166
691
    return FAILURE;
4167
691
  }
4168
4169
1.79k
  zend_compile_expr(&arg_node, args->child[0]);
4170
1.79k
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4171
1.79k
  if (type != _IS_BOOL) {
4172
1.43k
    opline->extended_value = (1 << type);
4173
1.43k
  } else {
4174
360
    opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE);
4175
360
  }
4176
1.79k
  return SUCCESS;
4177
2.48k
}
4178
/* }}} */
4179
4180
static zend_result zend_compile_func_is_scalar(znode *result, const zend_ast_list *args) /* {{{ */
4181
308
{
4182
308
  znode arg_node;
4183
308
  zend_op *opline;
4184
4185
308
  if (args->children != 1) {
4186
15
    return FAILURE;
4187
15
  }
4188
4189
293
  zend_compile_expr(&arg_node, args->child[0]);
4190
293
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4191
293
  opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING);
4192
293
  return SUCCESS;
4193
308
}
4194
4195
static zend_result zend_compile_func_cast(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4196
932
{
4197
932
  znode arg_node;
4198
932
  zend_op *opline;
4199
4200
932
  if (args->children != 1) {
4201
131
    return FAILURE;
4202
131
  }
4203
4204
801
  zend_compile_expr(&arg_node, args->child[0]);
4205
801
  if (type == _IS_BOOL) {
4206
205
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL);
4207
596
  } else {
4208
596
    opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
4209
596
    opline->extended_value = type;
4210
596
  }
4211
801
  return SUCCESS;
4212
932
}
4213
/* }}} */
4214
4215
static zend_result zend_compile_func_defined(znode *result, const zend_ast_list *args) /* {{{ */
4216
1.99k
{
4217
1.99k
  zend_string *name;
4218
1.99k
  zend_op *opline;
4219
4220
1.99k
  if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
4221
175
    return FAILURE;
4222
175
  }
4223
4224
1.81k
  name = zval_get_string(zend_ast_get_zval(args->child[0]));
4225
1.81k
  if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
4226
519
    zend_string_release_ex(name, 0);
4227
519
    return FAILURE;
4228
519
  }
4229
4230
1.29k
  if (zend_try_ct_eval_const(&result->u.constant, name, false)) {
4231
89
    zend_string_release_ex(name, 0);
4232
89
    zval_ptr_dtor(&result->u.constant);
4233
89
    ZVAL_TRUE(&result->u.constant);
4234
89
    result->op_type = IS_CONST;
4235
89
    return SUCCESS;
4236
89
  }
4237
4238
1.21k
  opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
4239
1.21k
  opline->op1_type = IS_CONST;
4240
1.21k
  LITERAL_STR(opline->op1, name);
4241
1.21k
  opline->extended_value = zend_alloc_cache_slot();
4242
4243
1.21k
  return SUCCESS;
4244
1.29k
}
4245
/* }}} */
4246
4247
static zend_result zend_compile_func_chr(znode *result, const zend_ast_list *args) /* {{{ */
4248
1.04k
{
4249
1.04k
  zval *zint;
4250
1.04k
  if (
4251
1.04k
    args->children == 1
4252
730
    && args->child[0]->kind == ZEND_AST_ZVAL
4253
511
    && (zint = zend_ast_get_zval(args->child[0]))
4254
511
    && Z_TYPE_P(zint) == IS_LONG
4255
453
    && Z_LVAL_P(zint) >= 0
4256
453
    && Z_LVAL_P(zint) <= 255
4257
1.04k
  ) {
4258
322
    result->op_type = IS_CONST;
4259
322
    ZVAL_CHAR(&result->u.constant, Z_LVAL_P(zint));
4260
322
    return SUCCESS;
4261
724
  } else {
4262
724
    return FAILURE;
4263
724
  }
4264
1.04k
}
4265
/* }}} */
4266
4267
static zend_result zend_compile_func_ord(znode *result, const zend_ast_list *args) /* {{{ */
4268
594
{
4269
594
  zval *str;
4270
594
  if (
4271
594
    args->children == 1
4272
523
    && args->child[0]->kind == ZEND_AST_ZVAL
4273
375
    && (str = zend_ast_get_zval(args->child[0]))
4274
375
    && Z_TYPE_P(str) == IS_STRING
4275
137
    && Z_STRLEN_P(str) == 1
4276
594
  ) {
4277
63
    result->op_type = IS_CONST;
4278
63
    ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(str)[0]);
4279
63
    return SUCCESS;
4280
531
  } else {
4281
531
    return FAILURE;
4282
531
  }
4283
594
}
4284
/* }}} */
4285
4286
/* We can only calculate the stack size for functions that have been fully compiled, otherwise
4287
 * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for
4288
 * directly or indirectly recursive function calls. */
4289
181k
static bool fbc_is_finalized(const zend_function *fbc) {
4290
181k
  return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO);
4291
181k
}
4292
4293
static bool zend_compile_ignore_class(const zend_class_entry *ce, const zend_string *filename)
4294
61.1k
{
4295
61.1k
  if (ce->type == ZEND_INTERNAL_CLASS) {
4296
39.2k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES;
4297
39.2k
  } else {
4298
21.8k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4299
16.2k
      && ce->info.user.filename != filename;
4300
21.8k
  }
4301
61.1k
}
4302
4303
static bool zend_compile_ignore_function(const zend_function *fbc, const zend_string *filename)
4304
167k
{
4305
167k
  if (fbc->type == ZEND_INTERNAL_FUNCTION) {
4306
143k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS;
4307
143k
  } else {
4308
23.9k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)
4309
23.9k
      || ((CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4310
21.4k
        && fbc->op_array.filename != filename);
4311
23.9k
  }
4312
167k
}
4313
4314
static zend_result zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
4315
6.82k
{
4316
6.82k
  zend_string *name, *lcname;
4317
6.82k
  zend_function *fbc;
4318
6.82k
  zend_op *opline;
4319
4320
6.82k
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
4321
2.36k
    return FAILURE;
4322
2.36k
  }
4323
4324
4.45k
  name = zend_ast_get_str(name_ast);
4325
4.45k
  lcname = zend_string_tolower(name);
4326
4327
4.45k
  fbc = zend_hash_find_ptr(CG(function_table), lcname);
4328
4.45k
  if (!fbc
4329
909
   || !fbc_is_finalized(fbc)
4330
3.54k
   || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
4331
3.54k
    zend_string_release_ex(lcname, 0);
4332
3.54k
    return FAILURE;
4333
3.54k
  }
4334
4335
909
  opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
4336
909
  opline->extended_value = num_args;
4337
909
  opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
4338
909
  opline->op2_type = IS_CONST;
4339
909
  LITERAL_STR(opline->op2, lcname);
4340
909
  opline->result.num = zend_alloc_cache_slot();
4341
4342
909
  return SUCCESS;
4343
4.45k
}
4344
/* }}} */
4345
4346
static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
4347
6.82k
{
4348
6.82k
  zend_op *opline;
4349
6.82k
  znode name_node;
4350
4351
6.82k
  if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
4352
909
    return;
4353
909
  }
4354
4355
5.91k
  zend_compile_expr(&name_node, name_ast);
4356
4357
5.91k
  opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
4358
5.91k
  opline->op1_type = IS_CONST;
4359
5.91k
  LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
4360
5.91k
  opline->extended_value = num_args;
4361
5.91k
}
4362
/* }}} */
4363
4364
/* cufa = call_user_func_array */
4365
static zend_result zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t type) /* {{{ */
4366
2.33k
{
4367
2.33k
  znode arg_node;
4368
2.33k
  zend_op *opline;
4369
4370
2.33k
  if (args->children != 2) {
4371
100
    return FAILURE;
4372
100
  }
4373
4374
2.23k
  zend_compile_init_user_func(args->child[0], 0, lcname);
4375
2.23k
  if (args->child[1]->kind == ZEND_AST_CALL
4376
1.57k
   && args->child[1]->child[0]->kind == ZEND_AST_ZVAL
4377
1.51k
   && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING
4378
1.45k
   && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) {
4379
1.44k
    zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]);
4380
1.44k
    zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]);
4381
1.44k
    bool is_fully_qualified;
4382
1.44k
    zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified);
4383
4384
1.44k
    if (zend_string_equals_literal_ci(name, "array_slice")
4385
1.05k
       && !zend_args_contain_unpack_or_named(list)
4386
847
     && list->children == 3
4387
561
     && list->child[1]->kind == ZEND_AST_ZVAL) {
4388
454
      zval *zv = zend_ast_get_zval(list->child[1]);
4389
4390
454
      if (Z_TYPE_P(zv) == IS_LONG
4391
431
       && Z_LVAL_P(zv) >= 0
4392
431
       && Z_LVAL_P(zv) <= 0x7fffffff) {
4393
222
        zend_op *opline;
4394
222
        znode len_node;
4395
4396
222
        zend_compile_expr(&arg_node, list->child[0]);
4397
222
        zend_compile_expr(&len_node, list->child[2]);
4398
222
        opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node);
4399
222
        opline->extended_value = Z_LVAL_P(zv);
4400
222
        opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4401
222
        if (type == BP_VAR_R || type == BP_VAR_IS) {
4402
222
          opline->result_type = IS_TMP_VAR;
4403
222
          result->op_type = IS_TMP_VAR;
4404
222
        }
4405
222
        zend_string_release_ex(name, 0);
4406
222
        return SUCCESS;
4407
222
      }
4408
454
    }
4409
1.21k
    zend_string_release_ex(name, 0);
4410
1.21k
  }
4411
2.01k
  zend_compile_expr(&arg_node, args->child[1]);
4412
2.01k
  zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
4413
2.01k
  zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
4414
2.01k
  opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4415
2.01k
  if (type == BP_VAR_R || type == BP_VAR_IS) {
4416
1.84k
    opline->result_type = IS_TMP_VAR;
4417
1.84k
    result->op_type = IS_TMP_VAR;
4418
1.84k
  }
4419
2.01k
  opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4420
4421
2.01k
  return SUCCESS;
4422
2.23k
}
4423
/* }}} */
4424
4425
/* cuf = call_user_func */
4426
static zend_result zend_compile_func_cuf(znode *result, const zend_ast_list *args, zend_string *lcname, uint32_t type) /* {{{ */
4427
4.77k
{
4428
4.77k
  uint32_t i;
4429
4430
4.77k
  if (args->children < 1) {
4431
191
    return FAILURE;
4432
191
  }
4433
4434
4.58k
  zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
4435
8.72k
  for (i = 1; i < args->children; ++i) {
4436
4.13k
    zend_ast *arg_ast = args->child[i];
4437
4.13k
    znode arg_node;
4438
4.13k
    zend_op *opline;
4439
4440
4.13k
    zend_compile_expr(&arg_node, arg_ast);
4441
4442
4.13k
    opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
4443
4.13k
    opline->op2.num = i;
4444
4.13k
    opline->result.var = EX_NUM_TO_VAR(i - 1);
4445
4.13k
  }
4446
4.58k
  zend_op *opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4447
4.58k
  if (type == BP_VAR_R || type == BP_VAR_IS) {
4448
913
    opline->result_type = IS_TMP_VAR;
4449
913
    result->op_type = IS_TMP_VAR;
4450
913
  }
4451
4452
4.58k
  return SUCCESS;
4453
4.77k
}
4454
/* }}} */
4455
4456
static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, const zend_function *fbc, uint32_t lineno, uint32_t type) /* {{{ */
4457
73.4k
{
4458
73.4k
  if (EG(assertions) >= 0) {
4459
73.4k
    znode name_node;
4460
73.4k
    zend_op *opline;
4461
73.4k
    uint32_t check_op_number = get_next_op_number();
4462
4463
73.4k
    zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
4464
4465
73.4k
    if (fbc && fbc_is_finalized(fbc)) {
4466
13.8k
      name_node.op_type = IS_CONST;
4467
13.8k
      ZVAL_STR_COPY(&name_node.u.constant, name);
4468
4469
13.8k
      opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
4470
59.6k
    } else {
4471
59.6k
      opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
4472
59.6k
      opline->op2_type = IS_CONST;
4473
59.6k
      opline->op2.constant = zend_add_ns_func_name_literal(name);
4474
59.6k
    }
4475
73.4k
    opline->result.num = zend_alloc_cache_slot();
4476
4477
73.4k
    if (args->children == 1) {
4478
      /* add "assert(condition) as assertion message */
4479
14.3k
      zend_ast *arg = zend_ast_create_zval_from_str(
4480
14.3k
        zend_ast_export("assert(", args->child[0], ")"));
4481
14.3k
      if (args->child[0]->kind == ZEND_AST_NAMED_ARG) {
4482
        /* If the original argument was named, add the new argument as named as well,
4483
         * as mixing named and positional is not allowed. */
4484
1.13k
        zend_ast *name = zend_ast_create_zval_from_str(
4485
1.13k
          ZSTR_INIT_LITERAL("description", 0));
4486
1.13k
        arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg);
4487
1.13k
      }
4488
14.3k
      args = (zend_ast_list *)zend_ast_list_add((zend_ast *) args, arg);
4489
14.3k
    }
4490
4491
73.4k
    zend_compile_call_common(result, (zend_ast*)args, fbc, lineno, type);
4492
4493
73.4k
    opline = &CG(active_op_array)->opcodes[check_op_number];
4494
73.4k
    opline->op2.opline_num = get_next_op_number();
4495
73.4k
    SET_NODE(opline->result, result);
4496
73.4k
  } else {
4497
0
    if (!fbc) {
4498
0
      zend_string_release_ex(name, 0);
4499
0
    }
4500
0
    result->op_type = IS_CONST;
4501
0
    ZVAL_TRUE(&result->u.constant);
4502
0
  }
4503
73.4k
}
4504
/* }}} */
4505
4506
static zend_result zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */
4507
11.8k
{
4508
11.8k
  bool strict = false;
4509
11.8k
  znode array, needly;
4510
11.8k
  zend_op *opline;
4511
4512
11.8k
  if (args->children == 3) {
4513
10.0k
    if (args->child[2]->kind == ZEND_AST_ZVAL) {
4514
874
      strict = zend_is_true(zend_ast_get_zval(args->child[2]));
4515
9.20k
    } else if (args->child[2]->kind == ZEND_AST_CONST) {
4516
1.40k
      zval value;
4517
1.40k
      zend_ast *name_ast = args->child[2]->child[0];
4518
1.40k
      bool is_fully_qualified;
4519
1.40k
      zend_string *resolved_name = zend_resolve_const_name(
4520
1.40k
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
4521
4522
1.40k
      if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) {
4523
1.12k
        zend_string_release_ex(resolved_name, 0);
4524
1.12k
        return FAILURE;
4525
1.12k
      }
4526
4527
287
      zend_string_release_ex(resolved_name, 0);
4528
287
      strict = zend_is_true(&value);
4529
287
      zval_ptr_dtor(&value);
4530
7.80k
    } else {
4531
7.80k
      return FAILURE;
4532
7.80k
    }
4533
10.0k
  } else if (args->children != 2) {
4534
257
    return FAILURE;
4535
257
  }
4536
4537
2.63k
  if (args->child[1]->kind != ZEND_AST_ARRAY
4538
2.13k
   || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) {
4539
581
    return FAILURE;
4540
581
  }
4541
4542
2.05k
  if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) {
4543
1.78k
    bool ok = true;
4544
1.78k
    zval *val, tmp;
4545
1.78k
    HashTable *src = Z_ARRVAL(array.u.constant);
4546
1.78k
    HashTable *dst = zend_new_array(zend_hash_num_elements(src));
4547
4548
1.78k
    ZVAL_TRUE(&tmp);
4549
4550
1.78k
    if (strict) {
4551
5.03k
      ZEND_HASH_FOREACH_VAL(src, val) {
4552
5.03k
        if (Z_TYPE_P(val) == IS_STRING) {
4553
70
          zend_hash_add(dst, Z_STR_P(val), &tmp);
4554
1.44k
        } else if (Z_TYPE_P(val) == IS_LONG) {
4555
1.30k
          zend_hash_index_add(dst, Z_LVAL_P(val), &tmp);
4556
1.30k
        } else {
4557
135
          zend_array_destroy(dst);
4558
135
          ok = false;
4559
135
          break;
4560
135
        }
4561
5.03k
      } ZEND_HASH_FOREACH_END();
4562
972
    } else {
4563
3.94k
      ZEND_HASH_FOREACH_VAL(src, val) {
4564
3.94k
        if (Z_TYPE_P(val) != IS_STRING
4565
877
         || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) {
4566
737
          zend_array_destroy(dst);
4567
737
          ok = false;
4568
737
          break;
4569
737
        }
4570
488
        zend_hash_add(dst, Z_STR_P(val), &tmp);
4571
488
      } ZEND_HASH_FOREACH_END();
4572
972
    }
4573
4574
1.78k
    zend_array_destroy(src);
4575
1.78k
    if (!ok) {
4576
872
      return FAILURE;
4577
872
    }
4578
915
    Z_ARRVAL(array.u.constant) = dst;
4579
915
  }
4580
1.18k
  array.op_type = IS_CONST;
4581
4582
1.18k
  zend_compile_expr(&needly, args->child[0]);
4583
4584
1.18k
  opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array);
4585
1.18k
  opline->extended_value = strict;
4586
4587
1.18k
  return SUCCESS;
4588
2.05k
}
4589
/* }}} */
4590
4591
static zend_result zend_compile_func_count(znode *result, const zend_ast_list *args, const zend_string *lcname) /* {{{ */
4592
1.25k
{
4593
1.25k
  znode arg_node;
4594
1.25k
  zend_op *opline;
4595
4596
1.25k
  if (args->children != 1) {
4597
36
    return FAILURE;
4598
36
  }
4599
4600
1.21k
  zend_compile_expr(&arg_node, args->child[0]);
4601
1.21k
  opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL);
4602
1.21k
  opline->extended_value = zend_string_equals_literal(lcname, "sizeof");
4603
4604
1.21k
  return SUCCESS;
4605
1.25k
}
4606
/* }}} */
4607
4608
static zend_result zend_compile_func_get_class(znode *result, const zend_ast_list *args) /* {{{ */
4609
1.40k
{
4610
1.40k
  if (args->children == 0) {
4611
94
    zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL);
4612
1.31k
  } else {
4613
1.31k
    znode arg_node;
4614
4615
1.31k
    if (args->children != 1) {
4616
194
      return FAILURE;
4617
194
    }
4618
4619
1.12k
    zend_compile_expr(&arg_node, args->child[0]);
4620
1.12k
    zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL);
4621
1.12k
  }
4622
1.21k
  return SUCCESS;
4623
1.40k
}
4624
/* }}} */
4625
4626
static zend_result zend_compile_func_get_called_class(znode *result, const zend_ast_list *args) /* {{{ */
4627
270
{
4628
270
  if (args->children != 0) {
4629
107
    return FAILURE;
4630
107
  }
4631
4632
163
  zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL);
4633
163
  return SUCCESS;
4634
270
}
4635
/* }}} */
4636
4637
static zend_result zend_compile_func_gettype(znode *result, const zend_ast_list *args) /* {{{ */
4638
465
{
4639
465
  znode arg_node;
4640
4641
465
  if (args->children != 1) {
4642
250
    return FAILURE;
4643
250
  }
4644
4645
215
  zend_compile_expr(&arg_node, args->child[0]);
4646
215
  zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL);
4647
215
  return SUCCESS;
4648
465
}
4649
/* }}} */
4650
4651
static zend_result zend_compile_func_num_args(znode *result, const zend_ast_list *args) /* {{{ */
4652
108
{
4653
108
  if (CG(active_op_array)->function_name && args->children == 0) {
4654
56
    zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL);
4655
56
    return SUCCESS;
4656
56
  } else {
4657
52
    return FAILURE;
4658
52
  }
4659
108
}
4660
/* }}} */
4661
4662
static zend_result zend_compile_func_get_args(znode *result, const zend_ast_list *args) /* {{{ */
4663
579
{
4664
579
  if (CG(active_op_array)->function_name && args->children == 0) {
4665
389
    zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL);
4666
389
    return SUCCESS;
4667
389
  } else {
4668
190
    return FAILURE;
4669
190
  }
4670
579
}
4671
/* }}} */
4672
4673
static zend_result zend_compile_func_array_key_exists(znode *result, const zend_ast_list *args) /* {{{ */
4674
287
{
4675
287
  znode subject, needle;
4676
4677
287
  if (args->children != 2) {
4678
90
    return FAILURE;
4679
90
  }
4680
4681
197
  zend_compile_expr(&needle, args->child[0]);
4682
197
  zend_compile_expr(&subject, args->child[1]);
4683
4684
197
  zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject);
4685
197
  return SUCCESS;
4686
287
}
4687
/* }}} */
4688
4689
static zend_result zend_compile_func_array_slice(znode *result, const zend_ast_list *args) /* {{{ */
4690
2.90k
{
4691
2.90k
  if (CG(active_op_array)->function_name
4692
1.81k
   && args->children == 2
4693
586
   && args->child[0]->kind == ZEND_AST_CALL
4694
459
   && args->child[0]->child[0]->kind == ZEND_AST_ZVAL
4695
333
   && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING
4696
333
   && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST
4697
333
   && args->child[1]->kind == ZEND_AST_ZVAL) {
4698
4699
306
    zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]);
4700
306
    bool is_fully_qualified;
4701
306
    zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified);
4702
306
    const zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]);
4703
306
    const zval *zv = zend_ast_get_zval(args->child[1]);
4704
306
    znode first;
4705
4706
306
    if (zend_string_equals_literal_ci(name, "func_get_args")
4707
230
     && list->children == 0
4708
191
     && Z_TYPE_P(zv) == IS_LONG
4709
50
     && Z_LVAL_P(zv) >= 0) {
4710
50
      first.op_type = IS_CONST;
4711
50
      ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv));
4712
50
      zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL);
4713
50
      zend_string_release_ex(name, 0);
4714
50
      return SUCCESS;
4715
50
    }
4716
256
    zend_string_release_ex(name, 0);
4717
256
  }
4718
2.85k
  return FAILURE;
4719
2.90k
}
4720
/* }}} */
4721
4722
static uint32_t find_frameless_function_offset(uint32_t arity, const void *handler)
4723
1.32M
{
4724
1.32M
  void **handlers = zend_flf_handlers;
4725
1.32M
  void **current = handlers;
4726
12.1M
  while (current) {
4727
12.1M
    if (*current == handler) {
4728
1.32M
      return current - handlers;
4729
1.32M
    }
4730
10.8M
    current++;
4731
10.8M
  }
4732
4733
0
  return (uint32_t)-1;
4734
1.32M
}
4735
4736
static const zend_frameless_function_info *find_frameless_function_info(const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4737
814k
{
4738
814k
  if (zend_execute_internal) {
4739
96.0k
    return NULL;
4740
96.0k
  }
4741
4742
718k
  if (ZEND_USER_CODE(fbc->type)) {
4743
12
    return NULL;
4744
12
  }
4745
4746
718k
  const zend_frameless_function_info *frameless_function_info = fbc->internal_function.frameless_function_infos;
4747
718k
  if (!frameless_function_info) {
4748
17.9k
    return NULL;
4749
17.9k
  }
4750
4751
700k
  if (args->children > 3) {
4752
310
    return NULL;
4753
310
  }
4754
4755
894k
  while (frameless_function_info->handler) {
4756
859k
    if (frameless_function_info->num_args >= args->children
4757
734k
     && fbc->common.required_num_args <= args->children
4758
665k
     && (!(fbc->common.fn_flags & ZEND_ACC_VARIADIC)
4759
664k
      || frameless_function_info->num_args == args->children)) {
4760
664k
      uint32_t num_args = frameless_function_info->num_args;
4761
664k
      uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4762
664k
      if (offset == (uint32_t)-1) {
4763
0
        continue;
4764
0
      }
4765
664k
      return frameless_function_info;
4766
664k
    }
4767
194k
    frameless_function_info++;
4768
194k
  }
4769
4770
35.4k
  return NULL;
4771
699k
}
4772
4773
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)
4774
664k
{
4775
664k
  uint32_t lineno = CG(zend_lineno);
4776
664k
  uint32_t num_args = frameless_function_info->num_args;
4777
664k
  uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4778
664k
  znode arg_zvs[3];
4779
2.11M
  for (uint32_t i = 0; i < num_args; i++) {
4780
1.45M
    if (i < args->children) {
4781
1.45M
      zend_compile_expr(&arg_zvs[i], args->child[i]);
4782
1.45M
    } else {
4783
0
      const zend_arg_info *arg_info = &fbc->common.arg_info[i];
4784
0
      arg_zvs[i].op_type = IS_CONST;
4785
0
      if (zend_get_default_from_internal_arg_info(&arg_zvs[i].u.constant, arg_info) == FAILURE) {
4786
0
        ZEND_UNREACHABLE();
4787
0
      }
4788
0
    }
4789
1.45M
  }
4790
664k
  uint8_t opcode = ZEND_FRAMELESS_ICALL_0 + num_args;
4791
664k
  uint32_t opnum = get_next_op_number();
4792
664k
  zend_op *opline = zend_emit_op_tmp(result, opcode, NULL, NULL);
4793
664k
  opline->extended_value = offset;
4794
664k
  opline->lineno = lineno;
4795
664k
  if (num_args >= 1) {
4796
663k
    SET_NODE(opline->op1, &arg_zvs[0]);
4797
663k
  }
4798
664k
  if (num_args >= 2) {
4799
661k
    SET_NODE(opline->op2, &arg_zvs[1]);
4800
661k
  }
4801
664k
  if (num_args >= 3) {
4802
124k
    zend_emit_op_data(&arg_zvs[2]);
4803
124k
  }
4804
664k
  return opnum;
4805
664k
}
4806
4807
static uint32_t zend_compile_frameless_icall(znode *result, const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4808
116k
{
4809
116k
  const zend_frameless_function_info *frameless_function_info = find_frameless_function_info(args, fbc, type);
4810
116k
  if (!frameless_function_info) {
4811
105k
    return (uint32_t)-1;
4812
105k
  }
4813
4814
10.3k
  return zend_compile_frameless_icall_ex(result, args, fbc, frameless_function_info, type);
4815
116k
}
4816
4817
static void zend_compile_ns_call(znode *result, const znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */
4818
3.65M
{
4819
3.65M
  int name_constants = zend_add_ns_func_name_literal(Z_STR(name_node->u.constant));
4820
4821
  /* Find frameless function with same name. */
4822
3.65M
  const zend_function *frameless_function = NULL;
4823
3.65M
  if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT
4824
3.64M
   && !zend_args_contain_unpack_or_named(zend_ast_get_list(args_ast))
4825
   /* Avoid blowing up op count with nested frameless branches. */
4826
3.62M
   && !CG(context).in_jmp_frameless_branch) {
4827
2.35M
    zend_string *lc_func_name = Z_STR_P(CT_CONSTANT_EX(CG(active_op_array), name_constants + 2));
4828
2.35M
    frameless_function = zend_hash_find_ptr(CG(function_table), lc_func_name);
4829
2.35M
  }
4830
4831
  /* Check whether any frameless handler may actually be used. */
4832
3.65M
  uint32_t jmp_fl_opnum = 0;
4833
3.65M
  const zend_frameless_function_info *frameless_function_info = NULL;
4834
3.65M
  if (frameless_function) {
4835
697k
    frameless_function_info = find_frameless_function_info(zend_ast_get_list(args_ast), frameless_function, type);
4836
697k
    if (frameless_function_info) {
4837
653k
      CG(context).in_jmp_frameless_branch = true;
4838
653k
      znode op1;
4839
653k
      op1.op_type = IS_CONST;
4840
653k
      ZVAL_COPY(&op1.u.constant, CT_CONSTANT_EX(CG(active_op_array), name_constants + 1));
4841
653k
      jmp_fl_opnum = get_next_op_number();
4842
653k
      zend_emit_op(NULL, ZEND_JMP_FRAMELESS, &op1, NULL);
4843
653k
    }
4844
697k
  }
4845
4846
  /* Compile ns call. */
4847
3.65M
  zend_op *opline = get_next_op();
4848
3.65M
  opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
4849
3.65M
  opline->op2_type = IS_CONST;
4850
3.65M
  opline->op2.constant = name_constants;
4851
3.65M
  opline->result.num = zend_alloc_cache_slot();
4852
3.65M
  zend_compile_call_common(result, args_ast, NULL, lineno, type);
4853
4854
  /* Compile frameless call. */
4855
3.65M
  if (frameless_function_info) {
4856
653k
    CG(zend_lineno) = lineno;
4857
4858
653k
    uint32_t jmp_end_opnum = zend_emit_jump(0);
4859
653k
    uint32_t jmp_fl_target = get_next_op_number();
4860
4861
653k
    uint32_t flf_icall_opnum = zend_compile_frameless_icall_ex(NULL, zend_ast_get_list(args_ast), frameless_function, frameless_function_info, type);
4862
4863
653k
    zend_op *jmp_fl = &CG(active_op_array)->opcodes[jmp_fl_opnum];
4864
653k
    jmp_fl->op2.opline_num = jmp_fl_target;
4865
653k
    jmp_fl->extended_value = zend_alloc_cache_slot();
4866
653k
    zend_op *flf_icall = &CG(active_op_array)->opcodes[flf_icall_opnum];
4867
653k
    SET_NODE(flf_icall->result, result);
4868
653k
    zend_update_jump_target_to_next(jmp_end_opnum);
4869
4870
653k
    CG(context).in_jmp_frameless_branch = false;
4871
653k
  }
4872
3.65M
}
4873
/* }}} */
4874
4875
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node);
4876
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node);
4877
static void zend_compile_rope_finalize(znode *result, uint32_t j, zend_op *init_opline, zend_op *opline);
4878
4879
static zend_result zend_compile_func_sprintf(znode *result, zend_ast_list *args) /* {{{ */
4880
4.50k
{
4881
  /* Bail out if we do not have a format string. */
4882
4.50k
  if (args->children < 1) {
4883
102
    return FAILURE;
4884
102
  }
4885
4886
4.40k
  zend_eval_const_expr(&args->child[0]);
4887
  /* Bail out if the format string is not constant. */
4888
4.40k
  if (args->child[0]->kind != ZEND_AST_ZVAL) {
4889
313
    return FAILURE;
4890
313
  }
4891
4892
4.09k
  zval *format_string = zend_ast_get_zval(args->child[0]);
4893
4.09k
  if (Z_TYPE_P(format_string) != IS_STRING) {
4894
76
    return FAILURE;
4895
76
  }
4896
4.01k
  if (Z_STRLEN_P(format_string) >= 256) {
4897
60
    return FAILURE;
4898
60
  }
4899
4900
3.95k
  char *p;
4901
3.95k
  char *end;
4902
3.95k
  uint32_t placeholder_count;
4903
4904
3.95k
  placeholder_count = 0;
4905
3.95k
  p = Z_STRVAL_P(format_string);
4906
3.95k
  end = p + Z_STRLEN_P(format_string);
4907
4908
8.81k
  for (;;) {
4909
8.81k
    p = memchr(p, '%', end - p);
4910
8.81k
    if (!p) {
4911
3.53k
      break;
4912
3.53k
    }
4913
4914
5.27k
    char *q = p + 1;
4915
5.27k
    if (q == end) {
4916
112
      return FAILURE;
4917
112
    }
4918
4919
5.16k
    switch (*q) {
4920
3.38k
      case 's':
4921
4.01k
      case 'd':
4922
4.01k
        placeholder_count++;
4923
4.01k
        break;
4924
849
      case '%':
4925
849
        break;
4926
305
      default:
4927
305
        return FAILURE;
4928
5.16k
    }
4929
4930
4.86k
    p = q;
4931
4.86k
    p++;
4932
4.86k
  }
4933
4934
  /* Bail out if the number of placeholders does not match the number of values. */
4935
3.53k
  if (placeholder_count != (args->children - 1)) {
4936
296
    return FAILURE;
4937
296
  }
4938
4939
  /* Handle empty format strings. */
4940
3.24k
  if (Z_STRLEN_P(format_string) == 0) {
4941
44
    result->op_type = IS_CONST;
4942
44
    ZVAL_EMPTY_STRING(&result->u.constant);
4943
4944
44
    return SUCCESS;
4945
44
  }
4946
4947
3.19k
  znode *elements = NULL;
4948
4949
3.19k
  if (placeholder_count > 0) {
4950
2.83k
    elements = safe_emalloc(sizeof(*elements), placeholder_count, 0);
4951
2.83k
  }
4952
4953
  /* Compile the value expressions first for error handling that is consistent
4954
   * with a function call: Values that fail to convert to a string may emit errors.
4955
   */
4956
6.91k
  for (uint32_t i = 0; i < placeholder_count; i++) {
4957
3.71k
    zend_compile_expr(elements + i, args->child[1 + i]);
4958
3.71k
  }
4959
4960
3.19k
  uint32_t rope_elements = 0;
4961
3.19k
  uint32_t rope_init_lineno = -1;
4962
3.19k
  zend_op *opline = NULL;
4963
4964
3.19k
  placeholder_count = 0;
4965
3.19k
  p = Z_STRVAL_P(format_string);
4966
3.19k
  end = p + Z_STRLEN_P(format_string);
4967
3.19k
  char *offset = p;
4968
7.37k
  for (;;) {
4969
7.37k
    p = memchr(p, '%', end - p);
4970
7.37k
    if (!p) {
4971
3.19k
      break;
4972
3.19k
    }
4973
4974
4.17k
    char *q = p + 1;
4975
4.17k
    ZEND_ASSERT(q < end);
4976
4.17k
    ZEND_ASSERT(*q == 's' || *q == 'd' || *q == '%');
4977
4978
4.17k
    if (*q == '%') {
4979
      /* Optimization to not create a dedicated rope element for the literal '%':
4980
       * Include the first '%' within the "constant" part instead of dropping the
4981
       * full placeholder.
4982
       */
4983
465
      p++;
4984
465
    }
4985
4986
4.17k
    if (p != offset) {
4987
2.76k
      znode const_node;
4988
2.76k
      const_node.op_type = IS_CONST;
4989
2.76k
      ZVAL_STRINGL(&const_node.u.constant, offset, p - offset);
4990
2.76k
      if (rope_elements == 0) {
4991
1.56k
        rope_init_lineno = get_next_op_number();
4992
1.56k
      }
4993
2.76k
      opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4994
2.76k
    }
4995
4996
4.17k
    if (*q != '%') {
4997
3.71k
      switch (*q) {
4998
3.32k
        case 's':
4999
          /* Perform the cast of constants when actually evaluating the corresponding placeholder
5000
           * for correct error reporting.
5001
           */
5002
3.32k
          if (elements[placeholder_count].op_type == IS_CONST) {
5003
677
            if (Z_TYPE(elements[placeholder_count].u.constant) == IS_ARRAY) {
5004
75
              zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_STRING;
5005
602
            } else {
5006
602
              convert_to_string(&elements[placeholder_count].u.constant);
5007
602
            }
5008
677
          }
5009
3.32k
          break;
5010
386
        case 'd':
5011
386
          zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_LONG;
5012
386
          break;
5013
0
        default: ZEND_UNREACHABLE();
5014
3.71k
      }
5015
5016
3.71k
      if (rope_elements == 0) {
5017
1.30k
        rope_init_lineno = get_next_op_number();
5018
1.30k
      }
5019
3.71k
      opline = zend_compile_rope_add(result, rope_elements++, &elements[placeholder_count]);
5020
5021
3.71k
      placeholder_count++;
5022
3.71k
    }
5023
5024
4.17k
    p = q;
5025
4.17k
    p++;
5026
4.17k
    offset = p;
5027
4.17k
  }
5028
3.19k
  if (end != offset) {
5029
    /* Add the constant part after the last placeholder. */
5030
2.63k
    znode const_node;
5031
2.63k
    const_node.op_type = IS_CONST;
5032
2.63k
    ZVAL_STRINGL(&const_node.u.constant, offset, end - offset);
5033
2.63k
    if (rope_elements == 0) {
5034
326
      rope_init_lineno = get_next_op_number();
5035
326
    }
5036
2.63k
    opline = zend_compile_rope_add(result, rope_elements++, &const_node);
5037
2.63k
  }
5038
3.19k
  ZEND_ASSERT(opline != NULL);
5039
5040
3.19k
  zend_op *init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
5041
3.19k
  zend_compile_rope_finalize(result, rope_elements, init_opline, opline);
5042
3.19k
  efree(elements);
5043
5044
3.19k
  return SUCCESS;
5045
3.19k
}
5046
5047
static zend_result zend_compile_func_printf(znode *result, zend_ast_list *args) /* {{{ */
5048
3.59k
{
5049
  /* Special case: printf with a single constant string argument and no format specifiers.
5050
   * In this case, just emit ECHO and return the string length if needed. */
5051
3.59k
  if (args->children == 1) {
5052
761
    zend_eval_const_expr(&args->child[0]);
5053
761
    if (args->child[0]->kind != ZEND_AST_ZVAL) {
5054
108
      return FAILURE;
5055
108
    }
5056
653
    zval *format_string = zend_ast_get_zval(args->child[0]);
5057
653
    if (Z_TYPE_P(format_string) != IS_STRING) {
5058
200
      return FAILURE;
5059
200
    }
5060
    /* Check if there are any format specifiers */
5061
453
    if (!memchr(Z_STRVAL_P(format_string), '%', Z_STRLEN_P(format_string))) {
5062
      /* No format specifiers - just emit ECHO and return string length */
5063
340
      znode format_node;
5064
340
      zend_compile_expr(&format_node, args->child[0]);
5065
340
      zend_emit_op(NULL, ZEND_ECHO, &format_node, NULL);
5066
5067
      /* Return the string length as a constant if the result is used */
5068
340
      result->op_type = IS_CONST;
5069
340
      ZVAL_LONG(&result->u.constant, Z_STRLEN_P(format_string));
5070
340
      return SUCCESS;
5071
340
    }
5072
453
  }
5073
5074
  /* Fall back to sprintf optimization for format strings with specifiers */
5075
2.95k
  znode rope_result;
5076
2.95k
  if (zend_compile_func_sprintf(&rope_result, args) != SUCCESS) {
5077
366
    return FAILURE;
5078
366
  }
5079
5080
  /* printf() returns the amount of bytes written, so just an ECHO of the
5081
   * resulting sprintf() optimisation might not be enough. At this early
5082
   * stage we can't detect if the result is actually used, so we just emit
5083
   * the opcodes and let them be cleaned up by the dead code elimination
5084
   * pass in the Zend Optimizer if the result of the printf() is in fact
5085
   * unused */
5086
2.58k
  znode copy;
5087
2.58k
  if (rope_result.op_type != IS_CONST) {
5088
    /* Note: ZEND_COPY_TMP is only valid for TMPVAR. */
5089
2.29k
    ZEND_ASSERT(rope_result.op_type == IS_TMP_VAR);
5090
2.29k
    zend_emit_op_tmp(&copy, ZEND_COPY_TMP, &rope_result, NULL);
5091
2.29k
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5092
2.29k
    zend_emit_op_tmp(result, ZEND_STRLEN, &copy, NULL);
5093
2.29k
  } else {
5094
289
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5095
289
    result->op_type = IS_CONST;
5096
289
    ZVAL_LONG(&result->u.constant, Z_STRLEN(rope_result.u.constant));
5097
289
  }
5098
5099
2.58k
  return SUCCESS;
5100
2.58k
}
5101
5102
static zend_result zend_compile_func_clone(znode *result, const zend_ast_list *args)
5103
4.89k
{
5104
4.89k
  znode arg_node;
5105
5106
4.89k
  if (args->children != 1) {
5107
224
    return FAILURE;
5108
224
  }
5109
5110
4.67k
  zend_compile_expr(&arg_node, args->child[0]);
5111
4.67k
  zend_emit_op_tmp(result, ZEND_CLONE, &arg_node, NULL);
5112
5113
4.67k
  return SUCCESS;
5114
4.89k
}
5115
5116
static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t lineno) /* {{{ */
5117
536
{
5118
  /* Bail out if we do not have exactly two parameters. */
5119
536
  if (args->children != 2) {
5120
68
    return FAILURE;
5121
68
  }
5122
5123
468
  zend_ast *callback = args->child[0];
5124
5125
  /* Bail out if the callback is not a FCC/PFA. */
5126
468
  zend_ast *args_ast;
5127
468
  switch (callback->kind) {
5128
136
    case ZEND_AST_CALL:
5129
150
    case ZEND_AST_STATIC_CALL:
5130
150
      args_ast = zend_ast_call_get_args(callback);
5131
150
      if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT) {
5132
64
        return FAILURE;
5133
64
      }
5134
5135
86
      break;
5136
318
    default:
5137
318
      return FAILURE;
5138
468
  }
5139
5140
  /* Bail out if the callback is assert() due to the AST stringification logic
5141
   * breaking for the generated call.
5142
   */
5143
86
  if (callback->kind == ZEND_AST_CALL
5144
72
   && callback->child[0]->kind == ZEND_AST_ZVAL 
5145
70
   && Z_TYPE_P(zend_ast_get_zval(callback->child[0])) == IS_STRING
5146
70
   && zend_string_equals_literal_ci(zend_ast_get_str(callback->child[0]), "assert")) {
5147
1
    return FAILURE;
5148
1
  }
5149
5150
85
  zend_ast_list *callback_args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args);
5151
85
  if (callback_args->children != 1 || callback_args->child[0]->attr != ZEND_PLACEHOLDER_VARIADIC) {
5152
    /* Full PFA is not yet implemented, will fail in zend_compile_call_common(). */
5153
0
    return FAILURE;
5154
0
  }
5155
5156
85
  znode value;
5157
85
  value.op_type = IS_TMP_VAR;
5158
85
  value.u.op.var = get_temporary_variable();
5159
85
  zend_ast *call_args = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&value));
5160
5161
85
  zend_op *opline;
5162
5163
85
  znode array;
5164
85
  zend_compile_expr(&array, args->child[1]);
5165
  /* array is an argument to both ZEND_TYPE_ASSERT and to ZEND_FE_RESET_R. */
5166
85
  if (array.op_type == IS_CONST) {
5167
39
    Z_TRY_ADDREF(array.u.constant);
5168
39
  }
5169
5170
  /* Verify that the input array actually is an array. */
5171
85
  znode name;
5172
85
  name.op_type = IS_CONST;
5173
85
  ZVAL_STR_COPY(&name.u.constant, lcname);
5174
85
  opline = zend_emit_op(NULL, ZEND_TYPE_ASSERT, &name, &array);
5175
85
  opline->lineno = lineno;
5176
85
  opline->extended_value = (2 << 16) | IS_ARRAY;
5177
85
  const zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5178
85
  const Bucket *fbc_bucket = ZEND_CONTAINER_OF(fbc_zv, Bucket, val);
5179
85
  Z_EXTRA_P(CT_CONSTANT(opline->op1)) = fbc_bucket - CG(function_table)->arData;
5180
5181
  /* Initialize the result array. */
5182
85
  zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
5183
5184
  /* foreach loop starts here. */
5185
85
  znode key;
5186
5187
85
  uint32_t opnum_reset = get_next_op_number();
5188
85
  znode reset_node;
5189
85
  zend_emit_op(&reset_node, ZEND_FE_RESET_R, &array, NULL);
5190
85
  zend_begin_loop(ZEND_FE_FREE, &reset_node, false);
5191
85
  uint32_t opnum_fetch = get_next_op_number();
5192
85
  zend_emit_op_tmp(&key, ZEND_FE_FETCH_R, &reset_node, &value);
5193
5194
  /* loop body */
5195
85
  znode call_result;
5196
85
  switch (callback->kind) {
5197
71
    case ZEND_AST_CALL:
5198
71
      zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_CALL, callback->child[0], call_args));
5199
71
      break;
5200
14
    case ZEND_AST_STATIC_CALL:
5201
14
      zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_STATIC_CALL, callback->child[0], callback->child[1], call_args));
5202
14
      break;
5203
85
  }
5204
85
  opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT, &call_result, &key);
5205
85
  SET_NODE(opline->result, result);
5206
  /* end loop body */
5207
5208
85
  zend_emit_jump(opnum_fetch);
5209
5210
85
  uint32_t opnum_loop_end = get_next_op_number();
5211
85
  opline = &CG(active_op_array)->opcodes[opnum_reset];
5212
85
  opline->op2.opline_num = opnum_loop_end;
5213
85
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
5214
85
  opline->extended_value = opnum_loop_end;
5215
5216
85
  zend_end_loop(opnum_fetch, &reset_node);
5217
85
  zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
5218
5219
85
  return SUCCESS;
5220
85
}
5221
5222
static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type, uint32_t lineno) /* {{{ */
5223
141k
{
5224
141k
  if (zend_string_equals_literal(lcname, "strlen")) {
5225
1.06k
    return zend_compile_func_strlen(result, args);
5226
140k
  } else if (zend_string_equals_literal(lcname, "is_null")) {
5227
251
    return zend_compile_func_typecheck(result, args, IS_NULL);
5228
140k
  } else if (zend_string_equals_literal(lcname, "is_bool")) {
5229
437
    return zend_compile_func_typecheck(result, args, _IS_BOOL);
5230
139k
  } else if (zend_string_equals_literal(lcname, "is_long")
5231
139k
    || zend_string_equals_literal(lcname, "is_int")
5232
139k
    || zend_string_equals_literal(lcname, "is_integer")
5233
139k
  ) {
5234
515
    return zend_compile_func_typecheck(result, args, IS_LONG);
5235
139k
  } else if (zend_string_equals_literal(lcname, "is_float")
5236
139k
    || zend_string_equals_literal(lcname, "is_double")
5237
139k
  ) {
5238
558
    return zend_compile_func_typecheck(result, args, IS_DOUBLE);
5239
138k
  } else if (zend_string_equals_literal(lcname, "is_string")) {
5240
58
    return zend_compile_func_typecheck(result, args, IS_STRING);
5241
138k
  } else if (zend_string_equals_literal(lcname, "is_array")) {
5242
320
    return zend_compile_func_typecheck(result, args, IS_ARRAY);
5243
138k
  } else if (zend_string_equals_literal(lcname, "is_object")) {
5244
144
    return zend_compile_func_typecheck(result, args, IS_OBJECT);
5245
138k
  } else if (zend_string_equals_literal(lcname, "is_resource")) {
5246
203
    return zend_compile_func_typecheck(result, args, IS_RESOURCE);
5247
138k
  } else if (zend_string_equals_literal(lcname, "is_scalar")) {
5248
308
    return zend_compile_func_is_scalar(result, args);
5249
137k
  } else if (zend_string_equals_literal(lcname, "boolval")) {
5250
206
    return zend_compile_func_cast(result, args, _IS_BOOL);
5251
137k
  } else if (zend_string_equals_literal(lcname, "intval")) {
5252
65
    return zend_compile_func_cast(result, args, IS_LONG);
5253
137k
  } else if (zend_string_equals_literal(lcname, "floatval")
5254
137k
    || zend_string_equals_literal(lcname, "doubleval")
5255
137k
  ) {
5256
463
    return zend_compile_func_cast(result, args, IS_DOUBLE);
5257
137k
  } else if (zend_string_equals_literal(lcname, "strval")) {
5258
198
    return zend_compile_func_cast(result, args, IS_STRING);
5259
136k
  } else if (zend_string_equals_literal(lcname, "defined")) {
5260
1.99k
    return zend_compile_func_defined(result, args);
5261
134k
  } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) {
5262
1.04k
    return zend_compile_func_chr(result, args);
5263
133k
  } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) {
5264
594
    return zend_compile_func_ord(result, args);
5265
133k
  } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
5266
2.33k
    return zend_compile_func_cufa(result, args, lcname, type);
5267
130k
  } else if (zend_string_equals_literal(lcname, "call_user_func")) {
5268
4.77k
    return zend_compile_func_cuf(result, args, lcname, type);
5269
126k
  } else if (zend_string_equals_literal(lcname, "in_array")) {
5270
11.8k
    return zend_compile_func_in_array(result, args);
5271
114k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_COUNT))
5272
113k
      || zend_string_equals_literal(lcname, "sizeof")) {
5273
1.25k
    return zend_compile_func_count(result, args, lcname);
5274
113k
  } else if (zend_string_equals_literal(lcname, "get_class")) {
5275
1.40k
    return zend_compile_func_get_class(result, args);
5276
111k
  } else if (zend_string_equals_literal(lcname, "get_called_class")) {
5277
270
    return zend_compile_func_get_called_class(result, args);
5278
111k
  } else if (zend_string_equals_literal(lcname, "gettype")) {
5279
465
    return zend_compile_func_gettype(result, args);
5280
110k
  } else if (zend_string_equals_literal(lcname, "func_num_args")) {
5281
108
    return zend_compile_func_num_args(result, args);
5282
110k
  } else if (zend_string_equals_literal(lcname, "func_get_args")) {
5283
579
    return zend_compile_func_get_args(result, args);
5284
110k
  } else if (zend_string_equals_literal(lcname, "array_slice")) {
5285
2.90k
    return zend_compile_func_array_slice(result, args);
5286
107k
  } else if (zend_string_equals_literal(lcname, "array_key_exists")) {
5287
287
    return zend_compile_func_array_key_exists(result, args);
5288
107k
  } else if (zend_string_equals_literal(lcname, "sprintf")) {
5289
1.55k
    return zend_compile_func_sprintf(result, args);
5290
105k
  } else if (zend_string_equals_literal(lcname, "printf")) {
5291
3.59k
    return zend_compile_func_printf(result, args);
5292
101k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_CLONE))) {
5293
4.89k
    return zend_compile_func_clone(result, args);
5294
97.0k
  } else if (zend_string_equals_literal(lcname, "array_map")) {
5295
536
    return zend_compile_func_array_map(result, args, lcname, lineno);
5296
96.5k
  } else {
5297
96.5k
    return FAILURE;
5298
96.5k
  }
5299
141k
}
5300
5301
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) /* {{{ */
5302
166k
{
5303
166k
  if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
5304
0
    return FAILURE;
5305
0
  }
5306
5307
166k
  if (fbc->type != ZEND_INTERNAL_FUNCTION) {
5308
    /* If the function is part of disabled_functions, it may be redeclared as a userland
5309
     * function with a different implementation. Don't use the VM builtin in that case. */
5310
23.3k
    return FAILURE;
5311
23.3k
  }
5312
5313
142k
  if (zend_args_contain_unpack_or_named(args)) {
5314
910
    return FAILURE;
5315
910
  }
5316
5317
141k
  if (zend_try_compile_special_func_ex(result, lcname, args, type, lineno) == SUCCESS) {
5318
25.4k
    return SUCCESS;
5319
25.4k
  }
5320
5321
116k
  return zend_compile_frameless_icall(result, args, fbc, type) != (uint32_t)-1 ? SUCCESS : FAILURE;
5322
141k
}
5323
5324
5
static const char *zend_get_cstring_from_property_hook_kind(zend_property_hook_kind kind) {
5325
5
  switch (kind) {
5326
0
    case ZEND_PROPERTY_HOOK_GET:
5327
0
      return "get";
5328
5
    case ZEND_PROPERTY_HOOK_SET:
5329
5
      return "set";
5330
0
    default: ZEND_UNREACHABLE();
5331
5
  }
5332
5
}
5333
5334
static zend_string *zend_copy_unmangled_prop_name(zend_string *prop_name)
5335
751
{
5336
751
  if (ZSTR_VAL(prop_name)[0] != '\0') {
5337
469
    return zend_string_copy(prop_name);
5338
469
  } else {
5339
282
    const char *unmangled = zend_get_unmangled_property_name(prop_name);
5340
282
    return zend_string_init(unmangled, strlen(unmangled), /* persistent */ false);
5341
282
  }
5342
751
}
5343
5344
static bool zend_compile_parent_property_hook_call(znode *result, const zend_ast *ast, uint32_t type)
5345
34.1k
{
5346
34.1k
  ZEND_ASSERT(ast->kind == ZEND_AST_STATIC_CALL);
5347
5348
34.1k
  const zend_ast *class_ast = ast->child[0];
5349
34.1k
  zend_ast *method_ast = ast->child[1];
5350
5351
  /* Recognize parent::$prop::get() pattern. */
5352
34.1k
  if (class_ast->kind != ZEND_AST_STATIC_PROP
5353
1.91k
   || (class_ast->attr & ZEND_PARENTHESIZED_STATIC_PROP)
5354
1.81k
   || class_ast->child[0]->kind != ZEND_AST_ZVAL
5355
1.38k
   || Z_TYPE_P(zend_ast_get_zval(class_ast->child[0])) != IS_STRING
5356
1.38k
   || zend_get_class_fetch_type(zend_ast_get_str(class_ast->child[0])) != ZEND_FETCH_CLASS_PARENT
5357
1.09k
   || class_ast->child[1]->kind != ZEND_AST_ZVAL
5358
979
   || method_ast->kind != ZEND_AST_ZVAL
5359
944
   || Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING
5360
944
   || (!zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "get")
5361
33.4k
    && !zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "set"))) {
5362
33.4k
    return false;
5363
33.4k
  }
5364
5365
709
  zend_class_entry *ce = CG(active_class_entry);
5366
709
  if (!ce) {
5367
8
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"parent\" when no class scope is active");
5368
8
  }
5369
5370
701
  zend_ast *args_ast = ast->child[2];
5371
701
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
5372
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for parent property hook call");
5373
7
  }
5374
5375
694
  zval *property_hook_name_zv = zend_ast_get_zval(class_ast->child[1]);
5376
694
  zend_string *property_name = zval_get_string(property_hook_name_zv);
5377
694
  zend_string *hook_name = zend_ast_get_str(method_ast);
5378
694
  zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(hook_name);
5379
694
  ZEND_ASSERT(hook_kind != (uint32_t)-1);
5380
5381
694
  const zend_string *prop_info_name = CG(context).active_property_info_name;
5382
694
  if (!prop_info_name) {
5383
5
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() outside a property hook",
5384
5
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name));
5385
5
  }
5386
5387
689
  const char *unmangled_prop_name = zend_get_unmangled_property_name(prop_info_name);
5388
689
  if (!zend_string_equals_cstr(property_name, unmangled_prop_name, strlen(unmangled_prop_name))) {
5389
17
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property ($%s)",
5390
17
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), unmangled_prop_name);
5391
17
  }
5392
672
  if (hook_kind != CG(context).active_property_hook_kind) {
5393
5
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property hook (%s)",
5394
5
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), zend_get_cstring_from_property_hook_kind(CG(context).active_property_hook_kind));
5395
5
  }
5396
5397
667
  zend_op *opline = get_next_op();
5398
667
  opline->opcode = ZEND_INIT_PARENT_PROPERTY_HOOK_CALL;
5399
667
  opline->op1_type = IS_CONST;
5400
667
  opline->op1.constant = zend_add_literal_string(&property_name);
5401
667
  opline->op2.num = hook_kind;
5402
5403
667
  zend_compile_call_common(result, args_ast, NULL, zend_ast_get_lineno(method_ast), BP_VAR_R);
5404
5405
667
  return true;
5406
672
}
5407
5408
static void zend_compile_call(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
5409
4.03M
{
5410
4.03M
  zend_ast *name_ast = ast->child[0];
5411
4.03M
  zend_ast *args_ast = ast->child[1];
5412
4.03M
  bool is_callable_convert = args_ast->kind == ZEND_AST_CALLABLE_CONVERT;
5413
5414
4.03M
  znode name_node;
5415
5416
4.03M
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
5417
44.6k
    zend_compile_expr(&name_node, name_ast);
5418
44.6k
    zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno, type);
5419
44.6k
    return;
5420
44.6k
  }
5421
5422
3.99M
  {
5423
3.99M
    bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
5424
3.99M
    if (runtime_resolution) {
5425
3.71M
      if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")
5426
59.7k
          && !is_callable_convert) {
5427
59.6k
        zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL, ast->lineno, type);
5428
3.65M
      } else {
5429
3.65M
        zend_compile_ns_call(result, &name_node, args_ast, ast->lineno, type);
5430
3.65M
      }
5431
3.71M
      return;
5432
3.71M
    }
5433
3.99M
  }
5434
5435
278k
  {
5436
278k
    const zval *name = &name_node.u.constant;
5437
278k
    zend_string *lcname = zend_string_tolower(Z_STR_P(name));
5438
278k
    zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5439
278k
    const zend_function *fbc = fbc_zv ? Z_PTR_P(fbc_zv) : NULL;
5440
278k
    zend_op *opline;
5441
5442
    /* Special assert() handling should apply independently of compiler flags. */
5443
278k
    if (fbc && zend_string_equals_literal(lcname, "assert") && !is_callable_convert) {
5444
13.8k
      zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc, ast->lineno, type);
5445
13.8k
      zend_string_release(lcname);
5446
13.8k
      zval_ptr_dtor(&name_node.u.constant);
5447
13.8k
      return;
5448
13.8k
    }
5449
5450
264k
    if (!fbc
5451
166k
     || !fbc_is_finalized(fbc)
5452
166k
     || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
5453
97.8k
      zend_string_release_ex(lcname, 0);
5454
97.8k
      zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno, type);
5455
97.8k
      return;
5456
97.8k
    }
5457
5458
166k
    if (!is_callable_convert &&
5459
166k
        zend_try_compile_special_func(result, lcname,
5460
166k
        zend_ast_get_list(args_ast), fbc, type, ast->lineno) == SUCCESS
5461
166k
    ) {
5462
35.8k
      zend_string_release_ex(lcname, 0);
5463
35.8k
      zval_ptr_dtor(&name_node.u.constant);
5464
35.8k
      return;
5465
35.8k
    }
5466
5467
130k
    zval_ptr_dtor(&name_node.u.constant);
5468
130k
    ZVAL_STR(&name_node.u.constant, lcname);
5469
5470
130k
    opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
5471
130k
    opline->result.num = zend_alloc_cache_slot();
5472
5473
    /* Store offset to function from symbol table in op2.extra. */
5474
130k
    if (fbc->type == ZEND_INTERNAL_FUNCTION) {
5475
107k
      const Bucket *fbc_bucket = ZEND_CONTAINER_OF(fbc_zv, Bucket, val);
5476
107k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = fbc_bucket - CG(function_table)->arData;
5477
107k
    }
5478
5479
130k
    zend_compile_call_common(result, args_ast, fbc, ast->lineno, type);
5480
130k
  }
5481
130k
}
5482
/* }}} */
5483
5484
static void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5485
261k
{
5486
261k
  zend_ast *obj_ast = ast->child[0];
5487
261k
  zend_ast *method_ast = ast->child[1];
5488
261k
  zend_ast *args_ast = ast->child[2];
5489
5490
261k
  znode obj_node, method_node;
5491
261k
  zend_op *opline;
5492
261k
  const zend_function *fbc = NULL;
5493
261k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL;
5494
261k
  uint32_t short_circuiting_checkpoint = zend_short_circuiting_checkpoint();
5495
5496
261k
  if (is_this_fetch(obj_ast)) {
5497
1.40k
    if (this_guaranteed_exists()) {
5498
1.32k
      obj_node.op_type = IS_UNUSED;
5499
1.32k
    } else {
5500
81
      zend_emit_op_tmp(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
5501
81
    }
5502
1.40k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
5503
5504
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
5505
     * check for a nullsafe access. */
5506
260k
  } else {
5507
260k
    zend_short_circuiting_mark_inner(obj_ast);
5508
260k
    zend_compile_expr(&obj_node, obj_ast);
5509
260k
    if (nullsafe) {
5510
3.21k
      zend_emit_jmp_null(&obj_node, type);
5511
3.21k
    }
5512
260k
  }
5513
5514
261k
  zend_compile_expr(&method_node, method_ast);
5515
261k
  opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
5516
5517
261k
  if (method_node.op_type == IS_CONST) {
5518
259k
    if (Z_TYPE(method_node.u.constant) != IS_STRING) {
5519
10
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5520
10
    }
5521
5522
259k
    opline->op2_type = IS_CONST;
5523
259k
    opline->op2.constant = zend_add_func_name_literal(
5524
259k
      Z_STR(method_node.u.constant));
5525
259k
    opline->result.num = zend_alloc_cache_slots(2);
5526
259k
  } else {
5527
2.02k
    SET_NODE(opline->op2, &method_node);
5528
2.02k
  }
5529
5530
  /* Check if this calls a known method on $this */
5531
261k
  if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST &&
5532
1.21k
      CG(active_class_entry) && zend_is_scope_known()) {
5533
806
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5534
806
    fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname);
5535
5536
    /* We only know the exact method that is being called if it is either private or final.
5537
     * Otherwise an overriding method in a child class may be called. */
5538
806
    if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) {
5539
334
      fbc = NULL;
5540
334
    }
5541
806
  }
5542
5543
261k
  if (zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast), type)) {
5544
298
    if (short_circuiting_checkpoint != zend_short_circuiting_checkpoint()) {
5545
12
      zend_error_noreturn(E_COMPILE_ERROR,
5546
12
        "Cannot combine nullsafe operator with Closure creation");
5547
12
    }
5548
298
  }
5549
261k
}
5550
/* }}} */
5551
5552
static bool zend_is_constructor(const zend_string *name) /* {{{ */
5553
33.9k
{
5554
33.9k
  return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
5555
33.9k
}
5556
/* }}} */
5557
5558
static bool is_func_accessible(const zend_function *fbc)
5559
38.6k
{
5560
38.6k
  if ((fbc->common.fn_flags & ZEND_ACC_PUBLIC) || fbc->common.scope == CG(active_class_entry)) {
5561
38.5k
    return true;
5562
38.5k
  }
5563
5564
132
  if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE)
5565
57
    && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED)
5566
57
    && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED))
5567
21
    && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) {
5568
0
    return true;
5569
0
  }
5570
5571
132
  return false;
5572
132
}
5573
5574
static const zend_function *zend_get_compatible_func_or_null(const zend_class_entry *ce, zend_string *lcname) /* {{{ */
5575
5.05k
{
5576
5.05k
  const zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname);
5577
5578
5.05k
  if (!fbc || is_func_accessible(fbc)) {
5579
4.96k
    return fbc;
5580
4.96k
  }
5581
5582
87
  return NULL;
5583
5.05k
}
5584
/* }}} */
5585
5586
static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5587
34.1k
{
5588
34.1k
  zend_ast *class_ast = ast->child[0];
5589
34.1k
  zend_ast *method_ast = ast->child[1];
5590
34.1k
  zend_ast *args_ast = ast->child[2];
5591
5592
34.1k
  znode class_node, method_node;
5593
34.1k
  zend_op *opline;
5594
34.1k
  const zend_function *fbc = NULL;
5595
5596
34.1k
  if (zend_compile_parent_property_hook_call(result, ast, type)) {
5597
667
    return;
5598
667
  }
5599
5600
33.5k
  zend_short_circuiting_mark_inner(class_ast);
5601
33.5k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5602
5603
33.5k
  zend_compile_expr(&method_node, method_ast);
5604
5605
33.5k
  if (method_node.op_type == IS_CONST) {
5606
32.5k
    zval *name = &method_node.u.constant;
5607
32.5k
    if (Z_TYPE_P(name) != IS_STRING) {
5608
3
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5609
3
    }
5610
32.5k
    if (zend_is_constructor(Z_STR_P(name))) {
5611
324
      zval_ptr_dtor(name);
5612
324
      method_node.op_type = IS_UNUSED;
5613
324
    }
5614
32.5k
  }
5615
5616
33.5k
  opline = get_next_op();
5617
33.5k
  opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
5618
5619
33.5k
  zend_set_class_name_op1(opline, &class_node);
5620
5621
33.5k
  if (method_node.op_type == IS_CONST) {
5622
32.2k
    opline->op2_type = IS_CONST;
5623
32.2k
    opline->op2.constant = zend_add_func_name_literal(
5624
32.2k
      Z_STR(method_node.u.constant));
5625
32.2k
    opline->result.num = zend_alloc_cache_slots(2);
5626
32.2k
  } else {
5627
1.25k
    if (opline->op1_type == IS_CONST) {
5628
277
      opline->result.num = zend_alloc_cache_slot();
5629
277
    }
5630
1.25k
    SET_NODE(opline->op2, &method_node);
5631
1.25k
  }
5632
5633
  /* Check if we already know which method we're calling */
5634
33.5k
  if (opline->op2_type == IS_CONST) {
5635
32.2k
    zend_class_entry *ce = NULL;
5636
32.2k
    if (opline->op1_type == IS_CONST) {
5637
15.5k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5638
15.5k
      ce = zend_hash_find_ptr(CG(class_table), lcname);
5639
15.5k
      if (ce) {
5640
4.42k
        if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5641
0
          ce = NULL;
5642
0
        }
5643
11.1k
      } else if (CG(active_class_entry)
5644
813
          && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5645
286
        ce = CG(active_class_entry);
5646
286
      }
5647
16.6k
    } else if (opline->op1_type == IS_UNUSED
5648
13.0k
        && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5649
12.2k
        && zend_is_scope_known()) {
5650
343
      ce = CG(active_class_entry);
5651
343
    }
5652
32.2k
    if (ce) {
5653
5.05k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5654
5.05k
      fbc = zend_get_compatible_func_or_null(ce, lcname);
5655
5.05k
    }
5656
32.2k
  }
5657
5658
33.5k
  zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast), type);
5659
33.5k
}
5660
/* }}} */
5661
5662
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel);
5663
5664
static void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
5665
75.9k
{
5666
75.9k
  zend_ast *class_ast = ast->child[0];
5667
75.9k
  zend_ast *args_ast = ast->child[1];
5668
5669
75.9k
  znode class_node, ctor_result;
5670
75.9k
  zend_op *opline;
5671
5672
75.9k
  if (class_ast->kind == ZEND_AST_CLASS) {
5673
    /* anon class declaration */
5674
1.75k
    zend_compile_class_decl(&class_node, class_ast, false);
5675
74.1k
  } else {
5676
74.1k
    zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5677
74.1k
  }
5678
5679
75.9k
  opline = zend_emit_op_tmp(result, ZEND_NEW, NULL, NULL);
5680
5681
75.9k
  zend_set_class_name_op1(opline, &class_node);
5682
5683
75.9k
  if (opline->op1_type == IS_CONST) {
5684
70.9k
    opline->op2.num = zend_alloc_cache_slot();
5685
70.9k
  }
5686
5687
75.9k
  zend_class_entry *ce = NULL;
5688
75.9k
  if (opline->op1_type == IS_CONST) {
5689
70.9k
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5690
70.9k
    ce = zend_hash_find_ptr(CG(class_table), lcname);
5691
70.9k
    if (ce) {
5692
48.7k
      if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5693
0
        ce = NULL;
5694
0
      }
5695
48.7k
    } else if (CG(active_class_entry)
5696
1.31k
        && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5697
426
      ce = CG(active_class_entry);
5698
426
    }
5699
70.9k
  } else if (opline->op1_type == IS_UNUSED
5700
470
      && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5701
243
      && zend_is_scope_known()) {
5702
122
    ce = CG(active_class_entry);
5703
122
  }
5704
5705
5706
75.9k
  const zend_function *fbc = NULL;
5707
75.9k
  if (ce
5708
49.2k
      && ce->default_object_handlers->get_constructor == zend_std_get_constructor
5709
49.0k
      && ce->constructor
5710
34.3k
      && is_func_accessible(ce->constructor)) {
5711
34.3k
    fbc = ce->constructor;
5712
34.3k
  }
5713
5714
75.9k
  zend_compile_call_common(&ctor_result, args_ast, fbc, ast->lineno, BP_VAR_R);
5715
75.9k
  zend_do_free(&ctor_result);
5716
75.9k
}
5717
/* }}} */
5718
5719
static void zend_compile_global_var(zend_ast *ast) /* {{{ */
5720
2.42k
{
5721
2.42k
  zend_ast *var_ast = ast->child[0];
5722
2.42k
  zend_ast *name_ast = var_ast->child[0];
5723
5724
2.42k
  znode name_node, result;
5725
5726
2.42k
  zend_compile_expr(&name_node, name_ast);
5727
2.42k
  if (name_node.op_type == IS_CONST) {
5728
2.07k
    convert_to_string(&name_node.u.constant);
5729
2.07k
  }
5730
5731
  // TODO(GLOBALS) Forbid "global $GLOBALS"?
5732
2.42k
  if (is_this_fetch(var_ast)) {
5733
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable");
5734
2.41k
  } else if (zend_try_compile_cv(&result, var_ast, BP_VAR_R) == SUCCESS) {
5735
1.72k
    zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
5736
1.72k
    opline->extended_value = zend_alloc_cache_slot();
5737
1.72k
  } else {
5738
    /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W
5739
     * to not free the name_node operand, so it can be reused in the following
5740
     * ASSIGN_REF, which then frees it. */
5741
695
    zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
5742
695
    opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
5743
5744
695
    if (name_node.op_type == IS_CONST) {
5745
344
      zend_string_addref(Z_STR(name_node.u.constant));
5746
344
    }
5747
5748
695
    zend_emit_assign_ref_znode(
5749
695
      zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)),
5750
695
      &result
5751
695
    );
5752
695
  }
5753
2.42k
}
5754
/* }}} */
5755
5756
static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */
5757
68.7k
{
5758
68.7k
  zend_op *opline;
5759
68.7k
  if (!CG(active_op_array)->static_variables) {
5760
0
    if (CG(active_op_array)->scope) {
5761
0
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5762
0
    }
5763
0
    CG(active_op_array)->static_variables = zend_new_array(8);
5764
0
  }
5765
5766
68.7k
  value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value);
5767
5768
68.7k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5769
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5770
0
  }
5771
5772
68.7k
  opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL);
5773
68.7k
  opline->op1_type = IS_CV;
5774
68.7k
  opline->op1.var = lookup_cv(var_name);
5775
68.7k
  opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode;
5776
68.7k
}
5777
/* }}} */
5778
5779
static void zend_compile_static_var(zend_ast *ast) /* {{{ */
5780
6.25k
{
5781
6.25k
  zend_ast *var_ast = ast->child[0];
5782
6.25k
  zend_string *var_name = zend_ast_get_str(var_ast);
5783
5784
6.25k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5785
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5786
5
  }
5787
5788
6.24k
  if (!CG(active_op_array)->static_variables) {
5789
5.33k
    if (CG(active_op_array)->scope) {
5790
4.36k
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5791
4.36k
    }
5792
5.33k
    CG(active_op_array)->static_variables = zend_new_array(8);
5793
5.33k
  }
5794
5795
6.24k
  if (zend_hash_exists(CG(active_op_array)->static_variables, var_name)) {
5796
12
    zend_error_noreturn_unchecked(E_COMPILE_ERROR, "Duplicate declaration of static variable $%S", var_name);
5797
12
  }
5798
5799
6.23k
  zend_eval_const_expr(&ast->child[1]);
5800
6.23k
  zend_ast *value_ast = ast->child[1];
5801
5802
6.23k
  if (!value_ast || value_ast->kind == ZEND_AST_ZVAL) {
5803
5.70k
    zval *value_zv = value_ast
5804
5.70k
      ? zend_ast_get_zval(value_ast)
5805
5.70k
      : &EG(uninitialized_zval);
5806
5.70k
    Z_TRY_ADDREF_P(value_zv);
5807
5.70k
    zend_compile_static_var_common(var_name, value_zv, ZEND_BIND_REF);
5808
5.70k
  } else {
5809
532
    zend_op *opline;
5810
5811
532
    zval *placeholder_ptr = zend_hash_update(CG(active_op_array)->static_variables, var_name, &EG(uninitialized_zval));
5812
532
    uint32_t placeholder_offset = (uint32_t)((char*)placeholder_ptr - (char*)CG(active_op_array)->static_variables->arData);
5813
5814
532
    uint32_t static_def_jmp_opnum = get_next_op_number();
5815
532
    opline = zend_emit_op(NULL, ZEND_BIND_INIT_STATIC_OR_JMP, NULL, NULL);
5816
532
    opline->op1_type = IS_CV;
5817
532
    opline->op1.var = lookup_cv(var_name);
5818
532
    opline->extended_value = placeholder_offset;
5819
5820
532
    znode expr;
5821
532
    zend_compile_expr(&expr, value_ast);
5822
5823
532
    opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, &expr);
5824
532
    opline->op1_type = IS_CV;
5825
532
    opline->op1.var = lookup_cv(var_name);
5826
532
    opline->extended_value = placeholder_offset | ZEND_BIND_REF;
5827
5828
532
    zend_update_jump_target_to_next(static_def_jmp_opnum);
5829
532
  }
5830
6.23k
}
5831
/* }}} */
5832
5833
static void zend_compile_unset(const zend_ast *ast) /* {{{ */
5834
8.47k
{
5835
8.47k
  zend_ast *var_ast = ast->child[0];
5836
8.47k
  znode var_node;
5837
8.47k
  zend_op *opline;
5838
5839
8.47k
  zend_ensure_writable_variable(var_ast);
5840
5841
8.47k
  if (is_global_var_fetch(var_ast)) {
5842
919
    if (!var_ast->child[1]) {
5843
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
5844
5
    }
5845
5846
914
    zend_compile_expr(&var_node, var_ast->child[1]);
5847
914
    if (var_node.op_type == IS_CONST) {
5848
532
      convert_to_string(&var_node.u.constant);
5849
532
    }
5850
5851
914
    opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL);
5852
914
    opline->extended_value = ZEND_FETCH_GLOBAL;
5853
914
    return;
5854
919
  }
5855
5856
7.55k
  switch (var_ast->kind) {
5857
4.10k
    case ZEND_AST_VAR:
5858
4.10k
      if (is_this_fetch(var_ast)) {
5859
8
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this");
5860
4.09k
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_UNSET) == SUCCESS) {
5861
3.90k
        opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL);
5862
3.90k
      } else {
5863
194
        opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, false);
5864
194
        opline->opcode = ZEND_UNSET_VAR;
5865
194
      }
5866
4.09k
      return;
5867
4.09k
    case ZEND_AST_DIM:
5868
2.22k
      opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET, /* by_ref */ false);
5869
2.22k
      opline->opcode = ZEND_UNSET_DIM;
5870
2.22k
      return;
5871
1.18k
    case ZEND_AST_PROP:
5872
1.18k
    case ZEND_AST_NULLSAFE_PROP:
5873
1.18k
      opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, false);
5874
1.18k
      opline->opcode = ZEND_UNSET_OBJ;
5875
1.18k
      return;
5876
26
    case ZEND_AST_STATIC_PROP:
5877
26
      opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, false, false);
5878
26
      opline->opcode = ZEND_UNSET_STATIC_PROP;
5879
26
      return;
5880
0
    default: ZEND_UNREACHABLE();
5881
7.55k
  }
5882
7.55k
}
5883
/* }}} */
5884
5885
static bool zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */
5886
232k
{
5887
232k
  const zend_loop_var *base;
5888
232k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5889
5890
232k
  if (!loop_var) {
5891
1.55k
    return true;
5892
1.55k
  }
5893
230k
  base = zend_stack_base(&CG(loop_var_stack));
5894
238k
  for (; loop_var >= base; loop_var--) {
5895
236k
    if (loop_var->opcode == ZEND_FAST_CALL) {
5896
3.23k
      zend_op *opline = get_next_op();
5897
5898
3.23k
      opline->opcode = ZEND_FAST_CALL;
5899
3.23k
      opline->result_type = IS_TMP_VAR;
5900
3.23k
      opline->result.var = loop_var->var_num;
5901
3.23k
      if (return_value) {
5902
800
        SET_NODE(opline->op2, return_value);
5903
800
      }
5904
3.23k
      opline->op1.num = loop_var->try_catch_offset;
5905
233k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5906
1.50k
      zend_op *opline = get_next_op();
5907
1.50k
      opline->opcode = ZEND_DISCARD_EXCEPTION;
5908
1.50k
      opline->op1_type = IS_TMP_VAR;
5909
1.50k
      opline->op1.var = loop_var->var_num;
5910
231k
    } else if (loop_var->opcode == ZEND_RETURN) {
5911
      /* Stack separator */
5912
226k
      break;
5913
226k
    } else if (depth <= 1) {
5914
2.06k
      return 1;
5915
3.14k
    } else if (loop_var->opcode == ZEND_NOP) {
5916
      /* Loop doesn't have freeable variable */
5917
1.63k
      depth--;
5918
1.63k
    } else {
5919
1.50k
      zend_op *opline;
5920
5921
1.50k
      ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR));
5922
1.50k
      opline = get_next_op();
5923
1.50k
      opline->opcode = loop_var->opcode;
5924
1.50k
      opline->op1_type = loop_var->var_type;
5925
1.50k
      opline->op1.var = loop_var->var_num;
5926
1.50k
      opline->extended_value = ZEND_FREE_ON_RETURN;
5927
1.50k
      depth--;
5928
1.50k
      }
5929
236k
  }
5930
228k
  return (depth == 0);
5931
230k
}
5932
/* }}} */
5933
5934
static bool zend_handle_loops_and_finally(znode *return_value) /* {{{ */
5935
230k
{
5936
230k
  return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value);
5937
230k
}
5938
/* }}} */
5939
5940
static bool zend_has_finally_ex(zend_long depth) /* {{{ */
5941
1.31k
{
5942
1.31k
  const zend_loop_var *base;
5943
1.31k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5944
5945
1.31k
  if (!loop_var) {
5946
234
    return false;
5947
234
  }
5948
1.08k
  base = zend_stack_base(&CG(loop_var_stack));
5949
2.17k
  for (; loop_var >= base; loop_var--) {
5950
2.08k
    if (loop_var->opcode == ZEND_FAST_CALL) {
5951
261
      return 1;
5952
1.82k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5953
993
    } else if (loop_var->opcode == ZEND_RETURN) {
5954
      /* Stack separator */
5955
727
      return 0;
5956
727
    } else if (depth <= 1) {
5957
0
      return 0;
5958
266
    } else {
5959
266
      depth--;
5960
266
      }
5961
2.08k
  }
5962
92
  return false;
5963
1.08k
}
5964
/* }}} */
5965
5966
static bool zend_has_finally(void) /* {{{ */
5967
1.31k
{
5968
1.31k
  return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1);
5969
1.31k
}
5970
/* }}} */
5971
5972
static void zend_compile_return(const zend_ast *ast) /* {{{ */
5973
227k
{
5974
227k
  zend_ast *expr_ast = ast->child[0];
5975
227k
  bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0;
5976
227k
  bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
5977
5978
227k
  znode expr_node;
5979
227k
  zend_op *opline;
5980
5981
227k
  if (is_generator) {
5982
    /* For generators the by-ref flag refers to yields, not returns */
5983
159k
    by_ref = false;
5984
159k
  }
5985
5986
227k
  if (!expr_ast) {
5987
1.83k
    expr_node.op_type = IS_CONST;
5988
1.83k
    ZVAL_NULL(&expr_node.u.constant);
5989
225k
  } else if (by_ref && zend_is_variable_or_call(expr_ast)) {
5990
33.6k
    zend_assert_not_short_circuited(expr_ast);
5991
33.6k
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
5992
192k
  } else {
5993
192k
    zend_compile_expr(&expr_node, expr_ast);
5994
192k
  }
5995
5996
227k
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
5997
2.08k
   && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
5998
1.31k
   && zend_has_finally()) {
5999
    /* Copy return value into temporary VAR to avoid modification in finally code */
6000
261
    if (by_ref) {
6001
113
      zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
6002
148
    } else {
6003
148
      zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL);
6004
148
    }
6005
261
  }
6006
6007
  /* Generator return types are handled separately */
6008
227k
  if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
6009
7.74k
    zend_emit_return_type_check(
6010
7.74k
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
6011
7.74k
  }
6012
6013
227k
  uint32_t opnum_before_finally = get_next_op_number();
6014
6015
227k
  zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
6016
6017
  /* Content of reference might have changed in finally, repeat type check. */
6018
227k
  if (by_ref
6019
   /* Check if any opcodes were emitted since the last return type check. */
6020
36.3k
   && opnum_before_finally != get_next_op_number()
6021
940
   && !is_generator
6022
940
   && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
6023
43
    zend_emit_return_type_check(
6024
43
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
6025
43
  }
6026
6027
227k
  opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
6028
227k
    &expr_node, NULL);
6029
6030
227k
  if (by_ref && expr_ast) {
6031
34.9k
    if (zend_is_call(expr_ast)) {
6032
31.7k
      opline->extended_value = ZEND_RETURNS_FUNCTION;
6033
31.7k
    } else if (!zend_is_variable(expr_ast) || zend_ast_is_short_circuited(expr_ast)) {
6034
1.25k
      opline->extended_value = ZEND_RETURNS_VALUE;
6035
1.25k
    }
6036
34.9k
  }
6037
227k
}
6038
/* }}} */
6039
6040
static void zend_compile_void_cast(znode *result, const zend_ast *ast)
6041
566
{
6042
566
  zend_ast *expr_ast = ast->child[0];
6043
566
  znode expr_node;
6044
566
  zend_op *opline;
6045
6046
566
  zend_compile_expr(&expr_node, expr_ast);
6047
6048
566
  switch (expr_node.op_type) {
6049
344
    case IS_TMP_VAR:
6050
344
    case IS_VAR:
6051
344
      opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6052
344
      opline->extended_value = ZEND_FREE_VOID_CAST;
6053
344
      break;
6054
143
    case IS_CONST:
6055
143
      zend_do_free(&expr_node);
6056
143
      break;
6057
566
  }
6058
566
}
6059
6060
static void zend_compile_echo(const zend_ast *ast) /* {{{ */
6061
2.48M
{
6062
2.48M
  zend_op *opline;
6063
2.48M
  zend_ast *expr_ast = ast->child[0];
6064
6065
2.48M
  znode expr_node;
6066
2.48M
  zend_compile_expr(&expr_node, expr_ast);
6067
6068
2.48M
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
6069
2.48M
  opline->extended_value = 0;
6070
2.48M
}
6071
/* }}} */
6072
6073
static void zend_compile_throw(znode *result, const zend_ast *ast) /* {{{ */
6074
3.50k
{
6075
3.50k
  zend_ast *expr_ast = ast->child[0];
6076
6077
3.50k
  znode expr_node;
6078
3.50k
  zend_compile_expr(&expr_node, expr_ast);
6079
6080
3.50k
  zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
6081
3.50k
  if (result) {
6082
    /* Mark this as an "expression throw" for opcache. */
6083
750
    opline->extended_value = ZEND_THROW_IS_EXPR;
6084
750
    result->op_type = IS_CONST;
6085
750
    ZVAL_TRUE(&result->u.constant);
6086
750
  }
6087
3.50k
}
6088
/* }}} */
6089
6090
static void zend_compile_break_continue(const zend_ast *ast) /* {{{ */
6091
2.20k
{
6092
2.20k
  zend_ast *depth_ast = ast->child[0];
6093
6094
2.20k
  zend_op *opline;
6095
2.20k
  zend_long depth;
6096
6097
2.20k
  ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
6098
6099
2.20k
  if (depth_ast) {
6100
338
    const zval *depth_zv;
6101
338
    if (depth_ast->kind != ZEND_AST_ZVAL) {
6102
15
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand "
6103
15
        "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
6104
15
    }
6105
6106
323
    depth_zv = zend_ast_get_zval(depth_ast);
6107
323
    if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
6108
11
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers",
6109
11
        ast->kind == ZEND_AST_BREAK ? "break" : "continue");
6110
11
    }
6111
6112
312
    depth = Z_LVAL_P(depth_zv);
6113
1.86k
  } else {
6114
1.86k
    depth = 1;
6115
1.86k
  }
6116
6117
2.17k
  if (CG(context).current_brk_cont == -1) {
6118
30
    zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
6119
30
      ast->kind == ZEND_AST_BREAK ? "break" : "continue");
6120
2.14k
  } else {
6121
2.14k
    if (!zend_handle_loops_and_finally_ex(depth, NULL)) {
6122
87
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s",
6123
87
        ast->kind == ZEND_AST_BREAK ? "break" : "continue",
6124
87
        depth, depth == 1 ? "" : "s");
6125
87
    }
6126
2.14k
  }
6127
6128
2.06k
  if (ast->kind == ZEND_AST_CONTINUE) {
6129
1.14k
    int d, cur = CG(context).current_brk_cont;
6130
1.23k
    for (d = depth - 1; d > 0; d--) {
6131
89
      cur = CG(context).brk_cont_array[cur].parent;
6132
89
      ZEND_ASSERT(cur != -1);
6133
89
    }
6134
6135
1.14k
    if (CG(context).brk_cont_array[cur].is_switch) {
6136
638
      if (depth == 1) {
6137
589
        if (CG(context).brk_cont_array[cur].parent == -1) {
6138
229
          zend_error(E_WARNING,
6139
229
            "\"continue\" targeting switch is equivalent to \"break\"");
6140
360
        } else {
6141
360
          zend_error(E_WARNING,
6142
360
            "\"continue\" targeting switch is equivalent to \"break\". " \
6143
360
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
6144
360
            depth + 1);
6145
360
        }
6146
589
      } else {
6147
49
        if (CG(context).brk_cont_array[cur].parent == -1) {
6148
37
          zend_error(E_WARNING,
6149
37
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"",
6150
37
            depth, depth);
6151
37
        } else {
6152
12
          zend_error(E_WARNING,
6153
12
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
6154
12
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
6155
12
            depth, depth, depth + 1);
6156
12
        }
6157
49
      }
6158
638
    }
6159
1.14k
  }
6160
6161
2.06k
  opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
6162
2.06k
  opline->op1.num = CG(context).current_brk_cont;
6163
2.06k
  opline->op2.num = depth;
6164
2.06k
}
6165
/* }}} */
6166
6167
void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */
6168
2.30k
{
6169
2.30k
  zend_label *dest;
6170
2.30k
  int remove_oplines = opline->op1.num;
6171
2.30k
  zval *label;
6172
2.30k
  uint32_t opnum = opline - op_array->opcodes;
6173
6174
2.30k
  label = CT_CONSTANT_EX(op_array, opline->op2.constant);
6175
2.30k
  if (CG(context).labels == NULL ||
6176
2.28k
      (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL
6177
2.30k
  ) {
6178
59
    CG(in_compilation) = 1;
6179
59
    CG(active_op_array) = op_array;
6180
59
    CG(zend_lineno) = opline->lineno;
6181
59
    zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
6182
59
  }
6183
6184
2.24k
  zval_ptr_dtor_str(label);
6185
2.24k
  ZVAL_NULL(label);
6186
6187
2.24k
  uint32_t current = opline->extended_value;
6188
3.70k
  for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) {
6189
1.47k
    if (current == -1) {
6190
15
      CG(in_compilation) = 1;
6191
15
      CG(active_op_array) = op_array;
6192
15
      CG(zend_lineno) = opline->lineno;
6193
15
      zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
6194
15
    }
6195
1.46k
    if (CG(context).brk_cont_array[current].start >= 0) {
6196
936
      remove_oplines--;
6197
936
    }
6198
1.46k
  }
6199
6200
5.24k
  for (current = 0; current < op_array->last_try_catch; ++current) {
6201
4.01k
    const zend_try_catch_element *elem = &op_array->try_catch_array[current];
6202
4.01k
    if (elem->try_op > opnum) {
6203
999
      break;
6204
999
    }
6205
3.01k
    if (elem->finally_op && opnum < elem->finally_op - 1
6206
1.96k
      && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op)
6207
3.01k
    ) {
6208
946
      remove_oplines--;
6209
946
    }
6210
3.01k
  }
6211
6212
2.23k
  opline->opcode = ZEND_JMP;
6213
2.23k
  SET_UNUSED(opline->op1);
6214
2.23k
  SET_UNUSED(opline->op2);
6215
2.23k
  SET_UNUSED(opline->result);
6216
2.23k
  opline->op1.opline_num = dest->opline_num;
6217
2.23k
  opline->extended_value = 0;
6218
6219
2.23k
  ZEND_ASSERT(remove_oplines >= 0);
6220
3.30k
  while (remove_oplines--) {
6221
1.07k
    opline--;
6222
1.07k
    MAKE_NOP(opline);
6223
1.07k
    ZEND_VM_SET_OPCODE_HANDLER(opline);
6224
1.07k
  }
6225
2.23k
}
6226
/* }}} */
6227
6228
static void zend_compile_goto(const zend_ast *ast) /* {{{ */
6229
2.54k
{
6230
2.54k
  zend_ast *label_ast = ast->child[0];
6231
2.54k
  znode label_node;
6232
2.54k
  zend_op *opline;
6233
6234
2.54k
  zend_compile_expr(&label_node, label_ast);
6235
6236
  /* Label resolution and unwinding adjustments happen in pass two. */
6237
2.54k
  uint32_t opnum_start = get_next_op_number();
6238
2.54k
  zend_handle_loops_and_finally(NULL);
6239
2.54k
  opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
6240
2.54k
  opline->op1.num = get_next_op_number() - opnum_start - 1;
6241
2.54k
  opline->extended_value = CG(context).current_brk_cont;
6242
2.54k
}
6243
/* }}} */
6244
6245
static void zend_compile_label(const zend_ast *ast) /* {{{ */
6246
3.09k
{
6247
3.09k
  zend_string *label = zend_ast_get_str(ast->child[0]);
6248
3.09k
  zend_label dest;
6249
6250
3.09k
  if (!CG(context).labels) {
6251
2.06k
    ALLOC_HASHTABLE(CG(context).labels);
6252
2.06k
    zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0);
6253
2.06k
  }
6254
6255
3.09k
  dest.brk_cont = CG(context).current_brk_cont;
6256
3.09k
  dest.opline_num = get_next_op_number();
6257
6258
3.09k
  if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
6259
41
    zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
6260
41
  }
6261
3.09k
}
6262
/* }}} */
6263
6264
static void zend_compile_while(const zend_ast *ast) /* {{{ */
6265
6.42k
{
6266
6.42k
  zend_ast *cond_ast = ast->child[0];
6267
6.42k
  zend_ast *stmt_ast = ast->child[1];
6268
6.42k
  znode cond_node;
6269
6.42k
  uint32_t opnum_start, opnum_jmp, opnum_cond;
6270
6271
6.42k
  opnum_jmp = zend_emit_jump(0);
6272
6273
6.42k
  zend_begin_loop(ZEND_NOP, NULL, false);
6274
6275
6.42k
  opnum_start = get_next_op_number();
6276
6.42k
  zend_compile_stmt(stmt_ast);
6277
6278
6.42k
  opnum_cond = get_next_op_number();
6279
6.42k
  zend_update_jump_target(opnum_jmp, opnum_cond);
6280
6.42k
  zend_compile_expr(&cond_node, cond_ast);
6281
6282
6.42k
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6283
6284
6.42k
  zend_end_loop(opnum_cond, NULL);
6285
6.42k
}
6286
/* }}} */
6287
6288
static void zend_compile_do_while(const zend_ast *ast) /* {{{ */
6289
6.24k
{
6290
6.24k
  zend_ast *stmt_ast = ast->child[0];
6291
6.24k
  zend_ast *cond_ast = ast->child[1];
6292
6293
6.24k
  znode cond_node;
6294
6.24k
  uint32_t opnum_start, opnum_cond;
6295
6296
6.24k
  zend_begin_loop(ZEND_NOP, NULL, false);
6297
6298
6.24k
  opnum_start = get_next_op_number();
6299
6.24k
  zend_compile_stmt(stmt_ast);
6300
6301
6.24k
  opnum_cond = get_next_op_number();
6302
6.24k
  zend_compile_expr(&cond_node, cond_ast);
6303
6304
6.24k
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6305
6306
6.24k
  zend_end_loop(opnum_cond, NULL);
6307
6.24k
}
6308
/* }}} */
6309
6310
static void zend_compile_for_expr_list(znode *result, zend_ast *ast) /* {{{ */
6311
36.5k
{
6312
36.5k
  const zend_ast_list *list;
6313
36.5k
  uint32_t i;
6314
6315
36.5k
  result->op_type = IS_CONST;
6316
36.5k
  ZVAL_TRUE(&result->u.constant);
6317
6318
36.5k
  if (!ast) {
6319
8.55k
    return;
6320
8.55k
  }
6321
6322
27.9k
  list = zend_ast_get_list(ast);
6323
58.2k
  for (i = 0; i < list->children; ++i) {
6324
30.2k
    zend_ast *expr_ast = list->child[i];
6325
6326
30.2k
    zend_do_free(result);
6327
30.2k
    if (expr_ast->kind == ZEND_AST_CAST_VOID) {
6328
138
      zend_compile_void_cast(NULL, expr_ast);
6329
138
      result->op_type = IS_CONST;
6330
138
      ZVAL_NULL(&result->u.constant);
6331
30.1k
    } else {
6332
30.1k
      zend_compile_expr(result, expr_ast);
6333
30.1k
    }
6334
30.2k
  }
6335
27.9k
}
6336
/* }}} */
6337
6338
static void zend_compile_for(const zend_ast *ast) /* {{{ */
6339
12.2k
{
6340
12.2k
  zend_ast *init_ast = ast->child[0];
6341
12.2k
  zend_ast *cond_ast = ast->child[1];
6342
12.2k
  zend_ast *loop_ast = ast->child[2];
6343
12.2k
  zend_ast *stmt_ast = ast->child[3];
6344
6345
12.2k
  znode result;
6346
12.2k
  uint32_t opnum_start, opnum_jmp, opnum_loop;
6347
6348
12.2k
  zend_compile_for_expr_list(&result, init_ast);
6349
12.2k
  zend_do_free(&result);
6350
6351
12.2k
  opnum_jmp = zend_emit_jump(0);
6352
6353
12.2k
  zend_begin_loop(ZEND_NOP, NULL, false);
6354
6355
12.2k
  opnum_start = get_next_op_number();
6356
12.2k
  zend_compile_stmt(stmt_ast);
6357
6358
12.2k
  opnum_loop = get_next_op_number();
6359
12.2k
  zend_compile_for_expr_list(&result, loop_ast);
6360
12.2k
  zend_do_free(&result);
6361
6362
12.2k
  zend_update_jump_target_to_next(opnum_jmp);
6363
12.2k
  zend_compile_for_expr_list(&result, cond_ast);
6364
12.2k
  zend_do_extended_stmt(NULL);
6365
6366
12.2k
  zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
6367
6368
12.2k
  zend_end_loop(opnum_loop, NULL);
6369
12.2k
}
6370
/* }}} */
6371
6372
static void zend_compile_foreach(zend_ast *ast) /* {{{ */
6373
16.7k
{
6374
16.7k
  zend_ast *expr_ast = ast->child[0];
6375
16.7k
  zend_ast *value_ast = ast->child[1];
6376
16.7k
  zend_ast *key_ast = ast->child[2];
6377
16.7k
  zend_ast *stmt_ast = ast->child[3];
6378
16.7k
  bool by_ref = value_ast->kind == ZEND_AST_REF;
6379
16.7k
  bool is_variable = (zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast))
6380
3.26k
    || zend_is_call(expr_ast);
6381
6382
16.7k
  znode expr_node, reset_node, value_node, key_node;
6383
16.7k
  zend_op *opline;
6384
16.7k
  uint32_t opnum_reset, opnum_fetch;
6385
6386
16.7k
  if (key_ast) {
6387
1.89k
    if (key_ast->kind == ZEND_AST_REF) {
6388
6
      zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
6389
6
    }
6390
1.89k
    if (key_ast->kind == ZEND_AST_ARRAY) {
6391
11
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
6392
11
    }
6393
1.89k
  }
6394
6395
16.7k
  if (by_ref) {
6396
1.40k
    value_ast = value_ast->child[0];
6397
1.40k
  }
6398
6399
16.7k
  if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
6400
121
    by_ref = true;
6401
121
  }
6402
6403
16.7k
  if (by_ref && is_variable) {
6404
1.24k
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
6405
15.4k
  } else {
6406
15.4k
    zend_compile_expr(&expr_node, expr_ast);
6407
15.4k
  }
6408
6409
16.7k
  if (by_ref) {
6410
1.52k
    zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
6411
1.52k
  }
6412
6413
16.7k
  opnum_reset = get_next_op_number();
6414
16.7k
  opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
6415
16.7k
  if (!by_ref) {
6416
15.1k
    opline->result_type = IS_TMP_VAR;
6417
15.1k
    reset_node.op_type = IS_TMP_VAR;
6418
15.1k
  }
6419
6420
16.7k
  zend_begin_loop(ZEND_FE_FREE, &reset_node, false);
6421
6422
16.7k
  opnum_fetch = get_next_op_number();
6423
16.7k
  opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
6424
6425
16.7k
  if (is_this_fetch(value_ast)) {
6426
8
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6427
16.7k
  } else if (value_ast->kind == ZEND_AST_VAR &&
6428
16.1k
    zend_try_compile_cv(&value_node, value_ast, BP_VAR_R) == SUCCESS) {
6429
16.0k
    SET_NODE(opline->op2, &value_node);
6430
16.0k
  } else {
6431
680
    opline->op2_type = by_ref ? IS_VAR : IS_TMP_VAR;
6432
680
    opline->op2.var = get_temporary_variable();
6433
680
    GET_NODE(&value_node, opline->op2);
6434
680
    if (value_ast->kind == ZEND_AST_ARRAY) {
6435
340
      zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr, BP_VAR_R);
6436
340
    } else if (by_ref) {
6437
135
      zend_emit_assign_ref_znode(value_ast, &value_node);
6438
205
    } else {
6439
205
      zend_emit_assign_znode(value_ast, &value_node);
6440
205
    }
6441
680
  }
6442
6443
16.7k
  if (key_ast) {
6444
1.87k
    opline = &CG(active_op_array)->opcodes[opnum_fetch];
6445
1.87k
    zend_make_tmp_result(&key_node, opline);
6446
1.87k
    zend_emit_assign_znode(key_ast, &key_node);
6447
1.87k
  }
6448
6449
16.7k
  zend_compile_stmt(stmt_ast);
6450
6451
  /* Place JMP and FE_FREE on the line where foreach starts. It would be
6452
   * better to use the end line, but this information is not available
6453
   * currently. */
6454
16.7k
  CG(zend_lineno) = ast->lineno;
6455
16.7k
  zend_emit_jump(opnum_fetch);
6456
6457
16.7k
  opline = &CG(active_op_array)->opcodes[opnum_reset];
6458
16.7k
  opline->op2.opline_num = get_next_op_number();
6459
6460
16.7k
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
6461
16.7k
  opline->extended_value = get_next_op_number();
6462
6463
16.7k
  zend_end_loop(opnum_fetch, &reset_node);
6464
6465
16.7k
  opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
6466
16.7k
}
6467
/* }}} */
6468
6469
static void zend_compile_if(zend_ast *ast) /* {{{ */
6470
69.9k
{
6471
69.9k
  const zend_ast_list *list = zend_ast_get_list(ast);
6472
69.9k
  uint32_t i;
6473
69.9k
  uint32_t *jmp_opnums = NULL;
6474
6475
69.9k
  if (list->children > 1) {
6476
19.3k
    jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
6477
19.3k
  }
6478
6479
174k
  for (i = 0; i < list->children; ++i) {
6480
104k
    const zend_ast *elem_ast = list->child[i];
6481
104k
    zend_ast *cond_ast = elem_ast->child[0];
6482
104k
    zend_ast *stmt_ast = elem_ast->child[1];
6483
6484
104k
    if (cond_ast) {
6485
86.6k
      znode cond_node;
6486
86.6k
      uint32_t opnum_jmpz;
6487
6488
86.6k
      if (i > 0) {
6489
16.7k
        CG(zend_lineno) = cond_ast->lineno;
6490
16.7k
        zend_do_extended_stmt(NULL);
6491
16.7k
      }
6492
6493
86.6k
      zend_compile_expr(&cond_node, cond_ast);
6494
86.6k
      opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6495
6496
86.6k
      zend_compile_stmt(stmt_ast);
6497
6498
86.6k
      if (i != list->children - 1) {
6499
        /* Set the lineno of JMP to the position of the if keyword, as we don't want to
6500
         * report the last line in the if branch as covered if it hasn't actually executed. */
6501
34.7k
        CG(zend_lineno) = elem_ast->lineno;
6502
34.7k
        jmp_opnums[i] = zend_emit_jump(0);
6503
34.7k
      }
6504
86.6k
      zend_update_jump_target_to_next(opnum_jmpz);
6505
86.6k
    } else {
6506
      /* "else" can only occur as last element. */
6507
17.9k
      ZEND_ASSERT(i == list->children - 1);
6508
17.9k
      zend_compile_stmt(stmt_ast);
6509
17.9k
    }
6510
104k
  }
6511
6512
69.9k
  if (list->children > 1) {
6513
54.0k
    for (i = 0; i < list->children - 1; ++i) {
6514
34.6k
      zend_update_jump_target_to_next(jmp_opnums[i]);
6515
34.6k
    }
6516
19.3k
    efree(jmp_opnums);
6517
19.3k
  }
6518
69.9k
}
6519
/* }}} */
6520
6521
10.9k
static uint8_t determine_switch_jumptable_type(const zend_ast_list *cases) {
6522
10.9k
  uint32_t i;
6523
10.9k
  uint8_t common_type = IS_UNDEF;
6524
19.6k
  for (i = 0; i < cases->children; i++) {
6525
17.2k
    zend_ast *case_ast = cases->child[i];
6526
17.2k
    zend_ast **cond_ast = &case_ast->child[0];
6527
17.2k
    const zval *cond_zv;
6528
17.2k
    if (!case_ast->child[0]) {
6529
      /* Skip default clause */
6530
257
      continue;
6531
257
    }
6532
6533
17.0k
    zend_eval_const_expr(cond_ast);
6534
17.0k
    if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6535
      /* Non-constant case */
6536
6.12k
      return IS_UNDEF;
6537
6.12k
    }
6538
6539
10.8k
    cond_zv = zend_ast_get_zval(case_ast->child[0]);
6540
10.8k
    if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6541
      /* We only optimize switched on integers and strings */
6542
2.17k
      return IS_UNDEF;
6543
2.17k
    }
6544
6545
8.70k
    if (common_type == IS_UNDEF) {
6546
4.41k
      common_type = Z_TYPE_P(cond_zv);
6547
4.41k
    } else if (common_type != Z_TYPE_P(cond_zv)) {
6548
      /* Non-uniform case types */
6549
22
      return IS_UNDEF;
6550
22
    }
6551
6552
8.68k
    if (Z_TYPE_P(cond_zv) == IS_STRING
6553
8.16k
        && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) {
6554
      /* Numeric strings cannot be compared with a simple hash lookup */
6555
221
      return IS_UNDEF;
6556
221
    }
6557
8.68k
  }
6558
6559
2.36k
  return common_type;
6560
10.9k
}
6561
6562
2.05k
static bool should_use_jumptable(const zend_ast_list *cases, uint8_t jumptable_type) {
6563
2.05k
  if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) {
6564
0
    return false;
6565
0
  }
6566
6567
  /* Thresholds are chosen based on when the average switch time for equidistributed
6568
   * input becomes smaller when using the jumptable optimization. */
6569
2.05k
  if (jumptable_type == IS_LONG) {
6570
175
    return cases->children >= 5;
6571
1.88k
  } else {
6572
1.88k
    ZEND_ASSERT(jumptable_type == IS_STRING);
6573
1.88k
    return cases->children >= 2;
6574
1.88k
  }
6575
2.05k
}
6576
6577
static void zend_compile_switch(zend_ast *ast) /* {{{ */
6578
10.9k
{
6579
10.9k
  zend_ast *expr_ast = ast->child[0];
6580
10.9k
  zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
6581
6582
10.9k
  uint32_t i;
6583
10.9k
  bool has_default_case = false;
6584
6585
10.9k
  znode expr_node, case_node;
6586
10.9k
  zend_op *opline;
6587
10.9k
  uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1;
6588
10.9k
  uint8_t jumptable_type;
6589
10.9k
  HashTable *jumptable = NULL;
6590
6591
10.9k
  zend_compile_expr(&expr_node, expr_ast);
6592
6593
10.9k
  zend_begin_loop(ZEND_FREE, &expr_node, true);
6594
6595
10.9k
  case_node.op_type = IS_TMP_VAR;
6596
10.9k
  case_node.u.op.var = get_temporary_variable();
6597
6598
10.9k
  jumptable_type = determine_switch_jumptable_type(cases);
6599
10.9k
  if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) {
6600
1.72k
    znode jumptable_op;
6601
6602
1.72k
    ALLOC_HASHTABLE(jumptable);
6603
1.72k
    zend_hash_init(jumptable, cases->children, NULL, NULL, 0);
6604
1.72k
    jumptable_op.op_type = IS_CONST;
6605
1.72k
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6606
6607
1.72k
    opline = zend_emit_op(NULL,
6608
1.72k
      jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING,
6609
1.72k
      &expr_node, &jumptable_op);
6610
1.72k
    if (opline->op1_type == IS_CONST) {
6611
846
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6612
846
    }
6613
1.72k
    opnum_switch = opline - CG(active_op_array)->opcodes;
6614
1.72k
  }
6615
6616
10.9k
  jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
6617
44.8k
  for (i = 0; i < cases->children; ++i) {
6618
33.9k
    zend_ast *case_ast = cases->child[i];
6619
33.9k
    zend_ast *cond_ast = case_ast->child[0];
6620
33.9k
    znode cond_node;
6621
6622
33.9k
    if (case_ast->attr == ZEND_ALT_CASE_SYNTAX) {
6623
133
      CG(zend_lineno) = case_ast->lineno;
6624
133
      zend_error(E_DEPRECATED, "Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead");
6625
133
    }
6626
6627
33.9k
    if (!cond_ast) {
6628
280
      if (has_default_case) {
6629
16
        CG(zend_lineno) = case_ast->lineno;
6630
16
        zend_error_noreturn(E_COMPILE_ERROR,
6631
16
          "Switch statements may only contain one default clause");
6632
16
      }
6633
264
      has_default_case = true;
6634
264
      continue;
6635
280
    }
6636
6637
33.7k
    zend_compile_expr(&cond_node, cond_ast);
6638
6639
33.7k
    if (expr_node.op_type == IS_CONST
6640
28.9k
      && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
6641
13.4k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6642
20.2k
    } else if (expr_node.op_type == IS_CONST
6643
15.5k
      && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
6644
9.09k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
6645
11.1k
    } else {
6646
11.1k
      opline = zend_emit_op(NULL,
6647
11.1k
        (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL,
6648
11.1k
        &expr_node, &cond_node);
6649
11.1k
      SET_NODE(opline->result, &case_node);
6650
11.1k
      if (opline->op1_type == IS_CONST) {
6651
6.44k
        Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6652
6.44k
      }
6653
6654
11.1k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6655
11.1k
    }
6656
33.7k
  }
6657
6658
10.8k
  opnum_default_jmp = zend_emit_jump(0);
6659
6660
44.8k
  for (i = 0; i < cases->children; ++i) {
6661
33.9k
    zend_ast *case_ast = cases->child[i];
6662
33.9k
    zend_ast *cond_ast = case_ast->child[0];
6663
33.9k
    zend_ast *stmt_ast = case_ast->child[1];
6664
6665
33.9k
    if (cond_ast) {
6666
33.6k
      zend_update_jump_target_to_next(jmpnz_opnums[i]);
6667
6668
33.6k
      if (jumptable) {
6669
4.32k
        zval *cond_zv = zend_ast_get_zval(cond_ast);
6670
4.32k
        zval jmp_target;
6671
4.32k
        ZVAL_LONG(&jmp_target, get_next_op_number());
6672
6673
4.32k
        ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type);
6674
4.32k
        if (Z_TYPE_P(cond_zv) == IS_LONG) {
6675
177
          zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6676
4.15k
        } else {
6677
4.15k
          ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6678
4.15k
          zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6679
4.15k
        }
6680
4.32k
      }
6681
33.6k
    } else {
6682
248
      zend_update_jump_target_to_next(opnum_default_jmp);
6683
6684
248
      if (jumptable) {
6685
44
        ZEND_ASSERT(opnum_switch != (uint32_t)-1);
6686
44
        opline = &CG(active_op_array)->opcodes[opnum_switch];
6687
44
        opline->extended_value = get_next_op_number();
6688
44
      }
6689
248
    }
6690
6691
33.9k
    zend_compile_stmt(stmt_ast);
6692
33.9k
  }
6693
6694
10.8k
  if (!has_default_case) {
6695
10.6k
    zend_update_jump_target_to_next(opnum_default_jmp);
6696
6697
10.6k
    if (jumptable) {
6698
1.66k
      opline = &CG(active_op_array)->opcodes[opnum_switch];
6699
1.66k
      opline->extended_value = get_next_op_number();
6700
1.66k
    }
6701
10.6k
  }
6702
6703
10.8k
  zend_end_loop(get_next_op_number(), &expr_node);
6704
6705
10.8k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
6706
1.45k
    opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6707
1.45k
    opline->extended_value = ZEND_FREE_SWITCH;
6708
9.43k
  } else if (expr_node.op_type == IS_CONST) {
6709
8.70k
    zval_ptr_dtor_nogc(&expr_node.u.constant);
6710
8.70k
  }
6711
6712
10.8k
  efree(jmpnz_opnums);
6713
10.8k
}
6714
/* }}} */
6715
6716
static uint32_t count_match_conds(const zend_ast_list *arms)
6717
2.95k
{
6718
2.95k
  uint32_t num_conds = 0;
6719
6720
7.28k
  for (uint32_t i = 0; i < arms->children; i++) {
6721
4.33k
    const zend_ast *arm_ast = arms->child[i];
6722
4.33k
    if (arm_ast->child[0] == NULL) {
6723
607
      continue;
6724
607
    }
6725
6726
3.72k
    const zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6727
3.72k
    num_conds += conds->children;
6728
3.72k
  }
6729
6730
2.95k
  return num_conds;
6731
2.95k
}
6732
6733
2.95k
static bool can_match_use_jumptable(const zend_ast_list *arms) {
6734
5.31k
  for (uint32_t i = 0; i < arms->children; i++) {
6735
3.70k
    const zend_ast *arm_ast = arms->child[i];
6736
3.70k
    if (!arm_ast->child[0]) {
6737
      /* Skip default arm */
6738
480
      continue;
6739
480
    }
6740
6741
3.22k
    zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6742
7.36k
    for (uint32_t j = 0; j < conds->children; j++) {
6743
5.48k
      zend_ast **cond_ast = &conds->child[j];
6744
6745
5.48k
      zend_eval_const_expr(cond_ast);
6746
5.48k
      if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6747
908
        return false;
6748
908
      }
6749
6750
4.57k
      const zval *cond_zv = zend_ast_get_zval(*cond_ast);
6751
4.57k
      if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6752
432
        return false;
6753
432
      }
6754
4.57k
    }
6755
3.22k
  }
6756
6757
1.61k
  return true;
6758
2.95k
}
6759
6760
static bool zend_is_pipe_optimizable_callable_name(zend_ast *ast)
6761
662
{
6762
662
  if (ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(ast)) == IS_STRING) {
6763
    /* Assert compilation adds a message operand, but this is incompatible with the
6764
     * pipe optimization that uses a temporary znode for the reference elimination.
6765
     * Therefore, disable the optimization for assert.
6766
     * Note that "assert" as a name is always treated as fully qualified. */
6767
423
    return !zend_string_equals_literal_ci(zend_ast_get_str(ast), "assert");
6768
423
  }
6769
6770
239
  return true;
6771
662
}
6772
6773
static void zend_compile_pipe(znode *result, zend_ast *ast, uint32_t type)
6774
30.6k
{
6775
30.6k
  zend_ast *operand_ast = ast->child[0];
6776
30.6k
  zend_ast *callable_ast = ast->child[1];
6777
6778
30.6k
  if (callable_ast->kind == ZEND_AST_ARROW_FUNC && !(callable_ast->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) {
6779
7
    zend_error_noreturn(E_COMPILE_ERROR, "Arrow functions on the right hand side of |> must be parenthesized");
6780
7
  }
6781
6782
  /* Compile the left hand side down to a value first. */
6783
30.6k
  znode operand_result;
6784
30.6k
  zend_compile_expr(&operand_result, operand_ast);
6785
6786
  /* Wrap simple values in a ZEND_QM_ASSIGN opcode to ensure references
6787
   * always fail. They will already fail in complex cases like arrays,
6788
   * so those don't need a wrapper. */
6789
30.6k
  znode wrapped_operand_result;
6790
30.6k
  if (operand_result.op_type & (IS_CV|IS_VAR)) {
6791
111
    zend_emit_op_tmp(&wrapped_operand_result, ZEND_QM_ASSIGN, &operand_result, NULL);
6792
30.5k
  } else {
6793
30.5k
    wrapped_operand_result = operand_result;
6794
30.5k
  }
6795
6796
  /* Turn the operand into a function parameter list. */
6797
30.6k
  zend_ast *arg_list_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&wrapped_operand_result));
6798
6799
30.6k
  zend_ast *fcall_ast;
6800
30.6k
  znode callable_result;
6801
6802
  /* Turn $foo |> bar(...) into bar($foo). */
6803
30.6k
  if (callable_ast->kind == ZEND_AST_CALL
6804
894
    && callable_ast->child[1]->kind == ZEND_AST_CALLABLE_CONVERT
6805
662
    && zend_is_pipe_optimizable_callable_name(callable_ast->child[0])) {
6806
623
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6807
623
        callable_ast->child[0], arg_list_ast);
6808
  /* Turn $foo |> bar::baz(...) into bar::baz($foo). */
6809
30.0k
  } else if (callable_ast->kind == ZEND_AST_STATIC_CALL
6810
320
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6811
244
    fcall_ast = zend_ast_create(ZEND_AST_STATIC_CALL,
6812
244
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6813
  /* Turn $foo |> $bar->baz(...) into $bar->baz($foo). */
6814
29.8k
  } else if (callable_ast->kind == ZEND_AST_METHOD_CALL
6815
1.06k
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6816
309
    fcall_ast = zend_ast_create(ZEND_AST_METHOD_CALL,
6817
309
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6818
  /* Turn $foo |> $expr into ($expr)($foo) */
6819
29.5k
  } else {
6820
29.5k
    zend_compile_expr(&callable_result, callable_ast);
6821
29.5k
    callable_ast = zend_ast_create_znode(&callable_result);
6822
29.5k
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6823
29.5k
      callable_ast, arg_list_ast);
6824
29.5k
  }
6825
6826
30.6k
  zend_do_extended_stmt(&operand_result);
6827
6828
30.6k
  zend_compile_var(result, fcall_ast, type, /* by_ref */ false);
6829
30.6k
}
6830
6831
static void zend_compile_match(znode *result, zend_ast *ast)
6832
2.95k
{
6833
2.95k
  zend_ast *expr_ast = ast->child[0];
6834
2.95k
  zend_ast_list *arms = zend_ast_get_list(ast->child[1]);
6835
2.95k
  bool has_default_arm = false;
6836
2.95k
  uint32_t opnum_match = (uint32_t)-1;
6837
6838
2.95k
  znode expr_node;
6839
2.95k
  zend_compile_expr(&expr_node, expr_ast);
6840
6841
2.95k
  znode case_node;
6842
2.95k
  case_node.op_type = IS_TMP_VAR;
6843
2.95k
  case_node.u.op.var = get_temporary_variable();
6844
6845
2.95k
  uint32_t num_conds = count_match_conds(arms);
6846
2.95k
  uint8_t can_use_jumptable = can_match_use_jumptable(arms);
6847
2.95k
  bool uses_jumptable = can_use_jumptable && num_conds >= 2;
6848
2.95k
  HashTable *jumptable = NULL;
6849
2.95k
  uint32_t *jmpnz_opnums = NULL;
6850
6851
7.27k
  for (uint32_t i = 0; i < arms->children; ++i) {
6852
4.33k
    zend_ast *arm_ast = arms->child[i];
6853
6854
4.33k
    if (!arm_ast->child[0]) {
6855
607
      if (has_default_arm) {
6856
5
        CG(zend_lineno) = arm_ast->lineno;
6857
5
        zend_error_noreturn(E_COMPILE_ERROR,
6858
5
          "Match expressions may only contain one default arm");
6859
5
      }
6860
602
      has_default_arm = true;
6861
602
    }
6862
4.33k
  }
6863
6864
2.94k
  if (uses_jumptable) {
6865
1.09k
    znode jumptable_op;
6866
6867
1.09k
    ALLOC_HASHTABLE(jumptable);
6868
1.09k
    zend_hash_init(jumptable, num_conds, NULL, NULL, 0);
6869
1.09k
    jumptable_op.op_type = IS_CONST;
6870
1.09k
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6871
6872
1.09k
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH, &expr_node, &jumptable_op);
6873
1.09k
    if (opline->op1_type == IS_CONST) {
6874
376
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6875
376
    }
6876
1.09k
    opnum_match = opline - CG(active_op_array)->opcodes;
6877
1.84k
  } else {
6878
1.84k
    jmpnz_opnums = safe_emalloc(sizeof(uint32_t), num_conds, 0);
6879
1.84k
    uint32_t cond_count = 0;
6880
4.42k
    for (uint32_t i = 0; i < arms->children; ++i) {
6881
2.57k
      zend_ast *arm_ast = arms->child[i];
6882
6883
2.57k
      if (!arm_ast->child[0]) {
6884
456
        continue;
6885
456
      }
6886
6887
2.11k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6888
5.77k
      for (uint32_t j = 0; j < conds->children; j++) {
6889
3.66k
        zend_ast *cond_ast = conds->child[j];
6890
6891
3.66k
        znode cond_node;
6892
3.66k
        zend_compile_expr(&cond_node, cond_ast);
6893
6894
3.66k
        uint32_t opcode = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE_STRICT : ZEND_IS_IDENTICAL;
6895
3.66k
        zend_op *opline = zend_emit_op(NULL, opcode, &expr_node, &cond_node);
6896
3.66k
        SET_NODE(opline->result, &case_node);
6897
3.66k
        if (opline->op1_type == IS_CONST) {
6898
1.97k
          Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6899
1.97k
        }
6900
6901
3.66k
        jmpnz_opnums[cond_count] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6902
6903
3.66k
        cond_count++;
6904
3.66k
      }
6905
2.11k
    }
6906
1.84k
  }
6907
6908
2.94k
  uint32_t opnum_default_jmp = 0;
6909
2.94k
  if (!uses_jumptable) {
6910
1.84k
    opnum_default_jmp = zend_emit_jump(0);
6911
1.84k
  }
6912
6913
2.94k
  bool is_first_case = true;
6914
2.94k
  uint32_t cond_count = 0;
6915
2.94k
  uint32_t *jmp_end_opnums = safe_emalloc(sizeof(uint32_t), arms->children, 0);
6916
6917
  // The generated default arm is emitted first to avoid live range issues where the tmpvar
6918
  // for the arm result is freed even though it has not been initialized yet.
6919
2.94k
  if (!has_default_arm) {
6920
2.34k
    if (!uses_jumptable) {
6921
1.39k
      zend_update_jump_target_to_next(opnum_default_jmp);
6922
1.39k
    }
6923
6924
2.34k
    if (jumptable) {
6925
957
      zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6926
957
      opline->extended_value = get_next_op_number();
6927
957
    }
6928
6929
2.34k
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH_ERROR, &expr_node, NULL);
6930
2.34k
    if (opline->op1_type == IS_CONST) {
6931
862
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6932
862
    }
6933
2.34k
    if (arms->children == 0) {
6934
      /* Mark this as an "expression throw" for opcache. */
6935
152
      opline->extended_value = ZEND_THROW_IS_EXPR;
6936
152
    }
6937
2.34k
  }
6938
6939
7.25k
  for (uint32_t i = 0; i < arms->children; ++i) {
6940
4.30k
    zend_ast *arm_ast = arms->child[i];
6941
4.30k
    zend_ast *body_ast = arm_ast->child[1];
6942
6943
4.30k
    if (arm_ast->child[0] != NULL) {
6944
3.71k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6945
6946
10.8k
      for (uint32_t j = 0; j < conds->children; j++) {
6947
7.16k
        zend_ast *cond_ast = conds->child[j];
6948
6949
7.16k
        if (jmpnz_opnums != NULL) {
6950
3.66k
          zend_update_jump_target_to_next(jmpnz_opnums[cond_count]);
6951
3.66k
        }
6952
6953
7.16k
        if (jumptable) {
6954
3.50k
          zval *cond_zv = zend_ast_get_zval(cond_ast);
6955
3.50k
          zval jmp_target;
6956
3.50k
          ZVAL_LONG(&jmp_target, get_next_op_number());
6957
6958
3.50k
          if (Z_TYPE_P(cond_zv) == IS_LONG) {
6959
3.18k
            zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6960
3.18k
          } else {
6961
323
            ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6962
323
            zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6963
323
          }
6964
3.50k
        }
6965
6966
7.16k
        cond_count++;
6967
7.16k
      }
6968
3.71k
    } else {
6969
597
      if (!uses_jumptable) {
6970
456
        zend_update_jump_target_to_next(opnum_default_jmp);
6971
456
      }
6972
6973
597
      if (jumptable) {
6974
141
        ZEND_ASSERT(opnum_match != (uint32_t)-1);
6975
141
        zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6976
141
        opline->extended_value = get_next_op_number();
6977
141
      }
6978
597
    }
6979
6980
4.30k
    znode body_node;
6981
4.30k
    zend_compile_expr(&body_node, body_ast);
6982
6983
4.30k
    if (is_first_case) {
6984
2.79k
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL);
6985
2.79k
      is_first_case = false;
6986
2.79k
    } else {
6987
1.51k
      zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL);
6988
1.51k
      SET_NODE(opline_qm_assign->result, result);
6989
1.51k
    }
6990
6991
4.30k
    jmp_end_opnums[i] = zend_emit_jump(0);
6992
4.30k
  }
6993
6994
  // Initialize result in case there is no arm
6995
2.94k
  if (arms->children == 0) {
6996
152
    result->op_type = IS_CONST;
6997
152
    ZVAL_NULL(&result->u.constant);
6998
152
  }
6999
7000
7.25k
  for (uint32_t i = 0; i < arms->children; ++i) {
7001
4.30k
    zend_update_jump_target_to_next(jmp_end_opnums[i]);
7002
4.30k
  }
7003
7004
2.94k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
7005
1.34k
    zend_op *opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
7006
1.34k
    opline->extended_value = ZEND_FREE_SWITCH;
7007
1.60k
  } else if (expr_node.op_type == IS_CONST) {
7008
1.11k
    zval_ptr_dtor_nogc(&expr_node.u.constant);
7009
1.11k
  }
7010
7011
2.94k
  if (jmpnz_opnums != NULL) {
7012
1.84k
    efree(jmpnz_opnums);
7013
1.84k
  }
7014
2.94k
  efree(jmp_end_opnums);
7015
2.94k
}
7016
7017
static void zend_compile_try(const zend_ast *ast) /* {{{ */
7018
30.1k
{
7019
30.1k
  zend_ast *try_ast = ast->child[0];
7020
30.1k
  const zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
7021
30.1k
  zend_ast *finally_ast = ast->child[2];
7022
7023
30.1k
  uint32_t i, j;
7024
30.1k
  zend_op *opline;
7025
30.1k
  uint32_t try_catch_offset;
7026
30.1k
  uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
7027
30.1k
  uint32_t orig_fast_call_var = CG(context).fast_call_var;
7028
30.1k
  uint32_t orig_try_catch_offset = CG(context).try_catch_offset;
7029
7030
30.1k
  if (catches->children == 0 && !finally_ast) {
7031
69
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
7032
69
  }
7033
7034
  /* label: try { } must not be equal to try { label: } */
7035
30.0k
  if (CG(context).labels) {
7036
918
    zend_label *label;
7037
918
    ZEND_HASH_MAP_REVERSE_FOREACH_PTR(CG(context).labels, label) {
7038
918
      if (label->opline_num == get_next_op_number()) {
7039
550
        zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
7040
550
      }
7041
918
      break;
7042
2.75k
    } ZEND_HASH_FOREACH_END();
7043
918
  }
7044
7045
30.0k
  try_catch_offset = zend_add_try_element(get_next_op_number());
7046
7047
30.0k
  if (finally_ast) {
7048
2.85k
    zend_loop_var fast_call;
7049
2.85k
    if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
7050
1.43k
      CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
7051
1.43k
    }
7052
2.85k
    CG(context).fast_call_var = get_temporary_variable();
7053
7054
    /* Push FAST_CALL on unwind stack */
7055
2.85k
    fast_call.opcode = ZEND_FAST_CALL;
7056
2.85k
    fast_call.var_type = IS_TMP_VAR;
7057
2.85k
    fast_call.var_num = CG(context).fast_call_var;
7058
2.85k
    fast_call.try_catch_offset = try_catch_offset;
7059
2.85k
    zend_stack_push(&CG(loop_var_stack), &fast_call);
7060
2.85k
  }
7061
7062
30.0k
  CG(context).try_catch_offset = try_catch_offset;
7063
7064
30.0k
  zend_compile_stmt(try_ast);
7065
7066
30.0k
  if (catches->children != 0) {
7067
27.3k
    jmp_opnums[0] = zend_emit_jump(0);
7068
27.3k
  }
7069
7070
62.0k
  for (i = 0; i < catches->children; ++i) {
7071
31.9k
    const zend_ast *catch_ast = catches->child[i];
7072
31.9k
    const zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]);
7073
31.9k
    zend_ast *var_ast = catch_ast->child[1];
7074
31.9k
    zend_ast *stmt_ast = catch_ast->child[2];
7075
31.9k
    zend_string *var_name = var_ast ? zval_make_interned_string(zend_ast_get_zval(var_ast)) : NULL;
7076
31.9k
    bool is_last_catch = (i + 1 == catches->children);
7077
7078
31.9k
    uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0);
7079
31.9k
    uint32_t opnum_catch = (uint32_t)-1;
7080
7081
31.9k
    CG(zend_lineno) = catch_ast->lineno;
7082
7083
70.1k
    for (j = 0; j < classes->children; j++) {
7084
38.1k
      zend_ast *class_ast = classes->child[j];
7085
38.1k
      bool is_last_class = (j + 1 == classes->children);
7086
7087
38.1k
      if (!zend_is_const_default_class_ref(class_ast)) {
7088
7
        zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
7089
7
      }
7090
7091
38.1k
      opnum_catch = get_next_op_number();
7092
38.1k
      if (i == 0 && j == 0) {
7093
27.3k
        CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
7094
27.3k
      }
7095
7096
38.1k
      opline = get_next_op();
7097
38.1k
      opline->opcode = ZEND_CATCH;
7098
38.1k
      opline->op1_type = IS_CONST;
7099
38.1k
      opline->op1.constant = zend_add_class_name_literal(
7100
38.1k
          zend_resolve_class_name_ast(class_ast));
7101
38.1k
      opline->extended_value = zend_alloc_cache_slot();
7102
7103
38.1k
      if (var_name && zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
7104
7
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
7105
7
      }
7106
7107
38.1k
      opline->result_type = var_name ? IS_CV : IS_UNUSED;
7108
38.1k
      opline->result.var = var_name ? lookup_cv(var_name) : -1;
7109
7110
38.1k
      if (is_last_catch && is_last_class) {
7111
27.3k
        opline->extended_value |= ZEND_LAST_CATCH;
7112
27.3k
      }
7113
7114
38.1k
      if (!is_last_class) {
7115
6.19k
        jmp_multicatch[j] = zend_emit_jump(0);
7116
6.19k
        opline = &CG(active_op_array)->opcodes[opnum_catch];
7117
6.19k
        opline->op2.opline_num = get_next_op_number();
7118
6.19k
      }
7119
38.1k
    }
7120
7121
38.1k
    for (j = 0; j < classes->children - 1; j++) {
7122
6.18k
      zend_update_jump_target_to_next(jmp_multicatch[j]);
7123
6.18k
    }
7124
7125
31.9k
    efree(jmp_multicatch);
7126
7127
31.9k
    zend_compile_stmt(stmt_ast);
7128
7129
31.9k
    if (!is_last_catch) {
7130
4.64k
      jmp_opnums[i + 1] = zend_emit_jump(0);
7131
4.64k
    }
7132
7133
31.9k
    ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class");
7134
31.9k
    opline = &CG(active_op_array)->opcodes[opnum_catch];
7135
31.9k
    if (!is_last_catch) {
7136
4.64k
      opline->op2.opline_num = get_next_op_number();
7137
4.64k
    }
7138
31.9k
  }
7139
7140
61.9k
  for (i = 0; i < catches->children; ++i) {
7141
31.9k
    zend_update_jump_target_to_next(jmp_opnums[i]);
7142
31.9k
  }
7143
7144
30.0k
  if (finally_ast) {
7145
2.84k
    zend_loop_var discard_exception;
7146
2.84k
    uint32_t opnum_jmp = get_next_op_number() + 1;
7147
7148
    /* Pop FAST_CALL from unwind stack */
7149
2.84k
    zend_stack_del_top(&CG(loop_var_stack));
7150
7151
    /* Push DISCARD_EXCEPTION on unwind stack */
7152
2.84k
    discard_exception.opcode = ZEND_DISCARD_EXCEPTION;
7153
2.84k
    discard_exception.var_type = IS_TMP_VAR;
7154
2.84k
    discard_exception.var_num = CG(context).fast_call_var;
7155
2.84k
    zend_stack_push(&CG(loop_var_stack), &discard_exception);
7156
7157
2.84k
    CG(zend_lineno) = finally_ast->lineno;
7158
7159
2.84k
    opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
7160
2.84k
    opline->op1.num = try_catch_offset;
7161
2.84k
    opline->result_type = IS_TMP_VAR;
7162
2.84k
    opline->result.var = CG(context).fast_call_var;
7163
7164
2.84k
    zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
7165
7166
2.84k
    zend_compile_stmt(finally_ast);
7167
7168
2.84k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
7169
2.84k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
7170
2.84k
      = get_next_op_number();
7171
7172
2.84k
    opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
7173
2.84k
    opline->op1_type = IS_TMP_VAR;
7174
2.84k
    opline->op1.var = CG(context).fast_call_var;
7175
2.84k
    opline->op2.num = orig_try_catch_offset;
7176
7177
2.84k
    zend_update_jump_target_to_next(opnum_jmp);
7178
7179
2.84k
    CG(context).fast_call_var = orig_fast_call_var;
7180
7181
    /* Pop DISCARD_EXCEPTION from unwind stack */
7182
2.84k
    zend_stack_del_top(&CG(loop_var_stack));
7183
2.84k
  }
7184
7185
30.0k
  CG(context).try_catch_offset = orig_try_catch_offset;
7186
7187
30.0k
  efree(jmp_opnums);
7188
30.0k
}
7189
/* }}} */
7190
7191
/* Encoding declarations must already be handled during parsing */
7192
bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
7193
3.34k
{
7194
3.34k
  const zend_ast_list *declares = zend_ast_get_list(ast);
7195
3.34k
  uint32_t i;
7196
7.36k
  for (i = 0; i < declares->children; ++i) {
7197
4.03k
    const zend_ast *declare_ast = declares->child[i];
7198
4.03k
    zend_ast *name_ast = declare_ast->child[0];
7199
4.03k
    zend_ast *value_ast = declare_ast->child[1];
7200
4.03k
    const zend_string *name = zend_ast_get_str(name_ast);
7201
7202
4.03k
    if (zend_string_equals_literal_ci(name, "encoding")) {
7203
170
      if (value_ast->kind != ZEND_AST_ZVAL) {
7204
22
        zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0);
7205
22
        return false;
7206
22
      }
7207
7208
148
      if (CG(multibyte)) {
7209
0
        zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
7210
7211
0
        const zend_encoding *new_encoding, *old_encoding;
7212
0
        zend_encoding_filter old_input_filter;
7213
7214
0
        CG(encoding_declared) = 1;
7215
7216
0
        new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
7217
0
        if (!new_encoding) {
7218
0
          zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
7219
0
        } else {
7220
0
          old_input_filter = LANG_SCNG(input_filter);
7221
0
          old_encoding = LANG_SCNG(script_encoding);
7222
0
          zend_multibyte_set_filter(new_encoding);
7223
7224
          /* need to re-scan if input filter changed */
7225
0
          if (old_input_filter != LANG_SCNG(input_filter) ||
7226
0
             (old_input_filter && new_encoding != old_encoding)) {
7227
0
            zend_multibyte_yyinput_again(old_input_filter, old_encoding);
7228
0
          }
7229
0
        }
7230
7231
0
        zend_string_release_ex(encoding_name, 0);
7232
148
      } else {
7233
148
        zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
7234
148
          "Zend multibyte feature is turned off by settings");
7235
148
      }
7236
148
    }
7237
4.03k
  }
7238
7239
3.32k
  return true;
7240
3.34k
}
7241
/* }}} */
7242
7243
/* Check whether this is the first statement, not counting declares. */
7244
static zend_result zend_is_first_statement(const zend_ast *ast, bool allow_nop) /* {{{ */
7245
3.69k
{
7246
3.69k
  uint32_t i = 0;
7247
3.69k
  const zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
7248
7249
29.6k
  while (i < file_ast->children) {
7250
29.6k
    if (file_ast->child[i] == ast) {
7251
3.63k
      return SUCCESS;
7252
25.9k
    } else if (file_ast->child[i] == NULL) {
7253
2.32k
      if (!allow_nop) {
7254
0
        return FAILURE;
7255
0
      }
7256
23.6k
    } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
7257
57
      return FAILURE;
7258
57
    }
7259
25.9k
    i++;
7260
25.9k
  }
7261
4
  return FAILURE;
7262
3.69k
}
7263
/* }}} */
7264
7265
static void zend_compile_declare(const zend_ast *ast) /* {{{ */
7266
8.30k
{
7267
8.30k
  const zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
7268
8.30k
  zend_ast *stmt_ast = ast->child[1];
7269
8.30k
  zend_declarables orig_declarables = FC(declarables);
7270
8.30k
  uint32_t i;
7271
7272
18.7k
  for (i = 0; i < declares->children; ++i) {
7273
10.5k
    zend_ast *declare_ast = declares->child[i];
7274
10.5k
    zend_ast *name_ast = declare_ast->child[0];
7275
10.5k
    zend_ast **value_ast_ptr = &declare_ast->child[1];
7276
10.5k
    zend_string *name = zend_ast_get_str(name_ast);
7277
7278
10.5k
    if ((*value_ast_ptr)->kind != ZEND_AST_ZVAL) {
7279
23
      zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
7280
23
    }
7281
7282
10.5k
    if (zend_string_equals_literal_ci(name, "ticks")) {
7283
7.24k
      zval value_zv;
7284
7.24k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7285
7.24k
      FC(declarables).ticks = zval_get_long(&value_zv);
7286
7.24k
      zval_ptr_dtor_nogc(&value_zv);
7287
7.24k
    } else if (zend_string_equals_literal_ci(name, "encoding")) {
7288
7289
114
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ false)) {
7290
7
        zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
7291
7
          "the very first statement in the script");
7292
7
      }
7293
3.14k
    } else if (zend_string_equals_literal_ci(name, "strict_types")) {
7294
770
      zval value_zv;
7295
7296
770
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
7297
25
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
7298
25
          "the very first statement in the script");
7299
25
      }
7300
7301
745
      if (ast->child[1] != NULL) {
7302
11
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
7303
11
          "use block mode");
7304
11
      }
7305
7306
734
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7307
7308
734
      if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
7309
50
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
7310
50
      }
7311
7312
684
      if (Z_LVAL(value_zv) == 1) {
7313
541
        CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
7314
541
      }
7315
7316
2.37k
    } else {
7317
2.37k
      zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
7318
2.37k
    }
7319
10.5k
  }
7320
7321
8.18k
  if (stmt_ast) {
7322
1.20k
    zend_compile_stmt(stmt_ast);
7323
7324
1.20k
    FC(declarables) = orig_declarables;
7325
1.20k
  }
7326
8.18k
}
7327
/* }}} */
7328
7329
static void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
7330
2.18M
{
7331
2.18M
  const zend_ast_list *list = zend_ast_get_list(ast);
7332
2.18M
  uint32_t i;
7333
9.93M
  for (i = 0; i < list->children; ++i) {
7334
7.75M
    zend_compile_stmt(list->child[i]);
7335
7.75M
  }
7336
2.18M
}
7337
/* }}} */
7338
7339
ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
7340
1.42M
{
7341
1.42M
  uint32_t i, n;
7342
7343
1.42M
  func->common.arg_flags[0] = 0;
7344
1.42M
  func->common.arg_flags[1] = 0;
7345
1.42M
  func->common.arg_flags[2] = 0;
7346
1.42M
  if (func->common.arg_info) {
7347
1.42M
    n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
7348
1.42M
    i = 0;
7349
2.88M
    while (i < n) {
7350
1.46M
      ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i]));
7351
1.46M
      i++;
7352
1.46M
    }
7353
1.42M
    if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_VARIADIC) && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) {
7354
461
      uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]);
7355
5.89k
      while (i < MAX_ARG_FLAG_NUM) {
7356
5.42k
        ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
7357
5.42k
        i++;
7358
5.42k
      }
7359
461
    }
7360
1.42M
  }
7361
1.42M
}
7362
/* }}} */
7363
7364
static zend_type zend_compile_single_typename(zend_ast *ast)
7365
910k
{
7366
910k
  ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE));
7367
910k
  if (ast->kind == ZEND_AST_TYPE) {
7368
6.28k
    if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) {
7369
5
      zend_error_noreturn(E_COMPILE_ERROR,
7370
5
        "Cannot use \"static\" when no class scope is active");
7371
5
    }
7372
7373
6.28k
    return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0);
7374
904k
  } else {
7375
904k
    zend_string *type_name = zend_ast_get_str(ast);
7376
904k
    uint8_t type_code = zend_lookup_builtin_type_by_name(type_name);
7377
7378
904k
    if (type_code != 0) {
7379
326k
      if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) {
7380
6
        zend_error_noreturn(E_COMPILE_ERROR,
7381
6
          "Type declaration '%s' must be unqualified",
7382
6
          ZSTR_VAL(zend_string_tolower(type_name)));
7383
6
      }
7384
7385
      /* Transform iterable into a type union alias */
7386
326k
      if (type_code == IS_ITERABLE) {
7387
        /* Set iterable bit for BC compat during Reflection and string representation of type */
7388
300k
        zend_type iterable = (zend_type) ZEND_TYPE_INIT_CLASS_MASK(ZSTR_KNOWN(ZEND_STR_TRAVERSABLE),
7389
300k
                  (MAY_BE_ARRAY|_ZEND_TYPE_ITERABLE_BIT));
7390
300k
        return iterable;
7391
300k
      }
7392
7393
25.4k
      return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0);
7394
577k
    } else {
7395
577k
      const char *correct_name;
7396
577k
      uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
7397
577k
      zend_string *class_name = type_name;
7398
7399
577k
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
7400
576k
        class_name = zend_resolve_class_name_ast(ast);
7401
576k
        zend_assert_valid_class_name(class_name, "a type name");
7402
576k
      } else {
7403
953
        ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT);
7404
7405
953
        zend_ensure_valid_class_fetch_type(fetch_type);
7406
7407
953
        bool substitute_self_parent = zend_is_scope_known()
7408
658
          && !(CG(active_class_entry)->ce_flags & ZEND_ACC_ANON_CLASS);
7409
7410
953
        if (fetch_type == ZEND_FETCH_CLASS_SELF) {
7411
          /* Scope might be unknown for unbound closures and traits */
7412
741
          if (substitute_self_parent) {
7413
495
            class_name = CG(active_class_entry)->name;
7414
495
            ZEND_ASSERT(class_name && "must know class name when resolving self type at compile time");
7415
495
          }
7416
741
        } else {
7417
212
          ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_PARENT);
7418
          /* Scope might be unknown for unbound closures and traits */
7419
212
          if (substitute_self_parent) {
7420
150
            class_name = CG(active_class_entry)->parent_name;
7421
150
            ZEND_ASSERT(class_name && "must know class name when resolving parent type at compile time");
7422
150
          }
7423
195
        }
7424
953
        zend_string_addref(class_name);
7425
936
      }
7426
7427
577k
      if (ast->attr == ZEND_NAME_NOT_FQ
7428
576k
          && zend_is_confusable_type(type_name, &correct_name)
7429
48.9k
          && zend_is_not_imported(type_name)) {
7430
48.9k
        const char *extra =
7431
48.9k
          FC(current_namespace) ? " or import the class with \"use\"" : "";
7432
48.9k
        if (correct_name) {
7433
35.1k
          zend_error(E_COMPILE_WARNING,
7434
35.1k
            "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? "
7435
35.1k
            "Write \"\\%s\"%s to suppress this warning",
7436
35.1k
            ZSTR_VAL(type_name), correct_name, ZSTR_VAL(class_name), extra);
7437
35.1k
        } else {
7438
13.7k
          zend_error(E_COMPILE_WARNING,
7439
13.7k
            "\"%s\" is not a supported builtin type "
7440
13.7k
            "and will be interpreted as a class name. "
7441
13.7k
            "Write \"\\%s\"%s to suppress this warning",
7442
13.7k
            ZSTR_VAL(type_name), ZSTR_VAL(class_name), extra);
7443
13.7k
        }
7444
48.9k
      }
7445
7446
577k
      class_name = zend_new_interned_string(class_name);
7447
577k
      zend_alloc_ce_cache(class_name);
7448
577k
      return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, /* allow null */ false, 0);
7449
577k
    }
7450
904k
  }
7451
910k
}
7452
7453
static void zend_are_intersection_types_redundant(const zend_type left_type, const zend_type right_type)
7454
8.22k
{
7455
8.22k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(left_type));
7456
8.22k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(right_type));
7457
8.22k
  const zend_type_list *l_type_list = ZEND_TYPE_LIST(left_type);
7458
8.22k
  const zend_type_list *r_type_list = ZEND_TYPE_LIST(right_type);
7459
8.22k
  const zend_type_list *smaller_type_list, *larger_type_list;
7460
8.22k
  bool flipped = false;
7461
7462
8.22k
  if (r_type_list->num_types < l_type_list->num_types) {
7463
2.91k
    smaller_type_list = r_type_list;
7464
2.91k
    larger_type_list = l_type_list;
7465
2.91k
    flipped = true;
7466
5.30k
  } else {
7467
5.30k
    smaller_type_list = l_type_list;
7468
5.30k
    larger_type_list = r_type_list;
7469
5.30k
  }
7470
7471
8.22k
  unsigned int sum = 0;
7472
8.22k
  const zend_type *outer_type;
7473
31.6k
  ZEND_TYPE_LIST_FOREACH(smaller_type_list, outer_type) {
7474
31.6k
    const zend_type *inner_type;
7475
105k
    ZEND_TYPE_LIST_FOREACH(larger_type_list, inner_type) {
7476
105k
      if (zend_string_equals_ci(ZEND_TYPE_NAME(*inner_type), ZEND_TYPE_NAME(*outer_type))) {
7477
8.04k
        sum++;
7478
8.04k
        break;
7479
8.04k
      }
7480
105k
    } ZEND_TYPE_LIST_FOREACH_END();
7481
31.6k
  } ZEND_TYPE_LIST_FOREACH_END();
7482
7483
8.22k
  if (sum == smaller_type_list->num_types) {
7484
18
    zend_string *smaller_type_str;
7485
18
    zend_string *larger_type_str;
7486
18
    if (flipped) {
7487
5
      smaller_type_str = zend_type_to_string(right_type);
7488
5
      larger_type_str = zend_type_to_string(left_type);
7489
13
    } else {
7490
13
      smaller_type_str = zend_type_to_string(left_type);
7491
13
      larger_type_str = zend_type_to_string(right_type);
7492
13
    }
7493
18
    if (smaller_type_list->num_types == larger_type_list->num_types) {
7494
9
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant with type %s",
7495
9
        ZSTR_VAL(smaller_type_str), ZSTR_VAL(larger_type_str));
7496
9
    } else {
7497
9
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7498
9
        ZSTR_VAL(larger_type_str), ZSTR_VAL(smaller_type_str));
7499
9
    }
7500
18
  }
7501
8.22k
}
7502
7503
static void zend_is_intersection_type_redundant_by_single_type(const zend_type intersection_type, const zend_type single_type)
7504
12.1k
{
7505
12.1k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(intersection_type));
7506
12.1k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(single_type));
7507
7508
12.1k
  const zend_type *single_intersection_type = NULL;
7509
42.2k
  ZEND_TYPE_FOREACH(intersection_type, single_intersection_type) {
7510
42.2k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(*single_intersection_type), ZEND_TYPE_NAME(single_type))) {
7511
5
      zend_string *single_type_str = zend_type_to_string(single_type);
7512
5
      zend_string *complete_type = zend_type_to_string(intersection_type);
7513
5
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7514
5
          ZSTR_VAL(complete_type), ZSTR_VAL(single_type_str));
7515
5
    }
7516
42.2k
  } ZEND_TYPE_FOREACH_END();
7517
12.1k
}
7518
7519
/* Used by both intersection and union types prior to transforming the type list to a full zend_type */
7520
static void zend_is_type_list_redundant_by_single_type(const zend_type_list *type_list, const zend_type type)
7521
204k
{
7522
204k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(type));
7523
415k
  for (size_t i = 0; i < type_list->num_types - 1; i++) {
7524
210k
    if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7525
8.66k
      zend_is_intersection_type_redundant_by_single_type(type_list->types[i], type);
7526
8.66k
      continue;
7527
8.66k
    }
7528
201k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(type_list->types[i]), ZEND_TYPE_NAME(type))) {
7529
51
      zend_string *single_type_str = zend_type_to_string(type);
7530
51
      zend_error_noreturn(E_COMPILE_ERROR, "Duplicate type %s is redundant", ZSTR_VAL(single_type_str));
7531
51
    }
7532
201k
  }
7533
204k
}
7534
7535
static zend_type zend_compile_typename(zend_ast *ast);
7536
7537
static zend_type zend_compile_typename_ex(
7538
    zend_ast *ast, bool force_allow_null, bool *forced_allow_null) /* {{{ */
7539
720k
{
7540
720k
  bool is_marked_nullable = ast->attr & ZEND_TYPE_NULLABLE;
7541
720k
  zend_ast_attr orig_ast_attr = ast->attr;
7542
720k
  zend_type type = ZEND_TYPE_INIT_NONE(0);
7543
7544
720k
  if (is_marked_nullable) {
7545
3.19k
    ast->attr &= ~ZEND_TYPE_NULLABLE;
7546
3.19k
  }
7547
7548
720k
  if (ast->kind == ZEND_AST_TYPE_UNION) {
7549
180k
    const zend_ast_list *list = zend_ast_get_list(ast);
7550
180k
    zend_type_list *type_list;
7551
180k
    bool is_composite = false;
7552
180k
    bool has_only_iterable_class = true;
7553
180k
    ALLOCA_FLAG(use_heap)
7554
7555
180k
    type_list = do_alloca(ZEND_TYPE_LIST_SIZE(list->children), use_heap);
7556
180k
    type_list->num_types = 0;
7557
7558
547k
    for (uint32_t i = 0; i < list->children; i++) {
7559
367k
      zend_ast *type_ast = list->child[i];
7560
367k
      zend_type single_type;
7561
367k
      uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7562
7563
367k
      if (type_ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7564
9.61k
        has_only_iterable_class = false;
7565
9.61k
        is_composite = true;
7566
        /* The first class type can be stored directly as the type ptr payload. */
7567
9.61k
        if (ZEND_TYPE_IS_COMPLEX(type) && !ZEND_TYPE_HAS_LIST(type)) {
7568
          /* Switch from single name to name list. */
7569
1.29k
          type_list->num_types = 1;
7570
1.29k
          type_list->types[0] = type;
7571
          /* Clear MAY_BE_* type flags */
7572
1.29k
          ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7573
1.29k
        }
7574
        /* Mark type as list type */
7575
9.61k
        ZEND_TYPE_SET_LIST(type, type_list);
7576
7577
9.61k
        single_type = zend_compile_typename(type_ast);
7578
9.61k
        ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(single_type));
7579
7580
9.61k
        type_list->types[type_list->num_types++] = single_type;
7581
7582
        /* Check for trivially redundant class types */
7583
21.2k
        for (size_t i = 0; i < type_list->num_types - 1; i++) {
7584
11.6k
          if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7585
8.22k
            zend_are_intersection_types_redundant(single_type, type_list->types[i]);
7586
8.22k
            continue;
7587
8.22k
          }
7588
          /* Type from type list is a simple type */
7589
3.44k
          zend_is_intersection_type_redundant_by_single_type(single_type, type_list->types[i]);
7590
3.44k
        }
7591
9.61k
        continue;
7592
9.61k
      }
7593
7594
357k
      single_type = zend_compile_single_typename(type_ast);
7595
357k
      uint32_t single_type_mask = ZEND_TYPE_PURE_MASK(single_type);
7596
7597
357k
      if (single_type_mask == MAY_BE_ANY) {
7598
5
        zend_error_noreturn(E_COMPILE_ERROR, "Type mixed can only be used as a standalone type");
7599
5
      }
7600
357k
      if (ZEND_TYPE_IS_COMPLEX(single_type) && !ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7601
311k
        has_only_iterable_class = false;
7602
311k
      }
7603
7604
357k
      uint32_t type_mask_overlap = ZEND_TYPE_PURE_MASK(type) & single_type_mask;
7605
357k
      if (type_mask_overlap) {
7606
28
        zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap);
7607
28
        zend_string *overlap_type_str = zend_type_to_string(overlap_type);
7608
28
        zend_error_noreturn(E_COMPILE_ERROR,
7609
28
          "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str));
7610
28
      }
7611
7612
357k
      if ( ((type_mask & MAY_BE_TRUE) && (single_type_mask == MAY_BE_FALSE))
7613
357k
          || ((type_mask & MAY_BE_FALSE) && (single_type_mask == MAY_BE_TRUE)) ) {
7614
10
        zend_error_noreturn(E_COMPILE_ERROR,
7615
10
          "Type contains both true and false, bool must be used instead");
7616
10
      }
7617
357k
      ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type);
7618
      /* Clear MAY_BE_* type flags */
7619
357k
      ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK;
7620
7621
357k
      if (ZEND_TYPE_IS_COMPLEX(single_type)) {
7622
353k
        if (!ZEND_TYPE_IS_COMPLEX(type) && !is_composite) {
7623
          /* The first class type can be stored directly as the type ptr payload. */
7624
172k
          ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type));
7625
172k
          ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT;
7626
180k
        } else {
7627
180k
          if (type_list->num_types == 0) {
7628
            /* Switch from single name to name list. */
7629
170k
            type_list->num_types = 1;
7630
170k
            type_list->types[0] = type;
7631
            /* Clear MAY_BE_* type flags */
7632
170k
            ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7633
170k
            ZEND_TYPE_SET_LIST(type, type_list);
7634
170k
          }
7635
7636
180k
          type_list->types[type_list->num_types++] = single_type;
7637
7638
          /* Check for trivially redundant class types */
7639
180k
          zend_is_type_list_redundant_by_single_type(type_list, single_type);
7640
180k
        }
7641
353k
      }
7642
357k
    }
7643
7644
180k
    if (type_list->num_types) {
7645
178k
      zend_type_list *list = zend_arena_alloc(
7646
178k
        &CG(arena), ZEND_TYPE_LIST_SIZE(type_list->num_types));
7647
178k
      memcpy(list, type_list, ZEND_TYPE_LIST_SIZE(type_list->num_types));
7648
178k
      ZEND_TYPE_SET_LIST(type, list);
7649
178k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7650
      /* Inform that the type list is a union type */
7651
178k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7652
178k
    }
7653
7654
180k
    free_alloca(type_list, use_heap);
7655
7656
180k
    uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7657
180k
    if ((type_mask & MAY_BE_OBJECT) &&
7658
468
        ((!has_only_iterable_class && ZEND_TYPE_IS_COMPLEX(type)) || (type_mask & MAY_BE_STATIC))) {
7659
27
      zend_string *type_str = zend_type_to_string(type);
7660
27
      zend_error_noreturn(E_COMPILE_ERROR,
7661
27
        "Type %s contains both object and a class type, which is redundant",
7662
27
        ZSTR_VAL(type_str));
7663
27
    }
7664
539k
  } else if (ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7665
11.0k
    const zend_ast_list *list = zend_ast_get_list(ast);
7666
11.0k
    zend_type_list *type_list;
7667
7668
    /* Allocate the type list directly on the arena as it must be a type
7669
     * list of the same number of elements as the AST list has children */
7670
11.0k
    type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children));
7671
11.0k
    type_list->num_types = 0;
7672
7673
11.0k
    ZEND_ASSERT(list->children > 1);
7674
7675
35.1k
    for (uint32_t i = 0; i < list->children; i++) {
7676
24.2k
      zend_ast *type_ast = list->child[i];
7677
24.2k
      zend_type single_type = zend_compile_single_typename(type_ast);
7678
7679
      /* An intersection of union types cannot exist so invalidate it
7680
       * Currently only can happen with iterable getting canonicalized to Traversable|array */
7681
24.2k
      if (ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7682
5
        zend_string *standard_type_str = zend_type_to_string(single_type);
7683
5
        zend_error_noreturn(E_COMPILE_ERROR,
7684
5
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7685
5
      }
7686
      /* An intersection of standard types cannot exist so invalidate it */
7687
24.2k
      if (ZEND_TYPE_IS_ONLY_MASK(single_type)) {
7688
63
        zend_string *standard_type_str = zend_type_to_string(single_type);
7689
63
        zend_error_noreturn(E_COMPILE_ERROR,
7690
63
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7691
63
      }
7692
      /* Check for "self" and "parent" too */
7693
24.1k
      if (
7694
24.1k
        zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_SELF))
7695
24.1k
        || zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_PARENT))
7696
24.1k
      ) {
7697
10
        zend_error_noreturn(E_COMPILE_ERROR,
7698
10
          "Type %s cannot be part of an intersection type", ZSTR_VAL(ZEND_TYPE_NAME(single_type)));
7699
10
      }
7700
7701
      /* Add type to the type list */
7702
24.1k
      type_list->types[type_list->num_types++] = single_type;
7703
7704
      /* Check for trivially redundant class types */
7705
24.1k
      zend_is_type_list_redundant_by_single_type(type_list, single_type);
7706
24.1k
    }
7707
7708
10.9k
    ZEND_ASSERT(list->children == type_list->num_types);
7709
7710
    /* An implicitly nullable intersection type needs to be converted to a DNF type */
7711
10.9k
    if (force_allow_null) {
7712
164
      zend_type intersection_type = ZEND_TYPE_INIT_NONE(0);
7713
164
      ZEND_TYPE_SET_LIST(intersection_type, type_list);
7714
164
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_INTERSECTION_BIT;
7715
164
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_ARENA_BIT;
7716
7717
164
      zend_type_list *dnf_type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(1));
7718
164
      dnf_type_list->num_types = 1;
7719
164
      dnf_type_list->types[0] = intersection_type;
7720
164
      ZEND_TYPE_SET_LIST(type, dnf_type_list);
7721
      /* Inform that the type list is a DNF type */
7722
164
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7723
164
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7724
10.7k
    } else {
7725
10.7k
      ZEND_TYPE_SET_LIST(type, type_list);
7726
      /* Inform that the type list is an intersection type */
7727
10.7k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_INTERSECTION_BIT;
7728
10.7k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7729
10.7k
    }
7730
528k
  } else {
7731
528k
    type = zend_compile_single_typename(ast);
7732
528k
  }
7733
7734
719k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
7735
7736
719k
  if (type_mask == MAY_BE_ANY && is_marked_nullable) {
7737
13
    zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be marked as nullable since mixed already includes null");
7738
13
  }
7739
7740
719k
  if ((type_mask & MAY_BE_NULL) && is_marked_nullable) {
7741
5
    zend_error_noreturn(E_COMPILE_ERROR, "null cannot be marked as nullable");
7742
5
  }
7743
7744
719k
  if (force_allow_null && !is_marked_nullable && !(type_mask & MAY_BE_NULL)) {
7745
264
    *forced_allow_null = true;
7746
264
  }
7747
7748
719k
  if (is_marked_nullable || force_allow_null) {
7749
3.52k
    ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
7750
3.52k
    type_mask = ZEND_TYPE_PURE_MASK(type);
7751
3.52k
  }
7752
7753
719k
  if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_VOID)) {
7754
10
    zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type");
7755
10
  }
7756
7757
719k
  if ((type_mask & MAY_BE_NEVER) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_NEVER)) {
7758
6
    zend_error_noreturn(E_COMPILE_ERROR, "never can only be used as a standalone type");
7759
6
  }
7760
7761
719k
  ast->attr = orig_ast_attr;
7762
719k
  return type;
7763
719k
}
7764
/* }}} */
7765
7766
static zend_type zend_compile_typename(zend_ast *ast)
7767
131k
{
7768
131k
  bool forced_allow_null;
7769
131k
  return zend_compile_typename_ex(ast, false, &forced_allow_null);
7770
131k
}
7771
7772
/* May convert value from int to float. */
7773
static bool zend_is_valid_default_value(zend_type type, zval *value)
7774
3.68k
{
7775
3.68k
  ZEND_ASSERT(ZEND_TYPE_IS_SET(type));
7776
3.68k
  if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) {
7777
2.66k
    return true;
7778
2.66k
  }
7779
1.02k
  if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) {
7780
    /* Integers are allowed as initializers for floating-point values. */
7781
864
    convert_to_double(value);
7782
864
    return true;
7783
864
  }
7784
159
  return false;
7785
1.02k
}
7786
7787
static void zend_compile_attributes(
7788
  HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target, uint32_t promoted
7789
1.32M
) /* {{{ */ {
7790
1.32M
  zend_attribute *attr;
7791
1.32M
  zend_internal_attribute *config;
7792
7793
1.32M
  const zend_ast_list *list = zend_ast_get_list(ast);
7794
1.32M
  uint32_t g, i, j;
7795
7796
1.32M
  ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE_LIST);
7797
7798
2.65M
  for (g = 0; g < list->children; g++) {
7799
1.33M
    const zend_ast_list *group = zend_ast_get_list(list->child[g]);
7800
7801
1.33M
    ZEND_ASSERT(group->kind == ZEND_AST_ATTRIBUTE_GROUP);
7802
7803
3.85M
    for (i = 0; i < group->children; i++) {
7804
2.52M
      ZEND_ASSERT(group->child[i]->kind == ZEND_AST_ATTRIBUTE);
7805
7806
2.52M
      const zend_ast *el = group->child[i];
7807
7808
2.52M
      if (el->child[1] &&
7809
47.9k
          el->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) {
7810
11
          zend_error_noreturn(E_COMPILE_ERROR,
7811
11
              "Cannot create Closure as attribute argument");
7812
11
      }
7813
7814
2.52M
      zend_string *name = zend_resolve_class_name_ast(el->child[0]);
7815
2.52M
      zend_string *lcname = zend_string_tolower_ex(name, false);
7816
2.52M
      zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL;
7817
7818
2.52M
      config = zend_internal_attribute_get(lcname);
7819
2.52M
      zend_string_release(lcname);
7820
7821
      /* Exclude internal attributes that do not match on promoted properties. */
7822
2.52M
      if (config && !(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7823
817
        if (promoted & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL)) {
7824
7
          zend_string_release(name);
7825
7
          continue;
7826
7
        }
7827
817
      }
7828
7829
2.52M
      uint32_t flags = (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES)
7830
2.52M
        ? ZEND_ATTRIBUTE_STRICT_TYPES : 0;
7831
2.52M
      attr = zend_add_attribute(
7832
2.52M
        attributes, name, args ? args->children : 0, flags, offset, el->lineno);
7833
2.52M
      zend_string_release(name);
7834
7835
      /* Populate arguments */
7836
2.52M
      if (args) {
7837
47.8k
        ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST);
7838
7839
47.8k
        bool uses_named_args = false;
7840
109k
        for (j = 0; j < args->children; j++) {
7841
61.2k
          zend_ast **arg_ast_ptr = &args->child[j];
7842
61.2k
          zend_ast *arg_ast = *arg_ast_ptr;
7843
7844
61.2k
          if (arg_ast->kind == ZEND_AST_UNPACK) {
7845
5
            zend_error_noreturn(E_COMPILE_ERROR,
7846
5
              "Cannot use unpacking in attribute argument list");
7847
5
          }
7848
7849
61.2k
          if (arg_ast->kind == ZEND_AST_NAMED_ARG) {
7850
1.11k
            attr->args[j].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0]));
7851
1.11k
            arg_ast_ptr = &arg_ast->child[1];
7852
1.11k
            uses_named_args = true;
7853
7854
6.23k
            for (uint32_t k = 0; k < j; k++) {
7855
5.13k
              if (attr->args[k].name &&
7856
3.25k
                  zend_string_equals(attr->args[k].name, attr->args[j].name)) {
7857
8
                zend_error_noreturn(E_COMPILE_ERROR, "Duplicate named parameter $%s",
7858
8
                  ZSTR_VAL(attr->args[j].name));
7859
8
              }
7860
5.13k
            }
7861
60.1k
          } else if (uses_named_args) {
7862
39
            zend_error_noreturn(E_COMPILE_ERROR,
7863
39
              "Cannot use positional argument after named argument");
7864
39
          }
7865
7866
61.1k
          zend_const_expr_to_zval(
7867
61.1k
            &attr->args[j].value, arg_ast_ptr, /* allow_dynamic */ true);
7868
61.1k
        }
7869
47.8k
      }
7870
2.52M
    }
7871
1.33M
  }
7872
7873
1.32M
  if (*attributes != NULL) {
7874
    /* Allow delaying target validation for forward compatibility. */
7875
1.32M
    const zend_attribute *delayed_target_validation = NULL;
7876
1.32M
    if (target == ZEND_ATTRIBUTE_TARGET_PARAMETER) {
7877
44.5k
      ZEND_ASSERT(offset >= 1);
7878
      /* zend_get_parameter_attribute_str will add 1 too */
7879
44.5k
      delayed_target_validation = zend_get_parameter_attribute_str(
7880
44.5k
        *attributes,
7881
44.5k
        "delayedtargetvalidation",
7882
44.5k
        strlen("delayedtargetvalidation"),
7883
44.5k
        offset - 1
7884
44.5k
      );
7885
1.28M
    } else {
7886
1.28M
      delayed_target_validation = zend_get_attribute_str(
7887
1.28M
        *attributes,
7888
1.28M
        "delayedtargetvalidation",
7889
1.28M
        strlen("delayedtargetvalidation")
7890
1.28M
      );
7891
1.28M
    }
7892
    /* Validate attributes in a secondary loop (needed to detect repeated attributes). */
7893
9.03M
    ZEND_HASH_PACKED_FOREACH_PTR(*attributes, attr) {
7894
9.03M
      if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) {
7895
3.18M
        continue;
7896
3.18M
      }
7897
7898
9.03M
      bool run_validator = true;
7899
3.89k
      if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7900
378
        if (delayed_target_validation == NULL) {
7901
44
          zend_string *location = zend_get_attribute_target_names(target);
7902
44
          zend_string *allowed = zend_get_attribute_target_names(config->flags);
7903
7904
44
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
7905
44
            ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
7906
44
          );
7907
44
        }
7908
334
        run_validator = false;
7909
334
      }
7910
7911
3.85k
      if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
7912
3.85k
        if (zend_is_attribute_repeated(*attributes, attr)) {
7913
16
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name));
7914
16
        }
7915
3.85k
      }
7916
7917
      /* Validators are not run if the target is already invalid */
7918
3.83k
      if (run_validator && config->validator != NULL) {
7919
2.11k
        zend_string *error = config->validator(attr, target, CG(active_class_entry));
7920
2.11k
        if (error != NULL) {
7921
301
          if (delayed_target_validation == NULL) {
7922
86
            zend_error_noreturn(E_COMPILE_ERROR, "%s", ZSTR_VAL(error));
7923
215
          } else {
7924
215
            attr->validation_error = error;
7925
215
          }
7926
301
        }
7927
2.11k
      }
7928
3.83k
    } ZEND_HASH_FOREACH_END();
7929
1.32M
  }
7930
1.32M
}
7931
/* }}} */
7932
7933
static void zend_compile_property_hooks(
7934
    zend_property_info *prop_info, zend_string *prop_name,
7935
    zend_ast *prop_type_ast, const zend_ast_list *hooks);
7936
7937
typedef struct {
7938
  const zend_string *property_name;
7939
  bool uses_property;
7940
} find_property_usage_context;
7941
7942
static void zend_property_hook_find_property_usage(zend_ast **ast_ptr, void *_context) /* {{{ */
7943
33.6k
{
7944
33.6k
  zend_ast *ast = *ast_ptr;
7945
33.6k
  find_property_usage_context *context = (find_property_usage_context *) _context;
7946
7947
33.6k
  if (ast == NULL) {
7948
204
    return;
7949
33.4k
  } else if (ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_NULLSAFE_PROP) {
7950
2.62k
    const zend_ast *object_ast = ast->child[0];
7951
2.62k
    zend_ast *property_ast = ast->child[1];
7952
7953
2.62k
    if (object_ast->kind == ZEND_AST_VAR
7954
2.23k
     && object_ast->child[0]->kind == ZEND_AST_ZVAL
7955
2.14k
     && property_ast->kind == ZEND_AST_ZVAL) {
7956
2.02k
      const zval *object = zend_ast_get_zval(object_ast->child[0]);
7957
2.02k
      const zval *property = zend_ast_get_zval(property_ast);
7958
2.02k
      if (Z_TYPE_P(object) == IS_STRING
7959
2.01k
        && Z_TYPE_P(property) == IS_STRING
7960
2.01k
        && zend_string_equals_literal(Z_STR_P(object), "this")
7961
1.70k
        && zend_string_equals(Z_STR_P(property), context->property_name)) {
7962
795
        context->uses_property = true;
7963
        /* No need to look for references in this branch. */
7964
795
        return;
7965
795
      }
7966
2.02k
    }
7967
2.62k
  }
7968
7969
  /* Don't search across function/class boundaries. */
7970
32.6k
  if (!zend_ast_is_special(ast)) {
7971
20.8k
    zend_ast_apply(ast, zend_property_hook_find_property_usage, context);
7972
20.8k
  }
7973
32.6k
}
7974
7975
static bool zend_property_hook_uses_property(const zend_string *property_name, const zend_string *hook_name, zend_ast *hook_ast)
7976
5.00k
{
7977
5.00k
  if (zend_string_equals_literal_ci(hook_name, "set")
7978
2.04k
   && hook_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
7979
641
    return true;
7980
641
  }
7981
7982
4.36k
  find_property_usage_context context = { property_name, false };
7983
4.36k
  zend_property_hook_find_property_usage(&hook_ast, &context);
7984
4.36k
  return context.uses_property;
7985
5.00k
}
7986
7987
static bool zend_property_is_virtual(const zend_class_entry *ce, const zend_string *property_name, zend_ast *hooks_ast)
7988
34.3k
{
7989
34.3k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
7990
289
    return true;
7991
289
  }
7992
34.0k
  if (!hooks_ast) {
7993
30.2k
    return false;
7994
30.2k
  }
7995
7996
34.0k
  bool is_virtual = true;
7997
7998
3.80k
  const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
7999
9.32k
  for (uint32_t i = 0; i < hooks->children; i++) {
8000
5.52k
    const zend_ast_decl *hook = (const zend_ast_decl *) hooks->child[i];
8001
5.52k
    zend_ast *body = hook->child[2];
8002
5.52k
    if (body && zend_property_hook_uses_property(property_name, hook->name, body)) {
8003
1.38k
      is_virtual = false;
8004
1.38k
    }
8005
5.52k
  }
8006
8007
3.80k
  return is_virtual;
8008
34.0k
}
8009
8010
static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */
8011
1.84M
{
8012
1.84M
  zend_ast_list *list = zend_ast_get_list(ast);
8013
1.84M
  uint32_t i;
8014
1.84M
  zend_op_array *op_array = CG(active_op_array);
8015
1.84M
  zend_arg_info *arg_infos;
8016
8017
1.84M
  if (return_type_ast || fallback_return_type) {
8018
    /* Use op_array->arg_info[-1] for return type */
8019
105k
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
8020
105k
    arg_infos->name = NULL;
8021
105k
    if (return_type_ast) {
8022
99.6k
      arg_infos->type = zend_compile_typename(return_type_ast);
8023
99.6k
      ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS(
8024
99.6k
        (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0, /* is_tentative */ 0);
8025
99.6k
    } else {
8026
5.96k
      arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0);
8027
5.96k
    }
8028
105k
    arg_infos->doc_comment = NULL;
8029
105k
    arg_infos++;
8030
105k
    op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
8031
8032
105k
    if (ZEND_TYPE_CONTAINS_CODE(arg_infos[-1].type, IS_VOID)
8033
4.20k
        && (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
8034
455
      zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
8035
455
      zend_error(E_DEPRECATED, "%s(): Returning by reference from a void function is deprecated", ZSTR_VAL(func_name));
8036
455
      zend_string_release(func_name);
8037
455
    }
8038
1.74M
  } else {
8039
1.74M
    if (list->children == 0) {
8040
452k
      return;
8041
452k
    }
8042
1.28M
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
8043
1.28M
  }
8044
8045
  /* Find last required parameter number for deprecation message. */
8046
1.39M
  uint32_t last_required_param = (uint32_t) -1;
8047
2.83M
  for (i = 0; i < list->children; ++i) {
8048
1.43M
    zend_ast *param_ast = list->child[i];
8049
1.43M
    zend_ast *default_ast_ptr = param_ast->child[2];
8050
1.43M
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
8051
1.43M
    if (!default_ast_ptr && !is_variadic) {
8052
1.42M
      last_required_param = i;
8053
1.42M
    }
8054
1.43M
  }
8055
8056
1.39M
  const uint32_t promotion_flags = ZEND_ACC_PPP_MASK | ZEND_ACC_PPP_SET_MASK | ZEND_ACC_READONLY | ZEND_ACC_FINAL;
8057
2.83M
  for (i = 0; i < list->children; ++i) {
8058
1.43M
    zend_ast *param_ast = list->child[i];
8059
1.43M
    zend_ast *type_ast = param_ast->child[0];
8060
1.43M
    zend_ast *var_ast = param_ast->child[1];
8061
1.43M
    zend_ast **default_ast_ptr = &param_ast->child[2];
8062
1.43M
    zend_ast *attributes_ast = param_ast->child[3];
8063
1.43M
    zend_ast *doc_comment_ast = param_ast->child[4];
8064
1.43M
    zend_ast *hooks_ast = param_ast->child[5];
8065
1.43M
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast));
8066
1.43M
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
8067
1.43M
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
8068
1.43M
    uint32_t property_flags = param_ast->attr & promotion_flags;
8069
1.43M
    bool is_promoted = property_flags || hooks_ast;
8070
8071
1.43M
    CG(zend_lineno) = param_ast->lineno;
8072
8073
1.43M
    znode var_node, default_node;
8074
1.43M
    uint8_t opcode;
8075
1.43M
    zend_op *opline;
8076
1.43M
    zend_arg_info *arg_info;
8077
8078
1.43M
    if (zend_is_auto_global(name)) {
8079
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
8080
2
        ZSTR_VAL(name));
8081
2
    }
8082
8083
1.43M
    var_node.op_type = IS_CV;
8084
1.43M
    var_node.u.op.var = lookup_cv(name);
8085
8086
1.43M
    if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
8087
36
      zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
8088
36
        ZSTR_VAL(name));
8089
1.43M
    } else if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8090
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
8091
1.43M
    } else if (zend_string_equals_literal(name, "http_response_header")) {
8092
133
      CG(context).has_assigned_to_http_response_header = true;
8093
133
    }
8094
8095
1.43M
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
8096
7
      zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
8097
7
    }
8098
8099
1.43M
    if (is_variadic) {
8100
917
      opcode = ZEND_RECV_VARIADIC;
8101
917
      default_node.op_type = IS_UNUSED;
8102
917
      op_array->fn_flags |= ZEND_ACC_VARIADIC;
8103
8104
917
      if (*default_ast_ptr) {
8105
5
        zend_error_noreturn(E_COMPILE_ERROR,
8106
5
          "Variadic parameter cannot have a default value");
8107
5
      }
8108
1.43M
    } else if (*default_ast_ptr) {
8109
      /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
8110
6.23k
      uint32_t cops = CG(compiler_options);
8111
6.23k
      CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
8112
6.23k
      opcode = ZEND_RECV_INIT;
8113
6.23k
      default_node.op_type = IS_CONST;
8114
6.23k
      zend_const_expr_to_zval(
8115
6.23k
        &default_node.u.constant, default_ast_ptr, /* allow_dynamic */ true);
8116
6.23k
      CG(compiler_options) = cops;
8117
1.42M
    } else {
8118
1.42M
      opcode = ZEND_RECV;
8119
1.42M
      default_node.op_type = IS_UNUSED;
8120
1.42M
      op_array->required_num_args = i + 1;
8121
1.42M
    }
8122
8123
1.43M
    arg_info = &arg_infos[i];
8124
1.43M
    arg_info->name = zend_string_copy(name);
8125
1.43M
    arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0);
8126
1.43M
    arg_info->default_value = NULL;
8127
1.43M
    arg_info->doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
8128
8129
1.43M
    if (attributes_ast) {
8130
44.5k
      zend_compile_attributes(
8131
44.5k
        &op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER,
8132
44.5k
        is_promoted ? ZEND_ATTRIBUTE_TARGET_PROPERTY : 0
8133
44.5k
      );
8134
44.5k
    }
8135
8136
1.43M
    bool forced_allow_nullable = false;
8137
1.43M
    if (type_ast) {
8138
588k
      uint32_t default_type = *default_ast_ptr ? Z_TYPE(default_node.u.constant) : IS_UNDEF;
8139
588k
      bool force_nullable = default_type == IS_NULL && !is_promoted;
8140
8141
588k
      op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
8142
588k
      arg_info->type = zend_compile_typename_ex(type_ast, force_nullable, &forced_allow_nullable);
8143
588k
      if (forced_allow_nullable) {
8144
264
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
8145
264
        zend_error(E_DEPRECATED,
8146
264
           "%s(): Implicitly marking parameter $%s as nullable is deprecated, the explicit nullable type "
8147
264
           "must be used instead", ZSTR_VAL(func_name), ZSTR_VAL(name));
8148
264
        zend_string_release(func_name);
8149
264
      }
8150
8151
588k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) {
8152
6
        zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");
8153
6
      }
8154
8155
588k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_NEVER) {
8156
5
        zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type");
8157
5
      }
8158
8159
588k
      if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable
8160
616
          && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) {
8161
41
        zend_string *type_str = zend_type_to_string(arg_info->type);
8162
41
        zend_error_noreturn(E_COMPILE_ERROR,
8163
41
          "Cannot use %s as default value for parameter $%s of type %s",
8164
41
          zend_get_type_by_const(default_type),
8165
41
          ZSTR_VAL(name), ZSTR_VAL(type_str));
8166
41
      }
8167
588k
    }
8168
1.43M
    if (last_required_param != (uint32_t) -1
8169
1.43M
     && i < last_required_param
8170
53.4k
     && default_node.op_type == IS_CONST) {
8171
      /* Ignore parameters of the form "Type $param = null".
8172
       * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
8173
810
      if (!forced_allow_nullable) {
8174
618
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
8175
618
        zend_ast *required_param_ast = list->child[last_required_param];
8176
618
        zend_error(E_DEPRECATED,
8177
618
          "%s(): Optional parameter $%s declared before required parameter $%s "
8178
618
          "is implicitly treated as a required parameter",
8179
618
          ZSTR_VAL(func_name), ZSTR_VAL(name), ZSTR_VAL(zend_ast_get_str(required_param_ast->child[1])));
8180
618
        zend_string_release(func_name);
8181
618
      }
8182
8183
      /* Regardless of whether we issue a deprecation, convert this parameter into
8184
       * a required parameter without a default value. This ensures that it cannot be
8185
       * used as an optional parameter even with named parameters. */
8186
810
      opcode = ZEND_RECV;
8187
810
      default_node.op_type = IS_UNUSED;
8188
810
      zval_ptr_dtor(&default_node.u.constant);
8189
810
    }
8190
8191
1.43M
    opline = zend_emit_op(NULL, opcode, NULL, &default_node);
8192
1.43M
    SET_NODE(opline->result, &var_node);
8193
1.43M
    opline->op1.num = i + 1;
8194
8195
1.43M
    uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic, /* is_tentative */ 0)
8196
1.43M
      | (is_promoted ? _ZEND_IS_PROMOTED_BIT : 0);
8197
1.43M
    ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags;
8198
1.43M
    if (opcode == ZEND_RECV) {
8199
1.43M
      opline->op2.num = type_ast ?
8200
844k
        ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY;
8201
1.43M
    }
8202
8203
1.43M
    if (is_promoted) {
8204
1.13k
      const zend_op_array *active_op_array = CG(active_op_array);
8205
1.13k
      zend_class_entry *scope = active_op_array->scope;
8206
8207
1.13k
      bool is_ctor =
8208
1.13k
        scope && zend_is_constructor(active_op_array->function_name);
8209
1.13k
      if (!is_ctor) {
8210
39
        zend_error_noreturn(E_COMPILE_ERROR,
8211
39
          "Cannot declare promoted property outside a constructor");
8212
39
      }
8213
1.09k
      if ((active_op_array->fn_flags & ZEND_ACC_ABSTRACT)
8214
1.08k
          || (scope->ce_flags & ZEND_ACC_INTERFACE)) {
8215
5
        zend_error_noreturn(E_COMPILE_ERROR,
8216
5
          "Cannot declare promoted property in an abstract constructor");
8217
5
      }
8218
1.08k
      if (is_variadic) {
8219
5
        zend_error_noreturn(E_COMPILE_ERROR,
8220
5
          "Cannot declare variadic promoted property");
8221
5
      }
8222
1.08k
      if (zend_hash_exists(&scope->properties_info, name)) {
8223
5
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
8224
5
          ZSTR_VAL(scope->name), ZSTR_VAL(name));
8225
5
      }
8226
1.07k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_CALLABLE) {
8227
5
        zend_string *str = zend_type_to_string(arg_info->type);
8228
5
        zend_error_noreturn(E_COMPILE_ERROR,
8229
5
          "Property %s::$%s cannot have type %s",
8230
5
          ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str));
8231
5
      }
8232
8233
1.07k
      if (!(property_flags & ZEND_ACC_READONLY) && (scope->ce_flags & ZEND_ACC_READONLY_CLASS)) {
8234
22
        property_flags |= ZEND_ACC_READONLY;
8235
22
      }
8236
8237
      /* Recompile the type, as it has different memory management requirements. */
8238
1.07k
      zend_type type = ZEND_TYPE_INIT_NONE(0);
8239
1.07k
      if (type_ast) {
8240
826
        type = zend_compile_typename(type_ast);
8241
826
      }
8242
8243
      /* Don't give the property an explicit default value. For typed properties this means
8244
       * uninitialized, for untyped properties it means an implicit null default value.
8245
       * Properties with hooks get an implicit default value of undefined until inheritance,
8246
       * where it is changed to null only once we know it is not virtual. If we were to set it
8247
       * here, we couldn't verify that a true virtual property must not have an explicit
8248
       * default value. */
8249
1.07k
      zval default_value;
8250
1.07k
      if (ZEND_TYPE_IS_SET(type) || hooks_ast) {
8251
878
        ZVAL_UNDEF(&default_value);
8252
878
      } else {
8253
194
        if (property_flags & ZEND_ACC_READONLY) {
8254
10
          zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
8255
10
            ZSTR_VAL(scope->name), ZSTR_VAL(name));
8256
10
        }
8257
8258
184
        ZVAL_NULL(&default_value);
8259
184
      }
8260
8261
1.06k
      zend_string *doc_comment =
8262
1.06k
        doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
8263
1.06k
      zend_property_info *prop = zend_declare_typed_property(
8264
1.06k
        scope, name, &default_value,
8265
1.06k
        property_flags | (zend_property_is_virtual(scope, name, hooks_ast) ? ZEND_ACC_VIRTUAL : 0) | ZEND_ACC_PROMOTED,
8266
1.06k
        doc_comment, type);
8267
1.06k
      if (hooks_ast) {
8268
124
        const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
8269
124
        zend_compile_property_hooks(prop, name, type_ast, hooks);
8270
124
      }
8271
1.06k
      if (attributes_ast) {
8272
62
        zend_compile_attributes(
8273
62
          &prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, ZEND_ATTRIBUTE_TARGET_PARAMETER);
8274
8275
62
        zend_attribute *override_attribute = zend_get_attribute_str(prop->attributes, "override", sizeof("override")-1);
8276
62
        if (override_attribute) {
8277
7
          prop->flags |= ZEND_ACC_OVERRIDE;
8278
7
        }
8279
62
      }
8280
1.06k
    }
8281
1.43M
  }
8282
8283
  /* These are assigned at the end to avoid uninitialized memory in case of an error */
8284
1.39M
  op_array->num_args = list->children;
8285
1.39M
  op_array->arg_info = arg_infos;
8286
8287
  /* Don't count the variadic argument */
8288
1.39M
  if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
8289
900
    op_array->num_args--;
8290
900
  }
8291
1.39M
  zend_set_function_arg_flags((zend_function*)op_array);
8292
8293
2.83M
  for (i = 0; i < list->children; i++) {
8294
1.43M
    zend_ast *param_ast = list->child[i];
8295
1.43M
    zend_ast *hooks_ast = param_ast->child[5];
8296
1.43M
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
8297
1.43M
    uint32_t flags = param_ast->attr & promotion_flags;
8298
1.43M
    bool is_promoted = flags || hooks_ast;
8299
1.43M
    if (!is_promoted) {
8300
1.43M
      continue;
8301
1.43M
    }
8302
8303
1.04k
    CG(zend_lineno) = param_ast->lineno;
8304
8305
    /* Emit $this->prop = $prop for promoted properties. */
8306
1.04k
    zend_string *name = zend_ast_get_str(param_ast->child[1]);
8307
1.04k
    znode name_node, value_node;
8308
1.04k
    name_node.op_type = IS_CONST;
8309
1.04k
    ZVAL_STR_COPY(&name_node.u.constant, name);
8310
1.04k
    value_node.op_type = IS_CV;
8311
1.04k
    value_node.u.op.var = lookup_cv(name);
8312
8313
1.04k
    zend_op *opline = zend_emit_op(NULL,
8314
1.04k
      is_ref ? ZEND_ASSIGN_OBJ_REF : ZEND_ASSIGN_OBJ, NULL, &name_node);
8315
1.04k
    opline->extended_value = zend_alloc_cache_slots(3);
8316
1.04k
    zend_emit_op_data(&value_node);
8317
1.04k
  }
8318
1.39M
}
8319
/* }}} */
8320
8321
static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */
8322
2.06k
{
8323
2.06k
  const zend_ast_list *list = zend_ast_get_list(uses_ast);
8324
2.06k
  uint32_t i;
8325
8326
2.06k
  if (!list->children) {
8327
0
    return;
8328
0
  }
8329
8330
2.06k
  if (!op_array->static_variables) {
8331
2.06k
    op_array->static_variables = zend_new_array(8);
8332
2.06k
  }
8333
8334
7.17k
  for (i = 0; i < list->children; ++i) {
8335
5.12k
    zend_ast *var_name_ast = list->child[i];
8336
5.12k
    zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast));
8337
5.12k
    uint32_t mode = var_name_ast->attr;
8338
5.12k
    zend_op *opline;
8339
5.12k
    zval *value;
8340
8341
5.12k
    if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8342
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
8343
5
    }
8344
8345
5.11k
    if (zend_is_auto_global(var_name)) {
8346
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable");
8347
5
    }
8348
8349
5.11k
    value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval));
8350
5.11k
    if (!value) {
8351
9
      zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8352
9
        "Cannot use variable $%S twice", var_name);
8353
9
    }
8354
8355
5.10k
    CG(zend_lineno) = zend_ast_get_lineno(var_name_ast);
8356
8357
5.10k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8358
5.10k
    opline->op2_type = IS_CV;
8359
5.10k
    opline->op2.var = lookup_cv(var_name);
8360
5.10k
    opline->extended_value =
8361
5.10k
      (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode;
8362
5.10k
  }
8363
2.06k
}
8364
/* }}} */
8365
8366
typedef struct {
8367
  HashTable uses;
8368
  bool varvars_used;
8369
} closure_info;
8370
8371
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast);
8372
8373
2.97M
static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
8374
2.97M
  if (!ast) {
8375
160k
    return;
8376
160k
  }
8377
8378
2.81M
  if (ast->kind == ZEND_AST_VAR) {
8379
164k
    zend_ast *name_ast = ast->child[0];
8380
164k
    if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) {
8381
162k
      zend_string *name = zend_ast_get_str(name_ast);
8382
162k
      if (zend_is_auto_global(name)) {
8383
        /* These is no need to explicitly import auto-globals. */
8384
2.27k
        return;
8385
2.27k
      }
8386
8387
160k
      if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8388
        /* $this does not need to be explicitly imported. */
8389
10.5k
        return;
8390
10.5k
      }
8391
8392
149k
      zend_hash_add_empty_element(&info->uses, name);
8393
149k
    } else {
8394
1.53k
      info->varvars_used = true;
8395
1.53k
      find_implicit_binds_recursively(info, name_ast);
8396
1.53k
    }
8397
2.65M
  } else if (zend_ast_is_list(ast)) {
8398
222k
    const zend_ast_list *list = zend_ast_get_list(ast);
8399
222k
    uint32_t i;
8400
673k
    for (i = 0; i < list->children; i++) {
8401
451k
      find_implicit_binds_recursively(info, list->child[i]);
8402
451k
    }
8403
2.43M
  } else if (ast->kind == ZEND_AST_CLOSURE) {
8404
    /* For normal closures add the use() list. */
8405
457
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8406
457
    zend_ast *uses_ast = closure_ast->child[1];
8407
457
    if (uses_ast) {
8408
249
      const zend_ast_list *uses_list = zend_ast_get_list(uses_ast);
8409
249
      uint32_t i;
8410
1.80k
      for (i = 0; i < uses_list->children; i++) {
8411
1.55k
        zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i]));
8412
1.55k
      }
8413
249
    }
8414
2.43M
  } else if (ast->kind == ZEND_AST_ARROW_FUNC) {
8415
    /* For arrow functions recursively check the expression. */
8416
545k
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8417
545k
    closure_info inner_info;
8418
545k
    find_implicit_binds(&inner_info, closure_ast->child[0], closure_ast->child[2]);
8419
545k
    if (inner_info.varvars_used) {
8420
8.51k
      info->varvars_used = true;
8421
8.51k
    }
8422
545k
    if (zend_hash_num_elements(&inner_info.uses)) {
8423
475k
      zend_hash_copy(&info->uses, &inner_info.uses, NULL);
8424
475k
    }
8425
545k
    zend_hash_destroy(&inner_info.uses);
8426
1.88M
  } else if (!zend_ast_is_special(ast)) {
8427
1.32M
    uint32_t i, children = zend_ast_get_num_children(ast);
8428
3.10M
    for (i = 0; i < children; i++) {
8429
1.77M
      find_implicit_binds_recursively(info, ast->child[i]);
8430
1.77M
    }
8431
1.32M
  }
8432
2.81M
}
8433
8434
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast)
8435
752k
{
8436
752k
  const zend_ast_list *param_list = zend_ast_get_list(params_ast);
8437
752k
  uint32_t i;
8438
8439
752k
  zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0);
8440
752k
  info->varvars_used = false;
8441
8442
752k
  find_implicit_binds_recursively(info, stmt_ast);
8443
8444
  /* Remove variables that are parameters */
8445
808k
  for (i = 0; i < param_list->children; i++) {
8446
55.8k
    const zend_ast *param_ast = param_list->child[i];
8447
55.8k
    zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1]));
8448
55.8k
  }
8449
752k
}
8450
8451
static void compile_implicit_lexical_binds(
8452
    const closure_info *info, znode *closure, zend_op_array *op_array)
8453
207k
{
8454
207k
  zend_string *var_name;
8455
207k
  zend_op *opline;
8456
8457
  /* TODO We might want to use a special binding mode if varvars_used is set. */
8458
207k
  if (zend_hash_num_elements(&info->uses) == 0) {
8459
198k
    return;
8460
198k
  }
8461
8462
8.84k
  if (!op_array->static_variables) {
8463
8.84k
    op_array->static_variables = zend_new_array(8);
8464
8.84k
  }
8465
8466
133k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name) {
8467
133k
    zval *value = zend_hash_add(
8468
133k
      op_array->static_variables, var_name, &EG(uninitialized_zval));
8469
133k
    uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData);
8470
8471
133k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8472
133k
    opline->op2_type = IS_CV;
8473
133k
    opline->op2.var = lookup_cv(var_name);
8474
133k
    opline->extended_value = offset | ZEND_BIND_IMPLICIT;
8475
133k
  } ZEND_HASH_FOREACH_END();
8476
8.84k
}
8477
8478
static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
8479
2.04k
{
8480
2.04k
  const zend_op_array *op_array = CG(active_op_array);
8481
2.04k
  const zend_ast_list *list = zend_ast_get_list(ast);
8482
2.04k
  uint32_t i;
8483
8484
7.12k
  for (i = 0; i < list->children; ++i) {
8485
5.07k
    uint32_t mode = ZEND_BIND_EXPLICIT;
8486
5.07k
    zend_ast *var_ast = list->child[i];
8487
5.07k
    zend_string *var_name = zend_ast_get_str(var_ast);
8488
5.07k
    zval zv;
8489
5.07k
    ZVAL_NULL(&zv);
8490
8491
5.07k
    {
8492
5.07k
      int i;
8493
14.6k
      for (i = 0; i < op_array->last_var; i++) {
8494
9.62k
        if (zend_string_equals(op_array->vars[i], var_name)) {
8495
5
          zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8496
5
            "Cannot use lexical variable $%S as a parameter name", var_name);
8497
5
        }
8498
9.62k
      }
8499
5.07k
    }
8500
8501
5.07k
    CG(zend_lineno) = zend_ast_get_lineno(var_ast);
8502
8503
5.07k
    if (var_ast->attr) {
8504
2.16k
      mode |= ZEND_BIND_REF;
8505
2.16k
    }
8506
8507
5.07k
    zend_compile_static_var_common(var_name, &zv, mode);
8508
5.07k
  }
8509
2.04k
}
8510
/* }}} */
8511
8512
static void zend_compile_implicit_closure_uses(const closure_info *info)
8513
206k
{
8514
206k
  zend_string *var_name;
8515
530k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name) {
8516
530k
    zval zv;
8517
530k
    ZVAL_NULL(&zv);
8518
530k
    zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT);
8519
530k
  } ZEND_HASH_FOREACH_END();
8520
206k
}
8521
8522
6.13k
static void add_stringable_interface(zend_class_entry *ce) {
8523
16.7k
  for (uint32_t i = 0; i < ce->num_interfaces; i++) {
8524
10.6k
    if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) {
8525
      /* Interface already explicitly implemented */
8526
46
      return;
8527
46
    }
8528
10.6k
  }
8529
8530
6.08k
  ce->num_interfaces++;
8531
6.08k
  ce->interface_names =
8532
6.08k
    erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
8533
  // TODO: Add known interned strings instead?
8534
6.08k
  ce->interface_names[ce->num_interfaces - 1].name =
8535
6.08k
    ZSTR_INIT_LITERAL("Stringable", 0);
8536
6.08k
  ce->interface_names[ce->num_interfaces - 1].lc_name =
8537
6.08k
    ZSTR_INIT_LITERAL("stringable", 0);
8538
6.08k
}
8539
8540
static zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, bool has_body) /* {{{ */
8541
129k
{
8542
129k
  zend_class_entry *ce = CG(active_class_entry);
8543
129k
  bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
8544
129k
  uint32_t fn_flags = op_array->fn_flags;
8545
8546
129k
  zend_string *lcname;
8547
8548
129k
  if (fn_flags & ZEND_ACC_READONLY) {
8549
0
    zend_error(E_COMPILE_ERROR, "Cannot use 'readonly' as method modifier");
8550
0
  }
8551
8552
129k
  if ((fn_flags & ZEND_ACC_PRIVATE) && (fn_flags & ZEND_ACC_FINAL) && !zend_is_constructor(name)) {
8553
179
    zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes");
8554
179
  }
8555
8556
129k
  if ((fn_flags & ZEND_ACC_ABSTRACT)
8557
492
   && !(ce->ce_flags & (ZEND_ACC_EXPLICIT_ABSTRACT_CLASS|ZEND_ACC_TRAIT))) {
8558
    // Don't say that the class should be declared abstract if it is
8559
    // anonymous or an enum and can't be abstract
8560
39
    if (ce->ce_flags & ZEND_ACC_ANON_CLASS) {
8561
5
      zend_error_noreturn(E_COMPILE_ERROR, "Anonymous class method %s() must not be abstract",
8562
5
        ZSTR_VAL(name));
8563
34
    } else if (ce->ce_flags & (ZEND_ACC_ENUM|ZEND_ACC_INTERFACE)) {
8564
23
      zend_error_noreturn(E_COMPILE_ERROR, "%s method %s::%s() must not be abstract",
8565
23
        zend_get_object_type_case(ce, true), ZSTR_VAL(ce->name), ZSTR_VAL(name));
8566
23
    } else {
8567
11
      zend_error_noreturn(E_COMPILE_ERROR, "Class %s declares abstract method %s() and must therefore be declared abstract",
8568
11
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
8569
11
    }
8570
39
  }
8571
8572
129k
  if (in_interface) {
8573
835
    if (!(fn_flags & ZEND_ACC_PUBLIC)) {
8574
1
      zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
8575
1
        "%s::%s() must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8576
1
    }
8577
834
    if (fn_flags & ZEND_ACC_FINAL) {
8578
5
      zend_error_noreturn(E_COMPILE_ERROR, "Interface method "
8579
5
        "%s::%s() must not be final", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8580
5
    }
8581
829
    op_array->fn_flags |= ZEND_ACC_ABSTRACT;
8582
829
  }
8583
8584
129k
  if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
8585
1.28k
    if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8586
5
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
8587
5
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8588
5
    }
8589
8590
1.27k
    if (has_body) {
8591
5
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
8592
5
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8593
5
    }
8594
8595
1.27k
    ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
8596
128k
  } else if (!has_body) {
8597
7
    zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
8598
7
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8599
7
  }
8600
8601
129k
  op_array->scope = ce;
8602
129k
  op_array->function_name = zend_string_copy(name);
8603
8604
129k
  lcname = zend_string_tolower(name);
8605
129k
  lcname = zend_new_interned_string(lcname);
8606
8607
129k
  if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
8608
31
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
8609
31
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8610
31
  }
8611
8612
129k
  zend_add_magic_method(ce, (zend_function *) op_array, lcname);
8613
129k
  if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)
8614
6.15k
      && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8615
6.13k
    add_stringable_interface(ce);
8616
6.13k
  }
8617
8618
129k
  return lcname;
8619
129k
}
8620
/* }}} */
8621
8622
1.69M
static uint32_t zend_add_dynamic_func_def(zend_op_array *def) {
8623
1.69M
  zend_op_array *op_array = CG(active_op_array);
8624
1.69M
  uint32_t def_offset = op_array->num_dynamic_func_defs++;
8625
1.69M
  op_array->dynamic_func_defs = erealloc(
8626
1.69M
    op_array->dynamic_func_defs, op_array->num_dynamic_func_defs * sizeof(zend_op_array *));
8627
1.69M
  op_array->dynamic_func_defs[def_offset] = def;
8628
1.69M
  return def_offset;
8629
1.69M
}
8630
8631
enum func_decl_level {
8632
  FUNC_DECL_LEVEL_TOPLEVEL,
8633
  FUNC_DECL_LEVEL_NESTED,
8634
  FUNC_DECL_LEVEL_CONSTEXPR,
8635
};
8636
8637
static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array, const zend_ast_decl *decl, enum func_decl_level level) /* {{{ */
8638
1.71M
{
8639
1.71M
  zend_string *unqualified_name, *name, *lcname;
8640
1.71M
  zend_op *opline;
8641
8642
1.71M
  if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8643
1.69M
    zend_string *filename = op_array->filename;
8644
1.69M
    uint32_t start_lineno = decl->start_lineno;
8645
8646
1.69M
    zend_string *class = zend_empty_string;
8647
1.69M
    zend_string *separator = zend_empty_string;
8648
1.69M
    zend_string *function = filename;
8649
1.69M
    const char *parens = "";
8650
8651
1.69M
    if (CG(active_op_array) && CG(active_op_array)->function_name) {
8652
1.68M
      if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
8653
        /* If the parent function is a closure, don't redundantly
8654
         * add the classname and parentheses.
8655
         */
8656
1.68M
        function = CG(active_op_array)->function_name;
8657
1.68M
      } else {
8658
1.72k
        function = CG(active_op_array)->function_name;
8659
1.72k
        parens = "()";
8660
8661
1.72k
        if (CG(active_class_entry) && CG(active_class_entry)->name) {
8662
1.08k
          class = CG(active_class_entry)->name;
8663
1.08k
          separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM);
8664
1.08k
        }
8665
1.72k
      }
8666
1.68M
    }
8667
8668
1.69M
    unqualified_name = zend_strpprintf_unchecked(
8669
1.69M
      0,
8670
1.69M
      "{closure:%S%S%S%s:%" PRIu32 "}",
8671
1.69M
      class,
8672
1.69M
      separator,
8673
1.69M
      function,
8674
1.69M
      parens,
8675
1.69M
      start_lineno
8676
1.69M
    );
8677
8678
1.69M
    op_array->function_name = name = unqualified_name;
8679
1.69M
  } else {
8680
18.4k
    unqualified_name = decl->name;
8681
18.4k
    op_array->function_name = name = zend_prefix_with_ns(unqualified_name);
8682
18.4k
  }
8683
8684
1.71M
  lcname = zend_string_tolower(name);
8685
8686
1.71M
  if (FC(imports_function)) {
8687
106
    const zend_string *import_name =
8688
106
      zend_hash_find_ptr_lc(FC(imports_function), unqualified_name);
8689
106
    if (import_name && !zend_string_equals_ci(lcname, import_name)) {
8690
9
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare function %s() (previously declared as local import)",
8691
9
        ZSTR_VAL(name));
8692
9
    }
8693
106
  }
8694
8695
1.71M
  if (zend_string_equals_literal(lcname, "__autoload")) {
8696
1
    zend_error_noreturn(E_COMPILE_ERROR,
8697
1
      "__autoload() is no longer supported, use spl_autoload_register() instead");
8698
1
  }
8699
8700
1.71M
  if (zend_string_equals_literal_ci(unqualified_name, "assert")) {
8701
5
    zend_error(E_COMPILE_ERROR,
8702
5
      "Defining a custom assert() function is not allowed, "
8703
5
      "as the function has special semantics");
8704
5
  }
8705
8706
1.71M
  zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
8707
1.71M
  switch (level) {
8708
1.69M
    case FUNC_DECL_LEVEL_NESTED: {
8709
1.69M
      uint32_t func_ref = zend_add_dynamic_func_def(op_array);
8710
1.69M
      if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8711
1.69M
        opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
8712
1.69M
        opline->op2.num = func_ref;
8713
1.69M
      } else {
8714
1.57k
        opline = get_next_op();
8715
1.57k
        opline->opcode = ZEND_DECLARE_FUNCTION;
8716
1.57k
        opline->op1_type = IS_CONST;
8717
1.57k
        LITERAL_STR(opline->op1, zend_string_copy(lcname));
8718
1.57k
        opline->op2.num = func_ref;
8719
1.57k
      }
8720
1.69M
      break;
8721
0
    }
8722
122
    case FUNC_DECL_LEVEL_CONSTEXPR:
8723
16.9k
    case FUNC_DECL_LEVEL_TOPLEVEL:
8724
      /* Nothing to do. */
8725
16.9k
      break;
8726
1.71M
  }
8727
1.71M
  return lcname;
8728
1.71M
}
8729
/* }}} */
8730
8731
static zend_op_array *zend_compile_func_decl_ex(
8732
  znode *result, zend_ast *ast, enum func_decl_level level,
8733
  zend_string *property_info_name,
8734
  zend_property_hook_kind hook_kind
8735
1.84M
) {
8736
1.84M
  zend_ast_decl *decl = (zend_ast_decl *) ast;
8737
1.84M
  zend_ast *params_ast = decl->child[0];
8738
1.84M
  zend_ast *uses_ast = decl->child[1];
8739
1.84M
  zend_ast *stmt_ast = decl->child[2];
8740
1.84M
  zend_ast *return_type_ast = decl->child[3];
8741
1.84M
  bool is_method = decl->kind == ZEND_AST_METHOD;
8742
1.84M
  zend_string *lcname = NULL;
8743
1.84M
  bool is_hook = decl->kind == ZEND_AST_PROPERTY_HOOK;
8744
8745
1.84M
  zend_class_entry *orig_class_entry = CG(active_class_entry);
8746
1.84M
  zend_op_array *orig_op_array = CG(active_op_array);
8747
1.84M
  zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
8748
1.84M
  zend_oparray_context orig_oparray_context;
8749
1.84M
  closure_info info;
8750
8751
1.84M
  init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
8752
8753
1.84M
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
8754
0
    op_array->fn_flags |= ZEND_ACC_PRELOADED;
8755
0
  }
8756
8757
1.84M
  op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
8758
1.84M
  op_array->fn_flags |= decl->flags;
8759
1.84M
  op_array->line_start = decl->start_lineno;
8760
1.84M
  op_array->line_end = decl->end_lineno;
8761
1.84M
  if (decl->doc_comment) {
8762
384
    op_array->doc_comment = zend_string_copy(decl->doc_comment);
8763
384
  }
8764
8765
1.84M
  if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
8766
1.69M
    op_array->fn_flags |= ZEND_ACC_CLOSURE;
8767
1.69M
  }
8768
8769
1.84M
  if (is_hook) {
8770
4.81k
    zend_class_entry *ce = CG(active_class_entry);
8771
4.81k
    op_array->scope = ce;
8772
4.81k
    op_array->function_name = zend_string_copy(decl->name);
8773
1.84M
  } else if (is_method) {
8774
129k
    bool has_body = stmt_ast != NULL;
8775
129k
    lcname = zend_begin_method_decl(op_array, decl->name, has_body);
8776
1.71M
  } else {
8777
1.71M
    lcname = zend_begin_func_decl(result, op_array, decl, level);
8778
1.71M
    if (decl->kind == ZEND_AST_ARROW_FUNC) {
8779
207k
      find_implicit_binds(&info, params_ast, stmt_ast);
8780
207k
      compile_implicit_lexical_binds(&info, result, op_array);
8781
1.50M
    } else if (uses_ast) {
8782
2.06k
      zend_compile_closure_binding(result, op_array, uses_ast);
8783
2.06k
    }
8784
1.71M
  }
8785
8786
1.84M
  CG(active_op_array) = op_array;
8787
8788
1.84M
  zend_oparray_context_begin(&orig_oparray_context, op_array);
8789
1.84M
  CG(context).active_property_info_name = property_info_name;
8790
1.84M
  CG(context).active_property_hook_kind = hook_kind;
8791
8792
1.84M
  if (decl->child[4]) {
8793
1.27M
    int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
8794
8795
1.27M
    if (is_method || is_hook) {
8796
928
      target = ZEND_ATTRIBUTE_TARGET_METHOD;
8797
928
    }
8798
8799
1.27M
    zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target, 0);
8800
8801
1.27M
    const zend_attribute *override_attribute = zend_get_attribute_str(
8802
1.27M
      op_array->attributes,
8803
1.27M
      "override",
8804
1.27M
      sizeof("override")-1
8805
1.27M
    );
8806
8807
1.27M
    if (override_attribute) {
8808
405
      op_array->fn_flags |= ZEND_ACC_OVERRIDE;
8809
405
    }
8810
8811
1.27M
    const zend_attribute *deprecated_attribute = zend_get_attribute_str(
8812
1.27M
      op_array->attributes,
8813
1.27M
      "deprecated",
8814
1.27M
      sizeof("deprecated")-1
8815
1.27M
    );
8816
8817
1.27M
    if (deprecated_attribute) {
8818
267
      op_array->fn_flags |= ZEND_ACC_DEPRECATED;
8819
267
    }
8820
8821
    // ZEND_ACC_NODISCARD is added via an attribute validator
8822
1.27M
  }
8823
8824
  /* Do not leak the class scope into free standing functions, even if they are dynamically
8825
   * defined inside a class method. This is necessary for correct handling of magic constants.
8826
   * For example __CLASS__ should always be "" inside a free standing function. */
8827
1.84M
  if (decl->kind == ZEND_AST_FUNC_DECL) {
8828
18.3k
    CG(active_class_entry) = NULL;
8829
18.3k
  }
8830
8831
1.84M
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8832
16.8k
    op_array->fn_flags |= ZEND_ACC_TOP_LEVEL;
8833
16.8k
  }
8834
8835
1.84M
  {
8836
    /* Push a separator to the loop variable stack */
8837
1.84M
    zend_loop_var dummy_var;
8838
1.84M
    dummy_var.opcode = ZEND_RETURN;
8839
8840
1.84M
    zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
8841
1.84M
  }
8842
8843
1.84M
  zend_compile_params(params_ast, return_type_ast,
8844
1.84M
    is_method && zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME) ? IS_STRING : 0);
8845
1.84M
  if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
8846
294k
    zend_mark_function_as_generator();
8847
294k
    zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
8848
294k
  }
8849
1.84M
  if (decl->kind == ZEND_AST_ARROW_FUNC) {
8850
206k
    zend_compile_implicit_closure_uses(&info);
8851
206k
    zend_hash_destroy(&info.uses);
8852
1.64M
  } else if (uses_ast) {
8853
2.04k
    zend_compile_closure_uses(uses_ast);
8854
2.04k
  }
8855
8856
1.84M
  if (ast->kind == ZEND_AST_ARROW_FUNC && decl->child[2]->kind != ZEND_AST_RETURN) {
8857
11.4k
    bool needs_return = true;
8858
11.4k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8859
1.22k
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8860
1.22k
      needs_return = !ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER);
8861
1.22k
    }
8862
11.4k
    if (needs_return) {
8863
11.0k
      stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
8864
11.0k
      decl->child[2] = stmt_ast;
8865
11.0k
    }
8866
11.4k
  }
8867
8868
1.84M
  if (op_array->fn_flags & ZEND_ACC_NODISCARD) {
8869
    /* ZEND_ACC_NODISCARD gets added by the attribute validator, but only
8870
     * if the method is not a hook; if it is a hook, then the validator
8871
     * will have returned an error message, even if the error message was
8872
     * delayed with #[\DelayedTargetValidation] that ZEND_ACC_NODISCARD
8873
     * flag should not have been added. */
8874
254
    ZEND_ASSERT(!is_hook);
8875
8876
254
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8877
198
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8878
198
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_VOID)) {
8879
5
        zend_error_noreturn(E_COMPILE_ERROR,
8880
5
          "A void %s does not return a value, but #[\\NoDiscard] requires a return value",
8881
5
          CG(active_class_entry) != NULL ? "method" : "function");
8882
5
      }
8883
8884
193
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
8885
5
        zend_error_noreturn(E_COMPILE_ERROR,
8886
5
          "A never returning %s does not return a value, but #[\\NoDiscard] requires a return value",
8887
5
          CG(active_class_entry) != NULL ? "method" : "function");
8888
5
      }
8889
193
    }
8890
254
  }
8891
8892
1.84M
  zend_compile_stmt(stmt_ast);
8893
8894
1.84M
  if (is_method) {
8895
129k
    CG(zend_lineno) = decl->start_lineno;
8896
129k
    zend_check_magic_method_implementation(
8897
129k
      CG(active_class_entry), (zend_function *) op_array, lcname, E_COMPILE_ERROR);
8898
1.71M
  } else if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8899
    /* Only register the function after a successful compile */
8900
16.3k
    if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
8901
97
      CG(zend_lineno) = decl->start_lineno;
8902
97
      do_bind_function_error(lcname, op_array, true);
8903
97
    }
8904
16.3k
  }
8905
8906
  /* put the implicit return on the really last line */
8907
1.84M
  CG(zend_lineno) = decl->end_lineno;
8908
8909
1.84M
  zend_do_extended_stmt(NULL);
8910
1.84M
  zend_emit_final_return(false);
8911
8912
1.84M
  pass_two(CG(active_op_array));
8913
1.84M
  zend_oparray_context_end(&orig_oparray_context);
8914
8915
  /* Pop the loop variable stack separator */
8916
1.84M
  zend_stack_del_top(&CG(loop_var_stack));
8917
8918
1.84M
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8919
16.2k
    zend_observer_function_declared_notify(op_array, lcname);
8920
16.2k
  }
8921
8922
1.84M
  if (lcname != NULL) {
8923
1.84M
    zend_string_release_ex(lcname, 0);
8924
1.84M
  }
8925
8926
1.84M
  CG(active_op_array) = orig_op_array;
8927
1.84M
  CG(active_class_entry) = orig_class_entry;
8928
8929
1.84M
  return op_array;
8930
1.84M
}
8931
8932
static zend_op_array *zend_compile_func_decl(znode *result, zend_ast *ast, enum func_decl_level level)
8933
1.84M
{
8934
1.84M
  return zend_compile_func_decl_ex(result, ast, level, /* property_info */ NULL, (zend_property_hook_kind)-1);
8935
1.84M
}
8936
8937
5.65k
zend_property_hook_kind zend_get_property_hook_kind_from_name(const zend_string *name) {
8938
5.65k
  if (zend_string_equals_literal_ci(name, "get")) {
8939
3.67k
    return ZEND_PROPERTY_HOOK_GET;
8940
3.67k
  } else if (zend_string_equals_literal_ci(name, "set")) {
8941
1.85k
    return ZEND_PROPERTY_HOOK_SET;
8942
1.85k
  } else {
8943
116
    return (zend_property_hook_kind)-1;
8944
116
  }
8945
5.65k
}
8946
8947
static void zend_compile_property_hooks(
8948
    zend_property_info *prop_info, zend_string *prop_name,
8949
    zend_ast *prop_type_ast, const zend_ast_list *hooks)
8950
4.07k
{
8951
4.07k
  zend_class_entry *ce = CG(active_class_entry);
8952
8953
4.07k
  if (prop_info->flags & ZEND_ACC_READONLY) {
8954
15
    zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly");
8955
15
  }
8956
8957
4.06k
  if (hooks->children == 0) {
8958
21
    zend_error_noreturn(E_COMPILE_ERROR, "Property hook list must not be empty");
8959
21
  }
8960
8961
8.82k
  for (uint32_t i = 0; i < hooks->children; i++) {
8962
5.02k
    zend_ast_decl *hook = (zend_ast_decl *) hooks->child[i];
8963
5.02k
    zend_string *name = hook->name;
8964
5.02k
    zend_ast *stmt_ast = hook->child[2];
8965
5.02k
    zend_ast **return_type_ast_ptr = NULL;
8966
5.02k
    zend_ast **value_type_ast_ptr = NULL;
8967
5.02k
    CG(zend_lineno) = hook->start_lineno;
8968
8969
    /* Non-private hooks are always public. This avoids having to copy the hook when inheriting
8970
     * hooks from protected properties to public ones. */
8971
5.02k
    uint32_t hook_visibility = (prop_info->flags & ZEND_ACC_PPP_MASK) != ZEND_ACC_PRIVATE ? ZEND_ACC_PUBLIC : ZEND_ACC_PRIVATE;
8972
5.02k
    hook->flags |= hook_visibility;
8973
8974
5.02k
    if (prop_info->flags & ZEND_ACC_STATIC) {
8975
8
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare hooks for static property");
8976
8
    }
8977
5.01k
    if ((hook->flags & ZEND_ACC_FINAL) && (hook->flags & ZEND_ACC_PRIVATE)) {
8978
5
      zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both final and private");
8979
5
    }
8980
5.00k
    if ((ce->ce_flags & ZEND_ACC_INTERFACE)
8981
4.71k
     || ((prop_info->flags & ZEND_ACC_ABSTRACT) && !stmt_ast)) {
8982
710
      hook->flags |= ZEND_ACC_ABSTRACT;
8983
8984
710
      if (stmt_ast) {
8985
20
        zend_error_noreturn(E_COMPILE_ERROR, "Abstract property hook cannot have body");
8986
20
      }
8987
690
      if (hook->flags & ZEND_ACC_PRIVATE) {
8988
5
        zend_error_noreturn(E_COMPILE_ERROR,
8989
5
          "Property hook cannot be both abstract and private");
8990
5
      }
8991
685
      if (hook->flags & ZEND_ACC_FINAL) {
8992
6
        zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both abstract and final");
8993
6
      }
8994
4.29k
    } else if (!stmt_ast) {
8995
18
      zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract property hook must have a body");
8996
18
    }
8997
8998
4.95k
    zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(name);
8999
4.95k
    if (hook_kind == (zend_property_hook_kind)-1) {
9000
116
      zend_error_noreturn(E_COMPILE_ERROR,
9001
116
        "Unknown hook \"%s\" for property %s::$%s, expected \"get\" or \"set\"",
9002
116
        ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9003
116
    }
9004
9005
4.84k
    if (stmt_ast && stmt_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
9006
1.94k
      stmt_ast = stmt_ast->child[0];
9007
1.94k
      if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
9008
1.60k
        stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
9009
1.60k
      } else {
9010
346
        ZEND_ASSERT(hook_kind == ZEND_PROPERTY_HOOK_SET);
9011
346
        stmt_ast = zend_ast_create(ZEND_AST_ASSIGN,
9012
346
          zend_ast_create(ZEND_AST_PROP,
9013
346
            zend_ast_create(ZEND_AST_VAR, zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_THIS))),
9014
346
            zend_ast_create_zval_from_str(zend_copy_unmangled_prop_name(prop_info->name))),
9015
346
          stmt_ast);
9016
346
      }
9017
1.94k
      stmt_ast = zend_ast_create_list(1, ZEND_AST_STMT_LIST, stmt_ast);
9018
1.94k
      hook->child[2] = stmt_ast;
9019
1.94k
    }
9020
9021
4.84k
    if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
9022
3.10k
      if (hook->child[0]) {
9023
7
        zend_error_noreturn(E_COMPILE_ERROR, "get hook of property %s::$%s must not have a parameter list",
9024
7
          ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9025
7
      }
9026
9027
3.10k
      hook->child[0] = zend_ast_create_list(0, ZEND_AST_PARAM_LIST);
9028
9029
3.10k
      return_type_ast_ptr = &hook->child[3];
9030
3.10k
      *return_type_ast_ptr = prop_type_ast;
9031
3.10k
    } else if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
9032
1.73k
      if (hook->child[0]) {
9033
178
        const zend_ast_list *param_list = zend_ast_get_list(hook->child[0]);
9034
178
        if (param_list->children != 1) {
9035
1
          zend_error_noreturn(E_COMPILE_ERROR, "%s hook of property %s::$%s must accept exactly one parameters",
9036
1
            ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9037
1
        }
9038
177
        const zend_ast *value_param_ast = param_list->child[0];
9039
177
        if (value_param_ast->attr & ZEND_PARAM_REF) {
9040
5
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be pass-by-reference",
9041
5
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9042
5
        }
9043
172
        if (value_param_ast->attr & ZEND_PARAM_VARIADIC) {
9044
5
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be variadic",
9045
5
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9046
5
        }
9047
167
        if (value_param_ast->child[2]) {
9048
5
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not have a default value",
9049
5
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9050
5
        }
9051
162
        if ((prop_type_ast != NULL) != (value_param_ast->child[0] != NULL)) {
9052
5
          zend_hooked_property_variance_error_ex(zend_ast_get_str(value_param_ast->child[1]), ce->name, prop_info->name);
9053
5
        }
9054
1.55k
      } else {
9055
1.55k
        zend_ast *param_name_ast = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VALUE));
9056
1.55k
        zend_ast *param = zend_ast_create(
9057
1.55k
          ZEND_AST_PARAM, prop_type_ast, param_name_ast,
9058
1.55k
          /* expr */ NULL, /* doc_comment */ NULL, /* attributes */ NULL,
9059
1.55k
          /* hooks */ NULL);
9060
1.55k
        value_type_ast_ptr = &param->child[0];
9061
1.55k
        hook->child[0] = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, param);
9062
1.55k
      }
9063
1.71k
      zend_ast *return_type = zend_ast_create(ZEND_AST_TYPE);
9064
1.71k
      return_type->attr = IS_VOID;
9065
1.71k
      hook->child[3] = return_type;
9066
1.71k
    } else {
9067
0
      ZEND_UNREACHABLE();
9068
0
    }
9069
9070
4.81k
    hook->name = zend_strpprintf(0, "$%s::%s", ZSTR_VAL(prop_name), ZSTR_VAL(name));
9071
9072
4.81k
    zend_function *func = (zend_function *) zend_compile_func_decl_ex(
9073
4.81k
      NULL, (zend_ast *) hook, FUNC_DECL_LEVEL_NESTED, prop_info->name, hook_kind);
9074
9075
4.81k
    func->common.prop_info = prop_info;
9076
9077
4.81k
    if (!prop_info->hooks) {
9078
3.81k
      prop_info->hooks = zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE);
9079
3.81k
      memset(prop_info->hooks, 0, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
9080
3.81k
    }
9081
9082
4.81k
    if (prop_info->hooks[hook_kind]) {
9083
24
      zend_error_noreturn(E_COMPILE_ERROR,
9084
24
        "Cannot redeclare property hook \"%s\"", ZSTR_VAL(name));
9085
24
    }
9086
4.79k
    prop_info->hooks[hook_kind] = func;
9087
9088
4.79k
    if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
9089
1.68k
      switch (zend_verify_property_hook_variance(prop_info, func)) {
9090
1.63k
        case INHERITANCE_SUCCESS:
9091
1.63k
          break;
9092
49
        case INHERITANCE_UNRESOLVED:
9093
49
          ce->num_hooked_prop_variance_checks++;
9094
49
          break;
9095
6
        case INHERITANCE_ERROR:
9096
6
          zend_hooked_property_variance_error(prop_info);
9097
0
        case INHERITANCE_WARNING:
9098
0
          ZEND_UNREACHABLE();
9099
1.68k
      }
9100
1.68k
    }
9101
9102
4.78k
    zend_string_release(name);
9103
    /* Un-share type ASTs to avoid double-frees of zval nodes. */
9104
4.78k
    if (return_type_ast_ptr) {
9105
3.05k
      *return_type_ast_ptr = NULL;
9106
3.05k
    }
9107
4.78k
    if (value_type_ast_ptr) {
9108
1.53k
      *value_type_ast_ptr = NULL;
9109
1.53k
    }
9110
4.78k
  }
9111
9112
3.80k
  ce->num_hooked_props++;
9113
9114
  /* See zend_link_hooked_object_iter(). */
9115
3.80k
#ifndef ZEND_OPCACHE_SHM_REATTACHMENT
9116
3.80k
  if (!ce->get_iterator) {
9117
    /* Will be removed again, in case of Iterator or IteratorAggregate. */
9118
2.77k
    ce->get_iterator = zend_hooked_object_get_iterator;
9119
2.77k
  }
9120
3.80k
#endif
9121
9122
3.80k
  if (!prop_info->ce->parent_name) {
9123
2.41k
    zend_verify_hooked_property(ce, prop_info, prop_name);
9124
2.41k
  }
9125
3.80k
}
9126
9127
static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */
9128
32.4k
{
9129
32.4k
  const zend_ast_list *list = zend_ast_get_list(ast);
9130
32.4k
  zend_class_entry *ce = CG(active_class_entry);
9131
32.4k
  uint32_t i, children = list->children;
9132
9133
32.4k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
9134
13
    zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", ZSTR_VAL(ce->name));
9135
13
  }
9136
9137
32.4k
  if ((flags & ZEND_ACC_FINAL) && (flags & ZEND_ACC_PRIVATE)) {
9138
5
    zend_error_noreturn(E_COMPILE_ERROR, "Property cannot be both final and private");
9139
5
  }
9140
9141
32.4k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9142
304
    if (flags & ZEND_ACC_FINAL) {
9143
5
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be final");
9144
5
    }
9145
299
    if (flags & (ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE)) {
9146
5
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be protected or private");
9147
5
    }
9148
294
    if (flags & ZEND_ACC_ABSTRACT) {
9149
5
      zend_error_noreturn(E_COMPILE_ERROR,
9150
5
        "Property in interface cannot be explicitly abstract. "
9151
5
        "All interface members are implicitly abstract");
9152
5
    }
9153
289
    flags |= ZEND_ACC_ABSTRACT;
9154
289
  }
9155
9156
65.5k
  for (i = 0; i < children; ++i) {
9157
33.3k
    zend_property_info *info;
9158
33.3k
    zend_ast *prop_ast = list->child[i];
9159
33.3k
    zend_ast *name_ast = prop_ast->child[0];
9160
33.3k
    zend_ast **value_ast_ptr = &prop_ast->child[1];
9161
33.3k
    zend_ast *doc_comment_ast = prop_ast->child[2];
9162
33.3k
    zend_ast *hooks_ast = prop_ast->child[3];
9163
33.3k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
9164
33.3k
    zend_string *doc_comment = NULL;
9165
33.3k
    zval value_zv;
9166
33.3k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
9167
33.3k
    flags |= zend_property_is_virtual(ce, name, hooks_ast) ? ZEND_ACC_VIRTUAL : 0;
9168
9169
33.3k
    zend_string *old_active_property_info_name = CG(context).active_property_info_name;
9170
33.3k
    CG(context).active_property_info_name = name;
9171
9172
33.3k
    if (!hooks_ast) {
9173
29.3k
      if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9174
5
        zend_error_noreturn(E_COMPILE_ERROR,
9175
5
          "Interfaces may only include hooked properties");
9176
5
      }
9177
29.3k
      if (flags & ZEND_ACC_ABSTRACT) {
9178
5
        zend_error_noreturn(E_COMPILE_ERROR,
9179
5
          "Only hooked properties may be declared abstract");
9180
5
      }
9181
29.3k
    }
9182
33.3k
    if ((flags & ZEND_ACC_ABSTRACT)) {
9183
645
      ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
9184
645
    }
9185
9186
33.3k
    if (type_ast) {
9187
18.5k
      type = zend_compile_typename(type_ast);
9188
9189
18.5k
      if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_NEVER|MAY_BE_CALLABLE)) {
9190
5
        zend_string *str = zend_type_to_string(type);
9191
5
        zend_error_noreturn(E_COMPILE_ERROR,
9192
5
          "Property %s::$%s cannot have type %s",
9193
5
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9194
5
      }
9195
18.5k
    }
9196
9197
    /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */
9198
33.2k
    if (doc_comment_ast) {
9199
415
      doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9200
415
    }
9201
9202
33.2k
    if (zend_hash_exists(&ce->properties_info, name)) {
9203
47
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
9204
47
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
9205
47
    }
9206
9207
33.2k
    if (*value_ast_ptr) {
9208
9.06k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9209
9210
9.06k
      if (ZEND_TYPE_IS_SET(type) && !Z_CONSTANT(value_zv)
9211
2.11k
          && !zend_is_valid_default_value(type, &value_zv)) {
9212
83
        zend_string *str = zend_type_to_string(type);
9213
83
        if (Z_TYPE(value_zv) == IS_NULL && !ZEND_TYPE_IS_INTERSECTION(type)) {
9214
22
          ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
9215
22
          zend_string *nullable_str = zend_type_to_string(type);
9216
9217
22
          zend_error_noreturn(E_COMPILE_ERROR,
9218
22
            "Default value for property of type %s may not be null. "
9219
22
            "Use the nullable type %s to allow null default value",
9220
22
            ZSTR_VAL(str), ZSTR_VAL(nullable_str));
9221
61
        } else {
9222
61
          zend_error_noreturn(E_COMPILE_ERROR,
9223
61
            "Cannot use %s as default value for property %s::$%s of type %s",
9224
61
            zend_zval_value_name(&value_zv),
9225
61
            ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9226
61
        }
9227
83
      }
9228
24.1k
    } else if (!ZEND_TYPE_IS_SET(type) && !hooks_ast) {
9229
6.08k
      ZVAL_NULL(&value_zv);
9230
18.1k
    } else {
9231
18.1k
      ZVAL_UNDEF(&value_zv);
9232
18.1k
    }
9233
9234
33.1k
    if ((ce->ce_flags & ZEND_ACC_READONLY_CLASS)) {
9235
29
      flags |= ZEND_ACC_READONLY;
9236
29
    }
9237
9238
33.1k
    if (flags & ZEND_ACC_READONLY) {
9239
525
      if (!ZEND_TYPE_IS_SET(type)) {
9240
10
        zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
9241
10
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9242
10
      }
9243
515
      if (!Z_ISUNDEF(value_zv)) {
9244
7
        zend_error_noreturn(E_COMPILE_ERROR,
9245
7
          "Readonly property %s::$%s cannot have default value",
9246
7
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9247
7
      }
9248
508
      if (flags & ZEND_ACC_STATIC) {
9249
9
        zend_error_noreturn(E_COMPILE_ERROR,
9250
9
          "Static property %s::$%s cannot be readonly",
9251
9
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9252
9
      }
9253
508
    }
9254
9255
33.1k
    info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type);
9256
9257
33.1k
    if (hooks_ast) {
9258
3.95k
      zend_compile_property_hooks(info, name, type_ast, zend_ast_get_list(hooks_ast));
9259
3.95k
    }
9260
9261
33.1k
    if (attr_ast) {
9262
1.81k
      zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0);
9263
9264
1.81k
      const zend_attribute *override_attribute = zend_get_attribute_str(info->attributes, "override", sizeof("override")-1);
9265
1.81k
      if (override_attribute) {
9266
125
        info->flags |= ZEND_ACC_OVERRIDE;
9267
125
      }
9268
1.81k
    }
9269
9270
33.1k
    CG(context).active_property_info_name = old_active_property_info_name;
9271
33.1k
  }
9272
32.4k
}
9273
/* }}} */
9274
9275
static void zend_compile_prop_group(const zend_ast *ast) /* {{{ */
9276
32.4k
{
9277
32.4k
  zend_ast *type_ast = ast->child[0];
9278
32.4k
  zend_ast *prop_ast = ast->child[1];
9279
32.4k
  zend_ast *attr_ast = ast->child[2];
9280
9281
32.4k
  zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast);
9282
32.4k
}
9283
/* }}} */
9284
9285
static void zend_check_trait_alias_modifiers(uint32_t attr) /* {{{ */
9286
331k
{
9287
331k
  if (attr & ZEND_ACC_STATIC) {
9288
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"static\" as method modifier in trait alias");
9289
331k
  } else if (attr & ZEND_ACC_ABSTRACT) {
9290
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"abstract\" as method modifier in trait alias");
9291
5
  }
9292
331k
}
9293
/* }}} */
9294
9295
static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast, zend_ast *type_ast)
9296
6.44k
{
9297
6.44k
  const zend_ast_list *list = zend_ast_get_list(ast);
9298
6.44k
  zend_class_entry *ce = CG(active_class_entry);
9299
6.44k
  uint32_t i, children = list->children;
9300
9301
12.8k
  for (i = 0; i < children; ++i) {
9302
6.49k
    zend_class_constant *c;
9303
6.49k
    zend_ast *const_ast = list->child[i];
9304
6.49k
    zend_ast *name_ast = const_ast->child[0];
9305
6.49k
    zend_ast **value_ast_ptr = &const_ast->child[1];
9306
6.49k
    zend_ast *doc_comment_ast = const_ast->child[2];
9307
6.49k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
9308
6.49k
    zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
9309
6.49k
    zval value_zv;
9310
6.49k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
9311
9312
6.49k
    if (type_ast) {
9313
2.33k
      type = zend_compile_typename(type_ast);
9314
9315
2.33k
      uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9316
9317
2.33k
      if (type_mask != MAY_BE_ANY && (type_mask & (MAY_BE_CALLABLE|MAY_BE_VOID|MAY_BE_NEVER))) {
9318
5
        zend_string *type_str = zend_type_to_string(type);
9319
9320
5
        zend_error_noreturn(E_COMPILE_ERROR, "Class constant %s::%s cannot have type %s",
9321
5
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9322
5
      }
9323
2.33k
    }
9324
9325
6.49k
    if (UNEXPECTED((flags & ZEND_ACC_PRIVATE) && (flags & ZEND_ACC_FINAL))) {
9326
5
      zend_error_noreturn(
9327
5
        E_COMPILE_ERROR, "Private constant %s::%s cannot be final as it is not visible to other classes",
9328
5
        ZSTR_VAL(ce->name), ZSTR_VAL(name)
9329
5
      );
9330
5
    }
9331
9332
6.48k
    zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9333
9334
6.48k
    if (!Z_CONSTANT(value_zv) && ZEND_TYPE_IS_SET(type) && !zend_is_valid_default_value(type, &value_zv)) {
9335
35
      zend_string *type_str = zend_type_to_string(type);
9336
9337
35
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as value for class constant %s::%s of type %s",
9338
35
        zend_zval_type_name(&value_zv), ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9339
35
    }
9340
9341
6.45k
    c = zend_declare_typed_class_constant(ce, name, &value_zv, flags, doc_comment, type);
9342
9343
6.45k
    if (attr_ast) {
9344
502
      zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9345
9346
502
      const zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9347
9348
502
      if (deprecated) {
9349
146
        ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9350
        /* For deprecated constants, we need to flag the zval for recursion
9351
         * detection. Make sure the zval is separated out of shm. */
9352
146
        ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
9353
146
        ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
9354
146
      }
9355
502
    }
9356
6.45k
  }
9357
6.44k
}
9358
9359
static void zend_compile_class_const_group(const zend_ast *ast) /* {{{ */
9360
6.44k
{
9361
6.44k
  zend_ast *const_ast = ast->child[0];
9362
6.44k
  zend_ast *attr_ast = ast->child[1];
9363
6.44k
  zend_ast *type_ast = ast->child[2];
9364
9365
6.44k
  zend_compile_class_const_decl(const_ast, ast->attr, attr_ast, type_ast);
9366
6.44k
}
9367
/* }}} */
9368
9369
static void zend_compile_method_ref(const zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */
9370
332k
{
9371
332k
  zend_ast *class_ast = ast->child[0];
9372
332k
  zend_ast *method_ast = ast->child[1];
9373
9374
332k
  method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
9375
9376
332k
  if (class_ast) {
9377
1.93k
    method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name");
9378
330k
  } else {
9379
330k
    method_ref->class_name = NULL;
9380
330k
  }
9381
332k
}
9382
/* }}} */
9383
9384
static void zend_compile_trait_precedence(const zend_ast *ast) /* {{{ */
9385
868
{
9386
868
  const zend_ast *method_ref_ast = ast->child[0];
9387
868
  zend_ast *insteadof_ast = ast->child[1];
9388
868
  const zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast);
9389
868
  uint32_t i;
9390
9391
868
  zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*));
9392
868
  zend_compile_method_ref(method_ref_ast, &precedence->trait_method);
9393
868
  precedence->num_excludes = insteadof_list->children;
9394
9395
2.17k
  for (i = 0; i < insteadof_list->children; ++i) {
9396
1.30k
    zend_ast *name_ast = insteadof_list->child[i];
9397
1.30k
    precedence->exclude_class_names[i] =
9398
1.30k
      zend_resolve_const_class_name_reference(name_ast, "trait name");
9399
1.30k
  }
9400
9401
868
  zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
9402
868
}
9403
/* }}} */
9404
9405
static void zend_compile_trait_alias(const zend_ast *ast) /* {{{ */
9406
331k
{
9407
331k
  const zend_ast *method_ref_ast = ast->child[0];
9408
331k
  zend_ast *alias_ast = ast->child[1];
9409
331k
  uint32_t modifiers = ast->attr;
9410
9411
331k
  zend_trait_alias *alias;
9412
9413
331k
  zend_check_trait_alias_modifiers(modifiers);
9414
9415
331k
  alias = emalloc(sizeof(zend_trait_alias));
9416
331k
  zend_compile_method_ref(method_ref_ast, &alias->trait_method);
9417
331k
  alias->modifiers = modifiers;
9418
9419
331k
  if (alias_ast) {
9420
330k
    alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
9421
330k
  } else {
9422
299
    alias->alias = NULL;
9423
299
  }
9424
9425
331k
  zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
9426
331k
}
9427
/* }}} */
9428
9429
static void zend_compile_use_trait(const zend_ast *ast) /* {{{ */
9430
112k
{
9431
112k
  const zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
9432
112k
  zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
9433
112k
  zend_class_entry *ce = CG(active_class_entry);
9434
112k
  uint32_t i;
9435
9436
112k
  ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children));
9437
9438
226k
  for (i = 0; i < traits->children; ++i) {
9439
113k
    zend_ast *trait_ast = traits->child[i];
9440
9441
113k
    if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9442
5
      zend_string *name = zend_ast_get_str(trait_ast);
9443
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
9444
5
        "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
9445
5
    }
9446
9447
113k
    ce->trait_names[ce->num_traits].name =
9448
113k
      zend_resolve_const_class_name_reference(trait_ast, "trait name");
9449
113k
    ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name);
9450
113k
    ce->num_traits++;
9451
113k
  }
9452
9453
112k
  if (!adaptations) {
9454
2.23k
    return;
9455
2.23k
  }
9456
9457
442k
  for (i = 0; i < adaptations->children; ++i) {
9458
332k
    const zend_ast *adaptation_ast = adaptations->child[i];
9459
332k
    switch (adaptation_ast->kind) {
9460
868
      case ZEND_AST_TRAIT_PRECEDENCE:
9461
868
        zend_compile_trait_precedence(adaptation_ast);
9462
868
        break;
9463
331k
      case ZEND_AST_TRAIT_ALIAS:
9464
331k
        zend_compile_trait_alias(adaptation_ast);
9465
331k
        break;
9466
0
      default: ZEND_UNREACHABLE();
9467
332k
    }
9468
332k
  }
9469
110k
}
9470
/* }}} */
9471
9472
static void zend_compile_implements(zend_ast *ast) /* {{{ */
9473
8.18k
{
9474
8.18k
  const zend_ast_list *list = zend_ast_get_list(ast);
9475
8.18k
  zend_class_entry *ce = CG(active_class_entry);
9476
8.18k
  zend_class_name *interface_names;
9477
8.18k
  uint32_t i;
9478
9479
8.18k
  interface_names = emalloc(sizeof(zend_class_name) * list->children);
9480
9481
22.4k
  for (i = 0; i < list->children; ++i) {
9482
14.2k
    zend_ast *class_ast = list->child[i];
9483
14.2k
    interface_names[i].name =
9484
14.2k
      zend_resolve_const_class_name_reference(class_ast, "interface name");
9485
14.2k
    interface_names[i].lc_name = zend_string_tolower(interface_names[i].name);
9486
14.2k
  }
9487
9488
8.18k
  ce->num_interfaces = list->children;
9489
8.18k
  ce->interface_names = interface_names;
9490
8.18k
}
9491
/* }}} */
9492
9493
static zend_string *zend_generate_anon_class_name(const zend_ast_decl *decl)
9494
1.75k
{
9495
1.75k
  zend_string *filename = CG(active_op_array)->filename;
9496
1.75k
  uint32_t start_lineno = decl->start_lineno;
9497
9498
  /* Use parent or first interface as prefix. */
9499
1.75k
  zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS);
9500
1.75k
  if (decl->child[0]) {
9501
141
    prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name");
9502
1.61k
  } else if (decl->child[1]) {
9503
474
    const zend_ast_list *list = zend_ast_get_list(decl->child[1]);
9504
474
    prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name");
9505
474
  }
9506
9507
1.75k
  zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32,
9508
1.75k
    ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
9509
1.75k
  zend_string_release(prefix);
9510
1.75k
  return zend_new_interned_string(result);
9511
1.75k
}
9512
9513
static void zend_compile_enum_backing_type(zend_class_entry *ce, zend_ast *enum_backing_type_ast)
9514
731
{
9515
731
  ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM);
9516
731
  zend_type type = zend_compile_typename(enum_backing_type_ast);
9517
731
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9518
731
  if (ZEND_TYPE_IS_COMPLEX(type) || (type_mask != MAY_BE_LONG && type_mask != MAY_BE_STRING)) {
9519
55
    zend_string *type_string = zend_type_to_string(type);
9520
55
    zend_error_noreturn(E_COMPILE_ERROR,
9521
55
      "Enum backing type must be int or string, %s given",
9522
55
      ZSTR_VAL(type_string));
9523
55
  }
9524
676
  if (type_mask == MAY_BE_LONG) {
9525
356
    ce->enum_backing_type = IS_LONG;
9526
356
  } else {
9527
320
    ZEND_ASSERT(type_mask == MAY_BE_STRING);
9528
320
    ce->enum_backing_type = IS_STRING;
9529
319
  }
9530
676
  zend_type_release(type, 0);
9531
675
}
9532
9533
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel) /* {{{ */
9534
327k
{
9535
327k
  const zend_ast_decl *decl = (const zend_ast_decl *) ast;
9536
327k
  zend_ast *extends_ast = decl->child[0];
9537
327k
  zend_ast *implements_ast = decl->child[1];
9538
327k
  zend_ast *stmt_ast = decl->child[2];
9539
327k
  zend_ast *enum_backing_type_ast = decl->child[4];
9540
327k
  zend_string *name, *lcname;
9541
327k
  zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
9542
327k
  zend_op *opline;
9543
9544
327k
  zend_class_entry *original_ce = CG(active_class_entry);
9545
9546
327k
  if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
9547
325k
    zend_string *unqualified_name = decl->name;
9548
9549
325k
    if (CG(active_class_entry)) {
9550
8
      zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
9551
8
    }
9552
9553
325k
    const char *type = "a class name";
9554
325k
    if (decl->flags & ZEND_ACC_ENUM) {
9555
11.7k
      type = "an enum name";
9556
314k
    } else if (decl->flags & ZEND_ACC_INTERFACE) {
9557
6.39k
      type = "an interface name";
9558
307k
    } else if (decl->flags & ZEND_ACC_TRAIT) {
9559
2.72k
      type = "a trait name";
9560
2.72k
    }
9561
325k
    zend_assert_valid_class_name(unqualified_name, type);
9562
325k
    name = zend_prefix_with_ns(unqualified_name);
9563
325k
    name = zend_new_interned_string(name);
9564
325k
    lcname = zend_string_tolower(name);
9565
9566
325k
    if (FC(imports)) {
9567
537
      zend_string *import_name =
9568
537
        zend_hash_find_ptr_lc(FC(imports), unqualified_name);
9569
537
      if (import_name && !zend_string_equals_ci(lcname, import_name)) {
9570
12
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s "
9571
12
            "(previously declared as local import)", ZSTR_VAL(name));
9572
12
      }
9573
537
    }
9574
9575
325k
    zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS);
9576
325k
  } else {
9577
    /* Find an anon class name that is not in use yet. */
9578
1.75k
    name = NULL;
9579
1.75k
    lcname = NULL;
9580
1.75k
    do {
9581
1.75k
      zend_tmp_string_release(name);
9582
1.75k
      zend_tmp_string_release(lcname);
9583
1.75k
      name = zend_generate_anon_class_name(decl);
9584
1.75k
      lcname = zend_string_tolower(name);
9585
1.75k
    } while (zend_hash_exists(CG(class_table), lcname));
9586
1.75k
  }
9587
327k
  lcname = zend_new_interned_string(lcname);
9588
9589
327k
  ce->type = ZEND_USER_CLASS;
9590
327k
  ce->name = name;
9591
327k
  zend_initialize_class_data(ce, true);
9592
327k
  if (!(decl->flags & ZEND_ACC_ANON_CLASS)) {
9593
325k
    zend_alloc_ce_cache(ce->name);
9594
325k
  }
9595
9596
327k
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
9597
0
    ce->ce_flags |= ZEND_ACC_PRELOADED;
9598
0
    ZEND_MAP_PTR_NEW(ce->static_members_table);
9599
0
    ZEND_MAP_PTR_NEW(ce->mutable_data);
9600
0
  }
9601
9602
327k
  ce->ce_flags |= decl->flags;
9603
327k
  ce->info.user.filename = zend_string_copy(zend_get_compiled_filename());
9604
327k
  ce->info.user.line_start = decl->start_lineno;
9605
327k
  ce->info.user.line_end = decl->end_lineno;
9606
9607
327k
  if (decl->doc_comment) {
9608
180
    ce->doc_comment = zend_string_copy(decl->doc_comment);
9609
180
  }
9610
9611
327k
  if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
9612
    /* Serialization is not supported for anonymous classes */
9613
1.75k
    ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
9614
1.75k
  }
9615
9616
327k
  if (extends_ast) {
9617
66.5k
    ce->parent_name =
9618
66.5k
      zend_resolve_const_class_name_reference(extends_ast, "class name");
9619
66.5k
  }
9620
9621
327k
  CG(active_class_entry) = ce;
9622
9623
327k
  if (decl->child[3]) {
9624
2.56k
    zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS, 0);
9625
2.56k
  }
9626
9627
327k
  if (implements_ast) {
9628
8.18k
    zend_compile_implements(implements_ast);
9629
8.18k
  }
9630
9631
327k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
9632
11.7k
    if (enum_backing_type_ast != NULL) {
9633
731
      zend_compile_enum_backing_type(ce, enum_backing_type_ast);
9634
731
    }
9635
11.7k
    zend_enum_add_interfaces(ce);
9636
11.7k
    zend_enum_register_props(ce);
9637
11.7k
  }
9638
9639
327k
  zend_compile_stmt(stmt_ast);
9640
9641
  /* Reset lineno for final opcodes and errors */
9642
327k
  CG(zend_lineno) = ast->lineno;
9643
9644
327k
  if ((ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) == ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) {
9645
79
    zend_verify_abstract_class(ce);
9646
79
  }
9647
9648
327k
  CG(active_class_entry) = original_ce;
9649
9650
327k
  if (toplevel) {
9651
41.9k
    ce->ce_flags |= ZEND_ACC_TOP_LEVEL;
9652
41.9k
  }
9653
9654
  /* We currently don't early-bind classes that implement interfaces or use traits */
9655
327k
  if (!ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9656
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
9657
   /* See zend_link_hooked_object_iter(). */
9658
   && !ce->num_hooked_props
9659
#endif
9660
193k
   && !(CG(compiler_options) & ZEND_COMPILE_WITHOUT_EXECUTION)) {
9661
193k
    if (toplevel) {
9662
33.4k
      if (extends_ast) {
9663
8.76k
        zend_class_entry *parent_ce = zend_lookup_class_ex(
9664
8.76k
          ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
9665
9666
8.76k
        if (parent_ce
9667
7.98k
         && !zend_compile_ignore_class(parent_ce, ce->info.user.filename)) {
9668
7.98k
          if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) {
9669
4.88k
            zend_string_release(lcname);
9670
4.88k
            return;
9671
4.88k
          }
9672
7.98k
        }
9673
24.6k
      } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) {
9674
21.1k
        zend_string_release(lcname);
9675
21.1k
        zend_build_properties_info_table(ce);
9676
21.1k
        zend_inheritance_check_override(ce);
9677
21.1k
        ce->ce_flags |= ZEND_ACC_LINKED;
9678
21.1k
        zend_observer_class_linked_notify(ce, lcname);
9679
21.1k
        return;
9680
21.1k
      } else {
9681
3.46k
        goto link_unbound;
9682
3.46k
      }
9683
160k
    } else if (!extends_ast) {
9684
106k
link_unbound:
9685
      /* Link unbound simple class */
9686
106k
      zend_build_properties_info_table(ce);
9687
106k
      zend_inheritance_check_override(ce);
9688
106k
      ce->ce_flags |= ZEND_ACC_LINKED;
9689
106k
    }
9690
193k
  }
9691
9692
301k
  opline = get_next_op();
9693
9694
301k
  if (ce->parent_name) {
9695
    /* Lowercased parent name */
9696
60.6k
    zend_string *lc_parent_name = zend_string_tolower(ce->parent_name);
9697
60.6k
    opline->op2_type = IS_CONST;
9698
60.6k
    LITERAL_STR(opline->op2, lc_parent_name);
9699
60.6k
  }
9700
9701
301k
  opline->op1_type = IS_CONST;
9702
  /* It's possible that `lcname` is not an interned string because it was not yet in the interned string table.
9703
   * However, by this point another thread may have caused `lcname` to be added in the interned string table.
9704
   * This will cause `lcname` to get freed once it is found in the interned string table. If we were to use
9705
   * LITERAL_STR() here we would not change the `lcname` pointer to the new value, and it would point to the
9706
   * now-freed string. This will cause issues when we use `lcname` in the code below. We solve this by using
9707
   * zend_add_literal_string() which gives us the new value. */
9708
301k
  opline->op1.constant = zend_add_literal_string(&lcname);
9709
9710
301k
  if (decl->flags & ZEND_ACC_ANON_CLASS) {
9711
1.73k
    opline->opcode = ZEND_DECLARE_ANON_CLASS;
9712
1.73k
    opline->extended_value = zend_alloc_cache_slot();
9713
1.73k
    zend_make_var_result(result, opline);
9714
1.73k
    if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) {
9715
      /* We checked above that the class name is not used. This really shouldn't happen. */
9716
0
      zend_error_noreturn(E_ERROR,
9717
0
        "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name));
9718
0
    }
9719
299k
  } else {
9720
    /* Generate RTD keys until we find one that isn't in use yet. */
9721
299k
    zend_string *key = NULL;
9722
299k
    do {
9723
299k
      zend_tmp_string_release(key);
9724
299k
      key = zend_build_runtime_definition_key(lcname, decl->start_lineno);
9725
299k
    } while (!zend_hash_add_ptr(CG(class_table), key, ce));
9726
9727
    /* RTD key is placed after lcname literal in op1 */
9728
299k
    zend_add_literal_string(&key);
9729
9730
299k
    opline->opcode = ZEND_DECLARE_CLASS;
9731
299k
    if (toplevel
9732
15.0k
       && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING)
9733
        /* We currently don't early-bind classes that implement interfaces or use traits */
9734
5.46k
       && !ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9735
299k
    ) {
9736
1.33k
      if (!extends_ast) {
9737
        /* Use empty string for classes without parents to avoid new handler, and special
9738
         * handling of zend_early_binding. */
9739
822
        opline->op2_type = IS_CONST;
9740
822
        LITERAL_STR(opline->op2, ZSTR_EMPTY_ALLOC());
9741
822
      }
9742
1.33k
      CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING;
9743
1.33k
      opline->opcode = ZEND_DECLARE_CLASS_DELAYED;
9744
1.33k
      opline->extended_value = zend_alloc_cache_slot();
9745
1.33k
      opline->result_type = IS_UNUSED;
9746
1.33k
      opline->result.opline_num = -1;
9747
1.33k
    }
9748
299k
  }
9749
301k
}
9750
/* }}} */
9751
9752
static void zend_compile_enum_case(zend_ast *ast)
9753
18.6k
{
9754
18.6k
  zend_class_entry *enum_class = CG(active_class_entry);
9755
18.6k
  if (!(enum_class->ce_flags & ZEND_ACC_ENUM)) {
9756
5
    zend_error_noreturn(E_COMPILE_ERROR, "Case can only be used in enums");
9757
5
  }
9758
9759
18.6k
  zend_string *enum_case_name = zval_make_interned_string(zend_ast_get_zval(ast->child[0]));
9760
18.6k
  zend_string *enum_class_name = enum_class->name;
9761
9762
18.6k
  zval class_name_zval;
9763
18.6k
  ZVAL_STR_COPY(&class_name_zval, enum_class_name);
9764
18.6k
  zend_ast *class_name_ast = zend_ast_create_zval(&class_name_zval);
9765
9766
18.6k
  zval case_id_zval;
9767
18.6k
  int case_id = zend_enum_next_case_id(enum_class);
9768
18.6k
  ZVAL_LONG(&case_id_zval, case_id);
9769
18.6k
  zend_ast *case_id_ast = zend_ast_create_zval(&case_id_zval);
9770
9771
18.6k
  zval case_name_zval;
9772
18.6k
  ZVAL_STR_COPY(&case_name_zval, enum_case_name);
9773
18.6k
  zend_ast *case_name_ast = zend_ast_create_zval(&case_name_zval);
9774
9775
18.6k
  zend_ast *case_value_ast = ast->child[1];
9776
  // Remove case_value_ast from the original AST to avoid freeing it, as it will be freed by zend_const_expr_to_zval
9777
18.6k
  ast->child[1] = NULL;
9778
18.6k
  if (enum_class->enum_backing_type != IS_UNDEF && case_value_ast == NULL) {
9779
5
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of backed enum %s must have a value",
9780
5
      ZSTR_VAL(enum_case_name),
9781
5
      ZSTR_VAL(enum_class_name));
9782
18.6k
  } else if (enum_class->enum_backing_type == IS_UNDEF && case_value_ast != NULL) {
9783
12
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-backed enum %s must not have a value",
9784
12
      ZSTR_VAL(enum_case_name),
9785
12
      ZSTR_VAL(enum_class_name));
9786
12
  }
9787
9788
18.6k
  zend_ast *const_enum_init_ast = zend_ast_create(ZEND_AST_CONST_ENUM_INIT,
9789
18.6k
      class_name_ast, case_id_ast, case_name_ast, case_value_ast);
9790
9791
18.6k
  zval value_zv;
9792
18.6k
  zend_const_expr_to_zval(&value_zv, &const_enum_init_ast, /* allow_dynamic */ false);
9793
9794
  /* Doc comment has been appended as second last element in ZEND_AST_ENUM ast - attributes are conventionally last */
9795
18.6k
  zend_ast *doc_comment_ast = ast->child[2];
9796
18.6k
  zend_string *doc_comment = NULL;
9797
18.6k
  if (doc_comment_ast) {
9798
12.9k
    doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9799
12.9k
  }
9800
9801
18.6k
  zend_class_constant *c = zend_declare_class_constant_ex(enum_class, enum_case_name, &value_zv, ZEND_ACC_PUBLIC, doc_comment);
9802
18.6k
  ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE;
9803
18.6k
  zend_ast_destroy(const_enum_init_ast);
9804
9805
18.6k
  zend_ast *attr_ast = ast->child[3];
9806
18.6k
  if (attr_ast) {
9807
237
    zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9808
9809
237
    zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9810
9811
237
    if (deprecated) {
9812
65
      ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9813
65
    }
9814
237
  }
9815
18.6k
}
9816
9817
static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
9818
1.75k
{
9819
1.75k
  switch (type) {
9820
956
    case ZEND_SYMBOL_CLASS:
9821
956
      if (!FC(imports)) {
9822
674
        FC(imports) = emalloc(sizeof(HashTable));
9823
674
        zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
9824
674
      }
9825
956
      return FC(imports);
9826
526
    case ZEND_SYMBOL_FUNCTION:
9827
526
      if (!FC(imports_function)) {
9828
393
        FC(imports_function) = emalloc(sizeof(HashTable));
9829
393
        zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
9830
393
      }
9831
526
      return FC(imports_function);
9832
271
    case ZEND_SYMBOL_CONST:
9833
271
      if (!FC(imports_const)) {
9834
202
        FC(imports_const) = emalloc(sizeof(HashTable));
9835
202
        zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
9836
202
      }
9837
271
      return FC(imports_const);
9838
0
    default: ZEND_UNREACHABLE();
9839
1.75k
  }
9840
9841
0
  return NULL;
9842
1.75k
}
9843
/* }}} */
9844
9845
static char *zend_get_use_type_str(uint32_t type) /* {{{ */
9846
64
{
9847
64
  switch (type) {
9848
32
    case ZEND_SYMBOL_CLASS:
9849
32
      return "";
9850
17
    case ZEND_SYMBOL_FUNCTION:
9851
17
      return " function";
9852
15
    case ZEND_SYMBOL_CONST:
9853
15
      return " const";
9854
0
    default: ZEND_UNREACHABLE();
9855
64
  }
9856
9857
0
  return " unknown";
9858
64
}
9859
/* }}} */
9860
9861
static void zend_check_already_in_use(uint32_t type, const zend_string *old_name, const zend_string *new_name, const zend_string *check_name) /* {{{ */
9862
43
{
9863
43
  if (zend_string_equals_ci(old_name, check_name)) {
9864
18
    return;
9865
18
  }
9866
9867
25
  zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9868
25
    "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9869
43
}
9870
/* }}} */
9871
9872
static void zend_compile_use(zend_ast *ast) /* {{{ */
9873
1.75k
{
9874
1.75k
  const zend_ast_list *list = zend_ast_get_list(ast);
9875
1.75k
  uint32_t i;
9876
1.75k
  zend_string *current_ns = FC(current_namespace);
9877
1.75k
  uint32_t type = ast->attr;
9878
1.75k
  HashTable *current_import = zend_get_import_ht(type);
9879
1.75k
  bool case_sensitive = type == ZEND_SYMBOL_CONST;
9880
9881
3.63k
  for (i = 0; i < list->children; ++i) {
9882
1.94k
    const zend_ast *use_ast = list->child[i];
9883
1.94k
    zend_ast *old_name_ast = use_ast->child[0];
9884
1.94k
    zend_ast *new_name_ast = use_ast->child[1];
9885
1.94k
    zend_string *old_name = zend_ast_get_str(old_name_ast);
9886
1.94k
    zend_string *new_name, *lookup_name;
9887
9888
1.94k
    if (new_name_ast) {
9889
391
      new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
9890
1.55k
    } else {
9891
1.55k
      const char *unqualified_name;
9892
1.55k
      size_t unqualified_name_len;
9893
1.55k
      if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
9894
        /* The form "use A\B" is equivalent to "use A\B as B" */
9895
725
        new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
9896
831
      } else {
9897
831
        new_name = zend_string_copy(old_name);
9898
9899
831
        if (!current_ns) {
9900
457
          zend_error(E_WARNING, "The use statement with non-compound name '%s' "
9901
457
            "has no effect", ZSTR_VAL(new_name));
9902
457
        }
9903
831
      }
9904
1.55k
    }
9905
9906
1.94k
    if (case_sensitive) {
9907
296
      lookup_name = zend_string_copy(new_name);
9908
1.65k
    } else {
9909
1.65k
      lookup_name = zend_string_tolower(new_name);
9910
1.65k
    }
9911
9912
1.94k
    if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) {
9913
23
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
9914
23
        "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
9915
23
    }
9916
9917
1.92k
    if (current_ns) {
9918
1.01k
      zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
9919
1.01k
      zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
9920
1.01k
      ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
9921
1.01k
      memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1);
9922
9923
1.01k
      if (zend_have_seen_symbol(ns_name, type)) {
9924
16
        zend_check_already_in_use(type, old_name, new_name, ns_name);
9925
16
      }
9926
9927
1.01k
      zend_string_efree(ns_name);
9928
1.01k
    } else if (zend_have_seen_symbol(lookup_name, type)) {
9929
27
      zend_check_already_in_use(type, old_name, new_name, lookup_name);
9930
27
    }
9931
9932
1.92k
    zend_string_addref(old_name);
9933
1.92k
    old_name = zend_new_interned_string(old_name);
9934
1.92k
    if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
9935
39
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9936
39
        "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9937
39
    }
9938
9939
1.88k
    zend_string_release_ex(lookup_name, 0);
9940
1.88k
    zend_string_release_ex(new_name, 0);
9941
1.88k
  }
9942
1.75k
}
9943
/* }}} */
9944
9945
static void zend_compile_group_use(const zend_ast *ast) /* {{{ */
9946
175
{
9947
175
  uint32_t i;
9948
175
  const zend_string *ns = zend_ast_get_str(ast->child[0]);
9949
175
  const zend_ast_list *list = zend_ast_get_list(ast->child[1]);
9950
9951
678
  for (i = 0; i < list->children; i++) {
9952
503
    zend_ast *inline_use, *use = list->child[i];
9953
503
    zval *name_zval = zend_ast_get_zval(use->child[0]);
9954
503
    zend_string *name = Z_STR_P(name_zval);
9955
503
    zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
9956
503
    zend_string_release_ex(name, 0);
9957
503
    ZVAL_STR(name_zval, compound_ns);
9958
503
    inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
9959
503
    inline_use->attr = ast->attr ? ast->attr : use->attr;
9960
503
    zend_compile_use(inline_use);
9961
503
  }
9962
175
}
9963
/* }}} */
9964
9965
static void zend_compile_const_decl(zend_ast *ast) /* {{{ */
9966
4.84k
{
9967
4.84k
  zend_ast_list *list = zend_ast_get_list(ast);
9968
4.84k
  uint32_t i;
9969
4.84k
  zend_ast *attributes_ast = NULL;
9970
4.84k
  zend_op *last_op = NULL;
9971
10.9k
  for (i = 0; i < list->children; ++i) {
9972
6.15k
    zend_ast *const_ast = list->child[i];
9973
6.15k
    if (const_ast->kind == ZEND_AST_ATTRIBUTE_LIST) {
9974
736
      ZEND_ASSERT(i == list->children - 1);
9975
736
      attributes_ast = const_ast;
9976
736
      continue;
9977
736
    }
9978
5.41k
    ZEND_ASSERT(const_ast->kind == ZEND_AST_CONST_ELEM);
9979
5.41k
    zend_ast *name_ast = const_ast->child[0];
9980
5.41k
    zend_ast **value_ast_ptr = &const_ast->child[1];
9981
5.41k
    zend_string *unqualified_name = zend_ast_get_str(name_ast);
9982
9983
5.41k
    zend_string *name;
9984
5.41k
    znode name_node, value_node;
9985
5.41k
    zval *value_zv = &value_node.u.constant;
9986
9987
5.41k
    value_node.op_type = IS_CONST;
9988
5.41k
    zend_const_expr_to_zval(value_zv, value_ast_ptr, /* allow_dynamic */ true);
9989
9990
5.41k
    if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) {
9991
6
      zend_error_noreturn(E_COMPILE_ERROR,
9992
6
        "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name));
9993
6
    }
9994
9995
5.41k
    name = zend_prefix_with_ns(unqualified_name);
9996
5.41k
    name = zend_new_interned_string(name);
9997
9998
5.41k
    if (FC(imports_const)) {
9999
868
      zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name);
10000
868
      if (import_name && !zend_string_equals(import_name, name)) {
10001
9
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because "
10002
9
          "the name is already in use", ZSTR_VAL(name));
10003
9
      }
10004
868
    }
10005
10006
5.40k
    name_node.op_type = IS_CONST;
10007
5.40k
    ZVAL_STR(&name_node.u.constant, name);
10008
10009
5.40k
    last_op = zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
10010
10011
5.40k
    zend_register_seen_symbol(name, ZEND_SYMBOL_CONST);
10012
5.40k
  }
10013
4.83k
  if (attributes_ast == NULL) {
10014
3.99k
    return;
10015
3.99k
  }
10016
  /* Validate: attributes can only be applied to one constant at a time
10017
   * Since we store the AST for the attributes in the list of children,
10018
   * there should be exactly 2 children. */
10019
843
  if (list->children > 2) {
10020
6
    zend_error_noreturn(
10021
6
      E_COMPILE_ERROR,
10022
6
      "Cannot apply attributes to multiple constants at once"
10023
6
    );
10024
6
  }
10025
10026
837
  HashTable *attributes = NULL;
10027
837
  zend_compile_attributes(&attributes, list->child[1], 0, ZEND_ATTRIBUTE_TARGET_CONST, 0);
10028
10029
837
  ZEND_ASSERT(last_op != NULL);
10030
837
  last_op->opcode = ZEND_DECLARE_ATTRIBUTED_CONST;
10031
719
  znode attribs_node;
10032
719
  attribs_node.op_type = IS_CONST;
10033
719
  ZVAL_PTR(&attribs_node.u.constant, attributes);
10034
719
  zend_emit_op_data(&attribs_node);
10035
719
  CG(active_op_array)->fn_flags |= ZEND_ACC_PTR_OPS;
10036
719
}
10037
/* }}}*/
10038
10039
static void zend_compile_namespace(const zend_ast *ast) /* {{{ */
10040
4.30k
{
10041
4.30k
  zend_ast *name_ast = ast->child[0];
10042
4.30k
  zend_ast *stmt_ast = ast->child[1];
10043
4.30k
  zend_string *name;
10044
4.30k
  bool with_bracket = stmt_ast != NULL;
10045
10046
  /* handle mixed syntax declaration or nested namespaces */
10047
4.30k
  if (!FC(has_bracketed_namespaces)) {
10048
3.49k
    if (FC(current_namespace)) {
10049
      /* previous namespace declarations were unbracketed */
10050
678
      if (with_bracket) {
10051
7
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
10052
7
          "with unbracketed namespace declarations");
10053
7
      }
10054
678
    }
10055
3.49k
  } else {
10056
    /* previous namespace declarations were bracketed */
10057
819
    if (!with_bracket) {
10058
8
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
10059
8
        "with unbracketed namespace declarations");
10060
811
    } else if (FC(current_namespace) || FC(in_namespace)) {
10061
6
      zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
10062
6
    }
10063
819
  }
10064
10065
4.30k
  bool is_first_namespace = (!with_bracket && !FC(current_namespace))
10066
2.01k
    || (with_bracket && !FC(has_bracketed_namespaces));
10067
4.28k
  if (is_first_namespace && FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
10068
29
    zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
10069
29
      "the very first statement or after any declare call in the script");
10070
29
  }
10071
10072
4.25k
  if (FC(current_namespace)) {
10073
671
    zend_string_release_ex(FC(current_namespace), 0);
10074
671
  }
10075
10076
4.25k
  if (name_ast) {
10077
3.78k
    name = zend_ast_get_str(name_ast);
10078
10079
3.78k
    if (zend_string_equals_literal_ci(name, "namespace")) {
10080
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name));
10081
5
    }
10082
10083
3.77k
    FC(current_namespace) = zend_string_copy(name);
10084
3.77k
  } else {
10085
478
    FC(current_namespace) = NULL;
10086
478
  }
10087
10088
4.25k
  zend_reset_import_tables();
10089
10090
4.25k
  FC(in_namespace) = 1;
10091
4.25k
  if (with_bracket) {
10092
1.33k
    FC(has_bracketed_namespaces) = 1;
10093
1.33k
  }
10094
10095
4.25k
  if (stmt_ast) {
10096
1.33k
    zend_compile_top_stmt(stmt_ast);
10097
1.33k
    zend_end_namespace();
10098
1.33k
  }
10099
4.25k
}
10100
/* }}} */
10101
10102
static void zend_compile_halt_compiler(const zend_ast *ast) /* {{{ */
10103
81
{
10104
81
  zend_ast *offset_ast = ast->child[0];
10105
81
  zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
10106
10107
81
  const char const_name[] = "__COMPILER_HALT_OFFSET__";
10108
10109
81
  if (FC(has_bracketed_namespaces) && FC(in_namespace)) {
10110
0
    zend_error_noreturn(E_COMPILE_ERROR,
10111
0
      "__HALT_COMPILER() can only be used from the outermost scope");
10112
0
  }
10113
10114
81
  const zend_string *filename = zend_get_compiled_filename();
10115
81
  zend_string *name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
10116
81
    ZSTR_VAL(filename), ZSTR_LEN(filename), false);
10117
10118
  /* Avoid repeated declaration of the __COMPILER_HALT_OFFSET__ constant in
10119
   * case this file was already included. */
10120
81
  if (!zend_hash_find(EG(zend_constants), name)) {
10121
81
    zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0);
10122
81
  }
10123
81
  zend_string_release_ex(name, 0);
10124
81
}
10125
/* }}} */
10126
10127
static bool zend_try_ct_eval_magic_const(zval *zv, const zend_ast *ast) /* {{{ */
10128
17.7k
{
10129
17.7k
  const zend_op_array *op_array = CG(active_op_array);
10130
17.7k
  const zend_class_entry *ce = CG(active_class_entry);
10131
10132
17.7k
  switch (ast->attr) {
10133
512
    case T_LINE:
10134
512
      ZVAL_LONG(zv, ast->lineno);
10135
512
      break;
10136
4.17k
    case T_FILE:
10137
4.17k
      ZVAL_STR_COPY(zv, CG(compiled_filename));
10138
4.17k
      break;
10139
789
    case T_DIR:
10140
789
    {
10141
789
      const zend_string *filename = CG(compiled_filename);
10142
789
      zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
10143
#ifdef ZEND_WIN32
10144
      ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
10145
#else
10146
789
      ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
10147
789
#endif
10148
10149
789
      if (zend_string_equals_literal(dirname, ".")) {
10150
256
        dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
10151
256
#ifdef HAVE_GETCWD
10152
256
        ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN));
10153
#elif defined(HAVE_GETWD)
10154
        ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname)));
10155
#endif
10156
256
        ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname));
10157
256
      }
10158
10159
789
      ZVAL_STR(zv, dirname);
10160
789
      break;
10161
0
    }
10162
1.74k
    case T_FUNC_C:
10163
1.74k
      if (op_array && op_array->function_name) {
10164
1.63k
        ZVAL_STR_COPY(zv, op_array->function_name);
10165
1.63k
      } else {
10166
112
        ZVAL_EMPTY_STRING(zv);
10167
112
      }
10168
1.74k
      break;
10169
620
    case T_PROPERTY_C: {
10170
620
      zend_string *prop_info_name = CG(context).active_property_info_name;
10171
620
      if (prop_info_name) {
10172
405
        ZVAL_STR(zv, zend_copy_unmangled_prop_name(prop_info_name));
10173
405
      } else {
10174
215
        ZVAL_EMPTY_STRING(zv);
10175
215
      }
10176
620
      break;
10177
0
    }
10178
4.53k
    case T_METHOD_C:
10179
      /* Detect whether we are directly inside a class (e.g. a class constant) and treat
10180
       * this as not being inside a function. */
10181
4.53k
      if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
10182
442
        op_array = NULL;
10183
442
      }
10184
4.53k
      if (op_array && op_array->function_name) {
10185
3.83k
        if (op_array->scope) {
10186
3.35k
          ZVAL_NEW_STR(zv,
10187
3.35k
            zend_create_member_string(op_array->scope->name, op_array->function_name));
10188
3.35k
        } else {
10189
478
          ZVAL_STR_COPY(zv, op_array->function_name);
10190
478
        }
10191
3.83k
      } else {
10192
697
        ZVAL_EMPTY_STRING(zv);
10193
697
      }
10194
4.53k
      break;
10195
3.33k
    case T_CLASS_C:
10196
3.33k
      if (ce) {
10197
2.27k
        if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10198
1.29k
          return 0;
10199
1.29k
        } else {
10200
983
          ZVAL_STR_COPY(zv, ce->name);
10201
983
        }
10202
2.27k
      } else {
10203
1.05k
        ZVAL_EMPTY_STRING(zv);
10204
1.05k
      }
10205
2.04k
      break;
10206
2.04k
    case T_TRAIT_C:
10207
1.31k
      if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10208
270
        ZVAL_STR_COPY(zv, ce->name);
10209
1.04k
      } else {
10210
1.04k
        ZVAL_EMPTY_STRING(zv);
10211
1.04k
      }
10212
1.31k
      break;
10213
773
    case T_NS_C:
10214
773
      if (FC(current_namespace)) {
10215
568
        ZVAL_STR_COPY(zv, FC(current_namespace));
10216
568
      } else {
10217
205
        ZVAL_EMPTY_STRING(zv);
10218
205
      }
10219
773
      break;
10220
0
    default: ZEND_UNREACHABLE();
10221
17.7k
  }
10222
10223
16.4k
  return true;
10224
17.7k
}
10225
/* }}} */
10226
10227
ZEND_API bool zend_is_op_long_compatible(const zval *op)
10228
205k
{
10229
205k
  if (Z_TYPE_P(op) == IS_ARRAY) {
10230
377
    return false;
10231
377
  }
10232
10233
204k
  if (Z_TYPE_P(op) == IS_DOUBLE
10234
80.0k
    && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval_silent(Z_DVAL_P(op)))) {
10235
66.4k
    return false;
10236
66.4k
  }
10237
10238
138k
  if (Z_TYPE_P(op) == IS_STRING) {
10239
11.1k
    double dval = 0;
10240
11.1k
    uint8_t is_num = is_numeric_str_function(Z_STR_P(op), NULL, &dval);
10241
11.1k
    if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval_silent(dval)))) {
10242
4.32k
      return false;
10243
4.32k
    }
10244
11.1k
  }
10245
10246
134k
  return true;
10247
138k
}
10248
10249
ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, const zval *op2) /* {{{ */
10250
778k
{
10251
778k
  if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) {
10252
    /* Array to string warning. */
10253
31.5k
    return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY;
10254
31.5k
  }
10255
10256
746k
  if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV
10257
520k
               || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR
10258
446k
               || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) {
10259
    /* Only the numeric operations throw errors. */
10260
358k
    return 0;
10261
358k
  }
10262
10263
387k
  if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) {
10264
108k
    if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) {
10265
      /* Adding two arrays is allowed. */
10266
101k
      return 0;
10267
101k
    }
10268
10269
    /* Numeric operators throw when one of the operands is an array. */
10270
6.60k
    return 1;
10271
108k
  }
10272
10273
  /* While basic arithmetic operators always produce numeric string errors,
10274
   * bitwise operators don't produce errors if both operands are strings */
10275
279k
  if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)
10276
87.3k
    && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
10277
15.2k
    return 0;
10278
15.2k
  }
10279
10280
264k
  if (Z_TYPE_P(op1) == IS_STRING
10281
57.0k
    && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) {
10282
33.2k
    return 1;
10283
33.2k
  }
10284
10285
230k
  if (Z_TYPE_P(op2) == IS_STRING
10286
34.5k
    && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) {
10287
26.7k
    return 1;
10288
26.7k
  }
10289
10290
  /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */
10291
204k
  if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR
10292
183k
      || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) {
10293
88.8k
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2)) {
10294
60.9k
      return 1;
10295
60.9k
    }
10296
88.8k
  }
10297
10298
143k
  if (opcode == ZEND_DIV && zval_get_double(op2) == 0.0) {
10299
    /* Division by zero throws an error. */
10300
732
    return 1;
10301
732
  }
10302
10303
  /* Mod is an operation that will cast float/float-strings to integers which might
10304
     produce float to int incompatible errors, and also cannot be divided by 0 */
10305
142k
  if (opcode == ZEND_MOD) {
10306
15.3k
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2) || zval_get_long(op2) == 0) {
10307
11.5k
      return 1;
10308
11.5k
    }
10309
15.3k
  }
10310
10311
130k
  if ((opcode == ZEND_POW) && zval_get_double(op1) == 0 && zval_get_double(op2) < 0) {
10312
    /* 0 ** (<0) throws a division by zero error. */
10313
1.04k
    return 1;
10314
1.04k
  }
10315
129k
  if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) {
10316
    /* Shift by negative number throws an error. */
10317
317
    return 1;
10318
317
  }
10319
10320
129k
  return 0;
10321
129k
}
10322
/* }}} */
10323
10324
static inline bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
10325
726k
{
10326
726k
  if (zend_binary_op_produces_error(opcode, op1, op2)) {
10327
117k
    return false;
10328
117k
  }
10329
10330
609k
  const binary_op_type fn = get_binary_op(opcode);
10331
609k
  fn(result, op1, op2);
10332
609k
  return true;
10333
726k
}
10334
/* }}} */
10335
10336
ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op)
10337
192k
{
10338
192k
  if (opcode == ZEND_BW_NOT) {
10339
    /* BW_NOT on string does not convert the string into an integer. */
10340
6.71k
    if (Z_TYPE_P(op) == IS_STRING) {
10341
1.00k
      return 0;
10342
1.00k
    }
10343
5.70k
    return Z_TYPE_P(op) <= IS_TRUE || !zend_is_op_long_compatible(op);
10344
6.71k
  }
10345
  /* Can happen when called from zend_optimizer_eval_unary_op() */
10346
185k
  if (opcode == ZEND_BOOL || opcode == ZEND_BOOL_NOT) {
10347
    /* ZEND_BOOL/ZEND_BOOL_NOT warns when casting NAN. */
10348
185k
    return Z_TYPE_P(op) == IS_DOUBLE;
10349
185k
  }
10350
10351
0
  return 0;
10352
185k
}
10353
10354
static inline bool zend_try_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */
10355
191k
{
10356
191k
  if (zend_unary_op_produces_error(opcode, op)) {
10357
1.94k
    return false;
10358
1.94k
  }
10359
10360
189k
  const unary_op_type fn = get_unary_op(opcode);
10361
189k
  fn(result, op);
10362
189k
  return true;
10363
191k
}
10364
/* }}} */
10365
10366
static inline bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
10367
64.1k
{
10368
64.1k
  zval right;
10369
64.1k
  ZVAL_LONG(&right, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10370
64.1k
  return zend_try_ct_eval_binary_op(result, ZEND_MUL, op, &right);
10371
64.1k
}
10372
/* }}} */
10373
10374
static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
10375
19.4k
{
10376
19.4k
  const binary_op_type fn = kind == ZEND_AST_GREATER
10377
19.4k
    ? is_smaller_function : is_smaller_or_equal_function;
10378
19.4k
  fn(result, op2, op1);
10379
19.4k
}
10380
/* }}} */
10381
10382
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
10383
1.72M
{
10384
1.72M
  const zend_ast_list *list = zend_ast_get_list(ast);
10385
1.72M
  zend_ast *last_elem_ast = NULL;
10386
1.72M
  uint32_t i;
10387
1.72M
  bool is_constant = true;
10388
10389
1.72M
  if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
10390
5
    zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
10391
5
  }
10392
10393
  /* First ensure that *all* child nodes are constant and by-val */
10394
3.84M
  for (i = 0; i < list->children; ++i) {
10395
2.12M
    zend_ast *elem_ast = list->child[i];
10396
10397
2.12M
    if (elem_ast == NULL) {
10398
      /* Report error at line of last non-empty element */
10399
118
      if (last_elem_ast) {
10400
65
        CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast);
10401
65
      }
10402
118
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
10403
118
    }
10404
10405
2.12M
    if (elem_ast->kind != ZEND_AST_UNPACK) {
10406
1.99M
      zend_eval_const_expr(&elem_ast->child[0]);
10407
1.99M
      zend_eval_const_expr(&elem_ast->child[1]);
10408
10409
1.99M
      if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL
10410
502k
        || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
10411
1.99M
      ) {
10412
1.49M
        is_constant = false;
10413
1.49M
      }
10414
1.99M
    } else {
10415
131k
      zend_eval_const_expr(&elem_ast->child[0]);
10416
10417
131k
      if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) {
10418
130k
        is_constant = false;
10419
130k
      }
10420
131k
    }
10421
10422
2.12M
    last_elem_ast = elem_ast;
10423
2.12M
  }
10424
10425
1.72M
  if (!is_constant) {
10426
1.48M
    return false;
10427
1.48M
  }
10428
10429
238k
  if (!list->children) {
10430
48.1k
    ZVAL_EMPTY_ARRAY(result);
10431
48.1k
    return true;
10432
48.1k
  }
10433
10434
190k
  array_init_size(result, list->children);
10435
628k
  for (i = 0; i < list->children; ++i) {
10436
440k
    const zend_ast *elem_ast = list->child[i];
10437
440k
    zend_ast *value_ast = elem_ast->child[0];
10438
440k
    zend_ast *key_ast;
10439
10440
440k
    zval *value = zend_ast_get_zval(value_ast);
10441
440k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
10442
961
      if (Z_TYPE_P(value) == IS_ARRAY) {
10443
954
        const HashTable *ht = Z_ARRVAL_P(value);
10444
954
        zval *val;
10445
954
        zend_string *key;
10446
10447
2.93k
        ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
10448
2.93k
          if (key) {
10449
149
            zend_hash_update(Z_ARRVAL_P(result), key, val);
10450
414
          } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
10451
12
            zval_ptr_dtor(result);
10452
12
            return 0;
10453
12
          }
10454
551
          Z_TRY_ADDREF_P(val);
10455
551
        } ZEND_HASH_FOREACH_END();
10456
10457
942
        continue;
10458
954
      } else {
10459
7
        zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked, %s given", zend_zval_value_name(value));
10460
7
      }
10461
961
    }
10462
10463
439k
    Z_TRY_ADDREF_P(value);
10464
10465
439k
    key_ast = elem_ast->child[1];
10466
439k
    if (key_ast) {
10467
128k
      const zval *key = zend_ast_get_zval(key_ast);
10468
128k
      switch (Z_TYPE_P(key)) {
10469
5.65k
        case IS_LONG:
10470
5.65k
          zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value);
10471
5.65k
          break;
10472
120k
        case IS_STRING:
10473
120k
          zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value);
10474
120k
          break;
10475
2.07k
        case IS_DOUBLE: {
10476
2.07k
          zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key));
10477
          /* Incompatible float will generate an error, leave this to run-time */
10478
2.07k
          if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) {
10479
1.69k
            goto fail;
10480
1.69k
          }
10481
375
          zend_hash_index_update(Z_ARRVAL_P(result), lval, value);
10482
375
          break;
10483
2.07k
        }
10484
255
        case IS_FALSE:
10485
255
          zend_hash_index_update(Z_ARRVAL_P(result), 0, value);
10486
255
          break;
10487
81
        case IS_TRUE:
10488
81
          zend_hash_index_update(Z_ARRVAL_P(result), 1, value);
10489
81
          break;
10490
103
        case IS_NULL:
10491
          /* Null key will generate a warning at run-time. */
10492
103
          goto fail;
10493
3
        default:
10494
3
          zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type");
10495
128k
      }
10496
311k
    } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) {
10497
1.80k
fail:
10498
1.80k
      zval_ptr_dtor_nogc(value);
10499
1.80k
      zval_ptr_dtor(result);
10500
1.80k
      return 0;
10501
9
    }
10502
439k
  }
10503
10504
188k
  return true;
10505
190k
}
10506
/* }}} */
10507
10508
static void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */
10509
3.60M
{
10510
3.60M
  zend_ast *left_ast = ast->child[0];
10511
3.60M
  zend_ast *right_ast = ast->child[1];
10512
3.60M
  uint32_t opcode = ast->attr;
10513
10514
3.60M
  znode left_node, right_node;
10515
10516
3.60M
  zend_compile_expr(&left_node, left_ast);
10517
3.60M
  zend_compile_expr(&right_node, right_ast);
10518
10519
3.60M
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10520
617k
    if (zend_try_ct_eval_binary_op(&result->u.constant, opcode,
10521
617k
        &left_node.u.constant, &right_node.u.constant)
10522
617k
    ) {
10523
546k
      result->op_type = IS_CONST;
10524
546k
      zval_ptr_dtor(&left_node.u.constant);
10525
546k
      zval_ptr_dtor(&right_node.u.constant);
10526
546k
      return;
10527
546k
    }
10528
617k
  }
10529
10530
3.06M
  do {
10531
3.06M
    if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) {
10532
      /* convert $x === null to is_null($x) (i.e. ZEND_TYPE_CHECK opcode). Do the same thing for false/true. (covers IS_NULL, IS_FALSE, and IS_TRUE) */
10533
38.4k
      if (left_node.op_type == IS_CONST) {
10534
8.75k
        if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) {
10535
772
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL);
10536
772
          opline->extended_value =
10537
772
            (opcode == ZEND_IS_IDENTICAL) ?
10538
578
              (1 << Z_TYPE(left_node.u.constant)) :
10539
772
              (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant)));
10540
772
          return;
10541
772
        }
10542
29.6k
      } else if (right_node.op_type == IS_CONST) {
10543
27.7k
        if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) {
10544
26.1k
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL);
10545
26.1k
          opline->extended_value =
10546
26.1k
            (opcode == ZEND_IS_IDENTICAL) ?
10547
7.32k
              (1 << Z_TYPE(right_node.u.constant)) :
10548
26.1k
              (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant)));
10549
26.1k
          return;
10550
26.1k
        }
10551
27.7k
      }
10552
3.02M
    } else if (opcode == ZEND_CONCAT) {
10553
      /* convert constant operands to strings at compile-time */
10554
146k
      if (left_node.op_type == IS_CONST) {
10555
13.6k
        if (Z_TYPE(left_node.u.constant) == IS_ARRAY) {
10556
120
          zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING;
10557
13.5k
        } else {
10558
13.5k
          convert_to_string(&left_node.u.constant);
10559
13.5k
        }
10560
13.6k
      }
10561
146k
      if (right_node.op_type == IS_CONST) {
10562
23.8k
        if (Z_TYPE(right_node.u.constant) == IS_ARRAY) {
10563
292
          zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING;
10564
23.5k
        } else {
10565
23.5k
          convert_to_string(&right_node.u.constant);
10566
23.5k
        }
10567
23.8k
      }
10568
146k
      if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10569
0
        opcode = ZEND_FAST_CONCAT;
10570
0
      }
10571
146k
    }
10572
3.03M
    zend_emit_op_tmp(result, opcode, &left_node, &right_node);
10573
3.03M
  } while (0);
10574
3.06M
}
10575
/* }}} */
10576
10577
/* We do not use zend_compile_binary_op for this because we want to retain the left-to-right
10578
 * evaluation order. */
10579
static void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */
10580
539k
{
10581
539k
  zend_ast *left_ast = ast->child[0];
10582
539k
  zend_ast *right_ast = ast->child[1];
10583
539k
  znode left_node, right_node;
10584
10585
539k
  ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL);
10586
10587
539k
  zend_compile_expr(&left_node, left_ast);
10588
539k
  zend_compile_expr(&right_node, right_ast);
10589
10590
539k
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10591
17.8k
    result->op_type = IS_CONST;
10592
17.8k
    zend_ct_eval_greater(&result->u.constant, ast->kind,
10593
17.8k
      &left_node.u.constant, &right_node.u.constant);
10594
17.8k
    zval_ptr_dtor(&left_node.u.constant);
10595
17.8k
    zval_ptr_dtor(&right_node.u.constant);
10596
17.8k
    return;
10597
17.8k
  }
10598
10599
522k
  zend_emit_op_tmp(result,
10600
522k
    ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL,
10601
522k
    &right_node, &left_node);
10602
522k
}
10603
/* }}} */
10604
10605
static void zend_compile_unary_op(znode *result, const zend_ast *ast) /* {{{ */
10606
1.41M
{
10607
1.41M
  zend_ast *expr_ast = ast->child[0];
10608
1.41M
  uint32_t opcode = ast->attr;
10609
10610
1.41M
  znode expr_node;
10611
1.41M
  zend_compile_expr(&expr_node, expr_ast);
10612
10613
1.41M
  if (expr_node.op_type == IS_CONST
10614
184k
      && zend_try_ct_eval_unary_op(&result->u.constant, opcode, &expr_node.u.constant)) {
10615
183k
    result->op_type = IS_CONST;
10616
183k
    zval_ptr_dtor(&expr_node.u.constant);
10617
183k
    return;
10618
183k
  }
10619
10620
1.22M
  zend_emit_op_tmp(result, opcode, &expr_node, NULL);
10621
1.22M
}
10622
/* }}} */
10623
10624
static void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */
10625
267k
{
10626
267k
  zend_ast *expr_ast = ast->child[0];
10627
267k
  znode expr_node, right_node;
10628
10629
267k
  ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS);
10630
10631
267k
  zend_compile_expr(&expr_node, expr_ast);
10632
10633
267k
  if (expr_node.op_type == IS_CONST
10634
46.8k
    && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) {
10635
33.7k
    result->op_type = IS_CONST;
10636
33.7k
    zval_ptr_dtor(&expr_node.u.constant);
10637
33.7k
    return;
10638
33.7k
  }
10639
10640
233k
  right_node.op_type = IS_CONST;
10641
233k
  ZVAL_LONG(&right_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10642
233k
  zend_emit_op_tmp(result, ZEND_MUL, &expr_node, &right_node);
10643
233k
}
10644
/* }}} */
10645
10646
static void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */
10647
15.6k
{
10648
15.6k
  zend_ast *left_ast = ast->child[0];
10649
15.6k
  zend_ast *right_ast = ast->child[1];
10650
10651
15.6k
  znode left_node, right_node;
10652
15.6k
  zend_op *opline_jmpz, *opline_bool;
10653
15.6k
  uint32_t opnum_jmpz;
10654
10655
15.6k
  ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR);
10656
10657
15.6k
  zend_compile_expr(&left_node, left_ast);
10658
10659
15.6k
  if (left_node.op_type == IS_CONST) {
10660
2.74k
    if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant))
10661
2.02k
     || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) {
10662
1.24k
      result->op_type = IS_CONST;
10663
1.24k
      ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant));
10664
1.50k
    } else {
10665
1.50k
      zend_compile_expr(&right_node, right_ast);
10666
10667
1.50k
      if (right_node.op_type == IS_CONST) {
10668
826
        result->op_type = IS_CONST;
10669
826
        ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant));
10670
10671
826
        zval_ptr_dtor(&right_node.u.constant);
10672
826
      } else {
10673
680
        zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL);
10674
680
      }
10675
1.50k
    }
10676
10677
2.74k
    zval_ptr_dtor(&left_node.u.constant);
10678
2.74k
    return;
10679
2.74k
  }
10680
10681
12.9k
  opnum_jmpz = get_next_op_number();
10682
12.9k
  opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
10683
12.9k
    &left_node, NULL);
10684
10685
12.9k
  if (left_node.op_type == IS_TMP_VAR) {
10686
10.3k
    SET_NODE(opline_jmpz->result, &left_node);
10687
10.3k
    GET_NODE(result, opline_jmpz->result);
10688
10.3k
  } else {
10689
2.51k
    zend_make_tmp_result(result, opline_jmpz);
10690
2.51k
  }
10691
10692
12.9k
  zend_compile_expr(&right_node, right_ast);
10693
10694
12.9k
  opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
10695
12.9k
  SET_NODE(opline_bool->result, result);
10696
10697
12.9k
  zend_update_jump_target_to_next(opnum_jmpz);
10698
12.9k
}
10699
/* }}} */
10700
10701
static void zend_compile_post_incdec(znode *result, const zend_ast *ast) /* {{{ */
10702
11.1k
{
10703
11.1k
  zend_ast *var_ast = ast->child[0];
10704
11.1k
  ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
10705
10706
11.1k
  zend_ensure_writable_variable(var_ast);
10707
10708
11.1k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10709
850
    zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, false);
10710
850
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
10711
850
    zend_make_tmp_result(result, opline);
10712
10.3k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10713
847
    zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, false, false);
10714
847
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP;
10715
847
    zend_make_tmp_result(result, opline);
10716
9.48k
  } else {
10717
9.48k
    znode var_node;
10718
9.48k
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10719
9.48k
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10720
182
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10721
182
    }
10722
9.48k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC,
10723
9.48k
      &var_node, NULL);
10724
9.48k
  }
10725
11.1k
}
10726
/* }}} */
10727
10728
static void zend_compile_pre_incdec(znode *result, const zend_ast *ast) /* {{{ */
10729
4.05k
{
10730
4.05k
  zend_ast *var_ast = ast->child[0];
10731
4.05k
  ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
10732
10733
4.05k
  zend_ensure_writable_variable(var_ast);
10734
10735
4.05k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10736
757
    zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, false);
10737
757
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;
10738
757
    opline->result_type = IS_TMP_VAR;
10739
757
    result->op_type = IS_TMP_VAR;
10740
3.29k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10741
169
    zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, false, false);
10742
169
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP;
10743
169
    opline->result_type = IS_TMP_VAR;
10744
169
    result->op_type = IS_TMP_VAR;
10745
3.13k
  } else {
10746
3.13k
    znode var_node;
10747
3.13k
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10748
3.13k
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10749
112
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10750
112
    }
10751
3.13k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC,
10752
3.13k
      &var_node, NULL);
10753
3.13k
  }
10754
4.05k
}
10755
/* }}} */
10756
10757
static void zend_compile_cast(znode *result, const zend_ast *ast) /* {{{ */
10758
3.13k
{
10759
3.13k
  zend_ast *expr_ast = ast->child[0];
10760
3.13k
  znode expr_node;
10761
3.13k
  zend_op *opline;
10762
10763
3.13k
  zend_compile_expr(&expr_node, expr_ast);
10764
10765
3.13k
  if (ast->attr == _IS_BOOL) {
10766
267
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL);
10767
2.86k
  } else if (ast->attr == IS_NULL) {
10768
19
    zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
10769
2.84k
  } else {
10770
2.84k
    opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
10771
2.84k
    opline->extended_value = ast->attr;
10772
2.84k
  }
10773
3.13k
}
10774
/* }}} */
10775
10776
static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */
10777
3.74k
{
10778
3.74k
  zend_ast *cond_ast = ast->child[0];
10779
3.74k
  zend_ast *false_ast = ast->child[2];
10780
10781
3.74k
  znode cond_node, false_node;
10782
3.74k
  zend_op *opline_qm_assign;
10783
3.74k
  uint32_t opnum_jmp_set;
10784
10785
3.74k
  ZEND_ASSERT(ast->child[1] == NULL);
10786
10787
3.74k
  zend_compile_expr(&cond_node, cond_ast);
10788
10789
3.74k
  opnum_jmp_set = get_next_op_number();
10790
3.74k
  zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL);
10791
10792
3.74k
  zend_compile_expr(&false_node, false_ast);
10793
10794
3.74k
  opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10795
3.74k
  SET_NODE(opline_qm_assign->result, result);
10796
10797
3.74k
  zend_update_jump_target_to_next(opnum_jmp_set);
10798
3.74k
}
10799
/* }}} */
10800
10801
static void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
10802
9.97k
{
10803
9.97k
  zend_ast *cond_ast = ast->child[0];
10804
9.97k
  zend_ast *true_ast = ast->child[1];
10805
9.97k
  zend_ast *false_ast = ast->child[2];
10806
10807
9.97k
  znode cond_node, true_node, false_node;
10808
9.97k
  zend_op *opline_qm_assign2;
10809
9.97k
  uint32_t opnum_jmpz, opnum_jmp;
10810
10811
9.97k
  if (cond_ast->kind == ZEND_AST_CONDITIONAL
10812
1.52k
      && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) {
10813
1.10k
    if (cond_ast->child[1]) {
10814
27
      if (true_ast) {
10815
13
        zend_error(E_COMPILE_ERROR,
10816
13
          "Unparenthesized `a ? b : c ? d : e` is not supported. "
10817
13
          "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`");
10818
14
      } else {
10819
14
        zend_error(E_COMPILE_ERROR,
10820
14
          "Unparenthesized `a ? b : c ?: d` is not supported. "
10821
14
          "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`");
10822
14
      }
10823
1.08k
    } else {
10824
1.08k
      if (true_ast) {
10825
8
        zend_error(E_COMPILE_ERROR,
10826
8
          "Unparenthesized `a ?: b ? c : d` is not supported. "
10827
8
          "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`");
10828
1.07k
      } else {
10829
        /* This case is harmless:  (a ?: b) ?: c always produces the same result
10830
         * as a ?: (b ?: c). */
10831
1.07k
      }
10832
1.08k
    }
10833
1.10k
  }
10834
10835
9.97k
  if (!true_ast) {
10836
3.74k
    zend_compile_shorthand_conditional(result, ast);
10837
3.74k
    return;
10838
3.74k
  }
10839
10840
6.22k
  zend_compile_expr(&cond_node, cond_ast);
10841
10842
6.22k
  opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
10843
10844
6.22k
  zend_compile_expr(&true_node, true_ast);
10845
10846
6.22k
  zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL);
10847
10848
6.22k
  opnum_jmp = zend_emit_jump(0);
10849
10850
6.22k
  zend_update_jump_target_to_next(opnum_jmpz);
10851
10852
6.22k
  zend_compile_expr(&false_node, false_ast);
10853
10854
6.22k
  opline_qm_assign2 = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10855
6.22k
  SET_NODE(opline_qm_assign2->result, result);
10856
10857
6.22k
  zend_update_jump_target_to_next(opnum_jmp);
10858
6.22k
}
10859
/* }}} */
10860
10861
static void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
10862
1.75M
{
10863
1.75M
  zend_ast *expr_ast = ast->child[0];
10864
1.75M
  zend_ast *default_ast = ast->child[1];
10865
10866
1.75M
  znode expr_node, default_node;
10867
1.75M
  zend_op *opline;
10868
1.75M
  uint32_t opnum;
10869
10870
1.75M
  zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, false);
10871
10872
1.75M
  opnum = get_next_op_number();
10873
1.75M
  zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL);
10874
10875
1.75M
  zend_compile_expr(&default_node, default_ast);
10876
10877
1.75M
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL);
10878
1.75M
  SET_NODE(opline->result, result);
10879
10880
1.75M
  opline = &CG(active_op_array)->opcodes[opnum];
10881
1.75M
  opline->op2.opline_num = get_next_op_number();
10882
1.75M
}
10883
/* }}} */
10884
10885
467k
static void znode_dtor(zval *zv) {
10886
467k
  znode *node = Z_PTR_P(zv);
10887
467k
  if (node->op_type == IS_CONST) {
10888
9.46k
    zval_ptr_dtor_nogc(&node->u.constant);
10889
9.46k
  }
10890
467k
  efree(node);
10891
467k
}
10892
10893
static void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */
10894
188k
{
10895
188k
  zend_ast *var_ast = ast->child[0];
10896
188k
  zend_ast *default_ast = ast->child[1];
10897
10898
188k
  znode var_node_is, var_node_w, default_node, assign_node, *node;
10899
188k
  zend_op *opline;
10900
188k
  uint32_t coalesce_opnum;
10901
188k
  bool need_frees = false;
10902
10903
  /* Remember expressions compiled during the initial BP_VAR_IS lookup,
10904
   * to avoid double-evaluation when we compile again with BP_VAR_W. */
10905
188k
  HashTable *orig_memoized_exprs = CG(memoized_exprs);
10906
188k
  const zend_memoize_mode orig_memoize_mode = CG(memoize_mode);
10907
10908
188k
  zend_ensure_writable_variable(var_ast);
10909
188k
  if (is_this_fetch(var_ast)) {
10910
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
10911
5
  }
10912
10913
188k
  ALLOC_HASHTABLE(CG(memoized_exprs));
10914
188k
  zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0);
10915
10916
188k
  CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
10917
188k
  zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, false);
10918
10919
188k
  coalesce_opnum = get_next_op_number();
10920
188k
  zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL);
10921
10922
188k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
10923
188k
  if (var_ast->kind == ZEND_AST_DIM) {
10924
185k
    zend_compile_expr_with_potential_assign_to_self(&default_node, default_ast, var_ast);
10925
185k
  } else {
10926
3.15k
    zend_compile_expr(&default_node, default_ast);
10927
3.15k
  }
10928
10929
188k
  CG(memoize_mode) = ZEND_MEMOIZE_FETCH;
10930
188k
  zend_compile_var(&var_node_w, var_ast, BP_VAR_W, false);
10931
10932
  /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */
10933
188k
  opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
10934
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
10935
188k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
10936
188k
  switch (kind) {
10937
1.81k
    case ZEND_AST_VAR:
10938
1.81k
      zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node);
10939
1.81k
      break;
10940
860
    case ZEND_AST_STATIC_PROP:
10941
860
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
10942
860
      opline->result_type = IS_TMP_VAR;
10943
860
      var_node_w.op_type = IS_TMP_VAR;
10944
860
      zend_emit_op_data(&default_node);
10945
860
      assign_node = var_node_w;
10946
860
      break;
10947
184k
    case ZEND_AST_DIM:
10948
184k
      opline->opcode = ZEND_ASSIGN_DIM;
10949
184k
      opline->result_type = IS_TMP_VAR;
10950
184k
      var_node_w.op_type = IS_TMP_VAR;
10951
184k
      zend_emit_op_data(&default_node);
10952
184k
      assign_node = var_node_w;
10953
184k
      break;
10954
713
    case ZEND_AST_PROP:
10955
713
    case ZEND_AST_NULLSAFE_PROP:
10956
713
      opline->opcode = ZEND_ASSIGN_OBJ;
10957
713
      opline->result_type = IS_TMP_VAR;
10958
713
      var_node_w.op_type = IS_TMP_VAR;
10959
713
      zend_emit_op_data(&default_node);
10960
713
      assign_node = var_node_w;
10961
713
      break;
10962
0
    default: ZEND_UNREACHABLE();
10963
188k
  }
10964
10965
187k
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL);
10966
187k
  SET_NODE(opline->result, result);
10967
10968
576k
  ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10969
576k
    if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10970
184k
      need_frees = true;
10971
184k
      break;
10972
184k
    }
10973
576k
  } ZEND_HASH_FOREACH_END();
10974
10975
  /* Free DUPed expressions if there are any */
10976
187k
  if (need_frees) {
10977
184k
    uint32_t jump_opnum = zend_emit_jump(0);
10978
184k
    zend_update_jump_target_to_next(coalesce_opnum);
10979
1.10M
    ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10980
1.10M
      if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10981
455k
        zend_emit_op(NULL, ZEND_FREE, node, NULL);
10982
455k
      }
10983
1.10M
    } ZEND_HASH_FOREACH_END();
10984
184k
    zend_update_jump_target_to_next(jump_opnum);
10985
184k
  } else {
10986
3.08k
    zend_update_jump_target_to_next(coalesce_opnum);
10987
3.08k
  }
10988
10989
187k
  zend_hash_destroy(CG(memoized_exprs));
10990
187k
  FREE_HASHTABLE(CG(memoized_exprs));
10991
187k
  CG(memoized_exprs) = orig_memoized_exprs;
10992
187k
  CG(memoize_mode) = orig_memoize_mode;
10993
187k
}
10994
/* }}} */
10995
10996
static void zend_compile_print(znode *result, const zend_ast *ast) /* {{{ */
10997
9.15k
{
10998
9.15k
  zend_op *opline;
10999
9.15k
  zend_ast *expr_ast = ast->child[0];
11000
11001
9.15k
  znode expr_node;
11002
9.15k
  zend_compile_expr(&expr_node, expr_ast);
11003
11004
9.15k
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
11005
9.15k
  opline->extended_value = 1;
11006
11007
9.15k
  result->op_type = IS_CONST;
11008
9.15k
  ZVAL_LONG(&result->u.constant, 1);
11009
9.15k
}
11010
/* }}} */
11011
11012
static void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
11013
363k
{
11014
363k
  zend_ast *value_ast = ast->child[0];
11015
363k
  zend_ast *key_ast = ast->child[1];
11016
11017
363k
  znode value_node, key_node;
11018
363k
  znode *value_node_ptr = NULL, *key_node_ptr = NULL;
11019
363k
  zend_op *opline;
11020
363k
  bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
11021
11022
363k
  zend_mark_function_as_generator();
11023
11024
363k
  if (key_ast) {
11025
722
    zend_compile_expr(&key_node, key_ast);
11026
722
    key_node_ptr = &key_node;
11027
722
  }
11028
11029
363k
  if (value_ast) {
11030
361k
    if (returns_by_ref && zend_is_variable_or_call(value_ast)) {
11031
157k
      zend_assert_not_short_circuited(value_ast);
11032
157k
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
11033
203k
    } else {
11034
203k
      zend_compile_expr(&value_node, value_ast);
11035
203k
    }
11036
361k
    value_node_ptr = &value_node;
11037
361k
  }
11038
11039
363k
  opline = zend_emit_op_tmp(result, ZEND_YIELD, value_node_ptr, key_node_ptr);
11040
11041
363k
  if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
11042
157k
    opline->extended_value = ZEND_RETURNS_FUNCTION;
11043
157k
  }
11044
363k
}
11045
/* }}} */
11046
11047
static void zend_compile_yield_from(znode *result, const zend_ast *ast) /* {{{ */
11048
1.47k
{
11049
1.47k
  zend_ast *expr_ast = ast->child[0];
11050
1.47k
  znode expr_node;
11051
11052
1.47k
  zend_mark_function_as_generator();
11053
11054
1.47k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
11055
5
    zend_error_noreturn(E_COMPILE_ERROR,
11056
5
      "Cannot use \"yield from\" inside a by-reference generator");
11057
5
  }
11058
11059
1.46k
  zend_compile_expr(&expr_node, expr_ast);
11060
1.46k
  zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
11061
1.46k
}
11062
/* }}} */
11063
11064
static void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */
11065
794
{
11066
794
  zend_ast *obj_ast = ast->child[0];
11067
794
  zend_ast *class_ast = ast->child[1];
11068
11069
794
  znode obj_node, class_node;
11070
794
  zend_op *opline;
11071
11072
794
  zend_compile_expr(&obj_node, obj_ast);
11073
794
  if (obj_node.op_type == IS_CONST) {
11074
118
    zend_do_free(&obj_node);
11075
118
    result->op_type = IS_CONST;
11076
118
    ZVAL_FALSE(&result->u.constant);
11077
118
    return;
11078
118
  }
11079
11080
676
  zend_compile_class_ref(&class_node, class_ast,
11081
676
    ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION | ZEND_FETCH_CLASS_SILENT);
11082
11083
676
  opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL);
11084
11085
676
  if (class_node.op_type == IS_CONST) {
11086
418
    opline->op2_type = IS_CONST;
11087
418
    opline->op2.constant = zend_add_class_name_literal(
11088
418
      Z_STR(class_node.u.constant));
11089
418
    opline->extended_value = zend_alloc_cache_slot();
11090
418
  } else {
11091
258
    SET_NODE(opline->op2, &class_node);
11092
258
  }
11093
676
}
11094
/* }}} */
11095
11096
static void zend_compile_include_or_eval(znode *result, const zend_ast *ast) /* {{{ */
11097
8.15k
{
11098
8.15k
  zend_ast *expr_ast = ast->child[0];
11099
8.15k
  znode expr_node;
11100
8.15k
  zend_op *opline;
11101
11102
8.15k
  zend_do_extended_fcall_begin();
11103
8.15k
  zend_compile_expr(&expr_node, expr_ast);
11104
11105
8.15k
  opline = zend_emit_op_tmp(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL);
11106
8.15k
  opline->extended_value = ast->attr;
11107
11108
8.15k
  zend_do_extended_fcall_end();
11109
8.15k
}
11110
/* }}} */
11111
11112
static void zend_compile_isset_or_empty(znode *result, const zend_ast *ast) /* {{{ */
11113
7.98k
{
11114
7.98k
  zend_ast *var_ast = ast->child[0];
11115
11116
7.98k
  znode var_node;
11117
7.98k
  zend_op *opline = NULL;
11118
11119
7.98k
  ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY);
11120
11121
7.98k
  if (!zend_is_variable(var_ast)) {
11122
258
    if (ast->kind == ZEND_AST_EMPTY) {
11123
      /* empty(expr) can be transformed to !expr */
11124
234
      zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast);
11125
234
      zend_compile_expr(result, not_ast);
11126
234
      return;
11127
234
    } else {
11128
24
      zend_error_noreturn(E_COMPILE_ERROR,
11129
24
        "Cannot use isset() on the result of an expression "
11130
24
        "(you can use \"null !== expression\" instead)");
11131
24
    }
11132
258
  }
11133
11134
7.72k
  if (is_globals_fetch(var_ast)) {
11135
231
    result->op_type = IS_CONST;
11136
231
    ZVAL_BOOL(&result->u.constant, ast->kind == ZEND_AST_ISSET);
11137
231
    return;
11138
231
  }
11139
11140
7.49k
  if (is_global_var_fetch(var_ast)) {
11141
334
    if (!var_ast->child[1]) {
11142
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
11143
6
    }
11144
11145
328
    zend_compile_expr(&var_node, var_ast->child[1]);
11146
328
    if (var_node.op_type == IS_CONST) {
11147
274
      convert_to_string(&var_node.u.constant);
11148
274
    }
11149
11150
328
    opline = zend_emit_op_tmp(result, ZEND_ISSET_ISEMPTY_VAR, &var_node, NULL);
11151
328
    opline->extended_value =
11152
328
      ZEND_FETCH_GLOBAL | (ast->kind == ZEND_AST_EMPTY ? ZEND_ISEMPTY : 0);
11153
328
    return;
11154
334
  }
11155
11156
7.15k
  zend_short_circuiting_mark_inner(var_ast);
11157
7.15k
  switch (var_ast->kind) {
11158
1.80k
    case ZEND_AST_VAR:
11159
1.80k
      if (is_this_fetch(var_ast)) {
11160
133
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL);
11161
133
        CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
11162
1.66k
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_IS) == SUCCESS) {
11163
1.46k
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL);
11164
1.46k
      } else {
11165
199
        opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, false);
11166
199
        opline->opcode = ZEND_ISSET_ISEMPTY_VAR;
11167
199
      }
11168
1.80k
      break;
11169
3.87k
    case ZEND_AST_DIM:
11170
3.87k
      opline = zend_compile_dim(result, var_ast, BP_VAR_IS, /* by_ref */ false);
11171
3.87k
      opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
11172
3.87k
      break;
11173
1.10k
    case ZEND_AST_PROP:
11174
1.20k
    case ZEND_AST_NULLSAFE_PROP:
11175
1.20k
      opline = zend_compile_prop(result, var_ast, BP_VAR_IS, false);
11176
1.20k
      opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
11177
1.20k
      break;
11178
287
    case ZEND_AST_STATIC_PROP:
11179
287
      opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, false, false);
11180
287
      opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP;
11181
287
      break;
11182
0
    default: ZEND_UNREACHABLE();
11183
7.15k
  }
11184
11185
7.14k
  result->op_type = opline->result_type = IS_TMP_VAR;
11186
7.14k
  if (!(ast->kind == ZEND_AST_ISSET)) {
11187
1.16k
    opline->extended_value |= ZEND_ISEMPTY;
11188
1.16k
  }
11189
7.14k
}
11190
/* }}} */
11191
11192
static void zend_compile_silence(znode *result, const zend_ast *ast) /* {{{ */
11193
14.8M
{
11194
14.8M
  zend_ast *expr_ast = ast->child[0];
11195
14.8M
  znode silence_node;
11196
11197
14.8M
  zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL);
11198
11199
14.8M
  if (expr_ast->kind == ZEND_AST_VAR) {
11200
    /* For @$var we need to force a FETCH instruction, otherwise the CV access will
11201
     * happen outside the silenced section. */
11202
223k
    zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, false );
11203
14.6M
  } else {
11204
14.6M
    zend_compile_expr(result, expr_ast);
11205
14.6M
  }
11206
11207
14.8M
  zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL);
11208
14.8M
}
11209
/* }}} */
11210
11211
static void zend_compile_shell_exec(znode *result, const zend_ast *ast) /* {{{ */
11212
64.4k
{
11213
64.4k
  zend_ast *expr_ast = ast->child[0];
11214
11215
64.4k
  zval fn_name;
11216
64.4k
  zend_ast *name_ast, *args_ast, *call_ast;
11217
11218
64.4k
  zend_error(E_DEPRECATED, "The backtick (`) operator is deprecated, use shell_exec() instead");
11219
11220
64.4k
  ZVAL_STRING(&fn_name, "shell_exec");
11221
64.4k
  name_ast = zend_ast_create_zval(&fn_name);
11222
64.4k
  args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast);
11223
64.4k
  call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast);
11224
11225
64.4k
  zend_compile_expr(result, call_ast);
11226
11227
64.4k
  zval_ptr_dtor(&fn_name);
11228
64.4k
}
11229
/* }}} */
11230
11231
static void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
11232
234k
{
11233
234k
  zend_ast_list *list = zend_ast_get_list(ast);
11234
234k
  zend_op *opline;
11235
234k
  uint32_t i, opnum_init = -1;
11236
234k
  bool packed = true;
11237
11238
234k
  if (zend_try_ct_eval_array(&result->u.constant, ast)) {
11239
196k
    result->op_type = IS_CONST;
11240
196k
    return;
11241
196k
  }
11242
11243
  /* Empty arrays are handled at compile-time */
11244
37.6k
  ZEND_ASSERT(list->children > 0);
11245
11246
159k
  for (i = 0; i < list->children; ++i) {
11247
122k
    zend_ast *elem_ast = list->child[i];
11248
122k
    zend_ast *value_ast, *key_ast;
11249
122k
    bool by_ref;
11250
122k
    znode value_node, key_node, *key_node_ptr = NULL;
11251
11252
122k
    if (elem_ast == NULL) {
11253
0
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
11254
0
    }
11255
11256
122k
    value_ast = elem_ast->child[0];
11257
11258
122k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
11259
1.31k
      zend_compile_expr(&value_node, value_ast);
11260
1.31k
      if (i == 0) {
11261
992
        opnum_init = get_next_op_number();
11262
992
        opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
11263
992
      }
11264
1.31k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL);
11265
1.31k
      SET_NODE(opline->result, result);
11266
1.31k
      continue;
11267
1.31k
    }
11268
11269
121k
    key_ast = elem_ast->child[1];
11270
121k
    by_ref = elem_ast->attr;
11271
11272
121k
    if (key_ast) {
11273
9.29k
      zend_compile_expr(&key_node, key_ast);
11274
9.29k
      zend_handle_numeric_op(&key_node);
11275
9.29k
      key_node_ptr = &key_node;
11276
9.29k
    }
11277
11278
121k
    if (by_ref) {
11279
1.05k
      zend_ensure_writable_variable(value_ast);
11280
1.05k
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
11281
120k
    } else {
11282
120k
      zend_compile_expr(&value_node, value_ast);
11283
120k
    }
11284
11285
121k
    if (i == 0) {
11286
36.5k
      opnum_init = get_next_op_number();
11287
36.5k
      opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr);
11288
36.5k
      opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT;
11289
84.6k
    } else {
11290
84.6k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT,
11291
84.6k
        &value_node, key_node_ptr);
11292
84.6k
      SET_NODE(opline->result, result);
11293
84.6k
    }
11294
121k
    opline->extended_value |= by_ref;
11295
11296
121k
    if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) {
11297
5.15k
      packed = false;
11298
5.15k
    }
11299
121k
  }
11300
11301
  /* Add a flag to INIT_ARRAY if we know this array cannot be packed */
11302
37.5k
  if (!packed) {
11303
1.91k
    ZEND_ASSERT(opnum_init != (uint32_t)-1);
11304
1.91k
    opline = &CG(active_op_array)->opcodes[opnum_init];
11305
1.91k
    opline->extended_value |= ZEND_ARRAY_NOT_PACKED;
11306
1.91k
  }
11307
37.5k
}
11308
/* }}} */
11309
11310
static void zend_compile_const(znode *result, const zend_ast *ast) /* {{{ */
11311
9.78M
{
11312
9.78M
  zend_ast *name_ast = ast->child[0];
11313
11314
9.78M
  zend_op *opline;
11315
11316
9.78M
  bool is_fully_qualified;
11317
9.78M
  zend_string *orig_name = zend_ast_get_str(name_ast);
11318
9.78M
  zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified);
11319
11320
9.78M
  if (zend_string_equals_literal(resolved_name, "__COMPILER_HALT_OFFSET__") || (name_ast->attr != ZEND_NAME_RELATIVE && zend_string_equals_literal(orig_name, "__COMPILER_HALT_OFFSET__"))) {
11321
916
    zend_ast *last = CG(ast);
11322
11323
1.83k
    while (last && last->kind == ZEND_AST_STMT_LIST) {
11324
1.07k
      const zend_ast_list *list = zend_ast_get_list(last);
11325
1.07k
      if (list->children == 0) {
11326
156
        break;
11327
156
      }
11328
923
      last = list->child[list->children-1];
11329
923
    }
11330
916
    if (last && last->kind == ZEND_AST_HALT_COMPILER) {
11331
176
      result->op_type = IS_CONST;
11332
176
      ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
11333
176
      zend_string_release_ex(resolved_name, 0);
11334
176
      return;
11335
176
    }
11336
916
  }
11337
11338
9.78M
  if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
11339
31.0k
    result->op_type = IS_CONST;
11340
31.0k
    zend_string_release_ex(resolved_name, 0);
11341
31.0k
    return;
11342
31.0k
  }
11343
11344
9.75M
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
11345
9.75M
  opline->op2_type = IS_CONST;
11346
11347
9.75M
  if (is_fully_qualified || !FC(current_namespace)) {
11348
158k
    opline->op1.num = 0;
11349
158k
    opline->op2.constant = zend_add_const_name_literal(
11350
158k
      resolved_name, false);
11351
9.59M
  } else {
11352
9.59M
    opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE;
11353
9.59M
    opline->op2.constant = zend_add_const_name_literal(
11354
9.59M
      resolved_name, true);
11355
9.59M
  }
11356
9.75M
  opline->extended_value = zend_alloc_cache_slot();
11357
9.75M
}
11358
/* }}} */
11359
11360
static void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */
11361
81.8k
{
11362
81.8k
  zend_ast *class_ast;
11363
81.8k
  zend_ast *const_ast;
11364
81.8k
  znode class_node, const_node;
11365
81.8k
  zend_op *opline;
11366
11367
81.8k
  zend_eval_const_expr(&ast->child[0]);
11368
81.8k
  zend_eval_const_expr(&ast->child[1]);
11369
11370
81.8k
  class_ast = ast->child[0];
11371
81.8k
  const_ast = ast->child[1];
11372
11373
81.8k
  if (class_ast->kind == ZEND_AST_ZVAL && const_ast->kind == ZEND_AST_ZVAL) {
11374
30.3k
    zval *const_zv = zend_ast_get_zval(const_ast);
11375
30.3k
    if (Z_TYPE_P(const_zv) == IS_STRING) {
11376
29.9k
      zend_string *const_str = Z_STR_P(const_zv);
11377
29.9k
      zend_string *resolved_name = zend_resolve_class_name_ast(class_ast);
11378
29.9k
      if (zend_try_ct_eval_class_const(&result->u.constant, resolved_name, const_str)) {
11379
310
        result->op_type = IS_CONST;
11380
310
        zend_string_release_ex(resolved_name, 0);
11381
310
        return;
11382
310
      }
11383
29.6k
      zend_string_release_ex(resolved_name, 0);
11384
29.6k
    }
11385
30.3k
  }
11386
11387
81.5k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
11388
11389
81.5k
  zend_compile_expr(&const_node, const_ast);
11390
11391
81.5k
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node);
11392
11393
81.5k
  zend_set_class_name_op1(opline, &class_node);
11394
11395
81.5k
  if (opline->op1_type == IS_CONST || opline->op2_type == IS_CONST) {
11396
80.6k
    opline->extended_value = zend_alloc_cache_slots(2);
11397
80.6k
  }
11398
81.5k
}
11399
/* }}} */
11400
11401
static void zend_compile_class_name(znode *result, const zend_ast *ast) /* {{{ */
11402
5.60k
{
11403
5.60k
  zend_ast *class_ast = ast->child[0];
11404
11405
5.60k
  if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) {
11406
3.47k
    result->op_type = IS_CONST;
11407
3.47k
    return;
11408
3.47k
  }
11409
11410
2.12k
  if (class_ast->kind == ZEND_AST_ZVAL) {
11411
652
    zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11412
652
    opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
11413
1.47k
  } else {
11414
1.47k
    znode expr_node;
11415
1.47k
    zend_compile_expr(&expr_node, class_ast);
11416
1.47k
    if (expr_node.op_type == IS_CONST) {
11417
      /* Unlikely case that happen if class_ast is constant folded.
11418
       * Handle it here, to avoid needing a CONST specialization in the VM. */
11419
11
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"::class\" on %s",
11420
11
        zend_zval_value_name(&expr_node.u.constant));
11421
11
    }
11422
11423
1.46k
    zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL);
11424
1.46k
  }
11425
2.12k
}
11426
/* }}} */
11427
11428
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */
11429
343k
{
11430
343k
  if (num == 0) {
11431
83.6k
    result->op_type = IS_TMP_VAR;
11432
83.6k
    result->u.op.var = -1;
11433
83.6k
    opline->opcode = ZEND_ROPE_INIT;
11434
259k
  } else {
11435
259k
    opline->opcode = ZEND_ROPE_ADD;
11436
259k
    SET_NODE(opline->op1, result);
11437
259k
  }
11438
343k
  SET_NODE(opline->op2, elem_node);
11439
343k
  SET_NODE(opline->result, result);
11440
343k
  opline->extended_value = num;
11441
343k
  return opline;
11442
343k
}
11443
/* }}} */
11444
11445
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */
11446
284k
{
11447
284k
  zend_op *opline = get_next_op();
11448
11449
284k
  if (num == 0) {
11450
17.7k
    result->op_type = IS_TMP_VAR;
11451
17.7k
    result->u.op.var = -1;
11452
17.7k
    opline->opcode = ZEND_ROPE_INIT;
11453
266k
  } else {
11454
266k
    opline->opcode = ZEND_ROPE_ADD;
11455
266k
    SET_NODE(opline->op1, result);
11456
266k
  }
11457
284k
  SET_NODE(opline->op2, elem_node);
11458
284k
  SET_NODE(opline->result, result);
11459
284k
  opline->extended_value = num;
11460
284k
  return opline;
11461
284k
}
11462
/* }}} */
11463
11464
static void zend_compile_rope_finalize(znode *result, uint32_t rope_elements, zend_op *init_opline, zend_op *opline)
11465
101k
{
11466
101k
  if (rope_elements == 1) {
11467
2.40k
    if (opline->op2_type == IS_CONST) {
11468
615
      GET_NODE(result, opline->op2);
11469
615
      ZVAL_UNDEF(CT_CONSTANT(opline->op2));
11470
615
      SET_UNUSED(opline->op2);
11471
615
      MAKE_NOP(opline);
11472
1.78k
    } else {
11473
1.78k
      opline->opcode = ZEND_CAST;
11474
1.78k
      opline->extended_value = IS_STRING;
11475
1.78k
      opline->op1_type = opline->op2_type;
11476
1.78k
      opline->op1 = opline->op2;
11477
1.78k
      SET_UNUSED(opline->op2);
11478
1.78k
      zend_make_tmp_result(result, opline);
11479
1.78k
    }
11480
99.0k
  } else if (rope_elements == 2) {
11481
7.19k
    opline->opcode = ZEND_FAST_CONCAT;
11482
7.19k
    opline->extended_value = 0;
11483
7.19k
    opline->op1_type = init_opline->op2_type;
11484
7.19k
    opline->op1 = init_opline->op2;
11485
7.19k
    zend_make_tmp_result(result, opline);
11486
7.19k
    MAKE_NOP(init_opline);
11487
91.8k
  } else {
11488
91.8k
    uint32_t var;
11489
11490
91.8k
    init_opline->extended_value = rope_elements;
11491
91.8k
    opline->opcode = ZEND_ROPE_END;
11492
91.8k
    zend_make_tmp_result(result, opline);
11493
91.8k
    var = opline->op1.var = get_temporary_variable();
11494
11495
    /* Allocates the necessary number of zval slots to keep the rope */
11496
91.8k
    uint32_t i = ((rope_elements * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
11497
348k
    while (i > 1) {
11498
256k
      get_temporary_variable();
11499
256k
      i--;
11500
256k
    }
11501
11502
    /* Update all the previous opcodes to use the same variable */
11503
841k
    while (opline != init_opline) {
11504
749k
      opline--;
11505
749k
      if (opline->opcode == ZEND_ROPE_ADD &&
11506
440k
          opline->result.var == (uint32_t)-1) {
11507
426k
        opline->op1.var = var;
11508
426k
        opline->result.var = var;
11509
426k
      } else if (opline->opcode == ZEND_ROPE_INIT &&
11510
92.5k
                 opline->result.var == (uint32_t)-1) {
11511
91.8k
        opline->result.var = var;
11512
91.8k
      }
11513
749k
    }
11514
91.8k
  }
11515
101k
}
11516
11517
static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
11518
98.2k
{
11519
98.2k
  uint32_t i, j;
11520
98.2k
  uint32_t rope_init_lineno = -1;
11521
98.2k
  zend_op *opline = NULL, *init_opline;
11522
98.2k
  znode elem_node, last_const_node;
11523
98.2k
  zend_ast_list *list = zend_ast_get_list(ast);
11524
98.2k
  uint32_t reserved_op_number = -1;
11525
11526
98.2k
  ZEND_ASSERT(list->children > 0);
11527
11528
98.2k
  j = 0;
11529
98.2k
  last_const_node.op_type = IS_UNUSED;
11530
717k
  for (i = 0; i < list->children; i++) {
11531
619k
    zend_ast *encaps_var = list->child[i];
11532
11533
619k
    if (encaps_var->attr & (ZEND_ENCAPS_VAR_DOLLAR_CURLY|ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11534
64.0k
      if ((encaps_var->kind == ZEND_AST_VAR || encaps_var->kind == ZEND_AST_DIM) && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY)) {
11535
958
        zend_error(E_DEPRECATED, "Using ${var} in strings is deprecated, use {$var} instead");
11536
63.0k
      } else if (encaps_var->kind == ZEND_AST_VAR && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11537
63.0k
        zend_error(E_DEPRECATED, "Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead");
11538
63.0k
      }
11539
64.0k
    }
11540
11541
619k
    zend_compile_expr(&elem_node, encaps_var);
11542
11543
619k
    if (elem_node.op_type == IS_CONST) {
11544
344k
      convert_to_string(&elem_node.u.constant);
11545
11546
344k
      if (Z_STRLEN(elem_node.u.constant) == 0) {
11547
1.41k
        zval_ptr_dtor(&elem_node.u.constant);
11548
343k
      } else if (last_const_node.op_type == IS_CONST) {
11549
0
        concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant);
11550
0
        zval_ptr_dtor(&elem_node.u.constant);
11551
343k
      } else {
11552
343k
        last_const_node.op_type = IS_CONST;
11553
343k
        ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant);
11554
        /* Reserve place for ZEND_ROPE_ADD instruction */
11555
343k
        reserved_op_number = get_next_op_number();
11556
343k
        opline = get_next_op();
11557
343k
        opline->opcode = ZEND_NOP;
11558
343k
      }
11559
344k
      continue;
11560
344k
    } else {
11561
275k
      if (j == 0) {
11562
98.2k
        if (last_const_node.op_type == IS_CONST) {
11563
83.6k
          rope_init_lineno = reserved_op_number;
11564
83.6k
        } else {
11565
14.5k
          rope_init_lineno = get_next_op_number();
11566
14.5k
        }
11567
98.2k
      }
11568
275k
      if (last_const_node.op_type == IS_CONST) {
11569
251k
        opline = &CG(active_op_array)->opcodes[reserved_op_number];
11570
251k
        zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11571
251k
        last_const_node.op_type = IS_UNUSED;
11572
251k
      }
11573
275k
      opline = zend_compile_rope_add(result, j++, &elem_node);
11574
275k
    }
11575
619k
  }
11576
11577
98.2k
  if (j == 0) {
11578
0
    result->op_type = IS_CONST;
11579
0
    if (last_const_node.op_type == IS_CONST) {
11580
0
      ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant);
11581
0
    } else {
11582
0
      ZVAL_EMPTY_STRING(&result->u.constant);
11583
      /* empty string */
11584
0
    }
11585
0
    CG(active_op_array)->last = reserved_op_number - 1;
11586
0
    return;
11587
98.2k
  } else if (last_const_node.op_type == IS_CONST) {
11588
91.7k
    opline = &CG(active_op_array)->opcodes[reserved_op_number];
11589
91.7k
    opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11590
91.7k
  }
11591
98.2k
  init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
11592
98.2k
  zend_compile_rope_finalize(result, j, init_opline, opline);
11593
98.2k
}
11594
/* }}} */
11595
11596
static void zend_compile_magic_const(znode *result, const zend_ast *ast) /* {{{ */
11597
16.2k
{
11598
16.2k
  zend_op *opline;
11599
11600
16.2k
  if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) {
11601
15.3k
    result->op_type = IS_CONST;
11602
15.3k
    return;
11603
15.3k
  }
11604
11605
972
  ZEND_ASSERT(ast->attr == T_CLASS_C &&
11606
972
              CG(active_class_entry) &&
11607
972
              (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
11608
11609
972
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11610
972
  opline->op1.num = ZEND_FETCH_CLASS_SELF;
11611
972
}
11612
/* }}} */
11613
11614
static bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */
11615
63.6k
{
11616
63.6k
  return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP
11617
57.6k
    || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL
11618
56.7k
    || kind == ZEND_AST_AND || kind == ZEND_AST_OR
11619
56.4k
    || kind == ZEND_AST_UNARY_OP
11620
55.6k
    || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS
11621
54.6k
    || kind == ZEND_AST_CAST
11622
54.1k
    || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM
11623
52.7k
    || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM
11624
49.3k
    || kind == ZEND_AST_UNPACK
11625
49.2k
    || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST
11626
24.1k
    || kind == ZEND_AST_CLASS_NAME
11627
24.0k
    || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE
11628
22.9k
    || kind == ZEND_AST_CONST_ENUM_INIT
11629
4.30k
    || kind == ZEND_AST_NEW || kind == ZEND_AST_ARG_LIST
11630
2.31k
    || kind == ZEND_AST_NAMED_ARG
11631
2.25k
    || kind == ZEND_AST_PROP || kind == ZEND_AST_NULLSAFE_PROP
11632
1.72k
    || kind == ZEND_AST_CLOSURE
11633
1.59k
    || kind == ZEND_AST_CALL || kind == ZEND_AST_STATIC_CALL || kind == ZEND_AST_CALLABLE_CONVERT;
11634
63.6k
}
11635
/* }}} */
11636
11637
static void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
11638
3.26k
{
11639
3.26k
  zend_ast *ast = *ast_ptr;
11640
3.26k
  zend_ast *class_ast = ast->child[0];
11641
3.26k
  zend_string *class_name;
11642
3.26k
  int fetch_type;
11643
11644
3.26k
  if (class_ast->kind != ZEND_AST_ZVAL) {
11645
22
    zend_error_noreturn(E_COMPILE_ERROR,
11646
22
      "Dynamic class names are not allowed in compile-time class constant references");
11647
22
  }
11648
3.24k
  if (Z_TYPE_P(zend_ast_get_zval(class_ast)) != IS_STRING) {
11649
10
    zend_throw_error(NULL, "Class name must be a valid object or a string");
11650
10
  }
11651
11652
3.24k
  class_name = zend_ast_get_str(class_ast);
11653
3.24k
  fetch_type = zend_get_class_fetch_type(class_name);
11654
11655
3.24k
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11656
5
    zend_error_noreturn(E_COMPILE_ERROR,
11657
5
      "\"static::\" is not allowed in compile-time constants");
11658
5
  }
11659
11660
3.24k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
11661
2.44k
    zend_string *tmp = zend_resolve_class_name_ast(class_ast);
11662
11663
2.44k
    zend_string_release_ex(class_name, 0);
11664
2.44k
    if (tmp != class_name) {
11665
439
      zval *zv = zend_ast_get_zval(class_ast);
11666
439
      ZVAL_STR(zv, tmp);
11667
439
      class_ast->attr = ZEND_NAME_FQ;
11668
439
    }
11669
2.44k
  }
11670
11671
3.24k
  ast->attr |= ZEND_FETCH_CLASS_EXCEPTION;
11672
3.24k
}
11673
/* }}} */
11674
11675
static void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */
11676
74
{
11677
74
  zend_ast *ast = *ast_ptr;
11678
74
  zend_ast *class_ast = ast->child[0];
11679
74
  if (class_ast->kind != ZEND_AST_ZVAL) {
11680
5
    zend_error_noreturn(E_COMPILE_ERROR,
11681
5
      "(expression)::class cannot be used in constant expressions");
11682
5
  }
11683
11684
69
  zend_string *class_name = zend_ast_get_str(class_ast);
11685
69
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
11686
11687
69
  switch (fetch_type) {
11688
64
    case ZEND_FETCH_CLASS_SELF:
11689
64
    case ZEND_FETCH_CLASS_PARENT:
11690
      /* For the const-eval representation store the fetch type instead of the name. */
11691
64
      zend_string_release(class_name);
11692
64
      ast->child[0] = NULL;
11693
64
      ast->attr = fetch_type;
11694
64
      return;
11695
5
    case ZEND_FETCH_CLASS_STATIC:
11696
5
      zend_error_noreturn(E_COMPILE_ERROR,
11697
5
        "static::class cannot be used for compile-time class name resolution");
11698
0
    default: ZEND_UNREACHABLE();
11699
69
  }
11700
69
}
11701
11702
static void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */
11703
21.8k
{
11704
21.8k
  zend_ast *ast = *ast_ptr;
11705
21.8k
  zend_ast *name_ast = ast->child[0];
11706
21.8k
  zend_string *orig_name = zend_ast_get_str(name_ast);
11707
21.8k
  bool is_fully_qualified;
11708
21.8k
  zval result;
11709
21.8k
  zend_string *resolved_name;
11710
11711
21.8k
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11712
11713
21.8k
  resolved_name = zend_resolve_const_name(
11714
21.8k
    orig_name, name_ast->attr, &is_fully_qualified);
11715
11716
21.8k
  if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
11717
0
    zend_string_release_ex(resolved_name, 0);
11718
0
    zend_ast_destroy(ast);
11719
0
    *ast_ptr = zend_ast_create_zval(&result);
11720
0
    return;
11721
0
  }
11722
11723
21.8k
  zend_ast_destroy(ast);
11724
21.8k
  *ast_ptr = zend_ast_create_constant(resolved_name,
11725
21.8k
    !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0);
11726
21.8k
}
11727
/* }}} */
11728
11729
static void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
11730
322
{
11731
322
  zend_ast *ast = *ast_ptr;
11732
11733
  /* Other cases already resolved by constant folding */
11734
322
  ZEND_ASSERT(ast->attr == T_CLASS_C);
11735
11736
322
  zend_ast_destroy(ast);
11737
322
  *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS);
11738
322
}
11739
/* }}} */
11740
11741
static void zend_compile_const_expr_class_reference(zend_ast *class_ast)
11742
1.14k
{
11743
1.14k
  if (class_ast->kind == ZEND_AST_CLASS) {
11744
0
    zend_error_noreturn(E_COMPILE_ERROR,
11745
0
      "Cannot use anonymous class in constant expression");
11746
0
  }
11747
1.14k
  if (class_ast->kind != ZEND_AST_ZVAL) {
11748
4
    zend_error_noreturn(E_COMPILE_ERROR,
11749
4
      "Cannot use dynamic class name in constant expression");
11750
4
  }
11751
11752
1.14k
  zend_string *class_name = zend_resolve_class_name_ast(class_ast);
11753
1.14k
  int fetch_type = zend_get_class_fetch_type(class_name);
11754
1.14k
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11755
7
    zend_error_noreturn(E_COMPILE_ERROR,
11756
7
      "\"static\" is not allowed in compile-time constants");
11757
7
  }
11758
11759
1.13k
  zval *class_ast_zv = zend_ast_get_zval(class_ast);
11760
1.13k
  zval_ptr_dtor_nogc(class_ast_zv);
11761
1.13k
  ZVAL_STR(class_ast_zv, class_name);
11762
1.13k
  class_ast->attr = fetch_type << ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT;
11763
1.13k
}
11764
11765
static void zend_compile_const_expr_new(zend_ast **ast_ptr)
11766
997
{
11767
997
  zend_ast *class_ast = (*ast_ptr)->child[0];
11768
997
  zend_compile_const_expr_class_reference(class_ast);
11769
11770
997
  const zend_ast *args_ast = (*ast_ptr)->child[1];
11771
997
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
11772
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
11773
5
  }
11774
997
}
11775
11776
static void zend_compile_const_expr_closure(zend_ast **ast_ptr)
11777
132
{
11778
132
  zend_ast_decl *closure_ast = (zend_ast_decl *) *ast_ptr;
11779
132
  const zend_ast *uses_ast = closure_ast->child[1];
11780
132
  if (!(closure_ast->flags & ZEND_ACC_STATIC)) {
11781
5
    zend_error_noreturn(E_COMPILE_ERROR,
11782
5
      "Closures in constant expressions must be static");
11783
5
  }
11784
127
  if (uses_ast) {
11785
5
    zend_error_noreturn(E_COMPILE_ERROR,
11786
5
      "Cannot use(...) variables in constant expression");
11787
5
  }
11788
11789
122
  znode node;
11790
122
  zend_op_array *op = zend_compile_func_decl(&node, (zend_ast*)closure_ast, FUNC_DECL_LEVEL_CONSTEXPR);
11791
11792
122
  zend_ast_destroy(*ast_ptr);
11793
122
  *ast_ptr = zend_ast_create_op_array(op);
11794
122
}
11795
11796
static void zend_compile_const_expr_fcc(zend_ast **ast_ptr)
11797
797
{
11798
797
  zend_ast **args_ast;
11799
797
  switch ((*ast_ptr)->kind) {
11800
648
    case ZEND_AST_CALL:
11801
648
      args_ast = &(*ast_ptr)->child[1];
11802
648
      break;
11803
149
    case ZEND_AST_STATIC_CALL:
11804
149
      args_ast = &(*ast_ptr)->child[2];
11805
149
      break;
11806
0
    default: ZEND_UNREACHABLE();
11807
797
  }
11808
797
  if ((*args_ast)->kind != ZEND_AST_CALLABLE_CONVERT) {
11809
34
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11810
34
  }
11811
763
  ZEND_MAP_PTR_NEW(((zend_ast_fcc *)*args_ast)->fptr);
11812
11813
763
  switch ((*ast_ptr)->kind) {
11814
615
    case ZEND_AST_CALL: {
11815
615
      zend_ast *name_ast = (*ast_ptr)->child[0];
11816
615
      if (name_ast->kind != ZEND_AST_ZVAL) {
11817
14
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic function name in constant expression");
11818
14
      }
11819
601
      zval *name_ast_zv = zend_ast_get_zval(name_ast);
11820
601
      if (Z_TYPE_P(name_ast_zv) != IS_STRING) {
11821
5
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal function name");
11822
5
      }
11823
601
      bool is_fully_qualified;
11824
596
      zend_string *name = zend_resolve_function_name(Z_STR_P(name_ast_zv), name_ast->attr, &is_fully_qualified);
11825
596
      zval_ptr_dtor_nogc(name_ast_zv);
11826
596
      ZVAL_STR(name_ast_zv, name);
11827
596
      if (is_fully_qualified) {
11828
93
        name_ast->attr = ZEND_NAME_FQ;
11829
93
      }
11830
596
      break;
11831
601
    }
11832
148
    case ZEND_AST_STATIC_CALL: {
11833
148
      zend_ast *class_ast = (*ast_ptr)->child[0];
11834
148
      zend_compile_const_expr_class_reference(class_ast);
11835
148
      zend_ast *method_ast = (*ast_ptr)->child[1];
11836
148
      if (method_ast->kind != ZEND_AST_ZVAL) {
11837
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic method name in constant expression");
11838
0
      }
11839
148
      if (Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING) {
11840
0
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal method name");
11841
0
      }
11842
148
      break;
11843
148
    }
11844
148
    default: ZEND_UNREACHABLE();
11845
763
  }
11846
763
}
11847
11848
static void zend_compile_const_expr_args(zend_ast **ast_ptr)
11849
989
{
11850
989
  zend_ast_list *list = zend_ast_get_list(*ast_ptr);
11851
989
  bool uses_named_args = false;
11852
1.39k
  for (uint32_t i = 0; i < list->children; i++) {
11853
409
    const zend_ast *arg = list->child[i];
11854
409
    if (arg->kind == ZEND_AST_UNPACK) {
11855
2
      zend_error_noreturn(E_COMPILE_ERROR,
11856
2
        "Argument unpacking in constant expressions is not supported");
11857
2
    }
11858
407
    if (arg->kind == ZEND_AST_NAMED_ARG) {
11859
67
      uses_named_args = true;
11860
340
    } else if (uses_named_args) {
11861
2
      zend_error_noreturn(E_COMPILE_ERROR,
11862
2
        "Cannot use positional argument after named argument");
11863
2
    }
11864
407
  }
11865
985
  if (uses_named_args) {
11866
64
    list->attr = 1;
11867
64
  }
11868
985
}
11869
11870
typedef struct {
11871
  /* Whether the value of this expression may differ on each evaluation. */
11872
  bool allow_dynamic;
11873
} const_expr_context;
11874
11875
static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */
11876
251k
{
11877
251k
  const const_expr_context *ctx = context;
11878
251k
  zend_ast *ast = *ast_ptr;
11879
251k
  if (ast == NULL || ast->kind == ZEND_AST_ZVAL) {
11880
188k
    return;
11881
188k
  }
11882
11883
63.6k
  if (!zend_is_allowed_in_const_expr(ast->kind)) {
11884
62
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11885
62
  }
11886
11887
63.5k
  switch (ast->kind) {
11888
3.26k
    case ZEND_AST_CLASS_CONST:
11889
3.26k
      zend_compile_const_expr_class_const(ast_ptr);
11890
3.26k
      break;
11891
74
    case ZEND_AST_CLASS_NAME:
11892
74
      zend_compile_const_expr_class_name(ast_ptr);
11893
74
      break;
11894
21.8k
    case ZEND_AST_CONST:
11895
21.8k
      zend_compile_const_expr_const(ast_ptr);
11896
21.8k
      break;
11897
322
    case ZEND_AST_MAGIC_CONST:
11898
322
      zend_compile_const_expr_magic_const(ast_ptr);
11899
322
      break;
11900
455
    case ZEND_AST_CAST:
11901
455
      if (ast->attr == IS_OBJECT && !ctx->allow_dynamic) {
11902
9
        zend_error_noreturn(E_COMPILE_ERROR,
11903
9
          "Object casts are not supported in this context");
11904
9
      }
11905
446
      break;
11906
1.00k
    case ZEND_AST_NEW:
11907
1.00k
      if (!ctx->allow_dynamic) {
11908
6
        zend_error_noreturn(E_COMPILE_ERROR,
11909
6
          "New expressions are not supported in this context");
11910
6
      }
11911
997
      zend_compile_const_expr_new(ast_ptr);
11912
997
      break;
11913
989
    case ZEND_AST_ARG_LIST:
11914
989
      zend_compile_const_expr_args(ast_ptr);
11915
989
      break;
11916
132
    case ZEND_AST_CLOSURE:
11917
132
      zend_compile_const_expr_closure(ast_ptr);
11918
      /* Return, because we do not want to traverse the children. */
11919
132
      return;
11920
648
    case ZEND_AST_CALL:
11921
797
    case ZEND_AST_STATIC_CALL:
11922
797
      zend_compile_const_expr_fcc(ast_ptr);
11923
797
      break;
11924
63.5k
  }
11925
11926
63.3k
  zend_ast_apply(ast, zend_compile_const_expr, context);
11927
63.3k
}
11928
/* }}} */
11929
11930
void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynamic) /* {{{ */
11931
114k
{
11932
114k
  const_expr_context context;
11933
114k
  context.allow_dynamic = allow_dynamic;
11934
11935
114k
  zend_eval_const_expr(ast_ptr);
11936
114k
  zend_compile_const_expr(ast_ptr, &context);
11937
114k
  if ((*ast_ptr)->kind != ZEND_AST_ZVAL) {
11938
    /* Replace with compiled AST zval representation. */
11939
38.5k
    zval ast_zv;
11940
38.5k
    ZVAL_AST(&ast_zv, zend_ast_copy(*ast_ptr));
11941
38.5k
    zend_ast_destroy(*ast_ptr);
11942
38.5k
    *ast_ptr = zend_ast_create_zval(&ast_zv);
11943
38.5k
  }
11944
114k
  ZVAL_COPY(result, zend_ast_get_zval(*ast_ptr));
11945
114k
}
11946
/* }}} */
11947
11948
/* Same as compile_stmt, but with early binding */
11949
void zend_compile_top_stmt(zend_ast *ast) /* {{{ */
11950
662k
{
11951
662k
  if (!ast) {
11952
84.7k
    return;
11953
84.7k
  }
11954
11955
577k
  if (ast->kind == ZEND_AST_STMT_LIST) {
11956
106k
    const zend_ast_list *list = zend_ast_get_list(ast);
11957
106k
    uint32_t i;
11958
684k
    for (i = 0; i < list->children; ++i) {
11959
578k
      zend_compile_top_stmt(list->child[i]);
11960
578k
    }
11961
106k
    return;
11962
106k
  }
11963
11964
471k
  if (ast->kind == ZEND_AST_FUNC_DECL) {
11965
16.8k
    CG(zend_lineno) = ast->lineno;
11966
16.8k
    zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_TOPLEVEL);
11967
16.8k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11968
454k
  } else if (ast->kind == ZEND_AST_CLASS) {
11969
43.5k
    CG(zend_lineno) = ast->lineno;
11970
43.5k
    zend_compile_class_decl(NULL, ast, true);
11971
43.5k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11972
411k
  } else {
11973
411k
    zend_compile_stmt(ast);
11974
411k
  }
11975
471k
  if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) {
11976
461k
    zend_verify_namespace();
11977
461k
  }
11978
471k
}
11979
/* }}} */
11980
11981
static void zend_compile_stmt(zend_ast *ast) /* {{{ */
11982
10.5M
{
11983
10.5M
  if (!ast) {
11984
661k
    return;
11985
661k
  }
11986
11987
9.92M
  CG(zend_lineno) = ast->lineno;
11988
11989
9.92M
  if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) {
11990
0
    zend_do_extended_stmt(NULL);
11991
0
  }
11992
11993
9.92M
  switch (ast->kind) {
11994
2.18M
    case ZEND_AST_STMT_LIST:
11995
2.18M
      zend_compile_stmt_list(ast);
11996
2.18M
      break;
11997
2.42k
    case ZEND_AST_GLOBAL:
11998
2.42k
      zend_compile_global_var(ast);
11999
2.42k
      break;
12000
6.25k
    case ZEND_AST_STATIC:
12001
6.25k
      zend_compile_static_var(ast);
12002
6.25k
      break;
12003
8.47k
    case ZEND_AST_UNSET:
12004
8.47k
      zend_compile_unset(ast);
12005
8.47k
      break;
12006
227k
    case ZEND_AST_RETURN:
12007
227k
      zend_compile_return(ast);
12008
227k
      break;
12009
2.48M
    case ZEND_AST_ECHO:
12010
2.48M
      zend_compile_echo(ast);
12011
2.48M
      break;
12012
1.04k
    case ZEND_AST_BREAK:
12013
2.20k
    case ZEND_AST_CONTINUE:
12014
2.20k
      zend_compile_break_continue(ast);
12015
2.20k
      break;
12016
2.54k
    case ZEND_AST_GOTO:
12017
2.54k
      zend_compile_goto(ast);
12018
2.54k
      break;
12019
3.09k
    case ZEND_AST_LABEL:
12020
3.09k
      zend_compile_label(ast);
12021
3.09k
      break;
12022
6.42k
    case ZEND_AST_WHILE:
12023
6.42k
      zend_compile_while(ast);
12024
6.42k
      break;
12025
6.24k
    case ZEND_AST_DO_WHILE:
12026
6.24k
      zend_compile_do_while(ast);
12027
6.24k
      break;
12028
12.2k
    case ZEND_AST_FOR:
12029
12.2k
      zend_compile_for(ast);
12030
12.2k
      break;
12031
16.7k
    case ZEND_AST_FOREACH:
12032
16.7k
      zend_compile_foreach(ast);
12033
16.7k
      break;
12034
69.9k
    case ZEND_AST_IF:
12035
69.9k
      zend_compile_if(ast);
12036
69.9k
      break;
12037
10.9k
    case ZEND_AST_SWITCH:
12038
10.9k
      zend_compile_switch(ast);
12039
10.9k
      break;
12040
30.1k
    case ZEND_AST_TRY:
12041
30.1k
      zend_compile_try(ast);
12042
30.1k
      break;
12043
8.30k
    case ZEND_AST_DECLARE:
12044
8.30k
      zend_compile_declare(ast);
12045
8.30k
      break;
12046
1.57k
    case ZEND_AST_FUNC_DECL:
12047
131k
    case ZEND_AST_METHOD:
12048
131k
      zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_NESTED);
12049
131k
      break;
12050
18.6k
    case ZEND_AST_ENUM_CASE:
12051
18.6k
      zend_compile_enum_case(ast);
12052
18.6k
      break;
12053
32.4k
    case ZEND_AST_PROP_GROUP:
12054
32.4k
      zend_compile_prop_group(ast);
12055
32.4k
      break;
12056
6.44k
    case ZEND_AST_CLASS_CONST_GROUP:
12057
6.44k
      zend_compile_class_const_group(ast);
12058
6.44k
      break;
12059
112k
    case ZEND_AST_USE_TRAIT:
12060
112k
      zend_compile_use_trait(ast);
12061
112k
      break;
12062
282k
    case ZEND_AST_CLASS:
12063
282k
      zend_compile_class_decl(NULL, ast, false);
12064
282k
      break;
12065
175
    case ZEND_AST_GROUP_USE:
12066
175
      zend_compile_group_use(ast);
12067
175
      break;
12068
1.25k
    case ZEND_AST_USE:
12069
1.25k
      zend_compile_use(ast);
12070
1.25k
      break;
12071
4.84k
    case ZEND_AST_CONST_DECL:
12072
4.84k
      zend_compile_const_decl(ast);
12073
4.84k
      break;
12074
4.30k
    case ZEND_AST_NAMESPACE:
12075
4.30k
      zend_compile_namespace(ast);
12076
4.30k
      break;
12077
81
    case ZEND_AST_HALT_COMPILER:
12078
81
      zend_compile_halt_compiler(ast);
12079
81
      break;
12080
2.75k
    case ZEND_AST_THROW:
12081
2.75k
      zend_compile_expr(NULL, ast);
12082
2.75k
      break;
12083
428
    case ZEND_AST_CAST_VOID:
12084
428
      zend_compile_void_cast(NULL, ast);
12085
428
      break;
12086
128k
    case ZEND_AST_ASSIGN: {
12087
128k
      znode result;
12088
128k
      zend_compile_assign(&result, ast, /* stmt */ true, BP_VAR_R);
12089
128k
      zend_do_free(&result);
12090
128k
      return;
12091
1.57k
    }
12092
7.60k
    case ZEND_AST_ASSIGN_REF:
12093
7.60k
      zend_compile_assign_ref(NULL, ast, BP_VAR_R);
12094
7.60k
      return;
12095
4.10M
    default:
12096
4.10M
    {
12097
4.10M
      znode result;
12098
4.10M
      zend_compile_expr(&result, ast);
12099
4.10M
      zend_do_free(&result);
12100
4.10M
    }
12101
9.92M
  }
12102
12103
9.78M
  if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) {
12104
24.7k
    zend_emit_tick();
12105
24.7k
  }
12106
9.78M
}
12107
/* }}} */
12108
12109
static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */
12110
45.9M
{
12111
  /* CG(zend_lineno) = ast->lineno; */
12112
45.9M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
12113
12114
45.9M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
12115
576k
    zend_compile_memoized_expr(result, ast, BP_VAR_R);
12116
576k
    return;
12117
576k
  }
12118
12119
45.4M
  switch (ast->kind) {
12120
5.31M
    case ZEND_AST_ZVAL:
12121
5.31M
      ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast));
12122
5.31M
      result->op_type = IS_CONST;
12123
5.31M
      return;
12124
66.7k
    case ZEND_AST_ZNODE:
12125
66.7k
      *result = *zend_ast_get_znode(ast);
12126
66.7k
      return;
12127
680k
    case ZEND_AST_VAR:
12128
747k
    case ZEND_AST_DIM:
12129
780k
    case ZEND_AST_PROP:
12130
844k
    case ZEND_AST_NULLSAFE_PROP:
12131
849k
    case ZEND_AST_STATIC_PROP:
12132
4.39M
    case ZEND_AST_CALL:
12133
4.47M
    case ZEND_AST_METHOD_CALL:
12134
4.47M
    case ZEND_AST_NULLSAFE_METHOD_CALL:
12135
4.50M
    case ZEND_AST_STATIC_CALL:
12136
4.53M
    case ZEND_AST_PIPE:
12137
4.53M
      zend_compile_var(result, ast, BP_VAR_R, false);
12138
4.53M
      return;
12139
298k
    case ZEND_AST_ASSIGN:
12140
298k
      zend_compile_assign(result, ast, /* stmt */ false, BP_VAR_R);
12141
298k
      return;
12142
1.37k
    case ZEND_AST_ASSIGN_REF:
12143
1.37k
      zend_compile_assign_ref(result, ast, BP_VAR_R);
12144
1.37k
      return;
12145
75.9k
    case ZEND_AST_NEW:
12146
75.9k
      zend_compile_new(result, ast);
12147
75.9k
      return;
12148
95.7k
    case ZEND_AST_ASSIGN_OP:
12149
95.7k
      zend_compile_compound_assign(result, ast);
12150
95.7k
      return;
12151
3.60M
    case ZEND_AST_BINARY_OP:
12152
3.60M
      zend_compile_binary_op(result, ast);
12153
3.60M
      return;
12154
415k
    case ZEND_AST_GREATER:
12155
539k
    case ZEND_AST_GREATER_EQUAL:
12156
539k
      zend_compile_greater(result, ast);
12157
539k
      return;
12158
1.41M
    case ZEND_AST_UNARY_OP:
12159
1.41M
      zend_compile_unary_op(result, ast);
12160
1.41M
      return;
12161
34.3k
    case ZEND_AST_UNARY_PLUS:
12162
267k
    case ZEND_AST_UNARY_MINUS:
12163
267k
      zend_compile_unary_pm(result, ast);
12164
267k
      return;
12165
9.71k
    case ZEND_AST_AND:
12166
15.6k
    case ZEND_AST_OR:
12167
15.6k
      zend_compile_short_circuiting(result, ast);
12168
15.6k
      return;
12169
7.91k
    case ZEND_AST_POST_INC:
12170
11.1k
    case ZEND_AST_POST_DEC:
12171
11.1k
      zend_compile_post_incdec(result, ast);
12172
11.1k
      return;
12173
2.48k
    case ZEND_AST_PRE_INC:
12174
4.05k
    case ZEND_AST_PRE_DEC:
12175
4.05k
      zend_compile_pre_incdec(result, ast);
12176
4.05k
      return;
12177
3.13k
    case ZEND_AST_CAST:
12178
3.13k
      zend_compile_cast(result, ast);
12179
3.13k
      return;
12180
9.97k
    case ZEND_AST_CONDITIONAL:
12181
9.97k
      zend_compile_conditional(result, ast);
12182
9.97k
      return;
12183
1.75M
    case ZEND_AST_COALESCE:
12184
1.75M
      zend_compile_coalesce(result, ast);
12185
1.75M
      return;
12186
188k
    case ZEND_AST_ASSIGN_COALESCE:
12187
188k
      zend_compile_assign_coalesce(result, ast);
12188
188k
      return;
12189
9.15k
    case ZEND_AST_PRINT:
12190
9.15k
      zend_compile_print(result, ast);
12191
9.15k
      return;
12192
363k
    case ZEND_AST_YIELD:
12193
363k
      zend_compile_yield(result, ast);
12194
363k
      return;
12195
1.47k
    case ZEND_AST_YIELD_FROM:
12196
1.47k
      zend_compile_yield_from(result, ast);
12197
1.47k
      return;
12198
794
    case ZEND_AST_INSTANCEOF:
12199
794
      zend_compile_instanceof(result, ast);
12200
794
      return;
12201
8.15k
    case ZEND_AST_INCLUDE_OR_EVAL:
12202
8.15k
      zend_compile_include_or_eval(result, ast);
12203
8.15k
      return;
12204
6.58k
    case ZEND_AST_ISSET:
12205
7.98k
    case ZEND_AST_EMPTY:
12206
7.98k
      zend_compile_isset_or_empty(result, ast);
12207
7.98k
      return;
12208
14.8M
    case ZEND_AST_SILENCE:
12209
14.8M
      zend_compile_silence(result, ast);
12210
14.8M
      return;
12211
64.4k
    case ZEND_AST_SHELL_EXEC:
12212
64.4k
      zend_compile_shell_exec(result, ast);
12213
64.4k
      return;
12214
234k
    case ZEND_AST_ARRAY:
12215
234k
      zend_compile_array(result, ast);
12216
234k
      return;
12217
9.78M
    case ZEND_AST_CONST:
12218
9.78M
      zend_compile_const(result, ast);
12219
9.78M
      return;
12220
81.8k
    case ZEND_AST_CLASS_CONST:
12221
81.8k
      zend_compile_class_const(result, ast);
12222
81.8k
      return;
12223
5.60k
    case ZEND_AST_CLASS_NAME:
12224
5.60k
      zend_compile_class_name(result, ast);
12225
5.60k
      return;
12226
98.2k
    case ZEND_AST_ENCAPS_LIST:
12227
98.2k
      zend_compile_encaps_list(result, ast);
12228
98.2k
      return;
12229
16.2k
    case ZEND_AST_MAGIC_CONST:
12230
16.2k
      zend_compile_magic_const(result, ast);
12231
16.2k
      return;
12232
1.48M
    case ZEND_AST_CLOSURE:
12233
1.69M
    case ZEND_AST_ARROW_FUNC:
12234
1.69M
      zend_compile_func_decl(result, ast, FUNC_DECL_LEVEL_NESTED);
12235
1.69M
      return;
12236
3.50k
    case ZEND_AST_THROW:
12237
3.50k
      zend_compile_throw(result, ast);
12238
3.50k
      return;
12239
2.95k
    case ZEND_AST_MATCH:
12240
2.95k
      zend_compile_match(result, ast);
12241
2.95k
      return;
12242
0
    default:
12243
0
      ZEND_ASSERT(0 /* not supported */);
12244
45.4M
  }
12245
45.4M
}
12246
/* }}} */
12247
12248
static void zend_compile_expr(znode *result, zend_ast *ast)
12249
45.9M
{
12250
45.9M
  zend_check_stack_limit();
12251
12252
45.9M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12253
45.9M
  zend_compile_expr_inner(result, ast);
12254
45.9M
  zend_short_circuiting_commit(checkpoint, result, ast);
12255
45.9M
#if ZEND_DEBUG
12256
45.9M
  if (result) {
12257
    /* BP_VAR_R is not allowed to produce IS_VAR. */
12258
45.9M
    ZEND_ASSERT(result->op_type != IS_VAR);
12259
45.9M
  }
12260
45.9M
#endif
12261
45.9M
}
12262
12263
static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
12264
7.95M
{
12265
7.95M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
12266
12267
7.95M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
12268
735k
    switch (ast->kind) {
12269
29.8k
      case ZEND_AST_CALL:
12270
358k
      case ZEND_AST_METHOD_CALL:
12271
358k
      case ZEND_AST_NULLSAFE_METHOD_CALL:
12272
359k
      case ZEND_AST_STATIC_CALL:
12273
359k
        zend_compile_memoized_expr(result, ast, BP_VAR_W);
12274
        /* This might not actually produce an opcode, e.g. for expressions evaluated at comptime. */
12275
359k
        return NULL;
12276
735k
    }
12277
735k
  }
12278
12279
7.59M
  switch (ast->kind) {
12280
777k
    case ZEND_AST_VAR:
12281
777k
      return zend_compile_simple_var(result, ast, type, false);
12282
504k
    case ZEND_AST_DIM:
12283
504k
      return zend_compile_dim(result, ast, type, by_ref);
12284
42.8k
    case ZEND_AST_PROP:
12285
109k
    case ZEND_AST_NULLSAFE_PROP:
12286
109k
      return zend_compile_prop(result, ast, type, by_ref);
12287
8.30k
    case ZEND_AST_STATIC_PROP:
12288
8.30k
      return zend_compile_static_prop(result, ast, type, by_ref, false);
12289
4.03M
    case ZEND_AST_CALL:
12290
4.03M
      zend_compile_call(result, ast, type);
12291
4.03M
      return NULL;
12292
258k
    case ZEND_AST_METHOD_CALL:
12293
261k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
12294
261k
      zend_compile_method_call(result, ast, type);
12295
261k
      return NULL;
12296
34.1k
    case ZEND_AST_STATIC_CALL:
12297
34.1k
      zend_compile_static_call(result, ast, type);
12298
34.1k
      return NULL;
12299
30.6k
    case ZEND_AST_PIPE:
12300
30.6k
      zend_compile_pipe(result, ast, type);
12301
30.6k
      return NULL;
12302
2.66k
    case ZEND_AST_ZNODE:
12303
2.66k
      *result = *zend_ast_get_znode(ast);
12304
2.66k
      return NULL;
12305
2.77k
    case ZEND_AST_ASSIGN_REF:
12306
2.77k
      zend_compile_assign_ref(result, ast, type);
12307
2.77k
      return NULL;
12308
24
    case ZEND_AST_ASSIGN:
12309
24
      zend_compile_assign(result, ast, false, type);
12310
24
      return NULL;
12311
1.82M
    default:
12312
1.82M
      if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
12313
189
        zend_error_noreturn(E_COMPILE_ERROR,
12314
189
          "Cannot use temporary expression in write context");
12315
189
      }
12316
12317
1.82M
      zend_compile_expr(result, ast);
12318
1.82M
      return NULL;
12319
7.59M
  }
12320
7.59M
}
12321
12322
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12323
7.95M
{
12324
7.95M
  zend_check_stack_limit();
12325
12326
7.95M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12327
7.95M
  zend_op *opcode = zend_compile_var_inner(result, ast, type, by_ref);
12328
7.95M
  zend_short_circuiting_commit(checkpoint, result, ast);
12329
7.95M
#if ZEND_DEBUG
12330
7.95M
  if (result
12331
7.94M
   && (type == BP_VAR_R || type == BP_VAR_IS)
12332
   /* Don't check memoized result, as it will force BP_VAR_W even for BP_VAR_IS. */
12333
6.90M
   && CG(memoize_mode) == ZEND_MEMOIZE_NONE
12334
7.95M
  ) {
12335
    /* BP_VAR_{R,IS} is not allowed to produce IS_VAR. */
12336
6.53M
    ZEND_ASSERT(result->op_type != IS_VAR);
12337
6.53M
  }
12338
7.95M
#endif
12339
7.95M
  return opcode;
12340
7.95M
}
12341
12342
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12343
1.51M
{
12344
1.51M
  zend_check_stack_limit();
12345
12346
1.51M
  switch (ast->kind) {
12347
663k
    case ZEND_AST_VAR:
12348
663k
      return zend_compile_simple_var(result, ast, type, true);
12349
337k
    case ZEND_AST_DIM:
12350
337k
      return zend_delayed_compile_dim(result, ast, type, by_ref);
12351
32.4k
    case ZEND_AST_PROP:
12352
43.0k
    case ZEND_AST_NULLSAFE_PROP:
12353
43.0k
    {
12354
43.0k
      zend_op *opline = zend_delayed_compile_prop(result, ast, type);
12355
43.0k
      if (by_ref) {
12356
1.03k
        opline->extended_value |= ZEND_FETCH_REF;
12357
1.03k
      }
12358
43.0k
      return opline;
12359
32.4k
    }
12360
5.95k
    case ZEND_AST_STATIC_PROP:
12361
5.95k
      return zend_compile_static_prop(result, ast, type, by_ref, true);
12362
463k
    default:
12363
463k
      return zend_compile_var(result, ast, type, false);
12364
1.51M
  }
12365
1.51M
}
12366
/* }}} */
12367
12368
bool zend_try_ct_eval_cast(zval *result, uint32_t type, zval *op1)
12369
17.6k
{
12370
  /* NAN warns when casting */
12371
17.6k
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))) {
12372
0
    return false;
12373
0
  }
12374
17.6k
  switch (type) {
12375
87
    case _IS_BOOL:
12376
87
      ZVAL_BOOL(result, zend_is_true(op1));
12377
87
      return true;
12378
369
    case IS_LONG:
12379
369
      if (Z_TYPE_P(op1) == IS_DOUBLE && !ZEND_DOUBLE_FITS_LONG(Z_DVAL_P((op1)))) {
12380
109
        return false;
12381
109
      }
12382
260
      ZVAL_LONG(result, zval_get_long(op1));
12383
260
      return true;
12384
923
    case IS_DOUBLE:
12385
923
      ZVAL_DOUBLE(result, zval_get_double(op1));
12386
923
      return true;
12387
15.2k
    case IS_STRING:
12388
      /* Conversion from double to string takes into account run-time
12389
         'precision' setting and cannot be evaluated at compile-time */
12390
15.2k
      if (Z_TYPE_P(op1) != IS_ARRAY && Z_TYPE_P(op1) != IS_DOUBLE) {
12391
14.3k
        ZVAL_STR(result, zval_get_string(op1));
12392
14.3k
        return true;
12393
14.3k
      }
12394
857
      break;
12395
857
    case IS_ARRAY:
12396
284
      ZVAL_COPY(result, op1);
12397
284
      convert_to_array(result);
12398
284
      return true;
12399
17.6k
  }
12400
1.63k
  return false;
12401
17.6k
}
12402
12403
static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
12404
7.32M
{
12405
7.32M
  zend_ast *ast = *ast_ptr;
12406
7.32M
  zval result;
12407
12408
7.32M
  if (!ast) {
12409
1.87M
    return;
12410
1.87M
  }
12411
12412
5.45M
  zend_check_stack_limit();
12413
12414
5.45M
  switch (ast->kind) {
12415
81.7k
    case ZEND_AST_BINARY_OP:
12416
81.7k
      zend_eval_const_expr(&ast->child[0]);
12417
81.7k
      zend_eval_const_expr(&ast->child[1]);
12418
81.7k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12419
36.5k
        return;
12420
36.5k
      }
12421
12422
45.1k
      if (!zend_try_ct_eval_binary_op(&result, ast->attr,
12423
45.1k
          zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
12424
45.1k
      ) {
12425
27.7k
        return;
12426
27.7k
      }
12427
17.4k
      break;
12428
17.4k
    case ZEND_AST_GREATER:
12429
4.05k
    case ZEND_AST_GREATER_EQUAL:
12430
4.05k
      zend_eval_const_expr(&ast->child[0]);
12431
4.05k
      zend_eval_const_expr(&ast->child[1]);
12432
4.05k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12433
2.48k
        return;
12434
2.48k
      }
12435
12436
1.57k
      zend_ct_eval_greater(&result, ast->kind,
12437
1.57k
        zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]));
12438
1.57k
      break;
12439
2.59k
    case ZEND_AST_AND:
12440
3.93k
    case ZEND_AST_OR:
12441
3.93k
    {
12442
3.93k
      bool child0_is_true, child1_is_true;
12443
3.93k
      zend_eval_const_expr(&ast->child[0]);
12444
3.93k
      zend_eval_const_expr(&ast->child[1]);
12445
3.93k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12446
1.54k
        return;
12447
1.54k
      }
12448
12449
2.38k
      child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0]));
12450
2.38k
      if (child0_is_true == (ast->kind == ZEND_AST_OR)) {
12451
336
        ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR);
12452
336
        break;
12453
336
      }
12454
12455
2.05k
      if (ast->child[1]->kind != ZEND_AST_ZVAL) {
12456
546
        return;
12457
546
      }
12458
12459
1.50k
      child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1]));
12460
1.50k
      if (ast->kind == ZEND_AST_OR) {
12461
667
        ZVAL_BOOL(&result, child0_is_true || child1_is_true);
12462
838
      } else {
12463
838
        ZVAL_BOOL(&result, child0_is_true && child1_is_true);
12464
838
      }
12465
1.50k
      break;
12466
2.05k
    }
12467
8.70k
    case ZEND_AST_UNARY_OP:
12468
8.70k
      zend_eval_const_expr(&ast->child[0]);
12469
8.70k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12470
2.22k
        return;
12471
2.22k
      }
12472
12473
6.47k
      if (!zend_try_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12474
729
        return;
12475
729
      }
12476
5.74k
      break;
12477
5.74k
    case ZEND_AST_UNARY_PLUS:
12478
20.5k
    case ZEND_AST_UNARY_MINUS:
12479
20.5k
      zend_eval_const_expr(&ast->child[0]);
12480
20.5k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12481
3.26k
        return;
12482
3.26k
      }
12483
12484
17.2k
      if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) {
12485
4.73k
        return;
12486
4.73k
      }
12487
12.5k
      break;
12488
23.3k
    case ZEND_AST_COALESCE:
12489
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12490
23.3k
      if (ast->child[0]->kind == ZEND_AST_DIM) {
12491
548
        ast->child[0]->attr |= ZEND_DIM_IS;
12492
548
      }
12493
23.3k
      zend_eval_const_expr(&ast->child[0]);
12494
12495
23.3k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12496
        /* ensure everything was compile-time evaluated at least once */
12497
22.8k
        zend_eval_const_expr(&ast->child[1]);
12498
22.8k
        return;
12499
22.8k
      }
12500
12501
511
      if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) {
12502
447
        zend_eval_const_expr(&ast->child[1]);
12503
447
        *ast_ptr = ast->child[1];
12504
447
        ast->child[1] = NULL;
12505
447
        zend_ast_destroy(ast);
12506
447
      } else {
12507
64
        *ast_ptr = ast->child[0];
12508
64
        ast->child[0] = NULL;
12509
64
        zend_ast_destroy(ast);
12510
64
      }
12511
511
      return;
12512
2.01k
    case ZEND_AST_CONDITIONAL:
12513
2.01k
    {
12514
2.01k
      zend_ast **child, *child_ast;
12515
2.01k
      zend_eval_const_expr(&ast->child[0]);
12516
2.01k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12517
        /* ensure everything was compile-time evaluated at least once */
12518
1.84k
        if (ast->child[1]) {
12519
1.42k
          zend_eval_const_expr(&ast->child[1]);
12520
1.42k
        }
12521
1.84k
        zend_eval_const_expr(&ast->child[2]);
12522
1.84k
        return;
12523
1.84k
      }
12524
12525
173
      child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];
12526
173
      if (*child == NULL) {
12527
99
        child--;
12528
99
      }
12529
173
      child_ast = *child;
12530
173
      *child = NULL;
12531
173
      zend_ast_destroy(ast);
12532
173
      *ast_ptr = child_ast;
12533
173
      zend_eval_const_expr(ast_ptr);
12534
173
      return;
12535
2.01k
    }
12536
687k
    case ZEND_AST_DIM:
12537
687k
    {
12538
      /* constant expression should be always read context ... */
12539
687k
      const zval *container, *dim;
12540
12541
687k
      if (ast->child[1] == NULL) {
12542
40
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
12543
40
      }
12544
12545
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12546
687k
      if ((ast->attr & ZEND_DIM_IS) && ast->child[0]->kind == ZEND_AST_DIM) {
12547
853
        ast->child[0]->attr |= ZEND_DIM_IS;
12548
853
      }
12549
12550
687k
      zend_eval_const_expr(&ast->child[0]);
12551
687k
      zend_eval_const_expr(&ast->child[1]);
12552
687k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12553
669k
        return;
12554
669k
      }
12555
12556
17.7k
      container = zend_ast_get_zval(ast->child[0]);
12557
17.7k
      dim = zend_ast_get_zval(ast->child[1]);
12558
12559
17.7k
      if (Z_TYPE_P(container) == IS_ARRAY) {
12560
10.3k
        zval *el;
12561
10.3k
        if (Z_TYPE_P(dim) == IS_LONG) {
12562
1.20k
          el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim));
12563
1.20k
          if (el) {
12564
584
            ZVAL_COPY(&result, el);
12565
624
          } else {
12566
624
            return;
12567
624
          }
12568
9.12k
        } else if (Z_TYPE_P(dim) == IS_STRING) {
12569
8.99k
          el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim));
12570
8.99k
          if (el) {
12571
388
            ZVAL_COPY(&result, el);
12572
8.61k
          } else {
12573
8.61k
            return;
12574
8.61k
          }
12575
8.99k
        } else {
12576
124
          return; /* warning... handle at runtime */
12577
124
        }
12578
10.3k
      } else if (Z_TYPE_P(container) == IS_STRING) {
12579
7.26k
        zend_long offset;
12580
7.26k
        uint8_t c;
12581
7.26k
        if (Z_TYPE_P(dim) == IS_LONG) {
12582
5.73k
          offset = Z_LVAL_P(dim);
12583
5.73k
        } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) {
12584
1.03k
          return;
12585
1.03k
        }
12586
6.22k
        if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
12587
5.90k
          return;
12588
5.90k
        }
12589
322
        c = (uint8_t) Z_STRVAL_P(container)[offset];
12590
322
        ZVAL_CHAR(&result, c);
12591
322
      } else if (Z_TYPE_P(container) <= IS_FALSE) {
12592
12
        return; /* warning... handle at runtime */
12593
162
      } else {
12594
162
        return;
12595
162
      }
12596
1.29k
      break;
12597
17.7k
    }
12598
1.48M
    case ZEND_AST_ARRAY:
12599
1.48M
      if (!zend_try_ct_eval_array(&result, ast)) {
12600
1.44M
        return;
12601
1.44M
      }
12602
37.7k
      break;
12603
37.7k
    case ZEND_AST_MAGIC_CONST:
12604
1.51k
      if (!zend_try_ct_eval_magic_const(&result, ast)) {
12605
323
        return;
12606
323
      }
12607
1.19k
      break;
12608
158k
    case ZEND_AST_CONST:
12609
158k
    {
12610
158k
      zend_ast *name_ast = ast->child[0];
12611
158k
      bool is_fully_qualified;
12612
158k
      zend_string *resolved_name = zend_resolve_const_name(
12613
158k
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
12614
12615
158k
      if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
12616
152k
        zend_string_release_ex(resolved_name, 0);
12617
152k
        return;
12618
152k
      }
12619
12620
5.91k
      zend_string_release_ex(resolved_name, 0);
12621
5.91k
      break;
12622
158k
    }
12623
607k
    case ZEND_AST_CLASS_CONST:
12624
607k
    {
12625
607k
      zend_ast *class_ast;
12626
607k
      zend_ast *name_ast;
12627
607k
      zend_string *resolved_name;
12628
12629
607k
      zend_eval_const_expr(&ast->child[0]);
12630
607k
      zend_eval_const_expr(&ast->child[1]);
12631
12632
607k
      if (UNEXPECTED(ast->child[1]->kind != ZEND_AST_ZVAL
12633
607k
        || Z_TYPE_P(zend_ast_get_zval(ast->child[1])) != IS_STRING)) {
12634
2.40k
        return;
12635
2.40k
      }
12636
12637
604k
      class_ast = ast->child[0];
12638
604k
      name_ast = ast->child[1];
12639
12640
604k
      if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) {
12641
563k
        return;
12642
563k
      }
12643
12644
41.3k
      resolved_name = zend_resolve_class_name_ast(class_ast);
12645
41.3k
      if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) {
12646
41.1k
        zend_string_release_ex(resolved_name, 0);
12647
41.1k
        return;
12648
41.1k
      }
12649
12650
218
      zend_string_release_ex(resolved_name, 0);
12651
218
      break;
12652
41.3k
    }
12653
2.92k
    case ZEND_AST_CLASS_NAME:
12654
2.92k
    {
12655
2.92k
      zend_ast *class_ast = ast->child[0];
12656
2.92k
      if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) {
12657
1.30k
        return;
12658
1.30k
      }
12659
1.62k
      break;
12660
2.92k
    }
12661
    // TODO: We should probably use zend_ast_apply to recursively walk nodes without
12662
    // special handling. It is required that all nodes that are part of a const expr
12663
    // are visited. Probably we should be distinguishing evaluation of const expr and
12664
    // normal exprs here.
12665
5.08k
    case ZEND_AST_ARG_LIST:
12666
5.08k
    {
12667
5.08k
      zend_ast_list *list = zend_ast_get_list(ast);
12668
6.15k
      for (uint32_t i = 0; i < list->children; i++) {
12669
1.07k
        zend_eval_const_expr(&list->child[i]);
12670
1.07k
      }
12671
5.08k
      return;
12672
2.92k
    }
12673
5.08k
    case ZEND_AST_NEW:
12674
5.08k
      zend_eval_const_expr(&ast->child[0]);
12675
5.08k
      zend_eval_const_expr(&ast->child[1]);
12676
5.08k
      return;
12677
152
    case ZEND_AST_NAMED_ARG:
12678
152
      zend_eval_const_expr(&ast->child[1]);
12679
152
      return;
12680
18.6k
    case ZEND_AST_CONST_ENUM_INIT:
12681
18.6k
      zend_eval_const_expr(&ast->child[3]);
12682
18.6k
      return;
12683
4.00k
    case ZEND_AST_PROP:
12684
4.57k
    case ZEND_AST_NULLSAFE_PROP:
12685
4.57k
      zend_eval_const_expr(&ast->child[0]);
12686
4.57k
      zend_eval_const_expr(&ast->child[1]);
12687
4.57k
      return;
12688
3.54k
    case ZEND_AST_CAST:
12689
3.54k
      if (ast->attr == IS_NULL) {
12690
5
        zend_error_noreturn(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
12691
5
      }
12692
3.53k
      zend_eval_const_expr(&ast->child[0]);
12693
3.53k
      if (ast->child[0]->kind == ZEND_AST_ZVAL
12694
2.91k
       && zend_try_ct_eval_cast(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12695
2.47k
        break;
12696
2.47k
      }
12697
1.06k
      return;
12698
2.32M
    default:
12699
2.32M
      return;
12700
5.45M
  }
12701
12702
89.1k
  zend_ast_destroy(ast);
12703
89.1k
  *ast_ptr = zend_ast_create_zval(&result);
12704
89.1k
}
12705
/* }}} */