Coverage Report

Created: 2026-02-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_compile.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to version 2.00 of the Zend license,     |
8
   | that is bundled with this package in the file LICENSE, and is        |
9
   | available through the world-wide-web at the following url:           |
10
   | http://www.zend.com/license/2_00.txt.                                |
11
   | If you did not receive a copy of the Zend license and are unable to  |
12
   | obtain it through the world-wide-web, please send a note to          |
13
   | license@zend.com so we can mail you a copy immediately.              |
14
   +----------------------------------------------------------------------+
15
   | Authors: Andi Gutmans <andi@php.net>                                 |
16
   |          Zeev Suraski <zeev@php.net>                                 |
17
   |          Nikita Popov <nikic@php.net>                                |
18
   +----------------------------------------------------------------------+
19
*/
20
21
#include <zend_language_parser.h>
22
#include "zend.h"
23
#include "zend_ast.h"
24
#include "zend_attributes.h"
25
#include "zend_compile.h"
26
#include "zend_constants.h"
27
#include "zend_llist.h"
28
#include "zend_API.h"
29
#include "zend_exceptions.h"
30
#include "zend_interfaces.h"
31
#include "zend_types.h"
32
#include "zend_virtual_cwd.h"
33
#include "zend_multibyte.h"
34
#include "zend_language_scanner.h"
35
#include "zend_inheritance.h"
36
#include "zend_vm.h"
37
#include "zend_enum.h"
38
#include "zend_observer.h"
39
#include "zend_call_stack.h"
40
#include "zend_frameless_function.h"
41
#include "zend_property_hooks.h"
42
43
21.0M
#define SET_NODE(target, src) do { \
44
21.0M
    target ## _type = (src)->op_type; \
45
21.0M
    if ((src)->op_type == IS_CONST) { \
46
3.68M
      target.constant = zend_add_literal(&(src)->u.constant); \
47
17.3M
    } else { \
48
17.3M
      target = (src)->u.op; \
49
17.3M
    } \
50
21.0M
  } while (0)
51
52
14.0M
#define GET_NODE(target, src) do { \
53
14.0M
    (target)->op_type = src ## _type; \
54
14.0M
    if ((target)->op_type == IS_CONST) { \
55
764
      ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
56
14.0M
    } else { \
57
14.0M
      (target)->u.op = src; \
58
14.0M
    } \
59
14.0M
  } while (0)
60
61
28.8M
#define FC(member) (CG(file_context).member)
62
63
typedef struct _zend_loop_var {
64
  uint8_t opcode;
65
  uint8_t var_type;
66
  uint32_t   var_num;
67
  uint32_t   try_catch_offset;
68
} zend_loop_var;
69
70
4.92M
static inline uint32_t zend_alloc_cache_slots(unsigned count) {
71
4.92M
  if (count == 0) {
72
    /* Even if no cache slots are desired, the VM handler may still want to acquire
73
     * CACHE_ADDR() unconditionally. Returning zero makes sure that the address
74
     * calculation is still legal and ubsan does not complain. */
75
0
    return 0;
76
0
  }
77
78
4.92M
  zend_op_array *op_array = CG(active_op_array);
79
4.92M
  uint32_t ret = op_array->cache_size;
80
4.92M
  op_array->cache_size += count * sizeof(void*);
81
4.92M
  return ret;
82
4.92M
}
83
84
4.66M
static inline uint32_t zend_alloc_cache_slot(void) {
85
4.66M
  return zend_alloc_cache_slots(1);
86
4.66M
}
87
88
ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
89
ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename, zend_compile_position position);
90
91
#ifndef ZTS
92
ZEND_API zend_compiler_globals compiler_globals;
93
ZEND_API zend_executor_globals executor_globals;
94
#endif
95
96
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2);
97
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast);
98
static void zend_eval_const_expr(zend_ast **ast_ptr);
99
100
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
101
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
102
static void zend_compile_expr(znode *result, zend_ast *ast);
103
static void zend_compile_stmt(zend_ast *ast);
104
static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type);
105
106
#ifdef ZEND_CHECK_STACK_LIMIT
107
zend_never_inline static void zend_stack_limit_error(void)
108
0
{
109
0
  size_t max_stack_size = 0;
110
0
  if ((uintptr_t) EG(stack_base) > (uintptr_t) EG(stack_limit)) {
111
0
    max_stack_size = (size_t) ((uintptr_t) EG(stack_base) - (uintptr_t) EG(stack_limit));
112
0
  }
113
114
0
  zend_error_noreturn(E_COMPILE_ERROR,
115
0
    "Maximum call stack size of %zu bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached during compilation. Try splitting expression",
116
0
    max_stack_size);
117
0
}
118
119
static void zend_check_stack_limit(void)
120
26.2M
{
121
26.2M
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
122
0
    zend_stack_limit_error();
123
0
  }
124
26.2M
}
125
#else /* ZEND_CHECK_STACK_LIMIT */
126
static void zend_check_stack_limit(void)
127
{
128
}
129
#endif /* ZEND_CHECK_STACK_LIMIT */
130
131
static void init_op(zend_op *op)
132
29.2M
{
133
29.2M
  MAKE_NOP(op);
134
29.2M
  op->extended_value = 0;
135
29.2M
  op->lineno = CG(zend_lineno);
136
#ifdef ZEND_VERIFY_TYPE_INFERENCE
137
  op->op1_use_type = 0;
138
  op->op2_use_type = 0;
139
  op->result_use_type = 0;
140
  op->op1_def_type = 0;
141
  op->op2_def_type = 0;
142
  op->result_def_type = 0;
143
#endif
144
29.2M
}
145
146
static zend_always_inline uint32_t get_next_op_number(void)
147
5.64M
{
148
5.64M
  return CG(active_op_array)->last;
149
5.64M
}
150
151
static zend_op *get_next_op(void)
152
28.6M
{
153
28.6M
  zend_op_array *op_array = CG(active_op_array);
154
28.6M
  uint32_t next_op_num = op_array->last++;
155
28.6M
  zend_op *next_op;
156
157
28.6M
  if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) {
158
214k
    CG(context).opcodes_size *= 4;
159
214k
    op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op));
160
214k
  }
161
162
28.6M
  next_op = &(op_array->opcodes[next_op_num]);
163
164
28.6M
  init_op(next_op);
165
166
28.6M
  return next_op;
167
28.6M
}
168
169
static zend_brk_cont_element *get_next_brk_cont_element(void)
170
38.2k
{
171
38.2k
  CG(context).last_brk_cont++;
172
38.2k
  CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont);
173
38.2k
  return &CG(context).brk_cont_array[CG(context).last_brk_cont-1];
174
38.2k
}
175
176
static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */
177
117k
{
178
117k
  zend_string *filename = CG(active_op_array)->filename;
179
117k
  zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32,
180
117k
    '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
181
117k
  return zend_new_interned_string(result);
182
117k
}
183
/* }}} */
184
185
static bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
186
4.53M
{
187
4.53M
  const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
188
4.53M
  if (ns_separator != NULL) {
189
4.16M
    *result = ns_separator + 1;
190
4.16M
    *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
191
4.16M
    return 1;
192
4.16M
  }
193
194
372k
  return 0;
195
4.53M
}
196
/* }}} */
197
198
struct reserved_class_name {
199
  const char *name;
200
  size_t len;
201
};
202
static const struct reserved_class_name reserved_class_names[] = {
203
  {ZEND_STRL("bool")},
204
  {ZEND_STRL("false")},
205
  {ZEND_STRL("float")},
206
  {ZEND_STRL("int")},
207
  {ZEND_STRL("null")},
208
  {ZEND_STRL("parent")},
209
  {ZEND_STRL("self")},
210
  {ZEND_STRL("static")},
211
  {ZEND_STRL("string")},
212
  {ZEND_STRL("true")},
213
  {ZEND_STRL("void")},
214
  {ZEND_STRL("never")},
215
  {ZEND_STRL("iterable")},
216
  {ZEND_STRL("object")},
217
  {ZEND_STRL("mixed")},
218
  /* These are not usable as class names because they're proper tokens,
219
   * but they are here for class aliases. */
220
  {ZEND_STRL("array")},
221
  {ZEND_STRL("callable")},
222
  {NULL, 0}
223
};
224
225
static bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */
226
263k
{
227
263k
  const struct reserved_class_name *reserved = reserved_class_names;
228
229
263k
  const char *uqname = ZSTR_VAL(name);
230
263k
  size_t uqname_len = ZSTR_LEN(name);
231
263k
  zend_get_unqualified_name(name, &uqname, &uqname_len);
232
233
4.74M
  for (; reserved->name; ++reserved) {
234
4.47M
    if (uqname_len == reserved->len
235
177k
      && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
236
4.47M
    ) {
237
92
      return 1;
238
92
    }
239
4.47M
  }
240
241
263k
  return 0;
242
263k
}
243
/* }}} */
244
245
void zend_assert_valid_class_name(const zend_string *name, const char *type) /* {{{ */
246
262k
{
247
262k
  if (zend_is_reserved_class_name(name)) {
248
69
    zend_error_noreturn(E_COMPILE_ERROR,
249
69
      "Cannot use \"%s\" as %s as it is reserved", ZSTR_VAL(name), type);
250
69
  }
251
262k
  if (zend_string_equals_literal(name, "_")) {
252
383
    zend_error(E_DEPRECATED, "Using \"_\" as %s is deprecated since 8.4", type);
253
383
  }
254
262k
}
255
/* }}} */
256
257
typedef struct _builtin_type_info {
258
  const char* name;
259
  const size_t name_len;
260
  const uint8_t type;
261
} builtin_type_info;
262
263
static const builtin_type_info builtin_types[] = {
264
  {ZEND_STRL("null"), IS_NULL},
265
  {ZEND_STRL("true"), IS_TRUE},
266
  {ZEND_STRL("false"), IS_FALSE},
267
  {ZEND_STRL("int"), IS_LONG},
268
  {ZEND_STRL("float"), IS_DOUBLE},
269
  {ZEND_STRL("string"), IS_STRING},
270
  {ZEND_STRL("bool"), _IS_BOOL},
271
  {ZEND_STRL("void"), IS_VOID},
272
  {ZEND_STRL("never"), IS_NEVER},
273
  {ZEND_STRL("iterable"), IS_ITERABLE},
274
  {ZEND_STRL("object"), IS_OBJECT},
275
  {ZEND_STRL("mixed"), IS_MIXED},
276
  {NULL, 0, IS_UNDEF}
277
};
278
279
typedef struct {
280
  const char *name;
281
  size_t name_len;
282
  const char *correct_name;
283
} confusable_type_info;
284
285
static const confusable_type_info confusable_types[] = {
286
  {ZEND_STRL("boolean"), "bool"},
287
  {ZEND_STRL("integer"), "int"},
288
  {ZEND_STRL("double"), "float"},
289
  {ZEND_STRL("resource"), NULL},
290
  {NULL, 0, NULL},
291
};
292
293
static zend_always_inline uint8_t zend_lookup_builtin_type_by_name(const zend_string *name) /* {{{ */
294
157k
{
295
157k
  const builtin_type_info *info = &builtin_types[0];
296
297
1.85M
  for (; info->name; ++info) {
298
1.72M
    if (ZSTR_LEN(name) == info->name_len
299
101k
      && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
300
1.72M
    ) {
301
36.0k
      return info->type;
302
36.0k
    }
303
1.72M
  }
304
305
121k
  return 0;
306
157k
}
307
/* }}} */
308
309
static zend_always_inline bool zend_is_confusable_type(const zend_string *name, const char **correct_name) /* {{{ */
310
120k
{
311
120k
  const confusable_type_info *info = confusable_types;
312
313
  /* Intentionally using case-sensitive comparison here, because "integer" is likely intended
314
   * as a scalar type, while "Integer" is likely a class type. */
315
592k
  for (; info->name; ++info) {
316
475k
    if (zend_string_equals_cstr(name, info->name, info->name_len)) {
317
3.74k
      *correct_name = info->correct_name;
318
3.74k
      return 1;
319
3.74k
    }
320
475k
  }
321
322
116k
  return 0;
323
120k
}
324
/* }}} */
325
326
3.74k
static bool zend_is_not_imported(zend_string *name) {
327
  /* Assuming "name" is unqualified here. */
328
3.74k
  return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL;
329
3.74k
}
330
331
void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_array *op_array) /* {{{ */
332
670k
{
333
670k
  *prev_context = CG(context);
334
670k
  CG(context).prev = CG(context).op_array ? prev_context : NULL;
335
670k
  CG(context).op_array = op_array;
336
670k
  CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
337
670k
  CG(context).vars_size = 0;
338
670k
  CG(context).literals_size = 0;
339
670k
  CG(context).fast_call_var = -1;
340
670k
  CG(context).try_catch_offset = -1;
341
670k
  CG(context).current_brk_cont = -1;
342
670k
  CG(context).last_brk_cont = 0;
343
670k
  CG(context).has_assigned_to_http_response_header = false;
344
670k
  CG(context).brk_cont_array = NULL;
345
670k
  CG(context).labels = NULL;
346
670k
  CG(context).in_jmp_frameless_branch = false;
347
670k
  CG(context).active_property_info_name = NULL;
348
670k
  CG(context).active_property_hook_kind = (zend_property_hook_kind)-1;
349
670k
}
350
/* }}} */
351
352
void zend_oparray_context_end(const zend_oparray_context *prev_context) /* {{{ */
353
663k
{
354
663k
  if (CG(context).brk_cont_array) {
355
27.2k
    efree(CG(context).brk_cont_array);
356
27.2k
    CG(context).brk_cont_array = NULL;
357
27.2k
  }
358
663k
  if (CG(context).labels) {
359
1.73k
    zend_hash_destroy(CG(context).labels);
360
1.73k
    FREE_HASHTABLE(CG(context).labels);
361
1.73k
    CG(context).labels = NULL;
362
1.73k
  }
363
663k
  CG(context) = *prev_context;
364
663k
}
365
/* }}} */
366
367
static void zend_reset_import_tables(void) /* {{{ */
368
72.8k
{
369
72.8k
  if (FC(imports)) {
370
699
    zend_hash_destroy(FC(imports));
371
699
    efree(FC(imports));
372
699
    FC(imports) = NULL;
373
699
  }
374
375
72.8k
  if (FC(imports_function)) {
376
412
    zend_hash_destroy(FC(imports_function));
377
412
    efree(FC(imports_function));
378
412
    FC(imports_function) = NULL;
379
412
  }
380
381
72.8k
  if (FC(imports_const)) {
382
277
    zend_hash_destroy(FC(imports_const));
383
277
    efree(FC(imports_const));
384
277
    FC(imports_const) = NULL;
385
277
  }
386
387
72.8k
  zend_hash_clean(&FC(seen_symbols));
388
72.8k
}
389
/* }}} */
390
391
68.7k
static void zend_end_namespace(void) /* {{{ */ {
392
68.7k
  FC(in_namespace) = 0;
393
68.7k
  zend_reset_import_tables();
394
68.7k
  if (FC(current_namespace)) {
395
2.56k
    zend_string_release_ex(FC(current_namespace), 0);
396
2.56k
    FC(current_namespace) = NULL;
397
2.56k
  }
398
68.7k
}
399
/* }}} */
400
401
void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
402
72.3k
{
403
72.3k
  *prev_context = CG(file_context);
404
72.3k
  FC(imports) = NULL;
405
72.3k
  FC(imports_function) = NULL;
406
72.3k
  FC(imports_const) = NULL;
407
72.3k
  FC(current_namespace) = NULL;
408
72.3k
  FC(in_namespace) = 0;
409
72.3k
  FC(has_bracketed_namespaces) = 0;
410
72.3k
  FC(declarables).ticks = 0;
411
72.3k
  zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
412
72.3k
}
413
/* }}} */
414
415
void zend_file_context_end(const zend_file_context *prev_context) /* {{{ */
416
67.4k
{
417
67.4k
  zend_end_namespace();
418
67.4k
  zend_hash_destroy(&FC(seen_symbols));
419
67.4k
  CG(file_context) = *prev_context;
420
67.4k
}
421
/* }}} */
422
423
void zend_init_compiler_data_structures(void) /* {{{ */
424
196k
{
425
196k
  zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
426
196k
  zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
427
196k
  zend_stack_init(&CG(short_circuiting_opnums), sizeof(uint32_t));
428
196k
  CG(active_class_entry) = NULL;
429
196k
  CG(in_compilation) = 0;
430
196k
  CG(skip_shebang) = 0;
431
432
196k
  CG(encoding_declared) = 0;
433
196k
  CG(memoized_exprs) = NULL;
434
196k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
435
196k
}
436
/* }}} */
437
438
652k
static void zend_register_seen_symbol(zend_string *name, uint32_t kind) {
439
652k
  zval *zv = zend_hash_find(&FC(seen_symbols), name);
440
652k
  if (zv) {
441
585k
    Z_LVAL_P(zv) |= kind;
442
585k
  } else {
443
66.8k
    zval tmp;
444
66.8k
    ZVAL_LONG(&tmp, kind);
445
66.8k
    zend_hash_add_new(&FC(seen_symbols), name, &tmp);
446
66.8k
  }
447
652k
}
448
449
2.23k
static bool zend_have_seen_symbol(zend_string *name, uint32_t kind) {
450
2.23k
  const zval *zv = zend_hash_find(&FC(seen_symbols), name);
451
2.23k
  return zv && (Z_LVAL_P(zv) & kind) != 0;
452
2.23k
}
453
454
void init_compiler(void) /* {{{ */
455
191k
{
456
191k
  CG(arena) = zend_arena_create(64 * 1024);
457
191k
  CG(active_op_array) = NULL;
458
191k
  memset(&CG(context), 0, sizeof(CG(context)));
459
191k
  zend_init_compiler_data_structures();
460
191k
  zend_init_rsrc_list();
461
191k
  zend_stream_init();
462
191k
  CG(unclean_shutdown) = 0;
463
464
191k
  CG(delayed_variance_obligations) = NULL;
465
191k
  CG(delayed_autoloads) = NULL;
466
191k
  CG(unlinked_uses) = NULL;
467
191k
  CG(current_linking_class) = NULL;
468
191k
}
469
/* }}} */
470
471
void shutdown_compiler(void) /* {{{ */
472
196k
{
473
  /* Reset filename before destroying the arena, as file cache may use arena allocated strings. */
474
196k
  zend_restore_compiled_filename(NULL);
475
476
196k
  zend_stack_destroy(&CG(loop_var_stack));
477
196k
  zend_stack_destroy(&CG(delayed_oplines_stack));
478
196k
  zend_stack_destroy(&CG(short_circuiting_opnums));
479
480
196k
  if (CG(delayed_variance_obligations)) {
481
247
    zend_hash_destroy(CG(delayed_variance_obligations));
482
247
    FREE_HASHTABLE(CG(delayed_variance_obligations));
483
247
    CG(delayed_variance_obligations) = NULL;
484
247
  }
485
196k
  if (CG(delayed_autoloads)) {
486
255
    zend_hash_destroy(CG(delayed_autoloads));
487
255
    FREE_HASHTABLE(CG(delayed_autoloads));
488
255
    CG(delayed_autoloads) = NULL;
489
255
  }
490
196k
  if (CG(unlinked_uses)) {
491
214
    zend_hash_destroy(CG(unlinked_uses));
492
214
    FREE_HASHTABLE(CG(unlinked_uses));
493
214
    CG(unlinked_uses) = NULL;
494
214
  }
495
196k
  CG(current_linking_class) = NULL;
496
196k
}
497
/* }}} */
498
499
ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
500
119k
{
501
119k
  CG(compiled_filename) = zend_string_copy(new_compiled_filename);
502
119k
  return new_compiled_filename;
503
119k
}
504
/* }}} */
505
506
ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
507
442k
{
508
442k
  if (CG(compiled_filename)) {
509
119k
    zend_string_release(CG(compiled_filename));
510
119k
    CG(compiled_filename) = NULL;
511
119k
  }
512
442k
  CG(compiled_filename) = original_compiled_filename;
513
442k
}
514
/* }}} */
515
516
ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
517
1.22M
{
518
1.22M
  return CG(compiled_filename);
519
1.22M
}
520
/* }}} */
521
522
ZEND_API uint32_t zend_get_compiled_lineno(void) /* {{{ */
523
287k
{
524
287k
  return CG(zend_lineno);
525
287k
}
526
/* }}} */
527
528
ZEND_API bool zend_is_compiling(void) /* {{{ */
529
2.61M
{
530
2.61M
  return CG(in_compilation);
531
2.61M
}
532
/* }}} */
533
534
static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */
535
14.2M
{
536
14.2M
  return (uint32_t)CG(active_op_array)->T++;
537
14.2M
}
538
/* }}} */
539
540
1.62M
static uint32_t lookup_cv(zend_string *name) /* {{{ */{
541
1.62M
  zend_op_array *op_array = CG(active_op_array);
542
1.62M
  int i = 0;
543
1.62M
  zend_ulong hash_value = zend_string_hash_val(name);
544
545
6.31M
  while (i < op_array->last_var) {
546
5.29M
    if (ZSTR_H(op_array->vars[i]) == hash_value
547
606k
     && zend_string_equals(op_array->vars[i], name)) {
548
605k
      return EX_NUM_TO_VAR(i);
549
605k
    }
550
4.68M
    i++;
551
4.68M
  }
552
1.01M
  i = op_array->last_var;
553
1.01M
  op_array->last_var++;
554
1.01M
  if (op_array->last_var > CG(context).vars_size) {
555
576k
    CG(context).vars_size += 16; /* FIXME */
556
576k
    op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
557
576k
  }
558
559
1.01M
  op_array->vars[i] = zend_string_copy(name);
560
1.01M
  return EX_NUM_TO_VAR(i);
561
1.62M
}
562
/* }}} */
563
564
zend_string *zval_make_interned_string(zval *zv)
565
16.9M
{
566
16.9M
  ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
567
16.9M
  Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
568
16.9M
  if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
569
3.37M
    Z_TYPE_FLAGS_P(zv) = 0;
570
3.37M
  }
571
16.9M
  return Z_STR_P(zv);
572
16.9M
}
573
574
/* Common part of zend_add_literal and zend_append_individual_literal */
575
static inline void zend_insert_literal(const zend_op_array *op_array, zval *zv, int literal_position) /* {{{ */
576
16.9M
{
577
16.9M
  zval *lit = CT_CONSTANT_EX(op_array, literal_position);
578
16.9M
  if (Z_TYPE_P(zv) == IS_STRING) {
579
15.5M
    zval_make_interned_string(zv);
580
15.5M
  }
581
16.9M
  ZVAL_COPY_VALUE(lit, zv);
582
16.9M
  Z_EXTRA_P(lit) = 0;
583
16.9M
}
584
/* }}} */
585
586
/* Is used while compiling a function, using the context to keep track
587
   of an approximate size to avoid to relocate to often.
588
   Literals are truncated to actual size in the second compiler pass (pass_two()). */
589
static int zend_add_literal(zval *zv) /* {{{ */
590
16.9M
{
591
16.9M
  zend_op_array *op_array = CG(active_op_array);
592
16.9M
  uint32_t i = op_array->last_literal;
593
16.9M
  op_array->last_literal++;
594
16.9M
  if (i >= CG(context).literals_size) {
595
2.83M
    while (i >= CG(context).literals_size) {
596
1.41M
      CG(context).literals_size += 16; /* FIXME */
597
1.41M
    }
598
1.41M
    op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
599
1.41M
  }
600
16.9M
  zend_insert_literal(op_array, zv, i);
601
16.9M
  return i;
602
16.9M
}
603
/* }}} */
604
605
static inline int zend_add_literal_string(zend_string **str) /* {{{ */
606
13.1M
{
607
13.1M
  int ret;
608
13.1M
  zval zv;
609
13.1M
  ZVAL_STR(&zv, *str);
610
13.1M
  ret = zend_add_literal(&zv);
611
13.1M
  *str = Z_STR(zv);
612
13.1M
  return ret;
613
13.1M
}
614
/* }}} */
615
616
static int zend_add_func_name_literal(zend_string *name) /* {{{ */
617
152k
{
618
  /* Original name */
619
152k
  int ret = zend_add_literal_string(&name);
620
621
  /* Lowercased name */
622
152k
  zend_string *lc_name = zend_string_tolower(name);
623
152k
  zend_add_literal_string(&lc_name);
624
625
152k
  return ret;
626
152k
}
627
/* }}} */
628
629
static int zend_add_ns_func_name_literal(zend_string *name) /* {{{ */
630
1.13M
{
631
1.13M
  const char *unqualified_name;
632
1.13M
  size_t unqualified_name_len;
633
634
  /* Original name */
635
1.13M
  int ret = zend_add_literal_string(&name);
636
637
  /* Lowercased name */
638
1.13M
  zend_string *lc_name = zend_string_tolower(name);
639
1.13M
  zend_add_literal_string(&lc_name);
640
641
  /* Lowercased unqualified name */
642
1.13M
  if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
643
1.13M
    lc_name = zend_string_alloc(unqualified_name_len, 0);
644
1.13M
    zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
645
1.13M
    zend_add_literal_string(&lc_name);
646
1.13M
  }
647
648
1.13M
  return ret;
649
1.13M
}
650
/* }}} */
651
652
static int zend_add_class_name_literal(zend_string *name) /* {{{ */
653
140k
{
654
  /* Original name */
655
140k
  int ret = zend_add_literal_string(&name);
656
657
  /* Lowercased name */
658
140k
  zend_string *lc_name = zend_string_tolower(name);
659
140k
  zend_add_literal_string(&lc_name);
660
661
140k
  return ret;
662
140k
}
663
/* }}} */
664
665
static int zend_add_const_name_literal(zend_string *name, bool unqualified) /* {{{ */
666
3.00M
{
667
3.00M
  zend_string *tmp_name;
668
669
3.00M
  int ret = zend_add_literal_string(&name);
670
671
3.00M
  size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
672
3.00M
  const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
673
3.00M
  if (after_ns) {
674
2.87M
    after_ns += 1;
675
2.87M
    ns_len = after_ns - ZSTR_VAL(name) - 1;
676
2.87M
    after_ns_len = ZSTR_LEN(name) - ns_len - 1;
677
678
    /* lowercased namespace name & original constant name */
679
2.87M
    tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
680
2.87M
    zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
681
2.87M
    zend_add_literal_string(&tmp_name);
682
683
2.87M
    if (!unqualified) {
684
1.91k
      return ret;
685
1.91k
    }
686
2.87M
  } else {
687
130k
    after_ns = ZSTR_VAL(name);
688
130k
  }
689
690
  /* original unqualified constant name */
691
2.99M
  tmp_name = zend_string_init(after_ns, after_ns_len, 0);
692
2.99M
  zend_add_literal_string(&tmp_name);
693
694
2.99M
  return ret;
695
3.00M
}
696
/* }}} */
697
698
59.7k
#define LITERAL_STR(op, str) do { \
699
59.7k
    zval _c; \
700
59.7k
    ZVAL_STR(&_c, str); \
701
59.7k
    op.constant = zend_add_literal(&_c); \
702
59.7k
  } while (0)
703
704
void zend_stop_lexing(void)
705
55
{
706
55
  if (LANG_SCNG(on_event)) {
707
0
    LANG_SCNG(on_event)(ON_STOP, END, 0, NULL, 0, LANG_SCNG(on_event_context));
708
0
  }
709
710
55
  LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
711
55
}
712
713
static inline void zend_begin_loop(
714
    uint8_t free_opcode, const znode *loop_var, bool is_switch) /* {{{ */
715
38.2k
{
716
38.2k
  zend_brk_cont_element *brk_cont_element;
717
38.2k
  int parent = CG(context).current_brk_cont;
718
38.2k
  zend_loop_var info = {0};
719
720
38.2k
  CG(context).current_brk_cont = CG(context).last_brk_cont;
721
38.2k
  brk_cont_element = get_next_brk_cont_element();
722
38.2k
  brk_cont_element->parent = parent;
723
38.2k
  brk_cont_element->is_switch = is_switch;
724
725
38.2k
  if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
726
18.7k
    uint32_t start = get_next_op_number();
727
728
18.7k
    info.opcode = free_opcode;
729
18.7k
    info.var_type = loop_var->op_type;
730
18.7k
    info.var_num = loop_var->u.op.var;
731
18.7k
    brk_cont_element->start = start;
732
19.4k
  } else {
733
19.4k
    info.opcode = ZEND_NOP;
734
    /* The start field is used to free temporary variables in case of exceptions.
735
     * We won't try to free something of we don't have loop variable.  */
736
19.4k
    brk_cont_element->start = -1;
737
19.4k
  }
738
739
38.2k
  zend_stack_push(&CG(loop_var_stack), &info);
740
38.2k
}
741
/* }}} */
742
743
static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
744
38.0k
{
745
38.0k
  uint32_t end = get_next_op_number();
746
38.0k
  zend_brk_cont_element *brk_cont_element
747
38.0k
    = &CG(context).brk_cont_array[CG(context).current_brk_cont];
748
38.0k
  brk_cont_element->cont = cont_addr;
749
38.0k
  brk_cont_element->brk = end;
750
38.0k
  CG(context).current_brk_cont = brk_cont_element->parent;
751
752
38.0k
  zend_stack_del_top(&CG(loop_var_stack));
753
38.0k
}
754
/* }}} */
755
756
bool zend_op_may_elide_result(uint8_t opcode)
757
1.60M
{
758
1.60M
  switch (opcode) {
759
118k
    case ZEND_ASSIGN:
760
127k
    case ZEND_ASSIGN_DIM:
761
136k
    case ZEND_ASSIGN_OBJ:
762
138k
    case ZEND_ASSIGN_STATIC_PROP:
763
165k
    case ZEND_ASSIGN_OP:
764
166k
    case ZEND_ASSIGN_DIM_OP:
765
167k
    case ZEND_ASSIGN_OBJ_OP:
766
167k
    case ZEND_ASSIGN_STATIC_PROP_OP:
767
167k
    case ZEND_PRE_INC_STATIC_PROP:
768
167k
    case ZEND_PRE_DEC_STATIC_PROP:
769
167k
    case ZEND_PRE_INC_OBJ:
770
168k
    case ZEND_PRE_DEC_OBJ:
771
168k
    case ZEND_PRE_INC:
772
168k
    case ZEND_PRE_DEC:
773
266k
    case ZEND_DO_FCALL:
774
274k
    case ZEND_DO_ICALL:
775
280k
    case ZEND_DO_UCALL:
776
707k
    case ZEND_DO_FCALL_BY_NAME:
777
709k
    case ZEND_YIELD:
778
709k
    case ZEND_YIELD_FROM:
779
712k
    case ZEND_INCLUDE_OR_EVAL:
780
712k
      return true;
781
897k
    default:
782
897k
      return false;
783
1.60M
  }
784
1.60M
}
785
786
static void zend_do_free(znode *op1) /* {{{ */
787
1.73M
{
788
1.73M
  if (op1->op_type == IS_TMP_VAR) {
789
1.61M
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
790
791
1.67M
    while (opline->opcode == ZEND_END_SILENCE ||
792
1.66M
           opline->opcode == ZEND_OP_DATA) {
793
66.6k
      opline--;
794
66.6k
    }
795
796
1.61M
    if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
797
1.59M
      switch (opline->opcode) {
798
1.34k
        case ZEND_BOOL:
799
5.21k
        case ZEND_BOOL_NOT:
800
          /* boolean results don't have to be freed */
801
5.21k
          return;
802
83
        case ZEND_POST_INC_STATIC_PROP:
803
110
        case ZEND_POST_DEC_STATIC_PROP:
804
387
        case ZEND_POST_INC_OBJ:
805
506
        case ZEND_POST_DEC_OBJ:
806
4.71k
        case ZEND_POST_INC:
807
5.82k
        case ZEND_POST_DEC:
808
          /* convert $i++ to ++$i */
809
5.82k
          opline->opcode -= 2;
810
5.82k
          SET_UNUSED(opline->result);
811
5.82k
          return;
812
1.58M
        default:
813
1.58M
          if (zend_op_may_elide_result(opline->opcode)) {
814
710k
            SET_UNUSED(opline->result);
815
710k
            return;
816
710k
          }
817
876k
          break;
818
1.59M
      }
819
1.59M
    }
820
821
889k
    zend_emit_op(NULL, ZEND_FREE, op1, NULL);
822
889k
  } else if (op1->op_type == IS_VAR) {
823
65.8k
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
824
65.8k
    while (opline->opcode == ZEND_END_SILENCE ||
825
65.8k
        opline->opcode == ZEND_EXT_FCALL_END ||
826
65.8k
        opline->opcode == ZEND_OP_DATA) {
827
0
      opline--;
828
0
    }
829
65.8k
    if (opline->result_type == IS_VAR
830
64.5k
      && opline->result.var == op1->u.op.var) {
831
64.5k
      if (opline->opcode == ZEND_FETCH_THIS) {
832
0
        opline->opcode = ZEND_NOP;
833
0
      }
834
64.5k
      if (!ZEND_OP_IS_FRAMELESS_ICALL(opline->opcode)) {
835
64.5k
        SET_UNUSED(opline->result);
836
64.5k
      } else {
837
        /* Frameless calls usually use the return value, so always emit a free. This should be
838
         * faster than checking RETURN_VALUE_USED inside the handler. */
839
0
        zend_emit_op(NULL, ZEND_FREE, op1, NULL);
840
0
      }
841
64.5k
    } else {
842
4.16k
      while (opline >= CG(active_op_array)->opcodes) {
843
4.16k
        if ((opline->opcode == ZEND_FETCH_LIST_R ||
844
4.04k
             opline->opcode == ZEND_FETCH_LIST_W ||
845
2.76k
             opline->opcode == ZEND_EXT_STMT) &&
846
1.40k
            opline->op1_type == IS_VAR &&
847
1.40k
            opline->op1.var == op1->u.op.var) {
848
1.36k
          zend_emit_op(NULL, ZEND_FREE, op1, NULL);
849
1.36k
          return;
850
1.36k
        }
851
2.80k
        if (opline->result_type == IS_VAR
852
1.37k
          && opline->result.var == op1->u.op.var) {
853
0
          if (opline->opcode == ZEND_NEW) {
854
0
            zend_emit_op(NULL, ZEND_FREE, op1, NULL);
855
0
          }
856
0
          break;
857
0
        }
858
2.80k
        opline--;
859
2.80k
      }
860
1.36k
    }
861
65.8k
  } else if (op1->op_type == IS_CONST) {
862
    /* Destroy value without using GC: When opcache moves arrays into SHM it will
863
     * free the zend_array structure, so references to it from outside the op array
864
     * become invalid. GC would cause such a reference in the root buffer. */
865
57.2k
    zval_ptr_dtor_nogc(&op1->u.constant);
866
57.2k
  }
867
1.73M
}
868
/* }}} */
869
870
871
static const char *zend_modifier_token_to_string(uint32_t token)
872
72
{
873
72
  switch (token) {
874
5
    case T_PUBLIC:
875
5
      return "public";
876
1
    case T_PROTECTED:
877
1
      return "protected";
878
4
    case T_PRIVATE:
879
4
      return "private";
880
16
    case T_STATIC:
881
16
      return "static";
882
14
    case T_FINAL:
883
14
      return "final";
884
22
    case T_READONLY:
885
22
      return "readonly";
886
8
    case T_ABSTRACT:
887
8
      return "abstract";
888
0
    case T_PUBLIC_SET:
889
0
      return "public(set)";
890
0
    case T_PROTECTED_SET:
891
0
      return "protected(set)";
892
2
    case T_PRIVATE_SET:
893
2
      return "private(set)";
894
72
    EMPTY_SWITCH_DEFAULT_CASE()
895
72
  }
896
72
}
897
898
uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t token)
899
51.2k
{
900
51.2k
  switch (token) {
901
33.8k
    case T_PUBLIC:
902
33.8k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
903
33.8k
        return ZEND_ACC_PUBLIC;
904
33.8k
      }
905
5
      break;
906
2.01k
    case T_PROTECTED:
907
2.01k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
908
2.01k
        return ZEND_ACC_PROTECTED;
909
2.01k
      }
910
1
      break;
911
4.20k
    case T_PRIVATE:
912
4.20k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
913
4.19k
        return ZEND_ACC_PRIVATE;
914
4.19k
      }
915
4
      break;
916
837
    case T_READONLY:
917
837
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
918
823
        return ZEND_ACC_READONLY;
919
823
      }
920
14
      break;
921
868
    case T_ABSTRACT:
922
868
      if (target == ZEND_MODIFIER_TARGET_METHOD || target == ZEND_MODIFIER_TARGET_PROPERTY) {
923
867
        return ZEND_ACC_ABSTRACT;
924
867
      }
925
1
      break;
926
1.28k
    case T_FINAL:
927
1.28k
      return ZEND_ACC_FINAL;
928
7.32k
    case T_STATIC:
929
7.32k
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_METHOD) {
930
7.31k
        return ZEND_ACC_STATIC;
931
7.31k
      }
932
10
      break;
933
321
    case T_PUBLIC_SET:
934
321
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
935
321
        return ZEND_ACC_PUBLIC_SET;
936
321
      }
937
0
      break;
938
189
    case T_PROTECTED_SET:
939
189
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
940
189
        return ZEND_ACC_PROTECTED_SET;
941
189
      }
942
0
      break;
943
367
    case T_PRIVATE_SET:
944
367
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
945
365
        return ZEND_ACC_PRIVATE_SET;
946
365
      }
947
2
      break;
948
51.2k
  }
949
950
37
  char *member;
951
37
  if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
952
0
    member = "property";
953
37
  } else if (target == ZEND_MODIFIER_TARGET_METHOD) {
954
9
    member = "method";
955
28
  } else if (target == ZEND_MODIFIER_TARGET_CONSTANT) {
956
7
    member = "class constant";
957
21
  } else if (target == ZEND_MODIFIER_TARGET_CPP) {
958
6
    member = "parameter";
959
15
  } else if (target == ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
960
15
    member = "property hook";
961
15
  } else {
962
0
    ZEND_UNREACHABLE();
963
0
  }
964
965
37
  zend_throw_exception_ex(zend_ce_compile_error, 0,
966
37
    "Cannot use the %s modifier on a %s", zend_modifier_token_to_string(token), member);
967
37
  return 0;
968
37
}
969
970
uint32_t zend_modifier_list_to_flags(zend_modifier_target target, zend_ast *modifiers)
971
43.4k
{
972
43.4k
  uint32_t flags = 0;
973
43.4k
  const zend_ast_list *modifier_list = zend_ast_get_list(modifiers);
974
975
93.7k
  for (uint32_t i = 0; i < modifier_list->children; i++) {
976
50.3k
    uint32_t token = (uint32_t) Z_LVAL_P(zend_ast_get_zval(modifier_list->child[i]));
977
50.3k
    uint32_t new_flag = zend_modifier_token_to_flag(target, token);
978
50.3k
    if (!new_flag) {
979
31
      return 0;
980
31
    }
981
    /* Don't error immediately for duplicate flags, we want to prioritize the errors from zend_add_member_modifier(). */
982
50.3k
    bool duplicate_flag = (flags & new_flag);
983
50.3k
    flags = zend_add_member_modifier(flags, new_flag, target);
984
50.3k
    if (!flags) {
985
48
      return 0;
986
48
    }
987
50.3k
    if (duplicate_flag) {
988
35
      zend_throw_exception_ex(zend_ce_compile_error, 0,
989
35
        "Multiple %s modifiers are not allowed", zend_modifier_token_to_string(token));
990
35
      return 0;
991
35
    }
992
50.3k
  }
993
994
43.3k
  return flags;
995
43.4k
}
996
997
uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
998
149
{
999
149
  uint32_t new_flags = flags | new_flag;
1000
149
  if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
1001
1
    zend_throw_exception(zend_ce_compile_error,
1002
1
      "Multiple abstract modifiers are not allowed", 0);
1003
1
    return 0;
1004
1
  }
1005
148
  if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
1006
5
    zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
1007
5
    return 0;
1008
5
  }
1009
143
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
1010
5
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
1011
5
    return 0;
1012
5
  }
1013
138
  if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
1014
6
    zend_throw_exception(zend_ce_compile_error,
1015
6
      "Cannot use the final modifier on an abstract class", 0);
1016
6
    return 0;
1017
6
  }
1018
132
  return new_flags;
1019
138
}
1020
/* }}} */
1021
1022
uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag)
1023
52
{
1024
52
  uint32_t new_flags = flags | new_flag;
1025
52
  if (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
1026
5
    zend_throw_exception(zend_ce_compile_error,
1027
5
      "Cannot use the abstract modifier on an anonymous class", 0);
1028
5
    return 0;
1029
5
  }
1030
47
  if (new_flag & ZEND_ACC_FINAL) {
1031
5
    zend_throw_exception(zend_ce_compile_error, "Cannot use the final modifier on an anonymous class", 0);
1032
5
    return 0;
1033
5
  }
1034
42
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
1035
5
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
1036
5
    return 0;
1037
5
  }
1038
37
  return new_flags;
1039
42
}
1040
1041
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target) /* {{{ */
1042
50.3k
{
1043
50.3k
  uint32_t new_flags = flags | new_flag;
1044
50.3k
  if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
1045
28
    zend_throw_exception(zend_ce_compile_error,
1046
28
      "Multiple access type modifiers are not allowed", 0);
1047
28
    return 0;
1048
28
  }
1049
50.3k
  if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
1050
10
    if (target == ZEND_MODIFIER_TARGET_METHOD) {
1051
5
      zend_throw_exception(zend_ce_compile_error,
1052
5
        "Cannot use the final modifier on an abstract method", 0);
1053
5
      return 0;
1054
5
    }
1055
5
    if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
1056
5
      zend_throw_exception(zend_ce_compile_error,
1057
5
        "Cannot use the final modifier on an abstract property", 0);
1058
5
      return 0;
1059
5
    }
1060
5
  }
1061
50.3k
  if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
1062
24.8k
    if ((flags & ZEND_ACC_PPP_SET_MASK) && (new_flag & ZEND_ACC_PPP_SET_MASK)) {
1063
10
      zend_throw_exception(zend_ce_compile_error,
1064
10
        "Multiple access type modifiers are not allowed", 0);
1065
10
      return 0;
1066
10
    }
1067
24.8k
  }
1068
50.3k
  return new_flags;
1069
50.3k
}
1070
/* }}} */
1071
1072
15.9k
ZEND_API zend_string *zend_create_member_string(const zend_string *class_name, const zend_string *member_name) {
1073
15.9k
  return zend_string_concat3(
1074
15.9k
    ZSTR_VAL(class_name), ZSTR_LEN(class_name),
1075
15.9k
    "::", sizeof("::") - 1,
1076
15.9k
    ZSTR_VAL(member_name), ZSTR_LEN(member_name));
1077
15.9k
}
1078
1079
4.94M
static zend_string *zend_concat_names(const char *name1, size_t name1_len, const char *name2, size_t name2_len) {
1080
4.94M
  return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len);
1081
4.94M
}
1082
1083
5.52M
static zend_string *zend_prefix_with_ns(zend_string *name) {
1084
5.52M
  if (FC(current_namespace)) {
1085
4.94M
    const zend_string *ns = FC(current_namespace);
1086
4.94M
    return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
1087
4.94M
  } else {
1088
583k
    return zend_string_copy(name);
1089
583k
  }
1090
5.52M
}
1091
1092
static zend_string *zend_resolve_non_class_name(
1093
  zend_string *name, uint32_t type, bool *is_fully_qualified,
1094
  bool case_sensitive, const HashTable *current_import_sub
1095
4.50M
) {
1096
4.50M
  const char *compound;
1097
4.50M
  *is_fully_qualified = false;
1098
1099
4.50M
  if (ZSTR_VAL(name)[0] == '\\') {
1100
    /* Remove \ prefix (only relevant if this is a string rather than a label) */
1101
294
    *is_fully_qualified = true;
1102
294
    return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1103
294
  }
1104
1105
4.50M
  if (type == ZEND_NAME_FQ) {
1106
65.3k
    *is_fully_qualified = true;
1107
65.3k
    return zend_string_copy(name);
1108
65.3k
  }
1109
1110
4.43M
  if (type == ZEND_NAME_RELATIVE) {
1111
860
    *is_fully_qualified = true;
1112
860
    return zend_prefix_with_ns(name);
1113
860
  }
1114
1115
4.43M
  if (current_import_sub) {
1116
    /* If an unqualified name is a function/const alias, replace it. */
1117
1.54k
    zend_string *import_name;
1118
1.54k
    if (case_sensitive) {
1119
827
      import_name = zend_hash_find_ptr(current_import_sub, name);
1120
827
    } else {
1121
721
      import_name = zend_hash_find_ptr_lc(current_import_sub, name);
1122
721
    }
1123
1124
1.54k
    if (import_name) {
1125
502
      *is_fully_qualified = true;
1126
502
      return zend_string_copy(import_name);
1127
502
    }
1128
1.54k
  }
1129
1130
4.43M
  compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1131
4.43M
  if (compound) {
1132
5.20k
    *is_fully_qualified = true;
1133
5.20k
  }
1134
1135
4.43M
  if (compound && FC(imports)) {
1136
    /* If the first part of a qualified name is an alias, substitute it. */
1137
2.24k
    size_t len = compound - ZSTR_VAL(name);
1138
2.24k
    const zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1139
1140
2.24k
    if (import_name) {
1141
364
      return zend_concat_names(
1142
364
        ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1143
364
    }
1144
2.24k
  }
1145
1146
4.43M
  return zend_prefix_with_ns(name);
1147
4.43M
}
1148
/* }}} */
1149
1150
static zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1151
1.36M
{
1152
1.36M
  return zend_resolve_non_class_name(
1153
1.36M
    name, type, is_fully_qualified, false, FC(imports_function));
1154
1.36M
}
1155
1156
static zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1157
3.13M
{
1158
3.13M
  return zend_resolve_non_class_name(
1159
3.13M
    name, type, is_fully_qualified, true, FC(imports_const));
1160
3.13M
}
1161
1162
static zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
1163
946k
{
1164
946k
  const char *compound;
1165
1166
946k
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1167
3.20k
    if (type == ZEND_NAME_FQ) {
1168
7
      zend_error_noreturn(E_COMPILE_ERROR,
1169
7
        "'\\%s' is an invalid class name", ZSTR_VAL(name));
1170
7
    }
1171
3.19k
    if (type == ZEND_NAME_RELATIVE) {
1172
0
      zend_error_noreturn(E_COMPILE_ERROR,
1173
0
        "'namespace\\%s' is an invalid class name", ZSTR_VAL(name));
1174
0
    }
1175
3.19k
    ZEND_ASSERT(type == ZEND_NAME_NOT_FQ);
1176
3.19k
    return zend_string_copy(name);
1177
3.19k
  }
1178
1179
943k
  if (type == ZEND_NAME_RELATIVE) {
1180
430
    return zend_prefix_with_ns(name);
1181
430
  }
1182
1183
942k
  if (type == ZEND_NAME_FQ) {
1184
12.9k
    if (ZSTR_VAL(name)[0] == '\\') {
1185
      /* Remove \ prefix (only relevant if this is a string rather than a label) */
1186
55
      name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1187
55
      if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1188
1
        zend_error_noreturn(E_COMPILE_ERROR,
1189
1
          "'\\%s' is an invalid class name", ZSTR_VAL(name));
1190
1
      }
1191
54
      return name;
1192
55
    }
1193
1194
12.8k
    return zend_string_copy(name);
1195
12.9k
  }
1196
1197
929k
  if (FC(imports)) {
1198
3.04k
    compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1199
3.04k
    if (compound) {
1200
      /* If the first part of a qualified name is an alias, substitute it. */
1201
716
      size_t len = compound - ZSTR_VAL(name);
1202
716
      const zend_string *import_name =
1203
716
        zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1204
1205
716
      if (import_name) {
1206
282
        return zend_concat_names(
1207
282
          ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1208
282
      }
1209
2.32k
    } else {
1210
      /* If an unqualified name is an alias, replace it. */
1211
2.32k
      zend_string *import_name
1212
2.32k
        = zend_hash_find_ptr_lc(FC(imports), name);
1213
1214
2.32k
      if (import_name) {
1215
985
        return zend_string_copy(import_name);
1216
985
      }
1217
2.32k
    }
1218
3.04k
  }
1219
1220
  /* If not fully qualified and not an alias, prepend the current namespace */
1221
928k
  return zend_prefix_with_ns(name);
1222
929k
}
1223
/* }}} */
1224
1225
static zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
1226
880k
{
1227
880k
  const zval *class_name = zend_ast_get_zval(ast);
1228
880k
  if (Z_TYPE_P(class_name) != IS_STRING) {
1229
26
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1230
26
  }
1231
880k
  return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
1232
880k
}
1233
/* }}} */
1234
1235
static void label_ptr_dtor(zval *zv) /* {{{ */
1236
2.34k
{
1237
2.34k
  efree_size(Z_PTR_P(zv), sizeof(zend_label));
1238
2.34k
}
1239
/* }}} */
1240
1241
2.00k
static void str_dtor(zval *zv)  /* {{{ */ {
1242
2.00k
  zend_string_release_ex(Z_STR_P(zv), 0);
1243
2.00k
}
1244
/* }}} */
1245
1246
static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
1247
29.0k
{
1248
29.0k
  zend_op_array *op_array = CG(active_op_array);
1249
29.0k
  uint32_t try_catch_offset = op_array->last_try_catch++;
1250
29.0k
  zend_try_catch_element *elem;
1251
1252
29.0k
  op_array->try_catch_array = safe_erealloc(
1253
29.0k
    op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
1254
1255
29.0k
  elem = &op_array->try_catch_array[try_catch_offset];
1256
29.0k
  elem->try_op = try_op;
1257
29.0k
  elem->catch_op = 0;
1258
29.0k
  elem->finally_op = 0;
1259
29.0k
  elem->finally_end = 0;
1260
1261
29.0k
  return try_catch_offset;
1262
29.0k
}
1263
/* }}} */
1264
1265
ZEND_API void function_add_ref(zend_function *function) /* {{{ */
1266
1.60k
{
1267
1.60k
  if (function->type == ZEND_USER_FUNCTION) {
1268
1.60k
    zend_op_array *op_array = &function->op_array;
1269
1.60k
    if (op_array->refcount) {
1270
209
      (*op_array->refcount)++;
1271
209
    }
1272
1273
1.60k
    ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
1274
1.60k
    ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
1275
1.60k
  }
1276
1277
1.60k
  if (function->common.function_name) {
1278
1.60k
    zend_string_addref(function->common.function_name);
1279
1.60k
  }
1280
1.60k
}
1281
/* }}} */
1282
1283
static zend_never_inline ZEND_COLD ZEND_NORETURN void do_bind_function_error(const zend_string *lcname, const zend_op_array *op_array, bool compile_time) /* {{{ */
1284
105
{
1285
105
  const zval *zv = zend_hash_find_known_hash(compile_time ? CG(function_table) : EG(function_table), lcname);
1286
105
  int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
1287
105
  const zend_function *old_function;
1288
1289
105
  ZEND_ASSERT(zv != NULL);
1290
105
  old_function = Z_PTR_P(zv);
1291
105
  if (old_function->type == ZEND_USER_FUNCTION
1292
100
    && old_function->op_array.last > 0) {
1293
100
    zend_error_noreturn(error_level, "Cannot redeclare function %s() (previously declared in %s:%d)",
1294
100
          op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name),
1295
100
          ZSTR_VAL(old_function->op_array.filename),
1296
100
          old_function->op_array.line_start);
1297
100
  } else {
1298
5
    zend_error_noreturn(error_level, "Cannot redeclare function %s()",
1299
5
      op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name));
1300
5
  }
1301
105
}
1302
1303
ZEND_API zend_result do_bind_function(zend_function *func, const zval *lcname) /* {{{ */
1304
96
{
1305
96
  zend_function *added_func = zend_hash_add_ptr(EG(function_table), Z_STR_P(lcname), func);
1306
96
  if (UNEXPECTED(!added_func)) {
1307
5
    do_bind_function_error(Z_STR_P(lcname), &func->op_array, false);
1308
0
    return FAILURE;
1309
5
  }
1310
1311
91
  if (func->op_array.refcount) {
1312
14
    ++*func->op_array.refcount;
1313
14
  }
1314
91
  if (func->common.function_name) {
1315
91
    zend_string_addref(func->common.function_name);
1316
91
  }
1317
91
  zend_observer_function_declared_notify(&func->op_array, Z_STR_P(lcname));
1318
91
  return SUCCESS;
1319
96
}
1320
/* }}} */
1321
1322
ZEND_API zend_class_entry *zend_bind_class_in_slot(
1323
    zval *class_table_slot, const zval *lcname, zend_string *lc_parent_name)
1324
6.23k
{
1325
6.23k
  zend_class_entry *ce = Z_PTR_P(class_table_slot);
1326
6.23k
  bool is_preloaded =
1327
6.23k
    (ce->ce_flags & ZEND_ACC_PRELOADED) && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD);
1328
6.23k
  bool success;
1329
6.23k
  if (EXPECTED(!is_preloaded)) {
1330
6.23k
    success = zend_hash_set_bucket_key(EG(class_table), (Bucket*) class_table_slot, Z_STR_P(lcname)) != NULL;
1331
6.23k
  } else {
1332
    /* If preloading is used, don't replace the existing bucket, add a new one. */
1333
0
    success = zend_hash_add_ptr(EG(class_table), Z_STR_P(lcname), ce) != NULL;
1334
0
  }
1335
6.23k
  if (UNEXPECTED(!success)) {
1336
113
    zend_class_entry *old_class = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1337
113
    ZEND_ASSERT(old_class);
1338
113
    zend_class_redeclaration_error(E_COMPILE_ERROR, old_class);
1339
113
    return NULL;
1340
113
  }
1341
1342
6.11k
  if (ce->ce_flags & ZEND_ACC_LINKED) {
1343
254
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1344
254
    return ce;
1345
254
  }
1346
1347
5.86k
  ce = zend_do_link_class(ce, lc_parent_name, Z_STR_P(lcname));
1348
5.86k
  if (ce) {
1349
4.65k
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1350
4.65k
    return ce;
1351
4.65k
  }
1352
1353
1.21k
  if (!is_preloaded) {
1354
    /* Reload bucket pointer, the hash table may have been reallocated */
1355
205
    zval *zv = zend_hash_find(EG(class_table), Z_STR_P(lcname));
1356
205
    zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(lcname + 1));
1357
1.00k
  } else {
1358
1.00k
    zend_hash_del(EG(class_table), Z_STR_P(lcname));
1359
1.00k
  }
1360
1.21k
  return NULL;
1361
5.86k
}
1362
1363
ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */
1364
5.71k
{
1365
5.71k
  zval *rtd_key, *zv;
1366
1367
5.71k
  rtd_key = lcname + 1;
1368
1369
5.71k
  zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(rtd_key));
1370
1371
5.71k
  if (UNEXPECTED(!zv)) {
1372
10
    const zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1373
10
    ZEND_ASSERT(ce);
1374
10
    zend_class_redeclaration_error(E_COMPILE_ERROR, ce);
1375
10
    return FAILURE;
1376
10
  }
1377
1378
  /* Register the derived class */
1379
5.70k
  return zend_bind_class_in_slot(zv, lcname, lc_parent_name) ? SUCCESS : FAILURE;
1380
5.71k
}
1381
/* }}} */
1382
1383
15.0k
static zend_string *add_type_string(zend_string *type, zend_string *new_type, bool is_intersection) {
1384
15.0k
  zend_string *result;
1385
15.0k
  if (type == NULL) {
1386
11.0k
    return zend_string_copy(new_type);
1387
11.0k
  }
1388
1389
4.05k
  if (is_intersection) {
1390
1.86k
    result = zend_string_concat3(ZSTR_VAL(type), ZSTR_LEN(type),
1391
1.86k
      "&", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1392
1.86k
    zend_string_release(type);
1393
2.19k
  } else {
1394
2.19k
    result = zend_string_concat3(
1395
2.19k
      ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1396
2.19k
    zend_string_release(type);
1397
2.19k
  }
1398
4.05k
  return result;
1399
15.0k
}
1400
1401
2.80k
static zend_string *resolve_class_name(zend_string *name, const zend_class_entry *scope) {
1402
2.80k
  if (scope) {
1403
1.78k
    if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1404
29
      name = scope->name;
1405
1.75k
    } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT)) && scope->parent) {
1406
0
      name = scope->parent->name;
1407
0
    }
1408
1.78k
  }
1409
1410
  /* The resolved name for anonymous classes contains null bytes. Cut off everything after the
1411
   * null byte here, to avoid larger parts of the type being omitted by printing code later. */
1412
2.80k
  size_t len = strlen(ZSTR_VAL(name));
1413
2.80k
  if (len != ZSTR_LEN(name)) {
1414
12
    return zend_string_init(ZSTR_VAL(name), len, 0);
1415
12
  }
1416
2.79k
  return zend_string_copy(name);
1417
2.80k
}
1418
1419
static zend_string *add_intersection_type(zend_string *str,
1420
  const zend_type_list *intersection_type_list, zend_class_entry *scope,
1421
  bool is_bracketed)
1422
1.11k
{
1423
1.11k
  const zend_type *single_type;
1424
1.11k
  zend_string *intersection_str = NULL;
1425
1426
4.10k
  ZEND_TYPE_LIST_FOREACH(intersection_type_list, single_type) {
1427
4.10k
    ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*single_type));
1428
4.10k
    ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*single_type));
1429
1430
2.98k
    intersection_str = add_type_string(intersection_str, ZEND_TYPE_NAME(*single_type), /* is_intersection */ true);
1431
2.98k
  } ZEND_TYPE_LIST_FOREACH_END();
1432
1433
1.11k
  ZEND_ASSERT(intersection_str);
1434
1435
1.11k
  if (is_bracketed) {
1436
798
    zend_string *result = zend_string_concat3("(", 1, ZSTR_VAL(intersection_str), ZSTR_LEN(intersection_str), ")", 1);
1437
798
    zend_string_release(intersection_str);
1438
798
    intersection_str = result;
1439
798
  }
1440
1.11k
  str = add_type_string(str, intersection_str, /* is_intersection */ false);
1441
1.11k
  zend_string_release(intersection_str);
1442
1.11k
  return str;
1443
1.11k
}
1444
1445
11.8k
zend_string *zend_type_to_string_resolved(const zend_type type, zend_class_entry *scope) {
1446
11.8k
  zend_string *str = NULL;
1447
1448
  /* Pure intersection type */
1449
11.8k
  if (ZEND_TYPE_IS_INTERSECTION(type)) {
1450
320
    ZEND_ASSERT(!ZEND_TYPE_IS_UNION(type));
1451
320
    str = add_intersection_type(str, ZEND_TYPE_LIST(type), scope, /* is_bracketed */ false);
1452
11.5k
  } else if (ZEND_TYPE_HAS_LIST(type)) {
1453
    /* A union type might not be a list */
1454
496
    const zend_type *list_type;
1455
2.16k
    ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
1456
2.16k
      if (ZEND_TYPE_IS_INTERSECTION(*list_type)) {
1457
798
        str = add_intersection_type(str, ZEND_TYPE_LIST(*list_type), scope, /* is_bracketed */ true);
1458
798
        continue;
1459
798
      }
1460
868
      ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type));
1461
868
      ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*list_type));
1462
1463
868
      zend_string *name = ZEND_TYPE_NAME(*list_type);
1464
868
      zend_string *resolved = resolve_class_name(name, scope);
1465
868
      str = add_type_string(str, resolved, /* is_intersection */ false);
1466
868
      zend_string_release(resolved);
1467
868
    } ZEND_TYPE_LIST_FOREACH_END();
1468
11.0k
  } else if (ZEND_TYPE_HAS_NAME(type)) {
1469
1.93k
    str = resolve_class_name(ZEND_TYPE_NAME(type), scope);
1470
1.93k
  }
1471
1472
11.8k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
1473
1474
11.8k
  if (type_mask == MAY_BE_ANY) {
1475
97
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_MIXED), /* is_intersection */ false);
1476
1477
97
    return str;
1478
97
  }
1479
11.7k
  if (type_mask & MAY_BE_STATIC) {
1480
100
    zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC);
1481
    // During compilation of eval'd code the called scope refers to the scope calling the eval
1482
100
    if (scope && !zend_is_compiling()) {
1483
38
      const zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
1484
38
      if (called_scope) {
1485
15
        name = called_scope->name;
1486
15
      }
1487
38
    }
1488
100
    str = add_type_string(str, name, /* is_intersection */ false);
1489
100
  }
1490
11.7k
  if (type_mask & MAY_BE_CALLABLE) {
1491
47
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE), /* is_intersection */ false);
1492
47
  }
1493
11.7k
  if (type_mask & MAY_BE_OBJECT) {
1494
161
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT), /* is_intersection */ false);
1495
161
  }
1496
11.7k
  if (type_mask & MAY_BE_ARRAY) {
1497
993
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY), /* is_intersection */ false);
1498
993
  }
1499
11.7k
  if (type_mask & MAY_BE_STRING) {
1500
4.80k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING), /* is_intersection */ false);
1501
4.80k
  }
1502
11.7k
  if (type_mask & MAY_BE_LONG) {
1503
2.83k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT), /* is_intersection */ false);
1504
2.83k
  }
1505
11.7k
  if (type_mask & MAY_BE_DOUBLE) {
1506
285
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT), /* is_intersection */ false);
1507
285
  }
1508
11.7k
  if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
1509
376
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL), /* is_intersection */ false);
1510
11.3k
  } else if (type_mask & MAY_BE_FALSE) {
1511
101
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE), /* is_intersection */ false);
1512
11.2k
  } else if (type_mask & MAY_BE_TRUE) {
1513
41
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_TRUE), /* is_intersection */ false);
1514
41
  }
1515
11.7k
  if (type_mask & MAY_BE_VOID) {
1516
97
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID), /* is_intersection */ false);
1517
97
  }
1518
11.7k
  if (type_mask & MAY_BE_NEVER) {
1519
18
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NEVER), /* is_intersection */ false);
1520
18
  }
1521
1522
11.7k
  if (type_mask & MAY_BE_NULL) {
1523
999
    bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
1524
999
    bool has_intersection = !str || memchr(ZSTR_VAL(str), '&', ZSTR_LEN(str)) != NULL;
1525
999
    if (!is_union && !has_intersection) {
1526
866
      zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
1527
866
      zend_string_release(str);
1528
866
      return nullable_str;
1529
866
    }
1530
1531
133
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE), /* is_intersection */ false);
1532
133
  }
1533
10.8k
  return str;
1534
11.7k
}
1535
1536
7.77k
ZEND_API zend_string *zend_type_to_string(zend_type type) {
1537
7.77k
  return zend_type_to_string_resolved(type, NULL);
1538
7.77k
}
1539
1540
1.45k
static bool is_generator_compatible_class_type(const zend_string *name) {
1541
1.45k
  return zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_TRAVERSABLE))
1542
1.12k
    || zend_string_equals_literal_ci(name, "Iterator")
1543
825
    || zend_string_equals_literal_ci(name, "Generator");
1544
1.45k
}
1545
1546
static void zend_mark_function_as_generator(void) /* {{{ */
1547
282k
{
1548
282k
  if (!CG(active_op_array)->function_name) {
1549
62
    zend_error_noreturn(E_COMPILE_ERROR,
1550
62
      "The \"yield\" expression can only be used inside a function");
1551
62
  }
1552
1553
282k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
1554
953
    const zend_type return_type = CG(active_op_array)->arg_info[-1].type;
1555
953
    bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_OBJECT) != 0;
1556
953
    if (!valid_type) {
1557
915
      const zend_type *single_type;
1558
2.37k
      ZEND_TYPE_FOREACH(return_type, single_type) {
1559
2.37k
        if (ZEND_TYPE_HAS_NAME(*single_type)
1560
1.45k
            && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) {
1561
887
          valid_type = true;
1562
887
          break;
1563
887
        }
1564
2.37k
      } ZEND_TYPE_FOREACH_END();
1565
915
    }
1566
1567
953
    if (!valid_type) {
1568
28
      zend_string *str = zend_type_to_string(return_type);
1569
28
      zend_error_noreturn(E_COMPILE_ERROR,
1570
28
        "Generator return type must be a supertype of Generator, %s given",
1571
28
        ZSTR_VAL(str));
1572
28
    }
1573
953
  }
1574
1575
282k
  CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR;
1576
282k
}
1577
/* }}} */
1578
1579
ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal) /* {{{ */
1580
49.4k
{
1581
49.4k
  size_t prop_name_length = 1 + src1_length + 1 + src2_length;
1582
49.4k
  zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
1583
1584
49.4k
  ZSTR_VAL(prop_name)[0] = '\0';
1585
49.4k
  memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
1586
49.4k
  memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
1587
49.4k
  return prop_name;
1588
49.4k
}
1589
/* }}} */
1590
1591
ZEND_API zend_result zend_unmangle_property_name_ex(const zend_string *name, const char **class_name, const char **prop_name, size_t *prop_len) /* {{{ */
1592
890k
{
1593
890k
  size_t class_name_len;
1594
890k
  size_t anonclass_src_len;
1595
1596
890k
  *class_name = NULL;
1597
1598
890k
  if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
1599
802k
    *prop_name = ZSTR_VAL(name);
1600
802k
    if (prop_len) {
1601
546k
      *prop_len = ZSTR_LEN(name);
1602
546k
    }
1603
802k
    return SUCCESS;
1604
802k
  }
1605
88.1k
  if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
1606
750
    zend_error(E_NOTICE, "Illegal member variable name");
1607
750
    *prop_name = ZSTR_VAL(name);
1608
750
    if (prop_len) {
1609
713
      *prop_len = ZSTR_LEN(name);
1610
713
    }
1611
750
    return FAILURE;
1612
750
  }
1613
1614
87.4k
  class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
1615
87.4k
  if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
1616
436
    zend_error(E_NOTICE, "Corrupt member variable name");
1617
436
    *prop_name = ZSTR_VAL(name);
1618
436
    if (prop_len) {
1619
284
      *prop_len = ZSTR_LEN(name);
1620
284
    }
1621
436
    return FAILURE;
1622
436
  }
1623
1624
86.9k
  *class_name = ZSTR_VAL(name) + 1;
1625
86.9k
  anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2);
1626
86.9k
  if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) {
1627
19.1k
    class_name_len += anonclass_src_len + 1;
1628
19.1k
  }
1629
86.9k
  *prop_name = ZSTR_VAL(name) + class_name_len + 2;
1630
86.9k
  if (prop_len) {
1631
53.9k
    *prop_len = ZSTR_LEN(name) - class_name_len - 2;
1632
53.9k
  }
1633
86.9k
  return SUCCESS;
1634
87.4k
}
1635
/* }}} */
1636
1637
static bool array_is_const_ex(const zend_array *array, uint32_t *max_checks)
1638
203
{
1639
203
  if (zend_hash_num_elements(array) > *max_checks) {
1640
3
    return false;
1641
3
  }
1642
200
  *max_checks -= zend_hash_num_elements(array);
1643
1644
200
  zval *element;
1645
572
  ZEND_HASH_FOREACH_VAL(array, element) {
1646
572
    if (Z_TYPE_P(element) < IS_ARRAY) {
1647
36
      continue;
1648
150
    } else if (Z_TYPE_P(element) == IS_ARRAY) {
1649
150
      if (!array_is_const_ex(array, max_checks)) {
1650
150
        return false;
1651
150
      }
1652
150
    } else {
1653
0
      return false;
1654
0
    }
1655
572
  } ZEND_HASH_FOREACH_END();
1656
1657
50
  return true;
1658
200
}
1659
1660
static bool array_is_const(const zend_array *array)
1661
53
{
1662
53
  uint32_t max_checks = 50;
1663
53
  return array_is_const_ex(array, &max_checks);
1664
53
}
1665
1666
8.98k
static bool can_ct_eval_const(const zend_constant *c) {
1667
8.98k
  if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
1668
173
    return 0;
1669
173
  }
1670
8.81k
  if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
1671
8.79k
      && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
1672
7.94k
      && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
1673
7.94k
        && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
1674
7.94k
    return 1;
1675
7.94k
  }
1676
869
  if (Z_TYPE(c->value) < IS_ARRAY
1677
869
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1678
16
    return 1;
1679
853
  } else if (Z_TYPE(c->value) == IS_ARRAY
1680
0
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)
1681
0
      && array_is_const(Z_ARR(c->value))) {
1682
0
    return 1;
1683
0
  }
1684
853
  return 0;
1685
869
}
1686
1687
static bool zend_try_ct_eval_const(zval *zv, zend_string *name, bool is_fully_qualified) /* {{{ */
1688
3.14M
{
1689
  /* Substitute true, false and null (including unqualified usage in namespaces)
1690
   * before looking up the possibly namespaced name. */
1691
3.14M
  const char *lookup_name = ZSTR_VAL(name);
1692
3.14M
  size_t lookup_len = ZSTR_LEN(name);
1693
1694
3.14M
  if (!is_fully_qualified) {
1695
3.13M
    zend_get_unqualified_name(name, &lookup_name, &lookup_len);
1696
3.13M
  }
1697
1698
3.14M
  zend_constant *c;
1699
3.14M
  if ((c = zend_get_special_const(lookup_name, lookup_len))) {
1700
33.8k
    ZVAL_COPY_VALUE(zv, &c->value);
1701
33.8k
    return 1;
1702
33.8k
  }
1703
3.10M
  c = zend_hash_find_ptr(EG(zend_constants), name);
1704
3.10M
  if (c && can_ct_eval_const(c)) {
1705
7.95k
    ZVAL_COPY_OR_DUP(zv, &c->value);
1706
7.95k
    return 1;
1707
7.95k
  }
1708
3.09M
  return 0;
1709
3.10M
}
1710
/* }}} */
1711
1712
static inline bool zend_is_scope_known(void) /* {{{ */
1713
12.2k
{
1714
12.2k
  if (!CG(active_op_array)) {
1715
    /* This can only happen when evaluating a default value string. */
1716
0
    return 0;
1717
0
  }
1718
1719
12.2k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
1720
    /* Closures can be rebound to a different scope */
1721
1.76k
    return 0;
1722
1.76k
  }
1723
1724
10.4k
  if (!CG(active_class_entry)) {
1725
    /* The scope is known if we're in a free function (no scope), but not if we're in
1726
     * a file/eval (which inherits including/eval'ing scope). */
1727
1.27k
    return CG(active_op_array)->function_name != NULL;
1728
1.27k
  }
1729
1730
  /* For traits self etc refers to the using class, not the trait itself */
1731
9.15k
  return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
1732
10.4k
}
1733
/* }}} */
1734
1735
static inline bool class_name_refers_to_active_ce(const zend_string *class_name, uint32_t fetch_type) /* {{{ */
1736
34.2k
{
1737
34.2k
  if (!CG(active_class_entry)) {
1738
29.7k
    return 0;
1739
29.7k
  }
1740
4.47k
  if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
1741
1.00k
    return 1;
1742
1.00k
  }
1743
3.46k
  return fetch_type == ZEND_FETCH_CLASS_DEFAULT
1744
2.83k
    && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
1745
4.47k
}
1746
/* }}} */
1747
1748
uint32_t zend_get_class_fetch_type(const zend_string *name) /* {{{ */
1749
1.31M
{
1750
1.31M
  if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1751
9.30k
    return ZEND_FETCH_CLASS_SELF;
1752
1.30M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT))) {
1753
5.27k
    return ZEND_FETCH_CLASS_PARENT;
1754
1.30M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_STATIC))) {
1755
1.63k
    return ZEND_FETCH_CLASS_STATIC;
1756
1.30M
  } else {
1757
1.30M
    return ZEND_FETCH_CLASS_DEFAULT;
1758
1.30M
  }
1759
1.31M
}
1760
/* }}} */
1761
1762
static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
1763
223k
{
1764
  /* Fully qualified names are always default refs */
1765
223k
  if (name_ast->attr == ZEND_NAME_FQ) {
1766
2.90k
    return ZEND_FETCH_CLASS_DEFAULT;
1767
2.90k
  }
1768
1769
220k
  return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
1770
223k
}
1771
/* }}} */
1772
1773
static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type)
1774
65.7k
{
1775
65.7k
  zend_string *class_name = zend_ast_get_str(ast);
1776
65.7k
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) {
1777
15
    zend_error_noreturn(E_COMPILE_ERROR,
1778
15
      "Cannot use \"%s\" as %s, as it is reserved",
1779
15
      ZSTR_VAL(class_name), type);
1780
15
  }
1781
65.7k
  return zend_resolve_class_name(class_name, ast->attr);
1782
65.7k
}
1783
1784
static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
1785
10.7k
{
1786
10.7k
  if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) {
1787
4.28k
    zend_class_entry *ce = CG(active_class_entry);
1788
4.28k
    if (!ce) {
1789
22
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
1790
22
        fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
1791
22
        fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
1792
4.25k
    } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) {
1793
26
      zend_error_noreturn(E_COMPILE_ERROR,
1794
26
        "Cannot use \"parent\" when current class scope has no parent");
1795
26
    }
1796
4.28k
  }
1797
10.7k
}
1798
/* }}} */
1799
1800
static bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */
1801
6.73k
{
1802
6.73k
  uint32_t fetch_type;
1803
6.73k
  const zval *class_name;
1804
1805
6.73k
  if (class_ast->kind != ZEND_AST_ZVAL) {
1806
1.79k
    return 0;
1807
1.79k
  }
1808
1809
4.94k
  class_name = zend_ast_get_zval(class_ast);
1810
1811
4.94k
  if (Z_TYPE_P(class_name) != IS_STRING) {
1812
5
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1813
5
  }
1814
1815
4.93k
  fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name));
1816
4.93k
  zend_ensure_valid_class_fetch_type(fetch_type);
1817
1818
4.93k
  switch (fetch_type) {
1819
462
    case ZEND_FETCH_CLASS_SELF:
1820
462
      if (CG(active_class_entry) && zend_is_scope_known()) {
1821
196
        ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
1822
196
        return 1;
1823
196
      }
1824
266
      return 0;
1825
822
    case ZEND_FETCH_CLASS_PARENT:
1826
822
      if (CG(active_class_entry) && CG(active_class_entry)->parent_name
1827
489
          && zend_is_scope_known()) {
1828
489
        ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name);
1829
489
        return 1;
1830
489
      }
1831
333
      return 0;
1832
295
    case ZEND_FETCH_CLASS_STATIC:
1833
295
      return 0;
1834
3.35k
    case ZEND_FETCH_CLASS_DEFAULT:
1835
3.35k
      ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
1836
3.35k
      return 1;
1837
4.93k
    EMPTY_SWITCH_DEFAULT_CASE()
1838
4.93k
  }
1839
4.93k
}
1840
/* }}} */
1841
1842
/* We don't use zend_verify_const_access because we need to deal with unlinked classes. */
1843
static bool zend_verify_ct_const_access(const zend_class_constant *c, const zend_class_entry *scope)
1844
589
{
1845
589
  if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED) {
1846
15
    return 0;
1847
574
  } else if (c->ce->ce_flags & ZEND_ACC_TRAIT) {
1848
    /* This condition is only met on directly accessing trait constants,
1849
     * because the ce is replaced to the class entry of the composing class
1850
     * on binding. */
1851
1
    return 0;
1852
573
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) {
1853
389
    return 1;
1854
389
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) {
1855
184
    return c->ce == scope;
1856
184
  } else {
1857
0
    zend_class_entry *ce = c->ce;
1858
0
    while (1) {
1859
0
      if (ce == scope) {
1860
0
        return 1;
1861
0
      }
1862
0
      if (!ce->parent) {
1863
0
        break;
1864
0
      }
1865
0
      if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
1866
0
        ce = ce->parent;
1867
0
      } else {
1868
0
        ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name);
1869
0
        if (!ce) {
1870
0
          break;
1871
0
        }
1872
0
      }
1873
0
    }
1874
    /* Reverse case cannot be true during compilation */
1875
0
    return 0;
1876
0
  }
1877
589
}
1878
1879
static bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
1880
34.2k
{
1881
34.2k
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
1882
34.2k
  zend_class_constant *cc;
1883
34.2k
  zval *c;
1884
1885
34.2k
  if (class_name_refers_to_active_ce(class_name, fetch_type)) {
1886
1.10k
    cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
1887
33.1k
  } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1888
553
    const zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name);
1889
553
    if (ce) {
1890
62
      cc = zend_hash_find_ptr(&ce->constants_table, name);
1891
491
    } else {
1892
491
      return 0;
1893
491
    }
1894
32.6k
  } else {
1895
32.6k
    return 0;
1896
32.6k
  }
1897
1898
1.16k
  if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
1899
334
    return 0;
1900
334
  }
1901
1902
833
  if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) {
1903
260
    return 0;
1904
260
  }
1905
1906
573
  c = &cc->value;
1907
1908
  /* Substitute case-sensitive (or lowercase) persistent class constants */
1909
573
  if (Z_TYPE_P(c) < IS_ARRAY) {
1910
346
    ZVAL_COPY_OR_DUP(zv, c);
1911
346
    return 1;
1912
346
  } else if (Z_TYPE_P(c) == IS_ARRAY && array_is_const(Z_ARR_P(c))) {
1913
50
    ZVAL_COPY_OR_DUP(zv, c);
1914
50
    return 1;
1915
50
  }
1916
1917
177
  return 0;
1918
573
}
1919
/* }}} */
1920
1921
static void zend_add_to_list(void *result, void *item) /* {{{ */
1922
2.34k
{
1923
2.34k
  void** list = *(void**)result;
1924
2.34k
  size_t n = 0;
1925
1926
2.34k
  if (list) {
1927
45.3k
    while (list[n]) {
1928
43.6k
      n++;
1929
43.6k
    }
1930
1.70k
  }
1931
1932
2.34k
  list = erealloc(list, sizeof(void*) * (n+2));
1933
1934
2.34k
  list[n]   = item;
1935
2.34k
  list[n+1] = NULL;
1936
1937
2.34k
  *(void**)result = list;
1938
2.34k
}
1939
/* }}} */
1940
1941
static void zend_do_extended_stmt(znode* result) /* {{{ */
1942
736k
{
1943
736k
  zend_op *opline;
1944
1945
736k
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) {
1946
736k
    return;
1947
736k
  }
1948
1949
0
  opline = get_next_op();
1950
1951
0
  opline->opcode = ZEND_EXT_STMT;
1952
0
  if (result) {
1953
0
    SET_NODE(opline->op1, result);
1954
0
  }
1955
0
}
1956
/* }}} */
1957
1958
static void zend_do_extended_fcall_begin(void) /* {{{ */
1959
1.57M
{
1960
1.57M
  zend_op *opline;
1961
1962
1.57M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1963
1.57M
    return;
1964
1.57M
  }
1965
1966
0
  opline = get_next_op();
1967
1968
0
  opline->opcode = ZEND_EXT_FCALL_BEGIN;
1969
0
}
1970
/* }}} */
1971
1972
static void zend_do_extended_fcall_end(void) /* {{{ */
1973
1.57M
{
1974
1.57M
  zend_op *opline;
1975
1976
1.57M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1977
1.57M
    return;
1978
1.57M
  }
1979
1980
0
  opline = get_next_op();
1981
1982
0
  opline->opcode = ZEND_EXT_FCALL_END;
1983
0
}
1984
/* }}} */
1985
1986
0
ZEND_API bool zend_is_auto_global_str(const char *name, size_t len) /* {{{ */ {
1987
0
  zend_auto_global *auto_global;
1988
1989
0
  if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) {
1990
0
    if (auto_global->armed) {
1991
0
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1992
0
    }
1993
0
    return 1;
1994
0
  }
1995
0
  return 0;
1996
0
}
1997
/* }}} */
1998
1999
ZEND_API bool zend_is_auto_global(zend_string *name) /* {{{ */
2000
1.61M
{
2001
1.61M
  zend_auto_global *auto_global;
2002
2003
1.61M
  if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) {
2004
5.79k
    if (auto_global->armed) {
2005
154
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
2006
154
    }
2007
5.79k
    return 1;
2008
5.79k
  }
2009
1.60M
  return 0;
2010
1.61M
}
2011
/* }}} */
2012
2013
ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
2014
128
{
2015
128
  zend_auto_global auto_global;
2016
128
  zend_result retval;
2017
2018
128
  auto_global.name = name;
2019
128
  auto_global.auto_global_callback = auto_global_callback;
2020
128
  auto_global.jit = jit;
2021
2022
128
  retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
2023
2024
128
  return retval;
2025
128
}
2026
/* }}} */
2027
2028
ZEND_API void zend_activate_auto_globals(void) /* {{{ */
2029
191k
{
2030
191k
  zend_auto_global *auto_global;
2031
2032
3.45M
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2033
3.45M
    auto_global->armed = auto_global->jit || auto_global->auto_global_callback;
2034
3.45M
  } ZEND_HASH_FOREACH_END();
2035
2036
3.45M
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2037
3.45M
    if (auto_global->armed && !auto_global->jit) {
2038
766k
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
2039
766k
    }
2040
3.45M
  } ZEND_HASH_FOREACH_END();
2041
191k
}
2042
/* }}} */
2043
2044
int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
2045
8.39M
{
2046
8.39M
  zval zv;
2047
8.39M
  int ret;
2048
2049
8.39M
  if (CG(increment_lineno)) {
2050
11.3k
    CG(zend_lineno)++;
2051
11.3k
    CG(increment_lineno) = 0;
2052
11.3k
  }
2053
2054
8.39M
  ret = lex_scan(&zv, elem);
2055
8.39M
  ZEND_ASSERT(!EG(exception) || ret == T_ERROR);
2056
8.39M
  return ret;
2057
2058
8.39M
}
2059
/* }}} */
2060
2061
ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers) /* {{{ */
2062
145k
{
2063
145k
  bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS;
2064
2065
145k
  ce->refcount = 1;
2066
145k
  ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
2067
145k
  ce->ce_flags2 = 0;
2068
2069
145k
  if (CG(compiler_options) & ZEND_COMPILE_GUARDS) {
2070
0
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2071
0
  }
2072
2073
145k
  ce->default_properties_table = NULL;
2074
145k
  ce->default_static_members_table = NULL;
2075
145k
  zend_hash_init(&ce->properties_info, 8, NULL, NULL, persistent_hashes);
2076
145k
  zend_hash_init(&ce->constants_table, 8, NULL, NULL, persistent_hashes);
2077
145k
  zend_hash_init(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes);
2078
2079
145k
  ce->doc_comment = NULL;
2080
2081
145k
  ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
2082
145k
  ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
2083
2084
145k
  ce->default_object_handlers = &std_object_handlers;
2085
145k
  ce->default_properties_count = 0;
2086
145k
  ce->default_static_members_count = 0;
2087
145k
  ce->properties_info_table = NULL;
2088
145k
  ce->attributes = NULL;
2089
145k
  ce->enum_backing_type = IS_UNDEF;
2090
145k
  ce->backed_enum_table = NULL;
2091
2092
145k
  if (nullify_handlers) {
2093
143k
    ce->constructor = NULL;
2094
143k
    ce->destructor = NULL;
2095
143k
    ce->clone = NULL;
2096
143k
    ce->__get = NULL;
2097
143k
    ce->__set = NULL;
2098
143k
    ce->__unset = NULL;
2099
143k
    ce->__isset = NULL;
2100
143k
    ce->__call = NULL;
2101
143k
    ce->__callstatic = NULL;
2102
143k
    ce->__tostring = NULL;
2103
143k
    ce->__serialize = NULL;
2104
143k
    ce->__unserialize = NULL;
2105
143k
    ce->__debugInfo = NULL;
2106
143k
    ce->create_object = NULL;
2107
143k
    ce->get_iterator = NULL;
2108
143k
    ce->iterator_funcs_ptr = NULL;
2109
143k
    ce->arrayaccess_funcs_ptr = NULL;
2110
143k
    ce->get_static_method = NULL;
2111
143k
    ce->parent = NULL;
2112
143k
    ce->parent_name = NULL;
2113
143k
    ce->num_interfaces = 0;
2114
143k
    ce->interfaces = NULL;
2115
143k
    ce->num_traits = 0;
2116
143k
    ce->num_hooked_props = 0;
2117
143k
    ce->num_hooked_prop_variance_checks = 0;
2118
143k
    ce->trait_names = NULL;
2119
143k
    ce->trait_aliases = NULL;
2120
143k
    ce->trait_precedences = NULL;
2121
143k
    ce->serialize = NULL;
2122
143k
    ce->unserialize = NULL;
2123
143k
    if (ce->type == ZEND_INTERNAL_CLASS) {
2124
0
      ce->info.internal.module = NULL;
2125
0
      ce->info.internal.builtin_functions = NULL;
2126
0
    }
2127
143k
  }
2128
145k
}
2129
/* }}} */
2130
2131
ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
2132
0
{
2133
0
  return op_array->vars[EX_VAR_TO_NUM(var)];
2134
0
}
2135
/* }}} */
2136
2137
zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
2138
0
{
2139
0
  zval *left_zv = zend_ast_get_zval(left_ast);
2140
0
  zend_string *left = Z_STR_P(left_zv);
2141
0
  zend_string *right = zend_ast_get_str(right_ast);
2142
2143
0
  zend_string *result;
2144
0
  size_t left_len = ZSTR_LEN(left);
2145
0
  size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
2146
2147
0
  result = zend_string_extend(left, len, 0);
2148
0
  ZSTR_VAL(result)[left_len] = '\\';
2149
0
  memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
2150
0
  ZSTR_VAL(result)[len] = '\0';
2151
0
  zend_string_release_ex(right, 0);
2152
2153
0
  ZVAL_STR(left_zv, result);
2154
0
  return left_ast;
2155
0
}
2156
/* }}} */
2157
2158
zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
2159
1.30k
{
2160
1.30k
  zval *zv = zend_ast_get_zval(ast);
2161
1.30k
  if (Z_TYPE_P(zv) == IS_LONG) {
2162
1.00k
    if (Z_LVAL_P(zv) == 0) {
2163
557
      ZVAL_NEW_STR(zv, ZSTR_INIT_LITERAL("-0", 0));
2164
557
    } else {
2165
448
      ZEND_ASSERT(Z_LVAL_P(zv) > 0);
2166
448
      Z_LVAL_P(zv) *= -1;
2167
448
    }
2168
1.00k
  } else if (Z_TYPE_P(zv) == IS_STRING) {
2169
297
    size_t orig_len = Z_STRLEN_P(zv);
2170
297
    Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0);
2171
297
    memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
2172
297
    Z_STRVAL_P(zv)[0] = '-';
2173
297
  } else {
2174
0
    ZEND_UNREACHABLE();
2175
0
  }
2176
1.30k
  return ast;
2177
1.30k
}
2178
/* }}} */
2179
2180
static void zend_verify_namespace(void) /* {{{ */
2181
403k
{
2182
403k
  if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
2183
38
    zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
2184
38
  }
2185
403k
}
2186
/* }}} */
2187
2188
/* {{{ zend_dirname
2189
   Returns directory name component of path */
2190
ZEND_API size_t zend_dirname(char *path, size_t len)
2191
1.29k
{
2192
1.29k
  char *end = path + len - 1;
2193
1.29k
  unsigned int len_adjust = 0;
2194
2195
#ifdef ZEND_WIN32
2196
  /* Note that on Win32 CWD is per drive (heritage from CP/M).
2197
   * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
2198
   */
2199
  if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
2200
    /* Skip over the drive spec (if any) so as not to change */
2201
    path += 2;
2202
    len_adjust += 2;
2203
    if (2 == len) {
2204
      /* Return "c:" on Win32 for dirname("c:").
2205
       * It would be more consistent to return "c:."
2206
       * but that would require making the string *longer*.
2207
       */
2208
      return len;
2209
    }
2210
  }
2211
#endif
2212
2213
1.29k
  if (len == 0) {
2214
    /* Illegal use of this function */
2215
0
    return 0;
2216
0
  }
2217
2218
  /* Strip trailing slashes */
2219
1.29k
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2220
0
    end--;
2221
0
  }
2222
1.29k
  if (end < path) {
2223
    /* The path only contained slashes */
2224
0
    path[0] = DEFAULT_SLASH;
2225
0
    path[1] = '\0';
2226
0
    return 1 + len_adjust;
2227
0
  }
2228
2229
  /* Strip filename */
2230
14.3k
  while (end >= path && !IS_SLASH_P_EX(end, end == path)) {
2231
13.0k
    end--;
2232
13.0k
  }
2233
1.29k
  if (end < path) {
2234
    /* No slash found, therefore return '.' */
2235
799
    path[0] = '.';
2236
799
    path[1] = '\0';
2237
799
    return 1 + len_adjust;
2238
799
  }
2239
2240
  /* Strip slashes which came before the file name */
2241
996
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2242
498
    end--;
2243
498
  }
2244
498
  if (end < path) {
2245
5
    path[0] = DEFAULT_SLASH;
2246
5
    path[1] = '\0';
2247
5
    return 1 + len_adjust;
2248
5
  }
2249
493
  *(end+1) = '\0';
2250
2251
493
  return (size_t)(end + 1 - path) + len_adjust;
2252
498
}
2253
/* }}} */
2254
2255
static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */
2256
620k
{
2257
620k
  uint_fast8_t factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3;
2258
2259
620k
  switch (type) {
2260
337k
    case BP_VAR_R:
2261
337k
      opline->result_type = IS_TMP_VAR;
2262
337k
      result->op_type = IS_TMP_VAR;
2263
337k
      return;
2264
96.1k
    case BP_VAR_W:
2265
96.1k
      opline->opcode += 1 * factor;
2266
96.1k
      return;
2267
10.4k
    case BP_VAR_RW:
2268
10.4k
      opline->opcode += 2 * factor;
2269
10.4k
      return;
2270
66.0k
    case BP_VAR_IS:
2271
66.0k
      opline->result_type = IS_TMP_VAR;
2272
66.0k
      result->op_type = IS_TMP_VAR;
2273
66.0k
      opline->opcode += 3 * factor;
2274
66.0k
      return;
2275
106k
    case BP_VAR_FUNC_ARG:
2276
106k
      opline->opcode += 4 * factor;
2277
106k
      return;
2278
3.71k
    case BP_VAR_UNSET:
2279
3.71k
      opline->opcode += 5 * factor;
2280
3.71k
      return;
2281
620k
    EMPTY_SWITCH_DEFAULT_CASE()
2282
620k
  }
2283
620k
}
2284
/* }}} */
2285
2286
static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
2287
2.26M
{
2288
2.26M
  opline->result_type = IS_VAR;
2289
2.26M
  opline->result.var = get_temporary_variable();
2290
2.26M
  GET_NODE(result, opline->result);
2291
2.26M
}
2292
/* }}} */
2293
2294
static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
2295
11.7M
{
2296
11.7M
  opline->result_type = IS_TMP_VAR;
2297
11.7M
  opline->result.var = get_temporary_variable();
2298
11.7M
  GET_NODE(result, opline->result);
2299
11.7M
}
2300
/* }}} */
2301
2302
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2303
13.8M
{
2304
13.8M
  zend_op *opline = get_next_op();
2305
13.8M
  opline->opcode = opcode;
2306
2307
13.8M
  if (op1 != NULL) {
2308
10.7M
    SET_NODE(opline->op1, op1);
2309
10.7M
  }
2310
2311
13.8M
  if (op2 != NULL) {
2312
641k
    SET_NODE(opline->op2, op2);
2313
641k
  }
2314
2315
13.8M
  if (result) {
2316
1.74M
    zend_make_var_result(result, opline);
2317
1.74M
  }
2318
13.8M
  return opline;
2319
13.8M
}
2320
/* }}} */
2321
2322
static zend_op *zend_emit_op_tmp(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2323
12.4M
{
2324
12.4M
  zend_op *opline = get_next_op();
2325
12.4M
  opline->opcode = opcode;
2326
2327
12.4M
  if (op1 != NULL) {
2328
3.52M
    SET_NODE(opline->op1, op1);
2329
3.52M
  }
2330
2331
12.4M
  if (op2 != NULL) {
2332
1.77M
    SET_NODE(opline->op2, op2);
2333
1.77M
  }
2334
2335
12.4M
  if (result) {
2336
11.6M
    zend_make_tmp_result(result, opline);
2337
11.6M
  }
2338
2339
12.4M
  return opline;
2340
12.4M
}
2341
/* }}} */
2342
2343
static void zend_emit_tick(void) /* {{{ */
2344
30.7k
{
2345
30.7k
  zend_op *opline;
2346
2347
  /* This prevents a double TICK generated by the parser statement of "declare()" */
2348
30.7k
  if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
2349
2.36k
    return;
2350
2.36k
  }
2351
2352
28.3k
  opline = get_next_op();
2353
2354
28.3k
  opline->opcode = ZEND_TICKS;
2355
28.3k
  opline->extended_value = FC(declarables).ticks;
2356
28.3k
}
2357
/* }}} */
2358
2359
static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
2360
84.7k
{
2361
84.7k
  return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
2362
84.7k
}
2363
/* }}} */
2364
2365
static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
2366
425k
{
2367
425k
  uint32_t opnum = get_next_op_number();
2368
425k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
2369
425k
  opline->op1.opline_num = opnum_target;
2370
425k
  return opnum;
2371
425k
}
2372
/* }}} */
2373
2374
ZEND_API bool zend_is_smart_branch(const zend_op *opline) /* {{{ */
2375
201k
{
2376
201k
  switch (opline->opcode) {
2377
2.04k
    case ZEND_IS_IDENTICAL:
2378
2.45k
    case ZEND_IS_NOT_IDENTICAL:
2379
27.7k
    case ZEND_IS_EQUAL:
2380
28.5k
    case ZEND_IS_NOT_EQUAL:
2381
134k
    case ZEND_IS_SMALLER:
2382
135k
    case ZEND_IS_SMALLER_OR_EQUAL:
2383
139k
    case ZEND_CASE:
2384
140k
    case ZEND_CASE_STRICT:
2385
140k
    case ZEND_ISSET_ISEMPTY_CV:
2386
140k
    case ZEND_ISSET_ISEMPTY_VAR:
2387
141k
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2388
141k
    case ZEND_ISSET_ISEMPTY_PROP_OBJ:
2389
141k
    case ZEND_ISSET_ISEMPTY_STATIC_PROP:
2390
141k
    case ZEND_INSTANCEOF:
2391
141k
    case ZEND_TYPE_CHECK:
2392
141k
    case ZEND_DEFINED:
2393
141k
    case ZEND_IN_ARRAY:
2394
142k
    case ZEND_ARRAY_KEY_EXISTS:
2395
142k
      return 1;
2396
59.4k
    default:
2397
59.4k
      return 0;
2398
201k
  }
2399
201k
}
2400
/* }}} */
2401
2402
static inline uint32_t zend_emit_cond_jump(uint8_t opcode, znode *cond, uint32_t opnum_target) /* {{{ */
2403
270k
{
2404
270k
  uint32_t opnum = get_next_op_number();
2405
270k
  zend_op *opline;
2406
2407
270k
  if (cond->op_type == IS_TMP_VAR && opnum > 0) {
2408
161k
    opline = CG(active_op_array)->opcodes + opnum - 1;
2409
161k
    if (opline->result_type == IS_TMP_VAR
2410
159k
     && opline->result.var == cond->u.op.var
2411
159k
     && zend_is_smart_branch(opline)) {
2412
141k
      if (opcode == ZEND_JMPZ) {
2413
117k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ;
2414
117k
      } else {
2415
24.9k
        ZEND_ASSERT(opcode == ZEND_JMPNZ);
2416
24.9k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ;
2417
24.9k
      }
2418
141k
    }
2419
161k
  }
2420
270k
  opline = zend_emit_op(NULL, opcode, cond, NULL);
2421
270k
  opline->op2.opline_num = opnum_target;
2422
270k
  return opnum;
2423
270k
}
2424
/* }}} */
2425
2426
static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
2427
703k
{
2428
703k
  zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
2429
703k
  switch (opline->opcode) {
2430
411k
    case ZEND_JMP:
2431
411k
      opline->op1.opline_num = opnum_target;
2432
411k
      break;
2433
235k
    case ZEND_JMPZ:
2434
257k
    case ZEND_JMPNZ:
2435
261k
    case ZEND_JMPZ_EX:
2436
265k
    case ZEND_JMPNZ_EX:
2437
267k
    case ZEND_JMP_SET:
2438
291k
    case ZEND_COALESCE:
2439
291k
    case ZEND_JMP_NULL:
2440
292k
    case ZEND_BIND_INIT_STATIC_OR_JMP:
2441
292k
    case ZEND_JMP_FRAMELESS:
2442
292k
      opline->op2.opline_num = opnum_target;
2443
292k
      break;
2444
703k
    EMPTY_SWITCH_DEFAULT_CASE()
2445
703k
  }
2446
703k
}
2447
/* }}} */
2448
2449
static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
2450
700k
{
2451
700k
  zend_update_jump_target(opnum_jump, get_next_op_number());
2452
700k
}
2453
/* }}} */
2454
2455
static inline zend_op *zend_delayed_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2456
528k
{
2457
528k
  zend_op tmp_opline;
2458
2459
528k
  init_op(&tmp_opline);
2460
2461
528k
  tmp_opline.opcode = opcode;
2462
528k
  if (op1 != NULL) {
2463
528k
    SET_NODE(tmp_opline.op1, op1);
2464
528k
  }
2465
528k
  if (op2 != NULL) {
2466
514k
    SET_NODE(tmp_opline.op2, op2);
2467
514k
  }
2468
528k
  if (result) {
2469
525k
    zend_make_var_result(result, &tmp_opline);
2470
525k
  }
2471
2472
528k
  zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
2473
528k
  return zend_stack_top(&CG(delayed_oplines_stack));
2474
528k
}
2475
/* }}} */
2476
2477
static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
2478
745k
{
2479
745k
  return zend_stack_count(&CG(delayed_oplines_stack));
2480
745k
}
2481
/* }}} */
2482
2483
static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
2484
744k
{
2485
744k
  zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
2486
744k
  uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
2487
2488
744k
  ZEND_ASSERT(count >= offset);
2489
1.27M
  for (i = offset; i < count; ++i) {
2490
528k
    if (EXPECTED(oplines[i].opcode != ZEND_NOP)) {
2491
519k
      opline = get_next_op();
2492
519k
      memcpy(opline, &oplines[i], sizeof(zend_op));
2493
519k
    } else {
2494
8.86k
      opline = CG(active_op_array)->opcodes + oplines[i].extended_value;
2495
8.86k
    }
2496
528k
  }
2497
2498
744k
  CG(delayed_oplines_stack).top = offset;
2499
744k
  return opline;
2500
744k
}
2501
/* }}} */
2502
2503
static bool zend_ast_kind_is_short_circuited(zend_ast_kind ast_kind)
2504
20.8M
{
2505
20.8M
  switch (ast_kind) {
2506
430k
    case ZEND_AST_DIM:
2507
514k
    case ZEND_AST_PROP:
2508
638k
    case ZEND_AST_NULLSAFE_PROP:
2509
655k
    case ZEND_AST_STATIC_PROP:
2510
795k
    case ZEND_AST_METHOD_CALL:
2511
800k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2512
829k
    case ZEND_AST_STATIC_CALL:
2513
829k
      return 1;
2514
20.0M
    default:
2515
20.0M
      return 0;
2516
20.8M
  }
2517
20.8M
}
2518
2519
static bool zend_ast_is_short_circuited(const zend_ast *ast)
2520
935k
{
2521
935k
  switch (ast->kind) {
2522
184k
    case ZEND_AST_DIM:
2523
212k
    case ZEND_AST_PROP:
2524
221k
    case ZEND_AST_STATIC_PROP:
2525
235k
    case ZEND_AST_METHOD_CALL:
2526
237k
    case ZEND_AST_STATIC_CALL:
2527
237k
      return zend_ast_is_short_circuited(ast->child[0]);
2528
567
    case ZEND_AST_NULLSAFE_PROP:
2529
662
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2530
662
      return 1;
2531
697k
    default:
2532
697k
      return 0;
2533
935k
  }
2534
935k
}
2535
2536
static void zend_assert_not_short_circuited(const zend_ast *ast)
2537
10.6k
{
2538
10.6k
  if (zend_ast_is_short_circuited(ast)) {
2539
42
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain");
2540
42
  }
2541
10.6k
}
2542
2543
/* Mark nodes that are an inner part of a short-circuiting chain.
2544
 * We should not perform a "commit" on them, as it will be performed by the outer-most node.
2545
 * We do this to avoid passing down an argument in various compile functions. */
2546
2547
843k
#define ZEND_SHORT_CIRCUITING_INNER 0x8000
2548
2549
588k
static void zend_short_circuiting_mark_inner(zend_ast *ast) {
2550
588k
  if (zend_ast_kind_is_short_circuited(ast->kind)) {
2551
276k
    ast->attr |= ZEND_SHORT_CIRCUITING_INNER;
2552
276k
  }
2553
588k
}
2554
2555
static uint32_t zend_short_circuiting_checkpoint(void)
2556
20.3M
{
2557
20.3M
  return zend_stack_count(&CG(short_circuiting_opnums));
2558
20.3M
}
2559
2560
static void zend_short_circuiting_commit(uint32_t checkpoint, znode *result, const zend_ast *ast)
2561
20.2M
{
2562
20.2M
  bool is_short_circuited = zend_ast_kind_is_short_circuited(ast->kind)
2563
19.6M
    || ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY;
2564
20.2M
  if (!is_short_circuited) {
2565
19.6M
    ZEND_ASSERT(zend_stack_count(&CG(short_circuiting_opnums)) == checkpoint
2566
19.6M
      && "Short circuiting stack should be empty");
2567
19.6M
    return;
2568
19.6M
  }
2569
2570
566k
  if (ast->attr & ZEND_SHORT_CIRCUITING_INNER) {
2571
    /* Outer-most node will commit. */
2572
68.3k
    return;
2573
68.3k
  }
2574
2575
564k
  while (zend_stack_count(&CG(short_circuiting_opnums)) != checkpoint) {
2576
65.9k
    uint32_t opnum = *(uint32_t *) zend_stack_top(&CG(short_circuiting_opnums));
2577
65.9k
    zend_op *opline = &CG(active_op_array)->opcodes[opnum];
2578
65.9k
    opline->op2.opline_num = get_next_op_number();
2579
65.9k
    SET_NODE(opline->result, result);
2580
65.9k
    opline->extended_value |=
2581
65.9k
      ast->kind == ZEND_AST_ISSET ? ZEND_SHORT_CIRCUITING_CHAIN_ISSET :
2582
65.9k
      ast->kind == ZEND_AST_EMPTY ? ZEND_SHORT_CIRCUITING_CHAIN_EMPTY :
2583
65.8k
                                    ZEND_SHORT_CIRCUITING_CHAIN_EXPR;
2584
65.9k
    zend_stack_del_top(&CG(short_circuiting_opnums));
2585
65.9k
  }
2586
498k
}
2587
2588
static void zend_emit_jmp_null(znode *obj_node, uint32_t bp_type)
2589
66.0k
{
2590
66.0k
  uint32_t jmp_null_opnum = get_next_op_number();
2591
66.0k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP_NULL, obj_node, NULL);
2592
66.0k
  if (opline->op1_type == IS_CONST) {
2593
815
    Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
2594
815
  }
2595
66.0k
  if (bp_type == BP_VAR_IS) {
2596
884
    opline->extended_value |= ZEND_JMP_NULL_BP_VAR_IS;
2597
884
  }
2598
66.0k
  zend_stack_push(&CG(short_circuiting_opnums), &jmp_null_opnum);
2599
66.0k
}
2600
2601
static inline bool zend_is_variable_or_call(const zend_ast *ast);
2602
2603
static void zend_compile_memoized_expr(znode *result, zend_ast *expr, uint32_t type) /* {{{ */
2604
149k
{
2605
149k
  const zend_memoize_mode memoize_mode = CG(memoize_mode);
2606
149k
  if (memoize_mode == ZEND_MEMOIZE_COMPILE) {
2607
74.8k
    znode memoized_result;
2608
2609
    /* Go through normal compilation */
2610
74.8k
    CG(memoize_mode) = ZEND_MEMOIZE_NONE;
2611
74.8k
    if (zend_is_variable_or_call(expr)) {
2612
17.9k
      zend_compile_var(result, expr, type, /* by_ref */ false);
2613
56.9k
    } else {
2614
56.9k
      zend_compile_expr(result, expr);
2615
56.9k
    }
2616
74.8k
    CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
2617
2618
74.8k
    if (result->op_type == IS_VAR) {
2619
15.4k
      zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL);
2620
59.4k
    } else if (result->op_type == IS_TMP_VAR) {
2621
52.7k
      zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL);
2622
52.7k
    } else {
2623
6.68k
      if (result->op_type == IS_CONST) {
2624
5.71k
        Z_TRY_ADDREF(result->u.constant);
2625
5.71k
      }
2626
6.68k
      memoized_result = *result;
2627
6.68k
    }
2628
2629
74.8k
    zend_hash_index_update_mem(
2630
74.8k
      CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode));
2631
74.8k
  } else if (memoize_mode == ZEND_MEMOIZE_FETCH) {
2632
74.1k
    const znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr);
2633
74.1k
    *result = *memoized_result;
2634
74.1k
    if (result->op_type == IS_CONST) {
2635
5.43k
      Z_TRY_ADDREF(result->u.constant);
2636
5.43k
    }
2637
74.1k
  } else {
2638
0
    ZEND_UNREACHABLE();
2639
0
  }
2640
149k
}
2641
/* }}} */
2642
2643
static void zend_emit_return_type_check(
2644
    znode *expr, const zend_arg_info *return_info, bool implicit) /* {{{ */
2645
20.1k
{
2646
20.1k
  zend_type type = return_info->type;
2647
20.1k
  if (ZEND_TYPE_IS_SET(type)) {
2648
20.1k
    zend_op *opline;
2649
2650
    /* `return ...;` is illegal in a void function (but `return;` isn't) */
2651
20.1k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) {
2652
3.75k
      if (expr) {
2653
17
        if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
2654
7
          zend_error_noreturn(E_COMPILE_ERROR,
2655
7
            "A void %s must not return a value "
2656
7
            "(did you mean \"return;\" instead of \"return null;\"?)",
2657
7
            CG(active_class_entry) != NULL ? "method" : "function");
2658
10
        } else {
2659
10
          zend_error_noreturn(E_COMPILE_ERROR, "A void %s must not return a value",
2660
10
          CG(active_class_entry) != NULL ? "method" : "function");
2661
10
        }
2662
17
      }
2663
      /* we don't need run-time check */
2664
3.74k
      return;
2665
3.75k
    }
2666
2667
    /* `return` is illegal in a never-returning function */
2668
16.3k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_NEVER)) {
2669
      /* Implicit case handled separately using VERIFY_NEVER_TYPE opcode. */
2670
9
      ZEND_ASSERT(!implicit);
2671
9
      zend_error_noreturn(E_COMPILE_ERROR, "A never-returning %s must not return",
2672
9
        CG(active_class_entry) != NULL ? "method" : "function");
2673
9
    }
2674
2675
16.3k
    if (!expr && !implicit) {
2676
11
      if (ZEND_TYPE_ALLOW_NULL(type)) {
2677
5
        zend_error_noreturn(E_COMPILE_ERROR,
2678
5
          "A %s with return type must return a value "
2679
5
          "(did you mean \"return null;\" instead of \"return;\"?)",
2680
5
          CG(active_class_entry) != NULL ? "method" : "function");
2681
6
      } else {
2682
6
        zend_error_noreturn(E_COMPILE_ERROR,
2683
6
          "A %s with return type must return a value",
2684
6
          CG(active_class_entry) != NULL ? "method" : "function");
2685
6
      }
2686
11
    }
2687
2688
16.3k
    if (expr && ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) {
2689
      /* we don't need run-time check for mixed return type */
2690
485
      return;
2691
485
    }
2692
2693
15.8k
    if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) {
2694
      /* we don't need run-time check */
2695
1.24k
      return;
2696
1.24k
    }
2697
2698
14.6k
    opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
2699
14.6k
    if (expr && expr->op_type == IS_CONST) {
2700
177
      opline->result_type = expr->op_type = IS_TMP_VAR;
2701
177
      opline->result.var = expr->u.op.var = get_temporary_variable();
2702
177
    }
2703
14.6k
  }
2704
20.1k
}
2705
/* }}} */
2706
2707
void zend_emit_final_return(bool return_one) /* {{{ */
2708
663k
{
2709
663k
  znode zn;
2710
663k
  zend_op *ret;
2711
663k
  bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
2712
2713
663k
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)
2714
16.0k
      && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) {
2715
15.7k
    zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
2716
2717
15.7k
    if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
2718
369
      zend_emit_op(NULL, ZEND_VERIFY_NEVER_TYPE, NULL, NULL);
2719
369
      return;
2720
369
    }
2721
2722
15.3k
    zend_emit_return_type_check(NULL, return_info, true);
2723
15.3k
  }
2724
2725
663k
  zn.op_type = IS_CONST;
2726
663k
  if (return_one) {
2727
64.6k
    ZVAL_LONG(&zn.u.constant, 1);
2728
598k
  } else {
2729
598k
    ZVAL_NULL(&zn.u.constant);
2730
598k
  }
2731
2732
663k
  ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
2733
663k
  ret->extended_value = -1;
2734
663k
}
2735
/* }}} */
2736
2737
static inline bool zend_is_variable(const zend_ast *ast) /* {{{ */
2738
2.06M
{
2739
2.06M
  return ast->kind == ZEND_AST_VAR
2740
1.94M
    || ast->kind == ZEND_AST_DIM
2741
1.86M
    || ast->kind == ZEND_AST_PROP
2742
1.86M
    || ast->kind == ZEND_AST_NULLSAFE_PROP
2743
1.85M
    || ast->kind == ZEND_AST_STATIC_PROP;
2744
2.06M
}
2745
/* }}} */
2746
2747
static bool zend_propagate_list_refs(zend_ast *ast);
2748
2749
static inline bool zend_is_passable_by_ref(const zend_ast *ast)
2750
1.91M
{
2751
1.91M
  if (zend_is_variable(ast) || ast->kind == ZEND_AST_ASSIGN_REF) {
2752
140k
    return true;
2753
140k
  }
2754
1.77M
  if (ast->kind == ZEND_AST_ASSIGN
2755
3.25k
   && UNEXPECTED(ast->child[0]->kind == ZEND_AST_ARRAY)
2756
22
   && zend_propagate_list_refs(ast->child[0])) {
2757
10
    return true;
2758
10
  }
2759
1.77M
  return false;
2760
1.77M
}
2761
2762
static inline bool zend_is_call(const zend_ast *ast) /* {{{ */
2763
2.18M
{
2764
2.18M
  return ast->kind == ZEND_AST_CALL
2765
2.10M
    || ast->kind == ZEND_AST_METHOD_CALL
2766
2.07M
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
2767
2.07M
    || ast->kind == ZEND_AST_STATIC_CALL
2768
2.06M
    || ast->kind == ZEND_AST_PIPE;
2769
2.18M
}
2770
/* }}} */
2771
2772
static inline bool zend_is_variable_or_call(const zend_ast *ast) /* {{{ */
2773
98.9k
{
2774
98.9k
  return zend_is_variable(ast) || zend_is_call(ast);
2775
98.9k
}
2776
/* }}} */
2777
2778
static inline bool zend_is_unticked_stmt(const zend_ast *ast) /* {{{ */
2779
43.8k
{
2780
43.8k
  return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL
2781
33.0k
    || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP
2782
31.8k
    || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD;
2783
43.8k
}
2784
/* }}} */
2785
2786
static inline bool zend_can_write_to_variable(const zend_ast *ast) /* {{{ */
2787
19.1k
{
2788
19.1k
  while (
2789
22.1k
    ast->kind == ZEND_AST_DIM
2790
20.8k
    || ast->kind == ZEND_AST_PROP
2791
19.1k
  ) {
2792
2.97k
    ast = ast->child[0];
2793
2.97k
  }
2794
2795
19.1k
  return zend_is_variable_or_call(ast) && !zend_ast_is_short_circuited(ast);
2796
19.1k
}
2797
/* }}} */
2798
2799
static inline bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
2800
36.4k
{
2801
36.4k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2802
0
    return 0;
2803
0
  }
2804
2805
36.4k
  return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
2806
36.4k
}
2807
/* }}} */
2808
2809
static inline void zend_handle_numeric_op(znode *node) /* {{{ */
2810
4.30k
{
2811
4.30k
  if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
2812
2.31k
    zend_ulong index;
2813
2814
2.31k
    if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
2815
306
      zval_ptr_dtor(&node->u.constant);
2816
306
      ZVAL_LONG(&node->u.constant, index);
2817
306
    }
2818
2.31k
  }
2819
4.30k
}
2820
/* }}} */
2821
2822
static inline void zend_handle_numeric_dim(const zend_op *opline, znode *dim_node) /* {{{ */
2823
257k
{
2824
257k
  if (Z_TYPE(dim_node->u.constant) == IS_STRING) {
2825
233k
    zend_ulong index;
2826
2827
233k
    if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) {
2828
      /* For numeric indexes we also keep the original value to use by ArrayAccess
2829
       * See bug #63217
2830
       */
2831
81.8k
      int c = zend_add_literal(&dim_node->u.constant);
2832
81.8k
      ZEND_ASSERT(opline->op2.constant + 1 == c);
2833
81.8k
      ZVAL_LONG(CT_CONSTANT(opline->op2), index);
2834
81.8k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE;
2835
81.8k
      return;
2836
81.8k
    }
2837
233k
  }
2838
257k
}
2839
/* }}} */
2840
2841
static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
2842
115k
{
2843
115k
  if (class_node->op_type == IS_CONST) {
2844
94.9k
    opline->op1_type = IS_CONST;
2845
94.9k
    opline->op1.constant = zend_add_class_name_literal(
2846
94.9k
      Z_STR(class_node->u.constant));
2847
94.9k
  } else {
2848
21.0k
    SET_NODE(opline->op1, class_node);
2849
21.0k
  }
2850
115k
}
2851
/* }}} */
2852
2853
static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */
2854
128k
{
2855
128k
  uint32_t fetch_type;
2856
2857
128k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2858
20.3k
    znode name_node;
2859
2860
20.3k
    zend_compile_expr(&name_node, name_ast);
2861
2862
20.3k
    if (name_node.op_type == IS_CONST) {
2863
594
      zend_string *name;
2864
2865
594
      if (Z_TYPE(name_node.u.constant) != IS_STRING) {
2866
6
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
2867
6
      }
2868
2869
588
      name = Z_STR(name_node.u.constant);
2870
588
      fetch_type = zend_get_class_fetch_type(name);
2871
2872
588
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
2873
429
        result->op_type = IS_CONST;
2874
429
        ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ));
2875
429
      } else {
2876
159
        zend_ensure_valid_class_fetch_type(fetch_type);
2877
159
        result->op_type = IS_UNUSED;
2878
159
        result->u.op.num = fetch_type | fetch_flags;
2879
159
      }
2880
2881
588
      zend_string_release_ex(name, 0);
2882
19.8k
    } else {
2883
19.8k
      zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
2884
19.8k
      opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags;
2885
19.8k
    }
2886
20.3k
    return;
2887
20.3k
  }
2888
2889
  /* Fully qualified names are always default refs */
2890
108k
  if (name_ast->attr == ZEND_NAME_FQ) {
2891
3.85k
    result->op_type = IS_CONST;
2892
3.85k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2893
3.85k
    return;
2894
3.85k
  }
2895
2896
104k
  fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
2897
104k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
2898
99.8k
    result->op_type = IS_CONST;
2899
99.8k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2900
99.8k
  } else {
2901
4.79k
    zend_ensure_valid_class_fetch_type(fetch_type);
2902
4.79k
    result->op_type = IS_UNUSED;
2903
4.79k
    result->u.op.num = fetch_type | fetch_flags;
2904
4.79k
  }
2905
104k
}
2906
/* }}} */
2907
2908
static zend_result zend_try_compile_cv(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
2909
1.14M
{
2910
1.14M
  zend_ast *name_ast = ast->child[0];
2911
1.14M
  if (name_ast->kind == ZEND_AST_ZVAL) {
2912
1.07M
    zval *zv = zend_ast_get_zval(name_ast);
2913
1.07M
    zend_string *name;
2914
2915
1.07M
    if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
2916
1.02M
      name = zval_make_interned_string(zv);
2917
1.02M
    } else {
2918
56.9k
      name = zend_new_interned_string(zval_get_string_func(zv));
2919
56.9k
    }
2920
2921
1.07M
    if (zend_is_auto_global(name)) {
2922
654
      return FAILURE;
2923
654
    }
2924
2925
1.07M
    if (!CG(context).has_assigned_to_http_response_header && zend_string_equals_literal(name, "http_response_header")) {
2926
193
      if (type == BP_VAR_R) {
2927
124
        zend_error(E_DEPRECATED,
2928
124
          "The predefined locally scoped $http_response_header variable is deprecated,"
2929
124
          " call http_get_last_response_headers() instead");
2930
124
      } else if (type == BP_VAR_W) {
2931
11
        CG(context).has_assigned_to_http_response_header = true;
2932
11
      }
2933
193
    }
2934
2935
1.07M
    result->op_type = IS_CV;
2936
1.07M
    result->u.op.var = lookup_cv(name);
2937
2938
1.07M
    if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) {
2939
56.9k
      zend_string_release_ex(name, 0);
2940
56.9k
    }
2941
2942
1.07M
    return SUCCESS;
2943
1.07M
  }
2944
2945
68.3k
  return FAILURE;
2946
1.14M
}
2947
/* }}} */
2948
2949
static zend_op *zend_compile_simple_var_no_cv(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2950
90.3k
{
2951
90.3k
  zend_ast *name_ast = ast->child[0];
2952
90.3k
  znode name_node;
2953
90.3k
  zend_op *opline;
2954
2955
90.3k
  zend_compile_expr(&name_node, name_ast);
2956
90.3k
  if (name_node.op_type == IS_CONST) {
2957
23.7k
    convert_to_string(&name_node.u.constant);
2958
23.7k
  }
2959
2960
90.3k
  if (delayed) {
2961
7.05k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2962
83.3k
  } else {
2963
83.3k
    opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2964
83.3k
  }
2965
2966
90.3k
  if (name_node.op_type == IS_CONST &&
2967
23.7k
      zend_is_auto_global(Z_STR(name_node.u.constant))) {
2968
2969
634
    opline->extended_value = ZEND_FETCH_GLOBAL;
2970
89.7k
  } else {
2971
    // TODO: Have a test case for this?
2972
89.7k
    if (name_node.op_type == IS_CONST
2973
23.0k
      && type == BP_VAR_R
2974
22.7k
      && zend_string_equals_literal(Z_STR(name_node.u.constant), "http_response_header")) {
2975
130
      zend_error(E_DEPRECATED,
2976
130
        "The predefined locally scoped $http_response_header variable is deprecated,"
2977
130
        " call http_get_last_response_headers() instead");
2978
130
    }
2979
89.7k
    opline->extended_value = ZEND_FETCH_LOCAL;
2980
89.7k
  }
2981
2982
90.3k
  zend_adjust_for_fetch_type(opline, result, type);
2983
90.3k
  return opline;
2984
90.3k
}
2985
/* }}} */
2986
2987
static bool is_this_fetch(const zend_ast *ast) /* {{{ */
2988
1.75M
{
2989
1.75M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2990
1.56M
    const zval *name = zend_ast_get_zval(ast->child[0]);
2991
1.56M
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals(Z_STR_P(name), ZSTR_KNOWN(ZEND_STR_THIS));
2992
1.56M
  }
2993
2994
198k
  return 0;
2995
1.75M
}
2996
/* }}} */
2997
2998
static bool is_globals_fetch(const zend_ast *ast)
2999
4.09M
{
3000
4.09M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
3001
1.69M
    const zval *name = zend_ast_get_zval(ast->child[0]);
3002
1.69M
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "GLOBALS");
3003
1.69M
  }
3004
3005
2.39M
  return 0;
3006
4.09M
}
3007
3008
static bool is_global_var_fetch(const zend_ast *ast)
3009
520k
{
3010
520k
  return ast->kind == ZEND_AST_DIM && is_globals_fetch(ast->child[0]);
3011
520k
}
3012
3013
static bool this_guaranteed_exists(void) /* {{{ */
3014
18.3k
{
3015
18.3k
  const zend_oparray_context *ctx = &CG(context);
3016
78.3k
  while (ctx) {
3017
    /* Instance methods always have a $this.
3018
     * This also includes closures that have a scope and use $this. */
3019
78.3k
    const zend_op_array *op_array = ctx->op_array;
3020
78.3k
    if (op_array->fn_flags & ZEND_ACC_STATIC) {
3021
34
      return false;
3022
78.3k
    } else if (op_array->scope) {
3023
10.1k
      return true;
3024
68.1k
    } else if (!(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
3025
8.10k
      return false;
3026
8.10k
    }
3027
60.0k
    ctx = ctx->prev;
3028
60.0k
  }
3029
0
  return false;
3030
18.3k
}
3031
/* }}} */
3032
3033
static zend_op *zend_compile_simple_var(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
3034
1.11M
{
3035
1.11M
  if (is_this_fetch(ast)) {
3036
6.36k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL);
3037
6.36k
    if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
3038
6.26k
      opline->result_type = IS_TMP_VAR;
3039
6.26k
      result->op_type = IS_TMP_VAR;
3040
6.26k
    }
3041
6.36k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3042
6.36k
    return opline;
3043
1.11M
  } else if (is_globals_fetch(ast)) {
3044
1.88k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_GLOBALS, NULL, NULL);
3045
1.88k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3046
1.71k
      opline->result_type = IS_TMP_VAR;
3047
1.71k
      result->op_type = IS_TMP_VAR;
3048
1.71k
    }
3049
1.88k
    return opline;
3050
1.10M
  } else if (zend_try_compile_cv(result, ast, type) == FAILURE) {
3051
67.2k
    return zend_compile_simple_var_no_cv(result, ast, type, delayed);
3052
67.2k
  }
3053
1.04M
  return NULL;
3054
1.11M
}
3055
/* }}} */
3056
3057
static void zend_separate_if_call_and_write(znode *node, const zend_ast *ast, uint32_t type) /* {{{ */
3058
499k
{
3059
499k
  if (type != BP_VAR_R
3060
259k
   && type != BP_VAR_IS
3061
   /* Whether a FUNC_ARG is R may only be determined at runtime. */
3062
197k
   && type != BP_VAR_FUNC_ARG
3063
92.1k
   && zend_is_call(ast)) {
3064
16.3k
    if (node->op_type == IS_VAR) {
3065
16.3k
      zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
3066
16.3k
      opline->result_type = IS_VAR;
3067
16.3k
      opline->result.var = opline->op1.var;
3068
16.3k
    } else {
3069
13
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3070
13
    }
3071
16.3k
  }
3072
499k
}
3073
/* }}} */
3074
3075
static inline void zend_emit_assign_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3076
4.92k
{
3077
4.92k
  znode dummy_node;
3078
4.92k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
3079
4.92k
    zend_ast_create_znode(value_node));
3080
4.92k
  zend_compile_expr(&dummy_node, assign_ast);
3081
4.92k
  zend_do_free(&dummy_node);
3082
4.92k
}
3083
/* }}} */
3084
3085
static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
3086
384k
{
3087
384k
  zend_ast *var_ast = ast->child[0];
3088
384k
  zend_ast *dim_ast = ast->child[1];
3089
384k
  zend_op *opline;
3090
3091
384k
  znode var_node, dim_node;
3092
3093
384k
  if (is_globals_fetch(var_ast)) {
3094
1.83k
    if (dim_ast == NULL) {
3095
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot append to $GLOBALS");
3096
7
    }
3097
3098
1.82k
    zend_compile_expr(&dim_node, dim_ast);
3099
1.82k
    if (dim_node.op_type == IS_CONST) {
3100
1.41k
      convert_to_string(&dim_node.u.constant);
3101
1.41k
    }
3102
3103
1.82k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &dim_node, NULL);
3104
1.82k
    opline->extended_value = ZEND_FETCH_GLOBAL;
3105
1.82k
    zend_adjust_for_fetch_type(opline, result, type);
3106
1.82k
    return opline;
3107
382k
  } else {
3108
382k
    zend_short_circuiting_mark_inner(var_ast);
3109
382k
    opline = zend_delayed_compile_var(&var_node, var_ast, type, false);
3110
382k
    if (opline) {
3111
211k
      if (type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) {
3112
2.76k
        opline->extended_value |= ZEND_FETCH_DIM_WRITE;
3113
208k
      } else if (opline->opcode == ZEND_FETCH_DIM_W
3114
175k
          || opline->opcode == ZEND_FETCH_DIM_RW
3115
173k
          || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3116
123k
          || opline->opcode == ZEND_FETCH_DIM_UNSET) {
3117
85.8k
        opline->extended_value = ZEND_FETCH_DIM_DIM;
3118
85.8k
      }
3119
211k
    }
3120
382k
  }
3121
3122
382k
  zend_separate_if_call_and_write(&var_node, var_ast, type);
3123
3124
382k
  if (dim_ast == NULL) {
3125
11.5k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3126
191
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
3127
191
    }
3128
11.3k
    if (type == BP_VAR_UNSET) {
3129
8
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
3130
8
    }
3131
11.3k
    dim_node.op_type = IS_UNUSED;
3132
371k
  } else {
3133
371k
    zend_compile_expr(&dim_node, dim_ast);
3134
371k
  }
3135
3136
382k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
3137
382k
  zend_adjust_for_fetch_type(opline, result, type);
3138
382k
  if (by_ref) {
3139
55.2k
    opline->extended_value = ZEND_FETCH_DIM_REF;
3140
55.2k
  }
3141
3142
382k
  if (dim_node.op_type == IS_CONST) {
3143
252k
    zend_handle_numeric_dim(opline, &dim_node);
3144
252k
  }
3145
382k
  return opline;
3146
382k
}
3147
3148
static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3149
165k
{
3150
165k
  uint32_t offset = zend_delayed_compile_begin();
3151
165k
  zend_delayed_compile_dim(result, ast, type, by_ref);
3152
165k
  return zend_delayed_compile_end(offset);
3153
165k
}
3154
/* }}} */
3155
3156
static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3157
132k
{
3158
132k
  zend_ast *obj_ast = ast->child[0];
3159
132k
  zend_ast *prop_ast = ast->child[1];
3160
3161
132k
  znode obj_node, prop_node;
3162
132k
  zend_op *opline;
3163
132k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_PROP;
3164
3165
132k
  if (is_this_fetch(obj_ast)) {
3166
16.8k
    if (this_guaranteed_exists()) {
3167
8.82k
      obj_node.op_type = IS_UNUSED;
3168
8.82k
    } else {
3169
8.05k
      opline = zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
3170
8.05k
      if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
3171
8.00k
        opline->result_type = IS_TMP_VAR;
3172
8.00k
        obj_node.op_type = IS_TMP_VAR;
3173
8.00k
      }
3174
8.05k
    }
3175
16.8k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3176
3177
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
3178
     * check for a nullsafe access. */
3179
115k
  } else {
3180
115k
    zend_short_circuiting_mark_inner(obj_ast);
3181
115k
    opline = zend_delayed_compile_var(&obj_node, obj_ast, type, false);
3182
115k
    if (opline && (opline->opcode == ZEND_FETCH_DIM_W
3183
19.9k
        || opline->opcode == ZEND_FETCH_DIM_RW
3184
19.6k
        || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3185
19.5k
        || opline->opcode == ZEND_FETCH_DIM_UNSET)) {
3186
1.19k
      opline->extended_value = ZEND_FETCH_DIM_OBJ;
3187
1.19k
    }
3188
3189
115k
    zend_separate_if_call_and_write(&obj_node, obj_ast, type);
3190
115k
    if (nullsafe) {
3191
63.6k
      if (obj_node.op_type == IS_TMP_VAR) {
3192
        /* Flush delayed oplines */
3193
32.2k
        zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
3194
32.2k
        uint32_t var = obj_node.u.op.var;
3195
32.2k
        uint32_t count = zend_stack_count(&CG(delayed_oplines_stack));
3196
32.2k
        uint32_t i = count;
3197
3198
327k
        while (i > 0 && oplines[i-1].result_type == IS_TMP_VAR && oplines[i-1].result.var == var) {
3199
296k
          i--;
3200
296k
          if (oplines[i].op1_type == IS_TMP_VAR) {
3201
295k
            var = oplines[i].op1.var;
3202
295k
          } else {
3203
1.24k
            break;
3204
1.24k
          }
3205
296k
        }
3206
328k
        for (; i < count; ++i) {
3207
296k
          if (oplines[i].opcode != ZEND_NOP) {
3208
8.86k
            opline = get_next_op();
3209
8.86k
            memcpy(opline, &oplines[i], sizeof(zend_op));
3210
8.86k
            oplines[i].opcode = ZEND_NOP;
3211
8.86k
            oplines[i].extended_value = opline - CG(active_op_array)->opcodes;
3212
8.86k
          }
3213
296k
        }
3214
32.2k
      }
3215
63.6k
      zend_emit_jmp_null(&obj_node, type);
3216
63.6k
    }
3217
115k
  }
3218
3219
132k
  zend_compile_expr(&prop_node, prop_ast);
3220
3221
132k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
3222
132k
  if (opline->op2_type == IS_CONST) {
3223
129k
    convert_to_string(CT_CONSTANT(opline->op2));
3224
129k
    zend_string_hash_val(Z_STR_P(CT_CONSTANT(opline->op2)));
3225
129k
    opline->extended_value = zend_alloc_cache_slots(3);
3226
129k
  }
3227
3228
132k
  zend_adjust_for_fetch_type(opline, result, type);
3229
3230
132k
  return opline;
3231
132k
}
3232
/* }}} */
3233
3234
static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3235
98.4k
{
3236
98.4k
  uint32_t offset = zend_delayed_compile_begin();
3237
98.4k
  zend_op *opline = zend_delayed_compile_prop(result, ast, type);
3238
98.4k
  if (by_ref) { /* shared with cache_slot */
3239
2.91k
    opline->extended_value |= ZEND_FETCH_REF;
3240
2.91k
  }
3241
98.4k
  return zend_delayed_compile_end(offset);
3242
98.4k
}
3243
/* }}} */
3244
3245
static zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref, bool delayed) /* {{{ */
3246
13.5k
{
3247
13.5k
  zend_ast *class_ast = ast->child[0];
3248
13.5k
  zend_ast *prop_ast = ast->child[1];
3249
3250
13.5k
  znode class_node, prop_node;
3251
13.5k
  zend_op *opline;
3252
3253
13.5k
  zend_short_circuiting_mark_inner(class_ast);
3254
13.5k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
3255
3256
13.5k
  zend_compile_expr(&prop_node, prop_ast);
3257
3258
13.5k
  if (delayed) {
3259
5.14k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3260
8.40k
  } else {
3261
8.40k
    opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3262
8.40k
  }
3263
13.5k
  if (opline->op1_type == IS_CONST) {
3264
11.7k
    convert_to_string(CT_CONSTANT(opline->op1));
3265
11.7k
    opline->extended_value = zend_alloc_cache_slots(3);
3266
11.7k
  }
3267
13.5k
  if (class_node.op_type == IS_CONST) {
3268
8.44k
    opline->op2_type = IS_CONST;
3269
8.44k
    opline->op2.constant = zend_add_class_name_literal(
3270
8.44k
      Z_STR(class_node.u.constant));
3271
8.44k
    if (opline->op1_type != IS_CONST) {
3272
1.44k
      opline->extended_value = zend_alloc_cache_slot();
3273
1.44k
    }
3274
8.44k
  } else {
3275
5.11k
    SET_NODE(opline->op2, &class_node);
3276
5.11k
  }
3277
3278
13.5k
  if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */
3279
994
    opline->extended_value |= ZEND_FETCH_REF;
3280
994
  }
3281
3282
13.5k
  zend_adjust_for_fetch_type(opline, result, type);
3283
13.5k
  return opline;
3284
13.5k
}
3285
/* }}} */
3286
3287
5.06k
static void zend_verify_list_assign_target(const zend_ast *var_ast, zend_ast_attr array_style) /* {{{ */ {
3288
5.06k
  if (var_ast->kind == ZEND_AST_ARRAY) {
3289
283
    if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) {
3290
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead");
3291
5
    }
3292
278
    if (array_style != var_ast->attr) {
3293
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()");
3294
6
    }
3295
4.78k
  } else if (!zend_can_write_to_variable(var_ast)) {
3296
84
    zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values");
3297
84
  }
3298
5.06k
}
3299
/* }}} */
3300
3301
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node);
3302
3303
/* Propagate refs used on leaf elements to the surrounding list() structures. */
3304
3.71k
static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
3305
3.71k
  const zend_ast_list *list = zend_ast_get_list(ast);
3306
3.71k
  bool has_refs = false;
3307
3.71k
  uint32_t i;
3308
3309
18.2k
  for (i = 0; i < list->children; ++i) {
3310
14.5k
    zend_ast *elem_ast = list->child[i];
3311
3312
14.5k
    if (elem_ast) {
3313
5.51k
      zend_ast *var_ast = elem_ast->child[0];
3314
5.51k
      if (var_ast->kind == ZEND_AST_ARRAY) {
3315
283
        elem_ast->attr = zend_propagate_list_refs(var_ast);
3316
283
      }
3317
5.51k
      has_refs |= elem_ast->attr;
3318
5.51k
    }
3319
14.5k
  }
3320
3321
3.71k
  return has_refs;
3322
3.71k
}
3323
/* }}} */
3324
3325
static bool list_is_keyed(const zend_ast_list *list)
3326
3.25k
{
3327
3.98k
  for (uint32_t i = 0; i < list->children; i++) {
3328
3.95k
    const zend_ast *child = list->child[i];
3329
3.95k
    if (child) {
3330
3.22k
      return child->kind == ZEND_AST_ARRAY_ELEM && child->child[1] != NULL;
3331
3.22k
    }
3332
3.95k
  }
3333
29
  return false;
3334
3.25k
}
3335
3336
static void zend_compile_list_assign(
3337
    znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style, uint32_t type) /* {{{ */
3338
3.25k
{
3339
3.25k
  zend_ast_list *list = zend_ast_get_list(ast);
3340
3.25k
  uint32_t i;
3341
3.25k
  bool has_elems = false;
3342
3.25k
  bool is_keyed = list_is_keyed(list);
3343
3344
3.25k
  if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
3345
124
    zval_make_interned_string(&expr_node->u.constant);
3346
124
  }
3347
3348
9.31k
  for (i = 0; i < list->children; ++i) {
3349
6.06k
    zend_ast *elem_ast = list->child[i];
3350
6.06k
    zend_ast *var_ast, *key_ast;
3351
6.06k
    znode fetch_result, dim_node;
3352
6.06k
    zend_op *opline;
3353
3354
6.06k
    if (elem_ast == NULL) {
3355
974
      if (is_keyed) {
3356
14
        zend_error(E_COMPILE_ERROR,
3357
14
          "Cannot use empty array entries in keyed array assignment");
3358
960
      } else {
3359
960
        continue;
3360
960
      }
3361
974
    }
3362
3363
5.10k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
3364
9
      zend_error(E_COMPILE_ERROR,
3365
9
          "Spread operator is not supported in assignments");
3366
9
    }
3367
3368
5.10k
    var_ast = elem_ast->child[0];
3369
5.10k
    key_ast = elem_ast->child[1];
3370
5.10k
    has_elems = true;
3371
3372
5.10k
    if (is_keyed) {
3373
889
      if (key_ast == NULL) {
3374
5
        zend_error(E_COMPILE_ERROR,
3375
5
          "Cannot mix keyed and unkeyed array entries in assignments");
3376
5
      }
3377
3378
889
      zend_compile_expr(&dim_node, key_ast);
3379
4.21k
    } else {
3380
4.21k
      if (key_ast != NULL) {
3381
7
        zend_error(E_COMPILE_ERROR,
3382
7
          "Cannot mix keyed and unkeyed array entries in assignments");
3383
7
      }
3384
3385
4.21k
      dim_node.op_type = IS_CONST;
3386
4.21k
      ZVAL_LONG(&dim_node.u.constant, i);
3387
4.21k
    }
3388
3389
5.10k
    if (expr_node->op_type == IS_CONST) {
3390
546
      Z_TRY_ADDREF(expr_node->u.constant);
3391
546
    }
3392
3393
5.10k
    zend_verify_list_assign_target(var_ast, array_style);
3394
3395
5.10k
    opline = zend_emit_op(&fetch_result,
3396
5.10k
      elem_ast->attr ? (expr_node->op_type == IS_CV ? ZEND_FETCH_DIM_W : ZEND_FETCH_LIST_W) : ZEND_FETCH_LIST_R, expr_node, &dim_node);
3397
5.10k
    if (opline->opcode == ZEND_FETCH_LIST_R) {
3398
3.49k
      opline->result_type = IS_TMP_VAR;
3399
3.49k
      fetch_result.op_type = IS_TMP_VAR;
3400
3.49k
    }
3401
3402
5.10k
    if (dim_node.op_type == IS_CONST) {
3403
4.82k
      zend_handle_numeric_dim(opline, &dim_node);
3404
4.82k
    }
3405
3406
5.10k
    if (elem_ast->attr) {
3407
1.47k
      zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL);
3408
1.47k
    }
3409
5.10k
    if (var_ast->kind == ZEND_AST_ARRAY) {
3410
272
      zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr, type);
3411
4.83k
    } else if (elem_ast->attr) {
3412
1.43k
      zend_emit_assign_ref_znode(var_ast, &fetch_result);
3413
3.40k
    } else {
3414
3.40k
      zend_emit_assign_znode(var_ast, &fetch_result);
3415
3.40k
    }
3416
5.10k
  }
3417
3418
3.25k
  if (has_elems == 0) {
3419
29
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
3420
29
  }
3421
3422
3.22k
  if (result) {
3423
139
    if ((type == BP_VAR_R || type == BP_VAR_IS) && expr_node->op_type == IS_VAR) {
3424
      /* Deref. */
3425
17
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, expr_node, NULL);
3426
122
    } else {
3427
122
      *result = *expr_node;
3428
122
    }
3429
3.08k
  } else {
3430
3.08k
    zend_do_free(expr_node);
3431
3.08k
  }
3432
3.22k
}
3433
/* }}} */
3434
3435
static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
3436
526k
{
3437
526k
  if (ast->kind == ZEND_AST_CALL || ast->kind == ZEND_AST_PIPE) {
3438
75
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
3439
75
  }
3440
526k
  if (
3441
526k
    ast->kind == ZEND_AST_METHOD_CALL
3442
526k
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
3443
526k
    || ast->kind == ZEND_AST_STATIC_CALL
3444
526k
  ) {
3445
15
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
3446
15
  }
3447
526k
  if (zend_ast_is_short_circuited(ast)) {
3448
23
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use nullsafe operator in write context");
3449
23
  }
3450
526k
  if (is_globals_fetch(ast)) {
3451
31
    zend_error_noreturn(E_COMPILE_ERROR,
3452
31
      "$GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax");
3453
31
  }
3454
526k
}
3455
/* }}} */
3456
3457
/* Detects $a... = $a pattern */
3458
static bool zend_is_assign_to_self(const zend_ast *var_ast, const zend_ast *expr_ast) /* {{{ */
3459
34.3k
{
3460
34.3k
  if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
3461
29.8k
    return 0;
3462
29.8k
  }
3463
3464
11.2k
  while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
3465
6.67k
    var_ast = var_ast->child[0];
3466
6.67k
  }
3467
3468
4.56k
  if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
3469
542
    return 0;
3470
542
  }
3471
3472
4.01k
  {
3473
4.01k
    zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
3474
4.01k
    zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
3475
4.01k
    bool result = zend_string_equals(name1, name2);
3476
4.01k
    zend_string_release_ex(name1, 0);
3477
4.01k
    zend_string_release_ex(name2, 0);
3478
4.01k
    return result;
3479
4.56k
  }
3480
4.56k
}
3481
/* }}} */
3482
3483
static void zend_compile_expr_with_potential_assign_to_self(
3484
34.3k
    znode *expr_node, zend_ast *expr_ast, const zend_ast *var_ast) {
3485
34.3k
  if (zend_is_assign_to_self(var_ast, expr_ast) && !is_this_fetch(expr_ast)) {
3486
    /* $a[0] = $a should evaluate the right $a first */
3487
711
    znode cv_node;
3488
3489
711
    if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3490
68
      zend_compile_simple_var_no_cv(expr_node, expr_ast, BP_VAR_R, false);
3491
643
    } else {
3492
643
      zend_emit_op_tmp(expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3493
643
    }
3494
33.6k
  } else {
3495
33.6k
    zend_compile_expr(expr_node, expr_ast);
3496
33.6k
  }
3497
34.3k
}
3498
3499
static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type) /* {{{ */
3500
382k
{
3501
382k
  zend_ast *var_ast = ast->child[0];
3502
382k
  zend_ast *expr_ast = ast->child[1];
3503
3504
382k
  znode var_node, expr_node;
3505
382k
  zend_op *opline;
3506
382k
  uint32_t offset;
3507
382k
  if (is_this_fetch(var_ast)) {
3508
9
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3509
9
  }
3510
3511
382k
  zend_ensure_writable_variable(var_ast);
3512
3513
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3514
382k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3515
382k
  switch (kind) {
3516
356k
    case ZEND_AST_VAR:
3517
356k
      offset = zend_delayed_compile_begin();
3518
356k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, false);
3519
356k
      zend_compile_expr(&expr_node, expr_ast);
3520
356k
      zend_delayed_compile_end(offset);
3521
356k
      CG(zend_lineno) = zend_ast_get_lineno(var_ast);
3522
356k
      zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node);
3523
356k
      return;
3524
2.27k
    case ZEND_AST_STATIC_PROP:
3525
2.27k
      offset = zend_delayed_compile_begin();
3526
2.27k
      zend_delayed_compile_var(result, var_ast, BP_VAR_W, false);
3527
2.27k
      zend_compile_expr(&expr_node, expr_ast);
3528
3529
2.27k
      opline = zend_delayed_compile_end(offset);
3530
2.27k
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
3531
2.27k
      opline->result_type = IS_TMP_VAR;
3532
2.27k
      result->op_type = IS_TMP_VAR;
3533
3534
2.27k
      zend_emit_op_data(&expr_node);
3535
2.27k
      return;
3536
10.7k
    case ZEND_AST_DIM:
3537
10.7k
      offset = zend_delayed_compile_begin();
3538
10.7k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_W, /* by_ref */ false);
3539
10.7k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3540
3541
10.7k
      opline = zend_delayed_compile_end(offset);
3542
10.7k
      opline->opcode = ZEND_ASSIGN_DIM;
3543
10.7k
      opline->result_type = IS_TMP_VAR;
3544
10.7k
      result->op_type = IS_TMP_VAR;
3545
3546
10.7k
      opline = zend_emit_op_data(&expr_node);
3547
10.7k
      return;
3548
10.2k
    case ZEND_AST_PROP:
3549
10.2k
    case ZEND_AST_NULLSAFE_PROP:
3550
10.2k
      offset = zend_delayed_compile_begin();
3551
10.2k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
3552
10.2k
      zend_compile_expr(&expr_node, expr_ast);
3553
3554
10.2k
      opline = zend_delayed_compile_end(offset);
3555
10.2k
      opline->opcode = ZEND_ASSIGN_OBJ;
3556
10.2k
      opline->result_type = IS_TMP_VAR;
3557
10.2k
      result->op_type = IS_TMP_VAR;
3558
3559
10.2k
      zend_emit_op_data(&expr_node);
3560
10.2k
      return;
3561
3.05k
    case ZEND_AST_ARRAY:
3562
3.05k
      if (zend_propagate_list_refs(var_ast)) {
3563
1.28k
        if (!zend_is_variable_or_call(expr_ast)) {
3564
19
          zend_error_noreturn(E_COMPILE_ERROR,
3565
19
            "Cannot assign reference to non referenceable value");
3566
1.26k
        } else {
3567
1.26k
          zend_assert_not_short_circuited(expr_ast);
3568
1.26k
        }
3569
3570
1.26k
        zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
3571
        /* MAKE_REF is usually not necessary for CVs. However, if there are
3572
         * self-assignments, this forces the RHS to evaluate first. */
3573
1.26k
        zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
3574
1.77k
      } else {
3575
1.77k
        if (expr_ast->kind == ZEND_AST_VAR) {
3576
          /* list($a, $b) = $a should evaluate the right $a first */
3577
467
          znode cv_node;
3578
3579
467
          if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3580
211
            zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, false);
3581
256
          } else {
3582
256
            zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3583
256
          }
3584
1.30k
        } else {
3585
1.30k
          zend_compile_expr(&expr_node, expr_ast);
3586
1.30k
        }
3587
1.77k
      }
3588
3589
3.04k
      zend_compile_list_assign(!stmt ? result : NULL, var_ast, &expr_node, var_ast->attr, type);
3590
3.04k
      if (stmt) {
3591
2.31k
        result->op_type = IS_UNUSED;
3592
2.31k
      }
3593
3.04k
      return;
3594
0
    EMPTY_SWITCH_DEFAULT_CASE();
3595
382k
  }
3596
382k
}
3597
/* }}} */
3598
3599
static void zend_compile_assign_ref(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3600
7.04k
{
3601
7.04k
  zend_ast *target_ast = ast->child[0];
3602
7.04k
  zend_ast *source_ast = ast->child[1];
3603
3604
7.04k
  znode target_node, source_node;
3605
7.04k
  zend_op *opline;
3606
7.04k
  uint32_t offset, flags;
3607
3608
7.04k
  if (is_this_fetch(target_ast)) {
3609
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3610
5
  }
3611
7.03k
  zend_ensure_writable_variable(target_ast);
3612
7.03k
  zend_assert_not_short_circuited(source_ast);
3613
7.03k
  if (is_globals_fetch(source_ast)) {
3614
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot acquire reference to $GLOBALS");
3615
5
  }
3616
3617
7.03k
  offset = zend_delayed_compile_begin();
3618
7.03k
  zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, true);
3619
7.03k
  zend_compile_var(&source_node, source_ast, BP_VAR_W, true);
3620
3621
7.03k
  if ((target_ast->kind != ZEND_AST_VAR
3622
4.87k
    || target_ast->child[0]->kind != ZEND_AST_ZVAL)
3623
2.82k
   && source_ast->kind != ZEND_AST_ZNODE
3624
1.98k
   && source_node.op_type != IS_CV) {
3625
    /* Both LHS and RHS expressions may modify the same data structure,
3626
     * and the modification during RHS evaluation may dangle the pointer
3627
     * to the result of the LHS evaluation.
3628
     * Use MAKE_REF instruction to replace direct pointer with REFERENCE.
3629
     * See: Bug #71539
3630
     */
3631
1.03k
    zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL);
3632
1.03k
  }
3633
3634
7.03k
  opline = zend_delayed_compile_end(offset);
3635
3636
7.03k
  if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
3637
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3638
5
  }
3639
3640
7.02k
  flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0;
3641
3642
7.02k
  if (opline && opline->opcode == ZEND_FETCH_OBJ_W) {
3643
851
    opline->opcode = ZEND_ASSIGN_OBJ_REF;
3644
851
    opline->extended_value &= ~ZEND_FETCH_REF;
3645
851
    opline->extended_value |= flags;
3646
851
    if (result) {
3647
55
      *result = target_node;
3648
796
    } else {
3649
796
      SET_UNUSED(opline->result);
3650
796
    }
3651
851
    zend_emit_op_data(&source_node);
3652
6.17k
  } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) {
3653
221
    opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF;
3654
221
    opline->extended_value &= ~ZEND_FETCH_REF;
3655
221
    opline->extended_value |= flags;
3656
221
    if (result) {
3657
35
      *result = target_node;
3658
186
    } else {
3659
186
      SET_UNUSED(opline->result);
3660
186
    }
3661
221
    zend_emit_op_data(&source_node);
3662
5.95k
  } else {
3663
5.95k
    opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
3664
5.95k
    opline->extended_value = flags;
3665
5.95k
  }
3666
3667
7.02k
  if (result && (type == BP_VAR_R || type == BP_VAR_IS)) {
3668
    /* Deref. */
3669
1.31k
    znode tmp_result = *result;
3670
1.31k
    zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &tmp_result, NULL);
3671
1.31k
  }
3672
7.02k
}
3673
/* }}} */
3674
3675
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3676
2.22k
{
3677
2.22k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
3678
2.22k
    zend_ast_create_znode(value_node));
3679
2.22k
  zend_compile_stmt(assign_ast);
3680
2.22k
}
3681
/* }}} */
3682
3683
static void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
3684
95.6k
{
3685
95.6k
  zend_ast *var_ast = ast->child[0];
3686
95.6k
  zend_ast *expr_ast = ast->child[1];
3687
95.6k
  uint32_t opcode = ast->attr;
3688
3689
95.6k
  znode var_node, expr_node;
3690
95.6k
  zend_op *opline;
3691
95.6k
  uint32_t offset, cache_slot;
3692
3693
95.6k
  zend_ensure_writable_variable(var_ast);
3694
3695
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3696
95.6k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3697
95.6k
  switch (kind) {
3698
92.1k
    case ZEND_AST_VAR:
3699
92.1k
      offset = zend_delayed_compile_begin();
3700
92.1k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, false);
3701
92.1k
      zend_compile_expr(&expr_node, expr_ast);
3702
92.1k
      zend_delayed_compile_end(offset);
3703
92.1k
      opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node);
3704
92.1k
      opline->extended_value = opcode;
3705
92.1k
      return;
3706
386
    case ZEND_AST_STATIC_PROP:
3707
386
      offset = zend_delayed_compile_begin();
3708
386
      zend_delayed_compile_var(result, var_ast, BP_VAR_RW, false);
3709
386
      zend_compile_expr(&expr_node, expr_ast);
3710
3711
386
      opline = zend_delayed_compile_end(offset);
3712
386
      cache_slot = opline->extended_value;
3713
386
      opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP;
3714
386
      opline->extended_value = opcode;
3715
386
      opline->result_type = IS_TMP_VAR;
3716
386
      result->op_type = IS_TMP_VAR;
3717
3718
386
      opline = zend_emit_op_data(&expr_node);
3719
386
      opline->extended_value = cache_slot;
3720
386
      return;
3721
2.15k
    case ZEND_AST_DIM:
3722
2.15k
      offset = zend_delayed_compile_begin();
3723
2.15k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_RW, /* by_ref */ false);
3724
2.15k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3725
3726
2.15k
      opline = zend_delayed_compile_end(offset);
3727
2.15k
      opline->opcode = ZEND_ASSIGN_DIM_OP;
3728
2.15k
      opline->extended_value = opcode;
3729
2.15k
      opline->result_type = IS_TMP_VAR;
3730
2.15k
      result->op_type = IS_TMP_VAR;
3731
3732
2.15k
      zend_emit_op_data(&expr_node);
3733
2.15k
      return;
3734
992
    case ZEND_AST_PROP:
3735
992
    case ZEND_AST_NULLSAFE_PROP:
3736
992
      offset = zend_delayed_compile_begin();
3737
992
      zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
3738
992
      zend_compile_expr(&expr_node, expr_ast);
3739
3740
992
      opline = zend_delayed_compile_end(offset);
3741
992
      cache_slot = opline->extended_value;
3742
992
      opline->opcode = ZEND_ASSIGN_OBJ_OP;
3743
992
      opline->extended_value = opcode;
3744
992
      opline->result_type = IS_TMP_VAR;
3745
992
      result->op_type = IS_TMP_VAR;
3746
3747
992
      opline = zend_emit_op_data(&expr_node);
3748
992
      opline->extended_value = cache_slot;
3749
992
      return;
3750
95.6k
    EMPTY_SWITCH_DEFAULT_CASE()
3751
95.6k
  }
3752
95.6k
}
3753
/* }}} */
3754
3755
4.87k
static uint32_t zend_get_arg_num(const zend_function *fn, const zend_string *arg_name) {
3756
  // TODO: Caching?
3757
17.6k
  for (uint32_t i = 0; i < fn->common.num_args; i++) {
3758
14.3k
    zend_arg_info *arg_info = &fn->op_array.arg_info[i];
3759
14.3k
    if (zend_string_equals(arg_info->name, arg_name)) {
3760
1.53k
      return i + 1;
3761
1.53k
    }
3762
14.3k
  }
3763
3764
  /* Either an invalid argument name, or collected into a variadic argument. */
3765
3.34k
  return (uint32_t) -1;
3766
4.87k
}
3767
3768
static uint32_t zend_compile_args(
3769
    zend_ast *ast, const zend_function *fbc, bool *may_have_extra_named_args) /* {{{ */
3770
1.56M
{
3771
1.56M
  const zend_ast_list *args = zend_ast_get_list(ast);
3772
1.56M
  uint32_t i;
3773
1.56M
  bool uses_arg_unpack = false;
3774
1.56M
  uint32_t arg_count = 0; /* number of arguments not including unpacks */
3775
3776
  /* Whether named arguments are used syntactically, to enforce language level limitations.
3777
   * May not actually use named argument passing. */
3778
1.56M
  bool uses_named_args = false;
3779
  /* Whether there may be any undef arguments due to the use of named arguments. */
3780
1.56M
  bool may_have_undef = false;
3781
  /* Whether there may be any extra named arguments collected into a variadic. */
3782
1.56M
  *may_have_extra_named_args = false;
3783
3784
3.56M
  for (i = 0; i < args->children; ++i) {
3785
2.00M
    zend_ast *arg = args->child[i];
3786
2.00M
    zend_string *arg_name = NULL;
3787
2.00M
    uint32_t arg_num = i + 1;
3788
3789
2.00M
    znode arg_node;
3790
2.00M
    zend_op *opline;
3791
2.00M
    uint8_t opcode;
3792
3793
2.00M
    if (arg->kind == ZEND_AST_UNPACK) {
3794
1.22k
      if (uses_named_args) {
3795
5
        zend_error_noreturn(E_COMPILE_ERROR,
3796
5
          "Cannot use argument unpacking after named arguments");
3797
5
      }
3798
3799
      /* Unpack may contain named arguments. */
3800
1.21k
      may_have_undef = true;
3801
1.21k
      if (!fbc || (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3802
859
        *may_have_extra_named_args = true;
3803
859
      }
3804
3805
1.21k
      uses_arg_unpack = true;
3806
1.21k
      fbc = NULL;
3807
3808
1.21k
      zend_compile_expr(&arg_node, arg->child[0]);
3809
1.21k
      opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
3810
1.21k
      opline->op2.num = arg_count;
3811
1.21k
      opline->result.var = EX_NUM_TO_VAR(arg_count - 1);
3812
3813
1.21k
      continue;
3814
1.22k
    }
3815
3816
1.99M
    if (arg->kind == ZEND_AST_NAMED_ARG) {
3817
15.8k
      uses_named_args = true;
3818
15.8k
      arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0]));
3819
15.8k
      arg = arg->child[1];
3820
3821
15.8k
      if (fbc && !uses_arg_unpack) {
3822
4.87k
        arg_num = zend_get_arg_num(fbc, arg_name);
3823
4.87k
        if (arg_num == arg_count + 1 && !may_have_undef) {
3824
          /* Using named arguments, but passing in order. */
3825
439
          arg_name = NULL;
3826
439
          arg_count++;
3827
4.44k
        } else {
3828
          // TODO: We could track which arguments were passed, even if out of order.
3829
4.44k
          may_have_undef = true;
3830
4.44k
          if (arg_num == (uint32_t) -1 && (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3831
230
            *may_have_extra_named_args = true;
3832
230
          }
3833
4.44k
        }
3834
10.9k
      } else {
3835
10.9k
        arg_num = (uint32_t) -1;
3836
10.9k
        may_have_undef = true;
3837
10.9k
        *may_have_extra_named_args = true;
3838
10.9k
      }
3839
1.98M
    } else {
3840
1.98M
      if (uses_arg_unpack) {
3841
11
        zend_error_noreturn(E_COMPILE_ERROR,
3842
11
          "Cannot use positional argument after argument unpacking");
3843
11
      }
3844
3845
1.98M
      if (uses_named_args) {
3846
37
        zend_error_noreturn(E_COMPILE_ERROR,
3847
37
          "Cannot use positional argument after named argument");
3848
37
      }
3849
3850
1.98M
      arg_count++;
3851
1.98M
    }
3852
3853
    /* Treat passing of $GLOBALS the same as passing a call.
3854
     * This will error at runtime if the argument is by-ref. */
3855
1.99M
    if (zend_is_call(arg) || is_globals_fetch(arg)) {
3856
82.7k
      uint32_t type = is_globals_fetch(arg) || (fbc && !ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num))
3857
82.7k
        ? BP_VAR_R : BP_VAR_FUNC_ARG;
3858
82.7k
      zend_compile_var(&arg_node, arg, type, /* by_ref */ false);
3859
82.7k
      if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
3860
        /* Function call was converted into builtin instruction */
3861
37.8k
        if (!fbc || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3862
1.55k
          opcode = ZEND_SEND_VAL_EX;
3863
36.2k
        } else {
3864
36.2k
          opcode = ZEND_SEND_VAL;
3865
36.2k
        }
3866
44.9k
      } else {
3867
44.9k
        if (fbc && arg_num != (uint32_t) -1) {
3868
595
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3869
542
            opcode = ZEND_SEND_VAR_NO_REF;
3870
542
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3871
            /* For IS_VAR operands, SEND_VAL will pass through the operand without
3872
             * dereferencing, so it will use a by-ref pass if the call returned by-ref
3873
             * and a by-value pass if it returned by-value. */
3874
53
            opcode = ZEND_SEND_VAL;
3875
53
          } else {
3876
0
            opcode = ZEND_SEND_VAR;
3877
0
          }
3878
44.3k
        } else {
3879
44.3k
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3880
44.3k
        }
3881
44.9k
      }
3882
1.91M
    } else if (zend_is_passable_by_ref(arg) && !zend_ast_is_short_circuited(arg)) {
3883
139k
      if (fbc && arg_num != (uint32_t) -1) {
3884
69.9k
        if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3885
2.21k
          zend_compile_var(&arg_node, arg, BP_VAR_W, true);
3886
2.21k
          opcode = ZEND_SEND_REF;
3887
67.7k
        } else {
3888
67.7k
          zend_compile_var(&arg_node, arg, BP_VAR_R, false);
3889
67.7k
          opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR;
3890
67.7k
        }
3891
69.9k
      } else {
3892
69.8k
        do {
3893
69.8k
          if (arg->kind == ZEND_AST_VAR) {
3894
14.5k
            CG(zend_lineno) = zend_ast_get_lineno(ast);
3895
14.5k
            if (is_this_fetch(arg)) {
3896
1.46k
              zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL);
3897
1.46k
              opcode = ZEND_SEND_VAR_EX;
3898
1.46k
              CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3899
1.46k
              break;
3900
13.1k
            } else if (zend_try_compile_cv(&arg_node, arg, BP_VAR_R) == SUCCESS) {
3901
12.8k
              opcode = ZEND_SEND_VAR_EX;
3902
12.8k
              break;
3903
12.8k
            }
3904
14.5k
          }
3905
55.6k
          opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL);
3906
55.6k
          if (arg_name) {
3907
2.40k
            opline->op2_type = IS_CONST;
3908
2.40k
            zend_string_addref(arg_name);
3909
2.40k
            opline->op2.constant = zend_add_literal_string(&arg_name);
3910
2.40k
            opline->result.num = zend_alloc_cache_slots(2);
3911
53.2k
          } else {
3912
53.2k
            opline->op2.num = arg_num;
3913
53.2k
          }
3914
55.6k
          zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, true);
3915
55.6k
          opcode = ZEND_SEND_FUNC_ARG;
3916
55.6k
        } while (0);
3917
69.8k
      }
3918
1.77M
    } else {
3919
1.77M
      zend_compile_expr(&arg_node, arg);
3920
1.77M
      if (arg_node.op_type == IS_VAR) {
3921
        /* pass ++$a or something similar */
3922
0
        if (fbc && arg_num != (uint32_t) -1) {
3923
0
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3924
0
            opcode = ZEND_SEND_VAR_NO_REF;
3925
0
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3926
0
            opcode = ZEND_SEND_VAL;
3927
0
          } else {
3928
0
            opcode = ZEND_SEND_VAR;
3929
0
          }
3930
0
        } else {
3931
0
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3932
0
        }
3933
1.77M
      } else if (arg_node.op_type == IS_CV) {
3934
0
        if (fbc && arg_num != (uint32_t) -1) {
3935
0
          if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3936
0
            opcode = ZEND_SEND_REF;
3937
0
          } else {
3938
0
            opcode = ZEND_SEND_VAR;
3939
0
          }
3940
0
        } else {
3941
0
          opcode = ZEND_SEND_VAR_EX;
3942
0
        }
3943
1.77M
      } else {
3944
        /* Delay "Only variables can be passed by reference" error to execution */
3945
1.77M
        if (fbc && arg_num != (uint32_t) -1 && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3946
99.5k
          opcode = ZEND_SEND_VAL;
3947
1.67M
        } else {
3948
1.67M
          opcode = ZEND_SEND_VAL_EX;
3949
1.67M
        }
3950
1.77M
      }
3951
1.77M
    }
3952
3953
1.99M
    opline = zend_emit_op(NULL, opcode, &arg_node, NULL);
3954
1.99M
    if (arg_name) {
3955
15.4k
      opline->op2_type = IS_CONST;
3956
15.4k
      zend_string_addref(arg_name);
3957
15.4k
      opline->op2.constant = zend_add_literal_string(&arg_name);
3958
15.4k
      opline->result.num = zend_alloc_cache_slots(2);
3959
1.98M
    } else {
3960
1.98M
      opline->op2.opline_num = arg_num;
3961
1.98M
      opline->result.var = EX_NUM_TO_VAR(arg_num - 1);
3962
1.98M
    }
3963
1.99M
  }
3964
3965
1.56M
  if (may_have_undef) {
3966
12.3k
    zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
3967
12.3k
  }
3968
3969
1.56M
  return arg_count;
3970
1.56M
}
3971
/* }}} */
3972
3973
ZEND_API uint8_t zend_get_call_op(const zend_op *init_op, const zend_function *fbc, bool result_used) /* {{{ */
3974
1.66M
{
3975
1.66M
  uint32_t no_discard = result_used ? 0 : ZEND_ACC_NODISCARD;
3976
3977
1.66M
  if (fbc && init_op->opcode != ZEND_NEW) {
3978
222k
    ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE));
3979
222k
    if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) {
3980
180k
      if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) {
3981
20.7k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3982
20.6k
          return ZEND_DO_ICALL;
3983
20.6k
        } else {
3984
85
          return ZEND_DO_FCALL_BY_NAME;
3985
85
        }
3986
20.7k
      }
3987
180k
    } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){
3988
41.7k
      if (zend_execute_ex == execute_ex) {
3989
20.4k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3990
20.2k
          return ZEND_DO_UCALL;
3991
20.2k
        } else {
3992
201
          return ZEND_DO_FCALL_BY_NAME;
3993
201
        }
3994
20.4k
      }
3995
41.7k
    }
3996
1.44M
  } else if (zend_execute_ex == execute_ex &&
3997
1.37M
             !zend_execute_internal &&
3998
1.33M
             (init_op->opcode == ZEND_INIT_FCALL_BY_NAME ||
3999
1.26M
              init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) {
4000
1.20M
    return ZEND_DO_FCALL_BY_NAME;
4001
1.20M
  }
4002
416k
  return ZEND_DO_FCALL;
4003
1.66M
}
4004
/* }}} */
4005
4006
static bool zend_compile_call_common(znode *result, zend_ast *args_ast, const zend_function *fbc, uint32_t lineno, uint32_t type) /* {{{ */
4007
1.57M
{
4008
1.57M
  zend_op *opline;
4009
1.57M
  uint32_t opnum_init = get_next_op_number() - 1;
4010
4011
1.57M
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
4012
2.67k
    opline = &CG(active_op_array)->opcodes[opnum_init];
4013
2.67k
    opline->extended_value = 0;
4014
    /* opcode array may be reallocated, so don't access opcode field after zend_emit_op_tmp(). */
4015
2.67k
    uint8_t opcode = opline->opcode;
4016
4017
2.67k
    if (opcode == ZEND_NEW) {
4018
16
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
4019
16
    }
4020
4021
2.65k
    zend_ast_list *args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args);
4022
2.65k
    if (args->children != 1 || args->child[0]->attr != ZEND_PLACEHOLDER_VARIADIC) {
4023
19
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot create a Closure for call expression with more than one argument, or non-variadic placeholders");
4024
19
    }
4025
4026
2.64k
    if (opcode == ZEND_INIT_FCALL) {
4027
482
      opline->op1.num = zend_vm_calc_used_stack(0, fbc);
4028
482
    }
4029
4030
2.64k
    zend_op *callable_convert_op = zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT, NULL, NULL);
4031
2.64k
    if (opcode == ZEND_INIT_FCALL
4032
2.15k
     || opcode == ZEND_INIT_FCALL_BY_NAME
4033
2.08k
     || opcode == ZEND_INIT_NS_FCALL_BY_NAME) {
4034
2.08k
      callable_convert_op->extended_value = zend_alloc_cache_slot();
4035
2.08k
    } else {
4036
556
      callable_convert_op->extended_value = (uint32_t)-1;
4037
556
    }
4038
2.64k
    return true;
4039
2.65k
  }
4040
4041
1.57M
  bool may_have_extra_named_args;
4042
1.56M
  uint32_t arg_count = zend_compile_args(args_ast, fbc, &may_have_extra_named_args);
4043
4044
1.56M
  zend_do_extended_fcall_begin();
4045
4046
1.56M
  opline = &CG(active_op_array)->opcodes[opnum_init];
4047
1.56M
  opline->extended_value = arg_count;
4048
1.56M
  uint8_t init_opcode = opline->opcode;
4049
4050
1.56M
  if (init_opcode == ZEND_INIT_FCALL) {
4051
120k
    opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
4052
120k
  }
4053
4054
1.56M
  uint8_t call_op = zend_get_call_op(
4055
1.56M
    opline,
4056
1.56M
    fbc,
4057
    /* result_used: At this point we do not yet reliably
4058
     * know if the result is used. Deoptimize #[\NoDiscard]
4059
     * calls to be sure. The optimizer will fix this up.
4060
     */
4061
1.56M
    false
4062
1.56M
  );
4063
1.56M
  opline = zend_emit_op(result, call_op, NULL, NULL);
4064
1.56M
  if (type == BP_VAR_R || type == BP_VAR_IS) {
4065
1.50M
    if (init_opcode != ZEND_NEW && opline->result_type == IS_VAR) {
4066
1.44M
      opline->result_type = IS_TMP_VAR;
4067
1.44M
      result->op_type = IS_TMP_VAR;
4068
1.44M
    }
4069
1.50M
  }
4070
1.56M
  if (may_have_extra_named_args) {
4071
10.4k
    opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4072
10.4k
  }
4073
1.56M
  opline->lineno = lineno;
4074
1.56M
  zend_do_extended_fcall_end();
4075
1.56M
  return false;
4076
1.57M
}
4077
/* }}} */
4078
4079
static bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
4080
1.35M
{
4081
1.35M
  zend_string *orig_name = zend_ast_get_str(name_ast);
4082
1.35M
  bool is_fully_qualified;
4083
4084
1.35M
  name_node->op_type = IS_CONST;
4085
1.35M
  ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
4086
1.35M
    orig_name, name_ast->attr, &is_fully_qualified));
4087
4088
1.35M
  return !is_fully_qualified && FC(current_namespace);
4089
1.35M
}
4090
/* }}} */
4091
4092
static void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */
4093
174k
{
4094
174k
  if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
4095
83.1k
    const char *colon;
4096
83.1k
    zend_string *str = Z_STR(name_node->u.constant);
4097
83.1k
    if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
4098
276
      zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
4099
276
      zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
4100
276
      zend_op *opline = get_next_op();
4101
4102
276
      opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
4103
276
      opline->op1_type = IS_CONST;
4104
276
      opline->op1.constant = zend_add_class_name_literal(class);
4105
276
      opline->op2_type = IS_CONST;
4106
276
      opline->op2.constant = zend_add_func_name_literal(method);
4107
      /* 2 slots, for class and method */
4108
276
      opline->result.num = zend_alloc_cache_slots(2);
4109
276
      zval_ptr_dtor(&name_node->u.constant);
4110
82.8k
    } else {
4111
82.8k
      zend_op *opline = get_next_op();
4112
4113
82.8k
      opline->opcode = ZEND_INIT_FCALL_BY_NAME;
4114
82.8k
      opline->op2_type = IS_CONST;
4115
82.8k
      opline->op2.constant = zend_add_func_name_literal(str);
4116
82.8k
      opline->result.num = zend_alloc_cache_slot();
4117
82.8k
    }
4118
91.6k
  } else {
4119
91.6k
    zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node);
4120
91.6k
  }
4121
4122
174k
  zend_compile_call_common(result, args_ast, NULL, lineno, type);
4123
174k
}
4124
/* }}} */
4125
4126
static inline bool zend_args_contain_unpack_or_named(const zend_ast_list *args) /* {{{ */
4127
1.19M
{
4128
1.19M
  uint32_t i;
4129
2.81M
  for (i = 0; i < args->children; ++i) {
4130
1.63M
    const zend_ast *arg = args->child[i];
4131
1.63M
    if (arg->kind == ZEND_AST_UNPACK || arg->kind == ZEND_AST_NAMED_ARG) {
4132
8.52k
      return 1;
4133
8.52k
    }
4134
1.63M
  }
4135
1.18M
  return 0;
4136
1.19M
}
4137
/* }}} */
4138
4139
static zend_result zend_compile_func_strlen(znode *result, const zend_ast_list *args) /* {{{ */
4140
704
{
4141
704
  znode arg_node;
4142
4143
704
  if (args->children != 1) {
4144
62
    return FAILURE;
4145
62
  }
4146
4147
642
  zend_compile_expr(&arg_node, args->child[0]);
4148
642
  if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
4149
124
    result->op_type = IS_CONST;
4150
124
    ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
4151
124
    zval_ptr_dtor_str(&arg_node.u.constant);
4152
518
  } else {
4153
518
    zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
4154
518
  }
4155
642
  return SUCCESS;
4156
704
}
4157
/* }}} */
4158
4159
static zend_result zend_compile_func_typecheck(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4160
1.64k
{
4161
1.64k
  znode arg_node;
4162
1.64k
  zend_op *opline;
4163
4164
1.64k
  if (args->children != 1) {
4165
445
    return FAILURE;
4166
445
  }
4167
4168
1.20k
  zend_compile_expr(&arg_node, args->child[0]);
4169
1.20k
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4170
1.20k
  if (type != _IS_BOOL) {
4171
1.14k
    opline->extended_value = (1 << type);
4172
1.14k
  } else {
4173
57
    opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE);
4174
57
  }
4175
1.20k
  return SUCCESS;
4176
1.64k
}
4177
/* }}} */
4178
4179
static zend_result zend_compile_func_is_scalar(znode *result, const zend_ast_list *args) /* {{{ */
4180
191
{
4181
191
  znode arg_node;
4182
191
  zend_op *opline;
4183
4184
191
  if (args->children != 1) {
4185
35
    return FAILURE;
4186
35
  }
4187
4188
156
  zend_compile_expr(&arg_node, args->child[0]);
4189
156
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4190
156
  opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING);
4191
156
  return SUCCESS;
4192
191
}
4193
4194
static zend_result zend_compile_func_cast(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4195
996
{
4196
996
  znode arg_node;
4197
996
  zend_op *opline;
4198
4199
996
  if (args->children != 1) {
4200
258
    return FAILURE;
4201
258
  }
4202
4203
738
  zend_compile_expr(&arg_node, args->child[0]);
4204
738
  if (type == _IS_BOOL) {
4205
190
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL);
4206
548
  } else {
4207
548
    opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
4208
548
    opline->extended_value = type;
4209
548
  }
4210
738
  return SUCCESS;
4211
996
}
4212
/* }}} */
4213
4214
static zend_result zend_compile_func_defined(znode *result, const zend_ast_list *args) /* {{{ */
4215
1.74k
{
4216
1.74k
  zend_string *name;
4217
1.74k
  zend_op *opline;
4218
4219
1.74k
  if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
4220
209
    return FAILURE;
4221
209
  }
4222
4223
1.53k
  name = zval_get_string(zend_ast_get_zval(args->child[0]));
4224
1.53k
  if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
4225
401
    zend_string_release_ex(name, 0);
4226
401
    return FAILURE;
4227
401
  }
4228
4229
1.13k
  if (zend_try_ct_eval_const(&result->u.constant, name, false)) {
4230
41
    zend_string_release_ex(name, 0);
4231
41
    zval_ptr_dtor(&result->u.constant);
4232
41
    ZVAL_TRUE(&result->u.constant);
4233
41
    result->op_type = IS_CONST;
4234
41
    return SUCCESS;
4235
41
  }
4236
4237
1.09k
  opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
4238
1.09k
  opline->op1_type = IS_CONST;
4239
1.09k
  LITERAL_STR(opline->op1, name);
4240
1.09k
  opline->extended_value = zend_alloc_cache_slot();
4241
4242
1.09k
  return SUCCESS;
4243
1.13k
}
4244
/* }}} */
4245
4246
static zend_result zend_compile_func_chr(znode *result, const zend_ast_list *args) /* {{{ */
4247
1.18k
{
4248
1.18k
  zval *zint;
4249
1.18k
  if (
4250
1.18k
    args->children == 1
4251
929
    && args->child[0]->kind == ZEND_AST_ZVAL
4252
764
    && (zint = zend_ast_get_zval(args->child[0]))
4253
764
    && Z_TYPE_P(zint) == IS_LONG
4254
708
    && Z_LVAL_P(zint) >= 0
4255
708
    && Z_LVAL_P(zint) <= 255
4256
1.18k
  ) {
4257
497
    result->op_type = IS_CONST;
4258
497
    ZVAL_CHAR(&result->u.constant, Z_LVAL_P(zint));
4259
497
    return SUCCESS;
4260
685
  } else {
4261
685
    return FAILURE;
4262
685
  }
4263
1.18k
}
4264
/* }}} */
4265
4266
static zend_result zend_compile_func_ord(znode *result, const zend_ast_list *args) /* {{{ */
4267
574
{
4268
574
  zval *str;
4269
574
  if (
4270
574
    args->children == 1
4271
366
    && args->child[0]->kind == ZEND_AST_ZVAL
4272
274
    && (str = zend_ast_get_zval(args->child[0]))
4273
274
    && Z_TYPE_P(str) == IS_STRING
4274
190
    && Z_STRLEN_P(str) == 1
4275
574
  ) {
4276
42
    result->op_type = IS_CONST;
4277
42
    ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(str)[0]);
4278
42
    return SUCCESS;
4279
532
  } else {
4280
532
    return FAILURE;
4281
532
  }
4282
574
}
4283
/* }}} */
4284
4285
/* We can only calculate the stack size for functions that have been fully compiled, otherwise
4286
 * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for
4287
 * directly or indirectly recursive function calls. */
4288
141k
static bool fbc_is_finalized(const zend_function *fbc) {
4289
141k
  return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO);
4290
141k
}
4291
4292
static bool zend_compile_ignore_class(const zend_class_entry *ce, const zend_string *filename)
4293
56.6k
{
4294
56.6k
  if (ce->type == ZEND_INTERNAL_CLASS) {
4295
37.7k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES;
4296
37.7k
  } else {
4297
18.8k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4298
14.6k
      && ce->info.user.filename != filename;
4299
18.8k
  }
4300
56.6k
}
4301
4302
static bool zend_compile_ignore_function(const zend_function *fbc, const zend_string *filename)
4303
127k
{
4304
127k
  if (fbc->type == ZEND_INTERNAL_FUNCTION) {
4305
107k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS;
4306
107k
  } else {
4307
20.3k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)
4308
20.3k
      || ((CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4309
17.0k
        && fbc->op_array.filename != filename);
4310
20.3k
  }
4311
127k
}
4312
4313
static zend_result zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
4314
3.65k
{
4315
3.65k
  zend_string *name, *lcname;
4316
3.65k
  zend_function *fbc;
4317
3.65k
  zend_op *opline;
4318
4319
3.65k
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
4320
2.45k
    return FAILURE;
4321
2.45k
  }
4322
4323
1.20k
  name = zend_ast_get_str(name_ast);
4324
1.20k
  lcname = zend_string_tolower(name);
4325
4326
1.20k
  fbc = zend_hash_find_ptr(CG(function_table), lcname);
4327
1.20k
  if (!fbc
4328
662
   || !fbc_is_finalized(fbc)
4329
662
   || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
4330
538
    zend_string_release_ex(lcname, 0);
4331
538
    return FAILURE;
4332
538
  }
4333
4334
662
  opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
4335
662
  opline->extended_value = num_args;
4336
662
  opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
4337
662
  opline->op2_type = IS_CONST;
4338
662
  LITERAL_STR(opline->op2, lcname);
4339
662
  opline->result.num = zend_alloc_cache_slot();
4340
4341
662
  return SUCCESS;
4342
1.20k
}
4343
/* }}} */
4344
4345
static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
4346
3.65k
{
4347
3.65k
  zend_op *opline;
4348
3.65k
  znode name_node;
4349
4350
3.65k
  if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
4351
662
    return;
4352
662
  }
4353
4354
2.99k
  zend_compile_expr(&name_node, name_ast);
4355
4356
2.99k
  opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
4357
2.99k
  opline->op1_type = IS_CONST;
4358
2.99k
  LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
4359
2.99k
  opline->extended_value = num_args;
4360
2.99k
}
4361
/* }}} */
4362
4363
/* cufa = call_user_func_array */
4364
static zend_result zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t type) /* {{{ */
4365
1.74k
{
4366
1.74k
  znode arg_node;
4367
1.74k
  zend_op *opline;
4368
4369
1.74k
  if (args->children != 2) {
4370
41
    return FAILURE;
4371
41
  }
4372
4373
1.70k
  zend_compile_init_user_func(args->child[0], 0, lcname);
4374
1.70k
  if (args->child[1]->kind == ZEND_AST_CALL
4375
1.25k
   && args->child[1]->child[0]->kind == ZEND_AST_ZVAL
4376
1.22k
   && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING
4377
1.20k
   && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) {
4378
1.19k
    zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]);
4379
1.19k
    zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]);
4380
1.19k
    bool is_fully_qualified;
4381
1.19k
    zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified);
4382
4383
1.19k
    if (zend_string_equals_literal_ci(name, "array_slice")
4384
741
       && !zend_args_contain_unpack_or_named(list)
4385
643
     && list->children == 3
4386
370
     && list->child[1]->kind == ZEND_AST_ZVAL) {
4387
287
      zval *zv = zend_ast_get_zval(list->child[1]);
4388
4389
287
      if (Z_TYPE_P(zv) == IS_LONG
4390
275
       && Z_LVAL_P(zv) >= 0
4391
275
       && Z_LVAL_P(zv) <= 0x7fffffff) {
4392
102
        zend_op *opline;
4393
102
        znode len_node;
4394
4395
102
        zend_compile_expr(&arg_node, list->child[0]);
4396
102
        zend_compile_expr(&len_node, list->child[2]);
4397
102
        opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node);
4398
102
        opline->extended_value = Z_LVAL_P(zv);
4399
102
        opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4400
102
        if (type == BP_VAR_R || type == BP_VAR_IS) {
4401
102
          opline->result_type = IS_TMP_VAR;
4402
102
          result->op_type = IS_TMP_VAR;
4403
102
        }
4404
102
        zend_string_release_ex(name, 0);
4405
102
        return SUCCESS;
4406
102
      }
4407
287
    }
4408
1.09k
    zend_string_release_ex(name, 0);
4409
1.09k
  }
4410
1.60k
  zend_compile_expr(&arg_node, args->child[1]);
4411
1.60k
  zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
4412
1.60k
  zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
4413
1.60k
  opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4414
1.60k
  if (type == BP_VAR_R || type == BP_VAR_IS) {
4415
1.49k
    opline->result_type = IS_TMP_VAR;
4416
1.49k
    result->op_type = IS_TMP_VAR;
4417
1.49k
  }
4418
1.60k
  opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4419
4420
1.60k
  return SUCCESS;
4421
1.70k
}
4422
/* }}} */
4423
4424
/* cuf = call_user_func */
4425
static zend_result zend_compile_func_cuf(znode *result, const zend_ast_list *args, zend_string *lcname, uint32_t type) /* {{{ */
4426
2.11k
{
4427
2.11k
  uint32_t i;
4428
4429
2.11k
  if (args->children < 1) {
4430
161
    return FAILURE;
4431
161
  }
4432
4433
1.94k
  zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
4434
5.82k
  for (i = 1; i < args->children; ++i) {
4435
3.87k
    zend_ast *arg_ast = args->child[i];
4436
3.87k
    znode arg_node;
4437
3.87k
    zend_op *opline;
4438
4439
3.87k
    zend_compile_expr(&arg_node, arg_ast);
4440
4441
3.87k
    opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
4442
3.87k
    opline->op2.num = i;
4443
3.87k
    opline->result.var = EX_NUM_TO_VAR(i - 1);
4444
3.87k
  }
4445
1.94k
  zend_op *opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4446
1.94k
  if (type == BP_VAR_R || type == BP_VAR_IS) {
4447
784
    opline->result_type = IS_TMP_VAR;
4448
784
    result->op_type = IS_TMP_VAR;
4449
784
  }
4450
4451
1.94k
  return SUCCESS;
4452
2.11k
}
4453
/* }}} */
4454
4455
static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, const zend_function *fbc, uint32_t lineno, uint32_t type) /* {{{ */
4456
64.8k
{
4457
64.8k
  if (EG(assertions) >= 0) {
4458
64.8k
    znode name_node;
4459
64.8k
    zend_op *opline;
4460
64.8k
    uint32_t check_op_number = get_next_op_number();
4461
4462
64.8k
    zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
4463
4464
64.8k
    if (fbc && fbc_is_finalized(fbc)) {
4465
14.0k
      name_node.op_type = IS_CONST;
4466
14.0k
      ZVAL_STR_COPY(&name_node.u.constant, name);
4467
4468
14.0k
      opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
4469
50.7k
    } else {
4470
50.7k
      opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
4471
50.7k
      opline->op2_type = IS_CONST;
4472
50.7k
      opline->op2.constant = zend_add_ns_func_name_literal(name);
4473
50.7k
    }
4474
64.8k
    opline->result.num = zend_alloc_cache_slot();
4475
4476
64.8k
    if (args->children == 1) {
4477
      /* add "assert(condition) as assertion message */
4478
14.2k
      zend_ast *arg = zend_ast_create_zval_from_str(
4479
14.2k
        zend_ast_export("assert(", args->child[0], ")"));
4480
14.2k
      if (args->child[0]->kind == ZEND_AST_NAMED_ARG) {
4481
        /* If the original argument was named, add the new argument as named as well,
4482
         * as mixing named and positional is not allowed. */
4483
349
        zend_ast *name = zend_ast_create_zval_from_str(
4484
349
          ZSTR_INIT_LITERAL("description", 0));
4485
349
        arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg);
4486
349
      }
4487
14.2k
      args = (zend_ast_list *)zend_ast_list_add((zend_ast *) args, arg);
4488
14.2k
    }
4489
4490
64.8k
    zend_compile_call_common(result, (zend_ast*)args, fbc, lineno, type);
4491
4492
64.8k
    opline = &CG(active_op_array)->opcodes[check_op_number];
4493
64.8k
    opline->op2.opline_num = get_next_op_number();
4494
64.8k
    SET_NODE(opline->result, result);
4495
64.8k
  } else {
4496
0
    if (!fbc) {
4497
0
      zend_string_release_ex(name, 0);
4498
0
    }
4499
0
    result->op_type = IS_CONST;
4500
0
    ZVAL_TRUE(&result->u.constant);
4501
0
  }
4502
64.8k
}
4503
/* }}} */
4504
4505
static zend_result zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */
4506
2.20k
{
4507
2.20k
  bool strict = false;
4508
2.20k
  znode array, needly;
4509
2.20k
  zend_op *opline;
4510
4511
2.20k
  if (args->children == 3) {
4512
844
    if (args->child[2]->kind == ZEND_AST_ZVAL) {
4513
446
      strict = zend_is_true(zend_ast_get_zval(args->child[2]));
4514
446
    } else if (args->child[2]->kind == ZEND_AST_CONST) {
4515
376
      zval value;
4516
376
      zend_ast *name_ast = args->child[2]->child[0];
4517
376
      bool is_fully_qualified;
4518
376
      zend_string *resolved_name = zend_resolve_const_name(
4519
376
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
4520
4521
376
      if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) {
4522
196
        zend_string_release_ex(resolved_name, 0);
4523
196
        return FAILURE;
4524
196
      }
4525
4526
180
      zend_string_release_ex(resolved_name, 0);
4527
180
      strict = zend_is_true(&value);
4528
180
      zval_ptr_dtor(&value);
4529
180
    } else {
4530
22
      return FAILURE;
4531
22
    }
4532
1.36k
  } else if (args->children != 2) {
4533
179
    return FAILURE;
4534
179
  }
4535
4536
1.80k
  if (args->child[1]->kind != ZEND_AST_ARRAY
4537
1.30k
   || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) {
4538
580
    return FAILURE;
4539
580
  }
4540
4541
1.22k
  if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) {
4542
1.06k
    bool ok = true;
4543
1.06k
    zval *val, tmp;
4544
1.06k
    HashTable *src = Z_ARRVAL(array.u.constant);
4545
1.06k
    HashTable *dst = zend_new_array(zend_hash_num_elements(src));
4546
4547
1.06k
    ZVAL_TRUE(&tmp);
4548
4549
1.06k
    if (strict) {
4550
2.90k
      ZEND_HASH_FOREACH_VAL(src, val) {
4551
2.90k
        if (Z_TYPE_P(val) == IS_STRING) {
4552
11
          zend_hash_add(dst, Z_STR_P(val), &tmp);
4553
819
        } else if (Z_TYPE_P(val) == IS_LONG) {
4554
778
          zend_hash_index_add(dst, Z_LVAL_P(val), &tmp);
4555
778
        } else {
4556
41
          zend_array_destroy(dst);
4557
41
          ok = false;
4558
41
          break;
4559
41
        }
4560
2.90k
      } ZEND_HASH_FOREACH_END();
4561
636
    } else {
4562
2.77k
      ZEND_HASH_FOREACH_VAL(src, val) {
4563
2.77k
        if (Z_TYPE_P(val) != IS_STRING
4564
425
         || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) {
4565
425
          zend_array_destroy(dst);
4566
425
          ok = false;
4567
425
          break;
4568
425
        }
4569
336
        zend_hash_add(dst, Z_STR_P(val), &tmp);
4570
336
      } ZEND_HASH_FOREACH_END();
4571
636
    }
4572
4573
1.06k
    zend_array_destroy(src);
4574
1.06k
    if (!ok) {
4575
466
      return FAILURE;
4576
466
    }
4577
600
    Z_ARRVAL(array.u.constant) = dst;
4578
600
  }
4579
762
  array.op_type = IS_CONST;
4580
4581
762
  zend_compile_expr(&needly, args->child[0]);
4582
4583
762
  opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array);
4584
762
  opline->extended_value = strict;
4585
4586
762
  return SUCCESS;
4587
1.22k
}
4588
/* }}} */
4589
4590
static zend_result zend_compile_func_count(znode *result, const zend_ast_list *args, const zend_string *lcname) /* {{{ */
4591
889
{
4592
889
  znode arg_node;
4593
889
  zend_op *opline;
4594
4595
889
  if (args->children != 1) {
4596
49
    return FAILURE;
4597
49
  }
4598
4599
840
  zend_compile_expr(&arg_node, args->child[0]);
4600
840
  opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL);
4601
840
  opline->extended_value = zend_string_equals_literal(lcname, "sizeof");
4602
4603
840
  return SUCCESS;
4604
889
}
4605
/* }}} */
4606
4607
static zend_result zend_compile_func_get_class(znode *result, const zend_ast_list *args) /* {{{ */
4608
1.13k
{
4609
1.13k
  if (args->children == 0) {
4610
48
    zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL);
4611
1.08k
  } else {
4612
1.08k
    znode arg_node;
4613
4614
1.08k
    if (args->children != 1) {
4615
198
      return FAILURE;
4616
198
    }
4617
4618
889
    zend_compile_expr(&arg_node, args->child[0]);
4619
889
    zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL);
4620
889
  }
4621
937
  return SUCCESS;
4622
1.13k
}
4623
/* }}} */
4624
4625
static zend_result zend_compile_func_get_called_class(znode *result, const zend_ast_list *args) /* {{{ */
4626
138
{
4627
138
  if (args->children != 0) {
4628
38
    return FAILURE;
4629
38
  }
4630
4631
100
  zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL);
4632
100
  return SUCCESS;
4633
138
}
4634
/* }}} */
4635
4636
static zend_result zend_compile_func_gettype(znode *result, const zend_ast_list *args) /* {{{ */
4637
441
{
4638
441
  znode arg_node;
4639
4640
441
  if (args->children != 1) {
4641
8
    return FAILURE;
4642
8
  }
4643
4644
433
  zend_compile_expr(&arg_node, args->child[0]);
4645
433
  zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL);
4646
433
  return SUCCESS;
4647
441
}
4648
/* }}} */
4649
4650
static zend_result zend_compile_func_num_args(znode *result, const zend_ast_list *args) /* {{{ */
4651
185
{
4652
185
  if (CG(active_op_array)->function_name && args->children == 0) {
4653
57
    zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL);
4654
57
    return SUCCESS;
4655
128
  } else {
4656
128
    return FAILURE;
4657
128
  }
4658
185
}
4659
/* }}} */
4660
4661
static zend_result zend_compile_func_get_args(znode *result, const zend_ast_list *args) /* {{{ */
4662
537
{
4663
537
  if (CG(active_op_array)->function_name && args->children == 0) {
4664
229
    zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL);
4665
229
    return SUCCESS;
4666
308
  } else {
4667
308
    return FAILURE;
4668
308
  }
4669
537
}
4670
/* }}} */
4671
4672
static zend_result zend_compile_func_array_key_exists(znode *result, const zend_ast_list *args) /* {{{ */
4673
270
{
4674
270
  znode subject, needle;
4675
4676
270
  if (args->children != 2) {
4677
92
    return FAILURE;
4678
92
  }
4679
4680
178
  zend_compile_expr(&needle, args->child[0]);
4681
178
  zend_compile_expr(&subject, args->child[1]);
4682
4683
178
  zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject);
4684
178
  return SUCCESS;
4685
270
}
4686
/* }}} */
4687
4688
static zend_result zend_compile_func_array_slice(znode *result, const zend_ast_list *args) /* {{{ */
4689
1.66k
{
4690
1.66k
  if (CG(active_op_array)->function_name
4691
770
   && args->children == 2
4692
380
   && args->child[0]->kind == ZEND_AST_CALL
4693
293
   && args->child[0]->child[0]->kind == ZEND_AST_ZVAL
4694
256
   && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING
4695
256
   && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST
4696
255
   && args->child[1]->kind == ZEND_AST_ZVAL) {
4697
4698
243
    zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]);
4699
243
    bool is_fully_qualified;
4700
243
    zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified);
4701
243
    const zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]);
4702
243
    const zval *zv = zend_ast_get_zval(args->child[1]);
4703
243
    znode first;
4704
4705
243
    if (zend_string_equals_literal_ci(name, "func_get_args")
4706
122
     && list->children == 0
4707
87
     && Z_TYPE_P(zv) == IS_LONG
4708
42
     && Z_LVAL_P(zv) >= 0) {
4709
42
      first.op_type = IS_CONST;
4710
42
      ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv));
4711
42
      zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL);
4712
42
      zend_string_release_ex(name, 0);
4713
42
      return SUCCESS;
4714
42
    }
4715
201
    zend_string_release_ex(name, 0);
4716
201
  }
4717
1.62k
  return FAILURE;
4718
1.66k
}
4719
/* }}} */
4720
4721
static uint32_t find_frameless_function_offset(uint32_t arity, const void *handler)
4722
418k
{
4723
418k
  void **handlers = zend_flf_handlers;
4724
418k
  void **current = handlers;
4725
3.70M
  while (current) {
4726
3.70M
    if (*current == handler) {
4727
418k
      return current - handlers;
4728
418k
    }
4729
3.28M
    current++;
4730
3.28M
  }
4731
4732
0
  return (uint32_t)-1;
4733
418k
}
4734
4735
static const zend_frameless_function_info *find_frameless_function_info(const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4736
323k
{
4737
323k
  if (zend_execute_internal) {
4738
81.2k
    return NULL;
4739
81.2k
  }
4740
4741
242k
  if (ZEND_USER_CODE(fbc->type)) {
4742
3
    return NULL;
4743
3
  }
4744
4745
242k
  const zend_frameless_function_info *frameless_function_info = fbc->internal_function.frameless_function_infos;
4746
242k
  if (!frameless_function_info) {
4747
22.3k
    return NULL;
4748
22.3k
  }
4749
4750
219k
  if (args->children > 3) {
4751
196
    return NULL;
4752
196
  }
4753
4754
272k
  while (frameless_function_info->handler) {
4755
262k
    if (frameless_function_info->num_args >= args->children
4756
229k
     && fbc->common.required_num_args <= args->children
4757
209k
     && (!(fbc->common.fn_flags & ZEND_ACC_VARIADIC)
4758
209k
      || frameless_function_info->num_args == args->children)) {
4759
209k
      uint32_t num_args = frameless_function_info->num_args;
4760
209k
      uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4761
209k
      if (offset == (uint32_t)-1) {
4762
0
        continue;
4763
0
      }
4764
209k
      return frameless_function_info;
4765
209k
    }
4766
52.9k
    frameless_function_info++;
4767
52.9k
  }
4768
4769
10.0k
  return NULL;
4770
219k
}
4771
4772
static uint32_t zend_compile_frameless_icall_ex(znode *result, const zend_ast_list *args, const zend_function *fbc, const zend_frameless_function_info *frameless_function_info, uint32_t type)
4773
209k
{
4774
209k
  uint32_t lineno = CG(zend_lineno);
4775
209k
  uint32_t num_args = frameless_function_info->num_args;
4776
209k
  uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4777
209k
  znode arg_zvs[3];
4778
652k
  for (uint32_t i = 0; i < num_args; i++) {
4779
442k
    if (i < args->children) {
4780
442k
      zend_compile_expr(&arg_zvs[i], args->child[i]);
4781
442k
    } else {
4782
0
      const zend_arg_info *arg_info = &fbc->common.arg_info[i];
4783
0
      arg_zvs[i].op_type = IS_CONST;
4784
0
      if (zend_get_default_from_internal_arg_info(&arg_zvs[i].u.constant, arg_info) == FAILURE) {
4785
0
        ZEND_UNREACHABLE();
4786
0
      }
4787
0
    }
4788
442k
  }
4789
209k
  uint8_t opcode = ZEND_FRAMELESS_ICALL_0 + num_args;
4790
209k
  uint32_t opnum = get_next_op_number();
4791
209k
  zend_op *opline = zend_emit_op_tmp(result, opcode, NULL, NULL);
4792
209k
  opline->extended_value = offset;
4793
209k
  opline->lineno = lineno;
4794
209k
  if (num_args >= 1) {
4795
209k
    SET_NODE(opline->op1, &arg_zvs[0]);
4796
209k
  }
4797
209k
  if (num_args >= 2) {
4798
199k
    SET_NODE(opline->op2, &arg_zvs[1]);
4799
199k
  }
4800
209k
  if (num_args >= 3) {
4801
33.3k
    zend_emit_op_data(&arg_zvs[2]);
4802
33.3k
  }
4803
209k
  return opnum;
4804
209k
}
4805
4806
static uint32_t zend_compile_frameless_icall(znode *result, const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4807
90.3k
{
4808
90.3k
  const zend_frameless_function_info *frameless_function_info = find_frameless_function_info(args, fbc, type);
4809
90.3k
  if (!frameless_function_info) {
4810
89.0k
    return (uint32_t)-1;
4811
89.0k
  }
4812
4813
1.36k
  return zend_compile_frameless_icall_ex(result, args, fbc, frameless_function_info, type);
4814
90.3k
}
4815
4816
static void zend_compile_ns_call(znode *result, const znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */
4817
1.08M
{
4818
1.08M
  int name_constants = zend_add_ns_func_name_literal(Z_STR(name_node->u.constant));
4819
4820
  /* Find frameless function with same name. */
4821
1.08M
  const zend_function *frameless_function = NULL;
4822
1.08M
  if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT
4823
1.08M
   && !zend_args_contain_unpack_or_named(zend_ast_get_list(args_ast))
4824
   /* Avoid blowing up op count with nested frameless branches. */
4825
1.07M
   && !CG(context).in_jmp_frameless_branch) {
4826
685k
    zend_string *lc_func_name = Z_STR_P(CT_CONSTANT_EX(CG(active_op_array), name_constants + 2));
4827
685k
    frameless_function = zend_hash_find_ptr(CG(function_table), lc_func_name);
4828
685k
  }
4829
4830
  /* Check whether any frameless handler may actually be used. */
4831
1.08M
  uint32_t jmp_fl_opnum = 0;
4832
1.08M
  const zend_frameless_function_info *frameless_function_info = NULL;
4833
1.08M
  if (frameless_function) {
4834
232k
    frameless_function_info = find_frameless_function_info(zend_ast_get_list(args_ast), frameless_function, type);
4835
232k
    if (frameless_function_info) {
4836
208k
      CG(context).in_jmp_frameless_branch = true;
4837
208k
      znode op1;
4838
208k
      op1.op_type = IS_CONST;
4839
208k
      ZVAL_COPY(&op1.u.constant, CT_CONSTANT_EX(CG(active_op_array), name_constants + 1));
4840
208k
      jmp_fl_opnum = get_next_op_number();
4841
208k
      zend_emit_op(NULL, ZEND_JMP_FRAMELESS, &op1, NULL);
4842
208k
    }
4843
232k
  }
4844
4845
  /* Compile ns call. */
4846
1.08M
  zend_op *opline = get_next_op();
4847
1.08M
  opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
4848
1.08M
  opline->op2_type = IS_CONST;
4849
1.08M
  opline->op2.constant = name_constants;
4850
1.08M
  opline->result.num = zend_alloc_cache_slot();
4851
1.08M
  zend_compile_call_common(result, args_ast, NULL, lineno, type);
4852
4853
  /* Compile frameless call. */
4854
1.08M
  if (frameless_function_info) {
4855
208k
    CG(zend_lineno) = lineno;
4856
4857
208k
    uint32_t jmp_end_opnum = zend_emit_jump(0);
4858
208k
    uint32_t jmp_fl_target = get_next_op_number();
4859
4860
208k
    uint32_t flf_icall_opnum = zend_compile_frameless_icall_ex(NULL, zend_ast_get_list(args_ast), frameless_function, frameless_function_info, type);
4861
4862
208k
    zend_op *jmp_fl = &CG(active_op_array)->opcodes[jmp_fl_opnum];
4863
208k
    jmp_fl->op2.opline_num = jmp_fl_target;
4864
208k
    jmp_fl->extended_value = zend_alloc_cache_slot();
4865
208k
    zend_op *flf_icall = &CG(active_op_array)->opcodes[flf_icall_opnum];
4866
208k
    SET_NODE(flf_icall->result, result);
4867
208k
    zend_update_jump_target_to_next(jmp_end_opnum);
4868
4869
208k
    CG(context).in_jmp_frameless_branch = false;
4870
208k
  }
4871
1.08M
}
4872
/* }}} */
4873
4874
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node);
4875
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node);
4876
static void zend_compile_rope_finalize(znode *result, uint32_t j, zend_op *init_opline, zend_op *opline);
4877
4878
static zend_result zend_compile_func_sprintf(znode *result, zend_ast_list *args) /* {{{ */
4879
3.85k
{
4880
  /* Bail out if we do not have a format string. */
4881
3.85k
  if (args->children < 1) {
4882
220
    return FAILURE;
4883
220
  }
4884
4885
3.63k
  zend_eval_const_expr(&args->child[0]);
4886
  /* Bail out if the format string is not constant. */
4887
3.63k
  if (args->child[0]->kind != ZEND_AST_ZVAL) {
4888
261
    return FAILURE;
4889
261
  }
4890
4891
3.37k
  zval *format_string = zend_ast_get_zval(args->child[0]);
4892
3.37k
  if (Z_TYPE_P(format_string) != IS_STRING) {
4893
31
    return FAILURE;
4894
31
  }
4895
3.34k
  if (Z_STRLEN_P(format_string) >= 256) {
4896
57
    return FAILURE;
4897
57
  }
4898
4899
3.28k
  char *p;
4900
3.28k
  char *end;
4901
3.28k
  uint32_t placeholder_count;
4902
4903
3.28k
  placeholder_count = 0;
4904
3.28k
  p = Z_STRVAL_P(format_string);
4905
3.28k
  end = p + Z_STRLEN_P(format_string);
4906
4907
6.70k
  for (;;) {
4908
6.70k
    p = memchr(p, '%', end - p);
4909
6.70k
    if (!p) {
4910
2.90k
      break;
4911
2.90k
    }
4912
4913
3.80k
    char *q = p + 1;
4914
3.80k
    if (q == end) {
4915
222
      return FAILURE;
4916
222
    }
4917
4918
3.58k
    switch (*q) {
4919
2.70k
      case 's':
4920
3.00k
      case 'd':
4921
3.00k
        placeholder_count++;
4922
3.00k
        break;
4923
414
      case '%':
4924
414
        break;
4925
164
      default:
4926
164
        return FAILURE;
4927
3.58k
    }
4928
4929
3.42k
    p = q;
4930
3.42k
    p++;
4931
3.42k
  }
4932
4933
  /* Bail out if the number of placeholders does not match the number of values. */
4934
2.90k
  if (placeholder_count != (args->children - 1)) {
4935
121
    return FAILURE;
4936
121
  }
4937
4938
  /* Handle empty format strings. */
4939
2.77k
  if (Z_STRLEN_P(format_string) == 0) {
4940
63
    result->op_type = IS_CONST;
4941
63
    ZVAL_EMPTY_STRING(&result->u.constant);
4942
4943
63
    return SUCCESS;
4944
63
  }
4945
4946
2.71k
  znode *elements = NULL;
4947
4948
2.71k
  if (placeholder_count > 0) {
4949
2.00k
    elements = safe_emalloc(sizeof(*elements), placeholder_count, 0);
4950
2.00k
  }
4951
4952
  /* Compile the value expressions first for error handling that is consistent
4953
   * with a function call: Values that fail to convert to a string may emit errors.
4954
   */
4955
5.57k
  for (uint32_t i = 0; i < placeholder_count; i++) {
4956
2.85k
    zend_compile_expr(elements + i, args->child[1 + i]);
4957
2.85k
  }
4958
4959
2.71k
  uint32_t rope_elements = 0;
4960
2.71k
  uint32_t rope_init_lineno = -1;
4961
2.71k
  zend_op *opline = NULL;
4962
4963
2.71k
  placeholder_count = 0;
4964
2.71k
  p = Z_STRVAL_P(format_string);
4965
2.71k
  end = p + Z_STRLEN_P(format_string);
4966
2.71k
  char *offset = p;
4967
5.78k
  for (;;) {
4968
5.78k
    p = memchr(p, '%', end - p);
4969
5.78k
    if (!p) {
4970
2.71k
      break;
4971
2.71k
    }
4972
4973
3.06k
    char *q = p + 1;
4974
3.06k
    ZEND_ASSERT(q < end);
4975
3.06k
    ZEND_ASSERT(*q == 's' || *q == 'd' || *q == '%');
4976
4977
3.06k
    if (*q == '%') {
4978
      /* Optimization to not create a dedicated rope element for the literal '%':
4979
       * Include the first '%' within the "constant" part instead of dropping the
4980
       * full placeholder.
4981
       */
4982
210
      p++;
4983
210
    }
4984
4985
3.06k
    if (p != offset) {
4986
2.10k
      znode const_node;
4987
2.10k
      const_node.op_type = IS_CONST;
4988
2.10k
      ZVAL_STRINGL(&const_node.u.constant, offset, p - offset);
4989
2.10k
      if (rope_elements == 0) {
4990
1.19k
        rope_init_lineno = get_next_op_number();
4991
1.19k
      }
4992
2.10k
      opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4993
2.10k
    }
4994
4995
3.06k
    if (*q != '%') {
4996
2.85k
      switch (*q) {
4997
2.62k
        case 's':
4998
          /* Perform the cast of constants when actually evaluating the corresponding placeholder
4999
           * for correct error reporting.
5000
           */
5001
2.62k
          if (elements[placeholder_count].op_type == IS_CONST) {
5002
507
            if (Z_TYPE(elements[placeholder_count].u.constant) == IS_ARRAY) {
5003
68
              zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_STRING;
5004
439
            } else {
5005
439
              convert_to_string(&elements[placeholder_count].u.constant);
5006
439
            }
5007
507
          }
5008
2.62k
          break;
5009
231
        case 'd':
5010
231
          zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_LONG;
5011
231
          break;
5012
0
        EMPTY_SWITCH_DEFAULT_CASE();
5013
2.85k
      }
5014
5015
2.85k
      if (rope_elements == 0) {
5016
901
        rope_init_lineno = get_next_op_number();
5017
901
      }
5018
2.85k
      opline = zend_compile_rope_add(result, rope_elements++, &elements[placeholder_count]);
5019
5020
2.85k
      placeholder_count++;
5021
2.85k
    }
5022
5023
3.06k
    p = q;
5024
3.06k
    p++;
5025
3.06k
    offset = p;
5026
3.06k
  }
5027
2.71k
  if (end != offset) {
5028
    /* Add the constant part after the last placeholder. */
5029
2.50k
    znode const_node;
5030
2.50k
    const_node.op_type = IS_CONST;
5031
2.50k
    ZVAL_STRINGL(&const_node.u.constant, offset, end - offset);
5032
2.50k
    if (rope_elements == 0) {
5033
622
      rope_init_lineno = get_next_op_number();
5034
622
    }
5035
2.50k
    opline = zend_compile_rope_add(result, rope_elements++, &const_node);
5036
2.50k
  }
5037
2.71k
  ZEND_ASSERT(opline != NULL);
5038
5039
2.71k
  zend_op *init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
5040
2.71k
  zend_compile_rope_finalize(result, rope_elements, init_opline, opline);
5041
2.71k
  efree(elements);
5042
5043
2.71k
  return SUCCESS;
5044
2.71k
}
5045
5046
static zend_result zend_compile_func_printf(znode *result, zend_ast_list *args) /* {{{ */
5047
2.57k
{
5048
  /* Special case: printf with a single constant string argument and no format specifiers.
5049
   * In this case, just emit ECHO and return the string length if needed. */
5050
2.57k
  if (args->children == 1) {
5051
687
    zend_eval_const_expr(&args->child[0]);
5052
687
    if (args->child[0]->kind != ZEND_AST_ZVAL) {
5053
33
      return FAILURE;
5054
33
    }
5055
654
    zval *format_string = zend_ast_get_zval(args->child[0]);
5056
654
    if (Z_TYPE_P(format_string) != IS_STRING) {
5057
22
      return FAILURE;
5058
22
    }
5059
    /* Check if there are any format specifiers */
5060
632
    if (!memchr(Z_STRVAL_P(format_string), '%', Z_STRLEN_P(format_string))) {
5061
      /* No format specifiers - just emit ECHO and return string length */
5062
347
      znode format_node;
5063
347
      zend_compile_expr(&format_node, args->child[0]);
5064
347
      zend_emit_op(NULL, ZEND_ECHO, &format_node, NULL);
5065
5066
      /* Return the string length as a constant if the result is used */
5067
347
      result->op_type = IS_CONST;
5068
347
      ZVAL_LONG(&result->u.constant, Z_STRLEN_P(format_string));
5069
347
      return SUCCESS;
5070
347
    }
5071
632
  }
5072
5073
  /* Fall back to sprintf optimization for format strings with specifiers */
5074
2.17k
  znode rope_result;
5075
2.17k
  if (zend_compile_func_sprintf(&rope_result, args) != SUCCESS) {
5076
347
    return FAILURE;
5077
347
  }
5078
5079
  /* printf() returns the amount of bytes written, so just an ECHO of the
5080
   * resulting sprintf() optimisation might not be enough. At this early
5081
   * stage we can't detect if the result is actually used, so we just emit
5082
   * the opcodes and let them be cleaned up by the dead code elimination
5083
   * pass in the Zend Optimizer if the result of the printf() is in fact
5084
   * unused */
5085
1.82k
  znode copy;
5086
1.82k
  if (rope_result.op_type != IS_CONST) {
5087
    /* Note: ZEND_COPY_TMP is only valid for TMPVAR. */
5088
1.68k
    ZEND_ASSERT(rope_result.op_type == IS_TMP_VAR);
5089
1.68k
    zend_emit_op_tmp(&copy, ZEND_COPY_TMP, &rope_result, NULL);
5090
1.68k
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5091
1.68k
    zend_emit_op_tmp(result, ZEND_STRLEN, &copy, NULL);
5092
1.68k
  } else {
5093
141
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5094
141
    result->op_type = IS_CONST;
5095
141
    ZVAL_LONG(&result->u.constant, Z_STRLEN(rope_result.u.constant));
5096
141
  }
5097
5098
1.82k
  return SUCCESS;
5099
1.82k
}
5100
5101
static zend_result zend_compile_func_clone(znode *result, const zend_ast_list *args)
5102
945
{
5103
945
  znode arg_node;
5104
5105
945
  if (args->children != 1) {
5106
318
    return FAILURE;
5107
318
  }
5108
5109
627
  zend_compile_expr(&arg_node, args->child[0]);
5110
627
  zend_emit_op_tmp(result, ZEND_CLONE, &arg_node, NULL);
5111
5112
627
  return SUCCESS;
5113
945
}
5114
5115
static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t lineno) /* {{{ */
5116
316
{
5117
  /* Bail out if we do not have exactly two parameters. */
5118
316
  if (args->children != 2) {
5119
28
    return FAILURE;
5120
28
  }
5121
5122
288
  zend_ast *callback = args->child[0];
5123
5124
  /* Bail out if the callback is not a FCC/PFA. */
5125
288
  zend_ast *args_ast;
5126
288
  switch (callback->kind) {
5127
43
    case ZEND_AST_CALL:
5128
43
    case ZEND_AST_STATIC_CALL:
5129
43
      args_ast = zend_ast_call_get_args(callback);
5130
43
      if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT) {
5131
15
        return FAILURE;
5132
15
      }
5133
5134
28
      break;
5135
245
    default:
5136
245
      return FAILURE;
5137
288
  }
5138
5139
  /* Bail out if the callback is assert() due to the AST stringification logic
5140
   * breaking for the generated call.
5141
   */
5142
28
  if (callback->kind == ZEND_AST_CALL
5143
28
   && callback->child[0]->kind == ZEND_AST_ZVAL 
5144
26
   && Z_TYPE_P(zend_ast_get_zval(callback->child[0])) == IS_STRING
5145
26
   && zend_string_equals_literal_ci(zend_ast_get_str(callback->child[0]), "assert")) {
5146
0
    return FAILURE;
5147
0
  }
5148
5149
28
  zend_ast_list *callback_args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args);
5150
28
  if (callback_args->children != 1 || callback_args->child[0]->attr != ZEND_PLACEHOLDER_VARIADIC) {
5151
    /* Full PFA is not yet implemented, will fail in zend_compile_call_common(). */
5152
0
    return FAILURE;
5153
0
  }
5154
5155
28
  znode value;
5156
28
  value.op_type = IS_TMP_VAR;
5157
28
  value.u.op.var = get_temporary_variable();
5158
28
  zend_ast *call_args = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&value));
5159
5160
28
  zend_op *opline;
5161
5162
28
  znode array;
5163
28
  zend_compile_expr(&array, args->child[1]);
5164
  /* array is an argument to both ZEND_TYPE_ASSERT and to ZEND_FE_RESET_R. */
5165
28
  if (array.op_type == IS_CONST) {
5166
11
    Z_TRY_ADDREF(array.u.constant);
5167
11
  }
5168
5169
  /* Verify that the input array actually is an array. */
5170
28
  znode name;
5171
28
  name.op_type = IS_CONST;
5172
28
  ZVAL_STR_COPY(&name.u.constant, lcname);
5173
28
  opline = zend_emit_op(NULL, ZEND_TYPE_ASSERT, &name, &array);
5174
28
  opline->lineno = lineno;
5175
28
  opline->extended_value = (2 << 16) | IS_ARRAY;
5176
28
  const zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5177
28
  const Bucket *fbc_bucket = (const Bucket*)((uintptr_t)fbc_zv - XtOffsetOf(Bucket, val));
5178
28
  Z_EXTRA_P(CT_CONSTANT(opline->op1)) = fbc_bucket - CG(function_table)->arData;
5179
5180
  /* Initialize the result array. */
5181
28
  zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
5182
5183
  /* foreach loop starts here. */
5184
28
  znode key;
5185
5186
28
  uint32_t opnum_reset = get_next_op_number();
5187
28
  znode reset_node;
5188
28
  zend_emit_op(&reset_node, ZEND_FE_RESET_R, &array, NULL);
5189
28
  zend_begin_loop(ZEND_FE_FREE, &reset_node, false);
5190
28
  uint32_t opnum_fetch = get_next_op_number();
5191
28
  zend_emit_op_tmp(&key, ZEND_FE_FETCH_R, &reset_node, &value);
5192
5193
  /* loop body */
5194
28
  znode call_result;
5195
28
  switch (callback->kind) {
5196
28
    case ZEND_AST_CALL:
5197
28
      zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_CALL, callback->child[0], call_args));
5198
28
      break;
5199
0
    case ZEND_AST_STATIC_CALL:
5200
0
      zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_STATIC_CALL, callback->child[0], callback->child[1], call_args));
5201
0
      break;
5202
28
  }
5203
28
  opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT, &call_result, &key);
5204
28
  SET_NODE(opline->result, result);
5205
  /* end loop body */
5206
5207
28
  zend_emit_jump(opnum_fetch);
5208
5209
28
  uint32_t opnum_loop_end = get_next_op_number();
5210
28
  opline = &CG(active_op_array)->opcodes[opnum_reset];
5211
28
  opline->op2.opline_num = opnum_loop_end;
5212
28
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
5213
28
  opline->extended_value = opnum_loop_end;
5214
5215
28
  zend_end_loop(opnum_fetch, &reset_node);
5216
28
  zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
5217
5218
28
  return SUCCESS;
5219
28
}
5220
5221
static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type, uint32_t lineno) /* {{{ */
5222
105k
{
5223
105k
  if (zend_string_equals_literal(lcname, "strlen")) {
5224
704
    return zend_compile_func_strlen(result, args);
5225
105k
  } else if (zend_string_equals_literal(lcname, "is_null")) {
5226
300
    return zend_compile_func_typecheck(result, args, IS_NULL);
5227
104k
  } else if (zend_string_equals_literal(lcname, "is_bool")) {
5228
58
    return zend_compile_func_typecheck(result, args, _IS_BOOL);
5229
104k
  } else if (zend_string_equals_literal(lcname, "is_long")
5230
104k
    || zend_string_equals_literal(lcname, "is_int")
5231
104k
    || zend_string_equals_literal(lcname, "is_integer")
5232
104k
  ) {
5233
353
    return zend_compile_func_typecheck(result, args, IS_LONG);
5234
104k
  } else if (zend_string_equals_literal(lcname, "is_float")
5235
104k
    || zend_string_equals_literal(lcname, "is_double")
5236
104k
  ) {
5237
419
    return zend_compile_func_typecheck(result, args, IS_DOUBLE);
5238
103k
  } else if (zend_string_equals_literal(lcname, "is_string")) {
5239
84
    return zend_compile_func_typecheck(result, args, IS_STRING);
5240
103k
  } else if (zend_string_equals_literal(lcname, "is_array")) {
5241
234
    return zend_compile_func_typecheck(result, args, IS_ARRAY);
5242
103k
  } else if (zend_string_equals_literal(lcname, "is_object")) {
5243
123
    return zend_compile_func_typecheck(result, args, IS_OBJECT);
5244
103k
  } else if (zend_string_equals_literal(lcname, "is_resource")) {
5245
76
    return zend_compile_func_typecheck(result, args, IS_RESOURCE);
5246
103k
  } else if (zend_string_equals_literal(lcname, "is_scalar")) {
5247
191
    return zend_compile_func_is_scalar(result, args);
5248
103k
  } else if (zend_string_equals_literal(lcname, "boolval")) {
5249
190
    return zend_compile_func_cast(result, args, _IS_BOOL);
5250
103k
  } else if (zend_string_equals_literal(lcname, "intval")) {
5251
110
    return zend_compile_func_cast(result, args, IS_LONG);
5252
102k
  } else if (zend_string_equals_literal(lcname, "floatval")
5253
102k
    || zend_string_equals_literal(lcname, "doubleval")
5254
102k
  ) {
5255
390
    return zend_compile_func_cast(result, args, IS_DOUBLE);
5256
102k
  } else if (zend_string_equals_literal(lcname, "strval")) {
5257
306
    return zend_compile_func_cast(result, args, IS_STRING);
5258
102k
  } else if (zend_string_equals_literal(lcname, "defined")) {
5259
1.74k
    return zend_compile_func_defined(result, args);
5260
100k
  } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) {
5261
1.18k
    return zend_compile_func_chr(result, args);
5262
99.3k
  } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) {
5263
574
    return zend_compile_func_ord(result, args);
5264
98.7k
  } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
5265
1.74k
    return zend_compile_func_cufa(result, args, lcname, type);
5266
97.0k
  } else if (zend_string_equals_literal(lcname, "call_user_func")) {
5267
2.11k
    return zend_compile_func_cuf(result, args, lcname, type);
5268
94.9k
  } else if (zend_string_equals_literal(lcname, "in_array")) {
5269
2.20k
    return zend_compile_func_in_array(result, args);
5270
92.7k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_COUNT))
5271
91.9k
      || zend_string_equals_literal(lcname, "sizeof")) {
5272
889
    return zend_compile_func_count(result, args, lcname);
5273
91.8k
  } else if (zend_string_equals_literal(lcname, "get_class")) {
5274
1.13k
    return zend_compile_func_get_class(result, args);
5275
90.6k
  } else if (zend_string_equals_literal(lcname, "get_called_class")) {
5276
138
    return zend_compile_func_get_called_class(result, args);
5277
90.5k
  } else if (zend_string_equals_literal(lcname, "gettype")) {
5278
441
    return zend_compile_func_gettype(result, args);
5279
90.1k
  } else if (zend_string_equals_literal(lcname, "func_num_args")) {
5280
185
    return zend_compile_func_num_args(result, args);
5281
89.9k
  } else if (zend_string_equals_literal(lcname, "func_get_args")) {
5282
537
    return zend_compile_func_get_args(result, args);
5283
89.3k
  } else if (zend_string_equals_literal(lcname, "array_slice")) {
5284
1.66k
    return zend_compile_func_array_slice(result, args);
5285
87.7k
  } else if (zend_string_equals_literal(lcname, "array_key_exists")) {
5286
270
    return zend_compile_func_array_key_exists(result, args);
5287
87.4k
  } else if (zend_string_equals_literal(lcname, "sprintf")) {
5288
1.68k
    return zend_compile_func_sprintf(result, args);
5289
85.7k
  } else if (zend_string_equals_literal(lcname, "printf")) {
5290
2.57k
    return zend_compile_func_printf(result, args);
5291
83.1k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_CLONE))) {
5292
945
    return zend_compile_func_clone(result, args);
5293
82.2k
  } else if (zend_string_equals_literal(lcname, "array_map")) {
5294
316
    return zend_compile_func_array_map(result, args, lcname, lineno);
5295
81.9k
  } else {
5296
81.9k
    return FAILURE;
5297
81.9k
  }
5298
105k
}
5299
5300
static zend_result zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, const zend_function *fbc, uint32_t type, uint32_t lineno) /* {{{ */
5301
126k
{
5302
126k
  if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
5303
0
    return FAILURE;
5304
0
  }
5305
5306
126k
  if (fbc->type != ZEND_INTERNAL_FUNCTION) {
5307
    /* If the function is part of disabled_functions, it may be redeclared as a userland
5308
     * function with a different implementation. Don't use the VM builtin in that case. */
5309
19.7k
    return FAILURE;
5310
19.7k
  }
5311
5312
106k
  if (zend_args_contain_unpack_or_named(args)) {
5313
922
    return FAILURE;
5314
922
  }
5315
5316
105k
  if (zend_try_compile_special_func_ex(result, lcname, args, type, lineno) == SUCCESS) {
5317
15.4k
    return SUCCESS;
5318
15.4k
  }
5319
5320
90.3k
  return zend_compile_frameless_icall(result, args, fbc, type) != (uint32_t)-1 ? SUCCESS : FAILURE;
5321
105k
}
5322
5323
6
static const char *zend_get_cstring_from_property_hook_kind(zend_property_hook_kind kind) {
5324
6
  switch (kind) {
5325
1
    case ZEND_PROPERTY_HOOK_GET:
5326
1
      return "get";
5327
5
    case ZEND_PROPERTY_HOOK_SET:
5328
5
      return "set";
5329
6
    EMPTY_SWITCH_DEFAULT_CASE()
5330
6
  }
5331
6
}
5332
5333
static zend_string *zend_copy_unmangled_prop_name(zend_string *prop_name)
5334
529
{
5335
529
  if (ZSTR_VAL(prop_name)[0] != '\0') {
5336
429
    return zend_string_copy(prop_name);
5337
429
  } else {
5338
100
    const char *unmangled = zend_get_unmangled_property_name(prop_name);
5339
100
    return zend_string_init(unmangled, strlen(unmangled), /* persistent */ false);
5340
100
  }
5341
529
}
5342
5343
static bool zend_compile_parent_property_hook_call(znode *result, const zend_ast *ast, uint32_t type)
5344
14.6k
{
5345
14.6k
  ZEND_ASSERT(ast->kind == ZEND_AST_STATIC_CALL);
5346
5347
14.6k
  const zend_ast *class_ast = ast->child[0];
5348
14.6k
  zend_ast *method_ast = ast->child[1];
5349
5350
  /* Recognize parent::$prop::get() pattern. */
5351
14.6k
  if (class_ast->kind != ZEND_AST_STATIC_PROP
5352
2.02k
   || (class_ast->attr & ZEND_PARENTHESIZED_STATIC_PROP)
5353
1.92k
   || class_ast->child[0]->kind != ZEND_AST_ZVAL
5354
1.80k
   || Z_TYPE_P(zend_ast_get_zval(class_ast->child[0])) != IS_STRING
5355
1.80k
   || zend_get_class_fetch_type(zend_ast_get_str(class_ast->child[0])) != ZEND_FETCH_CLASS_PARENT
5356
1.13k
   || class_ast->child[1]->kind != ZEND_AST_ZVAL
5357
993
   || method_ast->kind != ZEND_AST_ZVAL
5358
944
   || Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING
5359
944
   || (!zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "get")
5360
14.0k
    && !zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "set"))) {
5361
14.0k
    return false;
5362
14.0k
  }
5363
5364
608
  zend_class_entry *ce = CG(active_class_entry);
5365
608
  if (!ce) {
5366
8
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"parent\" when no class scope is active");
5367
8
  }
5368
5369
600
  zend_ast *args_ast = ast->child[2];
5370
600
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
5371
6
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for parent property hook call");
5372
6
  }
5373
5374
594
  zval *property_hook_name_zv = zend_ast_get_zval(class_ast->child[1]);
5375
594
  zend_string *property_name = zval_get_string(property_hook_name_zv);
5376
594
  zend_string *hook_name = zend_ast_get_str(method_ast);
5377
594
  zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(hook_name);
5378
594
  ZEND_ASSERT(hook_kind != (uint32_t)-1);
5379
5380
594
  const zend_string *prop_info_name = CG(context).active_property_info_name;
5381
594
  if (!prop_info_name) {
5382
5
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() outside a property hook",
5383
5
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name));
5384
5
  }
5385
5386
589
  const char *unmangled_prop_name = zend_get_unmangled_property_name(prop_info_name);
5387
589
  if (!zend_string_equals_cstr(property_name, unmangled_prop_name, strlen(unmangled_prop_name))) {
5388
23
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property ($%s)",
5389
23
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), unmangled_prop_name);
5390
23
  }
5391
566
  if (hook_kind != CG(context).active_property_hook_kind) {
5392
6
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property hook (%s)",
5393
6
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), zend_get_cstring_from_property_hook_kind(CG(context).active_property_hook_kind));
5394
6
  }
5395
5396
560
  zend_op *opline = get_next_op();
5397
560
  opline->opcode = ZEND_INIT_PARENT_PROPERTY_HOOK_CALL;
5398
560
  opline->op1_type = IS_CONST;
5399
560
  opline->op1.constant = zend_add_literal_string(&property_name);
5400
560
  opline->op2.num = hook_kind;
5401
5402
560
  zend_compile_call_common(result, args_ast, NULL, zend_ast_get_lineno(method_ast), BP_VAR_R);
5403
5404
560
  return true;
5405
566
}
5406
5407
static void zend_compile_call(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
5408
1.45M
{
5409
1.45M
  zend_ast *name_ast = ast->child[0];
5410
1.45M
  zend_ast *args_ast = ast->child[1];
5411
1.45M
  bool is_callable_convert = args_ast->kind == ZEND_AST_CALLABLE_CONVERT;
5412
5413
1.45M
  znode name_node;
5414
5415
1.45M
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
5416
92.5k
    zend_compile_expr(&name_node, name_ast);
5417
92.5k
    zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno, type);
5418
92.5k
    return;
5419
92.5k
  }
5420
5421
1.35M
  {
5422
1.35M
    bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
5423
1.35M
    if (runtime_resolution) {
5424
1.13M
      if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")
5425
51.0k
          && !is_callable_convert) {
5426
50.7k
        zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL, ast->lineno, type);
5427
1.08M
      } else {
5428
1.08M
        zend_compile_ns_call(result, &name_node, args_ast, ast->lineno, type);
5429
1.08M
      }
5430
1.13M
      return;
5431
1.13M
    }
5432
1.35M
  }
5433
5434
223k
  {
5435
223k
    const zval *name = &name_node.u.constant;
5436
223k
    zend_string *lcname = zend_string_tolower(Z_STR_P(name));
5437
223k
    zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5438
223k
    const zend_function *fbc = fbc_zv ? Z_PTR_P(fbc_zv) : NULL;
5439
223k
    zend_op *opline;
5440
5441
    /* Special assert() handling should apply independently of compiler flags. */
5442
223k
    if (fbc && zend_string_equals_literal(lcname, "assert") && !is_callable_convert) {
5443
14.0k
      zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc, ast->lineno, type);
5444
14.0k
      zend_string_release(lcname);
5445
14.0k
      zval_ptr_dtor(&name_node.u.constant);
5446
14.0k
      return;
5447
14.0k
    }
5448
5449
209k
    if (!fbc
5450
126k
     || !fbc_is_finalized(fbc)
5451
126k
     || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
5452
82.2k
      zend_string_release_ex(lcname, 0);
5453
82.2k
      zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno, type);
5454
82.2k
      return;
5455
82.2k
    }
5456
5457
126k
    if (!is_callable_convert &&
5458
126k
        zend_try_compile_special_func(result, lcname,
5459
126k
        zend_ast_get_list(args_ast), fbc, type, ast->lineno) == SUCCESS
5460
126k
    ) {
5461
16.7k
      zend_string_release_ex(lcname, 0);
5462
16.7k
      zval_ptr_dtor(&name_node.u.constant);
5463
16.7k
      return;
5464
16.7k
    }
5465
5466
110k
    zval_ptr_dtor(&name_node.u.constant);
5467
110k
    ZVAL_NEW_STR(&name_node.u.constant, lcname);
5468
5469
110k
    opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
5470
110k
    opline->result.num = zend_alloc_cache_slot();
5471
5472
    /* Store offset to function from symbol table in op2.extra. */
5473
110k
    if (fbc->type == ZEND_INTERNAL_FUNCTION) {
5474
90.3k
      const Bucket *fbc_bucket = (const Bucket*)((uintptr_t)fbc_zv - XtOffsetOf(Bucket, val));
5475
90.3k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = fbc_bucket - CG(function_table)->arData;
5476
90.3k
    }
5477
5478
110k
    zend_compile_call_common(result, args_ast, fbc, ast->lineno, type);
5479
110k
  }
5480
110k
}
5481
/* }}} */
5482
5483
static void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5484
57.5k
{
5485
57.5k
  zend_ast *obj_ast = ast->child[0];
5486
57.5k
  zend_ast *method_ast = ast->child[1];
5487
57.5k
  zend_ast *args_ast = ast->child[2];
5488
5489
57.5k
  znode obj_node, method_node;
5490
57.5k
  zend_op *opline;
5491
57.5k
  const zend_function *fbc = NULL;
5492
57.5k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL;
5493
57.5k
  uint32_t short_circuiting_checkpoint = zend_short_circuiting_checkpoint();
5494
5495
57.5k
  if (is_this_fetch(obj_ast)) {
5496
1.43k
    if (this_guaranteed_exists()) {
5497
1.33k
      obj_node.op_type = IS_UNUSED;
5498
1.33k
    } else {
5499
92
      zend_emit_op_tmp(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
5500
92
    }
5501
1.43k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
5502
5503
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
5504
     * check for a nullsafe access. */
5505
56.1k
  } else {
5506
56.1k
    zend_short_circuiting_mark_inner(obj_ast);
5507
56.1k
    zend_compile_expr(&obj_node, obj_ast);
5508
56.1k
    if (nullsafe) {
5509
2.37k
      zend_emit_jmp_null(&obj_node, type);
5510
2.37k
    }
5511
56.1k
  }
5512
5513
57.5k
  zend_compile_expr(&method_node, method_ast);
5514
57.5k
  opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
5515
5516
57.5k
  if (method_node.op_type == IS_CONST) {
5517
56.8k
    if (Z_TYPE(method_node.u.constant) != IS_STRING) {
5518
12
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5519
12
    }
5520
5521
56.7k
    opline->op2_type = IS_CONST;
5522
56.7k
    opline->op2.constant = zend_add_func_name_literal(
5523
56.7k
      Z_STR(method_node.u.constant));
5524
56.7k
    opline->result.num = zend_alloc_cache_slots(2);
5525
56.7k
  } else {
5526
781
    SET_NODE(opline->op2, &method_node);
5527
781
  }
5528
5529
  /* Check if this calls a known method on $this */
5530
57.5k
  if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST &&
5531
1.27k
      CG(active_class_entry) && zend_is_scope_known()) {
5532
934
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5533
934
    fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname);
5534
5535
    /* We only know the exact method that is being called if it is either private or final.
5536
     * Otherwise an overriding method in a child class may be called. */
5537
934
    if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) {
5538
283
      fbc = NULL;
5539
283
    }
5540
934
  }
5541
5542
57.5k
  if (zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast), type)) {
5543
336
    if (short_circuiting_checkpoint != zend_short_circuiting_checkpoint()) {
5544
13
      zend_error_noreturn(E_COMPILE_ERROR,
5545
13
        "Cannot combine nullsafe operator with Closure creation");
5546
13
    }
5547
336
  }
5548
57.5k
}
5549
/* }}} */
5550
5551
static bool zend_is_constructor(const zend_string *name) /* {{{ */
5552
14.0k
{
5553
14.0k
  return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
5554
14.0k
}
5555
/* }}} */
5556
5557
static bool is_func_accessible(const zend_function *fbc)
5558
37.8k
{
5559
37.8k
  if ((fbc->common.fn_flags & ZEND_ACC_PUBLIC) || fbc->common.scope == CG(active_class_entry)) {
5560
37.7k
    return true;
5561
37.7k
  }
5562
5563
153
  if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE)
5564
80
    && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED)
5565
80
    && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED))
5566
21
    && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) {
5567
0
    return true;
5568
0
  }
5569
5570
153
  return false;
5571
153
}
5572
5573
static const zend_function *zend_get_compatible_func_or_null(const zend_class_entry *ce, zend_string *lcname) /* {{{ */
5574
4.25k
{
5575
4.25k
  const zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname);
5576
5577
4.25k
  if (!fbc || is_func_accessible(fbc)) {
5578
4.10k
    return fbc;
5579
4.10k
  }
5580
5581
146
  return NULL;
5582
4.25k
}
5583
/* }}} */
5584
5585
static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5586
14.6k
{
5587
14.6k
  zend_ast *class_ast = ast->child[0];
5588
14.6k
  zend_ast *method_ast = ast->child[1];
5589
14.6k
  zend_ast *args_ast = ast->child[2];
5590
5591
14.6k
  znode class_node, method_node;
5592
14.6k
  zend_op *opline;
5593
14.6k
  const zend_function *fbc = NULL;
5594
5595
14.6k
  if (zend_compile_parent_property_hook_call(result, ast, type)) {
5596
560
    return;
5597
560
  }
5598
5599
14.0k
  zend_short_circuiting_mark_inner(class_ast);
5600
14.0k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5601
5602
14.0k
  zend_compile_expr(&method_node, method_ast);
5603
5604
14.0k
  if (method_node.op_type == IS_CONST) {
5605
13.1k
    zval *name = &method_node.u.constant;
5606
13.1k
    if (Z_TYPE_P(name) != IS_STRING) {
5607
3
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5608
3
    }
5609
13.1k
    if (zend_is_constructor(Z_STR_P(name))) {
5610
402
      zval_ptr_dtor(name);
5611
402
      method_node.op_type = IS_UNUSED;
5612
402
    }
5613
13.1k
  }
5614
5615
14.0k
  opline = get_next_op();
5616
14.0k
  opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
5617
5618
14.0k
  zend_set_class_name_op1(opline, &class_node);
5619
5620
14.0k
  if (method_node.op_type == IS_CONST) {
5621
12.7k
    opline->op2_type = IS_CONST;
5622
12.7k
    opline->op2.constant = zend_add_func_name_literal(
5623
12.7k
      Z_STR(method_node.u.constant));
5624
12.7k
    opline->result.num = zend_alloc_cache_slots(2);
5625
12.7k
  } else {
5626
1.36k
    if (opline->op1_type == IS_CONST) {
5627
467
      opline->result.num = zend_alloc_cache_slot();
5628
467
    }
5629
1.36k
    SET_NODE(opline->op2, &method_node);
5630
1.36k
  }
5631
5632
  /* Check if we already know which method we're calling */
5633
14.0k
  if (opline->op2_type == IS_CONST) {
5634
12.7k
    zend_class_entry *ce = NULL;
5635
12.7k
    if (opline->op1_type == IS_CONST) {
5636
7.21k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5637
7.21k
      ce = zend_hash_find_ptr(CG(class_table), lcname);
5638
7.21k
      if (ce) {
5639
3.96k
        if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5640
0
          ce = NULL;
5641
0
        }
5642
3.96k
      } else if (CG(active_class_entry)
5643
482
          && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5644
98
        ce = CG(active_class_entry);
5645
98
      }
5646
7.21k
    } else if (opline->op1_type == IS_UNUSED
5647
1.31k
        && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5648
359
        && zend_is_scope_known()) {
5649
191
      ce = CG(active_class_entry);
5650
191
    }
5651
12.7k
    if (ce) {
5652
4.25k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5653
4.25k
      fbc = zend_get_compatible_func_or_null(ce, lcname);
5654
4.25k
    }
5655
12.7k
  }
5656
5657
14.0k
  zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast), type);
5658
14.0k
}
5659
/* }}} */
5660
5661
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel);
5662
5663
static void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
5664
64.5k
{
5665
64.5k
  zend_ast *class_ast = ast->child[0];
5666
64.5k
  zend_ast *args_ast = ast->child[1];
5667
5668
64.5k
  znode class_node, ctor_result;
5669
64.5k
  zend_op *opline;
5670
5671
64.5k
  if (class_ast->kind == ZEND_AST_CLASS) {
5672
    /* anon class declaration */
5673
1.64k
    zend_compile_class_decl(&class_node, class_ast, false);
5674
62.9k
  } else {
5675
62.9k
    zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5676
62.9k
  }
5677
5678
64.5k
  opline = zend_emit_op_tmp(result, ZEND_NEW, NULL, NULL);
5679
5680
64.5k
  zend_set_class_name_op1(opline, &class_node);
5681
5682
64.5k
  if (opline->op1_type == IS_CONST) {
5683
61.9k
    opline->op2.num = zend_alloc_cache_slot();
5684
61.9k
  }
5685
5686
64.5k
  zend_class_entry *ce = NULL;
5687
64.5k
  if (opline->op1_type == IS_CONST) {
5688
61.9k
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5689
61.9k
    ce = zend_hash_find_ptr(CG(class_table), lcname);
5690
61.9k
    if (ce) {
5691
45.6k
      if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5692
0
        ce = NULL;
5693
0
      }
5694
45.6k
    } else if (CG(active_class_entry)
5695
1.12k
        && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5696
446
      ce = CG(active_class_entry);
5697
446
    }
5698
61.9k
  } else if (opline->op1_type == IS_UNUSED
5699
300
      && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5700
132
      && zend_is_scope_known()) {
5701
82
    ce = CG(active_class_entry);
5702
82
  }
5703
5704
5705
64.5k
  const zend_function *fbc = NULL;
5706
64.5k
  if (ce
5707
46.2k
      && ce->default_object_handlers->get_constructor == zend_std_get_constructor
5708
46.1k
      && ce->constructor
5709
34.1k
      && is_func_accessible(ce->constructor)) {
5710
34.1k
    fbc = ce->constructor;
5711
34.1k
  }
5712
5713
64.5k
  zend_compile_call_common(&ctor_result, args_ast, fbc, ast->lineno, BP_VAR_R);
5714
64.5k
  zend_do_free(&ctor_result);
5715
64.5k
}
5716
/* }}} */
5717
5718
static void zend_compile_global_var(zend_ast *ast) /* {{{ */
5719
2.27k
{
5720
2.27k
  zend_ast *var_ast = ast->child[0];
5721
2.27k
  zend_ast *name_ast = var_ast->child[0];
5722
5723
2.27k
  znode name_node, result;
5724
5725
2.27k
  zend_compile_expr(&name_node, name_ast);
5726
2.27k
  if (name_node.op_type == IS_CONST) {
5727
1.88k
    convert_to_string(&name_node.u.constant);
5728
1.88k
  }
5729
5730
  // TODO(GLOBALS) Forbid "global $GLOBALS"?
5731
2.27k
  if (is_this_fetch(var_ast)) {
5732
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable");
5733
2.27k
  } else if (zend_try_compile_cv(&result, var_ast, BP_VAR_R) == SUCCESS) {
5734
1.72k
    zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
5735
1.72k
    opline->extended_value = zend_alloc_cache_slot();
5736
1.72k
  } else {
5737
    /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W
5738
     * to not free the name_node operand, so it can be reused in the following
5739
     * ASSIGN_REF, which then frees it. */
5740
544
    zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
5741
544
    opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
5742
5743
544
    if (name_node.op_type == IS_CONST) {
5744
156
      zend_string_addref(Z_STR(name_node.u.constant));
5745
156
    }
5746
5747
544
    zend_emit_assign_ref_znode(
5748
544
      zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)),
5749
544
      &result
5750
544
    );
5751
544
  }
5752
2.27k
}
5753
/* }}} */
5754
5755
static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */
5756
93.8k
{
5757
93.8k
  zend_op *opline;
5758
93.8k
  if (!CG(active_op_array)->static_variables) {
5759
0
    if (CG(active_op_array)->scope) {
5760
0
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5761
0
    }
5762
0
    CG(active_op_array)->static_variables = zend_new_array(8);
5763
0
  }
5764
5765
93.8k
  value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value);
5766
5767
93.8k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5768
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5769
0
  }
5770
5771
93.8k
  opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL);
5772
93.8k
  opline->op1_type = IS_CV;
5773
93.8k
  opline->op1.var = lookup_cv(var_name);
5774
93.8k
  opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode;
5775
93.8k
}
5776
/* }}} */
5777
5778
static void zend_compile_static_var(zend_ast *ast) /* {{{ */
5779
31.3k
{
5780
31.3k
  zend_ast *var_ast = ast->child[0];
5781
31.3k
  zend_string *var_name = zend_ast_get_str(var_ast);
5782
5783
31.3k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5784
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5785
5
  }
5786
5787
31.3k
  if (!CG(active_op_array)->static_variables) {
5788
30.4k
    if (CG(active_op_array)->scope) {
5789
29.4k
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5790
29.4k
    }
5791
30.4k
    CG(active_op_array)->static_variables = zend_new_array(8);
5792
30.4k
  }
5793
5794
31.3k
  if (zend_hash_exists(CG(active_op_array)->static_variables, var_name)) {
5795
11
    zend_error_noreturn_unchecked(E_COMPILE_ERROR, "Duplicate declaration of static variable $%S", var_name);
5796
11
  }
5797
5798
31.3k
  zend_eval_const_expr(&ast->child[1]);
5799
31.3k
  zend_ast *value_ast = ast->child[1];
5800
5801
31.3k
  if (!value_ast || value_ast->kind == ZEND_AST_ZVAL) {
5802
31.0k
    zval *value_zv = value_ast
5803
31.0k
      ? zend_ast_get_zval(value_ast)
5804
31.0k
      : &EG(uninitialized_zval);
5805
31.0k
    Z_TRY_ADDREF_P(value_zv);
5806
31.0k
    zend_compile_static_var_common(var_name, value_zv, ZEND_BIND_REF);
5807
31.0k
  } else {
5808
354
    zend_op *opline;
5809
5810
354
    zval *placeholder_ptr = zend_hash_update(CG(active_op_array)->static_variables, var_name, &EG(uninitialized_zval));
5811
354
    uint32_t placeholder_offset = (uint32_t)((char*)placeholder_ptr - (char*)CG(active_op_array)->static_variables->arData);
5812
5813
354
    uint32_t static_def_jmp_opnum = get_next_op_number();
5814
354
    opline = zend_emit_op(NULL, ZEND_BIND_INIT_STATIC_OR_JMP, NULL, NULL);
5815
354
    opline->op1_type = IS_CV;
5816
354
    opline->op1.var = lookup_cv(var_name);
5817
354
    opline->extended_value = placeholder_offset;
5818
5819
354
    znode expr;
5820
354
    zend_compile_expr(&expr, value_ast);
5821
5822
354
    opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, &expr);
5823
354
    opline->op1_type = IS_CV;
5824
354
    opline->op1.var = lookup_cv(var_name);
5825
354
    opline->extended_value = placeholder_offset | ZEND_BIND_REF;
5826
5827
354
    zend_update_jump_target_to_next(static_def_jmp_opnum);
5828
354
  }
5829
31.3k
}
5830
/* }}} */
5831
5832
static void zend_compile_unset(const zend_ast *ast) /* {{{ */
5833
5.34k
{
5834
5.34k
  zend_ast *var_ast = ast->child[0];
5835
5.34k
  znode var_node;
5836
5.34k
  zend_op *opline;
5837
5838
5.34k
  zend_ensure_writable_variable(var_ast);
5839
5840
5.34k
  if (is_global_var_fetch(var_ast)) {
5841
266
    if (!var_ast->child[1]) {
5842
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
5843
5
    }
5844
5845
261
    zend_compile_expr(&var_node, var_ast->child[1]);
5846
261
    if (var_node.op_type == IS_CONST) {
5847
236
      convert_to_string(&var_node.u.constant);
5848
236
    }
5849
5850
261
    opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL);
5851
261
    opline->extended_value = ZEND_FETCH_GLOBAL;
5852
261
    return;
5853
266
  }
5854
5855
5.08k
  switch (var_ast->kind) {
5856
2.51k
    case ZEND_AST_VAR:
5857
2.51k
      if (is_this_fetch(var_ast)) {
5858
8
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this");
5859
2.50k
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_UNSET) == SUCCESS) {
5860
2.33k
        opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL);
5861
2.33k
      } else {
5862
167
        opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, false);
5863
167
        opline->opcode = ZEND_UNSET_VAR;
5864
167
      }
5865
2.50k
      return;
5866
2.50k
    case ZEND_AST_DIM:
5867
1.60k
      opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET, /* by_ref */ false);
5868
1.60k
      opline->opcode = ZEND_UNSET_DIM;
5869
1.60k
      return;
5870
941
    case ZEND_AST_PROP:
5871
941
    case ZEND_AST_NULLSAFE_PROP:
5872
941
      opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, false);
5873
941
      opline->opcode = ZEND_UNSET_OBJ;
5874
941
      return;
5875
21
    case ZEND_AST_STATIC_PROP:
5876
21
      opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, false, false);
5877
21
      opline->opcode = ZEND_UNSET_STATIC_PROP;
5878
21
      return;
5879
5.08k
    EMPTY_SWITCH_DEFAULT_CASE()
5880
5.08k
  }
5881
5.08k
}
5882
/* }}} */
5883
5884
static bool zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */
5885
34.2k
{
5886
34.2k
  const zend_loop_var *base;
5887
34.2k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5888
5889
34.2k
  if (!loop_var) {
5890
1.35k
    return 1;
5891
1.35k
  }
5892
32.8k
  base = zend_stack_base(&CG(loop_var_stack));
5893
39.6k
  for (; loop_var >= base; loop_var--) {
5894
38.1k
    if (loop_var->opcode == ZEND_FAST_CALL) {
5895
2.07k
      zend_op *opline = get_next_op();
5896
5897
2.07k
      opline->opcode = ZEND_FAST_CALL;
5898
2.07k
      opline->result_type = IS_TMP_VAR;
5899
2.07k
      opline->result.var = loop_var->var_num;
5900
2.07k
      if (return_value) {
5901
1.05k
        SET_NODE(opline->op2, return_value);
5902
1.05k
      }
5903
2.07k
      opline->op1.num = loop_var->try_catch_offset;
5904
36.0k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5905
1.33k
      zend_op *opline = get_next_op();
5906
1.33k
      opline->opcode = ZEND_DISCARD_EXCEPTION;
5907
1.33k
      opline->op1_type = IS_TMP_VAR;
5908
1.33k
      opline->op1.var = loop_var->var_num;
5909
34.7k
    } else if (loop_var->opcode == ZEND_RETURN) {
5910
      /* Stack separator */
5911
30.1k
      break;
5912
30.1k
    } else if (depth <= 1) {
5913
1.20k
      return 1;
5914
3.36k
    } else if (loop_var->opcode == ZEND_NOP) {
5915
      /* Loop doesn't have freeable variable */
5916
1.87k
      depth--;
5917
1.87k
    } else {
5918
1.49k
      zend_op *opline;
5919
5920
1.49k
      ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR));
5921
1.49k
      opline = get_next_op();
5922
1.49k
      opline->opcode = loop_var->opcode;
5923
1.49k
      opline->op1_type = loop_var->var_type;
5924
1.49k
      opline->op1.var = loop_var->var_num;
5925
1.49k
      opline->extended_value = ZEND_FREE_ON_RETURN;
5926
1.49k
      depth--;
5927
1.49k
      }
5928
38.1k
  }
5929
31.6k
  return (depth == 0);
5930
32.8k
}
5931
/* }}} */
5932
5933
static bool zend_handle_loops_and_finally(znode *return_value) /* {{{ */
5934
32.9k
{
5935
32.9k
  return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value);
5936
32.9k
}
5937
/* }}} */
5938
5939
static bool zend_has_finally_ex(zend_long depth) /* {{{ */
5940
1.38k
{
5941
1.38k
  const zend_loop_var *base;
5942
1.38k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5943
5944
1.38k
  if (!loop_var) {
5945
388
    return 0;
5946
388
  }
5947
995
  base = zend_stack_base(&CG(loop_var_stack));
5948
1.75k
  for (; loop_var >= base; loop_var--) {
5949
1.59k
    if (loop_var->opcode == ZEND_FAST_CALL) {
5950
365
      return 1;
5951
1.23k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5952
629
    } else if (loop_var->opcode == ZEND_RETURN) {
5953
      /* Stack separator */
5954
466
      return 0;
5955
466
    } else if (depth <= 1) {
5956
0
      return 0;
5957
135
    } else {
5958
135
      depth--;
5959
135
      }
5960
1.59k
  }
5961
164
  return 0;
5962
995
}
5963
/* }}} */
5964
5965
static bool zend_has_finally(void) /* {{{ */
5966
1.38k
{
5967
1.38k
  return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1);
5968
1.38k
}
5969
/* }}} */
5970
5971
static void zend_compile_return(const zend_ast *ast) /* {{{ */
5972
31.8k
{
5973
31.8k
  zend_ast *expr_ast = ast->child[0];
5974
31.8k
  bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0;
5975
31.8k
  bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
5976
5977
31.8k
  znode expr_node;
5978
31.8k
  zend_op *opline;
5979
5980
31.8k
  if (is_generator) {
5981
    /* For generators the by-ref flag refers to yields, not returns */
5982
1.92k
    by_ref = false;
5983
1.92k
  }
5984
5985
31.8k
  if (!expr_ast) {
5986
903
    expr_node.op_type = IS_CONST;
5987
903
    ZVAL_NULL(&expr_node.u.constant);
5988
30.9k
  } else if (by_ref && zend_is_variable_or_call(expr_ast)) {
5989
1.90k
    zend_assert_not_short_circuited(expr_ast);
5990
1.90k
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
5991
29.0k
  } else {
5992
29.0k
    zend_compile_expr(&expr_node, expr_ast);
5993
29.0k
  }
5994
5995
31.8k
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
5996
2.05k
   && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
5997
1.38k
   && zend_has_finally()) {
5998
    /* Copy return value into temporary VAR to avoid modification in finally code */
5999
365
    if (by_ref) {
6000
150
      zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
6001
215
    } else {
6002
215
      zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL);
6003
215
    }
6004
365
  }
6005
6006
  /* Generator return types are handled separately */
6007
31.8k
  if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
6008
4.77k
    zend_emit_return_type_check(
6009
4.77k
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
6010
4.77k
  }
6011
6012
31.8k
  uint32_t opnum_before_finally = get_next_op_number();
6013
6014
31.8k
  zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
6015
6016
  /* Content of reference might have changed in finally, repeat type check. */
6017
31.8k
  if (by_ref
6018
   /* Check if any opcodes were emitted since the last return type check. */
6019
3.49k
   && opnum_before_finally != get_next_op_number()
6020
889
   && !is_generator
6021
889
   && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
6022
17
    zend_emit_return_type_check(
6023
17
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
6024
17
  }
6025
6026
31.8k
  opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
6027
31.8k
    &expr_node, NULL);
6028
6029
31.8k
  if (by_ref && expr_ast) {
6030
3.05k
    if (zend_is_call(expr_ast)) {
6031
295
      opline->extended_value = ZEND_RETURNS_FUNCTION;
6032
2.76k
    } else if (!zend_is_variable(expr_ast) || zend_ast_is_short_circuited(expr_ast)) {
6033
1.16k
      opline->extended_value = ZEND_RETURNS_VALUE;
6034
1.16k
    }
6035
3.05k
  }
6036
31.8k
}
6037
/* }}} */
6038
6039
static void zend_compile_void_cast(znode *result, const zend_ast *ast)
6040
419
{
6041
419
  zend_ast *expr_ast = ast->child[0];
6042
419
  znode expr_node;
6043
419
  zend_op *opline;
6044
6045
419
  zend_compile_expr(&expr_node, expr_ast);
6046
6047
419
  switch (expr_node.op_type) {
6048
362
    case IS_TMP_VAR:
6049
362
    case IS_VAR:
6050
362
      opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6051
362
      opline->extended_value = ZEND_FREE_VOID_CAST;
6052
362
      break;
6053
46
    case IS_CONST:
6054
46
      zend_do_free(&expr_node);
6055
46
      break;
6056
419
  }
6057
419
}
6058
6059
static void zend_compile_echo(const zend_ast *ast) /* {{{ */
6060
910k
{
6061
910k
  zend_op *opline;
6062
910k
  zend_ast *expr_ast = ast->child[0];
6063
6064
910k
  znode expr_node;
6065
910k
  zend_compile_expr(&expr_node, expr_ast);
6066
6067
910k
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
6068
910k
  opline->extended_value = 0;
6069
910k
}
6070
/* }}} */
6071
6072
static void zend_compile_throw(znode *result, const zend_ast *ast) /* {{{ */
6073
3.22k
{
6074
3.22k
  zend_ast *expr_ast = ast->child[0];
6075
6076
3.22k
  znode expr_node;
6077
3.22k
  zend_compile_expr(&expr_node, expr_ast);
6078
6079
3.22k
  zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
6080
3.22k
  if (result) {
6081
    /* Mark this as an "expression throw" for opcache. */
6082
711
    opline->extended_value = ZEND_THROW_IS_EXPR;
6083
711
    result->op_type = IS_CONST;
6084
711
    ZVAL_TRUE(&result->u.constant);
6085
711
  }
6086
3.22k
}
6087
/* }}} */
6088
6089
static void zend_compile_break_continue(const zend_ast *ast) /* {{{ */
6090
1.33k
{
6091
1.33k
  zend_ast *depth_ast = ast->child[0];
6092
6093
1.33k
  zend_op *opline;
6094
1.33k
  zend_long depth;
6095
6096
1.33k
  ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
6097
6098
1.33k
  if (depth_ast) {
6099
248
    const zval *depth_zv;
6100
248
    if (depth_ast->kind != ZEND_AST_ZVAL) {
6101
15
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand "
6102
15
        "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
6103
15
    }
6104
6105
233
    depth_zv = zend_ast_get_zval(depth_ast);
6106
233
    if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
6107
7
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers",
6108
7
        ast->kind == ZEND_AST_BREAK ? "break" : "continue");
6109
7
    }
6110
6111
226
    depth = Z_LVAL_P(depth_zv);
6112
1.08k
  } else {
6113
1.08k
    depth = 1;
6114
1.08k
  }
6115
6116
1.30k
  if (CG(context).current_brk_cont == -1) {
6117
26
    zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
6118
26
      ast->kind == ZEND_AST_BREAK ? "break" : "continue");
6119
1.28k
  } else {
6120
1.28k
    if (!zend_handle_loops_and_finally_ex(depth, NULL)) {
6121
76
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s",
6122
76
        ast->kind == ZEND_AST_BREAK ? "break" : "continue",
6123
76
        depth, depth == 1 ? "" : "s");
6124
76
    }
6125
1.28k
  }
6126
6127
1.20k
  if (ast->kind == ZEND_AST_CONTINUE) {
6128
539
    int d, cur = CG(context).current_brk_cont;
6129
601
    for (d = depth - 1; d > 0; d--) {
6130
62
      cur = CG(context).brk_cont_array[cur].parent;
6131
62
      ZEND_ASSERT(cur != -1);
6132
62
    }
6133
6134
539
    if (CG(context).brk_cont_array[cur].is_switch) {
6135
152
      if (depth == 1) {
6136
129
        if (CG(context).brk_cont_array[cur].parent == -1) {
6137
32
          zend_error(E_WARNING,
6138
32
            "\"continue\" targeting switch is equivalent to \"break\"");
6139
97
        } else {
6140
97
          zend_error(E_WARNING,
6141
97
            "\"continue\" targeting switch is equivalent to \"break\". " \
6142
97
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
6143
97
            depth + 1);
6144
97
        }
6145
129
      } else {
6146
23
        if (CG(context).brk_cont_array[cur].parent == -1) {
6147
11
          zend_error(E_WARNING,
6148
11
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"",
6149
11
            depth, depth);
6150
12
        } else {
6151
12
          zend_error(E_WARNING,
6152
12
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
6153
12
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
6154
12
            depth, depth, depth + 1);
6155
12
        }
6156
23
      }
6157
152
    }
6158
539
  }
6159
6160
1.20k
  opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
6161
1.20k
  opline->op1.num = CG(context).current_brk_cont;
6162
1.20k
  opline->op2.num = depth;
6163
1.20k
}
6164
/* }}} */
6165
6166
void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */
6167
980
{
6168
980
  zend_label *dest;
6169
980
  int remove_oplines = opline->op1.num;
6170
980
  zval *label;
6171
980
  uint32_t opnum = opline - op_array->opcodes;
6172
6173
980
  label = CT_CONSTANT_EX(op_array, opline->op2.constant);
6174
980
  if (CG(context).labels == NULL ||
6175
964
      (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL
6176
980
  ) {
6177
56
    CG(in_compilation) = 1;
6178
56
    CG(active_op_array) = op_array;
6179
56
    CG(zend_lineno) = opline->lineno;
6180
56
    zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
6181
56
  }
6182
6183
924
  zval_ptr_dtor_str(label);
6184
924
  ZVAL_NULL(label);
6185
6186
924
  uint32_t current = opline->extended_value;
6187
2.37k
  for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) {
6188
1.46k
    if (current == -1) {
6189
11
      CG(in_compilation) = 1;
6190
11
      CG(active_op_array) = op_array;
6191
11
      CG(zend_lineno) = opline->lineno;
6192
11
      zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
6193
11
    }
6194
1.45k
    if (CG(context).brk_cont_array[current].start >= 0) {
6195
890
      remove_oplines--;
6196
890
    }
6197
1.45k
  }
6198
6199
1.83k
  for (current = 0; current < op_array->last_try_catch; ++current) {
6200
1.00k
    const zend_try_catch_element *elem = &op_array->try_catch_array[current];
6201
1.00k
    if (elem->try_op > opnum) {
6202
91
      break;
6203
91
    }
6204
918
    if (elem->finally_op && opnum < elem->finally_op - 1
6205
599
      && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op)
6206
918
    ) {
6207
366
      remove_oplines--;
6208
366
    }
6209
918
  }
6210
6211
913
  opline->opcode = ZEND_JMP;
6212
913
  SET_UNUSED(opline->op1);
6213
913
  SET_UNUSED(opline->op2);
6214
913
  SET_UNUSED(opline->result);
6215
913
  opline->op1.opline_num = dest->opline_num;
6216
913
  opline->extended_value = 0;
6217
6218
913
  ZEND_ASSERT(remove_oplines >= 0);
6219
1.20k
  while (remove_oplines--) {
6220
289
    opline--;
6221
289
    MAKE_NOP(opline);
6222
289
    ZEND_VM_SET_OPCODE_HANDLER(opline);
6223
289
  }
6224
913
}
6225
/* }}} */
6226
6227
static void zend_compile_goto(const zend_ast *ast) /* {{{ */
6228
1.26k
{
6229
1.26k
  zend_ast *label_ast = ast->child[0];
6230
1.26k
  znode label_node;
6231
1.26k
  zend_op *opline;
6232
6233
1.26k
  zend_compile_expr(&label_node, label_ast);
6234
6235
  /* Label resolution and unwinding adjustments happen in pass two. */
6236
1.26k
  uint32_t opnum_start = get_next_op_number();
6237
1.26k
  zend_handle_loops_and_finally(NULL);
6238
1.26k
  opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
6239
1.26k
  opline->op1.num = get_next_op_number() - opnum_start - 1;
6240
1.26k
  opline->extended_value = CG(context).current_brk_cont;
6241
1.26k
}
6242
/* }}} */
6243
6244
static void zend_compile_label(const zend_ast *ast) /* {{{ */
6245
2.59k
{
6246
2.59k
  zend_string *label = zend_ast_get_str(ast->child[0]);
6247
2.59k
  zend_label dest;
6248
6249
2.59k
  if (!CG(context).labels) {
6250
1.86k
    ALLOC_HASHTABLE(CG(context).labels);
6251
1.86k
    zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0);
6252
1.86k
  }
6253
6254
2.59k
  dest.brk_cont = CG(context).current_brk_cont;
6255
2.59k
  dest.opline_num = get_next_op_number();
6256
6257
2.59k
  if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
6258
42
    zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
6259
42
  }
6260
2.59k
}
6261
/* }}} */
6262
6263
static void zend_compile_while(const zend_ast *ast) /* {{{ */
6264
2.59k
{
6265
2.59k
  zend_ast *cond_ast = ast->child[0];
6266
2.59k
  zend_ast *stmt_ast = ast->child[1];
6267
2.59k
  znode cond_node;
6268
2.59k
  uint32_t opnum_start, opnum_jmp, opnum_cond;
6269
6270
2.59k
  opnum_jmp = zend_emit_jump(0);
6271
6272
2.59k
  zend_begin_loop(ZEND_NOP, NULL, false);
6273
6274
2.59k
  opnum_start = get_next_op_number();
6275
2.59k
  zend_compile_stmt(stmt_ast);
6276
6277
2.59k
  opnum_cond = get_next_op_number();
6278
2.59k
  zend_update_jump_target(opnum_jmp, opnum_cond);
6279
2.59k
  zend_compile_expr(&cond_node, cond_ast);
6280
6281
2.59k
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6282
6283
2.59k
  zend_end_loop(opnum_cond, NULL);
6284
2.59k
}
6285
/* }}} */
6286
6287
static void zend_compile_do_while(const zend_ast *ast) /* {{{ */
6288
829
{
6289
829
  zend_ast *stmt_ast = ast->child[0];
6290
829
  zend_ast *cond_ast = ast->child[1];
6291
6292
829
  znode cond_node;
6293
829
  uint32_t opnum_start, opnum_cond;
6294
6295
829
  zend_begin_loop(ZEND_NOP, NULL, false);
6296
6297
829
  opnum_start = get_next_op_number();
6298
829
  zend_compile_stmt(stmt_ast);
6299
6300
829
  opnum_cond = get_next_op_number();
6301
829
  zend_compile_expr(&cond_node, cond_ast);
6302
6303
829
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6304
6305
829
  zend_end_loop(opnum_cond, NULL);
6306
829
}
6307
/* }}} */
6308
6309
static void zend_compile_for_expr_list(znode *result, zend_ast *ast) /* {{{ */
6310
28.0k
{
6311
28.0k
  const zend_ast_list *list;
6312
28.0k
  uint32_t i;
6313
6314
28.0k
  result->op_type = IS_CONST;
6315
28.0k
  ZVAL_TRUE(&result->u.constant);
6316
6317
28.0k
  if (!ast) {
6318
6.32k
    return;
6319
6.32k
  }
6320
6321
21.7k
  list = zend_ast_get_list(ast);
6322
44.9k
  for (i = 0; i < list->children; ++i) {
6323
23.1k
    zend_ast *expr_ast = list->child[i];
6324
6325
23.1k
    zend_do_free(result);
6326
23.1k
    if (expr_ast->kind == ZEND_AST_CAST_VOID) {
6327
118
      zend_compile_void_cast(NULL, expr_ast);
6328
118
      result->op_type = IS_CONST;
6329
118
      ZVAL_NULL(&result->u.constant);
6330
23.0k
    } else {
6331
23.0k
      zend_compile_expr(result, expr_ast);
6332
23.0k
    }
6333
23.1k
  }
6334
21.7k
}
6335
/* }}} */
6336
6337
static void zend_compile_for(const zend_ast *ast) /* {{{ */
6338
9.40k
{
6339
9.40k
  zend_ast *init_ast = ast->child[0];
6340
9.40k
  zend_ast *cond_ast = ast->child[1];
6341
9.40k
  zend_ast *loop_ast = ast->child[2];
6342
9.40k
  zend_ast *stmt_ast = ast->child[3];
6343
6344
9.40k
  znode result;
6345
9.40k
  uint32_t opnum_start, opnum_jmp, opnum_loop;
6346
6347
9.40k
  zend_compile_for_expr_list(&result, init_ast);
6348
9.40k
  zend_do_free(&result);
6349
6350
9.40k
  opnum_jmp = zend_emit_jump(0);
6351
6352
9.40k
  zend_begin_loop(ZEND_NOP, NULL, false);
6353
6354
9.40k
  opnum_start = get_next_op_number();
6355
9.40k
  zend_compile_stmt(stmt_ast);
6356
6357
9.40k
  opnum_loop = get_next_op_number();
6358
9.40k
  zend_compile_for_expr_list(&result, loop_ast);
6359
9.40k
  zend_do_free(&result);
6360
6361
9.40k
  zend_update_jump_target_to_next(opnum_jmp);
6362
9.40k
  zend_compile_for_expr_list(&result, cond_ast);
6363
9.40k
  zend_do_extended_stmt(NULL);
6364
6365
9.40k
  zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
6366
6367
9.40k
  zend_end_loop(opnum_loop, NULL);
6368
9.40k
}
6369
/* }}} */
6370
6371
static void zend_compile_foreach(zend_ast *ast) /* {{{ */
6372
17.2k
{
6373
17.2k
  zend_ast *expr_ast = ast->child[0];
6374
17.2k
  zend_ast *value_ast = ast->child[1];
6375
17.2k
  zend_ast *key_ast = ast->child[2];
6376
17.2k
  zend_ast *stmt_ast = ast->child[3];
6377
17.2k
  bool by_ref = value_ast->kind == ZEND_AST_REF;
6378
17.2k
  bool is_variable = (zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast))
6379
3.73k
    || zend_is_call(expr_ast);
6380
6381
17.2k
  znode expr_node, reset_node, value_node, key_node;
6382
17.2k
  zend_op *opline;
6383
17.2k
  uint32_t opnum_reset, opnum_fetch;
6384
6385
17.2k
  if (key_ast) {
6386
1.36k
    if (key_ast->kind == ZEND_AST_REF) {
6387
7
      zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
6388
7
    }
6389
1.36k
    if (key_ast->kind == ZEND_AST_ARRAY) {
6390
8
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
6391
8
    }
6392
1.36k
  }
6393
6394
17.2k
  if (by_ref) {
6395
1.27k
    value_ast = value_ast->child[0];
6396
1.27k
  }
6397
6398
17.2k
  if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
6399
96
    by_ref = true;
6400
96
  }
6401
6402
17.2k
  if (by_ref && is_variable) {
6403
1.00k
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
6404
16.2k
  } else {
6405
16.2k
    zend_compile_expr(&expr_node, expr_ast);
6406
16.2k
  }
6407
6408
17.2k
  if (by_ref) {
6409
1.37k
    zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
6410
1.37k
  }
6411
6412
17.2k
  opnum_reset = get_next_op_number();
6413
17.2k
  opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
6414
17.2k
  if (!by_ref) {
6415
15.8k
    opline->result_type = IS_TMP_VAR;
6416
15.8k
    reset_node.op_type = IS_TMP_VAR;
6417
15.8k
  }
6418
6419
17.2k
  zend_begin_loop(ZEND_FE_FREE, &reset_node, false);
6420
6421
17.2k
  opnum_fetch = get_next_op_number();
6422
17.2k
  opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
6423
6424
17.2k
  if (is_this_fetch(value_ast)) {
6425
8
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6426
17.2k
  } else if (value_ast->kind == ZEND_AST_VAR &&
6427
16.6k
    zend_try_compile_cv(&value_node, value_ast, BP_VAR_R) == SUCCESS) {
6428
16.3k
    SET_NODE(opline->op2, &value_node);
6429
16.3k
  } else {
6430
923
    opline->op2_type = by_ref ? IS_VAR : IS_TMP_VAR;
6431
923
    opline->op2.var = get_temporary_variable();
6432
923
    GET_NODE(&value_node, opline->op2);
6433
923
    if (value_ast->kind == ZEND_AST_ARRAY) {
6434
354
      zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr, BP_VAR_R);
6435
569
    } else if (by_ref) {
6436
250
      zend_emit_assign_ref_znode(value_ast, &value_node);
6437
319
    } else {
6438
319
      zend_emit_assign_znode(value_ast, &value_node);
6439
319
    }
6440
923
  }
6441
6442
17.2k
  if (key_ast) {
6443
1.34k
    opline = &CG(active_op_array)->opcodes[opnum_fetch];
6444
1.34k
    zend_make_tmp_result(&key_node, opline);
6445
1.34k
    zend_emit_assign_znode(key_ast, &key_node);
6446
1.34k
  }
6447
6448
17.2k
  zend_compile_stmt(stmt_ast);
6449
6450
  /* Place JMP and FE_FREE on the line where foreach starts. It would be
6451
   * better to use the end line, but this information is not available
6452
   * currently. */
6453
17.2k
  CG(zend_lineno) = ast->lineno;
6454
17.2k
  zend_emit_jump(opnum_fetch);
6455
6456
17.2k
  opline = &CG(active_op_array)->opcodes[opnum_reset];
6457
17.2k
  opline->op2.opline_num = get_next_op_number();
6458
6459
17.2k
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
6460
17.2k
  opline->extended_value = get_next_op_number();
6461
6462
17.2k
  zend_end_loop(opnum_fetch, &reset_node);
6463
6464
17.2k
  opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
6465
17.2k
}
6466
/* }}} */
6467
6468
static void zend_compile_if(zend_ast *ast) /* {{{ */
6469
177k
{
6470
177k
  const zend_ast_list *list = zend_ast_get_list(ast);
6471
177k
  uint32_t i;
6472
177k
  uint32_t *jmp_opnums = NULL;
6473
6474
177k
  if (list->children > 1) {
6475
61.0k
    jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
6476
61.0k
  }
6477
6478
468k
  for (i = 0; i < list->children; ++i) {
6479
290k
    const zend_ast *elem_ast = list->child[i];
6480
290k
    zend_ast *cond_ast = elem_ast->child[0];
6481
290k
    zend_ast *stmt_ast = elem_ast->child[1];
6482
6483
290k
    if (cond_ast) {
6484
229k
      znode cond_node;
6485
229k
      uint32_t opnum_jmpz;
6486
6487
229k
      if (i > 0) {
6488
52.3k
        CG(zend_lineno) = cond_ast->lineno;
6489
52.3k
        zend_do_extended_stmt(NULL);
6490
52.3k
      }
6491
6492
229k
      zend_compile_expr(&cond_node, cond_ast);
6493
229k
      opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6494
6495
229k
      zend_compile_stmt(stmt_ast);
6496
6497
229k
      if (i != list->children - 1) {
6498
        /* Set the lineno of JMP to the position of the if keyword, as we don't want to
6499
         * report the last line in the if branch as covered if it hasn't actually executed. */
6500
113k
        CG(zend_lineno) = elem_ast->lineno;
6501
113k
        jmp_opnums[i] = zend_emit_jump(0);
6502
113k
      }
6503
229k
      zend_update_jump_target_to_next(opnum_jmpz);
6504
229k
    } else {
6505
      /* "else" can only occur as last element. */
6506
60.8k
      ZEND_ASSERT(i == list->children - 1);
6507
60.8k
      zend_compile_stmt(stmt_ast);
6508
60.8k
    }
6509
290k
  }
6510
6511
177k
  if (list->children > 1) {
6512
174k
    for (i = 0; i < list->children - 1; ++i) {
6513
113k
      zend_update_jump_target_to_next(jmp_opnums[i]);
6514
113k
    }
6515
61.0k
    efree(jmp_opnums);
6516
61.0k
  }
6517
177k
}
6518
/* }}} */
6519
6520
8.09k
static uint8_t determine_switch_jumptable_type(const zend_ast_list *cases) {
6521
8.09k
  uint32_t i;
6522
8.09k
  uint8_t common_type = IS_UNDEF;
6523
23.1k
  for (i = 0; i < cases->children; i++) {
6524
18.5k
    zend_ast *case_ast = cases->child[i];
6525
18.5k
    zend_ast **cond_ast = &case_ast->child[0];
6526
18.5k
    const zval *cond_zv;
6527
18.5k
    if (!case_ast->child[0]) {
6528
      /* Skip default clause */
6529
198
      continue;
6530
198
    }
6531
6532
18.3k
    zend_eval_const_expr(cond_ast);
6533
18.3k
    if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6534
      /* Non-constant case */
6535
446
      return IS_UNDEF;
6536
446
    }
6537
6538
17.9k
    cond_zv = zend_ast_get_zval(case_ast->child[0]);
6539
17.9k
    if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6540
      /* We only optimize switched on integers and strings */
6541
2.86k
      return IS_UNDEF;
6542
2.86k
    }
6543
6544
15.0k
    if (common_type == IS_UNDEF) {
6545
7.46k
      common_type = Z_TYPE_P(cond_zv);
6546
7.58k
    } else if (common_type != Z_TYPE_P(cond_zv)) {
6547
      /* Non-uniform case types */
6548
40
      return IS_UNDEF;
6549
40
    }
6550
6551
15.0k
    if (Z_TYPE_P(cond_zv) == IS_STRING
6552
14.0k
        && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) {
6553
      /* Numeric strings cannot be compared with a simple hash lookup */
6554
108
      return IS_UNDEF;
6555
108
    }
6556
15.0k
  }
6557
6558
4.63k
  return common_type;
6559
8.09k
}
6560
6561
4.48k
static bool should_use_jumptable(const zend_ast_list *cases, uint8_t jumptable_type) {
6562
4.48k
  if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) {
6563
0
    return 0;
6564
0
  }
6565
6566
  /* Thresholds are chosen based on when the average switch time for equidistributed
6567
   * input becomes smaller when using the jumptable optimization. */
6568
4.48k
  if (jumptable_type == IS_LONG) {
6569
156
    return cases->children >= 5;
6570
4.32k
  } else {
6571
4.32k
    ZEND_ASSERT(jumptable_type == IS_STRING);
6572
4.32k
    return cases->children >= 2;
6573
4.32k
  }
6574
4.48k
}
6575
6576
static void zend_compile_switch(zend_ast *ast) /* {{{ */
6577
8.09k
{
6578
8.09k
  zend_ast *expr_ast = ast->child[0];
6579
8.09k
  zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
6580
6581
8.09k
  uint32_t i;
6582
8.09k
  bool has_default_case = false;
6583
6584
8.09k
  znode expr_node, case_node;
6585
8.09k
  zend_op *opline;
6586
8.09k
  uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1;
6587
8.09k
  uint8_t jumptable_type;
6588
8.09k
  HashTable *jumptable = NULL;
6589
6590
8.09k
  zend_compile_expr(&expr_node, expr_ast);
6591
6592
8.09k
  zend_begin_loop(ZEND_FREE, &expr_node, true);
6593
6594
8.09k
  case_node.op_type = IS_TMP_VAR;
6595
8.09k
  case_node.u.op.var = get_temporary_variable();
6596
6597
8.09k
  jumptable_type = determine_switch_jumptable_type(cases);
6598
8.09k
  if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) {
6599
3.90k
    znode jumptable_op;
6600
6601
3.90k
    ALLOC_HASHTABLE(jumptable);
6602
3.90k
    zend_hash_init(jumptable, cases->children, NULL, NULL, 0);
6603
3.90k
    jumptable_op.op_type = IS_CONST;
6604
3.90k
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6605
6606
3.90k
    opline = zend_emit_op(NULL,
6607
3.90k
      jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING,
6608
3.90k
      &expr_node, &jumptable_op);
6609
3.90k
    if (opline->op1_type == IS_CONST) {
6610
3.51k
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6611
3.51k
    }
6612
3.90k
    opnum_switch = opline - CG(active_op_array)->opcodes;
6613
3.90k
  }
6614
6615
8.09k
  jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
6616
28.3k
  for (i = 0; i < cases->children; ++i) {
6617
20.2k
    zend_ast *case_ast = cases->child[i];
6618
20.2k
    zend_ast *cond_ast = case_ast->child[0];
6619
20.2k
    znode cond_node;
6620
6621
20.2k
    if (case_ast->attr == ZEND_ALT_CASE_SYNTAX) {
6622
88
      CG(zend_lineno) = case_ast->lineno;
6623
88
      zend_error(E_DEPRECATED, "Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead");
6624
88
    }
6625
6626
20.2k
    if (!cond_ast) {
6627
207
      if (has_default_case) {
6628
14
        CG(zend_lineno) = case_ast->lineno;
6629
14
        zend_error_noreturn(E_COMPILE_ERROR,
6630
14
          "Switch statements may only contain one default clause");
6631
14
      }
6632
193
      has_default_case = true;
6633
193
      continue;
6634
207
    }
6635
6636
20.0k
    zend_compile_expr(&cond_node, cond_ast);
6637
6638
20.0k
    if (expr_node.op_type == IS_CONST
6639
15.6k
      && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
6640
967
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6641
19.0k
    } else if (expr_node.op_type == IS_CONST
6642
14.6k
      && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
6643
1.27k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
6644
17.7k
    } else {
6645
17.7k
      opline = zend_emit_op(NULL,
6646
17.7k
        (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL,
6647
17.7k
        &expr_node, &cond_node);
6648
17.7k
      SET_NODE(opline->result, &case_node);
6649
17.7k
      if (opline->op1_type == IS_CONST) {
6650
13.3k
        Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6651
13.3k
      }
6652
6653
17.7k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6654
17.7k
    }
6655
20.0k
  }
6656
6657
8.08k
  opnum_default_jmp = zend_emit_jump(0);
6658
6659
28.1k
  for (i = 0; i < cases->children; ++i) {
6660
20.0k
    zend_ast *case_ast = cases->child[i];
6661
20.0k
    zend_ast *cond_ast = case_ast->child[0];
6662
20.0k
    zend_ast *stmt_ast = case_ast->child[1];
6663
6664
20.0k
    if (cond_ast) {
6665
19.9k
      zend_update_jump_target_to_next(jmpnz_opnums[i]);
6666
6667
19.9k
      if (jumptable) {
6668
9.60k
        zval *cond_zv = zend_ast_get_zval(cond_ast);
6669
9.60k
        zval jmp_target;
6670
9.60k
        ZVAL_LONG(&jmp_target, get_next_op_number());
6671
6672
9.60k
        ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type);
6673
9.60k
        if (Z_TYPE_P(cond_zv) == IS_LONG) {
6674
585
          zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6675
9.01k
        } else {
6676
9.01k
          ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6677
9.01k
          zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6678
9.01k
        }
6679
9.60k
      }
6680
19.9k
    } else {
6681
179
      zend_update_jump_target_to_next(opnum_default_jmp);
6682
6683
179
      if (jumptable) {
6684
39
        ZEND_ASSERT(opnum_switch != (uint32_t)-1);
6685
39
        opline = &CG(active_op_array)->opcodes[opnum_switch];
6686
39
        opline->extended_value = get_next_op_number();
6687
39
      }
6688
179
    }
6689
6690
20.0k
    zend_compile_stmt(stmt_ast);
6691
20.0k
  }
6692
6693
8.08k
  if (!has_default_case) {
6694
7.90k
    zend_update_jump_target_to_next(opnum_default_jmp);
6695
6696
7.90k
    if (jumptable) {
6697
3.85k
      opline = &CG(active_op_array)->opcodes[opnum_switch];
6698
3.85k
      opline->extended_value = get_next_op_number();
6699
3.85k
    }
6700
7.90k
  }
6701
6702
8.08k
  zend_end_loop(get_next_op_number(), &expr_node);
6703
6704
8.08k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
6705
1.44k
    opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6706
1.44k
    opline->extended_value = ZEND_FREE_SWITCH;
6707
6.63k
  } else if (expr_node.op_type == IS_CONST) {
6708
6.34k
    zval_ptr_dtor_nogc(&expr_node.u.constant);
6709
6.34k
  }
6710
6711
8.08k
  efree(jmpnz_opnums);
6712
8.08k
}
6713
/* }}} */
6714
6715
static uint32_t count_match_conds(const zend_ast_list *arms)
6716
2.28k
{
6717
2.28k
  uint32_t num_conds = 0;
6718
6719
5.70k
  for (uint32_t i = 0; i < arms->children; i++) {
6720
3.41k
    const zend_ast *arm_ast = arms->child[i];
6721
3.41k
    if (arm_ast->child[0] == NULL) {
6722
575
      continue;
6723
575
    }
6724
6725
2.84k
    const zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6726
2.84k
    num_conds += conds->children;
6727
2.84k
  }
6728
6729
2.28k
  return num_conds;
6730
2.28k
}
6731
6732
2.28k
static bool can_match_use_jumptable(const zend_ast_list *arms) {
6733
4.17k
  for (uint32_t i = 0; i < arms->children; i++) {
6734
2.90k
    const zend_ast *arm_ast = arms->child[i];
6735
2.90k
    if (!arm_ast->child[0]) {
6736
      /* Skip default arm */
6737
455
      continue;
6738
455
    }
6739
6740
2.44k
    zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6741
6.19k
    for (uint32_t j = 0; j < conds->children; j++) {
6742
4.76k
      zend_ast **cond_ast = &conds->child[j];
6743
6744
4.76k
      zend_eval_const_expr(cond_ast);
6745
4.76k
      if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6746
894
        return 0;
6747
894
      }
6748
6749
3.87k
      const zval *cond_zv = zend_ast_get_zval(*cond_ast);
6750
3.87k
      if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6751
128
        return 0;
6752
128
      }
6753
3.87k
    }
6754
2.44k
  }
6755
6756
1.26k
  return 1;
6757
2.28k
}
6758
6759
static bool zend_is_pipe_optimizable_callable_name(zend_ast *ast)
6760
211
{
6761
211
  if (ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(ast)) == IS_STRING) {
6762
    /* Assert compilation adds a message operand, but this is incompatible with the
6763
     * pipe optimization that uses a temporary znode for the reference elimination.
6764
     * Therefore, disable the optimization for assert.
6765
     * Note that "assert" as a name is always treated as fully qualified. */
6766
147
    return !zend_string_equals_literal_ci(zend_ast_get_str(ast), "assert");
6767
147
  }
6768
6769
64
  return true;
6770
211
}
6771
6772
static void zend_compile_pipe(znode *result, zend_ast *ast, uint32_t type)
6773
78.0k
{
6774
78.0k
  zend_ast *operand_ast = ast->child[0];
6775
78.0k
  zend_ast *callable_ast = ast->child[1];
6776
6777
78.0k
  if (callable_ast->kind == ZEND_AST_ARROW_FUNC && !(callable_ast->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) {
6778
7
    zend_error_noreturn(E_COMPILE_ERROR, "Arrow functions on the right hand side of |> must be parenthesized");
6779
7
  }
6780
6781
  /* Compile the left hand side down to a value first. */
6782
78.0k
  znode operand_result;
6783
78.0k
  zend_compile_expr(&operand_result, operand_ast);
6784
6785
  /* Wrap simple values in a ZEND_QM_ASSIGN opcode to ensure references
6786
   * always fail. They will already fail in complex cases like arrays,
6787
   * so those don't need a wrapper. */
6788
78.0k
  znode wrapped_operand_result;
6789
78.0k
  if (operand_result.op_type & (IS_CV|IS_VAR)) {
6790
53
    zend_emit_op_tmp(&wrapped_operand_result, ZEND_QM_ASSIGN, &operand_result, NULL);
6791
77.9k
  } else {
6792
77.9k
    wrapped_operand_result = operand_result;
6793
77.9k
  }
6794
6795
  /* Turn the operand into a function parameter list. */
6796
78.0k
  zend_ast *arg_list_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&wrapped_operand_result));
6797
6798
78.0k
  zend_ast *fcall_ast;
6799
78.0k
  znode callable_result;
6800
6801
  /* Turn $foo |> bar(...) into bar($foo). */
6802
78.0k
  if (callable_ast->kind == ZEND_AST_CALL
6803
328
    && callable_ast->child[1]->kind == ZEND_AST_CALLABLE_CONVERT
6804
211
    && zend_is_pipe_optimizable_callable_name(callable_ast->child[0])) {
6805
191
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6806
191
        callable_ast->child[0], arg_list_ast);
6807
  /* Turn $foo |> bar::baz(...) into bar::baz($foo). */
6808
77.8k
  } else if (callable_ast->kind == ZEND_AST_STATIC_CALL
6809
283
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6810
143
    fcall_ast = zend_ast_create(ZEND_AST_STATIC_CALL,
6811
143
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6812
  /* Turn $foo |> $bar->baz(...) into $bar->baz($foo). */
6813
77.6k
  } else if (callable_ast->kind == ZEND_AST_METHOD_CALL
6814
1.12k
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6815
782
    fcall_ast = zend_ast_create(ZEND_AST_METHOD_CALL,
6816
782
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6817
  /* Turn $foo |> $expr into ($expr)($foo) */
6818
76.9k
  } else {
6819
76.9k
    zend_compile_expr(&callable_result, callable_ast);
6820
76.9k
    callable_ast = zend_ast_create_znode(&callable_result);
6821
76.9k
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6822
76.9k
      callable_ast, arg_list_ast);
6823
76.9k
  }
6824
6825
78.0k
  zend_do_extended_stmt(&operand_result);
6826
6827
78.0k
  zend_compile_var(result, fcall_ast, type, /* by_ref */ false);
6828
78.0k
}
6829
6830
static void zend_compile_match(znode *result, zend_ast *ast)
6831
2.28k
{
6832
2.28k
  zend_ast *expr_ast = ast->child[0];
6833
2.28k
  zend_ast_list *arms = zend_ast_get_list(ast->child[1]);
6834
2.28k
  bool has_default_arm = false;
6835
2.28k
  uint32_t opnum_match = (uint32_t)-1;
6836
6837
2.28k
  znode expr_node;
6838
2.28k
  zend_compile_expr(&expr_node, expr_ast);
6839
6840
2.28k
  znode case_node;
6841
2.28k
  case_node.op_type = IS_TMP_VAR;
6842
2.28k
  case_node.u.op.var = get_temporary_variable();
6843
6844
2.28k
  uint32_t num_conds = count_match_conds(arms);
6845
2.28k
  uint8_t can_use_jumptable = can_match_use_jumptable(arms);
6846
2.28k
  bool uses_jumptable = can_use_jumptable && num_conds >= 2;
6847
2.28k
  HashTable *jumptable = NULL;
6848
2.28k
  uint32_t *jmpnz_opnums = NULL;
6849
6850
5.69k
  for (uint32_t i = 0; i < arms->children; ++i) {
6851
3.41k
    zend_ast *arm_ast = arms->child[i];
6852
6853
3.41k
    if (!arm_ast->child[0]) {
6854
575
      if (has_default_arm) {
6855
8
        CG(zend_lineno) = arm_ast->lineno;
6856
8
        zend_error_noreturn(E_COMPILE_ERROR,
6857
8
          "Match expressions may only contain one default arm");
6858
8
      }
6859
567
      has_default_arm = true;
6860
567
    }
6861
3.41k
  }
6862
6863
2.28k
  if (uses_jumptable) {
6864
699
    znode jumptable_op;
6865
6866
699
    ALLOC_HASHTABLE(jumptable);
6867
699
    zend_hash_init(jumptable, num_conds, NULL, NULL, 0);
6868
699
    jumptable_op.op_type = IS_CONST;
6869
699
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6870
6871
699
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH, &expr_node, &jumptable_op);
6872
699
    if (opline->op1_type == IS_CONST) {
6873
283
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6874
283
    }
6875
699
    opnum_match = opline - CG(active_op_array)->opcodes;
6876
1.58k
  } else {
6877
1.58k
    jmpnz_opnums = safe_emalloc(sizeof(uint32_t), num_conds, 0);
6878
1.58k
    uint32_t cond_count = 0;
6879
3.70k
    for (uint32_t i = 0; i < arms->children; ++i) {
6880
2.12k
      zend_ast *arm_ast = arms->child[i];
6881
6882
2.12k
      if (!arm_ast->child[0]) {
6883
422
        continue;
6884
422
      }
6885
6886
1.70k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6887
4.47k
      for (uint32_t j = 0; j < conds->children; j++) {
6888
2.77k
        zend_ast *cond_ast = conds->child[j];
6889
6890
2.77k
        znode cond_node;
6891
2.77k
        zend_compile_expr(&cond_node, cond_ast);
6892
6893
2.77k
        uint32_t opcode = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE_STRICT : ZEND_IS_IDENTICAL;
6894
2.77k
        zend_op *opline = zend_emit_op(NULL, opcode, &expr_node, &cond_node);
6895
2.77k
        SET_NODE(opline->result, &case_node);
6896
2.77k
        if (opline->op1_type == IS_CONST) {
6897
1.14k
          Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6898
1.14k
        }
6899
6900
2.77k
        jmpnz_opnums[cond_count] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6901
6902
2.77k
        cond_count++;
6903
2.77k
      }
6904
1.70k
    }
6905
1.58k
  }
6906
6907
2.28k
  uint32_t opnum_default_jmp = 0;
6908
2.28k
  if (!uses_jumptable) {
6909
1.58k
    opnum_default_jmp = zend_emit_jump(0);
6910
1.58k
  }
6911
6912
2.28k
  bool is_first_case = true;
6913
2.28k
  uint32_t cond_count = 0;
6914
2.28k
  uint32_t *jmp_end_opnums = safe_emalloc(sizeof(uint32_t), arms->children, 0);
6915
6916
  // The generated default arm is emitted first to avoid live range issues where the tmpvar
6917
  // for the arm result is freed even though it has not been initialized yet.
6918
2.28k
  if (!has_default_arm) {
6919
1.72k
    if (!uses_jumptable) {
6920
1.15k
      zend_update_jump_target_to_next(opnum_default_jmp);
6921
1.15k
    }
6922
6923
1.72k
    if (jumptable) {
6924
562
      zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6925
562
      opline->extended_value = get_next_op_number();
6926
562
    }
6927
6928
1.72k
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH_ERROR, &expr_node, NULL);
6929
1.72k
    if (opline->op1_type == IS_CONST) {
6930
404
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6931
404
    }
6932
1.72k
    if (arms->children == 0) {
6933
      /* Mark this as an "expression throw" for opcache. */
6934
199
      opline->extended_value = ZEND_THROW_IS_EXPR;
6935
199
    }
6936
1.72k
  }
6937
6938
5.66k
  for (uint32_t i = 0; i < arms->children; ++i) {
6939
3.38k
    zend_ast *arm_ast = arms->child[i];
6940
3.38k
    zend_ast *body_ast = arm_ast->child[1];
6941
6942
3.38k
    if (arm_ast->child[0] != NULL) {
6943
2.82k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6944
6945
8.54k
      for (uint32_t j = 0; j < conds->children; j++) {
6946
5.71k
        zend_ast *cond_ast = conds->child[j];
6947
6948
5.71k
        if (jmpnz_opnums != NULL) {
6949
2.77k
          zend_update_jump_target_to_next(jmpnz_opnums[cond_count]);
6950
2.77k
        }
6951
6952
5.71k
        if (jumptable) {
6953
2.93k
          zval *cond_zv = zend_ast_get_zval(cond_ast);
6954
2.93k
          zval jmp_target;
6955
2.93k
          ZVAL_LONG(&jmp_target, get_next_op_number());
6956
6957
2.93k
          if (Z_TYPE_P(cond_zv) == IS_LONG) {
6958
2.49k
            zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6959
2.49k
          } else {
6960
443
            ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6961
443
            zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6962
443
          }
6963
2.93k
        }
6964
6965
5.71k
        cond_count++;
6966
5.71k
      }
6967
2.82k
    } else {
6968
559
      if (!uses_jumptable) {
6969
422
        zend_update_jump_target_to_next(opnum_default_jmp);
6970
422
      }
6971
6972
559
      if (jumptable) {
6973
137
        ZEND_ASSERT(opnum_match != (uint32_t)-1);
6974
137
        zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6975
137
        opline->extended_value = get_next_op_number();
6976
137
      }
6977
559
    }
6978
6979
3.38k
    znode body_node;
6980
3.38k
    zend_compile_expr(&body_node, body_ast);
6981
6982
3.38k
    if (is_first_case) {
6983
2.08k
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL);
6984
2.08k
      is_first_case = false;
6985
2.08k
    } else {
6986
1.30k
      zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL);
6987
1.30k
      SET_NODE(opline_qm_assign->result, result);
6988
1.30k
    }
6989
6990
3.38k
    jmp_end_opnums[i] = zend_emit_jump(0);
6991
3.38k
  }
6992
6993
  // Initialize result in case there is no arm
6994
2.28k
  if (arms->children == 0) {
6995
199
    result->op_type = IS_CONST;
6996
199
    ZVAL_NULL(&result->u.constant);
6997
199
  }
6998
6999
5.66k
  for (uint32_t i = 0; i < arms->children; ++i) {
7000
3.38k
    zend_update_jump_target_to_next(jmp_end_opnums[i]);
7001
3.38k
  }
7002
7003
2.28k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
7004
1.29k
    zend_op *opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
7005
1.29k
    opline->extended_value = ZEND_FREE_SWITCH;
7006
1.29k
  } else if (expr_node.op_type == IS_CONST) {
7007
708
    zval_ptr_dtor_nogc(&expr_node.u.constant);
7008
708
  }
7009
7010
2.28k
  if (jmpnz_opnums != NULL) {
7011
1.58k
    efree(jmpnz_opnums);
7012
1.58k
  }
7013
2.28k
  efree(jmp_end_opnums);
7014
2.28k
}
7015
7016
static void zend_compile_try(const zend_ast *ast) /* {{{ */
7017
29.1k
{
7018
29.1k
  zend_ast *try_ast = ast->child[0];
7019
29.1k
  const zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
7020
29.1k
  zend_ast *finally_ast = ast->child[2];
7021
7022
29.1k
  uint32_t i, j;
7023
29.1k
  zend_op *opline;
7024
29.1k
  uint32_t try_catch_offset;
7025
29.1k
  uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
7026
29.1k
  uint32_t orig_fast_call_var = CG(context).fast_call_var;
7027
29.1k
  uint32_t orig_try_catch_offset = CG(context).try_catch_offset;
7028
7029
29.1k
  if (catches->children == 0 && !finally_ast) {
7030
70
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
7031
70
  }
7032
7033
  /* label: try { } must not be equal to try { label: } */
7034
29.0k
  if (CG(context).labels) {
7035
293
    zend_label *label;
7036
293
    ZEND_HASH_MAP_REVERSE_FOREACH_PTR(CG(context).labels, label) {
7037
293
      if (label->opline_num == get_next_op_number()) {
7038
97
        zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
7039
97
      }
7040
293
      break;
7041
879
    } ZEND_HASH_FOREACH_END();
7042
293
  }
7043
7044
29.0k
  try_catch_offset = zend_add_try_element(get_next_op_number());
7045
7046
29.0k
  if (finally_ast) {
7047
2.58k
    zend_loop_var fast_call;
7048
2.58k
    if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
7049
958
      CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
7050
958
    }
7051
2.58k
    CG(context).fast_call_var = get_temporary_variable();
7052
7053
    /* Push FAST_CALL on unwind stack */
7054
2.58k
    fast_call.opcode = ZEND_FAST_CALL;
7055
2.58k
    fast_call.var_type = IS_TMP_VAR;
7056
2.58k
    fast_call.var_num = CG(context).fast_call_var;
7057
2.58k
    fast_call.try_catch_offset = try_catch_offset;
7058
2.58k
    zend_stack_push(&CG(loop_var_stack), &fast_call);
7059
2.58k
  }
7060
7061
29.0k
  CG(context).try_catch_offset = try_catch_offset;
7062
7063
29.0k
  zend_compile_stmt(try_ast);
7064
7065
29.0k
  if (catches->children != 0) {
7066
26.6k
    jmp_opnums[0] = zend_emit_jump(0);
7067
26.6k
  }
7068
7069
60.4k
  for (i = 0; i < catches->children; ++i) {
7070
31.4k
    const zend_ast *catch_ast = catches->child[i];
7071
31.4k
    const zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]);
7072
31.4k
    zend_ast *var_ast = catch_ast->child[1];
7073
31.4k
    zend_ast *stmt_ast = catch_ast->child[2];
7074
31.4k
    zend_string *var_name = var_ast ? zval_make_interned_string(zend_ast_get_zval(var_ast)) : NULL;
7075
31.4k
    bool is_last_catch = (i + 1 == catches->children);
7076
7077
31.4k
    uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0);
7078
31.4k
    uint32_t opnum_catch = (uint32_t)-1;
7079
7080
31.4k
    CG(zend_lineno) = catch_ast->lineno;
7081
7082
67.8k
    for (j = 0; j < classes->children; j++) {
7083
36.4k
      zend_ast *class_ast = classes->child[j];
7084
36.4k
      bool is_last_class = (j + 1 == classes->children);
7085
7086
36.4k
      if (!zend_is_const_default_class_ref(class_ast)) {
7087
7
        zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
7088
7
      }
7089
7090
36.4k
      opnum_catch = get_next_op_number();
7091
36.4k
      if (i == 0 && j == 0) {
7092
26.6k
        CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
7093
26.6k
      }
7094
7095
36.4k
      opline = get_next_op();
7096
36.4k
      opline->opcode = ZEND_CATCH;
7097
36.4k
      opline->op1_type = IS_CONST;
7098
36.4k
      opline->op1.constant = zend_add_class_name_literal(
7099
36.4k
          zend_resolve_class_name_ast(class_ast));
7100
36.4k
      opline->extended_value = zend_alloc_cache_slot();
7101
7102
36.4k
      if (var_name && zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
7103
7
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
7104
7
      }
7105
7106
36.4k
      opline->result_type = var_name ? IS_CV : IS_UNUSED;
7107
36.4k
      opline->result.var = var_name ? lookup_cv(var_name) : -1;
7108
7109
36.4k
      if (is_last_catch && is_last_class) {
7110
26.6k
        opline->extended_value |= ZEND_LAST_CATCH;
7111
26.6k
      }
7112
7113
36.4k
      if (!is_last_class) {
7114
5.02k
        jmp_multicatch[j] = zend_emit_jump(0);
7115
5.02k
        opline = &CG(active_op_array)->opcodes[opnum_catch];
7116
5.02k
        opline->op2.opline_num = get_next_op_number();
7117
5.02k
      }
7118
36.4k
    }
7119
7120
36.4k
    for (j = 0; j < classes->children - 1; j++) {
7121
5.02k
      zend_update_jump_target_to_next(jmp_multicatch[j]);
7122
5.02k
    }
7123
7124
31.4k
    efree(jmp_multicatch);
7125
7126
31.4k
    zend_compile_stmt(stmt_ast);
7127
7128
31.4k
    if (!is_last_catch) {
7129
4.78k
      jmp_opnums[i + 1] = zend_emit_jump(0);
7130
4.78k
    }
7131
7132
31.4k
    ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class");
7133
31.4k
    opline = &CG(active_op_array)->opcodes[opnum_catch];
7134
31.4k
    if (!is_last_catch) {
7135
4.78k
      opline->op2.opline_num = get_next_op_number();
7136
4.78k
    }
7137
31.4k
  }
7138
7139
60.4k
  for (i = 0; i < catches->children; ++i) {
7140
31.4k
    zend_update_jump_target_to_next(jmp_opnums[i]);
7141
31.4k
  }
7142
7143
29.0k
  if (finally_ast) {
7144
2.57k
    zend_loop_var discard_exception;
7145
2.57k
    uint32_t opnum_jmp = get_next_op_number() + 1;
7146
7147
    /* Pop FAST_CALL from unwind stack */
7148
2.57k
    zend_stack_del_top(&CG(loop_var_stack));
7149
7150
    /* Push DISCARD_EXCEPTION on unwind stack */
7151
2.57k
    discard_exception.opcode = ZEND_DISCARD_EXCEPTION;
7152
2.57k
    discard_exception.var_type = IS_TMP_VAR;
7153
2.57k
    discard_exception.var_num = CG(context).fast_call_var;
7154
2.57k
    zend_stack_push(&CG(loop_var_stack), &discard_exception);
7155
7156
2.57k
    CG(zend_lineno) = finally_ast->lineno;
7157
7158
2.57k
    opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
7159
2.57k
    opline->op1.num = try_catch_offset;
7160
2.57k
    opline->result_type = IS_TMP_VAR;
7161
2.57k
    opline->result.var = CG(context).fast_call_var;
7162
7163
2.57k
    zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
7164
7165
2.57k
    zend_compile_stmt(finally_ast);
7166
7167
2.57k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
7168
2.57k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
7169
2.57k
      = get_next_op_number();
7170
7171
2.57k
    opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
7172
2.57k
    opline->op1_type = IS_TMP_VAR;
7173
2.57k
    opline->op1.var = CG(context).fast_call_var;
7174
2.57k
    opline->op2.num = orig_try_catch_offset;
7175
7176
2.57k
    zend_update_jump_target_to_next(opnum_jmp);
7177
7178
2.57k
    CG(context).fast_call_var = orig_fast_call_var;
7179
7180
    /* Pop DISCARD_EXCEPTION from unwind stack */
7181
2.57k
    zend_stack_del_top(&CG(loop_var_stack));
7182
2.57k
  }
7183
7184
29.0k
  CG(context).try_catch_offset = orig_try_catch_offset;
7185
7186
29.0k
  efree(jmp_opnums);
7187
29.0k
}
7188
/* }}} */
7189
7190
/* Encoding declarations must already be handled during parsing */
7191
bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
7192
2.67k
{
7193
2.67k
  const zend_ast_list *declares = zend_ast_get_list(ast);
7194
2.67k
  uint32_t i;
7195
6.10k
  for (i = 0; i < declares->children; ++i) {
7196
3.45k
    const zend_ast *declare_ast = declares->child[i];
7197
3.45k
    zend_ast *name_ast = declare_ast->child[0];
7198
3.45k
    zend_ast *value_ast = declare_ast->child[1];
7199
3.45k
    const zend_string *name = zend_ast_get_str(name_ast);
7200
7201
3.45k
    if (zend_string_equals_literal_ci(name, "encoding")) {
7202
168
      if (value_ast->kind != ZEND_AST_ZVAL) {
7203
21
        zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0);
7204
21
        return 0;
7205
21
      }
7206
7207
147
      if (CG(multibyte)) {
7208
0
        zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
7209
7210
0
        const zend_encoding *new_encoding, *old_encoding;
7211
0
        zend_encoding_filter old_input_filter;
7212
7213
0
        CG(encoding_declared) = 1;
7214
7215
0
        new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
7216
0
        if (!new_encoding) {
7217
0
          zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
7218
0
        } else {
7219
0
          old_input_filter = LANG_SCNG(input_filter);
7220
0
          old_encoding = LANG_SCNG(script_encoding);
7221
0
          zend_multibyte_set_filter(new_encoding);
7222
7223
          /* need to re-scan if input filter changed */
7224
0
          if (old_input_filter != LANG_SCNG(input_filter) ||
7225
0
             (old_input_filter && new_encoding != old_encoding)) {
7226
0
            zend_multibyte_yyinput_again(old_input_filter, old_encoding);
7227
0
          }
7228
0
        }
7229
7230
0
        zend_string_release_ex(encoding_name, 0);
7231
147
      } else {
7232
147
        zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
7233
147
          "Zend multibyte feature is turned off by settings");
7234
147
      }
7235
147
    }
7236
3.45k
  }
7237
7238
2.65k
  return 1;
7239
2.67k
}
7240
/* }}} */
7241
7242
/* Check whether this is the first statement, not counting declares. */
7243
static zend_result zend_is_first_statement(const zend_ast *ast, bool allow_nop) /* {{{ */
7244
2.84k
{
7245
2.84k
  uint32_t i = 0;
7246
2.84k
  const zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
7247
7248
15.3k
  while (i < file_ast->children) {
7249
15.3k
    if (file_ast->child[i] == ast) {
7250
2.80k
      return SUCCESS;
7251
12.5k
    } else if (file_ast->child[i] == NULL) {
7252
1.82k
      if (!allow_nop) {
7253
0
        return FAILURE;
7254
0
      }
7255
10.7k
    } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
7256
47
      return FAILURE;
7257
47
    }
7258
12.4k
    i++;
7259
12.4k
  }
7260
1
  return FAILURE;
7261
2.84k
}
7262
/* }}} */
7263
7264
static void zend_compile_declare(const zend_ast *ast) /* {{{ */
7265
4.84k
{
7266
4.84k
  const zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
7267
4.84k
  zend_ast *stmt_ast = ast->child[1];
7268
4.84k
  zend_declarables orig_declarables = FC(declarables);
7269
4.84k
  uint32_t i;
7270
7271
13.4k
  for (i = 0; i < declares->children; ++i) {
7272
8.74k
    zend_ast *declare_ast = declares->child[i];
7273
8.74k
    zend_ast *name_ast = declare_ast->child[0];
7274
8.74k
    zend_ast **value_ast_ptr = &declare_ast->child[1];
7275
8.74k
    zend_string *name = zend_ast_get_str(name_ast);
7276
7277
8.74k
    if ((*value_ast_ptr)->kind != ZEND_AST_ZVAL) {
7278
25
      zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
7279
25
    }
7280
7281
8.71k
    if (zend_string_equals_literal_ci(name, "ticks")) {
7282
5.26k
      zval value_zv;
7283
5.26k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7284
5.26k
      FC(declarables).ticks = zval_get_long(&value_zv);
7285
5.26k
      zval_ptr_dtor_nogc(&value_zv);
7286
5.26k
    } else if (zend_string_equals_literal_ci(name, "encoding")) {
7287
7288
88
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ false)) {
7289
6
        zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
7290
6
          "the very first statement in the script");
7291
6
      }
7292
3.36k
    } else if (zend_string_equals_literal_ci(name, "strict_types")) {
7293
467
      zval value_zv;
7294
7295
467
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
7296
16
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
7297
16
          "the very first statement in the script");
7298
16
      }
7299
7300
451
      if (ast->child[1] != NULL) {
7301
9
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
7302
9
          "use block mode");
7303
9
      }
7304
7305
442
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7306
7307
442
      if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
7308
49
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
7309
49
      }
7310
7311
393
      if (Z_LVAL(value_zv) == 1) {
7312
286
        CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
7313
286
      }
7314
7315
2.89k
    } else {
7316
2.89k
      zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
7317
2.89k
    }
7318
8.71k
  }
7319
7320
4.74k
  if (stmt_ast) {
7321
402
    zend_compile_stmt(stmt_ast);
7322
7323
402
    FC(declarables) = orig_declarables;
7324
402
  }
7325
4.74k
}
7326
/* }}} */
7327
7328
static void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
7329
1.03M
{
7330
1.03M
  const zend_ast_list *list = zend_ast_get_list(ast);
7331
1.03M
  uint32_t i;
7332
3.97M
  for (i = 0; i < list->children; ++i) {
7333
2.93M
    zend_compile_stmt(list->child[i]);
7334
2.93M
  }
7335
1.03M
}
7336
/* }}} */
7337
7338
ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
7339
364k
{
7340
364k
  uint32_t i, n;
7341
7342
364k
  func->common.arg_flags[0] = 0;
7343
364k
  func->common.arg_flags[1] = 0;
7344
364k
  func->common.arg_flags[2] = 0;
7345
364k
  if (func->common.arg_info) {
7346
364k
    n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
7347
364k
    i = 0;
7348
746k
    while (i < n) {
7349
381k
      ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i]));
7350
381k
      i++;
7351
381k
    }
7352
364k
    if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_VARIADIC) && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) {
7353
683
      uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]);
7354
8.77k
      while (i < MAX_ARG_FLAG_NUM) {
7355
8.09k
        ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
7356
8.09k
        i++;
7357
8.09k
      }
7358
683
    }
7359
364k
  }
7360
364k
}
7361
/* }}} */
7362
7363
static zend_type zend_compile_single_typename(zend_ast *ast)
7364
160k
{
7365
160k
  ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE));
7366
160k
  if (ast->kind == ZEND_AST_TYPE) {
7367
3.75k
    if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) {
7368
5
      zend_error_noreturn(E_COMPILE_ERROR,
7369
5
        "Cannot use \"static\" when no class scope is active");
7370
5
    }
7371
7372
3.74k
    return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0);
7373
157k
  } else {
7374
157k
    zend_string *type_name = zend_ast_get_str(ast);
7375
157k
    uint8_t type_code = zend_lookup_builtin_type_by_name(type_name);
7376
7377
157k
    if (type_code != 0) {
7378
36.0k
      if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) {
7379
5
        zend_error_noreturn(E_COMPILE_ERROR,
7380
5
          "Type declaration '%s' must be unqualified",
7381
5
          ZSTR_VAL(zend_string_tolower(type_name)));
7382
5
      }
7383
7384
      /* Transform iterable into a type union alias */
7385
36.0k
      if (type_code == IS_ITERABLE) {
7386
        /* Set iterable bit for BC compat during Reflection and string representation of type */
7387
11.9k
        zend_type iterable = (zend_type) ZEND_TYPE_INIT_CLASS_MASK(ZSTR_KNOWN(ZEND_STR_TRAVERSABLE),
7388
11.9k
                  (MAY_BE_ARRAY|_ZEND_TYPE_ITERABLE_BIT));
7389
11.9k
        return iterable;
7390
11.9k
      }
7391
7392
24.0k
      return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0);
7393
121k
    } else {
7394
121k
      const char *correct_name;
7395
121k
      uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
7396
121k
      zend_string *class_name = type_name;
7397
7398
121k
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
7399
120k
        class_name = zend_resolve_class_name_ast(ast);
7400
120k
        zend_assert_valid_class_name(class_name, "a type name");
7401
120k
      } else {
7402
809
        ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT);
7403
7404
809
        zend_ensure_valid_class_fetch_type(fetch_type);
7405
7406
809
        bool substitute_self_parent = zend_is_scope_known()
7407
500
          && !(CG(active_class_entry)->ce_flags & ZEND_ACC_ANON_CLASS);
7408
7409
809
        if (fetch_type == ZEND_FETCH_CLASS_SELF) {
7410
          /* Scope might be unknown for unbound closures and traits */
7411
626
          if (substitute_self_parent) {
7412
369
            class_name = CG(active_class_entry)->name;
7413
369
            ZEND_ASSERT(class_name && "must know class name when resolving self type at compile time");
7414
369
          }
7415
626
        } else {
7416
183
          ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_PARENT);
7417
          /* Scope might be unknown for unbound closures and traits */
7418
183
          if (substitute_self_parent) {
7419
118
            class_name = CG(active_class_entry)->parent_name;
7420
118
            ZEND_ASSERT(class_name && "must know class name when resolving parent type at compile time");
7421
118
          }
7422
163
        }
7423
809
        zend_string_addref(class_name);
7424
789
      }
7425
7426
121k
      if (ast->attr == ZEND_NAME_NOT_FQ
7427
120k
          && zend_is_confusable_type(type_name, &correct_name)
7428
3.74k
          && zend_is_not_imported(type_name)) {
7429
3.73k
        const char *extra =
7430
3.73k
          FC(current_namespace) ? " or import the class with \"use\"" : "";
7431
3.73k
        if (correct_name) {
7432
3.49k
          zend_error(E_COMPILE_WARNING,
7433
3.49k
            "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? "
7434
3.49k
            "Write \"\\%s\"%s to suppress this warning",
7435
3.49k
            ZSTR_VAL(type_name), correct_name, ZSTR_VAL(class_name), extra);
7436
3.49k
        } else {
7437
238
          zend_error(E_COMPILE_WARNING,
7438
238
            "\"%s\" is not a supported builtin type "
7439
238
            "and will be interpreted as a class name. "
7440
238
            "Write \"\\%s\"%s to suppress this warning",
7441
238
            ZSTR_VAL(type_name), ZSTR_VAL(class_name), extra);
7442
238
        }
7443
3.73k
      }
7444
7445
121k
      class_name = zend_new_interned_string(class_name);
7446
121k
      zend_alloc_ce_cache(class_name);
7447
121k
      return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, /* allow null */ false, 0);
7448
121k
    }
7449
157k
  }
7450
160k
}
7451
7452
static void zend_are_intersection_types_redundant(const zend_type left_type, const zend_type right_type)
7453
4.16k
{
7454
4.16k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(left_type));
7455
4.16k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(right_type));
7456
4.16k
  const zend_type_list *l_type_list = ZEND_TYPE_LIST(left_type);
7457
4.16k
  const zend_type_list *r_type_list = ZEND_TYPE_LIST(right_type);
7458
4.16k
  const zend_type_list *smaller_type_list, *larger_type_list;
7459
4.16k
  bool flipped = false;
7460
7461
4.16k
  if (r_type_list->num_types < l_type_list->num_types) {
7462
1.41k
    smaller_type_list = r_type_list;
7463
1.41k
    larger_type_list = l_type_list;
7464
1.41k
    flipped = true;
7465
2.74k
  } else {
7466
2.74k
    smaller_type_list = l_type_list;
7467
2.74k
    larger_type_list = r_type_list;
7468
2.74k
  }
7469
7470
4.16k
  unsigned int sum = 0;
7471
4.16k
  const zend_type *outer_type;
7472
16.1k
  ZEND_TYPE_LIST_FOREACH(smaller_type_list, outer_type)
7473
16.1k
    const zend_type *inner_type;
7474
56.6k
    ZEND_TYPE_LIST_FOREACH(larger_type_list, inner_type)
7475
56.6k
      if (zend_string_equals_ci(ZEND_TYPE_NAME(*inner_type), ZEND_TYPE_NAME(*outer_type))) {
7476
4.13k
        sum++;
7477
4.13k
        break;
7478
4.13k
      }
7479
56.6k
    ZEND_TYPE_LIST_FOREACH_END();
7480
16.1k
  ZEND_TYPE_LIST_FOREACH_END();
7481
7482
4.16k
  if (sum == smaller_type_list->num_types) {
7483
18
    zend_string *smaller_type_str;
7484
18
    zend_string *larger_type_str;
7485
18
    if (flipped) {
7486
5
      smaller_type_str = zend_type_to_string(right_type);
7487
5
      larger_type_str = zend_type_to_string(left_type);
7488
13
    } else {
7489
13
      smaller_type_str = zend_type_to_string(left_type);
7490
13
      larger_type_str = zend_type_to_string(right_type);
7491
13
    }
7492
18
    if (smaller_type_list->num_types == larger_type_list->num_types) {
7493
8
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant with type %s",
7494
8
        ZSTR_VAL(smaller_type_str), ZSTR_VAL(larger_type_str));
7495
10
    } else {
7496
10
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7497
10
        ZSTR_VAL(larger_type_str), ZSTR_VAL(smaller_type_str));
7498
10
    }
7499
18
  }
7500
4.16k
}
7501
7502
static void zend_is_intersection_type_redundant_by_single_type(const zend_type intersection_type, const zend_type single_type)
7503
9.91k
{
7504
9.91k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(intersection_type));
7505
9.91k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(single_type));
7506
7507
9.91k
  const zend_type *single_intersection_type = NULL;
7508
33.6k
  ZEND_TYPE_FOREACH(intersection_type, single_intersection_type)
7509
33.6k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(*single_intersection_type), ZEND_TYPE_NAME(single_type))) {
7510
10
      zend_string *single_type_str = zend_type_to_string(single_type);
7511
10
      zend_string *complete_type = zend_type_to_string(intersection_type);
7512
10
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7513
10
          ZSTR_VAL(complete_type), ZSTR_VAL(single_type_str));
7514
10
    }
7515
33.6k
  ZEND_TYPE_FOREACH_END();
7516
9.91k
}
7517
7518
/* Used by both intersection and union types prior to transforming the type list to a full zend_type */
7519
static void zend_is_type_list_redundant_by_single_type(const zend_type_list *type_list, const zend_type type)
7520
31.1k
{
7521
31.1k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(type));
7522
58.3k
  for (size_t i = 0; i < type_list->num_types - 1; i++) {
7523
27.2k
    if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7524
6.25k
      zend_is_intersection_type_redundant_by_single_type(type_list->types[i], type);
7525
6.25k
      continue;
7526
6.25k
    }
7527
20.9k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(type_list->types[i]), ZEND_TYPE_NAME(type))) {
7528
45
      zend_string *single_type_str = zend_type_to_string(type);
7529
45
      zend_error_noreturn(E_COMPILE_ERROR, "Duplicate type %s is redundant", ZSTR_VAL(single_type_str));
7530
45
    }
7531
20.9k
  }
7532
31.1k
}
7533
7534
static zend_type zend_compile_typename(zend_ast *ast);
7535
7536
static zend_type zend_compile_typename_ex(
7537
    zend_ast *ast, bool force_allow_null, bool *forced_allow_null) /* {{{ */
7538
141k
{
7539
141k
  bool is_marked_nullable = ast->attr & ZEND_TYPE_NULLABLE;
7540
141k
  zend_ast_attr orig_ast_attr = ast->attr;
7541
141k
  zend_type type = ZEND_TYPE_INIT_NONE(0);
7542
7543
141k
  if (is_marked_nullable) {
7544
2.67k
    ast->attr &= ~ZEND_TYPE_NULLABLE;
7545
2.67k
  }
7546
7547
141k
  if (ast->kind == ZEND_AST_TYPE_UNION) {
7548
13.4k
    const zend_ast_list *list = zend_ast_get_list(ast);
7549
13.4k
    zend_type_list *type_list;
7550
13.4k
    bool is_composite = false;
7551
13.4k
    bool has_only_iterable_class = true;
7552
13.4k
    ALLOCA_FLAG(use_heap)
7553
7554
13.4k
    type_list = do_alloca(ZEND_TYPE_LIST_SIZE(list->children), use_heap);
7555
13.4k
    type_list->num_types = 0;
7556
7557
42.6k
    for (uint32_t i = 0; i < list->children; i++) {
7558
29.3k
      zend_ast *type_ast = list->child[i];
7559
29.3k
      zend_type single_type;
7560
29.3k
      uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7561
7562
29.3k
      if (type_ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7563
8.41k
        has_only_iterable_class = false;
7564
8.41k
        is_composite = true;
7565
        /* The first class type can be stored directly as the type ptr payload. */
7566
8.41k
        if (ZEND_TYPE_IS_COMPLEX(type) && !ZEND_TYPE_HAS_LIST(type)) {
7567
          /* Switch from single name to name list. */
7568
2.10k
          type_list->num_types = 1;
7569
2.10k
          type_list->types[0] = type;
7570
          /* Clear MAY_BE_* type flags */
7571
2.10k
          ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7572
2.10k
        }
7573
        /* Mark type as list type */
7574
8.41k
        ZEND_TYPE_SET_LIST(type, type_list);
7575
7576
8.41k
        single_type = zend_compile_typename(type_ast);
7577
8.41k
        ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(single_type));
7578
7579
8.41k
        type_list->types[type_list->num_types++] = single_type;
7580
7581
        /* Check for trivially redundant class types */
7582
16.2k
        for (size_t i = 0; i < type_list->num_types - 1; i++) {
7583
7.82k
          if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7584
4.16k
            zend_are_intersection_types_redundant(single_type, type_list->types[i]);
7585
4.16k
            continue;
7586
4.16k
          }
7587
          /* Type from type list is a simple type */
7588
3.65k
          zend_is_intersection_type_redundant_by_single_type(single_type, type_list->types[i]);
7589
3.65k
        }
7590
8.40k
        continue;
7591
8.41k
      }
7592
7593
20.9k
      single_type = zend_compile_single_typename(type_ast);
7594
20.9k
      uint32_t single_type_mask = ZEND_TYPE_PURE_MASK(single_type);
7595
7596
20.9k
      if (single_type_mask == MAY_BE_ANY) {
7597
5
        zend_error_noreturn(E_COMPILE_ERROR, "Type mixed can only be used as a standalone type");
7598
5
      }
7599
20.8k
      if (ZEND_TYPE_IS_COMPLEX(single_type) && !ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7600
16.1k
        has_only_iterable_class = false;
7601
16.1k
      }
7602
7603
20.8k
      uint32_t type_mask_overlap = ZEND_TYPE_PURE_MASK(type) & single_type_mask;
7604
20.8k
      if (type_mask_overlap) {
7605
29
        zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap);
7606
29
        zend_string *overlap_type_str = zend_type_to_string(overlap_type);
7607
29
        zend_error_noreturn(E_COMPILE_ERROR,
7608
29
          "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str));
7609
29
      }
7610
7611
20.8k
      if ( ((type_mask & MAY_BE_TRUE) && (single_type_mask == MAY_BE_FALSE))
7612
20.8k
          || ((type_mask & MAY_BE_FALSE) && (single_type_mask == MAY_BE_TRUE)) ) {
7613
10
        zend_error_noreturn(E_COMPILE_ERROR,
7614
10
          "Type contains both true and false, bool must be used instead");
7615
10
      }
7616
20.8k
      ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type);
7617
      /* Clear MAY_BE_* type flags */
7618
20.8k
      ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK;
7619
7620
20.8k
      if (ZEND_TYPE_IS_COMPLEX(single_type)) {
7621
16.6k
        if (!ZEND_TYPE_IS_COMPLEX(type) && !is_composite) {
7622
          /* The first class type can be stored directly as the type ptr payload. */
7623
6.89k
          ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type));
7624
6.89k
          ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT;
7625
9.74k
        } else {
7626
9.74k
          if (type_list->num_types == 0) {
7627
            /* Switch from single name to name list. */
7628
3.96k
            type_list->num_types = 1;
7629
3.96k
            type_list->types[0] = type;
7630
            /* Clear MAY_BE_* type flags */
7631
3.96k
            ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7632
3.96k
            ZEND_TYPE_SET_LIST(type, type_list);
7633
3.96k
          }
7634
7635
9.74k
          type_list->types[type_list->num_types++] = single_type;
7636
7637
          /* Check for trivially redundant class types */
7638
9.74k
          zend_is_type_list_redundant_by_single_type(type_list, single_type);
7639
9.74k
        }
7640
16.6k
      }
7641
20.8k
    }
7642
7643
13.3k
    if (type_list->num_types) {
7644
11.2k
      zend_type_list *list = zend_arena_alloc(
7645
11.2k
        &CG(arena), ZEND_TYPE_LIST_SIZE(type_list->num_types));
7646
11.2k
      memcpy(list, type_list, ZEND_TYPE_LIST_SIZE(type_list->num_types));
7647
11.2k
      ZEND_TYPE_SET_LIST(type, list);
7648
11.2k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7649
      /* Inform that the type list is a union type */
7650
11.2k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7651
11.2k
    }
7652
7653
13.3k
    free_alloca(type_list, use_heap);
7654
7655
13.3k
    uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7656
13.3k
    if ((type_mask & MAY_BE_OBJECT) &&
7657
586
        ((!has_only_iterable_class && ZEND_TYPE_IS_COMPLEX(type)) || (type_mask & MAY_BE_STATIC))) {
7658
24
      zend_string *type_str = zend_type_to_string(type);
7659
24
      zend_error_noreturn(E_COMPILE_ERROR,
7660
24
        "Type %s contains both object and a class type, which is redundant",
7661
24
        ZSTR_VAL(type_str));
7662
24
    }
7663
128k
  } else if (ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7664
10.0k
    const zend_ast_list *list = zend_ast_get_list(ast);
7665
10.0k
    zend_type_list *type_list;
7666
7667
    /* Allocate the type list directly on the arena as it must be a type
7668
     * list of the same number of elements as the AST list has children */
7669
10.0k
    type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children));
7670
10.0k
    type_list->num_types = 0;
7671
7672
10.0k
    ZEND_ASSERT(list->children > 1);
7673
7674
31.4k
    for (uint32_t i = 0; i < list->children; i++) {
7675
21.5k
      zend_ast *type_ast = list->child[i];
7676
21.5k
      zend_type single_type = zend_compile_single_typename(type_ast);
7677
7678
      /* An intersection of union types cannot exist so invalidate it
7679
       * Currently only can happen with iterable getting canonicalized to Traversable|array */
7680
21.5k
      if (ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7681
5
        zend_string *standard_type_str = zend_type_to_string(single_type);
7682
5
        zend_error_noreturn(E_COMPILE_ERROR,
7683
5
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7684
5
      }
7685
      /* An intersection of standard types cannot exist so invalidate it */
7686
21.4k
      if (ZEND_TYPE_IS_ONLY_MASK(single_type)) {
7687
61
        zend_string *standard_type_str = zend_type_to_string(single_type);
7688
61
        zend_error_noreturn(E_COMPILE_ERROR,
7689
61
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7690
61
      }
7691
      /* Check for "self" and "parent" too */
7692
21.4k
      if (
7693
21.4k
        zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_SELF))
7694
21.4k
        || zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_PARENT))
7695
21.4k
      ) {
7696
13
        zend_error_noreturn(E_COMPILE_ERROR,
7697
13
          "Type %s cannot be part of an intersection type", ZSTR_VAL(ZEND_TYPE_NAME(single_type)));
7698
13
      }
7699
7700
      /* Add type to the type list */
7701
21.4k
      type_list->types[type_list->num_types++] = single_type;
7702
7703
      /* Check for trivially redundant class types */
7704
21.4k
      zend_is_type_list_redundant_by_single_type(type_list, single_type);
7705
21.4k
    }
7706
7707
9.93k
    ZEND_ASSERT(list->children == type_list->num_types);
7708
7709
    /* An implicitly nullable intersection type needs to be converted to a DNF type */
7710
9.93k
    if (force_allow_null) {
7711
632
      zend_type intersection_type = ZEND_TYPE_INIT_NONE(0);
7712
632
      ZEND_TYPE_SET_LIST(intersection_type, type_list);
7713
632
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_INTERSECTION_BIT;
7714
632
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_ARENA_BIT;
7715
7716
632
      zend_type_list *dnf_type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(1));
7717
632
      dnf_type_list->num_types = 1;
7718
632
      dnf_type_list->types[0] = intersection_type;
7719
632
      ZEND_TYPE_SET_LIST(type, dnf_type_list);
7720
      /* Inform that the type list is a DNF type */
7721
632
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7722
632
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7723
9.29k
    } else {
7724
9.29k
      ZEND_TYPE_SET_LIST(type, type_list);
7725
      /* Inform that the type list is an intersection type */
7726
9.29k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_INTERSECTION_BIT;
7727
9.29k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7728
9.29k
    }
7729
118k
  } else {
7730
118k
    type = zend_compile_single_typename(ast);
7731
118k
  }
7732
7733
141k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
7734
7735
141k
  if (type_mask == MAY_BE_ANY && is_marked_nullable) {
7736
13
    zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be marked as nullable since mixed already includes null");
7737
13
  }
7738
7739
141k
  if ((type_mask & MAY_BE_NULL) && is_marked_nullable) {
7740
5
    zend_error_noreturn(E_COMPILE_ERROR, "null cannot be marked as nullable");
7741
5
  }
7742
7743
141k
  if (force_allow_null && !is_marked_nullable && !(type_mask & MAY_BE_NULL)) {
7744
844
    *forced_allow_null = true;
7745
844
  }
7746
7747
141k
  if (is_marked_nullable || force_allow_null) {
7748
3.56k
    ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
7749
3.56k
    type_mask = ZEND_TYPE_PURE_MASK(type);
7750
3.56k
  }
7751
7752
141k
  if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_VOID)) {
7753
10
    zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type");
7754
10
  }
7755
7756
141k
  if ((type_mask & MAY_BE_NEVER) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_NEVER)) {
7757
6
    zend_error_noreturn(E_COMPILE_ERROR, "never can only be used as a standalone type");
7758
6
  }
7759
7760
141k
  ast->attr = orig_ast_attr;
7761
141k
  return type;
7762
141k
}
7763
/* }}} */
7764
7765
static zend_type zend_compile_typename(zend_ast *ast)
7766
43.5k
{
7767
43.5k
  bool forced_allow_null;
7768
43.5k
  return zend_compile_typename_ex(ast, false, &forced_allow_null);
7769
43.5k
}
7770
7771
/* May convert value from int to float. */
7772
static bool zend_is_valid_default_value(zend_type type, zval *value)
7773
3.84k
{
7774
3.84k
  ZEND_ASSERT(ZEND_TYPE_IS_SET(type));
7775
3.84k
  if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) {
7776
3.04k
    return 1;
7777
3.04k
  }
7778
802
  if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) {
7779
    /* Integers are allowed as initializers for floating-point values. */
7780
673
    convert_to_double(value);
7781
673
    return 1;
7782
673
  }
7783
129
  return 0;
7784
802
}
7785
7786
static void zend_compile_attributes(
7787
  HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target, uint32_t promoted
7788
319k
) /* {{{ */ {
7789
319k
  zend_attribute *attr;
7790
319k
  zend_internal_attribute *config;
7791
7792
319k
  const zend_ast_list *list = zend_ast_get_list(ast);
7793
319k
  uint32_t g, i, j;
7794
7795
319k
  ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE_LIST);
7796
7797
641k
  for (g = 0; g < list->children; g++) {
7798
321k
    const zend_ast_list *group = zend_ast_get_list(list->child[g]);
7799
7800
321k
    ZEND_ASSERT(group->kind == ZEND_AST_ATTRIBUTE_GROUP);
7801
7802
900k
    for (i = 0; i < group->children; i++) {
7803
579k
      ZEND_ASSERT(group->child[i]->kind == ZEND_AST_ATTRIBUTE);
7804
7805
579k
      const zend_ast *el = group->child[i];
7806
7807
579k
      if (el->child[1] &&
7808
16.5k
          el->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) {
7809
10
          zend_error_noreturn(E_COMPILE_ERROR,
7810
10
              "Cannot create Closure as attribute argument");
7811
10
      }
7812
7813
579k
      zend_string *name = zend_resolve_class_name_ast(el->child[0]);
7814
579k
      zend_string *lcname = zend_string_tolower_ex(name, false);
7815
579k
      zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL;
7816
7817
579k
      config = zend_internal_attribute_get(lcname);
7818
579k
      zend_string_release(lcname);
7819
7820
      /* Exclude internal attributes that do not match on promoted properties. */
7821
579k
      if (config && !(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7822
543
        if (promoted & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL)) {
7823
9
          zend_string_release(name);
7824
9
          continue;
7825
9
        }
7826
543
      }
7827
7828
579k
      uint32_t flags = (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES)
7829
579k
        ? ZEND_ATTRIBUTE_STRICT_TYPES : 0;
7830
579k
      attr = zend_add_attribute(
7831
579k
        attributes, name, args ? args->children : 0, flags, offset, el->lineno);
7832
579k
      zend_string_release(name);
7833
7834
      /* Populate arguments */
7835
579k
      if (args) {
7836
16.5k
        ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST);
7837
7838
16.5k
        bool uses_named_args = false;
7839
42.1k
        for (j = 0; j < args->children; j++) {
7840
25.5k
          zend_ast **arg_ast_ptr = &args->child[j];
7841
25.5k
          zend_ast *arg_ast = *arg_ast_ptr;
7842
7843
25.5k
          if (arg_ast->kind == ZEND_AST_UNPACK) {
7844
5
            zend_error_noreturn(E_COMPILE_ERROR,
7845
5
              "Cannot use unpacking in attribute argument list");
7846
5
          }
7847
7848
25.5k
          if (arg_ast->kind == ZEND_AST_NAMED_ARG) {
7849
1.86k
            attr->args[j].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0]));
7850
1.86k
            arg_ast_ptr = &arg_ast->child[1];
7851
1.86k
            uses_named_args = true;
7852
7853
9.97k
            for (uint32_t k = 0; k < j; k++) {
7854
8.11k
              if (attr->args[k].name &&
7855
6.96k
                  zend_string_equals(attr->args[k].name, attr->args[j].name)) {
7856
9
                zend_error_noreturn(E_COMPILE_ERROR, "Duplicate named parameter $%s",
7857
9
                  ZSTR_VAL(attr->args[j].name));
7858
9
              }
7859
8.11k
            }
7860
23.7k
          } else if (uses_named_args) {
7861
20
            zend_error_noreturn(E_COMPILE_ERROR,
7862
20
              "Cannot use positional argument after named argument");
7863
20
          }
7864
7865
25.5k
          zend_const_expr_to_zval(
7866
25.5k
            &attr->args[j].value, arg_ast_ptr, /* allow_dynamic */ true);
7867
25.5k
        }
7868
16.5k
      }
7869
579k
    }
7870
321k
  }
7871
7872
319k
  if (*attributes != NULL) {
7873
    /* Allow delaying target validation for forward compatibility. */
7874
319k
    const zend_attribute *delayed_target_validation = NULL;
7875
319k
    if (target == ZEND_ATTRIBUTE_TARGET_PARAMETER) {
7876
14.8k
      ZEND_ASSERT(offset >= 1);
7877
      /* zend_get_parameter_attribute_str will add 1 too */
7878
14.8k
      delayed_target_validation = zend_get_parameter_attribute_str(
7879
14.8k
        *attributes,
7880
14.8k
        "delayedtargetvalidation",
7881
14.8k
        strlen("delayedtargetvalidation"),
7882
14.8k
        offset - 1
7883
14.8k
      );
7884
304k
    } else {
7885
304k
      delayed_target_validation = zend_get_attribute_str(
7886
304k
        *attributes,
7887
304k
        "delayedtargetvalidation",
7888
304k
        strlen("delayedtargetvalidation")
7889
304k
      );
7890
304k
    }
7891
    /* Validate attributes in a secondary loop (needed to detect repeated attributes). */
7892
2.22M
    ZEND_HASH_PACKED_FOREACH_PTR(*attributes, attr) {
7893
2.22M
      if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) {
7894
791k
        continue;
7895
791k
      }
7896
7897
2.22M
      bool run_validator = true;
7898
4.23k
      if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7899
274
        if (delayed_target_validation == NULL) {
7900
30
          zend_string *location = zend_get_attribute_target_names(target);
7901
30
          zend_string *allowed = zend_get_attribute_target_names(config->flags);
7902
7903
30
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
7904
30
            ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
7905
30
          );
7906
30
        }
7907
244
        run_validator = false;
7908
244
      }
7909
7910
4.20k
      if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
7911
4.20k
        if (zend_is_attribute_repeated(*attributes, attr)) {
7912
22
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name));
7913
22
        }
7914
4.20k
      }
7915
7916
      /* Validators are not run if the target is already invalid */
7917
4.17k
      if (run_validator && config->validator != NULL) {
7918
2.46k
        zend_string *error = config->validator(attr, target, CG(active_class_entry));
7919
2.46k
        if (error != NULL) {
7920
222
          if (delayed_target_validation == NULL) {
7921
87
            zend_error_noreturn(E_COMPILE_ERROR, "%s", ZSTR_VAL(error));
7922
135
          } else {
7923
135
            attr->validation_error = error;
7924
135
          }
7925
222
        }
7926
2.46k
      }
7927
4.17k
    } ZEND_HASH_FOREACH_END();
7928
319k
  }
7929
319k
}
7930
/* }}} */
7931
7932
static void zend_compile_property_hooks(
7933
    zend_property_info *prop_info, zend_string *prop_name,
7934
    zend_ast *prop_type_ast, const zend_ast_list *hooks);
7935
7936
typedef struct {
7937
  const zend_string *property_name;
7938
  bool uses_property;
7939
} find_property_usage_context;
7940
7941
static void zend_property_hook_find_property_usage(zend_ast **ast_ptr, void *_context) /* {{{ */
7942
28.5k
{
7943
28.5k
  zend_ast *ast = *ast_ptr;
7944
28.5k
  find_property_usage_context *context = (find_property_usage_context *) _context;
7945
7946
28.5k
  if (ast == NULL) {
7947
201
    return;
7948
28.3k
  } else if (ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_NULLSAFE_PROP) {
7949
2.27k
    const zend_ast *object_ast = ast->child[0];
7950
2.27k
    zend_ast *property_ast = ast->child[1];
7951
7952
2.27k
    if (object_ast->kind == ZEND_AST_VAR
7953
2.02k
     && object_ast->child[0]->kind == ZEND_AST_ZVAL
7954
1.99k
     && property_ast->kind == ZEND_AST_ZVAL) {
7955
1.92k
      const zval *object = zend_ast_get_zval(object_ast->child[0]);
7956
1.92k
      const zval *property = zend_ast_get_zval(property_ast);
7957
1.92k
      if (Z_TYPE_P(object) == IS_STRING
7958
1.89k
        && Z_TYPE_P(property) == IS_STRING
7959
1.89k
        && zend_string_equals_literal(Z_STR_P(object), "this")
7960
1.61k
        && zend_string_equals(Z_STR_P(property), context->property_name)) {
7961
649
        context->uses_property = true;
7962
        /* No need to look for references in this branch. */
7963
649
        return;
7964
649
      }
7965
1.92k
    }
7966
2.27k
  }
7967
7968
  /* Don't search across function/class boundaries. */
7969
27.7k
  if (!zend_ast_is_special(ast)) {
7970
17.9k
    zend_ast_apply(ast, zend_property_hook_find_property_usage, context);
7971
17.9k
  }
7972
27.7k
}
7973
7974
static bool zend_property_hook_uses_property(const zend_string *property_name, const zend_string *hook_name, zend_ast *hook_ast)
7975
3.94k
{
7976
3.94k
  if (zend_string_equals_literal_ci(hook_name, "set")
7977
1.59k
   && hook_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
7978
424
    return true;
7979
424
  }
7980
7981
3.51k
  find_property_usage_context context = { property_name, false };
7982
3.51k
  zend_property_hook_find_property_usage(&hook_ast, &context);
7983
3.51k
  return context.uses_property;
7984
3.94k
}
7985
7986
static bool zend_property_is_virtual(const zend_class_entry *ce, const zend_string *property_name, zend_ast *hooks_ast)
7987
34.5k
{
7988
34.5k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
7989
214
    return true;
7990
214
  }
7991
34.3k
  if (!hooks_ast) {
7992
31.1k
    return false;
7993
31.1k
  }
7994
7995
34.3k
  bool is_virtual = true;
7996
7997
3.11k
  const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
7998
7.50k
  for (uint32_t i = 0; i < hooks->children; i++) {
7999
4.39k
    const zend_ast_decl *hook = (const zend_ast_decl *) hooks->child[i];
8000
4.39k
    zend_ast *body = hook->child[2];
8001
4.39k
    if (body && zend_property_hook_uses_property(property_name, hook->name, body)) {
8002
1.03k
      is_virtual = false;
8003
1.03k
    }
8004
4.39k
  }
8005
8006
3.11k
  return is_virtual;
8007
34.3k
}
8008
8009
static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */
8010
598k
{
8011
598k
  zend_ast_list *list = zend_ast_get_list(ast);
8012
598k
  uint32_t i;
8013
598k
  zend_op_array *op_array = CG(active_op_array);
8014
598k
  zend_arg_info *arg_infos;
8015
8016
598k
  if (return_type_ast || fallback_return_type) {
8017
    /* Use op_array->arg_info[-1] for return type */
8018
16.4k
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
8019
16.4k
    arg_infos->name = NULL;
8020
16.4k
    if (return_type_ast) {
8021
15.8k
      arg_infos->type = zend_compile_typename(return_type_ast);
8022
15.8k
      ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS(
8023
15.8k
        (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0, /* is_tentative */ 0);
8024
15.8k
    } else {
8025
667
      arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0);
8026
667
    }
8027
16.4k
    arg_infos++;
8028
16.4k
    op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
8029
8030
16.4k
    if (ZEND_TYPE_CONTAINS_CODE(arg_infos[-1].type, IS_VOID)
8031
3.51k
        && (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
8032
121
      zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
8033
121
      zend_error(E_DEPRECATED, "%s(): Returning by reference from a void function is deprecated", ZSTR_VAL(func_name));
8034
121
      zend_string_release(func_name);
8035
121
    }
8036
581k
  } else {
8037
581k
    if (list->children == 0) {
8038
259k
      return;
8039
259k
    }
8040
322k
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
8041
322k
  }
8042
8043
  /* Find last required parameter number for deprecation message. */
8044
338k
  uint32_t last_required_param = (uint32_t) -1;
8045
694k
  for (i = 0; i < list->children; ++i) {
8046
356k
    zend_ast *param_ast = list->child[i];
8047
356k
    zend_ast *default_ast_ptr = param_ast->child[2];
8048
356k
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
8049
356k
    if (!default_ast_ptr && !is_variadic) {
8050
349k
      last_required_param = i;
8051
349k
    }
8052
356k
  }
8053
8054
338k
  const uint32_t promotion_flags = ZEND_ACC_PPP_MASK | ZEND_ACC_PPP_SET_MASK | ZEND_ACC_READONLY | ZEND_ACC_FINAL;
8055
694k
  for (i = 0; i < list->children; ++i) {
8056
355k
    zend_ast *param_ast = list->child[i];
8057
355k
    zend_ast *type_ast = param_ast->child[0];
8058
355k
    zend_ast *var_ast = param_ast->child[1];
8059
355k
    zend_ast **default_ast_ptr = &param_ast->child[2];
8060
355k
    zend_ast *attributes_ast = param_ast->child[3];
8061
355k
    zend_ast *doc_comment_ast = param_ast->child[4];
8062
355k
    zend_ast *hooks_ast = param_ast->child[5];
8063
355k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast));
8064
355k
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
8065
355k
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
8066
355k
    uint32_t property_flags = param_ast->attr & promotion_flags;
8067
355k
    bool is_promoted = property_flags || hooks_ast;
8068
8069
355k
    CG(zend_lineno) = param_ast->lineno;
8070
8071
355k
    znode var_node, default_node;
8072
355k
    uint8_t opcode;
8073
355k
    zend_op *opline;
8074
355k
    zend_arg_info *arg_info;
8075
8076
355k
    if (zend_is_auto_global(name)) {
8077
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
8078
2
        ZSTR_VAL(name));
8079
2
    }
8080
8081
355k
    var_node.op_type = IS_CV;
8082
355k
    var_node.u.op.var = lookup_cv(name);
8083
8084
355k
    if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
8085
23
      zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
8086
23
        ZSTR_VAL(name));
8087
355k
    } else if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8088
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
8089
355k
    } else if (zend_string_equals_literal(name, "http_response_header")) {
8090
51
      CG(context).has_assigned_to_http_response_header = true;
8091
51
    }
8092
8093
355k
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
8094
6
      zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
8095
6
    }
8096
8097
355k
    if (is_variadic) {
8098
1.05k
      opcode = ZEND_RECV_VARIADIC;
8099
1.05k
      default_node.op_type = IS_UNUSED;
8100
1.05k
      op_array->fn_flags |= ZEND_ACC_VARIADIC;
8101
8102
1.05k
      if (*default_ast_ptr) {
8103
5
        zend_error_noreturn(E_COMPILE_ERROR,
8104
5
          "Variadic parameter cannot have a default value");
8105
5
      }
8106
354k
    } else if (*default_ast_ptr) {
8107
      /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
8108
5.84k
      uint32_t cops = CG(compiler_options);
8109
5.84k
      CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
8110
5.84k
      opcode = ZEND_RECV_INIT;
8111
5.84k
      default_node.op_type = IS_CONST;
8112
5.84k
      zend_const_expr_to_zval(
8113
5.84k
        &default_node.u.constant, default_ast_ptr, /* allow_dynamic */ true);
8114
5.84k
      CG(compiler_options) = cops;
8115
349k
    } else {
8116
349k
      opcode = ZEND_RECV;
8117
349k
      default_node.op_type = IS_UNUSED;
8118
349k
      op_array->required_num_args = i + 1;
8119
349k
    }
8120
8121
355k
    arg_info = &arg_infos[i];
8122
355k
    arg_info->name = zend_string_copy(name);
8123
355k
    arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0);
8124
355k
    arg_info->default_value = NULL;
8125
8126
355k
    if (attributes_ast) {
8127
14.8k
      zend_compile_attributes(
8128
14.8k
        &op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER,
8129
14.8k
        is_promoted ? ZEND_ATTRIBUTE_TARGET_PROPERTY : 0
8130
14.8k
      );
8131
14.8k
    }
8132
8133
355k
    bool forced_allow_nullable = false;
8134
355k
    if (type_ast) {
8135
98.2k
      uint32_t default_type = *default_ast_ptr ? Z_TYPE(default_node.u.constant) : IS_UNDEF;
8136
98.2k
      bool force_nullable = default_type == IS_NULL && !is_promoted;
8137
8138
98.2k
      op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
8139
98.2k
      arg_info->type = zend_compile_typename_ex(type_ast, force_nullable, &forced_allow_nullable);
8140
98.2k
      if (forced_allow_nullable) {
8141
843
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
8142
843
        zend_error(E_DEPRECATED,
8143
843
           "%s(): Implicitly marking parameter $%s as nullable is deprecated, the explicit nullable type "
8144
843
           "must be used instead", ZSTR_VAL(func_name), ZSTR_VAL(name));
8145
843
        zend_string_release(func_name);
8146
843
      }
8147
8148
98.2k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) {
8149
6
        zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");
8150
6
      }
8151
8152
98.2k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_NEVER) {
8153
5
        zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type");
8154
5
      }
8155
8156
98.2k
      if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable
8157
620
          && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) {
8158
34
        zend_string *type_str = zend_type_to_string(arg_info->type);
8159
34
        zend_error_noreturn(E_COMPILE_ERROR,
8160
34
          "Cannot use %s as default value for parameter $%s of type %s",
8161
34
          zend_get_type_by_const(default_type),
8162
34
          ZSTR_VAL(name), ZSTR_VAL(type_str));
8163
34
      }
8164
98.2k
    }
8165
355k
    if (last_required_param != (uint32_t) -1
8166
351k
     && i < last_required_param
8167
24.1k
     && default_node.op_type == IS_CONST) {
8168
      /* Ignore parameters of the form "Type $param = null".
8169
       * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
8170
1.40k
      if (!forced_allow_nullable) {
8171
620
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
8172
620
        zend_ast *required_param_ast = list->child[last_required_param];
8173
620
        zend_error(E_DEPRECATED,
8174
620
          "%s(): Optional parameter $%s declared before required parameter $%s "
8175
620
          "is implicitly treated as a required parameter",
8176
620
          ZSTR_VAL(func_name), ZSTR_VAL(name), ZSTR_VAL(zend_ast_get_str(required_param_ast->child[1])));
8177
620
        zend_string_release(func_name);
8178
620
      }
8179
8180
      /* Regardless of whether we issue a deprecation, convert this parameter into
8181
       * a required parameter without a default value. This ensures that it cannot be
8182
       * used as an optional parameter even with named parameters. */
8183
1.40k
      opcode = ZEND_RECV;
8184
1.40k
      default_node.op_type = IS_UNUSED;
8185
1.40k
      zval_ptr_dtor(&default_node.u.constant);
8186
1.40k
    }
8187
8188
355k
    opline = zend_emit_op(NULL, opcode, NULL, &default_node);
8189
355k
    SET_NODE(opline->result, &var_node);
8190
355k
    opline->op1.num = i + 1;
8191
8192
355k
    uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic, /* is_tentative */ 0)
8193
355k
      | (is_promoted ? _ZEND_IS_PROMOTED_BIT : 0);
8194
355k
    ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags;
8195
355k
    if (opcode == ZEND_RECV) {
8196
350k
      opline->op2.num = type_ast ?
8197
254k
        ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY;
8198
350k
    }
8199
8200
355k
    if (is_promoted) {
8201
892
      const zend_op_array *active_op_array = CG(active_op_array);
8202
892
      zend_class_entry *scope = active_op_array->scope;
8203
8204
892
      bool is_ctor =
8205
892
        scope && zend_is_constructor(active_op_array->function_name);
8206
892
      if (!is_ctor) {
8207
25
        zend_error_noreturn(E_COMPILE_ERROR,
8208
25
          "Cannot declare promoted property outside a constructor");
8209
25
      }
8210
867
      if ((active_op_array->fn_flags & ZEND_ACC_ABSTRACT)
8211
862
          || (scope->ce_flags & ZEND_ACC_INTERFACE)) {
8212
5
        zend_error_noreturn(E_COMPILE_ERROR,
8213
5
          "Cannot declare promoted property in an abstract constructor");
8214
5
      }
8215
862
      if (is_variadic) {
8216
5
        zend_error_noreturn(E_COMPILE_ERROR,
8217
5
          "Cannot declare variadic promoted property");
8218
5
      }
8219
857
      if (zend_hash_exists(&scope->properties_info, name)) {
8220
5
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
8221
5
          ZSTR_VAL(scope->name), ZSTR_VAL(name));
8222
5
      }
8223
852
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_CALLABLE) {
8224
5
        zend_string *str = zend_type_to_string(arg_info->type);
8225
5
        zend_error_noreturn(E_COMPILE_ERROR,
8226
5
          "Property %s::$%s cannot have type %s",
8227
5
          ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str));
8228
5
      }
8229
8230
847
      if (!(property_flags & ZEND_ACC_READONLY) && (scope->ce_flags & ZEND_ACC_READONLY_CLASS)) {
8231
22
        property_flags |= ZEND_ACC_READONLY;
8232
22
      }
8233
8234
      /* Recompile the type, as it has different memory management requirements. */
8235
847
      zend_type type = ZEND_TYPE_INIT_NONE(0);
8236
847
      if (type_ast) {
8237
667
        type = zend_compile_typename(type_ast);
8238
667
      }
8239
8240
      /* Don't give the property an explicit default value. For typed properties this means
8241
       * uninitialized, for untyped properties it means an implicit null default value.
8242
       * Properties with hooks get an implicit default value of undefined until inheritance,
8243
       * where it is changed to null only once we know it is not virtual. If we were to set it
8244
       * here, we couldn't verify that a true virtual property must not have an explicit
8245
       * default value. */
8246
847
      zval default_value;
8247
847
      if (ZEND_TYPE_IS_SET(type) || hooks_ast) {
8248
684
        ZVAL_UNDEF(&default_value);
8249
684
      } else {
8250
163
        if (property_flags & ZEND_ACC_READONLY) {
8251
10
          zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
8252
10
            ZSTR_VAL(scope->name), ZSTR_VAL(name));
8253
10
        }
8254
8255
153
        ZVAL_NULL(&default_value);
8256
153
      }
8257
8258
837
      zend_string *doc_comment =
8259
837
        doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
8260
837
      zend_property_info *prop = zend_declare_typed_property(
8261
837
        scope, name, &default_value,
8262
837
        property_flags | (zend_property_is_virtual(scope, name, hooks_ast) ? ZEND_ACC_VIRTUAL : 0) | ZEND_ACC_PROMOTED,
8263
837
        doc_comment, type);
8264
837
      if (hooks_ast) {
8265
72
        const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
8266
72
        zend_compile_property_hooks(prop, name, type_ast, hooks);
8267
72
      }
8268
837
      if (attributes_ast) {
8269
68
        zend_compile_attributes(
8270
68
          &prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, ZEND_ATTRIBUTE_TARGET_PARAMETER);
8271
8272
68
        zend_attribute *override_attribute = zend_get_attribute_str(prop->attributes, "override", sizeof("override")-1);
8273
68
        if (override_attribute) {
8274
8
          prop->flags |= ZEND_ACC_OVERRIDE;
8275
8
        }
8276
68
      }
8277
837
    }
8278
355k
  }
8279
8280
  /* These are assigned at the end to avoid uninitialized memory in case of an error */
8281
338k
  op_array->num_args = list->children;
8282
338k
  op_array->arg_info = arg_infos;
8283
8284
  /* Don't count the variadic argument */
8285
338k
  if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
8286
1.03k
    op_array->num_args--;
8287
1.03k
  }
8288
338k
  zend_set_function_arg_flags((zend_function*)op_array);
8289
8290
694k
  for (i = 0; i < list->children; i++) {
8291
355k
    zend_ast *param_ast = list->child[i];
8292
355k
    zend_ast *hooks_ast = param_ast->child[5];
8293
355k
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
8294
355k
    uint32_t flags = param_ast->attr & promotion_flags;
8295
355k
    bool is_promoted = flags || hooks_ast;
8296
355k
    if (!is_promoted) {
8297
354k
      continue;
8298
354k
    }
8299
8300
820
    CG(zend_lineno) = param_ast->lineno;
8301
8302
    /* Emit $this->prop = $prop for promoted properties. */
8303
820
    zend_string *name = zend_ast_get_str(param_ast->child[1]);
8304
820
    znode name_node, value_node;
8305
820
    name_node.op_type = IS_CONST;
8306
820
    ZVAL_STR_COPY(&name_node.u.constant, name);
8307
820
    value_node.op_type = IS_CV;
8308
820
    value_node.u.op.var = lookup_cv(name);
8309
8310
820
    zend_op *opline = zend_emit_op(NULL,
8311
820
      is_ref ? ZEND_ASSIGN_OBJ_REF : ZEND_ASSIGN_OBJ, NULL, &name_node);
8312
820
    opline->extended_value = zend_alloc_cache_slots(3);
8313
820
    zend_emit_op_data(&value_node);
8314
820
  }
8315
338k
}
8316
/* }}} */
8317
8318
static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */
8319
1.42k
{
8320
1.42k
  const zend_ast_list *list = zend_ast_get_list(uses_ast);
8321
1.42k
  uint32_t i;
8322
8323
1.42k
  if (!list->children) {
8324
0
    return;
8325
0
  }
8326
8327
1.42k
  if (!op_array->static_variables) {
8328
1.42k
    op_array->static_variables = zend_new_array(8);
8329
1.42k
  }
8330
8331
4.67k
  for (i = 0; i < list->children; ++i) {
8332
3.25k
    zend_ast *var_name_ast = list->child[i];
8333
3.25k
    zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast));
8334
3.25k
    uint32_t mode = var_name_ast->attr;
8335
3.25k
    zend_op *opline;
8336
3.25k
    zval *value;
8337
8338
3.25k
    if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8339
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
8340
5
    }
8341
8342
3.25k
    if (zend_is_auto_global(var_name)) {
8343
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable");
8344
5
    }
8345
8346
3.24k
    value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval));
8347
3.24k
    if (!value) {
8348
6
      zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8349
6
        "Cannot use variable $%S twice", var_name);
8350
6
    }
8351
8352
3.24k
    CG(zend_lineno) = zend_ast_get_lineno(var_name_ast);
8353
8354
3.24k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8355
3.24k
    opline->op2_type = IS_CV;
8356
3.24k
    opline->op2.var = lookup_cv(var_name);
8357
3.24k
    opline->extended_value =
8358
3.24k
      (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode;
8359
3.24k
  }
8360
1.42k
}
8361
/* }}} */
8362
8363
typedef struct {
8364
  HashTable uses;
8365
  bool varvars_used;
8366
} closure_info;
8367
8368
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast);
8369
8370
1.24M
static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
8371
1.24M
  if (!ast) {
8372
3.21k
    return;
8373
3.21k
  }
8374
8375
1.24M
  if (ast->kind == ZEND_AST_VAR) {
8376
155k
    zend_ast *name_ast = ast->child[0];
8377
155k
    if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) {
8378
153k
      zend_string *name = zend_ast_get_str(name_ast);
8379
153k
      if (zend_is_auto_global(name)) {
8380
        /* These is no need to explicitly import auto-globals. */
8381
4.41k
        return;
8382
4.41k
      }
8383
8384
149k
      if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8385
        /* $this does not need to be explicitly imported. */
8386
9.95k
        return;
8387
9.95k
      }
8388
8389
139k
      zend_hash_add_empty_element(&info->uses, name);
8390
139k
    } else {
8391
1.91k
      info->varvars_used = true;
8392
1.91k
      find_implicit_binds_recursively(info, name_ast);
8393
1.91k
    }
8394
1.08M
  } else if (zend_ast_is_list(ast)) {
8395
31.9k
    const zend_ast_list *list = zend_ast_get_list(ast);
8396
31.9k
    uint32_t i;
8397
299k
    for (i = 0; i < list->children; i++) {
8398
267k
      find_implicit_binds_recursively(info, list->child[i]);
8399
267k
    }
8400
1.05M
  } else if (ast->kind == ZEND_AST_CLOSURE) {
8401
    /* For normal closures add the use() list. */
8402
145
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8403
145
    zend_ast *uses_ast = closure_ast->child[1];
8404
145
    if (uses_ast) {
8405
80
      const zend_ast_list *uses_list = zend_ast_get_list(uses_ast);
8406
80
      uint32_t i;
8407
596
      for (i = 0; i < uses_list->children; i++) {
8408
516
        zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i]));
8409
516
      }
8410
80
    }
8411
1.05M
  } else if (ast->kind == ZEND_AST_ARROW_FUNC) {
8412
    /* For arrow functions recursively check the expression. */
8413
394k
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8414
394k
    closure_info inner_info;
8415
394k
    find_implicit_binds(&inner_info, closure_ast->child[0], closure_ast->child[2]);
8416
394k
    if (inner_info.varvars_used) {
8417
4.19k
      info->varvars_used = true;
8418
4.19k
    }
8419
394k
    if (zend_hash_num_elements(&inner_info.uses)) {
8420
366k
      zend_hash_copy(&info->uses, &inner_info.uses, NULL);
8421
366k
    }
8422
394k
    zend_hash_destroy(&inner_info.uses);
8423
661k
  } else if (!zend_ast_is_special(ast)) {
8424
478k
    uint32_t i, children = zend_ast_get_num_children(ast);
8425
1.04M
    for (i = 0; i < children; i++) {
8426
567k
      find_implicit_binds_recursively(info, ast->child[i]);
8427
567k
    }
8428
478k
  }
8429
1.24M
}
8430
8431
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast)
8432
410k
{
8433
410k
  const zend_ast_list *param_list = zend_ast_get_list(params_ast);
8434
410k
  uint32_t i;
8435
8436
410k
  zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0);
8437
410k
  info->varvars_used = false;
8438
8439
410k
  find_implicit_binds_recursively(info, stmt_ast);
8440
8441
  /* Remove variables that are parameters */
8442
460k
  for (i = 0; i < param_list->children; i++) {
8443
49.9k
    const zend_ast *param_ast = param_list->child[i];
8444
49.9k
    zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1]));
8445
49.9k
  }
8446
410k
}
8447
8448
static void compile_implicit_lexical_binds(
8449
    const closure_info *info, znode *closure, zend_op_array *op_array)
8450
15.4k
{
8451
15.4k
  zend_string *var_name;
8452
15.4k
  zend_op *opline;
8453
8454
  /* TODO We might want to use a special binding mode if varvars_used is set. */
8455
15.4k
  if (zend_hash_num_elements(&info->uses) == 0) {
8456
7.82k
    return;
8457
7.82k
  }
8458
8459
7.64k
  if (!op_array->static_variables) {
8460
7.64k
    op_array->static_variables = zend_new_array(8);
8461
7.64k
  }
8462
8463
135k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8464
135k
    zval *value = zend_hash_add(
8465
135k
      op_array->static_variables, var_name, &EG(uninitialized_zval));
8466
135k
    uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData);
8467
8468
135k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8469
135k
    opline->op2_type = IS_CV;
8470
135k
    opline->op2.var = lookup_cv(var_name);
8471
135k
    opline->extended_value = offset | ZEND_BIND_IMPLICIT;
8472
135k
  ZEND_HASH_FOREACH_END();
8473
7.64k
}
8474
8475
static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
8476
1.41k
{
8477
1.41k
  const zend_op_array *op_array = CG(active_op_array);
8478
1.41k
  const zend_ast_list *list = zend_ast_get_list(ast);
8479
1.41k
  uint32_t i;
8480
8481
4.63k
  for (i = 0; i < list->children; ++i) {
8482
3.22k
    uint32_t mode = ZEND_BIND_EXPLICIT;
8483
3.22k
    zend_ast *var_ast = list->child[i];
8484
3.22k
    zend_string *var_name = zend_ast_get_str(var_ast);
8485
3.22k
    zval zv;
8486
3.22k
    ZVAL_NULL(&zv);
8487
8488
3.22k
    {
8489
3.22k
      int i;
8490
9.21k
      for (i = 0; i < op_array->last_var; i++) {
8491
5.98k
        if (zend_string_equals(op_array->vars[i], var_name)) {
8492
5
          zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8493
5
            "Cannot use lexical variable $%S as a parameter name", var_name);
8494
5
        }
8495
5.98k
      }
8496
3.22k
    }
8497
8498
3.22k
    CG(zend_lineno) = zend_ast_get_lineno(var_ast);
8499
8500
3.22k
    if (var_ast->attr) {
8501
1.54k
      mode |= ZEND_BIND_REF;
8502
1.54k
    }
8503
8504
3.22k
    zend_compile_static_var_common(var_name, &zv, mode);
8505
3.22k
  }
8506
1.41k
}
8507
/* }}} */
8508
8509
static void zend_compile_implicit_closure_uses(const closure_info *info)
8510
15.4k
{
8511
15.4k
  zend_string *var_name;
8512
150k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8513
150k
    zval zv;
8514
150k
    ZVAL_NULL(&zv);
8515
150k
    zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT);
8516
150k
  ZEND_HASH_FOREACH_END();
8517
15.4k
}
8518
8519
793
static void add_stringable_interface(zend_class_entry *ce) {
8520
1.34k
  for (uint32_t i = 0; i < ce->num_interfaces; i++) {
8521
584
    if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) {
8522
      /* Interface already explicitly implemented */
8523
28
      return;
8524
28
    }
8525
584
  }
8526
8527
765
  ce->num_interfaces++;
8528
765
  ce->interface_names =
8529
765
    erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
8530
  // TODO: Add known interned strings instead?
8531
765
  ce->interface_names[ce->num_interfaces - 1].name =
8532
765
    ZSTR_INIT_LITERAL("Stringable", 0);
8533
765
  ce->interface_names[ce->num_interfaces - 1].lc_name =
8534
765
    ZSTR_INIT_LITERAL("stringable", 0);
8535
765
}
8536
8537
static zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, bool has_body) /* {{{ */
8538
87.1k
{
8539
87.1k
  zend_class_entry *ce = CG(active_class_entry);
8540
87.1k
  bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
8541
87.1k
  uint32_t fn_flags = op_array->fn_flags;
8542
8543
87.1k
  zend_string *lcname;
8544
8545
87.1k
  if (fn_flags & ZEND_ACC_READONLY) {
8546
0
    zend_error(E_COMPILE_ERROR, "Cannot use 'readonly' as method modifier");
8547
0
  }
8548
8549
87.1k
  if ((fn_flags & ZEND_ACC_PRIVATE) && (fn_flags & ZEND_ACC_FINAL) && !zend_is_constructor(name)) {
8550
72
    zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes");
8551
72
  }
8552
8553
87.1k
  if ((fn_flags & ZEND_ACC_ABSTRACT)
8554
434
   && !(ce->ce_flags & (ZEND_ACC_EXPLICIT_ABSTRACT_CLASS|ZEND_ACC_TRAIT))) {
8555
    // Don't say that the class should be declared abstract if it is
8556
    // anonymous or an enum and can't be abstract
8557
29
    if (ce->ce_flags & ZEND_ACC_ANON_CLASS) {
8558
5
      zend_error_noreturn(E_COMPILE_ERROR, "Anonymous class method %s() must not be abstract",
8559
5
        ZSTR_VAL(name));
8560
24
    } else if (ce->ce_flags & (ZEND_ACC_ENUM|ZEND_ACC_INTERFACE)) {
8561
14
      zend_error_noreturn(E_COMPILE_ERROR, "%s method %s::%s() must not be abstract",
8562
14
        zend_get_object_type_case(ce, true), ZSTR_VAL(ce->name), ZSTR_VAL(name));
8563
14
    } else {
8564
10
      zend_error_noreturn(E_COMPILE_ERROR, "Class %s declares abstract method %s() and must therefore be declared abstract",
8565
10
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
8566
10
    }
8567
29
  }
8568
8569
87.1k
  if (in_interface) {
8570
678
    if (!(fn_flags & ZEND_ACC_PUBLIC)) {
8571
1
      zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
8572
1
        "%s::%s() must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8573
1
    }
8574
677
    if (fn_flags & ZEND_ACC_FINAL) {
8575
5
      zend_error_noreturn(E_COMPILE_ERROR, "Interface method "
8576
5
        "%s::%s() must not be final", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8577
5
    }
8578
672
    op_array->fn_flags |= ZEND_ACC_ABSTRACT;
8579
672
  }
8580
8581
87.1k
  if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
8582
1.07k
    if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8583
5
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
8584
5
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8585
5
    }
8586
8587
1.07k
    if (has_body) {
8588
4
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
8589
4
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8590
4
    }
8591
8592
1.06k
    ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
8593
86.0k
  } else if (!has_body) {
8594
10
    zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
8595
10
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8596
10
  }
8597
8598
87.1k
  op_array->scope = ce;
8599
87.1k
  op_array->function_name = zend_string_copy(name);
8600
8601
87.1k
  lcname = zend_string_tolower(name);
8602
87.1k
  lcname = zend_new_interned_string(lcname);
8603
8604
87.1k
  if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
8605
26
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
8606
26
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8607
26
  }
8608
8609
87.1k
  zend_add_magic_method(ce, (zend_function *) op_array, lcname);
8610
87.1k
  if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)
8611
812
      && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8612
793
    add_stringable_interface(ce);
8613
793
  }
8614
8615
87.1k
  return lcname;
8616
87.1k
}
8617
/* }}} */
8618
8619
493k
static uint32_t zend_add_dynamic_func_def(zend_op_array *def) {
8620
493k
  zend_op_array *op_array = CG(active_op_array);
8621
493k
  uint32_t def_offset = op_array->num_dynamic_func_defs++;
8622
493k
  op_array->dynamic_func_defs = erealloc(
8623
493k
    op_array->dynamic_func_defs, op_array->num_dynamic_func_defs * sizeof(zend_op_array *));
8624
493k
  op_array->dynamic_func_defs[def_offset] = def;
8625
493k
  return def_offset;
8626
493k
}
8627
8628
enum func_decl_level {
8629
  FUNC_DECL_LEVEL_TOPLEVEL,
8630
  FUNC_DECL_LEVEL_NESTED,
8631
  FUNC_DECL_LEVEL_CONSTEXPR,
8632
};
8633
8634
static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array, const zend_ast_decl *decl, enum func_decl_level level) /* {{{ */
8635
507k
{
8636
507k
  zend_string *unqualified_name, *name, *lcname;
8637
507k
  zend_op *opline;
8638
8639
507k
  if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8640
490k
    zend_string *filename = op_array->filename;
8641
490k
    uint32_t start_lineno = decl->start_lineno;
8642
8643
490k
    zend_string *class = zend_empty_string;
8644
490k
    zend_string *separator = zend_empty_string;
8645
490k
    zend_string *function = filename;
8646
490k
    const char *parens = "";
8647
8648
490k
    if (CG(active_op_array) && CG(active_op_array)->function_name) {
8649
479k
      if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
8650
        /* If the parent function is a closure, don't redundantly
8651
         * add the classname and parentheses.
8652
         */
8653
477k
        function = CG(active_op_array)->function_name;
8654
477k
      } else {
8655
1.98k
        function = CG(active_op_array)->function_name;
8656
1.98k
        parens = "()";
8657
8658
1.98k
        if (CG(active_class_entry) && CG(active_class_entry)->name) {
8659
1.53k
          class = CG(active_class_entry)->name;
8660
1.53k
          separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM);
8661
1.53k
        }
8662
1.98k
      }
8663
479k
    }
8664
8665
490k
    unqualified_name = zend_strpprintf_unchecked(
8666
490k
      0,
8667
490k
      "{closure:%S%S%S%s:%" PRIu32 "}",
8668
490k
      class,
8669
490k
      separator,
8670
490k
      function,
8671
490k
      parens,
8672
490k
      start_lineno
8673
490k
    );
8674
8675
490k
    op_array->function_name = name = unqualified_name;
8676
490k
  } else {
8677
17.1k
    unqualified_name = decl->name;
8678
17.1k
    op_array->function_name = name = zend_prefix_with_ns(unqualified_name);
8679
17.1k
  }
8680
8681
507k
  lcname = zend_string_tolower(name);
8682
8683
507k
  if (FC(imports_function)) {
8684
57
    const zend_string *import_name =
8685
57
      zend_hash_find_ptr_lc(FC(imports_function), unqualified_name);
8686
57
    if (import_name && !zend_string_equals_ci(lcname, import_name)) {
8687
9
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare function %s() (previously declared as local import)",
8688
9
        ZSTR_VAL(name));
8689
9
    }
8690
57
  }
8691
8692
507k
  if (zend_string_equals_literal(lcname, "__autoload")) {
8693
1
    zend_error_noreturn(E_COMPILE_ERROR,
8694
1
      "__autoload() is no longer supported, use spl_autoload_register() instead");
8695
1
  }
8696
8697
507k
  if (zend_string_equals_literal_ci(unqualified_name, "assert")) {
8698
5
    zend_error(E_COMPILE_ERROR,
8699
5
      "Defining a custom assert() function is not allowed, "
8700
5
      "as the function has special semantics");
8701
5
  }
8702
8703
507k
  zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
8704
507k
  switch (level) {
8705
493k
    case FUNC_DECL_LEVEL_NESTED: {
8706
493k
      uint32_t func_ref = zend_add_dynamic_func_def(op_array);
8707
493k
      if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8708
489k
        opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
8709
489k
        opline->op2.num = func_ref;
8710
489k
      } else {
8711
3.46k
        opline = get_next_op();
8712
3.46k
        opline->opcode = ZEND_DECLARE_FUNCTION;
8713
3.46k
        opline->op1_type = IS_CONST;
8714
3.46k
        LITERAL_STR(opline->op1, zend_string_copy(lcname));
8715
3.46k
        opline->op2.num = func_ref;
8716
3.46k
      }
8717
493k
      break;
8718
0
    }
8719
131
    case FUNC_DECL_LEVEL_CONSTEXPR:
8720
13.7k
    case FUNC_DECL_LEVEL_TOPLEVEL:
8721
      /* Nothing to do. */
8722
13.7k
      break;
8723
507k
  }
8724
507k
  return lcname;
8725
507k
}
8726
/* }}} */
8727
8728
static zend_op_array *zend_compile_func_decl_ex(
8729
  znode *result, zend_ast *ast, enum func_decl_level level,
8730
  zend_string *property_info_name,
8731
  zend_property_hook_kind hook_kind
8732
598k
) {
8733
598k
  zend_ast_decl *decl = (zend_ast_decl *) ast;
8734
598k
  zend_ast *params_ast = decl->child[0];
8735
598k
  zend_ast *uses_ast = decl->child[1];
8736
598k
  zend_ast *stmt_ast = decl->child[2];
8737
598k
  zend_ast *return_type_ast = decl->child[3];
8738
598k
  bool is_method = decl->kind == ZEND_AST_METHOD;
8739
598k
  zend_string *lcname = NULL;
8740
598k
  bool is_hook = decl->kind == ZEND_AST_PROPERTY_HOOK;
8741
8742
598k
  zend_class_entry *orig_class_entry = CG(active_class_entry);
8743
598k
  zend_op_array *orig_op_array = CG(active_op_array);
8744
598k
  zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
8745
598k
  zend_oparray_context orig_oparray_context;
8746
598k
  closure_info info;
8747
8748
598k
  init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
8749
8750
598k
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
8751
0
    op_array->fn_flags |= ZEND_ACC_PRELOADED;
8752
0
  }
8753
8754
598k
  op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
8755
598k
  op_array->fn_flags |= decl->flags;
8756
598k
  op_array->line_start = decl->start_lineno;
8757
598k
  op_array->line_end = decl->end_lineno;
8758
598k
  if (decl->doc_comment) {
8759
139
    op_array->doc_comment = zend_string_copy(decl->doc_comment);
8760
139
  }
8761
8762
598k
  if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
8763
490k
    op_array->fn_flags |= ZEND_ACC_CLOSURE;
8764
490k
  }
8765
8766
598k
  if (is_hook) {
8767
3.83k
    zend_class_entry *ce = CG(active_class_entry);
8768
3.83k
    op_array->scope = ce;
8769
3.83k
    op_array->function_name = zend_string_copy(decl->name);
8770
594k
  } else if (is_method) {
8771
87.1k
    bool has_body = stmt_ast != NULL;
8772
87.1k
    lcname = zend_begin_method_decl(op_array, decl->name, has_body);
8773
507k
  } else {
8774
507k
    lcname = zend_begin_func_decl(result, op_array, decl, level);
8775
507k
    if (decl->kind == ZEND_AST_ARROW_FUNC) {
8776
15.4k
      find_implicit_binds(&info, params_ast, stmt_ast);
8777
15.4k
      compile_implicit_lexical_binds(&info, result, op_array);
8778
491k
    } else if (uses_ast) {
8779
1.42k
      zend_compile_closure_binding(result, op_array, uses_ast);
8780
1.42k
    }
8781
507k
  }
8782
8783
598k
  CG(active_op_array) = op_array;
8784
8785
598k
  zend_oparray_context_begin(&orig_oparray_context, op_array);
8786
598k
  CG(context).active_property_info_name = property_info_name;
8787
598k
  CG(context).active_property_hook_kind = hook_kind;
8788
8789
598k
  if (decl->child[4]) {
8790
301k
    int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
8791
8792
301k
    if (is_method || is_hook) {
8793
1.09k
      target = ZEND_ATTRIBUTE_TARGET_METHOD;
8794
1.09k
    }
8795
8796
301k
    zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target, 0);
8797
8798
301k
    const zend_attribute *override_attribute = zend_get_attribute_str(
8799
301k
      op_array->attributes,
8800
301k
      "override",
8801
301k
      sizeof("override")-1
8802
301k
    );
8803
8804
301k
    if (override_attribute) {
8805
712
      op_array->fn_flags |= ZEND_ACC_OVERRIDE;
8806
712
    }
8807
8808
301k
    const zend_attribute *deprecated_attribute = zend_get_attribute_str(
8809
301k
      op_array->attributes,
8810
301k
      "deprecated",
8811
301k
      sizeof("deprecated")-1
8812
301k
    );
8813
8814
301k
    if (deprecated_attribute) {
8815
245
      op_array->fn_flags |= ZEND_ACC_DEPRECATED;
8816
245
    }
8817
8818
    // ZEND_ACC_NODISCARD is added via an attribute validator
8819
301k
  }
8820
8821
  /* Do not leak the class scope into free standing functions, even if they are dynamically
8822
   * defined inside a class method. This is necessary for correct handling of magic constants.
8823
   * For example __CLASS__ should always be "" inside a free standing function. */
8824
598k
  if (decl->kind == ZEND_AST_FUNC_DECL) {
8825
17.0k
    CG(active_class_entry) = NULL;
8826
17.0k
  }
8827
8828
598k
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8829
13.6k
    op_array->fn_flags |= ZEND_ACC_TOP_LEVEL;
8830
13.6k
  }
8831
8832
598k
  {
8833
    /* Push a separator to the loop variable stack */
8834
598k
    zend_loop_var dummy_var;
8835
598k
    dummy_var.opcode = ZEND_RETURN;
8836
8837
598k
    zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
8838
598k
  }
8839
8840
598k
  zend_compile_params(params_ast, return_type_ast,
8841
598k
    is_method && zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME) ? IS_STRING : 0);
8842
598k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
8843
110k
    zend_mark_function_as_generator();
8844
110k
    zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
8845
110k
  }
8846
598k
  if (decl->kind == ZEND_AST_ARROW_FUNC) {
8847
15.4k
    zend_compile_implicit_closure_uses(&info);
8848
15.4k
    zend_hash_destroy(&info.uses);
8849
582k
  } else if (uses_ast) {
8850
1.41k
    zend_compile_closure_uses(uses_ast);
8851
1.41k
  }
8852
8853
598k
  if (ast->kind == ZEND_AST_ARROW_FUNC && decl->child[2]->kind != ZEND_AST_RETURN) {
8854
9.36k
    bool needs_return = true;
8855
9.36k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8856
709
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8857
709
      needs_return = !ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER);
8858
709
    }
8859
9.36k
    if (needs_return) {
8860
9.13k
      stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
8861
9.13k
      decl->child[2] = stmt_ast;
8862
9.13k
    }
8863
9.36k
  }
8864
8865
598k
  if (op_array->fn_flags & ZEND_ACC_NODISCARD) {
8866
    /* ZEND_ACC_NODISCARD gets added by the attribute validator, but only
8867
     * if the method is not a hook; if it is a hook, then the validator
8868
     * will have returned an error message, even if the error message was
8869
     * delayed with #[\DelayedTargetValidation] that ZEND_ACC_NODISCARD
8870
     * flag should not have been added. */
8871
933
    ZEND_ASSERT(!is_hook);
8872
8873
933
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8874
890
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8875
890
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_VOID)) {
8876
5
        zend_error_noreturn(E_COMPILE_ERROR,
8877
5
          "A void %s does not return a value, but #[\\NoDiscard] requires a return value",
8878
5
          CG(active_class_entry) != NULL ? "method" : "function");
8879
5
      }
8880
8881
885
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
8882
5
        zend_error_noreturn(E_COMPILE_ERROR,
8883
5
          "A never returning %s does not return a value, but #[\\NoDiscard] requires a return value",
8884
5
          CG(active_class_entry) != NULL ? "method" : "function");
8885
5
      }
8886
885
    }
8887
933
  }
8888
8889
598k
  zend_compile_stmt(stmt_ast);
8890
8891
598k
  if (is_method) {
8892
86.8k
    CG(zend_lineno) = decl->start_lineno;
8893
86.8k
    zend_check_magic_method_implementation(
8894
86.8k
      CG(active_class_entry), (zend_function *) op_array, lcname, E_COMPILE_ERROR);
8895
511k
  } else if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8896
    /* Only register the function after a successful compile */
8897
13.1k
    if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
8898
100
      CG(zend_lineno) = decl->start_lineno;
8899
100
      do_bind_function_error(lcname, op_array, true);
8900
100
    }
8901
13.1k
  }
8902
8903
  /* put the implicit return on the really last line */
8904
598k
  CG(zend_lineno) = decl->end_lineno;
8905
8906
598k
  zend_do_extended_stmt(NULL);
8907
598k
  zend_emit_final_return(false);
8908
8909
598k
  pass_two(CG(active_op_array));
8910
598k
  zend_oparray_context_end(&orig_oparray_context);
8911
8912
  /* Pop the loop variable stack separator */
8913
598k
  zend_stack_del_top(&CG(loop_var_stack));
8914
8915
598k
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8916
13.0k
    zend_observer_function_declared_notify(op_array, lcname);
8917
13.0k
  }
8918
8919
598k
  if (lcname != NULL) {
8920
592k
    zend_string_release_ex(lcname, 0);
8921
592k
  }
8922
8923
598k
  CG(active_op_array) = orig_op_array;
8924
598k
  CG(active_class_entry) = orig_class_entry;
8925
8926
598k
  return op_array;
8927
598k
}
8928
8929
static zend_op_array *zend_compile_func_decl(znode *result, zend_ast *ast, enum func_decl_level level)
8930
594k
{
8931
594k
  return zend_compile_func_decl_ex(result, ast, level, /* property_info */ NULL, (zend_property_hook_kind)-1);
8932
594k
}
8933
8934
4.56k
zend_property_hook_kind zend_get_property_hook_kind_from_name(const zend_string *name) {
8935
4.56k
  if (zend_string_equals_literal_ci(name, "get")) {
8936
3.02k
    return ZEND_PROPERTY_HOOK_GET;
8937
3.02k
  } else if (zend_string_equals_literal_ci(name, "set")) {
8938
1.43k
    return ZEND_PROPERTY_HOOK_SET;
8939
1.43k
  } else {
8940
103
    return (zend_property_hook_kind)-1;
8941
103
  }
8942
4.56k
}
8943
8944
static void zend_compile_property_hooks(
8945
    zend_property_info *prop_info, zend_string *prop_name,
8946
    zend_ast *prop_type_ast, const zend_ast_list *hooks)
8947
3.31k
{
8948
3.31k
  zend_class_entry *ce = CG(active_class_entry);
8949
8950
3.31k
  if (prop_info->flags & ZEND_ACC_READONLY) {
8951
15
    zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly");
8952
15
  }
8953
8954
3.30k
  if (hooks->children == 0) {
8955
14
    zend_error_noreturn(E_COMPILE_ERROR, "Property hook list must not be empty");
8956
14
  }
8957
8958
7.10k
  for (uint32_t i = 0; i < hooks->children; i++) {
8959
4.02k
    zend_ast_decl *hook = (zend_ast_decl *) hooks->child[i];
8960
4.02k
    zend_string *name = hook->name;
8961
4.02k
    zend_ast *stmt_ast = hook->child[2];
8962
4.02k
    zend_ast **return_type_ast_ptr = NULL;
8963
4.02k
    zend_ast **value_type_ast_ptr = NULL;
8964
4.02k
    CG(zend_lineno) = hook->start_lineno;
8965
8966
    /* Non-private hooks are always public. This avoids having to copy the hook when inheriting
8967
     * hooks from protected properties to public ones. */
8968
4.02k
    uint32_t hook_visibility = (prop_info->flags & ZEND_ACC_PPP_MASK) != ZEND_ACC_PRIVATE ? ZEND_ACC_PUBLIC : ZEND_ACC_PRIVATE;
8969
4.02k
    hook->flags |= hook_visibility;
8970
8971
4.02k
    if (prop_info->flags & ZEND_ACC_STATIC) {
8972
13
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare hooks for static property");
8973
13
    }
8974
4.01k
    if ((hook->flags & ZEND_ACC_FINAL) && (hook->flags & ZEND_ACC_PRIVATE)) {
8975
5
      zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both final and private");
8976
5
    }
8977
4.00k
    if ((ce->ce_flags & ZEND_ACC_INTERFACE)
8978
3.79k
     || ((prop_info->flags & ZEND_ACC_ABSTRACT) && !stmt_ast)) {
8979
569
      hook->flags |= ZEND_ACC_ABSTRACT;
8980
8981
569
      if (stmt_ast) {
8982
12
        zend_error_noreturn(E_COMPILE_ERROR, "Abstract property hook cannot have body");
8983
12
      }
8984
557
      if (hook->flags & ZEND_ACC_PRIVATE) {
8985
5
        zend_error_noreturn(E_COMPILE_ERROR,
8986
5
          "Property hook cannot be both abstract and private");
8987
5
      }
8988
552
      if (hook->flags & ZEND_ACC_FINAL) {
8989
6
        zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both abstract and final");
8990
6
      }
8991
3.43k
    } else if (!stmt_ast) {
8992
16
      zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract property hook must have a body");
8993
16
    }
8994
8995
3.96k
    zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(name);
8996
3.96k
    if (hook_kind == (zend_property_hook_kind)-1) {
8997
103
      zend_error_noreturn(E_COMPILE_ERROR,
8998
103
        "Unknown hook \"%s\" for property %s::$%s, expected \"get\" or \"set\"",
8999
103
        ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9000
103
    }
9001
9002
3.86k
    if (stmt_ast && stmt_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
9003
1.46k
      stmt_ast = stmt_ast->child[0];
9004
1.46k
      if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
9005
1.21k
        stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
9006
1.21k
      } else {
9007
248
        ZEND_ASSERT(hook_kind == ZEND_PROPERTY_HOOK_SET);
9008
248
        stmt_ast = zend_ast_create(ZEND_AST_ASSIGN,
9009
248
          zend_ast_create(ZEND_AST_PROP,
9010
248
            zend_ast_create(ZEND_AST_VAR, zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_THIS))),
9011
248
            zend_ast_create_zval_from_str(zend_copy_unmangled_prop_name(prop_info->name))),
9012
248
          stmt_ast);
9013
248
      }
9014
1.46k
      stmt_ast = zend_ast_create_list(1, ZEND_AST_STMT_LIST, stmt_ast);
9015
1.46k
      hook->child[2] = stmt_ast;
9016
1.46k
    }
9017
9018
3.86k
    if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
9019
2.52k
      if (hook->child[0]) {
9020
7
        zend_error_noreturn(E_COMPILE_ERROR, "get hook of property %s::$%s must not have a parameter list",
9021
7
          ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9022
7
      }
9023
9024
2.51k
      hook->child[0] = zend_ast_create_list(0, ZEND_AST_PARAM_LIST);
9025
9026
2.51k
      return_type_ast_ptr = &hook->child[3];
9027
2.51k
      *return_type_ast_ptr = prop_type_ast;
9028
2.51k
    } else if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
9029
1.34k
      if (hook->child[0]) {
9030
157
        const zend_ast_list *param_list = zend_ast_get_list(hook->child[0]);
9031
157
        if (param_list->children != 1) {
9032
0
          zend_error_noreturn(E_COMPILE_ERROR, "%s hook of property %s::$%s must accept exactly one parameters",
9033
0
            ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9034
0
        }
9035
157
        const zend_ast *value_param_ast = param_list->child[0];
9036
157
        if (value_param_ast->attr & ZEND_PARAM_REF) {
9037
5
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be pass-by-reference",
9038
5
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9039
5
        }
9040
152
        if (value_param_ast->attr & ZEND_PARAM_VARIADIC) {
9041
5
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be variadic",
9042
5
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9043
5
        }
9044
147
        if (value_param_ast->child[2]) {
9045
5
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not have a default value",
9046
5
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
9047
5
        }
9048
142
        if ((prop_type_ast != NULL) != (value_param_ast->child[0] != NULL)) {
9049
5
          zend_hooked_property_variance_error_ex(zend_ast_get_str(value_param_ast->child[1]), ce->name, prop_info->name);
9050
5
        }
9051
1.18k
      } else {
9052
1.18k
        zend_ast *param_name_ast = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VALUE));
9053
1.18k
        zend_ast *param = zend_ast_create(
9054
1.18k
          ZEND_AST_PARAM, prop_type_ast, param_name_ast,
9055
1.18k
          /* expr */ NULL, /* doc_comment */ NULL, /* attributes */ NULL,
9056
1.18k
          /* hooks */ NULL);
9057
1.18k
        value_type_ast_ptr = &param->child[0];
9058
1.18k
        hook->child[0] = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, param);
9059
1.18k
      }
9060
1.32k
      zend_ast *return_type = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VOID));
9061
1.32k
      return_type->attr = ZEND_NAME_NOT_FQ;
9062
1.32k
      hook->child[3] = return_type;
9063
1.32k
    } else {
9064
0
      ZEND_UNREACHABLE();
9065
0
    }
9066
9067
3.83k
    hook->name = zend_strpprintf(0, "$%s::%s", ZSTR_VAL(prop_name), ZSTR_VAL(name));
9068
9069
3.83k
    zend_function *func = (zend_function *) zend_compile_func_decl_ex(
9070
3.83k
      NULL, (zend_ast *) hook, FUNC_DECL_LEVEL_NESTED, prop_info->name, hook_kind);
9071
9072
3.83k
    func->common.prop_info = prop_info;
9073
9074
3.83k
    if (!prop_info->hooks) {
9075
3.06k
      prop_info->hooks = zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE);
9076
3.06k
      memset(prop_info->hooks, 0, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
9077
3.06k
    }
9078
9079
3.83k
    if (prop_info->hooks[hook_kind]) {
9080
19
      zend_error_noreturn(E_COMPILE_ERROR,
9081
19
        "Cannot redeclare property hook \"%s\"", ZSTR_VAL(name));
9082
19
    }
9083
3.81k
    prop_info->hooks[hook_kind] = func;
9084
9085
3.81k
    if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
9086
1.29k
      switch (zend_verify_property_hook_variance(prop_info, func)) {
9087
1.24k
        case INHERITANCE_SUCCESS:
9088
1.24k
          break;
9089
41
        case INHERITANCE_UNRESOLVED:
9090
41
          ce->num_hooked_prop_variance_checks++;
9091
41
          break;
9092
7
        case INHERITANCE_ERROR:
9093
7
          zend_hooked_property_variance_error(prop_info);
9094
0
        case INHERITANCE_WARNING:
9095
0
          ZEND_UNREACHABLE();
9096
1.29k
      }
9097
1.29k
    }
9098
9099
3.81k
    zend_string_release(name);
9100
    /* Un-share type ASTs to avoid double-frees of zval nodes. */
9101
3.81k
    if (return_type_ast_ptr) {
9102
2.46k
      *return_type_ast_ptr = NULL;
9103
2.46k
    }
9104
3.81k
    if (value_type_ast_ptr) {
9105
1.15k
      *value_type_ast_ptr = NULL;
9106
1.15k
    }
9107
3.81k
  }
9108
9109
3.07k
  ce->num_hooked_props++;
9110
9111
  /* See zend_link_hooked_object_iter(). */
9112
3.07k
#ifndef ZEND_OPCACHE_SHM_REATTACHMENT
9113
3.07k
  if (!ce->get_iterator) {
9114
    /* Will be removed again, in case of Iterator or IteratorAggregate. */
9115
2.32k
    ce->get_iterator = zend_hooked_object_get_iterator;
9116
2.32k
  }
9117
3.07k
#endif
9118
9119
3.07k
  if (!prop_info->ce->parent_name) {
9120
1.97k
    zend_verify_hooked_property(ce, prop_info, prop_name);
9121
1.97k
  }
9122
3.07k
}
9123
9124
static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */
9125
33.3k
{
9126
33.3k
  const zend_ast_list *list = zend_ast_get_list(ast);
9127
33.3k
  zend_class_entry *ce = CG(active_class_entry);
9128
33.3k
  uint32_t i, children = list->children;
9129
9130
33.3k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
9131
13
    zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", ZSTR_VAL(ce->name));
9132
13
  }
9133
9134
33.3k
  if ((flags & ZEND_ACC_FINAL) && (flags & ZEND_ACC_PRIVATE)) {
9135
5
    zend_error_noreturn(E_COMPILE_ERROR, "Property cannot be both final and private");
9136
5
  }
9137
9138
33.3k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9139
229
    if (flags & ZEND_ACC_FINAL) {
9140
5
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be final");
9141
5
    }
9142
224
    if (flags & (ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE)) {
9143
5
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be protected or private");
9144
5
    }
9145
219
    if (flags & ZEND_ACC_ABSTRACT) {
9146
5
      zend_error_noreturn(E_COMPILE_ERROR,
9147
5
        "Property in interface cannot be explicitly abstract. "
9148
5
        "All interface members are implicitly abstract");
9149
5
    }
9150
214
    flags |= ZEND_ACC_ABSTRACT;
9151
214
  }
9152
9153
66.8k
  for (i = 0; i < children; ++i) {
9154
33.6k
    zend_property_info *info;
9155
33.6k
    zend_ast *prop_ast = list->child[i];
9156
33.6k
    zend_ast *name_ast = prop_ast->child[0];
9157
33.6k
    zend_ast **value_ast_ptr = &prop_ast->child[1];
9158
33.6k
    zend_ast *doc_comment_ast = prop_ast->child[2];
9159
33.6k
    zend_ast *hooks_ast = prop_ast->child[3];
9160
33.6k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
9161
33.6k
    zend_string *doc_comment = NULL;
9162
33.6k
    zval value_zv;
9163
33.6k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
9164
33.6k
    flags |= zend_property_is_virtual(ce, name, hooks_ast) ? ZEND_ACC_VIRTUAL : 0;
9165
9166
33.6k
    zend_string *old_active_property_info_name = CG(context).active_property_info_name;
9167
33.6k
    CG(context).active_property_info_name = name;
9168
9169
33.6k
    if (!hooks_ast) {
9170
30.4k
      if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9171
0
        zend_error_noreturn(E_COMPILE_ERROR,
9172
0
          "Interfaces may only include hooked properties");
9173
0
      }
9174
30.4k
      if (flags & ZEND_ACC_ABSTRACT) {
9175
5
        zend_error_noreturn(E_COMPILE_ERROR,
9176
5
          "Only hooked properties may be declared abstract");
9177
5
      }
9178
30.4k
    }
9179
33.6k
    if ((flags & ZEND_ACC_ABSTRACT)) {
9180
518
      ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
9181
518
    }
9182
9183
33.6k
    if (type_ast) {
9184
15.4k
      type = zend_compile_typename(type_ast);
9185
9186
15.4k
      if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_NEVER|MAY_BE_CALLABLE)) {
9187
5
        zend_string *str = zend_type_to_string(type);
9188
5
        zend_error_noreturn(E_COMPILE_ERROR,
9189
5
          "Property %s::$%s cannot have type %s",
9190
5
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9191
5
      }
9192
15.4k
    }
9193
9194
    /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */
9195
33.6k
    if (doc_comment_ast) {
9196
380
      doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9197
380
    }
9198
9199
33.6k
    if (zend_hash_exists(&ce->properties_info, name)) {
9200
41
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
9201
41
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
9202
41
    }
9203
9204
33.6k
    if (*value_ast_ptr) {
9205
14.9k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9206
9207
14.9k
      if (ZEND_TYPE_IS_SET(type) && !Z_CONSTANT(value_zv)
9208
2.12k
          && !zend_is_valid_default_value(type, &value_zv)) {
9209
71
        zend_string *str = zend_type_to_string(type);
9210
71
        if (Z_TYPE(value_zv) == IS_NULL && !ZEND_TYPE_IS_INTERSECTION(type)) {
9211
20
          ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
9212
20
          zend_string *nullable_str = zend_type_to_string(type);
9213
9214
20
          zend_error_noreturn(E_COMPILE_ERROR,
9215
20
            "Default value for property of type %s may not be null. "
9216
20
            "Use the nullable type %s to allow null default value",
9217
20
            ZSTR_VAL(str), ZSTR_VAL(nullable_str));
9218
51
        } else {
9219
51
          zend_error_noreturn(E_COMPILE_ERROR,
9220
51
            "Cannot use %s as default value for property %s::$%s of type %s",
9221
51
            zend_zval_value_name(&value_zv),
9222
51
            ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9223
51
        }
9224
71
      }
9225
18.6k
    } else if (!ZEND_TYPE_IS_SET(type) && !hooks_ast) {
9226
3.93k
      ZVAL_NULL(&value_zv);
9227
14.7k
    } else {
9228
14.7k
      ZVAL_UNDEF(&value_zv);
9229
14.7k
    }
9230
9231
33.5k
    if ((ce->ce_flags & ZEND_ACC_READONLY_CLASS)) {
9232
35
      flags |= ZEND_ACC_READONLY;
9233
35
    }
9234
9235
33.5k
    if (flags & ZEND_ACC_READONLY) {
9236
502
      if (!ZEND_TYPE_IS_SET(type)) {
9237
13
        zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
9238
13
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9239
13
      }
9240
489
      if (!Z_ISUNDEF(value_zv)) {
9241
7
        zend_error_noreturn(E_COMPILE_ERROR,
9242
7
          "Readonly property %s::$%s cannot have default value",
9243
7
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9244
7
      }
9245
482
      if (flags & ZEND_ACC_STATIC) {
9246
9
        zend_error_noreturn(E_COMPILE_ERROR,
9247
9
          "Static property %s::$%s cannot be readonly",
9248
9
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9249
9
      }
9250
482
    }
9251
9252
33.5k
    info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type);
9253
9254
33.5k
    if (hooks_ast) {
9255
3.24k
      zend_compile_property_hooks(info, name, type_ast, zend_ast_get_list(hooks_ast));
9256
3.24k
    }
9257
9258
33.5k
    if (attr_ast) {
9259
688
      zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0);
9260
9261
688
      const zend_attribute *override_attribute = zend_get_attribute_str(info->attributes, "override", sizeof("override")-1);
9262
688
      if (override_attribute) {
9263
96
        info->flags |= ZEND_ACC_OVERRIDE;
9264
96
      }
9265
688
    }
9266
9267
33.5k
    CG(context).active_property_info_name = old_active_property_info_name;
9268
33.5k
  }
9269
33.3k
}
9270
/* }}} */
9271
9272
static void zend_compile_prop_group(const zend_ast *ast) /* {{{ */
9273
33.3k
{
9274
33.3k
  zend_ast *type_ast = ast->child[0];
9275
33.3k
  zend_ast *prop_ast = ast->child[1];
9276
33.3k
  zend_ast *attr_ast = ast->child[2];
9277
9278
33.3k
  zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast);
9279
33.3k
}
9280
/* }}} */
9281
9282
static void zend_check_trait_alias_modifiers(uint32_t attr) /* {{{ */
9283
1.77k
{
9284
1.77k
  if (attr & ZEND_ACC_STATIC) {
9285
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"static\" as method modifier in trait alias");
9286
1.76k
  } else if (attr & ZEND_ACC_ABSTRACT) {
9287
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"abstract\" as method modifier in trait alias");
9288
5
  }
9289
1.77k
}
9290
/* }}} */
9291
9292
static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast, zend_ast *type_ast)
9293
8.18k
{
9294
8.18k
  const zend_ast_list *list = zend_ast_get_list(ast);
9295
8.18k
  zend_class_entry *ce = CG(active_class_entry);
9296
8.18k
  uint32_t i, children = list->children;
9297
9298
16.3k
  for (i = 0; i < children; ++i) {
9299
8.22k
    zend_class_constant *c;
9300
8.22k
    zend_ast *const_ast = list->child[i];
9301
8.22k
    zend_ast *name_ast = const_ast->child[0];
9302
8.22k
    zend_ast **value_ast_ptr = &const_ast->child[1];
9303
8.22k
    zend_ast *doc_comment_ast = const_ast->child[2];
9304
8.22k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
9305
8.22k
    zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
9306
8.22k
    zval value_zv;
9307
8.22k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
9308
9309
8.22k
    if (type_ast) {
9310
2.49k
      type = zend_compile_typename(type_ast);
9311
9312
2.49k
      uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9313
9314
2.49k
      if (type_mask != MAY_BE_ANY && (type_mask & (MAY_BE_CALLABLE|MAY_BE_VOID|MAY_BE_NEVER))) {
9315
5
        zend_string *type_str = zend_type_to_string(type);
9316
9317
5
        zend_error_noreturn(E_COMPILE_ERROR, "Class constant %s::%s cannot have type %s",
9318
5
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9319
5
      }
9320
2.49k
    }
9321
9322
8.22k
    if (UNEXPECTED((flags & ZEND_ACC_PRIVATE) && (flags & ZEND_ACC_FINAL))) {
9323
5
      zend_error_noreturn(
9324
5
        E_COMPILE_ERROR, "Private constant %s::%s cannot be final as it is not visible to other classes",
9325
5
        ZSTR_VAL(ce->name), ZSTR_VAL(name)
9326
5
      );
9327
5
    }
9328
9329
8.21k
    zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9330
9331
8.21k
    if (!Z_CONSTANT(value_zv) && ZEND_TYPE_IS_SET(type) && !zend_is_valid_default_value(type, &value_zv)) {
9332
24
      zend_string *type_str = zend_type_to_string(type);
9333
9334
24
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as value for class constant %s::%s of type %s",
9335
24
        zend_zval_type_name(&value_zv), ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9336
24
    }
9337
9338
8.19k
    c = zend_declare_typed_class_constant(ce, name, &value_zv, flags, doc_comment, type);
9339
9340
8.19k
    if (attr_ast) {
9341
361
      zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9342
9343
361
      const zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9344
9345
361
      if (deprecated) {
9346
212
        ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9347
        /* For deprecated constants, we need to flag the zval for recursion
9348
         * detection. Make sure the zval is separated out of shm. */
9349
212
        ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
9350
212
        ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
9351
212
      }
9352
361
    }
9353
8.19k
  }
9354
8.18k
}
9355
9356
static void zend_compile_class_const_group(const zend_ast *ast) /* {{{ */
9357
8.18k
{
9358
8.18k
  zend_ast *const_ast = ast->child[0];
9359
8.18k
  zend_ast *attr_ast = ast->child[1];
9360
8.18k
  zend_ast *type_ast = ast->child[2];
9361
9362
8.18k
  zend_compile_class_const_decl(const_ast, ast->attr, attr_ast, type_ast);
9363
8.18k
}
9364
/* }}} */
9365
9366
static void zend_compile_method_ref(const zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */
9367
2.34k
{
9368
2.34k
  zend_ast *class_ast = ast->child[0];
9369
2.34k
  zend_ast *method_ast = ast->child[1];
9370
9371
2.34k
  method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
9372
9373
2.34k
  if (class_ast) {
9374
1.05k
    method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name");
9375
1.29k
  } else {
9376
1.29k
    method_ref->class_name = NULL;
9377
1.29k
  }
9378
2.34k
}
9379
/* }}} */
9380
9381
static void zend_compile_trait_precedence(const zend_ast *ast) /* {{{ */
9382
588
{
9383
588
  const zend_ast *method_ref_ast = ast->child[0];
9384
588
  zend_ast *insteadof_ast = ast->child[1];
9385
588
  const zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast);
9386
588
  uint32_t i;
9387
9388
588
  zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*));
9389
588
  zend_compile_method_ref(method_ref_ast, &precedence->trait_method);
9390
588
  precedence->num_excludes = insteadof_list->children;
9391
9392
1.51k
  for (i = 0; i < insteadof_list->children; ++i) {
9393
927
    zend_ast *name_ast = insteadof_list->child[i];
9394
927
    precedence->exclude_class_names[i] =
9395
927
      zend_resolve_const_class_name_reference(name_ast, "trait name");
9396
927
  }
9397
9398
588
  zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
9399
588
}
9400
/* }}} */
9401
9402
static void zend_compile_trait_alias(const zend_ast *ast) /* {{{ */
9403
1.77k
{
9404
1.77k
  const zend_ast *method_ref_ast = ast->child[0];
9405
1.77k
  zend_ast *alias_ast = ast->child[1];
9406
1.77k
  uint32_t modifiers = ast->attr;
9407
9408
1.77k
  zend_trait_alias *alias;
9409
9410
1.77k
  zend_check_trait_alias_modifiers(modifiers);
9411
9412
1.77k
  alias = emalloc(sizeof(zend_trait_alias));
9413
1.77k
  zend_compile_method_ref(method_ref_ast, &alias->trait_method);
9414
1.77k
  alias->modifiers = modifiers;
9415
9416
1.77k
  if (alias_ast) {
9417
1.30k
    alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
9418
1.30k
  } else {
9419
463
    alias->alias = NULL;
9420
463
  }
9421
9422
1.77k
  zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
9423
1.77k
}
9424
/* }}} */
9425
9426
static void zend_compile_use_trait(const zend_ast *ast) /* {{{ */
9427
2.28k
{
9428
2.28k
  const zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
9429
2.28k
  zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
9430
2.28k
  zend_class_entry *ce = CG(active_class_entry);
9431
2.28k
  uint32_t i;
9432
9433
2.28k
  ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children));
9434
9435
6.06k
  for (i = 0; i < traits->children; ++i) {
9436
3.78k
    zend_ast *trait_ast = traits->child[i];
9437
9438
3.78k
    if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9439
5
      zend_string *name = zend_ast_get_str(trait_ast);
9440
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
9441
5
        "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
9442
5
    }
9443
9444
3.77k
    ce->trait_names[ce->num_traits].name =
9445
3.77k
      zend_resolve_const_class_name_reference(trait_ast, "trait name");
9446
3.77k
    ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name);
9447
3.77k
    ce->num_traits++;
9448
3.77k
  }
9449
9450
2.28k
  if (!adaptations) {
9451
1.69k
    return;
9452
1.69k
  }
9453
9454
2.93k
  for (i = 0; i < adaptations->children; ++i) {
9455
2.36k
    const zend_ast *adaptation_ast = adaptations->child[i];
9456
2.36k
    switch (adaptation_ast->kind) {
9457
588
      case ZEND_AST_TRAIT_PRECEDENCE:
9458
588
        zend_compile_trait_precedence(adaptation_ast);
9459
588
        break;
9460
1.77k
      case ZEND_AST_TRAIT_ALIAS:
9461
1.77k
        zend_compile_trait_alias(adaptation_ast);
9462
1.77k
        break;
9463
2.36k
      EMPTY_SWITCH_DEFAULT_CASE()
9464
2.36k
    }
9465
2.36k
  }
9466
592
}
9467
/* }}} */
9468
9469
static void zend_compile_implements(zend_ast *ast) /* {{{ */
9470
2.91k
{
9471
2.91k
  const zend_ast_list *list = zend_ast_get_list(ast);
9472
2.91k
  zend_class_entry *ce = CG(active_class_entry);
9473
2.91k
  zend_class_name *interface_names;
9474
2.91k
  uint32_t i;
9475
9476
2.91k
  interface_names = emalloc(sizeof(zend_class_name) * list->children);
9477
9478
6.68k
  for (i = 0; i < list->children; ++i) {
9479
3.77k
    zend_ast *class_ast = list->child[i];
9480
3.77k
    interface_names[i].name =
9481
3.77k
      zend_resolve_const_class_name_reference(class_ast, "interface name");
9482
3.77k
    interface_names[i].lc_name = zend_string_tolower(interface_names[i].name);
9483
3.77k
  }
9484
9485
2.91k
  ce->num_interfaces = list->children;
9486
2.91k
  ce->interface_names = interface_names;
9487
2.91k
}
9488
/* }}} */
9489
9490
static zend_string *zend_generate_anon_class_name(const zend_ast_decl *decl)
9491
1.64k
{
9492
1.64k
  zend_string *filename = CG(active_op_array)->filename;
9493
1.64k
  uint32_t start_lineno = decl->start_lineno;
9494
9495
  /* Use parent or first interface as prefix. */
9496
1.64k
  zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS);
9497
1.64k
  if (decl->child[0]) {
9498
76
    prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name");
9499
1.56k
  } else if (decl->child[1]) {
9500
506
    const zend_ast_list *list = zend_ast_get_list(decl->child[1]);
9501
506
    prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name");
9502
506
  }
9503
9504
1.64k
  zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32,
9505
1.64k
    ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
9506
1.64k
  zend_string_release(prefix);
9507
1.64k
  return zend_new_interned_string(result);
9508
1.64k
}
9509
9510
static void zend_compile_enum_backing_type(zend_class_entry *ce, zend_ast *enum_backing_type_ast)
9511
764
{
9512
764
  ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM);
9513
764
  zend_type type = zend_compile_typename(enum_backing_type_ast);
9514
764
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9515
764
  if (ZEND_TYPE_IS_COMPLEX(type) || (type_mask != MAY_BE_LONG && type_mask != MAY_BE_STRING)) {
9516
50
    zend_string *type_string = zend_type_to_string(type);
9517
50
    zend_error_noreturn(E_COMPILE_ERROR,
9518
50
      "Enum backing type must be int or string, %s given",
9519
50
      ZSTR_VAL(type_string));
9520
50
  }
9521
714
  if (type_mask == MAY_BE_LONG) {
9522
386
    ce->enum_backing_type = IS_LONG;
9523
386
  } else {
9524
328
    ZEND_ASSERT(type_mask == MAY_BE_STRING);
9525
328
    ce->enum_backing_type = IS_STRING;
9526
326
  }
9527
714
  zend_type_release(type, 0);
9528
712
}
9529
9530
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel) /* {{{ */
9531
143k
{
9532
143k
  const zend_ast_decl *decl = (const zend_ast_decl *) ast;
9533
143k
  zend_ast *extends_ast = decl->child[0];
9534
143k
  zend_ast *implements_ast = decl->child[1];
9535
143k
  zend_ast *stmt_ast = decl->child[2];
9536
143k
  zend_ast *enum_backing_type_ast = decl->child[4];
9537
143k
  zend_string *name, *lcname;
9538
143k
  zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
9539
143k
  zend_op *opline;
9540
9541
143k
  zend_class_entry *original_ce = CG(active_class_entry);
9542
9543
143k
  if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
9544
141k
    zend_string *unqualified_name = decl->name;
9545
9546
141k
    if (CG(active_class_entry)) {
9547
9
      zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
9548
9
    }
9549
9550
141k
    const char *type = "a class name";
9551
141k
    if (decl->flags & ZEND_ACC_ENUM) {
9552
5.22k
      type = "an enum name";
9553
136k
    } else if (decl->flags & ZEND_ACC_INTERFACE) {
9554
5.73k
      type = "an interface name";
9555
130k
    } else if (decl->flags & ZEND_ACC_TRAIT) {
9556
2.28k
      type = "a trait name";
9557
2.28k
    }
9558
141k
    zend_assert_valid_class_name(unqualified_name, type);
9559
141k
    name = zend_prefix_with_ns(unqualified_name);
9560
141k
    name = zend_new_interned_string(name);
9561
141k
    lcname = zend_string_tolower(name);
9562
9563
141k
    if (FC(imports)) {
9564
593
      zend_string *import_name =
9565
593
        zend_hash_find_ptr_lc(FC(imports), unqualified_name);
9566
593
      if (import_name && !zend_string_equals_ci(lcname, import_name)) {
9567
12
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s "
9568
12
            "(previously declared as local import)", ZSTR_VAL(name));
9569
12
      }
9570
593
    }
9571
9572
141k
    zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS);
9573
141k
  } else {
9574
    /* Find an anon class name that is not in use yet. */
9575
1.64k
    name = NULL;
9576
1.64k
    lcname = NULL;
9577
1.64k
    do {
9578
1.64k
      zend_tmp_string_release(name);
9579
1.64k
      zend_tmp_string_release(lcname);
9580
1.64k
      name = zend_generate_anon_class_name(decl);
9581
1.64k
      lcname = zend_string_tolower(name);
9582
1.64k
    } while (zend_hash_exists(CG(class_table), lcname));
9583
1.64k
  }
9584
143k
  lcname = zend_new_interned_string(lcname);
9585
9586
143k
  ce->type = ZEND_USER_CLASS;
9587
143k
  ce->name = name;
9588
143k
  zend_initialize_class_data(ce, true);
9589
143k
  if (!(decl->flags & ZEND_ACC_ANON_CLASS)) {
9590
141k
    zend_alloc_ce_cache(ce->name);
9591
141k
  }
9592
9593
143k
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
9594
0
    ce->ce_flags |= ZEND_ACC_PRELOADED;
9595
0
    ZEND_MAP_PTR_NEW(ce->static_members_table);
9596
0
    ZEND_MAP_PTR_NEW(ce->mutable_data);
9597
0
  }
9598
9599
143k
  ce->ce_flags |= decl->flags;
9600
143k
  ce->info.user.filename = zend_string_copy(zend_get_compiled_filename());
9601
143k
  ce->info.user.line_start = decl->start_lineno;
9602
143k
  ce->info.user.line_end = decl->end_lineno;
9603
9604
143k
  if (decl->doc_comment) {
9605
33
    ce->doc_comment = zend_string_copy(decl->doc_comment);
9606
33
  }
9607
9608
143k
  if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
9609
    /* Serialization is not supported for anonymous classes */
9610
1.64k
    ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
9611
1.64k
  }
9612
9613
143k
  if (extends_ast) {
9614
55.6k
    ce->parent_name =
9615
55.6k
      zend_resolve_const_class_name_reference(extends_ast, "class name");
9616
55.6k
  }
9617
9618
143k
  CG(active_class_entry) = ce;
9619
9620
143k
  if (decl->child[3]) {
9621
1.72k
    zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS, 0);
9622
1.72k
  }
9623
9624
143k
  if (implements_ast) {
9625
2.91k
    zend_compile_implements(implements_ast);
9626
2.91k
  }
9627
9628
143k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
9629
5.19k
    if (enum_backing_type_ast != NULL) {
9630
764
      zend_compile_enum_backing_type(ce, enum_backing_type_ast);
9631
764
    }
9632
5.19k
    zend_enum_add_interfaces(ce);
9633
5.19k
    zend_enum_register_props(ce);
9634
5.19k
  }
9635
9636
143k
  zend_compile_stmt(stmt_ast);
9637
9638
  /* Reset lineno for final opcodes and errors */
9639
143k
  CG(zend_lineno) = ast->lineno;
9640
9641
143k
  if ((ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) == ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) {
9642
34
    zend_verify_abstract_class(ce);
9643
34
  }
9644
9645
143k
  CG(active_class_entry) = original_ce;
9646
9647
143k
  if (toplevel) {
9648
34.5k
    ce->ce_flags |= ZEND_ACC_TOP_LEVEL;
9649
34.5k
  }
9650
9651
  /* We currently don't early-bind classes that implement interfaces or use traits */
9652
143k
  if (!ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9653
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
9654
   /* See zend_link_hooked_object_iter(). */
9655
   && !ce->num_hooked_props
9656
#endif
9657
131k
   && !(CG(compiler_options) & ZEND_COMPILE_WITHOUT_EXECUTION)) {
9658
131k
    if (toplevel) {
9659
28.6k
      if (extends_ast) {
9660
7.60k
        zend_class_entry *parent_ce = zend_lookup_class_ex(
9661
7.60k
          ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
9662
9663
7.60k
        if (parent_ce
9664
6.96k
         && !zend_compile_ignore_class(parent_ce, ce->info.user.filename)) {
9665
6.96k
          if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) {
9666
4.12k
            zend_string_release(lcname);
9667
4.12k
            return;
9668
4.12k
          }
9669
6.96k
        }
9670
21.0k
      } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) {
9671
18.0k
        zend_string_release(lcname);
9672
18.0k
        zend_build_properties_info_table(ce);
9673
18.0k
        zend_inheritance_check_override(ce);
9674
18.0k
        ce->ce_flags |= ZEND_ACC_LINKED;
9675
18.0k
        zend_observer_class_linked_notify(ce, lcname);
9676
18.0k
        return;
9677
18.0k
      } else {
9678
3.03k
        goto link_unbound;
9679
3.03k
      }
9680
102k
    } else if (!extends_ast) {
9681
58.4k
link_unbound:
9682
      /* Link unbound simple class */
9683
58.4k
      zend_build_properties_info_table(ce);
9684
58.4k
      zend_inheritance_check_override(ce);
9685
58.4k
      ce->ce_flags |= ZEND_ACC_LINKED;
9686
58.4k
    }
9687
131k
  }
9688
9689
121k
  opline = get_next_op();
9690
9691
121k
  if (ce->parent_name) {
9692
    /* Lowercased parent name */
9693
50.6k
    zend_string *lc_parent_name = zend_string_tolower(ce->parent_name);
9694
50.6k
    opline->op2_type = IS_CONST;
9695
50.6k
    LITERAL_STR(opline->op2, lc_parent_name);
9696
50.6k
  }
9697
9698
121k
  opline->op1_type = IS_CONST;
9699
  /* It's possible that `lcname` is not an interned string because it was not yet in the interned string table.
9700
   * However, by this point another thread may have caused `lcname` to be added in the interned string table.
9701
   * This will cause `lcname` to get freed once it is found in the interned string table. If we were to use
9702
   * LITERAL_STR() here we would not change the `lcname` pointer to the new value, and it would point to the
9703
   * now-freed string. This will cause issues when we use `lcname` in the code below. We solve this by using
9704
   * zend_add_literal_string() which gives us the new value. */
9705
121k
  opline->op1.constant = zend_add_literal_string(&lcname);
9706
9707
121k
  if (decl->flags & ZEND_ACC_ANON_CLASS) {
9708
1.62k
    opline->opcode = ZEND_DECLARE_ANON_CLASS;
9709
1.62k
    opline->extended_value = zend_alloc_cache_slot();
9710
1.62k
    zend_make_var_result(result, opline);
9711
1.62k
    if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) {
9712
      /* We checked above that the class name is not used. This really shouldn't happen. */
9713
0
      zend_error_noreturn(E_ERROR,
9714
0
        "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name));
9715
0
    }
9716
119k
  } else {
9717
    /* Generate RTD keys until we find one that isn't in use yet. */
9718
119k
    zend_string *key = NULL;
9719
119k
    do {
9720
119k
      zend_tmp_string_release(key);
9721
119k
      key = zend_build_runtime_definition_key(lcname, decl->start_lineno);
9722
119k
    } while (!zend_hash_add_ptr(CG(class_table), key, ce));
9723
9724
    /* RTD key is placed after lcname literal in op1 */
9725
119k
    zend_add_literal_string(&key);
9726
9727
119k
    opline->opcode = ZEND_DECLARE_CLASS;
9728
119k
    if (toplevel
9729
11.6k
       && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING)
9730
        /* We currently don't early-bind classes that implement interfaces or use traits */
9731
5.09k
       && !ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9732
119k
    ) {
9733
1.28k
      if (!extends_ast) {
9734
        /* Use empty string for classes without parents to avoid new handler, and special
9735
         * handling of zend_early_binding. */
9736
842
        opline->op2_type = IS_CONST;
9737
842
        LITERAL_STR(opline->op2, ZSTR_EMPTY_ALLOC());
9738
842
      }
9739
1.28k
      CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING;
9740
1.28k
      opline->opcode = ZEND_DECLARE_CLASS_DELAYED;
9741
1.28k
      opline->extended_value = zend_alloc_cache_slot();
9742
1.28k
      opline->result_type = IS_UNUSED;
9743
1.28k
      opline->result.opline_num = -1;
9744
1.28k
    }
9745
119k
  }
9746
121k
}
9747
/* }}} */
9748
9749
static void zend_compile_enum_case(zend_ast *ast)
9750
8.25k
{
9751
8.25k
  zend_class_entry *enum_class = CG(active_class_entry);
9752
8.25k
  if (!(enum_class->ce_flags & ZEND_ACC_ENUM)) {
9753
5
    zend_error_noreturn(E_COMPILE_ERROR, "Case can only be used in enums");
9754
5
  }
9755
9756
8.25k
  zend_string *enum_case_name = zval_make_interned_string(zend_ast_get_zval(ast->child[0]));
9757
8.25k
  zend_string *enum_class_name = enum_class->name;
9758
9759
8.25k
  zval class_name_zval;
9760
8.25k
  ZVAL_STR_COPY(&class_name_zval, enum_class_name);
9761
8.25k
  zend_ast *class_name_ast = zend_ast_create_zval(&class_name_zval);
9762
9763
8.25k
  zval case_id_zval;
9764
8.25k
  int case_id = zend_enum_next_case_id(enum_class);
9765
8.25k
  ZVAL_LONG(&case_id_zval, case_id);
9766
8.25k
  zend_ast *case_id_ast = zend_ast_create_zval(&case_id_zval);
9767
9768
8.25k
  zval case_name_zval;
9769
8.25k
  ZVAL_STR_COPY(&case_name_zval, enum_case_name);
9770
8.25k
  zend_ast *case_name_ast = zend_ast_create_zval(&case_name_zval);
9771
9772
8.25k
  zend_ast *case_value_ast = ast->child[1];
9773
  // Remove case_value_ast from the original AST to avoid freeing it, as it will be freed by zend_const_expr_to_zval
9774
8.25k
  ast->child[1] = NULL;
9775
8.25k
  if (enum_class->enum_backing_type != IS_UNDEF && case_value_ast == NULL) {
9776
5
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of backed enum %s must have a value",
9777
5
      ZSTR_VAL(enum_case_name),
9778
5
      ZSTR_VAL(enum_class_name));
9779
8.24k
  } else if (enum_class->enum_backing_type == IS_UNDEF && case_value_ast != NULL) {
9780
9
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-backed enum %s must not have a value",
9781
9
      ZSTR_VAL(enum_case_name),
9782
9
      ZSTR_VAL(enum_class_name));
9783
9
  }
9784
9785
8.23k
  zend_ast *const_enum_init_ast = zend_ast_create(ZEND_AST_CONST_ENUM_INIT,
9786
8.23k
      class_name_ast, case_id_ast, case_name_ast, case_value_ast);
9787
9788
8.23k
  zval value_zv;
9789
8.23k
  zend_const_expr_to_zval(&value_zv, &const_enum_init_ast, /* allow_dynamic */ false);
9790
9791
  /* Doc comment has been appended as second last element in ZEND_AST_ENUM ast - attributes are conventionally last */
9792
8.23k
  zend_ast *doc_comment_ast = ast->child[2];
9793
8.23k
  zend_string *doc_comment = NULL;
9794
8.23k
  if (doc_comment_ast) {
9795
5.54k
    doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9796
5.54k
  }
9797
9798
8.23k
  zend_class_constant *c = zend_declare_class_constant_ex(enum_class, enum_case_name, &value_zv, ZEND_ACC_PUBLIC, doc_comment);
9799
8.23k
  ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE;
9800
8.23k
  zend_ast_destroy(const_enum_init_ast);
9801
9802
8.23k
  zend_ast *attr_ast = ast->child[3];
9803
8.23k
  if (attr_ast) {
9804
106
    zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9805
9806
106
    zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9807
9808
106
    if (deprecated) {
9809
39
      ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9810
39
    }
9811
106
  }
9812
8.23k
}
9813
9814
static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
9815
2.09k
{
9816
2.09k
  switch (type) {
9817
1.19k
    case ZEND_SYMBOL_CLASS:
9818
1.19k
      if (!FC(imports)) {
9819
823
        FC(imports) = emalloc(sizeof(HashTable));
9820
823
        zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
9821
823
      }
9822
1.19k
      return FC(imports);
9823
524
    case ZEND_SYMBOL_FUNCTION:
9824
524
      if (!FC(imports_function)) {
9825
438
        FC(imports_function) = emalloc(sizeof(HashTable));
9826
438
        zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
9827
438
      }
9828
524
      return FC(imports_function);
9829
374
    case ZEND_SYMBOL_CONST:
9830
374
      if (!FC(imports_const)) {
9831
307
        FC(imports_const) = emalloc(sizeof(HashTable));
9832
307
        zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
9833
307
      }
9834
374
      return FC(imports_const);
9835
2.09k
    EMPTY_SWITCH_DEFAULT_CASE()
9836
2.09k
  }
9837
9838
0
  return NULL;
9839
2.09k
}
9840
/* }}} */
9841
9842
static char *zend_get_use_type_str(uint32_t type) /* {{{ */
9843
69
{
9844
69
  switch (type) {
9845
39
    case ZEND_SYMBOL_CLASS:
9846
39
      return "";
9847
15
    case ZEND_SYMBOL_FUNCTION:
9848
15
      return " function";
9849
15
    case ZEND_SYMBOL_CONST:
9850
15
      return " const";
9851
69
    EMPTY_SWITCH_DEFAULT_CASE()
9852
69
  }
9853
9854
0
  return " unknown";
9855
69
}
9856
/* }}} */
9857
9858
static void zend_check_already_in_use(uint32_t type, const zend_string *old_name, const zend_string *new_name, const zend_string *check_name) /* {{{ */
9859
41
{
9860
41
  if (zend_string_equals_ci(old_name, check_name)) {
9861
14
    return;
9862
14
  }
9863
9864
27
  zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9865
27
    "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9866
41
}
9867
/* }}} */
9868
9869
static void zend_compile_use(zend_ast *ast) /* {{{ */
9870
2.09k
{
9871
2.09k
  const zend_ast_list *list = zend_ast_get_list(ast);
9872
2.09k
  uint32_t i;
9873
2.09k
  zend_string *current_ns = FC(current_namespace);
9874
2.09k
  uint32_t type = ast->attr;
9875
2.09k
  HashTable *current_import = zend_get_import_ht(type);
9876
2.09k
  bool case_sensitive = type == ZEND_SYMBOL_CONST;
9877
9878
4.28k
  for (i = 0; i < list->children; ++i) {
9879
2.25k
    const zend_ast *use_ast = list->child[i];
9880
2.25k
    zend_ast *old_name_ast = use_ast->child[0];
9881
2.25k
    zend_ast *new_name_ast = use_ast->child[1];
9882
2.25k
    zend_string *old_name = zend_ast_get_str(old_name_ast);
9883
2.25k
    zend_string *new_name, *lookup_name;
9884
9885
2.25k
    if (new_name_ast) {
9886
523
      new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
9887
1.73k
    } else {
9888
1.73k
      const char *unqualified_name;
9889
1.73k
      size_t unqualified_name_len;
9890
1.73k
      if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
9891
        /* The form "use A\B" is equivalent to "use A\B as B" */
9892
902
        new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
9893
902
      } else {
9894
833
        new_name = zend_string_copy(old_name);
9895
9896
833
        if (!current_ns) {
9897
476
          zend_error(E_WARNING, "The use statement with non-compound name '%s' "
9898
476
            "has no effect", ZSTR_VAL(new_name));
9899
476
        }
9900
833
      }
9901
1.73k
    }
9902
9903
2.25k
    if (case_sensitive) {
9904
422
      lookup_name = zend_string_copy(new_name);
9905
1.83k
    } else {
9906
1.83k
      lookup_name = zend_string_tolower(new_name);
9907
1.83k
    }
9908
9909
2.25k
    if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) {
9910
23
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
9911
23
        "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
9912
23
    }
9913
9914
2.23k
    if (current_ns) {
9915
1.25k
      zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
9916
1.25k
      zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
9917
1.25k
      ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
9918
1.25k
      memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1);
9919
9920
1.25k
      if (zend_have_seen_symbol(ns_name, type)) {
9921
16
        zend_check_already_in_use(type, old_name, new_name, ns_name);
9922
16
      }
9923
9924
1.25k
      zend_string_efree(ns_name);
9925
1.25k
    } else if (zend_have_seen_symbol(lookup_name, type)) {
9926
25
      zend_check_already_in_use(type, old_name, new_name, lookup_name);
9927
25
    }
9928
9929
2.23k
    zend_string_addref(old_name);
9930
2.23k
    old_name = zend_new_interned_string(old_name);
9931
2.23k
    if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
9932
42
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9933
42
        "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9934
42
    }
9935
9936
2.19k
    zend_string_release_ex(lookup_name, 0);
9937
2.19k
    zend_string_release_ex(new_name, 0);
9938
2.19k
  }
9939
2.09k
}
9940
/* }}} */
9941
9942
static void zend_compile_group_use(const zend_ast *ast) /* {{{ */
9943
281
{
9944
281
  uint32_t i;
9945
281
  const zend_string *ns = zend_ast_get_str(ast->child[0]);
9946
281
  const zend_ast_list *list = zend_ast_get_list(ast->child[1]);
9947
9948
1.05k
  for (i = 0; i < list->children; i++) {
9949
771
    zend_ast *inline_use, *use = list->child[i];
9950
771
    zval *name_zval = zend_ast_get_zval(use->child[0]);
9951
771
    zend_string *name = Z_STR_P(name_zval);
9952
771
    zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
9953
771
    zend_string_release_ex(name, 0);
9954
771
    ZVAL_STR(name_zval, compound_ns);
9955
771
    inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
9956
771
    inline_use->attr = ast->attr ? ast->attr : use->attr;
9957
771
    zend_compile_use(inline_use);
9958
771
  }
9959
281
}
9960
/* }}} */
9961
9962
static void zend_compile_const_decl(zend_ast *ast) /* {{{ */
9963
3.49k
{
9964
3.49k
  zend_ast_list *list = zend_ast_get_list(ast);
9965
3.49k
  uint32_t i;
9966
3.49k
  zend_ast *attributes_ast = NULL;
9967
3.49k
  zend_op *last_op = NULL;
9968
7.62k
  for (i = 0; i < list->children; ++i) {
9969
4.14k
    zend_ast *const_ast = list->child[i];
9970
4.14k
    if (const_ast->kind == ZEND_AST_ATTRIBUTE_LIST) {
9971
364
      ZEND_ASSERT(i == list->children - 1);
9972
364
      attributes_ast = const_ast;
9973
364
      continue;
9974
364
    }
9975
3.78k
    ZEND_ASSERT(const_ast->kind == ZEND_AST_CONST_ELEM);
9976
3.78k
    zend_ast *name_ast = const_ast->child[0];
9977
3.78k
    zend_ast **value_ast_ptr = &const_ast->child[1];
9978
3.78k
    zend_string *unqualified_name = zend_ast_get_str(name_ast);
9979
9980
3.78k
    zend_string *name;
9981
3.78k
    znode name_node, value_node;
9982
3.78k
    zval *value_zv = &value_node.u.constant;
9983
9984
3.78k
    value_node.op_type = IS_CONST;
9985
3.78k
    zend_const_expr_to_zval(value_zv, value_ast_ptr, /* allow_dynamic */ true);
9986
9987
3.78k
    if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) {
9988
5
      zend_error_noreturn(E_COMPILE_ERROR,
9989
5
        "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name));
9990
5
    }
9991
9992
3.77k
    name = zend_prefix_with_ns(unqualified_name);
9993
3.77k
    name = zend_new_interned_string(name);
9994
9995
3.77k
    if (FC(imports_const)) {
9996
327
      zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name);
9997
327
      if (import_name && !zend_string_equals(import_name, name)) {
9998
9
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because "
9999
9
          "the name is already in use", ZSTR_VAL(name));
10000
9
      }
10001
327
    }
10002
10003
3.77k
    name_node.op_type = IS_CONST;
10004
3.77k
    ZVAL_STR(&name_node.u.constant, name);
10005
10006
3.77k
    last_op = zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
10007
10008
3.77k
    zend_register_seen_symbol(name, ZEND_SYMBOL_CONST);
10009
3.77k
  }
10010
3.47k
  if (attributes_ast == NULL) {
10011
3.02k
    return;
10012
3.02k
  }
10013
  /* Validate: attributes can only be applied to one constant at a time
10014
   * Since we store the AST for the attributes in the list of children,
10015
   * there should be exactly 2 children. */
10016
457
  if (list->children > 2) {
10017
6
    zend_error_noreturn(
10018
6
      E_COMPILE_ERROR,
10019
6
      "Cannot apply attributes to multiple constants at once"
10020
6
    );
10021
6
  }
10022
10023
451
  HashTable *attributes = NULL;
10024
451
  zend_compile_attributes(&attributes, list->child[1], 0, ZEND_ATTRIBUTE_TARGET_CONST, 0);
10025
10026
451
  ZEND_ASSERT(last_op != NULL);
10027
451
  last_op->opcode = ZEND_DECLARE_ATTRIBUTED_CONST;
10028
352
  znode attribs_node;
10029
352
  attribs_node.op_type = IS_CONST;
10030
352
  ZVAL_PTR(&attribs_node.u.constant, attributes);
10031
352
  zend_emit_op_data(&attribs_node);
10032
352
  CG(active_op_array)->fn_flags |= ZEND_ACC_PTR_OPS;
10033
352
}
10034
/* }}}*/
10035
10036
static void zend_compile_namespace(const zend_ast *ast) /* {{{ */
10037
4.07k
{
10038
4.07k
  zend_ast *name_ast = ast->child[0];
10039
4.07k
  zend_ast *stmt_ast = ast->child[1];
10040
4.07k
  zend_string *name;
10041
4.07k
  bool with_bracket = stmt_ast != NULL;
10042
10043
  /* handle mixed syntax declaration or nested namespaces */
10044
4.07k
  if (!FC(has_bracketed_namespaces)) {
10045
3.07k
    if (FC(current_namespace)) {
10046
      /* previous namespace declarations were unbracketed */
10047
777
      if (with_bracket) {
10048
8
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
10049
8
          "with unbracketed namespace declarations");
10050
8
      }
10051
777
    }
10052
3.07k
  } else {
10053
    /* previous namespace declarations were bracketed */
10054
1.00k
    if (!with_bracket) {
10055
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
10056
6
        "with unbracketed namespace declarations");
10057
1.00k
    } else if (FC(current_namespace) || FC(in_namespace)) {
10058
7
      zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
10059
7
    }
10060
1.00k
  }
10061
10062
4.07k
  bool is_first_namespace = (!with_bracket && !FC(current_namespace))
10063
2.23k
    || (with_bracket && !FC(has_bracketed_namespaces));
10064
4.05k
  if (is_first_namespace && FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
10065
26
    zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
10066
26
      "the very first statement or after any declare call in the script");
10067
26
  }
10068
10069
4.02k
  if (FC(current_namespace)) {
10070
769
    zend_string_release_ex(FC(current_namespace), 0);
10071
769
  }
10072
10073
4.02k
  if (name_ast) {
10074
3.46k
    name = zend_ast_get_str(name_ast);
10075
10076
3.46k
    if (zend_string_equals_literal_ci(name, "namespace")) {
10077
5
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name));
10078
5
    }
10079
10080
3.45k
    FC(current_namespace) = zend_string_copy(name);
10081
3.45k
  } else {
10082
568
    FC(current_namespace) = NULL;
10083
568
  }
10084
10085
4.02k
  zend_reset_import_tables();
10086
10087
4.02k
  FC(in_namespace) = 1;
10088
4.02k
  if (with_bracket) {
10089
1.45k
    FC(has_bracketed_namespaces) = 1;
10090
1.45k
  }
10091
10092
4.02k
  if (stmt_ast) {
10093
1.45k
    zend_compile_top_stmt(stmt_ast);
10094
1.45k
    zend_end_namespace();
10095
1.45k
  }
10096
4.02k
}
10097
/* }}} */
10098
10099
static void zend_compile_halt_compiler(const zend_ast *ast) /* {{{ */
10100
51
{
10101
51
  zend_ast *offset_ast = ast->child[0];
10102
51
  zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
10103
10104
51
  const char const_name[] = "__COMPILER_HALT_OFFSET__";
10105
10106
51
  if (FC(has_bracketed_namespaces) && FC(in_namespace)) {
10107
0
    zend_error_noreturn(E_COMPILE_ERROR,
10108
0
      "__HALT_COMPILER() can only be used from the outermost scope");
10109
0
  }
10110
10111
51
  const zend_string *filename = zend_get_compiled_filename();
10112
51
  zend_string *name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
10113
51
    ZSTR_VAL(filename), ZSTR_LEN(filename), false);
10114
10115
  /* Avoid repeated declaration of the __COMPILER_HALT_OFFSET__ constant in
10116
   * case this file was already included. */
10117
51
  if (!zend_hash_find(EG(zend_constants), name)) {
10118
51
    zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0);
10119
51
  }
10120
51
  zend_string_release_ex(name, 0);
10121
51
}
10122
/* }}} */
10123
10124
static bool zend_try_ct_eval_magic_const(zval *zv, const zend_ast *ast) /* {{{ */
10125
23.8k
{
10126
23.8k
  const zend_op_array *op_array = CG(active_op_array);
10127
23.8k
  const zend_class_entry *ce = CG(active_class_entry);
10128
10129
23.8k
  switch (ast->attr) {
10130
115
    case T_LINE:
10131
115
      ZVAL_LONG(zv, ast->lineno);
10132
115
      break;
10133
4.30k
    case T_FILE:
10134
4.30k
      ZVAL_STR_COPY(zv, CG(compiled_filename));
10135
4.30k
      break;
10136
1.23k
    case T_DIR:
10137
1.23k
    {
10138
1.23k
      const zend_string *filename = CG(compiled_filename);
10139
1.23k
      zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
10140
#ifdef ZEND_WIN32
10141
      ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
10142
#else
10143
1.23k
      ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
10144
1.23k
#endif
10145
10146
1.23k
      if (zend_string_equals_literal(dirname, ".")) {
10147
799
        dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
10148
799
#ifdef HAVE_GETCWD
10149
799
        ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN));
10150
#elif defined(HAVE_GETWD)
10151
        ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname)));
10152
#endif
10153
799
        ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname));
10154
799
      }
10155
10156
1.23k
      ZVAL_STR(zv, dirname);
10157
1.23k
      break;
10158
0
    }
10159
8.08k
    case T_FUNC_C:
10160
8.08k
      if (op_array && op_array->function_name) {
10161
7.90k
        ZVAL_STR_COPY(zv, op_array->function_name);
10162
7.90k
      } else {
10163
186
        ZVAL_EMPTY_STRING(zv);
10164
186
      }
10165
8.08k
      break;
10166
343
    case T_PROPERTY_C: {
10167
343
      zend_string *prop_info_name = CG(context).active_property_info_name;
10168
343
      if (prop_info_name) {
10169
281
        ZVAL_STR(zv, zend_copy_unmangled_prop_name(prop_info_name));
10170
281
      } else {
10171
62
        ZVAL_EMPTY_STRING(zv);
10172
62
      }
10173
343
      break;
10174
0
    }
10175
4.06k
    case T_METHOD_C:
10176
      /* Detect whether we are directly inside a class (e.g. a class constant) and treat
10177
       * this as not being inside a function. */
10178
4.06k
      if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
10179
199
        op_array = NULL;
10180
199
      }
10181
4.06k
      if (op_array && op_array->function_name) {
10182
3.79k
        if (op_array->scope) {
10183
3.42k
          ZVAL_NEW_STR(zv,
10184
3.42k
            zend_create_member_string(op_array->scope->name, op_array->function_name));
10185
3.42k
        } else {
10186
367
          ZVAL_STR_COPY(zv, op_array->function_name);
10187
367
        }
10188
3.79k
      } else {
10189
266
        ZVAL_EMPTY_STRING(zv);
10190
266
      }
10191
4.06k
      break;
10192
3.93k
    case T_CLASS_C:
10193
3.93k
      if (ce) {
10194
1.97k
        if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10195
1.47k
          return 0;
10196
1.47k
        } else {
10197
497
          ZVAL_STR_COPY(zv, ce->name);
10198
497
        }
10199
1.97k
      } else {
10200
1.96k
        ZVAL_EMPTY_STRING(zv);
10201
1.96k
      }
10202
2.45k
      break;
10203
2.45k
    case T_TRAIT_C:
10204
1.32k
      if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10205
270
        ZVAL_STR_COPY(zv, ce->name);
10206
1.05k
      } else {
10207
1.05k
        ZVAL_EMPTY_STRING(zv);
10208
1.05k
      }
10209
1.32k
      break;
10210
453
    case T_NS_C:
10211
453
      if (FC(current_namespace)) {
10212
236
        ZVAL_STR_COPY(zv, FC(current_namespace));
10213
236
      } else {
10214
217
        ZVAL_EMPTY_STRING(zv);
10215
217
      }
10216
453
      break;
10217
23.8k
    EMPTY_SWITCH_DEFAULT_CASE()
10218
23.8k
  }
10219
10220
22.3k
  return 1;
10221
23.8k
}
10222
/* }}} */
10223
10224
ZEND_API bool zend_is_op_long_compatible(const zval *op)
10225
55.0k
{
10226
55.0k
  if (Z_TYPE_P(op) == IS_ARRAY) {
10227
326
    return false;
10228
326
  }
10229
10230
54.7k
  if (Z_TYPE_P(op) == IS_DOUBLE
10231
14.5k
    && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval_silent(Z_DVAL_P(op)))) {
10232
10.2k
    return false;
10233
10.2k
  }
10234
10235
44.4k
  if (Z_TYPE_P(op) == IS_STRING) {
10236
9.19k
    double dval = 0;
10237
9.19k
    uint8_t is_num = is_numeric_str_function(Z_STR_P(op), NULL, &dval);
10238
9.19k
    if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval_silent(dval)))) {
10239
2.50k
      return false;
10240
2.50k
    }
10241
9.19k
  }
10242
10243
41.9k
  return true;
10244
44.4k
}
10245
10246
ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, const zval *op2) /* {{{ */
10247
403k
{
10248
403k
  if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) {
10249
    /* Array to string warning. */
10250
31.4k
    return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY;
10251
31.4k
  }
10252
10253
372k
  if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV
10254
273k
               || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR
10255
232k
               || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) {
10256
    /* Only the numeric operations throw errors. */
10257
200k
    return 0;
10258
200k
  }
10259
10260
171k
  if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) {
10261
7.79k
    if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) {
10262
      /* Adding two arrays is allowed. */
10263
3.86k
      return 0;
10264
3.86k
    }
10265
10266
    /* Numeric operators throw when one of the operands is an array. */
10267
3.92k
    return 1;
10268
7.79k
  }
10269
10270
  /* While basic arithmetic operators always produce numeric string errors,
10271
   * bitwise operators don't produce errors if both operands are strings */
10272
163k
  if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)
10273
31.2k
    && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
10274
15.9k
    return 0;
10275
15.9k
  }
10276
10277
147k
  if (Z_TYPE_P(op1) == IS_STRING
10278
46.9k
    && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) {
10279
22.0k
    return 1;
10280
22.0k
  }
10281
10282
125k
  if (Z_TYPE_P(op2) == IS_STRING
10283
17.9k
    && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) {
10284
11.0k
    return 1;
10285
11.0k
  }
10286
10287
  /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */
10288
114k
  if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR
10289
108k
      || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) {
10290
16.7k
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2)) {
10291
6.12k
      return 1;
10292
6.12k
    }
10293
16.7k
  }
10294
10295
108k
  if (opcode == ZEND_DIV && zval_get_double(op2) == 0.0) {
10296
    /* Division by zero throws an error. */
10297
1.01k
    return 1;
10298
1.01k
  }
10299
10300
  /* Mod is an operation that will cast float/float-strings to integers which might
10301
     produce float to int incompatible errors, and also cannot be divided by 0 */
10302
107k
  if (opcode == ZEND_MOD) {
10303
12.0k
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2) || zval_get_long(op2) == 0) {
10304
8.86k
      return 1;
10305
8.86k
    }
10306
12.0k
  }
10307
10308
98.3k
  if ((opcode == ZEND_POW) && zval_get_double(op1) == 0 && zval_get_double(op2) < 0) {
10309
    /* 0 ** (<0) throws a division by zero error. */
10310
42
    return 1;
10311
42
  }
10312
98.2k
  if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) {
10313
    /* Shift by negative number throws an error. */
10314
217
    return 1;
10315
217
  }
10316
10317
98.0k
  return 0;
10318
98.2k
}
10319
/* }}} */
10320
10321
static inline bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
10322
357k
{
10323
357k
  if (zend_binary_op_produces_error(opcode, op1, op2)) {
10324
32.9k
    return 0;
10325
32.9k
  }
10326
10327
325k
  const binary_op_type fn = get_binary_op(opcode);
10328
325k
  fn(result, op1, op2);
10329
325k
  return 1;
10330
357k
}
10331
/* }}} */
10332
10333
ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op)
10334
119k
{
10335
119k
  if (opcode == ZEND_BW_NOT) {
10336
    /* BW_NOT on string does not convert the string into an integer. */
10337
4.94k
    if (Z_TYPE_P(op) == IS_STRING) {
10338
998
      return 0;
10339
998
    }
10340
3.94k
    return Z_TYPE_P(op) <= IS_TRUE || !zend_is_op_long_compatible(op);
10341
4.94k
  }
10342
  /* Can happen when called from zend_optimizer_eval_unary_op() */
10343
114k
  if (opcode == ZEND_BOOL || opcode == ZEND_BOOL_NOT) {
10344
    /* ZEND_BOOL/ZEND_BOOL_NOT warns when casting NAN. */
10345
114k
    return Z_TYPE_P(op) == IS_DOUBLE;
10346
114k
  }
10347
10348
0
  return 0;
10349
114k
}
10350
10351
static inline bool zend_try_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */
10352
118k
{
10353
118k
  if (zend_unary_op_produces_error(opcode, op)) {
10354
1.01k
    return 0;
10355
1.01k
  }
10356
10357
117k
  const unary_op_type fn = get_unary_op(opcode);
10358
117k
  fn(result, op);
10359
117k
  return 1;
10360
118k
}
10361
/* }}} */
10362
10363
static inline bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
10364
51.1k
{
10365
51.1k
  zval right;
10366
51.1k
  ZVAL_LONG(&right, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10367
51.1k
  return zend_try_ct_eval_binary_op(result, ZEND_MUL, op, &right);
10368
51.1k
}
10369
/* }}} */
10370
10371
static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
10372
7.43k
{
10373
7.43k
  const binary_op_type fn = kind == ZEND_AST_GREATER
10374
7.43k
    ? is_smaller_function : is_smaller_or_equal_function;
10375
7.43k
  fn(result, op2, op1);
10376
7.43k
}
10377
/* }}} */
10378
10379
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
10380
1.99M
{
10381
1.99M
  const zend_ast_list *list = zend_ast_get_list(ast);
10382
1.99M
  zend_ast *last_elem_ast = NULL;
10383
1.99M
  uint32_t i;
10384
1.99M
  bool is_constant = true;
10385
10386
1.99M
  if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
10387
5
    zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
10388
5
  }
10389
10390
  /* First ensure that *all* child nodes are constant and by-val */
10391
4.33M
  for (i = 0; i < list->children; ++i) {
10392
2.33M
    zend_ast *elem_ast = list->child[i];
10393
10394
2.33M
    if (elem_ast == NULL) {
10395
      /* Report error at line of last non-empty element */
10396
95
      if (last_elem_ast) {
10397
54
        CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast);
10398
54
      }
10399
95
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
10400
95
    }
10401
10402
2.33M
    if (elem_ast->kind != ZEND_AST_UNPACK) {
10403
2.20M
      zend_eval_const_expr(&elem_ast->child[0]);
10404
2.20M
      zend_eval_const_expr(&elem_ast->child[1]);
10405
10406
2.20M
      if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL
10407
360k
        || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
10408
2.20M
      ) {
10409
1.84M
        is_constant = false;
10410
1.84M
      }
10411
2.20M
    } else {
10412
131k
      zend_eval_const_expr(&elem_ast->child[0]);
10413
10414
131k
      if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) {
10415
130k
        is_constant = false;
10416
130k
      }
10417
131k
    }
10418
10419
2.33M
    last_elem_ast = elem_ast;
10420
2.33M
  }
10421
10422
1.99M
  if (!is_constant) {
10423
1.91M
    return 0;
10424
1.91M
  }
10425
10426
74.2k
  if (!list->children) {
10427
15.5k
    ZVAL_EMPTY_ARRAY(result);
10428
15.5k
    return 1;
10429
15.5k
  }
10430
10431
58.6k
  array_init_size(result, list->children);
10432
368k
  for (i = 0; i < list->children; ++i) {
10433
311k
    const zend_ast *elem_ast = list->child[i];
10434
311k
    zend_ast *value_ast = elem_ast->child[0];
10435
311k
    zend_ast *key_ast;
10436
10437
311k
    zval *value = zend_ast_get_zval(value_ast);
10438
311k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
10439
740
      if (Z_TYPE_P(value) == IS_ARRAY) {
10440
734
        const HashTable *ht = Z_ARRVAL_P(value);
10441
734
        zval *val;
10442
734
        zend_string *key;
10443
10444
1.87k
        ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
10445
1.87k
          if (key) {
10446
109
            zend_hash_update(Z_ARRVAL_P(result), key, val);
10447
250
          } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
10448
12
            zval_ptr_dtor(result);
10449
12
            return 0;
10450
12
          }
10451
347
          Z_TRY_ADDREF_P(val);
10452
347
        } ZEND_HASH_FOREACH_END();
10453
10454
722
        continue;
10455
734
      } else {
10456
6
        zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked, %s given", zend_zval_value_name(value));
10457
6
      }
10458
740
    }
10459
10460
311k
    Z_TRY_ADDREF_P(value);
10461
10462
311k
    key_ast = elem_ast->child[1];
10463
311k
    if (key_ast) {
10464
14.5k
      const zval *key = zend_ast_get_zval(key_ast);
10465
14.5k
      switch (Z_TYPE_P(key)) {
10466
5.25k
        case IS_LONG:
10467
5.25k
          zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value);
10468
5.25k
          break;
10469
7.13k
        case IS_STRING:
10470
7.13k
          zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value);
10471
7.13k
          break;
10472
1.84k
        case IS_DOUBLE: {
10473
1.84k
          zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key));
10474
          /* Incompatible float will generate an error, leave this to run-time */
10475
1.84k
          if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) {
10476
1.61k
            goto fail;
10477
1.61k
          }
10478
235
          zend_hash_index_update(Z_ARRVAL_P(result), lval, value);
10479
235
          break;
10480
1.84k
        }
10481
260
        case IS_FALSE:
10482
260
          zend_hash_index_update(Z_ARRVAL_P(result), 0, value);
10483
260
          break;
10484
48
        case IS_TRUE:
10485
48
          zend_hash_index_update(Z_ARRVAL_P(result), 1, value);
10486
48
          break;
10487
27
        case IS_NULL:
10488
          /* Null key will generate a warning at run-time. */
10489
27
          goto fail;
10490
3
        default:
10491
3
          zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type");
10492
14.5k
      }
10493
296k
    } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) {
10494
1.70k
fail:
10495
1.70k
      zval_ptr_dtor_nogc(value);
10496
1.70k
      zval_ptr_dtor(result);
10497
1.70k
      return 0;
10498
66
    }
10499
311k
  }
10500
10501
56.9k
  return 1;
10502
58.6k
}
10503
/* }}} */
10504
10505
static void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */
10506
1.28M
{
10507
1.28M
  zend_ast *left_ast = ast->child[0];
10508
1.28M
  zend_ast *right_ast = ast->child[1];
10509
1.28M
  uint32_t opcode = ast->attr;
10510
10511
1.28M
  znode left_node, right_node;
10512
10513
1.28M
  zend_compile_expr(&left_node, left_ast);
10514
1.28M
  zend_compile_expr(&right_node, right_ast);
10515
10516
1.28M
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10517
279k
    if (zend_try_ct_eval_binary_op(&result->u.constant, opcode,
10518
279k
        &left_node.u.constant, &right_node.u.constant)
10519
279k
    ) {
10520
268k
      result->op_type = IS_CONST;
10521
268k
      zval_ptr_dtor(&left_node.u.constant);
10522
268k
      zval_ptr_dtor(&right_node.u.constant);
10523
268k
      return;
10524
268k
    }
10525
279k
  }
10526
10527
1.01M
  do {
10528
1.01M
    if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) {
10529
      /* convert $x === null to is_null($x) (i.e. ZEND_TYPE_CHECK opcode). Do the same thing for false/true. (covers IS_NULL, IS_FALSE, and IS_TRUE) */
10530
4.78k
      if (left_node.op_type == IS_CONST) {
10531
1.29k
        if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) {
10532
539
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL);
10533
539
          opline->extended_value =
10534
539
            (opcode == ZEND_IS_IDENTICAL) ?
10535
390
              (1 << Z_TYPE(left_node.u.constant)) :
10536
539
              (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant)));
10537
539
          return;
10538
539
        }
10539
3.48k
      } else if (right_node.op_type == IS_CONST) {
10540
1.64k
        if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) {
10541
377
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL);
10542
377
          opline->extended_value =
10543
377
            (opcode == ZEND_IS_IDENTICAL) ?
10544
151
              (1 << Z_TYPE(right_node.u.constant)) :
10545
377
              (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant)));
10546
377
          return;
10547
377
        }
10548
1.64k
      }
10549
1.01M
    } else if (opcode == ZEND_CONCAT) {
10550
      /* convert constant operands to strings at compile-time */
10551
89.5k
      if (left_node.op_type == IS_CONST) {
10552
14.3k
        if (Z_TYPE(left_node.u.constant) == IS_ARRAY) {
10553
311
          zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING;
10554
14.0k
        } else {
10555
14.0k
          convert_to_string(&left_node.u.constant);
10556
14.0k
        }
10557
14.3k
      }
10558
89.5k
      if (right_node.op_type == IS_CONST) {
10559
20.9k
        if (Z_TYPE(right_node.u.constant) == IS_ARRAY) {
10560
357
          zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING;
10561
20.5k
        } else {
10562
20.5k
          convert_to_string(&right_node.u.constant);
10563
20.5k
        }
10564
20.9k
      }
10565
89.5k
      if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10566
0
        opcode = ZEND_FAST_CONCAT;
10567
0
      }
10568
89.5k
    }
10569
1.01M
    zend_emit_op_tmp(result, opcode, &left_node, &right_node);
10570
1.01M
  } while (0);
10571
1.01M
}
10572
/* }}} */
10573
10574
/* We do not use zend_compile_binary_op for this because we want to retain the left-to-right
10575
 * evaluation order. */
10576
static void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */
10577
94.0k
{
10578
94.0k
  zend_ast *left_ast = ast->child[0];
10579
94.0k
  zend_ast *right_ast = ast->child[1];
10580
94.0k
  znode left_node, right_node;
10581
10582
94.0k
  ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL);
10583
10584
94.0k
  zend_compile_expr(&left_node, left_ast);
10585
94.0k
  zend_compile_expr(&right_node, right_ast);
10586
10587
94.0k
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10588
6.35k
    result->op_type = IS_CONST;
10589
6.35k
    zend_ct_eval_greater(&result->u.constant, ast->kind,
10590
6.35k
      &left_node.u.constant, &right_node.u.constant);
10591
6.35k
    zval_ptr_dtor(&left_node.u.constant);
10592
6.35k
    zval_ptr_dtor(&right_node.u.constant);
10593
6.35k
    return;
10594
6.35k
  }
10595
10596
87.6k
  zend_emit_op_tmp(result,
10597
87.6k
    ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL,
10598
87.6k
    &right_node, &left_node);
10599
87.6k
}
10600
/* }}} */
10601
10602
static void zend_compile_unary_op(znode *result, const zend_ast *ast) /* {{{ */
10603
441k
{
10604
441k
  zend_ast *expr_ast = ast->child[0];
10605
441k
  uint32_t opcode = ast->attr;
10606
10607
441k
  znode expr_node;
10608
441k
  zend_compile_expr(&expr_node, expr_ast);
10609
10610
441k
  if (expr_node.op_type == IS_CONST
10611
115k
      && zend_try_ct_eval_unary_op(&result->u.constant, opcode, &expr_node.u.constant)) {
10612
115k
    result->op_type = IS_CONST;
10613
115k
    zval_ptr_dtor(&expr_node.u.constant);
10614
115k
    return;
10615
115k
  }
10616
10617
326k
  zend_emit_op_tmp(result, opcode, &expr_node, NULL);
10618
326k
}
10619
/* }}} */
10620
10621
static void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */
10622
216k
{
10623
216k
  zend_ast *expr_ast = ast->child[0];
10624
216k
  znode expr_node, right_node;
10625
10626
216k
  ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS);
10627
10628
216k
  zend_compile_expr(&expr_node, expr_ast);
10629
10630
216k
  if (expr_node.op_type == IS_CONST
10631
35.8k
    && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) {
10632
31.3k
    result->op_type = IS_CONST;
10633
31.3k
    zval_ptr_dtor(&expr_node.u.constant);
10634
31.3k
    return;
10635
31.3k
  }
10636
10637
184k
  right_node.op_type = IS_CONST;
10638
184k
  ZVAL_LONG(&right_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10639
184k
  zend_emit_op_tmp(result, ZEND_MUL, &expr_node, &right_node);
10640
184k
}
10641
/* }}} */
10642
10643
static void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */
10644
10.6k
{
10645
10.6k
  zend_ast *left_ast = ast->child[0];
10646
10.6k
  zend_ast *right_ast = ast->child[1];
10647
10648
10.6k
  znode left_node, right_node;
10649
10.6k
  zend_op *opline_jmpz, *opline_bool;
10650
10.6k
  uint32_t opnum_jmpz;
10651
10652
10.6k
  ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR);
10653
10654
10.6k
  zend_compile_expr(&left_node, left_ast);
10655
10656
10.6k
  if (left_node.op_type == IS_CONST) {
10657
2.02k
    if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant))
10658
1.26k
     || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) {
10659
1.21k
      result->op_type = IS_CONST;
10660
1.21k
      ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant));
10661
1.21k
    } else {
10662
811
      zend_compile_expr(&right_node, right_ast);
10663
10664
811
      if (right_node.op_type == IS_CONST) {
10665
368
        result->op_type = IS_CONST;
10666
368
        ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant));
10667
10668
368
        zval_ptr_dtor(&right_node.u.constant);
10669
443
      } else {
10670
443
        zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL);
10671
443
      }
10672
811
    }
10673
10674
2.02k
    zval_ptr_dtor(&left_node.u.constant);
10675
2.02k
    return;
10676
2.02k
  }
10677
10678
8.66k
  opnum_jmpz = get_next_op_number();
10679
8.66k
  opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
10680
8.66k
    &left_node, NULL);
10681
10682
8.66k
  if (left_node.op_type == IS_TMP_VAR) {
10683
7.94k
    SET_NODE(opline_jmpz->result, &left_node);
10684
7.94k
    GET_NODE(result, opline_jmpz->result);
10685
7.94k
  } else {
10686
713
    zend_make_tmp_result(result, opline_jmpz);
10687
713
  }
10688
10689
8.66k
  zend_compile_expr(&right_node, right_ast);
10690
10691
8.66k
  opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
10692
8.66k
  SET_NODE(opline_bool->result, result);
10693
10694
8.66k
  zend_update_jump_target_to_next(opnum_jmpz);
10695
8.66k
}
10696
/* }}} */
10697
10698
static void zend_compile_post_incdec(znode *result, const zend_ast *ast) /* {{{ */
10699
9.19k
{
10700
9.19k
  zend_ast *var_ast = ast->child[0];
10701
9.19k
  ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
10702
10703
9.19k
  zend_ensure_writable_variable(var_ast);
10704
10705
9.19k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10706
681
    zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, false);
10707
681
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
10708
681
    zend_make_tmp_result(result, opline);
10709
8.50k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10710
671
    zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, false, false);
10711
671
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP;
10712
671
    zend_make_tmp_result(result, opline);
10713
7.83k
  } else {
10714
7.83k
    znode var_node;
10715
7.83k
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10716
7.83k
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10717
130
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10718
130
    }
10719
7.83k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC,
10720
7.83k
      &var_node, NULL);
10721
7.83k
  }
10722
9.19k
}
10723
/* }}} */
10724
10725
static void zend_compile_pre_incdec(znode *result, const zend_ast *ast) /* {{{ */
10726
2.80k
{
10727
2.80k
  zend_ast *var_ast = ast->child[0];
10728
2.80k
  ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
10729
10730
2.80k
  zend_ensure_writable_variable(var_ast);
10731
10732
2.80k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10733
627
    zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, false);
10734
627
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;
10735
627
    opline->result_type = IS_TMP_VAR;
10736
627
    result->op_type = IS_TMP_VAR;
10737
2.17k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10738
181
    zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, false, false);
10739
181
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP;
10740
181
    opline->result_type = IS_TMP_VAR;
10741
181
    result->op_type = IS_TMP_VAR;
10742
1.99k
  } else {
10743
1.99k
    znode var_node;
10744
1.99k
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10745
1.99k
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10746
88
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10747
88
    }
10748
1.99k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC,
10749
1.99k
      &var_node, NULL);
10750
1.99k
  }
10751
2.80k
}
10752
/* }}} */
10753
10754
static void zend_compile_cast(znode *result, const zend_ast *ast) /* {{{ */
10755
2.61k
{
10756
2.61k
  zend_ast *expr_ast = ast->child[0];
10757
2.61k
  znode expr_node;
10758
2.61k
  zend_op *opline;
10759
10760
2.61k
  zend_compile_expr(&expr_node, expr_ast);
10761
10762
2.61k
  if (ast->attr == _IS_BOOL) {
10763
154
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL);
10764
2.46k
  } else if (ast->attr == IS_NULL) {
10765
16
    zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
10766
2.44k
  } else {
10767
2.44k
    opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
10768
2.44k
    opline->extended_value = ast->attr;
10769
2.44k
  }
10770
2.61k
}
10771
/* }}} */
10772
10773
static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */
10774
2.54k
{
10775
2.54k
  zend_ast *cond_ast = ast->child[0];
10776
2.54k
  zend_ast *false_ast = ast->child[2];
10777
10778
2.54k
  znode cond_node, false_node;
10779
2.54k
  zend_op *opline_qm_assign;
10780
2.54k
  uint32_t opnum_jmp_set;
10781
10782
2.54k
  ZEND_ASSERT(ast->child[1] == NULL);
10783
10784
2.54k
  zend_compile_expr(&cond_node, cond_ast);
10785
10786
2.54k
  opnum_jmp_set = get_next_op_number();
10787
2.54k
  zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL);
10788
10789
2.54k
  zend_compile_expr(&false_node, false_ast);
10790
10791
2.54k
  opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10792
2.54k
  SET_NODE(opline_qm_assign->result, result);
10793
10794
2.54k
  zend_update_jump_target_to_next(opnum_jmp_set);
10795
2.54k
}
10796
/* }}} */
10797
10798
static void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
10799
7.36k
{
10800
7.36k
  zend_ast *cond_ast = ast->child[0];
10801
7.36k
  zend_ast *true_ast = ast->child[1];
10802
7.36k
  zend_ast *false_ast = ast->child[2];
10803
10804
7.36k
  znode cond_node, true_node, false_node;
10805
7.36k
  zend_op *opline_qm_assign2;
10806
7.36k
  uint32_t opnum_jmpz, opnum_jmp;
10807
10808
7.36k
  if (cond_ast->kind == ZEND_AST_CONDITIONAL
10809
1.02k
      && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) {
10810
887
    if (cond_ast->child[1]) {
10811
29
      if (true_ast) {
10812
16
        zend_error(E_COMPILE_ERROR,
10813
16
          "Unparenthesized `a ? b : c ? d : e` is not supported. "
10814
16
          "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`");
10815
16
      } else {
10816
13
        zend_error(E_COMPILE_ERROR,
10817
13
          "Unparenthesized `a ? b : c ?: d` is not supported. "
10818
13
          "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`");
10819
13
      }
10820
858
    } else {
10821
858
      if (true_ast) {
10822
6
        zend_error(E_COMPILE_ERROR,
10823
6
          "Unparenthesized `a ?: b ? c : d` is not supported. "
10824
6
          "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`");
10825
852
      } else {
10826
        /* This case is harmless:  (a ?: b) ?: c always produces the same result
10827
         * as a ?: (b ?: c). */
10828
852
      }
10829
858
    }
10830
887
  }
10831
10832
7.36k
  if (!true_ast) {
10833
2.54k
    zend_compile_shorthand_conditional(result, ast);
10834
2.54k
    return;
10835
2.54k
  }
10836
10837
4.82k
  zend_compile_expr(&cond_node, cond_ast);
10838
10839
4.82k
  opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
10840
10841
4.82k
  zend_compile_expr(&true_node, true_ast);
10842
10843
4.82k
  zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL);
10844
10845
4.82k
  opnum_jmp = zend_emit_jump(0);
10846
10847
4.82k
  zend_update_jump_target_to_next(opnum_jmpz);
10848
10849
4.82k
  zend_compile_expr(&false_node, false_ast);
10850
10851
4.82k
  opline_qm_assign2 = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10852
4.82k
  SET_NODE(opline_qm_assign2->result, result);
10853
10854
4.82k
  zend_update_jump_target_to_next(opnum_jmp);
10855
4.82k
}
10856
/* }}} */
10857
10858
static void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
10859
551k
{
10860
551k
  zend_ast *expr_ast = ast->child[0];
10861
551k
  zend_ast *default_ast = ast->child[1];
10862
10863
551k
  znode expr_node, default_node;
10864
551k
  zend_op *opline;
10865
551k
  uint32_t opnum;
10866
10867
551k
  zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, false);
10868
10869
551k
  opnum = get_next_op_number();
10870
551k
  zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL);
10871
10872
551k
  zend_compile_expr(&default_node, default_ast);
10873
10874
551k
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL);
10875
551k
  SET_NODE(opline->result, result);
10876
10877
551k
  opline = &CG(active_op_array)->opcodes[opnum];
10878
551k
  opline->op2.opline_num = get_next_op_number();
10879
551k
}
10880
/* }}} */
10881
10882
74.1k
static void znode_dtor(zval *zv) {
10883
74.1k
  znode *node = Z_PTR_P(zv);
10884
74.1k
  if (node->op_type == IS_CONST) {
10885
5.42k
    zval_ptr_dtor_nogc(&node->u.constant);
10886
5.42k
  }
10887
74.1k
  efree(node);
10888
74.1k
}
10889
10890
static void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */
10891
24.0k
{
10892
24.0k
  zend_ast *var_ast = ast->child[0];
10893
24.0k
  zend_ast *default_ast = ast->child[1];
10894
10895
24.0k
  znode var_node_is, var_node_w, default_node, assign_node, *node;
10896
24.0k
  zend_op *opline;
10897
24.0k
  uint32_t coalesce_opnum;
10898
24.0k
  bool need_frees = false;
10899
10900
  /* Remember expressions compiled during the initial BP_VAR_IS lookup,
10901
   * to avoid double-evaluation when we compile again with BP_VAR_W. */
10902
24.0k
  HashTable *orig_memoized_exprs = CG(memoized_exprs);
10903
24.0k
  const zend_memoize_mode orig_memoize_mode = CG(memoize_mode);
10904
10905
24.0k
  zend_ensure_writable_variable(var_ast);
10906
24.0k
  if (is_this_fetch(var_ast)) {
10907
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
10908
5
  }
10909
10910
24.0k
  ALLOC_HASHTABLE(CG(memoized_exprs));
10911
24.0k
  zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0);
10912
10913
24.0k
  CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
10914
24.0k
  zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, false);
10915
10916
24.0k
  coalesce_opnum = get_next_op_number();
10917
24.0k
  zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL);
10918
10919
24.0k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
10920
24.0k
  if (var_ast->kind == ZEND_AST_DIM) {
10921
21.5k
    zend_compile_expr_with_potential_assign_to_self(&default_node, default_ast, var_ast);
10922
21.5k
  } else {
10923
2.52k
    zend_compile_expr(&default_node, default_ast);
10924
2.52k
  }
10925
10926
24.0k
  CG(memoize_mode) = ZEND_MEMOIZE_FETCH;
10927
24.0k
  zend_compile_var(&var_node_w, var_ast, BP_VAR_W, false);
10928
10929
  /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */
10930
24.0k
  opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
10931
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
10932
24.0k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
10933
24.0k
  switch (kind) {
10934
1.36k
    case ZEND_AST_VAR:
10935
1.36k
      zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node);
10936
1.36k
      break;
10937
642
    case ZEND_AST_STATIC_PROP:
10938
642
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
10939
642
      opline->result_type = IS_TMP_VAR;
10940
642
      var_node_w.op_type = IS_TMP_VAR;
10941
642
      zend_emit_op_data(&default_node);
10942
642
      assign_node = var_node_w;
10943
642
      break;
10944
21.2k
    case ZEND_AST_DIM:
10945
21.2k
      opline->opcode = ZEND_ASSIGN_DIM;
10946
21.2k
      opline->result_type = IS_TMP_VAR;
10947
21.2k
      var_node_w.op_type = IS_TMP_VAR;
10948
21.2k
      zend_emit_op_data(&default_node);
10949
21.2k
      assign_node = var_node_w;
10950
21.2k
      break;
10951
622
    case ZEND_AST_PROP:
10952
622
    case ZEND_AST_NULLSAFE_PROP:
10953
622
      opline->opcode = ZEND_ASSIGN_OBJ;
10954
622
      opline->result_type = IS_TMP_VAR;
10955
622
      var_node_w.op_type = IS_TMP_VAR;
10956
622
      zend_emit_op_data(&default_node);
10957
622
      assign_node = var_node_w;
10958
622
      break;
10959
0
    EMPTY_SWITCH_DEFAULT_CASE();
10960
24.0k
  }
10961
10962
23.8k
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL);
10963
23.8k
  SET_NODE(opline->result, result);
10964
10965
76.3k
  ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10966
76.3k
    if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10967
21.2k
      need_frees = true;
10968
21.2k
      break;
10969
21.2k
    }
10970
76.3k
  } ZEND_HASH_FOREACH_END();
10971
10972
  /* Free DUPed expressions if there are any */
10973
23.8k
  if (need_frees) {
10974
21.2k
    uint32_t jump_opnum = zend_emit_jump(0);
10975
21.2k
    zend_update_jump_target_to_next(coalesce_opnum);
10976
163k
    ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10977
163k
      if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10978
67.7k
        zend_emit_op(NULL, ZEND_FREE, node, NULL);
10979
67.7k
      }
10980
163k
    } ZEND_HASH_FOREACH_END();
10981
21.2k
    zend_update_jump_target_to_next(jump_opnum);
10982
21.2k
  } else {
10983
2.69k
    zend_update_jump_target_to_next(coalesce_opnum);
10984
2.69k
  }
10985
10986
23.8k
  zend_hash_destroy(CG(memoized_exprs));
10987
23.8k
  FREE_HASHTABLE(CG(memoized_exprs));
10988
23.8k
  CG(memoized_exprs) = orig_memoized_exprs;
10989
23.8k
  CG(memoize_mode) = orig_memoize_mode;
10990
23.8k
}
10991
/* }}} */
10992
10993
static void zend_compile_print(znode *result, const zend_ast *ast) /* {{{ */
10994
3.09k
{
10995
3.09k
  zend_op *opline;
10996
3.09k
  zend_ast *expr_ast = ast->child[0];
10997
10998
3.09k
  znode expr_node;
10999
3.09k
  zend_compile_expr(&expr_node, expr_ast);
11000
11001
3.09k
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
11002
3.09k
  opline->extended_value = 1;
11003
11004
3.09k
  result->op_type = IS_CONST;
11005
3.09k
  ZVAL_LONG(&result->u.constant, 1);
11006
3.09k
}
11007
/* }}} */
11008
11009
static void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
11010
169k
{
11011
169k
  zend_ast *value_ast = ast->child[0];
11012
169k
  zend_ast *key_ast = ast->child[1];
11013
11014
169k
  znode value_node, key_node;
11015
169k
  znode *value_node_ptr = NULL, *key_node_ptr = NULL;
11016
169k
  zend_op *opline;
11017
169k
  bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
11018
11019
169k
  zend_mark_function_as_generator();
11020
11021
169k
  if (key_ast) {
11022
375
    zend_compile_expr(&key_node, key_ast);
11023
375
    key_node_ptr = &key_node;
11024
375
  }
11025
11026
169k
  if (value_ast) {
11027
167k
    if (returns_by_ref && zend_is_variable_or_call(value_ast)) {
11028
445
      zend_assert_not_short_circuited(value_ast);
11029
445
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
11030
167k
    } else {
11031
167k
      zend_compile_expr(&value_node, value_ast);
11032
167k
    }
11033
167k
    value_node_ptr = &value_node;
11034
167k
  }
11035
11036
169k
  opline = zend_emit_op_tmp(result, ZEND_YIELD, value_node_ptr, key_node_ptr);
11037
11038
169k
  if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
11039
128
    opline->extended_value = ZEND_RETURNS_FUNCTION;
11040
128
  }
11041
169k
}
11042
/* }}} */
11043
11044
static void zend_compile_yield_from(znode *result, const zend_ast *ast) /* {{{ */
11045
1.72k
{
11046
1.72k
  zend_ast *expr_ast = ast->child[0];
11047
1.72k
  znode expr_node;
11048
11049
1.72k
  zend_mark_function_as_generator();
11050
11051
1.72k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
11052
5
    zend_error_noreturn(E_COMPILE_ERROR,
11053
5
      "Cannot use \"yield from\" inside a by-reference generator");
11054
5
  }
11055
11056
1.71k
  zend_compile_expr(&expr_node, expr_ast);
11057
1.71k
  zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
11058
1.71k
}
11059
/* }}} */
11060
11061
static void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */
11062
964
{
11063
964
  zend_ast *obj_ast = ast->child[0];
11064
964
  zend_ast *class_ast = ast->child[1];
11065
11066
964
  znode obj_node, class_node;
11067
964
  zend_op *opline;
11068
11069
964
  zend_compile_expr(&obj_node, obj_ast);
11070
964
  if (obj_node.op_type == IS_CONST) {
11071
29
    zend_do_free(&obj_node);
11072
29
    result->op_type = IS_CONST;
11073
29
    ZVAL_FALSE(&result->u.constant);
11074
29
    return;
11075
29
  }
11076
11077
935
  zend_compile_class_ref(&class_node, class_ast,
11078
935
    ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION | ZEND_FETCH_CLASS_SILENT);
11079
11080
935
  opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL);
11081
11082
935
  if (class_node.op_type == IS_CONST) {
11083
733
    opline->op2_type = IS_CONST;
11084
733
    opline->op2.constant = zend_add_class_name_literal(
11085
733
      Z_STR(class_node.u.constant));
11086
733
    opline->extended_value = zend_alloc_cache_slot();
11087
733
  } else {
11088
202
    SET_NODE(opline->op2, &class_node);
11089
202
  }
11090
935
}
11091
/* }}} */
11092
11093
static void zend_compile_include_or_eval(znode *result, const zend_ast *ast) /* {{{ */
11094
7.63k
{
11095
7.63k
  zend_ast *expr_ast = ast->child[0];
11096
7.63k
  znode expr_node;
11097
7.63k
  zend_op *opline;
11098
11099
7.63k
  zend_do_extended_fcall_begin();
11100
7.63k
  zend_compile_expr(&expr_node, expr_ast);
11101
11102
7.63k
  opline = zend_emit_op_tmp(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL);
11103
7.63k
  opline->extended_value = ast->attr;
11104
11105
7.63k
  zend_do_extended_fcall_end();
11106
7.63k
}
11107
/* }}} */
11108
11109
static void zend_compile_isset_or_empty(znode *result, const zend_ast *ast) /* {{{ */
11110
14.0k
{
11111
14.0k
  zend_ast *var_ast = ast->child[0];
11112
11113
14.0k
  znode var_node;
11114
14.0k
  zend_op *opline = NULL;
11115
11116
14.0k
  ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY);
11117
11118
14.0k
  if (!zend_is_variable(var_ast)) {
11119
168
    if (ast->kind == ZEND_AST_EMPTY) {
11120
      /* empty(expr) can be transformed to !expr */
11121
129
      zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast);
11122
129
      zend_compile_expr(result, not_ast);
11123
129
      return;
11124
129
    } else {
11125
39
      zend_error_noreturn(E_COMPILE_ERROR,
11126
39
        "Cannot use isset() on the result of an expression "
11127
39
        "(you can use \"null !== expression\" instead)");
11128
39
    }
11129
168
  }
11130
11131
13.8k
  if (is_globals_fetch(var_ast)) {
11132
249
    result->op_type = IS_CONST;
11133
249
    ZVAL_BOOL(&result->u.constant, ast->kind == ZEND_AST_ISSET);
11134
249
    return;
11135
249
  }
11136
11137
13.6k
  if (is_global_var_fetch(var_ast)) {
11138
7.75k
    if (!var_ast->child[1]) {
11139
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
11140
6
    }
11141
11142
7.75k
    zend_compile_expr(&var_node, var_ast->child[1]);
11143
7.75k
    if (var_node.op_type == IS_CONST) {
11144
7.72k
      convert_to_string(&var_node.u.constant);
11145
7.72k
    }
11146
11147
7.75k
    opline = zend_emit_op_tmp(result, ZEND_ISSET_ISEMPTY_VAR, &var_node, NULL);
11148
7.75k
    opline->extended_value =
11149
7.75k
      ZEND_FETCH_GLOBAL | (ast->kind == ZEND_AST_EMPTY ? ZEND_ISEMPTY : 0);
11150
7.75k
    return;
11151
7.75k
  }
11152
11153
5.88k
  zend_short_circuiting_mark_inner(var_ast);
11154
5.88k
  switch (var_ast->kind) {
11155
1.28k
    case ZEND_AST_VAR:
11156
1.28k
      if (is_this_fetch(var_ast)) {
11157
111
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL);
11158
111
        CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
11159
1.17k
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_IS) == SUCCESS) {
11160
1.02k
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL);
11161
1.02k
      } else {
11162
151
        opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, false);
11163
151
        opline->opcode = ZEND_ISSET_ISEMPTY_VAR;
11164
151
      }
11165
1.28k
      break;
11166
3.41k
    case ZEND_AST_DIM:
11167
3.41k
      opline = zend_compile_dim(result, var_ast, BP_VAR_IS, /* by_ref */ false);
11168
3.41k
      opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
11169
3.41k
      break;
11170
838
    case ZEND_AST_PROP:
11171
946
    case ZEND_AST_NULLSAFE_PROP:
11172
946
      opline = zend_compile_prop(result, var_ast, BP_VAR_IS, false);
11173
946
      opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
11174
946
      break;
11175
241
    case ZEND_AST_STATIC_PROP:
11176
241
      opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, false, false);
11177
241
      opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP;
11178
241
      break;
11179
5.88k
    EMPTY_SWITCH_DEFAULT_CASE()
11180
5.88k
  }
11181
11182
5.87k
  result->op_type = opline->result_type = IS_TMP_VAR;
11183
5.87k
  if (!(ast->kind == ZEND_AST_ISSET)) {
11184
815
    opline->extended_value |= ZEND_ISEMPTY;
11185
815
  }
11186
5.87k
}
11187
/* }}} */
11188
11189
static void zend_compile_silence(znode *result, const zend_ast *ast) /* {{{ */
11190
5.13M
{
11191
5.13M
  zend_ast *expr_ast = ast->child[0];
11192
5.13M
  znode silence_node;
11193
11194
5.13M
  zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL);
11195
11196
5.13M
  if (expr_ast->kind == ZEND_AST_VAR) {
11197
    /* For @$var we need to force a FETCH instruction, otherwise the CV access will
11198
     * happen outside the silenced section. */
11199
22.4k
    zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, false );
11200
5.11M
  } else {
11201
5.11M
    zend_compile_expr(result, expr_ast);
11202
5.11M
  }
11203
11204
5.13M
  zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL);
11205
5.13M
}
11206
/* }}} */
11207
11208
static void zend_compile_shell_exec(znode *result, const zend_ast *ast) /* {{{ */
11209
58.2k
{
11210
58.2k
  zend_ast *expr_ast = ast->child[0];
11211
11212
58.2k
  zval fn_name;
11213
58.2k
  zend_ast *name_ast, *args_ast, *call_ast;
11214
11215
58.2k
  zend_error(E_DEPRECATED, "The backtick (`) operator is deprecated, use shell_exec() instead");
11216
11217
58.2k
  ZVAL_STRING(&fn_name, "shell_exec");
11218
58.2k
  name_ast = zend_ast_create_zval(&fn_name);
11219
58.2k
  args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast);
11220
58.2k
  call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast);
11221
11222
58.2k
  zend_compile_expr(result, call_ast);
11223
11224
58.2k
  zval_ptr_dtor(&fn_name);
11225
58.2k
}
11226
/* }}} */
11227
11228
static void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
11229
74.6k
{
11230
74.6k
  zend_ast_list *list = zend_ast_get_list(ast);
11231
74.6k
  zend_op *opline;
11232
74.6k
  uint32_t i, opnum_init = -1;
11233
74.6k
  bool packed = true;
11234
11235
74.6k
  if (zend_try_ct_eval_array(&result->u.constant, ast)) {
11236
42.1k
    result->op_type = IS_CONST;
11237
42.1k
    return;
11238
42.1k
  }
11239
11240
  /* Empty arrays are handled at compile-time */
11241
32.5k
  ZEND_ASSERT(list->children > 0);
11242
11243
136k
  for (i = 0; i < list->children; ++i) {
11244
104k
    zend_ast *elem_ast = list->child[i];
11245
104k
    zend_ast *value_ast, *key_ast;
11246
104k
    bool by_ref;
11247
104k
    znode value_node, key_node, *key_node_ptr = NULL;
11248
11249
104k
    if (elem_ast == NULL) {
11250
0
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
11251
0
    }
11252
11253
104k
    value_ast = elem_ast->child[0];
11254
11255
104k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
11256
1.28k
      zend_compile_expr(&value_node, value_ast);
11257
1.28k
      if (i == 0) {
11258
986
        opnum_init = get_next_op_number();
11259
986
        opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
11260
986
      }
11261
1.28k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL);
11262
1.28k
      SET_NODE(opline->result, result);
11263
1.28k
      continue;
11264
1.28k
    }
11265
11266
103k
    key_ast = elem_ast->child[1];
11267
103k
    by_ref = elem_ast->attr;
11268
11269
103k
    if (key_ast) {
11270
4.30k
      zend_compile_expr(&key_node, key_ast);
11271
4.30k
      zend_handle_numeric_op(&key_node);
11272
4.30k
      key_node_ptr = &key_node;
11273
4.30k
    }
11274
11275
103k
    if (by_ref) {
11276
357
      zend_ensure_writable_variable(value_ast);
11277
357
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
11278
102k
    } else {
11279
102k
      zend_compile_expr(&value_node, value_ast);
11280
102k
    }
11281
11282
103k
    if (i == 0) {
11283
31.4k
      opnum_init = get_next_op_number();
11284
31.4k
      opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr);
11285
31.4k
      opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT;
11286
71.8k
    } else {
11287
71.8k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT,
11288
71.8k
        &value_node, key_node_ptr);
11289
71.8k
      SET_NODE(opline->result, result);
11290
71.8k
    }
11291
103k
    opline->extended_value |= by_ref;
11292
11293
103k
    if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) {
11294
2.00k
      packed = false;
11295
2.00k
    }
11296
103k
  }
11297
11298
  /* Add a flag to INIT_ARRAY if we know this array cannot be packed */
11299
32.4k
  if (!packed) {
11300
1.04k
    ZEND_ASSERT(opnum_init != (uint32_t)-1);
11301
1.04k
    opline = &CG(active_op_array)->opcodes[opnum_init];
11302
1.04k
    opline->extended_value |= ZEND_ARRAY_NOT_PACKED;
11303
1.04k
  }
11304
32.4k
}
11305
/* }}} */
11306
11307
static void zend_compile_const(znode *result, const zend_ast *ast) /* {{{ */
11308
3.03M
{
11309
3.03M
  zend_ast *name_ast = ast->child[0];
11310
11311
3.03M
  zend_op *opline;
11312
11313
3.03M
  bool is_fully_qualified;
11314
3.03M
  zend_string *orig_name = zend_ast_get_str(name_ast);
11315
3.03M
  zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified);
11316
11317
3.03M
  if (zend_string_equals_literal(resolved_name, "__COMPILER_HALT_OFFSET__") || (name_ast->attr != ZEND_NAME_RELATIVE && zend_string_equals_literal(orig_name, "__COMPILER_HALT_OFFSET__"))) {
11318
485
    zend_ast *last = CG(ast);
11319
11320
974
    while (last && last->kind == ZEND_AST_STMT_LIST) {
11321
641
      const zend_ast_list *list = zend_ast_get_list(last);
11322
641
      if (list->children == 0) {
11323
152
        break;
11324
152
      }
11325
489
      last = list->child[list->children-1];
11326
489
    }
11327
485
    if (last && last->kind == ZEND_AST_HALT_COMPILER) {
11328
80
      result->op_type = IS_CONST;
11329
80
      ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
11330
80
      zend_string_release_ex(resolved_name, 0);
11331
80
      return;
11332
80
    }
11333
485
  }
11334
11335
3.03M
  if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
11336
36.8k
    result->op_type = IS_CONST;
11337
36.8k
    zend_string_release_ex(resolved_name, 0);
11338
36.8k
    return;
11339
36.8k
  }
11340
11341
3.00M
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
11342
3.00M
  opline->op2_type = IS_CONST;
11343
11344
3.00M
  if (is_fully_qualified || !FC(current_namespace)) {
11345
132k
    opline->op1.num = 0;
11346
132k
    opline->op2.constant = zend_add_const_name_literal(
11347
132k
      resolved_name, false);
11348
2.86M
  } else {
11349
2.86M
    opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE;
11350
2.86M
    opline->op2.constant = zend_add_const_name_literal(
11351
2.86M
      resolved_name, true);
11352
2.86M
  }
11353
3.00M
  opline->extended_value = zend_alloc_cache_slot();
11354
3.00M
}
11355
/* }}} */
11356
11357
static void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */
11358
37.7k
{
11359
37.7k
  zend_ast *class_ast;
11360
37.7k
  zend_ast *const_ast;
11361
37.7k
  znode class_node, const_node;
11362
37.7k
  zend_op *opline;
11363
11364
37.7k
  zend_eval_const_expr(&ast->child[0]);
11365
37.7k
  zend_eval_const_expr(&ast->child[1]);
11366
11367
37.7k
  class_ast = ast->child[0];
11368
37.7k
  const_ast = ast->child[1];
11369
11370
37.7k
  if (class_ast->kind == ZEND_AST_ZVAL && const_ast->kind == ZEND_AST_ZVAL) {
11371
25.6k
    zval *const_zv = zend_ast_get_zval(const_ast);
11372
25.6k
    if (Z_TYPE_P(const_zv) == IS_STRING) {
11373
25.1k
      zend_string *const_str = Z_STR_P(const_zv);
11374
25.1k
      zend_string *resolved_name = zend_resolve_class_name_ast(class_ast);
11375
25.1k
      if (zend_try_ct_eval_class_const(&result->u.constant, resolved_name, const_str)) {
11376
230
        result->op_type = IS_CONST;
11377
230
        zend_string_release_ex(resolved_name, 0);
11378
230
        return;
11379
230
      }
11380
24.9k
      zend_string_release_ex(resolved_name, 0);
11381
24.9k
    }
11382
25.6k
  }
11383
11384
37.4k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
11385
11386
37.4k
  zend_compile_expr(&const_node, const_ast);
11387
11388
37.4k
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node);
11389
11390
37.4k
  zend_set_class_name_op1(opline, &class_node);
11391
11392
37.4k
  if (opline->op1_type == IS_CONST || opline->op2_type == IS_CONST) {
11393
37.0k
    opline->extended_value = zend_alloc_cache_slots(2);
11394
37.0k
  }
11395
37.4k
}
11396
/* }}} */
11397
11398
static void zend_compile_class_name(znode *result, const zend_ast *ast) /* {{{ */
11399
4.19k
{
11400
4.19k
  zend_ast *class_ast = ast->child[0];
11401
11402
4.19k
  if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) {
11403
2.54k
    result->op_type = IS_CONST;
11404
2.54k
    return;
11405
2.54k
  }
11406
11407
1.65k
  if (class_ast->kind == ZEND_AST_ZVAL) {
11408
509
    zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11409
509
    opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
11410
1.14k
  } else {
11411
1.14k
    znode expr_node;
11412
1.14k
    zend_compile_expr(&expr_node, class_ast);
11413
1.14k
    if (expr_node.op_type == IS_CONST) {
11414
      /* Unlikely case that happen if class_ast is constant folded.
11415
       * Handle it here, to avoid needing a CONST specialization in the VM. */
11416
8
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"::class\" on %s",
11417
8
        zend_zval_value_name(&expr_node.u.constant));
11418
8
    }
11419
11420
1.13k
    zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL);
11421
1.13k
  }
11422
1.65k
}
11423
/* }}} */
11424
11425
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */
11426
259k
{
11427
259k
  if (num == 0) {
11428
28.6k
    result->op_type = IS_TMP_VAR;
11429
28.6k
    result->u.op.var = -1;
11430
28.6k
    opline->opcode = ZEND_ROPE_INIT;
11431
230k
  } else {
11432
230k
    opline->opcode = ZEND_ROPE_ADD;
11433
230k
    SET_NODE(opline->op1, result);
11434
230k
  }
11435
259k
  SET_NODE(opline->op2, elem_node);
11436
259k
  SET_NODE(opline->result, result);
11437
259k
  opline->extended_value = num;
11438
259k
  return opline;
11439
259k
}
11440
/* }}} */
11441
11442
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */
11443
252k
{
11444
252k
  zend_op *opline = get_next_op();
11445
11446
252k
  if (num == 0) {
11447
60.5k
    result->op_type = IS_TMP_VAR;
11448
60.5k
    result->u.op.var = -1;
11449
60.5k
    opline->opcode = ZEND_ROPE_INIT;
11450
191k
  } else {
11451
191k
    opline->opcode = ZEND_ROPE_ADD;
11452
191k
    SET_NODE(opline->op1, result);
11453
191k
  }
11454
252k
  SET_NODE(opline->op2, elem_node);
11455
252k
  SET_NODE(opline->result, result);
11456
252k
  opline->extended_value = num;
11457
252k
  return opline;
11458
252k
}
11459
/* }}} */
11460
11461
static void zend_compile_rope_finalize(znode *result, uint32_t rope_elements, zend_op *init_opline, zend_op *opline)
11462
89.2k
{
11463
89.2k
  if (rope_elements == 1) {
11464
1.87k
    if (opline->op2_type == IS_CONST) {
11465
764
      GET_NODE(result, opline->op2);
11466
764
      ZVAL_UNDEF(CT_CONSTANT(opline->op2));
11467
764
      SET_UNUSED(opline->op2);
11468
764
      MAKE_NOP(opline);
11469
1.11k
    } else {
11470
1.11k
      opline->opcode = ZEND_CAST;
11471
1.11k
      opline->extended_value = IS_STRING;
11472
1.11k
      opline->op1_type = opline->op2_type;
11473
1.11k
      opline->op1 = opline->op2;
11474
1.11k
      SET_UNUSED(opline->op2);
11475
1.11k
      zend_make_tmp_result(result, opline);
11476
1.11k
    }
11477
87.3k
  } else if (rope_elements == 2) {
11478
53.4k
    opline->opcode = ZEND_FAST_CONCAT;
11479
53.4k
    opline->extended_value = 0;
11480
53.4k
    opline->op1_type = init_opline->op2_type;
11481
53.4k
    opline->op1 = init_opline->op2;
11482
53.4k
    zend_make_tmp_result(result, opline);
11483
53.4k
    MAKE_NOP(init_opline);
11484
53.4k
  } else {
11485
33.9k
    uint32_t var;
11486
11487
33.9k
    init_opline->extended_value = rope_elements;
11488
33.9k
    opline->opcode = ZEND_ROPE_END;
11489
33.9k
    zend_make_tmp_result(result, opline);
11490
33.9k
    var = opline->op1.var = get_temporary_variable();
11491
11492
    /* Allocates the necessary number of zval slots to keep the rope */
11493
33.9k
    uint32_t i = ((rope_elements * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
11494
216k
    while (i > 1) {
11495
183k
      get_temporary_variable();
11496
183k
      i--;
11497
183k
    }
11498
11499
    /* Update all the previous opcodes to use the same variable */
11500
508k
    while (opline != init_opline) {
11501
474k
      opline--;
11502
474k
      if (opline->opcode == ZEND_ROPE_ADD &&
11503
336k
          opline->result.var == (uint32_t)-1) {
11504
335k
        opline->op1.var = var;
11505
335k
        opline->result.var = var;
11506
335k
      } else if (opline->opcode == ZEND_ROPE_INIT &&
11507
34.2k
                 opline->result.var == (uint32_t)-1) {
11508
33.9k
        opline->result.var = var;
11509
33.9k
      }
11510
474k
    }
11511
33.9k
  }
11512
89.2k
}
11513
11514
static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
11515
86.5k
{
11516
86.5k
  uint32_t i, j;
11517
86.5k
  uint32_t rope_init_lineno = -1;
11518
86.5k
  zend_op *opline = NULL, *init_opline;
11519
86.5k
  znode elem_node, last_const_node;
11520
86.5k
  zend_ast_list *list = zend_ast_get_list(ast);
11521
86.5k
  uint32_t reserved_op_number = -1;
11522
11523
86.5k
  ZEND_ASSERT(list->children > 0);
11524
11525
86.5k
  j = 0;
11526
86.5k
  last_const_node.op_type = IS_UNUSED;
11527
591k
  for (i = 0; i < list->children; i++) {
11528
505k
    zend_ast *encaps_var = list->child[i];
11529
11530
505k
    if (encaps_var->attr & (ZEND_ENCAPS_VAR_DOLLAR_CURLY|ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11531
55.3k
      if ((encaps_var->kind == ZEND_AST_VAR || encaps_var->kind == ZEND_AST_DIM) && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY)) {
11532
490
        zend_error(E_DEPRECATED, "Using ${var} in strings is deprecated, use {$var} instead");
11533
54.9k
      } else if (encaps_var->kind == ZEND_AST_VAR && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11534
54.9k
        zend_error(E_DEPRECATED, "Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead");
11535
54.9k
      }
11536
55.3k
    }
11537
11538
505k
    zend_compile_expr(&elem_node, encaps_var);
11539
11540
505k
    if (elem_node.op_type == IS_CONST) {
11541
260k
      convert_to_string(&elem_node.u.constant);
11542
11543
260k
      if (Z_STRLEN(elem_node.u.constant) == 0) {
11544
667
        zval_ptr_dtor(&elem_node.u.constant);
11545
259k
      } else if (last_const_node.op_type == IS_CONST) {
11546
0
        concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant);
11547
0
        zval_ptr_dtor(&elem_node.u.constant);
11548
259k
      } else {
11549
259k
        last_const_node.op_type = IS_CONST;
11550
259k
        ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant);
11551
        /* Reserve place for ZEND_ROPE_ADD instruction */
11552
259k
        reserved_op_number = get_next_op_number();
11553
259k
        opline = get_next_op();
11554
259k
        opline->opcode = ZEND_NOP;
11555
259k
      }
11556
260k
      continue;
11557
260k
    } else {
11558
244k
      if (j == 0) {
11559
86.5k
        if (last_const_node.op_type == IS_CONST) {
11560
28.6k
          rope_init_lineno = reserved_op_number;
11561
57.8k
        } else {
11562
57.8k
          rope_init_lineno = get_next_op_number();
11563
57.8k
        }
11564
86.5k
      }
11565
244k
      if (last_const_node.op_type == IS_CONST) {
11566
177k
        opline = &CG(active_op_array)->opcodes[reserved_op_number];
11567
177k
        zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11568
177k
        last_const_node.op_type = IS_UNUSED;
11569
177k
      }
11570
244k
      opline = zend_compile_rope_add(result, j++, &elem_node);
11571
244k
    }
11572
505k
  }
11573
11574
86.5k
  if (j == 0) {
11575
0
    result->op_type = IS_CONST;
11576
0
    if (last_const_node.op_type == IS_CONST) {
11577
0
      ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant);
11578
0
    } else {
11579
0
      ZVAL_EMPTY_STRING(&result->u.constant);
11580
      /* empty string */
11581
0
    }
11582
0
    CG(active_op_array)->last = reserved_op_number - 1;
11583
0
    return;
11584
86.5k
  } else if (last_const_node.op_type == IS_CONST) {
11585
82.1k
    opline = &CG(active_op_array)->opcodes[reserved_op_number];
11586
82.1k
    opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11587
82.1k
  }
11588
86.5k
  init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
11589
86.5k
  zend_compile_rope_finalize(result, j, init_opline, opline);
11590
86.5k
}
11591
/* }}} */
11592
11593
static void zend_compile_magic_const(znode *result, const zend_ast *ast) /* {{{ */
11594
22.6k
{
11595
22.6k
  zend_op *opline;
11596
11597
22.6k
  if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) {
11598
21.4k
    result->op_type = IS_CONST;
11599
21.4k
    return;
11600
21.4k
  }
11601
11602
1.24k
  ZEND_ASSERT(ast->attr == T_CLASS_C &&
11603
1.24k
              CG(active_class_entry) &&
11604
1.24k
              (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
11605
11606
1.24k
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11607
1.24k
  opline->op1.num = ZEND_FETCH_CLASS_SELF;
11608
1.24k
}
11609
/* }}} */
11610
11611
static bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */
11612
39.1k
{
11613
39.1k
  return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP
11614
35.3k
    || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL
11615
34.7k
    || kind == ZEND_AST_AND || kind == ZEND_AST_OR
11616
34.2k
    || kind == ZEND_AST_UNARY_OP
11617
33.5k
    || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS
11618
32.8k
    || kind == ZEND_AST_CAST
11619
32.4k
    || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM
11620
31.1k
    || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM
11621
28.9k
    || kind == ZEND_AST_UNPACK
11622
28.8k
    || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST
11623
11.7k
    || kind == ZEND_AST_CLASS_NAME
11624
11.6k
    || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE
11625
11.0k
    || kind == ZEND_AST_CONST_ENUM_INIT
11626
2.82k
    || kind == ZEND_AST_NEW || kind == ZEND_AST_ARG_LIST
11627
1.32k
    || kind == ZEND_AST_NAMED_ARG
11628
1.26k
    || kind == ZEND_AST_PROP || kind == ZEND_AST_NULLSAFE_PROP
11629
872
    || kind == ZEND_AST_CLOSURE
11630
731
    || kind == ZEND_AST_CALL || kind == ZEND_AST_STATIC_CALL || kind == ZEND_AST_CALLABLE_CONVERT;
11631
39.1k
}
11632
/* }}} */
11633
11634
static void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
11635
2.82k
{
11636
2.82k
  zend_ast *ast = *ast_ptr;
11637
2.82k
  zend_ast *class_ast = ast->child[0];
11638
2.82k
  zend_string *class_name;
11639
2.82k
  int fetch_type;
11640
11641
2.82k
  if (class_ast->kind != ZEND_AST_ZVAL) {
11642
15
    zend_error_noreturn(E_COMPILE_ERROR,
11643
15
      "Dynamic class names are not allowed in compile-time class constant references");
11644
15
  }
11645
2.80k
  if (Z_TYPE_P(zend_ast_get_zval(class_ast)) != IS_STRING) {
11646
9
    zend_throw_error(NULL, "Class name must be a valid object or a string");
11647
9
  }
11648
11649
2.80k
  class_name = zend_ast_get_str(class_ast);
11650
2.80k
  fetch_type = zend_get_class_fetch_type(class_name);
11651
11652
2.80k
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11653
5
    zend_error_noreturn(E_COMPILE_ERROR,
11654
5
      "\"static::\" is not allowed in compile-time constants");
11655
5
  }
11656
11657
2.80k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
11658
2.14k
    zend_string *tmp = zend_resolve_class_name_ast(class_ast);
11659
11660
2.14k
    zend_string_release_ex(class_name, 0);
11661
2.14k
    if (tmp != class_name) {
11662
535
      zval *zv = zend_ast_get_zval(class_ast);
11663
535
      ZVAL_STR(zv, tmp);
11664
535
      class_ast->attr = ZEND_NAME_FQ;
11665
535
    }
11666
2.14k
  }
11667
11668
2.80k
  ast->attr |= ZEND_FETCH_CLASS_EXCEPTION;
11669
2.80k
}
11670
/* }}} */
11671
11672
static void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */
11673
47
{
11674
47
  zend_ast *ast = *ast_ptr;
11675
47
  zend_ast *class_ast = ast->child[0];
11676
47
  if (class_ast->kind != ZEND_AST_ZVAL) {
11677
5
    zend_error_noreturn(E_COMPILE_ERROR,
11678
5
      "(expression)::class cannot be used in constant expressions");
11679
5
  }
11680
11681
42
  zend_string *class_name = zend_ast_get_str(class_ast);
11682
42
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
11683
11684
42
  switch (fetch_type) {
11685
37
    case ZEND_FETCH_CLASS_SELF:
11686
37
    case ZEND_FETCH_CLASS_PARENT:
11687
      /* For the const-eval representation store the fetch type instead of the name. */
11688
37
      zend_string_release(class_name);
11689
37
      ast->child[0] = NULL;
11690
37
      ast->attr = fetch_type;
11691
37
      return;
11692
5
    case ZEND_FETCH_CLASS_STATIC:
11693
5
      zend_error_noreturn(E_COMPILE_ERROR,
11694
5
        "static::class cannot be used for compile-time class name resolution");
11695
42
    EMPTY_SWITCH_DEFAULT_CASE()
11696
42
  }
11697
42
}
11698
11699
static void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */
11700
14.2k
{
11701
14.2k
  zend_ast *ast = *ast_ptr;
11702
14.2k
  zend_ast *name_ast = ast->child[0];
11703
14.2k
  zend_string *orig_name = zend_ast_get_str(name_ast);
11704
14.2k
  bool is_fully_qualified;
11705
14.2k
  zval result;
11706
14.2k
  zend_string *resolved_name;
11707
11708
14.2k
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11709
11710
14.2k
  resolved_name = zend_resolve_const_name(
11711
14.2k
    orig_name, name_ast->attr, &is_fully_qualified);
11712
11713
14.2k
  if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
11714
0
    zend_string_release_ex(resolved_name, 0);
11715
0
    zend_ast_destroy(ast);
11716
0
    *ast_ptr = zend_ast_create_zval(&result);
11717
0
    return;
11718
0
  }
11719
11720
14.2k
  zend_ast_destroy(ast);
11721
14.2k
  *ast_ptr = zend_ast_create_constant(resolved_name,
11722
14.2k
    !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0);
11723
14.2k
}
11724
/* }}} */
11725
11726
static void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
11727
238
{
11728
238
  zend_ast *ast = *ast_ptr;
11729
11730
  /* Other cases already resolved by constant folding */
11731
238
  ZEND_ASSERT(ast->attr == T_CLASS_C);
11732
11733
238
  zend_ast_destroy(ast);
11734
238
  *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS);
11735
238
}
11736
/* }}} */
11737
11738
static void zend_compile_const_expr_class_reference(zend_ast *class_ast)
11739
878
{
11740
878
  if (class_ast->kind == ZEND_AST_CLASS) {
11741
0
    zend_error_noreturn(E_COMPILE_ERROR,
11742
0
      "Cannot use anonymous class in constant expression");
11743
0
  }
11744
878
  if (class_ast->kind != ZEND_AST_ZVAL) {
11745
3
    zend_error_noreturn(E_COMPILE_ERROR,
11746
3
      "Cannot use dynamic class name in constant expression");
11747
3
  }
11748
11749
875
  zend_string *class_name = zend_resolve_class_name_ast(class_ast);
11750
875
  int fetch_type = zend_get_class_fetch_type(class_name);
11751
875
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11752
6
    zend_error_noreturn(E_COMPILE_ERROR,
11753
6
      "\"static\" is not allowed in compile-time constants");
11754
6
  }
11755
11756
869
  zval *class_ast_zv = zend_ast_get_zval(class_ast);
11757
869
  zval_ptr_dtor_nogc(class_ast_zv);
11758
869
  ZVAL_STR(class_ast_zv, class_name);
11759
869
  class_ast->attr = fetch_type << ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT;
11760
869
}
11761
11762
static void zend_compile_const_expr_new(zend_ast **ast_ptr)
11763
753
{
11764
753
  zend_ast *class_ast = (*ast_ptr)->child[0];
11765
753
  zend_compile_const_expr_class_reference(class_ast);
11766
11767
753
  const zend_ast *args_ast = (*ast_ptr)->child[1];
11768
753
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
11769
5
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
11770
5
  }
11771
753
}
11772
11773
static void zend_compile_const_expr_closure(zend_ast **ast_ptr)
11774
141
{
11775
141
  zend_ast_decl *closure_ast = (zend_ast_decl *) *ast_ptr;
11776
141
  const zend_ast *uses_ast = closure_ast->child[1];
11777
141
  if (!(closure_ast->flags & ZEND_ACC_STATIC)) {
11778
5
    zend_error_noreturn(E_COMPILE_ERROR,
11779
5
      "Closures in constant expressions must be static");
11780
5
  }
11781
136
  if (uses_ast) {
11782
5
    zend_error_noreturn(E_COMPILE_ERROR,
11783
5
      "Cannot use(...) variables in constant expression");
11784
5
  }
11785
11786
131
  znode node;
11787
131
  zend_op_array *op = zend_compile_func_decl(&node, (zend_ast*)closure_ast, FUNC_DECL_LEVEL_CONSTEXPR);
11788
11789
131
  zend_ast_destroy(*ast_ptr);
11790
131
  *ast_ptr = zend_ast_create_op_array(op);
11791
131
}
11792
11793
static void zend_compile_const_expr_fcc(zend_ast **ast_ptr)
11794
369
{
11795
369
  zend_ast **args_ast;
11796
369
  switch ((*ast_ptr)->kind) {
11797
243
    case ZEND_AST_CALL:
11798
243
      args_ast = &(*ast_ptr)->child[1];
11799
243
      break;
11800
126
    case ZEND_AST_STATIC_CALL:
11801
126
      args_ast = &(*ast_ptr)->child[2];
11802
126
      break;
11803
0
    EMPTY_SWITCH_DEFAULT_CASE();
11804
369
  }
11805
369
  if ((*args_ast)->kind != ZEND_AST_CALLABLE_CONVERT) {
11806
26
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11807
26
  }
11808
343
  ZEND_MAP_PTR_NEW(((zend_ast_fcc *)*args_ast)->fptr);
11809
11810
343
  switch ((*ast_ptr)->kind) {
11811
218
    case ZEND_AST_CALL: {
11812
218
      zend_ast *name_ast = (*ast_ptr)->child[0];
11813
218
      if (name_ast->kind != ZEND_AST_ZVAL) {
11814
14
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic function name in constant expression");
11815
14
      }
11816
204
      zval *name_ast_zv = zend_ast_get_zval(name_ast);
11817
204
      if (Z_TYPE_P(name_ast_zv) != IS_STRING) {
11818
5
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal function name");
11819
5
      }
11820
204
      bool is_fully_qualified;
11821
199
      zend_string *name = zend_resolve_function_name(Z_STR_P(name_ast_zv), name_ast->attr, &is_fully_qualified);
11822
199
      zval_ptr_dtor_nogc(name_ast_zv);
11823
199
      ZVAL_STR(name_ast_zv, name);
11824
199
      if (is_fully_qualified) {
11825
41
        name_ast->attr = ZEND_NAME_FQ;
11826
41
      }
11827
199
      break;
11828
204
    }
11829
125
    case ZEND_AST_STATIC_CALL: {
11830
125
      zend_ast *class_ast = (*ast_ptr)->child[0];
11831
125
      zend_compile_const_expr_class_reference(class_ast);
11832
125
      zend_ast *method_ast = (*ast_ptr)->child[1];
11833
125
      if (method_ast->kind != ZEND_AST_ZVAL) {
11834
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic method name in constant expression");
11835
0
      }
11836
125
      if (Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING) {
11837
0
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal method name");
11838
0
      }
11839
125
      break;
11840
125
    }
11841
125
    EMPTY_SWITCH_DEFAULT_CASE();
11842
343
  }
11843
343
}
11844
11845
static void zend_compile_const_expr_args(zend_ast **ast_ptr)
11846
744
{
11847
744
  zend_ast_list *list = zend_ast_get_list(*ast_ptr);
11848
744
  bool uses_named_args = false;
11849
1.05k
  for (uint32_t i = 0; i < list->children; i++) {
11850
312
    const zend_ast *arg = list->child[i];
11851
312
    if (arg->kind == ZEND_AST_UNPACK) {
11852
1
      zend_error_noreturn(E_COMPILE_ERROR,
11853
1
        "Argument unpacking in constant expressions is not supported");
11854
1
    }
11855
311
    if (arg->kind == ZEND_AST_NAMED_ARG) {
11856
61
      uses_named_args = true;
11857
250
    } else if (uses_named_args) {
11858
1
      zend_error_noreturn(E_COMPILE_ERROR,
11859
1
        "Cannot use positional argument after named argument");
11860
1
    }
11861
311
  }
11862
742
  if (uses_named_args) {
11863
59
    list->attr = 1;
11864
59
  }
11865
742
}
11866
11867
typedef struct {
11868
  /* Whether the value of this expression may differ on each evaluation. */
11869
  bool allow_dynamic;
11870
} const_expr_context;
11871
11872
static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */
11873
148k
{
11874
148k
  const const_expr_context *ctx = context;
11875
148k
  zend_ast *ast = *ast_ptr;
11876
148k
  if (ast == NULL || ast->kind == ZEND_AST_ZVAL) {
11877
109k
    return;
11878
109k
  }
11879
11880
39.1k
  if (!zend_is_allowed_in_const_expr(ast->kind)) {
11881
44
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11882
44
  }
11883
11884
39.0k
  switch (ast->kind) {
11885
2.82k
    case ZEND_AST_CLASS_CONST:
11886
2.82k
      zend_compile_const_expr_class_const(ast_ptr);
11887
2.82k
      break;
11888
47
    case ZEND_AST_CLASS_NAME:
11889
47
      zend_compile_const_expr_class_name(ast_ptr);
11890
47
      break;
11891
14.2k
    case ZEND_AST_CONST:
11892
14.2k
      zend_compile_const_expr_const(ast_ptr);
11893
14.2k
      break;
11894
238
    case ZEND_AST_MAGIC_CONST:
11895
238
      zend_compile_const_expr_magic_const(ast_ptr);
11896
238
      break;
11897
354
    case ZEND_AST_CAST:
11898
354
      if (ast->attr == IS_OBJECT && !ctx->allow_dynamic) {
11899
6
        zend_error_noreturn(E_COMPILE_ERROR,
11900
6
          "Object casts are not supported in this context");
11901
6
      }
11902
348
      break;
11903
759
    case ZEND_AST_NEW:
11904
759
      if (!ctx->allow_dynamic) {
11905
6
        zend_error_noreturn(E_COMPILE_ERROR,
11906
6
          "New expressions are not supported in this context");
11907
6
      }
11908
753
      zend_compile_const_expr_new(ast_ptr);
11909
753
      break;
11910
744
    case ZEND_AST_ARG_LIST:
11911
744
      zend_compile_const_expr_args(ast_ptr);
11912
744
      break;
11913
141
    case ZEND_AST_CLOSURE:
11914
141
      zend_compile_const_expr_closure(ast_ptr);
11915
      /* Return, because we do not want to traverse the children. */
11916
141
      return;
11917
243
    case ZEND_AST_CALL:
11918
369
    case ZEND_AST_STATIC_CALL:
11919
369
      zend_compile_const_expr_fcc(ast_ptr);
11920
369
      break;
11921
39.0k
  }
11922
11923
38.8k
  zend_ast_apply(ast, zend_compile_const_expr, context);
11924
38.8k
}
11925
/* }}} */
11926
11927
void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynamic) /* {{{ */
11928
72.2k
{
11929
72.2k
  const_expr_context context;
11930
72.2k
  context.allow_dynamic = allow_dynamic;
11931
11932
72.2k
  zend_eval_const_expr(ast_ptr);
11933
72.2k
  zend_compile_const_expr(ast_ptr, &context);
11934
72.2k
  if ((*ast_ptr)->kind != ZEND_AST_ZVAL) {
11935
    /* Replace with compiled AST zval representation. */
11936
21.3k
    zval ast_zv;
11937
21.3k
    ZVAL_AST(&ast_zv, zend_ast_copy(*ast_ptr));
11938
21.3k
    zend_ast_destroy(*ast_ptr);
11939
21.3k
    *ast_ptr = zend_ast_create_zval(&ast_zv);
11940
21.3k
  }
11941
72.2k
  ZVAL_COPY(result, zend_ast_get_zval(*ast_ptr));
11942
72.2k
}
11943
/* }}} */
11944
11945
/* Same as compile_stmt, but with early binding */
11946
void zend_compile_top_stmt(zend_ast *ast) /* {{{ */
11947
581k
{
11948
581k
  if (!ast) {
11949
77.1k
    return;
11950
77.1k
  }
11951
11952
504k
  if (ast->kind == ZEND_AST_STMT_LIST) {
11953
91.9k
    const zend_ast_list *list = zend_ast_get_list(ast);
11954
91.9k
    uint32_t i;
11955
599k
    for (i = 0; i < list->children; ++i) {
11956
507k
      zend_compile_top_stmt(list->child[i]);
11957
507k
    }
11958
91.9k
    return;
11959
91.9k
  }
11960
11961
412k
  if (ast->kind == ZEND_AST_FUNC_DECL) {
11962
13.6k
    CG(zend_lineno) = ast->lineno;
11963
13.6k
    zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_TOPLEVEL);
11964
13.6k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11965
398k
  } else if (ast->kind == ZEND_AST_CLASS) {
11966
36.0k
    CG(zend_lineno) = ast->lineno;
11967
36.0k
    zend_compile_class_decl(NULL, ast, true);
11968
36.0k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11969
362k
  } else {
11970
362k
    zend_compile_stmt(ast);
11971
362k
  }
11972
412k
  if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) {
11973
403k
    zend_verify_namespace();
11974
403k
  }
11975
412k
}
11976
/* }}} */
11977
11978
static void zend_compile_stmt(zend_ast *ast) /* {{{ */
11979
4.44M
{
11980
4.44M
  if (!ast) {
11981
282k
    return;
11982
282k
  }
11983
11984
4.16M
  CG(zend_lineno) = ast->lineno;
11985
11986
4.16M
  if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) {
11987
0
    zend_do_extended_stmt(NULL);
11988
0
  }
11989
11990
4.16M
  switch (ast->kind) {
11991
1.03M
    case ZEND_AST_STMT_LIST:
11992
1.03M
      zend_compile_stmt_list(ast);
11993
1.03M
      break;
11994
2.27k
    case ZEND_AST_GLOBAL:
11995
2.27k
      zend_compile_global_var(ast);
11996
2.27k
      break;
11997
31.3k
    case ZEND_AST_STATIC:
11998
31.3k
      zend_compile_static_var(ast);
11999
31.3k
      break;
12000
5.34k
    case ZEND_AST_UNSET:
12001
5.34k
      zend_compile_unset(ast);
12002
5.34k
      break;
12003
31.8k
    case ZEND_AST_RETURN:
12004
31.8k
      zend_compile_return(ast);
12005
31.8k
      break;
12006
910k
    case ZEND_AST_ECHO:
12007
910k
      zend_compile_echo(ast);
12008
910k
      break;
12009
781
    case ZEND_AST_BREAK:
12010
1.33k
    case ZEND_AST_CONTINUE:
12011
1.33k
      zend_compile_break_continue(ast);
12012
1.33k
      break;
12013
1.26k
    case ZEND_AST_GOTO:
12014
1.26k
      zend_compile_goto(ast);
12015
1.26k
      break;
12016
2.59k
    case ZEND_AST_LABEL:
12017
2.59k
      zend_compile_label(ast);
12018
2.59k
      break;
12019
2.59k
    case ZEND_AST_WHILE:
12020
2.59k
      zend_compile_while(ast);
12021
2.59k
      break;
12022
829
    case ZEND_AST_DO_WHILE:
12023
829
      zend_compile_do_while(ast);
12024
829
      break;
12025
9.40k
    case ZEND_AST_FOR:
12026
9.40k
      zend_compile_for(ast);
12027
9.40k
      break;
12028
17.2k
    case ZEND_AST_FOREACH:
12029
17.2k
      zend_compile_foreach(ast);
12030
17.2k
      break;
12031
177k
    case ZEND_AST_IF:
12032
177k
      zend_compile_if(ast);
12033
177k
      break;
12034
8.09k
    case ZEND_AST_SWITCH:
12035
8.09k
      zend_compile_switch(ast);
12036
8.09k
      break;
12037
29.1k
    case ZEND_AST_TRY:
12038
29.1k
      zend_compile_try(ast);
12039
29.1k
      break;
12040
4.84k
    case ZEND_AST_DECLARE:
12041
4.84k
      zend_compile_declare(ast);
12042
4.84k
      break;
12043
3.46k
    case ZEND_AST_FUNC_DECL:
12044
90.6k
    case ZEND_AST_METHOD:
12045
90.6k
      zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_NESTED);
12046
90.6k
      break;
12047
8.25k
    case ZEND_AST_ENUM_CASE:
12048
8.25k
      zend_compile_enum_case(ast);
12049
8.25k
      break;
12050
33.3k
    case ZEND_AST_PROP_GROUP:
12051
33.3k
      zend_compile_prop_group(ast);
12052
33.3k
      break;
12053
8.18k
    case ZEND_AST_CLASS_CONST_GROUP:
12054
8.18k
      zend_compile_class_const_group(ast);
12055
8.18k
      break;
12056
2.28k
    case ZEND_AST_USE_TRAIT:
12057
2.28k
      zend_compile_use_trait(ast);
12058
2.28k
      break;
12059
105k
    case ZEND_AST_CLASS:
12060
105k
      zend_compile_class_decl(NULL, ast, false);
12061
105k
      break;
12062
281
    case ZEND_AST_GROUP_USE:
12063
281
      zend_compile_group_use(ast);
12064
281
      break;
12065
1.32k
    case ZEND_AST_USE:
12066
1.32k
      zend_compile_use(ast);
12067
1.32k
      break;
12068
3.49k
    case ZEND_AST_CONST_DECL:
12069
3.49k
      zend_compile_const_decl(ast);
12070
3.49k
      break;
12071
4.07k
    case ZEND_AST_NAMESPACE:
12072
4.07k
      zend_compile_namespace(ast);
12073
4.07k
      break;
12074
51
    case ZEND_AST_HALT_COMPILER:
12075
51
      zend_compile_halt_compiler(ast);
12076
51
      break;
12077
2.51k
    case ZEND_AST_THROW:
12078
2.51k
      zend_compile_expr(NULL, ast);
12079
2.51k
      break;
12080
301
    case ZEND_AST_CAST_VOID:
12081
301
      zend_compile_void_cast(NULL, ast);
12082
301
      break;
12083
127k
    case ZEND_AST_ASSIGN: {
12084
127k
      znode result;
12085
127k
      zend_compile_assign(&result, ast, /* stmt */ true, BP_VAR_R);
12086
127k
      zend_do_free(&result);
12087
127k
      return;
12088
3.46k
    }
12089
5.71k
    case ZEND_AST_ASSIGN_REF:
12090
5.71k
      zend_compile_assign_ref(NULL, ast, BP_VAR_R);
12091
5.71k
      return;
12092
1.49M
    default:
12093
1.49M
    {
12094
1.49M
      znode result;
12095
1.49M
      zend_compile_expr(&result, ast);
12096
1.49M
      zend_do_free(&result);
12097
1.49M
    }
12098
4.16M
  }
12099
12100
4.02M
  if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) {
12101
30.7k
    zend_emit_tick();
12102
30.7k
  }
12103
4.02M
}
12104
/* }}} */
12105
12106
static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */
12107
17.2M
{
12108
  /* CG(zend_lineno) = ast->lineno; */
12109
17.2M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
12110
12111
17.2M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
12112
118k
    zend_compile_memoized_expr(result, ast, BP_VAR_R);
12113
118k
    return;
12114
118k
  }
12115
12116
17.0M
  switch (ast->kind) {
12117
2.72M
    case ZEND_AST_ZVAL:
12118
2.72M
      ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast));
12119
2.72M
      result->op_type = IS_CONST;
12120
2.72M
      return;
12121
160k
    case ZEND_AST_ZNODE:
12122
160k
      *result = *zend_ast_get_znode(ast);
12123
160k
      return;
12124
433k
    case ZEND_AST_VAR:
12125
493k
    case ZEND_AST_DIM:
12126
520k
    case ZEND_AST_PROP:
12127
580k
    case ZEND_AST_NULLSAFE_PROP:
12128
584k
    case ZEND_AST_STATIC_PROP:
12129
1.87M
    case ZEND_AST_CALL:
12130
1.90M
    case ZEND_AST_METHOD_CALL:
12131
1.91M
    case ZEND_AST_NULLSAFE_METHOD_CALL:
12132
1.92M
    case ZEND_AST_STATIC_CALL:
12133
1.92M
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
12134
1.99M
    case ZEND_AST_PIPE:
12135
1.99M
      zend_compile_var(result, ast, BP_VAR_R, false);
12136
1.99M
      return;
12137
255k
    case ZEND_AST_ASSIGN:
12138
255k
      zend_compile_assign(result, ast, /* stmt */ false, BP_VAR_R);
12139
255k
      return;
12140
732
    case ZEND_AST_ASSIGN_REF:
12141
732
      zend_compile_assign_ref(result, ast, BP_VAR_R);
12142
732
      return;
12143
64.5k
    case ZEND_AST_NEW:
12144
64.5k
      zend_compile_new(result, ast);
12145
64.5k
      return;
12146
95.6k
    case ZEND_AST_ASSIGN_OP:
12147
95.6k
      zend_compile_compound_assign(result, ast);
12148
95.6k
      return;
12149
1.28M
    case ZEND_AST_BINARY_OP:
12150
1.28M
      zend_compile_binary_op(result, ast);
12151
1.28M
      return;
12152
74.4k
    case ZEND_AST_GREATER:
12153
94.0k
    case ZEND_AST_GREATER_EQUAL:
12154
94.0k
      zend_compile_greater(result, ast);
12155
94.0k
      return;
12156
441k
    case ZEND_AST_UNARY_OP:
12157
441k
      zend_compile_unary_op(result, ast);
12158
441k
      return;
12159
29.0k
    case ZEND_AST_UNARY_PLUS:
12160
216k
    case ZEND_AST_UNARY_MINUS:
12161
216k
      zend_compile_unary_pm(result, ast);
12162
216k
      return;
12163
6.44k
    case ZEND_AST_AND:
12164
10.6k
    case ZEND_AST_OR:
12165
10.6k
      zend_compile_short_circuiting(result, ast);
12166
10.6k
      return;
12167
5.85k
    case ZEND_AST_POST_INC:
12168
9.19k
    case ZEND_AST_POST_DEC:
12169
9.19k
      zend_compile_post_incdec(result, ast);
12170
9.19k
      return;
12171
1.64k
    case ZEND_AST_PRE_INC:
12172
2.80k
    case ZEND_AST_PRE_DEC:
12173
2.80k
      zend_compile_pre_incdec(result, ast);
12174
2.80k
      return;
12175
2.61k
    case ZEND_AST_CAST:
12176
2.61k
      zend_compile_cast(result, ast);
12177
2.61k
      return;
12178
7.36k
    case ZEND_AST_CONDITIONAL:
12179
7.36k
      zend_compile_conditional(result, ast);
12180
7.36k
      return;
12181
551k
    case ZEND_AST_COALESCE:
12182
551k
      zend_compile_coalesce(result, ast);
12183
551k
      return;
12184
24.0k
    case ZEND_AST_ASSIGN_COALESCE:
12185
24.0k
      zend_compile_assign_coalesce(result, ast);
12186
24.0k
      return;
12187
3.09k
    case ZEND_AST_PRINT:
12188
3.09k
      zend_compile_print(result, ast);
12189
3.09k
      return;
12190
169k
    case ZEND_AST_YIELD:
12191
169k
      zend_compile_yield(result, ast);
12192
169k
      return;
12193
1.72k
    case ZEND_AST_YIELD_FROM:
12194
1.72k
      zend_compile_yield_from(result, ast);
12195
1.72k
      return;
12196
964
    case ZEND_AST_INSTANCEOF:
12197
964
      zend_compile_instanceof(result, ast);
12198
964
      return;
12199
7.63k
    case ZEND_AST_INCLUDE_OR_EVAL:
12200
7.63k
      zend_compile_include_or_eval(result, ast);
12201
7.63k
      return;
12202
13.1k
    case ZEND_AST_ISSET:
12203
14.0k
    case ZEND_AST_EMPTY:
12204
14.0k
      zend_compile_isset_or_empty(result, ast);
12205
14.0k
      return;
12206
5.13M
    case ZEND_AST_SILENCE:
12207
5.13M
      zend_compile_silence(result, ast);
12208
5.13M
      return;
12209
58.2k
    case ZEND_AST_SHELL_EXEC:
12210
58.2k
      zend_compile_shell_exec(result, ast);
12211
58.2k
      return;
12212
74.6k
    case ZEND_AST_ARRAY:
12213
74.6k
      zend_compile_array(result, ast);
12214
74.6k
      return;
12215
3.03M
    case ZEND_AST_CONST:
12216
3.03M
      zend_compile_const(result, ast);
12217
3.03M
      return;
12218
37.7k
    case ZEND_AST_CLASS_CONST:
12219
37.7k
      zend_compile_class_const(result, ast);
12220
37.7k
      return;
12221
4.19k
    case ZEND_AST_CLASS_NAME:
12222
4.19k
      zend_compile_class_name(result, ast);
12223
4.19k
      return;
12224
86.5k
    case ZEND_AST_ENCAPS_LIST:
12225
86.5k
      zend_compile_encaps_list(result, ast);
12226
86.5k
      return;
12227
22.6k
    case ZEND_AST_MAGIC_CONST:
12228
22.6k
      zend_compile_magic_const(result, ast);
12229
22.6k
      return;
12230
474k
    case ZEND_AST_CLOSURE:
12231
489k
    case ZEND_AST_ARROW_FUNC:
12232
489k
      zend_compile_func_decl(result, ast, FUNC_DECL_LEVEL_NESTED);
12233
489k
      return;
12234
3.22k
    case ZEND_AST_THROW:
12235
3.22k
      zend_compile_throw(result, ast);
12236
3.22k
      return;
12237
2.28k
    case ZEND_AST_MATCH:
12238
2.28k
      zend_compile_match(result, ast);
12239
2.28k
      return;
12240
0
    default:
12241
0
      ZEND_ASSERT(0 /* not supported */);
12242
17.0M
  }
12243
17.0M
}
12244
/* }}} */
12245
12246
static void zend_compile_expr(znode *result, zend_ast *ast)
12247
17.2M
{
12248
17.2M
  zend_check_stack_limit();
12249
12250
17.2M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12251
17.2M
  zend_compile_expr_inner(result, ast);
12252
17.2M
  zend_short_circuiting_commit(checkpoint, result, ast);
12253
17.2M
#if ZEND_DEBUG
12254
17.2M
  if (result) {
12255
    /* BP_VAR_R is not allowed to produce IS_VAR. */
12256
17.2M
    ZEND_ASSERT(result->op_type != IS_VAR);
12257
17.2M
  }
12258
17.2M
#endif
12259
17.2M
}
12260
12261
static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
12262
3.04M
{
12263
3.04M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
12264
12265
3.04M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
12266
79.1k
    switch (ast->kind) {
12267
5.17k
      case ZEND_AST_CALL:
12268
29.7k
      case ZEND_AST_METHOD_CALL:
12269
29.7k
      case ZEND_AST_NULLSAFE_METHOD_CALL:
12270
30.9k
      case ZEND_AST_STATIC_CALL:
12271
30.9k
        zend_compile_memoized_expr(result, ast, BP_VAR_W);
12272
        /* This might not actually produce an opcode, e.g. for expressions evaluated at comptime. */
12273
30.9k
        return NULL;
12274
79.1k
    }
12275
79.1k
  }
12276
12277
3.01M
  switch (ast->kind) {
12278
517k
    case ZEND_AST_VAR:
12279
517k
      return zend_compile_simple_var(result, ast, type, false);
12280
160k
    case ZEND_AST_DIM:
12281
160k
      return zend_compile_dim(result, ast, type, by_ref);
12282
35.1k
    case ZEND_AST_PROP:
12283
95.2k
    case ZEND_AST_NULLSAFE_PROP:
12284
95.2k
      return zend_compile_prop(result, ast, type, by_ref);
12285
7.23k
    case ZEND_AST_STATIC_PROP:
12286
7.23k
      return zend_compile_static_prop(result, ast, type, by_ref, false);
12287
1.45M
    case ZEND_AST_CALL:
12288
1.45M
      zend_compile_call(result, ast, type);
12289
1.45M
      return NULL;
12290
0
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
12291
0
      zend_compile_parent_property_hook_call(result, ast, type);
12292
0
      return NULL;
12293
55.1k
    case ZEND_AST_METHOD_CALL:
12294
57.5k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
12295
57.5k
      zend_compile_method_call(result, ast, type);
12296
57.5k
      return NULL;
12297
14.6k
    case ZEND_AST_STATIC_CALL:
12298
14.6k
      zend_compile_static_call(result, ast, type);
12299
14.6k
      return NULL;
12300
78.0k
    case ZEND_AST_PIPE:
12301
78.0k
      zend_compile_pipe(result, ast, type);
12302
78.0k
      return NULL;
12303
2.21k
    case ZEND_AST_ZNODE:
12304
2.21k
      *result = *zend_ast_get_znode(ast);
12305
2.21k
      return NULL;
12306
598
    case ZEND_AST_ASSIGN_REF:
12307
598
      zend_compile_assign_ref(result, ast, type);
12308
598
      return NULL;
12309
16
    case ZEND_AST_ASSIGN:
12310
16
      zend_compile_assign(result, ast, false, type);
12311
16
      return NULL;
12312
630k
    default:
12313
630k
      if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
12314
173
        zend_error_noreturn(E_COMPILE_ERROR,
12315
173
          "Cannot use temporary expression in write context");
12316
173
      }
12317
12318
629k
      zend_compile_expr(result, ast);
12319
629k
      return NULL;
12320
3.01M
  }
12321
3.01M
}
12322
12323
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12324
3.04M
{
12325
3.04M
  zend_check_stack_limit();
12326
12327
3.04M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12328
3.04M
  zend_op *opcode = zend_compile_var_inner(result, ast, type, by_ref);
12329
3.04M
  zend_short_circuiting_commit(checkpoint, result, ast);
12330
3.04M
#if ZEND_DEBUG
12331
3.04M
  if (result
12332
3.04M
   && (type == BP_VAR_R || type == BP_VAR_IS)
12333
   /* Don't check memoized result, as it will force BP_VAR_W even for BP_VAR_IS. */
12334
2.85M
   && CG(memoize_mode) == ZEND_MEMOIZE_NONE
12335
3.04M
  ) {
12336
    /* BP_VAR_{R,IS} is not allowed to produce IS_VAR. */
12337
2.81M
    ZEND_ASSERT(result->op_type != IS_VAR);
12338
2.81M
  }
12339
3.04M
#endif
12340
3.04M
  return opcode;
12341
3.04M
}
12342
12343
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12344
956k
{
12345
956k
  zend_check_stack_limit();
12346
12347
956k
  switch (ast->kind) {
12348
599k
    case ZEND_AST_VAR:
12349
599k
      return zend_compile_simple_var(result, ast, type, true);
12350
206k
    case ZEND_AST_DIM:
12351
206k
      return zend_delayed_compile_dim(result, ast, type, by_ref);
12352
19.6k
    case ZEND_AST_PROP:
12353
23.0k
    case ZEND_AST_NULLSAFE_PROP:
12354
23.0k
    {
12355
23.0k
      zend_op *opline = zend_delayed_compile_prop(result, ast, type);
12356
23.0k
      if (by_ref) {
12357
851
        opline->extended_value |= ZEND_FETCH_REF;
12358
851
      }
12359
23.0k
      return opline;
12360
19.6k
    }
12361
5.20k
    case ZEND_AST_STATIC_PROP:
12362
5.20k
      return zend_compile_static_prop(result, ast, type, by_ref, true);
12363
122k
    default:
12364
122k
      return zend_compile_var(result, ast, type, false);
12365
956k
  }
12366
956k
}
12367
/* }}} */
12368
12369
bool zend_try_ct_eval_cast(zval *result, uint32_t type, zval *op1)
12370
14.0k
{
12371
  /* NAN warns when casting */
12372
14.0k
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))) {
12373
0
    return false;
12374
0
  }
12375
14.0k
  switch (type) {
12376
49
    case _IS_BOOL:
12377
49
      ZVAL_BOOL(result, zend_is_true(op1));
12378
49
      return true;
12379
336
    case IS_LONG:
12380
336
      if (Z_TYPE_P(op1) == IS_DOUBLE && !ZEND_DOUBLE_FITS_LONG(Z_DVAL_P((op1)))) {
12381
27
        return false;
12382
27
      }
12383
309
      ZVAL_LONG(result, zval_get_long(op1));
12384
309
      return true;
12385
240
    case IS_DOUBLE:
12386
240
      ZVAL_DOUBLE(result, zval_get_double(op1));
12387
240
      return true;
12388
12.6k
    case IS_STRING:
12389
      /* Conversion from double to string takes into account run-time
12390
         'precision' setting and cannot be evaluated at compile-time */
12391
12.6k
      if (Z_TYPE_P(op1) != IS_ARRAY && Z_TYPE_P(op1) != IS_DOUBLE) {
12392
11.6k
        ZVAL_STR(result, zval_get_string(op1));
12393
11.6k
        return true;
12394
11.6k
      }
12395
1.01k
      break;
12396
1.01k
    case IS_ARRAY:
12397
94
      ZVAL_COPY(result, op1);
12398
94
      convert_to_array(result);
12399
94
      return true;
12400
14.0k
  }
12401
1.64k
  return false;
12402
14.0k
}
12403
12404
static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
12405
7.23M
{
12406
7.23M
  zend_ast *ast = *ast_ptr;
12407
7.23M
  zval result;
12408
12409
7.23M
  if (!ast) {
12410
2.21M
    return;
12411
2.21M
  }
12412
12413
5.01M
  zend_check_stack_limit();
12414
12415
5.01M
  switch (ast->kind) {
12416
54.2k
    case ZEND_AST_BINARY_OP:
12417
54.2k
      zend_eval_const_expr(&ast->child[0]);
12418
54.2k
      zend_eval_const_expr(&ast->child[1]);
12419
54.2k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12420
26.9k
        return;
12421
26.9k
      }
12422
12423
27.2k
      if (!zend_try_ct_eval_binary_op(&result, ast->attr,
12424
27.2k
          zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
12425
27.2k
      ) {
12426
13.7k
        return;
12427
13.7k
      }
12428
13.5k
      break;
12429
13.5k
    case ZEND_AST_GREATER:
12430
3.37k
    case ZEND_AST_GREATER_EQUAL:
12431
3.37k
      zend_eval_const_expr(&ast->child[0]);
12432
3.37k
      zend_eval_const_expr(&ast->child[1]);
12433
3.37k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12434
2.30k
        return;
12435
2.30k
      }
12436
12437
1.07k
      zend_ct_eval_greater(&result, ast->kind,
12438
1.07k
        zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]));
12439
1.07k
      break;
12440
1.56k
    case ZEND_AST_AND:
12441
2.61k
    case ZEND_AST_OR:
12442
2.61k
    {
12443
2.61k
      bool child0_is_true, child1_is_true;
12444
2.61k
      zend_eval_const_expr(&ast->child[0]);
12445
2.61k
      zend_eval_const_expr(&ast->child[1]);
12446
2.61k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12447
1.30k
        return;
12448
1.30k
      }
12449
12450
1.30k
      child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0]));
12451
1.30k
      if (child0_is_true == (ast->kind == ZEND_AST_OR)) {
12452
257
        ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR);
12453
257
        break;
12454
257
      }
12455
12456
1.05k
      if (ast->child[1]->kind != ZEND_AST_ZVAL) {
12457
280
        return;
12458
280
      }
12459
12460
771
      child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1]));
12461
771
      if (ast->kind == ZEND_AST_OR) {
12462
74
        ZVAL_BOOL(&result, child0_is_true || child1_is_true);
12463
697
      } else {
12464
697
        ZVAL_BOOL(&result, child0_is_true && child1_is_true);
12465
697
      }
12466
771
      break;
12467
1.05k
    }
12468
5.07k
    case ZEND_AST_UNARY_OP:
12469
5.07k
      zend_eval_const_expr(&ast->child[0]);
12470
5.07k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12471
1.73k
        return;
12472
1.73k
      }
12473
12474
3.34k
      if (!zend_try_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12475
491
        return;
12476
491
      }
12477
2.85k
      break;
12478
3.27k
    case ZEND_AST_UNARY_PLUS:
12479
17.5k
    case ZEND_AST_UNARY_MINUS:
12480
17.5k
      zend_eval_const_expr(&ast->child[0]);
12481
17.5k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12482
2.24k
        return;
12483
2.24k
      }
12484
12485
15.2k
      if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) {
12486
3.28k
        return;
12487
3.28k
      }
12488
11.9k
      break;
12489
22.5k
    case ZEND_AST_COALESCE:
12490
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12491
22.5k
      if (ast->child[0]->kind == ZEND_AST_DIM) {
12492
473
        ast->child[0]->attr |= ZEND_DIM_IS;
12493
473
      }
12494
22.5k
      zend_eval_const_expr(&ast->child[0]);
12495
12496
22.5k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12497
        /* ensure everything was compile-time evaluated at least once */
12498
22.2k
        zend_eval_const_expr(&ast->child[1]);
12499
22.2k
        return;
12500
22.2k
      }
12501
12502
261
      if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) {
12503
200
        zend_eval_const_expr(&ast->child[1]);
12504
200
        *ast_ptr = ast->child[1];
12505
200
        ast->child[1] = NULL;
12506
200
        zend_ast_destroy(ast);
12507
200
      } else {
12508
61
        *ast_ptr = ast->child[0];
12509
61
        ast->child[0] = NULL;
12510
61
        zend_ast_destroy(ast);
12511
61
      }
12512
261
      return;
12513
2.97k
    case ZEND_AST_CONDITIONAL:
12514
2.97k
    {
12515
2.97k
      zend_ast **child, *child_ast;
12516
2.97k
      zend_eval_const_expr(&ast->child[0]);
12517
2.97k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12518
        /* ensure everything was compile-time evaluated at least once */
12519
2.69k
        if (ast->child[1]) {
12520
1.82k
          zend_eval_const_expr(&ast->child[1]);
12521
1.82k
        }
12522
2.69k
        zend_eval_const_expr(&ast->child[2]);
12523
2.69k
        return;
12524
2.69k
      }
12525
12526
275
      child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];
12527
275
      if (*child == NULL) {
12528
215
        child--;
12529
215
      }
12530
275
      child_ast = *child;
12531
275
      *child = NULL;
12532
275
      zend_ast_destroy(ast);
12533
275
      *ast_ptr = child_ast;
12534
275
      zend_eval_const_expr(ast_ptr);
12535
275
      return;
12536
2.97k
    }
12537
907k
    case ZEND_AST_DIM:
12538
907k
    {
12539
      /* constant expression should be always read context ... */
12540
907k
      const zval *container, *dim;
12541
12542
907k
      if (ast->child[1] == NULL) {
12543
34
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
12544
34
      }
12545
12546
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12547
907k
      if ((ast->attr & ZEND_DIM_IS) && ast->child[0]->kind == ZEND_AST_DIM) {
12548
1.37k
        ast->child[0]->attr |= ZEND_DIM_IS;
12549
1.37k
      }
12550
12551
907k
      zend_eval_const_expr(&ast->child[0]);
12552
907k
      zend_eval_const_expr(&ast->child[1]);
12553
907k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12554
895k
        return;
12555
895k
      }
12556
12557
12.1k
      container = zend_ast_get_zval(ast->child[0]);
12558
12.1k
      dim = zend_ast_get_zval(ast->child[1]);
12559
12560
12.1k
      if (Z_TYPE_P(container) == IS_ARRAY) {
12561
7.68k
        zval *el;
12562
7.68k
        if (Z_TYPE_P(dim) == IS_LONG) {
12563
787
          el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim));
12564
787
          if (el) {
12565
438
            ZVAL_COPY(&result, el);
12566
438
          } else {
12567
349
            return;
12568
349
          }
12569
6.90k
        } else if (Z_TYPE_P(dim) == IS_STRING) {
12570
6.71k
          el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim));
12571
6.71k
          if (el) {
12572
220
            ZVAL_COPY(&result, el);
12573
6.49k
          } else {
12574
6.49k
            return;
12575
6.49k
          }
12576
6.71k
        } else {
12577
183
          return; /* warning... handle at runtime */
12578
183
        }
12579
7.68k
      } else if (Z_TYPE_P(container) == IS_STRING) {
12580
4.21k
        zend_long offset;
12581
4.21k
        uint8_t c;
12582
4.21k
        if (Z_TYPE_P(dim) == IS_LONG) {
12583
3.56k
          offset = Z_LVAL_P(dim);
12584
3.56k
        } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) {
12585
466
          return;
12586
466
        }
12587
3.75k
        if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
12588
3.65k
          return;
12589
3.65k
        }
12590
94
        c = (uint8_t) Z_STRVAL_P(container)[offset];
12591
94
        ZVAL_CHAR(&result, c);
12592
277
      } else if (Z_TYPE_P(container) <= IS_FALSE) {
12593
8
        return; /* warning... handle at runtime */
12594
269
      } else {
12595
269
        return;
12596
269
      }
12597
752
      break;
12598
12.1k
    }
12599
1.91M
    case ZEND_AST_ARRAY:
12600
1.91M
      if (!zend_try_ct_eval_array(&result, ast)) {
12601
1.88M
        return;
12602
1.88M
      }
12603
29.0k
      break;
12604
29.0k
    case ZEND_AST_MAGIC_CONST:
12605
1.19k
      if (!zend_try_ct_eval_magic_const(&result, ast)) {
12606
238
        return;
12607
238
      }
12608
952
      break;
12609
86.2k
    case ZEND_AST_CONST:
12610
86.2k
    {
12611
86.2k
      zend_ast *name_ast = ast->child[0];
12612
86.2k
      bool is_fully_qualified;
12613
86.2k
      zend_string *resolved_name = zend_resolve_const_name(
12614
86.2k
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
12615
12616
86.2k
      if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
12617
81.5k
        zend_string_release_ex(resolved_name, 0);
12618
81.5k
        return;
12619
81.5k
      }
12620
12621
4.66k
      zend_string_release_ex(resolved_name, 0);
12622
4.66k
      break;
12623
86.2k
    }
12624
228k
    case ZEND_AST_CLASS_CONST:
12625
228k
    {
12626
228k
      zend_ast *class_ast;
12627
228k
      zend_ast *name_ast;
12628
228k
      zend_string *resolved_name;
12629
12630
228k
      zend_eval_const_expr(&ast->child[0]);
12631
228k
      zend_eval_const_expr(&ast->child[1]);
12632
12633
228k
      if (UNEXPECTED(ast->child[1]->kind != ZEND_AST_ZVAL
12634
228k
        || Z_TYPE_P(zend_ast_get_zval(ast->child[1])) != IS_STRING)) {
12635
736
        return;
12636
736
      }
12637
12638
228k
      class_ast = ast->child[0];
12639
228k
      name_ast = ast->child[1];
12640
12641
228k
      if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) {
12642
219k
        return;
12643
219k
      }
12644
12645
9.12k
      resolved_name = zend_resolve_class_name_ast(class_ast);
12646
9.12k
      if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) {
12647
8.93k
        zend_string_release_ex(resolved_name, 0);
12648
8.93k
        return;
12649
8.93k
      }
12650
12651
184
      zend_string_release_ex(resolved_name, 0);
12652
184
      break;
12653
9.12k
    }
12654
2.54k
    case ZEND_AST_CLASS_NAME:
12655
2.54k
    {
12656
2.54k
      zend_ast *class_ast = ast->child[0];
12657
2.54k
      if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) {
12658
1.04k
        return;
12659
1.04k
      }
12660
1.49k
      break;
12661
2.54k
    }
12662
    // TODO: We should probably use zend_ast_apply to recursively walk nodes without
12663
    // special handling. It is required that all nodes that are part of a const expr
12664
    // are visited. Probably we should be distinguishing evaluation of const expr and
12665
    // normal exprs here.
12666
3.03k
    case ZEND_AST_ARG_LIST:
12667
3.03k
    {
12668
3.03k
      zend_ast_list *list = zend_ast_get_list(ast);
12669
3.91k
      for (uint32_t i = 0; i < list->children; i++) {
12670
884
        zend_eval_const_expr(&list->child[i]);
12671
884
      }
12672
3.03k
      return;
12673
2.54k
    }
12674
3.03k
    case ZEND_AST_NEW:
12675
3.03k
      zend_eval_const_expr(&ast->child[0]);
12676
3.03k
      zend_eval_const_expr(&ast->child[1]);
12677
3.03k
      return;
12678
158
    case ZEND_AST_NAMED_ARG:
12679
158
      zend_eval_const_expr(&ast->child[1]);
12680
158
      return;
12681
8.23k
    case ZEND_AST_CONST_ENUM_INIT:
12682
8.23k
      zend_eval_const_expr(&ast->child[3]);
12683
8.23k
      return;
12684
464
    case ZEND_AST_PROP:
12685
705
    case ZEND_AST_NULLSAFE_PROP:
12686
705
      zend_eval_const_expr(&ast->child[0]);
12687
705
      zend_eval_const_expr(&ast->child[1]);
12688
705
      return;
12689
3.60k
    case ZEND_AST_CAST:
12690
3.60k
      if (ast->attr == IS_NULL) {
12691
5
        zend_error_noreturn(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
12692
5
      }
12693
3.59k
      zend_eval_const_expr(&ast->child[0]);
12694
3.59k
      if (ast->child[0]->kind == ZEND_AST_ZVAL
12695
1.50k
       && zend_try_ct_eval_cast(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12696
797
        break;
12697
797
      }
12698
2.79k
      return;
12699
1.74M
    default:
12700
1.74M
      return;
12701
5.01M
  }
12702
12703
67.8k
  zend_ast_destroy(ast);
12704
67.8k
  *ast_ptr = zend_ast_create_zval(&result);
12705
67.8k
}
12706
/* }}} */